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