xref: /freebsd/sys/dev/ste/if_ste.c (revision c0270e602aa2d06a209d2faccadb85bcec40adb1)
1c8befdd5SWarner Losh /*-
2c8befdd5SWarner Losh  * Copyright (c) 1997, 1998, 1999
3c8befdd5SWarner Losh  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4c8befdd5SWarner Losh  *
5c8befdd5SWarner Losh  * Redistribution and use in source and binary forms, with or without
6c8befdd5SWarner Losh  * modification, are permitted provided that the following conditions
7c8befdd5SWarner Losh  * are met:
8c8befdd5SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9c8befdd5SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10c8befdd5SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11c8befdd5SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12c8befdd5SWarner Losh  *    documentation and/or other materials provided with the distribution.
13c8befdd5SWarner Losh  * 3. All advertising materials mentioning features or use of this software
14c8befdd5SWarner Losh  *    must display the following acknowledgement:
15c8befdd5SWarner Losh  *	This product includes software developed by Bill Paul.
16c8befdd5SWarner Losh  * 4. Neither the name of the author nor the names of any co-contributors
17c8befdd5SWarner Losh  *    may be used to endorse or promote products derived from this software
18c8befdd5SWarner Losh  *    without specific prior written permission.
19c8befdd5SWarner Losh  *
20c8befdd5SWarner Losh  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21c8befdd5SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c8befdd5SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c8befdd5SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24c8befdd5SWarner Losh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25c8befdd5SWarner Losh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26c8befdd5SWarner Losh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27c8befdd5SWarner Losh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28c8befdd5SWarner Losh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29c8befdd5SWarner Losh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30c8befdd5SWarner Losh  * THE POSSIBILITY OF SUCH DAMAGE.
31c8befdd5SWarner Losh  */
32c8befdd5SWarner Losh 
33c8befdd5SWarner Losh #include <sys/cdefs.h>
34c8befdd5SWarner Losh __FBSDID("$FreeBSD$");
35c8befdd5SWarner Losh 
36c8befdd5SWarner Losh #ifdef HAVE_KERNEL_OPTION_HEADERS
37c8befdd5SWarner Losh #include "opt_device_polling.h"
38c8befdd5SWarner Losh #endif
39c8befdd5SWarner Losh 
40c8befdd5SWarner Losh #include <sys/param.h>
41c8befdd5SWarner Losh #include <sys/systm.h>
42a1b2c209SPyun YongHyeon #include <sys/bus.h>
43a1b2c209SPyun YongHyeon #include <sys/endian.h>
44c8befdd5SWarner Losh #include <sys/kernel.h>
45a1b2c209SPyun YongHyeon #include <sys/lock.h>
46a1b2c209SPyun YongHyeon #include <sys/malloc.h>
47a1b2c209SPyun YongHyeon #include <sys/mbuf.h>
48c8befdd5SWarner Losh #include <sys/module.h>
49a1b2c209SPyun YongHyeon #include <sys/rman.h>
50c8befdd5SWarner Losh #include <sys/socket.h>
51a1b2c209SPyun YongHyeon #include <sys/sockio.h>
52c8befdd5SWarner Losh #include <sys/sysctl.h>
53c8befdd5SWarner Losh 
54a1b2c209SPyun YongHyeon #include <net/bpf.h>
55c8befdd5SWarner Losh #include <net/if.h>
56c8befdd5SWarner Losh #include <net/if_arp.h>
57c8befdd5SWarner Losh #include <net/ethernet.h>
58c8befdd5SWarner Losh #include <net/if_dl.h>
59c8befdd5SWarner Losh #include <net/if_media.h>
60c8befdd5SWarner Losh #include <net/if_types.h>
61c8befdd5SWarner Losh #include <net/if_vlan_var.h>
62c8befdd5SWarner Losh 
63c8befdd5SWarner Losh #include <machine/bus.h>
64c8befdd5SWarner Losh #include <machine/resource.h>
65c8befdd5SWarner Losh 
66c8befdd5SWarner Losh #include <dev/mii/mii.h>
67c8befdd5SWarner Losh #include <dev/mii/miivar.h>
68c8befdd5SWarner Losh 
69c8befdd5SWarner Losh #include <dev/pci/pcireg.h>
70c8befdd5SWarner Losh #include <dev/pci/pcivar.h>
71c8befdd5SWarner Losh 
72a1b2c209SPyun YongHyeon #include <dev/ste/if_stereg.h>
73a1b2c209SPyun YongHyeon 
74c8befdd5SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
75c8befdd5SWarner Losh #include "miibus_if.h"
76c8befdd5SWarner Losh 
77c8befdd5SWarner Losh MODULE_DEPEND(ste, pci, 1, 1, 1);
78c8befdd5SWarner Losh MODULE_DEPEND(ste, ether, 1, 1, 1);
79c8befdd5SWarner Losh MODULE_DEPEND(ste, miibus, 1, 1, 1);
80c8befdd5SWarner Losh 
81c8befdd5SWarner Losh /*
82c8befdd5SWarner Losh  * Various supported device vendors/types and their names.
83c8befdd5SWarner Losh  */
84c8befdd5SWarner Losh static struct ste_type ste_devs[] = {
85c8befdd5SWarner Losh 	{ ST_VENDORID, ST_DEVICEID_ST201_1, "Sundance ST201 10/100BaseTX" },
86c8befdd5SWarner Losh 	{ ST_VENDORID, ST_DEVICEID_ST201_2, "Sundance ST201 10/100BaseTX" },
87c8befdd5SWarner Losh 	{ DL_VENDORID, DL_DEVICEID_DL10050, "D-Link DL10050 10/100BaseTX" },
88c8befdd5SWarner Losh 	{ 0, 0, NULL }
89c8befdd5SWarner Losh };
90c8befdd5SWarner Losh 
91c8befdd5SWarner Losh static int	ste_attach(device_t);
92c8befdd5SWarner Losh static int	ste_detach(device_t);
93084dc54bSPyun YongHyeon static int	ste_probe(device_t);
94c8befdd5SWarner Losh static int	ste_shutdown(device_t);
95084dc54bSPyun YongHyeon 
96a1b2c209SPyun YongHyeon static int	ste_dma_alloc(struct ste_softc *);
97a1b2c209SPyun YongHyeon static void	ste_dma_free(struct ste_softc *);
98a1b2c209SPyun YongHyeon static void	ste_dmamap_cb(void *, bus_dma_segment_t *, int, int);
99084dc54bSPyun YongHyeon static int 	ste_eeprom_wait(struct ste_softc *);
100a1b2c209SPyun YongHyeon static int	ste_encap(struct ste_softc *, struct mbuf **,
101a1b2c209SPyun YongHyeon 		    struct ste_chain *);
102c8befdd5SWarner Losh static int	ste_ifmedia_upd(struct ifnet *);
103c8befdd5SWarner Losh static void	ste_ifmedia_upd_locked(struct ifnet *);
104c8befdd5SWarner Losh static void	ste_ifmedia_sts(struct ifnet *, struct ifmediareq *);
105084dc54bSPyun YongHyeon static void	ste_init(void *);
106084dc54bSPyun YongHyeon static void	ste_init_locked(struct ste_softc *);
107c8befdd5SWarner Losh static int	ste_init_rx_list(struct ste_softc *);
108c8befdd5SWarner Losh static void	ste_init_tx_list(struct ste_softc *);
109084dc54bSPyun YongHyeon static void	ste_intr(void *);
110084dc54bSPyun YongHyeon static int	ste_ioctl(struct ifnet *, u_long, caddr_t);
111084dc54bSPyun YongHyeon static int	ste_mii_readreg(struct ste_softc *, struct ste_mii_frame *);
112084dc54bSPyun YongHyeon static void	ste_mii_send(struct ste_softc *, uint32_t, int);
113084dc54bSPyun YongHyeon static void	ste_mii_sync(struct ste_softc *);
114084dc54bSPyun YongHyeon static int	ste_mii_writereg(struct ste_softc *, struct ste_mii_frame *);
115084dc54bSPyun YongHyeon static int	ste_miibus_readreg(device_t, int, int);
116084dc54bSPyun YongHyeon static void	ste_miibus_statchg(device_t);
117084dc54bSPyun YongHyeon static int	ste_miibus_writereg(device_t, int, int, int);
118a1b2c209SPyun YongHyeon static int	ste_newbuf(struct ste_softc *, struct ste_chain_onefrag *);
119084dc54bSPyun YongHyeon static int	ste_read_eeprom(struct ste_softc *, caddr_t, int, int, int);
120084dc54bSPyun YongHyeon static void	ste_reset(struct ste_softc *);
121a1b2c209SPyun YongHyeon static int	ste_rxeof(struct ste_softc *, int);
122084dc54bSPyun YongHyeon static void	ste_setmulti(struct ste_softc *);
123084dc54bSPyun YongHyeon static void	ste_start(struct ifnet *);
124084dc54bSPyun YongHyeon static void	ste_start_locked(struct ifnet *);
12510f695eeSPyun YongHyeon static void	ste_stats_update(struct ste_softc *);
126084dc54bSPyun YongHyeon static void	ste_stop(struct ste_softc *);
12710f695eeSPyun YongHyeon static void	ste_tick(void *);
128084dc54bSPyun YongHyeon static void	ste_txeoc(struct ste_softc *);
129084dc54bSPyun YongHyeon static void	ste_txeof(struct ste_softc *);
130084dc54bSPyun YongHyeon static void	ste_wait(struct ste_softc *);
131084dc54bSPyun YongHyeon static void	ste_watchdog(struct ste_softc *);
132c8befdd5SWarner Losh 
133c8befdd5SWarner Losh static device_method_t ste_methods[] = {
134c8befdd5SWarner Losh 	/* Device interface */
135c8befdd5SWarner Losh 	DEVMETHOD(device_probe,		ste_probe),
136c8befdd5SWarner Losh 	DEVMETHOD(device_attach,	ste_attach),
137c8befdd5SWarner Losh 	DEVMETHOD(device_detach,	ste_detach),
138c8befdd5SWarner Losh 	DEVMETHOD(device_shutdown,	ste_shutdown),
139c8befdd5SWarner Losh 
140c8befdd5SWarner Losh 	/* bus interface */
141c8befdd5SWarner Losh 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
142c8befdd5SWarner Losh 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
143c8befdd5SWarner Losh 
144c8befdd5SWarner Losh 	/* MII interface */
145c8befdd5SWarner Losh 	DEVMETHOD(miibus_readreg,	ste_miibus_readreg),
146c8befdd5SWarner Losh 	DEVMETHOD(miibus_writereg,	ste_miibus_writereg),
147c8befdd5SWarner Losh 	DEVMETHOD(miibus_statchg,	ste_miibus_statchg),
148c8befdd5SWarner Losh 
149c8befdd5SWarner Losh 	{ 0, 0 }
150c8befdd5SWarner Losh };
151c8befdd5SWarner Losh 
152c8befdd5SWarner Losh static driver_t ste_driver = {
153c8befdd5SWarner Losh 	"ste",
154c8befdd5SWarner Losh 	ste_methods,
155c8befdd5SWarner Losh 	sizeof(struct ste_softc)
156c8befdd5SWarner Losh };
157c8befdd5SWarner Losh 
158c8befdd5SWarner Losh static devclass_t ste_devclass;
159c8befdd5SWarner Losh 
160c8befdd5SWarner Losh DRIVER_MODULE(ste, pci, ste_driver, ste_devclass, 0, 0);
161c8befdd5SWarner Losh DRIVER_MODULE(miibus, ste, miibus_driver, miibus_devclass, 0, 0);
162c8befdd5SWarner Losh 
163c8befdd5SWarner Losh #define STE_SETBIT4(sc, reg, x)				\
164c8befdd5SWarner Losh 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
165c8befdd5SWarner Losh 
166c8befdd5SWarner Losh #define STE_CLRBIT4(sc, reg, x)				\
167c8befdd5SWarner Losh 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
168c8befdd5SWarner Losh 
169c8befdd5SWarner Losh #define STE_SETBIT2(sc, reg, x)				\
170c8befdd5SWarner Losh 	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | (x))
171c8befdd5SWarner Losh 
172c8befdd5SWarner Losh #define STE_CLRBIT2(sc, reg, x)				\
173c8befdd5SWarner Losh 	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~(x))
174c8befdd5SWarner Losh 
175c8befdd5SWarner Losh #define STE_SETBIT1(sc, reg, x)				\
176c8befdd5SWarner Losh 	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | (x))
177c8befdd5SWarner Losh 
178c8befdd5SWarner Losh #define STE_CLRBIT1(sc, reg, x)				\
179c8befdd5SWarner Losh 	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~(x))
180c8befdd5SWarner Losh 
181c8befdd5SWarner Losh 
182c8befdd5SWarner Losh #define MII_SET(x)		STE_SETBIT1(sc, STE_PHYCTL, x)
183c8befdd5SWarner Losh #define MII_CLR(x)		STE_CLRBIT1(sc, STE_PHYCTL, x)
184c8befdd5SWarner Losh 
185c8befdd5SWarner Losh /*
186c8befdd5SWarner Losh  * Sync the PHYs by setting data bit and strobing the clock 32 times.
187c8befdd5SWarner Losh  */
188c8befdd5SWarner Losh static void
18960270842SPyun YongHyeon ste_mii_sync(struct ste_softc *sc)
190c8befdd5SWarner Losh {
19142306cb0SPyun YongHyeon 	int i;
192c8befdd5SWarner Losh 
193c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA);
194c8befdd5SWarner Losh 
195c8befdd5SWarner Losh 	for (i = 0; i < 32; i++) {
196c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
197c8befdd5SWarner Losh 		DELAY(1);
198c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
199c8befdd5SWarner Losh 		DELAY(1);
200c8befdd5SWarner Losh 	}
201c8befdd5SWarner Losh }
202c8befdd5SWarner Losh 
203c8befdd5SWarner Losh /*
204c8befdd5SWarner Losh  * Clock a series of bits through the MII.
205c8befdd5SWarner Losh  */
206c8befdd5SWarner Losh static void
20756af54f2SPyun YongHyeon ste_mii_send(struct ste_softc *sc, uint32_t bits, int cnt)
208c8befdd5SWarner Losh {
209c8befdd5SWarner Losh 	int i;
210c8befdd5SWarner Losh 
211c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
212c8befdd5SWarner Losh 
213c8befdd5SWarner Losh 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
214c8befdd5SWarner Losh 		if (bits & i) {
215c8befdd5SWarner Losh 			MII_SET(STE_PHYCTL_MDATA);
216c8befdd5SWarner Losh                 } else {
217c8befdd5SWarner Losh 			MII_CLR(STE_PHYCTL_MDATA);
218c8befdd5SWarner Losh                 }
219c8befdd5SWarner Losh 		DELAY(1);
220c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
221c8befdd5SWarner Losh 		DELAY(1);
222c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
223c8befdd5SWarner Losh 	}
224c8befdd5SWarner Losh }
225c8befdd5SWarner Losh 
226c8befdd5SWarner Losh /*
227c8befdd5SWarner Losh  * Read an PHY register through the MII.
228c8befdd5SWarner Losh  */
229c8befdd5SWarner Losh static int
23060270842SPyun YongHyeon ste_mii_readreg(struct ste_softc *sc, struct ste_mii_frame *frame)
231c8befdd5SWarner Losh {
232c8befdd5SWarner Losh 	int i, ack;
233c8befdd5SWarner Losh 
234c8befdd5SWarner Losh 	/*
235c8befdd5SWarner Losh 	 * Set up frame for RX.
236c8befdd5SWarner Losh 	 */
237c8befdd5SWarner Losh 	frame->mii_stdelim = STE_MII_STARTDELIM;
238c8befdd5SWarner Losh 	frame->mii_opcode = STE_MII_READOP;
239c8befdd5SWarner Losh 	frame->mii_turnaround = 0;
240c8befdd5SWarner Losh 	frame->mii_data = 0;
241c8befdd5SWarner Losh 
242c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_PHYCTL, 0);
243c8befdd5SWarner Losh 	/*
244c8befdd5SWarner Losh  	 * Turn on data xmit.
245c8befdd5SWarner Losh 	 */
246c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR);
247c8befdd5SWarner Losh 
248c8befdd5SWarner Losh 	ste_mii_sync(sc);
249c8befdd5SWarner Losh 
250c8befdd5SWarner Losh 	/*
251c8befdd5SWarner Losh 	 * Send command/address info.
252c8befdd5SWarner Losh 	 */
253c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_stdelim, 2);
254c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_opcode, 2);
255c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_phyaddr, 5);
256c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_regaddr, 5);
257c8befdd5SWarner Losh 
258c8befdd5SWarner Losh 	/* Turn off xmit. */
259c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MDIR);
260c8befdd5SWarner Losh 
261c8befdd5SWarner Losh 	/* Idle bit */
262c8befdd5SWarner Losh 	MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA));
263c8befdd5SWarner Losh 	DELAY(1);
264c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
265c8befdd5SWarner Losh 	DELAY(1);
266c8befdd5SWarner Losh 
267c8befdd5SWarner Losh 	/* Check for ack */
268c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
269c8befdd5SWarner Losh 	DELAY(1);
270c8befdd5SWarner Losh 	ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA;
271c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
272c8befdd5SWarner Losh 	DELAY(1);
273c8befdd5SWarner Losh 
274c8befdd5SWarner Losh 	/*
275c8befdd5SWarner Losh 	 * Now try reading data bits. If the ack failed, we still
276c8befdd5SWarner Losh 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
277c8befdd5SWarner Losh 	 */
278c8befdd5SWarner Losh 	if (ack) {
279c8befdd5SWarner Losh 		for (i = 0; i < 16; i++) {
280c8befdd5SWarner Losh 			MII_CLR(STE_PHYCTL_MCLK);
281c8befdd5SWarner Losh 			DELAY(1);
282c8befdd5SWarner Losh 			MII_SET(STE_PHYCTL_MCLK);
283c8befdd5SWarner Losh 			DELAY(1);
284c8befdd5SWarner Losh 		}
285c8befdd5SWarner Losh 		goto fail;
286c8befdd5SWarner Losh 	}
287c8befdd5SWarner Losh 
288c8befdd5SWarner Losh 	for (i = 0x8000; i; i >>= 1) {
289c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
290c8befdd5SWarner Losh 		DELAY(1);
291c8befdd5SWarner Losh 		if (!ack) {
292c8befdd5SWarner Losh 			if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA)
293c8befdd5SWarner Losh 				frame->mii_data |= i;
294c8befdd5SWarner Losh 			DELAY(1);
295c8befdd5SWarner Losh 		}
296c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
297c8befdd5SWarner Losh 		DELAY(1);
298c8befdd5SWarner Losh 	}
299c8befdd5SWarner Losh 
300c8befdd5SWarner Losh fail:
301c8befdd5SWarner Losh 
302c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
303c8befdd5SWarner Losh 	DELAY(1);
304c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
305c8befdd5SWarner Losh 	DELAY(1);
306c8befdd5SWarner Losh 
307c8befdd5SWarner Losh 	if (ack)
308c8befdd5SWarner Losh 		return (1);
309c8befdd5SWarner Losh 	return (0);
310c8befdd5SWarner Losh }
311c8befdd5SWarner Losh 
312c8befdd5SWarner Losh /*
313c8befdd5SWarner Losh  * Write to a PHY register through the MII.
314c8befdd5SWarner Losh  */
315c8befdd5SWarner Losh static int
31660270842SPyun YongHyeon ste_mii_writereg(struct ste_softc *sc, struct ste_mii_frame *frame)
317c8befdd5SWarner Losh {
318c8befdd5SWarner Losh 
319c8befdd5SWarner Losh 	/*
320c8befdd5SWarner Losh 	 * Set up frame for TX.
321c8befdd5SWarner Losh 	 */
322c8befdd5SWarner Losh 
323c8befdd5SWarner Losh 	frame->mii_stdelim = STE_MII_STARTDELIM;
324c8befdd5SWarner Losh 	frame->mii_opcode = STE_MII_WRITEOP;
325c8befdd5SWarner Losh 	frame->mii_turnaround = STE_MII_TURNAROUND;
326c8befdd5SWarner Losh 
327c8befdd5SWarner Losh 	/*
328c8befdd5SWarner Losh  	 * Turn on data output.
329c8befdd5SWarner Losh 	 */
330c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR);
331c8befdd5SWarner Losh 
332c8befdd5SWarner Losh 	ste_mii_sync(sc);
333c8befdd5SWarner Losh 
334c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_stdelim, 2);
335c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_opcode, 2);
336c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_phyaddr, 5);
337c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_regaddr, 5);
338c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_turnaround, 2);
339c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_data, 16);
340c8befdd5SWarner Losh 
341c8befdd5SWarner Losh 	/* Idle bit. */
342c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
343c8befdd5SWarner Losh 	DELAY(1);
344c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
345c8befdd5SWarner Losh 	DELAY(1);
346c8befdd5SWarner Losh 
347c8befdd5SWarner Losh 	/*
348c8befdd5SWarner Losh 	 * Turn off xmit.
349c8befdd5SWarner Losh 	 */
350c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MDIR);
351c8befdd5SWarner Losh 
352c8befdd5SWarner Losh 	return (0);
353c8befdd5SWarner Losh }
354c8befdd5SWarner Losh 
355c8befdd5SWarner Losh static int
35660270842SPyun YongHyeon ste_miibus_readreg(device_t dev, int phy, int reg)
357c8befdd5SWarner Losh {
358c8befdd5SWarner Losh 	struct ste_softc *sc;
359c8befdd5SWarner Losh 	struct ste_mii_frame frame;
360c8befdd5SWarner Losh 
361c8befdd5SWarner Losh 	sc = device_get_softc(dev);
362c8befdd5SWarner Losh 
3634465097bSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_ONE_PHY) != 0 && phy != 0)
364c8befdd5SWarner Losh 		return (0);
365c8befdd5SWarner Losh 
366c8befdd5SWarner Losh 	bzero((char *)&frame, sizeof(frame));
367c8befdd5SWarner Losh 
368c8befdd5SWarner Losh 	frame.mii_phyaddr = phy;
369c8befdd5SWarner Losh 	frame.mii_regaddr = reg;
370c8befdd5SWarner Losh 	ste_mii_readreg(sc, &frame);
371c8befdd5SWarner Losh 
372c8befdd5SWarner Losh 	return (frame.mii_data);
373c8befdd5SWarner Losh }
374c8befdd5SWarner Losh 
375c8befdd5SWarner Losh static int
37660270842SPyun YongHyeon ste_miibus_writereg(device_t dev, int phy, int reg, int data)
377c8befdd5SWarner Losh {
378c8befdd5SWarner Losh 	struct ste_softc *sc;
379c8befdd5SWarner Losh 	struct ste_mii_frame frame;
380c8befdd5SWarner Losh 
381c8befdd5SWarner Losh 	sc = device_get_softc(dev);
382c8befdd5SWarner Losh 	bzero((char *)&frame, sizeof(frame));
383c8befdd5SWarner Losh 
384c8befdd5SWarner Losh 	frame.mii_phyaddr = phy;
385c8befdd5SWarner Losh 	frame.mii_regaddr = reg;
386c8befdd5SWarner Losh 	frame.mii_data = data;
387c8befdd5SWarner Losh 
388c8befdd5SWarner Losh 	ste_mii_writereg(sc, &frame);
389c8befdd5SWarner Losh 
390c8befdd5SWarner Losh 	return (0);
391c8befdd5SWarner Losh }
392c8befdd5SWarner Losh 
393c8befdd5SWarner Losh static void
39460270842SPyun YongHyeon ste_miibus_statchg(device_t dev)
395c8befdd5SWarner Losh {
396c8befdd5SWarner Losh 	struct ste_softc *sc;
397c8befdd5SWarner Losh 	struct mii_data *mii;
39810f695eeSPyun YongHyeon 	struct ifnet *ifp;
39910f695eeSPyun YongHyeon 	uint16_t cfg;
400c8befdd5SWarner Losh 
401c8befdd5SWarner Losh 	sc = device_get_softc(dev);
402c8befdd5SWarner Losh 
403c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
40410f695eeSPyun YongHyeon 	ifp = sc->ste_ifp;
40510f695eeSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
40610f695eeSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
40710f695eeSPyun YongHyeon 		return;
408c8befdd5SWarner Losh 
40910f695eeSPyun YongHyeon 	sc->ste_flags &= ~STE_FLAG_LINK;
41010f695eeSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
41110f695eeSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
41210f695eeSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
41310f695eeSPyun YongHyeon 		case IFM_10_T:
41410f695eeSPyun YongHyeon 		case IFM_100_TX:
41510f695eeSPyun YongHyeon 		case IFM_100_FX:
41610f695eeSPyun YongHyeon 		case IFM_100_T4:
41710f695eeSPyun YongHyeon 			sc->ste_flags |= STE_FLAG_LINK;
41810f695eeSPyun YongHyeon 		default:
41910f695eeSPyun YongHyeon 			break;
42010f695eeSPyun YongHyeon 		}
42110f695eeSPyun YongHyeon 	}
42210f695eeSPyun YongHyeon 
42310f695eeSPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
42410f695eeSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_LINK) != 0) {
42510f695eeSPyun YongHyeon 		cfg = CSR_READ_2(sc, STE_MACCTL0);
42610f695eeSPyun YongHyeon 		cfg &= ~(STE_MACCTL0_FLOWCTL_ENABLE | STE_MACCTL0_FULLDUPLEX);
42710f695eeSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
42810f695eeSPyun YongHyeon 			/*
42910f695eeSPyun YongHyeon 			 * ST201 data sheet says driver should enable receiving
43010f695eeSPyun YongHyeon 			 * MAC control frames bit of receive mode register to
43110f695eeSPyun YongHyeon 			 * receive flow-control frames but the register has no
43210f695eeSPyun YongHyeon 			 * such bits. In addition the controller has no ability
43310f695eeSPyun YongHyeon 			 * to send pause frames so it should be handled in
43410f695eeSPyun YongHyeon 			 * driver. Implementing pause timer handling in driver
43510f695eeSPyun YongHyeon 			 * layer is not trivial, so don't enable flow-control
43610f695eeSPyun YongHyeon 			 * here.
43710f695eeSPyun YongHyeon 			 */
43810f695eeSPyun YongHyeon 			cfg |= STE_MACCTL0_FULLDUPLEX;
43910f695eeSPyun YongHyeon 		}
44010f695eeSPyun YongHyeon 		CSR_WRITE_2(sc, STE_MACCTL0, cfg);
441c8befdd5SWarner Losh 	}
442c8befdd5SWarner Losh }
443c8befdd5SWarner Losh 
444c8befdd5SWarner Losh static int
44560270842SPyun YongHyeon ste_ifmedia_upd(struct ifnet *ifp)
446c8befdd5SWarner Losh {
447c8befdd5SWarner Losh 	struct ste_softc *sc;
448c8befdd5SWarner Losh 
449c8befdd5SWarner Losh 	sc = ifp->if_softc;
450c8befdd5SWarner Losh 	STE_LOCK(sc);
451c8befdd5SWarner Losh 	ste_ifmedia_upd_locked(ifp);
452c8befdd5SWarner Losh 	STE_UNLOCK(sc);
453c8befdd5SWarner Losh 
454c8befdd5SWarner Losh 	return (0);
455c8befdd5SWarner Losh }
456c8befdd5SWarner Losh 
457c8befdd5SWarner Losh static void
45860270842SPyun YongHyeon ste_ifmedia_upd_locked(struct ifnet *ifp)
459c8befdd5SWarner Losh {
460c8befdd5SWarner Losh 	struct ste_softc *sc;
461c8befdd5SWarner Losh 	struct mii_data *mii;
462c8befdd5SWarner Losh 
463c8befdd5SWarner Losh 	sc = ifp->if_softc;
464c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
465c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
4664465097bSPyun YongHyeon 	sc->ste_flags &= ~STE_FLAG_LINK;
467c8befdd5SWarner Losh 	if (mii->mii_instance) {
468c8befdd5SWarner Losh 		struct mii_softc	*miisc;
469c8befdd5SWarner Losh 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
470c8befdd5SWarner Losh 			mii_phy_reset(miisc);
471c8befdd5SWarner Losh 	}
472c8befdd5SWarner Losh 	mii_mediachg(mii);
473c8befdd5SWarner Losh }
474c8befdd5SWarner Losh 
475c8befdd5SWarner Losh static void
47660270842SPyun YongHyeon ste_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
477c8befdd5SWarner Losh {
478c8befdd5SWarner Losh 	struct ste_softc *sc;
479c8befdd5SWarner Losh 	struct mii_data *mii;
480c8befdd5SWarner Losh 
481c8befdd5SWarner Losh 	sc = ifp->if_softc;
482c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
483c8befdd5SWarner Losh 
484c8befdd5SWarner Losh 	STE_LOCK(sc);
485c8befdd5SWarner Losh 	mii_pollstat(mii);
486c8befdd5SWarner Losh 	ifmr->ifm_active = mii->mii_media_active;
487c8befdd5SWarner Losh 	ifmr->ifm_status = mii->mii_media_status;
488c8befdd5SWarner Losh 	STE_UNLOCK(sc);
489c8befdd5SWarner Losh }
490c8befdd5SWarner Losh 
491c8befdd5SWarner Losh static void
49260270842SPyun YongHyeon ste_wait(struct ste_softc *sc)
493c8befdd5SWarner Losh {
49442306cb0SPyun YongHyeon 	int i;
495c8befdd5SWarner Losh 
496c8befdd5SWarner Losh 	for (i = 0; i < STE_TIMEOUT; i++) {
497c8befdd5SWarner Losh 		if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG))
498c8befdd5SWarner Losh 			break;
4991bf71544SPyun YongHyeon 		DELAY(1);
500c8befdd5SWarner Losh 	}
501c8befdd5SWarner Losh 
502c8befdd5SWarner Losh 	if (i == STE_TIMEOUT)
503c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "command never completed!\n");
504c8befdd5SWarner Losh }
505c8befdd5SWarner Losh 
506c8befdd5SWarner Losh /*
507c8befdd5SWarner Losh  * The EEPROM is slow: give it time to come ready after issuing
508c8befdd5SWarner Losh  * it a command.
509c8befdd5SWarner Losh  */
510c8befdd5SWarner Losh static int
51160270842SPyun YongHyeon ste_eeprom_wait(struct ste_softc *sc)
512c8befdd5SWarner Losh {
513c8befdd5SWarner Losh 	int i;
514c8befdd5SWarner Losh 
515c8befdd5SWarner Losh 	DELAY(1000);
516c8befdd5SWarner Losh 
517c8befdd5SWarner Losh 	for (i = 0; i < 100; i++) {
518c8befdd5SWarner Losh 		if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY)
519c8befdd5SWarner Losh 			DELAY(1000);
520c8befdd5SWarner Losh 		else
521c8befdd5SWarner Losh 			break;
522c8befdd5SWarner Losh 	}
523c8befdd5SWarner Losh 
524c8befdd5SWarner Losh 	if (i == 100) {
525c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "eeprom failed to come ready\n");
526c8befdd5SWarner Losh 		return (1);
527c8befdd5SWarner Losh 	}
528c8befdd5SWarner Losh 
529c8befdd5SWarner Losh 	return (0);
530c8befdd5SWarner Losh }
531c8befdd5SWarner Losh 
532c8befdd5SWarner Losh /*
533c8befdd5SWarner Losh  * Read a sequence of words from the EEPROM. Note that ethernet address
534c8befdd5SWarner Losh  * data is stored in the EEPROM in network byte order.
535c8befdd5SWarner Losh  */
536c8befdd5SWarner Losh static int
53760270842SPyun YongHyeon ste_read_eeprom(struct ste_softc *sc, caddr_t dest, int off, int cnt, int swap)
538c8befdd5SWarner Losh {
539f2632c3bSPyun YongHyeon 	uint16_t word, *ptr;
540c8befdd5SWarner Losh 	int err = 0, i;
541c8befdd5SWarner Losh 
542c8befdd5SWarner Losh 	if (ste_eeprom_wait(sc))
543c8befdd5SWarner Losh 		return (1);
544c8befdd5SWarner Losh 
545c8befdd5SWarner Losh 	for (i = 0; i < cnt; i++) {
546c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i));
547c8befdd5SWarner Losh 		err = ste_eeprom_wait(sc);
548c8befdd5SWarner Losh 		if (err)
549c8befdd5SWarner Losh 			break;
550c8befdd5SWarner Losh 		word = CSR_READ_2(sc, STE_EEPROM_DATA);
55156af54f2SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
552c8befdd5SWarner Losh 		if (swap)
553c8befdd5SWarner Losh 			*ptr = ntohs(word);
554c8befdd5SWarner Losh 		else
555c8befdd5SWarner Losh 			*ptr = word;
556c8befdd5SWarner Losh 	}
557c8befdd5SWarner Losh 
558c8befdd5SWarner Losh 	return (err ? 1 : 0);
559c8befdd5SWarner Losh }
560c8befdd5SWarner Losh 
561c8befdd5SWarner Losh static void
56260270842SPyun YongHyeon ste_setmulti(struct ste_softc *sc)
563c8befdd5SWarner Losh {
564c8befdd5SWarner Losh 	struct ifnet *ifp;
565c8befdd5SWarner Losh 	struct ifmultiaddr *ifma;
566f2632c3bSPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
567f2632c3bSPyun YongHyeon 	int h;
568c8befdd5SWarner Losh 
569c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
570c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
571c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
572c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
573c8befdd5SWarner Losh 		return;
574c8befdd5SWarner Losh 	}
575c8befdd5SWarner Losh 
576c8befdd5SWarner Losh 	/* first, zot all the existing hash bits */
577c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR0, 0);
578c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR1, 0);
579c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR2, 0);
580c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR3, 0);
581c8befdd5SWarner Losh 
582c8befdd5SWarner Losh 	/* now program new ones */
583eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
584c8befdd5SWarner Losh 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
585c8befdd5SWarner Losh 		if (ifma->ifma_addr->sa_family != AF_LINK)
586c8befdd5SWarner Losh 			continue;
587c8befdd5SWarner Losh 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
588c8befdd5SWarner Losh 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x3F;
589c8befdd5SWarner Losh 		if (h < 32)
590c8befdd5SWarner Losh 			hashes[0] |= (1 << h);
591c8befdd5SWarner Losh 		else
592c8befdd5SWarner Losh 			hashes[1] |= (1 << (h - 32));
593c8befdd5SWarner Losh 	}
594eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
595c8befdd5SWarner Losh 
596c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR0, hashes[0] & 0xFFFF);
597c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR1, (hashes[0] >> 16) & 0xFFFF);
598c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR2, hashes[1] & 0xFFFF);
599c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR3, (hashes[1] >> 16) & 0xFFFF);
600c8befdd5SWarner Losh 	STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
601c8befdd5SWarner Losh 	STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
602c8befdd5SWarner Losh }
603c8befdd5SWarner Losh 
604c8befdd5SWarner Losh #ifdef DEVICE_POLLING
605c8befdd5SWarner Losh static poll_handler_t ste_poll, ste_poll_locked;
606c8befdd5SWarner Losh 
6071abcdbd1SAttilio Rao static int
608c8befdd5SWarner Losh ste_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
609c8befdd5SWarner Losh {
610c8befdd5SWarner Losh 	struct ste_softc *sc = ifp->if_softc;
6111abcdbd1SAttilio Rao 	int rx_npkts = 0;
612c8befdd5SWarner Losh 
613c8befdd5SWarner Losh 	STE_LOCK(sc);
614c8befdd5SWarner Losh 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6151abcdbd1SAttilio Rao 		rx_npkts = ste_poll_locked(ifp, cmd, count);
616c8befdd5SWarner Losh 	STE_UNLOCK(sc);
6171abcdbd1SAttilio Rao 	return (rx_npkts);
618c8befdd5SWarner Losh }
619c8befdd5SWarner Losh 
6201abcdbd1SAttilio Rao static int
621c8befdd5SWarner Losh ste_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
622c8befdd5SWarner Losh {
623c8befdd5SWarner Losh 	struct ste_softc *sc = ifp->if_softc;
6241abcdbd1SAttilio Rao 	int rx_npkts;
625c8befdd5SWarner Losh 
626c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
627c8befdd5SWarner Losh 
628a1b2c209SPyun YongHyeon 	rx_npkts = ste_rxeof(sc, count);
629c8befdd5SWarner Losh 	ste_txeof(sc);
630c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
631c8befdd5SWarner Losh 		ste_start_locked(ifp);
632c8befdd5SWarner Losh 
633c8befdd5SWarner Losh 	if (cmd == POLL_AND_CHECK_STATUS) {
63456af54f2SPyun YongHyeon 		uint16_t status;
635c8befdd5SWarner Losh 
636c8befdd5SWarner Losh 		status = CSR_READ_2(sc, STE_ISR_ACK);
637c8befdd5SWarner Losh 
638c8befdd5SWarner Losh 		if (status & STE_ISR_TX_DONE)
639c8befdd5SWarner Losh 			ste_txeoc(sc);
640c8befdd5SWarner Losh 
64110f695eeSPyun YongHyeon 		if (status & STE_ISR_STATS_OFLOW)
642c8befdd5SWarner Losh 			ste_stats_update(sc);
643c8befdd5SWarner Losh 
6448d9f6dd9SPyun YongHyeon 		if (status & STE_ISR_HOSTERR)
645c8befdd5SWarner Losh 			ste_init_locked(sc);
646c8befdd5SWarner Losh 	}
6471abcdbd1SAttilio Rao 	return (rx_npkts);
648c8befdd5SWarner Losh }
649c8befdd5SWarner Losh #endif /* DEVICE_POLLING */
650c8befdd5SWarner Losh 
651c8befdd5SWarner Losh static void
65260270842SPyun YongHyeon ste_intr(void *xsc)
653c8befdd5SWarner Losh {
654c8befdd5SWarner Losh 	struct ste_softc *sc;
655c8befdd5SWarner Losh 	struct ifnet *ifp;
65656af54f2SPyun YongHyeon 	uint16_t status;
657c8befdd5SWarner Losh 
658c8befdd5SWarner Losh 	sc = xsc;
659c8befdd5SWarner Losh 	STE_LOCK(sc);
660c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
661c8befdd5SWarner Losh 
662c8befdd5SWarner Losh #ifdef DEVICE_POLLING
663c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING) {
664c8befdd5SWarner Losh 		STE_UNLOCK(sc);
665c8befdd5SWarner Losh 		return;
666c8befdd5SWarner Losh 	}
667c8befdd5SWarner Losh #endif
668c8befdd5SWarner Losh 
669c8befdd5SWarner Losh 	/* See if this is really our interrupt. */
670c8befdd5SWarner Losh 	if (!(CSR_READ_2(sc, STE_ISR) & STE_ISR_INTLATCH)) {
671c8befdd5SWarner Losh 		STE_UNLOCK(sc);
672c8befdd5SWarner Losh 		return;
673c8befdd5SWarner Losh 	}
674c8befdd5SWarner Losh 
675c8befdd5SWarner Losh 	for (;;) {
676c8befdd5SWarner Losh 		status = CSR_READ_2(sc, STE_ISR_ACK);
677c8befdd5SWarner Losh 
678c8befdd5SWarner Losh 		if (!(status & STE_INTRS))
679c8befdd5SWarner Losh 			break;
680c8befdd5SWarner Losh 
681a1b2c209SPyun YongHyeon 		if (status & STE_ISR_RX_DMADONE)
682a1b2c209SPyun YongHyeon 			ste_rxeof(sc, -1);
683c8befdd5SWarner Losh 
684c8befdd5SWarner Losh 		if (status & STE_ISR_TX_DMADONE)
685c8befdd5SWarner Losh 			ste_txeof(sc);
686c8befdd5SWarner Losh 
687c8befdd5SWarner Losh 		if (status & STE_ISR_TX_DONE)
688c8befdd5SWarner Losh 			ste_txeoc(sc);
689c8befdd5SWarner Losh 
69010f695eeSPyun YongHyeon 		if (status & STE_ISR_STATS_OFLOW)
691c8befdd5SWarner Losh 			ste_stats_update(sc);
692c8befdd5SWarner Losh 
6938d9f6dd9SPyun YongHyeon 		if (status & STE_ISR_HOSTERR)
694c8befdd5SWarner Losh 			ste_init_locked(sc);
695c8befdd5SWarner Losh 	}
696c8befdd5SWarner Losh 
697c8befdd5SWarner Losh 	/* Re-enable interrupts */
698c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
699c8befdd5SWarner Losh 
700c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
701c8befdd5SWarner Losh 		ste_start_locked(ifp);
702c8befdd5SWarner Losh 
703c8befdd5SWarner Losh 	STE_UNLOCK(sc);
704c8befdd5SWarner Losh }
705c8befdd5SWarner Losh 
706c8befdd5SWarner Losh /*
707c8befdd5SWarner Losh  * A frame has been uploaded: pass the resulting mbuf chain up to
708c8befdd5SWarner Losh  * the higher level protocols.
709c8befdd5SWarner Losh  */
7101abcdbd1SAttilio Rao static int
711a1b2c209SPyun YongHyeon ste_rxeof(struct ste_softc *sc, int count)
712c8befdd5SWarner Losh {
713c8befdd5SWarner Losh         struct mbuf *m;
714c8befdd5SWarner Losh         struct ifnet *ifp;
715c8befdd5SWarner Losh 	struct ste_chain_onefrag *cur_rx;
71656af54f2SPyun YongHyeon 	uint32_t rxstat;
717a1b2c209SPyun YongHyeon 	int total_len, rx_npkts;
718c8befdd5SWarner Losh 
719c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
720c8befdd5SWarner Losh 
721a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
722a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map,
723a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
724c8befdd5SWarner Losh 
725c8befdd5SWarner Losh 	cur_rx = sc->ste_cdata.ste_rx_head;
726a1b2c209SPyun YongHyeon 	for (rx_npkts = 0; rx_npkts < STE_RX_LIST_CNT; rx_npkts++,
727a1b2c209SPyun YongHyeon 	    cur_rx = cur_rx->ste_next) {
728a1b2c209SPyun YongHyeon 		rxstat = le32toh(cur_rx->ste_ptr->ste_status);
729a1b2c209SPyun YongHyeon 		if ((rxstat & STE_RXSTAT_DMADONE) == 0)
730a1b2c209SPyun YongHyeon 			break;
731a1b2c209SPyun YongHyeon #ifdef DEVICE_POLLING
732a1b2c209SPyun YongHyeon 		if (ifp->if_capenable & IFCAP_POLLING) {
733a1b2c209SPyun YongHyeon 			if (count == 0)
734a1b2c209SPyun YongHyeon 				break;
735a1b2c209SPyun YongHyeon 			count--;
736a1b2c209SPyun YongHyeon 		}
737a1b2c209SPyun YongHyeon #endif
738a1b2c209SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
739a1b2c209SPyun YongHyeon 			break;
740c8befdd5SWarner Losh 		/*
741c8befdd5SWarner Losh 		 * If an error occurs, update stats, clear the
742c8befdd5SWarner Losh 		 * status word and leave the mbuf cluster in place:
743c8befdd5SWarner Losh 		 * it should simply get re-used next time this descriptor
744c8befdd5SWarner Losh 	 	 * comes up in the ring.
745c8befdd5SWarner Losh 		 */
746c8befdd5SWarner Losh 		if (rxstat & STE_RXSTAT_FRAME_ERR) {
747c8befdd5SWarner Losh 			ifp->if_ierrors++;
748c8befdd5SWarner Losh 			cur_rx->ste_ptr->ste_status = 0;
749c8befdd5SWarner Losh 			continue;
750c8befdd5SWarner Losh 		}
751c8befdd5SWarner Losh 
752c8befdd5SWarner Losh 		/* No errors; receive the packet. */
753c8befdd5SWarner Losh 		m = cur_rx->ste_mbuf;
754a1b2c209SPyun YongHyeon 		total_len = STE_RX_BYTES(rxstat);
755c8befdd5SWarner Losh 
756c8befdd5SWarner Losh 		/*
757c8befdd5SWarner Losh 		 * Try to conjure up a new mbuf cluster. If that
758c8befdd5SWarner Losh 		 * fails, it means we have an out of memory condition and
759c8befdd5SWarner Losh 		 * should leave the buffer in place and continue. This will
760c8befdd5SWarner Losh 		 * result in a lost packet, but there's little else we
761c8befdd5SWarner Losh 		 * can do in this situation.
762c8befdd5SWarner Losh 		 */
763a1b2c209SPyun YongHyeon 		if (ste_newbuf(sc, cur_rx) != 0) {
764c8befdd5SWarner Losh 			ifp->if_ierrors++;
765c8befdd5SWarner Losh 			cur_rx->ste_ptr->ste_status = 0;
766c8befdd5SWarner Losh 			continue;
767c8befdd5SWarner Losh 		}
768c8befdd5SWarner Losh 
769c8befdd5SWarner Losh 		m->m_pkthdr.rcvif = ifp;
770c8befdd5SWarner Losh 		m->m_pkthdr.len = m->m_len = total_len;
771c8befdd5SWarner Losh 
772c8befdd5SWarner Losh 		ifp->if_ipackets++;
773c8befdd5SWarner Losh 		STE_UNLOCK(sc);
774c8befdd5SWarner Losh 		(*ifp->if_input)(ifp, m);
775c8befdd5SWarner Losh 		STE_LOCK(sc);
776a1b2c209SPyun YongHyeon 	}
777c8befdd5SWarner Losh 
778a1b2c209SPyun YongHyeon 	if (rx_npkts > 0) {
779a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_head = cur_rx;
780a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
781a1b2c209SPyun YongHyeon 		    sc->ste_cdata.ste_rx_list_map,
782a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
783c8befdd5SWarner Losh 	}
784c8befdd5SWarner Losh 
7851abcdbd1SAttilio Rao 	return (rx_npkts);
786c8befdd5SWarner Losh }
787c8befdd5SWarner Losh 
788c8befdd5SWarner Losh static void
78960270842SPyun YongHyeon ste_txeoc(struct ste_softc *sc)
790c8befdd5SWarner Losh {
791c8befdd5SWarner Losh 	struct ifnet *ifp;
792f2632c3bSPyun YongHyeon 	uint8_t txstat;
793c8befdd5SWarner Losh 
794c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
795c8befdd5SWarner Losh 
796c8befdd5SWarner Losh 	while ((txstat = CSR_READ_1(sc, STE_TX_STATUS)) &
797c8befdd5SWarner Losh 	    STE_TXSTATUS_TXDONE) {
798c8befdd5SWarner Losh 		if (txstat & STE_TXSTATUS_UNDERRUN ||
799c8befdd5SWarner Losh 		    txstat & STE_TXSTATUS_EXCESSCOLLS ||
800c8befdd5SWarner Losh 		    txstat & STE_TXSTATUS_RECLAIMERR) {
801c8befdd5SWarner Losh 			ifp->if_oerrors++;
802c8befdd5SWarner Losh 			device_printf(sc->ste_dev,
803c8befdd5SWarner Losh 			    "transmission error: %x\n", txstat);
804c8befdd5SWarner Losh 
805c8befdd5SWarner Losh 			ste_init_locked(sc);
806c8befdd5SWarner Losh 
807c8befdd5SWarner Losh 			if (txstat & STE_TXSTATUS_UNDERRUN &&
808c8befdd5SWarner Losh 			    sc->ste_tx_thresh < STE_PACKET_SIZE) {
809c8befdd5SWarner Losh 				sc->ste_tx_thresh += STE_MIN_FRAMELEN;
810c8befdd5SWarner Losh 				device_printf(sc->ste_dev,
811c8befdd5SWarner Losh 				    "tx underrun, increasing tx"
812c8befdd5SWarner Losh 				    " start threshold to %d bytes\n",
813c8befdd5SWarner Losh 				    sc->ste_tx_thresh);
814c8befdd5SWarner Losh 			}
815c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
816c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_TX_RECLAIM_THRESH,
817c8befdd5SWarner Losh 			    (STE_PACKET_SIZE >> 4));
818c8befdd5SWarner Losh 		}
819c8befdd5SWarner Losh 		ste_init_locked(sc);
820c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_TX_STATUS, txstat);
821c8befdd5SWarner Losh 	}
822c8befdd5SWarner Losh }
823c8befdd5SWarner Losh 
824c8befdd5SWarner Losh static void
82510f695eeSPyun YongHyeon ste_tick(void *arg)
82610f695eeSPyun YongHyeon {
82710f695eeSPyun YongHyeon 	struct ste_softc *sc;
82810f695eeSPyun YongHyeon 	struct mii_data *mii;
82910f695eeSPyun YongHyeon 
83010f695eeSPyun YongHyeon 	sc = (struct ste_softc *)arg;
83110f695eeSPyun YongHyeon 
83210f695eeSPyun YongHyeon 	STE_LOCK_ASSERT(sc);
83310f695eeSPyun YongHyeon 
83410f695eeSPyun YongHyeon 	mii = device_get_softc(sc->ste_miibus);
83510f695eeSPyun YongHyeon 	mii_tick(mii);
83610f695eeSPyun YongHyeon 	/*
83710f695eeSPyun YongHyeon 	 * ukphy(4) does not seem to generate CB that reports
83810f695eeSPyun YongHyeon 	 * resolved link state so if we know we lost a link,
83910f695eeSPyun YongHyeon 	 * explicitly check the link state.
84010f695eeSPyun YongHyeon 	 */
84110f695eeSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_LINK) == 0)
84210f695eeSPyun YongHyeon 		ste_miibus_statchg(sc->ste_dev);
84310f695eeSPyun YongHyeon 	ste_stats_update(sc);
84410f695eeSPyun YongHyeon 	ste_watchdog(sc);
84510f695eeSPyun YongHyeon 	callout_reset(&sc->ste_callout, hz, ste_tick, sc);
84610f695eeSPyun YongHyeon }
84710f695eeSPyun YongHyeon 
84810f695eeSPyun YongHyeon static void
84960270842SPyun YongHyeon ste_txeof(struct ste_softc *sc)
850c8befdd5SWarner Losh {
851c8befdd5SWarner Losh 	struct ifnet *ifp;
852f2632c3bSPyun YongHyeon 	struct ste_chain *cur_tx;
853a1b2c209SPyun YongHyeon 	uint32_t txstat;
854c8befdd5SWarner Losh 	int idx;
855c8befdd5SWarner Losh 
856a1b2c209SPyun YongHyeon 	STE_LOCK_ASSERT(sc);
857c8befdd5SWarner Losh 
858a1b2c209SPyun YongHyeon 	ifp = sc->ste_ifp;
859c8befdd5SWarner Losh 	idx = sc->ste_cdata.ste_tx_cons;
860a1b2c209SPyun YongHyeon 	if (idx == sc->ste_cdata.ste_tx_prod)
861a1b2c209SPyun YongHyeon 		return;
862a1b2c209SPyun YongHyeon 
863a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
864a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map,
865a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
866a1b2c209SPyun YongHyeon 
867c8befdd5SWarner Losh 	while (idx != sc->ste_cdata.ste_tx_prod) {
868c8befdd5SWarner Losh 		cur_tx = &sc->ste_cdata.ste_tx_chain[idx];
869a1b2c209SPyun YongHyeon 		txstat = le32toh(cur_tx->ste_ptr->ste_ctl);
870a1b2c209SPyun YongHyeon 		if ((txstat & STE_TXCTL_DMADONE) == 0)
871c8befdd5SWarner Losh 			break;
872a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map,
873a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
874a1b2c209SPyun YongHyeon 		bus_dmamap_unload(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map);
875a1b2c209SPyun YongHyeon 		KASSERT(cur_tx->ste_mbuf != NULL,
876a1b2c209SPyun YongHyeon 		    ("%s: freeing NULL mbuf!\n", __func__));
877c8befdd5SWarner Losh 		m_freem(cur_tx->ste_mbuf);
878c8befdd5SWarner Losh 		cur_tx->ste_mbuf = NULL;
879c8befdd5SWarner Losh 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
880c8befdd5SWarner Losh 		ifp->if_opackets++;
881a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_cnt--;
882c8befdd5SWarner Losh 		STE_INC(idx, STE_TX_LIST_CNT);
883c8befdd5SWarner Losh 	}
884c8befdd5SWarner Losh 
885c8befdd5SWarner Losh 	sc->ste_cdata.ste_tx_cons = idx;
886a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_cnt == 0)
8877cf545d0SJohn Baldwin 		sc->ste_timer = 0;
888c8befdd5SWarner Losh }
889c8befdd5SWarner Losh 
890c8befdd5SWarner Losh static void
89110f695eeSPyun YongHyeon ste_stats_update(struct ste_softc *sc)
892c8befdd5SWarner Losh {
893c8befdd5SWarner Losh 	struct ifnet *ifp;
894c8befdd5SWarner Losh 
895c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
896c8befdd5SWarner Losh 
897c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
898c8befdd5SWarner Losh 	ifp->if_collisions += CSR_READ_1(sc, STE_LATE_COLLS)
899c8befdd5SWarner Losh 	    + CSR_READ_1(sc, STE_MULTI_COLLS)
900c8befdd5SWarner Losh 	    + CSR_READ_1(sc, STE_SINGLE_COLLS);
901c8befdd5SWarner Losh }
902c8befdd5SWarner Losh 
903c8befdd5SWarner Losh /*
904c8befdd5SWarner Losh  * Probe for a Sundance ST201 chip. Check the PCI vendor and device
905c8befdd5SWarner Losh  * IDs against our list and return a device name if we find a match.
906c8befdd5SWarner Losh  */
907c8befdd5SWarner Losh static int
90860270842SPyun YongHyeon ste_probe(device_t dev)
909c8befdd5SWarner Losh {
910c8befdd5SWarner Losh 	struct ste_type *t;
911c8befdd5SWarner Losh 
912c8befdd5SWarner Losh 	t = ste_devs;
913c8befdd5SWarner Losh 
914c8befdd5SWarner Losh 	while (t->ste_name != NULL) {
915c8befdd5SWarner Losh 		if ((pci_get_vendor(dev) == t->ste_vid) &&
916c8befdd5SWarner Losh 		    (pci_get_device(dev) == t->ste_did)) {
917c8befdd5SWarner Losh 			device_set_desc(dev, t->ste_name);
918c8befdd5SWarner Losh 			return (BUS_PROBE_DEFAULT);
919c8befdd5SWarner Losh 		}
920c8befdd5SWarner Losh 		t++;
921c8befdd5SWarner Losh 	}
922c8befdd5SWarner Losh 
923c8befdd5SWarner Losh 	return (ENXIO);
924c8befdd5SWarner Losh }
925c8befdd5SWarner Losh 
926c8befdd5SWarner Losh /*
927c8befdd5SWarner Losh  * Attach the interface. Allocate softc structures, do ifmedia
928c8befdd5SWarner Losh  * setup and ethernet/BPF attach.
929c8befdd5SWarner Losh  */
930c8befdd5SWarner Losh static int
93160270842SPyun YongHyeon ste_attach(device_t dev)
932c8befdd5SWarner Losh {
933c8befdd5SWarner Losh 	struct ste_softc *sc;
934c8befdd5SWarner Losh 	struct ifnet *ifp;
935c8befdd5SWarner Losh 	u_char eaddr[6];
936f2632c3bSPyun YongHyeon 	int error = 0, rid;
937c8befdd5SWarner Losh 
938c8befdd5SWarner Losh 	sc = device_get_softc(dev);
939c8befdd5SWarner Losh 	sc->ste_dev = dev;
940c8befdd5SWarner Losh 
941c8befdd5SWarner Losh 	/*
942c8befdd5SWarner Losh 	 * Only use one PHY since this chip reports multiple
943c8befdd5SWarner Losh 	 * Note on the DFE-550 the PHY is at 1 on the DFE-580
944c8befdd5SWarner Losh 	 * it is at 0 & 1.  It is rev 0x12.
945c8befdd5SWarner Losh 	 */
946c8befdd5SWarner Losh 	if (pci_get_vendor(dev) == DL_VENDORID &&
947c8befdd5SWarner Losh 	    pci_get_device(dev) == DL_DEVICEID_DL10050 &&
948c8befdd5SWarner Losh 	    pci_get_revid(dev) == 0x12 )
9494465097bSPyun YongHyeon 		sc->ste_flags |= STE_FLAG_ONE_PHY;
950c8befdd5SWarner Losh 
951c8befdd5SWarner Losh 	mtx_init(&sc->ste_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
952c8befdd5SWarner Losh 	    MTX_DEF);
953c8befdd5SWarner Losh 	/*
954c8befdd5SWarner Losh 	 * Map control/status registers.
955c8befdd5SWarner Losh 	 */
956c8befdd5SWarner Losh 	pci_enable_busmaster(dev);
957c8befdd5SWarner Losh 
958c0270e60SPyun YongHyeon 	/* Prefer memory space register mapping over IO space. */
959c0270e60SPyun YongHyeon 	sc->ste_res_id = PCIR_BAR(1);
960c0270e60SPyun YongHyeon 	sc->ste_res_type = SYS_RES_MEMORY;
961c0270e60SPyun YongHyeon 	sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
962c0270e60SPyun YongHyeon 	    &sc->ste_res_id, RF_ACTIVE);
963c0270e60SPyun YongHyeon 	if (sc->ste_res == NULL) {
964c0270e60SPyun YongHyeon 		sc->ste_res_id = PCIR_BAR(0);
965c0270e60SPyun YongHyeon 		sc->ste_res_type = SYS_RES_IOPORT;
966c0270e60SPyun YongHyeon 		sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
967c0270e60SPyun YongHyeon 		    &sc->ste_res_id, RF_ACTIVE);
968c0270e60SPyun YongHyeon 	}
969c8befdd5SWarner Losh 	if (sc->ste_res == NULL) {
970c8befdd5SWarner Losh 		device_printf(dev, "couldn't map ports/memory\n");
971c8befdd5SWarner Losh 		error = ENXIO;
972c8befdd5SWarner Losh 		goto fail;
973c8befdd5SWarner Losh 	}
974c8befdd5SWarner Losh 
975c8befdd5SWarner Losh 	sc->ste_btag = rman_get_bustag(sc->ste_res);
976c8befdd5SWarner Losh 	sc->ste_bhandle = rman_get_bushandle(sc->ste_res);
977c8befdd5SWarner Losh 
978c8befdd5SWarner Losh 	/* Allocate interrupt */
979c8befdd5SWarner Losh 	rid = 0;
980c8befdd5SWarner Losh 	sc->ste_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
981c8befdd5SWarner Losh 	    RF_SHAREABLE | RF_ACTIVE);
982c8befdd5SWarner Losh 
983c8befdd5SWarner Losh 	if (sc->ste_irq == NULL) {
984c8befdd5SWarner Losh 		device_printf(dev, "couldn't map interrupt\n");
985c8befdd5SWarner Losh 		error = ENXIO;
986c8befdd5SWarner Losh 		goto fail;
987c8befdd5SWarner Losh 	}
988c8befdd5SWarner Losh 
98910f695eeSPyun YongHyeon 	callout_init_mtx(&sc->ste_callout, &sc->ste_mtx, 0);
990c8befdd5SWarner Losh 
991c8befdd5SWarner Losh 	/* Reset the adapter. */
992c8befdd5SWarner Losh 	ste_reset(sc);
993c8befdd5SWarner Losh 
994c8befdd5SWarner Losh 	/*
995c8befdd5SWarner Losh 	 * Get station address from the EEPROM.
996c8befdd5SWarner Losh 	 */
997c8befdd5SWarner Losh 	if (ste_read_eeprom(sc, eaddr,
998c8befdd5SWarner Losh 	    STE_EEADDR_NODE0, 3, 0)) {
999c8befdd5SWarner Losh 		device_printf(dev, "failed to read station address\n");
1000c8befdd5SWarner Losh 		error = ENXIO;;
1001c8befdd5SWarner Losh 		goto fail;
1002c8befdd5SWarner Losh 	}
1003c8befdd5SWarner Losh 
1004a1b2c209SPyun YongHyeon 	if ((error = ste_dma_alloc(sc)) != 0)
1005c8befdd5SWarner Losh 		goto fail;
1006c8befdd5SWarner Losh 
1007c8befdd5SWarner Losh 	ifp = sc->ste_ifp = if_alloc(IFT_ETHER);
1008c8befdd5SWarner Losh 	if (ifp == NULL) {
1009c8befdd5SWarner Losh 		device_printf(dev, "can not if_alloc()\n");
1010c8befdd5SWarner Losh 		error = ENOSPC;
1011c8befdd5SWarner Losh 		goto fail;
1012c8befdd5SWarner Losh 	}
1013c8befdd5SWarner Losh 
1014c8befdd5SWarner Losh 	/* Do MII setup. */
1015c8befdd5SWarner Losh 	if (mii_phy_probe(dev, &sc->ste_miibus,
1016c8befdd5SWarner Losh 	    ste_ifmedia_upd, ste_ifmedia_sts)) {
1017c8befdd5SWarner Losh 		device_printf(dev, "MII without any phy!\n");
1018c8befdd5SWarner Losh 		error = ENXIO;
1019c8befdd5SWarner Losh 		goto fail;
1020c8befdd5SWarner Losh 	}
1021c8befdd5SWarner Losh 
1022c8befdd5SWarner Losh 	ifp->if_softc = sc;
1023c8befdd5SWarner Losh 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1024c8befdd5SWarner Losh 	ifp->if_mtu = ETHERMTU;
1025c8befdd5SWarner Losh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1026c8befdd5SWarner Losh 	ifp->if_ioctl = ste_ioctl;
1027c8befdd5SWarner Losh 	ifp->if_start = ste_start;
1028c8befdd5SWarner Losh 	ifp->if_init = ste_init;
1029c8befdd5SWarner Losh 	IFQ_SET_MAXLEN(&ifp->if_snd, STE_TX_LIST_CNT - 1);
1030c8befdd5SWarner Losh 	ifp->if_snd.ifq_drv_maxlen = STE_TX_LIST_CNT - 1;
1031c8befdd5SWarner Losh 	IFQ_SET_READY(&ifp->if_snd);
1032c8befdd5SWarner Losh 
1033c8befdd5SWarner Losh 	sc->ste_tx_thresh = STE_TXSTART_THRESH;
1034c8befdd5SWarner Losh 
1035c8befdd5SWarner Losh 	/*
1036c8befdd5SWarner Losh 	 * Call MI attach routine.
1037c8befdd5SWarner Losh 	 */
1038c8befdd5SWarner Losh 	ether_ifattach(ifp, eaddr);
1039c8befdd5SWarner Losh 
1040c8befdd5SWarner Losh 	/*
1041c8befdd5SWarner Losh 	 * Tell the upper layer(s) we support long frames.
1042c8befdd5SWarner Losh 	 */
1043c8befdd5SWarner Losh 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1044c8befdd5SWarner Losh 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
1045c8befdd5SWarner Losh 	ifp->if_capenable = ifp->if_capabilities;
1046c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1047c8befdd5SWarner Losh 	ifp->if_capabilities |= IFCAP_POLLING;
1048c8befdd5SWarner Losh #endif
1049c8befdd5SWarner Losh 
1050c8befdd5SWarner Losh 	/* Hook interrupt last to avoid having to lock softc */
1051c8befdd5SWarner Losh 	error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET | INTR_MPSAFE,
1052c8befdd5SWarner Losh 	    NULL, ste_intr, sc, &sc->ste_intrhand);
1053c8befdd5SWarner Losh 
1054c8befdd5SWarner Losh 	if (error) {
1055c8befdd5SWarner Losh 		device_printf(dev, "couldn't set up irq\n");
1056c8befdd5SWarner Losh 		ether_ifdetach(ifp);
1057c8befdd5SWarner Losh 		goto fail;
1058c8befdd5SWarner Losh 	}
1059c8befdd5SWarner Losh 
1060c8befdd5SWarner Losh fail:
1061c8befdd5SWarner Losh 	if (error)
1062c8befdd5SWarner Losh 		ste_detach(dev);
1063c8befdd5SWarner Losh 
1064c8befdd5SWarner Losh 	return (error);
1065c8befdd5SWarner Losh }
1066c8befdd5SWarner Losh 
1067c8befdd5SWarner Losh /*
1068c8befdd5SWarner Losh  * Shutdown hardware and free up resources. This can be called any
1069c8befdd5SWarner Losh  * time after the mutex has been initialized. It is called in both
1070c8befdd5SWarner Losh  * the error case in attach and the normal detach case so it needs
1071c8befdd5SWarner Losh  * to be careful about only freeing resources that have actually been
1072c8befdd5SWarner Losh  * allocated.
1073c8befdd5SWarner Losh  */
1074c8befdd5SWarner Losh static int
107560270842SPyun YongHyeon ste_detach(device_t dev)
1076c8befdd5SWarner Losh {
1077c8befdd5SWarner Losh 	struct ste_softc *sc;
1078c8befdd5SWarner Losh 	struct ifnet *ifp;
1079c8befdd5SWarner Losh 
1080c8befdd5SWarner Losh 	sc = device_get_softc(dev);
1081c8befdd5SWarner Losh 	KASSERT(mtx_initialized(&sc->ste_mtx), ("ste mutex not initialized"));
1082c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1083c8befdd5SWarner Losh 
1084c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1085c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING)
1086c8befdd5SWarner Losh 		ether_poll_deregister(ifp);
1087c8befdd5SWarner Losh #endif
1088c8befdd5SWarner Losh 
1089c8befdd5SWarner Losh 	/* These should only be active if attach succeeded */
1090c8befdd5SWarner Losh 	if (device_is_attached(dev)) {
10917cf545d0SJohn Baldwin 		ether_ifdetach(ifp);
1092c8befdd5SWarner Losh 		STE_LOCK(sc);
1093c8befdd5SWarner Losh 		ste_stop(sc);
1094c8befdd5SWarner Losh 		STE_UNLOCK(sc);
109510f695eeSPyun YongHyeon 		callout_drain(&sc->ste_callout);
1096c8befdd5SWarner Losh 	}
1097c8befdd5SWarner Losh 	if (sc->ste_miibus)
1098c8befdd5SWarner Losh 		device_delete_child(dev, sc->ste_miibus);
1099c8befdd5SWarner Losh 	bus_generic_detach(dev);
1100c8befdd5SWarner Losh 
1101c8befdd5SWarner Losh 	if (sc->ste_intrhand)
1102c8befdd5SWarner Losh 		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1103c8befdd5SWarner Losh 	if (sc->ste_irq)
1104c8befdd5SWarner Losh 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1105c8befdd5SWarner Losh 	if (sc->ste_res)
1106c0270e60SPyun YongHyeon 		bus_release_resource(dev, sc->ste_res_type, sc->ste_res_id,
1107c0270e60SPyun YongHyeon 		    sc->ste_res);
1108c8befdd5SWarner Losh 
1109c8befdd5SWarner Losh 	if (ifp)
1110c8befdd5SWarner Losh 		if_free(ifp);
1111c8befdd5SWarner Losh 
1112a1b2c209SPyun YongHyeon 	ste_dma_free(sc);
1113c8befdd5SWarner Losh 	mtx_destroy(&sc->ste_mtx);
1114c8befdd5SWarner Losh 
1115c8befdd5SWarner Losh 	return (0);
1116c8befdd5SWarner Losh }
1117c8befdd5SWarner Losh 
1118a1b2c209SPyun YongHyeon struct ste_dmamap_arg {
1119a1b2c209SPyun YongHyeon 	bus_addr_t	ste_busaddr;
1120a1b2c209SPyun YongHyeon };
1121a1b2c209SPyun YongHyeon 
1122a1b2c209SPyun YongHyeon static void
1123a1b2c209SPyun YongHyeon ste_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1124c8befdd5SWarner Losh {
1125a1b2c209SPyun YongHyeon 	struct ste_dmamap_arg *ctx;
1126c8befdd5SWarner Losh 
1127a1b2c209SPyun YongHyeon 	if (error != 0)
1128a1b2c209SPyun YongHyeon 		return;
1129a1b2c209SPyun YongHyeon 
1130a1b2c209SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1131a1b2c209SPyun YongHyeon 
1132a1b2c209SPyun YongHyeon 	ctx = (struct ste_dmamap_arg *)arg;
1133a1b2c209SPyun YongHyeon 	ctx->ste_busaddr = segs[0].ds_addr;
1134c8befdd5SWarner Losh }
1135c8befdd5SWarner Losh 
1136a1b2c209SPyun YongHyeon static int
1137a1b2c209SPyun YongHyeon ste_dma_alloc(struct ste_softc *sc)
1138a1b2c209SPyun YongHyeon {
1139a1b2c209SPyun YongHyeon 	struct ste_chain *txc;
1140a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *rxc;
1141a1b2c209SPyun YongHyeon 	struct ste_dmamap_arg ctx;
1142a1b2c209SPyun YongHyeon 	int error, i;
1143c8befdd5SWarner Losh 
1144a1b2c209SPyun YongHyeon 	/* Create parent DMA tag. */
1145a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1146a1b2c209SPyun YongHyeon 	    bus_get_dma_tag(sc->ste_dev), /* parent */
1147a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1148a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1149a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1150a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1151a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1152a1b2c209SPyun YongHyeon 	    0,				/* nsegments */
1153a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1154a1b2c209SPyun YongHyeon 	    0,				/* flags */
1155a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1156a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_parent_tag);
1157a1b2c209SPyun YongHyeon 	if (error != 0) {
1158a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1159a1b2c209SPyun YongHyeon 		    "could not create parent DMA tag.\n");
1160a1b2c209SPyun YongHyeon 		goto fail;
1161a1b2c209SPyun YongHyeon 	}
1162c8befdd5SWarner Losh 
1163a1b2c209SPyun YongHyeon 	/* Create DMA tag for Tx descriptor list. */
1164a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1165a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1166a1b2c209SPyun YongHyeon 	    STE_DESC_ALIGN, 0,		/* alignment, boundary */
1167a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1168a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1169a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1170a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ,		/* maxsize */
1171a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1172a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ,		/* maxsegsize */
1173a1b2c209SPyun YongHyeon 	    0,				/* flags */
1174a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1175a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_list_tag);
1176a1b2c209SPyun YongHyeon 	if (error != 0) {
1177a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1178a1b2c209SPyun YongHyeon 		    "could not create Tx list DMA tag.\n");
1179a1b2c209SPyun YongHyeon 		goto fail;
1180a1b2c209SPyun YongHyeon 	}
1181a1b2c209SPyun YongHyeon 
1182a1b2c209SPyun YongHyeon 	/* Create DMA tag for Rx descriptor list. */
1183a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1184a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1185a1b2c209SPyun YongHyeon 	    STE_DESC_ALIGN, 0,		/* alignment, boundary */
1186a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1187a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1188a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1189a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ,		/* maxsize */
1190a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1191a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ,		/* maxsegsize */
1192a1b2c209SPyun YongHyeon 	    0,				/* flags */
1193a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1194a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_list_tag);
1195a1b2c209SPyun YongHyeon 	if (error != 0) {
1196a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1197a1b2c209SPyun YongHyeon 		    "could not create Rx list DMA tag.\n");
1198a1b2c209SPyun YongHyeon 		goto fail;
1199a1b2c209SPyun YongHyeon 	}
1200a1b2c209SPyun YongHyeon 
1201a1b2c209SPyun YongHyeon 	/* Create DMA tag for Tx buffers. */
1202a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1203a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1204a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1205a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1206a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1207a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1208a1b2c209SPyun YongHyeon 	    MCLBYTES * STE_MAXFRAGS,	/* maxsize */
1209a1b2c209SPyun YongHyeon 	    STE_MAXFRAGS,		/* nsegments */
1210a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1211a1b2c209SPyun YongHyeon 	    0,				/* flags */
1212a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1213a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_tag);
1214a1b2c209SPyun YongHyeon 	if (error != 0) {
1215a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev, "could not create Tx DMA tag.\n");
1216a1b2c209SPyun YongHyeon 		goto fail;
1217a1b2c209SPyun YongHyeon 	}
1218a1b2c209SPyun YongHyeon 
1219a1b2c209SPyun YongHyeon 	/* Create DMA tag for Rx buffers. */
1220a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1221a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1222a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1223a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1224a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1225a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1226a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsize */
1227a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1228a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1229a1b2c209SPyun YongHyeon 	    0,				/* flags */
1230a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1231a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_tag);
1232a1b2c209SPyun YongHyeon 	if (error != 0) {
1233a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev, "could not create Rx DMA tag.\n");
1234a1b2c209SPyun YongHyeon 		goto fail;
1235a1b2c209SPyun YongHyeon 	}
1236a1b2c209SPyun YongHyeon 
1237a1b2c209SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx list. */
1238a1b2c209SPyun YongHyeon 	error = bus_dmamem_alloc(sc->ste_cdata.ste_tx_list_tag,
1239a1b2c209SPyun YongHyeon 	    (void **)&sc->ste_ldata.ste_tx_list,
1240a1b2c209SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1241a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_list_map);
1242a1b2c209SPyun YongHyeon 	if (error != 0) {
1243a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1244a1b2c209SPyun YongHyeon 		    "could not allocate DMA'able memory for Tx list.\n");
1245a1b2c209SPyun YongHyeon 		goto fail;
1246a1b2c209SPyun YongHyeon 	}
1247a1b2c209SPyun YongHyeon 	ctx.ste_busaddr = 0;
1248a1b2c209SPyun YongHyeon 	error = bus_dmamap_load(sc->ste_cdata.ste_tx_list_tag,
1249a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map, sc->ste_ldata.ste_tx_list,
1250a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1251a1b2c209SPyun YongHyeon 	if (error != 0 || ctx.ste_busaddr == 0) {
1252a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1253a1b2c209SPyun YongHyeon 		    "could not load DMA'able memory for Tx list.\n");
1254a1b2c209SPyun YongHyeon 		goto fail;
1255a1b2c209SPyun YongHyeon 	}
1256a1b2c209SPyun YongHyeon 	sc->ste_ldata.ste_tx_list_paddr = ctx.ste_busaddr;
1257a1b2c209SPyun YongHyeon 
1258a1b2c209SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx list. */
1259a1b2c209SPyun YongHyeon 	error = bus_dmamem_alloc(sc->ste_cdata.ste_rx_list_tag,
1260a1b2c209SPyun YongHyeon 	    (void **)&sc->ste_ldata.ste_rx_list,
1261a1b2c209SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1262a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_list_map);
1263a1b2c209SPyun YongHyeon 	if (error != 0) {
1264a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1265a1b2c209SPyun YongHyeon 		    "could not allocate DMA'able memory for Rx list.\n");
1266a1b2c209SPyun YongHyeon 		goto fail;
1267a1b2c209SPyun YongHyeon 	}
1268a1b2c209SPyun YongHyeon 	ctx.ste_busaddr = 0;
1269a1b2c209SPyun YongHyeon 	error = bus_dmamap_load(sc->ste_cdata.ste_rx_list_tag,
1270a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map, sc->ste_ldata.ste_rx_list,
1271a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1272a1b2c209SPyun YongHyeon 	if (error != 0 || ctx.ste_busaddr == 0) {
1273a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1274a1b2c209SPyun YongHyeon 		    "could not load DMA'able memory for Rx list.\n");
1275a1b2c209SPyun YongHyeon 		goto fail;
1276a1b2c209SPyun YongHyeon 	}
1277a1b2c209SPyun YongHyeon 	sc->ste_ldata.ste_rx_list_paddr = ctx.ste_busaddr;
1278a1b2c209SPyun YongHyeon 
1279a1b2c209SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
1280a1b2c209SPyun YongHyeon 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1281a1b2c209SPyun YongHyeon 		txc = &sc->ste_cdata.ste_tx_chain[i];
1282a1b2c209SPyun YongHyeon 		txc->ste_ptr = NULL;
1283a1b2c209SPyun YongHyeon 		txc->ste_mbuf = NULL;
1284a1b2c209SPyun YongHyeon 		txc->ste_next = NULL;
1285a1b2c209SPyun YongHyeon 		txc->ste_phys = 0;
1286a1b2c209SPyun YongHyeon 		txc->ste_map = NULL;
1287a1b2c209SPyun YongHyeon 		error = bus_dmamap_create(sc->ste_cdata.ste_tx_tag, 0,
1288a1b2c209SPyun YongHyeon 		    &txc->ste_map);
1289a1b2c209SPyun YongHyeon 		if (error != 0) {
1290a1b2c209SPyun YongHyeon 			device_printf(sc->ste_dev,
1291a1b2c209SPyun YongHyeon 			    "could not create Tx dmamap.\n");
1292a1b2c209SPyun YongHyeon 			goto fail;
1293a1b2c209SPyun YongHyeon 		}
1294a1b2c209SPyun YongHyeon 	}
1295a1b2c209SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
1296a1b2c209SPyun YongHyeon 	if ((error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1297a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_sparemap)) != 0) {
1298a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1299a1b2c209SPyun YongHyeon 		    "could not create spare Rx dmamap.\n");
1300a1b2c209SPyun YongHyeon 		goto fail;
1301a1b2c209SPyun YongHyeon 	}
1302a1b2c209SPyun YongHyeon 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1303a1b2c209SPyun YongHyeon 		rxc = &sc->ste_cdata.ste_rx_chain[i];
1304a1b2c209SPyun YongHyeon 		rxc->ste_ptr = NULL;
1305a1b2c209SPyun YongHyeon 		rxc->ste_mbuf = NULL;
1306a1b2c209SPyun YongHyeon 		rxc->ste_next = NULL;
1307a1b2c209SPyun YongHyeon 		rxc->ste_map = NULL;
1308a1b2c209SPyun YongHyeon 		error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1309a1b2c209SPyun YongHyeon 		    &rxc->ste_map);
1310a1b2c209SPyun YongHyeon 		if (error != 0) {
1311a1b2c209SPyun YongHyeon 			device_printf(sc->ste_dev,
1312a1b2c209SPyun YongHyeon 			    "could not create Rx dmamap.\n");
1313a1b2c209SPyun YongHyeon 			goto fail;
1314a1b2c209SPyun YongHyeon 		}
1315a1b2c209SPyun YongHyeon 	}
1316a1b2c209SPyun YongHyeon 
1317a1b2c209SPyun YongHyeon fail:
1318a1b2c209SPyun YongHyeon 	return (error);
1319a1b2c209SPyun YongHyeon }
1320a1b2c209SPyun YongHyeon 
1321a1b2c209SPyun YongHyeon static void
1322a1b2c209SPyun YongHyeon ste_dma_free(struct ste_softc *sc)
1323a1b2c209SPyun YongHyeon {
1324a1b2c209SPyun YongHyeon 	struct ste_chain *txc;
1325a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *rxc;
1326a1b2c209SPyun YongHyeon 	int i;
1327a1b2c209SPyun YongHyeon 
1328a1b2c209SPyun YongHyeon 	/* Tx buffers. */
1329a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_tag != NULL) {
1330a1b2c209SPyun YongHyeon 		for (i = 0; i < STE_TX_LIST_CNT; i++) {
1331a1b2c209SPyun YongHyeon 			txc = &sc->ste_cdata.ste_tx_chain[i];
1332a1b2c209SPyun YongHyeon 			if (txc->ste_map != NULL) {
1333a1b2c209SPyun YongHyeon 				bus_dmamap_destroy(sc->ste_cdata.ste_tx_tag,
1334a1b2c209SPyun YongHyeon 				    txc->ste_map);
1335a1b2c209SPyun YongHyeon 				txc->ste_map = NULL;
1336a1b2c209SPyun YongHyeon 			}
1337a1b2c209SPyun YongHyeon 		}
1338a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_tx_tag);
1339a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_tag = NULL;
1340a1b2c209SPyun YongHyeon 	}
1341a1b2c209SPyun YongHyeon 	/* Rx buffers. */
1342a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_rx_tag != NULL) {
1343a1b2c209SPyun YongHyeon 		for (i = 0; i < STE_RX_LIST_CNT; i++) {
1344a1b2c209SPyun YongHyeon 			rxc = &sc->ste_cdata.ste_rx_chain[i];
1345a1b2c209SPyun YongHyeon 			if (rxc->ste_map != NULL) {
1346a1b2c209SPyun YongHyeon 				bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1347a1b2c209SPyun YongHyeon 				    rxc->ste_map);
1348a1b2c209SPyun YongHyeon 				rxc->ste_map = NULL;
1349a1b2c209SPyun YongHyeon 			}
1350a1b2c209SPyun YongHyeon 		}
1351a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_sparemap != NULL) {
1352a1b2c209SPyun YongHyeon 			bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1353a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_sparemap);
1354a1b2c209SPyun YongHyeon 			sc->ste_cdata.ste_rx_sparemap = NULL;
1355a1b2c209SPyun YongHyeon 		}
1356a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_rx_tag);
1357a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_tag = NULL;
1358a1b2c209SPyun YongHyeon 	}
1359a1b2c209SPyun YongHyeon 	/* Tx descriptor list. */
1360a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_list_tag != NULL) {
1361a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_list_map != NULL)
1362a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_tx_list_tag,
1363a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map);
1364a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_list_map != NULL &&
1365a1b2c209SPyun YongHyeon 		    sc->ste_ldata.ste_tx_list != NULL)
1366a1b2c209SPyun YongHyeon 			bus_dmamem_free(sc->ste_cdata.ste_tx_list_tag,
1367a1b2c209SPyun YongHyeon 			    sc->ste_ldata.ste_tx_list,
1368a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map);
1369a1b2c209SPyun YongHyeon 		sc->ste_ldata.ste_tx_list = NULL;
1370a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_list_map = NULL;
1371a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_tx_list_tag);
1372a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_list_tag = NULL;
1373a1b2c209SPyun YongHyeon 	}
1374a1b2c209SPyun YongHyeon 	/* Rx descriptor list. */
1375a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_rx_list_tag != NULL) {
1376a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_list_map != NULL)
1377a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_rx_list_tag,
1378a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_list_map);
1379a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_list_map != NULL &&
1380a1b2c209SPyun YongHyeon 		    sc->ste_ldata.ste_rx_list != NULL)
1381a1b2c209SPyun YongHyeon 			bus_dmamem_free(sc->ste_cdata.ste_rx_list_tag,
1382a1b2c209SPyun YongHyeon 			    sc->ste_ldata.ste_rx_list,
1383a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_list_map);
1384a1b2c209SPyun YongHyeon 		sc->ste_ldata.ste_rx_list = NULL;
1385a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_list_map = NULL;
1386a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_rx_list_tag);
1387a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_list_tag = NULL;
1388a1b2c209SPyun YongHyeon 	}
1389a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_parent_tag != NULL) {
1390a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_parent_tag);
1391a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_parent_tag = NULL;
1392a1b2c209SPyun YongHyeon 	}
1393a1b2c209SPyun YongHyeon }
1394a1b2c209SPyun YongHyeon 
1395a1b2c209SPyun YongHyeon static int
1396a1b2c209SPyun YongHyeon ste_newbuf(struct ste_softc *sc, struct ste_chain_onefrag *rxc)
1397a1b2c209SPyun YongHyeon {
1398a1b2c209SPyun YongHyeon 	struct mbuf *m;
1399a1b2c209SPyun YongHyeon 	bus_dma_segment_t segs[1];
1400a1b2c209SPyun YongHyeon 	bus_dmamap_t map;
1401a1b2c209SPyun YongHyeon 	int error, nsegs;
1402a1b2c209SPyun YongHyeon 
1403a1b2c209SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1404a1b2c209SPyun YongHyeon 	if (m == NULL)
1405a1b2c209SPyun YongHyeon 		return (ENOBUFS);
1406a1b2c209SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1407a1b2c209SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
1408a1b2c209SPyun YongHyeon 
1409a1b2c209SPyun YongHyeon 	if ((error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_rx_tag,
1410a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_sparemap, m, segs, &nsegs, 0)) != 0) {
1411a1b2c209SPyun YongHyeon 		m_freem(m);
1412a1b2c209SPyun YongHyeon 		return (error);
1413a1b2c209SPyun YongHyeon 	}
1414a1b2c209SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1415a1b2c209SPyun YongHyeon 
1416a1b2c209SPyun YongHyeon 	if (rxc->ste_mbuf != NULL) {
1417a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1418a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1419a1b2c209SPyun YongHyeon 		bus_dmamap_unload(sc->ste_cdata.ste_rx_tag, rxc->ste_map);
1420a1b2c209SPyun YongHyeon 	}
1421a1b2c209SPyun YongHyeon 	map = rxc->ste_map;
1422a1b2c209SPyun YongHyeon 	rxc->ste_map = sc->ste_cdata.ste_rx_sparemap;
1423a1b2c209SPyun YongHyeon 	sc->ste_cdata.ste_rx_sparemap = map;
1424a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1425a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1426a1b2c209SPyun YongHyeon 	rxc->ste_mbuf = m;
1427a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_status = 0;
1428a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_frag.ste_addr = htole32(segs[0].ds_addr);
1429a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_frag.ste_len = htole32(segs[0].ds_len |
1430a1b2c209SPyun YongHyeon 	    STE_FRAG_LAST);
1431c8befdd5SWarner Losh 	return (0);
1432c8befdd5SWarner Losh }
1433c8befdd5SWarner Losh 
1434c8befdd5SWarner Losh static int
143560270842SPyun YongHyeon ste_init_rx_list(struct ste_softc *sc)
1436c8befdd5SWarner Losh {
1437c8befdd5SWarner Losh 	struct ste_chain_data *cd;
1438c8befdd5SWarner Losh 	struct ste_list_data *ld;
1439a1b2c209SPyun YongHyeon 	int error, i;
1440c8befdd5SWarner Losh 
1441c8befdd5SWarner Losh 	cd = &sc->ste_cdata;
1442a1b2c209SPyun YongHyeon 	ld = &sc->ste_ldata;
1443a1b2c209SPyun YongHyeon 	bzero(ld->ste_rx_list, STE_RX_LIST_SZ);
1444c8befdd5SWarner Losh 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1445c8befdd5SWarner Losh 		cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i];
1446a1b2c209SPyun YongHyeon 		error = ste_newbuf(sc, &cd->ste_rx_chain[i]);
1447a1b2c209SPyun YongHyeon 		if (error != 0)
1448a1b2c209SPyun YongHyeon 			return (error);
1449c8befdd5SWarner Losh 		if (i == (STE_RX_LIST_CNT - 1)) {
1450a1b2c209SPyun YongHyeon 			cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[0];
1451a1b2c209SPyun YongHyeon 			ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr +
1452a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc_onefrag) * 0);
1453c8befdd5SWarner Losh 		} else {
1454a1b2c209SPyun YongHyeon 			cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[i + 1];
1455a1b2c209SPyun YongHyeon 			ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr +
1456a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc_onefrag) * (i + 1));
1457c8befdd5SWarner Losh 		}
1458c8befdd5SWarner Losh 	}
1459c8befdd5SWarner Losh 
1460c8befdd5SWarner Losh 	cd->ste_rx_head = &cd->ste_rx_chain[0];
1461a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
1462a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map,
1463a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1464c8befdd5SWarner Losh 
1465c8befdd5SWarner Losh 	return (0);
1466c8befdd5SWarner Losh }
1467c8befdd5SWarner Losh 
1468c8befdd5SWarner Losh static void
146960270842SPyun YongHyeon ste_init_tx_list(struct ste_softc *sc)
1470c8befdd5SWarner Losh {
1471c8befdd5SWarner Losh 	struct ste_chain_data *cd;
1472c8befdd5SWarner Losh 	struct ste_list_data *ld;
1473c8befdd5SWarner Losh 	int i;
1474c8befdd5SWarner Losh 
1475c8befdd5SWarner Losh 	cd = &sc->ste_cdata;
1476a1b2c209SPyun YongHyeon 	ld = &sc->ste_ldata;
1477a1b2c209SPyun YongHyeon 	bzero(ld->ste_tx_list, STE_TX_LIST_SZ);
1478c8befdd5SWarner Losh 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1479c8befdd5SWarner Losh 		cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i];
1480a1b2c209SPyun YongHyeon 		cd->ste_tx_chain[i].ste_mbuf = NULL;
1481a1b2c209SPyun YongHyeon 		if (i == (STE_TX_LIST_CNT - 1)) {
1482a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[0];
1483a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1484a1b2c209SPyun YongHyeon 			    ld->ste_tx_list_paddr +
1485a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc) * 0)));
1486a1b2c209SPyun YongHyeon 		} else {
1487a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[i + 1];
1488a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1489a1b2c209SPyun YongHyeon 			    ld->ste_tx_list_paddr +
1490a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc) * (i + 1))));
1491a1b2c209SPyun YongHyeon 		}
1492c8befdd5SWarner Losh 	}
1493c8befdd5SWarner Losh 
1494a1b2c209SPyun YongHyeon 	cd->ste_last_tx = NULL;
1495c8befdd5SWarner Losh 	cd->ste_tx_prod = 0;
1496c8befdd5SWarner Losh 	cd->ste_tx_cons = 0;
1497a1b2c209SPyun YongHyeon 	cd->ste_tx_cnt = 0;
1498a1b2c209SPyun YongHyeon 
1499a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1500a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map,
1501a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1502c8befdd5SWarner Losh }
1503c8befdd5SWarner Losh 
1504c8befdd5SWarner Losh static void
150560270842SPyun YongHyeon ste_init(void *xsc)
1506c8befdd5SWarner Losh {
1507c8befdd5SWarner Losh 	struct ste_softc *sc;
1508c8befdd5SWarner Losh 
1509c8befdd5SWarner Losh 	sc = xsc;
1510c8befdd5SWarner Losh 	STE_LOCK(sc);
1511c8befdd5SWarner Losh 	ste_init_locked(sc);
1512c8befdd5SWarner Losh 	STE_UNLOCK(sc);
1513c8befdd5SWarner Losh }
1514c8befdd5SWarner Losh 
1515c8befdd5SWarner Losh static void
151660270842SPyun YongHyeon ste_init_locked(struct ste_softc *sc)
1517c8befdd5SWarner Losh {
1518c8befdd5SWarner Losh 	struct ifnet *ifp;
1519f2632c3bSPyun YongHyeon 	int i;
1520c8befdd5SWarner Losh 
1521c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1522c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1523c8befdd5SWarner Losh 
1524c8befdd5SWarner Losh 	ste_stop(sc);
15258d9f6dd9SPyun YongHyeon 	/* Reset the chip to a known state. */
15268d9f6dd9SPyun YongHyeon 	ste_reset(sc);
1527c8befdd5SWarner Losh 
1528c8befdd5SWarner Losh 	/* Init our MAC address */
1529c8befdd5SWarner Losh 	for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
1530c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_PAR0 + i,
1531c8befdd5SWarner Losh 		    ((IF_LLADDR(sc->ste_ifp)[i] & 0xff) |
1532c8befdd5SWarner Losh 		     IF_LLADDR(sc->ste_ifp)[i + 1] << 8));
1533c8befdd5SWarner Losh 	}
1534c8befdd5SWarner Losh 
1535c8befdd5SWarner Losh 	/* Init RX list */
1536a1b2c209SPyun YongHyeon 	if (ste_init_rx_list(sc) != 0) {
1537c8befdd5SWarner Losh 		device_printf(sc->ste_dev,
1538c8befdd5SWarner Losh 		    "initialization failed: no memory for RX buffers\n");
1539c8befdd5SWarner Losh 		ste_stop(sc);
1540c8befdd5SWarner Losh 		return;
1541c8befdd5SWarner Losh 	}
1542c8befdd5SWarner Losh 
1543c8befdd5SWarner Losh 	/* Set RX polling interval */
1544c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 64);
1545c8befdd5SWarner Losh 
1546c8befdd5SWarner Losh 	/* Init TX descriptors */
1547c8befdd5SWarner Losh 	ste_init_tx_list(sc);
1548c8befdd5SWarner Losh 
1549c8befdd5SWarner Losh 	/* Set the TX freethresh value */
1550c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8);
1551c8befdd5SWarner Losh 
1552c8befdd5SWarner Losh 	/* Set the TX start threshold for best performance. */
1553c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
1554c8befdd5SWarner Losh 
1555c8befdd5SWarner Losh 	/* Set the TX reclaim threshold. */
1556c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4));
1557c8befdd5SWarner Losh 
1558c8befdd5SWarner Losh 	/* Set up the RX filter. */
1559c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_RX_MODE, STE_RXMODE_UNICAST);
1560c8befdd5SWarner Losh 
1561c8befdd5SWarner Losh 	/* If we want promiscuous mode, set the allframes bit. */
1562c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_PROMISC) {
1563c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1564c8befdd5SWarner Losh 	} else {
1565c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1566c8befdd5SWarner Losh 	}
1567c8befdd5SWarner Losh 
1568c8befdd5SWarner Losh 	/* Set capture broadcast bit to accept broadcast frames. */
1569c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_BROADCAST) {
1570c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1571c8befdd5SWarner Losh 	} else {
1572c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1573c8befdd5SWarner Losh 	}
1574c8befdd5SWarner Losh 
1575c8befdd5SWarner Losh 	ste_setmulti(sc);
1576c8befdd5SWarner Losh 
1577c8befdd5SWarner Losh 	/* Load the address of the RX list. */
1578c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1579c8befdd5SWarner Losh 	ste_wait(sc);
1580c8befdd5SWarner Losh 	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
1581a1b2c209SPyun YongHyeon 	    STE_ADDR_LO(sc->ste_ldata.ste_rx_list_paddr));
1582c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1583c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1584c8befdd5SWarner Losh 
1585a1b2c209SPyun YongHyeon 	/* Set TX polling interval(defer until we TX first packet). */
1586c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
1587c8befdd5SWarner Losh 
1588c8befdd5SWarner Losh 	/* Load address of the TX list */
1589c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1590c8befdd5SWarner Losh 	ste_wait(sc);
1591c8befdd5SWarner Losh 	CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
1592c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1593c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1594c8befdd5SWarner Losh 	ste_wait(sc);
1595c8befdd5SWarner Losh 
1596c8befdd5SWarner Losh 	/* Enable receiver and transmitter */
1597c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MACCTL0, 0);
1598c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MACCTL1, 0);
1599c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE);
1600c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE);
1601c8befdd5SWarner Losh 
1602c8befdd5SWarner Losh 	/* Enable stats counters. */
1603c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE);
1604c8befdd5SWarner Losh 
1605c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_ISR, 0xFFFF);
1606c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1607c8befdd5SWarner Losh 	/* Disable interrupts if we are polling. */
1608c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING)
1609c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_IMR, 0);
1610c8befdd5SWarner Losh 	else
1611c8befdd5SWarner Losh #endif
1612c8befdd5SWarner Losh 	/* Enable interrupts. */
1613c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1614c8befdd5SWarner Losh 
1615c8befdd5SWarner Losh 	/* Accept VLAN length packets */
1616c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAX_FRAMELEN, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN);
1617c8befdd5SWarner Losh 
1618c8befdd5SWarner Losh 	ste_ifmedia_upd_locked(ifp);
1619c8befdd5SWarner Losh 
1620c8befdd5SWarner Losh 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1621c8befdd5SWarner Losh 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1622c8befdd5SWarner Losh 
162310f695eeSPyun YongHyeon 	callout_reset(&sc->ste_callout, hz, ste_tick, sc);
1624c8befdd5SWarner Losh }
1625c8befdd5SWarner Losh 
1626c8befdd5SWarner Losh static void
162760270842SPyun YongHyeon ste_stop(struct ste_softc *sc)
1628c8befdd5SWarner Losh {
1629c8befdd5SWarner Losh 	struct ifnet *ifp;
1630a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *cur_rx;
1631a1b2c209SPyun YongHyeon 	struct ste_chain *cur_tx;
16328d9f6dd9SPyun YongHyeon 	uint32_t val;
1633f2632c3bSPyun YongHyeon 	int i;
1634c8befdd5SWarner Losh 
1635c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1636c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1637c8befdd5SWarner Losh 
163810f695eeSPyun YongHyeon 	callout_stop(&sc->ste_callout);
163910f695eeSPyun YongHyeon 	sc->ste_timer = 0;
1640c8befdd5SWarner Losh 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
1641c8befdd5SWarner Losh 
1642c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, 0);
16438d9f6dd9SPyun YongHyeon 	/* Stop pending DMA. */
16448d9f6dd9SPyun YongHyeon 	val = CSR_READ_4(sc, STE_DMACTL);
16458d9f6dd9SPyun YongHyeon 	val |= STE_DMACTL_TXDMA_STALL | STE_DMACTL_RXDMA_STALL;
16468d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_DMACTL, val);
1647c8befdd5SWarner Losh 	ste_wait(sc);
16488d9f6dd9SPyun YongHyeon 	/* Disable auto-polling. */
16498d9f6dd9SPyun YongHyeon 	CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 0);
16508d9f6dd9SPyun YongHyeon 	CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
16518d9f6dd9SPyun YongHyeon 	/* Nullify DMA address to stop any further DMA. */
16528d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 0);
16538d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
16548d9f6dd9SPyun YongHyeon 	/* Stop TX/RX MAC. */
16558d9f6dd9SPyun YongHyeon 	val = CSR_READ_2(sc, STE_MACCTL1);
16568d9f6dd9SPyun YongHyeon 	val |= STE_MACCTL1_TX_DISABLE | STE_MACCTL1_RX_DISABLE |
16578d9f6dd9SPyun YongHyeon 	    STE_MACCTL1_STATS_DISABLE;
16588d9f6dd9SPyun YongHyeon 	CSR_WRITE_2(sc, STE_MACCTL1, val);
16598d9f6dd9SPyun YongHyeon 	for (i = 0; i < STE_TIMEOUT; i++) {
16608d9f6dd9SPyun YongHyeon 		DELAY(10);
16618d9f6dd9SPyun YongHyeon 		if ((CSR_READ_2(sc, STE_MACCTL1) & (STE_MACCTL1_TX_DISABLE |
16628d9f6dd9SPyun YongHyeon 		    STE_MACCTL1_RX_DISABLE | STE_MACCTL1_STATS_DISABLE)) == 0)
16638d9f6dd9SPyun YongHyeon 			break;
16648d9f6dd9SPyun YongHyeon 	}
16658d9f6dd9SPyun YongHyeon 	if (i == STE_TIMEOUT)
16668d9f6dd9SPyun YongHyeon 		device_printf(sc->ste_dev, "Stopping MAC timed out\n");
16678d9f6dd9SPyun YongHyeon 	/* Acknowledge any pending interrupts. */
16688d9f6dd9SPyun YongHyeon 	CSR_READ_2(sc, STE_ISR_ACK);
16698d9f6dd9SPyun YongHyeon 	ste_stats_update(sc);
1670c8befdd5SWarner Losh 
1671c8befdd5SWarner Losh 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1672a1b2c209SPyun YongHyeon 		cur_rx = &sc->ste_cdata.ste_rx_chain[i];
1673a1b2c209SPyun YongHyeon 		if (cur_rx->ste_mbuf != NULL) {
1674a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_rx_tag,
1675a1b2c209SPyun YongHyeon 			    cur_rx->ste_map, BUS_DMASYNC_POSTREAD);
1676a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_rx_tag,
1677a1b2c209SPyun YongHyeon 			    cur_rx->ste_map);
1678a1b2c209SPyun YongHyeon 			m_freem(cur_rx->ste_mbuf);
1679a1b2c209SPyun YongHyeon 			cur_rx->ste_mbuf = NULL;
1680c8befdd5SWarner Losh 		}
1681c8befdd5SWarner Losh 	}
1682c8befdd5SWarner Losh 
1683c8befdd5SWarner Losh 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1684a1b2c209SPyun YongHyeon 		cur_tx = &sc->ste_cdata.ste_tx_chain[i];
1685a1b2c209SPyun YongHyeon 		if (cur_tx->ste_mbuf != NULL) {
1686a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_tag,
1687a1b2c209SPyun YongHyeon 			    cur_tx->ste_map, BUS_DMASYNC_POSTWRITE);
1688a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_tx_tag,
1689a1b2c209SPyun YongHyeon 			    cur_tx->ste_map);
1690a1b2c209SPyun YongHyeon 			m_freem(cur_tx->ste_mbuf);
1691a1b2c209SPyun YongHyeon 			cur_tx->ste_mbuf = NULL;
1692c8befdd5SWarner Losh 		}
1693c8befdd5SWarner Losh 	}
1694c8befdd5SWarner Losh }
1695c8befdd5SWarner Losh 
1696c8befdd5SWarner Losh static void
169760270842SPyun YongHyeon ste_reset(struct ste_softc *sc)
1698c8befdd5SWarner Losh {
1699c8befdd5SWarner Losh 	int i;
1700c8befdd5SWarner Losh 
1701c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_ASICCTL,
1702c8befdd5SWarner Losh 	    STE_ASICCTL_GLOBAL_RESET|STE_ASICCTL_RX_RESET|
1703c8befdd5SWarner Losh 	    STE_ASICCTL_TX_RESET|STE_ASICCTL_DMA_RESET|
1704c8befdd5SWarner Losh 	    STE_ASICCTL_FIFO_RESET|STE_ASICCTL_NETWORK_RESET|
1705c8befdd5SWarner Losh 	    STE_ASICCTL_AUTOINIT_RESET|STE_ASICCTL_HOST_RESET|
1706c8befdd5SWarner Losh 	    STE_ASICCTL_EXTRESET_RESET);
1707c8befdd5SWarner Losh 
1708c8befdd5SWarner Losh 	DELAY(100000);
1709c8befdd5SWarner Losh 
1710c8befdd5SWarner Losh 	for (i = 0; i < STE_TIMEOUT; i++) {
1711c8befdd5SWarner Losh 		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY))
1712c8befdd5SWarner Losh 			break;
1713c8befdd5SWarner Losh 	}
1714c8befdd5SWarner Losh 
1715c8befdd5SWarner Losh 	if (i == STE_TIMEOUT)
1716c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "global reset never completed\n");
1717c8befdd5SWarner Losh }
1718c8befdd5SWarner Losh 
1719c8befdd5SWarner Losh static int
172060270842SPyun YongHyeon ste_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1721c8befdd5SWarner Losh {
1722c8befdd5SWarner Losh 	struct ste_softc *sc;
1723c8befdd5SWarner Losh 	struct ifreq *ifr;
1724c8befdd5SWarner Losh 	struct mii_data *mii;
1725c8befdd5SWarner Losh 	int error = 0;
1726c8befdd5SWarner Losh 
1727c8befdd5SWarner Losh 	sc = ifp->if_softc;
1728c8befdd5SWarner Losh 	ifr = (struct ifreq *)data;
1729c8befdd5SWarner Losh 
1730c8befdd5SWarner Losh 	switch (command) {
1731c8befdd5SWarner Losh 	case SIOCSIFFLAGS:
1732c8befdd5SWarner Losh 		STE_LOCK(sc);
1733c8befdd5SWarner Losh 		if (ifp->if_flags & IFF_UP) {
1734c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1735c8befdd5SWarner Losh 			    ifp->if_flags & IFF_PROMISC &&
1736c8befdd5SWarner Losh 			    !(sc->ste_if_flags & IFF_PROMISC)) {
1737c8befdd5SWarner Losh 				STE_SETBIT1(sc, STE_RX_MODE,
1738c8befdd5SWarner Losh 				    STE_RXMODE_PROMISC);
1739c8befdd5SWarner Losh 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1740c8befdd5SWarner Losh 			    !(ifp->if_flags & IFF_PROMISC) &&
1741c8befdd5SWarner Losh 			    sc->ste_if_flags & IFF_PROMISC) {
1742c8befdd5SWarner Losh 				STE_CLRBIT1(sc, STE_RX_MODE,
1743c8befdd5SWarner Losh 				    STE_RXMODE_PROMISC);
1744c8befdd5SWarner Losh 			}
1745c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1746c8befdd5SWarner Losh 			    (ifp->if_flags ^ sc->ste_if_flags) & IFF_ALLMULTI)
1747c8befdd5SWarner Losh 				ste_setmulti(sc);
1748c8befdd5SWarner Losh 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1749c8befdd5SWarner Losh 				sc->ste_tx_thresh = STE_TXSTART_THRESH;
1750c8befdd5SWarner Losh 				ste_init_locked(sc);
1751c8befdd5SWarner Losh 			}
1752c8befdd5SWarner Losh 		} else {
1753c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1754c8befdd5SWarner Losh 				ste_stop(sc);
1755c8befdd5SWarner Losh 		}
1756c8befdd5SWarner Losh 		sc->ste_if_flags = ifp->if_flags;
1757c8befdd5SWarner Losh 		STE_UNLOCK(sc);
1758c8befdd5SWarner Losh 		error = 0;
1759c8befdd5SWarner Losh 		break;
1760c8befdd5SWarner Losh 	case SIOCADDMULTI:
1761c8befdd5SWarner Losh 	case SIOCDELMULTI:
1762c8befdd5SWarner Losh 		STE_LOCK(sc);
1763c8befdd5SWarner Losh 		ste_setmulti(sc);
1764c8befdd5SWarner Losh 		STE_UNLOCK(sc);
1765c8befdd5SWarner Losh 		error = 0;
1766c8befdd5SWarner Losh 		break;
1767c8befdd5SWarner Losh 	case SIOCGIFMEDIA:
1768c8befdd5SWarner Losh 	case SIOCSIFMEDIA:
1769c8befdd5SWarner Losh 		mii = device_get_softc(sc->ste_miibus);
1770c8befdd5SWarner Losh 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1771c8befdd5SWarner Losh 		break;
1772c8befdd5SWarner Losh 	case SIOCSIFCAP:
1773c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1774c8befdd5SWarner Losh 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
1775c8befdd5SWarner Losh 		    !(ifp->if_capenable & IFCAP_POLLING)) {
1776c8befdd5SWarner Losh 			error = ether_poll_register(ste_poll, ifp);
1777c8befdd5SWarner Losh 			if (error)
1778c8befdd5SWarner Losh 				return (error);
1779c8befdd5SWarner Losh 			STE_LOCK(sc);
1780c8befdd5SWarner Losh 			/* Disable interrupts */
1781c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_IMR, 0);
1782c8befdd5SWarner Losh 			ifp->if_capenable |= IFCAP_POLLING;
1783c8befdd5SWarner Losh 			STE_UNLOCK(sc);
1784c8befdd5SWarner Losh 			return (error);
1785c8befdd5SWarner Losh 
1786c8befdd5SWarner Losh 		}
1787c8befdd5SWarner Losh 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
1788c8befdd5SWarner Losh 		    ifp->if_capenable & IFCAP_POLLING) {
1789c8befdd5SWarner Losh 			error = ether_poll_deregister(ifp);
1790c8befdd5SWarner Losh 			/* Enable interrupts. */
1791c8befdd5SWarner Losh 			STE_LOCK(sc);
1792c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1793c8befdd5SWarner Losh 			ifp->if_capenable &= ~IFCAP_POLLING;
1794c8befdd5SWarner Losh 			STE_UNLOCK(sc);
1795c8befdd5SWarner Losh 			return (error);
1796c8befdd5SWarner Losh 		}
1797c8befdd5SWarner Losh #endif /* DEVICE_POLLING */
1798c8befdd5SWarner Losh 		break;
1799c8befdd5SWarner Losh 	default:
1800c8befdd5SWarner Losh 		error = ether_ioctl(ifp, command, data);
1801c8befdd5SWarner Losh 		break;
1802c8befdd5SWarner Losh 	}
1803c8befdd5SWarner Losh 
1804c8befdd5SWarner Losh 	return (error);
1805c8befdd5SWarner Losh }
1806c8befdd5SWarner Losh 
1807c8befdd5SWarner Losh static int
1808a1b2c209SPyun YongHyeon ste_encap(struct ste_softc *sc, struct mbuf **m_head, struct ste_chain *txc)
1809c8befdd5SWarner Losh {
1810a1b2c209SPyun YongHyeon 	struct ste_frag *frag;
1811c8befdd5SWarner Losh 	struct mbuf *m;
1812a1b2c209SPyun YongHyeon 	struct ste_desc *desc;
1813a1b2c209SPyun YongHyeon 	bus_dma_segment_t txsegs[STE_MAXFRAGS];
1814a1b2c209SPyun YongHyeon 	int error, i, nsegs;
1815c8befdd5SWarner Losh 
1816a1b2c209SPyun YongHyeon 	STE_LOCK_ASSERT(sc);
1817a1b2c209SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
1818c8befdd5SWarner Losh 
1819a1b2c209SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1820a1b2c209SPyun YongHyeon 	    txc->ste_map, *m_head, txsegs, &nsegs, 0);
1821a1b2c209SPyun YongHyeon 	if (error == EFBIG) {
1822a1b2c209SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, STE_MAXFRAGS);
1823a1b2c209SPyun YongHyeon 		if (m == NULL) {
1824a1b2c209SPyun YongHyeon 			m_freem(*m_head);
1825a1b2c209SPyun YongHyeon 			*m_head = NULL;
1826a1b2c209SPyun YongHyeon 			return (ENOMEM);
1827c8befdd5SWarner Losh 		}
1828a1b2c209SPyun YongHyeon 		*m_head = m;
1829a1b2c209SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1830a1b2c209SPyun YongHyeon 		    txc->ste_map, *m_head, txsegs, &nsegs, 0);
1831a1b2c209SPyun YongHyeon 		if (error != 0) {
1832a1b2c209SPyun YongHyeon 			m_freem(*m_head);
1833a1b2c209SPyun YongHyeon 			*m_head = NULL;
1834a1b2c209SPyun YongHyeon 			return (error);
1835c8befdd5SWarner Losh 		}
1836a1b2c209SPyun YongHyeon 	} else if (error != 0)
1837a1b2c209SPyun YongHyeon 		return (error);
1838a1b2c209SPyun YongHyeon 	if (nsegs == 0) {
1839a1b2c209SPyun YongHyeon 		m_freem(*m_head);
1840a1b2c209SPyun YongHyeon 		*m_head = NULL;
1841a1b2c209SPyun YongHyeon 		return (EIO);
1842a1b2c209SPyun YongHyeon 	}
1843a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, txc->ste_map,
1844a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
1845c8befdd5SWarner Losh 
1846a1b2c209SPyun YongHyeon 	desc = txc->ste_ptr;
1847a1b2c209SPyun YongHyeon 	for (i = 0; i < nsegs; i++) {
1848a1b2c209SPyun YongHyeon 		frag = &desc->ste_frags[i];
1849a1b2c209SPyun YongHyeon 		frag->ste_addr = htole32(STE_ADDR_LO(txsegs[i].ds_addr));
1850a1b2c209SPyun YongHyeon 		frag->ste_len = htole32(txsegs[i].ds_len);
1851a1b2c209SPyun YongHyeon 	}
1852a1b2c209SPyun YongHyeon 	desc->ste_frags[i - 1].ste_len |= htole32(STE_FRAG_LAST);
1853c8befdd5SWarner Losh 	/*
1854a1b2c209SPyun YongHyeon 	 * Because we use Tx polling we can't chain multiple
1855a1b2c209SPyun YongHyeon 	 * Tx descriptors here. Otherwise we race with controller.
1856c8befdd5SWarner Losh 	 */
1857a1b2c209SPyun YongHyeon 	desc->ste_next = 0;
1858a1b2c209SPyun YongHyeon 	desc->ste_ctl = htole32(STE_TXCTL_ALIGN_DIS | STE_TXCTL_DMAINTR);
1859a1b2c209SPyun YongHyeon 	txc->ste_mbuf = *m_head;
1860a1b2c209SPyun YongHyeon 	STE_INC(sc->ste_cdata.ste_tx_prod, STE_TX_LIST_CNT);
1861a1b2c209SPyun YongHyeon 	sc->ste_cdata.ste_tx_cnt++;
1862c8befdd5SWarner Losh 
1863c8befdd5SWarner Losh 	return (0);
1864c8befdd5SWarner Losh }
1865c8befdd5SWarner Losh 
1866c8befdd5SWarner Losh static void
186760270842SPyun YongHyeon ste_start(struct ifnet *ifp)
1868c8befdd5SWarner Losh {
1869c8befdd5SWarner Losh 	struct ste_softc *sc;
1870c8befdd5SWarner Losh 
1871c8befdd5SWarner Losh 	sc = ifp->if_softc;
1872c8befdd5SWarner Losh 	STE_LOCK(sc);
1873c8befdd5SWarner Losh 	ste_start_locked(ifp);
1874c8befdd5SWarner Losh 	STE_UNLOCK(sc);
1875c8befdd5SWarner Losh }
1876c8befdd5SWarner Losh 
1877c8befdd5SWarner Losh static void
187860270842SPyun YongHyeon ste_start_locked(struct ifnet *ifp)
1879c8befdd5SWarner Losh {
1880c8befdd5SWarner Losh 	struct ste_softc *sc;
1881c8befdd5SWarner Losh 	struct ste_chain *cur_tx;
1882f2632c3bSPyun YongHyeon 	struct mbuf *m_head = NULL;
1883a1b2c209SPyun YongHyeon 	int enq;
1884c8befdd5SWarner Losh 
1885c8befdd5SWarner Losh 	sc = ifp->if_softc;
1886c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1887c8befdd5SWarner Losh 
18884465097bSPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
18894465097bSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->ste_flags & STE_FLAG_LINK) == 0)
1890c8befdd5SWarner Losh 		return;
1891c8befdd5SWarner Losh 
1892a1b2c209SPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
1893a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_cnt == STE_TX_LIST_CNT - 1) {
1894c8befdd5SWarner Losh 			/*
1895a1b2c209SPyun YongHyeon 			 * Controller may have cached copy of the last used
1896a1b2c209SPyun YongHyeon 			 * next ptr so we have to reserve one TFD to avoid
1897a1b2c209SPyun YongHyeon 			 * TFD overruns.
1898c8befdd5SWarner Losh 			 */
1899c8befdd5SWarner Losh 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1900c8befdd5SWarner Losh 			break;
1901c8befdd5SWarner Losh 		}
1902c8befdd5SWarner Losh 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1903c8befdd5SWarner Losh 		if (m_head == NULL)
1904c8befdd5SWarner Losh 			break;
1905a1b2c209SPyun YongHyeon 		cur_tx = &sc->ste_cdata.ste_tx_chain[sc->ste_cdata.ste_tx_prod];
1906a1b2c209SPyun YongHyeon 		if (ste_encap(sc, &m_head, cur_tx) != 0) {
1907a1b2c209SPyun YongHyeon 			if (m_head == NULL)
1908c8befdd5SWarner Losh 				break;
1909a1b2c209SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1910a1b2c209SPyun YongHyeon 			break;
1911a1b2c209SPyun YongHyeon 		}
1912a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_last_tx == NULL) {
1913a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1914a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map,
1915a1b2c209SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1916c8befdd5SWarner Losh 			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1917c8befdd5SWarner Losh 			ste_wait(sc);
1918c8befdd5SWarner Losh 			CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
1919a1b2c209SPyun YongHyeon 	    		    STE_ADDR_LO(sc->ste_ldata.ste_tx_list_paddr));
1920c8befdd5SWarner Losh 			CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64);
1921c8befdd5SWarner Losh 			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1922c8befdd5SWarner Losh 			ste_wait(sc);
1923c8befdd5SWarner Losh 		} else {
1924a1b2c209SPyun YongHyeon 			sc->ste_cdata.ste_last_tx->ste_ptr->ste_next =
1925a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_last_tx->ste_phys;
1926a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1927a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map,
1928a1b2c209SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1929c8befdd5SWarner Losh 		}
1930a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_last_tx = cur_tx;
1931c8befdd5SWarner Losh 
1932a1b2c209SPyun YongHyeon 		enq++;
1933c8befdd5SWarner Losh 		/*
1934c8befdd5SWarner Losh 		 * If there's a BPF listener, bounce a copy of this frame
1935c8befdd5SWarner Losh 		 * to him.
1936c8befdd5SWarner Losh 	 	 */
1937a1b2c209SPyun YongHyeon 		BPF_MTAP(ifp, m_head);
1938c8befdd5SWarner Losh 	}
1939a1b2c209SPyun YongHyeon 
1940a1b2c209SPyun YongHyeon 	if (enq > 0)
1941a1b2c209SPyun YongHyeon 		sc->ste_timer = STE_TX_TIMEOUT;
1942c8befdd5SWarner Losh }
1943c8befdd5SWarner Losh 
1944c8befdd5SWarner Losh static void
19457cf545d0SJohn Baldwin ste_watchdog(struct ste_softc *sc)
1946c8befdd5SWarner Losh {
19477cf545d0SJohn Baldwin 	struct ifnet *ifp;
1948c8befdd5SWarner Losh 
19497cf545d0SJohn Baldwin 	ifp = sc->ste_ifp;
19507cf545d0SJohn Baldwin 	STE_LOCK_ASSERT(sc);
1951c8befdd5SWarner Losh 
195210f695eeSPyun YongHyeon 	if (sc->ste_timer == 0 || --sc->ste_timer)
195310f695eeSPyun YongHyeon 		return;
195410f695eeSPyun YongHyeon 
1955c8befdd5SWarner Losh 	ifp->if_oerrors++;
1956c8befdd5SWarner Losh 	if_printf(ifp, "watchdog timeout\n");
1957c8befdd5SWarner Losh 
1958c8befdd5SWarner Losh 	ste_txeoc(sc);
1959c8befdd5SWarner Losh 	ste_txeof(sc);
1960a1b2c209SPyun YongHyeon 	ste_rxeof(sc, -1);
1961c8befdd5SWarner Losh 	ste_init_locked(sc);
1962c8befdd5SWarner Losh 
1963c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1964c8befdd5SWarner Losh 		ste_start_locked(ifp);
1965c8befdd5SWarner Losh }
1966c8befdd5SWarner Losh 
1967c8befdd5SWarner Losh static int
196860270842SPyun YongHyeon ste_shutdown(device_t dev)
1969c8befdd5SWarner Losh {
1970c8befdd5SWarner Losh 	struct ste_softc *sc;
1971c8befdd5SWarner Losh 
1972c8befdd5SWarner Losh 	sc = device_get_softc(dev);
1973c8befdd5SWarner Losh 
1974c8befdd5SWarner Losh 	STE_LOCK(sc);
1975c8befdd5SWarner Losh 	ste_stop(sc);
1976c8befdd5SWarner Losh 	STE_UNLOCK(sc);
1977c8befdd5SWarner Losh 
1978c8befdd5SWarner Losh 	return (0);
1979c8befdd5SWarner Losh }
1980