xref: /freebsd/sys/dev/sis/if_sis.c (revision db3cb3640f547c063293e9fdc4db69e9dc120951)
1 /*-
2  * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
3  * Copyright (c) 1997, 1998, 1999
4  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
39  * available from http://www.sis.com.tw.
40  *
41  * This driver also supports the NatSemi DP83815. Datasheets are
42  * available from http://www.national.com.
43  *
44  * Written by Bill Paul <wpaul@ee.columbia.edu>
45  * Electrical Engineering Department
46  * Columbia University, New York City
47  */
48 /*
49  * The SiS 900 is a fairly simple chip. It uses bus master DMA with
50  * simple TX and RX descriptors of 3 longwords in size. The receiver
51  * has a single perfect filter entry for the station address and a
52  * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
53  * transceiver while the 7016 requires an external transceiver chip.
54  * Both chips offer the standard bit-bang MII interface as well as
55  * an enchanced PHY interface which simplifies accessing MII registers.
56  *
57  * The only downside to this chipset is that RX descriptors must be
58  * longword aligned.
59  */
60 
61 #ifdef HAVE_KERNEL_OPTION_HEADERS
62 #include "opt_device_polling.h"
63 #endif
64 
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/bus.h>
68 #include <sys/endian.h>
69 #include <sys/kernel.h>
70 #include <sys/lock.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/module.h>
74 #include <sys/socket.h>
75 #include <sys/sockio.h>
76 #include <sys/sysctl.h>
77 
78 #include <net/if.h>
79 #include <net/if_var.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_types.h>
85 #include <net/if_vlan_var.h>
86 
87 #include <net/bpf.h>
88 
89 #include <machine/bus.h>
90 #include <machine/resource.h>
91 #include <sys/rman.h>
92 
93 #include <dev/mii/mii.h>
94 #include <dev/mii/mii_bitbang.h>
95 #include <dev/mii/miivar.h>
96 
97 #include <dev/pci/pcireg.h>
98 #include <dev/pci/pcivar.h>
99 
100 #define SIS_USEIOSPACE
101 
102 #include <dev/sis/if_sisreg.h>
103 
104 MODULE_DEPEND(sis, pci, 1, 1, 1);
105 MODULE_DEPEND(sis, ether, 1, 1, 1);
106 MODULE_DEPEND(sis, miibus, 1, 1, 1);
107 
108 /* "device miibus" required.  See GENERIC if you get errors here. */
109 #include "miibus_if.h"
110 
111 #define	SIS_LOCK(_sc)		mtx_lock(&(_sc)->sis_mtx)
112 #define	SIS_UNLOCK(_sc)		mtx_unlock(&(_sc)->sis_mtx)
113 #define	SIS_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->sis_mtx, MA_OWNED)
114 
115 /*
116  * register space access macros
117  */
118 #define CSR_WRITE_4(sc, reg, val)	bus_write_4(sc->sis_res[0], reg, val)
119 
120 #define CSR_READ_4(sc, reg)		bus_read_4(sc->sis_res[0], reg)
121 
122 #define CSR_READ_2(sc, reg)		bus_read_2(sc->sis_res[0], reg)
123 
124 #define	CSR_BARRIER(sc, reg, length, flags)				\
125 	bus_barrier(sc->sis_res[0], reg, length, flags)
126 
127 /*
128  * Various supported device vendors/types and their names.
129  */
130 static const struct sis_type sis_devs[] = {
131 	{ SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
132 	{ SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
133 	{ NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP8381[56] 10/100BaseTX" },
134 	{ 0, 0, NULL }
135 };
136 
137 static int sis_detach(device_t);
138 static __inline void sis_discard_rxbuf(struct sis_rxdesc *);
139 static int sis_dma_alloc(struct sis_softc *);
140 static void sis_dma_free(struct sis_softc *);
141 static int sis_dma_ring_alloc(struct sis_softc *, bus_size_t, bus_size_t,
142     bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
143 static void sis_dmamap_cb(void *, bus_dma_segment_t *, int, int);
144 #ifndef __NO_STRICT_ALIGNMENT
145 static __inline void sis_fixup_rx(struct mbuf *);
146 #endif
147 static void sis_ifmedia_sts(struct ifnet *, struct ifmediareq *);
148 static int sis_ifmedia_upd(struct ifnet *);
149 static void sis_init(void *);
150 static void sis_initl(struct sis_softc *);
151 static void sis_intr(void *);
152 static int sis_ioctl(struct ifnet *, u_long, caddr_t);
153 static uint32_t sis_mii_bitbang_read(device_t);
154 static void sis_mii_bitbang_write(device_t, uint32_t);
155 static int sis_newbuf(struct sis_softc *, struct sis_rxdesc *);
156 static int sis_resume(device_t);
157 static int sis_rxeof(struct sis_softc *);
158 static void sis_rxfilter(struct sis_softc *);
159 static void sis_rxfilter_ns(struct sis_softc *);
160 static void sis_rxfilter_sis(struct sis_softc *);
161 static void sis_start(struct ifnet *);
162 static void sis_startl(struct ifnet *);
163 static void sis_stop(struct sis_softc *);
164 static int sis_suspend(device_t);
165 static void sis_add_sysctls(struct sis_softc *);
166 static void sis_watchdog(struct sis_softc *);
167 static void sis_wol(struct sis_softc *);
168 
169 /*
170  * MII bit-bang glue
171  */
172 static const struct mii_bitbang_ops sis_mii_bitbang_ops = {
173 	sis_mii_bitbang_read,
174 	sis_mii_bitbang_write,
175 	{
176 		SIS_MII_DATA,		/* MII_BIT_MDO */
177 		SIS_MII_DATA,		/* MII_BIT_MDI */
178 		SIS_MII_CLK,		/* MII_BIT_MDC */
179 		SIS_MII_DIR,		/* MII_BIT_DIR_HOST_PHY */
180 		0,			/* MII_BIT_DIR_PHY_HOST */
181 	}
182 };
183 
184 static struct resource_spec sis_res_spec[] = {
185 #ifdef SIS_USEIOSPACE
186 	{ SYS_RES_IOPORT,	SIS_PCI_LOIO,	RF_ACTIVE},
187 #else
188 	{ SYS_RES_MEMORY,	SIS_PCI_LOMEM,	RF_ACTIVE},
189 #endif
190 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE},
191 	{ -1, 0 }
192 };
193 
194 #define SIS_SETBIT(sc, reg, x)				\
195 	CSR_WRITE_4(sc, reg,				\
196 		CSR_READ_4(sc, reg) | (x))
197 
198 #define SIS_CLRBIT(sc, reg, x)				\
199 	CSR_WRITE_4(sc, reg,				\
200 		CSR_READ_4(sc, reg) & ~(x))
201 
202 #define SIO_SET(x)					\
203 	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
204 
205 #define SIO_CLR(x)					\
206 	CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
207 
208 /*
209  * Routine to reverse the bits in a word. Stolen almost
210  * verbatim from /usr/games/fortune.
211  */
212 static uint16_t
213 sis_reverse(uint16_t n)
214 {
215 	n = ((n >>  1) & 0x5555) | ((n <<  1) & 0xaaaa);
216 	n = ((n >>  2) & 0x3333) | ((n <<  2) & 0xcccc);
217 	n = ((n >>  4) & 0x0f0f) | ((n <<  4) & 0xf0f0);
218 	n = ((n >>  8) & 0x00ff) | ((n <<  8) & 0xff00);
219 
220 	return (n);
221 }
222 
223 static void
224 sis_delay(struct sis_softc *sc)
225 {
226 	int			idx;
227 
228 	for (idx = (300 / 33) + 1; idx > 0; idx--)
229 		CSR_READ_4(sc, SIS_CSR);
230 }
231 
232 static void
233 sis_eeprom_idle(struct sis_softc *sc)
234 {
235 	int		i;
236 
237 	SIO_SET(SIS_EECTL_CSEL);
238 	sis_delay(sc);
239 	SIO_SET(SIS_EECTL_CLK);
240 	sis_delay(sc);
241 
242 	for (i = 0; i < 25; i++) {
243 		SIO_CLR(SIS_EECTL_CLK);
244 		sis_delay(sc);
245 		SIO_SET(SIS_EECTL_CLK);
246 		sis_delay(sc);
247 	}
248 
249 	SIO_CLR(SIS_EECTL_CLK);
250 	sis_delay(sc);
251 	SIO_CLR(SIS_EECTL_CSEL);
252 	sis_delay(sc);
253 	CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
254 }
255 
256 /*
257  * Send a read command and address to the EEPROM, check for ACK.
258  */
259 static void
260 sis_eeprom_putbyte(struct sis_softc *sc, int addr)
261 {
262 	int		d, i;
263 
264 	d = addr | SIS_EECMD_READ;
265 
266 	/*
267 	 * Feed in each bit and stobe the clock.
268 	 */
269 	for (i = 0x400; i; i >>= 1) {
270 		if (d & i) {
271 			SIO_SET(SIS_EECTL_DIN);
272 		} else {
273 			SIO_CLR(SIS_EECTL_DIN);
274 		}
275 		sis_delay(sc);
276 		SIO_SET(SIS_EECTL_CLK);
277 		sis_delay(sc);
278 		SIO_CLR(SIS_EECTL_CLK);
279 		sis_delay(sc);
280 	}
281 }
282 
283 /*
284  * Read a word of data stored in the EEPROM at address 'addr.'
285  */
286 static void
287 sis_eeprom_getword(struct sis_softc *sc, int addr, uint16_t *dest)
288 {
289 	int		i;
290 	uint16_t	word = 0;
291 
292 	/* Force EEPROM to idle state. */
293 	sis_eeprom_idle(sc);
294 
295 	/* Enter EEPROM access mode. */
296 	sis_delay(sc);
297 	SIO_CLR(SIS_EECTL_CLK);
298 	sis_delay(sc);
299 	SIO_SET(SIS_EECTL_CSEL);
300 	sis_delay(sc);
301 
302 	/*
303 	 * Send address of word we want to read.
304 	 */
305 	sis_eeprom_putbyte(sc, addr);
306 
307 	/*
308 	 * Start reading bits from EEPROM.
309 	 */
310 	for (i = 0x8000; i; i >>= 1) {
311 		SIO_SET(SIS_EECTL_CLK);
312 		sis_delay(sc);
313 		if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
314 			word |= i;
315 		sis_delay(sc);
316 		SIO_CLR(SIS_EECTL_CLK);
317 		sis_delay(sc);
318 	}
319 
320 	/* Turn off EEPROM access mode. */
321 	sis_eeprom_idle(sc);
322 
323 	*dest = word;
324 }
325 
326 /*
327  * Read a sequence of words from the EEPROM.
328  */
329 static void
330 sis_read_eeprom(struct sis_softc *sc, caddr_t dest, int off, int cnt, int swap)
331 {
332 	int			i;
333 	uint16_t		word = 0, *ptr;
334 
335 	for (i = 0; i < cnt; i++) {
336 		sis_eeprom_getword(sc, off + i, &word);
337 		ptr = (uint16_t *)(dest + (i * 2));
338 		if (swap)
339 			*ptr = ntohs(word);
340 		else
341 			*ptr = word;
342 	}
343 }
344 
345 #if defined(__i386__) || defined(__amd64__)
346 static device_t
347 sis_find_bridge(device_t dev)
348 {
349 	devclass_t		pci_devclass;
350 	device_t		*pci_devices;
351 	int			pci_count = 0;
352 	device_t		*pci_children;
353 	int			pci_childcount = 0;
354 	device_t		*busp, *childp;
355 	device_t		child = NULL;
356 	int			i, j;
357 
358 	if ((pci_devclass = devclass_find("pci")) == NULL)
359 		return (NULL);
360 
361 	devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
362 
363 	for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
364 		if (device_get_children(*busp, &pci_children, &pci_childcount))
365 			continue;
366 		for (j = 0, childp = pci_children;
367 		    j < pci_childcount; j++, childp++) {
368 			if (pci_get_vendor(*childp) == SIS_VENDORID &&
369 			    pci_get_device(*childp) == 0x0008) {
370 				child = *childp;
371 				free(pci_children, M_TEMP);
372 				goto done;
373 			}
374 		}
375 		free(pci_children, M_TEMP);
376 	}
377 
378 done:
379 	free(pci_devices, M_TEMP);
380 	return (child);
381 }
382 
383 static void
384 sis_read_cmos(struct sis_softc *sc, device_t dev, caddr_t dest, int off, int cnt)
385 {
386 	device_t		bridge;
387 	uint8_t			reg;
388 	int			i;
389 	bus_space_tag_t		btag;
390 
391 	bridge = sis_find_bridge(dev);
392 	if (bridge == NULL)
393 		return;
394 	reg = pci_read_config(bridge, 0x48, 1);
395 	pci_write_config(bridge, 0x48, reg|0x40, 1);
396 
397 	/* XXX */
398 #if defined(__amd64__) || defined(__i386__)
399 	btag = X86_BUS_SPACE_IO;
400 #endif
401 
402 	for (i = 0; i < cnt; i++) {
403 		bus_space_write_1(btag, 0x0, 0x70, i + off);
404 		*(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
405 	}
406 
407 	pci_write_config(bridge, 0x48, reg & ~0x40, 1);
408 }
409 
410 static void
411 sis_read_mac(struct sis_softc *sc, device_t dev, caddr_t dest)
412 {
413 	uint32_t		filtsave, csrsave;
414 
415 	filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
416 	csrsave = CSR_READ_4(sc, SIS_CSR);
417 
418 	CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
419 	CSR_WRITE_4(sc, SIS_CSR, 0);
420 
421 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
422 
423 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
424 	((uint16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
425 	CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
426 	((uint16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
427 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
428 	((uint16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
429 
430 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
431 	CSR_WRITE_4(sc, SIS_CSR, csrsave);
432 }
433 #endif
434 
435 /*
436  * Read the MII serial port for the MII bit-bang module.
437  */
438 static uint32_t
439 sis_mii_bitbang_read(device_t dev)
440 {
441 	struct sis_softc	*sc;
442 	uint32_t		val;
443 
444 	sc = device_get_softc(dev);
445 
446 	val = CSR_READ_4(sc, SIS_EECTL);
447 	CSR_BARRIER(sc, SIS_EECTL, 4,
448 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
449 	return (val);
450 }
451 
452 /*
453  * Write the MII serial port for the MII bit-bang module.
454  */
455 static void
456 sis_mii_bitbang_write(device_t dev, uint32_t val)
457 {
458 	struct sis_softc	*sc;
459 
460 	sc = device_get_softc(dev);
461 
462 	CSR_WRITE_4(sc, SIS_EECTL, val);
463 	CSR_BARRIER(sc, SIS_EECTL, 4,
464 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
465 }
466 
467 static int
468 sis_miibus_readreg(device_t dev, int phy, int reg)
469 {
470 	struct sis_softc	*sc;
471 
472 	sc = device_get_softc(dev);
473 
474 	if (sc->sis_type == SIS_TYPE_83815) {
475 		if (phy != 0)
476 			return (0);
477 		/*
478 		 * The NatSemi chip can take a while after
479 		 * a reset to come ready, during which the BMSR
480 		 * returns a value of 0. This is *never* supposed
481 		 * to happen: some of the BMSR bits are meant to
482 		 * be hardwired in the on position, and this can
483 		 * confuse the miibus code a bit during the probe
484 		 * and attach phase. So we make an effort to check
485 		 * for this condition and wait for it to clear.
486 		 */
487 		if (!CSR_READ_4(sc, NS_BMSR))
488 			DELAY(1000);
489 		return CSR_READ_4(sc, NS_BMCR + (reg * 4));
490 	}
491 
492 	/*
493 	 * Chipsets < SIS_635 seem not to be able to read/write
494 	 * through mdio. Use the enhanced PHY access register
495 	 * again for them.
496 	 */
497 	if (sc->sis_type == SIS_TYPE_900 &&
498 	    sc->sis_rev < SIS_REV_635) {
499 		int i, val = 0;
500 
501 		if (phy != 0)
502 			return (0);
503 
504 		CSR_WRITE_4(sc, SIS_PHYCTL,
505 		    (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
506 		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
507 
508 		for (i = 0; i < SIS_TIMEOUT; i++) {
509 			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
510 				break;
511 		}
512 
513 		if (i == SIS_TIMEOUT) {
514 			device_printf(sc->sis_dev,
515 			    "PHY failed to come ready\n");
516 			return (0);
517 		}
518 
519 		val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
520 
521 		if (val == 0xFFFF)
522 			return (0);
523 
524 		return (val);
525 	} else
526 		return (mii_bitbang_readreg(dev, &sis_mii_bitbang_ops, phy,
527 		    reg));
528 }
529 
530 static int
531 sis_miibus_writereg(device_t dev, int phy, int reg, int data)
532 {
533 	struct sis_softc	*sc;
534 
535 	sc = device_get_softc(dev);
536 
537 	if (sc->sis_type == SIS_TYPE_83815) {
538 		if (phy != 0)
539 			return (0);
540 		CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
541 		return (0);
542 	}
543 
544 	/*
545 	 * Chipsets < SIS_635 seem not to be able to read/write
546 	 * through mdio. Use the enhanced PHY access register
547 	 * again for them.
548 	 */
549 	if (sc->sis_type == SIS_TYPE_900 &&
550 	    sc->sis_rev < SIS_REV_635) {
551 		int i;
552 
553 		if (phy != 0)
554 			return (0);
555 
556 		CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
557 		    (reg << 6) | SIS_PHYOP_WRITE);
558 		SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
559 
560 		for (i = 0; i < SIS_TIMEOUT; i++) {
561 			if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
562 				break;
563 		}
564 
565 		if (i == SIS_TIMEOUT)
566 			device_printf(sc->sis_dev,
567 			    "PHY failed to come ready\n");
568 	} else
569 		mii_bitbang_writereg(dev, &sis_mii_bitbang_ops, phy, reg,
570 		    data);
571 	return (0);
572 }
573 
574 static void
575 sis_miibus_statchg(device_t dev)
576 {
577 	struct sis_softc	*sc;
578 	struct mii_data		*mii;
579 	struct ifnet		*ifp;
580 	uint32_t		reg;
581 
582 	sc = device_get_softc(dev);
583 	SIS_LOCK_ASSERT(sc);
584 
585 	mii = device_get_softc(sc->sis_miibus);
586 	ifp = sc->sis_ifp;
587 	if (mii == NULL || ifp == NULL ||
588 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
589 		return;
590 
591 	sc->sis_flags &= ~SIS_FLAG_LINK;
592 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
593 	    (IFM_ACTIVE | IFM_AVALID)) {
594 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
595 		case IFM_10_T:
596 			CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
597 			sc->sis_flags |= SIS_FLAG_LINK;
598 			break;
599 		case IFM_100_TX:
600 			CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
601 			sc->sis_flags |= SIS_FLAG_LINK;
602 			break;
603 		default:
604 			break;
605 		}
606 	}
607 
608 	if ((sc->sis_flags & SIS_FLAG_LINK) == 0) {
609 		/*
610 		 * Stopping MACs seem to reset SIS_TX_LISTPTR and
611 		 * SIS_RX_LISTPTR which in turn requires resetting
612 		 * TX/RX buffers.  So just don't do anything for
613 		 * lost link.
614 		 */
615 		return;
616 	}
617 
618 	/* Set full/half duplex mode. */
619 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
620 		SIS_SETBIT(sc, SIS_TX_CFG,
621 		    (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
622 		SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
623 	} else {
624 		SIS_CLRBIT(sc, SIS_TX_CFG,
625 		    (SIS_TXCFG_IGN_HBEAT | SIS_TXCFG_IGN_CARR));
626 		SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
627 	}
628 
629 	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
630 		/*
631 		 * MPII03.D: Half Duplex Excessive Collisions.
632 		 * Also page 49 in 83816 manual
633 		 */
634 		SIS_SETBIT(sc, SIS_TX_CFG, SIS_TXCFG_MPII03D);
635 	}
636 
637 	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr < NS_SRR_16A &&
638 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
639 		/*
640 		 * Short Cable Receive Errors (MP21.E)
641 		 */
642 		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
643 		reg = CSR_READ_4(sc, NS_PHY_DSPCFG) & 0xfff;
644 		CSR_WRITE_4(sc, NS_PHY_DSPCFG, reg | 0x1000);
645 		DELAY(100);
646 		reg = CSR_READ_4(sc, NS_PHY_TDATA) & 0xff;
647 		if ((reg & 0x0080) == 0 || (reg > 0xd8 && reg <= 0xff)) {
648 			device_printf(sc->sis_dev,
649 			    "Applying short cable fix (reg=%x)\n", reg);
650 			CSR_WRITE_4(sc, NS_PHY_TDATA, 0x00e8);
651 			SIS_SETBIT(sc, NS_PHY_DSPCFG, 0x20);
652 		}
653 		CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
654 	}
655 	/* Enable TX/RX MACs. */
656 	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
657 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE | SIS_CSR_RX_ENABLE);
658 }
659 
660 static uint32_t
661 sis_mchash(struct sis_softc *sc, const uint8_t *addr)
662 {
663 	uint32_t		crc;
664 
665 	/* Compute CRC for the address value. */
666 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
667 
668 	/*
669 	 * return the filter bit position
670 	 *
671 	 * The NatSemi chip has a 512-bit filter, which is
672 	 * different than the SiS, so we special-case it.
673 	 */
674 	if (sc->sis_type == SIS_TYPE_83815)
675 		return (crc >> 23);
676 	else if (sc->sis_rev >= SIS_REV_635 ||
677 	    sc->sis_rev == SIS_REV_900B)
678 		return (crc >> 24);
679 	else
680 		return (crc >> 25);
681 }
682 
683 static void
684 sis_rxfilter(struct sis_softc *sc)
685 {
686 
687 	SIS_LOCK_ASSERT(sc);
688 
689 	if (sc->sis_type == SIS_TYPE_83815)
690 		sis_rxfilter_ns(sc);
691 	else
692 		sis_rxfilter_sis(sc);
693 }
694 
695 static void
696 sis_rxfilter_ns(struct sis_softc *sc)
697 {
698 	struct ifnet		*ifp;
699 	struct ifmultiaddr	*ifma;
700 	uint32_t		h, i, filter;
701 	int			bit, index;
702 
703 	ifp = sc->sis_ifp;
704 	filter = CSR_READ_4(sc, SIS_RXFILT_CTL);
705 	if (filter & SIS_RXFILTCTL_ENABLE) {
706 		/*
707 		 * Filter should be disabled to program other bits.
708 		 */
709 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter & ~SIS_RXFILTCTL_ENABLE);
710 		CSR_READ_4(sc, SIS_RXFILT_CTL);
711 	}
712 	filter &= ~(NS_RXFILTCTL_ARP | NS_RXFILTCTL_PERFECT |
713 	    NS_RXFILTCTL_MCHASH | SIS_RXFILTCTL_ALLPHYS | SIS_RXFILTCTL_BROAD |
714 	    SIS_RXFILTCTL_ALLMULTI);
715 
716 	if (ifp->if_flags & IFF_BROADCAST)
717 		filter |= SIS_RXFILTCTL_BROAD;
718 	/*
719 	 * For the NatSemi chip, we have to explicitly enable the
720 	 * reception of ARP frames, as well as turn on the 'perfect
721 	 * match' filter where we store the station address, otherwise
722 	 * we won't receive unicasts meant for this host.
723 	 */
724 	filter |= NS_RXFILTCTL_ARP | NS_RXFILTCTL_PERFECT;
725 
726 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
727 		filter |= SIS_RXFILTCTL_ALLMULTI;
728 		if (ifp->if_flags & IFF_PROMISC)
729 			filter |= SIS_RXFILTCTL_ALLPHYS;
730 	} else {
731 		/*
732 		 * We have to explicitly enable the multicast hash table
733 		 * on the NatSemi chip if we want to use it, which we do.
734 		 */
735 		filter |= NS_RXFILTCTL_MCHASH;
736 
737 		/* first, zot all the existing hash bits */
738 		for (i = 0; i < 32; i++) {
739 			CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO +
740 			    (i * 2));
741 			CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
742 		}
743 
744 		if_maddr_rlock(ifp);
745 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
746 			if (ifma->ifma_addr->sa_family != AF_LINK)
747 				continue;
748 			h = sis_mchash(sc,
749 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
750 			index = h >> 3;
751 			bit = h & 0x1F;
752 			CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO +
753 			    index);
754 			if (bit > 0xF)
755 				bit -= 0x10;
756 			SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
757 		}
758 		if_maddr_runlock(ifp);
759 	}
760 
761 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter);
762 	CSR_READ_4(sc, SIS_RXFILT_CTL);
763 }
764 
765 static void
766 sis_rxfilter_sis(struct sis_softc *sc)
767 {
768 	struct ifnet		*ifp;
769 	struct ifmultiaddr	*ifma;
770 	uint32_t		filter, h, i, n;
771 	uint16_t		hashes[16];
772 
773 	ifp = sc->sis_ifp;
774 
775 	/* hash table size */
776 	if (sc->sis_rev >= SIS_REV_635 || sc->sis_rev == SIS_REV_900B)
777 		n = 16;
778 	else
779 		n = 8;
780 
781 	filter = CSR_READ_4(sc, SIS_RXFILT_CTL);
782 	if (filter & SIS_RXFILTCTL_ENABLE) {
783 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter & ~SIS_RXFILTCTL_ENABLE);
784 		CSR_READ_4(sc, SIS_RXFILT_CTL);
785 	}
786 	filter &= ~(SIS_RXFILTCTL_ALLPHYS | SIS_RXFILTCTL_BROAD |
787 	    SIS_RXFILTCTL_ALLMULTI);
788 	if (ifp->if_flags & IFF_BROADCAST)
789 		filter |= SIS_RXFILTCTL_BROAD;
790 
791 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
792 		filter |= SIS_RXFILTCTL_ALLMULTI;
793 		if (ifp->if_flags & IFF_PROMISC)
794 			filter |= SIS_RXFILTCTL_ALLPHYS;
795 		for (i = 0; i < n; i++)
796 			hashes[i] = ~0;
797 	} else {
798 		for (i = 0; i < n; i++)
799 			hashes[i] = 0;
800 		i = 0;
801 		if_maddr_rlock(ifp);
802 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
803 			if (ifma->ifma_addr->sa_family != AF_LINK)
804 			continue;
805 			h = sis_mchash(sc,
806 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
807 			hashes[h >> 4] |= 1 << (h & 0xf);
808 			i++;
809 		}
810 		if_maddr_runlock(ifp);
811 		if (i > n) {
812 			filter |= SIS_RXFILTCTL_ALLMULTI;
813 			for (i = 0; i < n; i++)
814 				hashes[i] = ~0;
815 		}
816 	}
817 
818 	for (i = 0; i < n; i++) {
819 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
820 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, hashes[i]);
821 	}
822 
823 	CSR_WRITE_4(sc, SIS_RXFILT_CTL, filter);
824 	CSR_READ_4(sc, SIS_RXFILT_CTL);
825 }
826 
827 static void
828 sis_reset(struct sis_softc *sc)
829 {
830 	int		i;
831 
832 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
833 
834 	for (i = 0; i < SIS_TIMEOUT; i++) {
835 		if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
836 			break;
837 	}
838 
839 	if (i == SIS_TIMEOUT)
840 		device_printf(sc->sis_dev, "reset never completed\n");
841 
842 	/* Wait a little while for the chip to get its brains in order. */
843 	DELAY(1000);
844 
845 	/*
846 	 * If this is a NetSemi chip, make sure to clear
847 	 * PME mode.
848 	 */
849 	if (sc->sis_type == SIS_TYPE_83815) {
850 		CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
851 		CSR_WRITE_4(sc, NS_CLKRUN, 0);
852 	} else {
853 		/* Disable WOL functions. */
854 		CSR_WRITE_4(sc, SIS_PWRMAN_CTL, 0);
855 	}
856 }
857 
858 /*
859  * Probe for an SiS chip. Check the PCI vendor and device
860  * IDs against our list and return a device name if we find a match.
861  */
862 static int
863 sis_probe(device_t dev)
864 {
865 	const struct sis_type	*t;
866 
867 	t = sis_devs;
868 
869 	while (t->sis_name != NULL) {
870 		if ((pci_get_vendor(dev) == t->sis_vid) &&
871 		    (pci_get_device(dev) == t->sis_did)) {
872 			device_set_desc(dev, t->sis_name);
873 			return (BUS_PROBE_DEFAULT);
874 		}
875 		t++;
876 	}
877 
878 	return (ENXIO);
879 }
880 
881 /*
882  * Attach the interface. Allocate softc structures, do ifmedia
883  * setup and ethernet/BPF attach.
884  */
885 static int
886 sis_attach(device_t dev)
887 {
888 	u_char			eaddr[ETHER_ADDR_LEN];
889 	struct sis_softc	*sc;
890 	struct ifnet		*ifp;
891 	int			error = 0, pmc, waittime = 0;
892 
893 	waittime = 0;
894 	sc = device_get_softc(dev);
895 
896 	sc->sis_dev = dev;
897 
898 	mtx_init(&sc->sis_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
899 	    MTX_DEF);
900 	callout_init_mtx(&sc->sis_stat_ch, &sc->sis_mtx, 0);
901 
902 	if (pci_get_device(dev) == SIS_DEVICEID_900)
903 		sc->sis_type = SIS_TYPE_900;
904 	if (pci_get_device(dev) == SIS_DEVICEID_7016)
905 		sc->sis_type = SIS_TYPE_7016;
906 	if (pci_get_vendor(dev) == NS_VENDORID)
907 		sc->sis_type = SIS_TYPE_83815;
908 
909 	sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
910 	/*
911 	 * Map control/status registers.
912 	 */
913 	pci_enable_busmaster(dev);
914 
915 	error = bus_alloc_resources(dev, sis_res_spec, sc->sis_res);
916 	if (error) {
917 		device_printf(dev, "couldn't allocate resources\n");
918 		goto fail;
919 	}
920 
921 	/* Reset the adapter. */
922 	sis_reset(sc);
923 
924 	if (sc->sis_type == SIS_TYPE_900 &&
925 	    (sc->sis_rev == SIS_REV_635 ||
926 	    sc->sis_rev == SIS_REV_900B)) {
927 		SIO_SET(SIS_CFG_RND_CNT);
928 		SIO_SET(SIS_CFG_PERR_DETECT);
929 	}
930 
931 	/*
932 	 * Get station address from the EEPROM.
933 	 */
934 	switch (pci_get_vendor(dev)) {
935 	case NS_VENDORID:
936 		sc->sis_srr = CSR_READ_4(sc, NS_SRR);
937 
938 		/* We can't update the device description, so spew */
939 		if (sc->sis_srr == NS_SRR_15C)
940 			device_printf(dev, "Silicon Revision: DP83815C\n");
941 		else if (sc->sis_srr == NS_SRR_15D)
942 			device_printf(dev, "Silicon Revision: DP83815D\n");
943 		else if (sc->sis_srr == NS_SRR_16A)
944 			device_printf(dev, "Silicon Revision: DP83816A\n");
945 		else
946 			device_printf(dev, "Silicon Revision %x\n", sc->sis_srr);
947 
948 		/*
949 		 * Reading the MAC address out of the EEPROM on
950 		 * the NatSemi chip takes a bit more work than
951 		 * you'd expect. The address spans 4 16-bit words,
952 		 * with the first word containing only a single bit.
953 		 * You have to shift everything over one bit to
954 		 * get it aligned properly. Also, the bits are
955 		 * stored backwards (the LSB is really the MSB,
956 		 * and so on) so you have to reverse them in order
957 		 * to get the MAC address into the form we want.
958 		 * Why? Who the hell knows.
959 		 */
960 		{
961 			uint16_t		tmp[4];
962 
963 			sis_read_eeprom(sc, (caddr_t)&tmp,
964 			    NS_EE_NODEADDR, 4, 0);
965 
966 			/* Shift everything over one bit. */
967 			tmp[3] = tmp[3] >> 1;
968 			tmp[3] |= tmp[2] << 15;
969 			tmp[2] = tmp[2] >> 1;
970 			tmp[2] |= tmp[1] << 15;
971 			tmp[1] = tmp[1] >> 1;
972 			tmp[1] |= tmp[0] << 15;
973 
974 			/* Now reverse all the bits. */
975 			tmp[3] = sis_reverse(tmp[3]);
976 			tmp[2] = sis_reverse(tmp[2]);
977 			tmp[1] = sis_reverse(tmp[1]);
978 
979 			eaddr[0] = (tmp[1] >> 0) & 0xFF;
980 			eaddr[1] = (tmp[1] >> 8) & 0xFF;
981 			eaddr[2] = (tmp[2] >> 0) & 0xFF;
982 			eaddr[3] = (tmp[2] >> 8) & 0xFF;
983 			eaddr[4] = (tmp[3] >> 0) & 0xFF;
984 			eaddr[5] = (tmp[3] >> 8) & 0xFF;
985 		}
986 		break;
987 	case SIS_VENDORID:
988 	default:
989 #if defined(__i386__) || defined(__amd64__)
990 		/*
991 		 * If this is a SiS 630E chipset with an embedded
992 		 * SiS 900 controller, we have to read the MAC address
993 		 * from the APC CMOS RAM. Our method for doing this
994 		 * is very ugly since we have to reach out and grab
995 		 * ahold of hardware for which we cannot properly
996 		 * allocate resources. This code is only compiled on
997 		 * the i386 architecture since the SiS 630E chipset
998 		 * is for x86 motherboards only. Note that there are
999 		 * a lot of magic numbers in this hack. These are
1000 		 * taken from SiS's Linux driver. I'd like to replace
1001 		 * them with proper symbolic definitions, but that
1002 		 * requires some datasheets that I don't have access
1003 		 * to at the moment.
1004 		 */
1005 		if (sc->sis_rev == SIS_REV_630S ||
1006 		    sc->sis_rev == SIS_REV_630E ||
1007 		    sc->sis_rev == SIS_REV_630EA1)
1008 			sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
1009 
1010 		else if (sc->sis_rev == SIS_REV_635 ||
1011 			 sc->sis_rev == SIS_REV_630ET)
1012 			sis_read_mac(sc, dev, (caddr_t)&eaddr);
1013 		else if (sc->sis_rev == SIS_REV_96x) {
1014 			/* Allow to read EEPROM from LAN. It is shared
1015 			 * between a 1394 controller and the NIC and each
1016 			 * time we access it, we need to set SIS_EECMD_REQ.
1017 			 */
1018 			SIO_SET(SIS_EECMD_REQ);
1019 			for (waittime = 0; waittime < SIS_TIMEOUT;
1020 			    waittime++) {
1021 				/* Force EEPROM to idle state. */
1022 				sis_eeprom_idle(sc);
1023 				if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1024 					sis_read_eeprom(sc, (caddr_t)&eaddr,
1025 					    SIS_EE_NODEADDR, 3, 0);
1026 					break;
1027 				}
1028 				DELAY(1);
1029 			}
1030 			/*
1031 			 * Set SIS_EECTL_CLK to high, so a other master
1032 			 * can operate on the i2c bus.
1033 			 */
1034 			SIO_SET(SIS_EECTL_CLK);
1035 			/* Refuse EEPROM access by LAN */
1036 			SIO_SET(SIS_EECMD_DONE);
1037 		} else
1038 #endif
1039 			sis_read_eeprom(sc, (caddr_t)&eaddr,
1040 			    SIS_EE_NODEADDR, 3, 0);
1041 		break;
1042 	}
1043 
1044 	sis_add_sysctls(sc);
1045 
1046 	/* Allocate DMA'able memory. */
1047 	if ((error = sis_dma_alloc(sc)) != 0)
1048 		goto fail;
1049 
1050 	ifp = sc->sis_ifp = if_alloc(IFT_ETHER);
1051 	if (ifp == NULL) {
1052 		device_printf(dev, "can not if_alloc()\n");
1053 		error = ENOSPC;
1054 		goto fail;
1055 	}
1056 	ifp->if_softc = sc;
1057 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1058 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1059 	ifp->if_ioctl = sis_ioctl;
1060 	ifp->if_start = sis_start;
1061 	ifp->if_init = sis_init;
1062 	IFQ_SET_MAXLEN(&ifp->if_snd, SIS_TX_LIST_CNT - 1);
1063 	ifp->if_snd.ifq_drv_maxlen = SIS_TX_LIST_CNT - 1;
1064 	IFQ_SET_READY(&ifp->if_snd);
1065 
1066 	if (pci_find_cap(sc->sis_dev, PCIY_PMG, &pmc) == 0) {
1067 		if (sc->sis_type == SIS_TYPE_83815)
1068 			ifp->if_capabilities |= IFCAP_WOL;
1069 		else
1070 			ifp->if_capabilities |= IFCAP_WOL_MAGIC;
1071 		ifp->if_capenable = ifp->if_capabilities;
1072 	}
1073 
1074 	/*
1075 	 * Do MII setup.
1076 	 */
1077 	error = mii_attach(dev, &sc->sis_miibus, ifp, sis_ifmedia_upd,
1078 	    sis_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1079 	if (error != 0) {
1080 		device_printf(dev, "attaching PHYs failed\n");
1081 		goto fail;
1082 	}
1083 
1084 	/*
1085 	 * Call MI attach routine.
1086 	 */
1087 	ether_ifattach(ifp, eaddr);
1088 
1089 	/*
1090 	 * Tell the upper layer(s) we support long frames.
1091 	 */
1092 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1093 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
1094 	ifp->if_capenable = ifp->if_capabilities;
1095 #ifdef DEVICE_POLLING
1096 	ifp->if_capabilities |= IFCAP_POLLING;
1097 #endif
1098 
1099 	/* Hook interrupt last to avoid having to lock softc */
1100 	error = bus_setup_intr(dev, sc->sis_res[1], INTR_TYPE_NET | INTR_MPSAFE,
1101 	    NULL, sis_intr, sc, &sc->sis_intrhand);
1102 
1103 	if (error) {
1104 		device_printf(dev, "couldn't set up irq\n");
1105 		ether_ifdetach(ifp);
1106 		goto fail;
1107 	}
1108 
1109 fail:
1110 	if (error)
1111 		sis_detach(dev);
1112 
1113 	return (error);
1114 }
1115 
1116 /*
1117  * Shutdown hardware and free up resources. This can be called any
1118  * time after the mutex has been initialized. It is called in both
1119  * the error case in attach and the normal detach case so it needs
1120  * to be careful about only freeing resources that have actually been
1121  * allocated.
1122  */
1123 static int
1124 sis_detach(device_t dev)
1125 {
1126 	struct sis_softc	*sc;
1127 	struct ifnet		*ifp;
1128 
1129 	sc = device_get_softc(dev);
1130 	KASSERT(mtx_initialized(&sc->sis_mtx), ("sis mutex not initialized"));
1131 	ifp = sc->sis_ifp;
1132 
1133 #ifdef DEVICE_POLLING
1134 	if (ifp->if_capenable & IFCAP_POLLING)
1135 		ether_poll_deregister(ifp);
1136 #endif
1137 
1138 	/* These should only be active if attach succeeded. */
1139 	if (device_is_attached(dev)) {
1140 		SIS_LOCK(sc);
1141 		sis_stop(sc);
1142 		SIS_UNLOCK(sc);
1143 		callout_drain(&sc->sis_stat_ch);
1144 		ether_ifdetach(ifp);
1145 	}
1146 	if (sc->sis_miibus)
1147 		device_delete_child(dev, sc->sis_miibus);
1148 	bus_generic_detach(dev);
1149 
1150 	if (sc->sis_intrhand)
1151 		bus_teardown_intr(dev, sc->sis_res[1], sc->sis_intrhand);
1152 	bus_release_resources(dev, sis_res_spec, sc->sis_res);
1153 
1154 	if (ifp)
1155 		if_free(ifp);
1156 
1157 	sis_dma_free(sc);
1158 
1159 	mtx_destroy(&sc->sis_mtx);
1160 
1161 	return (0);
1162 }
1163 
1164 struct sis_dmamap_arg {
1165 	bus_addr_t	sis_busaddr;
1166 };
1167 
1168 static void
1169 sis_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1170 {
1171 	struct sis_dmamap_arg	*ctx;
1172 
1173 	if (error != 0)
1174 		return;
1175 
1176 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1177 
1178 	ctx = (struct sis_dmamap_arg *)arg;
1179 	ctx->sis_busaddr = segs[0].ds_addr;
1180 }
1181 
1182 static int
1183 sis_dma_ring_alloc(struct sis_softc *sc, bus_size_t alignment,
1184     bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
1185     bus_addr_t *paddr, const char *msg)
1186 {
1187 	struct sis_dmamap_arg	ctx;
1188 	int			error;
1189 
1190 	error = bus_dma_tag_create(sc->sis_parent_tag, alignment, 0,
1191 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, maxsize, 1,
1192 	    maxsize, 0, NULL, NULL, tag);
1193 	if (error != 0) {
1194 		device_printf(sc->sis_dev,
1195 		    "could not create %s dma tag\n", msg);
1196 		return (ENOMEM);
1197 	}
1198 	/* Allocate DMA'able memory for ring. */
1199 	error = bus_dmamem_alloc(*tag, (void **)ring,
1200 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
1201 	if (error != 0) {
1202 		device_printf(sc->sis_dev,
1203 		    "could not allocate DMA'able memory for %s\n", msg);
1204 		return (ENOMEM);
1205 	}
1206 	/* Load the address of the ring. */
1207 	ctx.sis_busaddr = 0;
1208 	error = bus_dmamap_load(*tag, *map, *ring, maxsize, sis_dmamap_cb,
1209 	    &ctx, BUS_DMA_NOWAIT);
1210 	if (error != 0) {
1211 		device_printf(sc->sis_dev,
1212 		    "could not load DMA'able memory for %s\n", msg);
1213 		return (ENOMEM);
1214 	}
1215 	*paddr = ctx.sis_busaddr;
1216 	return (0);
1217 }
1218 
1219 static int
1220 sis_dma_alloc(struct sis_softc *sc)
1221 {
1222 	struct sis_rxdesc	*rxd;
1223 	struct sis_txdesc	*txd;
1224 	int			error, i;
1225 
1226 	/* Allocate the parent bus DMA tag appropriate for PCI. */
1227 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sis_dev),
1228 	    1, 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1229 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
1230 	    0, NULL, NULL, &sc->sis_parent_tag);
1231 	if (error != 0) {
1232 		device_printf(sc->sis_dev,
1233 		    "could not allocate parent dma tag\n");
1234 		return (ENOMEM);
1235 	}
1236 
1237 	/* Create RX ring. */
1238 	error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_RX_LIST_SZ,
1239 	    &sc->sis_rx_list_tag, (uint8_t **)&sc->sis_rx_list,
1240 	    &sc->sis_rx_list_map, &sc->sis_rx_paddr, "RX ring");
1241 	if (error)
1242 		return (error);
1243 
1244 	/* Create TX ring. */
1245 	error = sis_dma_ring_alloc(sc, SIS_DESC_ALIGN, SIS_TX_LIST_SZ,
1246 	    &sc->sis_tx_list_tag, (uint8_t **)&sc->sis_tx_list,
1247 	    &sc->sis_tx_list_map, &sc->sis_tx_paddr, "TX ring");
1248 	if (error)
1249 		return (error);
1250 
1251 	/* Create tag for RX mbufs. */
1252 	error = bus_dma_tag_create(sc->sis_parent_tag, SIS_RX_BUF_ALIGN, 0,
1253 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1,
1254 	    MCLBYTES, 0, NULL, NULL, &sc->sis_rx_tag);
1255 	if (error) {
1256 		device_printf(sc->sis_dev, "could not allocate RX dma tag\n");
1257 		return (error);
1258 	}
1259 
1260 	/* Create tag for TX mbufs. */
1261 	error = bus_dma_tag_create(sc->sis_parent_tag, 1, 0,
1262 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1263 	    MCLBYTES * SIS_MAXTXSEGS, SIS_MAXTXSEGS, MCLBYTES, 0, NULL, NULL,
1264 	    &sc->sis_tx_tag);
1265 	if (error) {
1266 		device_printf(sc->sis_dev, "could not allocate TX dma tag\n");
1267 		return (error);
1268 	}
1269 
1270 	/* Create DMA maps for RX buffers. */
1271 	error = bus_dmamap_create(sc->sis_rx_tag, 0, &sc->sis_rx_sparemap);
1272 	if (error) {
1273 		device_printf(sc->sis_dev,
1274 		    "can't create spare DMA map for RX\n");
1275 		return (error);
1276 	}
1277 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1278 		rxd = &sc->sis_rxdesc[i];
1279 		rxd->rx_m = NULL;
1280 		error = bus_dmamap_create(sc->sis_rx_tag, 0, &rxd->rx_dmamap);
1281 		if (error) {
1282 			device_printf(sc->sis_dev,
1283 			    "can't create DMA map for RX\n");
1284 			return (error);
1285 		}
1286 	}
1287 
1288 	/* Create DMA maps for TX buffers. */
1289 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1290 		txd = &sc->sis_txdesc[i];
1291 		txd->tx_m = NULL;
1292 		error = bus_dmamap_create(sc->sis_tx_tag, 0, &txd->tx_dmamap);
1293 		if (error) {
1294 			device_printf(sc->sis_dev,
1295 			    "can't create DMA map for TX\n");
1296 			return (error);
1297 		}
1298 	}
1299 
1300 	return (0);
1301 }
1302 
1303 static void
1304 sis_dma_free(struct sis_softc *sc)
1305 {
1306 	struct sis_rxdesc	*rxd;
1307 	struct sis_txdesc	*txd;
1308 	int			i;
1309 
1310 	/* Destroy DMA maps for RX buffers. */
1311 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1312 		rxd = &sc->sis_rxdesc[i];
1313 		if (rxd->rx_dmamap)
1314 			bus_dmamap_destroy(sc->sis_rx_tag, rxd->rx_dmamap);
1315 	}
1316 	if (sc->sis_rx_sparemap)
1317 		bus_dmamap_destroy(sc->sis_rx_tag, sc->sis_rx_sparemap);
1318 
1319 	/* Destroy DMA maps for TX buffers. */
1320 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1321 		txd = &sc->sis_txdesc[i];
1322 		if (txd->tx_dmamap)
1323 			bus_dmamap_destroy(sc->sis_tx_tag, txd->tx_dmamap);
1324 	}
1325 
1326 	if (sc->sis_rx_tag)
1327 		bus_dma_tag_destroy(sc->sis_rx_tag);
1328 	if (sc->sis_tx_tag)
1329 		bus_dma_tag_destroy(sc->sis_tx_tag);
1330 
1331 	/* Destroy RX ring. */
1332 	if (sc->sis_rx_paddr)
1333 		bus_dmamap_unload(sc->sis_rx_list_tag, sc->sis_rx_list_map);
1334 	if (sc->sis_rx_list)
1335 		bus_dmamem_free(sc->sis_rx_list_tag, sc->sis_rx_list,
1336 		    sc->sis_rx_list_map);
1337 
1338 	if (sc->sis_rx_list_tag)
1339 		bus_dma_tag_destroy(sc->sis_rx_list_tag);
1340 
1341 	/* Destroy TX ring. */
1342 	if (sc->sis_tx_paddr)
1343 		bus_dmamap_unload(sc->sis_tx_list_tag, sc->sis_tx_list_map);
1344 
1345 	if (sc->sis_tx_list)
1346 		bus_dmamem_free(sc->sis_tx_list_tag, sc->sis_tx_list,
1347 		    sc->sis_tx_list_map);
1348 
1349 	if (sc->sis_tx_list_tag)
1350 		bus_dma_tag_destroy(sc->sis_tx_list_tag);
1351 
1352 	/* Destroy the parent tag. */
1353 	if (sc->sis_parent_tag)
1354 		bus_dma_tag_destroy(sc->sis_parent_tag);
1355 }
1356 
1357 /*
1358  * Initialize the TX and RX descriptors and allocate mbufs for them. Note that
1359  * we arrange the descriptors in a closed ring, so that the last descriptor
1360  * points back to the first.
1361  */
1362 static int
1363 sis_ring_init(struct sis_softc *sc)
1364 {
1365 	struct sis_rxdesc	*rxd;
1366 	struct sis_txdesc	*txd;
1367 	bus_addr_t		next;
1368 	int			error, i;
1369 
1370 	bzero(&sc->sis_tx_list[0], SIS_TX_LIST_SZ);
1371 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1372 		txd = &sc->sis_txdesc[i];
1373 		txd->tx_m = NULL;
1374 		if (i == SIS_TX_LIST_CNT - 1)
1375 			next = SIS_TX_RING_ADDR(sc, 0);
1376 		else
1377 			next = SIS_TX_RING_ADDR(sc, i + 1);
1378 		sc->sis_tx_list[i].sis_next = htole32(SIS_ADDR_LO(next));
1379 	}
1380 	sc->sis_tx_prod = sc->sis_tx_cons = sc->sis_tx_cnt = 0;
1381 	bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1382 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1383 
1384 	sc->sis_rx_cons = 0;
1385 	bzero(&sc->sis_rx_list[0], SIS_RX_LIST_SZ);
1386 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1387 		rxd = &sc->sis_rxdesc[i];
1388 		rxd->rx_desc = &sc->sis_rx_list[i];
1389 		if (i == SIS_RX_LIST_CNT - 1)
1390 			next = SIS_RX_RING_ADDR(sc, 0);
1391 		else
1392 			next = SIS_RX_RING_ADDR(sc, i + 1);
1393 		rxd->rx_desc->sis_next = htole32(SIS_ADDR_LO(next));
1394 		error = sis_newbuf(sc, rxd);
1395 		if (error)
1396 			return (error);
1397 	}
1398 	bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1399 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1400 
1401 	return (0);
1402 }
1403 
1404 /*
1405  * Initialize an RX descriptor and attach an MBUF cluster.
1406  */
1407 static int
1408 sis_newbuf(struct sis_softc *sc, struct sis_rxdesc *rxd)
1409 {
1410 	struct mbuf		*m;
1411 	bus_dma_segment_t	segs[1];
1412 	bus_dmamap_t		map;
1413 	int nsegs;
1414 
1415 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1416 	if (m == NULL)
1417 		return (ENOBUFS);
1418 	m->m_len = m->m_pkthdr.len = SIS_RXLEN;
1419 #ifndef __NO_STRICT_ALIGNMENT
1420 	m_adj(m, SIS_RX_BUF_ALIGN);
1421 #endif
1422 
1423 	if (bus_dmamap_load_mbuf_sg(sc->sis_rx_tag, sc->sis_rx_sparemap, m,
1424 	    segs, &nsegs, 0) != 0) {
1425 		m_freem(m);
1426 		return (ENOBUFS);
1427 	}
1428 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1429 
1430 	if (rxd->rx_m != NULL) {
1431 		bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
1432 		    BUS_DMASYNC_POSTREAD);
1433 		bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
1434 	}
1435 	map = rxd->rx_dmamap;
1436 	rxd->rx_dmamap = sc->sis_rx_sparemap;
1437 	sc->sis_rx_sparemap = map;
1438 	bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap, BUS_DMASYNC_PREREAD);
1439 	rxd->rx_m = m;
1440 	rxd->rx_desc->sis_ptr = htole32(SIS_ADDR_LO(segs[0].ds_addr));
1441 	rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1442 	return (0);
1443 }
1444 
1445 static __inline void
1446 sis_discard_rxbuf(struct sis_rxdesc *rxd)
1447 {
1448 
1449 	rxd->rx_desc->sis_cmdsts = htole32(SIS_RXLEN);
1450 }
1451 
1452 #ifndef __NO_STRICT_ALIGNMENT
1453 static __inline void
1454 sis_fixup_rx(struct mbuf *m)
1455 {
1456 	uint16_t		*src, *dst;
1457 	int			i;
1458 
1459 	src = mtod(m, uint16_t *);
1460 	dst = src - (SIS_RX_BUF_ALIGN - ETHER_ALIGN) / sizeof(*src);
1461 
1462 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1463 		*dst++ = *src++;
1464 
1465 	m->m_data -= SIS_RX_BUF_ALIGN - ETHER_ALIGN;
1466 }
1467 #endif
1468 
1469 /*
1470  * A frame has been uploaded: pass the resulting mbuf chain up to
1471  * the higher level protocols.
1472  */
1473 static int
1474 sis_rxeof(struct sis_softc *sc)
1475 {
1476 	struct mbuf		*m;
1477 	struct ifnet		*ifp;
1478 	struct sis_rxdesc	*rxd;
1479 	struct sis_desc		*cur_rx;
1480 	int			prog, rx_cons, rx_npkts = 0, total_len;
1481 	uint32_t		rxstat;
1482 
1483 	SIS_LOCK_ASSERT(sc);
1484 
1485 	bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1486 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1487 
1488 	rx_cons = sc->sis_rx_cons;
1489 	ifp = sc->sis_ifp;
1490 
1491 	for (prog = 0; (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1492 	    SIS_INC(rx_cons, SIS_RX_LIST_CNT), prog++) {
1493 #ifdef DEVICE_POLLING
1494 		if (ifp->if_capenable & IFCAP_POLLING) {
1495 			if (sc->rxcycles <= 0)
1496 				break;
1497 			sc->rxcycles--;
1498 		}
1499 #endif
1500 		cur_rx = &sc->sis_rx_list[rx_cons];
1501 		rxstat = le32toh(cur_rx->sis_cmdsts);
1502 		if ((rxstat & SIS_CMDSTS_OWN) == 0)
1503 			break;
1504 		rxd = &sc->sis_rxdesc[rx_cons];
1505 
1506 		total_len = (rxstat & SIS_CMDSTS_BUFLEN) - ETHER_CRC_LEN;
1507 		if ((ifp->if_capenable & IFCAP_VLAN_MTU) != 0 &&
1508 		    total_len <= (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN -
1509 		    ETHER_CRC_LEN))
1510 			rxstat &= ~SIS_RXSTAT_GIANT;
1511 		if (SIS_RXSTAT_ERROR(rxstat) != 0) {
1512 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1513 			if (rxstat & SIS_RXSTAT_COLL)
1514 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1515 			sis_discard_rxbuf(rxd);
1516 			continue;
1517 		}
1518 
1519 		/* Add a new receive buffer to the ring. */
1520 		m = rxd->rx_m;
1521 		if (sis_newbuf(sc, rxd) != 0) {
1522 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1523 			sis_discard_rxbuf(rxd);
1524 			continue;
1525 		}
1526 
1527 		/* No errors; receive the packet. */
1528 		m->m_pkthdr.len = m->m_len = total_len;
1529 #ifndef __NO_STRICT_ALIGNMENT
1530 		/*
1531 		 * On architectures without alignment problems we try to
1532 		 * allocate a new buffer for the receive ring, and pass up
1533 		 * the one where the packet is already, saving the expensive
1534 		 * copy operation.
1535 		 */
1536 		sis_fixup_rx(m);
1537 #endif
1538 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1539 		m->m_pkthdr.rcvif = ifp;
1540 
1541 		SIS_UNLOCK(sc);
1542 		(*ifp->if_input)(ifp, m);
1543 		SIS_LOCK(sc);
1544 		rx_npkts++;
1545 	}
1546 
1547 	if (prog > 0) {
1548 		sc->sis_rx_cons = rx_cons;
1549 		bus_dmamap_sync(sc->sis_rx_list_tag, sc->sis_rx_list_map,
1550 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1551 	}
1552 
1553 	return (rx_npkts);
1554 }
1555 
1556 /*
1557  * A frame was downloaded to the chip. It's safe for us to clean up
1558  * the list buffers.
1559  */
1560 
1561 static void
1562 sis_txeof(struct sis_softc *sc)
1563 {
1564 	struct ifnet		*ifp;
1565 	struct sis_desc		*cur_tx;
1566 	struct sis_txdesc	*txd;
1567 	uint32_t		cons, txstat;
1568 
1569 	SIS_LOCK_ASSERT(sc);
1570 
1571 	cons = sc->sis_tx_cons;
1572 	if (cons == sc->sis_tx_prod)
1573 		return;
1574 
1575 	ifp = sc->sis_ifp;
1576 	bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1577 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1578 
1579 	/*
1580 	 * Go through our tx list and free mbufs for those
1581 	 * frames that have been transmitted.
1582 	 */
1583 	for (; cons != sc->sis_tx_prod; SIS_INC(cons, SIS_TX_LIST_CNT)) {
1584 		cur_tx = &sc->sis_tx_list[cons];
1585 		txstat = le32toh(cur_tx->sis_cmdsts);
1586 		if ((txstat & SIS_CMDSTS_OWN) != 0)
1587 			break;
1588 		txd = &sc->sis_txdesc[cons];
1589 		if (txd->tx_m != NULL) {
1590 			bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
1591 			    BUS_DMASYNC_POSTWRITE);
1592 			bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1593 			m_freem(txd->tx_m);
1594 			txd->tx_m = NULL;
1595 			if ((txstat & SIS_CMDSTS_PKT_OK) != 0) {
1596 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1597 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
1598 				    (txstat & SIS_TXSTAT_COLLCNT) >> 16);
1599 			} else {
1600 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1601 				if (txstat & SIS_TXSTAT_EXCESSCOLLS)
1602 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1603 				if (txstat & SIS_TXSTAT_OUTOFWINCOLL)
1604 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1605 			}
1606 		}
1607 		sc->sis_tx_cnt--;
1608 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1609 	}
1610 	sc->sis_tx_cons = cons;
1611 	if (sc->sis_tx_cnt == 0)
1612 		sc->sis_watchdog_timer = 0;
1613 }
1614 
1615 static void
1616 sis_tick(void *xsc)
1617 {
1618 	struct sis_softc	*sc;
1619 	struct mii_data		*mii;
1620 
1621 	sc = xsc;
1622 	SIS_LOCK_ASSERT(sc);
1623 
1624 	mii = device_get_softc(sc->sis_miibus);
1625 	mii_tick(mii);
1626 	sis_watchdog(sc);
1627 	if ((sc->sis_flags & SIS_FLAG_LINK) == 0)
1628 		sis_miibus_statchg(sc->sis_dev);
1629 	callout_reset(&sc->sis_stat_ch, hz,  sis_tick, sc);
1630 }
1631 
1632 #ifdef DEVICE_POLLING
1633 static poll_handler_t sis_poll;
1634 
1635 static int
1636 sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1637 {
1638 	struct	sis_softc *sc = ifp->if_softc;
1639 	int rx_npkts = 0;
1640 
1641 	SIS_LOCK(sc);
1642 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1643 		SIS_UNLOCK(sc);
1644 		return (rx_npkts);
1645 	}
1646 
1647 	/*
1648 	 * On the sis, reading the status register also clears it.
1649 	 * So before returning to intr mode we must make sure that all
1650 	 * possible pending sources of interrupts have been served.
1651 	 * In practice this means run to completion the *eof routines,
1652 	 * and then call the interrupt routine
1653 	 */
1654 	sc->rxcycles = count;
1655 	rx_npkts = sis_rxeof(sc);
1656 	sis_txeof(sc);
1657 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1658 		sis_startl(ifp);
1659 
1660 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1661 		uint32_t	status;
1662 
1663 		/* Reading the ISR register clears all interrupts. */
1664 		status = CSR_READ_4(sc, SIS_ISR);
1665 
1666 		if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1667 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1668 
1669 		if (status & (SIS_ISR_RX_IDLE))
1670 			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1671 
1672 		if (status & SIS_ISR_SYSERR) {
1673 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1674 			sis_initl(sc);
1675 		}
1676 	}
1677 
1678 	SIS_UNLOCK(sc);
1679 	return (rx_npkts);
1680 }
1681 #endif /* DEVICE_POLLING */
1682 
1683 static void
1684 sis_intr(void *arg)
1685 {
1686 	struct sis_softc	*sc;
1687 	struct ifnet		*ifp;
1688 	uint32_t		status;
1689 
1690 	sc = arg;
1691 	ifp = sc->sis_ifp;
1692 
1693 	SIS_LOCK(sc);
1694 #ifdef DEVICE_POLLING
1695 	if (ifp->if_capenable & IFCAP_POLLING) {
1696 		SIS_UNLOCK(sc);
1697 		return;
1698 	}
1699 #endif
1700 
1701 	/* Reading the ISR register clears all interrupts. */
1702 	status = CSR_READ_4(sc, SIS_ISR);
1703 	if ((status & SIS_INTRS) == 0) {
1704 		/* Not ours. */
1705 		SIS_UNLOCK(sc);
1706 		return;
1707 	}
1708 
1709 	/* Disable interrupts. */
1710 	CSR_WRITE_4(sc, SIS_IER, 0);
1711 
1712 	for (;(status & SIS_INTRS) != 0;) {
1713 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1714 			break;
1715 		if (status &
1716 		    (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR |
1717 		    SIS_ISR_TX_OK | SIS_ISR_TX_IDLE) )
1718 			sis_txeof(sc);
1719 
1720 		if (status & (SIS_ISR_RX_DESC_OK | SIS_ISR_RX_OK |
1721 		    SIS_ISR_RX_ERR | SIS_ISR_RX_IDLE))
1722 			sis_rxeof(sc);
1723 
1724 		if (status & SIS_ISR_RX_OFLOW)
1725 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1726 
1727 		if (status & (SIS_ISR_RX_IDLE))
1728 			SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1729 
1730 		if (status & SIS_ISR_SYSERR) {
1731 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1732 			sis_initl(sc);
1733 			SIS_UNLOCK(sc);
1734 			return;
1735 		}
1736 		status = CSR_READ_4(sc, SIS_ISR);
1737 	}
1738 
1739 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1740 		/* Re-enable interrupts. */
1741 		CSR_WRITE_4(sc, SIS_IER, 1);
1742 
1743 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1744 			sis_startl(ifp);
1745 	}
1746 
1747 	SIS_UNLOCK(sc);
1748 }
1749 
1750 /*
1751  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1752  * pointers to the fragment pointers.
1753  */
1754 static int
1755 sis_encap(struct sis_softc *sc, struct mbuf **m_head)
1756 {
1757 	struct mbuf		*m;
1758 	struct sis_txdesc	*txd;
1759 	struct sis_desc		*f;
1760 	bus_dma_segment_t	segs[SIS_MAXTXSEGS];
1761 	bus_dmamap_t		map;
1762 	int			error, i, frag, nsegs, prod;
1763 	int			padlen;
1764 
1765 	prod = sc->sis_tx_prod;
1766 	txd = &sc->sis_txdesc[prod];
1767 	if ((sc->sis_flags & SIS_FLAG_MANUAL_PAD) != 0 &&
1768 	    (*m_head)->m_pkthdr.len < SIS_MIN_FRAMELEN) {
1769 		m = *m_head;
1770 		padlen = SIS_MIN_FRAMELEN - m->m_pkthdr.len;
1771 		if (M_WRITABLE(m) == 0) {
1772 			/* Get a writable copy. */
1773 			m = m_dup(*m_head, M_NOWAIT);
1774 			m_freem(*m_head);
1775 			if (m == NULL) {
1776 				*m_head = NULL;
1777 				return (ENOBUFS);
1778 			}
1779 			*m_head = m;
1780 		}
1781 		if (m->m_next != NULL || M_TRAILINGSPACE(m) < padlen) {
1782 			m = m_defrag(m, M_NOWAIT);
1783 			if (m == NULL) {
1784 				m_freem(*m_head);
1785 				*m_head = NULL;
1786 				return (ENOBUFS);
1787 			}
1788 		}
1789 		/*
1790 		 * Manually pad short frames, and zero the pad space
1791 		 * to avoid leaking data.
1792 		 */
1793 		bzero(mtod(m, char *) + m->m_pkthdr.len, padlen);
1794 		m->m_pkthdr.len += padlen;
1795 		m->m_len = m->m_pkthdr.len;
1796 		*m_head = m;
1797 	}
1798 	error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1799 	    *m_head, segs, &nsegs, 0);
1800 	if (error == EFBIG) {
1801 		m = m_collapse(*m_head, M_NOWAIT, SIS_MAXTXSEGS);
1802 		if (m == NULL) {
1803 			m_freem(*m_head);
1804 			*m_head = NULL;
1805 			return (ENOBUFS);
1806 		}
1807 		*m_head = m;
1808 		error = bus_dmamap_load_mbuf_sg(sc->sis_tx_tag, txd->tx_dmamap,
1809 		    *m_head, segs, &nsegs, 0);
1810 		if (error != 0) {
1811 			m_freem(*m_head);
1812 			*m_head = NULL;
1813 			return (error);
1814 		}
1815 	} else if (error != 0)
1816 		return (error);
1817 
1818 	/* Check for descriptor overruns. */
1819 	if (sc->sis_tx_cnt + nsegs > SIS_TX_LIST_CNT - 1) {
1820 		bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
1821 		return (ENOBUFS);
1822 	}
1823 
1824 	bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap, BUS_DMASYNC_PREWRITE);
1825 
1826 	frag = prod;
1827 	for (i = 0; i < nsegs; i++) {
1828 		f = &sc->sis_tx_list[prod];
1829 		if (i == 0)
1830 			f->sis_cmdsts = htole32(segs[i].ds_len |
1831 			    SIS_CMDSTS_MORE);
1832 		else
1833 			f->sis_cmdsts = htole32(segs[i].ds_len |
1834 			    SIS_CMDSTS_OWN | SIS_CMDSTS_MORE);
1835 		f->sis_ptr = htole32(SIS_ADDR_LO(segs[i].ds_addr));
1836 		SIS_INC(prod, SIS_TX_LIST_CNT);
1837 		sc->sis_tx_cnt++;
1838 	}
1839 
1840 	/* Update producer index. */
1841 	sc->sis_tx_prod = prod;
1842 
1843 	/* Remove MORE flag on the last descriptor. */
1844 	prod = (prod - 1) & (SIS_TX_LIST_CNT - 1);
1845 	f = &sc->sis_tx_list[prod];
1846 	f->sis_cmdsts &= ~htole32(SIS_CMDSTS_MORE);
1847 
1848 	/* Lastly transfer ownership of packet to the controller. */
1849 	f = &sc->sis_tx_list[frag];
1850 	f->sis_cmdsts |= htole32(SIS_CMDSTS_OWN);
1851 
1852 	/* Swap the last and the first dmamaps. */
1853 	map = txd->tx_dmamap;
1854 	txd->tx_dmamap = sc->sis_txdesc[prod].tx_dmamap;
1855 	sc->sis_txdesc[prod].tx_dmamap = map;
1856 	sc->sis_txdesc[prod].tx_m = *m_head;
1857 
1858 	return (0);
1859 }
1860 
1861 static void
1862 sis_start(struct ifnet *ifp)
1863 {
1864 	struct sis_softc	*sc;
1865 
1866 	sc = ifp->if_softc;
1867 	SIS_LOCK(sc);
1868 	sis_startl(ifp);
1869 	SIS_UNLOCK(sc);
1870 }
1871 
1872 static void
1873 sis_startl(struct ifnet *ifp)
1874 {
1875 	struct sis_softc	*sc;
1876 	struct mbuf		*m_head;
1877 	int			queued;
1878 
1879 	sc = ifp->if_softc;
1880 
1881 	SIS_LOCK_ASSERT(sc);
1882 
1883 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1884 	    IFF_DRV_RUNNING || (sc->sis_flags & SIS_FLAG_LINK) == 0)
1885 		return;
1886 
1887 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
1888 	    sc->sis_tx_cnt < SIS_TX_LIST_CNT - 4;) {
1889 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1890 		if (m_head == NULL)
1891 			break;
1892 
1893 		if (sis_encap(sc, &m_head) != 0) {
1894 			if (m_head == NULL)
1895 				break;
1896 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1897 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1898 			break;
1899 		}
1900 
1901 		queued++;
1902 
1903 		/*
1904 		 * If there's a BPF listener, bounce a copy of this frame
1905 		 * to him.
1906 		 */
1907 		BPF_MTAP(ifp, m_head);
1908 	}
1909 
1910 	if (queued) {
1911 		/* Transmit */
1912 		bus_dmamap_sync(sc->sis_tx_list_tag, sc->sis_tx_list_map,
1913 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1914 		SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
1915 
1916 		/*
1917 		 * Set a timeout in case the chip goes out to lunch.
1918 		 */
1919 		sc->sis_watchdog_timer = 5;
1920 	}
1921 }
1922 
1923 static void
1924 sis_init(void *xsc)
1925 {
1926 	struct sis_softc	*sc = xsc;
1927 
1928 	SIS_LOCK(sc);
1929 	sis_initl(sc);
1930 	SIS_UNLOCK(sc);
1931 }
1932 
1933 static void
1934 sis_initl(struct sis_softc *sc)
1935 {
1936 	struct ifnet		*ifp = sc->sis_ifp;
1937 	struct mii_data		*mii;
1938 	uint8_t			*eaddr;
1939 
1940 	SIS_LOCK_ASSERT(sc);
1941 
1942 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1943 		return;
1944 
1945 	/*
1946 	 * Cancel pending I/O and free all RX/TX buffers.
1947 	 */
1948 	sis_stop(sc);
1949 	/*
1950 	 * Reset the chip to a known state.
1951 	 */
1952 	sis_reset(sc);
1953 #ifdef notyet
1954 	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr >= NS_SRR_16A) {
1955 		/*
1956 		 * Configure 400usec of interrupt holdoff.  This is based
1957 		 * on emperical tests on a Soekris 4801.
1958  		 */
1959 		CSR_WRITE_4(sc, NS_IHR, 0x100 | 4);
1960 	}
1961 #endif
1962 
1963 	mii = device_get_softc(sc->sis_miibus);
1964 
1965 	/* Set MAC address */
1966 	eaddr = IF_LLADDR(sc->sis_ifp);
1967 	if (sc->sis_type == SIS_TYPE_83815) {
1968 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
1969 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
1970 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
1971 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
1972 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
1973 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
1974 	} else {
1975 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
1976 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[0] | eaddr[1] << 8);
1977 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
1978 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[2] | eaddr[3] << 8);
1979 		CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
1980 		CSR_WRITE_4(sc, SIS_RXFILT_DATA, eaddr[4] | eaddr[5] << 8);
1981 	}
1982 
1983 	/* Init circular TX/RX lists. */
1984 	if (sis_ring_init(sc) != 0) {
1985 		device_printf(sc->sis_dev,
1986 		    "initialization failed: no memory for rx buffers\n");
1987 		sis_stop(sc);
1988 		return;
1989 	}
1990 
1991 	if (sc->sis_type == SIS_TYPE_83815) {
1992 		if (sc->sis_manual_pad != 0)
1993 			sc->sis_flags |= SIS_FLAG_MANUAL_PAD;
1994 		else
1995 			sc->sis_flags &= ~SIS_FLAG_MANUAL_PAD;
1996 	}
1997 
1998 	/*
1999 	 * Short Cable Receive Errors (MP21.E)
2000 	 * also: Page 78 of the DP83815 data sheet (september 2002 version)
2001 	 * recommends the following register settings "for optimum
2002 	 * performance." for rev 15C.  Set this also for 15D parts as
2003 	 * they require it in practice.
2004 	 */
2005 	if (sc->sis_type == SIS_TYPE_83815 && sc->sis_srr <= NS_SRR_15D) {
2006 		CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
2007 		CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
2008 		/* set val for c2 */
2009 		CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
2010 		/* load/kill c2 */
2011 		CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
2012 		/* rais SD off, from 4 to c */
2013 		CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
2014 		CSR_WRITE_4(sc, NS_PHY_PAGE, 0);
2015 	}
2016 
2017 	sis_rxfilter(sc);
2018 	/* Turn the receive filter on */
2019 	SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
2020 
2021 	/*
2022 	 * Load the address of the RX and TX lists.
2023 	 */
2024 	CSR_WRITE_4(sc, SIS_RX_LISTPTR, SIS_ADDR_LO(sc->sis_rx_paddr));
2025 	CSR_WRITE_4(sc, SIS_TX_LISTPTR, SIS_ADDR_LO(sc->sis_tx_paddr));
2026 
2027 	/* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
2028 	 * the PCI bus. When this bit is set, the Max DMA Burst Size
2029 	 * for TX/RX DMA should be no larger than 16 double words.
2030 	 */
2031 	if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN) {
2032 		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
2033 	} else {
2034 		CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
2035 	}
2036 
2037 	/* Accept Long Packets for VLAN support */
2038 	SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
2039 
2040 	/*
2041 	 * Assume 100Mbps link, actual MAC configuration is done
2042 	 * after getting a valid link.
2043 	 */
2044 	CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
2045 
2046 	/*
2047 	 * Enable interrupts.
2048 	 */
2049 	CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
2050 #ifdef DEVICE_POLLING
2051 	/*
2052 	 * ... only enable interrupts if we are not polling, make sure
2053 	 * they are off otherwise.
2054 	 */
2055 	if (ifp->if_capenable & IFCAP_POLLING)
2056 		CSR_WRITE_4(sc, SIS_IER, 0);
2057 	else
2058 #endif
2059 	CSR_WRITE_4(sc, SIS_IER, 1);
2060 
2061 	/* Clear MAC disable. */
2062 	SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE | SIS_CSR_RX_DISABLE);
2063 
2064 	sc->sis_flags &= ~SIS_FLAG_LINK;
2065 	mii_mediachg(mii);
2066 
2067 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2068 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2069 
2070 	callout_reset(&sc->sis_stat_ch, hz,  sis_tick, sc);
2071 }
2072 
2073 /*
2074  * Set media options.
2075  */
2076 static int
2077 sis_ifmedia_upd(struct ifnet *ifp)
2078 {
2079 	struct sis_softc	*sc;
2080 	struct mii_data		*mii;
2081 	struct mii_softc	*miisc;
2082 	int			error;
2083 
2084 	sc = ifp->if_softc;
2085 
2086 	SIS_LOCK(sc);
2087 	mii = device_get_softc(sc->sis_miibus);
2088 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
2089 		PHY_RESET(miisc);
2090 	error = mii_mediachg(mii);
2091 	SIS_UNLOCK(sc);
2092 
2093 	return (error);
2094 }
2095 
2096 /*
2097  * Report current media status.
2098  */
2099 static void
2100 sis_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2101 {
2102 	struct sis_softc	*sc;
2103 	struct mii_data		*mii;
2104 
2105 	sc = ifp->if_softc;
2106 
2107 	SIS_LOCK(sc);
2108 	mii = device_get_softc(sc->sis_miibus);
2109 	mii_pollstat(mii);
2110 	ifmr->ifm_active = mii->mii_media_active;
2111 	ifmr->ifm_status = mii->mii_media_status;
2112 	SIS_UNLOCK(sc);
2113 }
2114 
2115 static int
2116 sis_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2117 {
2118 	struct sis_softc	*sc = ifp->if_softc;
2119 	struct ifreq		*ifr = (struct ifreq *) data;
2120 	struct mii_data		*mii;
2121 	int			error = 0, mask;
2122 
2123 	switch (command) {
2124 	case SIOCSIFFLAGS:
2125 		SIS_LOCK(sc);
2126 		if (ifp->if_flags & IFF_UP) {
2127 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
2128 			    ((ifp->if_flags ^ sc->sis_if_flags) &
2129 			    (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2130 				sis_rxfilter(sc);
2131 			else
2132 				sis_initl(sc);
2133 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2134 			sis_stop(sc);
2135 		sc->sis_if_flags = ifp->if_flags;
2136 		SIS_UNLOCK(sc);
2137 		break;
2138 	case SIOCADDMULTI:
2139 	case SIOCDELMULTI:
2140 		SIS_LOCK(sc);
2141 		sis_rxfilter(sc);
2142 		SIS_UNLOCK(sc);
2143 		break;
2144 	case SIOCGIFMEDIA:
2145 	case SIOCSIFMEDIA:
2146 		mii = device_get_softc(sc->sis_miibus);
2147 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2148 		break;
2149 	case SIOCSIFCAP:
2150 		SIS_LOCK(sc);
2151 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2152 #ifdef DEVICE_POLLING
2153 		if ((mask & IFCAP_POLLING) != 0 &&
2154 		    (IFCAP_POLLING & ifp->if_capabilities) != 0) {
2155 			ifp->if_capenable ^= IFCAP_POLLING;
2156 			if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
2157 				error = ether_poll_register(sis_poll, ifp);
2158 				if (error != 0) {
2159 					SIS_UNLOCK(sc);
2160 					break;
2161 				}
2162 				/* Disable interrupts. */
2163 				CSR_WRITE_4(sc, SIS_IER, 0);
2164                         } else {
2165                                 error = ether_poll_deregister(ifp);
2166                                 /* Enable interrupts. */
2167 				CSR_WRITE_4(sc, SIS_IER, 1);
2168                         }
2169 		}
2170 #endif /* DEVICE_POLLING */
2171 		if ((mask & IFCAP_WOL) != 0 &&
2172 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
2173 			if ((mask & IFCAP_WOL_UCAST) != 0)
2174 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
2175 			if ((mask & IFCAP_WOL_MCAST) != 0)
2176 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
2177 			if ((mask & IFCAP_WOL_MAGIC) != 0)
2178 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2179 		}
2180 		SIS_UNLOCK(sc);
2181 		break;
2182 	default:
2183 		error = ether_ioctl(ifp, command, data);
2184 		break;
2185 	}
2186 
2187 	return (error);
2188 }
2189 
2190 static void
2191 sis_watchdog(struct sis_softc *sc)
2192 {
2193 
2194 	SIS_LOCK_ASSERT(sc);
2195 
2196 	if (sc->sis_watchdog_timer == 0 || --sc->sis_watchdog_timer >0)
2197 		return;
2198 
2199 	device_printf(sc->sis_dev, "watchdog timeout\n");
2200 	if_inc_counter(sc->sis_ifp, IFCOUNTER_OERRORS, 1);
2201 
2202 	sc->sis_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2203 	sis_initl(sc);
2204 
2205 	if (!IFQ_DRV_IS_EMPTY(&sc->sis_ifp->if_snd))
2206 		sis_startl(sc->sis_ifp);
2207 }
2208 
2209 /*
2210  * Stop the adapter and free any mbufs allocated to the
2211  * RX and TX lists.
2212  */
2213 static void
2214 sis_stop(struct sis_softc *sc)
2215 {
2216 	struct ifnet *ifp;
2217 	struct sis_rxdesc *rxd;
2218 	struct sis_txdesc *txd;
2219 	int i;
2220 
2221 	SIS_LOCK_ASSERT(sc);
2222 
2223 	ifp = sc->sis_ifp;
2224 	sc->sis_watchdog_timer = 0;
2225 
2226 	callout_stop(&sc->sis_stat_ch);
2227 
2228 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2229 	CSR_WRITE_4(sc, SIS_IER, 0);
2230 	CSR_WRITE_4(sc, SIS_IMR, 0);
2231 	CSR_READ_4(sc, SIS_ISR); /* clear any interrupts already pending */
2232 	SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
2233 	DELAY(1000);
2234 	CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
2235 	CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2236 
2237 	sc->sis_flags &= ~SIS_FLAG_LINK;
2238 
2239 	/*
2240 	 * Free data in the RX lists.
2241 	 */
2242 	for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2243 		rxd = &sc->sis_rxdesc[i];
2244 		if (rxd->rx_m != NULL) {
2245 			bus_dmamap_sync(sc->sis_rx_tag, rxd->rx_dmamap,
2246 			    BUS_DMASYNC_POSTREAD);
2247 			bus_dmamap_unload(sc->sis_rx_tag, rxd->rx_dmamap);
2248 			m_freem(rxd->rx_m);
2249 			rxd->rx_m = NULL;
2250 		}
2251 	}
2252 
2253 	/*
2254 	 * Free the TX list buffers.
2255 	 */
2256 	for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2257 		txd = &sc->sis_txdesc[i];
2258 		if (txd->tx_m != NULL) {
2259 			bus_dmamap_sync(sc->sis_tx_tag, txd->tx_dmamap,
2260 			    BUS_DMASYNC_POSTWRITE);
2261 			bus_dmamap_unload(sc->sis_tx_tag, txd->tx_dmamap);
2262 			m_freem(txd->tx_m);
2263 			txd->tx_m = NULL;
2264 		}
2265 	}
2266 }
2267 
2268 /*
2269  * Stop all chip I/O so that the kernel's probe routines don't
2270  * get confused by errant DMAs when rebooting.
2271  */
2272 static int
2273 sis_shutdown(device_t dev)
2274 {
2275 
2276 	return (sis_suspend(dev));
2277 }
2278 
2279 static int
2280 sis_suspend(device_t dev)
2281 {
2282 	struct sis_softc	*sc;
2283 
2284 	sc = device_get_softc(dev);
2285 	SIS_LOCK(sc);
2286 	sis_stop(sc);
2287 	sis_wol(sc);
2288 	SIS_UNLOCK(sc);
2289 	return (0);
2290 }
2291 
2292 static int
2293 sis_resume(device_t dev)
2294 {
2295 	struct sis_softc	*sc;
2296 	struct ifnet		*ifp;
2297 
2298 	sc = device_get_softc(dev);
2299 	SIS_LOCK(sc);
2300 	ifp = sc->sis_ifp;
2301 	if ((ifp->if_flags & IFF_UP) != 0) {
2302 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2303 		sis_initl(sc);
2304 	}
2305 	SIS_UNLOCK(sc);
2306 	return (0);
2307 }
2308 
2309 static void
2310 sis_wol(struct sis_softc *sc)
2311 {
2312 	struct ifnet		*ifp;
2313 	uint32_t		val;
2314 	uint16_t		pmstat;
2315 	int			pmc;
2316 
2317 	ifp = sc->sis_ifp;
2318 	if ((ifp->if_capenable & IFCAP_WOL) == 0)
2319 		return;
2320 
2321 	if (sc->sis_type == SIS_TYPE_83815) {
2322 		/* Reset RXDP. */
2323 		CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2324 
2325 		/* Configure WOL events. */
2326 		CSR_READ_4(sc, NS_WCSR);
2327 		val = 0;
2328 		if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2329 			val |= NS_WCSR_WAKE_UCAST;
2330 		if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2331 			val |= NS_WCSR_WAKE_MCAST;
2332 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2333 			val |= NS_WCSR_WAKE_MAGIC;
2334 		CSR_WRITE_4(sc, NS_WCSR, val);
2335 		/* Enable PME and clear PMESTS. */
2336 		val = CSR_READ_4(sc, NS_CLKRUN);
2337 		val |= NS_CLKRUN_PMEENB | NS_CLKRUN_PMESTS;
2338 		CSR_WRITE_4(sc, NS_CLKRUN, val);
2339 		/* Enable silent RX mode. */
2340 		SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
2341 	} else {
2342 		if (pci_find_cap(sc->sis_dev, PCIY_PMG, &pmc) != 0)
2343 			return;
2344 		val = 0;
2345 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2346 			val |= SIS_PWRMAN_WOL_MAGIC;
2347 		CSR_WRITE_4(sc, SIS_PWRMAN_CTL, val);
2348 		/* Request PME. */
2349 		pmstat = pci_read_config(sc->sis_dev,
2350 		    pmc + PCIR_POWER_STATUS, 2);
2351 		pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2352 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2353 			pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2354 		pci_write_config(sc->sis_dev,
2355 		    pmc + PCIR_POWER_STATUS, pmstat, 2);
2356 	}
2357 }
2358 
2359 static void
2360 sis_add_sysctls(struct sis_softc *sc)
2361 {
2362 	struct sysctl_ctx_list *ctx;
2363 	struct sysctl_oid_list *children;
2364 	int unit;
2365 
2366 	ctx = device_get_sysctl_ctx(sc->sis_dev);
2367 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sis_dev));
2368 
2369 	unit = device_get_unit(sc->sis_dev);
2370 	/*
2371 	 * Unlike most other controllers, NS DP83815/DP83816 controllers
2372 	 * seem to pad with 0xFF when it encounter short frames.  According
2373 	 * to RFC 1042 the pad bytes should be 0x00.  Turning this tunable
2374 	 * on will have driver pad manully but it's disabled by default
2375 	 * because it will consume extra CPU cycles for short frames.
2376 	 */
2377 	sc->sis_manual_pad = 0;
2378 	SYSCTL_ADD_INT(ctx, children, OID_AUTO, "manual_pad",
2379 	    CTLFLAG_RWTUN, &sc->sis_manual_pad, 0, "Manually pad short frames");
2380 }
2381 
2382 static device_method_t sis_methods[] = {
2383 	/* Device interface */
2384 	DEVMETHOD(device_probe,		sis_probe),
2385 	DEVMETHOD(device_attach,	sis_attach),
2386 	DEVMETHOD(device_detach,	sis_detach),
2387 	DEVMETHOD(device_shutdown,	sis_shutdown),
2388 	DEVMETHOD(device_suspend,	sis_suspend),
2389 	DEVMETHOD(device_resume,	sis_resume),
2390 
2391 	/* MII interface */
2392 	DEVMETHOD(miibus_readreg,	sis_miibus_readreg),
2393 	DEVMETHOD(miibus_writereg,	sis_miibus_writereg),
2394 	DEVMETHOD(miibus_statchg,	sis_miibus_statchg),
2395 
2396 	DEVMETHOD_END
2397 };
2398 
2399 static driver_t sis_driver = {
2400 	"sis",
2401 	sis_methods,
2402 	sizeof(struct sis_softc)
2403 };
2404 
2405 static devclass_t sis_devclass;
2406 
2407 DRIVER_MODULE(sis, pci, sis_driver, sis_devclass, 0, 0);
2408 DRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);
2409