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