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