if_sis.c revision 119288
150974Swpaul/*
250974Swpaul * Copyright (c) 1997, 1998, 1999
350974Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
450974Swpaul *
550974Swpaul * Redistribution and use in source and binary forms, with or without
650974Swpaul * modification, are permitted provided that the following conditions
750974Swpaul * are met:
850974Swpaul * 1. Redistributions of source code must retain the above copyright
950974Swpaul *    notice, this list of conditions and the following disclaimer.
1050974Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1150974Swpaul *    notice, this list of conditions and the following disclaimer in the
1250974Swpaul *    documentation and/or other materials provided with the distribution.
1350974Swpaul * 3. All advertising materials mentioning features or use of this software
1450974Swpaul *    must display the following acknowledgement:
1550974Swpaul *	This product includes software developed by Bill Paul.
1650974Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1750974Swpaul *    may be used to endorse or promote products derived from this software
1850974Swpaul *    without specific prior written permission.
1950974Swpaul *
2050974Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2150974Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2250974Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2350974Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2450974Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2550974Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2650974Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2750974Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2850974Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2950974Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3050974Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3150974Swpaul */
3250974Swpaul
3350974Swpaul/*
3450974Swpaul * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
3550974Swpaul * available from http://www.sis.com.tw.
3650974Swpaul *
3764963Swpaul * This driver also supports the NatSemi DP83815. Datasheets are
3864963Swpaul * available from http://www.national.com.
3964963Swpaul *
4050974Swpaul * Written by Bill Paul <wpaul@ee.columbia.edu>
4150974Swpaul * Electrical Engineering Department
4250974Swpaul * Columbia University, New York City
4350974Swpaul */
4450974Swpaul
4550974Swpaul/*
4650974Swpaul * The SiS 900 is a fairly simple chip. It uses bus master DMA with
4750974Swpaul * simple TX and RX descriptors of 3 longwords in size. The receiver
4850974Swpaul * has a single perfect filter entry for the station address and a
4950974Swpaul * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
5050974Swpaul * transceiver while the 7016 requires an external transceiver chip.
5150974Swpaul * Both chips offer the standard bit-bang MII interface as well as
5250974Swpaul * an enchanced PHY interface which simplifies accessing MII registers.
5350974Swpaul *
5450974Swpaul * The only downside to this chipset is that RX descriptors must be
5550974Swpaul * longword aligned.
5650974Swpaul */
5750974Swpaul
58113038Sobrien#include <sys/cdefs.h>
59113038Sobrien__FBSDID("$FreeBSD: head/sys/pci/if_sis.c 119288 2003-08-22 07:20:27Z imp $");
60113038Sobrien
6150974Swpaul#include <sys/param.h>
6250974Swpaul#include <sys/systm.h>
6350974Swpaul#include <sys/sockio.h>
6450974Swpaul#include <sys/mbuf.h>
6550974Swpaul#include <sys/malloc.h>
6650974Swpaul#include <sys/kernel.h>
6750974Swpaul#include <sys/socket.h>
6887059Sluigi#include <sys/sysctl.h>
6950974Swpaul
7050974Swpaul#include <net/if.h>
7150974Swpaul#include <net/if_arp.h>
7250974Swpaul#include <net/ethernet.h>
7350974Swpaul#include <net/if_dl.h>
7450974Swpaul#include <net/if_media.h>
7587390Sjhay#include <net/if_types.h>
7687390Sjhay#include <net/if_vlan_var.h>
7750974Swpaul
7850974Swpaul#include <net/bpf.h>
7950974Swpaul
8050974Swpaul#include <machine/bus_pio.h>
8150974Swpaul#include <machine/bus_memio.h>
8250974Swpaul#include <machine/bus.h>
8350974Swpaul#include <machine/resource.h>
8450974Swpaul#include <sys/bus.h>
8550974Swpaul#include <sys/rman.h>
8650974Swpaul
8750974Swpaul#include <dev/mii/mii.h>
8850974Swpaul#include <dev/mii/miivar.h>
8950974Swpaul
90119288Simp#include <dev/pci/pcireg.h>
91119288Simp#include <dev/pci/pcivar.h>
9250974Swpaul
9350974Swpaul#define SIS_USEIOSPACE
9450974Swpaul
9550974Swpaul#include <pci/if_sisreg.h>
9650974Swpaul
97113506SmdoddMODULE_DEPEND(sis, pci, 1, 1, 1);
98113506SmdoddMODULE_DEPEND(sis, ether, 1, 1, 1);
9959758SpeterMODULE_DEPEND(sis, miibus, 1, 1, 1);
10059758Speter
10151089Speter/* "controller miibus0" required.  See GENERIC if you get errors here. */
10250974Swpaul#include "miibus_if.h"
10350974Swpaul
10450974Swpaul/*
10550974Swpaul * Various supported device vendors/types and their names.
10650974Swpaul */
10750974Swpaulstatic struct sis_type sis_devs[] = {
10850974Swpaul	{ SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
10950974Swpaul	{ SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
11062672Swpaul	{ NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP83815 10/100BaseTX" },
11150974Swpaul	{ 0, 0, NULL }
11250974Swpaul};
11350974Swpaul
11492739Salfredstatic int sis_probe		(device_t);
11592739Salfredstatic int sis_attach		(device_t);
11692739Salfredstatic int sis_detach		(device_t);
11750974Swpaul
11892739Salfredstatic int sis_newbuf		(struct sis_softc *,
11992739Salfred					struct sis_desc *, struct mbuf *);
12092739Salfredstatic int sis_encap		(struct sis_softc *,
12192739Salfred					struct mbuf *, u_int32_t *);
12292739Salfredstatic void sis_rxeof		(struct sis_softc *);
12392739Salfredstatic void sis_rxeoc		(struct sis_softc *);
12492739Salfredstatic void sis_txeof		(struct sis_softc *);
12592739Salfredstatic void sis_intr		(void *);
12692739Salfredstatic void sis_tick		(void *);
12792739Salfredstatic void sis_start		(struct ifnet *);
12892739Salfredstatic int sis_ioctl		(struct ifnet *, u_long, caddr_t);
12992739Salfredstatic void sis_init		(void *);
13092739Salfredstatic void sis_stop		(struct sis_softc *);
13192739Salfredstatic void sis_watchdog		(struct ifnet *);
13292739Salfredstatic void sis_shutdown		(device_t);
13392739Salfredstatic int sis_ifmedia_upd	(struct ifnet *);
13492739Salfredstatic void sis_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
13550974Swpaul
13692739Salfredstatic u_int16_t sis_reverse	(u_int16_t);
13792739Salfredstatic void sis_delay		(struct sis_softc *);
13892739Salfredstatic void sis_eeprom_idle	(struct sis_softc *);
13992739Salfredstatic void sis_eeprom_putbyte	(struct sis_softc *, int);
14092739Salfredstatic void sis_eeprom_getword	(struct sis_softc *, int, u_int16_t *);
14192739Salfredstatic void sis_read_eeprom	(struct sis_softc *, caddr_t, int, int, int);
14272197Swpaul#ifdef __i386__
14392739Salfredstatic void sis_read_cmos	(struct sis_softc *, device_t, caddr_t,
14492739Salfred							int, int);
14592739Salfredstatic void sis_read_mac	(struct sis_softc *, device_t, caddr_t);
14692739Salfredstatic device_t sis_find_bridge	(device_t);
14772197Swpaul#endif
14872197Swpaul
149109060Smbrstatic void sis_mii_sync	(struct sis_softc *);
150109060Smbrstatic void sis_mii_send	(struct sis_softc *, u_int32_t, int);
151109060Smbrstatic int sis_mii_readreg	(struct sis_softc *, struct sis_mii_frame *);
152109060Smbrstatic int sis_mii_writereg	(struct sis_softc *, struct sis_mii_frame *);
15392739Salfredstatic int sis_miibus_readreg	(device_t, int, int);
15492739Salfredstatic int sis_miibus_writereg	(device_t, int, int, int);
15592739Salfredstatic void sis_miibus_statchg	(device_t);
15650974Swpaul
15792739Salfredstatic void sis_setmulti_sis	(struct sis_softc *);
15892739Salfredstatic void sis_setmulti_ns	(struct sis_softc *);
15992739Salfredstatic u_int32_t sis_crc	(struct sis_softc *, caddr_t);
16092739Salfredstatic void sis_reset		(struct sis_softc *);
16192739Salfredstatic int sis_list_rx_init	(struct sis_softc *);
16292739Salfredstatic int sis_list_tx_init	(struct sis_softc *);
16350974Swpaul
16492739Salfredstatic void sis_dma_map_desc_ptr	(void *, bus_dma_segment_t *, int, int);
16592739Salfredstatic void sis_dma_map_desc_next	(void *, bus_dma_segment_t *, int, int);
16692739Salfredstatic void sis_dma_map_ring		(void *, bus_dma_segment_t *, int, int);
16750974Swpaul#ifdef SIS_USEIOSPACE
16850974Swpaul#define SIS_RES			SYS_RES_IOPORT
16950974Swpaul#define SIS_RID			SIS_PCI_LOIO
17050974Swpaul#else
17151030Swpaul#define SIS_RES			SYS_RES_MEMORY
17251030Swpaul#define SIS_RID			SIS_PCI_LOMEM
17350974Swpaul#endif
17450974Swpaul
17550974Swpaulstatic device_method_t sis_methods[] = {
17650974Swpaul	/* Device interface */
17750974Swpaul	DEVMETHOD(device_probe,		sis_probe),
17850974Swpaul	DEVMETHOD(device_attach,	sis_attach),
17950974Swpaul	DEVMETHOD(device_detach,	sis_detach),
18050974Swpaul	DEVMETHOD(device_shutdown,	sis_shutdown),
18150974Swpaul
18250974Swpaul	/* bus interface */
18350974Swpaul	DEVMETHOD(bus_print_child,	bus_generic_print_child),
18450974Swpaul	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
18550974Swpaul
18650974Swpaul	/* MII interface */
18750974Swpaul	DEVMETHOD(miibus_readreg,	sis_miibus_readreg),
18850974Swpaul	DEVMETHOD(miibus_writereg,	sis_miibus_writereg),
18950974Swpaul	DEVMETHOD(miibus_statchg,	sis_miibus_statchg),
19050974Swpaul
19150974Swpaul	{ 0, 0 }
19250974Swpaul};
19350974Swpaul
19450974Swpaulstatic driver_t sis_driver = {
19551455Swpaul	"sis",
19650974Swpaul	sis_methods,
19750974Swpaul	sizeof(struct sis_softc)
19850974Swpaul};
19950974Swpaul
20050974Swpaulstatic devclass_t sis_devclass;
20150974Swpaul
202113506SmdoddDRIVER_MODULE(sis, pci, sis_driver, sis_devclass, 0, 0);
20351473SwpaulDRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);
20450974Swpaul
20550974Swpaul#define SIS_SETBIT(sc, reg, x)				\
20650974Swpaul	CSR_WRITE_4(sc, reg,				\
20750974Swpaul		CSR_READ_4(sc, reg) | (x))
20850974Swpaul
20950974Swpaul#define SIS_CLRBIT(sc, reg, x)				\
21050974Swpaul	CSR_WRITE_4(sc, reg,				\
21150974Swpaul		CSR_READ_4(sc, reg) & ~(x))
21250974Swpaul
21350974Swpaul#define SIO_SET(x)					\
21450974Swpaul	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
21550974Swpaul
21650974Swpaul#define SIO_CLR(x)					\
21750974Swpaul	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
21850974Swpaul
21981713Swpaulstatic void
22081713Swpaulsis_dma_map_desc_next(arg, segs, nseg, error)
22181713Swpaul	void *arg;
22281713Swpaul	bus_dma_segment_t *segs;
22381713Swpaul	int nseg, error;
22481713Swpaul{
22581713Swpaul	struct sis_desc	*r;
22681713Swpaul
22781713Swpaul	r = arg;
22881713Swpaul	r->sis_next = segs->ds_addr;
22981713Swpaul
23081713Swpaul	return;
23181713Swpaul}
23281713Swpaul
23381713Swpaulstatic void
23481713Swpaulsis_dma_map_desc_ptr(arg, segs, nseg, error)
23581713Swpaul	void *arg;
23681713Swpaul	bus_dma_segment_t *segs;
23781713Swpaul	int nseg, error;
23881713Swpaul{
23981713Swpaul	struct sis_desc	*r;
24081713Swpaul
24181713Swpaul	r = arg;
24281713Swpaul	r->sis_ptr = segs->ds_addr;
24381713Swpaul
24481713Swpaul	return;
24581713Swpaul}
24681713Swpaul
24781713Swpaulstatic void
24881713Swpaulsis_dma_map_ring(arg, segs, nseg, error)
24981713Swpaul	void *arg;
25081713Swpaul	bus_dma_segment_t *segs;
25181713Swpaul	int nseg, error;
25281713Swpaul{
25381713Swpaul	u_int32_t *p;
25481713Swpaul
25581713Swpaul	p = arg;
25681713Swpaul	*p = segs->ds_addr;
25781713Swpaul
25881713Swpaul	return;
25981713Swpaul}
26081713Swpaul
26162672Swpaul/*
26262672Swpaul * Routine to reverse the bits in a word. Stolen almost
26362672Swpaul * verbatim from /usr/games/fortune.
26462672Swpaul */
265102334Salfredstatic u_int16_t
266102334Salfredsis_reverse(n)
26762672Swpaul	u_int16_t		n;
26862672Swpaul{
26962672Swpaul	n = ((n >>  1) & 0x5555) | ((n <<  1) & 0xaaaa);
27062672Swpaul	n = ((n >>  2) & 0x3333) | ((n <<  2) & 0xcccc);
27162672Swpaul	n = ((n >>  4) & 0x0f0f) | ((n <<  4) & 0xf0f0);
27262672Swpaul	n = ((n >>  8) & 0x00ff) | ((n <<  8) & 0xff00);
27362672Swpaul
27462672Swpaul	return(n);
27562672Swpaul}
27662672Swpaul
277102334Salfredstatic void
278102334Salfredsis_delay(sc)
27950974Swpaul	struct sis_softc	*sc;
28050974Swpaul{
28150974Swpaul	int			idx;
28250974Swpaul
28350974Swpaul	for (idx = (300 / 33) + 1; idx > 0; idx--)
28450974Swpaul		CSR_READ_4(sc, SIS_CSR);
28550974Swpaul
28650974Swpaul	return;
28750974Swpaul}
28850974Swpaul
289102334Salfredstatic void
290102334Salfredsis_eeprom_idle(sc)
29150974Swpaul	struct sis_softc	*sc;
29250974Swpaul{
29350974Swpaul	register int		i;
29450974Swpaul
29550974Swpaul	SIO_SET(SIS_EECTL_CSEL);
29650974Swpaul	sis_delay(sc);
29750974Swpaul	SIO_SET(SIS_EECTL_CLK);
29850974Swpaul	sis_delay(sc);
29950974Swpaul
30050974Swpaul	for (i = 0; i < 25; i++) {
30150974Swpaul		SIO_CLR(SIS_EECTL_CLK);
30250974Swpaul		sis_delay(sc);
30350974Swpaul		SIO_SET(SIS_EECTL_CLK);
30450974Swpaul		sis_delay(sc);
30550974Swpaul	}
30650974Swpaul
30750974Swpaul	SIO_CLR(SIS_EECTL_CLK);
30850974Swpaul	sis_delay(sc);
30950974Swpaul	SIO_CLR(SIS_EECTL_CSEL);
31050974Swpaul	sis_delay(sc);
31150974Swpaul	CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
31250974Swpaul
31350974Swpaul	return;
31450974Swpaul}
31550974Swpaul
31650974Swpaul/*
31750974Swpaul * Send a read command and address to the EEPROM, check for ACK.
31850974Swpaul */
319102334Salfredstatic void
320102334Salfredsis_eeprom_putbyte(sc, addr)
32150974Swpaul	struct sis_softc	*sc;
32250974Swpaul	int			addr;
32350974Swpaul{
32450974Swpaul	register int		d, i;
32550974Swpaul
32650974Swpaul	d = addr | SIS_EECMD_READ;
32750974Swpaul
32850974Swpaul	/*
32950974Swpaul	 * Feed in each bit and stobe the clock.
33050974Swpaul	 */
33150974Swpaul	for (i = 0x400; i; i >>= 1) {
33250974Swpaul		if (d & i) {
33350974Swpaul			SIO_SET(SIS_EECTL_DIN);
33450974Swpaul		} else {
33550974Swpaul			SIO_CLR(SIS_EECTL_DIN);
33650974Swpaul		}
33750974Swpaul		sis_delay(sc);
33850974Swpaul		SIO_SET(SIS_EECTL_CLK);
33950974Swpaul		sis_delay(sc);
34050974Swpaul		SIO_CLR(SIS_EECTL_CLK);
34150974Swpaul		sis_delay(sc);
34250974Swpaul	}
34350974Swpaul
34450974Swpaul	return;
34550974Swpaul}
34650974Swpaul
34750974Swpaul/*
34850974Swpaul * Read a word of data stored in the EEPROM at address 'addr.'
34950974Swpaul */
350102334Salfredstatic void
351102334Salfredsis_eeprom_getword(sc, addr, dest)
35250974Swpaul	struct sis_softc	*sc;
35350974Swpaul	int			addr;
35450974Swpaul	u_int16_t		*dest;
35550974Swpaul{
35650974Swpaul	register int		i;
35750974Swpaul	u_int16_t		word = 0;
35850974Swpaul
35950974Swpaul	/* Force EEPROM to idle state. */
36050974Swpaul	sis_eeprom_idle(sc);
36150974Swpaul
36250974Swpaul	/* Enter EEPROM access mode. */
36350974Swpaul	sis_delay(sc);
36462672Swpaul	SIO_CLR(SIS_EECTL_CLK);
36562672Swpaul	sis_delay(sc);
36650974Swpaul	SIO_SET(SIS_EECTL_CSEL);
36750974Swpaul	sis_delay(sc);
36850974Swpaul
36950974Swpaul	/*
37050974Swpaul	 * Send address of word we want to read.
37150974Swpaul	 */
37250974Swpaul	sis_eeprom_putbyte(sc, addr);
37350974Swpaul
37450974Swpaul	/*
37550974Swpaul	 * Start reading bits from EEPROM.
37650974Swpaul	 */
37750974Swpaul	for (i = 0x8000; i; i >>= 1) {
37850974Swpaul		SIO_SET(SIS_EECTL_CLK);
37950974Swpaul		sis_delay(sc);
38050974Swpaul		if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
38150974Swpaul			word |= i;
38250974Swpaul		sis_delay(sc);
38350974Swpaul		SIO_CLR(SIS_EECTL_CLK);
38450974Swpaul		sis_delay(sc);
38550974Swpaul	}
38650974Swpaul
38750974Swpaul	/* Turn off EEPROM access mode. */
38850974Swpaul	sis_eeprom_idle(sc);
38950974Swpaul
39050974Swpaul	*dest = word;
39150974Swpaul
39250974Swpaul	return;
39350974Swpaul}
39450974Swpaul
39550974Swpaul/*
39650974Swpaul * Read a sequence of words from the EEPROM.
39750974Swpaul */
398102334Salfredstatic void
399102334Salfredsis_read_eeprom(sc, dest, off, cnt, swap)
40050974Swpaul	struct sis_softc	*sc;
40150974Swpaul	caddr_t			dest;
40250974Swpaul	int			off;
40350974Swpaul	int			cnt;
40450974Swpaul	int			swap;
40550974Swpaul{
40650974Swpaul	int			i;
40750974Swpaul	u_int16_t		word = 0, *ptr;
40850974Swpaul
40950974Swpaul	for (i = 0; i < cnt; i++) {
41050974Swpaul		sis_eeprom_getword(sc, off + i, &word);
41150974Swpaul		ptr = (u_int16_t *)(dest + (i * 2));
41250974Swpaul		if (swap)
41350974Swpaul			*ptr = ntohs(word);
41450974Swpaul		else
41550974Swpaul			*ptr = word;
41650974Swpaul	}
41750974Swpaul
41850974Swpaul	return;
41950974Swpaul}
42050974Swpaul
42172197Swpaul#ifdef __i386__
422102334Salfredstatic device_t
423102334Salfredsis_find_bridge(dev)
42472197Swpaul	device_t		dev;
42572197Swpaul{
42672197Swpaul	devclass_t		pci_devclass;
42772197Swpaul	device_t		*pci_devices;
42872197Swpaul	int			pci_count = 0;
42972197Swpaul	device_t		*pci_children;
43072197Swpaul	int			pci_childcount = 0;
43172197Swpaul	device_t		*busp, *childp;
43287994Sarchie	device_t		child = NULL;
43372197Swpaul	int			i, j;
43472197Swpaul
43572197Swpaul	if ((pci_devclass = devclass_find("pci")) == NULL)
43672197Swpaul		return(NULL);
43772197Swpaul
43872197Swpaul	devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
43972197Swpaul
44072197Swpaul	for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
44172197Swpaul		pci_childcount = 0;
44272197Swpaul		device_get_children(*busp, &pci_children, &pci_childcount);
44372197Swpaul		for (j = 0, childp = pci_children;
44472197Swpaul		    j < pci_childcount; j++, childp++) {
44572197Swpaul			if (pci_get_vendor(*childp) == SIS_VENDORID &&
44672197Swpaul			    pci_get_device(*childp) == 0x0008) {
44787994Sarchie				child = *childp;
44887994Sarchie				goto done;
44972197Swpaul			}
45072197Swpaul		}
45172197Swpaul	}
45272197Swpaul
45387994Sarchiedone:
45472197Swpaul	free(pci_devices, M_TEMP);
45572197Swpaul	free(pci_children, M_TEMP);
45687994Sarchie	return(child);
45772197Swpaul}
45872197Swpaul
459102334Salfredstatic void
460102334Salfredsis_read_cmos(sc, dev, dest, off, cnt)
46172197Swpaul	struct sis_softc	*sc;
46272197Swpaul	device_t		dev;
46372197Swpaul	caddr_t			dest;
46472197Swpaul	int			off;
46572197Swpaul	int			cnt;
46672197Swpaul{
46772197Swpaul	device_t		bridge;
46872197Swpaul	u_int8_t		reg;
46972197Swpaul	int			i;
47072197Swpaul	bus_space_tag_t		btag;
47172197Swpaul
47272197Swpaul	bridge = sis_find_bridge(dev);
47372197Swpaul	if (bridge == NULL)
47472197Swpaul		return;
47572197Swpaul	reg = pci_read_config(bridge, 0x48, 1);
47672197Swpaul	pci_write_config(bridge, 0x48, reg|0x40, 1);
47772197Swpaul
47872197Swpaul	/* XXX */
47972197Swpaul	btag = I386_BUS_SPACE_IO;
48072197Swpaul
48172197Swpaul	for (i = 0; i < cnt; i++) {
48272197Swpaul		bus_space_write_1(btag, 0x0, 0x70, i + off);
48372197Swpaul		*(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
48472197Swpaul	}
48572197Swpaul
48672197Swpaul	pci_write_config(bridge, 0x48, reg & ~0x40, 1);
48772197Swpaul	return;
48872197Swpaul}
48989296Swpaul
490102334Salfredstatic void
491102334Salfredsis_read_mac(sc, dev, dest)
49289296Swpaul	struct sis_softc	*sc;
49389296Swpaul	device_t		dev;
49489296Swpaul	caddr_t			dest;
49589296Swpaul{
49689296Swpaul	u_int32_t		filtsave, csrsave;
49789296Swpaul
49889296Swpaul	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
49989296Swpaul	csrsave = CSR_READ_4(sc, SIS_CSR);
50089296Swpaul
50189296Swpaul	CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
50289296Swpaul	CSR_WRITE_4(sc, SIS_CSR, 0);
50389296Swpaul
50489296Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
50589296Swpaul
50689296Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
50789296Swpaul	((u_int16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
50889296Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
50989296Swpaul	((u_int16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
51089296Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
51189296Swpaul	((u_int16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
51289296Swpaul
51389296Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
51489296Swpaul	CSR_WRITE_4(sc, SIS_CSR, csrsave);
51589296Swpaul	return;
51689296Swpaul}
51772197Swpaul#endif
51872197Swpaul
519109060Smbr/*
520109060Smbr * Sync the PHYs by setting data bit and strobing the clock 32 times.
521109060Smbr */
522109060Smbrstatic void sis_mii_sync(sc)
523109060Smbr	struct sis_softc	*sc;
524109060Smbr{
525109060Smbr	register int		i;
526109060Smbr
527109060Smbr 	SIO_SET(SIS_MII_DIR|SIS_MII_DATA);
528109060Smbr
529109060Smbr 	for (i = 0; i < 32; i++) {
530109060Smbr 		SIO_SET(SIS_MII_CLK);
531109060Smbr 		DELAY(1);
532109060Smbr 		SIO_CLR(SIS_MII_CLK);
533109060Smbr 		DELAY(1);
534109060Smbr 	}
535109060Smbr
536109060Smbr 	return;
537109060Smbr}
538109060Smbr
539109060Smbr/*
540109060Smbr * Clock a series of bits through the MII.
541109060Smbr */
542109060Smbrstatic void sis_mii_send(sc, bits, cnt)
543109060Smbr	struct sis_softc	*sc;
544109060Smbr	u_int32_t		bits;
545109060Smbr	int			cnt;
546109060Smbr{
547109060Smbr	int			i;
548109060Smbr
549109060Smbr	SIO_CLR(SIS_MII_CLK);
550109060Smbr
551109060Smbr	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
552109060Smbr		if (bits & i) {
553109060Smbr			SIO_SET(SIS_MII_DATA);
554109060Smbr		} else {
555109060Smbr			SIO_CLR(SIS_MII_DATA);
556109060Smbr		}
557109060Smbr		DELAY(1);
558109060Smbr		SIO_CLR(SIS_MII_CLK);
559109060Smbr		DELAY(1);
560109060Smbr		SIO_SET(SIS_MII_CLK);
561109060Smbr	}
562109060Smbr}
563109060Smbr
564109060Smbr/*
565109060Smbr * Read an PHY register through the MII.
566109060Smbr */
567109060Smbrstatic int sis_mii_readreg(sc, frame)
568109060Smbr	struct sis_softc	*sc;
569109060Smbr	struct sis_mii_frame	*frame;
570109060Smbr
571109060Smbr{
572109060Smbr	int			i, ack, s;
573109060Smbr
574109060Smbr	s = splimp();
575109060Smbr
576109060Smbr	/*
577109060Smbr	 * Set up frame for RX.
578109060Smbr	 */
579109060Smbr	frame->mii_stdelim = SIS_MII_STARTDELIM;
580109060Smbr	frame->mii_opcode = SIS_MII_READOP;
581109060Smbr	frame->mii_turnaround = 0;
582109060Smbr	frame->mii_data = 0;
583109060Smbr
584109060Smbr	/*
585109060Smbr 	 * Turn on data xmit.
586109060Smbr	 */
587109060Smbr	SIO_SET(SIS_MII_DIR);
588109060Smbr
589109060Smbr	sis_mii_sync(sc);
590109060Smbr
591109060Smbr	/*
592109060Smbr	 * Send command/address info.
593109060Smbr	 */
594109060Smbr	sis_mii_send(sc, frame->mii_stdelim, 2);
595109060Smbr	sis_mii_send(sc, frame->mii_opcode, 2);
596109060Smbr	sis_mii_send(sc, frame->mii_phyaddr, 5);
597109060Smbr	sis_mii_send(sc, frame->mii_regaddr, 5);
598109060Smbr
599109060Smbr	/* Idle bit */
600109060Smbr	SIO_CLR((SIS_MII_CLK|SIS_MII_DATA));
601109060Smbr	DELAY(1);
602109060Smbr	SIO_SET(SIS_MII_CLK);
603109060Smbr	DELAY(1);
604109060Smbr
605109060Smbr	/* Turn off xmit. */
606109060Smbr	SIO_CLR(SIS_MII_DIR);
607109060Smbr
608109060Smbr	/* Check for ack */
609109060Smbr	SIO_CLR(SIS_MII_CLK);
610109060Smbr	DELAY(1);
611109060Smbr	ack = CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA;
612109060Smbr	SIO_SET(SIS_MII_CLK);
613109060Smbr	DELAY(1);
614109060Smbr
615109060Smbr	/*
616109060Smbr	 * Now try reading data bits. If the ack failed, we still
617109060Smbr	 * need to clock through 16 cycles to keep the PHY(s) in sync.
618109060Smbr	 */
619109060Smbr	if (ack) {
620109060Smbr		for(i = 0; i < 16; i++) {
621109060Smbr			SIO_CLR(SIS_MII_CLK);
622109060Smbr			DELAY(1);
623109060Smbr			SIO_SET(SIS_MII_CLK);
624109060Smbr			DELAY(1);
625109060Smbr		}
626109060Smbr		goto fail;
627109060Smbr	}
628109060Smbr
629109060Smbr	for (i = 0x8000; i; i >>= 1) {
630109060Smbr		SIO_CLR(SIS_MII_CLK);
631109060Smbr		DELAY(1);
632109060Smbr		if (!ack) {
633109060Smbr			if (CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA)
634109060Smbr				frame->mii_data |= i;
635109060Smbr			DELAY(1);
636109060Smbr		}
637109060Smbr		SIO_SET(SIS_MII_CLK);
638109060Smbr		DELAY(1);
639109060Smbr	}
640109060Smbr
641109060Smbrfail:
642109060Smbr
643109060Smbr	SIO_CLR(SIS_MII_CLK);
644109060Smbr	DELAY(1);
645109060Smbr	SIO_SET(SIS_MII_CLK);
646109060Smbr	DELAY(1);
647109060Smbr
648109060Smbr	splx(s);
649109060Smbr
650109060Smbr	if (ack)
651109060Smbr		return(1);
652109060Smbr	return(0);
653109060Smbr}
654109060Smbr
655109060Smbr/*
656109060Smbr * Write to a PHY register through the MII.
657109060Smbr */
658109060Smbrstatic int sis_mii_writereg(sc, frame)
659109060Smbr	struct sis_softc	*sc;
660109060Smbr	struct sis_mii_frame	*frame;
661109060Smbr
662109060Smbr{
663109060Smbr	int			s;
664109060Smbr
665109060Smbr	 s = splimp();
666109060Smbr 	/*
667109060Smbr 	 * Set up frame for TX.
668109060Smbr 	 */
669109060Smbr
670109060Smbr 	frame->mii_stdelim = SIS_MII_STARTDELIM;
671109060Smbr 	frame->mii_opcode = SIS_MII_WRITEOP;
672109060Smbr 	frame->mii_turnaround = SIS_MII_TURNAROUND;
673109060Smbr
674109060Smbr 	/*
675109060Smbr  	 * Turn on data output.
676109060Smbr 	 */
677109060Smbr 	SIO_SET(SIS_MII_DIR);
678109060Smbr
679109060Smbr 	sis_mii_sync(sc);
680109060Smbr
681109060Smbr 	sis_mii_send(sc, frame->mii_stdelim, 2);
682109060Smbr 	sis_mii_send(sc, frame->mii_opcode, 2);
683109060Smbr 	sis_mii_send(sc, frame->mii_phyaddr, 5);
684109060Smbr 	sis_mii_send(sc, frame->mii_regaddr, 5);
685109060Smbr 	sis_mii_send(sc, frame->mii_turnaround, 2);
686109060Smbr 	sis_mii_send(sc, frame->mii_data, 16);
687109060Smbr
688109060Smbr 	/* Idle bit. */
689109060Smbr 	SIO_SET(SIS_MII_CLK);
690109060Smbr 	DELAY(1);
691109060Smbr 	SIO_CLR(SIS_MII_CLK);
692109060Smbr 	DELAY(1);
693109060Smbr
694109060Smbr 	/*
695109060Smbr 	 * Turn off xmit.
696109060Smbr 	 */
697109060Smbr 	SIO_CLR(SIS_MII_DIR);
698109060Smbr
699109060Smbr 	splx(s);
700109060Smbr
701109060Smbr 	return(0);
702109060Smbr}
703109060Smbr
704102334Salfredstatic int
705102334Salfredsis_miibus_readreg(dev, phy, reg)
70650974Swpaul	device_t		dev;
70750974Swpaul	int			phy, reg;
70850974Swpaul{
70950974Swpaul	struct sis_softc	*sc;
710109060Smbr	struct sis_mii_frame    frame;
71150974Swpaul
71250974Swpaul	sc = device_get_softc(dev);
71350974Swpaul
71462672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
71562672Swpaul		if (phy != 0)
71662672Swpaul			return(0);
71762672Swpaul		/*
71862672Swpaul		 * The NatSemi chip can take a while after
71962672Swpaul		 * a reset to come ready, during which the BMSR
72062672Swpaul		 * returns a value of 0. This is *never* supposed
72162672Swpaul		 * to happen: some of the BMSR bits are meant to
72262672Swpaul		 * be hardwired in the on position, and this can
72362672Swpaul		 * confuse the miibus code a bit during the probe
72462672Swpaul		 * and attach phase. So we make an effort to check
72562672Swpaul		 * for this condition and wait for it to clear.
72662672Swpaul		 */
72762672Swpaul		if (!CSR_READ_4(sc, NS_BMSR))
72862672Swpaul			DELAY(1000);
729109060Smbr		return CSR_READ_4(sc, NS_BMCR + (reg * 4));
73062672Swpaul	}
73162672Swpaul
732109976Smbr	/*
733109976Smbr	 * Chipsets < SIS_635 seem not to be able to read/write
734109976Smbr	 * through mdio. Use the enhanced PHY access register
735109976Smbr	 * again for them.
736109976Smbr	 */
73789296Swpaul	if (sc->sis_type == SIS_TYPE_900 &&
738109976Smbr	    sc->sis_rev < SIS_REV_635) {
739109976Smbr		int i, val = 0;
74050974Swpaul
741109976Smbr		if (phy != 0)
742109976Smbr			return(0);
74350974Swpaul
744109976Smbr		CSR_WRITE_4(sc, SIS_PHYCTL,
745109976Smbr		    (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
746109976Smbr		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
74750974Swpaul
748109976Smbr		for (i = 0; i < SIS_TIMEOUT; i++) {
749109976Smbr			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
750109976Smbr				break;
751109976Smbr		}
752109976Smbr
753109976Smbr		if (i == SIS_TIMEOUT) {
754109976Smbr			printf("sis%d: PHY failed to come ready\n",
755109976Smbr			    sc->sis_unit);
756109976Smbr			return(0);
757109976Smbr		}
758109976Smbr
759109976Smbr		val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
760109976Smbr
761109976Smbr		if (val == 0xFFFF)
762109976Smbr			return(0);
763109976Smbr
764109976Smbr		return(val);
765109976Smbr	} else {
766109976Smbr		bzero((char *)&frame, sizeof(frame));
767109976Smbr
768109976Smbr		frame.mii_phyaddr = phy;
769109976Smbr		frame.mii_regaddr = reg;
770109976Smbr		sis_mii_readreg(sc, &frame);
771109976Smbr
772109976Smbr		return(frame.mii_data);
773109976Smbr	}
77450974Swpaul}
77550974Swpaul
776102334Salfredstatic int
777102334Salfredsis_miibus_writereg(dev, phy, reg, data)
77850974Swpaul	device_t		dev;
77950974Swpaul	int			phy, reg, data;
78050974Swpaul{
78150974Swpaul	struct sis_softc	*sc;
782109060Smbr	struct sis_mii_frame	frame;
78350974Swpaul
78450974Swpaul	sc = device_get_softc(dev);
78550974Swpaul
78662672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
78762672Swpaul		if (phy != 0)
78862672Swpaul			return(0);
78962672Swpaul		CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
79062672Swpaul		return(0);
79162672Swpaul	}
79262672Swpaul
793109976Smbr	/*
794109976Smbr	 * Chipsets < SIS_635 seem not to be able to read/write
795109976Smbr	 * through mdio. Use the enhanced PHY access register
796109976Smbr	 * again for them.
797109976Smbr	 */
798109976Smbr	if (sc->sis_type == SIS_TYPE_900 &&
799109976Smbr	    sc->sis_rev < SIS_REV_635) {
800109976Smbr		int i;
80150974Swpaul
802109976Smbr		if (phy != 0)
803109976Smbr			return(0);
80450974Swpaul
805109976Smbr		CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
806109976Smbr		    (reg << 6) | SIS_PHYOP_WRITE);
807109976Smbr		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
80850974Swpaul
809109976Smbr		for (i = 0; i < SIS_TIMEOUT; i++) {
810109976Smbr			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
811109976Smbr				break;
812109976Smbr		}
81350974Swpaul
814109976Smbr		if (i == SIS_TIMEOUT)
815109976Smbr			printf("sis%d: PHY failed to come ready\n",
816109976Smbr			    sc->sis_unit);
817109976Smbr	} else {
818109976Smbr		bzero((char *)&frame, sizeof(frame));
819109976Smbr
820109976Smbr		frame.mii_phyaddr = phy;
821109976Smbr		frame.mii_regaddr = reg;
822109976Smbr		frame.mii_data = data;
823109976Smbr		sis_mii_writereg(sc, &frame);
824109976Smbr	}
82550974Swpaul	return(0);
82650974Swpaul}
82750974Swpaul
828102334Salfredstatic void
829102334Salfredsis_miibus_statchg(dev)
83050974Swpaul	device_t		dev;
83150974Swpaul{
83250974Swpaul	struct sis_softc	*sc;
83350974Swpaul
83450974Swpaul	sc = device_get_softc(dev);
83564963Swpaul	sis_init(sc);
83650974Swpaul
83750974Swpaul	return;
83850974Swpaul}
83950974Swpaul
840102334Salfredstatic u_int32_t
841102334Salfredsis_crc(sc, addr)
84262672Swpaul	struct sis_softc	*sc;
84350974Swpaul	caddr_t			addr;
84450974Swpaul{
84550974Swpaul	u_int32_t		crc, carry;
84650974Swpaul	int			i, j;
84750974Swpaul	u_int8_t		c;
84850974Swpaul
84950974Swpaul	/* Compute CRC for the address value. */
85050974Swpaul	crc = 0xFFFFFFFF; /* initial value */
85150974Swpaul
85250974Swpaul	for (i = 0; i < 6; i++) {
85350974Swpaul		c = *(addr + i);
85450974Swpaul		for (j = 0; j < 8; j++) {
85550974Swpaul			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
85650974Swpaul			crc <<= 1;
85750974Swpaul			c >>= 1;
85850974Swpaul			if (carry)
85950974Swpaul				crc = (crc ^ 0x04c11db6) | carry;
86050974Swpaul		}
86150974Swpaul	}
86250974Swpaul
86362672Swpaul	/*
86462672Swpaul	 * return the filter bit position
86562672Swpaul	 *
86662672Swpaul	 * The NatSemi chip has a 512-bit filter, which is
86762672Swpaul	 * different than the SiS, so we special-case it.
86862672Swpaul	 */
86962672Swpaul	if (sc->sis_type == SIS_TYPE_83815)
870109060Smbr		return (crc >> 23);
871109976Smbr	else if (sc->sis_rev >= SIS_REV_635 ||
872109976Smbr	    sc->sis_rev == SIS_REV_900B)
873109060Smbr		return (crc >> 24);
874109976Smbr	else
875109976Smbr		return (crc >> 25);
87650974Swpaul}
87750974Swpaul
878102334Salfredstatic void
879102334Salfredsis_setmulti_ns(sc)
88050974Swpaul	struct sis_softc	*sc;
88150974Swpaul{
88250974Swpaul	struct ifnet		*ifp;
88350974Swpaul	struct ifmultiaddr	*ifma;
88450974Swpaul	u_int32_t		h = 0, i, filtsave;
88562672Swpaul	int			bit, index;
88650974Swpaul
88750974Swpaul	ifp = &sc->arpcom.ac_if;
88850974Swpaul
88950974Swpaul	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
89062672Swpaul		SIS_CLRBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
89150974Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
89250974Swpaul		return;
89350974Swpaul	}
89450974Swpaul
89562672Swpaul	/*
89662672Swpaul	 * We have to explicitly enable the multicast hash table
89762672Swpaul	 * on the NatSemi chip if we want to use it, which we do.
89862672Swpaul	 */
89962672Swpaul	SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
90050974Swpaul	SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
90150974Swpaul
90250974Swpaul	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
90350974Swpaul
90450974Swpaul	/* first, zot all the existing hash bits */
90562672Swpaul	for (i = 0; i < 32; i++) {
90662672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + (i*2));
90762672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
90862672Swpaul	}
90962672Swpaul
91072084Sphk	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
91162672Swpaul		if (ifma->ifma_addr->sa_family != AF_LINK)
91262672Swpaul			continue;
91362672Swpaul		h = sis_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
91462672Swpaul		index = h >> 3;
91562672Swpaul		bit = h & 0x1F;
91662672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
91762672Swpaul		if (bit > 0xF)
91862672Swpaul			bit -= 0x10;
91962672Swpaul		SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
92062672Swpaul	}
92162672Swpaul
92262672Swpaul	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
92362672Swpaul
92462672Swpaul	return;
92562672Swpaul}
92662672Swpaul
927102334Salfredstatic void
928102334Salfredsis_setmulti_sis(sc)
92962672Swpaul	struct sis_softc	*sc;
93062672Swpaul{
93162672Swpaul	struct ifnet		*ifp;
93262672Swpaul	struct ifmultiaddr	*ifma;
933109060Smbr	u_int32_t		h, i, n, ctl;
934109060Smbr	u_int16_t		hashes[16];
93562672Swpaul
93662672Swpaul	ifp = &sc->arpcom.ac_if;
93762672Swpaul
938109060Smbr	/* hash table size */
939109976Smbr	if (sc->sis_rev >= SIS_REV_635 ||
940109976Smbr	    sc->sis_rev == SIS_REV_900B)
941109976Smbr		n = 16;
942109976Smbr	else
943109976Smbr		n = 8;
94462672Swpaul
945109060Smbr	ctl = CSR_READ_4(sc, SIS_RXFILT_CTL) & SIS_RXFILTCTL_ENABLE;
94662672Swpaul
947109060Smbr	if (ifp->if_flags & IFF_BROADCAST)
948109060Smbr		ctl |= SIS_RXFILTCTL_BROAD;
94962672Swpaul
950109060Smbr	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
951109060Smbr		ctl |= SIS_RXFILTCTL_ALLMULTI;
952109060Smbr		if (ifp->if_flags & IFF_PROMISC)
953109060Smbr			ctl |= SIS_RXFILTCTL_BROAD|SIS_RXFILTCTL_ALLPHYS;
954109060Smbr		for (i = 0; i < n; i++)
955109060Smbr			hashes[i] = ~0;
956109060Smbr	} else {
957109060Smbr		for (i = 0; i < n; i++)
958109060Smbr			hashes[i] = 0;
959109060Smbr		i = 0;
960109060Smbr		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
961109060Smbr			if (ifma->ifma_addr->sa_family != AF_LINK)
962109060Smbr			continue;
963109060Smbr			h = sis_crc(sc,
964109060Smbr			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
965109060Smbr			hashes[h >> 4] |= 1 << (h & 0xf);
966109060Smbr			i++;
967109060Smbr		}
968109060Smbr		if (i > n) {
969109060Smbr			ctl |= SIS_RXFILTCTL_ALLMULTI;
970109060Smbr			for (i = 0; i < n; i++)
971109060Smbr				hashes[i] = ~0;
972109060Smbr		}
97350974Swpaul	}
97450974Swpaul
975109060Smbr	for (i = 0; i < n; i++) {
976109060Smbr		CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
977109060Smbr		CSR_WRITE_4(sc, SIS_RXFILT_DATA, hashes[i]);
97850974Swpaul	}
97950974Swpaul
980109060Smbr	CSR_WRITE_4(sc, SIS_RXFILT_CTL, ctl);
98150974Swpaul}
98250974Swpaul
983102334Salfredstatic void
984102334Salfredsis_reset(sc)
98550974Swpaul	struct sis_softc	*sc;
98650974Swpaul{
98750974Swpaul	register int		i;
98850974Swpaul
98950974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
99050974Swpaul
99150974Swpaul	for (i = 0; i < SIS_TIMEOUT; i++) {
99250974Swpaul		if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
99350974Swpaul			break;
99450974Swpaul	}
99550974Swpaul
99650974Swpaul	if (i == SIS_TIMEOUT)
99750974Swpaul		printf("sis%d: reset never completed\n", sc->sis_unit);
99850974Swpaul
99950974Swpaul	/* Wait a little while for the chip to get its brains in order. */
100050974Swpaul	DELAY(1000);
100172813Swpaul
100272813Swpaul	/*
100372813Swpaul	 * If this is a NetSemi chip, make sure to clear
100472813Swpaul	 * PME mode.
100572813Swpaul	 */
100672813Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
100772813Swpaul		CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
100872813Swpaul		CSR_WRITE_4(sc, NS_CLKRUN, 0);
100972813Swpaul	}
101072813Swpaul
101150974Swpaul        return;
101250974Swpaul}
101350974Swpaul
101450974Swpaul/*
101550974Swpaul * Probe for an SiS chip. Check the PCI vendor and device
101650974Swpaul * IDs against our list and return a device name if we find a match.
101750974Swpaul */
1018102334Salfredstatic int
1019102334Salfredsis_probe(dev)
102050974Swpaul	device_t		dev;
102150974Swpaul{
102250974Swpaul	struct sis_type		*t;
102350974Swpaul
102450974Swpaul	t = sis_devs;
102550974Swpaul
102650974Swpaul	while(t->sis_name != NULL) {
102750974Swpaul		if ((pci_get_vendor(dev) == t->sis_vid) &&
102850974Swpaul		    (pci_get_device(dev) == t->sis_did)) {
102950974Swpaul			device_set_desc(dev, t->sis_name);
103050974Swpaul			return(0);
103150974Swpaul		}
103250974Swpaul		t++;
103350974Swpaul	}
103450974Swpaul
103550974Swpaul	return(ENXIO);
103650974Swpaul}
103750974Swpaul
103850974Swpaul/*
103950974Swpaul * Attach the interface. Allocate softc structures, do ifmedia
104050974Swpaul * setup and ethernet/BPF attach.
104150974Swpaul */
1042102334Salfredstatic int
1043102334Salfredsis_attach(dev)
104450974Swpaul	device_t		dev;
104550974Swpaul{
104650974Swpaul	u_char			eaddr[ETHER_ADDR_LEN];
104750974Swpaul	struct sis_softc	*sc;
104850974Swpaul	struct ifnet		*ifp;
1049109061Smbr	int			unit, error = 0, rid, waittime = 0;
105050974Swpaul
1051109061Smbr	waittime = 0;
105250974Swpaul	sc = device_get_softc(dev);
105350974Swpaul	unit = device_get_unit(dev);
105450974Swpaul
105593818Sjhb	mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
105693818Sjhb	    MTX_DEF | MTX_RECURSE);
105769583Swpaul
105850974Swpaul	if (pci_get_device(dev) == SIS_DEVICEID_900)
105950974Swpaul		sc->sis_type = SIS_TYPE_900;
106050974Swpaul	if (pci_get_device(dev) == SIS_DEVICEID_7016)
106150974Swpaul		sc->sis_type = SIS_TYPE_7016;
106262672Swpaul	if (pci_get_vendor(dev) == NS_VENDORID)
106362672Swpaul		sc->sis_type = SIS_TYPE_83815;
106450974Swpaul
106589296Swpaul	sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
1066117208Simp#ifndef BURN_BRIDGES
106750974Swpaul	/*
106850974Swpaul	 * Handle power management nonsense.
106950974Swpaul	 */
107072813Swpaul	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
107172813Swpaul		u_int32_t		iobase, membase, irq;
107250974Swpaul
107372813Swpaul		/* Save important PCI config data. */
107472813Swpaul		iobase = pci_read_config(dev, SIS_PCI_LOIO, 4);
107572813Swpaul		membase = pci_read_config(dev, SIS_PCI_LOMEM, 4);
107672813Swpaul		irq = pci_read_config(dev, SIS_PCI_INTLINE, 4);
107750974Swpaul
107872813Swpaul		/* Reset the power state. */
107972813Swpaul		printf("sis%d: chip is in D%d power mode "
108072813Swpaul		    "-- setting to D0\n", unit,
108172813Swpaul		    pci_get_powerstate(dev));
108272813Swpaul		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
108350974Swpaul
108472813Swpaul		/* Restore PCI config data. */
108572813Swpaul		pci_write_config(dev, SIS_PCI_LOIO, iobase, 4);
108672813Swpaul		pci_write_config(dev, SIS_PCI_LOMEM, membase, 4);
108772813Swpaul		pci_write_config(dev, SIS_PCI_INTLINE, irq, 4);
108850974Swpaul	}
1089117208Simp#endif
109050974Swpaul	/*
109150974Swpaul	 * Map control/status registers.
109250974Swpaul	 */
109372813Swpaul	pci_enable_busmaster(dev);
109450974Swpaul
109550974Swpaul	rid = SIS_RID;
109650974Swpaul	sc->sis_res = bus_alloc_resource(dev, SIS_RES, &rid,
109750974Swpaul	    0, ~0, 1, RF_ACTIVE);
109850974Swpaul
109950974Swpaul	if (sc->sis_res == NULL) {
110050974Swpaul		printf("sis%d: couldn't map ports/memory\n", unit);
110150974Swpaul		error = ENXIO;
110250974Swpaul		goto fail;
110350974Swpaul	}
110450974Swpaul
110550974Swpaul	sc->sis_btag = rman_get_bustag(sc->sis_res);
110650974Swpaul	sc->sis_bhandle = rman_get_bushandle(sc->sis_res);
110750974Swpaul
110850974Swpaul	/* Allocate interrupt */
110950974Swpaul	rid = 0;
111050974Swpaul	sc->sis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
111150974Swpaul	    RF_SHAREABLE | RF_ACTIVE);
111250974Swpaul
111350974Swpaul	if (sc->sis_irq == NULL) {
111450974Swpaul		printf("sis%d: couldn't map interrupt\n", unit);
111550974Swpaul		error = ENXIO;
111650974Swpaul		goto fail;
111750974Swpaul	}
111850974Swpaul
111950974Swpaul	/* Reset the adapter. */
112050974Swpaul	sis_reset(sc);
112150974Swpaul
1122109976Smbr	if (sc->sis_type == SIS_TYPE_900 &&
1123109976Smbr            (sc->sis_rev == SIS_REV_635 ||
1124109976Smbr            sc->sis_rev == SIS_REV_900B)) {
1125109976Smbr		SIO_SET(SIS_CFG_RND_CNT);
1126109976Smbr		SIO_SET(SIS_CFG_PERR_DETECT);
1127109976Smbr	}
1128109976Smbr
112950974Swpaul	/*
113050974Swpaul	 * Get station address from the EEPROM.
113150974Swpaul	 */
113262672Swpaul	switch (pci_get_vendor(dev)) {
113362672Swpaul	case NS_VENDORID:
113462672Swpaul		/*
113562672Swpaul		 * Reading the MAC address out of the EEPROM on
113662672Swpaul		 * the NatSemi chip takes a bit more work than
113762672Swpaul		 * you'd expect. The address spans 4 16-bit words,
113862672Swpaul		 * with the first word containing only a single bit.
113962672Swpaul		 * You have to shift everything over one bit to
114062672Swpaul		 * get it aligned properly. Also, the bits are
114162672Swpaul		 * stored backwards (the LSB is really the MSB,
114262672Swpaul		 * and so on) so you have to reverse them in order
114362672Swpaul		 * to get the MAC address into the form we want.
114462672Swpaul		 * Why? Who the hell knows.
114562672Swpaul		 */
114662672Swpaul		{
114762672Swpaul			u_int16_t		tmp[4];
114850974Swpaul
114962672Swpaul			sis_read_eeprom(sc, (caddr_t)&tmp,
115062672Swpaul			    NS_EE_NODEADDR, 4, 0);
115162672Swpaul
115262672Swpaul			/* Shift everything over one bit. */
115362672Swpaul			tmp[3] = tmp[3] >> 1;
115462681Swpaul			tmp[3] |= tmp[2] << 15;
115562672Swpaul			tmp[2] = tmp[2] >> 1;
115662681Swpaul			tmp[2] |= tmp[1] << 15;
115762672Swpaul			tmp[1] = tmp[1] >> 1;
115862681Swpaul			tmp[1] |= tmp[0] << 15;
115962672Swpaul
116062672Swpaul			/* Now reverse all the bits. */
116162672Swpaul			tmp[3] = sis_reverse(tmp[3]);
116262672Swpaul			tmp[2] = sis_reverse(tmp[2]);
116362672Swpaul			tmp[1] = sis_reverse(tmp[1]);
116462672Swpaul
116562672Swpaul			bcopy((char *)&tmp[1], eaddr, ETHER_ADDR_LEN);
116662672Swpaul		}
116762672Swpaul		break;
116862672Swpaul	case SIS_VENDORID:
116962672Swpaul	default:
117072197Swpaul#ifdef __i386__
117172197Swpaul		/*
117272197Swpaul		 * If this is a SiS 630E chipset with an embedded
117372197Swpaul		 * SiS 900 controller, we have to read the MAC address
117472197Swpaul		 * from the APC CMOS RAM. Our method for doing this
117572197Swpaul		 * is very ugly since we have to reach out and grab
117672197Swpaul		 * ahold of hardware for which we cannot properly
117772197Swpaul		 * allocate resources. This code is only compiled on
117872197Swpaul		 * the i386 architecture since the SiS 630E chipset
117972197Swpaul		 * is for x86 motherboards only. Note that there are
118072197Swpaul		 * a lot of magic numbers in this hack. These are
118172197Swpaul		 * taken from SiS's Linux driver. I'd like to replace
118272197Swpaul		 * them with proper symbolic definitions, but that
118372197Swpaul		 * requires some datasheets that I don't have access
118472197Swpaul		 * to at the moment.
118572197Swpaul		 */
118689296Swpaul		if (sc->sis_rev == SIS_REV_630S ||
118789296Swpaul		    sc->sis_rev == SIS_REV_630E ||
118890328Sambrisko		    sc->sis_rev == SIS_REV_630EA1)
118972197Swpaul			sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
119089296Swpaul
119190328Sambrisko		else if (sc->sis_rev == SIS_REV_635 ||
119290328Sambrisko			 sc->sis_rev == SIS_REV_630ET)
119389296Swpaul			sis_read_mac(sc, dev, (caddr_t)&eaddr);
1194109061Smbr		else if (sc->sis_rev == SIS_REV_96x) {
1195109061Smbr			/* Allow to read EEPROM from LAN. It is shared
1196109061Smbr			 * between a 1394 controller and the NIC and each
1197109061Smbr			 * time we access it, we need to set SIS_EECMD_REQ.
1198109061Smbr			 */
1199109061Smbr			SIO_SET(SIS_EECMD_REQ);
1200109061Smbr			for (waittime = 0; waittime < SIS_TIMEOUT;
1201109061Smbr			    waittime++) {
1202109061Smbr				/* Force EEPROM to idle state. */
1203109061Smbr				sis_eeprom_idle(sc);
1204109061Smbr				if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1205109061Smbr					sis_read_eeprom(sc, (caddr_t)&eaddr,
1206109061Smbr					    SIS_EE_NODEADDR, 3, 0);
1207109061Smbr					break;
1208109061Smbr				}
1209109061Smbr				DELAY(1);
1210109061Smbr			}
1211109061Smbr			/*
1212109061Smbr			 * Set SIS_EECTL_CLK to high, so a other master
1213109061Smbr			 * can operate on the i2c bus.
1214109061Smbr			 */
1215109061Smbr			SIO_SET(SIS_EECTL_CLK);
1216109061Smbr			/* Refuse EEPROM access by LAN */
1217109061Smbr			SIO_SET(SIS_EECMD_DONE);
1218109061Smbr		} else
121972197Swpaul#endif
122072197Swpaul			sis_read_eeprom(sc, (caddr_t)&eaddr,
122172197Swpaul			    SIS_EE_NODEADDR, 3, 0);
122262672Swpaul		break;
122362672Swpaul	}
122462672Swpaul
122550974Swpaul	/*
122650974Swpaul	 * A SiS chip was detected. Inform the world.
122750974Swpaul	 */
122850974Swpaul	printf("sis%d: Ethernet address: %6D\n", unit, eaddr, ":");
122950974Swpaul
123050974Swpaul	sc->sis_unit = unit;
123150974Swpaul	callout_handle_init(&sc->sis_stat_ch);
123250974Swpaul	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
123350974Swpaul
123481713Swpaul	/*
123581713Swpaul	 * Allocate the parent bus DMA tag appropriate for PCI.
123681713Swpaul	 */
123781713Swpaul#define SIS_NSEG_NEW 32
123881713Swpaul	 error = bus_dma_tag_create(NULL,	/* parent */
123981713Swpaul			1, 0,			/* alignment, boundary */
124081713Swpaul			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
124181713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
124281713Swpaul			NULL, NULL,		/* filter, filterarg */
124381713Swpaul			MAXBSIZE, SIS_NSEG_NEW,	/* maxsize, nsegments */
124481713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
124581713Swpaul			BUS_DMA_ALLOCNOW,	/* flags */
1246117126Sscottl			NULL, NULL,		/* lockfunc, lockarg */
124781713Swpaul			&sc->sis_parent_tag);
1248112872Snjl	if (error)
1249112872Snjl		goto fail;
125050974Swpaul
125181713Swpaul	/*
1252112872Snjl	 * Now allocate a tag for the DMA descriptor lists and a chunk
1253112872Snjl	 * of DMA-able memory based on the tag.  Also obtain the physical
1254112872Snjl	 * addresses of the RX and TX ring, which we'll need later.
125581713Swpaul	 * All of our lists are allocated as a contiguous block
125681713Swpaul	 * of memory.
125781713Swpaul	 */
125881713Swpaul	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
125981713Swpaul			1, 0,			/* alignment, boundary */
126081713Swpaul			BUS_SPACE_MAXADDR,	/* lowaddr */
126181713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
126281713Swpaul			NULL, NULL,		/* filter, filterarg */
126381713Swpaul			SIS_RX_LIST_SZ, 1,	/* maxsize,nsegments */
126481713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
126581713Swpaul			0,			/* flags */
1266117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1267117126Sscottl			&Giant,			/* lockarg */
126881713Swpaul			&sc->sis_ldata.sis_rx_tag);
1269112872Snjl	if (error)
1270112872Snjl		goto fail;
127181713Swpaul
1272112872Snjl	error = bus_dmamem_alloc(sc->sis_ldata.sis_rx_tag,
1273118089Smux	    (void **)&sc->sis_ldata.sis_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1274112872Snjl	    &sc->sis_ldata.sis_rx_dmamap);
1275112872Snjl
1276112872Snjl	if (error) {
1277112872Snjl		printf("sis%d: no memory for rx list buffers!\n", unit);
1278112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1279112872Snjl		sc->sis_ldata.sis_rx_tag = NULL;
1280112872Snjl		goto fail;
1281112872Snjl	}
1282112872Snjl
1283112872Snjl	error = bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
1284112872Snjl	    sc->sis_ldata.sis_rx_dmamap, &(sc->sis_ldata.sis_rx_list[0]),
1285112872Snjl	    sizeof(struct sis_desc), sis_dma_map_ring,
1286112872Snjl	    &sc->sis_cdata.sis_rx_paddr, 0);
1287112872Snjl
1288112872Snjl	if (error) {
1289112872Snjl		printf("sis%d: cannot get address of the rx ring!\n", unit);
1290112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1291112872Snjl		    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1292112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1293112872Snjl		sc->sis_ldata.sis_rx_tag = NULL;
1294112872Snjl		goto fail;
1295112872Snjl	}
1296112872Snjl
129781713Swpaul	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
129881713Swpaul			1, 0,			/* alignment, boundary */
129981713Swpaul			BUS_SPACE_MAXADDR,	/* lowaddr */
130081713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
130181713Swpaul			NULL, NULL,		/* filter, filterarg */
130281713Swpaul			SIS_TX_LIST_SZ, 1,	/* maxsize,nsegments */
130381713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
130481713Swpaul			0,			/* flags */
1305117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1306117126Sscottl			&Giant,			/* lockarg */
130781713Swpaul			&sc->sis_ldata.sis_tx_tag);
1308112872Snjl	if (error)
1309112872Snjl		goto fail;
131081713Swpaul
131181713Swpaul	error = bus_dmamem_alloc(sc->sis_ldata.sis_tx_tag,
1312118089Smux	    (void **)&sc->sis_ldata.sis_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
131381713Swpaul	    &sc->sis_ldata.sis_tx_dmamap);
131481713Swpaul
131581713Swpaul	if (error) {
1316112872Snjl		printf("sis%d: no memory for tx list buffers!\n", unit);
131781713Swpaul		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1318112872Snjl		sc->sis_ldata.sis_tx_tag = NULL;
131950974Swpaul		goto fail;
132050974Swpaul	}
132150974Swpaul
1322112872Snjl	error = bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
1323112872Snjl	    sc->sis_ldata.sis_tx_dmamap, &(sc->sis_ldata.sis_tx_list[0]),
1324112872Snjl	    sizeof(struct sis_desc), sis_dma_map_ring,
1325112872Snjl	    &sc->sis_cdata.sis_tx_paddr, 0);
132681713Swpaul
132781713Swpaul	if (error) {
1328112872Snjl		printf("sis%d: cannot get address of the tx ring!\n", unit);
1329112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_tx_tag,
133081713Swpaul		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
133181713Swpaul		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1332112872Snjl		sc->sis_ldata.sis_tx_tag = NULL;
133381713Swpaul		goto fail;
133481713Swpaul	}
133581713Swpaul
1336112872Snjl	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
1337112872Snjl			1, 0,			/* alignment, boundary */
1338112872Snjl			BUS_SPACE_MAXADDR,	/* lowaddr */
1339112872Snjl			BUS_SPACE_MAXADDR,	/* highaddr */
1340112872Snjl			NULL, NULL,		/* filter, filterarg */
1341112872Snjl			MCLBYTES, 1,		/* maxsize,nsegments */
1342112872Snjl			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1343112872Snjl			0,			/* flags */
1344117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1345117126Sscottl			&Giant,			/* lockarg */
1346112872Snjl			&sc->sis_tag);
1347112872Snjl	if (error)
1348112872Snjl		goto fail;
134981713Swpaul
135081713Swpaul	/*
135181713Swpaul	 * Obtain the physical addresses of the RX and TX
135281713Swpaul	 * rings which we'll need later in the init routine.
135381713Swpaul	 */
135481713Swpaul
135550974Swpaul	ifp = &sc->arpcom.ac_if;
135650974Swpaul	ifp->if_softc = sc;
135750974Swpaul	ifp->if_unit = unit;
135850974Swpaul	ifp->if_name = "sis";
135950974Swpaul	ifp->if_mtu = ETHERMTU;
136050974Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
136150974Swpaul	ifp->if_ioctl = sis_ioctl;
136250974Swpaul	ifp->if_output = ether_output;
136350974Swpaul	ifp->if_start = sis_start;
136450974Swpaul	ifp->if_watchdog = sis_watchdog;
136550974Swpaul	ifp->if_init = sis_init;
136650974Swpaul	ifp->if_baudrate = 10000000;
136750974Swpaul	ifp->if_snd.ifq_maxlen = SIS_TX_LIST_CNT - 1;
136850974Swpaul
136950974Swpaul	/*
137050974Swpaul	 * Do MII setup.
137150974Swpaul	 */
137250974Swpaul	if (mii_phy_probe(dev, &sc->sis_miibus,
137350974Swpaul	    sis_ifmedia_upd, sis_ifmedia_sts)) {
137450974Swpaul		printf("sis%d: MII without any PHY!\n", sc->sis_unit);
137550974Swpaul		error = ENXIO;
137650974Swpaul		goto fail;
137750974Swpaul	}
137850974Swpaul
137950974Swpaul	/*
138063090Sarchie	 * Call MI attach routine.
138150974Swpaul	 */
1382106936Ssam	ether_ifattach(ifp, eaddr);
138387390Sjhay
138487390Sjhay	/*
138587390Sjhay	 * Tell the upper layer(s) we support long frames.
138687390Sjhay	 */
138787390Sjhay	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1388106936Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
138987390Sjhay
1390113609Snjl	/* Hook interrupt last to avoid having to lock softc */
1391112872Snjl	error = bus_setup_intr(dev, sc->sis_irq, INTR_TYPE_NET,
1392112872Snjl	    sis_intr, sc, &sc->sis_intrhand);
139350974Swpaul
1394112872Snjl	if (error) {
1395112872Snjl		printf("sis%d: couldn't set up irq\n", unit);
1396113609Snjl		ether_ifdetach(ifp);
1397112872Snjl		goto fail;
1398112872Snjl	}
1399112872Snjl
140050974Swpaulfail:
1401112872Snjl	if (error)
1402112872Snjl		sis_detach(dev);
1403112872Snjl
140450974Swpaul	return(error);
140550974Swpaul}
140650974Swpaul
1407113609Snjl/*
1408113609Snjl * Shutdown hardware and free up resources. This can be called any
1409113609Snjl * time after the mutex has been initialized. It is called in both
1410113609Snjl * the error case in attach and the normal detach case so it needs
1411113609Snjl * to be careful about only freeing resources that have actually been
1412113609Snjl * allocated.
1413113609Snjl */
1414102334Salfredstatic int
1415102334Salfredsis_detach(dev)
141650974Swpaul	device_t		dev;
141750974Swpaul{
141850974Swpaul	struct sis_softc	*sc;
141950974Swpaul	struct ifnet		*ifp;
142050974Swpaul
142150974Swpaul	sc = device_get_softc(dev);
1422112880Sjhb	KASSERT(mtx_initialized(&sc->sis_mtx), ("sis mutex not initialized"));
142367087Swpaul	SIS_LOCK(sc);
142450974Swpaul	ifp = &sc->arpcom.ac_if;
142550974Swpaul
1426118089Smux	/* These should only be active if attach succeeded. */
1427113812Simp	if (device_is_attached(dev)) {
1428113609Snjl		sis_reset(sc);
1429113609Snjl		sis_stop(sc);
1430112872Snjl		ether_ifdetach(ifp);
1431113609Snjl	}
1432113609Snjl	if (sc->sis_miibus)
1433112872Snjl		device_delete_child(dev, sc->sis_miibus);
1434113609Snjl	bus_generic_detach(dev);
143550974Swpaul
1436112872Snjl	if (sc->sis_intrhand)
1437112872Snjl		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1438112872Snjl	if (sc->sis_irq)
1439112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1440112872Snjl	if (sc->sis_res)
1441112872Snjl		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
144250974Swpaul
1443112872Snjl	if (sc->sis_ldata.sis_rx_tag) {
1444112872Snjl		bus_dmamap_unload(sc->sis_ldata.sis_rx_tag,
1445112872Snjl		    sc->sis_ldata.sis_rx_dmamap);
1446112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1447112872Snjl		    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1448112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1449112872Snjl	}
1450112872Snjl	if (sc->sis_ldata.sis_tx_tag) {
1451112872Snjl		bus_dmamap_unload(sc->sis_ldata.sis_tx_tag,
1452112872Snjl		    sc->sis_ldata.sis_tx_dmamap);
1453112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_tx_tag,
1454112872Snjl		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
1455112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1456112872Snjl	}
1457112872Snjl	if (sc->sis_parent_tag)
1458112872Snjl		bus_dma_tag_destroy(sc->sis_parent_tag);
1459112872Snjl	if (sc->sis_tag)
1460112872Snjl		bus_dma_tag_destroy(sc->sis_tag);
146150974Swpaul
146267087Swpaul	SIS_UNLOCK(sc);
146367087Swpaul	mtx_destroy(&sc->sis_mtx);
146450974Swpaul
146550974Swpaul	return(0);
146650974Swpaul}
146750974Swpaul
146850974Swpaul/*
146950974Swpaul * Initialize the transmit descriptors.
147050974Swpaul */
1471102334Salfredstatic int
1472102334Salfredsis_list_tx_init(sc)
147350974Swpaul	struct sis_softc	*sc;
147450974Swpaul{
147550974Swpaul	struct sis_list_data	*ld;
147650974Swpaul	struct sis_ring_data	*cd;
147787059Sluigi	int			i, nexti;
147850974Swpaul
147950974Swpaul	cd = &sc->sis_cdata;
148081713Swpaul	ld = &sc->sis_ldata;
148150974Swpaul
148250974Swpaul	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
148387102Sluigi		nexti = (i == (SIS_TX_LIST_CNT - 1)) ? 0 : i+1;
148450974Swpaul			ld->sis_tx_list[i].sis_nextdesc =
148587059Sluigi			    &ld->sis_tx_list[nexti];
148681713Swpaul			bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
148781713Swpaul			    sc->sis_ldata.sis_tx_dmamap,
148887059Sluigi			    &ld->sis_tx_list[nexti], sizeof(struct sis_desc),
148981713Swpaul			    sis_dma_map_desc_next, &ld->sis_tx_list[i], 0);
149050974Swpaul		ld->sis_tx_list[i].sis_mbuf = NULL;
149150974Swpaul		ld->sis_tx_list[i].sis_ptr = 0;
149250974Swpaul		ld->sis_tx_list[i].sis_ctl = 0;
149350974Swpaul	}
149450974Swpaul
149550974Swpaul	cd->sis_tx_prod = cd->sis_tx_cons = cd->sis_tx_cnt = 0;
149650974Swpaul
149781713Swpaul	bus_dmamap_sync(sc->sis_ldata.sis_tx_tag,
149881713Swpaul	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
149981713Swpaul
150050974Swpaul	return(0);
150150974Swpaul}
150250974Swpaul
150350974Swpaul/*
150450974Swpaul * Initialize the RX descriptors and allocate mbufs for them. Note that
150550974Swpaul * we arrange the descriptors in a closed ring, so that the last descriptor
150650974Swpaul * points back to the first.
150750974Swpaul */
1508102334Salfredstatic int
1509102334Salfredsis_list_rx_init(sc)
151050974Swpaul	struct sis_softc	*sc;
151150974Swpaul{
151250974Swpaul	struct sis_list_data	*ld;
151350974Swpaul	struct sis_ring_data	*cd;
151487059Sluigi	int			i,nexti;
151550974Swpaul
151681713Swpaul	ld = &sc->sis_ldata;
151750974Swpaul	cd = &sc->sis_cdata;
151850974Swpaul
151950974Swpaul	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
152050974Swpaul		if (sis_newbuf(sc, &ld->sis_rx_list[i], NULL) == ENOBUFS)
152150974Swpaul			return(ENOBUFS);
152287102Sluigi		nexti = (i == (SIS_RX_LIST_CNT - 1)) ? 0 : i+1;
152350974Swpaul			ld->sis_rx_list[i].sis_nextdesc =
152487059Sluigi			    &ld->sis_rx_list[nexti];
152581713Swpaul			bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
152681713Swpaul			    sc->sis_ldata.sis_rx_dmamap,
152787059Sluigi			    &ld->sis_rx_list[nexti],
152881713Swpaul			    sizeof(struct sis_desc), sis_dma_map_desc_next,
152981713Swpaul			    &ld->sis_rx_list[i], 0);
153050974Swpaul		}
153150974Swpaul
153281713Swpaul	bus_dmamap_sync(sc->sis_ldata.sis_rx_tag,
153381713Swpaul	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
153481713Swpaul
153550974Swpaul	cd->sis_rx_prod = 0;
153650974Swpaul
153750974Swpaul	return(0);
153850974Swpaul}
153950974Swpaul
154050974Swpaul/*
154150974Swpaul * Initialize an RX descriptor and attach an MBUF cluster.
154250974Swpaul */
1543102334Salfredstatic int
1544102334Salfredsis_newbuf(sc, c, m)
154550974Swpaul	struct sis_softc	*sc;
154650974Swpaul	struct sis_desc		*c;
154750974Swpaul	struct mbuf		*m;
154850974Swpaul{
154950974Swpaul
155081713Swpaul	if (c == NULL)
155181713Swpaul		return(EINVAL);
155281713Swpaul
155350974Swpaul	if (m == NULL) {
1554111119Simp		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1555101340Sluigi		if (m == NULL)
155650974Swpaul			return(ENOBUFS);
1557101340Sluigi	} else
1558101340Sluigi		m->m_data = m->m_ext.ext_buf;
155950974Swpaul
1560101340Sluigi	c->sis_mbuf = m;
156150974Swpaul	c->sis_ctl = SIS_RXLEN;
156250974Swpaul
156381713Swpaul	bus_dmamap_create(sc->sis_tag, 0, &c->sis_map);
156481713Swpaul	bus_dmamap_load(sc->sis_tag, c->sis_map,
1565101464Sluigi	    mtod(m, void *), MCLBYTES,
156681713Swpaul	    sis_dma_map_desc_ptr, c, 0);
156781713Swpaul	bus_dmamap_sync(sc->sis_tag, c->sis_map, BUS_DMASYNC_PREWRITE);
156881713Swpaul
156950974Swpaul	return(0);
157050974Swpaul}
157150974Swpaul
157250974Swpaul/*
157350974Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to
157450974Swpaul * the higher level protocols.
157550974Swpaul */
1576102334Salfredstatic void
1577102334Salfredsis_rxeof(sc)
157850974Swpaul	struct sis_softc	*sc;
157950974Swpaul{
158050974Swpaul        struct mbuf		*m;
158150974Swpaul        struct ifnet		*ifp;
158250974Swpaul	struct sis_desc		*cur_rx;
158350974Swpaul	int			i, total_len = 0;
158450974Swpaul	u_int32_t		rxstat;
158550974Swpaul
158650974Swpaul	ifp = &sc->arpcom.ac_if;
158750974Swpaul	i = sc->sis_cdata.sis_rx_prod;
158850974Swpaul
158981713Swpaul	while(SIS_OWNDESC(&sc->sis_ldata.sis_rx_list[i])) {
159050974Swpaul
159187902Sluigi#ifdef DEVICE_POLLING
1592102052Ssobomax		if (ifp->if_flags & IFF_POLLING) {
159387902Sluigi			if (sc->rxcycles <= 0)
159487902Sluigi				break;
159587902Sluigi			sc->rxcycles--;
159687902Sluigi		}
159787902Sluigi#endif /* DEVICE_POLLING */
159881713Swpaul		cur_rx = &sc->sis_ldata.sis_rx_list[i];
159950974Swpaul		rxstat = cur_rx->sis_rxstat;
160081713Swpaul		bus_dmamap_sync(sc->sis_tag,
160181713Swpaul		    cur_rx->sis_map, BUS_DMASYNC_POSTWRITE);
160281713Swpaul		bus_dmamap_unload(sc->sis_tag, cur_rx->sis_map);
160381713Swpaul		bus_dmamap_destroy(sc->sis_tag, cur_rx->sis_map);
160450974Swpaul		m = cur_rx->sis_mbuf;
160550974Swpaul		cur_rx->sis_mbuf = NULL;
160650974Swpaul		total_len = SIS_RXBYTES(cur_rx);
160750974Swpaul		SIS_INC(i, SIS_RX_LIST_CNT);
160850974Swpaul
160950974Swpaul		/*
161050974Swpaul		 * If an error occurs, update stats, clear the
161150974Swpaul		 * status word and leave the mbuf cluster in place:
161250974Swpaul		 * it should simply get re-used next time this descriptor
161350974Swpaul	 	 * comes up in the ring.
161450974Swpaul		 */
161550974Swpaul		if (!(rxstat & SIS_CMDSTS_PKT_OK)) {
161650974Swpaul			ifp->if_ierrors++;
161750974Swpaul			if (rxstat & SIS_RXSTAT_COLL)
161850974Swpaul				ifp->if_collisions++;
161950974Swpaul			sis_newbuf(sc, cur_rx, m);
162050974Swpaul			continue;
162150974Swpaul		}
162250974Swpaul
162350974Swpaul		/* No errors; receive the packet. */
162487059Sluigi#ifdef __i386__
162587059Sluigi		/*
162687059Sluigi		 * On the x86 we do not have alignment problems, so try to
162787059Sluigi		 * allocate a new buffer for the receive ring, and pass up
162887059Sluigi		 * the one where the packet is already, saving the expensive
162987059Sluigi		 * copy done in m_devget().
163087059Sluigi		 * If we are on an architecture with alignment problems, or
163187059Sluigi		 * if the allocation fails, then use m_devget and leave the
163287059Sluigi		 * existing buffer in the receive ring.
163387059Sluigi		 */
1634101464Sluigi		if (sis_newbuf(sc, cur_rx, NULL) == 0)
163587059Sluigi			m->m_pkthdr.len = m->m_len = total_len;
1636101464Sluigi		else
163787059Sluigi#endif
163887059Sluigi		{
163987059Sluigi			struct mbuf		*m0;
164087059Sluigi			m0 = m_devget(mtod(m, char *), total_len,
164187059Sluigi				ETHER_ALIGN, ifp, NULL);
164287059Sluigi			sis_newbuf(sc, cur_rx, m);
164387059Sluigi			if (m0 == NULL) {
164487059Sluigi				ifp->if_ierrors++;
164587059Sluigi				continue;
164687059Sluigi			}
164787059Sluigi			m = m0;
164850974Swpaul		}
164950974Swpaul
165050974Swpaul		ifp->if_ipackets++;
1651106936Ssam		m->m_pkthdr.rcvif = ifp;
1652106936Ssam
1653106936Ssam		(*ifp->if_input)(ifp, m);
165450974Swpaul	}
165550974Swpaul
165650974Swpaul	sc->sis_cdata.sis_rx_prod = i;
165750974Swpaul
165850974Swpaul	return;
165950974Swpaul}
166050974Swpaul
1661105219Sphkstatic void
1662102334Salfredsis_rxeoc(sc)
166350974Swpaul	struct sis_softc	*sc;
166450974Swpaul{
166550974Swpaul	sis_rxeof(sc);
166650974Swpaul	sis_init(sc);
166750974Swpaul	return;
166850974Swpaul}
166950974Swpaul
167050974Swpaul/*
167150974Swpaul * A frame was downloaded to the chip. It's safe for us to clean up
167250974Swpaul * the list buffers.
167350974Swpaul */
167450974Swpaul
1675102334Salfredstatic void
1676102334Salfredsis_txeof(sc)
167750974Swpaul	struct sis_softc	*sc;
167850974Swpaul{
167950974Swpaul	struct ifnet		*ifp;
168050974Swpaul	u_int32_t		idx;
168150974Swpaul
168250974Swpaul	ifp = &sc->arpcom.ac_if;
168350974Swpaul
168450974Swpaul	/*
168550974Swpaul	 * Go through our tx list and free mbufs for those
168650974Swpaul	 * frames that have been transmitted.
168750974Swpaul	 */
168899163Sluigi	for (idx = sc->sis_cdata.sis_tx_cons; sc->sis_cdata.sis_tx_cnt > 0;
168999163Sluigi	    sc->sis_cdata.sis_tx_cnt--, SIS_INC(idx, SIS_TX_LIST_CNT) ) {
169099163Sluigi		struct sis_desc *cur_tx = &sc->sis_ldata.sis_tx_list[idx];
169150974Swpaul
169250974Swpaul		if (SIS_OWNDESC(cur_tx))
169350974Swpaul			break;
169450974Swpaul
169599163Sluigi		if (cur_tx->sis_ctl & SIS_CMDSTS_MORE)
169650974Swpaul			continue;
169750974Swpaul
169850974Swpaul		if (!(cur_tx->sis_ctl & SIS_CMDSTS_PKT_OK)) {
169950974Swpaul			ifp->if_oerrors++;
170050974Swpaul			if (cur_tx->sis_txstat & SIS_TXSTAT_EXCESSCOLLS)
170150974Swpaul				ifp->if_collisions++;
170250974Swpaul			if (cur_tx->sis_txstat & SIS_TXSTAT_OUTOFWINCOLL)
170350974Swpaul				ifp->if_collisions++;
170450974Swpaul		}
170550974Swpaul
170650974Swpaul		ifp->if_collisions +=
170750974Swpaul		    (cur_tx->sis_txstat & SIS_TXSTAT_COLLCNT) >> 16;
170850974Swpaul
170950974Swpaul		ifp->if_opackets++;
171050974Swpaul		if (cur_tx->sis_mbuf != NULL) {
171150974Swpaul			m_freem(cur_tx->sis_mbuf);
171250974Swpaul			cur_tx->sis_mbuf = NULL;
171381713Swpaul			bus_dmamap_unload(sc->sis_tag, cur_tx->sis_map);
171481713Swpaul			bus_dmamap_destroy(sc->sis_tag, cur_tx->sis_map);
171550974Swpaul		}
171699163Sluigi	}
171750974Swpaul
171899163Sluigi	if (idx != sc->sis_cdata.sis_tx_cons) {
171999163Sluigi		/* we freed up some buffers */
172099163Sluigi		sc->sis_cdata.sis_tx_cons = idx;
172199163Sluigi		ifp->if_flags &= ~IFF_OACTIVE;
172250974Swpaul	}
172350974Swpaul
172499163Sluigi	ifp->if_timer = (sc->sis_cdata.sis_tx_cnt == 0) ? 0 : 5;
172550974Swpaul
172650974Swpaul	return;
172750974Swpaul}
172850974Swpaul
1729102334Salfredstatic void
1730102334Salfredsis_tick(xsc)
173150974Swpaul	void			*xsc;
173250974Swpaul{
173350974Swpaul	struct sis_softc	*sc;
173450974Swpaul	struct mii_data		*mii;
173564963Swpaul	struct ifnet		*ifp;
173650974Swpaul
173750974Swpaul	sc = xsc;
173867087Swpaul	SIS_LOCK(sc);
1739117858Scognet	sc->in_tick = 1;
174064963Swpaul	ifp = &sc->arpcom.ac_if;
174164963Swpaul
174250974Swpaul	mii = device_get_softc(sc->sis_miibus);
174350974Swpaul	mii_tick(mii);
174464963Swpaul
174584147Sjlemon	if (!sc->sis_link && mii->mii_media_status & IFM_ACTIVE &&
174684147Sjlemon	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
174784147Sjlemon		sc->sis_link++;
174884147Sjlemon		if (ifp->if_snd.ifq_head != NULL)
174984147Sjlemon			sis_start(ifp);
175064963Swpaul	}
175164963Swpaul
1752117858Scognet	sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1753117858Scognet	sc->in_tick = 0;
175467087Swpaul	SIS_UNLOCK(sc);
175550974Swpaul
175650974Swpaul	return;
175750974Swpaul}
175850974Swpaul
175987902Sluigi#ifdef DEVICE_POLLING
176087902Sluigistatic poll_handler_t sis_poll;
176187902Sluigi
176287902Sluigistatic void
176387902Sluigisis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
176487902Sluigi{
176587973Speter	struct	sis_softc *sc = ifp->if_softc;
176687973Speter
176787902Sluigi	SIS_LOCK(sc);
176887902Sluigi	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
176987902Sluigi		CSR_WRITE_4(sc, SIS_IER, 1);
177087902Sluigi		goto done;
177187902Sluigi	}
177287902Sluigi
177387902Sluigi	/*
177487902Sluigi	 * On the sis, reading the status register also clears it.
177587902Sluigi	 * So before returning to intr mode we must make sure that all
177687902Sluigi	 * possible pending sources of interrupts have been served.
177787902Sluigi	 * In practice this means run to completion the *eof routines,
177887902Sluigi	 * and then call the interrupt routine
177987902Sluigi	 */
178087902Sluigi	sc->rxcycles = count;
178187902Sluigi	sis_rxeof(sc);
178287902Sluigi	sis_txeof(sc);
178387902Sluigi	if (ifp->if_snd.ifq_head != NULL)
178487902Sluigi		sis_start(ifp);
178587902Sluigi
178687902Sluigi	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
178787902Sluigi		u_int32_t	status;
178887902Sluigi
178987902Sluigi		/* Reading the ISR register clears all interrupts. */
179087902Sluigi		status = CSR_READ_4(sc, SIS_ISR);
179187902Sluigi
179287902Sluigi		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
179387902Sluigi			sis_rxeoc(sc);
179487902Sluigi
179587902Sluigi		if (status & (SIS_ISR_RX_IDLE))
179687902Sluigi			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
179787902Sluigi
179887902Sluigi		if (status & SIS_ISR_SYSERR) {
179987902Sluigi			sis_reset(sc);
180087902Sluigi			sis_init(sc);
180187902Sluigi		}
180287902Sluigi	}
180387902Sluigidone:
180487902Sluigi	SIS_UNLOCK(sc);
180587902Sluigi	return;
180687902Sluigi}
180787902Sluigi#endif /* DEVICE_POLLING */
180887902Sluigi
1809102334Salfredstatic void
1810102334Salfredsis_intr(arg)
181150974Swpaul	void			*arg;
181250974Swpaul{
181350974Swpaul	struct sis_softc	*sc;
181450974Swpaul	struct ifnet		*ifp;
181550974Swpaul	u_int32_t		status;
181650974Swpaul
181750974Swpaul	sc = arg;
181850974Swpaul	ifp = &sc->arpcom.ac_if;
181950974Swpaul
182086984Sluigi	SIS_LOCK(sc);
182187902Sluigi#ifdef DEVICE_POLLING
1822102052Ssobomax	if (ifp->if_flags & IFF_POLLING)
182387902Sluigi		goto done;
182487902Sluigi	if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */
182587902Sluigi		CSR_WRITE_4(sc, SIS_IER, 0);
182687902Sluigi		goto done;
182787902Sluigi	}
182887902Sluigi#endif /* DEVICE_POLLING */
182987902Sluigi
183050974Swpaul	/* Supress unwanted interrupts */
183150974Swpaul	if (!(ifp->if_flags & IFF_UP)) {
183250974Swpaul		sis_stop(sc);
183386984Sluigi		goto done;
183450974Swpaul	}
183550974Swpaul
183650974Swpaul	/* Disable interrupts. */
183750974Swpaul	CSR_WRITE_4(sc, SIS_IER, 0);
183850974Swpaul
183950974Swpaul	for (;;) {
184050974Swpaul		/* Reading the ISR register clears all interrupts. */
184150974Swpaul		status = CSR_READ_4(sc, SIS_ISR);
184250974Swpaul
184350974Swpaul		if ((status & SIS_INTRS) == 0)
184450974Swpaul			break;
184550974Swpaul
184686984Sluigi		if (status &
184786984Sluigi		    (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
184886984Sluigi		     SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
184950974Swpaul			sis_txeof(sc);
185050974Swpaul
185186984Sluigi		if (status & (SIS_ISR_RX_DESC_OK|SIS_ISR_RX_OK|SIS_ISR_RX_IDLE))
185250974Swpaul			sis_rxeof(sc);
185350974Swpaul
185486984Sluigi		if (status & (SIS_ISR_RX_ERR | SIS_ISR_RX_OFLOW))
185550974Swpaul			sis_rxeoc(sc);
185650974Swpaul
185786984Sluigi		if (status & (SIS_ISR_RX_IDLE))
185886984Sluigi			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
185986984Sluigi
186050974Swpaul		if (status & SIS_ISR_SYSERR) {
186150974Swpaul			sis_reset(sc);
186250974Swpaul			sis_init(sc);
186350974Swpaul		}
186450974Swpaul	}
186550974Swpaul
186650974Swpaul	/* Re-enable interrupts. */
186750974Swpaul	CSR_WRITE_4(sc, SIS_IER, 1);
186850974Swpaul
186950974Swpaul	if (ifp->if_snd.ifq_head != NULL)
187050974Swpaul		sis_start(ifp);
187186984Sluigidone:
187267087Swpaul	SIS_UNLOCK(sc);
187367087Swpaul
187450974Swpaul	return;
187550974Swpaul}
187650974Swpaul
187750974Swpaul/*
187850974Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
187950974Swpaul * pointers to the fragment pointers.
188050974Swpaul */
1881102334Salfredstatic int
1882102334Salfredsis_encap(sc, m_head, txidx)
188350974Swpaul	struct sis_softc	*sc;
188450974Swpaul	struct mbuf		*m_head;
188550974Swpaul	u_int32_t		*txidx;
188650974Swpaul{
188750974Swpaul	struct sis_desc		*f = NULL;
188850974Swpaul	struct mbuf		*m;
1889112808Ssilby	int			frag, cur, cnt = 0, chainlen = 0;
189050974Swpaul
189150974Swpaul	/*
1892112808Ssilby	 * If there's no way we can send any packets, return now.
1893112808Ssilby	 */
1894112808Ssilby	if (SIS_TX_LIST_CNT - sc->sis_cdata.sis_tx_cnt < 2)
1895112808Ssilby		return (ENOBUFS);
1896112808Ssilby
1897112808Ssilby	/*
1898112808Ssilby	 * Count the number of frags in this chain to see if
1899112808Ssilby	 * we need to m_defrag.  Since the descriptor list is shared
1900112808Ssilby	 * by all packets, we'll m_defrag long chains so that they
1901112808Ssilby	 * do not use up the entire list, even if they would fit.
1902112808Ssilby	 */
1903112808Ssilby
1904112808Ssilby	for (m = m_head; m != NULL; m = m->m_next)
1905112808Ssilby		chainlen++;
1906112808Ssilby
1907112808Ssilby	if ((chainlen > SIS_TX_LIST_CNT / 4) ||
1908112808Ssilby	    ((SIS_TX_LIST_CNT - (chainlen + sc->sis_cdata.sis_tx_cnt)) < 2)) {
1909112808Ssilby		m = m_defrag(m_head, M_DONTWAIT);
1910112808Ssilby		if (m == NULL)
1911112808Ssilby			return (ENOBUFS);
1912112808Ssilby		m_head = m;
1913112808Ssilby	}
1914112808Ssilby
1915112808Ssilby	/*
191650974Swpaul 	 * Start packing the mbufs in this chain into
191750974Swpaul	 * the fragment pointers. Stop when we run out
191850974Swpaul 	 * of fragments or hit the end of the mbuf chain.
191950974Swpaul	 */
192050974Swpaul	m = m_head;
192150974Swpaul	cur = frag = *txidx;
192250974Swpaul
192350974Swpaul	for (m = m_head; m != NULL; m = m->m_next) {
192450974Swpaul		if (m->m_len != 0) {
192551042Swpaul			if ((SIS_TX_LIST_CNT -
192650974Swpaul			    (sc->sis_cdata.sis_tx_cnt + cnt)) < 2)
192750974Swpaul				return(ENOBUFS);
192881713Swpaul			f = &sc->sis_ldata.sis_tx_list[frag];
192950974Swpaul			f->sis_ctl = SIS_CMDSTS_MORE | m->m_len;
193081713Swpaul			bus_dmamap_create(sc->sis_tag, 0, &f->sis_map);
193181713Swpaul			bus_dmamap_load(sc->sis_tag, f->sis_map,
193281713Swpaul			    mtod(m, void *), m->m_len,
193381713Swpaul			    sis_dma_map_desc_ptr, f, 0);
193481713Swpaul			bus_dmamap_sync(sc->sis_tag,
193581713Swpaul			    f->sis_map, BUS_DMASYNC_PREREAD);
193650974Swpaul			if (cnt != 0)
193750974Swpaul				f->sis_ctl |= SIS_CMDSTS_OWN;
193850974Swpaul			cur = frag;
193950974Swpaul			SIS_INC(frag, SIS_TX_LIST_CNT);
194050974Swpaul			cnt++;
194150974Swpaul		}
194250974Swpaul	}
194350974Swpaul
194450974Swpaul	if (m != NULL)
194550974Swpaul		return(ENOBUFS);
194650974Swpaul
194781713Swpaul	sc->sis_ldata.sis_tx_list[cur].sis_mbuf = m_head;
194881713Swpaul	sc->sis_ldata.sis_tx_list[cur].sis_ctl &= ~SIS_CMDSTS_MORE;
194981713Swpaul	sc->sis_ldata.sis_tx_list[*txidx].sis_ctl |= SIS_CMDSTS_OWN;
195050974Swpaul	sc->sis_cdata.sis_tx_cnt += cnt;
195150974Swpaul	*txidx = frag;
195250974Swpaul
195350974Swpaul	return(0);
195450974Swpaul}
195550974Swpaul
195650974Swpaul/*
195750974Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
195850974Swpaul * to the mbuf data regions directly in the transmit lists. We also save a
195950974Swpaul * copy of the pointers since the transmit list fragment pointers are
196050974Swpaul * physical addresses.
196150974Swpaul */
196250974Swpaul
1963102334Salfredstatic void
1964102334Salfredsis_start(ifp)
196550974Swpaul	struct ifnet		*ifp;
196650974Swpaul{
196750974Swpaul	struct sis_softc	*sc;
196850974Swpaul	struct mbuf		*m_head = NULL;
196950974Swpaul	u_int32_t		idx;
197050974Swpaul
197150974Swpaul	sc = ifp->if_softc;
197267087Swpaul	SIS_LOCK(sc);
197350974Swpaul
197467087Swpaul	if (!sc->sis_link) {
197567087Swpaul		SIS_UNLOCK(sc);
197664963Swpaul		return;
197767087Swpaul	}
197864963Swpaul
197950974Swpaul	idx = sc->sis_cdata.sis_tx_prod;
198050974Swpaul
198167087Swpaul	if (ifp->if_flags & IFF_OACTIVE) {
198267087Swpaul		SIS_UNLOCK(sc);
198350974Swpaul		return;
198467087Swpaul	}
198550974Swpaul
198681713Swpaul	while(sc->sis_ldata.sis_tx_list[idx].sis_mbuf == NULL) {
198750974Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
198850974Swpaul		if (m_head == NULL)
198950974Swpaul			break;
199050974Swpaul
199150974Swpaul		if (sis_encap(sc, m_head, &idx)) {
199250974Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
199350974Swpaul			ifp->if_flags |= IFF_OACTIVE;
199450974Swpaul			break;
199550974Swpaul		}
199650974Swpaul
199750974Swpaul		/*
199850974Swpaul		 * If there's a BPF listener, bounce a copy of this frame
199950974Swpaul		 * to him.
200050974Swpaul		 */
2001106936Ssam		BPF_MTAP(ifp, m_head);
200251583Swpaul
200350974Swpaul	}
200450974Swpaul
200550974Swpaul	/* Transmit */
200650974Swpaul	sc->sis_cdata.sis_tx_prod = idx;
200750974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
200850974Swpaul
200950974Swpaul	/*
201050974Swpaul	 * Set a timeout in case the chip goes out to lunch.
201150974Swpaul	 */
201250974Swpaul	ifp->if_timer = 5;
201350974Swpaul
201467087Swpaul	SIS_UNLOCK(sc);
201567087Swpaul
201650974Swpaul	return;
201750974Swpaul}
201850974Swpaul
2019102334Salfredstatic void
2020102334Salfredsis_init(xsc)
202150974Swpaul	void			*xsc;
202250974Swpaul{
202350974Swpaul	struct sis_softc	*sc = xsc;
202450974Swpaul	struct ifnet		*ifp = &sc->arpcom.ac_if;
202550974Swpaul	struct mii_data		*mii;
202650974Swpaul
202767087Swpaul	SIS_LOCK(sc);
202850974Swpaul
202950974Swpaul	/*
203050974Swpaul	 * Cancel pending I/O and free all RX/TX buffers.
203150974Swpaul	 */
203250974Swpaul	sis_stop(sc);
203350974Swpaul
203450974Swpaul	mii = device_get_softc(sc->sis_miibus);
203550974Swpaul
203650974Swpaul	/* Set MAC address */
203762672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
203862672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
203962672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
204062672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
204162672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
204262672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
204362672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
204462672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
204562672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
204662672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
204762672Swpaul	} else {
204862672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
204962672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
205062672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
205162672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
205262672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
205362672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
205462672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
205562672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
205662672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
205762672Swpaul	}
205850974Swpaul
205950974Swpaul	/* Init circular RX list. */
206050974Swpaul	if (sis_list_rx_init(sc) == ENOBUFS) {
206150974Swpaul		printf("sis%d: initialization failed: no "
206250974Swpaul			"memory for rx buffers\n", sc->sis_unit);
206350974Swpaul		sis_stop(sc);
206467087Swpaul		SIS_UNLOCK(sc);
206550974Swpaul		return;
206650974Swpaul	}
206750974Swpaul
206850974Swpaul	/*
206950974Swpaul	 * Init tx descriptors.
207050974Swpaul	 */
207150974Swpaul	sis_list_tx_init(sc);
207250974Swpaul
207362672Swpaul	/*
207462672Swpaul	 * For the NatSemi chip, we have to explicitly enable the
207562672Swpaul	 * reception of ARP frames, as well as turn on the 'perfect
207662672Swpaul	 * match' filter where we store the station address, otherwise
207762672Swpaul	 * we won't receive unicasts meant for this host.
207862672Swpaul	 */
207962672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
208062672Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
208162672Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
208262672Swpaul	}
208362672Swpaul
208450974Swpaul	 /* If we want promiscuous mode, set the allframes bit. */
208550974Swpaul	if (ifp->if_flags & IFF_PROMISC) {
208650974Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
208750974Swpaul	} else {
208850974Swpaul		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
208950974Swpaul	}
209050974Swpaul
209150974Swpaul	/*
209250974Swpaul	 * Set the capture broadcast bit to capture broadcast frames.
209350974Swpaul	 */
209450974Swpaul	if (ifp->if_flags & IFF_BROADCAST) {
209550974Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
209650974Swpaul	} else {
209750974Swpaul		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
209850974Swpaul	}
209950974Swpaul
210050974Swpaul	/*
210150974Swpaul	 * Load the multicast filter.
210250974Swpaul	 */
210362672Swpaul	if (sc->sis_type == SIS_TYPE_83815)
210462672Swpaul		sis_setmulti_ns(sc);
210562672Swpaul	else
210662672Swpaul		sis_setmulti_sis(sc);
210750974Swpaul
210850974Swpaul	/* Turn the receive filter on */
210950974Swpaul	SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
211050974Swpaul
211150974Swpaul	/*
211250974Swpaul	 * Load the address of the RX and TX lists.
211350974Swpaul	 */
211481713Swpaul	CSR_WRITE_4(sc, SIS_RX_LISTPTR, sc->sis_cdata.sis_rx_paddr);
211581713Swpaul	CSR_WRITE_4(sc, SIS_TX_LISTPTR, sc->sis_cdata.sis_tx_paddr);
211650974Swpaul
2117109059Smbr	/* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
2118109059Smbr	 * the PCI bus. When this bit is set, the Max DMA Burst Size
2119109059Smbr	 * for TX/RX DMA should be no larger than 16 double words.
2120109059Smbr	 */
2121109059Smbr	if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
2122109059Smbr		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
2123109059Smbr	} else {
2124109059Smbr		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
2125109059Smbr	}
212664963Swpaul
2127109059Smbr
212887390Sjhay	/* Accept Long Packets for VLAN support */
212987390Sjhay	SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
213087390Sjhay
213150974Swpaul	/* Set TX configuration */
213264963Swpaul	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
213364963Swpaul		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
213464963Swpaul	} else {
213564963Swpaul		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
213664963Swpaul	}
213750974Swpaul
213864963Swpaul	/* Set full/half duplex mode. */
213964963Swpaul	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
214064963Swpaul		SIS_SETBIT(sc, SIS_TX_CFG,
214164963Swpaul		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
214264963Swpaul		SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
214364963Swpaul	} else {
214464963Swpaul		SIS_CLRBIT(sc, SIS_TX_CFG,
214564963Swpaul		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
214664963Swpaul		SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
214764963Swpaul	}
214864963Swpaul
2149119130Ssam	if (sc->sis_type == SIS_TYPE_83815 &&
2150119130Ssam	     IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
2151119130Ssam		uint32_t reg;
2152119130Ssam
2153119130Ssam		/*
2154119130Ssam		 * Some DP83815s experience problems when used with short
2155119130Ssam		 * (< 30m/100ft) Ethernet cables in 100BaseTX mode.  This
2156119130Ssam		 * sequence adjusts the DSP's signal attenuation to fix the
2157119130Ssam		 * problem.
2158119130Ssam		 */
2159119130Ssam		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
2160119130Ssam
2161119130Ssam		reg = CSR_READ_4(sc, NS_PHY_DSPCFG);
2162119130Ssam		CSR_WRITE_4(sc, NS_PHY_DSPCFG, (reg & 0xfff) | 0x1000);
2163119130Ssam		DELAY(100);
2164119130Ssam		reg = CSR_READ_4(sc, NS_PHY_TDATA);
2165119130Ssam		if ((reg & 0x0080) == 0 || (reg & 0xff) >= 0xd8) {
2166119130Ssam			CSR_WRITE_4(sc, NS_PHY_TDATA, 0x00e8);
2167119130Ssam			SIS_SETBIT(sc, NS_PHY_DSPCFG, 0x20);
2168119130Ssam		}
2169119130Ssam		CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
2170119130Ssam	}
2171119130Ssam
217250974Swpaul	/*
217350974Swpaul	 * Enable interrupts.
217450974Swpaul	 */
217550974Swpaul	CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
217687902Sluigi#ifdef DEVICE_POLLING
217787902Sluigi	/*
217887902Sluigi	 * ... only enable interrupts if we are not polling, make sure
217987902Sluigi	 * they are off otherwise.
218087902Sluigi	 */
2181102052Ssobomax	if (ifp->if_flags & IFF_POLLING)
218287902Sluigi		CSR_WRITE_4(sc, SIS_IER, 0);
218387902Sluigi	else
218487902Sluigi#endif /* DEVICE_POLLING */
218550974Swpaul	CSR_WRITE_4(sc, SIS_IER, 1);
218650974Swpaul
218750974Swpaul	/* Enable receiver and transmitter. */
218850974Swpaul	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
218950974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
219050974Swpaul
219164963Swpaul#ifdef notdef
219250974Swpaul	mii_mediachg(mii);
219364963Swpaul#endif
219450974Swpaul
219564963Swpaul	/*
219664963Swpaul	 * Page 75 of the DP83815 manual recommends the
219764963Swpaul	 * following register settings "for optimum
219864963Swpaul	 * performance." Note however that at least three
219964963Swpaul	 * of the registers are listed as "reserved" in
220064963Swpaul	 * the register map, so who knows what they do.
220164963Swpaul	 */
220264963Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
220364963Swpaul		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
220464963Swpaul		CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
220564963Swpaul		CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
220664963Swpaul		CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
220764963Swpaul		CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
220864963Swpaul	}
220964963Swpaul
221050974Swpaul	ifp->if_flags |= IFF_RUNNING;
221150974Swpaul	ifp->if_flags &= ~IFF_OACTIVE;
221250974Swpaul
2213117858Scognet	if (!sc->in_tick)
2214117858Scognet		sc->sis_stat_ch = timeout(sis_tick, sc, hz);
221550974Swpaul
221667087Swpaul	SIS_UNLOCK(sc);
221767087Swpaul
221850974Swpaul	return;
221950974Swpaul}
222050974Swpaul
222150974Swpaul/*
222250974Swpaul * Set media options.
222350974Swpaul */
2224102334Salfredstatic int
2225102334Salfredsis_ifmedia_upd(ifp)
222650974Swpaul	struct ifnet		*ifp;
222750974Swpaul{
222850974Swpaul	struct sis_softc	*sc;
222964963Swpaul	struct mii_data		*mii;
223050974Swpaul
223150974Swpaul	sc = ifp->if_softc;
223250974Swpaul
223364963Swpaul	mii = device_get_softc(sc->sis_miibus);
223464963Swpaul	sc->sis_link = 0;
223564963Swpaul	if (mii->mii_instance) {
223664963Swpaul		struct mii_softc	*miisc;
223772012Sphk		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
223864963Swpaul			mii_phy_reset(miisc);
223964963Swpaul	}
224064963Swpaul	mii_mediachg(mii);
224150974Swpaul
224250974Swpaul	return(0);
224350974Swpaul}
224450974Swpaul
224550974Swpaul/*
224650974Swpaul * Report current media status.
224750974Swpaul */
2248102334Salfredstatic void
2249102334Salfredsis_ifmedia_sts(ifp, ifmr)
225050974Swpaul	struct ifnet		*ifp;
225150974Swpaul	struct ifmediareq	*ifmr;
225250974Swpaul{
225350974Swpaul	struct sis_softc	*sc;
225450974Swpaul	struct mii_data		*mii;
225550974Swpaul
225650974Swpaul	sc = ifp->if_softc;
225750974Swpaul
225850974Swpaul	mii = device_get_softc(sc->sis_miibus);
225950974Swpaul	mii_pollstat(mii);
226050974Swpaul	ifmr->ifm_active = mii->mii_media_active;
226150974Swpaul	ifmr->ifm_status = mii->mii_media_status;
226250974Swpaul
226350974Swpaul	return;
226450974Swpaul}
226550974Swpaul
2266102334Salfredstatic int
2267102334Salfredsis_ioctl(ifp, command, data)
226850974Swpaul	struct ifnet		*ifp;
226950974Swpaul	u_long			command;
227050974Swpaul	caddr_t			data;
227150974Swpaul{
227250974Swpaul	struct sis_softc	*sc = ifp->if_softc;
227350974Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
227450974Swpaul	struct mii_data		*mii;
227567087Swpaul	int			error = 0;
227650974Swpaul
227750974Swpaul	switch(command) {
227850974Swpaul	case SIOCSIFFLAGS:
227950974Swpaul		if (ifp->if_flags & IFF_UP) {
228050974Swpaul			sis_init(sc);
228150974Swpaul		} else {
228250974Swpaul			if (ifp->if_flags & IFF_RUNNING)
228350974Swpaul				sis_stop(sc);
228450974Swpaul		}
228550974Swpaul		error = 0;
228650974Swpaul		break;
228750974Swpaul	case SIOCADDMULTI:
228850974Swpaul	case SIOCDELMULTI:
228981713Swpaul		SIS_LOCK(sc);
229062672Swpaul		if (sc->sis_type == SIS_TYPE_83815)
229162672Swpaul			sis_setmulti_ns(sc);
229262672Swpaul		else
229362672Swpaul			sis_setmulti_sis(sc);
229481713Swpaul		SIS_UNLOCK(sc);
229550974Swpaul		error = 0;
229650974Swpaul		break;
229750974Swpaul	case SIOCGIFMEDIA:
229850974Swpaul	case SIOCSIFMEDIA:
229950974Swpaul		mii = device_get_softc(sc->sis_miibus);
230081713Swpaul		SIS_LOCK(sc);
230150974Swpaul		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
230281713Swpaul		SIS_UNLOCK(sc);
230350974Swpaul		break;
230450974Swpaul	default:
2305106936Ssam		error = ether_ioctl(ifp, command, data);
230650974Swpaul		break;
230750974Swpaul	}
230850974Swpaul
230950974Swpaul	return(error);
231050974Swpaul}
231150974Swpaul
2312102334Salfredstatic void
2313102334Salfredsis_watchdog(ifp)
231450974Swpaul	struct ifnet		*ifp;
231550974Swpaul{
231650974Swpaul	struct sis_softc	*sc;
231750974Swpaul
231850974Swpaul	sc = ifp->if_softc;
231950974Swpaul
232067087Swpaul	SIS_LOCK(sc);
232167087Swpaul
232250974Swpaul	ifp->if_oerrors++;
232350974Swpaul	printf("sis%d: watchdog timeout\n", sc->sis_unit);
232450974Swpaul
232550974Swpaul	sis_stop(sc);
232650974Swpaul	sis_reset(sc);
232750974Swpaul	sis_init(sc);
232850974Swpaul
232950974Swpaul	if (ifp->if_snd.ifq_head != NULL)
233050974Swpaul		sis_start(ifp);
233150974Swpaul
233267087Swpaul	SIS_UNLOCK(sc);
233367087Swpaul
233450974Swpaul	return;
233550974Swpaul}
233650974Swpaul
233750974Swpaul/*
233850974Swpaul * Stop the adapter and free any mbufs allocated to the
233950974Swpaul * RX and TX lists.
234050974Swpaul */
2341102334Salfredstatic void
2342102334Salfredsis_stop(sc)
234350974Swpaul	struct sis_softc	*sc;
234450974Swpaul{
234550974Swpaul	register int		i;
234650974Swpaul	struct ifnet		*ifp;
234750974Swpaul
234867087Swpaul	SIS_LOCK(sc);
234950974Swpaul	ifp = &sc->arpcom.ac_if;
235050974Swpaul	ifp->if_timer = 0;
235150974Swpaul
235250974Swpaul	untimeout(sis_tick, sc, sc->sis_stat_ch);
235387472Speter
235487472Speter	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
235587902Sluigi#ifdef DEVICE_POLLING
235687902Sluigi	ether_poll_deregister(ifp);
235787902Sluigi#endif
235850974Swpaul	CSR_WRITE_4(sc, SIS_IER, 0);
235950974Swpaul	CSR_WRITE_4(sc, SIS_IMR, 0);
236050974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
236150974Swpaul	DELAY(1000);
236250974Swpaul	CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
236350974Swpaul	CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
236450974Swpaul
236564963Swpaul	sc->sis_link = 0;
236664963Swpaul
236750974Swpaul	/*
236850974Swpaul	 * Free data in the RX lists.
236950974Swpaul	 */
237050974Swpaul	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
237181713Swpaul		if (sc->sis_ldata.sis_rx_list[i].sis_mbuf != NULL) {
237281713Swpaul			bus_dmamap_unload(sc->sis_tag,
237381713Swpaul			    sc->sis_ldata.sis_rx_list[i].sis_map);
237481713Swpaul			bus_dmamap_destroy(sc->sis_tag,
237581713Swpaul			    sc->sis_ldata.sis_rx_list[i].sis_map);
237681713Swpaul			m_freem(sc->sis_ldata.sis_rx_list[i].sis_mbuf);
237781713Swpaul			sc->sis_ldata.sis_rx_list[i].sis_mbuf = NULL;
237850974Swpaul		}
237950974Swpaul	}
238081713Swpaul	bzero(sc->sis_ldata.sis_rx_list,
238181713Swpaul		sizeof(sc->sis_ldata.sis_rx_list));
238250974Swpaul
238350974Swpaul	/*
238450974Swpaul	 * Free the TX list buffers.
238550974Swpaul	 */
238650974Swpaul	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
238781713Swpaul		if (sc->sis_ldata.sis_tx_list[i].sis_mbuf != NULL) {
238881713Swpaul			bus_dmamap_unload(sc->sis_tag,
238981713Swpaul			    sc->sis_ldata.sis_tx_list[i].sis_map);
239081713Swpaul			bus_dmamap_destroy(sc->sis_tag,
239181713Swpaul			    sc->sis_ldata.sis_tx_list[i].sis_map);
239281713Swpaul			m_freem(sc->sis_ldata.sis_tx_list[i].sis_mbuf);
239381713Swpaul			sc->sis_ldata.sis_tx_list[i].sis_mbuf = NULL;
239450974Swpaul		}
239550974Swpaul	}
239650974Swpaul
239781713Swpaul	bzero(sc->sis_ldata.sis_tx_list,
239881713Swpaul		sizeof(sc->sis_ldata.sis_tx_list));
239950974Swpaul
240067087Swpaul	SIS_UNLOCK(sc);
240167087Swpaul
240250974Swpaul	return;
240350974Swpaul}
240450974Swpaul
240550974Swpaul/*
240650974Swpaul * Stop all chip I/O so that the kernel's probe routines don't
240750974Swpaul * get confused by errant DMAs when rebooting.
240850974Swpaul */
2409102334Salfredstatic void
2410102334Salfredsis_shutdown(dev)
241150974Swpaul	device_t		dev;
241250974Swpaul{
241350974Swpaul	struct sis_softc	*sc;
241450974Swpaul
241550974Swpaul	sc = device_get_softc(dev);
241667087Swpaul	SIS_LOCK(sc);
241750974Swpaul	sis_reset(sc);
241850974Swpaul	sis_stop(sc);
241967087Swpaul	SIS_UNLOCK(sc);
242050974Swpaul
242150974Swpaul	return;
242250974Swpaul}
2423