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