if_alc.c revision 1.29 1 1.29 jdolecek /* $NetBSD: if_alc.c,v 1.29 2018/12/09 11:14:02 jdolecek Exp $ */
2 1.1 jmcneill /* $OpenBSD: if_alc.c,v 1.1 2009/08/08 09:31:13 kevlo Exp $ */
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2009, Pyun YongHyeon <yongari (at) FreeBSD.org>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice unmodified, this list of conditions, and the following
12 1.1 jmcneill * disclaimer.
13 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
15 1.1 jmcneill * documentation and/or other materials provided with the distribution.
16 1.1 jmcneill *
17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 jmcneill * SUCH DAMAGE.
28 1.1 jmcneill */
29 1.1 jmcneill
30 1.2 jmcneill /* Driver for Atheros AR813x/AR815x PCIe Ethernet. */
31 1.1 jmcneill
32 1.1 jmcneill #ifdef _KERNEL_OPT
33 1.1 jmcneill #include "vlan.h"
34 1.1 jmcneill #endif
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/proc.h>
38 1.1 jmcneill #include <sys/endian.h>
39 1.1 jmcneill #include <sys/systm.h>
40 1.1 jmcneill #include <sys/types.h>
41 1.1 jmcneill #include <sys/sockio.h>
42 1.1 jmcneill #include <sys/mbuf.h>
43 1.1 jmcneill #include <sys/queue.h>
44 1.1 jmcneill #include <sys/kernel.h>
45 1.1 jmcneill #include <sys/device.h>
46 1.1 jmcneill #include <sys/callout.h>
47 1.1 jmcneill #include <sys/socket.h>
48 1.1 jmcneill #include <sys/module.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <sys/bus.h>
51 1.1 jmcneill
52 1.27 msaitoh #include <net/bpf.h>
53 1.1 jmcneill #include <net/if.h>
54 1.1 jmcneill #include <net/if_dl.h>
55 1.1 jmcneill #include <net/if_llc.h>
56 1.1 jmcneill #include <net/if_media.h>
57 1.1 jmcneill #include <net/if_ether.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 <dev/mii/mii.h>
70 1.1 jmcneill #include <dev/mii/miivar.h>
71 1.1 jmcneill
72 1.1 jmcneill #include <dev/pci/pcireg.h>
73 1.1 jmcneill #include <dev/pci/pcivar.h>
74 1.1 jmcneill #include <dev/pci/pcidevs.h>
75 1.1 jmcneill
76 1.1 jmcneill #include <dev/pci/if_alcreg.h>
77 1.1 jmcneill
78 1.2 jmcneill /*
79 1.2 jmcneill * Devices supported by this driver.
80 1.2 jmcneill */
81 1.2 jmcneill static struct alc_ident alc_ident_table[] = {
82 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8131, 9 * 1024,
83 1.2 jmcneill "Atheros AR8131 PCIe Gigabit Ethernet" },
84 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8132, 9 * 1024,
85 1.2 jmcneill "Atheros AR8132 PCIe Fast Ethernet" },
86 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151, 6 * 1024,
87 1.2 jmcneill "Atheros AR8151 v1.0 PCIe Gigabit Ethernet" },
88 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8151_V2, 6 * 1024,
89 1.2 jmcneill "Atheros AR8151 v2.0 PCIe Gigabit Ethernet" },
90 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B, 6 * 1024,
91 1.2 jmcneill "Atheros AR8152 v1.1 PCIe Fast Ethernet" },
92 1.2 jmcneill { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8152_B2, 6 * 1024,
93 1.2 jmcneill "Atheros AR8152 v2.0 PCIe Fast Ethernet" },
94 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8161, 9 * 1024,
95 1.12 christos "Atheros AR8161 PCIe Gigabit Ethernet" },
96 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8162, 9 * 1024,
97 1.12 christos "Atheros AR8162 PCIe Fast Ethernet" },
98 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8171, 9 * 1024,
99 1.12 christos "Atheros AR8171 PCIe Gigabit Ethernet" },
100 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_AR8172, 9 * 1024,
101 1.12 christos "Atheros AR8172 PCIe Fast Ethernet" },
102 1.12 christos { PCI_VENDOR_ATTANSIC, PCI_PRODUCT_ATTANSIC_E2200, 9 * 1024,
103 1.12 christos "Killer E2200 Gigabit Ethernet" },
104 1.2 jmcneill { 0, 0, 0, NULL },
105 1.2 jmcneill };
106 1.2 jmcneill
107 1.1 jmcneill static int alc_match(device_t, cfdata_t, void *);
108 1.1 jmcneill static void alc_attach(device_t, device_t, void *);
109 1.1 jmcneill static int alc_detach(device_t, int);
110 1.1 jmcneill
111 1.1 jmcneill static int alc_init(struct ifnet *);
112 1.7 mrg static int alc_init_backend(struct ifnet *, bool);
113 1.1 jmcneill static void alc_start(struct ifnet *);
114 1.1 jmcneill static int alc_ioctl(struct ifnet *, u_long, void *);
115 1.1 jmcneill static void alc_watchdog(struct ifnet *);
116 1.1 jmcneill static int alc_mediachange(struct ifnet *);
117 1.1 jmcneill static void alc_mediastatus(struct ifnet *, struct ifmediareq *);
118 1.1 jmcneill
119 1.12 christos static void alc_aspm(struct alc_softc *, int, int);
120 1.12 christos static void alc_aspm_813x(struct alc_softc *, int);
121 1.12 christos static void alc_aspm_816x(struct alc_softc *, int);
122 1.1 jmcneill static void alc_disable_l0s_l1(struct alc_softc *);
123 1.1 jmcneill static int alc_dma_alloc(struct alc_softc *);
124 1.1 jmcneill static void alc_dma_free(struct alc_softc *);
125 1.12 christos static void alc_dsp_fixup(struct alc_softc *, int);
126 1.1 jmcneill static int alc_encap(struct alc_softc *, struct mbuf **);
127 1.2 jmcneill static struct alc_ident *
128 1.2 jmcneill alc_find_ident(struct pci_attach_args *);
129 1.1 jmcneill static void alc_get_macaddr(struct alc_softc *);
130 1.12 christos static void alc_get_macaddr_813x(struct alc_softc *);
131 1.12 christos static void alc_get_macaddr_816x(struct alc_softc *);
132 1.12 christos static void alc_get_macaddr_par(struct alc_softc *);
133 1.1 jmcneill static void alc_init_cmb(struct alc_softc *);
134 1.1 jmcneill static void alc_init_rr_ring(struct alc_softc *);
135 1.7 mrg static int alc_init_rx_ring(struct alc_softc *, bool);
136 1.1 jmcneill static void alc_init_smb(struct alc_softc *);
137 1.1 jmcneill static void alc_init_tx_ring(struct alc_softc *);
138 1.1 jmcneill static int alc_intr(void *);
139 1.1 jmcneill static void alc_mac_config(struct alc_softc *);
140 1.12 christos static uint32_t alc_mii_readreg_813x(struct alc_softc *, int, int);
141 1.12 christos static uint32_t alc_mii_readreg_816x(struct alc_softc *, int, int);
142 1.12 christos static void alc_mii_writereg_813x(struct alc_softc *, int, int, int);
143 1.12 christos static void alc_mii_writereg_816x(struct alc_softc *, int, int, int);
144 1.1 jmcneill static int alc_miibus_readreg(device_t, int, int);
145 1.6 matt static void alc_miibus_statchg(struct ifnet *);
146 1.1 jmcneill static void alc_miibus_writereg(device_t, int, int, int);
147 1.12 christos static uint32_t alc_miidbg_readreg(struct alc_softc *, int);
148 1.12 christos static void alc_miidbg_writereg(struct alc_softc *, int, int);
149 1.12 christos static uint32_t alc_miiext_readreg(struct alc_softc *, int, int);
150 1.12 christos static uint32_t alc_miiext_writereg(struct alc_softc *, int, int, int);
151 1.7 mrg static int alc_newbuf(struct alc_softc *, struct alc_rxdesc *, bool);
152 1.1 jmcneill static void alc_phy_down(struct alc_softc *);
153 1.1 jmcneill static void alc_phy_reset(struct alc_softc *);
154 1.12 christos static void alc_phy_reset_813x(struct alc_softc *);
155 1.12 christos static void alc_phy_reset_816x(struct alc_softc *);
156 1.1 jmcneill static void alc_reset(struct alc_softc *);
157 1.1 jmcneill static void alc_rxeof(struct alc_softc *, struct rx_rdesc *);
158 1.1 jmcneill static int alc_rxintr(struct alc_softc *);
159 1.1 jmcneill static void alc_iff(struct alc_softc *);
160 1.1 jmcneill static void alc_rxvlan(struct alc_softc *);
161 1.1 jmcneill static void alc_start_queue(struct alc_softc *);
162 1.1 jmcneill static void alc_stats_clear(struct alc_softc *);
163 1.1 jmcneill static void alc_stats_update(struct alc_softc *);
164 1.1 jmcneill static void alc_stop(struct ifnet *, int);
165 1.1 jmcneill static void alc_stop_mac(struct alc_softc *);
166 1.1 jmcneill static void alc_stop_queue(struct alc_softc *);
167 1.1 jmcneill static void alc_tick(void *);
168 1.1 jmcneill static void alc_txeof(struct alc_softc *);
169 1.1 jmcneill
170 1.1 jmcneill uint32_t alc_dma_burst[] = { 128, 256, 512, 1024, 2048, 4096, 0 };
171 1.1 jmcneill
172 1.1 jmcneill CFATTACH_DECL_NEW(alc, sizeof(struct alc_softc),
173 1.1 jmcneill alc_match, alc_attach, alc_detach, NULL);
174 1.1 jmcneill
175 1.1 jmcneill int alcdebug = 0;
176 1.1 jmcneill #define DPRINTF(x) do { if (alcdebug) printf x; } while (0)
177 1.1 jmcneill
178 1.1 jmcneill #define ETHER_ALIGN 2
179 1.1 jmcneill #define ALC_CSUM_FEATURES (M_CSUM_TCPv4 | M_CSUM_UDPv4)
180 1.1 jmcneill
181 1.1 jmcneill static int
182 1.1 jmcneill alc_miibus_readreg(device_t dev, int phy, int reg)
183 1.1 jmcneill {
184 1.1 jmcneill struct alc_softc *sc = device_private(dev);
185 1.12 christos int v;
186 1.12 christos
187 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
188 1.12 christos v = alc_mii_readreg_816x(sc, phy, reg);
189 1.12 christos else
190 1.12 christos v = alc_mii_readreg_813x(sc, phy, reg);
191 1.12 christos return (v);
192 1.12 christos }
193 1.12 christos
194 1.12 christos static uint32_t
195 1.12 christos alc_mii_readreg_813x(struct alc_softc *sc, int phy, int reg)
196 1.12 christos {
197 1.1 jmcneill uint32_t v;
198 1.1 jmcneill int i;
199 1.1 jmcneill
200 1.1 jmcneill if (phy != sc->alc_phyaddr)
201 1.1 jmcneill return (0);
202 1.1 jmcneill
203 1.12 christos /*
204 1.12 christos * For AR8132 fast ethernet controller, do not report 1000baseT
205 1.12 christos * capability to mii(4). Even though AR8132 uses the same
206 1.12 christos * model/revision number of F1 gigabit PHY, the PHY has no
207 1.12 christos * ability to establish 1000baseT link.
208 1.12 christos */
209 1.12 christos if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0 &&
210 1.12 christos reg == MII_EXTSR)
211 1.12 christos return 0;
212 1.12 christos
213 1.1 jmcneill CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
214 1.1 jmcneill MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
215 1.1 jmcneill for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
216 1.1 jmcneill DELAY(5);
217 1.1 jmcneill v = CSR_READ_4(sc, ALC_MDIO);
218 1.1 jmcneill if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
219 1.1 jmcneill break;
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill if (i == 0) {
223 1.1 jmcneill printf("%s: phy read timeout: phy %d, reg %d\n",
224 1.1 jmcneill device_xname(sc->sc_dev), phy, reg);
225 1.1 jmcneill return (0);
226 1.1 jmcneill }
227 1.1 jmcneill
228 1.1 jmcneill return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
229 1.1 jmcneill }
230 1.1 jmcneill
231 1.12 christos static uint32_t
232 1.12 christos alc_mii_readreg_816x(struct alc_softc *sc, int phy, int reg)
233 1.12 christos {
234 1.12 christos uint32_t clk, v;
235 1.12 christos int i;
236 1.12 christos
237 1.12 christos if (phy != sc->alc_phyaddr)
238 1.12 christos return (0);
239 1.12 christos
240 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
241 1.12 christos clk = MDIO_CLK_25_128;
242 1.12 christos else
243 1.12 christos clk = MDIO_CLK_25_4;
244 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
245 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_REG_ADDR(reg));
246 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
247 1.12 christos DELAY(5);
248 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
249 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
250 1.12 christos break;
251 1.12 christos }
252 1.12 christos
253 1.12 christos if (i == 0) {
254 1.12 christos printf("%s: phy read timeout: phy %d, reg %d\n",
255 1.12 christos device_xname(sc->sc_dev), phy, reg);
256 1.12 christos return (0);
257 1.12 christos }
258 1.12 christos
259 1.12 christos return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
260 1.12 christos }
261 1.12 christos
262 1.1 jmcneill static void
263 1.1 jmcneill alc_miibus_writereg(device_t dev, int phy, int reg, int val)
264 1.1 jmcneill {
265 1.1 jmcneill struct alc_softc *sc = device_private(dev);
266 1.12 christos
267 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
268 1.12 christos alc_mii_writereg_816x(sc, phy, reg, val);
269 1.12 christos else
270 1.12 christos alc_mii_writereg_813x(sc, phy, reg, val);
271 1.12 christos
272 1.12 christos return;
273 1.12 christos }
274 1.12 christos
275 1.12 christos static void
276 1.12 christos alc_mii_writereg_813x(struct alc_softc *sc, int phy, int reg, int val)
277 1.12 christos {
278 1.1 jmcneill uint32_t v;
279 1.1 jmcneill int i;
280 1.1 jmcneill
281 1.1 jmcneill CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
282 1.1 jmcneill (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
283 1.1 jmcneill MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
284 1.1 jmcneill for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
285 1.1 jmcneill DELAY(5);
286 1.1 jmcneill v = CSR_READ_4(sc, ALC_MDIO);
287 1.1 jmcneill if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
288 1.1 jmcneill break;
289 1.1 jmcneill }
290 1.1 jmcneill
291 1.1 jmcneill if (i == 0)
292 1.1 jmcneill printf("%s: phy write timeout: phy %d, reg %d\n",
293 1.1 jmcneill device_xname(sc->sc_dev), phy, reg);
294 1.12 christos
295 1.12 christos return;
296 1.12 christos }
297 1.12 christos
298 1.12 christos static void
299 1.12 christos alc_mii_writereg_816x(struct alc_softc *sc, int phy, int reg, int val)
300 1.12 christos {
301 1.12 christos uint32_t clk, v;
302 1.12 christos int i;
303 1.12 christos
304 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
305 1.12 christos clk = MDIO_CLK_25_128;
306 1.12 christos else
307 1.12 christos clk = MDIO_CLK_25_4;
308 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
309 1.12 christos ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) | MDIO_REG_ADDR(reg) |
310 1.12 christos MDIO_SUP_PREAMBLE | clk);
311 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
312 1.12 christos DELAY(5);
313 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
314 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
315 1.12 christos break;
316 1.12 christos }
317 1.12 christos
318 1.12 christos if (i == 0)
319 1.12 christos printf("%s: phy write timeout: phy %d, reg %d\n",
320 1.12 christos device_xname(sc->sc_dev), phy, reg);
321 1.12 christos
322 1.12 christos return;
323 1.1 jmcneill }
324 1.1 jmcneill
325 1.1 jmcneill static void
326 1.6 matt alc_miibus_statchg(struct ifnet *ifp)
327 1.1 jmcneill {
328 1.6 matt struct alc_softc *sc = ifp->if_softc;
329 1.6 matt struct mii_data *mii = &sc->sc_miibus;
330 1.1 jmcneill uint32_t reg;
331 1.1 jmcneill
332 1.1 jmcneill if ((ifp->if_flags & IFF_RUNNING) == 0)
333 1.1 jmcneill return;
334 1.1 jmcneill
335 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
336 1.1 jmcneill if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
337 1.1 jmcneill (IFM_ACTIVE | IFM_AVALID)) {
338 1.1 jmcneill switch (IFM_SUBTYPE(mii->mii_media_active)) {
339 1.1 jmcneill case IFM_10_T:
340 1.1 jmcneill case IFM_100_TX:
341 1.1 jmcneill sc->alc_flags |= ALC_FLAG_LINK;
342 1.1 jmcneill break;
343 1.1 jmcneill case IFM_1000_T:
344 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0)
345 1.1 jmcneill sc->alc_flags |= ALC_FLAG_LINK;
346 1.1 jmcneill break;
347 1.1 jmcneill default:
348 1.1 jmcneill break;
349 1.1 jmcneill }
350 1.1 jmcneill }
351 1.1 jmcneill /* Stop Rx/Tx MACs. */
352 1.1 jmcneill alc_stop_mac(sc);
353 1.1 jmcneill
354 1.1 jmcneill /* Program MACs with resolved speed/duplex/flow-control. */
355 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
356 1.1 jmcneill alc_start_queue(sc);
357 1.1 jmcneill alc_mac_config(sc);
358 1.1 jmcneill /* Re-enable Tx/Rx MACs. */
359 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
360 1.1 jmcneill reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
361 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
362 1.1 jmcneill }
363 1.12 christos alc_aspm(sc, 0, IFM_SUBTYPE(mii->mii_media_active));
364 1.12 christos alc_dsp_fixup(sc, IFM_SUBTYPE(mii->mii_media_active));
365 1.12 christos }
366 1.12 christos
367 1.12 christos static uint32_t
368 1.12 christos alc_miidbg_readreg(struct alc_softc *sc, int reg)
369 1.12 christos {
370 1.12 christos
371 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
372 1.12 christos reg);
373 1.12 christos return (alc_miibus_readreg(sc->sc_dev, sc->alc_phyaddr,
374 1.12 christos ALC_MII_DBG_DATA));
375 1.1 jmcneill }
376 1.1 jmcneill
377 1.1 jmcneill static void
378 1.12 christos alc_miidbg_writereg(struct alc_softc *sc, int reg, int val)
379 1.12 christos {
380 1.12 christos
381 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_ADDR,
382 1.12 christos reg);
383 1.12 christos alc_miibus_writereg(sc->sc_dev, sc->alc_phyaddr, ALC_MII_DBG_DATA, val);
384 1.12 christos
385 1.12 christos return;
386 1.12 christos }
387 1.12 christos
388 1.12 christos static uint32_t
389 1.12 christos alc_miiext_readreg(struct alc_softc *sc, int devaddr, int reg)
390 1.12 christos {
391 1.12 christos uint32_t clk, v;
392 1.12 christos int i;
393 1.12 christos
394 1.12 christos CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
395 1.12 christos EXT_MDIO_DEVADDR(devaddr));
396 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
397 1.12 christos clk = MDIO_CLK_25_128;
398 1.12 christos else
399 1.12 christos clk = MDIO_CLK_25_4;
400 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
401 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
402 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
403 1.12 christos DELAY(5);
404 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
405 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
406 1.12 christos break;
407 1.12 christos }
408 1.12 christos
409 1.12 christos if (i == 0) {
410 1.12 christos printf("%s: phy ext read timeout: %d\n",
411 1.12 christos device_xname(sc->sc_dev), reg);
412 1.12 christos return (0);
413 1.12 christos }
414 1.12 christos
415 1.12 christos return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
416 1.12 christos }
417 1.12 christos
418 1.12 christos static uint32_t
419 1.12 christos alc_miiext_writereg(struct alc_softc *sc, int devaddr, int reg, int val)
420 1.12 christos {
421 1.12 christos uint32_t clk, v;
422 1.12 christos int i;
423 1.12 christos
424 1.12 christos CSR_WRITE_4(sc, ALC_EXT_MDIO, EXT_MDIO_REG(reg) |
425 1.12 christos EXT_MDIO_DEVADDR(devaddr));
426 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0)
427 1.12 christos clk = MDIO_CLK_25_128;
428 1.12 christos else
429 1.12 christos clk = MDIO_CLK_25_4;
430 1.12 christos CSR_WRITE_4(sc, ALC_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
431 1.12 christos ((val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT) |
432 1.12 christos MDIO_SUP_PREAMBLE | clk | MDIO_MODE_EXT);
433 1.12 christos for (i = ALC_PHY_TIMEOUT; i > 0; i--) {
434 1.12 christos DELAY(5);
435 1.12 christos v = CSR_READ_4(sc, ALC_MDIO);
436 1.12 christos if ((v & MDIO_OP_BUSY) == 0)
437 1.12 christos break;
438 1.12 christos }
439 1.12 christos
440 1.12 christos if (i == 0) {
441 1.12 christos printf("%s: phy ext write timeout: reg %d\n",
442 1.12 christos device_xname(sc->sc_dev), reg);
443 1.12 christos return (0);
444 1.12 christos }
445 1.12 christos
446 1.12 christos return (0);
447 1.12 christos }
448 1.12 christos
449 1.12 christos static void
450 1.12 christos alc_dsp_fixup(struct alc_softc *sc, int media)
451 1.12 christos {
452 1.12 christos uint16_t agc, len, val;
453 1.12 christos
454 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
455 1.12 christos return;
456 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_C0)
457 1.12 christos return;
458 1.12 christos
459 1.12 christos /*
460 1.12 christos * Vendor PHY magic.
461 1.12 christos * 1000BT/AZ, wrong cable length
462 1.12 christos */
463 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK) != 0) {
464 1.12 christos len = alc_miiext_readreg(sc, MII_EXT_PCS, MII_EXT_CLDCTL6);
465 1.12 christos len = (len >> EXT_CLDCTL6_CAB_LEN_SHIFT) &
466 1.12 christos EXT_CLDCTL6_CAB_LEN_MASK;
467 1.13 christos /* XXX: used to be (alc >> shift) & mask which is 0 */
468 1.13 christos agc = alc_miidbg_readreg(sc, MII_DBG_AGC) & DBG_AGC_2_VGA_MASK;
469 1.13 christos agc >>= DBG_AGC_2_VGA_SHIFT;
470 1.12 christos if ((media == IFM_1000_T && len > EXT_CLDCTL6_CAB_LEN_SHORT1G &&
471 1.12 christos agc > DBG_AGC_LONG1G_LIMT) ||
472 1.12 christos (media == IFM_100_TX && len > DBG_AGC_LONG100M_LIMT &&
473 1.12 christos agc > DBG_AGC_LONG1G_LIMT)) {
474 1.12 christos alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
475 1.12 christos DBG_AZ_ANADECT_LONG);
476 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG,
477 1.12 christos MII_EXT_ANEG_AFE);
478 1.12 christos val |= ANEG_AFEE_10BT_100M_TH;
479 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
480 1.12 christos val);
481 1.12 christos } else {
482 1.12 christos alc_miidbg_writereg(sc, MII_DBG_AZ_ANADECT,
483 1.12 christos DBG_AZ_ANADECT_DEFAULT);
484 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG,
485 1.12 christos MII_EXT_ANEG_AFE);
486 1.12 christos val &= ~ANEG_AFEE_10BT_100M_TH;
487 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE,
488 1.12 christos val);
489 1.12 christos }
490 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
491 1.12 christos AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
492 1.12 christos if (media == IFM_1000_T) {
493 1.12 christos /*
494 1.12 christos * Giga link threshold, raise the tolerance of
495 1.12 christos * noise 50%.
496 1.12 christos */
497 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
498 1.12 christos val &= ~DBG_MSE20DB_TH_MASK;
499 1.12 christos val |= (DBG_MSE20DB_TH_HI <<
500 1.12 christos DBG_MSE20DB_TH_SHIFT);
501 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
502 1.12 christos } else if (media == IFM_100_TX)
503 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
504 1.12 christos DBG_MSE16DB_UP);
505 1.12 christos }
506 1.12 christos } else {
507 1.12 christos val = alc_miiext_readreg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE);
508 1.12 christos val &= ~ANEG_AFEE_10BT_100M_TH;
509 1.12 christos alc_miiext_writereg(sc, MII_EXT_ANEG, MII_EXT_ANEG_AFE, val);
510 1.12 christos if ((sc->alc_flags & ALC_FLAG_LINK_WAR) != 0 &&
511 1.12 christos AR816X_REV(sc->alc_rev) == AR816X_REV_B0) {
512 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE16DB,
513 1.12 christos DBG_MSE16DB_DOWN);
514 1.12 christos val = alc_miidbg_readreg(sc, MII_DBG_MSE20DB);
515 1.12 christos val &= ~DBG_MSE20DB_TH_MASK;
516 1.12 christos val |= (DBG_MSE20DB_TH_DEFAULT << DBG_MSE20DB_TH_SHIFT);
517 1.12 christos alc_miidbg_writereg(sc, MII_DBG_MSE20DB, val);
518 1.12 christos }
519 1.12 christos }
520 1.12 christos }
521 1.12 christos
522 1.12 christos static void
523 1.1 jmcneill alc_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
524 1.1 jmcneill {
525 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
526 1.1 jmcneill struct mii_data *mii = &sc->sc_miibus;
527 1.1 jmcneill
528 1.15 leot if ((ifp->if_flags & IFF_UP) == 0)
529 1.15 leot return;
530 1.15 leot
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.29 jdolecek sc->sc_irq_handle = pci_intr_establish_xname(pc, ih, IPL_NET, alc_intr,
1212 1.29 jdolecek sc, device_xname(self));
1213 1.1 jmcneill if (sc->sc_irq_handle == NULL) {
1214 1.1 jmcneill printf(": could not establish interrupt");
1215 1.1 jmcneill if (intrstr != NULL)
1216 1.1 jmcneill printf(" at %s", intrstr);
1217 1.1 jmcneill printf("\n");
1218 1.1 jmcneill goto fail;
1219 1.1 jmcneill }
1220 1.4 matt aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1221 1.8 christos
1222 1.1 jmcneill /* Set PHY address. */
1223 1.1 jmcneill sc->alc_phyaddr = ALC_PHY_ADDR;
1224 1.1 jmcneill
1225 1.1 jmcneill /* Initialize DMA parameters. */
1226 1.1 jmcneill sc->alc_dma_rd_burst = 0;
1227 1.1 jmcneill sc->alc_dma_wr_burst = 0;
1228 1.1 jmcneill sc->alc_rcb = DMA_CFG_RCB_64;
1229 1.1 jmcneill if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
1230 1.1 jmcneill &base, NULL)) {
1231 1.1 jmcneill sc->alc_flags |= ALC_FLAG_PCIE;
1232 1.2 jmcneill sc->alc_expcap = base;
1233 1.1 jmcneill burst = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1234 1.9 msaitoh base + PCIE_DCSR) >> 16;
1235 1.1 jmcneill sc->alc_dma_rd_burst = (burst & 0x7000) >> 12;
1236 1.1 jmcneill sc->alc_dma_wr_burst = (burst & 0x00e0) >> 5;
1237 1.1 jmcneill if (alcdebug) {
1238 1.1 jmcneill printf("%s: Read request size : %u bytes.\n",
1239 1.8 christos device_xname(sc->sc_dev),
1240 1.1 jmcneill alc_dma_burst[sc->alc_dma_rd_burst]);
1241 1.1 jmcneill printf("%s: TLP payload size : %u bytes.\n",
1242 1.1 jmcneill device_xname(sc->sc_dev),
1243 1.1 jmcneill alc_dma_burst[sc->alc_dma_wr_burst]);
1244 1.1 jmcneill }
1245 1.12 christos if (alc_dma_burst[sc->alc_dma_rd_burst] > 1024)
1246 1.12 christos sc->alc_dma_rd_burst = 3;
1247 1.12 christos if (alc_dma_burst[sc->alc_dma_wr_burst] > 1024)
1248 1.12 christos sc->alc_dma_wr_burst = 3;
1249 1.12 christos
1250 1.1 jmcneill /* Clear data link and flow-control protocol error. */
1251 1.1 jmcneill val = CSR_READ_4(sc, ALC_PEX_UNC_ERR_SEV);
1252 1.1 jmcneill val &= ~(PEX_UNC_ERR_SEV_DLP | PEX_UNC_ERR_SEV_FCP);
1253 1.1 jmcneill CSR_WRITE_4(sc, ALC_PEX_UNC_ERR_SEV, val);
1254 1.12 christos
1255 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
1256 1.12 christos CSR_WRITE_4(sc, ALC_LTSSM_ID_CFG,
1257 1.12 christos CSR_READ_4(sc, ALC_LTSSM_ID_CFG) & ~LTSSM_ID_WRO_ENB);
1258 1.12 christos CSR_WRITE_4(sc, ALC_PCIE_PHYMISC,
1259 1.12 christos CSR_READ_4(sc, ALC_PCIE_PHYMISC) |
1260 1.12 christos PCIE_PHYMISC_FORCE_RCV_DET);
1261 1.12 christos if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B &&
1262 1.12 christos sc->alc_rev == ATHEROS_AR8152_B_V10) {
1263 1.12 christos val = CSR_READ_4(sc, ALC_PCIE_PHYMISC2);
1264 1.12 christos val &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK |
1265 1.12 christos PCIE_PHYMISC2_SERDES_TH_MASK);
1266 1.12 christos val |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
1267 1.12 christos val |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
1268 1.12 christos CSR_WRITE_4(sc, ALC_PCIE_PHYMISC2, val);
1269 1.12 christos }
1270 1.12 christos /* Disable ASPM L0S and L1. */
1271 1.12 christos cap = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1272 1.12 christos base + PCIE_LCAP) >> 16;
1273 1.24 maya if ((cap & PCIE_LCAP_ASPM) != 0) {
1274 1.12 christos ctl = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
1275 1.12 christos base + PCIE_LCSR) >> 16;
1276 1.12 christos if ((ctl & 0x08) != 0)
1277 1.12 christos sc->alc_rcb = DMA_CFG_RCB_128;
1278 1.12 christos if (alcdebug)
1279 1.12 christos printf("%s: RCB %u bytes\n",
1280 1.12 christos device_xname(sc->sc_dev),
1281 1.12 christos sc->alc_rcb == DMA_CFG_RCB_64 ? 64 : 128);
1282 1.12 christos state = ctl & 0x03;
1283 1.12 christos if (state & 0x01)
1284 1.12 christos sc->alc_flags |= ALC_FLAG_L0S;
1285 1.12 christos if (state & 0x02)
1286 1.12 christos sc->alc_flags |= ALC_FLAG_L1S;
1287 1.12 christos if (alcdebug)
1288 1.12 christos printf("%s: ASPM %s %s\n",
1289 1.12 christos device_xname(sc->sc_dev),
1290 1.12 christos aspm_state[state],
1291 1.12 christos state == 0 ? "disabled" : "enabled");
1292 1.12 christos alc_disable_l0s_l1(sc);
1293 1.12 christos } else {
1294 1.12 christos aprint_debug_dev(sc->sc_dev, "no ASPM support\n");
1295 1.12 christos }
1296 1.2 jmcneill } else {
1297 1.12 christos val = CSR_READ_4(sc, ALC_PDLL_TRNS1);
1298 1.12 christos val &= ~PDLL_TRNS1_D3PLLOFF_ENB;
1299 1.12 christos CSR_WRITE_4(sc, ALC_PDLL_TRNS1, val);
1300 1.12 christos val = CSR_READ_4(sc, ALC_MASTER_CFG);
1301 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
1302 1.12 christos (sc->alc_rev & 0x01) != 0) {
1303 1.12 christos if ((val & MASTER_WAKEN_25M) == 0 ||
1304 1.12 christos (val & MASTER_CLK_SEL_DIS) == 0) {
1305 1.12 christos val |= MASTER_WAKEN_25M | MASTER_CLK_SEL_DIS;
1306 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1307 1.12 christos }
1308 1.12 christos } else {
1309 1.12 christos if ((val & MASTER_WAKEN_25M) == 0 ||
1310 1.12 christos (val & MASTER_CLK_SEL_DIS) != 0) {
1311 1.12 christos val |= MASTER_WAKEN_25M;
1312 1.12 christos val &= ~MASTER_CLK_SEL_DIS;
1313 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, val);
1314 1.12 christos }
1315 1.12 christos }
1316 1.1 jmcneill }
1317 1.12 christos alc_aspm(sc, 1, IFM_UNKNOWN);
1318 1.1 jmcneill }
1319 1.1 jmcneill
1320 1.1 jmcneill /* Reset PHY. */
1321 1.1 jmcneill alc_phy_reset(sc);
1322 1.1 jmcneill
1323 1.1 jmcneill /* Reset the ethernet controller. */
1324 1.12 christos alc_stop_mac(sc);
1325 1.1 jmcneill alc_reset(sc);
1326 1.1 jmcneill
1327 1.1 jmcneill /*
1328 1.1 jmcneill * One odd thing is AR8132 uses the same PHY hardware(F1
1329 1.1 jmcneill * gigabit PHY) of AR8131. So atphy(4) of AR8132 reports
1330 1.1 jmcneill * the PHY supports 1000Mbps but that's not true. The PHY
1331 1.1 jmcneill * used in AR8132 can't establish gigabit link even if it
1332 1.1 jmcneill * shows the same PHY model/revision number of AR8131.
1333 1.1 jmcneill */
1334 1.2 jmcneill switch (sc->alc_ident->deviceid) {
1335 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8161:
1336 1.12 christos if (PCI_SUBSYS_ID(pci_conf_read(
1337 1.12 christos sc->sc_pct, sc->sc_pcitag, PCI_SUBSYS_ID_REG)) == 0x0091 &&
1338 1.12 christos sc->alc_rev == 0)
1339 1.12 christos sc->alc_flags |= ALC_FLAG_LINK_WAR;
1340 1.12 christos /* FALLTHROUGH */
1341 1.12 christos case PCI_PRODUCT_ATTANSIC_E2200:
1342 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8171:
1343 1.12 christos sc->alc_flags |= ALC_FLAG_AR816X_FAMILY;
1344 1.12 christos break;
1345 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8162:
1346 1.12 christos case PCI_PRODUCT_ATTANSIC_AR8172:
1347 1.12 christos sc->alc_flags |= ALC_FLAG_FASTETHER | ALC_FLAG_AR816X_FAMILY;
1348 1.12 christos break;
1349 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B:
1350 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8152_B2:
1351 1.2 jmcneill sc->alc_flags |= ALC_FLAG_APS;
1352 1.2 jmcneill /* FALLTHROUGH */
1353 1.1 jmcneill case PCI_PRODUCT_ATTANSIC_AR8132:
1354 1.2 jmcneill sc->alc_flags |= ALC_FLAG_FASTETHER;
1355 1.1 jmcneill break;
1356 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151:
1357 1.2 jmcneill case PCI_PRODUCT_ATTANSIC_AR8151_V2:
1358 1.2 jmcneill sc->alc_flags |= ALC_FLAG_APS;
1359 1.2 jmcneill /* FALLTHROUGH */
1360 1.1 jmcneill default:
1361 1.1 jmcneill break;
1362 1.1 jmcneill }
1363 1.12 christos sc->alc_flags |= ALC_FLAG_JUMBO;
1364 1.1 jmcneill
1365 1.1 jmcneill /*
1366 1.2 jmcneill * It seems that AR813x/AR815x has silicon bug for SMB. In
1367 1.1 jmcneill * addition, Atheros said that enabling SMB wouldn't improve
1368 1.1 jmcneill * performance. However I think it's bad to access lots of
1369 1.1 jmcneill * registers to extract MAC statistics.
1370 1.1 jmcneill */
1371 1.1 jmcneill sc->alc_flags |= ALC_FLAG_SMB_BUG;
1372 1.1 jmcneill /*
1373 1.1 jmcneill * Don't use Tx CMB. It is known to have silicon bug.
1374 1.1 jmcneill */
1375 1.1 jmcneill sc->alc_flags |= ALC_FLAG_CMB_BUG;
1376 1.1 jmcneill sc->alc_rev = PCI_REVISION(pa->pa_class);
1377 1.1 jmcneill sc->alc_chip_rev = CSR_READ_4(sc, ALC_MASTER_CFG) >>
1378 1.1 jmcneill MASTER_CHIP_REV_SHIFT;
1379 1.1 jmcneill if (alcdebug) {
1380 1.1 jmcneill printf("%s: PCI device revision : 0x%04x\n",
1381 1.1 jmcneill device_xname(sc->sc_dev), sc->alc_rev);
1382 1.1 jmcneill printf("%s: Chip id/revision : 0x%04x\n",
1383 1.1 jmcneill device_xname(sc->sc_dev), sc->alc_chip_rev);
1384 1.1 jmcneill printf("%s: %u Tx FIFO, %u Rx FIFO\n", device_xname(sc->sc_dev),
1385 1.1 jmcneill CSR_READ_4(sc, ALC_SRAM_TX_FIFO_LEN) * 8,
1386 1.1 jmcneill CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN) * 8);
1387 1.1 jmcneill }
1388 1.1 jmcneill
1389 1.1 jmcneill error = alc_dma_alloc(sc);
1390 1.1 jmcneill if (error)
1391 1.1 jmcneill goto fail;
1392 1.1 jmcneill
1393 1.1 jmcneill callout_init(&sc->sc_tick_ch, 0);
1394 1.1 jmcneill callout_setfunc(&sc->sc_tick_ch, alc_tick, sc);
1395 1.1 jmcneill
1396 1.1 jmcneill /* Load station address. */
1397 1.1 jmcneill alc_get_macaddr(sc);
1398 1.1 jmcneill
1399 1.1 jmcneill aprint_normal_dev(self, "Ethernet address %s\n",
1400 1.1 jmcneill ether_sprintf(sc->alc_eaddr));
1401 1.1 jmcneill
1402 1.1 jmcneill ifp = &sc->sc_ec.ec_if;
1403 1.1 jmcneill ifp->if_softc = sc;
1404 1.1 jmcneill ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1405 1.1 jmcneill ifp->if_init = alc_init;
1406 1.1 jmcneill ifp->if_ioctl = alc_ioctl;
1407 1.1 jmcneill ifp->if_start = alc_start;
1408 1.1 jmcneill ifp->if_stop = alc_stop;
1409 1.1 jmcneill ifp->if_watchdog = alc_watchdog;
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.18 christos IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_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.22 ozaki if_deferred_start_init(ifp, NULL);
1468 1.1 jmcneill ether_ifattach(ifp, sc->alc_eaddr);
1469 1.1 jmcneill
1470 1.1 jmcneill if (!pmf_device_register(self, NULL, NULL))
1471 1.1 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
1472 1.1 jmcneill else
1473 1.1 jmcneill pmf_class_network_register(self, ifp);
1474 1.1 jmcneill
1475 1.1 jmcneill return;
1476 1.1 jmcneill fail:
1477 1.1 jmcneill alc_dma_free(sc);
1478 1.1 jmcneill if (sc->sc_irq_handle != NULL) {
1479 1.1 jmcneill pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
1480 1.1 jmcneill sc->sc_irq_handle = NULL;
1481 1.1 jmcneill }
1482 1.1 jmcneill if (sc->sc_mem_size) {
1483 1.1 jmcneill bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
1484 1.1 jmcneill sc->sc_mem_size = 0;
1485 1.1 jmcneill }
1486 1.1 jmcneill }
1487 1.1 jmcneill
1488 1.1 jmcneill static int
1489 1.1 jmcneill alc_detach(device_t self, int flags)
1490 1.1 jmcneill {
1491 1.1 jmcneill struct alc_softc *sc = device_private(self);
1492 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
1493 1.1 jmcneill int s;
1494 1.1 jmcneill
1495 1.1 jmcneill s = splnet();
1496 1.1 jmcneill alc_stop(ifp, 0);
1497 1.1 jmcneill splx(s);
1498 1.1 jmcneill
1499 1.1 jmcneill mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
1500 1.1 jmcneill
1501 1.1 jmcneill /* Delete all remaining media. */
1502 1.1 jmcneill ifmedia_delete_instance(&sc->sc_miibus.mii_media, IFM_INST_ANY);
1503 1.1 jmcneill
1504 1.1 jmcneill ether_ifdetach(ifp);
1505 1.1 jmcneill if_detach(ifp);
1506 1.1 jmcneill alc_dma_free(sc);
1507 1.1 jmcneill
1508 1.1 jmcneill alc_phy_down(sc);
1509 1.1 jmcneill if (sc->sc_irq_handle != NULL) {
1510 1.1 jmcneill pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
1511 1.1 jmcneill sc->sc_irq_handle = NULL;
1512 1.1 jmcneill }
1513 1.1 jmcneill if (sc->sc_mem_size) {
1514 1.1 jmcneill bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
1515 1.1 jmcneill sc->sc_mem_size = 0;
1516 1.1 jmcneill }
1517 1.1 jmcneill
1518 1.1 jmcneill return (0);
1519 1.1 jmcneill }
1520 1.1 jmcneill
1521 1.1 jmcneill static int
1522 1.1 jmcneill alc_dma_alloc(struct alc_softc *sc)
1523 1.1 jmcneill {
1524 1.1 jmcneill struct alc_txdesc *txd;
1525 1.1 jmcneill struct alc_rxdesc *rxd;
1526 1.1 jmcneill int nsegs, error, i;
1527 1.1 jmcneill
1528 1.1 jmcneill /*
1529 1.1 jmcneill * Create DMA stuffs for TX ring
1530 1.1 jmcneill */
1531 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_TX_RING_SZ, 1,
1532 1.1 jmcneill ALC_TX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_tx_ring_map);
1533 1.1 jmcneill if (error) {
1534 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map = NULL;
1535 1.1 jmcneill return (ENOBUFS);
1536 1.1 jmcneill }
1537 1.1 jmcneill
1538 1.1 jmcneill /* Allocate DMA'able memory for TX ring */
1539 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, ALC_TX_RING_SZ,
1540 1.1 jmcneill ETHER_ALIGN, 0, &sc->alc_rdata.alc_tx_ring_seg, 1,
1541 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1542 1.1 jmcneill if (error) {
1543 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Tx ring.\n",
1544 1.1 jmcneill device_xname(sc->sc_dev));
1545 1.1 jmcneill return error;
1546 1.1 jmcneill }
1547 1.1 jmcneill
1548 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_tx_ring_seg,
1549 1.1 jmcneill nsegs, ALC_TX_RING_SZ, (void **)&sc->alc_rdata.alc_tx_ring,
1550 1.1 jmcneill BUS_DMA_NOWAIT);
1551 1.1 jmcneill if (error)
1552 1.1 jmcneill return (ENOBUFS);
1553 1.1 jmcneill
1554 1.1 jmcneill /* Load the DMA map for Tx ring. */
1555 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map,
1556 1.1 jmcneill sc->alc_rdata.alc_tx_ring, ALC_TX_RING_SZ, NULL, BUS_DMA_WAITOK);
1557 1.1 jmcneill if (error) {
1558 1.1 jmcneill printf("%s: could not load DMA'able memory for Tx ring.\n",
1559 1.1 jmcneill device_xname(sc->sc_dev));
1560 1.8 christos bus_dmamem_free(sc->sc_dmat,
1561 1.1 jmcneill &sc->alc_rdata.alc_tx_ring_seg, 1);
1562 1.1 jmcneill return error;
1563 1.1 jmcneill }
1564 1.1 jmcneill
1565 1.8 christos sc->alc_rdata.alc_tx_ring_paddr =
1566 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_segs[0].ds_addr;
1567 1.1 jmcneill
1568 1.1 jmcneill /*
1569 1.1 jmcneill * Create DMA stuffs for RX ring
1570 1.1 jmcneill */
1571 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_RX_RING_SZ, 1,
1572 1.1 jmcneill ALC_RX_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_ring_map);
1573 1.1 jmcneill if (error)
1574 1.1 jmcneill return (ENOBUFS);
1575 1.8 christos
1576 1.1 jmcneill /* Allocate DMA'able memory for RX ring */
1577 1.1 jmcneill error = bus_dmamem_alloc(sc->sc_dmat, ALC_RX_RING_SZ,
1578 1.1 jmcneill ETHER_ALIGN, 0, &sc->alc_rdata.alc_rx_ring_seg, 1,
1579 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1580 1.1 jmcneill if (error) {
1581 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Rx ring.\n",
1582 1.1 jmcneill device_xname(sc->sc_dev));
1583 1.1 jmcneill return error;
1584 1.1 jmcneill }
1585 1.1 jmcneill
1586 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rx_ring_seg,
1587 1.1 jmcneill nsegs, ALC_RX_RING_SZ, (void **)&sc->alc_rdata.alc_rx_ring,
1588 1.1 jmcneill BUS_DMA_NOWAIT);
1589 1.1 jmcneill if (error)
1590 1.1 jmcneill return (ENOBUFS);
1591 1.1 jmcneill
1592 1.1 jmcneill /* Load the DMA map for Rx ring. */
1593 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map,
1594 1.1 jmcneill sc->alc_rdata.alc_rx_ring, ALC_RX_RING_SZ, NULL, BUS_DMA_WAITOK);
1595 1.1 jmcneill if (error) {
1596 1.1 jmcneill printf("%s: could not load DMA'able memory for Rx ring.\n",
1597 1.1 jmcneill device_xname(sc->sc_dev));
1598 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1599 1.1 jmcneill &sc->alc_rdata.alc_rx_ring_seg, 1);
1600 1.1 jmcneill return error;
1601 1.1 jmcneill }
1602 1.1 jmcneill
1603 1.1 jmcneill sc->alc_rdata.alc_rx_ring_paddr =
1604 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_segs[0].ds_addr;
1605 1.1 jmcneill
1606 1.1 jmcneill /*
1607 1.1 jmcneill * Create DMA stuffs for RX return ring
1608 1.1 jmcneill */
1609 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_RR_RING_SZ, 1,
1610 1.1 jmcneill ALC_RR_RING_SZ, 0, BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rr_ring_map);
1611 1.1 jmcneill if (error)
1612 1.1 jmcneill return (ENOBUFS);
1613 1.1 jmcneill
1614 1.1 jmcneill /* Allocate DMA'able memory for RX return ring */
1615 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_RR_RING_SZ,
1616 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_rr_ring_seg, 1,
1617 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1618 1.1 jmcneill if (error) {
1619 1.1 jmcneill printf("%s: could not allocate DMA'able memory for Rx "
1620 1.1 jmcneill "return ring.\n", device_xname(sc->sc_dev));
1621 1.1 jmcneill return error;
1622 1.1 jmcneill }
1623 1.1 jmcneill
1624 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_rr_ring_seg,
1625 1.1 jmcneill nsegs, ALC_RR_RING_SZ, (void **)&sc->alc_rdata.alc_rr_ring,
1626 1.1 jmcneill BUS_DMA_NOWAIT);
1627 1.1 jmcneill if (error)
1628 1.1 jmcneill return (ENOBUFS);
1629 1.1 jmcneill
1630 1.1 jmcneill /* Load the DMA map for Rx return ring. */
1631 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map,
1632 1.1 jmcneill sc->alc_rdata.alc_rr_ring, ALC_RR_RING_SZ, NULL, BUS_DMA_WAITOK);
1633 1.1 jmcneill if (error) {
1634 1.1 jmcneill printf("%s: could not load DMA'able memory for Rx return ring."
1635 1.1 jmcneill "\n", device_xname(sc->sc_dev));
1636 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1637 1.1 jmcneill &sc->alc_rdata.alc_rr_ring_seg, 1);
1638 1.1 jmcneill return error;
1639 1.1 jmcneill }
1640 1.1 jmcneill
1641 1.8 christos sc->alc_rdata.alc_rr_ring_paddr =
1642 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map->dm_segs[0].ds_addr;
1643 1.1 jmcneill
1644 1.1 jmcneill /*
1645 1.8 christos * Create DMA stuffs for CMB block
1646 1.1 jmcneill */
1647 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_CMB_SZ, 1,
1648 1.8 christos ALC_CMB_SZ, 0, BUS_DMA_NOWAIT,
1649 1.1 jmcneill &sc->alc_cdata.alc_cmb_map);
1650 1.8 christos if (error)
1651 1.1 jmcneill return (ENOBUFS);
1652 1.1 jmcneill
1653 1.1 jmcneill /* Allocate DMA'able memory for CMB block */
1654 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_CMB_SZ,
1655 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_cmb_seg, 1,
1656 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1657 1.1 jmcneill if (error) {
1658 1.1 jmcneill printf("%s: could not allocate DMA'able memory for "
1659 1.1 jmcneill "CMB block\n", device_xname(sc->sc_dev));
1660 1.1 jmcneill return error;
1661 1.1 jmcneill }
1662 1.1 jmcneill
1663 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_cmb_seg,
1664 1.1 jmcneill nsegs, ALC_CMB_SZ, (void **)&sc->alc_rdata.alc_cmb,
1665 1.1 jmcneill BUS_DMA_NOWAIT);
1666 1.1 jmcneill if (error)
1667 1.1 jmcneill return (ENOBUFS);
1668 1.1 jmcneill
1669 1.1 jmcneill /* Load the DMA map for CMB block. */
1670 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_cmb_map,
1671 1.8 christos sc->alc_rdata.alc_cmb, ALC_CMB_SZ, NULL,
1672 1.1 jmcneill BUS_DMA_WAITOK);
1673 1.1 jmcneill if (error) {
1674 1.1 jmcneill printf("%s: could not load DMA'able memory for CMB block\n",
1675 1.1 jmcneill device_xname(sc->sc_dev));
1676 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1677 1.1 jmcneill &sc->alc_rdata.alc_cmb_seg, 1);
1678 1.1 jmcneill return error;
1679 1.1 jmcneill }
1680 1.1 jmcneill
1681 1.8 christos sc->alc_rdata.alc_cmb_paddr =
1682 1.1 jmcneill sc->alc_cdata.alc_cmb_map->dm_segs[0].ds_addr;
1683 1.1 jmcneill
1684 1.1 jmcneill /*
1685 1.1 jmcneill * Create DMA stuffs for SMB block
1686 1.1 jmcneill */
1687 1.8 christos error = bus_dmamap_create(sc->sc_dmat, ALC_SMB_SZ, 1,
1688 1.8 christos ALC_SMB_SZ, 0, BUS_DMA_NOWAIT,
1689 1.1 jmcneill &sc->alc_cdata.alc_smb_map);
1690 1.1 jmcneill if (error)
1691 1.1 jmcneill return (ENOBUFS);
1692 1.1 jmcneill
1693 1.1 jmcneill /* Allocate DMA'able memory for SMB block */
1694 1.8 christos error = bus_dmamem_alloc(sc->sc_dmat, ALC_SMB_SZ,
1695 1.8 christos ETHER_ALIGN, 0, &sc->alc_rdata.alc_smb_seg, 1,
1696 1.1 jmcneill &nsegs, BUS_DMA_NOWAIT);
1697 1.1 jmcneill if (error) {
1698 1.1 jmcneill printf("%s: could not allocate DMA'able memory for "
1699 1.1 jmcneill "SMB block\n", device_xname(sc->sc_dev));
1700 1.1 jmcneill return error;
1701 1.1 jmcneill }
1702 1.1 jmcneill
1703 1.1 jmcneill error = bus_dmamem_map(sc->sc_dmat, &sc->alc_rdata.alc_smb_seg,
1704 1.1 jmcneill nsegs, ALC_SMB_SZ, (void **)&sc->alc_rdata.alc_smb,
1705 1.1 jmcneill BUS_DMA_NOWAIT);
1706 1.1 jmcneill if (error)
1707 1.1 jmcneill return (ENOBUFS);
1708 1.1 jmcneill
1709 1.1 jmcneill /* Load the DMA map for SMB block */
1710 1.1 jmcneill error = bus_dmamap_load(sc->sc_dmat, sc->alc_cdata.alc_smb_map,
1711 1.8 christos sc->alc_rdata.alc_smb, ALC_SMB_SZ, NULL,
1712 1.1 jmcneill BUS_DMA_WAITOK);
1713 1.1 jmcneill if (error) {
1714 1.1 jmcneill printf("%s: could not load DMA'able memory for SMB block\n",
1715 1.1 jmcneill device_xname(sc->sc_dev));
1716 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1717 1.1 jmcneill &sc->alc_rdata.alc_smb_seg, 1);
1718 1.1 jmcneill return error;
1719 1.1 jmcneill }
1720 1.1 jmcneill
1721 1.8 christos sc->alc_rdata.alc_smb_paddr =
1722 1.1 jmcneill sc->alc_cdata.alc_smb_map->dm_segs[0].ds_addr;
1723 1.1 jmcneill
1724 1.1 jmcneill
1725 1.1 jmcneill /* Create DMA maps for Tx buffers. */
1726 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
1727 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
1728 1.1 jmcneill txd->tx_m = NULL;
1729 1.1 jmcneill txd->tx_dmamap = NULL;
1730 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, ALC_TSO_MAXSIZE,
1731 1.1 jmcneill ALC_MAXTXSEGS, ALC_TSO_MAXSEGSIZE, 0, BUS_DMA_NOWAIT,
1732 1.1 jmcneill &txd->tx_dmamap);
1733 1.1 jmcneill if (error) {
1734 1.1 jmcneill printf("%s: could not create Tx dmamap.\n",
1735 1.1 jmcneill device_xname(sc->sc_dev));
1736 1.1 jmcneill return error;
1737 1.1 jmcneill }
1738 1.1 jmcneill }
1739 1.1 jmcneill
1740 1.1 jmcneill /* Create DMA maps for Rx buffers. */
1741 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
1742 1.1 jmcneill BUS_DMA_NOWAIT, &sc->alc_cdata.alc_rx_sparemap);
1743 1.1 jmcneill if (error) {
1744 1.1 jmcneill printf("%s: could not create spare Rx dmamap.\n",
1745 1.1 jmcneill device_xname(sc->sc_dev));
1746 1.1 jmcneill return error;
1747 1.1 jmcneill }
1748 1.1 jmcneill
1749 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
1750 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
1751 1.1 jmcneill rxd->rx_m = NULL;
1752 1.1 jmcneill rxd->rx_dmamap = NULL;
1753 1.1 jmcneill error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1754 1.1 jmcneill MCLBYTES, 0, BUS_DMA_NOWAIT, &rxd->rx_dmamap);
1755 1.1 jmcneill if (error) {
1756 1.1 jmcneill printf("%s: could not create Rx dmamap.\n",
1757 1.1 jmcneill device_xname(sc->sc_dev));
1758 1.1 jmcneill return error;
1759 1.1 jmcneill }
1760 1.1 jmcneill }
1761 1.1 jmcneill
1762 1.1 jmcneill return (0);
1763 1.1 jmcneill }
1764 1.1 jmcneill
1765 1.1 jmcneill
1766 1.1 jmcneill static void
1767 1.1 jmcneill alc_dma_free(struct alc_softc *sc)
1768 1.1 jmcneill {
1769 1.1 jmcneill struct alc_txdesc *txd;
1770 1.1 jmcneill struct alc_rxdesc *rxd;
1771 1.1 jmcneill int i;
1772 1.1 jmcneill
1773 1.1 jmcneill /* Tx buffers */
1774 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
1775 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
1776 1.1 jmcneill if (txd->tx_dmamap != NULL) {
1777 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, txd->tx_dmamap);
1778 1.1 jmcneill txd->tx_dmamap = NULL;
1779 1.1 jmcneill }
1780 1.1 jmcneill }
1781 1.1 jmcneill /* Rx buffers */
1782 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
1783 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
1784 1.1 jmcneill if (rxd->rx_dmamap != NULL) {
1785 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, rxd->rx_dmamap);
1786 1.1 jmcneill rxd->rx_dmamap = NULL;
1787 1.1 jmcneill }
1788 1.1 jmcneill }
1789 1.1 jmcneill if (sc->alc_cdata.alc_rx_sparemap != NULL) {
1790 1.1 jmcneill bus_dmamap_destroy(sc->sc_dmat, sc->alc_cdata.alc_rx_sparemap);
1791 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap = NULL;
1792 1.1 jmcneill }
1793 1.1 jmcneill
1794 1.1 jmcneill /* Tx ring. */
1795 1.1 jmcneill if (sc->alc_cdata.alc_tx_ring_map != NULL)
1796 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map);
1797 1.1 jmcneill if (sc->alc_cdata.alc_tx_ring_map != NULL &&
1798 1.1 jmcneill sc->alc_rdata.alc_tx_ring != NULL)
1799 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1800 1.1 jmcneill &sc->alc_rdata.alc_tx_ring_seg, 1);
1801 1.1 jmcneill sc->alc_rdata.alc_tx_ring = NULL;
1802 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map = NULL;
1803 1.1 jmcneill
1804 1.1 jmcneill /* Rx ring. */
1805 1.8 christos if (sc->alc_cdata.alc_rx_ring_map != NULL)
1806 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map);
1807 1.1 jmcneill if (sc->alc_cdata.alc_rx_ring_map != NULL &&
1808 1.1 jmcneill sc->alc_rdata.alc_rx_ring != NULL)
1809 1.8 christos bus_dmamem_free(sc->sc_dmat,
1810 1.1 jmcneill &sc->alc_rdata.alc_rx_ring_seg, 1);
1811 1.1 jmcneill sc->alc_rdata.alc_rx_ring = NULL;
1812 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map = NULL;
1813 1.1 jmcneill
1814 1.1 jmcneill /* Rx return ring. */
1815 1.1 jmcneill if (sc->alc_cdata.alc_rr_ring_map != NULL)
1816 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map);
1817 1.1 jmcneill if (sc->alc_cdata.alc_rr_ring_map != NULL &&
1818 1.1 jmcneill sc->alc_rdata.alc_rr_ring != NULL)
1819 1.8 christos bus_dmamem_free(sc->sc_dmat,
1820 1.1 jmcneill &sc->alc_rdata.alc_rr_ring_seg, 1);
1821 1.1 jmcneill sc->alc_rdata.alc_rr_ring = NULL;
1822 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map = NULL;
1823 1.1 jmcneill
1824 1.1 jmcneill /* CMB block */
1825 1.1 jmcneill if (sc->alc_cdata.alc_cmb_map != NULL)
1826 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_cmb_map);
1827 1.1 jmcneill if (sc->alc_cdata.alc_cmb_map != NULL &&
1828 1.1 jmcneill sc->alc_rdata.alc_cmb != NULL)
1829 1.1 jmcneill bus_dmamem_free(sc->sc_dmat,
1830 1.1 jmcneill &sc->alc_rdata.alc_cmb_seg, 1);
1831 1.1 jmcneill sc->alc_rdata.alc_cmb = NULL;
1832 1.1 jmcneill sc->alc_cdata.alc_cmb_map = NULL;
1833 1.1 jmcneill
1834 1.1 jmcneill /* SMB block */
1835 1.1 jmcneill if (sc->alc_cdata.alc_smb_map != NULL)
1836 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, sc->alc_cdata.alc_smb_map);
1837 1.1 jmcneill if (sc->alc_cdata.alc_smb_map != NULL &&
1838 1.1 jmcneill sc->alc_rdata.alc_smb != NULL)
1839 1.8 christos bus_dmamem_free(sc->sc_dmat,
1840 1.1 jmcneill &sc->alc_rdata.alc_smb_seg, 1);
1841 1.1 jmcneill sc->alc_rdata.alc_smb = NULL;
1842 1.1 jmcneill sc->alc_cdata.alc_smb_map = NULL;
1843 1.1 jmcneill }
1844 1.1 jmcneill
1845 1.1 jmcneill static int
1846 1.1 jmcneill alc_encap(struct alc_softc *sc, struct mbuf **m_head)
1847 1.1 jmcneill {
1848 1.1 jmcneill struct alc_txdesc *txd, *txd_last;
1849 1.1 jmcneill struct tx_desc *desc;
1850 1.1 jmcneill struct mbuf *m;
1851 1.1 jmcneill bus_dmamap_t map;
1852 1.1 jmcneill uint32_t cflags, poff, vtag;
1853 1.1 jmcneill int error, idx, nsegs, prod;
1854 1.1 jmcneill
1855 1.1 jmcneill m = *m_head;
1856 1.1 jmcneill cflags = vtag = 0;
1857 1.1 jmcneill poff = 0;
1858 1.1 jmcneill
1859 1.1 jmcneill prod = sc->alc_cdata.alc_tx_prod;
1860 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[prod];
1861 1.1 jmcneill txd_last = txd;
1862 1.1 jmcneill map = txd->tx_dmamap;
1863 1.1 jmcneill
1864 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head, BUS_DMA_NOWAIT);
1865 1.1 jmcneill
1866 1.1 jmcneill if (error == EFBIG) {
1867 1.1 jmcneill error = 0;
1868 1.1 jmcneill
1869 1.1 jmcneill *m_head = m_pullup(*m_head, MHLEN);
1870 1.1 jmcneill if (*m_head == NULL) {
1871 1.1 jmcneill printf("%s: can't defrag TX mbuf\n",
1872 1.1 jmcneill device_xname(sc->sc_dev));
1873 1.1 jmcneill return ENOBUFS;
1874 1.1 jmcneill }
1875 1.1 jmcneill
1876 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat, map, *m_head,
1877 1.1 jmcneill BUS_DMA_NOWAIT);
1878 1.1 jmcneill
1879 1.1 jmcneill if (error != 0) {
1880 1.1 jmcneill printf("%s: could not load defragged TX mbuf\n",
1881 1.1 jmcneill device_xname(sc->sc_dev));
1882 1.1 jmcneill m_freem(*m_head);
1883 1.1 jmcneill *m_head = NULL;
1884 1.1 jmcneill return error;
1885 1.1 jmcneill }
1886 1.1 jmcneill } else if (error) {
1887 1.1 jmcneill printf("%s: could not load TX mbuf\n", device_xname(sc->sc_dev));
1888 1.1 jmcneill return (error);
1889 1.1 jmcneill }
1890 1.1 jmcneill
1891 1.1 jmcneill nsegs = map->dm_nsegs;
1892 1.1 jmcneill
1893 1.1 jmcneill if (nsegs == 0) {
1894 1.1 jmcneill m_freem(*m_head);
1895 1.1 jmcneill *m_head = NULL;
1896 1.1 jmcneill return (EIO);
1897 1.1 jmcneill }
1898 1.1 jmcneill
1899 1.1 jmcneill /* Check descriptor overrun. */
1900 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt + nsegs >= ALC_TX_RING_CNT - 3) {
1901 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, map);
1902 1.1 jmcneill return (ENOBUFS);
1903 1.1 jmcneill }
1904 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1905 1.1 jmcneill BUS_DMASYNC_PREWRITE);
1906 1.1 jmcneill
1907 1.1 jmcneill m = *m_head;
1908 1.1 jmcneill desc = NULL;
1909 1.1 jmcneill idx = 0;
1910 1.1 jmcneill #if NVLAN > 0
1911 1.1 jmcneill /* Configure VLAN hardware tag insertion. */
1912 1.25 knakahar if (vlan_has_tag(m)) {
1913 1.25 knakahar vtag = htons(vlan_get_tag(m));
1914 1.1 jmcneill vtag = (vtag << TD_VLAN_SHIFT) & TD_VLAN_MASK;
1915 1.1 jmcneill cflags |= TD_INS_VLAN_TAG;
1916 1.1 jmcneill }
1917 1.1 jmcneill #endif
1918 1.1 jmcneill /* Configure Tx checksum offload. */
1919 1.1 jmcneill if ((m->m_pkthdr.csum_flags & ALC_CSUM_FEATURES) != 0) {
1920 1.1 jmcneill cflags |= TD_CUSTOM_CSUM;
1921 1.1 jmcneill /* Set checksum start offset. */
1922 1.1 jmcneill cflags |= ((poff >> 1) << TD_PLOAD_OFFSET_SHIFT) &
1923 1.1 jmcneill TD_PLOAD_OFFSET_MASK;
1924 1.8 christos }
1925 1.1 jmcneill for (; idx < nsegs; idx++) {
1926 1.1 jmcneill desc = &sc->alc_rdata.alc_tx_ring[prod];
1927 1.1 jmcneill desc->len =
1928 1.1 jmcneill htole32(TX_BYTES(map->dm_segs[idx].ds_len) | vtag);
1929 1.1 jmcneill desc->flags = htole32(cflags);
1930 1.1 jmcneill desc->addr = htole64(map->dm_segs[idx].ds_addr);
1931 1.1 jmcneill sc->alc_cdata.alc_tx_cnt++;
1932 1.1 jmcneill ALC_DESC_INC(prod, ALC_TX_RING_CNT);
1933 1.1 jmcneill }
1934 1.1 jmcneill /* Update producer index. */
1935 1.1 jmcneill sc->alc_cdata.alc_tx_prod = prod;
1936 1.1 jmcneill
1937 1.1 jmcneill /* Finally set EOP on the last descriptor. */
1938 1.1 jmcneill prod = (prod + ALC_TX_RING_CNT - 1) % ALC_TX_RING_CNT;
1939 1.1 jmcneill desc = &sc->alc_rdata.alc_tx_ring[prod];
1940 1.1 jmcneill desc->flags |= htole32(TD_EOP);
1941 1.1 jmcneill
1942 1.1 jmcneill /* Swap dmamap of the first and the last. */
1943 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[prod];
1944 1.1 jmcneill map = txd_last->tx_dmamap;
1945 1.1 jmcneill txd_last->tx_dmamap = txd->tx_dmamap;
1946 1.1 jmcneill txd->tx_dmamap = map;
1947 1.1 jmcneill txd->tx_m = m;
1948 1.1 jmcneill
1949 1.1 jmcneill return (0);
1950 1.1 jmcneill }
1951 1.1 jmcneill
1952 1.1 jmcneill static void
1953 1.1 jmcneill alc_start(struct ifnet *ifp)
1954 1.1 jmcneill {
1955 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
1956 1.1 jmcneill struct mbuf *m_head;
1957 1.1 jmcneill int enq;
1958 1.1 jmcneill
1959 1.1 jmcneill if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1960 1.1 jmcneill return;
1961 1.15 leot if ((sc->alc_flags & ALC_FLAG_LINK) == 0)
1962 1.15 leot return;
1963 1.15 leot if (IFQ_IS_EMPTY(&ifp->if_snd))
1964 1.15 leot return;
1965 1.1 jmcneill
1966 1.1 jmcneill /* Reclaim transmitted frames. */
1967 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt >= ALC_TX_DESC_HIWAT)
1968 1.1 jmcneill alc_txeof(sc);
1969 1.1 jmcneill
1970 1.1 jmcneill enq = 0;
1971 1.1 jmcneill for (;;) {
1972 1.1 jmcneill IFQ_DEQUEUE(&ifp->if_snd, m_head);
1973 1.1 jmcneill if (m_head == NULL)
1974 1.1 jmcneill break;
1975 1.1 jmcneill
1976 1.1 jmcneill /*
1977 1.1 jmcneill * Pack the data into the transmit ring. If we
1978 1.1 jmcneill * don't have room, set the OACTIVE flag and wait
1979 1.1 jmcneill * for the NIC to drain the ring.
1980 1.1 jmcneill */
1981 1.1 jmcneill if (alc_encap(sc, &m_head)) {
1982 1.1 jmcneill if (m_head == NULL)
1983 1.1 jmcneill break;
1984 1.1 jmcneill ifp->if_flags |= IFF_OACTIVE;
1985 1.1 jmcneill break;
1986 1.1 jmcneill }
1987 1.1 jmcneill enq = 1;
1988 1.8 christos
1989 1.1 jmcneill /*
1990 1.1 jmcneill * If there's a BPF listener, bounce a copy of this frame
1991 1.1 jmcneill * to him.
1992 1.1 jmcneill */
1993 1.28 msaitoh bpf_mtap(ifp, m_head, BPF_D_OUT);
1994 1.1 jmcneill }
1995 1.1 jmcneill
1996 1.1 jmcneill if (enq) {
1997 1.1 jmcneill /* Sync descriptors. */
1998 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
1999 1.8 christos sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
2000 1.1 jmcneill BUS_DMASYNC_PREWRITE);
2001 1.1 jmcneill /* Kick. Assume we're using normal Tx priority queue. */
2002 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_TD_PROD_IDX,
2003 1.1 jmcneill (sc->alc_cdata.alc_tx_prod <<
2004 1.1 jmcneill MBOX_TD_PROD_LO_IDX_SHIFT) &
2005 1.1 jmcneill MBOX_TD_PROD_LO_IDX_MASK);
2006 1.1 jmcneill /* Set a timeout in case the chip goes out to lunch. */
2007 1.1 jmcneill ifp->if_timer = ALC_TX_TIMEOUT;
2008 1.1 jmcneill }
2009 1.1 jmcneill }
2010 1.1 jmcneill
2011 1.1 jmcneill static void
2012 1.1 jmcneill alc_watchdog(struct ifnet *ifp)
2013 1.1 jmcneill {
2014 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
2015 1.1 jmcneill
2016 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_LINK) == 0) {
2017 1.1 jmcneill printf("%s: watchdog timeout (missed link)\n",
2018 1.1 jmcneill device_xname(sc->sc_dev));
2019 1.1 jmcneill ifp->if_oerrors++;
2020 1.7 mrg alc_init_backend(ifp, false);
2021 1.1 jmcneill return;
2022 1.1 jmcneill }
2023 1.1 jmcneill
2024 1.1 jmcneill printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
2025 1.1 jmcneill ifp->if_oerrors++;
2026 1.7 mrg alc_init_backend(ifp, false);
2027 1.15 leot 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.16 christos struct mii_data *mii = &sc->sc_miibus;
2035 1.16 christos struct ifreq *ifr = (struct ifreq *)data;
2036 1.1 jmcneill int s, error = 0;
2037 1.1 jmcneill
2038 1.1 jmcneill s = splnet();
2039 1.1 jmcneill
2040 1.1 jmcneill error = ether_ioctl(ifp, cmd, data);
2041 1.16 christos switch (cmd) {
2042 1.16 christos case SIOCSIFADDR:
2043 1.16 christos ifp->if_flags |= IFF_UP;
2044 1.16 christos if (!(ifp->if_flags & IFF_RUNNING))
2045 1.16 christos alc_init(ifp);
2046 1.16 christos break;
2047 1.16 christos
2048 1.16 christos case SIOCSIFFLAGS:
2049 1.16 christos if (ifp->if_flags & IFF_UP) {
2050 1.16 christos if (ifp->if_flags & IFF_RUNNING)
2051 1.16 christos error = ENETRESET;
2052 1.16 christos else
2053 1.16 christos alc_init(ifp);
2054 1.16 christos } else {
2055 1.16 christos if (ifp->if_flags & IFF_RUNNING)
2056 1.16 christos alc_stop(ifp, 0);
2057 1.16 christos }
2058 1.16 christos break;
2059 1.16 christos
2060 1.16 christos case SIOCSIFMEDIA:
2061 1.16 christos case SIOCGIFMEDIA:
2062 1.16 christos error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
2063 1.16 christos break;
2064 1.16 christos
2065 1.16 christos default:
2066 1.16 christos error = ether_ioctl(ifp, cmd, data);
2067 1.16 christos break;
2068 1.16 christos }
2069 1.16 christos
2070 1.1 jmcneill if (error == ENETRESET) {
2071 1.1 jmcneill if (ifp->if_flags & IFF_RUNNING)
2072 1.1 jmcneill alc_iff(sc);
2073 1.1 jmcneill error = 0;
2074 1.1 jmcneill }
2075 1.1 jmcneill
2076 1.1 jmcneill splx(s);
2077 1.1 jmcneill return (error);
2078 1.1 jmcneill }
2079 1.1 jmcneill
2080 1.1 jmcneill static void
2081 1.1 jmcneill alc_mac_config(struct alc_softc *sc)
2082 1.1 jmcneill {
2083 1.1 jmcneill struct mii_data *mii;
2084 1.1 jmcneill uint32_t reg;
2085 1.1 jmcneill
2086 1.1 jmcneill mii = &sc->sc_miibus;
2087 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
2088 1.1 jmcneill reg &= ~(MAC_CFG_FULL_DUPLEX | MAC_CFG_TX_FC | MAC_CFG_RX_FC |
2089 1.1 jmcneill MAC_CFG_SPEED_MASK);
2090 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
2091 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
2092 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
2093 1.2 jmcneill reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
2094 1.1 jmcneill /* Reprogram MAC with resolved speed/duplex. */
2095 1.1 jmcneill switch (IFM_SUBTYPE(mii->mii_media_active)) {
2096 1.1 jmcneill case IFM_10_T:
2097 1.1 jmcneill case IFM_100_TX:
2098 1.1 jmcneill reg |= MAC_CFG_SPEED_10_100;
2099 1.1 jmcneill break;
2100 1.1 jmcneill case IFM_1000_T:
2101 1.1 jmcneill reg |= MAC_CFG_SPEED_1000;
2102 1.1 jmcneill break;
2103 1.1 jmcneill }
2104 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
2105 1.1 jmcneill reg |= MAC_CFG_FULL_DUPLEX;
2106 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
2107 1.1 jmcneill reg |= MAC_CFG_TX_FC;
2108 1.1 jmcneill if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
2109 1.1 jmcneill reg |= MAC_CFG_RX_FC;
2110 1.1 jmcneill }
2111 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
2112 1.1 jmcneill }
2113 1.1 jmcneill
2114 1.1 jmcneill static void
2115 1.1 jmcneill alc_stats_clear(struct alc_softc *sc)
2116 1.1 jmcneill {
2117 1.1 jmcneill struct smb sb, *smb;
2118 1.1 jmcneill uint32_t *reg;
2119 1.1 jmcneill int i;
2120 1.1 jmcneill
2121 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2122 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2123 1.8 christos sc->alc_cdata.alc_smb_map->dm_mapsize,
2124 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2125 1.1 jmcneill smb = sc->alc_rdata.alc_smb;
2126 1.1 jmcneill /* Update done, clear. */
2127 1.1 jmcneill smb->updated = 0;
2128 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2129 1.8 christos sc->alc_cdata.alc_smb_map->dm_mapsize,
2130 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2131 1.1 jmcneill } else {
2132 1.1 jmcneill for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2133 1.1 jmcneill reg++) {
2134 1.1 jmcneill CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2135 1.1 jmcneill i += sizeof(uint32_t);
2136 1.1 jmcneill }
2137 1.1 jmcneill /* Read Tx statistics. */
2138 1.1 jmcneill for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2139 1.1 jmcneill reg++) {
2140 1.1 jmcneill CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2141 1.1 jmcneill i += sizeof(uint32_t);
2142 1.1 jmcneill }
2143 1.1 jmcneill }
2144 1.1 jmcneill }
2145 1.1 jmcneill
2146 1.1 jmcneill static void
2147 1.1 jmcneill alc_stats_update(struct alc_softc *sc)
2148 1.1 jmcneill {
2149 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2150 1.1 jmcneill struct alc_hw_stats *stat;
2151 1.1 jmcneill struct smb sb, *smb;
2152 1.1 jmcneill uint32_t *reg;
2153 1.1 jmcneill int i;
2154 1.1 jmcneill
2155 1.1 jmcneill stat = &sc->alc_stats;
2156 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2157 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2158 1.1 jmcneill sc->alc_cdata.alc_smb_map->dm_mapsize,
2159 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2160 1.1 jmcneill smb = sc->alc_rdata.alc_smb;
2161 1.1 jmcneill if (smb->updated == 0)
2162 1.1 jmcneill return;
2163 1.1 jmcneill } else {
2164 1.1 jmcneill smb = &sb;
2165 1.1 jmcneill /* Read Rx statistics. */
2166 1.1 jmcneill for (reg = &sb.rx_frames, i = 0; reg <= &sb.rx_pkts_filtered;
2167 1.1 jmcneill reg++) {
2168 1.1 jmcneill *reg = CSR_READ_4(sc, ALC_RX_MIB_BASE + i);
2169 1.1 jmcneill i += sizeof(uint32_t);
2170 1.1 jmcneill }
2171 1.1 jmcneill /* Read Tx statistics. */
2172 1.1 jmcneill for (reg = &sb.tx_frames, i = 0; reg <= &sb.tx_mcast_bytes;
2173 1.1 jmcneill reg++) {
2174 1.1 jmcneill *reg = CSR_READ_4(sc, ALC_TX_MIB_BASE + i);
2175 1.1 jmcneill i += sizeof(uint32_t);
2176 1.1 jmcneill }
2177 1.1 jmcneill }
2178 1.1 jmcneill
2179 1.1 jmcneill /* Rx stats. */
2180 1.1 jmcneill stat->rx_frames += smb->rx_frames;
2181 1.1 jmcneill stat->rx_bcast_frames += smb->rx_bcast_frames;
2182 1.1 jmcneill stat->rx_mcast_frames += smb->rx_mcast_frames;
2183 1.1 jmcneill stat->rx_pause_frames += smb->rx_pause_frames;
2184 1.1 jmcneill stat->rx_control_frames += smb->rx_control_frames;
2185 1.1 jmcneill stat->rx_crcerrs += smb->rx_crcerrs;
2186 1.1 jmcneill stat->rx_lenerrs += smb->rx_lenerrs;
2187 1.1 jmcneill stat->rx_bytes += smb->rx_bytes;
2188 1.1 jmcneill stat->rx_runts += smb->rx_runts;
2189 1.1 jmcneill stat->rx_fragments += smb->rx_fragments;
2190 1.1 jmcneill stat->rx_pkts_64 += smb->rx_pkts_64;
2191 1.1 jmcneill stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
2192 1.1 jmcneill stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
2193 1.1 jmcneill stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
2194 1.1 jmcneill stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
2195 1.1 jmcneill stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
2196 1.1 jmcneill stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
2197 1.1 jmcneill stat->rx_pkts_truncated += smb->rx_pkts_truncated;
2198 1.1 jmcneill stat->rx_fifo_oflows += smb->rx_fifo_oflows;
2199 1.1 jmcneill stat->rx_rrs_errs += smb->rx_rrs_errs;
2200 1.1 jmcneill stat->rx_alignerrs += smb->rx_alignerrs;
2201 1.1 jmcneill stat->rx_bcast_bytes += smb->rx_bcast_bytes;
2202 1.1 jmcneill stat->rx_mcast_bytes += smb->rx_mcast_bytes;
2203 1.1 jmcneill stat->rx_pkts_filtered += smb->rx_pkts_filtered;
2204 1.1 jmcneill
2205 1.1 jmcneill /* Tx stats. */
2206 1.1 jmcneill stat->tx_frames += smb->tx_frames;
2207 1.1 jmcneill stat->tx_bcast_frames += smb->tx_bcast_frames;
2208 1.1 jmcneill stat->tx_mcast_frames += smb->tx_mcast_frames;
2209 1.1 jmcneill stat->tx_pause_frames += smb->tx_pause_frames;
2210 1.1 jmcneill stat->tx_excess_defer += smb->tx_excess_defer;
2211 1.1 jmcneill stat->tx_control_frames += smb->tx_control_frames;
2212 1.1 jmcneill stat->tx_deferred += smb->tx_deferred;
2213 1.1 jmcneill stat->tx_bytes += smb->tx_bytes;
2214 1.1 jmcneill stat->tx_pkts_64 += smb->tx_pkts_64;
2215 1.1 jmcneill stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
2216 1.1 jmcneill stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
2217 1.1 jmcneill stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
2218 1.1 jmcneill stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
2219 1.1 jmcneill stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
2220 1.1 jmcneill stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
2221 1.1 jmcneill stat->tx_single_colls += smb->tx_single_colls;
2222 1.1 jmcneill stat->tx_multi_colls += smb->tx_multi_colls;
2223 1.1 jmcneill stat->tx_late_colls += smb->tx_late_colls;
2224 1.1 jmcneill stat->tx_excess_colls += smb->tx_excess_colls;
2225 1.1 jmcneill stat->tx_underrun += smb->tx_underrun;
2226 1.1 jmcneill stat->tx_desc_underrun += smb->tx_desc_underrun;
2227 1.1 jmcneill stat->tx_lenerrs += smb->tx_lenerrs;
2228 1.1 jmcneill stat->tx_pkts_truncated += smb->tx_pkts_truncated;
2229 1.1 jmcneill stat->tx_bcast_bytes += smb->tx_bcast_bytes;
2230 1.1 jmcneill stat->tx_mcast_bytes += smb->tx_mcast_bytes;
2231 1.1 jmcneill
2232 1.1 jmcneill /* Update counters in ifnet. */
2233 1.1 jmcneill ifp->if_opackets += smb->tx_frames;
2234 1.1 jmcneill
2235 1.1 jmcneill ifp->if_collisions += smb->tx_single_colls +
2236 1.1 jmcneill smb->tx_multi_colls * 2 + smb->tx_late_colls +
2237 1.15 leot smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT;
2238 1.1 jmcneill
2239 1.15 leot ifp->if_oerrors += smb->tx_late_colls + smb->tx_excess_colls +
2240 1.15 leot smb->tx_underrun + smb->tx_pkts_truncated;
2241 1.1 jmcneill
2242 1.1 jmcneill ifp->if_ipackets += smb->rx_frames;
2243 1.1 jmcneill
2244 1.1 jmcneill ifp->if_ierrors += smb->rx_crcerrs + smb->rx_lenerrs +
2245 1.1 jmcneill smb->rx_runts + smb->rx_pkts_truncated +
2246 1.1 jmcneill smb->rx_fifo_oflows + smb->rx_rrs_errs +
2247 1.1 jmcneill smb->rx_alignerrs;
2248 1.1 jmcneill
2249 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0) {
2250 1.1 jmcneill /* Update done, clear. */
2251 1.1 jmcneill smb->updated = 0;
2252 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
2253 1.15 leot sc->alc_cdata.alc_smb_map->dm_mapsize,
2254 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2255 1.1 jmcneill }
2256 1.1 jmcneill }
2257 1.1 jmcneill
2258 1.1 jmcneill static int
2259 1.1 jmcneill alc_intr(void *arg)
2260 1.1 jmcneill {
2261 1.1 jmcneill struct alc_softc *sc = arg;
2262 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2263 1.1 jmcneill uint32_t status;
2264 1.1 jmcneill
2265 1.1 jmcneill status = CSR_READ_4(sc, ALC_INTR_STATUS);
2266 1.1 jmcneill if ((status & ALC_INTRS) == 0)
2267 1.1 jmcneill return (0);
2268 1.1 jmcneill
2269 1.1 jmcneill /* Acknowledge and disable interrupts. */
2270 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, status | INTR_DIS_INT);
2271 1.1 jmcneill
2272 1.1 jmcneill if (ifp->if_flags & IFF_RUNNING) {
2273 1.1 jmcneill if (status & INTR_RX_PKT) {
2274 1.1 jmcneill int error;
2275 1.1 jmcneill
2276 1.1 jmcneill error = alc_rxintr(sc);
2277 1.1 jmcneill if (error) {
2278 1.7 mrg alc_init_backend(ifp, false);
2279 1.1 jmcneill return (0);
2280 1.1 jmcneill }
2281 1.1 jmcneill }
2282 1.1 jmcneill
2283 1.1 jmcneill if (status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST |
2284 1.1 jmcneill INTR_TXQ_TO_RST)) {
2285 1.1 jmcneill if (status & INTR_DMA_RD_TO_RST)
2286 1.1 jmcneill printf("%s: DMA read error! -- resetting\n",
2287 1.1 jmcneill device_xname(sc->sc_dev));
2288 1.1 jmcneill if (status & INTR_DMA_WR_TO_RST)
2289 1.1 jmcneill printf("%s: DMA write error! -- resetting\n",
2290 1.1 jmcneill device_xname(sc->sc_dev));
2291 1.1 jmcneill if (status & INTR_TXQ_TO_RST)
2292 1.1 jmcneill printf("%s: TxQ reset! -- resetting\n",
2293 1.1 jmcneill device_xname(sc->sc_dev));
2294 1.7 mrg alc_init_backend(ifp, false);
2295 1.1 jmcneill return (0);
2296 1.1 jmcneill }
2297 1.1 jmcneill
2298 1.1 jmcneill alc_txeof(sc);
2299 1.22 ozaki if_schedule_deferred_start(ifp);
2300 1.1 jmcneill }
2301 1.1 jmcneill
2302 1.1 jmcneill /* Re-enable interrupts. */
2303 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0x7FFFFFFF);
2304 1.1 jmcneill return (1);
2305 1.1 jmcneill }
2306 1.1 jmcneill
2307 1.1 jmcneill static void
2308 1.1 jmcneill alc_txeof(struct alc_softc *sc)
2309 1.1 jmcneill {
2310 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2311 1.1 jmcneill struct alc_txdesc *txd;
2312 1.1 jmcneill uint32_t cons, prod;
2313 1.1 jmcneill int prog;
2314 1.1 jmcneill
2315 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt == 0)
2316 1.1 jmcneill return;
2317 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
2318 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_mapsize,
2319 1.1 jmcneill BUS_DMASYNC_POSTREAD);
2320 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2321 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
2322 1.8 christos sc->alc_cdata.alc_cmb_map->dm_mapsize,
2323 1.1 jmcneill BUS_DMASYNC_POSTREAD);
2324 1.1 jmcneill prod = sc->alc_rdata.alc_cmb->cons;
2325 1.1 jmcneill } else
2326 1.1 jmcneill prod = CSR_READ_4(sc, ALC_MBOX_TD_CONS_IDX);
2327 1.1 jmcneill /* Assume we're using normal Tx priority queue. */
2328 1.1 jmcneill prod = (prod & MBOX_TD_CONS_LO_IDX_MASK) >>
2329 1.1 jmcneill MBOX_TD_CONS_LO_IDX_SHIFT;
2330 1.1 jmcneill cons = sc->alc_cdata.alc_tx_cons;
2331 1.1 jmcneill /*
2332 1.1 jmcneill * Go through our Tx list and free mbufs for those
2333 1.1 jmcneill * frames which have been transmitted.
2334 1.1 jmcneill */
2335 1.1 jmcneill for (prog = 0; cons != prod; prog++,
2336 1.1 jmcneill ALC_DESC_INC(cons, ALC_TX_RING_CNT)) {
2337 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt <= 0)
2338 1.1 jmcneill break;
2339 1.1 jmcneill prog++;
2340 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
2341 1.1 jmcneill sc->alc_cdata.alc_tx_cnt--;
2342 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[cons];
2343 1.1 jmcneill if (txd->tx_m != NULL) {
2344 1.1 jmcneill /* Reclaim transmitted mbufs. */
2345 1.15 leot bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
2346 1.15 leot txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2347 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
2348 1.1 jmcneill m_freem(txd->tx_m);
2349 1.1 jmcneill txd->tx_m = NULL;
2350 1.1 jmcneill }
2351 1.1 jmcneill }
2352 1.1 jmcneill
2353 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
2354 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
2355 1.15 leot sc->alc_cdata.alc_cmb_map->dm_mapsize, BUS_DMASYNC_PREREAD);
2356 1.1 jmcneill sc->alc_cdata.alc_tx_cons = cons;
2357 1.1 jmcneill /*
2358 1.1 jmcneill * Unarm watchdog timer only when there is no pending
2359 1.1 jmcneill * frames in Tx queue.
2360 1.1 jmcneill */
2361 1.1 jmcneill if (sc->alc_cdata.alc_tx_cnt == 0)
2362 1.1 jmcneill ifp->if_timer = 0;
2363 1.1 jmcneill }
2364 1.1 jmcneill
2365 1.1 jmcneill static int
2366 1.7 mrg alc_newbuf(struct alc_softc *sc, struct alc_rxdesc *rxd, bool init)
2367 1.1 jmcneill {
2368 1.1 jmcneill struct mbuf *m;
2369 1.1 jmcneill bus_dmamap_t map;
2370 1.1 jmcneill int error;
2371 1.1 jmcneill
2372 1.1 jmcneill MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
2373 1.1 jmcneill if (m == NULL)
2374 1.1 jmcneill return (ENOBUFS);
2375 1.1 jmcneill MCLGET(m, init ? M_WAITOK : M_DONTWAIT);
2376 1.1 jmcneill if (!(m->m_flags & M_EXT)) {
2377 1.1 jmcneill m_freem(m);
2378 1.1 jmcneill return (ENOBUFS);
2379 1.1 jmcneill }
2380 1.1 jmcneill
2381 1.1 jmcneill m->m_len = m->m_pkthdr.len = RX_BUF_SIZE_MAX;
2382 1.1 jmcneill
2383 1.1 jmcneill error = bus_dmamap_load_mbuf(sc->sc_dmat,
2384 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap, m, BUS_DMA_NOWAIT);
2385 1.1 jmcneill
2386 1.1 jmcneill if (error != 0) {
2387 1.1 jmcneill m_freem(m);
2388 1.1 jmcneill
2389 1.1 jmcneill if (init)
2390 1.1 jmcneill printf("%s: can't load RX mbuf\n", device_xname(sc->sc_dev));
2391 1.1 jmcneill
2392 1.1 jmcneill return (error);
2393 1.1 jmcneill }
2394 1.1 jmcneill
2395 1.1 jmcneill if (rxd->rx_m != NULL) {
2396 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
2397 1.1 jmcneill rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
2398 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
2399 1.1 jmcneill }
2400 1.1 jmcneill map = rxd->rx_dmamap;
2401 1.1 jmcneill rxd->rx_dmamap = sc->alc_cdata.alc_rx_sparemap;
2402 1.1 jmcneill sc->alc_cdata.alc_rx_sparemap = map;
2403 1.15 leot bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0, rxd->rx_dmamap->dm_mapsize,
2404 1.15 leot BUS_DMASYNC_PREREAD);
2405 1.1 jmcneill rxd->rx_m = m;
2406 1.1 jmcneill rxd->rx_desc->addr = htole64(rxd->rx_dmamap->dm_segs[0].ds_addr);
2407 1.1 jmcneill return (0);
2408 1.1 jmcneill }
2409 1.1 jmcneill
2410 1.1 jmcneill static int
2411 1.1 jmcneill alc_rxintr(struct alc_softc *sc)
2412 1.1 jmcneill {
2413 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2414 1.1 jmcneill struct rx_rdesc *rrd;
2415 1.1 jmcneill uint32_t nsegs, status;
2416 1.1 jmcneill int rr_cons, prog;
2417 1.1 jmcneill
2418 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
2419 1.15 leot sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
2420 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2421 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
2422 1.15 leot sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
2423 1.15 leot BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2424 1.1 jmcneill rr_cons = sc->alc_cdata.alc_rr_cons;
2425 1.1 jmcneill for (prog = 0; (ifp->if_flags & IFF_RUNNING) != 0;) {
2426 1.1 jmcneill rrd = &sc->alc_rdata.alc_rr_ring[rr_cons];
2427 1.1 jmcneill status = le32toh(rrd->status);
2428 1.1 jmcneill if ((status & RRD_VALID) == 0)
2429 1.1 jmcneill break;
2430 1.1 jmcneill nsegs = RRD_RD_CNT(le32toh(rrd->rdinfo));
2431 1.1 jmcneill if (nsegs == 0) {
2432 1.1 jmcneill /* This should not happen! */
2433 1.1 jmcneill if (alcdebug)
2434 1.1 jmcneill printf("%s: unexpected segment count -- "
2435 1.1 jmcneill "resetting\n", device_xname(sc->sc_dev));
2436 1.1 jmcneill return (EIO);
2437 1.1 jmcneill }
2438 1.1 jmcneill alc_rxeof(sc, rrd);
2439 1.1 jmcneill /* Clear Rx return status. */
2440 1.1 jmcneill rrd->status = 0;
2441 1.1 jmcneill ALC_DESC_INC(rr_cons, ALC_RR_RING_CNT);
2442 1.1 jmcneill sc->alc_cdata.alc_rx_cons += nsegs;
2443 1.1 jmcneill sc->alc_cdata.alc_rx_cons %= ALC_RR_RING_CNT;
2444 1.1 jmcneill prog += nsegs;
2445 1.1 jmcneill }
2446 1.1 jmcneill
2447 1.1 jmcneill if (prog > 0) {
2448 1.1 jmcneill /* Update the consumer index. */
2449 1.1 jmcneill sc->alc_cdata.alc_rr_cons = rr_cons;
2450 1.1 jmcneill /* Sync Rx return descriptors. */
2451 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
2452 1.1 jmcneill sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
2453 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2454 1.1 jmcneill /*
2455 1.1 jmcneill * Sync updated Rx descriptors such that controller see
2456 1.1 jmcneill * modified buffer addresses.
2457 1.1 jmcneill */
2458 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
2459 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_mapsize,
2460 1.1 jmcneill BUS_DMASYNC_PREWRITE);
2461 1.1 jmcneill /*
2462 1.1 jmcneill * Let controller know availability of new Rx buffers.
2463 1.1 jmcneill * Since alc(4) use RXQ_CFG_RD_BURST_DEFAULT descriptors
2464 1.1 jmcneill * it may be possible to update ALC_MBOX_RD0_PROD_IDX
2465 1.1 jmcneill * only when Rx buffer pre-fetching is required. In
2466 1.1 jmcneill * addition we already set ALC_RX_RD_FREE_THRESH to
2467 1.1 jmcneill * RX_RD_FREE_THRESH_LO_DEFAULT descriptors. However
2468 1.1 jmcneill * it still seems that pre-fetching needs more
2469 1.1 jmcneill * experimentation.
2470 1.1 jmcneill */
2471 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX,
2472 1.1 jmcneill sc->alc_cdata.alc_rx_cons);
2473 1.1 jmcneill }
2474 1.1 jmcneill
2475 1.1 jmcneill return (0);
2476 1.1 jmcneill }
2477 1.1 jmcneill
2478 1.1 jmcneill /* Receive a frame. */
2479 1.1 jmcneill static void
2480 1.1 jmcneill alc_rxeof(struct alc_softc *sc, struct rx_rdesc *rrd)
2481 1.1 jmcneill {
2482 1.1 jmcneill struct ifnet *ifp = &sc->sc_ec.ec_if;
2483 1.1 jmcneill struct alc_rxdesc *rxd;
2484 1.1 jmcneill struct mbuf *mp, *m;
2485 1.1 jmcneill uint32_t rdinfo, status;
2486 1.1 jmcneill int count, nsegs, rx_cons;
2487 1.1 jmcneill
2488 1.1 jmcneill status = le32toh(rrd->status);
2489 1.1 jmcneill rdinfo = le32toh(rrd->rdinfo);
2490 1.1 jmcneill rx_cons = RRD_RD_IDX(rdinfo);
2491 1.1 jmcneill nsegs = RRD_RD_CNT(rdinfo);
2492 1.1 jmcneill
2493 1.1 jmcneill sc->alc_cdata.alc_rxlen = RRD_BYTES(status);
2494 1.1 jmcneill if (status & (RRD_ERR_SUM | RRD_ERR_LENGTH)) {
2495 1.1 jmcneill /*
2496 1.1 jmcneill * We want to pass the following frames to upper
2497 1.1 jmcneill * layer regardless of error status of Rx return
2498 1.1 jmcneill * ring.
2499 1.1 jmcneill *
2500 1.1 jmcneill * o IP/TCP/UDP checksum is bad.
2501 1.1 jmcneill * o frame length and protocol specific length
2502 1.1 jmcneill * does not match.
2503 1.1 jmcneill *
2504 1.1 jmcneill * Force network stack compute checksum for
2505 1.1 jmcneill * errored frames.
2506 1.1 jmcneill */
2507 1.1 jmcneill status |= RRD_TCP_UDPCSUM_NOK | RRD_IPCSUM_NOK;
2508 1.2 jmcneill if ((status & (RRD_ERR_CRC | RRD_ERR_ALIGN |
2509 1.2 jmcneill RRD_ERR_TRUNC | RRD_ERR_RUNT)) != 0)
2510 1.1 jmcneill return;
2511 1.1 jmcneill }
2512 1.1 jmcneill
2513 1.1 jmcneill for (count = 0; count < nsegs; count++,
2514 1.1 jmcneill ALC_DESC_INC(rx_cons, ALC_RX_RING_CNT)) {
2515 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[rx_cons];
2516 1.1 jmcneill mp = rxd->rx_m;
2517 1.1 jmcneill /* Add a new receive buffer to the ring. */
2518 1.7 mrg if (alc_newbuf(sc, rxd, false) != 0) {
2519 1.1 jmcneill ifp->if_iqdrops++;
2520 1.1 jmcneill /* Reuse Rx buffers. */
2521 1.1 jmcneill if (sc->alc_cdata.alc_rxhead != NULL)
2522 1.1 jmcneill m_freem(sc->alc_cdata.alc_rxhead);
2523 1.1 jmcneill break;
2524 1.1 jmcneill }
2525 1.1 jmcneill
2526 1.1 jmcneill /*
2527 1.1 jmcneill * Assume we've received a full sized frame.
2528 1.1 jmcneill * Actual size is fixed when we encounter the end of
2529 1.1 jmcneill * multi-segmented frame.
2530 1.1 jmcneill */
2531 1.1 jmcneill mp->m_len = sc->alc_buf_size;
2532 1.1 jmcneill
2533 1.1 jmcneill /* Chain received mbufs. */
2534 1.1 jmcneill if (sc->alc_cdata.alc_rxhead == NULL) {
2535 1.1 jmcneill sc->alc_cdata.alc_rxhead = mp;
2536 1.1 jmcneill sc->alc_cdata.alc_rxtail = mp;
2537 1.1 jmcneill } else {
2538 1.26 maxv m_remove_pkthdr(mp);
2539 1.1 jmcneill sc->alc_cdata.alc_rxprev_tail =
2540 1.1 jmcneill sc->alc_cdata.alc_rxtail;
2541 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_next = mp;
2542 1.1 jmcneill sc->alc_cdata.alc_rxtail = mp;
2543 1.1 jmcneill }
2544 1.1 jmcneill
2545 1.1 jmcneill if (count == nsegs - 1) {
2546 1.1 jmcneill /* Last desc. for this frame. */
2547 1.1 jmcneill m = sc->alc_cdata.alc_rxhead;
2548 1.26 maxv KASSERT(m->m_flags & M_PKTHDR);
2549 1.1 jmcneill /*
2550 1.1 jmcneill * It seems that L1C/L2C controller has no way
2551 1.1 jmcneill * to tell hardware to strip CRC bytes.
2552 1.1 jmcneill */
2553 1.1 jmcneill m->m_pkthdr.len =
2554 1.1 jmcneill sc->alc_cdata.alc_rxlen - ETHER_CRC_LEN;
2555 1.1 jmcneill if (nsegs > 1) {
2556 1.1 jmcneill /* Set last mbuf size. */
2557 1.1 jmcneill mp->m_len = sc->alc_cdata.alc_rxlen -
2558 1.1 jmcneill (nsegs - 1) * sc->alc_buf_size;
2559 1.1 jmcneill /* Remove the CRC bytes in chained mbufs. */
2560 1.1 jmcneill if (mp->m_len <= ETHER_CRC_LEN) {
2561 1.1 jmcneill sc->alc_cdata.alc_rxtail =
2562 1.1 jmcneill sc->alc_cdata.alc_rxprev_tail;
2563 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_len -=
2564 1.1 jmcneill (ETHER_CRC_LEN - mp->m_len);
2565 1.1 jmcneill sc->alc_cdata.alc_rxtail->m_next = NULL;
2566 1.1 jmcneill m_freem(mp);
2567 1.1 jmcneill } else {
2568 1.1 jmcneill mp->m_len -= ETHER_CRC_LEN;
2569 1.1 jmcneill }
2570 1.1 jmcneill } else
2571 1.1 jmcneill m->m_len = m->m_pkthdr.len;
2572 1.21 ozaki m_set_rcvif(m, ifp);
2573 1.1 jmcneill #if NVLAN > 0
2574 1.1 jmcneill /*
2575 1.1 jmcneill * Due to hardware bugs, Rx checksum offloading
2576 1.1 jmcneill * was intentionally disabled.
2577 1.1 jmcneill */
2578 1.1 jmcneill if (status & RRD_VLAN_TAG) {
2579 1.1 jmcneill u_int32_t vtag = RRD_VLAN(le32toh(rrd->vtag));
2580 1.25 knakahar vlan_set_tag(m, ntohs(vtag));
2581 1.1 jmcneill }
2582 1.1 jmcneill #endif
2583 1.1 jmcneill
2584 1.1 jmcneill /* Pass it on. */
2585 1.19 ozaki if_percpuq_enqueue(ifp->if_percpuq, m);
2586 1.1 jmcneill }
2587 1.1 jmcneill }
2588 1.1 jmcneill /* Reset mbuf chains. */
2589 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
2590 1.1 jmcneill }
2591 1.1 jmcneill
2592 1.1 jmcneill static void
2593 1.1 jmcneill alc_tick(void *xsc)
2594 1.1 jmcneill {
2595 1.1 jmcneill struct alc_softc *sc = xsc;
2596 1.1 jmcneill struct mii_data *mii = &sc->sc_miibus;
2597 1.1 jmcneill int s;
2598 1.1 jmcneill
2599 1.1 jmcneill s = splnet();
2600 1.1 jmcneill mii_tick(mii);
2601 1.1 jmcneill alc_stats_update(sc);
2602 1.1 jmcneill splx(s);
2603 1.1 jmcneill
2604 1.1 jmcneill callout_schedule(&sc->sc_tick_ch, hz);
2605 1.1 jmcneill }
2606 1.1 jmcneill
2607 1.1 jmcneill static void
2608 1.12 christos alc_osc_reset(struct alc_softc *sc)
2609 1.12 christos {
2610 1.12 christos uint32_t reg;
2611 1.12 christos
2612 1.12 christos reg = CSR_READ_4(sc, ALC_MISC3);
2613 1.12 christos reg &= ~MISC3_25M_BY_SW;
2614 1.12 christos reg |= MISC3_25M_NOTO_INTNL;
2615 1.12 christos CSR_WRITE_4(sc, ALC_MISC3, reg);
2616 1.12 christos
2617 1.12 christos reg = CSR_READ_4(sc, ALC_MISC);
2618 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0) {
2619 1.12 christos /*
2620 1.12 christos * Restore over-current protection default value.
2621 1.12 christos * This value could be reset by MAC reset.
2622 1.12 christos */
2623 1.12 christos reg &= ~MISC_PSW_OCP_MASK;
2624 1.12 christos reg |= (MISC_PSW_OCP_DEFAULT << MISC_PSW_OCP_SHIFT);
2625 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2626 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2627 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
2628 1.12 christos reg = CSR_READ_4(sc, ALC_MISC2);
2629 1.12 christos reg &= ~MISC2_CALB_START;
2630 1.12 christos CSR_WRITE_4(sc, ALC_MISC2, reg);
2631 1.12 christos CSR_WRITE_4(sc, ALC_MISC2, reg | MISC2_CALB_START);
2632 1.12 christos
2633 1.12 christos } else {
2634 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2635 1.12 christos /* Disable isolate for revision A devices. */
2636 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
2637 1.12 christos reg &= ~MISC_ISO_ENB;
2638 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg | MISC_INTNLOSC_OPEN);
2639 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2640 1.12 christos }
2641 1.12 christos
2642 1.12 christos DELAY(20);
2643 1.12 christos }
2644 1.12 christos
2645 1.12 christos static void
2646 1.1 jmcneill alc_reset(struct alc_softc *sc)
2647 1.1 jmcneill {
2648 1.12 christos uint32_t pmcfg, reg;
2649 1.1 jmcneill int i;
2650 1.1 jmcneill
2651 1.12 christos pmcfg = 0;
2652 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2653 1.12 christos /* Reset workaround. */
2654 1.12 christos CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, 1);
2655 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
2656 1.12 christos (sc->alc_rev & 0x01) != 0) {
2657 1.12 christos /* Disable L0s/L1s before reset. */
2658 1.12 christos pmcfg = CSR_READ_4(sc, ALC_PM_CFG);
2659 1.12 christos if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
2660 1.12 christos != 0) {
2661 1.12 christos pmcfg &= ~(PM_CFG_ASPM_L0S_ENB |
2662 1.12 christos PM_CFG_ASPM_L1_ENB);
2663 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
2664 1.12 christos }
2665 1.12 christos }
2666 1.12 christos }
2667 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2668 1.2 jmcneill reg |= MASTER_OOB_DIS_OFF | MASTER_RESET;
2669 1.2 jmcneill CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2670 1.12 christos
2671 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2672 1.12 christos for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2673 1.12 christos DELAY(10);
2674 1.12 christos if (CSR_READ_4(sc, ALC_MBOX_RD0_PROD_IDX) == 0)
2675 1.12 christos break;
2676 1.12 christos }
2677 1.12 christos if (i == 0)
2678 1.12 christos printf("%s: MAC reset timeout!\n", device_xname(sc->sc_dev));
2679 1.12 christos }
2680 1.1 jmcneill for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2681 1.1 jmcneill DELAY(10);
2682 1.1 jmcneill if ((CSR_READ_4(sc, ALC_MASTER_CFG) & MASTER_RESET) == 0)
2683 1.1 jmcneill break;
2684 1.1 jmcneill }
2685 1.1 jmcneill if (i == 0)
2686 1.1 jmcneill printf("%s: master reset timeout!\n", device_xname(sc->sc_dev));
2687 1.1 jmcneill
2688 1.1 jmcneill for (i = ALC_RESET_TIMEOUT; i > 0; i--) {
2689 1.12 christos reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
2690 1.12 christos if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC |
2691 1.12 christos IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
2692 1.1 jmcneill break;
2693 1.1 jmcneill DELAY(10);
2694 1.1 jmcneill }
2695 1.12 christos if (i == 0)
2696 1.12 christos printf("%s: reset timeout(0x%08x)!\n",
2697 1.12 christos device_xname(sc->sc_dev), reg);
2698 1.1 jmcneill
2699 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2700 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1 &&
2701 1.12 christos (sc->alc_rev & 0x01) != 0) {
2702 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2703 1.12 christos reg |= MASTER_CLK_SEL_DIS;
2704 1.12 christos CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2705 1.12 christos /* Restore L0s/L1s config. */
2706 1.12 christos if ((pmcfg & (PM_CFG_ASPM_L0S_ENB | PM_CFG_ASPM_L1_ENB))
2707 1.12 christos != 0)
2708 1.12 christos CSR_WRITE_4(sc, ALC_PM_CFG, pmcfg);
2709 1.12 christos }
2710 1.12 christos
2711 1.12 christos alc_osc_reset(sc);
2712 1.12 christos reg = CSR_READ_4(sc, ALC_MISC3);
2713 1.12 christos reg &= ~MISC3_25M_BY_SW;
2714 1.12 christos reg |= MISC3_25M_NOTO_INTNL;
2715 1.12 christos CSR_WRITE_4(sc, ALC_MISC3, reg);
2716 1.12 christos reg = CSR_READ_4(sc, ALC_MISC);
2717 1.12 christos reg &= ~MISC_INTNLOSC_OPEN;
2718 1.12 christos if (AR816X_REV(sc->alc_rev) <= AR816X_REV_A1)
2719 1.12 christos reg &= ~MISC_ISO_ENB;
2720 1.12 christos CSR_WRITE_4(sc, ALC_MISC, reg);
2721 1.12 christos DELAY(20);
2722 1.12 christos }
2723 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
2724 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
2725 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2)
2726 1.12 christos CSR_WRITE_4(sc, ALC_SERDES_LOCK,
2727 1.12 christos CSR_READ_4(sc, ALC_SERDES_LOCK) | SERDES_MAC_CLK_SLOWDOWN |
2728 1.12 christos SERDES_PHY_CLK_SLOWDOWN);
2729 1.1 jmcneill }
2730 1.1 jmcneill
2731 1.1 jmcneill static int
2732 1.1 jmcneill alc_init(struct ifnet *ifp)
2733 1.1 jmcneill {
2734 1.8 christos
2735 1.7 mrg return alc_init_backend(ifp, true);
2736 1.7 mrg }
2737 1.7 mrg
2738 1.7 mrg static int
2739 1.7 mrg alc_init_backend(struct ifnet *ifp, bool init)
2740 1.7 mrg {
2741 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
2742 1.1 jmcneill struct mii_data *mii;
2743 1.1 jmcneill uint8_t eaddr[ETHER_ADDR_LEN];
2744 1.1 jmcneill bus_addr_t paddr;
2745 1.1 jmcneill uint32_t reg, rxf_hi, rxf_lo;
2746 1.1 jmcneill int error;
2747 1.1 jmcneill
2748 1.1 jmcneill /*
2749 1.1 jmcneill * Cancel any pending I/O.
2750 1.1 jmcneill */
2751 1.1 jmcneill alc_stop(ifp, 0);
2752 1.1 jmcneill /*
2753 1.1 jmcneill * Reset the chip to a known state.
2754 1.1 jmcneill */
2755 1.1 jmcneill alc_reset(sc);
2756 1.1 jmcneill
2757 1.1 jmcneill /* Initialize Rx descriptors. */
2758 1.7 mrg error = alc_init_rx_ring(sc, init);
2759 1.1 jmcneill if (error != 0) {
2760 1.1 jmcneill printf("%s: no memory for Rx buffers.\n", device_xname(sc->sc_dev));
2761 1.1 jmcneill alc_stop(ifp, 0);
2762 1.1 jmcneill return (error);
2763 1.1 jmcneill }
2764 1.1 jmcneill alc_init_rr_ring(sc);
2765 1.1 jmcneill alc_init_tx_ring(sc);
2766 1.1 jmcneill alc_init_cmb(sc);
2767 1.1 jmcneill alc_init_smb(sc);
2768 1.1 jmcneill
2769 1.2 jmcneill /* Enable all clocks. */
2770 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2771 1.12 christos CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, CLK_GATING_DMAW_ENB |
2772 1.12 christos CLK_GATING_DMAR_ENB | CLK_GATING_TXQ_ENB |
2773 1.12 christos CLK_GATING_RXQ_ENB | CLK_GATING_TXMAC_ENB |
2774 1.12 christos CLK_GATING_RXMAC_ENB);
2775 1.12 christos if (AR816X_REV(sc->alc_rev) >= AR816X_REV_B0)
2776 1.12 christos CSR_WRITE_4(sc, ALC_IDLE_DECISN_TIMER,
2777 1.12 christos IDLE_DECISN_TIMER_DEFAULT_1MS);
2778 1.12 christos } else
2779 1.12 christos CSR_WRITE_4(sc, ALC_CLK_GATING_CFG, 0);
2780 1.12 christos
2781 1.2 jmcneill
2782 1.1 jmcneill /* Reprogram the station address. */
2783 1.1 jmcneill memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
2784 1.1 jmcneill CSR_WRITE_4(sc, ALC_PAR0,
2785 1.1 jmcneill eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
2786 1.1 jmcneill CSR_WRITE_4(sc, ALC_PAR1, eaddr[0] << 8 | eaddr[1]);
2787 1.1 jmcneill /*
2788 1.1 jmcneill * Clear WOL status and disable all WOL feature as WOL
2789 1.1 jmcneill * would interfere Rx operation under normal environments.
2790 1.1 jmcneill */
2791 1.1 jmcneill CSR_READ_4(sc, ALC_WOL_CFG);
2792 1.1 jmcneill CSR_WRITE_4(sc, ALC_WOL_CFG, 0);
2793 1.1 jmcneill /* Set Tx descriptor base addresses. */
2794 1.1 jmcneill paddr = sc->alc_rdata.alc_tx_ring_paddr;
2795 1.1 jmcneill CSR_WRITE_4(sc, ALC_TX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2796 1.1 jmcneill CSR_WRITE_4(sc, ALC_TDL_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2797 1.1 jmcneill /* We don't use high priority ring. */
2798 1.1 jmcneill CSR_WRITE_4(sc, ALC_TDH_HEAD_ADDR_LO, 0);
2799 1.1 jmcneill /* Set Tx descriptor counter. */
2800 1.1 jmcneill CSR_WRITE_4(sc, ALC_TD_RING_CNT,
2801 1.1 jmcneill (ALC_TX_RING_CNT << TD_RING_CNT_SHIFT) & TD_RING_CNT_MASK);
2802 1.1 jmcneill /* Set Rx descriptor base addresses. */
2803 1.1 jmcneill paddr = sc->alc_rdata.alc_rx_ring_paddr;
2804 1.1 jmcneill CSR_WRITE_4(sc, ALC_RX_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2805 1.1 jmcneill CSR_WRITE_4(sc, ALC_RD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2806 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2807 1.12 christos /* We use one Rx ring. */
2808 1.12 christos CSR_WRITE_4(sc, ALC_RD1_HEAD_ADDR_LO, 0);
2809 1.12 christos CSR_WRITE_4(sc, ALC_RD2_HEAD_ADDR_LO, 0);
2810 1.12 christos CSR_WRITE_4(sc, ALC_RD3_HEAD_ADDR_LO, 0);
2811 1.12 christos }
2812 1.1 jmcneill /* Set Rx descriptor counter. */
2813 1.1 jmcneill CSR_WRITE_4(sc, ALC_RD_RING_CNT,
2814 1.1 jmcneill (ALC_RX_RING_CNT << RD_RING_CNT_SHIFT) & RD_RING_CNT_MASK);
2815 1.1 jmcneill
2816 1.1 jmcneill /*
2817 1.1 jmcneill * Let hardware split jumbo frames into alc_max_buf_sized chunks.
2818 1.1 jmcneill * if it do not fit the buffer size. Rx return descriptor holds
2819 1.1 jmcneill * a counter that indicates how many fragments were made by the
2820 1.1 jmcneill * hardware. The buffer size should be multiple of 8 bytes.
2821 1.1 jmcneill * Since hardware has limit on the size of buffer size, always
2822 1.1 jmcneill * use the maximum value.
2823 1.1 jmcneill * For strict-alignment architectures make sure to reduce buffer
2824 1.1 jmcneill * size by 8 bytes to make room for alignment fixup.
2825 1.1 jmcneill */
2826 1.1 jmcneill sc->alc_buf_size = RX_BUF_SIZE_MAX;
2827 1.1 jmcneill CSR_WRITE_4(sc, ALC_RX_BUF_SIZE, sc->alc_buf_size);
2828 1.1 jmcneill
2829 1.1 jmcneill paddr = sc->alc_rdata.alc_rr_ring_paddr;
2830 1.1 jmcneill /* Set Rx return descriptor base addresses. */
2831 1.1 jmcneill CSR_WRITE_4(sc, ALC_RRD0_HEAD_ADDR_LO, ALC_ADDR_LO(paddr));
2832 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2833 1.12 christos /* We use one Rx return ring. */
2834 1.12 christos CSR_WRITE_4(sc, ALC_RRD1_HEAD_ADDR_LO, 0);
2835 1.12 christos CSR_WRITE_4(sc, ALC_RRD2_HEAD_ADDR_LO, 0);
2836 1.12 christos CSR_WRITE_4(sc, ALC_RRD3_HEAD_ADDR_LO, 0);
2837 1.12 christos }\
2838 1.1 jmcneill /* Set Rx return descriptor counter. */
2839 1.1 jmcneill CSR_WRITE_4(sc, ALC_RRD_RING_CNT,
2840 1.1 jmcneill (ALC_RR_RING_CNT << RRD_RING_CNT_SHIFT) & RRD_RING_CNT_MASK);
2841 1.1 jmcneill paddr = sc->alc_rdata.alc_cmb_paddr;
2842 1.1 jmcneill CSR_WRITE_4(sc, ALC_CMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
2843 1.1 jmcneill paddr = sc->alc_rdata.alc_smb_paddr;
2844 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_HI, ALC_ADDR_HI(paddr));
2845 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_BASE_ADDR_LO, ALC_ADDR_LO(paddr));
2846 1.1 jmcneill
2847 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B) {
2848 1.2 jmcneill /* Reconfigure SRAM - Vendor magic. */
2849 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_LEN, 0x000002A0);
2850 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_LEN, 0x00000100);
2851 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RX_FIFO_ADDR, 0x029F0000);
2852 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_RD0_ADDR, 0x02BF02A0);
2853 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TX_FIFO_ADDR, 0x03BF02C0);
2854 1.2 jmcneill CSR_WRITE_4(sc, ALC_SRAM_TD_ADDR, 0x03DF03C0);
2855 1.2 jmcneill CSR_WRITE_4(sc, ALC_TXF_WATER_MARK, 0x00000000);
2856 1.2 jmcneill CSR_WRITE_4(sc, ALC_RD_DMA_CFG, 0x00000000);
2857 1.2 jmcneill }
2858 1.2 jmcneill
2859 1.1 jmcneill /* Tell hardware that we're ready to load DMA blocks. */
2860 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_BLOCK, DMA_BLOCK_LOAD);
2861 1.1 jmcneill
2862 1.1 jmcneill /* Configure interrupt moderation timer. */
2863 1.1 jmcneill sc->alc_int_rx_mod = ALC_IM_RX_TIMER_DEFAULT;
2864 1.1 jmcneill sc->alc_int_tx_mod = ALC_IM_TX_TIMER_DEFAULT;
2865 1.1 jmcneill reg = ALC_USECS(sc->alc_int_rx_mod) << IM_TIMER_RX_SHIFT;
2866 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0)
2867 1.12 christos reg |= ALC_USECS(sc->alc_int_tx_mod) << IM_TIMER_TX_SHIFT;
2868 1.1 jmcneill CSR_WRITE_4(sc, ALC_IM_TIMER, reg);
2869 1.1 jmcneill /*
2870 1.1 jmcneill * We don't want to automatic interrupt clear as task queue
2871 1.1 jmcneill * for the interrupt should know interrupt status.
2872 1.1 jmcneill */
2873 1.12 christos reg = CSR_READ_4(sc, ALC_MASTER_CFG);
2874 1.12 christos reg &= ~(MASTER_IM_RX_TIMER_ENB | MASTER_IM_TX_TIMER_ENB);
2875 1.12 christos reg |= MASTER_SA_TIMER_ENB;
2876 1.1 jmcneill if (ALC_USECS(sc->alc_int_rx_mod) != 0)
2877 1.1 jmcneill reg |= MASTER_IM_RX_TIMER_ENB;
2878 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0 &&
2879 1.12 christos ALC_USECS(sc->alc_int_tx_mod) != 0)
2880 1.1 jmcneill reg |= MASTER_IM_TX_TIMER_ENB;
2881 1.1 jmcneill CSR_WRITE_4(sc, ALC_MASTER_CFG, reg);
2882 1.1 jmcneill /*
2883 1.1 jmcneill * Disable interrupt re-trigger timer. We don't want automatic
2884 1.1 jmcneill * re-triggering of un-ACKed interrupts.
2885 1.1 jmcneill */
2886 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_RETRIG_TIMER, ALC_USECS(0));
2887 1.1 jmcneill /* Configure CMB. */
2888 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2889 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, ALC_TX_RING_CNT / 3);
2890 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER,
2891 1.12 christos ALC_USECS(sc->alc_int_tx_mod));
2892 1.12 christos } else {
2893 1.12 christos if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0) {
2894 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TD_THRESH, 4);
2895 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(5000));
2896 1.12 christos } else
2897 1.12 christos CSR_WRITE_4(sc, ALC_CMB_TX_TIMER, ALC_USECS(0));
2898 1.12 christos }
2899 1.1 jmcneill /*
2900 1.1 jmcneill * Hardware can be configured to issue SMB interrupt based
2901 1.1 jmcneill * on programmed interval. Since there is a callout that is
2902 1.1 jmcneill * invoked for every hz in driver we use that instead of
2903 1.1 jmcneill * relying on periodic SMB interrupt.
2904 1.1 jmcneill */
2905 1.1 jmcneill CSR_WRITE_4(sc, ALC_SMB_STAT_TIMER, ALC_USECS(0));
2906 1.1 jmcneill /* Clear MAC statistics. */
2907 1.1 jmcneill alc_stats_clear(sc);
2908 1.1 jmcneill
2909 1.1 jmcneill /*
2910 1.1 jmcneill * Always use maximum frame size that controller can support.
2911 1.1 jmcneill * Otherwise received frames that has larger frame length
2912 1.1 jmcneill * than alc(4) MTU would be silently dropped in hardware. This
2913 1.1 jmcneill * would make path-MTU discovery hard as sender wouldn't get
2914 1.1 jmcneill * any responses from receiver. alc(4) supports
2915 1.1 jmcneill * multi-fragmented frames on Rx path so it has no issue on
2916 1.1 jmcneill * assembling fragmented frames. Using maximum frame size also
2917 1.1 jmcneill * removes the need to reinitialize hardware when interface
2918 1.1 jmcneill * MTU configuration was changed.
2919 1.1 jmcneill *
2920 1.1 jmcneill * Be conservative in what you do, be liberal in what you
2921 1.1 jmcneill * accept from others - RFC 793.
2922 1.1 jmcneill */
2923 1.2 jmcneill CSR_WRITE_4(sc, ALC_FRAME_SIZE, sc->alc_ident->max_framelen);
2924 1.1 jmcneill
2925 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
2926 1.12 christos /* Disable header split(?) */
2927 1.12 christos CSR_WRITE_4(sc, ALC_HDS_CFG, 0);
2928 1.12 christos
2929 1.12 christos /* Configure IPG/IFG parameters. */
2930 1.12 christos CSR_WRITE_4(sc, ALC_IPG_IFG_CFG,
2931 1.12 christos ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) &
2932 1.12 christos IPG_IFG_IPGT_MASK) |
2933 1.12 christos ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) &
2934 1.12 christos IPG_IFG_MIFG_MASK) |
2935 1.12 christos ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) &
2936 1.12 christos IPG_IFG_IPG1_MASK) |
2937 1.12 christos ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) &
2938 1.12 christos IPG_IFG_IPG2_MASK));
2939 1.12 christos /* Set parameters for half-duplex media. */
2940 1.12 christos CSR_WRITE_4(sc, ALC_HDPX_CFG,
2941 1.12 christos ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
2942 1.12 christos HDPX_CFG_LCOL_MASK) |
2943 1.12 christos ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
2944 1.12 christos HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
2945 1.12 christos ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
2946 1.12 christos HDPX_CFG_ABEBT_MASK) |
2947 1.12 christos ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
2948 1.12 christos HDPX_CFG_JAMIPG_MASK));
2949 1.12 christos }
2950 1.1 jmcneill
2951 1.1 jmcneill /*
2952 1.1 jmcneill * Set TSO/checksum offload threshold. For frames that is
2953 1.1 jmcneill * larger than this threshold, hardware wouldn't do
2954 1.1 jmcneill * TSO/checksum offloading.
2955 1.1 jmcneill */
2956 1.12 christos reg = (sc->alc_ident->max_framelen >> TSO_OFFLOAD_THRESH_UNIT_SHIFT) &
2957 1.12 christos TSO_OFFLOAD_THRESH_MASK;
2958 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
2959 1.12 christos reg |= TSO_OFFLOAD_ERRLGPKT_DROP_ENB;
2960 1.12 christos CSR_WRITE_4(sc, ALC_TSO_OFFLOAD_THRESH, reg);
2961 1.1 jmcneill /* Configure TxQ. */
2962 1.1 jmcneill reg = (alc_dma_burst[sc->alc_dma_rd_burst] <<
2963 1.1 jmcneill TXQ_CFG_TX_FIFO_BURST_SHIFT) & TXQ_CFG_TX_FIFO_BURST_MASK;
2964 1.2 jmcneill if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B ||
2965 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
2966 1.2 jmcneill reg >>= 1;
2967 1.1 jmcneill reg |= (TXQ_CFG_TD_BURST_DEFAULT << TXQ_CFG_TD_BURST_SHIFT) &
2968 1.1 jmcneill TXQ_CFG_TD_BURST_MASK;
2969 1.12 christos reg |= TXQ_CFG_IP_OPTION_ENB | TXQ_CFG_8023_ENB;
2970 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, reg | TXQ_CFG_ENHANCED_MODE);
2971 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2972 1.12 christos reg = (TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q1_BURST_SHIFT |
2973 1.12 christos TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q2_BURST_SHIFT |
2974 1.12 christos TXQ_CFG_TD_BURST_DEFAULT << HQTD_CFG_Q3_BURST_SHIFT |
2975 1.12 christos HQTD_CFG_BURST_ENB);
2976 1.12 christos CSR_WRITE_4(sc, ALC_HQTD_CFG, reg);
2977 1.12 christos reg = WRR_PRI_RESTRICT_NONE;
2978 1.12 christos reg |= (WRR_PRI_DEFAULT << WRR_PRI0_SHIFT |
2979 1.12 christos WRR_PRI_DEFAULT << WRR_PRI1_SHIFT |
2980 1.12 christos WRR_PRI_DEFAULT << WRR_PRI2_SHIFT |
2981 1.12 christos WRR_PRI_DEFAULT << WRR_PRI3_SHIFT);
2982 1.12 christos CSR_WRITE_4(sc, ALC_WRR, reg);
2983 1.12 christos } else {
2984 1.12 christos /* Configure Rx free descriptor pre-fetching. */
2985 1.12 christos CSR_WRITE_4(sc, ALC_RX_RD_FREE_THRESH,
2986 1.12 christos ((RX_RD_FREE_THRESH_HI_DEFAULT <<
2987 1.12 christos RX_RD_FREE_THRESH_HI_SHIFT) & RX_RD_FREE_THRESH_HI_MASK) |
2988 1.12 christos ((RX_RD_FREE_THRESH_LO_DEFAULT <<
2989 1.12 christos RX_RD_FREE_THRESH_LO_SHIFT) & RX_RD_FREE_THRESH_LO_MASK));
2990 1.12 christos }
2991 1.1 jmcneill
2992 1.1 jmcneill /*
2993 1.1 jmcneill * Configure flow control parameters.
2994 1.1 jmcneill * XON : 80% of Rx FIFO
2995 1.1 jmcneill * XOFF : 30% of Rx FIFO
2996 1.1 jmcneill */
2997 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
2998 1.12 christos reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
2999 1.12 christos reg &= SRAM_RX_FIFO_LEN_MASK;
3000 1.12 christos reg *= 8;
3001 1.12 christos if (reg > 8 * 1024)
3002 1.12 christos reg -= RX_FIFO_PAUSE_816X_RSVD;
3003 1.12 christos else
3004 1.12 christos reg -= RX_BUF_SIZE_MAX;
3005 1.12 christos reg /= 8;
3006 1.12 christos CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3007 1.12 christos ((reg << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3008 1.12 christos RX_FIFO_PAUSE_THRESH_LO_MASK) |
3009 1.12 christos (((RX_FIFO_PAUSE_816X_RSVD / 8) <<
3010 1.12 christos RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3011 1.12 christos RX_FIFO_PAUSE_THRESH_HI_MASK));
3012 1.12 christos } else if (sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8131 ||
3013 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8132) {
3014 1.2 jmcneill reg = CSR_READ_4(sc, ALC_SRAM_RX_FIFO_LEN);
3015 1.2 jmcneill rxf_hi = (reg * 8) / 10;
3016 1.2 jmcneill rxf_lo = (reg * 3) / 10;
3017 1.2 jmcneill CSR_WRITE_4(sc, ALC_RX_FIFO_PAUSE_THRESH,
3018 1.2 jmcneill ((rxf_lo << RX_FIFO_PAUSE_THRESH_LO_SHIFT) &
3019 1.2 jmcneill RX_FIFO_PAUSE_THRESH_LO_MASK) |
3020 1.2 jmcneill ((rxf_hi << RX_FIFO_PAUSE_THRESH_HI_SHIFT) &
3021 1.2 jmcneill RX_FIFO_PAUSE_THRESH_HI_MASK));
3022 1.2 jmcneill }
3023 1.2 jmcneill
3024 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3025 1.12 christos /* Disable RSS until I understand L1C/L2C's RSS logic. */
3026 1.12 christos CSR_WRITE_4(sc, ALC_RSS_IDT_TABLE0, 0);
3027 1.12 christos CSR_WRITE_4(sc, ALC_RSS_CPU, 0);
3028 1.12 christos }
3029 1.1 jmcneill
3030 1.1 jmcneill /* Configure RxQ. */
3031 1.1 jmcneill reg = (RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
3032 1.1 jmcneill RXQ_CFG_RD_BURST_MASK;
3033 1.1 jmcneill reg |= RXQ_CFG_RSS_MODE_DIS;
3034 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0)
3035 1.12 christos reg |= (RXQ_CFG_816X_IDT_TBL_SIZE_DEFAULT <<
3036 1.12 christos RXQ_CFG_816X_IDT_TBL_SIZE_SHIFT) &
3037 1.12 christos RXQ_CFG_816X_IDT_TBL_SIZE_MASK;
3038 1.12 christos if ((sc->alc_flags & ALC_FLAG_FASTETHER) == 0 &&
3039 1.12 christos sc->alc_ident->deviceid != PCI_PRODUCT_ATTANSIC_AR8151_V2)
3040 1.12 christos reg |= RXQ_CFG_ASPM_THROUGHPUT_LIMIT_1M;
3041 1.1 jmcneill CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3042 1.1 jmcneill
3043 1.1 jmcneill /* Configure DMA parameters. */
3044 1.1 jmcneill reg = DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI;
3045 1.1 jmcneill reg |= sc->alc_rcb;
3046 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_CMB_BUG) == 0)
3047 1.1 jmcneill reg |= DMA_CFG_CMB_ENB;
3048 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_SMB_BUG) == 0)
3049 1.1 jmcneill reg |= DMA_CFG_SMB_ENB;
3050 1.1 jmcneill else
3051 1.1 jmcneill reg |= DMA_CFG_SMB_DIS;
3052 1.1 jmcneill reg |= (sc->alc_dma_rd_burst & DMA_CFG_RD_BURST_MASK) <<
3053 1.1 jmcneill DMA_CFG_RD_BURST_SHIFT;
3054 1.1 jmcneill reg |= (sc->alc_dma_wr_burst & DMA_CFG_WR_BURST_MASK) <<
3055 1.1 jmcneill DMA_CFG_WR_BURST_SHIFT;
3056 1.1 jmcneill reg |= (DMA_CFG_RD_DELAY_CNT_DEFAULT << DMA_CFG_RD_DELAY_CNT_SHIFT) &
3057 1.1 jmcneill DMA_CFG_RD_DELAY_CNT_MASK;
3058 1.1 jmcneill reg |= (DMA_CFG_WR_DELAY_CNT_DEFAULT << DMA_CFG_WR_DELAY_CNT_SHIFT) &
3059 1.1 jmcneill DMA_CFG_WR_DELAY_CNT_MASK;
3060 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0) {
3061 1.12 christos switch (AR816X_REV(sc->alc_rev)) {
3062 1.12 christos case AR816X_REV_A0:
3063 1.12 christos case AR816X_REV_A1:
3064 1.12 christos reg |= DMA_CFG_RD_CHNL_SEL_1;
3065 1.12 christos break;
3066 1.12 christos case AR816X_REV_B0:
3067 1.12 christos /* FALLTHROUGH */
3068 1.12 christos default:
3069 1.12 christos reg |= DMA_CFG_RD_CHNL_SEL_3;
3070 1.12 christos break;
3071 1.12 christos }
3072 1.12 christos }
3073 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3074 1.1 jmcneill
3075 1.1 jmcneill /*
3076 1.1 jmcneill * Configure Tx/Rx MACs.
3077 1.1 jmcneill * - Auto-padding for short frames.
3078 1.1 jmcneill * - Enable CRC generation.
3079 1.1 jmcneill * Actual reconfiguration of MAC for resolved speed/duplex
3080 1.1 jmcneill * is followed after detection of link establishment.
3081 1.2 jmcneill * AR813x/AR815x always does checksum computation regardless
3082 1.1 jmcneill * of MAC_CFG_RXCSUM_ENB bit. Also the controller is known to
3083 1.1 jmcneill * have bug in protocol field in Rx return structure so
3084 1.1 jmcneill * these controllers can't handle fragmented frames. Disable
3085 1.1 jmcneill * Rx checksum offloading until there is a newer controller
3086 1.1 jmcneill * that has sane implementation.
3087 1.1 jmcneill */
3088 1.1 jmcneill reg = MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD | MAC_CFG_FULL_DUPLEX |
3089 1.1 jmcneill ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
3090 1.1 jmcneill MAC_CFG_PREAMBLE_MASK);
3091 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) != 0 ||
3092 1.12 christos sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151 ||
3093 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8151_V2 ||
3094 1.2 jmcneill sc->alc_ident->deviceid == PCI_PRODUCT_ATTANSIC_AR8152_B2)
3095 1.2 jmcneill reg |= MAC_CFG_HASH_ALG_CRC32 | MAC_CFG_SPEED_MODE_SW;
3096 1.1 jmcneill if ((sc->alc_flags & ALC_FLAG_FASTETHER) != 0)
3097 1.1 jmcneill reg |= MAC_CFG_SPEED_10_100;
3098 1.1 jmcneill else
3099 1.1 jmcneill reg |= MAC_CFG_SPEED_1000;
3100 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3101 1.1 jmcneill
3102 1.1 jmcneill /* Set up the receive filter. */
3103 1.1 jmcneill alc_iff(sc);
3104 1.1 jmcneill alc_rxvlan(sc);
3105 1.1 jmcneill
3106 1.1 jmcneill /* Acknowledge all pending interrupts and clear it. */
3107 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_MASK, ALC_INTRS);
3108 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3109 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0);
3110 1.1 jmcneill
3111 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
3112 1.1 jmcneill /* Switch to the current media. */
3113 1.1 jmcneill mii = &sc->sc_miibus;
3114 1.1 jmcneill mii_mediachg(mii);
3115 1.1 jmcneill
3116 1.1 jmcneill callout_schedule(&sc->sc_tick_ch, hz);
3117 1.1 jmcneill
3118 1.1 jmcneill ifp->if_flags |= IFF_RUNNING;
3119 1.1 jmcneill ifp->if_flags &= ~IFF_OACTIVE;
3120 1.1 jmcneill
3121 1.1 jmcneill return (0);
3122 1.1 jmcneill }
3123 1.1 jmcneill
3124 1.1 jmcneill static void
3125 1.1 jmcneill alc_stop(struct ifnet *ifp, int disable)
3126 1.1 jmcneill {
3127 1.1 jmcneill struct alc_softc *sc = ifp->if_softc;
3128 1.1 jmcneill struct alc_txdesc *txd;
3129 1.1 jmcneill struct alc_rxdesc *rxd;
3130 1.1 jmcneill uint32_t reg;
3131 1.1 jmcneill int i;
3132 1.1 jmcneill
3133 1.1 jmcneill callout_stop(&sc->sc_tick_ch);
3134 1.1 jmcneill
3135 1.1 jmcneill /*
3136 1.1 jmcneill * Mark the interface down and cancel the watchdog timer.
3137 1.1 jmcneill */
3138 1.1 jmcneill ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3139 1.1 jmcneill ifp->if_timer = 0;
3140 1.1 jmcneill
3141 1.1 jmcneill sc->alc_flags &= ~ALC_FLAG_LINK;
3142 1.1 jmcneill
3143 1.1 jmcneill alc_stats_update(sc);
3144 1.1 jmcneill
3145 1.1 jmcneill mii_down(&sc->sc_miibus);
3146 1.1 jmcneill
3147 1.1 jmcneill /* Disable interrupts. */
3148 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_MASK, 0);
3149 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3150 1.1 jmcneill
3151 1.1 jmcneill /* Disable DMA. */
3152 1.1 jmcneill reg = CSR_READ_4(sc, ALC_DMA_CFG);
3153 1.1 jmcneill reg &= ~(DMA_CFG_CMB_ENB | DMA_CFG_SMB_ENB);
3154 1.1 jmcneill reg |= DMA_CFG_SMB_DIS;
3155 1.1 jmcneill CSR_WRITE_4(sc, ALC_DMA_CFG, reg);
3156 1.1 jmcneill DELAY(1000);
3157 1.1 jmcneill
3158 1.1 jmcneill /* Stop Rx/Tx MACs. */
3159 1.1 jmcneill alc_stop_mac(sc);
3160 1.1 jmcneill
3161 1.1 jmcneill /* Disable interrupts which might be touched in taskq handler. */
3162 1.1 jmcneill CSR_WRITE_4(sc, ALC_INTR_STATUS, 0xFFFFFFFF);
3163 1.1 jmcneill
3164 1.12 christos /* Disable L0s/L1s */
3165 1.12 christos alc_aspm(sc, 0, IFM_UNKNOWN);
3166 1.12 christos
3167 1.1 jmcneill /* Reclaim Rx buffers that have been processed. */
3168 1.1 jmcneill if (sc->alc_cdata.alc_rxhead != NULL)
3169 1.1 jmcneill m_freem(sc->alc_cdata.alc_rxhead);
3170 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
3171 1.1 jmcneill /*
3172 1.1 jmcneill * Free Tx/Rx mbufs still in the queues.
3173 1.1 jmcneill */
3174 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
3175 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
3176 1.1 jmcneill if (rxd->rx_m != NULL) {
3177 1.15 leot bus_dmamap_sync(sc->sc_dmat, rxd->rx_dmamap, 0,
3178 1.15 leot rxd->rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
3179 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, rxd->rx_dmamap);
3180 1.1 jmcneill m_freem(rxd->rx_m);
3181 1.1 jmcneill rxd->rx_m = NULL;
3182 1.1 jmcneill }
3183 1.1 jmcneill }
3184 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
3185 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
3186 1.1 jmcneill if (txd->tx_m != NULL) {
3187 1.15 leot bus_dmamap_sync(sc->sc_dmat, txd->tx_dmamap, 0,
3188 1.15 leot txd->tx_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
3189 1.1 jmcneill bus_dmamap_unload(sc->sc_dmat, txd->tx_dmamap);
3190 1.1 jmcneill m_freem(txd->tx_m);
3191 1.1 jmcneill txd->tx_m = NULL;
3192 1.1 jmcneill }
3193 1.1 jmcneill }
3194 1.1 jmcneill }
3195 1.1 jmcneill
3196 1.1 jmcneill static void
3197 1.1 jmcneill alc_stop_mac(struct alc_softc *sc)
3198 1.1 jmcneill {
3199 1.1 jmcneill uint32_t reg;
3200 1.1 jmcneill int i;
3201 1.1 jmcneill
3202 1.12 christos alc_stop_queue(sc);
3203 1.1 jmcneill /* Disable Rx/Tx MAC. */
3204 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
3205 1.1 jmcneill if ((reg & (MAC_CFG_TX_ENB | MAC_CFG_RX_ENB)) != 0) {
3206 1.2 jmcneill reg &= ~(MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
3207 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3208 1.1 jmcneill }
3209 1.1 jmcneill for (i = ALC_TIMEOUT; i > 0; i--) {
3210 1.1 jmcneill reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3211 1.12 christos if ((reg & (IDLE_STATUS_RXMAC | IDLE_STATUS_TXMAC)) == 0)
3212 1.1 jmcneill break;
3213 1.1 jmcneill DELAY(10);
3214 1.1 jmcneill }
3215 1.1 jmcneill if (i == 0)
3216 1.1 jmcneill printf("%s: could not disable Rx/Tx MAC(0x%08x)!\n",
3217 1.1 jmcneill device_xname(sc->sc_dev), reg);
3218 1.1 jmcneill }
3219 1.1 jmcneill
3220 1.1 jmcneill static void
3221 1.1 jmcneill alc_start_queue(struct alc_softc *sc)
3222 1.1 jmcneill {
3223 1.1 jmcneill uint32_t qcfg[] = {
3224 1.1 jmcneill 0,
3225 1.1 jmcneill RXQ_CFG_QUEUE0_ENB,
3226 1.1 jmcneill RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB,
3227 1.1 jmcneill RXQ_CFG_QUEUE0_ENB | RXQ_CFG_QUEUE1_ENB | RXQ_CFG_QUEUE2_ENB,
3228 1.1 jmcneill RXQ_CFG_ENB
3229 1.1 jmcneill };
3230 1.1 jmcneill uint32_t cfg;
3231 1.1 jmcneill
3232 1.1 jmcneill /* Enable RxQ. */
3233 1.1 jmcneill cfg = CSR_READ_4(sc, ALC_RXQ_CFG);
3234 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3235 1.12 christos cfg &= ~RXQ_CFG_ENB;
3236 1.12 christos cfg |= qcfg[1];
3237 1.12 christos } else
3238 1.12 christos cfg |= RXQ_CFG_QUEUE0_ENB;
3239 1.1 jmcneill CSR_WRITE_4(sc, ALC_RXQ_CFG, cfg);
3240 1.1 jmcneill /* Enable TxQ. */
3241 1.1 jmcneill cfg = CSR_READ_4(sc, ALC_TXQ_CFG);
3242 1.1 jmcneill cfg |= TXQ_CFG_ENB;
3243 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, cfg);
3244 1.1 jmcneill }
3245 1.1 jmcneill
3246 1.1 jmcneill static void
3247 1.1 jmcneill alc_stop_queue(struct alc_softc *sc)
3248 1.1 jmcneill {
3249 1.1 jmcneill uint32_t reg;
3250 1.1 jmcneill int i;
3251 1.1 jmcneill
3252 1.1 jmcneill /* Disable RxQ. */
3253 1.1 jmcneill reg = CSR_READ_4(sc, ALC_RXQ_CFG);
3254 1.12 christos if ((sc->alc_flags & ALC_FLAG_AR816X_FAMILY) == 0) {
3255 1.12 christos if ((reg & RXQ_CFG_ENB) != 0) {
3256 1.12 christos reg &= ~RXQ_CFG_ENB;
3257 1.12 christos CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3258 1.12 christos }
3259 1.12 christos } else {
3260 1.12 christos if ((reg & RXQ_CFG_QUEUE0_ENB) != 0) {
3261 1.12 christos reg &= ~RXQ_CFG_QUEUE0_ENB;
3262 1.12 christos CSR_WRITE_4(sc, ALC_RXQ_CFG, reg);
3263 1.12 christos }
3264 1.12 christos }
3265 1.1 jmcneill /* Disable TxQ. */
3266 1.1 jmcneill reg = CSR_READ_4(sc, ALC_TXQ_CFG);
3267 1.2 jmcneill if ((reg & TXQ_CFG_ENB) != 0) {
3268 1.1 jmcneill reg &= ~TXQ_CFG_ENB;
3269 1.1 jmcneill CSR_WRITE_4(sc, ALC_TXQ_CFG, reg);
3270 1.1 jmcneill }
3271 1.12 christos DELAY(40);
3272 1.1 jmcneill for (i = ALC_TIMEOUT; i > 0; i--) {
3273 1.1 jmcneill reg = CSR_READ_4(sc, ALC_IDLE_STATUS);
3274 1.1 jmcneill if ((reg & (IDLE_STATUS_RXQ | IDLE_STATUS_TXQ)) == 0)
3275 1.1 jmcneill break;
3276 1.1 jmcneill DELAY(10);
3277 1.1 jmcneill }
3278 1.1 jmcneill if (i == 0)
3279 1.1 jmcneill printf("%s: could not disable RxQ/TxQ (0x%08x)!\n",
3280 1.1 jmcneill device_xname(sc->sc_dev), reg);
3281 1.1 jmcneill }
3282 1.1 jmcneill
3283 1.1 jmcneill static void
3284 1.1 jmcneill alc_init_tx_ring(struct alc_softc *sc)
3285 1.1 jmcneill {
3286 1.1 jmcneill struct alc_ring_data *rd;
3287 1.1 jmcneill struct alc_txdesc *txd;
3288 1.1 jmcneill int i;
3289 1.1 jmcneill
3290 1.1 jmcneill sc->alc_cdata.alc_tx_prod = 0;
3291 1.1 jmcneill sc->alc_cdata.alc_tx_cons = 0;
3292 1.1 jmcneill sc->alc_cdata.alc_tx_cnt = 0;
3293 1.1 jmcneill
3294 1.1 jmcneill rd = &sc->alc_rdata;
3295 1.1 jmcneill memset(rd->alc_tx_ring, 0, ALC_TX_RING_SZ);
3296 1.1 jmcneill for (i = 0; i < ALC_TX_RING_CNT; i++) {
3297 1.1 jmcneill txd = &sc->alc_cdata.alc_txdesc[i];
3298 1.1 jmcneill txd->tx_m = NULL;
3299 1.1 jmcneill }
3300 1.1 jmcneill
3301 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_tx_ring_map, 0,
3302 1.1 jmcneill sc->alc_cdata.alc_tx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
3303 1.1 jmcneill }
3304 1.1 jmcneill
3305 1.1 jmcneill static int
3306 1.7 mrg alc_init_rx_ring(struct alc_softc *sc, bool init)
3307 1.1 jmcneill {
3308 1.1 jmcneill struct alc_ring_data *rd;
3309 1.1 jmcneill struct alc_rxdesc *rxd;
3310 1.1 jmcneill int i;
3311 1.1 jmcneill
3312 1.1 jmcneill sc->alc_cdata.alc_rx_cons = ALC_RX_RING_CNT - 1;
3313 1.1 jmcneill rd = &sc->alc_rdata;
3314 1.1 jmcneill memset(rd->alc_rx_ring, 0, ALC_RX_RING_SZ);
3315 1.1 jmcneill for (i = 0; i < ALC_RX_RING_CNT; i++) {
3316 1.1 jmcneill rxd = &sc->alc_cdata.alc_rxdesc[i];
3317 1.1 jmcneill rxd->rx_m = NULL;
3318 1.1 jmcneill rxd->rx_desc = &rd->alc_rx_ring[i];
3319 1.7 mrg if (alc_newbuf(sc, rxd, init) != 0)
3320 1.1 jmcneill return (ENOBUFS);
3321 1.1 jmcneill }
3322 1.1 jmcneill
3323 1.1 jmcneill /*
3324 1.1 jmcneill * Since controller does not update Rx descriptors, driver
3325 1.1 jmcneill * does have to read Rx descriptors back so BUS_DMASYNC_PREWRITE
3326 1.1 jmcneill * is enough to ensure coherence.
3327 1.1 jmcneill */
3328 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rx_ring_map, 0,
3329 1.1 jmcneill sc->alc_cdata.alc_rx_ring_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
3330 1.1 jmcneill /* Let controller know availability of new Rx buffers. */
3331 1.1 jmcneill CSR_WRITE_4(sc, ALC_MBOX_RD0_PROD_IDX, sc->alc_cdata.alc_rx_cons);
3332 1.1 jmcneill
3333 1.1 jmcneill return (0);
3334 1.1 jmcneill }
3335 1.1 jmcneill
3336 1.1 jmcneill static void
3337 1.1 jmcneill alc_init_rr_ring(struct alc_softc *sc)
3338 1.1 jmcneill {
3339 1.1 jmcneill struct alc_ring_data *rd;
3340 1.1 jmcneill
3341 1.1 jmcneill sc->alc_cdata.alc_rr_cons = 0;
3342 1.1 jmcneill ALC_RXCHAIN_RESET(sc);
3343 1.1 jmcneill
3344 1.1 jmcneill rd = &sc->alc_rdata;
3345 1.1 jmcneill memset(rd->alc_rr_ring, 0, ALC_RR_RING_SZ);
3346 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_rr_ring_map, 0,
3347 1.15 leot sc->alc_cdata.alc_rr_ring_map->dm_mapsize,
3348 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3349 1.1 jmcneill }
3350 1.1 jmcneill
3351 1.1 jmcneill static void
3352 1.1 jmcneill alc_init_cmb(struct alc_softc *sc)
3353 1.1 jmcneill {
3354 1.1 jmcneill struct alc_ring_data *rd;
3355 1.1 jmcneill
3356 1.1 jmcneill rd = &sc->alc_rdata;
3357 1.1 jmcneill memset(rd->alc_cmb, 0, ALC_CMB_SZ);
3358 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_cmb_map, 0,
3359 1.15 leot sc->alc_cdata.alc_cmb_map->dm_mapsize,
3360 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3361 1.1 jmcneill }
3362 1.1 jmcneill
3363 1.1 jmcneill static void
3364 1.1 jmcneill alc_init_smb(struct alc_softc *sc)
3365 1.1 jmcneill {
3366 1.1 jmcneill struct alc_ring_data *rd;
3367 1.1 jmcneill
3368 1.1 jmcneill rd = &sc->alc_rdata;
3369 1.1 jmcneill memset(rd->alc_smb, 0, ALC_SMB_SZ);
3370 1.1 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->alc_cdata.alc_smb_map, 0,
3371 1.15 leot sc->alc_cdata.alc_smb_map->dm_mapsize,
3372 1.15 leot BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3373 1.1 jmcneill }
3374 1.1 jmcneill
3375 1.1 jmcneill static void
3376 1.1 jmcneill alc_rxvlan(struct alc_softc *sc)
3377 1.1 jmcneill {
3378 1.1 jmcneill uint32_t reg;
3379 1.1 jmcneill
3380 1.1 jmcneill reg = CSR_READ_4(sc, ALC_MAC_CFG);
3381 1.3 sborrill if (sc->sc_ec.ec_capenable & ETHERCAP_VLAN_HWTAGGING)
3382 1.1 jmcneill reg |= MAC_CFG_VLAN_TAG_STRIP;
3383 1.1 jmcneill else
3384 1.1 jmcneill reg &= ~MAC_CFG_VLAN_TAG_STRIP;
3385 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, reg);
3386 1.1 jmcneill }
3387 1.1 jmcneill
3388 1.1 jmcneill static void
3389 1.1 jmcneill alc_iff(struct alc_softc *sc)
3390 1.1 jmcneill {
3391 1.1 jmcneill struct ethercom *ec = &sc->sc_ec;
3392 1.1 jmcneill struct ifnet *ifp = &ec->ec_if;
3393 1.1 jmcneill struct ether_multi *enm;
3394 1.1 jmcneill struct ether_multistep step;
3395 1.1 jmcneill uint32_t crc;
3396 1.1 jmcneill uint32_t mchash[2];
3397 1.1 jmcneill uint32_t rxcfg;
3398 1.1 jmcneill
3399 1.1 jmcneill rxcfg = CSR_READ_4(sc, ALC_MAC_CFG);
3400 1.1 jmcneill rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
3401 1.1 jmcneill ifp->if_flags &= ~IFF_ALLMULTI;
3402 1.1 jmcneill
3403 1.1 jmcneill /*
3404 1.1 jmcneill * Always accept broadcast frames.
3405 1.1 jmcneill */
3406 1.1 jmcneill rxcfg |= MAC_CFG_BCAST;
3407 1.1 jmcneill
3408 1.1 jmcneill if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
3409 1.1 jmcneill ifp->if_flags |= IFF_ALLMULTI;
3410 1.1 jmcneill if (ifp->if_flags & IFF_PROMISC)
3411 1.1 jmcneill rxcfg |= MAC_CFG_PROMISC;
3412 1.1 jmcneill else
3413 1.1 jmcneill rxcfg |= MAC_CFG_ALLMULTI;
3414 1.1 jmcneill mchash[0] = mchash[1] = 0xFFFFFFFF;
3415 1.1 jmcneill } else {
3416 1.1 jmcneill /* Program new filter. */
3417 1.1 jmcneill memset(mchash, 0, sizeof(mchash));
3418 1.1 jmcneill
3419 1.1 jmcneill ETHER_FIRST_MULTI(step, ec, enm);
3420 1.1 jmcneill while (enm != NULL) {
3421 1.1 jmcneill crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
3422 1.1 jmcneill mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
3423 1.1 jmcneill ETHER_NEXT_MULTI(step, enm);
3424 1.1 jmcneill }
3425 1.1 jmcneill }
3426 1.1 jmcneill
3427 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAR0, mchash[0]);
3428 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAR1, mchash[1]);
3429 1.1 jmcneill CSR_WRITE_4(sc, ALC_MAC_CFG, rxcfg);
3430 1.1 jmcneill }
3431 1.1 jmcneill
3432 1.5 jmcneill MODULE(MODULE_CLASS_DRIVER, if_alc, "pci");
3433 1.1 jmcneill
3434 1.1 jmcneill #ifdef _MODULE
3435 1.1 jmcneill #include "ioconf.c"
3436 1.1 jmcneill #endif
3437 1.1 jmcneill
3438 1.1 jmcneill static int
3439 1.1 jmcneill if_alc_modcmd(modcmd_t cmd, void *opaque)
3440 1.1 jmcneill {
3441 1.1 jmcneill int error = 0;
3442 1.1 jmcneill
3443 1.1 jmcneill switch (cmd) {
3444 1.1 jmcneill case MODULE_CMD_INIT:
3445 1.1 jmcneill #ifdef _MODULE
3446 1.1 jmcneill error = config_init_component(cfdriver_ioconf_if_alc,
3447 1.1 jmcneill cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
3448 1.1 jmcneill #endif
3449 1.1 jmcneill return error;
3450 1.1 jmcneill case MODULE_CMD_FINI:
3451 1.1 jmcneill #ifdef _MODULE
3452 1.1 jmcneill error = config_fini_component(cfdriver_ioconf_if_alc,
3453 1.1 jmcneill cfattach_ioconf_if_alc, cfdata_ioconf_if_alc);
3454 1.1 jmcneill #endif
3455 1.1 jmcneill return error;
3456 1.1 jmcneill default:
3457 1.1 jmcneill return ENOTTY;
3458 1.1 jmcneill }
3459 1.1 jmcneill }
3460