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