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_var.h>
53 #include <net/if_arp.h>
54 #include <net/ethernet.h>
55 #include <net/if_dl.h>
56 #include <net/if_llc.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59 #include <net/if_vlan_var.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/tcp.h>
66 #include <dev/mii/mii.h>
67 #include <dev/mii/miivar.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
72 #include <machine/bus.h>
73 #include <machine/in_cksum.h>
75 #include <dev/alc/if_alcreg.h>
76 #include <dev/alc/if_alcvar.h>
78 /* "device miibus" required. See GENERIC if you get errors here. */
79 #include "miibus_if.h"
80 #undef ALC_USE_CUSTOM_CSUM
82 #ifdef ALC_USE_CUSTOM_CSUM
83 #define ALC_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
85 #define ALC_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
88 MODULE_DEPEND(alc, pci, 1, 1, 1);
89 MODULE_DEPEND(alc, ether, 1, 1, 1);
90 MODULE_DEPEND(alc, miibus, 1, 1, 1);
93 static int msi_disable = 0;
94 static int msix_disable = 0;
95 TUNABLE_INT("hw.alc.msi_disable", &msi_disable);
96 TUNABLE_INT("hw.alc.msix_disable", &msix_disable);
99 * Devices supported by this driver.
101 static struct alc_ident alc_ident_table[] = {
102 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8131, 9 * 1024,
103 "Atheros AR8131 PCIe Gigabit Ethernet" },
104 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8132, 9 * 1024,
105 "Atheros AR8132 PCIe Fast Ethernet" },
106 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151, 6 * 1024,
107 "Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
108 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8151_V2, 6 * 1024,
109 "Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
110 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B, 6 * 1024,
111 "Atheros AR8152 v1.1 PCIe Fast Ethernet" },
112 { VENDORID_ATHEROS, DEVICEID_ATHEROS_AR8152_B2, 6 * 1024,
113 "Atheros AR8152 v2.0 PCIe Fast Ethernet" },
117 static void alc_aspm(struct alc_softc *, int);
118 static int alc_attach(device_t);
119 static int alc_check_boundary(struct alc_softc *);
120 static int alc_detach(device_t);
121 static void alc_disable_l0s_l1(struct alc_softc *);
122 static int alc_dma_alloc(struct alc_softc *);
123 static void alc_dma_free(struct alc_softc *);
124 static void alc_dmamap_cb(void *, bus_dma_segment_t *, int, int);
125 static int alc_encap(struct alc_softc *, struct mbuf **);
126 static struct alc_ident *
127 alc_find_ident(device_t);
128 #ifndef __NO_STRICT_ALIGNMENT
130 alc_fixup_rx(struct ifnet *, struct mbuf *);
132 static void alc_get_macaddr(struct alc_softc *);
133 static void alc_init(void *);
134 static void alc_init_cmb(struct alc_softc *);
135 static void alc_init_locked(struct alc_softc *);
136 static void alc_init_rr_ring(struct alc_softc *);
137 static int alc_init_rx_ring(struct alc_softc *);
138 static void alc_init_smb(struct alc_softc *);
139 static void alc_init_tx_ring(struct alc_softc *);
140 static void alc_int_task(void *, int);
141 static int alc_intr(void *);
142 static int alc_ioctl(struct ifnet *, u_long, caddr_t);
143 static void alc_mac_config(struct alc_softc *);
144 static int alc_miibus_readreg(device_t, int, int);
145 static void alc_miibus_statchg(device_t);
146 static int alc_miibus_writereg(device_t, int, int, int);
147 static int alc_mediachange(struct ifnet *);
148 static void alc_mediastatus(struct ifnet *, struct ifmediareq *);
149 static int alc_newbuf(struct alc_softc *, struct alc_rxdesc *);
150 static void alc_phy_down(struct alc_softc *);
151 static void alc_phy_reset(struct alc_softc *);
152 static int alc_probe(device_t);
153 static void alc_reset(struct alc_softc *);
154 static int alc_resume(device_t);
155 static void alc_rxeof(struct alc_softc *, struct rx_rdesc *);
156 static int alc_rxintr(struct alc_softc *, int);
157 static void alc_rxfilter(struct alc_softc *);
158 static void alc_rxvlan(struct alc_softc *);
159 static void alc_setlinkspeed(struct alc_softc *);
160 static void alc_setwol(struct alc_softc *);
161 static int alc_shutdown(device_t);
162 static void alc_start(struct ifnet *);
163 static void alc_start_locked(struct ifnet *);
164 static void alc_start_queue(struct alc_softc *);
165 static void alc_stats_clear(struct alc_softc *);
166 static void alc_stats_update(struct alc_softc *);
167 static void alc_stop(struct alc_softc *);
168 static void alc_stop_mac(struct alc_softc *);
169 static void alc_stop_queue(struct alc_softc *);
170 static int alc_suspend(device_t);
171 static void alc_sysctl_node(struct alc_softc *);
172 static void alc_tick(void *);
173 static void alc_txeof(struct alc_softc *);
174 static void alc_watchdog(struct alc_softc *);
175 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
176 static int sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS);
177 static int sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS);
179 static device_method_t alc_methods[] = {
180 /* Device interface. */
181 DEVMETHOD(device_probe, alc_probe),
182 DEVMETHOD(device_attach, alc_attach),
183 DEVMETHOD(device_detach, alc_detach),
184 DEVMETHOD(device_shutdown, alc_shutdown),
185 DEVMETHOD(device_suspend, alc_suspend),
186 DEVMETHOD(device_resume, alc_resume),
189 DEVMETHOD(miibus_readreg, alc_miibus_readreg),
190 DEVMETHOD(miibus_writereg, alc_miibus_writereg),
191 DEVMETHOD(miibus_statchg, alc_miibus_statchg),
196 static driver_t alc_driver = {
199 sizeof(struct alc_softc)
202 static devclass_t alc_devclass;
204 DRIVER_MODULE(alc, pci, alc_driver, alc_devclass, 0, 0);
205 DRIVER_MODULE(miibus, alc, miibus_driver, miibus_devclass, 0, 0);
207 static struct resource_spec alc_res_spec_mem[] = {
208 { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
212 static struct resource_spec alc_irq_spec_legacy[] = {
213 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
217 static struct resource_spec alc_irq_spec_msi[] = {
218 { SYS_RES_IRQ, 1, RF_ACTIVE },
222 static struct resource_spec alc_irq_spec_msix[] = {
223 { SYS_RES_IRQ, 1, RF_ACTIVE },
227 static uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0 };
230 alc_miibus_readreg(device_t dev, int phy, int reg)
232 struct alc_softc *sc;
236 sc = device_get_softc(dev);
239 * For AR8132 fast ethernet controller, do not report 1000baseT
240 * capability to mii(4). Even though AR8132 uses the same
241 * model/revision number of F1 gigabit PHY, the PHY has no
242 * ability to establish 1000baseT link.
244 if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
248 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
249 MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
250 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
252 v = CSR_READ_4(sc, ALC_MDIO);
253 if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
258 device_printf(sc->alc_dev, "phy read timeout : %d\n", reg);
262 return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
266 alc_miibus_writereg(device_t dev, int phy, int reg, int val)
268 struct alc_softc *sc;
272 sc = device_get_softc(dev);
274 CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
275 (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
276 MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
277 for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
279 v = CSR_READ_4(sc, ALC_MDIO);
280 if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
285 device_printf(sc->alc_dev, "phy write timeout : %d\n", reg);
291 alc_miibus_statchg(device_t dev)
293 struct alc_softc *sc;
294 struct mii_data *mii;
298 sc = device_get_softc(dev);
300 mii = device_get_softc(sc->alc_miibus);
302 if (mii == NULL || ifp == NULL ||
303 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
306 sc->alc_flags &= ~ALC_FLAG_LINK;
307 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
308 (IFM_ACTIVE | IFM_AVALID)) {
309 switch (IFM_SUBTYPE(mii->mii_media_active)) {
312 sc->alc_flags |= ALC_FLAG_LINK;
315 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
316 sc->alc_flags |= ALC_FLAG_LINK;
323 /* Stop Rx/Tx MACs. */
326 /* Program MACs with resolved speed/duplex/flow-control. */
327 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
330 /* Re-enable Tx/Rx MACs. */
331 reg = CSR_READ_4(sc, ALC_MAC_CFG);
332 reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
333 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
334 alc_aspm(sc, IFM_SUBTYPE(mii->mii_media_active));
339 alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
341 struct alc_softc *sc;
342 struct mii_data *mii;
346 if ((ifp->if_flags & IFF_UP) == 0) {
350 mii = device_get_softc(sc->alc_miibus);
353 ifmr->ifm_status = mii->mii_media_status;
354 ifmr->ifm_active = mii->mii_media_active;
359 alc_mediachange(struct ifnet *ifp)
361 struct alc_softc *sc;
362 struct mii_data *mii;
363 struct mii_softc *miisc;
368 mii = device_get_softc(sc->alc_miibus);
369 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
371 error = mii_mediachg(mii);
377 static struct alc_ident *
378 alc_find_ident(device_t dev)
380 struct alc_ident *ident;
381 uint16_t vendor, devid;
383 vendor = pci_get_vendor(dev);
384 devid = pci_get_device(dev);
385 for (ident = alc_ident_table; ident->name != NULL; ident++) {
386 if (vendor == ident->vendorid && devid == ident->deviceid)
394 alc_probe(device_t dev)
396 struct alc_ident *ident;
398 ident = alc_find_ident(dev);
400 device_set_desc(dev, ident->name);
401 return (BUS_PROBE_DEFAULT);
408 alc_get_macaddr(struct alc_softc *sc)
415 opt = CSR_READ_4(sc, ALC_OPT_CFG);
416 if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
417 (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
419 * EEPROM found, let TWSI reload EEPROM configuration.
420 * This will set ethernet address of controller.
423 switch (sc->alc_ident->deviceid) {
424 case DEVICEID_ATHEROS_AR8131:
425 case DEVICEID_ATHEROS_AR8132:
426 if ((opt & OPT_CFG_CLK_ENB) == 0) {
427 opt |= OPT_CFG_CLK_ENB;
428 CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
429 CSR_READ_4(sc, ALC_OPT_CFG);
433 case DEVICEID_ATHEROS_AR8151:
434 case DEVICEID_ATHEROS_AR8151_V2:
435 case DEVICEID_ATHEROS_AR8152_B:
436 case DEVICEID_ATHEROS_AR8152_B2:
437 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
438 ALC_MII_DBG_ADDR, 0x00);
439 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
441 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
442 ALC_MII_DBG_DATA, val & 0xFF7F);
443 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
444 ALC_MII_DBG_ADDR, 0x3B);
445 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
447 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
448 ALC_MII_DBG_DATA, val | 0x0008);
453 CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
454 CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
455 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
456 CSR_READ_4(sc, ALC_WOL_CFG);
458 CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
459 TWSI_CFG_SW_LD_START);
460 for (i = 100; i > 0; i--) {
462 if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
463 TWSI_CFG_SW_LD_START) == 0)
467 device_printf(sc->alc_dev,
468 "reloading EEPROM timeout!\n");
471 device_printf(sc->alc_dev, "EEPROM not found!\n");
474 switch (sc->alc_ident->deviceid) {
475 case DEVICEID_ATHEROS_AR8131:
476 case DEVICEID_ATHEROS_AR8132:
477 if ((opt & OPT_CFG_CLK_ENB) != 0) {
478 opt &= ~OPT_CFG_CLK_ENB;
479 CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
480 CSR_READ_4(sc, ALC_OPT_CFG);
484 case DEVICEID_ATHEROS_AR8151:
485 case DEVICEID_ATHEROS_AR8151_V2:
486 case DEVICEID_ATHEROS_AR8152_B:
487 case DEVICEID_ATHEROS_AR8152_B2:
488 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
489 ALC_MII_DBG_ADDR, 0x00);
490 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
492 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
493 ALC_MII_DBG_DATA, val | 0x0080);
494 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
495 ALC_MII_DBG_ADDR, 0x3B);
496 val = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
498 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
499 ALC_MII_DBG_DATA, val & 0xFFF7);
505 ea[0] = CSR_READ_4(sc, ALC_PAR0);
506 ea[1] = CSR_READ_4(sc, ALC_PAR1);
507 sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
508 sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
509 sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
510 sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
511 sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
512 sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
516 alc_disable_l0s_l1(struct alc_softc *sc)
520 /* Another magic from vendor. */
521 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
522 pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
523 PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK |
524 PM_CFG_SERDES_PD_EX_L1);
525 pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
526 PM_CFG_SERDES_L1_ENB;
527 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
531 alc_phy_reset(struct alc_softc *sc)
535 /* Reset magic from Linux. */
536 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
537 CSR_READ_2(sc, ALC_GPHY_CFG);
540 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
541 GPHY_CFG_SEL_ANA_RESET);
542 CSR_READ_2(sc, ALC_GPHY_CFG);
545 /* DSP fixup, Vendor magic. */
546 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
547 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
548 ALC_MII_DBG_ADDR, 0x000A);
549 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
551 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
552 ALC_MII_DBG_DATA, data & 0xDFFF);
554 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
555 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
556 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
557 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
558 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
559 ALC_MII_DBG_ADDR, 0x003B);
560 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
562 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
563 ALC_MII_DBG_DATA, data & 0xFFF7);
566 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151) {
567 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
568 ALC_MII_DBG_ADDR, 0x0029);
569 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
570 ALC_MII_DBG_DATA, 0x929D);
572 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
573 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132 ||
574 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
575 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2) {
576 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
577 ALC_MII_DBG_ADDR, 0x0029);
578 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
579 ALC_MII_DBG_DATA, 0xB6DD);
582 /* Load DSP codes, vendor magic. */
583 data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
584 ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
585 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
586 ALC_MII_DBG_ADDR, MII_ANA_CFG18);
587 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
588 ALC_MII_DBG_DATA, data);
590 data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
591 ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
593 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
594 ALC_MII_DBG_ADDR, MII_ANA_CFG5);
595 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
596 ALC_MII_DBG_DATA, data);
598 data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
599 ANA_LONG_CABLE_TH_100_MASK) |
600 ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
601 ANA_SHORT_CABLE_TH_100_SHIFT) |
602 ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
603 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
604 ALC_MII_DBG_ADDR, MII_ANA_CFG54);
605 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
606 ALC_MII_DBG_DATA, data);
608 data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
609 ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
610 ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
611 ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
612 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
613 ALC_MII_DBG_ADDR, MII_ANA_CFG4);
614 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
615 ALC_MII_DBG_DATA, data);
617 data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
618 ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
620 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
621 ALC_MII_DBG_ADDR, MII_ANA_CFG0);
622 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
623 ALC_MII_DBG_DATA, data);
626 /* Disable hibernation. */
627 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
629 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
632 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
635 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
637 data = alc_miibus_readreg(sc->alc_dev, sc->alc_phyaddr,
640 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
645 alc_phy_down(struct alc_softc *sc)
648 switch (sc->alc_ident->deviceid) {
649 case DEVICEID_ATHEROS_AR8151:
650 case DEVICEID_ATHEROS_AR8151_V2:
652 * GPHY power down caused more problems on AR8151 v2.0.
653 * When driver is reloaded after GPHY power down,
654 * accesses to PHY/MAC registers hung the system. Only
655 * cold boot recovered from it. I'm not sure whether
656 * AR8151 v1.0 also requires this one though. I don't
657 * have AR8151 v1.0 controller in hand.
658 * The only option left is to isolate the PHY and
659 * initiates power down the PHY which in turn saves
660 * more power when driver is unloaded.
662 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
663 MII_BMCR, BMCR_ISO | BMCR_PDOWN);
666 /* Force PHY down. */
667 CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
668 GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
676 alc_aspm(struct alc_softc *sc, int media)
683 pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
684 if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
685 (ALC_FLAG_APS | ALC_FLAG_PCIE))
686 linkcfg = CSR_READ_2(sc, sc->alc_expcap +
690 pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
691 pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
692 pmcfg |= PM_CFG_MAC_ASPM_CHK;
693 pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
694 pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
696 if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
697 /* Disable extended sync except AR8152 B v1.0 */
698 linkcfg &= ~PCIEM_LINK_CTL_EXTENDED_SYNC;
699 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
700 sc->alc_rev == ATHEROS_AR8152_B_V10)
701 linkcfg |= PCIEM_LINK_CTL_EXTENDED_SYNC;
702 CSR_WRITE_2(sc, sc->alc_expcap + PCIER_LINK_CTL,
704 pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
706 pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
707 PM_CFG_L1_ENTRY_TIMER_SHIFT);
708 pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
709 pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
710 PM_CFG_PM_REQ_TIMER_SHIFT);
711 pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
714 if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
715 if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
716 pmcfg |= PM_CFG_ASPM_L0S_ENB;
717 if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
718 pmcfg |= PM_CFG_ASPM_L1_ENB;
719 if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
720 if (sc->alc_ident->deviceid ==
721 DEVICEID_ATHEROS_AR8152_B)
722 pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
723 pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
724 PM_CFG_SERDES_PLL_L1_ENB |
725 PM_CFG_SERDES_BUDS_RX_L1_ENB);
726 pmcfg |= PM_CFG_CLK_SWH_L1;
727 if (media == IFM_100_TX || media == IFM_1000_T) {
728 pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
729 switch (sc->alc_ident->deviceid) {
730 case DEVICEID_ATHEROS_AR8152_B:
732 PM_CFG_L1_ENTRY_TIMER_SHIFT);
734 case DEVICEID_ATHEROS_AR8152_B2:
735 case DEVICEID_ATHEROS_AR8151_V2:
737 PM_CFG_L1_ENTRY_TIMER_SHIFT);
741 PM_CFG_L1_ENTRY_TIMER_SHIFT);
746 pmcfg |= PM_CFG_SERDES_L1_ENB |
747 PM_CFG_SERDES_PLL_L1_ENB |
748 PM_CFG_SERDES_BUDS_RX_L1_ENB;
749 pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
750 PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
753 pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
754 PM_CFG_SERDES_PLL_L1_ENB);
755 pmcfg |= PM_CFG_CLK_SWH_L1;
756 if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
757 pmcfg |= PM_CFG_ASPM_L1_ENB;
759 CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
763 alc_attach(device_t dev)
765 struct alc_softc *sc;
767 char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
769 int base, error, i, msic, msixc, state;
770 uint32_t cap, ctl, val;
773 sc = device_get_softc(dev);
776 mtx_init(&sc->alc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
778 callout_init_mtx(&sc->alc_tick_ch, &sc->alc_mtx, 0);
779 TASK_INIT(&sc->alc_int_task, 0, alc_int_task, sc);
780 sc->alc_ident = alc_find_ident(dev);
782 /* Map the device. */
783 pci_enable_busmaster(dev);
784 sc->alc_res_spec = alc_res_spec_mem;
785 sc->alc_irq_spec = alc_irq_spec_legacy;
786 error = bus_alloc_resources(dev, sc->alc_res_spec, sc->alc_res);
788 device_printf(dev, "cannot allocate memory resources.\n");
792 /* Set PHY address. */
793 sc->alc_phyaddr = ALC_PHY_ADDR;
795 /* Initialize DMA parameters. */
796 sc->alc_dma_rd_burst = 0;
797 sc->alc_dma_wr_burst = 0;
798 sc->alc_rcb = DMA_CFG_RCB_64;
799 if (pci_find_cap(dev, PCIY_EXPRESS, &base) == 0) {
800 sc->alc_flags |= ALC_FLAG_PCIE;
801 sc->alc_expcap = base;
802 burst = CSR_READ_2(sc, base + PCIER_DEVICE_CTL);
803 sc->alc_dma_rd_burst =
804 (burst & PCIEM_CTL_MAX_READ_REQUEST) >> 12;
805 sc->alc_dma_wr_burst = (burst & PCIEM_CTL_MAX_PAYLOAD) >> 5;
807 device_printf(dev, "Read request size : %u bytes.\n",
808 alc_dma_burst[sc->alc_dma_rd_burst]);
809 device_printf(dev, "TLP payload size : %u bytes.\n",
810 alc_dma_burst[sc->alc_dma_wr_burst]);
812 if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
813 sc->alc_dma_rd_burst = 3;
814 if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
815 sc->alc_dma_wr_burst = 3;
816 /* Clear data link and flow-control protocol error. */
817 val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
818 val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
819 CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
820 CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
821 CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
822 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
823 CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
824 PCIE_PHYMISC_FORCE_RCV_DET);
825 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B &&
826 pci_get_revid(dev) == ATHEROS_AR8152_B_V10) {
827 val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
828 val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
829 PCIE_PHYMISC2_SERDES_TH_MASK);
830 val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
831 val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
832 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
834 /* Disable ASPM L0S and L1. */
835 cap = CSR_READ_2(sc, base + PCIER_LINK_CAP);
836 if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
837 ctl = CSR_READ_2(sc, base + PCIER_LINK_CTL);
838 if ((ctl & PCIEM_LINK_CTL_RCB) != 0)
839 sc->alc_rcb = DMA_CFG_RCB_128;
841 device_printf(dev, "RCB %u bytes\n",
842 sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
843 state = ctl & PCIEM_LINK_CTL_ASPMC;
844 if (state & PCIEM_LINK_CTL_ASPMC_L0S)
845 sc->alc_flags |= ALC_FLAG_L0S;
846 if (state & PCIEM_LINK_CTL_ASPMC_L1)
847 sc->alc_flags |= ALC_FLAG_L1S;
849 device_printf(sc->alc_dev, "ASPM %s %s\n",
851 state == 0 ? "disabled" : "enabled");
852 alc_disable_l0s_l1(sc);
855 device_printf(sc->alc_dev,
856 "no ASPM support\n");
863 /* Reset the ethernet controller. */
867 * One odd thing is AR8132 uses the same PHY hardware(F1
868 * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
869 * the PHY supports 1000Mbps but that's not true. The PHY
870 * used in AR8132 can't establish gigabit link even if it
871 * shows the same PHY model/revision number of AR8131.
873 switch (sc->alc_ident->deviceid) {
874 case DEVICEID_ATHEROS_AR8152_B:
875 case DEVICEID_ATHEROS_AR8152_B2:
876 sc->alc_flags |= ALC_FLAG_APS;
878 case DEVICEID_ATHEROS_AR8132:
879 sc->alc_flags |= ALC_FLAG_FASTETHER;
881 case DEVICEID_ATHEROS_AR8151:
882 case DEVICEID_ATHEROS_AR8151_V2:
883 sc->alc_flags |= ALC_FLAG_APS;
888 sc->alc_flags |= ALC_FLAG_ASPM_MON | ALC_FLAG_JUMBO;
891 * It seems that AR813x/AR815x has silicon bug for SMB. In
892 * addition, Atheros said that enabling SMB wouldn't improve
893 * performance. However I think it's bad to access lots of
894 * registers to extract MAC statistics.
896 sc->alc_flags |= ALC_FLAG_SMB_BUG;
898 * Don't use Tx CMB. It is known to have silicon bug.
900 sc->alc_flags |= ALC_FLAG_CMB_BUG;
901 sc->alc_rev = pci_get_revid(dev);
902 sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
903 MASTER_CHIP_REV_SHIFT;
905 device_printf(dev, "PCI device revision : 0x%04x\n",
907 device_printf(dev, "Chip id/revision : 0x%04x\n",
910 device_printf(dev, "%u Tx FIFO, %u Rx FIFO\n",
911 CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
912 CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
914 /* Allocate IRQ resources. */
915 msixc = pci_msix_count(dev);
916 msic = pci_msi_count(dev);
918 device_printf(dev, "MSIX count : %d\n", msixc);
919 device_printf(dev, "MSI count : %d\n", msic);
921 /* Prefer MSIX over MSI. */
922 if (msix_disable == 0 || msi_disable == 0) {
923 if (msix_disable == 0 && msixc == ALC_MSIX_MESSAGES &&
924 pci_alloc_msix(dev, &msixc) == 0) {
925 if (msic == ALC_MSIX_MESSAGES) {
927 "Using %d MSIX message(s).\n", msixc);
928 sc->alc_flags |= ALC_FLAG_MSIX;
929 sc->alc_irq_spec = alc_irq_spec_msix;
931 pci_release_msi(dev);
933 if (msi_disable == 0 && (sc->alc_flags & ALC_FLAG_MSIX) == 0 &&
934 msic == ALC_MSI_MESSAGES &&
935 pci_alloc_msi(dev, &msic) == 0) {
936 if (msic == ALC_MSI_MESSAGES) {
938 "Using %d MSI message(s).\n", msic);
939 sc->alc_flags |= ALC_FLAG_MSI;
940 sc->alc_irq_spec = alc_irq_spec_msi;
942 pci_release_msi(dev);
946 error = bus_alloc_resources(dev, sc->alc_irq_spec, sc->alc_irq);
948 device_printf(dev, "cannot allocate IRQ resources.\n");
952 /* Create device sysctl node. */
955 if ((error = alc_dma_alloc(sc) != 0))
958 /* Load station address. */
961 ifp = sc->alc_ifp = if_alloc(IFT_ETHER);
963 device_printf(dev, "cannot allocate ifnet structure.\n");
969 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
970 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
971 ifp->if_ioctl = alc_ioctl;
972 ifp->if_start = alc_start;
973 ifp->if_init = alc_init;
974 ifp->if_snd.ifq_drv_maxlen = ALC_TX_RING_CNT - 1;
975 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
976 IFQ_SET_READY(&ifp->if_snd);
977 ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_TSO4;
978 ifp->if_hwassist = ALC_CSUM_FEATURES | CSUM_TSO;
979 if (pci_find_cap(dev, PCIY_PMG, &base) == 0) {
980 ifp->if_capabilities |= IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST;
981 sc->alc_flags |= ALC_FLAG_PM;
982 sc->alc_pmcap = base;
984 ifp->if_capenable = ifp->if_capabilities;
986 /* Set up MII bus. */
987 error = mii_attach(dev, &sc->alc_miibus, ifp, alc_mediachange,
988 alc_mediastatus, BMSR_DEFCAPMASK, sc->alc_phyaddr, MII_OFFSET_ANY,
991 device_printf(dev, "attaching PHYs failed\n");
995 ether_ifattach(ifp, sc->alc_eaddr);
997 /* VLAN capability setup. */
998 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
999 IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
1000 ifp->if_capenable = ifp->if_capabilities;
1003 * It seems enabling Tx checksum offloading makes more trouble.
1004 * Sometimes the controller does not receive any frames when
1005 * Tx checksum offloading is enabled. I'm not sure whether this
1006 * is a bug in Tx checksum offloading logic or I got broken
1007 * sample boards. To safety, don't enable Tx checksum offloading
1008 * by default but give chance to users to toggle it if they know
1009 * their controllers work without problems.
1011 ifp->if_capenable &= ~IFCAP_TXCSUM;
1012 ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
1014 /* Tell the upper layer(s) we support long frames. */
1015 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1017 /* Create local taskq. */
1018 sc->alc_tq = taskqueue_create_fast("alc_taskq", M_WAITOK,
1019 taskqueue_thread_enqueue, &sc->alc_tq);
1020 if (sc->alc_tq == NULL) {
1021 device_printf(dev, "could not create taskqueue.\n");
1022 ether_ifdetach(ifp);
1026 taskqueue_start_threads(&sc->alc_tq, 1, PI_NET, "%s taskq",
1027 device_get_nameunit(sc->alc_dev));
1029 if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1030 msic = ALC_MSIX_MESSAGES;
1031 else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1032 msic = ALC_MSI_MESSAGES;
1035 for (i = 0; i < msic; i++) {
1036 error = bus_setup_intr(dev, sc->alc_irq[i],
1037 INTR_TYPE_NET | INTR_MPSAFE, alc_intr, NULL, sc,
1038 &sc->alc_intrhand[i]);
1043 device_printf(dev, "could not set up interrupt handler.\n");
1044 taskqueue_free(sc->alc_tq);
1046 ether_ifdetach(ifp);
1058 alc_detach(device_t dev)
1060 struct alc_softc *sc;
1064 sc = device_get_softc(dev);
1067 if (device_is_attached(dev)) {
1068 ether_ifdetach(ifp);
1072 callout_drain(&sc->alc_tick_ch);
1073 taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1076 if (sc->alc_tq != NULL) {
1077 taskqueue_drain(sc->alc_tq, &sc->alc_int_task);
1078 taskqueue_free(sc->alc_tq);
1082 if (sc->alc_miibus != NULL) {
1083 device_delete_child(dev, sc->alc_miibus);
1084 sc->alc_miibus = NULL;
1086 bus_generic_detach(dev);
1094 if ((sc->alc_flags & ALC_FLAG_MSIX) != 0)
1095 msic = ALC_MSIX_MESSAGES;
1096 else if ((sc->alc_flags & ALC_FLAG_MSI) != 0)
1097 msic = ALC_MSI_MESSAGES;
1100 for (i = 0; i < msic; i++) {
1101 if (sc->alc_intrhand[i] != NULL) {
1102 bus_teardown_intr(dev, sc->alc_irq[i],
1103 sc->alc_intrhand[i]);
1104 sc->alc_intrhand[i] = NULL;
1107 if (sc->alc_res[0] != NULL)
1109 bus_release_resources(dev, sc->alc_irq_spec, sc->alc_irq);
1110 if ((sc->alc_flags & (ALC_FLAG_MSI | ALC_FLAG_MSIX)) != 0)
1111 pci_release_msi(dev);
1112 bus_release_resources(dev, sc->alc_res_spec, sc->alc_res);
1113 mtx_destroy(&sc->alc_mtx);
1118 #define ALC_SYSCTL_STAT_ADD32(c, h, n, p, d) \
1119 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
1120 #define ALC_SYSCTL_STAT_ADD64(c, h, n, p, d) \
1121 SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
1124 alc_sysctl_node(struct alc_softc *sc)
1126 struct sysctl_ctx_list *ctx;
1127 struct sysctl_oid_list *child, *parent;
1128 struct sysctl_oid *tree;
1129 struct alc_hw_stats *stats;
1132 stats = &sc->alc_stats;
1133 ctx = device_get_sysctl_ctx(sc->alc_dev);
1134 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->alc_dev));
1136 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_rx_mod",
1137 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_rx_mod, 0,
1138 sysctl_hw_alc_int_mod, "I", "alc Rx interrupt moderation");
1139 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_tx_mod",
1140 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_int_tx_mod, 0,
1141 sysctl_hw_alc_int_mod, "I", "alc Tx interrupt moderation");
1142 /* Pull in device tunables. */
1143 sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1144 error = resource_int_value(device_get_name(sc->alc_dev),
1145 device_get_unit(sc->alc_dev), "int_rx_mod", &sc->alc_int_rx_mod);
1147 if (sc->alc_int_rx_mod < ALC_IM_TIMER_MIN ||
1148 sc->alc_int_rx_mod > ALC_IM_TIMER_MAX) {
1149 device_printf(sc->alc_dev, "int_rx_mod value out of "
1150 "range; using default: %d\n",
1151 ALC_IM_RX_TIMER_DEFAULT);
1152 sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
1155 sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1156 error = resource_int_value(device_get_name(sc->alc_dev),
1157 device_get_unit(sc->alc_dev), "int_tx_mod", &sc->alc_int_tx_mod);
1159 if (sc->alc_int_tx_mod < ALC_IM_TIMER_MIN ||
1160 sc->alc_int_tx_mod > ALC_IM_TIMER_MAX) {
1161 device_printf(sc->alc_dev, "int_tx_mod value out of "
1162 "range; using default: %d\n",
1163 ALC_IM_TX_TIMER_DEFAULT);
1164 sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
1167 SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
1168 CTLTYPE_INT | CTLFLAG_RW, &sc->alc_process_limit, 0,
1169 sysctl_hw_alc_proc_limit, "I",
1170 "max number of Rx events to process");
1171 /* Pull in device tunables. */
1172 sc->alc_process_limit = ALC_PROC_DEFAULT;
1173 error = resource_int_value(device_get_name(sc->alc_dev),
1174 device_get_unit(sc->alc_dev), "process_limit",
1175 &sc->alc_process_limit);
1177 if (sc->alc_process_limit < ALC_PROC_MIN ||
1178 sc->alc_process_limit > ALC_PROC_MAX) {
1179 device_printf(sc->alc_dev,
1180 "process_limit value out of range; "
1181 "using default: %d\n", ALC_PROC_DEFAULT);
1182 sc->alc_process_limit = ALC_PROC_DEFAULT;
1186 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
1187 NULL, "ALC statistics");
1188 parent = SYSCTL_CHILDREN(tree);
1190 /* Rx statistics. */
1191 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
1192 NULL, "Rx MAC statistics");
1193 child = SYSCTL_CHILDREN(tree);
1194 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1195 &stats->rx_frames, "Good frames");
1196 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1197 &stats->rx_bcast_frames, "Good broadcast frames");
1198 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1199 &stats->rx_mcast_frames, "Good multicast frames");
1200 ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1201 &stats->rx_pause_frames, "Pause control frames");
1202 ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1203 &stats->rx_control_frames, "Control frames");
1204 ALC_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
1205 &stats->rx_crcerrs, "CRC errors");
1206 ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1207 &stats->rx_lenerrs, "Frames with length mismatched");
1208 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1209 &stats->rx_bytes, "Good octets");
1210 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1211 &stats->rx_bcast_bytes, "Good broadcast octets");
1212 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1213 &stats->rx_mcast_bytes, "Good multicast octets");
1214 ALC_SYSCTL_STAT_ADD32(ctx, child, "runts",
1215 &stats->rx_runts, "Too short frames");
1216 ALC_SYSCTL_STAT_ADD32(ctx, child, "fragments",
1217 &stats->rx_fragments, "Fragmented frames");
1218 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1219 &stats->rx_pkts_64, "64 bytes frames");
1220 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1221 &stats->rx_pkts_65_127, "65 to 127 bytes frames");
1222 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1223 &stats->rx_pkts_128_255, "128 to 255 bytes frames");
1224 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1225 &stats->rx_pkts_256_511, "256 to 511 bytes frames");
1226 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1227 &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
1228 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1229 &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
1230 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1231 &stats->rx_pkts_1519_max, "1519 to max frames");
1232 ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1233 &stats->rx_pkts_truncated, "Truncated frames due to MTU size");
1234 ALC_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
1235 &stats->rx_fifo_oflows, "FIFO overflows");
1236 ALC_SYSCTL_STAT_ADD32(ctx, child, "rrs_errs",
1237 &stats->rx_rrs_errs, "Return status write-back errors");
1238 ALC_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
1239 &stats->rx_alignerrs, "Alignment errors");
1240 ALC_SYSCTL_STAT_ADD32(ctx, child, "filtered",
1241 &stats->rx_pkts_filtered,
1242 "Frames dropped due to address filtering");
1244 /* Tx statistics. */
1245 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
1246 NULL, "Tx MAC statistics");
1247 child = SYSCTL_CHILDREN(tree);
1248 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
1249 &stats->tx_frames, "Good frames");
1250 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
1251 &stats->tx_bcast_frames, "Good broadcast frames");
1252 ALC_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
1253 &stats->tx_mcast_frames, "Good multicast frames");
1254 ALC_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
1255 &stats->tx_pause_frames, "Pause control frames");
1256 ALC_SYSCTL_STAT_ADD32(ctx, child, "control_frames",
1257 &stats->tx_control_frames, "Control frames");
1258 ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
1259 &stats->tx_excess_defer, "Frames with excessive derferrals");
1260 ALC_SYSCTL_STAT_ADD32(ctx, child, "defers",
1261 &stats->tx_excess_defer, "Frames with derferrals");
1262 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
1263 &stats->tx_bytes, "Good octets");
1264 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_bcast_octets",
1265 &stats->tx_bcast_bytes, "Good broadcast octets");
1266 ALC_SYSCTL_STAT_ADD64(ctx, child, "good_mcast_octets",
1267 &stats->tx_mcast_bytes, "Good multicast octets");
1268 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
1269 &stats->tx_pkts_64, "64 bytes frames");
1270 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
1271 &stats->tx_pkts_65_127, "65 to 127 bytes frames");
1272 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
1273 &stats->tx_pkts_128_255, "128 to 255 bytes frames");
1274 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
1275 &stats->tx_pkts_256_511, "256 to 511 bytes frames");
1276 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
1277 &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
1278 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
1279 &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
1280 ALC_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
1281 &stats->tx_pkts_1519_max, "1519 to max frames");
1282 ALC_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
1283 &stats->tx_single_colls, "Single collisions");
1284 ALC_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
1285 &stats->tx_multi_colls, "Multiple collisions");
1286 ALC_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
1287 &stats->tx_late_colls, "Late collisions");
1288 ALC_SYSCTL_STAT_ADD32(ctx, child, "excess_colls",
1289 &stats->tx_excess_colls, "Excessive collisions");
1290 ALC_SYSCTL_STAT_ADD32(ctx, child, "abort",
1291 &stats->tx_abort, "Aborted frames due to Excessive collisions");
1292 ALC_SYSCTL_STAT_ADD32(ctx, child, "underruns",
1293 &stats->tx_underrun, "FIFO underruns");
1294 ALC_SYSCTL_STAT_ADD32(ctx, child, "desc_underruns",
1295 &stats->tx_desc_underrun, "Descriptor write-back errors");
1296 ALC_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
1297 &stats->tx_lenerrs, "Frames with length mismatched");
1298 ALC_SYSCTL_STAT_ADD32(ctx, child, "trunc_errs",
1299 &stats->tx_pkts_truncated, "Truncated frames due to MTU size");
1302 #undef ALC_SYSCTL_STAT_ADD32
1303 #undef ALC_SYSCTL_STAT_ADD64
1305 struct alc_dmamap_arg {
1306 bus_addr_t alc_busaddr;
1310 alc_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1312 struct alc_dmamap_arg *ctx;
1317 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1319 ctx = (struct alc_dmamap_arg *)arg;
1320 ctx->alc_busaddr = segs[0].ds_addr;
1324 * Normal and high Tx descriptors shares single Tx high address.
1325 * Four Rx descriptor/return rings and CMB shares the same Rx
1329 alc_check_boundary(struct alc_softc *sc)
1331 bus_addr_t cmb_end, rx_ring_end, rr_ring_end, tx_ring_end;
1333 rx_ring_end = sc->alc_rdata.alc_rx_ring_paddr + ALC_RX_RING_SZ;
1334 rr_ring_end = sc->alc_rdata.alc_rr_ring_paddr + ALC_RR_RING_SZ;
1335 cmb_end = sc->alc_rdata.alc_cmb_paddr + ALC_CMB_SZ;
1336 tx_ring_end = sc->alc_rdata.alc_tx_ring_paddr + ALC_TX_RING_SZ;
1338 /* 4GB boundary crossing is not allowed. */
1339 if ((ALC_ADDR_HI(rx_ring_end) !=
1340 ALC_ADDR_HI(sc->alc_rdata.alc_rx_ring_paddr)) ||
1341 (ALC_ADDR_HI(rr_ring_end) !=
1342 ALC_ADDR_HI(sc->alc_rdata.alc_rr_ring_paddr)) ||
1343 (ALC_ADDR_HI(cmb_end) !=
1344 ALC_ADDR_HI(sc->alc_rdata.alc_cmb_paddr)) ||
1345 (ALC_ADDR_HI(tx_ring_end) !=
1346 ALC_ADDR_HI(sc->alc_rdata.alc_tx_ring_paddr)))
1349 * Make sure Rx return descriptor/Rx descriptor/CMB use
1350 * the same high address.
1352 if ((ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(rr_ring_end)) ||
1353 (ALC_ADDR_HI(rx_ring_end) != ALC_ADDR_HI(cmb_end)))
1360 alc_dma_alloc(struct alc_softc *sc)
1362 struct alc_txdesc *txd;
1363 struct alc_rxdesc *rxd;
1365 struct alc_dmamap_arg ctx;
1368 lowaddr = BUS_SPACE_MAXADDR;
1370 /* Create parent DMA tag. */
1371 error = bus_dma_tag_create(
1372 bus_get_dma_tag(sc->alc_dev), /* parent */
1373 1, 0, /* alignment, boundary */
1374 lowaddr, /* lowaddr */
1375 BUS_SPACE_MAXADDR, /* highaddr */
1376 NULL, NULL, /* filter, filterarg */
1377 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1379 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1381 NULL, NULL, /* lockfunc, lockarg */
1382 &sc->alc_cdata.alc_parent_tag);
1384 device_printf(sc->alc_dev,
1385 "could not create parent DMA tag.\n");
1389 /* Create DMA tag for Tx descriptor ring. */
1390 error = bus_dma_tag_create(
1391 sc->alc_cdata.alc_parent_tag, /* parent */
1392 ALC_TX_RING_ALIGN, 0, /* alignment, boundary */
1393 BUS_SPACE_MAXADDR, /* lowaddr */
1394 BUS_SPACE_MAXADDR, /* highaddr */
1395 NULL, NULL, /* filter, filterarg */
1396 ALC_TX_RING_SZ, /* maxsize */
1398 ALC_TX_RING_SZ, /* maxsegsize */
1400 NULL, NULL, /* lockfunc, lockarg */
1401 &sc->alc_cdata.alc_tx_ring_tag);
1403 device_printf(sc->alc_dev,
1404 "could not create Tx ring DMA tag.\n");
1408 /* Create DMA tag for Rx free descriptor ring. */
1409 error = bus_dma_tag_create(
1410 sc->alc_cdata.alc_parent_tag, /* parent */
1411 ALC_RX_RING_ALIGN, 0, /* alignment, boundary */
1412 BUS_SPACE_MAXADDR, /* lowaddr */
1413 BUS_SPACE_MAXADDR, /* highaddr */
1414 NULL, NULL, /* filter, filterarg */
1415 ALC_RX_RING_SZ, /* maxsize */
1417 ALC_RX_RING_SZ, /* maxsegsize */
1419 NULL, NULL, /* lockfunc, lockarg */
1420 &sc->alc_cdata.alc_rx_ring_tag);
1422 device_printf(sc->alc_dev,
1423 "could not create Rx ring DMA tag.\n");
1426 /* Create DMA tag for Rx return descriptor ring. */
1427 error = bus_dma_tag_create(
1428 sc->alc_cdata.alc_parent_tag, /* parent */
1429 ALC_RR_RING_ALIGN, 0, /* alignment, boundary */
1430 BUS_SPACE_MAXADDR, /* lowaddr */
1431 BUS_SPACE_MAXADDR, /* highaddr */
1432 NULL, NULL, /* filter, filterarg */
1433 ALC_RR_RING_SZ, /* maxsize */
1435 ALC_RR_RING_SZ, /* maxsegsize */
1437 NULL, NULL, /* lockfunc, lockarg */
1438 &sc->alc_cdata.alc_rr_ring_tag);
1440 device_printf(sc->alc_dev,
1441 "could not create Rx return ring DMA tag.\n");
1445 /* Create DMA tag for coalescing message block. */
1446 error = bus_dma_tag_create(
1447 sc->alc_cdata.alc_parent_tag, /* parent */
1448 ALC_CMB_ALIGN, 0, /* alignment, boundary */
1449 BUS_SPACE_MAXADDR, /* lowaddr */
1450 BUS_SPACE_MAXADDR, /* highaddr */
1451 NULL, NULL, /* filter, filterarg */
1452 ALC_CMB_SZ, /* maxsize */
1454 ALC_CMB_SZ, /* maxsegsize */
1456 NULL, NULL, /* lockfunc, lockarg */
1457 &sc->alc_cdata.alc_cmb_tag);
1459 device_printf(sc->alc_dev,
1460 "could not create CMB DMA tag.\n");
1463 /* Create DMA tag for status message block. */
1464 error = bus_dma_tag_create(
1465 sc->alc_cdata.alc_parent_tag, /* parent */
1466 ALC_SMB_ALIGN, 0, /* alignment, boundary */
1467 BUS_SPACE_MAXADDR, /* lowaddr */
1468 BUS_SPACE_MAXADDR, /* highaddr */
1469 NULL, NULL, /* filter, filterarg */
1470 ALC_SMB_SZ, /* maxsize */
1472 ALC_SMB_SZ, /* maxsegsize */
1474 NULL, NULL, /* lockfunc, lockarg */
1475 &sc->alc_cdata.alc_smb_tag);
1477 device_printf(sc->alc_dev,
1478 "could not create SMB DMA tag.\n");
1482 /* Allocate DMA'able memory and load the DMA map for Tx ring. */
1483 error = bus_dmamem_alloc(sc->alc_cdata.alc_tx_ring_tag,
1484 (void **)&sc->alc_rdata.alc_tx_ring,
1485 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1486 &sc->alc_cdata.alc_tx_ring_map);
1488 device_printf(sc->alc_dev,
1489 "could not allocate DMA'able memory for Tx ring.\n");
1492 ctx.alc_busaddr = 0;
1493 error = bus_dmamap_load(sc->alc_cdata.alc_tx_ring_tag,
1494 sc->alc_cdata.alc_tx_ring_map, sc->alc_rdata.alc_tx_ring,
1495 ALC_TX_RING_SZ, alc_dmamap_cb, &ctx, 0);
1496 if (error != 0 || ctx.alc_busaddr == 0) {
1497 device_printf(sc->alc_dev,
1498 "could not load DMA'able memory for Tx ring.\n");
1501 sc->alc_rdata.alc_tx_ring_paddr = ctx.alc_busaddr;
1503 /* Allocate DMA'able memory and load the DMA map for Rx ring. */
1504 error = bus_dmamem_alloc(sc->alc_cdata.alc_rx_ring_tag,
1505 (void **)&sc->alc_rdata.alc_rx_ring,
1506 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1507 &sc->alc_cdata.alc_rx_ring_map);
1509 device_printf(sc->alc_dev,
1510 "could not allocate DMA'able memory for Rx ring.\n");
1513 ctx.alc_busaddr = 0;
1514 error = bus_dmamap_load(sc->alc_cdata.alc_rx_ring_tag,
1515 sc->alc_cdata.alc_rx_ring_map, sc->alc_rdata.alc_rx_ring,
1516 ALC_RX_RING_SZ, alc_dmamap_cb, &ctx, 0);
1517 if (error != 0 || ctx.alc_busaddr == 0) {
1518 device_printf(sc->alc_dev,
1519 "could not load DMA'able memory for Rx ring.\n");
1522 sc->alc_rdata.alc_rx_ring_paddr = ctx.alc_busaddr;
1524 /* Allocate DMA'able memory and load the DMA map for Rx return ring. */
1525 error = bus_dmamem_alloc(sc->alc_cdata.alc_rr_ring_tag,
1526 (void **)&sc->alc_rdata.alc_rr_ring,
1527 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1528 &sc->alc_cdata.alc_rr_ring_map);
1530 device_printf(sc->alc_dev,
1531 "could not allocate DMA'able memory for Rx return ring.\n");
1534 ctx.alc_busaddr = 0;
1535 error = bus_dmamap_load(sc->alc_cdata.alc_rr_ring_tag,
1536 sc->alc_cdata.alc_rr_ring_map, sc->alc_rdata.alc_rr_ring,
1537 ALC_RR_RING_SZ, alc_dmamap_cb, &ctx, 0);
1538 if (error != 0 || ctx.alc_busaddr == 0) {
1539 device_printf(sc->alc_dev,
1540 "could not load DMA'able memory for Tx ring.\n");
1543 sc->alc_rdata.alc_rr_ring_paddr = ctx.alc_busaddr;
1545 /* Allocate DMA'able memory and load the DMA map for CMB. */
1546 error = bus_dmamem_alloc(sc->alc_cdata.alc_cmb_tag,
1547 (void **)&sc->alc_rdata.alc_cmb,
1548 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1549 &sc->alc_cdata.alc_cmb_map);
1551 device_printf(sc->alc_dev,
1552 "could not allocate DMA'able memory for CMB.\n");
1555 ctx.alc_busaddr = 0;
1556 error = bus_dmamap_load(sc->alc_cdata.alc_cmb_tag,
1557 sc->alc_cdata.alc_cmb_map, sc->alc_rdata.alc_cmb,
1558 ALC_CMB_SZ, alc_dmamap_cb, &ctx, 0);
1559 if (error != 0 || ctx.alc_busaddr == 0) {
1560 device_printf(sc->alc_dev,
1561 "could not load DMA'able memory for CMB.\n");
1564 sc->alc_rdata.alc_cmb_paddr = ctx.alc_busaddr;
1566 /* Allocate DMA'able memory and load the DMA map for SMB. */
1567 error = bus_dmamem_alloc(sc->alc_cdata.alc_smb_tag,
1568 (void **)&sc->alc_rdata.alc_smb,
1569 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1570 &sc->alc_cdata.alc_smb_map);
1572 device_printf(sc->alc_dev,
1573 "could not allocate DMA'able memory for SMB.\n");
1576 ctx.alc_busaddr = 0;
1577 error = bus_dmamap_load(sc->alc_cdata.alc_smb_tag,
1578 sc->alc_cdata.alc_smb_map, sc->alc_rdata.alc_smb,
1579 ALC_SMB_SZ, alc_dmamap_cb, &ctx, 0);
1580 if (error != 0 || ctx.alc_busaddr == 0) {
1581 device_printf(sc->alc_dev,
1582 "could not load DMA'able memory for CMB.\n");
1585 sc->alc_rdata.alc_smb_paddr = ctx.alc_busaddr;
1587 /* Make sure we've not crossed 4GB boundary. */
1588 if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
1589 (error = alc_check_boundary(sc)) != 0) {
1590 device_printf(sc->alc_dev, "4GB boundary crossed, "
1591 "switching to 32bit DMA addressing mode.\n");
1594 * Limit max allowable DMA address space to 32bit
1597 lowaddr = BUS_SPACE_MAXADDR_32BIT;
1602 * Create Tx buffer parent tag.
1603 * AR813x/AR815x allows 64bit DMA addressing of Tx/Rx buffers
1604 * so it needs separate parent DMA tag as parent DMA address
1605 * space could be restricted to be within 32bit address space
1606 * by 4GB boundary crossing.
1608 error = bus_dma_tag_create(
1609 bus_get_dma_tag(sc->alc_dev), /* parent */
1610 1, 0, /* alignment, boundary */
1611 BUS_SPACE_MAXADDR, /* lowaddr */
1612 BUS_SPACE_MAXADDR, /* highaddr */
1613 NULL, NULL, /* filter, filterarg */
1614 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1616 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1618 NULL, NULL, /* lockfunc, lockarg */
1619 &sc->alc_cdata.alc_buffer_tag);
1621 device_printf(sc->alc_dev,
1622 "could not create parent buffer DMA tag.\n");
1626 /* Create DMA tag for Tx buffers. */
1627 error = bus_dma_tag_create(
1628 sc->alc_cdata.alc_buffer_tag, /* parent */
1629 1, 0, /* alignment, boundary */
1630 BUS_SPACE_MAXADDR, /* lowaddr */
1631 BUS_SPACE_MAXADDR, /* highaddr */
1632 NULL, NULL, /* filter, filterarg */
1633 ALC_TSO_MAXSIZE, /* maxsize */
1634 ALC_MAXTXSEGS, /* nsegments */
1635 ALC_TSO_MAXSEGSIZE, /* maxsegsize */
1637 NULL, NULL, /* lockfunc, lockarg */
1638 &sc->alc_cdata.alc_tx_tag);
1640 device_printf(sc->alc_dev, "could not create Tx DMA tag.\n");
1644 /* Create DMA tag for Rx buffers. */
1645 error = bus_dma_tag_create(
1646 sc->alc_cdata.alc_buffer_tag, /* parent */
1647 ALC_RX_BUF_ALIGN, 0, /* alignment, boundary */
1648 BUS_SPACE_MAXADDR, /* lowaddr */
1649 BUS_SPACE_MAXADDR, /* highaddr */
1650 NULL, NULL, /* filter, filterarg */
1651 MCLBYTES, /* maxsize */
1653 MCLBYTES, /* maxsegsize */
1655 NULL, NULL, /* lockfunc, lockarg */
1656 &sc->alc_cdata.alc_rx_tag);
1658 device_printf(sc->alc_dev, "could not create Rx DMA tag.\n");
1661 /* Create DMA maps for Tx buffers. */
1662 for (i = 0; i < ALC_TX_RING_CNT; i++) {
1663 txd = &sc->alc_cdata.alc_txdesc[i];
1665 txd->tx_dmamap = NULL;
1666 error = bus_dmamap_create(sc->alc_cdata.alc_tx_tag, 0,
1669 device_printf(sc->alc_dev,
1670 "could not create Tx dmamap.\n");
1674 /* Create DMA maps for Rx buffers. */
1675 if ((error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
1676 &sc->alc_cdata.alc_rx_sparemap)) != 0) {
1677 device_printf(sc->alc_dev,
1678 "could not create spare Rx dmamap.\n");
1681 for (i = 0; i < ALC_RX_RING_CNT; i++) {
1682 rxd = &sc->alc_cdata.alc_rxdesc[i];
1684 rxd->rx_dmamap = NULL;
1685 error = bus_dmamap_create(sc->alc_cdata.alc_rx_tag, 0,
1688 device_printf(sc->alc_dev,
1689 "could not create Rx dmamap.\n");
1699 alc_dma_free(struct alc_softc *sc)
1701 struct alc_txdesc *txd;
1702 struct alc_rxdesc *rxd;
1706 if (sc->alc_cdata.alc_tx_tag != NULL) {
1707 for (i = 0; i < ALC_TX_RING_CNT; i++) {
1708 txd = &sc->alc_cdata.alc_txdesc[i];
1709 if (txd->tx_dmamap != NULL) {
1710 bus_dmamap_destroy(sc->alc_cdata.alc_tx_tag,
1712 txd->tx_dmamap = NULL;
1715 bus_dma_tag_destroy(sc->alc_cdata.alc_tx_tag);
1716 sc->alc_cdata.alc_tx_tag = NULL;
1719 if (sc->alc_cdata.alc_rx_tag != NULL) {
1720 for (i = 0; i < ALC_RX_RING_CNT; i++) {
1721 rxd = &sc->alc_cdata.alc_rxdesc[i];
1722 if (rxd->rx_dmamap != NULL) {
1723 bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
1725 rxd->rx_dmamap = NULL;
1728 if (sc->alc_cdata.alc_rx_sparemap != NULL) {
1729 bus_dmamap_destroy(sc->alc_cdata.alc_rx_tag,
1730 sc->alc_cdata.alc_rx_sparemap);
1731 sc->alc_cdata.alc_rx_sparemap = NULL;
1733 bus_dma_tag_destroy(sc->alc_cdata.alc_rx_tag);
1734 sc->alc_cdata.alc_rx_tag = NULL;
1736 /* Tx descriptor ring. */
1737 if (sc->alc_cdata.alc_tx_ring_tag != NULL) {
1738 if (sc->alc_rdata.alc_tx_ring_paddr != 0)
1739 bus_dmamap_unload(sc->alc_cdata.alc_tx_ring_tag,
1740 sc->alc_cdata.alc_tx_ring_map);
1741 if (sc->alc_rdata.alc_tx_ring != NULL)
1742 bus_dmamem_free(sc->alc_cdata.alc_tx_ring_tag,
1743 sc->alc_rdata.alc_tx_ring,
1744 sc->alc_cdata.alc_tx_ring_map);
1745 sc->alc_rdata.alc_tx_ring_paddr = 0;
1746 sc->alc_rdata.alc_tx_ring = NULL;
1747 bus_dma_tag_destroy(sc->alc_cdata.alc_tx_ring_tag);
1748 sc->alc_cdata.alc_tx_ring_tag = NULL;
1751 if (sc->alc_cdata.alc_rx_ring_tag != NULL) {
1752 if (sc->alc_rdata.alc_rx_ring_paddr != 0)
1753 bus_dmamap_unload(sc->alc_cdata.alc_rx_ring_tag,
1754 sc->alc_cdata.alc_rx_ring_map);
1755 if (sc->alc_rdata.alc_rx_ring != NULL)
1756 bus_dmamem_free(sc->alc_cdata.alc_rx_ring_tag,
1757 sc->alc_rdata.alc_rx_ring,
1758 sc->alc_cdata.alc_rx_ring_map);
1759 sc->alc_rdata.alc_rx_ring_paddr = 0;
1760 sc->alc_rdata.alc_rx_ring = NULL;
1761 bus_dma_tag_destroy(sc->alc_cdata.alc_rx_ring_tag);
1762 sc->alc_cdata.alc_rx_ring_tag = NULL;
1764 /* Rx return ring. */
1765 if (sc->alc_cdata.alc_rr_ring_tag != NULL) {
1766 if (sc->alc_rdata.alc_rr_ring_paddr != 0)
1767 bus_dmamap_unload(sc->alc_cdata.alc_rr_ring_tag,
1768 sc->alc_cdata.alc_rr_ring_map);
1769 if (sc->alc_rdata.alc_rr_ring != NULL)
1770 bus_dmamem_free(sc->alc_cdata.alc_rr_ring_tag,
1771 sc->alc_rdata.alc_rr_ring,
1772 sc->alc_cdata.alc_rr_ring_map);
1773 sc->alc_rdata.alc_rr_ring_paddr = 0;
1774 sc->alc_rdata.alc_rr_ring = NULL;
1775 bus_dma_tag_destroy(sc->alc_cdata.alc_rr_ring_tag);
1776 sc->alc_cdata.alc_rr_ring_tag = NULL;
1779 if (sc->alc_cdata.alc_cmb_tag != NULL) {
1780 if (sc->alc_rdata.alc_cmb_paddr != 0)
1781 bus_dmamap_unload(sc->alc_cdata.alc_cmb_tag,
1782 sc->alc_cdata.alc_cmb_map);
1783 if (sc->alc_rdata.alc_cmb != NULL)
1784 bus_dmamem_free(sc->alc_cdata.alc_cmb_tag,
1785 sc->alc_rdata.alc_cmb,
1786 sc->alc_cdata.alc_cmb_map);
1787 sc->alc_rdata.alc_cmb_paddr = 0;
1788 sc->alc_rdata.alc_cmb = NULL;
1789 bus_dma_tag_destroy(sc->alc_cdata.alc_cmb_tag);
1790 sc->alc_cdata.alc_cmb_tag = NULL;
1793 if (sc->alc_cdata.alc_smb_tag != NULL) {
1794 if (sc->alc_rdata.alc_smb_paddr != 0)
1795 bus_dmamap_unload(sc->alc_cdata.alc_smb_tag,
1796 sc->alc_cdata.alc_smb_map);
1797 if (sc->alc_rdata.alc_smb != NULL)
1798 bus_dmamem_free(sc->alc_cdata.alc_smb_tag,
1799 sc->alc_rdata.alc_smb,
1800 sc->alc_cdata.alc_smb_map);
1801 sc->alc_rdata.alc_smb_paddr = 0;
1802 sc->alc_rdata.alc_smb = NULL;
1803 bus_dma_tag_destroy(sc->alc_cdata.alc_smb_tag);
1804 sc->alc_cdata.alc_smb_tag = NULL;
1806 if (sc->alc_cdata.alc_buffer_tag != NULL) {
1807 bus_dma_tag_destroy(sc->alc_cdata.alc_buffer_tag);
1808 sc->alc_cdata.alc_buffer_tag = NULL;
1810 if (sc->alc_cdata.alc_parent_tag != NULL) {
1811 bus_dma_tag_destroy(sc->alc_cdata.alc_parent_tag);
1812 sc->alc_cdata.alc_parent_tag = NULL;
1817 alc_shutdown(device_t dev)
1820 return (alc_suspend(dev));
1824 * Note, this driver resets the link speed to 10/100Mbps by
1825 * restarting auto-negotiation in suspend/shutdown phase but we
1826 * don't know whether that auto-negotiation would succeed or not
1827 * as driver has no control after powering off/suspend operation.
1828 * If the renegotiation fail WOL may not work. Running at 1Gbps
1829 * will draw more power than 375mA at 3.3V which is specified in
1830 * PCI specification and that would result in complete
1831 * shutdowning power to ethernet controller.
1834 * Save current negotiated media speed/duplex/flow-control to
1835 * softc and restore the same link again after resuming. PHY
1836 * handling such as power down/resetting to 100Mbps may be better
1837 * handled in suspend method in phy driver.
1840 alc_setlinkspeed(struct alc_softc *sc)
1842 struct mii_data *mii;
1845 mii = device_get_softc(sc->alc_miibus);
1848 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1849 (IFM_ACTIVE | IFM_AVALID)) {
1850 switch IFM_SUBTYPE(mii->mii_media_active) {
1861 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr, MII_100T2CR, 0);
1862 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
1863 MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
1864 alc_miibus_writereg(sc->alc_dev, sc->alc_phyaddr,
1865 MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
1869 * Poll link state until alc(4) get a 10/100Mbps link.
1871 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1873 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
1874 == (IFM_ACTIVE | IFM_AVALID)) {
1875 switch (IFM_SUBTYPE(
1876 mii->mii_media_active)) {
1886 pause("alclnk", hz);
1889 if (i == MII_ANEGTICKS_GIGE)
1890 device_printf(sc->alc_dev,
1891 "establishing a link failed, WOL may not work!");
1894 * No link, force MAC to have 100Mbps, full-duplex link.
1895 * This is the last resort and may/may not work.
1897 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1898 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1903 alc_setwol(struct alc_softc *sc)
1909 ALC_LOCK_ASSERT(sc);
1911 alc_disable_l0s_l1(sc);
1913 if ((sc->alc_flags & ALC_FLAG_PM) == 0) {
1915 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
1916 reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
1917 reg |= PCIE_PHYMISC_FORCE_RCV_DET;
1918 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
1919 /* Force PHY power down. */
1921 CSR_WRITE_4(sc, ALC_MASTER_CFG,
1922 CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
1926 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
1927 if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
1928 alc_setlinkspeed(sc);
1929 CSR_WRITE_4(sc, ALC_MASTER_CFG,
1930 CSR_READ_4(sc, ALC_MASTER_CFG) & ~MASTER_CLK_SEL_DIS);
1934 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
1935 pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
1936 CSR_WRITE_4(sc, ALC_WOL_CFG, pmcs);
1937 reg = CSR_READ_4(sc, ALC_MAC_CFG);
1938 reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC | MAC_CFG_ALLMULTI |
1940 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
1941 reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
1942 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1943 reg |= MAC_CFG_RX_ENB;
1944 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
1946 reg = CSR_READ_4(sc, ALC_PCIE_PHYMISC);
1947 reg |= PCIE_PHYMISC_FORCE_RCV_DET;
1948 CSR_WRITE_4(sc, ALC_PCIE_PHYMISC, reg);
1949 if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1950 /* WOL disabled, PHY power down. */
1952 CSR_WRITE_4(sc, ALC_MASTER_CFG,
1953 CSR_READ_4(sc, ALC_MASTER_CFG) | MASTER_CLK_SEL_DIS);
1956 pmstat = pci_read_config(sc->alc_dev,
1957 sc->alc_pmcap + PCIR_POWER_STATUS, 2);
1958 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1959 if ((ifp->if_capenable & IFCAP_WOL) != 0)
1960 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1961 pci_write_config(sc->alc_dev,
1962 sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
1966 alc_suspend(device_t dev)
1968 struct alc_softc *sc;
1970 sc = device_get_softc(dev);
1981 alc_resume(device_t dev)
1983 struct alc_softc *sc;
1987 sc = device_get_softc(dev);
1990 if ((sc->alc_flags & ALC_FLAG_PM) != 0) {
1991 /* Disable PME and clear PME status. */
1992 pmstat = pci_read_config(sc->alc_dev,
1993 sc->alc_pmcap + PCIR_POWER_STATUS, 2);
1994 if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
1995 pmstat &= ~PCIM_PSTAT_PMEENABLE;
1996 pci_write_config(sc->alc_dev,
1997 sc->alc_pmcap + PCIR_POWER_STATUS, pmstat, 2);
2003 if ((ifp->if_flags & IFF_UP) != 0) {
2004 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2005 alc_init_locked(sc);
2013 alc_encap(struct alc_softc *sc, struct mbuf **m_head)
2015 struct alc_txdesc *txd, *txd_last;
2016 struct tx_desc *desc;
2020 bus_dma_segment_t txsegs[ALC_MAXTXSEGS];
2022 uint32_t cflags, hdrlen, ip_off, poff, vtag;
2023 int error, idx, nsegs, prod;
2025 ALC_LOCK_ASSERT(sc);
2027 M_ASSERTPKTHDR((*m_head));
2033 if ((m->m_pkthdr.csum_flags & (ALC_CSUM_FEATURES | CSUM_TSO)) != 0) {
2035 * AR813x/AR815x requires offset of TCP/UDP header in its
2036 * Tx descriptor to perform Tx checksum offloading. TSO
2037 * also requires TCP header offset and modification of
2038 * IP/TCP header. This kind of operation takes many CPU
2039 * cycles on FreeBSD so fast host CPU is required to get
2040 * smooth TSO performance.
2042 struct ether_header *eh;
2044 if (M_WRITABLE(m) == 0) {
2045 /* Get a writable copy. */
2046 m = m_dup(*m_head, M_NOWAIT);
2047 /* Release original mbufs. */
2056 ip_off = sizeof(struct ether_header);
2057 m = m_pullup(m, ip_off);
2062 eh = mtod(m, struct ether_header *);
2064 * Check if hardware VLAN insertion is off.
2065 * Additional check for LLC/SNAP frame?
2067 if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
2068 ip_off = sizeof(struct ether_vlan_header);
2069 m = m_pullup(m, ip_off);
2075 m = m_pullup(m, ip_off + sizeof(struct ip));
2080 ip = (struct ip *)(mtod(m, char *) + ip_off);
2081 poff = ip_off + (ip->ip_hl << 2);
2082 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2083 m = m_pullup(m, poff + sizeof(struct tcphdr));
2088 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2089 m = m_pullup(m, poff + (tcp->th_off << 2));
2095 * Due to strict adherence of Microsoft NDIS
2096 * Large Send specification, hardware expects
2097 * a pseudo TCP checksum inserted by upper
2098 * stack. Unfortunately the pseudo TCP
2099 * checksum that NDIS refers to does not include
2100 * TCP payload length so driver should recompute
2101 * the pseudo checksum here. Hopefully this
2102 * wouldn't be much burden on modern CPUs.
2104 * Reset IP checksum and recompute TCP pseudo
2105 * checksum as NDIS specification said.
2107 ip = (struct ip *)(mtod(m, char *) + ip_off);
2108 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
2110 tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
2111 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
2116 prod = sc->alc_cdata.alc_tx_prod;
2117 txd = &sc->alc_cdata.alc_txdesc[prod];
2119 map = txd->tx_dmamap;
2121 error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2122 *m_head, txsegs, &nsegs, 0);
2123 if (error == EFBIG) {
2124 m = m_collapse(*m_head, M_NOWAIT, ALC_MAXTXSEGS);
2131 error = bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_tx_tag, map,
2132 *m_head, txsegs, &nsegs, 0);
2138 } else if (error != 0)
2146 /* Check descriptor overrun. */
2147 if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
2148 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag, map);
2151 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag, map, BUS_DMASYNC_PREWRITE);
2154 cflags = TD_ETHERNET;
2158 /* Configure VLAN hardware tag insertion. */
2159 if ((m->m_flags & M_VLANTAG) != 0) {
2160 vtag = htons(m->m_pkthdr.ether_vtag);
2161 vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
2162 cflags |= TD_INS_VLAN_TAG;
2164 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2165 /* Request TSO and set MSS. */
2166 cflags |= TD_TSO | TD_TSO_DESCV1;
2167 cflags |= ((uint32_t)m->m_pkthdr.tso_segsz << TD_MSS_SHIFT) &
2169 /* Set TCP header offset. */
2170 cflags |= (poff << TD_TCPHDR_OFFSET_SHIFT) &
2171 TD_TCPHDR_OFFSET_MASK;
2173 * AR813x/AR815x requires the first buffer should
2174 * only hold IP/TCP header data. Payload should
2175 * be handled in other descriptors.
2177 hdrlen = poff + (tcp->th_off << 2);
2178 desc = &sc->alc_rdata.alc_tx_ring[prod];
2179 desc->len = htole32(TX_BYTES(hdrlen | vtag));
2180 desc->flags = htole32(cflags);
2181 desc->addr = htole64(txsegs[0].ds_addr);
2182 sc->alc_cdata.alc_tx_cnt++;
2183 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2184 if (m->m_len - hdrlen > 0) {
2185 /* Handle remaining payload of the first fragment. */
2186 desc = &sc->alc_rdata.alc_tx_ring[prod];
2187 desc->len = htole32(TX_BYTES((m->m_len - hdrlen) |
2189 desc->flags = htole32(cflags);
2190 desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
2191 sc->alc_cdata.alc_tx_cnt++;
2192 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2194 /* Handle remaining fragments. */
2196 } else if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
2197 /* Configure Tx checksum offload. */
2198 #ifdef ALC_USE_CUSTOM_CSUM
2199 cflags |= TD_CUSTOM_CSUM;
2200 /* Set checksum start offset. */
2201 cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
2202 TD_PLOAD_OFFSET_MASK;
2203 /* Set checksum insertion position of TCP/UDP. */
2204 cflags |= (((poff + m->m_pkthdr.csum_data) >> 1) <<
2205 TD_CUSTOM_CSUM_OFFSET_SHIFT) & TD_CUSTOM_CSUM_OFFSET_MASK;
2207 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
2208 cflags |= TD_IPCSUM;
2209 if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2210 cflags |= TD_TCPCSUM;
2211 if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2212 cflags |= TD_UDPCSUM;
2213 /* Set TCP/UDP header offset. */
2214 cflags |= (poff << TD_L4HDR_OFFSET_SHIFT) &
2215 TD_L4HDR_OFFSET_MASK;
2218 for (; idx < nsegs; idx++) {
2219 desc = &sc->alc_rdata.alc_tx_ring[prod];
2220 desc->len = htole32(TX_BYTES(txsegs[idx].ds_len) | vtag);
2221 desc->flags = htole32(cflags);
2222 desc->addr = htole64(txsegs[idx].ds_addr);
2223 sc->alc_cdata.alc_tx_cnt++;
2224 ALC_DESC_INC(prod, ALC_TX_RING_CNT);
2226 /* Update producer index. */
2227 sc->alc_cdata.alc_tx_prod = prod;
2229 /* Finally set EOP on the last descriptor. */
2230 prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
2231 desc = &sc->alc_rdata.alc_tx_ring[prod];
2232 desc->flags |= htole32(TD_EOP);
2234 /* Swap dmamap of the first and the last. */
2235 txd = &sc->alc_cdata.alc_txdesc[prod];
2236 map = txd_last->tx_dmamap;
2237 txd_last->tx_dmamap = txd->tx_dmamap;
2238 txd->tx_dmamap = map;
2245 alc_start(struct ifnet *ifp)
2247 struct alc_softc *sc;
2251 alc_start_locked(ifp);
2256 alc_start_locked(struct ifnet *ifp)
2258 struct alc_softc *sc;
2259 struct mbuf *m_head;
2264 ALC_LOCK_ASSERT(sc);
2266 /* Reclaim transmitted frames. */
2267 if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
2270 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2271 IFF_DRV_RUNNING || (sc->alc_flags & ALC_FLAG_LINK) == 0)
2274 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
2275 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2279 * Pack the data into the transmit ring. If we
2280 * don't have room, set the OACTIVE flag and wait
2281 * for the NIC to drain the ring.
2283 if (alc_encap(sc, &m_head)) {
2286 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2287 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2293 * If there's a BPF listener, bounce a copy of this frame
2296 ETHER_BPF_MTAP(ifp, m_head);
2300 /* Sync descriptors. */
2301 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2302 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
2303 /* Kick. Assume we're using normal Tx priority queue. */
2304 CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2305 (sc->alc_cdata.alc_tx_prod <<
2306 MBOX_TD_PROD_LO_IDX_SHIFT) &
2307 MBOX_TD_PROD_LO_IDX_MASK);
2308 /* Set a timeout in case the chip goes out to lunch. */
2309 sc->alc_watchdog_timer = ALC_TX_TIMEOUT;
2314 alc_watchdog(struct alc_softc *sc)
2318 ALC_LOCK_ASSERT(sc);
2320 if (sc->alc_watchdog_timer == 0 || --sc->alc_watchdog_timer)
2324 if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
2325 if_printf(sc->alc_ifp, "watchdog timeout (lost link)\n");
2326 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2327 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2328 alc_init_locked(sc);
2331 if_printf(sc->alc_ifp, "watchdog timeout -- resetting\n");
2332 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2333 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2334 alc_init_locked(sc);
2335 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2336 alc_start_locked(ifp);
2340 alc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2342 struct alc_softc *sc;
2344 struct mii_data *mii;
2348 ifr = (struct ifreq *)data;
2352 if (ifr->ifr_mtu < ETHERMIN ||
2353 ifr->ifr_mtu > (sc->alc_ident->max_framelen -
2354 sizeof(struct ether_vlan_header) - ETHER_CRC_LEN) ||
2355 ((sc->alc_flags & ALC_FLAG_JUMBO) == 0 &&
2356 ifr->ifr_mtu > ETHERMTU))
2358 else if (ifp->if_mtu != ifr->ifr_mtu) {
2360 ifp->if_mtu = ifr->ifr_mtu;
2361 /* AR813x/AR815x has 13 bits MSS field. */
2362 if (ifp->if_mtu > ALC_TSO_MTU &&
2363 (ifp->if_capenable & IFCAP_TSO4) != 0) {
2364 ifp->if_capenable &= ~IFCAP_TSO4;
2365 ifp->if_hwassist &= ~CSUM_TSO;
2366 VLAN_CAPABILITIES(ifp);
2373 if ((ifp->if_flags & IFF_UP) != 0) {
2374 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2375 ((ifp->if_flags ^ sc->alc_if_flags) &
2376 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2379 alc_init_locked(sc);
2380 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2382 sc->alc_if_flags = ifp->if_flags;
2388 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2394 mii = device_get_softc(sc->alc_miibus);
2395 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2399 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2400 if ((mask & IFCAP_TXCSUM) != 0 &&
2401 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
2402 ifp->if_capenable ^= IFCAP_TXCSUM;
2403 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
2404 ifp->if_hwassist |= ALC_CSUM_FEATURES;
2406 ifp->if_hwassist &= ~ALC_CSUM_FEATURES;
2408 if ((mask & IFCAP_TSO4) != 0 &&
2409 (ifp->if_capabilities & IFCAP_TSO4) != 0) {
2410 ifp->if_capenable ^= IFCAP_TSO4;
2411 if ((ifp->if_capenable & IFCAP_TSO4) != 0) {
2412 /* AR813x/AR815x has 13 bits MSS field. */
2413 if (ifp->if_mtu > ALC_TSO_MTU) {
2414 ifp->if_capenable &= ~IFCAP_TSO4;
2415 ifp->if_hwassist &= ~CSUM_TSO;
2417 ifp->if_hwassist |= CSUM_TSO;
2419 ifp->if_hwassist &= ~CSUM_TSO;
2421 if ((mask & IFCAP_WOL_MCAST) != 0 &&
2422 (ifp->if_capabilities & IFCAP_WOL_MCAST) != 0)
2423 ifp->if_capenable ^= IFCAP_WOL_MCAST;
2424 if ((mask & IFCAP_WOL_MAGIC) != 0 &&
2425 (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
2426 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2427 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2428 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2429 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2432 if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2433 (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2434 ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2435 if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
2436 (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
2437 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
2438 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2439 ifp->if_capenable &=
2440 ~(IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM);
2442 VLAN_CAPABILITIES(ifp);
2445 error = ether_ioctl(ifp, cmd, data);
2453 alc_mac_config(struct alc_softc *sc)
2455 struct mii_data *mii;
2458 ALC_LOCK_ASSERT(sc);
2460 mii = device_get_softc(sc->alc_miibus);
2461 reg = CSR_READ_4(sc, ALC_MAC_CFG);
2462 reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
2463 MAC_CFG_SPEED_MASK);
2464 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
2465 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
2466 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
2467 reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
2468 /* Reprogram MAC with resolved speed/duplex. */
2469 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2472 reg |= MAC_CFG_SPEED_10_100;
2475 reg |= MAC_CFG_SPEED_1000;
2478 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2479 reg |= MAC_CFG_FULL_DUPLEX;
2480 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2481 reg |= MAC_CFG_TX_FC;
2482 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2483 reg |= MAC_CFG_RX_FC;
2485 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2489 alc_stats_clear(struct alc_softc *sc)
2491 struct smb sb, *smb;
2495 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2496 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2497 sc->alc_cdata.alc_smb_map,
2498 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2499 smb = sc->alc_rdata.alc_smb;
2500 /* Update done, clear. */
2502 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2503 sc->alc_cdata.alc_smb_map,
2504 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2506 for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2508 CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2509 i += sizeof(uint32_t);
2511 /* Read Tx statistics. */
2512 for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2514 CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2515 i += sizeof(uint32_t);
2521 alc_stats_update(struct alc_softc *sc)
2523 struct alc_hw_stats *stat;
2524 struct smb sb, *smb;
2529 ALC_LOCK_ASSERT(sc);
2532 stat = &sc->alc_stats;
2533 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2534 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2535 sc->alc_cdata.alc_smb_map,
2536 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2537 smb = sc->alc_rdata.alc_smb;
2538 if (smb->updated == 0)
2542 /* Read Rx statistics. */
2543 for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2545 *reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2546 i += sizeof(uint32_t);
2548 /* Read Tx statistics. */
2549 for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2551 *reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2552 i += sizeof(uint32_t);
2557 stat->rx_frames += smb->rx_frames;
2558 stat->rx_bcast_frames += smb->rx_bcast_frames;
2559 stat->rx_mcast_frames += smb->rx_mcast_frames;
2560 stat->rx_pause_frames += smb->rx_pause_frames;
2561 stat->rx_control_frames += smb->rx_control_frames;
2562 stat->rx_crcerrs += smb->rx_crcerrs;
2563 stat->rx_lenerrs += smb->rx_lenerrs;
2564 stat->rx_bytes += smb->rx_bytes;
2565 stat->rx_runts += smb->rx_runts;
2566 stat->rx_fragments += smb->rx_fragments;
2567 stat->rx_pkts_64 += smb->rx_pkts_64;
2568 stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
2569 stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
2570 stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
2571 stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
2572 stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
2573 stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
2574 stat->rx_pkts_truncated += smb->rx_pkts_truncated;
2575 stat->rx_fifo_oflows += smb->rx_fifo_oflows;
2576 stat->rx_rrs_errs += smb->rx_rrs_errs;
2577 stat->rx_alignerrs += smb->rx_alignerrs;
2578 stat->rx_bcast_bytes += smb->rx_bcast_bytes;
2579 stat->rx_mcast_bytes += smb->rx_mcast_bytes;
2580 stat->rx_pkts_filtered += smb->rx_pkts_filtered;
2583 stat->tx_frames += smb->tx_frames;
2584 stat->tx_bcast_frames += smb->tx_bcast_frames;
2585 stat->tx_mcast_frames += smb->tx_mcast_frames;
2586 stat->tx_pause_frames += smb->tx_pause_frames;
2587 stat->tx_excess_defer += smb->tx_excess_defer;
2588 stat->tx_control_frames += smb->tx_control_frames;
2589 stat->tx_deferred += smb->tx_deferred;
2590 stat->tx_bytes += smb->tx_bytes;
2591 stat->tx_pkts_64 += smb->tx_pkts_64;
2592 stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
2593 stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
2594 stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
2595 stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
2596 stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
2597 stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
2598 stat->tx_single_colls += smb->tx_single_colls;
2599 stat->tx_multi_colls += smb->tx_multi_colls;
2600 stat->tx_late_colls += smb->tx_late_colls;
2601 stat->tx_excess_colls += smb->tx_excess_colls;
2602 stat->tx_abort += smb->tx_abort;
2603 stat->tx_underrun += smb->tx_underrun;
2604 stat->tx_desc_underrun += smb->tx_desc_underrun;
2605 stat->tx_lenerrs += smb->tx_lenerrs;
2606 stat->tx_pkts_truncated += smb->tx_pkts_truncated;
2607 stat->tx_bcast_bytes += smb->tx_bcast_bytes;
2608 stat->tx_mcast_bytes += smb->tx_mcast_bytes;
2610 /* Update counters in ifnet. */
2611 if_inc_counter(ifp, IFCOUNTER_OPACKETS, smb->tx_frames);
2613 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, smb->tx_single_colls +
2614 smb->tx_multi_colls * 2 + smb->tx_late_colls +
2615 smb->tx_abort * HDPX_CFG_RETRY_DEFAULT);
2619 * tx_pkts_truncated counter looks suspicious. It constantly
2620 * increments with no sign of Tx errors. This may indicate
2621 * the counter name is not correct one so I've removed the
2622 * counter in output errors.
2624 if_inc_counter(ifp, IFCOUNTER_OERRORS,
2625 smb->tx_abort + smb->tx_late_colls + smb->tx_underrun);
2627 if_inc_counter(ifp, IFCOUNTER_IPACKETS, smb->rx_frames);
2629 if_inc_counter(ifp, IFCOUNTER_IERRORS,
2630 smb->rx_crcerrs + smb->rx_lenerrs +
2631 smb->rx_runts + smb->rx_pkts_truncated +
2632 smb->rx_fifo_oflows + smb->rx_rrs_errs +
2635 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2636 /* Update done, clear. */
2638 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag,
2639 sc->alc_cdata.alc_smb_map,
2640 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2647 struct alc_softc *sc;
2650 sc = (struct alc_softc *)arg;
2652 status = CSR_READ_4(sc, ALC_INTR_STATUS);
2653 if ((status & ALC_INTRS) == 0)
2654 return (FILTER_STRAY);
2655 /* Disable interrupts. */
2656 CSR_WRITE_4(sc, ALC_INTR_STATUS, INTR_DIS_INT);
2657 taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
2659 return (FILTER_HANDLED);
2663 alc_int_task(void *arg, int pending)
2665 struct alc_softc *sc;
2670 sc = (struct alc_softc *)arg;
2673 status = CSR_READ_4(sc, ALC_INTR_STATUS);
2675 if (sc->alc_morework != 0) {
2676 sc->alc_morework = 0;
2677 status |= INTR_RX_PKT;
2679 if ((status & ALC_INTRS) == 0)
2682 /* Acknowledge interrupts but still disable interrupts. */
2683 CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
2686 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2687 if ((status & INTR_RX_PKT) != 0) {
2688 more = alc_rxintr(sc, sc->alc_process_limit);
2690 sc->alc_morework = 1;
2691 else if (more == EIO) {
2692 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2693 alc_init_locked(sc);
2698 if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
2699 INTR_TXQ_TO_RST)) != 0) {
2700 if ((status & INTR_DMA_RD_TO_RST) != 0)
2701 device_printf(sc->alc_dev,
2702 "DMA read error! -- resetting\n");
2703 if ((status & INTR_DMA_WR_TO_RST) != 0)
2704 device_printf(sc->alc_dev,
2705 "DMA write error! -- resetting\n");
2706 if ((status & INTR_TXQ_TO_RST) != 0)
2707 device_printf(sc->alc_dev,
2708 "TxQ reset! -- resetting\n");
2709 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2710 alc_init_locked(sc);
2714 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2715 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2716 alc_start_locked(ifp);
2719 if (more == EAGAIN ||
2720 (CSR_READ_4(sc, ALC_INTR_STATUS) & ALC_INTRS) != 0) {
2722 taskqueue_enqueue(sc->alc_tq, &sc->alc_int_task);
2727 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2728 /* Re-enable interrupts if we're running. */
2729 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
2735 alc_txeof(struct alc_softc *sc)
2738 struct alc_txdesc *txd;
2739 uint32_t cons, prod;
2742 ALC_LOCK_ASSERT(sc);
2746 if (sc->alc_cdata.alc_tx_cnt == 0)
2748 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
2749 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_POSTWRITE);
2750 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2751 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
2752 sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_POSTREAD);
2753 prod = sc->alc_rdata.alc_cmb->cons;
2755 prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
2756 /* Assume we're using normal Tx priority queue. */
2757 prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
2758 MBOX_TD_CONS_LO_IDX_SHIFT;
2759 cons = sc->alc_cdata.alc_tx_cons;
2761 * Go through our Tx list and free mbufs for those
2762 * frames which have been transmitted.
2764 for (prog = 0; cons != prod; prog++,
2765 ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
2766 if (sc->alc_cdata.alc_tx_cnt <= 0)
2769 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2770 sc->alc_cdata.alc_tx_cnt--;
2771 txd = &sc->alc_cdata.alc_txdesc[cons];
2772 if (txd->tx_m != NULL) {
2773 /* Reclaim transmitted mbufs. */
2774 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
2775 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2776 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
2783 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
2784 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag,
2785 sc->alc_cdata.alc_cmb_map, BUS_DMASYNC_PREREAD);
2786 sc->alc_cdata.alc_tx_cons = cons;
2788 * Unarm watchdog timer only when there is no pending
2789 * frames in Tx queue.
2791 if (sc->alc_cdata.alc_tx_cnt == 0)
2792 sc->alc_watchdog_timer = 0;
2796 alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd)
2799 bus_dma_segment_t segs[1];
2803 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2806 m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
2807 #ifndef __NO_STRICT_ALIGNMENT
2808 m_adj(m, sizeof(uint64_t));
2811 if (bus_dmamap_load_mbuf_sg(sc->alc_cdata.alc_rx_tag,
2812 sc->alc_cdata.alc_rx_sparemap, m, segs, &nsegs, 0) != 0) {
2816 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2818 if (rxd->rx_m != NULL) {
2819 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
2820 BUS_DMASYNC_POSTREAD);
2821 bus_dmamap_unload(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap);
2823 map = rxd->rx_dmamap;
2824 rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
2825 sc->alc_cdata.alc_rx_sparemap = map;
2826 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag, rxd->rx_dmamap,
2827 BUS_DMASYNC_PREREAD);
2829 rxd->rx_desc->addr = htole64(segs[0].ds_addr);
2834 alc_rxintr(struct alc_softc *sc, int count)
2837 struct rx_rdesc *rrd;
2838 uint32_t nsegs, status;
2841 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
2842 sc->alc_cdata.alc_rr_ring_map,
2843 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2844 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
2845 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_POSTWRITE);
2846 rr_cons = sc->alc_cdata.alc_rr_cons;
2848 for (prog = 0; (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;) {
2851 rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
2852 status = le32toh(rrd->status);
2853 if ((status & RRD_VALID) == 0)
2855 nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
2857 /* This should not happen! */
2858 device_printf(sc->alc_dev,
2859 "unexpected segment count -- resetting\n");
2863 /* Clear Rx return status. */
2865 ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
2866 sc->alc_cdata.alc_rx_cons += nsegs;
2867 sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
2872 /* Update the consumer index. */
2873 sc->alc_cdata.alc_rr_cons = rr_cons;
2874 /* Sync Rx return descriptors. */
2875 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
2876 sc->alc_cdata.alc_rr_ring_map,
2877 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2879 * Sync updated Rx descriptors such that controller see
2880 * modified buffer addresses.
2882 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
2883 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
2885 * Let controller know availability of new Rx buffers.
2886 * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
2887 * it may be possible to update ALC_MBOX_RD0_PROD_IDX
2888 * only when Rx buffer pre-fetching is required. In
2889 * addition we already set ALC_RX_RD_FREE_THRESH to
2890 * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
2891 * it still seems that pre-fetching needs more
2894 CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
2895 sc->alc_cdata.alc_rx_cons);
2898 return (count > 0 ? 0 : EAGAIN);
2901 #ifndef __NO_STRICT_ALIGNMENT
2902 static struct mbuf *
2903 alc_fixup_rx(struct ifnet *ifp, struct mbuf *m)
2907 uint16_t *src, *dst;
2909 src = mtod(m, uint16_t *);
2912 if (m->m_next == NULL) {
2913 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2919 * Append a new mbuf to received mbuf chain and copy ethernet
2920 * header from the mbuf chain. This can save lots of CPU
2921 * cycles for jumbo frame.
2923 MGETHDR(n, M_NOWAIT, MT_DATA);
2925 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2929 bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
2930 m->m_data += ETHER_HDR_LEN;
2931 m->m_len -= ETHER_HDR_LEN;
2932 n->m_len = ETHER_HDR_LEN;
2933 M_MOVE_PKTHDR(n, m);
2939 /* Receive a frame. */
2941 alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
2943 struct alc_rxdesc *rxd;
2945 struct mbuf *mp, *m;
2946 uint32_t rdinfo, status, vtag;
2947 int count, nsegs, rx_cons;
2950 status = le32toh(rrd->status);
2951 rdinfo = le32toh(rrd->rdinfo);
2952 rx_cons = RRD_RD_IDX(rdinfo);
2953 nsegs = RRD_RD_CNT(rdinfo);
2955 sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
2956 if ((status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) != 0) {
2958 * We want to pass the following frames to upper
2959 * layer regardless of error status of Rx return
2962 * o IP/TCP/UDP checksum is bad.
2963 * o frame length and protocol specific length
2966 * Force network stack compute checksum for
2969 status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
2970 if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
2971 RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
2975 for (count = 0; count < nsegs; count++,
2976 ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
2977 rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
2979 /* Add a new receive buffer to the ring. */
2980 if (alc_newbuf(sc, rxd) != 0) {
2981 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2982 /* Reuse Rx buffers. */
2983 if (sc->alc_cdata.alc_rxhead != NULL)
2984 m_freem(sc->alc_cdata.alc_rxhead);
2989 * Assume we've received a full sized frame.
2990 * Actual size is fixed when we encounter the end of
2991 * multi-segmented frame.
2993 mp->m_len = sc->alc_buf_size;
2995 /* Chain received mbufs. */
2996 if (sc->alc_cdata.alc_rxhead == NULL) {
2997 sc->alc_cdata.alc_rxhead = mp;
2998 sc->alc_cdata.alc_rxtail = mp;
3000 mp->m_flags &= ~M_PKTHDR;
3001 sc->alc_cdata.alc_rxprev_tail =
3002 sc->alc_cdata.alc_rxtail;
3003 sc->alc_cdata.alc_rxtail->m_next = mp;
3004 sc->alc_cdata.alc_rxtail = mp;
3007 if (count == nsegs - 1) {
3008 /* Last desc. for this frame. */
3009 m = sc->alc_cdata.alc_rxhead;
3010 m->m_flags |= M_PKTHDR;
3012 * It seems that L1C/L2C controller has no way
3013 * to tell hardware to strip CRC bytes.
3016 sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
3018 /* Set last mbuf size. */
3019 mp->m_len = sc->alc_cdata.alc_rxlen -
3020 (nsegs - 1) * sc->alc_buf_size;
3021 /* Remove the CRC bytes in chained mbufs. */
3022 if (mp->m_len <= ETHER_CRC_LEN) {
3023 sc->alc_cdata.alc_rxtail =
3024 sc->alc_cdata.alc_rxprev_tail;
3025 sc->alc_cdata.alc_rxtail->m_len -=
3026 (ETHER_CRC_LEN - mp->m_len);
3027 sc->alc_cdata.alc_rxtail->m_next = NULL;
3030 mp->m_len -= ETHER_CRC_LEN;
3033 m->m_len = m->m_pkthdr.len;
3034 m->m_pkthdr.rcvif = ifp;
3036 * Due to hardware bugs, Rx checksum offloading
3037 * was intentionally disabled.
3039 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
3040 (status & RRD_VLAN_TAG) != 0) {
3041 vtag = RRD_VLAN(le32toh(rrd->vtag));
3042 m->m_pkthdr.ether_vtag = ntohs(vtag);
3043 m->m_flags |= M_VLANTAG;
3045 #ifndef __NO_STRICT_ALIGNMENT
3046 m = alc_fixup_rx(ifp, m);
3052 (*ifp->if_input)(ifp, m);
3057 /* Reset mbuf chains. */
3058 ALC_RXCHAIN_RESET(sc);
3064 struct alc_softc *sc;
3065 struct mii_data *mii;
3067 sc = (struct alc_softc *)arg;
3069 ALC_LOCK_ASSERT(sc);
3071 mii = device_get_softc(sc->alc_miibus);
3073 alc_stats_update(sc);
3075 * alc(4) does not rely on Tx completion interrupts to reclaim
3076 * transferred buffers. Instead Tx completion interrupts are
3077 * used to hint for scheduling Tx task. So it's necessary to
3078 * release transmitted buffers by kicking Tx completion
3079 * handler. This limits the maximum reclamation delay to a hz.
3083 callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3087 alc_reset(struct alc_softc *sc)
3092 reg = CSR_READ_4(sc, ALC_MASTER_CFG) & 0xFFFF;
3093 reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
3094 CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3095 for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3097 if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
3101 device_printf(sc->alc_dev, "master reset timeout!\n");
3103 for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
3104 if ((reg = CSR_READ_4(sc, ALC_IDLE_STATUS)) == 0)
3110 device_printf(sc->alc_dev, "reset timeout(0x%08x)!\n", reg);
3116 struct alc_softc *sc;
3118 sc = (struct alc_softc *)xsc;
3120 alc_init_locked(sc);
3125 alc_init_locked(struct alc_softc *sc)
3128 struct mii_data *mii;
3129 uint8_t eaddr[ETHER_ADDR_LEN];
3131 uint32_t reg, rxf_hi, rxf_lo;
3133 ALC_LOCK_ASSERT(sc);
3136 mii = device_get_softc(sc->alc_miibus);
3138 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3141 * Cancel any pending I/O.
3145 * Reset the chip to a known state.
3149 /* Initialize Rx descriptors. */
3150 if (alc_init_rx_ring(sc) != 0) {
3151 device_printf(sc->alc_dev, "no memory for Rx buffers.\n");
3155 alc_init_rr_ring(sc);
3156 alc_init_tx_ring(sc);
3160 /* Enable all clocks. */
3161 CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
3163 /* Reprogram the station address. */
3164 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
3165 CSR_WRITE_4(sc, ALC_PAR0,
3166 eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
3167 CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
3169 * Clear WOL status and disable all WOL feature as WOL
3170 * would interfere Rx operation under normal environments.
3172 CSR_READ_4(sc, ALC_WOL_CFG);
3173 CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
3174 /* Set Tx descriptor base addresses. */
3175 paddr = sc->alc_rdata.alc_tx_ring_paddr;
3176 CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3177 CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3178 /* We don't use high priority ring. */
3179 CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
3180 /* Set Tx descriptor counter. */
3181 CSR_WRITE_4(sc, ALC_TD_RING_CNT,
3182 (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
3183 /* Set Rx descriptor base addresses. */
3184 paddr = sc->alc_rdata.alc_rx_ring_paddr;
3185 CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3186 CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3187 /* We use one Rx ring. */
3188 CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
3189 CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
3190 CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
3191 /* Set Rx descriptor counter. */
3192 CSR_WRITE_4(sc, ALC_RD_RING_CNT,
3193 (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
3196 * Let hardware split jumbo frames into alc_max_buf_sized chunks.
3197 * if it do not fit the buffer size. Rx return descriptor holds
3198 * a counter that indicates how many fragments were made by the
3199 * hardware. The buffer size should be multiple of 8 bytes.
3200 * Since hardware has limit on the size of buffer size, always
3201 * use the maximum value.
3202 * For strict-alignment architectures make sure to reduce buffer
3203 * size by 8 bytes to make room for alignment fixup.
3205 #ifndef __NO_STRICT_ALIGNMENT
3206 sc->alc_buf_size = RX_BUF_SIZE_MAX - sizeof(uint64_t);
3208 sc->alc_buf_size = RX_BUF_SIZE_MAX;
3210 CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
3212 paddr = sc->alc_rdata.alc_rr_ring_paddr;
3213 /* Set Rx return descriptor base addresses. */
3214 CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
3215 /* We use one Rx return ring. */
3216 CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
3217 CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
3218 CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
3219 /* Set Rx return descriptor counter. */
3220 CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
3221 (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
3222 paddr = sc->alc_rdata.alc_cmb_paddr;
3223 CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
3224 paddr = sc->alc_rdata.alc_smb_paddr;
3225 CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
3226 CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
3228 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B) {
3229 /* Reconfigure SRAM - Vendor magic. */
3230 CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
3231 CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
3232 CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
3233 CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
3234 CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
3235 CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
3236 CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
3237 CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
3240 /* Tell hardware that we're ready to load DMA blocks. */
3241 CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
3243 /* Configure interrupt moderation timer. */
3244 reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
3245 reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
3246 CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
3248 * We don't want to automatic interrupt clear as task queue
3249 * for the interrupt should know interrupt status.
3251 reg = MASTER_SA_TIMER_ENB;
3252 if (ALC_USECS(sc->alc_int_rx_mod) != 0)
3253 reg |= MASTER_IM_RX_TIMER_ENB;
3254 if (ALC_USECS(sc->alc_int_tx_mod) != 0)
3255 reg |= MASTER_IM_TX_TIMER_ENB;
3256 CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
3258 * Disable interrupt re-trigger timer. We don't want automatic
3259 * re-triggering of un-ACKed interrupts.
3261 CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
3262 /* Configure CMB. */
3263 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
3264 CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
3265 CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
3267 CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
3269 * Hardware can be configured to issue SMB interrupt based
3270 * on programmed interval. Since there is a callout that is
3271 * invoked for every hz in driver we use that instead of
3272 * relying on periodic SMB interrupt.
3274 CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
3275 /* Clear MAC statistics. */
3276 alc_stats_clear(sc);
3279 * Always use maximum frame size that controller can support.
3280 * Otherwise received frames that has larger frame length
3281 * than alc(4) MTU would be silently dropped in hardware. This
3282 * would make path-MTU discovery hard as sender wouldn't get
3283 * any responses from receiver. alc(4) supports
3284 * multi-fragmented frames on Rx path so it has no issue on
3285 * assembling fragmented frames. Using maximum frame size also
3286 * removes the need to reinitialize hardware when interface
3287 * MTU configuration was changed.
3289 * Be conservative in what you do, be liberal in what you
3290 * accept from others - RFC 793.
3292 CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
3294 /* Disable header split(?) */
3295 CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
3297 /* Configure IPG/IFG parameters. */
3298 CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
3299 ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK) |
3300 ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
3301 ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
3302 ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK));
3303 /* Set parameters for half-duplex media. */
3304 CSR_WRITE_4(sc, ALC_HDPX_CFG,
3305 ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
3306 HDPX_CFG_LCOL_MASK) |
3307 ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
3308 HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
3309 ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
3310 HDPX_CFG_ABEBT_MASK) |
3311 ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
3312 HDPX_CFG_JAMIPG_MASK));
3314 * Set TSO/checksum offload threshold. For frames that is
3315 * larger than this threshold, hardware wouldn't do
3316 * TSO/checksum offloading.
3318 CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH,
3319 (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
3320 TSO_OFFLOAD_THRESH_MASK);
3321 /* Configure TxQ. */
3322 reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
3323 TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
3324 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3325 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
3327 reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
3328 TXQ_CFG_TD_BURST_MASK;
3329 CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
3331 /* Configure Rx free descriptor pre-fetching. */
3332 CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
3333 ((RX_RD_FREE_THRESH_HI_DEFAULT << RX_RD_FREE_THRESH_HI_SHIFT) &
3334 RX_RD_FREE_THRESH_HI_MASK) |
3335 ((RX_RD_FREE_THRESH_LO_DEFAULT << RX_RD_FREE_THRESH_LO_SHIFT) &
3336 RX_RD_FREE_THRESH_LO_MASK));
3339 * Configure flow control parameters.
3340 * XON : 80% of Rx FIFO
3341 * XOFF : 30% of Rx FIFO
3343 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8131 ||
3344 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8132) {
3345 reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
3346 rxf_hi = (reg * 8) / 10;
3347 rxf_lo = (reg * 3) / 10;
3348 CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3349 ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3350 RX_FIFO_PAUSE_THRESH_LO_MASK) |
3351 ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3352 RX_FIFO_PAUSE_THRESH_HI_MASK));
3355 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B ||
3356 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2)
3357 CSR_WRITE_4(sc, ALC_SERDES_LOCK,
3358 CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
3359 SERDES_PHY_CLK_SLOWDOWN);
3361 /* Disable RSS until I understand L1C/L2C's RSS logic. */
3362 CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
3363 CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
3365 /* Configure RxQ. */
3366 reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
3367 RXQ_CFG_RD_BURST_MASK;
3368 reg |= RXQ_CFG_RSS_MODE_DIS;
3369 if ((sc->alc_flags & ALC_FLAG_ASPM_MON) != 0)
3370 reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_1M;
3371 CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3373 /* Configure DMA parameters. */
3374 reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
3376 if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3377 reg |= DMA_CFG_CMB_ENB;
3378 if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
3379 reg |= DMA_CFG_SMB_ENB;
3381 reg |= DMA_CFG_SMB_DIS;
3382 reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
3383 DMA_CFG_RD_BURST_SHIFT;
3384 reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
3385 DMA_CFG_WR_BURST_SHIFT;
3386 reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
3387 DMA_CFG_RD_DELAY_CNT_MASK;
3388 reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
3389 DMA_CFG_WR_DELAY_CNT_MASK;
3390 CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3393 * Configure Tx/Rx MACs.
3394 * - Auto-padding for short frames.
3395 * - Enable CRC generation.
3396 * Actual reconfiguration of MAC for resolved speed/duplex
3397 * is followed after detection of link establishment.
3398 * AR813x/AR815x always does checksum computation regardless
3399 * of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
3400 * have bug in protocol field in Rx return structure so
3401 * these controllers can't handle fragmented frames. Disable
3402 * Rx checksum offloading until there is a newer controller
3403 * that has sane implementation.
3405 reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
3406 ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
3407 MAC_CFG_PREAMBLE_MASK);
3408 if (sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151 ||
3409 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8151_V2 ||
3410 sc->alc_ident->deviceid == DEVICEID_ATHEROS_AR8152_B2)
3411 reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3412 if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
3413 reg |= MAC_CFG_SPEED_10_100;
3415 reg |= MAC_CFG_SPEED_1000;
3416 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3418 /* Set up the receive filter. */
3422 /* Acknowledge all pending interrupts and clear it. */
3423 CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
3424 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3425 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
3427 sc->alc_flags &= ~ALC_FLAG_LINK;
3428 /* Switch to the current media. */
3431 callout_reset(&sc->alc_tick_ch, hz, alc_tick, sc);
3433 ifp->if_drv_flags |= IFF_DRV_RUNNING;
3434 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3438 alc_stop(struct alc_softc *sc)
3441 struct alc_txdesc *txd;
3442 struct alc_rxdesc *rxd;
3446 ALC_LOCK_ASSERT(sc);
3448 * Mark the interface down and cancel the watchdog timer.
3451 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3452 sc->alc_flags &= ~ALC_FLAG_LINK;
3453 callout_stop(&sc->alc_tick_ch);
3454 sc->alc_watchdog_timer = 0;
3455 alc_stats_update(sc);
3456 /* Disable interrupts. */
3457 CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
3458 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3461 reg = CSR_READ_4(sc, ALC_DMA_CFG);
3462 reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
3463 reg |= DMA_CFG_SMB_DIS;
3464 CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3466 /* Stop Rx/Tx MACs. */
3468 /* Disable interrupts which might be touched in taskq handler. */
3469 CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3471 /* Reclaim Rx buffers that have been processed. */
3472 if (sc->alc_cdata.alc_rxhead != NULL)
3473 m_freem(sc->alc_cdata.alc_rxhead);
3474 ALC_RXCHAIN_RESET(sc);
3476 * Free Tx/Rx mbufs still in the queues.
3478 for (i = 0; i < ALC_RX_RING_CNT; i++) {
3479 rxd = &sc->alc_cdata.alc_rxdesc[i];
3480 if (rxd->rx_m != NULL) {
3481 bus_dmamap_sync(sc->alc_cdata.alc_rx_tag,
3482 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3483 bus_dmamap_unload(sc->alc_cdata.alc_rx_tag,
3489 for (i = 0; i < ALC_TX_RING_CNT; i++) {
3490 txd = &sc->alc_cdata.alc_txdesc[i];
3491 if (txd->tx_m != NULL) {
3492 bus_dmamap_sync(sc->alc_cdata.alc_tx_tag,
3493 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3494 bus_dmamap_unload(sc->alc_cdata.alc_tx_tag,
3503 alc_stop_mac(struct alc_softc *sc)
3508 ALC_LOCK_ASSERT(sc);
3510 /* Disable Rx/Tx MAC. */
3511 reg = CSR_READ_4(sc, ALC_MAC_CFG);
3512 if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
3513 reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
3514 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3516 for (i = ALC_TIMEOUT; i > 0; i--) {
3517 reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3523 device_printf(sc->alc_dev,
3524 "could not disable Rx/Tx MAC(0x%08x)!\n", reg);
3528 alc_start_queue(struct alc_softc *sc)
3533 RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
3534 RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
3539 ALC_LOCK_ASSERT(sc);
3542 cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
3543 cfg &= ~RXQ_CFG_ENB;
3545 CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
3547 cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
3549 CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
3553 alc_stop_queue(struct alc_softc *sc)
3558 ALC_LOCK_ASSERT(sc);
3561 reg = CSR_READ_4(sc, ALC_RXQ_CFG);
3562 if ((reg & RXQ_CFG_ENB) != 0) {
3563 reg &= ~RXQ_CFG_ENB;
3564 CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3567 reg = CSR_READ_4(sc, ALC_TXQ_CFG);
3568 if ((reg & TXQ_CFG_ENB) != 0) {
3569 reg &= ~TXQ_CFG_ENB;
3570 CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
3572 for (i = ALC_TIMEOUT; i > 0; i--) {
3573 reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3574 if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3579 device_printf(sc->alc_dev,
3580 "could not disable RxQ/TxQ (0x%08x)!\n", reg);
3584 alc_init_tx_ring(struct alc_softc *sc)
3586 struct alc_ring_data *rd;
3587 struct alc_txdesc *txd;
3590 ALC_LOCK_ASSERT(sc);
3592 sc->alc_cdata.alc_tx_prod = 0;
3593 sc->alc_cdata.alc_tx_cons = 0;
3594 sc->alc_cdata.alc_tx_cnt = 0;
3596 rd = &sc->alc_rdata;
3597 bzero(rd->alc_tx_ring, ALC_TX_RING_SZ);
3598 for (i = 0; i < ALC_TX_RING_CNT; i++) {
3599 txd = &sc->alc_cdata.alc_txdesc[i];
3603 bus_dmamap_sync(sc->alc_cdata.alc_tx_ring_tag,
3604 sc->alc_cdata.alc_tx_ring_map, BUS_DMASYNC_PREWRITE);
3608 alc_init_rx_ring(struct alc_softc *sc)
3610 struct alc_ring_data *rd;
3611 struct alc_rxdesc *rxd;
3614 ALC_LOCK_ASSERT(sc);
3616 sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
3617 sc->alc_morework = 0;
3618 rd = &sc->alc_rdata;
3619 bzero(rd->alc_rx_ring, ALC_RX_RING_SZ);
3620 for (i = 0; i < ALC_RX_RING_CNT; i++) {
3621 rxd = &sc->alc_cdata.alc_rxdesc[i];
3623 rxd->rx_desc = &rd->alc_rx_ring[i];
3624 if (alc_newbuf(sc, rxd) != 0)
3629 * Since controller does not update Rx descriptors, driver
3630 * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
3631 * is enough to ensure coherence.
3633 bus_dmamap_sync(sc->alc_cdata.alc_rx_ring_tag,
3634 sc->alc_cdata.alc_rx_ring_map, BUS_DMASYNC_PREWRITE);
3635 /* Let controller know availability of new Rx buffers. */
3636 CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
3642 alc_init_rr_ring(struct alc_softc *sc)
3644 struct alc_ring_data *rd;
3646 ALC_LOCK_ASSERT(sc);
3648 sc->alc_cdata.alc_rr_cons = 0;
3649 ALC_RXCHAIN_RESET(sc);
3651 rd = &sc->alc_rdata;
3652 bzero(rd->alc_rr_ring, ALC_RR_RING_SZ);
3653 bus_dmamap_sync(sc->alc_cdata.alc_rr_ring_tag,
3654 sc->alc_cdata.alc_rr_ring_map,
3655 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3659 alc_init_cmb(struct alc_softc *sc)
3661 struct alc_ring_data *rd;
3663 ALC_LOCK_ASSERT(sc);
3665 rd = &sc->alc_rdata;
3666 bzero(rd->alc_cmb, ALC_CMB_SZ);
3667 bus_dmamap_sync(sc->alc_cdata.alc_cmb_tag, sc->alc_cdata.alc_cmb_map,
3668 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3672 alc_init_smb(struct alc_softc *sc)
3674 struct alc_ring_data *rd;
3676 ALC_LOCK_ASSERT(sc);
3678 rd = &sc->alc_rdata;
3679 bzero(rd->alc_smb, ALC_SMB_SZ);
3680 bus_dmamap_sync(sc->alc_cdata.alc_smb_tag, sc->alc_cdata.alc_smb_map,
3681 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3685 alc_rxvlan(struct alc_softc *sc)
3690 ALC_LOCK_ASSERT(sc);
3693 reg = CSR_READ_4(sc, ALC_MAC_CFG);
3694 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
3695 reg |= MAC_CFG_VLAN_TAG_STRIP;
3697 reg &= ~MAC_CFG_VLAN_TAG_STRIP;
3698 CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3702 alc_rxfilter(struct alc_softc *sc)
3705 struct ifmultiaddr *ifma;
3710 ALC_LOCK_ASSERT(sc);
3714 bzero(mchash, sizeof(mchash));
3715 rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
3716 rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
3717 if ((ifp->if_flags & IFF_BROADCAST) != 0)
3718 rxcfg |= MAC_CFG_BCAST;
3719 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
3720 if ((ifp->if_flags & IFF_PROMISC) != 0)
3721 rxcfg |= MAC_CFG_PROMISC;
3722 if ((ifp->if_flags & IFF_ALLMULTI) != 0)
3723 rxcfg |= MAC_CFG_ALLMULTI;
3724 mchash[0] = 0xFFFFFFFF;
3725 mchash[1] = 0xFFFFFFFF;
3729 if_maddr_rlock(ifp);
3730 TAILQ_FOREACH(ifma, &sc->alc_ifp->if_multiaddrs, ifma_link) {
3731 if (ifma->ifma_addr->sa_family != AF_LINK)
3733 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3734 ifma->ifma_addr), ETHER_ADDR_LEN);
3735 mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
3737 if_maddr_runlock(ifp);
3740 CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
3741 CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
3742 CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
3746 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3752 value = *(int *)arg1;
3753 error = sysctl_handle_int(oidp, &value, 0, req);
3754 if (error || req->newptr == NULL)
3756 if (value < low || value > high)
3758 *(int *)arg1 = value;
3764 sysctl_hw_alc_proc_limit(SYSCTL_HANDLER_ARGS)
3766 return (sysctl_int_range(oidp, arg1, arg2, req,
3767 ALC_PROC_MIN, ALC_PROC_MAX));
3771 sysctl_hw_alc_int_mod(SYSCTL_HANDLER_ARGS)
3774 return (sysctl_int_range(oidp, arg1, arg2, req,
3775 ALC_IM_TIMER_MIN, ALC_IM_TIMER_MAX));