2 * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 1997, 1998, 1999
4 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
39 * available from http://www.sis.com.tw.
41 * This driver also supports the NatSemi DP83815. Datasheets are
42 * available from http://www.national.com.
44 * Written by Bill Paul <wpaul@ee.columbia.edu>
45 * Electrical Engineering Department
46 * Columbia University, New York City
49 * The SiS 900 is a fairly simple chip. It uses bus master DMA with
50 * simple TX and RX descriptors of 3 longwords in size. The receiver
51 * has a single perfect filter entry for the station address and a
52 * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
53 * transceiver while the 7016 requires an external transceiver chip.
54 * Both chips offer the standard bit-bang MII interface as well as
55 * an enchanced PHY interface which simplifies accessing MII registers.
57 * The only downside to this chipset is that RX descriptors must be
61 #ifdef HAVE_KERNEL_OPTION_HEADERS
62 #include "opt_device_polling.h"
65 #include <sys/param.h>
66 #include <sys/systm.h>
68 #include <sys/endian.h>
69 #include <sys/kernel.h>
71 #include <sys/malloc.h>
73 #include <sys/module.h>
74 #include <sys/socket.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83 #include <net/if_types.h>
84 #include <net/if_vlan_var.h>
88 #include <machine/bus.h>
89 #include <machine/resource.h>
93 #include <dev/mii/mii.h>
94 #include <dev/mii/miivar.h>
96 #include <dev/pci/pcireg.h>
97 #include <dev/pci/pcivar.h>
99 #define SIS_USEIOSPACE
101 #include <dev/sis/if_sisreg.h>
103 MODULE_DEPEND(sis, pci, 1, 1, 1);
104 MODULE_DEPEND(sis, ether, 1, 1, 1);
105 MODULE_DEPEND(sis, miibus, 1, 1, 1);
107 /* "device miibus" required. See GENERIC if you get errors here. */
108 #include "miibus_if.h"
110 #define SIS_LOCK(_sc) mtx_lock(&(_sc)->sis_mtx)
111 #define SIS_UNLOCK(_sc) mtx_unlock(&(_sc)->sis_mtx)
112 #define SIS_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sis_mtx, MA_OWNED)
115 * register space access macros
117 #define CSR_WRITE_4(sc, reg, val) bus_write_4(sc->sis_res[0], reg, val)
119 #define CSR_READ_4(sc, reg) bus_read_4(sc->sis_res[0], reg)
121 #define CSR_READ_2(sc, reg) bus_read_2(sc->sis_res[0], reg)
124 * Various supported device vendors/types and their names.
126 static struct sis_type sis_devs[] = {
127 { SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
128 { SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
129 { NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP8381[56] 10/100BaseTX" },
133 static int sis_detach(device_t);
134 static __inline void sis_discard_rxbuf(struct sis_rxdesc *);
135 static int sis_dma_alloc(struct sis_softc *);
136 static void sis_dma_free(struct sis_softc *);
137 static int sis_dma_ring_alloc(struct sis_softc *, bus_size_t, bus_size_t,
138 bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
139 static void sis_dmamap_cb(void *, bus_dma_segment_t *, int, int);
140 #ifndef __NO_STRICT_ALIGNMENT
141 static __inline void sis_fixup_rx(struct mbuf *);
143 static void sis_ifmedia_sts(struct ifnet *, struct ifmediareq *);
144 static int sis_ifmedia_upd(struct ifnet *);
145 static void sis_init(void *);
146 static void sis_initl(struct sis_softc *);
147 static void sis_intr(void *);
148 static int sis_ioctl(struct ifnet *, u_long, caddr_t);
149 static int sis_newbuf(struct sis_softc *, struct sis_rxdesc *);
150 static int sis_resume(device_t);
151 static int sis_rxeof(struct sis_softc *);
152 static void sis_start(struct ifnet *);
153 static void sis_startl(struct ifnet *);
154 static void sis_stop(struct sis_softc *);
155 static int sis_suspend(device_t);
156 static void sis_add_sysctls(struct sis_softc *);
157 static void sis_watchdog(struct sis_softc *);
158 static void sis_wol(struct sis_softc *);
161 static struct resource_spec sis_res_spec[] = {
162 #ifdef SIS_USEIOSPACE
163 { SYS_RES_IOPORT, SIS_PCI_LOIO, RF_ACTIVE},
165 { SYS_RES_MEMORY, SIS_PCI_LOMEM, RF_ACTIVE},
167 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE},
171 #define SIS_SETBIT(sc, reg, x) \
172 CSR_WRITE_4(sc, reg, \
173 CSR_READ_4(sc, reg) | (x))
175 #define SIS_CLRBIT(sc, reg, x) \
176 CSR_WRITE_4(sc, reg, \
177 CSR_READ_4(sc, reg) & ~(x))
180 CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
183 CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
186 * Routine to reverse the bits in a word. Stolen almost
187 * verbatim from /usr/games/fortune.
190 sis_reverse(uint16_t n)
192 n = ((n >> 1) & 0x5555) | ((n << 1) & 0xaaaa);
193 n = ((n >> 2) & 0x3333) | ((n << 2) & 0xcccc);
194 n = ((n >> 4) & 0x0f0f) | ((n << 4) & 0xf0f0);
195 n = ((n >> 8) & 0x00ff) | ((n << 8) & 0xff00);
201 sis_delay(struct sis_softc *sc)
205 for (idx = (300 / 33) + 1; idx > 0; idx--)
206 CSR_READ_4(sc, SIS_CSR);
210 sis_eeprom_idle(struct sis_softc *sc)
214 SIO_SET(SIS_EECTL_CSEL);
216 SIO_SET(SIS_EECTL_CLK);
219 for (i = 0; i < 25; i++) {
220 SIO_CLR(SIS_EECTL_CLK);
222 SIO_SET(SIS_EECTL_CLK);
226 SIO_CLR(SIS_EECTL_CLK);
228 SIO_CLR(SIS_EECTL_CSEL);
230 CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
234 * Send a read command and address to the EEPROM, check for ACK.
237 sis_eeprom_putbyte(struct sis_softc *sc, int addr)
241 d = addr | SIS_EECMD_READ;
244 * Feed in each bit and stobe the clock.
246 for (i = 0x400; i; i >>= 1) {
248 SIO_SET(SIS_EECTL_DIN);
250 SIO_CLR(SIS_EECTL_DIN);
253 SIO_SET(SIS_EECTL_CLK);
255 SIO_CLR(SIS_EECTL_CLK);
261 * Read a word of data stored in the EEPROM at address 'addr.'
264 sis_eeprom_getword(struct sis_softc *sc, int addr, uint16_t *dest)
269 /* Force EEPROM to idle state. */
272 /* Enter EEPROM access mode. */
274 SIO_CLR(SIS_EECTL_CLK);
276 SIO_SET(SIS_EECTL_CSEL);
280 * Send address of word we want to read.
282 sis_eeprom_putbyte(sc, addr);
285 * Start reading bits from EEPROM.
287 for (i = 0x8000; i; i >>= 1) {
288 SIO_SET(SIS_EECTL_CLK);
290 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
293 SIO_CLR(SIS_EECTL_CLK);
297 /* Turn off EEPROM access mode. */
304 * Read a sequence of words from the EEPROM.
307 sis_read_eeprom(struct sis_softc *sc, caddr_t dest, int off, int cnt, int swap)
310 uint16_t word = 0, *ptr;
312 for (i = 0; i < cnt; i++) {
313 sis_eeprom_getword(sc, off + i, &word);
314 ptr = (uint16_t *)(dest + (i * 2));
322 #if defined(__i386__) || defined(__amd64__)
324 sis_find_bridge(device_t dev)
326 devclass_t pci_devclass;
327 device_t *pci_devices;
329 device_t *pci_children;
330 int pci_childcount = 0;
331 device_t *busp, *childp;
332 device_t child = NULL;
335 if ((pci_devclass = devclass_find("pci")) == NULL)
338 devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
340 for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
341 if (device_get_children(*busp, &pci_children, &pci_childcount))
343 for (j = 0, childp = pci_children;
344 j < pci_childcount; j++, childp++) {
345 if (pci_get_vendor(*childp) == SIS_VENDORID &&
346 pci_get_device(*childp) == 0x0008) {
348 free(pci_children, M_TEMP);
352 free(pci_children, M_TEMP);
356 free(pci_devices, M_TEMP);
361 sis_read_cmos(struct sis_softc *sc, device_t dev, caddr_t dest, int off, int cnt)
366 bus_space_tag_t btag;
368 bridge = sis_find_bridge(dev);
371 reg = pci_read_config(bridge, 0x48, 1);
372 pci_write_config(bridge, 0x48, reg|0x40, 1);
375 #if defined(__i386__)
376 btag = I386_BUS_SPACE_IO;
377 #elif defined(__amd64__)
378 btag = AMD64_BUS_SPACE_IO;
381 for (i = 0; i < cnt; i++) {
382 bus_space_write_1(btag, 0x0, 0x70, i + off);
383 *(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
386 pci_write_config(bridge, 0x48, reg & ~0x40, 1);
390 sis_read_mac(struct sis_softc *sc, device_t dev, caddr_t dest)
392 uint32_t filtsave, csrsave;
394 filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
395 csrsave = CSR_READ_4(sc, SIS_CSR);
397 CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
398 CSR_WRITE_4(sc, SIS_CSR, 0);
400 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
402 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
403 ((uint16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
404 CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
405 ((uint16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
406 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
407 ((uint16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
409 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
410 CSR_WRITE_4(sc, SIS_CSR, csrsave);
415 * Sync the PHYs by setting data bit and strobing the clock 32 times.
418 sis_mii_sync(struct sis_softc *sc)
422 SIO_SET(SIS_MII_DIR|SIS_MII_DATA);
424 for (i = 0; i < 32; i++) {
425 SIO_SET(SIS_MII_CLK);
427 SIO_CLR(SIS_MII_CLK);
433 * Clock a series of bits through the MII.
436 sis_mii_send(struct sis_softc *sc, uint32_t bits, int cnt)
440 SIO_CLR(SIS_MII_CLK);
442 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
444 SIO_SET(SIS_MII_DATA);
446 SIO_CLR(SIS_MII_DATA);
449 SIO_CLR(SIS_MII_CLK);
451 SIO_SET(SIS_MII_CLK);
456 * Read an PHY register through the MII.
459 sis_mii_readreg(struct sis_softc *sc, struct sis_mii_frame *frame)
464 * Set up frame for RX.
466 frame->mii_stdelim = SIS_MII_STARTDELIM;
467 frame->mii_opcode = SIS_MII_READOP;
468 frame->mii_turnaround = 0;
474 SIO_SET(SIS_MII_DIR);
479 * Send command/address info.
481 sis_mii_send(sc, frame->mii_stdelim, 2);
482 sis_mii_send(sc, frame->mii_opcode, 2);
483 sis_mii_send(sc, frame->mii_phyaddr, 5);
484 sis_mii_send(sc, frame->mii_regaddr, 5);
487 SIO_CLR((SIS_MII_CLK|SIS_MII_DATA));
489 SIO_SET(SIS_MII_CLK);
493 SIO_CLR(SIS_MII_DIR);
496 SIO_CLR(SIS_MII_CLK);
498 ack = CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA;
499 SIO_SET(SIS_MII_CLK);
503 * Now try reading data bits. If the ack failed, we still
504 * need to clock through 16 cycles to keep the PHY(s) in sync.
507 for (i = 0; i < 16; i++) {
508 SIO_CLR(SIS_MII_CLK);
510 SIO_SET(SIS_MII_CLK);
516 for (i = 0x8000; i; i >>= 1) {
517 SIO_CLR(SIS_MII_CLK);
520 if (CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA)
521 frame->mii_data |= i;
524 SIO_SET(SIS_MII_CLK);
530 SIO_CLR(SIS_MII_CLK);
532 SIO_SET(SIS_MII_CLK);
541 * Write to a PHY register through the MII.
544 sis_mii_writereg(struct sis_softc *sc, struct sis_mii_frame *frame)
548 * Set up frame for TX.
551 frame->mii_stdelim = SIS_MII_STARTDELIM;
552 frame->mii_opcode = SIS_MII_WRITEOP;
553 frame->mii_turnaround = SIS_MII_TURNAROUND;
556 * Turn on data output.
558 SIO_SET(SIS_MII_DIR);
562 sis_mii_send(sc, frame->mii_stdelim, 2);
563 sis_mii_send(sc, frame->mii_opcode, 2);
564 sis_mii_send(sc, frame->mii_phyaddr, 5);
565 sis_mii_send(sc, frame->mii_regaddr, 5);
566 sis_mii_send(sc, frame->mii_turnaround, 2);
567 sis_mii_send(sc, frame->mii_data, 16);
570 SIO_SET(SIS_MII_CLK);
572 SIO_CLR(SIS_MII_CLK);
578 SIO_CLR(SIS_MII_DIR);
584 sis_miibus_readreg(device_t dev, int phy, int reg)
586 struct sis_softc *sc;
587 struct sis_mii_frame frame;
589 sc = device_get_softc(dev);
591 if (sc->sis_type == SIS_TYPE_83815) {
595 * The NatSemi chip can take a while after
596 * a reset to come ready, during which the BMSR
597 * returns a value of 0. This is *never* supposed
598 * to happen: some of the BMSR bits are meant to
599 * be hardwired in the on position, and this can
600 * confuse the miibus code a bit during the probe
601 * and attach phase. So we make an effort to check
602 * for this condition and wait for it to clear.
604 if (!CSR_READ_4(sc, NS_BMSR))
606 return CSR_READ_4(sc, NS_BMCR + (reg * 4));
610 * Chipsets < SIS_635 seem not to be able to read/write
611 * through mdio. Use the enhanced PHY access register
614 if (sc->sis_type == SIS_TYPE_900 &&
615 sc->sis_rev < SIS_REV_635) {
621 CSR_WRITE_4(sc, SIS_PHYCTL,
622 (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
623 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
625 for (i = 0; i < SIS_TIMEOUT; i++) {
626 if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
630 if (i == SIS_TIMEOUT) {
631 device_printf(sc->sis_dev, "PHY failed to come ready\n");
635 val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
642 bzero((char *)&frame, sizeof(frame));
644 frame.mii_phyaddr = phy;
645 frame.mii_regaddr = reg;
646 sis_mii_readreg(sc, &frame);
648 return (frame.mii_data);
653 sis_miibus_writereg(device_t dev, int phy, int reg, int data)
655 struct sis_softc *sc;
656 struct sis_mii_frame frame;
658 sc = device_get_softc(dev);
660 if (sc->sis_type == SIS_TYPE_83815) {
663 CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
668 * Chipsets < SIS_635 seem not to be able to read/write
669 * through mdio. Use the enhanced PHY access register
672 if (sc->sis_type == SIS_TYPE_900 &&
673 sc->sis_rev < SIS_REV_635) {
679 CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
680 (reg << 6) | SIS_PHYOP_WRITE);
681 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
683 for (i = 0; i < SIS_TIMEOUT; i++) {
684 if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
688 if (i == SIS_TIMEOUT)
689 device_printf(sc->sis_dev, "PHY failed to come ready\n");
691 bzero((char *)&frame, sizeof(frame));
693 frame.mii_phyaddr = phy;
694 frame.mii_regaddr = reg;
695 frame.mii_data = data;
696 sis_mii_writereg(sc, &frame);
702 sis_miibus_statchg(device_t dev)
704 struct sis_softc *sc;
705 struct mii_data *mii;
709 sc = device_get_softc(dev);
712 mii = device_get_softc(sc->sis_miibus);
714 if (mii == NULL || ifp == NULL ||
715 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
718 sc->sis_flags &= ~SIS_FLAG_LINK;
719 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
720 (IFM_ACTIVE | IFM_AVALID)) {
721 switch (IFM_SUBTYPE(mii->mii_media_active)) {
723 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
724 sc->sis_flags |= SIS_FLAG_LINK;
727 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
728 sc->sis_flags |= SIS_FLAG_LINK;
735 if ((sc->sis_flags & SIS_FLAG_LINK) == 0) {
737 * Stopping MACs seem to reset SIS_TX_LISTPTR and
738 * SIS_RX_LISTPTR which in turn requires resetting
739 * TX/RX buffers. So just don't do anything for
745 /* Set full/half duplex mode. */
746 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
747 SIS_SETBIT(sc, SIS_TX_CFG,
748 (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
749 SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
751 SIS_CLRBIT(sc, SIS_TX_CFG,
752 (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
753 SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
756 if (sc->sis_type == SIS_TYPE_83816) {
758 * MPII03.D: Half Duplex Excessive Collisions.
759 * Also page 49 in 83816 manual
761 SIS_SETBIT(sc, SIS_TX_CFG, SIS_TXCFG_MPII03D);
764 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr < NS_SRR_16A &&
765 IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
767 * Short Cable Receive Errors (MP21.E)
769 CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
770 reg = CSR_READ_4(sc, NS_PHY_DSPCFG) & 0xfff;
771 CSR_WRITE_4(sc, NS_PHY_DSPCFG, reg | 0x1000);
773 reg = CSR_READ_4(sc, NS_PHY_TDATA) & 0xff;
774 if ((reg & 0x0080) == 0 || (reg > 0xd8 && reg <= 0xff)) {
775 device_printf(sc->sis_dev,
776 "Applying short cable fix (reg=%x)\n", reg);
777 CSR_WRITE_4(sc, NS_PHY_TDATA, 0x00e8);
778 SIS_SETBIT(sc, NS_PHY_DSPCFG, 0x20);
780 CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
782 /* Enable TX/RX MACs. */
783 SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
784 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE | SIS_CSR_RX_ENABLE);
788 sis_mchash(struct sis_softc *sc, const uint8_t *addr)
792 /* Compute CRC for the address value. */
793 crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
796 * return the filter bit position
798 * The NatSemi chip has a 512-bit filter, which is
799 * different than the SiS, so we special-case it.
801 if (sc->sis_type == SIS_TYPE_83815)
803 else if (sc->sis_rev >= SIS_REV_635 ||
804 sc->sis_rev == SIS_REV_900B)
811 sis_setmulti_ns(struct sis_softc *sc)
814 struct ifmultiaddr *ifma;
815 uint32_t h = 0, i, filtsave;
820 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
821 SIS_CLRBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
822 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
827 * We have to explicitly enable the multicast hash table
828 * on the NatSemi chip if we want to use it, which we do.
830 SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
831 SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
833 filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
835 /* first, zot all the existing hash bits */
836 for (i = 0; i < 32; i++) {
837 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + (i*2));
838 CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
842 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
843 if (ifma->ifma_addr->sa_family != AF_LINK)
846 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
849 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
852 SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
854 if_maddr_runlock(ifp);
856 CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
860 sis_setmulti_sis(struct sis_softc *sc)
863 struct ifmultiaddr *ifma;
864 uint32_t h, i, n, ctl;
869 /* hash table size */
870 if (sc->sis_rev >= SIS_REV_635 ||
871 sc->sis_rev == SIS_REV_900B)
876 ctl = CSR_READ_4(sc, SIS_RXFILT_CTL) & SIS_RXFILTCTL_ENABLE;
878 if (ifp->if_flags & IFF_BROADCAST)
879 ctl |= SIS_RXFILTCTL_BROAD;
881 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
882 ctl |= SIS_RXFILTCTL_ALLMULTI;
883 if (ifp->if_flags & IFF_PROMISC)
884 ctl |= SIS_RXFILTCTL_BROAD|SIS_RXFILTCTL_ALLPHYS;
885 for (i = 0; i < n; i++)
888 for (i = 0; i < n; i++)
892 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
893 if (ifma->ifma_addr->sa_family != AF_LINK)
896 LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
897 hashes[h >> 4] |= 1 << (h & 0xf);
900 if_maddr_runlock(ifp);
902 ctl |= SIS_RXFILTCTL_ALLMULTI;
903 for (i = 0; i < n; i++)
908 for (i = 0; i < n; i++) {
909 CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
910 CSR_WRITE_4(sc, SIS_RXFILT_DATA, hashes[i]);
913 CSR_WRITE_4(sc, SIS_RXFILT_CTL, ctl);
917 sis_reset(struct sis_softc *sc)
921 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
923 for (i = 0; i < SIS_TIMEOUT; i++) {
924 if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
928 if (i == SIS_TIMEOUT)
929 device_printf(sc->sis_dev, "reset never completed\n");
931 /* Wait a little while for the chip to get its brains in order. */
935 * If this is a NetSemi chip, make sure to clear
938 if (sc->sis_type == SIS_TYPE_83815) {
939 CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
940 CSR_WRITE_4(sc, NS_CLKRUN, 0);
942 /* Disable WOL functions. */
943 CSR_WRITE_4(sc, SIS_PWRMAN_CTL, 0);
948 * Probe for an SiS chip. Check the PCI vendor and device
949 * IDs against our list and return a device name if we find a match.
952 sis_probe(device_t dev)
958 while (t->sis_name != NULL) {
959 if ((pci_get_vendor(dev) == t->sis_vid) &&
960 (pci_get_device(dev) == t->sis_did)) {
961 device_set_desc(dev, t->sis_name);
962 return (BUS_PROBE_DEFAULT);
971 * Attach the interface. Allocate softc structures, do ifmedia
972 * setup and ethernet/BPF attach.
975 sis_attach(device_t dev)
977 u_char eaddr[ETHER_ADDR_LEN];
978 struct sis_softc *sc;
980 int error = 0, pmc, waittime = 0;
983 sc = device_get_softc(dev);
987 mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
989 callout_init_mtx(&sc->sis_stat_ch, &sc->sis_mtx, 0);
991 if (pci_get_device(dev) == SIS_DEVICEID_900)
992 sc->sis_type = SIS_TYPE_900;
993 if (pci_get_device(dev) == SIS_DEVICEID_7016)
994 sc->sis_type = SIS_TYPE_7016;
995 if (pci_get_vendor(dev) == NS_VENDORID)
996 sc->sis_type = SIS_TYPE_83815;
998 sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
1000 * Map control/status registers.
1002 pci_enable_busmaster(dev);
1004 error = bus_alloc_resources(dev, sis_res_spec, sc->sis_res);
1006 device_printf(dev, "couldn't allocate resources\n");
1010 /* Reset the adapter. */
1013 if (sc->sis_type == SIS_TYPE_900 &&
1014 (sc->sis_rev == SIS_REV_635 ||
1015 sc->sis_rev == SIS_REV_900B)) {
1016 SIO_SET(SIS_CFG_RND_CNT);
1017 SIO_SET(SIS_CFG_PERR_DETECT);
1021 * Get station address from the EEPROM.
1023 switch (pci_get_vendor(dev)) {
1025 sc->sis_srr = CSR_READ_4(sc, NS_SRR);
1027 /* We can't update the device description, so spew */
1028 if (sc->sis_srr == NS_SRR_15C)
1029 device_printf(dev, "Silicon Revision: DP83815C\n");
1030 else if (sc->sis_srr == NS_SRR_15D)
1031 device_printf(dev, "Silicon Revision: DP83815D\n");
1032 else if (sc->sis_srr == NS_SRR_16A)
1033 device_printf(dev, "Silicon Revision: DP83816A\n");
1035 device_printf(dev, "Silicon Revision %x\n", sc->sis_srr);
1038 * Reading the MAC address out of the EEPROM on
1039 * the NatSemi chip takes a bit more work than
1040 * you'd expect. The address spans 4 16-bit words,
1041 * with the first word containing only a single bit.
1042 * You have to shift everything over one bit to
1043 * get it aligned properly. Also, the bits are
1044 * stored backwards (the LSB is really the MSB,
1045 * and so on) so you have to reverse them in order
1046 * to get the MAC address into the form we want.
1047 * Why? Who the hell knows.
1052 sis_read_eeprom(sc, (caddr_t)&tmp,
1053 NS_EE_NODEADDR, 4, 0);
1055 /* Shift everything over one bit. */
1056 tmp[3] = tmp[3] >> 1;
1057 tmp[3] |= tmp[2] << 15;
1058 tmp[2] = tmp[2] >> 1;
1059 tmp[2] |= tmp[1] << 15;
1060 tmp[1] = tmp[1] >> 1;
1061 tmp[1] |= tmp[0] << 15;
1063 /* Now reverse all the bits. */
1064 tmp[3] = sis_reverse(tmp[3]);
1065 tmp[2] = sis_reverse(tmp[2]);
1066 tmp[1] = sis_reverse(tmp[1]);
1068 eaddr[0] = (tmp[1] >> 0) & 0xFF;
1069 eaddr[1] = (tmp[1] >> 8) & 0xFF;
1070 eaddr[2] = (tmp[2] >> 0) & 0xFF;
1071 eaddr[3] = (tmp[2] >> 8) & 0xFF;
1072 eaddr[4] = (tmp[3] >> 0) & 0xFF;
1073 eaddr[5] = (tmp[3] >> 8) & 0xFF;
1078 #if defined(__i386__) || defined(__amd64__)
1080 * If this is a SiS 630E chipset with an embedded
1081 * SiS 900 controller, we have to read the MAC address
1082 * from the APC CMOS RAM. Our method for doing this
1083 * is very ugly since we have to reach out and grab
1084 * ahold of hardware for which we cannot properly
1085 * allocate resources. This code is only compiled on
1086 * the i386 architecture since the SiS 630E chipset
1087 * is for x86 motherboards only. Note that there are
1088 * a lot of magic numbers in this hack. These are
1089 * taken from SiS's Linux driver. I'd like to replace
1090 * them with proper symbolic definitions, but that
1091 * requires some datasheets that I don't have access
1094 if (sc->sis_rev == SIS_REV_630S ||
1095 sc->sis_rev == SIS_REV_630E ||
1096 sc->sis_rev == SIS_REV_630EA1)
1097 sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
1099 else if (sc->sis_rev == SIS_REV_635 ||
1100 sc->sis_rev == SIS_REV_630ET)
1101 sis_read_mac(sc, dev, (caddr_t)&eaddr);
1102 else if (sc->sis_rev == SIS_REV_96x) {
1103 /* Allow to read EEPROM from LAN. It is shared
1104 * between a 1394 controller and the NIC and each
1105 * time we access it, we need to set SIS_EECMD_REQ.
1107 SIO_SET(SIS_EECMD_REQ);
1108 for (waittime = 0; waittime < SIS_TIMEOUT;
1110 /* Force EEPROM to idle state. */
1111 sis_eeprom_idle(sc);
1112 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1113 sis_read_eeprom(sc, (caddr_t)&eaddr,
1114 SIS_EE_NODEADDR, 3, 0);
1120 * Set SIS_EECTL_CLK to high, so a other master
1121 * can operate on the i2c bus.
1123 SIO_SET(SIS_EECTL_CLK);
1124 /* Refuse EEPROM access by LAN */
1125 SIO_SET(SIS_EECMD_DONE);
1128 sis_read_eeprom(sc, (caddr_t)&eaddr,
1129 SIS_EE_NODEADDR, 3, 0);
1133 sis_add_sysctls(sc);
1135 /* Allocate DMA'able memory. */
1136 if ((error = sis_dma_alloc(sc)) != 0)
1139 ifp = sc->sis_ifp = if_alloc(IFT_ETHER);
1141 device_printf(dev, "can not if_alloc()\n");
1146 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1147 ifp->if_mtu = ETHERMTU;
1148 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1149 ifp->if_ioctl = sis_ioctl;
1150 ifp->if_start = sis_start;
1151 ifp->if_init = sis_init;
1152 IFQ_SET_MAXLEN(&ifp->if_snd, SIS_TX_LIST_CNT - 1);
1153 ifp->if_snd.ifq_drv_maxlen = SIS_TX_LIST_CNT - 1;
1154 IFQ_SET_READY(&ifp->if_snd);
1156 if (pci_find_extcap(sc->sis_dev, PCIY_PMG, &pmc) == 0) {
1157 if (sc->sis_type == SIS_TYPE_83815)
1158 ifp->if_capabilities |= IFCAP_WOL;
1160 ifp->if_capabilities |= IFCAP_WOL_MAGIC;
1161 ifp->if_capenable = ifp->if_capabilities;
1167 error = mii_attach(dev, &sc->sis_miibus, ifp, sis_ifmedia_upd,
1168 sis_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1170 device_printf(dev, "attaching PHYs failed\n");
1175 * Call MI attach routine.
1177 ether_ifattach(ifp, eaddr);
1180 * Tell the upper layer(s) we support long frames.
1182 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1183 ifp->if_capabilities |= IFCAP_VLAN_MTU;
1184 ifp->if_capenable = ifp->if_capabilities;
1185 #ifdef DEVICE_POLLING
1186 ifp->if_capabilities |= IFCAP_POLLING;
1189 /* Hook interrupt last to avoid having to lock softc */
1190 error = bus_setup_intr(dev, sc->sis_res[1], INTR_TYPE_NET | INTR_MPSAFE,
1191 NULL, sis_intr, sc, &sc->sis_intrhand);
1194 device_printf(dev, "couldn't set up irq\n");
1195 ether_ifdetach(ifp);
1207 * Shutdown hardware and free up resources. This can be called any
1208 * time after the mutex has been initialized. It is called in both
1209 * the error case in attach and the normal detach case so it needs
1210 * to be careful about only freeing resources that have actually been
1214 sis_detach(device_t dev)
1216 struct sis_softc *sc;
1219 sc = device_get_softc(dev);
1220 KASSERT(mtx_initialized(&sc->sis_mtx), ("sis mutex not initialized"));
1223 #ifdef DEVICE_POLLING
1224 if (ifp->if_capenable & IFCAP_POLLING)
1225 ether_poll_deregister(ifp);
1228 /* These should only be active if attach succeeded. */
1229 if (device_is_attached(dev)) {
1233 callout_drain(&sc->sis_stat_ch);
1234 ether_ifdetach(ifp);
1237 device_delete_child(dev, sc->sis_miibus);
1238 bus_generic_detach(dev);
1240 if (sc->sis_intrhand)
1241 bus_teardown_intr(dev, sc->sis_res[1], sc->sis_intrhand);
1242 bus_release_resources(dev, sis_res_spec, sc->sis_res);
1249 mtx_destroy(&sc->sis_mtx);
1254 struct sis_dmamap_arg {
1255 bus_addr_t sis_busaddr;
1259 sis_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1261 struct sis_dmamap_arg *ctx;
1266 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1268 ctx = (struct sis_dmamap_arg *)arg;
1269 ctx->sis_busaddr = segs[0].ds_addr;
1273 sis_dma_ring_alloc(struct sis_softc *sc, bus_size_t alignment,
1274 bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
1275 bus_addr_t *paddr, const char *msg)
1277 struct sis_dmamap_arg ctx;
1280 error = bus_dma_tag_create(sc->sis_parent_tag, alignment, 0,
1281 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, maxsize, 1,
1282 maxsize, 0, NULL, NULL, tag);
1284 device_printf(sc->sis_dev,
1285 "could not create %s dma tag\n", msg);
1288 /* Allocate DMA'able memory for ring. */
1289 error = bus_dmamem_alloc(*tag, (void **)ring,
1290 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
1292 device_printf(sc->sis_dev,
1293 "could not allocate DMA'able memory for %s\n", msg);
1296 /* Load the address of the ring. */
1297 ctx.sis_busaddr = 0;
1298 error = bus_dmamap_load(*tag, *map, *ring, maxsize, sis_dmamap_cb,
1299 &ctx, BUS_DMA_NOWAIT);
1301 device_printf(sc->sis_dev,
1302 "could not load DMA'able memory for %s\n", msg);
1305 *paddr = ctx.sis_busaddr;
1310 sis_dma_alloc(struct sis_softc *sc)
1312 struct sis_rxdesc *rxd;
1313 struct sis_txdesc *txd;
1316 /* Allocate the parent bus DMA tag appropriate for PCI. */
1317 error = bus_dma_tag_create(bus_get_dma_tag(sc->sis_dev),
1318 1, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1319 NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
1320 0, NULL, NULL, &sc->sis_parent_tag);
1322 device_printf(sc->sis_dev,
1323 "could not allocate parent dma tag\n");
1327 /* Create RX ring. */
1328 error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_RX_LIST_SZ,
1329 &sc->sis_rx_list_tag, (uint8_t **)&sc->sis_rx_list,
1330 &sc->sis_rx_list_map, &sc->sis_rx_paddr, "RX ring");
1334 /* Create TX ring. */
1335 error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_TX_LIST_SZ,
1336 &sc->sis_tx_list_tag, (uint8_t **)&sc->sis_tx_list,
1337 &sc->sis_tx_list_map, &sc->sis_tx_paddr, "TX ring");
1341 /* Create tag for RX mbufs. */
1342 error = bus_dma_tag_create(sc->sis_parent_tag, SIS_RX_BUF_ALIGN, 0,
1343 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
1344 MCLBYTES, 0, NULL, NULL, &sc->sis_rx_tag);
1346 device_printf(sc->sis_dev, "could not allocate RX dma tag\n");
1350 /* Create tag for TX mbufs. */
1351 error = bus_dma_tag_create(sc->sis_parent_tag, 1, 0,
1352 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1353 MCLBYTES * SIS_MAXTXSEGS, SIS_MAXTXSEGS, MCLBYTES, 0, NULL, NULL,
1356 device_printf(sc->sis_dev, "could not allocate TX dma tag\n");
1360 /* Create DMA maps for RX buffers. */
1361 error = bus_dmamap_create(sc->sis_rx_tag, 0, &sc->sis_rx_sparemap);
1363 device_printf(sc->sis_dev,
1364 "can't create spare DMA map for RX\n");
1367 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1368 rxd = &sc->sis_rxdesc[i];
1370 error = bus_dmamap_create(sc->sis_rx_tag, 0, &rxd->rx_dmamap);
1372 device_printf(sc->sis_dev,
1373 "can't create DMA map for RX\n");
1378 /* Create DMA maps for TX buffers. */
1379 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1380 txd = &sc->sis_txdesc[i];
1382 error = bus_dmamap_create(sc->sis_tx_tag, 0, &txd->tx_dmamap);
1384 device_printf(sc->sis_dev,
1385 "can't create DMA map for TX\n");
1394 sis_dma_free(struct sis_softc *sc)
1396 struct sis_rxdesc *rxd;
1397 struct sis_txdesc *txd;
1400 /* Destroy DMA maps for RX buffers. */
1401 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1402 rxd = &sc->sis_rxdesc[i];
1404 bus_dmamap_destroy(sc->sis_rx_tag, rxd->rx_dmamap);
1406 if (sc->sis_rx_sparemap)
1407 bus_dmamap_destroy(sc->sis_rx_tag, sc->sis_rx_sparemap);
1409 /* Destroy DMA maps for TX buffers. */
1410 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1411 txd = &sc->sis_txdesc[i];
1413 bus_dmamap_destroy(sc->sis_tx_tag, txd->tx_dmamap);
1417 bus_dma_tag_destroy(sc->sis_rx_tag);
1419 bus_dma_tag_destroy(sc->sis_tx_tag);
1421 /* Destroy RX ring. */
1422 if (sc->sis_rx_list_map)
1423 bus_dmamap_unload(sc->sis_rx_list_tag, sc->sis_rx_list_map);
1424 if (sc->sis_rx_list_map && sc->sis_rx_list)
1425 bus_dmamem_free(sc->sis_rx_list_tag, sc->sis_rx_list,
1426 sc->sis_rx_list_map);
1428 if (sc->sis_rx_list_tag)
1429 bus_dma_tag_destroy(sc->sis_rx_list_tag);
1431 /* Destroy TX ring. */
1432 if (sc->sis_tx_list_map)
1433 bus_dmamap_unload(sc->sis_tx_list_tag, sc->sis_tx_list_map);
1435 if (sc->sis_tx_list_map && sc->sis_tx_list)
1436 bus_dmamem_free(sc->sis_tx_list_tag, sc->sis_tx_list,
1437 sc->sis_tx_list_map);
1439 if (sc->sis_tx_list_tag)
1440 bus_dma_tag_destroy(sc->sis_tx_list_tag);
1442 /* Destroy the parent tag. */
1443 if (sc->sis_parent_tag)
1444 bus_dma_tag_destroy(sc->sis_parent_tag);
1448 * Initialize the TX and RX descriptors and allocate mbufs for them. Note that
1449 * we arrange the descriptors in a closed ring, so that the last descriptor
1450 * points back to the first.
1453 sis_ring_init(struct sis_softc *sc)
1455 struct sis_rxdesc *rxd;
1456 struct sis_txdesc *txd;
1460 bzero(&sc->sis_tx_list[0], SIS_TX_LIST_SZ);
1461 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1462 txd = &sc->sis_txdesc[i];
1464 if (i == SIS_TX_LIST_CNT - 1)
1465 next = SIS_TX_RING_ADDR(sc, 0);
1467 next = SIS_TX_RING_ADDR(sc, i + 1);
1468 sc->sis_tx_list[i].sis_next = htole32(SIS_ADDR_LO(next));
1470 sc->sis_tx_prod = sc->sis_tx_cons = sc->sis_tx_cnt = 0;
1471 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1472 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1474 sc->sis_rx_cons = 0;
1475 bzero(&sc->sis_rx_list[0], SIS_RX_LIST_SZ);
1476 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1477 rxd = &sc->sis_rxdesc[i];
1478 rxd->rx_desc = &sc->sis_rx_list[i];
1479 if (i == SIS_RX_LIST_CNT - 1)
1480 next = SIS_RX_RING_ADDR(sc, 0);
1482 next = SIS_RX_RING_ADDR(sc, i + 1);
1483 rxd->rx_desc->sis_next = htole32(SIS_ADDR_LO(next));
1484 error = sis_newbuf(sc, rxd);
1488 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1489 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1495 * Initialize an RX descriptor and attach an MBUF cluster.
1498 sis_newbuf(struct sis_softc *sc, struct sis_rxdesc *rxd)
1501 bus_dma_segment_t segs[1];
1505 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1508 m->m_len = m->m_pkthdr.len = SIS_RXLEN;
1509 #ifndef __NO_STRICT_ALIGNMENT
1510 m_adj(m, SIS_RX_BUF_ALIGN);
1513 if (bus_dmamap_load_mbuf_sg(sc->sis_rx_tag, sc->sis_rx_sparemap, m,
1514 segs, &nsegs, 0) != 0) {
1518 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1520 if (rxd->rx_m != NULL) {
1521 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
1522 BUS_DMASYNC_POSTREAD);
1523 bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
1525 map = rxd->rx_dmamap;
1526 rxd->rx_dmamap = sc->sis_rx_sparemap;
1527 sc->sis_rx_sparemap = map;
1528 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap, BUS_DMASYNC_PREREAD);
1530 rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1531 rxd->rx_desc->sis_ptr = htole32(SIS_ADDR_LO(segs[0].ds_addr));
1535 static __inline void
1536 sis_discard_rxbuf(struct sis_rxdesc *rxd)
1539 rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1542 #ifndef __NO_STRICT_ALIGNMENT
1543 static __inline void
1544 sis_fixup_rx(struct mbuf *m)
1546 uint16_t *src, *dst;
1549 src = mtod(m, uint16_t *);
1550 dst = src - (SIS_RX_BUF_ALIGN - ETHER_ALIGN) / sizeof(*src);
1552 for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1555 m->m_data -= SIS_RX_BUF_ALIGN - ETHER_ALIGN;
1560 * A frame has been uploaded: pass the resulting mbuf chain up to
1561 * the higher level protocols.
1564 sis_rxeof(struct sis_softc *sc)
1568 struct sis_rxdesc *rxd;
1569 struct sis_desc *cur_rx;
1570 int prog, rx_cons, rx_npkts = 0, total_len;
1573 SIS_LOCK_ASSERT(sc);
1575 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1576 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1578 rx_cons = sc->sis_rx_cons;
1581 for (prog = 0; (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1582 SIS_INC(rx_cons, SIS_RX_LIST_CNT), prog++) {
1583 #ifdef DEVICE_POLLING
1584 if (ifp->if_capenable & IFCAP_POLLING) {
1585 if (sc->rxcycles <= 0)
1590 cur_rx = &sc->sis_rx_list[rx_cons];
1591 rxstat = le32toh(cur_rx->sis_cmdsts);
1592 if ((rxstat & SIS_CMDSTS_OWN) == 0)
1594 rxd = &sc->sis_rxdesc[rx_cons];
1596 total_len = (rxstat & SIS_CMDSTS_BUFLEN) - ETHER_CRC_LEN;
1597 if ((ifp->if_capenable & IFCAP_VLAN_MTU) != 0 &&
1598 total_len <= (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN -
1600 rxstat &= ~SIS_RXSTAT_GIANT;
1601 if (SIS_RXSTAT_ERROR(rxstat) != 0) {
1603 if (rxstat & SIS_RXSTAT_COLL)
1604 ifp->if_collisions++;
1605 sis_discard_rxbuf(rxd);
1609 /* Add a new receive buffer to the ring. */
1611 if (sis_newbuf(sc, rxd) != 0) {
1613 sis_discard_rxbuf(rxd);
1617 /* No errors; receive the packet. */
1618 m->m_pkthdr.len = m->m_len = total_len;
1619 #ifndef __NO_STRICT_ALIGNMENT
1621 * On architectures without alignment problems we try to
1622 * allocate a new buffer for the receive ring, and pass up
1623 * the one where the packet is already, saving the expensive
1629 m->m_pkthdr.rcvif = ifp;
1632 (*ifp->if_input)(ifp, m);
1638 sc->sis_rx_cons = rx_cons;
1639 bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1640 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1647 * A frame was downloaded to the chip. It's safe for us to clean up
1652 sis_txeof(struct sis_softc *sc)
1655 struct sis_desc *cur_tx;
1656 struct sis_txdesc *txd;
1657 uint32_t cons, txstat;
1659 SIS_LOCK_ASSERT(sc);
1661 cons = sc->sis_tx_cons;
1662 if (cons == sc->sis_tx_prod)
1666 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1667 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1670 * Go through our tx list and free mbufs for those
1671 * frames that have been transmitted.
1673 for (; cons != sc->sis_tx_prod; SIS_INC(cons, SIS_TX_LIST_CNT)) {
1674 cur_tx = &sc->sis_tx_list[cons];
1675 txstat = le32toh(cur_tx->sis_cmdsts);
1676 if ((txstat & SIS_CMDSTS_OWN) != 0)
1678 txd = &sc->sis_txdesc[cons];
1679 if (txd->tx_m != NULL) {
1680 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
1681 BUS_DMASYNC_POSTWRITE);
1682 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1685 if ((txstat & SIS_CMDSTS_PKT_OK) != 0) {
1687 ifp->if_collisions +=
1688 (txstat & SIS_TXSTAT_COLLCNT) >> 16;
1691 if (txstat & SIS_TXSTAT_EXCESSCOLLS)
1692 ifp->if_collisions++;
1693 if (txstat & SIS_TXSTAT_OUTOFWINCOLL)
1694 ifp->if_collisions++;
1698 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1700 sc->sis_tx_cons = cons;
1701 if (sc->sis_tx_cnt == 0)
1702 sc->sis_watchdog_timer = 0;
1708 struct sis_softc *sc;
1709 struct mii_data *mii;
1713 SIS_LOCK_ASSERT(sc);
1716 mii = device_get_softc(sc->sis_miibus);
1719 if ((sc->sis_flags & SIS_FLAG_LINK) == 0)
1720 sis_miibus_statchg(sc->sis_dev);
1721 callout_reset(&sc->sis_stat_ch, hz, sis_tick, sc);
1724 #ifdef DEVICE_POLLING
1725 static poll_handler_t sis_poll;
1728 sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1730 struct sis_softc *sc = ifp->if_softc;
1734 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1740 * On the sis, reading the status register also clears it.
1741 * So before returning to intr mode we must make sure that all
1742 * possible pending sources of interrupts have been served.
1743 * In practice this means run to completion the *eof routines,
1744 * and then call the interrupt routine
1746 sc->rxcycles = count;
1747 rx_npkts = sis_rxeof(sc);
1749 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1752 if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1755 /* Reading the ISR register clears all interrupts. */
1756 status = CSR_READ_4(sc, SIS_ISR);
1758 if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1761 if (status & (SIS_ISR_RX_IDLE))
1762 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1764 if (status & SIS_ISR_SYSERR) {
1765 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1773 #endif /* DEVICE_POLLING */
1778 struct sis_softc *sc;
1786 #ifdef DEVICE_POLLING
1787 if (ifp->if_capenable & IFCAP_POLLING) {
1793 /* Reading the ISR register clears all interrupts. */
1794 status = CSR_READ_4(sc, SIS_ISR);
1795 if ((status & SIS_INTRS) == 0) {
1801 /* Disable interrupts. */
1802 CSR_WRITE_4(sc, SIS_IER, 0);
1804 for (;(status & SIS_INTRS) != 0;) {
1805 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1808 (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
1809 SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
1812 if (status & (SIS_ISR_RX_DESC_OK | SIS_ISR_RX_OK |
1813 SIS_ISR_RX_ERR | SIS_ISR_RX_IDLE))
1816 if (status & SIS_ISR_RX_OFLOW)
1819 if (status & (SIS_ISR_RX_IDLE))
1820 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1822 if (status & SIS_ISR_SYSERR) {
1823 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1828 status = CSR_READ_4(sc, SIS_ISR);
1831 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1832 /* Re-enable interrupts. */
1833 CSR_WRITE_4(sc, SIS_IER, 1);
1835 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1843 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1844 * pointers to the fragment pointers.
1847 sis_encap(struct sis_softc *sc, struct mbuf **m_head)
1850 struct sis_txdesc *txd;
1852 bus_dma_segment_t segs[SIS_MAXTXSEGS];
1854 int error, i, frag, nsegs, prod;
1857 prod = sc->sis_tx_prod;
1858 txd = &sc->sis_txdesc[prod];
1859 if ((sc->sis_flags & SIS_FLAG_MANUAL_PAD) != 0 &&
1860 (*m_head)->m_pkthdr.len < SIS_MIN_FRAMELEN) {
1862 padlen = SIS_MIN_FRAMELEN - m->m_pkthdr.len;
1863 if (M_WRITABLE(m) == 0) {
1864 /* Get a writable copy. */
1865 m = m_dup(*m_head, M_DONTWAIT);
1873 if (m->m_next != NULL || M_TRAILINGSPACE(m) < padlen) {
1874 m = m_defrag(m, M_DONTWAIT);
1882 * Manually pad short frames, and zero the pad space
1883 * to avoid leaking data.
1885 bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1886 m->m_pkthdr.len += padlen;
1887 m->m_len = m->m_pkthdr.len;
1890 error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1891 *m_head, segs, &nsegs, 0);
1892 if (error == EFBIG) {
1893 m = m_collapse(*m_head, M_DONTWAIT, SIS_MAXTXSEGS);
1900 error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1901 *m_head, segs, &nsegs, 0);
1907 } else if (error != 0)
1910 /* Check for descriptor overruns. */
1911 if (sc->sis_tx_cnt + nsegs > SIS_TX_LIST_CNT - 1) {
1912 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1916 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap, BUS_DMASYNC_PREWRITE);
1919 for (i = 0; i < nsegs; i++) {
1920 f = &sc->sis_tx_list[prod];
1922 f->sis_cmdsts = htole32(segs[i].ds_len |
1925 f->sis_cmdsts = htole32(segs[i].ds_len |
1926 SIS_CMDSTS_OWN | SIS_CMDSTS_MORE);
1927 f->sis_ptr = htole32(SIS_ADDR_LO(segs[i].ds_addr));
1928 SIS_INC(prod, SIS_TX_LIST_CNT);
1932 /* Update producer index. */
1933 sc->sis_tx_prod = prod;
1935 /* Remove MORE flag on the last descriptor. */
1936 prod = (prod - 1) & (SIS_TX_LIST_CNT - 1);
1937 f = &sc->sis_tx_list[prod];
1938 f->sis_cmdsts &= ~htole32(SIS_CMDSTS_MORE);
1940 /* Lastly transfer ownership of packet to the controller. */
1941 f = &sc->sis_tx_list[frag];
1942 f->sis_cmdsts |= htole32(SIS_CMDSTS_OWN);
1944 /* Swap the last and the first dmamaps. */
1945 map = txd->tx_dmamap;
1946 txd->tx_dmamap = sc->sis_txdesc[prod].tx_dmamap;
1947 sc->sis_txdesc[prod].tx_dmamap = map;
1948 sc->sis_txdesc[prod].tx_m = *m_head;
1954 sis_start(struct ifnet *ifp)
1956 struct sis_softc *sc;
1965 sis_startl(struct ifnet *ifp)
1967 struct sis_softc *sc;
1968 struct mbuf *m_head;
1973 SIS_LOCK_ASSERT(sc);
1975 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1976 IFF_DRV_RUNNING || (sc->sis_flags & SIS_FLAG_LINK) == 0)
1979 for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
1980 sc->sis_tx_cnt < SIS_TX_LIST_CNT - 4;) {
1981 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1985 if (sis_encap(sc, &m_head) != 0) {
1988 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1989 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1996 * If there's a BPF listener, bounce a copy of this frame
1999 BPF_MTAP(ifp, m_head);
2004 bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
2005 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2006 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
2009 * Set a timeout in case the chip goes out to lunch.
2011 sc->sis_watchdog_timer = 5;
2018 struct sis_softc *sc = xsc;
2026 sis_initl(struct sis_softc *sc)
2028 struct ifnet *ifp = sc->sis_ifp;
2029 struct mii_data *mii;
2032 SIS_LOCK_ASSERT(sc);
2034 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2038 * Cancel pending I/O and free all RX/TX buffers.
2042 * Reset the chip to a known state.
2046 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
2048 * Configure 400usec of interrupt holdoff. This is based
2049 * on emperical tests on a Soekris 4801.
2051 CSR_WRITE_4(sc, NS_IHR, 0x100 | 4);
2055 mii = device_get_softc(sc->sis_miibus);
2057 /* Set MAC address */
2058 eaddr = IF_LLADDR(sc->sis_ifp);
2059 if (sc->sis_type == SIS_TYPE_83815) {
2060 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
2061 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
2062 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
2063 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
2064 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
2065 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
2067 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
2068 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
2069 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
2070 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
2071 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
2072 CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
2075 /* Init circular TX/RX lists. */
2076 if (sis_ring_init(sc) != 0) {
2077 device_printf(sc->sis_dev,
2078 "initialization failed: no memory for rx buffers\n");
2083 if (sc->sis_type == SIS_TYPE_83815 || sc->sis_type == SIS_TYPE_83816) {
2084 if (sc->sis_manual_pad != 0)
2085 sc->sis_flags |= SIS_FLAG_MANUAL_PAD;
2087 sc->sis_flags &= ~SIS_FLAG_MANUAL_PAD;
2091 * Short Cable Receive Errors (MP21.E)
2092 * also: Page 78 of the DP83815 data sheet (september 2002 version)
2093 * recommends the following register settings "for optimum
2094 * performance." for rev 15C. Set this also for 15D parts as
2095 * they require it in practice.
2097 if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr <= NS_SRR_15D) {
2098 CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
2099 CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
2100 /* set val for c2 */
2101 CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
2103 CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
2104 /* rais SD off, from 4 to c */
2105 CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
2106 CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
2110 * For the NatSemi chip, we have to explicitly enable the
2111 * reception of ARP frames, as well as turn on the 'perfect
2112 * match' filter where we store the station address, otherwise
2113 * we won't receive unicasts meant for this host.
2115 if (sc->sis_type == SIS_TYPE_83815) {
2116 SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
2117 SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
2120 /* If we want promiscuous mode, set the allframes bit. */
2121 if (ifp->if_flags & IFF_PROMISC) {
2122 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
2124 SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
2128 * Set the capture broadcast bit to capture broadcast frames.
2130 if (ifp->if_flags & IFF_BROADCAST) {
2131 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
2133 SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
2137 * Load the multicast filter.
2139 if (sc->sis_type == SIS_TYPE_83815)
2140 sis_setmulti_ns(sc);
2142 sis_setmulti_sis(sc);
2144 /* Turn the receive filter on */
2145 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
2148 * Load the address of the RX and TX lists.
2150 CSR_WRITE_4(sc, SIS_RX_LISTPTR, SIS_ADDR_LO(sc->sis_rx_paddr));
2151 CSR_WRITE_4(sc, SIS_TX_LISTPTR, SIS_ADDR_LO(sc->sis_tx_paddr));
2153 /* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
2154 * the PCI bus. When this bit is set, the Max DMA Burst Size
2155 * for TX/RX DMA should be no larger than 16 double words.
2157 if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
2158 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
2160 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
2163 /* Accept Long Packets for VLAN support */
2164 SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
2167 * Assume 100Mbps link, actual MAC configuration is done
2168 * after getting a valid link.
2170 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
2173 * Enable interrupts.
2175 CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
2176 #ifdef DEVICE_POLLING
2178 * ... only enable interrupts if we are not polling, make sure
2179 * they are off otherwise.
2181 if (ifp->if_capenable & IFCAP_POLLING)
2182 CSR_WRITE_4(sc, SIS_IER, 0);
2185 CSR_WRITE_4(sc, SIS_IER, 1);
2187 /* Clear MAC disable. */
2188 SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
2190 sc->sis_flags &= ~SIS_FLAG_LINK;
2193 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2194 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2196 callout_reset(&sc->sis_stat_ch, hz, sis_tick, sc);
2200 * Set media options.
2203 sis_ifmedia_upd(struct ifnet *ifp)
2205 struct sis_softc *sc;
2206 struct mii_data *mii;
2212 mii = device_get_softc(sc->sis_miibus);
2213 if (mii->mii_instance) {
2214 struct mii_softc *miisc;
2215 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
2216 mii_phy_reset(miisc);
2218 error = mii_mediachg(mii);
2225 * Report current media status.
2228 sis_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2230 struct sis_softc *sc;
2231 struct mii_data *mii;
2236 mii = device_get_softc(sc->sis_miibus);
2239 ifmr->ifm_active = mii->mii_media_active;
2240 ifmr->ifm_status = mii->mii_media_status;
2244 sis_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2246 struct sis_softc *sc = ifp->if_softc;
2247 struct ifreq *ifr = (struct ifreq *) data;
2248 struct mii_data *mii;
2249 int error = 0, mask;
2254 if (ifp->if_flags & IFF_UP) {
2255 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2256 ((ifp->if_flags ^ sc->sis_if_flags) &
2257 (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2258 if (sc->sis_type == SIS_TYPE_83815)
2259 sis_setmulti_ns(sc);
2261 sis_setmulti_sis(sc);
2264 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2267 sc->sis_if_flags = ifp->if_flags;
2274 if (sc->sis_type == SIS_TYPE_83815)
2275 sis_setmulti_ns(sc);
2277 sis_setmulti_sis(sc);
2282 mii = device_get_softc(sc->sis_miibus);
2283 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2287 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2288 #ifdef DEVICE_POLLING
2289 if ((mask & IFCAP_POLLING) != 0 &&
2290 (IFCAP_POLLING & ifp->if_capabilities) != 0) {
2291 ifp->if_capenable ^= IFCAP_POLLING;
2292 if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
2293 error = ether_poll_register(sis_poll, ifp);
2298 /* Disable interrupts. */
2299 CSR_WRITE_4(sc, SIS_IER, 0);
2301 error = ether_poll_deregister(ifp);
2302 /* Enable interrupts. */
2303 CSR_WRITE_4(sc, SIS_IER, 1);
2306 #endif /* DEVICE_POLLING */
2307 if ((mask & IFCAP_WOL) != 0 &&
2308 (ifp->if_capabilities & IFCAP_WOL) != 0) {
2309 if ((mask & IFCAP_WOL_UCAST) != 0)
2310 ifp->if_capenable ^= IFCAP_WOL_UCAST;
2311 if ((mask & IFCAP_WOL_MCAST) != 0)
2312 ifp->if_capenable ^= IFCAP_WOL_MCAST;
2313 if ((mask & IFCAP_WOL_MAGIC) != 0)
2314 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2319 error = ether_ioctl(ifp, command, data);
2327 sis_watchdog(struct sis_softc *sc)
2330 SIS_LOCK_ASSERT(sc);
2332 if (sc->sis_watchdog_timer == 0 || --sc->sis_watchdog_timer >0)
2335 device_printf(sc->sis_dev, "watchdog timeout\n");
2336 sc->sis_ifp->if_oerrors++;
2338 sc->sis_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2341 if (!IFQ_DRV_IS_EMPTY(&sc->sis_ifp->if_snd))
2342 sis_startl(sc->sis_ifp);
2346 * Stop the adapter and free any mbufs allocated to the
2350 sis_stop(struct sis_softc *sc)
2353 struct sis_rxdesc *rxd;
2354 struct sis_txdesc *txd;
2357 SIS_LOCK_ASSERT(sc);
2360 sc->sis_watchdog_timer = 0;
2362 callout_stop(&sc->sis_stat_ch);
2364 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2365 CSR_WRITE_4(sc, SIS_IER, 0);
2366 CSR_WRITE_4(sc, SIS_IMR, 0);
2367 CSR_READ_4(sc, SIS_ISR); /* clear any interrupts already pending */
2368 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
2370 CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
2371 CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2373 sc->sis_flags &= ~SIS_FLAG_LINK;
2376 * Free data in the RX lists.
2378 for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2379 rxd = &sc->sis_rxdesc[i];
2380 if (rxd->rx_m != NULL) {
2381 bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
2382 BUS_DMASYNC_POSTREAD);
2383 bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
2390 * Free the TX list buffers.
2392 for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2393 txd = &sc->sis_txdesc[i];
2394 if (txd->tx_m != NULL) {
2395 bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
2396 BUS_DMASYNC_POSTWRITE);
2397 bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
2405 * Stop all chip I/O so that the kernel's probe routines don't
2406 * get confused by errant DMAs when rebooting.
2409 sis_shutdown(device_t dev)
2412 return (sis_suspend(dev));
2416 sis_suspend(device_t dev)
2418 struct sis_softc *sc;
2420 sc = device_get_softc(dev);
2429 sis_resume(device_t dev)
2431 struct sis_softc *sc;
2434 sc = device_get_softc(dev);
2437 if ((ifp->if_flags & IFF_UP) != 0) {
2438 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2446 sis_wol(struct sis_softc *sc)
2454 if ((ifp->if_capenable & IFCAP_WOL) == 0)
2457 if (sc->sis_type == SIS_TYPE_83815) {
2459 CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2461 /* Configure WOL events. */
2462 CSR_READ_4(sc, NS_WCSR);
2464 if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2465 val |= NS_WCSR_WAKE_UCAST;
2466 if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2467 val |= NS_WCSR_WAKE_MCAST;
2468 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2469 val |= NS_WCSR_WAKE_MAGIC;
2470 CSR_WRITE_4(sc, NS_WCSR, val);
2471 /* Enable PME and clear PMESTS. */
2472 val = CSR_READ_4(sc, NS_CLKRUN);
2473 val |= NS_CLKRUN_PMEENB | NS_CLKRUN_PMESTS;
2474 CSR_WRITE_4(sc, NS_CLKRUN, val);
2475 /* Enable silent RX mode. */
2476 SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
2478 if (pci_find_extcap(sc->sis_dev, PCIY_PMG, &pmc) != 0)
2481 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2482 val |= SIS_PWRMAN_WOL_MAGIC;
2483 CSR_WRITE_4(sc, SIS_PWRMAN_CTL, val);
2485 pmstat = pci_read_config(sc->sis_dev,
2486 pmc + PCIR_POWER_STATUS, 2);
2487 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2488 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2489 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2490 pci_write_config(sc->sis_dev,
2491 pmc + PCIR_POWER_STATUS, pmstat, 2);
2496 sis_add_sysctls(struct sis_softc *sc)
2498 struct sysctl_ctx_list *ctx;
2499 struct sysctl_oid_list *children;
2503 ctx = device_get_sysctl_ctx(sc->sis_dev);
2504 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sis_dev));
2506 unit = device_get_unit(sc->sis_dev);
2508 * Unlike most other controllers, NS DP83815/DP83816 controllers
2509 * seem to pad with 0xFF when it encounter short frames. According
2510 * to RFC 1042 the pad bytes should be 0x00. Turning this tunable
2511 * on will have driver pad manully but it's disabled by default
2512 * because it will consume extra CPU cycles for short frames.
2514 sc->sis_manual_pad = 0;
2515 snprintf(tn, sizeof(tn), "dev.sis.%d.manual_pad", unit);
2516 TUNABLE_INT_FETCH(tn, &sc->sis_manual_pad);
2517 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "manual_pad",
2518 CTLFLAG_RW, &sc->sis_manual_pad, 0, "Manually pad short frames");
2521 static device_method_t sis_methods[] = {
2522 /* Device interface */
2523 DEVMETHOD(device_probe, sis_probe),
2524 DEVMETHOD(device_attach, sis_attach),
2525 DEVMETHOD(device_detach, sis_detach),
2526 DEVMETHOD(device_shutdown, sis_shutdown),
2527 DEVMETHOD(device_suspend, sis_suspend),
2528 DEVMETHOD(device_resume, sis_resume),
2531 DEVMETHOD(bus_print_child, bus_generic_print_child),
2532 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
2535 DEVMETHOD(miibus_readreg, sis_miibus_readreg),
2536 DEVMETHOD(miibus_writereg, sis_miibus_writereg),
2537 DEVMETHOD(miibus_statchg, sis_miibus_statchg),
2542 static driver_t sis_driver = {
2545 sizeof(struct sis_softc)
2548 static devclass_t sis_devclass;
2550 DRIVER_MODULE(sis, pci, sis_driver, sis_devclass, 0, 0);
2551 DRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);