2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
5 * Copyright (c) 1997, 1998, 1999
6 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
41 * available from http://www.sis.com.tw.
43 * This driver also supports the NatSemi DP83815. Datasheets are
44 * available from http://www.national.com.
46 * Written by Bill Paul <wpaul@ee.columbia.edu>
47 * Electrical Engineering Department
48 * Columbia University, New York City
51 * The SiS 900 is a fairly simple chip. It uses bus master DMA with
52 * simple TX and RX descriptors of 3 longwords in size. The receiver
53 * has a single perfect filter entry for the station address and a
54 * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
55 * transceiver while the 7016 requires an external transceiver chip.
56 * Both chips offer the standard bit-bang MII interface as well as
57 * an enchanced PHY interface which simplifies accessing MII registers.
59 * The only downside to this chipset is that RX descriptors must be
63 #ifdef HAVE_KERNEL_OPTION_HEADERS
64 #include "opt_device_polling.h"
67 #include <sys/param.h>
68 #include <sys/systm.h>
70 #include <sys/endian.h>
71 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/module.h>
76 #include <sys/socket.h>
77 #include <sys/sockio.h>
78 #include <sys/sysctl.h>
81 #include <net/if_var.h>
82 #include <net/if_arp.h>
83 #include <net/ethernet.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_types.h>
87 #include <net/if_vlan_var.h>
91 #include <machine/bus.h>
92 #include <machine/resource.h>
95 #include <dev/mii/mii.h>
96 #include <dev/mii/mii_bitbang.h>
97 #include <dev/mii/miivar.h>
99 #include <dev/pci/pcireg.h>
100 #include <dev/pci/pcivar.h>
102 #define SIS_USEIOSPACE
104 #include <dev/sis/if_sisreg.h>
106 MODULE_DEPEND(sis, pci, 1, 1, 1);
107 MODULE_DEPEND(sis, ether, 1, 1, 1);
108 MODULE_DEPEND(sis, miibus, 1, 1, 1);
110 /* "device miibus" required. See GENERIC if you get errors here. */
111 #include "miibus_if.h"
113 #define SIS_LOCK(_sc) mtx_lock(&(_sc)->sis_mtx)
114 #define SIS_UNLOCK(_sc) mtx_unlock(&(_sc)->sis_mtx)
115 #define SIS_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sis_mtx, MA_OWNED)
118 * register space access macros
120 #define CSR_WRITE_4(sc, reg, val) bus_write_4(sc->sis_res[0], reg, val)
122 #define CSR_READ_4(sc, reg) bus_read_4(sc->sis_res[0], reg)
124 #define CSR_READ_2(sc, reg) bus_read_2(sc->sis_res[0], reg)
126 #define CSR_BARRIER(sc, reg, length, flags) \
127 bus_barrier(sc->sis_res[0], reg, length, flags)
130 * Various supported device vendors/types and their names.
132 static const struct sis_type sis_devs[] = {
133 { SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
134 { SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
135 { NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP8381[56] 10/100BaseTX" },
139 static int sis_detach(device_t);
140 static __inline void sis_discard_rxbuf(struct sis_rxdesc *);
141 static int sis_dma_alloc(struct sis_softc *);
142 static void sis_dma_free(struct sis_softc *);
143 static int sis_dma_ring_alloc(struct sis_softc *, bus_size_t, bus_size_t,
144 bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
145 static void sis_dmamap_cb(void *, bus_dma_segment_t *, int, int);
146 #ifndef __NO_STRICT_ALIGNMENT
147 static __inline void sis_fixup_rx(struct mbuf *);
149 static void sis_ifmedia_sts(if_t, struct ifmediareq *);
150 static int sis_ifmedia_upd(if_t);
151 static void sis_init(void *);
152 static void sis_initl(struct sis_softc *);
153 static void sis_intr(void *);
154 static int sis_ioctl(if_t, u_long, caddr_t);
155 static uint32_t sis_mii_bitbang_read(device_t);
156 static void sis_mii_bitbang_write(device_t, uint32_t);
157 static int sis_newbuf(struct sis_softc *, struct sis_rxdesc *);
158 static int sis_resume(device_t);
159 static int sis_rxeof(struct sis_softc *);
160 static void sis_rxfilter(struct sis_softc *);
161 static void sis_rxfilter_ns(struct sis_softc *);
162 static void sis_rxfilter_sis(struct sis_softc *);
163 static void sis_start(if_t);
164 static void sis_startl(if_t);
165 static void sis_stop(struct sis_softc *);
166 static int sis_suspend(device_t);
167 static void sis_add_sysctls(struct sis_softc *);
168 static void sis_watchdog(struct sis_softc *);
169 static void sis_wol(struct sis_softc *);
174 static const struct mii_bitbang_ops sis_mii_bitbang_ops = {
175 sis_mii_bitbang_read,
176 sis_mii_bitbang_write,
178 SIS_MII_DATA, /* MII_BIT_MDO */
179 SIS_MII_DATA, /* MII_BIT_MDI */
180 SIS_MII_CLK, /* MII_BIT_MDC */
181 SIS_MII_DIR, /* MII_BIT_DIR_HOST_PHY */
182 0, /* MII_BIT_DIR_PHY_HOST */
186 static struct resource_spec sis_res_spec[] = {
187 #ifdef SIS_USEIOSPACE
188 { SYS_RES_IOPORT, SIS_PCI_LOIO, RF_ACTIVE},
190 { SYS_RES_MEMORY, SIS_PCI_LOMEM, RF_ACTIVE},
192 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
196 #define SIS_SETBIT(sc, reg, x) \
197 CSR_WRITE_4(sc, reg, \
198 CSR_READ_4(sc, reg) | (x))
200 #define SIS_CLRBIT(sc, reg, x) \
201 CSR_WRITE_4(sc, reg, \
202 CSR_READ_4(sc, reg) & ~(x))
205 CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
208 CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
211 * Routine to reverse the bits in a word. Stolen almost
212 * verbatim from /usr/games/fortune.
215 sis_reverse(uint16_t n)
217 n = ((n >> 1) & 0x5555) | ((n << 1) & 0xaaaa);
218 n = ((n >> 2) & 0x3333) | ((n << 2) & 0xcccc);
219 n = ((n >> 4) & 0x0f0f) | ((n << 4) & 0xf0f0);
220 n = ((n >> 8) & 0x00ff) | ((n << 8) & 0xff00);
226 sis_delay(struct sis_softc *sc)
230 for (idx = (300 / 33) + 1; idx > 0; idx--)
231 CSR_READ_4(sc, SIS_CSR);
235 sis_eeprom_idle(struct sis_softc *sc)
239 SIO_SET(SIS_EECTL_CSEL);
241 SIO_SET(SIS_EECTL_CLK);
244 for (i = 0; i < 25; i++) {
245 SIO_CLR(SIS_EECTL_CLK);
247 SIO_SET(SIS_EECTL_CLK);
251 SIO_CLR(SIS_EECTL_CLK);
253 SIO_CLR(SIS_EECTL_CSEL);
255 CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
259 * Send a read command and address to the EEPROM, check for ACK.
262 sis_eeprom_putbyte(struct sis_softc *sc, int addr)
266 d = addr | SIS_EECMD_READ;
269 * Feed in each bit and stobe the clock.
271 for (i = 0x400; i; i >>= 1) {
273 SIO_SET(SIS_EECTL_DIN);
275 SIO_CLR(SIS_EECTL_DIN);
278 SIO_SET(SIS_EECTL_CLK);
280 SIO_CLR(SIS_EECTL_CLK);
286 * Read a word of data stored in the EEPROM at address 'addr.'
289 sis_eeprom_getword(struct sis_softc *sc, int addr, uint16_t *dest)
294 /* Force EEPROM to idle state. */
297 /* Enter EEPROM access mode. */
299 SIO_CLR(SIS_EECTL_CLK);
301 SIO_SET(SIS_EECTL_CSEL);
305 * Send address of word we want to read.
307 sis_eeprom_putbyte(sc, addr);
310 * Start reading bits from EEPROM.
312 for (i = 0x8000; i; i >>= 1) {
313 SIO_SET(SIS_EECTL_CLK);
315 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
318 SIO_CLR(SIS_EECTL_CLK);
322 /* Turn off EEPROM access mode. */
329 * Read a sequence of words from the EEPROM.
332 sis_read_eeprom(struct sis_softc *sc, caddr_t dest, int off, int cnt, int swap)
335 uint16_t word = 0, *ptr;
337 for (i = 0; i < cnt; i++) {
338 sis_eeprom_getword(sc, off + i, &word);
339 ptr = (uint16_t *)(dest + (i * 2));
347 #if defined(__i386__) || defined(__amd64__)
349 sis_find_bridge(device_t dev)
351 devclass_t pci_devclass;
352 device_t *pci_devices;
354 device_t *pci_children;
355 int pci_childcount = 0;
356 device_t *busp, *childp;
357 device_t child = NULL;
360 if ((pci_devclass = devclass_find("pci")) == NULL)
363 devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
365 for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
366 if (device_get_children(*busp, &pci_children, &pci_childcount))
368 for (j = 0, childp = pci_children;
369 j < pci_childcount; j++, childp++) {
370 if (pci_get_vendor(*childp) == SIS_VENDORID &&
371 pci_get_device(*childp) == 0x0008) {
373 free(pci_children, M_TEMP);
377 free(pci_children, M_TEMP);
381 free(pci_devices, M_TEMP);
386 sis_read_cmos(struct sis_softc *sc, device_t dev, caddr_t dest, int off, int cnt)
391 bus_space_tag_t btag;
393 bridge = sis_find_bridge(dev);
396 reg = pci_read_config(bridge, 0x48, 1);
397 pci_write_config(bridge, 0x48, reg|0x40, 1);
400 #if defined(__amd64__) || defined(__i386__)
401 btag = X86_BUS_SPACE_IO;
404 for (i = 0; i < cnt; i++) {
405 bus_space_write_1(btag, 0x0, 0x70, i + off);
406 *(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
409 pci_write_config(bridge, 0x48, reg & ~0x40, 1);
413 sis_read_mac(struct sis_softc *sc, device_t dev, caddr_t dest)
415 uint32_t filtsave, csrsave;
417 filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
418 csrsave = CSR_READ_4(sc, SIS_CSR);
420 CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
421 CSR_WRITE_4(sc, SIS_CSR, 0);
423 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
425 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
426 ((uint16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
427 CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
428 ((uint16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
429 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
430 ((uint16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
432 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
433 CSR_WRITE_4(sc, SIS_CSR, csrsave);
438 * Read the MII serial port for the MII bit-bang module.
441 sis_mii_bitbang_read(device_t dev)
443 struct sis_softc *sc;
446 sc = device_get_softc(dev);
448 val = CSR_READ_4(sc, SIS_EECTL);
449 CSR_BARRIER(sc, SIS_EECTL, 4,
450 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
455 * Write the MII serial port for the MII bit-bang module.
458 sis_mii_bitbang_write(device_t dev, uint32_t val)
460 struct sis_softc *sc;
462 sc = device_get_softc(dev);
464 CSR_WRITE_4(sc, SIS_EECTL, val);
465 CSR_BARRIER(sc, SIS_EECTL, 4,
466 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
470 sis_miibus_readreg(device_t dev, int phy, int reg)
472 struct sis_softc *sc;
474 sc = device_get_softc(dev);
476 if (sc->sis_type == SIS_TYPE_83815) {
480 * The NatSemi chip can take a while after
481 * a reset to come ready, during which the BMSR
482 * returns a value of 0. This is *never* supposed
483 * to happen: some of the BMSR bits are meant to
484 * be hardwired in the on position, and this can
485 * confuse the miibus code a bit during the probe
486 * and attach phase. So we make an effort to check
487 * for this condition and wait for it to clear.
489 if (!CSR_READ_4(sc, NS_BMSR))
491 return CSR_READ_4(sc, NS_BMCR + (reg * 4));
495 * Chipsets < SIS_635 seem not to be able to read/write
496 * through mdio. Use the enhanced PHY access register
499 if (sc->sis_type == SIS_TYPE_900 &&
500 sc->sis_rev < SIS_REV_635) {
506 CSR_WRITE_4(sc, SIS_PHYCTL,
507 (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
508 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
510 for (i = 0; i < SIS_TIMEOUT; i++) {
511 if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
515 if (i == SIS_TIMEOUT) {
516 device_printf(sc->sis_dev,
517 "PHY failed to come ready\n");
521 val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
528 return (mii_bitbang_readreg(dev, &sis_mii_bitbang_ops, phy,
533 sis_miibus_writereg(device_t dev, int phy, int reg, int data)
535 struct sis_softc *sc;
537 sc = device_get_softc(dev);
539 if (sc->sis_type == SIS_TYPE_83815) {
542 CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
547 * Chipsets < SIS_635 seem not to be able to read/write
548 * through mdio. Use the enhanced PHY access register
551 if (sc->sis_type == SIS_TYPE_900 &&
552 sc->sis_rev < SIS_REV_635) {
558 CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
559 (reg << 6) | SIS_PHYOP_WRITE);
560 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
562 for (i = 0; i < SIS_TIMEOUT; i++) {
563 if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
567 if (i == SIS_TIMEOUT)
568 device_printf(sc->sis_dev,
569 "PHY failed to come ready\n");
571 mii_bitbang_writereg(dev, &sis_mii_bitbang_ops, phy, reg,
577 sis_miibus_statchg(device_t dev)
579 struct sis_softc *sc;
580 struct mii_data *mii;
584 sc = device_get_softc(dev);
587 mii = device_get_softc(sc->sis_miibus);
589 if (mii == NULL || ifp == NULL ||
590 (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
593 sc->sis_flags &= ~SIS_FLAG_LINK;
594 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
595 (IFM_ACTIVE | IFM_AVALID)) {
596 switch (IFM_SUBTYPE(mii->mii_media_active)) {
598 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
599 sc->sis_flags |= SIS_FLAG_LINK;
602 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
603 sc->sis_flags |= SIS_FLAG_LINK;
610 if ((sc->sis_flags & SIS_FLAG_LINK) == 0) {
612 * Stopping MACs seem to reset SIS_TX_LISTPTR and
613 * SIS_RX_LISTPTR which in turn requires resetting
614 * TX/RX buffers. So just don't do anything for
620 /* Set full/half duplex mode. */
621 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
622 SIS_SETBIT(sc, SIS_TX_CFG,
623 (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
624 SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
626 SIS_CLRBIT(sc, SIS_TX_CFG,
627 (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
628 SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
631 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
633 * MPII03.D: Half Duplex Excessive Collisions.
634 * Also page 49 in 83816 manual
636 SIS_SETBIT(sc, SIS_TX_CFG, SIS_TXCFG_MPII03D);
639 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr < NS_SRR_16A &&
640 IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
642 * Short Cable Receive Errors (MP21.E)
644 CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
645 reg = CSR_READ_4(sc, NS_PHY_DSPCFG) & 0xfff;
646 CSR_WRITE_4(sc, NS_PHY_DSPCFG, reg | 0x1000);
648 reg = CSR_READ_4(sc, NS_PHY_TDATA) & 0xff;
649 if ((reg & 0x0080) == 0 || (reg > 0xd8 && reg <= 0xff)) {
650 device_printf(sc->sis_dev,
651 "Applying short cable fix (reg=%x)\n", reg);
652 CSR_WRITE_4(sc, NS_PHY_TDATA, 0x00e8);
653 SIS_SETBIT(sc, NS_PHY_DSPCFG, 0x20);
655 CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
657 /* Enable TX/RX MACs. */
658 SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
659 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE | SIS_CSR_RX_ENABLE);
663 sis_mchash(struct sis_softc *sc, const uint8_t *addr)
667 /* Compute CRC for the address value. */
668 crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
671 * return the filter bit position
673 * The NatSemi chip has a 512-bit filter, which is
674 * different than the SiS, so we special-case it.
676 if (sc->sis_type == SIS_TYPE_83815)
678 else if (sc->sis_rev >= SIS_REV_635 ||
679 sc->sis_rev == SIS_REV_900B)
686 sis_rxfilter(struct sis_softc *sc)
691 if (sc->sis_type == SIS_TYPE_83815)
694 sis_rxfilter_sis(sc);
698 sis_write_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
700 struct sis_softc *sc = arg;
704 h = sis_mchash(sc, LLADDR(sdl));
707 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
710 SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
716 sis_rxfilter_ns(struct sis_softc *sc)
722 filter = CSR_READ_4(sc, SIS_RXFILT_CTL);
723 if (filter & SIS_RXFILTCTL_ENABLE) {
725 * Filter should be disabled to program other bits.
727 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter & ~SIS_RXFILTCTL_ENABLE);
728 CSR_READ_4(sc, SIS_RXFILT_CTL);
730 filter &= ~(NS_RXFILTCTL_ARP | NS_RXFILTCTL_PERFECT |
731 NS_RXFILTCTL_MCHASH | SIS_RXFILTCTL_ALLPHYS | SIS_RXFILTCTL_BROAD |
732 SIS_RXFILTCTL_ALLMULTI);
734 if (if_getflags(ifp) & IFF_BROADCAST)
735 filter |= SIS_RXFILTCTL_BROAD;
737 * For the NatSemi chip, we have to explicitly enable the
738 * reception of ARP frames, as well as turn on the 'perfect
739 * match' filter where we store the station address, otherwise
740 * we won't receive unicasts meant for this host.
742 filter |= NS_RXFILTCTL_ARP | NS_RXFILTCTL_PERFECT;
744 if (if_getflags(ifp) & (IFF_ALLMULTI | IFF_PROMISC)) {
745 filter |= SIS_RXFILTCTL_ALLMULTI;
746 if (if_getflags(ifp) & IFF_PROMISC)
747 filter |= SIS_RXFILTCTL_ALLPHYS;
750 * We have to explicitly enable the multicast hash table
751 * on the NatSemi chip if we want to use it, which we do.
753 filter |= NS_RXFILTCTL_MCHASH;
755 /* first, zot all the existing hash bits */
756 for (i = 0; i < 32; i++) {
757 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO +
759 CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
762 if_foreach_llmaddr(ifp, sis_write_maddr, sc);
765 /* Turn the receive filter on */
766 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter | SIS_RXFILTCTL_ENABLE);
767 CSR_READ_4(sc, SIS_RXFILT_CTL);
770 struct sis_hash_maddr_ctx {
771 struct sis_softc *sc;
776 sis_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
778 struct sis_hash_maddr_ctx *ctx = arg;
781 h = sis_mchash(ctx->sc, LLADDR(sdl));
782 ctx->hashes[h >> 4] |= 1 << (h & 0xf);
788 sis_rxfilter_sis(struct sis_softc *sc)
791 struct sis_hash_maddr_ctx ctx;
792 uint32_t filter, i, n;
796 /* hash table size */
797 if (sc->sis_rev >= SIS_REV_635 || sc->sis_rev == SIS_REV_900B)
802 filter = CSR_READ_4(sc, SIS_RXFILT_CTL);
803 if (filter & SIS_RXFILTCTL_ENABLE) {
804 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter & ~SIS_RXFILTCTL_ENABLE);
805 CSR_READ_4(sc, SIS_RXFILT_CTL);
807 filter &= ~(SIS_RXFILTCTL_ALLPHYS | SIS_RXFILTCTL_BROAD |
808 SIS_RXFILTCTL_ALLMULTI);
809 if (if_getflags(ifp) & IFF_BROADCAST)
810 filter |= SIS_RXFILTCTL_BROAD;
812 if (if_getflags(ifp) & (IFF_ALLMULTI | IFF_PROMISC)) {
813 filter |= SIS_RXFILTCTL_ALLMULTI;
814 if (if_getflags(ifp) & IFF_PROMISC)
815 filter |= SIS_RXFILTCTL_ALLPHYS;
816 for (i = 0; i < n; i++)
819 for (i = 0; i < n; i++)
822 if (if_foreach_llmaddr(ifp, sis_hash_maddr, &ctx) > n) {
823 filter |= SIS_RXFILTCTL_ALLMULTI;
824 for (i = 0; i < n; i++)
829 for (i = 0; i < n; i++) {
830 CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
831 CSR_WRITE_4(sc, SIS_RXFILT_DATA, ctx.hashes[i]);
834 /* Turn the receive filter on */
835 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter | SIS_RXFILTCTL_ENABLE);
836 CSR_READ_4(sc, SIS_RXFILT_CTL);
840 sis_reset(struct sis_softc *sc)
844 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
846 for (i = 0; i < SIS_TIMEOUT; i++) {
847 if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
851 if (i == SIS_TIMEOUT)
852 device_printf(sc->sis_dev, "reset never completed\n");
854 /* Wait a little while for the chip to get its brains in order. */
858 * If this is a NetSemi chip, make sure to clear
861 if (sc->sis_type == SIS_TYPE_83815) {
862 CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
863 CSR_WRITE_4(sc, NS_CLKRUN, 0);
865 /* Disable WOL functions. */
866 CSR_WRITE_4(sc, SIS_PWRMAN_CTL, 0);
871 * Probe for an SiS chip. Check the PCI vendor and device
872 * IDs against our list and return a device name if we find a match.
875 sis_probe(device_t dev)
877 const struct sis_type *t;
881 while (t->sis_name != NULL) {
882 if ((pci_get_vendor(dev) == t->sis_vid) &&
883 (pci_get_device(dev) == t->sis_did)) {
884 device_set_desc(dev, t->sis_name);
885 return (BUS_PROBE_DEFAULT);
894 * Attach the interface. Allocate softc structures, do ifmedia
895 * setup and ethernet/BPF attach.
898 sis_attach(device_t dev)
900 u_char eaddr[ETHER_ADDR_LEN];
901 struct sis_softc *sc;
905 sc = device_get_softc(dev);
909 mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
911 callout_init_mtx(&sc->sis_stat_ch, &sc->sis_mtx, 0);
913 if (pci_get_device(dev) == SIS_DEVICEID_900)
914 sc->sis_type = SIS_TYPE_900;
915 if (pci_get_device(dev) == SIS_DEVICEID_7016)
916 sc->sis_type = SIS_TYPE_7016;
917 if (pci_get_vendor(dev) == NS_VENDORID)
918 sc->sis_type = SIS_TYPE_83815;
920 sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
922 * Map control/status registers.
924 pci_enable_busmaster(dev);
926 error = bus_alloc_resources(dev, sis_res_spec, sc->sis_res);
928 device_printf(dev, "couldn't allocate resources\n");
932 /* Reset the adapter. */
935 if (sc->sis_type == SIS_TYPE_900 &&
936 (sc->sis_rev == SIS_REV_635 ||
937 sc->sis_rev == SIS_REV_900B)) {
938 SIO_SET(SIS_CFG_RND_CNT);
939 SIO_SET(SIS_CFG_PERR_DETECT);
943 * Get station address from the EEPROM.
945 switch (pci_get_vendor(dev)) {
947 sc->sis_srr = CSR_READ_4(sc, NS_SRR);
949 /* We can't update the device description, so spew */
950 if (sc->sis_srr == NS_SRR_15C)
951 device_printf(dev, "Silicon Revision: DP83815C\n");
952 else if (sc->sis_srr == NS_SRR_15D)
953 device_printf(dev, "Silicon Revision: DP83815D\n");
954 else if (sc->sis_srr == NS_SRR_16A)
955 device_printf(dev, "Silicon Revision: DP83816A\n");
957 device_printf(dev, "Silicon Revision %x\n", sc->sis_srr);
960 * Reading the MAC address out of the EEPROM on
961 * the NatSemi chip takes a bit more work than
962 * you'd expect. The address spans 4 16-bit words,
963 * with the first word containing only a single bit.
964 * You have to shift everything over one bit to
965 * get it aligned properly. Also, the bits are
966 * stored backwards (the LSB is really the MSB,
967 * and so on) so you have to reverse them in order
968 * to get the MAC address into the form we want.
969 * Why? Who the hell knows.
974 sis_read_eeprom(sc, (caddr_t)&tmp,
975 NS_EE_NODEADDR, 4, 0);
977 /* Shift everything over one bit. */
978 tmp[3] = tmp[3] >> 1;
979 tmp[3] |= tmp[2] << 15;
980 tmp[2] = tmp[2] >> 1;
981 tmp[2] |= tmp[1] << 15;
982 tmp[1] = tmp[1] >> 1;
983 tmp[1] |= tmp[0] << 15;
985 /* Now reverse all the bits. */
986 tmp[3] = sis_reverse(tmp[3]);
987 tmp[2] = sis_reverse(tmp[2]);
988 tmp[1] = sis_reverse(tmp[1]);
990 eaddr[0] = (tmp[1] >> 0) & 0xFF;
991 eaddr[1] = (tmp[1] >> 8) & 0xFF;
992 eaddr[2] = (tmp[2] >> 0) & 0xFF;
993 eaddr[3] = (tmp[2] >> 8) & 0xFF;
994 eaddr[4] = (tmp[3] >> 0) & 0xFF;
995 eaddr[5] = (tmp[3] >> 8) & 0xFF;
1000 #if defined(__i386__) || defined(__amd64__)
1002 * If this is a SiS 630E chipset with an embedded
1003 * SiS 900 controller, we have to read the MAC address
1004 * from the APC CMOS RAM. Our method for doing this
1005 * is very ugly since we have to reach out and grab
1006 * ahold of hardware for which we cannot properly
1007 * allocate resources. This code is only compiled on
1008 * the i386 architecture since the SiS 630E chipset
1009 * is for x86 motherboards only. Note that there are
1010 * a lot of magic numbers in this hack. These are
1011 * taken from SiS's Linux driver. I'd like to replace
1012 * them with proper symbolic definitions, but that
1013 * requires some datasheets that I don't have access
1016 if (sc->sis_rev == SIS_REV_630S ||
1017 sc->sis_rev == SIS_REV_630E ||
1018 sc->sis_rev == SIS_REV_630EA1)
1019 sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
1021 else if (sc->sis_rev == SIS_REV_635 ||
1022 sc->sis_rev == SIS_REV_630ET)
1023 sis_read_mac(sc, dev, (caddr_t)&eaddr);
1024 else if (sc->sis_rev == SIS_REV_96x) {
1025 /* Allow to read EEPROM from LAN. It is shared
1026 * between a 1394 controller and the NIC and each
1027 * time we access it, we need to set SIS_EECMD_REQ.
1029 SIO_SET(SIS_EECMD_REQ);
1030 for (int waittime = 0; waittime < SIS_TIMEOUT;
1032 /* Force EEPROM to idle state. */
1033 sis_eeprom_idle(sc);
1034 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1035 sis_read_eeprom(sc, (caddr_t)&eaddr,
1036 SIS_EE_NODEADDR, 3, 0);
1042 * Set SIS_EECTL_CLK to high, so a other master
1043 * can operate on the i2c bus.
1045 SIO_SET(SIS_EECTL_CLK);
1046 /* Refuse EEPROM access by LAN */
1047 SIO_SET(SIS_EECMD_DONE);
1050 sis_read_eeprom(sc, (caddr_t)&eaddr,
1051 SIS_EE_NODEADDR, 3, 0);
1055 sis_add_sysctls(sc);
1057 /* Allocate DMA'able memory. */
1058 if ((error = sis_dma_alloc(sc)) != 0)
1061 ifp = sc->sis_ifp = if_alloc(IFT_ETHER);
1063 device_printf(dev, "can not if_alloc()\n");
1067 if_setsoftc(ifp, sc);
1068 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1069 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
1070 if_setioctlfn(ifp, sis_ioctl);
1071 if_setstartfn(ifp, sis_start);
1072 if_setinitfn(ifp, sis_init);
1073 if_setsendqlen(ifp, SIS_TX_LIST_CNT - 1);
1074 if_setsendqready(ifp);
1076 if (pci_find_cap(sc->sis_dev, PCIY_PMG, &pmc) == 0) {
1077 if (sc->sis_type == SIS_TYPE_83815)
1078 if_setcapabilitiesbit(ifp, IFCAP_WOL, 0);
1080 if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC, 0);
1081 if_setcapenable(ifp, if_getcapabilities(ifp));
1087 error = mii_attach(dev, &sc->sis_miibus, ifp, sis_ifmedia_upd,
1088 sis_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1090 device_printf(dev, "attaching PHYs failed\n");
1095 * Call MI attach routine.
1097 ether_ifattach(ifp, eaddr);
1100 * Tell the upper layer(s) we support long frames.
1102 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
1103 if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
1104 if_setcapenable(ifp, if_getcapabilities(ifp));
1105 #ifdef DEVICE_POLLING
1106 if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
1109 /* Hook interrupt last to avoid having to lock softc */
1110 error = bus_setup_intr(dev, sc->sis_res[1], INTR_TYPE_NET | INTR_MPSAFE,
1111 NULL, sis_intr, sc, &sc->sis_intrhand);
1114 device_printf(dev, "couldn't set up irq\n");
1115 ether_ifdetach(ifp);
1127 * Shutdown hardware and free up resources. This can be called any
1128 * time after the mutex has been initialized. It is called in both
1129 * the error case in attach and the normal detach case so it needs
1130 * to be careful about only freeing resources that have actually been
1134 sis_detach(device_t dev)
1136 struct sis_softc *sc;
1139 sc = device_get_softc(dev);
1140 KASSERT(mtx_initialized(&sc->sis_mtx), ("sis mutex not initialized"));
1143 #ifdef DEVICE_POLLING
1144 if (if_getcapenable(ifp) & IFCAP_POLLING)
1145 ether_poll_deregister(ifp);
1148 /* These should only be active if attach succeeded. */
1149 if (device_is_attached(dev)) {
1153 callout_drain(&sc->sis_stat_ch);
1154 ether_ifdetach(ifp);
1157 device_delete_child(dev, sc->sis_miibus);
1158 bus_generic_detach(dev);
1160 if (sc->sis_intrhand)
1161 bus_teardown_intr(dev, sc->sis_res[1], sc->sis_intrhand);
1162 bus_release_resources(dev, sis_res_spec, sc->sis_res);
1169 mtx_destroy(&sc->sis_mtx);
1174 struct sis_dmamap_arg {
1175 bus_addr_t sis_busaddr;
1179 sis_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1181 struct sis_dmamap_arg *ctx;
1186 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1188 ctx = (struct sis_dmamap_arg *)arg;
1189 ctx->sis_busaddr = segs[0].ds_addr;
1193 sis_dma_ring_alloc(struct sis_softc *sc, bus_size_t alignment,
1194 bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
1195 bus_addr_t *paddr, const char *msg)
1197 struct sis_dmamap_arg ctx;
1200 error = bus_dma_tag_create(sc->sis_parent_tag, alignment, 0,
1201 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, maxsize, 1,
1202 maxsize, 0, NULL, NULL, tag);
1204 device_printf(sc->sis_dev,
1205 "could not create %s dma tag\n", msg);
1208 /* Allocate DMA'able memory for ring. */
1209 error = bus_dmamem_alloc(*tag, (void **)ring,
1210 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
1212 device_printf(sc->sis_dev,
1213 "could not allocate DMA'able memory for %s\n", msg);
1216 /* Load the address of the ring. */
1217 ctx.sis_busaddr = 0;
1218 error = bus_dmamap_load(*tag, *map, *ring, maxsize, sis_dmamap_cb,
1219 &ctx, BUS_DMA_NOWAIT);
1221 device_printf(sc->sis_dev,
1222 "could not load DMA'able memory for %s\n", msg);
1225 *paddr = ctx.sis_busaddr;
1230 sis_dma_alloc(struct sis_softc *sc)
1232 struct sis_rxdesc *rxd;
1233 struct sis_txdesc *txd;
1236 /* Allocate the parent bus DMA tag appropriate for PCI. */
1237 error = bus_dma_tag_create(bus_get_dma_tag(sc->sis_dev),
1238 1, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1239 NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
1240 0, NULL, NULL, &sc->sis_parent_tag);
1242 device_printf(sc->sis_dev,
1243 "could not allocate parent dma tag\n");
1247 /* Create RX ring. */
1248 error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_RX_LIST_SZ,
1249 &sc->sis_rx_list_tag, (uint8_t **)&sc->sis_rx_list,
1250 &sc->sis_rx_list_map, &sc->sis_rx_paddr, "RX ring");
1254 /* Create TX ring. */
1255 error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_TX_LIST_SZ,
1256 &sc->sis_tx_list_tag, (uint8_t **)&sc->sis_tx_list,
1257 &sc->sis_tx_list_map, &sc->sis_tx_paddr, "TX ring");
1261 /* Create tag for RX mbufs. */
1262 error = bus_dma_tag_create(sc->sis_parent_tag, SIS_RX_BUF_ALIGN, 0,
1263 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
1264 MCLBYTES, 0, NULL, NULL, &sc->sis_rx_tag);
1266 device_printf(sc->sis_dev, "could not allocate RX dma tag\n");
1270 /* Create tag for TX mbufs. */
1271 error = bus_dma_tag_create(sc->sis_parent_tag, 1, 0,
1272 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1273 MCLBYTES * SIS_MAXTXSEGS, SIS_MAXTXSEGS, MCLBYTES, 0, NULL, NULL,
1276 device_printf(sc->sis_dev, "could not allocate TX dma tag\n");
1280 /* Create DMA maps for RX buffers. */
1281 error = bus_dmamap_create(sc->sis_rx_tag, 0, &sc->sis_rx_sparemap);
1283 device_printf(sc->sis_dev,
1284 "can't create spare DMA map for RX\n");
1287 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1288 rxd = &sc->sis_rxdesc[i];
1290 error = bus_dmamap_create(sc->sis_rx_tag, 0, &rxd->rx_dmamap);
1292 device_printf(sc->sis_dev,
1293 "can't create DMA map for RX\n");
1298 /* Create DMA maps for TX buffers. */
1299 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1300 txd = &sc->sis_txdesc[i];
1302 error = bus_dmamap_create(sc->sis_tx_tag, 0, &txd->tx_dmamap);
1304 device_printf(sc->sis_dev,
1305 "can't create DMA map for TX\n");
1314 sis_dma_free(struct sis_softc *sc)
1316 struct sis_rxdesc *rxd;
1317 struct sis_txdesc *txd;
1320 /* Destroy DMA maps for RX buffers. */
1321 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1322 rxd = &sc->sis_rxdesc[i];
1324 bus_dmamap_destroy(sc->sis_rx_tag, rxd->rx_dmamap);
1326 if (sc->sis_rx_sparemap)
1327 bus_dmamap_destroy(sc->sis_rx_tag, sc->sis_rx_sparemap);
1329 /* Destroy DMA maps for TX buffers. */
1330 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1331 txd = &sc->sis_txdesc[i];
1333 bus_dmamap_destroy(sc->sis_tx_tag, txd->tx_dmamap);
1337 bus_dma_tag_destroy(sc->sis_rx_tag);
1339 bus_dma_tag_destroy(sc->sis_tx_tag);
1341 /* Destroy RX ring. */
1342 if (sc->sis_rx_paddr)
1343 bus_dmamap_unload(sc->sis_rx_list_tag, sc->sis_rx_list_map);
1344 if (sc->sis_rx_list)
1345 bus_dmamem_free(sc->sis_rx_list_tag, sc->sis_rx_list,
1346 sc->sis_rx_list_map);
1348 if (sc->sis_rx_list_tag)
1349 bus_dma_tag_destroy(sc->sis_rx_list_tag);
1351 /* Destroy TX ring. */
1352 if (sc->sis_tx_paddr)
1353 bus_dmamap_unload(sc->sis_tx_list_tag, sc->sis_tx_list_map);
1355 if (sc->sis_tx_list)
1356 bus_dmamem_free(sc->sis_tx_list_tag, sc->sis_tx_list,
1357 sc->sis_tx_list_map);
1359 if (sc->sis_tx_list_tag)
1360 bus_dma_tag_destroy(sc->sis_tx_list_tag);
1362 /* Destroy the parent tag. */
1363 if (sc->sis_parent_tag)
1364 bus_dma_tag_destroy(sc->sis_parent_tag);
1368 * Initialize the TX and RX descriptors and allocate mbufs for them. Note that
1369 * we arrange the descriptors in a closed ring, so that the last descriptor
1370 * points back to the first.
1373 sis_ring_init(struct sis_softc *sc)
1375 struct sis_rxdesc *rxd;
1376 struct sis_txdesc *txd;
1380 bzero(&sc->sis_tx_list[0], SIS_TX_LIST_SZ);
1381 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1382 txd = &sc->sis_txdesc[i];
1384 if (i == SIS_TX_LIST_CNT - 1)
1385 next = SIS_TX_RING_ADDR(sc, 0);
1387 next = SIS_TX_RING_ADDR(sc, i + 1);
1388 sc->sis_tx_list[i].sis_next = htole32(SIS_ADDR_LO(next));
1390 sc->sis_tx_prod = sc->sis_tx_cons = sc->sis_tx_cnt = 0;
1391 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1392 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1394 sc->sis_rx_cons = 0;
1395 bzero(&sc->sis_rx_list[0], SIS_RX_LIST_SZ);
1396 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1397 rxd = &sc->sis_rxdesc[i];
1398 rxd->rx_desc = &sc->sis_rx_list[i];
1399 if (i == SIS_RX_LIST_CNT - 1)
1400 next = SIS_RX_RING_ADDR(sc, 0);
1402 next = SIS_RX_RING_ADDR(sc, i + 1);
1403 rxd->rx_desc->sis_next = htole32(SIS_ADDR_LO(next));
1404 error = sis_newbuf(sc, rxd);
1408 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1409 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1415 * Initialize an RX descriptor and attach an MBUF cluster.
1418 sis_newbuf(struct sis_softc *sc, struct sis_rxdesc *rxd)
1421 bus_dma_segment_t segs[1];
1425 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1428 m->m_len = m->m_pkthdr.len = SIS_RXLEN;
1429 #ifndef __NO_STRICT_ALIGNMENT
1430 m_adj(m, SIS_RX_BUF_ALIGN);
1433 if (bus_dmamap_load_mbuf_sg(sc->sis_rx_tag, sc->sis_rx_sparemap, m,
1434 segs, &nsegs, 0) != 0) {
1438 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1440 if (rxd->rx_m != NULL) {
1441 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
1442 BUS_DMASYNC_POSTREAD);
1443 bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
1445 map = rxd->rx_dmamap;
1446 rxd->rx_dmamap = sc->sis_rx_sparemap;
1447 sc->sis_rx_sparemap = map;
1448 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap, BUS_DMASYNC_PREREAD);
1450 rxd->rx_desc->sis_ptr = htole32(SIS_ADDR_LO(segs[0].ds_addr));
1451 rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1455 static __inline void
1456 sis_discard_rxbuf(struct sis_rxdesc *rxd)
1459 rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1462 #ifndef __NO_STRICT_ALIGNMENT
1463 static __inline void
1464 sis_fixup_rx(struct mbuf *m)
1466 uint16_t *src, *dst;
1469 src = mtod(m, uint16_t *);
1470 dst = src - (SIS_RX_BUF_ALIGN - ETHER_ALIGN) / sizeof(*src);
1472 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1475 m->m_data -= SIS_RX_BUF_ALIGN - ETHER_ALIGN;
1480 * A frame has been uploaded: pass the resulting mbuf chain up to
1481 * the higher level protocols.
1484 sis_rxeof(struct sis_softc *sc)
1488 struct sis_rxdesc *rxd;
1489 struct sis_desc *cur_rx;
1490 int prog, rx_cons, rx_npkts = 0, total_len;
1493 SIS_LOCK_ASSERT(sc);
1495 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1496 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1498 rx_cons = sc->sis_rx_cons;
1501 for (prog = 0; (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0;
1502 SIS_INC(rx_cons, SIS_RX_LIST_CNT), prog++) {
1503 #ifdef DEVICE_POLLING
1504 if (if_getcapenable(ifp) & IFCAP_POLLING) {
1505 if (sc->rxcycles <= 0)
1510 cur_rx = &sc->sis_rx_list[rx_cons];
1511 rxstat = le32toh(cur_rx->sis_cmdsts);
1512 if ((rxstat & SIS_CMDSTS_OWN) == 0)
1514 rxd = &sc->sis_rxdesc[rx_cons];
1516 total_len = (rxstat & SIS_CMDSTS_BUFLEN) - ETHER_CRC_LEN;
1517 if ((if_getcapenable(ifp) & IFCAP_VLAN_MTU) != 0 &&
1518 total_len <= (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN -
1520 rxstat &= ~SIS_RXSTAT_GIANT;
1521 if (SIS_RXSTAT_ERROR(rxstat) != 0) {
1522 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1523 if (rxstat & SIS_RXSTAT_COLL)
1524 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1525 sis_discard_rxbuf(rxd);
1529 /* Add a new receive buffer to the ring. */
1531 if (sis_newbuf(sc, rxd) != 0) {
1532 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1533 sis_discard_rxbuf(rxd);
1537 /* No errors; receive the packet. */
1538 m->m_pkthdr.len = m->m_len = total_len;
1539 #ifndef __NO_STRICT_ALIGNMENT
1541 * On architectures without alignment problems we try to
1542 * allocate a new buffer for the receive ring, and pass up
1543 * the one where the packet is already, saving the expensive
1548 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1549 m->m_pkthdr.rcvif = ifp;
1558 sc->sis_rx_cons = rx_cons;
1559 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1560 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1567 * A frame was downloaded to the chip. It's safe for us to clean up
1572 sis_txeof(struct sis_softc *sc)
1575 struct sis_desc *cur_tx;
1576 struct sis_txdesc *txd;
1577 uint32_t cons, txstat;
1579 SIS_LOCK_ASSERT(sc);
1581 cons = sc->sis_tx_cons;
1582 if (cons == sc->sis_tx_prod)
1586 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1587 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1590 * Go through our tx list and free mbufs for those
1591 * frames that have been transmitted.
1593 for (; cons != sc->sis_tx_prod; SIS_INC(cons, SIS_TX_LIST_CNT)) {
1594 cur_tx = &sc->sis_tx_list[cons];
1595 txstat = le32toh(cur_tx->sis_cmdsts);
1596 if ((txstat & SIS_CMDSTS_OWN) != 0)
1598 txd = &sc->sis_txdesc[cons];
1599 if (txd->tx_m != NULL) {
1600 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
1601 BUS_DMASYNC_POSTWRITE);
1602 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1605 if ((txstat & SIS_CMDSTS_PKT_OK) != 0) {
1606 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1607 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
1608 (txstat & SIS_TXSTAT_COLLCNT) >> 16);
1610 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1611 if (txstat & SIS_TXSTAT_EXCESSCOLLS)
1612 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1613 if (txstat & SIS_TXSTAT_OUTOFWINCOLL)
1614 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1618 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1620 sc->sis_tx_cons = cons;
1621 if (sc->sis_tx_cnt == 0)
1622 sc->sis_watchdog_timer = 0;
1628 struct sis_softc *sc;
1629 struct mii_data *mii;
1632 SIS_LOCK_ASSERT(sc);
1634 mii = device_get_softc(sc->sis_miibus);
1637 if ((sc->sis_flags & SIS_FLAG_LINK) == 0)
1638 sis_miibus_statchg(sc->sis_dev);
1639 callout_reset(&sc->sis_stat_ch, hz, sis_tick, sc);
1642 #ifdef DEVICE_POLLING
1643 static poll_handler_t sis_poll;
1646 sis_poll(if_t ifp, enum poll_cmd cmd, int count)
1648 struct sis_softc *sc = if_getsoftc(ifp);
1652 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1658 * On the sis, reading the status register also clears it.
1659 * So before returning to intr mode we must make sure that all
1660 * possible pending sources of interrupts have been served.
1661 * In practice this means run to completion the *eof routines,
1662 * and then call the interrupt routine
1664 sc->rxcycles = count;
1665 rx_npkts = sis_rxeof(sc);
1667 if (!if_sendq_empty(ifp))
1670 if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1673 /* Reading the ISR register clears all interrupts. */
1674 status = CSR_READ_4(sc, SIS_ISR);
1676 if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1677 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1679 if (status & (SIS_ISR_RX_IDLE))
1680 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1682 if (status & SIS_ISR_SYSERR) {
1683 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1691 #endif /* DEVICE_POLLING */
1696 struct sis_softc *sc;
1704 #ifdef DEVICE_POLLING
1705 if (if_getcapenable(ifp) & IFCAP_POLLING) {
1711 /* Reading the ISR register clears all interrupts. */
1712 status = CSR_READ_4(sc, SIS_ISR);
1713 if ((status & SIS_INTRS) == 0) {
1719 /* Disable interrupts. */
1720 CSR_WRITE_4(sc, SIS_IER, 0);
1722 for (;(status & SIS_INTRS) != 0;) {
1723 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
1726 (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
1727 SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
1730 if (status & (SIS_ISR_RX_DESC_OK | SIS_ISR_RX_OK |
1731 SIS_ISR_RX_ERR | SIS_ISR_RX_IDLE))
1734 if (status & SIS_ISR_RX_OFLOW)
1735 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1737 if (status & (SIS_ISR_RX_IDLE))
1738 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1740 if (status & SIS_ISR_SYSERR) {
1741 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
1746 status = CSR_READ_4(sc, SIS_ISR);
1749 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1750 /* Re-enable interrupts. */
1751 CSR_WRITE_4(sc, SIS_IER, 1);
1753 if (!if_sendq_empty(ifp))
1761 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1762 * pointers to the fragment pointers.
1765 sis_encap(struct sis_softc *sc, struct mbuf **m_head)
1768 struct sis_txdesc *txd;
1770 bus_dma_segment_t segs[SIS_MAXTXSEGS];
1772 int error, i, frag, nsegs, prod;
1775 prod = sc->sis_tx_prod;
1776 txd = &sc->sis_txdesc[prod];
1777 if ((sc->sis_flags & SIS_FLAG_MANUAL_PAD) != 0 &&
1778 (*m_head)->m_pkthdr.len < SIS_MIN_FRAMELEN) {
1780 padlen = SIS_MIN_FRAMELEN - m->m_pkthdr.len;
1781 if (M_WRITABLE(m) == 0) {
1782 /* Get a writable copy. */
1783 m = m_dup(*m_head, M_NOWAIT);
1791 if (m->m_next != NULL || M_TRAILINGSPACE(m) < padlen) {
1792 m = m_defrag(m, M_NOWAIT);
1800 * Manually pad short frames, and zero the pad space
1801 * to avoid leaking data.
1803 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1804 m->m_pkthdr.len += padlen;
1805 m->m_len = m->m_pkthdr.len;
1808 error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1809 *m_head, segs, &nsegs, 0);
1810 if (error == EFBIG) {
1811 m = m_collapse(*m_head, M_NOWAIT, SIS_MAXTXSEGS);
1818 error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1819 *m_head, segs, &nsegs, 0);
1825 } else if (error != 0)
1828 /* Check for descriptor overruns. */
1829 if (sc->sis_tx_cnt + nsegs > SIS_TX_LIST_CNT - 1) {
1830 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1834 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap, BUS_DMASYNC_PREWRITE);
1837 for (i = 0; i < nsegs; i++) {
1838 f = &sc->sis_tx_list[prod];
1840 f->sis_cmdsts = htole32(segs[i].ds_len |
1843 f->sis_cmdsts = htole32(segs[i].ds_len |
1844 SIS_CMDSTS_OWN | SIS_CMDSTS_MORE);
1845 f->sis_ptr = htole32(SIS_ADDR_LO(segs[i].ds_addr));
1846 SIS_INC(prod, SIS_TX_LIST_CNT);
1850 /* Update producer index. */
1851 sc->sis_tx_prod = prod;
1853 /* Remove MORE flag on the last descriptor. */
1854 prod = (prod - 1) & (SIS_TX_LIST_CNT - 1);
1855 f = &sc->sis_tx_list[prod];
1856 f->sis_cmdsts &= ~htole32(SIS_CMDSTS_MORE);
1858 /* Lastly transfer ownership of packet to the controller. */
1859 f = &sc->sis_tx_list[frag];
1860 f->sis_cmdsts |= htole32(SIS_CMDSTS_OWN);
1862 /* Swap the last and the first dmamaps. */
1863 map = txd->tx_dmamap;
1864 txd->tx_dmamap = sc->sis_txdesc[prod].tx_dmamap;
1865 sc->sis_txdesc[prod].tx_dmamap = map;
1866 sc->sis_txdesc[prod].tx_m = *m_head;
1874 struct sis_softc *sc;
1876 sc = if_getsoftc(ifp);
1883 sis_startl(if_t ifp)
1885 struct sis_softc *sc;
1886 struct mbuf *m_head;
1889 sc = if_getsoftc(ifp);
1891 SIS_LOCK_ASSERT(sc);
1893 if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1894 IFF_DRV_RUNNING || (sc->sis_flags & SIS_FLAG_LINK) == 0)
1897 for (queued = 0; !if_sendq_empty(ifp) &&
1898 sc->sis_tx_cnt < SIS_TX_LIST_CNT - 4;) {
1899 m_head = if_dequeue(ifp);
1903 if (sis_encap(sc, &m_head) != 0) {
1906 if_sendq_prepend(ifp, m_head);
1907 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1914 * If there's a BPF listener, bounce a copy of this frame
1917 BPF_MTAP(ifp, m_head);
1922 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1923 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1924 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
1927 * Set a timeout in case the chip goes out to lunch.
1929 sc->sis_watchdog_timer = 5;
1936 struct sis_softc *sc = xsc;
1944 sis_initl(struct sis_softc *sc)
1946 if_t ifp = sc->sis_ifp;
1947 struct mii_data *mii;
1950 SIS_LOCK_ASSERT(sc);
1952 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
1956 * Cancel pending I/O and free all RX/TX buffers.
1960 * Reset the chip to a known state.
1964 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
1966 * Configure 400usec of interrupt holdoff. This is based
1967 * on empirical tests on a Soekris 4801.
1969 CSR_WRITE_4(sc, NS_IHR, 0x100 | 4);
1973 mii = device_get_softc(sc->sis_miibus);
1975 /* Set MAC address */
1976 eaddr = if_getlladdr(sc->sis_ifp);
1977 if (sc->sis_type == SIS_TYPE_83815) {
1978 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
1979 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
1980 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
1981 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
1982 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
1983 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
1985 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
1986 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
1987 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
1988 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
1989 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
1990 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
1993 /* Init circular TX/RX lists. */
1994 if (sis_ring_init(sc) != 0) {
1995 device_printf(sc->sis_dev,
1996 "initialization failed: no memory for rx buffers\n");
2001 if (sc->sis_type == SIS_TYPE_83815) {
2002 if (sc->sis_manual_pad != 0)
2003 sc->sis_flags |= SIS_FLAG_MANUAL_PAD;
2005 sc->sis_flags &= ~SIS_FLAG_MANUAL_PAD;
2009 * Short Cable Receive Errors (MP21.E)
2010 * also: Page 78 of the DP83815 data sheet (september 2002 version)
2011 * recommends the following register settings "for optimum
2012 * performance." for rev 15C. Set this also for 15D parts as
2013 * they require it in practice.
2015 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr <= NS_SRR_15D) {
2016 CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
2017 CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
2018 /* set val for c2 */
2019 CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
2021 CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
2022 /* rais SD off, from 4 to c */
2023 CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
2024 CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
2030 * Load the address of the RX and TX lists.
2032 CSR_WRITE_4(sc, SIS_RX_LISTPTR, SIS_ADDR_LO(sc->sis_rx_paddr));
2033 CSR_WRITE_4(sc, SIS_TX_LISTPTR, SIS_ADDR_LO(sc->sis_tx_paddr));
2035 /* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
2036 * the PCI bus. When this bit is set, the Max DMA Burst Size
2037 * for TX/RX DMA should be no larger than 16 double words.
2039 if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
2040 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
2042 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
2045 /* Accept Long Packets for VLAN support */
2046 SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
2049 * Assume 100Mbps link, actual MAC configuration is done
2050 * after getting a valid link.
2052 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
2055 * Enable interrupts.
2057 CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
2058 #ifdef DEVICE_POLLING
2060 * ... only enable interrupts if we are not polling, make sure
2061 * they are off otherwise.
2063 if (if_getcapenable(ifp) & IFCAP_POLLING)
2064 CSR_WRITE_4(sc, SIS_IER, 0);
2067 CSR_WRITE_4(sc, SIS_IER, 1);
2069 /* Clear MAC disable. */
2070 SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
2072 sc->sis_flags &= ~SIS_FLAG_LINK;
2075 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
2076 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
2078 callout_reset(&sc->sis_stat_ch, hz, sis_tick, sc);
2082 * Set media options.
2085 sis_ifmedia_upd(if_t ifp)
2087 struct sis_softc *sc;
2088 struct mii_data *mii;
2089 struct mii_softc *miisc;
2092 sc = if_getsoftc(ifp);
2095 mii = device_get_softc(sc->sis_miibus);
2096 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
2098 error = mii_mediachg(mii);
2105 * Report current media status.
2108 sis_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
2110 struct sis_softc *sc;
2111 struct mii_data *mii;
2113 sc = if_getsoftc(ifp);
2116 mii = device_get_softc(sc->sis_miibus);
2118 ifmr->ifm_active = mii->mii_media_active;
2119 ifmr->ifm_status = mii->mii_media_status;
2124 sis_ioctl(if_t ifp, u_long command, caddr_t data)
2126 struct sis_softc *sc = if_getsoftc(ifp);
2127 struct ifreq *ifr = (struct ifreq *) data;
2128 struct mii_data *mii;
2129 int error = 0, mask;
2134 if (if_getflags(ifp) & IFF_UP) {
2135 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0 &&
2136 ((if_getflags(ifp) ^ sc->sis_if_flags) &
2137 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2141 } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2143 sc->sis_if_flags = if_getflags(ifp);
2149 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
2155 mii = device_get_softc(sc->sis_miibus);
2156 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2160 mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
2161 #ifdef DEVICE_POLLING
2162 if ((mask & IFCAP_POLLING) != 0 &&
2163 (IFCAP_POLLING & if_getcapabilities(ifp)) != 0) {
2164 if_togglecapenable(ifp, IFCAP_POLLING);
2165 if ((IFCAP_POLLING & if_getcapenable(ifp)) != 0) {
2166 error = ether_poll_register(sis_poll, ifp);
2171 /* Disable interrupts. */
2172 CSR_WRITE_4(sc, SIS_IER, 0);
2174 error = ether_poll_deregister(ifp);
2175 /* Enable interrupts. */
2176 CSR_WRITE_4(sc, SIS_IER, 1);
2179 #endif /* DEVICE_POLLING */
2180 if ((mask & IFCAP_WOL) != 0 &&
2181 (if_getcapabilities(ifp) & IFCAP_WOL) != 0) {
2182 if ((mask & IFCAP_WOL_UCAST) != 0)
2183 if_togglecapenable(ifp, IFCAP_WOL_UCAST);
2184 if ((mask & IFCAP_WOL_MCAST) != 0)
2185 if_togglecapenable(ifp, IFCAP_WOL_MCAST);
2186 if ((mask & IFCAP_WOL_MAGIC) != 0)
2187 if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
2192 error = ether_ioctl(ifp, command, data);
2200 sis_watchdog(struct sis_softc *sc)
2203 SIS_LOCK_ASSERT(sc);
2205 if (sc->sis_watchdog_timer == 0 || --sc->sis_watchdog_timer >0)
2208 device_printf(sc->sis_dev, "watchdog timeout\n");
2209 if_inc_counter(sc->sis_ifp, IFCOUNTER_OERRORS, 1);
2211 if_setdrvflagbits(sc->sis_ifp, 0, IFF_DRV_RUNNING);
2214 if (!if_sendq_empty(sc->sis_ifp))
2215 sis_startl(sc->sis_ifp);
2219 * Stop the adapter and free any mbufs allocated to the
2223 sis_stop(struct sis_softc *sc)
2226 struct sis_rxdesc *rxd;
2227 struct sis_txdesc *txd;
2230 SIS_LOCK_ASSERT(sc);
2233 sc->sis_watchdog_timer = 0;
2235 callout_stop(&sc->sis_stat_ch);
2237 if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
2238 CSR_WRITE_4(sc, SIS_IER, 0);
2239 CSR_WRITE_4(sc, SIS_IMR, 0);
2240 CSR_READ_4(sc, SIS_ISR); /* clear any interrupts already pending */
2241 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
2243 CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
2244 CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2246 sc->sis_flags &= ~SIS_FLAG_LINK;
2249 * Free data in the RX lists.
2251 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2252 rxd = &sc->sis_rxdesc[i];
2253 if (rxd->rx_m != NULL) {
2254 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
2255 BUS_DMASYNC_POSTREAD);
2256 bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
2263 * Free the TX list buffers.
2265 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2266 txd = &sc->sis_txdesc[i];
2267 if (txd->tx_m != NULL) {
2268 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
2269 BUS_DMASYNC_POSTWRITE);
2270 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
2278 * Stop all chip I/O so that the kernel's probe routines don't
2279 * get confused by errant DMAs when rebooting.
2282 sis_shutdown(device_t dev)
2285 return (sis_suspend(dev));
2289 sis_suspend(device_t dev)
2291 struct sis_softc *sc;
2293 sc = device_get_softc(dev);
2302 sis_resume(device_t dev)
2304 struct sis_softc *sc;
2307 sc = device_get_softc(dev);
2310 if ((if_getflags(ifp) & IFF_UP) != 0) {
2311 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2319 sis_wol(struct sis_softc *sc)
2327 if ((if_getcapenable(ifp) & IFCAP_WOL) == 0)
2330 if (sc->sis_type == SIS_TYPE_83815) {
2332 CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2334 /* Configure WOL events. */
2335 CSR_READ_4(sc, NS_WCSR);
2337 if ((if_getcapenable(ifp) & IFCAP_WOL_UCAST) != 0)
2338 val |= NS_WCSR_WAKE_UCAST;
2339 if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
2340 val |= NS_WCSR_WAKE_MCAST;
2341 if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
2342 val |= NS_WCSR_WAKE_MAGIC;
2343 CSR_WRITE_4(sc, NS_WCSR, val);
2344 /* Enable PME and clear PMESTS. */
2345 val = CSR_READ_4(sc, NS_CLKRUN);
2346 val |= NS_CLKRUN_PMEENB | NS_CLKRUN_PMESTS;
2347 CSR_WRITE_4(sc, NS_CLKRUN, val);
2348 /* Enable silent RX mode. */
2349 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
2351 if (pci_find_cap(sc->sis_dev, PCIY_PMG, &pmc) != 0)
2354 if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
2355 val |= SIS_PWRMAN_WOL_MAGIC;
2356 CSR_WRITE_4(sc, SIS_PWRMAN_CTL, val);
2358 pmstat = pci_read_config(sc->sis_dev,
2359 pmc + PCIR_POWER_STATUS, 2);
2360 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2361 if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
2362 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2363 pci_write_config(sc->sis_dev,
2364 pmc + PCIR_POWER_STATUS, pmstat, 2);
2369 sis_add_sysctls(struct sis_softc *sc)
2371 struct sysctl_ctx_list *ctx;
2372 struct sysctl_oid_list *children;
2374 ctx = device_get_sysctl_ctx(sc->sis_dev);
2375 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sis_dev));
2378 * Unlike most other controllers, NS DP83815/DP83816 controllers
2379 * seem to pad with 0xFF when it encounter short frames. According
2380 * to RFC 1042 the pad bytes should be 0x00. Turning this tunable
2381 * on will have driver pad manully but it's disabled by default
2382 * because it will consume extra CPU cycles for short frames.
2384 sc->sis_manual_pad = 0;
2385 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "manual_pad",
2386 CTLFLAG_RWTUN, &sc->sis_manual_pad, 0, "Manually pad short frames");
2389 static device_method_t sis_methods[] = {
2390 /* Device interface */
2391 DEVMETHOD(device_probe, sis_probe),
2392 DEVMETHOD(device_attach, sis_attach),
2393 DEVMETHOD(device_detach, sis_detach),
2394 DEVMETHOD(device_shutdown, sis_shutdown),
2395 DEVMETHOD(device_suspend, sis_suspend),
2396 DEVMETHOD(device_resume, sis_resume),
2399 DEVMETHOD(miibus_readreg, sis_miibus_readreg),
2400 DEVMETHOD(miibus_writereg, sis_miibus_writereg),
2401 DEVMETHOD(miibus_statchg, sis_miibus_statchg),
2406 static driver_t sis_driver = {
2409 sizeof(struct sis_softc)
2412 DRIVER_MODULE(sis, pci, sis_driver, 0, 0);
2413 DRIVER_MODULE(miibus, sis, miibus_driver, 0, 0);