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.24 2008/09/07 07:48:29 swildner 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/serialize.h>
36 #include <sys/thread2.h>
38 #include <net/ethernet.h>
40 #include <net/ifq_var.h>
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
47 #include <machine/clock.h>
49 #include <bus/isa/isa_device.h>
52 DECLARE_DUMMY_MODULE(if_el);
54 /* For debugging convenience */
56 #define dprintf(x) kprintf x
61 /* el_softc: per line info and status */
62 static struct el_softc {
63 struct arpcom arpcom; /* Ethernet common */
64 u_short el_base; /* Base I/O addr */
65 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
69 static int el_attach(struct isa_device *);
70 static void el_init(void *);
71 static int el_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
72 static int el_probe(struct isa_device *);
73 static void el_start(struct ifnet *);
74 static void el_reset(void *);
75 static void el_watchdog(struct ifnet *);
77 static void el_stop(void *);
78 static int el_xmit(struct el_softc *,int);
79 static void elintr(void *);
80 static __inline void elread(struct el_softc *,caddr_t,int);
81 static struct mbuf *elget(caddr_t,int,struct ifnet *);
82 static __inline void el_hardreset(void *);
84 /* isa_driver structure for autoconf */
85 struct isa_driver eldriver = {
86 el_probe, el_attach, "el"
89 static struct lwkt_serialize el_serializer;
91 /* Probe routine. See if the card is there and at the right place. */
93 el_probe(struct isa_device *idev)
96 u_short base; /* Just for convenience */
97 u_char station_addr[ETHER_ADDR_LEN];
100 lwkt_serialize_init(&el_serializer);
102 /* Grab some info for our structure */
103 sc = &el_softc[idev->id_unit];
104 sc->el_base = idev->id_iobase;
107 /* First check the base */
108 if((base < 0x280) || (base > 0x3f0)) {
109 kprintf("el%d: ioaddr must be between 0x280 and 0x3f0\n",
114 /* Now attempt to grab the station address from the PROM
115 * and see if it contains the 3com vendor code.
117 dprintf(("Probing 3c501 at 0x%x...\n",base));
119 /* Reset the board */
120 dprintf(("Resetting board...\n"));
121 outb(base+EL_AC,EL_AC_RESET);
124 dprintf(("Reading station address...\n"));
125 /* Now read the address */
126 for(i=0;i<ETHER_ADDR_LEN;i++) {
127 outb(base+EL_GPBL,i);
128 station_addr[i] = inb(base+EL_EAW);
130 dprintf(("Address is %6D\n",station_addr, ":"));
132 /* If the vendor code is ok, return a 1. We'll assume that
133 * whoever configured this system is right about the IRQ.
135 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
136 || (station_addr[2] != 0x8c)) {
137 dprintf(("Bad vendor code.\n"));
140 dprintf(("Vendor code ok.\n"));
141 /* Copy the station address into the arpcom structure */
142 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
147 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
149 el_hardreset(void *xsc)
151 struct el_softc *sc = xsc;
157 /* First reset the board */
158 outb(base+EL_AC,EL_AC_RESET);
162 /* Then give it back its ethernet address. Thanks to the mach
163 * source code for this undocumented goodie...
165 for(j=0;j<ETHER_ADDR_LEN;j++)
166 outb(base+j,sc->arpcom.ac_enaddr[j]);
169 /* Attach the interface to the kernel data structures. By the time
170 * this is called, we know that the card exists at the given I/O address.
171 * We still assume that the IRQ given is correct.
174 el_attach(struct isa_device *idev)
179 dprintf(("Attaching el%d...\n",idev->id_unit));
181 /* Get things pointing to the right places. */
182 idev->id_intr = (inthand2_t *)elintr;
183 sc = &el_softc[idev->id_unit];
184 ifp = &sc->arpcom.ac_if;
186 /* Now reset the board */
187 dprintf(("Resetting board...\n"));
190 /* Initialize ifnet structure */
192 if_initname(ifp, "el", idev->id_unit);
193 ifp->if_mtu = ETHERMTU;
194 ifp->if_start = el_start;
195 ifp->if_ioctl = el_ioctl;
196 ifp->if_watchdog = el_watchdog;
197 ifp->if_init = el_init;
198 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
199 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
200 ifq_set_ready(&ifp->if_snd);
202 /* Now we can attach the interface */
203 dprintf(("Attaching interface...\n"));
204 ether_ifattach(ifp, sc->arpcom.ac_enaddr, &el_serializer);
206 dprintf(("el_attach() finished.\n"));
210 /* This routine resets the interface. */
214 struct el_softc *sc = xsc;
216 dprintf(("elreset()\n"));
224 struct el_softc *sc = xsc;
226 outb(sc->el_base+EL_AC,0);
229 /* Initialize interface. */
233 struct el_softc *sc = xsc;
234 struct ifnet *ifp = &sc->arpcom.ac_if;
235 u_short base = sc->el_base;
238 /* First, reset the board. */
239 dprintf(("Resetting board...\n"));
243 dprintf(("Configuring rx...\n"));
244 if(ifp->if_flags & IFF_PROMISC)
245 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
247 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
251 dprintf(("Configuring tx...\n"));
254 /* Start reception */
255 dprintf(("Starting reception...\n"));
256 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
258 /* Set flags appropriately */
259 ifp->if_flags |= IFF_RUNNING;
260 ifp->if_flags &= ~IFF_OACTIVE;
262 /* And start output. */
266 /* Start output on interface. Get datagrams from the queue and output
267 * them, giving the receiver a chance between datagrams. Call only
268 * from splimp or interrupt level!
271 el_start(struct ifnet *ifp)
276 int i, len, retries, done;
278 /* Get things pointing in the right directions */
282 dprintf(("el_start()...\n"));
284 /* Don't do anything if output is active */
285 if (ifp->if_flags & IFF_OACTIVE)
287 ifp->if_flags |= IFF_OACTIVE;
289 /* The main loop. They warned me against endless loops, but
290 * would I listen? NOOO....
293 /* Dequeue the next datagram */
294 m0 = ifq_dequeue(&ifp->if_snd, NULL);
296 /* If there's nothing to send, return. */
298 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
302 /* Disable the receiver */
303 outb(base+EL_AC,EL_AC_HOST);
306 /* Copy the datagram to the buffer. */
308 for(m = m0; m != NULL; m = m->m_next) {
311 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
316 len = max(len,ETHER_MIN_LEN);
318 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
320 /* Transfer datagram to board */
321 dprintf(("el: xfr pkt length=%d...\n",len));
323 outb(base+EL_GPBL,(i & 0xff));
324 outb(base+EL_GPBH,((i>>8)&0xff));
325 outsb(base+EL_BUF,sc->el_pktbuf,len);
327 /* Now transmit the datagram */
331 if(el_xmit(sc,len)) { /* Something went wrong */
335 /* Check out status */
336 i = inb(base+EL_TXS);
337 dprintf(("tx status=0x%x\n",i));
338 if(!(i & EL_TXS_READY)) {
339 dprintf(("el: err txs=%x\n",i));
340 sc->arpcom.ac_if.if_oerrors++;
341 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
342 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
344 outb(base+EL_AC,EL_AC_HOST);
351 sc->arpcom.ac_if.if_opackets++;
355 if(done == -1) /* Packet not transmitted */
358 /* Now give the card a chance to receive.
359 * Gotta love 3c501s...
362 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
366 /* This function actually attempts to transmit a datagram downloaded
367 * to the board. Call at splimp or interrupt, after downloading data!
368 * Returns 0 on success, non-0 on failure
371 el_xmit(struct el_softc *sc,int len)
376 gpl = EL_BUFSIZ - len;
377 dprintf(("el: xmit..."));
378 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
379 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
380 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
382 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
385 dprintf(("tx not ready\n"));
386 sc->arpcom.ac_if.if_oerrors++;
389 dprintf(("%d cycles.\n",(20000-i)));
393 /* Pass a packet up to the higher levels. */
395 elread(struct el_softc *sc,caddr_t buf,int len)
400 * Put packet into an mbuf chain
402 m = elget(buf,len,&sc->arpcom.ac_if);
404 sc->arpcom.ac_if.if_input(&sc->arpcom.ac_if, m);
407 /* controller interrupt */
414 int stat, rxstat, len, done;
416 lwkt_serialize_enter(&el_serializer);
418 /* Get things pointing properly */
419 sc = &el_softc[unit];
422 dprintf(("elintr: "));
424 /* Check board status */
425 stat = inb(base+EL_AS);
426 if(stat & EL_AS_RXBUSY) {
428 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
429 lwkt_serialize_exit(&el_serializer);
435 rxstat = inb(base+EL_RXS);
436 if(rxstat & EL_RXS_STALE) {
438 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
439 lwkt_serialize_exit(&el_serializer);
443 /* If there's an overflow, reinit the board. */
444 if(!(rxstat & EL_RXS_NOFLOW)) {
445 dprintf(("overflow.\n"));
447 /* Put board back into receive mode */
448 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
449 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
451 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
455 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
456 lwkt_serialize_exit(&el_serializer);
460 /* Incoming packet */
461 len = inb(base+EL_RBL);
462 len |= inb(base+EL_RBH) << 8;
463 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
464 outb(base+EL_AC,EL_AC_HOST);
466 /* If packet too short or too long, restore rx mode and return
468 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
469 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
470 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
472 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
476 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
477 lwkt_serialize_exit(&el_serializer);
481 sc->arpcom.ac_if.if_ipackets++;
483 /* Copy the data into our buffer */
484 outb(base+EL_GPBL,0);
485 outb(base+EL_GPBH,0);
486 insb(base+EL_BUF,sc->el_pktbuf,len);
488 outb(base+EL_AC,EL_AC_RX);
489 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
490 dprintf(("%6D\n",sc->el_pktbuf,":"));
492 /* Pass data up to upper levels */
493 elread(sc,(caddr_t)(sc->el_pktbuf),len);
495 /* Is there another packet? */
496 stat = inb(base+EL_AS);
498 /* If so, do it all again (i.e. don't set done to 1) */
499 if(!(stat & EL_AS_RXBUSY))
500 dprintf(("<rescan> "));
506 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
507 lwkt_serialize_exit(&el_serializer);
511 * Pull read data off a interface.
512 * Len is length of data, with local net header stripped.
515 elget(caddr_t buf, int totlen, struct ifnet *ifp)
517 struct mbuf *top, **mp, *m;
525 MGETHDR(m, MB_DONTWAIT, MT_DATA);
528 m->m_pkthdr.len = totlen;
534 MGET(m, MB_DONTWAIT, MT_DATA);
541 len = min(totlen, epkt - cp);
542 if (len >= MINCLSIZE) {
543 MCLGET(m, MB_DONTWAIT);
544 if (m->m_flags & M_EXT)
545 m->m_len = len = min(len, MCLBYTES);
550 * Place initial small packet/header at end of mbuf.
552 if (len < m->m_len) {
553 if (top == 0 && len + max_linkhdr <= m->m_len)
554 m->m_data += max_linkhdr;
559 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
571 * Process an ioctl request. This code needs some work - it looks
575 el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
582 * If interface is marked down and it is running, then stop it
584 if (((ifp->if_flags & IFF_UP) == 0) &&
585 (ifp->if_flags & IFF_RUNNING)) {
586 el_stop(ifp->if_softc);
587 ifp->if_flags &= ~IFF_RUNNING;
590 * If interface is marked up and it is stopped, then start it
592 if ((ifp->if_flags & IFF_UP) &&
593 ((ifp->if_flags & IFF_RUNNING) == 0))
594 el_init(ifp->if_softc);
598 error = ether_ioctl(ifp, command, data);
604 /* Device timeout routine */
606 el_watchdog(struct ifnet *ifp)
608 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
610 el_reset(ifp->if_softc);