if_el.c revision 1.32 1 /* $NetBSD: if_el.c,v 1.32 1995/07/24 04:12:50 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 break;
363 /* Check out status. */
364 i = inb(iobase+EL_TXS);
365 dprintf(("tx status=0x%x\n", i));
366 if ((i & EL_TXS_READY) == 0) {
367 dprintf(("el: err txs=%x\n", i));
368 ifp->if_oerrors++;
369 if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
370 if ((i & EL_TXC_DCOLL16) == 0 &&
371 retries < 15) {
372 retries++;
373 outb(iobase+EL_AC, EL_AC_HOST);
374 }
375 } else
376 break;
377 } else {
378 ifp->if_opackets++;
379 break;
380 }
381 }
382
383 /*
384 * Now give the card a chance to receive.
385 * Gotta love 3c501s...
386 */
387 (void)inb(iobase+EL_AS);
388 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
389 splx(s);
390 /* Interrupt here. */
391 s = splimp();
392 }
393
394 ifp->if_flags &= ~IFF_OACTIVE;
395 splx(s);
396 }
397
398 /*
399 * This function actually attempts to transmit a datagram downloaded to the
400 * board. Call at splimp or interrupt, after downloading data! Returns 0 on
401 * success, non-0 on failure.
402 */
403 static int
404 el_xmit(sc)
405 struct el_softc *sc;
406 {
407 int iobase = sc->sc_iobase;
408 int i;
409
410 dprintf(("el: xmit..."));
411 outb(iobase+EL_AC, EL_AC_TXFRX);
412 i = 20000;
413 while ((inb(iobase+EL_AS) & EL_AS_TXBUSY) && (i > 0))
414 i--;
415 if (i == 0) {
416 dprintf(("tx not ready\n"));
417 sc->sc_arpcom.ac_if.if_oerrors++;
418 return -1;
419 }
420 dprintf(("%d cycles.\n", 20000 - i));
421 return 0;
422 }
423
424 /*
425 * Controller interrupt.
426 */
427 int
428 elintr(arg)
429 void *arg;
430 {
431 register struct el_softc *sc = arg;
432 int iobase = sc->sc_iobase;
433 int stat, rxstat, len;
434
435 dprintf(("elintr: "));
436
437 /* Check board status. */
438 stat = inb(iobase+EL_AS);
439 if (stat & EL_AS_RXBUSY) {
440 (void)inb(iobase+EL_RXC);
441 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
442 return 0;
443 }
444
445 for (;;) {
446 rxstat = inb(iobase+EL_RXS);
447 if (rxstat & EL_RXS_STALE)
448 break;
449
450 /* If there's an overflow, reinit the board. */
451 if ((rxstat & EL_RXS_NOFLOW) == 0) {
452 dprintf(("overflow.\n"));
453 el_hardreset(sc);
454 reset:
455 /* Put board back into receive mode. */
456 if (sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC)
457 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_PROMISC);
458 else
459 outb(iobase+EL_RXC, EL_RXC_AGF | EL_RXC_DSHORT | EL_RXC_DDRIB | EL_RXC_DOFLOW | EL_RXC_ABROAD);
460 (void)inb(iobase+EL_AS);
461 outb(iobase+EL_RBC, 0);
462 break;
463 }
464
465 /* Incoming packet. */
466 len = inb(iobase+EL_RBL);
467 len |= inb(iobase+EL_RBH) << 8;
468 dprintf(("receive len=%d rxstat=%x ", len, rxstat));
469 outb(iobase+EL_AC, EL_AC_HOST);
470
471 /* Pass data up to upper levels. */
472 elread(sc, len);
473
474 /* Is there another packet? */
475 stat = inb(iobase+EL_AS);
476
477 /* If so, do it all again. */
478 if ((stat & EL_AS_RXBUSY) != 0)
479 break;
480
481 dprintf(("<rescan> "));
482 }
483
484 (void)inb(iobase+EL_RXC);
485 outb(iobase+EL_AC, EL_AC_IRQE | EL_AC_RX);
486 return 1;
487 }
488
489 /*
490 * Pass a packet to the higher levels.
491 */
492 void
493 elread(sc, len)
494 register struct el_softc *sc;
495 int len;
496 {
497 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
498 struct mbuf *m;
499 struct ether_header *eh;
500
501 if (len <= sizeof(struct ether_header) ||
502 len > ETHER_MAX_LEN) {
503 printf("%s: invalid packet size %d; dropping\n",
504 sc->sc_dev.dv_xname, len);
505 ifp->if_ierrors++;
506 return;
507 }
508
509 /* Pull packet off interface. */
510 m = elget(sc, len);
511 if (m == 0) {
512 ifp->if_ierrors++;
513 return;
514 }
515
516 ifp->if_ipackets++;
517
518 /* We assume that the header fit entirely in one mbuf. */
519 eh = mtod(m, struct ether_header *);
520
521 #if NBPFILTER > 0
522 /*
523 * Check if there's a BPF listener on this interface.
524 * If so, hand off the raw packet to BPF.
525 */
526 if (ifp->if_bpf) {
527 bpf_mtap(ifp->if_bpf, m);
528
529 /*
530 * Note that the interface cannot be in promiscuous mode if
531 * there are no BPF listeners. And if we are in promiscuous
532 * mode, we have to check if this packet is really ours.
533 */
534 if ((ifp->if_flags & IFF_PROMISC) &&
535 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
536 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
537 sizeof(eh->ether_dhost)) != 0) {
538 m_freem(m);
539 return;
540 }
541 }
542 #endif
543
544 /* We assume that the header fit entirely in one mbuf. */
545 m_adj(m, sizeof(struct ether_header));
546 ether_input(ifp, eh, m);
547 }
548
549 /*
550 * Pull read data off a interface. Len is length of data, with local net
551 * header stripped. We copy the data into mbufs. When full cluster sized
552 * units are present we copy into clusters.
553 */
554 struct mbuf *
555 elget(sc, totlen)
556 struct el_softc *sc;
557 int totlen;
558 {
559 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
560 int iobase = sc->sc_iobase;
561 struct mbuf *top, **mp, *m;
562 int len;
563
564 MGETHDR(m, M_DONTWAIT, MT_DATA);
565 if (m == 0)
566 return 0;
567 m->m_pkthdr.rcvif = ifp;
568 m->m_pkthdr.len = totlen;
569 len = MHLEN;
570 top = 0;
571 mp = ⊤
572
573 outb(iobase+EL_GPBL, 0);
574 outb(iobase+EL_GPBH, 0);
575
576 while (totlen > 0) {
577 if (top) {
578 MGET(m, M_DONTWAIT, MT_DATA);
579 if (m == 0) {
580 m_freem(top);
581 return 0;
582 }
583 len = MLEN;
584 }
585 if (totlen >= MINCLSIZE) {
586 MCLGET(m, M_DONTWAIT);
587 if (m->m_flags & M_EXT)
588 len = MCLBYTES;
589 }
590 m->m_len = len = min(totlen, len);
591 insb(iobase+EL_BUF, mtod(m, caddr_t), len);
592 totlen -= len;
593 *mp = m;
594 mp = &m->m_next;
595 }
596
597 outb(iobase+EL_RBC, 0);
598 outb(iobase+EL_AC, EL_AC_RX);
599
600 return top;
601 }
602
603 /*
604 * Process an ioctl request. This code needs some work - it looks pretty ugly.
605 */
606 int
607 elioctl(ifp, cmd, data)
608 register struct ifnet *ifp;
609 u_long cmd;
610 caddr_t data;
611 {
612 struct el_softc *sc = elcd.cd_devs[ifp->if_unit];
613 struct ifaddr *ifa = (struct ifaddr *)data;
614 struct ifreq *ifr = (struct ifreq *)data;
615 int s, error = 0;
616
617 s = splimp();
618
619 switch (cmd) {
620
621 case SIOCSIFADDR:
622 ifp->if_flags |= IFF_UP;
623
624 switch (ifa->ifa_addr->sa_family) {
625 #ifdef INET
626 case AF_INET:
627 elinit(sc);
628 arp_ifinit(&sc->sc_arpcom, ifa);
629 break;
630 #endif
631 #ifdef NS
632 /* XXX - This code is probably wrong. */
633 case AF_NS:
634 {
635 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
636
637 if (ns_nullhost(*ina))
638 ina->x_host =
639 *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
640 else
641 bcopy(ina->x_host.c_host,
642 sc->sc_arpcom.ac_enaddr,
643 sizeof(sc->sc_arpcom.ac_enaddr));
644 /* Set new address. */
645 elinit(sc);
646 break;
647 }
648 #endif
649 default:
650 elinit(sc);
651 break;
652 }
653 break;
654
655 case SIOCSIFFLAGS:
656 if ((ifp->if_flags & IFF_UP) == 0 &&
657 (ifp->if_flags & IFF_RUNNING) != 0) {
658 /*
659 * If interface is marked down and it is running, then
660 * stop it.
661 */
662 elstop(sc);
663 ifp->if_flags &= ~IFF_RUNNING;
664 } else if ((ifp->if_flags & IFF_UP) != 0 &&
665 (ifp->if_flags & IFF_RUNNING) == 0) {
666 /*
667 * If interface is marked up and it is stopped, then
668 * start it.
669 */
670 elinit(sc);
671 } else {
672 /*
673 * Some other important flag might have changed, so
674 * reset.
675 */
676 elreset(sc);
677 }
678 break;
679
680 default:
681 error = EINVAL;
682 break;
683 }
684
685 splx(s);
686 return error;
687 }
688
689 /*
690 * Device timeout routine.
691 */
692 void
693 elwatchdog(unit)
694 int unit;
695 {
696 struct el_softc *sc = elcd.cd_devs[unit];
697
698 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
699 sc->sc_arpcom.ac_if.if_oerrors++;
700
701 elreset(sc);
702 }
703