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