xref: /freebsd/sys/dev/ae/if_ae.c (revision 3f14814f05b5085b408ea25ae0bca68f4603c0ea)
1ba26d470SStanislav Sedov /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
4ba26d470SStanislav Sedov  * Copyright (c) 2008 Stanislav Sedov <stas@FreeBSD.org>.
5ba26d470SStanislav Sedov  * All rights reserved.
6ba26d470SStanislav Sedov  *
7ba26d470SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
8ba26d470SStanislav Sedov  * modification, are permitted provided that the following conditions
9ba26d470SStanislav Sedov  * are met:
10ba26d470SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
11ba26d470SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
12ba26d470SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
13ba26d470SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
14ba26d470SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
15ba26d470SStanislav Sedov  *
16ba26d470SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ba26d470SStanislav Sedov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ba26d470SStanislav Sedov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ba26d470SStanislav Sedov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ba26d470SStanislav Sedov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ba26d470SStanislav Sedov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ba26d470SStanislav Sedov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ba26d470SStanislav Sedov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ba26d470SStanislav Sedov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ba26d470SStanislav Sedov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ba26d470SStanislav Sedov  *
27ba26d470SStanislav Sedov  * Driver for Attansic Technology Corp. L2 FastEthernet adapter.
28ba26d470SStanislav Sedov  *
29ba26d470SStanislav Sedov  * This driver is heavily based on age(4) Attansic L1 driver by Pyun YongHyeon.
30ba26d470SStanislav Sedov  */
31ba26d470SStanislav Sedov 
32ba26d470SStanislav Sedov #include <sys/cdefs.h>
33ba26d470SStanislav Sedov __FBSDID("$FreeBSD$");
34ba26d470SStanislav Sedov 
35ba26d470SStanislav Sedov #include <sys/param.h>
36ba26d470SStanislav Sedov #include <sys/systm.h>
37ba26d470SStanislav Sedov #include <sys/bus.h>
38ba26d470SStanislav Sedov #include <sys/endian.h>
39ba26d470SStanislav Sedov #include <sys/kernel.h>
4076039bc8SGleb Smirnoff #include <sys/lock.h>
41ba26d470SStanislav Sedov #include <sys/malloc.h>
42ba26d470SStanislav Sedov #include <sys/mbuf.h>
4376039bc8SGleb Smirnoff #include <sys/mutex.h>
44ba26d470SStanislav Sedov #include <sys/rman.h>
45ba26d470SStanislav Sedov #include <sys/module.h>
46ba26d470SStanislav Sedov #include <sys/queue.h>
47ba26d470SStanislav Sedov #include <sys/socket.h>
48ba26d470SStanislav Sedov #include <sys/sockio.h>
49ba26d470SStanislav Sedov #include <sys/sysctl.h>
50ba26d470SStanislav Sedov #include <sys/taskqueue.h>
51ba26d470SStanislav Sedov 
52ba26d470SStanislav Sedov #include <net/bpf.h>
53ba26d470SStanislav Sedov #include <net/if.h>
5476039bc8SGleb Smirnoff #include <net/if_var.h>
55ba26d470SStanislav Sedov #include <net/if_arp.h>
56ba26d470SStanislav Sedov #include <net/ethernet.h>
57ba26d470SStanislav Sedov #include <net/if_dl.h>
58ba26d470SStanislav Sedov #include <net/if_media.h>
59ba26d470SStanislav Sedov #include <net/if_types.h>
60ba26d470SStanislav Sedov #include <net/if_vlan_var.h>
61ba26d470SStanislav Sedov 
62ba26d470SStanislav Sedov #include <netinet/in.h>
63ba26d470SStanislav Sedov #include <netinet/in_systm.h>
64ba26d470SStanislav Sedov #include <netinet/ip.h>
65ba26d470SStanislav Sedov #include <netinet/tcp.h>
66ba26d470SStanislav Sedov 
67ba26d470SStanislav Sedov #include <dev/mii/mii.h>
68ba26d470SStanislav Sedov #include <dev/mii/miivar.h>
69ba26d470SStanislav Sedov #include <dev/pci/pcireg.h>
70ba26d470SStanislav Sedov #include <dev/pci/pcivar.h>
71ba26d470SStanislav Sedov 
72ba26d470SStanislav Sedov #include <machine/bus.h>
73ba26d470SStanislav Sedov 
74ba26d470SStanislav Sedov #include "miibus_if.h"
75ba26d470SStanislav Sedov 
76ba26d470SStanislav Sedov #include "if_aereg.h"
77ba26d470SStanislav Sedov #include "if_aevar.h"
78ba26d470SStanislav Sedov 
79ba26d470SStanislav Sedov /*
80ba26d470SStanislav Sedov  * Devices supported by this driver.
81ba26d470SStanislav Sedov  */
82ba26d470SStanislav Sedov static struct ae_dev {
83ba26d470SStanislav Sedov 	uint16_t	vendorid;
84ba26d470SStanislav Sedov 	uint16_t	deviceid;
85ba26d470SStanislav Sedov 	const char	*name;
86ba26d470SStanislav Sedov } ae_devs[] = {
87ba26d470SStanislav Sedov 	{ VENDORID_ATTANSIC, DEVICEID_ATTANSIC_L2,
88ba26d470SStanislav Sedov 		"Attansic Technology Corp, L2 FastEthernet" },
89ba26d470SStanislav Sedov };
90a380994fSPedro F. Giffuni #define	AE_DEVS_COUNT nitems(ae_devs)
91ba26d470SStanislav Sedov 
92ba26d470SStanislav Sedov static struct resource_spec ae_res_spec_mem[] = {
93ba26d470SStanislav Sedov 	{ SYS_RES_MEMORY,       PCIR_BAR(0),    RF_ACTIVE },
94ba26d470SStanislav Sedov 	{ -1,			0,		0 }
95ba26d470SStanislav Sedov };
96ba26d470SStanislav Sedov static struct resource_spec ae_res_spec_irq[] = {
97ba26d470SStanislav Sedov 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
98ba26d470SStanislav Sedov 	{ -1,			0,		0 }
99ba26d470SStanislav Sedov };
100ba26d470SStanislav Sedov static struct resource_spec ae_res_spec_msi[] = {
101ba26d470SStanislav Sedov 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
102ba26d470SStanislav Sedov 	{ -1,			0,		0 }
103ba26d470SStanislav Sedov };
104ba26d470SStanislav Sedov 
105ba26d470SStanislav Sedov static int	ae_probe(device_t dev);
106ba26d470SStanislav Sedov static int	ae_attach(device_t dev);
107ba26d470SStanislav Sedov static void	ae_pcie_init(ae_softc_t *sc);
108ba26d470SStanislav Sedov static void	ae_phy_reset(ae_softc_t *sc);
109ba26d470SStanislav Sedov static void	ae_phy_init(ae_softc_t *sc);
110ba26d470SStanislav Sedov static int	ae_reset(ae_softc_t *sc);
111ba26d470SStanislav Sedov static void	ae_init(void *arg);
112ba26d470SStanislav Sedov static int	ae_init_locked(ae_softc_t *sc);
113850f048eSWarner Losh static int	ae_detach(device_t dev);
114ba26d470SStanislav Sedov static int	ae_miibus_readreg(device_t dev, int phy, int reg);
115ba26d470SStanislav Sedov static int	ae_miibus_writereg(device_t dev, int phy, int reg, int val);
116ba26d470SStanislav Sedov static void	ae_miibus_statchg(device_t dev);
117*3f14814fSJustin Hibbits static void	ae_mediastatus(if_t ifp, struct ifmediareq *ifmr);
118*3f14814fSJustin Hibbits static int	ae_mediachange(if_t ifp);
119ba26d470SStanislav Sedov static void	ae_retrieve_address(ae_softc_t *sc);
120ba26d470SStanislav Sedov static void	ae_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs,
121ba26d470SStanislav Sedov     int error);
122ba26d470SStanislav Sedov static int	ae_alloc_rings(ae_softc_t *sc);
123ba26d470SStanislav Sedov static void	ae_dma_free(ae_softc_t *sc);
124ba26d470SStanislav Sedov static int	ae_shutdown(device_t dev);
125ba26d470SStanislav Sedov static int	ae_suspend(device_t dev);
126ba26d470SStanislav Sedov static void	ae_powersave_disable(ae_softc_t *sc);
127ba26d470SStanislav Sedov static void	ae_powersave_enable(ae_softc_t *sc);
128ba26d470SStanislav Sedov static int	ae_resume(device_t dev);
129ba26d470SStanislav Sedov static unsigned int	ae_tx_avail_size(ae_softc_t *sc);
130ba26d470SStanislav Sedov static int	ae_encap(ae_softc_t *sc, struct mbuf **m_head);
131*3f14814fSJustin Hibbits static void	ae_start(if_t ifp);
132*3f14814fSJustin Hibbits static void	ae_start_locked(if_t ifp);
133ba26d470SStanislav Sedov static void	ae_link_task(void *arg, int pending);
134ba26d470SStanislav Sedov static void	ae_stop_rxmac(ae_softc_t *sc);
135ba26d470SStanislav Sedov static void	ae_stop_txmac(ae_softc_t *sc);
136ba26d470SStanislav Sedov static void	ae_mac_config(ae_softc_t *sc);
137ba26d470SStanislav Sedov static int	ae_intr(void *arg);
138ba26d470SStanislav Sedov static void	ae_int_task(void *arg, int pending);
139ba26d470SStanislav Sedov static void	ae_tx_intr(ae_softc_t *sc);
1402e6383a8SPyun YongHyeon static void	ae_rxeof(ae_softc_t *sc, ae_rxd_t *rxd);
141ba26d470SStanislav Sedov static void	ae_rx_intr(ae_softc_t *sc);
142ba26d470SStanislav Sedov static void	ae_watchdog(ae_softc_t *sc);
143ba26d470SStanislav Sedov static void	ae_tick(void *arg);
144ba26d470SStanislav Sedov static void	ae_rxfilter(ae_softc_t *sc);
145ba26d470SStanislav Sedov static void	ae_rxvlan(ae_softc_t *sc);
146*3f14814fSJustin Hibbits static int	ae_ioctl(if_t ifp, u_long cmd, caddr_t data);
147ba26d470SStanislav Sedov static void	ae_stop(ae_softc_t *sc);
148ba26d470SStanislav Sedov static int	ae_check_eeprom_present(ae_softc_t *sc, int *vpdc);
149ba26d470SStanislav Sedov static int	ae_vpd_read_word(ae_softc_t *sc, int reg, uint32_t *word);
150ba26d470SStanislav Sedov static int	ae_get_vpd_eaddr(ae_softc_t *sc, uint32_t *eaddr);
151ba26d470SStanislav Sedov static int	ae_get_reg_eaddr(ae_softc_t *sc, uint32_t *eaddr);
152ba26d470SStanislav Sedov static void	ae_update_stats_rx(uint16_t flags, ae_stats_t *stats);
153ba26d470SStanislav Sedov static void	ae_update_stats_tx(uint16_t flags, ae_stats_t *stats);
154ba26d470SStanislav Sedov static void	ae_init_tunables(ae_softc_t *sc);
155ba26d470SStanislav Sedov 
156ba26d470SStanislav Sedov static device_method_t ae_methods[] = {
157ba26d470SStanislav Sedov 	/* Device interface. */
158ba26d470SStanislav Sedov 	DEVMETHOD(device_probe,		ae_probe),
159ba26d470SStanislav Sedov 	DEVMETHOD(device_attach,	ae_attach),
160ba26d470SStanislav Sedov 	DEVMETHOD(device_detach,	ae_detach),
161ba26d470SStanislav Sedov 	DEVMETHOD(device_shutdown,	ae_shutdown),
162ba26d470SStanislav Sedov 	DEVMETHOD(device_suspend,	ae_suspend),
163ba26d470SStanislav Sedov 	DEVMETHOD(device_resume,	ae_resume),
164ba26d470SStanislav Sedov 
165ba26d470SStanislav Sedov 	/* MII interface. */
166ba26d470SStanislav Sedov 	DEVMETHOD(miibus_readreg,	ae_miibus_readreg),
167ba26d470SStanislav Sedov 	DEVMETHOD(miibus_writereg,	ae_miibus_writereg),
168ba26d470SStanislav Sedov 	DEVMETHOD(miibus_statchg,	ae_miibus_statchg),
169ba26d470SStanislav Sedov 	{ NULL, NULL }
170ba26d470SStanislav Sedov };
171ba26d470SStanislav Sedov static driver_t ae_driver = {
172ba26d470SStanislav Sedov         "ae",
173ba26d470SStanislav Sedov         ae_methods,
174ba26d470SStanislav Sedov         sizeof(ae_softc_t)
175ba26d470SStanislav Sedov };
176ba26d470SStanislav Sedov 
17727f71c58SJohn Baldwin DRIVER_MODULE(ae, pci, ae_driver, 0, 0);
17875b6758dSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, ae, ae_devs,
179329e817fSWarner Losh     nitems(ae_devs));
1803e38757dSJohn Baldwin DRIVER_MODULE(miibus, ae, miibus_driver, 0, 0);
181ba26d470SStanislav Sedov MODULE_DEPEND(ae, pci, 1, 1, 1);
182ba26d470SStanislav Sedov MODULE_DEPEND(ae, ether, 1, 1, 1);
183ba26d470SStanislav Sedov MODULE_DEPEND(ae, miibus, 1, 1, 1);
184ba26d470SStanislav Sedov 
185ba26d470SStanislav Sedov /*
186ba26d470SStanislav Sedov  * Tunables.
187ba26d470SStanislav Sedov  */
188ba26d470SStanislav Sedov static int msi_disable = 0;
189ba26d470SStanislav Sedov TUNABLE_INT("hw.ae.msi_disable", &msi_disable);
190ba26d470SStanislav Sedov 
191ba26d470SStanislav Sedov #define	AE_READ_4(sc, reg) \
192ba26d470SStanislav Sedov 	bus_read_4((sc)->mem[0], (reg))
193ba26d470SStanislav Sedov #define	AE_READ_2(sc, reg) \
194ba26d470SStanislav Sedov 	bus_read_2((sc)->mem[0], (reg))
195ba26d470SStanislav Sedov #define	AE_READ_1(sc, reg) \
196ba26d470SStanislav Sedov 	bus_read_1((sc)->mem[0], (reg))
197ba26d470SStanislav Sedov #define	AE_WRITE_4(sc, reg, val) \
198ba26d470SStanislav Sedov 	bus_write_4((sc)->mem[0], (reg), (val))
199ba26d470SStanislav Sedov #define	AE_WRITE_2(sc, reg, val) \
200ba26d470SStanislav Sedov 	bus_write_2((sc)->mem[0], (reg), (val))
201ba26d470SStanislav Sedov #define	AE_WRITE_1(sc, reg, val) \
202ba26d470SStanislav Sedov 	bus_write_1((sc)->mem[0], (reg), (val))
203ba26d470SStanislav Sedov #define	AE_PHY_READ(sc, reg) \
204ba26d470SStanislav Sedov 	ae_miibus_readreg(sc->dev, 0, reg)
205ba26d470SStanislav Sedov #define	AE_PHY_WRITE(sc, reg, val) \
206ba26d470SStanislav Sedov 	ae_miibus_writereg(sc->dev, 0, reg, val)
207ba26d470SStanislav Sedov #define	AE_CHECK_EADDR_VALID(eaddr) \
208ba26d470SStanislav Sedov 	((eaddr[0] == 0 && eaddr[1] == 0) || \
209ba26d470SStanislav Sedov 	(eaddr[0] == 0xffffffff && eaddr[1] == 0xffff))
210ba26d470SStanislav Sedov #define	AE_RXD_VLAN(vtag) \
211ba26d470SStanislav Sedov 	(((vtag) >> 4) | (((vtag) & 0x07) << 13) | (((vtag) & 0x08) << 9))
212ba26d470SStanislav Sedov #define	AE_TXD_VLAN(vtag) \
213ba26d470SStanislav Sedov 	(((vtag) << 4) | (((vtag) >> 13) & 0x07) | (((vtag) >> 9) & 0x08))
214ba26d470SStanislav Sedov 
215ba26d470SStanislav Sedov static int
216ba26d470SStanislav Sedov ae_probe(device_t dev)
217ba26d470SStanislav Sedov {
218ba26d470SStanislav Sedov 	uint16_t deviceid, vendorid;
219ba26d470SStanislav Sedov 	int i;
220ba26d470SStanislav Sedov 
221ba26d470SStanislav Sedov 	vendorid = pci_get_vendor(dev);
222ba26d470SStanislav Sedov 	deviceid = pci_get_device(dev);
223ba26d470SStanislav Sedov 
224ba26d470SStanislav Sedov 	/*
225ba26d470SStanislav Sedov 	 * Search through the list of supported devs for matching one.
226ba26d470SStanislav Sedov 	 */
227ba26d470SStanislav Sedov 	for (i = 0; i < AE_DEVS_COUNT; i++) {
228ba26d470SStanislav Sedov 		if (vendorid == ae_devs[i].vendorid &&
229ba26d470SStanislav Sedov 		    deviceid == ae_devs[i].deviceid) {
230ba26d470SStanislav Sedov 			device_set_desc(dev, ae_devs[i].name);
231ba26d470SStanislav Sedov 			return (BUS_PROBE_DEFAULT);
232ba26d470SStanislav Sedov 		}
233ba26d470SStanislav Sedov 	}
234ba26d470SStanislav Sedov 	return (ENXIO);
235ba26d470SStanislav Sedov }
236ba26d470SStanislav Sedov 
237ba26d470SStanislav Sedov static int
238ba26d470SStanislav Sedov ae_attach(device_t dev)
239ba26d470SStanislav Sedov {
240ba26d470SStanislav Sedov 	ae_softc_t *sc;
241*3f14814fSJustin Hibbits 	if_t ifp;
242ba26d470SStanislav Sedov 	uint8_t chiprev;
243ba26d470SStanislav Sedov 	uint32_t pcirev;
244ba26d470SStanislav Sedov 	int nmsi, pmc;
245ba26d470SStanislav Sedov 	int error;
246ba26d470SStanislav Sedov 
247ba26d470SStanislav Sedov 	sc = device_get_softc(dev); /* Automatically allocated and zeroed
248ba26d470SStanislav Sedov 				       on attach. */
249ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
250ba26d470SStanislav Sedov 	sc->dev = dev;
251ba26d470SStanislav Sedov 
252ba26d470SStanislav Sedov 	/*
253ba26d470SStanislav Sedov 	 * Initialize mutexes and tasks.
254ba26d470SStanislav Sedov 	 */
255ba26d470SStanislav Sedov 	mtx_init(&sc->mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, MTX_DEF);
256ba26d470SStanislav Sedov 	callout_init_mtx(&sc->tick_ch, &sc->mtx, 0);
257ba26d470SStanislav Sedov 	TASK_INIT(&sc->int_task, 0, ae_int_task, sc);
258ba26d470SStanislav Sedov 	TASK_INIT(&sc->link_task, 0, ae_link_task, sc);
259ba26d470SStanislav Sedov 
260ba26d470SStanislav Sedov 	pci_enable_busmaster(dev);		/* Enable bus mastering. */
261ba26d470SStanislav Sedov 
262ba26d470SStanislav Sedov 	sc->spec_mem = ae_res_spec_mem;
263ba26d470SStanislav Sedov 
264ba26d470SStanislav Sedov 	/*
265ba26d470SStanislav Sedov 	 * Allocate memory-mapped registers.
266ba26d470SStanislav Sedov 	 */
267ba26d470SStanislav Sedov 	error = bus_alloc_resources(dev, sc->spec_mem, sc->mem);
268ba26d470SStanislav Sedov 	if (error != 0) {
269ba26d470SStanislav Sedov 		device_printf(dev, "could not allocate memory resources.\n");
270ba26d470SStanislav Sedov 		sc->spec_mem = NULL;
271ba26d470SStanislav Sedov 		goto fail;
272ba26d470SStanislav Sedov 	}
273ba26d470SStanislav Sedov 
274ba26d470SStanislav Sedov 	/*
275ba26d470SStanislav Sedov 	 * Retrieve PCI and chip revisions.
276ba26d470SStanislav Sedov 	 */
277ba26d470SStanislav Sedov 	pcirev = pci_get_revid(dev);
278ba26d470SStanislav Sedov 	chiprev = (AE_READ_4(sc, AE_MASTER_REG) >> AE_MASTER_REVNUM_SHIFT) &
279ba26d470SStanislav Sedov 	    AE_MASTER_REVNUM_MASK;
280ba26d470SStanislav Sedov 	if (bootverbose) {
281ba26d470SStanislav Sedov 		device_printf(dev, "pci device revision: %#04x\n", pcirev);
282ba26d470SStanislav Sedov 		device_printf(dev, "chip id: %#02x\n", chiprev);
283ba26d470SStanislav Sedov 	}
284ba26d470SStanislav Sedov 	nmsi = pci_msi_count(dev);
285ba26d470SStanislav Sedov 	if (bootverbose)
286ba26d470SStanislav Sedov 		device_printf(dev, "MSI count: %d.\n", nmsi);
287ba26d470SStanislav Sedov 
288ba26d470SStanislav Sedov 	/*
289ba26d470SStanislav Sedov 	 * Allocate interrupt resources.
290ba26d470SStanislav Sedov 	 */
291ba26d470SStanislav Sedov 	if (msi_disable == 0 && nmsi == 1) {
292ba26d470SStanislav Sedov 		error = pci_alloc_msi(dev, &nmsi);
293ba26d470SStanislav Sedov 		if (error == 0) {
294ba26d470SStanislav Sedov 			device_printf(dev, "Using MSI messages.\n");
295ba26d470SStanislav Sedov 			sc->spec_irq = ae_res_spec_msi;
296ba26d470SStanislav Sedov 			error = bus_alloc_resources(dev, sc->spec_irq, sc->irq);
297ba26d470SStanislav Sedov 			if (error != 0) {
298ba26d470SStanislav Sedov 				device_printf(dev, "MSI allocation failed.\n");
299ba26d470SStanislav Sedov 				sc->spec_irq = NULL;
300ba26d470SStanislav Sedov 				pci_release_msi(dev);
301ba26d470SStanislav Sedov 			} else {
302ba26d470SStanislav Sedov 				sc->flags |= AE_FLAG_MSI;
303ba26d470SStanislav Sedov 			}
304ba26d470SStanislav Sedov 		}
305ba26d470SStanislav Sedov 	}
306ba26d470SStanislav Sedov 	if (sc->spec_irq == NULL) {
307ba26d470SStanislav Sedov 		sc->spec_irq = ae_res_spec_irq;
308ba26d470SStanislav Sedov 		error = bus_alloc_resources(dev, sc->spec_irq, sc->irq);
309ba26d470SStanislav Sedov 		if (error != 0) {
310ba26d470SStanislav Sedov 			device_printf(dev, "could not allocate IRQ resources.\n");
311ba26d470SStanislav Sedov 			sc->spec_irq = NULL;
312ba26d470SStanislav Sedov 			goto fail;
313ba26d470SStanislav Sedov 		}
314ba26d470SStanislav Sedov 	}
315ba26d470SStanislav Sedov 
316ba26d470SStanislav Sedov 	ae_init_tunables(sc);
317ba26d470SStanislav Sedov 
318ba26d470SStanislav Sedov 	ae_phy_reset(sc);		/* Reset PHY. */
319ba26d470SStanislav Sedov 	error = ae_reset(sc);		/* Reset the controller itself. */
320ba26d470SStanislav Sedov 	if (error != 0)
321ba26d470SStanislav Sedov 		goto fail;
322ba26d470SStanislav Sedov 
323ba26d470SStanislav Sedov 	ae_pcie_init(sc);
324ba26d470SStanislav Sedov 
325ba26d470SStanislav Sedov 	ae_retrieve_address(sc);	/* Load MAC address. */
326ba26d470SStanislav Sedov 
327ba26d470SStanislav Sedov 	error = ae_alloc_rings(sc);	/* Allocate ring buffers. */
328ba26d470SStanislav Sedov 	if (error != 0)
329ba26d470SStanislav Sedov 		goto fail;
330ba26d470SStanislav Sedov 
331ba26d470SStanislav Sedov 	ifp = sc->ifp = if_alloc(IFT_ETHER);
332ba26d470SStanislav Sedov 	if (ifp == NULL) {
333ba26d470SStanislav Sedov 		device_printf(dev, "could not allocate ifnet structure.\n");
334ba26d470SStanislav Sedov 		error = ENXIO;
3357c9fdc4dSGavin Atkinson 		goto fail;
336ba26d470SStanislav Sedov 	}
337ba26d470SStanislav Sedov 
338*3f14814fSJustin Hibbits 	if_setsoftc(ifp, sc);
339ba26d470SStanislav Sedov 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
340*3f14814fSJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
341*3f14814fSJustin Hibbits 	if_setioctlfn(ifp, ae_ioctl);
342*3f14814fSJustin Hibbits 	if_setstartfn(ifp, ae_start);
343*3f14814fSJustin Hibbits 	if_setinitfn(ifp, ae_init);
344*3f14814fSJustin Hibbits 	if_setcapabilities(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING);
345*3f14814fSJustin Hibbits 	if_sethwassist(ifp, 0);
346*3f14814fSJustin Hibbits 	if_setsendqlen(ifp, ifqmaxlen);
347*3f14814fSJustin Hibbits 	if_setsendqready(ifp);
3483b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_PMG, &pmc) == 0) {
349*3f14814fSJustin Hibbits 		if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC, 0);
350ba26d470SStanislav Sedov 		sc->flags |= AE_FLAG_PMG;
351ba26d470SStanislav Sedov 	}
352*3f14814fSJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
353ba26d470SStanislav Sedov 
354ba26d470SStanislav Sedov 	/*
355ba26d470SStanislav Sedov 	 * Configure and attach MII bus.
356ba26d470SStanislav Sedov 	 */
3578e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->miibus, ifp, ae_mediachange,
3588e5d93dbSMarius Strobl 	    ae_mediastatus, BMSR_DEFCAPMASK, AE_PHYADDR_DEFAULT,
3598e5d93dbSMarius Strobl 	    MII_OFFSET_ANY, 0);
360ba26d470SStanislav Sedov 	if (error != 0) {
3618e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
362ba26d470SStanislav Sedov 		goto fail;
363ba26d470SStanislav Sedov 	}
364ba26d470SStanislav Sedov 
365ba26d470SStanislav Sedov 	ether_ifattach(ifp, sc->eaddr);
366ba26d470SStanislav Sedov 	/* Tell the upper layer(s) we support long frames. */
367*3f14814fSJustin Hibbits 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
368ba26d470SStanislav Sedov 
369ba26d470SStanislav Sedov 	/*
370ba26d470SStanislav Sedov 	 * Create and run all helper tasks.
371ba26d470SStanislav Sedov 	 */
372ba26d470SStanislav Sedov 	sc->tq = taskqueue_create_fast("ae_taskq", M_WAITOK,
373ba26d470SStanislav Sedov             taskqueue_thread_enqueue, &sc->tq);
374ba26d470SStanislav Sedov 	if (sc->tq == NULL) {
375ba26d470SStanislav Sedov 		device_printf(dev, "could not create taskqueue.\n");
376ba26d470SStanislav Sedov 		ether_ifdetach(ifp);
377ba26d470SStanislav Sedov 		error = ENXIO;
378ba26d470SStanislav Sedov 		goto fail;
379ba26d470SStanislav Sedov 	}
380ba26d470SStanislav Sedov 	taskqueue_start_threads(&sc->tq, 1, PI_NET, "%s taskq",
381ba26d470SStanislav Sedov 	    device_get_nameunit(sc->dev));
382ba26d470SStanislav Sedov 
383ba26d470SStanislav Sedov 	/*
384ba26d470SStanislav Sedov 	 * Configure interrupt handlers.
385ba26d470SStanislav Sedov 	 */
386ba26d470SStanislav Sedov 	error = bus_setup_intr(dev, sc->irq[0], INTR_TYPE_NET | INTR_MPSAFE,
387ba26d470SStanislav Sedov 	    ae_intr, NULL, sc, &sc->intrhand);
388ba26d470SStanislav Sedov 	if (error != 0) {
389ba26d470SStanislav Sedov 		device_printf(dev, "could not set up interrupt handler.\n");
390ba26d470SStanislav Sedov 		taskqueue_free(sc->tq);
391ba26d470SStanislav Sedov 		sc->tq = NULL;
392ba26d470SStanislav Sedov 		ether_ifdetach(ifp);
393ba26d470SStanislav Sedov 		goto fail;
394ba26d470SStanislav Sedov 	}
395ba26d470SStanislav Sedov 
396ba26d470SStanislav Sedov fail:
397ba26d470SStanislav Sedov 	if (error != 0)
398ba26d470SStanislav Sedov 		ae_detach(dev);
399ba26d470SStanislav Sedov 
400ba26d470SStanislav Sedov 	return (error);
401ba26d470SStanislav Sedov }
402ba26d470SStanislav Sedov 
4036dc7dc9aSMatthew D Fleming #define	AE_SYSCTL(stx, parent, name, desc, ptr)	\
4046dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, parent, OID_AUTO, name, CTLFLAG_RD, ptr, 0, desc)
4056dc7dc9aSMatthew D Fleming 
406ba26d470SStanislav Sedov static void
407ba26d470SStanislav Sedov ae_init_tunables(ae_softc_t *sc)
408ba26d470SStanislav Sedov {
409ba26d470SStanislav Sedov 	struct sysctl_ctx_list *ctx;
410ba26d470SStanislav Sedov 	struct sysctl_oid *root, *stats, *stats_rx, *stats_tx;
411ba26d470SStanislav Sedov 	struct ae_stats *ae_stats;
412ba26d470SStanislav Sedov 
413ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
414ba26d470SStanislav Sedov 	ae_stats = &sc->stats;
415ba26d470SStanislav Sedov 
416ba26d470SStanislav Sedov 	ctx = device_get_sysctl_ctx(sc->dev);
417ba26d470SStanislav Sedov 	root = device_get_sysctl_tree(sc->dev);
418ba26d470SStanislav Sedov 	stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(root), OID_AUTO, "stats",
4197029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "ae statistics");
420ba26d470SStanislav Sedov 
421ba26d470SStanislav Sedov 	/*
422ba26d470SStanislav Sedov 	 * Receiver statistcics.
423ba26d470SStanislav Sedov 	 */
424ba26d470SStanislav Sedov 	stats_rx = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(stats), OID_AUTO, "rx",
4257029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
4266dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "bcast",
4276dc7dc9aSMatthew D Fleming 	    "broadcast frames", &ae_stats->rx_bcast);
4286dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "mcast",
4296dc7dc9aSMatthew D Fleming 	    "multicast frames", &ae_stats->rx_mcast);
4306dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "pause",
4316dc7dc9aSMatthew D Fleming 	    "PAUSE frames", &ae_stats->rx_pause);
4326dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "control",
4336dc7dc9aSMatthew D Fleming 	    "control frames", &ae_stats->rx_ctrl);
4346dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "crc_errors",
4356dc7dc9aSMatthew D Fleming 	    "frames with CRC errors", &ae_stats->rx_crcerr);
4366dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "code_errors",
4376dc7dc9aSMatthew D Fleming 	    "frames with invalid opcode", &ae_stats->rx_codeerr);
4386dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "runt",
4396dc7dc9aSMatthew D Fleming 	    "runt frames", &ae_stats->rx_runt);
4406dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "frag",
4416dc7dc9aSMatthew D Fleming 	    "fragmented frames", &ae_stats->rx_frag);
4426dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "align_errors",
4436dc7dc9aSMatthew D Fleming 	    "frames with alignment errors", &ae_stats->rx_align);
4446dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_rx), "truncated",
4456dc7dc9aSMatthew D Fleming 	    "frames truncated due to Rx FIFO inderrun", &ae_stats->rx_trunc);
446ba26d470SStanislav Sedov 
447ba26d470SStanislav Sedov 	/*
448ba26d470SStanislav Sedov 	 * Receiver statistcics.
449ba26d470SStanislav Sedov 	 */
450ba26d470SStanislav Sedov 	stats_tx = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(stats), OID_AUTO, "tx",
4517029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
4526dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "bcast",
4536dc7dc9aSMatthew D Fleming 	    "broadcast frames", &ae_stats->tx_bcast);
4546dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "mcast",
4556dc7dc9aSMatthew D Fleming 	    "multicast frames", &ae_stats->tx_mcast);
4566dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "pause",
4576dc7dc9aSMatthew D Fleming 	    "PAUSE frames", &ae_stats->tx_pause);
4586dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "control",
4596dc7dc9aSMatthew D Fleming 	    "control frames", &ae_stats->tx_ctrl);
4606dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "defers",
4616dc7dc9aSMatthew D Fleming 	    "deferrals occuried", &ae_stats->tx_defer);
4626dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "exc_defers",
4636dc7dc9aSMatthew D Fleming 	    "excessive deferrals occuried", &ae_stats->tx_excdefer);
4646dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "singlecols",
4656dc7dc9aSMatthew D Fleming 	    "single collisions occuried", &ae_stats->tx_singlecol);
4666dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "multicols",
4676dc7dc9aSMatthew D Fleming 	    "multiple collisions occuried", &ae_stats->tx_multicol);
4686dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "latecols",
4696dc7dc9aSMatthew D Fleming 	    "late collisions occuried", &ae_stats->tx_latecol);
4706dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "aborts",
4716dc7dc9aSMatthew D Fleming 	    "transmit aborts due collisions", &ae_stats->tx_abortcol);
4726dc7dc9aSMatthew D Fleming 	AE_SYSCTL(ctx, SYSCTL_CHILDREN(stats_tx), "underruns",
4736dc7dc9aSMatthew D Fleming 	    "Tx FIFO underruns", &ae_stats->tx_underrun);
474ba26d470SStanislav Sedov }
475ba26d470SStanislav Sedov 
476ba26d470SStanislav Sedov static void
477ba26d470SStanislav Sedov ae_pcie_init(ae_softc_t *sc)
478ba26d470SStanislav Sedov {
479ba26d470SStanislav Sedov 
480ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_PCIE_LTSSM_TESTMODE_REG, AE_PCIE_LTSSM_TESTMODE_DEFAULT);
481ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_PCIE_DLL_TX_CTRL_REG, AE_PCIE_DLL_TX_CTRL_DEFAULT);
482ba26d470SStanislav Sedov }
483ba26d470SStanislav Sedov 
484ba26d470SStanislav Sedov static void
485ba26d470SStanislav Sedov ae_phy_reset(ae_softc_t *sc)
486ba26d470SStanislav Sedov {
487ba26d470SStanislav Sedov 
488ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_PHY_ENABLE_REG, AE_PHY_ENABLE);
489ba26d470SStanislav Sedov 	DELAY(1000);	/* XXX: pause(9) ? */
490ba26d470SStanislav Sedov }
491ba26d470SStanislav Sedov 
492ba26d470SStanislav Sedov static int
493ba26d470SStanislav Sedov ae_reset(ae_softc_t *sc)
494ba26d470SStanislav Sedov {
495ba26d470SStanislav Sedov 	int i;
496ba26d470SStanislav Sedov 
497ba26d470SStanislav Sedov 	/*
498ba26d470SStanislav Sedov 	 * Issue a soft reset.
499ba26d470SStanislav Sedov 	 */
500ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MASTER_REG, AE_MASTER_SOFT_RESET);
501ba26d470SStanislav Sedov 	bus_barrier(sc->mem[0], AE_MASTER_REG, 4,
502ba26d470SStanislav Sedov 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
503ba26d470SStanislav Sedov 
504ba26d470SStanislav Sedov 	/*
505ba26d470SStanislav Sedov 	 * Wait for reset to complete.
506ba26d470SStanislav Sedov 	 */
507ba26d470SStanislav Sedov 	for (i = 0; i < AE_RESET_TIMEOUT; i++) {
508ba26d470SStanislav Sedov 		if ((AE_READ_4(sc, AE_MASTER_REG) & AE_MASTER_SOFT_RESET) == 0)
509ba26d470SStanislav Sedov 			break;
510ba26d470SStanislav Sedov 		DELAY(10);
511ba26d470SStanislav Sedov 	}
512ba26d470SStanislav Sedov 	if (i == AE_RESET_TIMEOUT) {
513ba26d470SStanislav Sedov 		device_printf(sc->dev, "reset timeout.\n");
514ba26d470SStanislav Sedov 		return (ENXIO);
515ba26d470SStanislav Sedov 	}
516ba26d470SStanislav Sedov 
517ba26d470SStanislav Sedov 	/*
518ba26d470SStanislav Sedov 	 * Wait for everything to enter idle state.
519ba26d470SStanislav Sedov 	 */
520ba26d470SStanislav Sedov 	for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
521ba26d470SStanislav Sedov 		if (AE_READ_4(sc, AE_IDLE_REG) == 0)
522ba26d470SStanislav Sedov 			break;
523ba26d470SStanislav Sedov 		DELAY(100);
524ba26d470SStanislav Sedov 	}
525ba26d470SStanislav Sedov 	if (i == AE_IDLE_TIMEOUT) {
526ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not enter idle state.\n");
527ba26d470SStanislav Sedov 		return (ENXIO);
528ba26d470SStanislav Sedov 	}
529ba26d470SStanislav Sedov 	return (0);
530ba26d470SStanislav Sedov }
531ba26d470SStanislav Sedov 
532ba26d470SStanislav Sedov static void
533ba26d470SStanislav Sedov ae_init(void *arg)
534ba26d470SStanislav Sedov {
535ba26d470SStanislav Sedov 	ae_softc_t *sc;
536ba26d470SStanislav Sedov 
537ba26d470SStanislav Sedov 	sc = (ae_softc_t *)arg;
538ba26d470SStanislav Sedov 	AE_LOCK(sc);
539ba26d470SStanislav Sedov 	ae_init_locked(sc);
540ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
541ba26d470SStanislav Sedov }
542ba26d470SStanislav Sedov 
543ba26d470SStanislav Sedov static void
544ba26d470SStanislav Sedov ae_phy_init(ae_softc_t *sc)
545ba26d470SStanislav Sedov {
546ba26d470SStanislav Sedov 
547ba26d470SStanislav Sedov 	/*
548ba26d470SStanislav Sedov 	 * Enable link status change interrupt.
549ba26d470SStanislav Sedov 	 * XXX magic numbers.
550ba26d470SStanislav Sedov 	 */
551ba26d470SStanislav Sedov #ifdef notyet
552ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, 18, 0xc00);
553ba26d470SStanislav Sedov #endif
554ba26d470SStanislav Sedov }
555ba26d470SStanislav Sedov 
556ba26d470SStanislav Sedov static int
557ba26d470SStanislav Sedov ae_init_locked(ae_softc_t *sc)
558ba26d470SStanislav Sedov {
559*3f14814fSJustin Hibbits 	if_t ifp;
560ba26d470SStanislav Sedov 	struct mii_data *mii;
561ba26d470SStanislav Sedov 	uint8_t eaddr[ETHER_ADDR_LEN];
562ba26d470SStanislav Sedov 	uint32_t val;
563ba26d470SStanislav Sedov 	bus_addr_t addr;
564ba26d470SStanislav Sedov 
565ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
566ba26d470SStanislav Sedov 
567ba26d470SStanislav Sedov 	ifp = sc->ifp;
568*3f14814fSJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
569b7dc7dd4SPyun YongHyeon 		return (0);
570ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
571ba26d470SStanislav Sedov 
572ba26d470SStanislav Sedov 	ae_stop(sc);
573ba26d470SStanislav Sedov 	ae_reset(sc);
574ba26d470SStanislav Sedov 	ae_pcie_init(sc);		/* Initialize PCIE stuff. */
575ba26d470SStanislav Sedov 	ae_phy_init(sc);
576ba26d470SStanislav Sedov 	ae_powersave_disable(sc);
577ba26d470SStanislav Sedov 
578ba26d470SStanislav Sedov 	/*
579ba26d470SStanislav Sedov 	 * Clear and disable interrupts.
580ba26d470SStanislav Sedov 	 */
581ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, 0xffffffff);
582ba26d470SStanislav Sedov 
583ba26d470SStanislav Sedov 	/*
584ba26d470SStanislav Sedov 	 * Set the MAC address.
585ba26d470SStanislav Sedov 	 */
586*3f14814fSJustin Hibbits 	bcopy(if_getlladdr(ifp), eaddr, ETHER_ADDR_LEN);
587ba26d470SStanislav Sedov 	val = eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5];
588ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_EADDR0_REG, val);
589ba26d470SStanislav Sedov 	val = eaddr[0] << 8 | eaddr[1];
590ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_EADDR1_REG, val);
591ba26d470SStanislav Sedov 
592fcb1bf41SPyun YongHyeon 	bzero(sc->rxd_base_dma, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING);
593b9a17c6cSPyun YongHyeon 	bzero(sc->txd_base, AE_TXD_BUFSIZE_DEFAULT);
594b9a17c6cSPyun YongHyeon 	bzero(sc->txs_base, AE_TXS_COUNT_DEFAULT * 4);
595ba26d470SStanislav Sedov 	/*
596ba26d470SStanislav Sedov 	 * Set ring buffers base addresses.
597ba26d470SStanislav Sedov 	 */
598ba26d470SStanislav Sedov 	addr = sc->dma_rxd_busaddr;
599ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_DESC_ADDR_HI_REG, BUS_ADDR_HI(addr));
600ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_RXD_ADDR_LO_REG, BUS_ADDR_LO(addr));
601ba26d470SStanislav Sedov 	addr = sc->dma_txd_busaddr;
602ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_TXD_ADDR_LO_REG, BUS_ADDR_LO(addr));
603ba26d470SStanislav Sedov 	addr = sc->dma_txs_busaddr;
604ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_TXS_ADDR_LO_REG, BUS_ADDR_LO(addr));
605ba26d470SStanislav Sedov 
606ba26d470SStanislav Sedov 	/*
607ba26d470SStanislav Sedov 	 * Configure ring buffers sizes.
608ba26d470SStanislav Sedov 	 */
609ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_RXD_COUNT_REG, AE_RXD_COUNT_DEFAULT);
610ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_TXD_BUFSIZE_REG, AE_TXD_BUFSIZE_DEFAULT / 4);
611ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_TXS_COUNT_REG, AE_TXS_COUNT_DEFAULT);
612ba26d470SStanislav Sedov 
613ba26d470SStanislav Sedov 	/*
614ba26d470SStanislav Sedov 	 * Configure interframe gap parameters.
615ba26d470SStanislav Sedov 	 */
616ba26d470SStanislav Sedov 	val = ((AE_IFG_TXIPG_DEFAULT << AE_IFG_TXIPG_SHIFT) &
617ba26d470SStanislav Sedov 	    AE_IFG_TXIPG_MASK) |
618ba26d470SStanislav Sedov 	    ((AE_IFG_RXIPG_DEFAULT << AE_IFG_RXIPG_SHIFT) &
619ba26d470SStanislav Sedov 	    AE_IFG_RXIPG_MASK) |
620ba26d470SStanislav Sedov 	    ((AE_IFG_IPGR1_DEFAULT << AE_IFG_IPGR1_SHIFT) &
621ba26d470SStanislav Sedov 	    AE_IFG_IPGR1_MASK) |
622ba26d470SStanislav Sedov 	    ((AE_IFG_IPGR2_DEFAULT << AE_IFG_IPGR2_SHIFT) &
623ba26d470SStanislav Sedov 	    AE_IFG_IPGR2_MASK);
624ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_IFG_REG, val);
625ba26d470SStanislav Sedov 
626ba26d470SStanislav Sedov 	/*
627ba26d470SStanislav Sedov 	 * Configure half-duplex operation.
628ba26d470SStanislav Sedov 	 */
629ba26d470SStanislav Sedov 	val = ((AE_HDPX_LCOL_DEFAULT << AE_HDPX_LCOL_SHIFT) &
630ba26d470SStanislav Sedov 	    AE_HDPX_LCOL_MASK) |
631ba26d470SStanislav Sedov 	    ((AE_HDPX_RETRY_DEFAULT << AE_HDPX_RETRY_SHIFT) &
632ba26d470SStanislav Sedov 	    AE_HDPX_RETRY_MASK) |
633ba26d470SStanislav Sedov 	    ((AE_HDPX_ABEBT_DEFAULT << AE_HDPX_ABEBT_SHIFT) &
634ba26d470SStanislav Sedov 	    AE_HDPX_ABEBT_MASK) |
635ba26d470SStanislav Sedov 	    ((AE_HDPX_JAMIPG_DEFAULT << AE_HDPX_JAMIPG_SHIFT) &
636ba26d470SStanislav Sedov 	    AE_HDPX_JAMIPG_MASK) | AE_HDPX_EXC_EN;
637ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_HDPX_REG, val);
638ba26d470SStanislav Sedov 
639ba26d470SStanislav Sedov 	/*
640ba26d470SStanislav Sedov 	 * Configure interrupt moderate timer.
641ba26d470SStanislav Sedov 	 */
642ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_IMT_REG, AE_IMT_DEFAULT);
643ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MASTER_REG);
644ba26d470SStanislav Sedov 	val |= AE_MASTER_IMT_EN;
645ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MASTER_REG, val);
646ba26d470SStanislav Sedov 
647ba26d470SStanislav Sedov 	/*
648ba26d470SStanislav Sedov 	 * Configure interrupt clearing timer.
649ba26d470SStanislav Sedov 	 */
650ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_ICT_REG, AE_ICT_DEFAULT);
651ba26d470SStanislav Sedov 
652ba26d470SStanislav Sedov 	/*
653ba26d470SStanislav Sedov 	 * Configure MTU.
654ba26d470SStanislav Sedov 	 */
655*3f14814fSJustin Hibbits 	val = if_getmtu(ifp) + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
656ba26d470SStanislav Sedov 	    ETHER_CRC_LEN;
657ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_MTU_REG, val);
658ba26d470SStanislav Sedov 
659ba26d470SStanislav Sedov 	/*
660ba26d470SStanislav Sedov 	 * Configure cut-through threshold.
661ba26d470SStanislav Sedov 	 */
662ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_CUT_THRESH_REG, AE_CUT_THRESH_DEFAULT);
663ba26d470SStanislav Sedov 
664ba26d470SStanislav Sedov 	/*
665ba26d470SStanislav Sedov 	 * Configure flow control.
666ba26d470SStanislav Sedov 	 */
667ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_FLOW_THRESH_HI_REG, (AE_RXD_COUNT_DEFAULT / 8) * 7);
668ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_FLOW_THRESH_LO_REG, (AE_RXD_COUNT_MIN / 8) >
669ba26d470SStanislav Sedov 	    (AE_RXD_COUNT_DEFAULT / 12) ? (AE_RXD_COUNT_MIN / 8) :
670ba26d470SStanislav Sedov 	    (AE_RXD_COUNT_DEFAULT / 12));
671ba26d470SStanislav Sedov 
672ba26d470SStanislav Sedov 	/*
673ba26d470SStanislav Sedov 	 * Init mailboxes.
674ba26d470SStanislav Sedov 	 */
675ba26d470SStanislav Sedov 	sc->txd_cur = sc->rxd_cur = 0;
676ba26d470SStanislav Sedov 	sc->txs_ack = sc->txd_ack = 0;
677ba26d470SStanislav Sedov 	sc->rxd_cur = 0;
678ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_MB_TXD_IDX_REG, sc->txd_cur);
679ba26d470SStanislav Sedov 	AE_WRITE_2(sc, AE_MB_RXD_IDX_REG, sc->rxd_cur);
680ba26d470SStanislav Sedov 
681ba26d470SStanislav Sedov 	sc->tx_inproc = 0;	/* Number of packets the chip processes now. */
682ba26d470SStanislav Sedov 	sc->flags |= AE_FLAG_TXAVAIL;	/* Free Tx's available. */
683ba26d470SStanislav Sedov 
684ba26d470SStanislav Sedov 	/*
685ba26d470SStanislav Sedov 	 * Enable DMA.
686ba26d470SStanislav Sedov 	 */
687ba26d470SStanislav Sedov 	AE_WRITE_1(sc, AE_DMAREAD_REG, AE_DMAREAD_EN);
688ba26d470SStanislav Sedov 	AE_WRITE_1(sc, AE_DMAWRITE_REG, AE_DMAWRITE_EN);
689ba26d470SStanislav Sedov 
690ba26d470SStanislav Sedov 	/*
691ba26d470SStanislav Sedov 	 * Check if everything is OK.
692ba26d470SStanislav Sedov 	 */
693ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_ISR_REG);
694ba26d470SStanislav Sedov 	if ((val & AE_ISR_PHY_LINKDOWN) != 0) {
695ba26d470SStanislav Sedov 		device_printf(sc->dev, "Initialization failed.\n");
696ba26d470SStanislav Sedov 		return (ENXIO);
697ba26d470SStanislav Sedov 	}
698ba26d470SStanislav Sedov 
699ba26d470SStanislav Sedov 	/*
700ba26d470SStanislav Sedov 	 * Clear interrupt status.
701ba26d470SStanislav Sedov 	 */
702ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, 0x3fffffff);
703ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, 0x0);
704ba26d470SStanislav Sedov 
705ba26d470SStanislav Sedov 	/*
706ba26d470SStanislav Sedov 	 * Enable interrupts.
707ba26d470SStanislav Sedov 	 */
708ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MASTER_REG);
709ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MASTER_REG, val | AE_MASTER_MANUAL_INT);
710ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_IMR_REG, AE_IMR_DEFAULT);
711ba26d470SStanislav Sedov 
712ba26d470SStanislav Sedov 	/*
713ba26d470SStanislav Sedov 	 * Disable WOL.
714ba26d470SStanislav Sedov 	 */
715ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_WOL_REG, 0);
716ba26d470SStanislav Sedov 
717ba26d470SStanislav Sedov 	/*
718ba26d470SStanislav Sedov 	 * Configure MAC.
719ba26d470SStanislav Sedov 	 */
720ba26d470SStanislav Sedov 	val = AE_MAC_TX_CRC_EN | AE_MAC_TX_AUTOPAD |
721ba26d470SStanislav Sedov 	    AE_MAC_FULL_DUPLEX | AE_MAC_CLK_PHY |
722ba26d470SStanislav Sedov 	    AE_MAC_TX_FLOW_EN | AE_MAC_RX_FLOW_EN |
723ba26d470SStanislav Sedov 	    ((AE_HALFBUF_DEFAULT << AE_HALFBUF_SHIFT) & AE_HALFBUF_MASK) |
724ba26d470SStanislav Sedov 	    ((AE_MAC_PREAMBLE_DEFAULT << AE_MAC_PREAMBLE_SHIFT) &
725ba26d470SStanislav Sedov 	    AE_MAC_PREAMBLE_MASK);
726ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MAC_REG, val);
727ba26d470SStanislav Sedov 
728ba26d470SStanislav Sedov 	/*
729ba26d470SStanislav Sedov 	 * Configure Rx MAC.
730ba26d470SStanislav Sedov 	 */
731ba26d470SStanislav Sedov 	ae_rxfilter(sc);
732ba26d470SStanislav Sedov 	ae_rxvlan(sc);
733ba26d470SStanislav Sedov 
734ba26d470SStanislav Sedov 	/*
735ba26d470SStanislav Sedov 	 * Enable Tx/Rx.
736ba26d470SStanislav Sedov 	 */
737ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MAC_REG);
738ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MAC_REG, val | AE_MAC_TX_EN | AE_MAC_RX_EN);
739ba26d470SStanislav Sedov 
740ba26d470SStanislav Sedov 	sc->flags &= ~AE_FLAG_LINK;
741ba26d470SStanislav Sedov 	mii_mediachg(mii);	/* Switch to the current media. */
742ba26d470SStanislav Sedov 
743ba26d470SStanislav Sedov 	callout_reset(&sc->tick_ch, hz, ae_tick, sc);
744ba26d470SStanislav Sedov 
745*3f14814fSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
746*3f14814fSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
747ba26d470SStanislav Sedov 
748ba26d470SStanislav Sedov #ifdef AE_DEBUG
749ba26d470SStanislav Sedov 	device_printf(sc->dev, "Initialization complete.\n");
750ba26d470SStanislav Sedov #endif
751ba26d470SStanislav Sedov 
752ba26d470SStanislav Sedov 	return (0);
753ba26d470SStanislav Sedov }
754ba26d470SStanislav Sedov 
755850f048eSWarner Losh static int
756ba26d470SStanislav Sedov ae_detach(device_t dev)
757ba26d470SStanislav Sedov {
758ba26d470SStanislav Sedov 	struct ae_softc *sc;
759*3f14814fSJustin Hibbits 	if_t ifp;
760ba26d470SStanislav Sedov 
761ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
762ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae: %d]: sc is NULL", __LINE__));
763ba26d470SStanislav Sedov 	ifp = sc->ifp;
764ba26d470SStanislav Sedov 	if (device_is_attached(dev)) {
765ba26d470SStanislav Sedov 		AE_LOCK(sc);
766ba26d470SStanislav Sedov 		sc->flags |= AE_FLAG_DETACH;
767ba26d470SStanislav Sedov 		ae_stop(sc);
768ba26d470SStanislav Sedov 		AE_UNLOCK(sc);
769ba26d470SStanislav Sedov 		callout_drain(&sc->tick_ch);
770ba26d470SStanislav Sedov 		taskqueue_drain(sc->tq, &sc->int_task);
771ba26d470SStanislav Sedov 		taskqueue_drain(taskqueue_swi, &sc->link_task);
772ba26d470SStanislav Sedov 		ether_ifdetach(ifp);
773ba26d470SStanislav Sedov 	}
774ba26d470SStanislav Sedov 	if (sc->tq != NULL) {
775ba26d470SStanislav Sedov 		taskqueue_drain(sc->tq, &sc->int_task);
776ba26d470SStanislav Sedov 		taskqueue_free(sc->tq);
777ba26d470SStanislav Sedov 		sc->tq = NULL;
778ba26d470SStanislav Sedov 	}
779ba26d470SStanislav Sedov 	if (sc->miibus != NULL) {
780ba26d470SStanislav Sedov 		device_delete_child(dev, sc->miibus);
781ba26d470SStanislav Sedov 		sc->miibus = NULL;
782ba26d470SStanislav Sedov 	}
783ba26d470SStanislav Sedov 	bus_generic_detach(sc->dev);
784ba26d470SStanislav Sedov 	ae_dma_free(sc);
785ba26d470SStanislav Sedov 	if (sc->intrhand != NULL) {
786ba26d470SStanislav Sedov 		bus_teardown_intr(dev, sc->irq[0], sc->intrhand);
787ba26d470SStanislav Sedov 		sc->intrhand = NULL;
788ba26d470SStanislav Sedov 	}
789ba26d470SStanislav Sedov 	if (ifp != NULL) {
790ba26d470SStanislav Sedov 		if_free(ifp);
791ba26d470SStanislav Sedov 		sc->ifp = NULL;
792ba26d470SStanislav Sedov 	}
793ba26d470SStanislav Sedov 	if (sc->spec_irq != NULL)
794ba26d470SStanislav Sedov 		bus_release_resources(dev, sc->spec_irq, sc->irq);
795ba26d470SStanislav Sedov 	if (sc->spec_mem != NULL)
796ba26d470SStanislav Sedov 		bus_release_resources(dev, sc->spec_mem, sc->mem);
797ba26d470SStanislav Sedov 	if ((sc->flags & AE_FLAG_MSI) != 0)
798ba26d470SStanislav Sedov 		pci_release_msi(dev);
799ba26d470SStanislav Sedov 	mtx_destroy(&sc->mtx);
800ba26d470SStanislav Sedov 
801ba26d470SStanislav Sedov 	return (0);
802ba26d470SStanislav Sedov }
803ba26d470SStanislav Sedov 
804ba26d470SStanislav Sedov static int
805ba26d470SStanislav Sedov ae_miibus_readreg(device_t dev, int phy, int reg)
806ba26d470SStanislav Sedov {
807ba26d470SStanislav Sedov 	ae_softc_t *sc;
808ba26d470SStanislav Sedov 	uint32_t val;
809ba26d470SStanislav Sedov 	int i;
810ba26d470SStanislav Sedov 
811ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
812ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
813ba26d470SStanislav Sedov 
814ba26d470SStanislav Sedov 	/*
815ba26d470SStanislav Sedov 	 * Locking is done in upper layers.
816ba26d470SStanislav Sedov 	 */
817ba26d470SStanislav Sedov 
818ba26d470SStanislav Sedov 	val = ((reg << AE_MDIO_REGADDR_SHIFT) & AE_MDIO_REGADDR_MASK) |
819ba26d470SStanislav Sedov 	    AE_MDIO_START | AE_MDIO_READ | AE_MDIO_SUP_PREAMBLE |
820ba26d470SStanislav Sedov 	    ((AE_MDIO_CLK_25_4 << AE_MDIO_CLK_SHIFT) & AE_MDIO_CLK_MASK);
821ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MDIO_REG, val);
822ba26d470SStanislav Sedov 
823ba26d470SStanislav Sedov 	/*
824ba26d470SStanislav Sedov 	 * Wait for operation to complete.
825ba26d470SStanislav Sedov 	 */
826ba26d470SStanislav Sedov 	for (i = 0; i < AE_MDIO_TIMEOUT; i++) {
827ba26d470SStanislav Sedov 		DELAY(2);
828ba26d470SStanislav Sedov 		val = AE_READ_4(sc, AE_MDIO_REG);
829ba26d470SStanislav Sedov 		if ((val & (AE_MDIO_START | AE_MDIO_BUSY)) == 0)
830ba26d470SStanislav Sedov 			break;
831ba26d470SStanislav Sedov 	}
832ba26d470SStanislav Sedov 	if (i == AE_MDIO_TIMEOUT) {
833ba26d470SStanislav Sedov 		device_printf(sc->dev, "phy read timeout: %d.\n", reg);
834ba26d470SStanislav Sedov 		return (0);
835ba26d470SStanislav Sedov 	}
836ba26d470SStanislav Sedov 	return ((val << AE_MDIO_DATA_SHIFT) & AE_MDIO_DATA_MASK);
837ba26d470SStanislav Sedov }
838ba26d470SStanislav Sedov 
839ba26d470SStanislav Sedov static int
840ba26d470SStanislav Sedov ae_miibus_writereg(device_t dev, int phy, int reg, int val)
841ba26d470SStanislav Sedov {
842ba26d470SStanislav Sedov 	ae_softc_t *sc;
843ba26d470SStanislav Sedov 	uint32_t aereg;
844ba26d470SStanislav Sedov 	int i;
845ba26d470SStanislav Sedov 
846ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
847ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
848ba26d470SStanislav Sedov 
849ba26d470SStanislav Sedov 	/*
850ba26d470SStanislav Sedov 	 * Locking is done in upper layers.
851ba26d470SStanislav Sedov 	 */
852ba26d470SStanislav Sedov 
853ba26d470SStanislav Sedov 	aereg = ((reg << AE_MDIO_REGADDR_SHIFT) & AE_MDIO_REGADDR_MASK) |
854ba26d470SStanislav Sedov 	    AE_MDIO_START | AE_MDIO_SUP_PREAMBLE |
855ba26d470SStanislav Sedov 	    ((AE_MDIO_CLK_25_4 << AE_MDIO_CLK_SHIFT) & AE_MDIO_CLK_MASK) |
856ba26d470SStanislav Sedov 	    ((val << AE_MDIO_DATA_SHIFT) & AE_MDIO_DATA_MASK);
857ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MDIO_REG, aereg);
858ba26d470SStanislav Sedov 
859ba26d470SStanislav Sedov 	/*
860ba26d470SStanislav Sedov 	 * Wait for operation to complete.
861ba26d470SStanislav Sedov 	 */
862ba26d470SStanislav Sedov 	for (i = 0; i < AE_MDIO_TIMEOUT; i++) {
863ba26d470SStanislav Sedov 		DELAY(2);
864ba26d470SStanislav Sedov 		aereg = AE_READ_4(sc, AE_MDIO_REG);
865ba26d470SStanislav Sedov 		if ((aereg & (AE_MDIO_START | AE_MDIO_BUSY)) == 0)
866ba26d470SStanislav Sedov 			break;
867ba26d470SStanislav Sedov 	}
868ba26d470SStanislav Sedov 	if (i == AE_MDIO_TIMEOUT) {
869ba26d470SStanislav Sedov 		device_printf(sc->dev, "phy write timeout: %d.\n", reg);
870ba26d470SStanislav Sedov 	}
871ba26d470SStanislav Sedov 	return (0);
872ba26d470SStanislav Sedov }
873ba26d470SStanislav Sedov 
874ba26d470SStanislav Sedov static void
875ba26d470SStanislav Sedov ae_miibus_statchg(device_t dev)
876ba26d470SStanislav Sedov {
877ba26d470SStanislav Sedov 	ae_softc_t *sc;
878ba26d470SStanislav Sedov 
879ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
880ba26d470SStanislav Sedov 	taskqueue_enqueue(taskqueue_swi, &sc->link_task);
881ba26d470SStanislav Sedov }
882ba26d470SStanislav Sedov 
883ba26d470SStanislav Sedov static void
884*3f14814fSJustin Hibbits ae_mediastatus(if_t ifp, struct ifmediareq *ifmr)
885ba26d470SStanislav Sedov {
886ba26d470SStanislav Sedov 	ae_softc_t *sc;
887ba26d470SStanislav Sedov 	struct mii_data *mii;
888ba26d470SStanislav Sedov 
889*3f14814fSJustin Hibbits 	sc = if_getsoftc(ifp);
890ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
891ba26d470SStanislav Sedov 
892ba26d470SStanislav Sedov 	AE_LOCK(sc);
893ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
894ba26d470SStanislav Sedov 	mii_pollstat(mii);
895ba26d470SStanislav Sedov 	ifmr->ifm_status = mii->mii_media_status;
896ba26d470SStanislav Sedov 	ifmr->ifm_active = mii->mii_media_active;
897ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
898ba26d470SStanislav Sedov }
899ba26d470SStanislav Sedov 
900ba26d470SStanislav Sedov static int
901*3f14814fSJustin Hibbits ae_mediachange(if_t ifp)
902ba26d470SStanislav Sedov {
903ba26d470SStanislav Sedov 	ae_softc_t *sc;
904ba26d470SStanislav Sedov 	struct mii_data *mii;
905ba26d470SStanislav Sedov 	struct mii_softc *mii_sc;
906ba26d470SStanislav Sedov 	int error;
907ba26d470SStanislav Sedov 
908ba26d470SStanislav Sedov 	/* XXX: check IFF_UP ?? */
909*3f14814fSJustin Hibbits 	sc = if_getsoftc(ifp);
910ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
911ba26d470SStanislav Sedov 	AE_LOCK(sc);
912ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
913ba26d470SStanislav Sedov 	LIST_FOREACH(mii_sc, &mii->mii_phys, mii_list)
9143fcb7a53SMarius Strobl 		PHY_RESET(mii_sc);
915ba26d470SStanislav Sedov 	error = mii_mediachg(mii);
916ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
917ba26d470SStanislav Sedov 
918ba26d470SStanislav Sedov 	return (error);
919ba26d470SStanislav Sedov }
920ba26d470SStanislav Sedov 
921ba26d470SStanislav Sedov static int
922ba26d470SStanislav Sedov ae_check_eeprom_present(ae_softc_t *sc, int *vpdc)
923ba26d470SStanislav Sedov {
924ba26d470SStanislav Sedov 	int error;
925ba26d470SStanislav Sedov 	uint32_t val;
926ba26d470SStanislav Sedov 
927ba26d470SStanislav Sedov 	KASSERT(vpdc != NULL, ("[ae, %d]: vpdc is NULL!\n", __LINE__));
928ba26d470SStanislav Sedov 
929ba26d470SStanislav Sedov 	/*
930ba26d470SStanislav Sedov 	 * Not sure why, but Linux does this.
931ba26d470SStanislav Sedov 	 */
932ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_SPICTL_REG);
933ba26d470SStanislav Sedov 	if ((val & AE_SPICTL_VPD_EN) != 0) {
934ba26d470SStanislav Sedov 		val &= ~AE_SPICTL_VPD_EN;
935ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_SPICTL_REG, val);
936ba26d470SStanislav Sedov 	}
9373b0a4aefSJohn Baldwin 	error = pci_find_cap(sc->dev, PCIY_VPD, vpdc);
938ba26d470SStanislav Sedov 	return (error);
939ba26d470SStanislav Sedov }
940ba26d470SStanislav Sedov 
941ba26d470SStanislav Sedov static int
942ba26d470SStanislav Sedov ae_vpd_read_word(ae_softc_t *sc, int reg, uint32_t *word)
943ba26d470SStanislav Sedov {
944ba26d470SStanislav Sedov 	uint32_t val;
945ba26d470SStanislav Sedov 	int i;
946ba26d470SStanislav Sedov 
947ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_VPD_DATA_REG, 0);	/* Clear register value. */
948ba26d470SStanislav Sedov 
949ba26d470SStanislav Sedov 	/*
950ba26d470SStanislav Sedov 	 * VPD registers start at offset 0x100. Read them.
951ba26d470SStanislav Sedov 	 */
952ba26d470SStanislav Sedov 	val = 0x100 + reg * 4;
953ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_VPD_CAP_REG, (val << AE_VPD_CAP_ADDR_SHIFT) &
954ba26d470SStanislav Sedov 	    AE_VPD_CAP_ADDR_MASK);
955ba26d470SStanislav Sedov 	for (i = 0; i < AE_VPD_TIMEOUT; i++) {
956ba26d470SStanislav Sedov 		DELAY(2000);
957ba26d470SStanislav Sedov 		val = AE_READ_4(sc, AE_VPD_CAP_REG);
958ba26d470SStanislav Sedov 		if ((val & AE_VPD_CAP_DONE) != 0)
959ba26d470SStanislav Sedov 			break;
960ba26d470SStanislav Sedov 	}
961ba26d470SStanislav Sedov 	if (i == AE_VPD_TIMEOUT) {
962ba26d470SStanislav Sedov 		device_printf(sc->dev, "timeout reading VPD register %d.\n",
963ba26d470SStanislav Sedov 		    reg);
964ba26d470SStanislav Sedov 		return (ETIMEDOUT);
965ba26d470SStanislav Sedov 	}
966ba26d470SStanislav Sedov 	*word = AE_READ_4(sc, AE_VPD_DATA_REG);
967ba26d470SStanislav Sedov 	return (0);
968ba26d470SStanislav Sedov }
969ba26d470SStanislav Sedov 
970ba26d470SStanislav Sedov static int
971ba26d470SStanislav Sedov ae_get_vpd_eaddr(ae_softc_t *sc, uint32_t *eaddr)
972ba26d470SStanislav Sedov {
973ba26d470SStanislav Sedov 	uint32_t word, reg, val;
974ba26d470SStanislav Sedov 	int error;
975ba26d470SStanislav Sedov 	int found;
976ba26d470SStanislav Sedov 	int vpdc;
977ba26d470SStanislav Sedov 	int i;
978ba26d470SStanislav Sedov 
979ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
980ba26d470SStanislav Sedov 	KASSERT(eaddr != NULL, ("[ae, %d]: eaddr is NULL", __LINE__));
981ba26d470SStanislav Sedov 
982ba26d470SStanislav Sedov 	/*
983ba26d470SStanislav Sedov 	 * Check for EEPROM.
984ba26d470SStanislav Sedov 	 */
985ba26d470SStanislav Sedov 	error = ae_check_eeprom_present(sc, &vpdc);
986ba26d470SStanislav Sedov 	if (error != 0)
987ba26d470SStanislav Sedov 		return (error);
988ba26d470SStanislav Sedov 
989ba26d470SStanislav Sedov 	/*
990ba26d470SStanislav Sedov 	 * Read the VPD configuration space.
991ba26d470SStanislav Sedov 	 * Each register is prefixed with signature,
992ba26d470SStanislav Sedov 	 * so we can check if it is valid.
993ba26d470SStanislav Sedov 	 */
994ba26d470SStanislav Sedov 	for (i = 0, found = 0; i < AE_VPD_NREGS; i++) {
995ba26d470SStanislav Sedov 		error = ae_vpd_read_word(sc, i, &word);
996ba26d470SStanislav Sedov 		if (error != 0)
997ba26d470SStanislav Sedov 			break;
998ba26d470SStanislav Sedov 
999ba26d470SStanislav Sedov 		/*
1000ba26d470SStanislav Sedov 		 * Check signature.
1001ba26d470SStanislav Sedov 		 */
1002ba26d470SStanislav Sedov 		if ((word & AE_VPD_SIG_MASK) != AE_VPD_SIG)
1003ba26d470SStanislav Sedov 			break;
1004ba26d470SStanislav Sedov 		reg = word >> AE_VPD_REG_SHIFT;
1005ba26d470SStanislav Sedov 		i++;	/* Move to the next word. */
1006ba26d470SStanislav Sedov 
1007ba26d470SStanislav Sedov 		if (reg != AE_EADDR0_REG && reg != AE_EADDR1_REG)
1008ba26d470SStanislav Sedov 			continue;
1009ba26d470SStanislav Sedov 
1010ba26d470SStanislav Sedov 		error = ae_vpd_read_word(sc, i, &val);
1011ba26d470SStanislav Sedov 		if (error != 0)
1012ba26d470SStanislav Sedov 			break;
1013ba26d470SStanislav Sedov 		if (reg == AE_EADDR0_REG)
1014ba26d470SStanislav Sedov 			eaddr[0] = val;
1015ba26d470SStanislav Sedov 		else
1016ba26d470SStanislav Sedov 			eaddr[1] = val;
1017ba26d470SStanislav Sedov 		found++;
1018ba26d470SStanislav Sedov 	}
1019ba26d470SStanislav Sedov 
1020ba26d470SStanislav Sedov 	if (found < 2)
1021ba26d470SStanislav Sedov 		return (ENOENT);
1022ba26d470SStanislav Sedov 
1023ba26d470SStanislav Sedov 	eaddr[1] &= 0xffff;	/* Only last 2 bytes are used. */
1024ba26d470SStanislav Sedov 	if (AE_CHECK_EADDR_VALID(eaddr) != 0) {
1025ba26d470SStanislav Sedov 		if (bootverbose)
1026ba26d470SStanislav Sedov 			device_printf(sc->dev,
1027ba26d470SStanislav Sedov 			    "VPD ethernet address registers are invalid.\n");
1028ba26d470SStanislav Sedov 		return (EINVAL);
1029ba26d470SStanislav Sedov 	}
1030ba26d470SStanislav Sedov 	return (0);
1031ba26d470SStanislav Sedov }
1032ba26d470SStanislav Sedov 
1033ba26d470SStanislav Sedov static int
1034ba26d470SStanislav Sedov ae_get_reg_eaddr(ae_softc_t *sc, uint32_t *eaddr)
1035ba26d470SStanislav Sedov {
1036ba26d470SStanislav Sedov 
1037ba26d470SStanislav Sedov 	/*
1038ba26d470SStanislav Sedov 	 * BIOS is supposed to set this.
1039ba26d470SStanislav Sedov 	 */
1040ba26d470SStanislav Sedov 	eaddr[0] = AE_READ_4(sc, AE_EADDR0_REG);
1041ba26d470SStanislav Sedov 	eaddr[1] = AE_READ_4(sc, AE_EADDR1_REG);
1042ba26d470SStanislav Sedov 	eaddr[1] &= 0xffff;	/* Only last 2 bytes are used. */
1043ba26d470SStanislav Sedov 
1044ba26d470SStanislav Sedov 	if (AE_CHECK_EADDR_VALID(eaddr) != 0) {
1045ba26d470SStanislav Sedov 		if (bootverbose)
1046ba26d470SStanislav Sedov 			device_printf(sc->dev,
1047f0b108cfSGavin Atkinson 			    "Ethernet address registers are invalid.\n");
1048ba26d470SStanislav Sedov 		return (EINVAL);
1049ba26d470SStanislav Sedov 	}
1050ba26d470SStanislav Sedov 	return (0);
1051ba26d470SStanislav Sedov }
1052ba26d470SStanislav Sedov 
1053ba26d470SStanislav Sedov static void
1054ba26d470SStanislav Sedov ae_retrieve_address(ae_softc_t *sc)
1055ba26d470SStanislav Sedov {
1056ba26d470SStanislav Sedov 	uint32_t eaddr[2] = {0, 0};
1057ba26d470SStanislav Sedov 	int error;
1058ba26d470SStanislav Sedov 
1059ba26d470SStanislav Sedov 	/*
1060ba26d470SStanislav Sedov 	 *Check for EEPROM.
1061ba26d470SStanislav Sedov 	 */
1062ba26d470SStanislav Sedov 	error = ae_get_vpd_eaddr(sc, eaddr);
1063ba26d470SStanislav Sedov 	if (error != 0)
1064ba26d470SStanislav Sedov 		error = ae_get_reg_eaddr(sc, eaddr);
1065ba26d470SStanislav Sedov 	if (error != 0) {
1066ba26d470SStanislav Sedov 		if (bootverbose)
1067ba26d470SStanislav Sedov 			device_printf(sc->dev,
1068ba26d470SStanislav Sedov 			    "Generating random ethernet address.\n");
1069ba26d470SStanislav Sedov 		eaddr[0] = arc4random();
1070ba26d470SStanislav Sedov 
1071ba26d470SStanislav Sedov 		/*
1072ba26d470SStanislav Sedov 		 * Set OUI to ASUSTek COMPUTER INC.
1073ba26d470SStanislav Sedov 		 */
1074ba26d470SStanislav Sedov 		sc->eaddr[0] = 0x02;	/* U/L bit set. */
1075ba26d470SStanislav Sedov 		sc->eaddr[1] = 0x1f;
1076ba26d470SStanislav Sedov 		sc->eaddr[2] = 0xc6;
1077ba26d470SStanislav Sedov 		sc->eaddr[3] = (eaddr[0] >> 16) & 0xff;
1078ba26d470SStanislav Sedov 		sc->eaddr[4] = (eaddr[0] >> 8) & 0xff;
1079ba26d470SStanislav Sedov 		sc->eaddr[5] = (eaddr[0] >> 0) & 0xff;
1080ba26d470SStanislav Sedov 	} else {
1081ba26d470SStanislav Sedov 		sc->eaddr[0] = (eaddr[1] >> 8) & 0xff;
1082ba26d470SStanislav Sedov 		sc->eaddr[1] = (eaddr[1] >> 0) & 0xff;
1083ba26d470SStanislav Sedov 		sc->eaddr[2] = (eaddr[0] >> 24) & 0xff;
1084ba26d470SStanislav Sedov 		sc->eaddr[3] = (eaddr[0] >> 16) & 0xff;
1085ba26d470SStanislav Sedov 		sc->eaddr[4] = (eaddr[0] >> 8) & 0xff;
1086ba26d470SStanislav Sedov 		sc->eaddr[5] = (eaddr[0] >> 0) & 0xff;
1087ba26d470SStanislav Sedov 	}
1088ba26d470SStanislav Sedov }
1089ba26d470SStanislav Sedov 
1090ba26d470SStanislav Sedov static void
1091ba26d470SStanislav Sedov ae_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1092ba26d470SStanislav Sedov {
1093ba26d470SStanislav Sedov 	bus_addr_t *addr = arg;
1094ba26d470SStanislav Sedov 
1095ba26d470SStanislav Sedov 	if (error != 0)
1096ba26d470SStanislav Sedov 		return;
1097ba26d470SStanislav Sedov 	KASSERT(nsegs == 1, ("[ae, %d]: %d segments instead of 1!", __LINE__,
1098ba26d470SStanislav Sedov 	    nsegs));
1099ba26d470SStanislav Sedov 	*addr = segs[0].ds_addr;
1100ba26d470SStanislav Sedov }
1101ba26d470SStanislav Sedov 
1102ba26d470SStanislav Sedov static int
1103ba26d470SStanislav Sedov ae_alloc_rings(ae_softc_t *sc)
1104ba26d470SStanislav Sedov {
1105ba26d470SStanislav Sedov 	bus_addr_t busaddr;
1106ba26d470SStanislav Sedov 	int error;
1107ba26d470SStanislav Sedov 
1108ba26d470SStanislav Sedov 	/*
1109ba26d470SStanislav Sedov 	 * Create parent DMA tag.
1110ba26d470SStanislav Sedov 	 */
1111ba26d470SStanislav Sedov 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
1112ba26d470SStanislav Sedov 	    1, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1113ba26d470SStanislav Sedov 	    NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
1114ba26d470SStanislav Sedov 	    BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
1115ba26d470SStanislav Sedov 	    &sc->dma_parent_tag);
1116ba26d470SStanislav Sedov 	if (error != 0) {
1117ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not creare parent DMA tag.\n");
1118ba26d470SStanislav Sedov 		return (error);
1119ba26d470SStanislav Sedov 	}
1120ba26d470SStanislav Sedov 
1121ba26d470SStanislav Sedov 	/*
1122ba26d470SStanislav Sedov 	 * Create DMA tag for TxD.
1123ba26d470SStanislav Sedov 	 */
1124ba26d470SStanislav Sedov 	error = bus_dma_tag_create(sc->dma_parent_tag,
1125b9a17c6cSPyun YongHyeon 	    8, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1126ba26d470SStanislav Sedov 	    NULL, NULL, AE_TXD_BUFSIZE_DEFAULT, 1,
1127ba26d470SStanislav Sedov 	    AE_TXD_BUFSIZE_DEFAULT, 0, NULL, NULL,
1128ba26d470SStanislav Sedov 	    &sc->dma_txd_tag);
1129ba26d470SStanislav Sedov 	if (error != 0) {
1130ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not creare TxD DMA tag.\n");
1131ba26d470SStanislav Sedov 		return (error);
1132ba26d470SStanislav Sedov 	}
1133ba26d470SStanislav Sedov 
1134ba26d470SStanislav Sedov 	/*
1135ba26d470SStanislav Sedov 	 * Create DMA tag for TxS.
1136ba26d470SStanislav Sedov 	 */
1137ba26d470SStanislav Sedov 	error = bus_dma_tag_create(sc->dma_parent_tag,
1138b9a17c6cSPyun YongHyeon 	    8, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1139ba26d470SStanislav Sedov 	    NULL, NULL, AE_TXS_COUNT_DEFAULT * 4, 1,
1140ba26d470SStanislav Sedov 	    AE_TXS_COUNT_DEFAULT * 4, 0, NULL, NULL,
1141ba26d470SStanislav Sedov 	    &sc->dma_txs_tag);
1142ba26d470SStanislav Sedov 	if (error != 0) {
1143ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not creare TxS DMA tag.\n");
1144ba26d470SStanislav Sedov 		return (error);
1145ba26d470SStanislav Sedov 	}
1146ba26d470SStanislav Sedov 
1147ba26d470SStanislav Sedov 	/*
1148ba26d470SStanislav Sedov 	 * Create DMA tag for RxD.
1149ba26d470SStanislav Sedov 	 */
1150ba26d470SStanislav Sedov 	error = bus_dma_tag_create(sc->dma_parent_tag,
1151ba26d470SStanislav Sedov 	    128, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1152fcb1bf41SPyun YongHyeon 	    NULL, NULL, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING, 1,
1153fcb1bf41SPyun YongHyeon 	    AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING, 0, NULL, NULL,
1154ba26d470SStanislav Sedov 	    &sc->dma_rxd_tag);
1155ba26d470SStanislav Sedov 	if (error != 0) {
1156ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not creare TxS DMA tag.\n");
1157ba26d470SStanislav Sedov 		return (error);
1158ba26d470SStanislav Sedov 	}
1159ba26d470SStanislav Sedov 
1160ba26d470SStanislav Sedov 	/*
1161ba26d470SStanislav Sedov 	 * Allocate TxD DMA memory.
1162ba26d470SStanislav Sedov 	 */
1163ba26d470SStanislav Sedov 	error = bus_dmamem_alloc(sc->dma_txd_tag, (void **)&sc->txd_base,
1164ba26d470SStanislav Sedov 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1165ba26d470SStanislav Sedov 	    &sc->dma_txd_map);
1166ba26d470SStanislav Sedov 	if (error != 0) {
1167ba26d470SStanislav Sedov 		device_printf(sc->dev,
1168ba26d470SStanislav Sedov 		    "could not allocate DMA memory for TxD ring.\n");
1169ba26d470SStanislav Sedov 		return (error);
1170ba26d470SStanislav Sedov 	}
1171ba26d470SStanislav Sedov 	error = bus_dmamap_load(sc->dma_txd_tag, sc->dma_txd_map, sc->txd_base,
1172ba26d470SStanislav Sedov 	    AE_TXD_BUFSIZE_DEFAULT, ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1173ba26d470SStanislav Sedov 	if (error != 0 || busaddr == 0) {
1174ba26d470SStanislav Sedov 		device_printf(sc->dev,
1175ba26d470SStanislav Sedov 		    "could not load DMA map for TxD ring.\n");
1176ba26d470SStanislav Sedov 		return (error);
1177ba26d470SStanislav Sedov 	}
1178ba26d470SStanislav Sedov 	sc->dma_txd_busaddr = busaddr;
1179ba26d470SStanislav Sedov 
1180ba26d470SStanislav Sedov 	/*
1181ba26d470SStanislav Sedov 	 * Allocate TxS DMA memory.
1182ba26d470SStanislav Sedov 	 */
1183ba26d470SStanislav Sedov 	error = bus_dmamem_alloc(sc->dma_txs_tag, (void **)&sc->txs_base,
1184ba26d470SStanislav Sedov 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1185ba26d470SStanislav Sedov 	    &sc->dma_txs_map);
1186ba26d470SStanislav Sedov 	if (error != 0) {
1187ba26d470SStanislav Sedov 		device_printf(sc->dev,
1188ba26d470SStanislav Sedov 		    "could not allocate DMA memory for TxS ring.\n");
1189ba26d470SStanislav Sedov 		return (error);
1190ba26d470SStanislav Sedov 	}
1191ba26d470SStanislav Sedov 	error = bus_dmamap_load(sc->dma_txs_tag, sc->dma_txs_map, sc->txs_base,
1192ba26d470SStanislav Sedov 	    AE_TXS_COUNT_DEFAULT * 4, ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1193ba26d470SStanislav Sedov 	if (error != 0 || busaddr == 0) {
1194ba26d470SStanislav Sedov 		device_printf(sc->dev,
1195ba26d470SStanislav Sedov 		    "could not load DMA map for TxS ring.\n");
1196ba26d470SStanislav Sedov 		return (error);
1197ba26d470SStanislav Sedov 	}
1198ba26d470SStanislav Sedov 	sc->dma_txs_busaddr = busaddr;
1199ba26d470SStanislav Sedov 
1200ba26d470SStanislav Sedov 	/*
1201ba26d470SStanislav Sedov 	 * Allocate RxD DMA memory.
1202ba26d470SStanislav Sedov 	 */
1203ba26d470SStanislav Sedov 	error = bus_dmamem_alloc(sc->dma_rxd_tag, (void **)&sc->rxd_base_dma,
1204ba26d470SStanislav Sedov 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1205ba26d470SStanislav Sedov 	    &sc->dma_rxd_map);
1206ba26d470SStanislav Sedov 	if (error != 0) {
1207ba26d470SStanislav Sedov 		device_printf(sc->dev,
1208ba26d470SStanislav Sedov 		    "could not allocate DMA memory for RxD ring.\n");
1209ba26d470SStanislav Sedov 		return (error);
1210ba26d470SStanislav Sedov 	}
1211ba26d470SStanislav Sedov 	error = bus_dmamap_load(sc->dma_rxd_tag, sc->dma_rxd_map,
1212fcb1bf41SPyun YongHyeon 	    sc->rxd_base_dma, AE_RXD_COUNT_DEFAULT * 1536 + AE_RXD_PADDING,
1213fcb1bf41SPyun YongHyeon 	    ae_dmamap_cb, &busaddr, BUS_DMA_NOWAIT);
1214ba26d470SStanislav Sedov 	if (error != 0 || busaddr == 0) {
1215ba26d470SStanislav Sedov 		device_printf(sc->dev,
1216ba26d470SStanislav Sedov 		    "could not load DMA map for RxD ring.\n");
1217ba26d470SStanislav Sedov 		return (error);
1218ba26d470SStanislav Sedov 	}
1219fcb1bf41SPyun YongHyeon 	sc->dma_rxd_busaddr = busaddr + AE_RXD_PADDING;
1220fcb1bf41SPyun YongHyeon 	sc->rxd_base = (ae_rxd_t *)(sc->rxd_base_dma + AE_RXD_PADDING);
1221ba26d470SStanislav Sedov 
1222ba26d470SStanislav Sedov 	return (0);
1223ba26d470SStanislav Sedov }
1224ba26d470SStanislav Sedov 
1225ba26d470SStanislav Sedov static void
1226ba26d470SStanislav Sedov ae_dma_free(ae_softc_t *sc)
1227ba26d470SStanislav Sedov {
1228ba26d470SStanislav Sedov 
1229ba26d470SStanislav Sedov 	if (sc->dma_txd_tag != NULL) {
1230068d8643SJohn Baldwin 		if (sc->dma_txd_busaddr != 0)
1231ba26d470SStanislav Sedov 			bus_dmamap_unload(sc->dma_txd_tag, sc->dma_txd_map);
1232ba26d470SStanislav Sedov 		if (sc->txd_base != NULL)
1233ba26d470SStanislav Sedov 			bus_dmamem_free(sc->dma_txd_tag, sc->txd_base,
1234ba26d470SStanislav Sedov 			    sc->dma_txd_map);
1235ba26d470SStanislav Sedov 		bus_dma_tag_destroy(sc->dma_txd_tag);
1236ba26d470SStanislav Sedov 		sc->dma_txd_tag = NULL;
1237ba26d470SStanislav Sedov 		sc->txd_base = NULL;
1238068d8643SJohn Baldwin 		sc->dma_txd_busaddr = 0;
1239ba26d470SStanislav Sedov 	}
1240ba26d470SStanislav Sedov 	if (sc->dma_txs_tag != NULL) {
1241068d8643SJohn Baldwin 		if (sc->dma_txs_busaddr != 0)
1242ba26d470SStanislav Sedov 			bus_dmamap_unload(sc->dma_txs_tag, sc->dma_txs_map);
1243ba26d470SStanislav Sedov 		if (sc->txs_base != NULL)
1244ba26d470SStanislav Sedov 			bus_dmamem_free(sc->dma_txs_tag, sc->txs_base,
1245ba26d470SStanislav Sedov 			    sc->dma_txs_map);
1246ba26d470SStanislav Sedov 		bus_dma_tag_destroy(sc->dma_txs_tag);
1247ba26d470SStanislav Sedov 		sc->dma_txs_tag = NULL;
1248ba26d470SStanislav Sedov 		sc->txs_base = NULL;
1249068d8643SJohn Baldwin 		sc->dma_txs_busaddr = 0;
1250ba26d470SStanislav Sedov 	}
1251ba26d470SStanislav Sedov 	if (sc->dma_rxd_tag != NULL) {
1252068d8643SJohn Baldwin 		if (sc->dma_rxd_busaddr != 0)
1253ba26d470SStanislav Sedov 			bus_dmamap_unload(sc->dma_rxd_tag, sc->dma_rxd_map);
1254ba26d470SStanislav Sedov 		if (sc->rxd_base_dma != NULL)
1255068d8643SJohn Baldwin 			bus_dmamem_free(sc->dma_rxd_tag, sc->rxd_base_dma,
1256068d8643SJohn Baldwin 			    sc->dma_rxd_map);
1257ba26d470SStanislav Sedov 		bus_dma_tag_destroy(sc->dma_rxd_tag);
1258ba26d470SStanislav Sedov 		sc->dma_rxd_tag = NULL;
1259ba26d470SStanislav Sedov 		sc->rxd_base_dma = NULL;
1260068d8643SJohn Baldwin 		sc->dma_rxd_busaddr = 0;
1261ba26d470SStanislav Sedov 	}
1262ba26d470SStanislav Sedov 	if (sc->dma_parent_tag != NULL) {
1263ba26d470SStanislav Sedov 		bus_dma_tag_destroy(sc->dma_parent_tag);
1264ba26d470SStanislav Sedov 		sc->dma_parent_tag = NULL;
1265ba26d470SStanislav Sedov 	}
1266ba26d470SStanislav Sedov }
1267ba26d470SStanislav Sedov 
1268ba26d470SStanislav Sedov static int
1269ba26d470SStanislav Sedov ae_shutdown(device_t dev)
1270ba26d470SStanislav Sedov {
1271ba26d470SStanislav Sedov 	ae_softc_t *sc;
1272ba26d470SStanislav Sedov 	int error;
1273ba26d470SStanislav Sedov 
1274ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
1275ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae: %d]: sc is NULL", __LINE__));
1276ba26d470SStanislav Sedov 
1277ba26d470SStanislav Sedov 	error = ae_suspend(dev);
1278ba26d470SStanislav Sedov 	AE_LOCK(sc);
1279ba26d470SStanislav Sedov 	ae_powersave_enable(sc);
1280ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1281ba26d470SStanislav Sedov 	return (error);
1282ba26d470SStanislav Sedov }
1283ba26d470SStanislav Sedov 
1284ba26d470SStanislav Sedov static void
1285ba26d470SStanislav Sedov ae_powersave_disable(ae_softc_t *sc)
1286ba26d470SStanislav Sedov {
1287ba26d470SStanislav Sedov 	uint32_t val;
1288ba26d470SStanislav Sedov 
1289ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1290ba26d470SStanislav Sedov 
1291ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 0);
1292ba26d470SStanislav Sedov 	val = AE_PHY_READ(sc, AE_PHY_DBG_DATA);
1293ba26d470SStanislav Sedov 	if (val & AE_PHY_DBG_POWERSAVE) {
1294ba26d470SStanislav Sedov 		val &= ~AE_PHY_DBG_POWERSAVE;
1295ba26d470SStanislav Sedov 		AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, val);
1296ba26d470SStanislav Sedov 		DELAY(1000);
1297ba26d470SStanislav Sedov 	}
1298ba26d470SStanislav Sedov }
1299ba26d470SStanislav Sedov 
1300ba26d470SStanislav Sedov static void
1301ba26d470SStanislav Sedov ae_powersave_enable(ae_softc_t *sc)
1302ba26d470SStanislav Sedov {
1303ba26d470SStanislav Sedov 	uint32_t val;
1304ba26d470SStanislav Sedov 
1305ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1306ba26d470SStanislav Sedov 
1307ba26d470SStanislav Sedov 	/*
1308ba26d470SStanislav Sedov 	 * XXX magic numbers.
1309ba26d470SStanislav Sedov 	 */
1310ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 0);
1311ba26d470SStanislav Sedov 	val = AE_PHY_READ(sc, AE_PHY_DBG_DATA);
1312ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, val | 0x1000);
1313ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 2);
1314ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, 0x3000);
1315ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_ADDR, 3);
1316ba26d470SStanislav Sedov 	AE_PHY_WRITE(sc, AE_PHY_DBG_DATA, 0);
1317ba26d470SStanislav Sedov }
1318ba26d470SStanislav Sedov 
1319ba26d470SStanislav Sedov static void
1320ba26d470SStanislav Sedov ae_pm_init(ae_softc_t *sc)
1321ba26d470SStanislav Sedov {
1322*3f14814fSJustin Hibbits 	if_t ifp;
1323ba26d470SStanislav Sedov 	uint32_t val;
1324ba26d470SStanislav Sedov 	uint16_t pmstat;
1325ba26d470SStanislav Sedov 	struct mii_data *mii;
1326ba26d470SStanislav Sedov 	int pmc;
1327ba26d470SStanislav Sedov 
1328ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1329ba26d470SStanislav Sedov 
1330ba26d470SStanislav Sedov 	ifp = sc->ifp;
1331ba26d470SStanislav Sedov 	if ((sc->flags & AE_FLAG_PMG) == 0) {
1332ba26d470SStanislav Sedov 		/* Disable WOL entirely. */
1333ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_WOL_REG, 0);
1334ba26d470SStanislav Sedov 		return;
1335ba26d470SStanislav Sedov 	}
1336ba26d470SStanislav Sedov 
1337ba26d470SStanislav Sedov 	/*
1338ba26d470SStanislav Sedov 	 * Configure WOL if enabled.
1339ba26d470SStanislav Sedov 	 */
1340*3f14814fSJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
1341ba26d470SStanislav Sedov 		mii = device_get_softc(sc->miibus);
1342ba26d470SStanislav Sedov 		mii_pollstat(mii);
1343ba26d470SStanislav Sedov 		if ((mii->mii_media_status & IFM_AVALID) != 0 &&
1344ba26d470SStanislav Sedov 		    (mii->mii_media_status & IFM_ACTIVE) != 0) {
1345ba26d470SStanislav Sedov 			AE_WRITE_4(sc, AE_WOL_REG, AE_WOL_MAGIC | \
1346ba26d470SStanislav Sedov 			    AE_WOL_MAGIC_PME);
1347ba26d470SStanislav Sedov 
1348ba26d470SStanislav Sedov 			/*
1349ba26d470SStanislav Sedov 			 * Configure MAC.
1350ba26d470SStanislav Sedov 			 */
1351ba26d470SStanislav Sedov 			val = AE_MAC_RX_EN | AE_MAC_CLK_PHY | \
1352ba26d470SStanislav Sedov 			    AE_MAC_TX_CRC_EN | AE_MAC_TX_AUTOPAD | \
1353ba26d470SStanislav Sedov 			    ((AE_HALFBUF_DEFAULT << AE_HALFBUF_SHIFT) & \
1354ba26d470SStanislav Sedov 			    AE_HALFBUF_MASK) | \
1355ba26d470SStanislav Sedov 			    ((AE_MAC_PREAMBLE_DEFAULT << \
1356ba26d470SStanislav Sedov 			    AE_MAC_PREAMBLE_SHIFT) & AE_MAC_PREAMBLE_MASK) | \
1357ba26d470SStanislav Sedov 			    AE_MAC_BCAST_EN | AE_MAC_MCAST_EN;
1358ba26d470SStanislav Sedov 			if ((IFM_OPTIONS(mii->mii_media_active) & \
1359ba26d470SStanislav Sedov 			    IFM_FDX) != 0)
1360ba26d470SStanislav Sedov 				val |= AE_MAC_FULL_DUPLEX;
1361ba26d470SStanislav Sedov 			AE_WRITE_4(sc, AE_MAC_REG, val);
1362ba26d470SStanislav Sedov 
1363ba26d470SStanislav Sedov 		} else {	/* No link. */
1364ba26d470SStanislav Sedov 			AE_WRITE_4(sc, AE_WOL_REG, AE_WOL_LNKCHG | \
1365ba26d470SStanislav Sedov 			    AE_WOL_LNKCHG_PME);
1366ba26d470SStanislav Sedov 			AE_WRITE_4(sc, AE_MAC_REG, 0);
1367ba26d470SStanislav Sedov 		}
1368ba26d470SStanislav Sedov 	} else {
1369ba26d470SStanislav Sedov 		ae_powersave_enable(sc);
1370ba26d470SStanislav Sedov 	}
1371ba26d470SStanislav Sedov 
1372ba26d470SStanislav Sedov 	/*
1373ba26d470SStanislav Sedov 	 * PCIE hacks. Magic numbers.
1374ba26d470SStanislav Sedov 	 */
1375ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_PCIE_PHYMISC_REG);
1376ba26d470SStanislav Sedov 	val |= AE_PCIE_PHYMISC_FORCE_RCV_DET;
1377ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_PCIE_PHYMISC_REG, val);
1378ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_PCIE_DLL_TX_CTRL_REG);
1379ba26d470SStanislav Sedov 	val |= AE_PCIE_DLL_TX_CTRL_SEL_NOR_CLK;
1380ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_PCIE_DLL_TX_CTRL_REG, val);
1381ba26d470SStanislav Sedov 
1382ba26d470SStanislav Sedov 	/*
1383ba26d470SStanislav Sedov 	 * Configure PME.
1384ba26d470SStanislav Sedov 	 */
13854fb52be9SSergey Kandaurov 	if (pci_find_cap(sc->dev, PCIY_PMG, &pmc) == 0) {
1386ba26d470SStanislav Sedov 		pmstat = pci_read_config(sc->dev, pmc + PCIR_POWER_STATUS, 2);
1387ba26d470SStanislav Sedov 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1388*3f14814fSJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
1389ba26d470SStanislav Sedov 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1390ba26d470SStanislav Sedov 		pci_write_config(sc->dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1391ba26d470SStanislav Sedov 	}
1392858f9d27SKevin Lo }
1393ba26d470SStanislav Sedov 
1394ba26d470SStanislav Sedov static int
1395ba26d470SStanislav Sedov ae_suspend(device_t dev)
1396ba26d470SStanislav Sedov {
1397ba26d470SStanislav Sedov 	ae_softc_t *sc;
1398ba26d470SStanislav Sedov 
1399ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
1400ba26d470SStanislav Sedov 
1401ba26d470SStanislav Sedov 	AE_LOCK(sc);
1402ba26d470SStanislav Sedov 	ae_stop(sc);
1403ba26d470SStanislav Sedov 	ae_pm_init(sc);
1404ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1405ba26d470SStanislav Sedov 
1406ba26d470SStanislav Sedov 	return (0);
1407ba26d470SStanislav Sedov }
1408ba26d470SStanislav Sedov 
1409ba26d470SStanislav Sedov static int
1410ba26d470SStanislav Sedov ae_resume(device_t dev)
1411ba26d470SStanislav Sedov {
1412ba26d470SStanislav Sedov 	ae_softc_t *sc;
1413ba26d470SStanislav Sedov 
1414ba26d470SStanislav Sedov 	sc = device_get_softc(dev);
1415ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1416ba26d470SStanislav Sedov 
1417ba26d470SStanislav Sedov 	AE_LOCK(sc);
1418ba26d470SStanislav Sedov 	AE_READ_4(sc, AE_WOL_REG);	/* Clear WOL status. */
1419*3f14814fSJustin Hibbits 	if ((if_getflags(sc->ifp) & IFF_UP) != 0)
1420ba26d470SStanislav Sedov 		ae_init_locked(sc);
1421ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1422ba26d470SStanislav Sedov 
1423ba26d470SStanislav Sedov 	return (0);
1424ba26d470SStanislav Sedov }
1425ba26d470SStanislav Sedov 
1426ba26d470SStanislav Sedov static unsigned int
1427ba26d470SStanislav Sedov ae_tx_avail_size(ae_softc_t *sc)
1428ba26d470SStanislav Sedov {
1429ba26d470SStanislav Sedov 	unsigned int avail;
1430ba26d470SStanislav Sedov 
1431ba26d470SStanislav Sedov 	if (sc->txd_cur >= sc->txd_ack)
1432ba26d470SStanislav Sedov 		avail = AE_TXD_BUFSIZE_DEFAULT - (sc->txd_cur - sc->txd_ack);
1433ba26d470SStanislav Sedov 	else
1434ba26d470SStanislav Sedov 		avail = sc->txd_ack - sc->txd_cur;
1435ba26d470SStanislav Sedov 
1436e1878334SPyun YongHyeon 	return (avail);
1437ba26d470SStanislav Sedov }
1438ba26d470SStanislav Sedov 
1439ba26d470SStanislav Sedov static int
1440ba26d470SStanislav Sedov ae_encap(ae_softc_t *sc, struct mbuf **m_head)
1441ba26d470SStanislav Sedov {
1442ba26d470SStanislav Sedov 	struct mbuf *m0;
1443ba26d470SStanislav Sedov 	ae_txd_t *hdr;
1444ba26d470SStanislav Sedov 	unsigned int to_end;
1445ba26d470SStanislav Sedov 	uint16_t len;
1446ba26d470SStanislav Sedov 
1447ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1448ba26d470SStanislav Sedov 
1449ba26d470SStanislav Sedov 	m0 = *m_head;
1450ba26d470SStanislav Sedov 	len = m0->m_pkthdr.len;
1451ba26d470SStanislav Sedov 
1452ba26d470SStanislav Sedov 	if ((sc->flags & AE_FLAG_TXAVAIL) == 0 ||
1453e1878334SPyun YongHyeon 	    len + sizeof(ae_txd_t) + 3 > ae_tx_avail_size(sc)) {
1454ba26d470SStanislav Sedov #ifdef AE_DEBUG
1455ba26d470SStanislav Sedov 		if_printf(sc->ifp, "No free Tx available.\n");
1456ba26d470SStanislav Sedov #endif
1457ba26d470SStanislav Sedov 		return ENOBUFS;
1458ba26d470SStanislav Sedov 	}
1459ba26d470SStanislav Sedov 
1460ba26d470SStanislav Sedov 	hdr = (ae_txd_t *)(sc->txd_base + sc->txd_cur);
1461ba26d470SStanislav Sedov 	bzero(hdr, sizeof(*hdr));
1462e1878334SPyun YongHyeon 	/* Skip header size. */
1463e1878334SPyun YongHyeon 	sc->txd_cur = (sc->txd_cur + sizeof(ae_txd_t)) % AE_TXD_BUFSIZE_DEFAULT;
1464e1878334SPyun YongHyeon 	/* Space available to the end of the ring */
1465e1878334SPyun YongHyeon 	to_end = AE_TXD_BUFSIZE_DEFAULT - sc->txd_cur;
1466ba26d470SStanislav Sedov 	if (to_end >= len) {
1467ba26d470SStanislav Sedov 		m_copydata(m0, 0, len, (caddr_t)(sc->txd_base + sc->txd_cur));
1468ba26d470SStanislav Sedov 	} else {
1469ba26d470SStanislav Sedov 		m_copydata(m0, 0, to_end, (caddr_t)(sc->txd_base +
1470ba26d470SStanislav Sedov 		    sc->txd_cur));
1471ba26d470SStanislav Sedov 		m_copydata(m0, to_end, len - to_end, (caddr_t)sc->txd_base);
1472ba26d470SStanislav Sedov 	}
1473ba26d470SStanislav Sedov 
1474ba26d470SStanislav Sedov 	/*
1475ba26d470SStanislav Sedov 	 * Set TxD flags and parameters.
1476ba26d470SStanislav Sedov 	 */
1477ba26d470SStanislav Sedov 	if ((m0->m_flags & M_VLANTAG) != 0) {
1478ba26d470SStanislav Sedov 		hdr->vlan = htole16(AE_TXD_VLAN(m0->m_pkthdr.ether_vtag));
1479ba26d470SStanislav Sedov 		hdr->len = htole16(len | AE_TXD_INSERT_VTAG);
1480ba26d470SStanislav Sedov 	} else {
1481ba26d470SStanislav Sedov 		hdr->len = htole16(len);
1482ba26d470SStanislav Sedov 	}
1483ba26d470SStanislav Sedov 
1484ba26d470SStanislav Sedov 	/*
1485ba26d470SStanislav Sedov 	 * Set current TxD position and round up to a 4-byte boundary.
1486ba26d470SStanislav Sedov 	 */
1487ba26d470SStanislav Sedov 	sc->txd_cur = ((sc->txd_cur + len + 3) & ~3) % AE_TXD_BUFSIZE_DEFAULT;
1488ba26d470SStanislav Sedov 	if (sc->txd_cur == sc->txd_ack)
1489ba26d470SStanislav Sedov 		sc->flags &= ~AE_FLAG_TXAVAIL;
1490ba26d470SStanislav Sedov #ifdef AE_DEBUG
1491ba26d470SStanislav Sedov 	if_printf(sc->ifp, "New txd_cur = %d.\n", sc->txd_cur);
1492ba26d470SStanislav Sedov #endif
1493ba26d470SStanislav Sedov 
1494ba26d470SStanislav Sedov 	/*
1495ba26d470SStanislav Sedov 	 * Update TxS position and check if there are empty TxS available.
1496ba26d470SStanislav Sedov 	 */
1497ba26d470SStanislav Sedov 	sc->txs_base[sc->txs_cur].flags &= ~htole16(AE_TXS_UPDATE);
1498ba26d470SStanislav Sedov 	sc->txs_cur = (sc->txs_cur + 1) % AE_TXS_COUNT_DEFAULT;
1499ba26d470SStanislav Sedov 	if (sc->txs_cur == sc->txs_ack)
1500ba26d470SStanislav Sedov 		sc->flags &= ~AE_FLAG_TXAVAIL;
1501ba26d470SStanislav Sedov 
1502ba26d470SStanislav Sedov 	/*
1503ba26d470SStanislav Sedov 	 * Synchronize DMA memory.
1504ba26d470SStanislav Sedov 	 */
1505ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map, BUS_DMASYNC_PREREAD |
1506ba26d470SStanislav Sedov 	    BUS_DMASYNC_PREWRITE);
1507ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1508ba26d470SStanislav Sedov 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1509ba26d470SStanislav Sedov 
1510ba26d470SStanislav Sedov 	return (0);
1511ba26d470SStanislav Sedov }
1512ba26d470SStanislav Sedov 
1513ba26d470SStanislav Sedov static void
1514*3f14814fSJustin Hibbits ae_start(if_t ifp)
1515ba26d470SStanislav Sedov {
1516ba26d470SStanislav Sedov 	ae_softc_t *sc;
151732341ad6SJohn Baldwin 
1518*3f14814fSJustin Hibbits 	sc = if_getsoftc(ifp);
151932341ad6SJohn Baldwin 	AE_LOCK(sc);
152032341ad6SJohn Baldwin 	ae_start_locked(ifp);
152132341ad6SJohn Baldwin 	AE_UNLOCK(sc);
152232341ad6SJohn Baldwin }
152332341ad6SJohn Baldwin 
152432341ad6SJohn Baldwin static void
1525*3f14814fSJustin Hibbits ae_start_locked(if_t ifp)
152632341ad6SJohn Baldwin {
152732341ad6SJohn Baldwin 	ae_softc_t *sc;
1528ba26d470SStanislav Sedov 	unsigned int count;
1529ba26d470SStanislav Sedov 	struct mbuf *m0;
1530ba26d470SStanislav Sedov 	int error;
1531ba26d470SStanislav Sedov 
1532*3f14814fSJustin Hibbits 	sc = if_getsoftc(ifp);
1533ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
153432341ad6SJohn Baldwin 	AE_LOCK_ASSERT(sc);
1535ba26d470SStanislav Sedov 
1536ba26d470SStanislav Sedov #ifdef AE_DEBUG
1537ba26d470SStanislav Sedov 	if_printf(ifp, "Start called.\n");
1538ba26d470SStanislav Sedov #endif
1539ba26d470SStanislav Sedov 
1540*3f14814fSJustin Hibbits 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
154132341ad6SJohn Baldwin 	    IFF_DRV_RUNNING || (sc->flags & AE_FLAG_LINK) == 0)
1542ba26d470SStanislav Sedov 		return;
1543ba26d470SStanislav Sedov 
1544ba26d470SStanislav Sedov 	count = 0;
1545*3f14814fSJustin Hibbits 	while (!if_sendq_empty(ifp)) {
1546*3f14814fSJustin Hibbits 		m0 = if_dequeue(ifp);
1547ba26d470SStanislav Sedov 		if (m0 == NULL)
1548ba26d470SStanislav Sedov 			break;	/* Nothing to do. */
1549ba26d470SStanislav Sedov 
1550ba26d470SStanislav Sedov 		error = ae_encap(sc, &m0);
1551ba26d470SStanislav Sedov 		if (error != 0) {
1552ba26d470SStanislav Sedov 			if (m0 != NULL) {
1553*3f14814fSJustin Hibbits 				if_sendq_prepend(ifp, m0);
1554*3f14814fSJustin Hibbits 				if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1555ba26d470SStanislav Sedov #ifdef AE_DEBUG
1556ba26d470SStanislav Sedov 				if_printf(ifp, "Setting OACTIVE.\n");
1557ba26d470SStanislav Sedov #endif
1558ba26d470SStanislav Sedov 			}
1559ba26d470SStanislav Sedov 			break;
1560ba26d470SStanislav Sedov 		}
1561ba26d470SStanislav Sedov 		count++;
1562ba26d470SStanislav Sedov 		sc->tx_inproc++;
1563ba26d470SStanislav Sedov 
1564ba26d470SStanislav Sedov 		/* Bounce a copy of the frame to BPF. */
1565ba26d470SStanislav Sedov 		ETHER_BPF_MTAP(ifp, m0);
1566ba26d470SStanislav Sedov 
1567ba26d470SStanislav Sedov 		m_freem(m0);
1568ba26d470SStanislav Sedov 	}
1569ba26d470SStanislav Sedov 
1570ba26d470SStanislav Sedov 	if (count > 0) {	/* Something was dequeued. */
1571ba26d470SStanislav Sedov 		AE_WRITE_2(sc, AE_MB_TXD_IDX_REG, sc->txd_cur / 4);
1572ba26d470SStanislav Sedov 		sc->wd_timer = AE_TX_TIMEOUT;	/* Load watchdog. */
1573ba26d470SStanislav Sedov #ifdef AE_DEBUG
1574ba26d470SStanislav Sedov 		if_printf(ifp, "%d packets dequeued.\n", count);
1575ba26d470SStanislav Sedov 		if_printf(ifp, "Tx pos now is %d.\n", sc->txd_cur);
1576ba26d470SStanislav Sedov #endif
1577ba26d470SStanislav Sedov 	}
1578ba26d470SStanislav Sedov }
1579ba26d470SStanislav Sedov 
1580ba26d470SStanislav Sedov static void
1581ba26d470SStanislav Sedov ae_link_task(void *arg, int pending)
1582ba26d470SStanislav Sedov {
1583ba26d470SStanislav Sedov 	ae_softc_t *sc;
1584ba26d470SStanislav Sedov 	struct mii_data *mii;
1585*3f14814fSJustin Hibbits 	if_t ifp;
1586ba26d470SStanislav Sedov 	uint32_t val;
1587ba26d470SStanislav Sedov 
1588ba26d470SStanislav Sedov 	sc = (ae_softc_t *)arg;
1589ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1590ba26d470SStanislav Sedov 	AE_LOCK(sc);
1591ba26d470SStanislav Sedov 
1592ba26d470SStanislav Sedov 	ifp = sc->ifp;
1593ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
1594ba26d470SStanislav Sedov 	if (mii == NULL || ifp == NULL ||
1595*3f14814fSJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
1596ba26d470SStanislav Sedov 		AE_UNLOCK(sc);	/* XXX: could happen? */
1597ba26d470SStanislav Sedov 		return;
1598ba26d470SStanislav Sedov 	}
1599ba26d470SStanislav Sedov 
1600ba26d470SStanislav Sedov 	sc->flags &= ~AE_FLAG_LINK;
1601ba26d470SStanislav Sedov 	if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
1602ba26d470SStanislav Sedov 	    (IFM_AVALID | IFM_ACTIVE)) {
1603ba26d470SStanislav Sedov 		switch(IFM_SUBTYPE(mii->mii_media_active)) {
1604ba26d470SStanislav Sedov 		case IFM_10_T:
1605ba26d470SStanislav Sedov 		case IFM_100_TX:
1606ba26d470SStanislav Sedov 			sc->flags |= AE_FLAG_LINK;
1607ba26d470SStanislav Sedov 			break;
1608ba26d470SStanislav Sedov 		default:
1609ba26d470SStanislav Sedov 			break;
1610ba26d470SStanislav Sedov 		}
1611ba26d470SStanislav Sedov 	}
1612ba26d470SStanislav Sedov 
1613ba26d470SStanislav Sedov 	/*
1614ba26d470SStanislav Sedov 	 * Stop Rx/Tx MACs.
1615ba26d470SStanislav Sedov 	 */
1616ba26d470SStanislav Sedov 	ae_stop_rxmac(sc);
1617ba26d470SStanislav Sedov 	ae_stop_txmac(sc);
1618ba26d470SStanislav Sedov 
1619ba26d470SStanislav Sedov 	if ((sc->flags & AE_FLAG_LINK) != 0) {
1620ba26d470SStanislav Sedov 		ae_mac_config(sc);
1621ba26d470SStanislav Sedov 
1622ba26d470SStanislav Sedov 		/*
1623ba26d470SStanislav Sedov 		 * Restart DMA engines.
1624ba26d470SStanislav Sedov 		 */
1625ba26d470SStanislav Sedov 		AE_WRITE_1(sc, AE_DMAREAD_REG, AE_DMAREAD_EN);
1626ba26d470SStanislav Sedov 		AE_WRITE_1(sc, AE_DMAWRITE_REG, AE_DMAWRITE_EN);
1627ba26d470SStanislav Sedov 
1628ba26d470SStanislav Sedov 		/*
1629ba26d470SStanislav Sedov 		 * Enable Rx and Tx MACs.
1630ba26d470SStanislav Sedov 		 */
1631ba26d470SStanislav Sedov 		val = AE_READ_4(sc, AE_MAC_REG);
1632ba26d470SStanislav Sedov 		val |= AE_MAC_TX_EN | AE_MAC_RX_EN;
1633ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_MAC_REG, val);
1634ba26d470SStanislav Sedov 	}
1635ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1636ba26d470SStanislav Sedov }
1637ba26d470SStanislav Sedov 
1638ba26d470SStanislav Sedov static void
1639ba26d470SStanislav Sedov ae_stop_rxmac(ae_softc_t *sc)
1640ba26d470SStanislav Sedov {
1641ba26d470SStanislav Sedov 	uint32_t val;
1642ba26d470SStanislav Sedov 	int i;
1643ba26d470SStanislav Sedov 
1644ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1645ba26d470SStanislav Sedov 
1646ba26d470SStanislav Sedov 	/*
1647ba26d470SStanislav Sedov 	 * Stop Rx MAC engine.
1648ba26d470SStanislav Sedov 	 */
1649ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MAC_REG);
1650ba26d470SStanislav Sedov 	if ((val & AE_MAC_RX_EN) != 0) {
1651ba26d470SStanislav Sedov 		val &= ~AE_MAC_RX_EN;
1652ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_MAC_REG, val);
1653ba26d470SStanislav Sedov 	}
1654ba26d470SStanislav Sedov 
1655ba26d470SStanislav Sedov 	/*
1656ba26d470SStanislav Sedov 	 * Stop Rx DMA engine.
1657ba26d470SStanislav Sedov 	 */
1658ba26d470SStanislav Sedov 	if (AE_READ_1(sc, AE_DMAWRITE_REG) == AE_DMAWRITE_EN)
1659ba26d470SStanislav Sedov 		AE_WRITE_1(sc, AE_DMAWRITE_REG, 0);
1660ba26d470SStanislav Sedov 
1661ba26d470SStanislav Sedov 	/*
1662ba26d470SStanislav Sedov 	 * Wait for IDLE state.
1663ba26d470SStanislav Sedov 	 */
16647b0c04c2SGleb Smirnoff 	for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
1665ba26d470SStanislav Sedov 		val = AE_READ_4(sc, AE_IDLE_REG);
1666ba26d470SStanislav Sedov 		if ((val & (AE_IDLE_RXMAC | AE_IDLE_DMAWRITE)) == 0)
1667ba26d470SStanislav Sedov 			break;
1668ba26d470SStanislav Sedov 		DELAY(100);
1669ba26d470SStanislav Sedov 	}
1670ba26d470SStanislav Sedov 	if (i == AE_IDLE_TIMEOUT)
1671ba26d470SStanislav Sedov 		device_printf(sc->dev, "timed out while stopping Rx MAC.\n");
1672ba26d470SStanislav Sedov }
1673ba26d470SStanislav Sedov 
1674ba26d470SStanislav Sedov static void
1675ba26d470SStanislav Sedov ae_stop_txmac(ae_softc_t *sc)
1676ba26d470SStanislav Sedov {
1677ba26d470SStanislav Sedov 	uint32_t val;
1678ba26d470SStanislav Sedov 	int i;
1679ba26d470SStanislav Sedov 
1680ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1681ba26d470SStanislav Sedov 
1682ba26d470SStanislav Sedov 	/*
1683ba26d470SStanislav Sedov 	 * Stop Tx MAC engine.
1684ba26d470SStanislav Sedov 	 */
1685ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MAC_REG);
1686ba26d470SStanislav Sedov 	if ((val & AE_MAC_TX_EN) != 0) {
1687ba26d470SStanislav Sedov 		val &= ~AE_MAC_TX_EN;
1688ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_MAC_REG, val);
1689ba26d470SStanislav Sedov 	}
1690ba26d470SStanislav Sedov 
1691ba26d470SStanislav Sedov 	/*
1692ba26d470SStanislav Sedov 	 * Stop Tx DMA engine.
1693ba26d470SStanislav Sedov 	 */
1694ba26d470SStanislav Sedov 	if (AE_READ_1(sc, AE_DMAREAD_REG) == AE_DMAREAD_EN)
1695ba26d470SStanislav Sedov 		AE_WRITE_1(sc, AE_DMAREAD_REG, 0);
1696ba26d470SStanislav Sedov 
1697ba26d470SStanislav Sedov 	/*
1698ba26d470SStanislav Sedov 	 * Wait for IDLE state.
1699ba26d470SStanislav Sedov 	 */
1700857b699dSPyun YongHyeon 	for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
1701ba26d470SStanislav Sedov 		val = AE_READ_4(sc, AE_IDLE_REG);
1702ba26d470SStanislav Sedov 		if ((val & (AE_IDLE_TXMAC | AE_IDLE_DMAREAD)) == 0)
1703ba26d470SStanislav Sedov 			break;
1704ba26d470SStanislav Sedov 		DELAY(100);
1705ba26d470SStanislav Sedov 	}
1706ba26d470SStanislav Sedov 	if (i == AE_IDLE_TIMEOUT)
1707ba26d470SStanislav Sedov 		device_printf(sc->dev, "timed out while stopping Tx MAC.\n");
1708ba26d470SStanislav Sedov }
1709ba26d470SStanislav Sedov 
1710ba26d470SStanislav Sedov static void
1711ba26d470SStanislav Sedov ae_mac_config(ae_softc_t *sc)
1712ba26d470SStanislav Sedov {
1713ba26d470SStanislav Sedov 	struct mii_data *mii;
1714ba26d470SStanislav Sedov 	uint32_t val;
1715ba26d470SStanislav Sedov 
1716ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1717ba26d470SStanislav Sedov 
1718ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
1719ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MAC_REG);
1720ba26d470SStanislav Sedov 	val &= ~AE_MAC_FULL_DUPLEX;
1721ba26d470SStanislav Sedov 	/* XXX disable AE_MAC_TX_FLOW_EN? */
1722ba26d470SStanislav Sedov 
1723ba26d470SStanislav Sedov 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1724ba26d470SStanislav Sedov 		val |= AE_MAC_FULL_DUPLEX;
1725ba26d470SStanislav Sedov 
1726ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MAC_REG, val);
1727ba26d470SStanislav Sedov }
1728ba26d470SStanislav Sedov 
1729ba26d470SStanislav Sedov static int
1730ba26d470SStanislav Sedov ae_intr(void *arg)
1731ba26d470SStanislav Sedov {
1732ba26d470SStanislav Sedov 	ae_softc_t *sc;
1733ba26d470SStanislav Sedov 	uint32_t val;
1734ba26d470SStanislav Sedov 
1735ba26d470SStanislav Sedov 	sc = (ae_softc_t *)arg;
1736ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL", __LINE__));
1737ba26d470SStanislav Sedov 
1738ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_ISR_REG);
1739ba26d470SStanislav Sedov 	if (val == 0 || (val & AE_IMR_DEFAULT) == 0)
1740ba26d470SStanislav Sedov 		return (FILTER_STRAY);
1741ba26d470SStanislav Sedov 
1742ba26d470SStanislav Sedov 	/* Disable interrupts. */
1743ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, AE_ISR_DISABLE);
1744ba26d470SStanislav Sedov 
1745ba26d470SStanislav Sedov 	/* Schedule interrupt processing. */
1746ba26d470SStanislav Sedov 	taskqueue_enqueue(sc->tq, &sc->int_task);
1747ba26d470SStanislav Sedov 
1748ba26d470SStanislav Sedov 	return (FILTER_HANDLED);
1749ba26d470SStanislav Sedov }
1750ba26d470SStanislav Sedov 
1751ba26d470SStanislav Sedov static void
1752ba26d470SStanislav Sedov ae_int_task(void *arg, int pending)
1753ba26d470SStanislav Sedov {
1754ba26d470SStanislav Sedov 	ae_softc_t *sc;
1755*3f14814fSJustin Hibbits 	if_t ifp;
1756ba26d470SStanislav Sedov 	uint32_t val;
1757ba26d470SStanislav Sedov 
1758ba26d470SStanislav Sedov 	sc = (ae_softc_t *)arg;
1759ba26d470SStanislav Sedov 
1760ba26d470SStanislav Sedov 	AE_LOCK(sc);
1761ba26d470SStanislav Sedov 
1762ba26d470SStanislav Sedov 	ifp = sc->ifp;
1763ba26d470SStanislav Sedov 
1764ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_ISR_REG);	/* Read interrupt status. */
1765b9a17c6cSPyun YongHyeon 	if (val == 0) {
1766b9a17c6cSPyun YongHyeon 		AE_UNLOCK(sc);
1767b9a17c6cSPyun YongHyeon 		return;
1768b9a17c6cSPyun YongHyeon 	}
1769ba26d470SStanislav Sedov 
1770ba26d470SStanislav Sedov 	/*
1771ba26d470SStanislav Sedov 	 * Clear interrupts and disable them.
1772ba26d470SStanislav Sedov 	 */
1773ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, val | AE_ISR_DISABLE);
1774ba26d470SStanislav Sedov 
1775ba26d470SStanislav Sedov #ifdef AE_DEBUG
1776ba26d470SStanislav Sedov 	if_printf(ifp, "Interrupt received: 0x%08x\n", val);
1777ba26d470SStanislav Sedov #endif
1778ba26d470SStanislav Sedov 
1779*3f14814fSJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1780ba26d470SStanislav Sedov 		if ((val & (AE_ISR_DMAR_TIMEOUT | AE_ISR_DMAW_TIMEOUT |
1781ba26d470SStanislav Sedov 		    AE_ISR_PHY_LINKDOWN)) != 0) {
1782*3f14814fSJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1783ba26d470SStanislav Sedov 			ae_init_locked(sc);
1784b7dc7dd4SPyun YongHyeon 			AE_UNLOCK(sc);
1785b7dc7dd4SPyun YongHyeon 			return;
1786ba26d470SStanislav Sedov 		}
1787ba26d470SStanislav Sedov 		if ((val & AE_ISR_TX_EVENT) != 0)
1788ba26d470SStanislav Sedov 			ae_tx_intr(sc);
1789ba26d470SStanislav Sedov 		if ((val & AE_ISR_RX_EVENT) != 0)
1790ba26d470SStanislav Sedov 			ae_rx_intr(sc);
1791ba26d470SStanislav Sedov 		/*
1792ba26d470SStanislav Sedov 		 * Re-enable interrupts.
1793ba26d470SStanislav Sedov 		 */
1794ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_ISR_REG, 0);
1795ba26d470SStanislav Sedov 
1796b9a17c6cSPyun YongHyeon 		if ((sc->flags & AE_FLAG_TXAVAIL) != 0) {
1797*3f14814fSJustin Hibbits 			if (!if_sendq_empty(ifp))
1798b9a17c6cSPyun YongHyeon 				ae_start_locked(ifp);
1799b9a17c6cSPyun YongHyeon 		}
1800b9a17c6cSPyun YongHyeon 	}
1801b9a17c6cSPyun YongHyeon 
1802ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1803ba26d470SStanislav Sedov }
1804ba26d470SStanislav Sedov 
1805ba26d470SStanislav Sedov static void
1806ba26d470SStanislav Sedov ae_tx_intr(ae_softc_t *sc)
1807ba26d470SStanislav Sedov {
1808*3f14814fSJustin Hibbits 	if_t ifp;
1809ba26d470SStanislav Sedov 	ae_txd_t *txd;
1810ba26d470SStanislav Sedov 	ae_txs_t *txs;
1811ba26d470SStanislav Sedov 	uint16_t flags;
1812ba26d470SStanislav Sedov 
1813ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1814ba26d470SStanislav Sedov 
1815ba26d470SStanislav Sedov 	ifp = sc->ifp;
1816ba26d470SStanislav Sedov 
1817ba26d470SStanislav Sedov #ifdef AE_DEBUG
1818ba26d470SStanislav Sedov 	if_printf(ifp, "Tx interrupt occuried.\n");
1819ba26d470SStanislav Sedov #endif
1820ba26d470SStanislav Sedov 
1821ba26d470SStanislav Sedov 	/*
1822ba26d470SStanislav Sedov 	 * Syncronize DMA buffers.
1823ba26d470SStanislav Sedov 	 */
1824ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map,
1825ba26d470SStanislav Sedov 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1826ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1827ba26d470SStanislav Sedov 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1828ba26d470SStanislav Sedov 
1829ba26d470SStanislav Sedov 	for (;;) {
1830ba26d470SStanislav Sedov 		txs = sc->txs_base + sc->txs_ack;
1831ba26d470SStanislav Sedov 		flags = le16toh(txs->flags);
1832ba26d470SStanislav Sedov 		if ((flags & AE_TXS_UPDATE) == 0)
1833ba26d470SStanislav Sedov 			break;
1834ba26d470SStanislav Sedov 		txs->flags = htole16(flags & ~AE_TXS_UPDATE);
1835ba26d470SStanislav Sedov 		/* Update stats. */
1836ba26d470SStanislav Sedov 		ae_update_stats_tx(flags, &sc->stats);
1837ba26d470SStanislav Sedov 
1838ba26d470SStanislav Sedov 		/*
1839ba26d470SStanislav Sedov 		 * Update TxS position.
1840ba26d470SStanislav Sedov 		 */
1841ba26d470SStanislav Sedov 		sc->txs_ack = (sc->txs_ack + 1) % AE_TXS_COUNT_DEFAULT;
1842ba26d470SStanislav Sedov 		sc->flags |= AE_FLAG_TXAVAIL;
1843ba26d470SStanislav Sedov 
1844ba26d470SStanislav Sedov 		txd = (ae_txd_t *)(sc->txd_base + sc->txd_ack);
1845ba26d470SStanislav Sedov 		if (txs->len != txd->len)
1846ba26d470SStanislav Sedov 			device_printf(sc->dev, "Size mismatch: TxS:%d TxD:%d\n",
1847ba26d470SStanislav Sedov 			    le16toh(txs->len), le16toh(txd->len));
1848ba26d470SStanislav Sedov 
1849ba26d470SStanislav Sedov 		/*
1850ba26d470SStanislav Sedov 		 * Move txd ack and align on 4-byte boundary.
1851ba26d470SStanislav Sedov 		 */
1852e1878334SPyun YongHyeon 		sc->txd_ack = ((sc->txd_ack + le16toh(txd->len) +
1853e1878334SPyun YongHyeon 		    sizeof(ae_txs_t) + 3) & ~3) % AE_TXD_BUFSIZE_DEFAULT;
1854ba26d470SStanislav Sedov 
1855ba26d470SStanislav Sedov 		if ((flags & AE_TXS_SUCCESS) != 0)
1856c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1857ba26d470SStanislav Sedov 		else
1858c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1859ba26d470SStanislav Sedov 
1860ba26d470SStanislav Sedov 		sc->tx_inproc--;
1861ba26d470SStanislav Sedov 	}
1862ba26d470SStanislav Sedov 
1863b9a17c6cSPyun YongHyeon 	if ((sc->flags & AE_FLAG_TXAVAIL) != 0)
1864*3f14814fSJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1865ba26d470SStanislav Sedov 	if (sc->tx_inproc < 0) {
1866ba26d470SStanislav Sedov 		if_printf(ifp, "Received stray Tx interrupt(s).\n");
1867ba26d470SStanislav Sedov 		sc->tx_inproc = 0;
1868ba26d470SStanislav Sedov 	}
1869ba26d470SStanislav Sedov 
1870ba26d470SStanislav Sedov 	if (sc->tx_inproc == 0)
1871ba26d470SStanislav Sedov 		sc->wd_timer = 0;	/* Unarm watchdog. */
1872ba26d470SStanislav Sedov 
1873ba26d470SStanislav Sedov 	/*
1874ba26d470SStanislav Sedov 	 * Syncronize DMA buffers.
1875ba26d470SStanislav Sedov 	 */
1876ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txd_tag, sc->dma_txd_map,
1877ba26d470SStanislav Sedov 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1878ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_txs_tag, sc->dma_txs_map,
1879ba26d470SStanislav Sedov 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1880ba26d470SStanislav Sedov }
1881ba26d470SStanislav Sedov 
18822e6383a8SPyun YongHyeon static void
1883ba26d470SStanislav Sedov ae_rxeof(ae_softc_t *sc, ae_rxd_t *rxd)
1884ba26d470SStanislav Sedov {
1885*3f14814fSJustin Hibbits 	if_t ifp;
1886ba26d470SStanislav Sedov 	struct mbuf *m;
1887ba26d470SStanislav Sedov 	unsigned int size;
1888ba26d470SStanislav Sedov 	uint16_t flags;
1889ba26d470SStanislav Sedov 
1890ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1891ba26d470SStanislav Sedov 
1892ba26d470SStanislav Sedov 	ifp = sc->ifp;
1893ba26d470SStanislav Sedov 	flags = le16toh(rxd->flags);
1894ba26d470SStanislav Sedov 
1895ba26d470SStanislav Sedov #ifdef AE_DEBUG
1896ba26d470SStanislav Sedov 	if_printf(ifp, "Rx interrupt occuried.\n");
1897ba26d470SStanislav Sedov #endif
1898ba26d470SStanislav Sedov 	size = le16toh(rxd->len) - ETHER_CRC_LEN;
1899e4ec1e68SStanislav Sedov 	if (size < (ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_VLAN_ENCAP_LEN)) {
1900e4ec1e68SStanislav Sedov 		if_printf(ifp, "Runt frame received.");
1901c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
19022e6383a8SPyun YongHyeon 		return;
1903ba26d470SStanislav Sedov 	}
1904ba26d470SStanislav Sedov 
1905ba26d470SStanislav Sedov 	m = m_devget(&rxd->data[0], size, ETHER_ALIGN, ifp, NULL);
19062e6383a8SPyun YongHyeon 	if (m == NULL) {
1907c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
19082e6383a8SPyun YongHyeon 		return;
19092e6383a8SPyun YongHyeon 	}
1910ba26d470SStanislav Sedov 
1911*3f14814fSJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
1912ba26d470SStanislav Sedov 	    (flags & AE_RXD_HAS_VLAN) != 0) {
1913ba26d470SStanislav Sedov 		m->m_pkthdr.ether_vtag = AE_RXD_VLAN(le16toh(rxd->vlan));
1914ba26d470SStanislav Sedov 		m->m_flags |= M_VLANTAG;
1915ba26d470SStanislav Sedov 	}
1916ba26d470SStanislav Sedov 
1917c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1918ba26d470SStanislav Sedov 	/*
1919ba26d470SStanislav Sedov 	 * Pass it through.
1920ba26d470SStanislav Sedov 	 */
1921ba26d470SStanislav Sedov 	AE_UNLOCK(sc);
1922*3f14814fSJustin Hibbits 	if_input(ifp, m);
1923ba26d470SStanislav Sedov 	AE_LOCK(sc);
1924ba26d470SStanislav Sedov }
1925ba26d470SStanislav Sedov 
1926ba26d470SStanislav Sedov static void
1927ba26d470SStanislav Sedov ae_rx_intr(ae_softc_t *sc)
1928ba26d470SStanislav Sedov {
1929ba26d470SStanislav Sedov 	ae_rxd_t *rxd;
1930*3f14814fSJustin Hibbits 	if_t ifp;
1931ba26d470SStanislav Sedov 	uint16_t flags;
19322e6383a8SPyun YongHyeon 	int count;
1933ba26d470SStanislav Sedov 
1934ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
1935ba26d470SStanislav Sedov 
1936ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1937ba26d470SStanislav Sedov 
1938ba26d470SStanislav Sedov 	ifp = sc->ifp;
1939ba26d470SStanislav Sedov 
1940ba26d470SStanislav Sedov 	/*
1941ba26d470SStanislav Sedov 	 * Syncronize DMA buffers.
1942ba26d470SStanislav Sedov 	 */
1943ba26d470SStanislav Sedov 	bus_dmamap_sync(sc->dma_rxd_tag, sc->dma_rxd_map,
1944ba26d470SStanislav Sedov 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1945ba26d470SStanislav Sedov 
1946b9a17c6cSPyun YongHyeon 	for (count = 0;; count++) {
1947ba26d470SStanislav Sedov 		rxd = (ae_rxd_t *)(sc->rxd_base + sc->rxd_cur);
1948ba26d470SStanislav Sedov 		flags = le16toh(rxd->flags);
1949ba26d470SStanislav Sedov 		if ((flags & AE_RXD_UPDATE) == 0)
1950ba26d470SStanislav Sedov 			break;
1951ba26d470SStanislav Sedov 		rxd->flags = htole16(flags & ~AE_RXD_UPDATE);
1952ba26d470SStanislav Sedov 		/* Update stats. */
1953ba26d470SStanislav Sedov 		ae_update_stats_rx(flags, &sc->stats);
1954ba26d470SStanislav Sedov 
1955ba26d470SStanislav Sedov 		/*
1956ba26d470SStanislav Sedov 		 * Update position index.
1957ba26d470SStanislav Sedov 		 */
1958ba26d470SStanislav Sedov 		sc->rxd_cur = (sc->rxd_cur + 1) % AE_RXD_COUNT_DEFAULT;
1959ba26d470SStanislav Sedov 
19602e6383a8SPyun YongHyeon 		if ((flags & AE_RXD_SUCCESS) != 0)
19612e6383a8SPyun YongHyeon 			ae_rxeof(sc, rxd);
19622e6383a8SPyun YongHyeon 		else
1963c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1964ba26d470SStanislav Sedov 	}
1965ba26d470SStanislav Sedov 
1966b9a17c6cSPyun YongHyeon 	if (count > 0) {
1967b9a17c6cSPyun YongHyeon 		bus_dmamap_sync(sc->dma_rxd_tag, sc->dma_rxd_map,
1968b9a17c6cSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1969ba26d470SStanislav Sedov 		/*
1970ba26d470SStanislav Sedov 		 * Update Rx index.
1971ba26d470SStanislav Sedov 		 */
1972ba26d470SStanislav Sedov 		AE_WRITE_2(sc, AE_MB_RXD_IDX_REG, sc->rxd_cur);
1973ba26d470SStanislav Sedov 	}
1974b9a17c6cSPyun YongHyeon }
1975ba26d470SStanislav Sedov 
1976ba26d470SStanislav Sedov static void
1977ba26d470SStanislav Sedov ae_watchdog(ae_softc_t *sc)
1978ba26d470SStanislav Sedov {
1979*3f14814fSJustin Hibbits 	if_t ifp;
1980ba26d470SStanislav Sedov 
1981ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
1982ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
1983ba26d470SStanislav Sedov 	ifp = sc->ifp;
1984ba26d470SStanislav Sedov 
1985ba26d470SStanislav Sedov 	if (sc->wd_timer == 0 || --sc->wd_timer != 0)
1986ba26d470SStanislav Sedov 		return;		/* Noting to do. */
1987ba26d470SStanislav Sedov 
1988ba26d470SStanislav Sedov 	if ((sc->flags & AE_FLAG_LINK) == 0)
1989ba26d470SStanislav Sedov 		if_printf(ifp, "watchdog timeout (missed link).\n");
1990ba26d470SStanislav Sedov 	else
1991ba26d470SStanislav Sedov 		if_printf(ifp, "watchdog timeout - resetting.\n");
1992ba26d470SStanislav Sedov 
1993c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1994*3f14814fSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1995ba26d470SStanislav Sedov 	ae_init_locked(sc);
1996*3f14814fSJustin Hibbits 	if (!if_sendq_empty(ifp))
199732341ad6SJohn Baldwin 		ae_start_locked(ifp);
1998ba26d470SStanislav Sedov }
1999ba26d470SStanislav Sedov 
2000ba26d470SStanislav Sedov static void
2001ba26d470SStanislav Sedov ae_tick(void *arg)
2002ba26d470SStanislav Sedov {
2003ba26d470SStanislav Sedov 	ae_softc_t *sc;
2004ba26d470SStanislav Sedov 	struct mii_data *mii;
2005ba26d470SStanislav Sedov 
2006ba26d470SStanislav Sedov 	sc = (ae_softc_t *)arg;
2007ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
2008ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
2009ba26d470SStanislav Sedov 
2010ba26d470SStanislav Sedov 	mii = device_get_softc(sc->miibus);
2011ba26d470SStanislav Sedov 	mii_tick(mii);
2012ba26d470SStanislav Sedov 	ae_watchdog(sc);	/* Watchdog check. */
2013ba26d470SStanislav Sedov 	callout_reset(&sc->tick_ch, hz, ae_tick, sc);
2014ba26d470SStanislav Sedov }
2015ba26d470SStanislav Sedov 
2016ba26d470SStanislav Sedov static void
2017ba26d470SStanislav Sedov ae_rxvlan(ae_softc_t *sc)
2018ba26d470SStanislav Sedov {
2019*3f14814fSJustin Hibbits 	if_t ifp;
2020ba26d470SStanislav Sedov 	uint32_t val;
2021ba26d470SStanislav Sedov 
2022ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
2023ba26d470SStanislav Sedov 	ifp = sc->ifp;
2024ba26d470SStanislav Sedov 	val = AE_READ_4(sc, AE_MAC_REG);
2025ba26d470SStanislav Sedov 	val &= ~AE_MAC_RMVLAN_EN;
2026*3f14814fSJustin Hibbits 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
2027ba26d470SStanislav Sedov 		val |= AE_MAC_RMVLAN_EN;
2028ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MAC_REG, val);
2029ba26d470SStanislav Sedov }
2030ba26d470SStanislav Sedov 
2031f1c5eb0dSGleb Smirnoff static u_int
2032f1c5eb0dSGleb Smirnoff ae_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2033f1c5eb0dSGleb Smirnoff {
2034f1c5eb0dSGleb Smirnoff 	uint32_t crc, *mchash = arg;
2035f1c5eb0dSGleb Smirnoff 
2036f1c5eb0dSGleb Smirnoff 	crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
2037f1c5eb0dSGleb Smirnoff 	mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
2038f1c5eb0dSGleb Smirnoff 
2039f1c5eb0dSGleb Smirnoff 	return (1);
2040f1c5eb0dSGleb Smirnoff }
2041f1c5eb0dSGleb Smirnoff 
2042ba26d470SStanislav Sedov static void
2043ba26d470SStanislav Sedov ae_rxfilter(ae_softc_t *sc)
2044ba26d470SStanislav Sedov {
2045*3f14814fSJustin Hibbits 	if_t ifp;
2046ba26d470SStanislav Sedov 	uint32_t mchash[2];
2047ba26d470SStanislav Sedov 	uint32_t rxcfg;
2048ba26d470SStanislav Sedov 
2049ba26d470SStanislav Sedov 	KASSERT(sc != NULL, ("[ae, %d]: sc is NULL!", __LINE__));
2050ba26d470SStanislav Sedov 
2051ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
2052ba26d470SStanislav Sedov 
2053ba26d470SStanislav Sedov 	ifp = sc->ifp;
2054ba26d470SStanislav Sedov 
2055ba26d470SStanislav Sedov 	rxcfg = AE_READ_4(sc, AE_MAC_REG);
2056ba26d470SStanislav Sedov 	rxcfg &= ~(AE_MAC_MCAST_EN | AE_MAC_BCAST_EN | AE_MAC_PROMISC_EN);
2057ba26d470SStanislav Sedov 
2058*3f14814fSJustin Hibbits 	if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
2059ba26d470SStanislav Sedov 		rxcfg |= AE_MAC_BCAST_EN;
2060*3f14814fSJustin Hibbits 	if ((if_getflags(ifp) & IFF_PROMISC) != 0)
2061ba26d470SStanislav Sedov 		rxcfg |= AE_MAC_PROMISC_EN;
2062*3f14814fSJustin Hibbits 	if ((if_getflags(ifp) & IFF_ALLMULTI) != 0)
2063ba26d470SStanislav Sedov 		rxcfg |= AE_MAC_MCAST_EN;
2064ba26d470SStanislav Sedov 
2065ba26d470SStanislav Sedov 	/*
2066ba26d470SStanislav Sedov 	 * Wipe old settings.
2067ba26d470SStanislav Sedov 	 */
2068ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_REG_MHT0, 0);
2069ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_REG_MHT1, 0);
2070*3f14814fSJustin Hibbits 	if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2071ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_REG_MHT0, 0xffffffff);
2072ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_REG_MHT1, 0xffffffff);
2073ba26d470SStanislav Sedov 		AE_WRITE_4(sc, AE_MAC_REG, rxcfg);
2074ba26d470SStanislav Sedov 		return;
2075ba26d470SStanislav Sedov 	}
2076ba26d470SStanislav Sedov 
2077ba26d470SStanislav Sedov 	/*
2078ba26d470SStanislav Sedov 	 * Load multicast tables.
2079ba26d470SStanislav Sedov 	 */
2080ba26d470SStanislav Sedov 	bzero(mchash, sizeof(mchash));
2081f1c5eb0dSGleb Smirnoff 	if_foreach_llmaddr(ifp, ae_hash_maddr, &mchash);
2082ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_REG_MHT0, mchash[0]);
2083ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_REG_MHT1, mchash[1]);
2084ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_MAC_REG, rxcfg);
2085ba26d470SStanislav Sedov }
2086ba26d470SStanislav Sedov 
2087ba26d470SStanislav Sedov static int
2088*3f14814fSJustin Hibbits ae_ioctl(if_t ifp, u_long cmd, caddr_t data)
2089ba26d470SStanislav Sedov {
2090ba26d470SStanislav Sedov 	struct ae_softc *sc;
2091ba26d470SStanislav Sedov 	struct ifreq *ifr;
2092ba26d470SStanislav Sedov 	struct mii_data *mii;
2093ba26d470SStanislav Sedov 	int error, mask;
2094ba26d470SStanislav Sedov 
2095*3f14814fSJustin Hibbits 	sc = if_getsoftc(ifp);
2096ba26d470SStanislav Sedov 	ifr = (struct ifreq *)data;
2097ba26d470SStanislav Sedov 	error = 0;
2098ba26d470SStanislav Sedov 
2099ba26d470SStanislav Sedov 	switch (cmd) {
2100ba26d470SStanislav Sedov 	case SIOCSIFMTU:
2101ba26d470SStanislav Sedov 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
2102ba26d470SStanislav Sedov 			error = EINVAL;
2103*3f14814fSJustin Hibbits 		else if (if_getmtu(ifp) != ifr->ifr_mtu) {
2104ba26d470SStanislav Sedov 			AE_LOCK(sc);
2105*3f14814fSJustin Hibbits 			if_setmtu(ifp, ifr->ifr_mtu);
2106*3f14814fSJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2107*3f14814fSJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2108ba26d470SStanislav Sedov 				ae_init_locked(sc);
2109b7dc7dd4SPyun YongHyeon 			}
2110ba26d470SStanislav Sedov 			AE_UNLOCK(sc);
2111ba26d470SStanislav Sedov 		}
2112ba26d470SStanislav Sedov 		break;
2113ba26d470SStanislav Sedov 	case SIOCSIFFLAGS:
2114ba26d470SStanislav Sedov 		AE_LOCK(sc);
2115*3f14814fSJustin Hibbits 		if ((if_getflags(ifp) & IFF_UP) != 0) {
2116*3f14814fSJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
2117*3f14814fSJustin Hibbits 				if (((if_getflags(ifp) ^ sc->if_flags)
2118ba26d470SStanislav Sedov 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2119ba26d470SStanislav Sedov 					ae_rxfilter(sc);
2120ba26d470SStanislav Sedov 			} else {
2121ba26d470SStanislav Sedov 				if ((sc->flags & AE_FLAG_DETACH) == 0)
2122ba26d470SStanislav Sedov 					ae_init_locked(sc);
2123ba26d470SStanislav Sedov 			}
2124ba26d470SStanislav Sedov 		} else {
2125*3f14814fSJustin Hibbits 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2126ba26d470SStanislav Sedov 				ae_stop(sc);
2127ba26d470SStanislav Sedov 		}
2128*3f14814fSJustin Hibbits 		sc->if_flags = if_getflags(ifp);
2129ba26d470SStanislav Sedov 		AE_UNLOCK(sc);
2130ba26d470SStanislav Sedov 		break;
2131ba26d470SStanislav Sedov 	case SIOCADDMULTI:
2132ba26d470SStanislav Sedov 	case SIOCDELMULTI:
2133ba26d470SStanislav Sedov 		AE_LOCK(sc);
2134*3f14814fSJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2135ba26d470SStanislav Sedov 			ae_rxfilter(sc);
2136ba26d470SStanislav Sedov 		AE_UNLOCK(sc);
2137ba26d470SStanislav Sedov 		break;
2138ba26d470SStanislav Sedov 	case SIOCSIFMEDIA:
2139ba26d470SStanislav Sedov 	case SIOCGIFMEDIA:
2140ba26d470SStanislav Sedov 		mii = device_get_softc(sc->miibus);
2141ba26d470SStanislav Sedov 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2142ba26d470SStanislav Sedov 		break;
2143ba26d470SStanislav Sedov 	case SIOCSIFCAP:
2144ba26d470SStanislav Sedov 		AE_LOCK(sc);
2145*3f14814fSJustin Hibbits 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2146ba26d470SStanislav Sedov 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2147*3f14814fSJustin Hibbits 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
2148*3f14814fSJustin Hibbits 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
2149ba26d470SStanislav Sedov 			ae_rxvlan(sc);
2150ba26d470SStanislav Sedov 		}
2151ba26d470SStanislav Sedov 		VLAN_CAPABILITIES(ifp);
2152ba26d470SStanislav Sedov 		AE_UNLOCK(sc);
2153ba26d470SStanislav Sedov 		break;
2154ba26d470SStanislav Sedov 	default:
2155ba26d470SStanislav Sedov 		error = ether_ioctl(ifp, cmd, data);
2156ba26d470SStanislav Sedov 		break;
2157ba26d470SStanislav Sedov 	}
2158ba26d470SStanislav Sedov 	return (error);
2159ba26d470SStanislav Sedov }
2160ba26d470SStanislav Sedov 
2161ba26d470SStanislav Sedov static void
2162ba26d470SStanislav Sedov ae_stop(ae_softc_t *sc)
2163ba26d470SStanislav Sedov {
2164*3f14814fSJustin Hibbits 	if_t ifp;
2165ba26d470SStanislav Sedov 	int i;
2166ba26d470SStanislav Sedov 
2167ba26d470SStanislav Sedov 	AE_LOCK_ASSERT(sc);
2168ba26d470SStanislav Sedov 
2169ba26d470SStanislav Sedov 	ifp = sc->ifp;
2170*3f14814fSJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
2171ba26d470SStanislav Sedov 	sc->flags &= ~AE_FLAG_LINK;
2172ba26d470SStanislav Sedov 	sc->wd_timer = 0;	/* Cancel watchdog. */
2173ba26d470SStanislav Sedov 	callout_stop(&sc->tick_ch);
2174ba26d470SStanislav Sedov 
2175ba26d470SStanislav Sedov 	/*
2176ba26d470SStanislav Sedov 	 * Clear and disable interrupts.
2177ba26d470SStanislav Sedov 	 */
2178ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_IMR_REG, 0);
2179ba26d470SStanislav Sedov 	AE_WRITE_4(sc, AE_ISR_REG, 0xffffffff);
2180ba26d470SStanislav Sedov 
2181ba26d470SStanislav Sedov 	/*
2182ba26d470SStanislav Sedov 	 * Stop Rx/Tx MACs.
2183ba26d470SStanislav Sedov 	 */
2184ba26d470SStanislav Sedov 	ae_stop_txmac(sc);
2185ba26d470SStanislav Sedov 	ae_stop_rxmac(sc);
2186ba26d470SStanislav Sedov 
2187ba26d470SStanislav Sedov 	/*
2188ba26d470SStanislav Sedov 	 * Stop DMA engines.
2189ba26d470SStanislav Sedov 	 */
2190ba26d470SStanislav Sedov 	AE_WRITE_1(sc, AE_DMAREAD_REG, ~AE_DMAREAD_EN);
2191ba26d470SStanislav Sedov 	AE_WRITE_1(sc, AE_DMAWRITE_REG, ~AE_DMAWRITE_EN);
2192ba26d470SStanislav Sedov 
2193ba26d470SStanislav Sedov 	/*
2194ba26d470SStanislav Sedov 	 * Wait for everything to enter idle state.
2195ba26d470SStanislav Sedov 	 */
2196ba26d470SStanislav Sedov 	for (i = 0; i < AE_IDLE_TIMEOUT; i++) {
2197ba26d470SStanislav Sedov 		if (AE_READ_4(sc, AE_IDLE_REG) == 0)
2198ba26d470SStanislav Sedov 			break;
2199ba26d470SStanislav Sedov 		DELAY(100);
2200ba26d470SStanislav Sedov 	}
2201ba26d470SStanislav Sedov 	if (i == AE_IDLE_TIMEOUT)
2202ba26d470SStanislav Sedov 		device_printf(sc->dev, "could not enter idle state in stop.\n");
2203ba26d470SStanislav Sedov }
2204ba26d470SStanislav Sedov 
2205ba26d470SStanislav Sedov static void
2206ba26d470SStanislav Sedov ae_update_stats_tx(uint16_t flags, ae_stats_t *stats)
2207ba26d470SStanislav Sedov {
2208ba26d470SStanislav Sedov 
2209ba26d470SStanislav Sedov 	if ((flags & AE_TXS_BCAST) != 0)
2210ba26d470SStanislav Sedov 		stats->tx_bcast++;
2211ba26d470SStanislav Sedov 	if ((flags & AE_TXS_MCAST) != 0)
2212ba26d470SStanislav Sedov 		stats->tx_mcast++;
2213ba26d470SStanislav Sedov 	if ((flags & AE_TXS_PAUSE) != 0)
2214ba26d470SStanislav Sedov 		stats->tx_pause++;
2215ba26d470SStanislav Sedov 	if ((flags & AE_TXS_CTRL) != 0)
2216ba26d470SStanislav Sedov 		stats->tx_ctrl++;
2217ba26d470SStanislav Sedov 	if ((flags & AE_TXS_DEFER) != 0)
2218ba26d470SStanislav Sedov 		stats->tx_defer++;
2219ba26d470SStanislav Sedov 	if ((flags & AE_TXS_EXCDEFER) != 0)
2220ba26d470SStanislav Sedov 		stats->tx_excdefer++;
2221ba26d470SStanislav Sedov 	if ((flags & AE_TXS_SINGLECOL) != 0)
2222ba26d470SStanislav Sedov 		stats->tx_singlecol++;
2223ba26d470SStanislav Sedov 	if ((flags & AE_TXS_MULTICOL) != 0)
2224ba26d470SStanislav Sedov 		stats->tx_multicol++;
2225ba26d470SStanislav Sedov 	if ((flags & AE_TXS_LATECOL) != 0)
2226ba26d470SStanislav Sedov 		stats->tx_latecol++;
2227ba26d470SStanislav Sedov 	if ((flags & AE_TXS_ABORTCOL) != 0)
2228ba26d470SStanislav Sedov 		stats->tx_abortcol++;
2229ba26d470SStanislav Sedov 	if ((flags & AE_TXS_UNDERRUN) != 0)
2230ba26d470SStanislav Sedov 		stats->tx_underrun++;
2231ba26d470SStanislav Sedov }
2232ba26d470SStanislav Sedov 
2233ba26d470SStanislav Sedov static void
2234ba26d470SStanislav Sedov ae_update_stats_rx(uint16_t flags, ae_stats_t *stats)
2235ba26d470SStanislav Sedov {
2236ba26d470SStanislav Sedov 
2237ba26d470SStanislav Sedov 	if ((flags & AE_RXD_BCAST) != 0)
2238ba26d470SStanislav Sedov 		stats->rx_bcast++;
2239ba26d470SStanislav Sedov 	if ((flags & AE_RXD_MCAST) != 0)
2240ba26d470SStanislav Sedov 		stats->rx_mcast++;
2241ba26d470SStanislav Sedov 	if ((flags & AE_RXD_PAUSE) != 0)
2242ba26d470SStanislav Sedov 		stats->rx_pause++;
2243ba26d470SStanislav Sedov 	if ((flags & AE_RXD_CTRL) != 0)
2244ba26d470SStanislav Sedov 		stats->rx_ctrl++;
2245ba26d470SStanislav Sedov 	if ((flags & AE_RXD_CRCERR) != 0)
2246ba26d470SStanislav Sedov 		stats->rx_crcerr++;
2247ba26d470SStanislav Sedov 	if ((flags & AE_RXD_CODEERR) != 0)
2248ba26d470SStanislav Sedov 		stats->rx_codeerr++;
2249ba26d470SStanislav Sedov 	if ((flags & AE_RXD_RUNT) != 0)
2250ba26d470SStanislav Sedov 		stats->rx_runt++;
2251ba26d470SStanislav Sedov 	if ((flags & AE_RXD_FRAG) != 0)
2252ba26d470SStanislav Sedov 		stats->rx_frag++;
2253ba26d470SStanislav Sedov 	if ((flags & AE_RXD_TRUNC) != 0)
2254ba26d470SStanislav Sedov 		stats->rx_trunc++;
2255ba26d470SStanislav Sedov 	if ((flags & AE_RXD_ALIGN) != 0)
2256ba26d470SStanislav Sedov 		stats->rx_align++;
2257ba26d470SStanislav Sedov }
2258