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