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