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