xref: /freebsd/sys/dev/alc/if_alc.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*-
2  * Copyright (c) 2009, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/rman.h>
44 #include <sys/queue.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/sysctl.h>
48 #include <sys/taskqueue.h>
49 
50 #include <net/bpf.h>
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_llc.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 #include <net/if_vlan_var.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/tcp.h>
65 
66 #include <dev/mii/mii.h>
67 #include <dev/mii/miivar.h>
68 
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
71 
72 #include <machine/bus.h>
73 #include <machine/in_cksum.h>
74 
75 #include <dev/alc/if_alcreg.h>
76 #include <dev/alc/if_alcvar.h>
77 
78 /* "device miibus" required.  See GENERIC if you get errors here. */
79 #include "miibus_if.h"
80 #undef ALC_USE_CUSTOM_CSUM
81 
82 #ifdef ALC_USE_CUSTOM_CSUM
83 #define	ALC_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
84 #else
85 #define	ALC_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
86 #endif
87 
88 MODULE_DEPEND(alc, pci, 1, 1, 1);
89 MODULE_DEPEND(alc, ether, 1, 1, 1);
90 MODULE_DEPEND(alc, miibus, 1, 1, 1);
91 
92 /* Tunables. */
93 static int msi_disable = 0;
94 static int msix_disable = 0;
95 TUNABLE_INT("hw.alc.msi_disable", &msi_disable);
96 TUNABLE_INT("hw.alc.msix_disable", &msix_disable);
97 
98 /*
99  * Devices supported by this driver.
100  */
101 static struct alc_ident alc_ident_table[] = {
102 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8131, 9 * 1024,
103 		"Atheros AR8131 PCIe Gigabit Ethernet" },
104 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8132, 9 * 1024,
105 		"Atheros AR8132 PCIe Fast Ethernet" },
106 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151, 6 * 1024,
107 		"Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
108 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151_V2, 6 * 1024,
109 		"Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
110 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B, 6 * 1024,
111 		"Atheros AR8152 v1.1 PCIe Fast Ethernet" },
112 	{ VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B2, 6 * 1024,
113 		"Atheros AR8152 v2.0 PCIe Fast Ethernet" },
114 	{ 0, 0, 0, NULL}
115 };
116 
117 static void	alc_aspm(struct alc_softc *, int);
118 static int	alc_attach(device_t);
119 static int	alc_check_boundary(struct alc_softc *);
120 static int	alc_detach(device_t);
121 static void	alc_disable_l0s_l1(struct alc_softc *);
122 static int	alc_dma_alloc(struct alc_softc *);
123 static void	alc_dma_free(struct alc_softc *);
124 static void	alc_dmamap_cb(void *, bus_dma_segment_t *, int, int);
125 static int	alc_encap(struct alc_softc *, struct mbuf **);
126 static struct alc_ident *
127 		alc_find_ident(device_t);
128 #ifndef __NO_STRICT_ALIGNMENT
129 static struct mbuf *
130 		alc_fixup_rx(struct ifnet *, struct mbuf *);
131 #endif
132 static void	alc_get_macaddr(struct alc_softc *);
133 static void	alc_init(void *);
134 static void	alc_init_cmb(struct alc_softc *);
135 static void	alc_init_locked(struct alc_softc *);
136 static void	alc_init_rr_ring(struct alc_softc *);
137 static int	alc_init_rx_ring(struct alc_softc *);
138 static void	alc_init_smb(struct alc_softc *);
139 static void	alc_init_tx_ring(struct alc_softc *);
140 static void	alc_int_task(void *, int);
141 static int	alc_intr(void *);
142 static int	alc_ioctl(struct ifnet *, u_long, caddr_t);
143 static void	alc_mac_config(struct alc_softc *);
144 static int	alc_miibus_readreg(device_t, int, int);
145 static void	alc_miibus_statchg(device_t);
146 static int	alc_miibus_writereg(device_t, int, int, int);
147 static int	alc_mediachange(struct ifnet *);
148 static void	alc_mediastatus(struct ifnet *, struct ifmediareq *);
149 static int	alc_newbuf(struct alc_softc *, struct alc_rxdesc *);
150 static void	alc_phy_down(struct alc_softc *);
151 static void	alc_phy_reset(struct alc_softc *);
152 static int	alc_probe(device_t);
153 static void	alc_reset(struct alc_softc *);
154 static int	alc_resume(device_t);
155 static void	alc_rxeof(struct alc_softc *, struct rx_rdesc *);
156 static int	alc_rxintr(struct alc_softc *, int);
157 static void	alc_rxfilter(struct alc_softc *);
158 static void	alc_rxvlan(struct alc_softc *);
159 static void	alc_setlinkspeed(struct alc_softc *);
160 static void	alc_setwol(struct alc_softc *);
161 static int	alc_shutdown(device_t);
162 static void	alc_start(struct ifnet *);
163 static void	alc_start_locked(struct ifnet *);
164 static void	alc_start_queue(struct alc_softc *);
165 static void	alc_stats_clear(struct alc_softc *);
166 static void	alc_stats_update(struct alc_softc *);
167 static void	alc_stop(struct alc_softc *);
168 static void	alc_stop_mac(struct alc_softc *);
169 static void	alc_stop_queue(struct alc_softc *);
170 static int	alc_suspend(device_t);
171 static void	alc_sysctl_node(struct alc_softc *);
172 static void	alc_tick(void *);
173 static void	alc_txeof(struct alc_softc *);
174 static void	alc_watchdog(struct alc_softc *);
175 static int	sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
176 static int	sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS);
177 static int	sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS);
178 
179 static device_method_t alc_methods[] = {
180 	/* Device interface. */
181 	DEVMETHOD(device_probe,		alc_probe),
182 	DEVMETHOD(device_attach,	alc_attach),
183 	DEVMETHOD(device_detach,	alc_detach),
184 	DEVMETHOD(device_shutdown,	alc_shutdown),
185 	DEVMETHOD(device_suspend,	alc_suspend),
186 	DEVMETHOD(device_resume,	alc_resume),
187 
188 	/* MII interface. */
189 	DEVMETHOD(miibus_readreg,	alc_miibus_readreg),
190 	DEVMETHOD(miibus_writereg,	alc_miibus_writereg),
191 	DEVMETHOD(miibus_statchg,	alc_miibus_statchg),
192 
193 	{ NULL, NULL }
194 };
195 
196 static driver_t alc_driver = {
197 	"alc",
198 	alc_methods,
199 	sizeof(struct alc_softc)
200 };
201 
202 static devclass_t alc_devclass;
203 
204 DRIVER_MODULE(alc, pci, alc_driver, alc_devclass, 0, 0);
205 DRIVER_MODULE(miibus, alc, miibus_driver, miibus_devclass, 0, 0);
206 
207 static struct resource_spec alc_res_spec_mem[] = {
208 	{ SYS_RES_MEMORY,	PCIR_BAR(0),	RF_ACTIVE },
209 	{ -1,			0,		0 }
210 };
211 
212 static struct resource_spec alc_irq_spec_legacy[] = {
213 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
214 	{ -1,			0,		0 }
215 };
216 
217 static struct resource_spec alc_irq_spec_msi[] = {
218 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
219 	{ -1,			0,		0 }
220 };
221 
222 static struct resource_spec alc_irq_spec_msix[] = {
223 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
224 	{ -1,			0,		0 }
225 };
226 
227 static uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0 };
228 
229 static int
230 alc_miibus_readreg(device_t dev, int phy, int reg)
231 {
232 	struct alc_softc *sc;
233 	uint32_t v;
234 	int i;
235 
236 	sc = device_get_softc(dev);
237 
238 	/*
239 	 * For AR8132 fast ethernet controller, do not report 1000baseT
240 	 * capability to mii(4). Even though AR8132 uses the same
241 	 * model/revision number of F1 gigabit PHY, the PHY has no
242 	 * ability to establish 1000baseT link.
243 	 */
244 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
245 	    reg == MII_EXTSR)
246 		return (0);
247 
248 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
249 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
250 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
251 		DELAY(5);
252 		v = CSR_READ_4(sc, ALC_MDIO);
253 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
254 			break;
255 	}
256 
257 	if (i == 0) {
258 		device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
259 		return (0);
260 	}
261 
262 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
263 }
264 
265 static int
266 alc_miibus_writereg(device_t dev, int phy, int reg, int val)
267 {
268 	struct alc_softc *sc;
269 	uint32_t v;
270 	int i;
271 
272 	sc = device_get_softc(dev);
273 
274 	CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
275 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
276 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
277 	for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
278 		DELAY(5);
279 		v = CSR_READ_4(sc, ALC_MDIO);
280 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
281 			break;
282 	}
283 
284 	if (i == 0)
285 		device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
286 
287 	return (0);
288 }
289 
290 static void
291 alc_miibus_statchg(device_t dev)
292 {
293 	struct alc_softc *sc;
294 	struct mii_data *mii;
295 	struct ifnet *ifp;
296 	uint32_t reg;
297 
298 	sc = device_get_softc(dev);
299 
300 	mii = device_get_softc(sc->alc_miibus);
301 	ifp = sc->alc_ifp;
302 	if (mii == NULL || ifp == NULL ||
303 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
304 		return;
305 
306 	sc->alc_flags &= ~ALC_FLAG_LINK;
307 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
308 	    (IFM_ACTIVE | IFM_AVALID)) {
309 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
310 		case IFM_10_T:
311 		case IFM_100_TX:
312 			sc->alc_flags |= ALC_FLAG_LINK;
313 			break;
314 		case IFM_1000_T:
315 			if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
316 				sc->alc_flags |= ALC_FLAG_LINK;
317 			break;
318 		default:
319 			break;
320 		}
321 	}
322 	alc_stop_queue(sc);
323 	/* Stop Rx/Tx MACs. */
324 	alc_stop_mac(sc);
325 
326 	/* Program MACs with resolved speed/duplex/flow-control. */
327 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
328 		alc_start_queue(sc);
329 		alc_mac_config(sc);
330 		/* Re-enable Tx/Rx MACs. */
331 		reg = CSR_READ_4(sc, ALC_MAC_CFG);
332 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
333 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
334 		alc_aspm(sc, IFM_SUBTYPE(mii->mii_media_active));
335 	}
336 }
337 
338 static void
339 alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
340 {
341 	struct alc_softc *sc;
342 	struct mii_data *mii;
343 
344 	sc = ifp->if_softc;
345 	ALC_LOCK(sc);
346 	if ((ifp->if_flags & IFF_UP) == 0) {
347 		ALC_UNLOCK(sc);
348 		return;
349 	}
350 	mii = device_get_softc(sc->alc_miibus);
351 
352 	mii_pollstat(mii);
353 	ifmr->ifm_status = mii->mii_media_status;
354 	ifmr->ifm_active = mii->mii_media_active;
355 	ALC_UNLOCK(sc);
356 }
357 
358 static int
359 alc_mediachange(struct ifnet *ifp)
360 {
361 	struct alc_softc *sc;
362 	struct mii_data *mii;
363 	struct mii_softc *miisc;
364 	int error;
365 
366 	sc = ifp->if_softc;
367 	ALC_LOCK(sc);
368 	mii = device_get_softc(sc->alc_miibus);
369 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
370 		PHY_RESET(miisc);
371 	error = mii_mediachg(mii);
372 	ALC_UNLOCK(sc);
373 
374 	return (error);
375 }
376 
377 static struct alc_ident *
378 alc_find_ident(device_t dev)
379 {
380 	struct alc_ident *ident;
381 	uint16_t vendor, devid;
382 
383 	vendor = pci_get_vendor(dev);
384 	devid = pci_get_device(dev);
385 	for (ident = alc_ident_table; ident->name != NULL; ident++) {
386 		if (vendor == ident->vendorid && devid == ident->deviceid)
387 			return (ident);
388 	}
389 
390 	return (NULL);
391 }
392 
393 static int
394 alc_probe(device_t dev)
395 {
396 	struct alc_ident *ident;
397 
398 	ident = alc_find_ident(dev);
399 	if (ident != NULL) {
400 		device_set_desc(dev, ident->name);
401 		return (BUS_PROBE_DEFAULT);
402 	}
403 
404 	return (ENXIO);
405 }
406 
407 static void
408 alc_get_macaddr(struct alc_softc *sc)
409 {
410 	uint32_t ea[2], opt;
411 	uint16_t val;
412 	int eeprom, i;
413 
414 	eeprom = 0;
415 	opt = CSR_READ_4(sc, ALC_OPT_CFG);
416 	if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
417 	    (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
418 		/*
419 		 * EEPROM found, let TWSI reload EEPROM configuration.
420 		 * This will set ethernet address of controller.
421 		 */
422 		eeprom++;
423 		switch (sc->alc_ident->deviceid) {
424 		case DEVICEID_ATHEROS_AR8131:
425 		case DEVICEID_ATHEROS_AR8132:
426 			if ((opt & OPT_CFG_CLK_ENB) == 0) {
427 				opt |= OPT_CFG_CLK_ENB;
428 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
429 				CSR_READ_4(sc, ALC_OPT_CFG);
430 				DELAY(1000);
431 			}
432 			break;
433 		case DEVICEID_ATHEROS_AR8151:
434 		case DEVICEID_ATHEROS_AR8151_V2:
435 		case DEVICEID_ATHEROS_AR8152_B:
436 		case DEVICEID_ATHEROS_AR8152_B2:
437 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
438 			    ALC_MII_DBG_ADDR, 0x00);
439 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
440 			    ALC_MII_DBG_DATA);
441 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
442 			    ALC_MII_DBG_DATA, val & 0xFF7F);
443 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
444 			    ALC_MII_DBG_ADDR, 0x3B);
445 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
446 			    ALC_MII_DBG_DATA);
447 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
448 			    ALC_MII_DBG_DATA, val | 0x0008);
449 			DELAY(20);
450 			break;
451 		}
452 
453 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
454 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
455 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
456 		CSR_READ_4(sc, ALC_WOL_CFG);
457 
458 		CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
459 		    TWSI_CFG_SW_LD_START);
460 		for (i = 100; i > 0; i--) {
461 			DELAY(1000);
462 			if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
463 			    TWSI_CFG_SW_LD_START) == 0)
464 				break;
465 		}
466 		if (i == 0)
467 			device_printf(sc->alc_dev,
468 			    "reloading EEPROM timeout!\n");
469 	} else {
470 		if (bootverbose)
471 			device_printf(sc->alc_dev, "EEPROM not found!\n");
472 	}
473 	if (eeprom != 0) {
474 		switch (sc->alc_ident->deviceid) {
475 		case DEVICEID_ATHEROS_AR8131:
476 		case DEVICEID_ATHEROS_AR8132:
477 			if ((opt & OPT_CFG_CLK_ENB) != 0) {
478 				opt &= ~OPT_CFG_CLK_ENB;
479 				CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
480 				CSR_READ_4(sc, ALC_OPT_CFG);
481 				DELAY(1000);
482 			}
483 			break;
484 		case DEVICEID_ATHEROS_AR8151:
485 		case DEVICEID_ATHEROS_AR8151_V2:
486 		case DEVICEID_ATHEROS_AR8152_B:
487 		case DEVICEID_ATHEROS_AR8152_B2:
488 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
489 			    ALC_MII_DBG_ADDR, 0x00);
490 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
491 			    ALC_MII_DBG_DATA);
492 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
493 			    ALC_MII_DBG_DATA, val | 0x0080);
494 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
495 			    ALC_MII_DBG_ADDR, 0x3B);
496 			val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
497 			    ALC_MII_DBG_DATA);
498 			alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
499 			    ALC_MII_DBG_DATA, val & 0xFFF7);
500 			DELAY(20);
501 			break;
502 		}
503 	}
504 
505 	ea[0] = CSR_READ_4(sc, ALC_PAR0);
506 	ea[1] = CSR_READ_4(sc, ALC_PAR1);
507 	sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
508 	sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
509 	sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
510 	sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
511 	sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
512 	sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
513 }
514 
515 static void
516 alc_disable_l0s_l1(struct alc_softc *sc)
517 {
518 	uint32_t pmcfg;
519 
520 	/* Another magic from vendor. */
521 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
522 	pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
523 	    PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK |
524 	    PM_CFG_SERDES_PD_EX_L1);
525 	pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
526 	    PM_CFG_SERDES_L1_ENB;
527 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
528 }
529 
530 static void
531 alc_phy_reset(struct alc_softc *sc)
532 {
533 	uint16_t data;
534 
535 	/* Reset magic from Linux. */
536 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
537 	CSR_READ_2(sc, ALC_GPHY_CFG);
538 	DELAY(10 * 1000);
539 
540 	CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
541 	    GPHY_CFG_SEL_ANA_RESET);
542 	CSR_READ_2(sc, ALC_GPHY_CFG);
543 	DELAY(10 * 1000);
544 
545 	/* DSP fixup, Vendor magic. */
546 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
547 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
548 		    ALC_MII_DBG_ADDR, 0x000A);
549 		data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
550 		    ALC_MII_DBG_DATA);
551 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
552 		    ALC_MII_DBG_DATA, data & 0xDFFF);
553 	}
554 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
555 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
556 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
557 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
558 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
559 		    ALC_MII_DBG_ADDR, 0x003B);
560 		data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
561 		    ALC_MII_DBG_DATA);
562 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
563 		    ALC_MII_DBG_DATA, data & 0xFFF7);
564 		DELAY(20 * 1000);
565 	}
566 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151) {
567 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
568 		    ALC_MII_DBG_ADDR, 0x0029);
569 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
570 		    ALC_MII_DBG_DATA, 0x929D);
571 	}
572 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
573 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132 ||
574 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
575 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
576 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
577 		    ALC_MII_DBG_ADDR, 0x0029);
578 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
579 		    ALC_MII_DBG_DATA, 0xB6DD);
580 	}
581 
582 	/* Load DSP codes, vendor magic. */
583 	data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
584 	    ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
585 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
586 	    ALC_MII_DBG_ADDR, MII_ANA_CFG18);
587 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
588 	    ALC_MII_DBG_DATA, data);
589 
590 	data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
591 	    ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
592 	    ANA_SERDES_EN_LCKDT;
593 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
594 	    ALC_MII_DBG_ADDR, MII_ANA_CFG5);
595 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
596 	    ALC_MII_DBG_DATA, data);
597 
598 	data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
599 	    ANA_LONG_CABLE_TH_100_MASK) |
600 	    ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
601 	    ANA_SHORT_CABLE_TH_100_SHIFT) |
602 	    ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
603 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
604 	    ALC_MII_DBG_ADDR, MII_ANA_CFG54);
605 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
606 	    ALC_MII_DBG_DATA, data);
607 
608 	data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
609 	    ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
610 	    ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
611 	    ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
612 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
613 	    ALC_MII_DBG_ADDR, MII_ANA_CFG4);
614 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
615 	    ALC_MII_DBG_DATA, data);
616 
617 	data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
618 	    ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
619 	    ANA_OEN_125M;
620 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
621 	    ALC_MII_DBG_ADDR, MII_ANA_CFG0);
622 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
623 	    ALC_MII_DBG_DATA, data);
624 	DELAY(1000);
625 
626 	/* Disable hibernation. */
627 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
628 	    0x0029);
629 	data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
630 	    ALC_MII_DBG_DATA);
631 	data &= ~0x8000;
632 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
633 	    data);
634 
635 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
636 	    0x000B);
637 	data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
638 	    ALC_MII_DBG_DATA);
639 	data &= ~0x8000;
640 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
641 	    data);
642 }
643 
644 static void
645 alc_phy_down(struct alc_softc *sc)
646 {
647 
648 	switch (sc->alc_ident->deviceid) {
649 	case DEVICEID_ATHEROS_AR8151:
650 	case DEVICEID_ATHEROS_AR8151_V2:
651 		/*
652 		 * GPHY power down caused more problems on AR8151 v2.0.
653 		 * When driver is reloaded after GPHY power down,
654 		 * accesses to PHY/MAC registers hung the system. Only
655 		 * cold boot recovered from it.  I'm not sure whether
656 		 * AR8151 v1.0 also requires this one though.  I don't
657 		 * have AR8151 v1.0 controller in hand.
658 		 * The only option left is to isolate the PHY and
659 		 * initiates power down the PHY which in turn saves
660 		 * more power when driver is unloaded.
661 		 */
662 		alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
663 		    MII_BMCR, BMCR_ISO | BMCR_PDOWN);
664 		break;
665 	default:
666 		/* Force PHY down. */
667 		CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
668 		    GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
669 		    GPHY_CFG_PWDOWN_HW);
670 		DELAY(1000);
671 		break;
672 	}
673 }
674 
675 static void
676 alc_aspm(struct alc_softc *sc, int media)
677 {
678 	uint32_t pmcfg;
679 	uint16_t linkcfg;
680 
681 	ALC_LOCK_ASSERT(sc);
682 
683 	pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
684 	if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
685 	    (ALC_FLAG_APS | ALC_FLAG_PCIE))
686 		linkcfg = CSR_READ_2(sc, sc->alc_expcap +
687 		    PCIER_LINK_CTL);
688 	else
689 		linkcfg = 0;
690 	pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
691 	pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
692 	pmcfg |= PM_CFG_MAC_ASPM_CHK;
693 	pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
694 	pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
695 
696 	if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
697 		/* Disable extended sync except AR8152 B v1.0 */
698 		linkcfg &= ~PCIEM_LINK_CTL_EXTENDED_SYNC;
699 		if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
700 		    sc->alc_rev == ATHEROS_AR8152_B_V10)
701 			linkcfg |= PCIEM_LINK_CTL_EXTENDED_SYNC;
702 		CSR_WRITE_2(sc, sc->alc_expcap + PCIER_LINK_CTL,
703 		    linkcfg);
704 		pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
705 		    PM_CFG_HOTRST);
706 		pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
707 		    PM_CFG_L1_ENTRY_TIMER_SHIFT);
708 		pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
709 		pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
710 		    PM_CFG_PM_REQ_TIMER_SHIFT);
711 		pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
712 	}
713 
714 	if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
715 		if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
716 			pmcfg |= PM_CFG_ASPM_L0S_ENB;
717 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
718 			pmcfg |= PM_CFG_ASPM_L1_ENB;
719 		if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
720 			if (sc->alc_ident->deviceid ==
721 			    DEVICEID_ATHEROS_AR8152_B)
722 				pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
723 			pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
724 			    PM_CFG_SERDES_PLL_L1_ENB |
725 			    PM_CFG_SERDES_BUDS_RX_L1_ENB);
726 			pmcfg |= PM_CFG_CLK_SWH_L1;
727 			if (media == IFM_100_TX || media == IFM_1000_T) {
728 				pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
729 				switch (sc->alc_ident->deviceid) {
730 				case DEVICEID_ATHEROS_AR8152_B:
731 					pmcfg |= (7 <<
732 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
733 					break;
734 				case DEVICEID_ATHEROS_AR8152_B2:
735 				case DEVICEID_ATHEROS_AR8151_V2:
736 					pmcfg |= (4 <<
737 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
738 					break;
739 				default:
740 					pmcfg |= (15 <<
741 					    PM_CFG_L1_ENTRY_TIMER_SHIFT);
742 					break;
743 				}
744 			}
745 		} else {
746 			pmcfg |= PM_CFG_SERDES_L1_ENB |
747 			    PM_CFG_SERDES_PLL_L1_ENB |
748 			    PM_CFG_SERDES_BUDS_RX_L1_ENB;
749 			pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
750 			    PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
751 		}
752 	} else {
753 		pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
754 		    PM_CFG_SERDES_PLL_L1_ENB);
755 		pmcfg |= PM_CFG_CLK_SWH_L1;
756 		if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
757 			pmcfg |= PM_CFG_ASPM_L1_ENB;
758 	}
759 	CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
760 }
761 
762 static int
763 alc_attach(device_t dev)
764 {
765 	struct alc_softc *sc;
766 	struct ifnet *ifp;
767 	char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
768 	uint16_t burst;
769 	int base, error, i, msic, msixc, state;
770 	uint32_t cap, ctl, val;
771 
772 	error = 0;
773 	sc = device_get_softc(dev);
774 	sc->alc_dev = dev;
775 
776 	mtx_init(&sc->alc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
777 	    MTX_DEF);
778 	callout_init_mtx(&sc->alc_tick_ch, &sc->alc_mtx, 0);
779 	TASK_INIT(&sc->alc_int_task, 0, alc_int_task, sc);
780 	sc->alc_ident = alc_find_ident(dev);
781 
782 	/* Map the device. */
783 	pci_enable_busmaster(dev);
784 	sc->alc_res_spec = alc_res_spec_mem;
785 	sc->alc_irq_spec = alc_irq_spec_legacy;
786 	error = bus_alloc_resources(dev, sc->alc_res_spec, sc->alc_res);
787 	if (error != 0) {
788 		device_printf(dev, "cannot allocate memory resources.\n");
789 		goto fail;
790 	}
791 
792 	/* Set PHY address. */
793 	sc->alc_phyaddr = ALC_PHY_ADDR;
794 
795 	/* Initialize DMA parameters. */
796 	sc->alc_dma_rd_burst = 0;
797 	sc->alc_dma_wr_burst = 0;
798 	sc->alc_rcb = DMA_CFG_RCB_64;
799 	if (pci_find_cap(dev, PCIY_EXPRESS, &base) == 0) {
800 		sc->alc_flags |= ALC_FLAG_PCIE;
801 		sc->alc_expcap = base;
802 		burst = CSR_READ_2(sc, base + PCIER_DEVICE_CTL);
803 		sc->alc_dma_rd_burst =
804 		    (burst & PCIEM_CTL_MAX_READ_REQUEST) >> 12;
805 		sc->alc_dma_wr_burst = (burst & PCIEM_CTL_MAX_PAYLOAD) >> 5;
806 		if (bootverbose) {
807 			device_printf(dev, "Read request size : %u bytes.\n",
808 			    alc_dma_burst[sc->alc_dma_rd_burst]);
809 			device_printf(dev, "TLP payload size : %u bytes.\n",
810 			    alc_dma_burst[sc->alc_dma_wr_burst]);
811 		}
812 		if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
813 			sc->alc_dma_rd_burst = 3;
814 		if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
815 			sc->alc_dma_wr_burst = 3;
816 		/* Clear data link and flow-control protocol error. */
817 		val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
818 		val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
819 		CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
820 		CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
821 		    CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
822 		CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
823 		    CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
824 		    PCIE_PHYMISC_FORCE_RCV_DET);
825 		if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
826 		    pci_get_revid(dev) == ATHEROS_AR8152_B_V10) {
827 			val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
828 			val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
829 			    PCIE_PHYMISC2_SERDES_TH_MASK);
830 			val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
831 			val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
832 			CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
833 		}
834 		/* Disable ASPM L0S and L1. */
835 		cap = CSR_READ_2(sc, base + PCIER_LINK_CAP);
836 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
837 			ctl = CSR_READ_2(sc, base + PCIER_LINK_CTL);
838 			if ((ctl & PCIEM_LINK_CTL_RCB) != 0)
839 				sc->alc_rcb = DMA_CFG_RCB_128;
840 			if (bootverbose)
841 				device_printf(dev, "RCB %u bytes\n",
842 				    sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
843 			state = ctl & PCIEM_LINK_CTL_ASPMC;
844 			if (state & PCIEM_LINK_CTL_ASPMC_L0S)
845 				sc->alc_flags |= ALC_FLAG_L0S;
846 			if (state & PCIEM_LINK_CTL_ASPMC_L1)
847 				sc->alc_flags |= ALC_FLAG_L1S;
848 			if (bootverbose)
849 				device_printf(sc->alc_dev, "ASPM %s %s\n",
850 				    aspm_state[state],
851 				    state == 0 ? "disabled" : "enabled");
852 			alc_disable_l0s_l1(sc);
853 		} else {
854 			if (bootverbose)
855 				device_printf(sc->alc_dev,
856 				    "no ASPM support\n");
857 		}
858 	}
859 
860 	/* Reset PHY. */
861 	alc_phy_reset(sc);
862 
863 	/* Reset the ethernet controller. */
864 	alc_reset(sc);
865 
866 	/*
867 	 * One odd thing is AR8132 uses the same PHY hardware(F1
868 	 * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
869 	 * the PHY supports 1000Mbps but that's not true. The PHY
870 	 * used in AR8132 can't establish gigabit link even if it
871 	 * shows the same PHY model/revision number of AR8131.
872 	 */
873 	switch (sc->alc_ident->deviceid) {
874 	case DEVICEID_ATHEROS_AR8152_B:
875 	case DEVICEID_ATHEROS_AR8152_B2:
876 		sc->alc_flags |= ALC_FLAG_APS;
877 		/* FALLTHROUGH */
878 	case DEVICEID_ATHEROS_AR8132:
879 		sc->alc_flags |= ALC_FLAG_FASTETHER;
880 		break;
881 	case DEVICEID_ATHEROS_AR8151:
882 	case DEVICEID_ATHEROS_AR8151_V2:
883 		sc->alc_flags |= ALC_FLAG_APS;
884 		/* FALLTHROUGH */
885 	default:
886 		break;
887 	}
888 	sc->alc_flags |= ALC_FLAG_ASPM_MON | ALC_FLAG_JUMBO;
889 
890 	/*
891 	 * It seems that AR813x/AR815x has silicon bug for SMB. In
892 	 * addition, Atheros said that enabling SMB wouldn't improve
893 	 * performance. However I think it's bad to access lots of
894 	 * registers to extract MAC statistics.
895 	 */
896 	sc->alc_flags |= ALC_FLAG_SMB_BUG;
897 	/*
898 	 * Don't use Tx CMB. It is known to have silicon bug.
899 	 */
900 	sc->alc_flags |= ALC_FLAG_CMB_BUG;
901 	sc->alc_rev = pci_get_revid(dev);
902 	sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
903 	    MASTER_CHIP_REV_SHIFT;
904 	if (bootverbose) {
905 		device_printf(dev, "PCI device revision : 0x%04x\n",
906 		    sc->alc_rev);
907 		device_printf(dev, "Chip id/revision : 0x%04x\n",
908 		    sc->alc_chip_rev);
909 	}
910 	device_printf(dev, "%u Tx FIFO, %u Rx FIFO\n",
911 	    CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
912 	    CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
913 
914 	/* Allocate IRQ resources. */
915 	msixc = pci_msix_count(dev);
916 	msic = pci_msi_count(dev);
917 	if (bootverbose) {
918 		device_printf(dev, "MSIX count : %d\n", msixc);
919 		device_printf(dev, "MSI count : %d\n", msic);
920 	}
921 	/* Prefer MSIX over MSI. */
922 	if (msix_disable == 0 || msi_disable == 0) {
923 		if (msix_disable == 0 && msixc == ALC_MSIX_MESSAGES &&
924 		    pci_alloc_msix(dev, &msixc) == 0) {
925 			if (msic == ALC_MSIX_MESSAGES) {
926 				device_printf(dev,
927 				    "Using %d MSIX message(s).\n", msixc);
928 				sc->alc_flags |= ALC_FLAG_MSIX;
929 				sc->alc_irq_spec = alc_irq_spec_msix;
930 			} else
931 				pci_release_msi(dev);
932 		}
933 		if (msi_disable == 0 && (sc->alc_flags & ALC_FLAG_MSIX) == 0 &&
934 		    msic == ALC_MSI_MESSAGES &&
935 		    pci_alloc_msi(dev, &msic) == 0) {
936 			if (msic == ALC_MSI_MESSAGES) {
937 				device_printf(dev,
938 				    "Using %d MSI message(s).\n", msic);
939 				sc->alc_flags |= ALC_FLAG_MSI;
940 				sc->alc_irq_spec = alc_irq_spec_msi;
941 			} else
942 				pci_release_msi(dev);
943 		}
944 	}
945 
946 	error = bus_alloc_resources(dev, sc->alc_irq_spec, sc->alc_irq);
947 	if (error != 0) {
948 		device_printf(dev, "cannot allocate IRQ resources.\n");
949 		goto fail;
950 	}
951 
952 	/* Create device sysctl node. */
953 	alc_sysctl_node(sc);
954 
955 	if ((error = alc_dma_alloc(sc) != 0))
956 		goto fail;
957 
958 	/* Load station address. */
959 	alc_get_macaddr(sc);
960 
961 	ifp = sc->alc_ifp = if_alloc(IFT_ETHER);
962 	if (ifp == NULL) {
963 		device_printf(dev, "cannot allocate ifnet structure.\n");
964 		error = ENXIO;
965 		goto fail;
966 	}
967 
968 	ifp->if_softc = sc;
969 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
970 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
971 	ifp->if_ioctl = alc_ioctl;
972 	ifp->if_start = alc_start;
973 	ifp->if_init = alc_init;
974 	ifp->if_snd.ifq_drv_maxlen = ALC_TX_RING_CNT - 1;
975 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
976 	IFQ_SET_READY(&ifp->if_snd);
977 	ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO4;
978 	ifp->if_hwassist = ALC_CSUM_FEATURES | CSUM_TSO;
979 	if (pci_find_cap(dev, PCIY_PMG, &base) == 0) {
980 		ifp->if_capabilities |= IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST;
981 		sc->alc_flags |= ALC_FLAG_PM;
982 		sc->alc_pmcap = base;
983 	}
984 	ifp->if_capenable = ifp->if_capabilities;
985 
986 	/* Set up MII bus. */
987 	error = mii_attach(dev, &sc->alc_miibus, ifp, alc_mediachange,
988 	    alc_mediastatus, BMSR_DEFCAPMASK, sc->alc_phyaddr, MII_OFFSET_ANY,
989 	    MIIF_DOPAUSE);
990 	if (error != 0) {
991 		device_printf(dev, "attaching PHYs failed\n");
992 		goto fail;
993 	}
994 
995 	ether_ifattach(ifp, sc->alc_eaddr);
996 
997 	/* VLAN capability setup. */
998 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
999 	    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
1000 	ifp->if_capenable = ifp->if_capabilities;
1001 	/*
1002 	 * XXX
1003 	 * It seems enabling Tx checksum offloading makes more trouble.
1004 	 * Sometimes the controller does not receive any frames when
1005 	 * Tx checksum offloading is enabled. I'm not sure whether this
1006 	 * is a bug in Tx checksum offloading logic or I got broken
1007 	 * sample boards. To safety, don't enable Tx checksum offloading
1008 	 * by default but give chance to users to toggle it if they know
1009 	 * their controllers work without problems.
1010 	 */
1011 	ifp->if_capenable &= ~IFCAP_TXCSUM;
1012 	ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
1013 
1014 	/* Tell the upper layer(s) we support long frames. */
1015 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1016 
1017 	/* Create local taskq. */
1018 	sc->alc_tq = taskqueue_create_fast("alc_taskq", M_WAITOK,
1019 	    taskqueue_thread_enqueue, &sc->alc_tq);
1020 	if (sc->alc_tq == NULL) {
1021 		device_printf(dev, "could not create taskqueue.\n");
1022 		ether_ifdetach(ifp);
1023 		error = ENXIO;
1024 		goto fail;
1025 	}
1026 	taskqueue_start_threads(&sc->alc_tq, 1, PI_NET, "%s taskq",
1027 	    device_get_nameunit(sc->alc_dev));
1028 
1029 	if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1030 		msic = ALC_MSIX_MESSAGES;
1031 	else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1032 		msic = ALC_MSI_MESSAGES;
1033 	else
1034 		msic = 1;
1035 	for (i = 0; i < msic; i++) {
1036 		error = bus_setup_intr(dev, sc->alc_irq[i],
1037 		    INTR_TYPE_NET | INTR_MPSAFE, alc_intr, NULL, sc,
1038 		    &sc->alc_intrhand[i]);
1039 		if (error != 0)
1040 			break;
1041 	}
1042 	if (error != 0) {
1043 		device_printf(dev, "could not set up interrupt handler.\n");
1044 		taskqueue_free(sc->alc_tq);
1045 		sc->alc_tq = NULL;
1046 		ether_ifdetach(ifp);
1047 		goto fail;
1048 	}
1049 
1050 fail:
1051 	if (error != 0)
1052 		alc_detach(dev);
1053 
1054 	return (error);
1055 }
1056 
1057 static int
1058 alc_detach(device_t dev)
1059 {
1060 	struct alc_softc *sc;
1061 	struct ifnet *ifp;
1062 	int i, msic;
1063 
1064 	sc = device_get_softc(dev);
1065 
1066 	ifp = sc->alc_ifp;
1067 	if (device_is_attached(dev)) {
1068 		ether_ifdetach(ifp);
1069 		ALC_LOCK(sc);
1070 		alc_stop(sc);
1071 		ALC_UNLOCK(sc);
1072 		callout_drain(&sc->alc_tick_ch);
1073 		taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1074 	}
1075 
1076 	if (sc->alc_tq != NULL) {
1077 		taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1078 		taskqueue_free(sc->alc_tq);
1079 		sc->alc_tq = NULL;
1080 	}
1081 
1082 	if (sc->alc_miibus != NULL) {
1083 		device_delete_child(dev, sc->alc_miibus);
1084 		sc->alc_miibus = NULL;
1085 	}
1086 	bus_generic_detach(dev);
1087 	alc_dma_free(sc);
1088 
1089 	if (ifp != NULL) {
1090 		if_free(ifp);
1091 		sc->alc_ifp = NULL;
1092 	}
1093 
1094 	if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1095 		msic = ALC_MSIX_MESSAGES;
1096 	else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1097 		msic = ALC_MSI_MESSAGES;
1098 	else
1099 		msic = 1;
1100 	for (i = 0; i < msic; i++) {
1101 		if (sc->alc_intrhand[i] != NULL) {
1102 			bus_teardown_intr(dev, sc->alc_irq[i],
1103 			    sc->alc_intrhand[i]);
1104 			sc->alc_intrhand[i] = NULL;
1105 		}
1106 	}
1107 	if (sc->alc_res[0] != NULL)
1108 		alc_phy_down(sc);
1109 	bus_release_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1110 	if ((sc->alc_flags & (ALC_FLAG_MSI | ALC_FLAG_MSIX)) != 0)
1111 		pci_release_msi(dev);
1112 	bus_release_resources(dev, sc->alc_res_spec, sc->alc_res);
1113 	mtx_destroy(&sc->alc_mtx);
1114 
1115 	return (0);
1116 }
1117 
1118 #define	ALC_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
1119 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
1120 #define	ALC_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
1121 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
1122 
1123 static void
1124 alc_sysctl_node(struct alc_softc *sc)
1125 {
1126 	struct sysctl_ctx_list *ctx;
1127 	struct sysctl_oid_list *child, *parent;
1128 	struct sysctl_oid *tree;
1129 	struct alc_hw_stats *stats;
1130 	int error;
1131 
1132 	stats = &sc->alc_stats;
1133 	ctx = device_get_sysctl_ctx(sc->alc_dev);
1134 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->alc_dev));
1135 
1136 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_rx_mod",
1137 	    CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_rx_mod, 0,
1138 	    sysctl_hw_alc_int_mod, "I", "alc Rx interrupt moderation");
1139 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_tx_mod",
1140 	    CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_tx_mod, 0,
1141 	    sysctl_hw_alc_int_mod, "I", "alc Tx interrupt moderation");
1142 	/* Pull in device tunables. */
1143 	sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1144 	error = resource_int_value(device_get_name(sc->alc_dev),
1145 	    device_get_unit(sc->alc_dev), "int_rx_mod", &sc->alc_int_rx_mod);
1146 	if (error == 0) {
1147 		if (sc->alc_int_rx_mod < ALC_IM_TIMER_MIN ||
1148 		    sc->alc_int_rx_mod > ALC_IM_TIMER_MAX) {
1149 			device_printf(sc->alc_dev, "int_rx_mod value out of "
1150 			    "range; using default: %d\n",
1151 			    ALC_IM_RX_TIMER_DEFAULT);
1152 			sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1153 		}
1154 	}
1155 	sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1156 	error = resource_int_value(device_get_name(sc->alc_dev),
1157 	    device_get_unit(sc->alc_dev), "int_tx_mod", &sc->alc_int_tx_mod);
1158 	if (error == 0) {
1159 		if (sc->alc_int_tx_mod < ALC_IM_TIMER_MIN ||
1160 		    sc->alc_int_tx_mod > ALC_IM_TIMER_MAX) {
1161 			device_printf(sc->alc_dev, "int_tx_mod value out of "
1162 			    "range; using default: %d\n",
1163 			    ALC_IM_TX_TIMER_DEFAULT);
1164 			sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1165 		}
1166 	}
1167 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
1168 	    CTLTYPE_INT | CTLFLAG_RW, &sc->alc_process_limit, 0,
1169 	    sysctl_hw_alc_proc_limit, "I",
1170 	    "max number of Rx events to process");
1171 	/* Pull in device tunables. */
1172 	sc->alc_process_limit = ALC_PROC_DEFAULT;
1173 	error = resource_int_value(device_get_name(sc->alc_dev),
1174 	    device_get_unit(sc->alc_dev), "process_limit",
1175 	    &sc->alc_process_limit);
1176 	if (error == 0) {
1177 		if (sc->alc_process_limit < ALC_PROC_MIN ||
1178 		    sc->alc_process_limit > ALC_PROC_MAX) {
1179 			device_printf(sc->alc_dev,
1180 			    "process_limit value out of range; "
1181 			    "using default: %d\n", ALC_PROC_DEFAULT);
1182 			sc->alc_process_limit = ALC_PROC_DEFAULT;
1183 		}
1184 	}
1185 
1186 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
1187 	    NULL, "ALC statistics");
1188 	parent = SYSCTL_CHILDREN(tree);
1189 
1190 	/* Rx statistics. */
1191 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
1192 	    NULL, "Rx MAC statistics");
1193 	child = SYSCTL_CHILDREN(tree);
1194 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1195 	    &stats->rx_frames, "Good frames");
1196 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1197 	    &stats->rx_bcast_frames, "Good broadcast frames");
1198 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1199 	    &stats->rx_mcast_frames, "Good multicast frames");
1200 	ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1201 	    &stats->rx_pause_frames, "Pause control frames");
1202 	ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1203 	    &stats->rx_control_frames, "Control frames");
1204 	ALC_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
1205 	    &stats->rx_crcerrs, "CRC errors");
1206 	ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1207 	    &stats->rx_lenerrs, "Frames with length mismatched");
1208 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1209 	    &stats->rx_bytes, "Good octets");
1210 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1211 	    &stats->rx_bcast_bytes, "Good broadcast octets");
1212 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1213 	    &stats->rx_mcast_bytes, "Good multicast octets");
1214 	ALC_SYSCTL_STAT_ADD32(ctx, child, "runts",
1215 	    &stats->rx_runts, "Too short frames");
1216 	ALC_SYSCTL_STAT_ADD32(ctx, child, "fragments",
1217 	    &stats->rx_fragments, "Fragmented frames");
1218 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1219 	    &stats->rx_pkts_64, "64 bytes frames");
1220 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1221 	    &stats->rx_pkts_65_127, "65 to 127 bytes frames");
1222 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1223 	    &stats->rx_pkts_128_255, "128 to 255 bytes frames");
1224 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1225 	    &stats->rx_pkts_256_511, "256 to 511 bytes frames");
1226 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1227 	    &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
1228 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1229 	    &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
1230 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1231 	    &stats->rx_pkts_1519_max, "1519 to max frames");
1232 	ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1233 	    &stats->rx_pkts_truncated, "Truncated frames due to MTU size");
1234 	ALC_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
1235 	    &stats->rx_fifo_oflows, "FIFO overflows");
1236 	ALC_SYSCTL_STAT_ADD32(ctx, child, "rrs_errs",
1237 	    &stats->rx_rrs_errs, "Return status write-back errors");
1238 	ALC_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
1239 	    &stats->rx_alignerrs, "Alignment errors");
1240 	ALC_SYSCTL_STAT_ADD32(ctx, child, "filtered",
1241 	    &stats->rx_pkts_filtered,
1242 	    "Frames dropped due to address filtering");
1243 
1244 	/* Tx statistics. */
1245 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
1246 	    NULL, "Tx MAC statistics");
1247 	child = SYSCTL_CHILDREN(tree);
1248 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1249 	    &stats->tx_frames, "Good frames");
1250 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1251 	    &stats->tx_bcast_frames, "Good broadcast frames");
1252 	ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1253 	    &stats->tx_mcast_frames, "Good multicast frames");
1254 	ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1255 	    &stats->tx_pause_frames, "Pause control frames");
1256 	ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1257 	    &stats->tx_control_frames, "Control frames");
1258 	ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
1259 	    &stats->tx_excess_defer, "Frames with excessive derferrals");
1260 	ALC_SYSCTL_STAT_ADD32(ctx, child, "defers",
1261 	    &stats->tx_excess_defer, "Frames with derferrals");
1262 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1263 	    &stats->tx_bytes, "Good octets");
1264 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1265 	    &stats->tx_bcast_bytes, "Good broadcast octets");
1266 	ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1267 	    &stats->tx_mcast_bytes, "Good multicast octets");
1268 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1269 	    &stats->tx_pkts_64, "64 bytes frames");
1270 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1271 	    &stats->tx_pkts_65_127, "65 to 127 bytes frames");
1272 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1273 	    &stats->tx_pkts_128_255, "128 to 255 bytes frames");
1274 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1275 	    &stats->tx_pkts_256_511, "256 to 511 bytes frames");
1276 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1277 	    &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
1278 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1279 	    &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
1280 	ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1281 	    &stats->tx_pkts_1519_max, "1519 to max frames");
1282 	ALC_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
1283 	    &stats->tx_single_colls, "Single collisions");
1284 	ALC_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
1285 	    &stats->tx_multi_colls, "Multiple collisions");
1286 	ALC_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
1287 	    &stats->tx_late_colls, "Late collisions");
1288 	ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_colls",
1289 	    &stats->tx_excess_colls, "Excessive collisions");
1290 	ALC_SYSCTL_STAT_ADD32(ctx, child, "abort",
1291 	    &stats->tx_abort, "Aborted frames due to Excessive collisions");
1292 	ALC_SYSCTL_STAT_ADD32(ctx, child, "underruns",
1293 	    &stats->tx_underrun, "FIFO underruns");
1294 	ALC_SYSCTL_STAT_ADD32(ctx, child, "desc_underruns",
1295 	    &stats->tx_desc_underrun, "Descriptor write-back errors");
1296 	ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1297 	    &stats->tx_lenerrs, "Frames with length mismatched");
1298 	ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1299 	    &stats->tx_pkts_truncated, "Truncated frames due to MTU size");
1300 }
1301 
1302 #undef ALC_SYSCTL_STAT_ADD32
1303 #undef ALC_SYSCTL_STAT_ADD64
1304 
1305 struct alc_dmamap_arg {
1306 	bus_addr_t	alc_busaddr;
1307 };
1308 
1309 static void
1310 alc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1311 {
1312 	struct alc_dmamap_arg *ctx;
1313 
1314 	if (error != 0)
1315 		return;
1316 
1317 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1318 
1319 	ctx = (struct alc_dmamap_arg *)arg;
1320 	ctx->alc_busaddr = segs[0].ds_addr;
1321 }
1322 
1323 /*
1324  * Normal and high Tx descriptors shares single Tx high address.
1325  * Four Rx descriptor/return rings and CMB shares the same Rx
1326  * high address.
1327  */
1328 static int
1329 alc_check_boundary(struct alc_softc *sc)
1330 {
1331 	bus_addr_t cmb_end, rx_ring_end, rr_ring_end, tx_ring_end;
1332 
1333 	rx_ring_end = sc->alc_rdata.alc_rx_ring_paddr + ALC_RX_RING_SZ;
1334 	rr_ring_end = sc->alc_rdata.alc_rr_ring_paddr + ALC_RR_RING_SZ;
1335 	cmb_end = sc->alc_rdata.alc_cmb_paddr + ALC_CMB_SZ;
1336 	tx_ring_end = sc->alc_rdata.alc_tx_ring_paddr + ALC_TX_RING_SZ;
1337 
1338 	/* 4GB boundary crossing is not allowed. */
1339 	if ((ALC_ADDR_HI(rx_ring_end) !=
1340 	    ALC_ADDR_HI(sc->alc_rdata.alc_rx_ring_paddr)) ||
1341 	    (ALC_ADDR_HI(rr_ring_end) !=
1342 	    ALC_ADDR_HI(sc->alc_rdata.alc_rr_ring_paddr)) ||
1343 	    (ALC_ADDR_HI(cmb_end) !=
1344 	    ALC_ADDR_HI(sc->alc_rdata.alc_cmb_paddr)) ||
1345 	    (ALC_ADDR_HI(tx_ring_end) !=
1346 	    ALC_ADDR_HI(sc->alc_rdata.alc_tx_ring_paddr)))
1347 		return (EFBIG);
1348 	/*
1349 	 * Make sure Rx return descriptor/Rx descriptor/CMB use
1350 	 * the same high address.
1351 	 */
1352 	if ((ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(rr_ring_end)) ||
1353 	    (ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(cmb_end)))
1354 		return (EFBIG);
1355 
1356 	return (0);
1357 }
1358 
1359 static int
1360 alc_dma_alloc(struct alc_softc *sc)
1361 {
1362 	struct alc_txdesc *txd;
1363 	struct alc_rxdesc *rxd;
1364 	bus_addr_t lowaddr;
1365 	struct alc_dmamap_arg ctx;
1366 	int error, i;
1367 
1368 	lowaddr = BUS_SPACE_MAXADDR;
1369 again:
1370 	/* Create parent DMA tag. */
1371 	error = bus_dma_tag_create(
1372 	    bus_get_dma_tag(sc->alc_dev), /* parent */
1373 	    1, 0,			/* alignment, boundary */
1374 	    lowaddr,			/* lowaddr */
1375 	    BUS_SPACE_MAXADDR,		/* highaddr */
1376 	    NULL, NULL,			/* filter, filterarg */
1377 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1378 	    0,				/* nsegments */
1379 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1380 	    0,				/* flags */
1381 	    NULL, NULL,			/* lockfunc, lockarg */
1382 	    &sc->alc_cdata.alc_parent_tag);
1383 	if (error != 0) {
1384 		device_printf(sc->alc_dev,
1385 		    "could not create parent DMA tag.\n");
1386 		goto fail;
1387 	}
1388 
1389 	/* Create DMA tag for Tx descriptor ring. */
1390 	error = bus_dma_tag_create(
1391 	    sc->alc_cdata.alc_parent_tag, /* parent */
1392 	    ALC_TX_RING_ALIGN, 0,	/* alignment, boundary */
1393 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1394 	    BUS_SPACE_MAXADDR,		/* highaddr */
1395 	    NULL, NULL,			/* filter, filterarg */
1396 	    ALC_TX_RING_SZ,		/* maxsize */
1397 	    1,				/* nsegments */
1398 	    ALC_TX_RING_SZ,		/* maxsegsize */
1399 	    0,				/* flags */
1400 	    NULL, NULL,			/* lockfunc, lockarg */
1401 	    &sc->alc_cdata.alc_tx_ring_tag);
1402 	if (error != 0) {
1403 		device_printf(sc->alc_dev,
1404 		    "could not create Tx ring DMA tag.\n");
1405 		goto fail;
1406 	}
1407 
1408 	/* Create DMA tag for Rx free descriptor ring. */
1409 	error = bus_dma_tag_create(
1410 	    sc->alc_cdata.alc_parent_tag, /* parent */
1411 	    ALC_RX_RING_ALIGN, 0,	/* alignment, boundary */
1412 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1413 	    BUS_SPACE_MAXADDR,		/* highaddr */
1414 	    NULL, NULL,			/* filter, filterarg */
1415 	    ALC_RX_RING_SZ,		/* maxsize */
1416 	    1,				/* nsegments */
1417 	    ALC_RX_RING_SZ,		/* maxsegsize */
1418 	    0,				/* flags */
1419 	    NULL, NULL,			/* lockfunc, lockarg */
1420 	    &sc->alc_cdata.alc_rx_ring_tag);
1421 	if (error != 0) {
1422 		device_printf(sc->alc_dev,
1423 		    "could not create Rx ring DMA tag.\n");
1424 		goto fail;
1425 	}
1426 	/* Create DMA tag for Rx return descriptor ring. */
1427 	error = bus_dma_tag_create(
1428 	    sc->alc_cdata.alc_parent_tag, /* parent */
1429 	    ALC_RR_RING_ALIGN, 0,	/* alignment, boundary */
1430 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1431 	    BUS_SPACE_MAXADDR,		/* highaddr */
1432 	    NULL, NULL,			/* filter, filterarg */
1433 	    ALC_RR_RING_SZ,		/* maxsize */
1434 	    1,				/* nsegments */
1435 	    ALC_RR_RING_SZ,		/* maxsegsize */
1436 	    0,				/* flags */
1437 	    NULL, NULL,			/* lockfunc, lockarg */
1438 	    &sc->alc_cdata.alc_rr_ring_tag);
1439 	if (error != 0) {
1440 		device_printf(sc->alc_dev,
1441 		    "could not create Rx return ring DMA tag.\n");
1442 		goto fail;
1443 	}
1444 
1445 	/* Create DMA tag for coalescing message block. */
1446 	error = bus_dma_tag_create(
1447 	    sc->alc_cdata.alc_parent_tag, /* parent */
1448 	    ALC_CMB_ALIGN, 0,		/* alignment, boundary */
1449 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1450 	    BUS_SPACE_MAXADDR,		/* highaddr */
1451 	    NULL, NULL,			/* filter, filterarg */
1452 	    ALC_CMB_SZ,			/* maxsize */
1453 	    1,				/* nsegments */
1454 	    ALC_CMB_SZ,			/* maxsegsize */
1455 	    0,				/* flags */
1456 	    NULL, NULL,			/* lockfunc, lockarg */
1457 	    &sc->alc_cdata.alc_cmb_tag);
1458 	if (error != 0) {
1459 		device_printf(sc->alc_dev,
1460 		    "could not create CMB DMA tag.\n");
1461 		goto fail;
1462 	}
1463 	/* Create DMA tag for status message block. */
1464 	error = bus_dma_tag_create(
1465 	    sc->alc_cdata.alc_parent_tag, /* parent */
1466 	    ALC_SMB_ALIGN, 0,		/* alignment, boundary */
1467 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1468 	    BUS_SPACE_MAXADDR,		/* highaddr */
1469 	    NULL, NULL,			/* filter, filterarg */
1470 	    ALC_SMB_SZ,			/* maxsize */
1471 	    1,				/* nsegments */
1472 	    ALC_SMB_SZ,			/* maxsegsize */
1473 	    0,				/* flags */
1474 	    NULL, NULL,			/* lockfunc, lockarg */
1475 	    &sc->alc_cdata.alc_smb_tag);
1476 	if (error != 0) {
1477 		device_printf(sc->alc_dev,
1478 		    "could not create SMB DMA tag.\n");
1479 		goto fail;
1480 	}
1481 
1482 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
1483 	error = bus_dmamem_alloc(sc->alc_cdata.alc_tx_ring_tag,
1484 	    (void **)&sc->alc_rdata.alc_tx_ring,
1485 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1486 	    &sc->alc_cdata.alc_tx_ring_map);
1487 	if (error != 0) {
1488 		device_printf(sc->alc_dev,
1489 		    "could not allocate DMA'able memory for Tx ring.\n");
1490 		goto fail;
1491 	}
1492 	ctx.alc_busaddr = 0;
1493 	error = bus_dmamap_load(sc->alc_cdata.alc_tx_ring_tag,
1494 	    sc->alc_cdata.alc_tx_ring_map, sc->alc_rdata.alc_tx_ring,
1495 	    ALC_TX_RING_SZ, alc_dmamap_cb, &ctx, 0);
1496 	if (error != 0 || ctx.alc_busaddr == 0) {
1497 		device_printf(sc->alc_dev,
1498 		    "could not load DMA'able memory for Tx ring.\n");
1499 		goto fail;
1500 	}
1501 	sc->alc_rdata.alc_tx_ring_paddr = ctx.alc_busaddr;
1502 
1503 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
1504 	error = bus_dmamem_alloc(sc->alc_cdata.alc_rx_ring_tag,
1505 	    (void **)&sc->alc_rdata.alc_rx_ring,
1506 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1507 	    &sc->alc_cdata.alc_rx_ring_map);
1508 	if (error != 0) {
1509 		device_printf(sc->alc_dev,
1510 		    "could not allocate DMA'able memory for Rx ring.\n");
1511 		goto fail;
1512 	}
1513 	ctx.alc_busaddr = 0;
1514 	error = bus_dmamap_load(sc->alc_cdata.alc_rx_ring_tag,
1515 	    sc->alc_cdata.alc_rx_ring_map, sc->alc_rdata.alc_rx_ring,
1516 	    ALC_RX_RING_SZ, alc_dmamap_cb, &ctx, 0);
1517 	if (error != 0 || ctx.alc_busaddr == 0) {
1518 		device_printf(sc->alc_dev,
1519 		    "could not load DMA'able memory for Rx ring.\n");
1520 		goto fail;
1521 	}
1522 	sc->alc_rdata.alc_rx_ring_paddr = ctx.alc_busaddr;
1523 
1524 	/* Allocate DMA'able memory and load the DMA map for Rx return ring. */
1525 	error = bus_dmamem_alloc(sc->alc_cdata.alc_rr_ring_tag,
1526 	    (void **)&sc->alc_rdata.alc_rr_ring,
1527 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1528 	    &sc->alc_cdata.alc_rr_ring_map);
1529 	if (error != 0) {
1530 		device_printf(sc->alc_dev,
1531 		    "could not allocate DMA'able memory for Rx return ring.\n");
1532 		goto fail;
1533 	}
1534 	ctx.alc_busaddr = 0;
1535 	error = bus_dmamap_load(sc->alc_cdata.alc_rr_ring_tag,
1536 	    sc->alc_cdata.alc_rr_ring_map, sc->alc_rdata.alc_rr_ring,
1537 	    ALC_RR_RING_SZ, alc_dmamap_cb, &ctx, 0);
1538 	if (error != 0 || ctx.alc_busaddr == 0) {
1539 		device_printf(sc->alc_dev,
1540 		    "could not load DMA'able memory for Tx ring.\n");
1541 		goto fail;
1542 	}
1543 	sc->alc_rdata.alc_rr_ring_paddr = ctx.alc_busaddr;
1544 
1545 	/* Allocate DMA'able memory and load the DMA map for CMB. */
1546 	error = bus_dmamem_alloc(sc->alc_cdata.alc_cmb_tag,
1547 	    (void **)&sc->alc_rdata.alc_cmb,
1548 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1549 	    &sc->alc_cdata.alc_cmb_map);
1550 	if (error != 0) {
1551 		device_printf(sc->alc_dev,
1552 		    "could not allocate DMA'able memory for CMB.\n");
1553 		goto fail;
1554 	}
1555 	ctx.alc_busaddr = 0;
1556 	error = bus_dmamap_load(sc->alc_cdata.alc_cmb_tag,
1557 	    sc->alc_cdata.alc_cmb_map, sc->alc_rdata.alc_cmb,
1558 	    ALC_CMB_SZ, alc_dmamap_cb, &ctx, 0);
1559 	if (error != 0 || ctx.alc_busaddr == 0) {
1560 		device_printf(sc->alc_dev,
1561 		    "could not load DMA'able memory for CMB.\n");
1562 		goto fail;
1563 	}
1564 	sc->alc_rdata.alc_cmb_paddr = ctx.alc_busaddr;
1565 
1566 	/* Allocate DMA'able memory and load the DMA map for SMB. */
1567 	error = bus_dmamem_alloc(sc->alc_cdata.alc_smb_tag,
1568 	    (void **)&sc->alc_rdata.alc_smb,
1569 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1570 	    &sc->alc_cdata.alc_smb_map);
1571 	if (error != 0) {
1572 		device_printf(sc->alc_dev,
1573 		    "could not allocate DMA'able memory for SMB.\n");
1574 		goto fail;
1575 	}
1576 	ctx.alc_busaddr = 0;
1577 	error = bus_dmamap_load(sc->alc_cdata.alc_smb_tag,
1578 	    sc->alc_cdata.alc_smb_map, sc->alc_rdata.alc_smb,
1579 	    ALC_SMB_SZ, alc_dmamap_cb, &ctx, 0);
1580 	if (error != 0 || ctx.alc_busaddr == 0) {
1581 		device_printf(sc->alc_dev,
1582 		    "could not load DMA'able memory for CMB.\n");
1583 		goto fail;
1584 	}
1585 	sc->alc_rdata.alc_smb_paddr = ctx.alc_busaddr;
1586 
1587 	/* Make sure we've not crossed 4GB boundary. */
1588 	if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
1589 	    (error = alc_check_boundary(sc)) != 0) {
1590 		device_printf(sc->alc_dev, "4GB boundary crossed, "
1591 		    "switching to 32bit DMA addressing mode.\n");
1592 		alc_dma_free(sc);
1593 		/*
1594 		 * Limit max allowable DMA address space to 32bit
1595 		 * and try again.
1596 		 */
1597 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
1598 		goto again;
1599 	}
1600 
1601 	/*
1602 	 * Create Tx buffer parent tag.
1603 	 * AR813x/AR815x allows 64bit DMA addressing of Tx/Rx buffers
1604 	 * so it needs separate parent DMA tag as parent DMA address
1605 	 * space could be restricted to be within 32bit address space
1606 	 * by 4GB boundary crossing.
1607 	 */
1608 	error = bus_dma_tag_create(
1609 	    bus_get_dma_tag(sc->alc_dev), /* parent */
1610 	    1, 0,			/* alignment, boundary */
1611 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1612 	    BUS_SPACE_MAXADDR,		/* highaddr */
1613 	    NULL, NULL,			/* filter, filterarg */
1614 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1615 	    0,				/* nsegments */
1616 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1617 	    0,				/* flags */
1618 	    NULL, NULL,			/* lockfunc, lockarg */
1619 	    &sc->alc_cdata.alc_buffer_tag);
1620 	if (error != 0) {
1621 		device_printf(sc->alc_dev,
1622 		    "could not create parent buffer DMA tag.\n");
1623 		goto fail;
1624 	}
1625 
1626 	/* Create DMA tag for Tx buffers. */
1627 	error = bus_dma_tag_create(
1628 	    sc->alc_cdata.alc_buffer_tag, /* parent */
1629 	    1, 0,			/* alignment, boundary */
1630 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1631 	    BUS_SPACE_MAXADDR,		/* highaddr */
1632 	    NULL, NULL,			/* filter, filterarg */
1633 	    ALC_TSO_MAXSIZE,		/* maxsize */
1634 	    ALC_MAXTXSEGS,		/* nsegments */
1635 	    ALC_TSO_MAXSEGSIZE,		/* maxsegsize */
1636 	    0,				/* flags */
1637 	    NULL, NULL,			/* lockfunc, lockarg */
1638 	    &sc->alc_cdata.alc_tx_tag);
1639 	if (error != 0) {
1640 		device_printf(sc->alc_dev, "could not create Tx DMA tag.\n");
1641 		goto fail;
1642 	}
1643 
1644 	/* Create DMA tag for Rx buffers. */
1645 	error = bus_dma_tag_create(
1646 	    sc->alc_cdata.alc_buffer_tag, /* parent */
1647 	    ALC_RX_BUF_ALIGN, 0,	/* alignment, boundary */
1648 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1649 	    BUS_SPACE_MAXADDR,		/* highaddr */
1650 	    NULL, NULL,			/* filter, filterarg */
1651 	    MCLBYTES,			/* maxsize */
1652 	    1,				/* nsegments */
1653 	    MCLBYTES,			/* maxsegsize */
1654 	    0,				/* flags */
1655 	    NULL, NULL,			/* lockfunc, lockarg */
1656 	    &sc->alc_cdata.alc_rx_tag);
1657 	if (error != 0) {
1658 		device_printf(sc->alc_dev, "could not create Rx DMA tag.\n");
1659 		goto fail;
1660 	}
1661 	/* Create DMA maps for Tx buffers. */
1662 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
1663 		txd = &sc->alc_cdata.alc_txdesc[i];
1664 		txd->tx_m = NULL;
1665 		txd->tx_dmamap = NULL;
1666 		error = bus_dmamap_create(sc->alc_cdata.alc_tx_tag, 0,
1667 		    &txd->tx_dmamap);
1668 		if (error != 0) {
1669 			device_printf(sc->alc_dev,
1670 			    "could not create Tx dmamap.\n");
1671 			goto fail;
1672 		}
1673 	}
1674 	/* Create DMA maps for Rx buffers. */
1675 	if ((error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
1676 	    &sc->alc_cdata.alc_rx_sparemap)) != 0) {
1677 		device_printf(sc->alc_dev,
1678 		    "could not create spare Rx dmamap.\n");
1679 		goto fail;
1680 	}
1681 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
1682 		rxd = &sc->alc_cdata.alc_rxdesc[i];
1683 		rxd->rx_m = NULL;
1684 		rxd->rx_dmamap = NULL;
1685 		error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
1686 		    &rxd->rx_dmamap);
1687 		if (error != 0) {
1688 			device_printf(sc->alc_dev,
1689 			    "could not create Rx dmamap.\n");
1690 			goto fail;
1691 		}
1692 	}
1693 
1694 fail:
1695 	return (error);
1696 }
1697 
1698 static void
1699 alc_dma_free(struct alc_softc *sc)
1700 {
1701 	struct alc_txdesc *txd;
1702 	struct alc_rxdesc *rxd;
1703 	int i;
1704 
1705 	/* Tx buffers. */
1706 	if (sc->alc_cdata.alc_tx_tag != NULL) {
1707 		for (i = 0; i < ALC_TX_RING_CNT; i++) {
1708 			txd = &sc->alc_cdata.alc_txdesc[i];
1709 			if (txd->tx_dmamap != NULL) {
1710 				bus_dmamap_destroy(sc->alc_cdata.alc_tx_tag,
1711 				    txd->tx_dmamap);
1712 				txd->tx_dmamap = NULL;
1713 			}
1714 		}
1715 		bus_dma_tag_destroy(sc->alc_cdata.alc_tx_tag);
1716 		sc->alc_cdata.alc_tx_tag = NULL;
1717 	}
1718 	/* Rx buffers */
1719 	if (sc->alc_cdata.alc_rx_tag != NULL) {
1720 		for (i = 0; i < ALC_RX_RING_CNT; i++) {
1721 			rxd = &sc->alc_cdata.alc_rxdesc[i];
1722 			if (rxd->rx_dmamap != NULL) {
1723 				bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
1724 				    rxd->rx_dmamap);
1725 				rxd->rx_dmamap = NULL;
1726 			}
1727 		}
1728 		if (sc->alc_cdata.alc_rx_sparemap != NULL) {
1729 			bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
1730 			    sc->alc_cdata.alc_rx_sparemap);
1731 			sc->alc_cdata.alc_rx_sparemap = NULL;
1732 		}
1733 		bus_dma_tag_destroy(sc->alc_cdata.alc_rx_tag);
1734 		sc->alc_cdata.alc_rx_tag = NULL;
1735 	}
1736 	/* Tx descriptor ring. */
1737 	if (sc->alc_cdata.alc_tx_ring_tag != NULL) {
1738 		if (sc->alc_rdata.alc_tx_ring_paddr != 0)
1739 			bus_dmamap_unload(sc->alc_cdata.alc_tx_ring_tag,
1740 			    sc->alc_cdata.alc_tx_ring_map);
1741 		if (sc->alc_rdata.alc_tx_ring != NULL)
1742 			bus_dmamem_free(sc->alc_cdata.alc_tx_ring_tag,
1743 			    sc->alc_rdata.alc_tx_ring,
1744 			    sc->alc_cdata.alc_tx_ring_map);
1745 		sc->alc_rdata.alc_tx_ring_paddr = 0;
1746 		sc->alc_rdata.alc_tx_ring = NULL;
1747 		bus_dma_tag_destroy(sc->alc_cdata.alc_tx_ring_tag);
1748 		sc->alc_cdata.alc_tx_ring_tag = NULL;
1749 	}
1750 	/* Rx ring. */
1751 	if (sc->alc_cdata.alc_rx_ring_tag != NULL) {
1752 		if (sc->alc_rdata.alc_rx_ring_paddr != 0)
1753 			bus_dmamap_unload(sc->alc_cdata.alc_rx_ring_tag,
1754 			    sc->alc_cdata.alc_rx_ring_map);
1755 		if (sc->alc_rdata.alc_rx_ring != NULL)
1756 			bus_dmamem_free(sc->alc_cdata.alc_rx_ring_tag,
1757 			    sc->alc_rdata.alc_rx_ring,
1758 			    sc->alc_cdata.alc_rx_ring_map);
1759 		sc->alc_rdata.alc_rx_ring_paddr = 0;
1760 		sc->alc_rdata.alc_rx_ring = NULL;
1761 		bus_dma_tag_destroy(sc->alc_cdata.alc_rx_ring_tag);
1762 		sc->alc_cdata.alc_rx_ring_tag = NULL;
1763 	}
1764 	/* Rx return ring. */
1765 	if (sc->alc_cdata.alc_rr_ring_tag != NULL) {
1766 		if (sc->alc_rdata.alc_rr_ring_paddr != 0)
1767 			bus_dmamap_unload(sc->alc_cdata.alc_rr_ring_tag,
1768 			    sc->alc_cdata.alc_rr_ring_map);
1769 		if (sc->alc_rdata.alc_rr_ring != NULL)
1770 			bus_dmamem_free(sc->alc_cdata.alc_rr_ring_tag,
1771 			    sc->alc_rdata.alc_rr_ring,
1772 			    sc->alc_cdata.alc_rr_ring_map);
1773 		sc->alc_rdata.alc_rr_ring_paddr = 0;
1774 		sc->alc_rdata.alc_rr_ring = NULL;
1775 		bus_dma_tag_destroy(sc->alc_cdata.alc_rr_ring_tag);
1776 		sc->alc_cdata.alc_rr_ring_tag = NULL;
1777 	}
1778 	/* CMB block */
1779 	if (sc->alc_cdata.alc_cmb_tag != NULL) {
1780 		if (sc->alc_rdata.alc_cmb_paddr != 0)
1781 			bus_dmamap_unload(sc->alc_cdata.alc_cmb_tag,
1782 			    sc->alc_cdata.alc_cmb_map);
1783 		if (sc->alc_rdata.alc_cmb != NULL)
1784 			bus_dmamem_free(sc->alc_cdata.alc_cmb_tag,
1785 			    sc->alc_rdata.alc_cmb,
1786 			    sc->alc_cdata.alc_cmb_map);
1787 		sc->alc_rdata.alc_cmb_paddr = 0;
1788 		sc->alc_rdata.alc_cmb = NULL;
1789 		bus_dma_tag_destroy(sc->alc_cdata.alc_cmb_tag);
1790 		sc->alc_cdata.alc_cmb_tag = NULL;
1791 	}
1792 	/* SMB block */
1793 	if (sc->alc_cdata.alc_smb_tag != NULL) {
1794 		if (sc->alc_rdata.alc_smb_paddr != 0)
1795 			bus_dmamap_unload(sc->alc_cdata.alc_smb_tag,
1796 			    sc->alc_cdata.alc_smb_map);
1797 		if (sc->alc_rdata.alc_smb != NULL)
1798 			bus_dmamem_free(sc->alc_cdata.alc_smb_tag,
1799 			    sc->alc_rdata.alc_smb,
1800 			    sc->alc_cdata.alc_smb_map);
1801 		sc->alc_rdata.alc_smb_paddr = 0;
1802 		sc->alc_rdata.alc_smb = NULL;
1803 		bus_dma_tag_destroy(sc->alc_cdata.alc_smb_tag);
1804 		sc->alc_cdata.alc_smb_tag = NULL;
1805 	}
1806 	if (sc->alc_cdata.alc_buffer_tag != NULL) {
1807 		bus_dma_tag_destroy(sc->alc_cdata.alc_buffer_tag);
1808 		sc->alc_cdata.alc_buffer_tag = NULL;
1809 	}
1810 	if (sc->alc_cdata.alc_parent_tag != NULL) {
1811 		bus_dma_tag_destroy(sc->alc_cdata.alc_parent_tag);
1812 		sc->alc_cdata.alc_parent_tag = NULL;
1813 	}
1814 }
1815 
1816 static int
1817 alc_shutdown(device_t dev)
1818 {
1819 
1820 	return (alc_suspend(dev));
1821 }
1822 
1823 /*
1824  * Note, this driver resets the link speed to 10/100Mbps by
1825  * restarting auto-negotiation in suspend/shutdown phase but we
1826  * don't know whether that auto-negotiation would succeed or not
1827  * as driver has no control after powering off/suspend operation.
1828  * If the renegotiation fail WOL may not work. Running at 1Gbps
1829  * will draw more power than 375mA at 3.3V which is specified in
1830  * PCI specification and that would result in complete
1831  * shutdowning power to ethernet controller.
1832  *
1833  * TODO
1834  * Save current negotiated media speed/duplex/flow-control to
1835  * softc and restore the same link again after resuming. PHY
1836  * handling such as power down/resetting to 100Mbps may be better
1837  * handled in suspend method in phy driver.
1838  */
1839 static void
1840 alc_setlinkspeed(struct alc_softc *sc)
1841 {
1842 	struct mii_data *mii;
1843 	int aneg, i;
1844 
1845 	mii = device_get_softc(sc->alc_miibus);
1846 	mii_pollstat(mii);
1847 	aneg = 0;
1848 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1849 	    (IFM_ACTIVE | IFM_AVALID)) {
1850 		switch IFM_SUBTYPE(mii->mii_media_active) {
1851 		case IFM_10_T:
1852 		case IFM_100_TX:
1853 			return;
1854 		case IFM_1000_T:
1855 			aneg++;
1856 			break;
1857 		default:
1858 			break;
1859 		}
1860 	}
1861 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, MII_100T2CR, 0);
1862 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
1863 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1864 	alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
1865 	    MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
1866 	DELAY(1000);
1867 	if (aneg != 0) {
1868 		/*
1869 		 * Poll link state until alc(4) get a 10/100Mbps link.
1870 		 */
1871 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1872 			mii_pollstat(mii);
1873 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
1874 			    == (IFM_ACTIVE | IFM_AVALID)) {
1875 				switch (IFM_SUBTYPE(
1876 				    mii->mii_media_active)) {
1877 				case IFM_10_T:
1878 				case IFM_100_TX:
1879 					alc_mac_config(sc);
1880 					return;
1881 				default:
1882 					break;
1883 				}
1884 			}
1885 			ALC_UNLOCK(sc);
1886 			pause("alclnk", hz);
1887 			ALC_LOCK(sc);
1888 		}
1889 		if (i == MII_ANEGTICKS_GIGE)
1890 			device_printf(sc->alc_dev,
1891 			    "establishing a link failed, WOL may not work!");
1892 	}
1893 	/*
1894 	 * No link, force MAC to have 100Mbps, full-duplex link.
1895 	 * This is the last resort and may/may not work.
1896 	 */
1897 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1898 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1899 	alc_mac_config(sc);
1900 }
1901 
1902 static void
1903 alc_setwol(struct alc_softc *sc)
1904 {
1905 	struct ifnet *ifp;
1906 	uint32_t reg, pmcs;
1907 	uint16_t pmstat;
1908 
1909 	ALC_LOCK_ASSERT(sc);
1910 
1911 	alc_disable_l0s_l1(sc);
1912 	ifp = sc->alc_ifp;
1913 	if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
1914 		/* Disable WOL. */
1915 		CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
1916 		reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
1917 		reg |= PCIE_PHYMISC_FORCE_RCV_DET;
1918 		CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
1919 		/* Force PHY power down. */
1920 		alc_phy_down(sc);
1921 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
1922 		    CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
1923 		return;
1924 	}
1925 
1926 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
1927 		if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
1928 			alc_setlinkspeed(sc);
1929 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
1930 		    CSR_READ_4(sc, ALC_MASTER_CFG) & ~MASTER_CLK_SEL_DIS);
1931 	}
1932 
1933 	pmcs = 0;
1934 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
1935 		pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
1936 	CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
1937 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
1938 	reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
1939 	    MAC_CFG_BCAST);
1940 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
1941 		reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
1942 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
1943 		reg |= MAC_CFG_RX_ENB;
1944 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
1945 
1946 	reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
1947 	reg |= PCIE_PHYMISC_FORCE_RCV_DET;
1948 	CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
1949 	if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1950 		/* WOL disabled, PHY power down. */
1951 		alc_phy_down(sc);
1952 		CSR_WRITE_4(sc, ALC_MASTER_CFG,
1953 		    CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
1954 	}
1955 	/* Request PME. */
1956 	pmstat = pci_read_config(sc->alc_dev,
1957 	    sc->alc_pmcap + PCIR_POWER_STATUS, 2);
1958 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1959 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
1960 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1961 	pci_write_config(sc->alc_dev,
1962 	    sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
1963 }
1964 
1965 static int
1966 alc_suspend(device_t dev)
1967 {
1968 	struct alc_softc *sc;
1969 
1970 	sc = device_get_softc(dev);
1971 
1972 	ALC_LOCK(sc);
1973 	alc_stop(sc);
1974 	alc_setwol(sc);
1975 	ALC_UNLOCK(sc);
1976 
1977 	return (0);
1978 }
1979 
1980 static int
1981 alc_resume(device_t dev)
1982 {
1983 	struct alc_softc *sc;
1984 	struct ifnet *ifp;
1985 	uint16_t pmstat;
1986 
1987 	sc = device_get_softc(dev);
1988 
1989 	ALC_LOCK(sc);
1990 	if ((sc->alc_flags & ALC_FLAG_PM) != 0) {
1991 		/* Disable PME and clear PME status. */
1992 		pmstat = pci_read_config(sc->alc_dev,
1993 		    sc->alc_pmcap + PCIR_POWER_STATUS, 2);
1994 		if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
1995 			pmstat &= ~PCIM_PSTAT_PMEENABLE;
1996 			pci_write_config(sc->alc_dev,
1997 			    sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
1998 		}
1999 	}
2000 	/* Reset PHY. */
2001 	alc_phy_reset(sc);
2002 	ifp = sc->alc_ifp;
2003 	if ((ifp->if_flags & IFF_UP) != 0) {
2004 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2005 		alc_init_locked(sc);
2006 	}
2007 	ALC_UNLOCK(sc);
2008 
2009 	return (0);
2010 }
2011 
2012 static int
2013 alc_encap(struct alc_softc *sc, struct mbuf **m_head)
2014 {
2015 	struct alc_txdesc *txd, *txd_last;
2016 	struct tx_desc *desc;
2017 	struct mbuf *m;
2018 	struct ip *ip;
2019 	struct tcphdr *tcp;
2020 	bus_dma_segment_t txsegs[ALC_MAXTXSEGS];
2021 	bus_dmamap_t map;
2022 	uint32_t cflags, hdrlen, ip_off, poff, vtag;
2023 	int error, idx, nsegs, prod;
2024 
2025 	ALC_LOCK_ASSERT(sc);
2026 
2027 	M_ASSERTPKTHDR((*m_head));
2028 
2029 	m = *m_head;
2030 	ip = NULL;
2031 	tcp = NULL;
2032 	ip_off = poff = 0;
2033 	if ((m->m_pkthdr.csum_flags & (ALC_CSUM_FEATURES | CSUM_TSO)) != 0) {
2034 		/*
2035 		 * AR813x/AR815x requires offset of TCP/UDP header in its
2036 		 * Tx descriptor to perform Tx checksum offloading. TSO
2037 		 * also requires TCP header offset and modification of
2038 		 * IP/TCP header. This kind of operation takes many CPU
2039 		 * cycles on FreeBSD so fast host CPU is required to get
2040 		 * smooth TSO performance.
2041 		 */
2042 		struct ether_header *eh;
2043 
2044 		if (M_WRITABLE(m) == 0) {
2045 			/* Get a writable copy. */
2046 			m = m_dup(*m_head, M_NOWAIT);
2047 			/* Release original mbufs. */
2048 			m_freem(*m_head);
2049 			if (m == NULL) {
2050 				*m_head = NULL;
2051 				return (ENOBUFS);
2052 			}
2053 			*m_head = m;
2054 		}
2055 
2056 		ip_off = sizeof(struct ether_header);
2057 		m = m_pullup(m, ip_off);
2058 		if (m == NULL) {
2059 			*m_head = NULL;
2060 			return (ENOBUFS);
2061 		}
2062 		eh = mtod(m, struct ether_header *);
2063 		/*
2064 		 * Check if hardware VLAN insertion is off.
2065 		 * Additional check for LLC/SNAP frame?
2066 		 */
2067 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2068 			ip_off = sizeof(struct ether_vlan_header);
2069 			m = m_pullup(m, ip_off);
2070 			if (m == NULL) {
2071 				*m_head = NULL;
2072 				return (ENOBUFS);
2073 			}
2074 		}
2075 		m = m_pullup(m, ip_off + sizeof(struct ip));
2076 		if (m == NULL) {
2077 			*m_head = NULL;
2078 			return (ENOBUFS);
2079 		}
2080 		ip = (struct ip *)(mtod(m, char *) + ip_off);
2081 		poff = ip_off + (ip->ip_hl << 2);
2082 		if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2083 			m = m_pullup(m, poff + sizeof(struct tcphdr));
2084 			if (m == NULL) {
2085 				*m_head = NULL;
2086 				return (ENOBUFS);
2087 			}
2088 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2089 			m = m_pullup(m, poff + (tcp->th_off << 2));
2090 			if (m == NULL) {
2091 				*m_head = NULL;
2092 				return (ENOBUFS);
2093 			}
2094 			/*
2095 			 * Due to strict adherence of Microsoft NDIS
2096 			 * Large Send specification, hardware expects
2097 			 * a pseudo TCP checksum inserted by upper
2098 			 * stack. Unfortunately the pseudo TCP
2099 			 * checksum that NDIS refers to does not include
2100 			 * TCP payload length so driver should recompute
2101 			 * the pseudo checksum here. Hopefully this
2102 			 * wouldn't be much burden on modern CPUs.
2103 			 *
2104 			 * Reset IP checksum and recompute TCP pseudo
2105 			 * checksum as NDIS specification said.
2106 			 */
2107 			ip = (struct ip *)(mtod(m, char *) + ip_off);
2108 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2109 			ip->ip_sum = 0;
2110 			tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
2111 			    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
2112 		}
2113 		*m_head = m;
2114 	}
2115 
2116 	prod = sc->alc_cdata.alc_tx_prod;
2117 	txd = &sc->alc_cdata.alc_txdesc[prod];
2118 	txd_last = txd;
2119 	map = txd->tx_dmamap;
2120 
2121 	error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2122 	    *m_head, txsegs, &nsegs, 0);
2123 	if (error == EFBIG) {
2124 		m = m_collapse(*m_head, M_NOWAIT, ALC_MAXTXSEGS);
2125 		if (m == NULL) {
2126 			m_freem(*m_head);
2127 			*m_head = NULL;
2128 			return (ENOMEM);
2129 		}
2130 		*m_head = m;
2131 		error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2132 		    *m_head, txsegs, &nsegs, 0);
2133 		if (error != 0) {
2134 			m_freem(*m_head);
2135 			*m_head = NULL;
2136 			return (error);
2137 		}
2138 	} else if (error != 0)
2139 		return (error);
2140 	if (nsegs == 0) {
2141 		m_freem(*m_head);
2142 		*m_head = NULL;
2143 		return (EIO);
2144 	}
2145 
2146 	/* Check descriptor overrun. */
2147 	if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
2148 		bus_dmamap_unload(sc->alc_cdata.alc_tx_tag, map);
2149 		return (ENOBUFS);
2150 	}
2151 	bus_dmamap_sync(sc->alc_cdata.alc_tx_tag, map, BUS_DMASYNC_PREWRITE);
2152 
2153 	m = *m_head;
2154 	cflags = TD_ETHERNET;
2155 	vtag = 0;
2156 	desc = NULL;
2157 	idx = 0;
2158 	/* Configure VLAN hardware tag insertion. */
2159 	if ((m->m_flags & M_VLANTAG) != 0) {
2160 		vtag = htons(m->m_pkthdr.ether_vtag);
2161 		vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
2162 		cflags |= TD_INS_VLAN_TAG;
2163 	}
2164 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2165 		/* Request TSO and set MSS. */
2166 		cflags |= TD_TSO | TD_TSO_DESCV1;
2167 		cflags |= ((uint32_t)m->m_pkthdr.tso_segsz << TD_MSS_SHIFT) &
2168 		    TD_MSS_MASK;
2169 		/* Set TCP header offset. */
2170 		cflags |= (poff << TD_TCPHDR_OFFSET_SHIFT) &
2171 		    TD_TCPHDR_OFFSET_MASK;
2172 		/*
2173 		 * AR813x/AR815x requires the first buffer should
2174 		 * only hold IP/TCP header data. Payload should
2175 		 * be handled in other descriptors.
2176 		 */
2177 		hdrlen = poff + (tcp->th_off << 2);
2178 		desc = &sc->alc_rdata.alc_tx_ring[prod];
2179 		desc->len = htole32(TX_BYTES(hdrlen | vtag));
2180 		desc->flags = htole32(cflags);
2181 		desc->addr = htole64(txsegs[0].ds_addr);
2182 		sc->alc_cdata.alc_tx_cnt++;
2183 		ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2184 		if (m->m_len - hdrlen > 0) {
2185 			/* Handle remaining payload of the first fragment. */
2186 			desc = &sc->alc_rdata.alc_tx_ring[prod];
2187 			desc->len = htole32(TX_BYTES((m->m_len - hdrlen) |
2188 			    vtag));
2189 			desc->flags = htole32(cflags);
2190 			desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
2191 			sc->alc_cdata.alc_tx_cnt++;
2192 			ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2193 		}
2194 		/* Handle remaining fragments. */
2195 		idx = 1;
2196 	} else if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
2197 		/* Configure Tx checksum offload. */
2198 #ifdef ALC_USE_CUSTOM_CSUM
2199 		cflags |= TD_CUSTOM_CSUM;
2200 		/* Set checksum start offset. */
2201 		cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
2202 		    TD_PLOAD_OFFSET_MASK;
2203 		/* Set checksum insertion position of TCP/UDP. */
2204 		cflags |= (((poff + m->m_pkthdr.csum_data) >> 1) <<
2205 		    TD_CUSTOM_CSUM_OFFSET_SHIFT) & TD_CUSTOM_CSUM_OFFSET_MASK;
2206 #else
2207 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
2208 			cflags |= TD_IPCSUM;
2209 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2210 			cflags |= TD_TCPCSUM;
2211 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2212 			cflags |= TD_UDPCSUM;
2213 		/* Set TCP/UDP header offset. */
2214 		cflags |= (poff << TD_L4HDR_OFFSET_SHIFT) &
2215 		    TD_L4HDR_OFFSET_MASK;
2216 #endif
2217 	}
2218 	for (; idx < nsegs; idx++) {
2219 		desc = &sc->alc_rdata.alc_tx_ring[prod];
2220 		desc->len = htole32(TX_BYTES(txsegs[idx].ds_len) | vtag);
2221 		desc->flags = htole32(cflags);
2222 		desc->addr = htole64(txsegs[idx].ds_addr);
2223 		sc->alc_cdata.alc_tx_cnt++;
2224 		ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2225 	}
2226 	/* Update producer index. */
2227 	sc->alc_cdata.alc_tx_prod = prod;
2228 
2229 	/* Finally set EOP on the last descriptor. */
2230 	prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
2231 	desc = &sc->alc_rdata.alc_tx_ring[prod];
2232 	desc->flags |= htole32(TD_EOP);
2233 
2234 	/* Swap dmamap of the first and the last. */
2235 	txd = &sc->alc_cdata.alc_txdesc[prod];
2236 	map = txd_last->tx_dmamap;
2237 	txd_last->tx_dmamap = txd->tx_dmamap;
2238 	txd->tx_dmamap = map;
2239 	txd->tx_m = m;
2240 
2241 	return (0);
2242 }
2243 
2244 static void
2245 alc_start(struct ifnet *ifp)
2246 {
2247 	struct alc_softc *sc;
2248 
2249 	sc = ifp->if_softc;
2250 	ALC_LOCK(sc);
2251 	alc_start_locked(ifp);
2252 	ALC_UNLOCK(sc);
2253 }
2254 
2255 static void
2256 alc_start_locked(struct ifnet *ifp)
2257 {
2258 	struct alc_softc *sc;
2259 	struct mbuf *m_head;
2260 	int enq;
2261 
2262 	sc = ifp->if_softc;
2263 
2264 	ALC_LOCK_ASSERT(sc);
2265 
2266 	/* Reclaim transmitted frames. */
2267 	if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
2268 		alc_txeof(sc);
2269 
2270 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2271 	    IFF_DRV_RUNNING || (sc->alc_flags & ALC_FLAG_LINK) == 0)
2272 		return;
2273 
2274 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
2275 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2276 		if (m_head == NULL)
2277 			break;
2278 		/*
2279 		 * Pack the data into the transmit ring. If we
2280 		 * don't have room, set the OACTIVE flag and wait
2281 		 * for the NIC to drain the ring.
2282 		 */
2283 		if (alc_encap(sc, &m_head)) {
2284 			if (m_head == NULL)
2285 				break;
2286 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2287 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2288 			break;
2289 		}
2290 
2291 		enq++;
2292 		/*
2293 		 * If there's a BPF listener, bounce a copy of this frame
2294 		 * to him.
2295 		 */
2296 		ETHER_BPF_MTAP(ifp, m_head);
2297 	}
2298 
2299 	if (enq > 0) {
2300 		/* Sync descriptors. */
2301 		bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2302 		    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
2303 		/* Kick. Assume we're using normal Tx priority queue. */
2304 		CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2305 		    (sc->alc_cdata.alc_tx_prod <<
2306 		    MBOX_TD_PROD_LO_IDX_SHIFT) &
2307 		    MBOX_TD_PROD_LO_IDX_MASK);
2308 		/* Set a timeout in case the chip goes out to lunch. */
2309 		sc->alc_watchdog_timer = ALC_TX_TIMEOUT;
2310 	}
2311 }
2312 
2313 static void
2314 alc_watchdog(struct alc_softc *sc)
2315 {
2316 	struct ifnet *ifp;
2317 
2318 	ALC_LOCK_ASSERT(sc);
2319 
2320 	if (sc->alc_watchdog_timer == 0 || --sc->alc_watchdog_timer)
2321 		return;
2322 
2323 	ifp = sc->alc_ifp;
2324 	if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
2325 		if_printf(sc->alc_ifp, "watchdog timeout (lost link)\n");
2326 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2327 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2328 		alc_init_locked(sc);
2329 		return;
2330 	}
2331 	if_printf(sc->alc_ifp, "watchdog timeout -- resetting\n");
2332 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2333 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2334 	alc_init_locked(sc);
2335 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2336 		alc_start_locked(ifp);
2337 }
2338 
2339 static int
2340 alc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2341 {
2342 	struct alc_softc *sc;
2343 	struct ifreq *ifr;
2344 	struct mii_data *mii;
2345 	int error, mask;
2346 
2347 	sc = ifp->if_softc;
2348 	ifr = (struct ifreq *)data;
2349 	error = 0;
2350 	switch (cmd) {
2351 	case SIOCSIFMTU:
2352 		if (ifr->ifr_mtu < ETHERMIN ||
2353 		    ifr->ifr_mtu > (sc->alc_ident->max_framelen -
2354 		    sizeof(struct ether_vlan_header) - ETHER_CRC_LEN) ||
2355 		    ((sc->alc_flags & ALC_FLAG_JUMBO) == 0 &&
2356 		    ifr->ifr_mtu > ETHERMTU))
2357 			error = EINVAL;
2358 		else if (ifp->if_mtu != ifr->ifr_mtu) {
2359 			ALC_LOCK(sc);
2360 			ifp->if_mtu = ifr->ifr_mtu;
2361 			/* AR813x/AR815x has 13 bits MSS field. */
2362 			if (ifp->if_mtu > ALC_TSO_MTU &&
2363 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2364 				ifp->if_capenable &= ~IFCAP_TSO4;
2365 				ifp->if_hwassist &= ~CSUM_TSO;
2366 				VLAN_CAPABILITIES(ifp);
2367 			}
2368 			ALC_UNLOCK(sc);
2369 		}
2370 		break;
2371 	case SIOCSIFFLAGS:
2372 		ALC_LOCK(sc);
2373 		if ((ifp->if_flags & IFF_UP) != 0) {
2374 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2375 			    ((ifp->if_flags ^ sc->alc_if_flags) &
2376 			    (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2377 				alc_rxfilter(sc);
2378 			else
2379 				alc_init_locked(sc);
2380 		} else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2381 			alc_stop(sc);
2382 		sc->alc_if_flags = ifp->if_flags;
2383 		ALC_UNLOCK(sc);
2384 		break;
2385 	case SIOCADDMULTI:
2386 	case SIOCDELMULTI:
2387 		ALC_LOCK(sc);
2388 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2389 			alc_rxfilter(sc);
2390 		ALC_UNLOCK(sc);
2391 		break;
2392 	case SIOCSIFMEDIA:
2393 	case SIOCGIFMEDIA:
2394 		mii = device_get_softc(sc->alc_miibus);
2395 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2396 		break;
2397 	case SIOCSIFCAP:
2398 		ALC_LOCK(sc);
2399 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2400 		if ((mask & IFCAP_TXCSUM) != 0 &&
2401 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
2402 			ifp->if_capenable ^= IFCAP_TXCSUM;
2403 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
2404 				ifp->if_hwassist |= ALC_CSUM_FEATURES;
2405 			else
2406 				ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
2407 		}
2408 		if ((mask & IFCAP_TSO4) != 0 &&
2409 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2410 			ifp->if_capenable ^= IFCAP_TSO4;
2411 			if ((ifp->if_capenable & IFCAP_TSO4) != 0) {
2412 				/* AR813x/AR815x has 13 bits MSS field. */
2413 				if (ifp->if_mtu > ALC_TSO_MTU) {
2414 					ifp->if_capenable &= ~IFCAP_TSO4;
2415 					ifp->if_hwassist &= ~CSUM_TSO;
2416 				} else
2417 					ifp->if_hwassist |= CSUM_TSO;
2418 			} else
2419 				ifp->if_hwassist &= ~CSUM_TSO;
2420 		}
2421 		if ((mask & IFCAP_WOL_MCAST) != 0 &&
2422 		    (ifp->if_capabilities & IFCAP_WOL_MCAST) != 0)
2423 			ifp->if_capenable ^= IFCAP_WOL_MCAST;
2424 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
2425 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
2426 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2427 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2428 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2429 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2430 			alc_rxvlan(sc);
2431 		}
2432 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2433 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2434 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2435 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
2436 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
2437 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2438 		if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2439 			ifp->if_capenable &=
2440 			    ~(IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
2441 		ALC_UNLOCK(sc);
2442 		VLAN_CAPABILITIES(ifp);
2443 		break;
2444 	default:
2445 		error = ether_ioctl(ifp, cmd, data);
2446 		break;
2447 	}
2448 
2449 	return (error);
2450 }
2451 
2452 static void
2453 alc_mac_config(struct alc_softc *sc)
2454 {
2455 	struct mii_data *mii;
2456 	uint32_t reg;
2457 
2458 	ALC_LOCK_ASSERT(sc);
2459 
2460 	mii = device_get_softc(sc->alc_miibus);
2461 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
2462 	reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
2463 	    MAC_CFG_SPEED_MASK);
2464 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
2465 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
2466 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
2467 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
2468 	/* Reprogram MAC with resolved speed/duplex. */
2469 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
2470 	case IFM_10_T:
2471 	case IFM_100_TX:
2472 		reg |= MAC_CFG_SPEED_10_100;
2473 		break;
2474 	case IFM_1000_T:
2475 		reg |= MAC_CFG_SPEED_1000;
2476 		break;
2477 	}
2478 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2479 		reg |= MAC_CFG_FULL_DUPLEX;
2480 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2481 			reg |= MAC_CFG_TX_FC;
2482 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2483 			reg |= MAC_CFG_RX_FC;
2484 	}
2485 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2486 }
2487 
2488 static void
2489 alc_stats_clear(struct alc_softc *sc)
2490 {
2491 	struct smb sb, *smb;
2492 	uint32_t *reg;
2493 	int i;
2494 
2495 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2496 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2497 		    sc->alc_cdata.alc_smb_map,
2498 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2499 		smb = sc->alc_rdata.alc_smb;
2500 		/* Update done, clear. */
2501 		smb->updated = 0;
2502 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2503 		    sc->alc_cdata.alc_smb_map,
2504 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2505 	} else {
2506 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2507 		    reg++) {
2508 			CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2509 			i += sizeof(uint32_t);
2510 		}
2511 		/* Read Tx statistics. */
2512 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2513 		    reg++) {
2514 			CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2515 			i += sizeof(uint32_t);
2516 		}
2517 	}
2518 }
2519 
2520 static void
2521 alc_stats_update(struct alc_softc *sc)
2522 {
2523 	struct alc_hw_stats *stat;
2524 	struct smb sb, *smb;
2525 	struct ifnet *ifp;
2526 	uint32_t *reg;
2527 	int i;
2528 
2529 	ALC_LOCK_ASSERT(sc);
2530 
2531 	ifp = sc->alc_ifp;
2532 	stat = &sc->alc_stats;
2533 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2534 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2535 		    sc->alc_cdata.alc_smb_map,
2536 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2537 		smb = sc->alc_rdata.alc_smb;
2538 		if (smb->updated == 0)
2539 			return;
2540 	} else {
2541 		smb = &sb;
2542 		/* Read Rx statistics. */
2543 		for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2544 		    reg++) {
2545 			*reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2546 			i += sizeof(uint32_t);
2547 		}
2548 		/* Read Tx statistics. */
2549 		for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2550 		    reg++) {
2551 			*reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2552 			i += sizeof(uint32_t);
2553 		}
2554 	}
2555 
2556 	/* Rx stats. */
2557 	stat->rx_frames += smb->rx_frames;
2558 	stat->rx_bcast_frames += smb->rx_bcast_frames;
2559 	stat->rx_mcast_frames += smb->rx_mcast_frames;
2560 	stat->rx_pause_frames += smb->rx_pause_frames;
2561 	stat->rx_control_frames += smb->rx_control_frames;
2562 	stat->rx_crcerrs += smb->rx_crcerrs;
2563 	stat->rx_lenerrs += smb->rx_lenerrs;
2564 	stat->rx_bytes += smb->rx_bytes;
2565 	stat->rx_runts += smb->rx_runts;
2566 	stat->rx_fragments += smb->rx_fragments;
2567 	stat->rx_pkts_64 += smb->rx_pkts_64;
2568 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
2569 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
2570 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
2571 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
2572 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
2573 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
2574 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
2575 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
2576 	stat->rx_rrs_errs += smb->rx_rrs_errs;
2577 	stat->rx_alignerrs += smb->rx_alignerrs;
2578 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
2579 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
2580 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
2581 
2582 	/* Tx stats. */
2583 	stat->tx_frames += smb->tx_frames;
2584 	stat->tx_bcast_frames += smb->tx_bcast_frames;
2585 	stat->tx_mcast_frames += smb->tx_mcast_frames;
2586 	stat->tx_pause_frames += smb->tx_pause_frames;
2587 	stat->tx_excess_defer += smb->tx_excess_defer;
2588 	stat->tx_control_frames += smb->tx_control_frames;
2589 	stat->tx_deferred += smb->tx_deferred;
2590 	stat->tx_bytes += smb->tx_bytes;
2591 	stat->tx_pkts_64 += smb->tx_pkts_64;
2592 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
2593 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
2594 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
2595 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
2596 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
2597 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
2598 	stat->tx_single_colls += smb->tx_single_colls;
2599 	stat->tx_multi_colls += smb->tx_multi_colls;
2600 	stat->tx_late_colls += smb->tx_late_colls;
2601 	stat->tx_excess_colls += smb->tx_excess_colls;
2602 	stat->tx_abort += smb->tx_abort;
2603 	stat->tx_underrun += smb->tx_underrun;
2604 	stat->tx_desc_underrun += smb->tx_desc_underrun;
2605 	stat->tx_lenerrs += smb->tx_lenerrs;
2606 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
2607 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
2608 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
2609 
2610 	/* Update counters in ifnet. */
2611 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, smb->tx_frames);
2612 
2613 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS, smb->tx_single_colls +
2614 	    smb->tx_multi_colls * 2 + smb->tx_late_colls +
2615 	    smb->tx_abort * HDPX_CFG_RETRY_DEFAULT);
2616 
2617 	/*
2618 	 * XXX
2619 	 * tx_pkts_truncated counter looks suspicious. It constantly
2620 	 * increments with no sign of Tx errors. This may indicate
2621 	 * the counter name is not correct one so I've removed the
2622 	 * counter in output errors.
2623 	 */
2624 	if_inc_counter(ifp, IFCOUNTER_OERRORS,
2625 	    smb->tx_abort + smb->tx_late_colls + smb->tx_underrun);
2626 
2627 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, smb->rx_frames);
2628 
2629 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
2630 	    smb->rx_crcerrs + smb->rx_lenerrs +
2631 	    smb->rx_runts + smb->rx_pkts_truncated +
2632 	    smb->rx_fifo_oflows + smb->rx_rrs_errs +
2633 	    smb->rx_alignerrs);
2634 
2635 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2636 		/* Update done, clear. */
2637 		smb->updated = 0;
2638 		bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2639 		    sc->alc_cdata.alc_smb_map,
2640 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2641 	}
2642 }
2643 
2644 static int
2645 alc_intr(void *arg)
2646 {
2647 	struct alc_softc *sc;
2648 	uint32_t status;
2649 
2650 	sc = (struct alc_softc *)arg;
2651 
2652 	status = CSR_READ_4(sc, ALC_INTR_STATUS);
2653 	if ((status & ALC_INTRS) == 0)
2654 		return (FILTER_STRAY);
2655 	/* Disable interrupts. */
2656 	CSR_WRITE_4(sc, ALC_INTR_STATUS, INTR_DIS_INT);
2657 	taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
2658 
2659 	return (FILTER_HANDLED);
2660 }
2661 
2662 static void
2663 alc_int_task(void *arg, int pending)
2664 {
2665 	struct alc_softc *sc;
2666 	struct ifnet *ifp;
2667 	uint32_t status;
2668 	int more;
2669 
2670 	sc = (struct alc_softc *)arg;
2671 	ifp = sc->alc_ifp;
2672 
2673 	status = CSR_READ_4(sc, ALC_INTR_STATUS);
2674 	ALC_LOCK(sc);
2675 	if (sc->alc_morework != 0) {
2676 		sc->alc_morework = 0;
2677 		status |= INTR_RX_PKT;
2678 	}
2679 	if ((status & ALC_INTRS) == 0)
2680 		goto done;
2681 
2682 	/* Acknowledge interrupts but still disable interrupts. */
2683 	CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
2684 
2685 	more = 0;
2686 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2687 		if ((status & INTR_RX_PKT) != 0) {
2688 			more = alc_rxintr(sc, sc->alc_process_limit);
2689 			if (more == EAGAIN)
2690 				sc->alc_morework = 1;
2691 			else if (more == EIO) {
2692 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2693 				alc_init_locked(sc);
2694 				ALC_UNLOCK(sc);
2695 				return;
2696 			}
2697 		}
2698 		if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
2699 		    INTR_TXQ_TO_RST)) != 0) {
2700 			if ((status & INTR_DMA_RD_TO_RST) != 0)
2701 				device_printf(sc->alc_dev,
2702 				    "DMA read error! -- resetting\n");
2703 			if ((status & INTR_DMA_WR_TO_RST) != 0)
2704 				device_printf(sc->alc_dev,
2705 				    "DMA write error! -- resetting\n");
2706 			if ((status & INTR_TXQ_TO_RST) != 0)
2707 				device_printf(sc->alc_dev,
2708 				    "TxQ reset! -- resetting\n");
2709 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2710 			alc_init_locked(sc);
2711 			ALC_UNLOCK(sc);
2712 			return;
2713 		}
2714 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2715 		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2716 			alc_start_locked(ifp);
2717 	}
2718 
2719 	if (more == EAGAIN ||
2720 	    (CSR_READ_4(sc, ALC_INTR_STATUS) & ALC_INTRS) != 0) {
2721 		ALC_UNLOCK(sc);
2722 		taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
2723 		return;
2724 	}
2725 
2726 done:
2727 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2728 		/* Re-enable interrupts if we're running. */
2729 		CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
2730 	}
2731 	ALC_UNLOCK(sc);
2732 }
2733 
2734 static void
2735 alc_txeof(struct alc_softc *sc)
2736 {
2737 	struct ifnet *ifp;
2738 	struct alc_txdesc *txd;
2739 	uint32_t cons, prod;
2740 	int prog;
2741 
2742 	ALC_LOCK_ASSERT(sc);
2743 
2744 	ifp = sc->alc_ifp;
2745 
2746 	if (sc->alc_cdata.alc_tx_cnt == 0)
2747 		return;
2748 	bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2749 	    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_POSTWRITE);
2750 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2751 		bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
2752 		    sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_POSTREAD);
2753 		prod = sc->alc_rdata.alc_cmb->cons;
2754 	} else
2755 		prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
2756 	/* Assume we're using normal Tx priority queue. */
2757 	prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
2758 	    MBOX_TD_CONS_LO_IDX_SHIFT;
2759 	cons = sc->alc_cdata.alc_tx_cons;
2760 	/*
2761 	 * Go through our Tx list and free mbufs for those
2762 	 * frames which have been transmitted.
2763 	 */
2764 	for (prog = 0; cons != prod; prog++,
2765 	    ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
2766 		if (sc->alc_cdata.alc_tx_cnt <= 0)
2767 			break;
2768 		prog++;
2769 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2770 		sc->alc_cdata.alc_tx_cnt--;
2771 		txd = &sc->alc_cdata.alc_txdesc[cons];
2772 		if (txd->tx_m != NULL) {
2773 			/* Reclaim transmitted mbufs. */
2774 			bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
2775 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2776 			bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
2777 			    txd->tx_dmamap);
2778 			m_freem(txd->tx_m);
2779 			txd->tx_m = NULL;
2780 		}
2781 	}
2782 
2783 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
2784 		bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
2785 		    sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_PREREAD);
2786 	sc->alc_cdata.alc_tx_cons = cons;
2787 	/*
2788 	 * Unarm watchdog timer only when there is no pending
2789 	 * frames in Tx queue.
2790 	 */
2791 	if (sc->alc_cdata.alc_tx_cnt == 0)
2792 		sc->alc_watchdog_timer = 0;
2793 }
2794 
2795 static int
2796 alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd)
2797 {
2798 	struct mbuf *m;
2799 	bus_dma_segment_t segs[1];
2800 	bus_dmamap_t map;
2801 	int nsegs;
2802 
2803 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2804 	if (m == NULL)
2805 		return (ENOBUFS);
2806 	m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
2807 #ifndef __NO_STRICT_ALIGNMENT
2808 	m_adj(m, sizeof(uint64_t));
2809 #endif
2810 
2811 	if (bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_rx_tag,
2812 	    sc->alc_cdata.alc_rx_sparemap, m, segs, &nsegs, 0) != 0) {
2813 		m_freem(m);
2814 		return (ENOBUFS);
2815 	}
2816 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2817 
2818 	if (rxd->rx_m != NULL) {
2819 		bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
2820 		    BUS_DMASYNC_POSTREAD);
2821 		bus_dmamap_unload(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap);
2822 	}
2823 	map = rxd->rx_dmamap;
2824 	rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
2825 	sc->alc_cdata.alc_rx_sparemap = map;
2826 	bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
2827 	    BUS_DMASYNC_PREREAD);
2828 	rxd->rx_m = m;
2829 	rxd->rx_desc->addr = htole64(segs[0].ds_addr);
2830 	return (0);
2831 }
2832 
2833 static int
2834 alc_rxintr(struct alc_softc *sc, int count)
2835 {
2836 	struct ifnet *ifp;
2837 	struct rx_rdesc *rrd;
2838 	uint32_t nsegs, status;
2839 	int rr_cons, prog;
2840 
2841 	bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
2842 	    sc->alc_cdata.alc_rr_ring_map,
2843 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2844 	bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
2845 	    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_POSTWRITE);
2846 	rr_cons = sc->alc_cdata.alc_rr_cons;
2847 	ifp = sc->alc_ifp;
2848 	for (prog = 0; (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;) {
2849 		if (count-- <= 0)
2850 			break;
2851 		rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
2852 		status = le32toh(rrd->status);
2853 		if ((status & RRD_VALID) == 0)
2854 			break;
2855 		nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
2856 		if (nsegs == 0) {
2857 			/* This should not happen! */
2858 			device_printf(sc->alc_dev,
2859 			    "unexpected segment count -- resetting\n");
2860 			return (EIO);
2861 		}
2862 		alc_rxeof(sc, rrd);
2863 		/* Clear Rx return status. */
2864 		rrd->status = 0;
2865 		ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
2866 		sc->alc_cdata.alc_rx_cons += nsegs;
2867 		sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
2868 		prog += nsegs;
2869 	}
2870 
2871 	if (prog > 0) {
2872 		/* Update the consumer index. */
2873 		sc->alc_cdata.alc_rr_cons = rr_cons;
2874 		/* Sync Rx return descriptors. */
2875 		bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
2876 		    sc->alc_cdata.alc_rr_ring_map,
2877 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2878 		/*
2879 		 * Sync updated Rx descriptors such that controller see
2880 		 * modified buffer addresses.
2881 		 */
2882 		bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
2883 		    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
2884 		/*
2885 		 * Let controller know availability of new Rx buffers.
2886 		 * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
2887 		 * it may be possible to update ALC_MBOX_RD0_PROD_IDX
2888 		 * only when Rx buffer pre-fetching is required. In
2889 		 * addition we already set ALC_RX_RD_FREE_THRESH to
2890 		 * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
2891 		 * it still seems that pre-fetching needs more
2892 		 * experimentation.
2893 		 */
2894 		CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
2895 		    sc->alc_cdata.alc_rx_cons);
2896 	}
2897 
2898 	return (count > 0 ? 0 : EAGAIN);
2899 }
2900 
2901 #ifndef __NO_STRICT_ALIGNMENT
2902 static struct mbuf *
2903 alc_fixup_rx(struct ifnet *ifp, struct mbuf *m)
2904 {
2905 	struct mbuf *n;
2906         int i;
2907         uint16_t *src, *dst;
2908 
2909 	src = mtod(m, uint16_t *);
2910 	dst = src - 3;
2911 
2912 	if (m->m_next == NULL) {
2913 		for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2914 			*dst++ = *src++;
2915 		m->m_data -= 6;
2916 		return (m);
2917 	}
2918 	/*
2919 	 * Append a new mbuf to received mbuf chain and copy ethernet
2920 	 * header from the mbuf chain. This can save lots of CPU
2921 	 * cycles for jumbo frame.
2922 	 */
2923 	MGETHDR(n, M_NOWAIT, MT_DATA);
2924 	if (n == NULL) {
2925 		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2926 		m_freem(m);
2927 		return (NULL);
2928 	}
2929 	bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
2930 	m->m_data += ETHER_HDR_LEN;
2931 	m->m_len -= ETHER_HDR_LEN;
2932 	n->m_len = ETHER_HDR_LEN;
2933 	M_MOVE_PKTHDR(n, m);
2934 	n->m_next = m;
2935 	return (n);
2936 }
2937 #endif
2938 
2939 /* Receive a frame. */
2940 static void
2941 alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
2942 {
2943 	struct alc_rxdesc *rxd;
2944 	struct ifnet *ifp;
2945 	struct mbuf *mp, *m;
2946 	uint32_t rdinfo, status, vtag;
2947 	int count, nsegs, rx_cons;
2948 
2949 	ifp = sc->alc_ifp;
2950 	status = le32toh(rrd->status);
2951 	rdinfo = le32toh(rrd->rdinfo);
2952 	rx_cons = RRD_RD_IDX(rdinfo);
2953 	nsegs = RRD_RD_CNT(rdinfo);
2954 
2955 	sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
2956 	if ((status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) != 0) {
2957 		/*
2958 		 * We want to pass the following frames to upper
2959 		 * layer regardless of error status of Rx return
2960 		 * ring.
2961 		 *
2962 		 *  o IP/TCP/UDP checksum is bad.
2963 		 *  o frame length and protocol specific length
2964 		 *     does not match.
2965 		 *
2966 		 *  Force network stack compute checksum for
2967 		 *  errored frames.
2968 		 */
2969 		status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
2970 		if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
2971 		    RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
2972 			return;
2973 	}
2974 
2975 	for (count = 0; count < nsegs; count++,
2976 	    ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
2977 		rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
2978 		mp = rxd->rx_m;
2979 		/* Add a new receive buffer to the ring. */
2980 		if (alc_newbuf(sc, rxd) != 0) {
2981 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2982 			/* Reuse Rx buffers. */
2983 			if (sc->alc_cdata.alc_rxhead != NULL)
2984 				m_freem(sc->alc_cdata.alc_rxhead);
2985 			break;
2986 		}
2987 
2988 		/*
2989 		 * Assume we've received a full sized frame.
2990 		 * Actual size is fixed when we encounter the end of
2991 		 * multi-segmented frame.
2992 		 */
2993 		mp->m_len = sc->alc_buf_size;
2994 
2995 		/* Chain received mbufs. */
2996 		if (sc->alc_cdata.alc_rxhead == NULL) {
2997 			sc->alc_cdata.alc_rxhead = mp;
2998 			sc->alc_cdata.alc_rxtail = mp;
2999 		} else {
3000 			mp->m_flags &= ~M_PKTHDR;
3001 			sc->alc_cdata.alc_rxprev_tail =
3002 			    sc->alc_cdata.alc_rxtail;
3003 			sc->alc_cdata.alc_rxtail->m_next = mp;
3004 			sc->alc_cdata.alc_rxtail = mp;
3005 		}
3006 
3007 		if (count == nsegs - 1) {
3008 			/* Last desc. for this frame. */
3009 			m = sc->alc_cdata.alc_rxhead;
3010 			m->m_flags |= M_PKTHDR;
3011 			/*
3012 			 * It seems that L1C/L2C controller has no way
3013 			 * to tell hardware to strip CRC bytes.
3014 			 */
3015 			m->m_pkthdr.len =
3016 			    sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
3017 			if (nsegs > 1) {
3018 				/* Set last mbuf size. */
3019 				mp->m_len = sc->alc_cdata.alc_rxlen -
3020 				    (nsegs - 1) * sc->alc_buf_size;
3021 				/* Remove the CRC bytes in chained mbufs. */
3022 				if (mp->m_len <= ETHER_CRC_LEN) {
3023 					sc->alc_cdata.alc_rxtail =
3024 					    sc->alc_cdata.alc_rxprev_tail;
3025 					sc->alc_cdata.alc_rxtail->m_len -=
3026 					    (ETHER_CRC_LEN - mp->m_len);
3027 					sc->alc_cdata.alc_rxtail->m_next = NULL;
3028 					m_freem(mp);
3029 				} else {
3030 					mp->m_len -= ETHER_CRC_LEN;
3031 				}
3032 			} else
3033 				m->m_len = m->m_pkthdr.len;
3034 			m->m_pkthdr.rcvif = ifp;
3035 			/*
3036 			 * Due to hardware bugs, Rx checksum offloading
3037 			 * was intentionally disabled.
3038 			 */
3039 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
3040 			    (status & RRD_VLAN_TAG) != 0) {
3041 				vtag = RRD_VLAN(le32toh(rrd->vtag));
3042 				m->m_pkthdr.ether_vtag = ntohs(vtag);
3043 				m->m_flags |= M_VLANTAG;
3044 			}
3045 #ifndef __NO_STRICT_ALIGNMENT
3046 			m = alc_fixup_rx(ifp, m);
3047 			if (m != NULL)
3048 #endif
3049 			{
3050 			/* Pass it on. */
3051 			ALC_UNLOCK(sc);
3052 			(*ifp->if_input)(ifp, m);
3053 			ALC_LOCK(sc);
3054 			}
3055 		}
3056 	}
3057 	/* Reset mbuf chains. */
3058 	ALC_RXCHAIN_RESET(sc);
3059 }
3060 
3061 static void
3062 alc_tick(void *arg)
3063 {
3064 	struct alc_softc *sc;
3065 	struct mii_data *mii;
3066 
3067 	sc = (struct alc_softc *)arg;
3068 
3069 	ALC_LOCK_ASSERT(sc);
3070 
3071 	mii = device_get_softc(sc->alc_miibus);
3072 	mii_tick(mii);
3073 	alc_stats_update(sc);
3074 	/*
3075 	 * alc(4) does not rely on Tx completion interrupts to reclaim
3076 	 * transferred buffers. Instead Tx completion interrupts are
3077 	 * used to hint for scheduling Tx task. So it's necessary to
3078 	 * release transmitted buffers by kicking Tx completion
3079 	 * handler. This limits the maximum reclamation delay to a hz.
3080 	 */
3081 	alc_txeof(sc);
3082 	alc_watchdog(sc);
3083 	callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3084 }
3085 
3086 static void
3087 alc_reset(struct alc_softc *sc)
3088 {
3089 	uint32_t reg;
3090 	int i;
3091 
3092 	reg = CSR_READ_4(sc, ALC_MASTER_CFG) & 0xFFFF;
3093 	reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
3094 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3095 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3096 		DELAY(10);
3097 		if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
3098 			break;
3099 	}
3100 	if (i == 0)
3101 		device_printf(sc->alc_dev, "master reset timeout!\n");
3102 
3103 	for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3104 		if ((reg = CSR_READ_4(sc, ALC_IDLE_STATUS)) == 0)
3105 			break;
3106 		DELAY(10);
3107 	}
3108 
3109 	if (i == 0)
3110 		device_printf(sc->alc_dev, "reset timeout(0x%08x)!\n", reg);
3111 }
3112 
3113 static void
3114 alc_init(void *xsc)
3115 {
3116 	struct alc_softc *sc;
3117 
3118 	sc = (struct alc_softc *)xsc;
3119 	ALC_LOCK(sc);
3120 	alc_init_locked(sc);
3121 	ALC_UNLOCK(sc);
3122 }
3123 
3124 static void
3125 alc_init_locked(struct alc_softc *sc)
3126 {
3127 	struct ifnet *ifp;
3128 	struct mii_data *mii;
3129 	uint8_t eaddr[ETHER_ADDR_LEN];
3130 	bus_addr_t paddr;
3131 	uint32_t reg, rxf_hi, rxf_lo;
3132 
3133 	ALC_LOCK_ASSERT(sc);
3134 
3135 	ifp = sc->alc_ifp;
3136 	mii = device_get_softc(sc->alc_miibus);
3137 
3138 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3139 		return;
3140 	/*
3141 	 * Cancel any pending I/O.
3142 	 */
3143 	alc_stop(sc);
3144 	/*
3145 	 * Reset the chip to a known state.
3146 	 */
3147 	alc_reset(sc);
3148 
3149 	/* Initialize Rx descriptors. */
3150 	if (alc_init_rx_ring(sc) != 0) {
3151 		device_printf(sc->alc_dev, "no memory for Rx buffers.\n");
3152 		alc_stop(sc);
3153 		return;
3154 	}
3155 	alc_init_rr_ring(sc);
3156 	alc_init_tx_ring(sc);
3157 	alc_init_cmb(sc);
3158 	alc_init_smb(sc);
3159 
3160 	/* Enable all clocks. */
3161 	CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
3162 
3163 	/* Reprogram the station address. */
3164 	bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
3165 	CSR_WRITE_4(sc, ALC_PAR0,
3166 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
3167 	CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
3168 	/*
3169 	 * Clear WOL status and disable all WOL feature as WOL
3170 	 * would interfere Rx operation under normal environments.
3171 	 */
3172 	CSR_READ_4(sc, ALC_WOL_CFG);
3173 	CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
3174 	/* Set Tx descriptor base addresses. */
3175 	paddr = sc->alc_rdata.alc_tx_ring_paddr;
3176 	CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3177 	CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3178 	/* We don't use high priority ring. */
3179 	CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
3180 	/* Set Tx descriptor counter. */
3181 	CSR_WRITE_4(sc, ALC_TD_RING_CNT,
3182 	    (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
3183 	/* Set Rx descriptor base addresses. */
3184 	paddr = sc->alc_rdata.alc_rx_ring_paddr;
3185 	CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3186 	CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3187 	/* We use one Rx ring. */
3188 	CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
3189 	CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
3190 	CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
3191 	/* Set Rx descriptor counter. */
3192 	CSR_WRITE_4(sc, ALC_RD_RING_CNT,
3193 	    (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
3194 
3195 	/*
3196 	 * Let hardware split jumbo frames into alc_max_buf_sized chunks.
3197 	 * if it do not fit the buffer size. Rx return descriptor holds
3198 	 * a counter that indicates how many fragments were made by the
3199 	 * hardware. The buffer size should be multiple of 8 bytes.
3200 	 * Since hardware has limit on the size of buffer size, always
3201 	 * use the maximum value.
3202 	 * For strict-alignment architectures make sure to reduce buffer
3203 	 * size by 8 bytes to make room for alignment fixup.
3204 	 */
3205 #ifndef __NO_STRICT_ALIGNMENT
3206 	sc->alc_buf_size = RX_BUF_SIZE_MAX - sizeof(uint64_t);
3207 #else
3208 	sc->alc_buf_size = RX_BUF_SIZE_MAX;
3209 #endif
3210 	CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
3211 
3212 	paddr = sc->alc_rdata.alc_rr_ring_paddr;
3213 	/* Set Rx return descriptor base addresses. */
3214 	CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3215 	/* We use one Rx return ring. */
3216 	CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
3217 	CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
3218 	CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
3219 	/* Set Rx return descriptor counter. */
3220 	CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
3221 	    (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
3222 	paddr = sc->alc_rdata.alc_cmb_paddr;
3223 	CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
3224 	paddr = sc->alc_rdata.alc_smb_paddr;
3225 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3226 	CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
3227 
3228 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
3229 		/* Reconfigure SRAM - Vendor magic. */
3230 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
3231 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
3232 		CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
3233 		CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
3234 		CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
3235 		CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
3236 		CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
3237 		CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
3238 	}
3239 
3240 	/* Tell hardware that we're ready to load DMA blocks. */
3241 	CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
3242 
3243 	/* Configure interrupt moderation timer. */
3244 	reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
3245 	reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
3246 	CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
3247 	/*
3248 	 * We don't want to automatic interrupt clear as task queue
3249 	 * for the interrupt should know interrupt status.
3250 	 */
3251 	reg = MASTER_SA_TIMER_ENB;
3252 	if (ALC_USECS(sc->alc_int_rx_mod) != 0)
3253 		reg |= MASTER_IM_RX_TIMER_ENB;
3254 	if (ALC_USECS(sc->alc_int_tx_mod) != 0)
3255 		reg |= MASTER_IM_TX_TIMER_ENB;
3256 	CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3257 	/*
3258 	 * Disable interrupt re-trigger timer. We don't want automatic
3259 	 * re-triggering of un-ACKed interrupts.
3260 	 */
3261 	CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
3262 	/* Configure CMB. */
3263 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
3264 		CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
3265 		CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
3266 	} else
3267 		CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
3268 	/*
3269 	 * Hardware can be configured to issue SMB interrupt based
3270 	 * on programmed interval. Since there is a callout that is
3271 	 * invoked for every hz in driver we use that instead of
3272 	 * relying on periodic SMB interrupt.
3273 	 */
3274 	CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
3275 	/* Clear MAC statistics. */
3276 	alc_stats_clear(sc);
3277 
3278 	/*
3279 	 * Always use maximum frame size that controller can support.
3280 	 * Otherwise received frames that has larger frame length
3281 	 * than alc(4) MTU would be silently dropped in hardware. This
3282 	 * would make path-MTU discovery hard as sender wouldn't get
3283 	 * any responses from receiver. alc(4) supports
3284 	 * multi-fragmented frames on Rx path so it has no issue on
3285 	 * assembling fragmented frames. Using maximum frame size also
3286 	 * removes the need to reinitialize hardware when interface
3287 	 * MTU configuration was changed.
3288 	 *
3289 	 * Be conservative in what you do, be liberal in what you
3290 	 * accept from others - RFC 793.
3291 	 */
3292 	CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
3293 
3294 	/* Disable header split(?) */
3295 	CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
3296 
3297 	/* Configure IPG/IFG parameters. */
3298 	CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
3299 	    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK) |
3300 	    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
3301 	    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
3302 	    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK));
3303 	/* Set parameters for half-duplex media. */
3304 	CSR_WRITE_4(sc, ALC_HDPX_CFG,
3305 	    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
3306 	    HDPX_CFG_LCOL_MASK) |
3307 	    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
3308 	    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
3309 	    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
3310 	    HDPX_CFG_ABEBT_MASK) |
3311 	    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
3312 	    HDPX_CFG_JAMIPG_MASK));
3313 	/*
3314 	 * Set TSO/checksum offload threshold. For frames that is
3315 	 * larger than this threshold, hardware wouldn't do
3316 	 * TSO/checksum offloading.
3317 	 */
3318 	CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH,
3319 	    (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
3320 	    TSO_OFFLOAD_THRESH_MASK);
3321 	/* Configure TxQ. */
3322 	reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
3323 	    TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
3324 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3325 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
3326 		reg >>= 1;
3327 	reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
3328 	    TXQ_CFG_TD_BURST_MASK;
3329 	CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
3330 
3331 	/* Configure Rx free descriptor pre-fetching. */
3332 	CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
3333 	    ((RX_RD_FREE_THRESH_HI_DEFAULT << RX_RD_FREE_THRESH_HI_SHIFT) &
3334 	    RX_RD_FREE_THRESH_HI_MASK) |
3335 	    ((RX_RD_FREE_THRESH_LO_DEFAULT << RX_RD_FREE_THRESH_LO_SHIFT) &
3336 	    RX_RD_FREE_THRESH_LO_MASK));
3337 
3338 	/*
3339 	 * Configure flow control parameters.
3340 	 * XON  : 80% of Rx FIFO
3341 	 * XOFF : 30% of Rx FIFO
3342 	 */
3343 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
3344 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132) {
3345 		reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
3346 		rxf_hi = (reg * 8) / 10;
3347 		rxf_lo = (reg * 3) / 10;
3348 		CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3349 		    ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3350 		     RX_FIFO_PAUSE_THRESH_LO_MASK) |
3351 		    ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3352 		     RX_FIFO_PAUSE_THRESH_HI_MASK));
3353 	}
3354 
3355 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3356 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2)
3357 		CSR_WRITE_4(sc, ALC_SERDES_LOCK,
3358 		    CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
3359 		    SERDES_PHY_CLK_SLOWDOWN);
3360 
3361 	/* Disable RSS until I understand L1C/L2C's RSS logic. */
3362 	CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
3363 	CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
3364 
3365 	/* Configure RxQ. */
3366 	reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
3367 	    RXQ_CFG_RD_BURST_MASK;
3368 	reg |= RXQ_CFG_RSS_MODE_DIS;
3369 	if ((sc->alc_flags & ALC_FLAG_ASPM_MON) != 0)
3370 		reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_1M;
3371 	CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3372 
3373 	/* Configure DMA parameters. */
3374 	reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
3375 	reg |= sc->alc_rcb;
3376 	if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3377 		reg |= DMA_CFG_CMB_ENB;
3378 	if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
3379 		reg |= DMA_CFG_SMB_ENB;
3380 	else
3381 		reg |= DMA_CFG_SMB_DIS;
3382 	reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
3383 	    DMA_CFG_RD_BURST_SHIFT;
3384 	reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
3385 	    DMA_CFG_WR_BURST_SHIFT;
3386 	reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
3387 	    DMA_CFG_RD_DELAY_CNT_MASK;
3388 	reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
3389 	    DMA_CFG_WR_DELAY_CNT_MASK;
3390 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3391 
3392 	/*
3393 	 * Configure Tx/Rx MACs.
3394 	 *  - Auto-padding for short frames.
3395 	 *  - Enable CRC generation.
3396 	 *  Actual reconfiguration of MAC for resolved speed/duplex
3397 	 *  is followed after detection of link establishment.
3398 	 *  AR813x/AR815x always does checksum computation regardless
3399 	 *  of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
3400 	 *  have bug in protocol field in Rx return structure so
3401 	 *  these controllers can't handle fragmented frames. Disable
3402 	 *  Rx checksum offloading until there is a newer controller
3403 	 *  that has sane implementation.
3404 	 */
3405 	reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
3406 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
3407 	    MAC_CFG_PREAMBLE_MASK);
3408 	if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
3409 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
3410 	    sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
3411 		reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3412 	if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
3413 		reg |= MAC_CFG_SPEED_10_100;
3414 	else
3415 		reg |= MAC_CFG_SPEED_1000;
3416 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3417 
3418 	/* Set up the receive filter. */
3419 	alc_rxfilter(sc);
3420 	alc_rxvlan(sc);
3421 
3422 	/* Acknowledge all pending interrupts and clear it. */
3423 	CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
3424 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3425 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
3426 
3427 	sc->alc_flags &= ~ALC_FLAG_LINK;
3428 	/* Switch to the current media. */
3429 	mii_mediachg(mii);
3430 
3431 	callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3432 
3433 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3434 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3435 }
3436 
3437 static void
3438 alc_stop(struct alc_softc *sc)
3439 {
3440 	struct ifnet *ifp;
3441 	struct alc_txdesc *txd;
3442 	struct alc_rxdesc *rxd;
3443 	uint32_t reg;
3444 	int i;
3445 
3446 	ALC_LOCK_ASSERT(sc);
3447 	/*
3448 	 * Mark the interface down and cancel the watchdog timer.
3449 	 */
3450 	ifp = sc->alc_ifp;
3451 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3452 	sc->alc_flags &= ~ALC_FLAG_LINK;
3453 	callout_stop(&sc->alc_tick_ch);
3454 	sc->alc_watchdog_timer = 0;
3455 	alc_stats_update(sc);
3456 	/* Disable interrupts. */
3457 	CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
3458 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3459 	alc_stop_queue(sc);
3460 	/* Disable DMA. */
3461 	reg = CSR_READ_4(sc, ALC_DMA_CFG);
3462 	reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
3463 	reg |= DMA_CFG_SMB_DIS;
3464 	CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3465 	DELAY(1000);
3466 	/* Stop Rx/Tx MACs. */
3467 	alc_stop_mac(sc);
3468 	/* Disable interrupts which might be touched in taskq handler. */
3469 	CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3470 
3471 	/* Reclaim Rx buffers that have been processed. */
3472 	if (sc->alc_cdata.alc_rxhead != NULL)
3473 		m_freem(sc->alc_cdata.alc_rxhead);
3474 	ALC_RXCHAIN_RESET(sc);
3475 	/*
3476 	 * Free Tx/Rx mbufs still in the queues.
3477 	 */
3478 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
3479 		rxd = &sc->alc_cdata.alc_rxdesc[i];
3480 		if (rxd->rx_m != NULL) {
3481 			bus_dmamap_sync(sc->alc_cdata.alc_rx_tag,
3482 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3483 			bus_dmamap_unload(sc->alc_cdata.alc_rx_tag,
3484 			    rxd->rx_dmamap);
3485 			m_freem(rxd->rx_m);
3486 			rxd->rx_m = NULL;
3487 		}
3488 	}
3489 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
3490 		txd = &sc->alc_cdata.alc_txdesc[i];
3491 		if (txd->tx_m != NULL) {
3492 			bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
3493 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3494 			bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
3495 			    txd->tx_dmamap);
3496 			m_freem(txd->tx_m);
3497 			txd->tx_m = NULL;
3498 		}
3499 	}
3500 }
3501 
3502 static void
3503 alc_stop_mac(struct alc_softc *sc)
3504 {
3505 	uint32_t reg;
3506 	int i;
3507 
3508 	ALC_LOCK_ASSERT(sc);
3509 
3510 	/* Disable Rx/Tx MAC. */
3511 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
3512 	if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
3513 		reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
3514 		CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3515 	}
3516 	for (i = ALC_TIMEOUT; i > 0; i--) {
3517 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3518 		if (reg == 0)
3519 			break;
3520 		DELAY(10);
3521 	}
3522 	if (i == 0)
3523 		device_printf(sc->alc_dev,
3524 		    "could not disable Rx/Tx MAC(0x%08x)!\n", reg);
3525 }
3526 
3527 static void
3528 alc_start_queue(struct alc_softc *sc)
3529 {
3530 	uint32_t qcfg[] = {
3531 		0,
3532 		RXQ_CFG_QUEUE0_ENB,
3533 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
3534 		RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
3535 		RXQ_CFG_ENB
3536 	};
3537 	uint32_t cfg;
3538 
3539 	ALC_LOCK_ASSERT(sc);
3540 
3541 	/* Enable RxQ. */
3542 	cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
3543 	cfg &= ~RXQ_CFG_ENB;
3544 	cfg |= qcfg[1];
3545 	CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
3546 	/* Enable TxQ. */
3547 	cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
3548 	cfg |= TXQ_CFG_ENB;
3549 	CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
3550 }
3551 
3552 static void
3553 alc_stop_queue(struct alc_softc *sc)
3554 {
3555 	uint32_t reg;
3556 	int i;
3557 
3558 	ALC_LOCK_ASSERT(sc);
3559 
3560 	/* Disable RxQ. */
3561 	reg = CSR_READ_4(sc, ALC_RXQ_CFG);
3562 	if ((reg & RXQ_CFG_ENB) != 0) {
3563 		reg &= ~RXQ_CFG_ENB;
3564 		CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3565 	}
3566 	/* Disable TxQ. */
3567 	reg = CSR_READ_4(sc, ALC_TXQ_CFG);
3568 	if ((reg & TXQ_CFG_ENB) != 0) {
3569 		reg &= ~TXQ_CFG_ENB;
3570 		CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
3571 	}
3572 	for (i = ALC_TIMEOUT; i > 0; i--) {
3573 		reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3574 		if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3575 			break;
3576 		DELAY(10);
3577 	}
3578 	if (i == 0)
3579 		device_printf(sc->alc_dev,
3580 		    "could not disable RxQ/TxQ (0x%08x)!\n", reg);
3581 }
3582 
3583 static void
3584 alc_init_tx_ring(struct alc_softc *sc)
3585 {
3586 	struct alc_ring_data *rd;
3587 	struct alc_txdesc *txd;
3588 	int i;
3589 
3590 	ALC_LOCK_ASSERT(sc);
3591 
3592 	sc->alc_cdata.alc_tx_prod = 0;
3593 	sc->alc_cdata.alc_tx_cons = 0;
3594 	sc->alc_cdata.alc_tx_cnt = 0;
3595 
3596 	rd = &sc->alc_rdata;
3597 	bzero(rd->alc_tx_ring, ALC_TX_RING_SZ);
3598 	for (i = 0; i < ALC_TX_RING_CNT; i++) {
3599 		txd = &sc->alc_cdata.alc_txdesc[i];
3600 		txd->tx_m = NULL;
3601 	}
3602 
3603 	bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
3604 	    sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
3605 }
3606 
3607 static int
3608 alc_init_rx_ring(struct alc_softc *sc)
3609 {
3610 	struct alc_ring_data *rd;
3611 	struct alc_rxdesc *rxd;
3612 	int i;
3613 
3614 	ALC_LOCK_ASSERT(sc);
3615 
3616 	sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
3617 	sc->alc_morework = 0;
3618 	rd = &sc->alc_rdata;
3619 	bzero(rd->alc_rx_ring, ALC_RX_RING_SZ);
3620 	for (i = 0; i < ALC_RX_RING_CNT; i++) {
3621 		rxd = &sc->alc_cdata.alc_rxdesc[i];
3622 		rxd->rx_m = NULL;
3623 		rxd->rx_desc = &rd->alc_rx_ring[i];
3624 		if (alc_newbuf(sc, rxd) != 0)
3625 			return (ENOBUFS);
3626 	}
3627 
3628 	/*
3629 	 * Since controller does not update Rx descriptors, driver
3630 	 * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
3631 	 * is enough to ensure coherence.
3632 	 */
3633 	bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3634 	    sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
3635 	/* Let controller know availability of new Rx buffers. */
3636 	CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
3637 
3638 	return (0);
3639 }
3640 
3641 static void
3642 alc_init_rr_ring(struct alc_softc *sc)
3643 {
3644 	struct alc_ring_data *rd;
3645 
3646 	ALC_LOCK_ASSERT(sc);
3647 
3648 	sc->alc_cdata.alc_rr_cons = 0;
3649 	ALC_RXCHAIN_RESET(sc);
3650 
3651 	rd = &sc->alc_rdata;
3652 	bzero(rd->alc_rr_ring, ALC_RR_RING_SZ);
3653 	bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3654 	    sc->alc_cdata.alc_rr_ring_map,
3655 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3656 }
3657 
3658 static void
3659 alc_init_cmb(struct alc_softc *sc)
3660 {
3661 	struct alc_ring_data *rd;
3662 
3663 	ALC_LOCK_ASSERT(sc);
3664 
3665 	rd = &sc->alc_rdata;
3666 	bzero(rd->alc_cmb, ALC_CMB_SZ);
3667 	bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag, sc->alc_cdata.alc_cmb_map,
3668 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3669 }
3670 
3671 static void
3672 alc_init_smb(struct alc_softc *sc)
3673 {
3674 	struct alc_ring_data *rd;
3675 
3676 	ALC_LOCK_ASSERT(sc);
3677 
3678 	rd = &sc->alc_rdata;
3679 	bzero(rd->alc_smb, ALC_SMB_SZ);
3680 	bus_dmamap_sync(sc->alc_cdata.alc_smb_tag, sc->alc_cdata.alc_smb_map,
3681 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3682 }
3683 
3684 static void
3685 alc_rxvlan(struct alc_softc *sc)
3686 {
3687 	struct ifnet *ifp;
3688 	uint32_t reg;
3689 
3690 	ALC_LOCK_ASSERT(sc);
3691 
3692 	ifp = sc->alc_ifp;
3693 	reg = CSR_READ_4(sc, ALC_MAC_CFG);
3694 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
3695 		reg |= MAC_CFG_VLAN_TAG_STRIP;
3696 	else
3697 		reg &= ~MAC_CFG_VLAN_TAG_STRIP;
3698 	CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3699 }
3700 
3701 static void
3702 alc_rxfilter(struct alc_softc *sc)
3703 {
3704 	struct ifnet *ifp;
3705 	struct ifmultiaddr *ifma;
3706 	uint32_t crc;
3707 	uint32_t mchash[2];
3708 	uint32_t rxcfg;
3709 
3710 	ALC_LOCK_ASSERT(sc);
3711 
3712 	ifp = sc->alc_ifp;
3713 
3714 	bzero(mchash, sizeof(mchash));
3715 	rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
3716 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
3717 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
3718 		rxcfg |= MAC_CFG_BCAST;
3719 	if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
3720 		if ((ifp->if_flags & IFF_PROMISC) != 0)
3721 			rxcfg |= MAC_CFG_PROMISC;
3722 		if ((ifp->if_flags & IFF_ALLMULTI) != 0)
3723 			rxcfg |= MAC_CFG_ALLMULTI;
3724 		mchash[0] = 0xFFFFFFFF;
3725 		mchash[1] = 0xFFFFFFFF;
3726 		goto chipit;
3727 	}
3728 
3729 	if_maddr_rlock(ifp);
3730 	TAILQ_FOREACH(ifma, &sc->alc_ifp->if_multiaddrs, ifma_link) {
3731 		if (ifma->ifma_addr->sa_family != AF_LINK)
3732 			continue;
3733 		crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3734 		    ifma->ifma_addr), ETHER_ADDR_LEN);
3735 		mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
3736 	}
3737 	if_maddr_runlock(ifp);
3738 
3739 chipit:
3740 	CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
3741 	CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
3742 	CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
3743 }
3744 
3745 static int
3746 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3747 {
3748 	int error, value;
3749 
3750 	if (arg1 == NULL)
3751 		return (EINVAL);
3752 	value = *(int *)arg1;
3753 	error = sysctl_handle_int(oidp, &value, 0, req);
3754 	if (error || req->newptr == NULL)
3755 		return (error);
3756 	if (value < low || value > high)
3757 		return (EINVAL);
3758 	*(int *)arg1 = value;
3759 
3760 	return (0);
3761 }
3762 
3763 static int
3764 sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS)
3765 {
3766 	return (sysctl_int_range(oidp, arg1, arg2, req,
3767 	    ALC_PROC_MIN, ALC_PROC_MAX));
3768 }
3769 
3770 static int
3771 sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS)
3772 {
3773 
3774 	return (sysctl_int_range(oidp, arg1, arg2, req,
3775 	    ALC_IM_TIMER_MIN, ALC_IM_TIMER_MAX));
3776 }
3777