if_ec.c revision 1.2.6.3 1 1.2.6.3 nathanw /* $NetBSD: if_ec.c,v 1.2.6.3 2002/10/18 02:40:13 nathanw Exp $ */
2 1.2.6.2 nathanw
3 1.2.6.2 nathanw /*
4 1.2.6.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.2.6.2 nathanw * All rights reserved.
6 1.2.6.2 nathanw *
7 1.2.6.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.2.6.2 nathanw * by Matthew Fredette.
9 1.2.6.2 nathanw *
10 1.2.6.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.2.6.2 nathanw * modification, are permitted provided that the following conditions
12 1.2.6.2 nathanw * are met:
13 1.2.6.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.2.6.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.2.6.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.6.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.2.6.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.2.6.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.2.6.2 nathanw * must display the following acknowledgement:
20 1.2.6.2 nathanw * This product includes software developed by the NetBSD
21 1.2.6.2 nathanw * Foundation, Inc. and its contributors.
22 1.2.6.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.6.2 nathanw * contributors may be used to endorse or promote products derived
24 1.2.6.2 nathanw * from this software without specific prior written permission.
25 1.2.6.2 nathanw *
26 1.2.6.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.6.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.6.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.6.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.6.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.6.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.6.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.6.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.6.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.6.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.6.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.2.6.2 nathanw */
38 1.2.6.2 nathanw
39 1.2.6.2 nathanw /*
40 1.2.6.2 nathanw * 3Com 3C400 device driver
41 1.2.6.2 nathanw */
42 1.2.6.2 nathanw
43 1.2.6.2 nathanw #include "opt_inet.h"
44 1.2.6.2 nathanw #include "opt_ns.h"
45 1.2.6.2 nathanw #include "bpfilter.h"
46 1.2.6.2 nathanw #include "rnd.h"
47 1.2.6.2 nathanw
48 1.2.6.2 nathanw #include <sys/param.h>
49 1.2.6.2 nathanw #include <sys/systm.h>
50 1.2.6.2 nathanw #include <sys/errno.h>
51 1.2.6.2 nathanw #include <sys/ioctl.h>
52 1.2.6.2 nathanw #include <sys/mbuf.h>
53 1.2.6.2 nathanw #include <sys/socket.h>
54 1.2.6.2 nathanw #include <sys/syslog.h>
55 1.2.6.2 nathanw #include <sys/device.h>
56 1.2.6.2 nathanw #include <sys/endian.h>
57 1.2.6.2 nathanw #if NRND > 0
58 1.2.6.2 nathanw #include <sys/rnd.h>
59 1.2.6.2 nathanw #endif
60 1.2.6.2 nathanw
61 1.2.6.2 nathanw #include <net/if.h>
62 1.2.6.2 nathanw #include <net/if_dl.h>
63 1.2.6.2 nathanw #include <net/if_types.h>
64 1.2.6.2 nathanw
65 1.2.6.2 nathanw #include <net/if_ether.h>
66 1.2.6.2 nathanw #include <net/if_media.h>
67 1.2.6.2 nathanw
68 1.2.6.2 nathanw #ifdef INET
69 1.2.6.2 nathanw #include <netinet/in.h>
70 1.2.6.2 nathanw #include <netinet/in_systm.h>
71 1.2.6.2 nathanw #include <netinet/in_var.h>
72 1.2.6.2 nathanw #include <netinet/ip.h>
73 1.2.6.2 nathanw #include <netinet/if_inarp.h>
74 1.2.6.2 nathanw #endif
75 1.2.6.2 nathanw
76 1.2.6.2 nathanw #ifdef NS
77 1.2.6.2 nathanw #include <netns/ns.h>
78 1.2.6.2 nathanw #include <netns/ns_if.h>
79 1.2.6.2 nathanw #endif
80 1.2.6.2 nathanw
81 1.2.6.2 nathanw #if NBPFILTER > 0
82 1.2.6.2 nathanw #include <net/bpf.h>
83 1.2.6.2 nathanw #include <net/bpfdesc.h>
84 1.2.6.2 nathanw #endif
85 1.2.6.2 nathanw
86 1.2.6.2 nathanw #include <machine/cpu.h>
87 1.2.6.2 nathanw #include <machine/autoconf.h>
88 1.2.6.2 nathanw #include <machine/idprom.h>
89 1.2.6.2 nathanw #include <machine/bus.h>
90 1.2.6.2 nathanw #include <machine/intr.h>
91 1.2.6.2 nathanw
92 1.2.6.2 nathanw #include <sun2/dev/if_ecreg.h>
93 1.2.6.2 nathanw
94 1.2.6.2 nathanw /*
95 1.2.6.2 nathanw * Interface softc.
96 1.2.6.2 nathanw */
97 1.2.6.2 nathanw struct ec_softc {
98 1.2.6.2 nathanw struct device sc_dev;
99 1.2.6.2 nathanw void *sc_ih;
100 1.2.6.2 nathanw
101 1.2.6.2 nathanw struct ethercom sc_ethercom; /* ethernet common */
102 1.2.6.2 nathanw struct ifmedia sc_media; /* our supported media */
103 1.2.6.2 nathanw
104 1.2.6.2 nathanw bus_space_tag_t sc_iot; /* bus space tag */
105 1.2.6.2 nathanw bus_space_handle_t sc_ioh; /* bus space handle */
106 1.2.6.2 nathanw
107 1.2.6.2 nathanw u_char sc_jammed; /* nonzero if the net is jammed */
108 1.2.6.2 nathanw u_char sc_colliding; /* nonzero if the net is colliding */
109 1.2.6.2 nathanw u_int32_t sc_backoff_seed; /* seed for the backoff PRNG */
110 1.2.6.2 nathanw
111 1.2.6.2 nathanw #if NRND > 0
112 1.2.6.2 nathanw rndsource_element_t rnd_source;
113 1.2.6.2 nathanw #endif
114 1.2.6.2 nathanw };
115 1.2.6.2 nathanw
116 1.2.6.2 nathanw /* Macros to read and write the CSR. */
117 1.2.6.2 nathanw #define ECREG_CSR_RD bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR)
118 1.2.6.2 nathanw #define ECREG_CSR_WR(val) bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR, val)
119 1.2.6.2 nathanw
120 1.2.6.2 nathanw /* After this many collisions, the packet is dropped. */
121 1.2.6.2 nathanw #define EC_COLLISIONS_JAMMED 16
122 1.2.6.2 nathanw
123 1.2.6.2 nathanw /*
124 1.2.6.2 nathanw * Various constants used in the backoff pseudorandom
125 1.2.6.2 nathanw * number generator.
126 1.2.6.2 nathanw */
127 1.2.6.2 nathanw #define EC_BACKOFF_PRNG_COLL_MAX 10
128 1.2.6.2 nathanw #define EC_BACKOFF_PRNG_MUL 1103515245
129 1.2.6.2 nathanw #define EC_BACKOFF_PRNG_ADD 12345
130 1.2.6.2 nathanw #define EC_BACKOFF_PRNG_MASK 0x7fffffff
131 1.2.6.2 nathanw
132 1.2.6.2 nathanw /*
133 1.2.6.2 nathanw * Prototypes
134 1.2.6.2 nathanw */
135 1.2.6.2 nathanw int ec_intr __P((void *));
136 1.2.6.2 nathanw void ec_reset __P((struct ifnet *));
137 1.2.6.2 nathanw int ec_init __P((struct ifnet *));
138 1.2.6.2 nathanw int ec_ioctl __P((struct ifnet *, u_long, caddr_t));
139 1.2.6.2 nathanw void ec_watchdog __P((struct ifnet *));
140 1.2.6.2 nathanw void ec_start __P((struct ifnet *));
141 1.2.6.2 nathanw
142 1.2.6.2 nathanw void ec_recv __P((struct ec_softc *, int));
143 1.2.6.2 nathanw void ec_coll __P((struct ec_softc *));
144 1.2.6.2 nathanw void ec_copyin __P((struct ec_softc *, void *, int, size_t));
145 1.2.6.2 nathanw void ec_copyout __P((struct ec_softc *, const void *, int, size_t));
146 1.2.6.2 nathanw
147 1.2.6.2 nathanw int ec_mediachange __P((struct ifnet *));
148 1.2.6.2 nathanw void ec_mediastatus __P((struct ifnet *, struct ifmediareq *));
149 1.2.6.2 nathanw
150 1.2.6.2 nathanw int ec_match __P((struct device *, struct cfdata *, void *));
151 1.2.6.2 nathanw void ec_attach __P((struct device *, struct device *, void *));
152 1.2.6.2 nathanw
153 1.2.6.3 nathanw CFATTACH_DECL(ec, sizeof(struct ec_softc),
154 1.2.6.3 nathanw ec_match, ec_attach, NULL, NULL);
155 1.2.6.2 nathanw
156 1.2.6.2 nathanw /*
157 1.2.6.2 nathanw * Copy board memory to kernel.
158 1.2.6.2 nathanw */
159 1.2.6.2 nathanw void
160 1.2.6.2 nathanw ec_copyin(sc, p, offset, size)
161 1.2.6.2 nathanw struct ec_softc *sc;
162 1.2.6.2 nathanw void *p;
163 1.2.6.2 nathanw int offset;
164 1.2.6.2 nathanw size_t size;
165 1.2.6.2 nathanw {
166 1.2.6.2 nathanw bus_space_copyin(sc->sc_iot, sc->sc_ioh, offset, p, size);
167 1.2.6.2 nathanw }
168 1.2.6.2 nathanw
169 1.2.6.2 nathanw /*
170 1.2.6.2 nathanw * Copy from kernel space to board memory.
171 1.2.6.2 nathanw */
172 1.2.6.2 nathanw void
173 1.2.6.2 nathanw ec_copyout(sc, p, offset, size)
174 1.2.6.2 nathanw struct ec_softc *sc;
175 1.2.6.2 nathanw const void *p;
176 1.2.6.2 nathanw int offset;
177 1.2.6.2 nathanw size_t size;
178 1.2.6.2 nathanw {
179 1.2.6.2 nathanw bus_space_copyout(sc->sc_iot, sc->sc_ioh, offset, p, size);
180 1.2.6.2 nathanw }
181 1.2.6.2 nathanw
182 1.2.6.2 nathanw int
183 1.2.6.2 nathanw ec_match(parent, match, aux)
184 1.2.6.2 nathanw struct device *parent;
185 1.2.6.2 nathanw struct cfdata *match;
186 1.2.6.2 nathanw void *aux;
187 1.2.6.2 nathanw {
188 1.2.6.2 nathanw struct mbmem_attach_args *mbma = aux;
189 1.2.6.2 nathanw bus_space_handle_t bh;
190 1.2.6.2 nathanw int matched;
191 1.2.6.2 nathanw
192 1.2.6.2 nathanw /* No default Multibus address. */
193 1.2.6.2 nathanw if (mbma->mbma_paddr == -1)
194 1.2.6.2 nathanw return (0);
195 1.2.6.2 nathanw
196 1.2.6.2 nathanw /* Make sure there is something there... */
197 1.2.6.2 nathanw if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
198 1.2.6.2 nathanw 0, &bh))
199 1.2.6.2 nathanw return (0);
200 1.2.6.2 nathanw matched = (bus_space_peek_2(mbma->mbma_bustag, bh, 0, NULL) == 0);
201 1.2.6.2 nathanw bus_space_unmap(mbma->mbma_bustag, bh, ECREG_BANK_SZ);
202 1.2.6.2 nathanw if (!matched)
203 1.2.6.2 nathanw return (0);
204 1.2.6.2 nathanw
205 1.2.6.2 nathanw /* Default interrupt priority. */
206 1.2.6.2 nathanw if (mbma->mbma_pri == -1)
207 1.2.6.2 nathanw mbma->mbma_pri = 3;
208 1.2.6.2 nathanw
209 1.2.6.2 nathanw return (1);
210 1.2.6.2 nathanw }
211 1.2.6.2 nathanw
212 1.2.6.2 nathanw void
213 1.2.6.2 nathanw ec_attach(parent, self, aux)
214 1.2.6.2 nathanw struct device *parent, *self;
215 1.2.6.2 nathanw void *aux;
216 1.2.6.2 nathanw {
217 1.2.6.2 nathanw struct ec_softc *sc = (void *) self;
218 1.2.6.2 nathanw struct mbmem_attach_args *mbma = aux;
219 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
220 1.2.6.2 nathanw u_int8_t myaddr[ETHER_ADDR_LEN];
221 1.2.6.2 nathanw
222 1.2.6.2 nathanw printf("\n");
223 1.2.6.2 nathanw
224 1.2.6.2 nathanw /* Map in the board control regs. */
225 1.2.6.2 nathanw sc->sc_iot = mbma->mbma_bustag;
226 1.2.6.2 nathanw if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
227 1.2.6.2 nathanw 0, &sc->sc_ioh))
228 1.2.6.2 nathanw panic("ec_attach: can't map regs");
229 1.2.6.2 nathanw
230 1.2.6.2 nathanw /* Reset the board. */
231 1.2.6.2 nathanw ECREG_CSR_WR(EC_CSR_RESET);
232 1.2.6.2 nathanw delay(160);
233 1.2.6.2 nathanw
234 1.2.6.2 nathanw /*
235 1.2.6.2 nathanw * Copy out the board ROM Ethernet address,
236 1.2.6.2 nathanw * and use the non-vendor-ID part to seed
237 1.2.6.2 nathanw * our backoff pseudorandom number generator.
238 1.2.6.2 nathanw */
239 1.2.6.2 nathanw bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, ECREG_AROM, myaddr, ETHER_ADDR_LEN);
240 1.2.6.2 nathanw sc->sc_backoff_seed = (myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
241 1.2.6.2 nathanw
242 1.2.6.2 nathanw /* Initialize ifnet structure. */
243 1.2.6.2 nathanw bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
244 1.2.6.2 nathanw ifp->if_softc = sc;
245 1.2.6.2 nathanw ifp->if_start = ec_start;
246 1.2.6.2 nathanw ifp->if_ioctl = ec_ioctl;
247 1.2.6.2 nathanw ifp->if_init = ec_init;
248 1.2.6.2 nathanw ifp->if_watchdog = ec_watchdog;
249 1.2.6.2 nathanw ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
250 1.2.6.2 nathanw IFQ_SET_READY(&ifp->if_snd);
251 1.2.6.2 nathanw
252 1.2.6.2 nathanw /* Initialize ifmedia structures. */
253 1.2.6.2 nathanw ifmedia_init(&sc->sc_media, 0, ec_mediachange, ec_mediastatus);
254 1.2.6.2 nathanw ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
255 1.2.6.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
256 1.2.6.2 nathanw
257 1.2.6.2 nathanw /* Now we can attach the interface. */
258 1.2.6.2 nathanw if_attach(ifp);
259 1.2.6.2 nathanw idprom_etheraddr(myaddr);
260 1.2.6.2 nathanw ether_ifattach(ifp, myaddr);
261 1.2.6.2 nathanw printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
262 1.2.6.2 nathanw
263 1.2.6.2 nathanw bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
264 1.2.6.2 nathanw ec_intr, sc);
265 1.2.6.2 nathanw
266 1.2.6.2 nathanw #if NRND > 0
267 1.2.6.2 nathanw rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
268 1.2.6.2 nathanw RND_TYPE_NET, 0);
269 1.2.6.2 nathanw #endif
270 1.2.6.2 nathanw }
271 1.2.6.2 nathanw
272 1.2.6.2 nathanw /*
273 1.2.6.2 nathanw * Reset interface.
274 1.2.6.2 nathanw */
275 1.2.6.2 nathanw void
276 1.2.6.2 nathanw ec_reset(ifp)
277 1.2.6.2 nathanw struct ifnet *ifp;
278 1.2.6.2 nathanw {
279 1.2.6.2 nathanw int s;
280 1.2.6.2 nathanw
281 1.2.6.2 nathanw s = splnet();
282 1.2.6.2 nathanw ec_init(ifp);
283 1.2.6.2 nathanw splx(s);
284 1.2.6.2 nathanw }
285 1.2.6.2 nathanw
286 1.2.6.2 nathanw
287 1.2.6.2 nathanw /*
288 1.2.6.2 nathanw * Initialize interface.
289 1.2.6.2 nathanw */
290 1.2.6.2 nathanw int
291 1.2.6.2 nathanw ec_init(ifp)
292 1.2.6.2 nathanw struct ifnet *ifp;
293 1.2.6.2 nathanw {
294 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
295 1.2.6.2 nathanw
296 1.2.6.2 nathanw /* Reset the board. */
297 1.2.6.2 nathanw ECREG_CSR_WR(EC_CSR_RESET);
298 1.2.6.2 nathanw delay(160);
299 1.2.6.2 nathanw
300 1.2.6.2 nathanw /* Set the Ethernet address. */
301 1.2.6.2 nathanw bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, ECREG_ARAM, LLADDR(sc->sc_ethercom.ec_if.if_sadl), ETHER_ADDR_LEN);
302 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
303 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & 0);
304 1.2.6.2 nathanw
305 1.2.6.2 nathanw /* Enable interrupts. */
306 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_BINT | EC_CSR_AINT | (ifp->if_flags & IFF_PROMISC ? EC_CSR_PROMISC : EC_CSR_PA));
307 1.2.6.2 nathanw
308 1.2.6.2 nathanw /* Set flags appropriately. */
309 1.2.6.2 nathanw ifp->if_flags |= IFF_RUNNING;
310 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
311 1.2.6.2 nathanw
312 1.2.6.2 nathanw /* Start output. */
313 1.2.6.2 nathanw ec_start(ifp);
314 1.2.6.2 nathanw
315 1.2.6.2 nathanw return (0);
316 1.2.6.2 nathanw }
317 1.2.6.2 nathanw
318 1.2.6.2 nathanw /*
319 1.2.6.2 nathanw * Start output on interface.
320 1.2.6.2 nathanw */
321 1.2.6.2 nathanw void
322 1.2.6.2 nathanw ec_start(ifp)
323 1.2.6.2 nathanw struct ifnet *ifp;
324 1.2.6.2 nathanw {
325 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
326 1.2.6.2 nathanw struct mbuf *m, *m0;
327 1.2.6.2 nathanw int s;
328 1.2.6.2 nathanw u_int count;
329 1.2.6.2 nathanw bus_size_t off;
330 1.2.6.2 nathanw
331 1.2.6.2 nathanw s = splnet();
332 1.2.6.2 nathanw
333 1.2.6.2 nathanw /* Don't do anything if output is active. */
334 1.2.6.2 nathanw if ((ifp->if_flags & IFF_OACTIVE) != 0) {
335 1.2.6.2 nathanw splx(s);
336 1.2.6.2 nathanw return;
337 1.2.6.2 nathanw }
338 1.2.6.2 nathanw /* Don't do anything if the output queue is empty. */
339 1.2.6.2 nathanw IFQ_DEQUEUE(&ifp->if_snd, m0);
340 1.2.6.2 nathanw if (m0 == NULL) {
341 1.2.6.2 nathanw splx(s);
342 1.2.6.2 nathanw return;
343 1.2.6.2 nathanw }
344 1.2.6.2 nathanw
345 1.2.6.2 nathanw #if NBPFILTER > 0
346 1.2.6.2 nathanw /* The BPF tap. */
347 1.2.6.2 nathanw if (ifp->if_bpf)
348 1.2.6.2 nathanw bpf_mtap(ifp->if_bpf, m0);
349 1.2.6.2 nathanw #endif
350 1.2.6.2 nathanw
351 1.2.6.2 nathanw /* Size the packet. */
352 1.2.6.2 nathanw for (count = EC_BUF_SZ, m = m0; m != NULL; m = m->m_next)
353 1.2.6.2 nathanw count -= m->m_len;
354 1.2.6.2 nathanw
355 1.2.6.2 nathanw /* Copy the packet into the xmit buffer. */
356 1.2.6.2 nathanw count = MIN(count, EC_PKT_MAXTDOFF);
357 1.2.6.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, count);
358 1.2.6.2 nathanw for (off = count, m = m0; m != 0; off += m->m_len, m = m->m_next)
359 1.2.6.2 nathanw ec_copyout(sc, mtod(m, u_int8_t *), ECREG_TBUF + off, m->m_len);
360 1.2.6.2 nathanw m_freem(m0);
361 1.2.6.2 nathanw
362 1.2.6.2 nathanw /* Enable the transmitter. */
363 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) | EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
364 1.2.6.2 nathanw ifp->if_flags |= IFF_OACTIVE;
365 1.2.6.2 nathanw
366 1.2.6.2 nathanw /* Done. */
367 1.2.6.2 nathanw splx(s);
368 1.2.6.2 nathanw }
369 1.2.6.2 nathanw
370 1.2.6.2 nathanw /*
371 1.2.6.2 nathanw * Controller interrupt.
372 1.2.6.2 nathanw */
373 1.2.6.2 nathanw int
374 1.2.6.2 nathanw ec_intr(arg)
375 1.2.6.2 nathanw void *arg;
376 1.2.6.2 nathanw {
377 1.2.6.2 nathanw struct ec_softc *sc = arg;
378 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
379 1.2.6.2 nathanw int recv_first;
380 1.2.6.2 nathanw int recv_second;
381 1.2.6.2 nathanw int retval;
382 1.2.6.2 nathanw struct mbuf *m0;
383 1.2.6.2 nathanw
384 1.2.6.2 nathanw retval = 0;
385 1.2.6.2 nathanw
386 1.2.6.2 nathanw /* Check for received packet(s). */
387 1.2.6.2 nathanw recv_first = recv_second = 0;
388 1.2.6.2 nathanw switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
389 1.2.6.2 nathanw
390 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_ABSW):
391 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
392 1.2.6.2 nathanw /* Neither buffer is full. Is this a transmit interrupt?
393 1.2.6.2 nathanw * Acknowledge the interrupt ourselves. */
394 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
395 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_BINT | EC_CSR_AINT);
396 1.2.6.2 nathanw break;
397 1.2.6.2 nathanw
398 1.2.6.2 nathanw case EC_CSR_BBSW:
399 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_RBBA):
400 1.2.6.2 nathanw /* Only the A buffer is full. */
401 1.2.6.2 nathanw recv_first = EC_CSR_AINT;
402 1.2.6.2 nathanw break;
403 1.2.6.2 nathanw
404 1.2.6.2 nathanw case EC_CSR_ABSW:
405 1.2.6.2 nathanw case (EC_CSR_ABSW | EC_CSR_RBBA):
406 1.2.6.2 nathanw /* Only the B buffer is full. */
407 1.2.6.2 nathanw recv_first = EC_CSR_BINT;
408 1.2.6.2 nathanw break;
409 1.2.6.2 nathanw
410 1.2.6.2 nathanw case 0:
411 1.2.6.2 nathanw /* Both the A buffer and the B buffer are full, and the A
412 1.2.6.2 nathanw * buffer is older than the B buffer. */
413 1.2.6.2 nathanw recv_first = EC_CSR_AINT;
414 1.2.6.2 nathanw recv_second = EC_CSR_BINT;
415 1.2.6.2 nathanw break;
416 1.2.6.2 nathanw
417 1.2.6.2 nathanw case EC_CSR_RBBA:
418 1.2.6.2 nathanw /* Both the A buffer and the B buffer are full, and the B
419 1.2.6.2 nathanw * buffer is older than the A buffer. */
420 1.2.6.2 nathanw recv_first = EC_CSR_BINT;
421 1.2.6.2 nathanw recv_second = EC_CSR_AINT;
422 1.2.6.2 nathanw break;
423 1.2.6.2 nathanw }
424 1.2.6.2 nathanw
425 1.2.6.2 nathanw /* Receive packets. */
426 1.2.6.2 nathanw if (recv_first) {
427 1.2.6.2 nathanw
428 1.2.6.2 nathanw /* Acknowledge the interrupt. */
429 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & ((EC_CSR_BINT | EC_CSR_AINT | EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK) ^ (recv_first | recv_second)));
430 1.2.6.2 nathanw
431 1.2.6.2 nathanw /* Receive a packet. */
432 1.2.6.2 nathanw ec_recv(sc, recv_first);
433 1.2.6.2 nathanw
434 1.2.6.2 nathanw /* Receive a packet. */
435 1.2.6.2 nathanw if (recv_second)
436 1.2.6.2 nathanw ec_recv(sc, recv_second);
437 1.2.6.2 nathanw
438 1.2.6.2 nathanw retval++;
439 1.2.6.2 nathanw }
440 1.2.6.2 nathanw /* Check for a transmitted packet. */
441 1.2.6.2 nathanw if (ifp->if_flags & IFF_OACTIVE) {
442 1.2.6.2 nathanw
443 1.2.6.2 nathanw /* If we got a collision. */
444 1.2.6.2 nathanw if (ECREG_CSR_RD & EC_CSR_JAM) {
445 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
446 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_collisions++;
447 1.2.6.2 nathanw retval++;
448 1.2.6.2 nathanw ec_coll(sc);
449 1.2.6.2 nathanw
450 1.2.6.2 nathanw }
451 1.2.6.2 nathanw /* If we transmitted a packet. */
452 1.2.6.2 nathanw else if ((ECREG_CSR_RD & EC_CSR_TBSW) == 0) {
453 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
454 1.2.6.2 nathanw retval++;
455 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_opackets++;
456 1.2.6.2 nathanw sc->sc_jammed = 0;
457 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
458 1.2.6.2 nathanw IFQ_POLL(&ifp->if_snd, m0);
459 1.2.6.2 nathanw if (m0 != NULL)
460 1.2.6.2 nathanw ec_start(ifp);
461 1.2.6.2 nathanw }
462 1.2.6.2 nathanw } else {
463 1.2.6.2 nathanw
464 1.2.6.2 nathanw /* Make sure we disable transmitter interrupts. */
465 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
466 1.2.6.2 nathanw }
467 1.2.6.2 nathanw
468 1.2.6.2 nathanw return retval;
469 1.2.6.2 nathanw }
470 1.2.6.2 nathanw
471 1.2.6.2 nathanw /*
472 1.2.6.2 nathanw * Read in a packet from the board.
473 1.2.6.2 nathanw */
474 1.2.6.2 nathanw void
475 1.2.6.2 nathanw ec_recv(sc, intbit)
476 1.2.6.2 nathanw struct ec_softc *sc;
477 1.2.6.2 nathanw int intbit;
478 1.2.6.2 nathanw {
479 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
480 1.2.6.2 nathanw struct mbuf *m0, *m, *newm;
481 1.2.6.2 nathanw bus_size_t buf;
482 1.2.6.2 nathanw u_int16_t status;
483 1.2.6.2 nathanw u_int16_t doff;
484 1.2.6.2 nathanw int length, total_length;
485 1.2.6.2 nathanw
486 1.2.6.2 nathanw buf = EC_CSR_INT_BUF(intbit);
487 1.2.6.2 nathanw
488 1.2.6.2 nathanw /* Read in the packet status. */
489 1.2.6.2 nathanw status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
490 1.2.6.2 nathanw doff = status & EC_PKT_DOFF;
491 1.2.6.2 nathanw
492 1.2.6.2 nathanw for (total_length = -1, m0 = 0;;) {
493 1.2.6.2 nathanw
494 1.2.6.2 nathanw /* Check for an error. */
495 1.2.6.2 nathanw if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
496 1.2.6.2 nathanw doff < EC_PKT_MINRDOFF ||
497 1.2.6.2 nathanw doff > EC_PKT_MAXRDOFF) {
498 1.2.6.2 nathanw printf("%s: garbled packet, status 0x%04x; dropping\n",
499 1.2.6.2 nathanw sc->sc_dev.dv_xname, (unsigned int) status);
500 1.2.6.2 nathanw break;
501 1.2.6.2 nathanw }
502 1.2.6.2 nathanw
503 1.2.6.2 nathanw /* Adjust for the header. */
504 1.2.6.2 nathanw total_length = doff - EC_PKT_RDOFF;
505 1.2.6.2 nathanw buf += EC_PKT_RDOFF;
506 1.2.6.2 nathanw
507 1.2.6.2 nathanw /* XXX - sometimes the card reports a large data offset. */
508 1.2.6.2 nathanw if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
509 1.2.6.2 nathanw #ifdef DEBUG
510 1.2.6.2 nathanw printf("%s: fixing too-large length of %d\n",
511 1.2.6.2 nathanw sc->sc_dev.dv_xname, total_length);
512 1.2.6.2 nathanw #endif
513 1.2.6.2 nathanw total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
514 1.2.6.2 nathanw }
515 1.2.6.2 nathanw
516 1.2.6.2 nathanw MGETHDR(m0, M_DONTWAIT, MT_DATA);
517 1.2.6.2 nathanw if (m0 == 0)
518 1.2.6.2 nathanw break;
519 1.2.6.2 nathanw m0->m_pkthdr.rcvif = ifp;
520 1.2.6.2 nathanw m0->m_pkthdr.len = total_length;
521 1.2.6.2 nathanw length = MHLEN;
522 1.2.6.2 nathanw m = m0;
523 1.2.6.2 nathanw
524 1.2.6.2 nathanw while (total_length > 0) {
525 1.2.6.2 nathanw if (total_length >= MINCLSIZE) {
526 1.2.6.2 nathanw MCLGET(m, M_DONTWAIT);
527 1.2.6.2 nathanw if ((m->m_flags & M_EXT) == 0)
528 1.2.6.2 nathanw break;
529 1.2.6.2 nathanw length = MCLBYTES;
530 1.2.6.2 nathanw }
531 1.2.6.2 nathanw m->m_len = length = min(total_length, length);
532 1.2.6.2 nathanw ec_copyin(sc, mtod(m, u_int8_t *), buf, length);
533 1.2.6.2 nathanw total_length -= length;
534 1.2.6.2 nathanw buf += length;
535 1.2.6.2 nathanw
536 1.2.6.2 nathanw if (total_length > 0) {
537 1.2.6.2 nathanw MGET(newm, M_DONTWAIT, MT_DATA);
538 1.2.6.2 nathanw if (newm == 0)
539 1.2.6.2 nathanw break;
540 1.2.6.2 nathanw length = MLEN;
541 1.2.6.2 nathanw m = m->m_next = newm;
542 1.2.6.2 nathanw }
543 1.2.6.2 nathanw }
544 1.2.6.2 nathanw break;
545 1.2.6.2 nathanw }
546 1.2.6.2 nathanw
547 1.2.6.2 nathanw if (total_length == 0) {
548 1.2.6.2 nathanw ifp->if_ipackets++;
549 1.2.6.2 nathanw
550 1.2.6.2 nathanw #if NBPFILTER > 0
551 1.2.6.2 nathanw /*
552 1.2.6.2 nathanw * Check if there's a BPF listener on this interface.
553 1.2.6.2 nathanw * If so, hand off the raw packet to BPF.
554 1.2.6.2 nathanw */
555 1.2.6.2 nathanw if (ifp->if_bpf)
556 1.2.6.2 nathanw bpf_mtap(ifp->if_bpf, m0);
557 1.2.6.2 nathanw #endif
558 1.2.6.2 nathanw
559 1.2.6.2 nathanw /* Pass the packet up. */
560 1.2.6.2 nathanw (*ifp->if_input) (ifp, m0);
561 1.2.6.2 nathanw
562 1.2.6.2 nathanw } else {
563 1.2.6.2 nathanw /* Something went wrong. */
564 1.2.6.2 nathanw if (m0)
565 1.2.6.2 nathanw m_freem(m0);
566 1.2.6.2 nathanw ifp->if_ierrors++;
567 1.2.6.2 nathanw }
568 1.2.6.2 nathanw
569 1.2.6.2 nathanw /* Give the receive buffer back to the card. */
570 1.2.6.2 nathanw buf = EC_CSR_INT_BUF(intbit);
571 1.2.6.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
572 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_INT_BSW(intbit) | intbit);
573 1.2.6.2 nathanw }
574 1.2.6.2 nathanw
575 1.2.6.2 nathanw int
576 1.2.6.2 nathanw ec_mediachange(ifp)
577 1.2.6.2 nathanw struct ifnet *ifp;
578 1.2.6.2 nathanw {
579 1.2.6.2 nathanw return (0);
580 1.2.6.2 nathanw }
581 1.2.6.2 nathanw
582 1.2.6.2 nathanw void
583 1.2.6.2 nathanw ec_mediastatus(ifp, ifmr)
584 1.2.6.2 nathanw struct ifnet *ifp;
585 1.2.6.2 nathanw struct ifmediareq *ifmr;
586 1.2.6.2 nathanw {
587 1.2.6.2 nathanw if ((ifp->if_flags & IFF_UP) == 0)
588 1.2.6.2 nathanw return;
589 1.2.6.2 nathanw
590 1.2.6.2 nathanw ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
591 1.2.6.2 nathanw }
592 1.2.6.2 nathanw
593 1.2.6.2 nathanw /*
594 1.2.6.2 nathanw * Process an ioctl request. This code needs some work - it looks pretty ugly.
595 1.2.6.2 nathanw */
596 1.2.6.2 nathanw int
597 1.2.6.2 nathanw ec_ioctl(ifp, cmd, data)
598 1.2.6.2 nathanw struct ifnet *ifp;
599 1.2.6.2 nathanw u_long cmd;
600 1.2.6.2 nathanw caddr_t data;
601 1.2.6.2 nathanw {
602 1.2.6.2 nathanw struct ifaddr *ifa = (struct ifaddr *) data;
603 1.2.6.2 nathanw struct ifreq *ifr = (struct ifreq *)data;
604 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
605 1.2.6.2 nathanw int s, error = 0;
606 1.2.6.2 nathanw
607 1.2.6.2 nathanw s = splnet();
608 1.2.6.2 nathanw
609 1.2.6.2 nathanw switch (cmd) {
610 1.2.6.2 nathanw
611 1.2.6.2 nathanw case SIOCSIFADDR:
612 1.2.6.2 nathanw ifp->if_flags |= IFF_UP;
613 1.2.6.2 nathanw
614 1.2.6.2 nathanw switch (ifa->ifa_addr->sa_family) {
615 1.2.6.2 nathanw #ifdef INET
616 1.2.6.2 nathanw case AF_INET:
617 1.2.6.2 nathanw ec_init(ifp);
618 1.2.6.2 nathanw arp_ifinit(ifp, ifa);
619 1.2.6.2 nathanw break;
620 1.2.6.2 nathanw #endif
621 1.2.6.2 nathanw #ifdef NS
622 1.2.6.2 nathanw /* XXX - This code is probably wrong. */
623 1.2.6.2 nathanw case AF_NS:
624 1.2.6.2 nathanw {
625 1.2.6.2 nathanw struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
626 1.2.6.2 nathanw
627 1.2.6.2 nathanw if (ns_nullhost(*ina))
628 1.2.6.2 nathanw ina->x_host =
629 1.2.6.2 nathanw *(union ns_host *) LLADDR(ifp->if_sadl);
630 1.2.6.2 nathanw else
631 1.2.6.2 nathanw bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
632 1.2.6.2 nathanw ETHER_ADDR_LEN);
633 1.2.6.2 nathanw /* Set new address. */
634 1.2.6.2 nathanw ec_init(ifp);
635 1.2.6.2 nathanw break;
636 1.2.6.2 nathanw }
637 1.2.6.2 nathanw #endif
638 1.2.6.2 nathanw default:
639 1.2.6.2 nathanw ec_init(ifp);
640 1.2.6.2 nathanw break;
641 1.2.6.2 nathanw }
642 1.2.6.2 nathanw break;
643 1.2.6.2 nathanw
644 1.2.6.2 nathanw case SIOCSIFFLAGS:
645 1.2.6.2 nathanw if ((ifp->if_flags & IFF_UP) == 0 &&
646 1.2.6.2 nathanw (ifp->if_flags & IFF_RUNNING) != 0) {
647 1.2.6.2 nathanw /*
648 1.2.6.2 nathanw * If interface is marked down and it is running, then
649 1.2.6.2 nathanw * stop it.
650 1.2.6.2 nathanw */
651 1.2.6.2 nathanw ifp->if_flags &= ~IFF_RUNNING;
652 1.2.6.2 nathanw } else if ((ifp->if_flags & IFF_UP) != 0 &&
653 1.2.6.2 nathanw (ifp->if_flags & IFF_RUNNING) == 0) {
654 1.2.6.2 nathanw /*
655 1.2.6.2 nathanw * If interface is marked up and it is stopped, then
656 1.2.6.2 nathanw * start it.
657 1.2.6.2 nathanw */
658 1.2.6.2 nathanw ec_init(ifp);
659 1.2.6.2 nathanw } else {
660 1.2.6.2 nathanw /*
661 1.2.6.2 nathanw * Some other important flag might have changed, so
662 1.2.6.2 nathanw * reset.
663 1.2.6.2 nathanw */
664 1.2.6.2 nathanw ec_reset(ifp);
665 1.2.6.2 nathanw }
666 1.2.6.2 nathanw break;
667 1.2.6.2 nathanw
668 1.2.6.2 nathanw case SIOCGIFMEDIA:
669 1.2.6.2 nathanw case SIOCSIFMEDIA:
670 1.2.6.2 nathanw error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
671 1.2.6.2 nathanw break;
672 1.2.6.2 nathanw
673 1.2.6.2 nathanw default:
674 1.2.6.2 nathanw error = EINVAL;
675 1.2.6.2 nathanw break;
676 1.2.6.2 nathanw }
677 1.2.6.2 nathanw
678 1.2.6.2 nathanw splx(s);
679 1.2.6.2 nathanw return error;
680 1.2.6.2 nathanw }
681 1.2.6.2 nathanw
682 1.2.6.2 nathanw /*
683 1.2.6.2 nathanw * Collision routine.
684 1.2.6.2 nathanw */
685 1.2.6.2 nathanw void
686 1.2.6.2 nathanw ec_coll(sc)
687 1.2.6.2 nathanw struct ec_softc *sc;
688 1.2.6.2 nathanw {
689 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
690 1.2.6.2 nathanw u_short jams;
691 1.2.6.2 nathanw struct mbuf *m0;
692 1.2.6.2 nathanw
693 1.2.6.2 nathanw if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
694 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_oerrors++;
695 1.2.6.2 nathanw if (!sc->sc_jammed)
696 1.2.6.2 nathanw printf("%s: ethernet jammed\n",
697 1.2.6.2 nathanw sc->sc_dev.dv_xname);
698 1.2.6.2 nathanw sc->sc_jammed = 1;
699 1.2.6.2 nathanw sc->sc_colliding = 0;
700 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
701 1.2.6.2 nathanw IFQ_POLL(&ifp->if_snd, m0);
702 1.2.6.2 nathanw if (m0 != NULL)
703 1.2.6.2 nathanw ec_start(ifp);
704 1.2.6.2 nathanw } else {
705 1.2.6.2 nathanw jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
706 1.2.6.2 nathanw sc->sc_backoff_seed = ((sc->sc_backoff_seed * EC_BACKOFF_PRNG_MUL) + EC_BACKOFF_PRNG_ADD) & EC_BACKOFF_PRNG_MASK;
707 1.2.6.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_BACKOFF, -(((sc->sc_backoff_seed >> 8) & ~(-1 << jams)) + 1));
708 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
709 1.2.6.2 nathanw }
710 1.2.6.2 nathanw }
711 1.2.6.2 nathanw
712 1.2.6.2 nathanw /*
713 1.2.6.2 nathanw * Device timeout routine.
714 1.2.6.2 nathanw */
715 1.2.6.2 nathanw void
716 1.2.6.2 nathanw ec_watchdog(ifp)
717 1.2.6.2 nathanw struct ifnet *ifp;
718 1.2.6.2 nathanw {
719 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
720 1.2.6.2 nathanw
721 1.2.6.2 nathanw log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
722 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_oerrors++;
723 1.2.6.2 nathanw
724 1.2.6.2 nathanw ec_reset(ifp);
725 1.2.6.2 nathanw }
726