2 * Copyright (c) 2009, Pyun YongHyeon <yongari@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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
28 /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.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>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_llc.h>
56 #include <net/if_media.h>
57 #include <net/if_types.h>
58 #include <net/if_vlan_var.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/tcp.h>
65 #include <dev/mii/mii.h>
66 #include <dev/mii/miivar.h>
68 #include <dev/pci/pcireg.h>
69 #include <dev/pci/pcivar.h>
71 #include <machine/bus.h>
72 #include <machine/in_cksum.h>
74 #include <dev/alc/if_alcreg.h>
75 #include <dev/alc/if_alcvar.h>
77 /* "device miibus" required. See GENERIC if you get errors here. */
78 #include "miibus_if.h"
79 #undef ALC_USE_CUSTOM_CSUM
81 #ifdef ALC_USE_CUSTOM_CSUM
82 #define ALC_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
84 #define ALC_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
87 MODULE_DEPEND(alc, pci, 1, 1, 1);
88 MODULE_DEPEND(alc, ether, 1, 1, 1);
89 MODULE_DEPEND(alc, miibus, 1, 1, 1);
92 static int msi_disable = 0;
93 static int msix_disable = 0;
94 TUNABLE_INT("hw.alc.msi_disable", &msi_disable);
95 TUNABLE_INT("hw.alc.msix_disable", &msix_disable);
98 * Devices supported by this driver.
100 static struct alc_ident alc_ident_table[] = {
101 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8131, 9 * 1024,
102 "Atheros AR8131 PCIe Gigabit Ethernet" },
103 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8132, 9 * 1024,
104 "Atheros AR8132 PCIe Fast Ethernet" },
105 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151, 6 * 1024,
106 "Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
107 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151_V2, 6 * 1024,
108 "Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
109 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B, 6 * 1024,
110 "Atheros AR8152 v1.1 PCIe Fast Ethernet" },
111 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B2, 6 * 1024,
112 "Atheros AR8152 v2.0 PCIe Fast Ethernet" },
113 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8161, 9 * 1024,
114 "Atheros AR8161 PCIe Gigabit Ethernet" },
115 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8162, 9 * 1024,
116 "Atheros AR8162 PCIe Fast Ethernet" },
117 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8171, 9 * 1024,
118 "Atheros AR8171 PCIe Gigabit Ethernet" },
119 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8172, 9 * 1024,
120 "Atheros AR8172 PCIe Fast Ethernet" },
121 { VENDORID_ATHEROS, DEVICEID_ATHEROS_E2200, 9 * 1024,
122 "Killer E2200 Gigabit Ethernet" },
126 static void alc_aspm(struct alc_softc *, int, int);
127 static void alc_aspm_813x(struct alc_softc *, int);
128 static void alc_aspm_816x(struct alc_softc *, int);
129 static int alc_attach(device_t);
130 static int alc_check_boundary(struct alc_softc *);
131 static void alc_config_msi(struct alc_softc *);
132 static int alc_detach(device_t);
133 static void alc_disable_l0s_l1(struct alc_softc *);
134 static int alc_dma_alloc(struct alc_softc *);
135 static void alc_dma_free(struct alc_softc *);
136 static void alc_dmamap_cb(void *, bus_dma_segment_t *, int, int);
137 static void alc_dsp_fixup(struct alc_softc *, int);
138 static int alc_encap(struct alc_softc *, struct mbuf **);
139 static struct alc_ident *
140 alc_find_ident(device_t);
141 #ifndef __NO_STRICT_ALIGNMENT
143 alc_fixup_rx(struct ifnet *, struct mbuf *);
145 static void alc_get_macaddr(struct alc_softc *);
146 static void alc_get_macaddr_813x(struct alc_softc *);
147 static void alc_get_macaddr_816x(struct alc_softc *);
148 static void alc_get_macaddr_par(struct alc_softc *);
149 static void alc_init(void *);
150 static void alc_init_cmb(struct alc_softc *);
151 static void alc_init_locked(struct alc_softc *);
152 static void alc_init_rr_ring(struct alc_softc *);
153 static int alc_init_rx_ring(struct alc_softc *);
154 static void alc_init_smb(struct alc_softc *);
155 static void alc_init_tx_ring(struct alc_softc *);
156 static void alc_int_task(void *, int);
157 static int alc_intr(void *);
158 static int alc_ioctl(struct ifnet *, u_long, caddr_t);
159 static void alc_mac_config(struct alc_softc *);
160 static uint32_t alc_mii_readreg_813x(struct alc_softc *, int, int);
161 static uint32_t alc_mii_readreg_816x(struct alc_softc *, int, int);
162 static uint32_t alc_mii_writereg_813x(struct alc_softc *, int, int, int);
163 static uint32_t alc_mii_writereg_816x(struct alc_softc *, int, int, int);
164 static int alc_miibus_readreg(device_t, int, int);
165 static void alc_miibus_statchg(device_t);
166 static int alc_miibus_writereg(device_t, int, int, int);
167 static uint32_t alc_miidbg_readreg(struct alc_softc *, int);
168 static uint32_t alc_miidbg_writereg(struct alc_softc *, int, int);
169 static uint32_t alc_miiext_readreg(struct alc_softc *, int, int);
170 static uint32_t alc_miiext_writereg(struct alc_softc *, int, int, int);
171 static int alc_mediachange(struct ifnet *);
172 static int alc_mediachange_locked(struct alc_softc *);
173 static void alc_mediastatus(struct ifnet *, struct ifmediareq *);
174 static int alc_newbuf(struct alc_softc *, struct alc_rxdesc *);
175 static void alc_osc_reset(struct alc_softc *);
176 static void alc_phy_down(struct alc_softc *);
177 static void alc_phy_reset(struct alc_softc *);
178 static void alc_phy_reset_813x(struct alc_softc *);
179 static void alc_phy_reset_816x(struct alc_softc *);
180 static int alc_probe(device_t);
181 static void alc_reset(struct alc_softc *);
182 static int alc_resume(device_t);
183 static void alc_rxeof(struct alc_softc *, struct rx_rdesc *);
184 static int alc_rxintr(struct alc_softc *, int);
185 static void alc_rxfilter(struct alc_softc *);
186 static void alc_rxvlan(struct alc_softc *);
187 static void alc_setlinkspeed(struct alc_softc *);
188 static void alc_setwol(struct alc_softc *);
189 static void alc_setwol_813x(struct alc_softc *);
190 static void alc_setwol_816x(struct alc_softc *);
191 static int alc_shutdown(device_t);
192 static void alc_start(struct ifnet *);
193 static void alc_start_locked(struct ifnet *);
194 static void alc_start_queue(struct alc_softc *);
195 static void alc_stats_clear(struct alc_softc *);
196 static void alc_stats_update(struct alc_softc *);
197 static void alc_stop(struct alc_softc *);
198 static void alc_stop_mac(struct alc_softc *);
199 static void alc_stop_queue(struct alc_softc *);
200 static int alc_suspend(device_t);
201 static void alc_sysctl_node(struct alc_softc *);
202 static void alc_tick(void *);
203 static void alc_txeof(struct alc_softc *);
204 static void alc_watchdog(struct alc_softc *);
205 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
206 static int sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS);
207 static int sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS);
209 static device_method_t alc_methods[] = {
210 /* Device interface. */
211 DEVMETHOD(device_probe, alc_probe),
212 DEVMETHOD(device_attach, alc_attach),
213 DEVMETHOD(device_detach, alc_detach),
214 DEVMETHOD(device_shutdown, alc_shutdown),
215 DEVMETHOD(device_suspend, alc_suspend),
216 DEVMETHOD(device_resume, alc_resume),
219 DEVMETHOD(miibus_readreg, alc_miibus_readreg),
220 DEVMETHOD(miibus_writereg, alc_miibus_writereg),
221 DEVMETHOD(miibus_statchg, alc_miibus_statchg),
226 static driver_t alc_driver = {
229 sizeof(struct alc_softc)
232 static devclass_t alc_devclass;
234 DRIVER_MODULE(alc, pci, alc_driver, alc_devclass, 0, 0);
235 DRIVER_MODULE(miibus, alc, miibus_driver, miibus_devclass, 0, 0);
237 static struct resource_spec alc_res_spec_mem[] = {
238 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
242 static struct resource_spec alc_irq_spec_legacy[] = {
243 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
247 static struct resource_spec alc_irq_spec_msi[] = {
248 { SYS_RES_IRQ, 1, RF_ACTIVE },
252 static struct resource_spec alc_irq_spec_msix[] = {
253 { SYS_RES_IRQ, 1, RF_ACTIVE },
257 static uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0 };
260 alc_miibus_readreg(device_t dev, int phy, int reg)
262 struct alc_softc *sc;
265 sc = device_get_softc(dev);
266 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
267 v = alc_mii_readreg_816x(sc, phy, reg);
269 v = alc_mii_readreg_813x(sc, phy, reg);
274 alc_mii_readreg_813x(struct alc_softc *sc, int phy, int reg)
280 * For AR8132 fast ethernet controller, do not report 1000baseT
281 * capability to mii(4). Even though AR8132 uses the same
282 * model/revision number of F1 gigabit PHY, the PHY has no
283 * ability to establish 1000baseT link.
285 if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
289 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
290 MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
291 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
293 v = CSR_READ_4(sc, ALC_MDIO);
294 if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
299 device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
303 return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
307 alc_mii_readreg_816x(struct alc_softc *sc, int phy, int reg)
312 if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
313 clk = MDIO_CLK_25_128;
316 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
317 MDIO_SUP_PREAMBLE | clk | MDIO_REG_ADDR(reg));
318 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
320 v = CSR_READ_4(sc, ALC_MDIO);
321 if ((v & MDIO_OP_BUSY) == 0)
326 device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
330 return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
334 alc_miibus_writereg(device_t dev, int phy, int reg, int val)
336 struct alc_softc *sc;
339 sc = device_get_softc(dev);
340 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
341 v = alc_mii_writereg_816x(sc, phy, reg, val);
343 v = alc_mii_writereg_813x(sc, phy, reg, val);
348 alc_mii_writereg_813x(struct alc_softc *sc, int phy, int reg, int val)
353 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
354 (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
355 MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
356 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
358 v = CSR_READ_4(sc, ALC_MDIO);
359 if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
364 device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
370 alc_mii_writereg_816x(struct alc_softc *sc, int phy, int reg, int val)
375 if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
376 clk = MDIO_CLK_25_128;
379 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
380 ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) | MDIO_REG_ADDR(reg) |
381 MDIO_SUP_PREAMBLE | clk);
382 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
384 v = CSR_READ_4(sc, ALC_MDIO);
385 if ((v & MDIO_OP_BUSY) == 0)
390 device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
396 alc_miibus_statchg(device_t dev)
398 struct alc_softc *sc;
399 struct mii_data *mii;
403 sc = device_get_softc(dev);
405 mii = device_get_softc(sc->alc_miibus);
407 if (mii == NULL || ifp == NULL ||
408 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
411 sc->alc_flags &= ~ALC_FLAG_LINK;
412 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
413 (IFM_ACTIVE | IFM_AVALID)) {
414 switch (IFM_SUBTYPE(mii->mii_media_active)) {
417 sc->alc_flags |= ALC_FLAG_LINK;
420 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
421 sc->alc_flags |= ALC_FLAG_LINK;
427 /* Stop Rx/Tx MACs. */
430 /* Program MACs with resolved speed/duplex/flow-control. */
431 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
434 /* Re-enable Tx/Rx MACs. */
435 reg = CSR_READ_4(sc, ALC_MAC_CFG);
436 reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
437 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
439 alc_aspm(sc, 0, IFM_SUBTYPE(mii->mii_media_active));
440 alc_dsp_fixup(sc, IFM_SUBTYPE(mii->mii_media_active));
444 alc_miidbg_readreg(struct alc_softc *sc, int reg)
447 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
449 return (alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
454 alc_miidbg_writereg(struct alc_softc *sc, int reg, int val)
457 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
459 return (alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
460 ALC_MII_DBG_DATA, val));
464 alc_miiext_readreg(struct alc_softc *sc, int devaddr, int reg)
469 CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
470 EXT_MDIO_DEVADDR(devaddr));
471 if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
472 clk = MDIO_CLK_25_128;
475 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
476 MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
477 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
479 v = CSR_READ_4(sc, ALC_MDIO);
480 if ((v & MDIO_OP_BUSY) == 0)
485 device_printf(sc->alc_dev, "phy ext read timeout : %d, %d\n",
490 return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
494 alc_miiext_writereg(struct alc_softc *sc, int devaddr, int reg, int val)
499 CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
500 EXT_MDIO_DEVADDR(devaddr));
501 if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
502 clk = MDIO_CLK_25_128;
505 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
506 ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) |
507 MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
508 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
510 v = CSR_READ_4(sc, ALC_MDIO);
511 if ((v & MDIO_OP_BUSY) == 0)
516 device_printf(sc->alc_dev, "phy ext write timeout : %d, %d\n",
523 alc_dsp_fixup(struct alc_softc *sc, int media)
525 uint16_t agc, len, val;
527 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
529 if (AR816X_REV(sc->alc_rev) >= AR816X_REV_C0)
534 * 1000BT/AZ, wrong cable length
536 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
537 len = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL6);
538 len = (len >> EXT_CLDCTL6_CAB_LEN_SHIFT) &
539 EXT_CLDCTL6_CAB_LEN_MASK;
540 agc = alc_miidbg_readreg(sc, MII_DBG_AGC);
541 agc = (agc >> DBG_AGC_2_VGA_SHIFT) & DBG_AGC_2_VGA_MASK;
542 if ((media == IFM_1000_T && len > EXT_CLDCTL6_CAB_LEN_SHORT1G &&
543 agc > DBG_AGC_LONG1G_LIMT) ||
544 (media == IFM_100_TX && len > DBG_AGC_LONG100M_LIMT &&
545 agc > DBG_AGC_LONG1G_LIMT)) {
546 alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
547 DBG_AZ_ANADECT_LONG);
548 val = alc_miiext_readreg(sc, MII_EXT_ANEG,
550 val |= ANEG_AFEE_10BT_100M_TH;
551 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
554 alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
555 DBG_AZ_ANADECT_DEFAULT);
556 val = alc_miiext_readreg(sc, MII_EXT_ANEG,
558 val &= ~ANEG_AFEE_10BT_100M_TH;
559 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
562 if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
563 AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
564 if (media == IFM_1000_T) {
566 * Giga link threshold, raise the tolerance of
569 val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
570 val &= ~DBG_MSE20DB_TH_MASK;
571 val |= (DBG_MSE20DB_TH_HI <<
572 DBG_MSE20DB_TH_SHIFT);
573 alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
574 } else if (media == IFM_100_TX)
575 alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
579 val = alc_miiext_readreg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE);
580 val &= ~ANEG_AFEE_10BT_100M_TH;
581 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, val);
582 if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
583 AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
584 alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
586 val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
587 val &= ~DBG_MSE20DB_TH_MASK;
588 val |= (DBG_MSE20DB_TH_DEFAULT << DBG_MSE20DB_TH_SHIFT);
589 alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
595 alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
597 struct alc_softc *sc;
598 struct mii_data *mii;
602 if ((ifp->if_flags & IFF_UP) == 0) {
606 mii = device_get_softc(sc->alc_miibus);
609 ifmr->ifm_status = mii->mii_media_status;
610 ifmr->ifm_active = mii->mii_media_active;
615 alc_mediachange(struct ifnet *ifp)
617 struct alc_softc *sc;
622 error = alc_mediachange_locked(sc);
629 alc_mediachange_locked(struct alc_softc *sc)
631 struct mii_data *mii;
632 struct mii_softc *miisc;
637 mii = device_get_softc(sc->alc_miibus);
638 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
640 error = mii_mediachg(mii);
645 static struct alc_ident *
646 alc_find_ident(device_t dev)
648 struct alc_ident *ident;
649 uint16_t vendor, devid;
651 vendor = pci_get_vendor(dev);
652 devid = pci_get_device(dev);
653 for (ident = alc_ident_table; ident->name != NULL; ident++) {
654 if (vendor == ident->vendorid && devid == ident->deviceid)
662 alc_probe(device_t dev)
664 struct alc_ident *ident;
666 ident = alc_find_ident(dev);
668 device_set_desc(dev, ident->name);
669 return (BUS_PROBE_DEFAULT);
676 alc_get_macaddr(struct alc_softc *sc)
679 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
680 alc_get_macaddr_816x(sc);
682 alc_get_macaddr_813x(sc);
686 alc_get_macaddr_813x(struct alc_softc *sc)
693 opt = CSR_READ_4(sc, ALC_OPT_CFG);
694 if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
695 (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
697 * EEPROM found, let TWSI reload EEPROM configuration.
698 * This will set ethernet address of controller.
701 switch (sc->alc_ident->deviceid) {
702 case DEVICEID_ATHEROS_AR8131:
703 case DEVICEID_ATHEROS_AR8132:
704 if ((opt & OPT_CFG_CLK_ENB) == 0) {
705 opt |= OPT_CFG_CLK_ENB;
706 CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
707 CSR_READ_4(sc, ALC_OPT_CFG);
711 case DEVICEID_ATHEROS_AR8151:
712 case DEVICEID_ATHEROS_AR8151_V2:
713 case DEVICEID_ATHEROS_AR8152_B:
714 case DEVICEID_ATHEROS_AR8152_B2:
715 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
716 ALC_MII_DBG_ADDR, 0x00);
717 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
719 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
720 ALC_MII_DBG_DATA, val & 0xFF7F);
721 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
722 ALC_MII_DBG_ADDR, 0x3B);
723 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
725 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
726 ALC_MII_DBG_DATA, val | 0x0008);
731 CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
732 CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
733 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
734 CSR_READ_4(sc, ALC_WOL_CFG);
736 CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
737 TWSI_CFG_SW_LD_START);
738 for (i = 100; i > 0; i--) {
740 if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
741 TWSI_CFG_SW_LD_START) == 0)
745 device_printf(sc->alc_dev,
746 "reloading EEPROM timeout!\n");
749 device_printf(sc->alc_dev, "EEPROM not found!\n");
752 switch (sc->alc_ident->deviceid) {
753 case DEVICEID_ATHEROS_AR8131:
754 case DEVICEID_ATHEROS_AR8132:
755 if ((opt & OPT_CFG_CLK_ENB) != 0) {
756 opt &= ~OPT_CFG_CLK_ENB;
757 CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
758 CSR_READ_4(sc, ALC_OPT_CFG);
762 case DEVICEID_ATHEROS_AR8151:
763 case DEVICEID_ATHEROS_AR8151_V2:
764 case DEVICEID_ATHEROS_AR8152_B:
765 case DEVICEID_ATHEROS_AR8152_B2:
766 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
767 ALC_MII_DBG_ADDR, 0x00);
768 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
770 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
771 ALC_MII_DBG_DATA, val | 0x0080);
772 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
773 ALC_MII_DBG_ADDR, 0x3B);
774 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
776 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
777 ALC_MII_DBG_DATA, val & 0xFFF7);
783 alc_get_macaddr_par(sc);
787 alc_get_macaddr_816x(struct alc_softc *sc)
793 /* Try to reload station address via TWSI. */
794 for (i = 100; i > 0; i--) {
795 reg = CSR_READ_4(sc, ALC_SLD);
796 if ((reg & (SLD_PROGRESS | SLD_START)) == 0)
801 CSR_WRITE_4(sc, ALC_SLD, reg | SLD_START);
802 for (i = 100; i > 0; i--) {
804 reg = CSR_READ_4(sc, ALC_SLD);
805 if ((reg & SLD_START) == 0)
810 else if (bootverbose)
811 device_printf(sc->alc_dev,
812 "reloading station address via TWSI timed out!\n");
815 /* Try to reload station address from EEPROM or FLASH. */
817 reg = CSR_READ_4(sc, ALC_EEPROM_LD);
818 if ((reg & (EEPROM_LD_EEPROM_EXIST |
819 EEPROM_LD_FLASH_EXIST)) != 0) {
820 for (i = 100; i > 0; i--) {
821 reg = CSR_READ_4(sc, ALC_EEPROM_LD);
822 if ((reg & (EEPROM_LD_PROGRESS |
823 EEPROM_LD_START)) == 0)
828 CSR_WRITE_4(sc, ALC_EEPROM_LD, reg |
830 for (i = 100; i > 0; i--) {
832 reg = CSR_READ_4(sc, ALC_EEPROM_LD);
833 if ((reg & EEPROM_LD_START) == 0)
836 } else if (bootverbose)
837 device_printf(sc->alc_dev,
838 "reloading EEPROM/FLASH timed out!\n");
842 alc_get_macaddr_par(sc);
846 alc_get_macaddr_par(struct alc_softc *sc)
850 ea[0] = CSR_READ_4(sc, ALC_PAR0);
851 ea[1] = CSR_READ_4(sc, ALC_PAR1);
852 sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
853 sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
854 sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
855 sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
856 sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
857 sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
861 alc_disable_l0s_l1(struct alc_softc *sc)
865 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
866 /* Another magic from vendor. */
867 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
868 pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
869 PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
870 PM_CFG_MAC_ASPM_CHK | PM_CFG_SERDES_PD_EX_L1);
871 pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB |
872 PM_CFG_SERDES_PLL_L1_ENB | PM_CFG_SERDES_L1_ENB;
873 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
878 alc_phy_reset(struct alc_softc *sc)
881 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
882 alc_phy_reset_816x(sc);
884 alc_phy_reset_813x(sc);
888 alc_phy_reset_813x(struct alc_softc *sc)
892 /* Reset magic from Linux. */
893 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
894 CSR_READ_2(sc, ALC_GPHY_CFG);
897 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
898 GPHY_CFG_SEL_ANA_RESET);
899 CSR_READ_2(sc, ALC_GPHY_CFG);
902 /* DSP fixup, Vendor magic. */
903 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
904 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
905 ALC_MII_DBG_ADDR, 0x000A);
906 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
908 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
909 ALC_MII_DBG_DATA, data & 0xDFFF);
911 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
912 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
913 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
914 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
915 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
916 ALC_MII_DBG_ADDR, 0x003B);
917 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
919 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
920 ALC_MII_DBG_DATA, data & 0xFFF7);
923 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151) {
924 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
925 ALC_MII_DBG_ADDR, 0x0029);
926 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
927 ALC_MII_DBG_DATA, 0x929D);
929 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
930 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132 ||
931 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
932 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
933 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
934 ALC_MII_DBG_ADDR, 0x0029);
935 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
936 ALC_MII_DBG_DATA, 0xB6DD);
939 /* Load DSP codes, vendor magic. */
940 data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
941 ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
942 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
943 ALC_MII_DBG_ADDR, MII_ANA_CFG18);
944 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
945 ALC_MII_DBG_DATA, data);
947 data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
948 ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
950 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
951 ALC_MII_DBG_ADDR, MII_ANA_CFG5);
952 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
953 ALC_MII_DBG_DATA, data);
955 data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
956 ANA_LONG_CABLE_TH_100_MASK) |
957 ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
958 ANA_SHORT_CABLE_TH_100_SHIFT) |
959 ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
960 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
961 ALC_MII_DBG_ADDR, MII_ANA_CFG54);
962 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
963 ALC_MII_DBG_DATA, data);
965 data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
966 ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
967 ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
968 ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
969 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
970 ALC_MII_DBG_ADDR, MII_ANA_CFG4);
971 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
972 ALC_MII_DBG_DATA, data);
974 data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
975 ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
977 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
978 ALC_MII_DBG_ADDR, MII_ANA_CFG0);
979 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
980 ALC_MII_DBG_DATA, data);
983 /* Disable hibernation. */
984 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
986 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
989 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
992 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
994 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
997 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
1002 alc_phy_reset_816x(struct alc_softc *sc)
1006 val = CSR_READ_4(sc, ALC_GPHY_CFG);
1007 val &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
1008 GPHY_CFG_GATE_25M_ENB | GPHY_CFG_PHY_IDDQ | GPHY_CFG_PHY_PLL_ON |
1009 GPHY_CFG_PWDOWN_HW | GPHY_CFG_100AB_ENB);
1010 val |= GPHY_CFG_SEL_ANA_RESET;
1012 val |= GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN | GPHY_CFG_SEL_ANA_RESET;
1014 /* Disable PHY hibernation. */
1015 val &= ~(GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN);
1017 CSR_WRITE_4(sc, ALC_GPHY_CFG, val);
1019 CSR_WRITE_4(sc, ALC_GPHY_CFG, val | GPHY_CFG_EXT_RESET);
1022 /* Vendor PHY magic. */
1024 alc_miidbg_writereg(sc, MII_DBG_LEGCYPS, DBG_LEGCYPS_DEFAULT);
1025 alc_miidbg_writereg(sc, MII_DBG_SYSMODCTL, DBG_SYSMODCTL_DEFAULT);
1026 alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_VDRVBIAS,
1027 EXT_VDRVBIAS_DEFAULT);
1029 /* Disable PHY hibernation. */
1030 alc_miidbg_writereg(sc, MII_DBG_LEGCYPS,
1031 DBG_LEGCYPS_DEFAULT & ~DBG_LEGCYPS_ENB);
1032 alc_miidbg_writereg(sc, MII_DBG_HIBNEG,
1033 DBG_HIBNEG_DEFAULT & ~(DBG_HIBNEG_PSHIB_EN | DBG_HIBNEG_HIB_PULSE));
1034 alc_miidbg_writereg(sc, MII_DBG_GREENCFG, DBG_GREENCFG_DEFAULT);
1037 /* XXX Disable EEE. */
1038 val = CSR_READ_4(sc, ALC_LPI_CTL);
1039 val &= ~LPI_CTL_ENB;
1040 CSR_WRITE_4(sc, ALC_LPI_CTL, val);
1041 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_LOCAL_EEEADV, 0);
1043 /* PHY power saving. */
1044 alc_miidbg_writereg(sc, MII_DBG_TST10BTCFG, DBG_TST10BTCFG_DEFAULT);
1045 alc_miidbg_writereg(sc, MII_DBG_SRDSYSMOD, DBG_SRDSYSMOD_DEFAULT);
1046 alc_miidbg_writereg(sc, MII_DBG_TST100BTCFG, DBG_TST100BTCFG_DEFAULT);
1047 alc_miidbg_writereg(sc, MII_DBG_ANACTL, DBG_ANACTL_DEFAULT);
1048 val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
1049 val &= ~DBG_GREENCFG2_GATE_DFSE_EN;
1050 alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
1052 /* RTL8139C, 120m issue. */
1053 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_NLP78,
1054 ANEG_NLP78_120M_DEFAULT);
1055 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
1056 ANEG_S3DIG10_DEFAULT);
1058 if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0) {
1059 /* Turn off half amplitude. */
1060 val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3);
1061 val |= EXT_CLDCTL3_BP_CABLE1TH_DET_GT;
1062 alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3, val);
1063 /* Turn off Green feature. */
1064 val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
1065 val |= DBG_GREENCFG2_BP_GREEN;
1066 alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
1067 /* Turn off half bias. */
1068 val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5);
1069 val |= EXT_CLDCTL5_BP_VD_HLFBIAS;
1070 alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5, val);
1075 alc_phy_down(struct alc_softc *sc)
1079 switch (sc->alc_ident->deviceid) {
1080 case DEVICEID_ATHEROS_AR8161:
1081 case DEVICEID_ATHEROS_E2200:
1082 case DEVICEID_ATHEROS_AR8162:
1083 case DEVICEID_ATHEROS_AR8171:
1084 case DEVICEID_ATHEROS_AR8172:
1085 gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
1086 gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
1087 GPHY_CFG_100AB_ENB | GPHY_CFG_PHY_PLL_ON);
1088 gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE |
1089 GPHY_CFG_SEL_ANA_RESET;
1090 gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
1091 CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
1093 case DEVICEID_ATHEROS_AR8151:
1094 case DEVICEID_ATHEROS_AR8151_V2:
1095 case DEVICEID_ATHEROS_AR8152_B:
1096 case DEVICEID_ATHEROS_AR8152_B2:
1098 * GPHY power down caused more problems on AR8151 v2.0.
1099 * When driver is reloaded after GPHY power down,
1100 * accesses to PHY/MAC registers hung the system. Only
1101 * cold boot recovered from it. I'm not sure whether
1102 * AR8151 v1.0 also requires this one though. I don't
1103 * have AR8151 v1.0 controller in hand.
1104 * The only option left is to isolate the PHY and
1105 * initiates power down the PHY which in turn saves
1106 * more power when driver is unloaded.
1108 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
1109 MII_BMCR, BMCR_ISO | BMCR_PDOWN);
1112 /* Force PHY down. */
1113 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
1114 GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
1115 GPHY_CFG_PWDOWN_HW);
1122 alc_aspm(struct alc_softc *sc, int init, int media)
1125 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
1126 alc_aspm_816x(sc, init);
1128 alc_aspm_813x(sc, media);
1132 alc_aspm_813x(struct alc_softc *sc, int media)
1137 if ((sc->alc_flags & ALC_FLAG_LINK) == 0)
1140 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
1141 if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
1142 (ALC_FLAG_APS | ALC_FLAG_PCIE))
1143 linkcfg = CSR_READ_2(sc, sc->alc_expcap +
1147 pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
1148 pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
1149 pmcfg |= PM_CFG_MAC_ASPM_CHK;
1150 pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
1151 pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
1153 if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
1154 /* Disable extended sync except AR8152 B v1.0 */
1155 linkcfg &= ~PCIEM_LINK_CTL_EXTENDED_SYNC;
1156 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
1157 sc->alc_rev == ATHEROS_AR8152_B_V10)
1158 linkcfg |= PCIEM_LINK_CTL_EXTENDED_SYNC;
1159 CSR_WRITE_2(sc, sc->alc_expcap + PCIER_LINK_CTL,
1161 pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
1163 pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
1164 PM_CFG_L1_ENTRY_TIMER_SHIFT);
1165 pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
1166 pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
1167 PM_CFG_PM_REQ_TIMER_SHIFT);
1168 pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
1171 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
1172 if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
1173 pmcfg |= PM_CFG_ASPM_L0S_ENB;
1174 if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
1175 pmcfg |= PM_CFG_ASPM_L1_ENB;
1176 if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
1177 if (sc->alc_ident->deviceid ==
1178 DEVICEID_ATHEROS_AR8152_B)
1179 pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
1180 pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
1181 PM_CFG_SERDES_PLL_L1_ENB |
1182 PM_CFG_SERDES_BUDS_RX_L1_ENB);
1183 pmcfg |= PM_CFG_CLK_SWH_L1;
1184 if (media == IFM_100_TX || media == IFM_1000_T) {
1185 pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
1186 switch (sc->alc_ident->deviceid) {
1187 case DEVICEID_ATHEROS_AR8152_B:
1189 PM_CFG_L1_ENTRY_TIMER_SHIFT);
1191 case DEVICEID_ATHEROS_AR8152_B2:
1192 case DEVICEID_ATHEROS_AR8151_V2:
1194 PM_CFG_L1_ENTRY_TIMER_SHIFT);
1198 PM_CFG_L1_ENTRY_TIMER_SHIFT);
1203 pmcfg |= PM_CFG_SERDES_L1_ENB |
1204 PM_CFG_SERDES_PLL_L1_ENB |
1205 PM_CFG_SERDES_BUDS_RX_L1_ENB;
1206 pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
1207 PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
1210 pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
1211 PM_CFG_SERDES_PLL_L1_ENB);
1212 pmcfg |= PM_CFG_CLK_SWH_L1;
1213 if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
1214 pmcfg |= PM_CFG_ASPM_L1_ENB;
1216 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1220 alc_aspm_816x(struct alc_softc *sc, int init)
1224 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
1225 pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_816X_MASK;
1226 pmcfg |= PM_CFG_L1_ENTRY_TIMER_816X_DEFAULT;
1227 pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
1228 pmcfg |= PM_CFG_PM_REQ_TIMER_816X_DEFAULT;
1229 pmcfg &= ~PM_CFG_LCKDET_TIMER_MASK;
1230 pmcfg |= PM_CFG_LCKDET_TIMER_DEFAULT;
1231 pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_CLK_SWH_L1 | PM_CFG_PCIE_RECV;
1232 pmcfg &= ~(PM_CFG_RX_L1_AFTER_L0S | PM_CFG_TX_L1_AFTER_L0S |
1233 PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB |
1234 PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
1235 PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SA_DLY_ENB |
1236 PM_CFG_MAC_ASPM_CHK | PM_CFG_HOTRST);
1237 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1238 (sc->alc_rev & 0x01) != 0)
1239 pmcfg |= PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB;
1240 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
1241 /* Link up, enable both L0s, L1s. */
1242 pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1243 PM_CFG_MAC_ASPM_CHK;
1246 pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1247 PM_CFG_MAC_ASPM_CHK;
1248 else if ((sc->alc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1249 pmcfg |= PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK;
1251 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1255 alc_init_pcie(struct alc_softc *sc)
1257 const char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
1258 uint32_t cap, ctl, val;
1261 /* Clear data link and flow-control protocol error. */
1262 val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
1263 val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
1264 CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
1266 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1267 CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
1268 CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
1269 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
1270 CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
1271 PCIE_PHYMISC_FORCE_RCV_DET);
1272 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
1273 sc->alc_rev == ATHEROS_AR8152_B_V10) {
1274 val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
1275 val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
1276 PCIE_PHYMISC2_SERDES_TH_MASK);
1277 val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
1278 val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
1279 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
1281 /* Disable ASPM L0S and L1. */
1282 cap = CSR_READ_2(sc, sc->alc_expcap + PCIER_LINK_CAP);
1283 if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1284 ctl = CSR_READ_2(sc, sc->alc_expcap + PCIER_LINK_CTL);
1285 if ((ctl & PCIEM_LINK_CTL_RCB) != 0)
1286 sc->alc_rcb = DMA_CFG_RCB_128;
1288 device_printf(sc->alc_dev, "RCB %u bytes\n",
1289 sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
1290 state = ctl & PCIEM_LINK_CTL_ASPMC;
1291 if (state & PCIEM_LINK_CTL_ASPMC_L0S)
1292 sc->alc_flags |= ALC_FLAG_L0S;
1293 if (state & PCIEM_LINK_CTL_ASPMC_L1)
1294 sc->alc_flags |= ALC_FLAG_L1S;
1296 device_printf(sc->alc_dev, "ASPM %s %s\n",
1298 state == 0 ? "disabled" : "enabled");
1299 alc_disable_l0s_l1(sc);
1302 device_printf(sc->alc_dev,
1303 "no ASPM support\n");
1306 val = CSR_READ_4(sc, ALC_PDLL_TRNS1);
1307 val &= ~PDLL_TRNS1_D3PLLOFF_ENB;
1308 CSR_WRITE_4(sc, ALC_PDLL_TRNS1, val);
1309 val = CSR_READ_4(sc, ALC_MASTER_CFG);
1310 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1311 (sc->alc_rev & 0x01) != 0) {
1312 if ((val & MASTER_WAKEN_25M) == 0 ||
1313 (val & MASTER_CLK_SEL_DIS) == 0) {
1314 val |= MASTER_WAKEN_25M | MASTER_CLK_SEL_DIS;
1315 CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1318 if ((val & MASTER_WAKEN_25M) == 0 ||
1319 (val & MASTER_CLK_SEL_DIS) != 0) {
1320 val |= MASTER_WAKEN_25M;
1321 val &= ~MASTER_CLK_SEL_DIS;
1322 CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1326 alc_aspm(sc, 1, IFM_UNKNOWN);
1330 alc_config_msi(struct alc_softc *sc)
1334 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
1336 * It seems interrupt moderation is controlled by
1337 * ALC_MSI_RETRANS_TIMER register if MSI/MSIX is active.
1338 * Driver uses RX interrupt moderation parameter to
1339 * program ALC_MSI_RETRANS_TIMER register.
1341 ctl = CSR_READ_4(sc, ALC_MSI_RETRANS_TIMER);
1342 ctl &= ~MSI_RETRANS_TIMER_MASK;
1343 ctl &= ~MSI_RETRANS_MASK_SEL_LINE;
1344 mod = ALC_USECS(sc->alc_int_rx_mod);
1348 if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1349 CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, ctl |
1350 MSI_RETRANS_MASK_SEL_STD);
1351 else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1352 CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, ctl |
1353 MSI_RETRANS_MASK_SEL_LINE);
1355 CSR_WRITE_4(sc, ALC_MSI_RETRANS_TIMER, 0);
1360 alc_attach(device_t dev)
1362 struct alc_softc *sc;
1364 int base, error, i, msic, msixc;
1368 sc = device_get_softc(dev);
1370 sc->alc_rev = pci_get_revid(dev);
1372 mtx_init(&sc->alc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1374 callout_init_mtx(&sc->alc_tick_ch, &sc->alc_mtx, 0);
1375 TASK_INIT(&sc->alc_int_task, 0, alc_int_task, sc);
1376 sc->alc_ident = alc_find_ident(dev);
1378 /* Map the device. */
1379 pci_enable_busmaster(dev);
1380 sc->alc_res_spec = alc_res_spec_mem;
1381 sc->alc_irq_spec = alc_irq_spec_legacy;
1382 error = bus_alloc_resources(dev, sc->alc_res_spec, sc->alc_res);
1384 device_printf(dev, "cannot allocate memory resources.\n");
1388 /* Set PHY address. */
1389 sc->alc_phyaddr = ALC_PHY_ADDR;
1392 * One odd thing is AR8132 uses the same PHY hardware(F1
1393 * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
1394 * the PHY supports 1000Mbps but that's not true. The PHY
1395 * used in AR8132 can't establish gigabit link even if it
1396 * shows the same PHY model/revision number of AR8131.
1398 switch (sc->alc_ident->deviceid) {
1399 case DEVICEID_ATHEROS_AR8161:
1400 if (pci_get_subvendor(dev) == VENDORID_ATHEROS &&
1401 pci_get_subdevice(dev) == 0x0091 && sc->alc_rev == 0)
1402 sc->alc_flags |= ALC_FLAG_LINK_WAR;
1404 case DEVICEID_ATHEROS_E2200:
1405 case DEVICEID_ATHEROS_AR8171:
1406 sc->alc_flags |= ALC_FLAG_AR816X_FAMILY;
1408 case DEVICEID_ATHEROS_AR8162:
1409 case DEVICEID_ATHEROS_AR8172:
1410 sc->alc_flags |= ALC_FLAG_FASTETHER | ALC_FLAG_AR816X_FAMILY;
1412 case DEVICEID_ATHEROS_AR8152_B:
1413 case DEVICEID_ATHEROS_AR8152_B2:
1414 sc->alc_flags |= ALC_FLAG_APS;
1416 case DEVICEID_ATHEROS_AR8132:
1417 sc->alc_flags |= ALC_FLAG_FASTETHER;
1419 case DEVICEID_ATHEROS_AR8151:
1420 case DEVICEID_ATHEROS_AR8151_V2:
1421 sc->alc_flags |= ALC_FLAG_APS;
1426 sc->alc_flags |= ALC_FLAG_JUMBO;
1429 * It seems that AR813x/AR815x has silicon bug for SMB. In
1430 * addition, Atheros said that enabling SMB wouldn't improve
1431 * performance. However I think it's bad to access lots of
1432 * registers to extract MAC statistics.
1434 sc->alc_flags |= ALC_FLAG_SMB_BUG;
1436 * Don't use Tx CMB. It is known to have silicon bug.
1438 sc->alc_flags |= ALC_FLAG_CMB_BUG;
1439 sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
1440 MASTER_CHIP_REV_SHIFT;
1442 device_printf(dev, "PCI device revision : 0x%04x\n",
1444 device_printf(dev, "Chip id/revision : 0x%04x\n",
1446 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
1447 device_printf(dev, "AR816x revision : 0x%x\n",
1448 AR816X_REV(sc->alc_rev));
1450 device_printf(dev, "%u Tx FIFO, %u Rx FIFO\n",
1451 CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
1452 CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
1454 /* Initialize DMA parameters. */
1455 sc->alc_dma_rd_burst = 0;
1456 sc->alc_dma_wr_burst = 0;
1457 sc->alc_rcb = DMA_CFG_RCB_64;
1458 if (pci_find_cap(dev, PCIY_EXPRESS, &base) == 0) {
1459 sc->alc_flags |= ALC_FLAG_PCIE;
1460 sc->alc_expcap = base;
1461 burst = CSR_READ_2(sc, base + PCIER_DEVICE_CTL);
1462 sc->alc_dma_rd_burst =
1463 (burst & PCIEM_CTL_MAX_READ_REQUEST) >> 12;
1464 sc->alc_dma_wr_burst = (burst & PCIEM_CTL_MAX_PAYLOAD) >> 5;
1466 device_printf(dev, "Read request size : %u bytes.\n",
1467 alc_dma_burst[sc->alc_dma_rd_burst]);
1468 device_printf(dev, "TLP payload size : %u bytes.\n",
1469 alc_dma_burst[sc->alc_dma_wr_burst]);
1471 if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
1472 sc->alc_dma_rd_burst = 3;
1473 if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
1474 sc->alc_dma_wr_burst = 3;
1481 /* Reset the ethernet controller. */
1485 /* Allocate IRQ resources. */
1486 msixc = pci_msix_count(dev);
1487 msic = pci_msi_count(dev);
1489 device_printf(dev, "MSIX count : %d\n", msixc);
1490 device_printf(dev, "MSI count : %d\n", msic);
1497 * Prefer MSIX over MSI.
1498 * AR816x controller has a silicon bug that MSI interrupt
1499 * does not assert if PCIM_CMD_INTxDIS bit of command
1500 * register is set. pci(4) was taught to handle that case.
1502 if (msix_disable == 0 || msi_disable == 0) {
1503 if (msix_disable == 0 && msixc > 0 &&
1504 pci_alloc_msix(dev, &msixc) == 0) {
1507 "Using %d MSIX message(s).\n", msixc);
1508 sc->alc_flags |= ALC_FLAG_MSIX;
1509 sc->alc_irq_spec = alc_irq_spec_msix;
1511 pci_release_msi(dev);
1513 if (msi_disable == 0 && (sc->alc_flags & ALC_FLAG_MSIX) == 0 &&
1514 msic > 0 && pci_alloc_msi(dev, &msic) == 0) {
1517 "Using %d MSI message(s).\n", msic);
1518 sc->alc_flags |= ALC_FLAG_MSI;
1519 sc->alc_irq_spec = alc_irq_spec_msi;
1521 pci_release_msi(dev);
1525 error = bus_alloc_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1527 device_printf(dev, "cannot allocate IRQ resources.\n");
1531 /* Create device sysctl node. */
1532 alc_sysctl_node(sc);
1534 if ((error = alc_dma_alloc(sc) != 0))
1537 /* Load station address. */
1538 alc_get_macaddr(sc);
1540 ifp = sc->alc_ifp = if_alloc(IFT_ETHER);
1542 device_printf(dev, "cannot allocate ifnet structure.\n");
1548 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1549 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1550 ifp->if_ioctl = alc_ioctl;
1551 ifp->if_start = alc_start;
1552 ifp->if_init = alc_init;
1553 ifp->if_snd.ifq_drv_maxlen = ALC_TX_RING_CNT - 1;
1554 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
1555 IFQ_SET_READY(&ifp->if_snd);
1556 ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO4;
1557 ifp->if_hwassist = ALC_CSUM_FEATURES | CSUM_TSO;
1558 if (pci_find_cap(dev, PCIY_PMG, &base) == 0) {
1559 ifp->if_capabilities |= IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST;
1560 sc->alc_flags |= ALC_FLAG_PM;
1561 sc->alc_pmcap = base;
1563 ifp->if_capenable = ifp->if_capabilities;
1565 /* Set up MII bus. */
1566 error = mii_attach(dev, &sc->alc_miibus, ifp, alc_mediachange,
1567 alc_mediastatus, BMSR_DEFCAPMASK, sc->alc_phyaddr, MII_OFFSET_ANY,
1570 device_printf(dev, "attaching PHYs failed\n");
1574 ether_ifattach(ifp, sc->alc_eaddr);
1576 /* VLAN capability setup. */
1577 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
1578 IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
1579 ifp->if_capenable = ifp->if_capabilities;
1582 * It seems enabling Tx checksum offloading makes more trouble.
1583 * Sometimes the controller does not receive any frames when
1584 * Tx checksum offloading is enabled. I'm not sure whether this
1585 * is a bug in Tx checksum offloading logic or I got broken
1586 * sample boards. To safety, don't enable Tx checksum offloading
1587 * by default but give chance to users to toggle it if they know
1588 * their controllers work without problems.
1589 * Fortunately, Tx checksum offloading for AR816x family
1592 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1593 ifp->if_capenable &= ~IFCAP_TXCSUM;
1594 ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
1597 /* Tell the upper layer(s) we support long frames. */
1598 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1600 /* Create local taskq. */
1601 sc->alc_tq = taskqueue_create_fast("alc_taskq", M_WAITOK,
1602 taskqueue_thread_enqueue, &sc->alc_tq);
1603 if (sc->alc_tq == NULL) {
1604 device_printf(dev, "could not create taskqueue.\n");
1605 ether_ifdetach(ifp);
1609 taskqueue_start_threads(&sc->alc_tq, 1, PI_NET, "%s taskq",
1610 device_get_nameunit(sc->alc_dev));
1613 if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1614 msic = ALC_MSIX_MESSAGES;
1615 else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1616 msic = ALC_MSI_MESSAGES;
1619 for (i = 0; i < msic; i++) {
1620 error = bus_setup_intr(dev, sc->alc_irq[i],
1621 INTR_TYPE_NET | INTR_MPSAFE, alc_intr, NULL, sc,
1622 &sc->alc_intrhand[i]);
1627 device_printf(dev, "could not set up interrupt handler.\n");
1628 taskqueue_free(sc->alc_tq);
1630 ether_ifdetach(ifp);
1642 alc_detach(device_t dev)
1644 struct alc_softc *sc;
1648 sc = device_get_softc(dev);
1651 if (device_is_attached(dev)) {
1652 ether_ifdetach(ifp);
1656 callout_drain(&sc->alc_tick_ch);
1657 taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1660 if (sc->alc_tq != NULL) {
1661 taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1662 taskqueue_free(sc->alc_tq);
1666 if (sc->alc_miibus != NULL) {
1667 device_delete_child(dev, sc->alc_miibus);
1668 sc->alc_miibus = NULL;
1670 bus_generic_detach(dev);
1678 if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1679 msic = ALC_MSIX_MESSAGES;
1680 else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1681 msic = ALC_MSI_MESSAGES;
1684 for (i = 0; i < msic; i++) {
1685 if (sc->alc_intrhand[i] != NULL) {
1686 bus_teardown_intr(dev, sc->alc_irq[i],
1687 sc->alc_intrhand[i]);
1688 sc->alc_intrhand[i] = NULL;
1691 if (sc->alc_res[0] != NULL)
1693 bus_release_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1694 if ((sc->alc_flags & (ALC_FLAG_MSI | ALC_FLAG_MSIX)) != 0)
1695 pci_release_msi(dev);
1696 bus_release_resources(dev, sc->alc_res_spec, sc->alc_res);
1697 mtx_destroy(&sc->alc_mtx);
1702 #define ALC_SYSCTL_STAT_ADD32(c, h, n, p, d) \
1703 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
1704 #define ALC_SYSCTL_STAT_ADD64(c, h, n, p, d) \
1705 SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
1708 alc_sysctl_node(struct alc_softc *sc)
1710 struct sysctl_ctx_list *ctx;
1711 struct sysctl_oid_list *child, *parent;
1712 struct sysctl_oid *tree;
1713 struct alc_hw_stats *stats;
1716 stats = &sc->alc_stats;
1717 ctx = device_get_sysctl_ctx(sc->alc_dev);
1718 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->alc_dev));
1720 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_rx_mod",
1721 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_rx_mod, 0,
1722 sysctl_hw_alc_int_mod, "I", "alc Rx interrupt moderation");
1723 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_tx_mod",
1724 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_tx_mod, 0,
1725 sysctl_hw_alc_int_mod, "I", "alc Tx interrupt moderation");
1726 /* Pull in device tunables. */
1727 sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1728 error = resource_int_value(device_get_name(sc->alc_dev),
1729 device_get_unit(sc->alc_dev), "int_rx_mod", &sc->alc_int_rx_mod);
1731 if (sc->alc_int_rx_mod < ALC_IM_TIMER_MIN ||
1732 sc->alc_int_rx_mod > ALC_IM_TIMER_MAX) {
1733 device_printf(sc->alc_dev, "int_rx_mod value out of "
1734 "range; using default: %d\n",
1735 ALC_IM_RX_TIMER_DEFAULT);
1736 sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1739 sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1740 error = resource_int_value(device_get_name(sc->alc_dev),
1741 device_get_unit(sc->alc_dev), "int_tx_mod", &sc->alc_int_tx_mod);
1743 if (sc->alc_int_tx_mod < ALC_IM_TIMER_MIN ||
1744 sc->alc_int_tx_mod > ALC_IM_TIMER_MAX) {
1745 device_printf(sc->alc_dev, "int_tx_mod value out of "
1746 "range; using default: %d\n",
1747 ALC_IM_TX_TIMER_DEFAULT);
1748 sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1751 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
1752 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_process_limit, 0,
1753 sysctl_hw_alc_proc_limit, "I",
1754 "max number of Rx events to process");
1755 /* Pull in device tunables. */
1756 sc->alc_process_limit = ALC_PROC_DEFAULT;
1757 error = resource_int_value(device_get_name(sc->alc_dev),
1758 device_get_unit(sc->alc_dev), "process_limit",
1759 &sc->alc_process_limit);
1761 if (sc->alc_process_limit < ALC_PROC_MIN ||
1762 sc->alc_process_limit > ALC_PROC_MAX) {
1763 device_printf(sc->alc_dev,
1764 "process_limit value out of range; "
1765 "using default: %d\n", ALC_PROC_DEFAULT);
1766 sc->alc_process_limit = ALC_PROC_DEFAULT;
1770 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
1771 NULL, "ALC statistics");
1772 parent = SYSCTL_CHILDREN(tree);
1774 /* Rx statistics. */
1775 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
1776 NULL, "Rx MAC statistics");
1777 child = SYSCTL_CHILDREN(tree);
1778 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1779 &stats->rx_frames, "Good frames");
1780 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1781 &stats->rx_bcast_frames, "Good broadcast frames");
1782 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1783 &stats->rx_mcast_frames, "Good multicast frames");
1784 ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1785 &stats->rx_pause_frames, "Pause control frames");
1786 ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1787 &stats->rx_control_frames, "Control frames");
1788 ALC_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
1789 &stats->rx_crcerrs, "CRC errors");
1790 ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1791 &stats->rx_lenerrs, "Frames with length mismatched");
1792 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1793 &stats->rx_bytes, "Good octets");
1794 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1795 &stats->rx_bcast_bytes, "Good broadcast octets");
1796 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1797 &stats->rx_mcast_bytes, "Good multicast octets");
1798 ALC_SYSCTL_STAT_ADD32(ctx, child, "runts",
1799 &stats->rx_runts, "Too short frames");
1800 ALC_SYSCTL_STAT_ADD32(ctx, child, "fragments",
1801 &stats->rx_fragments, "Fragmented frames");
1802 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1803 &stats->rx_pkts_64, "64 bytes frames");
1804 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1805 &stats->rx_pkts_65_127, "65 to 127 bytes frames");
1806 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1807 &stats->rx_pkts_128_255, "128 to 255 bytes frames");
1808 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1809 &stats->rx_pkts_256_511, "256 to 511 bytes frames");
1810 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1811 &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
1812 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1813 &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
1814 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1815 &stats->rx_pkts_1519_max, "1519 to max frames");
1816 ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1817 &stats->rx_pkts_truncated, "Truncated frames due to MTU size");
1818 ALC_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
1819 &stats->rx_fifo_oflows, "FIFO overflows");
1820 ALC_SYSCTL_STAT_ADD32(ctx, child, "rrs_errs",
1821 &stats->rx_rrs_errs, "Return status write-back errors");
1822 ALC_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
1823 &stats->rx_alignerrs, "Alignment errors");
1824 ALC_SYSCTL_STAT_ADD32(ctx, child, "filtered",
1825 &stats->rx_pkts_filtered,
1826 "Frames dropped due to address filtering");
1828 /* Tx statistics. */
1829 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
1830 NULL, "Tx MAC statistics");
1831 child = SYSCTL_CHILDREN(tree);
1832 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1833 &stats->tx_frames, "Good frames");
1834 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1835 &stats->tx_bcast_frames, "Good broadcast frames");
1836 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1837 &stats->tx_mcast_frames, "Good multicast frames");
1838 ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1839 &stats->tx_pause_frames, "Pause control frames");
1840 ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1841 &stats->tx_control_frames, "Control frames");
1842 ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
1843 &stats->tx_excess_defer, "Frames with excessive derferrals");
1844 ALC_SYSCTL_STAT_ADD32(ctx, child, "defers",
1845 &stats->tx_excess_defer, "Frames with derferrals");
1846 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1847 &stats->tx_bytes, "Good octets");
1848 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1849 &stats->tx_bcast_bytes, "Good broadcast octets");
1850 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1851 &stats->tx_mcast_bytes, "Good multicast octets");
1852 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1853 &stats->tx_pkts_64, "64 bytes frames");
1854 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1855 &stats->tx_pkts_65_127, "65 to 127 bytes frames");
1856 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1857 &stats->tx_pkts_128_255, "128 to 255 bytes frames");
1858 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1859 &stats->tx_pkts_256_511, "256 to 511 bytes frames");
1860 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1861 &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
1862 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1863 &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
1864 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1865 &stats->tx_pkts_1519_max, "1519 to max frames");
1866 ALC_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
1867 &stats->tx_single_colls, "Single collisions");
1868 ALC_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
1869 &stats->tx_multi_colls, "Multiple collisions");
1870 ALC_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
1871 &stats->tx_late_colls, "Late collisions");
1872 ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_colls",
1873 &stats->tx_excess_colls, "Excessive collisions");
1874 ALC_SYSCTL_STAT_ADD32(ctx, child, "underruns",
1875 &stats->tx_underrun, "FIFO underruns");
1876 ALC_SYSCTL_STAT_ADD32(ctx, child, "desc_underruns",
1877 &stats->tx_desc_underrun, "Descriptor write-back errors");
1878 ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1879 &stats->tx_lenerrs, "Frames with length mismatched");
1880 ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1881 &stats->tx_pkts_truncated, "Truncated frames due to MTU size");
1884 #undef ALC_SYSCTL_STAT_ADD32
1885 #undef ALC_SYSCTL_STAT_ADD64
1887 struct alc_dmamap_arg {
1888 bus_addr_t alc_busaddr;
1892 alc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1894 struct alc_dmamap_arg *ctx;
1899 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1901 ctx = (struct alc_dmamap_arg *)arg;
1902 ctx->alc_busaddr = segs[0].ds_addr;
1906 * Normal and high Tx descriptors shares single Tx high address.
1907 * Four Rx descriptor/return rings and CMB shares the same Rx
1911 alc_check_boundary(struct alc_softc *sc)
1913 bus_addr_t cmb_end, rx_ring_end, rr_ring_end, tx_ring_end;
1915 rx_ring_end = sc->alc_rdata.alc_rx_ring_paddr + ALC_RX_RING_SZ;
1916 rr_ring_end = sc->alc_rdata.alc_rr_ring_paddr + ALC_RR_RING_SZ;
1917 cmb_end = sc->alc_rdata.alc_cmb_paddr + ALC_CMB_SZ;
1918 tx_ring_end = sc->alc_rdata.alc_tx_ring_paddr + ALC_TX_RING_SZ;
1920 /* 4GB boundary crossing is not allowed. */
1921 if ((ALC_ADDR_HI(rx_ring_end) !=
1922 ALC_ADDR_HI(sc->alc_rdata.alc_rx_ring_paddr)) ||
1923 (ALC_ADDR_HI(rr_ring_end) !=
1924 ALC_ADDR_HI(sc->alc_rdata.alc_rr_ring_paddr)) ||
1925 (ALC_ADDR_HI(cmb_end) !=
1926 ALC_ADDR_HI(sc->alc_rdata.alc_cmb_paddr)) ||
1927 (ALC_ADDR_HI(tx_ring_end) !=
1928 ALC_ADDR_HI(sc->alc_rdata.alc_tx_ring_paddr)))
1931 * Make sure Rx return descriptor/Rx descriptor/CMB use
1932 * the same high address.
1934 if ((ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(rr_ring_end)) ||
1935 (ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(cmb_end)))
1942 alc_dma_alloc(struct alc_softc *sc)
1944 struct alc_txdesc *txd;
1945 struct alc_rxdesc *rxd;
1947 struct alc_dmamap_arg ctx;
1950 lowaddr = BUS_SPACE_MAXADDR;
1952 /* Create parent DMA tag. */
1953 error = bus_dma_tag_create(
1954 bus_get_dma_tag(sc->alc_dev), /* parent */
1955 1, 0, /* alignment, boundary */
1956 lowaddr, /* lowaddr */
1957 BUS_SPACE_MAXADDR, /* highaddr */
1958 NULL, NULL, /* filter, filterarg */
1959 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1961 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1963 NULL, NULL, /* lockfunc, lockarg */
1964 &sc->alc_cdata.alc_parent_tag);
1966 device_printf(sc->alc_dev,
1967 "could not create parent DMA tag.\n");
1971 /* Create DMA tag for Tx descriptor ring. */
1972 error = bus_dma_tag_create(
1973 sc->alc_cdata.alc_parent_tag, /* parent */
1974 ALC_TX_RING_ALIGN, 0, /* alignment, boundary */
1975 BUS_SPACE_MAXADDR, /* lowaddr */
1976 BUS_SPACE_MAXADDR, /* highaddr */
1977 NULL, NULL, /* filter, filterarg */
1978 ALC_TX_RING_SZ, /* maxsize */
1980 ALC_TX_RING_SZ, /* maxsegsize */
1982 NULL, NULL, /* lockfunc, lockarg */
1983 &sc->alc_cdata.alc_tx_ring_tag);
1985 device_printf(sc->alc_dev,
1986 "could not create Tx ring DMA tag.\n");
1990 /* Create DMA tag for Rx free descriptor ring. */
1991 error = bus_dma_tag_create(
1992 sc->alc_cdata.alc_parent_tag, /* parent */
1993 ALC_RX_RING_ALIGN, 0, /* alignment, boundary */
1994 BUS_SPACE_MAXADDR, /* lowaddr */
1995 BUS_SPACE_MAXADDR, /* highaddr */
1996 NULL, NULL, /* filter, filterarg */
1997 ALC_RX_RING_SZ, /* maxsize */
1999 ALC_RX_RING_SZ, /* maxsegsize */
2001 NULL, NULL, /* lockfunc, lockarg */
2002 &sc->alc_cdata.alc_rx_ring_tag);
2004 device_printf(sc->alc_dev,
2005 "could not create Rx ring DMA tag.\n");
2008 /* Create DMA tag for Rx return descriptor ring. */
2009 error = bus_dma_tag_create(
2010 sc->alc_cdata.alc_parent_tag, /* parent */
2011 ALC_RR_RING_ALIGN, 0, /* alignment, boundary */
2012 BUS_SPACE_MAXADDR, /* lowaddr */
2013 BUS_SPACE_MAXADDR, /* highaddr */
2014 NULL, NULL, /* filter, filterarg */
2015 ALC_RR_RING_SZ, /* maxsize */
2017 ALC_RR_RING_SZ, /* maxsegsize */
2019 NULL, NULL, /* lockfunc, lockarg */
2020 &sc->alc_cdata.alc_rr_ring_tag);
2022 device_printf(sc->alc_dev,
2023 "could not create Rx return ring DMA tag.\n");
2027 /* Create DMA tag for coalescing message block. */
2028 error = bus_dma_tag_create(
2029 sc->alc_cdata.alc_parent_tag, /* parent */
2030 ALC_CMB_ALIGN, 0, /* alignment, boundary */
2031 BUS_SPACE_MAXADDR, /* lowaddr */
2032 BUS_SPACE_MAXADDR, /* highaddr */
2033 NULL, NULL, /* filter, filterarg */
2034 ALC_CMB_SZ, /* maxsize */
2036 ALC_CMB_SZ, /* maxsegsize */
2038 NULL, NULL, /* lockfunc, lockarg */
2039 &sc->alc_cdata.alc_cmb_tag);
2041 device_printf(sc->alc_dev,
2042 "could not create CMB DMA tag.\n");
2045 /* Create DMA tag for status message block. */
2046 error = bus_dma_tag_create(
2047 sc->alc_cdata.alc_parent_tag, /* parent */
2048 ALC_SMB_ALIGN, 0, /* alignment, boundary */
2049 BUS_SPACE_MAXADDR, /* lowaddr */
2050 BUS_SPACE_MAXADDR, /* highaddr */
2051 NULL, NULL, /* filter, filterarg */
2052 ALC_SMB_SZ, /* maxsize */
2054 ALC_SMB_SZ, /* maxsegsize */
2056 NULL, NULL, /* lockfunc, lockarg */
2057 &sc->alc_cdata.alc_smb_tag);
2059 device_printf(sc->alc_dev,
2060 "could not create SMB DMA tag.\n");
2064 /* Allocate DMA'able memory and load the DMA map for Tx ring. */
2065 error = bus_dmamem_alloc(sc->alc_cdata.alc_tx_ring_tag,
2066 (void **)&sc->alc_rdata.alc_tx_ring,
2067 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2068 &sc->alc_cdata.alc_tx_ring_map);
2070 device_printf(sc->alc_dev,
2071 "could not allocate DMA'able memory for Tx ring.\n");
2074 ctx.alc_busaddr = 0;
2075 error = bus_dmamap_load(sc->alc_cdata.alc_tx_ring_tag,
2076 sc->alc_cdata.alc_tx_ring_map, sc->alc_rdata.alc_tx_ring,
2077 ALC_TX_RING_SZ, alc_dmamap_cb, &ctx, 0);
2078 if (error != 0 || ctx.alc_busaddr == 0) {
2079 device_printf(sc->alc_dev,
2080 "could not load DMA'able memory for Tx ring.\n");
2083 sc->alc_rdata.alc_tx_ring_paddr = ctx.alc_busaddr;
2085 /* Allocate DMA'able memory and load the DMA map for Rx ring. */
2086 error = bus_dmamem_alloc(sc->alc_cdata.alc_rx_ring_tag,
2087 (void **)&sc->alc_rdata.alc_rx_ring,
2088 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2089 &sc->alc_cdata.alc_rx_ring_map);
2091 device_printf(sc->alc_dev,
2092 "could not allocate DMA'able memory for Rx ring.\n");
2095 ctx.alc_busaddr = 0;
2096 error = bus_dmamap_load(sc->alc_cdata.alc_rx_ring_tag,
2097 sc->alc_cdata.alc_rx_ring_map, sc->alc_rdata.alc_rx_ring,
2098 ALC_RX_RING_SZ, alc_dmamap_cb, &ctx, 0);
2099 if (error != 0 || ctx.alc_busaddr == 0) {
2100 device_printf(sc->alc_dev,
2101 "could not load DMA'able memory for Rx ring.\n");
2104 sc->alc_rdata.alc_rx_ring_paddr = ctx.alc_busaddr;
2106 /* Allocate DMA'able memory and load the DMA map for Rx return ring. */
2107 error = bus_dmamem_alloc(sc->alc_cdata.alc_rr_ring_tag,
2108 (void **)&sc->alc_rdata.alc_rr_ring,
2109 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2110 &sc->alc_cdata.alc_rr_ring_map);
2112 device_printf(sc->alc_dev,
2113 "could not allocate DMA'able memory for Rx return ring.\n");
2116 ctx.alc_busaddr = 0;
2117 error = bus_dmamap_load(sc->alc_cdata.alc_rr_ring_tag,
2118 sc->alc_cdata.alc_rr_ring_map, sc->alc_rdata.alc_rr_ring,
2119 ALC_RR_RING_SZ, alc_dmamap_cb, &ctx, 0);
2120 if (error != 0 || ctx.alc_busaddr == 0) {
2121 device_printf(sc->alc_dev,
2122 "could not load DMA'able memory for Tx ring.\n");
2125 sc->alc_rdata.alc_rr_ring_paddr = ctx.alc_busaddr;
2127 /* Allocate DMA'able memory and load the DMA map for CMB. */
2128 error = bus_dmamem_alloc(sc->alc_cdata.alc_cmb_tag,
2129 (void **)&sc->alc_rdata.alc_cmb,
2130 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2131 &sc->alc_cdata.alc_cmb_map);
2133 device_printf(sc->alc_dev,
2134 "could not allocate DMA'able memory for CMB.\n");
2137 ctx.alc_busaddr = 0;
2138 error = bus_dmamap_load(sc->alc_cdata.alc_cmb_tag,
2139 sc->alc_cdata.alc_cmb_map, sc->alc_rdata.alc_cmb,
2140 ALC_CMB_SZ, alc_dmamap_cb, &ctx, 0);
2141 if (error != 0 || ctx.alc_busaddr == 0) {
2142 device_printf(sc->alc_dev,
2143 "could not load DMA'able memory for CMB.\n");
2146 sc->alc_rdata.alc_cmb_paddr = ctx.alc_busaddr;
2148 /* Allocate DMA'able memory and load the DMA map for SMB. */
2149 error = bus_dmamem_alloc(sc->alc_cdata.alc_smb_tag,
2150 (void **)&sc->alc_rdata.alc_smb,
2151 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2152 &sc->alc_cdata.alc_smb_map);
2154 device_printf(sc->alc_dev,
2155 "could not allocate DMA'able memory for SMB.\n");
2158 ctx.alc_busaddr = 0;
2159 error = bus_dmamap_load(sc->alc_cdata.alc_smb_tag,
2160 sc->alc_cdata.alc_smb_map, sc->alc_rdata.alc_smb,
2161 ALC_SMB_SZ, alc_dmamap_cb, &ctx, 0);
2162 if (error != 0 || ctx.alc_busaddr == 0) {
2163 device_printf(sc->alc_dev,
2164 "could not load DMA'able memory for CMB.\n");
2167 sc->alc_rdata.alc_smb_paddr = ctx.alc_busaddr;
2169 /* Make sure we've not crossed 4GB boundary. */
2170 if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
2171 (error = alc_check_boundary(sc)) != 0) {
2172 device_printf(sc->alc_dev, "4GB boundary crossed, "
2173 "switching to 32bit DMA addressing mode.\n");
2176 * Limit max allowable DMA address space to 32bit
2179 lowaddr = BUS_SPACE_MAXADDR_32BIT;
2184 * Create Tx buffer parent tag.
2185 * AR81[3567]x allows 64bit DMA addressing of Tx/Rx buffers
2186 * so it needs separate parent DMA tag as parent DMA address
2187 * space could be restricted to be within 32bit address space
2188 * by 4GB boundary crossing.
2190 error = bus_dma_tag_create(
2191 bus_get_dma_tag(sc->alc_dev), /* parent */
2192 1, 0, /* alignment, boundary */
2193 BUS_SPACE_MAXADDR, /* lowaddr */
2194 BUS_SPACE_MAXADDR, /* highaddr */
2195 NULL, NULL, /* filter, filterarg */
2196 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
2198 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
2200 NULL, NULL, /* lockfunc, lockarg */
2201 &sc->alc_cdata.alc_buffer_tag);
2203 device_printf(sc->alc_dev,
2204 "could not create parent buffer DMA tag.\n");
2208 /* Create DMA tag for Tx buffers. */
2209 error = bus_dma_tag_create(
2210 sc->alc_cdata.alc_buffer_tag, /* parent */
2211 1, 0, /* alignment, boundary */
2212 BUS_SPACE_MAXADDR, /* lowaddr */
2213 BUS_SPACE_MAXADDR, /* highaddr */
2214 NULL, NULL, /* filter, filterarg */
2215 ALC_TSO_MAXSIZE, /* maxsize */
2216 ALC_MAXTXSEGS, /* nsegments */
2217 ALC_TSO_MAXSEGSIZE, /* maxsegsize */
2219 NULL, NULL, /* lockfunc, lockarg */
2220 &sc->alc_cdata.alc_tx_tag);
2222 device_printf(sc->alc_dev, "could not create Tx DMA tag.\n");
2226 /* Create DMA tag for Rx buffers. */
2227 error = bus_dma_tag_create(
2228 sc->alc_cdata.alc_buffer_tag, /* parent */
2229 ALC_RX_BUF_ALIGN, 0, /* alignment, boundary */
2230 BUS_SPACE_MAXADDR, /* lowaddr */
2231 BUS_SPACE_MAXADDR, /* highaddr */
2232 NULL, NULL, /* filter, filterarg */
2233 MCLBYTES, /* maxsize */
2235 MCLBYTES, /* maxsegsize */
2237 NULL, NULL, /* lockfunc, lockarg */
2238 &sc->alc_cdata.alc_rx_tag);
2240 device_printf(sc->alc_dev, "could not create Rx DMA tag.\n");
2243 /* Create DMA maps for Tx buffers. */
2244 for (i = 0; i < ALC_TX_RING_CNT; i++) {
2245 txd = &sc->alc_cdata.alc_txdesc[i];
2247 txd->tx_dmamap = NULL;
2248 error = bus_dmamap_create(sc->alc_cdata.alc_tx_tag, 0,
2251 device_printf(sc->alc_dev,
2252 "could not create Tx dmamap.\n");
2256 /* Create DMA maps for Rx buffers. */
2257 if ((error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
2258 &sc->alc_cdata.alc_rx_sparemap)) != 0) {
2259 device_printf(sc->alc_dev,
2260 "could not create spare Rx dmamap.\n");
2263 for (i = 0; i < ALC_RX_RING_CNT; i++) {
2264 rxd = &sc->alc_cdata.alc_rxdesc[i];
2266 rxd->rx_dmamap = NULL;
2267 error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
2270 device_printf(sc->alc_dev,
2271 "could not create Rx dmamap.\n");
2281 alc_dma_free(struct alc_softc *sc)
2283 struct alc_txdesc *txd;
2284 struct alc_rxdesc *rxd;
2288 if (sc->alc_cdata.alc_tx_tag != NULL) {
2289 for (i = 0; i < ALC_TX_RING_CNT; i++) {
2290 txd = &sc->alc_cdata.alc_txdesc[i];
2291 if (txd->tx_dmamap != NULL) {
2292 bus_dmamap_destroy(sc->alc_cdata.alc_tx_tag,
2294 txd->tx_dmamap = NULL;
2297 bus_dma_tag_destroy(sc->alc_cdata.alc_tx_tag);
2298 sc->alc_cdata.alc_tx_tag = NULL;
2301 if (sc->alc_cdata.alc_rx_tag != NULL) {
2302 for (i = 0; i < ALC_RX_RING_CNT; i++) {
2303 rxd = &sc->alc_cdata.alc_rxdesc[i];
2304 if (rxd->rx_dmamap != NULL) {
2305 bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
2307 rxd->rx_dmamap = NULL;
2310 if (sc->alc_cdata.alc_rx_sparemap != NULL) {
2311 bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
2312 sc->alc_cdata.alc_rx_sparemap);
2313 sc->alc_cdata.alc_rx_sparemap = NULL;
2315 bus_dma_tag_destroy(sc->alc_cdata.alc_rx_tag);
2316 sc->alc_cdata.alc_rx_tag = NULL;
2318 /* Tx descriptor ring. */
2319 if (sc->alc_cdata.alc_tx_ring_tag != NULL) {
2320 if (sc->alc_cdata.alc_tx_ring_map != NULL)
2321 bus_dmamap_unload(sc->alc_cdata.alc_tx_ring_tag,
2322 sc->alc_cdata.alc_tx_ring_map);
2323 if (sc->alc_cdata.alc_tx_ring_map != NULL &&
2324 sc->alc_rdata.alc_tx_ring != NULL)
2325 bus_dmamem_free(sc->alc_cdata.alc_tx_ring_tag,
2326 sc->alc_rdata.alc_tx_ring,
2327 sc->alc_cdata.alc_tx_ring_map);
2328 sc->alc_rdata.alc_tx_ring = NULL;
2329 sc->alc_cdata.alc_tx_ring_map = NULL;
2330 bus_dma_tag_destroy(sc->alc_cdata.alc_tx_ring_tag);
2331 sc->alc_cdata.alc_tx_ring_tag = NULL;
2334 if (sc->alc_cdata.alc_rx_ring_tag != NULL) {
2335 if (sc->alc_cdata.alc_rx_ring_map != NULL)
2336 bus_dmamap_unload(sc->alc_cdata.alc_rx_ring_tag,
2337 sc->alc_cdata.alc_rx_ring_map);
2338 if (sc->alc_cdata.alc_rx_ring_map != NULL &&
2339 sc->alc_rdata.alc_rx_ring != NULL)
2340 bus_dmamem_free(sc->alc_cdata.alc_rx_ring_tag,
2341 sc->alc_rdata.alc_rx_ring,
2342 sc->alc_cdata.alc_rx_ring_map);
2343 sc->alc_rdata.alc_rx_ring = NULL;
2344 sc->alc_cdata.alc_rx_ring_map = NULL;
2345 bus_dma_tag_destroy(sc->alc_cdata.alc_rx_ring_tag);
2346 sc->alc_cdata.alc_rx_ring_tag = NULL;
2348 /* Rx return ring. */
2349 if (sc->alc_cdata.alc_rr_ring_tag != NULL) {
2350 if (sc->alc_cdata.alc_rr_ring_map != NULL)
2351 bus_dmamap_unload(sc->alc_cdata.alc_rr_ring_tag,
2352 sc->alc_cdata.alc_rr_ring_map);
2353 if (sc->alc_cdata.alc_rr_ring_map != NULL &&
2354 sc->alc_rdata.alc_rr_ring != NULL)
2355 bus_dmamem_free(sc->alc_cdata.alc_rr_ring_tag,
2356 sc->alc_rdata.alc_rr_ring,
2357 sc->alc_cdata.alc_rr_ring_map);
2358 sc->alc_rdata.alc_rr_ring = NULL;
2359 sc->alc_cdata.alc_rr_ring_map = NULL;
2360 bus_dma_tag_destroy(sc->alc_cdata.alc_rr_ring_tag);
2361 sc->alc_cdata.alc_rr_ring_tag = NULL;
2364 if (sc->alc_cdata.alc_cmb_tag != NULL) {
2365 if (sc->alc_cdata.alc_cmb_map != NULL)
2366 bus_dmamap_unload(sc->alc_cdata.alc_cmb_tag,
2367 sc->alc_cdata.alc_cmb_map);
2368 if (sc->alc_cdata.alc_cmb_map != NULL &&
2369 sc->alc_rdata.alc_cmb != NULL)
2370 bus_dmamem_free(sc->alc_cdata.alc_cmb_tag,
2371 sc->alc_rdata.alc_cmb,
2372 sc->alc_cdata.alc_cmb_map);
2373 sc->alc_rdata.alc_cmb = NULL;
2374 sc->alc_cdata.alc_cmb_map = NULL;
2375 bus_dma_tag_destroy(sc->alc_cdata.alc_cmb_tag);
2376 sc->alc_cdata.alc_cmb_tag = NULL;
2379 if (sc->alc_cdata.alc_smb_tag != NULL) {
2380 if (sc->alc_cdata.alc_smb_map != NULL)
2381 bus_dmamap_unload(sc->alc_cdata.alc_smb_tag,
2382 sc->alc_cdata.alc_smb_map);
2383 if (sc->alc_cdata.alc_smb_map != NULL &&
2384 sc->alc_rdata.alc_smb != NULL)
2385 bus_dmamem_free(sc->alc_cdata.alc_smb_tag,
2386 sc->alc_rdata.alc_smb,
2387 sc->alc_cdata.alc_smb_map);
2388 sc->alc_rdata.alc_smb = NULL;
2389 sc->alc_cdata.alc_smb_map = NULL;
2390 bus_dma_tag_destroy(sc->alc_cdata.alc_smb_tag);
2391 sc->alc_cdata.alc_smb_tag = NULL;
2393 if (sc->alc_cdata.alc_buffer_tag != NULL) {
2394 bus_dma_tag_destroy(sc->alc_cdata.alc_buffer_tag);
2395 sc->alc_cdata.alc_buffer_tag = NULL;
2397 if (sc->alc_cdata.alc_parent_tag != NULL) {
2398 bus_dma_tag_destroy(sc->alc_cdata.alc_parent_tag);
2399 sc->alc_cdata.alc_parent_tag = NULL;
2404 alc_shutdown(device_t dev)
2407 return (alc_suspend(dev));
2411 * Note, this driver resets the link speed to 10/100Mbps by
2412 * restarting auto-negotiation in suspend/shutdown phase but we
2413 * don't know whether that auto-negotiation would succeed or not
2414 * as driver has no control after powering off/suspend operation.
2415 * If the renegotiation fail WOL may not work. Running at 1Gbps
2416 * will draw more power than 375mA at 3.3V which is specified in
2417 * PCI specification and that would result in complete
2418 * shutdowning power to ethernet controller.
2421 * Save current negotiated media speed/duplex/flow-control to
2422 * softc and restore the same link again after resuming. PHY
2423 * handling such as power down/resetting to 100Mbps may be better
2424 * handled in suspend method in phy driver.
2427 alc_setlinkspeed(struct alc_softc *sc)
2429 struct mii_data *mii;
2432 mii = device_get_softc(sc->alc_miibus);
2435 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
2436 (IFM_ACTIVE | IFM_AVALID)) {
2437 switch IFM_SUBTYPE(mii->mii_media_active) {
2448 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, MII_100T2CR, 0);
2449 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
2450 MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
2451 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
2452 MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
2456 * Poll link state until alc(4) get a 10/100Mbps link.
2458 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
2460 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
2461 == (IFM_ACTIVE | IFM_AVALID)) {
2462 switch (IFM_SUBTYPE(
2463 mii->mii_media_active)) {
2473 pause("alclnk", hz);
2476 if (i == MII_ANEGTICKS_GIGE)
2477 device_printf(sc->alc_dev,
2478 "establishing a link failed, WOL may not work!");
2481 * No link, force MAC to have 100Mbps, full-duplex link.
2482 * This is the last resort and may/may not work.
2484 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
2485 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
2490 alc_setwol(struct alc_softc *sc)
2493 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2494 alc_setwol_816x(sc);
2496 alc_setwol_813x(sc);
2500 alc_setwol_813x(struct alc_softc *sc)
2506 ALC_LOCK_ASSERT(sc);
2508 alc_disable_l0s_l1(sc);
2510 if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
2512 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2513 reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
2514 reg |= PCIE_PHYMISC_FORCE_RCV_DET;
2515 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
2516 /* Force PHY power down. */
2518 CSR_WRITE_4(sc, ALC_MASTER_CFG,
2519 CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
2523 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
2524 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
2525 alc_setlinkspeed(sc);
2526 CSR_WRITE_4(sc, ALC_MASTER_CFG,
2527 CSR_READ_4(sc, ALC_MASTER_CFG) & ~MASTER_CLK_SEL_DIS);
2531 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2532 pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
2533 CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
2534 reg = CSR_READ_4(sc, ALC_MAC_CFG);
2535 reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
2537 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2538 reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
2539 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2540 reg |= MAC_CFG_RX_ENB;
2541 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2543 reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
2544 reg |= PCIE_PHYMISC_FORCE_RCV_DET;
2545 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
2546 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
2547 /* WOL disabled, PHY power down. */
2549 CSR_WRITE_4(sc, ALC_MASTER_CFG,
2550 CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
2553 pmstat = pci_read_config(sc->alc_dev,
2554 sc->alc_pmcap + PCIR_POWER_STATUS, 2);
2555 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2556 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2557 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2558 pci_write_config(sc->alc_dev,
2559 sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
2563 alc_setwol_816x(struct alc_softc *sc)
2566 uint32_t gphy, mac, master, pmcs, reg;
2569 ALC_LOCK_ASSERT(sc);
2572 master = CSR_READ_4(sc, ALC_MASTER_CFG);
2573 master &= ~MASTER_CLK_SEL_DIS;
2574 gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
2575 gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE | GPHY_CFG_100AB_ENB |
2576 GPHY_CFG_PHY_PLL_ON);
2577 gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE | GPHY_CFG_SEL_ANA_RESET;
2578 if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
2579 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2580 gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
2581 mac = CSR_READ_4(sc, ALC_MAC_CFG);
2583 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
2584 gphy |= GPHY_CFG_EXT_RESET;
2585 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
2586 alc_setlinkspeed(sc);
2589 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2590 pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
2591 CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
2592 mac = CSR_READ_4(sc, ALC_MAC_CFG);
2593 mac &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
2595 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2596 mac |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
2597 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2598 mac |= MAC_CFG_RX_ENB;
2599 alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
2604 reg = CSR_READ_4(sc, ALC_MISC);
2605 reg &= ~MISC_INTNLOSC_OPEN;
2606 CSR_WRITE_4(sc, ALC_MISC, reg);
2607 reg |= MISC_INTNLOSC_OPEN;
2608 CSR_WRITE_4(sc, ALC_MISC, reg);
2609 CSR_WRITE_4(sc, ALC_MASTER_CFG, master);
2610 CSR_WRITE_4(sc, ALC_MAC_CFG, mac);
2611 CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
2612 reg = CSR_READ_4(sc, ALC_PDLL_TRNS1);
2613 reg |= PDLL_TRNS1_D3PLLOFF_ENB;
2614 CSR_WRITE_4(sc, ALC_PDLL_TRNS1, reg);
2616 if ((sc->alc_flags & ALC_FLAG_PM) != 0) {
2618 pmstat = pci_read_config(sc->alc_dev,
2619 sc->alc_pmcap + PCIR_POWER_STATUS, 2);
2620 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2621 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2622 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2623 pci_write_config(sc->alc_dev,
2624 sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
2629 alc_suspend(device_t dev)
2631 struct alc_softc *sc;
2633 sc = device_get_softc(dev);
2644 alc_resume(device_t dev)
2646 struct alc_softc *sc;
2650 sc = device_get_softc(dev);
2653 if ((sc->alc_flags & ALC_FLAG_PM) != 0) {
2654 /* Disable PME and clear PME status. */
2655 pmstat = pci_read_config(sc->alc_dev,
2656 sc->alc_pmcap + PCIR_POWER_STATUS, 2);
2657 if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2658 pmstat &= ~PCIM_PSTAT_PMEENABLE;
2659 pci_write_config(sc->alc_dev,
2660 sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
2666 if ((ifp->if_flags & IFF_UP) != 0) {
2667 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2668 alc_init_locked(sc);
2676 alc_encap(struct alc_softc *sc, struct mbuf **m_head)
2678 struct alc_txdesc *txd, *txd_last;
2679 struct tx_desc *desc;
2683 bus_dma_segment_t txsegs[ALC_MAXTXSEGS];
2685 uint32_t cflags, hdrlen, ip_off, poff, vtag;
2686 int error, idx, nsegs, prod;
2688 ALC_LOCK_ASSERT(sc);
2690 M_ASSERTPKTHDR((*m_head));
2696 if ((m->m_pkthdr.csum_flags & (ALC_CSUM_FEATURES | CSUM_TSO)) != 0) {
2698 * AR81[3567]x requires offset of TCP/UDP header in its
2699 * Tx descriptor to perform Tx checksum offloading. TSO
2700 * also requires TCP header offset and modification of
2701 * IP/TCP header. This kind of operation takes many CPU
2702 * cycles on FreeBSD so fast host CPU is required to get
2703 * smooth TSO performance.
2705 struct ether_header *eh;
2707 if (M_WRITABLE(m) == 0) {
2708 /* Get a writable copy. */
2709 m = m_dup(*m_head, M_NOWAIT);
2710 /* Release original mbufs. */
2719 ip_off = sizeof(struct ether_header);
2720 m = m_pullup(m, ip_off);
2725 eh = mtod(m, struct ether_header *);
2727 * Check if hardware VLAN insertion is off.
2728 * Additional check for LLC/SNAP frame?
2730 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2731 ip_off = sizeof(struct ether_vlan_header);
2732 m = m_pullup(m, ip_off);
2738 m = m_pullup(m, ip_off + sizeof(struct ip));
2743 ip = (struct ip *)(mtod(m, char *) + ip_off);
2744 poff = ip_off + (ip->ip_hl << 2);
2745 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2746 m = m_pullup(m, poff + sizeof(struct tcphdr));
2751 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2752 m = m_pullup(m, poff + (tcp->th_off << 2));
2758 * Due to strict adherence of Microsoft NDIS
2759 * Large Send specification, hardware expects
2760 * a pseudo TCP checksum inserted by upper
2761 * stack. Unfortunately the pseudo TCP
2762 * checksum that NDIS refers to does not include
2763 * TCP payload length so driver should recompute
2764 * the pseudo checksum here. Hopefully this
2765 * wouldn't be much burden on modern CPUs.
2767 * Reset IP checksum and recompute TCP pseudo
2768 * checksum as NDIS specification said.
2770 ip = (struct ip *)(mtod(m, char *) + ip_off);
2771 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2773 tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
2774 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
2779 prod = sc->alc_cdata.alc_tx_prod;
2780 txd = &sc->alc_cdata.alc_txdesc[prod];
2782 map = txd->tx_dmamap;
2784 error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2785 *m_head, txsegs, &nsegs, 0);
2786 if (error == EFBIG) {
2787 m = m_collapse(*m_head, M_NOWAIT, ALC_MAXTXSEGS);
2794 error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2795 *m_head, txsegs, &nsegs, 0);
2801 } else if (error != 0)
2809 /* Check descriptor overrun. */
2810 if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
2811 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag, map);
2814 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag, map, BUS_DMASYNC_PREWRITE);
2817 cflags = TD_ETHERNET;
2821 /* Configure VLAN hardware tag insertion. */
2822 if ((m->m_flags & M_VLANTAG) != 0) {
2823 vtag = htons(m->m_pkthdr.ether_vtag);
2824 vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
2825 cflags |= TD_INS_VLAN_TAG;
2827 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2828 /* Request TSO and set MSS. */
2829 cflags |= TD_TSO | TD_TSO_DESCV1;
2830 cflags |= ((uint32_t)m->m_pkthdr.tso_segsz << TD_MSS_SHIFT) &
2832 /* Set TCP header offset. */
2833 cflags |= (poff << TD_TCPHDR_OFFSET_SHIFT) &
2834 TD_TCPHDR_OFFSET_MASK;
2836 * AR81[3567]x requires the first buffer should
2837 * only hold IP/TCP header data. Payload should
2838 * be handled in other descriptors.
2840 hdrlen = poff + (tcp->th_off << 2);
2841 desc = &sc->alc_rdata.alc_tx_ring[prod];
2842 desc->len = htole32(TX_BYTES(hdrlen | vtag));
2843 desc->flags = htole32(cflags);
2844 desc->addr = htole64(txsegs[0].ds_addr);
2845 sc->alc_cdata.alc_tx_cnt++;
2846 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2847 if (m->m_len - hdrlen > 0) {
2848 /* Handle remaining payload of the first fragment. */
2849 desc = &sc->alc_rdata.alc_tx_ring[prod];
2850 desc->len = htole32(TX_BYTES((m->m_len - hdrlen) |
2852 desc->flags = htole32(cflags);
2853 desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
2854 sc->alc_cdata.alc_tx_cnt++;
2855 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2857 /* Handle remaining fragments. */
2859 } else if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
2860 /* Configure Tx checksum offload. */
2861 #ifdef ALC_USE_CUSTOM_CSUM
2862 cflags |= TD_CUSTOM_CSUM;
2863 /* Set checksum start offset. */
2864 cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
2865 TD_PLOAD_OFFSET_MASK;
2866 /* Set checksum insertion position of TCP/UDP. */
2867 cflags |= (((poff + m->m_pkthdr.csum_data) >> 1) <<
2868 TD_CUSTOM_CSUM_OFFSET_SHIFT) & TD_CUSTOM_CSUM_OFFSET_MASK;
2870 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
2871 cflags |= TD_IPCSUM;
2872 if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2873 cflags |= TD_TCPCSUM;
2874 if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2875 cflags |= TD_UDPCSUM;
2876 /* Set TCP/UDP header offset. */
2877 cflags |= (poff << TD_L4HDR_OFFSET_SHIFT) &
2878 TD_L4HDR_OFFSET_MASK;
2881 for (; idx < nsegs; idx++) {
2882 desc = &sc->alc_rdata.alc_tx_ring[prod];
2883 desc->len = htole32(TX_BYTES(txsegs[idx].ds_len) | vtag);
2884 desc->flags = htole32(cflags);
2885 desc->addr = htole64(txsegs[idx].ds_addr);
2886 sc->alc_cdata.alc_tx_cnt++;
2887 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2889 /* Update producer index. */
2890 sc->alc_cdata.alc_tx_prod = prod;
2892 /* Finally set EOP on the last descriptor. */
2893 prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
2894 desc = &sc->alc_rdata.alc_tx_ring[prod];
2895 desc->flags |= htole32(TD_EOP);
2897 /* Swap dmamap of the first and the last. */
2898 txd = &sc->alc_cdata.alc_txdesc[prod];
2899 map = txd_last->tx_dmamap;
2900 txd_last->tx_dmamap = txd->tx_dmamap;
2901 txd->tx_dmamap = map;
2908 alc_start(struct ifnet *ifp)
2910 struct alc_softc *sc;
2914 alc_start_locked(ifp);
2919 alc_start_locked(struct ifnet *ifp)
2921 struct alc_softc *sc;
2922 struct mbuf *m_head;
2927 ALC_LOCK_ASSERT(sc);
2929 /* Reclaim transmitted frames. */
2930 if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
2933 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2934 IFF_DRV_RUNNING || (sc->alc_flags & ALC_FLAG_LINK) == 0)
2937 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
2938 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2942 * Pack the data into the transmit ring. If we
2943 * don't have room, set the OACTIVE flag and wait
2944 * for the NIC to drain the ring.
2946 if (alc_encap(sc, &m_head)) {
2949 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2950 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2956 * If there's a BPF listener, bounce a copy of this frame
2959 ETHER_BPF_MTAP(ifp, m_head);
2963 /* Sync descriptors. */
2964 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2965 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
2966 /* Kick. Assume we're using normal Tx priority queue. */
2967 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2968 CSR_WRITE_2(sc, ALC_MBOX_TD_PRI0_PROD_IDX,
2969 (uint16_t)sc->alc_cdata.alc_tx_prod);
2971 CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2972 (sc->alc_cdata.alc_tx_prod <<
2973 MBOX_TD_PROD_LO_IDX_SHIFT) &
2974 MBOX_TD_PROD_LO_IDX_MASK);
2975 /* Set a timeout in case the chip goes out to lunch. */
2976 sc->alc_watchdog_timer = ALC_TX_TIMEOUT;
2981 alc_watchdog(struct alc_softc *sc)
2985 ALC_LOCK_ASSERT(sc);
2987 if (sc->alc_watchdog_timer == 0 || --sc->alc_watchdog_timer)
2991 if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
2992 if_printf(sc->alc_ifp, "watchdog timeout (lost link)\n");
2994 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2995 alc_init_locked(sc);
2998 if_printf(sc->alc_ifp, "watchdog timeout -- resetting\n");
3000 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3001 alc_init_locked(sc);
3002 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3003 alc_start_locked(ifp);
3007 alc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3009 struct alc_softc *sc;
3011 struct mii_data *mii;
3015 ifr = (struct ifreq *)data;
3019 if (ifr->ifr_mtu < ETHERMIN ||
3020 ifr->ifr_mtu > (sc->alc_ident->max_framelen -
3021 sizeof(struct ether_vlan_header) - ETHER_CRC_LEN) ||
3022 ((sc->alc_flags & ALC_FLAG_JUMBO) == 0 &&
3023 ifr->ifr_mtu > ETHERMTU))
3025 else if (ifp->if_mtu != ifr->ifr_mtu) {
3027 ifp->if_mtu = ifr->ifr_mtu;
3028 /* AR81[3567]x has 13 bits MSS field. */
3029 if (ifp->if_mtu > ALC_TSO_MTU &&
3030 (ifp->if_capenable & IFCAP_TSO4) != 0) {
3031 ifp->if_capenable &= ~IFCAP_TSO4;
3032 ifp->if_hwassist &= ~CSUM_TSO;
3033 VLAN_CAPABILITIES(ifp);
3040 if ((ifp->if_flags & IFF_UP) != 0) {
3041 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
3042 ((ifp->if_flags ^ sc->alc_if_flags) &
3043 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3046 alc_init_locked(sc);
3047 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3049 sc->alc_if_flags = ifp->if_flags;
3055 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3061 mii = device_get_softc(sc->alc_miibus);
3062 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
3066 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3067 if ((mask & IFCAP_TXCSUM) != 0 &&
3068 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3069 ifp->if_capenable ^= IFCAP_TXCSUM;
3070 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3071 ifp->if_hwassist |= ALC_CSUM_FEATURES;
3073 ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
3075 if ((mask & IFCAP_TSO4) != 0 &&
3076 (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3077 ifp->if_capenable ^= IFCAP_TSO4;
3078 if ((ifp->if_capenable & IFCAP_TSO4) != 0) {
3079 /* AR81[3567]x has 13 bits MSS field. */
3080 if (ifp->if_mtu > ALC_TSO_MTU) {
3081 ifp->if_capenable &= ~IFCAP_TSO4;
3082 ifp->if_hwassist &= ~CSUM_TSO;
3084 ifp->if_hwassist |= CSUM_TSO;
3086 ifp->if_hwassist &= ~CSUM_TSO;
3088 if ((mask & IFCAP_WOL_MCAST) != 0 &&
3089 (ifp->if_capabilities & IFCAP_WOL_MCAST) != 0)
3090 ifp->if_capenable ^= IFCAP_WOL_MCAST;
3091 if ((mask & IFCAP_WOL_MAGIC) != 0 &&
3092 (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
3093 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
3094 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3095 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3096 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3099 if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
3100 (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
3101 ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
3102 if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3103 (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3104 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3105 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3106 ifp->if_capenable &=
3107 ~(IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
3109 VLAN_CAPABILITIES(ifp);
3112 error = ether_ioctl(ifp, cmd, data);
3120 alc_mac_config(struct alc_softc *sc)
3122 struct mii_data *mii;
3125 ALC_LOCK_ASSERT(sc);
3127 mii = device_get_softc(sc->alc_miibus);
3128 reg = CSR_READ_4(sc, ALC_MAC_CFG);
3129 reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
3130 MAC_CFG_SPEED_MASK);
3131 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3132 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
3133 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
3134 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
3135 reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3136 /* Reprogram MAC with resolved speed/duplex. */
3137 switch (IFM_SUBTYPE(mii->mii_media_active)) {
3140 reg |= MAC_CFG_SPEED_10_100;
3143 reg |= MAC_CFG_SPEED_1000;
3146 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
3147 reg |= MAC_CFG_FULL_DUPLEX;
3148 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
3149 reg |= MAC_CFG_TX_FC;
3150 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
3151 reg |= MAC_CFG_RX_FC;
3153 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3157 alc_stats_clear(struct alc_softc *sc)
3159 struct smb sb, *smb;
3163 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3164 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3165 sc->alc_cdata.alc_smb_map,
3166 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3167 smb = sc->alc_rdata.alc_smb;
3168 /* Update done, clear. */
3170 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3171 sc->alc_cdata.alc_smb_map,
3172 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3174 for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
3176 CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
3177 i += sizeof(uint32_t);
3179 /* Read Tx statistics. */
3180 for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
3182 CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
3183 i += sizeof(uint32_t);
3189 alc_stats_update(struct alc_softc *sc)
3191 struct alc_hw_stats *stat;
3192 struct smb sb, *smb;
3197 ALC_LOCK_ASSERT(sc);
3200 stat = &sc->alc_stats;
3201 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3202 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3203 sc->alc_cdata.alc_smb_map,
3204 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3205 smb = sc->alc_rdata.alc_smb;
3206 if (smb->updated == 0)
3210 /* Read Rx statistics. */
3211 for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
3213 *reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
3214 i += sizeof(uint32_t);
3216 /* Read Tx statistics. */
3217 for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
3219 *reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
3220 i += sizeof(uint32_t);
3225 stat->rx_frames += smb->rx_frames;
3226 stat->rx_bcast_frames += smb->rx_bcast_frames;
3227 stat->rx_mcast_frames += smb->rx_mcast_frames;
3228 stat->rx_pause_frames += smb->rx_pause_frames;
3229 stat->rx_control_frames += smb->rx_control_frames;
3230 stat->rx_crcerrs += smb->rx_crcerrs;
3231 stat->rx_lenerrs += smb->rx_lenerrs;
3232 stat->rx_bytes += smb->rx_bytes;
3233 stat->rx_runts += smb->rx_runts;
3234 stat->rx_fragments += smb->rx_fragments;
3235 stat->rx_pkts_64 += smb->rx_pkts_64;
3236 stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
3237 stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
3238 stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
3239 stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
3240 stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
3241 stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
3242 stat->rx_pkts_truncated += smb->rx_pkts_truncated;
3243 stat->rx_fifo_oflows += smb->rx_fifo_oflows;
3244 stat->rx_rrs_errs += smb->rx_rrs_errs;
3245 stat->rx_alignerrs += smb->rx_alignerrs;
3246 stat->rx_bcast_bytes += smb->rx_bcast_bytes;
3247 stat->rx_mcast_bytes += smb->rx_mcast_bytes;
3248 stat->rx_pkts_filtered += smb->rx_pkts_filtered;
3251 stat->tx_frames += smb->tx_frames;
3252 stat->tx_bcast_frames += smb->tx_bcast_frames;
3253 stat->tx_mcast_frames += smb->tx_mcast_frames;
3254 stat->tx_pause_frames += smb->tx_pause_frames;
3255 stat->tx_excess_defer += smb->tx_excess_defer;
3256 stat->tx_control_frames += smb->tx_control_frames;
3257 stat->tx_deferred += smb->tx_deferred;
3258 stat->tx_bytes += smb->tx_bytes;
3259 stat->tx_pkts_64 += smb->tx_pkts_64;
3260 stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
3261 stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
3262 stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
3263 stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
3264 stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
3265 stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
3266 stat->tx_single_colls += smb->tx_single_colls;
3267 stat->tx_multi_colls += smb->tx_multi_colls;
3268 stat->tx_late_colls += smb->tx_late_colls;
3269 stat->tx_excess_colls += smb->tx_excess_colls;
3270 stat->tx_underrun += smb->tx_underrun;
3271 stat->tx_desc_underrun += smb->tx_desc_underrun;
3272 stat->tx_lenerrs += smb->tx_lenerrs;
3273 stat->tx_pkts_truncated += smb->tx_pkts_truncated;
3274 stat->tx_bcast_bytes += smb->tx_bcast_bytes;
3275 stat->tx_mcast_bytes += smb->tx_mcast_bytes;
3277 /* Update counters in ifnet. */
3278 ifp->if_opackets += smb->tx_frames;
3280 ifp->if_collisions += smb->tx_single_colls +
3281 smb->tx_multi_colls * 2 + smb->tx_late_colls +
3282 smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT;
3286 * tx_pkts_truncated counter looks suspicious. It constantly
3287 * increments with no sign of Tx errors. This may indicate
3288 * the counter name is not correct one so I've removed the
3289 * counter in output errors.
3291 ifp->if_oerrors += smb->tx_late_colls + smb->tx_excess_colls +
3292 smb->tx_underrun + smb->tx_pkts_truncated;
3294 ifp->if_ipackets += smb->rx_frames;
3296 ifp->if_ierrors += smb->rx_crcerrs + smb->rx_lenerrs +
3297 smb->rx_runts + smb->rx_pkts_truncated +
3298 smb->rx_fifo_oflows + smb->rx_rrs_errs +
3301 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
3302 /* Update done, clear. */
3304 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
3305 sc->alc_cdata.alc_smb_map,
3306 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3313 struct alc_softc *sc;
3316 sc = (struct alc_softc *)arg;
3318 status = CSR_READ_4(sc, ALC_INTR_STATUS);
3319 if ((status & ALC_INTRS) == 0)
3320 return (FILTER_STRAY);
3321 /* Disable interrupts. */
3322 CSR_WRITE_4(sc, ALC_INTR_STATUS, INTR_DIS_INT);
3323 taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
3325 return (FILTER_HANDLED);
3329 alc_int_task(void *arg, int pending)
3331 struct alc_softc *sc;
3336 sc = (struct alc_softc *)arg;
3339 status = CSR_READ_4(sc, ALC_INTR_STATUS);
3341 if (sc->alc_morework != 0) {
3342 sc->alc_morework = 0;
3343 status |= INTR_RX_PKT;
3345 if ((status & ALC_INTRS) == 0)
3348 /* Acknowledge interrupts but still disable interrupts. */
3349 CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
3352 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3353 if ((status & INTR_RX_PKT) != 0) {
3354 more = alc_rxintr(sc, sc->alc_process_limit);
3356 sc->alc_morework = 1;
3357 else if (more == EIO) {
3358 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3359 alc_init_locked(sc);
3364 if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
3365 INTR_TXQ_TO_RST)) != 0) {
3366 if ((status & INTR_DMA_RD_TO_RST) != 0)
3367 device_printf(sc->alc_dev,
3368 "DMA read error! -- resetting\n");
3369 if ((status & INTR_DMA_WR_TO_RST) != 0)
3370 device_printf(sc->alc_dev,
3371 "DMA write error! -- resetting\n");
3372 if ((status & INTR_TXQ_TO_RST) != 0)
3373 device_printf(sc->alc_dev,
3374 "TxQ reset! -- resetting\n");
3375 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3376 alc_init_locked(sc);
3380 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
3381 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3382 alc_start_locked(ifp);
3385 if (more == EAGAIN ||
3386 (CSR_READ_4(sc, ALC_INTR_STATUS) & ALC_INTRS) != 0) {
3388 taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
3393 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3394 /* Re-enable interrupts if we're running. */
3395 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
3401 alc_txeof(struct alc_softc *sc)
3404 struct alc_txdesc *txd;
3405 uint32_t cons, prod;
3408 ALC_LOCK_ASSERT(sc);
3412 if (sc->alc_cdata.alc_tx_cnt == 0)
3414 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
3415 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_POSTWRITE);
3416 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
3417 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
3418 sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_POSTREAD);
3419 prod = sc->alc_rdata.alc_cmb->cons;
3421 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3422 prod = CSR_READ_2(sc, ALC_MBOX_TD_PRI0_CONS_IDX);
3424 prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
3425 /* Assume we're using normal Tx priority queue. */
3426 prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
3427 MBOX_TD_CONS_LO_IDX_SHIFT;
3430 cons = sc->alc_cdata.alc_tx_cons;
3432 * Go through our Tx list and free mbufs for those
3433 * frames which have been transmitted.
3435 for (prog = 0; cons != prod; prog++,
3436 ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
3437 if (sc->alc_cdata.alc_tx_cnt <= 0)
3440 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3441 sc->alc_cdata.alc_tx_cnt--;
3442 txd = &sc->alc_cdata.alc_txdesc[cons];
3443 if (txd->tx_m != NULL) {
3444 /* Reclaim transmitted mbufs. */
3445 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
3446 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3447 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
3454 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3455 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
3456 sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_PREREAD);
3457 sc->alc_cdata.alc_tx_cons = cons;
3459 * Unarm watchdog timer only when there is no pending
3460 * frames in Tx queue.
3462 if (sc->alc_cdata.alc_tx_cnt == 0)
3463 sc->alc_watchdog_timer = 0;
3467 alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd)
3470 bus_dma_segment_t segs[1];
3474 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3477 m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
3478 #ifndef __NO_STRICT_ALIGNMENT
3479 m_adj(m, sizeof(uint64_t));
3482 if (bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_rx_tag,
3483 sc->alc_cdata.alc_rx_sparemap, m, segs, &nsegs, 0) != 0) {
3487 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
3489 if (rxd->rx_m != NULL) {
3490 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
3491 BUS_DMASYNC_POSTREAD);
3492 bus_dmamap_unload(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap);
3494 map = rxd->rx_dmamap;
3495 rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
3496 sc->alc_cdata.alc_rx_sparemap = map;
3497 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
3498 BUS_DMASYNC_PREREAD);
3500 rxd->rx_desc->addr = htole64(segs[0].ds_addr);
3505 alc_rxintr(struct alc_softc *sc, int count)
3508 struct rx_rdesc *rrd;
3509 uint32_t nsegs, status;
3512 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3513 sc->alc_cdata.alc_rr_ring_map,
3514 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3515 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3516 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_POSTWRITE);
3517 rr_cons = sc->alc_cdata.alc_rr_cons;
3519 for (prog = 0; (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;) {
3522 rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
3523 status = le32toh(rrd->status);
3524 if ((status & RRD_VALID) == 0)
3526 nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
3528 /* This should not happen! */
3529 device_printf(sc->alc_dev,
3530 "unexpected segment count -- resetting\n");
3534 /* Clear Rx return status. */
3536 ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
3537 sc->alc_cdata.alc_rx_cons += nsegs;
3538 sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
3543 /* Update the consumer index. */
3544 sc->alc_cdata.alc_rr_cons = rr_cons;
3545 /* Sync Rx return descriptors. */
3546 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3547 sc->alc_cdata.alc_rr_ring_map,
3548 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3550 * Sync updated Rx descriptors such that controller see
3551 * modified buffer addresses.
3553 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3554 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
3556 * Let controller know availability of new Rx buffers.
3557 * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
3558 * it may be possible to update ALC_MBOX_RD0_PROD_IDX
3559 * only when Rx buffer pre-fetching is required. In
3560 * addition we already set ALC_RX_RD_FREE_THRESH to
3561 * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
3562 * it still seems that pre-fetching needs more
3565 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3566 CSR_WRITE_2(sc, ALC_MBOX_RD0_PROD_IDX,
3567 (uint16_t)sc->alc_cdata.alc_rx_cons);
3569 CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
3570 sc->alc_cdata.alc_rx_cons);
3573 return (count > 0 ? 0 : EAGAIN);
3576 #ifndef __NO_STRICT_ALIGNMENT
3577 static struct mbuf *
3578 alc_fixup_rx(struct ifnet *ifp, struct mbuf *m)
3582 uint16_t *src, *dst;
3584 src = mtod(m, uint16_t *);
3587 if (m->m_next == NULL) {
3588 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
3594 * Append a new mbuf to received mbuf chain and copy ethernet
3595 * header from the mbuf chain. This can save lots of CPU
3596 * cycles for jumbo frame.
3598 MGETHDR(n, M_NOWAIT, MT_DATA);
3604 bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
3605 m->m_data += ETHER_HDR_LEN;
3606 m->m_len -= ETHER_HDR_LEN;
3607 n->m_len = ETHER_HDR_LEN;
3608 M_MOVE_PKTHDR(n, m);
3614 /* Receive a frame. */
3616 alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
3618 struct alc_rxdesc *rxd;
3620 struct mbuf *mp, *m;
3621 uint32_t rdinfo, status, vtag;
3622 int count, nsegs, rx_cons;
3625 status = le32toh(rrd->status);
3626 rdinfo = le32toh(rrd->rdinfo);
3627 rx_cons = RRD_RD_IDX(rdinfo);
3628 nsegs = RRD_RD_CNT(rdinfo);
3630 sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
3631 if ((status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) != 0) {
3633 * We want to pass the following frames to upper
3634 * layer regardless of error status of Rx return
3637 * o IP/TCP/UDP checksum is bad.
3638 * o frame length and protocol specific length
3641 * Force network stack compute checksum for
3644 status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
3645 if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
3646 RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
3650 for (count = 0; count < nsegs; count++,
3651 ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
3652 rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
3654 /* Add a new receive buffer to the ring. */
3655 if (alc_newbuf(sc, rxd) != 0) {
3657 /* Reuse Rx buffers. */
3658 if (sc->alc_cdata.alc_rxhead != NULL)
3659 m_freem(sc->alc_cdata.alc_rxhead);
3664 * Assume we've received a full sized frame.
3665 * Actual size is fixed when we encounter the end of
3666 * multi-segmented frame.
3668 mp->m_len = sc->alc_buf_size;
3670 /* Chain received mbufs. */
3671 if (sc->alc_cdata.alc_rxhead == NULL) {
3672 sc->alc_cdata.alc_rxhead = mp;
3673 sc->alc_cdata.alc_rxtail = mp;
3675 mp->m_flags &= ~M_PKTHDR;
3676 sc->alc_cdata.alc_rxprev_tail =
3677 sc->alc_cdata.alc_rxtail;
3678 sc->alc_cdata.alc_rxtail->m_next = mp;
3679 sc->alc_cdata.alc_rxtail = mp;
3682 if (count == nsegs - 1) {
3683 /* Last desc. for this frame. */
3684 m = sc->alc_cdata.alc_rxhead;
3685 m->m_flags |= M_PKTHDR;
3687 * It seems that L1C/L2C controller has no way
3688 * to tell hardware to strip CRC bytes.
3691 sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
3693 /* Set last mbuf size. */
3694 mp->m_len = sc->alc_cdata.alc_rxlen -
3695 (nsegs - 1) * sc->alc_buf_size;
3696 /* Remove the CRC bytes in chained mbufs. */
3697 if (mp->m_len <= ETHER_CRC_LEN) {
3698 sc->alc_cdata.alc_rxtail =
3699 sc->alc_cdata.alc_rxprev_tail;
3700 sc->alc_cdata.alc_rxtail->m_len -=
3701 (ETHER_CRC_LEN - mp->m_len);
3702 sc->alc_cdata.alc_rxtail->m_next = NULL;
3705 mp->m_len -= ETHER_CRC_LEN;
3708 m->m_len = m->m_pkthdr.len;
3709 m->m_pkthdr.rcvif = ifp;
3711 * Due to hardware bugs, Rx checksum offloading
3712 * was intentionally disabled.
3714 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
3715 (status & RRD_VLAN_TAG) != 0) {
3716 vtag = RRD_VLAN(le32toh(rrd->vtag));
3717 m->m_pkthdr.ether_vtag = ntohs(vtag);
3718 m->m_flags |= M_VLANTAG;
3720 #ifndef __NO_STRICT_ALIGNMENT
3721 m = alc_fixup_rx(ifp, m);
3727 (*ifp->if_input)(ifp, m);
3732 /* Reset mbuf chains. */
3733 ALC_RXCHAIN_RESET(sc);
3739 struct alc_softc *sc;
3740 struct mii_data *mii;
3742 sc = (struct alc_softc *)arg;
3744 ALC_LOCK_ASSERT(sc);
3746 mii = device_get_softc(sc->alc_miibus);
3748 alc_stats_update(sc);
3750 * alc(4) does not rely on Tx completion interrupts to reclaim
3751 * transferred buffers. Instead Tx completion interrupts are
3752 * used to hint for scheduling Tx task. So it's necessary to
3753 * release transmitted buffers by kicking Tx completion
3754 * handler. This limits the maximum reclamation delay to a hz.
3758 callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3762 alc_osc_reset(struct alc_softc *sc)
3766 reg = CSR_READ_4(sc, ALC_MISC3);
3767 reg &= ~MISC3_25M_BY_SW;
3768 reg |= MISC3_25M_NOTO_INTNL;
3769 CSR_WRITE_4(sc, ALC_MISC3, reg);
3771 reg = CSR_READ_4(sc, ALC_MISC);
3772 if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0) {
3774 * Restore over-current protection default value.
3775 * This value could be reset by MAC reset.
3777 reg &= ~MISC_PSW_OCP_MASK;
3778 reg |= (MISC_PSW_OCP_DEFAULT << MISC_PSW_OCP_SHIFT);
3779 reg &= ~MISC_INTNLOSC_OPEN;
3780 CSR_WRITE_4(sc, ALC_MISC, reg);
3781 CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
3782 reg = CSR_READ_4(sc, ALC_MISC2);
3783 reg &= ~MISC2_CALB_START;
3784 CSR_WRITE_4(sc, ALC_MISC2, reg);
3785 CSR_WRITE_4(sc, ALC_MISC2, reg | MISC2_CALB_START);
3788 reg &= ~MISC_INTNLOSC_OPEN;
3789 /* Disable isolate for revision A devices. */
3790 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
3791 reg &= ~MISC_ISO_ENB;
3792 CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
3793 CSR_WRITE_4(sc, ALC_MISC, reg);
3800 alc_reset(struct alc_softc *sc)
3802 uint32_t pmcfg, reg;
3806 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3807 /* Reset workaround. */
3808 CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, 1);
3809 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
3810 (sc->alc_rev & 0x01) != 0) {
3811 /* Disable L0s/L1s before reset. */
3812 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
3813 if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
3815 pmcfg &= ~(PM_CFG_ASPM_L0S_ENB |
3816 PM_CFG_ASPM_L1_ENB);
3817 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
3821 reg = CSR_READ_4(sc, ALC_MASTER_CFG);
3822 reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
3823 CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3825 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3826 for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3828 if (CSR_READ_4(sc, ALC_MBOX_RD0_PROD_IDX) == 0)
3832 device_printf(sc->alc_dev, "MAC reset timeout!\n");
3834 for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3836 if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
3840 device_printf(sc->alc_dev, "master reset timeout!\n");
3842 for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3843 reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3844 if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC |
3845 IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3850 device_printf(sc->alc_dev, "reset timeout(0x%08x)!\n", reg);
3852 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3853 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
3854 (sc->alc_rev & 0x01) != 0) {
3855 reg = CSR_READ_4(sc, ALC_MASTER_CFG);
3856 reg |= MASTER_CLK_SEL_DIS;
3857 CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3858 /* Restore L0s/L1s config. */
3859 if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
3861 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
3865 reg = CSR_READ_4(sc, ALC_MISC3);
3866 reg &= ~MISC3_25M_BY_SW;
3867 reg |= MISC3_25M_NOTO_INTNL;
3868 CSR_WRITE_4(sc, ALC_MISC3, reg);
3869 reg = CSR_READ_4(sc, ALC_MISC);
3870 reg &= ~MISC_INTNLOSC_OPEN;
3871 if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
3872 reg &= ~MISC_ISO_ENB;
3873 CSR_WRITE_4(sc, ALC_MISC, reg);
3876 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3877 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3878 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2)
3879 CSR_WRITE_4(sc, ALC_SERDES_LOCK,
3880 CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
3881 SERDES_PHY_CLK_SLOWDOWN);
3887 struct alc_softc *sc;
3889 sc = (struct alc_softc *)xsc;
3891 alc_init_locked(sc);
3896 alc_init_locked(struct alc_softc *sc)
3899 struct mii_data *mii;
3900 uint8_t eaddr[ETHER_ADDR_LEN];
3902 uint32_t reg, rxf_hi, rxf_lo;
3904 ALC_LOCK_ASSERT(sc);
3907 mii = device_get_softc(sc->alc_miibus);
3909 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3912 * Cancel any pending I/O.
3916 * Reset the chip to a known state.
3920 /* Initialize Rx descriptors. */
3921 if (alc_init_rx_ring(sc) != 0) {
3922 device_printf(sc->alc_dev, "no memory for Rx buffers.\n");
3926 alc_init_rr_ring(sc);
3927 alc_init_tx_ring(sc);
3931 /* Enable all clocks. */
3932 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3933 CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, CLK_GATING_DMAW_ENB |
3934 CLK_GATING_DMAR_ENB | CLK_GATING_TXQ_ENB |
3935 CLK_GATING_RXQ_ENB | CLK_GATING_TXMAC_ENB |
3936 CLK_GATING_RXMAC_ENB);
3937 if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0)
3938 CSR_WRITE_4(sc, ALC_IDLE_DECISN_TIMER,
3939 IDLE_DECISN_TIMER_DEFAULT_1MS);
3941 CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
3943 /* Reprogram the station address. */
3944 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
3945 CSR_WRITE_4(sc, ALC_PAR0,
3946 eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
3947 CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
3949 * Clear WOL status and disable all WOL feature as WOL
3950 * would interfere Rx operation under normal environments.
3952 CSR_READ_4(sc, ALC_WOL_CFG);
3953 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
3954 /* Set Tx descriptor base addresses. */
3955 paddr = sc->alc_rdata.alc_tx_ring_paddr;
3956 CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3957 CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3958 /* We don't use high priority ring. */
3959 CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
3960 /* Set Tx descriptor counter. */
3961 CSR_WRITE_4(sc, ALC_TD_RING_CNT,
3962 (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
3963 /* Set Rx descriptor base addresses. */
3964 paddr = sc->alc_rdata.alc_rx_ring_paddr;
3965 CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3966 CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3967 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3968 /* We use one Rx ring. */
3969 CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
3970 CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
3971 CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
3973 /* Set Rx descriptor counter. */
3974 CSR_WRITE_4(sc, ALC_RD_RING_CNT,
3975 (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
3978 * Let hardware split jumbo frames into alc_max_buf_sized chunks.
3979 * if it do not fit the buffer size. Rx return descriptor holds
3980 * a counter that indicates how many fragments were made by the
3981 * hardware. The buffer size should be multiple of 8 bytes.
3982 * Since hardware has limit on the size of buffer size, always
3983 * use the maximum value.
3984 * For strict-alignment architectures make sure to reduce buffer
3985 * size by 8 bytes to make room for alignment fixup.
3987 #ifndef __NO_STRICT_ALIGNMENT
3988 sc->alc_buf_size = RX_BUF_SIZE_MAX - sizeof(uint64_t);
3990 sc->alc_buf_size = RX_BUF_SIZE_MAX;
3992 CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
3994 paddr = sc->alc_rdata.alc_rr_ring_paddr;
3995 /* Set Rx return descriptor base addresses. */
3996 CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3997 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3998 /* We use one Rx return ring. */
3999 CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
4000 CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
4001 CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
4003 /* Set Rx return descriptor counter. */
4004 CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
4005 (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
4006 paddr = sc->alc_rdata.alc_cmb_paddr;
4007 CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
4008 paddr = sc->alc_rdata.alc_smb_paddr;
4009 CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
4010 CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
4012 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
4013 /* Reconfigure SRAM - Vendor magic. */
4014 CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
4015 CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
4016 CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
4017 CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
4018 CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
4019 CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
4020 CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
4021 CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
4024 /* Tell hardware that we're ready to load DMA blocks. */
4025 CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
4027 /* Configure interrupt moderation timer. */
4028 reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
4029 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0)
4030 reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
4031 CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
4033 * We don't want to automatic interrupt clear as task queue
4034 * for the interrupt should know interrupt status.
4036 reg = CSR_READ_4(sc, ALC_MASTER_CFG);
4037 reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
4038 reg |= MASTER_SA_TIMER_ENB;
4039 if (ALC_USECS(sc->alc_int_rx_mod) != 0)
4040 reg |= MASTER_IM_RX_TIMER_ENB;
4041 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0 &&
4042 ALC_USECS(sc->alc_int_tx_mod) != 0)
4043 reg |= MASTER_IM_TX_TIMER_ENB;
4044 CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
4046 * Disable interrupt re-trigger timer. We don't want automatic
4047 * re-triggering of un-ACKed interrupts.
4049 CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
4050 /* Configure CMB. */
4051 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4052 CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, ALC_TX_RING_CNT / 3);
4053 CSR_WRITE_4(sc, ALC_CMB_TX_TIMER,
4054 ALC_USECS(sc->alc_int_tx_mod));
4056 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
4057 CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
4058 CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
4060 CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
4063 * Hardware can be configured to issue SMB interrupt based
4064 * on programmed interval. Since there is a callout that is
4065 * invoked for every hz in driver we use that instead of
4066 * relying on periodic SMB interrupt.
4068 CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
4069 /* Clear MAC statistics. */
4070 alc_stats_clear(sc);
4073 * Always use maximum frame size that controller can support.
4074 * Otherwise received frames that has larger frame length
4075 * than alc(4) MTU would be silently dropped in hardware. This
4076 * would make path-MTU discovery hard as sender wouldn't get
4077 * any responses from receiver. alc(4) supports
4078 * multi-fragmented frames on Rx path so it has no issue on
4079 * assembling fragmented frames. Using maximum frame size also
4080 * removes the need to reinitialize hardware when interface
4081 * MTU configuration was changed.
4083 * Be conservative in what you do, be liberal in what you
4084 * accept from others - RFC 793.
4086 CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
4088 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4089 /* Disable header split(?) */
4090 CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
4092 /* Configure IPG/IFG parameters. */
4093 CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
4094 ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) &
4095 IPG_IFG_IPGT_MASK) |
4096 ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) &
4097 IPG_IFG_MIFG_MASK) |
4098 ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) &
4099 IPG_IFG_IPG1_MASK) |
4100 ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) &
4101 IPG_IFG_IPG2_MASK));
4102 /* Set parameters for half-duplex media. */
4103 CSR_WRITE_4(sc, ALC_HDPX_CFG,
4104 ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
4105 HDPX_CFG_LCOL_MASK) |
4106 ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
4107 HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
4108 ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
4109 HDPX_CFG_ABEBT_MASK) |
4110 ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
4111 HDPX_CFG_JAMIPG_MASK));
4115 * Set TSO/checksum offload threshold. For frames that is
4116 * larger than this threshold, hardware wouldn't do
4117 * TSO/checksum offloading.
4119 reg = (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
4120 TSO_OFFLOAD_THRESH_MASK;
4121 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
4122 reg |= TSO_OFFLOAD_ERRLGPKT_DROP_ENB;
4123 CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH, reg);
4124 /* Configure TxQ. */
4125 reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
4126 TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
4127 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
4128 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
4130 reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
4131 TXQ_CFG_TD_BURST_MASK;
4132 reg |= TXQ_CFG_IP_OPTION_ENB | TXQ_CFG_8023_ENB;
4133 CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
4134 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4135 reg = (TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q1_BURST_SHIFT |
4136 TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q2_BURST_SHIFT |
4137 TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q3_BURST_SHIFT |
4138 HQTD_CFG_BURST_ENB);
4139 CSR_WRITE_4(sc, ALC_HQTD_CFG, reg);
4140 reg = WRR_PRI_RESTRICT_NONE;
4141 reg |= (WRR_PRI_DEFAULT << WRR_PRI0_SHIFT |
4142 WRR_PRI_DEFAULT << WRR_PRI1_SHIFT |
4143 WRR_PRI_DEFAULT << WRR_PRI2_SHIFT |
4144 WRR_PRI_DEFAULT << WRR_PRI3_SHIFT);
4145 CSR_WRITE_4(sc, ALC_WRR, reg);
4147 /* Configure Rx free descriptor pre-fetching. */
4148 CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
4149 ((RX_RD_FREE_THRESH_HI_DEFAULT <<
4150 RX_RD_FREE_THRESH_HI_SHIFT) & RX_RD_FREE_THRESH_HI_MASK) |
4151 ((RX_RD_FREE_THRESH_LO_DEFAULT <<
4152 RX_RD_FREE_THRESH_LO_SHIFT) & RX_RD_FREE_THRESH_LO_MASK));
4156 * Configure flow control parameters.
4157 * XON : 80% of Rx FIFO
4158 * XOFF : 30% of Rx FIFO
4160 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4161 reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
4162 reg &= SRAM_RX_FIFO_LEN_MASK;
4165 reg -= RX_FIFO_PAUSE_816X_RSVD;
4167 reg -= RX_BUF_SIZE_MAX;
4169 CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
4170 ((reg << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
4171 RX_FIFO_PAUSE_THRESH_LO_MASK) |
4172 (((RX_FIFO_PAUSE_816X_RSVD / 8) <<
4173 RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
4174 RX_FIFO_PAUSE_THRESH_HI_MASK));
4175 } else if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
4176 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132) {
4177 reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
4178 rxf_hi = (reg * 8) / 10;
4179 rxf_lo = (reg * 3) / 10;
4180 CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
4181 ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
4182 RX_FIFO_PAUSE_THRESH_LO_MASK) |
4183 ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
4184 RX_FIFO_PAUSE_THRESH_HI_MASK));
4187 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4188 /* Disable RSS until I understand L1C/L2C's RSS logic. */
4189 CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
4190 CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
4193 /* Configure RxQ. */
4194 reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
4195 RXQ_CFG_RD_BURST_MASK;
4196 reg |= RXQ_CFG_RSS_MODE_DIS;
4197 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
4198 reg |= (RXQ_CFG_816X_IDT_TBL_SIZE_DEFAULT <<
4199 RXQ_CFG_816X_IDT_TBL_SIZE_SHIFT) &
4200 RXQ_CFG_816X_IDT_TBL_SIZE_MASK;
4201 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0 &&
4202 sc->alc_ident->deviceid != DEVICEID_ATHEROS_AR8151_V2)
4203 reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_1M;
4204 CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4206 /* Configure DMA parameters. */
4207 reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
4209 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
4210 reg |= DMA_CFG_CMB_ENB;
4211 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
4212 reg |= DMA_CFG_SMB_ENB;
4214 reg |= DMA_CFG_SMB_DIS;
4215 reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
4216 DMA_CFG_RD_BURST_SHIFT;
4217 reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
4218 DMA_CFG_WR_BURST_SHIFT;
4219 reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
4220 DMA_CFG_RD_DELAY_CNT_MASK;
4221 reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
4222 DMA_CFG_WR_DELAY_CNT_MASK;
4223 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
4224 switch (AR816X_REV(sc->alc_rev)) {
4227 reg |= DMA_CFG_RD_CHNL_SEL_1;
4232 reg |= DMA_CFG_RD_CHNL_SEL_3;
4236 CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
4239 * Configure Tx/Rx MACs.
4240 * - Auto-padding for short frames.
4241 * - Enable CRC generation.
4242 * Actual reconfiguration of MAC for resolved speed/duplex
4243 * is followed after detection of link establishment.
4244 * AR813x/AR815x always does checksum computation regardless
4245 * of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
4246 * have bug in protocol field in Rx return structure so
4247 * these controllers can't handle fragmented frames. Disable
4248 * Rx checksum offloading until there is a newer controller
4249 * that has sane implementation.
4251 reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
4252 ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
4253 MAC_CFG_PREAMBLE_MASK);
4254 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
4255 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
4256 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
4257 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
4258 reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
4259 if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
4260 reg |= MAC_CFG_SPEED_10_100;
4262 reg |= MAC_CFG_SPEED_1000;
4263 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4265 /* Set up the receive filter. */
4269 /* Acknowledge all pending interrupts and clear it. */
4270 CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
4271 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4272 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
4274 ifp->if_drv_flags |= IFF_DRV_RUNNING;
4275 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4277 sc->alc_flags &= ~ALC_FLAG_LINK;
4278 /* Switch to the current media. */
4279 alc_mediachange_locked(sc);
4281 callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
4285 alc_stop(struct alc_softc *sc)
4288 struct alc_txdesc *txd;
4289 struct alc_rxdesc *rxd;
4293 ALC_LOCK_ASSERT(sc);
4295 * Mark the interface down and cancel the watchdog timer.
4298 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4299 sc->alc_flags &= ~ALC_FLAG_LINK;
4300 callout_stop(&sc->alc_tick_ch);
4301 sc->alc_watchdog_timer = 0;
4302 alc_stats_update(sc);
4303 /* Disable interrupts. */
4304 CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
4305 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4307 reg = CSR_READ_4(sc, ALC_DMA_CFG);
4308 reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
4309 reg |= DMA_CFG_SMB_DIS;
4310 CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
4312 /* Stop Rx/Tx MACs. */
4314 /* Disable interrupts which might be touched in taskq handler. */
4315 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
4316 /* Disable L0s/L1s */
4317 alc_aspm(sc, 0, IFM_UNKNOWN);
4318 /* Reclaim Rx buffers that have been processed. */
4319 if (sc->alc_cdata.alc_rxhead != NULL)
4320 m_freem(sc->alc_cdata.alc_rxhead);
4321 ALC_RXCHAIN_RESET(sc);
4323 * Free Tx/Rx mbufs still in the queues.
4325 for (i = 0; i < ALC_RX_RING_CNT; i++) {
4326 rxd = &sc->alc_cdata.alc_rxdesc[i];
4327 if (rxd->rx_m != NULL) {
4328 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag,
4329 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
4330 bus_dmamap_unload(sc->alc_cdata.alc_rx_tag,
4336 for (i = 0; i < ALC_TX_RING_CNT; i++) {
4337 txd = &sc->alc_cdata.alc_txdesc[i];
4338 if (txd->tx_m != NULL) {
4339 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
4340 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
4341 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
4350 alc_stop_mac(struct alc_softc *sc)
4356 /* Disable Rx/Tx MAC. */
4357 reg = CSR_READ_4(sc, ALC_MAC_CFG);
4358 if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
4359 reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
4360 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4362 for (i = ALC_TIMEOUT; i > 0; i--) {
4363 reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
4364 if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC)) == 0)
4369 device_printf(sc->alc_dev,
4370 "could not disable Rx/Tx MAC(0x%08x)!\n", reg);
4374 alc_start_queue(struct alc_softc *sc)
4379 RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
4380 RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
4385 ALC_LOCK_ASSERT(sc);
4388 cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
4389 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4390 cfg &= ~RXQ_CFG_ENB;
4393 cfg |= RXQ_CFG_QUEUE0_ENB;
4394 CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
4396 cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
4398 CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
4402 alc_stop_queue(struct alc_softc *sc)
4408 reg = CSR_READ_4(sc, ALC_RXQ_CFG);
4409 if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
4410 if ((reg & RXQ_CFG_ENB) != 0) {
4411 reg &= ~RXQ_CFG_ENB;
4412 CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4415 if ((reg & RXQ_CFG_QUEUE0_ENB) != 0) {
4416 reg &= ~RXQ_CFG_QUEUE0_ENB;
4417 CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
4421 reg = CSR_READ_4(sc, ALC_TXQ_CFG);
4422 if ((reg & TXQ_CFG_ENB) != 0) {
4423 reg &= ~TXQ_CFG_ENB;
4424 CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
4427 for (i = ALC_TIMEOUT; i > 0; i--) {
4428 reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
4429 if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
4434 device_printf(sc->alc_dev,
4435 "could not disable RxQ/TxQ (0x%08x)!\n", reg);
4439 alc_init_tx_ring(struct alc_softc *sc)
4441 struct alc_ring_data *rd;
4442 struct alc_txdesc *txd;
4445 ALC_LOCK_ASSERT(sc);
4447 sc->alc_cdata.alc_tx_prod = 0;
4448 sc->alc_cdata.alc_tx_cons = 0;
4449 sc->alc_cdata.alc_tx_cnt = 0;
4451 rd = &sc->alc_rdata;
4452 bzero(rd->alc_tx_ring, ALC_TX_RING_SZ);
4453 for (i = 0; i < ALC_TX_RING_CNT; i++) {
4454 txd = &sc->alc_cdata.alc_txdesc[i];
4458 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
4459 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
4463 alc_init_rx_ring(struct alc_softc *sc)
4465 struct alc_ring_data *rd;
4466 struct alc_rxdesc *rxd;
4469 ALC_LOCK_ASSERT(sc);
4471 sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
4472 sc->alc_morework = 0;
4473 rd = &sc->alc_rdata;
4474 bzero(rd->alc_rx_ring, ALC_RX_RING_SZ);
4475 for (i = 0; i < ALC_RX_RING_CNT; i++) {
4476 rxd = &sc->alc_cdata.alc_rxdesc[i];
4478 rxd->rx_desc = &rd->alc_rx_ring[i];
4479 if (alc_newbuf(sc, rxd) != 0)
4484 * Since controller does not update Rx descriptors, driver
4485 * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
4486 * is enough to ensure coherence.
4488 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
4489 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
4490 /* Let controller know availability of new Rx buffers. */
4491 CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
4497 alc_init_rr_ring(struct alc_softc *sc)
4499 struct alc_ring_data *rd;
4501 ALC_LOCK_ASSERT(sc);
4503 sc->alc_cdata.alc_rr_cons = 0;
4504 ALC_RXCHAIN_RESET(sc);
4506 rd = &sc->alc_rdata;
4507 bzero(rd->alc_rr_ring, ALC_RR_RING_SZ);
4508 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
4509 sc->alc_cdata.alc_rr_ring_map,
4510 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4514 alc_init_cmb(struct alc_softc *sc)
4516 struct alc_ring_data *rd;
4518 ALC_LOCK_ASSERT(sc);
4520 rd = &sc->alc_rdata;
4521 bzero(rd->alc_cmb, ALC_CMB_SZ);
4522 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag, sc->alc_cdata.alc_cmb_map,
4523 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4527 alc_init_smb(struct alc_softc *sc)
4529 struct alc_ring_data *rd;
4531 ALC_LOCK_ASSERT(sc);
4533 rd = &sc->alc_rdata;
4534 bzero(rd->alc_smb, ALC_SMB_SZ);
4535 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag, sc->alc_cdata.alc_smb_map,
4536 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4540 alc_rxvlan(struct alc_softc *sc)
4545 ALC_LOCK_ASSERT(sc);
4548 reg = CSR_READ_4(sc, ALC_MAC_CFG);
4549 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
4550 reg |= MAC_CFG_VLAN_TAG_STRIP;
4552 reg &= ~MAC_CFG_VLAN_TAG_STRIP;
4553 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
4557 alc_rxfilter(struct alc_softc *sc)
4560 struct ifmultiaddr *ifma;
4565 ALC_LOCK_ASSERT(sc);
4569 bzero(mchash, sizeof(mchash));
4570 rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
4571 rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
4572 if ((ifp->if_flags & IFF_BROADCAST) != 0)
4573 rxcfg |= MAC_CFG_BCAST;
4574 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
4575 if ((ifp->if_flags & IFF_PROMISC) != 0)
4576 rxcfg |= MAC_CFG_PROMISC;
4577 if ((ifp->if_flags & IFF_ALLMULTI) != 0)
4578 rxcfg |= MAC_CFG_ALLMULTI;
4579 mchash[0] = 0xFFFFFFFF;
4580 mchash[1] = 0xFFFFFFFF;
4584 if_maddr_rlock(ifp);
4585 TAILQ_FOREACH(ifma, &sc->alc_ifp->if_multiaddrs, ifma_link) {
4586 if (ifma->ifma_addr->sa_family != AF_LINK)
4588 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
4589 ifma->ifma_addr), ETHER_ADDR_LEN);
4590 mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
4592 if_maddr_runlock(ifp);
4595 CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
4596 CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
4597 CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
4601 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4607 value = *(int *)arg1;
4608 error = sysctl_handle_int(oidp, &value, 0, req);
4609 if (error || req->newptr == NULL)
4611 if (value < low || value > high)
4613 *(int *)arg1 = value;
4619 sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS)
4621 return (sysctl_int_range(oidp, arg1, arg2, req,
4622 ALC_PROC_MIN, ALC_PROC_MAX));
4626 sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS)
4629 return (sysctl_int_range(oidp, arg1, arg2, req,
4630 ALC_IM_TIMER_MIN, ALC_IM_TIMER_MAX));