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.4 2003/08/07 21:17:01 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>
34 #include <net/ethernet.h>
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
42 #include <machine/clock.h>
44 #include <bus/isa/i386/isa_device.h>
47 /* For debugging convenience */
49 #define dprintf(x) printf x
54 /* el_softc: per line info and status */
55 static struct el_softc {
56 struct arpcom arpcom; /* Ethernet common */
57 u_short el_base; /* Base I/O addr */
58 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
62 static int el_attach(struct isa_device *);
63 static void el_init(void *);
64 static int el_ioctl(struct ifnet *,u_long,caddr_t);
65 static int el_probe(struct isa_device *);
66 static void el_start(struct ifnet *);
67 static void el_reset(void *);
68 static void el_watchdog(struct ifnet *);
70 static void el_stop(void *);
71 static int el_xmit(struct el_softc *,int);
72 static ointhand2_t elintr;
73 static __inline void elread(struct el_softc *,caddr_t,int);
74 static struct mbuf *elget(caddr_t,int,struct ifnet *);
75 static __inline void el_hardreset(void *);
77 /* isa_driver structure for autoconf */
78 struct isa_driver eldriver = {
79 el_probe, el_attach, "el"
82 /* Probe routine. See if the card is there and at the right place. */
84 el_probe(struct isa_device *idev)
87 u_short base; /* Just for convenience */
88 u_char station_addr[ETHER_ADDR_LEN];
91 /* Grab some info for our structure */
92 sc = &el_softc[idev->id_unit];
93 sc->el_base = idev->id_iobase;
96 /* First check the base */
97 if((base < 0x280) || (base > 0x3f0)) {
98 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
103 /* Now attempt to grab the station address from the PROM
104 * and see if it contains the 3com vendor code.
106 dprintf(("Probing 3c501 at 0x%x...\n",base));
108 /* Reset the board */
109 dprintf(("Resetting board...\n"));
110 outb(base+EL_AC,EL_AC_RESET);
113 dprintf(("Reading station address...\n"));
114 /* Now read the address */
115 for(i=0;i<ETHER_ADDR_LEN;i++) {
116 outb(base+EL_GPBL,i);
117 station_addr[i] = inb(base+EL_EAW);
119 dprintf(("Address is %6D\n",station_addr, ":"));
121 /* If the vendor code is ok, return a 1. We'll assume that
122 * whoever configured this system is right about the IRQ.
124 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
125 || (station_addr[2] != 0x8c)) {
126 dprintf(("Bad vendor code.\n"));
129 dprintf(("Vendor code ok.\n"));
130 /* Copy the station address into the arpcom structure */
131 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
136 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
141 struct el_softc *sc = xsc;
147 /* First reset the board */
148 outb(base+EL_AC,EL_AC_RESET);
152 /* Then give it back its ethernet address. Thanks to the mach
153 * source code for this undocumented goodie...
155 for(j=0;j<ETHER_ADDR_LEN;j++)
156 outb(base+j,sc->arpcom.ac_enaddr[j]);
159 /* Attach the interface to the kernel data structures. By the time
160 * this is called, we know that the card exists at the given I/O address.
161 * We still assume that the IRQ given is correct.
164 el_attach(struct isa_device *idev)
170 dprintf(("Attaching el%d...\n",idev->id_unit));
172 /* Get things pointing to the right places. */
173 idev->id_ointr = elintr;
174 sc = &el_softc[idev->id_unit];
175 ifp = &sc->arpcom.ac_if;
178 /* Now reset the board */
179 dprintf(("Resetting board...\n"));
182 /* Initialize ifnet structure */
184 ifp->if_unit = idev->id_unit;
186 ifp->if_mtu = ETHERMTU;
187 ifp->if_output = ether_output;
188 ifp->if_start = el_start;
189 ifp->if_ioctl = el_ioctl;
190 ifp->if_watchdog = el_watchdog;
191 ifp->if_init = el_init;
192 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
194 /* Now we can attach the interface */
195 dprintf(("Attaching interface...\n"));
196 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
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 dprintf(("el_attach() finished.\n"));
206 /* This routine resets the interface. */
211 struct el_softc *sc = xsc;
214 dprintf(("elreset()\n"));
221 static void el_stop(xsc)
224 struct el_softc *sc = xsc;
226 outb(sc->el_base+EL_AC,0);
229 /* Initialize interface. */
234 struct el_softc *sc = xsc;
239 /* Set up pointers */
240 ifp = &sc->arpcom.ac_if;
243 /* If address not known, do nothing. */
244 if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
249 /* First, reset the board. */
250 dprintf(("Resetting board...\n"));
254 dprintf(("Configuring rx...\n"));
255 if(ifp->if_flags & IFF_PROMISC)
256 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
258 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
262 dprintf(("Configuring tx...\n"));
265 /* Start reception */
266 dprintf(("Starting reception...\n"));
267 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
269 /* Set flags appropriately */
270 ifp->if_flags |= IFF_RUNNING;
271 ifp->if_flags &= ~IFF_OACTIVE;
273 /* And start output. */
279 /* Start output on interface. Get datagrams from the queue and output
280 * them, giving the receiver a chance between datagrams. Call only
281 * from splimp or interrupt level!
284 el_start(struct ifnet *ifp)
289 int s, i, len, retries, done;
291 /* Get things pointing in the right directions */
295 dprintf(("el_start()...\n"));
298 /* Don't do anything if output is active */
299 if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
301 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
303 /* The main loop. They warned me against endless loops, but
304 * would I listen? NOOO....
307 /* Dequeue the next datagram */
308 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
310 /* If there's nothing to send, return. */
312 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
317 /* Disable the receiver */
318 outb(base+EL_AC,EL_AC_HOST);
321 /* Copy the datagram to the buffer. */
323 for(m = m0; m != NULL; m = m->m_next) {
326 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
331 len = max(len,ETHER_MIN_LEN);
333 /* Give the packet to the bpf, if any */
334 if(sc->arpcom.ac_if.if_bpf)
335 bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
337 /* Transfer datagram to board */
338 dprintf(("el: xfr pkt length=%d...\n",len));
340 outb(base+EL_GPBL,(i & 0xff));
341 outb(base+EL_GPBH,((i>>8)&0xff));
342 outsb(base+EL_BUF,sc->el_pktbuf,len);
344 /* Now transmit the datagram */
348 if(el_xmit(sc,len)) { /* Something went wrong */
352 /* Check out status */
353 i = inb(base+EL_TXS);
354 dprintf(("tx status=0x%x\n",i));
355 if(!(i & EL_TXS_READY)) {
356 dprintf(("el: err txs=%x\n",i));
357 sc->arpcom.ac_if.if_oerrors++;
358 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
359 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
361 outb(base+EL_AC,EL_AC_HOST);
368 sc->arpcom.ac_if.if_opackets++;
372 if(done == -1) /* Packet not transmitted */
375 /* Now give the card a chance to receive.
376 * Gotta love 3c501s...
378 (void)inb(base+EL_AS);
379 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
386 /* This function actually attempts to transmit a datagram downloaded
387 * to the board. Call at splimp or interrupt, after downloading data!
388 * Returns 0 on success, non-0 on failure
391 el_xmit(struct el_softc *sc,int len)
396 gpl = EL_BUFSIZ - len;
397 dprintf(("el: xmit..."));
398 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
399 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
400 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
402 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
405 dprintf(("tx not ready\n"));
406 sc->arpcom.ac_if.if_oerrors++;
409 dprintf(("%d cycles.\n",(20000-i)));
413 /* Pass a packet up to the higher levels. */
415 elread(struct el_softc *sc,caddr_t buf,int len)
417 struct ether_header *eh;
420 eh = (struct ether_header *)buf;
423 * Put packet into an mbuf chain
425 m = elget(buf,len,&sc->arpcom.ac_if);
429 ether_input(&sc->arpcom.ac_if,eh,m);
432 /* controller interrupt */
438 int stat, rxstat, len, done;
440 /* Get things pointing properly */
441 sc = &el_softc[unit];
444 dprintf(("elintr: "));
446 /* Check board status */
447 stat = inb(base+EL_AS);
448 if(stat & EL_AS_RXBUSY) {
449 (void)inb(base+EL_RXC);
450 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
456 rxstat = inb(base+EL_RXS);
457 if(rxstat & EL_RXS_STALE) {
458 (void)inb(base+EL_RXC);
459 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
463 /* If there's an overflow, reinit the board. */
464 if(!(rxstat & EL_RXS_NOFLOW)) {
465 dprintf(("overflow.\n"));
467 /* Put board back into receive mode */
468 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
469 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
471 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
472 (void)inb(base+EL_AS);
474 (void)inb(base+EL_RXC);
475 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
479 /* Incoming packet */
480 len = inb(base+EL_RBL);
481 len |= inb(base+EL_RBH) << 8;
482 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
483 outb(base+EL_AC,EL_AC_HOST);
485 /* If packet too short or too long, restore rx mode and return
487 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
488 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
489 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
491 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
492 (void)inb(base+EL_AS);
494 (void)inb(base+EL_RXC);
495 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
499 sc->arpcom.ac_if.if_ipackets++;
501 /* Copy the data into our buffer */
502 outb(base+EL_GPBL,0);
503 outb(base+EL_GPBH,0);
504 insb(base+EL_BUF,sc->el_pktbuf,len);
506 outb(base+EL_AC,EL_AC_RX);
507 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
508 dprintf(("%6D\n",sc->el_pktbuf,":"));
510 /* Pass data up to upper levels */
511 len -= sizeof(struct ether_header);
512 elread(sc,(caddr_t)(sc->el_pktbuf),len);
514 /* Is there another packet? */
515 stat = inb(base+EL_AS);
517 /* If so, do it all again (i.e. don't set done to 1) */
518 if(!(stat & EL_AS_RXBUSY))
519 dprintf(("<rescan> "));
524 (void)inb(base+EL_RXC);
525 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
530 * Pull read data off a interface.
531 * Len is length of data, with local net header stripped.
534 elget(buf, totlen, ifp)
539 struct mbuf *top, **mp, *m;
544 buf += sizeof(struct ether_header);
548 MGETHDR(m, M_DONTWAIT, MT_DATA);
551 m->m_pkthdr.rcvif = ifp;
552 m->m_pkthdr.len = totlen;
558 MGET(m, M_DONTWAIT, MT_DATA);
565 len = min(totlen, epkt - cp);
566 if (len >= MINCLSIZE) {
567 MCLGET(m, M_DONTWAIT);
568 if (m->m_flags & M_EXT)
569 m->m_len = len = min(len, MCLBYTES);
574 * Place initial small packet/header at end of mbuf.
576 if (len < m->m_len) {
577 if (top == 0 && len + max_linkhdr <= m->m_len)
578 m->m_data += max_linkhdr;
583 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
595 * Process an ioctl request. This code needs some work - it looks
599 el_ioctl(ifp, command, data)
612 error = ether_ioctl(ifp, command, data);
617 * If interface is marked down and it is running, then stop it
619 if (((ifp->if_flags & IFF_UP) == 0) &&
620 (ifp->if_flags & IFF_RUNNING)) {
621 el_stop(ifp->if_softc);
622 ifp->if_flags &= ~IFF_RUNNING;
625 * If interface is marked up and it is stopped, then start it
627 if ((ifp->if_flags & IFF_UP) &&
628 ((ifp->if_flags & IFF_RUNNING) == 0))
629 el_init(ifp->if_softc);
639 /* Device timeout routine */
641 el_watchdog(struct ifnet *ifp)
643 log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
645 el_reset(ifp->if_softc);