if_el.c revision 1.27 1 /* $NetBSD: if_el.c,v 1.27 1995/07/23 17:50:56 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 static int el_init __P((struct el_softc *));
86 static int el_ioctl __P((struct ifnet *, u_long, caddr_t));
87 static void el_start __P((struct ifnet *));
88 static void el_watchdog __P((int));
89 static void el_reset __P((struct el_softc *));
90 static void el_stop __P((struct el_softc *));
91 static int el_xmit __P((struct el_softc *, int));
92 static inline void elread __P((struct el_softc *, caddr_t, int));
93 static struct mbuf *elget __P((caddr_t, int, struct ifnet *));
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 el_stop(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 = el_start;
190 ifp->if_ioctl = el_ioctl;
191 ifp->if_watchdog = el_watchdog;
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 static void
218 el_reset(sc)
219 struct el_softc *sc;
220 {
221 int s;
222
223 dprintf(("elreset()\n"));
224 s = splimp();
225 el_stop(sc);
226 el_init(sc);
227 splx(s);
228 }
229
230 /*
231 * Stop interface.
232 */
233 static void
234 el_stop(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 static int
264 el_init(sc)
265 struct el_softc *sc;
266 {
267 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
268 int iobase = sc->sc_iobase;
269 int s;
270
271 s = splimp();
272
273 /* First, reset the board. */
274 el_hardreset(sc);
275
276 /* Configure rx. */
277 dprintf(("Configuring rx...\n"));
278 if (ifp->if_flags & IFF_PROMISC)
279 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_PROMISC);
280 else
281 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_ABROAD);
282 outb(iobase+EL_RBC, 0);
283
284 /* Configure TX. */
285 dprintf(("Configuring tx...\n"));
286 outb(iobase+EL_TXC, 0);
287
288 /* Start reception. */
289 dprintf(("Starting reception...\n"));
290 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
291
292 /* Set flags appropriately. */
293 ifp->if_flags |= IFF_RUNNING;
294 ifp->if_flags &= ~IFF_OACTIVE;
295
296 /* And start output. */
297 el_start(ifp);
298
299 splx(s);
300 }
301
302 /*
303 * Start output on interface. Get datagrams from the queue and output them,
304 * giving the receiver a chance between datagrams. Call only from splimp or
305 * interrupt level!
306 */
307 static void
308 el_start(ifp)
309 struct ifnet *ifp;
310 {
311 struct el_softc *sc = elcd.cd_devs[ifp->if_unit];
312 int iobase = sc->sc_iobase;
313 struct mbuf *m, *m0;
314 int s, i, off, retries;
315
316 dprintf(("el_start()...\n"));
317 s = splimp();
318
319 /* Don't do anything if output is active. */
320 if ((ifp->if_flags & IFF_OACTIVE) != 0) {
321 splx(s);
322 return;
323 }
324
325 ifp->if_flags |= IFF_OACTIVE;
326
327 /*
328 * The main loop. They warned me against endless loops, but would I
329 * listen? NOOO....
330 */
331 for (;;) {
332 /* Dequeue the next datagram. */
333 IF_DEQUEUE(&ifp->if_snd, m0);
334
335 /* If there's nothing to send, return. */
336 if (m0 == 0)
337 break;
338
339 #if NBPFILTER > 0
340 /* Give the packet to the bpf, if any. */
341 if (ifp->if_bpf)
342 bpf_mtap(ifp->if_bpf, m0);
343 #endif
344
345 /* Disable the receiver. */
346 outb(iobase+EL_AC, EL_AC_HOST);
347 outb(iobase+EL_RBC, 0);
348
349 /* Transfer datagram to board. */
350 dprintf(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
351 off = EL_BUFSIZ - max(m0->m_pkthdr.len, ETHER_MIN_LEN);
352 outb(iobase+EL_GPBL, off);
353 outb(iobase+EL_GPBH, off >> 8);
354
355 /* Copy the datagram to the buffer. */
356 for (m = m0; m != 0; m = m->m_next)
357 outsb(iobase+EL_BUF, mtod(m, caddr_t), m->m_len);
358
359 m_freem(m0);
360
361 /* Now transmit the datagram. */
362 retries = 0;
363 for (;;) {
364 outb(iobase+EL_GPBL, off);
365 outb(iobase+EL_GPBH, off >> 8);
366 if (el_xmit(sc))
367 break;
368 /* Check out status. */
369 i = inb(iobase+EL_TXS);
370 dprintf(("tx status=0x%x\n", i));
371 if ((i & EL_TXS_READY) == 0) {
372 dprintf(("el: err txs=%x\n", i));
373 ifp->if_oerrors++;
374 if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
375 if ((i & EL_TXC_DCOLL16) == 0 &&
376 retries < 15) {
377 retries++;
378 outb(iobase+EL_AC, EL_AC_HOST);
379 }
380 } else
381 break;
382 } else {
383 ifp->if_opackets++;
384 break;
385 }
386 }
387
388 /*
389 * Now give the card a chance to receive.
390 * Gotta love 3c501s...
391 */
392 (void)inb(iobase+EL_AS);
393 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
394 splx(s);
395 /* Interrupt here. */
396 s = splimp();
397 }
398
399 ifp->if_flags &= ~IFF_OACTIVE;
400 splx(s);
401 }
402
403 /*
404 * This function actually attempts to transmit a datagram downloaded to the
405 * board. Call at splimp or interrupt, after downloading data! Returns 0 on
406 * success, non-0 on failure.
407 */
408 static int
409 el_xmit(sc)
410 struct el_softc *sc;
411 {
412 int iobase = sc->sc_iobase;
413 int i;
414
415 dprintf(("el: xmit..."));
416 outb(iobase+EL_AC, EL_AC_TXFRX);
417 i = 20000;
418 while ((inb(iobase+EL_AS) & EL_AS_TXBUSY) && (i > 0))
419 i--;
420 if (i == 0) {
421 dprintf(("tx not ready\n"));
422 sc->sc_arpcom.ac_if.if_oerrors++;
423 return -1;
424 }
425 dprintf(("%d cycles.\n", 20000 - i));
426 return 0;
427 }
428
429 /*
430 * Controller interrupt.
431 */
432 int
433 elintr(arg)
434 void *arg;
435 {
436 register struct el_softc *sc = arg;
437 int iobase = sc->sc_iobase;
438 int stat, rxstat, len;
439
440 dprintf(("elintr: "));
441
442 /* Check board status. */
443 stat = inb(iobase+EL_AS);
444 if (stat & EL_AS_RXBUSY) {
445 (void)inb(iobase+EL_RXC);
446 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
447 return 0;
448 }
449
450 for (;;) {
451 rxstat = inb(iobase+EL_RXS);
452 if (rxstat & EL_RXS_STALE)
453 break;
454
455 /* If there's an overflow, reinit the board. */
456 if ((rxstat & EL_RXS_NOFLOW) == 0) {
457 dprintf(("overflow.\n"));
458 el_hardreset(sc);
459 reset:
460 /* Put board back into receive mode. */
461 if (sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC)
462 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_PROMISC);
463 else
464 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_ABROAD);
465 (void)inb(iobase+EL_AS);
466 outb(iobase+EL_RBC, 0);
467 break;
468 }
469
470 /* Incoming packet. */
471 len = inb(iobase+EL_RBL);
472 len |= inb(iobase+EL_RBH) << 8;
473 dprintf(("receive len=%d rxstat=%x ", len, rxstat));
474 outb(iobase+EL_AC, EL_AC_HOST);
475
476 /*
477 * If packet too short or too long, restore rx mode and return.
478 */
479 if (len <= sizeof(struct ether_header) ||
480 len > ETHER_MAX_LEN)
481 goto reset;
482
483 sc->sc_arpcom.ac_if.if_ipackets++;
484
485 /* Pass data up to upper levels. */
486 elread(sc, len);
487
488 /* Is there another packet? */
489 stat = inb(iobase+EL_AS);
490
491 /* If so, do it all again. */
492 if ((stat & EL_AS_RXBUSY) != 0)
493 break;
494
495 dprintf(("<rescan> "));
496 }
497
498 (void)inb(iobase+EL_RXC);
499 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
500 return 1;
501 }
502
503 /*
504 * Pass a packet up to the higher levels.
505 */
506 static inline void
507 elread(sc, len)
508 struct el_softc *sc;
509 int len;
510 {
511 struct ifnet *ifp;
512 struct mbuf *m;
513 struct ether_header *eh;
514
515 /* Pull packet off interface. */
516 ifp = &sc->sc_arpcom.ac_if;
517 m = elget(sc, len, ifp);
518 if (m == 0)
519 return;
520
521 /* We assume that the header fit entirely in one mbuf. */
522 eh = mtod(m, struct ether_header *);
523
524 #if NBPFILTER > 0
525 /*
526 * Check if there's a BPF listener on this interface.
527 * If so, hand off the raw packet to bpf.
528 */
529 if (ifp->if_bpf) {
530 bpf_mtap(ifp->if_bpf, m);
531
532 /*
533 * Note that the interface cannot be in promiscuous mode if
534 * there are no BPF listeners. And if we are in promiscuous
535 * mode, we have to check if this packet is really ours.
536 */
537 if ((ifp->if_flags & IFF_PROMISC) &&
538 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
539 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
540 sizeof(eh->ether_dhost)) != 0) {
541 m_freem(m);
542 return;
543 }
544 }
545 #endif
546
547 /* We assume that the header fit entirely in one mbuf. */
548 m->m_pkthdr.len -= sizeof(*eh);
549 m->m_len -= sizeof(*eh);
550 m->m_data += sizeof(*eh);
551
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, ifp)
562 struct el_softc *sc;
563 int totlen;
564 struct ifnet *ifp;
565 {
566 struct mbuf *top, **mp, *m;
567 int len;
568 int iobase = sc->sc_iobase;
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 static int
613 el_ioctl(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 el_init(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 el_init(sc);
652 break;
653 }
654 #endif
655 default:
656 el_init(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 el_stop(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 el_init(sc);
677 } else {
678 /*
679 * Some other important flag might have changed, so
680 * reset.
681 */
682 el_reset(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 static void
699 el_watchdog(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 el_reset(sc);
708 }
709