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