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