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