if_ec.c revision 1.2.6.2 1 1.2.6.2 nathanw /* $NetBSD: if_ec.c,v 1.2.6.2 2002/01/08 00:28:08 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.2 nathanw struct cfattach ec_ca = {
154 1.2.6.2 nathanw sizeof(struct ec_softc), ec_match, ec_attach
155 1.2.6.2 nathanw };
156 1.2.6.2 nathanw
157 1.2.6.2 nathanw /*
158 1.2.6.2 nathanw * Copy board memory to kernel.
159 1.2.6.2 nathanw */
160 1.2.6.2 nathanw void
161 1.2.6.2 nathanw ec_copyin(sc, p, offset, size)
162 1.2.6.2 nathanw struct ec_softc *sc;
163 1.2.6.2 nathanw void *p;
164 1.2.6.2 nathanw int offset;
165 1.2.6.2 nathanw size_t size;
166 1.2.6.2 nathanw {
167 1.2.6.2 nathanw bus_space_copyin(sc->sc_iot, sc->sc_ioh, offset, p, size);
168 1.2.6.2 nathanw }
169 1.2.6.2 nathanw
170 1.2.6.2 nathanw /*
171 1.2.6.2 nathanw * Copy from kernel space to board memory.
172 1.2.6.2 nathanw */
173 1.2.6.2 nathanw void
174 1.2.6.2 nathanw ec_copyout(sc, p, offset, size)
175 1.2.6.2 nathanw struct ec_softc *sc;
176 1.2.6.2 nathanw const void *p;
177 1.2.6.2 nathanw int offset;
178 1.2.6.2 nathanw size_t size;
179 1.2.6.2 nathanw {
180 1.2.6.2 nathanw bus_space_copyout(sc->sc_iot, sc->sc_ioh, offset, p, size);
181 1.2.6.2 nathanw }
182 1.2.6.2 nathanw
183 1.2.6.2 nathanw int
184 1.2.6.2 nathanw ec_match(parent, match, aux)
185 1.2.6.2 nathanw struct device *parent;
186 1.2.6.2 nathanw struct cfdata *match;
187 1.2.6.2 nathanw void *aux;
188 1.2.6.2 nathanw {
189 1.2.6.2 nathanw struct mbmem_attach_args *mbma = aux;
190 1.2.6.2 nathanw bus_space_handle_t bh;
191 1.2.6.2 nathanw int matched;
192 1.2.6.2 nathanw
193 1.2.6.2 nathanw /* No default Multibus address. */
194 1.2.6.2 nathanw if (mbma->mbma_paddr == -1)
195 1.2.6.2 nathanw return (0);
196 1.2.6.2 nathanw
197 1.2.6.2 nathanw /* Make sure there is something there... */
198 1.2.6.2 nathanw if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
199 1.2.6.2 nathanw 0, &bh))
200 1.2.6.2 nathanw return (0);
201 1.2.6.2 nathanw matched = (bus_space_peek_2(mbma->mbma_bustag, bh, 0, NULL) == 0);
202 1.2.6.2 nathanw bus_space_unmap(mbma->mbma_bustag, bh, ECREG_BANK_SZ);
203 1.2.6.2 nathanw if (!matched)
204 1.2.6.2 nathanw return (0);
205 1.2.6.2 nathanw
206 1.2.6.2 nathanw /* Default interrupt priority. */
207 1.2.6.2 nathanw if (mbma->mbma_pri == -1)
208 1.2.6.2 nathanw mbma->mbma_pri = 3;
209 1.2.6.2 nathanw
210 1.2.6.2 nathanw return (1);
211 1.2.6.2 nathanw }
212 1.2.6.2 nathanw
213 1.2.6.2 nathanw void
214 1.2.6.2 nathanw ec_attach(parent, self, aux)
215 1.2.6.2 nathanw struct device *parent, *self;
216 1.2.6.2 nathanw void *aux;
217 1.2.6.2 nathanw {
218 1.2.6.2 nathanw struct ec_softc *sc = (void *) self;
219 1.2.6.2 nathanw struct mbmem_attach_args *mbma = aux;
220 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
221 1.2.6.2 nathanw u_int8_t myaddr[ETHER_ADDR_LEN];
222 1.2.6.2 nathanw
223 1.2.6.2 nathanw printf("\n");
224 1.2.6.2 nathanw
225 1.2.6.2 nathanw /* Map in the board control regs. */
226 1.2.6.2 nathanw sc->sc_iot = mbma->mbma_bustag;
227 1.2.6.2 nathanw if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
228 1.2.6.2 nathanw 0, &sc->sc_ioh))
229 1.2.6.2 nathanw panic("ec_attach: can't map regs");
230 1.2.6.2 nathanw
231 1.2.6.2 nathanw /* Reset the board. */
232 1.2.6.2 nathanw ECREG_CSR_WR(EC_CSR_RESET);
233 1.2.6.2 nathanw delay(160);
234 1.2.6.2 nathanw
235 1.2.6.2 nathanw /*
236 1.2.6.2 nathanw * Copy out the board ROM Ethernet address,
237 1.2.6.2 nathanw * and use the non-vendor-ID part to seed
238 1.2.6.2 nathanw * our backoff pseudorandom number generator.
239 1.2.6.2 nathanw */
240 1.2.6.2 nathanw bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, ECREG_AROM, myaddr, ETHER_ADDR_LEN);
241 1.2.6.2 nathanw sc->sc_backoff_seed = (myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
242 1.2.6.2 nathanw
243 1.2.6.2 nathanw /* Initialize ifnet structure. */
244 1.2.6.2 nathanw bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
245 1.2.6.2 nathanw ifp->if_softc = sc;
246 1.2.6.2 nathanw ifp->if_start = ec_start;
247 1.2.6.2 nathanw ifp->if_ioctl = ec_ioctl;
248 1.2.6.2 nathanw ifp->if_init = ec_init;
249 1.2.6.2 nathanw ifp->if_watchdog = ec_watchdog;
250 1.2.6.2 nathanw ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
251 1.2.6.2 nathanw IFQ_SET_READY(&ifp->if_snd);
252 1.2.6.2 nathanw
253 1.2.6.2 nathanw /* Initialize ifmedia structures. */
254 1.2.6.2 nathanw ifmedia_init(&sc->sc_media, 0, ec_mediachange, ec_mediastatus);
255 1.2.6.2 nathanw ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
256 1.2.6.2 nathanw ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
257 1.2.6.2 nathanw
258 1.2.6.2 nathanw /* Now we can attach the interface. */
259 1.2.6.2 nathanw if_attach(ifp);
260 1.2.6.2 nathanw idprom_etheraddr(myaddr);
261 1.2.6.2 nathanw ether_ifattach(ifp, myaddr);
262 1.2.6.2 nathanw printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
263 1.2.6.2 nathanw
264 1.2.6.2 nathanw bus_intr_establish(mbma->mbma_bustag, mbma->mbma_pri, IPL_NET, 0,
265 1.2.6.2 nathanw ec_intr, sc);
266 1.2.6.2 nathanw
267 1.2.6.2 nathanw #if NRND > 0
268 1.2.6.2 nathanw rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
269 1.2.6.2 nathanw RND_TYPE_NET, 0);
270 1.2.6.2 nathanw #endif
271 1.2.6.2 nathanw }
272 1.2.6.2 nathanw
273 1.2.6.2 nathanw /*
274 1.2.6.2 nathanw * Reset interface.
275 1.2.6.2 nathanw */
276 1.2.6.2 nathanw void
277 1.2.6.2 nathanw ec_reset(ifp)
278 1.2.6.2 nathanw struct ifnet *ifp;
279 1.2.6.2 nathanw {
280 1.2.6.2 nathanw int s;
281 1.2.6.2 nathanw
282 1.2.6.2 nathanw s = splnet();
283 1.2.6.2 nathanw ec_init(ifp);
284 1.2.6.2 nathanw splx(s);
285 1.2.6.2 nathanw }
286 1.2.6.2 nathanw
287 1.2.6.2 nathanw
288 1.2.6.2 nathanw /*
289 1.2.6.2 nathanw * Initialize interface.
290 1.2.6.2 nathanw */
291 1.2.6.2 nathanw int
292 1.2.6.2 nathanw ec_init(ifp)
293 1.2.6.2 nathanw struct ifnet *ifp;
294 1.2.6.2 nathanw {
295 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
296 1.2.6.2 nathanw
297 1.2.6.2 nathanw /* Reset the board. */
298 1.2.6.2 nathanw ECREG_CSR_WR(EC_CSR_RESET);
299 1.2.6.2 nathanw delay(160);
300 1.2.6.2 nathanw
301 1.2.6.2 nathanw /* Set the Ethernet address. */
302 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);
303 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_AMSW);
304 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & 0);
305 1.2.6.2 nathanw
306 1.2.6.2 nathanw /* Enable interrupts. */
307 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));
308 1.2.6.2 nathanw
309 1.2.6.2 nathanw /* Set flags appropriately. */
310 1.2.6.2 nathanw ifp->if_flags |= IFF_RUNNING;
311 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
312 1.2.6.2 nathanw
313 1.2.6.2 nathanw /* Start output. */
314 1.2.6.2 nathanw ec_start(ifp);
315 1.2.6.2 nathanw
316 1.2.6.2 nathanw return (0);
317 1.2.6.2 nathanw }
318 1.2.6.2 nathanw
319 1.2.6.2 nathanw /*
320 1.2.6.2 nathanw * Start output on interface.
321 1.2.6.2 nathanw */
322 1.2.6.2 nathanw void
323 1.2.6.2 nathanw ec_start(ifp)
324 1.2.6.2 nathanw struct ifnet *ifp;
325 1.2.6.2 nathanw {
326 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
327 1.2.6.2 nathanw struct mbuf *m, *m0;
328 1.2.6.2 nathanw int s;
329 1.2.6.2 nathanw u_int count;
330 1.2.6.2 nathanw bus_size_t off;
331 1.2.6.2 nathanw
332 1.2.6.2 nathanw s = splnet();
333 1.2.6.2 nathanw
334 1.2.6.2 nathanw /* Don't do anything if output is active. */
335 1.2.6.2 nathanw if ((ifp->if_flags & IFF_OACTIVE) != 0) {
336 1.2.6.2 nathanw splx(s);
337 1.2.6.2 nathanw return;
338 1.2.6.2 nathanw }
339 1.2.6.2 nathanw /* Don't do anything if the output queue is empty. */
340 1.2.6.2 nathanw IFQ_DEQUEUE(&ifp->if_snd, m0);
341 1.2.6.2 nathanw if (m0 == NULL) {
342 1.2.6.2 nathanw splx(s);
343 1.2.6.2 nathanw return;
344 1.2.6.2 nathanw }
345 1.2.6.2 nathanw
346 1.2.6.2 nathanw #if NBPFILTER > 0
347 1.2.6.2 nathanw /* The BPF tap. */
348 1.2.6.2 nathanw if (ifp->if_bpf)
349 1.2.6.2 nathanw bpf_mtap(ifp->if_bpf, m0);
350 1.2.6.2 nathanw #endif
351 1.2.6.2 nathanw
352 1.2.6.2 nathanw /* Size the packet. */
353 1.2.6.2 nathanw for (count = EC_BUF_SZ, m = m0; m != NULL; m = m->m_next)
354 1.2.6.2 nathanw count -= m->m_len;
355 1.2.6.2 nathanw
356 1.2.6.2 nathanw /* Copy the packet into the xmit buffer. */
357 1.2.6.2 nathanw count = MIN(count, EC_PKT_MAXTDOFF);
358 1.2.6.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, count);
359 1.2.6.2 nathanw for (off = count, m = m0; m != 0; off += m->m_len, m = m->m_next)
360 1.2.6.2 nathanw ec_copyout(sc, mtod(m, u_int8_t *), ECREG_TBUF + off, m->m_len);
361 1.2.6.2 nathanw m_freem(m0);
362 1.2.6.2 nathanw
363 1.2.6.2 nathanw /* Enable the transmitter. */
364 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_PA) | EC_CSR_TBSW | EC_CSR_TINT | EC_CSR_JINT);
365 1.2.6.2 nathanw ifp->if_flags |= IFF_OACTIVE;
366 1.2.6.2 nathanw
367 1.2.6.2 nathanw /* Done. */
368 1.2.6.2 nathanw splx(s);
369 1.2.6.2 nathanw }
370 1.2.6.2 nathanw
371 1.2.6.2 nathanw /*
372 1.2.6.2 nathanw * Controller interrupt.
373 1.2.6.2 nathanw */
374 1.2.6.2 nathanw int
375 1.2.6.2 nathanw ec_intr(arg)
376 1.2.6.2 nathanw void *arg;
377 1.2.6.2 nathanw {
378 1.2.6.2 nathanw struct ec_softc *sc = arg;
379 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
380 1.2.6.2 nathanw int recv_first;
381 1.2.6.2 nathanw int recv_second;
382 1.2.6.2 nathanw int retval;
383 1.2.6.2 nathanw struct mbuf *m0;
384 1.2.6.2 nathanw
385 1.2.6.2 nathanw retval = 0;
386 1.2.6.2 nathanw
387 1.2.6.2 nathanw /* Check for received packet(s). */
388 1.2.6.2 nathanw recv_first = recv_second = 0;
389 1.2.6.2 nathanw switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
390 1.2.6.2 nathanw
391 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_ABSW):
392 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
393 1.2.6.2 nathanw /* Neither buffer is full. Is this a transmit interrupt?
394 1.2.6.2 nathanw * Acknowledge the interrupt ourselves. */
395 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
396 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_BINT | EC_CSR_AINT);
397 1.2.6.2 nathanw break;
398 1.2.6.2 nathanw
399 1.2.6.2 nathanw case EC_CSR_BBSW:
400 1.2.6.2 nathanw case (EC_CSR_BBSW | EC_CSR_RBBA):
401 1.2.6.2 nathanw /* Only the A buffer is full. */
402 1.2.6.2 nathanw recv_first = EC_CSR_AINT;
403 1.2.6.2 nathanw break;
404 1.2.6.2 nathanw
405 1.2.6.2 nathanw case EC_CSR_ABSW:
406 1.2.6.2 nathanw case (EC_CSR_ABSW | EC_CSR_RBBA):
407 1.2.6.2 nathanw /* Only the B buffer is full. */
408 1.2.6.2 nathanw recv_first = EC_CSR_BINT;
409 1.2.6.2 nathanw break;
410 1.2.6.2 nathanw
411 1.2.6.2 nathanw case 0:
412 1.2.6.2 nathanw /* Both the A buffer and the B buffer are full, and the A
413 1.2.6.2 nathanw * buffer is older than the B buffer. */
414 1.2.6.2 nathanw recv_first = EC_CSR_AINT;
415 1.2.6.2 nathanw recv_second = EC_CSR_BINT;
416 1.2.6.2 nathanw break;
417 1.2.6.2 nathanw
418 1.2.6.2 nathanw case EC_CSR_RBBA:
419 1.2.6.2 nathanw /* Both the A buffer and the B buffer are full, and the B
420 1.2.6.2 nathanw * buffer is older than the A buffer. */
421 1.2.6.2 nathanw recv_first = EC_CSR_BINT;
422 1.2.6.2 nathanw recv_second = EC_CSR_AINT;
423 1.2.6.2 nathanw break;
424 1.2.6.2 nathanw }
425 1.2.6.2 nathanw
426 1.2.6.2 nathanw /* Receive packets. */
427 1.2.6.2 nathanw if (recv_first) {
428 1.2.6.2 nathanw
429 1.2.6.2 nathanw /* Acknowledge the interrupt. */
430 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)));
431 1.2.6.2 nathanw
432 1.2.6.2 nathanw /* Receive a packet. */
433 1.2.6.2 nathanw ec_recv(sc, recv_first);
434 1.2.6.2 nathanw
435 1.2.6.2 nathanw /* Receive a packet. */
436 1.2.6.2 nathanw if (recv_second)
437 1.2.6.2 nathanw ec_recv(sc, recv_second);
438 1.2.6.2 nathanw
439 1.2.6.2 nathanw retval++;
440 1.2.6.2 nathanw }
441 1.2.6.2 nathanw /* Check for a transmitted packet. */
442 1.2.6.2 nathanw if (ifp->if_flags & IFF_OACTIVE) {
443 1.2.6.2 nathanw
444 1.2.6.2 nathanw /* If we got a collision. */
445 1.2.6.2 nathanw if (ECREG_CSR_RD & EC_CSR_JAM) {
446 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
447 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_collisions++;
448 1.2.6.2 nathanw retval++;
449 1.2.6.2 nathanw ec_coll(sc);
450 1.2.6.2 nathanw
451 1.2.6.2 nathanw }
452 1.2.6.2 nathanw /* If we transmitted a packet. */
453 1.2.6.2 nathanw else if ((ECREG_CSR_RD & EC_CSR_TBSW) == 0) {
454 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
455 1.2.6.2 nathanw retval++;
456 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_opackets++;
457 1.2.6.2 nathanw sc->sc_jammed = 0;
458 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
459 1.2.6.2 nathanw IFQ_POLL(&ifp->if_snd, m0);
460 1.2.6.2 nathanw if (m0 != NULL)
461 1.2.6.2 nathanw ec_start(ifp);
462 1.2.6.2 nathanw }
463 1.2.6.2 nathanw } else {
464 1.2.6.2 nathanw
465 1.2.6.2 nathanw /* Make sure we disable transmitter interrupts. */
466 1.2.6.2 nathanw ECREG_CSR_WR(ECREG_CSR_RD & (EC_CSR_BINT | EC_CSR_AINT | EC_CSR_PAMASK));
467 1.2.6.2 nathanw }
468 1.2.6.2 nathanw
469 1.2.6.2 nathanw return retval;
470 1.2.6.2 nathanw }
471 1.2.6.2 nathanw
472 1.2.6.2 nathanw /*
473 1.2.6.2 nathanw * Read in a packet from the board.
474 1.2.6.2 nathanw */
475 1.2.6.2 nathanw void
476 1.2.6.2 nathanw ec_recv(sc, intbit)
477 1.2.6.2 nathanw struct ec_softc *sc;
478 1.2.6.2 nathanw int intbit;
479 1.2.6.2 nathanw {
480 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
481 1.2.6.2 nathanw struct mbuf *m0, *m, *newm;
482 1.2.6.2 nathanw bus_size_t buf;
483 1.2.6.2 nathanw u_int16_t status;
484 1.2.6.2 nathanw u_int16_t doff;
485 1.2.6.2 nathanw int length, total_length;
486 1.2.6.2 nathanw
487 1.2.6.2 nathanw buf = EC_CSR_INT_BUF(intbit);
488 1.2.6.2 nathanw
489 1.2.6.2 nathanw /* Read in the packet status. */
490 1.2.6.2 nathanw status = bus_space_read_2(sc->sc_iot, sc->sc_ioh, buf);
491 1.2.6.2 nathanw doff = status & EC_PKT_DOFF;
492 1.2.6.2 nathanw
493 1.2.6.2 nathanw for (total_length = -1, m0 = 0;;) {
494 1.2.6.2 nathanw
495 1.2.6.2 nathanw /* Check for an error. */
496 1.2.6.2 nathanw if (status & (EC_PKT_FCSERR | EC_PKT_RGERR | EC_PKT_FRERR) ||
497 1.2.6.2 nathanw doff < EC_PKT_MINRDOFF ||
498 1.2.6.2 nathanw doff > EC_PKT_MAXRDOFF) {
499 1.2.6.2 nathanw printf("%s: garbled packet, status 0x%04x; dropping\n",
500 1.2.6.2 nathanw sc->sc_dev.dv_xname, (unsigned int) status);
501 1.2.6.2 nathanw break;
502 1.2.6.2 nathanw }
503 1.2.6.2 nathanw
504 1.2.6.2 nathanw /* Adjust for the header. */
505 1.2.6.2 nathanw total_length = doff - EC_PKT_RDOFF;
506 1.2.6.2 nathanw buf += EC_PKT_RDOFF;
507 1.2.6.2 nathanw
508 1.2.6.2 nathanw /* XXX - sometimes the card reports a large data offset. */
509 1.2.6.2 nathanw if (total_length > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
510 1.2.6.2 nathanw #ifdef DEBUG
511 1.2.6.2 nathanw printf("%s: fixing too-large length of %d\n",
512 1.2.6.2 nathanw sc->sc_dev.dv_xname, total_length);
513 1.2.6.2 nathanw #endif
514 1.2.6.2 nathanw total_length = (ETHER_MAX_LEN - ETHER_CRC_LEN);
515 1.2.6.2 nathanw }
516 1.2.6.2 nathanw
517 1.2.6.2 nathanw MGETHDR(m0, M_DONTWAIT, MT_DATA);
518 1.2.6.2 nathanw if (m0 == 0)
519 1.2.6.2 nathanw break;
520 1.2.6.2 nathanw m0->m_pkthdr.rcvif = ifp;
521 1.2.6.2 nathanw m0->m_pkthdr.len = total_length;
522 1.2.6.2 nathanw length = MHLEN;
523 1.2.6.2 nathanw m = m0;
524 1.2.6.2 nathanw
525 1.2.6.2 nathanw while (total_length > 0) {
526 1.2.6.2 nathanw if (total_length >= MINCLSIZE) {
527 1.2.6.2 nathanw MCLGET(m, M_DONTWAIT);
528 1.2.6.2 nathanw if ((m->m_flags & M_EXT) == 0)
529 1.2.6.2 nathanw break;
530 1.2.6.2 nathanw length = MCLBYTES;
531 1.2.6.2 nathanw }
532 1.2.6.2 nathanw m->m_len = length = min(total_length, length);
533 1.2.6.2 nathanw ec_copyin(sc, mtod(m, u_int8_t *), buf, length);
534 1.2.6.2 nathanw total_length -= length;
535 1.2.6.2 nathanw buf += length;
536 1.2.6.2 nathanw
537 1.2.6.2 nathanw if (total_length > 0) {
538 1.2.6.2 nathanw MGET(newm, M_DONTWAIT, MT_DATA);
539 1.2.6.2 nathanw if (newm == 0)
540 1.2.6.2 nathanw break;
541 1.2.6.2 nathanw length = MLEN;
542 1.2.6.2 nathanw m = m->m_next = newm;
543 1.2.6.2 nathanw }
544 1.2.6.2 nathanw }
545 1.2.6.2 nathanw break;
546 1.2.6.2 nathanw }
547 1.2.6.2 nathanw
548 1.2.6.2 nathanw if (total_length == 0) {
549 1.2.6.2 nathanw ifp->if_ipackets++;
550 1.2.6.2 nathanw
551 1.2.6.2 nathanw #if NBPFILTER > 0
552 1.2.6.2 nathanw /*
553 1.2.6.2 nathanw * Check if there's a BPF listener on this interface.
554 1.2.6.2 nathanw * If so, hand off the raw packet to BPF.
555 1.2.6.2 nathanw */
556 1.2.6.2 nathanw if (ifp->if_bpf)
557 1.2.6.2 nathanw bpf_mtap(ifp->if_bpf, m0);
558 1.2.6.2 nathanw #endif
559 1.2.6.2 nathanw
560 1.2.6.2 nathanw /* Pass the packet up. */
561 1.2.6.2 nathanw (*ifp->if_input) (ifp, m0);
562 1.2.6.2 nathanw
563 1.2.6.2 nathanw } else {
564 1.2.6.2 nathanw /* Something went wrong. */
565 1.2.6.2 nathanw if (m0)
566 1.2.6.2 nathanw m_freem(m0);
567 1.2.6.2 nathanw ifp->if_ierrors++;
568 1.2.6.2 nathanw }
569 1.2.6.2 nathanw
570 1.2.6.2 nathanw /* Give the receive buffer back to the card. */
571 1.2.6.2 nathanw buf = EC_CSR_INT_BUF(intbit);
572 1.2.6.2 nathanw bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
573 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_INT_BSW(intbit) | intbit);
574 1.2.6.2 nathanw }
575 1.2.6.2 nathanw
576 1.2.6.2 nathanw int
577 1.2.6.2 nathanw ec_mediachange(ifp)
578 1.2.6.2 nathanw struct ifnet *ifp;
579 1.2.6.2 nathanw {
580 1.2.6.2 nathanw return (0);
581 1.2.6.2 nathanw }
582 1.2.6.2 nathanw
583 1.2.6.2 nathanw void
584 1.2.6.2 nathanw ec_mediastatus(ifp, ifmr)
585 1.2.6.2 nathanw struct ifnet *ifp;
586 1.2.6.2 nathanw struct ifmediareq *ifmr;
587 1.2.6.2 nathanw {
588 1.2.6.2 nathanw if ((ifp->if_flags & IFF_UP) == 0)
589 1.2.6.2 nathanw return;
590 1.2.6.2 nathanw
591 1.2.6.2 nathanw ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
592 1.2.6.2 nathanw }
593 1.2.6.2 nathanw
594 1.2.6.2 nathanw /*
595 1.2.6.2 nathanw * Process an ioctl request. This code needs some work - it looks pretty ugly.
596 1.2.6.2 nathanw */
597 1.2.6.2 nathanw int
598 1.2.6.2 nathanw ec_ioctl(ifp, cmd, data)
599 1.2.6.2 nathanw struct ifnet *ifp;
600 1.2.6.2 nathanw u_long cmd;
601 1.2.6.2 nathanw caddr_t data;
602 1.2.6.2 nathanw {
603 1.2.6.2 nathanw struct ifaddr *ifa = (struct ifaddr *) data;
604 1.2.6.2 nathanw struct ifreq *ifr = (struct ifreq *)data;
605 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
606 1.2.6.2 nathanw int s, error = 0;
607 1.2.6.2 nathanw
608 1.2.6.2 nathanw s = splnet();
609 1.2.6.2 nathanw
610 1.2.6.2 nathanw switch (cmd) {
611 1.2.6.2 nathanw
612 1.2.6.2 nathanw case SIOCSIFADDR:
613 1.2.6.2 nathanw ifp->if_flags |= IFF_UP;
614 1.2.6.2 nathanw
615 1.2.6.2 nathanw switch (ifa->ifa_addr->sa_family) {
616 1.2.6.2 nathanw #ifdef INET
617 1.2.6.2 nathanw case AF_INET:
618 1.2.6.2 nathanw ec_init(ifp);
619 1.2.6.2 nathanw arp_ifinit(ifp, ifa);
620 1.2.6.2 nathanw break;
621 1.2.6.2 nathanw #endif
622 1.2.6.2 nathanw #ifdef NS
623 1.2.6.2 nathanw /* XXX - This code is probably wrong. */
624 1.2.6.2 nathanw case AF_NS:
625 1.2.6.2 nathanw {
626 1.2.6.2 nathanw struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
627 1.2.6.2 nathanw
628 1.2.6.2 nathanw if (ns_nullhost(*ina))
629 1.2.6.2 nathanw ina->x_host =
630 1.2.6.2 nathanw *(union ns_host *) LLADDR(ifp->if_sadl);
631 1.2.6.2 nathanw else
632 1.2.6.2 nathanw bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
633 1.2.6.2 nathanw ETHER_ADDR_LEN);
634 1.2.6.2 nathanw /* Set new address. */
635 1.2.6.2 nathanw ec_init(ifp);
636 1.2.6.2 nathanw break;
637 1.2.6.2 nathanw }
638 1.2.6.2 nathanw #endif
639 1.2.6.2 nathanw default:
640 1.2.6.2 nathanw ec_init(ifp);
641 1.2.6.2 nathanw break;
642 1.2.6.2 nathanw }
643 1.2.6.2 nathanw break;
644 1.2.6.2 nathanw
645 1.2.6.2 nathanw case SIOCSIFFLAGS:
646 1.2.6.2 nathanw if ((ifp->if_flags & IFF_UP) == 0 &&
647 1.2.6.2 nathanw (ifp->if_flags & IFF_RUNNING) != 0) {
648 1.2.6.2 nathanw /*
649 1.2.6.2 nathanw * If interface is marked down and it is running, then
650 1.2.6.2 nathanw * stop it.
651 1.2.6.2 nathanw */
652 1.2.6.2 nathanw ifp->if_flags &= ~IFF_RUNNING;
653 1.2.6.2 nathanw } else if ((ifp->if_flags & IFF_UP) != 0 &&
654 1.2.6.2 nathanw (ifp->if_flags & IFF_RUNNING) == 0) {
655 1.2.6.2 nathanw /*
656 1.2.6.2 nathanw * If interface is marked up and it is stopped, then
657 1.2.6.2 nathanw * start it.
658 1.2.6.2 nathanw */
659 1.2.6.2 nathanw ec_init(ifp);
660 1.2.6.2 nathanw } else {
661 1.2.6.2 nathanw /*
662 1.2.6.2 nathanw * Some other important flag might have changed, so
663 1.2.6.2 nathanw * reset.
664 1.2.6.2 nathanw */
665 1.2.6.2 nathanw ec_reset(ifp);
666 1.2.6.2 nathanw }
667 1.2.6.2 nathanw break;
668 1.2.6.2 nathanw
669 1.2.6.2 nathanw case SIOCGIFMEDIA:
670 1.2.6.2 nathanw case SIOCSIFMEDIA:
671 1.2.6.2 nathanw error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
672 1.2.6.2 nathanw break;
673 1.2.6.2 nathanw
674 1.2.6.2 nathanw default:
675 1.2.6.2 nathanw error = EINVAL;
676 1.2.6.2 nathanw break;
677 1.2.6.2 nathanw }
678 1.2.6.2 nathanw
679 1.2.6.2 nathanw splx(s);
680 1.2.6.2 nathanw return error;
681 1.2.6.2 nathanw }
682 1.2.6.2 nathanw
683 1.2.6.2 nathanw /*
684 1.2.6.2 nathanw * Collision routine.
685 1.2.6.2 nathanw */
686 1.2.6.2 nathanw void
687 1.2.6.2 nathanw ec_coll(sc)
688 1.2.6.2 nathanw struct ec_softc *sc;
689 1.2.6.2 nathanw {
690 1.2.6.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
691 1.2.6.2 nathanw u_short jams;
692 1.2.6.2 nathanw struct mbuf *m0;
693 1.2.6.2 nathanw
694 1.2.6.2 nathanw if ((++sc->sc_colliding) >= EC_COLLISIONS_JAMMED) {
695 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_oerrors++;
696 1.2.6.2 nathanw if (!sc->sc_jammed)
697 1.2.6.2 nathanw printf("%s: ethernet jammed\n",
698 1.2.6.2 nathanw sc->sc_dev.dv_xname);
699 1.2.6.2 nathanw sc->sc_jammed = 1;
700 1.2.6.2 nathanw sc->sc_colliding = 0;
701 1.2.6.2 nathanw ifp->if_flags &= ~IFF_OACTIVE;
702 1.2.6.2 nathanw IFQ_POLL(&ifp->if_snd, m0);
703 1.2.6.2 nathanw if (m0 != NULL)
704 1.2.6.2 nathanw ec_start(ifp);
705 1.2.6.2 nathanw } else {
706 1.2.6.2 nathanw jams = MAX(sc->sc_colliding, EC_BACKOFF_PRNG_COLL_MAX);
707 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;
708 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));
709 1.2.6.2 nathanw ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) | EC_CSR_JAM | EC_CSR_TINT | EC_CSR_JINT);
710 1.2.6.2 nathanw }
711 1.2.6.2 nathanw }
712 1.2.6.2 nathanw
713 1.2.6.2 nathanw /*
714 1.2.6.2 nathanw * Device timeout routine.
715 1.2.6.2 nathanw */
716 1.2.6.2 nathanw void
717 1.2.6.2 nathanw ec_watchdog(ifp)
718 1.2.6.2 nathanw struct ifnet *ifp;
719 1.2.6.2 nathanw {
720 1.2.6.2 nathanw struct ec_softc *sc = ifp->if_softc;
721 1.2.6.2 nathanw
722 1.2.6.2 nathanw log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
723 1.2.6.2 nathanw sc->sc_ethercom.ec_if.if_oerrors++;
724 1.2.6.2 nathanw
725 1.2.6.2 nathanw ec_reset(ifp);
726 1.2.6.2 nathanw }
727