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