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