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 $
10 * $DragonFly: src/sys/dev/netif/el/if_el.c,v 1.6 2004/01/06 01:40:47 dillon Exp $
12 /* Except of course for the portions of code lifted from other FreeBSD
13 * drivers (mainly elread, elget and el_ioctl)
15 /* 3COM Etherlink 3C501 device driver for FreeBSD */
16 /* Yeah, I know these cards suck, but you can also get them for free
19 /* Bugs/possible improvements:
20 * - Does not currently support DMA
21 * - Does not currently support multicasts
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/sockio.h>
31 #include <sys/socket.h>
32 #include <sys/syslog.h>
33 #include <sys/linker_set.h>
34 #include <sys/module.h>
36 #include <net/ethernet.h>
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
44 #include <machine/clock.h>
46 #include <bus/isa/i386/isa_device.h>
49 DECLARE_DUMMY_MODULE(if_el);
51 /* For debugging convenience */
53 #define dprintf(x) printf x
58 /* el_softc: per line info and status */
59 static struct el_softc {
60 struct arpcom arpcom; /* Ethernet common */
61 u_short el_base; /* Base I/O addr */
62 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
66 static int el_attach(struct isa_device *);
67 static void el_init(void *);
68 static int el_ioctl(struct ifnet *,u_long,caddr_t);
69 static int el_probe(struct isa_device *);
70 static void el_start(struct ifnet *);
71 static void el_reset(void *);
72 static void el_watchdog(struct ifnet *);
74 static void el_stop(void *);
75 static int el_xmit(struct el_softc *,int);
76 static ointhand2_t elintr;
77 static __inline void elread(struct el_softc *,caddr_t,int);
78 static struct mbuf *elget(caddr_t,int,struct ifnet *);
79 static __inline void el_hardreset(void *);
81 /* isa_driver structure for autoconf */
82 struct isa_driver eldriver = {
83 el_probe, el_attach, "el"
86 /* Probe routine. See if the card is there and at the right place. */
88 el_probe(struct isa_device *idev)
91 u_short base; /* Just for convenience */
92 u_char station_addr[ETHER_ADDR_LEN];
95 /* Grab some info for our structure */
96 sc = &el_softc[idev->id_unit];
97 sc->el_base = idev->id_iobase;
100 /* First check the base */
101 if((base < 0x280) || (base > 0x3f0)) {
102 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
107 /* Now attempt to grab the station address from the PROM
108 * and see if it contains the 3com vendor code.
110 dprintf(("Probing 3c501 at 0x%x...\n",base));
112 /* Reset the board */
113 dprintf(("Resetting board...\n"));
114 outb(base+EL_AC,EL_AC_RESET);
117 dprintf(("Reading station address...\n"));
118 /* Now read the address */
119 for(i=0;i<ETHER_ADDR_LEN;i++) {
120 outb(base+EL_GPBL,i);
121 station_addr[i] = inb(base+EL_EAW);
123 dprintf(("Address is %6D\n",station_addr, ":"));
125 /* If the vendor code is ok, return a 1. We'll assume that
126 * whoever configured this system is right about the IRQ.
128 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
129 || (station_addr[2] != 0x8c)) {
130 dprintf(("Bad vendor code.\n"));
133 dprintf(("Vendor code ok.\n"));
134 /* Copy the station address into the arpcom structure */
135 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
140 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
145 struct el_softc *sc = xsc;
151 /* First reset the board */
152 outb(base+EL_AC,EL_AC_RESET);
156 /* Then give it back its ethernet address. Thanks to the mach
157 * source code for this undocumented goodie...
159 for(j=0;j<ETHER_ADDR_LEN;j++)
160 outb(base+j,sc->arpcom.ac_enaddr[j]);
163 /* Attach the interface to the kernel data structures. By the time
164 * this is called, we know that the card exists at the given I/O address.
165 * We still assume that the IRQ given is correct.
168 el_attach(struct isa_device *idev)
174 dprintf(("Attaching el%d...\n",idev->id_unit));
176 /* Get things pointing to the right places. */
177 idev->id_ointr = elintr;
178 sc = &el_softc[idev->id_unit];
179 ifp = &sc->arpcom.ac_if;
182 /* Now reset the board */
183 dprintf(("Resetting board...\n"));
186 /* Initialize ifnet structure */
188 if_initname(ifp, "el", idev->id_unit);
189 ifp->if_mtu = ETHERMTU;
190 ifp->if_output = ether_output;
191 ifp->if_start = el_start;
192 ifp->if_ioctl = el_ioctl;
193 ifp->if_watchdog = el_watchdog;
194 ifp->if_init = el_init;
195 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
197 /* Now we can attach the interface */
198 dprintf(("Attaching interface...\n"));
199 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
201 /* Print out some information for the user */
202 printf("el%d: 3c501 address %6D\n",idev->id_unit,
203 sc->arpcom.ac_enaddr, ":");
205 dprintf(("el_attach() finished.\n"));
209 /* This routine resets the interface. */
214 struct el_softc *sc = xsc;
217 dprintf(("elreset()\n"));
224 static void el_stop(xsc)
227 struct el_softc *sc = xsc;
229 outb(sc->el_base+EL_AC,0);
232 /* Initialize interface. */
237 struct el_softc *sc = xsc;
242 /* Set up pointers */
243 ifp = &sc->arpcom.ac_if;
246 /* If address not known, do nothing. */
247 if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
252 /* First, reset the board. */
253 dprintf(("Resetting board...\n"));
257 dprintf(("Configuring rx...\n"));
258 if(ifp->if_flags & IFF_PROMISC)
259 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
261 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
265 dprintf(("Configuring tx...\n"));
268 /* Start reception */
269 dprintf(("Starting reception...\n"));
270 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
272 /* Set flags appropriately */
273 ifp->if_flags |= IFF_RUNNING;
274 ifp->if_flags &= ~IFF_OACTIVE;
276 /* And start output. */
282 /* Start output on interface. Get datagrams from the queue and output
283 * them, giving the receiver a chance between datagrams. Call only
284 * from splimp or interrupt level!
287 el_start(struct ifnet *ifp)
292 int s, i, len, retries, done;
294 /* Get things pointing in the right directions */
298 dprintf(("el_start()...\n"));
301 /* Don't do anything if output is active */
302 if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
304 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
306 /* The main loop. They warned me against endless loops, but
307 * would I listen? NOOO....
310 /* Dequeue the next datagram */
311 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
313 /* If there's nothing to send, return. */
315 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
320 /* Disable the receiver */
321 outb(base+EL_AC,EL_AC_HOST);
324 /* Copy the datagram to the buffer. */
326 for(m = m0; m != NULL; m = m->m_next) {
329 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
334 len = max(len,ETHER_MIN_LEN);
336 /* Give the packet to the bpf, if any */
337 if(sc->arpcom.ac_if.if_bpf)
338 bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
340 /* Transfer datagram to board */
341 dprintf(("el: xfr pkt length=%d...\n",len));
343 outb(base+EL_GPBL,(i & 0xff));
344 outb(base+EL_GPBH,((i>>8)&0xff));
345 outsb(base+EL_BUF,sc->el_pktbuf,len);
347 /* Now transmit the datagram */
351 if(el_xmit(sc,len)) { /* Something went wrong */
355 /* Check out status */
356 i = inb(base+EL_TXS);
357 dprintf(("tx status=0x%x\n",i));
358 if(!(i & EL_TXS_READY)) {
359 dprintf(("el: err txs=%x\n",i));
360 sc->arpcom.ac_if.if_oerrors++;
361 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
362 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
364 outb(base+EL_AC,EL_AC_HOST);
371 sc->arpcom.ac_if.if_opackets++;
375 if(done == -1) /* Packet not transmitted */
378 /* Now give the card a chance to receive.
379 * Gotta love 3c501s...
381 (void)inb(base+EL_AS);
382 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
389 /* This function actually attempts to transmit a datagram downloaded
390 * to the board. Call at splimp or interrupt, after downloading data!
391 * Returns 0 on success, non-0 on failure
394 el_xmit(struct el_softc *sc,int len)
399 gpl = EL_BUFSIZ - len;
400 dprintf(("el: xmit..."));
401 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
402 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
403 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
405 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
408 dprintf(("tx not ready\n"));
409 sc->arpcom.ac_if.if_oerrors++;
412 dprintf(("%d cycles.\n",(20000-i)));
416 /* Pass a packet up to the higher levels. */
418 elread(struct el_softc *sc,caddr_t buf,int len)
420 struct ether_header *eh;
423 eh = (struct ether_header *)buf;
426 * Put packet into an mbuf chain
428 m = elget(buf,len,&sc->arpcom.ac_if);
432 ether_input(&sc->arpcom.ac_if,eh,m);
435 /* controller interrupt */
441 int stat, rxstat, len, done;
443 /* Get things pointing properly */
444 sc = &el_softc[unit];
447 dprintf(("elintr: "));
449 /* Check board status */
450 stat = inb(base+EL_AS);
451 if(stat & EL_AS_RXBUSY) {
452 (void)inb(base+EL_RXC);
453 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
459 rxstat = inb(base+EL_RXS);
460 if(rxstat & EL_RXS_STALE) {
461 (void)inb(base+EL_RXC);
462 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
466 /* If there's an overflow, reinit the board. */
467 if(!(rxstat & EL_RXS_NOFLOW)) {
468 dprintf(("overflow.\n"));
470 /* Put board back into receive mode */
471 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
472 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
474 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
475 (void)inb(base+EL_AS);
477 (void)inb(base+EL_RXC);
478 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
482 /* Incoming packet */
483 len = inb(base+EL_RBL);
484 len |= inb(base+EL_RBH) << 8;
485 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
486 outb(base+EL_AC,EL_AC_HOST);
488 /* If packet too short or too long, restore rx mode and return
490 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
491 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
492 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
494 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
495 (void)inb(base+EL_AS);
497 (void)inb(base+EL_RXC);
498 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
502 sc->arpcom.ac_if.if_ipackets++;
504 /* Copy the data into our buffer */
505 outb(base+EL_GPBL,0);
506 outb(base+EL_GPBH,0);
507 insb(base+EL_BUF,sc->el_pktbuf,len);
509 outb(base+EL_AC,EL_AC_RX);
510 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
511 dprintf(("%6D\n",sc->el_pktbuf,":"));
513 /* Pass data up to upper levels */
514 len -= sizeof(struct ether_header);
515 elread(sc,(caddr_t)(sc->el_pktbuf),len);
517 /* Is there another packet? */
518 stat = inb(base+EL_AS);
520 /* If so, do it all again (i.e. don't set done to 1) */
521 if(!(stat & EL_AS_RXBUSY))
522 dprintf(("<rescan> "));
527 (void)inb(base+EL_RXC);
528 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
533 * Pull read data off a interface.
534 * Len is length of data, with local net header stripped.
537 elget(buf, totlen, ifp)
542 struct mbuf *top, **mp, *m;
547 buf += sizeof(struct ether_header);
551 MGETHDR(m, M_DONTWAIT, MT_DATA);
554 m->m_pkthdr.rcvif = ifp;
555 m->m_pkthdr.len = totlen;
561 MGET(m, M_DONTWAIT, MT_DATA);
568 len = min(totlen, epkt - cp);
569 if (len >= MINCLSIZE) {
570 MCLGET(m, M_DONTWAIT);
571 if (m->m_flags & M_EXT)
572 m->m_len = len = min(len, MCLBYTES);
577 * Place initial small packet/header at end of mbuf.
579 if (len < m->m_len) {
580 if (top == 0 && len + max_linkhdr <= m->m_len)
581 m->m_data += max_linkhdr;
586 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
598 * Process an ioctl request. This code needs some work - it looks
602 el_ioctl(ifp, command, data)
615 error = ether_ioctl(ifp, command, data);
620 * If interface is marked down and it is running, then stop it
622 if (((ifp->if_flags & IFF_UP) == 0) &&
623 (ifp->if_flags & IFF_RUNNING)) {
624 el_stop(ifp->if_softc);
625 ifp->if_flags &= ~IFF_RUNNING;
628 * If interface is marked up and it is stopped, then start it
630 if ((ifp->if_flags & IFF_UP) &&
631 ((ifp->if_flags & IFF_RUNNING) == 0))
632 el_init(ifp->if_softc);
642 /* Device timeout routine */
644 el_watchdog(struct ifnet *ifp)
646 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
648 el_reset(ifp->if_softc);