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.23 2008/08/02 01:14:42 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>
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)
180 dprintf(("Attaching el%d...\n",idev->id_unit));
182 /* Get things pointing to the right places. */
183 idev->id_intr = (inthand2_t *)elintr;
184 sc = &el_softc[idev->id_unit];
185 ifp = &sc->arpcom.ac_if;
188 /* Now reset the board */
189 dprintf(("Resetting board...\n"));
192 /* Initialize ifnet structure */
194 if_initname(ifp, "el", idev->id_unit);
195 ifp->if_mtu = ETHERMTU;
196 ifp->if_start = el_start;
197 ifp->if_ioctl = el_ioctl;
198 ifp->if_watchdog = el_watchdog;
199 ifp->if_init = el_init;
200 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX);
201 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
202 ifq_set_ready(&ifp->if_snd);
204 /* Now we can attach the interface */
205 dprintf(("Attaching interface...\n"));
206 ether_ifattach(ifp, sc->arpcom.ac_enaddr, &el_serializer);
208 dprintf(("el_attach() finished.\n"));
212 /* This routine resets the interface. */
216 struct el_softc *sc = xsc;
218 dprintf(("elreset()\n"));
226 struct el_softc *sc = xsc;
228 outb(sc->el_base+EL_AC,0);
231 /* Initialize interface. */
235 struct el_softc *sc = xsc;
236 struct ifnet *ifp = &sc->arpcom.ac_if;
237 u_short base = sc->el_base;
240 /* First, reset the board. */
241 dprintf(("Resetting board...\n"));
245 dprintf(("Configuring rx...\n"));
246 if(ifp->if_flags & IFF_PROMISC)
247 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
249 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
253 dprintf(("Configuring tx...\n"));
256 /* Start reception */
257 dprintf(("Starting reception...\n"));
258 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
260 /* Set flags appropriately */
261 ifp->if_flags |= IFF_RUNNING;
262 ifp->if_flags &= ~IFF_OACTIVE;
264 /* And start output. */
268 /* Start output on interface. Get datagrams from the queue and output
269 * them, giving the receiver a chance between datagrams. Call only
270 * from splimp or interrupt level!
273 el_start(struct ifnet *ifp)
278 int i, len, retries, done;
280 /* Get things pointing in the right directions */
284 dprintf(("el_start()...\n"));
286 /* Don't do anything if output is active */
287 if (ifp->if_flags & IFF_OACTIVE)
289 ifp->if_flags |= IFF_OACTIVE;
291 /* The main loop. They warned me against endless loops, but
292 * would I listen? NOOO....
295 /* Dequeue the next datagram */
296 m0 = ifq_dequeue(&ifp->if_snd, NULL);
298 /* If there's nothing to send, return. */
300 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
304 /* Disable the receiver */
305 outb(base+EL_AC,EL_AC_HOST);
308 /* Copy the datagram to the buffer. */
310 for(m = m0; m != NULL; m = m->m_next) {
313 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
318 len = max(len,ETHER_MIN_LEN);
320 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
322 /* Transfer datagram to board */
323 dprintf(("el: xfr pkt length=%d...\n",len));
325 outb(base+EL_GPBL,(i & 0xff));
326 outb(base+EL_GPBH,((i>>8)&0xff));
327 outsb(base+EL_BUF,sc->el_pktbuf,len);
329 /* Now transmit the datagram */
333 if(el_xmit(sc,len)) { /* Something went wrong */
337 /* Check out status */
338 i = inb(base+EL_TXS);
339 dprintf(("tx status=0x%x\n",i));
340 if(!(i & EL_TXS_READY)) {
341 dprintf(("el: err txs=%x\n",i));
342 sc->arpcom.ac_if.if_oerrors++;
343 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
344 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
346 outb(base+EL_AC,EL_AC_HOST);
353 sc->arpcom.ac_if.if_opackets++;
357 if(done == -1) /* Packet not transmitted */
360 /* Now give the card a chance to receive.
361 * Gotta love 3c501s...
364 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
368 /* This function actually attempts to transmit a datagram downloaded
369 * to the board. Call at splimp or interrupt, after downloading data!
370 * Returns 0 on success, non-0 on failure
373 el_xmit(struct el_softc *sc,int len)
378 gpl = EL_BUFSIZ - len;
379 dprintf(("el: xmit..."));
380 outb((sc->el_base)+EL_GPBL,(gpl & 0xff));
381 outb((sc->el_base)+EL_GPBH,((gpl>>8)&0xff));
382 outb((sc->el_base)+EL_AC,EL_AC_TXFRX);
384 while((inb((sc->el_base)+EL_AS) & EL_AS_TXBUSY) && (i>0))
387 dprintf(("tx not ready\n"));
388 sc->arpcom.ac_if.if_oerrors++;
391 dprintf(("%d cycles.\n",(20000-i)));
395 /* Pass a packet up to the higher levels. */
397 elread(struct el_softc *sc,caddr_t buf,int len)
402 * Put packet into an mbuf chain
404 m = elget(buf,len,&sc->arpcom.ac_if);
406 sc->arpcom.ac_if.if_input(&sc->arpcom.ac_if, m);
409 /* controller interrupt */
416 int stat, rxstat, len, done;
418 lwkt_serialize_enter(&el_serializer);
420 /* Get things pointing properly */
421 sc = &el_softc[unit];
424 dprintf(("elintr: "));
426 /* Check board status */
427 stat = inb(base+EL_AS);
428 if(stat & EL_AS_RXBUSY) {
430 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
431 lwkt_serialize_exit(&el_serializer);
437 rxstat = inb(base+EL_RXS);
438 if(rxstat & EL_RXS_STALE) {
440 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
441 lwkt_serialize_exit(&el_serializer);
445 /* If there's an overflow, reinit the board. */
446 if(!(rxstat & EL_RXS_NOFLOW)) {
447 dprintf(("overflow.\n"));
449 /* Put board back into receive mode */
450 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
451 outb(base+EL_RXC,(EL_RXC_PROMISC|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
453 outb(base+EL_RXC,(EL_RXC_ABROAD|EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
457 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
458 lwkt_serialize_exit(&el_serializer);
462 /* Incoming packet */
463 len = inb(base+EL_RBL);
464 len |= inb(base+EL_RBH) << 8;
465 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
466 outb(base+EL_AC,EL_AC_HOST);
468 /* If packet too short or too long, restore rx mode and return
470 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
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));
478 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
479 lwkt_serialize_exit(&el_serializer);
483 sc->arpcom.ac_if.if_ipackets++;
485 /* Copy the data into our buffer */
486 outb(base+EL_GPBL,0);
487 outb(base+EL_GPBH,0);
488 insb(base+EL_BUF,sc->el_pktbuf,len);
490 outb(base+EL_AC,EL_AC_RX);
491 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
492 dprintf(("%6D\n",sc->el_pktbuf,":"));
494 /* Pass data up to upper levels */
495 elread(sc,(caddr_t)(sc->el_pktbuf),len);
497 /* Is there another packet? */
498 stat = inb(base+EL_AS);
500 /* If so, do it all again (i.e. don't set done to 1) */
501 if(!(stat & EL_AS_RXBUSY))
502 dprintf(("<rescan> "));
508 outb(base+EL_AC,(EL_AC_IRQE|EL_AC_RX));
509 lwkt_serialize_exit(&el_serializer);
513 * Pull read data off a interface.
514 * Len is length of data, with local net header stripped.
517 elget(caddr_t buf, int totlen, struct ifnet *ifp)
519 struct mbuf *top, **mp, *m;
527 MGETHDR(m, MB_DONTWAIT, MT_DATA);
530 m->m_pkthdr.len = totlen;
536 MGET(m, MB_DONTWAIT, MT_DATA);
543 len = min(totlen, epkt - cp);
544 if (len >= MINCLSIZE) {
545 MCLGET(m, MB_DONTWAIT);
546 if (m->m_flags & M_EXT)
547 m->m_len = len = min(len, MCLBYTES);
552 * Place initial small packet/header at end of mbuf.
554 if (len < m->m_len) {
555 if (top == 0 && len + max_linkhdr <= m->m_len)
556 m->m_data += max_linkhdr;
561 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
573 * Process an ioctl request. This code needs some work - it looks
577 el_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
584 * If interface is marked down and it is running, then stop it
586 if (((ifp->if_flags & IFF_UP) == 0) &&
587 (ifp->if_flags & IFF_RUNNING)) {
588 el_stop(ifp->if_softc);
589 ifp->if_flags &= ~IFF_RUNNING;
592 * If interface is marked up and it is stopped, then start it
594 if ((ifp->if_flags & IFF_UP) &&
595 ((ifp->if_flags & IFF_RUNNING) == 0))
596 el_init(ifp->if_softc);
600 error = ether_ioctl(ifp, command, data);
606 /* Device timeout routine */
608 el_watchdog(struct ifnet *ifp)
610 log(LOG_ERR,"%s: device timeout\n", ifp->if_xname);
612 el_reset(ifp->if_softc);