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