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