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