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