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