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