1 /* Copyright (c) 1994, Matthew E. Kimmel. Permission is hereby granted
2 * to use, copy, modify and distribute this software provided that both
3 * the copyright notice and this permission notice appear in all copies
4 * of the software, derivative works or modified versions, and any
7 * Questions, comments, bug reports and fixes to kimmel@cs.umass.edu.
11 /* Except of course for the portions of code lifted from other FreeBSD
12 * drivers (mainly elread, elget and el_ioctl)
14 /* 3COM Etherlink 3C501 device driver for FreeBSD */
15 /* Yeah, I know these cards suck, but you can also get them for free
18 /* Bugs/possible improvements:
19 * - Does not currently support DMA
20 * - Does not currently support multicasts
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/sockio.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
33 #include <net/ethernet.h>
36 #include <netinet/in.h>
37 #include <netinet/if_ether.h>
41 #include <machine/clock.h>
43 #include <i386/isa/isa_device.h>
44 #include <i386/isa/if_elreg.h>
46 /* For debugging convenience */
48 #define dprintf(x) printf x
53 /* el_softc: per line info and status */
54 static struct el_softc {
55 struct arpcom arpcom; /* Ethernet common */
56 u_short el_base; /* Base I/O addr */
57 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
61 static int el_attach(struct isa_device *);
62 static void el_init(void *);
63 static int el_ioctl(struct ifnet *,u_long,caddr_t);
64 static int el_probe(struct isa_device *);
65 static void el_start(struct ifnet *);
66 static void el_reset(void *);
67 static void el_watchdog(struct ifnet *);
69 static void el_stop(void *);
70 static int el_xmit(struct el_softc *,int);
71 static ointhand2_t elintr;
72 static __inline void elread(struct el_softc *,caddr_t,int);
73 static struct mbuf *elget(caddr_t,int,int,struct ifnet *);
74 static __inline void el_hardreset(void *);
76 /* isa_driver structure for autoconf */
77 struct isa_driver eldriver = {
78 el_probe, el_attach, "el"
81 /* Probe routine. See if the card is there and at the right place. */
83 el_probe(struct isa_device *idev)
86 u_short base; /* Just for convenience */
87 u_char station_addr[ETHER_ADDR_LEN];
90 /* Grab some info for our structure */
91 sc = &el_softc[idev->id_unit];
92 sc->el_base = idev->id_iobase;
95 /* First check the base */
96 if((base < 0x280) || (base > 0x3f0)) {
97 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
102 /* Now attempt to grab the station address from the PROM
103 * and see if it contains the 3com vendor code.
105 dprintf(("Probing 3c501 at 0x%x...\n",base));
107 /* Reset the board */
108 dprintf(("Resetting board...\n"));
109 outb(base+EL_AC,EL_AC_RESET);
112 dprintf(("Reading station address...\n"));
113 /* Now read the address */
114 for(i=0;i<ETHER_ADDR_LEN;i++) {
115 outb(base+EL_GPBL,i);
116 station_addr[i] = inb(base+EL_EAW);
118 dprintf(("Address is %6D\n",station_addr, ":"));
120 /* If the vendor code is ok, return a 1. We'll assume that
121 * whoever configured this system is right about the IRQ.
123 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
124 || (station_addr[2] != 0x8c)) {
125 dprintf(("Bad vendor code.\n"));
128 dprintf(("Vendor code ok.\n"));
129 /* Copy the station address into the arpcom structure */
130 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
135 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
140 register struct el_softc *sc = xsc;
146 /* First reset the board */
147 outb(base+EL_AC,EL_AC_RESET);
151 /* Then give it back its ethernet address. Thanks to the mach
152 * source code for this undocumented goodie...
154 for(j=0;j<ETHER_ADDR_LEN;j++)
155 outb(base+j,sc->arpcom.ac_enaddr[j]);
158 /* Attach the interface to the kernel data structures. By the time
159 * this is called, we know that the card exists at the given I/O address.
160 * We still assume that the IRQ given is correct.
163 el_attach(struct isa_device *idev)
169 dprintf(("Attaching el%d...\n",idev->id_unit));
171 /* Get things pointing to the right places. */
172 idev->id_ointr = elintr;
173 sc = &el_softc[idev->id_unit];
174 ifp = &sc->arpcom.ac_if;
177 /* Now reset the board */
178 dprintf(("Resetting board...\n"));
181 /* Initialize ifnet structure */
183 ifp->if_unit = idev->id_unit;
185 ifp->if_mtu = ETHERMTU;
186 ifp->if_output = ether_output;
187 ifp->if_start = el_start;
188 ifp->if_ioctl = el_ioctl;
189 ifp->if_watchdog = el_watchdog;
190 ifp->if_init = el_init;
191 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
193 /* Now we can attach the interface */
194 dprintf(("Attaching interface...\n"));
198 /* Print out some information for the user */
199 printf("el%d: 3c501 address %6D\n",idev->id_unit,
200 sc->arpcom.ac_enaddr, ":");
202 /* Finally, attach to bpf filter if it is present. */
203 dprintf(("Attaching to BPF...\n"));
204 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
206 dprintf(("el_attach() finished.\n"));
210 /* This routine resets the interface. */
215 struct el_softc *sc = xsc;
218 dprintf(("elreset()\n"));
225 static void el_stop(xsc)
228 struct el_softc *sc = xsc;
230 outb(sc->el_base+EL_AC,0);
233 /* Initialize interface. */
238 struct el_softc *sc = xsc;
243 /* Set up pointers */
244 ifp = &sc->arpcom.ac_if;
247 /* If address not known, do nothing. */
248 if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
253 /* First, reset the board. */
254 dprintf(("Resetting board...\n"));
258 dprintf(("Configuring rx...\n"));
259 if(ifp->if_flags & IFF_PROMISC)
260 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
262 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
266 dprintf(("Configuring tx...\n"));
269 /* Start reception */
270 dprintf(("Starting reception...\n"));
271 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
273 /* Set flags appropriately */
274 ifp->if_flags |= IFF_RUNNING;
275 ifp->if_flags &= ~IFF_OACTIVE;
277 /* And start output. */
283 /* Start output on interface. Get datagrams from the queue and output
284 * them, giving the receiver a chance between datagrams. Call only
285 * from splimp or interrupt level!
288 el_start(struct ifnet *ifp)
293 int s, i, len, retries, done;
295 /* Get things pointing in the right directions */
299 dprintf(("el_start()...\n"));
302 /* Don't do anything if output is active */
303 if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
305 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
307 /* The main loop. They warned me against endless loops, but
308 * would I listen? NOOO....
311 /* Dequeue the next datagram */
312 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
314 /* If there's nothing to send, return. */
316 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
321 /* Disable the receiver */
322 outb(base+EL_AC,EL_AC_HOST);
325 /* Copy the datagram to the buffer. */
327 for(m = m0; m != NULL; m = m->m_next) {
330 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
335 len = max(len,ETHER_MIN_LEN);
337 /* Give the packet to the bpf, if any */
338 if(sc->arpcom.ac_if.if_bpf)
339 bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
341 /* Transfer datagram to board */
342 dprintf(("el: xfr pkt length=%d...\n",len));
344 outb(base+EL_GPBL,(i & 0xff));
345 outb(base+EL_GPBH,((i>>8)&0xff));
346 outsb(base+EL_BUF,sc->el_pktbuf,len);
348 /* Now transmit the datagram */
352 if(el_xmit(sc,len)) { /* Something went wrong */
356 /* Check out status */
357 i = inb(base+EL_TXS);
358 dprintf(("tx status=0x%x\n",i));
359 if(!(i & EL_TXS_READY)) {
360 dprintf(("el: err txs=%x\n",i));
361 sc->arpcom.ac_if.if_oerrors++;
362 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
363 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
365 outb(base+EL_AC,EL_AC_HOST);
372 sc->arpcom.ac_if.if_opackets++;
376 if(done == -1) /* Packet not transmitted */
379 /* Now give the card a chance to receive.
380 * Gotta love 3c501s...
382 (void)inb(base+EL_AS);
383 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
390 /* This function actually attempts to transmit a datagram downloaded
391 * to the board. Call at splimp or interrupt, after downloading data!
392 * Returns 0 on success, non-0 on failure
395 el_xmit(struct el_softc *sc,int len)
400 gpl = EL_BUFSIZ - len;
401 dprintf(("el: xmit..."));
402 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
403 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
404 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
406 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
409 dprintf(("tx not ready\n"));
410 sc->arpcom.ac_if.if_oerrors++;
413 dprintf(("%d cycles.\n",(20000-i)));
417 /* Pass a packet up to the higher levels. */
419 elread(struct el_softc *sc,caddr_t buf,int len)
421 register struct ether_header *eh;
424 eh = (struct ether_header *)buf;
427 * Check if there's a bpf filter listening on this interface.
428 * If so, hand off the raw packet to bpf.
430 if(sc->arpcom.ac_if.if_bpf) {
431 bpf_tap(&sc->arpcom.ac_if, buf,
432 len + sizeof(struct ether_header));
435 * Note that the interface cannot be in promiscuous mode if
436 * there are no bpf listeners. And if el are in promiscuous
437 * mode, el have to check if this packet is really ours.
439 * This test does not support multicasts.
441 if((sc->arpcom.ac_if.if_flags & IFF_PROMISC)
442 && bcmp(eh->ether_dhost,sc->arpcom.ac_enaddr,
443 sizeof(eh->ether_dhost)) != 0
444 && bcmp(eh->ether_dhost,etherbroadcastaddr,
445 sizeof(eh->ether_dhost)) != 0)
450 * Pull packet off interface.
452 m = elget(buf,len,0,&sc->arpcom.ac_if);
456 ether_input(&sc->arpcom.ac_if,eh,m);
459 /* controller interrupt */
463 register struct el_softc *sc;
465 int stat, rxstat, len, done;
467 /* Get things pointing properly */
468 sc = &el_softc[unit];
471 dprintf(("elintr: "));
473 /* Check board status */
474 stat = inb(base+EL_AS);
475 if(stat & EL_AS_RXBUSY) {
476 (void)inb(base+EL_RXC);
477 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
483 rxstat = inb(base+EL_RXS);
484 if(rxstat & EL_RXS_STALE) {
485 (void)inb(base+EL_RXC);
486 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
490 /* If there's an overflow, reinit the board. */
491 if(!(rxstat & EL_RXS_NOFLOW)) {
492 dprintf(("overflow.\n"));
494 /* Put board back into receive mode */
495 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
496 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
498 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
499 (void)inb(base+EL_AS);
501 (void)inb(base+EL_RXC);
502 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
506 /* Incoming packet */
507 len = inb(base+EL_RBL);
508 len |= inb(base+EL_RBH) << 8;
509 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
510 outb(base+EL_AC,EL_AC_HOST);
512 /* If packet too short or too long, restore rx mode and return
514 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
515 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
516 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
518 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
519 (void)inb(base+EL_AS);
521 (void)inb(base+EL_RXC);
522 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
526 sc->arpcom.ac_if.if_ipackets++;
528 /* Copy the data into our buffer */
529 outb(base+EL_GPBL,0);
530 outb(base+EL_GPBH,0);
531 insb(base+EL_BUF,sc->el_pktbuf,len);
533 outb(base+EL_AC,EL_AC_RX);
534 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
535 dprintf(("%6D\n",sc->el_pktbuf,":"));
537 /* Pass data up to upper levels */
538 len -= sizeof(struct ether_header);
539 elread(sc,(caddr_t)(sc->el_pktbuf),len);
541 /* Is there another packet? */
542 stat = inb(base+EL_AS);
544 /* If so, do it all again (i.e. don't set done to 1) */
545 if(!(stat & EL_AS_RXBUSY))
546 dprintf(("<rescan> "));
551 (void)inb(base+EL_RXC);
552 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
557 * Pull read data off a interface.
558 * Len is length of data, with local net header stripped.
561 elget(buf, totlen, off0, ifp)
566 struct mbuf *top, **mp, *m;
568 register caddr_t cp = buf;
571 buf += sizeof(struct ether_header);
577 cp += off + 2 * sizeof(u_short);
578 totlen -= 2 * sizeof(u_short);
581 MGETHDR(m, M_DONTWAIT, MT_DATA);
584 m->m_pkthdr.rcvif = ifp;
585 m->m_pkthdr.len = totlen;
591 MGET(m, M_DONTWAIT, MT_DATA);
598 len = min(totlen, epkt - cp);
599 if (len >= MINCLSIZE) {
600 MCLGET(m, M_DONTWAIT);
601 if (m->m_flags & M_EXT)
602 m->m_len = len = min(len, MCLBYTES);
607 * Place initial small packet/header at end of mbuf.
609 if (len < m->m_len) {
610 if (top == 0 && len + max_linkhdr <= m->m_len)
611 m->m_data += max_linkhdr;
616 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
628 * Process an ioctl request. This code needs some work - it looks
632 el_ioctl(ifp, command, data)
633 register struct ifnet *ifp;
645 error = ether_ioctl(ifp, command, data);
650 * If interface is marked down and it is running, then stop it
652 if (((ifp->if_flags & IFF_UP) == 0) &&
653 (ifp->if_flags & IFF_RUNNING)) {
654 el_stop(ifp->if_softc);
655 ifp->if_flags &= ~IFF_RUNNING;
658 * If interface is marked up and it is stopped, then start it
660 if ((ifp->if_flags & IFF_UP) &&
661 ((ifp->if_flags & IFF_RUNNING) == 0))
662 el_init(ifp->if_softc);
672 /* Device timeout routine */
674 el_watchdog(struct ifnet *ifp)
676 log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
678 el_reset(ifp->if_softc);