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