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.
9 * $FreeBSD: src/sys/i386/isa/if_el.c,v 1.47.2.2 2000/07/17 21:24:30 archie Exp $
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,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"));
195 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
197 /* Print out some information for the user */
198 printf("el%d: 3c501 address %6D\n",idev->id_unit,
199 sc->arpcom.ac_enaddr, ":");
201 dprintf(("el_attach() finished.\n"));
205 /* This routine resets the interface. */
210 struct el_softc *sc = xsc;
213 dprintf(("elreset()\n"));
220 static void el_stop(xsc)
223 struct el_softc *sc = xsc;
225 outb(sc->el_base+EL_AC,0);
228 /* Initialize interface. */
233 struct el_softc *sc = xsc;
238 /* Set up pointers */
239 ifp = &sc->arpcom.ac_if;
242 /* If address not known, do nothing. */
243 if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
248 /* First, reset the board. */
249 dprintf(("Resetting board...\n"));
253 dprintf(("Configuring rx...\n"));
254 if(ifp->if_flags & IFF_PROMISC)
255 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
257 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
261 dprintf(("Configuring tx...\n"));
264 /* Start reception */
265 dprintf(("Starting reception...\n"));
266 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
268 /* Set flags appropriately */
269 ifp->if_flags |= IFF_RUNNING;
270 ifp->if_flags &= ~IFF_OACTIVE;
272 /* And start output. */
278 /* Start output on interface. Get datagrams from the queue and output
279 * them, giving the receiver a chance between datagrams. Call only
280 * from splimp or interrupt level!
283 el_start(struct ifnet *ifp)
288 int s, i, len, retries, done;
290 /* Get things pointing in the right directions */
294 dprintf(("el_start()...\n"));
297 /* Don't do anything if output is active */
298 if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
300 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
302 /* The main loop. They warned me against endless loops, but
303 * would I listen? NOOO....
306 /* Dequeue the next datagram */
307 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
309 /* If there's nothing to send, return. */
311 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
316 /* Disable the receiver */
317 outb(base+EL_AC,EL_AC_HOST);
320 /* Copy the datagram to the buffer. */
322 for(m = m0; m != NULL; m = m->m_next) {
325 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
330 len = max(len,ETHER_MIN_LEN);
332 /* Give the packet to the bpf, if any */
333 if(sc->arpcom.ac_if.if_bpf)
334 bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
336 /* Transfer datagram to board */
337 dprintf(("el: xfr pkt length=%d...\n",len));
339 outb(base+EL_GPBL,(i & 0xff));
340 outb(base+EL_GPBH,((i>>8)&0xff));
341 outsb(base+EL_BUF,sc->el_pktbuf,len);
343 /* Now transmit the datagram */
347 if(el_xmit(sc,len)) { /* Something went wrong */
351 /* Check out status */
352 i = inb(base+EL_TXS);
353 dprintf(("tx status=0x%x\n",i));
354 if(!(i & EL_TXS_READY)) {
355 dprintf(("el: err txs=%x\n",i));
356 sc->arpcom.ac_if.if_oerrors++;
357 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
358 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
360 outb(base+EL_AC,EL_AC_HOST);
367 sc->arpcom.ac_if.if_opackets++;
371 if(done == -1) /* Packet not transmitted */
374 /* Now give the card a chance to receive.
375 * Gotta love 3c501s...
377 (void)inb(base+EL_AS);
378 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
385 /* This function actually attempts to transmit a datagram downloaded
386 * to the board. Call at splimp or interrupt, after downloading data!
387 * Returns 0 on success, non-0 on failure
390 el_xmit(struct el_softc *sc,int len)
395 gpl = EL_BUFSIZ - len;
396 dprintf(("el: xmit..."));
397 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
398 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
399 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
401 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
404 dprintf(("tx not ready\n"));
405 sc->arpcom.ac_if.if_oerrors++;
408 dprintf(("%d cycles.\n",(20000-i)));
412 /* Pass a packet up to the higher levels. */
414 elread(struct el_softc *sc,caddr_t buf,int len)
416 register struct ether_header *eh;
419 eh = (struct ether_header *)buf;
422 * Put packet into an mbuf chain
424 m = elget(buf,len,&sc->arpcom.ac_if);
428 ether_input(&sc->arpcom.ac_if,eh,m);
431 /* controller interrupt */
435 register struct el_softc *sc;
437 int stat, rxstat, len, done;
439 /* Get things pointing properly */
440 sc = &el_softc[unit];
443 dprintf(("elintr: "));
445 /* Check board status */
446 stat = inb(base+EL_AS);
447 if(stat & EL_AS_RXBUSY) {
448 (void)inb(base+EL_RXC);
449 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
455 rxstat = inb(base+EL_RXS);
456 if(rxstat & EL_RXS_STALE) {
457 (void)inb(base+EL_RXC);
458 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
462 /* If there's an overflow, reinit the board. */
463 if(!(rxstat & EL_RXS_NOFLOW)) {
464 dprintf(("overflow.\n"));
466 /* Put board back into receive mode */
467 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
468 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
470 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
471 (void)inb(base+EL_AS);
473 (void)inb(base+EL_RXC);
474 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
478 /* Incoming packet */
479 len = inb(base+EL_RBL);
480 len |= inb(base+EL_RBH) << 8;
481 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
482 outb(base+EL_AC,EL_AC_HOST);
484 /* If packet too short or too long, restore rx mode and return
486 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
487 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
488 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
490 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
491 (void)inb(base+EL_AS);
493 (void)inb(base+EL_RXC);
494 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
498 sc->arpcom.ac_if.if_ipackets++;
500 /* Copy the data into our buffer */
501 outb(base+EL_GPBL,0);
502 outb(base+EL_GPBH,0);
503 insb(base+EL_BUF,sc->el_pktbuf,len);
505 outb(base+EL_AC,EL_AC_RX);
506 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
507 dprintf(("%6D\n",sc->el_pktbuf,":"));
509 /* Pass data up to upper levels */
510 len -= sizeof(struct ether_header);
511 elread(sc,(caddr_t)(sc->el_pktbuf),len);
513 /* Is there another packet? */
514 stat = inb(base+EL_AS);
516 /* If so, do it all again (i.e. don't set done to 1) */
517 if(!(stat & EL_AS_RXBUSY))
518 dprintf(("<rescan> "));
523 (void)inb(base+EL_RXC);
524 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
529 * Pull read data off a interface.
530 * Len is length of data, with local net header stripped.
533 elget(buf, totlen, ifp)
538 struct mbuf *top, **mp, *m;
543 buf += sizeof(struct ether_header);
547 MGETHDR(m, M_DONTWAIT, MT_DATA);
550 m->m_pkthdr.rcvif = ifp;
551 m->m_pkthdr.len = totlen;
557 MGET(m, M_DONTWAIT, MT_DATA);
564 len = min(totlen, epkt - cp);
565 if (len >= MINCLSIZE) {
566 MCLGET(m, M_DONTWAIT);
567 if (m->m_flags & M_EXT)
568 m->m_len = len = min(len, MCLBYTES);
573 * Place initial small packet/header at end of mbuf.
575 if (len < m->m_len) {
576 if (top == 0 && len + max_linkhdr <= m->m_len)
577 m->m_data += max_linkhdr;
582 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
594 * Process an ioctl request. This code needs some work - it looks
598 el_ioctl(ifp, command, data)
599 register struct ifnet *ifp;
611 error = ether_ioctl(ifp, command, data);
616 * If interface is marked down and it is running, then stop it
618 if (((ifp->if_flags & IFF_UP) == 0) &&
619 (ifp->if_flags & IFF_RUNNING)) {
620 el_stop(ifp->if_softc);
621 ifp->if_flags &= ~IFF_RUNNING;
624 * If interface is marked up and it is stopped, then start it
626 if ((ifp->if_flags & IFF_UP) &&
627 ((ifp->if_flags & IFF_RUNNING) == 0))
628 el_init(ifp->if_softc);
638 /* Device timeout routine */
640 el_watchdog(struct ifnet *ifp)
642 log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
644 el_reset(ifp->if_softc);