xref: /dragonfly/sys/dev/netif/sis/if_sis.c (revision 2d8a3be7)
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/pci/if_sis.c,v 1.13.4.24 2003/03/05 18:42:33 njl Exp $
33  * $DragonFly: src/sys/dev/netif/sis/if_sis.c,v 1.5 2003/08/27 09:38:32 rob Exp $
34  *
35  * $FreeBSD: src/sys/pci/if_sis.c,v 1.13.4.24 2003/03/05 18:42:33 njl Exp $
36  */
37 
38 /*
39  * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
40  * available from http://www.sis.com.tw.
41  *
42  * This driver also supports the NatSemi DP83815. Datasheets are
43  * available from http://www.national.com.
44  *
45  * Written by Bill Paul <wpaul@ee.columbia.edu>
46  * Electrical Engineering Department
47  * Columbia University, New York City
48  */
49 
50 /*
51  * The SiS 900 is a fairly simple chip. It uses bus master DMA with
52  * simple TX and RX descriptors of 3 longwords in size. The receiver
53  * has a single perfect filter entry for the station address and a
54  * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
55  * transceiver while the 7016 requires an external transceiver chip.
56  * Both chips offer the standard bit-bang MII interface as well as
57  * an enchanced PHY interface which simplifies accessing MII registers.
58  *
59  * The only downside to this chipset is that RX descriptors must be
60  * longword aligned.
61  */
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sockio.h>
66 #include <sys/mbuf.h>
67 #include <sys/malloc.h>
68 #include <sys/kernel.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
71 
72 #include <net/if.h>
73 #include <net/if_arp.h>
74 #include <net/ethernet.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/vlan/if_vlan_var.h>
79 
80 #include <net/bpf.h>
81 
82 #include <vm/vm.h>              /* for vtophys */
83 #include <vm/pmap.h>            /* for vtophys */
84 #include <machine/clock.h>      /* for DELAY */
85 #include <machine/bus_pio.h>
86 #include <machine/bus_memio.h>
87 #include <machine/bus.h>
88 #include <machine/resource.h>
89 #include <sys/bus.h>
90 #include <sys/rman.h>
91 
92 #include "../mii_layer/mii.h"
93 #include "../mii_layer/miivar.h"
94 
95 #include <bus/pci/pcireg.h>
96 #include <bus/pci/pcivar.h>
97 
98 #define SIS_USEIOSPACE
99 
100 #include "if_sisreg.h"
101 
102 /* "controller miibus0" required.  See GENERIC if you get errors here. */
103 #include "miibus_if.h"
104 
105 /*
106  * Various supported device vendors/types and their names.
107  */
108 static struct sis_type sis_devs[] = {
109 	{ SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
110 	{ SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
111 	{ NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP83815 10/100BaseTX" },
112 	{ 0, 0, NULL }
113 };
114 
115 static int sis_probe		(device_t);
116 static int sis_attach		(device_t);
117 static int sis_detach		(device_t);
118 
119 static int sis_newbuf		(struct sis_softc *,
120 					struct sis_desc *,
121 					struct mbuf *);
122 static int sis_encap		(struct sis_softc *,
123 					struct mbuf *, u_int32_t *);
124 static void sis_rxeof		(struct sis_softc *);
125 static void sis_rxeoc		(struct sis_softc *);
126 static void sis_txeof		(struct sis_softc *);
127 static void sis_intr		(void *);
128 static void sis_tick		(void *);
129 static void sis_start		(struct ifnet *);
130 static int sis_ioctl		(struct ifnet *, u_long, caddr_t);
131 static void sis_init		(void *);
132 static void sis_stop		(struct sis_softc *);
133 static void sis_watchdog		(struct ifnet *);
134 static void sis_shutdown		(device_t);
135 static int sis_ifmedia_upd	(struct ifnet *);
136 static void sis_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
137 
138 static u_int16_t sis_reverse	(u_int16_t);
139 static void sis_delay		(struct sis_softc *);
140 static void sis_eeprom_idle	(struct sis_softc *);
141 static void sis_eeprom_putbyte	(struct sis_softc *, int);
142 static void sis_eeprom_getword	(struct sis_softc *, int, u_int16_t *);
143 static void sis_read_eeprom	(struct sis_softc *, caddr_t, int,
144 							int, int);
145 #ifdef __i386__
146 static void sis_read_cmos	(struct sis_softc *, device_t, caddr_t,
147 							int, int);
148 static void sis_read_mac	(struct sis_softc *, device_t, caddr_t);
149 static device_t sis_find_bridge	(device_t);
150 #endif
151 
152 static void sis_mii_sync	(struct sis_softc *);
153 static void sis_mii_send	(struct sis_softc *, u_int32_t, int);
154 static int sis_mii_readreg	(struct sis_softc *, struct sis_mii_frame *);
155 static int sis_mii_writereg	(struct sis_softc *, struct sis_mii_frame *);
156 static int sis_miibus_readreg	(device_t, int, int);
157 static int sis_miibus_writereg	(device_t, int, int, int);
158 static void sis_miibus_statchg	(device_t);
159 
160 static void sis_setmulti_sis	(struct sis_softc *);
161 static void sis_setmulti_ns	(struct sis_softc *);
162 static u_int32_t sis_crc	(struct sis_softc *, caddr_t);
163 static void sis_reset		(struct sis_softc *);
164 static int sis_list_rx_init	(struct sis_softc *);
165 static int sis_list_tx_init	(struct sis_softc *);
166 
167 #ifdef SIS_USEIOSPACE
168 #define SIS_RES			SYS_RES_IOPORT
169 #define SIS_RID			SIS_PCI_LOIO
170 #else
171 #define SIS_RES			SYS_RES_MEMORY
172 #define SIS_RID			SIS_PCI_LOMEM
173 #endif
174 
175 static device_method_t sis_methods[] = {
176 	/* Device interface */
177 	DEVMETHOD(device_probe,		sis_probe),
178 	DEVMETHOD(device_attach,	sis_attach),
179 	DEVMETHOD(device_detach,	sis_detach),
180 	DEVMETHOD(device_shutdown,	sis_shutdown),
181 
182 	/* bus interface */
183 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
184 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
185 
186 	/* MII interface */
187 	DEVMETHOD(miibus_readreg,	sis_miibus_readreg),
188 	DEVMETHOD(miibus_writereg,	sis_miibus_writereg),
189 	DEVMETHOD(miibus_statchg,	sis_miibus_statchg),
190 
191 	{ 0, 0 }
192 };
193 
194 static driver_t sis_driver = {
195 	"sis",
196 	sis_methods,
197 	sizeof(struct sis_softc)
198 };
199 
200 static devclass_t sis_devclass;
201 
202 DRIVER_MODULE(if_sis, pci, sis_driver, sis_devclass, 0, 0);
203 DRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);
204 
205 #define SIS_SETBIT(sc, reg, x)				\
206 	CSR_WRITE_4(sc, reg,				\
207 		CSR_READ_4(sc, reg) | (x))
208 
209 #define SIS_CLRBIT(sc, reg, x)				\
210 	CSR_WRITE_4(sc, reg,				\
211 		CSR_READ_4(sc, reg) & ~(x))
212 
213 #define SIO_SET(x)					\
214 	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
215 
216 #define SIO_CLR(x)					\
217 	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
218 
219 /*
220  * Routine to reverse the bits in a word. Stolen almost
221  * verbatim from /usr/games/fortune.
222  */
223 static u_int16_t sis_reverse(n)
224 	u_int16_t		n;
225 {
226 	n = ((n >>  1) & 0x5555) | ((n <<  1) & 0xaaaa);
227 	n = ((n >>  2) & 0x3333) | ((n <<  2) & 0xcccc);
228 	n = ((n >>  4) & 0x0f0f) | ((n <<  4) & 0xf0f0);
229 	n = ((n >>  8) & 0x00ff) | ((n <<  8) & 0xff00);
230 
231 	return(n);
232 }
233 
234 static void sis_delay(sc)
235 	struct sis_softc	*sc;
236 {
237 	int			idx;
238 
239 	for (idx = (300 / 33) + 1; idx > 0; idx--)
240 		CSR_READ_4(sc, SIS_CSR);
241 
242 	return;
243 }
244 
245 static void sis_eeprom_idle(sc)
246 	struct sis_softc	*sc;
247 {
248 	int		i;
249 
250 	SIO_SET(SIS_EECTL_CSEL);
251 	sis_delay(sc);
252 	SIO_SET(SIS_EECTL_CLK);
253 	sis_delay(sc);
254 
255 	for (i = 0; i < 25; i++) {
256 		SIO_CLR(SIS_EECTL_CLK);
257 		sis_delay(sc);
258 		SIO_SET(SIS_EECTL_CLK);
259 		sis_delay(sc);
260 	}
261 
262 	SIO_CLR(SIS_EECTL_CLK);
263 	sis_delay(sc);
264 	SIO_CLR(SIS_EECTL_CSEL);
265 	sis_delay(sc);
266 	CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
267 
268 	return;
269 }
270 
271 /*
272  * Send a read command and address to the EEPROM, check for ACK.
273  */
274 static void sis_eeprom_putbyte(sc, addr)
275 	struct sis_softc	*sc;
276 	int			addr;
277 {
278 	int		d, i;
279 
280 	d = addr | SIS_EECMD_READ;
281 
282 	/*
283 	 * Feed in each bit and stobe the clock.
284 	 */
285 	for (i = 0x400; i; i >>= 1) {
286 		if (d & i) {
287 			SIO_SET(SIS_EECTL_DIN);
288 		} else {
289 			SIO_CLR(SIS_EECTL_DIN);
290 		}
291 		sis_delay(sc);
292 		SIO_SET(SIS_EECTL_CLK);
293 		sis_delay(sc);
294 		SIO_CLR(SIS_EECTL_CLK);
295 		sis_delay(sc);
296 	}
297 
298 	return;
299 }
300 
301 /*
302  * Read a word of data stored in the EEPROM at address 'addr.'
303  */
304 static void sis_eeprom_getword(sc, addr, dest)
305 	struct sis_softc	*sc;
306 	int			addr;
307 	u_int16_t		*dest;
308 {
309 	int		i;
310 	u_int16_t		word = 0;
311 
312 	/* Force EEPROM to idle state. */
313 	sis_eeprom_idle(sc);
314 
315 	/* Enter EEPROM access mode. */
316 	sis_delay(sc);
317 	SIO_CLR(SIS_EECTL_CLK);
318 	sis_delay(sc);
319 	SIO_SET(SIS_EECTL_CSEL);
320 	sis_delay(sc);
321 
322 	/*
323 	 * Send address of word we want to read.
324 	 */
325 	sis_eeprom_putbyte(sc, addr);
326 
327 	/*
328 	 * Start reading bits from EEPROM.
329 	 */
330 	for (i = 0x8000; i; i >>= 1) {
331 		SIO_SET(SIS_EECTL_CLK);
332 		sis_delay(sc);
333 		if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
334 			word |= i;
335 		sis_delay(sc);
336 		SIO_CLR(SIS_EECTL_CLK);
337 		sis_delay(sc);
338 	}
339 
340 	/* Turn off EEPROM access mode. */
341 	sis_eeprom_idle(sc);
342 
343 	*dest = word;
344 
345 	return;
346 }
347 
348 /*
349  * Read a sequence of words from the EEPROM.
350  */
351 static void sis_read_eeprom(sc, dest, off, cnt, swap)
352 	struct sis_softc	*sc;
353 	caddr_t			dest;
354 	int			off;
355 	int			cnt;
356 	int			swap;
357 {
358 	int			i;
359 	u_int16_t		word = 0, *ptr;
360 
361 	for (i = 0; i < cnt; i++) {
362 		sis_eeprom_getword(sc, off + i, &word);
363 		ptr = (u_int16_t *)(dest + (i * 2));
364 		if (swap)
365 			*ptr = ntohs(word);
366 		else
367 			*ptr = word;
368 	}
369 
370 	return;
371 }
372 
373 #ifdef __i386__
374 static device_t sis_find_bridge(dev)
375 	device_t		dev;
376 {
377 	devclass_t		pci_devclass;
378 	device_t		*pci_devices;
379 	int			pci_count = 0;
380 	device_t		*pci_children;
381 	int			pci_childcount = 0;
382 	device_t		*busp, *childp;
383 	device_t		child = NULL;
384 	int			i, j;
385 
386 	if ((pci_devclass = devclass_find("pci")) == NULL)
387 		return(NULL);
388 
389 	devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
390 
391 	for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
392 		pci_childcount = 0;
393 		device_get_children(*busp, &pci_children, &pci_childcount);
394 		for (j = 0, childp = pci_children;
395 		    j < pci_childcount; j++, childp++) {
396 			if (pci_get_vendor(*childp) == SIS_VENDORID &&
397 			    pci_get_device(*childp) == 0x0008) {
398 				child = *childp;
399 				goto done;
400 			}
401 		}
402 	}
403 
404 done:
405 	free(pci_devices, M_TEMP);
406 	free(pci_children, M_TEMP);
407 	return(child);
408 }
409 
410 static void sis_read_cmos(sc, dev, dest, off, cnt)
411 	struct sis_softc	*sc;
412 	device_t		dev;
413 	caddr_t			dest;
414 	int			off;
415 	int			cnt;
416 {
417 	device_t		bridge;
418 	u_int8_t		reg;
419 	int			i;
420 	bus_space_tag_t		btag;
421 
422 	bridge = sis_find_bridge(dev);
423 	if (bridge == NULL)
424 		return;
425 	reg = pci_read_config(bridge, 0x48, 1);
426 	pci_write_config(bridge, 0x48, reg|0x40, 1);
427 
428 	/* XXX */
429 	btag = I386_BUS_SPACE_IO;
430 
431 	for (i = 0; i < cnt; i++) {
432 		bus_space_write_1(btag, 0x0, 0x70, i + off);
433 		*(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
434 	}
435 
436 	pci_write_config(bridge, 0x48, reg & ~0x40, 1);
437 	return;
438 }
439 
440 static void sis_read_mac(sc, dev, dest)
441 	struct sis_softc	*sc;
442 	device_t		dev;
443 	caddr_t			dest;
444 {
445 	u_int32_t		filtsave, csrsave;
446 
447 	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
448 	csrsave = CSR_READ_4(sc, SIS_CSR);
449 
450 	CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
451 	CSR_WRITE_4(sc, SIS_CSR, 0);
452 
453 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
454 
455 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
456 	((u_int16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
457 	CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
458 	((u_int16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
459 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
460 	((u_int16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
461 
462 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
463 	CSR_WRITE_4(sc, SIS_CSR, csrsave);
464 	return;
465 }
466 #endif
467 
468 /*
469  * Sync the PHYs by setting data bit and strobing the clock 32 times.
470  */
471 static void sis_mii_sync(sc)
472 	struct sis_softc	*sc;
473 {
474 	int		i;
475 
476 	SIO_SET(SIS_MII_DIR|SIS_MII_DATA);
477 
478 	for (i = 0; i < 32; i++) {
479 		SIO_SET(SIS_MII_CLK);
480 		DELAY(1);
481 		SIO_CLR(SIS_MII_CLK);
482 		DELAY(1);
483 	}
484 
485 	return;
486 }
487 
488 /*
489  * Clock a series of bits through the MII.
490  */
491 static void sis_mii_send(sc, bits, cnt)
492 	struct sis_softc	*sc;
493 	u_int32_t		bits;
494 	int			cnt;
495 {
496 	int			i;
497 
498 	SIO_CLR(SIS_MII_CLK);
499 
500 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
501                 if (bits & i) {
502 			SIO_SET(SIS_MII_DATA);
503                 } else {
504 			SIO_CLR(SIS_MII_DATA);
505                 }
506 		DELAY(1);
507 		SIO_CLR(SIS_MII_CLK);
508 		DELAY(1);
509 		SIO_SET(SIS_MII_CLK);
510 	}
511 }
512 
513 /*
514  * Read an PHY register through the MII.
515  */
516 static int sis_mii_readreg(sc, frame)
517 	struct sis_softc	*sc;
518 	struct sis_mii_frame	*frame;
519 
520 {
521 	int			i, ack, s;
522 
523 	s = splimp();
524 
525 	/*
526 	 * Set up frame for RX.
527 	 */
528 	frame->mii_stdelim = SIS_MII_STARTDELIM;
529 	frame->mii_opcode = SIS_MII_READOP;
530 	frame->mii_turnaround = 0;
531 	frame->mii_data = 0;
532 
533 	/*
534  	 * Turn on data xmit.
535 	 */
536 	SIO_SET(SIS_MII_DIR);
537 
538 	sis_mii_sync(sc);
539 
540 	/*
541 	 * Send command/address info.
542 	 */
543 	sis_mii_send(sc, frame->mii_stdelim, 2);
544 	sis_mii_send(sc, frame->mii_opcode, 2);
545 	sis_mii_send(sc, frame->mii_phyaddr, 5);
546 	sis_mii_send(sc, frame->mii_regaddr, 5);
547 
548 	/* Idle bit */
549 	SIO_CLR((SIS_MII_CLK|SIS_MII_DATA));
550 	DELAY(1);
551 	SIO_SET(SIS_MII_CLK);
552 	DELAY(1);
553 
554 	/* Turn off xmit. */
555 	SIO_CLR(SIS_MII_DIR);
556 
557 	/* Check for ack */
558 	SIO_CLR(SIS_MII_CLK);
559 	DELAY(1);
560 	ack = CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA;
561 	SIO_SET(SIS_MII_CLK);
562 	DELAY(1);
563 
564 	/*
565 	 * Now try reading data bits. If the ack failed, we still
566 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
567 	 */
568 	if (ack) {
569 		for(i = 0; i < 16; i++) {
570 			SIO_CLR(SIS_MII_CLK);
571 			DELAY(1);
572 			SIO_SET(SIS_MII_CLK);
573 			DELAY(1);
574 		}
575 		goto fail;
576 	}
577 
578 	for (i = 0x8000; i; i >>= 1) {
579 		SIO_CLR(SIS_MII_CLK);
580 		DELAY(1);
581 		if (!ack) {
582 			if (CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA)
583 				frame->mii_data |= i;
584 			DELAY(1);
585 		}
586 		SIO_SET(SIS_MII_CLK);
587 		DELAY(1);
588 	}
589 
590 fail:
591 
592 	SIO_CLR(SIS_MII_CLK);
593 	DELAY(1);
594 	SIO_SET(SIS_MII_CLK);
595 	DELAY(1);
596 
597 	splx(s);
598 
599 	if (ack)
600 		return(1);
601 	return(0);
602 }
603 
604 /*
605  * Write to a PHY register through the MII.
606  */
607 static int sis_mii_writereg(sc, frame)
608 	struct sis_softc	*sc;
609 	struct sis_mii_frame	*frame;
610 
611 {
612 	int			s;
613 
614 	s = splimp();
615 	/*
616 	 * Set up frame for TX.
617 	 */
618 
619 	frame->mii_stdelim = SIS_MII_STARTDELIM;
620 	frame->mii_opcode = SIS_MII_WRITEOP;
621 	frame->mii_turnaround = SIS_MII_TURNAROUND;
622 
623 	/*
624  	 * Turn on data output.
625 	 */
626 	SIO_SET(SIS_MII_DIR);
627 
628 	sis_mii_sync(sc);
629 
630 	sis_mii_send(sc, frame->mii_stdelim, 2);
631 	sis_mii_send(sc, frame->mii_opcode, 2);
632 	sis_mii_send(sc, frame->mii_phyaddr, 5);
633 	sis_mii_send(sc, frame->mii_regaddr, 5);
634 	sis_mii_send(sc, frame->mii_turnaround, 2);
635 	sis_mii_send(sc, frame->mii_data, 16);
636 
637 	/* Idle bit. */
638 	SIO_SET(SIS_MII_CLK);
639 	DELAY(1);
640 	SIO_CLR(SIS_MII_CLK);
641 	DELAY(1);
642 
643 	/*
644 	 * Turn off xmit.
645 	 */
646 	SIO_CLR(SIS_MII_DIR);
647 
648 	splx(s);
649 
650 	return(0);
651 }
652 
653 static int sis_miibus_readreg(dev, phy, reg)
654 	device_t		dev;
655 	int			phy, reg;
656 {
657 	struct sis_softc	*sc;
658 	int			i, val = 0;
659 	struct sis_mii_frame	frame;
660 
661 	sc = device_get_softc(dev);
662 
663 	if (sc->sis_type == SIS_TYPE_83815) {
664 		if (phy != 0)
665 			return(0);
666 		/*
667 		 * The NatSemi chip can take a while after
668 		 * a reset to come ready, during which the BMSR
669 		 * returns a value of 0. This is *never* supposed
670 		 * to happen: some of the BMSR bits are meant to
671 		 * be hardwired in the on position, and this can
672 		 * confuse the miibus code a bit during the probe
673 		 * and attach phase. So we make an effort to check
674 		 * for this condition and wait for it to clear.
675 		 */
676 		if (!CSR_READ_4(sc, NS_BMSR))
677 			DELAY(1000);
678 		return CSR_READ_4(sc, NS_BMCR + (reg * 4));
679 	}
680 	/*
681 	* Chipsets < SIS_635 seem not to be able to read/write
682 	* through mdio. Use the enhanced PHY access register
683 	* again for them.
684 	*/
685 	if (sc->sis_type == SIS_TYPE_900 &&
686 	    sc->sis_rev < SIS_REV_635) {
687 
688 		if (phy != 0)
689 			return(0);
690 
691 		CSR_WRITE_4(sc, SIS_PHYCTL,
692 		    (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
693 		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
694 
695 		for (i = 0; i < SIS_TIMEOUT; i++) {
696 			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
697 				break;
698 		}
699 
700 		if (i == SIS_TIMEOUT) {
701 			printf("sis%d: PHY failed to come ready\n",
702 			    sc->sis_unit);
703 			return(0);
704 		}
705 
706 		val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
707 
708 		if (val == 0xFFFF)
709 			return(0);
710 
711 		return(val);
712 	} else {
713 		bzero((char *)&frame, sizeof(frame));
714 		frame.mii_phyaddr = phy;
715 		frame.mii_regaddr = reg;
716 		sis_mii_readreg(sc, &frame);
717 		return(frame.mii_data);
718 	}
719 }
720 
721 static int sis_miibus_writereg(dev, phy, reg, data)
722 	device_t		dev;
723 	int			phy, reg, data;
724 {
725 	struct sis_softc	*sc;
726 	int			i;
727 	struct sis_mii_frame	frame;
728 
729 	sc = device_get_softc(dev);
730 
731 	if (sc->sis_type == SIS_TYPE_83815) {
732 		if (phy != 0)
733 			return(0);
734 		CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
735 		return(0);
736 	}
737 
738 	if (sc->sis_type == SIS_TYPE_900 &&
739 	    sc->sis_rev < SIS_REV_635) {
740 
741 		if (phy != 0)
742 			return(0);
743 		CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
744 		    (reg << 6) | SIS_PHYOP_WRITE);
745 		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
746 
747 		for (i = 0; i < SIS_TIMEOUT; i++) {
748 			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
749 				break;
750 		}
751 
752 		if (i == SIS_TIMEOUT)
753 			printf("sis%d: PHY failed to come ready\n",
754 			    sc->sis_unit);
755 	} else {
756 		bzero((char *)&frame, sizeof(frame));
757 
758 		frame.mii_phyaddr = phy;
759 		frame.mii_regaddr = reg;
760 		frame.mii_data = data;
761 		sis_mii_writereg(sc, &frame);
762 	}
763 	return(0);
764 }
765 
766 static void sis_miibus_statchg(dev)
767 	device_t		dev;
768 {
769 	struct sis_softc	*sc;
770 
771 	sc = device_get_softc(dev);
772 	sis_init(sc);
773 
774 	return;
775 }
776 
777 static u_int32_t sis_crc(sc, addr)
778 	struct sis_softc	*sc;
779 	caddr_t			addr;
780 {
781 	u_int32_t		crc, carry;
782 	int			i, j;
783 	u_int8_t		c;
784 
785 	/* Compute CRC for the address value. */
786 	crc = 0xFFFFFFFF; /* initial value */
787 
788 	for (i = 0; i < 6; i++) {
789 		c = *(addr + i);
790 		for (j = 0; j < 8; j++) {
791 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
792 			crc <<= 1;
793 			c >>= 1;
794 			if (carry)
795 				crc = (crc ^ 0x04c11db6) | carry;
796 		}
797 	}
798 
799 	/*
800 	 * return the filter bit position
801 	 *
802 	 * The NatSemi chip has a 512-bit filter, which is
803 	 * different than the SiS, so we special-case it.
804 	 */
805 	if (sc->sis_type == SIS_TYPE_83815)
806 		return (crc >> 23);
807 	else if (sc->sis_rev >= SIS_REV_635 ||
808 	    sc->sis_rev == SIS_REV_900B)
809 		return (crc >> 24);
810 	else
811 		return (crc >> 25);
812 }
813 
814 static void sis_setmulti_ns(sc)
815 	struct sis_softc	*sc;
816 {
817 	struct ifnet		*ifp;
818 	struct ifmultiaddr	*ifma;
819 	u_int32_t		h = 0, i, filtsave;
820 	int			bit, index;
821 
822 	ifp = &sc->arpcom.ac_if;
823 
824 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
825 		SIS_CLRBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
826 		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
827 		return;
828 	}
829 
830 	/*
831 	 * We have to explicitly enable the multicast hash table
832 	 * on the NatSemi chip if we want to use it, which we do.
833 	 */
834 	SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
835 	SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
836 
837 	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
838 
839 	/* first, zot all the existing hash bits */
840 	for (i = 0; i < 32; i++) {
841 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + (i*2));
842 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
843 	}
844 
845 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
846 	    ifma = ifma->ifma_link.le_next) {
847 		if (ifma->ifma_addr->sa_family != AF_LINK)
848 			continue;
849 		h = sis_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
850 		index = h >> 3;
851 		bit = h & 0x1F;
852 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
853 		if (bit > 0xF)
854 			bit -= 0x10;
855 		SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
856 	}
857 
858 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
859 
860 	return;
861 }
862 
863 static void sis_setmulti_sis(sc)
864 	struct sis_softc	*sc;
865 {
866 	struct ifnet		*ifp;
867 	struct ifmultiaddr	*ifma;
868 	u_int32_t		h, i, n, ctl;
869 	u_int16_t		hashes[16];
870 
871 	ifp = &sc->arpcom.ac_if;
872 
873 	/* hash table size */
874 	if (sc->sis_rev >= SIS_REV_635 ||
875 	    sc->sis_rev == SIS_REV_900B)
876 		n = 16;
877 	else
878 		n = 8;
879 
880 	ctl = CSR_READ_4(sc, SIS_RXFILT_CTL) & SIS_RXFILTCTL_ENABLE;
881 
882 	if (ifp->if_flags & IFF_BROADCAST)
883 		ctl |= SIS_RXFILTCTL_BROAD;
884 
885 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
886 		ctl |= SIS_RXFILTCTL_ALLMULTI;
887 		if (ifp->if_flags & IFF_PROMISC)
888 			ctl |= SIS_RXFILTCTL_BROAD|SIS_RXFILTCTL_ALLPHYS;
889 		for (i = 0; i < n; i++)
890 			hashes[i] = ~0;
891 	} else {
892 		for (i = 0; i < n; i++)
893 			hashes[i] = 0;
894 		i = 0;
895 		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
896 			if (ifma->ifma_addr->sa_family != AF_LINK)
897 				continue;
898 			h = sis_crc(sc,
899 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
900 			hashes[h >> 4] |= 1 << (h & 0xf);
901 			i++;
902 		}
903 		if (i > n) {
904 			ctl |= SIS_RXFILTCTL_ALLMULTI;
905 			for (i = 0; i < n; i++)
906 				hashes[i] = ~0;
907 		}
908 	}
909 
910 	for (i = 0; i < n; i++) {
911 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
912 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, hashes[i]);
913 	}
914 
915 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, ctl);
916 }
917 
918 static void sis_reset(sc)
919 	struct sis_softc	*sc;
920 {
921 	int		i;
922 
923 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
924 
925 	for (i = 0; i < SIS_TIMEOUT; i++) {
926 		if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
927 			break;
928 	}
929 
930 	if (i == SIS_TIMEOUT)
931 		printf("sis%d: reset never completed\n", sc->sis_unit);
932 
933 	/* Wait a little while for the chip to get its brains in order. */
934 	DELAY(1000);
935 
936 	/*
937 	 * If this is a NetSemi chip, make sure to clear
938 	 * PME mode.
939 	 */
940 	if (sc->sis_type == SIS_TYPE_83815) {
941 		CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
942 		CSR_WRITE_4(sc, NS_CLKRUN, 0);
943 	}
944 
945         return;
946 }
947 
948 /*
949  * Probe for an SiS chip. Check the PCI vendor and device
950  * IDs against our list and return a device name if we find a match.
951  */
952 static int sis_probe(dev)
953 	device_t		dev;
954 {
955 	struct sis_type		*t;
956 
957 	t = sis_devs;
958 
959 	while(t->sis_name != NULL) {
960 		if ((pci_get_vendor(dev) == t->sis_vid) &&
961 		    (pci_get_device(dev) == t->sis_did)) {
962 			device_set_desc(dev, t->sis_name);
963 			return(0);
964 		}
965 		t++;
966 	}
967 
968 	return(ENXIO);
969 }
970 
971 /*
972  * Attach the interface. Allocate softc structures, do ifmedia
973  * setup and ethernet/BPF attach.
974  */
975 static int sis_attach(dev)
976 	device_t		dev;
977 {
978 	int			s;
979 	u_char			eaddr[ETHER_ADDR_LEN];
980 	u_int32_t		command;
981 	struct sis_softc	*sc;
982 	struct ifnet		*ifp;
983 	int			unit, error, rid, waittime;
984 
985 	s = splimp();
986 
987 	error = waittime = 0;
988 	sc = device_get_softc(dev);
989 	unit = device_get_unit(dev);
990 	bzero(sc, sizeof(struct sis_softc));
991 
992 	if (pci_get_device(dev) == SIS_DEVICEID_900)
993 		sc->sis_type = SIS_TYPE_900;
994 	if (pci_get_device(dev) == SIS_DEVICEID_7016)
995 		sc->sis_type = SIS_TYPE_7016;
996 	if (pci_get_vendor(dev) == NS_VENDORID)
997 		sc->sis_type = SIS_TYPE_83815;
998 
999 	sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
1000 
1001 	/*
1002 	 * Handle power management nonsense.
1003 	 */
1004 
1005 	command = pci_read_config(dev, SIS_PCI_CAPID, 4) & 0x000000FF;
1006 	if (command == 0x01) {
1007 
1008 		command = pci_read_config(dev, SIS_PCI_PWRMGMTCTRL, 4);
1009 		if (command & SIS_PSTATE_MASK) {
1010 			u_int32_t		iobase, membase, irq;
1011 
1012 			/* Save important PCI config data. */
1013 			iobase = pci_read_config(dev, SIS_PCI_LOIO, 4);
1014 			membase = pci_read_config(dev, SIS_PCI_LOMEM, 4);
1015 			irq = pci_read_config(dev, SIS_PCI_INTLINE, 4);
1016 
1017 			/* Reset the power state. */
1018 			printf("sis%d: chip is in D%d power mode "
1019 			"-- setting to D0\n", unit, command & SIS_PSTATE_MASK);
1020 			command &= 0xFFFFFFFC;
1021 			pci_write_config(dev, SIS_PCI_PWRMGMTCTRL, command, 4);
1022 
1023 			/* Restore PCI config data. */
1024 			pci_write_config(dev, SIS_PCI_LOIO, iobase, 4);
1025 			pci_write_config(dev, SIS_PCI_LOMEM, membase, 4);
1026 			pci_write_config(dev, SIS_PCI_INTLINE, irq, 4);
1027 		}
1028 	}
1029 
1030 	/*
1031 	 * Map control/status registers.
1032 	 */
1033 	command = pci_read_config(dev, PCIR_COMMAND, 4);
1034 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1035 	pci_write_config(dev, PCIR_COMMAND, command, 4);
1036 	command = pci_read_config(dev, PCIR_COMMAND, 4);
1037 
1038 #ifdef SIS_USEIOSPACE
1039 	if (!(command & PCIM_CMD_PORTEN)) {
1040 		printf("sis%d: failed to enable I/O ports!\n", unit);
1041 		error = ENXIO;;
1042 		goto fail;
1043 	}
1044 #else
1045 	if (!(command & PCIM_CMD_MEMEN)) {
1046 		printf("sis%d: failed to enable memory mapping!\n", unit);
1047 		error = ENXIO;;
1048 		goto fail;
1049 	}
1050 #endif
1051 
1052 	rid = SIS_RID;
1053 	sc->sis_res = bus_alloc_resource(dev, SIS_RES, &rid,
1054 	    0, ~0, 1, RF_ACTIVE);
1055 
1056 	if (sc->sis_res == NULL) {
1057 		printf("sis%d: couldn't map ports/memory\n", unit);
1058 		error = ENXIO;
1059 		goto fail;
1060 	}
1061 
1062 	sc->sis_btag = rman_get_bustag(sc->sis_res);
1063 	sc->sis_bhandle = rman_get_bushandle(sc->sis_res);
1064 
1065 	/* Allocate interrupt */
1066 	rid = 0;
1067 	sc->sis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1068 	    RF_SHAREABLE | RF_ACTIVE);
1069 
1070 	if (sc->sis_irq == NULL) {
1071 		printf("sis%d: couldn't map interrupt\n", unit);
1072 		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1073 		error = ENXIO;
1074 		goto fail;
1075 	}
1076 
1077 	error = bus_setup_intr(dev, sc->sis_irq, INTR_TYPE_NET,
1078 	    sis_intr, sc, &sc->sis_intrhand);
1079 
1080 	if (error) {
1081 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1082 		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1083 		printf("sis%d: couldn't set up irq\n", unit);
1084 		goto fail;
1085 	}
1086 
1087 	/* Reset the adapter. */
1088 	sis_reset(sc);
1089 
1090 	if (sc->sis_type == SIS_TYPE_900 &&
1091             (sc->sis_rev == SIS_REV_635 ||
1092              sc->sis_rev == SIS_REV_900B)) {
1093 		SIO_SET(SIS_CFG_RND_CNT);
1094 		SIO_SET(SIS_CFG_PERR_DETECT);
1095 	}
1096 
1097 	/*
1098 	 * Get station address from the EEPROM.
1099 	 */
1100 	switch (pci_get_vendor(dev)) {
1101 	case NS_VENDORID:
1102 		/*
1103 		 * Reading the MAC address out of the EEPROM on
1104 		 * the NatSemi chip takes a bit more work than
1105 		 * you'd expect. The address spans 4 16-bit words,
1106 		 * with the first word containing only a single bit.
1107 		 * You have to shift everything over one bit to
1108 		 * get it aligned properly. Also, the bits are
1109 		 * stored backwards (the LSB is really the MSB,
1110 		 * and so on) so you have to reverse them in order
1111 		 * to get the MAC address into the form we want.
1112 		 * Why? Who the hell knows.
1113 		 */
1114 		{
1115 			u_int16_t		tmp[4];
1116 
1117 			sis_read_eeprom(sc, (caddr_t)&tmp,
1118 			    NS_EE_NODEADDR, 4, 0);
1119 
1120 			/* Shift everything over one bit. */
1121 			tmp[3] = tmp[3] >> 1;
1122 			tmp[3] |= tmp[2] << 15;
1123 			tmp[2] = tmp[2] >> 1;
1124 			tmp[2] |= tmp[1] << 15;
1125 			tmp[1] = tmp[1] >> 1;
1126 			tmp[1] |= tmp[0] << 15;
1127 
1128 			/* Now reverse all the bits. */
1129 			tmp[3] = sis_reverse(tmp[3]);
1130 			tmp[2] = sis_reverse(tmp[2]);
1131 			tmp[1] = sis_reverse(tmp[1]);
1132 
1133 			bcopy((char *)&tmp[1], eaddr, ETHER_ADDR_LEN);
1134 		}
1135 		break;
1136 	case SIS_VENDORID:
1137 	default:
1138 #ifdef __i386__
1139 		/*
1140 		 * If this is a SiS 630E chipset with an embedded
1141 		 * SiS 900 controller, we have to read the MAC address
1142 		 * from the APC CMOS RAM. Our method for doing this
1143 		 * is very ugly since we have to reach out and grab
1144 		 * ahold of hardware for which we cannot properly
1145 		 * allocate resources. This code is only compiled on
1146 		 * the i386 architecture since the SiS 630E chipset
1147 		 * is for x86 motherboards only. Note that there are
1148 		 * a lot of magic numbers in this hack. These are
1149 		 * taken from SiS's Linux driver. I'd like to replace
1150 		 * them with proper symbolic definitions, but that
1151 		 * requires some datasheets that I don't have access
1152 		 * to at the moment.
1153 		 */
1154 		if (sc->sis_rev == SIS_REV_630S ||
1155 		    sc->sis_rev == SIS_REV_630E ||
1156 		    sc->sis_rev == SIS_REV_630EA1)
1157 			sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
1158 
1159 		else if (sc->sis_rev == SIS_REV_635 ||
1160 			 sc->sis_rev == SIS_REV_630ET)
1161 			sis_read_mac(sc, dev, (caddr_t)&eaddr);
1162 		else if (sc->sis_rev == SIS_REV_96x) {
1163 			/*
1164 			 * Allow to read EEPROM from LAN. It is shared
1165 			 * between a 1394 controller and the NIC and each
1166 			 * time we access it, we need to set SIS_EECMD_REQ.
1167 			 */
1168 			SIO_SET(SIS_EECMD_REQ);
1169 			for (waittime = 0; waittime < SIS_TIMEOUT;
1170 			    waittime++) {
1171 				/* Force EEPROM to idle state. */
1172 				sis_eeprom_idle(sc);
1173 				if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1174 					sis_read_eeprom(sc, (caddr_t)&eaddr,
1175 					    SIS_EE_NODEADDR, 3, 0);
1176 					break;
1177 				}
1178 				DELAY(1);
1179 			}
1180 			/*
1181 			 * Set SIS_EECTL_CLK to high, so a other master
1182 			 * can operate on the i2c bus.
1183 			 */
1184 			SIO_SET(SIS_EECTL_CLK);
1185 			/* Refuse EEPROM access by LAN */
1186 			SIO_SET(SIS_EECMD_DONE);
1187 		} else
1188 #endif
1189 			sis_read_eeprom(sc, (caddr_t)&eaddr,
1190 			    SIS_EE_NODEADDR, 3, 0);
1191 		break;
1192 	}
1193 
1194 	/*
1195 	 * A SiS chip was detected. Inform the world.
1196 	 */
1197 	printf("sis%d: Ethernet address: %6D\n", unit, eaddr, ":");
1198 
1199 	sc->sis_unit = unit;
1200 	callout_handle_init(&sc->sis_stat_ch);
1201 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1202 
1203 	sc->sis_ldata = contigmalloc(sizeof(struct sis_list_data), M_DEVBUF,
1204 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1205 
1206 	if (sc->sis_ldata == NULL) {
1207 		printf("sis%d: no memory for list buffers!\n", unit);
1208 		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1209 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1210 		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1211 		error = ENXIO;
1212 		goto fail;
1213 	}
1214 	bzero(sc->sis_ldata, sizeof(struct sis_list_data));
1215 
1216 	ifp = &sc->arpcom.ac_if;
1217 	ifp->if_softc = sc;
1218 	ifp->if_unit = unit;
1219 	ifp->if_name = "sis";
1220 	ifp->if_mtu = ETHERMTU;
1221 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1222 	ifp->if_ioctl = sis_ioctl;
1223 	ifp->if_output = ether_output;
1224 	ifp->if_start = sis_start;
1225 	ifp->if_watchdog = sis_watchdog;
1226 	ifp->if_init = sis_init;
1227 	ifp->if_baudrate = 10000000;
1228 	ifp->if_snd.ifq_maxlen = SIS_TX_LIST_CNT - 1;
1229 
1230 	/*
1231 	 * Do MII setup.
1232 	 */
1233 	if (mii_phy_probe(dev, &sc->sis_miibus,
1234 	    sis_ifmedia_upd, sis_ifmedia_sts)) {
1235 		printf("sis%d: MII without any PHY!\n", sc->sis_unit);
1236 		contigfree(sc->sis_ldata, sizeof(struct sis_list_data),
1237 		    M_DEVBUF);
1238 		bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1239 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1240 		bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1241 		error = ENXIO;
1242 		goto fail;
1243 	}
1244 
1245 	/*
1246 	 * Call MI attach routine.
1247 	 */
1248 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1249 
1250 	/*
1251 	 * Tell the upper layer(s) we support long frames.
1252 	 */
1253 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1254 
1255 	callout_handle_init(&sc->sis_stat_ch);
1256 
1257 fail:
1258 	splx(s);
1259 	return(error);
1260 }
1261 
1262 static int sis_detach(dev)
1263 	device_t		dev;
1264 {
1265 	struct sis_softc	*sc;
1266 	struct ifnet		*ifp;
1267 	int			s;
1268 
1269 	s = splimp();
1270 
1271 	sc = device_get_softc(dev);
1272 	ifp = &sc->arpcom.ac_if;
1273 
1274 	sis_reset(sc);
1275 	sis_stop(sc);
1276 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1277 
1278 	bus_generic_detach(dev);
1279 	device_delete_child(dev, sc->sis_miibus);
1280 
1281 	bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1282 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1283 	bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1284 
1285 	contigfree(sc->sis_ldata, sizeof(struct sis_list_data), M_DEVBUF);
1286 
1287 	splx(s);
1288 
1289 	return(0);
1290 }
1291 
1292 /*
1293  * Initialize the transmit descriptors.
1294  */
1295 static int sis_list_tx_init(sc)
1296 	struct sis_softc	*sc;
1297 {
1298 	struct sis_list_data	*ld;
1299 	struct sis_ring_data	*cd;
1300 	int			i, nexti;
1301 
1302 	cd = &sc->sis_cdata;
1303 	ld = sc->sis_ldata;
1304 
1305 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1306 		nexti = (i == (SIS_TX_LIST_CNT - 1)) ? 0 : i+1;
1307 		ld->sis_tx_list[i].sis_nextdesc =
1308 			    &ld->sis_tx_list[nexti];
1309 		ld->sis_tx_list[i].sis_next =
1310 			    vtophys(&ld->sis_tx_list[nexti]);
1311 		ld->sis_tx_list[i].sis_mbuf = NULL;
1312 		ld->sis_tx_list[i].sis_ptr = 0;
1313 		ld->sis_tx_list[i].sis_ctl = 0;
1314 	}
1315 
1316 	cd->sis_tx_prod = cd->sis_tx_cons = cd->sis_tx_cnt = 0;
1317 
1318 	return(0);
1319 }
1320 
1321 
1322 /*
1323  * Initialize the RX descriptors and allocate mbufs for them. Note that
1324  * we arrange the descriptors in a closed ring, so that the last descriptor
1325  * points back to the first.
1326  */
1327 static int sis_list_rx_init(sc)
1328 	struct sis_softc	*sc;
1329 {
1330 	struct sis_list_data	*ld;
1331 	struct sis_ring_data	*cd;
1332 	int			i, nexti;
1333 
1334 	ld = sc->sis_ldata;
1335 	cd = &sc->sis_cdata;
1336 
1337 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1338 		if (sis_newbuf(sc, &ld->sis_rx_list[i], NULL) == ENOBUFS)
1339 			return(ENOBUFS);
1340 		nexti = (i == (SIS_RX_LIST_CNT - 1)) ? 0 : i+1;
1341 		ld->sis_rx_list[i].sis_nextdesc =
1342 			    &ld->sis_rx_list[nexti];
1343 		ld->sis_rx_list[i].sis_next =
1344 			    vtophys(&ld->sis_rx_list[nexti]);
1345 	}
1346 
1347 	cd->sis_rx_prod = 0;
1348 
1349 	return(0);
1350 }
1351 
1352 /*
1353  * Initialize an RX descriptor and attach an MBUF cluster.
1354  */
1355 static int sis_newbuf(sc, c, m)
1356 	struct sis_softc	*sc;
1357 	struct sis_desc		*c;
1358 	struct mbuf		*m;
1359 {
1360 
1361 	if (m == NULL) {
1362 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1363 		if (m == NULL)
1364 			return(ENOBUFS);
1365 	} else {
1366 		m->m_data = m->m_ext.ext_buf;
1367 	}
1368 
1369 	c->sis_mbuf = m;
1370 	c->sis_ptr = vtophys(mtod(m, caddr_t));
1371 	c->sis_ctl = SIS_RXLEN;
1372 
1373 	return(0);
1374 }
1375 
1376 /*
1377  * A frame has been uploaded: pass the resulting mbuf chain up to
1378  * the higher level protocols.
1379  */
1380 static void sis_rxeof(sc)
1381 	struct sis_softc	*sc;
1382 {
1383         struct mbuf		*m;
1384         struct ifnet		*ifp;
1385 	struct sis_desc		*cur_rx;
1386 	int			i, total_len = 0;
1387 	u_int32_t		rxstat;
1388 
1389 	ifp = &sc->arpcom.ac_if;
1390 	i = sc->sis_cdata.sis_rx_prod;
1391 
1392 	while(SIS_OWNDESC(&sc->sis_ldata->sis_rx_list[i])) {
1393 
1394 #ifdef DEVICE_POLLING
1395 		if (ifp->if_ipending & IFF_POLLING) {
1396 			if (sc->rxcycles <= 0)
1397 				break;
1398 			sc->rxcycles--;
1399 		}
1400 #endif /* DEVICE_POLLING */
1401 		cur_rx = &sc->sis_ldata->sis_rx_list[i];
1402 		rxstat = cur_rx->sis_rxstat;
1403 		m = cur_rx->sis_mbuf;
1404 		cur_rx->sis_mbuf = NULL;
1405 		total_len = SIS_RXBYTES(cur_rx);
1406 		SIS_INC(i, SIS_RX_LIST_CNT);
1407 
1408 		/*
1409 		 * If an error occurs, update stats, clear the
1410 		 * status word and leave the mbuf cluster in place:
1411 		 * it should simply get re-used next time this descriptor
1412 	 	 * comes up in the ring.
1413 		 */
1414 		if (!(rxstat & SIS_CMDSTS_PKT_OK)) {
1415 			ifp->if_ierrors++;
1416 			if (rxstat & SIS_RXSTAT_COLL)
1417 				ifp->if_collisions++;
1418 			sis_newbuf(sc, cur_rx, m);
1419 			continue;
1420 		}
1421 
1422 		/* No errors; receive the packet. */
1423 #ifdef __i386__
1424 		/*
1425 		 * On the x86 we do not have alignment problems, so try to
1426 		 * allocate a new buffer for the receive ring, and pass up
1427 		 * the one where the packet is already, saving the expensive
1428 		 * copy done in m_devget().
1429 		 * If we are on an architecture with alignment problems, or
1430 		 * if the allocation fails, then use m_devget and leave the
1431 		 * existing buffer in the receive ring.
1432 		 */
1433 		if (sis_newbuf(sc, cur_rx, NULL) == 0)
1434 			m->m_pkthdr.len = m->m_len = total_len;
1435 		else
1436 #endif
1437 		{
1438 			struct mbuf *m0;
1439 			m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1440 				total_len + ETHER_ALIGN, 0, ifp, NULL);
1441 			sis_newbuf(sc, cur_rx, m);
1442 			if (m0 == NULL) {
1443 				ifp->if_ierrors++;
1444 				continue;
1445 			}
1446 			m_adj(m0, ETHER_ALIGN);
1447 			m = m0;
1448 		}
1449 
1450 		ifp->if_ipackets++;
1451 		ether_input(ifp, NULL, m);
1452 	}
1453 
1454 	sc->sis_cdata.sis_rx_prod = i;
1455 
1456 	return;
1457 }
1458 
1459 void sis_rxeoc(sc)
1460 	struct sis_softc	*sc;
1461 {
1462 	sis_rxeof(sc);
1463 	/* sis_init(sc); */
1464 	return;
1465 }
1466 
1467 /*
1468  * A frame was downloaded to the chip. It's safe for us to clean up
1469  * the list buffers.
1470  */
1471 
1472 static void sis_txeof(sc)
1473 	struct sis_softc	*sc;
1474 {
1475 	struct sis_desc		*cur_tx = NULL;
1476 	struct ifnet		*ifp;
1477 	u_int32_t		idx;
1478 
1479 	ifp = &sc->arpcom.ac_if;
1480 
1481 	/*
1482 	 * Go through our tx list and free mbufs for those
1483 	 * frames that have been transmitted.
1484 	 */
1485 	idx = sc->sis_cdata.sis_tx_cons;
1486 	while (idx != sc->sis_cdata.sis_tx_prod) {
1487 		cur_tx = &sc->sis_ldata->sis_tx_list[idx];
1488 
1489 		if (SIS_OWNDESC(cur_tx))
1490 			break;
1491 
1492 		if (cur_tx->sis_ctl & SIS_CMDSTS_MORE) {
1493 			sc->sis_cdata.sis_tx_cnt--;
1494 			SIS_INC(idx, SIS_TX_LIST_CNT);
1495 			continue;
1496 		}
1497 
1498 		if (!(cur_tx->sis_ctl & SIS_CMDSTS_PKT_OK)) {
1499 			ifp->if_oerrors++;
1500 			if (cur_tx->sis_txstat & SIS_TXSTAT_EXCESSCOLLS)
1501 				ifp->if_collisions++;
1502 			if (cur_tx->sis_txstat & SIS_TXSTAT_OUTOFWINCOLL)
1503 				ifp->if_collisions++;
1504 		}
1505 
1506 		ifp->if_collisions +=
1507 		    (cur_tx->sis_txstat & SIS_TXSTAT_COLLCNT) >> 16;
1508 
1509 		ifp->if_opackets++;
1510 		if (cur_tx->sis_mbuf != NULL) {
1511 			m_freem(cur_tx->sis_mbuf);
1512 			cur_tx->sis_mbuf = NULL;
1513 		}
1514 
1515 		sc->sis_cdata.sis_tx_cnt--;
1516 		SIS_INC(idx, SIS_TX_LIST_CNT);
1517 		ifp->if_timer = 0;
1518 	}
1519 
1520 	if (idx != sc->sis_cdata.sis_tx_cons) {
1521 		sc->sis_cdata.sis_tx_cons = idx;
1522 		ifp->if_flags &= ~IFF_OACTIVE;
1523 	}
1524 	ifp->if_timer = (sc->sis_cdata.sis_tx_cnt == 0) ? 0 : 5;
1525 	return;
1526 }
1527 
1528 static void sis_tick(xsc)
1529 	void			*xsc;
1530 {
1531 	struct sis_softc	*sc;
1532 	struct mii_data		*mii;
1533 	struct ifnet		*ifp;
1534 	int			s;
1535 
1536 	s = splimp();
1537 
1538 	sc = xsc;
1539 	ifp = &sc->arpcom.ac_if;
1540 
1541 	mii = device_get_softc(sc->sis_miibus);
1542 	mii_tick(mii);
1543 
1544 	if (!sc->sis_link) {
1545 		mii_pollstat(mii);
1546 		if (mii->mii_media_status & IFM_ACTIVE &&
1547 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
1548 			sc->sis_link++;
1549 			if (ifp->if_snd.ifq_head != NULL)
1550 				sis_start(ifp);
1551 	}
1552 
1553 	sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1554 
1555 	splx(s);
1556 
1557 	return;
1558 }
1559 
1560 #ifdef DEVICE_POLLING
1561 static poll_handler_t sis_poll;
1562 
1563 static void
1564 sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1565 {
1566 	struct  sis_softc *sc = ifp->if_softc;
1567 
1568 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1569 		CSR_WRITE_4(sc, SIS_IER, 1);
1570 		return;
1571 	}
1572 
1573 	/*
1574 	 * On the sis, reading the status register also clears it.
1575 	 * So before returning to intr mode we must make sure that all
1576 	 * possible pending sources of interrupts have been served.
1577 	 * In practice this means run to completion the *eof routines,
1578 	 * and then call the interrupt routine
1579 	 */
1580 	sc->rxcycles = count;
1581 	sis_rxeof(sc);
1582 	sis_txeof(sc);
1583 	if (ifp->if_snd.ifq_head != NULL)
1584 		sis_start(ifp);
1585 
1586 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1587 		u_int32_t	status;
1588 
1589 		/* Reading the ISR register clears all interrupts. */
1590 		status = CSR_READ_4(sc, SIS_ISR);
1591 
1592 		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1593 			sis_rxeoc(sc);
1594 
1595 		if (status & (SIS_ISR_RX_IDLE))
1596 			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1597 
1598 		if (status & SIS_ISR_SYSERR) {
1599 			sis_reset(sc);
1600 			sis_init(sc);
1601 		}
1602 	}
1603 }
1604 #endif /* DEVICE_POLLING */
1605 
1606 static void sis_intr(arg)
1607 	void			*arg;
1608 {
1609 	struct sis_softc	*sc;
1610 	struct ifnet		*ifp;
1611 	u_int32_t		status;
1612 
1613 	sc = arg;
1614 	ifp = &sc->arpcom.ac_if;
1615 
1616 #ifdef DEVICE_POLLING
1617 	if (ifp->if_ipending & IFF_POLLING)
1618 		return;
1619 	if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */
1620 		CSR_WRITE_4(sc, SIS_IER, 0);
1621 		sis_poll(ifp, 0, 1);
1622 		return;
1623 	}
1624 #endif /* DEVICE_POLLING */
1625 
1626 	/* Supress unwanted interrupts */
1627 	if (!(ifp->if_flags & IFF_UP)) {
1628 		sis_stop(sc);
1629 		return;
1630 	}
1631 
1632 	/* Disable interrupts. */
1633 	CSR_WRITE_4(sc, SIS_IER, 0);
1634 
1635 	for (;;) {
1636 		/* Reading the ISR register clears all interrupts. */
1637 		status = CSR_READ_4(sc, SIS_ISR);
1638 
1639 		if ((status & SIS_INTRS) == 0)
1640 			break;
1641 
1642 		if (status &
1643 		    (SIS_ISR_TX_DESC_OK|SIS_ISR_TX_ERR|
1644 		     SIS_ISR_TX_OK|SIS_ISR_TX_IDLE) )
1645 			sis_txeof(sc);
1646 
1647 		if (status & (SIS_ISR_RX_DESC_OK|SIS_ISR_RX_OK|SIS_ISR_RX_IDLE))
1648 			sis_rxeof(sc);
1649 
1650 		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1651 			sis_rxeoc(sc);
1652 
1653 		if (status & (SIS_ISR_RX_IDLE))
1654 			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1655 
1656 		if (status & SIS_ISR_SYSERR) {
1657 			sis_reset(sc);
1658 			sis_init(sc);
1659 		}
1660 	}
1661 
1662 	/* Re-enable interrupts. */
1663 	CSR_WRITE_4(sc, SIS_IER, 1);
1664 
1665 	if (ifp->if_snd.ifq_head != NULL)
1666 		sis_start(ifp);
1667 
1668 	return;
1669 }
1670 
1671 /*
1672  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1673  * pointers to the fragment pointers.
1674  */
1675 static int sis_encap(sc, m_head, txidx)
1676 	struct sis_softc	*sc;
1677 	struct mbuf		*m_head;
1678 	u_int32_t		*txidx;
1679 {
1680 	struct sis_desc		*f = NULL;
1681 	struct mbuf		*m;
1682 	int			frag, cur, cnt = 0;
1683 
1684 	/*
1685  	 * Start packing the mbufs in this chain into
1686 	 * the fragment pointers. Stop when we run out
1687  	 * of fragments or hit the end of the mbuf chain.
1688 	 */
1689 	m = m_head;
1690 	cur = frag = *txidx;
1691 
1692 	for (m = m_head; m != NULL; m = m->m_next) {
1693 		if (m->m_len != 0) {
1694 			if ((SIS_TX_LIST_CNT -
1695 			    (sc->sis_cdata.sis_tx_cnt + cnt)) < 2)
1696 				return(ENOBUFS);
1697 			f = &sc->sis_ldata->sis_tx_list[frag];
1698 			f->sis_ctl = SIS_CMDSTS_MORE | m->m_len;
1699 			f->sis_ptr = vtophys(mtod(m, vm_offset_t));
1700 			if (cnt != 0)
1701 				f->sis_ctl |= SIS_CMDSTS_OWN;
1702 			cur = frag;
1703 			SIS_INC(frag, SIS_TX_LIST_CNT);
1704 			cnt++;
1705 		}
1706 	}
1707 
1708 	if (m != NULL)
1709 		return(ENOBUFS);
1710 
1711 	sc->sis_ldata->sis_tx_list[cur].sis_mbuf = m_head;
1712 	sc->sis_ldata->sis_tx_list[cur].sis_ctl &= ~SIS_CMDSTS_MORE;
1713 	sc->sis_ldata->sis_tx_list[*txidx].sis_ctl |= SIS_CMDSTS_OWN;
1714 	sc->sis_cdata.sis_tx_cnt += cnt;
1715 	*txidx = frag;
1716 
1717 	return(0);
1718 }
1719 
1720 /*
1721  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1722  * to the mbuf data regions directly in the transmit lists. We also save a
1723  * copy of the pointers since the transmit list fragment pointers are
1724  * physical addresses.
1725  */
1726 
1727 static void sis_start(ifp)
1728 	struct ifnet		*ifp;
1729 {
1730 	struct sis_softc	*sc;
1731 	struct mbuf		*m_head = NULL;
1732 	u_int32_t		idx;
1733 
1734 	sc = ifp->if_softc;
1735 
1736 	if (!sc->sis_link)
1737 		return;
1738 
1739 	idx = sc->sis_cdata.sis_tx_prod;
1740 
1741 	if (ifp->if_flags & IFF_OACTIVE)
1742 		return;
1743 
1744 	while(sc->sis_ldata->sis_tx_list[idx].sis_mbuf == NULL) {
1745 		IF_DEQUEUE(&ifp->if_snd, m_head);
1746 		if (m_head == NULL)
1747 			break;
1748 
1749 		if (sis_encap(sc, m_head, &idx)) {
1750 			IF_PREPEND(&ifp->if_snd, m_head);
1751 			ifp->if_flags |= IFF_OACTIVE;
1752 			break;
1753 		}
1754 
1755 		/*
1756 		 * If there's a BPF listener, bounce a copy of this frame
1757 		 * to him.
1758 		 */
1759 		if (ifp->if_bpf)
1760 			bpf_mtap(ifp, m_head);
1761 
1762 	}
1763 
1764 	/* Transmit */
1765 	sc->sis_cdata.sis_tx_prod = idx;
1766 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
1767 
1768 	/*
1769 	 * Set a timeout in case the chip goes out to lunch.
1770 	 */
1771 	ifp->if_timer = 5;
1772 
1773 	return;
1774 }
1775 
1776 static void sis_init(xsc)
1777 	void			*xsc;
1778 {
1779 	struct sis_softc	*sc = xsc;
1780 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1781 	struct mii_data		*mii;
1782 	int			s;
1783 
1784 	s = splimp();
1785 
1786 	/*
1787 	 * Cancel pending I/O and free all RX/TX buffers.
1788 	 */
1789 	sis_stop(sc);
1790 
1791 	mii = device_get_softc(sc->sis_miibus);
1792 
1793 	/* Set MAC address */
1794 	if (sc->sis_type == SIS_TYPE_83815) {
1795 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
1796 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1797 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1798 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
1799 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1800 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1801 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
1802 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1803 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1804 	} else {
1805 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
1806 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1807 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1808 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
1809 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1810 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1811 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
1812 		CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1813 		    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1814 	}
1815 
1816 	/* Init circular RX list. */
1817 	if (sis_list_rx_init(sc) == ENOBUFS) {
1818 		printf("sis%d: initialization failed: no "
1819 			"memory for rx buffers\n", sc->sis_unit);
1820 		sis_stop(sc);
1821 		(void)splx(s);
1822 		return;
1823 	}
1824 
1825 	/*
1826 	 * Init tx descriptors.
1827 	 */
1828 	sis_list_tx_init(sc);
1829 
1830 	/*
1831 	 * For the NatSemi chip, we have to explicitly enable the
1832 	 * reception of ARP frames, as well as turn on the 'perfect
1833 	 * match' filter where we store the station address, otherwise
1834 	 * we won't receive unicasts meant for this host.
1835 	 */
1836 	if (sc->sis_type == SIS_TYPE_83815) {
1837 		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
1838 		SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
1839 	}
1840 
1841 	 /* If we want promiscuous mode, set the allframes bit. */
1842 	if (ifp->if_flags & IFF_PROMISC) {
1843 		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1844 	} else {
1845 		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1846 	}
1847 
1848 	/*
1849 	 * Set the capture broadcast bit to capture broadcast frames.
1850 	 */
1851 	if (ifp->if_flags & IFF_BROADCAST) {
1852 		SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1853 	} else {
1854 		SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1855 	}
1856 
1857 	/*
1858 	 * Load the multicast filter.
1859 	 */
1860 	if (sc->sis_type == SIS_TYPE_83815)
1861 		sis_setmulti_ns(sc);
1862 	else
1863 		sis_setmulti_sis(sc);
1864 
1865 	/* Turn the receive filter on */
1866 	SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
1867 
1868 	/*
1869 	 * Load the address of the RX and TX lists.
1870 	 */
1871 	CSR_WRITE_4(sc, SIS_RX_LISTPTR,
1872 	    vtophys(&sc->sis_ldata->sis_rx_list[0]));
1873 	CSR_WRITE_4(sc, SIS_TX_LISTPTR,
1874 	    vtophys(&sc->sis_ldata->sis_tx_list[0]));
1875 
1876 	/* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
1877 	 * the PCI bus. When this bit is set, the Max DMA Burst Size
1878 	 * for TX/RX DMA should be no larger than 16 double words.
1879 	 */
1880 	if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
1881 		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
1882 	} else {
1883 		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
1884 	}
1885 
1886 	/* Accept Long Packets for VLAN support */
1887 	SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
1888 
1889 	/* Set TX configuration */
1890 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
1891 		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
1892 	} else {
1893 		CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
1894 	}
1895 
1896 	/* Set full/half duplex mode. */
1897 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1898 		SIS_SETBIT(sc, SIS_TX_CFG,
1899 		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1900 		SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1901 	} else {
1902 		SIS_CLRBIT(sc, SIS_TX_CFG,
1903 		    (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1904 		SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1905 	}
1906 
1907 	/*
1908 	 * Enable interrupts.
1909 	 */
1910 	CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
1911 #ifdef DEVICE_POLLING
1912 	/*
1913 	 * ... only enable interrupts if we are not polling, make sure
1914 	 * they are off otherwise.
1915 	 */
1916 	if (ifp->if_ipending & IFF_POLLING)
1917 		CSR_WRITE_4(sc, SIS_IER, 0);
1918 	else
1919 #endif /* DEVICE_POLLING */
1920 	CSR_WRITE_4(sc, SIS_IER, 1);
1921 
1922 	/* Enable receiver and transmitter. */
1923 	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
1924 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1925 
1926 #ifdef notdef
1927 	mii_mediachg(mii);
1928 #endif
1929 
1930 	/*
1931 	 * Page 75 of the DP83815 manual recommends the
1932 	 * following register settings "for optimum
1933 	 * performance." Note however that at least three
1934 	 * of the registers are listed as "reserved" in
1935 	 * the register map, so who knows what they do.
1936 	 */
1937 	if (sc->sis_type == SIS_TYPE_83815) {
1938 		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
1939 		CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
1940 		CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
1941 		CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
1942 		CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
1943 	}
1944 
1945 	ifp->if_flags |= IFF_RUNNING;
1946 	ifp->if_flags &= ~IFF_OACTIVE;
1947 
1948 	(void)splx(s);
1949 
1950 	sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1951 
1952 	return;
1953 }
1954 
1955 /*
1956  * Set media options.
1957  */
1958 static int sis_ifmedia_upd(ifp)
1959 	struct ifnet		*ifp;
1960 {
1961 	struct sis_softc	*sc;
1962 	struct mii_data		*mii;
1963 
1964 	sc = ifp->if_softc;
1965 
1966 	mii = device_get_softc(sc->sis_miibus);
1967 	sc->sis_link = 0;
1968 	if (mii->mii_instance) {
1969 		struct mii_softc	*miisc;
1970 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1971 		    miisc = LIST_NEXT(miisc, mii_list))
1972 			mii_phy_reset(miisc);
1973 	}
1974 	mii_mediachg(mii);
1975 
1976 	return(0);
1977 }
1978 
1979 /*
1980  * Report current media status.
1981  */
1982 static void sis_ifmedia_sts(ifp, ifmr)
1983 	struct ifnet		*ifp;
1984 	struct ifmediareq	*ifmr;
1985 {
1986 	struct sis_softc	*sc;
1987 	struct mii_data		*mii;
1988 
1989 	sc = ifp->if_softc;
1990 
1991 	mii = device_get_softc(sc->sis_miibus);
1992 	mii_pollstat(mii);
1993 	ifmr->ifm_active = mii->mii_media_active;
1994 	ifmr->ifm_status = mii->mii_media_status;
1995 
1996 	return;
1997 }
1998 
1999 static int sis_ioctl(ifp, command, data)
2000 	struct ifnet		*ifp;
2001 	u_long			command;
2002 	caddr_t			data;
2003 {
2004 	struct sis_softc	*sc = ifp->if_softc;
2005 	struct ifreq		*ifr = (struct ifreq *) data;
2006 	struct mii_data		*mii;
2007 	int			s, error = 0;
2008 
2009 	s = splimp();
2010 
2011 	switch(command) {
2012 	case SIOCSIFADDR:
2013 	case SIOCGIFADDR:
2014 	case SIOCSIFMTU:
2015 		error = ether_ioctl(ifp, command, data);
2016 		break;
2017 	case SIOCSIFFLAGS:
2018 		if (ifp->if_flags & IFF_UP) {
2019 			sis_init(sc);
2020 		} else {
2021 			if (ifp->if_flags & IFF_RUNNING)
2022 				sis_stop(sc);
2023 		}
2024 		error = 0;
2025 		break;
2026 	case SIOCADDMULTI:
2027 	case SIOCDELMULTI:
2028 		if (sc->sis_type == SIS_TYPE_83815)
2029 			sis_setmulti_ns(sc);
2030 		else
2031 			sis_setmulti_sis(sc);
2032 		error = 0;
2033 		break;
2034 	case SIOCGIFMEDIA:
2035 	case SIOCSIFMEDIA:
2036 		mii = device_get_softc(sc->sis_miibus);
2037 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2038 		break;
2039 	default:
2040 		error = EINVAL;
2041 		break;
2042 	}
2043 
2044 	(void)splx(s);
2045 
2046 	return(error);
2047 }
2048 
2049 static void sis_watchdog(ifp)
2050 	struct ifnet		*ifp;
2051 {
2052 	struct sis_softc	*sc;
2053 
2054 	sc = ifp->if_softc;
2055 
2056 	ifp->if_oerrors++;
2057 	printf("sis%d: watchdog timeout\n", sc->sis_unit);
2058 
2059 	sis_stop(sc);
2060 	sis_reset(sc);
2061 	sis_init(sc);
2062 
2063 	if (ifp->if_snd.ifq_head != NULL)
2064 		sis_start(ifp);
2065 
2066 	return;
2067 }
2068 
2069 /*
2070  * Stop the adapter and free any mbufs allocated to the
2071  * RX and TX lists.
2072  */
2073 static void sis_stop(sc)
2074 	struct sis_softc	*sc;
2075 {
2076 	int		i;
2077 	struct ifnet		*ifp;
2078 
2079 	ifp = &sc->arpcom.ac_if;
2080 	ifp->if_timer = 0;
2081 
2082 	untimeout(sis_tick, sc, sc->sis_stat_ch);
2083 
2084 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2085 #ifdef DEVICE_POLLING
2086 	ether_poll_deregister(ifp);
2087 #endif
2088 	CSR_WRITE_4(sc, SIS_IER, 0);
2089 	CSR_WRITE_4(sc, SIS_IMR, 0);
2090 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
2091 	DELAY(1000);
2092 	CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
2093 	CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2094 
2095 	sc->sis_link = 0;
2096 
2097 	/*
2098 	 * Free data in the RX lists.
2099 	 */
2100 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2101 		if (sc->sis_ldata->sis_rx_list[i].sis_mbuf != NULL) {
2102 			m_freem(sc->sis_ldata->sis_rx_list[i].sis_mbuf);
2103 			sc->sis_ldata->sis_rx_list[i].sis_mbuf = NULL;
2104 		}
2105 	}
2106 	bzero((char *)&sc->sis_ldata->sis_rx_list,
2107 		sizeof(sc->sis_ldata->sis_rx_list));
2108 
2109 	/*
2110 	 * Free the TX list buffers.
2111 	 */
2112 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2113 		if (sc->sis_ldata->sis_tx_list[i].sis_mbuf != NULL) {
2114 			m_freem(sc->sis_ldata->sis_tx_list[i].sis_mbuf);
2115 			sc->sis_ldata->sis_tx_list[i].sis_mbuf = NULL;
2116 		}
2117 	}
2118 
2119 	bzero((char *)&sc->sis_ldata->sis_tx_list,
2120 		sizeof(sc->sis_ldata->sis_tx_list));
2121 
2122 	return;
2123 }
2124 
2125 /*
2126  * Stop all chip I/O so that the kernel's probe routines don't
2127  * get confused by errant DMAs when rebooting.
2128  */
2129 static void sis_shutdown(dev)
2130 	device_t		dev;
2131 {
2132 	struct sis_softc	*sc;
2133 
2134 	sc = device_get_softc(dev);
2135 
2136 	sis_reset(sc);
2137 	sis_stop(sc);
2138 
2139 	return;
2140 }
2141