if_ae.c revision 1.2.6.3 1 1.2.6.3 yamt /* $Id: if_ae.c,v 1.2.6.3 2006/12/30 20:46:30 yamt Exp $ */
2 1.2.6.2 yamt /*-
3 1.2.6.2 yamt * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4 1.2.6.2 yamt * Copyright (c) 2006 Garrett D'Amore.
5 1.2.6.2 yamt * All rights reserved.
6 1.2.6.2 yamt *
7 1.2.6.2 yamt * This code was written by Garrett D'Amore for the Champaign-Urbana
8 1.2.6.2 yamt * Community Wireless Network Project.
9 1.2.6.2 yamt *
10 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or
11 1.2.6.2 yamt * without modification, are permitted provided that the following
12 1.2.6.2 yamt * conditions are met:
13 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above
16 1.2.6.2 yamt * copyright notice, this list of conditions and the following
17 1.2.6.2 yamt * disclaimer in the documentation and/or other materials provided
18 1.2.6.2 yamt * with the distribution.
19 1.2.6.2 yamt * 3. All advertising materials mentioning features or use of this
20 1.2.6.2 yamt * software must display the following acknowledgements:
21 1.2.6.2 yamt * This product includes software developed by the Urbana-Champaign
22 1.2.6.2 yamt * Independent Media Center.
23 1.2.6.2 yamt * This product includes software developed by Garrett D'Amore.
24 1.2.6.2 yamt * 4. Urbana-Champaign Independent Media Center's name and Garrett
25 1.2.6.2 yamt * D'Amore's name may not be used to endorse or promote products
26 1.2.6.2 yamt * derived from this software without specific prior written permission.
27 1.2.6.2 yamt *
28 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29 1.2.6.2 yamt * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30 1.2.6.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 1.2.6.2 yamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.2.6.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33 1.2.6.2 yamt * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34 1.2.6.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 1.2.6.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 1.2.6.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 1.2.6.2 yamt * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.2.6.2 yamt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.2.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40 1.2.6.2 yamt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.2.6.2 yamt */
42 1.2.6.2 yamt /*-
43 1.2.6.2 yamt * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
44 1.2.6.2 yamt * All rights reserved.
45 1.2.6.2 yamt *
46 1.2.6.2 yamt * This code is derived from software contributed to The NetBSD Foundation
47 1.2.6.2 yamt * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
48 1.2.6.2 yamt * NASA Ames Research Center; and by Charles M. Hannum.
49 1.2.6.2 yamt *
50 1.2.6.2 yamt * Redistribution and use in source and binary forms, with or without
51 1.2.6.2 yamt * modification, are permitted provided that the following conditions
52 1.2.6.2 yamt * are met:
53 1.2.6.2 yamt * 1. Redistributions of source code must retain the above copyright
54 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer.
55 1.2.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
56 1.2.6.2 yamt * notice, this list of conditions and the following disclaimer in the
57 1.2.6.2 yamt * documentation and/or other materials provided with the distribution.
58 1.2.6.2 yamt * 3. All advertising materials mentioning features or use of this software
59 1.2.6.2 yamt * must display the following acknowledgement:
60 1.2.6.2 yamt * This product includes software developed by the NetBSD
61 1.2.6.2 yamt * Foundation, Inc. and its contributors.
62 1.2.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
63 1.2.6.2 yamt * contributors may be used to endorse or promote products derived
64 1.2.6.2 yamt * from this software without specific prior written permission.
65 1.2.6.2 yamt *
66 1.2.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
67 1.2.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
68 1.2.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69 1.2.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
70 1.2.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
71 1.2.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
72 1.2.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
73 1.2.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
74 1.2.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
75 1.2.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
76 1.2.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
77 1.2.6.2 yamt */
78 1.2.6.2 yamt
79 1.2.6.2 yamt /*
80 1.2.6.2 yamt * Device driver for the onboard ethernet MAC found on the AR5312
81 1.2.6.2 yamt * chip's AHB bus.
82 1.2.6.2 yamt *
83 1.2.6.2 yamt * This device is very simliar to the tulip in most regards, and
84 1.2.6.2 yamt * the code is directly derived from NetBSD's tulip.c. However, it
85 1.2.6.2 yamt * is different enough that it did not seem to be a good idea to
86 1.2.6.2 yamt * add further complexity to the tulip driver, so we have our own.
87 1.2.6.2 yamt *
88 1.2.6.2 yamt * Also tulip has a lot of complexity in it for various parts/options
89 1.2.6.2 yamt * that we don't need, and on these little boxes with only ~8MB RAM, we
90 1.2.6.2 yamt * don't want any extra bloat.
91 1.2.6.2 yamt */
92 1.2.6.2 yamt
93 1.2.6.2 yamt /*
94 1.2.6.2 yamt * TODO:
95 1.2.6.2 yamt *
96 1.2.6.2 yamt * 1) Find out about BUS_MODE_ALIGN16B. This chip can apparently align
97 1.2.6.2 yamt * inbound packets on a half-word boundary, which would make life easier
98 1.2.6.2 yamt * for TCP/IP. (Aligning IP headers on a word.)
99 1.2.6.2 yamt *
100 1.2.6.2 yamt * 2) There is stuff in original tulip to shut down the device when reacting
101 1.2.6.2 yamt * to a a change in link status. Is that needed.
102 1.2.6.2 yamt *
103 1.2.6.2 yamt * 3) Test with variety of 10/100 HDX/FDX scenarios.
104 1.2.6.2 yamt *
105 1.2.6.2 yamt */
106 1.2.6.2 yamt
107 1.2.6.2 yamt #include <sys/cdefs.h>
108 1.2.6.3 yamt __KERNEL_RCSID(0, "$NetBSD: if_ae.c,v 1.2.6.3 2006/12/30 20:46:30 yamt Exp $");
109 1.2.6.2 yamt
110 1.2.6.2 yamt #include "bpfilter.h"
111 1.2.6.2 yamt
112 1.2.6.2 yamt #include <sys/param.h>
113 1.2.6.2 yamt #include <sys/systm.h>
114 1.2.6.2 yamt #include <sys/callout.h>
115 1.2.6.2 yamt #include <sys/mbuf.h>
116 1.2.6.2 yamt #include <sys/malloc.h>
117 1.2.6.2 yamt #include <sys/kernel.h>
118 1.2.6.2 yamt #include <sys/socket.h>
119 1.2.6.2 yamt #include <sys/ioctl.h>
120 1.2.6.2 yamt #include <sys/errno.h>
121 1.2.6.2 yamt #include <sys/device.h>
122 1.2.6.2 yamt
123 1.2.6.2 yamt #include <machine/endian.h>
124 1.2.6.2 yamt
125 1.2.6.2 yamt #include <uvm/uvm_extern.h>
126 1.2.6.2 yamt
127 1.2.6.2 yamt #include <net/if.h>
128 1.2.6.2 yamt #include <net/if_dl.h>
129 1.2.6.2 yamt #include <net/if_media.h>
130 1.2.6.2 yamt #include <net/if_ether.h>
131 1.2.6.2 yamt
132 1.2.6.2 yamt #if NBPFILTER > 0
133 1.2.6.2 yamt #include <net/bpf.h>
134 1.2.6.2 yamt #endif
135 1.2.6.2 yamt
136 1.2.6.2 yamt #include <machine/bus.h>
137 1.2.6.2 yamt #include <machine/intr.h>
138 1.2.6.2 yamt
139 1.2.6.2 yamt #include <dev/mii/mii.h>
140 1.2.6.2 yamt #include <dev/mii/miivar.h>
141 1.2.6.2 yamt #include <dev/mii/mii_bitbang.h>
142 1.2.6.2 yamt
143 1.2.6.2 yamt #include <mips/atheros/include/arbusvar.h>
144 1.2.6.2 yamt #include <mips/atheros/dev/aereg.h>
145 1.2.6.2 yamt #include <mips/atheros/dev/aevar.h>
146 1.2.6.2 yamt
147 1.2.6.2 yamt static const struct {
148 1.2.6.2 yamt u_int32_t txth_opmode; /* OPMODE bits */
149 1.2.6.2 yamt const char *txth_name; /* name of mode */
150 1.2.6.2 yamt } ae_txthresh[] = {
151 1.2.6.2 yamt { OPMODE_TR_32, "32 words" },
152 1.2.6.2 yamt { OPMODE_TR_64, "64 words" },
153 1.2.6.2 yamt { OPMODE_TR_128, "128 words" },
154 1.2.6.2 yamt { OPMODE_TR_256, "256 words" },
155 1.2.6.2 yamt { OPMODE_SF, "store and forward mode" },
156 1.2.6.2 yamt { 0, NULL },
157 1.2.6.2 yamt };
158 1.2.6.2 yamt
159 1.2.6.2 yamt static int ae_match(struct device *, struct cfdata *, void *);
160 1.2.6.2 yamt static void ae_attach(struct device *, struct device *, void *);
161 1.2.6.2 yamt static int ae_detach(struct device *, int);
162 1.2.6.2 yamt static int ae_activate(struct device *, enum devact);
163 1.2.6.2 yamt
164 1.2.6.2 yamt static void ae_reset(struct ae_softc *);
165 1.2.6.2 yamt static void ae_idle(struct ae_softc *, u_int32_t);
166 1.2.6.2 yamt
167 1.2.6.2 yamt static int ae_mediachange(struct ifnet *);
168 1.2.6.2 yamt static void ae_mediastatus(struct ifnet *, struct ifmediareq *);
169 1.2.6.2 yamt
170 1.2.6.2 yamt static void ae_start(struct ifnet *);
171 1.2.6.2 yamt static void ae_watchdog(struct ifnet *);
172 1.2.6.2 yamt static int ae_ioctl(struct ifnet *, u_long, caddr_t);
173 1.2.6.2 yamt static int ae_init(struct ifnet *);
174 1.2.6.2 yamt static void ae_stop(struct ifnet *, int);
175 1.2.6.2 yamt
176 1.2.6.2 yamt static void ae_shutdown(void *);
177 1.2.6.2 yamt
178 1.2.6.2 yamt static void ae_rxdrain(struct ae_softc *);
179 1.2.6.2 yamt static int ae_add_rxbuf(struct ae_softc *, int);
180 1.2.6.2 yamt
181 1.2.6.2 yamt static int ae_enable(struct ae_softc *);
182 1.2.6.2 yamt static void ae_disable(struct ae_softc *);
183 1.2.6.2 yamt static void ae_power(int, void *);
184 1.2.6.2 yamt
185 1.2.6.2 yamt static void ae_filter_setup(struct ae_softc *);
186 1.2.6.2 yamt
187 1.2.6.2 yamt static int ae_intr(void *);
188 1.2.6.2 yamt static void ae_rxintr(struct ae_softc *);
189 1.2.6.2 yamt static void ae_txintr(struct ae_softc *);
190 1.2.6.2 yamt
191 1.2.6.2 yamt static void ae_mii_tick(void *);
192 1.2.6.2 yamt static void ae_mii_statchg(struct device *);
193 1.2.6.2 yamt
194 1.2.6.2 yamt static int ae_mii_readreg(struct device *, int, int);
195 1.2.6.2 yamt static void ae_mii_writereg(struct device *, int, int, int);
196 1.2.6.2 yamt
197 1.2.6.2 yamt #ifdef AE_DEBUG
198 1.2.6.2 yamt #define DPRINTF(sc, x) if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
199 1.2.6.2 yamt printf x
200 1.2.6.2 yamt #else
201 1.2.6.2 yamt #define DPRINTF(sc, x) /* nothing */
202 1.2.6.2 yamt #endif
203 1.2.6.2 yamt
204 1.2.6.2 yamt #ifdef AE_STATS
205 1.2.6.2 yamt static void ae_print_stats(struct ae_softc *);
206 1.2.6.2 yamt #endif
207 1.2.6.2 yamt
208 1.2.6.2 yamt CFATTACH_DECL(ae, sizeof(struct ae_softc),
209 1.2.6.2 yamt ae_match, ae_attach, ae_detach, ae_activate);
210 1.2.6.2 yamt
211 1.2.6.2 yamt /*
212 1.2.6.2 yamt * ae_match:
213 1.2.6.2 yamt *
214 1.2.6.2 yamt * Check for a device match.
215 1.2.6.2 yamt */
216 1.2.6.2 yamt int
217 1.2.6.2 yamt ae_match(struct device *parent, struct cfdata *cf, void *aux)
218 1.2.6.2 yamt {
219 1.2.6.2 yamt struct arbus_attach_args *aa = aux;
220 1.2.6.2 yamt
221 1.2.6.2 yamt if (strcmp(aa->aa_name, cf->cf_name) == 0)
222 1.2.6.2 yamt return 1;
223 1.2.6.2 yamt
224 1.2.6.2 yamt return 0;
225 1.2.6.2 yamt
226 1.2.6.2 yamt }
227 1.2.6.2 yamt
228 1.2.6.2 yamt /*
229 1.2.6.2 yamt * ae_attach:
230 1.2.6.2 yamt *
231 1.2.6.2 yamt * Attach an ae interface to the system.
232 1.2.6.2 yamt */
233 1.2.6.2 yamt void
234 1.2.6.2 yamt ae_attach(struct device *parent, struct device *self, void *aux)
235 1.2.6.2 yamt {
236 1.2.6.2 yamt const uint8_t *enaddr;
237 1.2.6.2 yamt prop_data_t ea;
238 1.2.6.2 yamt struct ae_softc *sc = (void *)self;
239 1.2.6.2 yamt struct arbus_attach_args *aa = aux;
240 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
241 1.2.6.2 yamt int i, error;
242 1.2.6.2 yamt
243 1.2.6.2 yamt callout_init(&sc->sc_tick_callout);
244 1.2.6.2 yamt
245 1.2.6.2 yamt printf(": Atheros AR531X 10/100 Ethernet\n");
246 1.2.6.2 yamt
247 1.2.6.2 yamt /*
248 1.2.6.2 yamt * Try to get MAC address.
249 1.2.6.2 yamt */
250 1.2.6.2 yamt ea = prop_dictionary_get(device_properties(&sc->sc_dev), "mac-addr");
251 1.2.6.2 yamt if (ea == NULL) {
252 1.2.6.2 yamt printf("%s: unable to get mac-addr property\n",
253 1.2.6.2 yamt sc->sc_dev.dv_xname);
254 1.2.6.2 yamt return;
255 1.2.6.2 yamt }
256 1.2.6.2 yamt KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
257 1.2.6.2 yamt KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
258 1.2.6.2 yamt enaddr = prop_data_data_nocopy(ea);
259 1.2.6.2 yamt
260 1.2.6.2 yamt /* Announce ourselves. */
261 1.2.6.2 yamt printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
262 1.2.6.2 yamt ether_sprintf(enaddr));
263 1.2.6.2 yamt
264 1.2.6.3 yamt sc->sc_cirq = aa->aa_cirq;
265 1.2.6.3 yamt sc->sc_mirq = aa->aa_mirq;
266 1.2.6.2 yamt sc->sc_st = aa->aa_bst;
267 1.2.6.2 yamt sc->sc_dmat = aa->aa_dmat;
268 1.2.6.2 yamt
269 1.2.6.2 yamt SIMPLEQ_INIT(&sc->sc_txfreeq);
270 1.2.6.2 yamt SIMPLEQ_INIT(&sc->sc_txdirtyq);
271 1.2.6.2 yamt
272 1.2.6.2 yamt /*
273 1.2.6.2 yamt * Map registers.
274 1.2.6.2 yamt */
275 1.2.6.2 yamt sc->sc_size = aa->aa_size;
276 1.2.6.2 yamt if ((error = bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
277 1.2.6.2 yamt &sc->sc_sh)) != 0) {
278 1.2.6.2 yamt printf("%s: unable to map registers, error = %d\n",
279 1.2.6.2 yamt sc->sc_dev.dv_xname, error);
280 1.2.6.2 yamt goto fail_0;
281 1.2.6.2 yamt }
282 1.2.6.2 yamt
283 1.2.6.2 yamt /*
284 1.2.6.2 yamt * Allocate the control data structures, and create and load the
285 1.2.6.2 yamt * DMA map for it.
286 1.2.6.2 yamt */
287 1.2.6.2 yamt if ((error = bus_dmamem_alloc(sc->sc_dmat,
288 1.2.6.2 yamt sizeof(struct ae_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
289 1.2.6.2 yamt 1, &sc->sc_cdnseg, 0)) != 0) {
290 1.2.6.2 yamt printf("%s: unable to allocate control data, error = %d\n",
291 1.2.6.2 yamt sc->sc_dev.dv_xname, error);
292 1.2.6.2 yamt goto fail_1;
293 1.2.6.2 yamt }
294 1.2.6.2 yamt
295 1.2.6.2 yamt if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
296 1.2.6.2 yamt sizeof(struct ae_control_data), (caddr_t *)&sc->sc_control_data,
297 1.2.6.2 yamt BUS_DMA_COHERENT)) != 0) {
298 1.2.6.2 yamt printf("%s: unable to map control data, error = %d\n",
299 1.2.6.2 yamt sc->sc_dev.dv_xname, error);
300 1.2.6.2 yamt goto fail_2;
301 1.2.6.2 yamt }
302 1.2.6.2 yamt
303 1.2.6.2 yamt if ((error = bus_dmamap_create(sc->sc_dmat,
304 1.2.6.2 yamt sizeof(struct ae_control_data), 1,
305 1.2.6.2 yamt sizeof(struct ae_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
306 1.2.6.2 yamt printf("%s: unable to create control data DMA map, "
307 1.2.6.2 yamt "error = %d\n", sc->sc_dev.dv_xname, error);
308 1.2.6.2 yamt goto fail_3;
309 1.2.6.2 yamt }
310 1.2.6.2 yamt
311 1.2.6.2 yamt if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
312 1.2.6.2 yamt sc->sc_control_data, sizeof(struct ae_control_data), NULL,
313 1.2.6.2 yamt 0)) != 0) {
314 1.2.6.2 yamt printf("%s: unable to load control data DMA map, error = %d\n",
315 1.2.6.2 yamt sc->sc_dev.dv_xname, error);
316 1.2.6.2 yamt goto fail_4;
317 1.2.6.2 yamt }
318 1.2.6.2 yamt
319 1.2.6.2 yamt /*
320 1.2.6.2 yamt * Create the transmit buffer DMA maps.
321 1.2.6.2 yamt */
322 1.2.6.2 yamt for (i = 0; i < AE_TXQUEUELEN; i++) {
323 1.2.6.2 yamt if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
324 1.2.6.2 yamt AE_NTXSEGS, MCLBYTES, 0, 0,
325 1.2.6.2 yamt &sc->sc_txsoft[i].txs_dmamap)) != 0) {
326 1.2.6.2 yamt printf("%s: unable to create tx DMA map %d, "
327 1.2.6.2 yamt "error = %d\n", sc->sc_dev.dv_xname, i, error);
328 1.2.6.2 yamt goto fail_5;
329 1.2.6.2 yamt }
330 1.2.6.2 yamt }
331 1.2.6.2 yamt
332 1.2.6.2 yamt /*
333 1.2.6.2 yamt * Create the receive buffer DMA maps.
334 1.2.6.2 yamt */
335 1.2.6.2 yamt for (i = 0; i < AE_NRXDESC; i++) {
336 1.2.6.2 yamt if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
337 1.2.6.2 yamt MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
338 1.2.6.2 yamt printf("%s: unable to create rx DMA map %d, "
339 1.2.6.2 yamt "error = %d\n", sc->sc_dev.dv_xname, i, error);
340 1.2.6.2 yamt goto fail_6;
341 1.2.6.2 yamt }
342 1.2.6.2 yamt sc->sc_rxsoft[i].rxs_mbuf = NULL;
343 1.2.6.2 yamt }
344 1.2.6.2 yamt
345 1.2.6.2 yamt /*
346 1.2.6.2 yamt * Reset the chip to a known state.
347 1.2.6.2 yamt */
348 1.2.6.2 yamt ae_reset(sc);
349 1.2.6.2 yamt
350 1.2.6.2 yamt /*
351 1.2.6.2 yamt * From this point forward, the attachment cannot fail. A failure
352 1.2.6.2 yamt * before this point releases all resources that may have been
353 1.2.6.2 yamt * allocated.
354 1.2.6.2 yamt */
355 1.2.6.2 yamt sc->sc_flags |= AE_ATTACHED;
356 1.2.6.2 yamt
357 1.2.6.2 yamt /*
358 1.2.6.2 yamt * Initialize our media structures. This may probe the MII, if
359 1.2.6.2 yamt * present.
360 1.2.6.2 yamt */
361 1.2.6.2 yamt sc->sc_mii.mii_ifp = ifp;
362 1.2.6.2 yamt sc->sc_mii.mii_readreg = ae_mii_readreg;
363 1.2.6.2 yamt sc->sc_mii.mii_writereg = ae_mii_writereg;
364 1.2.6.2 yamt sc->sc_mii.mii_statchg = ae_mii_statchg;
365 1.2.6.2 yamt ifmedia_init(&sc->sc_mii.mii_media, 0, ae_mediachange,
366 1.2.6.2 yamt ae_mediastatus);
367 1.2.6.2 yamt mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
368 1.2.6.2 yamt MII_OFFSET_ANY, 0);
369 1.2.6.2 yamt
370 1.2.6.2 yamt if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
371 1.2.6.2 yamt ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
372 1.2.6.2 yamt ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
373 1.2.6.2 yamt } else
374 1.2.6.2 yamt ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
375 1.2.6.2 yamt
376 1.2.6.2 yamt sc->sc_tick = ae_mii_tick;
377 1.2.6.2 yamt
378 1.2.6.2 yamt strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
379 1.2.6.2 yamt ifp->if_softc = sc;
380 1.2.6.2 yamt ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
381 1.2.6.2 yamt sc->sc_if_flags = ifp->if_flags;
382 1.2.6.2 yamt ifp->if_ioctl = ae_ioctl;
383 1.2.6.2 yamt ifp->if_start = ae_start;
384 1.2.6.2 yamt ifp->if_watchdog = ae_watchdog;
385 1.2.6.2 yamt ifp->if_init = ae_init;
386 1.2.6.2 yamt ifp->if_stop = ae_stop;
387 1.2.6.2 yamt IFQ_SET_READY(&ifp->if_snd);
388 1.2.6.2 yamt
389 1.2.6.2 yamt /*
390 1.2.6.2 yamt * We can support 802.1Q VLAN-sized frames.
391 1.2.6.2 yamt */
392 1.2.6.2 yamt sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
393 1.2.6.2 yamt
394 1.2.6.2 yamt /*
395 1.2.6.2 yamt * Attach the interface.
396 1.2.6.2 yamt */
397 1.2.6.2 yamt if_attach(ifp);
398 1.2.6.2 yamt ether_ifattach(ifp, enaddr);
399 1.2.6.2 yamt
400 1.2.6.2 yamt #if NRND > 0
401 1.2.6.2 yamt rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
402 1.2.6.2 yamt RND_TYPE_NET, 0);
403 1.2.6.2 yamt #endif
404 1.2.6.2 yamt
405 1.2.6.2 yamt /*
406 1.2.6.2 yamt * Make sure the interface is shutdown during reboot.
407 1.2.6.2 yamt */
408 1.2.6.2 yamt sc->sc_sdhook = shutdownhook_establish(ae_shutdown, sc);
409 1.2.6.2 yamt if (sc->sc_sdhook == NULL)
410 1.2.6.2 yamt printf("%s: WARNING: unable to establish shutdown hook\n",
411 1.2.6.2 yamt sc->sc_dev.dv_xname);
412 1.2.6.2 yamt
413 1.2.6.2 yamt /*
414 1.2.6.2 yamt * Add a suspend hook to make sure we come back up after a
415 1.2.6.2 yamt * resume.
416 1.2.6.2 yamt */
417 1.2.6.3 yamt sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
418 1.2.6.3 yamt ae_power, sc);
419 1.2.6.2 yamt if (sc->sc_powerhook == NULL)
420 1.2.6.2 yamt printf("%s: WARNING: unable to establish power hook\n",
421 1.2.6.2 yamt sc->sc_dev.dv_xname);
422 1.2.6.2 yamt return;
423 1.2.6.2 yamt
424 1.2.6.2 yamt /*
425 1.2.6.2 yamt * Free any resources we've allocated during the failed attach
426 1.2.6.2 yamt * attempt. Do this in reverse order and fall through.
427 1.2.6.2 yamt */
428 1.2.6.2 yamt fail_6:
429 1.2.6.2 yamt for (i = 0; i < AE_NRXDESC; i++) {
430 1.2.6.2 yamt if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
431 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat,
432 1.2.6.2 yamt sc->sc_rxsoft[i].rxs_dmamap);
433 1.2.6.2 yamt }
434 1.2.6.2 yamt fail_5:
435 1.2.6.2 yamt for (i = 0; i < AE_TXQUEUELEN; i++) {
436 1.2.6.2 yamt if (sc->sc_txsoft[i].txs_dmamap != NULL)
437 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat,
438 1.2.6.2 yamt sc->sc_txsoft[i].txs_dmamap);
439 1.2.6.2 yamt }
440 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
441 1.2.6.2 yamt fail_4:
442 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
443 1.2.6.2 yamt fail_3:
444 1.2.6.2 yamt bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
445 1.2.6.2 yamt sizeof(struct ae_control_data));
446 1.2.6.2 yamt fail_2:
447 1.2.6.2 yamt bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
448 1.2.6.2 yamt fail_1:
449 1.2.6.2 yamt bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
450 1.2.6.2 yamt fail_0:
451 1.2.6.2 yamt return;
452 1.2.6.2 yamt }
453 1.2.6.2 yamt
454 1.2.6.2 yamt /*
455 1.2.6.2 yamt * ae_activate:
456 1.2.6.2 yamt *
457 1.2.6.2 yamt * Handle device activation/deactivation requests.
458 1.2.6.2 yamt */
459 1.2.6.2 yamt int
460 1.2.6.2 yamt ae_activate(struct device *self, enum devact act)
461 1.2.6.2 yamt {
462 1.2.6.2 yamt struct ae_softc *sc = (void *) self;
463 1.2.6.2 yamt int s, error = 0;
464 1.2.6.2 yamt
465 1.2.6.2 yamt s = splnet();
466 1.2.6.2 yamt switch (act) {
467 1.2.6.2 yamt case DVACT_ACTIVATE:
468 1.2.6.2 yamt error = EOPNOTSUPP;
469 1.2.6.2 yamt break;
470 1.2.6.2 yamt
471 1.2.6.2 yamt case DVACT_DEACTIVATE:
472 1.2.6.2 yamt mii_activate(&sc->sc_mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
473 1.2.6.2 yamt if_deactivate(&sc->sc_ethercom.ec_if);
474 1.2.6.2 yamt break;
475 1.2.6.2 yamt }
476 1.2.6.2 yamt splx(s);
477 1.2.6.2 yamt
478 1.2.6.2 yamt return (error);
479 1.2.6.2 yamt }
480 1.2.6.2 yamt
481 1.2.6.2 yamt /*
482 1.2.6.2 yamt * ae_detach:
483 1.2.6.2 yamt *
484 1.2.6.2 yamt * Detach a device interface.
485 1.2.6.2 yamt */
486 1.2.6.2 yamt int
487 1.2.6.2 yamt ae_detach(struct device *self, int flags)
488 1.2.6.2 yamt {
489 1.2.6.2 yamt struct ae_softc *sc = (void *)self;
490 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
491 1.2.6.2 yamt struct ae_rxsoft *rxs;
492 1.2.6.2 yamt struct ae_txsoft *txs;
493 1.2.6.2 yamt int i;
494 1.2.6.2 yamt
495 1.2.6.2 yamt /*
496 1.2.6.2 yamt * Succeed now if there isn't any work to do.
497 1.2.6.2 yamt */
498 1.2.6.2 yamt if ((sc->sc_flags & AE_ATTACHED) == 0)
499 1.2.6.2 yamt return (0);
500 1.2.6.2 yamt
501 1.2.6.2 yamt /* Unhook our tick handler. */
502 1.2.6.2 yamt if (sc->sc_tick)
503 1.2.6.2 yamt callout_stop(&sc->sc_tick_callout);
504 1.2.6.2 yamt
505 1.2.6.2 yamt /* Detach all PHYs */
506 1.2.6.2 yamt mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
507 1.2.6.2 yamt
508 1.2.6.2 yamt /* Delete all remaining media. */
509 1.2.6.2 yamt ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
510 1.2.6.2 yamt
511 1.2.6.2 yamt #if NRND > 0
512 1.2.6.2 yamt rnd_detach_source(&sc->sc_rnd_source);
513 1.2.6.2 yamt #endif
514 1.2.6.2 yamt ether_ifdetach(ifp);
515 1.2.6.2 yamt if_detach(ifp);
516 1.2.6.2 yamt
517 1.2.6.2 yamt for (i = 0; i < AE_NRXDESC; i++) {
518 1.2.6.2 yamt rxs = &sc->sc_rxsoft[i];
519 1.2.6.2 yamt if (rxs->rxs_mbuf != NULL) {
520 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
521 1.2.6.2 yamt m_freem(rxs->rxs_mbuf);
522 1.2.6.2 yamt rxs->rxs_mbuf = NULL;
523 1.2.6.2 yamt }
524 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
525 1.2.6.2 yamt }
526 1.2.6.2 yamt for (i = 0; i < AE_TXQUEUELEN; i++) {
527 1.2.6.2 yamt txs = &sc->sc_txsoft[i];
528 1.2.6.2 yamt if (txs->txs_mbuf != NULL) {
529 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
530 1.2.6.2 yamt m_freem(txs->txs_mbuf);
531 1.2.6.2 yamt txs->txs_mbuf = NULL;
532 1.2.6.2 yamt }
533 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
534 1.2.6.2 yamt }
535 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
536 1.2.6.2 yamt bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
537 1.2.6.2 yamt bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
538 1.2.6.2 yamt sizeof(struct ae_control_data));
539 1.2.6.2 yamt bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
540 1.2.6.2 yamt
541 1.2.6.2 yamt shutdownhook_disestablish(sc->sc_sdhook);
542 1.2.6.2 yamt powerhook_disestablish(sc->sc_powerhook);
543 1.2.6.2 yamt
544 1.2.6.2 yamt bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_size);
545 1.2.6.2 yamt
546 1.2.6.2 yamt
547 1.2.6.2 yamt return (0);
548 1.2.6.2 yamt }
549 1.2.6.2 yamt
550 1.2.6.2 yamt /*
551 1.2.6.2 yamt * ae_shutdown:
552 1.2.6.2 yamt *
553 1.2.6.2 yamt * Make sure the interface is stopped at reboot time.
554 1.2.6.2 yamt */
555 1.2.6.2 yamt static void
556 1.2.6.2 yamt ae_shutdown(void *arg)
557 1.2.6.2 yamt {
558 1.2.6.2 yamt struct ae_softc *sc = arg;
559 1.2.6.2 yamt
560 1.2.6.2 yamt ae_stop(&sc->sc_ethercom.ec_if, 1);
561 1.2.6.2 yamt }
562 1.2.6.2 yamt
563 1.2.6.2 yamt /*
564 1.2.6.2 yamt * ae_start: [ifnet interface function]
565 1.2.6.2 yamt *
566 1.2.6.2 yamt * Start packet transmission on the interface.
567 1.2.6.2 yamt */
568 1.2.6.2 yamt static void
569 1.2.6.2 yamt ae_start(struct ifnet *ifp)
570 1.2.6.2 yamt {
571 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
572 1.2.6.2 yamt struct mbuf *m0, *m;
573 1.2.6.2 yamt struct ae_txsoft *txs, *last_txs = NULL;
574 1.2.6.2 yamt bus_dmamap_t dmamap;
575 1.2.6.2 yamt int error, firsttx, nexttx, lasttx = 1, ofree, seg;
576 1.2.6.2 yamt
577 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_start: sc_flags 0x%08x, if_flags 0x%08x\n",
578 1.2.6.2 yamt sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
579 1.2.6.2 yamt
580 1.2.6.2 yamt
581 1.2.6.2 yamt if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
582 1.2.6.2 yamt return;
583 1.2.6.2 yamt
584 1.2.6.2 yamt /*
585 1.2.6.2 yamt * Remember the previous number of free descriptors and
586 1.2.6.2 yamt * the first descriptor we'll use.
587 1.2.6.2 yamt */
588 1.2.6.2 yamt ofree = sc->sc_txfree;
589 1.2.6.2 yamt firsttx = sc->sc_txnext;
590 1.2.6.2 yamt
591 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_start: txfree %d, txnext %d\n",
592 1.2.6.2 yamt sc->sc_dev.dv_xname, ofree, firsttx));
593 1.2.6.2 yamt
594 1.2.6.2 yamt /*
595 1.2.6.2 yamt * Loop through the send queue, setting up transmit descriptors
596 1.2.6.2 yamt * until we drain the queue, or use up all available transmit
597 1.2.6.2 yamt * descriptors.
598 1.2.6.2 yamt */
599 1.2.6.2 yamt while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
600 1.2.6.2 yamt sc->sc_txfree != 0) {
601 1.2.6.2 yamt /*
602 1.2.6.2 yamt * Grab a packet off the queue.
603 1.2.6.2 yamt */
604 1.2.6.2 yamt IFQ_POLL(&ifp->if_snd, m0);
605 1.2.6.2 yamt if (m0 == NULL)
606 1.2.6.2 yamt break;
607 1.2.6.2 yamt m = NULL;
608 1.2.6.2 yamt
609 1.2.6.2 yamt dmamap = txs->txs_dmamap;
610 1.2.6.2 yamt
611 1.2.6.2 yamt /*
612 1.2.6.2 yamt * Load the DMA map. If this fails, the packet either
613 1.2.6.2 yamt * didn't fit in the alloted number of segments, or we were
614 1.2.6.2 yamt * short on resources. In this case, we'll copy and try
615 1.2.6.2 yamt * again.
616 1.2.6.2 yamt */
617 1.2.6.2 yamt if (((mtod(m0, uintptr_t) & 3) != 0) ||
618 1.2.6.2 yamt bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
619 1.2.6.2 yamt BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
620 1.2.6.2 yamt MGETHDR(m, M_DONTWAIT, MT_DATA);
621 1.2.6.2 yamt if (m == NULL) {
622 1.2.6.2 yamt printf("%s: unable to allocate Tx mbuf\n",
623 1.2.6.2 yamt sc->sc_dev.dv_xname);
624 1.2.6.2 yamt break;
625 1.2.6.2 yamt }
626 1.2.6.2 yamt MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
627 1.2.6.2 yamt if (m0->m_pkthdr.len > MHLEN) {
628 1.2.6.2 yamt MCLGET(m, M_DONTWAIT);
629 1.2.6.2 yamt if ((m->m_flags & M_EXT) == 0) {
630 1.2.6.2 yamt printf("%s: unable to allocate Tx "
631 1.2.6.2 yamt "cluster\n", sc->sc_dev.dv_xname);
632 1.2.6.2 yamt m_freem(m);
633 1.2.6.2 yamt break;
634 1.2.6.2 yamt }
635 1.2.6.2 yamt }
636 1.2.6.2 yamt m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
637 1.2.6.2 yamt m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
638 1.2.6.2 yamt error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
639 1.2.6.2 yamt m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
640 1.2.6.2 yamt if (error) {
641 1.2.6.2 yamt printf("%s: unable to load Tx buffer, "
642 1.2.6.2 yamt "error = %d\n", sc->sc_dev.dv_xname,
643 1.2.6.2 yamt error);
644 1.2.6.2 yamt break;
645 1.2.6.2 yamt }
646 1.2.6.2 yamt }
647 1.2.6.2 yamt
648 1.2.6.2 yamt /*
649 1.2.6.2 yamt * Ensure we have enough descriptors free to describe
650 1.2.6.2 yamt * the packet.
651 1.2.6.2 yamt */
652 1.2.6.2 yamt if (dmamap->dm_nsegs > sc->sc_txfree) {
653 1.2.6.2 yamt /*
654 1.2.6.2 yamt * Not enough free descriptors to transmit this
655 1.2.6.2 yamt * packet. We haven't committed to anything yet,
656 1.2.6.2 yamt * so just unload the DMA map, put the packet
657 1.2.6.2 yamt * back on the queue, and punt. Notify the upper
658 1.2.6.2 yamt * layer that there are no more slots left.
659 1.2.6.2 yamt *
660 1.2.6.2 yamt * XXX We could allocate an mbuf and copy, but
661 1.2.6.2 yamt * XXX it is worth it?
662 1.2.6.2 yamt */
663 1.2.6.2 yamt ifp->if_flags |= IFF_OACTIVE;
664 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, dmamap);
665 1.2.6.2 yamt if (m != NULL)
666 1.2.6.2 yamt m_freem(m);
667 1.2.6.2 yamt break;
668 1.2.6.2 yamt }
669 1.2.6.2 yamt
670 1.2.6.2 yamt IFQ_DEQUEUE(&ifp->if_snd, m0);
671 1.2.6.2 yamt if (m != NULL) {
672 1.2.6.2 yamt m_freem(m0);
673 1.2.6.2 yamt m0 = m;
674 1.2.6.2 yamt }
675 1.2.6.2 yamt
676 1.2.6.2 yamt /*
677 1.2.6.2 yamt * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
678 1.2.6.2 yamt */
679 1.2.6.2 yamt
680 1.2.6.2 yamt /* Sync the DMA map. */
681 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
682 1.2.6.2 yamt BUS_DMASYNC_PREWRITE);
683 1.2.6.2 yamt
684 1.2.6.2 yamt /*
685 1.2.6.2 yamt * Initialize the transmit descriptors.
686 1.2.6.2 yamt */
687 1.2.6.2 yamt for (nexttx = sc->sc_txnext, seg = 0;
688 1.2.6.2 yamt seg < dmamap->dm_nsegs;
689 1.2.6.2 yamt seg++, nexttx = AE_NEXTTX(nexttx)) {
690 1.2.6.2 yamt /*
691 1.2.6.2 yamt * If this is the first descriptor we're
692 1.2.6.2 yamt * enqueueing, don't set the OWN bit just
693 1.2.6.2 yamt * yet. That could cause a race condition.
694 1.2.6.2 yamt * We'll do it below.
695 1.2.6.2 yamt */
696 1.2.6.2 yamt sc->sc_txdescs[nexttx].ad_status =
697 1.2.6.2 yamt (nexttx == firsttx) ? 0 : ADSTAT_OWN;
698 1.2.6.2 yamt sc->sc_txdescs[nexttx].ad_bufaddr1 =
699 1.2.6.2 yamt dmamap->dm_segs[seg].ds_addr;
700 1.2.6.2 yamt sc->sc_txdescs[nexttx].ad_ctl =
701 1.2.6.2 yamt (dmamap->dm_segs[seg].ds_len <<
702 1.2.6.2 yamt ADCTL_SIZE1_SHIFT) |
703 1.2.6.2 yamt (nexttx == (AE_NTXDESC - 1) ?
704 1.2.6.2 yamt ADCTL_ER : 0);
705 1.2.6.2 yamt lasttx = nexttx;
706 1.2.6.2 yamt }
707 1.2.6.2 yamt
708 1.2.6.2 yamt KASSERT(lasttx != -1);
709 1.2.6.2 yamt
710 1.2.6.2 yamt /* Set `first segment' and `last segment' appropriately. */
711 1.2.6.2 yamt sc->sc_txdescs[sc->sc_txnext].ad_ctl |= ADCTL_Tx_FS;
712 1.2.6.2 yamt sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_LS;
713 1.2.6.2 yamt
714 1.2.6.2 yamt #ifdef AE_DEBUG
715 1.2.6.2 yamt if (ifp->if_flags & IFF_DEBUG) {
716 1.2.6.2 yamt printf(" txsoft %p transmit chain:\n", txs);
717 1.2.6.2 yamt for (seg = sc->sc_txnext;; seg = AE_NEXTTX(seg)) {
718 1.2.6.2 yamt printf(" descriptor %d:\n", seg);
719 1.2.6.2 yamt printf(" ad_status: 0x%08x\n",
720 1.2.6.2 yamt sc->sc_txdescs[seg].ad_status);
721 1.2.6.2 yamt printf(" ad_ctl: 0x%08x\n",
722 1.2.6.2 yamt sc->sc_txdescs[seg].ad_ctl);
723 1.2.6.2 yamt printf(" ad_bufaddr1: 0x%08x\n",
724 1.2.6.2 yamt sc->sc_txdescs[seg].ad_bufaddr1);
725 1.2.6.2 yamt printf(" ad_bufaddr2: 0x%08x\n",
726 1.2.6.2 yamt sc->sc_txdescs[seg].ad_bufaddr2);
727 1.2.6.2 yamt if (seg == lasttx)
728 1.2.6.2 yamt break;
729 1.2.6.2 yamt }
730 1.2.6.2 yamt }
731 1.2.6.2 yamt #endif
732 1.2.6.2 yamt
733 1.2.6.2 yamt /* Sync the descriptors we're using. */
734 1.2.6.2 yamt AE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
735 1.2.6.2 yamt BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
736 1.2.6.2 yamt
737 1.2.6.2 yamt /*
738 1.2.6.2 yamt * Store a pointer to the packet so we can free it later,
739 1.2.6.2 yamt * and remember what txdirty will be once the packet is
740 1.2.6.2 yamt * done.
741 1.2.6.2 yamt */
742 1.2.6.2 yamt txs->txs_mbuf = m0;
743 1.2.6.2 yamt txs->txs_firstdesc = sc->sc_txnext;
744 1.2.6.2 yamt txs->txs_lastdesc = lasttx;
745 1.2.6.2 yamt txs->txs_ndescs = dmamap->dm_nsegs;
746 1.2.6.2 yamt
747 1.2.6.2 yamt /* Advance the tx pointer. */
748 1.2.6.2 yamt sc->sc_txfree -= dmamap->dm_nsegs;
749 1.2.6.2 yamt sc->sc_txnext = nexttx;
750 1.2.6.2 yamt
751 1.2.6.2 yamt SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
752 1.2.6.2 yamt SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
753 1.2.6.2 yamt
754 1.2.6.2 yamt last_txs = txs;
755 1.2.6.2 yamt
756 1.2.6.2 yamt #if NBPFILTER > 0
757 1.2.6.2 yamt /*
758 1.2.6.2 yamt * Pass the packet to any BPF listeners.
759 1.2.6.2 yamt */
760 1.2.6.2 yamt if (ifp->if_bpf)
761 1.2.6.2 yamt bpf_mtap(ifp->if_bpf, m0);
762 1.2.6.2 yamt #endif /* NBPFILTER > 0 */
763 1.2.6.2 yamt }
764 1.2.6.2 yamt
765 1.2.6.2 yamt if (txs == NULL || sc->sc_txfree == 0) {
766 1.2.6.2 yamt /* No more slots left; notify upper layer. */
767 1.2.6.2 yamt ifp->if_flags |= IFF_OACTIVE;
768 1.2.6.2 yamt }
769 1.2.6.2 yamt
770 1.2.6.2 yamt if (sc->sc_txfree != ofree) {
771 1.2.6.2 yamt DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
772 1.2.6.2 yamt sc->sc_dev.dv_xname, lasttx, firsttx));
773 1.2.6.2 yamt /*
774 1.2.6.2 yamt * Cause a transmit interrupt to happen on the
775 1.2.6.2 yamt * last packet we enqueued.
776 1.2.6.2 yamt */
777 1.2.6.2 yamt sc->sc_txdescs[lasttx].ad_ctl |= ADCTL_Tx_IC;
778 1.2.6.2 yamt AE_CDTXSYNC(sc, lasttx, 1,
779 1.2.6.2 yamt BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
780 1.2.6.2 yamt
781 1.2.6.2 yamt /*
782 1.2.6.2 yamt * The entire packet chain is set up. Give the
783 1.2.6.2 yamt * first descriptor to the chip now.
784 1.2.6.2 yamt */
785 1.2.6.2 yamt sc->sc_txdescs[firsttx].ad_status |= ADSTAT_OWN;
786 1.2.6.2 yamt AE_CDTXSYNC(sc, firsttx, 1,
787 1.2.6.2 yamt BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
788 1.2.6.2 yamt
789 1.2.6.2 yamt /* Wake up the transmitter. */
790 1.2.6.2 yamt /* XXX USE AUTOPOLLING? */
791 1.2.6.2 yamt AE_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
792 1.2.6.2 yamt AE_BARRIER(sc);
793 1.2.6.2 yamt
794 1.2.6.2 yamt /* Set a watchdog timer in case the chip flakes out. */
795 1.2.6.2 yamt ifp->if_timer = 5;
796 1.2.6.2 yamt }
797 1.2.6.2 yamt }
798 1.2.6.2 yamt
799 1.2.6.2 yamt /*
800 1.2.6.2 yamt * ae_watchdog: [ifnet interface function]
801 1.2.6.2 yamt *
802 1.2.6.2 yamt * Watchdog timer handler.
803 1.2.6.2 yamt */
804 1.2.6.2 yamt static void
805 1.2.6.2 yamt ae_watchdog(struct ifnet *ifp)
806 1.2.6.2 yamt {
807 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
808 1.2.6.2 yamt int doing_transmit;
809 1.2.6.2 yamt
810 1.2.6.2 yamt doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq));
811 1.2.6.2 yamt
812 1.2.6.2 yamt if (doing_transmit) {
813 1.2.6.2 yamt printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
814 1.2.6.2 yamt ifp->if_oerrors++;
815 1.2.6.2 yamt }
816 1.2.6.2 yamt else
817 1.2.6.2 yamt printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
818 1.2.6.2 yamt
819 1.2.6.2 yamt (void) ae_init(ifp);
820 1.2.6.2 yamt
821 1.2.6.2 yamt /* Try to get more packets going. */
822 1.2.6.2 yamt ae_start(ifp);
823 1.2.6.2 yamt }
824 1.2.6.2 yamt
825 1.2.6.2 yamt /*
826 1.2.6.2 yamt * ae_ioctl: [ifnet interface function]
827 1.2.6.2 yamt *
828 1.2.6.2 yamt * Handle control requests from the operator.
829 1.2.6.2 yamt */
830 1.2.6.2 yamt static int
831 1.2.6.2 yamt ae_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
832 1.2.6.2 yamt {
833 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
834 1.2.6.2 yamt struct ifreq *ifr = (struct ifreq *)data;
835 1.2.6.2 yamt int s, error;
836 1.2.6.2 yamt
837 1.2.6.2 yamt s = splnet();
838 1.2.6.2 yamt
839 1.2.6.2 yamt switch (cmd) {
840 1.2.6.2 yamt case SIOCSIFMEDIA:
841 1.2.6.2 yamt case SIOCGIFMEDIA:
842 1.2.6.2 yamt error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
843 1.2.6.2 yamt break;
844 1.2.6.2 yamt case SIOCSIFFLAGS:
845 1.2.6.2 yamt /* If the interface is up and running, only modify the receive
846 1.2.6.2 yamt * filter when setting promiscuous or debug mode. Otherwise
847 1.2.6.2 yamt * fall through to ether_ioctl, which will reset the chip.
848 1.2.6.2 yamt */
849 1.2.6.2 yamt #define RESETIGN (IFF_CANTCHANGE|IFF_DEBUG)
850 1.2.6.2 yamt if (((ifp->if_flags & (IFF_UP|IFF_RUNNING))
851 1.2.6.2 yamt == (IFF_UP|IFF_RUNNING))
852 1.2.6.2 yamt && ((ifp->if_flags & (~RESETIGN))
853 1.2.6.2 yamt == (sc->sc_if_flags & (~RESETIGN)))) {
854 1.2.6.2 yamt /* Set up the receive filter. */
855 1.2.6.2 yamt ae_filter_setup(sc);
856 1.2.6.2 yamt error = 0;
857 1.2.6.2 yamt break;
858 1.2.6.2 yamt #undef RESETIGN
859 1.2.6.2 yamt }
860 1.2.6.2 yamt /* FALLTHROUGH */
861 1.2.6.2 yamt default:
862 1.2.6.2 yamt error = ether_ioctl(ifp, cmd, data);
863 1.2.6.2 yamt if (error == ENETRESET) {
864 1.2.6.2 yamt if (ifp->if_flags & IFF_RUNNING) {
865 1.2.6.2 yamt /*
866 1.2.6.2 yamt * Multicast list has changed. Set the
867 1.2.6.2 yamt * hardware filter accordingly.
868 1.2.6.2 yamt */
869 1.2.6.2 yamt ae_filter_setup(sc);
870 1.2.6.2 yamt }
871 1.2.6.2 yamt error = 0;
872 1.2.6.2 yamt }
873 1.2.6.2 yamt break;
874 1.2.6.2 yamt }
875 1.2.6.2 yamt
876 1.2.6.2 yamt /* Try to get more packets going. */
877 1.2.6.2 yamt if (AE_IS_ENABLED(sc))
878 1.2.6.2 yamt ae_start(ifp);
879 1.2.6.2 yamt
880 1.2.6.2 yamt sc->sc_if_flags = ifp->if_flags;
881 1.2.6.2 yamt splx(s);
882 1.2.6.2 yamt return (error);
883 1.2.6.2 yamt }
884 1.2.6.2 yamt
885 1.2.6.2 yamt /*
886 1.2.6.2 yamt * ae_intr:
887 1.2.6.2 yamt *
888 1.2.6.2 yamt * Interrupt service routine.
889 1.2.6.2 yamt */
890 1.2.6.2 yamt int
891 1.2.6.2 yamt ae_intr(void *arg)
892 1.2.6.2 yamt {
893 1.2.6.2 yamt struct ae_softc *sc = arg;
894 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
895 1.2.6.2 yamt u_int32_t status, rxstatus, txstatus;
896 1.2.6.2 yamt int handled = 0, txthresh;
897 1.2.6.2 yamt
898 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_intr\n", sc->sc_dev.dv_xname));
899 1.2.6.2 yamt
900 1.2.6.2 yamt #ifdef DEBUG
901 1.2.6.2 yamt if (AE_IS_ENABLED(sc) == 0)
902 1.2.6.2 yamt panic("%s: ae_intr: not enabled", sc->sc_dev.dv_xname);
903 1.2.6.2 yamt #endif
904 1.2.6.2 yamt
905 1.2.6.2 yamt /*
906 1.2.6.2 yamt * If the interface isn't running, the interrupt couldn't
907 1.2.6.2 yamt * possibly have come from us.
908 1.2.6.2 yamt */
909 1.2.6.2 yamt if ((ifp->if_flags & IFF_RUNNING) == 0 ||
910 1.2.6.2 yamt !device_is_active(&sc->sc_dev)) {
911 1.2.6.2 yamt printf("spurious?!?\n");
912 1.2.6.2 yamt return (0);
913 1.2.6.2 yamt }
914 1.2.6.2 yamt
915 1.2.6.2 yamt for (;;) {
916 1.2.6.2 yamt status = AE_READ(sc, CSR_STATUS);
917 1.2.6.2 yamt if (status) {
918 1.2.6.2 yamt AE_WRITE(sc, CSR_STATUS, status);
919 1.2.6.2 yamt AE_BARRIER(sc);
920 1.2.6.2 yamt }
921 1.2.6.2 yamt
922 1.2.6.2 yamt if ((status & sc->sc_inten) == 0)
923 1.2.6.2 yamt break;
924 1.2.6.2 yamt
925 1.2.6.2 yamt handled = 1;
926 1.2.6.2 yamt
927 1.2.6.2 yamt rxstatus = status & sc->sc_rxint_mask;
928 1.2.6.2 yamt txstatus = status & sc->sc_txint_mask;
929 1.2.6.2 yamt
930 1.2.6.2 yamt if (rxstatus) {
931 1.2.6.2 yamt /* Grab new any new packets. */
932 1.2.6.2 yamt ae_rxintr(sc);
933 1.2.6.2 yamt
934 1.2.6.2 yamt if (rxstatus & STATUS_RU) {
935 1.2.6.2 yamt printf("%s: receive ring overrun\n",
936 1.2.6.2 yamt sc->sc_dev.dv_xname);
937 1.2.6.2 yamt /* Get the receive process going again. */
938 1.2.6.2 yamt AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
939 1.2.6.2 yamt AE_BARRIER(sc);
940 1.2.6.2 yamt break;
941 1.2.6.2 yamt }
942 1.2.6.2 yamt }
943 1.2.6.2 yamt
944 1.2.6.2 yamt if (txstatus) {
945 1.2.6.2 yamt /* Sweep up transmit descriptors. */
946 1.2.6.2 yamt ae_txintr(sc);
947 1.2.6.2 yamt
948 1.2.6.2 yamt if (txstatus & STATUS_TJT)
949 1.2.6.2 yamt printf("%s: transmit jabber timeout\n",
950 1.2.6.2 yamt sc->sc_dev.dv_xname);
951 1.2.6.2 yamt
952 1.2.6.2 yamt if (txstatus & STATUS_UNF) {
953 1.2.6.2 yamt /*
954 1.2.6.2 yamt * Increase our transmit threshold if
955 1.2.6.2 yamt * another is available.
956 1.2.6.2 yamt */
957 1.2.6.2 yamt txthresh = sc->sc_txthresh + 1;
958 1.2.6.2 yamt if (ae_txthresh[txthresh].txth_name != NULL) {
959 1.2.6.2 yamt uint32_t opmode;
960 1.2.6.2 yamt /* Idle the transmit process. */
961 1.2.6.2 yamt opmode = AE_READ(sc, CSR_OPMODE);
962 1.2.6.2 yamt ae_idle(sc, OPMODE_ST);
963 1.2.6.2 yamt
964 1.2.6.2 yamt sc->sc_txthresh = txthresh;
965 1.2.6.2 yamt opmode &=
966 1.2.6.2 yamt ~(OPMODE_TR|OPMODE_SF);
967 1.2.6.2 yamt opmode |=
968 1.2.6.2 yamt ae_txthresh[txthresh].txth_opmode;
969 1.2.6.2 yamt printf("%s: transmit underrun; new "
970 1.2.6.2 yamt "threshold: %s\n",
971 1.2.6.2 yamt sc->sc_dev.dv_xname,
972 1.2.6.2 yamt ae_txthresh[txthresh].txth_name);
973 1.2.6.2 yamt
974 1.2.6.2 yamt /*
975 1.2.6.2 yamt * Set the new threshold and restart
976 1.2.6.2 yamt * the transmit process.
977 1.2.6.2 yamt */
978 1.2.6.2 yamt AE_WRITE(sc, CSR_OPMODE, opmode);
979 1.2.6.2 yamt AE_BARRIER(sc);
980 1.2.6.2 yamt }
981 1.2.6.2 yamt /*
982 1.2.6.2 yamt * XXX Log every Nth underrun from
983 1.2.6.2 yamt * XXX now on?
984 1.2.6.2 yamt */
985 1.2.6.2 yamt }
986 1.2.6.2 yamt }
987 1.2.6.2 yamt
988 1.2.6.2 yamt if (status & (STATUS_TPS|STATUS_RPS)) {
989 1.2.6.2 yamt if (status & STATUS_TPS)
990 1.2.6.2 yamt printf("%s: transmit process stopped\n",
991 1.2.6.2 yamt sc->sc_dev.dv_xname);
992 1.2.6.2 yamt if (status & STATUS_RPS)
993 1.2.6.2 yamt printf("%s: receive process stopped\n",
994 1.2.6.2 yamt sc->sc_dev.dv_xname);
995 1.2.6.2 yamt (void) ae_init(ifp);
996 1.2.6.2 yamt break;
997 1.2.6.2 yamt }
998 1.2.6.2 yamt
999 1.2.6.2 yamt if (status & STATUS_SE) {
1000 1.2.6.2 yamt const char *str;
1001 1.2.6.2 yamt
1002 1.2.6.2 yamt if (status & STATUS_TX_ABORT)
1003 1.2.6.2 yamt str = "tx abort";
1004 1.2.6.2 yamt else if (status & STATUS_RX_ABORT)
1005 1.2.6.2 yamt str = "rx abort";
1006 1.2.6.2 yamt else
1007 1.2.6.2 yamt str = "unknown error";
1008 1.2.6.2 yamt
1009 1.2.6.2 yamt printf("%s: fatal system error: %s\n",
1010 1.2.6.2 yamt sc->sc_dev.dv_xname, str);
1011 1.2.6.2 yamt (void) ae_init(ifp);
1012 1.2.6.2 yamt break;
1013 1.2.6.2 yamt }
1014 1.2.6.2 yamt
1015 1.2.6.2 yamt /*
1016 1.2.6.2 yamt * Not handled:
1017 1.2.6.2 yamt *
1018 1.2.6.2 yamt * Transmit buffer unavailable -- normal
1019 1.2.6.2 yamt * condition, nothing to do, really.
1020 1.2.6.2 yamt *
1021 1.2.6.2 yamt * General purpose timer experied -- we don't
1022 1.2.6.2 yamt * use the general purpose timer.
1023 1.2.6.2 yamt *
1024 1.2.6.2 yamt * Early receive interrupt -- not available on
1025 1.2.6.2 yamt * all chips, we just use RI. We also only
1026 1.2.6.2 yamt * use single-segment receive DMA, so this
1027 1.2.6.2 yamt * is mostly useless.
1028 1.2.6.2 yamt */
1029 1.2.6.2 yamt }
1030 1.2.6.2 yamt
1031 1.2.6.2 yamt /* Try to get more packets going. */
1032 1.2.6.2 yamt ae_start(ifp);
1033 1.2.6.2 yamt
1034 1.2.6.2 yamt #if NRND > 0
1035 1.2.6.2 yamt if (handled)
1036 1.2.6.2 yamt rnd_add_uint32(&sc->sc_rnd_source, status);
1037 1.2.6.2 yamt #endif
1038 1.2.6.2 yamt return (handled);
1039 1.2.6.2 yamt }
1040 1.2.6.2 yamt
1041 1.2.6.2 yamt /*
1042 1.2.6.2 yamt * ae_rxintr:
1043 1.2.6.2 yamt *
1044 1.2.6.2 yamt * Helper; handle receive interrupts.
1045 1.2.6.2 yamt */
1046 1.2.6.2 yamt static void
1047 1.2.6.2 yamt ae_rxintr(struct ae_softc *sc)
1048 1.2.6.2 yamt {
1049 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1050 1.2.6.2 yamt struct ether_header *eh;
1051 1.2.6.2 yamt struct ae_rxsoft *rxs;
1052 1.2.6.2 yamt struct mbuf *m;
1053 1.2.6.2 yamt u_int32_t rxstat;
1054 1.2.6.2 yamt int i, len;
1055 1.2.6.2 yamt
1056 1.2.6.2 yamt for (i = sc->sc_rxptr;; i = AE_NEXTRX(i)) {
1057 1.2.6.2 yamt rxs = &sc->sc_rxsoft[i];
1058 1.2.6.2 yamt
1059 1.2.6.2 yamt AE_CDRXSYNC(sc, i,
1060 1.2.6.2 yamt BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1061 1.2.6.2 yamt
1062 1.2.6.2 yamt rxstat = sc->sc_rxdescs[i].ad_status;
1063 1.2.6.2 yamt
1064 1.2.6.2 yamt if (rxstat & ADSTAT_OWN) {
1065 1.2.6.2 yamt /*
1066 1.2.6.2 yamt * We have processed all of the receive buffers.
1067 1.2.6.2 yamt */
1068 1.2.6.2 yamt break;
1069 1.2.6.2 yamt }
1070 1.2.6.2 yamt
1071 1.2.6.2 yamt /*
1072 1.2.6.2 yamt * If any collisions were seen on the wire, count one.
1073 1.2.6.2 yamt */
1074 1.2.6.2 yamt if (rxstat & ADSTAT_Rx_CS)
1075 1.2.6.2 yamt ifp->if_collisions++;
1076 1.2.6.2 yamt
1077 1.2.6.2 yamt /*
1078 1.2.6.2 yamt * If an error occurred, update stats, clear the status
1079 1.2.6.2 yamt * word, and leave the packet buffer in place. It will
1080 1.2.6.2 yamt * simply be reused the next time the ring comes around.
1081 1.2.6.2 yamt * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
1082 1.2.6.2 yamt * error.
1083 1.2.6.2 yamt */
1084 1.2.6.2 yamt if (rxstat & ADSTAT_ES &&
1085 1.2.6.2 yamt ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
1086 1.2.6.2 yamt (rxstat & (ADSTAT_Rx_DE | ADSTAT_Rx_RF |
1087 1.2.6.2 yamt ADSTAT_Rx_DB | ADSTAT_Rx_CE)) != 0)) {
1088 1.2.6.2 yamt #define PRINTERR(bit, str) \
1089 1.2.6.2 yamt if (rxstat & (bit)) \
1090 1.2.6.2 yamt printf("%s: receive error: %s\n", \
1091 1.2.6.2 yamt sc->sc_dev.dv_xname, str)
1092 1.2.6.2 yamt ifp->if_ierrors++;
1093 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_DE, "descriptor error");
1094 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_RF, "runt frame");
1095 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_TL, "frame too long");
1096 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_RE, "MII error");
1097 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_DB, "dribbling bit");
1098 1.2.6.2 yamt PRINTERR(ADSTAT_Rx_CE, "CRC error");
1099 1.2.6.2 yamt #undef PRINTERR
1100 1.2.6.2 yamt AE_INIT_RXDESC(sc, i);
1101 1.2.6.2 yamt continue;
1102 1.2.6.2 yamt }
1103 1.2.6.2 yamt
1104 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1105 1.2.6.2 yamt rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1106 1.2.6.2 yamt
1107 1.2.6.2 yamt /*
1108 1.2.6.2 yamt * No errors; receive the packet. Note the chip
1109 1.2.6.2 yamt * includes the CRC with every packet.
1110 1.2.6.2 yamt */
1111 1.2.6.2 yamt len = ADSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;
1112 1.2.6.2 yamt
1113 1.2.6.2 yamt /*
1114 1.2.6.2 yamt * XXX: the Atheros part can align on half words. what
1115 1.2.6.2 yamt * is the performance implication of this? Probably
1116 1.2.6.2 yamt * minimal, and we should use it...
1117 1.2.6.2 yamt */
1118 1.2.6.2 yamt #ifdef __NO_STRICT_ALIGNMENT
1119 1.2.6.2 yamt /*
1120 1.2.6.2 yamt * Allocate a new mbuf cluster. If that fails, we are
1121 1.2.6.2 yamt * out of memory, and must drop the packet and recycle
1122 1.2.6.2 yamt * the buffer that's already attached to this descriptor.
1123 1.2.6.2 yamt */
1124 1.2.6.2 yamt m = rxs->rxs_mbuf;
1125 1.2.6.2 yamt if (ae_add_rxbuf(sc, i) != 0) {
1126 1.2.6.2 yamt ifp->if_ierrors++;
1127 1.2.6.2 yamt AE_INIT_RXDESC(sc, i);
1128 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1129 1.2.6.2 yamt rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1130 1.2.6.2 yamt continue;
1131 1.2.6.2 yamt }
1132 1.2.6.2 yamt #else
1133 1.2.6.2 yamt /*
1134 1.2.6.2 yamt * The chip's receive buffers must be 4-byte aligned.
1135 1.2.6.2 yamt * But this means that the data after the Ethernet header
1136 1.2.6.2 yamt * is misaligned. We must allocate a new buffer and
1137 1.2.6.2 yamt * copy the data, shifted forward 2 bytes.
1138 1.2.6.2 yamt */
1139 1.2.6.2 yamt MGETHDR(m, M_DONTWAIT, MT_DATA);
1140 1.2.6.2 yamt if (m == NULL) {
1141 1.2.6.2 yamt dropit:
1142 1.2.6.2 yamt ifp->if_ierrors++;
1143 1.2.6.2 yamt AE_INIT_RXDESC(sc, i);
1144 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1145 1.2.6.2 yamt rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1146 1.2.6.2 yamt continue;
1147 1.2.6.2 yamt }
1148 1.2.6.2 yamt MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1149 1.2.6.2 yamt if (len > (MHLEN - 2)) {
1150 1.2.6.2 yamt MCLGET(m, M_DONTWAIT);
1151 1.2.6.2 yamt if ((m->m_flags & M_EXT) == 0) {
1152 1.2.6.2 yamt m_freem(m);
1153 1.2.6.2 yamt goto dropit;
1154 1.2.6.2 yamt }
1155 1.2.6.2 yamt }
1156 1.2.6.2 yamt m->m_data += 2;
1157 1.2.6.2 yamt
1158 1.2.6.2 yamt /*
1159 1.2.6.2 yamt * Note that we use clusters for incoming frames, so the
1160 1.2.6.2 yamt * buffer is virtually contiguous.
1161 1.2.6.2 yamt */
1162 1.2.6.2 yamt memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1163 1.2.6.2 yamt
1164 1.2.6.2 yamt /* Allow the receive descriptor to continue using its mbuf. */
1165 1.2.6.2 yamt AE_INIT_RXDESC(sc, i);
1166 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1167 1.2.6.2 yamt rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1168 1.2.6.2 yamt #endif /* __NO_STRICT_ALIGNMENT */
1169 1.2.6.2 yamt
1170 1.2.6.2 yamt ifp->if_ipackets++;
1171 1.2.6.2 yamt eh = mtod(m, struct ether_header *);
1172 1.2.6.2 yamt m->m_pkthdr.rcvif = ifp;
1173 1.2.6.2 yamt m->m_pkthdr.len = m->m_len = len;
1174 1.2.6.2 yamt
1175 1.2.6.2 yamt #if NBPFILTER > 0
1176 1.2.6.2 yamt /*
1177 1.2.6.2 yamt * Pass this up to any BPF listeners, but only
1178 1.2.6.2 yamt * pass it up the stack if its for us.
1179 1.2.6.2 yamt */
1180 1.2.6.2 yamt if (ifp->if_bpf)
1181 1.2.6.2 yamt bpf_mtap(ifp->if_bpf, m);
1182 1.2.6.2 yamt #endif /* NPBFILTER > 0 */
1183 1.2.6.2 yamt
1184 1.2.6.2 yamt /* Pass it on. */
1185 1.2.6.2 yamt (*ifp->if_input)(ifp, m);
1186 1.2.6.2 yamt }
1187 1.2.6.2 yamt
1188 1.2.6.2 yamt /* Update the receive pointer. */
1189 1.2.6.2 yamt sc->sc_rxptr = i;
1190 1.2.6.2 yamt }
1191 1.2.6.2 yamt
1192 1.2.6.2 yamt /*
1193 1.2.6.2 yamt * ae_txintr:
1194 1.2.6.2 yamt *
1195 1.2.6.2 yamt * Helper; handle transmit interrupts.
1196 1.2.6.2 yamt */
1197 1.2.6.2 yamt static void
1198 1.2.6.2 yamt ae_txintr(struct ae_softc *sc)
1199 1.2.6.2 yamt {
1200 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1201 1.2.6.2 yamt struct ae_txsoft *txs;
1202 1.2.6.2 yamt u_int32_t txstat;
1203 1.2.6.2 yamt
1204 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_txintr: sc_flags 0x%08x\n",
1205 1.2.6.2 yamt sc->sc_dev.dv_xname, sc->sc_flags));
1206 1.2.6.2 yamt
1207 1.2.6.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
1208 1.2.6.2 yamt
1209 1.2.6.2 yamt /*
1210 1.2.6.2 yamt * Go through our Tx list and free mbufs for those
1211 1.2.6.2 yamt * frames that have been transmitted.
1212 1.2.6.2 yamt */
1213 1.2.6.2 yamt while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1214 1.2.6.2 yamt AE_CDTXSYNC(sc, txs->txs_lastdesc,
1215 1.2.6.2 yamt txs->txs_ndescs,
1216 1.2.6.2 yamt BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1217 1.2.6.2 yamt
1218 1.2.6.2 yamt #ifdef AE_DEBUG
1219 1.2.6.2 yamt if (ifp->if_flags & IFF_DEBUG) {
1220 1.2.6.2 yamt int i;
1221 1.2.6.2 yamt printf(" txsoft %p transmit chain:\n", txs);
1222 1.2.6.2 yamt for (i = txs->txs_firstdesc;; i = AE_NEXTTX(i)) {
1223 1.2.6.2 yamt printf(" descriptor %d:\n", i);
1224 1.2.6.2 yamt printf(" ad_status: 0x%08x\n",
1225 1.2.6.2 yamt sc->sc_txdescs[i].ad_status);
1226 1.2.6.2 yamt printf(" ad_ctl: 0x%08x\n",
1227 1.2.6.2 yamt sc->sc_txdescs[i].ad_ctl);
1228 1.2.6.2 yamt printf(" ad_bufaddr1: 0x%08x\n",
1229 1.2.6.2 yamt sc->sc_txdescs[i].ad_bufaddr1);
1230 1.2.6.2 yamt printf(" ad_bufaddr2: 0x%08x\n",
1231 1.2.6.2 yamt sc->sc_txdescs[i].ad_bufaddr2);
1232 1.2.6.2 yamt if (i == txs->txs_lastdesc)
1233 1.2.6.2 yamt break;
1234 1.2.6.2 yamt }
1235 1.2.6.2 yamt }
1236 1.2.6.2 yamt #endif
1237 1.2.6.2 yamt
1238 1.2.6.2 yamt txstat = sc->sc_txdescs[txs->txs_lastdesc].ad_status;
1239 1.2.6.2 yamt if (txstat & ADSTAT_OWN)
1240 1.2.6.2 yamt break;
1241 1.2.6.2 yamt
1242 1.2.6.2 yamt SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1243 1.2.6.2 yamt
1244 1.2.6.2 yamt sc->sc_txfree += txs->txs_ndescs;
1245 1.2.6.2 yamt
1246 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1247 1.2.6.2 yamt 0, txs->txs_dmamap->dm_mapsize,
1248 1.2.6.2 yamt BUS_DMASYNC_POSTWRITE);
1249 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1250 1.2.6.2 yamt m_freem(txs->txs_mbuf);
1251 1.2.6.2 yamt txs->txs_mbuf = NULL;
1252 1.2.6.2 yamt
1253 1.2.6.2 yamt SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1254 1.2.6.2 yamt
1255 1.2.6.2 yamt /*
1256 1.2.6.2 yamt * Check for errors and collisions.
1257 1.2.6.2 yamt */
1258 1.2.6.2 yamt #ifdef AE_STATS
1259 1.2.6.2 yamt if (txstat & ADSTAT_Tx_UF)
1260 1.2.6.2 yamt sc->sc_stats.ts_tx_uf++;
1261 1.2.6.2 yamt if (txstat & ADSTAT_Tx_TO)
1262 1.2.6.2 yamt sc->sc_stats.ts_tx_to++;
1263 1.2.6.2 yamt if (txstat & ADSTAT_Tx_EC)
1264 1.2.6.2 yamt sc->sc_stats.ts_tx_ec++;
1265 1.2.6.2 yamt if (txstat & ADSTAT_Tx_LC)
1266 1.2.6.2 yamt sc->sc_stats.ts_tx_lc++;
1267 1.2.6.2 yamt #endif
1268 1.2.6.2 yamt
1269 1.2.6.2 yamt if (txstat & (ADSTAT_Tx_UF|ADSTAT_Tx_TO))
1270 1.2.6.2 yamt ifp->if_oerrors++;
1271 1.2.6.2 yamt
1272 1.2.6.2 yamt if (txstat & ADSTAT_Tx_EC)
1273 1.2.6.2 yamt ifp->if_collisions += 16;
1274 1.2.6.2 yamt else
1275 1.2.6.2 yamt ifp->if_collisions += ADSTAT_Tx_COLLISIONS(txstat);
1276 1.2.6.2 yamt if (txstat & ADSTAT_Tx_LC)
1277 1.2.6.2 yamt ifp->if_collisions++;
1278 1.2.6.2 yamt
1279 1.2.6.2 yamt ifp->if_opackets++;
1280 1.2.6.2 yamt }
1281 1.2.6.2 yamt
1282 1.2.6.2 yamt /*
1283 1.2.6.2 yamt * If there are no more pending transmissions, cancel the watchdog
1284 1.2.6.2 yamt * timer.
1285 1.2.6.2 yamt */
1286 1.2.6.2 yamt if (txs == NULL)
1287 1.2.6.2 yamt ifp->if_timer = 0;
1288 1.2.6.2 yamt }
1289 1.2.6.2 yamt
1290 1.2.6.2 yamt #ifdef AE_STATS
1291 1.2.6.2 yamt void
1292 1.2.6.2 yamt ae_print_stats(struct ae_softc *sc)
1293 1.2.6.2 yamt {
1294 1.2.6.2 yamt
1295 1.2.6.2 yamt printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
1296 1.2.6.2 yamt sc->sc_dev.dv_xname,
1297 1.2.6.2 yamt sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
1298 1.2.6.2 yamt sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
1299 1.2.6.2 yamt }
1300 1.2.6.2 yamt #endif
1301 1.2.6.2 yamt
1302 1.2.6.2 yamt /*
1303 1.2.6.2 yamt * ae_reset:
1304 1.2.6.2 yamt *
1305 1.2.6.2 yamt * Perform a soft reset on the chip.
1306 1.2.6.2 yamt */
1307 1.2.6.2 yamt void
1308 1.2.6.2 yamt ae_reset(struct ae_softc *sc)
1309 1.2.6.2 yamt {
1310 1.2.6.2 yamt int i;
1311 1.2.6.2 yamt
1312 1.2.6.2 yamt AE_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
1313 1.2.6.2 yamt AE_BARRIER(sc);
1314 1.2.6.2 yamt
1315 1.2.6.2 yamt /*
1316 1.2.6.2 yamt * The chip doesn't take itself out of reset automatically.
1317 1.2.6.2 yamt * We need to do so after 2us.
1318 1.2.6.2 yamt */
1319 1.2.6.2 yamt delay(10);
1320 1.2.6.2 yamt AE_WRITE(sc, CSR_BUSMODE, 0);
1321 1.2.6.2 yamt AE_BARRIER(sc);
1322 1.2.6.2 yamt
1323 1.2.6.2 yamt for (i = 0; i < 1000; i++) {
1324 1.2.6.2 yamt /*
1325 1.2.6.2 yamt * Wait a bit for the reset to complete before peeking
1326 1.2.6.2 yamt * at the chip again.
1327 1.2.6.2 yamt */
1328 1.2.6.2 yamt delay(10);
1329 1.2.6.2 yamt if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
1330 1.2.6.2 yamt break;
1331 1.2.6.2 yamt }
1332 1.2.6.2 yamt
1333 1.2.6.2 yamt if (AE_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
1334 1.2.6.2 yamt printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1335 1.2.6.2 yamt
1336 1.2.6.2 yamt delay(1000);
1337 1.2.6.2 yamt }
1338 1.2.6.2 yamt
1339 1.2.6.2 yamt /*
1340 1.2.6.2 yamt * ae_init: [ ifnet interface function ]
1341 1.2.6.2 yamt *
1342 1.2.6.2 yamt * Initialize the interface. Must be called at splnet().
1343 1.2.6.2 yamt */
1344 1.2.6.2 yamt static int
1345 1.2.6.2 yamt ae_init(struct ifnet *ifp)
1346 1.2.6.2 yamt {
1347 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
1348 1.2.6.2 yamt struct ae_txsoft *txs;
1349 1.2.6.2 yamt struct ae_rxsoft *rxs;
1350 1.2.6.2 yamt uint8_t *enaddr;
1351 1.2.6.2 yamt int i, error = 0;
1352 1.2.6.2 yamt
1353 1.2.6.2 yamt if ((error = ae_enable(sc)) != 0)
1354 1.2.6.2 yamt goto out;
1355 1.2.6.2 yamt
1356 1.2.6.2 yamt /*
1357 1.2.6.2 yamt * Cancel any pending I/O.
1358 1.2.6.2 yamt */
1359 1.2.6.2 yamt ae_stop(ifp, 0);
1360 1.2.6.2 yamt
1361 1.2.6.2 yamt /*
1362 1.2.6.2 yamt * Reset the chip to a known state.
1363 1.2.6.2 yamt */
1364 1.2.6.2 yamt ae_reset(sc);
1365 1.2.6.2 yamt
1366 1.2.6.2 yamt /*
1367 1.2.6.2 yamt * Initialize the BUSMODE register.
1368 1.2.6.2 yamt */
1369 1.2.6.2 yamt AE_WRITE(sc, CSR_BUSMODE,
1370 1.2.6.2 yamt /* XXX: not sure if this is a good thing or not... */
1371 1.2.6.2 yamt //BUSMODE_ALIGN_16B |
1372 1.2.6.2 yamt BUSMODE_BAR | BUSMODE_BLE | BUSMODE_PBL_4LW);
1373 1.2.6.2 yamt AE_BARRIER(sc);
1374 1.2.6.2 yamt
1375 1.2.6.2 yamt /*
1376 1.2.6.2 yamt * Initialize the transmit descriptor ring.
1377 1.2.6.2 yamt */
1378 1.2.6.2 yamt memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1379 1.2.6.2 yamt for (i = 0; i < AE_NTXDESC; i++) {
1380 1.2.6.2 yamt sc->sc_txdescs[i].ad_ctl = 0;
1381 1.2.6.2 yamt sc->sc_txdescs[i].ad_bufaddr2 =
1382 1.2.6.2 yamt AE_CDTXADDR(sc, AE_NEXTTX(i));
1383 1.2.6.2 yamt }
1384 1.2.6.2 yamt sc->sc_txdescs[AE_NTXDESC - 1].ad_ctl |= ADCTL_ER;
1385 1.2.6.2 yamt AE_CDTXSYNC(sc, 0, AE_NTXDESC,
1386 1.2.6.2 yamt BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1387 1.2.6.2 yamt sc->sc_txfree = AE_NTXDESC;
1388 1.2.6.2 yamt sc->sc_txnext = 0;
1389 1.2.6.2 yamt
1390 1.2.6.2 yamt /*
1391 1.2.6.2 yamt * Initialize the transmit job descriptors.
1392 1.2.6.2 yamt */
1393 1.2.6.2 yamt SIMPLEQ_INIT(&sc->sc_txfreeq);
1394 1.2.6.2 yamt SIMPLEQ_INIT(&sc->sc_txdirtyq);
1395 1.2.6.2 yamt for (i = 0; i < AE_TXQUEUELEN; i++) {
1396 1.2.6.2 yamt txs = &sc->sc_txsoft[i];
1397 1.2.6.2 yamt txs->txs_mbuf = NULL;
1398 1.2.6.2 yamt SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1399 1.2.6.2 yamt }
1400 1.2.6.2 yamt
1401 1.2.6.2 yamt /*
1402 1.2.6.2 yamt * Initialize the receive descriptor and receive job
1403 1.2.6.2 yamt * descriptor rings.
1404 1.2.6.2 yamt */
1405 1.2.6.2 yamt for (i = 0; i < AE_NRXDESC; i++) {
1406 1.2.6.2 yamt rxs = &sc->sc_rxsoft[i];
1407 1.2.6.2 yamt if (rxs->rxs_mbuf == NULL) {
1408 1.2.6.2 yamt if ((error = ae_add_rxbuf(sc, i)) != 0) {
1409 1.2.6.2 yamt printf("%s: unable to allocate or map rx "
1410 1.2.6.2 yamt "buffer %d, error = %d\n",
1411 1.2.6.2 yamt sc->sc_dev.dv_xname, i, error);
1412 1.2.6.2 yamt /*
1413 1.2.6.2 yamt * XXX Should attempt to run with fewer receive
1414 1.2.6.2 yamt * XXX buffers instead of just failing.
1415 1.2.6.2 yamt */
1416 1.2.6.2 yamt ae_rxdrain(sc);
1417 1.2.6.2 yamt goto out;
1418 1.2.6.2 yamt }
1419 1.2.6.2 yamt } else
1420 1.2.6.2 yamt AE_INIT_RXDESC(sc, i);
1421 1.2.6.2 yamt }
1422 1.2.6.2 yamt sc->sc_rxptr = 0;
1423 1.2.6.2 yamt
1424 1.2.6.2 yamt /*
1425 1.2.6.2 yamt * Initialize the interrupt mask and enable interrupts.
1426 1.2.6.2 yamt */
1427 1.2.6.2 yamt /* normal interrupts */
1428 1.2.6.2 yamt sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
1429 1.2.6.2 yamt
1430 1.2.6.2 yamt /* abnormal interrupts */
1431 1.2.6.2 yamt sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
1432 1.2.6.2 yamt STATUS_RU | STATUS_RPS | STATUS_SE | STATUS_AIS;
1433 1.2.6.2 yamt
1434 1.2.6.2 yamt sc->sc_rxint_mask = STATUS_RI|STATUS_RU;
1435 1.2.6.2 yamt sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
1436 1.2.6.2 yamt
1437 1.2.6.2 yamt sc->sc_rxint_mask &= sc->sc_inten;
1438 1.2.6.2 yamt sc->sc_txint_mask &= sc->sc_inten;
1439 1.2.6.2 yamt
1440 1.2.6.2 yamt AE_WRITE(sc, CSR_INTEN, sc->sc_inten);
1441 1.2.6.2 yamt AE_WRITE(sc, CSR_STATUS, 0xffffffff);
1442 1.2.6.2 yamt
1443 1.2.6.2 yamt /*
1444 1.2.6.2 yamt * Give the transmit and receive rings to the chip.
1445 1.2.6.2 yamt */
1446 1.2.6.2 yamt AE_WRITE(sc, CSR_TXLIST, AE_CDTXADDR(sc, sc->sc_txnext));
1447 1.2.6.2 yamt AE_WRITE(sc, CSR_RXLIST, AE_CDRXADDR(sc, sc->sc_rxptr));
1448 1.2.6.2 yamt AE_BARRIER(sc);
1449 1.2.6.2 yamt
1450 1.2.6.2 yamt /*
1451 1.2.6.2 yamt * Set the station address.
1452 1.2.6.2 yamt */
1453 1.2.6.2 yamt enaddr = LLADDR(ifp->if_sadl);
1454 1.2.6.2 yamt AE_WRITE(sc, CSR_MACHI, enaddr[5] << 16 | enaddr[4]);
1455 1.2.6.2 yamt AE_WRITE(sc, CSR_MACLO, enaddr[3] << 24 | enaddr[2] << 16 |
1456 1.2.6.2 yamt enaddr[1] << 8 | enaddr[0]);
1457 1.2.6.2 yamt AE_BARRIER(sc);
1458 1.2.6.2 yamt
1459 1.2.6.2 yamt /*
1460 1.2.6.2 yamt * Set the receive filter. This will start the transmit and
1461 1.2.6.2 yamt * receive processes.
1462 1.2.6.2 yamt */
1463 1.2.6.2 yamt ae_filter_setup(sc);
1464 1.2.6.2 yamt
1465 1.2.6.2 yamt /*
1466 1.2.6.2 yamt * Set the current media.
1467 1.2.6.2 yamt */
1468 1.2.6.2 yamt ae_mediachange(ifp);
1469 1.2.6.2 yamt
1470 1.2.6.2 yamt /*
1471 1.2.6.2 yamt * Start the mac.
1472 1.2.6.2 yamt */
1473 1.2.6.2 yamt AE_SET(sc, CSR_MACCTL, MACCTL_RE | MACCTL_TE);
1474 1.2.6.2 yamt AE_BARRIER(sc);
1475 1.2.6.2 yamt
1476 1.2.6.2 yamt /*
1477 1.2.6.2 yamt * Write out the opmode.
1478 1.2.6.2 yamt */
1479 1.2.6.2 yamt AE_WRITE(sc, CSR_OPMODE, OPMODE_SR | OPMODE_ST |
1480 1.2.6.2 yamt ae_txthresh[sc->sc_txthresh].txth_opmode);
1481 1.2.6.2 yamt /*
1482 1.2.6.2 yamt * Start the receive process.
1483 1.2.6.2 yamt */
1484 1.2.6.2 yamt AE_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
1485 1.2.6.2 yamt AE_BARRIER(sc);
1486 1.2.6.2 yamt
1487 1.2.6.2 yamt if (sc->sc_tick != NULL) {
1488 1.2.6.2 yamt /* Start the one second clock. */
1489 1.2.6.2 yamt callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
1490 1.2.6.2 yamt }
1491 1.2.6.2 yamt
1492 1.2.6.2 yamt /*
1493 1.2.6.2 yamt * Note that the interface is now running.
1494 1.2.6.2 yamt */
1495 1.2.6.2 yamt ifp->if_flags |= IFF_RUNNING;
1496 1.2.6.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
1497 1.2.6.2 yamt sc->sc_if_flags = ifp->if_flags;
1498 1.2.6.2 yamt
1499 1.2.6.2 yamt out:
1500 1.2.6.2 yamt if (error) {
1501 1.2.6.2 yamt ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1502 1.2.6.2 yamt ifp->if_timer = 0;
1503 1.2.6.2 yamt printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1504 1.2.6.2 yamt }
1505 1.2.6.2 yamt return (error);
1506 1.2.6.2 yamt }
1507 1.2.6.2 yamt
1508 1.2.6.2 yamt /*
1509 1.2.6.2 yamt * ae_enable:
1510 1.2.6.2 yamt *
1511 1.2.6.2 yamt * Enable the chip.
1512 1.2.6.2 yamt */
1513 1.2.6.2 yamt static int
1514 1.2.6.2 yamt ae_enable(struct ae_softc *sc)
1515 1.2.6.2 yamt {
1516 1.2.6.2 yamt
1517 1.2.6.2 yamt if (AE_IS_ENABLED(sc) == 0) {
1518 1.2.6.3 yamt sc->sc_ih = arbus_intr_establish(sc->sc_cirq, sc->sc_mirq,
1519 1.2.6.3 yamt ae_intr, sc);
1520 1.2.6.2 yamt if (sc->sc_ih == NULL) {
1521 1.2.6.2 yamt printf("%s: unable to establish interrupt\n",
1522 1.2.6.2 yamt sc->sc_dev.dv_xname);
1523 1.2.6.2 yamt return (EIO);
1524 1.2.6.2 yamt }
1525 1.2.6.2 yamt sc->sc_flags |= AE_ENABLED;
1526 1.2.6.2 yamt }
1527 1.2.6.2 yamt return (0);
1528 1.2.6.2 yamt }
1529 1.2.6.2 yamt
1530 1.2.6.2 yamt /*
1531 1.2.6.2 yamt * ae_disable:
1532 1.2.6.2 yamt *
1533 1.2.6.2 yamt * Disable the chip.
1534 1.2.6.2 yamt */
1535 1.2.6.2 yamt static void
1536 1.2.6.2 yamt ae_disable(struct ae_softc *sc)
1537 1.2.6.2 yamt {
1538 1.2.6.2 yamt
1539 1.2.6.2 yamt if (AE_IS_ENABLED(sc)) {
1540 1.2.6.2 yamt arbus_intr_disestablish(sc->sc_ih);
1541 1.2.6.2 yamt sc->sc_flags &= ~AE_ENABLED;
1542 1.2.6.2 yamt }
1543 1.2.6.2 yamt }
1544 1.2.6.2 yamt
1545 1.2.6.2 yamt /*
1546 1.2.6.2 yamt * ae_power:
1547 1.2.6.2 yamt *
1548 1.2.6.2 yamt * Power management (suspend/resume) hook.
1549 1.2.6.2 yamt */
1550 1.2.6.2 yamt static void
1551 1.2.6.2 yamt ae_power(int why, void *arg)
1552 1.2.6.2 yamt {
1553 1.2.6.2 yamt struct ae_softc *sc = arg;
1554 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1555 1.2.6.2 yamt int s;
1556 1.2.6.2 yamt
1557 1.2.6.2 yamt printf("power called: %d, %x\n", why, (uint32_t)arg);
1558 1.2.6.2 yamt s = splnet();
1559 1.2.6.2 yamt switch (why) {
1560 1.2.6.2 yamt case PWR_STANDBY:
1561 1.2.6.2 yamt /* do nothing! */
1562 1.2.6.2 yamt break;
1563 1.2.6.2 yamt case PWR_SUSPEND:
1564 1.2.6.2 yamt ae_stop(ifp, 0);
1565 1.2.6.2 yamt ae_disable(sc);
1566 1.2.6.2 yamt break;
1567 1.2.6.2 yamt case PWR_RESUME:
1568 1.2.6.2 yamt if (ifp->if_flags & IFF_UP) {
1569 1.2.6.2 yamt ae_enable(sc);
1570 1.2.6.2 yamt ae_init(ifp);
1571 1.2.6.2 yamt }
1572 1.2.6.2 yamt break;
1573 1.2.6.2 yamt case PWR_SOFTSUSPEND:
1574 1.2.6.2 yamt case PWR_SOFTSTANDBY:
1575 1.2.6.2 yamt case PWR_SOFTRESUME:
1576 1.2.6.2 yamt break;
1577 1.2.6.2 yamt }
1578 1.2.6.2 yamt splx(s);
1579 1.2.6.2 yamt }
1580 1.2.6.2 yamt
1581 1.2.6.2 yamt /*
1582 1.2.6.2 yamt * ae_rxdrain:
1583 1.2.6.2 yamt *
1584 1.2.6.2 yamt * Drain the receive queue.
1585 1.2.6.2 yamt */
1586 1.2.6.2 yamt static void
1587 1.2.6.2 yamt ae_rxdrain(struct ae_softc *sc)
1588 1.2.6.2 yamt {
1589 1.2.6.2 yamt struct ae_rxsoft *rxs;
1590 1.2.6.2 yamt int i;
1591 1.2.6.2 yamt
1592 1.2.6.2 yamt for (i = 0; i < AE_NRXDESC; i++) {
1593 1.2.6.2 yamt rxs = &sc->sc_rxsoft[i];
1594 1.2.6.2 yamt if (rxs->rxs_mbuf != NULL) {
1595 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1596 1.2.6.2 yamt m_freem(rxs->rxs_mbuf);
1597 1.2.6.2 yamt rxs->rxs_mbuf = NULL;
1598 1.2.6.2 yamt }
1599 1.2.6.2 yamt }
1600 1.2.6.2 yamt }
1601 1.2.6.2 yamt
1602 1.2.6.2 yamt /*
1603 1.2.6.2 yamt * ae_stop: [ ifnet interface function ]
1604 1.2.6.2 yamt *
1605 1.2.6.2 yamt * Stop transmission on the interface.
1606 1.2.6.2 yamt */
1607 1.2.6.2 yamt static void
1608 1.2.6.2 yamt ae_stop(struct ifnet *ifp, int disable)
1609 1.2.6.2 yamt {
1610 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
1611 1.2.6.2 yamt struct ae_txsoft *txs;
1612 1.2.6.2 yamt
1613 1.2.6.2 yamt if (sc->sc_tick != NULL) {
1614 1.2.6.2 yamt /* Stop the one second clock. */
1615 1.2.6.2 yamt callout_stop(&sc->sc_tick_callout);
1616 1.2.6.2 yamt }
1617 1.2.6.2 yamt
1618 1.2.6.2 yamt /* Down the MII. */
1619 1.2.6.2 yamt mii_down(&sc->sc_mii);
1620 1.2.6.2 yamt
1621 1.2.6.2 yamt /* Disable interrupts. */
1622 1.2.6.2 yamt AE_WRITE(sc, CSR_INTEN, 0);
1623 1.2.6.2 yamt
1624 1.2.6.2 yamt /* Stop the transmit and receive processes. */
1625 1.2.6.2 yamt AE_WRITE(sc, CSR_OPMODE, 0);
1626 1.2.6.2 yamt AE_WRITE(sc, CSR_RXLIST, 0);
1627 1.2.6.2 yamt AE_WRITE(sc, CSR_TXLIST, 0);
1628 1.2.6.2 yamt AE_CLR(sc, CSR_MACCTL, MACCTL_TE | MACCTL_RE);
1629 1.2.6.2 yamt AE_BARRIER(sc);
1630 1.2.6.2 yamt
1631 1.2.6.2 yamt /*
1632 1.2.6.2 yamt * Release any queued transmit buffers.
1633 1.2.6.2 yamt */
1634 1.2.6.2 yamt while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1635 1.2.6.2 yamt SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1636 1.2.6.2 yamt if (txs->txs_mbuf != NULL) {
1637 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1638 1.2.6.2 yamt m_freem(txs->txs_mbuf);
1639 1.2.6.2 yamt txs->txs_mbuf = NULL;
1640 1.2.6.2 yamt }
1641 1.2.6.2 yamt SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1642 1.2.6.2 yamt }
1643 1.2.6.2 yamt
1644 1.2.6.2 yamt if (disable) {
1645 1.2.6.2 yamt ae_rxdrain(sc);
1646 1.2.6.2 yamt ae_disable(sc);
1647 1.2.6.2 yamt }
1648 1.2.6.2 yamt
1649 1.2.6.2 yamt /*
1650 1.2.6.2 yamt * Mark the interface down and cancel the watchdog timer.
1651 1.2.6.2 yamt */
1652 1.2.6.2 yamt ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1653 1.2.6.2 yamt sc->sc_if_flags = ifp->if_flags;
1654 1.2.6.2 yamt ifp->if_timer = 0;
1655 1.2.6.2 yamt
1656 1.2.6.2 yamt /*
1657 1.2.6.2 yamt * Reset the chip (needed on some flavors to actually disable it).
1658 1.2.6.2 yamt */
1659 1.2.6.2 yamt ae_reset(sc);
1660 1.2.6.2 yamt }
1661 1.2.6.2 yamt
1662 1.2.6.2 yamt /*
1663 1.2.6.2 yamt * ae_add_rxbuf:
1664 1.2.6.2 yamt *
1665 1.2.6.2 yamt * Add a receive buffer to the indicated descriptor.
1666 1.2.6.2 yamt */
1667 1.2.6.2 yamt static int
1668 1.2.6.2 yamt ae_add_rxbuf(struct ae_softc *sc, int idx)
1669 1.2.6.2 yamt {
1670 1.2.6.2 yamt struct ae_rxsoft *rxs = &sc->sc_rxsoft[idx];
1671 1.2.6.2 yamt struct mbuf *m;
1672 1.2.6.2 yamt int error;
1673 1.2.6.2 yamt
1674 1.2.6.2 yamt MGETHDR(m, M_DONTWAIT, MT_DATA);
1675 1.2.6.2 yamt if (m == NULL)
1676 1.2.6.2 yamt return (ENOBUFS);
1677 1.2.6.2 yamt
1678 1.2.6.2 yamt MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1679 1.2.6.2 yamt MCLGET(m, M_DONTWAIT);
1680 1.2.6.2 yamt if ((m->m_flags & M_EXT) == 0) {
1681 1.2.6.2 yamt m_freem(m);
1682 1.2.6.2 yamt return (ENOBUFS);
1683 1.2.6.2 yamt }
1684 1.2.6.2 yamt
1685 1.2.6.2 yamt if (rxs->rxs_mbuf != NULL)
1686 1.2.6.2 yamt bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1687 1.2.6.2 yamt
1688 1.2.6.2 yamt rxs->rxs_mbuf = m;
1689 1.2.6.2 yamt
1690 1.2.6.2 yamt error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1691 1.2.6.2 yamt m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1692 1.2.6.2 yamt BUS_DMA_READ|BUS_DMA_NOWAIT);
1693 1.2.6.2 yamt if (error) {
1694 1.2.6.2 yamt printf("%s: can't load rx DMA map %d, error = %d\n",
1695 1.2.6.2 yamt sc->sc_dev.dv_xname, idx, error);
1696 1.2.6.2 yamt panic("ae_add_rxbuf"); /* XXX */
1697 1.2.6.2 yamt }
1698 1.2.6.2 yamt
1699 1.2.6.2 yamt bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1700 1.2.6.2 yamt rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1701 1.2.6.2 yamt
1702 1.2.6.2 yamt AE_INIT_RXDESC(sc, idx);
1703 1.2.6.2 yamt
1704 1.2.6.2 yamt return (0);
1705 1.2.6.2 yamt }
1706 1.2.6.2 yamt
1707 1.2.6.2 yamt /*
1708 1.2.6.2 yamt * ae_filter_setup:
1709 1.2.6.2 yamt *
1710 1.2.6.2 yamt * Set the chip's receive filter.
1711 1.2.6.2 yamt */
1712 1.2.6.2 yamt static void
1713 1.2.6.2 yamt ae_filter_setup(struct ae_softc *sc)
1714 1.2.6.2 yamt {
1715 1.2.6.2 yamt struct ethercom *ec = &sc->sc_ethercom;
1716 1.2.6.2 yamt struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1717 1.2.6.2 yamt struct ether_multi *enm;
1718 1.2.6.2 yamt struct ether_multistep step;
1719 1.2.6.2 yamt uint32_t hash, mchash[2];
1720 1.2.6.2 yamt uint32_t macctl = 0;
1721 1.2.6.2 yamt
1722 1.2.6.2 yamt /*
1723 1.2.6.2 yamt * If the chip is running, we need to reset the interface,
1724 1.2.6.2 yamt * and will revisit here (with IFF_RUNNING) clear. The
1725 1.2.6.2 yamt * chip seems to really not like to have its multicast
1726 1.2.6.2 yamt * filter programmed without a reset.
1727 1.2.6.2 yamt */
1728 1.2.6.2 yamt if (ifp->if_flags & IFF_RUNNING) {
1729 1.2.6.2 yamt (void) ae_init(ifp);
1730 1.2.6.2 yamt return;
1731 1.2.6.2 yamt }
1732 1.2.6.2 yamt
1733 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_filter_setup: sc_flags 0x%08x\n",
1734 1.2.6.2 yamt sc->sc_dev.dv_xname, sc->sc_flags));
1735 1.2.6.2 yamt
1736 1.2.6.2 yamt macctl = AE_READ(sc, CSR_MACCTL);
1737 1.2.6.2 yamt macctl &= ~(MACCTL_PR | MACCTL_PM);
1738 1.2.6.2 yamt macctl |= MACCTL_HASH;
1739 1.2.6.2 yamt macctl |= MACCTL_HBD;
1740 1.2.6.2 yamt macctl |= MACCTL_PR;
1741 1.2.6.2 yamt
1742 1.2.6.2 yamt if (ifp->if_flags & IFF_PROMISC) {
1743 1.2.6.2 yamt macctl |= MACCTL_PR;
1744 1.2.6.2 yamt goto allmulti;
1745 1.2.6.2 yamt }
1746 1.2.6.2 yamt
1747 1.2.6.2 yamt mchash[0] = mchash[1] = 0;
1748 1.2.6.2 yamt
1749 1.2.6.2 yamt ETHER_FIRST_MULTI(step, ec, enm);
1750 1.2.6.2 yamt while (enm != NULL) {
1751 1.2.6.2 yamt if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1752 1.2.6.2 yamt /*
1753 1.2.6.2 yamt * We must listen to a range of multicast addresses.
1754 1.2.6.2 yamt * For now, just accept all multicasts, rather than
1755 1.2.6.2 yamt * trying to set only those filter bits needed to match
1756 1.2.6.2 yamt * the range. (At this time, the only use of address
1757 1.2.6.2 yamt * ranges is for IP multicast routing, for which the
1758 1.2.6.2 yamt * range is big enough to require all bits set.)
1759 1.2.6.2 yamt */
1760 1.2.6.2 yamt goto allmulti;
1761 1.2.6.2 yamt }
1762 1.2.6.2 yamt
1763 1.2.6.2 yamt /* Verify whether we use big or little endian hashes */
1764 1.2.6.2 yamt hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
1765 1.2.6.2 yamt mchash[hash >> 5] |= 1 << (hash & 0x1f);
1766 1.2.6.2 yamt ETHER_NEXT_MULTI(step, enm);
1767 1.2.6.2 yamt }
1768 1.2.6.2 yamt ifp->if_flags &= ~IFF_ALLMULTI;
1769 1.2.6.2 yamt goto setit;
1770 1.2.6.2 yamt
1771 1.2.6.2 yamt allmulti:
1772 1.2.6.2 yamt ifp->if_flags |= IFF_ALLMULTI;
1773 1.2.6.2 yamt mchash[0] = mchash[1] = 0xffffffff;
1774 1.2.6.2 yamt macctl |= MACCTL_PM;
1775 1.2.6.2 yamt
1776 1.2.6.2 yamt setit:
1777 1.2.6.2 yamt AE_WRITE(sc, CSR_HTHI, mchash[0]);
1778 1.2.6.2 yamt AE_WRITE(sc, CSR_HTHI, mchash[1]);
1779 1.2.6.2 yamt
1780 1.2.6.2 yamt AE_WRITE(sc, CSR_MACCTL, macctl);
1781 1.2.6.2 yamt AE_BARRIER(sc);
1782 1.2.6.2 yamt
1783 1.2.6.2 yamt DPRINTF(sc, ("%s: ae_filter_setup: returning %x\n",
1784 1.2.6.2 yamt sc->sc_dev.dv_xname, macctl));
1785 1.2.6.2 yamt }
1786 1.2.6.2 yamt
1787 1.2.6.2 yamt /*
1788 1.2.6.2 yamt * ae_idle:
1789 1.2.6.2 yamt *
1790 1.2.6.2 yamt * Cause the transmit and/or receive processes to go idle.
1791 1.2.6.2 yamt */
1792 1.2.6.2 yamt void
1793 1.2.6.2 yamt ae_idle(struct ae_softc *sc, u_int32_t bits)
1794 1.2.6.2 yamt {
1795 1.2.6.2 yamt static const char * const txstate_names[] = {
1796 1.2.6.2 yamt "STOPPED",
1797 1.2.6.2 yamt "RUNNING - FETCH",
1798 1.2.6.2 yamt "RUNNING - WAIT",
1799 1.2.6.2 yamt "RUNNING - READING",
1800 1.2.6.2 yamt "-- RESERVED --",
1801 1.2.6.2 yamt "RUNNING - SETUP",
1802 1.2.6.2 yamt "SUSPENDED",
1803 1.2.6.2 yamt "RUNNING - CLOSE",
1804 1.2.6.2 yamt };
1805 1.2.6.2 yamt static const char * const rxstate_names[] = {
1806 1.2.6.2 yamt "STOPPED",
1807 1.2.6.2 yamt "RUNNING - FETCH",
1808 1.2.6.2 yamt "RUNNING - CHECK",
1809 1.2.6.2 yamt "RUNNING - WAIT",
1810 1.2.6.2 yamt "SUSPENDED",
1811 1.2.6.2 yamt "RUNNING - CLOSE",
1812 1.2.6.2 yamt "RUNNING - FLUSH",
1813 1.2.6.2 yamt "RUNNING - QUEUE",
1814 1.2.6.2 yamt };
1815 1.2.6.2 yamt
1816 1.2.6.2 yamt u_int32_t csr, ackmask = 0;
1817 1.2.6.2 yamt int i;
1818 1.2.6.2 yamt
1819 1.2.6.2 yamt if (bits & OPMODE_ST)
1820 1.2.6.2 yamt ackmask |= STATUS_TPS;
1821 1.2.6.2 yamt
1822 1.2.6.2 yamt if (bits & OPMODE_SR)
1823 1.2.6.2 yamt ackmask |= STATUS_RPS;
1824 1.2.6.2 yamt
1825 1.2.6.2 yamt AE_CLR(sc, CSR_OPMODE, bits);
1826 1.2.6.2 yamt
1827 1.2.6.2 yamt for (i = 0; i < 1000; i++) {
1828 1.2.6.2 yamt if (AE_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
1829 1.2.6.2 yamt break;
1830 1.2.6.2 yamt delay(10);
1831 1.2.6.2 yamt }
1832 1.2.6.2 yamt
1833 1.2.6.2 yamt csr = AE_READ(sc, CSR_STATUS);
1834 1.2.6.2 yamt if ((csr & ackmask) != ackmask) {
1835 1.2.6.2 yamt if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
1836 1.2.6.2 yamt (csr & STATUS_TS) != STATUS_TS_STOPPED) {
1837 1.2.6.2 yamt printf("%s: transmit process failed to idle: "
1838 1.2.6.2 yamt "state %s\n", sc->sc_dev.dv_xname,
1839 1.2.6.2 yamt txstate_names[(csr & STATUS_TS) >> 20]);
1840 1.2.6.2 yamt }
1841 1.2.6.2 yamt if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
1842 1.2.6.2 yamt (csr & STATUS_RS) != STATUS_RS_STOPPED) {
1843 1.2.6.2 yamt printf("%s: receive process failed to idle: "
1844 1.2.6.2 yamt "state %s\n", sc->sc_dev.dv_xname,
1845 1.2.6.2 yamt rxstate_names[(csr & STATUS_RS) >> 17]);
1846 1.2.6.2 yamt }
1847 1.2.6.2 yamt }
1848 1.2.6.2 yamt }
1849 1.2.6.2 yamt
1850 1.2.6.2 yamt /*****************************************************************************
1851 1.2.6.2 yamt * Generic media support functions.
1852 1.2.6.2 yamt *****************************************************************************/
1853 1.2.6.2 yamt
1854 1.2.6.2 yamt /*
1855 1.2.6.2 yamt * ae_mediastatus: [ifmedia interface function]
1856 1.2.6.2 yamt *
1857 1.2.6.2 yamt * Query the current media.
1858 1.2.6.2 yamt */
1859 1.2.6.2 yamt void
1860 1.2.6.2 yamt ae_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
1861 1.2.6.2 yamt {
1862 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
1863 1.2.6.2 yamt
1864 1.2.6.2 yamt if (AE_IS_ENABLED(sc) == 0) {
1865 1.2.6.2 yamt ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1866 1.2.6.2 yamt ifmr->ifm_status = 0;
1867 1.2.6.2 yamt return;
1868 1.2.6.2 yamt }
1869 1.2.6.2 yamt
1870 1.2.6.2 yamt mii_pollstat(&sc->sc_mii);
1871 1.2.6.2 yamt ifmr->ifm_status = sc->sc_mii.mii_media_status;
1872 1.2.6.2 yamt ifmr->ifm_active = sc->sc_mii.mii_media_active;
1873 1.2.6.2 yamt }
1874 1.2.6.2 yamt
1875 1.2.6.2 yamt /*
1876 1.2.6.2 yamt * ae_mediachange: [ifmedia interface function]
1877 1.2.6.2 yamt *
1878 1.2.6.2 yamt * Update the current media.
1879 1.2.6.2 yamt */
1880 1.2.6.2 yamt int
1881 1.2.6.2 yamt ae_mediachange(struct ifnet *ifp)
1882 1.2.6.2 yamt {
1883 1.2.6.2 yamt struct ae_softc *sc = ifp->if_softc;
1884 1.2.6.2 yamt
1885 1.2.6.2 yamt if ((ifp->if_flags & IFF_UP) == 0)
1886 1.2.6.2 yamt return (0);
1887 1.2.6.2 yamt
1888 1.2.6.2 yamt mii_mediachg(&sc->sc_mii);
1889 1.2.6.2 yamt return (0);
1890 1.2.6.2 yamt }
1891 1.2.6.2 yamt
1892 1.2.6.2 yamt /*****************************************************************************
1893 1.2.6.2 yamt * Support functions for MII-attached media.
1894 1.2.6.2 yamt *****************************************************************************/
1895 1.2.6.2 yamt
1896 1.2.6.2 yamt /*
1897 1.2.6.2 yamt * ae_mii_tick:
1898 1.2.6.2 yamt *
1899 1.2.6.2 yamt * One second timer, used to tick the MII.
1900 1.2.6.2 yamt */
1901 1.2.6.2 yamt static void
1902 1.2.6.2 yamt ae_mii_tick(void *arg)
1903 1.2.6.2 yamt {
1904 1.2.6.2 yamt struct ae_softc *sc = arg;
1905 1.2.6.2 yamt int s;
1906 1.2.6.2 yamt
1907 1.2.6.2 yamt if (!device_is_active(&sc->sc_dev))
1908 1.2.6.2 yamt return;
1909 1.2.6.2 yamt
1910 1.2.6.2 yamt s = splnet();
1911 1.2.6.2 yamt mii_tick(&sc->sc_mii);
1912 1.2.6.2 yamt splx(s);
1913 1.2.6.2 yamt
1914 1.2.6.2 yamt callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
1915 1.2.6.2 yamt }
1916 1.2.6.2 yamt
1917 1.2.6.2 yamt /*
1918 1.2.6.2 yamt * ae_mii_statchg: [mii interface function]
1919 1.2.6.2 yamt *
1920 1.2.6.2 yamt * Callback from PHY when media changes.
1921 1.2.6.2 yamt */
1922 1.2.6.2 yamt static void
1923 1.2.6.2 yamt ae_mii_statchg(struct device *self)
1924 1.2.6.2 yamt {
1925 1.2.6.2 yamt struct ae_softc *sc = (struct ae_softc *)self;
1926 1.2.6.2 yamt uint32_t macctl, flowc;
1927 1.2.6.2 yamt
1928 1.2.6.2 yamt //opmode = AE_READ(sc, CSR_OPMODE);
1929 1.2.6.2 yamt macctl = AE_READ(sc, CSR_MACCTL);
1930 1.2.6.2 yamt
1931 1.2.6.2 yamt /* XXX: do we need to do this? */
1932 1.2.6.2 yamt /* Idle the transmit and receive processes. */
1933 1.2.6.2 yamt //ae_idle(sc, OPMODE_ST|OPMODE_SR);
1934 1.2.6.2 yamt
1935 1.2.6.2 yamt if (sc->sc_mii.mii_media_active & IFM_FDX) {
1936 1.2.6.2 yamt flowc = FLOWC_FCE;
1937 1.2.6.2 yamt macctl &= ~MACCTL_DRO;
1938 1.2.6.2 yamt macctl |= MACCTL_FDX;
1939 1.2.6.2 yamt } else {
1940 1.2.6.2 yamt flowc = 0; /* cannot do flow control in HDX */
1941 1.2.6.2 yamt macctl |= MACCTL_DRO;
1942 1.2.6.2 yamt macctl &= ~MACCTL_FDX;
1943 1.2.6.2 yamt }
1944 1.2.6.2 yamt
1945 1.2.6.2 yamt AE_WRITE(sc, CSR_FLOWC, flowc);
1946 1.2.6.2 yamt AE_WRITE(sc, CSR_MACCTL, macctl);
1947 1.2.6.2 yamt
1948 1.2.6.2 yamt /* restore operational mode */
1949 1.2.6.2 yamt //AE_WRITE(sc, CSR_OPMODE, opmode);
1950 1.2.6.2 yamt AE_BARRIER(sc);
1951 1.2.6.2 yamt }
1952 1.2.6.2 yamt
1953 1.2.6.2 yamt /*
1954 1.2.6.2 yamt * ae_mii_readreg:
1955 1.2.6.2 yamt *
1956 1.2.6.2 yamt * Read a PHY register.
1957 1.2.6.2 yamt */
1958 1.2.6.2 yamt static int
1959 1.2.6.2 yamt ae_mii_readreg(struct device *self, int phy, int reg)
1960 1.2.6.2 yamt {
1961 1.2.6.2 yamt struct ae_softc *sc = (struct ae_softc *)self;
1962 1.2.6.2 yamt uint32_t addr;
1963 1.2.6.2 yamt int i;
1964 1.2.6.2 yamt
1965 1.2.6.2 yamt addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT);
1966 1.2.6.2 yamt AE_WRITE(sc, CSR_MIIADDR, addr);
1967 1.2.6.2 yamt AE_BARRIER(sc);
1968 1.2.6.2 yamt for (i = 0; i < 100000000; i++) {
1969 1.2.6.2 yamt if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
1970 1.2.6.2 yamt break;
1971 1.2.6.2 yamt }
1972 1.2.6.2 yamt
1973 1.2.6.2 yamt return (AE_READ(sc, CSR_MIIDATA) & 0xffff);
1974 1.2.6.2 yamt }
1975 1.2.6.2 yamt
1976 1.2.6.2 yamt /*
1977 1.2.6.2 yamt * ae_mii_writereg:
1978 1.2.6.2 yamt *
1979 1.2.6.2 yamt * Write a PHY register.
1980 1.2.6.2 yamt */
1981 1.2.6.2 yamt static void
1982 1.2.6.2 yamt ae_mii_writereg(struct device *self, int phy, int reg, int val)
1983 1.2.6.2 yamt {
1984 1.2.6.2 yamt struct ae_softc *sc = (struct ae_softc *)self;
1985 1.2.6.2 yamt uint32_t addr;
1986 1.2.6.2 yamt int i;
1987 1.2.6.2 yamt
1988 1.2.6.2 yamt /* write the data register */
1989 1.2.6.2 yamt AE_WRITE(sc, CSR_MIIDATA, val);
1990 1.2.6.2 yamt
1991 1.2.6.2 yamt /* write the address to latch it in */
1992 1.2.6.2 yamt addr = (phy << MIIADDR_PHY_SHIFT) | (reg << MIIADDR_REG_SHIFT) |
1993 1.2.6.2 yamt MIIADDR_WRITE;
1994 1.2.6.2 yamt AE_WRITE(sc, CSR_MIIADDR, addr);
1995 1.2.6.2 yamt AE_BARRIER(sc);
1996 1.2.6.2 yamt
1997 1.2.6.2 yamt for (i = 0; i < 100000000; i++) {
1998 1.2.6.2 yamt if ((AE_READ(sc, CSR_MIIADDR) & MIIADDR_BUSY) == 0)
1999 1.2.6.2 yamt break;
2000 1.2.6.2 yamt }
2001 1.2.6.2 yamt }
2002