xref: /freebsd/sys/dev/alc/if_alc.c (revision ddaf6524682b3ab9e50f7575db319814dbbd053a)
1d68875ebSPyun YongHyeon /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4d68875ebSPyun YongHyeon  * Copyright (c) 2009, Pyun YongHyeon <yongari@FreeBSD.org>
5d68875ebSPyun YongHyeon  * All rights reserved.
6d68875ebSPyun YongHyeon  *
7d68875ebSPyun YongHyeon  * Redistribution and use in source and binary forms, with or without
8d68875ebSPyun YongHyeon  * modification, are permitted provided that the following conditions
9d68875ebSPyun YongHyeon  * are met:
10d68875ebSPyun YongHyeon  * 1. Redistributions of source code must retain the above copyright
11d68875ebSPyun YongHyeon  *    notice unmodified, this list of conditions, and the following
12d68875ebSPyun YongHyeon  *    disclaimer.
13d68875ebSPyun YongHyeon  * 2. Redistributions in binary form must reproduce the above copyright
14d68875ebSPyun YongHyeon  *    notice, this list of conditions and the following disclaimer in the
15d68875ebSPyun YongHyeon  *    documentation and/or other materials provided with the distribution.
16d68875ebSPyun YongHyeon  *
17d68875ebSPyun YongHyeon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18d68875ebSPyun YongHyeon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19d68875ebSPyun YongHyeon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20d68875ebSPyun YongHyeon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21d68875ebSPyun YongHyeon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22d68875ebSPyun YongHyeon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23d68875ebSPyun YongHyeon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24d68875ebSPyun YongHyeon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25d68875ebSPyun YongHyeon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26d68875ebSPyun YongHyeon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27d68875ebSPyun YongHyeon  * SUCH DAMAGE.
28d68875ebSPyun YongHyeon  */
29d68875ebSPyun YongHyeon 
302f70cceaSPyun YongHyeon /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
31d68875ebSPyun YongHyeon 
32d68875ebSPyun YongHyeon #include <sys/param.h>
33d68875ebSPyun YongHyeon #include <sys/systm.h>
34d68875ebSPyun YongHyeon #include <sys/bus.h>
35d68875ebSPyun YongHyeon #include <sys/endian.h>
36d68875ebSPyun YongHyeon #include <sys/kernel.h>
37d68875ebSPyun YongHyeon #include <sys/lock.h>
38d68875ebSPyun YongHyeon #include <sys/malloc.h>
39d68875ebSPyun YongHyeon #include <sys/mbuf.h>
40d68875ebSPyun YongHyeon #include <sys/module.h>
41d68875ebSPyun YongHyeon #include <sys/mutex.h>
42d68875ebSPyun YongHyeon #include <sys/rman.h>
43d68875ebSPyun YongHyeon #include <sys/queue.h>
44d68875ebSPyun YongHyeon #include <sys/socket.h>
45d68875ebSPyun YongHyeon #include <sys/sockio.h>
46d68875ebSPyun YongHyeon #include <sys/sysctl.h>
47d68875ebSPyun YongHyeon #include <sys/taskqueue.h>
48d68875ebSPyun YongHyeon 
49d68875ebSPyun YongHyeon #include <net/bpf.h>
507790c8c1SConrad Meyer #include <net/debugnet.h>
51d68875ebSPyun YongHyeon #include <net/if.h>
5276039bc8SGleb Smirnoff #include <net/if_var.h>
53d68875ebSPyun YongHyeon #include <net/if_arp.h>
54d68875ebSPyun YongHyeon #include <net/ethernet.h>
55d68875ebSPyun YongHyeon #include <net/if_dl.h>
56d68875ebSPyun YongHyeon #include <net/if_llc.h>
57d68875ebSPyun YongHyeon #include <net/if_media.h>
58d68875ebSPyun YongHyeon #include <net/if_types.h>
59d68875ebSPyun YongHyeon #include <net/if_vlan_var.h>
60d68875ebSPyun YongHyeon 
61d68875ebSPyun YongHyeon #include <netinet/in.h>
62d68875ebSPyun YongHyeon #include <netinet/in_systm.h>
63d68875ebSPyun YongHyeon #include <netinet/ip.h>
64d68875ebSPyun YongHyeon #include <netinet/tcp.h>
65d68875ebSPyun YongHyeon 
66d68875ebSPyun YongHyeon #include <dev/mii/mii.h>
67d68875ebSPyun YongHyeon #include <dev/mii/miivar.h>
68d68875ebSPyun YongHyeon 
69d68875ebSPyun YongHyeon #include <dev/pci/pcireg.h>
70d68875ebSPyun YongHyeon #include <dev/pci/pcivar.h>
71d68875ebSPyun YongHyeon 
72d68875ebSPyun YongHyeon #include <machine/bus.h>
73d68875ebSPyun YongHyeon #include <machine/in_cksum.h>
74d68875ebSPyun YongHyeon 
75d68875ebSPyun YongHyeon #include <dev/alc/if_alcreg.h>
76d68875ebSPyun YongHyeon #include <dev/alc/if_alcvar.h>
77d68875ebSPyun YongHyeon 
78d68875ebSPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
79d68875ebSPyun YongHyeon #include "miibus_if.h"
80d68875ebSPyun YongHyeon #undef ALC_USE_CUSTOM_CSUM
81d68875ebSPyun YongHyeon 
82d68875ebSPyun YongHyeon #ifdef ALC_USE_CUSTOM_CSUM
83d68875ebSPyun YongHyeon #define	ALC_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
84d68875ebSPyun YongHyeon #else
85d68875ebSPyun YongHyeon #define	ALC_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
86d68875ebSPyun YongHyeon #endif
87d68875ebSPyun YongHyeon 
88d68875ebSPyun YongHyeon MODULE_DEPEND(alc, pci, 1, 1, 1);
89d68875ebSPyun YongHyeon MODULE_DEPEND(alc, ether, 1, 1, 1);
90d68875ebSPyun YongHyeon MODULE_DEPEND(alc, miibus, 1, 1, 1);
91d68875ebSPyun YongHyeon 
92d68875ebSPyun YongHyeon /* Tunables. */
93d68875ebSPyun YongHyeon static int msi_disable = 0;
94d68875ebSPyun YongHyeon TUNABLE_INT("hw.alc.msi_disable", &msi_disable);
9505a95d19SLexi Winter 
9605a95d19SLexi Winter /*
9705a95d19SLexi Winter  * The default value of msix_disable is 2, which means to decide whether to
9805a95d19SLexi Winter  * enable MSI-X in alc_attach() depending on the card type.  The operator can
9905a95d19SLexi Winter  * set this to 0 or 1 to override the default.
10005a95d19SLexi Winter  */
10105a95d19SLexi Winter static int msix_disable = 2;
102d68875ebSPyun YongHyeon TUNABLE_INT("hw.alc.msix_disable", &msix_disable);
103d68875ebSPyun YongHyeon 
104d68875ebSPyun YongHyeon /*
105d68875ebSPyun YongHyeon  * Devices supported by this driver.
106d68875ebSPyun YongHyeon  */
1072f70cceaSPyun YongHyeon static struct alc_ident alc_ident_table[] = {
1082f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8131, 9 * 1024,
109d68875ebSPyun YongHyeon 		"Atheros AR8131 PCIe Gigabit Ethernet" },
1102f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8132, 9 * 1024,
1112f70cceaSPyun YongHyeon 		"Atheros AR8132 PCIe Fast Ethernet" },
1122f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151, 6 * 1024,
1132f70cceaSPyun YongHyeon 		"Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
1142f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151_V2, 6 * 1024,
1152f70cceaSPyun YongHyeon 		"Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
1162f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B, 6 * 1024,
1172f70cceaSPyun YongHyeon 		"Atheros AR8152 v1.1 PCIe Fast Ethernet" },
1182f70cceaSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B2, 6 * 1024,
1192f70cceaSPyun YongHyeon 		"Atheros AR8152 v2.0 PCIe Fast Ethernet" },
120b624ef0aSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8161, 9 * 1024,
121b624ef0aSPyun YongHyeon 		"Atheros AR8161 PCIe Gigabit Ethernet" },
122b624ef0aSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8162, 9 * 1024,
1230a9aceb8SPyun YongHyeon 		"Atheros AR8162 PCIe Fast Ethernet" },
124b624ef0aSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8171, 9 * 1024,
1250a9aceb8SPyun YongHyeon 		"Atheros AR8171 PCIe Gigabit Ethernet" },
126b624ef0aSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8172, 9 * 1024,
1270a9aceb8SPyun YongHyeon 		"Atheros AR8172 PCIe Fast Ethernet" },
128b624ef0aSPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_E2200, 9 * 1024,
129b624ef0aSPyun YongHyeon 		"Killer E2200 Gigabit Ethernet" },
130477cba21SPyun YongHyeon 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_E2400, 9 * 1024,
131477cba21SPyun YongHyeon 		"Killer E2400 Gigabit Ethernet" },
1321536a1b8SSepherosa Ziehau 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_E2500, 9 * 1024,
1331536a1b8SSepherosa Ziehau 		"Killer E2500 Gigabit Ethernet" },
1342f70cceaSPyun YongHyeon 	{ 0, 0, 0, NULL}
135d68875ebSPyun YongHyeon };
136d68875ebSPyun YongHyeon 
137b624ef0aSPyun YongHyeon static void	alc_aspm(struct alc_softc *, int, int);
138b624ef0aSPyun YongHyeon static void	alc_aspm_813x(struct alc_softc *, int);
139b624ef0aSPyun YongHyeon static void	alc_aspm_816x(struct alc_softc *, int);
140d68875ebSPyun YongHyeon static int	alc_attach(device_t);
141d68875ebSPyun YongHyeon static int	alc_check_boundary(struct alc_softc *);
142b624ef0aSPyun YongHyeon static void	alc_config_msi(struct alc_softc *);
143d68875ebSPyun YongHyeon static int	alc_detach(device_t);
144d68875ebSPyun YongHyeon static void	alc_disable_l0s_l1(struct alc_softc *);
145d68875ebSPyun YongHyeon static int	alc_dma_alloc(struct alc_softc *);
146d68875ebSPyun YongHyeon static void	alc_dma_free(struct alc_softc *);
147d68875ebSPyun YongHyeon static void	alc_dmamap_cb(void *, bus_dma_segment_t *, int, int);
148b624ef0aSPyun YongHyeon static void	alc_dsp_fixup(struct alc_softc *, int);
149d68875ebSPyun YongHyeon static int	alc_encap(struct alc_softc *, struct mbuf **);
1502f70cceaSPyun YongHyeon static struct alc_ident *
1512f70cceaSPyun YongHyeon 		alc_find_ident(device_t);
152d68875ebSPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
153d68875ebSPyun YongHyeon static struct mbuf *
15452436412SJustin Hibbits 		alc_fixup_rx(if_t, struct mbuf *);
155d68875ebSPyun YongHyeon #endif
156d68875ebSPyun YongHyeon static void	alc_get_macaddr(struct alc_softc *);
157b624ef0aSPyun YongHyeon static void	alc_get_macaddr_813x(struct alc_softc *);
158b624ef0aSPyun YongHyeon static void	alc_get_macaddr_816x(struct alc_softc *);
159b624ef0aSPyun YongHyeon static void	alc_get_macaddr_par(struct alc_softc *);
160d68875ebSPyun YongHyeon static void	alc_init(void *);
161d68875ebSPyun YongHyeon static void	alc_init_cmb(struct alc_softc *);
162d68875ebSPyun YongHyeon static void	alc_init_locked(struct alc_softc *);
163d68875ebSPyun YongHyeon static void	alc_init_rr_ring(struct alc_softc *);
164d68875ebSPyun YongHyeon static int	alc_init_rx_ring(struct alc_softc *);
165d68875ebSPyun YongHyeon static void	alc_init_smb(struct alc_softc *);
166d68875ebSPyun YongHyeon static void	alc_init_tx_ring(struct alc_softc *);
167d68875ebSPyun YongHyeon static void	alc_int_task(void *, int);
168d68875ebSPyun YongHyeon static int	alc_intr(void *);
16952436412SJustin Hibbits static int	alc_ioctl(if_t, u_long, caddr_t);
170d68875ebSPyun YongHyeon static void	alc_mac_config(struct alc_softc *);
171b624ef0aSPyun YongHyeon static uint32_t	alc_mii_readreg_813x(struct alc_softc *, int, int);
172b624ef0aSPyun YongHyeon static uint32_t	alc_mii_readreg_816x(struct alc_softc *, int, int);
173b624ef0aSPyun YongHyeon static uint32_t	alc_mii_writereg_813x(struct alc_softc *, int, int, int);
174b624ef0aSPyun YongHyeon static uint32_t	alc_mii_writereg_816x(struct alc_softc *, int, int, int);
175d68875ebSPyun YongHyeon static int	alc_miibus_readreg(device_t, int, int);
176d68875ebSPyun YongHyeon static void	alc_miibus_statchg(device_t);
177d68875ebSPyun YongHyeon static int	alc_miibus_writereg(device_t, int, int, int);
178b624ef0aSPyun YongHyeon static uint32_t	alc_miidbg_readreg(struct alc_softc *, int);
179b624ef0aSPyun YongHyeon static uint32_t	alc_miidbg_writereg(struct alc_softc *, int, int);
180b624ef0aSPyun YongHyeon static uint32_t	alc_miiext_readreg(struct alc_softc *, int, int);
181b624ef0aSPyun YongHyeon static uint32_t	alc_miiext_writereg(struct alc_softc *, int, int, int);
18252436412SJustin Hibbits static int	alc_mediachange(if_t);
183b624ef0aSPyun YongHyeon static int	alc_mediachange_locked(struct alc_softc *);
18452436412SJustin Hibbits static void	alc_mediastatus(if_t, struct ifmediareq *);
185d68875ebSPyun YongHyeon static int	alc_newbuf(struct alc_softc *, struct alc_rxdesc *);
186b624ef0aSPyun YongHyeon static void	alc_osc_reset(struct alc_softc *);
187d68875ebSPyun YongHyeon static void	alc_phy_down(struct alc_softc *);
188d68875ebSPyun YongHyeon static void	alc_phy_reset(struct alc_softc *);
189b624ef0aSPyun YongHyeon static void	alc_phy_reset_813x(struct alc_softc *);
190b624ef0aSPyun YongHyeon static void	alc_phy_reset_816x(struct alc_softc *);
191d68875ebSPyun YongHyeon static int	alc_probe(device_t);
192d68875ebSPyun YongHyeon static void	alc_reset(struct alc_softc *);
193d68875ebSPyun YongHyeon static int	alc_resume(device_t);
194d68875ebSPyun YongHyeon static void	alc_rxeof(struct alc_softc *, struct rx_rdesc *);
195d68875ebSPyun YongHyeon static int	alc_rxintr(struct alc_softc *, int);
196d68875ebSPyun YongHyeon static void	alc_rxfilter(struct alc_softc *);
197d68875ebSPyun YongHyeon static void	alc_rxvlan(struct alc_softc *);
198d68875ebSPyun YongHyeon static void	alc_setlinkspeed(struct alc_softc *);
199d68875ebSPyun YongHyeon static void	alc_setwol(struct alc_softc *);
200b624ef0aSPyun YongHyeon static void	alc_setwol_813x(struct alc_softc *);
201b624ef0aSPyun YongHyeon static void	alc_setwol_816x(struct alc_softc *);
202d68875ebSPyun YongHyeon static int	alc_shutdown(device_t);
20352436412SJustin Hibbits static void	alc_start(if_t);
20452436412SJustin Hibbits static void	alc_start_locked(if_t);
205d68875ebSPyun YongHyeon static void	alc_start_queue(struct alc_softc *);
2068a466583SMark Johnston static void	alc_start_tx(struct alc_softc *);
207d68875ebSPyun YongHyeon static void	alc_stats_clear(struct alc_softc *);
208d68875ebSPyun YongHyeon static void	alc_stats_update(struct alc_softc *);
209d68875ebSPyun YongHyeon static void	alc_stop(struct alc_softc *);
210d68875ebSPyun YongHyeon static void	alc_stop_mac(struct alc_softc *);
211d68875ebSPyun YongHyeon static void	alc_stop_queue(struct alc_softc *);
212d68875ebSPyun YongHyeon static int	alc_suspend(device_t);
213d68875ebSPyun YongHyeon static void	alc_sysctl_node(struct alc_softc *);
214d68875ebSPyun YongHyeon static void	alc_tick(void *);
215d68875ebSPyun YongHyeon static void	alc_txeof(struct alc_softc *);
216d68875ebSPyun YongHyeon static void	alc_watchdog(struct alc_softc *);
217d68875ebSPyun YongHyeon static int	sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
218d68875ebSPyun YongHyeon static int	sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS);
219d68875ebSPyun YongHyeon static int	sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS);
220d68875ebSPyun YongHyeon 
2217790c8c1SConrad Meyer DEBUGNET_DEFINE(alc);
2228a466583SMark Johnston 
223d68875ebSPyun YongHyeon static device_method_t alc_methods[] = {
224d68875ebSPyun YongHyeon 	/* Device interface. */
225d68875ebSPyun YongHyeon 	DEVMETHOD(device_probe,		alc_probe),
226d68875ebSPyun YongHyeon 	DEVMETHOD(device_attach,	alc_attach),
227d68875ebSPyun YongHyeon 	DEVMETHOD(device_detach,	alc_detach),
228d68875ebSPyun YongHyeon 	DEVMETHOD(device_shutdown,	alc_shutdown),
229d68875ebSPyun YongHyeon 	DEVMETHOD(device_suspend,	alc_suspend),
230d68875ebSPyun YongHyeon 	DEVMETHOD(device_resume,	alc_resume),
231d68875ebSPyun YongHyeon 
232d68875ebSPyun YongHyeon 	/* MII interface. */
233d68875ebSPyun YongHyeon 	DEVMETHOD(miibus_readreg,	alc_miibus_readreg),
234d68875ebSPyun YongHyeon 	DEVMETHOD(miibus_writereg,	alc_miibus_writereg),
235d68875ebSPyun YongHyeon 	DEVMETHOD(miibus_statchg,	alc_miibus_statchg),
236d68875ebSPyun YongHyeon 
2378a466583SMark Johnston 	DEVMETHOD_END
238d68875ebSPyun YongHyeon };
239d68875ebSPyun YongHyeon 
240d68875ebSPyun YongHyeon static driver_t alc_driver = {
241d68875ebSPyun YongHyeon 	"alc",
242d68875ebSPyun YongHyeon 	alc_methods,
243d68875ebSPyun YongHyeon 	sizeof(struct alc_softc)
244d68875ebSPyun YongHyeon };
245d68875ebSPyun YongHyeon 
2466d51c76dSJohn Baldwin DRIVER_MODULE(alc, pci, alc_driver, 0, 0);
247b042bc51SWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device", pci, alc, alc_ident_table,
248329e817fSWarner Losh     nitems(alc_ident_table) - 1);
2493e38757dSJohn Baldwin DRIVER_MODULE(miibus, alc, miibus_driver, 0, 0);
250d68875ebSPyun YongHyeon 
251d68875ebSPyun YongHyeon static struct resource_spec alc_res_spec_mem[] = {
252d68875ebSPyun YongHyeon 	{ SYS_RES_MEMORY,	PCIR_BAR(0),	RF_ACTIVE },
253d68875ebSPyun YongHyeon 	{ -1,			0,		0 }
254d68875ebSPyun YongHyeon };
255d68875ebSPyun YongHyeon 
256d68875ebSPyun YongHyeon static struct resource_spec alc_irq_spec_legacy[] = {
257d68875ebSPyun YongHyeon 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
258d68875ebSPyun YongHyeon 	{ -1,			0,		0 }
259d68875ebSPyun YongHyeon };
260d68875ebSPyun YongHyeon 
261d68875ebSPyun YongHyeon static struct resource_spec alc_irq_spec_msi[] = {
262d68875ebSPyun YongHyeon 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
263d68875ebSPyun YongHyeon 	{ -1,			0,		0 }
264d68875ebSPyun YongHyeon };
265d68875ebSPyun YongHyeon 
266d68875ebSPyun YongHyeon static struct resource_spec alc_irq_spec_msix[] = {
267d68875ebSPyun YongHyeon 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
268d68875ebSPyun YongHyeon 	{ -1,			0,		0 }
269d68875ebSPyun YongHyeon };
270d68875ebSPyun YongHyeon 
27103b4253bSPyun YongHyeon static uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0, 0 };
272d68875ebSPyun YongHyeon 
273d68875ebSPyun YongHyeon static int
alc_miibus_readreg(device_t dev,int phy,int reg)274d68875ebSPyun YongHyeon alc_miibus_readreg(device_t dev, int phy, int reg)
275d68875ebSPyun YongHyeon {
276d68875ebSPyun YongHyeon 	struct alc_softc *sc;
277b624ef0aSPyun YongHyeon 	int v;
278d68875ebSPyun YongHyeon 
279d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
280b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
281b624ef0aSPyun YongHyeon 		v = alc_mii_readreg_816x(sc, phy, reg);
282b624ef0aSPyun YongHyeon 	else
283b624ef0aSPyun YongHyeon 		v = alc_mii_readreg_813x(sc, phy, reg);
284b624ef0aSPyun YongHyeon 	return (v);
285b624ef0aSPyun YongHyeon }
286b624ef0aSPyun YongHyeon 
287b624ef0aSPyun YongHyeon static uint32_t
alc_mii_readreg_813x(struct alc_softc * sc,int phy,int reg)288b624ef0aSPyun YongHyeon alc_mii_readreg_813x(struct alc_softc *sc, int phy, int reg)
289b624ef0aSPyun YongHyeon {
290b624ef0aSPyun YongHyeon 	uint32_t v;
291b624ef0aSPyun YongHyeon 	int i;
292d68875ebSPyun YongHyeon 
293e3413501SPyun YongHyeon 	/*
294e3413501SPyun YongHyeon 	 * For AR8132 fast ethernet controller, do not report 1000baseT
295e3413501SPyun YongHyeon 	 * capability to mii(4). Even though AR8132 uses the same
296e3413501SPyun YongHyeon 	 * model/revision number of F1 gigabit PHY, the PHY has no
297e3413501SPyun YongHyeon 	 * ability to establish 1000baseT link.
298e3413501SPyun YongHyeon 	 */
299e3413501SPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
300e3413501SPyun YongHyeon 	    reg == MII_EXTSR)
301e3413501SPyun YongHyeon 		return (0);
302e3413501SPyun YongHyeon 
303d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
304d68875ebSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
305d68875ebSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
306d68875ebSPyun YongHyeon 		DELAY(5);
307d68875ebSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
308d68875ebSPyun YongHyeon 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
309d68875ebSPyun YongHyeon 			break;
310d68875ebSPyun YongHyeon 	}
311d68875ebSPyun YongHyeon 
312d68875ebSPyun YongHyeon 	if (i == 0) {
313d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
314d68875ebSPyun YongHyeon 		return (0);
315d68875ebSPyun YongHyeon 	}
316d68875ebSPyun YongHyeon 
317d68875ebSPyun YongHyeon 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
318d68875ebSPyun YongHyeon }
319d68875ebSPyun YongHyeon 
320b624ef0aSPyun YongHyeon static uint32_t
alc_mii_readreg_816x(struct alc_softc * sc,int phy,int reg)321b624ef0aSPyun YongHyeon alc_mii_readreg_816x(struct alc_softc *sc, int phy, int reg)
322b624ef0aSPyun YongHyeon {
323b624ef0aSPyun YongHyeon 	uint32_t clk, v;
324b624ef0aSPyun YongHyeon 	int i;
325b624ef0aSPyun YongHyeon 
326b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
327b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_128;
328b624ef0aSPyun YongHyeon 	else
329b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_4;
330b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
331b624ef0aSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | clk | MDIO_REG_ADDR(reg));
332b624ef0aSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
333b624ef0aSPyun YongHyeon 		DELAY(5);
334b624ef0aSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
335b624ef0aSPyun YongHyeon 		if ((v & MDIO_OP_BUSY) == 0)
336b624ef0aSPyun YongHyeon 			break;
337b624ef0aSPyun YongHyeon 	}
338b624ef0aSPyun YongHyeon 
339b624ef0aSPyun YongHyeon 	if (i == 0) {
340b624ef0aSPyun YongHyeon 		device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
341b624ef0aSPyun YongHyeon 		return (0);
342b624ef0aSPyun YongHyeon 	}
343b624ef0aSPyun YongHyeon 
344b624ef0aSPyun YongHyeon 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
345b624ef0aSPyun YongHyeon }
346b624ef0aSPyun YongHyeon 
347d68875ebSPyun YongHyeon static int
alc_miibus_writereg(device_t dev,int phy,int reg,int val)348d68875ebSPyun YongHyeon alc_miibus_writereg(device_t dev, int phy, int reg, int val)
349d68875ebSPyun YongHyeon {
350d68875ebSPyun YongHyeon 	struct alc_softc *sc;
351b624ef0aSPyun YongHyeon 	int v;
352d68875ebSPyun YongHyeon 
353d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
354b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
355b624ef0aSPyun YongHyeon 		v = alc_mii_writereg_816x(sc, phy, reg, val);
356b624ef0aSPyun YongHyeon 	else
357b624ef0aSPyun YongHyeon 		v = alc_mii_writereg_813x(sc, phy, reg, val);
358b624ef0aSPyun YongHyeon 	return (v);
359b624ef0aSPyun YongHyeon }
360b624ef0aSPyun YongHyeon 
361b624ef0aSPyun YongHyeon static uint32_t
alc_mii_writereg_813x(struct alc_softc * sc,int phy,int reg,int val)362b624ef0aSPyun YongHyeon alc_mii_writereg_813x(struct alc_softc *sc, int phy, int reg, int val)
363b624ef0aSPyun YongHyeon {
364b624ef0aSPyun YongHyeon 	uint32_t v;
365b624ef0aSPyun YongHyeon 	int i;
366d68875ebSPyun YongHyeon 
367d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
368d68875ebSPyun YongHyeon 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
369d68875ebSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
370d68875ebSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
371d68875ebSPyun YongHyeon 		DELAY(5);
372d68875ebSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
373d68875ebSPyun YongHyeon 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
374d68875ebSPyun YongHyeon 			break;
375d68875ebSPyun YongHyeon 	}
376d68875ebSPyun YongHyeon 
377d68875ebSPyun YongHyeon 	if (i == 0)
378d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
379d68875ebSPyun YongHyeon 
380d68875ebSPyun YongHyeon 	return (0);
381d68875ebSPyun YongHyeon }
382d68875ebSPyun YongHyeon 
383b624ef0aSPyun YongHyeon static uint32_t
alc_mii_writereg_816x(struct alc_softc * sc,int phy,int reg,int val)384b624ef0aSPyun YongHyeon alc_mii_writereg_816x(struct alc_softc *sc, int phy, int reg, int val)
385b624ef0aSPyun YongHyeon {
386b624ef0aSPyun YongHyeon 	uint32_t clk, v;
387b624ef0aSPyun YongHyeon 	int i;
388b624ef0aSPyun YongHyeon 
389b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
390b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_128;
391b624ef0aSPyun YongHyeon 	else
392b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_4;
393b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
394b624ef0aSPyun YongHyeon 	    ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) | MDIO_REG_ADDR(reg) |
395b624ef0aSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | clk);
396b624ef0aSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
397b624ef0aSPyun YongHyeon 		DELAY(5);
398b624ef0aSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
399b624ef0aSPyun YongHyeon 		if ((v & MDIO_OP_BUSY) == 0)
400b624ef0aSPyun YongHyeon 			break;
401b624ef0aSPyun YongHyeon 	}
402b624ef0aSPyun YongHyeon 
403b624ef0aSPyun YongHyeon 	if (i == 0)
404b624ef0aSPyun YongHyeon 		device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
405b624ef0aSPyun YongHyeon 
406b624ef0aSPyun YongHyeon 	return (0);
407b624ef0aSPyun YongHyeon }
408b624ef0aSPyun YongHyeon 
409d68875ebSPyun YongHyeon static void
alc_miibus_statchg(device_t dev)410d68875ebSPyun YongHyeon alc_miibus_statchg(device_t dev)
411d68875ebSPyun YongHyeon {
412d68875ebSPyun YongHyeon 	struct alc_softc *sc;
413d68875ebSPyun YongHyeon 	struct mii_data *mii;
41452436412SJustin Hibbits 	if_t ifp;
415d68875ebSPyun YongHyeon 	uint32_t reg;
416d68875ebSPyun YongHyeon 
417d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
418d68875ebSPyun YongHyeon 
419d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
420d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
421d68875ebSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
42252436412SJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
423d68875ebSPyun YongHyeon 		return;
424d68875ebSPyun YongHyeon 
425d68875ebSPyun YongHyeon 	sc->alc_flags &= ~ALC_FLAG_LINK;
426d68875ebSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
427d68875ebSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
428d68875ebSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
429d68875ebSPyun YongHyeon 		case IFM_10_T:
430d68875ebSPyun YongHyeon 		case IFM_100_TX:
431d68875ebSPyun YongHyeon 			sc->alc_flags |= ALC_FLAG_LINK;
432d68875ebSPyun YongHyeon 			break;
433d68875ebSPyun YongHyeon 		case IFM_1000_T:
434d68875ebSPyun YongHyeon 			if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
435d68875ebSPyun YongHyeon 				sc->alc_flags |= ALC_FLAG_LINK;
436d68875ebSPyun YongHyeon 			break;
437d68875ebSPyun YongHyeon 		default:
438d68875ebSPyun YongHyeon 			break;
439d68875ebSPyun YongHyeon 		}
440d68875ebSPyun YongHyeon 	}
441d68875ebSPyun YongHyeon 	/* Stop Rx/Tx MACs. */
442d68875ebSPyun YongHyeon 	alc_stop_mac(sc);
443d68875ebSPyun YongHyeon 
444d68875ebSPyun YongHyeon 	/* Program MACs with resolved speed/duplex/flow-control. */
445d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
446d68875ebSPyun YongHyeon 		alc_start_queue(sc);
447d68875ebSPyun YongHyeon 		alc_mac_config(sc);
448d68875ebSPyun YongHyeon 		/* Re-enable Tx/Rx MACs. */
449d68875ebSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_MAC_CFG);
450d68875ebSPyun YongHyeon 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
451d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
452b624ef0aSPyun YongHyeon 	}
453b624ef0aSPyun YongHyeon 	alc_aspm(sc, 0, IFM_SUBTYPE(mii->mii_media_active));
454b624ef0aSPyun YongHyeon 	alc_dsp_fixup(sc, IFM_SUBTYPE(mii->mii_media_active));
455b624ef0aSPyun YongHyeon }
456b624ef0aSPyun YongHyeon 
457b624ef0aSPyun YongHyeon static uint32_t
alc_miidbg_readreg(struct alc_softc * sc,int reg)458b624ef0aSPyun YongHyeon alc_miidbg_readreg(struct alc_softc *sc, int reg)
459b624ef0aSPyun YongHyeon {
460b624ef0aSPyun YongHyeon 
461b624ef0aSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
462b624ef0aSPyun YongHyeon 	    reg);
463b624ef0aSPyun YongHyeon 	return (alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
464b624ef0aSPyun YongHyeon 	    ALC_MII_DBG_DATA));
465b624ef0aSPyun YongHyeon }
466b624ef0aSPyun YongHyeon 
467b624ef0aSPyun YongHyeon static uint32_t
alc_miidbg_writereg(struct alc_softc * sc,int reg,int val)468b624ef0aSPyun YongHyeon alc_miidbg_writereg(struct alc_softc *sc, int reg, int val)
469b624ef0aSPyun YongHyeon {
470b624ef0aSPyun YongHyeon 
471b624ef0aSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
472b624ef0aSPyun YongHyeon 	    reg);
473b624ef0aSPyun YongHyeon 	return (alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
474b624ef0aSPyun YongHyeon 	    ALC_MII_DBG_DATA, val));
475b624ef0aSPyun YongHyeon }
476b624ef0aSPyun YongHyeon 
477b624ef0aSPyun YongHyeon static uint32_t
alc_miiext_readreg(struct alc_softc * sc,int devaddr,int reg)478b624ef0aSPyun YongHyeon alc_miiext_readreg(struct alc_softc *sc, int devaddr, int reg)
479b624ef0aSPyun YongHyeon {
480b624ef0aSPyun YongHyeon 	uint32_t clk, v;
481b624ef0aSPyun YongHyeon 	int i;
482b624ef0aSPyun YongHyeon 
483b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
484b624ef0aSPyun YongHyeon 	    EXT_MDIO_DEVADDR(devaddr));
485b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
486b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_128;
487b624ef0aSPyun YongHyeon 	else
488b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_4;
489b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
490b624ef0aSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
491b624ef0aSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
492b624ef0aSPyun YongHyeon 		DELAY(5);
493b624ef0aSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
494b624ef0aSPyun YongHyeon 		if ((v & MDIO_OP_BUSY) == 0)
495b624ef0aSPyun YongHyeon 			break;
496b624ef0aSPyun YongHyeon 	}
497b624ef0aSPyun YongHyeon 
498b624ef0aSPyun YongHyeon 	if (i == 0) {
499b624ef0aSPyun YongHyeon 		device_printf(sc->alc_dev, "phy ext read timeout : %d, %d\n",
500b624ef0aSPyun YongHyeon 		    devaddr, reg);
501b624ef0aSPyun YongHyeon 		return (0);
502b624ef0aSPyun YongHyeon 	}
503b624ef0aSPyun YongHyeon 
504b624ef0aSPyun YongHyeon 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
505b624ef0aSPyun YongHyeon }
506b624ef0aSPyun YongHyeon 
507b624ef0aSPyun YongHyeon static uint32_t
alc_miiext_writereg(struct alc_softc * sc,int devaddr,int reg,int val)508b624ef0aSPyun YongHyeon alc_miiext_writereg(struct alc_softc *sc, int devaddr, int reg, int val)
509b624ef0aSPyun YongHyeon {
510b624ef0aSPyun YongHyeon 	uint32_t clk, v;
511b624ef0aSPyun YongHyeon 	int i;
512b624ef0aSPyun YongHyeon 
513b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
514b624ef0aSPyun YongHyeon 	    EXT_MDIO_DEVADDR(devaddr));
515b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
516b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_128;
517b624ef0aSPyun YongHyeon 	else
518b624ef0aSPyun YongHyeon 		clk = MDIO_CLK_25_4;
519b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
520b624ef0aSPyun YongHyeon 	    ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) |
521b624ef0aSPyun YongHyeon 	    MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
522b624ef0aSPyun YongHyeon 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
523b624ef0aSPyun YongHyeon 		DELAY(5);
524b624ef0aSPyun YongHyeon 		v = CSR_READ_4(sc, ALC_MDIO);
525b624ef0aSPyun YongHyeon 		if ((v & MDIO_OP_BUSY) == 0)
526b624ef0aSPyun YongHyeon 			break;
527b624ef0aSPyun YongHyeon 	}
528b624ef0aSPyun YongHyeon 
529b624ef0aSPyun YongHyeon 	if (i == 0)
530b624ef0aSPyun YongHyeon 		device_printf(sc->alc_dev, "phy ext write timeout : %d, %d\n",
531b624ef0aSPyun YongHyeon 		    devaddr, reg);
532b624ef0aSPyun YongHyeon 
533b624ef0aSPyun YongHyeon 	return (0);
534b624ef0aSPyun YongHyeon }
535b624ef0aSPyun YongHyeon 
536b624ef0aSPyun YongHyeon static void
alc_dsp_fixup(struct alc_softc * sc,int media)537b624ef0aSPyun YongHyeon alc_dsp_fixup(struct alc_softc *sc, int media)
538b624ef0aSPyun YongHyeon {
539b624ef0aSPyun YongHyeon 	uint16_t agc, len, val;
540b624ef0aSPyun YongHyeon 
541b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
542b624ef0aSPyun YongHyeon 		return;
543b624ef0aSPyun YongHyeon 	if (AR816X_REV(sc->alc_rev) >= AR816X_REV_C0)
544b624ef0aSPyun YongHyeon 		return;
545b624ef0aSPyun YongHyeon 
546b624ef0aSPyun YongHyeon 	/*
547b624ef0aSPyun YongHyeon 	 * Vendor PHY magic.
548b624ef0aSPyun YongHyeon 	 * 1000BT/AZ, wrong cable length
549b624ef0aSPyun YongHyeon 	 */
550b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
551b624ef0aSPyun YongHyeon 		len = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL6);
552b624ef0aSPyun YongHyeon 		len = (len >> EXT_CLDCTL6_CAB_LEN_SHIFT) &
553b624ef0aSPyun YongHyeon 		    EXT_CLDCTL6_CAB_LEN_MASK;
554b624ef0aSPyun YongHyeon 		agc = alc_miidbg_readreg(sc, MII_DBG_AGC);
555b624ef0aSPyun YongHyeon 		agc = (agc >> DBG_AGC_2_VGA_SHIFT) & DBG_AGC_2_VGA_MASK;
556b624ef0aSPyun YongHyeon 		if ((media == IFM_1000_T && len > EXT_CLDCTL6_CAB_LEN_SHORT1G &&
557b624ef0aSPyun YongHyeon 		    agc > DBG_AGC_LONG1G_LIMT) ||
558b624ef0aSPyun YongHyeon 		    (media == IFM_100_TX && len > DBG_AGC_LONG100M_LIMT &&
559b624ef0aSPyun YongHyeon 		    agc > DBG_AGC_LONG1G_LIMT)) {
560b624ef0aSPyun YongHyeon 			alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
561b624ef0aSPyun YongHyeon 			    DBG_AZ_ANADECT_LONG);
562b624ef0aSPyun YongHyeon 			val = alc_miiext_readreg(sc, MII_EXT_ANEG,
563b624ef0aSPyun YongHyeon 			    MII_EXT_ANEG_AFE);
564b624ef0aSPyun YongHyeon 			val |= ANEG_AFEE_10BT_100M_TH;
565b624ef0aSPyun YongHyeon 			alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
566b624ef0aSPyun YongHyeon 			    val);
567b624ef0aSPyun YongHyeon 		} else {
568b624ef0aSPyun YongHyeon 			alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
569b624ef0aSPyun YongHyeon 			    DBG_AZ_ANADECT_DEFAULT);
570b624ef0aSPyun YongHyeon 			val = alc_miiext_readreg(sc, MII_EXT_ANEG,
571b624ef0aSPyun YongHyeon 			    MII_EXT_ANEG_AFE);
572b624ef0aSPyun YongHyeon 			val &= ~ANEG_AFEE_10BT_100M_TH;
573b624ef0aSPyun YongHyeon 			alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
574b624ef0aSPyun YongHyeon 			    val);
575b624ef0aSPyun YongHyeon 		}
576b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
577b624ef0aSPyun YongHyeon 		    AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
578b624ef0aSPyun YongHyeon 			if (media == IFM_1000_T) {
579b624ef0aSPyun YongHyeon 				/*
580b624ef0aSPyun YongHyeon 				 * Giga link threshold, raise the tolerance of
581b624ef0aSPyun YongHyeon 				 * noise 50%.
582b624ef0aSPyun YongHyeon 				 */
583b624ef0aSPyun YongHyeon 				val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
584b624ef0aSPyun YongHyeon 				val &= ~DBG_MSE20DB_TH_MASK;
585b624ef0aSPyun YongHyeon 				val |= (DBG_MSE20DB_TH_HI <<
586b624ef0aSPyun YongHyeon 				    DBG_MSE20DB_TH_SHIFT);
587b624ef0aSPyun YongHyeon 				alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
588b624ef0aSPyun YongHyeon 			} else if (media == IFM_100_TX)
589b624ef0aSPyun YongHyeon 				alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
590b624ef0aSPyun YongHyeon 				    DBG_MSE16DB_UP);
591b624ef0aSPyun YongHyeon 		}
592b624ef0aSPyun YongHyeon 	} else {
593b624ef0aSPyun YongHyeon 		val = alc_miiext_readreg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE);
594b624ef0aSPyun YongHyeon 		val &= ~ANEG_AFEE_10BT_100M_TH;
595b624ef0aSPyun YongHyeon 		alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, val);
596b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
597b624ef0aSPyun YongHyeon 		    AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
598b624ef0aSPyun YongHyeon 			alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
599b624ef0aSPyun YongHyeon 			    DBG_MSE16DB_DOWN);
600b624ef0aSPyun YongHyeon 			val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
601b624ef0aSPyun YongHyeon 			val &= ~DBG_MSE20DB_TH_MASK;
602b624ef0aSPyun YongHyeon 			val |= (DBG_MSE20DB_TH_DEFAULT << DBG_MSE20DB_TH_SHIFT);
603b624ef0aSPyun YongHyeon 			alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
604b624ef0aSPyun YongHyeon 		}
605d68875ebSPyun YongHyeon 	}
606d0b2f7efSPyun YongHyeon }
607d68875ebSPyun YongHyeon 
608d68875ebSPyun YongHyeon static void
alc_mediastatus(if_t ifp,struct ifmediareq * ifmr)60952436412SJustin Hibbits alc_mediastatus(if_t ifp, struct ifmediareq *ifmr)
610d68875ebSPyun YongHyeon {
611d68875ebSPyun YongHyeon 	struct alc_softc *sc;
612d68875ebSPyun YongHyeon 	struct mii_data *mii;
613d68875ebSPyun YongHyeon 
61452436412SJustin Hibbits 	sc = if_getsoftc(ifp);
615d68875ebSPyun YongHyeon 	ALC_LOCK(sc);
61652436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_UP) == 0) {
617d68875ebSPyun YongHyeon 		ALC_UNLOCK(sc);
618d68875ebSPyun YongHyeon 		return;
619d68875ebSPyun YongHyeon 	}
620d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
621d68875ebSPyun YongHyeon 
622d68875ebSPyun YongHyeon 	mii_pollstat(mii);
623d68875ebSPyun YongHyeon 	ifmr->ifm_status = mii->mii_media_status;
624d68875ebSPyun YongHyeon 	ifmr->ifm_active = mii->mii_media_active;
62557c81d92SPyun YongHyeon 	ALC_UNLOCK(sc);
626d68875ebSPyun YongHyeon }
627d68875ebSPyun YongHyeon 
628d68875ebSPyun YongHyeon static int
alc_mediachange(if_t ifp)62952436412SJustin Hibbits alc_mediachange(if_t ifp)
630d68875ebSPyun YongHyeon {
631d68875ebSPyun YongHyeon 	struct alc_softc *sc;
632d68875ebSPyun YongHyeon 	int error;
633d68875ebSPyun YongHyeon 
63452436412SJustin Hibbits 	sc = if_getsoftc(ifp);
635d68875ebSPyun YongHyeon 	ALC_LOCK(sc);
636b624ef0aSPyun YongHyeon 	error = alc_mediachange_locked(sc);
637b624ef0aSPyun YongHyeon 	ALC_UNLOCK(sc);
638b624ef0aSPyun YongHyeon 
639b624ef0aSPyun YongHyeon 	return (error);
640b624ef0aSPyun YongHyeon }
641b624ef0aSPyun YongHyeon 
642b624ef0aSPyun YongHyeon static int
alc_mediachange_locked(struct alc_softc * sc)643b624ef0aSPyun YongHyeon alc_mediachange_locked(struct alc_softc *sc)
644b624ef0aSPyun YongHyeon {
645b624ef0aSPyun YongHyeon 	struct mii_data *mii;
646b624ef0aSPyun YongHyeon 	struct mii_softc *miisc;
647b624ef0aSPyun YongHyeon 	int error;
648b624ef0aSPyun YongHyeon 
649b624ef0aSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
650b624ef0aSPyun YongHyeon 
651d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
652d68875ebSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
6533fcb7a53SMarius Strobl 		PHY_RESET(miisc);
654d68875ebSPyun YongHyeon 	error = mii_mediachg(mii);
655d68875ebSPyun YongHyeon 
656d68875ebSPyun YongHyeon 	return (error);
657d68875ebSPyun YongHyeon }
658d68875ebSPyun YongHyeon 
6592f70cceaSPyun YongHyeon static struct alc_ident *
alc_find_ident(device_t dev)6602f70cceaSPyun YongHyeon alc_find_ident(device_t dev)
661d68875ebSPyun YongHyeon {
6622f70cceaSPyun YongHyeon 	struct alc_ident *ident;
663d68875ebSPyun YongHyeon 	uint16_t vendor, devid;
664d68875ebSPyun YongHyeon 
665d68875ebSPyun YongHyeon 	vendor = pci_get_vendor(dev);
666d68875ebSPyun YongHyeon 	devid = pci_get_device(dev);
6672f70cceaSPyun YongHyeon 	for (ident = alc_ident_table; ident->name != NULL; ident++) {
6682f70cceaSPyun YongHyeon 		if (vendor == ident->vendorid && devid == ident->deviceid)
6692f70cceaSPyun YongHyeon 			return (ident);
670d68875ebSPyun YongHyeon 	}
6712f70cceaSPyun YongHyeon 
6722f70cceaSPyun YongHyeon 	return (NULL);
6732f70cceaSPyun YongHyeon }
6742f70cceaSPyun YongHyeon 
6752f70cceaSPyun YongHyeon static int
alc_probe(device_t dev)6762f70cceaSPyun YongHyeon alc_probe(device_t dev)
6772f70cceaSPyun YongHyeon {
6782f70cceaSPyun YongHyeon 	struct alc_ident *ident;
6792f70cceaSPyun YongHyeon 
6802f70cceaSPyun YongHyeon 	ident = alc_find_ident(dev);
6812f70cceaSPyun YongHyeon 	if (ident != NULL) {
6822f70cceaSPyun YongHyeon 		device_set_desc(dev, ident->name);
6832f70cceaSPyun YongHyeon 		return (BUS_PROBE_DEFAULT);
684d68875ebSPyun YongHyeon 	}
685d68875ebSPyun YongHyeon 
686d68875ebSPyun YongHyeon 	return (ENXIO);
687d68875ebSPyun YongHyeon }
688d68875ebSPyun YongHyeon 
689d68875ebSPyun YongHyeon static void
alc_get_macaddr(struct alc_softc * sc)690d68875ebSPyun YongHyeon alc_get_macaddr(struct alc_softc *sc)
691d68875ebSPyun YongHyeon {
692b624ef0aSPyun YongHyeon 
693b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
694b624ef0aSPyun YongHyeon 		alc_get_macaddr_816x(sc);
695b624ef0aSPyun YongHyeon 	else
696b624ef0aSPyun YongHyeon 		alc_get_macaddr_813x(sc);
697b624ef0aSPyun YongHyeon }
698b624ef0aSPyun YongHyeon 
699b624ef0aSPyun YongHyeon static void
alc_get_macaddr_813x(struct alc_softc * sc)700b624ef0aSPyun YongHyeon alc_get_macaddr_813x(struct alc_softc *sc)
701b624ef0aSPyun YongHyeon {
702b624ef0aSPyun YongHyeon 	uint32_t opt;
7032f70cceaSPyun YongHyeon 	uint16_t val;
7042f70cceaSPyun YongHyeon 	int eeprom, i;
705d68875ebSPyun YongHyeon 
7062f70cceaSPyun YongHyeon 	eeprom = 0;
707d68875ebSPyun YongHyeon 	opt = CSR_READ_4(sc, ALC_OPT_CFG);
7082f70cceaSPyun YongHyeon 	if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
7092f70cceaSPyun YongHyeon 	    (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
710d68875ebSPyun YongHyeon 		/*
711d68875ebSPyun YongHyeon 		 * EEPROM found, let TWSI reload EEPROM configuration.
712d68875ebSPyun YongHyeon 		 * This will set ethernet address of controller.
713d68875ebSPyun YongHyeon 		 */
7142f70cceaSPyun YongHyeon 		eeprom++;
7152f70cceaSPyun YongHyeon 		switch (sc->alc_ident->deviceid) {
7162f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8131:
7172f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8132:
718d68875ebSPyun YongHyeon 			if ((opt & OPT_CFG_CLK_ENB) == 0) {
719d68875ebSPyun YongHyeon 				opt |= OPT_CFG_CLK_ENB;
720d68875ebSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
721d68875ebSPyun YongHyeon 				CSR_READ_4(sc, ALC_OPT_CFG);
722d68875ebSPyun YongHyeon 				DELAY(1000);
723d68875ebSPyun YongHyeon 			}
7242f70cceaSPyun YongHyeon 			break;
7252f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8151:
7262f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8151_V2:
7272f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8152_B:
7282f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8152_B2:
7292f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7302f70cceaSPyun YongHyeon 			    ALC_MII_DBG_ADDR, 0x00);
7312f70cceaSPyun YongHyeon 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
7322f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA);
7332f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7342f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA, val & 0xFF7F);
7352f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7362f70cceaSPyun YongHyeon 			    ALC_MII_DBG_ADDR, 0x3B);
7372f70cceaSPyun YongHyeon 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
7382f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA);
7392f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7402f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA, val | 0x0008);
7412f70cceaSPyun YongHyeon 			DELAY(20);
7422f70cceaSPyun YongHyeon 			break;
7432f70cceaSPyun YongHyeon 		}
7442f70cceaSPyun YongHyeon 
7452f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
7462f70cceaSPyun YongHyeon 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
7472f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
7482f70cceaSPyun YongHyeon 		CSR_READ_4(sc, ALC_WOL_CFG);
7492f70cceaSPyun YongHyeon 
750d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
751d68875ebSPyun YongHyeon 		    TWSI_CFG_SW_LD_START);
752d68875ebSPyun YongHyeon 		for (i = 100; i > 0; i--) {
753d68875ebSPyun YongHyeon 			DELAY(1000);
754d68875ebSPyun YongHyeon 			if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
755d68875ebSPyun YongHyeon 			    TWSI_CFG_SW_LD_START) == 0)
756d68875ebSPyun YongHyeon 				break;
757d68875ebSPyun YongHyeon 		}
758d68875ebSPyun YongHyeon 		if (i == 0)
759d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
760d68875ebSPyun YongHyeon 			    "reloading EEPROM timeout!\n");
761d68875ebSPyun YongHyeon 	} else {
762d68875ebSPyun YongHyeon 		if (bootverbose)
763d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev, "EEPROM not found!\n");
764d68875ebSPyun YongHyeon 	}
7652f70cceaSPyun YongHyeon 	if (eeprom != 0) {
7662f70cceaSPyun YongHyeon 		switch (sc->alc_ident->deviceid) {
7672f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8131:
7682f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8132:
769d68875ebSPyun YongHyeon 			if ((opt & OPT_CFG_CLK_ENB) != 0) {
770d68875ebSPyun YongHyeon 				opt &= ~OPT_CFG_CLK_ENB;
771d68875ebSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
772d68875ebSPyun YongHyeon 				CSR_READ_4(sc, ALC_OPT_CFG);
773d68875ebSPyun YongHyeon 				DELAY(1000);
774d68875ebSPyun YongHyeon 			}
7752f70cceaSPyun YongHyeon 			break;
7762f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8151:
7772f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8151_V2:
7782f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8152_B:
7792f70cceaSPyun YongHyeon 		case DEVICEID_ATHEROS_AR8152_B2:
7802f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7812f70cceaSPyun YongHyeon 			    ALC_MII_DBG_ADDR, 0x00);
7822f70cceaSPyun YongHyeon 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
7832f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA);
7842f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7852f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA, val | 0x0080);
7862f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7872f70cceaSPyun YongHyeon 			    ALC_MII_DBG_ADDR, 0x3B);
7882f70cceaSPyun YongHyeon 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
7892f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA);
7902f70cceaSPyun YongHyeon 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
7912f70cceaSPyun YongHyeon 			    ALC_MII_DBG_DATA, val & 0xFFF7);
7922f70cceaSPyun YongHyeon 			DELAY(20);
7932f70cceaSPyun YongHyeon 			break;
7942f70cceaSPyun YongHyeon 		}
7952f70cceaSPyun YongHyeon 	}
796d68875ebSPyun YongHyeon 
797b624ef0aSPyun YongHyeon 	alc_get_macaddr_par(sc);
798b624ef0aSPyun YongHyeon }
799b624ef0aSPyun YongHyeon 
800b624ef0aSPyun YongHyeon static void
alc_get_macaddr_816x(struct alc_softc * sc)801b624ef0aSPyun YongHyeon alc_get_macaddr_816x(struct alc_softc *sc)
802b624ef0aSPyun YongHyeon {
803b624ef0aSPyun YongHyeon 	uint32_t reg;
804b624ef0aSPyun YongHyeon 	int i, reloaded;
805b624ef0aSPyun YongHyeon 
806b624ef0aSPyun YongHyeon 	reloaded = 0;
807b624ef0aSPyun YongHyeon 	/* Try to reload station address via TWSI. */
808b624ef0aSPyun YongHyeon 	for (i = 100; i > 0; i--) {
809b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_SLD);
810b624ef0aSPyun YongHyeon 		if ((reg & (SLD_PROGRESS | SLD_START)) == 0)
811b624ef0aSPyun YongHyeon 			break;
812b624ef0aSPyun YongHyeon 		DELAY(1000);
813b624ef0aSPyun YongHyeon 	}
814b624ef0aSPyun YongHyeon 	if (i != 0) {
815b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SLD, reg | SLD_START);
816b624ef0aSPyun YongHyeon 		for (i = 100; i > 0; i--) {
817b624ef0aSPyun YongHyeon 			DELAY(1000);
818b624ef0aSPyun YongHyeon 			reg = CSR_READ_4(sc, ALC_SLD);
819b624ef0aSPyun YongHyeon 			if ((reg & SLD_START) == 0)
820b624ef0aSPyun YongHyeon 				break;
821b624ef0aSPyun YongHyeon 		}
822b624ef0aSPyun YongHyeon 		if (i != 0)
823b624ef0aSPyun YongHyeon 			reloaded++;
824b624ef0aSPyun YongHyeon 		else if (bootverbose)
825b624ef0aSPyun YongHyeon 			device_printf(sc->alc_dev,
826b624ef0aSPyun YongHyeon 			    "reloading station address via TWSI timed out!\n");
827b624ef0aSPyun YongHyeon 	}
828b624ef0aSPyun YongHyeon 
829b624ef0aSPyun YongHyeon 	/* Try to reload station address from EEPROM or FLASH. */
830b624ef0aSPyun YongHyeon 	if (reloaded == 0) {
831b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_EEPROM_LD);
832b624ef0aSPyun YongHyeon 		if ((reg & (EEPROM_LD_EEPROM_EXIST |
833b624ef0aSPyun YongHyeon 		    EEPROM_LD_FLASH_EXIST)) != 0) {
834b624ef0aSPyun YongHyeon 			for (i = 100; i > 0; i--) {
835b624ef0aSPyun YongHyeon 				reg = CSR_READ_4(sc, ALC_EEPROM_LD);
836b624ef0aSPyun YongHyeon 				if ((reg & (EEPROM_LD_PROGRESS |
837b624ef0aSPyun YongHyeon 				    EEPROM_LD_START)) == 0)
838b624ef0aSPyun YongHyeon 					break;
839b624ef0aSPyun YongHyeon 				DELAY(1000);
840b624ef0aSPyun YongHyeon 			}
841b624ef0aSPyun YongHyeon 			if (i != 0) {
842b624ef0aSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_EEPROM_LD, reg |
843b624ef0aSPyun YongHyeon 				    EEPROM_LD_START);
844b624ef0aSPyun YongHyeon 				for (i = 100; i > 0; i--) {
845b624ef0aSPyun YongHyeon 					DELAY(1000);
846b624ef0aSPyun YongHyeon 					reg = CSR_READ_4(sc, ALC_EEPROM_LD);
847b624ef0aSPyun YongHyeon 					if ((reg & EEPROM_LD_START) == 0)
848b624ef0aSPyun YongHyeon 						break;
849b624ef0aSPyun YongHyeon 				}
850b624ef0aSPyun YongHyeon 			} else if (bootverbose)
851b624ef0aSPyun YongHyeon 				device_printf(sc->alc_dev,
852b624ef0aSPyun YongHyeon 				    "reloading EEPROM/FLASH timed out!\n");
853b624ef0aSPyun YongHyeon 		}
854b624ef0aSPyun YongHyeon 	}
855b624ef0aSPyun YongHyeon 
856b624ef0aSPyun YongHyeon 	alc_get_macaddr_par(sc);
857b624ef0aSPyun YongHyeon }
858b624ef0aSPyun YongHyeon 
859b624ef0aSPyun YongHyeon static void
alc_get_macaddr_par(struct alc_softc * sc)860b624ef0aSPyun YongHyeon alc_get_macaddr_par(struct alc_softc *sc)
861b624ef0aSPyun YongHyeon {
862b624ef0aSPyun YongHyeon 	uint32_t ea[2];
863b624ef0aSPyun YongHyeon 
864d68875ebSPyun YongHyeon 	ea[0] = CSR_READ_4(sc, ALC_PAR0);
865d68875ebSPyun YongHyeon 	ea[1] = CSR_READ_4(sc, ALC_PAR1);
866d68875ebSPyun YongHyeon 	sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
867d68875ebSPyun YongHyeon 	sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
868d68875ebSPyun YongHyeon 	sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
869d68875ebSPyun YongHyeon 	sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
870d68875ebSPyun YongHyeon 	sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
871d68875ebSPyun YongHyeon 	sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
872d68875ebSPyun YongHyeon }
873d68875ebSPyun YongHyeon 
874d68875ebSPyun YongHyeon static void
alc_disable_l0s_l1(struct alc_softc * sc)875d68875ebSPyun YongHyeon alc_disable_l0s_l1(struct alc_softc *sc)
876d68875ebSPyun YongHyeon {
877d68875ebSPyun YongHyeon 	uint32_t pmcfg;
878d68875ebSPyun YongHyeon 
879b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
880d68875ebSPyun YongHyeon 		/* Another magic from vendor. */
881d68875ebSPyun YongHyeon 		pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
882d68875ebSPyun YongHyeon 		pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
883b624ef0aSPyun YongHyeon 		    PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
884b624ef0aSPyun YongHyeon 		    PM_CFG_MAC_ASPM_CHK | PM_CFG_SERDES_PD_EX_L1);
885b624ef0aSPyun YongHyeon 		pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB |
886b624ef0aSPyun YongHyeon 		    PM_CFG_SERDES_PLL_L1_ENB | PM_CFG_SERDES_L1_ENB;
887d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
888d68875ebSPyun YongHyeon 	}
889b624ef0aSPyun YongHyeon }
890d68875ebSPyun YongHyeon 
891d68875ebSPyun YongHyeon static void
alc_phy_reset(struct alc_softc * sc)892d68875ebSPyun YongHyeon alc_phy_reset(struct alc_softc *sc)
893d68875ebSPyun YongHyeon {
894b624ef0aSPyun YongHyeon 
895b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
896b624ef0aSPyun YongHyeon 		alc_phy_reset_816x(sc);
897b624ef0aSPyun YongHyeon 	else
898b624ef0aSPyun YongHyeon 		alc_phy_reset_813x(sc);
899b624ef0aSPyun YongHyeon }
900b624ef0aSPyun YongHyeon 
901b624ef0aSPyun YongHyeon static void
alc_phy_reset_813x(struct alc_softc * sc)902b624ef0aSPyun YongHyeon alc_phy_reset_813x(struct alc_softc *sc)
903b624ef0aSPyun YongHyeon {
904d68875ebSPyun YongHyeon 	uint16_t data;
905d68875ebSPyun YongHyeon 
906d68875ebSPyun YongHyeon 	/* Reset magic from Linux. */
907462d5251SPyun YongHyeon 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
908d68875ebSPyun YongHyeon 	CSR_READ_2(sc, ALC_GPHY_CFG);
909d68875ebSPyun YongHyeon 	DELAY(10 * 1000);
910d68875ebSPyun YongHyeon 
911462d5251SPyun YongHyeon 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
912d68875ebSPyun YongHyeon 	    GPHY_CFG_SEL_ANA_RESET);
913d68875ebSPyun YongHyeon 	CSR_READ_2(sc, ALC_GPHY_CFG);
914d68875ebSPyun YongHyeon 	DELAY(10 * 1000);
915d68875ebSPyun YongHyeon 
9162f70cceaSPyun YongHyeon 	/* DSP fixup, Vendor magic. */
9172f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
9182f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9192f70cceaSPyun YongHyeon 		    ALC_MII_DBG_ADDR, 0x000A);
9202f70cceaSPyun YongHyeon 		data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
9212f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA);
9222f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9232f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA, data & 0xDFFF);
9242f70cceaSPyun YongHyeon 	}
9252f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
9262f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
9272f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
9282f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
9292f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9302f70cceaSPyun YongHyeon 		    ALC_MII_DBG_ADDR, 0x003B);
9312f70cceaSPyun YongHyeon 		data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
9322f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA);
9332f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9342f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA, data & 0xFFF7);
9352f70cceaSPyun YongHyeon 		DELAY(20 * 1000);
9362f70cceaSPyun YongHyeon 	}
9372f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151) {
9382f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9392f70cceaSPyun YongHyeon 		    ALC_MII_DBG_ADDR, 0x0029);
9402f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9412f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA, 0x929D);
9422f70cceaSPyun YongHyeon 	}
9432f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
9442f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132 ||
9452f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
9462f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
9472f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9482f70cceaSPyun YongHyeon 		    ALC_MII_DBG_ADDR, 0x0029);
9492f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
9502f70cceaSPyun YongHyeon 		    ALC_MII_DBG_DATA, 0xB6DD);
9512f70cceaSPyun YongHyeon 	}
9522f70cceaSPyun YongHyeon 
953d68875ebSPyun YongHyeon 	/* Load DSP codes, vendor magic. */
954d68875ebSPyun YongHyeon 	data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
955d68875ebSPyun YongHyeon 	    ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
956d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
957d68875ebSPyun YongHyeon 	    ALC_MII_DBG_ADDR, MII_ANA_CFG18);
958d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
959d68875ebSPyun YongHyeon 	    ALC_MII_DBG_DATA, data);
960d68875ebSPyun YongHyeon 
961d68875ebSPyun YongHyeon 	data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
962d68875ebSPyun YongHyeon 	    ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
963d68875ebSPyun YongHyeon 	    ANA_SERDES_EN_LCKDT;
964d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
965d68875ebSPyun YongHyeon 	    ALC_MII_DBG_ADDR, MII_ANA_CFG5);
966d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
967d68875ebSPyun YongHyeon 	    ALC_MII_DBG_DATA, data);
968d68875ebSPyun YongHyeon 
969d68875ebSPyun YongHyeon 	data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
970d68875ebSPyun YongHyeon 	    ANA_LONG_CABLE_TH_100_MASK) |
971d68875ebSPyun YongHyeon 	    ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
972d68875ebSPyun YongHyeon 	    ANA_SHORT_CABLE_TH_100_SHIFT) |
973d68875ebSPyun YongHyeon 	    ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
974d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
975d68875ebSPyun YongHyeon 	    ALC_MII_DBG_ADDR, MII_ANA_CFG54);
976d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
977d68875ebSPyun YongHyeon 	    ALC_MII_DBG_DATA, data);
978d68875ebSPyun YongHyeon 
979d68875ebSPyun YongHyeon 	data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
980d68875ebSPyun YongHyeon 	    ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
981d68875ebSPyun YongHyeon 	    ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
982d68875ebSPyun YongHyeon 	    ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
983d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
984d68875ebSPyun YongHyeon 	    ALC_MII_DBG_ADDR, MII_ANA_CFG4);
985d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
986d68875ebSPyun YongHyeon 	    ALC_MII_DBG_DATA, data);
987d68875ebSPyun YongHyeon 
988d68875ebSPyun YongHyeon 	data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
989d68875ebSPyun YongHyeon 	    ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
990d68875ebSPyun YongHyeon 	    ANA_OEN_125M;
991d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
992d68875ebSPyun YongHyeon 	    ALC_MII_DBG_ADDR, MII_ANA_CFG0);
993d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
994d68875ebSPyun YongHyeon 	    ALC_MII_DBG_DATA, data);
995d68875ebSPyun YongHyeon 	DELAY(1000);
996462d5251SPyun YongHyeon 
997462d5251SPyun YongHyeon 	/* Disable hibernation. */
998462d5251SPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
999462d5251SPyun YongHyeon 	    0x0029);
1000462d5251SPyun YongHyeon 	data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
1001462d5251SPyun YongHyeon 	    ALC_MII_DBG_DATA);
1002462d5251SPyun YongHyeon 	data &= ~0x8000;
1003462d5251SPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
1004462d5251SPyun YongHyeon 	    data);
1005462d5251SPyun YongHyeon 
1006462d5251SPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
1007462d5251SPyun YongHyeon 	    0x000B);
1008462d5251SPyun YongHyeon 	data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
1009462d5251SPyun YongHyeon 	    ALC_MII_DBG_DATA);
1010462d5251SPyun YongHyeon 	data &= ~0x8000;
1011462d5251SPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
1012462d5251SPyun YongHyeon 	    data);
1013d68875ebSPyun YongHyeon }
1014d68875ebSPyun YongHyeon 
1015d68875ebSPyun YongHyeon static void
alc_phy_reset_816x(struct alc_softc * sc)1016b624ef0aSPyun YongHyeon alc_phy_reset_816x(struct alc_softc *sc)
1017b624ef0aSPyun YongHyeon {
1018b624ef0aSPyun YongHyeon 	uint32_t val;
1019b624ef0aSPyun YongHyeon 
1020b624ef0aSPyun YongHyeon 	val = CSR_READ_4(sc, ALC_GPHY_CFG);
1021b624ef0aSPyun YongHyeon 	val &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
1022b624ef0aSPyun YongHyeon 	    GPHY_CFG_GATE_25M_ENB | GPHY_CFG_PHY_IDDQ | GPHY_CFG_PHY_PLL_ON |
1023b624ef0aSPyun YongHyeon 	    GPHY_CFG_PWDOWN_HW | GPHY_CFG_100AB_ENB);
1024b624ef0aSPyun YongHyeon 	val |= GPHY_CFG_SEL_ANA_RESET;
1025b624ef0aSPyun YongHyeon #ifdef notyet
1026b624ef0aSPyun YongHyeon 	val |= GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN | GPHY_CFG_SEL_ANA_RESET;
1027b624ef0aSPyun YongHyeon #else
1028b624ef0aSPyun YongHyeon 	/* Disable PHY hibernation. */
1029b624ef0aSPyun YongHyeon 	val &= ~(GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN);
1030b624ef0aSPyun YongHyeon #endif
1031b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_GPHY_CFG, val);
1032b624ef0aSPyun YongHyeon 	DELAY(10);
1033b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_GPHY_CFG, val | GPHY_CFG_EXT_RESET);
1034b624ef0aSPyun YongHyeon 	DELAY(800);
1035b624ef0aSPyun YongHyeon 
1036b624ef0aSPyun YongHyeon 	/* Vendor PHY magic. */
1037b624ef0aSPyun YongHyeon #ifdef notyet
1038b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_LEGCYPS, DBG_LEGCYPS_DEFAULT);
1039b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_SYSMODCTL, DBG_SYSMODCTL_DEFAULT);
1040b624ef0aSPyun YongHyeon 	alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_VDRVBIAS,
1041b624ef0aSPyun YongHyeon 	    EXT_VDRVBIAS_DEFAULT);
1042b624ef0aSPyun YongHyeon #else
1043b624ef0aSPyun YongHyeon 	/* Disable PHY hibernation. */
1044b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_LEGCYPS,
1045b624ef0aSPyun YongHyeon 	    DBG_LEGCYPS_DEFAULT & ~DBG_LEGCYPS_ENB);
1046b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_HIBNEG,
1047b624ef0aSPyun YongHyeon 	    DBG_HIBNEG_DEFAULT & ~(DBG_HIBNEG_PSHIB_EN | DBG_HIBNEG_HIB_PULSE));
1048b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_GREENCFG, DBG_GREENCFG_DEFAULT);
1049b624ef0aSPyun YongHyeon #endif
1050b624ef0aSPyun YongHyeon 
1051b624ef0aSPyun YongHyeon 	/* XXX Disable EEE. */
1052b624ef0aSPyun YongHyeon 	val = CSR_READ_4(sc, ALC_LPI_CTL);
1053b624ef0aSPyun YongHyeon 	val &= ~LPI_CTL_ENB;
1054b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_LPI_CTL, val);
1055b624ef0aSPyun YongHyeon 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_LOCAL_EEEADV, 0);
1056b624ef0aSPyun YongHyeon 
1057b624ef0aSPyun YongHyeon 	/* PHY power saving. */
1058b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_TST10BTCFG, DBG_TST10BTCFG_DEFAULT);
1059b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_SRDSYSMOD, DBG_SRDSYSMOD_DEFAULT);
1060b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_TST100BTCFG, DBG_TST100BTCFG_DEFAULT);
1061b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_ANACTL, DBG_ANACTL_DEFAULT);
1062b624ef0aSPyun YongHyeon 	val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
1063b624ef0aSPyun YongHyeon 	val &= ~DBG_GREENCFG2_GATE_DFSE_EN;
1064b624ef0aSPyun YongHyeon 	alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
1065b624ef0aSPyun YongHyeon 
1066b624ef0aSPyun YongHyeon 	/* RTL8139C, 120m issue. */
1067b624ef0aSPyun YongHyeon 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_NLP78,
1068b624ef0aSPyun YongHyeon 	    ANEG_NLP78_120M_DEFAULT);
1069b624ef0aSPyun YongHyeon 	alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
1070b624ef0aSPyun YongHyeon 	    ANEG_S3DIG10_DEFAULT);
1071b624ef0aSPyun YongHyeon 
1072b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0) {
1073b624ef0aSPyun YongHyeon 		/* Turn off half amplitude. */
1074b624ef0aSPyun YongHyeon 		val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3);
1075b624ef0aSPyun YongHyeon 		val |= EXT_CLDCTL3_BP_CABLE1TH_DET_GT;
1076b624ef0aSPyun YongHyeon 		alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3, val);
1077b624ef0aSPyun YongHyeon 		/* Turn off Green feature. */
1078b624ef0aSPyun YongHyeon 		val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
1079b624ef0aSPyun YongHyeon 		val |= DBG_GREENCFG2_BP_GREEN;
1080b624ef0aSPyun YongHyeon 		alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
1081b624ef0aSPyun YongHyeon 		/* Turn off half bias. */
1082b624ef0aSPyun YongHyeon 		val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5);
1083b624ef0aSPyun YongHyeon 		val |= EXT_CLDCTL5_BP_VD_HLFBIAS;
1084b624ef0aSPyun YongHyeon 		alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5, val);
1085b624ef0aSPyun YongHyeon 	}
1086b624ef0aSPyun YongHyeon }
1087b624ef0aSPyun YongHyeon 
1088b624ef0aSPyun YongHyeon static void
alc_phy_down(struct alc_softc * sc)1089d68875ebSPyun YongHyeon alc_phy_down(struct alc_softc *sc)
1090d68875ebSPyun YongHyeon {
1091b624ef0aSPyun YongHyeon 	uint32_t gphy;
1092d68875ebSPyun YongHyeon 
10932f70cceaSPyun YongHyeon 	switch (sc->alc_ident->deviceid) {
1094b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8161:
1095b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_E2200:
1096477cba21SPyun YongHyeon 	case DEVICEID_ATHEROS_E2400:
10971536a1b8SSepherosa Ziehau 	case DEVICEID_ATHEROS_E2500:
1098b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8162:
1099b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8171:
1100b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8172:
1101b624ef0aSPyun YongHyeon 		gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
1102b624ef0aSPyun YongHyeon 		gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
1103b624ef0aSPyun YongHyeon 		    GPHY_CFG_100AB_ENB | GPHY_CFG_PHY_PLL_ON);
1104b624ef0aSPyun YongHyeon 		gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE |
1105b624ef0aSPyun YongHyeon 		    GPHY_CFG_SEL_ANA_RESET;
1106b624ef0aSPyun YongHyeon 		gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
1107b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
1108b624ef0aSPyun YongHyeon 		break;
11092f70cceaSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8151:
11102f70cceaSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8151_V2:
1111b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8152_B:
1112b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8152_B2:
11132f70cceaSPyun YongHyeon 		/*
11142f70cceaSPyun YongHyeon 		 * GPHY power down caused more problems on AR8151 v2.0.
11152f70cceaSPyun YongHyeon 		 * When driver is reloaded after GPHY power down,
11162f70cceaSPyun YongHyeon 		 * accesses to PHY/MAC registers hung the system. Only
11172f70cceaSPyun YongHyeon 		 * cold boot recovered from it.  I'm not sure whether
11182f70cceaSPyun YongHyeon 		 * AR8151 v1.0 also requires this one though.  I don't
11192f70cceaSPyun YongHyeon 		 * have AR8151 v1.0 controller in hand.
11202f70cceaSPyun YongHyeon 		 * The only option left is to isolate the PHY and
11212f70cceaSPyun YongHyeon 		 * initiates power down the PHY which in turn saves
11222f70cceaSPyun YongHyeon 		 * more power when driver is unloaded.
11232f70cceaSPyun YongHyeon 		 */
11242f70cceaSPyun YongHyeon 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
11252f70cceaSPyun YongHyeon 		    MII_BMCR, BMCR_ISO | BMCR_PDOWN);
11262f70cceaSPyun YongHyeon 		break;
11272f70cceaSPyun YongHyeon 	default:
1128d68875ebSPyun YongHyeon 		/* Force PHY down. */
1129462d5251SPyun YongHyeon 		CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
11302f70cceaSPyun YongHyeon 		    GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
11312f70cceaSPyun YongHyeon 		    GPHY_CFG_PWDOWN_HW);
1132d68875ebSPyun YongHyeon 		DELAY(1000);
11332f70cceaSPyun YongHyeon 		break;
11342f70cceaSPyun YongHyeon 	}
1135d68875ebSPyun YongHyeon }
1136d68875ebSPyun YongHyeon 
1137d68875ebSPyun YongHyeon static void
alc_aspm(struct alc_softc * sc,int init,int media)1138b624ef0aSPyun YongHyeon alc_aspm(struct alc_softc *sc, int init, int media)
1139b624ef0aSPyun YongHyeon {
1140b624ef0aSPyun YongHyeon 
1141b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
1142b624ef0aSPyun YongHyeon 		alc_aspm_816x(sc, init);
1143b624ef0aSPyun YongHyeon 	else
1144b624ef0aSPyun YongHyeon 		alc_aspm_813x(sc, media);
1145b624ef0aSPyun YongHyeon }
1146b624ef0aSPyun YongHyeon 
1147b624ef0aSPyun YongHyeon static void
alc_aspm_813x(struct alc_softc * sc,int media)1148b624ef0aSPyun YongHyeon alc_aspm_813x(struct alc_softc *sc, int media)
1149d68875ebSPyun YongHyeon {
1150d68875ebSPyun YongHyeon 	uint32_t pmcfg;
11512f70cceaSPyun YongHyeon 	uint16_t linkcfg;
1152d68875ebSPyun YongHyeon 
1153b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) == 0)
1154b624ef0aSPyun YongHyeon 		return;
1155d68875ebSPyun YongHyeon 
1156d68875ebSPyun YongHyeon 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
11572f70cceaSPyun YongHyeon 	if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
11582f70cceaSPyun YongHyeon 	    (ALC_FLAG_APS | ALC_FLAG_PCIE))
11592f70cceaSPyun YongHyeon 		linkcfg = CSR_READ_2(sc, sc->alc_expcap +
1160389c8bd5SGavin Atkinson 		    PCIER_LINK_CTL);
11612f70cceaSPyun YongHyeon 	else
11622f70cceaSPyun YongHyeon 		linkcfg = 0;
1163d68875ebSPyun YongHyeon 	pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
11642f70cceaSPyun YongHyeon 	pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
1165d68875ebSPyun YongHyeon 	pmcfg |= PM_CFG_MAC_ASPM_CHK;
1166c27d7a76SPyun YongHyeon 	pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
11672f70cceaSPyun YongHyeon 	pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
11682f70cceaSPyun YongHyeon 
11692f70cceaSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
11702f70cceaSPyun YongHyeon 		/* Disable extended sync except AR8152 B v1.0 */
1171e935190aSGavin Atkinson 		linkcfg &= ~PCIEM_LINK_CTL_EXTENDED_SYNC;
11722f70cceaSPyun YongHyeon 		if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
11732f70cceaSPyun YongHyeon 		    sc->alc_rev == ATHEROS_AR8152_B_V10)
1174e935190aSGavin Atkinson 			linkcfg |= PCIEM_LINK_CTL_EXTENDED_SYNC;
1175389c8bd5SGavin Atkinson 		CSR_WRITE_2(sc, sc->alc_expcap + PCIER_LINK_CTL,
11762f70cceaSPyun YongHyeon 		    linkcfg);
11772f70cceaSPyun YongHyeon 		pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
11782f70cceaSPyun YongHyeon 		    PM_CFG_HOTRST);
11792f70cceaSPyun YongHyeon 		pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
11802f70cceaSPyun YongHyeon 		    PM_CFG_L1_ENTRY_TIMER_SHIFT);
11812f70cceaSPyun YongHyeon 		pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
11822f70cceaSPyun YongHyeon 		pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
11832f70cceaSPyun YongHyeon 		    PM_CFG_PM_REQ_TIMER_SHIFT);
11842f70cceaSPyun YongHyeon 		pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
11852f70cceaSPyun YongHyeon 	}
11862f70cceaSPyun YongHyeon 
1187d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
11882f70cceaSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
11892f70cceaSPyun YongHyeon 			pmcfg |= PM_CFG_ASPM_L0S_ENB;
11902f70cceaSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
11912f70cceaSPyun YongHyeon 			pmcfg |= PM_CFG_ASPM_L1_ENB;
11922f70cceaSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
11932f70cceaSPyun YongHyeon 			if (sc->alc_ident->deviceid ==
11942f70cceaSPyun YongHyeon 			    DEVICEID_ATHEROS_AR8152_B)
1195d68875ebSPyun YongHyeon 				pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
11962f70cceaSPyun YongHyeon 			pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
11972f70cceaSPyun YongHyeon 			    PM_CFG_SERDES_PLL_L1_ENB |
11982f70cceaSPyun YongHyeon 			    PM_CFG_SERDES_BUDS_RX_L1_ENB);
1199d68875ebSPyun YongHyeon 			pmcfg |= PM_CFG_CLK_SWH_L1;
12002f70cceaSPyun YongHyeon 			if (media == IFM_100_TX || media == IFM_1000_T) {
12012f70cceaSPyun YongHyeon 				pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
12022f70cceaSPyun YongHyeon 				switch (sc->alc_ident->deviceid) {
12032f70cceaSPyun YongHyeon 				case DEVICEID_ATHEROS_AR8152_B:
12042f70cceaSPyun YongHyeon 					pmcfg |= (7 <<
12052f70cceaSPyun YongHyeon 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
12062f70cceaSPyun YongHyeon 					break;
12072f70cceaSPyun YongHyeon 				case DEVICEID_ATHEROS_AR8152_B2:
12082f70cceaSPyun YongHyeon 				case DEVICEID_ATHEROS_AR8151_V2:
12092f70cceaSPyun YongHyeon 					pmcfg |= (4 <<
12102f70cceaSPyun YongHyeon 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
12112f70cceaSPyun YongHyeon 					break;
12122f70cceaSPyun YongHyeon 				default:
12132f70cceaSPyun YongHyeon 					pmcfg |= (15 <<
12142f70cceaSPyun YongHyeon 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
12152f70cceaSPyun YongHyeon 					break;
12162f70cceaSPyun YongHyeon 				}
12172f70cceaSPyun YongHyeon 			}
12182f70cceaSPyun YongHyeon 		} else {
12192f70cceaSPyun YongHyeon 			pmcfg |= PM_CFG_SERDES_L1_ENB |
12202f70cceaSPyun YongHyeon 			    PM_CFG_SERDES_PLL_L1_ENB |
12212f70cceaSPyun YongHyeon 			    PM_CFG_SERDES_BUDS_RX_L1_ENB;
12222f70cceaSPyun YongHyeon 			pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
12232f70cceaSPyun YongHyeon 			    PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
12242f70cceaSPyun YongHyeon 		}
12252f70cceaSPyun YongHyeon 	} else {
12262f70cceaSPyun YongHyeon 		pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
12272f70cceaSPyun YongHyeon 		    PM_CFG_SERDES_PLL_L1_ENB);
12282f70cceaSPyun YongHyeon 		pmcfg |= PM_CFG_CLK_SWH_L1;
12292f70cceaSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
12302f70cceaSPyun YongHyeon 			pmcfg |= PM_CFG_ASPM_L1_ENB;
1231d68875ebSPyun YongHyeon 	}
1232d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1233d68875ebSPyun YongHyeon }
1234d68875ebSPyun YongHyeon 
1235b624ef0aSPyun YongHyeon static void
alc_aspm_816x(struct alc_softc * sc,int init)1236b624ef0aSPyun YongHyeon alc_aspm_816x(struct alc_softc *sc, int init)
1237b624ef0aSPyun YongHyeon {
1238b624ef0aSPyun YongHyeon 	uint32_t pmcfg;
1239b624ef0aSPyun YongHyeon 
1240b624ef0aSPyun YongHyeon 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
1241b624ef0aSPyun YongHyeon 	pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_816X_MASK;
1242b624ef0aSPyun YongHyeon 	pmcfg |= PM_CFG_L1_ENTRY_TIMER_816X_DEFAULT;
1243b624ef0aSPyun YongHyeon 	pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
1244b624ef0aSPyun YongHyeon 	pmcfg |= PM_CFG_PM_REQ_TIMER_816X_DEFAULT;
1245b624ef0aSPyun YongHyeon 	pmcfg &= ~PM_CFG_LCKDET_TIMER_MASK;
1246b624ef0aSPyun YongHyeon 	pmcfg |= PM_CFG_LCKDET_TIMER_DEFAULT;
1247b624ef0aSPyun YongHyeon 	pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_CLK_SWH_L1 | PM_CFG_PCIE_RECV;
1248b624ef0aSPyun YongHyeon 	pmcfg &= ~(PM_CFG_RX_L1_AFTER_L0S | PM_CFG_TX_L1_AFTER_L0S |
1249b624ef0aSPyun YongHyeon 	    PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB |
1250b624ef0aSPyun YongHyeon 	    PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
1251b624ef0aSPyun YongHyeon 	    PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SA_DLY_ENB |
1252b624ef0aSPyun YongHyeon 	    PM_CFG_MAC_ASPM_CHK | PM_CFG_HOTRST);
1253b624ef0aSPyun YongHyeon 	if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1254b624ef0aSPyun YongHyeon 	    (sc->alc_rev & 0x01) != 0)
1255b624ef0aSPyun YongHyeon 		pmcfg |= PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB;
1256b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
1257b624ef0aSPyun YongHyeon 		/* Link up, enable both L0s, L1s. */
1258b624ef0aSPyun YongHyeon 		pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1259b624ef0aSPyun YongHyeon 		    PM_CFG_MAC_ASPM_CHK;
1260b624ef0aSPyun YongHyeon 	} else {
1261b624ef0aSPyun YongHyeon 		if (init != 0)
1262b624ef0aSPyun YongHyeon 			pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1263b624ef0aSPyun YongHyeon 			    PM_CFG_MAC_ASPM_CHK;
126452436412SJustin Hibbits 		else if ((if_getdrvflags(sc->alc_ifp) & IFF_DRV_RUNNING) != 0)
1265b624ef0aSPyun YongHyeon 			pmcfg |= PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK;
1266b624ef0aSPyun YongHyeon 	}
1267b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1268b624ef0aSPyun YongHyeon }
1269b624ef0aSPyun YongHyeon 
1270b624ef0aSPyun YongHyeon static void
alc_init_pcie(struct alc_softc * sc)1271b624ef0aSPyun YongHyeon alc_init_pcie(struct alc_softc *sc)
1272b624ef0aSPyun YongHyeon {
1273b624ef0aSPyun YongHyeon 	const char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
1274b624ef0aSPyun YongHyeon 	uint32_t cap, ctl, val;
1275b624ef0aSPyun YongHyeon 	int state;
1276b624ef0aSPyun YongHyeon 
1277b624ef0aSPyun YongHyeon 	/* Clear data link and flow-control protocol error. */
1278b624ef0aSPyun YongHyeon 	val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
1279b624ef0aSPyun YongHyeon 	val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
1280b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
1281b624ef0aSPyun YongHyeon 
1282b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1283b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
1284b624ef0aSPyun YongHyeon 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
1285b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
1286b624ef0aSPyun YongHyeon 		    CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
1287b624ef0aSPyun YongHyeon 		    PCIE_PHYMISC_FORCE_RCV_DET);
1288b624ef0aSPyun YongHyeon 		if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
1289b624ef0aSPyun YongHyeon 		    sc->alc_rev == ATHEROS_AR8152_B_V10) {
1290b624ef0aSPyun YongHyeon 			val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
1291b624ef0aSPyun YongHyeon 			val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
1292b624ef0aSPyun YongHyeon 			    PCIE_PHYMISC2_SERDES_TH_MASK);
1293b624ef0aSPyun YongHyeon 			val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
1294b624ef0aSPyun YongHyeon 			val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
1295b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
1296b624ef0aSPyun YongHyeon 		}
1297b624ef0aSPyun YongHyeon 		/* Disable ASPM L0S and L1. */
1298b624ef0aSPyun YongHyeon 		cap = CSR_READ_2(sc, sc->alc_expcap + PCIER_LINK_CAP);
1299b624ef0aSPyun YongHyeon 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1300b624ef0aSPyun YongHyeon 			ctl = CSR_READ_2(sc, sc->alc_expcap + PCIER_LINK_CTL);
1301b624ef0aSPyun YongHyeon 			if ((ctl & PCIEM_LINK_CTL_RCB) != 0)
1302b624ef0aSPyun YongHyeon 				sc->alc_rcb = DMA_CFG_RCB_128;
1303b624ef0aSPyun YongHyeon 			if (bootverbose)
1304b624ef0aSPyun YongHyeon 				device_printf(sc->alc_dev, "RCB %u bytes\n",
1305b624ef0aSPyun YongHyeon 				    sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
1306b624ef0aSPyun YongHyeon 			state = ctl & PCIEM_LINK_CTL_ASPMC;
1307b624ef0aSPyun YongHyeon 			if (state & PCIEM_LINK_CTL_ASPMC_L0S)
1308b624ef0aSPyun YongHyeon 				sc->alc_flags |= ALC_FLAG_L0S;
1309b624ef0aSPyun YongHyeon 			if (state & PCIEM_LINK_CTL_ASPMC_L1)
1310b624ef0aSPyun YongHyeon 				sc->alc_flags |= ALC_FLAG_L1S;
1311b624ef0aSPyun YongHyeon 			if (bootverbose)
1312b624ef0aSPyun YongHyeon 				device_printf(sc->alc_dev, "ASPM %s %s\n",
1313b624ef0aSPyun YongHyeon 				    aspm_state[state],
1314b624ef0aSPyun YongHyeon 				    state == 0 ? "disabled" : "enabled");
1315b624ef0aSPyun YongHyeon 			alc_disable_l0s_l1(sc);
1316b624ef0aSPyun YongHyeon 		} else {
1317b624ef0aSPyun YongHyeon 			if (bootverbose)
1318b624ef0aSPyun YongHyeon 				device_printf(sc->alc_dev,
1319b624ef0aSPyun YongHyeon 				    "no ASPM support\n");
1320b624ef0aSPyun YongHyeon 		}
1321b624ef0aSPyun YongHyeon 	} else {
1322b624ef0aSPyun YongHyeon 		val = CSR_READ_4(sc, ALC_PDLL_TRNS1);
1323b624ef0aSPyun YongHyeon 		val &= ~PDLL_TRNS1_D3PLLOFF_ENB;
1324b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_PDLL_TRNS1, val);
1325b624ef0aSPyun YongHyeon 		val = CSR_READ_4(sc, ALC_MASTER_CFG);
1326b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1327b624ef0aSPyun YongHyeon 		    (sc->alc_rev & 0x01) != 0) {
1328b624ef0aSPyun YongHyeon 			if ((val & MASTER_WAKEN_25M) == 0 ||
1329b624ef0aSPyun YongHyeon 			    (val & MASTER_CLK_SEL_DIS) == 0) {
1330b624ef0aSPyun YongHyeon 				val |= MASTER_WAKEN_25M | MASTER_CLK_SEL_DIS;
1331b624ef0aSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1332b624ef0aSPyun YongHyeon 			}
1333b624ef0aSPyun YongHyeon 		} else {
1334b624ef0aSPyun YongHyeon 			if ((val & MASTER_WAKEN_25M) == 0 ||
1335b624ef0aSPyun YongHyeon 			    (val & MASTER_CLK_SEL_DIS) != 0) {
1336b624ef0aSPyun YongHyeon 				val |= MASTER_WAKEN_25M;
1337b624ef0aSPyun YongHyeon 				val &= ~MASTER_CLK_SEL_DIS;
1338b624ef0aSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1339b624ef0aSPyun YongHyeon 			}
1340b624ef0aSPyun YongHyeon 		}
1341b624ef0aSPyun YongHyeon 	}
1342b624ef0aSPyun YongHyeon 	alc_aspm(sc, 1, IFM_UNKNOWN);
1343b624ef0aSPyun YongHyeon }
1344b624ef0aSPyun YongHyeon 
1345b624ef0aSPyun YongHyeon static void
alc_config_msi(struct alc_softc * sc)1346b624ef0aSPyun YongHyeon alc_config_msi(struct alc_softc *sc)
1347b624ef0aSPyun YongHyeon {
1348b624ef0aSPyun YongHyeon 	uint32_t ctl, mod;
1349b624ef0aSPyun YongHyeon 
1350b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
1351b624ef0aSPyun YongHyeon 		/*
1352b624ef0aSPyun YongHyeon 		 * It seems interrupt moderation is controlled by
1353b624ef0aSPyun YongHyeon 		 * ALC_MSI_RETRANS_TIMER register if MSI/MSIX is active.
1354b624ef0aSPyun YongHyeon 		 * Driver uses RX interrupt moderation parameter to
1355b624ef0aSPyun YongHyeon 		 * program ALC_MSI_RETRANS_TIMER register.
1356b624ef0aSPyun YongHyeon 		 */
1357b624ef0aSPyun YongHyeon 		ctl = CSR_READ_4(sc, ALC_MSI_RETRANS_TIMER);
1358b624ef0aSPyun YongHyeon 		ctl &= ~MSI_RETRANS_TIMER_MASK;
1359b624ef0aSPyun YongHyeon 		ctl &= ~MSI_RETRANS_MASK_SEL_LINE;
1360b624ef0aSPyun YongHyeon 		mod = ALC_USECS(sc->alc_int_rx_mod);
1361b624ef0aSPyun YongHyeon 		if (mod == 0)
1362b624ef0aSPyun YongHyeon 			mod = 1;
1363b624ef0aSPyun YongHyeon 		ctl |= mod;
1364b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1365b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, ctl |
1366b624ef0aSPyun YongHyeon 			    MSI_RETRANS_MASK_SEL_STD);
1367b624ef0aSPyun YongHyeon 		else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1368b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, ctl |
1369b624ef0aSPyun YongHyeon 			    MSI_RETRANS_MASK_SEL_LINE);
1370b624ef0aSPyun YongHyeon 		else
1371b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, 0);
1372b624ef0aSPyun YongHyeon 	}
1373b624ef0aSPyun YongHyeon }
1374b624ef0aSPyun YongHyeon 
1375d68875ebSPyun YongHyeon static int
alc_attach(device_t dev)1376d68875ebSPyun YongHyeon alc_attach(device_t dev)
1377d68875ebSPyun YongHyeon {
1378d68875ebSPyun YongHyeon 	struct alc_softc *sc;
137952436412SJustin Hibbits 	if_t ifp;
1380b624ef0aSPyun YongHyeon 	int base, error, i, msic, msixc;
1381d68875ebSPyun YongHyeon 	uint16_t burst;
1382d68875ebSPyun YongHyeon 
1383d68875ebSPyun YongHyeon 	error = 0;
1384d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
1385d68875ebSPyun YongHyeon 	sc->alc_dev = dev;
1386b624ef0aSPyun YongHyeon 	sc->alc_rev = pci_get_revid(dev);
1387d68875ebSPyun YongHyeon 
1388d68875ebSPyun YongHyeon 	mtx_init(&sc->alc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1389d68875ebSPyun YongHyeon 	    MTX_DEF);
1390d68875ebSPyun YongHyeon 	callout_init_mtx(&sc->alc_tick_ch, &sc->alc_mtx, 0);
13916c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->alc_int_task, 0, alc_int_task, sc);
13922f70cceaSPyun YongHyeon 	sc->alc_ident = alc_find_ident(dev);
1393d68875ebSPyun YongHyeon 
1394d68875ebSPyun YongHyeon 	/* Map the device. */
1395d68875ebSPyun YongHyeon 	pci_enable_busmaster(dev);
1396d68875ebSPyun YongHyeon 	sc->alc_res_spec = alc_res_spec_mem;
1397d68875ebSPyun YongHyeon 	sc->alc_irq_spec = alc_irq_spec_legacy;
1398d68875ebSPyun YongHyeon 	error = bus_alloc_resources(dev, sc->alc_res_spec, sc->alc_res);
1399d68875ebSPyun YongHyeon 	if (error != 0) {
1400d68875ebSPyun YongHyeon 		device_printf(dev, "cannot allocate memory resources.\n");
1401d68875ebSPyun YongHyeon 		goto fail;
1402d68875ebSPyun YongHyeon 	}
1403d68875ebSPyun YongHyeon 
1404d68875ebSPyun YongHyeon 	/* Set PHY address. */
1405d68875ebSPyun YongHyeon 	sc->alc_phyaddr = ALC_PHY_ADDR;
1406d68875ebSPyun YongHyeon 
1407b624ef0aSPyun YongHyeon 	/*
1408b624ef0aSPyun YongHyeon 	 * One odd thing is AR8132 uses the same PHY hardware(F1
1409b624ef0aSPyun YongHyeon 	 * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
1410b624ef0aSPyun YongHyeon 	 * the PHY supports 1000Mbps but that's not true. The PHY
1411b624ef0aSPyun YongHyeon 	 * used in AR8132 can't establish gigabit link even if it
1412b624ef0aSPyun YongHyeon 	 * shows the same PHY model/revision number of AR8131.
1413b624ef0aSPyun YongHyeon 	 */
1414b624ef0aSPyun YongHyeon 	switch (sc->alc_ident->deviceid) {
1415477cba21SPyun YongHyeon 	case DEVICEID_ATHEROS_E2200:
1416477cba21SPyun YongHyeon 	case DEVICEID_ATHEROS_E2400:
14171536a1b8SSepherosa Ziehau 	case DEVICEID_ATHEROS_E2500:
1418477cba21SPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_E2X00;
141905a95d19SLexi Winter 
142005a95d19SLexi Winter 		/*
142105a95d19SLexi Winter 		 * Disable MSI-X by default on Killer devices, since this is
142205a95d19SLexi Winter 		 * reported by several users to not work well.
142305a95d19SLexi Winter 		 */
142405a95d19SLexi Winter 		if (msix_disable == 2)
142505a95d19SLexi Winter 			msix_disable = 1;
142605a95d19SLexi Winter 
1427477cba21SPyun YongHyeon 		/* FALLTHROUGH */
1428b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8161:
1429b624ef0aSPyun YongHyeon 		if (pci_get_subvendor(dev) == VENDORID_ATHEROS &&
1430b624ef0aSPyun YongHyeon 		    pci_get_subdevice(dev) == 0x0091 && sc->alc_rev == 0)
1431b624ef0aSPyun YongHyeon 			sc->alc_flags |= ALC_FLAG_LINK_WAR;
1432b624ef0aSPyun YongHyeon 		/* FALLTHROUGH */
1433b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8171:
1434b624ef0aSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_AR816X_FAMILY;
1435b624ef0aSPyun YongHyeon 		break;
1436b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8162:
1437b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8172:
1438b624ef0aSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_FASTETHER | ALC_FLAG_AR816X_FAMILY;
1439b624ef0aSPyun YongHyeon 		break;
1440b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8152_B:
1441b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8152_B2:
1442b624ef0aSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_APS;
1443b624ef0aSPyun YongHyeon 		/* FALLTHROUGH */
1444b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8132:
1445b624ef0aSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_FASTETHER;
1446b624ef0aSPyun YongHyeon 		break;
1447b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8151:
1448b624ef0aSPyun YongHyeon 	case DEVICEID_ATHEROS_AR8151_V2:
1449b624ef0aSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_APS;
145077b63733SKonstantin Belousov 		if (CSR_READ_4(sc, ALC_MT_MAGIC) == MT_MAGIC)
145177b63733SKonstantin Belousov 			sc->alc_flags |= ALC_FLAG_MT;
1452b624ef0aSPyun YongHyeon 		/* FALLTHROUGH */
1453b624ef0aSPyun YongHyeon 	default:
1454b624ef0aSPyun YongHyeon 		break;
1455b624ef0aSPyun YongHyeon 	}
145605a95d19SLexi Winter 
145705a95d19SLexi Winter 	/*
145805a95d19SLexi Winter 	 * The default value of msix_disable is 2, which means auto-detect.  If
145905a95d19SLexi Winter 	 * we didn't auto-detect it, default to enabling it.
146005a95d19SLexi Winter 	 */
146105a95d19SLexi Winter 	if (msix_disable == 2)
146205a95d19SLexi Winter 		msix_disable = 0;
146305a95d19SLexi Winter 
1464b624ef0aSPyun YongHyeon 	sc->alc_flags |= ALC_FLAG_JUMBO;
1465b624ef0aSPyun YongHyeon 
1466b624ef0aSPyun YongHyeon 	/*
1467b624ef0aSPyun YongHyeon 	 * It seems that AR813x/AR815x has silicon bug for SMB. In
1468b624ef0aSPyun YongHyeon 	 * addition, Atheros said that enabling SMB wouldn't improve
1469b624ef0aSPyun YongHyeon 	 * performance. However I think it's bad to access lots of
1470b624ef0aSPyun YongHyeon 	 * registers to extract MAC statistics.
1471b624ef0aSPyun YongHyeon 	 */
1472b624ef0aSPyun YongHyeon 	sc->alc_flags |= ALC_FLAG_SMB_BUG;
1473b624ef0aSPyun YongHyeon 	/*
1474b624ef0aSPyun YongHyeon 	 * Don't use Tx CMB. It is known to have silicon bug.
1475b624ef0aSPyun YongHyeon 	 */
1476b624ef0aSPyun YongHyeon 	sc->alc_flags |= ALC_FLAG_CMB_BUG;
1477b624ef0aSPyun YongHyeon 	sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
1478b624ef0aSPyun YongHyeon 	    MASTER_CHIP_REV_SHIFT;
1479b624ef0aSPyun YongHyeon 	if (bootverbose) {
1480b624ef0aSPyun YongHyeon 		device_printf(dev, "PCI device revision : 0x%04x\n",
1481b624ef0aSPyun YongHyeon 		    sc->alc_rev);
1482b624ef0aSPyun YongHyeon 		device_printf(dev, "Chip id/revision : 0x%04x\n",
1483b624ef0aSPyun YongHyeon 		    sc->alc_chip_rev);
1484b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
1485b624ef0aSPyun YongHyeon 			device_printf(dev, "AR816x revision : 0x%x\n",
1486b624ef0aSPyun YongHyeon 			    AR816X_REV(sc->alc_rev));
1487b624ef0aSPyun YongHyeon 	}
1488b624ef0aSPyun YongHyeon 	device_printf(dev, "%u Tx FIFO, %u Rx FIFO\n",
1489b624ef0aSPyun YongHyeon 	    CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
1490b624ef0aSPyun YongHyeon 	    CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
1491b624ef0aSPyun YongHyeon 
1492d68875ebSPyun YongHyeon 	/* Initialize DMA parameters. */
1493d68875ebSPyun YongHyeon 	sc->alc_dma_rd_burst = 0;
1494d68875ebSPyun YongHyeon 	sc->alc_dma_wr_burst = 0;
1495d68875ebSPyun YongHyeon 	sc->alc_rcb = DMA_CFG_RCB_64;
14963b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &base) == 0) {
1497d68875ebSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_PCIE;
1498a4d3574cSPyun YongHyeon 		sc->alc_expcap = base;
1499389c8bd5SGavin Atkinson 		burst = CSR_READ_2(sc, base + PCIER_DEVICE_CTL);
1500d68875ebSPyun YongHyeon 		sc->alc_dma_rd_burst =
1501389c8bd5SGavin Atkinson 		    (burst & PCIEM_CTL_MAX_READ_REQUEST) >> 12;
1502389c8bd5SGavin Atkinson 		sc->alc_dma_wr_burst = (burst & PCIEM_CTL_MAX_PAYLOAD) >> 5;
1503d68875ebSPyun YongHyeon 		if (bootverbose) {
1504d68875ebSPyun YongHyeon 			device_printf(dev, "Read request size : %u bytes.\n",
1505d68875ebSPyun YongHyeon 			    alc_dma_burst[sc->alc_dma_rd_burst]);
1506d68875ebSPyun YongHyeon 			device_printf(dev, "TLP payload size : %u bytes.\n",
1507d68875ebSPyun YongHyeon 			    alc_dma_burst[sc->alc_dma_wr_burst]);
1508d68875ebSPyun YongHyeon 		}
15091e77baedSPyun YongHyeon 		if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
15101e77baedSPyun YongHyeon 			sc->alc_dma_rd_burst = 3;
15111e77baedSPyun YongHyeon 		if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
15121e77baedSPyun YongHyeon 			sc->alc_dma_wr_burst = 3;
1513477cba21SPyun YongHyeon 		/*
15141536a1b8SSepherosa Ziehau 		 * Force maximum payload size to 128 bytes for
15158cdb6b2dSKyle Evans 		 * E2200/E2400/E2500/AR8162/AR8171/AR8172.
1516477cba21SPyun YongHyeon 		 * Otherwise it triggers DMA write error.
1517477cba21SPyun YongHyeon 		 */
15188cdb6b2dSKyle Evans 		if ((sc->alc_flags &
15198cdb6b2dSKyle Evans 		    (ALC_FLAG_E2X00 | ALC_FLAG_AR816X_FAMILY)) != 0)
1520477cba21SPyun YongHyeon 			sc->alc_dma_wr_burst = 0;
1521b624ef0aSPyun YongHyeon 		alc_init_pcie(sc);
1522d68875ebSPyun YongHyeon 	}
1523d68875ebSPyun YongHyeon 
1524d68875ebSPyun YongHyeon 	/* Reset PHY. */
1525d68875ebSPyun YongHyeon 	alc_phy_reset(sc);
1526d68875ebSPyun YongHyeon 
1527d68875ebSPyun YongHyeon 	/* Reset the ethernet controller. */
1528b624ef0aSPyun YongHyeon 	alc_stop_mac(sc);
1529d68875ebSPyun YongHyeon 	alc_reset(sc);
1530d68875ebSPyun YongHyeon 
1531d68875ebSPyun YongHyeon 	/* Allocate IRQ resources. */
1532d68875ebSPyun YongHyeon 	msixc = pci_msix_count(dev);
1533d68875ebSPyun YongHyeon 	msic = pci_msi_count(dev);
1534d68875ebSPyun YongHyeon 	if (bootverbose) {
1535d68875ebSPyun YongHyeon 		device_printf(dev, "MSIX count : %d\n", msixc);
1536d68875ebSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
1537d68875ebSPyun YongHyeon 	}
1538b624ef0aSPyun YongHyeon 	if (msixc > 1)
1539b624ef0aSPyun YongHyeon 		msixc = 1;
1540b624ef0aSPyun YongHyeon 	if (msic > 1)
1541b624ef0aSPyun YongHyeon 		msic = 1;
1542b624ef0aSPyun YongHyeon 	/*
1543b624ef0aSPyun YongHyeon 	 * Prefer MSIX over MSI.
1544b624ef0aSPyun YongHyeon 	 * AR816x controller has a silicon bug that MSI interrupt
1545b624ef0aSPyun YongHyeon 	 * does not assert if PCIM_CMD_INTxDIS bit of command
1546b624ef0aSPyun YongHyeon 	 * register is set.  pci(4) was taught to handle that case.
1547b624ef0aSPyun YongHyeon 	 */
1548d68875ebSPyun YongHyeon 	if (msix_disable == 0 || msi_disable == 0) {
1549b624ef0aSPyun YongHyeon 		if (msix_disable == 0 && msixc > 0 &&
1550d68875ebSPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
1551b624ef0aSPyun YongHyeon 			if (msic == 1) {
1552d68875ebSPyun YongHyeon 				device_printf(dev,
1553d68875ebSPyun YongHyeon 				    "Using %d MSIX message(s).\n", msixc);
1554d68875ebSPyun YongHyeon 				sc->alc_flags |= ALC_FLAG_MSIX;
1555d68875ebSPyun YongHyeon 				sc->alc_irq_spec = alc_irq_spec_msix;
1556d68875ebSPyun YongHyeon 			} else
1557d68875ebSPyun YongHyeon 				pci_release_msi(dev);
1558d68875ebSPyun YongHyeon 		}
1559d68875ebSPyun YongHyeon 		if (msi_disable == 0 && (sc->alc_flags & ALC_FLAG_MSIX) == 0 &&
1560b624ef0aSPyun YongHyeon 		    msic > 0 && pci_alloc_msi(dev, &msic) == 0) {
1561b624ef0aSPyun YongHyeon 			if (msic == 1) {
1562d68875ebSPyun YongHyeon 				device_printf(dev,
1563d68875ebSPyun YongHyeon 				    "Using %d MSI message(s).\n", msic);
1564d68875ebSPyun YongHyeon 				sc->alc_flags |= ALC_FLAG_MSI;
1565d68875ebSPyun YongHyeon 				sc->alc_irq_spec = alc_irq_spec_msi;
1566d68875ebSPyun YongHyeon 			} else
1567d68875ebSPyun YongHyeon 				pci_release_msi(dev);
1568d68875ebSPyun YongHyeon 		}
1569d68875ebSPyun YongHyeon 	}
1570d68875ebSPyun YongHyeon 
1571d68875ebSPyun YongHyeon 	error = bus_alloc_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1572d68875ebSPyun YongHyeon 	if (error != 0) {
1573d68875ebSPyun YongHyeon 		device_printf(dev, "cannot allocate IRQ resources.\n");
1574d68875ebSPyun YongHyeon 		goto fail;
1575d68875ebSPyun YongHyeon 	}
1576d68875ebSPyun YongHyeon 
1577d68875ebSPyun YongHyeon 	/* Create device sysctl node. */
1578d68875ebSPyun YongHyeon 	alc_sysctl_node(sc);
1579d68875ebSPyun YongHyeon 
15809dda5c8fSPyun YongHyeon 	if ((error = alc_dma_alloc(sc)) != 0)
1581d68875ebSPyun YongHyeon 		goto fail;
1582d68875ebSPyun YongHyeon 
1583d68875ebSPyun YongHyeon 	/* Load station address. */
1584d68875ebSPyun YongHyeon 	alc_get_macaddr(sc);
1585d68875ebSPyun YongHyeon 
1586d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp = if_alloc(IFT_ETHER);
158752436412SJustin Hibbits 	if_setsoftc(ifp, sc);
1588d68875ebSPyun YongHyeon 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
158952436412SJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
159052436412SJustin Hibbits 	if_setioctlfn(ifp, alc_ioctl);
159152436412SJustin Hibbits 	if_setstartfn(ifp, alc_start);
159252436412SJustin Hibbits 	if_setinitfn(ifp, alc_init);
159352436412SJustin Hibbits 	if_setsendqlen(ifp, ALC_TX_RING_CNT - 1);
159452436412SJustin Hibbits 	if_setsendqready(ifp);
159552436412SJustin Hibbits 	if_setcapabilities(ifp, IFCAP_TXCSUM | IFCAP_TSO4);
159652436412SJustin Hibbits 	if_sethwassist(ifp, ALC_CSUM_FEATURES | CSUM_TSO);
1597*ddaf6524SJohn Baldwin 	if (pci_has_pm(dev)) {
159852436412SJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST, 0);
1599a4d3574cSPyun YongHyeon 		sc->alc_flags |= ALC_FLAG_PM;
1600a4d3574cSPyun YongHyeon 	}
160152436412SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
1602d68875ebSPyun YongHyeon 
1603d68875ebSPyun YongHyeon 	/* Set up MII bus. */
16048e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->alc_miibus, ifp, alc_mediachange,
16058e5d93dbSMarius Strobl 	    alc_mediastatus, BMSR_DEFCAPMASK, sc->alc_phyaddr, MII_OFFSET_ANY,
16069f4e8f46SPyun YongHyeon 	    MIIF_DOPAUSE);
16078e5d93dbSMarius Strobl 	if (error != 0) {
16088e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
1609d68875ebSPyun YongHyeon 		goto fail;
1610d68875ebSPyun YongHyeon 	}
1611d68875ebSPyun YongHyeon 
1612d68875ebSPyun YongHyeon 	ether_ifattach(ifp, sc->alc_eaddr);
1613d68875ebSPyun YongHyeon 
1614d68875ebSPyun YongHyeon 	/* VLAN capability setup. */
161552436412SJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
161652436412SJustin Hibbits 	    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO, 0);
161752436412SJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
1618d68875ebSPyun YongHyeon 	/*
1619d68875ebSPyun YongHyeon 	 * XXX
1620d68875ebSPyun YongHyeon 	 * It seems enabling Tx checksum offloading makes more trouble.
1621d68875ebSPyun YongHyeon 	 * Sometimes the controller does not receive any frames when
1622d68875ebSPyun YongHyeon 	 * Tx checksum offloading is enabled. I'm not sure whether this
1623d68875ebSPyun YongHyeon 	 * is a bug in Tx checksum offloading logic or I got broken
1624d68875ebSPyun YongHyeon 	 * sample boards. To safety, don't enable Tx checksum offloading
1625d68875ebSPyun YongHyeon 	 * by default but give chance to users to toggle it if they know
1626d68875ebSPyun YongHyeon 	 * their controllers work without problems.
1627b624ef0aSPyun YongHyeon 	 * Fortunately, Tx checksum offloading for AR816x family
1628b624ef0aSPyun YongHyeon 	 * seems to work.
1629d68875ebSPyun YongHyeon 	 */
1630b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
163152436412SJustin Hibbits 		if_setcapenablebit(ifp, 0, IFCAP_TXCSUM);
163252436412SJustin Hibbits 		if_sethwassistbits(ifp, 0, ALC_CSUM_FEATURES);
1633b624ef0aSPyun YongHyeon 	}
1634d68875ebSPyun YongHyeon 
1635d68875ebSPyun YongHyeon 	/* Tell the upper layer(s) we support long frames. */
163652436412SJustin Hibbits 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
1637d68875ebSPyun YongHyeon 
1638d68875ebSPyun YongHyeon 	/* Create local taskq. */
1639d68875ebSPyun YongHyeon 	sc->alc_tq = taskqueue_create_fast("alc_taskq", M_WAITOK,
1640d68875ebSPyun YongHyeon 	    taskqueue_thread_enqueue, &sc->alc_tq);
1641d68875ebSPyun YongHyeon 	taskqueue_start_threads(&sc->alc_tq, 1, PI_NET, "%s taskq",
1642d68875ebSPyun YongHyeon 	    device_get_nameunit(sc->alc_dev));
1643d68875ebSPyun YongHyeon 
1644b624ef0aSPyun YongHyeon 	alc_config_msi(sc);
1645d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1646d68875ebSPyun YongHyeon 		msic = ALC_MSIX_MESSAGES;
1647d68875ebSPyun YongHyeon 	else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1648d68875ebSPyun YongHyeon 		msic = ALC_MSI_MESSAGES;
1649d68875ebSPyun YongHyeon 	else
1650d68875ebSPyun YongHyeon 		msic = 1;
1651d68875ebSPyun YongHyeon 	for (i = 0; i < msic; i++) {
1652d68875ebSPyun YongHyeon 		error = bus_setup_intr(dev, sc->alc_irq[i],
1653d68875ebSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, alc_intr, NULL, sc,
1654d68875ebSPyun YongHyeon 		    &sc->alc_intrhand[i]);
1655d68875ebSPyun YongHyeon 		if (error != 0)
1656d68875ebSPyun YongHyeon 			break;
1657d68875ebSPyun YongHyeon 	}
1658d68875ebSPyun YongHyeon 	if (error != 0) {
1659d68875ebSPyun YongHyeon 		device_printf(dev, "could not set up interrupt handler.\n");
1660d68875ebSPyun YongHyeon 		taskqueue_free(sc->alc_tq);
1661d68875ebSPyun YongHyeon 		sc->alc_tq = NULL;
1662d68875ebSPyun YongHyeon 		ether_ifdetach(ifp);
1663d68875ebSPyun YongHyeon 		goto fail;
1664d68875ebSPyun YongHyeon 	}
1665d68875ebSPyun YongHyeon 
16667790c8c1SConrad Meyer 	/* Attach driver debugnet methods. */
16677790c8c1SConrad Meyer 	DEBUGNET_SET(ifp, alc);
16688a466583SMark Johnston 
1669d68875ebSPyun YongHyeon fail:
1670d68875ebSPyun YongHyeon 	if (error != 0)
1671d68875ebSPyun YongHyeon 		alc_detach(dev);
1672d68875ebSPyun YongHyeon 
1673d68875ebSPyun YongHyeon 	return (error);
1674d68875ebSPyun YongHyeon }
1675d68875ebSPyun YongHyeon 
1676d68875ebSPyun YongHyeon static int
alc_detach(device_t dev)1677d68875ebSPyun YongHyeon alc_detach(device_t dev)
1678d68875ebSPyun YongHyeon {
1679d68875ebSPyun YongHyeon 	struct alc_softc *sc;
168052436412SJustin Hibbits 	if_t ifp;
1681d68875ebSPyun YongHyeon 	int i, msic;
1682d68875ebSPyun YongHyeon 
1683d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
1684d68875ebSPyun YongHyeon 
1685d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
1686d68875ebSPyun YongHyeon 	if (device_is_attached(dev)) {
16873b33d630SJohn Baldwin 		ether_ifdetach(ifp);
1688d68875ebSPyun YongHyeon 		ALC_LOCK(sc);
1689d68875ebSPyun YongHyeon 		alc_stop(sc);
1690d68875ebSPyun YongHyeon 		ALC_UNLOCK(sc);
1691d68875ebSPyun YongHyeon 		callout_drain(&sc->alc_tick_ch);
1692d68875ebSPyun YongHyeon 		taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1693d68875ebSPyun YongHyeon 	}
1694d68875ebSPyun YongHyeon 
1695d68875ebSPyun YongHyeon 	if (sc->alc_tq != NULL) {
1696d68875ebSPyun YongHyeon 		taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1697d68875ebSPyun YongHyeon 		taskqueue_free(sc->alc_tq);
1698d68875ebSPyun YongHyeon 		sc->alc_tq = NULL;
1699d68875ebSPyun YongHyeon 	}
1700d68875ebSPyun YongHyeon 
1701d68875ebSPyun YongHyeon 	bus_generic_detach(dev);
1702d68875ebSPyun YongHyeon 	alc_dma_free(sc);
1703d68875ebSPyun YongHyeon 
1704d68875ebSPyun YongHyeon 	if (ifp != NULL) {
1705d68875ebSPyun YongHyeon 		if_free(ifp);
1706d68875ebSPyun YongHyeon 		sc->alc_ifp = NULL;
1707d68875ebSPyun YongHyeon 	}
1708d68875ebSPyun YongHyeon 
1709d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1710d68875ebSPyun YongHyeon 		msic = ALC_MSIX_MESSAGES;
1711d68875ebSPyun YongHyeon 	else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1712d68875ebSPyun YongHyeon 		msic = ALC_MSI_MESSAGES;
1713d68875ebSPyun YongHyeon 	else
1714d68875ebSPyun YongHyeon 		msic = 1;
1715d68875ebSPyun YongHyeon 	for (i = 0; i < msic; i++) {
1716d68875ebSPyun YongHyeon 		if (sc->alc_intrhand[i] != NULL) {
1717d68875ebSPyun YongHyeon 			bus_teardown_intr(dev, sc->alc_irq[i],
1718d68875ebSPyun YongHyeon 			    sc->alc_intrhand[i]);
1719d68875ebSPyun YongHyeon 			sc->alc_intrhand[i] = NULL;
1720d68875ebSPyun YongHyeon 		}
1721d68875ebSPyun YongHyeon 	}
1722e4d5e248SPyun YongHyeon 	if (sc->alc_res[0] != NULL)
1723d68875ebSPyun YongHyeon 		alc_phy_down(sc);
1724d68875ebSPyun YongHyeon 	bus_release_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1725d68875ebSPyun YongHyeon 	if ((sc->alc_flags & (ALC_FLAG_MSI | ALC_FLAG_MSIX)) != 0)
1726d68875ebSPyun YongHyeon 		pci_release_msi(dev);
1727d68875ebSPyun YongHyeon 	bus_release_resources(dev, sc->alc_res_spec, sc->alc_res);
1728d68875ebSPyun YongHyeon 	mtx_destroy(&sc->alc_mtx);
1729d68875ebSPyun YongHyeon 
1730d68875ebSPyun YongHyeon 	return (0);
1731d68875ebSPyun YongHyeon }
1732d68875ebSPyun YongHyeon 
1733d68875ebSPyun YongHyeon #define	ALC_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
1734d68875ebSPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
1735d68875ebSPyun YongHyeon #define	ALC_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
17366dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
1737d68875ebSPyun YongHyeon 
1738d68875ebSPyun YongHyeon static void
alc_sysctl_node(struct alc_softc * sc)1739d68875ebSPyun YongHyeon alc_sysctl_node(struct alc_softc *sc)
1740d68875ebSPyun YongHyeon {
1741d68875ebSPyun YongHyeon 	struct sysctl_ctx_list *ctx;
1742d68875ebSPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
1743d68875ebSPyun YongHyeon 	struct sysctl_oid *tree;
1744d68875ebSPyun YongHyeon 	struct alc_hw_stats *stats;
1745d68875ebSPyun YongHyeon 	int error;
1746d68875ebSPyun YongHyeon 
1747d68875ebSPyun YongHyeon 	stats = &sc->alc_stats;
1748d68875ebSPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->alc_dev);
1749d68875ebSPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->alc_dev));
1750d68875ebSPyun YongHyeon 
1751d68875ebSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_rx_mod",
17527029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->alc_int_rx_mod,
17537029da5cSPawel Biernacki 	    0, sysctl_hw_alc_int_mod, "I", "alc Rx interrupt moderation");
1754d68875ebSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_tx_mod",
17557029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->alc_int_tx_mod,
17567029da5cSPawel Biernacki 	    0, sysctl_hw_alc_int_mod, "I", "alc Tx interrupt moderation");
1757d68875ebSPyun YongHyeon 	/* Pull in device tunables. */
1758d68875ebSPyun YongHyeon 	sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1759d68875ebSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->alc_dev),
1760d68875ebSPyun YongHyeon 	    device_get_unit(sc->alc_dev), "int_rx_mod", &sc->alc_int_rx_mod);
1761d68875ebSPyun YongHyeon 	if (error == 0) {
1762d68875ebSPyun YongHyeon 		if (sc->alc_int_rx_mod < ALC_IM_TIMER_MIN ||
1763d68875ebSPyun YongHyeon 		    sc->alc_int_rx_mod > ALC_IM_TIMER_MAX) {
1764d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev, "int_rx_mod value out of "
1765d68875ebSPyun YongHyeon 			    "range; using default: %d\n",
1766d68875ebSPyun YongHyeon 			    ALC_IM_RX_TIMER_DEFAULT);
1767d68875ebSPyun YongHyeon 			sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1768d68875ebSPyun YongHyeon 		}
1769d68875ebSPyun YongHyeon 	}
1770d68875ebSPyun YongHyeon 	sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1771d68875ebSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->alc_dev),
1772d68875ebSPyun YongHyeon 	    device_get_unit(sc->alc_dev), "int_tx_mod", &sc->alc_int_tx_mod);
1773d68875ebSPyun YongHyeon 	if (error == 0) {
1774d68875ebSPyun YongHyeon 		if (sc->alc_int_tx_mod < ALC_IM_TIMER_MIN ||
1775d68875ebSPyun YongHyeon 		    sc->alc_int_tx_mod > ALC_IM_TIMER_MAX) {
1776d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev, "int_tx_mod value out of "
1777d68875ebSPyun YongHyeon 			    "range; using default: %d\n",
1778d68875ebSPyun YongHyeon 			    ALC_IM_TX_TIMER_DEFAULT);
1779d68875ebSPyun YongHyeon 			sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1780d68875ebSPyun YongHyeon 		}
1781d68875ebSPyun YongHyeon 	}
1782d68875ebSPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
17837029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
17847029da5cSPawel Biernacki 	    &sc->alc_process_limit, 0, sysctl_hw_alc_proc_limit, "I",
1785d68875ebSPyun YongHyeon 	    "max number of Rx events to process");
1786d68875ebSPyun YongHyeon 	/* Pull in device tunables. */
1787d68875ebSPyun YongHyeon 	sc->alc_process_limit = ALC_PROC_DEFAULT;
1788d68875ebSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->alc_dev),
1789d68875ebSPyun YongHyeon 	    device_get_unit(sc->alc_dev), "process_limit",
1790d68875ebSPyun YongHyeon 	    &sc->alc_process_limit);
1791d68875ebSPyun YongHyeon 	if (error == 0) {
1792d68875ebSPyun YongHyeon 		if (sc->alc_process_limit < ALC_PROC_MIN ||
1793d68875ebSPyun YongHyeon 		    sc->alc_process_limit > ALC_PROC_MAX) {
1794d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
1795d68875ebSPyun YongHyeon 			    "process_limit value out of range; "
1796d68875ebSPyun YongHyeon 			    "using default: %d\n", ALC_PROC_DEFAULT);
1797d68875ebSPyun YongHyeon 			sc->alc_process_limit = ALC_PROC_DEFAULT;
1798d68875ebSPyun YongHyeon 		}
1799d68875ebSPyun YongHyeon 	}
1800d68875ebSPyun YongHyeon 
18017029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
18027029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ALC statistics");
1803d68875ebSPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
1804d68875ebSPyun YongHyeon 
1805d68875ebSPyun YongHyeon 	/* Rx statistics. */
18067029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
18077029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
1808d68875ebSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
1809d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1810d68875ebSPyun YongHyeon 	    &stats->rx_frames, "Good frames");
1811d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1812d68875ebSPyun YongHyeon 	    &stats->rx_bcast_frames, "Good broadcast frames");
1813d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1814d68875ebSPyun YongHyeon 	    &stats->rx_mcast_frames, "Good multicast frames");
1815d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1816d68875ebSPyun YongHyeon 	    &stats->rx_pause_frames, "Pause control frames");
1817d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1818d68875ebSPyun YongHyeon 	    &stats->rx_control_frames, "Control frames");
1819d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
1820d68875ebSPyun YongHyeon 	    &stats->rx_crcerrs, "CRC errors");
1821d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1822d68875ebSPyun YongHyeon 	    &stats->rx_lenerrs, "Frames with length mismatched");
1823d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1824d68875ebSPyun YongHyeon 	    &stats->rx_bytes, "Good octets");
1825d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1826d68875ebSPyun YongHyeon 	    &stats->rx_bcast_bytes, "Good broadcast octets");
1827d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1828d68875ebSPyun YongHyeon 	    &stats->rx_mcast_bytes, "Good multicast octets");
1829d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "runts",
1830d68875ebSPyun YongHyeon 	    &stats->rx_runts, "Too short frames");
1831d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "fragments",
1832d68875ebSPyun YongHyeon 	    &stats->rx_fragments, "Fragmented frames");
1833d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1834d68875ebSPyun YongHyeon 	    &stats->rx_pkts_64, "64 bytes frames");
1835d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1836d68875ebSPyun YongHyeon 	    &stats->rx_pkts_65_127, "65 to 127 bytes frames");
1837d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1838d68875ebSPyun YongHyeon 	    &stats->rx_pkts_128_255, "128 to 255 bytes frames");
1839d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1840d68875ebSPyun YongHyeon 	    &stats->rx_pkts_256_511, "256 to 511 bytes frames");
1841d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1842d68875ebSPyun YongHyeon 	    &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
1843d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1844d68875ebSPyun YongHyeon 	    &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
1845d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1846d68875ebSPyun YongHyeon 	    &stats->rx_pkts_1519_max, "1519 to max frames");
1847d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1848d68875ebSPyun YongHyeon 	    &stats->rx_pkts_truncated, "Truncated frames due to MTU size");
1849d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
1850d68875ebSPyun YongHyeon 	    &stats->rx_fifo_oflows, "FIFO overflows");
1851d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "rrs_errs",
1852d68875ebSPyun YongHyeon 	    &stats->rx_rrs_errs, "Return status write-back errors");
1853d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
1854d68875ebSPyun YongHyeon 	    &stats->rx_alignerrs, "Alignment errors");
1855d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "filtered",
1856d68875ebSPyun YongHyeon 	    &stats->rx_pkts_filtered,
1857d68875ebSPyun YongHyeon 	    "Frames dropped due to address filtering");
1858d68875ebSPyun YongHyeon 
1859d68875ebSPyun YongHyeon 	/* Tx statistics. */
18607029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
18617029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
1862d68875ebSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
1863d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1864d68875ebSPyun YongHyeon 	    &stats->tx_frames, "Good frames");
1865d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1866d68875ebSPyun YongHyeon 	    &stats->tx_bcast_frames, "Good broadcast frames");
1867d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1868d68875ebSPyun YongHyeon 	    &stats->tx_mcast_frames, "Good multicast frames");
1869d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1870d68875ebSPyun YongHyeon 	    &stats->tx_pause_frames, "Pause control frames");
1871d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1872d68875ebSPyun YongHyeon 	    &stats->tx_control_frames, "Control frames");
1873d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
1874d68875ebSPyun YongHyeon 	    &stats->tx_excess_defer, "Frames with excessive derferrals");
1875d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "defers",
1876d68875ebSPyun YongHyeon 	    &stats->tx_excess_defer, "Frames with derferrals");
1877d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1878d68875ebSPyun YongHyeon 	    &stats->tx_bytes, "Good octets");
1879d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1880d68875ebSPyun YongHyeon 	    &stats->tx_bcast_bytes, "Good broadcast octets");
1881d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1882d68875ebSPyun YongHyeon 	    &stats->tx_mcast_bytes, "Good multicast octets");
1883d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1884d68875ebSPyun YongHyeon 	    &stats->tx_pkts_64, "64 bytes frames");
1885d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1886d68875ebSPyun YongHyeon 	    &stats->tx_pkts_65_127, "65 to 127 bytes frames");
1887d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1888d68875ebSPyun YongHyeon 	    &stats->tx_pkts_128_255, "128 to 255 bytes frames");
1889d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1890d68875ebSPyun YongHyeon 	    &stats->tx_pkts_256_511, "256 to 511 bytes frames");
1891d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1892d68875ebSPyun YongHyeon 	    &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
1893d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1894d68875ebSPyun YongHyeon 	    &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
1895d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1896d68875ebSPyun YongHyeon 	    &stats->tx_pkts_1519_max, "1519 to max frames");
1897d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
1898d68875ebSPyun YongHyeon 	    &stats->tx_single_colls, "Single collisions");
1899d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
1900d68875ebSPyun YongHyeon 	    &stats->tx_multi_colls, "Multiple collisions");
1901d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
1902d68875ebSPyun YongHyeon 	    &stats->tx_late_colls, "Late collisions");
1903d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_colls",
1904d68875ebSPyun YongHyeon 	    &stats->tx_excess_colls, "Excessive collisions");
1905d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "underruns",
1906d68875ebSPyun YongHyeon 	    &stats->tx_underrun, "FIFO underruns");
1907d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "desc_underruns",
1908d68875ebSPyun YongHyeon 	    &stats->tx_desc_underrun, "Descriptor write-back errors");
1909d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1910d68875ebSPyun YongHyeon 	    &stats->tx_lenerrs, "Frames with length mismatched");
1911d68875ebSPyun YongHyeon 	ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1912d68875ebSPyun YongHyeon 	    &stats->tx_pkts_truncated, "Truncated frames due to MTU size");
1913d68875ebSPyun YongHyeon }
1914d68875ebSPyun YongHyeon 
1915d68875ebSPyun YongHyeon #undef ALC_SYSCTL_STAT_ADD32
1916d68875ebSPyun YongHyeon #undef ALC_SYSCTL_STAT_ADD64
1917d68875ebSPyun YongHyeon 
1918d68875ebSPyun YongHyeon struct alc_dmamap_arg {
1919d68875ebSPyun YongHyeon 	bus_addr_t	alc_busaddr;
1920d68875ebSPyun YongHyeon };
1921d68875ebSPyun YongHyeon 
1922d68875ebSPyun YongHyeon static void
alc_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)1923d68875ebSPyun YongHyeon alc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1924d68875ebSPyun YongHyeon {
1925d68875ebSPyun YongHyeon 	struct alc_dmamap_arg *ctx;
1926d68875ebSPyun YongHyeon 
1927d68875ebSPyun YongHyeon 	if (error != 0)
1928d68875ebSPyun YongHyeon 		return;
1929d68875ebSPyun YongHyeon 
1930d68875ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1931d68875ebSPyun YongHyeon 
1932d68875ebSPyun YongHyeon 	ctx = (struct alc_dmamap_arg *)arg;
1933d68875ebSPyun YongHyeon 	ctx->alc_busaddr = segs[0].ds_addr;
1934d68875ebSPyun YongHyeon }
1935d68875ebSPyun YongHyeon 
1936d68875ebSPyun YongHyeon /*
1937d68875ebSPyun YongHyeon  * Normal and high Tx descriptors shares single Tx high address.
1938d68875ebSPyun YongHyeon  * Four Rx descriptor/return rings and CMB shares the same Rx
1939d68875ebSPyun YongHyeon  * high address.
1940d68875ebSPyun YongHyeon  */
1941d68875ebSPyun YongHyeon static int
alc_check_boundary(struct alc_softc * sc)1942d68875ebSPyun YongHyeon alc_check_boundary(struct alc_softc *sc)
1943d68875ebSPyun YongHyeon {
1944d68875ebSPyun YongHyeon 	bus_addr_t cmb_end, rx_ring_end, rr_ring_end, tx_ring_end;
1945d68875ebSPyun YongHyeon 
1946d68875ebSPyun YongHyeon 	rx_ring_end = sc->alc_rdata.alc_rx_ring_paddr + ALC_RX_RING_SZ;
1947d68875ebSPyun YongHyeon 	rr_ring_end = sc->alc_rdata.alc_rr_ring_paddr + ALC_RR_RING_SZ;
1948d68875ebSPyun YongHyeon 	cmb_end = sc->alc_rdata.alc_cmb_paddr + ALC_CMB_SZ;
1949d68875ebSPyun YongHyeon 	tx_ring_end = sc->alc_rdata.alc_tx_ring_paddr + ALC_TX_RING_SZ;
1950d68875ebSPyun YongHyeon 
1951d68875ebSPyun YongHyeon 	/* 4GB boundary crossing is not allowed. */
1952d68875ebSPyun YongHyeon 	if ((ALC_ADDR_HI(rx_ring_end) !=
1953d68875ebSPyun YongHyeon 	    ALC_ADDR_HI(sc->alc_rdata.alc_rx_ring_paddr)) ||
1954d68875ebSPyun YongHyeon 	    (ALC_ADDR_HI(rr_ring_end) !=
1955d68875ebSPyun YongHyeon 	    ALC_ADDR_HI(sc->alc_rdata.alc_rr_ring_paddr)) ||
1956d68875ebSPyun YongHyeon 	    (ALC_ADDR_HI(cmb_end) !=
1957d68875ebSPyun YongHyeon 	    ALC_ADDR_HI(sc->alc_rdata.alc_cmb_paddr)) ||
1958d68875ebSPyun YongHyeon 	    (ALC_ADDR_HI(tx_ring_end) !=
1959d68875ebSPyun YongHyeon 	    ALC_ADDR_HI(sc->alc_rdata.alc_tx_ring_paddr)))
1960d68875ebSPyun YongHyeon 		return (EFBIG);
1961d68875ebSPyun YongHyeon 	/*
1962d68875ebSPyun YongHyeon 	 * Make sure Rx return descriptor/Rx descriptor/CMB use
1963d68875ebSPyun YongHyeon 	 * the same high address.
1964d68875ebSPyun YongHyeon 	 */
1965d68875ebSPyun YongHyeon 	if ((ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(rr_ring_end)) ||
1966d68875ebSPyun YongHyeon 	    (ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(cmb_end)))
1967d68875ebSPyun YongHyeon 		return (EFBIG);
1968d68875ebSPyun YongHyeon 
1969d68875ebSPyun YongHyeon 	return (0);
1970d68875ebSPyun YongHyeon }
1971d68875ebSPyun YongHyeon 
1972d68875ebSPyun YongHyeon static int
alc_dma_alloc(struct alc_softc * sc)1973d68875ebSPyun YongHyeon alc_dma_alloc(struct alc_softc *sc)
1974d68875ebSPyun YongHyeon {
1975d68875ebSPyun YongHyeon 	struct alc_txdesc *txd;
1976d68875ebSPyun YongHyeon 	struct alc_rxdesc *rxd;
1977d68875ebSPyun YongHyeon 	bus_addr_t lowaddr;
1978d68875ebSPyun YongHyeon 	struct alc_dmamap_arg ctx;
1979d68875ebSPyun YongHyeon 	int error, i;
1980d68875ebSPyun YongHyeon 
1981d68875ebSPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
198277b63733SKonstantin Belousov 	if (sc->alc_flags & ALC_FLAG_MT)
198377b63733SKonstantin Belousov 		lowaddr = BUS_SPACE_MAXSIZE_32BIT;
1984d68875ebSPyun YongHyeon again:
1985d68875ebSPyun YongHyeon 	/* Create parent DMA tag. */
1986d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
1987d68875ebSPyun YongHyeon 	    bus_get_dma_tag(sc->alc_dev), /* parent */
1988d68875ebSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1989d68875ebSPyun YongHyeon 	    lowaddr,			/* lowaddr */
1990d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1991d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1992d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1993d68875ebSPyun YongHyeon 	    0,				/* nsegments */
1994d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1995d68875ebSPyun YongHyeon 	    0,				/* flags */
1996d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1997d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_parent_tag);
1998d68875ebSPyun YongHyeon 	if (error != 0) {
1999d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2000d68875ebSPyun YongHyeon 		    "could not create parent DMA tag.\n");
2001d68875ebSPyun YongHyeon 		goto fail;
2002d68875ebSPyun YongHyeon 	}
2003d68875ebSPyun YongHyeon 
2004d68875ebSPyun YongHyeon 	/* Create DMA tag for Tx descriptor ring. */
2005d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2006d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_parent_tag, /* parent */
2007d68875ebSPyun YongHyeon 	    ALC_TX_RING_ALIGN, 0,	/* alignment, boundary */
2008d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2009d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2010d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2011d68875ebSPyun YongHyeon 	    ALC_TX_RING_SZ,		/* maxsize */
2012d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2013d68875ebSPyun YongHyeon 	    ALC_TX_RING_SZ,		/* maxsegsize */
2014d68875ebSPyun YongHyeon 	    0,				/* flags */
2015d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2016d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_tx_ring_tag);
2017d68875ebSPyun YongHyeon 	if (error != 0) {
2018d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2019d68875ebSPyun YongHyeon 		    "could not create Tx ring DMA tag.\n");
2020d68875ebSPyun YongHyeon 		goto fail;
2021d68875ebSPyun YongHyeon 	}
2022d68875ebSPyun YongHyeon 
2023d68875ebSPyun YongHyeon 	/* Create DMA tag for Rx free descriptor ring. */
2024d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2025d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_parent_tag, /* parent */
2026d68875ebSPyun YongHyeon 	    ALC_RX_RING_ALIGN, 0,	/* alignment, boundary */
2027d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2028d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2029d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2030d68875ebSPyun YongHyeon 	    ALC_RX_RING_SZ,		/* maxsize */
2031d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2032d68875ebSPyun YongHyeon 	    ALC_RX_RING_SZ,		/* maxsegsize */
2033d68875ebSPyun YongHyeon 	    0,				/* flags */
2034d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2035d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rx_ring_tag);
2036d68875ebSPyun YongHyeon 	if (error != 0) {
2037d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2038d68875ebSPyun YongHyeon 		    "could not create Rx ring DMA tag.\n");
2039d68875ebSPyun YongHyeon 		goto fail;
2040d68875ebSPyun YongHyeon 	}
2041d68875ebSPyun YongHyeon 	/* Create DMA tag for Rx return descriptor ring. */
2042d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2043d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_parent_tag, /* parent */
2044d68875ebSPyun YongHyeon 	    ALC_RR_RING_ALIGN, 0,	/* alignment, boundary */
2045d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2046d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2047d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2048d68875ebSPyun YongHyeon 	    ALC_RR_RING_SZ,		/* maxsize */
2049d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2050d68875ebSPyun YongHyeon 	    ALC_RR_RING_SZ,		/* maxsegsize */
2051d68875ebSPyun YongHyeon 	    0,				/* flags */
2052d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2053d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rr_ring_tag);
2054d68875ebSPyun YongHyeon 	if (error != 0) {
2055d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2056d68875ebSPyun YongHyeon 		    "could not create Rx return ring DMA tag.\n");
2057d68875ebSPyun YongHyeon 		goto fail;
2058d68875ebSPyun YongHyeon 	}
2059d68875ebSPyun YongHyeon 
2060d68875ebSPyun YongHyeon 	/* Create DMA tag for coalescing message block. */
2061d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2062d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_parent_tag, /* parent */
2063d68875ebSPyun YongHyeon 	    ALC_CMB_ALIGN, 0,		/* alignment, boundary */
2064d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2065d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2066d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2067d68875ebSPyun YongHyeon 	    ALC_CMB_SZ,			/* maxsize */
2068d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2069d68875ebSPyun YongHyeon 	    ALC_CMB_SZ,			/* maxsegsize */
2070d68875ebSPyun YongHyeon 	    0,				/* flags */
2071d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2072d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_cmb_tag);
2073d68875ebSPyun YongHyeon 	if (error != 0) {
2074d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2075d68875ebSPyun YongHyeon 		    "could not create CMB DMA tag.\n");
2076d68875ebSPyun YongHyeon 		goto fail;
2077d68875ebSPyun YongHyeon 	}
2078d68875ebSPyun YongHyeon 	/* Create DMA tag for status message block. */
2079d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2080d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_parent_tag, /* parent */
2081d68875ebSPyun YongHyeon 	    ALC_SMB_ALIGN, 0,		/* alignment, boundary */
2082d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2083d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2084d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2085d68875ebSPyun YongHyeon 	    ALC_SMB_SZ,			/* maxsize */
2086d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2087d68875ebSPyun YongHyeon 	    ALC_SMB_SZ,			/* maxsegsize */
2088d68875ebSPyun YongHyeon 	    0,				/* flags */
2089d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2090d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_smb_tag);
2091d68875ebSPyun YongHyeon 	if (error != 0) {
2092d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2093d68875ebSPyun YongHyeon 		    "could not create SMB DMA tag.\n");
2094d68875ebSPyun YongHyeon 		goto fail;
2095d68875ebSPyun YongHyeon 	}
2096d68875ebSPyun YongHyeon 
2097d68875ebSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
2098d68875ebSPyun YongHyeon 	error = bus_dmamem_alloc(sc->alc_cdata.alc_tx_ring_tag,
2099d68875ebSPyun YongHyeon 	    (void **)&sc->alc_rdata.alc_tx_ring,
2100d68875ebSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2101d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_tx_ring_map);
2102d68875ebSPyun YongHyeon 	if (error != 0) {
2103d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2104d68875ebSPyun YongHyeon 		    "could not allocate DMA'able memory for Tx ring.\n");
2105d68875ebSPyun YongHyeon 		goto fail;
2106d68875ebSPyun YongHyeon 	}
2107d68875ebSPyun YongHyeon 	ctx.alc_busaddr = 0;
2108d68875ebSPyun YongHyeon 	error = bus_dmamap_load(sc->alc_cdata.alc_tx_ring_tag,
2109d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_tx_ring_map, sc->alc_rdata.alc_tx_ring,
2110d68875ebSPyun YongHyeon 	    ALC_TX_RING_SZ, alc_dmamap_cb, &ctx, 0);
2111d68875ebSPyun YongHyeon 	if (error != 0 || ctx.alc_busaddr == 0) {
2112d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2113d68875ebSPyun YongHyeon 		    "could not load DMA'able memory for Tx ring.\n");
2114d68875ebSPyun YongHyeon 		goto fail;
2115d68875ebSPyun YongHyeon 	}
2116d68875ebSPyun YongHyeon 	sc->alc_rdata.alc_tx_ring_paddr = ctx.alc_busaddr;
2117d68875ebSPyun YongHyeon 
2118d68875ebSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
2119d68875ebSPyun YongHyeon 	error = bus_dmamem_alloc(sc->alc_cdata.alc_rx_ring_tag,
2120d68875ebSPyun YongHyeon 	    (void **)&sc->alc_rdata.alc_rx_ring,
2121d68875ebSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2122d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rx_ring_map);
2123d68875ebSPyun YongHyeon 	if (error != 0) {
2124d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2125d68875ebSPyun YongHyeon 		    "could not allocate DMA'able memory for Rx ring.\n");
2126d68875ebSPyun YongHyeon 		goto fail;
2127d68875ebSPyun YongHyeon 	}
2128d68875ebSPyun YongHyeon 	ctx.alc_busaddr = 0;
2129d68875ebSPyun YongHyeon 	error = bus_dmamap_load(sc->alc_cdata.alc_rx_ring_tag,
2130d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rx_ring_map, sc->alc_rdata.alc_rx_ring,
2131d68875ebSPyun YongHyeon 	    ALC_RX_RING_SZ, alc_dmamap_cb, &ctx, 0);
2132d68875ebSPyun YongHyeon 	if (error != 0 || ctx.alc_busaddr == 0) {
2133d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2134d68875ebSPyun YongHyeon 		    "could not load DMA'able memory for Rx ring.\n");
2135d68875ebSPyun YongHyeon 		goto fail;
2136d68875ebSPyun YongHyeon 	}
2137d68875ebSPyun YongHyeon 	sc->alc_rdata.alc_rx_ring_paddr = ctx.alc_busaddr;
2138d68875ebSPyun YongHyeon 
2139d68875ebSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx return ring. */
2140d68875ebSPyun YongHyeon 	error = bus_dmamem_alloc(sc->alc_cdata.alc_rr_ring_tag,
2141d68875ebSPyun YongHyeon 	    (void **)&sc->alc_rdata.alc_rr_ring,
2142d68875ebSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2143d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rr_ring_map);
2144d68875ebSPyun YongHyeon 	if (error != 0) {
2145d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2146d68875ebSPyun YongHyeon 		    "could not allocate DMA'able memory for Rx return ring.\n");
2147d68875ebSPyun YongHyeon 		goto fail;
2148d68875ebSPyun YongHyeon 	}
2149d68875ebSPyun YongHyeon 	ctx.alc_busaddr = 0;
2150d68875ebSPyun YongHyeon 	error = bus_dmamap_load(sc->alc_cdata.alc_rr_ring_tag,
2151d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rr_ring_map, sc->alc_rdata.alc_rr_ring,
2152d68875ebSPyun YongHyeon 	    ALC_RR_RING_SZ, alc_dmamap_cb, &ctx, 0);
2153d68875ebSPyun YongHyeon 	if (error != 0 || ctx.alc_busaddr == 0) {
2154d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2155d68875ebSPyun YongHyeon 		    "could not load DMA'able memory for Tx ring.\n");
2156d68875ebSPyun YongHyeon 		goto fail;
2157d68875ebSPyun YongHyeon 	}
2158d68875ebSPyun YongHyeon 	sc->alc_rdata.alc_rr_ring_paddr = ctx.alc_busaddr;
2159d68875ebSPyun YongHyeon 
2160d68875ebSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for CMB. */
2161d68875ebSPyun YongHyeon 	error = bus_dmamem_alloc(sc->alc_cdata.alc_cmb_tag,
2162d68875ebSPyun YongHyeon 	    (void **)&sc->alc_rdata.alc_cmb,
2163d68875ebSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2164d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_cmb_map);
2165d68875ebSPyun YongHyeon 	if (error != 0) {
2166d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2167d68875ebSPyun YongHyeon 		    "could not allocate DMA'able memory for CMB.\n");
2168d68875ebSPyun YongHyeon 		goto fail;
2169d68875ebSPyun YongHyeon 	}
2170d68875ebSPyun YongHyeon 	ctx.alc_busaddr = 0;
2171d68875ebSPyun YongHyeon 	error = bus_dmamap_load(sc->alc_cdata.alc_cmb_tag,
2172d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_cmb_map, sc->alc_rdata.alc_cmb,
2173d68875ebSPyun YongHyeon 	    ALC_CMB_SZ, alc_dmamap_cb, &ctx, 0);
2174d68875ebSPyun YongHyeon 	if (error != 0 || ctx.alc_busaddr == 0) {
2175d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2176d68875ebSPyun YongHyeon 		    "could not load DMA'able memory for CMB.\n");
2177d68875ebSPyun YongHyeon 		goto fail;
2178d68875ebSPyun YongHyeon 	}
2179d68875ebSPyun YongHyeon 	sc->alc_rdata.alc_cmb_paddr = ctx.alc_busaddr;
2180d68875ebSPyun YongHyeon 
2181d68875ebSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for SMB. */
2182d68875ebSPyun YongHyeon 	error = bus_dmamem_alloc(sc->alc_cdata.alc_smb_tag,
2183d68875ebSPyun YongHyeon 	    (void **)&sc->alc_rdata.alc_smb,
2184d68875ebSPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2185d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_smb_map);
2186d68875ebSPyun YongHyeon 	if (error != 0) {
2187d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2188d68875ebSPyun YongHyeon 		    "could not allocate DMA'able memory for SMB.\n");
2189d68875ebSPyun YongHyeon 		goto fail;
2190d68875ebSPyun YongHyeon 	}
2191d68875ebSPyun YongHyeon 	ctx.alc_busaddr = 0;
2192d68875ebSPyun YongHyeon 	error = bus_dmamap_load(sc->alc_cdata.alc_smb_tag,
2193d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_smb_map, sc->alc_rdata.alc_smb,
2194d68875ebSPyun YongHyeon 	    ALC_SMB_SZ, alc_dmamap_cb, &ctx, 0);
2195d68875ebSPyun YongHyeon 	if (error != 0 || ctx.alc_busaddr == 0) {
2196d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2197d68875ebSPyun YongHyeon 		    "could not load DMA'able memory for CMB.\n");
2198d68875ebSPyun YongHyeon 		goto fail;
2199d68875ebSPyun YongHyeon 	}
2200d68875ebSPyun YongHyeon 	sc->alc_rdata.alc_smb_paddr = ctx.alc_busaddr;
2201d68875ebSPyun YongHyeon 
2202d68875ebSPyun YongHyeon 	/* Make sure we've not crossed 4GB boundary. */
2203d68875ebSPyun YongHyeon 	if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
2204d68875ebSPyun YongHyeon 	    (error = alc_check_boundary(sc)) != 0) {
2205d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "4GB boundary crossed, "
2206d68875ebSPyun YongHyeon 		    "switching to 32bit DMA addressing mode.\n");
2207d68875ebSPyun YongHyeon 		alc_dma_free(sc);
2208d68875ebSPyun YongHyeon 		/*
2209d68875ebSPyun YongHyeon 		 * Limit max allowable DMA address space to 32bit
2210d68875ebSPyun YongHyeon 		 * and try again.
2211d68875ebSPyun YongHyeon 		 */
2212d68875ebSPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
2213d68875ebSPyun YongHyeon 		goto again;
2214d68875ebSPyun YongHyeon 	}
2215d68875ebSPyun YongHyeon 
2216d68875ebSPyun YongHyeon 	/*
2217d68875ebSPyun YongHyeon 	 * Create Tx buffer parent tag.
2218b624ef0aSPyun YongHyeon 	 * AR81[3567]x allows 64bit DMA addressing of Tx/Rx buffers
2219d68875ebSPyun YongHyeon 	 * so it needs separate parent DMA tag as parent DMA address
2220d68875ebSPyun YongHyeon 	 * space could be restricted to be within 32bit address space
2221d68875ebSPyun YongHyeon 	 * by 4GB boundary crossing.
2222d68875ebSPyun YongHyeon 	 */
2223d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2224d68875ebSPyun YongHyeon 	    bus_get_dma_tag(sc->alc_dev), /* parent */
2225d68875ebSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
222677b63733SKonstantin Belousov 	    lowaddr,			/* lowaddr */
2227d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2228d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2229d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
2230d68875ebSPyun YongHyeon 	    0,				/* nsegments */
2231d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
2232d68875ebSPyun YongHyeon 	    0,				/* flags */
2233d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2234d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_buffer_tag);
2235d68875ebSPyun YongHyeon 	if (error != 0) {
2236d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2237d68875ebSPyun YongHyeon 		    "could not create parent buffer DMA tag.\n");
2238d68875ebSPyun YongHyeon 		goto fail;
2239d68875ebSPyun YongHyeon 	}
2240d68875ebSPyun YongHyeon 
2241d68875ebSPyun YongHyeon 	/* Create DMA tag for Tx buffers. */
2242d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2243d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_buffer_tag, /* parent */
2244d68875ebSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
2245d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2246d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2247d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2248d68875ebSPyun YongHyeon 	    ALC_TSO_MAXSIZE,		/* maxsize */
2249d68875ebSPyun YongHyeon 	    ALC_MAXTXSEGS,		/* nsegments */
2250d68875ebSPyun YongHyeon 	    ALC_TSO_MAXSEGSIZE,		/* maxsegsize */
2251d68875ebSPyun YongHyeon 	    0,				/* flags */
2252d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2253d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_tx_tag);
2254d68875ebSPyun YongHyeon 	if (error != 0) {
2255d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "could not create Tx DMA tag.\n");
2256d68875ebSPyun YongHyeon 		goto fail;
2257d68875ebSPyun YongHyeon 	}
2258d68875ebSPyun YongHyeon 
2259d68875ebSPyun YongHyeon 	/* Create DMA tag for Rx buffers. */
2260d68875ebSPyun YongHyeon 	error = bus_dma_tag_create(
2261d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_buffer_tag, /* parent */
2262d68875ebSPyun YongHyeon 	    ALC_RX_BUF_ALIGN, 0,	/* alignment, boundary */
2263d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
2264d68875ebSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
2265d68875ebSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
2266d68875ebSPyun YongHyeon 	    MCLBYTES,			/* maxsize */
2267d68875ebSPyun YongHyeon 	    1,				/* nsegments */
2268d68875ebSPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
2269d68875ebSPyun YongHyeon 	    0,				/* flags */
2270d68875ebSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
2271d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rx_tag);
2272d68875ebSPyun YongHyeon 	if (error != 0) {
2273d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "could not create Rx DMA tag.\n");
2274d68875ebSPyun YongHyeon 		goto fail;
2275d68875ebSPyun YongHyeon 	}
2276d68875ebSPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
2277d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
2278d68875ebSPyun YongHyeon 		txd = &sc->alc_cdata.alc_txdesc[i];
2279d68875ebSPyun YongHyeon 		txd->tx_m = NULL;
2280d68875ebSPyun YongHyeon 		txd->tx_dmamap = NULL;
2281d68875ebSPyun YongHyeon 		error = bus_dmamap_create(sc->alc_cdata.alc_tx_tag, 0,
2282d68875ebSPyun YongHyeon 		    &txd->tx_dmamap);
2283d68875ebSPyun YongHyeon 		if (error != 0) {
2284d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
2285d68875ebSPyun YongHyeon 			    "could not create Tx dmamap.\n");
2286d68875ebSPyun YongHyeon 			goto fail;
2287d68875ebSPyun YongHyeon 		}
2288d68875ebSPyun YongHyeon 	}
2289d68875ebSPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
2290d68875ebSPyun YongHyeon 	if ((error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
2291d68875ebSPyun YongHyeon 	    &sc->alc_cdata.alc_rx_sparemap)) != 0) {
2292d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
2293d68875ebSPyun YongHyeon 		    "could not create spare Rx dmamap.\n");
2294d68875ebSPyun YongHyeon 		goto fail;
2295d68875ebSPyun YongHyeon 	}
2296d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
2297d68875ebSPyun YongHyeon 		rxd = &sc->alc_cdata.alc_rxdesc[i];
2298d68875ebSPyun YongHyeon 		rxd->rx_m = NULL;
2299d68875ebSPyun YongHyeon 		rxd->rx_dmamap = NULL;
2300d68875ebSPyun YongHyeon 		error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
2301d68875ebSPyun YongHyeon 		    &rxd->rx_dmamap);
2302d68875ebSPyun YongHyeon 		if (error != 0) {
2303d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
2304d68875ebSPyun YongHyeon 			    "could not create Rx dmamap.\n");
2305d68875ebSPyun YongHyeon 			goto fail;
2306d68875ebSPyun YongHyeon 		}
2307d68875ebSPyun YongHyeon 	}
2308d68875ebSPyun YongHyeon 
2309d68875ebSPyun YongHyeon fail:
2310d68875ebSPyun YongHyeon 	return (error);
2311d68875ebSPyun YongHyeon }
2312d68875ebSPyun YongHyeon 
2313d68875ebSPyun YongHyeon static void
alc_dma_free(struct alc_softc * sc)2314d68875ebSPyun YongHyeon alc_dma_free(struct alc_softc *sc)
2315d68875ebSPyun YongHyeon {
2316d68875ebSPyun YongHyeon 	struct alc_txdesc *txd;
2317d68875ebSPyun YongHyeon 	struct alc_rxdesc *rxd;
2318d68875ebSPyun YongHyeon 	int i;
2319d68875ebSPyun YongHyeon 
2320d68875ebSPyun YongHyeon 	/* Tx buffers. */
2321d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_tag != NULL) {
2322d68875ebSPyun YongHyeon 		for (i = 0; i < ALC_TX_RING_CNT; i++) {
2323d68875ebSPyun YongHyeon 			txd = &sc->alc_cdata.alc_txdesc[i];
2324d68875ebSPyun YongHyeon 			if (txd->tx_dmamap != NULL) {
2325d68875ebSPyun YongHyeon 				bus_dmamap_destroy(sc->alc_cdata.alc_tx_tag,
2326d68875ebSPyun YongHyeon 				    txd->tx_dmamap);
2327d68875ebSPyun YongHyeon 				txd->tx_dmamap = NULL;
2328d68875ebSPyun YongHyeon 			}
2329d68875ebSPyun YongHyeon 		}
2330d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_tx_tag);
2331d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_tx_tag = NULL;
2332d68875ebSPyun YongHyeon 	}
2333d68875ebSPyun YongHyeon 	/* Rx buffers */
2334d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_rx_tag != NULL) {
2335d68875ebSPyun YongHyeon 		for (i = 0; i < ALC_RX_RING_CNT; i++) {
2336d68875ebSPyun YongHyeon 			rxd = &sc->alc_cdata.alc_rxdesc[i];
2337d68875ebSPyun YongHyeon 			if (rxd->rx_dmamap != NULL) {
2338d68875ebSPyun YongHyeon 				bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
2339d68875ebSPyun YongHyeon 				    rxd->rx_dmamap);
2340d68875ebSPyun YongHyeon 				rxd->rx_dmamap = NULL;
2341d68875ebSPyun YongHyeon 			}
2342d68875ebSPyun YongHyeon 		}
2343d68875ebSPyun YongHyeon 		if (sc->alc_cdata.alc_rx_sparemap != NULL) {
2344d68875ebSPyun YongHyeon 			bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
2345d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rx_sparemap);
2346d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rx_sparemap = NULL;
2347d68875ebSPyun YongHyeon 		}
2348d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_rx_tag);
2349d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_rx_tag = NULL;
2350d68875ebSPyun YongHyeon 	}
2351d68875ebSPyun YongHyeon 	/* Tx descriptor ring. */
2352d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_ring_tag != NULL) {
2353068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_tx_ring_paddr != 0)
2354d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_tx_ring_tag,
2355d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_tx_ring_map);
2356068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_tx_ring != NULL)
2357d68875ebSPyun YongHyeon 			bus_dmamem_free(sc->alc_cdata.alc_tx_ring_tag,
2358d68875ebSPyun YongHyeon 			    sc->alc_rdata.alc_tx_ring,
2359d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_tx_ring_map);
2360068d8643SJohn Baldwin 		sc->alc_rdata.alc_tx_ring_paddr = 0;
2361d68875ebSPyun YongHyeon 		sc->alc_rdata.alc_tx_ring = NULL;
2362d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_tx_ring_tag);
2363d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_tx_ring_tag = NULL;
2364d68875ebSPyun YongHyeon 	}
23656ece67d8SKevin Lo 	/* Rx ring. */
23666ece67d8SKevin Lo 	if (sc->alc_cdata.alc_rx_ring_tag != NULL) {
2367068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_rx_ring_paddr != 0)
23686ece67d8SKevin Lo 			bus_dmamap_unload(sc->alc_cdata.alc_rx_ring_tag,
23696ece67d8SKevin Lo 			    sc->alc_cdata.alc_rx_ring_map);
2370068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_rx_ring != NULL)
23716ece67d8SKevin Lo 			bus_dmamem_free(sc->alc_cdata.alc_rx_ring_tag,
23726ece67d8SKevin Lo 			    sc->alc_rdata.alc_rx_ring,
23736ece67d8SKevin Lo 			    sc->alc_cdata.alc_rx_ring_map);
2374068d8643SJohn Baldwin 		sc->alc_rdata.alc_rx_ring_paddr = 0;
23756ece67d8SKevin Lo 		sc->alc_rdata.alc_rx_ring = NULL;
23766ece67d8SKevin Lo 		bus_dma_tag_destroy(sc->alc_cdata.alc_rx_ring_tag);
23776ece67d8SKevin Lo 		sc->alc_cdata.alc_rx_ring_tag = NULL;
23786ece67d8SKevin Lo 	}
2379d68875ebSPyun YongHyeon 	/* Rx return ring. */
2380d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_rr_ring_tag != NULL) {
2381068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_rr_ring_paddr != 0)
2382d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_rr_ring_tag,
2383d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rr_ring_map);
2384068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_rr_ring != NULL)
2385d68875ebSPyun YongHyeon 			bus_dmamem_free(sc->alc_cdata.alc_rr_ring_tag,
2386d68875ebSPyun YongHyeon 			    sc->alc_rdata.alc_rr_ring,
2387d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rr_ring_map);
2388068d8643SJohn Baldwin 		sc->alc_rdata.alc_rr_ring_paddr = 0;
2389d68875ebSPyun YongHyeon 		sc->alc_rdata.alc_rr_ring = NULL;
2390d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_rr_ring_tag);
2391d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_rr_ring_tag = NULL;
2392d68875ebSPyun YongHyeon 	}
2393d68875ebSPyun YongHyeon 	/* CMB block */
2394d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_cmb_tag != NULL) {
2395068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_cmb_paddr != 0)
2396d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_cmb_tag,
2397d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_cmb_map);
2398068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_cmb != NULL)
2399d68875ebSPyun YongHyeon 			bus_dmamem_free(sc->alc_cdata.alc_cmb_tag,
2400d68875ebSPyun YongHyeon 			    sc->alc_rdata.alc_cmb,
2401d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_cmb_map);
2402068d8643SJohn Baldwin 		sc->alc_rdata.alc_cmb_paddr = 0;
2403d68875ebSPyun YongHyeon 		sc->alc_rdata.alc_cmb = NULL;
2404d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_cmb_tag);
2405d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_cmb_tag = NULL;
2406d68875ebSPyun YongHyeon 	}
2407d68875ebSPyun YongHyeon 	/* SMB block */
2408d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_smb_tag != NULL) {
2409068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_smb_paddr != 0)
2410d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_smb_tag,
2411d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_smb_map);
2412068d8643SJohn Baldwin 		if (sc->alc_rdata.alc_smb != NULL)
2413d68875ebSPyun YongHyeon 			bus_dmamem_free(sc->alc_cdata.alc_smb_tag,
2414d68875ebSPyun YongHyeon 			    sc->alc_rdata.alc_smb,
2415d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_smb_map);
2416068d8643SJohn Baldwin 		sc->alc_rdata.alc_smb_paddr = 0;
2417d68875ebSPyun YongHyeon 		sc->alc_rdata.alc_smb = NULL;
2418d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_smb_tag);
2419d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_smb_tag = NULL;
2420d68875ebSPyun YongHyeon 	}
2421d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_buffer_tag != NULL) {
2422d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_buffer_tag);
2423d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_buffer_tag = NULL;
2424d68875ebSPyun YongHyeon 	}
2425d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_parent_tag != NULL) {
2426d68875ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->alc_cdata.alc_parent_tag);
2427d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_parent_tag = NULL;
2428d68875ebSPyun YongHyeon 	}
2429d68875ebSPyun YongHyeon }
2430d68875ebSPyun YongHyeon 
2431d68875ebSPyun YongHyeon static int
alc_shutdown(device_t dev)2432d68875ebSPyun YongHyeon alc_shutdown(device_t dev)
2433d68875ebSPyun YongHyeon {
2434d68875ebSPyun YongHyeon 
2435d68875ebSPyun YongHyeon 	return (alc_suspend(dev));
2436d68875ebSPyun YongHyeon }
2437d68875ebSPyun YongHyeon 
2438d68875ebSPyun YongHyeon /*
2439d68875ebSPyun YongHyeon  * Note, this driver resets the link speed to 10/100Mbps by
2440d68875ebSPyun YongHyeon  * restarting auto-negotiation in suspend/shutdown phase but we
2441d68875ebSPyun YongHyeon  * don't know whether that auto-negotiation would succeed or not
2442d68875ebSPyun YongHyeon  * as driver has no control after powering off/suspend operation.
2443d68875ebSPyun YongHyeon  * If the renegotiation fail WOL may not work. Running at 1Gbps
2444d68875ebSPyun YongHyeon  * will draw more power than 375mA at 3.3V which is specified in
2445d68875ebSPyun YongHyeon  * PCI specification and that would result in complete
2446d68875ebSPyun YongHyeon  * shutdowning power to ethernet controller.
2447d68875ebSPyun YongHyeon  *
2448d68875ebSPyun YongHyeon  * TODO
2449d68875ebSPyun YongHyeon  * Save current negotiated media speed/duplex/flow-control to
2450d68875ebSPyun YongHyeon  * softc and restore the same link again after resuming. PHY
2451d68875ebSPyun YongHyeon  * handling such as power down/resetting to 100Mbps may be better
2452d68875ebSPyun YongHyeon  * handled in suspend method in phy driver.
2453d68875ebSPyun YongHyeon  */
2454d68875ebSPyun YongHyeon static void
alc_setlinkspeed(struct alc_softc * sc)2455d68875ebSPyun YongHyeon alc_setlinkspeed(struct alc_softc *sc)
2456d68875ebSPyun YongHyeon {
2457d68875ebSPyun YongHyeon 	struct mii_data *mii;
2458d68875ebSPyun YongHyeon 	int aneg, i;
2459d68875ebSPyun YongHyeon 
2460d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
2461d68875ebSPyun YongHyeon 	mii_pollstat(mii);
2462d68875ebSPyun YongHyeon 	aneg = 0;
2463d68875ebSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
2464d68875ebSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
2465d68875ebSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
2466d68875ebSPyun YongHyeon 		case IFM_10_T:
2467d68875ebSPyun YongHyeon 		case IFM_100_TX:
2468d68875ebSPyun YongHyeon 			return;
2469d68875ebSPyun YongHyeon 		case IFM_1000_T:
2470d68875ebSPyun YongHyeon 			aneg++;
2471d68875ebSPyun YongHyeon 			break;
2472d68875ebSPyun YongHyeon 		default:
2473d68875ebSPyun YongHyeon 			break;
2474d68875ebSPyun YongHyeon 		}
2475d68875ebSPyun YongHyeon 	}
2476d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, MII_100T2CR, 0);
2477d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
2478d68875ebSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
2479d68875ebSPyun YongHyeon 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
2480d68875ebSPyun YongHyeon 	    MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
2481d68875ebSPyun YongHyeon 	DELAY(1000);
2482d68875ebSPyun YongHyeon 	if (aneg != 0) {
2483d68875ebSPyun YongHyeon 		/*
2484d68875ebSPyun YongHyeon 		 * Poll link state until alc(4) get a 10/100Mbps link.
2485d68875ebSPyun YongHyeon 		 */
2486d68875ebSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
2487d68875ebSPyun YongHyeon 			mii_pollstat(mii);
2488d68875ebSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
2489d68875ebSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
2490d68875ebSPyun YongHyeon 				switch (IFM_SUBTYPE(
2491d68875ebSPyun YongHyeon 				    mii->mii_media_active)) {
2492d68875ebSPyun YongHyeon 				case IFM_10_T:
2493d68875ebSPyun YongHyeon 				case IFM_100_TX:
2494d68875ebSPyun YongHyeon 					alc_mac_config(sc);
2495d68875ebSPyun YongHyeon 					return;
2496d68875ebSPyun YongHyeon 				default:
2497d68875ebSPyun YongHyeon 					break;
2498d68875ebSPyun YongHyeon 				}
2499d68875ebSPyun YongHyeon 			}
2500d68875ebSPyun YongHyeon 			ALC_UNLOCK(sc);
2501d68875ebSPyun YongHyeon 			pause("alclnk", hz);
2502d68875ebSPyun YongHyeon 			ALC_LOCK(sc);
2503d68875ebSPyun YongHyeon 		}
2504d68875ebSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
2505d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
2506d68875ebSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
2507d68875ebSPyun YongHyeon 	}
2508d68875ebSPyun YongHyeon 	/*
2509d68875ebSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
2510d68875ebSPyun YongHyeon 	 * This is the last resort and may/may not work.
2511d68875ebSPyun YongHyeon 	 */
2512d68875ebSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
2513d68875ebSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
2514d68875ebSPyun YongHyeon 	alc_mac_config(sc);
2515d68875ebSPyun YongHyeon }
2516d68875ebSPyun YongHyeon 
2517d68875ebSPyun YongHyeon static void
alc_setwol(struct alc_softc * sc)2518d68875ebSPyun YongHyeon alc_setwol(struct alc_softc *sc)
2519d68875ebSPyun YongHyeon {
2520b624ef0aSPyun YongHyeon 
2521b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2522b624ef0aSPyun YongHyeon 		alc_setwol_816x(sc);
2523b624ef0aSPyun YongHyeon 	else
2524b624ef0aSPyun YongHyeon 		alc_setwol_813x(sc);
2525b624ef0aSPyun YongHyeon }
2526b624ef0aSPyun YongHyeon 
2527b624ef0aSPyun YongHyeon static void
alc_setwol_813x(struct alc_softc * sc)2528b624ef0aSPyun YongHyeon alc_setwol_813x(struct alc_softc *sc)
2529b624ef0aSPyun YongHyeon {
253052436412SJustin Hibbits 	if_t ifp;
253147ae892cSPyun YongHyeon 	uint32_t reg, pmcs;
2532d68875ebSPyun YongHyeon 
2533d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
2534d68875ebSPyun YongHyeon 
2535d68875ebSPyun YongHyeon 	alc_disable_l0s_l1(sc);
253647ae892cSPyun YongHyeon 	ifp = sc->alc_ifp;
2537a4d3574cSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
2538d68875ebSPyun YongHyeon 		/* Disable WOL. */
2539d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2540d68875ebSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
2541d68875ebSPyun YongHyeon 		reg |= PCIE_PHYMISC_FORCE_RCV_DET;
2542d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
2543d68875ebSPyun YongHyeon 		/* Force PHY power down. */
2544d68875ebSPyun YongHyeon 		alc_phy_down(sc);
254547ae892cSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
254647ae892cSPyun YongHyeon 		    CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
2547d68875ebSPyun YongHyeon 		return;
2548d68875ebSPyun YongHyeon 	}
2549d68875ebSPyun YongHyeon 
255052436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
2551d68875ebSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
2552d68875ebSPyun YongHyeon 			alc_setlinkspeed(sc);
255347ae892cSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
255447ae892cSPyun YongHyeon 		    CSR_READ_4(sc, ALC_MASTER_CFG) & ~MASTER_CLK_SEL_DIS);
2555d68875ebSPyun YongHyeon 	}
2556d68875ebSPyun YongHyeon 
2557d68875ebSPyun YongHyeon 	pmcs = 0;
255852436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
2559d68875ebSPyun YongHyeon 		pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
2560d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
2561d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
2562d68875ebSPyun YongHyeon 	reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
2563d68875ebSPyun YongHyeon 	    MAC_CFG_BCAST);
256452436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
2565d68875ebSPyun YongHyeon 		reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
256652436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
2567d68875ebSPyun YongHyeon 		reg |= MAC_CFG_RX_ENB;
2568d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2569d68875ebSPyun YongHyeon 
2570d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
2571d68875ebSPyun YongHyeon 	reg |= PCIE_PHYMISC_FORCE_RCV_DET;
2572d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
257352436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) == 0) {
2574d68875ebSPyun YongHyeon 		/* WOL disabled, PHY power down. */
2575d68875ebSPyun YongHyeon 		alc_phy_down(sc);
257647ae892cSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
257747ae892cSPyun YongHyeon 		    CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
2578d68875ebSPyun YongHyeon 	}
2579d68875ebSPyun YongHyeon 	/* Request PME. */
258052436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
2581*ddaf6524SJohn Baldwin 		pci_enable_pme(sc->alc_dev);
2582d68875ebSPyun YongHyeon }
2583d68875ebSPyun YongHyeon 
2584b624ef0aSPyun YongHyeon static void
alc_setwol_816x(struct alc_softc * sc)2585b624ef0aSPyun YongHyeon alc_setwol_816x(struct alc_softc *sc)
2586b624ef0aSPyun YongHyeon {
258752436412SJustin Hibbits 	if_t ifp;
2588b624ef0aSPyun YongHyeon 	uint32_t gphy, mac, master, pmcs, reg;
2589b624ef0aSPyun YongHyeon 
2590b624ef0aSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
2591b624ef0aSPyun YongHyeon 
2592b624ef0aSPyun YongHyeon 	ifp = sc->alc_ifp;
2593b624ef0aSPyun YongHyeon 	master = CSR_READ_4(sc, ALC_MASTER_CFG);
2594b624ef0aSPyun YongHyeon 	master &= ~MASTER_CLK_SEL_DIS;
2595b624ef0aSPyun YongHyeon 	gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
2596b624ef0aSPyun YongHyeon 	gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE | GPHY_CFG_100AB_ENB |
2597b624ef0aSPyun YongHyeon 	    GPHY_CFG_PHY_PLL_ON);
2598b624ef0aSPyun YongHyeon 	gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE | GPHY_CFG_SEL_ANA_RESET;
2599b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
2600b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2601b624ef0aSPyun YongHyeon 		gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
2602b624ef0aSPyun YongHyeon 		mac = CSR_READ_4(sc, ALC_MAC_CFG);
2603b624ef0aSPyun YongHyeon 	} else {
260452436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
2605b624ef0aSPyun YongHyeon 			gphy |= GPHY_CFG_EXT_RESET;
2606b624ef0aSPyun YongHyeon 			if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
2607b624ef0aSPyun YongHyeon 				alc_setlinkspeed(sc);
2608b624ef0aSPyun YongHyeon 		}
2609b624ef0aSPyun YongHyeon 		pmcs = 0;
261052436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
2611b624ef0aSPyun YongHyeon 			pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
2612b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
2613b624ef0aSPyun YongHyeon 		mac = CSR_READ_4(sc, ALC_MAC_CFG);
2614b624ef0aSPyun YongHyeon 		mac &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
2615b624ef0aSPyun YongHyeon 		    MAC_CFG_BCAST);
261652436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
2617b624ef0aSPyun YongHyeon 			mac |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
261852436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
2619b624ef0aSPyun YongHyeon 			mac |= MAC_CFG_RX_ENB;
2620b624ef0aSPyun YongHyeon 		alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
2621b624ef0aSPyun YongHyeon 		    ANEG_S3DIG10_SL);
2622b624ef0aSPyun YongHyeon 	}
2623b624ef0aSPyun YongHyeon 
2624b624ef0aSPyun YongHyeon 	/* Enable OSC. */
2625b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MISC);
2626b624ef0aSPyun YongHyeon 	reg &= ~MISC_INTNLOSC_OPEN;
2627b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MISC, reg);
2628b624ef0aSPyun YongHyeon 	reg |= MISC_INTNLOSC_OPEN;
2629b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MISC, reg);
2630b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MASTER_CFG, master);
2631b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, mac);
2632b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
2633b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_PDLL_TRNS1);
2634b624ef0aSPyun YongHyeon 	reg |= PDLL_TRNS1_D3PLLOFF_ENB;
2635b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PDLL_TRNS1, reg);
2636b624ef0aSPyun YongHyeon 
2637b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_PM) != 0) {
2638b624ef0aSPyun YongHyeon 		/* Request PME. */
263952436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
2640*ddaf6524SJohn Baldwin 			pci_enable_pme(sc->alc_dev);
2641b624ef0aSPyun YongHyeon 	}
2642b624ef0aSPyun YongHyeon }
2643b624ef0aSPyun YongHyeon 
2644d68875ebSPyun YongHyeon static int
alc_suspend(device_t dev)2645d68875ebSPyun YongHyeon alc_suspend(device_t dev)
2646d68875ebSPyun YongHyeon {
2647d68875ebSPyun YongHyeon 	struct alc_softc *sc;
2648d68875ebSPyun YongHyeon 
2649d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
2650d68875ebSPyun YongHyeon 
2651d68875ebSPyun YongHyeon 	ALC_LOCK(sc);
2652d68875ebSPyun YongHyeon 	alc_stop(sc);
2653d68875ebSPyun YongHyeon 	alc_setwol(sc);
2654d68875ebSPyun YongHyeon 	ALC_UNLOCK(sc);
2655d68875ebSPyun YongHyeon 
2656d68875ebSPyun YongHyeon 	return (0);
2657d68875ebSPyun YongHyeon }
2658d68875ebSPyun YongHyeon 
2659d68875ebSPyun YongHyeon static int
alc_resume(device_t dev)2660d68875ebSPyun YongHyeon alc_resume(device_t dev)
2661d68875ebSPyun YongHyeon {
2662d68875ebSPyun YongHyeon 	struct alc_softc *sc;
266352436412SJustin Hibbits 	if_t ifp;
2664d68875ebSPyun YongHyeon 
2665d68875ebSPyun YongHyeon 	sc = device_get_softc(dev);
2666d68875ebSPyun YongHyeon 
2667d68875ebSPyun YongHyeon 	/* Reset PHY. */
2668*ddaf6524SJohn Baldwin 	ALC_LOCK(sc);
2669d68875ebSPyun YongHyeon 	alc_phy_reset(sc);
2670d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
267152436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_UP) != 0) {
267252436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2673d68875ebSPyun YongHyeon 		alc_init_locked(sc);
2674d68875ebSPyun YongHyeon 	}
2675d68875ebSPyun YongHyeon 	ALC_UNLOCK(sc);
2676d68875ebSPyun YongHyeon 
2677d68875ebSPyun YongHyeon 	return (0);
2678d68875ebSPyun YongHyeon }
2679d68875ebSPyun YongHyeon 
2680d68875ebSPyun YongHyeon static int
alc_encap(struct alc_softc * sc,struct mbuf ** m_head)2681d68875ebSPyun YongHyeon alc_encap(struct alc_softc *sc, struct mbuf **m_head)
2682d68875ebSPyun YongHyeon {
2683d68875ebSPyun YongHyeon 	struct alc_txdesc *txd, *txd_last;
2684d68875ebSPyun YongHyeon 	struct tx_desc *desc;
2685d68875ebSPyun YongHyeon 	struct mbuf *m;
2686d68875ebSPyun YongHyeon 	struct ip *ip;
2687d68875ebSPyun YongHyeon 	struct tcphdr *tcp;
2688d68875ebSPyun YongHyeon 	bus_dma_segment_t txsegs[ALC_MAXTXSEGS];
2689d68875ebSPyun YongHyeon 	bus_dmamap_t map;
2690cb2f3e7fSPyun YongHyeon 	uint32_t cflags, hdrlen, ip_off, poff, vtag;
2691d68875ebSPyun YongHyeon 	int error, idx, nsegs, prod;
2692d68875ebSPyun YongHyeon 
2693d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
2694d68875ebSPyun YongHyeon 
2695d68875ebSPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
2696d68875ebSPyun YongHyeon 
2697d68875ebSPyun YongHyeon 	m = *m_head;
2698d68875ebSPyun YongHyeon 	ip = NULL;
2699d68875ebSPyun YongHyeon 	tcp = NULL;
2700cb2f3e7fSPyun YongHyeon 	ip_off = poff = 0;
2701d68875ebSPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & (ALC_CSUM_FEATURES | CSUM_TSO)) != 0) {
2702d68875ebSPyun YongHyeon 		/*
2703b624ef0aSPyun YongHyeon 		 * AR81[3567]x requires offset of TCP/UDP header in its
2704d68875ebSPyun YongHyeon 		 * Tx descriptor to perform Tx checksum offloading. TSO
2705d68875ebSPyun YongHyeon 		 * also requires TCP header offset and modification of
2706d68875ebSPyun YongHyeon 		 * IP/TCP header. This kind of operation takes many CPU
2707d68875ebSPyun YongHyeon 		 * cycles on FreeBSD so fast host CPU is required to get
2708d68875ebSPyun YongHyeon 		 * smooth TSO performance.
2709d68875ebSPyun YongHyeon 		 */
2710cb2f3e7fSPyun YongHyeon 		struct ether_header *eh;
2711d68875ebSPyun YongHyeon 
2712d68875ebSPyun YongHyeon 		if (M_WRITABLE(m) == 0) {
2713d68875ebSPyun YongHyeon 			/* Get a writable copy. */
2714c6499eccSGleb Smirnoff 			m = m_dup(*m_head, M_NOWAIT);
2715d68875ebSPyun YongHyeon 			/* Release original mbufs. */
2716d68875ebSPyun YongHyeon 			m_freem(*m_head);
2717d68875ebSPyun YongHyeon 			if (m == NULL) {
2718d68875ebSPyun YongHyeon 				*m_head = NULL;
2719d68875ebSPyun YongHyeon 				return (ENOBUFS);
2720d68875ebSPyun YongHyeon 			}
2721d68875ebSPyun YongHyeon 			*m_head = m;
2722d68875ebSPyun YongHyeon 		}
2723d68875ebSPyun YongHyeon 
2724cb2f3e7fSPyun YongHyeon 		ip_off = sizeof(struct ether_header);
2725cb2f3e7fSPyun YongHyeon 		m = m_pullup(m, ip_off);
2726d68875ebSPyun YongHyeon 		if (m == NULL) {
2727d68875ebSPyun YongHyeon 			*m_head = NULL;
2728d68875ebSPyun YongHyeon 			return (ENOBUFS);
2729d68875ebSPyun YongHyeon 		}
2730cb2f3e7fSPyun YongHyeon 		eh = mtod(m, struct ether_header *);
2731cb2f3e7fSPyun YongHyeon 		/*
2732cb2f3e7fSPyun YongHyeon 		 * Check if hardware VLAN insertion is off.
2733cb2f3e7fSPyun YongHyeon 		 * Additional check for LLC/SNAP frame?
2734cb2f3e7fSPyun YongHyeon 		 */
2735cb2f3e7fSPyun YongHyeon 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2736cb2f3e7fSPyun YongHyeon 			ip_off = sizeof(struct ether_vlan_header);
2737cb2f3e7fSPyun YongHyeon 			m = m_pullup(m, ip_off);
2738cb2f3e7fSPyun YongHyeon 			if (m == NULL) {
2739cb2f3e7fSPyun YongHyeon 				*m_head = NULL;
2740cb2f3e7fSPyun YongHyeon 				return (ENOBUFS);
2741cb2f3e7fSPyun YongHyeon 			}
2742cb2f3e7fSPyun YongHyeon 		}
2743cb2f3e7fSPyun YongHyeon 		m = m_pullup(m, ip_off + sizeof(struct ip));
2744cb2f3e7fSPyun YongHyeon 		if (m == NULL) {
2745cb2f3e7fSPyun YongHyeon 			*m_head = NULL;
2746cb2f3e7fSPyun YongHyeon 			return (ENOBUFS);
2747cb2f3e7fSPyun YongHyeon 		}
2748cb2f3e7fSPyun YongHyeon 		ip = (struct ip *)(mtod(m, char *) + ip_off);
2749cb2f3e7fSPyun YongHyeon 		poff = ip_off + (ip->ip_hl << 2);
2750d68875ebSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2751d68875ebSPyun YongHyeon 			m = m_pullup(m, poff + sizeof(struct tcphdr));
2752d68875ebSPyun YongHyeon 			if (m == NULL) {
2753d68875ebSPyun YongHyeon 				*m_head = NULL;
2754d68875ebSPyun YongHyeon 				return (ENOBUFS);
2755d68875ebSPyun YongHyeon 			}
2756d68875ebSPyun YongHyeon 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2757d68875ebSPyun YongHyeon 			m = m_pullup(m, poff + (tcp->th_off << 2));
2758d68875ebSPyun YongHyeon 			if (m == NULL) {
2759d68875ebSPyun YongHyeon 				*m_head = NULL;
2760d68875ebSPyun YongHyeon 				return (ENOBUFS);
2761d68875ebSPyun YongHyeon 			}
2762d68875ebSPyun YongHyeon 			/*
2763d68875ebSPyun YongHyeon 			 * Due to strict adherence of Microsoft NDIS
2764d68875ebSPyun YongHyeon 			 * Large Send specification, hardware expects
2765d68875ebSPyun YongHyeon 			 * a pseudo TCP checksum inserted by upper
2766d68875ebSPyun YongHyeon 			 * stack. Unfortunately the pseudo TCP
2767d68875ebSPyun YongHyeon 			 * checksum that NDIS refers to does not include
2768d68875ebSPyun YongHyeon 			 * TCP payload length so driver should recompute
2769d68875ebSPyun YongHyeon 			 * the pseudo checksum here. Hopefully this
2770d68875ebSPyun YongHyeon 			 * wouldn't be much burden on modern CPUs.
2771d68875ebSPyun YongHyeon 			 *
2772d68875ebSPyun YongHyeon 			 * Reset IP checksum and recompute TCP pseudo
2773d68875ebSPyun YongHyeon 			 * checksum as NDIS specification said.
2774d68875ebSPyun YongHyeon 			 */
277596486faaSPyun YongHyeon 			ip = (struct ip *)(mtod(m, char *) + ip_off);
277696486faaSPyun YongHyeon 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2777d68875ebSPyun YongHyeon 			ip->ip_sum = 0;
2778d68875ebSPyun YongHyeon 			tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
2779d68875ebSPyun YongHyeon 			    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
2780d68875ebSPyun YongHyeon 		}
2781d68875ebSPyun YongHyeon 		*m_head = m;
2782d68875ebSPyun YongHyeon 	}
2783d68875ebSPyun YongHyeon 
2784d68875ebSPyun YongHyeon 	prod = sc->alc_cdata.alc_tx_prod;
2785d68875ebSPyun YongHyeon 	txd = &sc->alc_cdata.alc_txdesc[prod];
2786d68875ebSPyun YongHyeon 	txd_last = txd;
2787d68875ebSPyun YongHyeon 	map = txd->tx_dmamap;
2788d68875ebSPyun YongHyeon 
2789d68875ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2790d68875ebSPyun YongHyeon 	    *m_head, txsegs, &nsegs, 0);
2791d68875ebSPyun YongHyeon 	if (error == EFBIG) {
2792c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, ALC_MAXTXSEGS);
2793d68875ebSPyun YongHyeon 		if (m == NULL) {
2794d68875ebSPyun YongHyeon 			m_freem(*m_head);
2795d68875ebSPyun YongHyeon 			*m_head = NULL;
2796d68875ebSPyun YongHyeon 			return (ENOMEM);
2797d68875ebSPyun YongHyeon 		}
2798d68875ebSPyun YongHyeon 		*m_head = m;
2799d68875ebSPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2800d68875ebSPyun YongHyeon 		    *m_head, txsegs, &nsegs, 0);
2801d68875ebSPyun YongHyeon 		if (error != 0) {
2802d68875ebSPyun YongHyeon 			m_freem(*m_head);
2803d68875ebSPyun YongHyeon 			*m_head = NULL;
2804d68875ebSPyun YongHyeon 			return (error);
2805d68875ebSPyun YongHyeon 		}
2806d68875ebSPyun YongHyeon 	} else if (error != 0)
2807d68875ebSPyun YongHyeon 		return (error);
2808d68875ebSPyun YongHyeon 	if (nsegs == 0) {
2809d68875ebSPyun YongHyeon 		m_freem(*m_head);
2810d68875ebSPyun YongHyeon 		*m_head = NULL;
2811d68875ebSPyun YongHyeon 		return (EIO);
2812d68875ebSPyun YongHyeon 	}
2813d68875ebSPyun YongHyeon 
2814d68875ebSPyun YongHyeon 	/* Check descriptor overrun. */
2815d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
2816d68875ebSPyun YongHyeon 		bus_dmamap_unload(sc->alc_cdata.alc_tx_tag, map);
2817d68875ebSPyun YongHyeon 		return (ENOBUFS);
2818d68875ebSPyun YongHyeon 	}
2819d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_tx_tag, map, BUS_DMASYNC_PREWRITE);
2820d68875ebSPyun YongHyeon 
2821d68875ebSPyun YongHyeon 	m = *m_head;
2822d68875ebSPyun YongHyeon 	cflags = TD_ETHERNET;
2823d68875ebSPyun YongHyeon 	vtag = 0;
2824d68875ebSPyun YongHyeon 	desc = NULL;
2825d68875ebSPyun YongHyeon 	idx = 0;
2826d68875ebSPyun YongHyeon 	/* Configure VLAN hardware tag insertion. */
2827d68875ebSPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0) {
2828d68875ebSPyun YongHyeon 		vtag = htons(m->m_pkthdr.ether_vtag);
2829d68875ebSPyun YongHyeon 		vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
2830d68875ebSPyun YongHyeon 		cflags |= TD_INS_VLAN_TAG;
2831d68875ebSPyun YongHyeon 	}
28326da6d0a9SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2833d68875ebSPyun YongHyeon 		/* Request TSO and set MSS. */
2834d68875ebSPyun YongHyeon 		cflags |= TD_TSO | TD_TSO_DESCV1;
2835d68875ebSPyun YongHyeon 		cflags |= ((uint32_t)m->m_pkthdr.tso_segsz << TD_MSS_SHIFT) &
2836d68875ebSPyun YongHyeon 		    TD_MSS_MASK;
2837d68875ebSPyun YongHyeon 		/* Set TCP header offset. */
2838d68875ebSPyun YongHyeon 		cflags |= (poff << TD_TCPHDR_OFFSET_SHIFT) &
2839d68875ebSPyun YongHyeon 		    TD_TCPHDR_OFFSET_MASK;
2840d68875ebSPyun YongHyeon 		/*
2841b624ef0aSPyun YongHyeon 		 * AR81[3567]x requires the first buffer should
2842d68875ebSPyun YongHyeon 		 * only hold IP/TCP header data. Payload should
2843d68875ebSPyun YongHyeon 		 * be handled in other descriptors.
2844d68875ebSPyun YongHyeon 		 */
2845d68875ebSPyun YongHyeon 		hdrlen = poff + (tcp->th_off << 2);
2846d68875ebSPyun YongHyeon 		desc = &sc->alc_rdata.alc_tx_ring[prod];
2847d68875ebSPyun YongHyeon 		desc->len = htole32(TX_BYTES(hdrlen | vtag));
2848d68875ebSPyun YongHyeon 		desc->flags = htole32(cflags);
2849d68875ebSPyun YongHyeon 		desc->addr = htole64(txsegs[0].ds_addr);
2850d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_tx_cnt++;
2851d68875ebSPyun YongHyeon 		ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2852d68875ebSPyun YongHyeon 		if (m->m_len - hdrlen > 0) {
2853d68875ebSPyun YongHyeon 			/* Handle remaining payload of the first fragment. */
2854d68875ebSPyun YongHyeon 			desc = &sc->alc_rdata.alc_tx_ring[prod];
2855d68875ebSPyun YongHyeon 			desc->len = htole32(TX_BYTES((m->m_len - hdrlen) |
2856d68875ebSPyun YongHyeon 			    vtag));
2857d68875ebSPyun YongHyeon 			desc->flags = htole32(cflags);
2858d68875ebSPyun YongHyeon 			desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
2859d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_tx_cnt++;
2860d68875ebSPyun YongHyeon 			ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2861d68875ebSPyun YongHyeon 		}
2862d68875ebSPyun YongHyeon 		/* Handle remaining fragments. */
2863d68875ebSPyun YongHyeon 		idx = 1;
28646da6d0a9SPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
28656da6d0a9SPyun YongHyeon 		/* Configure Tx checksum offload. */
28666da6d0a9SPyun YongHyeon #ifdef ALC_USE_CUSTOM_CSUM
28676da6d0a9SPyun YongHyeon 		cflags |= TD_CUSTOM_CSUM;
28686da6d0a9SPyun YongHyeon 		/* Set checksum start offset. */
28696da6d0a9SPyun YongHyeon 		cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
28706da6d0a9SPyun YongHyeon 		    TD_PLOAD_OFFSET_MASK;
28716da6d0a9SPyun YongHyeon 		/* Set checksum insertion position of TCP/UDP. */
28726da6d0a9SPyun YongHyeon 		cflags |= (((poff + m->m_pkthdr.csum_data) >> 1) <<
28736da6d0a9SPyun YongHyeon 		    TD_CUSTOM_CSUM_OFFSET_SHIFT) & TD_CUSTOM_CSUM_OFFSET_MASK;
28746da6d0a9SPyun YongHyeon #else
28756da6d0a9SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
28766da6d0a9SPyun YongHyeon 			cflags |= TD_IPCSUM;
28776da6d0a9SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
28786da6d0a9SPyun YongHyeon 			cflags |= TD_TCPCSUM;
28796da6d0a9SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
28806da6d0a9SPyun YongHyeon 			cflags |= TD_UDPCSUM;
28816da6d0a9SPyun YongHyeon 		/* Set TCP/UDP header offset. */
28826da6d0a9SPyun YongHyeon 		cflags |= (poff << TD_L4HDR_OFFSET_SHIFT) &
28836da6d0a9SPyun YongHyeon 		    TD_L4HDR_OFFSET_MASK;
28846da6d0a9SPyun YongHyeon #endif
2885d68875ebSPyun YongHyeon 	}
2886d68875ebSPyun YongHyeon 	for (; idx < nsegs; idx++) {
2887d68875ebSPyun YongHyeon 		desc = &sc->alc_rdata.alc_tx_ring[prod];
2888d68875ebSPyun YongHyeon 		desc->len = htole32(TX_BYTES(txsegs[idx].ds_len) | vtag);
2889d68875ebSPyun YongHyeon 		desc->flags = htole32(cflags);
2890d68875ebSPyun YongHyeon 		desc->addr = htole64(txsegs[idx].ds_addr);
2891d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_tx_cnt++;
2892d68875ebSPyun YongHyeon 		ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2893d68875ebSPyun YongHyeon 	}
2894d68875ebSPyun YongHyeon 	/* Update producer index. */
2895d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_tx_prod = prod;
2896d68875ebSPyun YongHyeon 
2897d68875ebSPyun YongHyeon 	/* Finally set EOP on the last descriptor. */
2898d68875ebSPyun YongHyeon 	prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
2899d68875ebSPyun YongHyeon 	desc = &sc->alc_rdata.alc_tx_ring[prod];
2900d68875ebSPyun YongHyeon 	desc->flags |= htole32(TD_EOP);
2901d68875ebSPyun YongHyeon 
2902d68875ebSPyun YongHyeon 	/* Swap dmamap of the first and the last. */
2903d68875ebSPyun YongHyeon 	txd = &sc->alc_cdata.alc_txdesc[prod];
2904d68875ebSPyun YongHyeon 	map = txd_last->tx_dmamap;
2905d68875ebSPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
2906d68875ebSPyun YongHyeon 	txd->tx_dmamap = map;
2907d68875ebSPyun YongHyeon 	txd->tx_m = m;
2908d68875ebSPyun YongHyeon 
2909d68875ebSPyun YongHyeon 	return (0);
2910d68875ebSPyun YongHyeon }
2911d68875ebSPyun YongHyeon 
2912d68875ebSPyun YongHyeon static void
alc_start(if_t ifp)291352436412SJustin Hibbits alc_start(if_t ifp)
2914d68875ebSPyun YongHyeon {
291532341ad6SJohn Baldwin 	struct alc_softc *sc;
2916d68875ebSPyun YongHyeon 
291752436412SJustin Hibbits 	sc = if_getsoftc(ifp);
291832341ad6SJohn Baldwin 	ALC_LOCK(sc);
291932341ad6SJohn Baldwin 	alc_start_locked(ifp);
292032341ad6SJohn Baldwin 	ALC_UNLOCK(sc);
2921d68875ebSPyun YongHyeon }
2922d68875ebSPyun YongHyeon 
2923d68875ebSPyun YongHyeon static void
alc_start_locked(if_t ifp)292452436412SJustin Hibbits alc_start_locked(if_t ifp)
2925d68875ebSPyun YongHyeon {
2926d68875ebSPyun YongHyeon 	struct alc_softc *sc;
2927d68875ebSPyun YongHyeon 	struct mbuf *m_head;
2928d68875ebSPyun YongHyeon 	int enq;
2929d68875ebSPyun YongHyeon 
293052436412SJustin Hibbits 	sc = if_getsoftc(ifp);
2931d68875ebSPyun YongHyeon 
293232341ad6SJohn Baldwin 	ALC_LOCK_ASSERT(sc);
2933d68875ebSPyun YongHyeon 
2934d68875ebSPyun YongHyeon 	/* Reclaim transmitted frames. */
2935d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
2936d68875ebSPyun YongHyeon 		alc_txeof(sc);
2937d68875ebSPyun YongHyeon 
293852436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
293932341ad6SJohn Baldwin 	    IFF_DRV_RUNNING || (sc->alc_flags & ALC_FLAG_LINK) == 0)
2940d68875ebSPyun YongHyeon 		return;
2941d68875ebSPyun YongHyeon 
294252436412SJustin Hibbits 	for (enq = 0; !if_sendq_empty(ifp); ) {
294352436412SJustin Hibbits 		m_head = if_dequeue(ifp);
2944d68875ebSPyun YongHyeon 		if (m_head == NULL)
2945d68875ebSPyun YongHyeon 			break;
2946d68875ebSPyun YongHyeon 		/*
2947d68875ebSPyun YongHyeon 		 * Pack the data into the transmit ring. If we
2948d68875ebSPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
2949d68875ebSPyun YongHyeon 		 * for the NIC to drain the ring.
2950d68875ebSPyun YongHyeon 		 */
2951d68875ebSPyun YongHyeon 		if (alc_encap(sc, &m_head)) {
2952d68875ebSPyun YongHyeon 			if (m_head == NULL)
2953d68875ebSPyun YongHyeon 				break;
295452436412SJustin Hibbits 			if_sendq_prepend(ifp, m_head);
295552436412SJustin Hibbits 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
2956d68875ebSPyun YongHyeon 			break;
2957d68875ebSPyun YongHyeon 		}
2958d68875ebSPyun YongHyeon 
2959d68875ebSPyun YongHyeon 		enq++;
2960d68875ebSPyun YongHyeon 		/*
2961d68875ebSPyun YongHyeon 		 * If there's a BPF listener, bounce a copy of this frame
2962d68875ebSPyun YongHyeon 		 * to him.
2963d68875ebSPyun YongHyeon 		 */
2964d68875ebSPyun YongHyeon 		ETHER_BPF_MTAP(ifp, m_head);
2965d68875ebSPyun YongHyeon 	}
2966d68875ebSPyun YongHyeon 
29678a466583SMark Johnston 	if (enq > 0)
29688a466583SMark Johnston 		alc_start_tx(sc);
29698a466583SMark Johnston }
29708a466583SMark Johnston 
29718a466583SMark Johnston static void
alc_start_tx(struct alc_softc * sc)29728a466583SMark Johnston alc_start_tx(struct alc_softc *sc)
29738a466583SMark Johnston {
29748a466583SMark Johnston 
2975d68875ebSPyun YongHyeon 	/* Sync descriptors. */
2976d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2977d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
2978d68875ebSPyun YongHyeon 	/* Kick. Assume we're using normal Tx priority queue. */
2979b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2980b624ef0aSPyun YongHyeon 		CSR_WRITE_2(sc, ALC_MBOX_TD_PRI0_PROD_IDX,
2981b624ef0aSPyun YongHyeon 		    (uint16_t)sc->alc_cdata.alc_tx_prod);
2982b624ef0aSPyun YongHyeon 	else
2983d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2984d68875ebSPyun YongHyeon 		    (sc->alc_cdata.alc_tx_prod <<
2985d68875ebSPyun YongHyeon 		    MBOX_TD_PROD_LO_IDX_SHIFT) &
2986d68875ebSPyun YongHyeon 		    MBOX_TD_PROD_LO_IDX_MASK);
2987d68875ebSPyun YongHyeon 	/* Set a timeout in case the chip goes out to lunch. */
2988d68875ebSPyun YongHyeon 	sc->alc_watchdog_timer = ALC_TX_TIMEOUT;
2989d68875ebSPyun YongHyeon }
2990d68875ebSPyun YongHyeon 
2991d68875ebSPyun YongHyeon static void
alc_watchdog(struct alc_softc * sc)2992d68875ebSPyun YongHyeon alc_watchdog(struct alc_softc *sc)
2993d68875ebSPyun YongHyeon {
299452436412SJustin Hibbits 	if_t ifp;
2995d68875ebSPyun YongHyeon 
2996d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
2997d68875ebSPyun YongHyeon 
2998d68875ebSPyun YongHyeon 	if (sc->alc_watchdog_timer == 0 || --sc->alc_watchdog_timer)
2999d68875ebSPyun YongHyeon 		return;
3000d68875ebSPyun YongHyeon 
3001d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3002d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
3003d68875ebSPyun YongHyeon 		if_printf(sc->alc_ifp, "watchdog timeout (lost link)\n");
30049bce9009SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
300552436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3006d68875ebSPyun YongHyeon 		alc_init_locked(sc);
3007d68875ebSPyun YongHyeon 		return;
3008d68875ebSPyun YongHyeon 	}
3009d68875ebSPyun YongHyeon 	if_printf(sc->alc_ifp, "watchdog timeout -- resetting\n");
30109bce9009SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
301152436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3012d68875ebSPyun YongHyeon 	alc_init_locked(sc);
301352436412SJustin Hibbits 	if (!if_sendq_empty(ifp))
301432341ad6SJohn Baldwin 		alc_start_locked(ifp);
3015d68875ebSPyun YongHyeon }
3016d68875ebSPyun YongHyeon 
3017d68875ebSPyun YongHyeon static int
alc_ioctl(if_t ifp,u_long cmd,caddr_t data)301852436412SJustin Hibbits alc_ioctl(if_t ifp, u_long cmd, caddr_t data)
3019d68875ebSPyun YongHyeon {
3020d68875ebSPyun YongHyeon 	struct alc_softc *sc;
3021d68875ebSPyun YongHyeon 	struct ifreq *ifr;
3022d68875ebSPyun YongHyeon 	struct mii_data *mii;
3023d68875ebSPyun YongHyeon 	int error, mask;
3024d68875ebSPyun YongHyeon 
302552436412SJustin Hibbits 	sc = if_getsoftc(ifp);
3026d68875ebSPyun YongHyeon 	ifr = (struct ifreq *)data;
3027d68875ebSPyun YongHyeon 	error = 0;
3028d68875ebSPyun YongHyeon 	switch (cmd) {
3029d68875ebSPyun YongHyeon 	case SIOCSIFMTU:
30302f70cceaSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
30312f70cceaSPyun YongHyeon 		    ifr->ifr_mtu > (sc->alc_ident->max_framelen -
30322f70cceaSPyun YongHyeon 		    sizeof(struct ether_vlan_header) - ETHER_CRC_LEN) ||
3033d68875ebSPyun YongHyeon 		    ((sc->alc_flags & ALC_FLAG_JUMBO) == 0 &&
3034d68875ebSPyun YongHyeon 		    ifr->ifr_mtu > ETHERMTU))
3035d68875ebSPyun YongHyeon 			error = EINVAL;
303652436412SJustin Hibbits 		else if (if_getmtu(ifp) != ifr->ifr_mtu) {
3037d68875ebSPyun YongHyeon 			ALC_LOCK(sc);
303852436412SJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
3039b624ef0aSPyun YongHyeon 			/* AR81[3567]x has 13 bits MSS field. */
304052436412SJustin Hibbits 			if (if_getmtu(ifp) > ALC_TSO_MTU &&
304152436412SJustin Hibbits 			    (if_getcapenable(ifp) & IFCAP_TSO4) != 0) {
304252436412SJustin Hibbits 				if_setcapenablebit(ifp, 0, IFCAP_TSO4);
304352436412SJustin Hibbits 				if_sethwassistbits(ifp, 0, CSUM_TSO);
3044e67344a3SPyun YongHyeon 				VLAN_CAPABILITIES(ifp);
3045d68875ebSPyun YongHyeon 			}
3046d68875ebSPyun YongHyeon 			ALC_UNLOCK(sc);
3047d68875ebSPyun YongHyeon 		}
3048d68875ebSPyun YongHyeon 		break;
3049d68875ebSPyun YongHyeon 	case SIOCSIFFLAGS:
3050d68875ebSPyun YongHyeon 		ALC_LOCK(sc);
305152436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_UP) != 0) {
305252436412SJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
305352436412SJustin Hibbits 			    ((if_getflags(ifp) ^ sc->alc_if_flags) &
3054d68875ebSPyun YongHyeon 			    (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3055d68875ebSPyun YongHyeon 				alc_rxfilter(sc);
30563b33d630SJohn Baldwin 			else
3057d68875ebSPyun YongHyeon 				alc_init_locked(sc);
305852436412SJustin Hibbits 		} else if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3059d68875ebSPyun YongHyeon 			alc_stop(sc);
306052436412SJustin Hibbits 		sc->alc_if_flags = if_getflags(ifp);
3061d68875ebSPyun YongHyeon 		ALC_UNLOCK(sc);
3062d68875ebSPyun YongHyeon 		break;
3063d68875ebSPyun YongHyeon 	case SIOCADDMULTI:
3064d68875ebSPyun YongHyeon 	case SIOCDELMULTI:
3065d68875ebSPyun YongHyeon 		ALC_LOCK(sc);
306652436412SJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3067d68875ebSPyun YongHyeon 			alc_rxfilter(sc);
3068d68875ebSPyun YongHyeon 		ALC_UNLOCK(sc);
3069d68875ebSPyun YongHyeon 		break;
3070d68875ebSPyun YongHyeon 	case SIOCSIFMEDIA:
3071d68875ebSPyun YongHyeon 	case SIOCGIFMEDIA:
3072d68875ebSPyun YongHyeon 		mii = device_get_softc(sc->alc_miibus);
3073d68875ebSPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
3074d68875ebSPyun YongHyeon 		break;
3075d68875ebSPyun YongHyeon 	case SIOCSIFCAP:
3076d68875ebSPyun YongHyeon 		ALC_LOCK(sc);
307752436412SJustin Hibbits 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
3078d68875ebSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
307952436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
308052436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TXCSUM);
308152436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
308252436412SJustin Hibbits 				if_sethwassistbits(ifp, ALC_CSUM_FEATURES, 0);
3083d68875ebSPyun YongHyeon 			else
308452436412SJustin Hibbits 				if_sethwassistbits(ifp, 0, ALC_CSUM_FEATURES);
3085d68875ebSPyun YongHyeon 		}
3086d68875ebSPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
308752436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
308852436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TSO4);
308952436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0) {
3090b624ef0aSPyun YongHyeon 				/* AR81[3567]x has 13 bits MSS field. */
309152436412SJustin Hibbits 				if (if_getmtu(ifp) > ALC_TSO_MTU) {
309252436412SJustin Hibbits 					if_setcapenablebit(ifp, 0, IFCAP_TSO4);
309352436412SJustin Hibbits 					if_sethwassistbits(ifp, 0, CSUM_TSO);
3094d68875ebSPyun YongHyeon 				} else
309552436412SJustin Hibbits 					if_sethwassistbits(ifp, CSUM_TSO, 0);
3096d68875ebSPyun YongHyeon 			} else
309752436412SJustin Hibbits 				if_sethwassistbits(ifp, 0, CSUM_TSO);
3098d68875ebSPyun YongHyeon 		}
3099d68875ebSPyun YongHyeon 		if ((mask & IFCAP_WOL_MCAST) != 0 &&
310052436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_WOL_MCAST) != 0)
310152436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_WOL_MCAST);
3102d68875ebSPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
310352436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
310452436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
3105d68875ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
310652436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
310752436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
3108d68875ebSPyun YongHyeon 			alc_rxvlan(sc);
3109d68875ebSPyun YongHyeon 		}
3110d68875ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
311152436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
311252436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
3113d68875ebSPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
311452436412SJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
311552436412SJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
311652436412SJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
311752436412SJustin Hibbits 			if_setcapenablebit(ifp, 0,
311852436412SJustin Hibbits 			    IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
3119d68875ebSPyun YongHyeon 		ALC_UNLOCK(sc);
3120d68875ebSPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
3121d68875ebSPyun YongHyeon 		break;
3122d68875ebSPyun YongHyeon 	default:
3123d68875ebSPyun YongHyeon 		error = ether_ioctl(ifp, cmd, data);
3124d68875ebSPyun YongHyeon 		break;
3125d68875ebSPyun YongHyeon 	}
3126d68875ebSPyun YongHyeon 
3127d68875ebSPyun YongHyeon 	return (error);
3128d68875ebSPyun YongHyeon }
3129d68875ebSPyun YongHyeon 
3130d68875ebSPyun YongHyeon static void
alc_mac_config(struct alc_softc * sc)3131d68875ebSPyun YongHyeon alc_mac_config(struct alc_softc *sc)
3132d68875ebSPyun YongHyeon {
3133d68875ebSPyun YongHyeon 	struct mii_data *mii;
3134d68875ebSPyun YongHyeon 	uint32_t reg;
3135d68875ebSPyun YongHyeon 
3136d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
3137d68875ebSPyun YongHyeon 
3138d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
3139d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
3140d68875ebSPyun YongHyeon 	reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
3141d68875ebSPyun YongHyeon 	    MAC_CFG_SPEED_MASK);
3142b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3143b624ef0aSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
31442f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
31452f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
31462f70cceaSPyun YongHyeon 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3147d68875ebSPyun YongHyeon 	/* Reprogram MAC with resolved speed/duplex. */
3148d68875ebSPyun YongHyeon 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
3149d68875ebSPyun YongHyeon 	case IFM_10_T:
3150d68875ebSPyun YongHyeon 	case IFM_100_TX:
3151d68875ebSPyun YongHyeon 		reg |= MAC_CFG_SPEED_10_100;
3152d68875ebSPyun YongHyeon 		break;
3153d68875ebSPyun YongHyeon 	case IFM_1000_T:
3154d68875ebSPyun YongHyeon 		reg |= MAC_CFG_SPEED_1000;
3155d68875ebSPyun YongHyeon 		break;
3156d68875ebSPyun YongHyeon 	}
3157d68875ebSPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
3158d68875ebSPyun YongHyeon 		reg |= MAC_CFG_FULL_DUPLEX;
3159d68875ebSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
3160d68875ebSPyun YongHyeon 			reg |= MAC_CFG_TX_FC;
3161d68875ebSPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
3162d68875ebSPyun YongHyeon 			reg |= MAC_CFG_RX_FC;
3163d68875ebSPyun YongHyeon 	}
3164d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3165d68875ebSPyun YongHyeon }
3166d68875ebSPyun YongHyeon 
3167d68875ebSPyun YongHyeon static void
alc_stats_clear(struct alc_softc * sc)3168d68875ebSPyun YongHyeon alc_stats_clear(struct alc_softc *sc)
3169d68875ebSPyun YongHyeon {
3170d68875ebSPyun YongHyeon 	struct smb sb, *smb;
3171d68875ebSPyun YongHyeon 	uint32_t *reg;
3172d68875ebSPyun YongHyeon 	int i;
3173d68875ebSPyun YongHyeon 
3174d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3175d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3176d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_smb_map,
3177d68875ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3178d68875ebSPyun YongHyeon 		smb = sc->alc_rdata.alc_smb;
3179d68875ebSPyun YongHyeon 		/* Update done, clear. */
3180d68875ebSPyun YongHyeon 		smb->updated = 0;
3181d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3182d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_smb_map,
3183d68875ebSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3184d68875ebSPyun YongHyeon 	} else {
3185d68875ebSPyun YongHyeon 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
3186d68875ebSPyun YongHyeon 		    reg++) {
3187d68875ebSPyun YongHyeon 			CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
3188d68875ebSPyun YongHyeon 			i += sizeof(uint32_t);
3189d68875ebSPyun YongHyeon 		}
3190d68875ebSPyun YongHyeon 		/* Read Tx statistics. */
3191d68875ebSPyun YongHyeon 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
3192d68875ebSPyun YongHyeon 		    reg++) {
3193d68875ebSPyun YongHyeon 			CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
3194d68875ebSPyun YongHyeon 			i += sizeof(uint32_t);
3195d68875ebSPyun YongHyeon 		}
3196d68875ebSPyun YongHyeon 	}
3197d68875ebSPyun YongHyeon }
3198d68875ebSPyun YongHyeon 
3199d68875ebSPyun YongHyeon static void
alc_stats_update(struct alc_softc * sc)3200d68875ebSPyun YongHyeon alc_stats_update(struct alc_softc *sc)
3201d68875ebSPyun YongHyeon {
3202d68875ebSPyun YongHyeon 	struct alc_hw_stats *stat;
3203d68875ebSPyun YongHyeon 	struct smb sb, *smb;
320452436412SJustin Hibbits 	if_t ifp;
3205d68875ebSPyun YongHyeon 	uint32_t *reg;
3206d68875ebSPyun YongHyeon 	int i;
3207d68875ebSPyun YongHyeon 
3208d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
3209d68875ebSPyun YongHyeon 
3210d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3211d68875ebSPyun YongHyeon 	stat = &sc->alc_stats;
3212d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3213d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3214d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_smb_map,
3215d68875ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3216d68875ebSPyun YongHyeon 		smb = sc->alc_rdata.alc_smb;
3217d68875ebSPyun YongHyeon 		if (smb->updated == 0)
3218d68875ebSPyun YongHyeon 			return;
3219d68875ebSPyun YongHyeon 	} else {
3220d68875ebSPyun YongHyeon 		smb = &sb;
3221d68875ebSPyun YongHyeon 		/* Read Rx statistics. */
3222d68875ebSPyun YongHyeon 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
3223d68875ebSPyun YongHyeon 		    reg++) {
3224d68875ebSPyun YongHyeon 			*reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
3225d68875ebSPyun YongHyeon 			i += sizeof(uint32_t);
3226d68875ebSPyun YongHyeon 		}
3227d68875ebSPyun YongHyeon 		/* Read Tx statistics. */
3228d68875ebSPyun YongHyeon 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
3229d68875ebSPyun YongHyeon 		    reg++) {
3230d68875ebSPyun YongHyeon 			*reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
3231d68875ebSPyun YongHyeon 			i += sizeof(uint32_t);
3232d68875ebSPyun YongHyeon 		}
3233d68875ebSPyun YongHyeon 	}
3234d68875ebSPyun YongHyeon 
3235d68875ebSPyun YongHyeon 	/* Rx stats. */
3236d68875ebSPyun YongHyeon 	stat->rx_frames += smb->rx_frames;
3237d68875ebSPyun YongHyeon 	stat->rx_bcast_frames += smb->rx_bcast_frames;
3238d68875ebSPyun YongHyeon 	stat->rx_mcast_frames += smb->rx_mcast_frames;
3239d68875ebSPyun YongHyeon 	stat->rx_pause_frames += smb->rx_pause_frames;
3240d68875ebSPyun YongHyeon 	stat->rx_control_frames += smb->rx_control_frames;
3241d68875ebSPyun YongHyeon 	stat->rx_crcerrs += smb->rx_crcerrs;
3242d68875ebSPyun YongHyeon 	stat->rx_lenerrs += smb->rx_lenerrs;
3243d68875ebSPyun YongHyeon 	stat->rx_bytes += smb->rx_bytes;
3244d68875ebSPyun YongHyeon 	stat->rx_runts += smb->rx_runts;
3245d68875ebSPyun YongHyeon 	stat->rx_fragments += smb->rx_fragments;
3246d68875ebSPyun YongHyeon 	stat->rx_pkts_64 += smb->rx_pkts_64;
3247d68875ebSPyun YongHyeon 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
3248d68875ebSPyun YongHyeon 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
3249d68875ebSPyun YongHyeon 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
3250d68875ebSPyun YongHyeon 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
3251d68875ebSPyun YongHyeon 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
3252d68875ebSPyun YongHyeon 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
3253d68875ebSPyun YongHyeon 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
3254d68875ebSPyun YongHyeon 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
3255d68875ebSPyun YongHyeon 	stat->rx_rrs_errs += smb->rx_rrs_errs;
3256d68875ebSPyun YongHyeon 	stat->rx_alignerrs += smb->rx_alignerrs;
3257d68875ebSPyun YongHyeon 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
3258d68875ebSPyun YongHyeon 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
3259d68875ebSPyun YongHyeon 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
3260d68875ebSPyun YongHyeon 
3261d68875ebSPyun YongHyeon 	/* Tx stats. */
3262d68875ebSPyun YongHyeon 	stat->tx_frames += smb->tx_frames;
3263d68875ebSPyun YongHyeon 	stat->tx_bcast_frames += smb->tx_bcast_frames;
3264d68875ebSPyun YongHyeon 	stat->tx_mcast_frames += smb->tx_mcast_frames;
3265d68875ebSPyun YongHyeon 	stat->tx_pause_frames += smb->tx_pause_frames;
3266d68875ebSPyun YongHyeon 	stat->tx_excess_defer += smb->tx_excess_defer;
3267d68875ebSPyun YongHyeon 	stat->tx_control_frames += smb->tx_control_frames;
3268d68875ebSPyun YongHyeon 	stat->tx_deferred += smb->tx_deferred;
3269d68875ebSPyun YongHyeon 	stat->tx_bytes += smb->tx_bytes;
3270d68875ebSPyun YongHyeon 	stat->tx_pkts_64 += smb->tx_pkts_64;
3271d68875ebSPyun YongHyeon 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
3272d68875ebSPyun YongHyeon 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
3273d68875ebSPyun YongHyeon 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
3274d68875ebSPyun YongHyeon 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
3275d68875ebSPyun YongHyeon 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
3276d68875ebSPyun YongHyeon 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
3277d68875ebSPyun YongHyeon 	stat->tx_single_colls += smb->tx_single_colls;
3278d68875ebSPyun YongHyeon 	stat->tx_multi_colls += smb->tx_multi_colls;
3279d68875ebSPyun YongHyeon 	stat->tx_late_colls += smb->tx_late_colls;
3280d68875ebSPyun YongHyeon 	stat->tx_excess_colls += smb->tx_excess_colls;
3281d68875ebSPyun YongHyeon 	stat->tx_underrun += smb->tx_underrun;
3282d68875ebSPyun YongHyeon 	stat->tx_desc_underrun += smb->tx_desc_underrun;
3283d68875ebSPyun YongHyeon 	stat->tx_lenerrs += smb->tx_lenerrs;
3284d68875ebSPyun YongHyeon 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
3285d68875ebSPyun YongHyeon 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
3286d68875ebSPyun YongHyeon 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
3287d68875ebSPyun YongHyeon 
3288d68875ebSPyun YongHyeon 	/* Update counters in ifnet. */
32899bce9009SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, smb->tx_frames);
3290d68875ebSPyun YongHyeon 
32919bce9009SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, smb->tx_single_colls +
3292d68875ebSPyun YongHyeon 	    smb->tx_multi_colls * 2 + smb->tx_late_colls +
32930999f75aSPyun YongHyeon 	    smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT);
3294d68875ebSPyun YongHyeon 
32950999f75aSPyun YongHyeon 	if_inc_counter(ifp, IFCOUNTER_OERRORS, smb->tx_late_colls +
32960999f75aSPyun YongHyeon 	    smb->tx_excess_colls + smb->tx_underrun + smb->tx_pkts_truncated);
3297d68875ebSPyun YongHyeon 
32989bce9009SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, smb->rx_frames);
3299d68875ebSPyun YongHyeon 
33009bce9009SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
33019bce9009SGleb Smirnoff 	    smb->rx_crcerrs + smb->rx_lenerrs +
3302d68875ebSPyun YongHyeon 	    smb->rx_runts + smb->rx_pkts_truncated +
3303d68875ebSPyun YongHyeon 	    smb->rx_fifo_oflows + smb->rx_rrs_errs +
33049bce9009SGleb Smirnoff 	    smb->rx_alignerrs);
3305d68875ebSPyun YongHyeon 
3306d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3307d68875ebSPyun YongHyeon 		/* Update done, clear. */
3308d68875ebSPyun YongHyeon 		smb->updated = 0;
3309d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3310d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_smb_map,
3311d68875ebSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3312d68875ebSPyun YongHyeon 	}
3313d68875ebSPyun YongHyeon }
3314d68875ebSPyun YongHyeon 
3315d68875ebSPyun YongHyeon static int
alc_intr(void * arg)3316d68875ebSPyun YongHyeon alc_intr(void *arg)
3317d68875ebSPyun YongHyeon {
3318d68875ebSPyun YongHyeon 	struct alc_softc *sc;
3319d68875ebSPyun YongHyeon 	uint32_t status;
3320d68875ebSPyun YongHyeon 
3321d68875ebSPyun YongHyeon 	sc = (struct alc_softc *)arg;
3322d68875ebSPyun YongHyeon 
332377b63733SKonstantin Belousov 	if (sc->alc_flags & ALC_FLAG_MT) {
332477b63733SKonstantin Belousov 		taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
332577b63733SKonstantin Belousov 		return (FILTER_HANDLED);
332677b63733SKonstantin Belousov 	}
332777b63733SKonstantin Belousov 
3328d68875ebSPyun YongHyeon 	status = CSR_READ_4(sc, ALC_INTR_STATUS);
3329d68875ebSPyun YongHyeon 	if ((status & ALC_INTRS) == 0)
3330d68875ebSPyun YongHyeon 		return (FILTER_STRAY);
3331d68875ebSPyun YongHyeon 	/* Disable interrupts. */
3332d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, INTR_DIS_INT);
3333d68875ebSPyun YongHyeon 	taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
3334d68875ebSPyun YongHyeon 
3335d68875ebSPyun YongHyeon 	return (FILTER_HANDLED);
3336d68875ebSPyun YongHyeon }
3337d68875ebSPyun YongHyeon 
3338d68875ebSPyun YongHyeon static void
alc_int_task(void * arg,int pending)3339d68875ebSPyun YongHyeon alc_int_task(void *arg, int pending)
3340d68875ebSPyun YongHyeon {
3341d68875ebSPyun YongHyeon 	struct alc_softc *sc;
334252436412SJustin Hibbits 	if_t ifp;
3343d68875ebSPyun YongHyeon 	uint32_t status;
3344d68875ebSPyun YongHyeon 	int more;
3345d68875ebSPyun YongHyeon 
3346d68875ebSPyun YongHyeon 	sc = (struct alc_softc *)arg;
3347d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3348d68875ebSPyun YongHyeon 
3349d68875ebSPyun YongHyeon 	status = CSR_READ_4(sc, ALC_INTR_STATUS);
33503b33d630SJohn Baldwin 	ALC_LOCK(sc);
33517e86a37eSPyun YongHyeon 	if (sc->alc_morework != 0) {
33527e86a37eSPyun YongHyeon 		sc->alc_morework = 0;
3353d68875ebSPyun YongHyeon 		status |= INTR_RX_PKT;
33547e86a37eSPyun YongHyeon 	}
3355d68875ebSPyun YongHyeon 	if ((status & ALC_INTRS) == 0)
3356d68875ebSPyun YongHyeon 		goto done;
3357d68875ebSPyun YongHyeon 
3358d68875ebSPyun YongHyeon 	/* Acknowledge interrupts but still disable interrupts. */
3359d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
3360d68875ebSPyun YongHyeon 
3361d68875ebSPyun YongHyeon 	more = 0;
336252436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
3363d68875ebSPyun YongHyeon 		if ((status & INTR_RX_PKT) != 0) {
3364d68875ebSPyun YongHyeon 			more = alc_rxintr(sc, sc->alc_process_limit);
3365d68875ebSPyun YongHyeon 			if (more == EAGAIN)
33667e86a37eSPyun YongHyeon 				sc->alc_morework = 1;
3367d68875ebSPyun YongHyeon 			else if (more == EIO) {
336852436412SJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3369d68875ebSPyun YongHyeon 				alc_init_locked(sc);
3370d68875ebSPyun YongHyeon 				ALC_UNLOCK(sc);
3371d68875ebSPyun YongHyeon 				return;
3372d68875ebSPyun YongHyeon 			}
3373d68875ebSPyun YongHyeon 		}
3374d68875ebSPyun YongHyeon 		if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
3375d68875ebSPyun YongHyeon 		    INTR_TXQ_TO_RST)) != 0) {
3376d68875ebSPyun YongHyeon 			if ((status & INTR_DMA_RD_TO_RST) != 0)
3377d68875ebSPyun YongHyeon 				device_printf(sc->alc_dev,
3378d68875ebSPyun YongHyeon 				    "DMA read error! -- resetting\n");
3379d68875ebSPyun YongHyeon 			if ((status & INTR_DMA_WR_TO_RST) != 0)
3380d68875ebSPyun YongHyeon 				device_printf(sc->alc_dev,
3381d68875ebSPyun YongHyeon 				    "DMA write error! -- resetting\n");
3382d68875ebSPyun YongHyeon 			if ((status & INTR_TXQ_TO_RST) != 0)
3383d68875ebSPyun YongHyeon 				device_printf(sc->alc_dev,
3384d68875ebSPyun YongHyeon 				    "TxQ reset! -- resetting\n");
338552436412SJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3386d68875ebSPyun YongHyeon 			alc_init_locked(sc);
3387d68875ebSPyun YongHyeon 			ALC_UNLOCK(sc);
3388d68875ebSPyun YongHyeon 			return;
3389d68875ebSPyun YongHyeon 		}
339052436412SJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
339152436412SJustin Hibbits 		    !if_sendq_empty(ifp))
33923b33d630SJohn Baldwin 			alc_start_locked(ifp);
3393d68875ebSPyun YongHyeon 	}
3394d68875ebSPyun YongHyeon 
3395d68875ebSPyun YongHyeon 	if (more == EAGAIN ||
3396d68875ebSPyun YongHyeon 	    (CSR_READ_4(sc, ALC_INTR_STATUS) & ALC_INTRS) != 0) {
33973b33d630SJohn Baldwin 		ALC_UNLOCK(sc);
3398d68875ebSPyun YongHyeon 		taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
3399d68875ebSPyun YongHyeon 		return;
3400d68875ebSPyun YongHyeon 	}
3401d68875ebSPyun YongHyeon 
3402d68875ebSPyun YongHyeon done:
340352436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
3404d68875ebSPyun YongHyeon 		/* Re-enable interrupts if we're running. */
340577b63733SKonstantin Belousov 		if (sc->alc_flags & ALC_FLAG_MT)
340677b63733SKonstantin Belousov 			CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
340777b63733SKonstantin Belousov 		else
3408d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
3409d68875ebSPyun YongHyeon 	}
34103b33d630SJohn Baldwin 	ALC_UNLOCK(sc);
3411d68875ebSPyun YongHyeon }
3412d68875ebSPyun YongHyeon 
3413d68875ebSPyun YongHyeon static void
alc_txeof(struct alc_softc * sc)3414d68875ebSPyun YongHyeon alc_txeof(struct alc_softc *sc)
3415d68875ebSPyun YongHyeon {
341652436412SJustin Hibbits 	if_t ifp;
3417d68875ebSPyun YongHyeon 	struct alc_txdesc *txd;
3418d68875ebSPyun YongHyeon 	uint32_t cons, prod;
3419d68875ebSPyun YongHyeon 
3420d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
3421d68875ebSPyun YongHyeon 
3422d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3423d68875ebSPyun YongHyeon 
3424d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_cnt == 0)
3425d68875ebSPyun YongHyeon 		return;
3426d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
3427d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_POSTWRITE);
3428d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
3429d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
3430d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_POSTREAD);
3431d68875ebSPyun YongHyeon 		prod = sc->alc_rdata.alc_cmb->cons;
3432b624ef0aSPyun YongHyeon 	} else {
3433b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3434b624ef0aSPyun YongHyeon 			prod = CSR_READ_2(sc, ALC_MBOX_TD_PRI0_CONS_IDX);
3435b624ef0aSPyun YongHyeon 		else {
3436d68875ebSPyun YongHyeon 			prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
3437d68875ebSPyun YongHyeon 			/* Assume we're using normal Tx priority queue. */
3438d68875ebSPyun YongHyeon 			prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
3439d68875ebSPyun YongHyeon 			    MBOX_TD_CONS_LO_IDX_SHIFT;
3440b624ef0aSPyun YongHyeon 		}
3441b624ef0aSPyun YongHyeon 	}
3442d68875ebSPyun YongHyeon 	cons = sc->alc_cdata.alc_tx_cons;
3443d68875ebSPyun YongHyeon 	/*
3444d68875ebSPyun YongHyeon 	 * Go through our Tx list and free mbufs for those
3445d68875ebSPyun YongHyeon 	 * frames which have been transmitted.
3446d68875ebSPyun YongHyeon 	 */
344764741244SDimitry Andric 	for (; cons != prod; ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
3448d68875ebSPyun YongHyeon 		if (sc->alc_cdata.alc_tx_cnt <= 0)
3449d68875ebSPyun YongHyeon 			break;
345052436412SJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
3451d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_tx_cnt--;
3452d68875ebSPyun YongHyeon 		txd = &sc->alc_cdata.alc_txdesc[cons];
3453d68875ebSPyun YongHyeon 		if (txd->tx_m != NULL) {
3454d68875ebSPyun YongHyeon 			/* Reclaim transmitted mbufs. */
3455d68875ebSPyun YongHyeon 			bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
3456d68875ebSPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3457d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
3458d68875ebSPyun YongHyeon 			    txd->tx_dmamap);
3459d68875ebSPyun YongHyeon 			m_freem(txd->tx_m);
3460d68875ebSPyun YongHyeon 			txd->tx_m = NULL;
3461d68875ebSPyun YongHyeon 		}
3462d68875ebSPyun YongHyeon 	}
3463d68875ebSPyun YongHyeon 
3464d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3465d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
3466d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_PREREAD);
3467d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_tx_cons = cons;
3468d68875ebSPyun YongHyeon 	/*
3469d68875ebSPyun YongHyeon 	 * Unarm watchdog timer only when there is no pending
3470d68875ebSPyun YongHyeon 	 * frames in Tx queue.
3471d68875ebSPyun YongHyeon 	 */
3472d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_tx_cnt == 0)
3473d68875ebSPyun YongHyeon 		sc->alc_watchdog_timer = 0;
3474d68875ebSPyun YongHyeon }
3475d68875ebSPyun YongHyeon 
3476d68875ebSPyun YongHyeon static int
alc_newbuf(struct alc_softc * sc,struct alc_rxdesc * rxd)3477d68875ebSPyun YongHyeon alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd)
3478d68875ebSPyun YongHyeon {
3479d68875ebSPyun YongHyeon 	struct mbuf *m;
3480d68875ebSPyun YongHyeon 	bus_dma_segment_t segs[1];
3481d68875ebSPyun YongHyeon 	bus_dmamap_t map;
3482d68875ebSPyun YongHyeon 	int nsegs;
3483d68875ebSPyun YongHyeon 
3484c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3485d68875ebSPyun YongHyeon 	if (m == NULL)
3486d68875ebSPyun YongHyeon 		return (ENOBUFS);
3487d68875ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
3488d68875ebSPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3489d68875ebSPyun YongHyeon 	m_adj(m, sizeof(uint64_t));
3490d68875ebSPyun YongHyeon #endif
3491d68875ebSPyun YongHyeon 
3492d68875ebSPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_rx_tag,
3493d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rx_sparemap, m, segs, &nsegs, 0) != 0) {
3494d68875ebSPyun YongHyeon 		m_freem(m);
3495d68875ebSPyun YongHyeon 		return (ENOBUFS);
3496d68875ebSPyun YongHyeon 	}
3497d68875ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
3498d68875ebSPyun YongHyeon 
3499d68875ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
3500d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
3501d68875ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
3502d68875ebSPyun YongHyeon 		bus_dmamap_unload(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap);
3503d68875ebSPyun YongHyeon 	}
3504d68875ebSPyun YongHyeon 	map = rxd->rx_dmamap;
3505d68875ebSPyun YongHyeon 	rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
3506d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_rx_sparemap = map;
3507d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
3508d68875ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
3509d68875ebSPyun YongHyeon 	rxd->rx_m = m;
3510d68875ebSPyun YongHyeon 	rxd->rx_desc->addr = htole64(segs[0].ds_addr);
3511d68875ebSPyun YongHyeon 	return (0);
3512d68875ebSPyun YongHyeon }
3513d68875ebSPyun YongHyeon 
3514d68875ebSPyun YongHyeon static int
alc_rxintr(struct alc_softc * sc,int count)3515d68875ebSPyun YongHyeon alc_rxintr(struct alc_softc *sc, int count)
3516d68875ebSPyun YongHyeon {
351752436412SJustin Hibbits 	if_t ifp;
3518d68875ebSPyun YongHyeon 	struct rx_rdesc *rrd;
3519d68875ebSPyun YongHyeon 	uint32_t nsegs, status;
3520d68875ebSPyun YongHyeon 	int rr_cons, prog;
3521d68875ebSPyun YongHyeon 
3522d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3523d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rr_ring_map,
3524d68875ebSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3525d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3526d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_POSTWRITE);
3527d68875ebSPyun YongHyeon 	rr_cons = sc->alc_cdata.alc_rr_cons;
3528d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
352952436412SJustin Hibbits 	for (prog = 0; (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0;) {
3530d68875ebSPyun YongHyeon 		if (count-- <= 0)
3531d68875ebSPyun YongHyeon 			break;
3532d68875ebSPyun YongHyeon 		rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
3533d68875ebSPyun YongHyeon 		status = le32toh(rrd->status);
3534d68875ebSPyun YongHyeon 		if ((status & RRD_VALID) == 0)
3535d68875ebSPyun YongHyeon 			break;
3536d68875ebSPyun YongHyeon 		nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
3537d68875ebSPyun YongHyeon 		if (nsegs == 0) {
3538d68875ebSPyun YongHyeon 			/* This should not happen! */
3539d68875ebSPyun YongHyeon 			device_printf(sc->alc_dev,
3540d68875ebSPyun YongHyeon 			    "unexpected segment count -- resetting\n");
3541d68875ebSPyun YongHyeon 			return (EIO);
3542d68875ebSPyun YongHyeon 		}
3543d68875ebSPyun YongHyeon 		alc_rxeof(sc, rrd);
3544d68875ebSPyun YongHyeon 		/* Clear Rx return status. */
3545d68875ebSPyun YongHyeon 		rrd->status = 0;
3546d68875ebSPyun YongHyeon 		ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
3547d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_rx_cons += nsegs;
3548d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
3549d68875ebSPyun YongHyeon 		prog += nsegs;
3550d68875ebSPyun YongHyeon 	}
3551d68875ebSPyun YongHyeon 
3552d68875ebSPyun YongHyeon 	if (prog > 0) {
3553d68875ebSPyun YongHyeon 		/* Update the consumer index. */
3554d68875ebSPyun YongHyeon 		sc->alc_cdata.alc_rr_cons = rr_cons;
3555d68875ebSPyun YongHyeon 		/* Sync Rx return descriptors. */
3556d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3557d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_rr_ring_map,
3558d68875ebSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3559d68875ebSPyun YongHyeon 		/*
3560d68875ebSPyun YongHyeon 		 * Sync updated Rx descriptors such that controller see
3561d68875ebSPyun YongHyeon 		 * modified buffer addresses.
3562d68875ebSPyun YongHyeon 		 */
3563d68875ebSPyun YongHyeon 		bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3564d68875ebSPyun YongHyeon 		    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
3565d68875ebSPyun YongHyeon 		/*
3566d68875ebSPyun YongHyeon 		 * Let controller know availability of new Rx buffers.
3567d68875ebSPyun YongHyeon 		 * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
3568d68875ebSPyun YongHyeon 		 * it may be possible to update ALC_MBOX_RD0_PROD_IDX
3569d68875ebSPyun YongHyeon 		 * only when Rx buffer pre-fetching is required. In
3570d68875ebSPyun YongHyeon 		 * addition we already set ALC_RX_RD_FREE_THRESH to
3571d68875ebSPyun YongHyeon 		 * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
3572d68875ebSPyun YongHyeon 		 * it still seems that pre-fetching needs more
3573d68875ebSPyun YongHyeon 		 * experimentation.
3574d68875ebSPyun YongHyeon 		 */
3575b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3576b624ef0aSPyun YongHyeon 			CSR_WRITE_2(sc, ALC_MBOX_RD0_PROD_IDX,
3577b624ef0aSPyun YongHyeon 			    (uint16_t)sc->alc_cdata.alc_rx_cons);
3578b624ef0aSPyun YongHyeon 		else
3579d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
3580d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rx_cons);
3581d68875ebSPyun YongHyeon 	}
3582d68875ebSPyun YongHyeon 
3583d68875ebSPyun YongHyeon 	return (count > 0 ? 0 : EAGAIN);
3584d68875ebSPyun YongHyeon }
3585d68875ebSPyun YongHyeon 
3586d68875ebSPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3587d68875ebSPyun YongHyeon static struct mbuf *
alc_fixup_rx(if_t ifp,struct mbuf * m)358852436412SJustin Hibbits alc_fixup_rx(if_t ifp, struct mbuf *m)
3589d68875ebSPyun YongHyeon {
3590d68875ebSPyun YongHyeon 	struct mbuf *n;
3591d68875ebSPyun YongHyeon         int i;
3592d68875ebSPyun YongHyeon         uint16_t *src, *dst;
3593d68875ebSPyun YongHyeon 
3594d68875ebSPyun YongHyeon 	src = mtod(m, uint16_t *);
3595d68875ebSPyun YongHyeon 	dst = src - 3;
3596d68875ebSPyun YongHyeon 
3597d68875ebSPyun YongHyeon 	if (m->m_next == NULL) {
3598d68875ebSPyun YongHyeon 		for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
3599d68875ebSPyun YongHyeon 			*dst++ = *src++;
3600d68875ebSPyun YongHyeon 		m->m_data -= 6;
3601d68875ebSPyun YongHyeon 		return (m);
3602d68875ebSPyun YongHyeon 	}
3603d68875ebSPyun YongHyeon 	/*
3604d68875ebSPyun YongHyeon 	 * Append a new mbuf to received mbuf chain and copy ethernet
3605d68875ebSPyun YongHyeon 	 * header from the mbuf chain. This can save lots of CPU
3606d68875ebSPyun YongHyeon 	 * cycles for jumbo frame.
3607d68875ebSPyun YongHyeon 	 */
3608c6499eccSGleb Smirnoff 	MGETHDR(n, M_NOWAIT, MT_DATA);
3609d68875ebSPyun YongHyeon 	if (n == NULL) {
36109bce9009SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
3611d68875ebSPyun YongHyeon 		m_freem(m);
3612d68875ebSPyun YongHyeon 		return (NULL);
3613d68875ebSPyun YongHyeon 	}
3614d68875ebSPyun YongHyeon 	bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
3615d68875ebSPyun YongHyeon 	m->m_data += ETHER_HDR_LEN;
3616d68875ebSPyun YongHyeon 	m->m_len -= ETHER_HDR_LEN;
3617d68875ebSPyun YongHyeon 	n->m_len = ETHER_HDR_LEN;
3618d68875ebSPyun YongHyeon 	M_MOVE_PKTHDR(n, m);
3619d68875ebSPyun YongHyeon 	n->m_next = m;
3620d68875ebSPyun YongHyeon 	return (n);
3621d68875ebSPyun YongHyeon }
3622d68875ebSPyun YongHyeon #endif
3623d68875ebSPyun YongHyeon 
3624d68875ebSPyun YongHyeon /* Receive a frame. */
3625d68875ebSPyun YongHyeon static void
alc_rxeof(struct alc_softc * sc,struct rx_rdesc * rrd)3626d68875ebSPyun YongHyeon alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
3627d68875ebSPyun YongHyeon {
3628d68875ebSPyun YongHyeon 	struct alc_rxdesc *rxd;
362952436412SJustin Hibbits 	if_t ifp;
3630d68875ebSPyun YongHyeon 	struct mbuf *mp, *m;
3631d68875ebSPyun YongHyeon 	uint32_t rdinfo, status, vtag;
3632d68875ebSPyun YongHyeon 	int count, nsegs, rx_cons;
3633d68875ebSPyun YongHyeon 
3634d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3635d68875ebSPyun YongHyeon 	status = le32toh(rrd->status);
3636d68875ebSPyun YongHyeon 	rdinfo = le32toh(rrd->rdinfo);
3637d68875ebSPyun YongHyeon 	rx_cons = RRD_RD_IDX(rdinfo);
3638d68875ebSPyun YongHyeon 	nsegs = RRD_RD_CNT(rdinfo);
3639d68875ebSPyun YongHyeon 
3640d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
3641d68875ebSPyun YongHyeon 	if ((status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) != 0) {
3642d68875ebSPyun YongHyeon 		/*
3643d68875ebSPyun YongHyeon 		 * We want to pass the following frames to upper
3644d68875ebSPyun YongHyeon 		 * layer regardless of error status of Rx return
3645d68875ebSPyun YongHyeon 		 * ring.
3646d68875ebSPyun YongHyeon 		 *
3647d68875ebSPyun YongHyeon 		 *  o IP/TCP/UDP checksum is bad.
3648d68875ebSPyun YongHyeon 		 *  o frame length and protocol specific length
3649d68875ebSPyun YongHyeon 		 *     does not match.
3650d68875ebSPyun YongHyeon 		 *
3651d68875ebSPyun YongHyeon 		 *  Force network stack compute checksum for
3652d68875ebSPyun YongHyeon 		 *  errored frames.
3653d68875ebSPyun YongHyeon 		 */
3654d68875ebSPyun YongHyeon 		status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
36559ed03f02SXin LI 		if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
36569ed03f02SXin LI 		    RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
3657d68875ebSPyun YongHyeon 			return;
3658d68875ebSPyun YongHyeon 	}
3659d68875ebSPyun YongHyeon 
3660d68875ebSPyun YongHyeon 	for (count = 0; count < nsegs; count++,
3661d68875ebSPyun YongHyeon 	    ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
3662d68875ebSPyun YongHyeon 		rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
3663d68875ebSPyun YongHyeon 		mp = rxd->rx_m;
3664d68875ebSPyun YongHyeon 		/* Add a new receive buffer to the ring. */
3665d68875ebSPyun YongHyeon 		if (alc_newbuf(sc, rxd) != 0) {
36669bce9009SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
3667d68875ebSPyun YongHyeon 			/* Reuse Rx buffers. */
3668d68875ebSPyun YongHyeon 			if (sc->alc_cdata.alc_rxhead != NULL)
3669d68875ebSPyun YongHyeon 				m_freem(sc->alc_cdata.alc_rxhead);
3670d68875ebSPyun YongHyeon 			break;
3671d68875ebSPyun YongHyeon 		}
3672d68875ebSPyun YongHyeon 
3673d68875ebSPyun YongHyeon 		/*
3674d68875ebSPyun YongHyeon 		 * Assume we've received a full sized frame.
3675d68875ebSPyun YongHyeon 		 * Actual size is fixed when we encounter the end of
3676d68875ebSPyun YongHyeon 		 * multi-segmented frame.
3677d68875ebSPyun YongHyeon 		 */
3678d68875ebSPyun YongHyeon 		mp->m_len = sc->alc_buf_size;
3679d68875ebSPyun YongHyeon 
3680d68875ebSPyun YongHyeon 		/* Chain received mbufs. */
3681d68875ebSPyun YongHyeon 		if (sc->alc_cdata.alc_rxhead == NULL) {
3682d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rxhead = mp;
3683d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rxtail = mp;
3684d68875ebSPyun YongHyeon 		} else {
3685d68875ebSPyun YongHyeon 			mp->m_flags &= ~M_PKTHDR;
3686d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rxprev_tail =
3687d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rxtail;
3688d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rxtail->m_next = mp;
3689d68875ebSPyun YongHyeon 			sc->alc_cdata.alc_rxtail = mp;
3690d68875ebSPyun YongHyeon 		}
3691d68875ebSPyun YongHyeon 
3692d68875ebSPyun YongHyeon 		if (count == nsegs - 1) {
3693d68875ebSPyun YongHyeon 			/* Last desc. for this frame. */
3694d68875ebSPyun YongHyeon 			m = sc->alc_cdata.alc_rxhead;
3695d68875ebSPyun YongHyeon 			m->m_flags |= M_PKTHDR;
3696d68875ebSPyun YongHyeon 			/*
3697d68875ebSPyun YongHyeon 			 * It seems that L1C/L2C controller has no way
3698d68875ebSPyun YongHyeon 			 * to tell hardware to strip CRC bytes.
3699d68875ebSPyun YongHyeon 			 */
3700d68875ebSPyun YongHyeon 			m->m_pkthdr.len =
3701d68875ebSPyun YongHyeon 			    sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
3702d68875ebSPyun YongHyeon 			if (nsegs > 1) {
3703d68875ebSPyun YongHyeon 				/* Set last mbuf size. */
3704d68875ebSPyun YongHyeon 				mp->m_len = sc->alc_cdata.alc_rxlen -
3705d68875ebSPyun YongHyeon 				    (nsegs - 1) * sc->alc_buf_size;
3706d68875ebSPyun YongHyeon 				/* Remove the CRC bytes in chained mbufs. */
3707d68875ebSPyun YongHyeon 				if (mp->m_len <= ETHER_CRC_LEN) {
3708d68875ebSPyun YongHyeon 					sc->alc_cdata.alc_rxtail =
3709d68875ebSPyun YongHyeon 					    sc->alc_cdata.alc_rxprev_tail;
3710d68875ebSPyun YongHyeon 					sc->alc_cdata.alc_rxtail->m_len -=
3711d68875ebSPyun YongHyeon 					    (ETHER_CRC_LEN - mp->m_len);
3712d68875ebSPyun YongHyeon 					sc->alc_cdata.alc_rxtail->m_next = NULL;
3713d68875ebSPyun YongHyeon 					m_freem(mp);
3714d68875ebSPyun YongHyeon 				} else {
3715d68875ebSPyun YongHyeon 					mp->m_len -= ETHER_CRC_LEN;
3716d68875ebSPyun YongHyeon 				}
3717d68875ebSPyun YongHyeon 			} else
3718d68875ebSPyun YongHyeon 				m->m_len = m->m_pkthdr.len;
3719d68875ebSPyun YongHyeon 			m->m_pkthdr.rcvif = ifp;
3720d68875ebSPyun YongHyeon 			/*
3721d68875ebSPyun YongHyeon 			 * Due to hardware bugs, Rx checksum offloading
3722d68875ebSPyun YongHyeon 			 * was intentionally disabled.
3723d68875ebSPyun YongHyeon 			 */
372452436412SJustin Hibbits 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
3725d68875ebSPyun YongHyeon 			    (status & RRD_VLAN_TAG) != 0) {
3726d68875ebSPyun YongHyeon 				vtag = RRD_VLAN(le32toh(rrd->vtag));
3727d68875ebSPyun YongHyeon 				m->m_pkthdr.ether_vtag = ntohs(vtag);
3728d68875ebSPyun YongHyeon 				m->m_flags |= M_VLANTAG;
3729d68875ebSPyun YongHyeon 			}
3730d68875ebSPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3731d68875ebSPyun YongHyeon 			m = alc_fixup_rx(ifp, m);
3732d68875ebSPyun YongHyeon 			if (m != NULL)
3733d68875ebSPyun YongHyeon #endif
3734d68875ebSPyun YongHyeon 			{
3735d68875ebSPyun YongHyeon 			/* Pass it on. */
37363b33d630SJohn Baldwin 			ALC_UNLOCK(sc);
373752436412SJustin Hibbits 			if_input(ifp, m);
37383b33d630SJohn Baldwin 			ALC_LOCK(sc);
3739d68875ebSPyun YongHyeon 			}
3740d68875ebSPyun YongHyeon 		}
3741d68875ebSPyun YongHyeon 	}
3742d68875ebSPyun YongHyeon 	/* Reset mbuf chains. */
3743d68875ebSPyun YongHyeon 	ALC_RXCHAIN_RESET(sc);
3744d68875ebSPyun YongHyeon }
3745d68875ebSPyun YongHyeon 
3746d68875ebSPyun YongHyeon static void
alc_tick(void * arg)3747d68875ebSPyun YongHyeon alc_tick(void *arg)
3748d68875ebSPyun YongHyeon {
3749d68875ebSPyun YongHyeon 	struct alc_softc *sc;
3750d68875ebSPyun YongHyeon 	struct mii_data *mii;
3751d68875ebSPyun YongHyeon 
3752d68875ebSPyun YongHyeon 	sc = (struct alc_softc *)arg;
3753d68875ebSPyun YongHyeon 
3754d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
3755d68875ebSPyun YongHyeon 
3756d68875ebSPyun YongHyeon 	mii = device_get_softc(sc->alc_miibus);
3757d68875ebSPyun YongHyeon 	mii_tick(mii);
3758d68875ebSPyun YongHyeon 	alc_stats_update(sc);
3759d68875ebSPyun YongHyeon 	/*
3760d68875ebSPyun YongHyeon 	 * alc(4) does not rely on Tx completion interrupts to reclaim
3761d68875ebSPyun YongHyeon 	 * transferred buffers. Instead Tx completion interrupts are
3762d68875ebSPyun YongHyeon 	 * used to hint for scheduling Tx task. So it's necessary to
3763d68875ebSPyun YongHyeon 	 * release transmitted buffers by kicking Tx completion
3764d68875ebSPyun YongHyeon 	 * handler. This limits the maximum reclamation delay to a hz.
3765d68875ebSPyun YongHyeon 	 */
3766d68875ebSPyun YongHyeon 	alc_txeof(sc);
3767d68875ebSPyun YongHyeon 	alc_watchdog(sc);
3768d68875ebSPyun YongHyeon 	callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3769d68875ebSPyun YongHyeon }
3770d68875ebSPyun YongHyeon 
3771d68875ebSPyun YongHyeon static void
alc_osc_reset(struct alc_softc * sc)3772b624ef0aSPyun YongHyeon alc_osc_reset(struct alc_softc *sc)
3773d68875ebSPyun YongHyeon {
3774d68875ebSPyun YongHyeon 	uint32_t reg;
3775b624ef0aSPyun YongHyeon 
3776b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MISC3);
3777b624ef0aSPyun YongHyeon 	reg &= ~MISC3_25M_BY_SW;
3778b624ef0aSPyun YongHyeon 	reg |= MISC3_25M_NOTO_INTNL;
3779b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MISC3, reg);
3780b624ef0aSPyun YongHyeon 
3781b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MISC);
3782b624ef0aSPyun YongHyeon 	if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0) {
3783b624ef0aSPyun YongHyeon 		/*
3784b624ef0aSPyun YongHyeon 		 * Restore over-current protection default value.
3785b624ef0aSPyun YongHyeon 		 * This value could be reset by MAC reset.
3786b624ef0aSPyun YongHyeon 		 */
3787b624ef0aSPyun YongHyeon 		reg &= ~MISC_PSW_OCP_MASK;
3788b624ef0aSPyun YongHyeon 		reg |= (MISC_PSW_OCP_DEFAULT << MISC_PSW_OCP_SHIFT);
3789b624ef0aSPyun YongHyeon 		reg &= ~MISC_INTNLOSC_OPEN;
3790b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC, reg);
3791b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
3792b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_MISC2);
3793b624ef0aSPyun YongHyeon 		reg &= ~MISC2_CALB_START;
3794b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC2, reg);
3795b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC2, reg | MISC2_CALB_START);
3796b624ef0aSPyun YongHyeon 
3797b624ef0aSPyun YongHyeon 	} else {
3798b624ef0aSPyun YongHyeon 		reg &= ~MISC_INTNLOSC_OPEN;
3799b624ef0aSPyun YongHyeon 		/* Disable isolate for revision A devices. */
3800b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
3801b624ef0aSPyun YongHyeon 			reg &= ~MISC_ISO_ENB;
3802b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
3803b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC, reg);
3804b624ef0aSPyun YongHyeon 	}
3805b624ef0aSPyun YongHyeon 
3806b624ef0aSPyun YongHyeon 	DELAY(20);
3807b624ef0aSPyun YongHyeon }
3808b624ef0aSPyun YongHyeon 
3809b624ef0aSPyun YongHyeon static void
alc_reset(struct alc_softc * sc)3810b624ef0aSPyun YongHyeon alc_reset(struct alc_softc *sc)
3811b624ef0aSPyun YongHyeon {
3812b624ef0aSPyun YongHyeon 	uint32_t pmcfg, reg;
3813d68875ebSPyun YongHyeon 	int i;
3814d68875ebSPyun YongHyeon 
3815b624ef0aSPyun YongHyeon 	pmcfg = 0;
3816b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3817b624ef0aSPyun YongHyeon 		/* Reset workaround. */
3818b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, 1);
3819b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
3820b624ef0aSPyun YongHyeon 		    (sc->alc_rev & 0x01) != 0) {
3821b624ef0aSPyun YongHyeon 			/* Disable L0s/L1s before reset. */
3822b624ef0aSPyun YongHyeon 			pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
3823b624ef0aSPyun YongHyeon 			if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
3824b624ef0aSPyun YongHyeon 			    != 0) {
3825b624ef0aSPyun YongHyeon 				pmcfg &= ~(PM_CFG_ASPM_L0S_ENB |
3826b624ef0aSPyun YongHyeon 				    PM_CFG_ASPM_L1_ENB);
3827b624ef0aSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
3828b624ef0aSPyun YongHyeon 			}
3829b624ef0aSPyun YongHyeon 		}
3830b624ef0aSPyun YongHyeon 	}
3831b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MASTER_CFG);
38322f70cceaSPyun YongHyeon 	reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
38332f70cceaSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3834b624ef0aSPyun YongHyeon 
3835b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3836b624ef0aSPyun YongHyeon 		for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3837b624ef0aSPyun YongHyeon 			DELAY(10);
3838b624ef0aSPyun YongHyeon 			if (CSR_READ_4(sc, ALC_MBOX_RD0_PROD_IDX) == 0)
3839b624ef0aSPyun YongHyeon 				break;
3840b624ef0aSPyun YongHyeon 		}
3841b624ef0aSPyun YongHyeon 		if (i == 0)
3842b624ef0aSPyun YongHyeon 			device_printf(sc->alc_dev, "MAC reset timeout!\n");
3843b624ef0aSPyun YongHyeon 	}
3844d68875ebSPyun YongHyeon 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3845d68875ebSPyun YongHyeon 		DELAY(10);
3846d68875ebSPyun YongHyeon 		if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
3847d68875ebSPyun YongHyeon 			break;
3848d68875ebSPyun YongHyeon 	}
3849d68875ebSPyun YongHyeon 	if (i == 0)
3850d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "master reset timeout!\n");
3851d68875ebSPyun YongHyeon 
3852d68875ebSPyun YongHyeon 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3853b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3854b624ef0aSPyun YongHyeon 		if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC |
3855b624ef0aSPyun YongHyeon 		    IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3856d68875ebSPyun YongHyeon 			break;
3857d68875ebSPyun YongHyeon 		DELAY(10);
3858d68875ebSPyun YongHyeon 	}
3859d68875ebSPyun YongHyeon 	if (i == 0)
3860d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "reset timeout(0x%08x)!\n", reg);
3861b624ef0aSPyun YongHyeon 
3862b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3863b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
3864b624ef0aSPyun YongHyeon 		    (sc->alc_rev & 0x01) != 0) {
3865b624ef0aSPyun YongHyeon 			reg = CSR_READ_4(sc, ALC_MASTER_CFG);
3866b624ef0aSPyun YongHyeon 			reg |= MASTER_CLK_SEL_DIS;
3867b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3868b624ef0aSPyun YongHyeon 			/* Restore L0s/L1s config. */
3869b624ef0aSPyun YongHyeon 			if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
3870b624ef0aSPyun YongHyeon 			    != 0)
3871b624ef0aSPyun YongHyeon 				CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
3872b624ef0aSPyun YongHyeon 		}
3873b624ef0aSPyun YongHyeon 
3874b624ef0aSPyun YongHyeon 		alc_osc_reset(sc);
3875b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_MISC3);
3876b624ef0aSPyun YongHyeon 		reg &= ~MISC3_25M_BY_SW;
3877b624ef0aSPyun YongHyeon 		reg |= MISC3_25M_NOTO_INTNL;
3878b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC3, reg);
3879b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_MISC);
3880b624ef0aSPyun YongHyeon 		reg &= ~MISC_INTNLOSC_OPEN;
3881b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
3882b624ef0aSPyun YongHyeon 			reg &= ~MISC_ISO_ENB;
3883b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MISC, reg);
3884b624ef0aSPyun YongHyeon 		DELAY(20);
3885b624ef0aSPyun YongHyeon 	}
3886b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3887b624ef0aSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3888b624ef0aSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2)
3889b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SERDES_LOCK,
3890b624ef0aSPyun YongHyeon 		    CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
3891b624ef0aSPyun YongHyeon 		    SERDES_PHY_CLK_SLOWDOWN);
3892d68875ebSPyun YongHyeon }
3893d68875ebSPyun YongHyeon 
3894d68875ebSPyun YongHyeon static void
alc_init(void * xsc)3895d68875ebSPyun YongHyeon alc_init(void *xsc)
3896d68875ebSPyun YongHyeon {
3897d68875ebSPyun YongHyeon 	struct alc_softc *sc;
3898d68875ebSPyun YongHyeon 
3899d68875ebSPyun YongHyeon 	sc = (struct alc_softc *)xsc;
3900d68875ebSPyun YongHyeon 	ALC_LOCK(sc);
3901d68875ebSPyun YongHyeon 	alc_init_locked(sc);
3902d68875ebSPyun YongHyeon 	ALC_UNLOCK(sc);
3903d68875ebSPyun YongHyeon }
3904d68875ebSPyun YongHyeon 
3905d68875ebSPyun YongHyeon static void
alc_init_locked(struct alc_softc * sc)3906d68875ebSPyun YongHyeon alc_init_locked(struct alc_softc *sc)
3907d68875ebSPyun YongHyeon {
390852436412SJustin Hibbits 	if_t ifp;
3909d68875ebSPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
3910d68875ebSPyun YongHyeon 	bus_addr_t paddr;
3911d68875ebSPyun YongHyeon 	uint32_t reg, rxf_hi, rxf_lo;
3912d68875ebSPyun YongHyeon 
3913d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
3914d68875ebSPyun YongHyeon 
3915d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
3916d68875ebSPyun YongHyeon 
391752436412SJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
3918d68875ebSPyun YongHyeon 		return;
3919d68875ebSPyun YongHyeon 	/*
3920d68875ebSPyun YongHyeon 	 * Cancel any pending I/O.
3921d68875ebSPyun YongHyeon 	 */
3922d68875ebSPyun YongHyeon 	alc_stop(sc);
3923d68875ebSPyun YongHyeon 	/*
3924d68875ebSPyun YongHyeon 	 * Reset the chip to a known state.
3925d68875ebSPyun YongHyeon 	 */
3926d68875ebSPyun YongHyeon 	alc_reset(sc);
3927d68875ebSPyun YongHyeon 
3928d68875ebSPyun YongHyeon 	/* Initialize Rx descriptors. */
3929d68875ebSPyun YongHyeon 	if (alc_init_rx_ring(sc) != 0) {
3930d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev, "no memory for Rx buffers.\n");
3931d68875ebSPyun YongHyeon 		alc_stop(sc);
3932d68875ebSPyun YongHyeon 		return;
3933d68875ebSPyun YongHyeon 	}
3934d68875ebSPyun YongHyeon 	alc_init_rr_ring(sc);
3935d68875ebSPyun YongHyeon 	alc_init_tx_ring(sc);
3936d68875ebSPyun YongHyeon 	alc_init_cmb(sc);
3937d68875ebSPyun YongHyeon 	alc_init_smb(sc);
3938d68875ebSPyun YongHyeon 
3939c27d7a76SPyun YongHyeon 	/* Enable all clocks. */
3940b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3941b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, CLK_GATING_DMAW_ENB |
3942b624ef0aSPyun YongHyeon 		    CLK_GATING_DMAR_ENB | CLK_GATING_TXQ_ENB |
3943b624ef0aSPyun YongHyeon 		    CLK_GATING_RXQ_ENB | CLK_GATING_TXMAC_ENB |
3944b624ef0aSPyun YongHyeon 		    CLK_GATING_RXMAC_ENB);
3945b624ef0aSPyun YongHyeon 		if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0)
3946b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_IDLE_DECISN_TIMER,
3947b624ef0aSPyun YongHyeon 			    IDLE_DECISN_TIMER_DEFAULT_1MS);
3948b624ef0aSPyun YongHyeon 	} else
3949c27d7a76SPyun YongHyeon 		CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
3950c27d7a76SPyun YongHyeon 
3951d68875ebSPyun YongHyeon 	/* Reprogram the station address. */
395252436412SJustin Hibbits 	bcopy(if_getlladdr(ifp), eaddr, ETHER_ADDR_LEN);
3953d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PAR0,
3954d68875ebSPyun YongHyeon 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
3955d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
3956d68875ebSPyun YongHyeon 	/*
3957d68875ebSPyun YongHyeon 	 * Clear WOL status and disable all WOL feature as WOL
3958d68875ebSPyun YongHyeon 	 * would interfere Rx operation under normal environments.
3959d68875ebSPyun YongHyeon 	 */
3960d68875ebSPyun YongHyeon 	CSR_READ_4(sc, ALC_WOL_CFG);
3961d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
3962d68875ebSPyun YongHyeon 	/* Set Tx descriptor base addresses. */
3963d68875ebSPyun YongHyeon 	paddr = sc->alc_rdata.alc_tx_ring_paddr;
3964d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3965d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3966d68875ebSPyun YongHyeon 	/* We don't use high priority ring. */
3967d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
3968d68875ebSPyun YongHyeon 	/* Set Tx descriptor counter. */
3969d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TD_RING_CNT,
3970d68875ebSPyun YongHyeon 	    (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
3971d68875ebSPyun YongHyeon 	/* Set Rx descriptor base addresses. */
3972d68875ebSPyun YongHyeon 	paddr = sc->alc_rdata.alc_rx_ring_paddr;
3973d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3974d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3975b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3976d68875ebSPyun YongHyeon 		/* We use one Rx ring. */
3977d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
3978d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
3979d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
3980b624ef0aSPyun YongHyeon 	}
3981d68875ebSPyun YongHyeon 	/* Set Rx descriptor counter. */
3982d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RD_RING_CNT,
3983d68875ebSPyun YongHyeon 	    (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
3984d68875ebSPyun YongHyeon 
3985d68875ebSPyun YongHyeon 	/*
3986d68875ebSPyun YongHyeon 	 * Let hardware split jumbo frames into alc_max_buf_sized chunks.
3987d68875ebSPyun YongHyeon 	 * if it do not fit the buffer size. Rx return descriptor holds
3988d68875ebSPyun YongHyeon 	 * a counter that indicates how many fragments were made by the
3989d68875ebSPyun YongHyeon 	 * hardware. The buffer size should be multiple of 8 bytes.
3990d68875ebSPyun YongHyeon 	 * Since hardware has limit on the size of buffer size, always
3991d68875ebSPyun YongHyeon 	 * use the maximum value.
3992d68875ebSPyun YongHyeon 	 * For strict-alignment architectures make sure to reduce buffer
3993d68875ebSPyun YongHyeon 	 * size by 8 bytes to make room for alignment fixup.
3994d68875ebSPyun YongHyeon 	 */
3995d68875ebSPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3996d68875ebSPyun YongHyeon 	sc->alc_buf_size = RX_BUF_SIZE_MAX - sizeof(uint64_t);
3997d68875ebSPyun YongHyeon #else
3998d68875ebSPyun YongHyeon 	sc->alc_buf_size = RX_BUF_SIZE_MAX;
3999d68875ebSPyun YongHyeon #endif
4000d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
4001d68875ebSPyun YongHyeon 
4002d68875ebSPyun YongHyeon 	paddr = sc->alc_rdata.alc_rr_ring_paddr;
4003d68875ebSPyun YongHyeon 	/* Set Rx return descriptor base addresses. */
4004d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
4005b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4006d68875ebSPyun YongHyeon 		/* We use one Rx return ring. */
4007d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
4008d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
4009d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
4010b624ef0aSPyun YongHyeon 	}
4011d68875ebSPyun YongHyeon 	/* Set Rx return descriptor counter. */
4012d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
4013d68875ebSPyun YongHyeon 	    (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
4014d68875ebSPyun YongHyeon 	paddr = sc->alc_rdata.alc_cmb_paddr;
4015d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
4016d68875ebSPyun YongHyeon 	paddr = sc->alc_rdata.alc_smb_paddr;
4017d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
4018d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
4019d68875ebSPyun YongHyeon 
40202f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
40212f70cceaSPyun YongHyeon 		/* Reconfigure SRAM - Vendor magic. */
40222f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
40232f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
40242f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
40252f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
40262f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
40272f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
40282f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
40292f70cceaSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
40302f70cceaSPyun YongHyeon 	}
40312f70cceaSPyun YongHyeon 
4032d68875ebSPyun YongHyeon 	/* Tell hardware that we're ready to load DMA blocks. */
4033d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
4034d68875ebSPyun YongHyeon 
4035d68875ebSPyun YongHyeon 	/* Configure interrupt moderation timer. */
4036d68875ebSPyun YongHyeon 	reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
4037b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0)
4038d68875ebSPyun YongHyeon 		reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
4039d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
4040d68875ebSPyun YongHyeon 	/*
4041d68875ebSPyun YongHyeon 	 * We don't want to automatic interrupt clear as task queue
4042d68875ebSPyun YongHyeon 	 * for the interrupt should know interrupt status.
4043d68875ebSPyun YongHyeon 	 */
4044b624ef0aSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MASTER_CFG);
4045b624ef0aSPyun YongHyeon 	reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
4046b624ef0aSPyun YongHyeon 	reg |= MASTER_SA_TIMER_ENB;
4047d68875ebSPyun YongHyeon 	if (ALC_USECS(sc->alc_int_rx_mod) != 0)
4048d68875ebSPyun YongHyeon 		reg |= MASTER_IM_RX_TIMER_ENB;
4049b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0 &&
4050b624ef0aSPyun YongHyeon 	    ALC_USECS(sc->alc_int_tx_mod) != 0)
4051d68875ebSPyun YongHyeon 		reg |= MASTER_IM_TX_TIMER_ENB;
4052d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
4053d68875ebSPyun YongHyeon 	/*
4054d68875ebSPyun YongHyeon 	 * Disable interrupt re-trigger timer. We don't want automatic
4055d68875ebSPyun YongHyeon 	 * re-triggering of un-ACKed interrupts.
4056d68875ebSPyun YongHyeon 	 */
4057d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
4058d68875ebSPyun YongHyeon 	/* Configure CMB. */
4059b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4060b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, ALC_TX_RING_CNT / 3);
4061b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_CMB_TX_TIMER,
4062b624ef0aSPyun YongHyeon 		    ALC_USECS(sc->alc_int_tx_mod));
4063b624ef0aSPyun YongHyeon 	} else {
4064a0bca955SPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
4065d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
4066d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
4067a0bca955SPyun YongHyeon 		} else
4068d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
4069b624ef0aSPyun YongHyeon 	}
4070d68875ebSPyun YongHyeon 	/*
4071d68875ebSPyun YongHyeon 	 * Hardware can be configured to issue SMB interrupt based
4072d68875ebSPyun YongHyeon 	 * on programmed interval. Since there is a callout that is
4073d68875ebSPyun YongHyeon 	 * invoked for every hz in driver we use that instead of
4074d68875ebSPyun YongHyeon 	 * relying on periodic SMB interrupt.
4075d68875ebSPyun YongHyeon 	 */
4076d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
4077d68875ebSPyun YongHyeon 	/* Clear MAC statistics. */
4078d68875ebSPyun YongHyeon 	alc_stats_clear(sc);
4079d68875ebSPyun YongHyeon 
4080d68875ebSPyun YongHyeon 	/*
4081d68875ebSPyun YongHyeon 	 * Always use maximum frame size that controller can support.
4082d68875ebSPyun YongHyeon 	 * Otherwise received frames that has larger frame length
4083d68875ebSPyun YongHyeon 	 * than alc(4) MTU would be silently dropped in hardware. This
4084d68875ebSPyun YongHyeon 	 * would make path-MTU discovery hard as sender wouldn't get
4085d68875ebSPyun YongHyeon 	 * any responses from receiver. alc(4) supports
4086d68875ebSPyun YongHyeon 	 * multi-fragmented frames on Rx path so it has no issue on
4087d68875ebSPyun YongHyeon 	 * assembling fragmented frames. Using maximum frame size also
4088d68875ebSPyun YongHyeon 	 * removes the need to reinitialize hardware when interface
4089d68875ebSPyun YongHyeon 	 * MTU configuration was changed.
4090d68875ebSPyun YongHyeon 	 *
4091d68875ebSPyun YongHyeon 	 * Be conservative in what you do, be liberal in what you
4092d68875ebSPyun YongHyeon 	 * accept from others - RFC 793.
4093d68875ebSPyun YongHyeon 	 */
40942f70cceaSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
4095d68875ebSPyun YongHyeon 
4096b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4097d68875ebSPyun YongHyeon 		/* Disable header split(?) */
4098d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
4099d68875ebSPyun YongHyeon 
4100d68875ebSPyun YongHyeon 		/* Configure IPG/IFG parameters. */
4101d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
4102b624ef0aSPyun YongHyeon 		    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) &
4103b624ef0aSPyun YongHyeon 		    IPG_IFG_IPGT_MASK) |
4104b624ef0aSPyun YongHyeon 		    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) &
4105b624ef0aSPyun YongHyeon 		    IPG_IFG_MIFG_MASK) |
4106b624ef0aSPyun YongHyeon 		    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) &
4107b624ef0aSPyun YongHyeon 		    IPG_IFG_IPG1_MASK) |
4108b624ef0aSPyun YongHyeon 		    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) &
4109b624ef0aSPyun YongHyeon 		    IPG_IFG_IPG2_MASK));
4110d68875ebSPyun YongHyeon 		/* Set parameters for half-duplex media. */
4111d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_HDPX_CFG,
4112d68875ebSPyun YongHyeon 		    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
4113d68875ebSPyun YongHyeon 		    HDPX_CFG_LCOL_MASK) |
4114d68875ebSPyun YongHyeon 		    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
4115d68875ebSPyun YongHyeon 		    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
4116d68875ebSPyun YongHyeon 		    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
4117d68875ebSPyun YongHyeon 		    HDPX_CFG_ABEBT_MASK) |
4118d68875ebSPyun YongHyeon 		    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
4119d68875ebSPyun YongHyeon 		    HDPX_CFG_JAMIPG_MASK));
4120b624ef0aSPyun YongHyeon 	}
4121b624ef0aSPyun YongHyeon 
4122d68875ebSPyun YongHyeon 	/*
4123d68875ebSPyun YongHyeon 	 * Set TSO/checksum offload threshold. For frames that is
4124d68875ebSPyun YongHyeon 	 * larger than this threshold, hardware wouldn't do
4125d68875ebSPyun YongHyeon 	 * TSO/checksum offloading.
4126d68875ebSPyun YongHyeon 	 */
4127b624ef0aSPyun YongHyeon 	reg = (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
4128b624ef0aSPyun YongHyeon 	    TSO_OFFLOAD_THRESH_MASK;
4129b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
4130b624ef0aSPyun YongHyeon 		reg |= TSO_OFFLOAD_ERRLGPKT_DROP_ENB;
4131b624ef0aSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH, reg);
4132d68875ebSPyun YongHyeon 	/* Configure TxQ. */
4133d68875ebSPyun YongHyeon 	reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
4134d68875ebSPyun YongHyeon 	    TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
41352f70cceaSPyun YongHyeon 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
41362f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
41372f70cceaSPyun YongHyeon 		reg >>= 1;
4138d68875ebSPyun YongHyeon 	reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
4139d68875ebSPyun YongHyeon 	    TXQ_CFG_TD_BURST_MASK;
4140b624ef0aSPyun YongHyeon 	reg |= TXQ_CFG_IP_OPTION_ENB | TXQ_CFG_8023_ENB;
4141d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
4142b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4143b624ef0aSPyun YongHyeon 		reg = (TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q1_BURST_SHIFT |
4144b624ef0aSPyun YongHyeon 		    TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q2_BURST_SHIFT |
4145b624ef0aSPyun YongHyeon 		    TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q3_BURST_SHIFT |
4146b624ef0aSPyun YongHyeon 		    HQTD_CFG_BURST_ENB);
4147b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_HQTD_CFG, reg);
4148b624ef0aSPyun YongHyeon 		reg = WRR_PRI_RESTRICT_NONE;
4149b624ef0aSPyun YongHyeon 		reg |= (WRR_PRI_DEFAULT << WRR_PRI0_SHIFT |
4150b624ef0aSPyun YongHyeon 		    WRR_PRI_DEFAULT << WRR_PRI1_SHIFT |
4151b624ef0aSPyun YongHyeon 		    WRR_PRI_DEFAULT << WRR_PRI2_SHIFT |
4152b624ef0aSPyun YongHyeon 		    WRR_PRI_DEFAULT << WRR_PRI3_SHIFT);
4153b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_WRR, reg);
4154b624ef0aSPyun YongHyeon 	} else {
4155d68875ebSPyun YongHyeon 		/* Configure Rx free descriptor pre-fetching. */
4156d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
4157b624ef0aSPyun YongHyeon 		    ((RX_RD_FREE_THRESH_HI_DEFAULT <<
4158b624ef0aSPyun YongHyeon 		    RX_RD_FREE_THRESH_HI_SHIFT) & RX_RD_FREE_THRESH_HI_MASK) |
4159b624ef0aSPyun YongHyeon 		    ((RX_RD_FREE_THRESH_LO_DEFAULT <<
4160b624ef0aSPyun YongHyeon 		    RX_RD_FREE_THRESH_LO_SHIFT) & RX_RD_FREE_THRESH_LO_MASK));
4161b624ef0aSPyun YongHyeon 	}
4162d68875ebSPyun YongHyeon 
4163d68875ebSPyun YongHyeon 	/*
4164d68875ebSPyun YongHyeon 	 * Configure flow control parameters.
4165d68875ebSPyun YongHyeon 	 * XON  : 80% of Rx FIFO
4166d68875ebSPyun YongHyeon 	 * XOFF : 30% of Rx FIFO
4167d68875ebSPyun YongHyeon 	 */
4168b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4169b624ef0aSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
4170b624ef0aSPyun YongHyeon 		reg &= SRAM_RX_FIFO_LEN_MASK;
4171b624ef0aSPyun YongHyeon 		reg *= 8;
4172b624ef0aSPyun YongHyeon 		if (reg > 8 * 1024)
4173b624ef0aSPyun YongHyeon 			reg -= RX_FIFO_PAUSE_816X_RSVD;
4174b624ef0aSPyun YongHyeon 		else
4175b624ef0aSPyun YongHyeon 			reg -= RX_BUF_SIZE_MAX;
4176b624ef0aSPyun YongHyeon 		reg /= 8;
4177b624ef0aSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
4178b624ef0aSPyun YongHyeon 		    ((reg << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
4179b624ef0aSPyun YongHyeon 		    RX_FIFO_PAUSE_THRESH_LO_MASK) |
4180b624ef0aSPyun YongHyeon 		    (((RX_FIFO_PAUSE_816X_RSVD / 8) <<
4181b624ef0aSPyun YongHyeon 		    RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
4182b624ef0aSPyun YongHyeon 		    RX_FIFO_PAUSE_THRESH_HI_MASK));
4183b624ef0aSPyun YongHyeon 	} else if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
41842f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132) {
4185d68875ebSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
4186d68875ebSPyun YongHyeon 		rxf_hi = (reg * 8) / 10;
4187d68875ebSPyun YongHyeon 		rxf_lo = (reg * 3) / 10;
4188d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
4189d68875ebSPyun YongHyeon 		    ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
4190d68875ebSPyun YongHyeon 		     RX_FIFO_PAUSE_THRESH_LO_MASK) |
4191d68875ebSPyun YongHyeon 		    ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
4192d68875ebSPyun YongHyeon 		     RX_FIFO_PAUSE_THRESH_HI_MASK));
41932f70cceaSPyun YongHyeon 	}
41942f70cceaSPyun YongHyeon 
4195b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4196d68875ebSPyun YongHyeon 		/* Disable RSS until I understand L1C/L2C's RSS logic. */
4197d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
4198d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
4199b624ef0aSPyun YongHyeon 	}
4200d68875ebSPyun YongHyeon 
4201d68875ebSPyun YongHyeon 	/* Configure RxQ. */
4202d68875ebSPyun YongHyeon 	reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
4203d68875ebSPyun YongHyeon 	    RXQ_CFG_RD_BURST_MASK;
4204d68875ebSPyun YongHyeon 	reg |= RXQ_CFG_RSS_MODE_DIS;
420503b4253bSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4206b624ef0aSPyun YongHyeon 		reg |= (RXQ_CFG_816X_IDT_TBL_SIZE_DEFAULT <<
4207b624ef0aSPyun YongHyeon 		    RXQ_CFG_816X_IDT_TBL_SIZE_SHIFT) &
4208b624ef0aSPyun YongHyeon 		    RXQ_CFG_816X_IDT_TBL_SIZE_MASK;
420903b4253bSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
421003b4253bSPyun YongHyeon 			reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_100M;
421103b4253bSPyun YongHyeon 	} else {
4212b624ef0aSPyun YongHyeon 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0 &&
4213b624ef0aSPyun YongHyeon 		    sc->alc_ident->deviceid != DEVICEID_ATHEROS_AR8151_V2)
421403b4253bSPyun YongHyeon 			reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_100M;
421503b4253bSPyun YongHyeon 	}
4216d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4217d68875ebSPyun YongHyeon 
4218d68875ebSPyun YongHyeon 	/* Configure DMA parameters. */
4219d68875ebSPyun YongHyeon 	reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
4220d68875ebSPyun YongHyeon 	reg |= sc->alc_rcb;
4221d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
4222d68875ebSPyun YongHyeon 		reg |= DMA_CFG_CMB_ENB;
4223d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
4224d68875ebSPyun YongHyeon 		reg |= DMA_CFG_SMB_ENB;
4225d68875ebSPyun YongHyeon 	else
4226d68875ebSPyun YongHyeon 		reg |= DMA_CFG_SMB_DIS;
4227d68875ebSPyun YongHyeon 	reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
4228d68875ebSPyun YongHyeon 	    DMA_CFG_RD_BURST_SHIFT;
4229d68875ebSPyun YongHyeon 	reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
4230d68875ebSPyun YongHyeon 	    DMA_CFG_WR_BURST_SHIFT;
4231d68875ebSPyun YongHyeon 	reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
4232d68875ebSPyun YongHyeon 	    DMA_CFG_RD_DELAY_CNT_MASK;
4233d68875ebSPyun YongHyeon 	reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
4234d68875ebSPyun YongHyeon 	    DMA_CFG_WR_DELAY_CNT_MASK;
4235b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4236b624ef0aSPyun YongHyeon 		switch (AR816X_REV(sc->alc_rev)) {
4237b624ef0aSPyun YongHyeon 		case AR816X_REV_A0:
4238b624ef0aSPyun YongHyeon 		case AR816X_REV_A1:
423903b4253bSPyun YongHyeon 			reg |= DMA_CFG_RD_CHNL_SEL_2;
4240b624ef0aSPyun YongHyeon 			break;
4241b624ef0aSPyun YongHyeon 		case AR816X_REV_B0:
4242b624ef0aSPyun YongHyeon 			/* FALLTHROUGH */
4243b624ef0aSPyun YongHyeon 		default:
424403b4253bSPyun YongHyeon 			reg |= DMA_CFG_RD_CHNL_SEL_4;
4245b624ef0aSPyun YongHyeon 			break;
4246b624ef0aSPyun YongHyeon 		}
4247b624ef0aSPyun YongHyeon 	}
4248d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
4249d68875ebSPyun YongHyeon 
4250d68875ebSPyun YongHyeon 	/*
4251d68875ebSPyun YongHyeon 	 * Configure Tx/Rx MACs.
4252d68875ebSPyun YongHyeon 	 *  - Auto-padding for short frames.
4253d68875ebSPyun YongHyeon 	 *  - Enable CRC generation.
4254d68875ebSPyun YongHyeon 	 *  Actual reconfiguration of MAC for resolved speed/duplex
4255d68875ebSPyun YongHyeon 	 *  is followed after detection of link establishment.
42562f70cceaSPyun YongHyeon 	 *  AR813x/AR815x always does checksum computation regardless
4257d68875ebSPyun YongHyeon 	 *  of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
4258d68875ebSPyun YongHyeon 	 *  have bug in protocol field in Rx return structure so
4259d68875ebSPyun YongHyeon 	 *  these controllers can't handle fragmented frames. Disable
4260d68875ebSPyun YongHyeon 	 *  Rx checksum offloading until there is a newer controller
4261d68875ebSPyun YongHyeon 	 *  that has sane implementation.
4262d68875ebSPyun YongHyeon 	 */
4263d68875ebSPyun YongHyeon 	reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
4264d68875ebSPyun YongHyeon 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
4265d68875ebSPyun YongHyeon 	    MAC_CFG_PREAMBLE_MASK);
4266b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
4267b624ef0aSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
42682f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
42692f70cceaSPyun YongHyeon 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
42702f70cceaSPyun YongHyeon 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
4271d68875ebSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
4272d68875ebSPyun YongHyeon 		reg |= MAC_CFG_SPEED_10_100;
4273d68875ebSPyun YongHyeon 	else
4274d68875ebSPyun YongHyeon 		reg |= MAC_CFG_SPEED_1000;
4275d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4276d68875ebSPyun YongHyeon 
4277d68875ebSPyun YongHyeon 	/* Set up the receive filter. */
4278d68875ebSPyun YongHyeon 	alc_rxfilter(sc);
4279d68875ebSPyun YongHyeon 	alc_rxvlan(sc);
4280d68875ebSPyun YongHyeon 
4281d68875ebSPyun YongHyeon 	/* Acknowledge all pending interrupts and clear it. */
4282d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
4283d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4284d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
4285d68875ebSPyun YongHyeon 
428652436412SJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
428752436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
4288b624ef0aSPyun YongHyeon 
4289b624ef0aSPyun YongHyeon 	sc->alc_flags &= ~ALC_FLAG_LINK;
4290b624ef0aSPyun YongHyeon 	/* Switch to the current media. */
4291b624ef0aSPyun YongHyeon 	alc_mediachange_locked(sc);
4292b624ef0aSPyun YongHyeon 
4293b624ef0aSPyun YongHyeon 	callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
4294d68875ebSPyun YongHyeon }
4295d68875ebSPyun YongHyeon 
4296d68875ebSPyun YongHyeon static void
alc_stop(struct alc_softc * sc)4297d68875ebSPyun YongHyeon alc_stop(struct alc_softc *sc)
4298d68875ebSPyun YongHyeon {
429952436412SJustin Hibbits 	if_t ifp;
4300d68875ebSPyun YongHyeon 	struct alc_txdesc *txd;
4301d68875ebSPyun YongHyeon 	struct alc_rxdesc *rxd;
4302d68875ebSPyun YongHyeon 	uint32_t reg;
4303d68875ebSPyun YongHyeon 	int i;
4304d68875ebSPyun YongHyeon 
4305d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4306d68875ebSPyun YongHyeon 	/*
4307d68875ebSPyun YongHyeon 	 * Mark the interface down and cancel the watchdog timer.
4308d68875ebSPyun YongHyeon 	 */
4309d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
431052436412SJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
4311d68875ebSPyun YongHyeon 	sc->alc_flags &= ~ALC_FLAG_LINK;
4312d68875ebSPyun YongHyeon 	callout_stop(&sc->alc_tick_ch);
4313d68875ebSPyun YongHyeon 	sc->alc_watchdog_timer = 0;
4314d68875ebSPyun YongHyeon 	alc_stats_update(sc);
4315d68875ebSPyun YongHyeon 	/* Disable interrupts. */
4316d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
4317d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4318d68875ebSPyun YongHyeon 	/* Disable DMA. */
4319d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_DMA_CFG);
4320d68875ebSPyun YongHyeon 	reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
4321d68875ebSPyun YongHyeon 	reg |= DMA_CFG_SMB_DIS;
4322d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
4323d68875ebSPyun YongHyeon 	DELAY(1000);
4324d68875ebSPyun YongHyeon 	/* Stop Rx/Tx MACs. */
4325d68875ebSPyun YongHyeon 	alc_stop_mac(sc);
4326d68875ebSPyun YongHyeon 	/* Disable interrupts which might be touched in taskq handler. */
4327d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4328b624ef0aSPyun YongHyeon 	/* Disable L0s/L1s */
4329b624ef0aSPyun YongHyeon 	alc_aspm(sc, 0, IFM_UNKNOWN);
4330d68875ebSPyun YongHyeon 	/* Reclaim Rx buffers that have been processed. */
4331d68875ebSPyun YongHyeon 	if (sc->alc_cdata.alc_rxhead != NULL)
4332d68875ebSPyun YongHyeon 		m_freem(sc->alc_cdata.alc_rxhead);
4333d68875ebSPyun YongHyeon 	ALC_RXCHAIN_RESET(sc);
4334d68875ebSPyun YongHyeon 	/*
4335d68875ebSPyun YongHyeon 	 * Free Tx/Rx mbufs still in the queues.
4336d68875ebSPyun YongHyeon 	 */
4337d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
4338d68875ebSPyun YongHyeon 		rxd = &sc->alc_cdata.alc_rxdesc[i];
4339d68875ebSPyun YongHyeon 		if (rxd->rx_m != NULL) {
4340d68875ebSPyun YongHyeon 			bus_dmamap_sync(sc->alc_cdata.alc_rx_tag,
4341d68875ebSPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
4342d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_rx_tag,
4343d68875ebSPyun YongHyeon 			    rxd->rx_dmamap);
4344d68875ebSPyun YongHyeon 			m_freem(rxd->rx_m);
4345d68875ebSPyun YongHyeon 			rxd->rx_m = NULL;
4346d68875ebSPyun YongHyeon 		}
4347d68875ebSPyun YongHyeon 	}
4348d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
4349d68875ebSPyun YongHyeon 		txd = &sc->alc_cdata.alc_txdesc[i];
4350d68875ebSPyun YongHyeon 		if (txd->tx_m != NULL) {
4351d68875ebSPyun YongHyeon 			bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
4352d68875ebSPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
4353d68875ebSPyun YongHyeon 			bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
4354d68875ebSPyun YongHyeon 			    txd->tx_dmamap);
4355d68875ebSPyun YongHyeon 			m_freem(txd->tx_m);
4356d68875ebSPyun YongHyeon 			txd->tx_m = NULL;
4357d68875ebSPyun YongHyeon 		}
4358d68875ebSPyun YongHyeon 	}
4359d68875ebSPyun YongHyeon }
4360d68875ebSPyun YongHyeon 
4361d68875ebSPyun YongHyeon static void
alc_stop_mac(struct alc_softc * sc)4362d68875ebSPyun YongHyeon alc_stop_mac(struct alc_softc *sc)
4363d68875ebSPyun YongHyeon {
4364d68875ebSPyun YongHyeon 	uint32_t reg;
4365d68875ebSPyun YongHyeon 	int i;
4366d68875ebSPyun YongHyeon 
4367b624ef0aSPyun YongHyeon 	alc_stop_queue(sc);
4368d68875ebSPyun YongHyeon 	/* Disable Rx/Tx MAC. */
4369d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
4370d68875ebSPyun YongHyeon 	if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
43715bec76e7SPyun YongHyeon 		reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
4372d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4373d68875ebSPyun YongHyeon 	}
4374d68875ebSPyun YongHyeon 	for (i = ALC_TIMEOUT; i > 0; i--) {
4375d68875ebSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
4376b624ef0aSPyun YongHyeon 		if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC)) == 0)
4377d68875ebSPyun YongHyeon 			break;
4378d68875ebSPyun YongHyeon 		DELAY(10);
4379d68875ebSPyun YongHyeon 	}
4380d68875ebSPyun YongHyeon 	if (i == 0)
4381d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
4382d68875ebSPyun YongHyeon 		    "could not disable Rx/Tx MAC(0x%08x)!\n", reg);
4383d68875ebSPyun YongHyeon }
4384d68875ebSPyun YongHyeon 
4385d68875ebSPyun YongHyeon static void
alc_start_queue(struct alc_softc * sc)4386d68875ebSPyun YongHyeon alc_start_queue(struct alc_softc *sc)
4387d68875ebSPyun YongHyeon {
4388d68875ebSPyun YongHyeon 	uint32_t qcfg[] = {
4389d68875ebSPyun YongHyeon 		0,
4390d68875ebSPyun YongHyeon 		RXQ_CFG_QUEUE0_ENB,
4391d68875ebSPyun YongHyeon 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
4392d68875ebSPyun YongHyeon 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
4393d68875ebSPyun YongHyeon 		RXQ_CFG_ENB
4394d68875ebSPyun YongHyeon 	};
4395d68875ebSPyun YongHyeon 	uint32_t cfg;
4396d68875ebSPyun YongHyeon 
4397d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4398d68875ebSPyun YongHyeon 
4399d68875ebSPyun YongHyeon 	/* Enable RxQ. */
4400d68875ebSPyun YongHyeon 	cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
4401b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4402d68875ebSPyun YongHyeon 		cfg &= ~RXQ_CFG_ENB;
4403d68875ebSPyun YongHyeon 		cfg |= qcfg[1];
4404b624ef0aSPyun YongHyeon 	} else
4405b624ef0aSPyun YongHyeon 		cfg |= RXQ_CFG_QUEUE0_ENB;
4406d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
4407d68875ebSPyun YongHyeon 	/* Enable TxQ. */
4408d68875ebSPyun YongHyeon 	cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
4409d68875ebSPyun YongHyeon 	cfg |= TXQ_CFG_ENB;
4410d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
4411d68875ebSPyun YongHyeon }
4412d68875ebSPyun YongHyeon 
4413d68875ebSPyun YongHyeon static void
alc_stop_queue(struct alc_softc * sc)4414d68875ebSPyun YongHyeon alc_stop_queue(struct alc_softc *sc)
4415d68875ebSPyun YongHyeon {
4416d68875ebSPyun YongHyeon 	uint32_t reg;
4417d68875ebSPyun YongHyeon 	int i;
4418d68875ebSPyun YongHyeon 
4419d68875ebSPyun YongHyeon 	/* Disable RxQ. */
4420d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_RXQ_CFG);
4421b624ef0aSPyun YongHyeon 	if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4422d68875ebSPyun YongHyeon 		if ((reg & RXQ_CFG_ENB) != 0) {
4423d68875ebSPyun YongHyeon 			reg &= ~RXQ_CFG_ENB;
4424d68875ebSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4425d68875ebSPyun YongHyeon 		}
4426b624ef0aSPyun YongHyeon 	} else {
4427b624ef0aSPyun YongHyeon 		if ((reg & RXQ_CFG_QUEUE0_ENB) != 0) {
4428b624ef0aSPyun YongHyeon 			reg &= ~RXQ_CFG_QUEUE0_ENB;
4429b624ef0aSPyun YongHyeon 			CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4430b624ef0aSPyun YongHyeon 		}
4431b624ef0aSPyun YongHyeon 	}
4432d68875ebSPyun YongHyeon 	/* Disable TxQ. */
4433d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_TXQ_CFG);
4434f69ddfbbSPyun YongHyeon 	if ((reg & TXQ_CFG_ENB) != 0) {
4435d68875ebSPyun YongHyeon 		reg &= ~TXQ_CFG_ENB;
4436d68875ebSPyun YongHyeon 		CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
4437d68875ebSPyun YongHyeon 	}
4438b624ef0aSPyun YongHyeon 	DELAY(40);
4439d68875ebSPyun YongHyeon 	for (i = ALC_TIMEOUT; i > 0; i--) {
4440d68875ebSPyun YongHyeon 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
4441d68875ebSPyun YongHyeon 		if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
4442d68875ebSPyun YongHyeon 			break;
4443d68875ebSPyun YongHyeon 		DELAY(10);
4444d68875ebSPyun YongHyeon 	}
4445d68875ebSPyun YongHyeon 	if (i == 0)
4446d68875ebSPyun YongHyeon 		device_printf(sc->alc_dev,
4447d68875ebSPyun YongHyeon 		    "could not disable RxQ/TxQ (0x%08x)!\n", reg);
4448d68875ebSPyun YongHyeon }
4449d68875ebSPyun YongHyeon 
4450d68875ebSPyun YongHyeon static void
alc_init_tx_ring(struct alc_softc * sc)4451d68875ebSPyun YongHyeon alc_init_tx_ring(struct alc_softc *sc)
4452d68875ebSPyun YongHyeon {
4453d68875ebSPyun YongHyeon 	struct alc_ring_data *rd;
4454d68875ebSPyun YongHyeon 	struct alc_txdesc *txd;
4455d68875ebSPyun YongHyeon 	int i;
4456d68875ebSPyun YongHyeon 
4457d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4458d68875ebSPyun YongHyeon 
4459d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_tx_prod = 0;
4460d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_tx_cons = 0;
4461d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_tx_cnt = 0;
4462d68875ebSPyun YongHyeon 
4463d68875ebSPyun YongHyeon 	rd = &sc->alc_rdata;
4464d68875ebSPyun YongHyeon 	bzero(rd->alc_tx_ring, ALC_TX_RING_SZ);
4465d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
4466d68875ebSPyun YongHyeon 		txd = &sc->alc_cdata.alc_txdesc[i];
4467d68875ebSPyun YongHyeon 		txd->tx_m = NULL;
4468d68875ebSPyun YongHyeon 	}
4469d68875ebSPyun YongHyeon 
4470d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
4471d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
4472d68875ebSPyun YongHyeon }
4473d68875ebSPyun YongHyeon 
4474d68875ebSPyun YongHyeon static int
alc_init_rx_ring(struct alc_softc * sc)4475d68875ebSPyun YongHyeon alc_init_rx_ring(struct alc_softc *sc)
4476d68875ebSPyun YongHyeon {
4477d68875ebSPyun YongHyeon 	struct alc_ring_data *rd;
4478d68875ebSPyun YongHyeon 	struct alc_rxdesc *rxd;
4479d68875ebSPyun YongHyeon 	int i;
4480d68875ebSPyun YongHyeon 
4481d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4482d68875ebSPyun YongHyeon 
4483d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
4484d68875ebSPyun YongHyeon 	sc->alc_morework = 0;
4485d68875ebSPyun YongHyeon 	rd = &sc->alc_rdata;
4486d68875ebSPyun YongHyeon 	bzero(rd->alc_rx_ring, ALC_RX_RING_SZ);
4487d68875ebSPyun YongHyeon 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
4488d68875ebSPyun YongHyeon 		rxd = &sc->alc_cdata.alc_rxdesc[i];
4489d68875ebSPyun YongHyeon 		rxd->rx_m = NULL;
4490d68875ebSPyun YongHyeon 		rxd->rx_desc = &rd->alc_rx_ring[i];
4491d68875ebSPyun YongHyeon 		if (alc_newbuf(sc, rxd) != 0)
4492d68875ebSPyun YongHyeon 			return (ENOBUFS);
4493d68875ebSPyun YongHyeon 	}
4494d68875ebSPyun YongHyeon 
4495d68875ebSPyun YongHyeon 	/*
4496d68875ebSPyun YongHyeon 	 * Since controller does not update Rx descriptors, driver
4497d68875ebSPyun YongHyeon 	 * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
4498d68875ebSPyun YongHyeon 	 * is enough to ensure coherence.
4499d68875ebSPyun YongHyeon 	 */
4500d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
4501d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
4502d68875ebSPyun YongHyeon 	/* Let controller know availability of new Rx buffers. */
4503d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
4504d68875ebSPyun YongHyeon 
4505d68875ebSPyun YongHyeon 	return (0);
4506d68875ebSPyun YongHyeon }
4507d68875ebSPyun YongHyeon 
4508d68875ebSPyun YongHyeon static void
alc_init_rr_ring(struct alc_softc * sc)4509d68875ebSPyun YongHyeon alc_init_rr_ring(struct alc_softc *sc)
4510d68875ebSPyun YongHyeon {
4511d68875ebSPyun YongHyeon 	struct alc_ring_data *rd;
4512d68875ebSPyun YongHyeon 
4513d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4514d68875ebSPyun YongHyeon 
4515d68875ebSPyun YongHyeon 	sc->alc_cdata.alc_rr_cons = 0;
4516d68875ebSPyun YongHyeon 	ALC_RXCHAIN_RESET(sc);
4517d68875ebSPyun YongHyeon 
4518d68875ebSPyun YongHyeon 	rd = &sc->alc_rdata;
4519d68875ebSPyun YongHyeon 	bzero(rd->alc_rr_ring, ALC_RR_RING_SZ);
4520d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
4521d68875ebSPyun YongHyeon 	    sc->alc_cdata.alc_rr_ring_map,
4522d68875ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4523d68875ebSPyun YongHyeon }
4524d68875ebSPyun YongHyeon 
4525d68875ebSPyun YongHyeon static void
alc_init_cmb(struct alc_softc * sc)4526d68875ebSPyun YongHyeon alc_init_cmb(struct alc_softc *sc)
4527d68875ebSPyun YongHyeon {
4528d68875ebSPyun YongHyeon 	struct alc_ring_data *rd;
4529d68875ebSPyun YongHyeon 
4530d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4531d68875ebSPyun YongHyeon 
4532d68875ebSPyun YongHyeon 	rd = &sc->alc_rdata;
4533d68875ebSPyun YongHyeon 	bzero(rd->alc_cmb, ALC_CMB_SZ);
4534d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag, sc->alc_cdata.alc_cmb_map,
4535d68875ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4536d68875ebSPyun YongHyeon }
4537d68875ebSPyun YongHyeon 
4538d68875ebSPyun YongHyeon static void
alc_init_smb(struct alc_softc * sc)4539d68875ebSPyun YongHyeon alc_init_smb(struct alc_softc *sc)
4540d68875ebSPyun YongHyeon {
4541d68875ebSPyun YongHyeon 	struct alc_ring_data *rd;
4542d68875ebSPyun YongHyeon 
4543d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4544d68875ebSPyun YongHyeon 
4545d68875ebSPyun YongHyeon 	rd = &sc->alc_rdata;
4546d68875ebSPyun YongHyeon 	bzero(rd->alc_smb, ALC_SMB_SZ);
4547d68875ebSPyun YongHyeon 	bus_dmamap_sync(sc->alc_cdata.alc_smb_tag, sc->alc_cdata.alc_smb_map,
4548d68875ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4549d68875ebSPyun YongHyeon }
4550d68875ebSPyun YongHyeon 
4551d68875ebSPyun YongHyeon static void
alc_rxvlan(struct alc_softc * sc)4552d68875ebSPyun YongHyeon alc_rxvlan(struct alc_softc *sc)
4553d68875ebSPyun YongHyeon {
455452436412SJustin Hibbits 	if_t ifp;
4555d68875ebSPyun YongHyeon 	uint32_t reg;
4556d68875ebSPyun YongHyeon 
4557d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4558d68875ebSPyun YongHyeon 
4559d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
4560d68875ebSPyun YongHyeon 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
456152436412SJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
4562d68875ebSPyun YongHyeon 		reg |= MAC_CFG_VLAN_TAG_STRIP;
4563d68875ebSPyun YongHyeon 	else
4564d68875ebSPyun YongHyeon 		reg &= ~MAC_CFG_VLAN_TAG_STRIP;
4565d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4566d68875ebSPyun YongHyeon }
4567d68875ebSPyun YongHyeon 
4568eed57e32SGleb Smirnoff static u_int
alc_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)4569eed57e32SGleb Smirnoff alc_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
4570eed57e32SGleb Smirnoff {
4571eed57e32SGleb Smirnoff 	uint32_t *mchash = arg;
4572eed57e32SGleb Smirnoff 	uint32_t crc;
4573eed57e32SGleb Smirnoff 
4574eed57e32SGleb Smirnoff 	crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
4575eed57e32SGleb Smirnoff 	mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
4576eed57e32SGleb Smirnoff 
4577eed57e32SGleb Smirnoff 	return (1);
4578eed57e32SGleb Smirnoff }
4579eed57e32SGleb Smirnoff 
4580d68875ebSPyun YongHyeon static void
alc_rxfilter(struct alc_softc * sc)4581d68875ebSPyun YongHyeon alc_rxfilter(struct alc_softc *sc)
4582d68875ebSPyun YongHyeon {
458352436412SJustin Hibbits 	if_t ifp;
4584d68875ebSPyun YongHyeon 	uint32_t mchash[2];
4585d68875ebSPyun YongHyeon 	uint32_t rxcfg;
4586d68875ebSPyun YongHyeon 
4587d68875ebSPyun YongHyeon 	ALC_LOCK_ASSERT(sc);
4588d68875ebSPyun YongHyeon 
4589d68875ebSPyun YongHyeon 	ifp = sc->alc_ifp;
4590d68875ebSPyun YongHyeon 
4591d68875ebSPyun YongHyeon 	bzero(mchash, sizeof(mchash));
4592d68875ebSPyun YongHyeon 	rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
4593d68875ebSPyun YongHyeon 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
459452436412SJustin Hibbits 	if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
4595d68875ebSPyun YongHyeon 		rxcfg |= MAC_CFG_BCAST;
459652436412SJustin Hibbits 	if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
459752436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_PROMISC) != 0)
4598d68875ebSPyun YongHyeon 			rxcfg |= MAC_CFG_PROMISC;
459952436412SJustin Hibbits 		if ((if_getflags(ifp) & IFF_ALLMULTI) != 0)
4600d68875ebSPyun YongHyeon 			rxcfg |= MAC_CFG_ALLMULTI;
4601d68875ebSPyun YongHyeon 		mchash[0] = 0xFFFFFFFF;
4602d68875ebSPyun YongHyeon 		mchash[1] = 0xFFFFFFFF;
4603d68875ebSPyun YongHyeon 		goto chipit;
4604d68875ebSPyun YongHyeon 	}
4605d68875ebSPyun YongHyeon 
4606eed57e32SGleb Smirnoff 	if_foreach_llmaddr(ifp, alc_hash_maddr, mchash);
4607d68875ebSPyun YongHyeon 
4608d68875ebSPyun YongHyeon chipit:
4609d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
4610d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
4611d68875ebSPyun YongHyeon 	CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
4612d68875ebSPyun YongHyeon }
4613d68875ebSPyun YongHyeon 
4614d68875ebSPyun YongHyeon static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)4615d68875ebSPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4616d68875ebSPyun YongHyeon {
4617d68875ebSPyun YongHyeon 	int error, value;
4618d68875ebSPyun YongHyeon 
4619d68875ebSPyun YongHyeon 	if (arg1 == NULL)
4620d68875ebSPyun YongHyeon 		return (EINVAL);
4621d68875ebSPyun YongHyeon 	value = *(int *)arg1;
4622d68875ebSPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
4623d68875ebSPyun YongHyeon 	if (error || req->newptr == NULL)
4624d68875ebSPyun YongHyeon 		return (error);
4625d68875ebSPyun YongHyeon 	if (value < low || value > high)
4626d68875ebSPyun YongHyeon 		return (EINVAL);
4627d68875ebSPyun YongHyeon 	*(int *)arg1 = value;
4628d68875ebSPyun YongHyeon 
4629d68875ebSPyun YongHyeon 	return (0);
4630d68875ebSPyun YongHyeon }
4631d68875ebSPyun YongHyeon 
4632d68875ebSPyun YongHyeon static int
sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS)4633d68875ebSPyun YongHyeon sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS)
4634d68875ebSPyun YongHyeon {
4635d68875ebSPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req,
4636d68875ebSPyun YongHyeon 	    ALC_PROC_MIN, ALC_PROC_MAX));
4637d68875ebSPyun YongHyeon }
4638d68875ebSPyun YongHyeon 
4639d68875ebSPyun YongHyeon static int
sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS)4640d68875ebSPyun YongHyeon sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS)
4641d68875ebSPyun YongHyeon {
4642d68875ebSPyun YongHyeon 
4643d68875ebSPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req,
4644d68875ebSPyun YongHyeon 	    ALC_IM_TIMER_MIN, ALC_IM_TIMER_MAX));
4645d68875ebSPyun YongHyeon }
46468a466583SMark Johnston 
46477790c8c1SConrad Meyer #ifdef DEBUGNET
46488a466583SMark Johnston static void
alc_debugnet_init(if_t ifp,int * nrxr,int * ncl,int * clsize)464952436412SJustin Hibbits alc_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
46508a466583SMark Johnston {
46517bc66190SMateusz Guzik 	struct alc_softc *sc __diagused;
46528a466583SMark Johnston 
46538a466583SMark Johnston 	sc = if_getsoftc(ifp);
46548a466583SMark Johnston 	KASSERT(sc->alc_buf_size <= MCLBYTES, ("incorrect cluster size"));
46558a466583SMark Johnston 
46568a466583SMark Johnston 	*nrxr = ALC_RX_RING_CNT;
46577790c8c1SConrad Meyer 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
46588a466583SMark Johnston 	*clsize = MCLBYTES;
46598a466583SMark Johnston }
46608a466583SMark Johnston 
46618a466583SMark Johnston static void
alc_debugnet_event(if_t ifp __unused,enum debugnet_ev event __unused)466252436412SJustin Hibbits alc_debugnet_event(if_t ifp __unused, enum debugnet_ev event __unused)
46638a466583SMark Johnston {
46648a466583SMark Johnston }
46658a466583SMark Johnston 
46668a466583SMark Johnston static int
alc_debugnet_transmit(if_t ifp,struct mbuf * m)466752436412SJustin Hibbits alc_debugnet_transmit(if_t ifp, struct mbuf *m)
46688a466583SMark Johnston {
46698a466583SMark Johnston 	struct alc_softc *sc;
46708a466583SMark Johnston 	int error;
46718a466583SMark Johnston 
46728a466583SMark Johnston 	sc = if_getsoftc(ifp);
46738a466583SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
46748a466583SMark Johnston 	    IFF_DRV_RUNNING)
46758a466583SMark Johnston 		return (EBUSY);
46768a466583SMark Johnston 
46778a466583SMark Johnston 	error = alc_encap(sc, &m);
46788a466583SMark Johnston 	if (error == 0)
46798a466583SMark Johnston 		alc_start_tx(sc);
46808a466583SMark Johnston 	return (error);
46818a466583SMark Johnston }
46828a466583SMark Johnston 
46838a466583SMark Johnston static int
alc_debugnet_poll(if_t ifp,int count)468452436412SJustin Hibbits alc_debugnet_poll(if_t ifp, int count)
46858a466583SMark Johnston {
46868a466583SMark Johnston 	struct alc_softc *sc;
46878a466583SMark Johnston 
46888a466583SMark Johnston 	sc = if_getsoftc(ifp);
46898a466583SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
46908a466583SMark Johnston 	    IFF_DRV_RUNNING)
46918a466583SMark Johnston 		return (EBUSY);
46928a466583SMark Johnston 
46938a466583SMark Johnston 	alc_txeof(sc);
46948a466583SMark Johnston 	return (alc_rxintr(sc, count));
46958a466583SMark Johnston }
46967790c8c1SConrad Meyer #endif /* DEBUGNET */
4697