if_alc.c revision 1.22 1 1.22 ozaki /* $NetBSD: if_alc.c,v 1.22 2016/12/08 01:12:01 ozaki-r Exp $ */
2 1.1 jmcneill /* $OpenBSD: if_alc.c,v 1.1 2009/08/08 09:31:13 kevlo Exp $ */
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2009, Pyun YongHyeon <yongari (at) FreeBSD.org>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice unmodified, this list of conditions, and the following
12 1.1 jmcneill * disclaimer.
13 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
15 1.1 jmcneill * documentation and/or other materials provided with the distribution.
16 1.1 jmcneill *
17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 jmcneill * SUCH DAMAGE.
28 1.1 jmcneill */
29 1.1 jmcneill
30 1.2 jmcneill /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
31 1.1 jmcneill
32 1.1 jmcneill #ifdef _KERNEL_OPT
33 1.1 jmcneill #include "vlan.h"
34 1.1 jmcneill #endif
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/proc.h>
38 1.1 jmcneill #include <sys/endian.h>
39 1.1 jmcneill #include <sys/systm.h>
40 1.1 jmcneill #include <sys/types.h>
41 1.1 jmcneill #include <sys/sockio.h>
42 1.1 jmcneill #include <sys/mbuf.h>
43 1.1 jmcneill #include <sys/queue.h>
44 1.1 jmcneill #include <sys/kernel.h>
45 1.1 jmcneill #include <sys/device.h>
46 1.1 jmcneill #include <sys/callout.h>
47 1.1 jmcneill #include <sys/socket.h>
48 1.1 jmcneill #include <sys/module.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <sys/bus.h>
51 1.1 jmcneill
52 1.1 jmcneill #include <net/if.h>
53 1.1 jmcneill #include <net/if_dl.h>
54 1.1 jmcneill #include <net/if_llc.h>
55 1.1 jmcneill #include <net/if_media.h>
56 1.1 jmcneill #include <net/if_ether.h>
57 1.1 jmcneill
58 1.1 jmcneill #include <net/bpf.h>
59 1.1 jmcneill
60 1.1 jmcneill #ifdef INET
61 1.1 jmcneill #include <netinet/in.h>
62 1.1 jmcneill #include <netinet/in_systm.h>
63 1.1 jmcneill #include <netinet/in_var.h>
64 1.1 jmcneill #include <netinet/ip.h>
65 1.1 jmcneill #endif
66 1.1 jmcneill
67 1.1 jmcneill #include <net/if_types.h>
68 1.1 jmcneill #include <net/if_vlanvar.h>
69 1.1 jmcneill
70 1.1 jmcneill #include <net/bpf.h>
71 1.1 jmcneill
72 1.1 jmcneill #include <dev/mii/mii.h>
73 1.1 jmcneill #include <dev/mii/miivar.h>
74 1.1 jmcneill
75 1.1 jmcneill #include <dev/pci/pcireg.h>
76 1.1 jmcneill #include <dev/pci/pcivar.h>
77 1.1 jmcneill #include <dev/pci/pcidevs.h>
78 1.1 jmcneill
79 1.1 jmcneill #include <dev/pci/if_alcreg.h>
80 1.1 jmcneill
81 1.2 jmcneill /*
82 1.2 jmcneill * Devices supported by this driver.
83 1.2 jmcneill */
84 1.2 jmcneill static struct alc_ident alc_ident_table[] = {
85 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8131, 9 * 1024,
86 1.2 jmcneill "Atheros AR8131 PCIe Gigabit Ethernet" },
87 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8132, 9 * 1024,
88 1.2 jmcneill "Atheros AR8132 PCIe Fast Ethernet" },
89 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151, 6 * 1024,
90 1.2 jmcneill "Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
91 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151_V2, 6 * 1024,
92 1.2 jmcneill "Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
93 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B, 6 * 1024,
94 1.2 jmcneill "Atheros AR8152 v1.1 PCIe Fast Ethernet" },
95 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B2, 6 * 1024,
96 1.2 jmcneill "Atheros AR8152 v2.0 PCIe Fast Ethernet" },
97 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8161, 9 * 1024,
98 1.12 christos "Atheros AR8161 PCIe Gigabit Ethernet" },
99 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8162, 9 * 1024,
100 1.12 christos "Atheros AR8162 PCIe Fast Ethernet" },
101 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8171, 9 * 1024,
102 1.12 christos "Atheros AR8171 PCIe Gigabit Ethernet" },
103 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8172, 9 * 1024,
104 1.12 christos "Atheros AR8172 PCIe Fast Ethernet" },
105 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_E2200, 9 * 1024,
106 1.12 christos "Killer E2200 Gigabit Ethernet" },
107 1.2 jmcneill { 0, 0, 0, NULL },
108 1.2 jmcneill };
109 1.2 jmcneill
110 1.1 jmcneill static int alc_match(device_t, cfdata_t, void *);
111 1.1 jmcneill static void alc_attach(device_t, device_t, void *);
112 1.1 jmcneill static int alc_detach(device_t, int);
113 1.1 jmcneill
114 1.1 jmcneill static int alc_init(struct ifnet *);
115 1.7 mrg static int alc_init_backend(struct ifnet *, bool);
116 1.1 jmcneill static void alc_start(struct ifnet *);
117 1.1 jmcneill static int alc_ioctl(struct ifnet *, u_long, void *);
118 1.1 jmcneill static void alc_watchdog(struct ifnet *);
119 1.1 jmcneill static int alc_mediachange(struct ifnet *);
120 1.1 jmcneill static void alc_mediastatus(struct ifnet *, struct ifmediareq *);
121 1.1 jmcneill
122 1.12 christos static void alc_aspm(struct alc_softc *, int, int);
123 1.12 christos static void alc_aspm_813x(struct alc_softc *, int);
124 1.12 christos static void alc_aspm_816x(struct alc_softc *, int);
125 1.1 jmcneill static void alc_disable_l0s_l1(struct alc_softc *);
126 1.1 jmcneill static int alc_dma_alloc(struct alc_softc *);
127 1.1 jmcneill static void alc_dma_free(struct alc_softc *);
128 1.12 christos static void alc_dsp_fixup(struct alc_softc *, int);
129 1.1 jmcneill static int alc_encap(struct alc_softc *, struct mbuf **);
130 1.2 jmcneill static struct alc_ident *
131 1.2 jmcneill alc_find_ident(struct pci_attach_args *);
132 1.1 jmcneill static void alc_get_macaddr(struct alc_softc *);
133 1.12 christos static void alc_get_macaddr_813x(struct alc_softc *);
134 1.12 christos static void alc_get_macaddr_816x(struct alc_softc *);
135 1.12 christos static void alc_get_macaddr_par(struct alc_softc *);
136 1.1 jmcneill static void alc_init_cmb(struct alc_softc *);
137 1.1 jmcneill static void alc_init_rr_ring(struct alc_softc *);
138 1.7 mrg static int alc_init_rx_ring(struct alc_softc *, bool);
139 1.1 jmcneill static void alc_init_smb(struct alc_softc *);
140 1.1 jmcneill static void alc_init_tx_ring(struct alc_softc *);
141 1.1 jmcneill static int alc_intr(void *);
142 1.1 jmcneill static void alc_mac_config(struct alc_softc *);
143 1.12 christos static uint32_t alc_mii_readreg_813x(struct alc_softc *, int, int);
144 1.12 christos static uint32_t alc_mii_readreg_816x(struct alc_softc *, int, int);
145 1.12 christos static void alc_mii_writereg_813x(struct alc_softc *, int, int, int);
146 1.12 christos static void alc_mii_writereg_816x(struct alc_softc *, int, int, int);
147 1.1 jmcneill static int alc_miibus_readreg(device_t, int, int);
148 1.6 matt static void alc_miibus_statchg(struct ifnet *);
149 1.1 jmcneill static void alc_miibus_writereg(device_t, int, int, int);
150 1.12 christos static uint32_t alc_miidbg_readreg(struct alc_softc *, int);
151 1.12 christos static void alc_miidbg_writereg(struct alc_softc *, int, int);
152 1.12 christos static uint32_t alc_miiext_readreg(struct alc_softc *, int, int);
153 1.12 christos static uint32_t alc_miiext_writereg(struct alc_softc *, int, int, int);
154 1.7 mrg static int alc_newbuf(struct alc_softc *, struct alc_rxdesc *, bool);
155 1.1 jmcneill static void alc_phy_down(struct alc_softc *);
156 1.1 jmcneill static void alc_phy_reset(struct alc_softc *);
157 1.12 christos static void alc_phy_reset_813x(struct alc_softc *);
158 1.12 christos static void alc_phy_reset_816x(struct alc_softc *);
159 1.1 jmcneill static void alc_reset(struct alc_softc *);
160 1.1 jmcneill static void alc_rxeof(struct alc_softc *, struct rx_rdesc *);
161 1.1 jmcneill static int alc_rxintr(struct alc_softc *);
162 1.1 jmcneill static void alc_iff(struct alc_softc *);
163 1.1 jmcneill static void alc_rxvlan(struct alc_softc *);
164 1.1 jmcneill static void alc_start_queue(struct alc_softc *);
165 1.1 jmcneill static void alc_stats_clear(struct alc_softc *);
166 1.1 jmcneill static void alc_stats_update(struct alc_softc *);
167 1.1 jmcneill static void alc_stop(struct ifnet *, int);
168 1.1 jmcneill static void alc_stop_mac(struct alc_softc *);
169 1.1 jmcneill static void alc_stop_queue(struct alc_softc *);
170 1.1 jmcneill static void alc_tick(void *);
171 1.1 jmcneill static void alc_txeof(struct alc_softc *);
172 1.1 jmcneill
173 1.1 jmcneill uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0 };
174 1.1 jmcneill
175 1.1 jmcneill CFATTACH_DECL_NEW(alc, sizeof(struct alc_softc),
176 1.1 jmcneill alc_match, alc_attach, alc_detach, NULL);
177 1.1 jmcneill
178 1.1 jmcneill int alcdebug = 0;
179 1.1 jmcneill #define DPRINTF(x) do { if (alcdebug) printf x; } while (0)
180 1.1 jmcneill
181 1.1 jmcneill #define ETHER_ALIGN 2
182 1.1 jmcneill #define ALC_CSUM_FEATURES (M_CSUM_TCPv4 | M_CSUM_UDPv4)
183 1.1 jmcneill
184 1.1 jmcneill static int
185 1.1 jmcneill alc_miibus_readreg(device_t dev, int phy, int reg)
186 1.1 jmcneill {
187 1.1 jmcneill struct alc_softc *sc = device_private(dev);
188 1.12 christos int v;
189 1.12 christos
190 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
191 1.12 christos v = alc_mii_readreg_816x(sc, phy, reg);
192 1.12 christos else
193 1.12 christos v = alc_mii_readreg_813x(sc, phy, reg);
194 1.12 christos return (v);
195 1.12 christos }
196 1.12 christos
197 1.12 christos static uint32_t
198 1.12 christos alc_mii_readreg_813x(struct alc_softc *sc, int phy, int reg)
199 1.12 christos {
200 1.1 jmcneill uint32_t v;
201 1.1 jmcneill int i;
202 1.1 jmcneill
203 1.1 jmcneill if (phy != sc->alc_phyaddr)
204 1.1 jmcneill return (0);
205 1.1 jmcneill
206 1.12 christos /*
207 1.12 christos * For AR8132 fast ethernet controller, do not report 1000baseT
208 1.12 christos * capability to mii(4). Even though AR8132 uses the same
209 1.12 christos * model/revision number of F1 gigabit PHY, the PHY has no
210 1.12 christos * ability to establish 1000baseT link.
211 1.12 christos */
212 1.12 christos if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
213 1.12 christos reg == MII_EXTSR)
214 1.12 christos return 0;
215 1.12 christos
216 1.1 jmcneill CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
217 1.1 jmcneill MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
218 1.1 jmcneill for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
219 1.1 jmcneill DELAY(5);
220 1.1 jmcneill v = CSR_READ_4(sc, ALC_MDIO);
221 1.1 jmcneill if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
222 1.1 jmcneill break;
223 1.1 jmcneill }
224 1.1 jmcneill
225 1.1 jmcneill if (i == 0) {
226 1.1 jmcneill printf("%s: phy read timeout: phy %d, reg %d\n",
227 1.1 jmcneill device_xname(sc->sc_dev), phy, reg);
228 1.1 jmcneill return (0);
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.1 jmcneill return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.12 christos static uint32_t
235 1.12 christos alc_mii_readreg_816x(struct alc_softc *sc, int phy, int reg)
236 1.12 christos {
237 1.12 christos uint32_t clk, v;
238 1.12 christos int i;
239 1.12 christos
240 1.12 christos if (phy != sc->alc_phyaddr)
241 1.12 christos return (0);
242 1.12 christos
243 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
244 1.12 christos clk = MDIO_CLK_25_128;
245 1.12 christos else
246 1.12 christos clk = MDIO_CLK_25_4;
247 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
248 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_REG_ADDR(reg));
249 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
250 1.12 christos DELAY(5);
251 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
252 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
253 1.12 christos break;
254 1.12 christos }
255 1.12 christos
256 1.12 christos if (i == 0) {
257 1.12 christos printf("%s: phy read timeout: phy %d, reg %d\n",
258 1.12 christos device_xname(sc->sc_dev), phy, reg);
259 1.12 christos return (0);
260 1.12 christos }
261 1.12 christos
262 1.12 christos return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
263 1.12 christos }
264 1.12 christos
265 1.1 jmcneill static void
266 1.1 jmcneill alc_miibus_writereg(device_t dev, int phy, int reg, int val)
267 1.1 jmcneill {
268 1.1 jmcneill struct alc_softc *sc = device_private(dev);
269 1.12 christos
270 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
271 1.12 christos alc_mii_writereg_816x(sc, phy, reg, val);
272 1.12 christos else
273 1.12 christos alc_mii_writereg_813x(sc, phy, reg, val);
274 1.12 christos
275 1.12 christos return;
276 1.12 christos }
277 1.12 christos
278 1.12 christos static void
279 1.12 christos alc_mii_writereg_813x(struct alc_softc *sc, int phy, int reg, int val)
280 1.12 christos {
281 1.1 jmcneill uint32_t v;
282 1.1 jmcneill int i;
283 1.1 jmcneill
284 1.1 jmcneill CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
285 1.1 jmcneill (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
286 1.1 jmcneill MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
287 1.1 jmcneill for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
288 1.1 jmcneill DELAY(5);
289 1.1 jmcneill v = CSR_READ_4(sc, ALC_MDIO);
290 1.1 jmcneill if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
291 1.1 jmcneill break;
292 1.1 jmcneill }
293 1.1 jmcneill
294 1.1 jmcneill if (i == 0)
295 1.1 jmcneill printf("%s: phy write timeout: phy %d, reg %d\n",
296 1.1 jmcneill device_xname(sc->sc_dev), phy, reg);
297 1.12 christos
298 1.12 christos return;
299 1.12 christos }
300 1.12 christos
301 1.12 christos static void
302 1.12 christos alc_mii_writereg_816x(struct alc_softc *sc, int phy, int reg, int val)
303 1.12 christos {
304 1.12 christos uint32_t clk, v;
305 1.12 christos int i;
306 1.12 christos
307 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
308 1.12 christos clk = MDIO_CLK_25_128;
309 1.12 christos else
310 1.12 christos clk = MDIO_CLK_25_4;
311 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
312 1.12 christos ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) | MDIO_REG_ADDR(reg) |
313 1.12 christos MDIO_SUP_PREAMBLE | clk);
314 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
315 1.12 christos DELAY(5);
316 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
317 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
318 1.12 christos break;
319 1.12 christos }
320 1.12 christos
321 1.12 christos if (i == 0)
322 1.12 christos printf("%s: phy write timeout: phy %d, reg %d\n",
323 1.12 christos device_xname(sc->sc_dev), phy, reg);
324 1.12 christos
325 1.12 christos return;
326 1.1 jmcneill }
327 1.1 jmcneill
328 1.1 jmcneill static void
329 1.6 matt alc_miibus_statchg(struct ifnet *ifp)
330 1.1 jmcneill {
331 1.6 matt struct alc_softc *sc = ifp->if_softc;
332 1.6 matt struct mii_data *mii = &sc->sc_miibus;
333 1.1 jmcneill uint32_t reg;
334 1.1 jmcneill
335 1.1 jmcneill if ((ifp->if_flags & IFF_RUNNING) == 0)
336 1.1 jmcneill return;
337 1.1 jmcneill
338 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
339 1.1 jmcneill if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
340 1.1 jmcneill (IFM_ACTIVE | IFM_AVALID)) {
341 1.1 jmcneill switch (IFM_SUBTYPE(mii->mii_media_active)) {
342 1.1 jmcneill case IFM_10_T:
343 1.1 jmcneill case IFM_100_TX:
344 1.1 jmcneill sc->alc_flags |= ALC_FLAG_LINK;
345 1.1 jmcneill break;
346 1.1 jmcneill case IFM_1000_T:
347 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
348 1.1 jmcneill sc->alc_flags |= ALC_FLAG_LINK;
349 1.1 jmcneill break;
350 1.1 jmcneill default:
351 1.1 jmcneill break;
352 1.1 jmcneill }
353 1.1 jmcneill }
354 1.1 jmcneill /* Stop Rx/Tx MACs. */
355 1.1 jmcneill alc_stop_mac(sc);
356 1.1 jmcneill
357 1.1 jmcneill /* Program MACs with resolved speed/duplex/flow-control. */
358 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
359 1.1 jmcneill alc_start_queue(sc);
360 1.1 jmcneill alc_mac_config(sc);
361 1.1 jmcneill /* Re-enable Tx/Rx MACs. */
362 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
363 1.1 jmcneill reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
364 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
365 1.1 jmcneill }
366 1.12 christos alc_aspm(sc, 0, IFM_SUBTYPE(mii->mii_media_active));
367 1.12 christos alc_dsp_fixup(sc, IFM_SUBTYPE(mii->mii_media_active));
368 1.12 christos }
369 1.12 christos
370 1.12 christos static uint32_t
371 1.12 christos alc_miidbg_readreg(struct alc_softc *sc, int reg)
372 1.12 christos {
373 1.12 christos
374 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
375 1.12 christos reg);
376 1.12 christos return (alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
377 1.12 christos ALC_MII_DBG_DATA));
378 1.1 jmcneill }
379 1.1 jmcneill
380 1.1 jmcneill static void
381 1.12 christos alc_miidbg_writereg(struct alc_softc *sc, int reg, int val)
382 1.12 christos {
383 1.12 christos
384 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
385 1.12 christos reg);
386 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA, val);
387 1.12 christos
388 1.12 christos return;
389 1.12 christos }
390 1.12 christos
391 1.12 christos static uint32_t
392 1.12 christos alc_miiext_readreg(struct alc_softc *sc, int devaddr, int reg)
393 1.12 christos {
394 1.12 christos uint32_t clk, v;
395 1.12 christos int i;
396 1.12 christos
397 1.12 christos CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
398 1.12 christos EXT_MDIO_DEVADDR(devaddr));
399 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
400 1.12 christos clk = MDIO_CLK_25_128;
401 1.12 christos else
402 1.12 christos clk = MDIO_CLK_25_4;
403 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
404 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
405 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
406 1.12 christos DELAY(5);
407 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
408 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
409 1.12 christos break;
410 1.12 christos }
411 1.12 christos
412 1.12 christos if (i == 0) {
413 1.12 christos printf("%s: phy ext read timeout: %d\n",
414 1.12 christos device_xname(sc->sc_dev), reg);
415 1.12 christos return (0);
416 1.12 christos }
417 1.12 christos
418 1.12 christos return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
419 1.12 christos }
420 1.12 christos
421 1.12 christos static uint32_t
422 1.12 christos alc_miiext_writereg(struct alc_softc *sc, int devaddr, int reg, int val)
423 1.12 christos {
424 1.12 christos uint32_t clk, v;
425 1.12 christos int i;
426 1.12 christos
427 1.12 christos CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
428 1.12 christos EXT_MDIO_DEVADDR(devaddr));
429 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
430 1.12 christos clk = MDIO_CLK_25_128;
431 1.12 christos else
432 1.12 christos clk = MDIO_CLK_25_4;
433 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
434 1.12 christos ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) |
435 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
436 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
437 1.12 christos DELAY(5);
438 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
439 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
440 1.12 christos break;
441 1.12 christos }
442 1.12 christos
443 1.12 christos if (i == 0) {
444 1.12 christos printf("%s: phy ext write timeout: reg %d\n",
445 1.12 christos device_xname(sc->sc_dev), reg);
446 1.12 christos return (0);
447 1.12 christos }
448 1.12 christos
449 1.12 christos return (0);
450 1.12 christos }
451 1.12 christos
452 1.12 christos static void
453 1.12 christos alc_dsp_fixup(struct alc_softc *sc, int media)
454 1.12 christos {
455 1.12 christos uint16_t agc, len, val;
456 1.12 christos
457 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
458 1.12 christos return;
459 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_C0)
460 1.12 christos return;
461 1.12 christos
462 1.12 christos /*
463 1.12 christos * Vendor PHY magic.
464 1.12 christos * 1000BT/AZ, wrong cable length
465 1.12 christos */
466 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
467 1.12 christos len = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL6);
468 1.12 christos len = (len >> EXT_CLDCTL6_CAB_LEN_SHIFT) &
469 1.12 christos EXT_CLDCTL6_CAB_LEN_MASK;
470 1.13 christos /* XXX: used to be (alc >> shift) & mask which is 0 */
471 1.13 christos agc = alc_miidbg_readreg(sc, MII_DBG_AGC) & DBG_AGC_2_VGA_MASK;
472 1.13 christos agc >>= DBG_AGC_2_VGA_SHIFT;
473 1.12 christos if ((media == IFM_1000_T && len > EXT_CLDCTL6_CAB_LEN_SHORT1G &&
474 1.12 christos agc > DBG_AGC_LONG1G_LIMT) ||
475 1.12 christos (media == IFM_100_TX && len > DBG_AGC_LONG100M_LIMT &&
476 1.12 christos agc > DBG_AGC_LONG1G_LIMT)) {
477 1.12 christos alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
478 1.12 christos DBG_AZ_ANADECT_LONG);
479 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG,
480 1.12 christos MII_EXT_ANEG_AFE);
481 1.12 christos val |= ANEG_AFEE_10BT_100M_TH;
482 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
483 1.12 christos val);
484 1.12 christos } else {
485 1.12 christos alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
486 1.12 christos DBG_AZ_ANADECT_DEFAULT);
487 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG,
488 1.12 christos MII_EXT_ANEG_AFE);
489 1.12 christos val &= ~ANEG_AFEE_10BT_100M_TH;
490 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
491 1.12 christos val);
492 1.12 christos }
493 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
494 1.12 christos AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
495 1.12 christos if (media == IFM_1000_T) {
496 1.12 christos /*
497 1.12 christos * Giga link threshold, raise the tolerance of
498 1.12 christos * noise 50%.
499 1.12 christos */
500 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
501 1.12 christos val &= ~DBG_MSE20DB_TH_MASK;
502 1.12 christos val |= (DBG_MSE20DB_TH_HI <<
503 1.12 christos DBG_MSE20DB_TH_SHIFT);
504 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
505 1.12 christos } else if (media == IFM_100_TX)
506 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
507 1.12 christos DBG_MSE16DB_UP);
508 1.12 christos }
509 1.12 christos } else {
510 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE);
511 1.12 christos val &= ~ANEG_AFEE_10BT_100M_TH;
512 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, val);
513 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
514 1.12 christos AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
515 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
516 1.12 christos DBG_MSE16DB_DOWN);
517 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
518 1.12 christos val &= ~DBG_MSE20DB_TH_MASK;
519 1.12 christos val |= (DBG_MSE20DB_TH_DEFAULT << DBG_MSE20DB_TH_SHIFT);
520 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
521 1.12 christos }
522 1.12 christos }
523 1.12 christos }
524 1.12 christos
525 1.12 christos static void
526 1.1 jmcneill alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
527 1.1 jmcneill {
528 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
529 1.1 jmcneill struct mii_data *mii = &sc->sc_miibus;
530 1.1 jmcneill
531 1.15 leot if ((ifp->if_flags & IFF_UP) == 0)
532 1.15 leot return;
533 1.15 leot
534 1.1 jmcneill mii_pollstat(mii);
535 1.1 jmcneill ifmr->ifm_status = mii->mii_media_status;
536 1.1 jmcneill ifmr->ifm_active = mii->mii_media_active;
537 1.1 jmcneill }
538 1.1 jmcneill
539 1.1 jmcneill static int
540 1.1 jmcneill alc_mediachange(struct ifnet *ifp)
541 1.1 jmcneill {
542 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
543 1.1 jmcneill struct mii_data *mii = &sc->sc_miibus;
544 1.1 jmcneill int error;
545 1.1 jmcneill
546 1.1 jmcneill if (mii->mii_instance != 0) {
547 1.1 jmcneill struct mii_softc *miisc;
548 1.1 jmcneill
549 1.1 jmcneill LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
550 1.1 jmcneill mii_phy_reset(miisc);
551 1.1 jmcneill }
552 1.1 jmcneill error = mii_mediachg(mii);
553 1.1 jmcneill
554 1.1 jmcneill return (error);
555 1.1 jmcneill }
556 1.1 jmcneill
557 1.2 jmcneill static struct alc_ident *
558 1.2 jmcneill alc_find_ident(struct pci_attach_args *pa)
559 1.2 jmcneill {
560 1.2 jmcneill struct alc_ident *ident;
561 1.2 jmcneill uint16_t vendor, devid;
562 1.2 jmcneill
563 1.2 jmcneill vendor = PCI_VENDOR(pa->pa_id);
564 1.2 jmcneill devid = PCI_PRODUCT(pa->pa_id);
565 1.2 jmcneill for (ident = alc_ident_table; ident->name != NULL; ident++) {
566 1.2 jmcneill if (vendor == ident->vendorid && devid == ident->deviceid)
567 1.2 jmcneill return (ident);
568 1.2 jmcneill }
569 1.2 jmcneill
570 1.2 jmcneill return (NULL);
571 1.2 jmcneill }
572 1.2 jmcneill
573 1.1 jmcneill static int
574 1.1 jmcneill alc_match(device_t dev, cfdata_t match, void *aux)
575 1.1 jmcneill {
576 1.1 jmcneill struct pci_attach_args *pa = aux;
577 1.1 jmcneill
578 1.2 jmcneill return alc_find_ident(pa) != NULL;
579 1.1 jmcneill }
580 1.1 jmcneill
581 1.1 jmcneill static void
582 1.1 jmcneill alc_get_macaddr(struct alc_softc *sc)
583 1.1 jmcneill {
584 1.12 christos
585 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
586 1.12 christos alc_get_macaddr_816x(sc);
587 1.12 christos else
588 1.12 christos alc_get_macaddr_813x(sc);
589 1.12 christos }
590 1.12 christos
591 1.12 christos static void
592 1.12 christos alc_get_macaddr_813x(struct alc_softc *sc)
593 1.12 christos {
594 1.12 christos uint32_t opt;
595 1.2 jmcneill uint16_t val;
596 1.2 jmcneill int eeprom, i;
597 1.1 jmcneill
598 1.2 jmcneill eeprom = 0;
599 1.1 jmcneill opt = CSR_READ_4(sc, ALC_OPT_CFG);
600 1.2 jmcneill if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_OTP_SEL) != 0 &&
601 1.2 jmcneill (CSR_READ_4(sc, ALC_TWSI_DEBUG) & TWSI_DEBUG_DEV_EXIST) != 0) {
602 1.1 jmcneill /*
603 1.1 jmcneill * EEPROM found, let TWSI reload EEPROM configuration.
604 1.1 jmcneill * This will set ethernet address of controller.
605 1.1 jmcneill */
606 1.2 jmcneill eeprom++;
607 1.2 jmcneill switch (sc->alc_ident->deviceid) {
608 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8131:
609 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8132:
610 1.2 jmcneill if ((opt & OPT_CFG_CLK_ENB) == 0) {
611 1.2 jmcneill opt |= OPT_CFG_CLK_ENB;
612 1.2 jmcneill CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
613 1.2 jmcneill CSR_READ_4(sc, ALC_OPT_CFG);
614 1.2 jmcneill DELAY(1000);
615 1.2 jmcneill }
616 1.2 jmcneill break;
617 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151:
618 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
619 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B:
620 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B2:
621 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
622 1.2 jmcneill ALC_MII_DBG_ADDR, 0x00);
623 1.2 jmcneill val = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
624 1.2 jmcneill ALC_MII_DBG_DATA);
625 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
626 1.2 jmcneill ALC_MII_DBG_DATA, val & 0xFF7F);
627 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
628 1.2 jmcneill ALC_MII_DBG_ADDR, 0x3B);
629 1.2 jmcneill val = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
630 1.2 jmcneill ALC_MII_DBG_DATA);
631 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
632 1.2 jmcneill ALC_MII_DBG_DATA, val | 0x0008);
633 1.2 jmcneill DELAY(20);
634 1.2 jmcneill break;
635 1.1 jmcneill }
636 1.2 jmcneill
637 1.2 jmcneill CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
638 1.2 jmcneill CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
639 1.2 jmcneill CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
640 1.2 jmcneill CSR_READ_4(sc, ALC_WOL_CFG);
641 1.2 jmcneill
642 1.1 jmcneill CSR_WRITE_4(sc, ALC_TWSI_CFG, CSR_READ_4(sc, ALC_TWSI_CFG) |
643 1.1 jmcneill TWSI_CFG_SW_LD_START);
644 1.1 jmcneill for (i = 100; i > 0; i--) {
645 1.1 jmcneill DELAY(1000);
646 1.1 jmcneill if ((CSR_READ_4(sc, ALC_TWSI_CFG) &
647 1.1 jmcneill TWSI_CFG_SW_LD_START) == 0)
648 1.1 jmcneill break;
649 1.1 jmcneill }
650 1.1 jmcneill if (i == 0)
651 1.8 christos printf("%s: reloading EEPROM timeout!\n",
652 1.1 jmcneill device_xname(sc->sc_dev));
653 1.1 jmcneill } else {
654 1.1 jmcneill if (alcdebug)
655 1.1 jmcneill printf("%s: EEPROM not found!\n", device_xname(sc->sc_dev));
656 1.1 jmcneill }
657 1.2 jmcneill if (eeprom != 0) {
658 1.2 jmcneill switch (sc->alc_ident->deviceid) {
659 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8131:
660 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8132:
661 1.2 jmcneill if ((opt & OPT_CFG_CLK_ENB) != 0) {
662 1.2 jmcneill opt &= ~OPT_CFG_CLK_ENB;
663 1.2 jmcneill CSR_WRITE_4(sc, ALC_OPT_CFG, opt);
664 1.2 jmcneill CSR_READ_4(sc, ALC_OPT_CFG);
665 1.2 jmcneill DELAY(1000);
666 1.2 jmcneill }
667 1.2 jmcneill break;
668 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151:
669 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
670 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B:
671 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B2:
672 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
673 1.2 jmcneill ALC_MII_DBG_ADDR, 0x00);
674 1.2 jmcneill val = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
675 1.2 jmcneill ALC_MII_DBG_DATA);
676 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
677 1.2 jmcneill ALC_MII_DBG_DATA, val | 0x0080);
678 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
679 1.2 jmcneill ALC_MII_DBG_ADDR, 0x3B);
680 1.2 jmcneill val = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
681 1.2 jmcneill ALC_MII_DBG_DATA);
682 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
683 1.2 jmcneill ALC_MII_DBG_DATA, val & 0xFFF7);
684 1.2 jmcneill DELAY(20);
685 1.2 jmcneill break;
686 1.2 jmcneill }
687 1.1 jmcneill }
688 1.1 jmcneill
689 1.12 christos alc_get_macaddr_par(sc);
690 1.12 christos }
691 1.12 christos
692 1.12 christos static void
693 1.12 christos alc_get_macaddr_816x(struct alc_softc *sc)
694 1.12 christos {
695 1.12 christos uint32_t reg;
696 1.12 christos int i, reloaded;
697 1.12 christos
698 1.12 christos reloaded = 0;
699 1.12 christos /* Try to reload station address via TWSI. */
700 1.12 christos for (i = 100; i > 0; i--) {
701 1.12 christos reg = CSR_READ_4(sc, ALC_SLD);
702 1.12 christos if ((reg & (SLD_PROGRESS | SLD_START)) == 0)
703 1.12 christos break;
704 1.12 christos DELAY(1000);
705 1.12 christos }
706 1.12 christos if (i != 0) {
707 1.12 christos CSR_WRITE_4(sc, ALC_SLD, reg | SLD_START);
708 1.12 christos for (i = 100; i > 0; i--) {
709 1.12 christos DELAY(1000);
710 1.12 christos reg = CSR_READ_4(sc, ALC_SLD);
711 1.12 christos if ((reg & SLD_START) == 0)
712 1.12 christos break;
713 1.12 christos }
714 1.12 christos if (i != 0)
715 1.12 christos reloaded++;
716 1.12 christos else if (alcdebug)
717 1.12 christos printf("%s: reloading station address via TWSI timed out!\n",
718 1.12 christos device_xname(sc->sc_dev));
719 1.12 christos }
720 1.12 christos
721 1.12 christos /* Try to reload station address from EEPROM or FLASH. */
722 1.12 christos if (reloaded == 0) {
723 1.12 christos reg = CSR_READ_4(sc, ALC_EEPROM_LD);
724 1.12 christos if ((reg & (EEPROM_LD_EEPROM_EXIST |
725 1.12 christos EEPROM_LD_FLASH_EXIST)) != 0) {
726 1.12 christos for (i = 100; i > 0; i--) {
727 1.12 christos reg = CSR_READ_4(sc, ALC_EEPROM_LD);
728 1.12 christos if ((reg & (EEPROM_LD_PROGRESS |
729 1.12 christos EEPROM_LD_START)) == 0)
730 1.12 christos break;
731 1.12 christos DELAY(1000);
732 1.12 christos }
733 1.12 christos if (i != 0) {
734 1.12 christos CSR_WRITE_4(sc, ALC_EEPROM_LD, reg |
735 1.12 christos EEPROM_LD_START);
736 1.12 christos for (i = 100; i > 0; i--) {
737 1.12 christos DELAY(1000);
738 1.12 christos reg = CSR_READ_4(sc, ALC_EEPROM_LD);
739 1.12 christos if ((reg & EEPROM_LD_START) == 0)
740 1.12 christos break;
741 1.12 christos }
742 1.12 christos } else if (alcdebug)
743 1.12 christos printf("%s: reloading EEPROM/FLASH timed out!\n",
744 1.12 christos device_xname(sc->sc_dev));
745 1.12 christos }
746 1.12 christos }
747 1.12 christos
748 1.12 christos alc_get_macaddr_par(sc);
749 1.12 christos }
750 1.12 christos
751 1.12 christos
752 1.12 christos static void
753 1.12 christos alc_get_macaddr_par(struct alc_softc *sc)
754 1.12 christos {
755 1.12 christos uint32_t ea[2];
756 1.12 christos
757 1.1 jmcneill ea[0] = CSR_READ_4(sc, ALC_PAR0);
758 1.1 jmcneill ea[1] = CSR_READ_4(sc, ALC_PAR1);
759 1.1 jmcneill sc->alc_eaddr[0] = (ea[1] >> 8) & 0xFF;
760 1.1 jmcneill sc->alc_eaddr[1] = (ea[1] >> 0) & 0xFF;
761 1.1 jmcneill sc->alc_eaddr[2] = (ea[0] >> 24) & 0xFF;
762 1.1 jmcneill sc->alc_eaddr[3] = (ea[0] >> 16) & 0xFF;
763 1.1 jmcneill sc->alc_eaddr[4] = (ea[0] >> 8) & 0xFF;
764 1.1 jmcneill sc->alc_eaddr[5] = (ea[0] >> 0) & 0xFF;
765 1.1 jmcneill }
766 1.1 jmcneill
767 1.1 jmcneill static void
768 1.1 jmcneill alc_disable_l0s_l1(struct alc_softc *sc)
769 1.1 jmcneill {
770 1.1 jmcneill uint32_t pmcfg;
771 1.1 jmcneill
772 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
773 1.12 christos /* Another magic from vendor. */
774 1.12 christos pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
775 1.12 christos pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_CLK_SWH_L1 |
776 1.12 christos PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
777 1.12 christos PM_CFG_MAC_ASPM_CHK | PM_CFG_SERDES_PD_EX_L1);
778 1.12 christos pmcfg |= PM_CFG_SERDES_BUDS_RX_L1_ENB |
779 1.12 christos PM_CFG_SERDES_PLL_L1_ENB | PM_CFG_SERDES_L1_ENB;
780 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
781 1.12 christos }
782 1.1 jmcneill }
783 1.1 jmcneill
784 1.1 jmcneill static void
785 1.1 jmcneill alc_phy_reset(struct alc_softc *sc)
786 1.1 jmcneill {
787 1.12 christos
788 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
789 1.12 christos alc_phy_reset_816x(sc);
790 1.12 christos else
791 1.12 christos alc_phy_reset_813x(sc);
792 1.12 christos }
793 1.12 christos
794 1.12 christos static void
795 1.12 christos alc_phy_reset_813x(struct alc_softc *sc)
796 1.12 christos {
797 1.1 jmcneill uint16_t data;
798 1.1 jmcneill
799 1.1 jmcneill /* Reset magic from Linux. */
800 1.12 christos CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_SEL_ANA_RESET);
801 1.1 jmcneill CSR_READ_2(sc, ALC_GPHY_CFG);
802 1.1 jmcneill DELAY(10 * 1000);
803 1.1 jmcneill
804 1.12 christos CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
805 1.1 jmcneill GPHY_CFG_SEL_ANA_RESET);
806 1.1 jmcneill CSR_READ_2(sc, ALC_GPHY_CFG);
807 1.1 jmcneill DELAY(10 * 1000);
808 1.1 jmcneill
809 1.2 jmcneill /* DSP fixup, Vendor magic. */
810 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B) {
811 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
812 1.2 jmcneill ALC_MII_DBG_ADDR, 0x000A);
813 1.2 jmcneill data = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
814 1.2 jmcneill ALC_MII_DBG_DATA);
815 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
816 1.2 jmcneill ALC_MII_DBG_DATA, data & 0xDFFF);
817 1.2 jmcneill }
818 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
819 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
820 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
821 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2) {
822 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
823 1.2 jmcneill ALC_MII_DBG_ADDR, 0x003B);
824 1.2 jmcneill data = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
825 1.2 jmcneill ALC_MII_DBG_DATA);
826 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
827 1.2 jmcneill ALC_MII_DBG_DATA, data & 0xFFF7);
828 1.2 jmcneill DELAY(20 * 1000);
829 1.2 jmcneill }
830 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151) {
831 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
832 1.2 jmcneill ALC_MII_DBG_ADDR, 0x0029);
833 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
834 1.2 jmcneill ALC_MII_DBG_DATA, 0x929D);
835 1.2 jmcneill }
836 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8131 ||
837 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8132 ||
838 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
839 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2) {
840 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
841 1.2 jmcneill ALC_MII_DBG_ADDR, 0x0029);
842 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
843 1.2 jmcneill ALC_MII_DBG_DATA, 0xB6DD);
844 1.2 jmcneill }
845 1.2 jmcneill
846 1.1 jmcneill /* Load DSP codes, vendor magic. */
847 1.1 jmcneill data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
848 1.1 jmcneill ((1 << ANA_INTERVAL_SEL_TIMER_SHIFT) & ANA_INTERVAL_SEL_TIMER_MASK);
849 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
850 1.1 jmcneill ALC_MII_DBG_ADDR, MII_ANA_CFG18);
851 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
852 1.1 jmcneill ALC_MII_DBG_DATA, data);
853 1.1 jmcneill
854 1.1 jmcneill data = ((2 << ANA_SERDES_CDR_BW_SHIFT) & ANA_SERDES_CDR_BW_MASK) |
855 1.1 jmcneill ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
856 1.1 jmcneill ANA_SERDES_EN_LCKDT;
857 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
858 1.1 jmcneill ALC_MII_DBG_ADDR, MII_ANA_CFG5);
859 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
860 1.1 jmcneill ALC_MII_DBG_DATA, data);
861 1.1 jmcneill
862 1.1 jmcneill data = ((44 << ANA_LONG_CABLE_TH_100_SHIFT) &
863 1.1 jmcneill ANA_LONG_CABLE_TH_100_MASK) |
864 1.1 jmcneill ((33 << ANA_SHORT_CABLE_TH_100_SHIFT) &
865 1.1 jmcneill ANA_SHORT_CABLE_TH_100_SHIFT) |
866 1.1 jmcneill ANA_BP_BAD_LINK_ACCUM | ANA_BP_SMALL_BW;
867 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
868 1.1 jmcneill ALC_MII_DBG_ADDR, MII_ANA_CFG54);
869 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
870 1.1 jmcneill ALC_MII_DBG_DATA, data);
871 1.1 jmcneill
872 1.1 jmcneill data = ((11 << ANA_IECHO_ADJ_3_SHIFT) & ANA_IECHO_ADJ_3_MASK) |
873 1.1 jmcneill ((11 << ANA_IECHO_ADJ_2_SHIFT) & ANA_IECHO_ADJ_2_MASK) |
874 1.1 jmcneill ((8 << ANA_IECHO_ADJ_1_SHIFT) & ANA_IECHO_ADJ_1_MASK) |
875 1.1 jmcneill ((8 << ANA_IECHO_ADJ_0_SHIFT) & ANA_IECHO_ADJ_0_MASK);
876 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
877 1.1 jmcneill ALC_MII_DBG_ADDR, MII_ANA_CFG4);
878 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
879 1.1 jmcneill ALC_MII_DBG_DATA, data);
880 1.1 jmcneill
881 1.1 jmcneill data = ((7 & ANA_MANUL_SWICH_ON_SHIFT) & ANA_MANUL_SWICH_ON_MASK) |
882 1.1 jmcneill ANA_RESTART_CAL | ANA_MAN_ENABLE | ANA_SEL_HSP | ANA_EN_HB |
883 1.1 jmcneill ANA_OEN_125M;
884 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
885 1.1 jmcneill ALC_MII_DBG_ADDR, MII_ANA_CFG0);
886 1.1 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
887 1.1 jmcneill ALC_MII_DBG_DATA, data);
888 1.1 jmcneill DELAY(1000);
889 1.12 christos
890 1.12 christos /* Disable hibernation. */
891 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
892 1.12 christos 0x0029);
893 1.12 christos data = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
894 1.12 christos ALC_MII_DBG_DATA);
895 1.12 christos data &= ~0x8000;
896 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
897 1.12 christos data);
898 1.12 christos
899 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
900 1.12 christos 0x000B);
901 1.12 christos data = alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
902 1.12 christos ALC_MII_DBG_DATA);
903 1.12 christos data &= ~0x8000;
904 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA,
905 1.12 christos data);
906 1.12 christos }
907 1.12 christos
908 1.12 christos static void
909 1.12 christos alc_phy_reset_816x(struct alc_softc *sc)
910 1.12 christos {
911 1.12 christos uint32_t val;
912 1.12 christos
913 1.12 christos val = CSR_READ_4(sc, ALC_GPHY_CFG);
914 1.12 christos val &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
915 1.12 christos GPHY_CFG_GATE_25M_ENB | GPHY_CFG_PHY_IDDQ | GPHY_CFG_PHY_PLL_ON |
916 1.12 christos GPHY_CFG_PWDOWN_HW | GPHY_CFG_100AB_ENB);
917 1.12 christos val |= GPHY_CFG_SEL_ANA_RESET;
918 1.12 christos #ifdef notyet
919 1.12 christos val |= GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN | GPHY_CFG_SEL_ANA_RESET;
920 1.12 christos #else
921 1.12 christos /* Disable PHY hibernation. */
922 1.12 christos val &= ~(GPHY_CFG_HIB_PULSE | GPHY_CFG_HIB_EN);
923 1.12 christos #endif
924 1.12 christos CSR_WRITE_4(sc, ALC_GPHY_CFG, val);
925 1.12 christos DELAY(10);
926 1.12 christos CSR_WRITE_4(sc, ALC_GPHY_CFG, val | GPHY_CFG_EXT_RESET);
927 1.12 christos DELAY(800);
928 1.12 christos
929 1.12 christos /* Vendor PHY magic. */
930 1.12 christos #ifdef notyet
931 1.12 christos alc_miidbg_writereg(sc, MII_DBG_LEGCYPS, DBG_LEGCYPS_DEFAULT);
932 1.12 christos alc_miidbg_writereg(sc, MII_DBG_SYSMODCTL, DBG_SYSMODCTL_DEFAULT);
933 1.12 christos alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_VDRVBIAS,
934 1.12 christos EXT_VDRVBIAS_DEFAULT);
935 1.12 christos #else
936 1.12 christos /* Disable PHY hibernation. */
937 1.12 christos alc_miidbg_writereg(sc, MII_DBG_LEGCYPS,
938 1.12 christos DBG_LEGCYPS_DEFAULT & ~DBG_LEGCYPS_ENB);
939 1.12 christos alc_miidbg_writereg(sc, MII_DBG_HIBNEG,
940 1.12 christos DBG_HIBNEG_DEFAULT & ~(DBG_HIBNEG_PSHIB_EN | DBG_HIBNEG_HIB_PULSE));
941 1.12 christos alc_miidbg_writereg(sc, MII_DBG_GREENCFG, DBG_GREENCFG_DEFAULT);
942 1.12 christos #endif
943 1.12 christos
944 1.12 christos /* XXX Disable EEE. */
945 1.12 christos val = CSR_READ_4(sc, ALC_LPI_CTL);
946 1.12 christos val &= ~LPI_CTL_ENB;
947 1.12 christos CSR_WRITE_4(sc, ALC_LPI_CTL, val);
948 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_LOCAL_EEEADV, 0);
949 1.12 christos
950 1.12 christos /* PHY power saving. */
951 1.12 christos alc_miidbg_writereg(sc, MII_DBG_TST10BTCFG, DBG_TST10BTCFG_DEFAULT);
952 1.12 christos alc_miidbg_writereg(sc, MII_DBG_SRDSYSMOD, DBG_SRDSYSMOD_DEFAULT);
953 1.12 christos alc_miidbg_writereg(sc, MII_DBG_TST100BTCFG, DBG_TST100BTCFG_DEFAULT);
954 1.12 christos alc_miidbg_writereg(sc, MII_DBG_ANACTL, DBG_ANACTL_DEFAULT);
955 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
956 1.12 christos val &= ~DBG_GREENCFG2_GATE_DFSE_EN;
957 1.12 christos alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
958 1.12 christos
959 1.12 christos /* RTL8139C, 120m issue. */
960 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_NLP78,
961 1.12 christos ANEG_NLP78_120M_DEFAULT);
962 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_S3DIG10,
963 1.12 christos ANEG_S3DIG10_DEFAULT);
964 1.12 christos
965 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0) {
966 1.12 christos /* Turn off half amplitude. */
967 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3);
968 1.12 christos val |= EXT_CLDCTL3_BP_CABLE1TH_DET_GT;
969 1.12 christos alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL3, val);
970 1.12 christos /* Turn off Green feature. */
971 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_GREENCFG2);
972 1.12 christos val |= DBG_GREENCFG2_BP_GREEN;
973 1.12 christos alc_miidbg_writereg(sc, MII_DBG_GREENCFG2, val);
974 1.12 christos /* Turn off half bias. */
975 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5);
976 1.12 christos val |= EXT_CLDCTL5_BP_VD_HLFBIAS;
977 1.12 christos alc_miiext_writereg(sc, MII_EXT_PCS, MII_EXT_CLDCTL5, val);
978 1.12 christos }
979 1.1 jmcneill }
980 1.1 jmcneill
981 1.1 jmcneill static void
982 1.1 jmcneill alc_phy_down(struct alc_softc *sc)
983 1.1 jmcneill {
984 1.12 christos uint32_t gphy;
985 1.12 christos
986 1.2 jmcneill switch (sc->alc_ident->deviceid) {
987 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8161:
988 1.12 christos case PCI_PRODUCT_ATTANSIC_E2200:
989 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8162:
990 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8171:
991 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8172:
992 1.12 christos gphy = CSR_READ_4(sc, ALC_GPHY_CFG);
993 1.12 christos gphy &= ~(GPHY_CFG_EXT_RESET | GPHY_CFG_LED_MODE |
994 1.12 christos GPHY_CFG_100AB_ENB | GPHY_CFG_PHY_PLL_ON);
995 1.12 christos gphy |= GPHY_CFG_HIB_EN | GPHY_CFG_HIB_PULSE |
996 1.12 christos GPHY_CFG_SEL_ANA_RESET;
997 1.12 christos gphy |= GPHY_CFG_PHY_IDDQ | GPHY_CFG_PWDOWN_HW;
998 1.12 christos CSR_WRITE_4(sc, ALC_GPHY_CFG, gphy);
999 1.12 christos break;
1000 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151:
1001 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
1002 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8152_B:
1003 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8152_B2:
1004 1.2 jmcneill /*
1005 1.2 jmcneill * GPHY power down caused more problems on AR8151 v2.0.
1006 1.2 jmcneill * When driver is reloaded after GPHY power down,
1007 1.2 jmcneill * accesses to PHY/MAC registers hung the system. Only
1008 1.2 jmcneill * cold boot recovered from it. I'm not sure whether
1009 1.2 jmcneill * AR8151 v1.0 also requires this one though. I don't
1010 1.2 jmcneill * have AR8151 v1.0 controller in hand.
1011 1.2 jmcneill * The only option left is to isolate the PHY and
1012 1.2 jmcneill * initiates power down the PHY which in turn saves
1013 1.2 jmcneill * more power when driver is unloaded.
1014 1.2 jmcneill */
1015 1.2 jmcneill alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr,
1016 1.2 jmcneill MII_BMCR, BMCR_ISO | BMCR_PDOWN);
1017 1.2 jmcneill break;
1018 1.2 jmcneill default:
1019 1.2 jmcneill /* Force PHY down. */
1020 1.12 christos CSR_WRITE_2(sc, ALC_GPHY_CFG, GPHY_CFG_EXT_RESET |
1021 1.2 jmcneill GPHY_CFG_SEL_ANA_RESET | GPHY_CFG_PHY_IDDQ |
1022 1.2 jmcneill GPHY_CFG_PWDOWN_HW);
1023 1.2 jmcneill DELAY(1000);
1024 1.2 jmcneill break;
1025 1.2 jmcneill }
1026 1.1 jmcneill }
1027 1.1 jmcneill
1028 1.1 jmcneill static void
1029 1.12 christos alc_aspm(struct alc_softc *sc, int init, int media)
1030 1.12 christos {
1031 1.12 christos
1032 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
1033 1.12 christos alc_aspm_816x(sc, init);
1034 1.12 christos else
1035 1.12 christos alc_aspm_813x(sc, media);
1036 1.12 christos }
1037 1.12 christos
1038 1.12 christos static void
1039 1.12 christos alc_aspm_813x(struct alc_softc *sc, int media)
1040 1.1 jmcneill {
1041 1.1 jmcneill uint32_t pmcfg;
1042 1.2 jmcneill uint16_t linkcfg;
1043 1.8 christos
1044 1.1 jmcneill pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
1045 1.2 jmcneill if ((sc->alc_flags & (ALC_FLAG_APS | ALC_FLAG_PCIE)) ==
1046 1.2 jmcneill (ALC_FLAG_APS | ALC_FLAG_PCIE))
1047 1.2 jmcneill linkcfg = CSR_READ_2(sc, sc->alc_expcap +
1048 1.9 msaitoh PCIE_LCSR);
1049 1.2 jmcneill else
1050 1.2 jmcneill linkcfg = 0;
1051 1.1 jmcneill pmcfg &= ~PM_CFG_SERDES_PD_EX_L1;
1052 1.2 jmcneill pmcfg &= ~(PM_CFG_L1_ENTRY_TIMER_MASK | PM_CFG_LCKDET_TIMER_MASK);
1053 1.1 jmcneill pmcfg |= PM_CFG_MAC_ASPM_CHK;
1054 1.2 jmcneill pmcfg |= (PM_CFG_LCKDET_TIMER_DEFAULT << PM_CFG_LCKDET_TIMER_SHIFT);
1055 1.2 jmcneill pmcfg &= ~(PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
1056 1.2 jmcneill
1057 1.2 jmcneill if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
1058 1.2 jmcneill /* Disable extended sync except AR8152 B v1.0 */
1059 1.2 jmcneill linkcfg &= ~0x80;
1060 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B &&
1061 1.2 jmcneill sc->alc_rev == ATHEROS_AR8152_B_V10)
1062 1.2 jmcneill linkcfg |= 0x80;
1063 1.9 msaitoh CSR_WRITE_2(sc, sc->alc_expcap + PCIE_LCSR,
1064 1.2 jmcneill linkcfg);
1065 1.2 jmcneill pmcfg &= ~(PM_CFG_EN_BUFS_RX_L0S | PM_CFG_SA_DLY_ENB |
1066 1.2 jmcneill PM_CFG_HOTRST);
1067 1.2 jmcneill pmcfg |= (PM_CFG_L1_ENTRY_TIMER_DEFAULT <<
1068 1.2 jmcneill PM_CFG_L1_ENTRY_TIMER_SHIFT);
1069 1.2 jmcneill pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
1070 1.2 jmcneill pmcfg |= (PM_CFG_PM_REQ_TIMER_DEFAULT <<
1071 1.2 jmcneill PM_CFG_PM_REQ_TIMER_SHIFT);
1072 1.2 jmcneill pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_PCIE_RECV;
1073 1.2 jmcneill }
1074 1.2 jmcneill
1075 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
1076 1.2 jmcneill if ((sc->alc_flags & ALC_FLAG_L0S) != 0)
1077 1.2 jmcneill pmcfg |= PM_CFG_ASPM_L0S_ENB;
1078 1.2 jmcneill if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
1079 1.2 jmcneill pmcfg |= PM_CFG_ASPM_L1_ENB;
1080 1.2 jmcneill if ((sc->alc_flags & ALC_FLAG_APS) != 0) {
1081 1.2 jmcneill if (sc->alc_ident->deviceid ==
1082 1.2 jmcneill PCI_PRODUCT_ATTANSIC_AR8152_B)
1083 1.2 jmcneill pmcfg &= ~PM_CFG_ASPM_L0S_ENB;
1084 1.2 jmcneill pmcfg &= ~(PM_CFG_SERDES_L1_ENB |
1085 1.2 jmcneill PM_CFG_SERDES_PLL_L1_ENB |
1086 1.2 jmcneill PM_CFG_SERDES_BUDS_RX_L1_ENB);
1087 1.2 jmcneill pmcfg |= PM_CFG_CLK_SWH_L1;
1088 1.2 jmcneill if (media == IFM_100_TX || media == IFM_1000_T) {
1089 1.2 jmcneill pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_MASK;
1090 1.2 jmcneill switch (sc->alc_ident->deviceid) {
1091 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B:
1092 1.2 jmcneill pmcfg |= (7 <<
1093 1.2 jmcneill PM_CFG_L1_ENTRY_TIMER_SHIFT);
1094 1.2 jmcneill break;
1095 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B2:
1096 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
1097 1.2 jmcneill pmcfg |= (4 <<
1098 1.2 jmcneill PM_CFG_L1_ENTRY_TIMER_SHIFT);
1099 1.2 jmcneill break;
1100 1.2 jmcneill default:
1101 1.2 jmcneill pmcfg |= (15 <<
1102 1.2 jmcneill PM_CFG_L1_ENTRY_TIMER_SHIFT);
1103 1.2 jmcneill break;
1104 1.2 jmcneill }
1105 1.2 jmcneill }
1106 1.2 jmcneill } else {
1107 1.2 jmcneill pmcfg |= PM_CFG_SERDES_L1_ENB |
1108 1.2 jmcneill PM_CFG_SERDES_PLL_L1_ENB |
1109 1.2 jmcneill PM_CFG_SERDES_BUDS_RX_L1_ENB;
1110 1.2 jmcneill pmcfg &= ~(PM_CFG_CLK_SWH_L1 |
1111 1.2 jmcneill PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB);
1112 1.2 jmcneill }
1113 1.1 jmcneill } else {
1114 1.2 jmcneill pmcfg &= ~(PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SERDES_L1_ENB |
1115 1.2 jmcneill PM_CFG_SERDES_PLL_L1_ENB);
1116 1.1 jmcneill pmcfg |= PM_CFG_CLK_SWH_L1;
1117 1.2 jmcneill if ((sc->alc_flags & ALC_FLAG_L1S) != 0)
1118 1.2 jmcneill pmcfg |= PM_CFG_ASPM_L1_ENB;
1119 1.1 jmcneill }
1120 1.1 jmcneill CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1121 1.1 jmcneill }
1122 1.1 jmcneill
1123 1.1 jmcneill static void
1124 1.12 christos alc_aspm_816x(struct alc_softc *sc, int init)
1125 1.12 christos {
1126 1.12 christos uint32_t pmcfg;
1127 1.12 christos
1128 1.12 christos pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
1129 1.12 christos pmcfg &= ~PM_CFG_L1_ENTRY_TIMER_816X_MASK;
1130 1.12 christos pmcfg |= PM_CFG_L1_ENTRY_TIMER_816X_DEFAULT;
1131 1.12 christos pmcfg &= ~PM_CFG_PM_REQ_TIMER_MASK;
1132 1.12 christos pmcfg |= PM_CFG_PM_REQ_TIMER_816X_DEFAULT;
1133 1.12 christos pmcfg &= ~PM_CFG_LCKDET_TIMER_MASK;
1134 1.12 christos pmcfg |= PM_CFG_LCKDET_TIMER_DEFAULT;
1135 1.12 christos pmcfg |= PM_CFG_SERDES_PD_EX_L1 | PM_CFG_CLK_SWH_L1 | PM_CFG_PCIE_RECV;
1136 1.12 christos pmcfg &= ~(PM_CFG_RX_L1_AFTER_L0S | PM_CFG_TX_L1_AFTER_L0S |
1137 1.12 christos PM_CFG_ASPM_L1_ENB | PM_CFG_ASPM_L0S_ENB |
1138 1.12 christos PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB |
1139 1.12 christos PM_CFG_SERDES_BUDS_RX_L1_ENB | PM_CFG_SA_DLY_ENB |
1140 1.12 christos PM_CFG_MAC_ASPM_CHK | PM_CFG_HOTRST);
1141 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1142 1.12 christos (sc->alc_rev & 0x01) != 0)
1143 1.12 christos pmcfg |= PM_CFG_SERDES_L1_ENB | PM_CFG_SERDES_PLL_L1_ENB;
1144 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
1145 1.12 christos /* Link up, enable both L0s, L1s. */
1146 1.12 christos pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1147 1.12 christos PM_CFG_MAC_ASPM_CHK;
1148 1.12 christos } else {
1149 1.12 christos if (init != 0)
1150 1.12 christos pmcfg |= PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB |
1151 1.12 christos PM_CFG_MAC_ASPM_CHK;
1152 1.12 christos else if ((sc->sc_ec.ec_if.if_flags & IFF_RUNNING) != 0)
1153 1.12 christos pmcfg |= PM_CFG_ASPM_L1_ENB | PM_CFG_MAC_ASPM_CHK;
1154 1.12 christos }
1155 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
1156 1.12 christos }
1157 1.12 christos
1158 1.12 christos static void
1159 1.1 jmcneill alc_attach(device_t parent, device_t self, void *aux)
1160 1.1 jmcneill {
1161 1.1 jmcneill
1162 1.1 jmcneill struct alc_softc *sc = device_private(self);
1163 1.1 jmcneill struct pci_attach_args *pa = aux;
1164 1.1 jmcneill pci_chipset_tag_t pc = pa->pa_pc;
1165 1.1 jmcneill pci_intr_handle_t ih;
1166 1.1 jmcneill const char *intrstr;
1167 1.1 jmcneill struct ifnet *ifp;
1168 1.1 jmcneill pcireg_t memtype;
1169 1.2 jmcneill const char *aspm_state[] = { "L0s/L1", "L0s", "L1", "L0s/L1" };
1170 1.1 jmcneill uint16_t burst;
1171 1.1 jmcneill int base, mii_flags, state, error = 0;
1172 1.1 jmcneill uint32_t cap, ctl, val;
1173 1.11 christos char intrbuf[PCI_INTRSTR_LEN];
1174 1.1 jmcneill
1175 1.2 jmcneill sc->alc_ident = alc_find_ident(pa);
1176 1.2 jmcneill
1177 1.1 jmcneill aprint_naive("\n");
1178 1.2 jmcneill aprint_normal(": %s\n", sc->alc_ident->name);
1179 1.1 jmcneill
1180 1.1 jmcneill sc->sc_dev = self;
1181 1.1 jmcneill sc->sc_dmat = pa->pa_dmat;
1182 1.1 jmcneill sc->sc_pct = pa->pa_pc;
1183 1.1 jmcneill sc->sc_pcitag = pa->pa_tag;
1184 1.1 jmcneill
1185 1.1 jmcneill /*
1186 1.1 jmcneill * Allocate IO memory
1187 1.1 jmcneill */
1188 1.1 jmcneill memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ALC_PCIR_BAR);
1189 1.1 jmcneill switch (memtype) {
1190 1.1 jmcneill case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1191 1.1 jmcneill case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M:
1192 1.1 jmcneill case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1193 1.1 jmcneill break;
1194 1.1 jmcneill default:
1195 1.1 jmcneill aprint_error_dev(self, "invalid base address register\n");
1196 1.1 jmcneill break;
1197 1.1 jmcneill }
1198 1.1 jmcneill
1199 1.1 jmcneill if (pci_mapreg_map(pa, ALC_PCIR_BAR, memtype, 0, &sc->sc_mem_bt,
1200 1.1 jmcneill &sc->sc_mem_bh, NULL, &sc->sc_mem_size)) {
1201 1.1 jmcneill aprint_error_dev(self, "could not map mem space\n");
1202 1.1 jmcneill return;
1203 1.1 jmcneill }
1204 1.1 jmcneill
1205 1.1 jmcneill if (pci_intr_map(pa, &ih) != 0) {
1206 1.1 jmcneill printf(": can't map interrupt\n");
1207 1.1 jmcneill goto fail;
1208 1.1 jmcneill }
1209 1.1 jmcneill
1210 1.1 jmcneill /*
1211 1.1 jmcneill * Allocate IRQ
1212 1.1 jmcneill */
1213 1.11 christos intrstr = pci_intr_string(sc->sc_pct, ih, intrbuf, sizeof(intrbuf));
1214 1.1 jmcneill sc->sc_irq_handle = pci_intr_establish(pc, ih, IPL_NET, alc_intr, sc);
1215 1.1 jmcneill if (sc->sc_irq_handle == NULL) {
1216 1.1 jmcneill printf(": could not establish interrupt");
1217 1.1 jmcneill if (intrstr != NULL)
1218 1.1 jmcneill printf(" at %s", intrstr);
1219 1.1 jmcneill printf("\n");
1220 1.1 jmcneill goto fail;
1221 1.1 jmcneill }
1222 1.4 matt aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1223 1.8 christos
1224 1.1 jmcneill /* Set PHY address. */
1225 1.1 jmcneill sc->alc_phyaddr = ALC_PHY_ADDR;
1226 1.1 jmcneill
1227 1.1 jmcneill /* Initialize DMA parameters. */
1228 1.1 jmcneill sc->alc_dma_rd_burst = 0;
1229 1.1 jmcneill sc->alc_dma_wr_burst = 0;
1230 1.1 jmcneill sc->alc_rcb = DMA_CFG_RCB_64;
1231 1.1 jmcneill if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
1232 1.1 jmcneill &base, NULL)) {
1233 1.1 jmcneill sc->alc_flags |= ALC_FLAG_PCIE;
1234 1.2 jmcneill sc->alc_expcap = base;
1235 1.1 jmcneill burst = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1236 1.9 msaitoh base + PCIE_DCSR) >> 16;
1237 1.1 jmcneill sc->alc_dma_rd_burst = (burst & 0x7000) >> 12;
1238 1.1 jmcneill sc->alc_dma_wr_burst = (burst & 0x00e0) >> 5;
1239 1.1 jmcneill if (alcdebug) {
1240 1.1 jmcneill printf("%s: Read request size : %u bytes.\n",
1241 1.8 christos device_xname(sc->sc_dev),
1242 1.1 jmcneill alc_dma_burst[sc->alc_dma_rd_burst]);
1243 1.1 jmcneill printf("%s: TLP payload size : %u bytes.\n",
1244 1.1 jmcneill device_xname(sc->sc_dev),
1245 1.1 jmcneill alc_dma_burst[sc->alc_dma_wr_burst]);
1246 1.1 jmcneill }
1247 1.12 christos if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
1248 1.12 christos sc->alc_dma_rd_burst = 3;
1249 1.12 christos if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
1250 1.12 christos sc->alc_dma_wr_burst = 3;
1251 1.12 christos
1252 1.1 jmcneill /* Clear data link and flow-control protocol error. */
1253 1.1 jmcneill val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
1254 1.1 jmcneill val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
1255 1.1 jmcneill CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
1256 1.12 christos
1257 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1258 1.12 christos CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
1259 1.12 christos CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
1260 1.12 christos CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
1261 1.12 christos CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
1262 1.12 christos PCIE_PHYMISC_FORCE_RCV_DET);
1263 1.12 christos if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B &&
1264 1.12 christos sc->alc_rev == ATHEROS_AR8152_B_V10) {
1265 1.12 christos val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
1266 1.12 christos val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
1267 1.12 christos PCIE_PHYMISC2_SERDES_TH_MASK);
1268 1.12 christos val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
1269 1.12 christos val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
1270 1.12 christos CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
1271 1.12 christos }
1272 1.12 christos /* Disable ASPM L0S and L1. */
1273 1.12 christos cap = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1274 1.12 christos base + PCIE_LCAP) >> 16;
1275 1.12 christos if ((cap & 0x00000c00) != 0) {
1276 1.12 christos ctl = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1277 1.12 christos base + PCIE_LCSR) >> 16;
1278 1.12 christos if ((ctl & 0x08) != 0)
1279 1.12 christos sc->alc_rcb = DMA_CFG_RCB_128;
1280 1.12 christos if (alcdebug)
1281 1.12 christos printf("%s: RCB %u bytes\n",
1282 1.12 christos device_xname(sc->sc_dev),
1283 1.12 christos sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
1284 1.12 christos state = ctl & 0x03;
1285 1.12 christos if (state & 0x01)
1286 1.12 christos sc->alc_flags |= ALC_FLAG_L0S;
1287 1.12 christos if (state & 0x02)
1288 1.12 christos sc->alc_flags |= ALC_FLAG_L1S;
1289 1.12 christos if (alcdebug)
1290 1.12 christos printf("%s: ASPM %s %s\n",
1291 1.12 christos device_xname(sc->sc_dev),
1292 1.12 christos aspm_state[state],
1293 1.12 christos state == 0 ? "disabled" : "enabled");
1294 1.12 christos alc_disable_l0s_l1(sc);
1295 1.12 christos } else {
1296 1.12 christos aprint_debug_dev(sc->sc_dev, "no ASPM support\n");
1297 1.12 christos }
1298 1.2 jmcneill } else {
1299 1.12 christos val = CSR_READ_4(sc, ALC_PDLL_TRNS1);
1300 1.12 christos val &= ~PDLL_TRNS1_D3PLLOFF_ENB;
1301 1.12 christos CSR_WRITE_4(sc, ALC_PDLL_TRNS1, val);
1302 1.12 christos val = CSR_READ_4(sc, ALC_MASTER_CFG);
1303 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1304 1.12 christos (sc->alc_rev & 0x01) != 0) {
1305 1.12 christos if ((val & MASTER_WAKEN_25M) == 0 ||
1306 1.12 christos (val & MASTER_CLK_SEL_DIS) == 0) {
1307 1.12 christos val |= MASTER_WAKEN_25M | MASTER_CLK_SEL_DIS;
1308 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1309 1.12 christos }
1310 1.12 christos } else {
1311 1.12 christos if ((val & MASTER_WAKEN_25M) == 0 ||
1312 1.12 christos (val & MASTER_CLK_SEL_DIS) != 0) {
1313 1.12 christos val |= MASTER_WAKEN_25M;
1314 1.12 christos val &= ~MASTER_CLK_SEL_DIS;
1315 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1316 1.12 christos }
1317 1.12 christos }
1318 1.1 jmcneill }
1319 1.12 christos alc_aspm(sc, 1, IFM_UNKNOWN);
1320 1.1 jmcneill }
1321 1.1 jmcneill
1322 1.1 jmcneill /* Reset PHY. */
1323 1.1 jmcneill alc_phy_reset(sc);
1324 1.1 jmcneill
1325 1.1 jmcneill /* Reset the ethernet controller. */
1326 1.12 christos alc_stop_mac(sc);
1327 1.1 jmcneill alc_reset(sc);
1328 1.1 jmcneill
1329 1.1 jmcneill /*
1330 1.1 jmcneill * One odd thing is AR8132 uses the same PHY hardware(F1
1331 1.1 jmcneill * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
1332 1.1 jmcneill * the PHY supports 1000Mbps but that's not true. The PHY
1333 1.1 jmcneill * used in AR8132 can't establish gigabit link even if it
1334 1.1 jmcneill * shows the same PHY model/revision number of AR8131.
1335 1.1 jmcneill */
1336 1.2 jmcneill switch (sc->alc_ident->deviceid) {
1337 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8161:
1338 1.12 christos if (PCI_SUBSYS_ID(pci_conf_read(
1339 1.12 christos sc->sc_pct, sc->sc_pcitag, PCI_SUBSYS_ID_REG)) == 0x0091 &&
1340 1.12 christos sc->alc_rev == 0)
1341 1.12 christos sc->alc_flags |= ALC_FLAG_LINK_WAR;
1342 1.12 christos /* FALLTHROUGH */
1343 1.12 christos case PCI_PRODUCT_ATTANSIC_E2200:
1344 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8171:
1345 1.12 christos sc->alc_flags |= ALC_FLAG_AR816X_FAMILY;
1346 1.12 christos break;
1347 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8162:
1348 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8172:
1349 1.12 christos sc->alc_flags |= ALC_FLAG_FASTETHER | ALC_FLAG_AR816X_FAMILY;
1350 1.12 christos break;
1351 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B:
1352 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B2:
1353 1.2 jmcneill sc->alc_flags |= ALC_FLAG_APS;
1354 1.2 jmcneill /* FALLTHROUGH */
1355 1.1 jmcneill case PCI_PRODUCT_ATTANSIC_AR8132:
1356 1.2 jmcneill sc->alc_flags |= ALC_FLAG_FASTETHER;
1357 1.1 jmcneill break;
1358 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151:
1359 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
1360 1.2 jmcneill sc->alc_flags |= ALC_FLAG_APS;
1361 1.2 jmcneill /* FALLTHROUGH */
1362 1.1 jmcneill default:
1363 1.1 jmcneill break;
1364 1.1 jmcneill }
1365 1.12 christos sc->alc_flags |= ALC_FLAG_JUMBO;
1366 1.1 jmcneill
1367 1.1 jmcneill /*
1368 1.2 jmcneill * It seems that AR813x/AR815x has silicon bug for SMB. In
1369 1.1 jmcneill * addition, Atheros said that enabling SMB wouldn't improve
1370 1.1 jmcneill * performance. However I think it's bad to access lots of
1371 1.1 jmcneill * registers to extract MAC statistics.
1372 1.1 jmcneill */
1373 1.1 jmcneill sc->alc_flags |= ALC_FLAG_SMB_BUG;
1374 1.1 jmcneill /*
1375 1.1 jmcneill * Don't use Tx CMB. It is known to have silicon bug.
1376 1.1 jmcneill */
1377 1.1 jmcneill sc->alc_flags |= ALC_FLAG_CMB_BUG;
1378 1.1 jmcneill sc->alc_rev = PCI_REVISION(pa->pa_class);
1379 1.1 jmcneill sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
1380 1.1 jmcneill MASTER_CHIP_REV_SHIFT;
1381 1.1 jmcneill if (alcdebug) {
1382 1.1 jmcneill printf("%s: PCI device revision : 0x%04x\n",
1383 1.1 jmcneill device_xname(sc->sc_dev), sc->alc_rev);
1384 1.1 jmcneill printf("%s: Chip id/revision : 0x%04x\n",
1385 1.1 jmcneill device_xname(sc->sc_dev), sc->alc_chip_rev);
1386 1.1 jmcneill printf("%s: %u Tx FIFO, %u Rx FIFO\n", device_xname(sc->sc_dev),
1387 1.1 jmcneill CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
1388 1.1 jmcneill CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
1389 1.1 jmcneill }
1390 1.1 jmcneill
1391 1.1 jmcneill error = alc_dma_alloc(sc);
1392 1.1 jmcneill if (error)
1393 1.1 jmcneill goto fail;
1394 1.1 jmcneill
1395 1.1 jmcneill callout_init(&sc->sc_tick_ch, 0);
1396 1.1 jmcneill callout_setfunc(&sc->sc_tick_ch, alc_tick, sc);
1397 1.1 jmcneill
1398 1.1 jmcneill /* Load station address. */
1399 1.1 jmcneill alc_get_macaddr(sc);
1400 1.1 jmcneill
1401 1.1 jmcneill aprint_normal_dev(self, "Ethernet address %s\n",
1402 1.1 jmcneill ether_sprintf(sc->alc_eaddr));
1403 1.1 jmcneill
1404 1.1 jmcneill ifp = &sc->sc_ec.ec_if;
1405 1.1 jmcneill ifp->if_softc = sc;
1406 1.1 jmcneill ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1407 1.1 jmcneill ifp->if_init = alc_init;
1408 1.1 jmcneill ifp->if_ioctl = alc_ioctl;
1409 1.1 jmcneill ifp->if_start = alc_start;
1410 1.1 jmcneill ifp->if_stop = alc_stop;
1411 1.1 jmcneill ifp->if_watchdog = alc_watchdog;
1412 1.1 jmcneill IFQ_SET_MAXLEN(&ifp->if_snd, ALC_TX_RING_CNT - 1);
1413 1.1 jmcneill IFQ_SET_READY(&ifp->if_snd);
1414 1.1 jmcneill strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
1415 1.1 jmcneill
1416 1.1 jmcneill sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1417 1.1 jmcneill
1418 1.1 jmcneill #ifdef ALC_CHECKSUM
1419 1.1 jmcneill ifp->if_capabilities |= IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
1420 1.1 jmcneill IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
1421 1.18 christos IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
1422 1.1 jmcneill #endif
1423 1.1 jmcneill
1424 1.1 jmcneill #if NVLAN > 0
1425 1.1 jmcneill sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
1426 1.1 jmcneill #endif
1427 1.1 jmcneill
1428 1.12 christos /*
1429 1.12 christos * XXX
1430 1.12 christos * It seems enabling Tx checksum offloading makes more trouble.
1431 1.12 christos * Sometimes the controller does not receive any frames when
1432 1.12 christos * Tx checksum offloading is enabled. I'm not sure whether this
1433 1.12 christos * is a bug in Tx checksum offloading logic or I got broken
1434 1.12 christos * sample boards. To safety, don't enable Tx checksum offloading
1435 1.12 christos * by default but give chance to users to toggle it if they know
1436 1.12 christos * their controllers work without problems.
1437 1.12 christos * Fortunately, Tx checksum offloading for AR816x family
1438 1.12 christos * seems to work.
1439 1.12 christos */
1440 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1441 1.12 christos ifp->if_capenable &= ~IFCAP_CSUM_IPv4_Tx;
1442 1.12 christos ifp->if_capabilities &= ~ALC_CSUM_FEATURES;
1443 1.12 christos }
1444 1.12 christos
1445 1.1 jmcneill /* Set up MII bus. */
1446 1.1 jmcneill sc->sc_miibus.mii_ifp = ifp;
1447 1.1 jmcneill sc->sc_miibus.mii_readreg = alc_miibus_readreg;
1448 1.1 jmcneill sc->sc_miibus.mii_writereg = alc_miibus_writereg;
1449 1.1 jmcneill sc->sc_miibus.mii_statchg = alc_miibus_statchg;
1450 1.1 jmcneill
1451 1.1 jmcneill sc->sc_ec.ec_mii = &sc->sc_miibus;
1452 1.1 jmcneill ifmedia_init(&sc->sc_miibus.mii_media, 0, alc_mediachange,
1453 1.1 jmcneill alc_mediastatus);
1454 1.1 jmcneill mii_flags = 0;
1455 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_JUMBO) != 0)
1456 1.1 jmcneill mii_flags |= MIIF_DOPAUSE;
1457 1.1 jmcneill mii_attach(self, &sc->sc_miibus, 0xffffffff, MII_PHY_ANY,
1458 1.1 jmcneill MII_OFFSET_ANY, mii_flags);
1459 1.1 jmcneill
1460 1.1 jmcneill if (LIST_FIRST(&sc->sc_miibus.mii_phys) == NULL) {
1461 1.1 jmcneill printf("%s: no PHY found!\n", device_xname(sc->sc_dev));
1462 1.1 jmcneill ifmedia_add(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL,
1463 1.1 jmcneill 0, NULL);
1464 1.1 jmcneill ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_MANUAL);
1465 1.8 christos } else
1466 1.1 jmcneill ifmedia_set(&sc->sc_miibus.mii_media, IFM_ETHER | IFM_AUTO);
1467 1.1 jmcneill
1468 1.1 jmcneill if_attach(ifp);
1469 1.22 ozaki if_deferred_start_init(ifp, NULL);
1470 1.1 jmcneill ether_ifattach(ifp, sc->alc_eaddr);
1471 1.1 jmcneill
1472 1.1 jmcneill if (!pmf_device_register(self, NULL, NULL))
1473 1.1 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
1474 1.1 jmcneill else
1475 1.1 jmcneill pmf_class_network_register(self, ifp);
1476 1.1 jmcneill
1477 1.1 jmcneill return;
1478 1.1 jmcneill fail:
1479 1.1 jmcneill alc_dma_free(sc);
1480 1.1 jmcneill if (sc->sc_irq_handle != NULL) {
1481 1.1 jmcneill pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
1482 1.1 jmcneill sc->sc_irq_handle = NULL;
1483 1.1 jmcneill }
1484 1.1 jmcneill if (sc->sc_mem_size) {
1485 1.1 jmcneill bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
1486 1.1 jmcneill sc->sc_mem_size = 0;
1487 1.1 jmcneill }
1488 1.1 jmcneill }
1489 1.1 jmcneill
1490 1.1 jmcneill static int
1491 1.1 jmcneill alc_detach(device_t self, int flags)
1492 1.1 jmcneill {
1493 1.1 jmcneill struct alc_softc *sc = device_private(self);
1494 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
1495 1.1 jmcneill int s;
1496 1.1 jmcneill
1497 1.1 jmcneill s = splnet();
1498 1.1 jmcneill alc_stop(ifp, 0);
1499 1.1 jmcneill splx(s);
1500 1.1 jmcneill
1501 1.1 jmcneill mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
1502 1.1 jmcneill
1503 1.1 jmcneill /* Delete all remaining media. */
1504 1.1 jmcneill ifmedia_delete_instance(&sc->sc_miibus.mii_media, IFM_INST_ANY);
1505 1.1 jmcneill
1506 1.1 jmcneill ether_ifdetach(ifp);
1507 1.1 jmcneill if_detach(ifp);
1508 1.1 jmcneill alc_dma_free(sc);
1509 1.1 jmcneill
1510 1.1 jmcneill alc_phy_down(sc);
1511 1.1 jmcneill if (sc->sc_irq_handle != NULL) {
1512 1.1 jmcneill pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
1513 1.1 jmcneill sc->sc_irq_handle = NULL;
1514 1.1 jmcneill }
1515 1.1 jmcneill if (sc->sc_mem_size) {
1516 1.1 jmcneill bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
1517 1.1 jmcneill sc->sc_mem_size = 0;
1518 1.1 jmcneill }
1519 1.1 jmcneill
1520 1.1 jmcneill return (0);
1521 1.1 jmcneill }
1522 1.1 jmcneill
1523 1.1 jmcneill static int
1524 1.1 jmcneill alc_dma_alloc(struct alc_softc *sc)
1525 1.1 jmcneill {
1526 1.1 jmcneill struct alc_txdesc *txd;
1527 1.1 jmcneill struct alc_rxdesc *rxd;
1528 1.1 jmcneill int nsegs, error, i;
1529 1.1 jmcneill
1530 1.1 jmcneill /*
1531 1.1 jmcneill * Create DMA stuffs for TX ring
1532 1.1 jmcneill */
1533 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_TX_RING_SZ, 1,
1534 1.1 jmcneill ALC_TX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_tx_ring_map);
1535 1.1 jmcneill if (error) {
1536 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map = NULL;
1537 1.1 jmcneill return (ENOBUFS);
1538 1.1 jmcneill }
1539 1.1 jmcneill
1540 1.1 jmcneill /* Allocate DMA'able memory for TX ring */
1541 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, ALC_TX_RING_SZ,
1542 1.1 jmcneill ETHER_ALIGN, 0, &sc->alc_rdata.alc_tx_ring_seg, 1,
1543 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1544 1.1 jmcneill if (error) {
1545 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Tx ring.\n",
1546 1.1 jmcneill device_xname(sc->sc_dev));
1547 1.1 jmcneill return error;
1548 1.1 jmcneill }
1549 1.1 jmcneill
1550 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_tx_ring_seg,
1551 1.1 jmcneill nsegs, ALC_TX_RING_SZ, (void **)&sc->alc_rdata.alc_tx_ring,
1552 1.1 jmcneill BUS_DMA_NOWAIT);
1553 1.1 jmcneill if (error)
1554 1.1 jmcneill return (ENOBUFS);
1555 1.1 jmcneill
1556 1.1 jmcneill /* Load the DMA map for Tx ring. */
1557 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map,
1558 1.1 jmcneill sc->alc_rdata.alc_tx_ring, ALC_TX_RING_SZ, NULL, BUS_DMA_WAITOK);
1559 1.1 jmcneill if (error) {
1560 1.1 jmcneill printf("%s: could not load DMA'able memory for Tx ring.\n",
1561 1.1 jmcneill device_xname(sc->sc_dev));
1562 1.8 christos bus_dmamem_free(sc->sc_dmat,
1563 1.1 jmcneill &sc->alc_rdata.alc_tx_ring_seg, 1);
1564 1.1 jmcneill return error;
1565 1.1 jmcneill }
1566 1.1 jmcneill
1567 1.8 christos sc->alc_rdata.alc_tx_ring_paddr =
1568 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_segs[0].ds_addr;
1569 1.1 jmcneill
1570 1.1 jmcneill /*
1571 1.1 jmcneill * Create DMA stuffs for RX ring
1572 1.1 jmcneill */
1573 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_RX_RING_SZ, 1,
1574 1.1 jmcneill ALC_RX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_ring_map);
1575 1.1 jmcneill if (error)
1576 1.1 jmcneill return (ENOBUFS);
1577 1.8 christos
1578 1.1 jmcneill /* Allocate DMA'able memory for RX ring */
1579 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, ALC_RX_RING_SZ,
1580 1.1 jmcneill ETHER_ALIGN, 0, &sc->alc_rdata.alc_rx_ring_seg, 1,
1581 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1582 1.1 jmcneill if (error) {
1583 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Rx ring.\n",
1584 1.1 jmcneill device_xname(sc->sc_dev));
1585 1.1 jmcneill return error;
1586 1.1 jmcneill }
1587 1.1 jmcneill
1588 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rx_ring_seg,
1589 1.1 jmcneill nsegs, ALC_RX_RING_SZ, (void **)&sc->alc_rdata.alc_rx_ring,
1590 1.1 jmcneill BUS_DMA_NOWAIT);
1591 1.1 jmcneill if (error)
1592 1.1 jmcneill return (ENOBUFS);
1593 1.1 jmcneill
1594 1.1 jmcneill /* Load the DMA map for Rx ring. */
1595 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map,
1596 1.1 jmcneill sc->alc_rdata.alc_rx_ring, ALC_RX_RING_SZ, NULL, BUS_DMA_WAITOK);
1597 1.1 jmcneill if (error) {
1598 1.1 jmcneill printf("%s: could not load DMA'able memory for Rx ring.\n",
1599 1.1 jmcneill device_xname(sc->sc_dev));
1600 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1601 1.1 jmcneill &sc->alc_rdata.alc_rx_ring_seg, 1);
1602 1.1 jmcneill return error;
1603 1.1 jmcneill }
1604 1.1 jmcneill
1605 1.1 jmcneill sc->alc_rdata.alc_rx_ring_paddr =
1606 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_segs[0].ds_addr;
1607 1.1 jmcneill
1608 1.1 jmcneill /*
1609 1.1 jmcneill * Create DMA stuffs for RX return ring
1610 1.1 jmcneill */
1611 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_RR_RING_SZ, 1,
1612 1.1 jmcneill ALC_RR_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rr_ring_map);
1613 1.1 jmcneill if (error)
1614 1.1 jmcneill return (ENOBUFS);
1615 1.1 jmcneill
1616 1.1 jmcneill /* Allocate DMA'able memory for RX return ring */
1617 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_RR_RING_SZ,
1618 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_rr_ring_seg, 1,
1619 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1620 1.1 jmcneill if (error) {
1621 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Rx "
1622 1.1 jmcneill "return ring.\n", device_xname(sc->sc_dev));
1623 1.1 jmcneill return error;
1624 1.1 jmcneill }
1625 1.1 jmcneill
1626 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rr_ring_seg,
1627 1.1 jmcneill nsegs, ALC_RR_RING_SZ, (void **)&sc->alc_rdata.alc_rr_ring,
1628 1.1 jmcneill BUS_DMA_NOWAIT);
1629 1.1 jmcneill if (error)
1630 1.1 jmcneill return (ENOBUFS);
1631 1.1 jmcneill
1632 1.1 jmcneill /* Load the DMA map for Rx return ring. */
1633 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map,
1634 1.1 jmcneill sc->alc_rdata.alc_rr_ring, ALC_RR_RING_SZ, NULL, BUS_DMA_WAITOK);
1635 1.1 jmcneill if (error) {
1636 1.1 jmcneill printf("%s: could not load DMA'able memory for Rx return ring."
1637 1.1 jmcneill "\n", device_xname(sc->sc_dev));
1638 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1639 1.1 jmcneill &sc->alc_rdata.alc_rr_ring_seg, 1);
1640 1.1 jmcneill return error;
1641 1.1 jmcneill }
1642 1.1 jmcneill
1643 1.8 christos sc->alc_rdata.alc_rr_ring_paddr =
1644 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map->dm_segs[0].ds_addr;
1645 1.1 jmcneill
1646 1.1 jmcneill /*
1647 1.8 christos * Create DMA stuffs for CMB block
1648 1.1 jmcneill */
1649 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_CMB_SZ, 1,
1650 1.8 christos ALC_CMB_SZ, 0, BUS_DMA_NOWAIT,
1651 1.1 jmcneill &sc->alc_cdata.alc_cmb_map);
1652 1.8 christos if (error)
1653 1.1 jmcneill return (ENOBUFS);
1654 1.1 jmcneill
1655 1.1 jmcneill /* Allocate DMA'able memory for CMB block */
1656 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_CMB_SZ,
1657 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_cmb_seg, 1,
1658 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1659 1.1 jmcneill if (error) {
1660 1.1 jmcneill printf("%s: could not allocate DMA'able memory for "
1661 1.1 jmcneill "CMB block\n", device_xname(sc->sc_dev));
1662 1.1 jmcneill return error;
1663 1.1 jmcneill }
1664 1.1 jmcneill
1665 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_cmb_seg,
1666 1.1 jmcneill nsegs, ALC_CMB_SZ, (void **)&sc->alc_rdata.alc_cmb,
1667 1.1 jmcneill BUS_DMA_NOWAIT);
1668 1.1 jmcneill if (error)
1669 1.1 jmcneill return (ENOBUFS);
1670 1.1 jmcneill
1671 1.1 jmcneill /* Load the DMA map for CMB block. */
1672 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_cmb_map,
1673 1.8 christos sc->alc_rdata.alc_cmb, ALC_CMB_SZ, NULL,
1674 1.1 jmcneill BUS_DMA_WAITOK);
1675 1.1 jmcneill if (error) {
1676 1.1 jmcneill printf("%s: could not load DMA'able memory for CMB block\n",
1677 1.1 jmcneill device_xname(sc->sc_dev));
1678 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1679 1.1 jmcneill &sc->alc_rdata.alc_cmb_seg, 1);
1680 1.1 jmcneill return error;
1681 1.1 jmcneill }
1682 1.1 jmcneill
1683 1.8 christos sc->alc_rdata.alc_cmb_paddr =
1684 1.1 jmcneill sc->alc_cdata.alc_cmb_map->dm_segs[0].ds_addr;
1685 1.1 jmcneill
1686 1.1 jmcneill /*
1687 1.1 jmcneill * Create DMA stuffs for SMB block
1688 1.1 jmcneill */
1689 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_SMB_SZ, 1,
1690 1.8 christos ALC_SMB_SZ, 0, BUS_DMA_NOWAIT,
1691 1.1 jmcneill &sc->alc_cdata.alc_smb_map);
1692 1.1 jmcneill if (error)
1693 1.1 jmcneill return (ENOBUFS);
1694 1.1 jmcneill
1695 1.1 jmcneill /* Allocate DMA'able memory for SMB block */
1696 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_SMB_SZ,
1697 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_smb_seg, 1,
1698 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1699 1.1 jmcneill if (error) {
1700 1.1 jmcneill printf("%s: could not allocate DMA'able memory for "
1701 1.1 jmcneill "SMB block\n", device_xname(sc->sc_dev));
1702 1.1 jmcneill return error;
1703 1.1 jmcneill }
1704 1.1 jmcneill
1705 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_smb_seg,
1706 1.1 jmcneill nsegs, ALC_SMB_SZ, (void **)&sc->alc_rdata.alc_smb,
1707 1.1 jmcneill BUS_DMA_NOWAIT);
1708 1.1 jmcneill if (error)
1709 1.1 jmcneill return (ENOBUFS);
1710 1.1 jmcneill
1711 1.1 jmcneill /* Load the DMA map for SMB block */
1712 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_smb_map,
1713 1.8 christos sc->alc_rdata.alc_smb, ALC_SMB_SZ, NULL,
1714 1.1 jmcneill BUS_DMA_WAITOK);
1715 1.1 jmcneill if (error) {
1716 1.1 jmcneill printf("%s: could not load DMA'able memory for SMB block\n",
1717 1.1 jmcneill device_xname(sc->sc_dev));
1718 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1719 1.1 jmcneill &sc->alc_rdata.alc_smb_seg, 1);
1720 1.1 jmcneill return error;
1721 1.1 jmcneill }
1722 1.1 jmcneill
1723 1.8 christos sc->alc_rdata.alc_smb_paddr =
1724 1.1 jmcneill sc->alc_cdata.alc_smb_map->dm_segs[0].ds_addr;
1725 1.1 jmcneill
1726 1.1 jmcneill
1727 1.1 jmcneill /* Create DMA maps for Tx buffers. */
1728 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
1729 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
1730 1.1 jmcneill txd->tx_m = NULL;
1731 1.1 jmcneill txd->tx_dmamap = NULL;
1732 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_TSO_MAXSIZE,
1733 1.1 jmcneill ALC_MAXTXSEGS, ALC_TSO_MAXSEGSIZE, 0, BUS_DMA_NOWAIT,
1734 1.1 jmcneill &txd->tx_dmamap);
1735 1.1 jmcneill if (error) {
1736 1.1 jmcneill printf("%s: could not create Tx dmamap.\n",
1737 1.1 jmcneill device_xname(sc->sc_dev));
1738 1.1 jmcneill return error;
1739 1.1 jmcneill }
1740 1.1 jmcneill }
1741 1.1 jmcneill
1742 1.1 jmcneill /* Create DMA maps for Rx buffers. */
1743 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
1744 1.1 jmcneill BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_sparemap);
1745 1.1 jmcneill if (error) {
1746 1.1 jmcneill printf("%s: could not create spare Rx dmamap.\n",
1747 1.1 jmcneill device_xname(sc->sc_dev));
1748 1.1 jmcneill return error;
1749 1.1 jmcneill }
1750 1.1 jmcneill
1751 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
1752 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
1753 1.1 jmcneill rxd->rx_m = NULL;
1754 1.1 jmcneill rxd->rx_dmamap = NULL;
1755 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1756 1.1 jmcneill MCLBYTES, 0, BUS_DMA_NOWAIT, &rxd->rx_dmamap);
1757 1.1 jmcneill if (error) {
1758 1.1 jmcneill printf("%s: could not create Rx dmamap.\n",
1759 1.1 jmcneill device_xname(sc->sc_dev));
1760 1.1 jmcneill return error;
1761 1.1 jmcneill }
1762 1.1 jmcneill }
1763 1.1 jmcneill
1764 1.1 jmcneill return (0);
1765 1.1 jmcneill }
1766 1.1 jmcneill
1767 1.1 jmcneill
1768 1.1 jmcneill static void
1769 1.1 jmcneill alc_dma_free(struct alc_softc *sc)
1770 1.1 jmcneill {
1771 1.1 jmcneill struct alc_txdesc *txd;
1772 1.1 jmcneill struct alc_rxdesc *rxd;
1773 1.1 jmcneill int i;
1774 1.1 jmcneill
1775 1.1 jmcneill /* Tx buffers */
1776 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
1777 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
1778 1.1 jmcneill if (txd->tx_dmamap != NULL) {
1779 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, txd->tx_dmamap);
1780 1.1 jmcneill txd->tx_dmamap = NULL;
1781 1.1 jmcneill }
1782 1.1 jmcneill }
1783 1.1 jmcneill /* Rx buffers */
1784 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
1785 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
1786 1.1 jmcneill if (rxd->rx_dmamap != NULL) {
1787 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, rxd->rx_dmamap);
1788 1.1 jmcneill rxd->rx_dmamap = NULL;
1789 1.1 jmcneill }
1790 1.1 jmcneill }
1791 1.1 jmcneill if (sc->alc_cdata.alc_rx_sparemap != NULL) {
1792 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->alc_cdata.alc_rx_sparemap);
1793 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap = NULL;
1794 1.1 jmcneill }
1795 1.1 jmcneill
1796 1.1 jmcneill /* Tx ring. */
1797 1.1 jmcneill if (sc->alc_cdata.alc_tx_ring_map != NULL)
1798 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map);
1799 1.1 jmcneill if (sc->alc_cdata.alc_tx_ring_map != NULL &&
1800 1.1 jmcneill sc->alc_rdata.alc_tx_ring != NULL)
1801 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1802 1.1 jmcneill &sc->alc_rdata.alc_tx_ring_seg, 1);
1803 1.1 jmcneill sc->alc_rdata.alc_tx_ring = NULL;
1804 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map = NULL;
1805 1.1 jmcneill
1806 1.1 jmcneill /* Rx ring. */
1807 1.8 christos if (sc->alc_cdata.alc_rx_ring_map != NULL)
1808 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map);
1809 1.1 jmcneill if (sc->alc_cdata.alc_rx_ring_map != NULL &&
1810 1.1 jmcneill sc->alc_rdata.alc_rx_ring != NULL)
1811 1.8 christos bus_dmamem_free(sc->sc_dmat,
1812 1.1 jmcneill &sc->alc_rdata.alc_rx_ring_seg, 1);
1813 1.1 jmcneill sc->alc_rdata.alc_rx_ring = NULL;
1814 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map = NULL;
1815 1.1 jmcneill
1816 1.1 jmcneill /* Rx return ring. */
1817 1.1 jmcneill if (sc->alc_cdata.alc_rr_ring_map != NULL)
1818 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map);
1819 1.1 jmcneill if (sc->alc_cdata.alc_rr_ring_map != NULL &&
1820 1.1 jmcneill sc->alc_rdata.alc_rr_ring != NULL)
1821 1.8 christos bus_dmamem_free(sc->sc_dmat,
1822 1.1 jmcneill &sc->alc_rdata.alc_rr_ring_seg, 1);
1823 1.1 jmcneill sc->alc_rdata.alc_rr_ring = NULL;
1824 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map = NULL;
1825 1.1 jmcneill
1826 1.1 jmcneill /* CMB block */
1827 1.1 jmcneill if (sc->alc_cdata.alc_cmb_map != NULL)
1828 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_cmb_map);
1829 1.1 jmcneill if (sc->alc_cdata.alc_cmb_map != NULL &&
1830 1.1 jmcneill sc->alc_rdata.alc_cmb != NULL)
1831 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1832 1.1 jmcneill &sc->alc_rdata.alc_cmb_seg, 1);
1833 1.1 jmcneill sc->alc_rdata.alc_cmb = NULL;
1834 1.1 jmcneill sc->alc_cdata.alc_cmb_map = NULL;
1835 1.1 jmcneill
1836 1.1 jmcneill /* SMB block */
1837 1.1 jmcneill if (sc->alc_cdata.alc_smb_map != NULL)
1838 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_smb_map);
1839 1.1 jmcneill if (sc->alc_cdata.alc_smb_map != NULL &&
1840 1.1 jmcneill sc->alc_rdata.alc_smb != NULL)
1841 1.8 christos bus_dmamem_free(sc->sc_dmat,
1842 1.1 jmcneill &sc->alc_rdata.alc_smb_seg, 1);
1843 1.1 jmcneill sc->alc_rdata.alc_smb = NULL;
1844 1.1 jmcneill sc->alc_cdata.alc_smb_map = NULL;
1845 1.1 jmcneill }
1846 1.1 jmcneill
1847 1.1 jmcneill static int
1848 1.1 jmcneill alc_encap(struct alc_softc *sc, struct mbuf **m_head)
1849 1.1 jmcneill {
1850 1.1 jmcneill struct alc_txdesc *txd, *txd_last;
1851 1.1 jmcneill struct tx_desc *desc;
1852 1.1 jmcneill struct mbuf *m;
1853 1.1 jmcneill bus_dmamap_t map;
1854 1.1 jmcneill uint32_t cflags, poff, vtag;
1855 1.1 jmcneill int error, idx, nsegs, prod;
1856 1.1 jmcneill #if NVLAN > 0
1857 1.1 jmcneill struct m_tag *mtag;
1858 1.1 jmcneill #endif
1859 1.1 jmcneill
1860 1.1 jmcneill m = *m_head;
1861 1.1 jmcneill cflags = vtag = 0;
1862 1.1 jmcneill poff = 0;
1863 1.1 jmcneill
1864 1.1 jmcneill prod = sc->alc_cdata.alc_tx_prod;
1865 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[prod];
1866 1.1 jmcneill txd_last = txd;
1867 1.1 jmcneill map = txd->tx_dmamap;
1868 1.1 jmcneill
1869 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head, BUS_DMA_NOWAIT);
1870 1.1 jmcneill
1871 1.1 jmcneill if (error == EFBIG) {
1872 1.1 jmcneill error = 0;
1873 1.1 jmcneill
1874 1.1 jmcneill *m_head = m_pullup(*m_head, MHLEN);
1875 1.1 jmcneill if (*m_head == NULL) {
1876 1.1 jmcneill printf("%s: can't defrag TX mbuf\n",
1877 1.1 jmcneill device_xname(sc->sc_dev));
1878 1.1 jmcneill return ENOBUFS;
1879 1.1 jmcneill }
1880 1.1 jmcneill
1881 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head,
1882 1.1 jmcneill BUS_DMA_NOWAIT);
1883 1.1 jmcneill
1884 1.1 jmcneill if (error != 0) {
1885 1.1 jmcneill printf("%s: could not load defragged TX mbuf\n",
1886 1.1 jmcneill device_xname(sc->sc_dev));
1887 1.1 jmcneill m_freem(*m_head);
1888 1.1 jmcneill *m_head = NULL;
1889 1.1 jmcneill return error;
1890 1.1 jmcneill }
1891 1.1 jmcneill } else if (error) {
1892 1.1 jmcneill printf("%s: could not load TX mbuf\n", device_xname(sc->sc_dev));
1893 1.1 jmcneill return (error);
1894 1.1 jmcneill }
1895 1.1 jmcneill
1896 1.1 jmcneill nsegs = map->dm_nsegs;
1897 1.1 jmcneill
1898 1.1 jmcneill if (nsegs == 0) {
1899 1.1 jmcneill m_freem(*m_head);
1900 1.1 jmcneill *m_head = NULL;
1901 1.1 jmcneill return (EIO);
1902 1.1 jmcneill }
1903 1.1 jmcneill
1904 1.1 jmcneill /* Check descriptor overrun. */
1905 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
1906 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, map);
1907 1.1 jmcneill return (ENOBUFS);
1908 1.1 jmcneill }
1909 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1910 1.1 jmcneill BUS_DMASYNC_PREWRITE);
1911 1.1 jmcneill
1912 1.1 jmcneill m = *m_head;
1913 1.1 jmcneill desc = NULL;
1914 1.1 jmcneill idx = 0;
1915 1.1 jmcneill #if NVLAN > 0
1916 1.1 jmcneill /* Configure VLAN hardware tag insertion. */
1917 1.1 jmcneill if ((mtag = VLAN_OUTPUT_TAG(&sc->sc_ec, m))) {
1918 1.1 jmcneill vtag = htons(VLAN_TAG_VALUE(mtag));
1919 1.1 jmcneill vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
1920 1.1 jmcneill cflags |= TD_INS_VLAN_TAG;
1921 1.1 jmcneill }
1922 1.1 jmcneill #endif
1923 1.1 jmcneill /* Configure Tx checksum offload. */
1924 1.1 jmcneill if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
1925 1.1 jmcneill cflags |= TD_CUSTOM_CSUM;
1926 1.1 jmcneill /* Set checksum start offset. */
1927 1.1 jmcneill cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
1928 1.1 jmcneill TD_PLOAD_OFFSET_MASK;
1929 1.8 christos }
1930 1.1 jmcneill for (; idx < nsegs; idx++) {
1931 1.1 jmcneill desc = &sc->alc_rdata.alc_tx_ring[prod];
1932 1.1 jmcneill desc->len =
1933 1.1 jmcneill htole32(TX_BYTES(map->dm_segs[idx].ds_len) | vtag);
1934 1.1 jmcneill desc->flags = htole32(cflags);
1935 1.1 jmcneill desc->addr = htole64(map->dm_segs[idx].ds_addr);
1936 1.1 jmcneill sc->alc_cdata.alc_tx_cnt++;
1937 1.1 jmcneill ALC_DESC_INC(prod, ALC_TX_RING_CNT);
1938 1.1 jmcneill }
1939 1.1 jmcneill /* Update producer index. */
1940 1.1 jmcneill sc->alc_cdata.alc_tx_prod = prod;
1941 1.1 jmcneill
1942 1.1 jmcneill /* Finally set EOP on the last descriptor. */
1943 1.1 jmcneill prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
1944 1.1 jmcneill desc = &sc->alc_rdata.alc_tx_ring[prod];
1945 1.1 jmcneill desc->flags |= htole32(TD_EOP);
1946 1.1 jmcneill
1947 1.1 jmcneill /* Swap dmamap of the first and the last. */
1948 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[prod];
1949 1.1 jmcneill map = txd_last->tx_dmamap;
1950 1.1 jmcneill txd_last->tx_dmamap = txd->tx_dmamap;
1951 1.1 jmcneill txd->tx_dmamap = map;
1952 1.1 jmcneill txd->tx_m = m;
1953 1.1 jmcneill
1954 1.1 jmcneill return (0);
1955 1.1 jmcneill }
1956 1.1 jmcneill
1957 1.1 jmcneill static void
1958 1.1 jmcneill alc_start(struct ifnet *ifp)
1959 1.1 jmcneill {
1960 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
1961 1.1 jmcneill struct mbuf *m_head;
1962 1.1 jmcneill int enq;
1963 1.1 jmcneill
1964 1.1 jmcneill if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1965 1.1 jmcneill return;
1966 1.15 leot if ((sc->alc_flags & ALC_FLAG_LINK) == 0)
1967 1.15 leot return;
1968 1.15 leot if (IFQ_IS_EMPTY(&ifp->if_snd))
1969 1.15 leot return;
1970 1.1 jmcneill
1971 1.1 jmcneill /* Reclaim transmitted frames. */
1972 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
1973 1.1 jmcneill alc_txeof(sc);
1974 1.1 jmcneill
1975 1.1 jmcneill enq = 0;
1976 1.1 jmcneill for (;;) {
1977 1.1 jmcneill IFQ_DEQUEUE(&ifp->if_snd, m_head);
1978 1.1 jmcneill if (m_head == NULL)
1979 1.1 jmcneill break;
1980 1.1 jmcneill
1981 1.1 jmcneill /*
1982 1.1 jmcneill * Pack the data into the transmit ring. If we
1983 1.1 jmcneill * don't have room, set the OACTIVE flag and wait
1984 1.1 jmcneill * for the NIC to drain the ring.
1985 1.1 jmcneill */
1986 1.1 jmcneill if (alc_encap(sc, &m_head)) {
1987 1.1 jmcneill if (m_head == NULL)
1988 1.1 jmcneill break;
1989 1.1 jmcneill ifp->if_flags |= IFF_OACTIVE;
1990 1.1 jmcneill break;
1991 1.1 jmcneill }
1992 1.1 jmcneill enq = 1;
1993 1.8 christos
1994 1.1 jmcneill /*
1995 1.1 jmcneill * If there's a BPF listener, bounce a copy of this frame
1996 1.1 jmcneill * to him.
1997 1.1 jmcneill */
1998 1.1 jmcneill bpf_mtap(ifp, m_head);
1999 1.1 jmcneill }
2000 1.1 jmcneill
2001 1.1 jmcneill if (enq) {
2002 1.1 jmcneill /* Sync descriptors. */
2003 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
2004 1.8 christos sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
2005 1.1 jmcneill BUS_DMASYNC_PREWRITE);
2006 1.1 jmcneill /* Kick. Assume we're using normal Tx priority queue. */
2007 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2008 1.1 jmcneill (sc->alc_cdata.alc_tx_prod <<
2009 1.1 jmcneill MBOX_TD_PROD_LO_IDX_SHIFT) &
2010 1.1 jmcneill MBOX_TD_PROD_LO_IDX_MASK);
2011 1.1 jmcneill /* Set a timeout in case the chip goes out to lunch. */
2012 1.1 jmcneill ifp->if_timer = ALC_TX_TIMEOUT;
2013 1.1 jmcneill }
2014 1.1 jmcneill }
2015 1.1 jmcneill
2016 1.1 jmcneill static void
2017 1.1 jmcneill alc_watchdog(struct ifnet *ifp)
2018 1.1 jmcneill {
2019 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
2020 1.1 jmcneill
2021 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
2022 1.1 jmcneill printf("%s: watchdog timeout (missed link)\n",
2023 1.1 jmcneill device_xname(sc->sc_dev));
2024 1.1 jmcneill ifp->if_oerrors++;
2025 1.7 mrg alc_init_backend(ifp, false);
2026 1.1 jmcneill return;
2027 1.1 jmcneill }
2028 1.1 jmcneill
2029 1.1 jmcneill printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
2030 1.1 jmcneill ifp->if_oerrors++;
2031 1.7 mrg alc_init_backend(ifp, false);
2032 1.15 leot alc_start(ifp);
2033 1.1 jmcneill }
2034 1.1 jmcneill
2035 1.1 jmcneill static int
2036 1.1 jmcneill alc_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2037 1.1 jmcneill {
2038 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
2039 1.16 christos struct mii_data *mii = &sc->sc_miibus;
2040 1.16 christos struct ifreq *ifr = (struct ifreq *)data;
2041 1.1 jmcneill int s, error = 0;
2042 1.1 jmcneill
2043 1.1 jmcneill s = splnet();
2044 1.1 jmcneill
2045 1.1 jmcneill error = ether_ioctl(ifp, cmd, data);
2046 1.16 christos switch (cmd) {
2047 1.16 christos case SIOCSIFADDR:
2048 1.16 christos ifp->if_flags |= IFF_UP;
2049 1.16 christos if (!(ifp->if_flags & IFF_RUNNING))
2050 1.16 christos alc_init(ifp);
2051 1.16 christos break;
2052 1.16 christos
2053 1.16 christos case SIOCSIFFLAGS:
2054 1.16 christos if (ifp->if_flags & IFF_UP) {
2055 1.16 christos if (ifp->if_flags & IFF_RUNNING)
2056 1.16 christos error = ENETRESET;
2057 1.16 christos else
2058 1.16 christos alc_init(ifp);
2059 1.16 christos } else {
2060 1.16 christos if (ifp->if_flags & IFF_RUNNING)
2061 1.16 christos alc_stop(ifp, 0);
2062 1.16 christos }
2063 1.16 christos break;
2064 1.16 christos
2065 1.16 christos case SIOCSIFMEDIA:
2066 1.16 christos case SIOCGIFMEDIA:
2067 1.16 christos error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2068 1.16 christos break;
2069 1.16 christos
2070 1.16 christos default:
2071 1.16 christos error = ether_ioctl(ifp, cmd, data);
2072 1.16 christos break;
2073 1.16 christos }
2074 1.16 christos
2075 1.1 jmcneill if (error == ENETRESET) {
2076 1.1 jmcneill if (ifp->if_flags & IFF_RUNNING)
2077 1.1 jmcneill alc_iff(sc);
2078 1.1 jmcneill error = 0;
2079 1.1 jmcneill }
2080 1.1 jmcneill
2081 1.1 jmcneill splx(s);
2082 1.1 jmcneill return (error);
2083 1.1 jmcneill }
2084 1.1 jmcneill
2085 1.1 jmcneill static void
2086 1.1 jmcneill alc_mac_config(struct alc_softc *sc)
2087 1.1 jmcneill {
2088 1.1 jmcneill struct mii_data *mii;
2089 1.1 jmcneill uint32_t reg;
2090 1.1 jmcneill
2091 1.1 jmcneill mii = &sc->sc_miibus;
2092 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
2093 1.1 jmcneill reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
2094 1.1 jmcneill MAC_CFG_SPEED_MASK);
2095 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
2096 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
2097 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
2098 1.2 jmcneill reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
2099 1.1 jmcneill /* Reprogram MAC with resolved speed/duplex. */
2100 1.1 jmcneill switch (IFM_SUBTYPE(mii->mii_media_active)) {
2101 1.1 jmcneill case IFM_10_T:
2102 1.1 jmcneill case IFM_100_TX:
2103 1.1 jmcneill reg |= MAC_CFG_SPEED_10_100;
2104 1.1 jmcneill break;
2105 1.1 jmcneill case IFM_1000_T:
2106 1.1 jmcneill reg |= MAC_CFG_SPEED_1000;
2107 1.1 jmcneill break;
2108 1.1 jmcneill }
2109 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2110 1.1 jmcneill reg |= MAC_CFG_FULL_DUPLEX;
2111 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2112 1.1 jmcneill reg |= MAC_CFG_TX_FC;
2113 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2114 1.1 jmcneill reg |= MAC_CFG_RX_FC;
2115 1.1 jmcneill }
2116 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2117 1.1 jmcneill }
2118 1.1 jmcneill
2119 1.1 jmcneill static void
2120 1.1 jmcneill alc_stats_clear(struct alc_softc *sc)
2121 1.1 jmcneill {
2122 1.1 jmcneill struct smb sb, *smb;
2123 1.1 jmcneill uint32_t *reg;
2124 1.1 jmcneill int i;
2125 1.1 jmcneill
2126 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2127 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2128 1.8 christos sc->alc_cdata.alc_smb_map->dm_mapsize,
2129 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2130 1.1 jmcneill smb = sc->alc_rdata.alc_smb;
2131 1.1 jmcneill /* Update done, clear. */
2132 1.1 jmcneill smb->updated = 0;
2133 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2134 1.8 christos sc->alc_cdata.alc_smb_map->dm_mapsize,
2135 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2136 1.1 jmcneill } else {
2137 1.1 jmcneill for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2138 1.1 jmcneill reg++) {
2139 1.1 jmcneill CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2140 1.1 jmcneill i += sizeof(uint32_t);
2141 1.1 jmcneill }
2142 1.1 jmcneill /* Read Tx statistics. */
2143 1.1 jmcneill for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2144 1.1 jmcneill reg++) {
2145 1.1 jmcneill CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2146 1.1 jmcneill i += sizeof(uint32_t);
2147 1.1 jmcneill }
2148 1.1 jmcneill }
2149 1.1 jmcneill }
2150 1.1 jmcneill
2151 1.1 jmcneill static void
2152 1.1 jmcneill alc_stats_update(struct alc_softc *sc)
2153 1.1 jmcneill {
2154 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2155 1.1 jmcneill struct alc_hw_stats *stat;
2156 1.1 jmcneill struct smb sb, *smb;
2157 1.1 jmcneill uint32_t *reg;
2158 1.1 jmcneill int i;
2159 1.1 jmcneill
2160 1.1 jmcneill stat = &sc->alc_stats;
2161 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2162 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2163 1.1 jmcneill sc->alc_cdata.alc_smb_map->dm_mapsize,
2164 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2165 1.1 jmcneill smb = sc->alc_rdata.alc_smb;
2166 1.1 jmcneill if (smb->updated == 0)
2167 1.1 jmcneill return;
2168 1.1 jmcneill } else {
2169 1.1 jmcneill smb = &sb;
2170 1.1 jmcneill /* Read Rx statistics. */
2171 1.1 jmcneill for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2172 1.1 jmcneill reg++) {
2173 1.1 jmcneill *reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2174 1.1 jmcneill i += sizeof(uint32_t);
2175 1.1 jmcneill }
2176 1.1 jmcneill /* Read Tx statistics. */
2177 1.1 jmcneill for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2178 1.1 jmcneill reg++) {
2179 1.1 jmcneill *reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2180 1.1 jmcneill i += sizeof(uint32_t);
2181 1.1 jmcneill }
2182 1.1 jmcneill }
2183 1.1 jmcneill
2184 1.1 jmcneill /* Rx stats. */
2185 1.1 jmcneill stat->rx_frames += smb->rx_frames;
2186 1.1 jmcneill stat->rx_bcast_frames += smb->rx_bcast_frames;
2187 1.1 jmcneill stat->rx_mcast_frames += smb->rx_mcast_frames;
2188 1.1 jmcneill stat->rx_pause_frames += smb->rx_pause_frames;
2189 1.1 jmcneill stat->rx_control_frames += smb->rx_control_frames;
2190 1.1 jmcneill stat->rx_crcerrs += smb->rx_crcerrs;
2191 1.1 jmcneill stat->rx_lenerrs += smb->rx_lenerrs;
2192 1.1 jmcneill stat->rx_bytes += smb->rx_bytes;
2193 1.1 jmcneill stat->rx_runts += smb->rx_runts;
2194 1.1 jmcneill stat->rx_fragments += smb->rx_fragments;
2195 1.1 jmcneill stat->rx_pkts_64 += smb->rx_pkts_64;
2196 1.1 jmcneill stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
2197 1.1 jmcneill stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
2198 1.1 jmcneill stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
2199 1.1 jmcneill stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
2200 1.1 jmcneill stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
2201 1.1 jmcneill stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
2202 1.1 jmcneill stat->rx_pkts_truncated += smb->rx_pkts_truncated;
2203 1.1 jmcneill stat->rx_fifo_oflows += smb->rx_fifo_oflows;
2204 1.1 jmcneill stat->rx_rrs_errs += smb->rx_rrs_errs;
2205 1.1 jmcneill stat->rx_alignerrs += smb->rx_alignerrs;
2206 1.1 jmcneill stat->rx_bcast_bytes += smb->rx_bcast_bytes;
2207 1.1 jmcneill stat->rx_mcast_bytes += smb->rx_mcast_bytes;
2208 1.1 jmcneill stat->rx_pkts_filtered += smb->rx_pkts_filtered;
2209 1.1 jmcneill
2210 1.1 jmcneill /* Tx stats. */
2211 1.1 jmcneill stat->tx_frames += smb->tx_frames;
2212 1.1 jmcneill stat->tx_bcast_frames += smb->tx_bcast_frames;
2213 1.1 jmcneill stat->tx_mcast_frames += smb->tx_mcast_frames;
2214 1.1 jmcneill stat->tx_pause_frames += smb->tx_pause_frames;
2215 1.1 jmcneill stat->tx_excess_defer += smb->tx_excess_defer;
2216 1.1 jmcneill stat->tx_control_frames += smb->tx_control_frames;
2217 1.1 jmcneill stat->tx_deferred += smb->tx_deferred;
2218 1.1 jmcneill stat->tx_bytes += smb->tx_bytes;
2219 1.1 jmcneill stat->tx_pkts_64 += smb->tx_pkts_64;
2220 1.1 jmcneill stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
2221 1.1 jmcneill stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
2222 1.1 jmcneill stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
2223 1.1 jmcneill stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
2224 1.1 jmcneill stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
2225 1.1 jmcneill stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
2226 1.1 jmcneill stat->tx_single_colls += smb->tx_single_colls;
2227 1.1 jmcneill stat->tx_multi_colls += smb->tx_multi_colls;
2228 1.1 jmcneill stat->tx_late_colls += smb->tx_late_colls;
2229 1.1 jmcneill stat->tx_excess_colls += smb->tx_excess_colls;
2230 1.1 jmcneill stat->tx_underrun += smb->tx_underrun;
2231 1.1 jmcneill stat->tx_desc_underrun += smb->tx_desc_underrun;
2232 1.1 jmcneill stat->tx_lenerrs += smb->tx_lenerrs;
2233 1.1 jmcneill stat->tx_pkts_truncated += smb->tx_pkts_truncated;
2234 1.1 jmcneill stat->tx_bcast_bytes += smb->tx_bcast_bytes;
2235 1.1 jmcneill stat->tx_mcast_bytes += smb->tx_mcast_bytes;
2236 1.1 jmcneill
2237 1.1 jmcneill /* Update counters in ifnet. */
2238 1.1 jmcneill ifp->if_opackets += smb->tx_frames;
2239 1.1 jmcneill
2240 1.1 jmcneill ifp->if_collisions += smb->tx_single_colls +
2241 1.1 jmcneill smb->tx_multi_colls * 2 + smb->tx_late_colls +
2242 1.15 leot smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT;
2243 1.1 jmcneill
2244 1.15 leot ifp->if_oerrors += smb->tx_late_colls + smb->tx_excess_colls +
2245 1.15 leot smb->tx_underrun + smb->tx_pkts_truncated;
2246 1.1 jmcneill
2247 1.1 jmcneill ifp->if_ipackets += smb->rx_frames;
2248 1.1 jmcneill
2249 1.1 jmcneill ifp->if_ierrors += smb->rx_crcerrs + smb->rx_lenerrs +
2250 1.1 jmcneill smb->rx_runts + smb->rx_pkts_truncated +
2251 1.1 jmcneill smb->rx_fifo_oflows + smb->rx_rrs_errs +
2252 1.1 jmcneill smb->rx_alignerrs;
2253 1.1 jmcneill
2254 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2255 1.1 jmcneill /* Update done, clear. */
2256 1.1 jmcneill smb->updated = 0;
2257 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2258 1.15 leot sc->alc_cdata.alc_smb_map->dm_mapsize,
2259 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2260 1.1 jmcneill }
2261 1.1 jmcneill }
2262 1.1 jmcneill
2263 1.1 jmcneill static int
2264 1.1 jmcneill alc_intr(void *arg)
2265 1.1 jmcneill {
2266 1.1 jmcneill struct alc_softc *sc = arg;
2267 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2268 1.1 jmcneill uint32_t status;
2269 1.1 jmcneill
2270 1.1 jmcneill status = CSR_READ_4(sc, ALC_INTR_STATUS);
2271 1.1 jmcneill if ((status & ALC_INTRS) == 0)
2272 1.1 jmcneill return (0);
2273 1.1 jmcneill
2274 1.1 jmcneill /* Acknowledge and disable interrupts. */
2275 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
2276 1.1 jmcneill
2277 1.1 jmcneill if (ifp->if_flags & IFF_RUNNING) {
2278 1.1 jmcneill if (status & INTR_RX_PKT) {
2279 1.1 jmcneill int error;
2280 1.1 jmcneill
2281 1.1 jmcneill error = alc_rxintr(sc);
2282 1.1 jmcneill if (error) {
2283 1.7 mrg alc_init_backend(ifp, false);
2284 1.1 jmcneill return (0);
2285 1.1 jmcneill }
2286 1.1 jmcneill }
2287 1.1 jmcneill
2288 1.1 jmcneill if (status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
2289 1.1 jmcneill INTR_TXQ_TO_RST)) {
2290 1.1 jmcneill if (status & INTR_DMA_RD_TO_RST)
2291 1.1 jmcneill printf("%s: DMA read error! -- resetting\n",
2292 1.1 jmcneill device_xname(sc->sc_dev));
2293 1.1 jmcneill if (status & INTR_DMA_WR_TO_RST)
2294 1.1 jmcneill printf("%s: DMA write error! -- resetting\n",
2295 1.1 jmcneill device_xname(sc->sc_dev));
2296 1.1 jmcneill if (status & INTR_TXQ_TO_RST)
2297 1.1 jmcneill printf("%s: TxQ reset! -- resetting\n",
2298 1.1 jmcneill device_xname(sc->sc_dev));
2299 1.7 mrg alc_init_backend(ifp, false);
2300 1.1 jmcneill return (0);
2301 1.1 jmcneill }
2302 1.1 jmcneill
2303 1.1 jmcneill alc_txeof(sc);
2304 1.22 ozaki if_schedule_deferred_start(ifp);
2305 1.1 jmcneill }
2306 1.1 jmcneill
2307 1.1 jmcneill /* Re-enable interrupts. */
2308 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
2309 1.1 jmcneill return (1);
2310 1.1 jmcneill }
2311 1.1 jmcneill
2312 1.1 jmcneill static void
2313 1.1 jmcneill alc_txeof(struct alc_softc *sc)
2314 1.1 jmcneill {
2315 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2316 1.1 jmcneill struct alc_txdesc *txd;
2317 1.1 jmcneill uint32_t cons, prod;
2318 1.1 jmcneill int prog;
2319 1.1 jmcneill
2320 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt == 0)
2321 1.1 jmcneill return;
2322 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
2323 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
2324 1.1 jmcneill BUS_DMASYNC_POSTREAD);
2325 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2326 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
2327 1.8 christos sc->alc_cdata.alc_cmb_map->dm_mapsize,
2328 1.1 jmcneill BUS_DMASYNC_POSTREAD);
2329 1.1 jmcneill prod = sc->alc_rdata.alc_cmb->cons;
2330 1.1 jmcneill } else
2331 1.1 jmcneill prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
2332 1.1 jmcneill /* Assume we're using normal Tx priority queue. */
2333 1.1 jmcneill prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
2334 1.1 jmcneill MBOX_TD_CONS_LO_IDX_SHIFT;
2335 1.1 jmcneill cons = sc->alc_cdata.alc_tx_cons;
2336 1.1 jmcneill /*
2337 1.1 jmcneill * Go through our Tx list and free mbufs for those
2338 1.1 jmcneill * frames which have been transmitted.
2339 1.1 jmcneill */
2340 1.1 jmcneill for (prog = 0; cons != prod; prog++,
2341 1.1 jmcneill ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
2342 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt <= 0)
2343 1.1 jmcneill break;
2344 1.1 jmcneill prog++;
2345 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
2346 1.1 jmcneill sc->alc_cdata.alc_tx_cnt--;
2347 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[cons];
2348 1.1 jmcneill if (txd->tx_m != NULL) {
2349 1.1 jmcneill /* Reclaim transmitted mbufs. */
2350 1.15 leot bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
2351 1.15 leot txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2352 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
2353 1.1 jmcneill m_freem(txd->tx_m);
2354 1.1 jmcneill txd->tx_m = NULL;
2355 1.1 jmcneill }
2356 1.1 jmcneill }
2357 1.1 jmcneill
2358 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
2359 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
2360 1.15 leot sc->alc_cdata.alc_cmb_map->dm_mapsize, BUS_DMASYNC_PREREAD);
2361 1.1 jmcneill sc->alc_cdata.alc_tx_cons = cons;
2362 1.1 jmcneill /*
2363 1.1 jmcneill * Unarm watchdog timer only when there is no pending
2364 1.1 jmcneill * frames in Tx queue.
2365 1.1 jmcneill */
2366 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt == 0)
2367 1.1 jmcneill ifp->if_timer = 0;
2368 1.1 jmcneill }
2369 1.1 jmcneill
2370 1.1 jmcneill static int
2371 1.7 mrg alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd, bool init)
2372 1.1 jmcneill {
2373 1.1 jmcneill struct mbuf *m;
2374 1.1 jmcneill bus_dmamap_t map;
2375 1.1 jmcneill int error;
2376 1.1 jmcneill
2377 1.1 jmcneill MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
2378 1.1 jmcneill if (m == NULL)
2379 1.1 jmcneill return (ENOBUFS);
2380 1.1 jmcneill MCLGET(m, init ? M_WAITOK : M_DONTWAIT);
2381 1.1 jmcneill if (!(m->m_flags & M_EXT)) {
2382 1.1 jmcneill m_freem(m);
2383 1.1 jmcneill return (ENOBUFS);
2384 1.1 jmcneill }
2385 1.1 jmcneill
2386 1.1 jmcneill m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
2387 1.1 jmcneill
2388 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat,
2389 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap, m, BUS_DMA_NOWAIT);
2390 1.1 jmcneill
2391 1.1 jmcneill if (error != 0) {
2392 1.1 jmcneill m_freem(m);
2393 1.1 jmcneill
2394 1.1 jmcneill if (init)
2395 1.1 jmcneill printf("%s: can't load RX mbuf\n", device_xname(sc->sc_dev));
2396 1.1 jmcneill
2397 1.1 jmcneill return (error);
2398 1.1 jmcneill }
2399 1.1 jmcneill
2400 1.1 jmcneill if (rxd->rx_m != NULL) {
2401 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
2402 1.1 jmcneill rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2403 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
2404 1.1 jmcneill }
2405 1.1 jmcneill map = rxd->rx_dmamap;
2406 1.1 jmcneill rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
2407 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap = map;
2408 1.15 leot bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0, rxd->rx_dmamap->dm_mapsize,
2409 1.15 leot BUS_DMASYNC_PREREAD);
2410 1.1 jmcneill rxd->rx_m = m;
2411 1.1 jmcneill rxd->rx_desc->addr = htole64(rxd->rx_dmamap->dm_segs[0].ds_addr);
2412 1.1 jmcneill return (0);
2413 1.1 jmcneill }
2414 1.1 jmcneill
2415 1.1 jmcneill static int
2416 1.1 jmcneill alc_rxintr(struct alc_softc *sc)
2417 1.1 jmcneill {
2418 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2419 1.1 jmcneill struct rx_rdesc *rrd;
2420 1.1 jmcneill uint32_t nsegs, status;
2421 1.1 jmcneill int rr_cons, prog;
2422 1.1 jmcneill
2423 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
2424 1.15 leot sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
2425 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2426 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
2427 1.15 leot sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
2428 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2429 1.1 jmcneill rr_cons = sc->alc_cdata.alc_rr_cons;
2430 1.1 jmcneill for (prog = 0; (ifp->if_flags & IFF_RUNNING) != 0;) {
2431 1.1 jmcneill rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
2432 1.1 jmcneill status = le32toh(rrd->status);
2433 1.1 jmcneill if ((status & RRD_VALID) == 0)
2434 1.1 jmcneill break;
2435 1.1 jmcneill nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
2436 1.1 jmcneill if (nsegs == 0) {
2437 1.1 jmcneill /* This should not happen! */
2438 1.1 jmcneill if (alcdebug)
2439 1.1 jmcneill printf("%s: unexpected segment count -- "
2440 1.1 jmcneill "resetting\n", device_xname(sc->sc_dev));
2441 1.1 jmcneill return (EIO);
2442 1.1 jmcneill }
2443 1.1 jmcneill alc_rxeof(sc, rrd);
2444 1.1 jmcneill /* Clear Rx return status. */
2445 1.1 jmcneill rrd->status = 0;
2446 1.1 jmcneill ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
2447 1.1 jmcneill sc->alc_cdata.alc_rx_cons += nsegs;
2448 1.1 jmcneill sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
2449 1.1 jmcneill prog += nsegs;
2450 1.1 jmcneill }
2451 1.1 jmcneill
2452 1.1 jmcneill if (prog > 0) {
2453 1.1 jmcneill /* Update the consumer index. */
2454 1.1 jmcneill sc->alc_cdata.alc_rr_cons = rr_cons;
2455 1.1 jmcneill /* Sync Rx return descriptors. */
2456 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
2457 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
2458 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2459 1.1 jmcneill /*
2460 1.1 jmcneill * Sync updated Rx descriptors such that controller see
2461 1.1 jmcneill * modified buffer addresses.
2462 1.1 jmcneill */
2463 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
2464 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
2465 1.1 jmcneill BUS_DMASYNC_PREWRITE);
2466 1.1 jmcneill /*
2467 1.1 jmcneill * Let controller know availability of new Rx buffers.
2468 1.1 jmcneill * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
2469 1.1 jmcneill * it may be possible to update ALC_MBOX_RD0_PROD_IDX
2470 1.1 jmcneill * only when Rx buffer pre-fetching is required. In
2471 1.1 jmcneill * addition we already set ALC_RX_RD_FREE_THRESH to
2472 1.1 jmcneill * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
2473 1.1 jmcneill * it still seems that pre-fetching needs more
2474 1.1 jmcneill * experimentation.
2475 1.1 jmcneill */
2476 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
2477 1.1 jmcneill sc->alc_cdata.alc_rx_cons);
2478 1.1 jmcneill }
2479 1.1 jmcneill
2480 1.1 jmcneill return (0);
2481 1.1 jmcneill }
2482 1.1 jmcneill
2483 1.1 jmcneill /* Receive a frame. */
2484 1.1 jmcneill static void
2485 1.1 jmcneill alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
2486 1.1 jmcneill {
2487 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2488 1.1 jmcneill struct alc_rxdesc *rxd;
2489 1.1 jmcneill struct mbuf *mp, *m;
2490 1.1 jmcneill uint32_t rdinfo, status;
2491 1.1 jmcneill int count, nsegs, rx_cons;
2492 1.1 jmcneill
2493 1.1 jmcneill status = le32toh(rrd->status);
2494 1.1 jmcneill rdinfo = le32toh(rrd->rdinfo);
2495 1.1 jmcneill rx_cons = RRD_RD_IDX(rdinfo);
2496 1.1 jmcneill nsegs = RRD_RD_CNT(rdinfo);
2497 1.1 jmcneill
2498 1.1 jmcneill sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
2499 1.1 jmcneill if (status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) {
2500 1.1 jmcneill /*
2501 1.1 jmcneill * We want to pass the following frames to upper
2502 1.1 jmcneill * layer regardless of error status of Rx return
2503 1.1 jmcneill * ring.
2504 1.1 jmcneill *
2505 1.1 jmcneill * o IP/TCP/UDP checksum is bad.
2506 1.1 jmcneill * o frame length and protocol specific length
2507 1.1 jmcneill * does not match.
2508 1.1 jmcneill *
2509 1.1 jmcneill * Force network stack compute checksum for
2510 1.1 jmcneill * errored frames.
2511 1.1 jmcneill */
2512 1.1 jmcneill status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
2513 1.2 jmcneill if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
2514 1.2 jmcneill RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
2515 1.1 jmcneill return;
2516 1.1 jmcneill }
2517 1.1 jmcneill
2518 1.1 jmcneill for (count = 0; count < nsegs; count++,
2519 1.1 jmcneill ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
2520 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
2521 1.1 jmcneill mp = rxd->rx_m;
2522 1.1 jmcneill /* Add a new receive buffer to the ring. */
2523 1.7 mrg if (alc_newbuf(sc, rxd, false) != 0) {
2524 1.1 jmcneill ifp->if_iqdrops++;
2525 1.1 jmcneill /* Reuse Rx buffers. */
2526 1.1 jmcneill if (sc->alc_cdata.alc_rxhead != NULL)
2527 1.1 jmcneill m_freem(sc->alc_cdata.alc_rxhead);
2528 1.1 jmcneill break;
2529 1.1 jmcneill }
2530 1.1 jmcneill
2531 1.1 jmcneill /*
2532 1.1 jmcneill * Assume we've received a full sized frame.
2533 1.1 jmcneill * Actual size is fixed when we encounter the end of
2534 1.1 jmcneill * multi-segmented frame.
2535 1.1 jmcneill */
2536 1.1 jmcneill mp->m_len = sc->alc_buf_size;
2537 1.1 jmcneill
2538 1.1 jmcneill /* Chain received mbufs. */
2539 1.1 jmcneill if (sc->alc_cdata.alc_rxhead == NULL) {
2540 1.1 jmcneill sc->alc_cdata.alc_rxhead = mp;
2541 1.1 jmcneill sc->alc_cdata.alc_rxtail = mp;
2542 1.1 jmcneill } else {
2543 1.1 jmcneill mp->m_flags &= ~M_PKTHDR;
2544 1.1 jmcneill sc->alc_cdata.alc_rxprev_tail =
2545 1.1 jmcneill sc->alc_cdata.alc_rxtail;
2546 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_next = mp;
2547 1.1 jmcneill sc->alc_cdata.alc_rxtail = mp;
2548 1.1 jmcneill }
2549 1.1 jmcneill
2550 1.1 jmcneill if (count == nsegs - 1) {
2551 1.1 jmcneill /* Last desc. for this frame. */
2552 1.1 jmcneill m = sc->alc_cdata.alc_rxhead;
2553 1.1 jmcneill m->m_flags |= M_PKTHDR;
2554 1.1 jmcneill /*
2555 1.1 jmcneill * It seems that L1C/L2C controller has no way
2556 1.1 jmcneill * to tell hardware to strip CRC bytes.
2557 1.1 jmcneill */
2558 1.1 jmcneill m->m_pkthdr.len =
2559 1.1 jmcneill sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
2560 1.1 jmcneill if (nsegs > 1) {
2561 1.1 jmcneill /* Set last mbuf size. */
2562 1.1 jmcneill mp->m_len = sc->alc_cdata.alc_rxlen -
2563 1.1 jmcneill (nsegs - 1) * sc->alc_buf_size;
2564 1.1 jmcneill /* Remove the CRC bytes in chained mbufs. */
2565 1.1 jmcneill if (mp->m_len <= ETHER_CRC_LEN) {
2566 1.1 jmcneill sc->alc_cdata.alc_rxtail =
2567 1.1 jmcneill sc->alc_cdata.alc_rxprev_tail;
2568 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_len -=
2569 1.1 jmcneill (ETHER_CRC_LEN - mp->m_len);
2570 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_next = NULL;
2571 1.1 jmcneill m_freem(mp);
2572 1.1 jmcneill } else {
2573 1.1 jmcneill mp->m_len -= ETHER_CRC_LEN;
2574 1.1 jmcneill }
2575 1.1 jmcneill } else
2576 1.1 jmcneill m->m_len = m->m_pkthdr.len;
2577 1.21 ozaki m_set_rcvif(m, ifp);
2578 1.1 jmcneill #if NVLAN > 0
2579 1.1 jmcneill /*
2580 1.1 jmcneill * Due to hardware bugs, Rx checksum offloading
2581 1.1 jmcneill * was intentionally disabled.
2582 1.1 jmcneill */
2583 1.1 jmcneill if (status & RRD_VLAN_TAG) {
2584 1.1 jmcneill u_int32_t vtag = RRD_VLAN(le32toh(rrd->vtag));
2585 1.1 jmcneill VLAN_INPUT_TAG(ifp, m, ntohs(vtag), );
2586 1.1 jmcneill }
2587 1.1 jmcneill #endif
2588 1.1 jmcneill
2589 1.1 jmcneill bpf_mtap(ifp, m);
2590 1.1 jmcneill
2591 1.1 jmcneill /* Pass it on. */
2592 1.19 ozaki if_percpuq_enqueue(ifp->if_percpuq, m);
2593 1.1 jmcneill }
2594 1.1 jmcneill }
2595 1.1 jmcneill /* Reset mbuf chains. */
2596 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
2597 1.1 jmcneill }
2598 1.1 jmcneill
2599 1.1 jmcneill static void
2600 1.1 jmcneill alc_tick(void *xsc)
2601 1.1 jmcneill {
2602 1.1 jmcneill struct alc_softc *sc = xsc;
2603 1.1 jmcneill struct mii_data *mii = &sc->sc_miibus;
2604 1.1 jmcneill int s;
2605 1.1 jmcneill
2606 1.1 jmcneill s = splnet();
2607 1.1 jmcneill mii_tick(mii);
2608 1.1 jmcneill alc_stats_update(sc);
2609 1.1 jmcneill splx(s);
2610 1.1 jmcneill
2611 1.1 jmcneill callout_schedule(&sc->sc_tick_ch, hz);
2612 1.1 jmcneill }
2613 1.1 jmcneill
2614 1.1 jmcneill static void
2615 1.12 christos alc_osc_reset(struct alc_softc *sc)
2616 1.12 christos {
2617 1.12 christos uint32_t reg;
2618 1.12 christos
2619 1.12 christos reg = CSR_READ_4(sc, ALC_MISC3);
2620 1.12 christos reg &= ~MISC3_25M_BY_SW;
2621 1.12 christos reg |= MISC3_25M_NOTO_INTNL;
2622 1.12 christos CSR_WRITE_4(sc, ALC_MISC3, reg);
2623 1.12 christos
2624 1.12 christos reg = CSR_READ_4(sc, ALC_MISC);
2625 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0) {
2626 1.12 christos /*
2627 1.12 christos * Restore over-current protection default value.
2628 1.12 christos * This value could be reset by MAC reset.
2629 1.12 christos */
2630 1.12 christos reg &= ~MISC_PSW_OCP_MASK;
2631 1.12 christos reg |= (MISC_PSW_OCP_DEFAULT << MISC_PSW_OCP_SHIFT);
2632 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2633 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2634 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
2635 1.12 christos reg = CSR_READ_4(sc, ALC_MISC2);
2636 1.12 christos reg &= ~MISC2_CALB_START;
2637 1.12 christos CSR_WRITE_4(sc, ALC_MISC2, reg);
2638 1.12 christos CSR_WRITE_4(sc, ALC_MISC2, reg | MISC2_CALB_START);
2639 1.12 christos
2640 1.12 christos } else {
2641 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2642 1.12 christos /* Disable isolate for revision A devices. */
2643 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
2644 1.12 christos reg &= ~MISC_ISO_ENB;
2645 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
2646 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2647 1.12 christos }
2648 1.12 christos
2649 1.12 christos DELAY(20);
2650 1.12 christos }
2651 1.12 christos
2652 1.12 christos static void
2653 1.1 jmcneill alc_reset(struct alc_softc *sc)
2654 1.1 jmcneill {
2655 1.12 christos uint32_t pmcfg, reg;
2656 1.1 jmcneill int i;
2657 1.1 jmcneill
2658 1.12 christos pmcfg = 0;
2659 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2660 1.12 christos /* Reset workaround. */
2661 1.12 christos CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, 1);
2662 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
2663 1.12 christos (sc->alc_rev & 0x01) != 0) {
2664 1.12 christos /* Disable L0s/L1s before reset. */
2665 1.12 christos pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
2666 1.12 christos if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
2667 1.12 christos != 0) {
2668 1.12 christos pmcfg &= ~(PM_CFG_ASPM_L0S_ENB |
2669 1.12 christos PM_CFG_ASPM_L1_ENB);
2670 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
2671 1.12 christos }
2672 1.12 christos }
2673 1.12 christos }
2674 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2675 1.2 jmcneill reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
2676 1.2 jmcneill CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2677 1.12 christos
2678 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2679 1.12 christos for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2680 1.12 christos DELAY(10);
2681 1.12 christos if (CSR_READ_4(sc, ALC_MBOX_RD0_PROD_IDX) == 0)
2682 1.12 christos break;
2683 1.12 christos }
2684 1.12 christos if (i == 0)
2685 1.12 christos printf("%s: MAC reset timeout!\n", device_xname(sc->sc_dev));
2686 1.12 christos }
2687 1.1 jmcneill for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2688 1.1 jmcneill DELAY(10);
2689 1.1 jmcneill if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
2690 1.1 jmcneill break;
2691 1.1 jmcneill }
2692 1.1 jmcneill if (i == 0)
2693 1.1 jmcneill printf("%s: master reset timeout!\n", device_xname(sc->sc_dev));
2694 1.1 jmcneill
2695 1.1 jmcneill for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2696 1.12 christos reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
2697 1.12 christos if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC |
2698 1.12 christos IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
2699 1.1 jmcneill break;
2700 1.1 jmcneill DELAY(10);
2701 1.1 jmcneill }
2702 1.12 christos if (i == 0)
2703 1.12 christos printf("%s: reset timeout(0x%08x)!\n",
2704 1.12 christos device_xname(sc->sc_dev), reg);
2705 1.1 jmcneill
2706 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2707 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
2708 1.12 christos (sc->alc_rev & 0x01) != 0) {
2709 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2710 1.12 christos reg |= MASTER_CLK_SEL_DIS;
2711 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2712 1.12 christos /* Restore L0s/L1s config. */
2713 1.12 christos if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
2714 1.12 christos != 0)
2715 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
2716 1.12 christos }
2717 1.12 christos
2718 1.12 christos alc_osc_reset(sc);
2719 1.12 christos reg = CSR_READ_4(sc, ALC_MISC3);
2720 1.12 christos reg &= ~MISC3_25M_BY_SW;
2721 1.12 christos reg |= MISC3_25M_NOTO_INTNL;
2722 1.12 christos CSR_WRITE_4(sc, ALC_MISC3, reg);
2723 1.12 christos reg = CSR_READ_4(sc, ALC_MISC);
2724 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2725 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
2726 1.12 christos reg &= ~MISC_ISO_ENB;
2727 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2728 1.12 christos DELAY(20);
2729 1.12 christos }
2730 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
2731 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
2732 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2)
2733 1.12 christos CSR_WRITE_4(sc, ALC_SERDES_LOCK,
2734 1.12 christos CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
2735 1.12 christos SERDES_PHY_CLK_SLOWDOWN);
2736 1.1 jmcneill }
2737 1.1 jmcneill
2738 1.1 jmcneill static int
2739 1.1 jmcneill alc_init(struct ifnet *ifp)
2740 1.1 jmcneill {
2741 1.8 christos
2742 1.7 mrg return alc_init_backend(ifp, true);
2743 1.7 mrg }
2744 1.7 mrg
2745 1.7 mrg static int
2746 1.7 mrg alc_init_backend(struct ifnet *ifp, bool init)
2747 1.7 mrg {
2748 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
2749 1.1 jmcneill struct mii_data *mii;
2750 1.1 jmcneill uint8_t eaddr[ETHER_ADDR_LEN];
2751 1.1 jmcneill bus_addr_t paddr;
2752 1.1 jmcneill uint32_t reg, rxf_hi, rxf_lo;
2753 1.1 jmcneill int error;
2754 1.1 jmcneill
2755 1.1 jmcneill /*
2756 1.1 jmcneill * Cancel any pending I/O.
2757 1.1 jmcneill */
2758 1.1 jmcneill alc_stop(ifp, 0);
2759 1.1 jmcneill /*
2760 1.1 jmcneill * Reset the chip to a known state.
2761 1.1 jmcneill */
2762 1.1 jmcneill alc_reset(sc);
2763 1.1 jmcneill
2764 1.1 jmcneill /* Initialize Rx descriptors. */
2765 1.7 mrg error = alc_init_rx_ring(sc, init);
2766 1.1 jmcneill if (error != 0) {
2767 1.1 jmcneill printf("%s: no memory for Rx buffers.\n", device_xname(sc->sc_dev));
2768 1.1 jmcneill alc_stop(ifp, 0);
2769 1.1 jmcneill return (error);
2770 1.1 jmcneill }
2771 1.1 jmcneill alc_init_rr_ring(sc);
2772 1.1 jmcneill alc_init_tx_ring(sc);
2773 1.1 jmcneill alc_init_cmb(sc);
2774 1.1 jmcneill alc_init_smb(sc);
2775 1.1 jmcneill
2776 1.2 jmcneill /* Enable all clocks. */
2777 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2778 1.12 christos CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, CLK_GATING_DMAW_ENB |
2779 1.12 christos CLK_GATING_DMAR_ENB | CLK_GATING_TXQ_ENB |
2780 1.12 christos CLK_GATING_RXQ_ENB | CLK_GATING_TXMAC_ENB |
2781 1.12 christos CLK_GATING_RXMAC_ENB);
2782 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0)
2783 1.12 christos CSR_WRITE_4(sc, ALC_IDLE_DECISN_TIMER,
2784 1.12 christos IDLE_DECISN_TIMER_DEFAULT_1MS);
2785 1.12 christos } else
2786 1.12 christos CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
2787 1.12 christos
2788 1.2 jmcneill
2789 1.1 jmcneill /* Reprogram the station address. */
2790 1.1 jmcneill memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
2791 1.1 jmcneill CSR_WRITE_4(sc, ALC_PAR0,
2792 1.1 jmcneill eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
2793 1.1 jmcneill CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
2794 1.1 jmcneill /*
2795 1.1 jmcneill * Clear WOL status and disable all WOL feature as WOL
2796 1.1 jmcneill * would interfere Rx operation under normal environments.
2797 1.1 jmcneill */
2798 1.1 jmcneill CSR_READ_4(sc, ALC_WOL_CFG);
2799 1.1 jmcneill CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2800 1.1 jmcneill /* Set Tx descriptor base addresses. */
2801 1.1 jmcneill paddr = sc->alc_rdata.alc_tx_ring_paddr;
2802 1.1 jmcneill CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2803 1.1 jmcneill CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2804 1.1 jmcneill /* We don't use high priority ring. */
2805 1.1 jmcneill CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
2806 1.1 jmcneill /* Set Tx descriptor counter. */
2807 1.1 jmcneill CSR_WRITE_4(sc, ALC_TD_RING_CNT,
2808 1.1 jmcneill (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
2809 1.1 jmcneill /* Set Rx descriptor base addresses. */
2810 1.1 jmcneill paddr = sc->alc_rdata.alc_rx_ring_paddr;
2811 1.1 jmcneill CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2812 1.1 jmcneill CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2813 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2814 1.12 christos /* We use one Rx ring. */
2815 1.12 christos CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
2816 1.12 christos CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
2817 1.12 christos CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
2818 1.12 christos }
2819 1.1 jmcneill /* Set Rx descriptor counter. */
2820 1.1 jmcneill CSR_WRITE_4(sc, ALC_RD_RING_CNT,
2821 1.1 jmcneill (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
2822 1.1 jmcneill
2823 1.1 jmcneill /*
2824 1.1 jmcneill * Let hardware split jumbo frames into alc_max_buf_sized chunks.
2825 1.1 jmcneill * if it do not fit the buffer size. Rx return descriptor holds
2826 1.1 jmcneill * a counter that indicates how many fragments were made by the
2827 1.1 jmcneill * hardware. The buffer size should be multiple of 8 bytes.
2828 1.1 jmcneill * Since hardware has limit on the size of buffer size, always
2829 1.1 jmcneill * use the maximum value.
2830 1.1 jmcneill * For strict-alignment architectures make sure to reduce buffer
2831 1.1 jmcneill * size by 8 bytes to make room for alignment fixup.
2832 1.1 jmcneill */
2833 1.1 jmcneill sc->alc_buf_size = RX_BUF_SIZE_MAX;
2834 1.1 jmcneill CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
2835 1.1 jmcneill
2836 1.1 jmcneill paddr = sc->alc_rdata.alc_rr_ring_paddr;
2837 1.1 jmcneill /* Set Rx return descriptor base addresses. */
2838 1.1 jmcneill CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2839 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2840 1.12 christos /* We use one Rx return ring. */
2841 1.12 christos CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
2842 1.12 christos CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
2843 1.12 christos CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
2844 1.12 christos }\
2845 1.1 jmcneill /* Set Rx return descriptor counter. */
2846 1.1 jmcneill CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
2847 1.1 jmcneill (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
2848 1.1 jmcneill paddr = sc->alc_rdata.alc_cmb_paddr;
2849 1.1 jmcneill CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
2850 1.1 jmcneill paddr = sc->alc_rdata.alc_smb_paddr;
2851 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2852 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
2853 1.1 jmcneill
2854 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B) {
2855 1.2 jmcneill /* Reconfigure SRAM - Vendor magic. */
2856 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
2857 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
2858 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
2859 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
2860 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
2861 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
2862 1.2 jmcneill CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
2863 1.2 jmcneill CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
2864 1.2 jmcneill }
2865 1.2 jmcneill
2866 1.1 jmcneill /* Tell hardware that we're ready to load DMA blocks. */
2867 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
2868 1.1 jmcneill
2869 1.1 jmcneill /* Configure interrupt moderation timer. */
2870 1.1 jmcneill sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
2871 1.1 jmcneill sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
2872 1.1 jmcneill reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
2873 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0)
2874 1.12 christos reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
2875 1.1 jmcneill CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
2876 1.1 jmcneill /*
2877 1.1 jmcneill * We don't want to automatic interrupt clear as task queue
2878 1.1 jmcneill * for the interrupt should know interrupt status.
2879 1.1 jmcneill */
2880 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2881 1.12 christos reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
2882 1.12 christos reg |= MASTER_SA_TIMER_ENB;
2883 1.1 jmcneill if (ALC_USECS(sc->alc_int_rx_mod) != 0)
2884 1.1 jmcneill reg |= MASTER_IM_RX_TIMER_ENB;
2885 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0 &&
2886 1.12 christos ALC_USECS(sc->alc_int_tx_mod) != 0)
2887 1.1 jmcneill reg |= MASTER_IM_TX_TIMER_ENB;
2888 1.1 jmcneill CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2889 1.1 jmcneill /*
2890 1.1 jmcneill * Disable interrupt re-trigger timer. We don't want automatic
2891 1.1 jmcneill * re-triggering of un-ACKed interrupts.
2892 1.1 jmcneill */
2893 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
2894 1.1 jmcneill /* Configure CMB. */
2895 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2896 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, ALC_TX_RING_CNT / 3);
2897 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER,
2898 1.12 christos ALC_USECS(sc->alc_int_tx_mod));
2899 1.12 christos } else {
2900 1.12 christos if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2901 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
2902 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
2903 1.12 christos } else
2904 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
2905 1.12 christos }
2906 1.1 jmcneill /*
2907 1.1 jmcneill * Hardware can be configured to issue SMB interrupt based
2908 1.1 jmcneill * on programmed interval. Since there is a callout that is
2909 1.1 jmcneill * invoked for every hz in driver we use that instead of
2910 1.1 jmcneill * relying on periodic SMB interrupt.
2911 1.1 jmcneill */
2912 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
2913 1.1 jmcneill /* Clear MAC statistics. */
2914 1.1 jmcneill alc_stats_clear(sc);
2915 1.1 jmcneill
2916 1.1 jmcneill /*
2917 1.1 jmcneill * Always use maximum frame size that controller can support.
2918 1.1 jmcneill * Otherwise received frames that has larger frame length
2919 1.1 jmcneill * than alc(4) MTU would be silently dropped in hardware. This
2920 1.1 jmcneill * would make path-MTU discovery hard as sender wouldn't get
2921 1.1 jmcneill * any responses from receiver. alc(4) supports
2922 1.1 jmcneill * multi-fragmented frames on Rx path so it has no issue on
2923 1.1 jmcneill * assembling fragmented frames. Using maximum frame size also
2924 1.1 jmcneill * removes the need to reinitialize hardware when interface
2925 1.1 jmcneill * MTU configuration was changed.
2926 1.1 jmcneill *
2927 1.1 jmcneill * Be conservative in what you do, be liberal in what you
2928 1.1 jmcneill * accept from others - RFC 793.
2929 1.1 jmcneill */
2930 1.2 jmcneill CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
2931 1.1 jmcneill
2932 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2933 1.12 christos /* Disable header split(?) */
2934 1.12 christos CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
2935 1.12 christos
2936 1.12 christos /* Configure IPG/IFG parameters. */
2937 1.12 christos CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
2938 1.12 christos ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) &
2939 1.12 christos IPG_IFG_IPGT_MASK) |
2940 1.12 christos ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) &
2941 1.12 christos IPG_IFG_MIFG_MASK) |
2942 1.12 christos ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) &
2943 1.12 christos IPG_IFG_IPG1_MASK) |
2944 1.12 christos ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) &
2945 1.12 christos IPG_IFG_IPG2_MASK));
2946 1.12 christos /* Set parameters for half-duplex media. */
2947 1.12 christos CSR_WRITE_4(sc, ALC_HDPX_CFG,
2948 1.12 christos ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
2949 1.12 christos HDPX_CFG_LCOL_MASK) |
2950 1.12 christos ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
2951 1.12 christos HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
2952 1.12 christos ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
2953 1.12 christos HDPX_CFG_ABEBT_MASK) |
2954 1.12 christos ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
2955 1.12 christos HDPX_CFG_JAMIPG_MASK));
2956 1.12 christos }
2957 1.1 jmcneill
2958 1.1 jmcneill /*
2959 1.1 jmcneill * Set TSO/checksum offload threshold. For frames that is
2960 1.1 jmcneill * larger than this threshold, hardware wouldn't do
2961 1.1 jmcneill * TSO/checksum offloading.
2962 1.1 jmcneill */
2963 1.12 christos reg = (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
2964 1.12 christos TSO_OFFLOAD_THRESH_MASK;
2965 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2966 1.12 christos reg |= TSO_OFFLOAD_ERRLGPKT_DROP_ENB;
2967 1.12 christos CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH, reg);
2968 1.1 jmcneill /* Configure TxQ. */
2969 1.1 jmcneill reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
2970 1.1 jmcneill TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
2971 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
2972 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
2973 1.2 jmcneill reg >>= 1;
2974 1.1 jmcneill reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
2975 1.1 jmcneill TXQ_CFG_TD_BURST_MASK;
2976 1.12 christos reg |= TXQ_CFG_IP_OPTION_ENB | TXQ_CFG_8023_ENB;
2977 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
2978 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2979 1.12 christos reg = (TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q1_BURST_SHIFT |
2980 1.12 christos TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q2_BURST_SHIFT |
2981 1.12 christos TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q3_BURST_SHIFT |
2982 1.12 christos HQTD_CFG_BURST_ENB);
2983 1.12 christos CSR_WRITE_4(sc, ALC_HQTD_CFG, reg);
2984 1.12 christos reg = WRR_PRI_RESTRICT_NONE;
2985 1.12 christos reg |= (WRR_PRI_DEFAULT << WRR_PRI0_SHIFT |
2986 1.12 christos WRR_PRI_DEFAULT << WRR_PRI1_SHIFT |
2987 1.12 christos WRR_PRI_DEFAULT << WRR_PRI2_SHIFT |
2988 1.12 christos WRR_PRI_DEFAULT << WRR_PRI3_SHIFT);
2989 1.12 christos CSR_WRITE_4(sc, ALC_WRR, reg);
2990 1.12 christos } else {
2991 1.12 christos /* Configure Rx free descriptor pre-fetching. */
2992 1.12 christos CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
2993 1.12 christos ((RX_RD_FREE_THRESH_HI_DEFAULT <<
2994 1.12 christos RX_RD_FREE_THRESH_HI_SHIFT) & RX_RD_FREE_THRESH_HI_MASK) |
2995 1.12 christos ((RX_RD_FREE_THRESH_LO_DEFAULT <<
2996 1.12 christos RX_RD_FREE_THRESH_LO_SHIFT) & RX_RD_FREE_THRESH_LO_MASK));
2997 1.12 christos }
2998 1.1 jmcneill
2999 1.1 jmcneill /*
3000 1.1 jmcneill * Configure flow control parameters.
3001 1.1 jmcneill * XON : 80% of Rx FIFO
3002 1.1 jmcneill * XOFF : 30% of Rx FIFO
3003 1.1 jmcneill */
3004 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3005 1.12 christos reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
3006 1.12 christos reg &= SRAM_RX_FIFO_LEN_MASK;
3007 1.12 christos reg *= 8;
3008 1.12 christos if (reg > 8 * 1024)
3009 1.12 christos reg -= RX_FIFO_PAUSE_816X_RSVD;
3010 1.12 christos else
3011 1.12 christos reg -= RX_BUF_SIZE_MAX;
3012 1.12 christos reg /= 8;
3013 1.12 christos CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3014 1.12 christos ((reg << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3015 1.12 christos RX_FIFO_PAUSE_THRESH_LO_MASK) |
3016 1.12 christos (((RX_FIFO_PAUSE_816X_RSVD / 8) <<
3017 1.12 christos RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3018 1.12 christos RX_FIFO_PAUSE_THRESH_HI_MASK));
3019 1.12 christos } else if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8131 ||
3020 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8132) {
3021 1.2 jmcneill reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
3022 1.2 jmcneill rxf_hi = (reg * 8) / 10;
3023 1.2 jmcneill rxf_lo = (reg * 3) / 10;
3024 1.2 jmcneill CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3025 1.2 jmcneill ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3026 1.2 jmcneill RX_FIFO_PAUSE_THRESH_LO_MASK) |
3027 1.2 jmcneill ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3028 1.2 jmcneill RX_FIFO_PAUSE_THRESH_HI_MASK));
3029 1.2 jmcneill }
3030 1.2 jmcneill
3031 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3032 1.12 christos /* Disable RSS until I understand L1C/L2C's RSS logic. */
3033 1.12 christos CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
3034 1.12 christos CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
3035 1.12 christos }
3036 1.1 jmcneill
3037 1.1 jmcneill /* Configure RxQ. */
3038 1.1 jmcneill reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
3039 1.1 jmcneill RXQ_CFG_RD_BURST_MASK;
3040 1.1 jmcneill reg |= RXQ_CFG_RSS_MODE_DIS;
3041 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3042 1.12 christos reg |= (RXQ_CFG_816X_IDT_TBL_SIZE_DEFAULT <<
3043 1.12 christos RXQ_CFG_816X_IDT_TBL_SIZE_SHIFT) &
3044 1.12 christos RXQ_CFG_816X_IDT_TBL_SIZE_MASK;
3045 1.12 christos if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0 &&
3046 1.12 christos sc->alc_ident->deviceid != PCI_PRODUCT_ATTANSIC_AR8151_V2)
3047 1.12 christos reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_1M;
3048 1.1 jmcneill CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3049 1.1 jmcneill
3050 1.1 jmcneill /* Configure DMA parameters. */
3051 1.1 jmcneill reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
3052 1.1 jmcneill reg |= sc->alc_rcb;
3053 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3054 1.1 jmcneill reg |= DMA_CFG_CMB_ENB;
3055 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
3056 1.1 jmcneill reg |= DMA_CFG_SMB_ENB;
3057 1.1 jmcneill else
3058 1.1 jmcneill reg |= DMA_CFG_SMB_DIS;
3059 1.1 jmcneill reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
3060 1.1 jmcneill DMA_CFG_RD_BURST_SHIFT;
3061 1.1 jmcneill reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
3062 1.1 jmcneill DMA_CFG_WR_BURST_SHIFT;
3063 1.1 jmcneill reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
3064 1.1 jmcneill DMA_CFG_RD_DELAY_CNT_MASK;
3065 1.1 jmcneill reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
3066 1.1 jmcneill DMA_CFG_WR_DELAY_CNT_MASK;
3067 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3068 1.12 christos switch (AR816X_REV(sc->alc_rev)) {
3069 1.12 christos case AR816X_REV_A0:
3070 1.12 christos case AR816X_REV_A1:
3071 1.12 christos reg |= DMA_CFG_RD_CHNL_SEL_1;
3072 1.12 christos break;
3073 1.12 christos case AR816X_REV_B0:
3074 1.12 christos /* FALLTHROUGH */
3075 1.12 christos default:
3076 1.12 christos reg |= DMA_CFG_RD_CHNL_SEL_3;
3077 1.12 christos break;
3078 1.12 christos }
3079 1.12 christos }
3080 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3081 1.1 jmcneill
3082 1.1 jmcneill /*
3083 1.1 jmcneill * Configure Tx/Rx MACs.
3084 1.1 jmcneill * - Auto-padding for short frames.
3085 1.1 jmcneill * - Enable CRC generation.
3086 1.1 jmcneill * Actual reconfiguration of MAC for resolved speed/duplex
3087 1.1 jmcneill * is followed after detection of link establishment.
3088 1.2 jmcneill * AR813x/AR815x always does checksum computation regardless
3089 1.1 jmcneill * of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
3090 1.1 jmcneill * have bug in protocol field in Rx return structure so
3091 1.1 jmcneill * these controllers can't handle fragmented frames. Disable
3092 1.1 jmcneill * Rx checksum offloading until there is a newer controller
3093 1.1 jmcneill * that has sane implementation.
3094 1.1 jmcneill */
3095 1.1 jmcneill reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
3096 1.1 jmcneill ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
3097 1.1 jmcneill MAC_CFG_PREAMBLE_MASK);
3098 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3099 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
3100 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
3101 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
3102 1.2 jmcneill reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3103 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
3104 1.1 jmcneill reg |= MAC_CFG_SPEED_10_100;
3105 1.1 jmcneill else
3106 1.1 jmcneill reg |= MAC_CFG_SPEED_1000;
3107 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3108 1.1 jmcneill
3109 1.1 jmcneill /* Set up the receive filter. */
3110 1.1 jmcneill alc_iff(sc);
3111 1.1 jmcneill alc_rxvlan(sc);
3112 1.1 jmcneill
3113 1.1 jmcneill /* Acknowledge all pending interrupts and clear it. */
3114 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
3115 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3116 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
3117 1.1 jmcneill
3118 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
3119 1.1 jmcneill /* Switch to the current media. */
3120 1.1 jmcneill mii = &sc->sc_miibus;
3121 1.1 jmcneill mii_mediachg(mii);
3122 1.1 jmcneill
3123 1.1 jmcneill callout_schedule(&sc->sc_tick_ch, hz);
3124 1.1 jmcneill
3125 1.1 jmcneill ifp->if_flags |= IFF_RUNNING;
3126 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
3127 1.1 jmcneill
3128 1.1 jmcneill return (0);
3129 1.1 jmcneill }
3130 1.1 jmcneill
3131 1.1 jmcneill static void
3132 1.1 jmcneill alc_stop(struct ifnet *ifp, int disable)
3133 1.1 jmcneill {
3134 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
3135 1.1 jmcneill struct alc_txdesc *txd;
3136 1.1 jmcneill struct alc_rxdesc *rxd;
3137 1.1 jmcneill uint32_t reg;
3138 1.1 jmcneill int i;
3139 1.1 jmcneill
3140 1.1 jmcneill callout_stop(&sc->sc_tick_ch);
3141 1.1 jmcneill
3142 1.1 jmcneill /*
3143 1.1 jmcneill * Mark the interface down and cancel the watchdog timer.
3144 1.1 jmcneill */
3145 1.1 jmcneill ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3146 1.1 jmcneill ifp->if_timer = 0;
3147 1.1 jmcneill
3148 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
3149 1.1 jmcneill
3150 1.1 jmcneill alc_stats_update(sc);
3151 1.1 jmcneill
3152 1.1 jmcneill mii_down(&sc->sc_miibus);
3153 1.1 jmcneill
3154 1.1 jmcneill /* Disable interrupts. */
3155 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
3156 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3157 1.1 jmcneill
3158 1.1 jmcneill /* Disable DMA. */
3159 1.1 jmcneill reg = CSR_READ_4(sc, ALC_DMA_CFG);
3160 1.1 jmcneill reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
3161 1.1 jmcneill reg |= DMA_CFG_SMB_DIS;
3162 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3163 1.1 jmcneill DELAY(1000);
3164 1.1 jmcneill
3165 1.1 jmcneill /* Stop Rx/Tx MACs. */
3166 1.1 jmcneill alc_stop_mac(sc);
3167 1.1 jmcneill
3168 1.1 jmcneill /* Disable interrupts which might be touched in taskq handler. */
3169 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3170 1.1 jmcneill
3171 1.12 christos /* Disable L0s/L1s */
3172 1.12 christos alc_aspm(sc, 0, IFM_UNKNOWN);
3173 1.12 christos
3174 1.1 jmcneill /* Reclaim Rx buffers that have been processed. */
3175 1.1 jmcneill if (sc->alc_cdata.alc_rxhead != NULL)
3176 1.1 jmcneill m_freem(sc->alc_cdata.alc_rxhead);
3177 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
3178 1.1 jmcneill /*
3179 1.1 jmcneill * Free Tx/Rx mbufs still in the queues.
3180 1.1 jmcneill */
3181 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
3182 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
3183 1.1 jmcneill if (rxd->rx_m != NULL) {
3184 1.15 leot bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
3185 1.15 leot rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
3186 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
3187 1.1 jmcneill m_freem(rxd->rx_m);
3188 1.1 jmcneill rxd->rx_m = NULL;
3189 1.1 jmcneill }
3190 1.1 jmcneill }
3191 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
3192 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
3193 1.1 jmcneill if (txd->tx_m != NULL) {
3194 1.15 leot bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
3195 1.15 leot txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
3196 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
3197 1.1 jmcneill m_freem(txd->tx_m);
3198 1.1 jmcneill txd->tx_m = NULL;
3199 1.1 jmcneill }
3200 1.1 jmcneill }
3201 1.1 jmcneill }
3202 1.1 jmcneill
3203 1.1 jmcneill static void
3204 1.1 jmcneill alc_stop_mac(struct alc_softc *sc)
3205 1.1 jmcneill {
3206 1.1 jmcneill uint32_t reg;
3207 1.1 jmcneill int i;
3208 1.1 jmcneill
3209 1.12 christos alc_stop_queue(sc);
3210 1.1 jmcneill /* Disable Rx/Tx MAC. */
3211 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
3212 1.1 jmcneill if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
3213 1.2 jmcneill reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
3214 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3215 1.1 jmcneill }
3216 1.1 jmcneill for (i = ALC_TIMEOUT; i > 0; i--) {
3217 1.1 jmcneill reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3218 1.12 christos if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC)) == 0)
3219 1.1 jmcneill break;
3220 1.1 jmcneill DELAY(10);
3221 1.1 jmcneill }
3222 1.1 jmcneill if (i == 0)
3223 1.1 jmcneill printf("%s: could not disable Rx/Tx MAC(0x%08x)!\n",
3224 1.1 jmcneill device_xname(sc->sc_dev), reg);
3225 1.1 jmcneill }
3226 1.1 jmcneill
3227 1.1 jmcneill static void
3228 1.1 jmcneill alc_start_queue(struct alc_softc *sc)
3229 1.1 jmcneill {
3230 1.1 jmcneill uint32_t qcfg[] = {
3231 1.1 jmcneill 0,
3232 1.1 jmcneill RXQ_CFG_QUEUE0_ENB,
3233 1.1 jmcneill RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
3234 1.1 jmcneill RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
3235 1.1 jmcneill RXQ_CFG_ENB
3236 1.1 jmcneill };
3237 1.1 jmcneill uint32_t cfg;
3238 1.1 jmcneill
3239 1.1 jmcneill /* Enable RxQ. */
3240 1.1 jmcneill cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
3241 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3242 1.12 christos cfg &= ~RXQ_CFG_ENB;
3243 1.12 christos cfg |= qcfg[1];
3244 1.12 christos } else
3245 1.12 christos cfg |= RXQ_CFG_QUEUE0_ENB;
3246 1.1 jmcneill CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
3247 1.1 jmcneill /* Enable TxQ. */
3248 1.1 jmcneill cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
3249 1.1 jmcneill cfg |= TXQ_CFG_ENB;
3250 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
3251 1.1 jmcneill }
3252 1.1 jmcneill
3253 1.1 jmcneill static void
3254 1.1 jmcneill alc_stop_queue(struct alc_softc *sc)
3255 1.1 jmcneill {
3256 1.1 jmcneill uint32_t reg;
3257 1.1 jmcneill int i;
3258 1.1 jmcneill
3259 1.1 jmcneill /* Disable RxQ. */
3260 1.1 jmcneill reg = CSR_READ_4(sc, ALC_RXQ_CFG);
3261 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3262 1.12 christos if ((reg & RXQ_CFG_ENB) != 0) {
3263 1.12 christos reg &= ~RXQ_CFG_ENB;
3264 1.12 christos CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3265 1.12 christos }
3266 1.12 christos } else {
3267 1.12 christos if ((reg & RXQ_CFG_QUEUE0_ENB) != 0) {
3268 1.12 christos reg &= ~RXQ_CFG_QUEUE0_ENB;
3269 1.12 christos CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3270 1.12 christos }
3271 1.12 christos }
3272 1.1 jmcneill /* Disable TxQ. */
3273 1.1 jmcneill reg = CSR_READ_4(sc, ALC_TXQ_CFG);
3274 1.2 jmcneill if ((reg & TXQ_CFG_ENB) != 0) {
3275 1.1 jmcneill reg &= ~TXQ_CFG_ENB;
3276 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
3277 1.1 jmcneill }
3278 1.12 christos DELAY(40);
3279 1.1 jmcneill for (i = ALC_TIMEOUT; i > 0; i--) {
3280 1.1 jmcneill reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3281 1.1 jmcneill if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3282 1.1 jmcneill break;
3283 1.1 jmcneill DELAY(10);
3284 1.1 jmcneill }
3285 1.1 jmcneill if (i == 0)
3286 1.1 jmcneill printf("%s: could not disable RxQ/TxQ (0x%08x)!\n",
3287 1.1 jmcneill device_xname(sc->sc_dev), reg);
3288 1.1 jmcneill }
3289 1.1 jmcneill
3290 1.1 jmcneill static void
3291 1.1 jmcneill alc_init_tx_ring(struct alc_softc *sc)
3292 1.1 jmcneill {
3293 1.1 jmcneill struct alc_ring_data *rd;
3294 1.1 jmcneill struct alc_txdesc *txd;
3295 1.1 jmcneill int i;
3296 1.1 jmcneill
3297 1.1 jmcneill sc->alc_cdata.alc_tx_prod = 0;
3298 1.1 jmcneill sc->alc_cdata.alc_tx_cons = 0;
3299 1.1 jmcneill sc->alc_cdata.alc_tx_cnt = 0;
3300 1.1 jmcneill
3301 1.1 jmcneill rd = &sc->alc_rdata;
3302 1.1 jmcneill memset(rd->alc_tx_ring, 0, ALC_TX_RING_SZ);
3303 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
3304 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
3305 1.1 jmcneill txd->tx_m = NULL;
3306 1.1 jmcneill }
3307 1.1 jmcneill
3308 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
3309 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
3310 1.1 jmcneill }
3311 1.1 jmcneill
3312 1.1 jmcneill static int
3313 1.7 mrg alc_init_rx_ring(struct alc_softc *sc, bool init)
3314 1.1 jmcneill {
3315 1.1 jmcneill struct alc_ring_data *rd;
3316 1.1 jmcneill struct alc_rxdesc *rxd;
3317 1.1 jmcneill int i;
3318 1.1 jmcneill
3319 1.1 jmcneill sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
3320 1.1 jmcneill rd = &sc->alc_rdata;
3321 1.1 jmcneill memset(rd->alc_rx_ring, 0, ALC_RX_RING_SZ);
3322 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
3323 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
3324 1.1 jmcneill rxd->rx_m = NULL;
3325 1.1 jmcneill rxd->rx_desc = &rd->alc_rx_ring[i];
3326 1.7 mrg if (alc_newbuf(sc, rxd, init) != 0)
3327 1.1 jmcneill return (ENOBUFS);
3328 1.1 jmcneill }
3329 1.1 jmcneill
3330 1.1 jmcneill /*
3331 1.1 jmcneill * Since controller does not update Rx descriptors, driver
3332 1.1 jmcneill * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
3333 1.1 jmcneill * is enough to ensure coherence.
3334 1.1 jmcneill */
3335 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
3336 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
3337 1.1 jmcneill /* Let controller know availability of new Rx buffers. */
3338 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
3339 1.1 jmcneill
3340 1.1 jmcneill return (0);
3341 1.1 jmcneill }
3342 1.1 jmcneill
3343 1.1 jmcneill static void
3344 1.1 jmcneill alc_init_rr_ring(struct alc_softc *sc)
3345 1.1 jmcneill {
3346 1.1 jmcneill struct alc_ring_data *rd;
3347 1.1 jmcneill
3348 1.1 jmcneill sc->alc_cdata.alc_rr_cons = 0;
3349 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
3350 1.1 jmcneill
3351 1.1 jmcneill rd = &sc->alc_rdata;
3352 1.1 jmcneill memset(rd->alc_rr_ring, 0, ALC_RR_RING_SZ);
3353 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
3354 1.15 leot sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
3355 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3356 1.1 jmcneill }
3357 1.1 jmcneill
3358 1.1 jmcneill static void
3359 1.1 jmcneill alc_init_cmb(struct alc_softc *sc)
3360 1.1 jmcneill {
3361 1.1 jmcneill struct alc_ring_data *rd;
3362 1.1 jmcneill
3363 1.1 jmcneill rd = &sc->alc_rdata;
3364 1.1 jmcneill memset(rd->alc_cmb, 0, ALC_CMB_SZ);
3365 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
3366 1.15 leot sc->alc_cdata.alc_cmb_map->dm_mapsize,
3367 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3368 1.1 jmcneill }
3369 1.1 jmcneill
3370 1.1 jmcneill static void
3371 1.1 jmcneill alc_init_smb(struct alc_softc *sc)
3372 1.1 jmcneill {
3373 1.1 jmcneill struct alc_ring_data *rd;
3374 1.1 jmcneill
3375 1.1 jmcneill rd = &sc->alc_rdata;
3376 1.1 jmcneill memset(rd->alc_smb, 0, ALC_SMB_SZ);
3377 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
3378 1.15 leot sc->alc_cdata.alc_smb_map->dm_mapsize,
3379 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3380 1.1 jmcneill }
3381 1.1 jmcneill
3382 1.1 jmcneill static void
3383 1.1 jmcneill alc_rxvlan(struct alc_softc *sc)
3384 1.1 jmcneill {
3385 1.1 jmcneill uint32_t reg;
3386 1.1 jmcneill
3387 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
3388 1.3 sborrill if (sc->sc_ec.ec_capenable & ETHERCAP_VLAN_HWTAGGING)
3389 1.1 jmcneill reg |= MAC_CFG_VLAN_TAG_STRIP;
3390 1.1 jmcneill else
3391 1.1 jmcneill reg &= ~MAC_CFG_VLAN_TAG_STRIP;
3392 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3393 1.1 jmcneill }
3394 1.1 jmcneill
3395 1.1 jmcneill static void
3396 1.1 jmcneill alc_iff(struct alc_softc *sc)
3397 1.1 jmcneill {
3398 1.1 jmcneill struct ethercom *ec = &sc->sc_ec;
3399 1.1 jmcneill struct ifnet *ifp = &ec->ec_if;
3400 1.1 jmcneill struct ether_multi *enm;
3401 1.1 jmcneill struct ether_multistep step;
3402 1.1 jmcneill uint32_t crc;
3403 1.1 jmcneill uint32_t mchash[2];
3404 1.1 jmcneill uint32_t rxcfg;
3405 1.1 jmcneill
3406 1.1 jmcneill rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
3407 1.1 jmcneill rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
3408 1.1 jmcneill ifp->if_flags &= ~IFF_ALLMULTI;
3409 1.1 jmcneill
3410 1.1 jmcneill /*
3411 1.1 jmcneill * Always accept broadcast frames.
3412 1.1 jmcneill */
3413 1.1 jmcneill rxcfg |= MAC_CFG_BCAST;
3414 1.1 jmcneill
3415 1.1 jmcneill if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
3416 1.1 jmcneill ifp->if_flags |= IFF_ALLMULTI;
3417 1.1 jmcneill if (ifp->if_flags & IFF_PROMISC)
3418 1.1 jmcneill rxcfg |= MAC_CFG_PROMISC;
3419 1.1 jmcneill else
3420 1.1 jmcneill rxcfg |= MAC_CFG_ALLMULTI;
3421 1.1 jmcneill mchash[0] = mchash[1] = 0xFFFFFFFF;
3422 1.1 jmcneill } else {
3423 1.1 jmcneill /* Program new filter. */
3424 1.1 jmcneill memset(mchash, 0, sizeof(mchash));
3425 1.1 jmcneill
3426 1.1 jmcneill ETHER_FIRST_MULTI(step, ec, enm);
3427 1.1 jmcneill while (enm != NULL) {
3428 1.1 jmcneill crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
3429 1.1 jmcneill mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
3430 1.1 jmcneill ETHER_NEXT_MULTI(step, enm);
3431 1.1 jmcneill }
3432 1.1 jmcneill }
3433 1.1 jmcneill
3434 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
3435 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
3436 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
3437 1.1 jmcneill }
3438 1.1 jmcneill
3439 1.5 jmcneill MODULE(MODULE_CLASS_DRIVER, if_alc, "pci");
3440 1.1 jmcneill
3441 1.1 jmcneill #ifdef _MODULE
3442 1.1 jmcneill #include "ioconf.c"
3443 1.1 jmcneill #endif
3444 1.1 jmcneill
3445 1.1 jmcneill static int
3446 1.1 jmcneill if_alc_modcmd(modcmd_t cmd, void *opaque)
3447 1.1 jmcneill {
3448 1.1 jmcneill int error = 0;
3449 1.1 jmcneill
3450 1.1 jmcneill switch (cmd) {
3451 1.1 jmcneill case MODULE_CMD_INIT:
3452 1.1 jmcneill #ifdef _MODULE
3453 1.1 jmcneill error = config_init_component(cfdriver_ioconf_if_alc,
3454 1.1 jmcneill cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
3455 1.1 jmcneill #endif
3456 1.1 jmcneill return error;
3457 1.1 jmcneill case MODULE_CMD_FINI:
3458 1.1 jmcneill #ifdef _MODULE
3459 1.1 jmcneill error = config_fini_component(cfdriver_ioconf_if_alc,
3460 1.1 jmcneill cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
3461 1.1 jmcneill #endif
3462 1.1 jmcneill return error;
3463 1.1 jmcneill default:
3464 1.1 jmcneill return ENOTTY;
3465 1.1 jmcneill }
3466 1.1 jmcneill }
3467