3 * Bill Paul <wpaul@windriver.com>. 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 Networking Technologies VT612x PCI gigabit ethernet NIC driver.
39 * Written by Bill Paul <wpaul@windriver.com>
40 * Senior Networking Software Engineer
45 * The VIA Networking VT6122 is a 32bit, 33/66Mhz PCI device that
46 * combines a tri-speed ethernet MAC and PHY, with the following
49 * o Jumbo frame support up to 16K
50 * o Transmit and receive flow control
51 * o IPv4 checksum offload
52 * o VLAN tag insertion and stripping
54 * o 64-bit multicast hash table filter
55 * o 64 entry CAM filter
56 * o 16K RX FIFO and 48K TX FIFO memory
57 * o Interrupt moderation
59 * The VT6122 supports up to four transmit DMA queues. The descriptors
60 * in the transmit ring can address up to 7 data fragments; frames which
61 * span more than 7 data buffers must be coalesced, but in general the
62 * BSD TCP/IP stack rarely generates frames more than 2 or 3 fragments
63 * long. The receive descriptors address only a single buffer.
65 * There are two peculiar design issues with the VT6122. One is that
66 * receive data buffers must be aligned on a 32-bit boundary. This is
67 * not a problem where the VT6122 is used as a LOM device in x86-based
68 * systems, but on architectures that generate unaligned access traps, we
69 * have to do some copying.
71 * The other issue has to do with the way 64-bit addresses are handled.
72 * The DMA descriptors only allow you to specify 48 bits of addressing
73 * information. The remaining 16 bits are specified using one of the
74 * I/O registers. If you only have a 32-bit system, then this isn't
75 * an issue, but if you have a 64-bit system and more than 4GB of
76 * memory, you must have to make sure your network data buffers reside
77 * in the same 48-bit 'segment.'
79 * Special thanks to Ryan Fu at VIA Networking for providing documentation
80 * and sample NICs for testing.
83 #ifdef HAVE_KERNEL_OPTION_HEADERS
84 #include "opt_device_polling.h"
87 #include <sys/param.h>
88 #include <sys/endian.h>
89 #include <sys/systm.h>
90 #include <sys/sockio.h>
92 #include <sys/malloc.h>
93 #include <sys/module.h>
94 #include <sys/kernel.h>
95 #include <sys/socket.h>
96 #include <sys/sysctl.h>
99 #include <net/if_arp.h>
100 #include <net/ethernet.h>
101 #include <net/if_dl.h>
102 #include <net/if_var.h>
103 #include <net/if_media.h>
104 #include <net/if_types.h>
105 #include <net/if_vlan_var.h>
109 #include <machine/bus.h>
110 #include <machine/resource.h>
112 #include <sys/rman.h>
114 #include <dev/mii/mii.h>
115 #include <dev/mii/miivar.h>
117 #include <dev/pci/pcireg.h>
118 #include <dev/pci/pcivar.h>
120 MODULE_DEPEND(vge, pci, 1, 1, 1);
121 MODULE_DEPEND(vge, ether, 1, 1, 1);
122 MODULE_DEPEND(vge, miibus, 1, 1, 1);
124 /* "device miibus" required. See GENERIC if you get errors here. */
125 #include "miibus_if.h"
127 #include <dev/vge/if_vgereg.h>
128 #include <dev/vge/if_vgevar.h>
130 #define VGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
133 static int msi_disable = 0;
134 TUNABLE_INT("hw.vge.msi_disable", &msi_disable);
137 * The SQE error counter of MIB seems to report bogus value.
138 * Vendor's workaround does not seem to work on PCIe based
139 * controllers. Disable it until we find better workaround.
141 #undef VGE_ENABLE_SQEERR
144 * Various supported device vendors/types and their names.
146 static struct vge_type vge_devs[] = {
147 { VIA_VENDORID, VIA_DEVICEID_61XX,
148 "VIA Networking Velocity Gigabit Ethernet" },
152 static int vge_attach(device_t);
153 static int vge_detach(device_t);
154 static int vge_probe(device_t);
155 static int vge_resume(device_t);
156 static int vge_shutdown(device_t);
157 static int vge_suspend(device_t);
159 static void vge_cam_clear(struct vge_softc *);
160 static int vge_cam_set(struct vge_softc *, uint8_t *);
161 static void vge_clrwol(struct vge_softc *);
162 static void vge_discard_rxbuf(struct vge_softc *, int);
163 static int vge_dma_alloc(struct vge_softc *);
164 static void vge_dma_free(struct vge_softc *);
165 static void vge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
167 static void vge_eeprom_getword(struct vge_softc *, int, uint16_t *);
169 static int vge_encap(struct vge_softc *, struct mbuf **);
170 #ifndef __NO_STRICT_ALIGNMENT
172 vge_fixup_rx(struct mbuf *);
174 static void vge_freebufs(struct vge_softc *);
175 static void vge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
176 static int vge_ifmedia_upd(struct ifnet *);
177 static int vge_ifmedia_upd_locked(struct vge_softc *);
178 static void vge_init(void *);
179 static void vge_init_locked(struct vge_softc *);
180 static void vge_intr(void *);
181 static void vge_intr_holdoff(struct vge_softc *);
182 static int vge_ioctl(struct ifnet *, u_long, caddr_t);
183 static void vge_link_statchg(void *);
184 static int vge_miibus_readreg(device_t, int, int);
185 static int vge_miibus_writereg(device_t, int, int, int);
186 static void vge_miipoll_start(struct vge_softc *);
187 static void vge_miipoll_stop(struct vge_softc *);
188 static int vge_newbuf(struct vge_softc *, int);
189 static void vge_read_eeprom(struct vge_softc *, caddr_t, int, int, int);
190 static void vge_reset(struct vge_softc *);
191 static int vge_rx_list_init(struct vge_softc *);
192 static int vge_rxeof(struct vge_softc *, int);
193 static void vge_rxfilter(struct vge_softc *);
194 static void vge_setmedia(struct vge_softc *);
195 static void vge_setvlan(struct vge_softc *);
196 static void vge_setwol(struct vge_softc *);
197 static void vge_start(struct ifnet *);
198 static void vge_start_locked(struct ifnet *);
199 static void vge_stats_clear(struct vge_softc *);
200 static void vge_stats_update(struct vge_softc *);
201 static void vge_stop(struct vge_softc *);
202 static void vge_sysctl_node(struct vge_softc *);
203 static int vge_tx_list_init(struct vge_softc *);
204 static void vge_txeof(struct vge_softc *);
205 static void vge_watchdog(void *);
207 static device_method_t vge_methods[] = {
208 /* Device interface */
209 DEVMETHOD(device_probe, vge_probe),
210 DEVMETHOD(device_attach, vge_attach),
211 DEVMETHOD(device_detach, vge_detach),
212 DEVMETHOD(device_suspend, vge_suspend),
213 DEVMETHOD(device_resume, vge_resume),
214 DEVMETHOD(device_shutdown, vge_shutdown),
217 DEVMETHOD(miibus_readreg, vge_miibus_readreg),
218 DEVMETHOD(miibus_writereg, vge_miibus_writereg),
223 static driver_t vge_driver = {
226 sizeof(struct vge_softc)
229 static devclass_t vge_devclass;
231 DRIVER_MODULE(vge, pci, vge_driver, vge_devclass, 0, 0);
232 DRIVER_MODULE(miibus, vge, miibus_driver, miibus_devclass, 0, 0);
236 * Read a word of data stored in the EEPROM at address 'addr.'
239 vge_eeprom_getword(struct vge_softc *sc, int addr, uint16_t *dest)
245 * Enter EEPROM embedded programming mode. In order to
246 * access the EEPROM at all, we first have to set the
247 * EELOAD bit in the CHIPCFG2 register.
249 CSR_SETBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
250 CSR_SETBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
252 /* Select the address of the word we want to read */
253 CSR_WRITE_1(sc, VGE_EEADDR, addr);
255 /* Issue read command */
256 CSR_SETBIT_1(sc, VGE_EECMD, VGE_EECMD_ERD);
258 /* Wait for the done bit to be set. */
259 for (i = 0; i < VGE_TIMEOUT; i++) {
260 if (CSR_READ_1(sc, VGE_EECMD) & VGE_EECMD_EDONE)
264 if (i == VGE_TIMEOUT) {
265 device_printf(sc->vge_dev, "EEPROM read timed out\n");
270 /* Read the result */
271 word = CSR_READ_2(sc, VGE_EERDDAT);
273 /* Turn off EEPROM access mode. */
274 CSR_CLRBIT_1(sc, VGE_EECSR, VGE_EECSR_EMBP/*|VGE_EECSR_ECS*/);
275 CSR_CLRBIT_1(sc, VGE_CHIPCFG2, VGE_CHIPCFG2_EELOAD);
282 * Read a sequence of words from the EEPROM.
285 vge_read_eeprom(struct vge_softc *sc, caddr_t dest, int off, int cnt, int swap)
289 uint16_t word = 0, *ptr;
291 for (i = 0; i < cnt; i++) {
292 vge_eeprom_getword(sc, off + i, &word);
293 ptr = (uint16_t *)(dest + (i * 2));
300 for (i = 0; i < ETHER_ADDR_LEN; i++)
301 dest[i] = CSR_READ_1(sc, VGE_PAR0 + i);
306 vge_miipoll_stop(struct vge_softc *sc)
310 CSR_WRITE_1(sc, VGE_MIICMD, 0);
312 for (i = 0; i < VGE_TIMEOUT; i++) {
314 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
318 if (i == VGE_TIMEOUT)
319 device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
323 vge_miipoll_start(struct vge_softc *sc)
327 /* First, make sure we're idle. */
329 CSR_WRITE_1(sc, VGE_MIICMD, 0);
330 CSR_WRITE_1(sc, VGE_MIIADDR, VGE_MIIADDR_SWMPL);
332 for (i = 0; i < VGE_TIMEOUT; i++) {
334 if (CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL)
338 if (i == VGE_TIMEOUT) {
339 device_printf(sc->vge_dev, "failed to idle MII autopoll\n");
343 /* Now enable auto poll mode. */
345 CSR_WRITE_1(sc, VGE_MIICMD, VGE_MIICMD_MAUTO);
347 /* And make sure it started. */
349 for (i = 0; i < VGE_TIMEOUT; i++) {
351 if ((CSR_READ_1(sc, VGE_MIISTS) & VGE_MIISTS_IIDL) == 0)
355 if (i == VGE_TIMEOUT)
356 device_printf(sc->vge_dev, "failed to start MII autopoll\n");
360 vge_miibus_readreg(device_t dev, int phy, int reg)
362 struct vge_softc *sc;
366 sc = device_get_softc(dev);
368 vge_miipoll_stop(sc);
370 /* Specify the register we want to read. */
371 CSR_WRITE_1(sc, VGE_MIIADDR, reg);
373 /* Issue read command. */
374 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_RCMD);
376 /* Wait for the read command bit to self-clear. */
377 for (i = 0; i < VGE_TIMEOUT; i++) {
379 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_RCMD) == 0)
383 if (i == VGE_TIMEOUT)
384 device_printf(sc->vge_dev, "MII read timed out\n");
386 rval = CSR_READ_2(sc, VGE_MIIDATA);
388 vge_miipoll_start(sc);
394 vge_miibus_writereg(device_t dev, int phy, int reg, int data)
396 struct vge_softc *sc;
399 sc = device_get_softc(dev);
401 vge_miipoll_stop(sc);
403 /* Specify the register we want to write. */
404 CSR_WRITE_1(sc, VGE_MIIADDR, reg);
406 /* Specify the data we want to write. */
407 CSR_WRITE_2(sc, VGE_MIIDATA, data);
409 /* Issue write command. */
410 CSR_SETBIT_1(sc, VGE_MIICMD, VGE_MIICMD_WCMD);
412 /* Wait for the write command bit to self-clear. */
413 for (i = 0; i < VGE_TIMEOUT; i++) {
415 if ((CSR_READ_1(sc, VGE_MIICMD) & VGE_MIICMD_WCMD) == 0)
419 if (i == VGE_TIMEOUT) {
420 device_printf(sc->vge_dev, "MII write timed out\n");
424 vge_miipoll_start(sc);
430 vge_cam_clear(struct vge_softc *sc)
435 * Turn off all the mask bits. This tells the chip
436 * that none of the entries in the CAM filter are valid.
437 * desired entries will be enabled as we fill the filter in.
440 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
441 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
442 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE);
443 for (i = 0; i < 8; i++)
444 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
446 /* Clear the VLAN filter too. */
448 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|VGE_CAMADDR_AVSEL|0);
449 for (i = 0; i < 8; i++)
450 CSR_WRITE_1(sc, VGE_CAM0 + i, 0);
452 CSR_WRITE_1(sc, VGE_CAMADDR, 0);
453 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
454 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
460 vge_cam_set(struct vge_softc *sc, uint8_t *addr)
464 if (sc->vge_camidx == VGE_CAM_MAXADDRS)
467 /* Select the CAM data page. */
468 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
469 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMDATA);
471 /* Set the filter entry we want to update and enable writing. */
472 CSR_WRITE_1(sc, VGE_CAMADDR, VGE_CAMADDR_ENABLE|sc->vge_camidx);
474 /* Write the address to the CAM registers */
475 for (i = 0; i < ETHER_ADDR_LEN; i++)
476 CSR_WRITE_1(sc, VGE_CAM0 + i, addr[i]);
478 /* Issue a write command. */
479 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_WRITE);
481 /* Wake for it to clear. */
482 for (i = 0; i < VGE_TIMEOUT; i++) {
484 if ((CSR_READ_1(sc, VGE_CAMCTL) & VGE_CAMCTL_WRITE) == 0)
488 if (i == VGE_TIMEOUT) {
489 device_printf(sc->vge_dev, "setting CAM filter failed\n");
494 /* Select the CAM mask page. */
495 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
496 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_CAMMASK);
498 /* Set the mask bit that enables this filter. */
499 CSR_SETBIT_1(sc, VGE_CAM0 + (sc->vge_camidx/8),
500 1<<(sc->vge_camidx & 7));
505 /* Turn off access to CAM. */
506 CSR_WRITE_1(sc, VGE_CAMADDR, 0);
507 CSR_CLRBIT_1(sc, VGE_CAMCTL, VGE_CAMCTL_PAGESEL);
508 CSR_SETBIT_1(sc, VGE_CAMCTL, VGE_PAGESEL_MAR);
514 vge_setvlan(struct vge_softc *sc)
522 cfg = CSR_READ_1(sc, VGE_RXCFG);
523 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
524 cfg |= VGE_VTAG_OPT2;
526 cfg &= ~VGE_VTAG_OPT2;
527 CSR_WRITE_1(sc, VGE_RXCFG, cfg);
531 * Program the multicast filter. We use the 64-entry CAM filter
532 * for perfect filtering. If there's more than 64 multicast addresses,
533 * we use the hash filter instead.
536 vge_rxfilter(struct vge_softc *sc)
539 struct ifmultiaddr *ifma;
540 uint32_t h, hashes[2];
546 /* First, zot all the multicast entries. */
550 rxcfg = CSR_READ_1(sc, VGE_RXCTL);
551 rxcfg &= ~(VGE_RXCTL_RX_MCAST | VGE_RXCTL_RX_BCAST |
552 VGE_RXCTL_RX_PROMISC);
554 * Always allow VLAN oversized frames and frames for
557 rxcfg |= VGE_RXCTL_RX_GIANT | VGE_RXCTL_RX_UCAST;
560 if ((ifp->if_flags & IFF_BROADCAST) != 0)
561 rxcfg |= VGE_RXCTL_RX_BCAST;
562 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
563 if ((ifp->if_flags & IFF_PROMISC) != 0)
564 rxcfg |= VGE_RXCTL_RX_PROMISC;
565 if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
566 hashes[0] = 0xFFFFFFFF;
567 hashes[1] = 0xFFFFFFFF;
573 /* Now program new ones */
575 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
576 if (ifma->ifma_addr->sa_family != AF_LINK)
578 error = vge_cam_set(sc,
579 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
584 /* If there were too many addresses, use the hash filter. */
588 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
589 if (ifma->ifma_addr->sa_family != AF_LINK)
591 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
592 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
594 hashes[0] |= (1 << h);
596 hashes[1] |= (1 << (h - 32));
599 if_maddr_runlock(ifp);
602 if (hashes[0] != 0 || hashes[1] != 0)
603 rxcfg |= VGE_RXCTL_RX_MCAST;
604 CSR_WRITE_4(sc, VGE_MAR0, hashes[0]);
605 CSR_WRITE_4(sc, VGE_MAR1, hashes[1]);
606 CSR_WRITE_1(sc, VGE_RXCTL, rxcfg);
610 vge_reset(struct vge_softc *sc)
614 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_SOFTRESET);
616 for (i = 0; i < VGE_TIMEOUT; i++) {
618 if ((CSR_READ_1(sc, VGE_CRS1) & VGE_CR1_SOFTRESET) == 0)
622 if (i == VGE_TIMEOUT) {
623 device_printf(sc->vge_dev, "soft reset timed out\n");
624 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_STOP_FORCE);
632 * Probe for a VIA gigabit chip. Check the PCI vendor and device
633 * IDs against our list and return a device name if we find a match.
636 vge_probe(device_t dev)
642 while (t->vge_name != NULL) {
643 if ((pci_get_vendor(dev) == t->vge_vid) &&
644 (pci_get_device(dev) == t->vge_did)) {
645 device_set_desc(dev, t->vge_name);
646 return (BUS_PROBE_DEFAULT);
655 * Map a single buffer address.
658 struct vge_dmamap_arg {
659 bus_addr_t vge_busaddr;
663 vge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
665 struct vge_dmamap_arg *ctx;
670 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
672 ctx = (struct vge_dmamap_arg *)arg;
673 ctx->vge_busaddr = segs[0].ds_addr;
677 vge_dma_alloc(struct vge_softc *sc)
679 struct vge_dmamap_arg ctx;
680 struct vge_txdesc *txd;
681 struct vge_rxdesc *rxd;
682 bus_addr_t lowaddr, tx_ring_end, rx_ring_end;
686 * It seems old PCI controllers do not support DAC. DAC
687 * configuration can be enabled by accessing VGE_CHIPCFG3
688 * register but honor EEPROM configuration instead of
689 * blindly overriding DAC configuration. PCIe based
690 * controllers are supposed to support 64bit DMA so enable
691 * 64bit DMA on these controllers.
693 if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
694 lowaddr = BUS_SPACE_MAXADDR;
696 lowaddr = BUS_SPACE_MAXADDR_32BIT;
699 /* Create parent ring tag. */
700 error = bus_dma_tag_create(bus_get_dma_tag(sc->vge_dev),/* parent */
701 1, 0, /* algnmnt, boundary */
702 lowaddr, /* lowaddr */
703 BUS_SPACE_MAXADDR, /* highaddr */
704 NULL, NULL, /* filter, filterarg */
705 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
707 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
709 NULL, NULL, /* lockfunc, lockarg */
710 &sc->vge_cdata.vge_ring_tag);
712 device_printf(sc->vge_dev,
713 "could not create parent DMA tag.\n");
717 /* Create tag for Tx ring. */
718 error = bus_dma_tag_create(sc->vge_cdata.vge_ring_tag,/* parent */
719 VGE_TX_RING_ALIGN, 0, /* algnmnt, boundary */
720 BUS_SPACE_MAXADDR, /* lowaddr */
721 BUS_SPACE_MAXADDR, /* highaddr */
722 NULL, NULL, /* filter, filterarg */
723 VGE_TX_LIST_SZ, /* maxsize */
725 VGE_TX_LIST_SZ, /* maxsegsize */
727 NULL, NULL, /* lockfunc, lockarg */
728 &sc->vge_cdata.vge_tx_ring_tag);
730 device_printf(sc->vge_dev,
731 "could not allocate Tx ring DMA tag.\n");
735 /* Create tag for Rx ring. */
736 error = bus_dma_tag_create(sc->vge_cdata.vge_ring_tag,/* parent */
737 VGE_RX_RING_ALIGN, 0, /* algnmnt, boundary */
738 BUS_SPACE_MAXADDR, /* lowaddr */
739 BUS_SPACE_MAXADDR, /* highaddr */
740 NULL, NULL, /* filter, filterarg */
741 VGE_RX_LIST_SZ, /* maxsize */
743 VGE_RX_LIST_SZ, /* maxsegsize */
745 NULL, NULL, /* lockfunc, lockarg */
746 &sc->vge_cdata.vge_rx_ring_tag);
748 device_printf(sc->vge_dev,
749 "could not allocate Rx ring DMA tag.\n");
753 /* Allocate DMA'able memory and load the DMA map for Tx ring. */
754 error = bus_dmamem_alloc(sc->vge_cdata.vge_tx_ring_tag,
755 (void **)&sc->vge_rdata.vge_tx_ring,
756 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
757 &sc->vge_cdata.vge_tx_ring_map);
759 device_printf(sc->vge_dev,
760 "could not allocate DMA'able memory for Tx ring.\n");
765 error = bus_dmamap_load(sc->vge_cdata.vge_tx_ring_tag,
766 sc->vge_cdata.vge_tx_ring_map, sc->vge_rdata.vge_tx_ring,
767 VGE_TX_LIST_SZ, vge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
768 if (error != 0 || ctx.vge_busaddr == 0) {
769 device_printf(sc->vge_dev,
770 "could not load DMA'able memory for Tx ring.\n");
773 sc->vge_rdata.vge_tx_ring_paddr = ctx.vge_busaddr;
775 /* Allocate DMA'able memory and load the DMA map for Rx ring. */
776 error = bus_dmamem_alloc(sc->vge_cdata.vge_rx_ring_tag,
777 (void **)&sc->vge_rdata.vge_rx_ring,
778 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
779 &sc->vge_cdata.vge_rx_ring_map);
781 device_printf(sc->vge_dev,
782 "could not allocate DMA'able memory for Rx ring.\n");
787 error = bus_dmamap_load(sc->vge_cdata.vge_rx_ring_tag,
788 sc->vge_cdata.vge_rx_ring_map, sc->vge_rdata.vge_rx_ring,
789 VGE_RX_LIST_SZ, vge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
790 if (error != 0 || ctx.vge_busaddr == 0) {
791 device_printf(sc->vge_dev,
792 "could not load DMA'able memory for Rx ring.\n");
795 sc->vge_rdata.vge_rx_ring_paddr = ctx.vge_busaddr;
797 /* Tx/Rx descriptor queue should reside within 4GB boundary. */
798 tx_ring_end = sc->vge_rdata.vge_tx_ring_paddr + VGE_TX_LIST_SZ;
799 rx_ring_end = sc->vge_rdata.vge_rx_ring_paddr + VGE_RX_LIST_SZ;
800 if ((VGE_ADDR_HI(tx_ring_end) !=
801 VGE_ADDR_HI(sc->vge_rdata.vge_tx_ring_paddr)) ||
802 (VGE_ADDR_HI(rx_ring_end) !=
803 VGE_ADDR_HI(sc->vge_rdata.vge_rx_ring_paddr)) ||
804 VGE_ADDR_HI(tx_ring_end) != VGE_ADDR_HI(rx_ring_end)) {
805 device_printf(sc->vge_dev, "4GB boundary crossed, "
806 "switching to 32bit DMA address mode.\n");
808 /* Limit DMA address space to 32bit and try again. */
809 lowaddr = BUS_SPACE_MAXADDR_32BIT;
813 if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
814 lowaddr = VGE_BUF_DMA_MAXADDR;
816 lowaddr = BUS_SPACE_MAXADDR_32BIT;
817 /* Create parent buffer tag. */
818 error = bus_dma_tag_create(bus_get_dma_tag(sc->vge_dev),/* parent */
819 1, 0, /* algnmnt, boundary */
820 lowaddr, /* lowaddr */
821 BUS_SPACE_MAXADDR, /* highaddr */
822 NULL, NULL, /* filter, filterarg */
823 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
825 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
827 NULL, NULL, /* lockfunc, lockarg */
828 &sc->vge_cdata.vge_buffer_tag);
830 device_printf(sc->vge_dev,
831 "could not create parent buffer DMA tag.\n");
835 /* Create tag for Tx buffers. */
836 error = bus_dma_tag_create(sc->vge_cdata.vge_buffer_tag,/* parent */
837 1, 0, /* algnmnt, boundary */
838 BUS_SPACE_MAXADDR, /* lowaddr */
839 BUS_SPACE_MAXADDR, /* highaddr */
840 NULL, NULL, /* filter, filterarg */
841 MCLBYTES * VGE_MAXTXSEGS, /* maxsize */
842 VGE_MAXTXSEGS, /* nsegments */
843 MCLBYTES, /* maxsegsize */
845 NULL, NULL, /* lockfunc, lockarg */
846 &sc->vge_cdata.vge_tx_tag);
848 device_printf(sc->vge_dev, "could not create Tx DMA tag.\n");
852 /* Create tag for Rx buffers. */
853 error = bus_dma_tag_create(sc->vge_cdata.vge_buffer_tag,/* parent */
854 VGE_RX_BUF_ALIGN, 0, /* algnmnt, boundary */
855 BUS_SPACE_MAXADDR, /* lowaddr */
856 BUS_SPACE_MAXADDR, /* highaddr */
857 NULL, NULL, /* filter, filterarg */
858 MCLBYTES, /* maxsize */
860 MCLBYTES, /* maxsegsize */
862 NULL, NULL, /* lockfunc, lockarg */
863 &sc->vge_cdata.vge_rx_tag);
865 device_printf(sc->vge_dev, "could not create Rx DMA tag.\n");
869 /* Create DMA maps for Tx buffers. */
870 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
871 txd = &sc->vge_cdata.vge_txdesc[i];
873 txd->tx_dmamap = NULL;
874 error = bus_dmamap_create(sc->vge_cdata.vge_tx_tag, 0,
877 device_printf(sc->vge_dev,
878 "could not create Tx dmamap.\n");
882 /* Create DMA maps for Rx buffers. */
883 if ((error = bus_dmamap_create(sc->vge_cdata.vge_rx_tag, 0,
884 &sc->vge_cdata.vge_rx_sparemap)) != 0) {
885 device_printf(sc->vge_dev,
886 "could not create spare Rx dmamap.\n");
889 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
890 rxd = &sc->vge_cdata.vge_rxdesc[i];
892 rxd->rx_dmamap = NULL;
893 error = bus_dmamap_create(sc->vge_cdata.vge_rx_tag, 0,
896 device_printf(sc->vge_dev,
897 "could not create Rx dmamap.\n");
907 vge_dma_free(struct vge_softc *sc)
909 struct vge_txdesc *txd;
910 struct vge_rxdesc *rxd;
914 if (sc->vge_cdata.vge_tx_ring_tag != NULL) {
915 if (sc->vge_rdata.vge_tx_ring_paddr)
916 bus_dmamap_unload(sc->vge_cdata.vge_tx_ring_tag,
917 sc->vge_cdata.vge_tx_ring_map);
918 if (sc->vge_rdata.vge_tx_ring)
919 bus_dmamem_free(sc->vge_cdata.vge_tx_ring_tag,
920 sc->vge_rdata.vge_tx_ring,
921 sc->vge_cdata.vge_tx_ring_map);
922 sc->vge_rdata.vge_tx_ring = NULL;
923 sc->vge_rdata.vge_tx_ring_paddr = 0;
924 bus_dma_tag_destroy(sc->vge_cdata.vge_tx_ring_tag);
925 sc->vge_cdata.vge_tx_ring_tag = NULL;
928 if (sc->vge_cdata.vge_rx_ring_tag != NULL) {
929 if (sc->vge_rdata.vge_rx_ring_paddr)
930 bus_dmamap_unload(sc->vge_cdata.vge_rx_ring_tag,
931 sc->vge_cdata.vge_rx_ring_map);
932 if (sc->vge_rdata.vge_rx_ring)
933 bus_dmamem_free(sc->vge_cdata.vge_rx_ring_tag,
934 sc->vge_rdata.vge_rx_ring,
935 sc->vge_cdata.vge_rx_ring_map);
936 sc->vge_rdata.vge_rx_ring = NULL;
937 sc->vge_rdata.vge_rx_ring_paddr = 0;
938 bus_dma_tag_destroy(sc->vge_cdata.vge_rx_ring_tag);
939 sc->vge_cdata.vge_rx_ring_tag = NULL;
942 if (sc->vge_cdata.vge_tx_tag != NULL) {
943 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
944 txd = &sc->vge_cdata.vge_txdesc[i];
945 if (txd->tx_dmamap != NULL) {
946 bus_dmamap_destroy(sc->vge_cdata.vge_tx_tag,
948 txd->tx_dmamap = NULL;
951 bus_dma_tag_destroy(sc->vge_cdata.vge_tx_tag);
952 sc->vge_cdata.vge_tx_tag = NULL;
955 if (sc->vge_cdata.vge_rx_tag != NULL) {
956 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
957 rxd = &sc->vge_cdata.vge_rxdesc[i];
958 if (rxd->rx_dmamap != NULL) {
959 bus_dmamap_destroy(sc->vge_cdata.vge_rx_tag,
961 rxd->rx_dmamap = NULL;
964 if (sc->vge_cdata.vge_rx_sparemap != NULL) {
965 bus_dmamap_destroy(sc->vge_cdata.vge_rx_tag,
966 sc->vge_cdata.vge_rx_sparemap);
967 sc->vge_cdata.vge_rx_sparemap = NULL;
969 bus_dma_tag_destroy(sc->vge_cdata.vge_rx_tag);
970 sc->vge_cdata.vge_rx_tag = NULL;
973 if (sc->vge_cdata.vge_buffer_tag != NULL) {
974 bus_dma_tag_destroy(sc->vge_cdata.vge_buffer_tag);
975 sc->vge_cdata.vge_buffer_tag = NULL;
977 if (sc->vge_cdata.vge_ring_tag != NULL) {
978 bus_dma_tag_destroy(sc->vge_cdata.vge_ring_tag);
979 sc->vge_cdata.vge_ring_tag = NULL;
984 * Attach the interface. Allocate softc structures, do ifmedia
985 * setup and ethernet/BPF attach.
988 vge_attach(device_t dev)
990 u_char eaddr[ETHER_ADDR_LEN];
991 struct vge_softc *sc;
993 int error = 0, cap, i, msic, rid;
995 sc = device_get_softc(dev);
998 mtx_init(&sc->vge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1000 callout_init_mtx(&sc->vge_watchdog, &sc->vge_mtx, 0);
1003 * Map control/status registers.
1005 pci_enable_busmaster(dev);
1008 sc->vge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1011 if (sc->vge_res == NULL) {
1012 device_printf(dev, "couldn't map ports/memory\n");
1017 if (pci_find_cap(dev, PCIY_EXPRESS, &cap) == 0) {
1018 sc->vge_flags |= VGE_FLAG_PCIE;
1019 sc->vge_expcap = cap;
1021 sc->vge_flags |= VGE_FLAG_JUMBO;
1022 if (pci_find_cap(dev, PCIY_PMG, &cap) == 0) {
1023 sc->vge_flags |= VGE_FLAG_PMCAP;
1024 sc->vge_pmcap = cap;
1027 msic = pci_msi_count(dev);
1028 if (msi_disable == 0 && msic > 0) {
1030 if (pci_alloc_msi(dev, &msic) == 0) {
1032 sc->vge_flags |= VGE_FLAG_MSI;
1033 device_printf(dev, "Using %d MSI message\n",
1037 pci_release_msi(dev);
1041 /* Allocate interrupt */
1042 sc->vge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1043 ((sc->vge_flags & VGE_FLAG_MSI) ? 0 : RF_SHAREABLE) | RF_ACTIVE);
1044 if (sc->vge_irq == NULL) {
1045 device_printf(dev, "couldn't map interrupt\n");
1050 /* Reset the adapter. */
1052 /* Reload EEPROM. */
1053 CSR_WRITE_1(sc, VGE_EECSR, VGE_EECSR_RELOAD);
1054 for (i = 0; i < VGE_TIMEOUT; i++) {
1056 if ((CSR_READ_1(sc, VGE_EECSR) & VGE_EECSR_RELOAD) == 0)
1059 if (i == VGE_TIMEOUT)
1060 device_printf(dev, "EEPROM reload timed out\n");
1062 * Clear PACPI as EEPROM reload will set the bit. Otherwise
1063 * MAC will receive magic packet which in turn confuses
1066 CSR_CLRBIT_1(sc, VGE_CHIPCFG0, VGE_CHIPCFG0_PACPI);
1069 * Get station address from the EEPROM.
1071 vge_read_eeprom(sc, (caddr_t)eaddr, VGE_EE_EADDR, 3, 0);
1073 * Save configured PHY address.
1074 * It seems the PHY address of PCIe controllers just
1075 * reflects media jump strapping status so we assume the
1076 * internal PHY address of PCIe controller is at 1.
1078 if ((sc->vge_flags & VGE_FLAG_PCIE) != 0)
1079 sc->vge_phyaddr = 1;
1081 sc->vge_phyaddr = CSR_READ_1(sc, VGE_MIICFG) &
1083 /* Clear WOL and take hardware from powerdown. */
1085 vge_sysctl_node(sc);
1086 error = vge_dma_alloc(sc);
1090 ifp = sc->vge_ifp = if_alloc(IFT_ETHER);
1092 device_printf(dev, "can not if_alloc()\n");
1097 vge_miipoll_start(sc);
1099 error = mii_attach(dev, &sc->vge_miibus, ifp, vge_ifmedia_upd,
1100 vge_ifmedia_sts, BMSR_DEFCAPMASK, sc->vge_phyaddr, MII_OFFSET_ANY,
1103 device_printf(dev, "attaching PHYs failed\n");
1108 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1109 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1110 ifp->if_ioctl = vge_ioctl;
1111 ifp->if_capabilities = IFCAP_VLAN_MTU;
1112 ifp->if_start = vge_start;
1113 ifp->if_hwassist = VGE_CSUM_FEATURES;
1114 ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM |
1115 IFCAP_VLAN_HWTAGGING;
1116 if ((sc->vge_flags & VGE_FLAG_PMCAP) != 0)
1117 ifp->if_capabilities |= IFCAP_WOL;
1118 ifp->if_capenable = ifp->if_capabilities;
1119 #ifdef DEVICE_POLLING
1120 ifp->if_capabilities |= IFCAP_POLLING;
1122 ifp->if_init = vge_init;
1123 IFQ_SET_MAXLEN(&ifp->if_snd, VGE_TX_DESC_CNT - 1);
1124 ifp->if_snd.ifq_drv_maxlen = VGE_TX_DESC_CNT - 1;
1125 IFQ_SET_READY(&ifp->if_snd);
1128 * Call MI attach routine.
1130 ether_ifattach(ifp, eaddr);
1132 /* Tell the upper layer(s) we support long frames. */
1133 ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1135 /* Hook interrupt last to avoid having to lock softc */
1136 error = bus_setup_intr(dev, sc->vge_irq, INTR_TYPE_NET|INTR_MPSAFE,
1137 NULL, vge_intr, sc, &sc->vge_intrhand);
1140 device_printf(dev, "couldn't set up irq\n");
1141 ether_ifdetach(ifp);
1153 * Shutdown hardware and free up resources. This can be called any
1154 * time after the mutex has been initialized. It is called in both
1155 * the error case in attach and the normal detach case so it needs
1156 * to be careful about only freeing resources that have actually been
1160 vge_detach(device_t dev)
1162 struct vge_softc *sc;
1165 sc = device_get_softc(dev);
1166 KASSERT(mtx_initialized(&sc->vge_mtx), ("vge mutex not initialized"));
1169 #ifdef DEVICE_POLLING
1170 if (ifp->if_capenable & IFCAP_POLLING)
1171 ether_poll_deregister(ifp);
1174 /* These should only be active if attach succeeded */
1175 if (device_is_attached(dev)) {
1176 ether_ifdetach(ifp);
1180 callout_drain(&sc->vge_watchdog);
1183 device_delete_child(dev, sc->vge_miibus);
1184 bus_generic_detach(dev);
1186 if (sc->vge_intrhand)
1187 bus_teardown_intr(dev, sc->vge_irq, sc->vge_intrhand);
1189 bus_release_resource(dev, SYS_RES_IRQ,
1190 sc->vge_flags & VGE_FLAG_MSI ? 1 : 0, sc->vge_irq);
1191 if (sc->vge_flags & VGE_FLAG_MSI)
1192 pci_release_msi(dev);
1194 bus_release_resource(dev, SYS_RES_MEMORY,
1195 PCIR_BAR(1), sc->vge_res);
1200 mtx_destroy(&sc->vge_mtx);
1206 vge_discard_rxbuf(struct vge_softc *sc, int prod)
1208 struct vge_rxdesc *rxd;
1211 rxd = &sc->vge_cdata.vge_rxdesc[prod];
1212 rxd->rx_desc->vge_sts = 0;
1213 rxd->rx_desc->vge_ctl = 0;
1216 * Note: the manual fails to document the fact that for
1217 * proper opration, the driver needs to replentish the RX
1218 * DMA ring 4 descriptors at a time (rather than one at a
1219 * time, like most chips). We can allocate the new buffers
1220 * but we should not set the OWN bits until we're ready
1221 * to hand back 4 of them in one shot.
1223 if ((prod % VGE_RXCHUNK) == (VGE_RXCHUNK - 1)) {
1224 for (i = VGE_RXCHUNK; i > 0; i--) {
1225 rxd->rx_desc->vge_sts = htole32(VGE_RDSTS_OWN);
1226 rxd = rxd->rxd_prev;
1228 sc->vge_cdata.vge_rx_commit += VGE_RXCHUNK;
1233 vge_newbuf(struct vge_softc *sc, int prod)
1235 struct vge_rxdesc *rxd;
1237 bus_dma_segment_t segs[1];
1241 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1245 * This is part of an evil trick to deal with strict-alignment
1246 * architectures. The VIA chip requires RX buffers to be aligned
1247 * on 32-bit boundaries, but that will hose strict-alignment
1248 * architectures. To get around this, we leave some empty space
1249 * at the start of each buffer and for non-strict-alignment hosts,
1250 * we copy the buffer back two bytes to achieve word alignment.
1251 * This is slightly more efficient than allocating a new buffer,
1252 * copying the contents, and discarding the old buffer.
1254 m->m_len = m->m_pkthdr.len = MCLBYTES;
1255 m_adj(m, VGE_RX_BUF_ALIGN);
1257 if (bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_rx_tag,
1258 sc->vge_cdata.vge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1262 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1264 rxd = &sc->vge_cdata.vge_rxdesc[prod];
1265 if (rxd->rx_m != NULL) {
1266 bus_dmamap_sync(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap,
1267 BUS_DMASYNC_POSTREAD);
1268 bus_dmamap_unload(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap);
1270 map = rxd->rx_dmamap;
1271 rxd->rx_dmamap = sc->vge_cdata.vge_rx_sparemap;
1272 sc->vge_cdata.vge_rx_sparemap = map;
1273 bus_dmamap_sync(sc->vge_cdata.vge_rx_tag, rxd->rx_dmamap,
1274 BUS_DMASYNC_PREREAD);
1277 rxd->rx_desc->vge_sts = 0;
1278 rxd->rx_desc->vge_ctl = 0;
1279 rxd->rx_desc->vge_addrlo = htole32(VGE_ADDR_LO(segs[0].ds_addr));
1280 rxd->rx_desc->vge_addrhi = htole32(VGE_ADDR_HI(segs[0].ds_addr) |
1281 (VGE_BUFLEN(segs[0].ds_len) << 16) | VGE_RXDESC_I);
1284 * Note: the manual fails to document the fact that for
1285 * proper operation, the driver needs to replenish the RX
1286 * DMA ring 4 descriptors at a time (rather than one at a
1287 * time, like most chips). We can allocate the new buffers
1288 * but we should not set the OWN bits until we're ready
1289 * to hand back 4 of them in one shot.
1291 if ((prod % VGE_RXCHUNK) == (VGE_RXCHUNK - 1)) {
1292 for (i = VGE_RXCHUNK; i > 0; i--) {
1293 rxd->rx_desc->vge_sts = htole32(VGE_RDSTS_OWN);
1294 rxd = rxd->rxd_prev;
1296 sc->vge_cdata.vge_rx_commit += VGE_RXCHUNK;
1303 vge_tx_list_init(struct vge_softc *sc)
1305 struct vge_ring_data *rd;
1306 struct vge_txdesc *txd;
1309 VGE_LOCK_ASSERT(sc);
1311 sc->vge_cdata.vge_tx_prodidx = 0;
1312 sc->vge_cdata.vge_tx_considx = 0;
1313 sc->vge_cdata.vge_tx_cnt = 0;
1315 rd = &sc->vge_rdata;
1316 bzero(rd->vge_tx_ring, VGE_TX_LIST_SZ);
1317 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
1318 txd = &sc->vge_cdata.vge_txdesc[i];
1320 txd->tx_desc = &rd->vge_tx_ring[i];
1323 bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1324 sc->vge_cdata.vge_tx_ring_map,
1325 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1331 vge_rx_list_init(struct vge_softc *sc)
1333 struct vge_ring_data *rd;
1334 struct vge_rxdesc *rxd;
1337 VGE_LOCK_ASSERT(sc);
1339 sc->vge_cdata.vge_rx_prodidx = 0;
1340 sc->vge_cdata.vge_head = NULL;
1341 sc->vge_cdata.vge_tail = NULL;
1342 sc->vge_cdata.vge_rx_commit = 0;
1344 rd = &sc->vge_rdata;
1345 bzero(rd->vge_rx_ring, VGE_RX_LIST_SZ);
1346 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1347 rxd = &sc->vge_cdata.vge_rxdesc[i];
1349 rxd->rx_desc = &rd->vge_rx_ring[i];
1352 &sc->vge_cdata.vge_rxdesc[VGE_RX_DESC_CNT - 1];
1354 rxd->rxd_prev = &sc->vge_cdata.vge_rxdesc[i - 1];
1355 if (vge_newbuf(sc, i) != 0)
1359 bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1360 sc->vge_cdata.vge_rx_ring_map,
1361 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1363 sc->vge_cdata.vge_rx_commit = 0;
1369 vge_freebufs(struct vge_softc *sc)
1371 struct vge_txdesc *txd;
1372 struct vge_rxdesc *rxd;
1376 VGE_LOCK_ASSERT(sc);
1380 * Free RX and TX mbufs still in the queues.
1382 for (i = 0; i < VGE_RX_DESC_CNT; i++) {
1383 rxd = &sc->vge_cdata.vge_rxdesc[i];
1384 if (rxd->rx_m != NULL) {
1385 bus_dmamap_sync(sc->vge_cdata.vge_rx_tag,
1386 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
1387 bus_dmamap_unload(sc->vge_cdata.vge_rx_tag,
1394 for (i = 0; i < VGE_TX_DESC_CNT; i++) {
1395 txd = &sc->vge_cdata.vge_txdesc[i];
1396 if (txd->tx_m != NULL) {
1397 bus_dmamap_sync(sc->vge_cdata.vge_tx_tag,
1398 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1399 bus_dmamap_unload(sc->vge_cdata.vge_tx_tag,
1403 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1408 #ifndef __NO_STRICT_ALIGNMENT
1409 static __inline void
1410 vge_fixup_rx(struct mbuf *m)
1413 uint16_t *src, *dst;
1415 src = mtod(m, uint16_t *);
1418 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1421 m->m_data -= ETHER_ALIGN;
1426 * RX handler. We support the reception of jumbo frames that have
1427 * been fragmented across multiple 2K mbuf cluster buffers.
1430 vge_rxeof(struct vge_softc *sc, int count)
1434 int prod, prog, total_len;
1435 struct vge_rxdesc *rxd;
1436 struct vge_rx_desc *cur_rx;
1437 uint32_t rxstat, rxctl;
1439 VGE_LOCK_ASSERT(sc);
1443 bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1444 sc->vge_cdata.vge_rx_ring_map,
1445 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1447 prod = sc->vge_cdata.vge_rx_prodidx;
1448 for (prog = 0; count > 0 &&
1449 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1450 VGE_RX_DESC_INC(prod)) {
1451 cur_rx = &sc->vge_rdata.vge_rx_ring[prod];
1452 rxstat = le32toh(cur_rx->vge_sts);
1453 if ((rxstat & VGE_RDSTS_OWN) != 0)
1457 rxctl = le32toh(cur_rx->vge_ctl);
1458 total_len = VGE_RXBYTES(rxstat);
1459 rxd = &sc->vge_cdata.vge_rxdesc[prod];
1463 * If the 'start of frame' bit is set, this indicates
1464 * either the first fragment in a multi-fragment receive,
1465 * or an intermediate fragment. Either way, we want to
1466 * accumulate the buffers.
1468 if ((rxstat & VGE_RXPKT_SOF) != 0) {
1469 if (vge_newbuf(sc, prod) != 0) {
1470 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1471 VGE_CHAIN_RESET(sc);
1472 vge_discard_rxbuf(sc, prod);
1475 m->m_len = MCLBYTES - VGE_RX_BUF_ALIGN;
1476 if (sc->vge_cdata.vge_head == NULL) {
1477 sc->vge_cdata.vge_head = m;
1478 sc->vge_cdata.vge_tail = m;
1480 m->m_flags &= ~M_PKTHDR;
1481 sc->vge_cdata.vge_tail->m_next = m;
1482 sc->vge_cdata.vge_tail = m;
1488 * Bad/error frames will have the RXOK bit cleared.
1489 * However, there's one error case we want to allow:
1490 * if a VLAN tagged frame arrives and the chip can't
1491 * match it against the CAM filter, it considers this
1492 * a 'VLAN CAM filter miss' and clears the 'RXOK' bit.
1493 * We don't want to drop the frame though: our VLAN
1494 * filtering is done in software.
1495 * We also want to receive bad-checksummed frames and
1496 * and frames with bad-length.
1498 if ((rxstat & VGE_RDSTS_RXOK) == 0 &&
1499 (rxstat & (VGE_RDSTS_VIDM | VGE_RDSTS_RLERR |
1500 VGE_RDSTS_CSUMERR)) == 0) {
1501 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1503 * If this is part of a multi-fragment packet,
1504 * discard all the pieces.
1506 VGE_CHAIN_RESET(sc);
1507 vge_discard_rxbuf(sc, prod);
1511 if (vge_newbuf(sc, prod) != 0) {
1512 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1513 VGE_CHAIN_RESET(sc);
1514 vge_discard_rxbuf(sc, prod);
1518 /* Chain received mbufs. */
1519 if (sc->vge_cdata.vge_head != NULL) {
1520 m->m_len = total_len % (MCLBYTES - VGE_RX_BUF_ALIGN);
1522 * Special case: if there's 4 bytes or less
1523 * in this buffer, the mbuf can be discarded:
1524 * the last 4 bytes is the CRC, which we don't
1525 * care about anyway.
1527 if (m->m_len <= ETHER_CRC_LEN) {
1528 sc->vge_cdata.vge_tail->m_len -=
1529 (ETHER_CRC_LEN - m->m_len);
1532 m->m_len -= ETHER_CRC_LEN;
1533 m->m_flags &= ~M_PKTHDR;
1534 sc->vge_cdata.vge_tail->m_next = m;
1536 m = sc->vge_cdata.vge_head;
1537 m->m_flags |= M_PKTHDR;
1538 m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1540 m->m_flags |= M_PKTHDR;
1541 m->m_pkthdr.len = m->m_len =
1542 (total_len - ETHER_CRC_LEN);
1545 #ifndef __NO_STRICT_ALIGNMENT
1548 m->m_pkthdr.rcvif = ifp;
1550 /* Do RX checksumming if enabled */
1551 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0 &&
1552 (rxctl & VGE_RDCTL_FRAG) == 0) {
1553 /* Check IP header checksum */
1554 if ((rxctl & VGE_RDCTL_IPPKT) != 0)
1555 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1556 if ((rxctl & VGE_RDCTL_IPCSUMOK) != 0)
1557 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1559 /* Check TCP/UDP checksum */
1560 if (rxctl & (VGE_RDCTL_TCPPKT | VGE_RDCTL_UDPPKT) &&
1561 rxctl & VGE_RDCTL_PROTOCSUMOK) {
1562 m->m_pkthdr.csum_flags |=
1563 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1564 m->m_pkthdr.csum_data = 0xffff;
1568 if ((rxstat & VGE_RDSTS_VTAG) != 0) {
1570 * The 32-bit rxctl register is stored in little-endian.
1571 * However, the 16-bit vlan tag is stored in big-endian,
1572 * so we have to byte swap it.
1574 m->m_pkthdr.ether_vtag =
1575 bswap16(rxctl & VGE_RDCTL_VLANID);
1576 m->m_flags |= M_VLANTAG;
1580 (*ifp->if_input)(ifp, m);
1582 sc->vge_cdata.vge_head = NULL;
1583 sc->vge_cdata.vge_tail = NULL;
1587 sc->vge_cdata.vge_rx_prodidx = prod;
1588 bus_dmamap_sync(sc->vge_cdata.vge_rx_ring_tag,
1589 sc->vge_cdata.vge_rx_ring_map,
1590 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1591 /* Update residue counter. */
1592 if (sc->vge_cdata.vge_rx_commit != 0) {
1593 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT,
1594 sc->vge_cdata.vge_rx_commit);
1595 sc->vge_cdata.vge_rx_commit = 0;
1602 vge_txeof(struct vge_softc *sc)
1605 struct vge_tx_desc *cur_tx;
1606 struct vge_txdesc *txd;
1610 VGE_LOCK_ASSERT(sc);
1614 if (sc->vge_cdata.vge_tx_cnt == 0)
1617 bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1618 sc->vge_cdata.vge_tx_ring_map,
1619 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1622 * Go through our tx list and free mbufs for those
1623 * frames that have been transmitted.
1625 cons = sc->vge_cdata.vge_tx_considx;
1626 prod = sc->vge_cdata.vge_tx_prodidx;
1627 for (; cons != prod; VGE_TX_DESC_INC(cons)) {
1628 cur_tx = &sc->vge_rdata.vge_tx_ring[cons];
1629 txstat = le32toh(cur_tx->vge_sts);
1630 if ((txstat & VGE_TDSTS_OWN) != 0)
1632 sc->vge_cdata.vge_tx_cnt--;
1633 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1635 txd = &sc->vge_cdata.vge_txdesc[cons];
1636 bus_dmamap_sync(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap,
1637 BUS_DMASYNC_POSTWRITE);
1638 bus_dmamap_unload(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap);
1640 KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n",
1644 txd->tx_desc->vge_frag[0].vge_addrhi = 0;
1646 bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1647 sc->vge_cdata.vge_tx_ring_map,
1648 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1649 sc->vge_cdata.vge_tx_considx = cons;
1650 if (sc->vge_cdata.vge_tx_cnt == 0)
1655 vge_link_statchg(void *xsc)
1657 struct vge_softc *sc;
1663 VGE_LOCK_ASSERT(sc);
1665 physts = CSR_READ_1(sc, VGE_PHYSTS0);
1666 if ((physts & VGE_PHYSTS_RESETSTS) == 0) {
1667 if ((physts & VGE_PHYSTS_LINK) == 0) {
1668 sc->vge_flags &= ~VGE_FLAG_LINK;
1669 if_link_state_change(sc->vge_ifp,
1672 sc->vge_flags |= VGE_FLAG_LINK;
1673 if_link_state_change(sc->vge_ifp,
1675 CSR_WRITE_1(sc, VGE_CRC2, VGE_CR2_FDX_TXFLOWCTL_ENABLE |
1676 VGE_CR2_FDX_RXFLOWCTL_ENABLE);
1677 if ((physts & VGE_PHYSTS_FDX) != 0) {
1678 if ((physts & VGE_PHYSTS_TXFLOWCAP) != 0)
1679 CSR_WRITE_1(sc, VGE_CRS2,
1680 VGE_CR2_FDX_TXFLOWCTL_ENABLE);
1681 if ((physts & VGE_PHYSTS_RXFLOWCAP) != 0)
1682 CSR_WRITE_1(sc, VGE_CRS2,
1683 VGE_CR2_FDX_RXFLOWCTL_ENABLE);
1685 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1686 vge_start_locked(ifp);
1690 * Restart MII auto-polling because link state change interrupt
1693 vge_miipoll_start(sc);
1696 #ifdef DEVICE_POLLING
1698 vge_poll (struct ifnet *ifp, enum poll_cmd cmd, int count)
1700 struct vge_softc *sc = ifp->if_softc;
1704 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1707 rx_npkts = vge_rxeof(sc, count);
1710 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1711 vge_start_locked(ifp);
1713 if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1715 status = CSR_READ_4(sc, VGE_ISR);
1716 if (status == 0xFFFFFFFF)
1719 CSR_WRITE_4(sc, VGE_ISR, status);
1722 * XXX check behaviour on receiver stalls.
1725 if (status & VGE_ISR_TXDMA_STALL ||
1726 status & VGE_ISR_RXDMA_STALL) {
1727 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1728 vge_init_locked(sc);
1731 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1732 vge_rxeof(sc, count);
1733 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1734 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1741 #endif /* DEVICE_POLLING */
1746 struct vge_softc *sc;
1754 if ((sc->vge_flags & VGE_FLAG_SUSPENDED) != 0 ||
1755 (ifp->if_flags & IFF_UP) == 0) {
1760 #ifdef DEVICE_POLLING
1761 if (ifp->if_capenable & IFCAP_POLLING) {
1762 status = CSR_READ_4(sc, VGE_ISR);
1763 CSR_WRITE_4(sc, VGE_ISR, status);
1764 if (status != 0xFFFFFFFF && (status & VGE_ISR_LINKSTS) != 0)
1765 vge_link_statchg(sc);
1771 /* Disable interrupts */
1772 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
1773 status = CSR_READ_4(sc, VGE_ISR);
1774 CSR_WRITE_4(sc, VGE_ISR, status | VGE_ISR_HOLDOFF_RELOAD);
1775 /* If the card has gone away the read returns 0xffff. */
1776 if (status == 0xFFFFFFFF || (status & VGE_INTRS) == 0)
1778 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1779 if (status & (VGE_ISR_RXOK|VGE_ISR_RXOK_HIPRIO))
1780 vge_rxeof(sc, VGE_RX_DESC_CNT);
1781 if (status & (VGE_ISR_RXOFLOW|VGE_ISR_RXNODESC)) {
1782 vge_rxeof(sc, VGE_RX_DESC_CNT);
1783 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
1784 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
1787 if (status & (VGE_ISR_TXOK0|VGE_ISR_TXOK_HIPRIO))
1790 if (status & (VGE_ISR_TXDMA_STALL|VGE_ISR_RXDMA_STALL)) {
1791 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1792 vge_init_locked(sc);
1795 if (status & VGE_ISR_LINKSTS)
1796 vge_link_statchg(sc);
1799 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1800 /* Re-enable interrupts */
1801 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
1803 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1804 vge_start_locked(ifp);
1810 vge_encap(struct vge_softc *sc, struct mbuf **m_head)
1812 struct vge_txdesc *txd;
1813 struct vge_tx_frag *frag;
1815 bus_dma_segment_t txsegs[VGE_MAXTXSEGS];
1816 int error, i, nsegs, padlen;
1819 VGE_LOCK_ASSERT(sc);
1821 M_ASSERTPKTHDR((*m_head));
1823 /* Argh. This chip does not autopad short frames. */
1824 if ((*m_head)->m_pkthdr.len < VGE_MIN_FRAMELEN) {
1826 padlen = VGE_MIN_FRAMELEN - m->m_pkthdr.len;
1827 if (M_WRITABLE(m) == 0) {
1828 /* Get a writable copy. */
1829 m = m_dup(*m_head, M_NOWAIT);
1837 if (M_TRAILINGSPACE(m) < padlen) {
1838 m = m_defrag(m, M_NOWAIT);
1846 * Manually pad short frames, and zero the pad space
1847 * to avoid leaking data.
1849 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1850 m->m_pkthdr.len += padlen;
1851 m->m_len = m->m_pkthdr.len;
1855 txd = &sc->vge_cdata.vge_txdesc[sc->vge_cdata.vge_tx_prodidx];
1857 error = bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_tx_tag,
1858 txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1859 if (error == EFBIG) {
1860 m = m_collapse(*m_head, M_NOWAIT, VGE_MAXTXSEGS);
1867 error = bus_dmamap_load_mbuf_sg(sc->vge_cdata.vge_tx_tag,
1868 txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1874 } else if (error != 0)
1876 bus_dmamap_sync(sc->vge_cdata.vge_tx_tag, txd->tx_dmamap,
1877 BUS_DMASYNC_PREWRITE);
1882 /* Configure checksum offload. */
1883 if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
1884 cflags |= VGE_TDCTL_IPCSUM;
1885 if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1886 cflags |= VGE_TDCTL_TCPCSUM;
1887 if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1888 cflags |= VGE_TDCTL_UDPCSUM;
1890 /* Configure VLAN. */
1891 if ((m->m_flags & M_VLANTAG) != 0)
1892 cflags |= m->m_pkthdr.ether_vtag | VGE_TDCTL_VTAG;
1893 txd->tx_desc->vge_sts = htole32(m->m_pkthdr.len << 16);
1896 * Velocity family seems to support TSO but no information
1897 * for MSS configuration is available. Also the number of
1898 * fragments supported by a descriptor is too small to hold
1899 * entire 64KB TCP/IP segment. Maybe VGE_TD_LS_MOF,
1900 * VGE_TD_LS_SOF and VGE_TD_LS_EOF could be used to build
1901 * longer chain of buffers but no additional information is
1904 * When telling the chip how many segments there are, we
1905 * must use nsegs + 1 instead of just nsegs. Darned if I
1906 * know why. This also means we can't use the last fragment
1907 * field of Tx descriptor.
1909 txd->tx_desc->vge_ctl = htole32(cflags | ((nsegs + 1) << 28) |
1911 for (i = 0; i < nsegs; i++) {
1912 frag = &txd->tx_desc->vge_frag[i];
1913 frag->vge_addrlo = htole32(VGE_ADDR_LO(txsegs[i].ds_addr));
1914 frag->vge_addrhi = htole32(VGE_ADDR_HI(txsegs[i].ds_addr) |
1915 (VGE_BUFLEN(txsegs[i].ds_len) << 16));
1918 sc->vge_cdata.vge_tx_cnt++;
1919 VGE_TX_DESC_INC(sc->vge_cdata.vge_tx_prodidx);
1922 * Finally request interrupt and give the first descriptor
1923 * ownership to hardware.
1925 txd->tx_desc->vge_ctl |= htole32(VGE_TDCTL_TIC);
1926 txd->tx_desc->vge_sts |= htole32(VGE_TDSTS_OWN);
1933 * Main transmit routine.
1937 vge_start(struct ifnet *ifp)
1939 struct vge_softc *sc;
1943 vge_start_locked(ifp);
1949 vge_start_locked(struct ifnet *ifp)
1951 struct vge_softc *sc;
1952 struct vge_txdesc *txd;
1953 struct mbuf *m_head;
1958 VGE_LOCK_ASSERT(sc);
1960 if ((sc->vge_flags & VGE_FLAG_LINK) == 0 ||
1961 (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1965 idx = sc->vge_cdata.vge_tx_prodidx;
1966 VGE_TX_DESC_DEC(idx);
1967 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
1968 sc->vge_cdata.vge_tx_cnt < VGE_TX_DESC_CNT - 1; ) {
1969 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1973 * Pack the data into the transmit ring. If we
1974 * don't have room, set the OACTIVE flag and wait
1975 * for the NIC to drain the ring.
1977 if (vge_encap(sc, &m_head)) {
1980 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1981 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1985 txd = &sc->vge_cdata.vge_txdesc[idx];
1986 txd->tx_desc->vge_frag[0].vge_addrhi |= htole32(VGE_TXDESC_Q);
1987 VGE_TX_DESC_INC(idx);
1991 * If there's a BPF listener, bounce a copy of this frame
1994 ETHER_BPF_MTAP(ifp, m_head);
1998 bus_dmamap_sync(sc->vge_cdata.vge_tx_ring_tag,
1999 sc->vge_cdata.vge_tx_ring_map,
2000 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2001 /* Issue a transmit command. */
2002 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_WAK0);
2004 * Set a timeout in case the chip goes out to lunch.
2013 struct vge_softc *sc = xsc;
2016 vge_init_locked(sc);
2021 vge_init_locked(struct vge_softc *sc)
2023 struct ifnet *ifp = sc->vge_ifp;
2026 VGE_LOCK_ASSERT(sc);
2028 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2032 * Cancel pending I/O and free all RX/TX buffers.
2036 vge_miipoll_start(sc);
2039 * Initialize the RX and TX descriptors and mbufs.
2042 error = vge_rx_list_init(sc);
2044 device_printf(sc->vge_dev, "no memory for Rx buffers.\n");
2047 vge_tx_list_init(sc);
2048 /* Clear MAC statistics. */
2049 vge_stats_clear(sc);
2050 /* Set our station address */
2051 for (i = 0; i < ETHER_ADDR_LEN; i++)
2052 CSR_WRITE_1(sc, VGE_PAR0 + i, IF_LLADDR(sc->vge_ifp)[i]);
2055 * Set receive FIFO threshold. Also allow transmission and
2056 * reception of VLAN tagged frames.
2058 CSR_CLRBIT_1(sc, VGE_RXCFG, VGE_RXCFG_FIFO_THR|VGE_RXCFG_VTAGOPT);
2059 CSR_SETBIT_1(sc, VGE_RXCFG, VGE_RXFIFOTHR_128BYTES);
2061 /* Set DMA burst length */
2062 CSR_CLRBIT_1(sc, VGE_DMACFG0, VGE_DMACFG0_BURSTLEN);
2063 CSR_SETBIT_1(sc, VGE_DMACFG0, VGE_DMABURST_128);
2065 CSR_SETBIT_1(sc, VGE_TXCFG, VGE_TXCFG_ARB_PRIO|VGE_TXCFG_NONBLK);
2067 /* Set collision backoff algorithm */
2068 CSR_CLRBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_CRANDOM|
2069 VGE_CHIPCFG1_CAP|VGE_CHIPCFG1_MBA|VGE_CHIPCFG1_BAKOPT);
2070 CSR_SETBIT_1(sc, VGE_CHIPCFG1, VGE_CHIPCFG1_OFSET);
2072 /* Disable LPSEL field in priority resolution */
2073 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_LPSEL_DIS);
2076 * Load the addresses of the DMA queues into the chip.
2077 * Note that we only use one transmit queue.
2080 CSR_WRITE_4(sc, VGE_TXDESC_HIADDR,
2081 VGE_ADDR_HI(sc->vge_rdata.vge_tx_ring_paddr));
2082 CSR_WRITE_4(sc, VGE_TXDESC_ADDR_LO0,
2083 VGE_ADDR_LO(sc->vge_rdata.vge_tx_ring_paddr));
2084 CSR_WRITE_2(sc, VGE_TXDESCNUM, VGE_TX_DESC_CNT - 1);
2086 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO,
2087 VGE_ADDR_LO(sc->vge_rdata.vge_rx_ring_paddr));
2088 CSR_WRITE_2(sc, VGE_RXDESCNUM, VGE_RX_DESC_CNT - 1);
2089 CSR_WRITE_2(sc, VGE_RXDESC_RESIDUECNT, VGE_RX_DESC_CNT);
2091 /* Configure interrupt moderation. */
2092 vge_intr_holdoff(sc);
2094 /* Enable and wake up the RX descriptor queue */
2095 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_RUN);
2096 CSR_WRITE_1(sc, VGE_RXQCSRS, VGE_RXQCSR_WAK);
2098 /* Enable the TX descriptor queue */
2099 CSR_WRITE_2(sc, VGE_TXQCSRS, VGE_TXQCSR_RUN0);
2101 /* Init the cam filter. */
2104 /* Set up receiver filter. */
2108 /* Initialize pause timer. */
2109 CSR_WRITE_2(sc, VGE_TX_PAUSE_TIMER, 0xFFFF);
2111 * Initialize flow control parameters.
2112 * TX XON high threshold : 48
2113 * TX pause low threshold : 24
2114 * Disable hald-duplex flow control
2116 CSR_WRITE_1(sc, VGE_CRC2, 0xFF);
2117 CSR_WRITE_1(sc, VGE_CRS2, VGE_CR2_XON_ENABLE | 0x0B);
2119 /* Enable jumbo frame reception (if desired) */
2121 /* Start the MAC. */
2122 CSR_WRITE_1(sc, VGE_CRC0, VGE_CR0_STOP);
2123 CSR_WRITE_1(sc, VGE_CRS1, VGE_CR1_NOPOLL);
2124 CSR_WRITE_1(sc, VGE_CRS0,
2125 VGE_CR0_TX_ENABLE|VGE_CR0_RX_ENABLE|VGE_CR0_START);
2127 #ifdef DEVICE_POLLING
2129 * Disable interrupts except link state change if we are polling.
2131 if (ifp->if_capenable & IFCAP_POLLING) {
2132 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS_POLLING);
2133 } else /* otherwise ... */
2137 * Enable interrupts.
2139 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
2141 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2142 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2144 sc->vge_flags &= ~VGE_FLAG_LINK;
2145 vge_ifmedia_upd_locked(sc);
2147 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2148 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2149 callout_reset(&sc->vge_watchdog, hz, vge_watchdog, sc);
2153 * Set media options.
2156 vge_ifmedia_upd(struct ifnet *ifp)
2158 struct vge_softc *sc;
2163 error = vge_ifmedia_upd_locked(sc);
2170 vge_ifmedia_upd_locked(struct vge_softc *sc)
2172 struct mii_data *mii;
2173 struct mii_softc *miisc;
2176 mii = device_get_softc(sc->vge_miibus);
2177 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
2180 error = mii_mediachg(mii);
2186 * Report current media status.
2189 vge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2191 struct vge_softc *sc;
2192 struct mii_data *mii;
2195 mii = device_get_softc(sc->vge_miibus);
2198 if ((ifp->if_flags & IFF_UP) == 0) {
2203 ifmr->ifm_active = mii->mii_media_active;
2204 ifmr->ifm_status = mii->mii_media_status;
2209 vge_setmedia(struct vge_softc *sc)
2211 struct mii_data *mii;
2212 struct ifmedia_entry *ife;
2214 mii = device_get_softc(sc->vge_miibus);
2215 ife = mii->mii_media.ifm_cur;
2218 * If the user manually selects a media mode, we need to turn
2219 * on the forced MAC mode bit in the DIAGCTL register. If the
2220 * user happens to choose a full duplex mode, we also need to
2221 * set the 'force full duplex' bit. This applies only to
2222 * 10Mbps and 100Mbps speeds. In autoselect mode, forced MAC
2223 * mode is disabled, and in 1000baseT mode, full duplex is
2224 * always implied, so we turn on the forced mode bit but leave
2225 * the FDX bit cleared.
2228 switch (IFM_SUBTYPE(ife->ifm_media)) {
2230 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2231 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2234 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2235 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2239 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2240 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
2241 CSR_SETBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2243 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2247 device_printf(sc->vge_dev, "unknown media type: %x\n",
2248 IFM_SUBTYPE(ife->ifm_media));
2254 vge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2256 struct vge_softc *sc = ifp->if_softc;
2257 struct ifreq *ifr = (struct ifreq *) data;
2258 struct mii_data *mii;
2259 int error = 0, mask;
2264 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > VGE_JUMBO_MTU)
2266 else if (ifp->if_mtu != ifr->ifr_mtu) {
2267 if (ifr->ifr_mtu > ETHERMTU &&
2268 (sc->vge_flags & VGE_FLAG_JUMBO) == 0)
2271 ifp->if_mtu = ifr->ifr_mtu;
2277 if ((ifp->if_flags & IFF_UP) != 0) {
2278 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2279 ((ifp->if_flags ^ sc->vge_if_flags) &
2280 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2283 vge_init_locked(sc);
2284 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2286 sc->vge_if_flags = ifp->if_flags;
2292 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2298 mii = device_get_softc(sc->vge_miibus);
2299 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2302 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2303 #ifdef DEVICE_POLLING
2304 if (mask & IFCAP_POLLING) {
2305 if (ifr->ifr_reqcap & IFCAP_POLLING) {
2306 error = ether_poll_register(vge_poll, ifp);
2310 /* Disable interrupts */
2311 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS_POLLING);
2312 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2313 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2314 ifp->if_capenable |= IFCAP_POLLING;
2317 error = ether_poll_deregister(ifp);
2318 /* Enable interrupts. */
2320 CSR_WRITE_4(sc, VGE_IMR, VGE_INTRS);
2321 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2322 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_GMSK);
2323 ifp->if_capenable &= ~IFCAP_POLLING;
2327 #endif /* DEVICE_POLLING */
2329 if ((mask & IFCAP_TXCSUM) != 0 &&
2330 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
2331 ifp->if_capenable ^= IFCAP_TXCSUM;
2332 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
2333 ifp->if_hwassist |= VGE_CSUM_FEATURES;
2335 ifp->if_hwassist &= ~VGE_CSUM_FEATURES;
2337 if ((mask & IFCAP_RXCSUM) != 0 &&
2338 (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
2339 ifp->if_capenable ^= IFCAP_RXCSUM;
2340 if ((mask & IFCAP_WOL_UCAST) != 0 &&
2341 (ifp->if_capabilities & IFCAP_WOL_UCAST) != 0)
2342 ifp->if_capenable ^= IFCAP_WOL_UCAST;
2343 if ((mask & IFCAP_WOL_MCAST) != 0 &&
2344 (ifp->if_capabilities & IFCAP_WOL_MCAST) != 0)
2345 ifp->if_capenable ^= IFCAP_WOL_MCAST;
2346 if ((mask & IFCAP_WOL_MAGIC) != 0 &&
2347 (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
2348 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2349 if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2350 (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2351 ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2352 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2353 (IFCAP_VLAN_HWTAGGING & ifp->if_capabilities) != 0) {
2354 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2358 VLAN_CAPABILITIES(ifp);
2361 error = ether_ioctl(ifp, command, data);
2369 vge_watchdog(void *arg)
2371 struct vge_softc *sc;
2375 VGE_LOCK_ASSERT(sc);
2376 vge_stats_update(sc);
2377 callout_reset(&sc->vge_watchdog, hz, vge_watchdog, sc);
2378 if (sc->vge_timer == 0 || --sc->vge_timer > 0)
2382 if_printf(ifp, "watchdog timeout\n");
2383 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2386 vge_rxeof(sc, VGE_RX_DESC_CNT);
2388 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2389 vge_init_locked(sc);
2393 * Stop the adapter and free any mbufs allocated to the
2397 vge_stop(struct vge_softc *sc)
2401 VGE_LOCK_ASSERT(sc);
2404 callout_stop(&sc->vge_watchdog);
2406 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2408 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_GMSK);
2409 CSR_WRITE_1(sc, VGE_CRS0, VGE_CR0_STOP);
2410 CSR_WRITE_4(sc, VGE_ISR, 0xFFFFFFFF);
2411 CSR_WRITE_2(sc, VGE_TXQCSRC, 0xFFFF);
2412 CSR_WRITE_1(sc, VGE_RXQCSRC, 0xFF);
2413 CSR_WRITE_4(sc, VGE_RXDESC_ADDR_LO, 0);
2415 vge_stats_update(sc);
2416 VGE_CHAIN_RESET(sc);
2422 * Device suspend routine. Stop the interface and save some PCI
2423 * settings in case the BIOS doesn't restore them properly on
2427 vge_suspend(device_t dev)
2429 struct vge_softc *sc;
2431 sc = device_get_softc(dev);
2436 sc->vge_flags |= VGE_FLAG_SUSPENDED;
2443 * Device resume routine. Restore some PCI settings in case the BIOS
2444 * doesn't, re-enable busmastering, and restart the interface if
2448 vge_resume(device_t dev)
2450 struct vge_softc *sc;
2454 sc = device_get_softc(dev);
2456 if ((sc->vge_flags & VGE_FLAG_PMCAP) != 0) {
2457 /* Disable PME and clear PME status. */
2458 pmstat = pci_read_config(sc->vge_dev,
2459 sc->vge_pmcap + PCIR_POWER_STATUS, 2);
2460 if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2461 pmstat &= ~PCIM_PSTAT_PMEENABLE;
2462 pci_write_config(sc->vge_dev,
2463 sc->vge_pmcap + PCIR_POWER_STATUS, pmstat, 2);
2467 /* Restart MII auto-polling. */
2468 vge_miipoll_start(sc);
2470 /* Reinitialize interface if necessary. */
2471 if ((ifp->if_flags & IFF_UP) != 0) {
2472 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2473 vge_init_locked(sc);
2475 sc->vge_flags &= ~VGE_FLAG_SUSPENDED;
2482 * Stop all chip I/O so that the kernel's probe routines don't
2483 * get confused by errant DMAs when rebooting.
2486 vge_shutdown(device_t dev)
2489 return (vge_suspend(dev));
2492 #define VGE_SYSCTL_STAT_ADD32(c, h, n, p, d) \
2493 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2496 vge_sysctl_node(struct vge_softc *sc)
2498 struct sysctl_ctx_list *ctx;
2499 struct sysctl_oid_list *child, *parent;
2500 struct sysctl_oid *tree;
2501 struct vge_hw_stats *stats;
2503 stats = &sc->vge_stats;
2504 ctx = device_get_sysctl_ctx(sc->vge_dev);
2505 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->vge_dev));
2507 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "int_holdoff",
2508 CTLFLAG_RW, &sc->vge_int_holdoff, 0, "interrupt holdoff");
2509 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "rx_coal_pkt",
2510 CTLFLAG_RW, &sc->vge_rx_coal_pkt, 0, "rx coalescing packet");
2511 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "tx_coal_pkt",
2512 CTLFLAG_RW, &sc->vge_tx_coal_pkt, 0, "tx coalescing packet");
2514 /* Pull in device tunables. */
2515 sc->vge_int_holdoff = VGE_INT_HOLDOFF_DEFAULT;
2516 resource_int_value(device_get_name(sc->vge_dev),
2517 device_get_unit(sc->vge_dev), "int_holdoff", &sc->vge_int_holdoff);
2518 sc->vge_rx_coal_pkt = VGE_RX_COAL_PKT_DEFAULT;
2519 resource_int_value(device_get_name(sc->vge_dev),
2520 device_get_unit(sc->vge_dev), "rx_coal_pkt", &sc->vge_rx_coal_pkt);
2521 sc->vge_tx_coal_pkt = VGE_TX_COAL_PKT_DEFAULT;
2522 resource_int_value(device_get_name(sc->vge_dev),
2523 device_get_unit(sc->vge_dev), "tx_coal_pkt", &sc->vge_tx_coal_pkt);
2525 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2526 NULL, "VGE statistics");
2527 parent = SYSCTL_CHILDREN(tree);
2529 /* Rx statistics. */
2530 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
2531 NULL, "RX MAC statistics");
2532 child = SYSCTL_CHILDREN(tree);
2533 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames",
2534 &stats->rx_frames, "frames");
2535 VGE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
2536 &stats->rx_good_frames, "Good frames");
2537 VGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
2538 &stats->rx_fifo_oflows, "FIFO overflows");
2539 VGE_SYSCTL_STAT_ADD32(ctx, child, "runts",
2540 &stats->rx_runts, "Too short frames");
2541 VGE_SYSCTL_STAT_ADD32(ctx, child, "runts_errs",
2542 &stats->rx_runts_errs, "Too short frames with errors");
2543 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
2544 &stats->rx_pkts_64, "64 bytes frames");
2545 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
2546 &stats->rx_pkts_65_127, "65 to 127 bytes frames");
2547 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
2548 &stats->rx_pkts_128_255, "128 to 255 bytes frames");
2549 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
2550 &stats->rx_pkts_256_511, "256 to 511 bytes frames");
2551 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
2552 &stats->rx_pkts_512_1023, "512 to 1023 bytes frames");
2553 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
2554 &stats->rx_pkts_1024_1518, "1024 to 1518 bytes frames");
2555 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max",
2556 &stats->rx_pkts_1519_max, "1519 to max frames");
2557 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1519_max_errs",
2558 &stats->rx_pkts_1519_max_errs, "1519 to max frames with error");
2559 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_jumbo",
2560 &stats->rx_jumbos, "Jumbo frames");
2561 VGE_SYSCTL_STAT_ADD32(ctx, child, "crcerrs",
2562 &stats->rx_crcerrs, "CRC errors");
2563 VGE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
2564 &stats->rx_pause_frames, "CRC errors");
2565 VGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
2566 &stats->rx_alignerrs, "Alignment errors");
2567 VGE_SYSCTL_STAT_ADD32(ctx, child, "nobufs",
2568 &stats->rx_nobufs, "Frames with no buffer event");
2569 VGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs",
2570 &stats->rx_symerrs, "Frames with symbol errors");
2571 VGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
2572 &stats->rx_lenerrs, "Frames with length mismatched");
2574 /* Tx statistics. */
2575 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
2576 NULL, "TX MAC statistics");
2577 child = SYSCTL_CHILDREN(tree);
2578 VGE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
2579 &stats->tx_good_frames, "Good frames");
2580 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_64",
2581 &stats->tx_pkts_64, "64 bytes frames");
2582 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_65_127",
2583 &stats->tx_pkts_65_127, "65 to 127 bytes frames");
2584 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_128_255",
2585 &stats->tx_pkts_128_255, "128 to 255 bytes frames");
2586 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_256_511",
2587 &stats->tx_pkts_256_511, "256 to 511 bytes frames");
2588 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_512_1023",
2589 &stats->tx_pkts_512_1023, "512 to 1023 bytes frames");
2590 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_1024_1518",
2591 &stats->tx_pkts_1024_1518, "1024 to 1518 bytes frames");
2592 VGE_SYSCTL_STAT_ADD32(ctx, child, "frames_jumbo",
2593 &stats->tx_jumbos, "Jumbo frames");
2594 VGE_SYSCTL_STAT_ADD32(ctx, child, "colls",
2595 &stats->tx_colls, "Collisions");
2596 VGE_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
2597 &stats->tx_latecolls, "Late collisions");
2598 VGE_SYSCTL_STAT_ADD32(ctx, child, "pause_frames",
2599 &stats->tx_pause, "Pause frames");
2600 #ifdef VGE_ENABLE_SQEERR
2601 VGE_SYSCTL_STAT_ADD32(ctx, child, "sqeerrs",
2602 &stats->tx_sqeerrs, "SQE errors");
2604 /* Clear MAC statistics. */
2605 vge_stats_clear(sc);
2608 #undef VGE_SYSCTL_STAT_ADD32
2611 vge_stats_clear(struct vge_softc *sc)
2615 CSR_WRITE_1(sc, VGE_MIBCSR,
2616 CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_FREEZE);
2617 CSR_WRITE_1(sc, VGE_MIBCSR,
2618 CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_CLR);
2619 for (i = VGE_TIMEOUT; i > 0; i--) {
2621 if ((CSR_READ_1(sc, VGE_MIBCSR) & VGE_MIBCSR_CLR) == 0)
2625 device_printf(sc->vge_dev, "MIB clear timed out!\n");
2626 CSR_WRITE_1(sc, VGE_MIBCSR, CSR_READ_1(sc, VGE_MIBCSR) &
2627 ~VGE_MIBCSR_FREEZE);
2631 vge_stats_update(struct vge_softc *sc)
2633 struct vge_hw_stats *stats;
2635 uint32_t mib[VGE_MIB_CNT], val;
2638 VGE_LOCK_ASSERT(sc);
2640 stats = &sc->vge_stats;
2643 CSR_WRITE_1(sc, VGE_MIBCSR,
2644 CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_FLUSH);
2645 for (i = VGE_TIMEOUT; i > 0; i--) {
2647 if ((CSR_READ_1(sc, VGE_MIBCSR) & VGE_MIBCSR_FLUSH) == 0)
2651 device_printf(sc->vge_dev, "MIB counter dump timed out!\n");
2652 vge_stats_clear(sc);
2656 bzero(mib, sizeof(mib));
2658 /* Set MIB read index to 0. */
2659 CSR_WRITE_1(sc, VGE_MIBCSR,
2660 CSR_READ_1(sc, VGE_MIBCSR) | VGE_MIBCSR_RINI);
2661 for (i = 0; i < VGE_MIB_CNT; i++) {
2662 val = CSR_READ_4(sc, VGE_MIBDATA);
2663 if (i != VGE_MIB_DATA_IDX(val)) {
2664 /* Reading interrupted. */
2667 mib[i] = val & VGE_MIB_DATA_MASK;
2671 stats->rx_frames += mib[VGE_MIB_RX_FRAMES];
2672 stats->rx_good_frames += mib[VGE_MIB_RX_GOOD_FRAMES];
2673 stats->rx_fifo_oflows += mib[VGE_MIB_RX_FIFO_OVERRUNS];
2674 stats->rx_runts += mib[VGE_MIB_RX_RUNTS];
2675 stats->rx_runts_errs += mib[VGE_MIB_RX_RUNTS_ERRS];
2676 stats->rx_pkts_64 += mib[VGE_MIB_RX_PKTS_64];
2677 stats->rx_pkts_65_127 += mib[VGE_MIB_RX_PKTS_65_127];
2678 stats->rx_pkts_128_255 += mib[VGE_MIB_RX_PKTS_128_255];
2679 stats->rx_pkts_256_511 += mib[VGE_MIB_RX_PKTS_256_511];
2680 stats->rx_pkts_512_1023 += mib[VGE_MIB_RX_PKTS_512_1023];
2681 stats->rx_pkts_1024_1518 += mib[VGE_MIB_RX_PKTS_1024_1518];
2682 stats->rx_pkts_1519_max += mib[VGE_MIB_RX_PKTS_1519_MAX];
2683 stats->rx_pkts_1519_max_errs += mib[VGE_MIB_RX_PKTS_1519_MAX_ERRS];
2684 stats->rx_jumbos += mib[VGE_MIB_RX_JUMBOS];
2685 stats->rx_crcerrs += mib[VGE_MIB_RX_CRCERRS];
2686 stats->rx_pause_frames += mib[VGE_MIB_RX_PAUSE];
2687 stats->rx_alignerrs += mib[VGE_MIB_RX_ALIGNERRS];
2688 stats->rx_nobufs += mib[VGE_MIB_RX_NOBUFS];
2689 stats->rx_symerrs += mib[VGE_MIB_RX_SYMERRS];
2690 stats->rx_lenerrs += mib[VGE_MIB_RX_LENERRS];
2693 stats->tx_good_frames += mib[VGE_MIB_TX_GOOD_FRAMES];
2694 stats->tx_pkts_64 += mib[VGE_MIB_TX_PKTS_64];
2695 stats->tx_pkts_65_127 += mib[VGE_MIB_TX_PKTS_65_127];
2696 stats->tx_pkts_128_255 += mib[VGE_MIB_TX_PKTS_128_255];
2697 stats->tx_pkts_256_511 += mib[VGE_MIB_TX_PKTS_256_511];
2698 stats->tx_pkts_512_1023 += mib[VGE_MIB_TX_PKTS_512_1023];
2699 stats->tx_pkts_1024_1518 += mib[VGE_MIB_TX_PKTS_1024_1518];
2700 stats->tx_jumbos += mib[VGE_MIB_TX_JUMBOS];
2701 stats->tx_colls += mib[VGE_MIB_TX_COLLS];
2702 stats->tx_pause += mib[VGE_MIB_TX_PAUSE];
2703 #ifdef VGE_ENABLE_SQEERR
2704 stats->tx_sqeerrs += mib[VGE_MIB_TX_SQEERRS];
2706 stats->tx_latecolls += mib[VGE_MIB_TX_LATECOLLS];
2708 /* Update counters in ifnet. */
2709 if_inc_counter(ifp, IFCOUNTER_OPACKETS, mib[VGE_MIB_TX_GOOD_FRAMES]);
2711 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
2712 mib[VGE_MIB_TX_COLLS] + mib[VGE_MIB_TX_LATECOLLS]);
2714 if_inc_counter(ifp, IFCOUNTER_OERRORS,
2715 mib[VGE_MIB_TX_COLLS] + mib[VGE_MIB_TX_LATECOLLS]);
2717 if_inc_counter(ifp, IFCOUNTER_IPACKETS, mib[VGE_MIB_RX_GOOD_FRAMES]);
2719 if_inc_counter(ifp, IFCOUNTER_IERRORS,
2720 mib[VGE_MIB_RX_FIFO_OVERRUNS] +
2721 mib[VGE_MIB_RX_RUNTS] +
2722 mib[VGE_MIB_RX_RUNTS_ERRS] +
2723 mib[VGE_MIB_RX_CRCERRS] +
2724 mib[VGE_MIB_RX_ALIGNERRS] +
2725 mib[VGE_MIB_RX_NOBUFS] +
2726 mib[VGE_MIB_RX_SYMERRS] +
2727 mib[VGE_MIB_RX_LENERRS]);
2731 vge_intr_holdoff(struct vge_softc *sc)
2735 VGE_LOCK_ASSERT(sc);
2738 * Set Tx interrupt supression threshold.
2739 * It's possible to use single-shot timer in VGE_CRS1 register
2740 * in Tx path such that driver can remove most of Tx completion
2741 * interrupts. However this requires additional access to
2742 * VGE_CRS1 register to reload the timer in addintion to
2743 * activating Tx kick command. Another downside is we don't know
2744 * what single-shot timer value should be used in advance so
2745 * reclaiming transmitted mbufs could be delayed a lot which in
2746 * turn slows down Tx operation.
2748 CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_TXSUPPTHR);
2749 CSR_WRITE_1(sc, VGE_TXSUPPTHR, sc->vge_tx_coal_pkt);
2751 /* Set Rx interrupt suppresion threshold. */
2752 CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_RXSUPPTHR);
2753 CSR_WRITE_1(sc, VGE_RXSUPPTHR, sc->vge_rx_coal_pkt);
2755 intctl = CSR_READ_1(sc, VGE_INTCTL1);
2756 intctl &= ~VGE_INTCTL_SC_RELOAD;
2757 intctl |= VGE_INTCTL_HC_RELOAD;
2758 if (sc->vge_tx_coal_pkt <= 0)
2759 intctl |= VGE_INTCTL_TXINTSUP_DISABLE;
2761 intctl &= ~VGE_INTCTL_TXINTSUP_DISABLE;
2762 if (sc->vge_rx_coal_pkt <= 0)
2763 intctl |= VGE_INTCTL_RXINTSUP_DISABLE;
2765 intctl &= ~VGE_INTCTL_RXINTSUP_DISABLE;
2766 CSR_WRITE_1(sc, VGE_INTCTL1, intctl);
2767 CSR_WRITE_1(sc, VGE_CRC3, VGE_CR3_INT_HOLDOFF);
2768 if (sc->vge_int_holdoff > 0) {
2769 /* Set interrupt holdoff timer. */
2770 CSR_WRITE_1(sc, VGE_CAMCTL, VGE_PAGESEL_INTHLDOFF);
2771 CSR_WRITE_1(sc, VGE_INTHOLDOFF,
2772 VGE_INT_HOLDOFF_USEC(sc->vge_int_holdoff));
2773 /* Enable holdoff timer. */
2774 CSR_WRITE_1(sc, VGE_CRS3, VGE_CR3_INT_HOLDOFF);
2779 vge_setlinkspeed(struct vge_softc *sc)
2781 struct mii_data *mii;
2784 VGE_LOCK_ASSERT(sc);
2786 mii = device_get_softc(sc->vge_miibus);
2789 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
2790 (IFM_ACTIVE | IFM_AVALID)) {
2791 switch IFM_SUBTYPE(mii->mii_media_active) {
2801 /* Clear forced MAC speed/duplex configuration. */
2802 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2803 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_FDXFORCE);
2804 vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_100T2CR, 0);
2805 vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_ANAR,
2806 ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
2807 vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_BMCR,
2808 BMCR_AUTOEN | BMCR_STARTNEG);
2811 /* Poll link state until vge(4) get a 10/100 link. */
2812 for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
2814 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
2815 == (IFM_ACTIVE | IFM_AVALID)) {
2816 switch (IFM_SUBTYPE(mii->mii_media_active)) {
2825 pause("vgelnk", hz);
2828 if (i == MII_ANEGTICKS_GIGE)
2829 device_printf(sc->vge_dev, "establishing link failed, "
2830 "WOL may not work!");
2833 * No link, force MAC to have 100Mbps, full-duplex link.
2834 * This is the last resort and may/may not work.
2836 mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
2837 mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
2841 vge_setwol(struct vge_softc *sc)
2847 VGE_LOCK_ASSERT(sc);
2849 if ((sc->vge_flags & VGE_FLAG_PMCAP) == 0) {
2850 /* No PME capability, PHY power down. */
2851 vge_miibus_writereg(sc->vge_dev, sc->vge_phyaddr, MII_BMCR,
2853 vge_miipoll_stop(sc);
2859 /* Clear WOL on pattern match. */
2860 CSR_WRITE_1(sc, VGE_WOLCR0C, VGE_WOLCR0_PATTERN_ALL);
2861 /* Disable WOL on magic/unicast packet. */
2862 CSR_WRITE_1(sc, VGE_WOLCR1C, 0x0F);
2863 CSR_WRITE_1(sc, VGE_WOLCFGC, VGE_WOLCFG_SAB | VGE_WOLCFG_SAM |
2865 if ((ifp->if_capenable & IFCAP_WOL) != 0) {
2866 vge_setlinkspeed(sc);
2868 if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2869 val |= VGE_WOLCR1_UCAST;
2870 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2871 val |= VGE_WOLCR1_MAGIC;
2872 CSR_WRITE_1(sc, VGE_WOLCR1S, val);
2874 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2875 val |= VGE_WOLCFG_SAM | VGE_WOLCFG_SAB;
2876 CSR_WRITE_1(sc, VGE_WOLCFGS, val | VGE_WOLCFG_PMEOVR);
2877 /* Disable MII auto-polling. */
2878 vge_miipoll_stop(sc);
2880 CSR_SETBIT_1(sc, VGE_DIAGCTL,
2881 VGE_DIAGCTL_MACFORCE | VGE_DIAGCTL_FDXFORCE);
2882 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_GMII);
2884 /* Clear WOL status on pattern match. */
2885 CSR_WRITE_1(sc, VGE_WOLSR0C, 0xFF);
2886 CSR_WRITE_1(sc, VGE_WOLSR1C, 0xFF);
2888 val = CSR_READ_1(sc, VGE_PWRSTAT);
2889 val |= VGE_STICKHW_SWPTAG;
2890 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2891 /* Put hardware into sleep. */
2892 val = CSR_READ_1(sc, VGE_PWRSTAT);
2893 val |= VGE_STICKHW_DS0 | VGE_STICKHW_DS1;
2894 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2895 /* Request PME if WOL is requested. */
2896 pmstat = pci_read_config(sc->vge_dev, sc->vge_pmcap +
2897 PCIR_POWER_STATUS, 2);
2898 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2899 if ((ifp->if_capenable & IFCAP_WOL) != 0)
2900 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2901 pci_write_config(sc->vge_dev, sc->vge_pmcap + PCIR_POWER_STATUS,
2906 vge_clrwol(struct vge_softc *sc)
2910 val = CSR_READ_1(sc, VGE_PWRSTAT);
2911 val &= ~VGE_STICKHW_SWPTAG;
2912 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2913 /* Disable WOL and clear power state indicator. */
2914 val = CSR_READ_1(sc, VGE_PWRSTAT);
2915 val &= ~(VGE_STICKHW_DS0 | VGE_STICKHW_DS1);
2916 CSR_WRITE_1(sc, VGE_PWRSTAT, val);
2918 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_GMII);
2919 CSR_CLRBIT_1(sc, VGE_DIAGCTL, VGE_DIAGCTL_MACFORCE);
2921 /* Clear WOL on pattern match. */
2922 CSR_WRITE_1(sc, VGE_WOLCR0C, VGE_WOLCR0_PATTERN_ALL);
2923 /* Disable WOL on magic/unicast packet. */
2924 CSR_WRITE_1(sc, VGE_WOLCR1C, 0x0F);
2925 CSR_WRITE_1(sc, VGE_WOLCFGC, VGE_WOLCFG_SAB | VGE_WOLCFG_SAM |
2927 /* Clear WOL status on pattern match. */
2928 CSR_WRITE_1(sc, VGE_WOLSR0C, 0xFF);
2929 CSR_WRITE_1(sc, VGE_WOLSR1C, 0xFF);