Eliminate use of curthread in if_ioctl functions by passing down the
[dragonfly.git] / sys / dev / netif / sis / if_sis.c
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/pci/if_sis.c,v 1.13.4.24 2003/03/05 18:42:33 njl Exp $
33  * $DragonFly: src/sys/dev/netif/sis/if_sis.c,v 1.10 2004/03/23 22:19:03 hsu Exp $
34  *
35  * $FreeBSD: src/sys/pci/if_sis.c,v 1.13.4.24 2003/03/05 18:42:33 njl Exp $
36  */
37
38 /*
39  * SiS 900/SiS 7016 fast ethernet PCI NIC driver. Datasheets are
40  * available from http://www.sis.com.tw.
41  *
42  * This driver also supports the NatSemi DP83815. Datasheets are
43  * available from http://www.national.com.
44  *
45  * Written by Bill Paul <wpaul@ee.columbia.edu>
46  * Electrical Engineering Department
47  * Columbia University, New York City
48  */
49
50 /*
51  * The SiS 900 is a fairly simple chip. It uses bus master DMA with
52  * simple TX and RX descriptors of 3 longwords in size. The receiver
53  * has a single perfect filter entry for the station address and a
54  * 128-bit multicast hash table. The SiS 900 has a built-in MII-based
55  * transceiver while the 7016 requires an external transceiver chip.
56  * Both chips offer the standard bit-bang MII interface as well as
57  * an enchanced PHY interface which simplifies accessing MII registers.
58  *
59  * The only downside to this chipset is that RX descriptors must be
60  * longword aligned.
61  */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/sockio.h>
66 #include <sys/mbuf.h>
67 #include <sys/malloc.h>
68 #include <sys/kernel.h>
69 #include <sys/socket.h>
70 #include <sys/sysctl.h>
71
72 #include <net/if.h>
73 #include <net/if_arp.h>
74 #include <net/ethernet.h>
75 #include <net/if_dl.h>
76 #include <net/if_media.h>
77 #include <net/if_types.h>
78 #include <net/vlan/if_vlan_var.h>
79
80 #include <net/bpf.h>
81
82 #include <vm/vm.h>              /* for vtophys */
83 #include <vm/pmap.h>            /* for vtophys */
84 #include <machine/clock.h>      /* for DELAY */
85 #include <machine/bus_pio.h>
86 #include <machine/bus_memio.h>
87 #include <machine/bus.h>
88 #include <machine/resource.h>
89 #include <sys/bus.h>
90 #include <sys/rman.h>
91
92 #include <dev/netif/mii_layer/mii.h>
93 #include <dev/netif/mii_layer/miivar.h>
94
95 #include <bus/pci/pcireg.h>
96 #include <bus/pci/pcivar.h>
97
98 #define SIS_USEIOSPACE
99
100 #include "if_sisreg.h"
101
102 /* "controller miibus0" required.  See GENERIC if you get errors here. */
103 #include "miibus_if.h"
104
105 /*
106  * Various supported device vendors/types and their names.
107  */
108 static struct sis_type sis_devs[] = {
109         { SIS_VENDORID, SIS_DEVICEID_900, "SiS 900 10/100BaseTX" },
110         { SIS_VENDORID, SIS_DEVICEID_7016, "SiS 7016 10/100BaseTX" },
111         { NS_VENDORID, NS_DEVICEID_DP83815, "NatSemi DP8381[56] 10/100BaseTX" },
112         { 0, 0, NULL }
113 };
114
115 static int      sis_probe(device_t);
116 static int      sis_attach(device_t);
117 static int      sis_detach(device_t);
118
119 static int      sis_newbuf(struct sis_softc *, struct sis_desc *,
120                            struct mbuf *);
121 static int      sis_encap(struct sis_softc *, struct mbuf *, uint32_t *);
122 static void     sis_rxeof(struct sis_softc *);
123 static void     sis_rxeoc(struct sis_softc *);
124 static void     sis_txeof(struct sis_softc *);
125 static void     sis_intr(void *);
126 static void     sis_tick(void *);
127 static void     sis_start(struct ifnet *);
128 static int      sis_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
129 static void     sis_init(void *);
130 static void     sis_stop(struct sis_softc *);
131 static void     sis_watchdog(struct ifnet *);
132 static void     sis_shutdown(device_t);
133 static int      sis_ifmedia_upd(struct ifnet *);
134 static void     sis_ifmedia_sts(struct ifnet *, struct ifmediareq *);
135
136 static uint16_t sis_reverse(uint16_t);
137 static void     sis_delay(struct sis_softc *);
138 static void     sis_eeprom_idle(struct sis_softc *);
139 static void     sis_eeprom_putbyte(struct sis_softc *, int);
140 static void     sis_eeprom_getword(struct sis_softc *, int, uint16_t *);
141 static void     sis_read_eeprom(struct sis_softc *, caddr_t, int, int, int);
142 #ifdef __i386__
143 static void     sis_read_cmos(struct sis_softc *, device_t, caddr_t, int, int);
144 static void     sis_read_mac(struct sis_softc *, device_t, caddr_t);
145 static device_t sis_find_bridge(device_t);
146 #endif
147
148 static void     sis_mii_sync(struct sis_softc *);
149 static void     sis_mii_send(struct sis_softc *, uint32_t, int);
150 static int      sis_mii_readreg(struct sis_softc *, struct sis_mii_frame *);
151 static int      sis_mii_writereg(struct sis_softc *, struct sis_mii_frame *);
152 static int      sis_miibus_readreg(device_t, int, int);
153 static int      sis_miibus_writereg(device_t, int, int, int);
154 static void     sis_miibus_statchg(device_t);
155
156 static void     sis_setmulti_sis(struct sis_softc *);
157 static void     sis_setmulti_ns(struct sis_softc *);
158 static uint32_t sis_mchash(struct sis_softc *, const uint8_t *);
159 static void     sis_reset(struct sis_softc *);
160 static int      sis_list_rx_init(struct sis_softc *);
161 static int      sis_list_tx_init(struct sis_softc *);
162 #ifdef SIS_USEIOSPACE
163 #define SIS_RES                 SYS_RES_IOPORT
164 #define SIS_RID                 SIS_PCI_LOIO
165 #else
166 #define SIS_RES                 SYS_RES_MEMORY
167 #define SIS_RID                 SIS_PCI_LOMEM
168 #endif
169
170 static device_method_t sis_methods[] = {
171         /* Device interface */
172         DEVMETHOD(device_probe,         sis_probe),
173         DEVMETHOD(device_attach,        sis_attach),
174         DEVMETHOD(device_detach,        sis_detach),
175         DEVMETHOD(device_shutdown,      sis_shutdown),
176
177         /* bus interface */
178         DEVMETHOD(bus_print_child,      bus_generic_print_child),
179         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
180
181         /* MII interface */
182         DEVMETHOD(miibus_readreg,       sis_miibus_readreg),
183         DEVMETHOD(miibus_writereg,      sis_miibus_writereg),
184         DEVMETHOD(miibus_statchg,       sis_miibus_statchg),
185
186         { 0, 0 }
187 };
188
189 static driver_t sis_driver = {
190         "sis",
191         sis_methods,
192         sizeof(struct sis_softc)
193 };
194
195 static devclass_t sis_devclass;
196
197 DECLARE_DUMMY_MODULE(if_sis);
198 DRIVER_MODULE(if_sis, pci, sis_driver, sis_devclass, 0, 0);
199 DRIVER_MODULE(miibus, sis, miibus_driver, miibus_devclass, 0, 0);
200
201 #define SIS_SETBIT(sc, reg, x)                          \
202         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
203
204 #define SIS_CLRBIT(sc, reg, x)                          \
205         CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
206
207 #define SIO_SET(x)                                      \
208         CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) | x)
209
210 #define SIO_CLR(x)                                      \
211         CSR_WRITE_4(sc, SIS_EECTL, CSR_READ_4(sc, SIS_EECTL) & ~x)
212
213 /*
214  * Routine to reverse the bits in a word. Stolen almost
215  * verbatim from /usr/games/fortune.
216  */
217 static uint16_t
218 sis_reverse(uint16_t n)
219 {
220         n = ((n >>  1) & 0x5555) | ((n <<  1) & 0xaaaa);
221         n = ((n >>  2) & 0x3333) | ((n <<  2) & 0xcccc);
222         n = ((n >>  4) & 0x0f0f) | ((n <<  4) & 0xf0f0);
223         n = ((n >>  8) & 0x00ff) | ((n <<  8) & 0xff00);
224
225         return(n);
226 }
227
228 static void
229 sis_delay(struct sis_softc *sc)
230 {
231         int idx;
232
233         for (idx = (300 / 33) + 1; idx > 0; idx--)
234                 CSR_READ_4(sc, SIS_CSR);
235 }
236
237 static void
238 sis_eeprom_idle(struct sis_softc *sc)
239 {
240         int i;
241
242         SIO_SET(SIS_EECTL_CSEL);
243         sis_delay(sc);
244         SIO_SET(SIS_EECTL_CLK);
245         sis_delay(sc);
246
247         for (i = 0; i < 25; i++) {
248                 SIO_CLR(SIS_EECTL_CLK);
249                 sis_delay(sc);
250                 SIO_SET(SIS_EECTL_CLK);
251                 sis_delay(sc);
252         }
253
254         SIO_CLR(SIS_EECTL_CLK);
255         sis_delay(sc);
256         SIO_CLR(SIS_EECTL_CSEL);
257         sis_delay(sc);
258         CSR_WRITE_4(sc, SIS_EECTL, 0x00000000);
259 }
260
261 /*
262  * Send a read command and address to the EEPROM, check for ACK.
263  */
264 static void
265 sis_eeprom_putbyte(struct sis_softc *sc, int addr)
266 {
267         int d, i;
268
269         d = addr | SIS_EECMD_READ;
270
271         /*
272          * Feed in each bit and stobe the clock.
273          */
274         for (i = 0x400; i; i >>= 1) {
275                 if (d & i)
276                         SIO_SET(SIS_EECTL_DIN);
277                 else
278                         SIO_CLR(SIS_EECTL_DIN);
279                 sis_delay(sc);
280                 SIO_SET(SIS_EECTL_CLK);
281                 sis_delay(sc);
282                 SIO_CLR(SIS_EECTL_CLK);
283                 sis_delay(sc);
284         }
285 }
286
287 /*
288  * Read a word of data stored in the EEPROM at address 'addr.'
289  */
290 static void
291 sis_eeprom_getword(struct sis_softc *sc, int addr, uint16_t *dest)
292 {
293         int i;
294         uint16_t word = 0;
295
296         /* Force EEPROM to idle state. */
297         sis_eeprom_idle(sc);
298
299         /* Enter EEPROM access mode. */
300         sis_delay(sc);
301         SIO_CLR(SIS_EECTL_CLK);
302         sis_delay(sc);
303         SIO_SET(SIS_EECTL_CSEL);
304         sis_delay(sc);
305
306         /*
307          * Send address of word we want to read.
308          */
309         sis_eeprom_putbyte(sc, addr);
310
311         /*
312          * Start reading bits from EEPROM.
313          */
314         for (i = 0x8000; i; i >>= 1) {
315                 SIO_SET(SIS_EECTL_CLK);
316                 sis_delay(sc);
317                 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECTL_DOUT)
318                         word |= i;
319                 sis_delay(sc);
320                 SIO_CLR(SIS_EECTL_CLK);
321                 sis_delay(sc);
322         }
323
324         /* Turn off EEPROM access mode. */
325         sis_eeprom_idle(sc);
326
327         *dest = word;
328 }
329
330 /*
331  * Read a sequence of words from the EEPROM.
332  */
333 static void
334 sis_read_eeprom(struct sis_softc *sc, caddr_t dest, int off, int cnt, int swap)
335 {
336         int i;
337         uint16_t word = 0, *ptr;
338
339         for (i = 0; i < cnt; i++) {
340                 sis_eeprom_getword(sc, off + i, &word);
341                 ptr = (uint16_t *)(dest + (i * 2));
342                 if (swap)
343                         *ptr = ntohs(word);
344                 else
345                         *ptr = word;
346         }
347 }
348
349 #ifdef __i386__
350 static device_t
351 sis_find_bridge(device_t dev)
352 {
353         devclass_t pci_devclass;
354         device_t *pci_devices;
355         int pci_count = 0;
356         device_t *pci_children;
357         int pci_childcount = 0;
358         device_t *busp, *childp;
359         device_t child = NULL;
360         int i, j;
361
362         if ((pci_devclass = devclass_find("pci")) == NULL)
363                 return(NULL);
364
365         devclass_get_devices(pci_devclass, &pci_devices, &pci_count);
366
367         for (i = 0, busp = pci_devices; i < pci_count; i++, busp++) {
368                 pci_childcount = 0;
369                 device_get_children(*busp, &pci_children, &pci_childcount);
370                 for (j = 0, childp = pci_children; j < pci_childcount;
371                      j++, childp++) {
372                         if (pci_get_vendor(*childp) == SIS_VENDORID &&
373                             pci_get_device(*childp) == 0x0008) {
374                                 child = *childp;
375                                 goto done;
376                         }
377                 }
378         }
379
380 done:
381         free(pci_devices, M_TEMP);
382         free(pci_children, M_TEMP);
383         return(child);
384 }
385
386 static void
387 sis_read_cmos(struct sis_softc *sc, device_t dev, caddr_t dest, int off,
388               int cnt)
389 {
390         device_t bridge;
391         uint8_t reg;
392         int i;
393         bus_space_tag_t btag;
394
395         bridge = sis_find_bridge(dev);
396         if (bridge == NULL)
397                 return;
398         reg = pci_read_config(bridge, 0x48, 1);
399         pci_write_config(bridge, 0x48, reg|0x40, 1);
400
401         /* XXX */
402         btag = I386_BUS_SPACE_IO;
403
404         for (i = 0; i < cnt; i++) {
405                 bus_space_write_1(btag, 0x0, 0x70, i + off);
406                 *(dest + i) = bus_space_read_1(btag, 0x0, 0x71);
407         }
408
409         pci_write_config(bridge, 0x48, reg & ~0x40, 1);
410 }
411
412 static void
413 sis_read_mac(struct sis_softc *sc, device_t dev, caddr_t dest)
414 {
415         uint32_t filtsave, csrsave;
416
417         filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
418         csrsave = CSR_READ_4(sc, SIS_CSR);
419
420         CSR_WRITE_4(sc, SIS_CSR, SIS_CSR_RELOAD | filtsave);
421         CSR_WRITE_4(sc, SIS_CSR, 0);
422                 
423         CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave & ~SIS_RXFILTCTL_ENABLE);
424
425         CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
426         ((uint16_t *)dest)[0] = CSR_READ_2(sc, SIS_RXFILT_DATA);
427         CSR_WRITE_4(sc, SIS_RXFILT_CTL,SIS_FILTADDR_PAR1);
428         ((uint16_t *)dest)[1] = CSR_READ_2(sc, SIS_RXFILT_DATA);
429         CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
430         ((uint16_t *)dest)[2] = CSR_READ_2(sc, SIS_RXFILT_DATA);
431
432         CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
433         CSR_WRITE_4(sc, SIS_CSR, csrsave);
434 }
435 #endif
436
437 /*
438  * Sync the PHYs by setting data bit and strobing the clock 32 times.
439  */
440 static void
441 sis_mii_sync(struct sis_softc *sc)
442 {
443         int i;
444
445         SIO_SET(SIS_MII_DIR|SIS_MII_DATA);
446
447         for (i = 0; i < 32; i++) {
448                 SIO_SET(SIS_MII_CLK);
449                 DELAY(1);
450                 SIO_CLR(SIS_MII_CLK);
451                 DELAY(1);
452         }
453 }
454
455 /*
456  * Clock a series of bits through the MII.
457  */
458 static void
459 sis_mii_send(struct sis_softc *sc, uint32_t bits, int cnt)
460 {
461         int i;
462
463         SIO_CLR(SIS_MII_CLK);
464
465         for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
466                 if (bits & i)
467                         SIO_SET(SIS_MII_DATA);
468                 else
469                         SIO_CLR(SIS_MII_DATA);
470                 DELAY(1);
471                 SIO_CLR(SIS_MII_CLK);
472                 DELAY(1);
473                 SIO_SET(SIS_MII_CLK);
474         }
475 }
476
477 /*
478  * Read an PHY register through the MII.
479  */
480 static int
481 sis_mii_readreg(struct sis_softc *sc, struct sis_mii_frame *frame)
482 {
483         int i, ack, s;
484
485         s = splimp();
486
487         /*
488          * Set up frame for RX.
489          */
490         frame->mii_stdelim = SIS_MII_STARTDELIM;
491         frame->mii_opcode = SIS_MII_READOP;
492         frame->mii_turnaround = 0;
493         frame->mii_data = 0;
494         
495         /*
496          * Turn on data xmit.
497          */
498         SIO_SET(SIS_MII_DIR);
499
500         sis_mii_sync(sc);
501
502         /*
503          * Send command/address info.
504          */
505         sis_mii_send(sc, frame->mii_stdelim, 2);
506         sis_mii_send(sc, frame->mii_opcode, 2);
507         sis_mii_send(sc, frame->mii_phyaddr, 5);
508         sis_mii_send(sc, frame->mii_regaddr, 5);
509
510         /* Idle bit */
511         SIO_CLR((SIS_MII_CLK|SIS_MII_DATA));
512         DELAY(1);
513         SIO_SET(SIS_MII_CLK);
514         DELAY(1);
515
516         /* Turn off xmit. */
517         SIO_CLR(SIS_MII_DIR);
518
519         /* Check for ack */
520         SIO_CLR(SIS_MII_CLK);
521         DELAY(1);
522         ack = CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA;
523         SIO_SET(SIS_MII_CLK);
524         DELAY(1);
525
526         /*
527          * Now try reading data bits. If the ack failed, we still
528          * need to clock through 16 cycles to keep the PHY(s) in sync.
529          */
530         if (ack) {
531                 for(i = 0; i < 16; i++) {
532                         SIO_CLR(SIS_MII_CLK);
533                         DELAY(1);
534                         SIO_SET(SIS_MII_CLK);
535                         DELAY(1);
536                 }
537                 goto fail;
538         }
539
540         for (i = 0x8000; i; i >>= 1) {
541                 SIO_CLR(SIS_MII_CLK);
542                 DELAY(1);
543                 if (!ack) {
544                         if (CSR_READ_4(sc, SIS_EECTL) & SIS_MII_DATA)
545                                 frame->mii_data |= i;
546                         DELAY(1);
547                 }
548                 SIO_SET(SIS_MII_CLK);
549                 DELAY(1);
550         }
551
552 fail:
553
554         SIO_CLR(SIS_MII_CLK);
555         DELAY(1);
556         SIO_SET(SIS_MII_CLK);
557         DELAY(1);
558
559         splx(s);
560
561         if (ack)
562                 return(1);
563         return(0);
564 }
565
566 /*
567  * Write to a PHY register through the MII.
568  */
569 static int
570 sis_mii_writereg(struct sis_softc *sc, struct sis_mii_frame *frame)
571 {
572         int s;
573
574         s = splimp();
575         /*
576          * Set up frame for TX.
577          */
578
579         frame->mii_stdelim = SIS_MII_STARTDELIM;
580         frame->mii_opcode = SIS_MII_WRITEOP;
581         frame->mii_turnaround = SIS_MII_TURNAROUND;
582
583         /*
584          * Turn on data output.
585          */
586         SIO_SET(SIS_MII_DIR);
587
588         sis_mii_sync(sc);
589
590         sis_mii_send(sc, frame->mii_stdelim, 2);
591         sis_mii_send(sc, frame->mii_opcode, 2);
592         sis_mii_send(sc, frame->mii_phyaddr, 5);
593         sis_mii_send(sc, frame->mii_regaddr, 5);
594         sis_mii_send(sc, frame->mii_turnaround, 2);
595         sis_mii_send(sc, frame->mii_data, 16);
596
597         /* Idle bit. */
598         SIO_SET(SIS_MII_CLK);
599         DELAY(1);
600         SIO_CLR(SIS_MII_CLK);
601         DELAY(1);
602
603         /*
604          * Turn off xmit.
605          */
606         SIO_CLR(SIS_MII_DIR);
607
608         splx(s);
609
610         return(0);
611 }
612
613 static int
614 sis_miibus_readreg(device_t dev, int phy, int reg)
615 {
616         struct sis_softc *sc;
617         struct sis_mii_frame frame;
618
619         sc = device_get_softc(dev);
620
621         if (sc->sis_type == SIS_TYPE_83815) {
622                 if (phy != 0)
623                         return(0);
624                 /*
625                  * The NatSemi chip can take a while after
626                  * a reset to come ready, during which the BMSR
627                  * returns a value of 0. This is *never* supposed
628                  * to happen: some of the BMSR bits are meant to
629                  * be hardwired in the on position, and this can
630                  * confuse the miibus code a bit during the probe
631                  * and attach phase. So we make an effort to check
632                  * for this condition and wait for it to clear.
633                  */
634                 if (!CSR_READ_4(sc, NS_BMSR))
635                         DELAY(1000);
636                 return CSR_READ_4(sc, NS_BMCR + (reg * 4));
637         }
638         /*
639          * Chipsets < SIS_635 seem not to be able to read/write
640          * through mdio. Use the enhanced PHY access register
641          * again for them.
642          */
643         if (sc->sis_type == SIS_TYPE_900 &&
644             sc->sis_rev < SIS_REV_635) {
645                 int i, val = 0;
646
647                 if (phy != 0)
648                         return(0);
649
650                 CSR_WRITE_4(sc, SIS_PHYCTL,
651                     (phy << 11) | (reg << 6) | SIS_PHYOP_READ);
652                 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
653
654                 for (i = 0; i < SIS_TIMEOUT; i++) {
655                         if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
656                                 break;
657                 }
658
659                 if (i == SIS_TIMEOUT) {
660                         printf("sis%d: PHY failed to come ready\n",
661                             sc->sis_unit);
662                         return(0);
663                 }
664
665                 val = (CSR_READ_4(sc, SIS_PHYCTL) >> 16) & 0xFFFF;
666
667                 if (val == 0xFFFF)
668                         return(0);
669
670                 return(val);
671         } else {
672                 bzero((char *)&frame, sizeof(frame));
673
674                 frame.mii_phyaddr = phy;
675                 frame.mii_regaddr = reg;
676                 sis_mii_readreg(sc, &frame);
677
678                 return(frame.mii_data);
679         }
680 }
681
682 static int
683 sis_miibus_writereg(device_t dev, int phy, int reg, int data)
684 {
685         struct sis_softc *sc;
686         struct sis_mii_frame frame;
687
688         sc = device_get_softc(dev);
689
690         if (sc->sis_type == SIS_TYPE_83815) {
691                 if (phy != 0)
692                         return(0);
693                 CSR_WRITE_4(sc, NS_BMCR + (reg * 4), data);
694                 return(0);
695         }
696
697         if (sc->sis_type == SIS_TYPE_900 &&
698             sc->sis_rev < SIS_REV_635) {
699                 int i;
700
701                 if (phy != 0)
702                         return(0);
703
704                 CSR_WRITE_4(sc, SIS_PHYCTL, (data << 16) | (phy << 11) |
705                     (reg << 6) | SIS_PHYOP_WRITE);
706                 SIS_SETBIT(sc, SIS_PHYCTL, SIS_PHYCTL_ACCESS);
707
708                 for (i = 0; i < SIS_TIMEOUT; i++) {
709                         if (!(CSR_READ_4(sc, SIS_PHYCTL) & SIS_PHYCTL_ACCESS))
710                                 break;
711                 }
712
713                 if (i == SIS_TIMEOUT)
714                         printf("sis%d: PHY failed to come ready\n",
715                             sc->sis_unit);
716         } else {
717                 bzero((char *)&frame, sizeof(frame));
718
719                 frame.mii_phyaddr = phy;
720                 frame.mii_regaddr = reg;
721                 frame.mii_data = data;
722                 sis_mii_writereg(sc, &frame);
723         }
724         return(0);
725 }
726
727 static void sis_miibus_statchg(device_t dev)
728 {
729         struct sis_softc *sc;
730
731         sc = device_get_softc(dev);
732         sis_init(sc);
733 }
734
735 static uint32_t
736 sis_mchash(struct sis_softc *sc, const uint8_t *addr)
737 {
738         uint32_t crc, carry; 
739         int i, j;
740         uint8_t c;
741
742         /* Compute CRC for the address value. */
743         crc = 0xFFFFFFFF; /* initial value */
744
745         for (i = 0; i < 6; i++) {
746                 c = *(addr + i);
747                 for (j = 0; j < 8; j++) {
748                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
749                         crc <<= 1;
750                         c >>= 1;
751                         if (carry)
752                                 crc = (crc ^ 0x04c11db6) | carry;
753                 }
754         }
755
756         /*
757          * return the filter bit position
758          *
759          * The NatSemi chip has a 512-bit filter, which is
760          * different than the SiS, so we special-case it.
761          */
762         if (sc->sis_type == SIS_TYPE_83815)
763                 return (crc >> 23);
764         else if (sc->sis_rev >= SIS_REV_635 || sc->sis_rev == SIS_REV_900B)
765                 return (crc >> 24);
766         else
767                 return (crc >> 25);
768 }
769
770 static void
771 sis_setmulti_ns(struct sis_softc *sc)
772 {
773         struct ifnet *ifp;
774         struct ifmultiaddr *ifma;
775         uint32_t h = 0, i, filtsave;
776         int bit, index;
777
778         ifp = &sc->arpcom.ac_if;
779
780         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
781                 SIS_CLRBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
782                 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
783                 return;
784         }
785
786         /*
787          * We have to explicitly enable the multicast hash table
788          * on the NatSemi chip if we want to use it, which we do.
789          */
790         SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_MCHASH);
791         SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLMULTI);
792
793         filtsave = CSR_READ_4(sc, SIS_RXFILT_CTL);
794
795         /* first, zot all the existing hash bits */
796         for (i = 0; i < 32; i++) {
797                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + (i*2));
798                 CSR_WRITE_4(sc, SIS_RXFILT_DATA, 0);
799         }
800
801         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
802                 if (ifma->ifma_addr->sa_family != AF_LINK)
803                         continue;
804                 h = sis_mchash(sc,
805                                LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
806                 index = h >> 3;
807                 bit = h & 0x1F;
808                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_FMEM_LO + index);
809                 if (bit > 0xF)
810                         bit -= 0x10;
811                 SIS_SETBIT(sc, SIS_RXFILT_DATA, (1 << bit));
812         }
813
814         CSR_WRITE_4(sc, SIS_RXFILT_CTL, filtsave);
815 }
816
817 static void
818 sis_setmulti_sis(struct sis_softc *sc)
819 {
820         struct ifnet *ifp;
821         struct ifmultiaddr *ifma;
822         uint32_t h, i, n, ctl;
823         uint16_t hashes[16];
824
825         ifp = &sc->arpcom.ac_if;
826
827         /* hash table size */
828         if (sc->sis_rev >= SIS_REV_635 || sc->sis_rev == SIS_REV_900B)
829                 n = 16;
830         else
831                 n = 8;
832
833         ctl = CSR_READ_4(sc, SIS_RXFILT_CTL) & SIS_RXFILTCTL_ENABLE;
834
835         if (ifp->if_flags & IFF_BROADCAST)
836                 ctl |= SIS_RXFILTCTL_BROAD;
837
838         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
839                 ctl |= SIS_RXFILTCTL_ALLMULTI;
840                 if (ifp->if_flags & IFF_PROMISC)
841                         ctl |= SIS_RXFILTCTL_BROAD|SIS_RXFILTCTL_ALLPHYS;
842                 for (i = 0; i < n; i++)
843                         hashes[i] = ~0;
844         } else {
845                 for (i = 0; i < n; i++)
846                         hashes[i] = 0;
847                 i = 0;
848                 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
849                         if (ifma->ifma_addr->sa_family != AF_LINK)
850                                 continue;
851                         h = sis_mchash(sc,
852                             LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
853                         hashes[h >> 4] |= 1 << (h & 0xf);
854                         i++;
855                 }
856                 if (i > n) {
857                         ctl |= SIS_RXFILTCTL_ALLMULTI;
858                         for (i = 0; i < n; i++)
859                                 hashes[i] = ~0;
860                 }
861         }
862
863         for (i = 0; i < n; i++) {
864                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, (4 + i) << 16);
865                 CSR_WRITE_4(sc, SIS_RXFILT_DATA, hashes[i]);
866         }
867
868         CSR_WRITE_4(sc, SIS_RXFILT_CTL, ctl);
869 }
870
871 static void
872 sis_reset(struct sis_softc *sc)
873 {
874         int i;
875
876         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RESET);
877
878         for (i = 0; i < SIS_TIMEOUT; i++) {
879                 if (!(CSR_READ_4(sc, SIS_CSR) & SIS_CSR_RESET))
880                         break;
881         }
882
883         if (i == SIS_TIMEOUT)
884                 printf("sis%d: reset never completed\n", sc->sis_unit);
885
886         /* Wait a little while for the chip to get its brains in order. */
887         DELAY(1000);
888
889         /*
890          * If this is a NetSemi chip, make sure to clear
891          * PME mode.
892          */
893         if (sc->sis_type == SIS_TYPE_83815) {
894                 CSR_WRITE_4(sc, NS_CLKRUN, NS_CLKRUN_PMESTS);
895                 CSR_WRITE_4(sc, NS_CLKRUN, 0);
896         }
897 }
898
899 /*
900  * Probe for an SiS chip. Check the PCI vendor and device
901  * IDs against our list and return a device name if we find a match.
902  */
903 static int
904 sis_probe(device_t dev)
905 {
906         struct sis_type *t;
907
908         t = sis_devs;
909
910         while(t->sis_name != NULL) {
911                 if ((pci_get_vendor(dev) == t->sis_vid) &&
912                     (pci_get_device(dev) == t->sis_did)) {
913                         device_set_desc(dev, t->sis_name);
914                         return(0);
915                 }
916                 t++;
917         }
918
919         return(ENXIO);
920 }
921
922 /*
923  * Attach the interface. Allocate softc structures, do ifmedia
924  * setup and ethernet/BPF attach.
925  */
926 static int
927 sis_attach(device_t dev)
928 {
929         int s;
930         uint8_t eaddr[ETHER_ADDR_LEN];
931         uint32_t command;
932         struct sis_softc *sc;
933         struct ifnet *ifp;
934         int unit, error, rid, waittime;
935
936         s = splimp();
937
938         error = waittime = 0;
939         sc = device_get_softc(dev);
940         unit = device_get_unit(dev);
941         bzero(sc, sizeof(struct sis_softc));
942
943         if (pci_get_device(dev) == SIS_DEVICEID_900)
944                 sc->sis_type = SIS_TYPE_900;
945         if (pci_get_device(dev) == SIS_DEVICEID_7016)
946                 sc->sis_type = SIS_TYPE_7016;
947         if (pci_get_vendor(dev) == NS_VENDORID)
948                 sc->sis_type = SIS_TYPE_83815;
949
950         sc->sis_rev = pci_read_config(dev, PCIR_REVID, 1);
951
952         /*
953          * Handle power management nonsense.
954          */
955
956         command = pci_read_config(dev, SIS_PCI_CAPID, 4) & 0x000000FF;
957         if (command == 0x01) {
958
959                 command = pci_read_config(dev, SIS_PCI_PWRMGMTCTRL, 4);
960                 if (command & SIS_PSTATE_MASK) {
961                         uint32_t                iobase, membase, irq;
962
963                         /* Save important PCI config data. */
964                         iobase = pci_read_config(dev, SIS_PCI_LOIO, 4);
965                         membase = pci_read_config(dev, SIS_PCI_LOMEM, 4);
966                         irq = pci_read_config(dev, SIS_PCI_INTLINE, 4);
967
968                         /* Reset the power state. */
969                         printf("sis%d: chip is in D%d power mode "
970                         "-- setting to D0\n", unit, command & SIS_PSTATE_MASK);
971                         command &= 0xFFFFFFFC;
972                         pci_write_config(dev, SIS_PCI_PWRMGMTCTRL, command, 4);
973
974                         /* Restore PCI config data. */
975                         pci_write_config(dev, SIS_PCI_LOIO, iobase, 4);
976                         pci_write_config(dev, SIS_PCI_LOMEM, membase, 4);
977                         pci_write_config(dev, SIS_PCI_INTLINE, irq, 4);
978                 }
979         }
980
981         /*
982          * Map control/status registers.
983          */
984         command = pci_read_config(dev, PCIR_COMMAND, 4);
985         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
986         pci_write_config(dev, PCIR_COMMAND, command, 4);
987         command = pci_read_config(dev, PCIR_COMMAND, 4);
988
989 #ifdef SIS_USEIOSPACE
990         if (!(command & PCIM_CMD_PORTEN)) {
991                 printf("sis%d: failed to enable I/O ports!\n", unit);
992                 error = ENXIO;;
993                 goto fail;
994         }
995 #else
996         if (!(command & PCIM_CMD_MEMEN)) {
997                 printf("sis%d: failed to enable memory mapping!\n", unit);
998                 error = ENXIO;;
999                 goto fail;
1000         }
1001 #endif
1002
1003         rid = SIS_RID;
1004         sc->sis_res = bus_alloc_resource(dev, SIS_RES, &rid,
1005             0, ~0, 1, RF_ACTIVE);
1006
1007         if (sc->sis_res == NULL) {
1008                 printf("sis%d: couldn't map ports/memory\n", unit);
1009                 error = ENXIO;
1010                 goto fail;
1011         }
1012
1013         sc->sis_btag = rman_get_bustag(sc->sis_res);
1014         sc->sis_bhandle = rman_get_bushandle(sc->sis_res);
1015
1016         /* Allocate interrupt */
1017         rid = 0;
1018         sc->sis_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1019             RF_SHAREABLE | RF_ACTIVE);
1020
1021         if (sc->sis_irq == NULL) {
1022                 printf("sis%d: couldn't map interrupt\n", unit);
1023                 bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1024                 error = ENXIO;
1025                 goto fail;
1026         }
1027
1028         error = bus_setup_intr(dev, sc->sis_irq, INTR_TYPE_NET,
1029             sis_intr, sc, &sc->sis_intrhand);
1030
1031         if (error) {
1032                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1033                 bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1034                 printf("sis%d: couldn't set up irq\n", unit);
1035                 goto fail;
1036         }
1037
1038         /* Reset the adapter. */
1039         sis_reset(sc);
1040
1041         if (sc->sis_type == SIS_TYPE_900 &&
1042             (sc->sis_rev == SIS_REV_635 ||
1043              sc->sis_rev == SIS_REV_900B)) {
1044                 SIO_SET(SIS_CFG_RND_CNT);
1045                 SIO_SET(SIS_CFG_PERR_DETECT);
1046         }
1047
1048         /*
1049          * Get station address from the EEPROM.
1050          */
1051         switch (pci_get_vendor(dev)) {
1052         case NS_VENDORID:
1053                 /*
1054                  * Reading the MAC address out of the EEPROM on
1055                  * the NatSemi chip takes a bit more work than
1056                  * you'd expect. The address spans 4 16-bit words,
1057                  * with the first word containing only a single bit.
1058                  * You have to shift everything over one bit to
1059                  * get it aligned properly. Also, the bits are
1060                  * stored backwards (the LSB is really the MSB,
1061                  * and so on) so you have to reverse them in order
1062                  * to get the MAC address into the form we want.
1063                  * Why? Who the hell knows.
1064                  */
1065                 {
1066                         uint16_t                tmp[4];
1067
1068                         sis_read_eeprom(sc, (caddr_t)&tmp,
1069                             NS_EE_NODEADDR, 4, 0);
1070
1071                         /* Shift everything over one bit. */
1072                         tmp[3] = tmp[3] >> 1;
1073                         tmp[3] |= tmp[2] << 15;
1074                         tmp[2] = tmp[2] >> 1;
1075                         tmp[2] |= tmp[1] << 15;
1076                         tmp[1] = tmp[1] >> 1;
1077                         tmp[1] |= tmp[0] << 15;
1078
1079                         /* Now reverse all the bits. */
1080                         tmp[3] = sis_reverse(tmp[3]);
1081                         tmp[2] = sis_reverse(tmp[2]);
1082                         tmp[1] = sis_reverse(tmp[1]);
1083
1084                         bcopy((char *)&tmp[1], eaddr, ETHER_ADDR_LEN);
1085                 }
1086                 break;
1087         case SIS_VENDORID:
1088         default:
1089 #ifdef __i386__
1090                 /*
1091                  * If this is a SiS 630E chipset with an embedded
1092                  * SiS 900 controller, we have to read the MAC address
1093                  * from the APC CMOS RAM. Our method for doing this
1094                  * is very ugly since we have to reach out and grab
1095                  * ahold of hardware for which we cannot properly
1096                  * allocate resources. This code is only compiled on
1097                  * the i386 architecture since the SiS 630E chipset
1098                  * is for x86 motherboards only. Note that there are
1099                  * a lot of magic numbers in this hack. These are
1100                  * taken from SiS's Linux driver. I'd like to replace
1101                  * them with proper symbolic definitions, but that
1102                  * requires some datasheets that I don't have access
1103                  * to at the moment.
1104                  */
1105                 if (sc->sis_rev == SIS_REV_630S ||
1106                     sc->sis_rev == SIS_REV_630E ||
1107                     sc->sis_rev == SIS_REV_630EA1)
1108                         sis_read_cmos(sc, dev, (caddr_t)&eaddr, 0x9, 6);
1109
1110                 else if (sc->sis_rev == SIS_REV_635 ||
1111                          sc->sis_rev == SIS_REV_630ET)
1112                         sis_read_mac(sc, dev, (caddr_t)&eaddr);
1113                 else if (sc->sis_rev == SIS_REV_96x) {
1114                         /*
1115                          * Allow to read EEPROM from LAN. It is shared
1116                          * between a 1394 controller and the NIC and each
1117                          * time we access it, we need to set SIS_EECMD_REQ.
1118                          */
1119                         SIO_SET(SIS_EECMD_REQ);
1120                         for (waittime = 0; waittime < SIS_TIMEOUT;
1121                             waittime++) {
1122                                 /* Force EEPROM to idle state. */
1123                                 sis_eeprom_idle(sc);
1124                                 if (CSR_READ_4(sc, SIS_EECTL) & SIS_EECMD_GNT) {
1125                                         sis_read_eeprom(sc, (caddr_t)&eaddr,
1126                                             SIS_EE_NODEADDR, 3, 0);
1127                                         break;
1128                                 }
1129                                 DELAY(1);
1130                         }
1131                         /*
1132                          * Set SIS_EECTL_CLK to high, so a other master
1133                          * can operate on the i2c bus.
1134                          */
1135                         SIO_SET(SIS_EECTL_CLK);
1136                         /* Refuse EEPROM access by LAN */
1137                         SIO_SET(SIS_EECMD_DONE);
1138                 } else
1139 #endif
1140                         sis_read_eeprom(sc, (caddr_t)&eaddr,
1141                             SIS_EE_NODEADDR, 3, 0);
1142                 break;
1143         }
1144
1145         /*
1146          * A SiS chip was detected. Inform the world.
1147          */
1148         printf("sis%d: Ethernet address: %6D\n", unit, eaddr, ":");
1149
1150         sc->sis_unit = unit;
1151         callout_handle_init(&sc->sis_stat_ch);
1152
1153         sc->sis_ldata = contigmalloc(sizeof(struct sis_list_data), M_DEVBUF,
1154             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1155
1156         if (sc->sis_ldata == NULL) {
1157                 printf("sis%d: no memory for list buffers!\n", unit);
1158                 bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1159                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1160                 bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1161                 error = ENXIO;
1162                 goto fail;
1163         }
1164         bzero(sc->sis_ldata, sizeof(struct sis_list_data));
1165
1166         ifp = &sc->arpcom.ac_if;
1167         ifp->if_softc = sc;
1168         if_initname(ifp, "sis", unit);
1169         ifp->if_mtu = ETHERMTU;
1170         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1171         ifp->if_ioctl = sis_ioctl;
1172         ifp->if_output = ether_output;
1173         ifp->if_start = sis_start;
1174         ifp->if_watchdog = sis_watchdog;
1175         ifp->if_init = sis_init;
1176         ifp->if_baudrate = 10000000;
1177         ifp->if_snd.ifq_maxlen = SIS_TX_LIST_CNT - 1;
1178
1179         /*
1180          * Do MII setup.
1181          */
1182         if (mii_phy_probe(dev, &sc->sis_miibus,
1183             sis_ifmedia_upd, sis_ifmedia_sts)) {
1184                 printf("sis%d: MII without any PHY!\n", sc->sis_unit);
1185                 contigfree(sc->sis_ldata, sizeof(struct sis_list_data),
1186                     M_DEVBUF);
1187                 bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1188                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1189                 bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1190                 error = ENXIO;
1191                 goto fail;
1192         }
1193
1194         /*
1195          * Call MI attach routine.
1196          */
1197         ether_ifattach(ifp, eaddr);
1198         
1199         /*
1200          * Tell the upper layer(s) we support long frames.
1201          */
1202         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1203
1204         callout_handle_init(&sc->sis_stat_ch);
1205
1206 fail:
1207         splx(s);
1208         return(error);
1209 }
1210
1211 static int
1212 sis_detach(device_t dev)
1213 {
1214         struct sis_softc *sc;
1215         struct ifnet *ifp;
1216         int s;
1217
1218         s = splimp();
1219
1220         sc = device_get_softc(dev);
1221         ifp = &sc->arpcom.ac_if;
1222
1223         sis_reset(sc);
1224         sis_stop(sc);
1225         ether_ifdetach(ifp);
1226
1227         bus_generic_detach(dev);
1228         device_delete_child(dev, sc->sis_miibus);
1229
1230         bus_teardown_intr(dev, sc->sis_irq, sc->sis_intrhand);
1231         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sis_irq);
1232         bus_release_resource(dev, SIS_RES, SIS_RID, sc->sis_res);
1233
1234         contigfree(sc->sis_ldata, sizeof(struct sis_list_data), M_DEVBUF);
1235
1236         splx(s);
1237
1238         return(0);
1239 }
1240
1241 /*
1242  * Initialize the transmit descriptors.
1243  */
1244 static int
1245 sis_list_tx_init(struct sis_softc *sc)
1246 {
1247         struct sis_list_data *ld;
1248         struct sis_ring_data *cd;
1249         int i, nexti;
1250
1251         cd = &sc->sis_cdata;
1252         ld = sc->sis_ldata;
1253
1254         for (i = 0; i < SIS_TX_LIST_CNT; i++) {
1255                 nexti = (i == (SIS_TX_LIST_CNT - 1)) ? 0 : i+1;
1256                 ld->sis_tx_list[i].sis_nextdesc =
1257                             &ld->sis_tx_list[nexti];
1258                 ld->sis_tx_list[i].sis_next =
1259                             vtophys(&ld->sis_tx_list[nexti]);
1260                 ld->sis_tx_list[i].sis_mbuf = NULL;
1261                 ld->sis_tx_list[i].sis_ptr = 0;
1262                 ld->sis_tx_list[i].sis_ctl = 0;
1263         }
1264
1265         cd->sis_tx_prod = cd->sis_tx_cons = cd->sis_tx_cnt = 0;
1266
1267         return(0);
1268 }
1269
1270 /*
1271  * Initialize the RX descriptors and allocate mbufs for them. Note that
1272  * we arrange the descriptors in a closed ring, so that the last descriptor
1273  * points back to the first.
1274  */
1275 static int
1276 sis_list_rx_init(struct sis_softc *sc)
1277 {
1278         struct sis_list_data *ld;
1279         struct sis_ring_data *cd;
1280         int i, nexti;
1281
1282         ld = sc->sis_ldata;
1283         cd = &sc->sis_cdata;
1284
1285         for (i = 0; i < SIS_RX_LIST_CNT; i++) {
1286                 if (sis_newbuf(sc, &ld->sis_rx_list[i], NULL) == ENOBUFS)
1287                         return(ENOBUFS);
1288                 nexti = (i == (SIS_RX_LIST_CNT - 1)) ? 0 : i+1;
1289                 ld->sis_rx_list[i].sis_nextdesc =
1290                             &ld->sis_rx_list[nexti];
1291                 ld->sis_rx_list[i].sis_next =
1292                             vtophys(&ld->sis_rx_list[nexti]);
1293         }
1294
1295         cd->sis_rx_prod = 0;
1296
1297         return(0);
1298 }
1299
1300 /*
1301  * Initialize an RX descriptor and attach an MBUF cluster.
1302  */
1303 static int
1304 sis_newbuf(struct sis_softc *sc, struct sis_desc *c, struct mbuf *m)
1305 {
1306         if (m == NULL) {
1307                 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1308                 if (m == NULL)
1309                         return(ENOBUFS);
1310         } else {
1311                 m->m_data = m->m_ext.ext_buf;
1312         }
1313
1314         c->sis_mbuf = m;
1315         c->sis_ptr = vtophys(mtod(m, caddr_t));
1316         c->sis_ctl = SIS_RXLEN;
1317
1318         return(0);
1319 }
1320
1321 /*
1322  * A frame has been uploaded: pass the resulting mbuf chain up to
1323  * the higher level protocols.
1324  */
1325 static void
1326 sis_rxeof(struct sis_softc *sc)
1327 {
1328         struct mbuf *m;
1329         struct ifnet *ifp;
1330         struct sis_desc *cur_rx;
1331         int i, total_len = 0;
1332         uint32_t rxstat;
1333
1334         ifp = &sc->arpcom.ac_if;
1335         i = sc->sis_cdata.sis_rx_prod;
1336
1337         while(SIS_OWNDESC(&sc->sis_ldata->sis_rx_list[i])) {
1338
1339 #ifdef DEVICE_POLLING
1340                 if (ifp->if_ipending & IFF_POLLING) {
1341                         if (sc->rxcycles <= 0)
1342                                 break;
1343                         sc->rxcycles--;
1344                 }
1345 #endif /* DEVICE_POLLING */
1346                 cur_rx = &sc->sis_ldata->sis_rx_list[i];
1347                 rxstat = cur_rx->sis_rxstat;
1348                 m = cur_rx->sis_mbuf;
1349                 cur_rx->sis_mbuf = NULL;
1350                 total_len = SIS_RXBYTES(cur_rx);
1351                 SIS_INC(i, SIS_RX_LIST_CNT);
1352
1353                 /*
1354                  * If an error occurs, update stats, clear the
1355                  * status word and leave the mbuf cluster in place:
1356                  * it should simply get re-used next time this descriptor
1357                  * comes up in the ring.
1358                  */
1359                 if (!(rxstat & SIS_CMDSTS_PKT_OK)) {
1360                         ifp->if_ierrors++;
1361                         if (rxstat & SIS_RXSTAT_COLL)
1362                                 ifp->if_collisions++;
1363                         sis_newbuf(sc, cur_rx, m);
1364                         continue;
1365                 }
1366
1367                 /* No errors; receive the packet. */
1368 #ifdef __i386__
1369                 /*
1370                  * On the x86 we do not have alignment problems, so try to
1371                  * allocate a new buffer for the receive ring, and pass up
1372                  * the one where the packet is already, saving the expensive
1373                  * copy done in m_devget().
1374                  * If we are on an architecture with alignment problems, or
1375                  * if the allocation fails, then use m_devget and leave the
1376                  * existing buffer in the receive ring.
1377                  */
1378                 if (sis_newbuf(sc, cur_rx, NULL) == 0)
1379                         m->m_pkthdr.len = m->m_len = total_len;
1380                 else
1381 #endif
1382                 {
1383                         struct mbuf *m0;
1384                         m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1385                                 total_len + ETHER_ALIGN, 0, ifp, NULL);
1386                         sis_newbuf(sc, cur_rx, m);
1387                         if (m0 == NULL) {
1388                                 ifp->if_ierrors++;
1389                                 continue;
1390                         }
1391                         m_adj(m0, ETHER_ALIGN);
1392                         m = m0;
1393                 }
1394
1395                 ifp->if_ipackets++;
1396                 ether_input(ifp, NULL, m);
1397         }
1398
1399         sc->sis_cdata.sis_rx_prod = i;
1400 }
1401
1402 static void
1403 sis_rxeoc(struct sis_softc *sc)
1404 {
1405         sis_rxeof(sc);
1406         /* sis_init(sc); */
1407 }
1408
1409 /*
1410  * A frame was downloaded to the chip. It's safe for us to clean up
1411  * the list buffers.
1412  */
1413
1414 static void
1415 sis_txeof(struct sis_softc *sc)
1416 {
1417         struct sis_desc *cur_tx = NULL;
1418         struct ifnet *ifp;
1419         uint32_t idx;
1420
1421         ifp = &sc->arpcom.ac_if;
1422
1423         /*
1424          * Go through our tx list and free mbufs for those
1425          * frames that have been transmitted.
1426          */
1427         idx = sc->sis_cdata.sis_tx_cons;
1428         while (idx != sc->sis_cdata.sis_tx_prod) {
1429                 cur_tx = &sc->sis_ldata->sis_tx_list[idx];
1430
1431                 if (SIS_OWNDESC(cur_tx))
1432                         break;
1433
1434                 if (cur_tx->sis_ctl & SIS_CMDSTS_MORE) {
1435                         sc->sis_cdata.sis_tx_cnt--;
1436                         SIS_INC(idx, SIS_TX_LIST_CNT);
1437                         continue;
1438                 }
1439
1440                 if (!(cur_tx->sis_ctl & SIS_CMDSTS_PKT_OK)) {
1441                         ifp->if_oerrors++;
1442                         if (cur_tx->sis_txstat & SIS_TXSTAT_EXCESSCOLLS)
1443                                 ifp->if_collisions++;
1444                         if (cur_tx->sis_txstat & SIS_TXSTAT_OUTOFWINCOLL)
1445                                 ifp->if_collisions++;
1446                 }
1447
1448                 ifp->if_collisions +=
1449                     (cur_tx->sis_txstat & SIS_TXSTAT_COLLCNT) >> 16;
1450
1451                 ifp->if_opackets++;
1452                 if (cur_tx->sis_mbuf != NULL) {
1453                         m_freem(cur_tx->sis_mbuf);
1454                         cur_tx->sis_mbuf = NULL;
1455                 }
1456
1457                 sc->sis_cdata.sis_tx_cnt--;
1458                 SIS_INC(idx, SIS_TX_LIST_CNT);
1459                 ifp->if_timer = 0;
1460         }
1461
1462         if (idx != sc->sis_cdata.sis_tx_cons) {
1463                 sc->sis_cdata.sis_tx_cons = idx;
1464                 ifp->if_flags &= ~IFF_OACTIVE;
1465         }
1466
1467         ifp->if_timer = (sc->sis_cdata.sis_tx_cnt == 0) ? 0 : 5;
1468 }
1469
1470 static void
1471 sis_tick(void *xsc)
1472 {
1473         struct sis_softc *sc;
1474         struct mii_data *mii;
1475         struct ifnet *ifp;
1476         int s;
1477
1478         s = splimp();
1479
1480         sc = xsc;
1481         ifp = &sc->arpcom.ac_if;
1482
1483         mii = device_get_softc(sc->sis_miibus);
1484         mii_tick(mii);
1485
1486         if (!sc->sis_link) {
1487                 mii_pollstat(mii);
1488                 if (mii->mii_media_status & IFM_ACTIVE &&
1489                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
1490                         sc->sis_link++;
1491                         if (ifp->if_snd.ifq_head != NULL)
1492                                 sis_start(ifp);
1493         }
1494
1495         sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1496
1497         splx(s);
1498 }
1499
1500 #ifdef DEVICE_POLLING
1501 static poll_handler_t sis_poll;
1502
1503 static void
1504 sis_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1505 {
1506         struct  sis_softc *sc = ifp->if_softc;
1507
1508         if (cmd == POLL_DEREGISTER) {   /* final call, enable interrupts */
1509                 CSR_WRITE_4(sc, SIS_IER, 1);
1510                 return;
1511         }
1512
1513         /*
1514          * On the sis, reading the status register also clears it.
1515          * So before returning to intr mode we must make sure that all
1516          * possible pending sources of interrupts have been served.
1517          * In practice this means run to completion the *eof routines,
1518          * and then call the interrupt routine
1519          */
1520         sc->rxcycles = count;
1521         sis_rxeof(sc);
1522         sis_txeof(sc);
1523         if (ifp->if_snd.ifq_head != NULL)
1524                 sis_start(ifp);
1525
1526         if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1527                 uint32_t status;
1528
1529                 /* Reading the ISR register clears all interrupts. */
1530                 status = CSR_READ_4(sc, SIS_ISR);
1531
1532                 if (status & (SIS_ISR_RX_ERR|SIS_ISR_RX_OFLOW))
1533                         sis_rxeoc(sc);
1534
1535                 if (status & (SIS_ISR_RX_IDLE))
1536                         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1537
1538                 if (status & SIS_ISR_SYSERR) {
1539                         sis_reset(sc);
1540                         sis_init(sc);
1541                 }
1542         }
1543 }
1544 #endif /* DEVICE_POLLING */
1545
1546 static void
1547 sis_intr(void *arg)
1548 {
1549         struct sis_softc *sc;
1550         struct ifnet *ifp;
1551         uint32_t status;
1552
1553         sc = arg;
1554         ifp = &sc->arpcom.ac_if;
1555
1556 #ifdef DEVICE_POLLING
1557         if (ifp->if_ipending & IFF_POLLING)
1558                 return;
1559         if (ether_poll_register(sis_poll, ifp)) { /* ok, disable interrupts */
1560                 CSR_WRITE_4(sc, SIS_IER, 0);
1561                 sis_poll(ifp, 0, 1);
1562                 return;
1563         }
1564 #endif /* DEVICE_POLLING */
1565
1566         /* Supress unwanted interrupts */
1567         if (!(ifp->if_flags & IFF_UP)) {
1568                 sis_stop(sc);
1569                 return;
1570         }
1571
1572         /* Disable interrupts. */
1573         CSR_WRITE_4(sc, SIS_IER, 0);
1574
1575         for (;;) {
1576                 /* Reading the ISR register clears all interrupts. */
1577                 status = CSR_READ_4(sc, SIS_ISR);
1578
1579                 if ((status & SIS_INTRS) == 0)
1580                         break;
1581
1582                 if (status &
1583                     (SIS_ISR_TX_DESC_OK | SIS_ISR_TX_ERR | SIS_ISR_TX_OK |
1584                      SIS_ISR_TX_IDLE) )
1585                         sis_txeof(sc);
1586
1587                 if (status &
1588                     (SIS_ISR_RX_DESC_OK | SIS_ISR_RX_OK | SIS_ISR_RX_IDLE))
1589                         sis_rxeof(sc);
1590
1591                 if (status & (SIS_ISR_RX_ERR | SIS_ISR_RX_OFLOW))
1592                         sis_rxeoc(sc);
1593
1594                 if (status & (SIS_ISR_RX_IDLE))
1595                         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1596
1597                 if (status & SIS_ISR_SYSERR) {
1598                         sis_reset(sc);
1599                         sis_init(sc);
1600                 }
1601         }
1602
1603         /* Re-enable interrupts. */
1604         CSR_WRITE_4(sc, SIS_IER, 1);
1605
1606         if (ifp->if_snd.ifq_head != NULL)
1607                 sis_start(ifp);
1608 }
1609
1610 /*
1611  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1612  * pointers to the fragment pointers.
1613  */
1614 static int
1615 sis_encap(struct sis_softc *sc, struct mbuf *m_head, uint32_t *txidx)
1616 {
1617         struct sis_desc *f = NULL;
1618         struct mbuf *m;
1619         int frag, cur, cnt = 0;
1620
1621         /*
1622          * Start packing the mbufs in this chain into
1623          * the fragment pointers. Stop when we run out
1624          * of fragments or hit the end of the mbuf chain.
1625          */
1626         m = m_head;
1627         cur = frag = *txidx;
1628
1629         for (m = m_head; m != NULL; m = m->m_next) {
1630                 if (m->m_len != 0) {
1631                         if ((SIS_TX_LIST_CNT -
1632                             (sc->sis_cdata.sis_tx_cnt + cnt)) < 2)
1633                                 return(ENOBUFS);
1634                         f = &sc->sis_ldata->sis_tx_list[frag];
1635                         f->sis_ctl = SIS_CMDSTS_MORE | m->m_len;
1636                         f->sis_ptr = vtophys(mtod(m, vm_offset_t));
1637                         if (cnt != 0)
1638                                 f->sis_ctl |= SIS_CMDSTS_OWN;
1639                         cur = frag;
1640                         SIS_INC(frag, SIS_TX_LIST_CNT);
1641                         cnt++;
1642                 }
1643         }
1644
1645         if (m != NULL)
1646                 return(ENOBUFS);
1647
1648         sc->sis_ldata->sis_tx_list[cur].sis_mbuf = m_head;
1649         sc->sis_ldata->sis_tx_list[cur].sis_ctl &= ~SIS_CMDSTS_MORE;
1650         sc->sis_ldata->sis_tx_list[*txidx].sis_ctl |= SIS_CMDSTS_OWN;
1651         sc->sis_cdata.sis_tx_cnt += cnt;
1652         *txidx = frag;
1653
1654         return(0);
1655 }
1656
1657 /*
1658  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1659  * to the mbuf data regions directly in the transmit lists. We also save a
1660  * copy of the pointers since the transmit list fragment pointers are
1661  * physical addresses.
1662  */
1663
1664 static void
1665 sis_start(struct ifnet *ifp)
1666 {
1667         struct sis_softc *sc;
1668         struct mbuf *m_head = NULL;
1669         uint32_t idx;
1670
1671         sc = ifp->if_softc;
1672
1673         if (!sc->sis_link)
1674                 return;
1675
1676         idx = sc->sis_cdata.sis_tx_prod;
1677
1678         if (ifp->if_flags & IFF_OACTIVE)
1679                 return;
1680
1681         while(sc->sis_ldata->sis_tx_list[idx].sis_mbuf == NULL) {
1682                 IF_DEQUEUE(&ifp->if_snd, m_head);
1683                 if (m_head == NULL)
1684                         break;
1685
1686                 if (sis_encap(sc, m_head, &idx)) {
1687                         IF_PREPEND(&ifp->if_snd, m_head);
1688                         ifp->if_flags |= IFF_OACTIVE;
1689                         break;
1690                 }
1691
1692                 /*
1693                  * If there's a BPF listener, bounce a copy of this frame
1694                  * to him.
1695                  */
1696                 BPF_MTAP(ifp, m_head);
1697         }
1698
1699         /* Transmit */
1700         sc->sis_cdata.sis_tx_prod = idx;
1701         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_ENABLE);
1702
1703         /*
1704          * Set a timeout in case the chip goes out to lunch.
1705          */
1706         ifp->if_timer = 5;
1707 }
1708
1709 static void
1710 sis_init(void *xsc)
1711 {
1712         struct sis_softc *sc = xsc;
1713         struct ifnet *ifp = &sc->arpcom.ac_if;
1714         struct mii_data *mii;
1715         int s;
1716
1717         s = splimp();
1718
1719         /*
1720          * Cancel pending I/O and free all RX/TX buffers.
1721          */
1722         sis_stop(sc);
1723
1724         mii = device_get_softc(sc->sis_miibus);
1725
1726         /* Set MAC address */
1727         if (sc->sis_type == SIS_TYPE_83815) {
1728                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR0);
1729                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1730                     ((uint16_t *)sc->arpcom.ac_enaddr)[0]);
1731                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR1);
1732                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1733                     ((uint16_t *)sc->arpcom.ac_enaddr)[1]);
1734                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, NS_FILTADDR_PAR2);
1735                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1736                     ((uint16_t *)sc->arpcom.ac_enaddr)[2]);
1737         } else {
1738                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR0);
1739                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1740                     ((uint16_t *)sc->arpcom.ac_enaddr)[0]);
1741                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR1);
1742                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1743                     ((uint16_t *)sc->arpcom.ac_enaddr)[1]);
1744                 CSR_WRITE_4(sc, SIS_RXFILT_CTL, SIS_FILTADDR_PAR2);
1745                 CSR_WRITE_4(sc, SIS_RXFILT_DATA,
1746                     ((uint16_t *)sc->arpcom.ac_enaddr)[2]);
1747         }
1748
1749         /* Init circular RX list. */
1750         if (sis_list_rx_init(sc) == ENOBUFS) {
1751                 printf("sis%d: initialization failed: no "
1752                         "memory for rx buffers\n", sc->sis_unit);
1753                 sis_stop(sc);
1754                 splx(s);
1755                 return;
1756         }
1757
1758         /*
1759          * Init tx descriptors.
1760          */
1761         sis_list_tx_init(sc);
1762
1763         /*
1764          * For the NatSemi chip, we have to explicitly enable the
1765          * reception of ARP frames, as well as turn on the 'perfect
1766          * match' filter where we store the station address, otherwise
1767          * we won't receive unicasts meant for this host.
1768          */
1769         if (sc->sis_type == SIS_TYPE_83815) {
1770                 SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_ARP);
1771                 SIS_SETBIT(sc, SIS_RXFILT_CTL, NS_RXFILTCTL_PERFECT);
1772         }
1773
1774          /* If we want promiscuous mode, set the allframes bit. */
1775         if (ifp->if_flags & IFF_PROMISC)
1776                 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1777         else
1778                 SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ALLPHYS);
1779
1780         /*
1781          * Set the capture broadcast bit to capture broadcast frames.
1782          */
1783         if (ifp->if_flags & IFF_BROADCAST)
1784                 SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1785         else
1786                 SIS_CLRBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_BROAD);
1787
1788         /*
1789          * Load the multicast filter.
1790          */
1791         if (sc->sis_type == SIS_TYPE_83815)
1792                 sis_setmulti_ns(sc);
1793         else
1794                 sis_setmulti_sis(sc);
1795
1796         /* Turn the receive filter on */
1797         SIS_SETBIT(sc, SIS_RXFILT_CTL, SIS_RXFILTCTL_ENABLE);
1798
1799         /*
1800          * Load the address of the RX and TX lists.
1801          */
1802         CSR_WRITE_4(sc, SIS_RX_LISTPTR,
1803             vtophys(&sc->sis_ldata->sis_rx_list[0]));
1804         CSR_WRITE_4(sc, SIS_TX_LISTPTR,
1805             vtophys(&sc->sis_ldata->sis_tx_list[0]));
1806
1807         /* SIS_CFG_EDB_MASTER_EN indicates the EDB bus is used instead of
1808          * the PCI bus. When this bit is set, the Max DMA Burst Size
1809          * for TX/RX DMA should be no larger than 16 double words.
1810          */
1811         if (CSR_READ_4(sc, SIS_CFG) & SIS_CFG_EDB_MASTER_EN)
1812                 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG64);
1813         else
1814                 CSR_WRITE_4(sc, SIS_RX_CFG, SIS_RXCFG256);
1815
1816         /* Accept Long Packets for VLAN support */
1817         SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_JABBER);
1818
1819         /* Set TX configuration */
1820         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
1821                 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_10);
1822         else
1823                 CSR_WRITE_4(sc, SIS_TX_CFG, SIS_TXCFG_100);
1824
1825         /* Set full/half duplex mode. */
1826         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1827                 SIS_SETBIT(sc, SIS_TX_CFG,
1828                     (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1829                 SIS_SETBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1830         } else {
1831                 SIS_CLRBIT(sc, SIS_TX_CFG,
1832                     (SIS_TXCFG_IGN_HBEAT|SIS_TXCFG_IGN_CARR));
1833                 SIS_CLRBIT(sc, SIS_RX_CFG, SIS_RXCFG_RX_TXPKTS);
1834         }
1835
1836         /*
1837          * Enable interrupts.
1838          */
1839         CSR_WRITE_4(sc, SIS_IMR, SIS_INTRS);
1840 #ifdef DEVICE_POLLING
1841         /*
1842          * ... only enable interrupts if we are not polling, make sure
1843          * they are off otherwise.
1844          */
1845         if (ifp->if_ipending & IFF_POLLING)
1846                 CSR_WRITE_4(sc, SIS_IER, 0);
1847         else
1848 #endif /* DEVICE_POLLING */
1849         CSR_WRITE_4(sc, SIS_IER, 1);
1850
1851         /* Enable receiver and transmitter. */
1852         SIS_CLRBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
1853         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_RX_ENABLE);
1854
1855 #ifdef notdef
1856         mii_mediachg(mii);
1857 #endif
1858
1859         /*
1860          * Page 75 of the DP83815 manual recommends the
1861          * following register settings "for optimum
1862          * performance." Note however that at least three
1863          * of the registers are listed as "reserved" in
1864          * the register map, so who knows what they do.
1865          */
1866         if (sc->sis_type == SIS_TYPE_83815) {
1867                 CSR_WRITE_4(sc, NS_PHY_PAGE, 0x0001);
1868                 CSR_WRITE_4(sc, NS_PHY_CR, 0x189C);
1869                 CSR_WRITE_4(sc, NS_PHY_TDATA, 0x0000);
1870                 CSR_WRITE_4(sc, NS_PHY_DSPCFG, 0x5040);
1871                 CSR_WRITE_4(sc, NS_PHY_SDCFG, 0x008C);
1872         }
1873
1874         ifp->if_flags |= IFF_RUNNING;
1875         ifp->if_flags &= ~IFF_OACTIVE;
1876
1877         splx(s);
1878
1879         sc->sis_stat_ch = timeout(sis_tick, sc, hz);
1880 }
1881
1882 /*
1883  * Set media options.
1884  */
1885 static int
1886 sis_ifmedia_upd(struct ifnet *ifp)
1887 {
1888         struct sis_softc *sc;
1889         struct mii_data *mii;
1890
1891         sc = ifp->if_softc;
1892
1893         mii = device_get_softc(sc->sis_miibus);
1894         sc->sis_link = 0;
1895         if (mii->mii_instance) {
1896                 struct mii_softc        *miisc;
1897                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1898                         mii_phy_reset(miisc);
1899         }
1900         mii_mediachg(mii);
1901
1902         return(0);
1903 }
1904
1905 /*
1906  * Report current media status.
1907  */
1908 static void
1909 sis_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1910 {
1911         struct sis_softc *sc;
1912         struct mii_data *mii;
1913
1914         sc = ifp->if_softc;
1915
1916         mii = device_get_softc(sc->sis_miibus);
1917         mii_pollstat(mii);
1918         ifmr->ifm_active = mii->mii_media_active;
1919         ifmr->ifm_status = mii->mii_media_status;
1920 }
1921
1922 static int
1923 sis_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1924 {
1925         struct sis_softc *sc = ifp->if_softc;
1926         struct ifreq *ifr = (struct ifreq *) data;
1927         struct mii_data *mii;
1928         int s, error = 0;
1929
1930         s = splimp();
1931
1932         switch(command) {
1933         case SIOCSIFADDR:
1934         case SIOCGIFADDR:
1935         case SIOCSIFMTU:
1936                 error = ether_ioctl(ifp, command, data);
1937                 break;
1938         case SIOCSIFFLAGS:
1939                 if (ifp->if_flags & IFF_UP) {
1940                         sis_init(sc);
1941                 } else {
1942                         if (ifp->if_flags & IFF_RUNNING)
1943                                 sis_stop(sc);
1944                 }
1945                 error = 0;
1946                 break;
1947         case SIOCADDMULTI:
1948         case SIOCDELMULTI:
1949                 if (sc->sis_type == SIS_TYPE_83815)
1950                         sis_setmulti_ns(sc);
1951                 else
1952                         sis_setmulti_sis(sc);
1953                 error = 0;
1954                 break;
1955         case SIOCGIFMEDIA:
1956         case SIOCSIFMEDIA:
1957                 mii = device_get_softc(sc->sis_miibus);
1958                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1959                 break;
1960         default:
1961                 error = EINVAL;
1962                 break;
1963         }
1964
1965         splx(s);
1966
1967         return(error);
1968 }
1969
1970 static void
1971 sis_watchdog(struct ifnet *ifp)
1972 {
1973         struct sis_softc *sc;
1974
1975         sc = ifp->if_softc;
1976
1977         ifp->if_oerrors++;
1978         printf("sis%d: watchdog timeout\n", sc->sis_unit);
1979
1980         sis_stop(sc);
1981         sis_reset(sc);
1982         sis_init(sc);
1983
1984         if (ifp->if_snd.ifq_head != NULL)
1985                 sis_start(ifp);
1986 }
1987
1988 /*
1989  * Stop the adapter and free any mbufs allocated to the
1990  * RX and TX lists.
1991  */
1992 static void
1993 sis_stop(struct sis_softc *sc)
1994 {
1995         int i;
1996         struct ifnet *ifp;
1997
1998         ifp = &sc->arpcom.ac_if;
1999         ifp->if_timer = 0;
2000
2001         untimeout(sis_tick, sc, sc->sis_stat_ch);
2002
2003         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2004 #ifdef DEVICE_POLLING
2005         ether_poll_deregister(ifp);
2006 #endif
2007         CSR_WRITE_4(sc, SIS_IER, 0);
2008         CSR_WRITE_4(sc, SIS_IMR, 0);
2009         SIS_SETBIT(sc, SIS_CSR, SIS_CSR_TX_DISABLE|SIS_CSR_RX_DISABLE);
2010         DELAY(1000);
2011         CSR_WRITE_4(sc, SIS_TX_LISTPTR, 0);
2012         CSR_WRITE_4(sc, SIS_RX_LISTPTR, 0);
2013
2014         sc->sis_link = 0;
2015
2016         /*
2017          * Free data in the RX lists.
2018          */
2019         for (i = 0; i < SIS_RX_LIST_CNT; i++) {
2020                 if (sc->sis_ldata->sis_rx_list[i].sis_mbuf != NULL) {
2021                         m_freem(sc->sis_ldata->sis_rx_list[i].sis_mbuf);
2022                         sc->sis_ldata->sis_rx_list[i].sis_mbuf = NULL;
2023                 }
2024         }
2025         bzero((char *)&sc->sis_ldata->sis_rx_list,
2026                 sizeof(sc->sis_ldata->sis_rx_list));
2027
2028         /*
2029          * Free the TX list buffers.
2030          */
2031         for (i = 0; i < SIS_TX_LIST_CNT; i++) {
2032                 if (sc->sis_ldata->sis_tx_list[i].sis_mbuf != NULL) {
2033                         m_freem(sc->sis_ldata->sis_tx_list[i].sis_mbuf);
2034                         sc->sis_ldata->sis_tx_list[i].sis_mbuf = NULL;
2035                 }
2036         }
2037
2038         bzero((char *)&sc->sis_ldata->sis_tx_list,
2039                 sizeof(sc->sis_ldata->sis_tx_list));
2040 }
2041
2042 /*
2043  * Stop all chip I/O so that the kernel's probe routines don't
2044  * get confused by errant DMAs when rebooting.
2045  */
2046 static void
2047 sis_shutdown(device_t dev)
2048 {
2049         struct sis_softc        *sc;
2050
2051         sc = device_get_softc(dev);
2052
2053         sis_reset(sc);
2054         sis_stop(sc);
2055 }