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.
11 /* Except of course for the portions of code lifted from other FreeBSD
12 * drivers (mainly elread, elget and el_ioctl)
14 /* 3COM Etherlink 3C501 device driver for FreeBSD */
15 /* Yeah, I know these cards suck, but you can also get them for free
18 /* Bugs/possible improvements:
19 * - Does not currently support DMA
20 * - Does not currently support multicasts
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/kernel.h>
28 #include <sys/sockio.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
34 #include <net/ethernet.h>
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
42 #include <machine/bus_pio.h>
43 #include <machine/bus.h>
44 #include <machine/resource.h>
48 #include <isa/isavar.h>
50 #include <i386/isa/if_elreg.h>
52 /* For debugging convenience */
54 #define dprintf(x) printf x
59 /* el_softc: per line info and status */
61 struct arpcom arpcom; /* Ethernet common */
62 bus_space_handle_t el_bhandle;
63 bus_space_tag_t el_btag;
65 struct resource *el_irq;
66 struct resource *el_res;
68 char el_pktbuf[EL_BUFSIZ]; /* Frame buffer */
72 static int el_attach(device_t);
73 static int el_detach(device_t);
74 static void el_init(void *);
75 static int el_ioctl(struct ifnet *,u_long,caddr_t);
76 static int el_probe(device_t);
77 static void el_start(struct ifnet *);
78 static void el_reset(void *);
79 static void el_watchdog(struct ifnet *);
80 static int el_shutdown(device_t);
82 static void el_stop(void *);
83 static int el_xmit(struct el_softc *,int);
84 static void elintr(void *);
85 static __inline void elread(struct el_softc *,caddr_t,int);
86 static struct mbuf *elget(caddr_t,int,struct ifnet *);
87 static __inline void el_hardreset(void *);
89 static device_method_t el_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, el_probe),
92 DEVMETHOD(device_attach, el_attach),
93 DEVMETHOD(device_detach, el_detach),
94 DEVMETHOD(device_shutdown, el_shutdown),
98 static driver_t el_driver = {
101 sizeof(struct el_softc)
104 static devclass_t el_devclass;
106 DRIVER_MODULE(if_el, isa, el_driver, el_devclass, 0, 0);
108 #define CSR_WRITE_1(sc, reg, val) \
109 bus_space_write_1(sc->el_btag, sc->el_bhandle, reg, val)
110 #define CSR_READ_1(sc, reg) \
111 bus_space_read_1(sc->el_btag, sc->el_bhandle, reg)
113 #define EL_LOCK(_sc) mtx_lock(&(_sc)->el_mtx)
114 #define EL_UNLOCK(_sc) mtx_unlock(&(_sc)->el_mtx)
116 /* Probe routine. See if the card is there and at the right place. */
118 el_probe(device_t dev)
121 u_short base; /* Just for convenience */
122 u_char station_addr[ETHER_ADDR_LEN];
125 /* Grab some info for our structure */
126 sc = device_get_softc(dev);
128 if (isa_get_logicalid(dev)) /* skip PnP probes */
131 if ((base = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0)
134 /* First check the base */
135 if((base < 0x280) || (base > 0x3f0)) {
137 "ioaddr must be between 0x280 and 0x3f0\n");
141 /* Temporarily map the resources. */
143 sc->el_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
144 0, ~0, EL_IOSIZ, RF_ACTIVE);
146 if (sc->el_res == NULL)
149 sc->el_btag = rman_get_bustag(sc->el_res);
150 sc->el_bhandle = rman_get_bushandle(sc->el_res);
151 mtx_init(&sc->el_mtx,
152 device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
155 /* Now attempt to grab the station address from the PROM
156 * and see if it contains the 3com vendor code.
158 dprintf(("Probing 3c501 at 0x%x...\n",base));
160 /* Reset the board */
161 dprintf(("Resetting board...\n"));
162 CSR_WRITE_1(sc,EL_AC,EL_AC_RESET);
164 CSR_WRITE_1(sc,EL_AC,0);
165 dprintf(("Reading station address...\n"));
166 /* Now read the address */
167 for(i=0;i<ETHER_ADDR_LEN;i++) {
168 CSR_WRITE_1(sc,EL_GPBL,i);
169 station_addr[i] = CSR_READ_1(sc,EL_EAW);
172 /* Now release resources */
173 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->el_res);
175 mtx_destroy(&sc->el_mtx);
177 dprintf(("Address is %6D\n",station_addr, ":"));
179 /* If the vendor code is ok, return a 1. We'll assume that
180 * whoever configured this system is right about the IRQ.
182 if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
183 || (station_addr[2] != 0x8c)) {
184 dprintf(("Bad vendor code.\n"));
187 dprintf(("Vendor code ok.\n"));
188 /* Copy the station address into the arpcom structure */
189 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
192 device_set_desc(dev, "3Com 3c501 Ethernet");
197 /* Do a hardware reset of the 3c501. Do not call until after el_probe()! */
202 register struct el_softc *sc = xsc;
205 /* First reset the board */
206 CSR_WRITE_1(sc,EL_AC,EL_AC_RESET);
208 CSR_WRITE_1(sc,EL_AC,0);
210 /* Then give it back its ethernet address. Thanks to the mach
211 * source code for this undocumented goodie...
213 for(j=0;j<ETHER_ADDR_LEN;j++)
214 CSR_WRITE_1(sc,j,sc->arpcom.ac_enaddr[j]);
217 /* Attach the interface to the kernel data structures. By the time
218 * this is called, we know that the card exists at the given I/O address.
219 * We still assume that the IRQ given is correct.
222 el_attach(device_t dev)
228 dprintf(("Attaching el%d...\n",device_get_unit(dev)));
230 /* Get things pointing to the right places. */
231 sc = device_get_softc(dev);
232 ifp = &sc->arpcom.ac_if;
235 sc->el_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
236 0, ~0, EL_IOSIZ, RF_ACTIVE);
238 if (sc->el_res == NULL)
242 sc->el_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
243 RF_SHAREABLE | RF_ACTIVE);
245 if (sc->el_irq == NULL) {
246 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
250 error = bus_setup_intr(dev, sc->el_irq, INTR_TYPE_NET,
251 elintr, sc, &sc->el_intrhand);
254 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->el_irq);
255 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
259 /* Now reset the board */
260 dprintf(("Resetting board...\n"));
263 /* Initialize ifnet structure */
265 ifp->if_unit = device_get_unit(dev);;
267 ifp->if_mtu = ETHERMTU;
268 ifp->if_output = ether_output;
269 ifp->if_start = el_start;
270 ifp->if_ioctl = el_ioctl;
271 ifp->if_watchdog = el_watchdog;
272 ifp->if_init = el_init;
273 ifp->if_flags = (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX);
274 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
276 /* Now we can attach the interface */
277 dprintf(("Attaching interface...\n"));
278 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
280 /* Print out some information for the user */
281 device_printf(dev, "3c501 address %6D\n",
282 sc->arpcom.ac_enaddr, ":");
284 dprintf(("el_attach() finished.\n"));
288 static int el_detach(dev)
294 sc = device_get_softc(dev);
295 ifp = &sc->arpcom.ac_if;
299 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
300 bus_teardown_intr(dev, sc->el_irq, sc->el_intrhand);
301 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->el_irq);
302 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
304 mtx_destroy(&sc->el_mtx);
315 sc = device_get_softc(dev);
320 /* This routine resets the interface. */
325 struct el_softc *sc = xsc;
327 dprintf(("elreset()\n"));
332 static void el_stop(xsc)
335 struct el_softc *sc = xsc;
338 CSR_WRITE_1(sc,EL_AC,0);
343 /* Initialize interface. */
348 struct el_softc *sc = xsc;
351 /* Set up pointers */
352 ifp = &sc->arpcom.ac_if;
354 /* If address not known, do nothing. */
355 if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
360 /* First, reset the board. */
361 dprintf(("Resetting board...\n"));
365 dprintf(("Configuring rx...\n"));
366 if(ifp->if_flags & IFF_PROMISC)
367 CSR_WRITE_1(sc,EL_RXC,
368 (EL_RXC_PROMISC|EL_RXC_ABROAD|EL_RXC_AMULTI|
369 EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
371 CSR_WRITE_1(sc,EL_RXC,
372 (EL_RXC_ABROAD|EL_RXC_AMULTI|
373 EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
374 CSR_WRITE_1(sc,EL_RBC,0);
377 dprintf(("Configuring tx...\n"));
378 CSR_WRITE_1(sc,EL_TXC,0);
380 /* Start reception */
381 dprintf(("Starting reception...\n"));
382 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
384 /* Set flags appropriately */
385 ifp->if_flags |= IFF_RUNNING;
386 ifp->if_flags &= ~IFF_OACTIVE;
388 /* And start output. */
394 /* Start output on interface. Get datagrams from the queue and output
395 * them, giving the receiver a chance between datagrams. Call only
396 * from splimp or interrupt level!
399 el_start(struct ifnet *ifp)
403 int i, len, retries, done;
405 /* Get things pointing in the right directions */
408 dprintf(("el_start()...\n"));
411 /* Don't do anything if output is active */
412 if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
414 sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
416 /* The main loop. They warned me against endless loops, but
417 * would I listen? NOOO....
420 /* Dequeue the next datagram */
421 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
423 /* If there's nothing to send, return. */
425 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
430 /* Disable the receiver */
431 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
432 CSR_WRITE_1(sc,EL_RBC,0);
434 /* Copy the datagram to the buffer. */
436 for(m = m0; m != NULL; m = m->m_next) {
439 bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
444 len = max(len,ETHER_MIN_LEN);
446 /* Give the packet to the bpf, if any */
447 if(sc->arpcom.ac_if.if_bpf)
448 bpf_tap(&sc->arpcom.ac_if, sc->el_pktbuf, len);
450 /* Transfer datagram to board */
451 dprintf(("el: xfr pkt length=%d...\n",len));
453 CSR_WRITE_1(sc,EL_GPBL,(i & 0xff));
454 CSR_WRITE_1(sc,EL_GPBH,((i>>8)&0xff));
455 bus_space_write_multi_1(sc->el_btag, sc->el_bhandle,
456 EL_BUF, sc->el_pktbuf, len);
458 /* Now transmit the datagram */
462 if(el_xmit(sc,len)) { /* Something went wrong */
466 /* Check out status */
467 i = CSR_READ_1(sc,EL_TXS);
468 dprintf(("tx status=0x%x\n",i));
469 if(!(i & EL_TXS_READY)) {
470 dprintf(("el: err txs=%x\n",i));
471 sc->arpcom.ac_if.if_oerrors++;
472 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
473 if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
475 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
482 sc->arpcom.ac_if.if_opackets++;
486 if(done == -1) /* Packet not transmitted */
489 /* Now give the card a chance to receive.
490 * Gotta love 3c501s...
492 (void)CSR_READ_1(sc,EL_AS);
493 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
500 /* This function actually attempts to transmit a datagram downloaded
501 * to the board. Call at splimp or interrupt, after downloading data!
502 * Returns 0 on success, non-0 on failure
505 el_xmit(struct el_softc *sc,int len)
510 gpl = EL_BUFSIZ - len;
511 dprintf(("el: xmit..."));
512 CSR_WRITE_1(sc,EL_GPBL,(gpl & 0xff));
513 CSR_WRITE_1(sc,EL_GPBH,((gpl>>8)&0xff));
514 CSR_WRITE_1(sc,EL_AC,EL_AC_TXFRX);
516 while((CSR_READ_1(sc,EL_AS) & EL_AS_TXBUSY) && (i>0))
519 dprintf(("tx not ready\n"));
520 sc->arpcom.ac_if.if_oerrors++;
523 dprintf(("%d cycles.\n",(20000-i)));
527 /* Pass a packet up to the higher levels. */
529 elread(struct el_softc *sc,caddr_t buf,int len)
531 register struct ether_header *eh;
534 eh = (struct ether_header *)buf;
537 * Put packet into an mbuf chain
539 m = elget(buf,len,&sc->arpcom.ac_if);
543 ether_input(&sc->arpcom.ac_if,eh,m);
546 /* controller interrupt */
550 register struct el_softc *sc;
551 int stat, rxstat, len, done;
554 /* Get things pointing properly */
557 dprintf(("elintr: "));
559 /* Check board status */
560 stat = CSR_READ_1(sc,EL_AS);
561 if(stat & EL_AS_RXBUSY) {
562 (void)CSR_READ_1(sc,EL_RXC);
563 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
570 rxstat = CSR_READ_1(sc,EL_RXS);
571 if(rxstat & EL_RXS_STALE) {
572 (void)CSR_READ_1(sc,EL_RXC);
573 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
578 /* If there's an overflow, reinit the board. */
579 if(!(rxstat & EL_RXS_NOFLOW)) {
580 dprintf(("overflow.\n"));
582 /* Put board back into receive mode */
583 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
584 CSR_WRITE_1(sc,EL_RXC,
585 (EL_RXC_PROMISC|EL_RXC_ABROAD|
586 EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
587 EL_RXC_DDRIB|EL_RXC_DOFLOW));
589 CSR_WRITE_1(sc,EL_RXC,
591 EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
592 EL_RXC_DDRIB|EL_RXC_DOFLOW));
593 (void)CSR_READ_1(sc,EL_AS);
594 CSR_WRITE_1(sc,EL_RBC,0);
595 (void)CSR_READ_1(sc,EL_RXC);
596 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
601 /* Incoming packet */
602 len = CSR_READ_1(sc,EL_RBL);
603 len |= CSR_READ_1(sc,EL_RBH) << 8;
604 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
605 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
607 /* If packet too short or too long, restore rx mode and return
609 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
610 if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
611 CSR_WRITE_1(sc,EL_RXC,
612 (EL_RXC_PROMISC|EL_RXC_ABROAD|
613 EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
614 EL_RXC_DDRIB|EL_RXC_DOFLOW));
616 CSR_WRITE_1(sc,EL_RXC,
618 EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
619 EL_RXC_DDRIB|EL_RXC_DOFLOW));
620 (void)CSR_READ_1(sc,EL_AS);
621 CSR_WRITE_1(sc,EL_RBC,0);
622 (void)CSR_READ_1(sc,EL_RXC);
623 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
628 sc->arpcom.ac_if.if_ipackets++;
630 /* Copy the data into our buffer */
631 CSR_WRITE_1(sc,EL_GPBL,0);
632 CSR_WRITE_1(sc,EL_GPBH,0);
633 bus_space_read_multi_1(sc->el_btag, sc->el_bhandle,
634 EL_BUF, sc->el_pktbuf, len);
635 CSR_WRITE_1(sc,EL_RBC,0);
636 CSR_WRITE_1(sc,EL_AC,EL_AC_RX);
637 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
638 dprintf(("%6D\n",sc->el_pktbuf,":"));
640 /* Pass data up to upper levels */
641 len -= sizeof(struct ether_header);
642 elread(sc,(caddr_t)(sc->el_pktbuf),len);
644 /* Is there another packet? */
645 stat = CSR_READ_1(sc,EL_AS);
647 /* If so, do it all again (i.e. don't set done to 1) */
648 if(!(stat & EL_AS_RXBUSY))
649 dprintf(("<rescan> "));
654 (void)CSR_READ_1(sc,EL_RXC);
655 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
661 * Pull read data off a interface.
662 * Len is length of data, with local net header stripped.
665 elget(buf, totlen, ifp)
670 struct mbuf *top, **mp, *m;
675 buf += sizeof(struct ether_header);
679 MGETHDR(m, M_DONTWAIT, MT_DATA);
682 m->m_pkthdr.rcvif = ifp;
683 m->m_pkthdr.len = totlen;
689 MGET(m, M_DONTWAIT, MT_DATA);
696 len = min(totlen, epkt - cp);
697 if (len >= MINCLSIZE) {
698 MCLGET(m, M_DONTWAIT);
699 if (m->m_flags & M_EXT)
700 m->m_len = len = min(len, MCLBYTES);
705 * Place initial small packet/header at end of mbuf.
707 if (len < m->m_len) {
708 if (top == 0 && len + max_linkhdr <= m->m_len)
709 m->m_data += max_linkhdr;
714 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
726 * Process an ioctl request. This code needs some work - it looks
730 el_ioctl(ifp, command, data)
731 register struct ifnet *ifp;
745 error = ether_ioctl(ifp, command, data);
750 * If interface is marked down and it is running, then stop it
752 if (((ifp->if_flags & IFF_UP) == 0) &&
753 (ifp->if_flags & IFF_RUNNING)) {
754 el_stop(ifp->if_softc);
755 ifp->if_flags &= ~IFF_RUNNING;
758 * If interface is marked up and it is stopped, then start it
760 if ((ifp->if_flags & IFF_UP) &&
761 ((ifp->if_flags & IFF_RUNNING) == 0))
762 el_init(ifp->if_softc);
772 /* Device timeout routine */
774 el_watchdog(struct ifnet *ifp)
776 log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
778 el_reset(ifp->if_softc);