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