xref: /freebsd/sys/dev/ste/if_ste.c (revision 55d7003e48364cfeecd69c130872cafb87c1857b)
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 
8181598b3eSPyun YongHyeon /* Define to show Tx error status. */
8281598b3eSPyun YongHyeon #define	STE_SHOW_TXERRORS
8381598b3eSPyun YongHyeon 
84c8befdd5SWarner Losh /*
85c8befdd5SWarner Losh  * Various supported device vendors/types and their names.
86c8befdd5SWarner Losh  */
87c8befdd5SWarner Losh static struct ste_type ste_devs[] = {
88c8befdd5SWarner Losh 	{ ST_VENDORID, ST_DEVICEID_ST201_1, "Sundance ST201 10/100BaseTX" },
89c8befdd5SWarner Losh 	{ ST_VENDORID, ST_DEVICEID_ST201_2, "Sundance ST201 10/100BaseTX" },
90c8befdd5SWarner Losh 	{ DL_VENDORID, DL_DEVICEID_DL10050, "D-Link DL10050 10/100BaseTX" },
91c8befdd5SWarner Losh 	{ 0, 0, NULL }
92c8befdd5SWarner Losh };
93c8befdd5SWarner Losh 
94c8befdd5SWarner Losh static int	ste_attach(device_t);
95c8befdd5SWarner Losh static int	ste_detach(device_t);
96084dc54bSPyun YongHyeon static int	ste_probe(device_t);
97c8befdd5SWarner Losh static int	ste_shutdown(device_t);
98084dc54bSPyun YongHyeon 
99a1b2c209SPyun YongHyeon static int	ste_dma_alloc(struct ste_softc *);
100a1b2c209SPyun YongHyeon static void	ste_dma_free(struct ste_softc *);
101a1b2c209SPyun YongHyeon static void	ste_dmamap_cb(void *, bus_dma_segment_t *, int, int);
102084dc54bSPyun YongHyeon static int 	ste_eeprom_wait(struct ste_softc *);
103a1b2c209SPyun YongHyeon static int	ste_encap(struct ste_softc *, struct mbuf **,
104a1b2c209SPyun YongHyeon 		    struct ste_chain *);
105c8befdd5SWarner Losh static int	ste_ifmedia_upd(struct ifnet *);
106c8befdd5SWarner Losh static void	ste_ifmedia_upd_locked(struct ifnet *);
107c8befdd5SWarner Losh static void	ste_ifmedia_sts(struct ifnet *, struct ifmediareq *);
108084dc54bSPyun YongHyeon static void	ste_init(void *);
109084dc54bSPyun YongHyeon static void	ste_init_locked(struct ste_softc *);
110c8befdd5SWarner Losh static int	ste_init_rx_list(struct ste_softc *);
111c8befdd5SWarner Losh static void	ste_init_tx_list(struct ste_softc *);
112084dc54bSPyun YongHyeon static void	ste_intr(void *);
113084dc54bSPyun YongHyeon static int	ste_ioctl(struct ifnet *, u_long, caddr_t);
114084dc54bSPyun YongHyeon static int	ste_mii_readreg(struct ste_softc *, struct ste_mii_frame *);
115084dc54bSPyun YongHyeon static void	ste_mii_send(struct ste_softc *, uint32_t, int);
116084dc54bSPyun YongHyeon static void	ste_mii_sync(struct ste_softc *);
117084dc54bSPyun YongHyeon static int	ste_mii_writereg(struct ste_softc *, struct ste_mii_frame *);
118084dc54bSPyun YongHyeon static int	ste_miibus_readreg(device_t, int, int);
119084dc54bSPyun YongHyeon static void	ste_miibus_statchg(device_t);
120084dc54bSPyun YongHyeon static int	ste_miibus_writereg(device_t, int, int, int);
121a1b2c209SPyun YongHyeon static int	ste_newbuf(struct ste_softc *, struct ste_chain_onefrag *);
122084dc54bSPyun YongHyeon static int	ste_read_eeprom(struct ste_softc *, caddr_t, int, int, int);
123084dc54bSPyun YongHyeon static void	ste_reset(struct ste_softc *);
12481598b3eSPyun YongHyeon static void	ste_restart_tx(struct ste_softc *);
125a1b2c209SPyun YongHyeon static int	ste_rxeof(struct ste_softc *, int);
126084dc54bSPyun YongHyeon static void	ste_setmulti(struct ste_softc *);
127084dc54bSPyun YongHyeon static void	ste_start(struct ifnet *);
128084dc54bSPyun YongHyeon static void	ste_start_locked(struct ifnet *);
12910f695eeSPyun YongHyeon static void	ste_stats_update(struct ste_softc *);
130084dc54bSPyun YongHyeon static void	ste_stop(struct ste_softc *);
13110f695eeSPyun YongHyeon static void	ste_tick(void *);
132084dc54bSPyun YongHyeon static void	ste_txeoc(struct ste_softc *);
133084dc54bSPyun YongHyeon static void	ste_txeof(struct ste_softc *);
134084dc54bSPyun YongHyeon static void	ste_wait(struct ste_softc *);
135084dc54bSPyun YongHyeon static void	ste_watchdog(struct ste_softc *);
136c8befdd5SWarner Losh 
137c8befdd5SWarner Losh static device_method_t ste_methods[] = {
138c8befdd5SWarner Losh 	/* Device interface */
139c8befdd5SWarner Losh 	DEVMETHOD(device_probe,		ste_probe),
140c8befdd5SWarner Losh 	DEVMETHOD(device_attach,	ste_attach),
141c8befdd5SWarner Losh 	DEVMETHOD(device_detach,	ste_detach),
142c8befdd5SWarner Losh 	DEVMETHOD(device_shutdown,	ste_shutdown),
143c8befdd5SWarner Losh 
144c8befdd5SWarner Losh 	/* bus interface */
145c8befdd5SWarner Losh 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
146c8befdd5SWarner Losh 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
147c8befdd5SWarner Losh 
148c8befdd5SWarner Losh 	/* MII interface */
149c8befdd5SWarner Losh 	DEVMETHOD(miibus_readreg,	ste_miibus_readreg),
150c8befdd5SWarner Losh 	DEVMETHOD(miibus_writereg,	ste_miibus_writereg),
151c8befdd5SWarner Losh 	DEVMETHOD(miibus_statchg,	ste_miibus_statchg),
152c8befdd5SWarner Losh 
153c8befdd5SWarner Losh 	{ 0, 0 }
154c8befdd5SWarner Losh };
155c8befdd5SWarner Losh 
156c8befdd5SWarner Losh static driver_t ste_driver = {
157c8befdd5SWarner Losh 	"ste",
158c8befdd5SWarner Losh 	ste_methods,
159c8befdd5SWarner Losh 	sizeof(struct ste_softc)
160c8befdd5SWarner Losh };
161c8befdd5SWarner Losh 
162c8befdd5SWarner Losh static devclass_t ste_devclass;
163c8befdd5SWarner Losh 
164c8befdd5SWarner Losh DRIVER_MODULE(ste, pci, ste_driver, ste_devclass, 0, 0);
165c8befdd5SWarner Losh DRIVER_MODULE(miibus, ste, miibus_driver, miibus_devclass, 0, 0);
166c8befdd5SWarner Losh 
167c8befdd5SWarner Losh #define STE_SETBIT4(sc, reg, x)				\
168c8befdd5SWarner Losh 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
169c8befdd5SWarner Losh 
170c8befdd5SWarner Losh #define STE_CLRBIT4(sc, reg, x)				\
171c8befdd5SWarner Losh 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
172c8befdd5SWarner Losh 
173c8befdd5SWarner Losh #define STE_SETBIT2(sc, reg, x)				\
174c8befdd5SWarner Losh 	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | (x))
175c8befdd5SWarner Losh 
176c8befdd5SWarner Losh #define STE_CLRBIT2(sc, reg, x)				\
177c8befdd5SWarner Losh 	CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~(x))
178c8befdd5SWarner Losh 
179c8befdd5SWarner Losh #define STE_SETBIT1(sc, reg, x)				\
180c8befdd5SWarner Losh 	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | (x))
181c8befdd5SWarner Losh 
182c8befdd5SWarner Losh #define STE_CLRBIT1(sc, reg, x)				\
183c8befdd5SWarner Losh 	CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~(x))
184c8befdd5SWarner Losh 
185c8befdd5SWarner Losh 
186c8befdd5SWarner Losh #define MII_SET(x)		STE_SETBIT1(sc, STE_PHYCTL, x)
187c8befdd5SWarner Losh #define MII_CLR(x)		STE_CLRBIT1(sc, STE_PHYCTL, x)
188c8befdd5SWarner Losh 
189c8befdd5SWarner Losh /*
190c8befdd5SWarner Losh  * Sync the PHYs by setting data bit and strobing the clock 32 times.
191c8befdd5SWarner Losh  */
192c8befdd5SWarner Losh static void
19360270842SPyun YongHyeon ste_mii_sync(struct ste_softc *sc)
194c8befdd5SWarner Losh {
19542306cb0SPyun YongHyeon 	int i;
196c8befdd5SWarner Losh 
197c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA);
198c8befdd5SWarner Losh 
199c8befdd5SWarner Losh 	for (i = 0; i < 32; i++) {
200c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
201c8befdd5SWarner Losh 		DELAY(1);
202c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
203c8befdd5SWarner Losh 		DELAY(1);
204c8befdd5SWarner Losh 	}
205c8befdd5SWarner Losh }
206c8befdd5SWarner Losh 
207c8befdd5SWarner Losh /*
208c8befdd5SWarner Losh  * Clock a series of bits through the MII.
209c8befdd5SWarner Losh  */
210c8befdd5SWarner Losh static void
21156af54f2SPyun YongHyeon ste_mii_send(struct ste_softc *sc, uint32_t bits, int cnt)
212c8befdd5SWarner Losh {
213c8befdd5SWarner Losh 	int i;
214c8befdd5SWarner Losh 
215c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
216c8befdd5SWarner Losh 
217c8befdd5SWarner Losh 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
218c8befdd5SWarner Losh 		if (bits & i) {
219c8befdd5SWarner Losh 			MII_SET(STE_PHYCTL_MDATA);
220c8befdd5SWarner Losh                 } else {
221c8befdd5SWarner Losh 			MII_CLR(STE_PHYCTL_MDATA);
222c8befdd5SWarner Losh                 }
223c8befdd5SWarner Losh 		DELAY(1);
224c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
225c8befdd5SWarner Losh 		DELAY(1);
226c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
227c8befdd5SWarner Losh 	}
228c8befdd5SWarner Losh }
229c8befdd5SWarner Losh 
230c8befdd5SWarner Losh /*
231c8befdd5SWarner Losh  * Read an PHY register through the MII.
232c8befdd5SWarner Losh  */
233c8befdd5SWarner Losh static int
23460270842SPyun YongHyeon ste_mii_readreg(struct ste_softc *sc, struct ste_mii_frame *frame)
235c8befdd5SWarner Losh {
236c8befdd5SWarner Losh 	int i, ack;
237c8befdd5SWarner Losh 
238c8befdd5SWarner Losh 	/*
239c8befdd5SWarner Losh 	 * Set up frame for RX.
240c8befdd5SWarner Losh 	 */
241c8befdd5SWarner Losh 	frame->mii_stdelim = STE_MII_STARTDELIM;
242c8befdd5SWarner Losh 	frame->mii_opcode = STE_MII_READOP;
243c8befdd5SWarner Losh 	frame->mii_turnaround = 0;
244c8befdd5SWarner Losh 	frame->mii_data = 0;
245c8befdd5SWarner Losh 
246c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_PHYCTL, 0);
247c8befdd5SWarner Losh 	/*
248c8befdd5SWarner Losh  	 * Turn on data xmit.
249c8befdd5SWarner Losh 	 */
250c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR);
251c8befdd5SWarner Losh 
252c8befdd5SWarner Losh 	ste_mii_sync(sc);
253c8befdd5SWarner Losh 
254c8befdd5SWarner Losh 	/*
255c8befdd5SWarner Losh 	 * Send command/address info.
256c8befdd5SWarner Losh 	 */
257c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_stdelim, 2);
258c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_opcode, 2);
259c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_phyaddr, 5);
260c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_regaddr, 5);
261c8befdd5SWarner Losh 
262c8befdd5SWarner Losh 	/* Turn off xmit. */
263c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MDIR);
264c8befdd5SWarner Losh 
265c8befdd5SWarner Losh 	/* Idle bit */
266c8befdd5SWarner Losh 	MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA));
267c8befdd5SWarner Losh 	DELAY(1);
268c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
269c8befdd5SWarner Losh 	DELAY(1);
270c8befdd5SWarner Losh 
271c8befdd5SWarner Losh 	/* Check for ack */
272c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
273c8befdd5SWarner Losh 	DELAY(1);
274c8befdd5SWarner Losh 	ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA;
275c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
276c8befdd5SWarner Losh 	DELAY(1);
277c8befdd5SWarner Losh 
278c8befdd5SWarner Losh 	/*
279c8befdd5SWarner Losh 	 * Now try reading data bits. If the ack failed, we still
280c8befdd5SWarner Losh 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
281c8befdd5SWarner Losh 	 */
282c8befdd5SWarner Losh 	if (ack) {
283c8befdd5SWarner Losh 		for (i = 0; i < 16; i++) {
284c8befdd5SWarner Losh 			MII_CLR(STE_PHYCTL_MCLK);
285c8befdd5SWarner Losh 			DELAY(1);
286c8befdd5SWarner Losh 			MII_SET(STE_PHYCTL_MCLK);
287c8befdd5SWarner Losh 			DELAY(1);
288c8befdd5SWarner Losh 		}
289c8befdd5SWarner Losh 		goto fail;
290c8befdd5SWarner Losh 	}
291c8befdd5SWarner Losh 
292c8befdd5SWarner Losh 	for (i = 0x8000; i; i >>= 1) {
293c8befdd5SWarner Losh 		MII_CLR(STE_PHYCTL_MCLK);
294c8befdd5SWarner Losh 		DELAY(1);
295c8befdd5SWarner Losh 		if (!ack) {
296c8befdd5SWarner Losh 			if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA)
297c8befdd5SWarner Losh 				frame->mii_data |= i;
298c8befdd5SWarner Losh 			DELAY(1);
299c8befdd5SWarner Losh 		}
300c8befdd5SWarner Losh 		MII_SET(STE_PHYCTL_MCLK);
301c8befdd5SWarner Losh 		DELAY(1);
302c8befdd5SWarner Losh 	}
303c8befdd5SWarner Losh 
304c8befdd5SWarner Losh fail:
305c8befdd5SWarner Losh 
306c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
307c8befdd5SWarner Losh 	DELAY(1);
308c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
309c8befdd5SWarner Losh 	DELAY(1);
310c8befdd5SWarner Losh 
311c8befdd5SWarner Losh 	if (ack)
312c8befdd5SWarner Losh 		return (1);
313c8befdd5SWarner Losh 	return (0);
314c8befdd5SWarner Losh }
315c8befdd5SWarner Losh 
316c8befdd5SWarner Losh /*
317c8befdd5SWarner Losh  * Write to a PHY register through the MII.
318c8befdd5SWarner Losh  */
319c8befdd5SWarner Losh static int
32060270842SPyun YongHyeon ste_mii_writereg(struct ste_softc *sc, struct ste_mii_frame *frame)
321c8befdd5SWarner Losh {
322c8befdd5SWarner Losh 
323c8befdd5SWarner Losh 	/*
324c8befdd5SWarner Losh 	 * Set up frame for TX.
325c8befdd5SWarner Losh 	 */
326c8befdd5SWarner Losh 
327c8befdd5SWarner Losh 	frame->mii_stdelim = STE_MII_STARTDELIM;
328c8befdd5SWarner Losh 	frame->mii_opcode = STE_MII_WRITEOP;
329c8befdd5SWarner Losh 	frame->mii_turnaround = STE_MII_TURNAROUND;
330c8befdd5SWarner Losh 
331c8befdd5SWarner Losh 	/*
332c8befdd5SWarner Losh  	 * Turn on data output.
333c8befdd5SWarner Losh 	 */
334c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MDIR);
335c8befdd5SWarner Losh 
336c8befdd5SWarner Losh 	ste_mii_sync(sc);
337c8befdd5SWarner Losh 
338c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_stdelim, 2);
339c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_opcode, 2);
340c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_phyaddr, 5);
341c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_regaddr, 5);
342c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_turnaround, 2);
343c8befdd5SWarner Losh 	ste_mii_send(sc, frame->mii_data, 16);
344c8befdd5SWarner Losh 
345c8befdd5SWarner Losh 	/* Idle bit. */
346c8befdd5SWarner Losh 	MII_SET(STE_PHYCTL_MCLK);
347c8befdd5SWarner Losh 	DELAY(1);
348c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MCLK);
349c8befdd5SWarner Losh 	DELAY(1);
350c8befdd5SWarner Losh 
351c8befdd5SWarner Losh 	/*
352c8befdd5SWarner Losh 	 * Turn off xmit.
353c8befdd5SWarner Losh 	 */
354c8befdd5SWarner Losh 	MII_CLR(STE_PHYCTL_MDIR);
355c8befdd5SWarner Losh 
356c8befdd5SWarner Losh 	return (0);
357c8befdd5SWarner Losh }
358c8befdd5SWarner Losh 
359c8befdd5SWarner Losh static int
36060270842SPyun YongHyeon ste_miibus_readreg(device_t dev, int phy, int reg)
361c8befdd5SWarner Losh {
362c8befdd5SWarner Losh 	struct ste_softc *sc;
363c8befdd5SWarner Losh 	struct ste_mii_frame frame;
364c8befdd5SWarner Losh 
365c8befdd5SWarner Losh 	sc = device_get_softc(dev);
366c8befdd5SWarner Losh 
3674465097bSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_ONE_PHY) != 0 && phy != 0)
368c8befdd5SWarner Losh 		return (0);
369c8befdd5SWarner Losh 
370c8befdd5SWarner Losh 	bzero((char *)&frame, sizeof(frame));
371c8befdd5SWarner Losh 
372c8befdd5SWarner Losh 	frame.mii_phyaddr = phy;
373c8befdd5SWarner Losh 	frame.mii_regaddr = reg;
374c8befdd5SWarner Losh 	ste_mii_readreg(sc, &frame);
375c8befdd5SWarner Losh 
376c8befdd5SWarner Losh 	return (frame.mii_data);
377c8befdd5SWarner Losh }
378c8befdd5SWarner Losh 
379c8befdd5SWarner Losh static int
38060270842SPyun YongHyeon ste_miibus_writereg(device_t dev, int phy, int reg, int data)
381c8befdd5SWarner Losh {
382c8befdd5SWarner Losh 	struct ste_softc *sc;
383c8befdd5SWarner Losh 	struct ste_mii_frame frame;
384c8befdd5SWarner Losh 
385c8befdd5SWarner Losh 	sc = device_get_softc(dev);
386c8befdd5SWarner Losh 	bzero((char *)&frame, sizeof(frame));
387c8befdd5SWarner Losh 
388c8befdd5SWarner Losh 	frame.mii_phyaddr = phy;
389c8befdd5SWarner Losh 	frame.mii_regaddr = reg;
390c8befdd5SWarner Losh 	frame.mii_data = data;
391c8befdd5SWarner Losh 
392c8befdd5SWarner Losh 	ste_mii_writereg(sc, &frame);
393c8befdd5SWarner Losh 
394c8befdd5SWarner Losh 	return (0);
395c8befdd5SWarner Losh }
396c8befdd5SWarner Losh 
397c8befdd5SWarner Losh static void
39860270842SPyun YongHyeon ste_miibus_statchg(device_t dev)
399c8befdd5SWarner Losh {
400c8befdd5SWarner Losh 	struct ste_softc *sc;
401c8befdd5SWarner Losh 	struct mii_data *mii;
40210f695eeSPyun YongHyeon 	struct ifnet *ifp;
40310f695eeSPyun YongHyeon 	uint16_t cfg;
404c8befdd5SWarner Losh 
405c8befdd5SWarner Losh 	sc = device_get_softc(dev);
406c8befdd5SWarner Losh 
407c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
40810f695eeSPyun YongHyeon 	ifp = sc->ste_ifp;
40910f695eeSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
41010f695eeSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
41110f695eeSPyun YongHyeon 		return;
412c8befdd5SWarner Losh 
41310f695eeSPyun YongHyeon 	sc->ste_flags &= ~STE_FLAG_LINK;
41410f695eeSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
41510f695eeSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
41610f695eeSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
41710f695eeSPyun YongHyeon 		case IFM_10_T:
41810f695eeSPyun YongHyeon 		case IFM_100_TX:
41910f695eeSPyun YongHyeon 		case IFM_100_FX:
42010f695eeSPyun YongHyeon 		case IFM_100_T4:
42110f695eeSPyun YongHyeon 			sc->ste_flags |= STE_FLAG_LINK;
42210f695eeSPyun YongHyeon 		default:
42310f695eeSPyun YongHyeon 			break;
42410f695eeSPyun YongHyeon 		}
42510f695eeSPyun YongHyeon 	}
42610f695eeSPyun YongHyeon 
42710f695eeSPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
42810f695eeSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_LINK) != 0) {
42910f695eeSPyun YongHyeon 		cfg = CSR_READ_2(sc, STE_MACCTL0);
43010f695eeSPyun YongHyeon 		cfg &= ~(STE_MACCTL0_FLOWCTL_ENABLE | STE_MACCTL0_FULLDUPLEX);
43110f695eeSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
43210f695eeSPyun YongHyeon 			/*
43310f695eeSPyun YongHyeon 			 * ST201 data sheet says driver should enable receiving
43410f695eeSPyun YongHyeon 			 * MAC control frames bit of receive mode register to
43510f695eeSPyun YongHyeon 			 * receive flow-control frames but the register has no
43610f695eeSPyun YongHyeon 			 * such bits. In addition the controller has no ability
43710f695eeSPyun YongHyeon 			 * to send pause frames so it should be handled in
43810f695eeSPyun YongHyeon 			 * driver. Implementing pause timer handling in driver
43910f695eeSPyun YongHyeon 			 * layer is not trivial, so don't enable flow-control
44010f695eeSPyun YongHyeon 			 * here.
44110f695eeSPyun YongHyeon 			 */
44210f695eeSPyun YongHyeon 			cfg |= STE_MACCTL0_FULLDUPLEX;
44310f695eeSPyun YongHyeon 		}
44410f695eeSPyun YongHyeon 		CSR_WRITE_2(sc, STE_MACCTL0, cfg);
445c8befdd5SWarner Losh 	}
446c8befdd5SWarner Losh }
447c8befdd5SWarner Losh 
448c8befdd5SWarner Losh static int
44960270842SPyun YongHyeon ste_ifmedia_upd(struct ifnet *ifp)
450c8befdd5SWarner Losh {
451c8befdd5SWarner Losh 	struct ste_softc *sc;
452c8befdd5SWarner Losh 
453c8befdd5SWarner Losh 	sc = ifp->if_softc;
454c8befdd5SWarner Losh 	STE_LOCK(sc);
455c8befdd5SWarner Losh 	ste_ifmedia_upd_locked(ifp);
456c8befdd5SWarner Losh 	STE_UNLOCK(sc);
457c8befdd5SWarner Losh 
458c8befdd5SWarner Losh 	return (0);
459c8befdd5SWarner Losh }
460c8befdd5SWarner Losh 
461c8befdd5SWarner Losh static void
46260270842SPyun YongHyeon ste_ifmedia_upd_locked(struct ifnet *ifp)
463c8befdd5SWarner Losh {
464c8befdd5SWarner Losh 	struct ste_softc *sc;
465c8befdd5SWarner Losh 	struct mii_data *mii;
466c8befdd5SWarner Losh 
467c8befdd5SWarner Losh 	sc = ifp->if_softc;
468c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
469c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
4704465097bSPyun YongHyeon 	sc->ste_flags &= ~STE_FLAG_LINK;
471c8befdd5SWarner Losh 	if (mii->mii_instance) {
472c8befdd5SWarner Losh 		struct mii_softc	*miisc;
473c8befdd5SWarner Losh 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
474c8befdd5SWarner Losh 			mii_phy_reset(miisc);
475c8befdd5SWarner Losh 	}
476c8befdd5SWarner Losh 	mii_mediachg(mii);
477c8befdd5SWarner Losh }
478c8befdd5SWarner Losh 
479c8befdd5SWarner Losh static void
48060270842SPyun YongHyeon ste_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
481c8befdd5SWarner Losh {
482c8befdd5SWarner Losh 	struct ste_softc *sc;
483c8befdd5SWarner Losh 	struct mii_data *mii;
484c8befdd5SWarner Losh 
485c8befdd5SWarner Losh 	sc = ifp->if_softc;
486c8befdd5SWarner Losh 	mii = device_get_softc(sc->ste_miibus);
487c8befdd5SWarner Losh 
488c8befdd5SWarner Losh 	STE_LOCK(sc);
489c8befdd5SWarner Losh 	mii_pollstat(mii);
490c8befdd5SWarner Losh 	ifmr->ifm_active = mii->mii_media_active;
491c8befdd5SWarner Losh 	ifmr->ifm_status = mii->mii_media_status;
492c8befdd5SWarner Losh 	STE_UNLOCK(sc);
493c8befdd5SWarner Losh }
494c8befdd5SWarner Losh 
495c8befdd5SWarner Losh static void
49660270842SPyun YongHyeon ste_wait(struct ste_softc *sc)
497c8befdd5SWarner Losh {
49842306cb0SPyun YongHyeon 	int i;
499c8befdd5SWarner Losh 
500c8befdd5SWarner Losh 	for (i = 0; i < STE_TIMEOUT; i++) {
501c8befdd5SWarner Losh 		if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG))
502c8befdd5SWarner Losh 			break;
5031bf71544SPyun YongHyeon 		DELAY(1);
504c8befdd5SWarner Losh 	}
505c8befdd5SWarner Losh 
506c8befdd5SWarner Losh 	if (i == STE_TIMEOUT)
507c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "command never completed!\n");
508c8befdd5SWarner Losh }
509c8befdd5SWarner Losh 
510c8befdd5SWarner Losh /*
511c8befdd5SWarner Losh  * The EEPROM is slow: give it time to come ready after issuing
512c8befdd5SWarner Losh  * it a command.
513c8befdd5SWarner Losh  */
514c8befdd5SWarner Losh static int
51560270842SPyun YongHyeon ste_eeprom_wait(struct ste_softc *sc)
516c8befdd5SWarner Losh {
517c8befdd5SWarner Losh 	int i;
518c8befdd5SWarner Losh 
519c8befdd5SWarner Losh 	DELAY(1000);
520c8befdd5SWarner Losh 
521c8befdd5SWarner Losh 	for (i = 0; i < 100; i++) {
522c8befdd5SWarner Losh 		if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY)
523c8befdd5SWarner Losh 			DELAY(1000);
524c8befdd5SWarner Losh 		else
525c8befdd5SWarner Losh 			break;
526c8befdd5SWarner Losh 	}
527c8befdd5SWarner Losh 
528c8befdd5SWarner Losh 	if (i == 100) {
529c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "eeprom failed to come ready\n");
530c8befdd5SWarner Losh 		return (1);
531c8befdd5SWarner Losh 	}
532c8befdd5SWarner Losh 
533c8befdd5SWarner Losh 	return (0);
534c8befdd5SWarner Losh }
535c8befdd5SWarner Losh 
536c8befdd5SWarner Losh /*
537c8befdd5SWarner Losh  * Read a sequence of words from the EEPROM. Note that ethernet address
538c8befdd5SWarner Losh  * data is stored in the EEPROM in network byte order.
539c8befdd5SWarner Losh  */
540c8befdd5SWarner Losh static int
54160270842SPyun YongHyeon ste_read_eeprom(struct ste_softc *sc, caddr_t dest, int off, int cnt, int swap)
542c8befdd5SWarner Losh {
543f2632c3bSPyun YongHyeon 	uint16_t word, *ptr;
544c8befdd5SWarner Losh 	int err = 0, i;
545c8befdd5SWarner Losh 
546c8befdd5SWarner Losh 	if (ste_eeprom_wait(sc))
547c8befdd5SWarner Losh 		return (1);
548c8befdd5SWarner Losh 
549c8befdd5SWarner Losh 	for (i = 0; i < cnt; i++) {
550c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i));
551c8befdd5SWarner Losh 		err = ste_eeprom_wait(sc);
552c8befdd5SWarner Losh 		if (err)
553c8befdd5SWarner Losh 			break;
554c8befdd5SWarner Losh 		word = CSR_READ_2(sc, STE_EEPROM_DATA);
55556af54f2SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
556c8befdd5SWarner Losh 		if (swap)
557c8befdd5SWarner Losh 			*ptr = ntohs(word);
558c8befdd5SWarner Losh 		else
559c8befdd5SWarner Losh 			*ptr = word;
560c8befdd5SWarner Losh 	}
561c8befdd5SWarner Losh 
562c8befdd5SWarner Losh 	return (err ? 1 : 0);
563c8befdd5SWarner Losh }
564c8befdd5SWarner Losh 
565c8befdd5SWarner Losh static void
56660270842SPyun YongHyeon ste_setmulti(struct ste_softc *sc)
567c8befdd5SWarner Losh {
568c8befdd5SWarner Losh 	struct ifnet *ifp;
569c8befdd5SWarner Losh 	struct ifmultiaddr *ifma;
570f2632c3bSPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
571f2632c3bSPyun YongHyeon 	int h;
572c8befdd5SWarner Losh 
573c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
574c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
575c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
576c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
577c8befdd5SWarner Losh 		return;
578c8befdd5SWarner Losh 	}
579c8befdd5SWarner Losh 
580c8befdd5SWarner Losh 	/* first, zot all the existing hash bits */
581c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR0, 0);
582c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR1, 0);
583c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR2, 0);
584c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR3, 0);
585c8befdd5SWarner Losh 
586c8befdd5SWarner Losh 	/* now program new ones */
587eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
588c8befdd5SWarner Losh 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
589c8befdd5SWarner Losh 		if (ifma->ifma_addr->sa_family != AF_LINK)
590c8befdd5SWarner Losh 			continue;
591c8befdd5SWarner Losh 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
592c8befdd5SWarner Losh 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x3F;
593c8befdd5SWarner Losh 		if (h < 32)
594c8befdd5SWarner Losh 			hashes[0] |= (1 << h);
595c8befdd5SWarner Losh 		else
596c8befdd5SWarner Losh 			hashes[1] |= (1 << (h - 32));
597c8befdd5SWarner Losh 	}
598eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
599c8befdd5SWarner Losh 
600c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR0, hashes[0] & 0xFFFF);
601c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR1, (hashes[0] >> 16) & 0xFFFF);
602c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR2, hashes[1] & 0xFFFF);
603c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAR3, (hashes[1] >> 16) & 0xFFFF);
604c8befdd5SWarner Losh 	STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_ALLMULTI);
605c8befdd5SWarner Losh 	STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_MULTIHASH);
606c8befdd5SWarner Losh }
607c8befdd5SWarner Losh 
608c8befdd5SWarner Losh #ifdef DEVICE_POLLING
609c8befdd5SWarner Losh static poll_handler_t ste_poll, ste_poll_locked;
610c8befdd5SWarner Losh 
6111abcdbd1SAttilio Rao static int
612c8befdd5SWarner Losh ste_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
613c8befdd5SWarner Losh {
614c8befdd5SWarner Losh 	struct ste_softc *sc = ifp->if_softc;
6151abcdbd1SAttilio Rao 	int rx_npkts = 0;
616c8befdd5SWarner Losh 
617c8befdd5SWarner Losh 	STE_LOCK(sc);
618c8befdd5SWarner Losh 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6191abcdbd1SAttilio Rao 		rx_npkts = ste_poll_locked(ifp, cmd, count);
620c8befdd5SWarner Losh 	STE_UNLOCK(sc);
6211abcdbd1SAttilio Rao 	return (rx_npkts);
622c8befdd5SWarner Losh }
623c8befdd5SWarner Losh 
6241abcdbd1SAttilio Rao static int
625c8befdd5SWarner Losh ste_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
626c8befdd5SWarner Losh {
627c8befdd5SWarner Losh 	struct ste_softc *sc = ifp->if_softc;
6281abcdbd1SAttilio Rao 	int rx_npkts;
629c8befdd5SWarner Losh 
630c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
631c8befdd5SWarner Losh 
632a1b2c209SPyun YongHyeon 	rx_npkts = ste_rxeof(sc, count);
633c8befdd5SWarner Losh 	ste_txeof(sc);
63481598b3eSPyun YongHyeon 	ste_txeoc(sc);
635c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
636c8befdd5SWarner Losh 		ste_start_locked(ifp);
637c8befdd5SWarner Losh 
638c8befdd5SWarner Losh 	if (cmd == POLL_AND_CHECK_STATUS) {
63956af54f2SPyun YongHyeon 		uint16_t status;
640c8befdd5SWarner Losh 
641c8befdd5SWarner Losh 		status = CSR_READ_2(sc, STE_ISR_ACK);
642c8befdd5SWarner Losh 
64310f695eeSPyun YongHyeon 		if (status & STE_ISR_STATS_OFLOW)
644c8befdd5SWarner Losh 			ste_stats_update(sc);
645c8befdd5SWarner Losh 
64655d7003eSPyun YongHyeon 		if (status & STE_ISR_HOSTERR) {
64755d7003eSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
648c8befdd5SWarner Losh 			ste_init_locked(sc);
649c8befdd5SWarner Losh 		}
65055d7003eSPyun YongHyeon 	}
6511abcdbd1SAttilio Rao 	return (rx_npkts);
652c8befdd5SWarner Losh }
653c8befdd5SWarner Losh #endif /* DEVICE_POLLING */
654c8befdd5SWarner Losh 
655c8befdd5SWarner Losh static void
65660270842SPyun YongHyeon ste_intr(void *xsc)
657c8befdd5SWarner Losh {
658c8befdd5SWarner Losh 	struct ste_softc *sc;
659c8befdd5SWarner Losh 	struct ifnet *ifp;
66056af54f2SPyun YongHyeon 	uint16_t status;
661c8befdd5SWarner Losh 
662c8befdd5SWarner Losh 	sc = xsc;
663c8befdd5SWarner Losh 	STE_LOCK(sc);
664c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
665c8befdd5SWarner Losh 
666c8befdd5SWarner Losh #ifdef DEVICE_POLLING
667c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING) {
668c8befdd5SWarner Losh 		STE_UNLOCK(sc);
669c8befdd5SWarner Losh 		return;
670c8befdd5SWarner Losh 	}
671c8befdd5SWarner Losh #endif
672c8befdd5SWarner Losh 
673c8befdd5SWarner Losh 	/* See if this is really our interrupt. */
674c8befdd5SWarner Losh 	if (!(CSR_READ_2(sc, STE_ISR) & STE_ISR_INTLATCH)) {
675c8befdd5SWarner Losh 		STE_UNLOCK(sc);
676c8befdd5SWarner Losh 		return;
677c8befdd5SWarner Losh 	}
678c8befdd5SWarner Losh 
679c8befdd5SWarner Losh 	for (;;) {
680c8befdd5SWarner Losh 		status = CSR_READ_2(sc, STE_ISR_ACK);
681c8befdd5SWarner Losh 
682c8befdd5SWarner Losh 		if (!(status & STE_INTRS))
683c8befdd5SWarner Losh 			break;
684c8befdd5SWarner Losh 
685a1b2c209SPyun YongHyeon 		if (status & STE_ISR_RX_DMADONE)
686a1b2c209SPyun YongHyeon 			ste_rxeof(sc, -1);
687c8befdd5SWarner Losh 
688c8befdd5SWarner Losh 		if (status & STE_ISR_TX_DMADONE)
689c8befdd5SWarner Losh 			ste_txeof(sc);
690c8befdd5SWarner Losh 
691c8befdd5SWarner Losh 		if (status & STE_ISR_TX_DONE)
692c8befdd5SWarner Losh 			ste_txeoc(sc);
693c8befdd5SWarner Losh 
69410f695eeSPyun YongHyeon 		if (status & STE_ISR_STATS_OFLOW)
695c8befdd5SWarner Losh 			ste_stats_update(sc);
696c8befdd5SWarner Losh 
69755d7003eSPyun YongHyeon 		if (status & STE_ISR_HOSTERR) {
698c8befdd5SWarner Losh 			ste_init_locked(sc);
69955d7003eSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
70055d7003eSPyun YongHyeon 		}
701c8befdd5SWarner Losh 	}
702c8befdd5SWarner Losh 
703c8befdd5SWarner Losh 	/* Re-enable interrupts */
704c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
705c8befdd5SWarner Losh 
706c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
707c8befdd5SWarner Losh 		ste_start_locked(ifp);
708c8befdd5SWarner Losh 
709c8befdd5SWarner Losh 	STE_UNLOCK(sc);
710c8befdd5SWarner Losh }
711c8befdd5SWarner Losh 
712c8befdd5SWarner Losh /*
713c8befdd5SWarner Losh  * A frame has been uploaded: pass the resulting mbuf chain up to
714c8befdd5SWarner Losh  * the higher level protocols.
715c8befdd5SWarner Losh  */
7161abcdbd1SAttilio Rao static int
717a1b2c209SPyun YongHyeon ste_rxeof(struct ste_softc *sc, int count)
718c8befdd5SWarner Losh {
719c8befdd5SWarner Losh         struct mbuf *m;
720c8befdd5SWarner Losh         struct ifnet *ifp;
721c8befdd5SWarner Losh 	struct ste_chain_onefrag *cur_rx;
72256af54f2SPyun YongHyeon 	uint32_t rxstat;
723a1b2c209SPyun YongHyeon 	int total_len, rx_npkts;
724c8befdd5SWarner Losh 
725c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
726c8befdd5SWarner Losh 
727a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
728a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map,
729a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
730c8befdd5SWarner Losh 
731c8befdd5SWarner Losh 	cur_rx = sc->ste_cdata.ste_rx_head;
732a1b2c209SPyun YongHyeon 	for (rx_npkts = 0; rx_npkts < STE_RX_LIST_CNT; rx_npkts++,
733a1b2c209SPyun YongHyeon 	    cur_rx = cur_rx->ste_next) {
734a1b2c209SPyun YongHyeon 		rxstat = le32toh(cur_rx->ste_ptr->ste_status);
735a1b2c209SPyun YongHyeon 		if ((rxstat & STE_RXSTAT_DMADONE) == 0)
736a1b2c209SPyun YongHyeon 			break;
737a1b2c209SPyun YongHyeon #ifdef DEVICE_POLLING
738a1b2c209SPyun YongHyeon 		if (ifp->if_capenable & IFCAP_POLLING) {
739a1b2c209SPyun YongHyeon 			if (count == 0)
740a1b2c209SPyun YongHyeon 				break;
741a1b2c209SPyun YongHyeon 			count--;
742a1b2c209SPyun YongHyeon 		}
743a1b2c209SPyun YongHyeon #endif
744a1b2c209SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
745a1b2c209SPyun YongHyeon 			break;
746c8befdd5SWarner Losh 		/*
747c8befdd5SWarner Losh 		 * If an error occurs, update stats, clear the
748c8befdd5SWarner Losh 		 * status word and leave the mbuf cluster in place:
749c8befdd5SWarner Losh 		 * it should simply get re-used next time this descriptor
750c8befdd5SWarner Losh 	 	 * comes up in the ring.
751c8befdd5SWarner Losh 		 */
752c8befdd5SWarner Losh 		if (rxstat & STE_RXSTAT_FRAME_ERR) {
753c8befdd5SWarner Losh 			ifp->if_ierrors++;
754c8befdd5SWarner Losh 			cur_rx->ste_ptr->ste_status = 0;
755c8befdd5SWarner Losh 			continue;
756c8befdd5SWarner Losh 		}
757c8befdd5SWarner Losh 
758c8befdd5SWarner Losh 		/* No errors; receive the packet. */
759c8befdd5SWarner Losh 		m = cur_rx->ste_mbuf;
760a1b2c209SPyun YongHyeon 		total_len = STE_RX_BYTES(rxstat);
761c8befdd5SWarner Losh 
762c8befdd5SWarner Losh 		/*
763c8befdd5SWarner Losh 		 * Try to conjure up a new mbuf cluster. If that
764c8befdd5SWarner Losh 		 * fails, it means we have an out of memory condition and
765c8befdd5SWarner Losh 		 * should leave the buffer in place and continue. This will
766c8befdd5SWarner Losh 		 * result in a lost packet, but there's little else we
767c8befdd5SWarner Losh 		 * can do in this situation.
768c8befdd5SWarner Losh 		 */
769a1b2c209SPyun YongHyeon 		if (ste_newbuf(sc, cur_rx) != 0) {
770c8befdd5SWarner Losh 			ifp->if_ierrors++;
771c8befdd5SWarner Losh 			cur_rx->ste_ptr->ste_status = 0;
772c8befdd5SWarner Losh 			continue;
773c8befdd5SWarner Losh 		}
774c8befdd5SWarner Losh 
775c8befdd5SWarner Losh 		m->m_pkthdr.rcvif = ifp;
776c8befdd5SWarner Losh 		m->m_pkthdr.len = m->m_len = total_len;
777c8befdd5SWarner Losh 
778c8befdd5SWarner Losh 		ifp->if_ipackets++;
779c8befdd5SWarner Losh 		STE_UNLOCK(sc);
780c8befdd5SWarner Losh 		(*ifp->if_input)(ifp, m);
781c8befdd5SWarner Losh 		STE_LOCK(sc);
782a1b2c209SPyun YongHyeon 	}
783c8befdd5SWarner Losh 
784a1b2c209SPyun YongHyeon 	if (rx_npkts > 0) {
785a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_head = cur_rx;
786a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
787a1b2c209SPyun YongHyeon 		    sc->ste_cdata.ste_rx_list_map,
788a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
789c8befdd5SWarner Losh 	}
790c8befdd5SWarner Losh 
7911abcdbd1SAttilio Rao 	return (rx_npkts);
792c8befdd5SWarner Losh }
793c8befdd5SWarner Losh 
794c8befdd5SWarner Losh static void
79560270842SPyun YongHyeon ste_txeoc(struct ste_softc *sc)
796c8befdd5SWarner Losh {
79781598b3eSPyun YongHyeon 	uint16_t txstat;
798c8befdd5SWarner Losh 	struct ifnet *ifp;
79981598b3eSPyun YongHyeon 
80081598b3eSPyun YongHyeon 	STE_LOCK_ASSERT(sc);
801c8befdd5SWarner Losh 
802c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
803c8befdd5SWarner Losh 
80481598b3eSPyun YongHyeon 	/*
80581598b3eSPyun YongHyeon 	 * STE_TX_STATUS register implements a queue of up to 31
80681598b3eSPyun YongHyeon 	 * transmit status byte. Writing an arbitrary value to the
80781598b3eSPyun YongHyeon 	 * register will advance the queue to the next transmit
80881598b3eSPyun YongHyeon 	 * status byte. This means if driver does not read
80981598b3eSPyun YongHyeon 	 * STE_TX_STATUS register after completing sending more
81081598b3eSPyun YongHyeon 	 * than 31 frames the controller would be stalled so driver
81181598b3eSPyun YongHyeon 	 * should re-wake the Tx MAC. This is the most severe
81281598b3eSPyun YongHyeon 	 * limitation of ST201 based controller.
81381598b3eSPyun YongHyeon 	 */
81481598b3eSPyun YongHyeon 	for (;;) {
81581598b3eSPyun YongHyeon 		txstat = CSR_READ_2(sc, STE_TX_STATUS);
81681598b3eSPyun YongHyeon 		if ((txstat & STE_TXSTATUS_TXDONE) == 0)
81781598b3eSPyun YongHyeon 			break;
81881598b3eSPyun YongHyeon 		if ((txstat & (STE_TXSTATUS_UNDERRUN |
81981598b3eSPyun YongHyeon 		    STE_TXSTATUS_EXCESSCOLLS | STE_TXSTATUS_RECLAIMERR |
82081598b3eSPyun YongHyeon 		    STE_TXSTATUS_STATSOFLOW)) != 0) {
821c8befdd5SWarner Losh 			ifp->if_oerrors++;
82281598b3eSPyun YongHyeon #ifdef	STE_SHOW_TXERRORS
82381598b3eSPyun YongHyeon 			device_printf(sc->ste_dev, "TX error : 0x%b\n",
82481598b3eSPyun YongHyeon 			    txstat & 0xFF, STE_ERR_BITS);
82581598b3eSPyun YongHyeon #endif
82681598b3eSPyun YongHyeon 			if ((txstat & STE_TXSTATUS_UNDERRUN) != 0 &&
827c8befdd5SWarner Losh 			    sc->ste_tx_thresh < STE_PACKET_SIZE) {
828c8befdd5SWarner Losh 				sc->ste_tx_thresh += STE_MIN_FRAMELEN;
82981598b3eSPyun YongHyeon 				if (sc->ste_tx_thresh > STE_PACKET_SIZE)
83081598b3eSPyun YongHyeon 					sc->ste_tx_thresh = STE_PACKET_SIZE;
831c8befdd5SWarner Losh 				device_printf(sc->ste_dev,
83281598b3eSPyun YongHyeon 				    "TX underrun, increasing TX"
833c8befdd5SWarner Losh 				    " start threshold to %d bytes\n",
834c8befdd5SWarner Losh 				    sc->ste_tx_thresh);
83581598b3eSPyun YongHyeon 				/* Make sure to disable active DMA cycles. */
83681598b3eSPyun YongHyeon 				STE_SETBIT4(sc, STE_DMACTL,
83781598b3eSPyun YongHyeon 				    STE_DMACTL_TXDMA_STALL);
83881598b3eSPyun YongHyeon 				ste_wait(sc);
83955d7003eSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
840c8befdd5SWarner Losh 				ste_init_locked(sc);
84181598b3eSPyun YongHyeon 				break;
84281598b3eSPyun YongHyeon 			}
84381598b3eSPyun YongHyeon 			/* Restart Tx. */
84481598b3eSPyun YongHyeon 			ste_restart_tx(sc);
84581598b3eSPyun YongHyeon 		}
84681598b3eSPyun YongHyeon 		/*
84781598b3eSPyun YongHyeon 		 * Advance to next status and ACK TxComplete
84881598b3eSPyun YongHyeon 		 * interrupt. ST201 data sheet was wrong here, to
84981598b3eSPyun YongHyeon 		 * get next Tx status, we have to write both
85081598b3eSPyun YongHyeon 		 * STE_TX_STATUS and STE_TX_FRAMEID register.
85181598b3eSPyun YongHyeon 		 * Otherwise controller returns the same status
85281598b3eSPyun YongHyeon 		 * as well as not acknowledge Tx completion
85381598b3eSPyun YongHyeon 		 * interrupt.
85481598b3eSPyun YongHyeon 		 */
855c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_TX_STATUS, txstat);
856c8befdd5SWarner Losh 	}
857c8befdd5SWarner Losh }
858c8befdd5SWarner Losh 
859c8befdd5SWarner Losh static void
86010f695eeSPyun YongHyeon ste_tick(void *arg)
86110f695eeSPyun YongHyeon {
86210f695eeSPyun YongHyeon 	struct ste_softc *sc;
86310f695eeSPyun YongHyeon 	struct mii_data *mii;
86410f695eeSPyun YongHyeon 
86510f695eeSPyun YongHyeon 	sc = (struct ste_softc *)arg;
86610f695eeSPyun YongHyeon 
86710f695eeSPyun YongHyeon 	STE_LOCK_ASSERT(sc);
86810f695eeSPyun YongHyeon 
86910f695eeSPyun YongHyeon 	mii = device_get_softc(sc->ste_miibus);
87010f695eeSPyun YongHyeon 	mii_tick(mii);
87110f695eeSPyun YongHyeon 	/*
87210f695eeSPyun YongHyeon 	 * ukphy(4) does not seem to generate CB that reports
87310f695eeSPyun YongHyeon 	 * resolved link state so if we know we lost a link,
87410f695eeSPyun YongHyeon 	 * explicitly check the link state.
87510f695eeSPyun YongHyeon 	 */
87610f695eeSPyun YongHyeon 	if ((sc->ste_flags & STE_FLAG_LINK) == 0)
87710f695eeSPyun YongHyeon 		ste_miibus_statchg(sc->ste_dev);
87810f695eeSPyun YongHyeon 	ste_stats_update(sc);
87910f695eeSPyun YongHyeon 	ste_watchdog(sc);
88010f695eeSPyun YongHyeon 	callout_reset(&sc->ste_callout, hz, ste_tick, sc);
88110f695eeSPyun YongHyeon }
88210f695eeSPyun YongHyeon 
88310f695eeSPyun YongHyeon static void
88460270842SPyun YongHyeon ste_txeof(struct ste_softc *sc)
885c8befdd5SWarner Losh {
886c8befdd5SWarner Losh 	struct ifnet *ifp;
887f2632c3bSPyun YongHyeon 	struct ste_chain *cur_tx;
888a1b2c209SPyun YongHyeon 	uint32_t txstat;
889c8befdd5SWarner Losh 	int idx;
890c8befdd5SWarner Losh 
891a1b2c209SPyun YongHyeon 	STE_LOCK_ASSERT(sc);
892c8befdd5SWarner Losh 
893a1b2c209SPyun YongHyeon 	ifp = sc->ste_ifp;
894c8befdd5SWarner Losh 	idx = sc->ste_cdata.ste_tx_cons;
895a1b2c209SPyun YongHyeon 	if (idx == sc->ste_cdata.ste_tx_prod)
896a1b2c209SPyun YongHyeon 		return;
897a1b2c209SPyun YongHyeon 
898a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
899a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map,
900a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
901a1b2c209SPyun YongHyeon 
902c8befdd5SWarner Losh 	while (idx != sc->ste_cdata.ste_tx_prod) {
903c8befdd5SWarner Losh 		cur_tx = &sc->ste_cdata.ste_tx_chain[idx];
904a1b2c209SPyun YongHyeon 		txstat = le32toh(cur_tx->ste_ptr->ste_ctl);
905a1b2c209SPyun YongHyeon 		if ((txstat & STE_TXCTL_DMADONE) == 0)
906c8befdd5SWarner Losh 			break;
907a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map,
908a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
909a1b2c209SPyun YongHyeon 		bus_dmamap_unload(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map);
910a1b2c209SPyun YongHyeon 		KASSERT(cur_tx->ste_mbuf != NULL,
911a1b2c209SPyun YongHyeon 		    ("%s: freeing NULL mbuf!\n", __func__));
912c8befdd5SWarner Losh 		m_freem(cur_tx->ste_mbuf);
913c8befdd5SWarner Losh 		cur_tx->ste_mbuf = NULL;
914c8befdd5SWarner Losh 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
915c8befdd5SWarner Losh 		ifp->if_opackets++;
916a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_cnt--;
917c8befdd5SWarner Losh 		STE_INC(idx, STE_TX_LIST_CNT);
918c8befdd5SWarner Losh 	}
919c8befdd5SWarner Losh 
920c8befdd5SWarner Losh 	sc->ste_cdata.ste_tx_cons = idx;
921a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_cnt == 0)
9227cf545d0SJohn Baldwin 		sc->ste_timer = 0;
923c8befdd5SWarner Losh }
924c8befdd5SWarner Losh 
925c8befdd5SWarner Losh static void
92610f695eeSPyun YongHyeon ste_stats_update(struct ste_softc *sc)
927c8befdd5SWarner Losh {
928c8befdd5SWarner Losh 	struct ifnet *ifp;
929c8befdd5SWarner Losh 
930c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
931c8befdd5SWarner Losh 
932c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
933c8befdd5SWarner Losh 	ifp->if_collisions += CSR_READ_1(sc, STE_LATE_COLLS)
934c8befdd5SWarner Losh 	    + CSR_READ_1(sc, STE_MULTI_COLLS)
935c8befdd5SWarner Losh 	    + CSR_READ_1(sc, STE_SINGLE_COLLS);
936c8befdd5SWarner Losh }
937c8befdd5SWarner Losh 
938c8befdd5SWarner Losh /*
939c8befdd5SWarner Losh  * Probe for a Sundance ST201 chip. Check the PCI vendor and device
940c8befdd5SWarner Losh  * IDs against our list and return a device name if we find a match.
941c8befdd5SWarner Losh  */
942c8befdd5SWarner Losh static int
94360270842SPyun YongHyeon ste_probe(device_t dev)
944c8befdd5SWarner Losh {
945c8befdd5SWarner Losh 	struct ste_type *t;
946c8befdd5SWarner Losh 
947c8befdd5SWarner Losh 	t = ste_devs;
948c8befdd5SWarner Losh 
949c8befdd5SWarner Losh 	while (t->ste_name != NULL) {
950c8befdd5SWarner Losh 		if ((pci_get_vendor(dev) == t->ste_vid) &&
951c8befdd5SWarner Losh 		    (pci_get_device(dev) == t->ste_did)) {
952c8befdd5SWarner Losh 			device_set_desc(dev, t->ste_name);
953c8befdd5SWarner Losh 			return (BUS_PROBE_DEFAULT);
954c8befdd5SWarner Losh 		}
955c8befdd5SWarner Losh 		t++;
956c8befdd5SWarner Losh 	}
957c8befdd5SWarner Losh 
958c8befdd5SWarner Losh 	return (ENXIO);
959c8befdd5SWarner Losh }
960c8befdd5SWarner Losh 
961c8befdd5SWarner Losh /*
962c8befdd5SWarner Losh  * Attach the interface. Allocate softc structures, do ifmedia
963c8befdd5SWarner Losh  * setup and ethernet/BPF attach.
964c8befdd5SWarner Losh  */
965c8befdd5SWarner Losh static int
96660270842SPyun YongHyeon ste_attach(device_t dev)
967c8befdd5SWarner Losh {
968c8befdd5SWarner Losh 	struct ste_softc *sc;
969c8befdd5SWarner Losh 	struct ifnet *ifp;
970c8befdd5SWarner Losh 	u_char eaddr[6];
971f2632c3bSPyun YongHyeon 	int error = 0, rid;
972c8befdd5SWarner Losh 
973c8befdd5SWarner Losh 	sc = device_get_softc(dev);
974c8befdd5SWarner Losh 	sc->ste_dev = dev;
975c8befdd5SWarner Losh 
976c8befdd5SWarner Losh 	/*
977c8befdd5SWarner Losh 	 * Only use one PHY since this chip reports multiple
978c8befdd5SWarner Losh 	 * Note on the DFE-550 the PHY is at 1 on the DFE-580
979c8befdd5SWarner Losh 	 * it is at 0 & 1.  It is rev 0x12.
980c8befdd5SWarner Losh 	 */
981c8befdd5SWarner Losh 	if (pci_get_vendor(dev) == DL_VENDORID &&
982c8befdd5SWarner Losh 	    pci_get_device(dev) == DL_DEVICEID_DL10050 &&
983c8befdd5SWarner Losh 	    pci_get_revid(dev) == 0x12 )
9844465097bSPyun YongHyeon 		sc->ste_flags |= STE_FLAG_ONE_PHY;
985c8befdd5SWarner Losh 
986c8befdd5SWarner Losh 	mtx_init(&sc->ste_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
987c8befdd5SWarner Losh 	    MTX_DEF);
988c8befdd5SWarner Losh 	/*
989c8befdd5SWarner Losh 	 * Map control/status registers.
990c8befdd5SWarner Losh 	 */
991c8befdd5SWarner Losh 	pci_enable_busmaster(dev);
992c8befdd5SWarner Losh 
993c0270e60SPyun YongHyeon 	/* Prefer memory space register mapping over IO space. */
994c0270e60SPyun YongHyeon 	sc->ste_res_id = PCIR_BAR(1);
995c0270e60SPyun YongHyeon 	sc->ste_res_type = SYS_RES_MEMORY;
996c0270e60SPyun YongHyeon 	sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
997c0270e60SPyun YongHyeon 	    &sc->ste_res_id, RF_ACTIVE);
998c0270e60SPyun YongHyeon 	if (sc->ste_res == NULL) {
999c0270e60SPyun YongHyeon 		sc->ste_res_id = PCIR_BAR(0);
1000c0270e60SPyun YongHyeon 		sc->ste_res_type = SYS_RES_IOPORT;
1001c0270e60SPyun YongHyeon 		sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
1002c0270e60SPyun YongHyeon 		    &sc->ste_res_id, RF_ACTIVE);
1003c0270e60SPyun YongHyeon 	}
1004c8befdd5SWarner Losh 	if (sc->ste_res == NULL) {
1005c8befdd5SWarner Losh 		device_printf(dev, "couldn't map ports/memory\n");
1006c8befdd5SWarner Losh 		error = ENXIO;
1007c8befdd5SWarner Losh 		goto fail;
1008c8befdd5SWarner Losh 	}
1009c8befdd5SWarner Losh 
1010c8befdd5SWarner Losh 	/* Allocate interrupt */
1011c8befdd5SWarner Losh 	rid = 0;
1012c8befdd5SWarner Losh 	sc->ste_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1013c8befdd5SWarner Losh 	    RF_SHAREABLE | RF_ACTIVE);
1014c8befdd5SWarner Losh 
1015c8befdd5SWarner Losh 	if (sc->ste_irq == NULL) {
1016c8befdd5SWarner Losh 		device_printf(dev, "couldn't map interrupt\n");
1017c8befdd5SWarner Losh 		error = ENXIO;
1018c8befdd5SWarner Losh 		goto fail;
1019c8befdd5SWarner Losh 	}
1020c8befdd5SWarner Losh 
102110f695eeSPyun YongHyeon 	callout_init_mtx(&sc->ste_callout, &sc->ste_mtx, 0);
1022c8befdd5SWarner Losh 
1023c8befdd5SWarner Losh 	/* Reset the adapter. */
1024c8befdd5SWarner Losh 	ste_reset(sc);
1025c8befdd5SWarner Losh 
1026c8befdd5SWarner Losh 	/*
1027c8befdd5SWarner Losh 	 * Get station address from the EEPROM.
1028c8befdd5SWarner Losh 	 */
1029c8befdd5SWarner Losh 	if (ste_read_eeprom(sc, eaddr,
1030c8befdd5SWarner Losh 	    STE_EEADDR_NODE0, 3, 0)) {
1031c8befdd5SWarner Losh 		device_printf(dev, "failed to read station address\n");
1032c8befdd5SWarner Losh 		error = ENXIO;;
1033c8befdd5SWarner Losh 		goto fail;
1034c8befdd5SWarner Losh 	}
1035c8befdd5SWarner Losh 
1036a1b2c209SPyun YongHyeon 	if ((error = ste_dma_alloc(sc)) != 0)
1037c8befdd5SWarner Losh 		goto fail;
1038c8befdd5SWarner Losh 
1039c8befdd5SWarner Losh 	ifp = sc->ste_ifp = if_alloc(IFT_ETHER);
1040c8befdd5SWarner Losh 	if (ifp == NULL) {
1041c8befdd5SWarner Losh 		device_printf(dev, "can not if_alloc()\n");
1042c8befdd5SWarner Losh 		error = ENOSPC;
1043c8befdd5SWarner Losh 		goto fail;
1044c8befdd5SWarner Losh 	}
1045c8befdd5SWarner Losh 
1046c8befdd5SWarner Losh 	/* Do MII setup. */
1047c8befdd5SWarner Losh 	if (mii_phy_probe(dev, &sc->ste_miibus,
1048c8befdd5SWarner Losh 	    ste_ifmedia_upd, ste_ifmedia_sts)) {
1049c8befdd5SWarner Losh 		device_printf(dev, "MII without any phy!\n");
1050c8befdd5SWarner Losh 		error = ENXIO;
1051c8befdd5SWarner Losh 		goto fail;
1052c8befdd5SWarner Losh 	}
1053c8befdd5SWarner Losh 
1054c8befdd5SWarner Losh 	ifp->if_softc = sc;
1055c8befdd5SWarner Losh 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1056c8befdd5SWarner Losh 	ifp->if_mtu = ETHERMTU;
1057c8befdd5SWarner Losh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1058c8befdd5SWarner Losh 	ifp->if_ioctl = ste_ioctl;
1059c8befdd5SWarner Losh 	ifp->if_start = ste_start;
1060c8befdd5SWarner Losh 	ifp->if_init = ste_init;
1061c8befdd5SWarner Losh 	IFQ_SET_MAXLEN(&ifp->if_snd, STE_TX_LIST_CNT - 1);
1062c8befdd5SWarner Losh 	ifp->if_snd.ifq_drv_maxlen = STE_TX_LIST_CNT - 1;
1063c8befdd5SWarner Losh 	IFQ_SET_READY(&ifp->if_snd);
1064c8befdd5SWarner Losh 
1065c8befdd5SWarner Losh 	sc->ste_tx_thresh = STE_TXSTART_THRESH;
1066c8befdd5SWarner Losh 
1067c8befdd5SWarner Losh 	/*
1068c8befdd5SWarner Losh 	 * Call MI attach routine.
1069c8befdd5SWarner Losh 	 */
1070c8befdd5SWarner Losh 	ether_ifattach(ifp, eaddr);
1071c8befdd5SWarner Losh 
1072c8befdd5SWarner Losh 	/*
1073c8befdd5SWarner Losh 	 * Tell the upper layer(s) we support long frames.
1074c8befdd5SWarner Losh 	 */
1075c8befdd5SWarner Losh 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1076c8befdd5SWarner Losh 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
1077c8befdd5SWarner Losh 	ifp->if_capenable = ifp->if_capabilities;
1078c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1079c8befdd5SWarner Losh 	ifp->if_capabilities |= IFCAP_POLLING;
1080c8befdd5SWarner Losh #endif
1081c8befdd5SWarner Losh 
1082c8befdd5SWarner Losh 	/* Hook interrupt last to avoid having to lock softc */
1083c8befdd5SWarner Losh 	error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET | INTR_MPSAFE,
1084c8befdd5SWarner Losh 	    NULL, ste_intr, sc, &sc->ste_intrhand);
1085c8befdd5SWarner Losh 
1086c8befdd5SWarner Losh 	if (error) {
1087c8befdd5SWarner Losh 		device_printf(dev, "couldn't set up irq\n");
1088c8befdd5SWarner Losh 		ether_ifdetach(ifp);
1089c8befdd5SWarner Losh 		goto fail;
1090c8befdd5SWarner Losh 	}
1091c8befdd5SWarner Losh 
1092c8befdd5SWarner Losh fail:
1093c8befdd5SWarner Losh 	if (error)
1094c8befdd5SWarner Losh 		ste_detach(dev);
1095c8befdd5SWarner Losh 
1096c8befdd5SWarner Losh 	return (error);
1097c8befdd5SWarner Losh }
1098c8befdd5SWarner Losh 
1099c8befdd5SWarner Losh /*
1100c8befdd5SWarner Losh  * Shutdown hardware and free up resources. This can be called any
1101c8befdd5SWarner Losh  * time after the mutex has been initialized. It is called in both
1102c8befdd5SWarner Losh  * the error case in attach and the normal detach case so it needs
1103c8befdd5SWarner Losh  * to be careful about only freeing resources that have actually been
1104c8befdd5SWarner Losh  * allocated.
1105c8befdd5SWarner Losh  */
1106c8befdd5SWarner Losh static int
110760270842SPyun YongHyeon ste_detach(device_t dev)
1108c8befdd5SWarner Losh {
1109c8befdd5SWarner Losh 	struct ste_softc *sc;
1110c8befdd5SWarner Losh 	struct ifnet *ifp;
1111c8befdd5SWarner Losh 
1112c8befdd5SWarner Losh 	sc = device_get_softc(dev);
1113c8befdd5SWarner Losh 	KASSERT(mtx_initialized(&sc->ste_mtx), ("ste mutex not initialized"));
1114c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1115c8befdd5SWarner Losh 
1116c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1117c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING)
1118c8befdd5SWarner Losh 		ether_poll_deregister(ifp);
1119c8befdd5SWarner Losh #endif
1120c8befdd5SWarner Losh 
1121c8befdd5SWarner Losh 	/* These should only be active if attach succeeded */
1122c8befdd5SWarner Losh 	if (device_is_attached(dev)) {
11237cf545d0SJohn Baldwin 		ether_ifdetach(ifp);
1124c8befdd5SWarner Losh 		STE_LOCK(sc);
1125c8befdd5SWarner Losh 		ste_stop(sc);
1126c8befdd5SWarner Losh 		STE_UNLOCK(sc);
112710f695eeSPyun YongHyeon 		callout_drain(&sc->ste_callout);
1128c8befdd5SWarner Losh 	}
1129c8befdd5SWarner Losh 	if (sc->ste_miibus)
1130c8befdd5SWarner Losh 		device_delete_child(dev, sc->ste_miibus);
1131c8befdd5SWarner Losh 	bus_generic_detach(dev);
1132c8befdd5SWarner Losh 
1133c8befdd5SWarner Losh 	if (sc->ste_intrhand)
1134c8befdd5SWarner Losh 		bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1135c8befdd5SWarner Losh 	if (sc->ste_irq)
1136c8befdd5SWarner Losh 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1137c8befdd5SWarner Losh 	if (sc->ste_res)
1138c0270e60SPyun YongHyeon 		bus_release_resource(dev, sc->ste_res_type, sc->ste_res_id,
1139c0270e60SPyun YongHyeon 		    sc->ste_res);
1140c8befdd5SWarner Losh 
1141c8befdd5SWarner Losh 	if (ifp)
1142c8befdd5SWarner Losh 		if_free(ifp);
1143c8befdd5SWarner Losh 
1144a1b2c209SPyun YongHyeon 	ste_dma_free(sc);
1145c8befdd5SWarner Losh 	mtx_destroy(&sc->ste_mtx);
1146c8befdd5SWarner Losh 
1147c8befdd5SWarner Losh 	return (0);
1148c8befdd5SWarner Losh }
1149c8befdd5SWarner Losh 
1150a1b2c209SPyun YongHyeon struct ste_dmamap_arg {
1151a1b2c209SPyun YongHyeon 	bus_addr_t	ste_busaddr;
1152a1b2c209SPyun YongHyeon };
1153a1b2c209SPyun YongHyeon 
1154a1b2c209SPyun YongHyeon static void
1155a1b2c209SPyun YongHyeon ste_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1156c8befdd5SWarner Losh {
1157a1b2c209SPyun YongHyeon 	struct ste_dmamap_arg *ctx;
1158c8befdd5SWarner Losh 
1159a1b2c209SPyun YongHyeon 	if (error != 0)
1160a1b2c209SPyun YongHyeon 		return;
1161a1b2c209SPyun YongHyeon 
1162a1b2c209SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1163a1b2c209SPyun YongHyeon 
1164a1b2c209SPyun YongHyeon 	ctx = (struct ste_dmamap_arg *)arg;
1165a1b2c209SPyun YongHyeon 	ctx->ste_busaddr = segs[0].ds_addr;
1166c8befdd5SWarner Losh }
1167c8befdd5SWarner Losh 
1168a1b2c209SPyun YongHyeon static int
1169a1b2c209SPyun YongHyeon ste_dma_alloc(struct ste_softc *sc)
1170a1b2c209SPyun YongHyeon {
1171a1b2c209SPyun YongHyeon 	struct ste_chain *txc;
1172a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *rxc;
1173a1b2c209SPyun YongHyeon 	struct ste_dmamap_arg ctx;
1174a1b2c209SPyun YongHyeon 	int error, i;
1175c8befdd5SWarner Losh 
1176a1b2c209SPyun YongHyeon 	/* Create parent DMA tag. */
1177a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1178a1b2c209SPyun YongHyeon 	    bus_get_dma_tag(sc->ste_dev), /* parent */
1179a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1180a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1181a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1182a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1183a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1184a1b2c209SPyun YongHyeon 	    0,				/* nsegments */
1185a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1186a1b2c209SPyun YongHyeon 	    0,				/* flags */
1187a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1188a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_parent_tag);
1189a1b2c209SPyun YongHyeon 	if (error != 0) {
1190a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1191a1b2c209SPyun YongHyeon 		    "could not create parent DMA tag.\n");
1192a1b2c209SPyun YongHyeon 		goto fail;
1193a1b2c209SPyun YongHyeon 	}
1194c8befdd5SWarner Losh 
1195a1b2c209SPyun YongHyeon 	/* Create DMA tag for Tx descriptor list. */
1196a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1197a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1198a1b2c209SPyun YongHyeon 	    STE_DESC_ALIGN, 0,		/* alignment, boundary */
1199a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1200a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1201a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1202a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ,		/* maxsize */
1203a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1204a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ,		/* maxsegsize */
1205a1b2c209SPyun YongHyeon 	    0,				/* flags */
1206a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1207a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_list_tag);
1208a1b2c209SPyun YongHyeon 	if (error != 0) {
1209a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1210a1b2c209SPyun YongHyeon 		    "could not create Tx list DMA tag.\n");
1211a1b2c209SPyun YongHyeon 		goto fail;
1212a1b2c209SPyun YongHyeon 	}
1213a1b2c209SPyun YongHyeon 
1214a1b2c209SPyun YongHyeon 	/* Create DMA tag for Rx descriptor list. */
1215a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1216a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1217a1b2c209SPyun YongHyeon 	    STE_DESC_ALIGN, 0,		/* alignment, boundary */
1218a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1219a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1220a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1221a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ,		/* maxsize */
1222a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1223a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ,		/* maxsegsize */
1224a1b2c209SPyun YongHyeon 	    0,				/* flags */
1225a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1226a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_list_tag);
1227a1b2c209SPyun YongHyeon 	if (error != 0) {
1228a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1229a1b2c209SPyun YongHyeon 		    "could not create Rx list DMA tag.\n");
1230a1b2c209SPyun YongHyeon 		goto fail;
1231a1b2c209SPyun YongHyeon 	}
1232a1b2c209SPyun YongHyeon 
1233a1b2c209SPyun YongHyeon 	/* Create DMA tag for Tx buffers. */
1234a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1235a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1236a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1237a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1238a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1239a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1240a1b2c209SPyun YongHyeon 	    MCLBYTES * STE_MAXFRAGS,	/* maxsize */
1241a1b2c209SPyun YongHyeon 	    STE_MAXFRAGS,		/* nsegments */
1242a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1243a1b2c209SPyun YongHyeon 	    0,				/* flags */
1244a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1245a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_tag);
1246a1b2c209SPyun YongHyeon 	if (error != 0) {
1247a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev, "could not create Tx DMA tag.\n");
1248a1b2c209SPyun YongHyeon 		goto fail;
1249a1b2c209SPyun YongHyeon 	}
1250a1b2c209SPyun YongHyeon 
1251a1b2c209SPyun YongHyeon 	/* Create DMA tag for Rx buffers. */
1252a1b2c209SPyun YongHyeon 	error = bus_dma_tag_create(
1253a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_parent_tag, /* parent */
1254a1b2c209SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1255a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1256a1b2c209SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1257a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1258a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsize */
1259a1b2c209SPyun YongHyeon 	    1,				/* nsegments */
1260a1b2c209SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1261a1b2c209SPyun YongHyeon 	    0,				/* flags */
1262a1b2c209SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1263a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_tag);
1264a1b2c209SPyun YongHyeon 	if (error != 0) {
1265a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev, "could not create Rx DMA tag.\n");
1266a1b2c209SPyun YongHyeon 		goto fail;
1267a1b2c209SPyun YongHyeon 	}
1268a1b2c209SPyun YongHyeon 
1269a1b2c209SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx list. */
1270a1b2c209SPyun YongHyeon 	error = bus_dmamem_alloc(sc->ste_cdata.ste_tx_list_tag,
1271a1b2c209SPyun YongHyeon 	    (void **)&sc->ste_ldata.ste_tx_list,
1272a1b2c209SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1273a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_tx_list_map);
1274a1b2c209SPyun YongHyeon 	if (error != 0) {
1275a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1276a1b2c209SPyun YongHyeon 		    "could not allocate DMA'able memory for Tx list.\n");
1277a1b2c209SPyun YongHyeon 		goto fail;
1278a1b2c209SPyun YongHyeon 	}
1279a1b2c209SPyun YongHyeon 	ctx.ste_busaddr = 0;
1280a1b2c209SPyun YongHyeon 	error = bus_dmamap_load(sc->ste_cdata.ste_tx_list_tag,
1281a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map, sc->ste_ldata.ste_tx_list,
1282a1b2c209SPyun YongHyeon 	    STE_TX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1283a1b2c209SPyun YongHyeon 	if (error != 0 || ctx.ste_busaddr == 0) {
1284a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1285a1b2c209SPyun YongHyeon 		    "could not load DMA'able memory for Tx list.\n");
1286a1b2c209SPyun YongHyeon 		goto fail;
1287a1b2c209SPyun YongHyeon 	}
1288a1b2c209SPyun YongHyeon 	sc->ste_ldata.ste_tx_list_paddr = ctx.ste_busaddr;
1289a1b2c209SPyun YongHyeon 
1290a1b2c209SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx list. */
1291a1b2c209SPyun YongHyeon 	error = bus_dmamem_alloc(sc->ste_cdata.ste_rx_list_tag,
1292a1b2c209SPyun YongHyeon 	    (void **)&sc->ste_ldata.ste_rx_list,
1293a1b2c209SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1294a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_list_map);
1295a1b2c209SPyun YongHyeon 	if (error != 0) {
1296a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1297a1b2c209SPyun YongHyeon 		    "could not allocate DMA'able memory for Rx list.\n");
1298a1b2c209SPyun YongHyeon 		goto fail;
1299a1b2c209SPyun YongHyeon 	}
1300a1b2c209SPyun YongHyeon 	ctx.ste_busaddr = 0;
1301a1b2c209SPyun YongHyeon 	error = bus_dmamap_load(sc->ste_cdata.ste_rx_list_tag,
1302a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map, sc->ste_ldata.ste_rx_list,
1303a1b2c209SPyun YongHyeon 	    STE_RX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1304a1b2c209SPyun YongHyeon 	if (error != 0 || ctx.ste_busaddr == 0) {
1305a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1306a1b2c209SPyun YongHyeon 		    "could not load DMA'able memory for Rx list.\n");
1307a1b2c209SPyun YongHyeon 		goto fail;
1308a1b2c209SPyun YongHyeon 	}
1309a1b2c209SPyun YongHyeon 	sc->ste_ldata.ste_rx_list_paddr = ctx.ste_busaddr;
1310a1b2c209SPyun YongHyeon 
1311a1b2c209SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
1312a1b2c209SPyun YongHyeon 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1313a1b2c209SPyun YongHyeon 		txc = &sc->ste_cdata.ste_tx_chain[i];
1314a1b2c209SPyun YongHyeon 		txc->ste_ptr = NULL;
1315a1b2c209SPyun YongHyeon 		txc->ste_mbuf = NULL;
1316a1b2c209SPyun YongHyeon 		txc->ste_next = NULL;
1317a1b2c209SPyun YongHyeon 		txc->ste_phys = 0;
1318a1b2c209SPyun YongHyeon 		txc->ste_map = NULL;
1319a1b2c209SPyun YongHyeon 		error = bus_dmamap_create(sc->ste_cdata.ste_tx_tag, 0,
1320a1b2c209SPyun YongHyeon 		    &txc->ste_map);
1321a1b2c209SPyun YongHyeon 		if (error != 0) {
1322a1b2c209SPyun YongHyeon 			device_printf(sc->ste_dev,
1323a1b2c209SPyun YongHyeon 			    "could not create Tx dmamap.\n");
1324a1b2c209SPyun YongHyeon 			goto fail;
1325a1b2c209SPyun YongHyeon 		}
1326a1b2c209SPyun YongHyeon 	}
1327a1b2c209SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
1328a1b2c209SPyun YongHyeon 	if ((error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1329a1b2c209SPyun YongHyeon 	    &sc->ste_cdata.ste_rx_sparemap)) != 0) {
1330a1b2c209SPyun YongHyeon 		device_printf(sc->ste_dev,
1331a1b2c209SPyun YongHyeon 		    "could not create spare Rx dmamap.\n");
1332a1b2c209SPyun YongHyeon 		goto fail;
1333a1b2c209SPyun YongHyeon 	}
1334a1b2c209SPyun YongHyeon 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1335a1b2c209SPyun YongHyeon 		rxc = &sc->ste_cdata.ste_rx_chain[i];
1336a1b2c209SPyun YongHyeon 		rxc->ste_ptr = NULL;
1337a1b2c209SPyun YongHyeon 		rxc->ste_mbuf = NULL;
1338a1b2c209SPyun YongHyeon 		rxc->ste_next = NULL;
1339a1b2c209SPyun YongHyeon 		rxc->ste_map = NULL;
1340a1b2c209SPyun YongHyeon 		error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1341a1b2c209SPyun YongHyeon 		    &rxc->ste_map);
1342a1b2c209SPyun YongHyeon 		if (error != 0) {
1343a1b2c209SPyun YongHyeon 			device_printf(sc->ste_dev,
1344a1b2c209SPyun YongHyeon 			    "could not create Rx dmamap.\n");
1345a1b2c209SPyun YongHyeon 			goto fail;
1346a1b2c209SPyun YongHyeon 		}
1347a1b2c209SPyun YongHyeon 	}
1348a1b2c209SPyun YongHyeon 
1349a1b2c209SPyun YongHyeon fail:
1350a1b2c209SPyun YongHyeon 	return (error);
1351a1b2c209SPyun YongHyeon }
1352a1b2c209SPyun YongHyeon 
1353a1b2c209SPyun YongHyeon static void
1354a1b2c209SPyun YongHyeon ste_dma_free(struct ste_softc *sc)
1355a1b2c209SPyun YongHyeon {
1356a1b2c209SPyun YongHyeon 	struct ste_chain *txc;
1357a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *rxc;
1358a1b2c209SPyun YongHyeon 	int i;
1359a1b2c209SPyun YongHyeon 
1360a1b2c209SPyun YongHyeon 	/* Tx buffers. */
1361a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_tag != NULL) {
1362a1b2c209SPyun YongHyeon 		for (i = 0; i < STE_TX_LIST_CNT; i++) {
1363a1b2c209SPyun YongHyeon 			txc = &sc->ste_cdata.ste_tx_chain[i];
1364a1b2c209SPyun YongHyeon 			if (txc->ste_map != NULL) {
1365a1b2c209SPyun YongHyeon 				bus_dmamap_destroy(sc->ste_cdata.ste_tx_tag,
1366a1b2c209SPyun YongHyeon 				    txc->ste_map);
1367a1b2c209SPyun YongHyeon 				txc->ste_map = NULL;
1368a1b2c209SPyun YongHyeon 			}
1369a1b2c209SPyun YongHyeon 		}
1370a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_tx_tag);
1371a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_tag = NULL;
1372a1b2c209SPyun YongHyeon 	}
1373a1b2c209SPyun YongHyeon 	/* Rx buffers. */
1374a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_rx_tag != NULL) {
1375a1b2c209SPyun YongHyeon 		for (i = 0; i < STE_RX_LIST_CNT; i++) {
1376a1b2c209SPyun YongHyeon 			rxc = &sc->ste_cdata.ste_rx_chain[i];
1377a1b2c209SPyun YongHyeon 			if (rxc->ste_map != NULL) {
1378a1b2c209SPyun YongHyeon 				bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1379a1b2c209SPyun YongHyeon 				    rxc->ste_map);
1380a1b2c209SPyun YongHyeon 				rxc->ste_map = NULL;
1381a1b2c209SPyun YongHyeon 			}
1382a1b2c209SPyun YongHyeon 		}
1383a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_sparemap != NULL) {
1384a1b2c209SPyun YongHyeon 			bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1385a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_sparemap);
1386a1b2c209SPyun YongHyeon 			sc->ste_cdata.ste_rx_sparemap = NULL;
1387a1b2c209SPyun YongHyeon 		}
1388a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_rx_tag);
1389a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_tag = NULL;
1390a1b2c209SPyun YongHyeon 	}
1391a1b2c209SPyun YongHyeon 	/* Tx descriptor list. */
1392a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_tx_list_tag != NULL) {
1393a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_list_map != NULL)
1394a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_tx_list_tag,
1395a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map);
1396a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_list_map != NULL &&
1397a1b2c209SPyun YongHyeon 		    sc->ste_ldata.ste_tx_list != NULL)
1398a1b2c209SPyun YongHyeon 			bus_dmamem_free(sc->ste_cdata.ste_tx_list_tag,
1399a1b2c209SPyun YongHyeon 			    sc->ste_ldata.ste_tx_list,
1400a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map);
1401a1b2c209SPyun YongHyeon 		sc->ste_ldata.ste_tx_list = NULL;
1402a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_list_map = NULL;
1403a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_tx_list_tag);
1404a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_tx_list_tag = NULL;
1405a1b2c209SPyun YongHyeon 	}
1406a1b2c209SPyun YongHyeon 	/* Rx descriptor list. */
1407a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_rx_list_tag != NULL) {
1408a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_list_map != NULL)
1409a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_rx_list_tag,
1410a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_list_map);
1411a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_rx_list_map != NULL &&
1412a1b2c209SPyun YongHyeon 		    sc->ste_ldata.ste_rx_list != NULL)
1413a1b2c209SPyun YongHyeon 			bus_dmamem_free(sc->ste_cdata.ste_rx_list_tag,
1414a1b2c209SPyun YongHyeon 			    sc->ste_ldata.ste_rx_list,
1415a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_rx_list_map);
1416a1b2c209SPyun YongHyeon 		sc->ste_ldata.ste_rx_list = NULL;
1417a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_list_map = NULL;
1418a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_rx_list_tag);
1419a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_rx_list_tag = NULL;
1420a1b2c209SPyun YongHyeon 	}
1421a1b2c209SPyun YongHyeon 	if (sc->ste_cdata.ste_parent_tag != NULL) {
1422a1b2c209SPyun YongHyeon 		bus_dma_tag_destroy(sc->ste_cdata.ste_parent_tag);
1423a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_parent_tag = NULL;
1424a1b2c209SPyun YongHyeon 	}
1425a1b2c209SPyun YongHyeon }
1426a1b2c209SPyun YongHyeon 
1427a1b2c209SPyun YongHyeon static int
1428a1b2c209SPyun YongHyeon ste_newbuf(struct ste_softc *sc, struct ste_chain_onefrag *rxc)
1429a1b2c209SPyun YongHyeon {
1430a1b2c209SPyun YongHyeon 	struct mbuf *m;
1431a1b2c209SPyun YongHyeon 	bus_dma_segment_t segs[1];
1432a1b2c209SPyun YongHyeon 	bus_dmamap_t map;
1433a1b2c209SPyun YongHyeon 	int error, nsegs;
1434a1b2c209SPyun YongHyeon 
1435a1b2c209SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1436a1b2c209SPyun YongHyeon 	if (m == NULL)
1437a1b2c209SPyun YongHyeon 		return (ENOBUFS);
1438a1b2c209SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1439a1b2c209SPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
1440a1b2c209SPyun YongHyeon 
1441a1b2c209SPyun YongHyeon 	if ((error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_rx_tag,
1442a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_sparemap, m, segs, &nsegs, 0)) != 0) {
1443a1b2c209SPyun YongHyeon 		m_freem(m);
1444a1b2c209SPyun YongHyeon 		return (error);
1445a1b2c209SPyun YongHyeon 	}
1446a1b2c209SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1447a1b2c209SPyun YongHyeon 
1448a1b2c209SPyun YongHyeon 	if (rxc->ste_mbuf != NULL) {
1449a1b2c209SPyun YongHyeon 		bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1450a1b2c209SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1451a1b2c209SPyun YongHyeon 		bus_dmamap_unload(sc->ste_cdata.ste_rx_tag, rxc->ste_map);
1452a1b2c209SPyun YongHyeon 	}
1453a1b2c209SPyun YongHyeon 	map = rxc->ste_map;
1454a1b2c209SPyun YongHyeon 	rxc->ste_map = sc->ste_cdata.ste_rx_sparemap;
1455a1b2c209SPyun YongHyeon 	sc->ste_cdata.ste_rx_sparemap = map;
1456a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1457a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1458a1b2c209SPyun YongHyeon 	rxc->ste_mbuf = m;
1459a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_status = 0;
1460a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_frag.ste_addr = htole32(segs[0].ds_addr);
1461a1b2c209SPyun YongHyeon 	rxc->ste_ptr->ste_frag.ste_len = htole32(segs[0].ds_len |
1462a1b2c209SPyun YongHyeon 	    STE_FRAG_LAST);
1463c8befdd5SWarner Losh 	return (0);
1464c8befdd5SWarner Losh }
1465c8befdd5SWarner Losh 
1466c8befdd5SWarner Losh static int
146760270842SPyun YongHyeon ste_init_rx_list(struct ste_softc *sc)
1468c8befdd5SWarner Losh {
1469c8befdd5SWarner Losh 	struct ste_chain_data *cd;
1470c8befdd5SWarner Losh 	struct ste_list_data *ld;
1471a1b2c209SPyun YongHyeon 	int error, i;
1472c8befdd5SWarner Losh 
1473c8befdd5SWarner Losh 	cd = &sc->ste_cdata;
1474a1b2c209SPyun YongHyeon 	ld = &sc->ste_ldata;
1475a1b2c209SPyun YongHyeon 	bzero(ld->ste_rx_list, STE_RX_LIST_SZ);
1476c8befdd5SWarner Losh 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1477c8befdd5SWarner Losh 		cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i];
1478a1b2c209SPyun YongHyeon 		error = ste_newbuf(sc, &cd->ste_rx_chain[i]);
1479a1b2c209SPyun YongHyeon 		if (error != 0)
1480a1b2c209SPyun YongHyeon 			return (error);
1481c8befdd5SWarner Losh 		if (i == (STE_RX_LIST_CNT - 1)) {
1482a1b2c209SPyun YongHyeon 			cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[0];
1483a1b2c209SPyun YongHyeon 			ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr +
1484a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc_onefrag) * 0);
1485c8befdd5SWarner Losh 		} else {
1486a1b2c209SPyun YongHyeon 			cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[i + 1];
1487a1b2c209SPyun YongHyeon 			ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr +
1488a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc_onefrag) * (i + 1));
1489c8befdd5SWarner Losh 		}
1490c8befdd5SWarner Losh 	}
1491c8befdd5SWarner Losh 
1492c8befdd5SWarner Losh 	cd->ste_rx_head = &cd->ste_rx_chain[0];
1493a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
1494a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_rx_list_map,
1495a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1496c8befdd5SWarner Losh 
1497c8befdd5SWarner Losh 	return (0);
1498c8befdd5SWarner Losh }
1499c8befdd5SWarner Losh 
1500c8befdd5SWarner Losh static void
150160270842SPyun YongHyeon ste_init_tx_list(struct ste_softc *sc)
1502c8befdd5SWarner Losh {
1503c8befdd5SWarner Losh 	struct ste_chain_data *cd;
1504c8befdd5SWarner Losh 	struct ste_list_data *ld;
1505c8befdd5SWarner Losh 	int i;
1506c8befdd5SWarner Losh 
1507c8befdd5SWarner Losh 	cd = &sc->ste_cdata;
1508a1b2c209SPyun YongHyeon 	ld = &sc->ste_ldata;
1509a1b2c209SPyun YongHyeon 	bzero(ld->ste_tx_list, STE_TX_LIST_SZ);
1510c8befdd5SWarner Losh 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1511c8befdd5SWarner Losh 		cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i];
1512a1b2c209SPyun YongHyeon 		cd->ste_tx_chain[i].ste_mbuf = NULL;
1513a1b2c209SPyun YongHyeon 		if (i == (STE_TX_LIST_CNT - 1)) {
1514a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[0];
1515a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1516a1b2c209SPyun YongHyeon 			    ld->ste_tx_list_paddr +
1517a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc) * 0)));
1518a1b2c209SPyun YongHyeon 		} else {
1519a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[i + 1];
1520a1b2c209SPyun YongHyeon 			cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1521a1b2c209SPyun YongHyeon 			    ld->ste_tx_list_paddr +
1522a1b2c209SPyun YongHyeon 			    (sizeof(struct ste_desc) * (i + 1))));
1523a1b2c209SPyun YongHyeon 		}
1524c8befdd5SWarner Losh 	}
1525c8befdd5SWarner Losh 
1526a1b2c209SPyun YongHyeon 	cd->ste_last_tx = NULL;
1527c8befdd5SWarner Losh 	cd->ste_tx_prod = 0;
1528c8befdd5SWarner Losh 	cd->ste_tx_cons = 0;
1529a1b2c209SPyun YongHyeon 	cd->ste_tx_cnt = 0;
1530a1b2c209SPyun YongHyeon 
1531a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1532a1b2c209SPyun YongHyeon 	    sc->ste_cdata.ste_tx_list_map,
1533a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1534c8befdd5SWarner Losh }
1535c8befdd5SWarner Losh 
1536c8befdd5SWarner Losh static void
153760270842SPyun YongHyeon ste_init(void *xsc)
1538c8befdd5SWarner Losh {
1539c8befdd5SWarner Losh 	struct ste_softc *sc;
1540c8befdd5SWarner Losh 
1541c8befdd5SWarner Losh 	sc = xsc;
1542c8befdd5SWarner Losh 	STE_LOCK(sc);
1543c8befdd5SWarner Losh 	ste_init_locked(sc);
1544c8befdd5SWarner Losh 	STE_UNLOCK(sc);
1545c8befdd5SWarner Losh }
1546c8befdd5SWarner Losh 
1547c8befdd5SWarner Losh static void
154860270842SPyun YongHyeon ste_init_locked(struct ste_softc *sc)
1549c8befdd5SWarner Losh {
1550c8befdd5SWarner Losh 	struct ifnet *ifp;
1551f2632c3bSPyun YongHyeon 	int i;
1552c8befdd5SWarner Losh 
1553c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1554c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1555c8befdd5SWarner Losh 
155655d7003eSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
155755d7003eSPyun YongHyeon 		return;
155855d7003eSPyun YongHyeon 
1559c8befdd5SWarner Losh 	ste_stop(sc);
15608d9f6dd9SPyun YongHyeon 	/* Reset the chip to a known state. */
15618d9f6dd9SPyun YongHyeon 	ste_reset(sc);
1562c8befdd5SWarner Losh 
1563c8befdd5SWarner Losh 	/* Init our MAC address */
1564c8befdd5SWarner Losh 	for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
1565c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_PAR0 + i,
1566c8befdd5SWarner Losh 		    ((IF_LLADDR(sc->ste_ifp)[i] & 0xff) |
1567c8befdd5SWarner Losh 		     IF_LLADDR(sc->ste_ifp)[i + 1] << 8));
1568c8befdd5SWarner Losh 	}
1569c8befdd5SWarner Losh 
1570c8befdd5SWarner Losh 	/* Init RX list */
1571a1b2c209SPyun YongHyeon 	if (ste_init_rx_list(sc) != 0) {
1572c8befdd5SWarner Losh 		device_printf(sc->ste_dev,
1573c8befdd5SWarner Losh 		    "initialization failed: no memory for RX buffers\n");
1574c8befdd5SWarner Losh 		ste_stop(sc);
1575c8befdd5SWarner Losh 		return;
1576c8befdd5SWarner Losh 	}
1577c8befdd5SWarner Losh 
1578c8befdd5SWarner Losh 	/* Set RX polling interval */
1579c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 64);
1580c8befdd5SWarner Losh 
1581c8befdd5SWarner Losh 	/* Init TX descriptors */
1582c8befdd5SWarner Losh 	ste_init_tx_list(sc);
1583c8befdd5SWarner Losh 
1584c8befdd5SWarner Losh 	/* Set the TX freethresh value */
1585c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8);
1586c8befdd5SWarner Losh 
1587c8befdd5SWarner Losh 	/* Set the TX start threshold for best performance. */
1588c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
1589c8befdd5SWarner Losh 
1590c8befdd5SWarner Losh 	/* Set the TX reclaim threshold. */
1591c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4));
1592c8befdd5SWarner Losh 
1593c8befdd5SWarner Losh 	/* Set up the RX filter. */
1594c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_RX_MODE, STE_RXMODE_UNICAST);
1595c8befdd5SWarner Losh 
1596c8befdd5SWarner Losh 	/* If we want promiscuous mode, set the allframes bit. */
1597c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_PROMISC) {
1598c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1599c8befdd5SWarner Losh 	} else {
1600c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_PROMISC);
1601c8befdd5SWarner Losh 	}
1602c8befdd5SWarner Losh 
1603c8befdd5SWarner Losh 	/* Set capture broadcast bit to accept broadcast frames. */
1604c8befdd5SWarner Losh 	if (ifp->if_flags & IFF_BROADCAST) {
1605c8befdd5SWarner Losh 		STE_SETBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1606c8befdd5SWarner Losh 	} else {
1607c8befdd5SWarner Losh 		STE_CLRBIT1(sc, STE_RX_MODE, STE_RXMODE_BROADCAST);
1608c8befdd5SWarner Losh 	}
1609c8befdd5SWarner Losh 
1610c8befdd5SWarner Losh 	ste_setmulti(sc);
1611c8befdd5SWarner Losh 
1612c8befdd5SWarner Losh 	/* Load the address of the RX list. */
1613c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1614c8befdd5SWarner Losh 	ste_wait(sc);
1615c8befdd5SWarner Losh 	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
1616a1b2c209SPyun YongHyeon 	    STE_ADDR_LO(sc->ste_ldata.ste_rx_list_paddr));
1617c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1618c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1619c8befdd5SWarner Losh 
1620a1b2c209SPyun YongHyeon 	/* Set TX polling interval(defer until we TX first packet). */
1621c8befdd5SWarner Losh 	CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
1622c8befdd5SWarner Losh 
1623c8befdd5SWarner Losh 	/* Load address of the TX list */
1624c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1625c8befdd5SWarner Losh 	ste_wait(sc);
1626c8befdd5SWarner Losh 	CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
1627c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1628c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1629c8befdd5SWarner Losh 	ste_wait(sc);
1630c8befdd5SWarner Losh 
1631c8befdd5SWarner Losh 	/* Enable receiver and transmitter */
1632c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MACCTL0, 0);
1633c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MACCTL1, 0);
1634c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE);
1635c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE);
1636c8befdd5SWarner Losh 
1637c8befdd5SWarner Losh 	/* Enable stats counters. */
1638c8befdd5SWarner Losh 	STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE);
1639c8befdd5SWarner Losh 
1640c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_ISR, 0xFFFF);
1641c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1642c8befdd5SWarner Losh 	/* Disable interrupts if we are polling. */
1643c8befdd5SWarner Losh 	if (ifp->if_capenable & IFCAP_POLLING)
1644c8befdd5SWarner Losh 		CSR_WRITE_2(sc, STE_IMR, 0);
1645c8befdd5SWarner Losh 	else
1646c8befdd5SWarner Losh #endif
1647c8befdd5SWarner Losh 	/* Enable interrupts. */
1648c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1649c8befdd5SWarner Losh 
1650c8befdd5SWarner Losh 	/* Accept VLAN length packets */
1651c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_MAX_FRAMELEN, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN);
1652c8befdd5SWarner Losh 
1653c8befdd5SWarner Losh 	ste_ifmedia_upd_locked(ifp);
1654c8befdd5SWarner Losh 
1655c8befdd5SWarner Losh 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1656c8befdd5SWarner Losh 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1657c8befdd5SWarner Losh 
165810f695eeSPyun YongHyeon 	callout_reset(&sc->ste_callout, hz, ste_tick, sc);
1659c8befdd5SWarner Losh }
1660c8befdd5SWarner Losh 
1661c8befdd5SWarner Losh static void
166260270842SPyun YongHyeon ste_stop(struct ste_softc *sc)
1663c8befdd5SWarner Losh {
1664c8befdd5SWarner Losh 	struct ifnet *ifp;
1665a1b2c209SPyun YongHyeon 	struct ste_chain_onefrag *cur_rx;
1666a1b2c209SPyun YongHyeon 	struct ste_chain *cur_tx;
16678d9f6dd9SPyun YongHyeon 	uint32_t val;
1668f2632c3bSPyun YongHyeon 	int i;
1669c8befdd5SWarner Losh 
1670c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1671c8befdd5SWarner Losh 	ifp = sc->ste_ifp;
1672c8befdd5SWarner Losh 
167310f695eeSPyun YongHyeon 	callout_stop(&sc->ste_callout);
167410f695eeSPyun YongHyeon 	sc->ste_timer = 0;
1675c8befdd5SWarner Losh 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
1676c8befdd5SWarner Losh 
1677c8befdd5SWarner Losh 	CSR_WRITE_2(sc, STE_IMR, 0);
16788d9f6dd9SPyun YongHyeon 	/* Stop pending DMA. */
16798d9f6dd9SPyun YongHyeon 	val = CSR_READ_4(sc, STE_DMACTL);
16808d9f6dd9SPyun YongHyeon 	val |= STE_DMACTL_TXDMA_STALL | STE_DMACTL_RXDMA_STALL;
16818d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_DMACTL, val);
1682c8befdd5SWarner Losh 	ste_wait(sc);
16838d9f6dd9SPyun YongHyeon 	/* Disable auto-polling. */
16848d9f6dd9SPyun YongHyeon 	CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 0);
16858d9f6dd9SPyun YongHyeon 	CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
16868d9f6dd9SPyun YongHyeon 	/* Nullify DMA address to stop any further DMA. */
16878d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 0);
16888d9f6dd9SPyun YongHyeon 	CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
16898d9f6dd9SPyun YongHyeon 	/* Stop TX/RX MAC. */
16908d9f6dd9SPyun YongHyeon 	val = CSR_READ_2(sc, STE_MACCTL1);
16918d9f6dd9SPyun YongHyeon 	val |= STE_MACCTL1_TX_DISABLE | STE_MACCTL1_RX_DISABLE |
16928d9f6dd9SPyun YongHyeon 	    STE_MACCTL1_STATS_DISABLE;
16938d9f6dd9SPyun YongHyeon 	CSR_WRITE_2(sc, STE_MACCTL1, val);
16948d9f6dd9SPyun YongHyeon 	for (i = 0; i < STE_TIMEOUT; i++) {
16958d9f6dd9SPyun YongHyeon 		DELAY(10);
16968d9f6dd9SPyun YongHyeon 		if ((CSR_READ_2(sc, STE_MACCTL1) & (STE_MACCTL1_TX_DISABLE |
16978d9f6dd9SPyun YongHyeon 		    STE_MACCTL1_RX_DISABLE | STE_MACCTL1_STATS_DISABLE)) == 0)
16988d9f6dd9SPyun YongHyeon 			break;
16998d9f6dd9SPyun YongHyeon 	}
17008d9f6dd9SPyun YongHyeon 	if (i == STE_TIMEOUT)
17018d9f6dd9SPyun YongHyeon 		device_printf(sc->ste_dev, "Stopping MAC timed out\n");
17028d9f6dd9SPyun YongHyeon 	/* Acknowledge any pending interrupts. */
17038d9f6dd9SPyun YongHyeon 	CSR_READ_2(sc, STE_ISR_ACK);
17048d9f6dd9SPyun YongHyeon 	ste_stats_update(sc);
1705c8befdd5SWarner Losh 
1706c8befdd5SWarner Losh 	for (i = 0; i < STE_RX_LIST_CNT; i++) {
1707a1b2c209SPyun YongHyeon 		cur_rx = &sc->ste_cdata.ste_rx_chain[i];
1708a1b2c209SPyun YongHyeon 		if (cur_rx->ste_mbuf != NULL) {
1709a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_rx_tag,
1710a1b2c209SPyun YongHyeon 			    cur_rx->ste_map, BUS_DMASYNC_POSTREAD);
1711a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_rx_tag,
1712a1b2c209SPyun YongHyeon 			    cur_rx->ste_map);
1713a1b2c209SPyun YongHyeon 			m_freem(cur_rx->ste_mbuf);
1714a1b2c209SPyun YongHyeon 			cur_rx->ste_mbuf = NULL;
1715c8befdd5SWarner Losh 		}
1716c8befdd5SWarner Losh 	}
1717c8befdd5SWarner Losh 
1718c8befdd5SWarner Losh 	for (i = 0; i < STE_TX_LIST_CNT; i++) {
1719a1b2c209SPyun YongHyeon 		cur_tx = &sc->ste_cdata.ste_tx_chain[i];
1720a1b2c209SPyun YongHyeon 		if (cur_tx->ste_mbuf != NULL) {
1721a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_tag,
1722a1b2c209SPyun YongHyeon 			    cur_tx->ste_map, BUS_DMASYNC_POSTWRITE);
1723a1b2c209SPyun YongHyeon 			bus_dmamap_unload(sc->ste_cdata.ste_tx_tag,
1724a1b2c209SPyun YongHyeon 			    cur_tx->ste_map);
1725a1b2c209SPyun YongHyeon 			m_freem(cur_tx->ste_mbuf);
1726a1b2c209SPyun YongHyeon 			cur_tx->ste_mbuf = NULL;
1727c8befdd5SWarner Losh 		}
1728c8befdd5SWarner Losh 	}
1729c8befdd5SWarner Losh }
1730c8befdd5SWarner Losh 
1731c8befdd5SWarner Losh static void
173260270842SPyun YongHyeon ste_reset(struct ste_softc *sc)
1733c8befdd5SWarner Losh {
1734c8befdd5SWarner Losh 	int i;
1735c8befdd5SWarner Losh 
1736c8befdd5SWarner Losh 	STE_SETBIT4(sc, STE_ASICCTL,
1737c8befdd5SWarner Losh 	    STE_ASICCTL_GLOBAL_RESET|STE_ASICCTL_RX_RESET|
1738c8befdd5SWarner Losh 	    STE_ASICCTL_TX_RESET|STE_ASICCTL_DMA_RESET|
1739c8befdd5SWarner Losh 	    STE_ASICCTL_FIFO_RESET|STE_ASICCTL_NETWORK_RESET|
1740c8befdd5SWarner Losh 	    STE_ASICCTL_AUTOINIT_RESET|STE_ASICCTL_HOST_RESET|
1741c8befdd5SWarner Losh 	    STE_ASICCTL_EXTRESET_RESET);
1742c8befdd5SWarner Losh 
1743c8befdd5SWarner Losh 	DELAY(100000);
1744c8befdd5SWarner Losh 
1745c8befdd5SWarner Losh 	for (i = 0; i < STE_TIMEOUT; i++) {
1746c8befdd5SWarner Losh 		if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY))
1747c8befdd5SWarner Losh 			break;
1748c8befdd5SWarner Losh 	}
1749c8befdd5SWarner Losh 
1750c8befdd5SWarner Losh 	if (i == STE_TIMEOUT)
1751c8befdd5SWarner Losh 		device_printf(sc->ste_dev, "global reset never completed\n");
1752c8befdd5SWarner Losh }
1753c8befdd5SWarner Losh 
175481598b3eSPyun YongHyeon static void
175581598b3eSPyun YongHyeon ste_restart_tx(struct ste_softc *sc)
175681598b3eSPyun YongHyeon {
175781598b3eSPyun YongHyeon 	uint16_t mac;
175881598b3eSPyun YongHyeon 	int i;
175981598b3eSPyun YongHyeon 
176081598b3eSPyun YongHyeon 	for (i = 0; i < STE_TIMEOUT; i++) {
176181598b3eSPyun YongHyeon 		mac = CSR_READ_2(sc, STE_MACCTL1);
176281598b3eSPyun YongHyeon 		mac |= STE_MACCTL1_TX_ENABLE;
176381598b3eSPyun YongHyeon 		CSR_WRITE_2(sc, STE_MACCTL1, mac);
176481598b3eSPyun YongHyeon 		mac = CSR_READ_2(sc, STE_MACCTL1);
176581598b3eSPyun YongHyeon 		if ((mac & STE_MACCTL1_TX_ENABLED) != 0)
176681598b3eSPyun YongHyeon 			break;
176781598b3eSPyun YongHyeon 		DELAY(10);
176881598b3eSPyun YongHyeon 	}
176981598b3eSPyun YongHyeon 
177081598b3eSPyun YongHyeon 	if (i == STE_TIMEOUT)
177181598b3eSPyun YongHyeon 		device_printf(sc->ste_dev, "starting Tx failed");
177281598b3eSPyun YongHyeon }
177381598b3eSPyun YongHyeon 
1774c8befdd5SWarner Losh static int
177560270842SPyun YongHyeon ste_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1776c8befdd5SWarner Losh {
1777c8befdd5SWarner Losh 	struct ste_softc *sc;
1778c8befdd5SWarner Losh 	struct ifreq *ifr;
1779c8befdd5SWarner Losh 	struct mii_data *mii;
1780c8befdd5SWarner Losh 	int error = 0;
1781c8befdd5SWarner Losh 
1782c8befdd5SWarner Losh 	sc = ifp->if_softc;
1783c8befdd5SWarner Losh 	ifr = (struct ifreq *)data;
1784c8befdd5SWarner Losh 
1785c8befdd5SWarner Losh 	switch (command) {
1786c8befdd5SWarner Losh 	case SIOCSIFFLAGS:
1787c8befdd5SWarner Losh 		STE_LOCK(sc);
1788c8befdd5SWarner Losh 		if (ifp->if_flags & IFF_UP) {
1789c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1790c8befdd5SWarner Losh 			    ifp->if_flags & IFF_PROMISC &&
1791c8befdd5SWarner Losh 			    !(sc->ste_if_flags & IFF_PROMISC)) {
1792c8befdd5SWarner Losh 				STE_SETBIT1(sc, STE_RX_MODE,
1793c8befdd5SWarner Losh 				    STE_RXMODE_PROMISC);
1794c8befdd5SWarner Losh 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1795c8befdd5SWarner Losh 			    !(ifp->if_flags & IFF_PROMISC) &&
1796c8befdd5SWarner Losh 			    sc->ste_if_flags & IFF_PROMISC) {
1797c8befdd5SWarner Losh 				STE_CLRBIT1(sc, STE_RX_MODE,
1798c8befdd5SWarner Losh 				    STE_RXMODE_PROMISC);
1799c8befdd5SWarner Losh 			}
1800c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1801c8befdd5SWarner Losh 			    (ifp->if_flags ^ sc->ste_if_flags) & IFF_ALLMULTI)
1802c8befdd5SWarner Losh 				ste_setmulti(sc);
1803c8befdd5SWarner Losh 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1804c8befdd5SWarner Losh 				sc->ste_tx_thresh = STE_TXSTART_THRESH;
1805c8befdd5SWarner Losh 				ste_init_locked(sc);
1806c8befdd5SWarner Losh 			}
1807c8befdd5SWarner Losh 		} else {
1808c8befdd5SWarner Losh 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1809c8befdd5SWarner Losh 				ste_stop(sc);
1810c8befdd5SWarner Losh 		}
1811c8befdd5SWarner Losh 		sc->ste_if_flags = ifp->if_flags;
1812c8befdd5SWarner Losh 		STE_UNLOCK(sc);
1813c8befdd5SWarner Losh 		error = 0;
1814c8befdd5SWarner Losh 		break;
1815c8befdd5SWarner Losh 	case SIOCADDMULTI:
1816c8befdd5SWarner Losh 	case SIOCDELMULTI:
1817c8befdd5SWarner Losh 		STE_LOCK(sc);
1818c8befdd5SWarner Losh 		ste_setmulti(sc);
1819c8befdd5SWarner Losh 		STE_UNLOCK(sc);
1820c8befdd5SWarner Losh 		error = 0;
1821c8befdd5SWarner Losh 		break;
1822c8befdd5SWarner Losh 	case SIOCGIFMEDIA:
1823c8befdd5SWarner Losh 	case SIOCSIFMEDIA:
1824c8befdd5SWarner Losh 		mii = device_get_softc(sc->ste_miibus);
1825c8befdd5SWarner Losh 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1826c8befdd5SWarner Losh 		break;
1827c8befdd5SWarner Losh 	case SIOCSIFCAP:
1828c8befdd5SWarner Losh #ifdef DEVICE_POLLING
1829c8befdd5SWarner Losh 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
1830c8befdd5SWarner Losh 		    !(ifp->if_capenable & IFCAP_POLLING)) {
1831c8befdd5SWarner Losh 			error = ether_poll_register(ste_poll, ifp);
1832c8befdd5SWarner Losh 			if (error)
1833c8befdd5SWarner Losh 				return (error);
1834c8befdd5SWarner Losh 			STE_LOCK(sc);
1835c8befdd5SWarner Losh 			/* Disable interrupts */
1836c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_IMR, 0);
1837c8befdd5SWarner Losh 			ifp->if_capenable |= IFCAP_POLLING;
1838c8befdd5SWarner Losh 			STE_UNLOCK(sc);
1839c8befdd5SWarner Losh 			return (error);
1840c8befdd5SWarner Losh 
1841c8befdd5SWarner Losh 		}
1842c8befdd5SWarner Losh 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
1843c8befdd5SWarner Losh 		    ifp->if_capenable & IFCAP_POLLING) {
1844c8befdd5SWarner Losh 			error = ether_poll_deregister(ifp);
1845c8befdd5SWarner Losh 			/* Enable interrupts. */
1846c8befdd5SWarner Losh 			STE_LOCK(sc);
1847c8befdd5SWarner Losh 			CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1848c8befdd5SWarner Losh 			ifp->if_capenable &= ~IFCAP_POLLING;
1849c8befdd5SWarner Losh 			STE_UNLOCK(sc);
1850c8befdd5SWarner Losh 			return (error);
1851c8befdd5SWarner Losh 		}
1852c8befdd5SWarner Losh #endif /* DEVICE_POLLING */
1853c8befdd5SWarner Losh 		break;
1854c8befdd5SWarner Losh 	default:
1855c8befdd5SWarner Losh 		error = ether_ioctl(ifp, command, data);
1856c8befdd5SWarner Losh 		break;
1857c8befdd5SWarner Losh 	}
1858c8befdd5SWarner Losh 
1859c8befdd5SWarner Losh 	return (error);
1860c8befdd5SWarner Losh }
1861c8befdd5SWarner Losh 
1862c8befdd5SWarner Losh static int
1863a1b2c209SPyun YongHyeon ste_encap(struct ste_softc *sc, struct mbuf **m_head, struct ste_chain *txc)
1864c8befdd5SWarner Losh {
1865a1b2c209SPyun YongHyeon 	struct ste_frag *frag;
1866c8befdd5SWarner Losh 	struct mbuf *m;
1867a1b2c209SPyun YongHyeon 	struct ste_desc *desc;
1868a1b2c209SPyun YongHyeon 	bus_dma_segment_t txsegs[STE_MAXFRAGS];
1869a1b2c209SPyun YongHyeon 	int error, i, nsegs;
1870c8befdd5SWarner Losh 
1871a1b2c209SPyun YongHyeon 	STE_LOCK_ASSERT(sc);
1872a1b2c209SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
1873c8befdd5SWarner Losh 
1874a1b2c209SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1875a1b2c209SPyun YongHyeon 	    txc->ste_map, *m_head, txsegs, &nsegs, 0);
1876a1b2c209SPyun YongHyeon 	if (error == EFBIG) {
1877a1b2c209SPyun YongHyeon 		m = m_collapse(*m_head, M_DONTWAIT, STE_MAXFRAGS);
1878a1b2c209SPyun YongHyeon 		if (m == NULL) {
1879a1b2c209SPyun YongHyeon 			m_freem(*m_head);
1880a1b2c209SPyun YongHyeon 			*m_head = NULL;
1881a1b2c209SPyun YongHyeon 			return (ENOMEM);
1882c8befdd5SWarner Losh 		}
1883a1b2c209SPyun YongHyeon 		*m_head = m;
1884a1b2c209SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1885a1b2c209SPyun YongHyeon 		    txc->ste_map, *m_head, txsegs, &nsegs, 0);
1886a1b2c209SPyun YongHyeon 		if (error != 0) {
1887a1b2c209SPyun YongHyeon 			m_freem(*m_head);
1888a1b2c209SPyun YongHyeon 			*m_head = NULL;
1889a1b2c209SPyun YongHyeon 			return (error);
1890c8befdd5SWarner Losh 		}
1891a1b2c209SPyun YongHyeon 	} else if (error != 0)
1892a1b2c209SPyun YongHyeon 		return (error);
1893a1b2c209SPyun YongHyeon 	if (nsegs == 0) {
1894a1b2c209SPyun YongHyeon 		m_freem(*m_head);
1895a1b2c209SPyun YongHyeon 		*m_head = NULL;
1896a1b2c209SPyun YongHyeon 		return (EIO);
1897a1b2c209SPyun YongHyeon 	}
1898a1b2c209SPyun YongHyeon 	bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, txc->ste_map,
1899a1b2c209SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
1900c8befdd5SWarner Losh 
1901a1b2c209SPyun YongHyeon 	desc = txc->ste_ptr;
1902a1b2c209SPyun YongHyeon 	for (i = 0; i < nsegs; i++) {
1903a1b2c209SPyun YongHyeon 		frag = &desc->ste_frags[i];
1904a1b2c209SPyun YongHyeon 		frag->ste_addr = htole32(STE_ADDR_LO(txsegs[i].ds_addr));
1905a1b2c209SPyun YongHyeon 		frag->ste_len = htole32(txsegs[i].ds_len);
1906a1b2c209SPyun YongHyeon 	}
1907a1b2c209SPyun YongHyeon 	desc->ste_frags[i - 1].ste_len |= htole32(STE_FRAG_LAST);
1908c8befdd5SWarner Losh 	/*
1909a1b2c209SPyun YongHyeon 	 * Because we use Tx polling we can't chain multiple
1910a1b2c209SPyun YongHyeon 	 * Tx descriptors here. Otherwise we race with controller.
1911c8befdd5SWarner Losh 	 */
1912a1b2c209SPyun YongHyeon 	desc->ste_next = 0;
1913a1b2c209SPyun YongHyeon 	desc->ste_ctl = htole32(STE_TXCTL_ALIGN_DIS | STE_TXCTL_DMAINTR);
1914a1b2c209SPyun YongHyeon 	txc->ste_mbuf = *m_head;
1915a1b2c209SPyun YongHyeon 	STE_INC(sc->ste_cdata.ste_tx_prod, STE_TX_LIST_CNT);
1916a1b2c209SPyun YongHyeon 	sc->ste_cdata.ste_tx_cnt++;
1917c8befdd5SWarner Losh 
1918c8befdd5SWarner Losh 	return (0);
1919c8befdd5SWarner Losh }
1920c8befdd5SWarner Losh 
1921c8befdd5SWarner Losh static void
192260270842SPyun YongHyeon ste_start(struct ifnet *ifp)
1923c8befdd5SWarner Losh {
1924c8befdd5SWarner Losh 	struct ste_softc *sc;
1925c8befdd5SWarner Losh 
1926c8befdd5SWarner Losh 	sc = ifp->if_softc;
1927c8befdd5SWarner Losh 	STE_LOCK(sc);
1928c8befdd5SWarner Losh 	ste_start_locked(ifp);
1929c8befdd5SWarner Losh 	STE_UNLOCK(sc);
1930c8befdd5SWarner Losh }
1931c8befdd5SWarner Losh 
1932c8befdd5SWarner Losh static void
193360270842SPyun YongHyeon ste_start_locked(struct ifnet *ifp)
1934c8befdd5SWarner Losh {
1935c8befdd5SWarner Losh 	struct ste_softc *sc;
1936c8befdd5SWarner Losh 	struct ste_chain *cur_tx;
1937f2632c3bSPyun YongHyeon 	struct mbuf *m_head = NULL;
1938a1b2c209SPyun YongHyeon 	int enq;
1939c8befdd5SWarner Losh 
1940c8befdd5SWarner Losh 	sc = ifp->if_softc;
1941c8befdd5SWarner Losh 	STE_LOCK_ASSERT(sc);
1942c8befdd5SWarner Losh 
19434465097bSPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
19444465097bSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->ste_flags & STE_FLAG_LINK) == 0)
1945c8befdd5SWarner Losh 		return;
1946c8befdd5SWarner Losh 
1947a1b2c209SPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
1948a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_tx_cnt == STE_TX_LIST_CNT - 1) {
1949c8befdd5SWarner Losh 			/*
1950a1b2c209SPyun YongHyeon 			 * Controller may have cached copy of the last used
1951a1b2c209SPyun YongHyeon 			 * next ptr so we have to reserve one TFD to avoid
1952a1b2c209SPyun YongHyeon 			 * TFD overruns.
1953c8befdd5SWarner Losh 			 */
1954c8befdd5SWarner Losh 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1955c8befdd5SWarner Losh 			break;
1956c8befdd5SWarner Losh 		}
1957c8befdd5SWarner Losh 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1958c8befdd5SWarner Losh 		if (m_head == NULL)
1959c8befdd5SWarner Losh 			break;
1960a1b2c209SPyun YongHyeon 		cur_tx = &sc->ste_cdata.ste_tx_chain[sc->ste_cdata.ste_tx_prod];
1961a1b2c209SPyun YongHyeon 		if (ste_encap(sc, &m_head, cur_tx) != 0) {
1962a1b2c209SPyun YongHyeon 			if (m_head == NULL)
1963c8befdd5SWarner Losh 				break;
1964a1b2c209SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1965a1b2c209SPyun YongHyeon 			break;
1966a1b2c209SPyun YongHyeon 		}
1967a1b2c209SPyun YongHyeon 		if (sc->ste_cdata.ste_last_tx == NULL) {
1968a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1969a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map,
1970a1b2c209SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1971c8befdd5SWarner Losh 			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1972c8befdd5SWarner Losh 			ste_wait(sc);
1973c8befdd5SWarner Losh 			CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
1974a1b2c209SPyun YongHyeon 	    		    STE_ADDR_LO(sc->ste_ldata.ste_tx_list_paddr));
1975c8befdd5SWarner Losh 			CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64);
1976c8befdd5SWarner Losh 			STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1977c8befdd5SWarner Losh 			ste_wait(sc);
1978c8befdd5SWarner Losh 		} else {
1979a1b2c209SPyun YongHyeon 			sc->ste_cdata.ste_last_tx->ste_ptr->ste_next =
1980a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_last_tx->ste_phys;
1981a1b2c209SPyun YongHyeon 			bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1982a1b2c209SPyun YongHyeon 			    sc->ste_cdata.ste_tx_list_map,
1983a1b2c209SPyun YongHyeon 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1984c8befdd5SWarner Losh 		}
1985a1b2c209SPyun YongHyeon 		sc->ste_cdata.ste_last_tx = cur_tx;
1986c8befdd5SWarner Losh 
1987a1b2c209SPyun YongHyeon 		enq++;
1988c8befdd5SWarner Losh 		/*
1989c8befdd5SWarner Losh 		 * If there's a BPF listener, bounce a copy of this frame
1990c8befdd5SWarner Losh 		 * to him.
1991c8befdd5SWarner Losh 	 	 */
1992a1b2c209SPyun YongHyeon 		BPF_MTAP(ifp, m_head);
1993c8befdd5SWarner Losh 	}
1994a1b2c209SPyun YongHyeon 
1995a1b2c209SPyun YongHyeon 	if (enq > 0)
1996a1b2c209SPyun YongHyeon 		sc->ste_timer = STE_TX_TIMEOUT;
1997c8befdd5SWarner Losh }
1998c8befdd5SWarner Losh 
1999c8befdd5SWarner Losh static void
20007cf545d0SJohn Baldwin ste_watchdog(struct ste_softc *sc)
2001c8befdd5SWarner Losh {
20027cf545d0SJohn Baldwin 	struct ifnet *ifp;
2003c8befdd5SWarner Losh 
20047cf545d0SJohn Baldwin 	ifp = sc->ste_ifp;
20057cf545d0SJohn Baldwin 	STE_LOCK_ASSERT(sc);
2006c8befdd5SWarner Losh 
200710f695eeSPyun YongHyeon 	if (sc->ste_timer == 0 || --sc->ste_timer)
200810f695eeSPyun YongHyeon 		return;
200910f695eeSPyun YongHyeon 
2010c8befdd5SWarner Losh 	ifp->if_oerrors++;
2011c8befdd5SWarner Losh 	if_printf(ifp, "watchdog timeout\n");
2012c8befdd5SWarner Losh 
2013c8befdd5SWarner Losh 	ste_txeof(sc);
201481598b3eSPyun YongHyeon 	ste_txeoc(sc);
2015a1b2c209SPyun YongHyeon 	ste_rxeof(sc, -1);
201655d7003eSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2017c8befdd5SWarner Losh 	ste_init_locked(sc);
2018c8befdd5SWarner Losh 
2019c8befdd5SWarner Losh 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2020c8befdd5SWarner Losh 		ste_start_locked(ifp);
2021c8befdd5SWarner Losh }
2022c8befdd5SWarner Losh 
2023c8befdd5SWarner Losh static int
202460270842SPyun YongHyeon ste_shutdown(device_t dev)
2025c8befdd5SWarner Losh {
2026c8befdd5SWarner Losh 	struct ste_softc *sc;
2027c8befdd5SWarner Losh 
2028c8befdd5SWarner Losh 	sc = device_get_softc(dev);
2029c8befdd5SWarner Losh 
2030c8befdd5SWarner Losh 	STE_LOCK(sc);
2031c8befdd5SWarner Losh 	ste_stop(sc);
2032c8befdd5SWarner Losh 	STE_UNLOCK(sc);
2033c8befdd5SWarner Losh 
2034c8befdd5SWarner Losh 	return (0);
2035c8befdd5SWarner Losh }
2036