2 * Copyright (c) 1997, 1998
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Winbond fast ethernet PCI NIC driver
39 * Supports various cheap network adapters based on the Winbond W89C840F
40 * fast ethernet controller chip. This includes adapters manufactured by
41 * Winbond itself and some made by Linksys.
43 * Written by Bill Paul <wpaul@ctr.columbia.edu>
44 * Electrical Engineering Department
45 * Columbia University, New York City
48 * The Winbond W89C840F chip is a bus master; in some ways it resembles
49 * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has
50 * one major difference which is that while the registers do many of
51 * the same things as a tulip adapter, the offsets are different: where
52 * tulip registers are typically spaced 8 bytes apart, the Winbond
53 * registers are spaced 4 bytes apart. The receiver filter is also
54 * programmed differently.
56 * Like the tulip, the Winbond chip uses small descriptors containing
57 * a status word, a control word and 32-bit areas that can either be used
58 * to point to two external data blocks, or to point to a single block
59 * and another descriptor in a linked list. Descriptors can be grouped
60 * together in blocks to form fixed length rings or can be chained
61 * together in linked lists. A single packet may be spread out over
62 * several descriptors if necessary.
64 * For the receive ring, this driver uses a linked list of descriptors,
65 * each pointing to a single mbuf cluster buffer, which us large enough
66 * to hold an entire packet. The link list is looped back to created a
69 * For transmission, the driver creates a linked list of 'super descriptors'
70 * which each contain several individual descriptors linked toghether.
71 * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we
72 * abuse as fragment pointers. This allows us to use a buffer managment
73 * scheme very similar to that used in the ThunderLAN and Etherlink XL
76 * Autonegotiation is performed using the external PHY via the MII bus.
77 * The sample boards I have all use a Davicom PHY.
79 * Note: the author of the Linux driver for the Winbond chip alludes
80 * to some sort of flaw in the chip's design that seems to mandate some
81 * drastic workaround which signigicantly impairs transmit performance.
82 * I have no idea what he's on about: transmit performance with all
83 * three of my test boards seems fine.
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
90 #include <sys/malloc.h>
91 #include <sys/module.h>
92 #include <sys/kernel.h>
93 #include <sys/socket.h>
94 #include <sys/queue.h>
97 #include <net/if_var.h>
98 #include <net/if_arp.h>
99 #include <net/ethernet.h>
100 #include <net/if_dl.h>
101 #include <net/if_media.h>
102 #include <net/if_types.h>
106 #include <vm/vm.h> /* for vtophys */
107 #include <vm/pmap.h> /* for vtophys */
108 #include <machine/bus.h>
109 #include <machine/resource.h>
111 #include <sys/rman.h>
113 #include <dev/pci/pcireg.h>
114 #include <dev/pci/pcivar.h>
116 #include <dev/mii/mii.h>
117 #include <dev/mii/mii_bitbang.h>
118 #include <dev/mii/miivar.h>
120 /* "device miibus" required. See GENERIC if you get errors here. */
121 #include "miibus_if.h"
123 #define WB_USEIOSPACE
125 #include <dev/wb/if_wbreg.h>
127 MODULE_DEPEND(wb, pci, 1, 1, 1);
128 MODULE_DEPEND(wb, ether, 1, 1, 1);
129 MODULE_DEPEND(wb, miibus, 1, 1, 1);
132 * Various supported device vendors/types and their names.
134 static const struct wb_type wb_devs[] = {
135 { WB_VENDORID, WB_DEVICEID_840F,
136 "Winbond W89C840F 10/100BaseTX" },
137 { CP_VENDORID, CP_DEVICEID_RL100,
138 "Compex RL100-ATX 10/100baseTX" },
142 static int wb_probe(device_t);
143 static int wb_attach(device_t);
144 static int wb_detach(device_t);
146 static int wb_bfree(struct mbuf *, void *addr, void *args);
147 static int wb_newbuf(struct wb_softc *, struct wb_chain_onefrag *,
149 static int wb_encap(struct wb_softc *, struct wb_chain *, struct mbuf *);
151 static void wb_rxeof(struct wb_softc *);
152 static void wb_rxeoc(struct wb_softc *);
153 static void wb_txeof(struct wb_softc *);
154 static void wb_txeoc(struct wb_softc *);
155 static void wb_intr(void *);
156 static void wb_tick(void *);
157 static void wb_start(struct ifnet *);
158 static void wb_start_locked(struct ifnet *);
159 static int wb_ioctl(struct ifnet *, u_long, caddr_t);
160 static void wb_init(void *);
161 static void wb_init_locked(struct wb_softc *);
162 static void wb_stop(struct wb_softc *);
163 static void wb_watchdog(struct wb_softc *);
164 static int wb_shutdown(device_t);
165 static int wb_ifmedia_upd(struct ifnet *);
166 static void wb_ifmedia_sts(struct ifnet *, struct ifmediareq *);
168 static void wb_eeprom_putbyte(struct wb_softc *, int);
169 static void wb_eeprom_getword(struct wb_softc *, int, u_int16_t *);
170 static void wb_read_eeprom(struct wb_softc *, caddr_t, int, int, int);
172 static void wb_setcfg(struct wb_softc *, u_int32_t);
173 static void wb_setmulti(struct wb_softc *);
174 static void wb_reset(struct wb_softc *);
175 static void wb_fixmedia(struct wb_softc *);
176 static int wb_list_rx_init(struct wb_softc *);
177 static int wb_list_tx_init(struct wb_softc *);
179 static int wb_miibus_readreg(device_t, int, int);
180 static int wb_miibus_writereg(device_t, int, int, int);
181 static void wb_miibus_statchg(device_t);
186 static uint32_t wb_mii_bitbang_read(device_t);
187 static void wb_mii_bitbang_write(device_t, uint32_t);
189 static const struct mii_bitbang_ops wb_mii_bitbang_ops = {
191 wb_mii_bitbang_write,
193 WB_SIO_MII_DATAOUT, /* MII_BIT_MDO */
194 WB_SIO_MII_DATAIN, /* MII_BIT_MDI */
195 WB_SIO_MII_CLK, /* MII_BIT_MDC */
196 WB_SIO_MII_DIR, /* MII_BIT_DIR_HOST_PHY */
197 0, /* MII_BIT_DIR_PHY_HOST */
202 #define WB_RES SYS_RES_IOPORT
203 #define WB_RID WB_PCI_LOIO
205 #define WB_RES SYS_RES_MEMORY
206 #define WB_RID WB_PCI_LOMEM
209 static device_method_t wb_methods[] = {
210 /* Device interface */
211 DEVMETHOD(device_probe, wb_probe),
212 DEVMETHOD(device_attach, wb_attach),
213 DEVMETHOD(device_detach, wb_detach),
214 DEVMETHOD(device_shutdown, wb_shutdown),
217 DEVMETHOD(miibus_readreg, wb_miibus_readreg),
218 DEVMETHOD(miibus_writereg, wb_miibus_writereg),
219 DEVMETHOD(miibus_statchg, wb_miibus_statchg),
224 static driver_t wb_driver = {
227 sizeof(struct wb_softc)
230 static devclass_t wb_devclass;
232 DRIVER_MODULE(wb, pci, wb_driver, wb_devclass, 0, 0);
233 DRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0);
235 #define WB_SETBIT(sc, reg, x) \
236 CSR_WRITE_4(sc, reg, \
237 CSR_READ_4(sc, reg) | (x))
239 #define WB_CLRBIT(sc, reg, x) \
240 CSR_WRITE_4(sc, reg, \
241 CSR_READ_4(sc, reg) & ~(x))
244 CSR_WRITE_4(sc, WB_SIO, \
245 CSR_READ_4(sc, WB_SIO) | (x))
248 CSR_WRITE_4(sc, WB_SIO, \
249 CSR_READ_4(sc, WB_SIO) & ~(x))
252 * Send a read command and address to the EEPROM, check for ACK.
255 wb_eeprom_putbyte(sc, addr)
261 d = addr | WB_EECMD_READ;
264 * Feed in each bit and stobe the clock.
266 for (i = 0x400; i; i >>= 1) {
268 SIO_SET(WB_SIO_EE_DATAIN);
270 SIO_CLR(WB_SIO_EE_DATAIN);
273 SIO_SET(WB_SIO_EE_CLK);
275 SIO_CLR(WB_SIO_EE_CLK);
281 * Read a word of data stored in the EEPROM at address 'addr.'
284 wb_eeprom_getword(sc, addr, dest)
292 /* Enter EEPROM access mode. */
293 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
296 * Send address of word we want to read.
298 wb_eeprom_putbyte(sc, addr);
300 CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
303 * Start reading bits from EEPROM.
305 for (i = 0x8000; i; i >>= 1) {
306 SIO_SET(WB_SIO_EE_CLK);
308 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT)
310 SIO_CLR(WB_SIO_EE_CLK);
314 /* Turn off EEPROM access mode. */
315 CSR_WRITE_4(sc, WB_SIO, 0);
321 * Read a sequence of words from the EEPROM.
324 wb_read_eeprom(sc, dest, off, cnt, swap)
332 u_int16_t word = 0, *ptr;
334 for (i = 0; i < cnt; i++) {
335 wb_eeprom_getword(sc, off + i, &word);
336 ptr = (u_int16_t *)(dest + (i * 2));
345 * Read the MII serial port for the MII bit-bang module.
348 wb_mii_bitbang_read(device_t dev)
353 sc = device_get_softc(dev);
355 val = CSR_READ_4(sc, WB_SIO);
356 CSR_BARRIER(sc, WB_SIO, 4,
357 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
363 * Write the MII serial port for the MII bit-bang module.
366 wb_mii_bitbang_write(device_t dev, uint32_t val)
370 sc = device_get_softc(dev);
372 CSR_WRITE_4(sc, WB_SIO, val);
373 CSR_BARRIER(sc, WB_SIO, 4,
374 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
378 wb_miibus_readreg(dev, phy, reg)
383 return (mii_bitbang_readreg(dev, &wb_mii_bitbang_ops, phy, reg));
387 wb_miibus_writereg(dev, phy, reg, data)
392 mii_bitbang_writereg(dev, &wb_mii_bitbang_ops, phy, reg, data);
398 wb_miibus_statchg(dev)
402 struct mii_data *mii;
404 sc = device_get_softc(dev);
405 mii = device_get_softc(sc->wb_miibus);
406 wb_setcfg(sc, mii->mii_media_active);
410 * Program the 64-bit multicast hash filter.
418 u_int32_t hashes[2] = { 0, 0 };
419 struct ifmultiaddr *ifma;
425 rxfilt = CSR_READ_4(sc, WB_NETCFG);
427 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
428 rxfilt |= WB_NETCFG_RX_MULTI;
429 CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
430 CSR_WRITE_4(sc, WB_MAR0, 0xFFFFFFFF);
431 CSR_WRITE_4(sc, WB_MAR1, 0xFFFFFFFF);
435 /* first, zot all the existing hash bits */
436 CSR_WRITE_4(sc, WB_MAR0, 0);
437 CSR_WRITE_4(sc, WB_MAR1, 0);
439 /* now program new ones */
441 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
442 if (ifma->ifma_addr->sa_family != AF_LINK)
444 h = ~ether_crc32_be(LLADDR((struct sockaddr_dl *)
445 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
447 hashes[0] |= (1 << h);
449 hashes[1] |= (1 << (h - 32));
452 if_maddr_runlock(ifp);
455 rxfilt |= WB_NETCFG_RX_MULTI;
457 rxfilt &= ~WB_NETCFG_RX_MULTI;
459 CSR_WRITE_4(sc, WB_MAR0, hashes[0]);
460 CSR_WRITE_4(sc, WB_MAR1, hashes[1]);
461 CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
465 * The Winbond manual states that in order to fiddle with the
466 * 'full-duplex' and '100Mbps' bits in the netconfig register, we
467 * first have to put the transmit and/or receive logic in the idle state.
476 if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) {
478 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON));
480 for (i = 0; i < WB_TIMEOUT; i++) {
482 if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) &&
483 (CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE))
488 device_printf(sc->wb_dev,
489 "failed to force tx and rx to idle state\n");
492 if (IFM_SUBTYPE(media) == IFM_10_T)
493 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
495 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
497 if ((media & IFM_GMASK) == IFM_FDX)
498 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
500 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
503 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON);
511 struct mii_data *mii;
512 struct mii_softc *miisc;
514 CSR_WRITE_4(sc, WB_NETCFG, 0);
515 CSR_WRITE_4(sc, WB_BUSCTL, 0);
516 CSR_WRITE_4(sc, WB_TXADDR, 0);
517 CSR_WRITE_4(sc, WB_RXADDR, 0);
519 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
520 WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
522 for (i = 0; i < WB_TIMEOUT; i++) {
524 if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET))
528 device_printf(sc->wb_dev, "reset never completed!\n");
530 /* Wait a little while for the chip to get its brains in order. */
533 if (sc->wb_miibus == NULL)
536 mii = device_get_softc(sc->wb_miibus);
537 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
545 struct mii_data *mii = NULL;
549 mii = device_get_softc(sc->wb_miibus);
553 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
554 media = mii->mii_media_active & ~IFM_10_T;
556 } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
557 media = mii->mii_media_active & ~IFM_100_TX;
562 ifmedia_set(&mii->mii_media, media);
566 * Probe for a Winbond chip. Check the PCI vendor and device
567 * IDs against our list and return a device name if we find a match.
573 const struct wb_type *t;
577 while(t->wb_name != NULL) {
578 if ((pci_get_vendor(dev) == t->wb_vid) &&
579 (pci_get_device(dev) == t->wb_did)) {
580 device_set_desc(dev, t->wb_name);
581 return (BUS_PROBE_DEFAULT);
590 * Attach the interface. Allocate softc structures, do ifmedia
591 * setup and ethernet/BPF attach.
597 u_char eaddr[ETHER_ADDR_LEN];
602 sc = device_get_softc(dev);
605 mtx_init(&sc->wb_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
607 callout_init_mtx(&sc->wb_stat_callout, &sc->wb_mtx, 0);
610 * Map control/status registers.
612 pci_enable_busmaster(dev);
615 sc->wb_res = bus_alloc_resource_any(dev, WB_RES, &rid, RF_ACTIVE);
617 if (sc->wb_res == NULL) {
618 device_printf(dev, "couldn't map ports/memory\n");
623 /* Allocate interrupt */
625 sc->wb_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
626 RF_SHAREABLE | RF_ACTIVE);
628 if (sc->wb_irq == NULL) {
629 device_printf(dev, "couldn't map interrupt\n");
634 /* Save the cache line size. */
635 sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF;
637 /* Reset the adapter. */
641 * Get station address from the EEPROM.
643 wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
645 sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF,
646 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
648 if (sc->wb_ldata == NULL) {
649 device_printf(dev, "no memory for list buffers!\n");
654 bzero(sc->wb_ldata, sizeof(struct wb_list_data));
656 ifp = sc->wb_ifp = if_alloc(IFT_ETHER);
658 device_printf(dev, "can not if_alloc()\n");
663 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
664 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
665 ifp->if_ioctl = wb_ioctl;
666 ifp->if_start = wb_start;
667 ifp->if_init = wb_init;
668 ifp->if_snd.ifq_maxlen = WB_TX_LIST_CNT - 1;
673 error = mii_attach(dev, &sc->wb_miibus, ifp, wb_ifmedia_upd,
674 wb_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
676 device_printf(dev, "attaching PHYs failed\n");
681 * Call MI attach routine.
683 ether_ifattach(ifp, eaddr);
685 /* Hook interrupt last to avoid having to lock softc */
686 error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET | INTR_MPSAFE,
687 NULL, wb_intr, sc, &sc->wb_intrhand);
690 device_printf(dev, "couldn't set up irq\n");
703 * Shutdown hardware and free up resources. This can be called any
704 * time after the mutex has been initialized. It is called in both
705 * the error case in attach and the normal detach case so it needs
706 * to be careful about only freeing resources that have actually been
716 sc = device_get_softc(dev);
717 KASSERT(mtx_initialized(&sc->wb_mtx), ("wb mutex not initialized"));
721 * Delete any miibus and phy devices attached to this interface.
722 * This should only be done if attach succeeded.
724 if (device_is_attached(dev)) {
729 callout_drain(&sc->wb_stat_callout);
732 device_delete_child(dev, sc->wb_miibus);
733 bus_generic_detach(dev);
736 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand);
738 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
740 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
746 contigfree(sc->wb_ldata, sizeof(struct wb_list_data) + 8,
750 mtx_destroy(&sc->wb_mtx);
756 * Initialize the transmit descriptors.
762 struct wb_chain_data *cd;
763 struct wb_list_data *ld;
769 for (i = 0; i < WB_TX_LIST_CNT; i++) {
770 cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i];
771 if (i == (WB_TX_LIST_CNT - 1)) {
772 cd->wb_tx_chain[i].wb_nextdesc =
775 cd->wb_tx_chain[i].wb_nextdesc =
776 &cd->wb_tx_chain[i + 1];
780 cd->wb_tx_free = &cd->wb_tx_chain[0];
781 cd->wb_tx_tail = cd->wb_tx_head = NULL;
788 * Initialize the RX descriptors and allocate mbufs for them. Note that
789 * we arrange the descriptors in a closed ring, so that the last descriptor
790 * points back to the first.
796 struct wb_chain_data *cd;
797 struct wb_list_data *ld;
803 for (i = 0; i < WB_RX_LIST_CNT; i++) {
804 cd->wb_rx_chain[i].wb_ptr =
805 (struct wb_desc *)&ld->wb_rx_list[i];
806 cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i];
807 if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS)
809 if (i == (WB_RX_LIST_CNT - 1)) {
810 cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0];
811 ld->wb_rx_list[i].wb_next =
812 vtophys(&ld->wb_rx_list[0]);
814 cd->wb_rx_chain[i].wb_nextdesc =
815 &cd->wb_rx_chain[i + 1];
816 ld->wb_rx_list[i].wb_next =
817 vtophys(&ld->wb_rx_list[i + 1]);
821 cd->wb_rx_head = &cd->wb_rx_chain[0];
827 wb_bfree(struct mbuf *m, void *buf, void *args)
830 return (EXT_FREE_OK);
834 * Initialize an RX descriptor and attach an MBUF cluster.
839 struct wb_chain_onefrag *c;
842 struct mbuf *m_new = NULL;
845 MGETHDR(m_new, M_NOWAIT, MT_DATA);
848 m_new->m_data = c->wb_buf;
849 m_new->m_pkthdr.len = m_new->m_len = WB_BUFBYTES;
850 MEXTADD(m_new, c->wb_buf, WB_BUFBYTES, wb_bfree, c->wb_buf,
851 NULL, 0, EXT_NET_DRV);
854 m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES;
855 m_new->m_data = m_new->m_ext.ext_buf;
858 m_adj(m_new, sizeof(u_int64_t));
861 c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t));
862 c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536;
863 c->wb_ptr->wb_status = WB_RXSTAT;
869 * A frame has been uploaded: pass the resulting mbuf chain up to
870 * the higher level protocols.
876 struct mbuf *m = NULL;
878 struct wb_chain_onefrag *cur_rx;
886 while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) &
888 struct mbuf *m0 = NULL;
890 cur_rx = sc->wb_cdata.wb_rx_head;
891 sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc;
895 if ((rxstat & WB_RXSTAT_MIIERR) ||
896 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) ||
897 (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) ||
898 !(rxstat & WB_RXSTAT_LASTFRAG) ||
899 !(rxstat & WB_RXSTAT_RXCMP)) {
901 wb_newbuf(sc, cur_rx, m);
902 device_printf(sc->wb_dev,
903 "receiver babbling: possible chip bug,"
911 if (rxstat & WB_RXSTAT_RXERR) {
913 wb_newbuf(sc, cur_rx, m);
917 /* No errors; receive the packet. */
918 total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status);
921 * XXX The Winbond chip includes the CRC with every
922 * received frame, and there's no way to turn this
923 * behavior off (at least, I can't find anything in
924 * the manual that explains how to do it) so we have
925 * to trim off the CRC manually.
927 total_len -= ETHER_CRC_LEN;
929 m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp,
931 wb_newbuf(sc, cur_rx, m);
940 (*ifp->if_input)(ifp, m);
951 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
952 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
953 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
954 if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND)
955 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
959 * A frame was downloaded to the chip. It's safe for us to clean up
966 struct wb_chain *cur_tx;
971 /* Clear the timeout timer. */
974 if (sc->wb_cdata.wb_tx_head == NULL)
978 * Go through our tx list and free mbufs for those
979 * frames that have been transmitted.
981 while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) {
984 cur_tx = sc->wb_cdata.wb_tx_head;
985 txstat = WB_TXSTATUS(cur_tx);
987 if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT)
990 if (txstat & WB_TXSTAT_TXERR) {
992 if (txstat & WB_TXSTAT_ABORT)
993 ifp->if_collisions++;
994 if (txstat & WB_TXSTAT_LATECOLL)
995 ifp->if_collisions++;
998 ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3;
1001 m_freem(cur_tx->wb_mbuf);
1002 cur_tx->wb_mbuf = NULL;
1004 if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) {
1005 sc->wb_cdata.wb_tx_head = NULL;
1006 sc->wb_cdata.wb_tx_tail = NULL;
1010 sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc;
1015 * TX 'end of channel' interrupt handler.
1019 struct wb_softc *sc;
1027 if (sc->wb_cdata.wb_tx_head == NULL) {
1028 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1029 sc->wb_cdata.wb_tx_tail = NULL;
1031 if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) {
1032 WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN;
1034 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1043 struct wb_softc *sc;
1051 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1056 /* Disable interrupts. */
1057 CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1061 status = CSR_READ_4(sc, WB_ISR);
1063 CSR_WRITE_4(sc, WB_ISR, status);
1065 if ((status & WB_INTRS) == 0)
1068 if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) {
1071 if (status & WB_ISR_RX_ERR)
1077 if (status & WB_ISR_RX_OK)
1080 if (status & WB_ISR_RX_IDLE)
1083 if (status & WB_ISR_TX_OK)
1086 if (status & WB_ISR_TX_NOBUF)
1089 if (status & WB_ISR_TX_IDLE) {
1091 if (sc->wb_cdata.wb_tx_head != NULL) {
1092 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1093 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1097 if (status & WB_ISR_TX_UNDERRUN) {
1100 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1101 /* Jack up TX threshold */
1102 sc->wb_txthresh += WB_TXTHRESH_CHUNK;
1103 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1104 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1105 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1108 if (status & WB_ISR_BUS_ERR) {
1115 /* Re-enable interrupts. */
1116 CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1118 if (ifp->if_snd.ifq_head != NULL) {
1119 wb_start_locked(ifp);
1129 struct wb_softc *sc;
1130 struct mii_data *mii;
1134 mii = device_get_softc(sc->wb_miibus);
1138 if (sc->wb_timer > 0 && --sc->wb_timer == 0)
1140 callout_reset(&sc->wb_stat_callout, hz, wb_tick, sc);
1144 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1145 * pointers to the fragment pointers.
1148 wb_encap(sc, c, m_head)
1149 struct wb_softc *sc;
1151 struct mbuf *m_head;
1154 struct wb_desc *f = NULL;
1159 * Start packing the mbufs in this chain into
1160 * the fragment pointers. Stop when we run out
1161 * of fragments or hit the end of the mbuf chain.
1166 for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1167 if (m->m_len != 0) {
1168 if (frag == WB_MAXFRAGS)
1170 total_len += m->m_len;
1171 f = &c->wb_ptr->wb_frag[frag];
1172 f->wb_ctl = WB_TXCTL_TLINK | m->m_len;
1174 f->wb_ctl |= WB_TXCTL_FIRSTFRAG;
1177 f->wb_status = WB_TXSTAT_OWN;
1178 f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]);
1179 f->wb_data = vtophys(mtod(m, vm_offset_t));
1185 * Handle special case: we used up all 16 fragments,
1186 * but we have more mbufs left in the chain. Copy the
1187 * data into an mbuf cluster. Note that we don't
1188 * bother clearing the values in the other fragment
1189 * pointers/counters; it wouldn't gain us anything,
1190 * and would waste cycles.
1193 struct mbuf *m_new = NULL;
1195 MGETHDR(m_new, M_NOWAIT, MT_DATA);
1198 if (m_head->m_pkthdr.len > MHLEN) {
1199 MCLGET(m_new, M_NOWAIT);
1200 if (!(m_new->m_flags & M_EXT)) {
1205 m_copydata(m_head, 0, m_head->m_pkthdr.len,
1206 mtod(m_new, caddr_t));
1207 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1210 f = &c->wb_ptr->wb_frag[0];
1212 f->wb_data = vtophys(mtod(m_new, caddr_t));
1213 f->wb_ctl = total_len = m_new->m_len;
1214 f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG;
1218 if (total_len < WB_MIN_FRAMELEN) {
1219 f = &c->wb_ptr->wb_frag[frag];
1220 f->wb_ctl = WB_MIN_FRAMELEN - total_len;
1221 f->wb_data = vtophys(&sc->wb_cdata.wb_pad);
1222 f->wb_ctl |= WB_TXCTL_TLINK;
1223 f->wb_status = WB_TXSTAT_OWN;
1227 c->wb_mbuf = m_head;
1228 c->wb_lastdesc = frag - 1;
1229 WB_TXCTL(c) |= WB_TXCTL_LASTFRAG;
1230 WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]);
1236 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1237 * to the mbuf data regions directly in the transmit lists. We also save a
1238 * copy of the pointers since the transmit list fragment pointers are
1239 * physical addresses.
1246 struct wb_softc *sc;
1250 wb_start_locked(ifp);
1255 wb_start_locked(ifp)
1258 struct wb_softc *sc;
1259 struct mbuf *m_head = NULL;
1260 struct wb_chain *cur_tx = NULL, *start_tx;
1266 * Check for an available queue slot. If there are none,
1269 if (sc->wb_cdata.wb_tx_free->wb_mbuf != NULL) {
1270 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1274 start_tx = sc->wb_cdata.wb_tx_free;
1276 while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) {
1277 IF_DEQUEUE(&ifp->if_snd, m_head);
1281 /* Pick a descriptor off the free list. */
1282 cur_tx = sc->wb_cdata.wb_tx_free;
1283 sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc;
1285 /* Pack the data into the descriptor. */
1286 wb_encap(sc, cur_tx, m_head);
1288 if (cur_tx != start_tx)
1289 WB_TXOWN(cur_tx) = WB_TXSTAT_OWN;
1292 * If there's a BPF listener, bounce a copy of this frame
1295 BPF_MTAP(ifp, cur_tx->wb_mbuf);
1299 * If there are no packets queued, bail.
1305 * Place the request for the upload interrupt
1306 * in the last descriptor in the chain. This way, if
1307 * we're chaining several packets at once, we'll only
1308 * get an interrupt once for the whole chain rather than
1309 * once for each packet.
1311 WB_TXCTL(cur_tx) |= WB_TXCTL_FINT;
1312 cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT;
1313 sc->wb_cdata.wb_tx_tail = cur_tx;
1315 if (sc->wb_cdata.wb_tx_head == NULL) {
1316 sc->wb_cdata.wb_tx_head = start_tx;
1317 WB_TXOWN(start_tx) = WB_TXSTAT_OWN;
1318 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1321 * We need to distinguish between the case where
1322 * the own bit is clear because the chip cleared it
1323 * and where the own bit is clear because we haven't
1324 * set it yet. The magic value WB_UNSET is just some
1325 * ramdomly chosen number which doesn't have the own
1326 * bit set. When we actually transmit the frame, the
1327 * status word will have _only_ the own bit set, so
1328 * the txeoc handler will be able to tell if it needs
1329 * to initiate another transmission to flush out pending
1332 WB_TXOWN(start_tx) = WB_UNSENT;
1336 * Set a timeout in case the chip goes out to lunch.
1345 struct wb_softc *sc = xsc;
1354 struct wb_softc *sc;
1356 struct ifnet *ifp = sc->wb_ifp;
1358 struct mii_data *mii;
1361 mii = device_get_softc(sc->wb_miibus);
1364 * Cancel pending I/O and free all RX/TX buffers.
1369 sc->wb_txthresh = WB_TXTHRESH_INIT;
1372 * Set cache alignment and burst length.
1375 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG);
1376 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1377 WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1380 CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION);
1381 WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG);
1382 switch(sc->wb_cachesize) {
1384 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG);
1387 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG);
1390 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG);
1394 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE);
1398 /* This doesn't tend to work too well at 100Mbps. */
1399 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON);
1401 /* Init our MAC address */
1402 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1403 CSR_WRITE_1(sc, WB_NODE0 + i, IF_LLADDR(sc->wb_ifp)[i]);
1406 /* Init circular RX list. */
1407 if (wb_list_rx_init(sc) == ENOBUFS) {
1408 device_printf(sc->wb_dev,
1409 "initialization failed: no memory for rx buffers\n");
1414 /* Init TX descriptors. */
1415 wb_list_tx_init(sc);
1417 /* If we want promiscuous mode, set the allframes bit. */
1418 if (ifp->if_flags & IFF_PROMISC) {
1419 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1421 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1425 * Set capture broadcast bit to capture broadcast frames.
1427 if (ifp->if_flags & IFF_BROADCAST) {
1428 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1430 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1434 * Program the multicast filter, if necessary.
1439 * Load the address of the RX list.
1441 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1442 CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
1445 * Enable interrupts.
1447 CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1448 CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF);
1450 /* Enable receiver and transmitter. */
1451 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1452 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
1454 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1455 CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0]));
1456 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1460 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1461 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1463 callout_reset(&sc->wb_stat_callout, hz, wb_tick, sc);
1467 * Set media options.
1473 struct wb_softc *sc;
1478 if (ifp->if_flags & IFF_UP)
1486 * Report current media status.
1489 wb_ifmedia_sts(ifp, ifmr)
1491 struct ifmediareq *ifmr;
1493 struct wb_softc *sc;
1494 struct mii_data *mii;
1499 mii = device_get_softc(sc->wb_miibus);
1502 ifmr->ifm_active = mii->mii_media_active;
1503 ifmr->ifm_status = mii->mii_media_status;
1508 wb_ioctl(ifp, command, data)
1513 struct wb_softc *sc = ifp->if_softc;
1514 struct mii_data *mii;
1515 struct ifreq *ifr = (struct ifreq *) data;
1521 if (ifp->if_flags & IFF_UP) {
1524 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1539 mii = device_get_softc(sc->wb_miibus);
1540 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1543 error = ether_ioctl(ifp, command, data);
1552 struct wb_softc *sc;
1559 if_printf(ifp, "watchdog timeout\n");
1561 if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
1562 if_printf(ifp, "no carrier - transceiver cable problem?\n");
1568 if (ifp->if_snd.ifq_head != NULL)
1569 wb_start_locked(ifp);
1573 * Stop the adapter and free any mbufs allocated to the
1578 struct wb_softc *sc;
1587 callout_stop(&sc->wb_stat_callout);
1589 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON));
1590 CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1591 CSR_WRITE_4(sc, WB_TXADDR, 0x00000000);
1592 CSR_WRITE_4(sc, WB_RXADDR, 0x00000000);
1595 * Free data in the RX lists.
1597 for (i = 0; i < WB_RX_LIST_CNT; i++) {
1598 if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) {
1599 m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf);
1600 sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL;
1603 bzero((char *)&sc->wb_ldata->wb_rx_list,
1604 sizeof(sc->wb_ldata->wb_rx_list));
1607 * Free the TX list buffers.
1609 for (i = 0; i < WB_TX_LIST_CNT; i++) {
1610 if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) {
1611 m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf);
1612 sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL;
1616 bzero((char *)&sc->wb_ldata->wb_tx_list,
1617 sizeof(sc->wb_ldata->wb_tx_list));
1619 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1623 * Stop all chip I/O so that the kernel's probe routines don't
1624 * get confused by errant DMAs when rebooting.
1630 struct wb_softc *sc;
1632 sc = device_get_softc(dev);