if_el.c revision 1.58 1 1.58 thorpej /* $NetBSD: if_el.c,v 1.58 1999/05/18 23:52:57 thorpej Exp $ */
2 1.14 cgd
3 1.2 cgd /*
4 1.2 cgd * Copyright (c) 1994, Matthew E. Kimmel. Permission is hereby granted
5 1.1 hpeyerl * to use, copy, modify and distribute this software provided that both
6 1.1 hpeyerl * the copyright notice and this permission notice appear in all copies
7 1.1 hpeyerl * of the software, derivative works or modified versions, and any
8 1.1 hpeyerl * portions thereof.
9 1.1 hpeyerl */
10 1.2 cgd
11 1.2 cgd /*
12 1.2 cgd * 3COM Etherlink 3C501 device driver
13 1.1 hpeyerl */
14 1.2 cgd
15 1.2 cgd /*
16 1.2 cgd * Bugs/possible improvements:
17 1.1 hpeyerl * - Does not currently support DMA
18 1.1 hpeyerl * - Does not currently support multicasts
19 1.1 hpeyerl */
20 1.2 cgd
21 1.53 jonathan #include "opt_inet.h"
22 1.54 jonathan #include "opt_ns.h"
23 1.1 hpeyerl #include "bpfilter.h"
24 1.49 explorer #include "rnd.h"
25 1.1 hpeyerl
26 1.1 hpeyerl #include <sys/param.h>
27 1.37 christos #include <sys/systm.h>
28 1.1 hpeyerl #include <sys/errno.h>
29 1.1 hpeyerl #include <sys/ioctl.h>
30 1.1 hpeyerl #include <sys/mbuf.h>
31 1.1 hpeyerl #include <sys/socket.h>
32 1.1 hpeyerl #include <sys/syslog.h>
33 1.3 mycroft #include <sys/device.h>
34 1.49 explorer #if NRND > 0
35 1.49 explorer #include <sys/rnd.h>
36 1.49 explorer #endif
37 1.1 hpeyerl
38 1.1 hpeyerl #include <net/if.h>
39 1.1 hpeyerl #include <net/if_dl.h>
40 1.1 hpeyerl #include <net/if_types.h>
41 1.1 hpeyerl
42 1.44 is #include <net/if_ether.h>
43 1.44 is
44 1.1 hpeyerl #ifdef INET
45 1.1 hpeyerl #include <netinet/in.h>
46 1.1 hpeyerl #include <netinet/in_systm.h>
47 1.1 hpeyerl #include <netinet/in_var.h>
48 1.1 hpeyerl #include <netinet/ip.h>
49 1.44 is #include <netinet/if_inarp.h>
50 1.1 hpeyerl #endif
51 1.1 hpeyerl
52 1.1 hpeyerl #ifdef NS
53 1.1 hpeyerl #include <netns/ns.h>
54 1.1 hpeyerl #include <netns/ns_if.h>
55 1.1 hpeyerl #endif
56 1.1 hpeyerl
57 1.1 hpeyerl #if NBPFILTER > 0
58 1.1 hpeyerl #include <net/bpf.h>
59 1.1 hpeyerl #include <net/bpfdesc.h>
60 1.1 hpeyerl #endif
61 1.1 hpeyerl
62 1.7 mycroft #include <machine/cpu.h>
63 1.39 mycroft #include <machine/intr.h>
64 1.40 thorpej #include <machine/bus.h>
65 1.1 hpeyerl
66 1.23 cgd #include <dev/isa/isavar.h>
67 1.21 cgd #include <dev/isa/if_elreg.h>
68 1.1 hpeyerl
69 1.3 mycroft /* for debugging convenience */
70 1.1 hpeyerl #ifdef EL_DEBUG
71 1.42 christos #define DPRINTF(x) printf x
72 1.1 hpeyerl #else
73 1.41 christos #define DPRINTF(x)
74 1.1 hpeyerl #endif
75 1.1 hpeyerl
76 1.2 cgd /*
77 1.3 mycroft * per-line info and status
78 1.2 cgd */
79 1.1 hpeyerl struct el_softc {
80 1.3 mycroft struct device sc_dev;
81 1.23 cgd void *sc_ih;
82 1.3 mycroft
83 1.44 is struct ethercom sc_ethercom; /* ethernet common */
84 1.43 thorpej bus_space_tag_t sc_iot; /* bus space identifier */
85 1.43 thorpej bus_space_handle_t sc_ioh; /* i/o handle */
86 1.49 explorer
87 1.49 explorer #if NRND > 0
88 1.49 explorer rndsource_element_t rnd_source;
89 1.49 explorer #endif
90 1.8 mycroft };
91 1.1 hpeyerl
92 1.2 cgd /*
93 1.3 mycroft * prototypes
94 1.2 cgd */
95 1.23 cgd int elintr __P((void *));
96 1.37 christos void elinit __P((struct el_softc *));
97 1.30 mycroft int elioctl __P((struct ifnet *, u_long, caddr_t));
98 1.30 mycroft void elstart __P((struct ifnet *));
99 1.38 thorpej void elwatchdog __P((struct ifnet *));
100 1.30 mycroft void elreset __P((struct el_softc *));
101 1.30 mycroft void elstop __P((struct el_softc *));
102 1.28 mycroft static int el_xmit __P((struct el_softc *));
103 1.30 mycroft void elread __P((struct el_softc *, int));
104 1.31 mycroft struct mbuf *elget __P((struct el_softc *sc, int));
105 1.5 mycroft static inline void el_hardreset __P((struct el_softc *));
106 1.1 hpeyerl
107 1.50 drochner int elprobe __P((struct device *, struct cfdata *, void *));
108 1.16 mycroft void elattach __P((struct device *, struct device *, void *));
109 1.8 mycroft
110 1.35 thorpej struct cfattach el_ca = {
111 1.35 thorpej sizeof(struct el_softc), elprobe, elattach
112 1.1 hpeyerl };
113 1.1 hpeyerl
114 1.2 cgd /*
115 1.2 cgd * Probe routine.
116 1.2 cgd *
117 1.2 cgd * See if the card is there and at the right place.
118 1.2 cgd * (XXX - cgd -- needs help)
119 1.2 cgd */
120 1.8 mycroft int
121 1.16 mycroft elprobe(parent, match, aux)
122 1.16 mycroft struct device *parent;
123 1.50 drochner struct cfdata *match;
124 1.50 drochner void *aux;
125 1.8 mycroft {
126 1.8 mycroft struct isa_attach_args *ia = aux;
127 1.43 thorpej bus_space_tag_t iot = ia->ia_iot;
128 1.43 thorpej bus_space_handle_t ioh;
129 1.18 mycroft int iobase = ia->ia_iobase;
130 1.40 thorpej u_int8_t station_addr[ETHER_ADDR_LEN];
131 1.40 thorpej u_int8_t i;
132 1.40 thorpej int rval;
133 1.40 thorpej
134 1.40 thorpej rval = 0;
135 1.1 hpeyerl
136 1.3 mycroft /* First check the base. */
137 1.48 mycroft if (iobase < 0x200 || iobase > 0x3f0)
138 1.3 mycroft return 0;
139 1.3 mycroft
140 1.40 thorpej /* Map i/o space. */
141 1.48 mycroft if (bus_space_map(iot, iobase, 16, 0, &ioh))
142 1.40 thorpej return 0;
143 1.3 mycroft
144 1.2 cgd /*
145 1.3 mycroft * Now attempt to grab the station address from the PROM and see if it
146 1.3 mycroft * contains the 3com vendor code.
147 1.1 hpeyerl */
148 1.41 christos DPRINTF(("Probing 3c501 at 0x%x...\n", iobase));
149 1.3 mycroft
150 1.3 mycroft /* Reset the board. */
151 1.41 christos DPRINTF(("Resetting board...\n"));
152 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
153 1.6 mycroft delay(5);
154 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, 0);
155 1.3 mycroft
156 1.3 mycroft /* Now read the address. */
157 1.41 christos DPRINTF(("Reading station address...\n"));
158 1.3 mycroft for (i = 0; i < ETHER_ADDR_LEN; i++) {
159 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBL, i);
160 1.43 thorpej station_addr[i] = bus_space_read_1(iot, ioh, EL_EAW);
161 1.1 hpeyerl }
162 1.41 christos DPRINTF(("Address is %s\n", ether_sprintf(station_addr)));
163 1.1 hpeyerl
164 1.2 cgd /*
165 1.3 mycroft * If the vendor code is ok, return a 1. We'll assume that whoever
166 1.3 mycroft * configured this system is right about the IRQ.
167 1.1 hpeyerl */
168 1.3 mycroft if (station_addr[0] != 0x02 || station_addr[1] != 0x60 ||
169 1.3 mycroft station_addr[2] != 0x8c) {
170 1.41 christos DPRINTF(("Bad vendor code.\n"));
171 1.40 thorpej goto out;
172 1.1 hpeyerl }
173 1.41 christos DPRINTF(("Vendor code ok.\n"));
174 1.8 mycroft
175 1.48 mycroft ia->ia_iosize = 16;
176 1.8 mycroft ia->ia_msize = 0;
177 1.40 thorpej rval = 1;
178 1.40 thorpej
179 1.40 thorpej out:
180 1.48 mycroft bus_space_unmap(iot, ioh, 16);
181 1.40 thorpej return rval;
182 1.1 hpeyerl }
183 1.1 hpeyerl
184 1.2 cgd /*
185 1.3 mycroft * Attach the interface to the kernel data structures. By the time this is
186 1.3 mycroft * called, we know that the card exists at the given I/O address. We still
187 1.3 mycroft * assume that the IRQ given is correct.
188 1.1 hpeyerl */
189 1.8 mycroft void
190 1.8 mycroft elattach(parent, self, aux)
191 1.8 mycroft struct device *parent, *self;
192 1.8 mycroft void *aux;
193 1.1 hpeyerl {
194 1.8 mycroft struct el_softc *sc = (void *)self;
195 1.9 mycroft struct isa_attach_args *ia = aux;
196 1.43 thorpej bus_space_tag_t iot = ia->ia_iot;
197 1.43 thorpej bus_space_handle_t ioh;
198 1.44 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
199 1.44 is u_int8_t myaddr[ETHER_ADDR_LEN];
200 1.40 thorpej u_int8_t i;
201 1.40 thorpej
202 1.42 christos printf("\n");
203 1.1 hpeyerl
204 1.41 christos DPRINTF(("Attaching %s...\n", sc->sc_dev.dv_xname));
205 1.1 hpeyerl
206 1.40 thorpej /* Map i/o space. */
207 1.43 thorpej if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
208 1.42 christos printf("%s: can't map i/o space\n", self->dv_xname);
209 1.40 thorpej return;
210 1.40 thorpej }
211 1.40 thorpej
212 1.43 thorpej sc->sc_iot = iot;
213 1.40 thorpej sc->sc_ioh = ioh;
214 1.40 thorpej
215 1.40 thorpej /* Reset the board. */
216 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
217 1.40 thorpej delay(5);
218 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, 0);
219 1.40 thorpej
220 1.40 thorpej /* Now read the address. */
221 1.40 thorpej for (i = 0; i < ETHER_ADDR_LEN; i++) {
222 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBL, i);
223 1.44 is myaddr[i] = bus_space_read_1(iot, ioh, EL_EAW);
224 1.40 thorpej }
225 1.40 thorpej
226 1.3 mycroft /* Stop the board. */
227 1.30 mycroft elstop(sc);
228 1.1 hpeyerl
229 1.3 mycroft /* Initialize ifnet structure. */
230 1.38 thorpej bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
231 1.38 thorpej ifp->if_softc = sc;
232 1.30 mycroft ifp->if_start = elstart;
233 1.30 mycroft ifp->if_ioctl = elioctl;
234 1.30 mycroft ifp->if_watchdog = elwatchdog;
235 1.3 mycroft ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
236 1.1 hpeyerl
237 1.3 mycroft /* Now we can attach the interface. */
238 1.41 christos DPRINTF(("Attaching interface...\n"));
239 1.1 hpeyerl if_attach(ifp);
240 1.44 is ether_ifattach(ifp, myaddr);
241 1.1 hpeyerl
242 1.3 mycroft /* Print out some information for the user. */
243 1.44 is printf("%s: address %s\n", self->dv_xname, ether_sprintf(myaddr));
244 1.1 hpeyerl
245 1.1 hpeyerl /* Finally, attach to bpf filter if it is present. */
246 1.1 hpeyerl #if NBPFILTER > 0
247 1.41 christos DPRINTF(("Attaching to BPF...\n"));
248 1.13 mycroft bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
249 1.1 hpeyerl #endif
250 1.1 hpeyerl
251 1.36 cgd sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
252 1.36 cgd IPL_NET, elintr, sc);
253 1.9 mycroft
254 1.49 explorer #if NRND > 0
255 1.49 explorer DPRINTF(("Attaching to random...\n"));
256 1.56 explorer rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
257 1.56 explorer RND_TYPE_NET, 0);
258 1.49 explorer #endif
259 1.49 explorer
260 1.41 christos DPRINTF(("elattach() finished.\n"));
261 1.1 hpeyerl }
262 1.1 hpeyerl
263 1.2 cgd /*
264 1.2 cgd * Reset interface.
265 1.2 cgd */
266 1.30 mycroft void
267 1.30 mycroft elreset(sc)
268 1.3 mycroft struct el_softc *sc;
269 1.1 hpeyerl {
270 1.1 hpeyerl int s;
271 1.1 hpeyerl
272 1.41 christos DPRINTF(("elreset()\n"));
273 1.34 mycroft s = splnet();
274 1.30 mycroft elstop(sc);
275 1.30 mycroft elinit(sc);
276 1.1 hpeyerl splx(s);
277 1.1 hpeyerl }
278 1.1 hpeyerl
279 1.2 cgd /*
280 1.2 cgd * Stop interface.
281 1.2 cgd */
282 1.30 mycroft void
283 1.30 mycroft elstop(sc)
284 1.3 mycroft struct el_softc *sc;
285 1.1 hpeyerl {
286 1.1 hpeyerl
287 1.43 thorpej bus_space_write_1(sc->sc_iot, sc->sc_ioh, EL_AC, 0);
288 1.1 hpeyerl }
289 1.1 hpeyerl
290 1.2 cgd /*
291 1.5 mycroft * Do a hardware reset of the board, and upload the ethernet address again in
292 1.5 mycroft * case the board forgets.
293 1.5 mycroft */
294 1.5 mycroft static inline void
295 1.5 mycroft el_hardreset(sc)
296 1.5 mycroft struct el_softc *sc;
297 1.5 mycroft {
298 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
299 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
300 1.5 mycroft int i;
301 1.5 mycroft
302 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_RESET);
303 1.6 mycroft delay(5);
304 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, 0);
305 1.5 mycroft
306 1.5 mycroft for (i = 0; i < ETHER_ADDR_LEN; i++)
307 1.44 is bus_space_write_1(iot, ioh, i,
308 1.44 is LLADDR(sc->sc_ethercom.ec_if.if_sadl)[i]);
309 1.5 mycroft }
310 1.5 mycroft
311 1.5 mycroft /*
312 1.2 cgd * Initialize interface.
313 1.2 cgd */
314 1.37 christos void
315 1.30 mycroft elinit(sc)
316 1.3 mycroft struct el_softc *sc;
317 1.1 hpeyerl {
318 1.44 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
319 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
320 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
321 1.1 hpeyerl
322 1.1 hpeyerl /* First, reset the board. */
323 1.5 mycroft el_hardreset(sc);
324 1.1 hpeyerl
325 1.3 mycroft /* Configure rx. */
326 1.41 christos DPRINTF(("Configuring rx...\n"));
327 1.2 cgd if (ifp->if_flags & IFF_PROMISC)
328 1.43 thorpej bus_space_write_1(iot, ioh, EL_RXC,
329 1.40 thorpej EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
330 1.40 thorpej EL_RXC_DOFLOW | EL_RXC_PROMISC);
331 1.1 hpeyerl else
332 1.43 thorpej bus_space_write_1(iot, ioh, EL_RXC,
333 1.40 thorpej EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
334 1.40 thorpej EL_RXC_DOFLOW | EL_RXC_ABROAD);
335 1.43 thorpej bus_space_write_1(iot, ioh, EL_RBC, 0);
336 1.1 hpeyerl
337 1.3 mycroft /* Configure TX. */
338 1.41 christos DPRINTF(("Configuring tx...\n"));
339 1.43 thorpej bus_space_write_1(iot, ioh, EL_TXC, 0);
340 1.1 hpeyerl
341 1.3 mycroft /* Start reception. */
342 1.41 christos DPRINTF(("Starting reception...\n"));
343 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
344 1.1 hpeyerl
345 1.3 mycroft /* Set flags appropriately. */
346 1.1 hpeyerl ifp->if_flags |= IFF_RUNNING;
347 1.1 hpeyerl ifp->if_flags &= ~IFF_OACTIVE;
348 1.1 hpeyerl
349 1.1 hpeyerl /* And start output. */
350 1.30 mycroft elstart(ifp);
351 1.1 hpeyerl }
352 1.1 hpeyerl
353 1.2 cgd /*
354 1.3 mycroft * Start output on interface. Get datagrams from the queue and output them,
355 1.34 mycroft * giving the receiver a chance between datagrams. Call only from splnet or
356 1.3 mycroft * interrupt level!
357 1.1 hpeyerl */
358 1.30 mycroft void
359 1.30 mycroft elstart(ifp)
360 1.2 cgd struct ifnet *ifp;
361 1.1 hpeyerl {
362 1.38 thorpej struct el_softc *sc = ifp->if_softc;
363 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
364 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
365 1.1 hpeyerl struct mbuf *m, *m0;
366 1.27 mycroft int s, i, off, retries;
367 1.1 hpeyerl
368 1.41 christos DPRINTF(("elstart()...\n"));
369 1.34 mycroft s = splnet();
370 1.1 hpeyerl
371 1.3 mycroft /* Don't do anything if output is active. */
372 1.27 mycroft if ((ifp->if_flags & IFF_OACTIVE) != 0) {
373 1.27 mycroft splx(s);
374 1.1 hpeyerl return;
375 1.27 mycroft }
376 1.27 mycroft
377 1.27 mycroft ifp->if_flags |= IFF_OACTIVE;
378 1.1 hpeyerl
379 1.3 mycroft /*
380 1.3 mycroft * The main loop. They warned me against endless loops, but would I
381 1.3 mycroft * listen? NOOO....
382 1.1 hpeyerl */
383 1.3 mycroft for (;;) {
384 1.3 mycroft /* Dequeue the next datagram. */
385 1.27 mycroft IF_DEQUEUE(&ifp->if_snd, m0);
386 1.1 hpeyerl
387 1.1 hpeyerl /* If there's nothing to send, return. */
388 1.27 mycroft if (m0 == 0)
389 1.27 mycroft break;
390 1.27 mycroft
391 1.27 mycroft #if NBPFILTER > 0
392 1.27 mycroft /* Give the packet to the bpf, if any. */
393 1.27 mycroft if (ifp->if_bpf)
394 1.27 mycroft bpf_mtap(ifp->if_bpf, m0);
395 1.27 mycroft #endif
396 1.1 hpeyerl
397 1.3 mycroft /* Disable the receiver. */
398 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
399 1.43 thorpej bus_space_write_1(iot, ioh, EL_RBC, 0);
400 1.1 hpeyerl
401 1.27 mycroft /* Transfer datagram to board. */
402 1.41 christos DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
403 1.27 mycroft off = EL_BUFSIZ - max(m0->m_pkthdr.len, ETHER_MIN_LEN);
404 1.40 thorpej #ifdef DIAGNOSTIC
405 1.40 thorpej if ((off & 0xffff) != off)
406 1.42 christos printf("%s: bogus off 0x%x\n",
407 1.40 thorpej sc->sc_dev.dv_xname, off);
408 1.40 thorpej #endif
409 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
410 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
411 1.27 mycroft
412 1.1 hpeyerl /* Copy the datagram to the buffer. */
413 1.27 mycroft for (m = m0; m != 0; m = m->m_next)
414 1.43 thorpej bus_space_write_multi_1(iot, ioh, EL_BUF,
415 1.40 thorpej mtod(m, u_int8_t *), m->m_len);
416 1.27 mycroft
417 1.1 hpeyerl m_freem(m0);
418 1.1 hpeyerl
419 1.3 mycroft /* Now transmit the datagram. */
420 1.3 mycroft retries = 0;
421 1.27 mycroft for (;;) {
422 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
423 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
424 1.33 mycroft if (el_xmit(sc)) {
425 1.33 mycroft ifp->if_oerrors++;
426 1.1 hpeyerl break;
427 1.33 mycroft }
428 1.3 mycroft /* Check out status. */
429 1.43 thorpej i = bus_space_read_1(iot, ioh, EL_TXS);
430 1.41 christos DPRINTF(("tx status=0x%x\n", i));
431 1.3 mycroft if ((i & EL_TXS_READY) == 0) {
432 1.41 christos DPRINTF(("el: err txs=%x\n", i));
433 1.3 mycroft if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
434 1.33 mycroft ifp->if_collisions++;
435 1.3 mycroft if ((i & EL_TXC_DCOLL16) == 0 &&
436 1.2 cgd retries < 15) {
437 1.1 hpeyerl retries++;
438 1.43 thorpej bus_space_write_1(iot, ioh,
439 1.40 thorpej EL_AC, EL_AC_HOST);
440 1.1 hpeyerl }
441 1.33 mycroft } else {
442 1.33 mycroft ifp->if_oerrors++;
443 1.27 mycroft break;
444 1.33 mycroft }
445 1.4 mycroft } else {
446 1.27 mycroft ifp->if_opackets++;
447 1.27 mycroft break;
448 1.4 mycroft }
449 1.1 hpeyerl }
450 1.1 hpeyerl
451 1.2 cgd /*
452 1.2 cgd * Now give the card a chance to receive.
453 1.1 hpeyerl * Gotta love 3c501s...
454 1.1 hpeyerl */
455 1.43 thorpej (void)bus_space_read_1(iot, ioh, EL_AS);
456 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
457 1.1 hpeyerl splx(s);
458 1.3 mycroft /* Interrupt here. */
459 1.34 mycroft s = splnet();
460 1.1 hpeyerl }
461 1.27 mycroft
462 1.43 thorpej (void)bus_space_read_1(iot, ioh, EL_AS);
463 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
464 1.27 mycroft ifp->if_flags &= ~IFF_OACTIVE;
465 1.27 mycroft splx(s);
466 1.1 hpeyerl }
467 1.1 hpeyerl
468 1.2 cgd /*
469 1.3 mycroft * This function actually attempts to transmit a datagram downloaded to the
470 1.34 mycroft * board. Call at splnet or interrupt, after downloading data! Returns 0 on
471 1.3 mycroft * success, non-0 on failure.
472 1.1 hpeyerl */
473 1.1 hpeyerl static int
474 1.27 mycroft el_xmit(sc)
475 1.2 cgd struct el_softc *sc;
476 1.1 hpeyerl {
477 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
478 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
479 1.1 hpeyerl int i;
480 1.1 hpeyerl
481 1.33 mycroft /*
482 1.33 mycroft * XXX
483 1.33 mycroft * This busy-waits for the tx completion. Can we get an interrupt
484 1.33 mycroft * instead?
485 1.33 mycroft */
486 1.33 mycroft
487 1.41 christos DPRINTF(("el: xmit..."));
488 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
489 1.1 hpeyerl i = 20000;
490 1.43 thorpej while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
491 1.1 hpeyerl i--;
492 1.2 cgd if (i == 0) {
493 1.41 christos DPRINTF(("tx not ready\n"));
494 1.3 mycroft return -1;
495 1.1 hpeyerl }
496 1.41 christos DPRINTF(("%d cycles.\n", 20000 - i));
497 1.3 mycroft return 0;
498 1.1 hpeyerl }
499 1.1 hpeyerl
500 1.3 mycroft /*
501 1.3 mycroft * Controller interrupt.
502 1.3 mycroft */
503 1.1 hpeyerl int
504 1.23 cgd elintr(arg)
505 1.23 cgd void *arg;
506 1.1 hpeyerl {
507 1.23 cgd register struct el_softc *sc = arg;
508 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
509 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
510 1.40 thorpej u_int8_t rxstat;
511 1.40 thorpej int len;
512 1.1 hpeyerl
513 1.41 christos DPRINTF(("elintr: "));
514 1.1 hpeyerl
515 1.3 mycroft /* Check board status. */
516 1.43 thorpej if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0) {
517 1.43 thorpej (void)bus_space_read_1(iot, ioh, EL_RXC);
518 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
519 1.10 mycroft return 0;
520 1.1 hpeyerl }
521 1.1 hpeyerl
522 1.27 mycroft for (;;) {
523 1.43 thorpej rxstat = bus_space_read_1(iot, ioh, EL_RXS);
524 1.27 mycroft if (rxstat & EL_RXS_STALE)
525 1.27 mycroft break;
526 1.1 hpeyerl
527 1.1 hpeyerl /* If there's an overflow, reinit the board. */
528 1.3 mycroft if ((rxstat & EL_RXS_NOFLOW) == 0) {
529 1.41 christos DPRINTF(("overflow.\n"));
530 1.5 mycroft el_hardreset(sc);
531 1.3 mycroft /* Put board back into receive mode. */
532 1.44 is if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC)
533 1.43 thorpej bus_space_write_1(iot, ioh, EL_RXC,
534 1.40 thorpej EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
535 1.40 thorpej EL_RXC_DOFLOW | EL_RXC_PROMISC);
536 1.1 hpeyerl else
537 1.43 thorpej bus_space_write_1(iot, ioh, EL_RXC,
538 1.40 thorpej EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB |
539 1.40 thorpej EL_RXC_DOFLOW | EL_RXC_ABROAD);
540 1.43 thorpej (void)bus_space_read_1(iot, ioh, EL_AS);
541 1.43 thorpej bus_space_write_1(iot, ioh, EL_RBC, 0);
542 1.27 mycroft break;
543 1.1 hpeyerl }
544 1.1 hpeyerl
545 1.3 mycroft /* Incoming packet. */
546 1.43 thorpej len = bus_space_read_1(iot, ioh, EL_RBL);
547 1.43 thorpej len |= bus_space_read_1(iot, ioh, EL_RBH) << 8;
548 1.41 christos DPRINTF(("receive len=%d rxstat=%x ", len, rxstat));
549 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_HOST);
550 1.1 hpeyerl
551 1.3 mycroft /* Pass data up to upper levels. */
552 1.27 mycroft elread(sc, len);
553 1.1 hpeyerl
554 1.1 hpeyerl /* Is there another packet? */
555 1.43 thorpej if ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_RXBUSY) != 0)
556 1.27 mycroft break;
557 1.49 explorer
558 1.49 explorer #if NRND > 0
559 1.49 explorer rnd_add_uint32(&sc->rnd_source, rxstat);
560 1.49 explorer #endif
561 1.27 mycroft
562 1.41 christos DPRINTF(("<rescan> "));
563 1.1 hpeyerl }
564 1.1 hpeyerl
565 1.43 thorpej (void)bus_space_read_1(iot, ioh, EL_RXC);
566 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
567 1.10 mycroft return 1;
568 1.1 hpeyerl }
569 1.1 hpeyerl
570 1.2 cgd /*
571 1.30 mycroft * Pass a packet to the higher levels.
572 1.2 cgd */
573 1.30 mycroft void
574 1.27 mycroft elread(sc, len)
575 1.31 mycroft register struct el_softc *sc;
576 1.2 cgd int len;
577 1.1 hpeyerl {
578 1.44 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
579 1.1 hpeyerl struct mbuf *m;
580 1.26 mycroft struct ether_header *eh;
581 1.1 hpeyerl
582 1.30 mycroft if (len <= sizeof(struct ether_header) ||
583 1.30 mycroft len > ETHER_MAX_LEN) {
584 1.42 christos printf("%s: invalid packet size %d; dropping\n",
585 1.32 mycroft sc->sc_dev.dv_xname, len);
586 1.30 mycroft ifp->if_ierrors++;
587 1.30 mycroft return;
588 1.30 mycroft }
589 1.30 mycroft
590 1.13 mycroft /* Pull packet off interface. */
591 1.30 mycroft m = elget(sc, len);
592 1.30 mycroft if (m == 0) {
593 1.30 mycroft ifp->if_ierrors++;
594 1.1 hpeyerl return;
595 1.30 mycroft }
596 1.30 mycroft
597 1.30 mycroft ifp->if_ipackets++;
598 1.1 hpeyerl
599 1.26 mycroft /* We assume that the header fit entirely in one mbuf. */
600 1.26 mycroft eh = mtod(m, struct ether_header *);
601 1.26 mycroft
602 1.1 hpeyerl #if NBPFILTER > 0
603 1.1 hpeyerl /*
604 1.13 mycroft * Check if there's a BPF listener on this interface.
605 1.30 mycroft * If so, hand off the raw packet to BPF.
606 1.1 hpeyerl */
607 1.26 mycroft if (ifp->if_bpf) {
608 1.26 mycroft bpf_mtap(ifp->if_bpf, m);
609 1.1 hpeyerl
610 1.1 hpeyerl /*
611 1.1 hpeyerl * Note that the interface cannot be in promiscuous mode if
612 1.13 mycroft * there are no BPF listeners. And if we are in promiscuous
613 1.13 mycroft * mode, we have to check if this packet is really ours.
614 1.1 hpeyerl */
615 1.26 mycroft if ((ifp->if_flags & IFF_PROMISC) &&
616 1.13 mycroft (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
617 1.44 is bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
618 1.13 mycroft sizeof(eh->ether_dhost)) != 0) {
619 1.13 mycroft m_freem(m);
620 1.1 hpeyerl return;
621 1.13 mycroft }
622 1.1 hpeyerl }
623 1.1 hpeyerl #endif
624 1.1 hpeyerl
625 1.58 thorpej (*ifp->if_input)(ifp, m);
626 1.1 hpeyerl }
627 1.1 hpeyerl
628 1.1 hpeyerl /*
629 1.3 mycroft * Pull read data off a interface. Len is length of data, with local net
630 1.13 mycroft * header stripped. We copy the data into mbufs. When full cluster sized
631 1.13 mycroft * units are present we copy into clusters.
632 1.1 hpeyerl */
633 1.31 mycroft struct mbuf *
634 1.30 mycroft elget(sc, totlen)
635 1.27 mycroft struct el_softc *sc;
636 1.26 mycroft int totlen;
637 1.26 mycroft {
638 1.44 is struct ifnet *ifp = &sc->sc_ethercom.ec_if;
639 1.43 thorpej bus_space_tag_t iot = sc->sc_iot;
640 1.43 thorpej bus_space_handle_t ioh = sc->sc_ioh;
641 1.55 mycroft struct mbuf *m, *m0, *newm;
642 1.26 mycroft int len;
643 1.26 mycroft
644 1.55 mycroft MGETHDR(m0, M_DONTWAIT, MT_DATA);
645 1.55 mycroft if (m0 == 0)
646 1.55 mycroft return (0);
647 1.55 mycroft m0->m_pkthdr.rcvif = ifp;
648 1.55 mycroft m0->m_pkthdr.len = totlen;
649 1.26 mycroft len = MHLEN;
650 1.55 mycroft m = m0;
651 1.26 mycroft
652 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBL, 0);
653 1.43 thorpej bus_space_write_1(iot, ioh, EL_GPBH, 0);
654 1.27 mycroft
655 1.26 mycroft while (totlen > 0) {
656 1.26 mycroft if (totlen >= MINCLSIZE) {
657 1.26 mycroft MCLGET(m, M_DONTWAIT);
658 1.55 mycroft if ((m->m_flags & M_EXT) == 0)
659 1.55 mycroft goto bad;
660 1.45 mycroft len = MCLBYTES;
661 1.26 mycroft }
662 1.55 mycroft
663 1.26 mycroft m->m_len = len = min(totlen, len);
664 1.43 thorpej bus_space_read_multi_1(iot, ioh, EL_BUF, mtod(m, u_int8_t *), len);
665 1.55 mycroft
666 1.26 mycroft totlen -= len;
667 1.55 mycroft if (totlen > 0) {
668 1.55 mycroft MGET(newm, M_DONTWAIT, MT_DATA);
669 1.55 mycroft if (newm == 0)
670 1.55 mycroft goto bad;
671 1.55 mycroft len = MLEN;
672 1.55 mycroft m = m->m_next = newm;
673 1.55 mycroft }
674 1.26 mycroft }
675 1.27 mycroft
676 1.43 thorpej bus_space_write_1(iot, ioh, EL_RBC, 0);
677 1.43 thorpej bus_space_write_1(iot, ioh, EL_AC, EL_AC_RX);
678 1.13 mycroft
679 1.55 mycroft return (m0);
680 1.55 mycroft
681 1.55 mycroft bad:
682 1.55 mycroft m_freem(m0);
683 1.55 mycroft return (0);
684 1.1 hpeyerl }
685 1.1 hpeyerl
686 1.1 hpeyerl /*
687 1.3 mycroft * Process an ioctl request. This code needs some work - it looks pretty ugly.
688 1.1 hpeyerl */
689 1.30 mycroft int
690 1.30 mycroft elioctl(ifp, cmd, data)
691 1.1 hpeyerl register struct ifnet *ifp;
692 1.15 cgd u_long cmd;
693 1.1 hpeyerl caddr_t data;
694 1.1 hpeyerl {
695 1.38 thorpej struct el_softc *sc = ifp->if_softc;
696 1.13 mycroft struct ifaddr *ifa = (struct ifaddr *)data;
697 1.1 hpeyerl int s, error = 0;
698 1.1 hpeyerl
699 1.34 mycroft s = splnet();
700 1.1 hpeyerl
701 1.13 mycroft switch (cmd) {
702 1.1 hpeyerl
703 1.1 hpeyerl case SIOCSIFADDR:
704 1.1 hpeyerl ifp->if_flags |= IFF_UP;
705 1.1 hpeyerl
706 1.1 hpeyerl switch (ifa->ifa_addr->sa_family) {
707 1.1 hpeyerl #ifdef INET
708 1.1 hpeyerl case AF_INET:
709 1.30 mycroft elinit(sc);
710 1.44 is arp_ifinit(ifp, ifa);
711 1.1 hpeyerl break;
712 1.1 hpeyerl #endif
713 1.1 hpeyerl #ifdef NS
714 1.24 mycroft /* XXX - This code is probably wrong. */
715 1.1 hpeyerl case AF_NS:
716 1.3 mycroft {
717 1.3 mycroft register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
718 1.3 mycroft
719 1.3 mycroft if (ns_nullhost(*ina))
720 1.3 mycroft ina->x_host =
721 1.44 is *(union ns_host *)LLADDR(ifp->if_sadl);
722 1.24 mycroft else
723 1.44 is bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
724 1.44 is ETHER_ADDR_LEN);
725 1.3 mycroft /* Set new address. */
726 1.30 mycroft elinit(sc);
727 1.3 mycroft break;
728 1.3 mycroft }
729 1.1 hpeyerl #endif
730 1.1 hpeyerl default:
731 1.30 mycroft elinit(sc);
732 1.1 hpeyerl break;
733 1.1 hpeyerl }
734 1.1 hpeyerl break;
735 1.1 hpeyerl
736 1.1 hpeyerl case SIOCSIFFLAGS:
737 1.3 mycroft if ((ifp->if_flags & IFF_UP) == 0 &&
738 1.13 mycroft (ifp->if_flags & IFF_RUNNING) != 0) {
739 1.3 mycroft /*
740 1.3 mycroft * If interface is marked down and it is running, then
741 1.3 mycroft * stop it.
742 1.3 mycroft */
743 1.30 mycroft elstop(sc);
744 1.1 hpeyerl ifp->if_flags &= ~IFF_RUNNING;
745 1.13 mycroft } else if ((ifp->if_flags & IFF_UP) != 0 &&
746 1.3 mycroft (ifp->if_flags & IFF_RUNNING) == 0) {
747 1.3 mycroft /*
748 1.3 mycroft * If interface is marked up and it is stopped, then
749 1.3 mycroft * start it.
750 1.3 mycroft */
751 1.30 mycroft elinit(sc);
752 1.1 hpeyerl } else {
753 1.3 mycroft /*
754 1.3 mycroft * Some other important flag might have changed, so
755 1.3 mycroft * reset.
756 1.3 mycroft */
757 1.30 mycroft elreset(sc);
758 1.1 hpeyerl }
759 1.24 mycroft break;
760 1.1 hpeyerl
761 1.1 hpeyerl default:
762 1.1 hpeyerl error = EINVAL;
763 1.24 mycroft break;
764 1.1 hpeyerl }
765 1.24 mycroft
766 1.22 mycroft splx(s);
767 1.3 mycroft return error;
768 1.1 hpeyerl }
769 1.1 hpeyerl
770 1.3 mycroft /*
771 1.3 mycroft * Device timeout routine.
772 1.3 mycroft */
773 1.30 mycroft void
774 1.38 thorpej elwatchdog(ifp)
775 1.38 thorpej struct ifnet *ifp;
776 1.1 hpeyerl {
777 1.38 thorpej struct el_softc *sc = ifp->if_softc;
778 1.1 hpeyerl
779 1.3 mycroft log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
780 1.44 is sc->sc_ethercom.ec_if.if_oerrors++;
781 1.1 hpeyerl
782 1.30 mycroft elreset(sc);
783 1.1 hpeyerl }
784