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