if_sis.c revision 101464
1272343Sngie/*
2272343Sngie * Copyright (c) 1997, 1998, 1999
3272343Sngie *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer.
10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer in the
12272343Sngie *    documentation and/or other materials provided with the distribution.
13272343Sngie * 3. All advertising materials mentioning features or use of this software
14272343Sngie *    must display the following acknowledgement:
15272343Sngie *	This product includes software developed by Bill Paul.
16272343Sngie * 4. Neither the name of the author nor the names of any co-contributors
17272343Sngie *    may be used to endorse or promote products derived from this software
18272343Sngie *    without specific prior written permission.
19272343Sngie *
20272343Sngie * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30272343Sngie * THE POSSIBILITY OF SUCH DAMAGE.
31272343Sngie *
32272343Sngie * $FreeBSD: head/sys/pci/if_sis.c 101464 2002-08-07 16:08:54Z luigi $
33272343Sngie */
34272343Sngie
35272343Sngie/*
36272343Sngie * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
37272343Sngie * available from http://www.sis.com.tw.
38272343Sngie *
39272343Sngie * This driver also supports the NatSemi DP83815. Datasheets are
40272343Sngie * available from http://www.national.com.
41272343Sngie *
42272343Sngie * Written by Bill Paul <wpaul@ee.columbia.edu>
43272343Sngie * Electrical Engineering Department
44272343Sngie * Columbia University, New York City
45272343Sngie */
46272343Sngie
47272343Sngie/*
48272343Sngie * The SiS 900 is a fairly simple chip. It uses bus master DMA with
49272343Sngie * simple TX and RX descriptors of 3 longwords in size. The receiver
50272343Sngie * has a single perfect filter entry for the station address and a
51272343Sngie * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
52272343Sngie * transceiver while the 7016 requires an external transceiver chip.
53272343Sngie * Both chips offer the standard bit-bang MII interface as well as
54272343Sngie * an enchanced PHY interface which simplifies accessing MII registers.
55272343Sngie *
56272343Sngie * The only downside to this chipset is that RX descriptors must be
57272343Sngie * longword aligned.
58272343Sngie */
59272343Sngie
60272343Sngie#include <sys/param.h>
61272343Sngie#include <sys/systm.h>
62272343Sngie#include <sys/sockio.h>
63272343Sngie#include <sys/mbuf.h>
64272343Sngie#include <sys/malloc.h>
65272343Sngie#include <sys/kernel.h>
66272343Sngie#include <sys/socket.h>
67272343Sngie#include <sys/sysctl.h>
68272343Sngie
69272343Sngie#include <net/if.h>
70272343Sngie#include <net/if_arp.h>
71272343Sngie#include <net/ethernet.h>
72272343Sngie#include <net/if_dl.h>
73272343Sngie#include <net/if_media.h>
74272343Sngie#include <net/if_types.h>
75272343Sngie#include <net/if_vlan_var.h>
76272343Sngie
77272343Sngie#include <net/bpf.h>
78272343Sngie
79272343Sngie#include <machine/bus_pio.h>
80272343Sngie#include <machine/bus_memio.h>
81272343Sngie#include <machine/bus.h>
82272343Sngie#include <machine/resource.h>
83272343Sngie#include <sys/bus.h>
84272343Sngie#include <sys/rman.h>
85272343Sngie
86272343Sngie#include <dev/mii/mii.h>
87272343Sngie#include <dev/mii/miivar.h>
88272343Sngie
89272343Sngie#include <pci/pcireg.h>
90272343Sngie#include <pci/pcivar.h>
91272343Sngie
92272343Sngie#define SIS_USEIOSPACE
93272343Sngie
94272343Sngie#include <pci/if_sisreg.h>
95272343Sngie
96272343SngieMODULE_DEPEND(sis, miibus, 1, 1, 1);
97272343Sngie
98272343Sngie/* "controller miibus0" required.  See GENERIC if you get errors here. */
99272343Sngie#include "miibus_if.h"
100272343Sngie
101272343Sngie#ifndef lint
102272343Sngiestatic const char rcsid[] =
103272343Sngie  "$FreeBSD: head/sys/pci/if_sis.c 101464 2002-08-07 16:08:54Z luigi $";
104272343Sngie#endif
105272343Sngie
106272343Sngie/*
107272343Sngie * Various supported device vendors/types and their names.
108272343Sngie */
109272343Sngiestatic struct sis_type sis_devs[] = {
110272343Sngie	{ SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
111272343Sngie	{ SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
112272343Sngie	{ NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP83815 10/100BaseTX" },
113272343Sngie	{ 0, 0, NULL }
114272343Sngie};
115272343Sngie
116272343Sngiestatic int sis_probe		(device_t);
117272343Sngiestatic int sis_attach		(device_t);
118272343Sngiestatic int sis_detach		(device_t);
119272343Sngie
120272343Sngiestatic int sis_newbuf		(struct sis_softc *,
121272343Sngie					struct sis_desc *, struct mbuf *);
122272343Sngiestatic int sis_encap		(struct sis_softc *,
123272343Sngie					struct mbuf *, u_int32_t *);
124272343Sngiestatic void sis_rxeof		(struct sis_softc *);
125272343Sngiestatic void sis_rxeoc		(struct sis_softc *);
126272343Sngiestatic void sis_txeof		(struct sis_softc *);
127272343Sngiestatic void sis_intr		(void *);
128272343Sngiestatic void sis_tick		(void *);
129272343Sngiestatic void sis_start		(struct ifnet *);
130272343Sngiestatic int sis_ioctl		(struct ifnet *, u_long, caddr_t);
131272343Sngiestatic void sis_init		(void *);
132272343Sngiestatic void sis_stop		(struct sis_softc *);
133272343Sngiestatic void sis_watchdog		(struct ifnet *);
134272343Sngiestatic void sis_shutdown		(device_t);
135272343Sngiestatic int sis_ifmedia_upd	(struct ifnet *);
136272343Sngiestatic void sis_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
137272343Sngie
138272343Sngiestatic u_int16_t sis_reverse	(u_int16_t);
139272343Sngiestatic void sis_delay		(struct sis_softc *);
140272343Sngiestatic void sis_eeprom_idle	(struct sis_softc *);
141272343Sngiestatic void sis_eeprom_putbyte	(struct sis_softc *, int);
142272343Sngiestatic void sis_eeprom_getword	(struct sis_softc *, int, u_int16_t *);
143272343Sngiestatic void sis_read_eeprom	(struct sis_softc *, caddr_t, int, int, int);
144272343Sngie#ifdef __i386__
145272343Sngiestatic void sis_read_cmos	(struct sis_softc *, device_t, caddr_t,
146272343Sngie							int, int);
147272343Sngiestatic void sis_read_mac	(struct sis_softc *, device_t, caddr_t);
148272343Sngiestatic device_t sis_find_bridge	(device_t);
149272343Sngie#endif
150272343Sngie
151272343Sngiestatic int sis_miibus_readreg	(device_t, int, int);
152272343Sngiestatic int sis_miibus_writereg	(device_t, int, int, int);
153272343Sngiestatic void sis_miibus_statchg	(device_t);
154272343Sngie
155272343Sngiestatic void sis_setmulti_sis	(struct sis_softc *);
156272343Sngiestatic void sis_setmulti_ns	(struct sis_softc *);
157272343Sngiestatic u_int32_t sis_crc	(struct sis_softc *, caddr_t);
158272343Sngiestatic void sis_reset		(struct sis_softc *);
159272343Sngiestatic int sis_list_rx_init	(struct sis_softc *);
160272343Sngiestatic int sis_list_tx_init	(struct sis_softc *);
161272343Sngie
162272343Sngiestatic void sis_dma_map_desc_ptr	(void *, bus_dma_segment_t *, int, int);
163272343Sngiestatic void sis_dma_map_desc_next	(void *, bus_dma_segment_t *, int, int);
164272343Sngiestatic void sis_dma_map_ring		(void *, bus_dma_segment_t *, int, int);
165272343Sngie#ifdef SIS_USEIOSPACE
166272343Sngie#define SIS_RES			SYS_RES_IOPORT
167272343Sngie#define SIS_RID			SIS_PCI_LOIO
168272343Sngie#else
169272343Sngie#define SIS_RES			SYS_RES_MEMORY
170272343Sngie#define SIS_RID			SIS_PCI_LOMEM
171272343Sngie#endif
172272343Sngie
173272343Sngiestatic device_method_t sis_methods[] = {
174272343Sngie	/* Device interface */
175272343Sngie	DEVMETHOD(device_probe,		sis_probe),
176272343Sngie	DEVMETHOD(device_attach,	sis_attach),
177272343Sngie	DEVMETHOD(device_detach,	sis_detach),
178272343Sngie	DEVMETHOD(device_shutdown,	sis_shutdown),
179272343Sngie
180272343Sngie	/* bus interface */
181272343Sngie	DEVMETHOD(bus_print_child,	bus_generic_print_child),
182272343Sngie	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
183272343Sngie
184272343Sngie	/* MII interface */
185272343Sngie	DEVMETHOD(miibus_readreg,	sis_miibus_readreg),
186272343Sngie	DEVMETHOD(miibus_writereg,	sis_miibus_writereg),
187272343Sngie	DEVMETHOD(miibus_statchg,	sis_miibus_statchg),
188272343Sngie
189272343Sngie	{ 0, 0 }
190272343Sngie};
191272343Sngie
192272343Sngiestatic driver_t sis_driver = {
193272343Sngie	"sis",
194272343Sngie	sis_methods,
195272343Sngie	sizeof(struct sis_softc)
196272343Sngie};
197272343Sngie
198272343Sngiestatic devclass_t sis_devclass;
199272343Sngie
200272343SngieDRIVER_MODULE(if_sis, pci, sis_driver, sis_devclass, 0, 0);
201272343SngieDRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);
202272343Sngie
203272343Sngie#define SIS_SETBIT(sc, reg, x)				\
204272343Sngie	CSR_WRITE_4(sc, reg,				\
205272343Sngie		CSR_READ_4(sc, reg) | (x))
206272343Sngie
207272343Sngie#define SIS_CLRBIT(sc, reg, x)				\
208272343Sngie	CSR_WRITE_4(sc, reg,				\
209272343Sngie		CSR_READ_4(sc, reg) & ~(x))
210272343Sngie
211272343Sngie#define SIO_SET(x)					\
212272343Sngie	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
213272343Sngie
214272343Sngie#define SIO_CLR(x)					\
215272343Sngie	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
216272343Sngie
217272343Sngiestatic void
218272343Sngiesis_dma_map_desc_next(arg, segs, nseg, error)
219272343Sngie	void *arg;
220272343Sngie	bus_dma_segment_t *segs;
221272343Sngie	int nseg, error;
222272343Sngie{
223272343Sngie	struct sis_desc	*r;
224272343Sngie
225272343Sngie	r = arg;
226272343Sngie	r->sis_next = segs->ds_addr;
227272343Sngie
228272343Sngie	return;
229272343Sngie}
230272343Sngie
231272343Sngiestatic void
232272343Sngiesis_dma_map_desc_ptr(arg, segs, nseg, error)
233272343Sngie	void *arg;
234272343Sngie	bus_dma_segment_t *segs;
235272343Sngie	int nseg, error;
236272343Sngie{
237272343Sngie	struct sis_desc	*r;
238272343Sngie
239272343Sngie	r = arg;
240272343Sngie	r->sis_ptr = segs->ds_addr;
241272343Sngie
242272343Sngie	return;
243272343Sngie}
244272343Sngie
245272343Sngiestatic void
246272343Sngiesis_dma_map_ring(arg, segs, nseg, error)
247272343Sngie	void *arg;
248272343Sngie	bus_dma_segment_t *segs;
249272343Sngie	int nseg, error;
250272343Sngie{
251272343Sngie	u_int32_t *p;
252272343Sngie
253272343Sngie	p = arg;
254272343Sngie	*p = segs->ds_addr;
255272343Sngie
256272343Sngie	return;
257272343Sngie}
258272343Sngie
259272343Sngie/*
260272343Sngie * Routine to reverse the bits in a word. Stolen almost
261272343Sngie * verbatim from /usr/games/fortune.
262272343Sngie */
263272343Sngiestatic u_int16_t sis_reverse(n)
264272343Sngie	u_int16_t		n;
265272343Sngie{
266272343Sngie	n = ((n >>  1) & 0x5555) | ((n <<  1) & 0xaaaa);
267272343Sngie	n = ((n >>  2) & 0x3333) | ((n <<  2) & 0xcccc);
268272343Sngie	n = ((n >>  4) & 0x0f0f) | ((n <<  4) & 0xf0f0);
269272343Sngie	n = ((n >>  8) & 0x00ff) | ((n <<  8) & 0xff00);
270272343Sngie
271272343Sngie	return(n);
272272343Sngie}
273272343Sngie
274272343Sngiestatic void sis_delay(sc)
275272343Sngie	struct sis_softc	*sc;
276272343Sngie{
277272343Sngie	int			idx;
278272343Sngie
279272343Sngie	for (idx = (300 / 33) + 1; idx > 0; idx--)
280272343Sngie		CSR_READ_4(sc, SIS_CSR);
281272343Sngie
282272343Sngie	return;
283272343Sngie}
284272343Sngie
285272343Sngiestatic void sis_eeprom_idle(sc)
286272343Sngie	struct sis_softc	*sc;
287272343Sngie{
288272343Sngie	register int		i;
289272343Sngie
290272343Sngie	SIO_SET(SIS_EECTL_CSEL);
291272343Sngie	sis_delay(sc);
292272343Sngie	SIO_SET(SIS_EECTL_CLK);
293272343Sngie	sis_delay(sc);
294272343Sngie
295272343Sngie	for (i = 0; i < 25; i++) {
296272343Sngie		SIO_CLR(SIS_EECTL_CLK);
297272343Sngie		sis_delay(sc);
298272343Sngie		SIO_SET(SIS_EECTL_CLK);
299272343Sngie		sis_delay(sc);
300272343Sngie	}
301272343Sngie
302272343Sngie	SIO_CLR(SIS_EECTL_CLK);
303272343Sngie	sis_delay(sc);
304272343Sngie	SIO_CLR(SIS_EECTL_CSEL);
305272343Sngie	sis_delay(sc);
306272343Sngie	CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
307272343Sngie
308272343Sngie	return;
309272343Sngie}
310272343Sngie
311272343Sngie/*
312272343Sngie * Send a read command and address to the EEPROM, check for ACK.
313272343Sngie */
314272343Sngiestatic void sis_eeprom_putbyte(sc, addr)
315272343Sngie	struct sis_softc	*sc;
316272343Sngie	int			addr;
317272343Sngie{
318272343Sngie	register int		d, i;
319272343Sngie
320272343Sngie	d = addr | SIS_EECMD_READ;
321272343Sngie
322272343Sngie	/*
323272343Sngie	 * Feed in each bit and stobe the clock.
324272343Sngie	 */
325272343Sngie	for (i = 0x400; i; i >>= 1) {
326272343Sngie		if (d & i) {
327272343Sngie			SIO_SET(SIS_EECTL_DIN);
328272343Sngie		} else {
329272343Sngie			SIO_CLR(SIS_EECTL_DIN);
330272343Sngie		}
331272343Sngie		sis_delay(sc);
332272343Sngie		SIO_SET(SIS_EECTL_CLK);
333272343Sngie		sis_delay(sc);
334272343Sngie		SIO_CLR(SIS_EECTL_CLK);
335272343Sngie		sis_delay(sc);
336272343Sngie	}
337272343Sngie
338272343Sngie	return;
339272343Sngie}
340272343Sngie
341272343Sngie/*
342272343Sngie * Read a word of data stored in the EEPROM at address 'addr.'
343272343Sngie */
344272343Sngiestatic void sis_eeprom_getword(sc, addr, dest)
345272343Sngie	struct sis_softc	*sc;
346272343Sngie	int			addr;
347272343Sngie	u_int16_t		*dest;
348272343Sngie{
349272343Sngie	register int		i;
350272343Sngie	u_int16_t		word = 0;
351272343Sngie
352272343Sngie	/* Force EEPROM to idle state. */
353272343Sngie	sis_eeprom_idle(sc);
354272343Sngie
355272343Sngie	/* Enter EEPROM access mode. */
356272343Sngie	sis_delay(sc);
357272343Sngie	SIO_CLR(SIS_EECTL_CLK);
358272343Sngie	sis_delay(sc);
359272343Sngie	SIO_SET(SIS_EECTL_CSEL);
360	sis_delay(sc);
361
362	/*
363	 * Send address of word we want to read.
364	 */
365	sis_eeprom_putbyte(sc, addr);
366
367	/*
368	 * Start reading bits from EEPROM.
369	 */
370	for (i = 0x8000; i; i >>= 1) {
371		SIO_SET(SIS_EECTL_CLK);
372		sis_delay(sc);
373		if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
374			word |= i;
375		sis_delay(sc);
376		SIO_CLR(SIS_EECTL_CLK);
377		sis_delay(sc);
378	}
379
380	/* Turn off EEPROM access mode. */
381	sis_eeprom_idle(sc);
382
383	*dest = word;
384
385	return;
386}
387
388/*
389 * Read a sequence of words from the EEPROM.
390 */
391static void sis_read_eeprom(sc, dest, off, cnt, swap)
392	struct sis_softc	*sc;
393	caddr_t			dest;
394	int			off;
395	int			cnt;
396	int			swap;
397{
398	int			i;
399	u_int16_t		word = 0, *ptr;
400
401	for (i = 0; i < cnt; i++) {
402		sis_eeprom_getword(sc, off + i, &word);
403		ptr = (u_int16_t *)(dest + (i * 2));
404		if (swap)
405			*ptr = ntohs(word);
406		else
407			*ptr = word;
408	}
409
410	return;
411}
412
413#ifdef __i386__
414static device_t sis_find_bridge(dev)
415	device_t		dev;
416{
417	devclass_t		pci_devclass;
418	device_t		*pci_devices;
419	int			pci_count = 0;
420	device_t		*pci_children;
421	int			pci_childcount = 0;
422	device_t		*busp, *childp;
423	device_t		child = NULL;
424	int			i, j;
425
426	if ((pci_devclass = devclass_find("pci")) == NULL)
427		return(NULL);
428
429	devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
430
431	for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
432		pci_childcount = 0;
433		device_get_children(*busp, &pci_children, &pci_childcount);
434		for (j = 0, childp = pci_children;
435		    j < pci_childcount; j++, childp++) {
436			if (pci_get_vendor(*childp) == SIS_VENDORID &&
437			    pci_get_device(*childp) == 0x0008) {
438				child = *childp;
439				goto done;
440			}
441		}
442	}
443
444done:
445	free(pci_devices, M_TEMP);
446	free(pci_children, M_TEMP);
447	return(child);
448}
449
450static void sis_read_cmos(sc, dev, dest, off, cnt)
451	struct sis_softc	*sc;
452	device_t		dev;
453	caddr_t			dest;
454	int			off;
455	int			cnt;
456{
457	device_t		bridge;
458	u_int8_t		reg;
459	int			i;
460	bus_space_tag_t		btag;
461
462	bridge = sis_find_bridge(dev);
463	if (bridge == NULL)
464		return;
465	reg = pci_read_config(bridge, 0x48, 1);
466	pci_write_config(bridge, 0x48, reg|0x40, 1);
467
468	/* XXX */
469	btag = I386_BUS_SPACE_IO;
470
471	for (i = 0; i < cnt; i++) {
472		bus_space_write_1(btag, 0x0, 0x70, i + off);
473		*(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
474	}
475
476	pci_write_config(bridge, 0x48, reg & ~0x40, 1);
477	return;
478}
479
480static void sis_read_mac(sc, dev, dest)
481	struct sis_softc	*sc;
482	device_t		dev;
483	caddr_t			dest;
484{
485	u_int32_t		filtsave, csrsave;
486
487	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
488	csrsave = CSR_READ_4(sc, SIS_CSR);
489
490	CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
491	CSR_WRITE_4(sc, SIS_CSR, 0);
492
493	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
494
495	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
496	((u_int16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
497	CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
498	((u_int16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
499	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
500	((u_int16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
501
502	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
503	CSR_WRITE_4(sc, SIS_CSR, csrsave);
504	return;
505}
506#endif
507
508static int sis_miibus_readreg(dev, phy, reg)
509	device_t		dev;
510	int			phy, reg;
511{
512	struct sis_softc	*sc;
513	int			i, val = 0;
514
515	sc = device_get_softc(dev);
516
517	if (sc->sis_type == SIS_TYPE_83815) {
518		if (phy != 0)
519			return(0);
520		/*
521		 * The NatSemi chip can take a while after
522		 * a reset to come ready, during which the BMSR
523		 * returns a value of 0. This is *never* supposed
524		 * to happen: some of the BMSR bits are meant to
525		 * be hardwired in the on position, and this can
526		 * confuse the miibus code a bit during the probe
527		 * and attach phase. So we make an effort to check
528		 * for this condition and wait for it to clear.
529		 */
530		if (!CSR_READ_4(sc, NS_BMSR))
531			DELAY(1000);
532		val = CSR_READ_4(sc, NS_BMCR + (reg * 4));
533		return(val);
534	}
535
536	if (sc->sis_type == SIS_TYPE_900 &&
537	    sc->sis_rev < SIS_REV_635 && phy != 0)
538		return(0);
539
540	CSR_WRITE_4(sc, SIS_PHYCTL, (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
541	SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
542
543	for (i = 0; i < SIS_TIMEOUT; i++) {
544		if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
545			break;
546	}
547
548	if (i == SIS_TIMEOUT) {
549		printf("sis%d: PHY failed to come ready\n", sc->sis_unit);
550		return(0);
551	}
552
553	val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
554
555	if (val == 0xFFFF)
556		return(0);
557
558	return(val);
559}
560
561static int sis_miibus_writereg(dev, phy, reg, data)
562	device_t		dev;
563	int			phy, reg, data;
564{
565	struct sis_softc	*sc;
566	int			i;
567
568	sc = device_get_softc(dev);
569
570	if (sc->sis_type == SIS_TYPE_83815) {
571		if (phy != 0)
572			return(0);
573		CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
574		return(0);
575	}
576
577	if (sc->sis_type == SIS_TYPE_900 && phy != 0)
578		return(0);
579
580	CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
581	    (reg << 6) | SIS_PHYOP_WRITE);
582	SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
583
584	for (i = 0; i < SIS_TIMEOUT; i++) {
585		if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
586			break;
587	}
588
589	if (i == SIS_TIMEOUT)
590		printf("sis%d: PHY failed to come ready\n", sc->sis_unit);
591
592	return(0);
593}
594
595static void sis_miibus_statchg(dev)
596	device_t		dev;
597{
598	struct sis_softc	*sc;
599
600	sc = device_get_softc(dev);
601	sis_init(sc);
602
603	return;
604}
605
606static u_int32_t sis_crc(sc, addr)
607	struct sis_softc	*sc;
608	caddr_t			addr;
609{
610	u_int32_t		crc, carry;
611	int			i, j;
612	u_int8_t		c;
613
614	/* Compute CRC for the address value. */
615	crc = 0xFFFFFFFF; /* initial value */
616
617	for (i = 0; i < 6; i++) {
618		c = *(addr + i);
619		for (j = 0; j < 8; j++) {
620			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
621			crc <<= 1;
622			c >>= 1;
623			if (carry)
624				crc = (crc ^ 0x04c11db6) | carry;
625		}
626	}
627
628	/*
629	 * return the filter bit position
630	 *
631	 * The NatSemi chip has a 512-bit filter, which is
632	 * different than the SiS, so we special-case it.
633	 */
634	if (sc->sis_type == SIS_TYPE_83815)
635		return((crc >> 23) & 0x1FF);
636
637	return((crc >> 25) & 0x0000007F);
638}
639
640static void sis_setmulti_ns(sc)
641	struct sis_softc	*sc;
642{
643	struct ifnet		*ifp;
644	struct ifmultiaddr	*ifma;
645	u_int32_t		h = 0, i, filtsave;
646	int			bit, index;
647
648	ifp = &sc->arpcom.ac_if;
649
650	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
651		SIS_CLRBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
652		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
653		return;
654	}
655
656	/*
657	 * We have to explicitly enable the multicast hash table
658	 * on the NatSemi chip if we want to use it, which we do.
659	 */
660	SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
661	SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
662
663	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
664
665	/* first, zot all the existing hash bits */
666	for (i = 0; i < 32; i++) {
667		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + (i*2));
668		CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
669	}
670
671	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
672		if (ifma->ifma_addr->sa_family != AF_LINK)
673			continue;
674		h = sis_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
675		index = h >> 3;
676		bit = h & 0x1F;
677		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
678		if (bit > 0xF)
679			bit -= 0x10;
680		SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
681	}
682
683	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
684
685	return;
686}
687
688static void sis_setmulti_sis(sc)
689	struct sis_softc	*sc;
690{
691	struct ifnet		*ifp;
692	struct ifmultiaddr	*ifma;
693	u_int32_t		h = 0, i, filtsave;
694
695	ifp = &sc->arpcom.ac_if;
696
697	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
698		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
699		return;
700	}
701
702	SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
703
704	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
705
706	/* first, zot all the existing hash bits */
707	for (i = 0; i < 8; i++) {
708		CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + ((i * 16) >> 4)) << 16);
709		CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
710	}
711
712	/* now program new ones */
713	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
714		if (ifma->ifma_addr->sa_family != AF_LINK)
715			continue;
716		h = sis_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
717		CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + (h >> 4)) << 16);
718		SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << (h & 0xF)));
719	}
720
721	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
722
723	return;
724}
725
726static void sis_reset(sc)
727	struct sis_softc	*sc;
728{
729	register int		i;
730
731	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
732
733	for (i = 0; i < SIS_TIMEOUT; i++) {
734		if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
735			break;
736	}
737
738	if (i == SIS_TIMEOUT)
739		printf("sis%d: reset never completed\n", sc->sis_unit);
740
741	/* Wait a little while for the chip to get its brains in order. */
742	DELAY(1000);
743
744	/*
745	 * If this is a NetSemi chip, make sure to clear
746	 * PME mode.
747	 */
748	if (sc->sis_type == SIS_TYPE_83815) {
749		CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
750		CSR_WRITE_4(sc, NS_CLKRUN, 0);
751	}
752
753        return;
754}
755
756/*
757 * Probe for an SiS chip. Check the PCI vendor and device
758 * IDs against our list and return a device name if we find a match.
759 */
760static int sis_probe(dev)
761	device_t		dev;
762{
763	struct sis_type		*t;
764
765	t = sis_devs;
766
767	while(t->sis_name != NULL) {
768		if ((pci_get_vendor(dev) == t->sis_vid) &&
769		    (pci_get_device(dev) == t->sis_did)) {
770			device_set_desc(dev, t->sis_name);
771			return(0);
772		}
773		t++;
774	}
775
776	return(ENXIO);
777}
778
779/*
780 * Attach the interface. Allocate softc structures, do ifmedia
781 * setup and ethernet/BPF attach.
782 */
783static int sis_attach(dev)
784	device_t		dev;
785{
786	u_char			eaddr[ETHER_ADDR_LEN];
787	u_int32_t		command;
788	struct sis_softc	*sc;
789	struct ifnet		*ifp;
790	int			unit, error = 0, rid;
791
792	sc = device_get_softc(dev);
793	unit = device_get_unit(dev);
794	bzero(sc, sizeof(struct sis_softc));
795
796	mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
797	    MTX_DEF | MTX_RECURSE);
798	SIS_LOCK(sc);
799
800	if (pci_get_device(dev) == SIS_DEVICEID_900)
801		sc->sis_type = SIS_TYPE_900;
802	if (pci_get_device(dev) == SIS_DEVICEID_7016)
803		sc->sis_type = SIS_TYPE_7016;
804	if (pci_get_vendor(dev) == NS_VENDORID)
805		sc->sis_type = SIS_TYPE_83815;
806
807	sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
808
809	/*
810	 * Handle power management nonsense.
811	 */
812	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
813		u_int32_t		iobase, membase, irq;
814
815		/* Save important PCI config data. */
816		iobase = pci_read_config(dev, SIS_PCI_LOIO, 4);
817		membase = pci_read_config(dev, SIS_PCI_LOMEM, 4);
818		irq = pci_read_config(dev, SIS_PCI_INTLINE, 4);
819
820		/* Reset the power state. */
821		printf("sis%d: chip is in D%d power mode "
822		    "-- setting to D0\n", unit,
823		    pci_get_powerstate(dev));
824		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
825
826		/* Restore PCI config data. */
827		pci_write_config(dev, SIS_PCI_LOIO, iobase, 4);
828		pci_write_config(dev, SIS_PCI_LOMEM, membase, 4);
829		pci_write_config(dev, SIS_PCI_INTLINE, irq, 4);
830	}
831
832	/*
833	 * Map control/status registers.
834	 */
835	pci_enable_busmaster(dev);
836	pci_enable_io(dev, SYS_RES_IOPORT);
837	pci_enable_io(dev, SYS_RES_MEMORY);
838	command = pci_read_config(dev, PCIR_COMMAND, 4);
839
840#ifdef SIS_USEIOSPACE
841	if (!(command & PCIM_CMD_PORTEN)) {
842		printf("sis%d: failed to enable I/O ports!\n", unit);
843		error = ENXIO;;
844		goto fail;
845	}
846#else
847	if (!(command & PCIM_CMD_MEMEN)) {
848		printf("sis%d: failed to enable memory mapping!\n", unit);
849		error = ENXIO;;
850		goto fail;
851	}
852#endif
853
854	rid = SIS_RID;
855	sc->sis_res = bus_alloc_resource(dev, SIS_RES, &rid,
856	    0, ~0, 1, RF_ACTIVE);
857
858	if (sc->sis_res == NULL) {
859		printf("sis%d: couldn't map ports/memory\n", unit);
860		error = ENXIO;
861		goto fail;
862	}
863
864	sc->sis_btag = rman_get_bustag(sc->sis_res);
865	sc->sis_bhandle = rman_get_bushandle(sc->sis_res);
866
867	/* Allocate interrupt */
868	rid = 0;
869	sc->sis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
870	    RF_SHAREABLE | RF_ACTIVE);
871
872	if (sc->sis_irq == NULL) {
873		printf("sis%d: couldn't map interrupt\n", unit);
874		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
875		error = ENXIO;
876		goto fail;
877	}
878
879	error = bus_setup_intr(dev, sc->sis_irq, INTR_TYPE_NET,
880	    sis_intr, sc, &sc->sis_intrhand);
881
882	if (error) {
883		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
884		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
885		printf("sis%d: couldn't set up irq\n", unit);
886		goto fail;
887	}
888
889	/* Reset the adapter. */
890	sis_reset(sc);
891
892	/*
893	 * Get station address from the EEPROM.
894	 */
895	switch (pci_get_vendor(dev)) {
896	case NS_VENDORID:
897		/*
898		 * Reading the MAC address out of the EEPROM on
899		 * the NatSemi chip takes a bit more work than
900		 * you'd expect. The address spans 4 16-bit words,
901		 * with the first word containing only a single bit.
902		 * You have to shift everything over one bit to
903		 * get it aligned properly. Also, the bits are
904		 * stored backwards (the LSB is really the MSB,
905		 * and so on) so you have to reverse them in order
906		 * to get the MAC address into the form we want.
907		 * Why? Who the hell knows.
908		 */
909		{
910			u_int16_t		tmp[4];
911
912			sis_read_eeprom(sc, (caddr_t)&tmp,
913			    NS_EE_NODEADDR, 4, 0);
914
915			/* Shift everything over one bit. */
916			tmp[3] = tmp[3] >> 1;
917			tmp[3] |= tmp[2] << 15;
918			tmp[2] = tmp[2] >> 1;
919			tmp[2] |= tmp[1] << 15;
920			tmp[1] = tmp[1] >> 1;
921			tmp[1] |= tmp[0] << 15;
922
923			/* Now reverse all the bits. */
924			tmp[3] = sis_reverse(tmp[3]);
925			tmp[2] = sis_reverse(tmp[2]);
926			tmp[1] = sis_reverse(tmp[1]);
927
928			bcopy((char *)&tmp[1], eaddr, ETHER_ADDR_LEN);
929		}
930		break;
931	case SIS_VENDORID:
932	default:
933#ifdef __i386__
934		/*
935		 * If this is a SiS 630E chipset with an embedded
936		 * SiS 900 controller, we have to read the MAC address
937		 * from the APC CMOS RAM. Our method for doing this
938		 * is very ugly since we have to reach out and grab
939		 * ahold of hardware for which we cannot properly
940		 * allocate resources. This code is only compiled on
941		 * the i386 architecture since the SiS 630E chipset
942		 * is for x86 motherboards only. Note that there are
943		 * a lot of magic numbers in this hack. These are
944		 * taken from SiS's Linux driver. I'd like to replace
945		 * them with proper symbolic definitions, but that
946		 * requires some datasheets that I don't have access
947		 * to at the moment.
948		 */
949		if (sc->sis_rev == SIS_REV_630S ||
950		    sc->sis_rev == SIS_REV_630E ||
951		    sc->sis_rev == SIS_REV_630EA1)
952			sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
953
954		else if (sc->sis_rev == SIS_REV_635 ||
955			 sc->sis_rev == SIS_REV_630ET)
956			sis_read_mac(sc, dev, (caddr_t)&eaddr);
957		else
958#endif
959			sis_read_eeprom(sc, (caddr_t)&eaddr,
960			    SIS_EE_NODEADDR, 3, 0);
961		break;
962	}
963
964	/*
965	 * A SiS chip was detected. Inform the world.
966	 */
967	printf("sis%d: Ethernet address: %6D\n", unit, eaddr, ":");
968
969	sc->sis_unit = unit;
970	callout_handle_init(&sc->sis_stat_ch);
971	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
972
973	/*
974	 * Allocate the parent bus DMA tag appropriate for PCI.
975	 */
976#define SIS_NSEG_NEW 32
977	 error = bus_dma_tag_create(NULL,	/* parent */
978			1, 0,			/* alignment, boundary */
979			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
980			BUS_SPACE_MAXADDR,	/* highaddr */
981			NULL, NULL,		/* filter, filterarg */
982			MAXBSIZE, SIS_NSEG_NEW,	/* maxsize, nsegments */
983			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
984			BUS_DMA_ALLOCNOW,	/* flags */
985			&sc->sis_parent_tag);
986
987	/*
988	 * Now allocate a tag for the DMA descriptor lists.
989	 * All of our lists are allocated as a contiguous block
990	 * of memory.
991	 */
992	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
993			1, 0,			/* alignment, boundary */
994			BUS_SPACE_MAXADDR,	/* lowaddr */
995			BUS_SPACE_MAXADDR,	/* highaddr */
996			NULL, NULL,		/* filter, filterarg */
997			SIS_RX_LIST_SZ, 1,	/* maxsize,nsegments */
998			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
999			0,			/* flags */
1000			&sc->sis_ldata.sis_rx_tag);
1001
1002	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
1003			1, 0,			/* alignment, boundary */
1004			BUS_SPACE_MAXADDR,	/* lowaddr */
1005			BUS_SPACE_MAXADDR,	/* highaddr */
1006			NULL, NULL,		/* filter, filterarg */
1007			SIS_TX_LIST_SZ, 1,	/* maxsize,nsegments */
1008			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1009			0,			/* flags */
1010			&sc->sis_ldata.sis_tx_tag);
1011
1012	error = bus_dma_tag_create(sc->sis_parent_tag,	/* parent */
1013			1, 0,			/* alignment, boundary */
1014			BUS_SPACE_MAXADDR,	/* lowaddr */
1015			BUS_SPACE_MAXADDR,	/* highaddr */
1016			NULL, NULL,		/* filter, filterarg */
1017			SIS_TX_LIST_SZ, 1,	/* maxsize,nsegments */
1018			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1019			0,			/* flags */
1020			&sc->sis_tag);
1021
1022	/*
1023	 * Now allocate a chunk of DMA-able memory based on the
1024	 * tag we just created.
1025	 */
1026	error = bus_dmamem_alloc(sc->sis_ldata.sis_tx_tag,
1027	    (void **)&sc->sis_ldata.sis_tx_list, BUS_DMA_NOWAIT,
1028	    &sc->sis_ldata.sis_tx_dmamap);
1029
1030	if (error) {
1031		printf("sis%d: no memory for list buffers!\n", unit);
1032		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1033		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1034		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1035		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1036		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1037		error = ENXIO;
1038		goto fail;
1039	}
1040
1041	error = bus_dmamem_alloc(sc->sis_ldata.sis_rx_tag,
1042	    (void **)&sc->sis_ldata.sis_rx_list, BUS_DMA_NOWAIT,
1043	    &sc->sis_ldata.sis_rx_dmamap);
1044
1045	if (error) {
1046		printf("sis%d: no memory for list buffers!\n", unit);
1047		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1048		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1049		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1050		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1051		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
1052		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1053		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1054		error = ENXIO;
1055		goto fail;
1056	}
1057
1058
1059	bzero(sc->sis_ldata.sis_tx_list, SIS_TX_LIST_SZ);
1060	bzero(sc->sis_ldata.sis_rx_list, SIS_RX_LIST_SZ);
1061
1062	/*
1063	 * Obtain the physical addresses of the RX and TX
1064	 * rings which we'll need later in the init routine.
1065	 */
1066	bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
1067	    sc->sis_ldata.sis_tx_dmamap, &(sc->sis_ldata.sis_tx_list[0]),
1068	    sizeof(struct sis_desc), sis_dma_map_ring,
1069	    &sc->sis_cdata.sis_tx_paddr, 0);
1070	bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
1071	    sc->sis_ldata.sis_rx_dmamap, &(sc->sis_ldata.sis_rx_list[0]),
1072	    sizeof(struct sis_desc), sis_dma_map_ring,
1073	    &sc->sis_cdata.sis_rx_paddr, 0);
1074
1075	ifp = &sc->arpcom.ac_if;
1076	ifp->if_softc = sc;
1077	ifp->if_unit = unit;
1078	ifp->if_name = "sis";
1079	ifp->if_mtu = ETHERMTU;
1080	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1081	ifp->if_ioctl = sis_ioctl;
1082	ifp->if_output = ether_output;
1083	ifp->if_start = sis_start;
1084	ifp->if_watchdog = sis_watchdog;
1085	ifp->if_init = sis_init;
1086	ifp->if_baudrate = 10000000;
1087	ifp->if_snd.ifq_maxlen = SIS_TX_LIST_CNT - 1;
1088
1089	/*
1090	 * Do MII setup.
1091	 */
1092	if (mii_phy_probe(dev, &sc->sis_miibus,
1093	    sis_ifmedia_upd, sis_ifmedia_sts)) {
1094		printf("sis%d: MII without any PHY!\n", sc->sis_unit);
1095		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1096		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1097		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1098		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1099		    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1100		bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1101		    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
1102		bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1103		bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1104		error = ENXIO;
1105		goto fail;
1106	}
1107
1108	/*
1109	 * Call MI attach routine.
1110	 */
1111	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1112
1113	/*
1114	 * Tell the upper layer(s) we support long frames.
1115	 */
1116	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1117
1118	callout_handle_init(&sc->sis_stat_ch);
1119	SIS_UNLOCK(sc);
1120	return(0);
1121
1122fail:
1123	SIS_UNLOCK(sc);
1124	mtx_destroy(&sc->sis_mtx);
1125	return(error);
1126}
1127
1128static int sis_detach(dev)
1129	device_t		dev;
1130{
1131	struct sis_softc	*sc;
1132	struct ifnet		*ifp;
1133
1134
1135	sc = device_get_softc(dev);
1136	SIS_LOCK(sc);
1137	ifp = &sc->arpcom.ac_if;
1138
1139	sis_reset(sc);
1140	sis_stop(sc);
1141	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1142
1143	bus_generic_detach(dev);
1144	device_delete_child(dev, sc->sis_miibus);
1145
1146	bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1147	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1148	bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1149
1150	bus_dmamap_unload(sc->sis_ldata.sis_rx_tag,
1151	    sc->sis_ldata.sis_rx_dmamap);
1152	bus_dmamap_unload(sc->sis_ldata.sis_tx_tag,
1153	    sc->sis_ldata.sis_tx_dmamap);
1154	bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1155	    sc->sis_ldata.sis_rx_list, sc->sis_ldata.sis_rx_dmamap);
1156	bus_dmamem_free(sc->sis_ldata.sis_rx_tag,
1157	    sc->sis_ldata.sis_tx_list, sc->sis_ldata.sis_tx_dmamap);
1158	bus_dma_tag_destroy(sc->sis_ldata.sis_rx_tag);
1159	bus_dma_tag_destroy(sc->sis_ldata.sis_tx_tag);
1160	bus_dma_tag_destroy(sc->sis_parent_tag);
1161
1162	SIS_UNLOCK(sc);
1163	mtx_destroy(&sc->sis_mtx);
1164
1165	return(0);
1166}
1167
1168/*
1169 * Initialize the transmit descriptors.
1170 */
1171static int sis_list_tx_init(sc)
1172	struct sis_softc	*sc;
1173{
1174	struct sis_list_data	*ld;
1175	struct sis_ring_data	*cd;
1176	int			i, nexti;
1177
1178	cd = &sc->sis_cdata;
1179	ld = &sc->sis_ldata;
1180
1181	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1182		nexti = (i == (SIS_TX_LIST_CNT - 1)) ? 0 : i+1;
1183			ld->sis_tx_list[i].sis_nextdesc =
1184			    &ld->sis_tx_list[nexti];
1185			bus_dmamap_load(sc->sis_ldata.sis_tx_tag,
1186			    sc->sis_ldata.sis_tx_dmamap,
1187			    &ld->sis_tx_list[nexti], sizeof(struct sis_desc),
1188			    sis_dma_map_desc_next, &ld->sis_tx_list[i], 0);
1189		ld->sis_tx_list[i].sis_mbuf = NULL;
1190		ld->sis_tx_list[i].sis_ptr = 0;
1191		ld->sis_tx_list[i].sis_ctl = 0;
1192	}
1193
1194	cd->sis_tx_prod = cd->sis_tx_cons = cd->sis_tx_cnt = 0;
1195
1196	bus_dmamap_sync(sc->sis_ldata.sis_tx_tag,
1197	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
1198
1199	return(0);
1200}
1201
1202/*
1203 * Initialize the RX descriptors and allocate mbufs for them. Note that
1204 * we arrange the descriptors in a closed ring, so that the last descriptor
1205 * points back to the first.
1206 */
1207static int sis_list_rx_init(sc)
1208	struct sis_softc	*sc;
1209{
1210	struct sis_list_data	*ld;
1211	struct sis_ring_data	*cd;
1212	int			i,nexti;
1213
1214	ld = &sc->sis_ldata;
1215	cd = &sc->sis_cdata;
1216
1217	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1218		if (sis_newbuf(sc, &ld->sis_rx_list[i], NULL) == ENOBUFS)
1219			return(ENOBUFS);
1220		nexti = (i == (SIS_RX_LIST_CNT - 1)) ? 0 : i+1;
1221			ld->sis_rx_list[i].sis_nextdesc =
1222			    &ld->sis_rx_list[nexti];
1223			bus_dmamap_load(sc->sis_ldata.sis_rx_tag,
1224			    sc->sis_ldata.sis_rx_dmamap,
1225			    &ld->sis_rx_list[nexti],
1226			    sizeof(struct sis_desc), sis_dma_map_desc_next,
1227			    &ld->sis_rx_list[i], 0);
1228		}
1229
1230	bus_dmamap_sync(sc->sis_ldata.sis_rx_tag,
1231	    sc->sis_ldata.sis_rx_dmamap, BUS_DMASYNC_PREWRITE);
1232
1233	cd->sis_rx_prod = 0;
1234
1235	return(0);
1236}
1237
1238/*
1239 * Initialize an RX descriptor and attach an MBUF cluster.
1240 */
1241static int sis_newbuf(sc, c, m)
1242	struct sis_softc	*sc;
1243	struct sis_desc		*c;
1244	struct mbuf		*m;
1245{
1246
1247	if (c == NULL)
1248		return(EINVAL);
1249
1250	if (m == NULL) {
1251		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1252		if (m == NULL)
1253			return(ENOBUFS);
1254	} else
1255		m->m_data = m->m_ext.ext_buf;
1256
1257	c->sis_mbuf = m;
1258	c->sis_ctl = SIS_RXLEN;
1259
1260	bus_dmamap_create(sc->sis_tag, 0, &c->sis_map);
1261	bus_dmamap_load(sc->sis_tag, c->sis_map,
1262	    mtod(m, void *), MCLBYTES,
1263	    sis_dma_map_desc_ptr, c, 0);
1264	bus_dmamap_sync(sc->sis_tag, c->sis_map, BUS_DMASYNC_PREWRITE);
1265
1266	return(0);
1267}
1268
1269/*
1270 * A frame has been uploaded: pass the resulting mbuf chain up to
1271 * the higher level protocols.
1272 */
1273static void sis_rxeof(sc)
1274	struct sis_softc	*sc;
1275{
1276        struct mbuf		*m;
1277        struct ifnet		*ifp;
1278	struct sis_desc		*cur_rx;
1279	int			i, total_len = 0;
1280	u_int32_t		rxstat;
1281
1282	ifp = &sc->arpcom.ac_if;
1283	i = sc->sis_cdata.sis_rx_prod;
1284
1285	while(SIS_OWNDESC(&sc->sis_ldata.sis_rx_list[i])) {
1286
1287#ifdef DEVICE_POLLING
1288		if (ifp->if_ipending & IFF_POLLING) {
1289			if (sc->rxcycles <= 0)
1290				break;
1291			sc->rxcycles--;
1292		}
1293#endif /* DEVICE_POLLING */
1294		cur_rx = &sc->sis_ldata.sis_rx_list[i];
1295		rxstat = cur_rx->sis_rxstat;
1296		bus_dmamap_sync(sc->sis_tag,
1297		    cur_rx->sis_map, BUS_DMASYNC_POSTWRITE);
1298		bus_dmamap_unload(sc->sis_tag, cur_rx->sis_map);
1299		bus_dmamap_destroy(sc->sis_tag, cur_rx->sis_map);
1300		m = cur_rx->sis_mbuf;
1301		cur_rx->sis_mbuf = NULL;
1302		total_len = SIS_RXBYTES(cur_rx);
1303		SIS_INC(i, SIS_RX_LIST_CNT);
1304
1305		/*
1306		 * If an error occurs, update stats, clear the
1307		 * status word and leave the mbuf cluster in place:
1308		 * it should simply get re-used next time this descriptor
1309	 	 * comes up in the ring.
1310		 */
1311		if (!(rxstat & SIS_CMDSTS_PKT_OK)) {
1312			ifp->if_ierrors++;
1313			if (rxstat & SIS_RXSTAT_COLL)
1314				ifp->if_collisions++;
1315			sis_newbuf(sc, cur_rx, m);
1316			continue;
1317		}
1318
1319		/* No errors; receive the packet. */
1320#ifdef __i386__
1321		/*
1322		 * On the x86 we do not have alignment problems, so try to
1323		 * allocate a new buffer for the receive ring, and pass up
1324		 * the one where the packet is already, saving the expensive
1325		 * copy done in m_devget().
1326		 * If we are on an architecture with alignment problems, or
1327		 * if the allocation fails, then use m_devget and leave the
1328		 * existing buffer in the receive ring.
1329		 */
1330		if (sis_newbuf(sc, cur_rx, NULL) == 0)
1331			m->m_pkthdr.len = m->m_len = total_len;
1332		else
1333#endif
1334		{
1335			struct mbuf		*m0;
1336			m0 = m_devget(mtod(m, char *), total_len,
1337				ETHER_ALIGN, ifp, NULL);
1338			sis_newbuf(sc, cur_rx, m);
1339			if (m0 == NULL) {
1340				ifp->if_ierrors++;
1341				continue;
1342			}
1343			m = m0;
1344		}
1345
1346		ifp->if_ipackets++;
1347		ether_input(ifp, NULL, m);
1348	}
1349
1350	sc->sis_cdata.sis_rx_prod = i;
1351
1352	return;
1353}
1354
1355void sis_rxeoc(sc)
1356	struct sis_softc	*sc;
1357{
1358	sis_rxeof(sc);
1359	sis_init(sc);
1360	return;
1361}
1362
1363/*
1364 * A frame was downloaded to the chip. It's safe for us to clean up
1365 * the list buffers.
1366 */
1367
1368static void sis_txeof(sc)
1369	struct sis_softc	*sc;
1370{
1371	struct ifnet		*ifp;
1372	u_int32_t		idx;
1373
1374	ifp = &sc->arpcom.ac_if;
1375
1376	/*
1377	 * Go through our tx list and free mbufs for those
1378	 * frames that have been transmitted.
1379	 */
1380	for (idx = sc->sis_cdata.sis_tx_cons; sc->sis_cdata.sis_tx_cnt > 0;
1381	    sc->sis_cdata.sis_tx_cnt--, SIS_INC(idx, SIS_TX_LIST_CNT) ) {
1382		struct sis_desc *cur_tx = &sc->sis_ldata.sis_tx_list[idx];
1383
1384		if (SIS_OWNDESC(cur_tx))
1385			break;
1386
1387		if (cur_tx->sis_ctl & SIS_CMDSTS_MORE)
1388			continue;
1389
1390		if (!(cur_tx->sis_ctl & SIS_CMDSTS_PKT_OK)) {
1391			ifp->if_oerrors++;
1392			if (cur_tx->sis_txstat & SIS_TXSTAT_EXCESSCOLLS)
1393				ifp->if_collisions++;
1394			if (cur_tx->sis_txstat & SIS_TXSTAT_OUTOFWINCOLL)
1395				ifp->if_collisions++;
1396		}
1397
1398		ifp->if_collisions +=
1399		    (cur_tx->sis_txstat & SIS_TXSTAT_COLLCNT) >> 16;
1400
1401		ifp->if_opackets++;
1402		if (cur_tx->sis_mbuf != NULL) {
1403			m_freem(cur_tx->sis_mbuf);
1404			cur_tx->sis_mbuf = NULL;
1405			bus_dmamap_unload(sc->sis_tag, cur_tx->sis_map);
1406			bus_dmamap_destroy(sc->sis_tag, cur_tx->sis_map);
1407		}
1408	}
1409
1410	if (idx != sc->sis_cdata.sis_tx_cons) {
1411		/* we freed up some buffers */
1412		sc->sis_cdata.sis_tx_cons = idx;
1413		ifp->if_flags &= ~IFF_OACTIVE;
1414	}
1415
1416	ifp->if_timer = (sc->sis_cdata.sis_tx_cnt == 0) ? 0 : 5;
1417
1418	return;
1419}
1420
1421static void sis_tick(xsc)
1422	void			*xsc;
1423{
1424	struct sis_softc	*sc;
1425	struct mii_data		*mii;
1426	struct ifnet		*ifp;
1427
1428	sc = xsc;
1429	SIS_LOCK(sc);
1430	ifp = &sc->arpcom.ac_if;
1431
1432	mii = device_get_softc(sc->sis_miibus);
1433	mii_tick(mii);
1434
1435	if (!sc->sis_link && mii->mii_media_status & IFM_ACTIVE &&
1436	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1437		sc->sis_link++;
1438		if (ifp->if_snd.ifq_head != NULL)
1439			sis_start(ifp);
1440	}
1441
1442	sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1443
1444	SIS_UNLOCK(sc);
1445
1446	return;
1447}
1448
1449#ifdef DEVICE_POLLING
1450static poll_handler_t sis_poll;
1451
1452static void
1453sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1454{
1455	struct	sis_softc *sc = ifp->if_softc;
1456
1457	SIS_LOCK(sc);
1458	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1459		CSR_WRITE_4(sc, SIS_IER, 1);
1460		goto done;
1461	}
1462
1463	/*
1464	 * On the sis, reading the status register also clears it.
1465	 * So before returning to intr mode we must make sure that all
1466	 * possible pending sources of interrupts have been served.
1467	 * In practice this means run to completion the *eof routines,
1468	 * and then call the interrupt routine
1469	 */
1470	sc->rxcycles = count;
1471	sis_rxeof(sc);
1472	sis_txeof(sc);
1473	if (ifp->if_snd.ifq_head != NULL)
1474		sis_start(ifp);
1475
1476	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1477		u_int32_t	status;
1478
1479		/* Reading the ISR register clears all interrupts. */
1480		status = CSR_READ_4(sc, SIS_ISR);
1481
1482		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1483			sis_rxeoc(sc);
1484
1485		if (status & (SIS_ISR_RX_IDLE))
1486			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1487
1488		if (status & SIS_ISR_SYSERR) {
1489			sis_reset(sc);
1490			sis_init(sc);
1491		}
1492	}
1493done:
1494	SIS_UNLOCK(sc);
1495	return;
1496}
1497#endif /* DEVICE_POLLING */
1498
1499static void sis_intr(arg)
1500	void			*arg;
1501{
1502	struct sis_softc	*sc;
1503	struct ifnet		*ifp;
1504	u_int32_t		status;
1505
1506	sc = arg;
1507	ifp = &sc->arpcom.ac_if;
1508
1509	SIS_LOCK(sc);
1510#ifdef DEVICE_POLLING
1511	if (ifp->if_ipending & IFF_POLLING)
1512		goto done;
1513	if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */
1514		CSR_WRITE_4(sc, SIS_IER, 0);
1515		goto done;
1516	}
1517#endif /* DEVICE_POLLING */
1518
1519	/* Supress unwanted interrupts */
1520	if (!(ifp->if_flags & IFF_UP)) {
1521		sis_stop(sc);
1522		goto done;
1523	}
1524
1525	/* Disable interrupts. */
1526	CSR_WRITE_4(sc, SIS_IER, 0);
1527
1528	for (;;) {
1529		/* Reading the ISR register clears all interrupts. */
1530		status = CSR_READ_4(sc, SIS_ISR);
1531
1532		if ((status & SIS_INTRS) == 0)
1533			break;
1534
1535		if (status &
1536		    (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
1537		     SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
1538			sis_txeof(sc);
1539
1540		if (status & (SIS_ISR_RX_DESC_OK|SIS_ISR_RX_OK|SIS_ISR_RX_IDLE))
1541			sis_rxeof(sc);
1542
1543		if (status & (SIS_ISR_RX_ERR | SIS_ISR_RX_OFLOW))
1544			sis_rxeoc(sc);
1545
1546		if (status & (SIS_ISR_RX_IDLE))
1547			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1548
1549		if (status & SIS_ISR_SYSERR) {
1550			sis_reset(sc);
1551			sis_init(sc);
1552		}
1553	}
1554
1555	/* Re-enable interrupts. */
1556	CSR_WRITE_4(sc, SIS_IER, 1);
1557
1558	if (ifp->if_snd.ifq_head != NULL)
1559		sis_start(ifp);
1560done:
1561	SIS_UNLOCK(sc);
1562
1563	return;
1564}
1565
1566/*
1567 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1568 * pointers to the fragment pointers.
1569 */
1570static int sis_encap(sc, m_head, txidx)
1571	struct sis_softc	*sc;
1572	struct mbuf		*m_head;
1573	u_int32_t		*txidx;
1574{
1575	struct sis_desc		*f = NULL;
1576	struct mbuf		*m;
1577	int			frag, cur, cnt = 0;
1578
1579	/*
1580 	 * Start packing the mbufs in this chain into
1581	 * the fragment pointers. Stop when we run out
1582 	 * of fragments or hit the end of the mbuf chain.
1583	 */
1584	m = m_head;
1585	cur = frag = *txidx;
1586
1587	for (m = m_head; m != NULL; m = m->m_next) {
1588		if (m->m_len != 0) {
1589			if ((SIS_TX_LIST_CNT -
1590			    (sc->sis_cdata.sis_tx_cnt + cnt)) < 2)
1591				return(ENOBUFS);
1592			f = &sc->sis_ldata.sis_tx_list[frag];
1593			f->sis_ctl = SIS_CMDSTS_MORE | m->m_len;
1594			bus_dmamap_create(sc->sis_tag, 0, &f->sis_map);
1595			bus_dmamap_load(sc->sis_tag, f->sis_map,
1596			    mtod(m, void *), m->m_len,
1597			    sis_dma_map_desc_ptr, f, 0);
1598			bus_dmamap_sync(sc->sis_tag,
1599			    f->sis_map, BUS_DMASYNC_PREREAD);
1600			if (cnt != 0)
1601				f->sis_ctl |= SIS_CMDSTS_OWN;
1602			cur = frag;
1603			SIS_INC(frag, SIS_TX_LIST_CNT);
1604			cnt++;
1605		}
1606	}
1607
1608	if (m != NULL)
1609		return(ENOBUFS);
1610
1611	sc->sis_ldata.sis_tx_list[cur].sis_mbuf = m_head;
1612	sc->sis_ldata.sis_tx_list[cur].sis_ctl &= ~SIS_CMDSTS_MORE;
1613	sc->sis_ldata.sis_tx_list[*txidx].sis_ctl |= SIS_CMDSTS_OWN;
1614	sc->sis_cdata.sis_tx_cnt += cnt;
1615	*txidx = frag;
1616
1617	return(0);
1618}
1619
1620/*
1621 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1622 * to the mbuf data regions directly in the transmit lists. We also save a
1623 * copy of the pointers since the transmit list fragment pointers are
1624 * physical addresses.
1625 */
1626
1627static void sis_start(ifp)
1628	struct ifnet		*ifp;
1629{
1630	struct sis_softc	*sc;
1631	struct mbuf		*m_head = NULL;
1632	u_int32_t		idx;
1633
1634	sc = ifp->if_softc;
1635	SIS_LOCK(sc);
1636
1637	if (!sc->sis_link) {
1638		SIS_UNLOCK(sc);
1639		return;
1640	}
1641
1642	idx = sc->sis_cdata.sis_tx_prod;
1643
1644	if (ifp->if_flags & IFF_OACTIVE) {
1645		SIS_UNLOCK(sc);
1646		return;
1647	}
1648
1649	while(sc->sis_ldata.sis_tx_list[idx].sis_mbuf == NULL) {
1650		IF_DEQUEUE(&ifp->if_snd, m_head);
1651		if (m_head == NULL)
1652			break;
1653
1654		if (sis_encap(sc, m_head, &idx)) {
1655			IF_PREPEND(&ifp->if_snd, m_head);
1656			ifp->if_flags |= IFF_OACTIVE;
1657			break;
1658		}
1659
1660		/*
1661		 * If there's a BPF listener, bounce a copy of this frame
1662		 * to him.
1663		 */
1664		if (ifp->if_bpf)
1665			bpf_mtap(ifp, m_head);
1666
1667	}
1668
1669	/* Transmit */
1670	sc->sis_cdata.sis_tx_prod = idx;
1671	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
1672
1673	/*
1674	 * Set a timeout in case the chip goes out to lunch.
1675	 */
1676	ifp->if_timer = 5;
1677
1678	SIS_UNLOCK(sc);
1679
1680	return;
1681}
1682
1683static void sis_init(xsc)
1684	void			*xsc;
1685{
1686	struct sis_softc	*sc = xsc;
1687	struct ifnet		*ifp = &sc->arpcom.ac_if;
1688	struct mii_data		*mii;
1689
1690	SIS_LOCK(sc);
1691
1692	/*
1693	 * Cancel pending I/O and free all RX/TX buffers.
1694	 */
1695	sis_stop(sc);
1696
1697	mii = device_get_softc(sc->sis_miibus);
1698
1699	/* Set MAC address */
1700	if (sc->sis_type == SIS_TYPE_83815) {
1701		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
1702		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1703		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1704		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
1705		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1706		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1707		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
1708		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1709		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1710	} else {
1711		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
1712		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1713		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1714		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
1715		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1716		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1717		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
1718		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1719		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1720	}
1721
1722	/* Init circular RX list. */
1723	if (sis_list_rx_init(sc) == ENOBUFS) {
1724		printf("sis%d: initialization failed: no "
1725			"memory for rx buffers\n", sc->sis_unit);
1726		sis_stop(sc);
1727		SIS_UNLOCK(sc);
1728		return;
1729	}
1730
1731	/*
1732	 * Init tx descriptors.
1733	 */
1734	sis_list_tx_init(sc);
1735
1736	/*
1737	 * For the NatSemi chip, we have to explicitly enable the
1738	 * reception of ARP frames, as well as turn on the 'perfect
1739	 * match' filter where we store the station address, otherwise
1740	 * we won't receive unicasts meant for this host.
1741	 */
1742	if (sc->sis_type == SIS_TYPE_83815) {
1743		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
1744		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
1745	}
1746
1747	 /* If we want promiscuous mode, set the allframes bit. */
1748	if (ifp->if_flags & IFF_PROMISC) {
1749		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1750	} else {
1751		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1752	}
1753
1754	/*
1755	 * Set the capture broadcast bit to capture broadcast frames.
1756	 */
1757	if (ifp->if_flags & IFF_BROADCAST) {
1758		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1759	} else {
1760		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1761	}
1762
1763	/*
1764	 * Load the multicast filter.
1765	 */
1766	if (sc->sis_type == SIS_TYPE_83815)
1767		sis_setmulti_ns(sc);
1768	else
1769		sis_setmulti_sis(sc);
1770
1771	/* Turn the receive filter on */
1772	SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
1773
1774	/*
1775	 * Load the address of the RX and TX lists.
1776	 */
1777	CSR_WRITE_4(sc, SIS_RX_LISTPTR, sc->sis_cdata.sis_rx_paddr);
1778	CSR_WRITE_4(sc, SIS_TX_LISTPTR, sc->sis_cdata.sis_tx_paddr);
1779
1780	/* Set RX configuration */
1781	CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG);
1782
1783	/* Accept Long Packets for VLAN support */
1784	SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
1785
1786	/* Set TX configuration */
1787	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
1788		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
1789	} else {
1790		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
1791	}
1792
1793	/* Set full/half duplex mode. */
1794	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1795		SIS_SETBIT(sc, SIS_TX_CFG,
1796		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1797		SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1798	} else {
1799		SIS_CLRBIT(sc, SIS_TX_CFG,
1800		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1801		SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1802	}
1803
1804	/*
1805	 * Enable interrupts.
1806	 */
1807	CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
1808#ifdef DEVICE_POLLING
1809	/*
1810	 * ... only enable interrupts if we are not polling, make sure
1811	 * they are off otherwise.
1812	 */
1813	if (ifp->if_ipending & IFF_POLLING)
1814		CSR_WRITE_4(sc, SIS_IER, 0);
1815	else
1816#endif /* DEVICE_POLLING */
1817	CSR_WRITE_4(sc, SIS_IER, 1);
1818
1819	/* Enable receiver and transmitter. */
1820	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
1821	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1822
1823#ifdef notdef
1824	mii_mediachg(mii);
1825#endif
1826
1827	/*
1828	 * Page 75 of the DP83815 manual recommends the
1829	 * following register settings "for optimum
1830	 * performance." Note however that at least three
1831	 * of the registers are listed as "reserved" in
1832	 * the register map, so who knows what they do.
1833	 */
1834	if (sc->sis_type == SIS_TYPE_83815) {
1835		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
1836		CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
1837		CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
1838		CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
1839		CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
1840	}
1841
1842	ifp->if_flags |= IFF_RUNNING;
1843	ifp->if_flags &= ~IFF_OACTIVE;
1844
1845	sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1846
1847	SIS_UNLOCK(sc);
1848
1849	return;
1850}
1851
1852/*
1853 * Set media options.
1854 */
1855static int sis_ifmedia_upd(ifp)
1856	struct ifnet		*ifp;
1857{
1858	struct sis_softc	*sc;
1859	struct mii_data		*mii;
1860
1861	sc = ifp->if_softc;
1862
1863	mii = device_get_softc(sc->sis_miibus);
1864	sc->sis_link = 0;
1865	if (mii->mii_instance) {
1866		struct mii_softc	*miisc;
1867		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1868			mii_phy_reset(miisc);
1869	}
1870	mii_mediachg(mii);
1871
1872	return(0);
1873}
1874
1875/*
1876 * Report current media status.
1877 */
1878static void sis_ifmedia_sts(ifp, ifmr)
1879	struct ifnet		*ifp;
1880	struct ifmediareq	*ifmr;
1881{
1882	struct sis_softc	*sc;
1883	struct mii_data		*mii;
1884
1885	sc = ifp->if_softc;
1886
1887	mii = device_get_softc(sc->sis_miibus);
1888	mii_pollstat(mii);
1889	ifmr->ifm_active = mii->mii_media_active;
1890	ifmr->ifm_status = mii->mii_media_status;
1891
1892	return;
1893}
1894
1895static int sis_ioctl(ifp, command, data)
1896	struct ifnet		*ifp;
1897	u_long			command;
1898	caddr_t			data;
1899{
1900	struct sis_softc	*sc = ifp->if_softc;
1901	struct ifreq		*ifr = (struct ifreq *) data;
1902	struct mii_data		*mii;
1903	int			error = 0;
1904
1905	switch(command) {
1906	case SIOCSIFADDR:
1907	case SIOCGIFADDR:
1908	case SIOCSIFMTU:
1909		error = ether_ioctl(ifp, command, data);
1910		break;
1911	case SIOCSIFFLAGS:
1912		if (ifp->if_flags & IFF_UP) {
1913			sis_init(sc);
1914		} else {
1915			if (ifp->if_flags & IFF_RUNNING)
1916				sis_stop(sc);
1917		}
1918		error = 0;
1919		break;
1920	case SIOCADDMULTI:
1921	case SIOCDELMULTI:
1922		SIS_LOCK(sc);
1923		if (sc->sis_type == SIS_TYPE_83815)
1924			sis_setmulti_ns(sc);
1925		else
1926			sis_setmulti_sis(sc);
1927		SIS_UNLOCK(sc);
1928		error = 0;
1929		break;
1930	case SIOCGIFMEDIA:
1931	case SIOCSIFMEDIA:
1932		mii = device_get_softc(sc->sis_miibus);
1933		SIS_LOCK(sc);
1934		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1935		SIS_UNLOCK(sc);
1936		break;
1937	default:
1938		error = EINVAL;
1939		break;
1940	}
1941
1942	return(error);
1943}
1944
1945static void sis_watchdog(ifp)
1946	struct ifnet		*ifp;
1947{
1948	struct sis_softc	*sc;
1949
1950	sc = ifp->if_softc;
1951
1952	SIS_LOCK(sc);
1953
1954	ifp->if_oerrors++;
1955	printf("sis%d: watchdog timeout\n", sc->sis_unit);
1956
1957	sis_stop(sc);
1958	sis_reset(sc);
1959	sis_init(sc);
1960
1961	if (ifp->if_snd.ifq_head != NULL)
1962		sis_start(ifp);
1963
1964	SIS_UNLOCK(sc);
1965
1966	return;
1967}
1968
1969/*
1970 * Stop the adapter and free any mbufs allocated to the
1971 * RX and TX lists.
1972 */
1973static void sis_stop(sc)
1974	struct sis_softc	*sc;
1975{
1976	register int		i;
1977	struct ifnet		*ifp;
1978
1979	SIS_LOCK(sc);
1980	ifp = &sc->arpcom.ac_if;
1981	ifp->if_timer = 0;
1982
1983	untimeout(sis_tick, sc, sc->sis_stat_ch);
1984
1985	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1986#ifdef DEVICE_POLLING
1987	ether_poll_deregister(ifp);
1988#endif
1989	CSR_WRITE_4(sc, SIS_IER, 0);
1990	CSR_WRITE_4(sc, SIS_IMR, 0);
1991	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
1992	DELAY(1000);
1993	CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
1994	CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
1995
1996	sc->sis_link = 0;
1997
1998	/*
1999	 * Free data in the RX lists.
2000	 */
2001	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2002		if (sc->sis_ldata.sis_rx_list[i].sis_mbuf != NULL) {
2003			bus_dmamap_unload(sc->sis_tag,
2004			    sc->sis_ldata.sis_rx_list[i].sis_map);
2005			bus_dmamap_destroy(sc->sis_tag,
2006			    sc->sis_ldata.sis_rx_list[i].sis_map);
2007			m_freem(sc->sis_ldata.sis_rx_list[i].sis_mbuf);
2008			sc->sis_ldata.sis_rx_list[i].sis_mbuf = NULL;
2009		}
2010	}
2011	bzero(sc->sis_ldata.sis_rx_list,
2012		sizeof(sc->sis_ldata.sis_rx_list));
2013
2014	/*
2015	 * Free the TX list buffers.
2016	 */
2017	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2018		if (sc->sis_ldata.sis_tx_list[i].sis_mbuf != NULL) {
2019			bus_dmamap_unload(sc->sis_tag,
2020			    sc->sis_ldata.sis_tx_list[i].sis_map);
2021			bus_dmamap_destroy(sc->sis_tag,
2022			    sc->sis_ldata.sis_tx_list[i].sis_map);
2023			m_freem(sc->sis_ldata.sis_tx_list[i].sis_mbuf);
2024			sc->sis_ldata.sis_tx_list[i].sis_mbuf = NULL;
2025		}
2026	}
2027
2028	bzero(sc->sis_ldata.sis_tx_list,
2029		sizeof(sc->sis_ldata.sis_tx_list));
2030
2031	SIS_UNLOCK(sc);
2032
2033	return;
2034}
2035
2036/*
2037 * Stop all chip I/O so that the kernel's probe routines don't
2038 * get confused by errant DMAs when rebooting.
2039 */
2040static void sis_shutdown(dev)
2041	device_t		dev;
2042{
2043	struct sis_softc	*sc;
2044
2045	sc = device_get_softc(dev);
2046	SIS_LOCK(sc);
2047	sis_reset(sc);
2048	sis_stop(sc);
2049	SIS_UNLOCK(sc);
2050
2051	return;
2052}
2053