if_sis.c revision 121262
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 121262 2003-10-19 23:28:02Z silby $");
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" },
110119712Sphk	{ NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP8381[56] 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 *,
121121262Ssilby					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
1055119712Sphk	sc->sis_self = dev;
1056119712Sphk
105793818Sjhb	mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
105893818Sjhb	    MTX_DEF | MTX_RECURSE);
105969583Swpaul
106050974Swpaul	if (pci_get_device(dev) == SIS_DEVICEID_900)
106150974Swpaul		sc->sis_type = SIS_TYPE_900;
106250974Swpaul	if (pci_get_device(dev) == SIS_DEVICEID_7016)
106350974Swpaul		sc->sis_type = SIS_TYPE_7016;
106462672Swpaul	if (pci_get_vendor(dev) == NS_VENDORID)
106562672Swpaul		sc->sis_type = SIS_TYPE_83815;
106650974Swpaul
106789296Swpaul	sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
1068117208Simp#ifndef BURN_BRIDGES
106950974Swpaul	/*
107050974Swpaul	 * Handle power management nonsense.
107150974Swpaul	 */
107272813Swpaul	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
107372813Swpaul		u_int32_t		iobase, membase, irq;
107450974Swpaul
107572813Swpaul		/* Save important PCI config data. */
107672813Swpaul		iobase = pci_read_config(dev, SIS_PCI_LOIO, 4);
107772813Swpaul		membase = pci_read_config(dev, SIS_PCI_LOMEM, 4);
107872813Swpaul		irq = pci_read_config(dev, SIS_PCI_INTLINE, 4);
107950974Swpaul
108072813Swpaul		/* Reset the power state. */
108172813Swpaul		printf("sis%d: chip is in D%d power mode "
108272813Swpaul		    "-- setting to D0\n", unit,
108372813Swpaul		    pci_get_powerstate(dev));
108472813Swpaul		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
108550974Swpaul
108672813Swpaul		/* Restore PCI config data. */
108772813Swpaul		pci_write_config(dev, SIS_PCI_LOIO, iobase, 4);
108872813Swpaul		pci_write_config(dev, SIS_PCI_LOMEM, membase, 4);
108972813Swpaul		pci_write_config(dev, SIS_PCI_INTLINE, irq, 4);
109050974Swpaul	}
1091117208Simp#endif
109250974Swpaul	/*
109350974Swpaul	 * Map control/status registers.
109450974Swpaul	 */
109572813Swpaul	pci_enable_busmaster(dev);
109650974Swpaul
109750974Swpaul	rid = SIS_RID;
109850974Swpaul	sc->sis_res = bus_alloc_resource(dev, SIS_RES, &rid,
109950974Swpaul	    0, ~0, 1, RF_ACTIVE);
110050974Swpaul
110150974Swpaul	if (sc->sis_res == NULL) {
110250974Swpaul		printf("sis%d: couldn't map ports/memory\n", unit);
110350974Swpaul		error = ENXIO;
110450974Swpaul		goto fail;
110550974Swpaul	}
110650974Swpaul
110750974Swpaul	sc->sis_btag = rman_get_bustag(sc->sis_res);
110850974Swpaul	sc->sis_bhandle = rman_get_bushandle(sc->sis_res);
110950974Swpaul
111050974Swpaul	/* Allocate interrupt */
111150974Swpaul	rid = 0;
111250974Swpaul	sc->sis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
111350974Swpaul	    RF_SHAREABLE | RF_ACTIVE);
111450974Swpaul
111550974Swpaul	if (sc->sis_irq == NULL) {
111650974Swpaul		printf("sis%d: couldn't map interrupt\n", unit);
111750974Swpaul		error = ENXIO;
111850974Swpaul		goto fail;
111950974Swpaul	}
112050974Swpaul
112150974Swpaul	/* Reset the adapter. */
112250974Swpaul	sis_reset(sc);
112350974Swpaul
1124109976Smbr	if (sc->sis_type == SIS_TYPE_900 &&
1125109976Smbr            (sc->sis_rev == SIS_REV_635 ||
1126109976Smbr            sc->sis_rev == SIS_REV_900B)) {
1127109976Smbr		SIO_SET(SIS_CFG_RND_CNT);
1128109976Smbr		SIO_SET(SIS_CFG_PERR_DETECT);
1129109976Smbr	}
1130109976Smbr
113150974Swpaul	/*
113250974Swpaul	 * Get station address from the EEPROM.
113350974Swpaul	 */
113462672Swpaul	switch (pci_get_vendor(dev)) {
113562672Swpaul	case NS_VENDORID:
1136119712Sphk		sc->sis_srr = CSR_READ_4(sc, NS_SRR);
1137119712Sphk
1138119712Sphk		/* We can't update the device description, so spew */
1139119712Sphk		if (sc->sis_srr == NS_SRR_15C)
1140119712Sphk			device_printf(dev, "Silicon Revision: DP83815C\n");
1141119712Sphk		else if (sc->sis_srr == NS_SRR_15D)
1142119712Sphk			device_printf(dev, "Silicon Revision: DP83815D\n");
1143119712Sphk		else if (sc->sis_srr == NS_SRR_16A)
1144119712Sphk			device_printf(dev, "Silicon Revision: DP83816A\n");
1145119712Sphk		else
1146119712Sphk			device_printf(dev, "Silicon Revision %x\n", sc->sis_srr);
1147119712Sphk
114862672Swpaul		/*
114962672Swpaul		 * Reading the MAC address out of the EEPROM on
115062672Swpaul		 * the NatSemi chip takes a bit more work than
115162672Swpaul		 * you'd expect. The address spans 4 16-bit words,
115262672Swpaul		 * with the first word containing only a single bit.
115362672Swpaul		 * You have to shift everything over one bit to
115462672Swpaul		 * get it aligned properly. Also, the bits are
115562672Swpaul		 * stored backwards (the LSB is really the MSB,
115662672Swpaul		 * and so on) so you have to reverse them in order
115762672Swpaul		 * to get the MAC address into the form we want.
115862672Swpaul		 * Why? Who the hell knows.
115962672Swpaul		 */
116062672Swpaul		{
116162672Swpaul			u_int16_t		tmp[4];
116250974Swpaul
116362672Swpaul			sis_read_eeprom(sc, (caddr_t)&tmp,
116462672Swpaul			    NS_EE_NODEADDR, 4, 0);
116562672Swpaul
116662672Swpaul			/* Shift everything over one bit. */
116762672Swpaul			tmp[3] = tmp[3] >> 1;
116862681Swpaul			tmp[3] |= tmp[2] << 15;
116962672Swpaul			tmp[2] = tmp[2] >> 1;
117062681Swpaul			tmp[2] |= tmp[1] << 15;
117162672Swpaul			tmp[1] = tmp[1] >> 1;
117262681Swpaul			tmp[1] |= tmp[0] << 15;
117362672Swpaul
117462672Swpaul			/* Now reverse all the bits. */
117562672Swpaul			tmp[3] = sis_reverse(tmp[3]);
117662672Swpaul			tmp[2] = sis_reverse(tmp[2]);
117762672Swpaul			tmp[1] = sis_reverse(tmp[1]);
117862672Swpaul
117962672Swpaul			bcopy((char *)&tmp[1], eaddr, ETHER_ADDR_LEN);
118062672Swpaul		}
118162672Swpaul		break;
118262672Swpaul	case SIS_VENDORID:
118362672Swpaul	default:
118472197Swpaul#ifdef __i386__
118572197Swpaul		/*
118672197Swpaul		 * If this is a SiS 630E chipset with an embedded
118772197Swpaul		 * SiS 900 controller, we have to read the MAC address
118872197Swpaul		 * from the APC CMOS RAM. Our method for doing this
118972197Swpaul		 * is very ugly since we have to reach out and grab
119072197Swpaul		 * ahold of hardware for which we cannot properly
119172197Swpaul		 * allocate resources. This code is only compiled on
119272197Swpaul		 * the i386 architecture since the SiS 630E chipset
119372197Swpaul		 * is for x86 motherboards only. Note that there are
119472197Swpaul		 * a lot of magic numbers in this hack. These are
119572197Swpaul		 * taken from SiS's Linux driver. I'd like to replace
119672197Swpaul		 * them with proper symbolic definitions, but that
119772197Swpaul		 * requires some datasheets that I don't have access
119872197Swpaul		 * to at the moment.
119972197Swpaul		 */
120089296Swpaul		if (sc->sis_rev == SIS_REV_630S ||
120189296Swpaul		    sc->sis_rev == SIS_REV_630E ||
120290328Sambrisko		    sc->sis_rev == SIS_REV_630EA1)
120372197Swpaul			sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
120489296Swpaul
120590328Sambrisko		else if (sc->sis_rev == SIS_REV_635 ||
120690328Sambrisko			 sc->sis_rev == SIS_REV_630ET)
120789296Swpaul			sis_read_mac(sc, dev, (caddr_t)&eaddr);
1208109061Smbr		else if (sc->sis_rev == SIS_REV_96x) {
1209109061Smbr			/* Allow to read EEPROM from LAN. It is shared
1210109061Smbr			 * between a 1394 controller and the NIC and each
1211109061Smbr			 * time we access it, we need to set SIS_EECMD_REQ.
1212109061Smbr			 */
1213109061Smbr			SIO_SET(SIS_EECMD_REQ);
1214109061Smbr			for (waittime = 0; waittime < SIS_TIMEOUT;
1215109061Smbr			    waittime++) {
1216109061Smbr				/* Force EEPROM to idle state. */
1217109061Smbr				sis_eeprom_idle(sc);
1218109061Smbr				if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1219109061Smbr					sis_read_eeprom(sc, (caddr_t)&eaddr,
1220109061Smbr					    SIS_EE_NODEADDR, 3, 0);
1221109061Smbr					break;
1222109061Smbr				}
1223109061Smbr				DELAY(1);
1224109061Smbr			}
1225109061Smbr			/*
1226109061Smbr			 * Set SIS_EECTL_CLK to high, so a other master
1227109061Smbr			 * can operate on the i2c bus.
1228109061Smbr			 */
1229109061Smbr			SIO_SET(SIS_EECTL_CLK);
1230109061Smbr			/* Refuse EEPROM access by LAN */
1231109061Smbr			SIO_SET(SIS_EECMD_DONE);
1232109061Smbr		} else
123372197Swpaul#endif
123472197Swpaul			sis_read_eeprom(sc, (caddr_t)&eaddr,
123572197Swpaul			    SIS_EE_NODEADDR, 3, 0);
123662672Swpaul		break;
123762672Swpaul	}
123862672Swpaul
123950974Swpaul	/*
124050974Swpaul	 * A SiS chip was detected. Inform the world.
124150974Swpaul	 */
124250974Swpaul	printf("sis%d: Ethernet address: %6D\n", unit, eaddr, ":");
124350974Swpaul
124450974Swpaul	sc->sis_unit = unit;
1245119785Ssam	callout_init(&sc->sis_stat_ch, CALLOUT_MPSAFE);
124650974Swpaul	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
124750974Swpaul
124881713Swpaul	/*
124981713Swpaul	 * Allocate the parent bus DMA tag appropriate for PCI.
125081713Swpaul	 */
125181713Swpaul#define SIS_NSEG_NEW 32
125281713Swpaul	 error = bus_dma_tag_create(NULL,	/* parent */
125381713Swpaul			1, 0,			/* alignment, boundary */
125481713Swpaul			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
125581713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
125681713Swpaul			NULL, NULL,		/* filter, filterarg */
125781713Swpaul			MAXBSIZE, SIS_NSEG_NEW,	/* maxsize, nsegments */
125881713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
125981713Swpaul			BUS_DMA_ALLOCNOW,	/* flags */
1260117126Sscottl			NULL, NULL,		/* lockfunc, lockarg */
126181713Swpaul			&sc->sis_parent_tag);
1262112872Snjl	if (error)
1263112872Snjl		goto fail;
126450974Swpaul
126581713Swpaul	/*
1266112872Snjl	 * Now allocate a tag for the DMA descriptor lists and a chunk
1267112872Snjl	 * of DMA-able memory based on the tag.  Also obtain the physical
1268112872Snjl	 * addresses of the RX and TX ring, which we'll need later.
126981713Swpaul	 * All of our lists are allocated as a contiguous block
127081713Swpaul	 * of memory.
127181713Swpaul	 */
127281713Swpaul	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
127381713Swpaul			1, 0,			/* alignment, boundary */
127481713Swpaul			BUS_SPACE_MAXADDR,	/* lowaddr */
127581713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
127681713Swpaul			NULL, NULL,		/* filter, filterarg */
127781713Swpaul			SIS_RX_LIST_SZ, 1,	/* maxsize,nsegments */
127881713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
127981713Swpaul			0,			/* flags */
1280117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1281117126Sscottl			&Giant,			/* lockarg */
128281713Swpaul			&sc->sis_ldata.sis_rx_tag);
1283112872Snjl	if (error)
1284112872Snjl		goto fail;
128581713Swpaul
1286112872Snjl	error = bus_dmamem_alloc(sc->sis_ldata.sis_rx_tag,
1287118089Smux	    (void **)&sc->sis_ldata.sis_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1288112872Snjl	    &sc->sis_ldata.sis_rx_dmamap);
1289112872Snjl
1290112872Snjl	if (error) {
1291112872Snjl		printf("sis%d: no memory for rx list buffers!\n", unit);
1292112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1293112872Snjl		sc->sis_ldata.sis_rx_tag = NULL;
1294112872Snjl		goto fail;
1295112872Snjl	}
1296112872Snjl
1297112872Snjl	error = bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
1298112872Snjl	    sc->sis_ldata.sis_rx_dmamap, &(sc->sis_ldata.sis_rx_list[0]),
1299112872Snjl	    sizeof(struct sis_desc), sis_dma_map_ring,
1300112872Snjl	    &sc->sis_cdata.sis_rx_paddr, 0);
1301112872Snjl
1302112872Snjl	if (error) {
1303112872Snjl		printf("sis%d: cannot get address of the rx ring!\n", unit);
1304112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1305112872Snjl		    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1306112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1307112872Snjl		sc->sis_ldata.sis_rx_tag = NULL;
1308112872Snjl		goto fail;
1309112872Snjl	}
1310112872Snjl
131181713Swpaul	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
131281713Swpaul			1, 0,			/* alignment, boundary */
131381713Swpaul			BUS_SPACE_MAXADDR,	/* lowaddr */
131481713Swpaul			BUS_SPACE_MAXADDR,	/* highaddr */
131581713Swpaul			NULL, NULL,		/* filter, filterarg */
131681713Swpaul			SIS_TX_LIST_SZ, 1,	/* maxsize,nsegments */
131781713Swpaul			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
131881713Swpaul			0,			/* flags */
1319117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1320117126Sscottl			&Giant,			/* lockarg */
132181713Swpaul			&sc->sis_ldata.sis_tx_tag);
1322112872Snjl	if (error)
1323112872Snjl		goto fail;
132481713Swpaul
132581713Swpaul	error = bus_dmamem_alloc(sc->sis_ldata.sis_tx_tag,
1326118089Smux	    (void **)&sc->sis_ldata.sis_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
132781713Swpaul	    &sc->sis_ldata.sis_tx_dmamap);
132881713Swpaul
132981713Swpaul	if (error) {
1330112872Snjl		printf("sis%d: no memory for tx list buffers!\n", unit);
133181713Swpaul		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1332112872Snjl		sc->sis_ldata.sis_tx_tag = NULL;
133350974Swpaul		goto fail;
133450974Swpaul	}
133550974Swpaul
1336112872Snjl	error = bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
1337112872Snjl	    sc->sis_ldata.sis_tx_dmamap, &(sc->sis_ldata.sis_tx_list[0]),
1338112872Snjl	    sizeof(struct sis_desc), sis_dma_map_ring,
1339112872Snjl	    &sc->sis_cdata.sis_tx_paddr, 0);
134081713Swpaul
134181713Swpaul	if (error) {
1342112872Snjl		printf("sis%d: cannot get address of the tx ring!\n", unit);
1343112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_tx_tag,
134481713Swpaul		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
134581713Swpaul		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1346112872Snjl		sc->sis_ldata.sis_tx_tag = NULL;
134781713Swpaul		goto fail;
134881713Swpaul	}
134981713Swpaul
1350112872Snjl	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
1351112872Snjl			1, 0,			/* alignment, boundary */
1352112872Snjl			BUS_SPACE_MAXADDR,	/* lowaddr */
1353112872Snjl			BUS_SPACE_MAXADDR,	/* highaddr */
1354112872Snjl			NULL, NULL,		/* filter, filterarg */
1355112872Snjl			MCLBYTES, 1,		/* maxsize,nsegments */
1356112872Snjl			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1357112872Snjl			0,			/* flags */
1358117126Sscottl			busdma_lock_mutex,	/* lockfunc */
1359117126Sscottl			&Giant,			/* lockarg */
1360112872Snjl			&sc->sis_tag);
1361112872Snjl	if (error)
1362112872Snjl		goto fail;
136381713Swpaul
136481713Swpaul	/*
136581713Swpaul	 * Obtain the physical addresses of the RX and TX
136681713Swpaul	 * rings which we'll need later in the init routine.
136781713Swpaul	 */
136881713Swpaul
136950974Swpaul	ifp = &sc->arpcom.ac_if;
137050974Swpaul	ifp->if_softc = sc;
137150974Swpaul	ifp->if_unit = unit;
137250974Swpaul	ifp->if_name = "sis";
137350974Swpaul	ifp->if_mtu = ETHERMTU;
137450974Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
137550974Swpaul	ifp->if_ioctl = sis_ioctl;
137650974Swpaul	ifp->if_output = ether_output;
137750974Swpaul	ifp->if_start = sis_start;
137850974Swpaul	ifp->if_watchdog = sis_watchdog;
137950974Swpaul	ifp->if_init = sis_init;
138050974Swpaul	ifp->if_baudrate = 10000000;
138150974Swpaul	ifp->if_snd.ifq_maxlen = SIS_TX_LIST_CNT - 1;
138250974Swpaul
138350974Swpaul	/*
138450974Swpaul	 * Do MII setup.
138550974Swpaul	 */
138650974Swpaul	if (mii_phy_probe(dev, &sc->sis_miibus,
138750974Swpaul	    sis_ifmedia_upd, sis_ifmedia_sts)) {
138850974Swpaul		printf("sis%d: MII without any PHY!\n", sc->sis_unit);
138950974Swpaul		error = ENXIO;
139050974Swpaul		goto fail;
139150974Swpaul	}
139250974Swpaul
139350974Swpaul	/*
139463090Sarchie	 * Call MI attach routine.
139550974Swpaul	 */
1396106936Ssam	ether_ifattach(ifp, eaddr);
139787390Sjhay
139887390Sjhay	/*
139987390Sjhay	 * Tell the upper layer(s) we support long frames.
140087390Sjhay	 */
140187390Sjhay	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1402106936Ssam	ifp->if_capabilities |= IFCAP_VLAN_MTU;
140387390Sjhay
1404113609Snjl	/* Hook interrupt last to avoid having to lock softc */
1405112872Snjl	error = bus_setup_intr(dev, sc->sis_irq, INTR_TYPE_NET,
1406112872Snjl	    sis_intr, sc, &sc->sis_intrhand);
140750974Swpaul
1408112872Snjl	if (error) {
1409112872Snjl		printf("sis%d: couldn't set up irq\n", unit);
1410113609Snjl		ether_ifdetach(ifp);
1411112872Snjl		goto fail;
1412112872Snjl	}
1413112872Snjl
141450974Swpaulfail:
1415112872Snjl	if (error)
1416112872Snjl		sis_detach(dev);
1417112872Snjl
141850974Swpaul	return(error);
141950974Swpaul}
142050974Swpaul
1421113609Snjl/*
1422113609Snjl * Shutdown hardware and free up resources. This can be called any
1423113609Snjl * time after the mutex has been initialized. It is called in both
1424113609Snjl * the error case in attach and the normal detach case so it needs
1425113609Snjl * to be careful about only freeing resources that have actually been
1426113609Snjl * allocated.
1427113609Snjl */
1428102334Salfredstatic int
1429102334Salfredsis_detach(dev)
143050974Swpaul	device_t		dev;
143150974Swpaul{
143250974Swpaul	struct sis_softc	*sc;
143350974Swpaul	struct ifnet		*ifp;
143450974Swpaul
143550974Swpaul	sc = device_get_softc(dev);
1436112880Sjhb	KASSERT(mtx_initialized(&sc->sis_mtx), ("sis mutex not initialized"));
143767087Swpaul	SIS_LOCK(sc);
143850974Swpaul	ifp = &sc->arpcom.ac_if;
143950974Swpaul
1440118089Smux	/* These should only be active if attach succeeded. */
1441113812Simp	if (device_is_attached(dev)) {
1442113609Snjl		sis_reset(sc);
1443113609Snjl		sis_stop(sc);
1444112872Snjl		ether_ifdetach(ifp);
1445113609Snjl	}
1446113609Snjl	if (sc->sis_miibus)
1447112872Snjl		device_delete_child(dev, sc->sis_miibus);
1448113609Snjl	bus_generic_detach(dev);
144950974Swpaul
1450112872Snjl	if (sc->sis_intrhand)
1451112872Snjl		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1452112872Snjl	if (sc->sis_irq)
1453112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1454112872Snjl	if (sc->sis_res)
1455112872Snjl		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
145650974Swpaul
1457112872Snjl	if (sc->sis_ldata.sis_rx_tag) {
1458112872Snjl		bus_dmamap_unload(sc->sis_ldata.sis_rx_tag,
1459112872Snjl		    sc->sis_ldata.sis_rx_dmamap);
1460112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1461112872Snjl		    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1462112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1463112872Snjl	}
1464112872Snjl	if (sc->sis_ldata.sis_tx_tag) {
1465112872Snjl		bus_dmamap_unload(sc->sis_ldata.sis_tx_tag,
1466112872Snjl		    sc->sis_ldata.sis_tx_dmamap);
1467112872Snjl		bus_dmamem_free(sc->sis_ldata.sis_tx_tag,
1468112872Snjl		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
1469112872Snjl		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1470112872Snjl	}
1471112872Snjl	if (sc->sis_parent_tag)
1472112872Snjl		bus_dma_tag_destroy(sc->sis_parent_tag);
1473112872Snjl	if (sc->sis_tag)
1474112872Snjl		bus_dma_tag_destroy(sc->sis_tag);
147550974Swpaul
147667087Swpaul	SIS_UNLOCK(sc);
147767087Swpaul	mtx_destroy(&sc->sis_mtx);
147850974Swpaul
147950974Swpaul	return(0);
148050974Swpaul}
148150974Swpaul
148250974Swpaul/*
148350974Swpaul * Initialize the transmit descriptors.
148450974Swpaul */
1485102334Salfredstatic int
1486102334Salfredsis_list_tx_init(sc)
148750974Swpaul	struct sis_softc	*sc;
148850974Swpaul{
148950974Swpaul	struct sis_list_data	*ld;
149050974Swpaul	struct sis_ring_data	*cd;
149187059Sluigi	int			i, nexti;
149250974Swpaul
149350974Swpaul	cd = &sc->sis_cdata;
149481713Swpaul	ld = &sc->sis_ldata;
149550974Swpaul
149650974Swpaul	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
149787102Sluigi		nexti = (i == (SIS_TX_LIST_CNT - 1)) ? 0 : i+1;
149850974Swpaul			ld->sis_tx_list[i].sis_nextdesc =
149987059Sluigi			    &ld->sis_tx_list[nexti];
150081713Swpaul			bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
150181713Swpaul			    sc->sis_ldata.sis_tx_dmamap,
150287059Sluigi			    &ld->sis_tx_list[nexti], sizeof(struct sis_desc),
150381713Swpaul			    sis_dma_map_desc_next, &ld->sis_tx_list[i], 0);
150450974Swpaul		ld->sis_tx_list[i].sis_mbuf = NULL;
150550974Swpaul		ld->sis_tx_list[i].sis_ptr = 0;
150650974Swpaul		ld->sis_tx_list[i].sis_ctl = 0;
150750974Swpaul	}
150850974Swpaul
150950974Swpaul	cd->sis_tx_prod = cd->sis_tx_cons = cd->sis_tx_cnt = 0;
151050974Swpaul
151181713Swpaul	bus_dmamap_sync(sc->sis_ldata.sis_tx_tag,
151281713Swpaul	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
151381713Swpaul
151450974Swpaul	return(0);
151550974Swpaul}
151650974Swpaul
151750974Swpaul/*
151850974Swpaul * Initialize the RX descriptors and allocate mbufs for them. Note that
151950974Swpaul * we arrange the descriptors in a closed ring, so that the last descriptor
152050974Swpaul * points back to the first.
152150974Swpaul */
1522102334Salfredstatic int
1523102334Salfredsis_list_rx_init(sc)
152450974Swpaul	struct sis_softc	*sc;
152550974Swpaul{
152650974Swpaul	struct sis_list_data	*ld;
152750974Swpaul	struct sis_ring_data	*cd;
152887059Sluigi	int			i,nexti;
152950974Swpaul
153081713Swpaul	ld = &sc->sis_ldata;
153150974Swpaul	cd = &sc->sis_cdata;
153250974Swpaul
153350974Swpaul	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
153450974Swpaul		if (sis_newbuf(sc, &ld->sis_rx_list[i], NULL) == ENOBUFS)
153550974Swpaul			return(ENOBUFS);
153687102Sluigi		nexti = (i == (SIS_RX_LIST_CNT - 1)) ? 0 : i+1;
153750974Swpaul			ld->sis_rx_list[i].sis_nextdesc =
153887059Sluigi			    &ld->sis_rx_list[nexti];
153981713Swpaul			bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
154081713Swpaul			    sc->sis_ldata.sis_rx_dmamap,
154187059Sluigi			    &ld->sis_rx_list[nexti],
154281713Swpaul			    sizeof(struct sis_desc), sis_dma_map_desc_next,
154381713Swpaul			    &ld->sis_rx_list[i], 0);
154450974Swpaul		}
154550974Swpaul
154681713Swpaul	bus_dmamap_sync(sc->sis_ldata.sis_rx_tag,
154781713Swpaul	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
154881713Swpaul
154950974Swpaul	cd->sis_rx_prod = 0;
155050974Swpaul
155150974Swpaul	return(0);
155250974Swpaul}
155350974Swpaul
155450974Swpaul/*
155550974Swpaul * Initialize an RX descriptor and attach an MBUF cluster.
155650974Swpaul */
1557102334Salfredstatic int
1558102334Salfredsis_newbuf(sc, c, m)
155950974Swpaul	struct sis_softc	*sc;
156050974Swpaul	struct sis_desc		*c;
156150974Swpaul	struct mbuf		*m;
156250974Swpaul{
156350974Swpaul
156481713Swpaul	if (c == NULL)
156581713Swpaul		return(EINVAL);
156681713Swpaul
156750974Swpaul	if (m == NULL) {
1568111119Simp		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1569101340Sluigi		if (m == NULL)
157050974Swpaul			return(ENOBUFS);
1571101340Sluigi	} else
1572101340Sluigi		m->m_data = m->m_ext.ext_buf;
157350974Swpaul
1574101340Sluigi	c->sis_mbuf = m;
157550974Swpaul	c->sis_ctl = SIS_RXLEN;
157650974Swpaul
157781713Swpaul	bus_dmamap_create(sc->sis_tag, 0, &c->sis_map);
157881713Swpaul	bus_dmamap_load(sc->sis_tag, c->sis_map,
1579101464Sluigi	    mtod(m, void *), MCLBYTES,
158081713Swpaul	    sis_dma_map_desc_ptr, c, 0);
158181713Swpaul	bus_dmamap_sync(sc->sis_tag, c->sis_map, BUS_DMASYNC_PREWRITE);
158281713Swpaul
158350974Swpaul	return(0);
158450974Swpaul}
158550974Swpaul
158650974Swpaul/*
158750974Swpaul * A frame has been uploaded: pass the resulting mbuf chain up to
158850974Swpaul * the higher level protocols.
158950974Swpaul */
1590102334Salfredstatic void
1591102334Salfredsis_rxeof(sc)
159250974Swpaul	struct sis_softc	*sc;
159350974Swpaul{
159450974Swpaul        struct mbuf		*m;
159550974Swpaul        struct ifnet		*ifp;
159650974Swpaul	struct sis_desc		*cur_rx;
159750974Swpaul	int			i, total_len = 0;
159850974Swpaul	u_int32_t		rxstat;
159950974Swpaul
160050974Swpaul	ifp = &sc->arpcom.ac_if;
160150974Swpaul	i = sc->sis_cdata.sis_rx_prod;
160250974Swpaul
160381713Swpaul	while(SIS_OWNDESC(&sc->sis_ldata.sis_rx_list[i])) {
160450974Swpaul
160587902Sluigi#ifdef DEVICE_POLLING
1606102052Ssobomax		if (ifp->if_flags & IFF_POLLING) {
160787902Sluigi			if (sc->rxcycles <= 0)
160887902Sluigi				break;
160987902Sluigi			sc->rxcycles--;
161087902Sluigi		}
161187902Sluigi#endif /* DEVICE_POLLING */
161281713Swpaul		cur_rx = &sc->sis_ldata.sis_rx_list[i];
161350974Swpaul		rxstat = cur_rx->sis_rxstat;
161481713Swpaul		bus_dmamap_sync(sc->sis_tag,
161581713Swpaul		    cur_rx->sis_map, BUS_DMASYNC_POSTWRITE);
161681713Swpaul		bus_dmamap_unload(sc->sis_tag, cur_rx->sis_map);
161781713Swpaul		bus_dmamap_destroy(sc->sis_tag, cur_rx->sis_map);
161850974Swpaul		m = cur_rx->sis_mbuf;
161950974Swpaul		cur_rx->sis_mbuf = NULL;
162050974Swpaul		total_len = SIS_RXBYTES(cur_rx);
162150974Swpaul		SIS_INC(i, SIS_RX_LIST_CNT);
162250974Swpaul
162350974Swpaul		/*
162450974Swpaul		 * If an error occurs, update stats, clear the
162550974Swpaul		 * status word and leave the mbuf cluster in place:
162650974Swpaul		 * it should simply get re-used next time this descriptor
162750974Swpaul	 	 * comes up in the ring.
162850974Swpaul		 */
162950974Swpaul		if (!(rxstat & SIS_CMDSTS_PKT_OK)) {
163050974Swpaul			ifp->if_ierrors++;
163150974Swpaul			if (rxstat & SIS_RXSTAT_COLL)
163250974Swpaul				ifp->if_collisions++;
163350974Swpaul			sis_newbuf(sc, cur_rx, m);
163450974Swpaul			continue;
163550974Swpaul		}
163650974Swpaul
163750974Swpaul		/* No errors; receive the packet. */
163887059Sluigi#ifdef __i386__
163987059Sluigi		/*
164087059Sluigi		 * On the x86 we do not have alignment problems, so try to
164187059Sluigi		 * allocate a new buffer for the receive ring, and pass up
164287059Sluigi		 * the one where the packet is already, saving the expensive
164387059Sluigi		 * copy done in m_devget().
164487059Sluigi		 * If we are on an architecture with alignment problems, or
164587059Sluigi		 * if the allocation fails, then use m_devget and leave the
164687059Sluigi		 * existing buffer in the receive ring.
164787059Sluigi		 */
1648101464Sluigi		if (sis_newbuf(sc, cur_rx, NULL) == 0)
164987059Sluigi			m->m_pkthdr.len = m->m_len = total_len;
1650101464Sluigi		else
165187059Sluigi#endif
165287059Sluigi		{
165387059Sluigi			struct mbuf		*m0;
165487059Sluigi			m0 = m_devget(mtod(m, char *), total_len,
165587059Sluigi				ETHER_ALIGN, ifp, NULL);
165687059Sluigi			sis_newbuf(sc, cur_rx, m);
165787059Sluigi			if (m0 == NULL) {
165887059Sluigi				ifp->if_ierrors++;
165987059Sluigi				continue;
166087059Sluigi			}
166187059Sluigi			m = m0;
166250974Swpaul		}
166350974Swpaul
166450974Swpaul		ifp->if_ipackets++;
1665106936Ssam		m->m_pkthdr.rcvif = ifp;
1666106936Ssam
1667106936Ssam		(*ifp->if_input)(ifp, m);
166850974Swpaul	}
166950974Swpaul
167050974Swpaul	sc->sis_cdata.sis_rx_prod = i;
167150974Swpaul
167250974Swpaul	return;
167350974Swpaul}
167450974Swpaul
1675105219Sphkstatic void
1676102334Salfredsis_rxeoc(sc)
167750974Swpaul	struct sis_softc	*sc;
167850974Swpaul{
167950974Swpaul	sis_rxeof(sc);
168050974Swpaul	sis_init(sc);
168150974Swpaul	return;
168250974Swpaul}
168350974Swpaul
168450974Swpaul/*
168550974Swpaul * A frame was downloaded to the chip. It's safe for us to clean up
168650974Swpaul * the list buffers.
168750974Swpaul */
168850974Swpaul
1689102334Salfredstatic void
1690102334Salfredsis_txeof(sc)
169150974Swpaul	struct sis_softc	*sc;
169250974Swpaul{
169350974Swpaul	struct ifnet		*ifp;
169450974Swpaul	u_int32_t		idx;
169550974Swpaul
169650974Swpaul	ifp = &sc->arpcom.ac_if;
169750974Swpaul
169850974Swpaul	/*
169950974Swpaul	 * Go through our tx list and free mbufs for those
170050974Swpaul	 * frames that have been transmitted.
170150974Swpaul	 */
170299163Sluigi	for (idx = sc->sis_cdata.sis_tx_cons; sc->sis_cdata.sis_tx_cnt > 0;
170399163Sluigi	    sc->sis_cdata.sis_tx_cnt--, SIS_INC(idx, SIS_TX_LIST_CNT) ) {
170499163Sluigi		struct sis_desc *cur_tx = &sc->sis_ldata.sis_tx_list[idx];
170550974Swpaul
170650974Swpaul		if (SIS_OWNDESC(cur_tx))
170750974Swpaul			break;
170850974Swpaul
170999163Sluigi		if (cur_tx->sis_ctl & SIS_CMDSTS_MORE)
171050974Swpaul			continue;
171150974Swpaul
171250974Swpaul		if (!(cur_tx->sis_ctl & SIS_CMDSTS_PKT_OK)) {
171350974Swpaul			ifp->if_oerrors++;
171450974Swpaul			if (cur_tx->sis_txstat & SIS_TXSTAT_EXCESSCOLLS)
171550974Swpaul				ifp->if_collisions++;
171650974Swpaul			if (cur_tx->sis_txstat & SIS_TXSTAT_OUTOFWINCOLL)
171750974Swpaul				ifp->if_collisions++;
171850974Swpaul		}
171950974Swpaul
172050974Swpaul		ifp->if_collisions +=
172150974Swpaul		    (cur_tx->sis_txstat & SIS_TXSTAT_COLLCNT) >> 16;
172250974Swpaul
172350974Swpaul		ifp->if_opackets++;
172450974Swpaul		if (cur_tx->sis_mbuf != NULL) {
172550974Swpaul			m_freem(cur_tx->sis_mbuf);
172650974Swpaul			cur_tx->sis_mbuf = NULL;
172781713Swpaul			bus_dmamap_unload(sc->sis_tag, cur_tx->sis_map);
172881713Swpaul			bus_dmamap_destroy(sc->sis_tag, cur_tx->sis_map);
172950974Swpaul		}
173099163Sluigi	}
173150974Swpaul
173299163Sluigi	if (idx != sc->sis_cdata.sis_tx_cons) {
173399163Sluigi		/* we freed up some buffers */
173499163Sluigi		sc->sis_cdata.sis_tx_cons = idx;
173599163Sluigi		ifp->if_flags &= ~IFF_OACTIVE;
173650974Swpaul	}
173750974Swpaul
173899163Sluigi	ifp->if_timer = (sc->sis_cdata.sis_tx_cnt == 0) ? 0 : 5;
173950974Swpaul
174050974Swpaul	return;
174150974Swpaul}
174250974Swpaul
1743102334Salfredstatic void
1744102334Salfredsis_tick(xsc)
174550974Swpaul	void			*xsc;
174650974Swpaul{
174750974Swpaul	struct sis_softc	*sc;
174850974Swpaul	struct mii_data		*mii;
174964963Swpaul	struct ifnet		*ifp;
175050974Swpaul
175150974Swpaul	sc = xsc;
175267087Swpaul	SIS_LOCK(sc);
1753117858Scognet	sc->in_tick = 1;
175464963Swpaul	ifp = &sc->arpcom.ac_if;
175564963Swpaul
175650974Swpaul	mii = device_get_softc(sc->sis_miibus);
175750974Swpaul	mii_tick(mii);
175864963Swpaul
175984147Sjlemon	if (!sc->sis_link && mii->mii_media_status & IFM_ACTIVE &&
176084147Sjlemon	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
176184147Sjlemon		sc->sis_link++;
176284147Sjlemon		if (ifp->if_snd.ifq_head != NULL)
176384147Sjlemon			sis_start(ifp);
176464963Swpaul	}
176564963Swpaul
1766119785Ssam	callout_reset(&sc->sis_stat_ch, hz,  sis_tick, sc);
1767117858Scognet	sc->in_tick = 0;
176867087Swpaul	SIS_UNLOCK(sc);
176950974Swpaul
177050974Swpaul	return;
177150974Swpaul}
177250974Swpaul
177387902Sluigi#ifdef DEVICE_POLLING
177487902Sluigistatic poll_handler_t sis_poll;
177587902Sluigi
177687902Sluigistatic void
177787902Sluigisis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
177887902Sluigi{
177987973Speter	struct	sis_softc *sc = ifp->if_softc;
178087973Speter
178187902Sluigi	SIS_LOCK(sc);
178287902Sluigi	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
178387902Sluigi		CSR_WRITE_4(sc, SIS_IER, 1);
178487902Sluigi		goto done;
178587902Sluigi	}
178687902Sluigi
178787902Sluigi	/*
178887902Sluigi	 * On the sis, reading the status register also clears it.
178987902Sluigi	 * So before returning to intr mode we must make sure that all
179087902Sluigi	 * possible pending sources of interrupts have been served.
179187902Sluigi	 * In practice this means run to completion the *eof routines,
179287902Sluigi	 * and then call the interrupt routine
179387902Sluigi	 */
179487902Sluigi	sc->rxcycles = count;
179587902Sluigi	sis_rxeof(sc);
179687902Sluigi	sis_txeof(sc);
179787902Sluigi	if (ifp->if_snd.ifq_head != NULL)
179887902Sluigi		sis_start(ifp);
179987902Sluigi
180087902Sluigi	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
180187902Sluigi		u_int32_t	status;
180287902Sluigi
180387902Sluigi		/* Reading the ISR register clears all interrupts. */
180487902Sluigi		status = CSR_READ_4(sc, SIS_ISR);
180587902Sluigi
180687902Sluigi		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
180787902Sluigi			sis_rxeoc(sc);
180887902Sluigi
180987902Sluigi		if (status & (SIS_ISR_RX_IDLE))
181087902Sluigi			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
181187902Sluigi
181287902Sluigi		if (status & SIS_ISR_SYSERR) {
181387902Sluigi			sis_reset(sc);
181487902Sluigi			sis_init(sc);
181587902Sluigi		}
181687902Sluigi	}
181787902Sluigidone:
181887902Sluigi	SIS_UNLOCK(sc);
181987902Sluigi	return;
182087902Sluigi}
182187902Sluigi#endif /* DEVICE_POLLING */
182287902Sluigi
1823102334Salfredstatic void
1824102334Salfredsis_intr(arg)
182550974Swpaul	void			*arg;
182650974Swpaul{
182750974Swpaul	struct sis_softc	*sc;
182850974Swpaul	struct ifnet		*ifp;
182950974Swpaul	u_int32_t		status;
183050974Swpaul
183150974Swpaul	sc = arg;
183250974Swpaul	ifp = &sc->arpcom.ac_if;
183350974Swpaul
183486984Sluigi	SIS_LOCK(sc);
183587902Sluigi#ifdef DEVICE_POLLING
1836102052Ssobomax	if (ifp->if_flags & IFF_POLLING)
183787902Sluigi		goto done;
183887902Sluigi	if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */
183987902Sluigi		CSR_WRITE_4(sc, SIS_IER, 0);
184087902Sluigi		goto done;
184187902Sluigi	}
184287902Sluigi#endif /* DEVICE_POLLING */
184387902Sluigi
184450974Swpaul	/* Supress unwanted interrupts */
184550974Swpaul	if (!(ifp->if_flags & IFF_UP)) {
184650974Swpaul		sis_stop(sc);
184786984Sluigi		goto done;
184850974Swpaul	}
184950974Swpaul
185050974Swpaul	/* Disable interrupts. */
185150974Swpaul	CSR_WRITE_4(sc, SIS_IER, 0);
185250974Swpaul
185350974Swpaul	for (;;) {
185450974Swpaul		/* Reading the ISR register clears all interrupts. */
185550974Swpaul		status = CSR_READ_4(sc, SIS_ISR);
185650974Swpaul
185750974Swpaul		if ((status & SIS_INTRS) == 0)
185850974Swpaul			break;
185950974Swpaul
186086984Sluigi		if (status &
186186984Sluigi		    (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
186286984Sluigi		     SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
186350974Swpaul			sis_txeof(sc);
186450974Swpaul
186586984Sluigi		if (status & (SIS_ISR_RX_DESC_OK|SIS_ISR_RX_OK|SIS_ISR_RX_IDLE))
186650974Swpaul			sis_rxeof(sc);
186750974Swpaul
186886984Sluigi		if (status & (SIS_ISR_RX_ERR | SIS_ISR_RX_OFLOW))
186950974Swpaul			sis_rxeoc(sc);
187050974Swpaul
187186984Sluigi		if (status & (SIS_ISR_RX_IDLE))
187286984Sluigi			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
187386984Sluigi
187450974Swpaul		if (status & SIS_ISR_SYSERR) {
187550974Swpaul			sis_reset(sc);
187650974Swpaul			sis_init(sc);
187750974Swpaul		}
187850974Swpaul	}
187950974Swpaul
188050974Swpaul	/* Re-enable interrupts. */
188150974Swpaul	CSR_WRITE_4(sc, SIS_IER, 1);
188250974Swpaul
188350974Swpaul	if (ifp->if_snd.ifq_head != NULL)
188450974Swpaul		sis_start(ifp);
188586984Sluigidone:
188667087Swpaul	SIS_UNLOCK(sc);
188767087Swpaul
188850974Swpaul	return;
188950974Swpaul}
189050974Swpaul
189150974Swpaul/*
189250974Swpaul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
189350974Swpaul * pointers to the fragment pointers.
189450974Swpaul */
1895102334Salfredstatic int
1896102334Salfredsis_encap(sc, m_head, txidx)
189750974Swpaul	struct sis_softc	*sc;
1898121262Ssilby	struct mbuf		**m_head;
189950974Swpaul	u_int32_t		*txidx;
190050974Swpaul{
190150974Swpaul	struct sis_desc		*f = NULL;
190250974Swpaul	struct mbuf		*m;
1903112808Ssilby	int			frag, cur, cnt = 0, chainlen = 0;
190450974Swpaul
190550974Swpaul	/*
1906112808Ssilby	 * If there's no way we can send any packets, return now.
1907112808Ssilby	 */
1908112808Ssilby	if (SIS_TX_LIST_CNT - sc->sis_cdata.sis_tx_cnt < 2)
1909112808Ssilby		return (ENOBUFS);
1910112808Ssilby
1911112808Ssilby	/*
1912112808Ssilby	 * Count the number of frags in this chain to see if
1913112808Ssilby	 * we need to m_defrag.  Since the descriptor list is shared
1914112808Ssilby	 * by all packets, we'll m_defrag long chains so that they
1915112808Ssilby	 * do not use up the entire list, even if they would fit.
1916112808Ssilby	 */
1917112808Ssilby
1918121262Ssilby	for (m = *m_head; m != NULL; m = m->m_next)
1919112808Ssilby		chainlen++;
1920112808Ssilby
1921112808Ssilby	if ((chainlen > SIS_TX_LIST_CNT / 4) ||
1922112808Ssilby	    ((SIS_TX_LIST_CNT - (chainlen + sc->sis_cdata.sis_tx_cnt)) < 2)) {
1923121262Ssilby		m = m_defrag(*m_head, M_DONTWAIT);
1924112808Ssilby		if (m == NULL)
1925112808Ssilby			return (ENOBUFS);
1926121262Ssilby		*m_head = m;
1927112808Ssilby	}
1928112808Ssilby
1929112808Ssilby	/*
193050974Swpaul 	 * Start packing the mbufs in this chain into
193150974Swpaul	 * the fragment pointers. Stop when we run out
193250974Swpaul 	 * of fragments or hit the end of the mbuf chain.
193350974Swpaul	 */
193450974Swpaul	cur = frag = *txidx;
193550974Swpaul
1936121262Ssilby	for (m = *m_head; m != NULL; m = m->m_next) {
193750974Swpaul		if (m->m_len != 0) {
193851042Swpaul			if ((SIS_TX_LIST_CNT -
193950974Swpaul			    (sc->sis_cdata.sis_tx_cnt + cnt)) < 2)
194050974Swpaul				return(ENOBUFS);
194181713Swpaul			f = &sc->sis_ldata.sis_tx_list[frag];
194250974Swpaul			f->sis_ctl = SIS_CMDSTS_MORE | m->m_len;
194381713Swpaul			bus_dmamap_create(sc->sis_tag, 0, &f->sis_map);
194481713Swpaul			bus_dmamap_load(sc->sis_tag, f->sis_map,
194581713Swpaul			    mtod(m, void *), m->m_len,
194681713Swpaul			    sis_dma_map_desc_ptr, f, 0);
194781713Swpaul			bus_dmamap_sync(sc->sis_tag,
194881713Swpaul			    f->sis_map, BUS_DMASYNC_PREREAD);
194950974Swpaul			if (cnt != 0)
195050974Swpaul				f->sis_ctl |= SIS_CMDSTS_OWN;
195150974Swpaul			cur = frag;
195250974Swpaul			SIS_INC(frag, SIS_TX_LIST_CNT);
195350974Swpaul			cnt++;
195450974Swpaul		}
195550974Swpaul	}
195650974Swpaul
195750974Swpaul	if (m != NULL)
195850974Swpaul		return(ENOBUFS);
195950974Swpaul
1960121262Ssilby	sc->sis_ldata.sis_tx_list[cur].sis_mbuf = *m_head;
196181713Swpaul	sc->sis_ldata.sis_tx_list[cur].sis_ctl &= ~SIS_CMDSTS_MORE;
196281713Swpaul	sc->sis_ldata.sis_tx_list[*txidx].sis_ctl |= SIS_CMDSTS_OWN;
196350974Swpaul	sc->sis_cdata.sis_tx_cnt += cnt;
196450974Swpaul	*txidx = frag;
196550974Swpaul
196650974Swpaul	return(0);
196750974Swpaul}
196850974Swpaul
196950974Swpaul/*
197050974Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
197150974Swpaul * to the mbuf data regions directly in the transmit lists. We also save a
197250974Swpaul * copy of the pointers since the transmit list fragment pointers are
197350974Swpaul * physical addresses.
197450974Swpaul */
197550974Swpaul
1976102334Salfredstatic void
1977102334Salfredsis_start(ifp)
197850974Swpaul	struct ifnet		*ifp;
197950974Swpaul{
198050974Swpaul	struct sis_softc	*sc;
198150974Swpaul	struct mbuf		*m_head = NULL;
198250974Swpaul	u_int32_t		idx;
198350974Swpaul
198450974Swpaul	sc = ifp->if_softc;
198567087Swpaul	SIS_LOCK(sc);
198650974Swpaul
198767087Swpaul	if (!sc->sis_link) {
198867087Swpaul		SIS_UNLOCK(sc);
198964963Swpaul		return;
199067087Swpaul	}
199164963Swpaul
199250974Swpaul	idx = sc->sis_cdata.sis_tx_prod;
199350974Swpaul
199467087Swpaul	if (ifp->if_flags & IFF_OACTIVE) {
199567087Swpaul		SIS_UNLOCK(sc);
199650974Swpaul		return;
199767087Swpaul	}
199850974Swpaul
199981713Swpaul	while(sc->sis_ldata.sis_tx_list[idx].sis_mbuf == NULL) {
200050974Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
200150974Swpaul		if (m_head == NULL)
200250974Swpaul			break;
200350974Swpaul
2004121262Ssilby		if (sis_encap(sc, &m_head, &idx)) {
200550974Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
200650974Swpaul			ifp->if_flags |= IFF_OACTIVE;
200750974Swpaul			break;
200850974Swpaul		}
200950974Swpaul
201050974Swpaul		/*
201150974Swpaul		 * If there's a BPF listener, bounce a copy of this frame
201250974Swpaul		 * to him.
201350974Swpaul		 */
2014106936Ssam		BPF_MTAP(ifp, m_head);
201551583Swpaul
201650974Swpaul	}
201750974Swpaul
201850974Swpaul	/* Transmit */
201950974Swpaul	sc->sis_cdata.sis_tx_prod = idx;
202050974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
202150974Swpaul
202250974Swpaul	/*
202350974Swpaul	 * Set a timeout in case the chip goes out to lunch.
202450974Swpaul	 */
202550974Swpaul	ifp->if_timer = 5;
202650974Swpaul
202767087Swpaul	SIS_UNLOCK(sc);
202867087Swpaul
202950974Swpaul	return;
203050974Swpaul}
203150974Swpaul
2032102334Salfredstatic void
2033102334Salfredsis_init(xsc)
203450974Swpaul	void			*xsc;
203550974Swpaul{
203650974Swpaul	struct sis_softc	*sc = xsc;
203750974Swpaul	struct ifnet		*ifp = &sc->arpcom.ac_if;
203850974Swpaul	struct mii_data		*mii;
203950974Swpaul
204067087Swpaul	SIS_LOCK(sc);
204150974Swpaul
204250974Swpaul	/*
204350974Swpaul	 * Cancel pending I/O and free all RX/TX buffers.
204450974Swpaul	 */
204550974Swpaul	sis_stop(sc);
204650974Swpaul
2047119712Sphk#ifdef notyet
2048119712Sphk	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
2049119712Sphk		/*
2050119712Sphk		 * Configure 400usec of interrupt holdoff.  This is based
2051119712Sphk		 * on emperical tests on a Soekris 4801.
2052119712Sphk 		 */
2053119712Sphk		CSR_WRITE_4(sc, NS_IHR, 0x100 | 4);
2054119712Sphk	}
2055119712Sphk#endif
2056119712Sphk
205750974Swpaul	mii = device_get_softc(sc->sis_miibus);
205850974Swpaul
205950974Swpaul	/* Set MAC address */
206062672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
206162672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
206262672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
206362672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
206462672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
206562672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
206662672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
206762672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
206862672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
206962672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
207062672Swpaul	} else {
207162672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
207262672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
207362672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
207462672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
207562672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
207662672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
207762672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
207862672Swpaul		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
207962672Swpaul		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
208062672Swpaul	}
208150974Swpaul
208250974Swpaul	/* Init circular RX list. */
208350974Swpaul	if (sis_list_rx_init(sc) == ENOBUFS) {
208450974Swpaul		printf("sis%d: initialization failed: no "
208550974Swpaul			"memory for rx buffers\n", sc->sis_unit);
208650974Swpaul		sis_stop(sc);
208767087Swpaul		SIS_UNLOCK(sc);
208850974Swpaul		return;
208950974Swpaul	}
209050974Swpaul
209150974Swpaul	/*
209250974Swpaul	 * Init tx descriptors.
209350974Swpaul	 */
209450974Swpaul	sis_list_tx_init(sc);
209550974Swpaul
209662672Swpaul	/*
209762672Swpaul	 * For the NatSemi chip, we have to explicitly enable the
209862672Swpaul	 * reception of ARP frames, as well as turn on the 'perfect
209962672Swpaul	 * match' filter where we store the station address, otherwise
210062672Swpaul	 * we won't receive unicasts meant for this host.
210162672Swpaul	 */
210262672Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
210362672Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
210462672Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
210562672Swpaul	}
210662672Swpaul
210750974Swpaul	 /* If we want promiscuous mode, set the allframes bit. */
210850974Swpaul	if (ifp->if_flags & IFF_PROMISC) {
210950974Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
211050974Swpaul	} else {
211150974Swpaul		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
211250974Swpaul	}
211350974Swpaul
211450974Swpaul	/*
211550974Swpaul	 * Set the capture broadcast bit to capture broadcast frames.
211650974Swpaul	 */
211750974Swpaul	if (ifp->if_flags & IFF_BROADCAST) {
211850974Swpaul		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
211950974Swpaul	} else {
212050974Swpaul		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
212150974Swpaul	}
212250974Swpaul
212350974Swpaul	/*
212450974Swpaul	 * Load the multicast filter.
212550974Swpaul	 */
212662672Swpaul	if (sc->sis_type == SIS_TYPE_83815)
212762672Swpaul		sis_setmulti_ns(sc);
212862672Swpaul	else
212962672Swpaul		sis_setmulti_sis(sc);
213050974Swpaul
213150974Swpaul	/* Turn the receive filter on */
213250974Swpaul	SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
213350974Swpaul
213450974Swpaul	/*
213550974Swpaul	 * Load the address of the RX and TX lists.
213650974Swpaul	 */
213781713Swpaul	CSR_WRITE_4(sc, SIS_RX_LISTPTR, sc->sis_cdata.sis_rx_paddr);
213881713Swpaul	CSR_WRITE_4(sc, SIS_TX_LISTPTR, sc->sis_cdata.sis_tx_paddr);
213950974Swpaul
2140109059Smbr	/* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
2141109059Smbr	 * the PCI bus. When this bit is set, the Max DMA Burst Size
2142109059Smbr	 * for TX/RX DMA should be no larger than 16 double words.
2143109059Smbr	 */
2144109059Smbr	if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
2145109059Smbr		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
2146109059Smbr	} else {
2147109059Smbr		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
2148109059Smbr	}
214964963Swpaul
2150109059Smbr
215187390Sjhay	/* Accept Long Packets for VLAN support */
215287390Sjhay	SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
215387390Sjhay
215450974Swpaul	/* Set TX configuration */
215564963Swpaul	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
215664963Swpaul		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
215764963Swpaul	} else {
215864963Swpaul		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
215964963Swpaul	}
216050974Swpaul
216164963Swpaul	/* Set full/half duplex mode. */
216264963Swpaul	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
216364963Swpaul		SIS_SETBIT(sc, SIS_TX_CFG,
216464963Swpaul		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
216564963Swpaul		SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
216664963Swpaul	} else {
216764963Swpaul		SIS_CLRBIT(sc, SIS_TX_CFG,
216864963Swpaul		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
216964963Swpaul		SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
217064963Swpaul	}
217164963Swpaul
2172119712Sphk	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr < NS_SRR_16A &&
2173119130Ssam	     IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
2174119130Ssam		uint32_t reg;
2175119130Ssam
2176119130Ssam		/*
2177119130Ssam		 * Some DP83815s experience problems when used with short
2178119130Ssam		 * (< 30m/100ft) Ethernet cables in 100BaseTX mode.  This
2179119130Ssam		 * sequence adjusts the DSP's signal attenuation to fix the
2180119130Ssam		 * problem.
2181119130Ssam		 */
2182119130Ssam		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
2183119130Ssam
2184119130Ssam		reg = CSR_READ_4(sc, NS_PHY_DSPCFG);
2185119130Ssam		CSR_WRITE_4(sc, NS_PHY_DSPCFG, (reg & 0xfff) | 0x1000);
2186119130Ssam		DELAY(100);
2187119130Ssam		reg = CSR_READ_4(sc, NS_PHY_TDATA);
2188119130Ssam		if ((reg & 0x0080) == 0 || (reg & 0xff) >= 0xd8) {
2189119712Sphk			device_printf(sc->sis_self, "Applying short cable fix (reg=%x)\n", reg);
2190119130Ssam			CSR_WRITE_4(sc, NS_PHY_TDATA, 0x00e8);
2191119130Ssam			SIS_SETBIT(sc, NS_PHY_DSPCFG, 0x20);
2192119130Ssam		}
2193119130Ssam		CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
2194119130Ssam	}
2195119130Ssam
219650974Swpaul	/*
219750974Swpaul	 * Enable interrupts.
219850974Swpaul	 */
219950974Swpaul	CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
220087902Sluigi#ifdef DEVICE_POLLING
220187902Sluigi	/*
220287902Sluigi	 * ... only enable interrupts if we are not polling, make sure
220387902Sluigi	 * they are off otherwise.
220487902Sluigi	 */
2205102052Ssobomax	if (ifp->if_flags & IFF_POLLING)
220687902Sluigi		CSR_WRITE_4(sc, SIS_IER, 0);
220787902Sluigi	else
220887902Sluigi#endif /* DEVICE_POLLING */
220950974Swpaul	CSR_WRITE_4(sc, SIS_IER, 1);
221050974Swpaul
221150974Swpaul	/* Enable receiver and transmitter. */
221250974Swpaul	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
221350974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
221450974Swpaul
221564963Swpaul#ifdef notdef
221650974Swpaul	mii_mediachg(mii);
221764963Swpaul#endif
221850974Swpaul
221964963Swpaul	/*
222064963Swpaul	 * Page 75 of the DP83815 manual recommends the
222164963Swpaul	 * following register settings "for optimum
222264963Swpaul	 * performance." Note however that at least three
222364963Swpaul	 * of the registers are listed as "reserved" in
222464963Swpaul	 * the register map, so who knows what they do.
222564963Swpaul	 */
222664963Swpaul	if (sc->sis_type == SIS_TYPE_83815) {
222764963Swpaul		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
222864963Swpaul		CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
222964963Swpaul		CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
223064963Swpaul		CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
223164963Swpaul		CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
223264963Swpaul	}
223364963Swpaul
223450974Swpaul	ifp->if_flags |= IFF_RUNNING;
223550974Swpaul	ifp->if_flags &= ~IFF_OACTIVE;
223650974Swpaul
2237117858Scognet	if (!sc->in_tick)
2238119785Ssam		callout_reset(&sc->sis_stat_ch, hz,  sis_tick, sc);
223950974Swpaul
224067087Swpaul	SIS_UNLOCK(sc);
224167087Swpaul
224250974Swpaul	return;
224350974Swpaul}
224450974Swpaul
224550974Swpaul/*
224650974Swpaul * Set media options.
224750974Swpaul */
2248102334Salfredstatic int
2249102334Salfredsis_ifmedia_upd(ifp)
225050974Swpaul	struct ifnet		*ifp;
225150974Swpaul{
225250974Swpaul	struct sis_softc	*sc;
225364963Swpaul	struct mii_data		*mii;
225450974Swpaul
225550974Swpaul	sc = ifp->if_softc;
225650974Swpaul
225764963Swpaul	mii = device_get_softc(sc->sis_miibus);
225864963Swpaul	sc->sis_link = 0;
225964963Swpaul	if (mii->mii_instance) {
226064963Swpaul		struct mii_softc	*miisc;
226172012Sphk		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
226264963Swpaul			mii_phy_reset(miisc);
226364963Swpaul	}
226464963Swpaul	mii_mediachg(mii);
226550974Swpaul
226650974Swpaul	return(0);
226750974Swpaul}
226850974Swpaul
226950974Swpaul/*
227050974Swpaul * Report current media status.
227150974Swpaul */
2272102334Salfredstatic void
2273102334Salfredsis_ifmedia_sts(ifp, ifmr)
227450974Swpaul	struct ifnet		*ifp;
227550974Swpaul	struct ifmediareq	*ifmr;
227650974Swpaul{
227750974Swpaul	struct sis_softc	*sc;
227850974Swpaul	struct mii_data		*mii;
227950974Swpaul
228050974Swpaul	sc = ifp->if_softc;
228150974Swpaul
228250974Swpaul	mii = device_get_softc(sc->sis_miibus);
228350974Swpaul	mii_pollstat(mii);
228450974Swpaul	ifmr->ifm_active = mii->mii_media_active;
228550974Swpaul	ifmr->ifm_status = mii->mii_media_status;
228650974Swpaul
228750974Swpaul	return;
228850974Swpaul}
228950974Swpaul
2290102334Salfredstatic int
2291102334Salfredsis_ioctl(ifp, command, data)
229250974Swpaul	struct ifnet		*ifp;
229350974Swpaul	u_long			command;
229450974Swpaul	caddr_t			data;
229550974Swpaul{
229650974Swpaul	struct sis_softc	*sc = ifp->if_softc;
229750974Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
229850974Swpaul	struct mii_data		*mii;
229967087Swpaul	int			error = 0;
230050974Swpaul
230150974Swpaul	switch(command) {
230250974Swpaul	case SIOCSIFFLAGS:
230350974Swpaul		if (ifp->if_flags & IFF_UP) {
230450974Swpaul			sis_init(sc);
230550974Swpaul		} else {
230650974Swpaul			if (ifp->if_flags & IFF_RUNNING)
230750974Swpaul				sis_stop(sc);
230850974Swpaul		}
230950974Swpaul		error = 0;
231050974Swpaul		break;
231150974Swpaul	case SIOCADDMULTI:
231250974Swpaul	case SIOCDELMULTI:
231381713Swpaul		SIS_LOCK(sc);
231462672Swpaul		if (sc->sis_type == SIS_TYPE_83815)
231562672Swpaul			sis_setmulti_ns(sc);
231662672Swpaul		else
231762672Swpaul			sis_setmulti_sis(sc);
231881713Swpaul		SIS_UNLOCK(sc);
231950974Swpaul		error = 0;
232050974Swpaul		break;
232150974Swpaul	case SIOCGIFMEDIA:
232250974Swpaul	case SIOCSIFMEDIA:
232350974Swpaul		mii = device_get_softc(sc->sis_miibus);
232481713Swpaul		SIS_LOCK(sc);
232550974Swpaul		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
232681713Swpaul		SIS_UNLOCK(sc);
232750974Swpaul		break;
232850974Swpaul	default:
2329106936Ssam		error = ether_ioctl(ifp, command, data);
233050974Swpaul		break;
233150974Swpaul	}
233250974Swpaul
233350974Swpaul	return(error);
233450974Swpaul}
233550974Swpaul
2336102334Salfredstatic void
2337102334Salfredsis_watchdog(ifp)
233850974Swpaul	struct ifnet		*ifp;
233950974Swpaul{
234050974Swpaul	struct sis_softc	*sc;
234150974Swpaul
234250974Swpaul	sc = ifp->if_softc;
234350974Swpaul
234467087Swpaul	SIS_LOCK(sc);
234567087Swpaul
234650974Swpaul	ifp->if_oerrors++;
234750974Swpaul	printf("sis%d: watchdog timeout\n", sc->sis_unit);
234850974Swpaul
234950974Swpaul	sis_stop(sc);
235050974Swpaul	sis_reset(sc);
235150974Swpaul	sis_init(sc);
235250974Swpaul
235350974Swpaul	if (ifp->if_snd.ifq_head != NULL)
235450974Swpaul		sis_start(ifp);
235550974Swpaul
235667087Swpaul	SIS_UNLOCK(sc);
235767087Swpaul
235850974Swpaul	return;
235950974Swpaul}
236050974Swpaul
236150974Swpaul/*
236250974Swpaul * Stop the adapter and free any mbufs allocated to the
236350974Swpaul * RX and TX lists.
236450974Swpaul */
2365102334Salfredstatic void
2366102334Salfredsis_stop(sc)
236750974Swpaul	struct sis_softc	*sc;
236850974Swpaul{
236950974Swpaul	register int		i;
237050974Swpaul	struct ifnet		*ifp;
237150974Swpaul
237267087Swpaul	SIS_LOCK(sc);
237350974Swpaul	ifp = &sc->arpcom.ac_if;
237450974Swpaul	ifp->if_timer = 0;
237550974Swpaul
2376119785Ssam	callout_stop(&sc->sis_stat_ch);
237787472Speter
237887472Speter	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
237987902Sluigi#ifdef DEVICE_POLLING
238087902Sluigi	ether_poll_deregister(ifp);
238187902Sluigi#endif
238250974Swpaul	CSR_WRITE_4(sc, SIS_IER, 0);
238350974Swpaul	CSR_WRITE_4(sc, SIS_IMR, 0);
238450974Swpaul	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
238550974Swpaul	DELAY(1000);
238650974Swpaul	CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
238750974Swpaul	CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
238850974Swpaul
238964963Swpaul	sc->sis_link = 0;
239064963Swpaul
239150974Swpaul	/*
239250974Swpaul	 * Free data in the RX lists.
239350974Swpaul	 */
239450974Swpaul	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
239581713Swpaul		if (sc->sis_ldata.sis_rx_list[i].sis_mbuf != NULL) {
239681713Swpaul			bus_dmamap_unload(sc->sis_tag,
239781713Swpaul			    sc->sis_ldata.sis_rx_list[i].sis_map);
239881713Swpaul			bus_dmamap_destroy(sc->sis_tag,
239981713Swpaul			    sc->sis_ldata.sis_rx_list[i].sis_map);
240081713Swpaul			m_freem(sc->sis_ldata.sis_rx_list[i].sis_mbuf);
240181713Swpaul			sc->sis_ldata.sis_rx_list[i].sis_mbuf = NULL;
240250974Swpaul		}
240350974Swpaul	}
240481713Swpaul	bzero(sc->sis_ldata.sis_rx_list,
240581713Swpaul		sizeof(sc->sis_ldata.sis_rx_list));
240650974Swpaul
240750974Swpaul	/*
240850974Swpaul	 * Free the TX list buffers.
240950974Swpaul	 */
241050974Swpaul	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
241181713Swpaul		if (sc->sis_ldata.sis_tx_list[i].sis_mbuf != NULL) {
241281713Swpaul			bus_dmamap_unload(sc->sis_tag,
241381713Swpaul			    sc->sis_ldata.sis_tx_list[i].sis_map);
241481713Swpaul			bus_dmamap_destroy(sc->sis_tag,
241581713Swpaul			    sc->sis_ldata.sis_tx_list[i].sis_map);
241681713Swpaul			m_freem(sc->sis_ldata.sis_tx_list[i].sis_mbuf);
241781713Swpaul			sc->sis_ldata.sis_tx_list[i].sis_mbuf = NULL;
241850974Swpaul		}
241950974Swpaul	}
242050974Swpaul
242181713Swpaul	bzero(sc->sis_ldata.sis_tx_list,
242281713Swpaul		sizeof(sc->sis_ldata.sis_tx_list));
242350974Swpaul
242467087Swpaul	SIS_UNLOCK(sc);
242567087Swpaul
242650974Swpaul	return;
242750974Swpaul}
242850974Swpaul
242950974Swpaul/*
243050974Swpaul * Stop all chip I/O so that the kernel's probe routines don't
243150974Swpaul * get confused by errant DMAs when rebooting.
243250974Swpaul */
2433102334Salfredstatic void
2434102334Salfredsis_shutdown(dev)
243550974Swpaul	device_t		dev;
243650974Swpaul{
243750974Swpaul	struct sis_softc	*sc;
243850974Swpaul
243950974Swpaul	sc = device_get_softc(dev);
244067087Swpaul	SIS_LOCK(sc);
244150974Swpaul	sis_reset(sc);
244250974Swpaul	sis_stop(sc);
244367087Swpaul	SIS_UNLOCK(sc);
244450974Swpaul
244550974Swpaul	return;
244650974Swpaul}
2447