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.16 2005/06/14 14:35:05 joerg 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>
35 #include <sys/thread2.h>
37 #include <net/ethernet.h>
39 #include <net/ifq_var.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
46 #include <machine/clock.h>
48 #include <bus/isa/i386/isa_device.h>
51 DECLARE_DUMMY_MODULE(if_el);
53 /* For debugging convenience */
55 #define dprintf(x) printf x
60 /* el_softc: per line info and status */
61 static struct el_softc {
62 struct arpcom arpcom; /* Ethernet common */
63 u_short el_base; /* Base I/O addr */
64 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
68 static int el_attach(struct isa_device *);
69 static void el_init(void *);
70 static int el_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
71 static int el_probe(struct isa_device *);
72 static void el_start(struct ifnet *);
73 static void el_reset(void *);
74 static void el_watchdog(struct ifnet *);
76 static void el_stop(void *);
77 static int el_xmit(struct el_softc *,int);
78 static ointhand2_t elintr;
79 static __inline void elread(struct el_softc *,caddr_t,int);
80 static struct mbuf *elget(caddr_t,int,struct ifnet *);
81 static __inline void el_hardreset(void *);
83 /* isa_driver structure for autoconf */
84 struct isa_driver eldriver = {
85 el_probe, el_attach, "el"
88 /* Probe routine. See if the card is there and at the right place. */
90 el_probe(struct isa_device *idev)
93 u_short base; /* Just for convenience */
94 u_char station_addr[ETHER_ADDR_LEN];
97 /* Grab some info for our structure */
98 sc = &el_softc[idev->id_unit];
99 sc->el_base = idev->id_iobase;
102 /* First check the base */
103 if((base < 0x280) || (base > 0x3f0)) {
104 printf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
109 /* Now attempt to grab the station address from the PROM
110 * and see if it contains the 3com vendor code.
112 dprintf(("Probing 3c501 at 0x%x...\n",base));
114 /* Reset the board */
115 dprintf(("Resetting board...\n"));
116 outb(base+EL_AC,EL_AC_RESET);
119 dprintf(("Reading station address...\n"));
120 /* Now read the address */
121 for(i=0;i<ETHER_ADDR_LEN;i++) {
122 outb(base+EL_GPBL,i);
123 station_addr[i] = inb(base+EL_EAW);
125 dprintf(("Address is %6D\n",station_addr, ":"));
127 /* If the vendor code is ok, return a 1. We'll assume that
128 * whoever configured this system is right about the IRQ.
130 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
131 || (station_addr[2] != 0x8c)) {
132 dprintf(("Bad vendor code.\n"));
135 dprintf(("Vendor code ok.\n"));
136 /* Copy the station address into the arpcom structure */
137 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
142 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
147 struct el_softc *sc = xsc;
153 /* First reset the board */
154 outb(base+EL_AC,EL_AC_RESET);
158 /* Then give it back its ethernet address. Thanks to the mach
159 * source code for this undocumented goodie...
161 for(j=0;j<ETHER_ADDR_LEN;j++)
162 outb(base+j,sc->arpcom.ac_enaddr[j]);
165 /* Attach the interface to the kernel data structures. By the time
166 * this is called, we know that the card exists at the given I/O address.
167 * We still assume that the IRQ given is correct.
170 el_attach(struct isa_device *idev)
176 dprintf(("Attaching el%d...\n",idev->id_unit));
178 /* Get things pointing to the right places. */
179 idev->id_ointr = elintr;
180 sc = &el_softc[idev->id_unit];
181 ifp = &sc->arpcom.ac_if;
184 /* Now reset the board */
185 dprintf(("Resetting board...\n"));
188 /* Initialize ifnet structure */
190 if_initname(ifp, "el", idev->id_unit);
191 ifp->if_mtu = ETHERMTU;
192 ifp->if_start = el_start;
193 ifp->if_ioctl = el_ioctl;
194 ifp->if_watchdog = el_watchdog;
195 ifp->if_init = el_init;
196 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
197 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
198 ifq_set_ready(&ifp->if_snd);
200 /* Now we can attach the interface */
201 dprintf(("Attaching interface...\n"));
202 ether_ifattach(ifp, sc->arpcom.ac_enaddr);
204 dprintf(("el_attach() finished.\n"));
208 /* This routine resets the interface. */
213 struct el_softc *sc = xsc;
215 dprintf(("elreset()\n"));
222 static void el_stop(xsc)
225 struct el_softc *sc = xsc;
227 outb(sc->el_base+EL_AC,0);
230 /* Initialize interface. */
235 struct el_softc *sc = xsc;
236 struct ifnet *ifp = &sc->arpcom.ac_if;
237 u_short base = sc->el_base;
241 /* First, reset the board. */
242 dprintf(("Resetting board...\n"));
246 dprintf(("Configuring rx...\n"));
247 if(ifp->if_flags & IFF_PROMISC)
248 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
250 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
254 dprintf(("Configuring tx...\n"));
257 /* Start reception */
258 dprintf(("Starting reception...\n"));
259 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
261 /* Set flags appropriately */
262 ifp->if_flags |= IFF_RUNNING;
263 ifp->if_flags &= ~IFF_OACTIVE;
265 /* And start output. */
271 /* Start output on interface. Get datagrams from the queue and output
272 * them, giving the receiver a chance between datagrams. Call only
273 * from splimp or interrupt level!
276 el_start(struct ifnet *ifp)
281 int i, len, retries, done;
283 /* Get things pointing in the right directions */
287 dprintf(("el_start()...\n"));
290 /* Don't do anything if output is active */
291 if (ifp->if_flags & IFF_OACTIVE)
293 ifp->if_flags |= IFF_OACTIVE;
295 /* The main loop. They warned me against endless loops, but
296 * would I listen? NOOO....
299 /* Dequeue the next datagram */
300 m0 = ifq_dequeue(&ifp->if_snd);
302 /* If there's nothing to send, return. */
304 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
309 /* Disable the receiver */
310 outb(base+EL_AC,EL_AC_HOST);
313 /* Copy the datagram to the buffer. */
315 for(m = m0; m != NULL; m = m->m_next) {
318 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
323 len = max(len,ETHER_MIN_LEN);
325 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
327 /* Transfer datagram to board */
328 dprintf(("el: xfr pkt length=%d...\n",len));
330 outb(base+EL_GPBL,(i & 0xff));
331 outb(base+EL_GPBH,((i>>8)&0xff));
332 outsb(base+EL_BUF,sc->el_pktbuf,len);
334 /* Now transmit the datagram */
338 if(el_xmit(sc,len)) { /* Something went wrong */
342 /* Check out status */
343 i = inb(base+EL_TXS);
344 dprintf(("tx status=0x%x\n",i));
345 if(!(i & EL_TXS_READY)) {
346 dprintf(("el: err txs=%x\n",i));
347 sc->arpcom.ac_if.if_oerrors++;
348 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
349 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
351 outb(base+EL_AC,EL_AC_HOST);
358 sc->arpcom.ac_if.if_opackets++;
362 if(done == -1) /* Packet not transmitted */
365 /* Now give the card a chance to receive.
366 * Gotta love 3c501s...
368 (void)inb(base+EL_AS);
369 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
375 /* This function actually attempts to transmit a datagram downloaded
376 * to the board. Call at splimp or interrupt, after downloading data!
377 * Returns 0 on success, non-0 on failure
380 el_xmit(struct el_softc *sc,int len)
385 gpl = EL_BUFSIZ - len;
386 dprintf(("el: xmit..."));
387 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
388 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
389 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
391 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
394 dprintf(("tx not ready\n"));
395 sc->arpcom.ac_if.if_oerrors++;
398 dprintf(("%d cycles.\n",(20000-i)));
402 /* Pass a packet up to the higher levels. */
404 elread(struct el_softc *sc,caddr_t buf,int len)
406 struct ether_header *eh;
409 eh = (struct ether_header *)buf;
412 * Put packet into an mbuf chain
414 m = elget(buf,len,&sc->arpcom.ac_if);
418 ether_input(&sc->arpcom.ac_if,eh,m);
421 /* controller interrupt */
427 int stat, rxstat, len, done;
429 /* Get things pointing properly */
430 sc = &el_softc[unit];
433 dprintf(("elintr: "));
435 /* Check board status */
436 stat = inb(base+EL_AS);
437 if(stat & EL_AS_RXBUSY) {
438 (void)inb(base+EL_RXC);
439 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
445 rxstat = inb(base+EL_RXS);
446 if(rxstat & EL_RXS_STALE) {
447 (void)inb(base+EL_RXC);
448 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
452 /* If there's an overflow, reinit the board. */
453 if(!(rxstat & EL_RXS_NOFLOW)) {
454 dprintf(("overflow.\n"));
456 /* Put board back into receive mode */
457 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
458 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
460 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
461 (void)inb(base+EL_AS);
463 (void)inb(base+EL_RXC);
464 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
468 /* Incoming packet */
469 len = inb(base+EL_RBL);
470 len |= inb(base+EL_RBH) << 8;
471 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
472 outb(base+EL_AC,EL_AC_HOST);
474 /* If packet too short or too long, restore rx mode and return
476 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
477 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
478 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
480 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
481 (void)inb(base+EL_AS);
483 (void)inb(base+EL_RXC);
484 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
488 sc->arpcom.ac_if.if_ipackets++;
490 /* Copy the data into our buffer */
491 outb(base+EL_GPBL,0);
492 outb(base+EL_GPBH,0);
493 insb(base+EL_BUF,sc->el_pktbuf,len);
495 outb(base+EL_AC,EL_AC_RX);
496 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
497 dprintf(("%6D\n",sc->el_pktbuf,":"));
499 /* Pass data up to upper levels */
500 len -= sizeof(struct ether_header);
501 elread(sc,(caddr_t)(sc->el_pktbuf),len);
503 /* Is there another packet? */
504 stat = inb(base+EL_AS);
506 /* If so, do it all again (i.e. don't set done to 1) */
507 if(!(stat & EL_AS_RXBUSY))
508 dprintf(("<rescan> "));
513 (void)inb(base+EL_RXC);
514 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
519 * Pull read data off a interface.
520 * Len is length of data, with local net header stripped.
523 elget(buf, totlen, ifp)
528 struct mbuf *top, **mp, *m;
533 buf += sizeof(struct ether_header);
537 MGETHDR(m, MB_DONTWAIT, MT_DATA);
540 m->m_pkthdr.rcvif = ifp;
541 m->m_pkthdr.len = totlen;
547 MGET(m, MB_DONTWAIT, MT_DATA);
554 len = min(totlen, epkt - cp);
555 if (len >= MINCLSIZE) {
556 MCLGET(m, MB_DONTWAIT);
557 if (m->m_flags & M_EXT)
558 m->m_len = len = min(len, MCLBYTES);
563 * Place initial small packet/header at end of mbuf.
565 if (len < m->m_len) {
566 if (top == 0 && len + max_linkhdr <= m->m_len)
567 m->m_data += max_linkhdr;
572 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
584 * Process an ioctl request. This code needs some work - it looks
588 el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
597 * If interface is marked down and it is running, then stop it
599 if (((ifp->if_flags & IFF_UP) == 0) &&
600 (ifp->if_flags & IFF_RUNNING)) {
601 el_stop(ifp->if_softc);
602 ifp->if_flags &= ~IFF_RUNNING;
605 * If interface is marked up and it is stopped, then start it
607 if ((ifp->if_flags & IFF_UP) &&
608 ((ifp->if_flags & IFF_RUNNING) == 0))
609 el_init(ifp->if_softc);
613 error = ether_ioctl(ifp, command, data);
622 /* Device timeout routine */
624 el_watchdog(struct ifnet *ifp)
626 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
628 el_reset(ifp->if_softc);