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 * VIA Rhine fast ethernet PCI NIC driver
39 * Supports various network adapters based on the VIA Rhine
40 * and Rhine II PCI controllers, including the D-Link DFE530TX.
41 * Datasheets are available at http://www.via.com.tw.
43 * Written by Bill Paul <wpaul@ctr.columbia.edu>
44 * Electrical Engineering Department
45 * Columbia University, New York City
49 * The VIA Rhine controllers are similar in some respects to the
50 * the DEC tulip chips, except less complicated. The controller
51 * uses an MII bus and an external physical layer interface. The
52 * receiver has a one entry perfect filter and a 64-bit hash table
53 * multicast filter. Transmit and receive descriptors are similar
56 * Some Rhine chips has a serious flaw in its transmit DMA mechanism:
57 * transmit buffers must be longword aligned. Unfortunately,
58 * FreeBSD doesn't guarantee that mbufs will be filled in starting
59 * at longword boundaries, so we have to do a buffer copy before
63 #ifdef HAVE_KERNEL_OPTION_HEADERS
64 #include "opt_device_polling.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/sockio.h>
71 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/module.h>
74 #include <sys/socket.h>
77 #include <net/ethernet.h>
78 #include <net/if_dl.h>
79 #include <net/if_media.h>
80 #include <net/if_types.h>
84 #include <vm/vm.h> /* for vtophys */
85 #include <vm/pmap.h> /* for vtophys */
86 #include <machine/bus.h>
87 #include <machine/resource.h>
91 #include <dev/mii/miivar.h>
93 #include <dev/pci/pcivar.h>
97 #include <pci/if_vrreg.h>
99 MODULE_DEPEND(vr, pci, 1, 1, 1);
100 MODULE_DEPEND(vr, ether, 1, 1, 1);
101 MODULE_DEPEND(vr, miibus, 1, 1, 1);
103 /* "device miibus" required. See GENERIC if you get errors here. */
104 #include "miibus_if.h"
107 * Various supported device vendors/types, their names & quirks
110 #define VR_Q_NEEDALIGN (1<<0)
111 #define VR_Q_CSUM (1<<1)
113 static struct vr_type {
119 { VIA_VENDORID, VIA_DEVICEID_RHINE,
121 "VIA VT3043 Rhine I 10/100BaseTX" },
122 { VIA_VENDORID, VIA_DEVICEID_RHINE_II,
124 "VIA VT86C100A Rhine II 10/100BaseTX" },
125 { VIA_VENDORID, VIA_DEVICEID_RHINE_II_2,
127 "VIA VT6102 Rhine II 10/100BaseTX" },
128 { VIA_VENDORID, VIA_DEVICEID_RHINE_III,
130 "VIA VT6105 Rhine III 10/100BaseTX" },
131 { VIA_VENDORID, VIA_DEVICEID_RHINE_III_M,
133 "VIA VT6105M Rhine III 10/100BaseTX" },
134 { DELTA_VENDORID, DELTA_DEVICEID_RHINE_II,
136 "Delta Electronics Rhine II 10/100BaseTX" },
137 { ADDTRON_VENDORID, ADDTRON_DEVICEID_RHINE_II,
139 "Addtron Technology Rhine II 10/100BaseTX" },
143 struct vr_list_data {
144 struct vr_desc vr_rx_list[VR_RX_LIST_CNT];
145 struct vr_desc vr_tx_list[VR_TX_LIST_CNT];
149 struct ifnet *vr_ifp; /* interface info */
151 struct resource *vr_res;
152 struct resource *vr_irq;
155 u_int8_t vr_revid; /* Rhine chip revision */
156 u_int8_t vr_flags; /* See VR_F_* below */
157 struct vr_list_data *vr_ldata;
158 struct callout vr_stat_callout;
160 int vr_suspended; /* if 1, sleeping/detaching */
162 struct vr_desc *vr_rx_head;
163 struct vr_desc *vr_tx_cons;
164 struct vr_desc *vr_tx_prod;
165 #ifdef DEVICE_POLLING
170 static int vr_probe(device_t);
171 static int vr_attach(device_t);
172 static int vr_detach(device_t);
174 static int vr_newbuf(struct vr_desc *, struct mbuf *);
176 static void vr_rxeof(struct vr_softc *);
177 static void vr_rxeoc(struct vr_softc *);
178 static void vr_txeof(struct vr_softc *);
179 static void vr_tick(void *);
180 static void vr_intr(void *);
181 static void vr_start(struct ifnet *);
182 static void vr_start_locked(struct ifnet *);
183 static int vr_ioctl(struct ifnet *, u_long, caddr_t);
184 static void vr_init(void *);
185 static void vr_init_locked(struct vr_softc *);
186 static void vr_stop(struct vr_softc *);
187 static void vr_watchdog(struct ifnet *);
188 static void vr_shutdown(device_t);
189 static int vr_ifmedia_upd(struct ifnet *);
190 static void vr_ifmedia_sts(struct ifnet *, struct ifmediareq *);
192 static int vr_mii_readreg(const struct vr_softc *, struct vr_mii_frame *);
193 static int vr_mii_writereg(const struct vr_softc *, const struct vr_mii_frame *);
194 static int vr_miibus_readreg(device_t, uint16_t, uint16_t);
195 static int vr_miibus_writereg(device_t, uint16_t, uint16_t, uint16_t);
196 static void vr_miibus_statchg(device_t);
198 static void vr_setcfg(struct vr_softc *, int);
199 static void vr_setmulti(struct vr_softc *);
200 static void vr_reset(const struct vr_softc *);
201 static int vr_list_rx_init(struct vr_softc *);
202 static int vr_list_tx_init(struct vr_softc *);
205 #define VR_RES SYS_RES_IOPORT
206 #define VR_RID VR_PCI_LOIO
208 #define VR_RES SYS_RES_MEMORY
209 #define VR_RID VR_PCI_LOMEM
212 static device_method_t vr_methods[] = {
213 /* Device interface */
214 DEVMETHOD(device_probe, vr_probe),
215 DEVMETHOD(device_attach, vr_attach),
216 DEVMETHOD(device_detach, vr_detach),
217 DEVMETHOD(device_shutdown, vr_shutdown),
220 DEVMETHOD(bus_print_child, bus_generic_print_child),
221 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
224 DEVMETHOD(miibus_readreg, vr_miibus_readreg),
225 DEVMETHOD(miibus_writereg, vr_miibus_writereg),
226 DEVMETHOD(miibus_statchg, vr_miibus_statchg),
231 static driver_t vr_driver = {
234 sizeof(struct vr_softc)
237 static devclass_t vr_devclass;
239 DRIVER_MODULE(vr, pci, vr_driver, vr_devclass, 0, 0);
240 DRIVER_MODULE(miibus, vr, miibus_driver, miibus_devclass, 0, 0);
241 #define VR_F_RESTART 0x01 /* Restart unit on next tick */
243 #define VR_LOCK(_sc) mtx_lock(&(_sc)->vr_mtx)
244 #define VR_UNLOCK(_sc) mtx_unlock(&(_sc)->vr_mtx)
245 #define VR_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->vr_mtx, MA_OWNED)
248 * register space access macros
250 #define CSR_WRITE_4(sc, reg, val) bus_write_4(sc->vr_res, reg, val)
251 #define CSR_WRITE_2(sc, reg, val) bus_write_2(sc->vr_res, reg, val)
252 #define CSR_WRITE_1(sc, reg, val) bus_write_1(sc->vr_res, reg, val)
254 #define CSR_READ_2(sc, reg) bus_read_2(sc->vr_res, reg)
255 #define CSR_READ_1(sc, reg) bus_read_1(sc->vr_res, reg)
257 #define VR_SETBIT(sc, reg, x) CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | (x))
258 #define VR_CLRBIT(sc, reg, x) CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~(x))
260 #define VR_SETBIT16(sc, reg, x) CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | (x))
261 #define VR_CLRBIT16(sc, reg, x) CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~(x))
265 * Read an PHY register through the MII.
268 vr_mii_readreg(const struct vr_softc *sc, struct vr_mii_frame *frame)
272 /* Set the PHY address. */
273 CSR_WRITE_1(sc, VR_PHYADDR, (CSR_READ_1(sc, VR_PHYADDR)& 0xe0)|
276 /* Set the register address. */
277 CSR_WRITE_1(sc, VR_MIIADDR, frame->mii_regaddr);
278 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_READ_ENB);
280 for (i = 0; i < 10000; i++) {
281 if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_READ_ENB) == 0)
285 frame->mii_data = CSR_READ_2(sc, VR_MIIDATA);
292 * Write to a PHY register through the MII.
295 vr_mii_writereg(const struct vr_softc *sc, const struct vr_mii_frame *frame)
299 /* Set the PHY address. */
300 CSR_WRITE_1(sc, VR_PHYADDR, (CSR_READ_1(sc, VR_PHYADDR)& 0xe0)|
303 /* Set the register address and data to write. */
304 CSR_WRITE_1(sc, VR_MIIADDR, frame->mii_regaddr);
305 CSR_WRITE_2(sc, VR_MIIDATA, frame->mii_data);
307 VR_SETBIT(sc, VR_MIICMD, VR_MIICMD_WRITE_ENB);
309 for (i = 0; i < 10000; i++) {
310 if ((CSR_READ_1(sc, VR_MIICMD) & VR_MIICMD_WRITE_ENB) == 0)
319 vr_miibus_readreg(device_t dev, uint16_t phy, uint16_t reg)
321 struct vr_mii_frame frame;
322 struct vr_softc *sc = device_get_softc(dev);
324 if (sc->vr_revid == REV_ID_VT6102_APOLLO && phy != 1)
327 bzero((char *)&frame, sizeof(frame));
328 frame.mii_phyaddr = phy;
329 frame.mii_regaddr = reg;
330 vr_mii_readreg(sc, &frame);
331 return (frame.mii_data);
335 vr_miibus_writereg(device_t dev, uint16_t phy, uint16_t reg, uint16_t data)
337 struct vr_mii_frame frame;
338 struct vr_softc *sc = device_get_softc(dev);
340 if (sc->vr_revid == REV_ID_VT6102_APOLLO && phy != 1)
343 bzero((char *)&frame, sizeof(frame));
344 frame.mii_phyaddr = phy;
345 frame.mii_regaddr = reg;
346 frame.mii_data = data;
347 vr_mii_writereg(sc, &frame);
353 vr_miibus_statchg(device_t dev)
355 struct mii_data *mii;
356 struct vr_softc *sc = device_get_softc(dev);
358 mii = device_get_softc(sc->vr_miibus);
359 vr_setcfg(sc, mii->mii_media_active);
363 * Program the 64-bit multicast hash filter.
366 vr_setmulti(struct vr_softc *sc)
368 struct ifnet *ifp = sc->vr_ifp;
370 uint32_t hashes[2] = { 0, 0 };
371 struct ifmultiaddr *ifma;
377 rxfilt = CSR_READ_1(sc, VR_RXCFG);
379 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
380 rxfilt |= VR_RXCFG_RX_MULTI;
381 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
382 CSR_WRITE_4(sc, VR_MAR0, 0xFFFFFFFF);
383 CSR_WRITE_4(sc, VR_MAR1, 0xFFFFFFFF);
387 /* First, zero out all the existing hash bits. */
388 CSR_WRITE_4(sc, VR_MAR0, 0);
389 CSR_WRITE_4(sc, VR_MAR1, 0);
391 /* Now program new ones. */
393 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
394 if (ifma->ifma_addr->sa_family != AF_LINK)
396 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
397 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
399 hashes[0] |= (1 << h);
401 hashes[1] |= (1 << (h - 32));
407 rxfilt |= VR_RXCFG_RX_MULTI;
409 rxfilt &= ~VR_RXCFG_RX_MULTI;
411 CSR_WRITE_4(sc, VR_MAR0, hashes[0]);
412 CSR_WRITE_4(sc, VR_MAR1, hashes[1]);
413 CSR_WRITE_1(sc, VR_RXCFG, rxfilt);
417 * In order to fiddle with the
418 * 'full-duplex' and '100Mbps' bits in the netconfig register, we
419 * first have to put the transmit and/or receive logic in the idle state.
422 vr_setcfg(struct vr_softc *sc, int media)
428 if (CSR_READ_2(sc, VR_COMMAND) & (VR_CMD_TX_ON|VR_CMD_RX_ON)) {
430 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_TX_ON|VR_CMD_RX_ON));
433 if ((media & IFM_GMASK) == IFM_FDX)
434 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
436 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_FULLDUPLEX);
439 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON|VR_CMD_RX_ON);
443 vr_reset(const struct vr_softc *sc)
447 /*VR_LOCK_ASSERT(sc);*/ /* XXX: Called during attach w/o lock. */
449 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RESET);
451 for (i = 0; i < VR_TIMEOUT; i++) {
453 if (!(CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RESET))
456 if (i == VR_TIMEOUT) {
457 if (sc->vr_revid < REV_ID_VT3065_A)
458 device_printf(sc->vr_dev, "reset never completed!\n");
460 /* Use newer force reset command */
461 device_printf(sc->vr_dev, "Using force reset command.\n");
462 VR_SETBIT(sc, VR_MISC_CR1, VR_MISCCR1_FORSRST);
466 /* Wait a little while for the chip to get its brains in order. */
471 * Probe for a VIA Rhine chip. Check the PCI vendor and device
472 * IDs against our list and return a match or NULL
474 static struct vr_type *
475 vr_match(device_t dev)
477 struct vr_type *t = vr_devs;
479 for (t = vr_devs; t->vr_name != NULL; t++)
480 if ((pci_get_vendor(dev) == t->vr_vid) &&
481 (pci_get_device(dev) == t->vr_did))
487 * Probe for a VIA Rhine chip. Check the PCI vendor and device
488 * IDs against our list and return a device name if we find a match.
491 vr_probe(device_t dev)
497 device_set_desc(dev, t->vr_name);
498 return (BUS_PROBE_DEFAULT);
504 * Attach the interface. Allocate softc structures, do ifmedia
505 * setup and ethernet/BPF attach.
508 vr_attach(device_t dev)
511 u_char eaddr[ETHER_ADDR_LEN];
517 sc = device_get_softc(dev);
520 KASSERT(t != NULL, ("Lost if_vr device match"));
521 sc->vr_quirks = t->vr_quirks;
522 device_printf(dev, "Quirks: 0x%x\n", sc->vr_quirks);
524 mtx_init(&sc->vr_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
526 callout_init_mtx(&sc->vr_stat_callout, &sc->vr_mtx, 0);
529 * Map control/status registers.
531 pci_enable_busmaster(dev);
532 sc->vr_revid = pci_read_config(dev, VR_PCI_REVID, 4) & 0x000000FF;
535 sc->vr_res = bus_alloc_resource_any(dev, VR_RES, &rid, RF_ACTIVE);
537 if (sc->vr_res == NULL) {
538 device_printf(dev, "couldn't map ports/memory\n");
543 /* Allocate interrupt */
545 sc->vr_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
546 RF_SHAREABLE | RF_ACTIVE);
548 if (sc->vr_irq == NULL) {
549 device_printf(dev, "couldn't map interrupt\n");
554 /* Allocate ifnet structure. */
555 ifp = sc->vr_ifp = if_alloc(IFT_ETHER);
557 device_printf(dev, "can not if_alloc()\n");
562 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
563 ifp->if_mtu = ETHERMTU;
564 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
565 ifp->if_ioctl = vr_ioctl;
566 ifp->if_start = vr_start;
567 ifp->if_watchdog = vr_watchdog;
568 ifp->if_init = vr_init;
569 IFQ_SET_MAXLEN(&ifp->if_snd, VR_TX_LIST_CNT - 1);
570 ifp->if_snd.ifq_maxlen = VR_TX_LIST_CNT - 1;
571 IFQ_SET_READY(&ifp->if_snd);
573 if (sc->vr_quirks & VR_Q_CSUM) {
574 ifp->if_hwassist = (CSUM_IP | CSUM_TCP | CSUM_UDP);
575 ifp->if_capabilities |= IFCAP_HWCSUM;
578 ifp->if_capabilities |= IFCAP_VLAN_MTU;
579 ifp->if_capenable = ifp->if_capabilities;
580 if (ifp->if_capenable & IFCAP_TXCSUM)
581 ifp->if_hwassist = (CSUM_IP | CSUM_TCP | CSUM_UDP);
583 ifp->if_hwassist = 0;
585 #ifdef DEVICE_POLLING
586 ifp->if_capabilities |= IFCAP_POLLING;
590 * Windows may put the chip in suspend mode when it
591 * shuts down. Be sure to kick it in the head to wake it
594 VR_CLRBIT(sc, VR_STICKHW, (VR_STICKHW_DS0|VR_STICKHW_DS1));
596 /* Reset the adapter. */
600 * Turn on bit2 (MIION) in PCI configuration register 0x53 during
601 * initialization and disable AUTOPOLL.
603 pci_write_config(dev, VR_PCI_MODE,
604 pci_read_config(dev, VR_PCI_MODE, 4) | (VR_MODE3_MIION << 24), 4);
605 VR_CLRBIT(sc, VR_MIICMD, VR_MIICMD_AUTOPOLL);
608 * Get station address. The way the Rhine chips work,
609 * you're not allowed to directly access the EEPROM once
610 * they've been programmed a special way. Consequently,
611 * we need to read the node address from the PAR0 and PAR1
614 VR_SETBIT(sc, VR_EECSR, VR_EECSR_LOAD);
616 for (i = 0; i < ETHER_ADDR_LEN; i++)
617 eaddr[i] = CSR_READ_1(sc, VR_PAR0 + i);
619 sc->vr_ldata = contigmalloc(sizeof(struct vr_list_data), M_DEVBUF,
620 M_NOWAIT | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
622 if (sc->vr_ldata == NULL) {
623 device_printf(dev, "no memory for list buffers!\n");
629 if (mii_phy_probe(dev, &sc->vr_miibus,
630 vr_ifmedia_upd, vr_ifmedia_sts)) {
631 device_printf(dev, "MII without any phy!\n");
636 /* Call MI attach routine. */
637 ether_ifattach(ifp, eaddr);
639 sc->vr_suspended = 0;
641 /* Hook interrupt last to avoid having to lock softc */
642 error = bus_setup_intr(dev, sc->vr_irq, INTR_TYPE_NET | INTR_MPSAFE,
643 NULL, vr_intr, sc, &sc->vr_intrhand);
646 device_printf(dev, "couldn't set up irq\n");
659 * Shutdown hardware and free up resources. This can be called any
660 * time after the mutex has been initialized. It is called in both
661 * the error case in attach and the normal detach case so it needs
662 * to be careful about only freeing resources that have actually been
666 vr_detach(device_t dev)
668 struct vr_softc *sc = device_get_softc(dev);
669 struct ifnet *ifp = sc->vr_ifp;
671 KASSERT(mtx_initialized(&sc->vr_mtx), ("vr mutex not initialized"));
673 #ifdef DEVICE_POLLING
674 if (ifp->if_capenable & IFCAP_POLLING)
675 ether_poll_deregister(ifp);
678 /* These should only be active if attach succeeded */
679 if (device_is_attached(dev)) {
681 sc->vr_suspended = 1;
684 callout_drain(&sc->vr_stat_callout);
688 device_delete_child(dev, sc->vr_miibus);
689 bus_generic_detach(dev);
692 bus_teardown_intr(dev, sc->vr_irq, sc->vr_intrhand);
694 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->vr_irq);
696 bus_release_resource(dev, VR_RES, VR_RID, sc->vr_res);
702 contigfree(sc->vr_ldata, sizeof(struct vr_list_data), M_DEVBUF);
704 mtx_destroy(&sc->vr_mtx);
710 * Initialize the transmit descriptors.
713 vr_list_tx_init(struct vr_softc *sc)
715 struct vr_list_data *ld;
719 for (i = 0; i < VR_TX_LIST_CNT; i++) {
720 if (i == (VR_TX_LIST_CNT - 1)) {
721 ld->vr_tx_list[i].vr_next =
723 ld->vr_tx_list[i].vr_nextphys =
724 vtophys(&ld->vr_tx_list[0]);
726 ld->vr_tx_list[i].vr_next =
727 &ld->vr_tx_list[i + 1];
728 ld->vr_tx_list[i].vr_nextphys =
729 vtophys(&ld->vr_tx_list[i + 1]);
732 sc->vr_tx_cons = sc->vr_tx_prod = &ld->vr_tx_list[0];
739 * Initialize the RX descriptors and allocate mbufs for them. Note that
740 * we arrange the descriptors in a closed ring, so that the last descriptor
741 * points back to the first.
744 vr_list_rx_init(struct vr_softc *sc)
746 struct vr_list_data *ld;
753 for (i = 0; i < VR_RX_LIST_CNT; i++) {
754 if (vr_newbuf(&ld->vr_rx_list[i], NULL) == ENOBUFS)
756 if (i == (VR_RX_LIST_CNT - 1)) {
757 ld->vr_rx_list[i].vr_next = &ld->vr_rx_list[0];
758 ld->vr_rx_list[i].vr_nextphys =
759 vtophys(&ld->vr_rx_list[0]);
761 ld->vr_rx_list[i].vr_next =
762 &ld->vr_rx_list[i + 1];
763 ld->vr_rx_list[i].vr_nextphys =
764 vtophys(&ld->vr_rx_list[i + 1]);
768 sc->vr_rx_head = &ld->vr_rx_list[0];
774 * Initialize an RX descriptor and attach an MBUF cluster.
775 * Note: the length fields are only 11 bits wide, which means the
776 * largest size we can specify is 2047. This is important because
777 * MCLBYTES is 2048, so we have to subtract one otherwise we'll
778 * overflow the field and make a mess.
781 vr_newbuf(struct vr_desc *c, struct mbuf *m)
783 struct mbuf *m_new = NULL;
786 m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
791 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
792 m_new->m_data = m_new->m_ext.ext_buf;
795 m_adj(m_new, sizeof(uint64_t));
798 c->vr_status = VR_RXSTAT;
799 c->vr_data = vtophys(mtod(m_new, caddr_t));
800 c->vr_ctl = VR_RXCTL | VR_RXLEN;
806 * A frame has been uploaded: pass the resulting mbuf chain up to
807 * the higher level protocols.
810 vr_rxeof(struct vr_softc *sc)
814 struct vr_desc *cur_rx;
816 uint32_t rxstat, rxctl;
821 while (!((rxstat = sc->vr_rx_head->vr_status) &
823 #ifdef DEVICE_POLLING
824 if (ifp->if_capenable & IFCAP_POLLING) {
825 if (sc->rxcycles <= 0)
831 cur_rx = sc->vr_rx_head;
832 sc->vr_rx_head = cur_rx->vr_next;
836 * If an error occurs, update stats, clear the
837 * status word and leave the mbuf cluster in place:
838 * it should simply get re-used next time this descriptor
839 * comes up in the ring.
841 if (rxstat & VR_RXSTAT_RXERR) {
843 device_printf(sc->vr_dev,
844 "rx error (%02x):", rxstat & 0x000000ff);
845 if (rxstat & VR_RXSTAT_CRCERR)
846 printf(" crc error");
847 if (rxstat & VR_RXSTAT_FRAMEALIGNERR)
848 printf(" frame alignment error\n");
849 if (rxstat & VR_RXSTAT_FIFOOFLOW)
850 printf(" FIFO overflow");
851 if (rxstat & VR_RXSTAT_GIANT)
852 printf(" received giant packet");
853 if (rxstat & VR_RXSTAT_RUNT)
854 printf(" received runt packet");
855 if (rxstat & VR_RXSTAT_BUSERR)
856 printf(" system bus error");
857 if (rxstat & VR_RXSTAT_BUFFERR)
858 printf("rx buffer error");
860 vr_newbuf(cur_rx, m);
864 /* No errors; receive the packet. */
865 total_len = VR_RXBYTES(cur_rx->vr_status);
866 if (ifp->if_capenable & IFCAP_RXCSUM) {
867 rxctl = cur_rx->vr_ctl;
868 if ((rxctl & VR_RXCTL_GOODIP) == VR_RXCTL_GOODIP)
869 m->m_pkthdr.csum_flags |=
870 CSUM_IP_CHECKED | CSUM_IP_VALID;
871 if ((rxctl & VR_RXCTL_GOODTCPUDP)) {
872 m->m_pkthdr.csum_flags |=
873 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
874 m->m_pkthdr.csum_data = 0xffff;
879 * XXX The VIA Rhine chip includes the CRC with every
880 * received frame, and there's no way to turn this
881 * behavior off (at least, I can't find anything in
882 * the manual that explains how to do it) so we have
883 * to trim off the CRC manually.
885 total_len -= ETHER_CRC_LEN;
887 m0 = m_devget(mtod(m, char *), total_len, ETHER_ALIGN, ifp,
889 vr_newbuf(cur_rx, m);
898 (*ifp->if_input)(ifp, m);
904 vr_rxeoc(struct vr_softc *sc)
906 struct ifnet *ifp = sc->vr_ifp;
913 VR_CLRBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
916 /* Wait for receiver to stop */
918 i && (CSR_READ_2(sc, VR_COMMAND) & VR_CMD_RX_ON);
924 device_printf(sc->vr_dev, "rx shutdown error!\n");
925 sc->vr_flags |= VR_F_RESTART;
931 CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_rx_head));
932 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_ON);
933 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_RX_GO);
937 * A frame was downloaded to the chip. It's safe for us to clean up
941 vr_txeof(struct vr_softc *sc)
943 struct vr_desc *cur_tx;
944 struct ifnet *ifp = sc->vr_ifp;
949 * Go through our tx list and free mbufs for those
950 * frames that have been transmitted.
952 cur_tx = sc->vr_tx_cons;
953 while (cur_tx != sc->vr_tx_prod) {
957 txstat = cur_tx->vr_status;
959 if ((txstat & VR_TXSTAT_ABRT) ||
960 (txstat & VR_TXSTAT_UDF)) {
962 i && (CSR_READ_2(sc, VR_COMMAND) & VR_CMD_TX_ON);
964 ; /* Wait for chip to shutdown */
966 device_printf(sc->vr_dev, "tx shutdown timeout\n");
967 sc->vr_flags |= VR_F_RESTART;
970 atomic_set_acq_32(&cur_tx->vr_status, VR_TXSTAT_OWN);
971 CSR_WRITE_4(sc, VR_TXADDR, vtophys(cur_tx));
975 if (txstat & VR_TXSTAT_OWN)
978 if (txstat & VR_TXSTAT_ERRSUM) {
980 if (txstat & VR_TXSTAT_DEFER)
981 ifp->if_collisions++;
982 if (txstat & VR_TXSTAT_LATECOLL)
983 ifp->if_collisions++;
986 ifp->if_collisions +=(txstat & VR_TXSTAT_COLLCNT) >> 3;
989 if (cur_tx->vr_mbuf != NULL)
990 m_freem(cur_tx->vr_mbuf);
991 cur_tx->vr_mbuf = NULL;
992 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
994 cur_tx = cur_tx->vr_next;
996 sc->vr_tx_cons = cur_tx;
997 if (cur_tx->vr_mbuf == NULL)
1004 struct vr_softc *sc = xsc;
1005 struct mii_data *mii;
1009 if (sc->vr_flags & VR_F_RESTART) {
1010 device_printf(sc->vr_dev, "restarting\n");
1014 sc->vr_flags &= ~VR_F_RESTART;
1017 mii = device_get_softc(sc->vr_miibus);
1019 callout_reset(&sc->vr_stat_callout, hz, vr_tick, sc);
1022 #ifdef DEVICE_POLLING
1023 static poll_handler_t vr_poll;
1024 static poll_handler_t vr_poll_locked;
1027 vr_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1029 struct vr_softc *sc = ifp->if_softc;
1032 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1033 vr_poll_locked(ifp, cmd, count);
1038 vr_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
1040 struct vr_softc *sc = ifp->if_softc;
1044 sc->rxcycles = count;
1047 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1048 vr_start_locked(ifp);
1050 if (cmd == POLL_AND_CHECK_STATUS) {
1053 /* Also check status register. */
1054 status = CSR_READ_2(sc, VR_ISR);
1056 CSR_WRITE_2(sc, VR_ISR, status);
1058 if ((status & VR_INTRS) == 0)
1061 if (status & VR_ISR_RX_DROPPED) {
1062 if_printf(ifp, "rx packet lost\n");
1066 if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) ||
1067 (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW)) {
1068 if_printf(ifp, "receive error (%04x)", status);
1069 if (status & VR_ISR_RX_NOBUF)
1070 printf(" no buffers");
1071 if (status & VR_ISR_RX_OFLOW)
1072 printf(" overflow");
1073 if (status & VR_ISR_RX_DROPPED)
1074 printf(" packet lost");
1079 if ((status & VR_ISR_BUSERR) ||
1080 (status & VR_ISR_TX_UNDERRUN)) {
1086 if ((status & VR_ISR_UDFI) ||
1087 (status & VR_ISR_TX_ABRT2) ||
1088 (status & VR_ISR_TX_ABRT)) {
1090 if (sc->vr_tx_cons->vr_mbuf != NULL) {
1091 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_ON);
1092 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_TX_GO);
1097 #endif /* DEVICE_POLLING */
1102 struct vr_softc *sc = arg;
1103 struct ifnet *ifp = sc->vr_ifp;
1108 if (sc->vr_suspended) {
1110 * Forcibly disable interrupts.
1111 * XXX: Mobile VIA based platforms may need
1112 * interrupt re-enable on resume.
1114 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1118 #ifdef DEVICE_POLLING
1119 if (ifp->if_capenable & IFCAP_POLLING)
1123 /* Suppress unwanted interrupts. */
1124 if (!(ifp->if_flags & IFF_UP)) {
1129 /* Disable interrupts. */
1130 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1133 status = CSR_READ_2(sc, VR_ISR);
1136 CSR_WRITE_2(sc, VR_ISR, status);
1138 if ((status & VR_INTRS) == 0)
1141 if (status & VR_ISR_RX_OK)
1144 if (status & VR_ISR_RX_DROPPED) {
1145 device_printf(sc->vr_dev, "rx packet lost\n");
1149 if ((status & VR_ISR_RX_ERR) || (status & VR_ISR_RX_NOBUF) ||
1150 (status & VR_ISR_RX_NOBUF) || (status & VR_ISR_RX_OFLOW)) {
1151 device_printf(sc->vr_dev, "receive error (%04x)", status);
1152 if (status & VR_ISR_RX_NOBUF)
1153 printf(" no buffers");
1154 if (status & VR_ISR_RX_OFLOW)
1155 printf(" overflow");
1156 if (status & VR_ISR_RX_DROPPED)
1157 printf(" packet lost");
1162 if ((status & VR_ISR_BUSERR) || (status & VR_ISR_TX_UNDERRUN)) {
1168 if ((status & VR_ISR_TX_OK) || (status & VR_ISR_TX_ABRT) ||
1169 (status & VR_ISR_TX_ABRT2) || (status & VR_ISR_UDFI)) {
1171 if ((status & VR_ISR_UDFI) ||
1172 (status & VR_ISR_TX_ABRT2) ||
1173 (status & VR_ISR_TX_ABRT)) {
1175 if (sc->vr_tx_cons->vr_mbuf != NULL) {
1176 VR_SETBIT16(sc, VR_COMMAND,
1178 VR_SETBIT16(sc, VR_COMMAND,
1185 /* Re-enable interrupts. */
1186 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1188 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1189 vr_start_locked(ifp);
1196 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1197 * to the mbuf data regions directly in the transmit lists. We also save a
1198 * copy of the pointers since the transmit list fragment pointers are
1199 * physical addresses.
1203 vr_start(struct ifnet *ifp)
1205 struct vr_softc *sc = ifp->if_softc;
1208 vr_start_locked(ifp);
1213 vr_start_locked(struct ifnet *ifp)
1215 struct vr_softc *sc = ifp->if_softc;
1216 struct mbuf *m, *m_head;
1217 struct vr_desc *cur_tx, *n_tx;
1218 struct vr_desc *f = NULL;
1221 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
1224 for (cur_tx = sc->vr_tx_prod;
1225 cur_tx->vr_next != sc->vr_tx_cons; ) {
1226 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1232 * Some VIA Rhine wants packet buffers to be longword
1233 * aligned, but very often our mbufs aren't. Rather than
1234 * waste time trying to decide when to copy and when not
1235 * to copy, just do it all the time.
1237 if (sc->vr_quirks & VR_Q_NEEDALIGN) {
1238 m = m_defrag(m_head, M_DONTWAIT);
1240 /* Rollback, send what we were able to encap. */
1241 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1248 * The Rhine chip doesn't auto-pad, so we have to make
1249 * sure to pad short frames out to the minimum frame length
1252 if (m_head->m_pkthdr.len < VR_MIN_FRAMELEN) {
1253 if (m_head->m_next != NULL)
1254 m_head = m_defrag(m_head, M_DONTWAIT);
1255 m_head->m_pkthdr.len += VR_MIN_FRAMELEN - m_head->m_len;
1256 m_head->m_len = m_head->m_pkthdr.len;
1257 /* XXX: bzero the padding bytes */
1261 for (m = m_head; m != NULL; m = m->m_next) {
1264 if (n_tx->vr_next == sc->vr_tx_cons) {
1265 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1266 sc->vr_tx_prod = cur_tx;
1269 KASSERT(n_tx->vr_mbuf == NULL, ("if_vr_tx overrun"));
1272 f->vr_data = vtophys(mtod(m, caddr_t));
1274 cval |= VR_TXCTL_TLINK;
1276 if ((ifp->if_capenable & IFCAP_TXCSUM) &&
1277 m_head->m_pkthdr.csum_flags) {
1278 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
1279 cval |= VR_TXCTL_IPCSUM;
1280 if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
1281 cval |= VR_TXCTL_TCPCSUM;
1282 if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
1283 cval |= VR_TXCTL_UDPCSUM;
1287 cval |= VR_TXCTL_FIRSTFRAG;
1290 n_tx = n_tx->vr_next;
1293 KASSERT(f != NULL, ("if_vr: no packet processed"));
1294 f->vr_ctl |= VR_TXCTL_LASTFRAG|VR_TXCTL_FINT;
1295 cur_tx->vr_mbuf = m_head;
1296 atomic_set_acq_32(&cur_tx->vr_status, VR_TXSTAT_OWN);
1298 /* Tell the chip to start transmitting. */
1299 VR_SETBIT16(sc, VR_COMMAND, /*VR_CMD_TX_ON|*/ VR_CMD_TX_GO);
1301 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1305 * If there's a BPF listener, bounce a copy of this frame
1308 BPF_MTAP(ifp, m_head);
1311 sc->vr_tx_prod = cur_tx;
1317 struct vr_softc *sc = xsc;
1325 vr_init_locked(struct vr_softc *sc)
1327 struct ifnet *ifp = sc->vr_ifp;
1328 struct mii_data *mii;
1333 mii = device_get_softc(sc->vr_miibus);
1335 /* Cancel pending I/O and free all RX/TX buffers. */
1339 /* Set our station address. */
1340 for (i = 0; i < ETHER_ADDR_LEN; i++)
1341 CSR_WRITE_1(sc, VR_PAR0 + i, IF_LLADDR(sc->vr_ifp)[i]);
1344 VR_CLRBIT(sc, VR_BCR0, VR_BCR0_DMA_LENGTH);
1345 VR_SETBIT(sc, VR_BCR0, VR_BCR0_DMA_STORENFWD);
1348 * BCR0 and BCR1 can override the RXCFG and TXCFG registers,
1349 * so we must set both.
1351 VR_CLRBIT(sc, VR_BCR0, VR_BCR0_RX_THRESH);
1352 VR_SETBIT(sc, VR_BCR0, VR_BCR0_RXTHRESH128BYTES);
1354 VR_CLRBIT(sc, VR_BCR1, VR_BCR1_TX_THRESH);
1355 VR_SETBIT(sc, VR_BCR1, VR_BCR1_TXTHRESHSTORENFWD);
1357 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_THRESH);
1358 VR_SETBIT(sc, VR_RXCFG, VR_RXTHRESH_128BYTES);
1360 VR_CLRBIT(sc, VR_TXCFG, VR_TXCFG_TX_THRESH);
1361 VR_SETBIT(sc, VR_TXCFG, VR_TXTHRESH_STORENFWD);
1363 /* Init circular RX list. */
1364 if (vr_list_rx_init(sc) == ENOBUFS) {
1365 device_printf(sc->vr_dev,
1366 "initialization failed: no memory for rx buffers\n");
1371 /* Init tx descriptors. */
1372 vr_list_tx_init(sc);
1374 /* If we want promiscuous mode, set the allframes bit. */
1375 if (ifp->if_flags & IFF_PROMISC)
1376 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1378 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_PROMISC);
1380 /* Set capture broadcast bit to capture broadcast frames. */
1381 if (ifp->if_flags & IFF_BROADCAST)
1382 VR_SETBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1384 VR_CLRBIT(sc, VR_RXCFG, VR_RXCFG_RX_BROAD);
1387 * Program the multicast filter, if necessary.
1392 * Load the address of the RX list.
1394 CSR_WRITE_4(sc, VR_RXADDR, vtophys(sc->vr_rx_head));
1396 /* Enable receiver and transmitter. */
1397 CSR_WRITE_2(sc, VR_COMMAND, VR_CMD_TX_NOPOLL|VR_CMD_START|
1398 VR_CMD_TX_ON|VR_CMD_RX_ON|
1401 CSR_WRITE_4(sc, VR_TXADDR, vtophys(&sc->vr_ldata->vr_tx_list[0]));
1403 CSR_WRITE_2(sc, VR_ISR, 0xFFFF);
1404 #ifdef DEVICE_POLLING
1406 * Disable interrupts if we are polling.
1408 if (ifp->if_capenable & IFCAP_POLLING)
1409 CSR_WRITE_2(sc, VR_IMR, 0);
1413 * Enable interrupts.
1415 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1419 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1420 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1422 callout_reset(&sc->vr_stat_callout, hz, vr_tick, sc);
1426 * Set media options.
1429 vr_ifmedia_upd(struct ifnet *ifp)
1431 struct vr_softc *sc = ifp->if_softc;
1433 if (ifp->if_flags & IFF_UP)
1440 * Report current media status.
1443 vr_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1445 struct vr_softc *sc = ifp->if_softc;
1446 struct mii_data *mii;
1448 mii = device_get_softc(sc->vr_miibus);
1452 ifmr->ifm_active = mii->mii_media_active;
1453 ifmr->ifm_status = mii->mii_media_status;
1457 vr_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1459 struct vr_softc *sc = ifp->if_softc;
1460 struct ifreq *ifr = (struct ifreq *) data;
1461 struct mii_data *mii;
1467 if (ifp->if_flags & IFF_UP) {
1470 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1485 mii = device_get_softc(sc->vr_miibus);
1486 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1489 #ifdef DEVICE_POLLING
1490 if (ifr->ifr_reqcap & IFCAP_POLLING &&
1491 !(ifp->if_capenable & IFCAP_POLLING)) {
1492 error = ether_poll_register(vr_poll, ifp);
1496 /* Disable interrupts */
1497 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1498 ifp->if_capenable |= IFCAP_POLLING;
1503 if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
1504 ifp->if_capenable & IFCAP_POLLING) {
1505 error = ether_poll_deregister(ifp);
1506 /* Enable interrupts. */
1508 CSR_WRITE_2(sc, VR_IMR, VR_INTRS);
1509 ifp->if_capenable &= ~IFCAP_POLLING;
1513 #endif /* DEVICE_POLLING */
1514 ifp->if_capenable = ifr->ifr_reqcap;
1515 if (ifp->if_capenable & IFCAP_TXCSUM)
1516 ifp->if_hwassist = (CSUM_IP | CSUM_TCP | CSUM_UDP);
1518 ifp->if_hwassist = 0;
1521 error = ether_ioctl(ifp, command, data);
1529 vr_watchdog(struct ifnet *ifp)
1531 struct vr_softc *sc = ifp->if_softc;
1536 if_printf(ifp, "watchdog timeout\n");
1542 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1543 vr_start_locked(ifp);
1549 * Stop the adapter and free any mbufs allocated to the
1553 vr_stop(struct vr_softc *sc)
1563 callout_stop(&sc->vr_stat_callout);
1564 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1566 VR_SETBIT16(sc, VR_COMMAND, VR_CMD_STOP);
1567 VR_CLRBIT16(sc, VR_COMMAND, (VR_CMD_RX_ON|VR_CMD_TX_ON));
1568 CSR_WRITE_2(sc, VR_IMR, 0x0000);
1569 CSR_WRITE_4(sc, VR_TXADDR, 0x00000000);
1570 CSR_WRITE_4(sc, VR_RXADDR, 0x00000000);
1573 * Free data in the RX lists.
1575 for (i = 0; i < VR_RX_LIST_CNT; i++)
1576 if (sc->vr_ldata->vr_rx_list[i].vr_mbuf != NULL)
1577 m_freem(sc->vr_ldata->vr_rx_list[i].vr_mbuf);
1578 bzero((char *)&sc->vr_ldata->vr_rx_list,
1579 sizeof(sc->vr_ldata->vr_rx_list));
1582 * Free the TX list buffers.
1584 for (i = 0; i < VR_TX_LIST_CNT; i++)
1585 if (sc->vr_ldata->vr_tx_list[i].vr_mbuf != NULL)
1586 m_freem(sc->vr_ldata->vr_tx_list[i].vr_mbuf);
1587 bzero((char *)&sc->vr_ldata->vr_tx_list,
1588 sizeof(sc->vr_ldata->vr_tx_list));
1592 * Stop all chip I/O so that the kernel's probe routines don't
1593 * get confused by errant DMAs when rebooting.
1596 vr_shutdown(device_t dev)