if_sip.c revision 1.11.4.8 1 /* $NetBSD: if_sip.c,v 1.11.4.8 2002/03/27 09:50:43 he Exp $ */
2
3 /*-
4 * Copyright (c) 1999 Network Computer, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Network Computer, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY NETWORK COMPUTER, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Device driver for the Silicon Integrated Systems SiS 900 and
34 * SiS 7016 10/100 PCI Ethernet controllers.
35 *
36 * Written by Jason R. Thorpe for Network Computer, Inc.
37 */
38
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53 #include <sys/queue.h>
54
55 #include <vm/vm.h> /* for PAGE_SIZE */
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #endif
65
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/if_inarp.h>
69 #endif
70
71 #ifdef NS
72 #include <netns/ns.h>
73 #include <netns/ns_if.h>
74 #endif
75
76 #include <machine/bus.h>
77 #include <machine/intr.h>
78 #include <machine/endian.h>
79
80 #include <dev/mii/mii.h>
81 #include <dev/mii/miivar.h>
82
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
85 #include <dev/pci/pcidevs.h>
86
87 #include <dev/pci/if_sipreg.h>
88
89 #if !defined(IF_POLL)
90 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
91 #endif
92
93 /*
94 * Transmit descriptor list size. This is arbitrary, but allocate
95 * enough descriptors for 64 pending transmissions, and 16 segments
96 * per packet. This MUST work out to a power of 2.
97 */
98 #define SIP_NTXSEGS 8
99
100 #define SIP_TXQUEUELEN 256
101 #define SIP_NTXDESC (SIP_TXQUEUELEN * SIP_NTXSEGS)
102 #define SIP_NTXDESC_MASK (SIP_NTXDESC - 1)
103 #define SIP_NEXTTX(x) (((x) + 1) & SIP_NTXDESC_MASK)
104
105 /*
106 * Receive descriptor list size. We have one Rx buffer per incoming
107 * packet, so this logic is a little simpler.
108 */
109 #define SIP_NRXDESC 128
110 #define SIP_NRXDESC_MASK (SIP_NRXDESC - 1)
111 #define SIP_NEXTRX(x) (((x) + 1) & SIP_NRXDESC_MASK)
112
113 /*
114 * Control structures are DMA'd to the SiS900 chip. We allocate them in
115 * a single clump that maps to a single DMA segment to make several things
116 * easier.
117 */
118 struct sip_control_data {
119 /*
120 * The transmit descriptors.
121 */
122 struct sip_desc scd_txdescs[SIP_NTXDESC];
123
124 /*
125 * The receive descriptors.
126 */
127 struct sip_desc scd_rxdescs[SIP_NRXDESC];
128 };
129
130 #define SIP_CDOFF(x) offsetof(struct sip_control_data, x)
131 #define SIP_CDTXOFF(x) SIP_CDOFF(scd_txdescs[(x)])
132 #define SIP_CDRXOFF(x) SIP_CDOFF(scd_rxdescs[(x)])
133
134 /*
135 * Software state for transmit jobs.
136 */
137 struct sip_txsoft {
138 struct mbuf *txs_mbuf; /* head of our mbuf chain */
139 bus_dmamap_t txs_dmamap; /* our DMA map */
140 int txs_firstdesc; /* first descriptor in packet */
141 int txs_lastdesc; /* last descriptor in packet */
142 SIMPLEQ_ENTRY(sip_txsoft) txs_q;
143 };
144
145 SIMPLEQ_HEAD(sip_txsq, sip_txsoft);
146
147 /*
148 * Software state for receive jobs.
149 */
150 struct sip_rxsoft {
151 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
152 bus_dmamap_t rxs_dmamap; /* our DMA map */
153 };
154
155 /*
156 * Software state per device.
157 */
158 struct sip_softc {
159 struct device sc_dev; /* generic device information */
160 bus_space_tag_t sc_st; /* bus space tag */
161 bus_space_handle_t sc_sh; /* bus space handle */
162 bus_dma_tag_t sc_dmat; /* bus DMA tag */
163 struct ethercom sc_ethercom; /* ethernet common data */
164 void *sc_sdhook; /* shutdown hook */
165
166 const struct sip_product *sc_model; /* which model are we? */
167
168 void *sc_ih; /* interrupt cookie */
169
170 struct mii_data sc_mii; /* MII/media information */
171
172 struct callout sc_tick_ch; /* tick callout */
173
174 bus_dmamap_t sc_cddmamap; /* control data DMA map */
175 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
176
177 /*
178 * Software state for transmit and receive descriptors.
179 */
180 struct sip_txsoft sc_txsoft[SIP_TXQUEUELEN];
181 struct sip_rxsoft sc_rxsoft[SIP_NRXDESC];
182
183 /*
184 * Control data structures.
185 */
186 struct sip_control_data *sc_control_data;
187 #define sc_txdescs sc_control_data->scd_txdescs
188 #define sc_rxdescs sc_control_data->scd_rxdescs
189
190 u_int32_t sc_txcfg; /* prototype TXCFG register */
191 u_int32_t sc_rxcfg; /* prototype RXCFG register */
192 u_int32_t sc_imr; /* prototype IMR register */
193 u_int32_t sc_rfcr; /* prototype RFCR register */
194
195 u_int32_t sc_tx_fill_thresh; /* transmit fill threshold */
196 u_int32_t sc_tx_drain_thresh; /* transmit drain threshold */
197
198 u_int32_t sc_rx_drain_thresh; /* receive drain threshold */
199
200 int sc_flags; /* misc. flags; see below */
201
202 int sc_txfree; /* number of free Tx descriptors */
203 int sc_txnext; /* next ready Tx descriptor */
204
205 struct sip_txsq sc_txfreeq; /* free Tx descsofts */
206 struct sip_txsq sc_txdirtyq; /* dirty Tx descsofts */
207
208 int sc_rxptr; /* next ready Rx descriptor/descsoft */
209 };
210
211 /* sc_flags */
212 #define SIPF_PAUSED 0x00000001 /* paused (802.3x flow control) */
213
214 #define SIP_CDTXADDR(sc, x) ((sc)->sc_cddma + SIP_CDTXOFF((x)))
215 #define SIP_CDRXADDR(sc, x) ((sc)->sc_cddma + SIP_CDRXOFF((x)))
216
217 #define SIP_CDTXSYNC(sc, x, n, ops) \
218 do { \
219 int __x, __n; \
220 \
221 __x = (x); \
222 __n = (n); \
223 \
224 /* If it will wrap around, sync to the end of the ring. */ \
225 if ((__x + __n) > SIP_NTXDESC) { \
226 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
227 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * \
228 (SIP_NTXDESC - __x), (ops)); \
229 __n -= (SIP_NTXDESC - __x); \
230 __x = 0; \
231 } \
232 \
233 /* Now sync whatever is left. */ \
234 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
235 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * __n, (ops)); \
236 } while (0)
237
238 #define SIP_CDRXSYNC(sc, x, ops) \
239 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
240 SIP_CDRXOFF((x)), sizeof(struct sip_desc), (ops))
241
242 /*
243 * Note we rely on MCLBYTES being a power of two below.
244 */
245 #define SIP_INIT_RXDESC(sc, x) \
246 do { \
247 struct sip_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
248 struct sip_desc *__sipd = &(sc)->sc_rxdescs[(x)]; \
249 \
250 __sipd->sipd_link = htole32(SIP_CDRXADDR((sc), SIP_NEXTRX((x)))); \
251 __sipd->sipd_bufptr = htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr); \
252 __sipd->sipd_cmdsts = htole32(CMDSTS_INTR | \
253 ((MCLBYTES - 1) & CMDSTS_SIZE_MASK)); \
254 SIP_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
255 } while (0)
256
257 #define SIP_TIMEOUT 1000
258
259 void sip_start __P((struct ifnet *));
260 void sip_watchdog __P((struct ifnet *));
261 int sip_ioctl __P((struct ifnet *, u_long, caddr_t));
262
263 void sip_shutdown __P((void *));
264
265 void sip_reset __P((struct sip_softc *));
266 int sip_init __P((struct sip_softc *));
267 void sip_stop __P((struct sip_softc *, int));
268 void sip_rxdrain __P((struct sip_softc *));
269 int sip_add_rxbuf __P((struct sip_softc *, int));
270 void sip_read_eeprom __P((struct sip_softc *, int, int, u_int16_t *));
271 void sip_tick __P((void *));
272
273 void sip_sis900_set_filter __P((struct sip_softc *));
274 void sip_dp83815_set_filter __P((struct sip_softc *));
275
276 void sip_sis900_read_macaddr __P((struct sip_softc *, u_int8_t *));
277 void sip_dp83815_read_macaddr __P((struct sip_softc *, u_int8_t *));
278
279 int sip_intr __P((void *));
280 void sip_txintr __P((struct sip_softc *));
281 void sip_rxintr __P((struct sip_softc *));
282
283 int sip_sis900_mii_readreg __P((struct device *, int, int));
284 void sip_sis900_mii_writereg __P((struct device *, int, int, int));
285 void sip_sis900_mii_statchg __P((struct device *));
286
287 int sip_dp83815_mii_readreg __P((struct device *, int, int));
288 void sip_dp83815_mii_writereg __P((struct device *, int, int, int));
289 void sip_dp83815_mii_statchg __P((struct device *));
290
291 int sip_mediachange __P((struct ifnet *));
292 void sip_mediastatus __P((struct ifnet *, struct ifmediareq *));
293
294 int sip_match __P((struct device *, struct cfdata *, void *));
295 void sip_attach __P((struct device *, struct device *, void *));
296
297 int sip_copy_small = 0;
298
299 struct cfattach sip_ca = {
300 sizeof(struct sip_softc), sip_match, sip_attach,
301 };
302
303 /*
304 * Descriptions of the variants of the SiS900.
305 */
306 struct sip_variant {
307 int (*sipv_mii_readreg) __P((struct device *, int, int));
308 void (*sipv_mii_writereg) __P((struct device *, int, int, int));
309 void (*sipv_mii_statchg) __P((struct device *));
310 void (*sipv_set_filter) __P((struct sip_softc *));
311 void (*sipv_read_macaddr) __P((struct sip_softc *, u_int8_t *));
312 };
313
314 const struct sip_variant sip_variant_sis900 = {
315 sip_sis900_mii_readreg, sip_sis900_mii_writereg,
316 sip_sis900_mii_statchg, sip_sis900_set_filter,
317 sip_sis900_read_macaddr
318 };
319
320 const struct sip_variant sip_variant_dp83815 = {
321 sip_dp83815_mii_readreg, sip_dp83815_mii_writereg,
322 sip_dp83815_mii_statchg, sip_dp83815_set_filter,
323 sip_dp83815_read_macaddr
324 };
325
326 /*
327 * Devices supported by this driver.
328 */
329 const struct sip_product {
330 pci_vendor_id_t sip_vendor;
331 pci_product_id_t sip_product;
332 const char *sip_name;
333 const struct sip_variant *sip_variant;
334 } sip_products[] = {
335 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_900,
336 "SiS 900 10/100 Ethernet",
337 &sip_variant_sis900 },
338 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_7016,
339 "SiS 7016 10/100 Ethernet",
340 &sip_variant_sis900 },
341
342 { PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815,
343 "NatSemi DP83815 10/100 Ethernet",
344 &sip_variant_dp83815 },
345
346 { 0, 0,
347 NULL,
348 NULL },
349 };
350
351 const struct sip_product *sip_lookup __P((const struct pci_attach_args *));
352
353 const struct sip_product *
354 sip_lookup(pa)
355 const struct pci_attach_args *pa;
356 {
357 const struct sip_product *sip;
358
359 for (sip = sip_products; sip->sip_name != NULL; sip++) {
360 if (PCI_VENDOR(pa->pa_id) == sip->sip_vendor &&
361 PCI_PRODUCT(pa->pa_id) == sip->sip_product)
362 return (sip);
363 }
364 return (NULL);
365 }
366
367 int
368 sip_match(parent, cf, aux)
369 struct device *parent;
370 struct cfdata *cf;
371 void *aux;
372 {
373 struct pci_attach_args *pa = aux;
374
375 if (sip_lookup(pa) != NULL)
376 return (1);
377
378 return (0);
379 }
380
381 void
382 sip_attach(parent, self, aux)
383 struct device *parent, *self;
384 void *aux;
385 {
386 struct sip_softc *sc = (struct sip_softc *) self;
387 struct pci_attach_args *pa = aux;
388 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
389 pci_chipset_tag_t pc = pa->pa_pc;
390 pci_intr_handle_t ih;
391 const char *intrstr = NULL;
392 bus_space_tag_t iot, memt;
393 bus_space_handle_t ioh, memh;
394 bus_dma_segment_t seg;
395 int ioh_valid, memh_valid;
396 int i, rseg, error;
397 const struct sip_product *sip;
398 pcireg_t pmode;
399 u_int8_t enaddr[ETHER_ADDR_LEN];
400 int pmreg;
401
402 callout_init(&sc->sc_tick_ch);
403
404 sip = sip_lookup(pa);
405 if (sip == NULL) {
406 printf("\n");
407 panic("sip_attach: impossible");
408 }
409
410 printf(": %s\n", sip->sip_name);
411
412 sc->sc_model = sip;
413
414 /*
415 * Map the device.
416 */
417 ioh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGIOA,
418 PCI_MAPREG_TYPE_IO, 0,
419 &iot, &ioh, NULL, NULL) == 0);
420 memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
421 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
422 &memt, &memh, NULL, NULL) == 0);
423
424 if (memh_valid) {
425 sc->sc_st = memt;
426 sc->sc_sh = memh;
427 } else if (ioh_valid) {
428 sc->sc_st = iot;
429 sc->sc_sh = ioh;
430 } else {
431 printf("%s: unable to map device registers\n",
432 sc->sc_dev.dv_xname);
433 return;
434 }
435
436 sc->sc_dmat = pa->pa_dmat;
437
438 /* Enable bus mastering. */
439 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
440 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
441 PCI_COMMAND_MASTER_ENABLE);
442
443 /* Get it out of power save mode if needed. */
444 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
445 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
446 if (pmode == 3) {
447 /*
448 * The card has lost all configuration data in
449 * this state, so punt.
450 */
451 printf("%s: unable to wake up from power state D3\n",
452 sc->sc_dev.dv_xname);
453 return;
454 }
455 if (pmode != 0) {
456 printf("%s: waking up from power state D%d\n",
457 sc->sc_dev.dv_xname, pmode);
458 pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
459 }
460 }
461
462 /*
463 * Map and establish our interrupt.
464 */
465 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
466 pa->pa_intrline, &ih)) {
467 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
468 return;
469 }
470 intrstr = pci_intr_string(pc, ih);
471 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, sip_intr, sc);
472 if (sc->sc_ih == NULL) {
473 printf("%s: unable to establish interrupt",
474 sc->sc_dev.dv_xname);
475 if (intrstr != NULL)
476 printf(" at %s", intrstr);
477 printf("\n");
478 return;
479 }
480 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
481
482 SIMPLEQ_INIT(&sc->sc_txfreeq);
483 SIMPLEQ_INIT(&sc->sc_txdirtyq);
484
485 /*
486 * Allocate the control data structures, and create and load the
487 * DMA map for it.
488 */
489 if ((error = bus_dmamem_alloc(sc->sc_dmat,
490 sizeof(struct sip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
491 0)) != 0) {
492 printf("%s: unable to allocate control data, error = %d\n",
493 sc->sc_dev.dv_xname, error);
494 goto fail_0;
495 }
496
497 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
498 sizeof(struct sip_control_data), (caddr_t *)&sc->sc_control_data,
499 BUS_DMA_COHERENT)) != 0) {
500 printf("%s: unable to map control data, error = %d\n",
501 sc->sc_dev.dv_xname, error);
502 goto fail_1;
503 }
504
505 if ((error = bus_dmamap_create(sc->sc_dmat,
506 sizeof(struct sip_control_data), 1,
507 sizeof(struct sip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
508 printf("%s: unable to create control data DMA map, "
509 "error = %d\n", sc->sc_dev.dv_xname, error);
510 goto fail_2;
511 }
512
513 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
514 sc->sc_control_data, sizeof(struct sip_control_data), NULL,
515 0)) != 0) {
516 printf("%s: unable to load control data DMA map, error = %d\n",
517 sc->sc_dev.dv_xname, error);
518 goto fail_3;
519 }
520
521 /*
522 * Create the transmit buffer DMA maps.
523 */
524 for (i = 0; i < SIP_TXQUEUELEN; i++) {
525 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
526 SIP_NTXSEGS, MCLBYTES, 0, 0,
527 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
528 printf("%s: unable to create tx DMA map %d, "
529 "error = %d\n", sc->sc_dev.dv_xname, i, error);
530 goto fail_4;
531 }
532 }
533
534 /*
535 * Create the receive buffer DMA maps.
536 */
537 for (i = 0; i < SIP_NRXDESC; i++) {
538 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
539 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
540 printf("%s: unable to create rx DMA map %d, "
541 "error = %d\n", sc->sc_dev.dv_xname, i, error);
542 goto fail_5;
543 }
544 sc->sc_rxsoft[i].rxs_mbuf = NULL;
545 }
546
547 /*
548 * Reset the chip to a known state.
549 */
550 sip_reset(sc);
551
552 /*
553 * Read the Ethernet address from the EEPROM.
554 */
555 sip->sip_variant->sipv_read_macaddr(sc, enaddr);
556
557 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
558 ether_sprintf(enaddr));
559
560 /*
561 * Initialize our media structures and probe the MII.
562 */
563 sc->sc_mii.mii_ifp = ifp;
564 sc->sc_mii.mii_readreg = sip->sip_variant->sipv_mii_readreg;
565 sc->sc_mii.mii_writereg = sip->sip_variant->sipv_mii_writereg;
566 sc->sc_mii.mii_statchg = sip->sip_variant->sipv_mii_statchg;
567 ifmedia_init(&sc->sc_mii.mii_media, 0, sip_mediachange,
568 sip_mediastatus);
569 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
570 MII_OFFSET_ANY, 0);
571 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
572 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
573 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
574 } else
575 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
576
577 ifp = &sc->sc_ethercom.ec_if;
578 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
579 ifp->if_softc = sc;
580 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
581 ifp->if_ioctl = sip_ioctl;
582 ifp->if_start = sip_start;
583 ifp->if_watchdog = sip_watchdog;
584
585 /*
586 * Attach the interface.
587 */
588 if_attach(ifp);
589 ether_ifattach(ifp, enaddr);
590 #if NBPFILTER > 0
591 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
592 sizeof(struct ether_header));
593 #endif
594
595 /*
596 * Make sure the interface is shutdown during reboot.
597 */
598 sc->sc_sdhook = shutdownhook_establish(sip_shutdown, sc);
599 if (sc->sc_sdhook == NULL)
600 printf("%s: WARNING: unable to establish shutdown hook\n",
601 sc->sc_dev.dv_xname);
602 return;
603
604 /*
605 * Free any resources we've allocated during the failed attach
606 * attempt. Do this in reverse order and fall through.
607 */
608 fail_5:
609 for (i = 0; i < SIP_NRXDESC; i++) {
610 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
611 bus_dmamap_destroy(sc->sc_dmat,
612 sc->sc_rxsoft[i].rxs_dmamap);
613 }
614 fail_4:
615 for (i = 0; i < SIP_TXQUEUELEN; i++) {
616 if (sc->sc_txsoft[i].txs_dmamap != NULL)
617 bus_dmamap_destroy(sc->sc_dmat,
618 sc->sc_txsoft[i].txs_dmamap);
619 }
620 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
621 fail_3:
622 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
623 fail_2:
624 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
625 sizeof(struct sip_control_data));
626 fail_1:
627 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
628 fail_0:
629 return;
630 }
631
632 /*
633 * sip_shutdown:
634 *
635 * Make sure the interface is stopped at reboot time.
636 */
637 void
638 sip_shutdown(arg)
639 void *arg;
640 {
641 struct sip_softc *sc = arg;
642
643 sip_stop(sc, 1);
644 }
645
646 /*
647 * sip_start: [ifnet interface function]
648 *
649 * Start packet transmission on the interface.
650 */
651 void
652 sip_start(ifp)
653 struct ifnet *ifp;
654 {
655 struct sip_softc *sc = ifp->if_softc;
656 struct mbuf *m0, *m;
657 struct sip_txsoft *txs;
658 bus_dmamap_t dmamap;
659 int error, firsttx, nexttx, lasttx, ofree, seg;
660
661 /*
662 * If we've been told to pause, don't transmit any more packets.
663 */
664 if (sc->sc_flags & SIPF_PAUSED)
665 ifp->if_flags |= IFF_OACTIVE;
666
667 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
668 return;
669
670 /*
671 * Remember the previous number of free descriptors and
672 * the first descriptor we'll use.
673 */
674 ofree = sc->sc_txfree;
675 firsttx = sc->sc_txnext;
676
677 /*
678 * Loop through the send queue, setting up transmit descriptors
679 * until we drain the queue, or use up all available transmit
680 * descriptors.
681 */
682 for (;;) {
683 if ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) == NULL) {
684 break;
685 }
686
687 /*
688 * Grab a packet off the queue.
689 */
690 IF_POLL(&ifp->if_snd, m0);
691 if (m0 == NULL)
692 break;
693
694 m = NULL;
695 dmamap = txs->txs_dmamap;
696
697 /*
698 * Load the DMA map. If this fails, the packet either
699 * didn't fit in the alloted number of segments, or we
700 * were short on resources. In this case, we'll copy
701 * and try again.
702 */
703 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
704 BUS_DMA_NOWAIT) != 0) {
705 MGETHDR(m, M_DONTWAIT, MT_DATA);
706 if (m == NULL) {
707 printf("%s: unable to allocate Tx mbuf\n",
708 sc->sc_dev.dv_xname);
709 break;
710 }
711 if (m0->m_pkthdr.len > MHLEN) {
712 MCLGET(m, M_DONTWAIT);
713 if ((m->m_flags & M_EXT) == 0) {
714 printf("%s: unable to allocate Tx "
715 "cluster\n", sc->sc_dev.dv_xname);
716 m_freem(m);
717 break;
718 }
719 }
720 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
721 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
722 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
723 m, BUS_DMA_NOWAIT);
724 if (error) {
725 printf("%s: unable to load Tx buffer, "
726 "error = %d\n", sc->sc_dev.dv_xname, error);
727 break;
728 }
729 }
730
731 /*
732 * Ensure we have enough descriptors free to describe
733 * the packet. Note, we always reserve one descriptor
734 * at the end of the ring as a termination point, to
735 * prevent wrap-around.
736 */
737 if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
738 /*
739 * Not enough free descriptors to transmit this
740 * packet. We haven't committed anything yet,
741 * so just unload the DMA map, put the packet
742 * back on the queue, and punt. Notify the upper
743 * layer that there are not more slots left.
744 *
745 * XXX We could allocate an mbuf and copy, but
746 * XXX is it worth it?
747 */
748 ifp->if_flags |= IFF_OACTIVE;
749 bus_dmamap_unload(sc->sc_dmat, dmamap);
750 if (m != NULL)
751 m_freem(m);
752 break;
753 }
754
755 IF_DEQUEUE(&ifp->if_snd, m0);
756 if (m != NULL) {
757 m_freem(m0);
758 m0 = m;
759 }
760 /*
761 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
762 */
763
764 /* Sync the DMA map. */
765 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
766 BUS_DMASYNC_PREWRITE);
767
768 /*
769 * Initialize the transmit descriptors.
770 */
771 for (nexttx = sc->sc_txnext, seg = 0;
772 seg < dmamap->dm_nsegs;
773 seg++, nexttx = SIP_NEXTTX(nexttx)) {
774 /*
775 * If this is the first descriptor we're
776 * enqueueing, don't set the OWN bit just
777 * yet. That could cause a race condition.
778 * We'll do it below.
779 */
780 sc->sc_txdescs[nexttx].sipd_bufptr =
781 htole32(dmamap->dm_segs[seg].ds_addr);
782 sc->sc_txdescs[nexttx].sipd_cmdsts =
783 htole32((nexttx == firsttx ? 0 : CMDSTS_OWN) |
784 CMDSTS_MORE | dmamap->dm_segs[seg].ds_len);
785 lasttx = nexttx;
786 }
787
788 /* Clear the MORE bit on the last segment. */
789 sc->sc_txdescs[lasttx].sipd_cmdsts &= htole32(~CMDSTS_MORE);
790
791 /* Sync the descriptors we're using. */
792 SIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
793 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
794
795 /*
796 * Store a pointer to the packet so we can free it later,
797 * and remember what txdirty will be once the packet is
798 * done.
799 */
800 txs->txs_mbuf = m0;
801 txs->txs_firstdesc = sc->sc_txnext;
802 txs->txs_lastdesc = lasttx;
803
804 /* Advance the tx pointer. */
805 sc->sc_txfree -= dmamap->dm_nsegs;
806 sc->sc_txnext = nexttx;
807
808 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
809 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
810
811 #if NBPFILTER > 0
812 /*
813 * Pass the packet to any BPF listeners.
814 */
815 if (ifp->if_bpf)
816 bpf_mtap(ifp->if_bpf, m0);
817 #endif /* NBPFILTER > 0 */
818 }
819
820 if (txs == NULL || sc->sc_txfree == 0) {
821 /* No more slots left; notify upper layer. */
822 ifp->if_flags |= IFF_OACTIVE;
823 }
824
825 if (sc->sc_txfree != ofree) {
826 /*
827 * Cause a descriptor interrupt to happen on the
828 * last packet we enqueued.
829 */
830 sc->sc_txdescs[lasttx].sipd_cmdsts |= htole32(CMDSTS_INTR);
831 SIP_CDTXSYNC(sc, lasttx, 1,
832 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
833
834 /*
835 * The entire packet chain is set up. Give the
836 * first descrptor to the chip now.
837 */
838 sc->sc_txdescs[firsttx].sipd_cmdsts |= htole32(CMDSTS_OWN);
839 SIP_CDTXSYNC(sc, firsttx, 1,
840 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
841
842 /*
843 * Start the transmit process. Note, the manual says
844 * that if there are no pending transmissions in the
845 * chip's internal queue (indicated by TXE being clear),
846 * then the driver software must set the TXDP to the
847 * first descriptor to be transmitted. However, if we
848 * do this, it causes serious performance degredation on
849 * the DP83820 under load, not setting TXDP doesn't seem
850 * to adversely affect the SiS 900 or DP83815.
851 *
852 * Well, I guess it wouldn't be the first time a manual
853 * has lied -- and they could be speaking of the NULL-
854 * terminated descriptor list case, rather than OWN-
855 * terminated rings.
856 */
857 #if 0
858 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR) &
859 CR_TXE) == 0) {
860 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP,
861 SIP_CDTXADDR(sc, firsttx));
862 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
863 }
864 #else
865 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
866 #endif
867
868 /* Set a watchdog timer in case the chip flakes out. */
869 ifp->if_timer = 5;
870 }
871 }
872
873 /*
874 * sip_watchdog: [ifnet interface function]
875 *
876 * Watchdog timer handler.
877 */
878 void
879 sip_watchdog(ifp)
880 struct ifnet *ifp;
881 {
882 struct sip_softc *sc = ifp->if_softc;
883
884 /*
885 * The chip seems to ignore the CMDSTS_INTR bit sometimes!
886 * If we get a timeout, try and sweep up transmit descriptors.
887 * If we manage to sweep them all up, ignore the lack of
888 * interrupt.
889 */
890 sip_txintr(sc);
891
892 if (sc->sc_txfree != SIP_NTXDESC) {
893 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
894 ifp->if_oerrors++;
895
896 /* Reset the interface. */
897 (void) sip_init(sc);
898 } else if (ifp->if_flags & IFF_DEBUG)
899 printf("%s: recovered from device timeout\n",
900 sc->sc_dev.dv_xname);
901
902 /* Try to get more packets going. */
903 sip_start(ifp);
904 }
905
906 /*
907 * sip_ioctl: [ifnet interface function]
908 *
909 * Handle control requests from the operator.
910 */
911 int
912 sip_ioctl(ifp, cmd, data)
913 struct ifnet *ifp;
914 u_long cmd;
915 caddr_t data;
916 {
917 struct sip_softc *sc = ifp->if_softc;
918 struct ifreq *ifr = (struct ifreq *)data;
919 struct ifaddr *ifa = (struct ifaddr *)data;
920 int s, error = 0;
921
922 s = splnet();
923
924 switch (cmd) {
925 case SIOCSIFADDR:
926 ifp->if_flags |= IFF_UP;
927
928 switch (ifa->ifa_addr->sa_family) {
929 #ifdef INET
930 case AF_INET:
931 if ((error = sip_init(sc)) != 0)
932 break;
933 arp_ifinit(ifp, ifa);
934 break;
935 #endif /* INET */
936 #ifdef NS
937 case AF_NS:
938 {
939 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
940
941 if (ns_nullhost(*ina))
942 ina->x_host = *(union ns_host *)
943 LLADDR(ifp->if_sadl);
944 else
945 memcpy(LLADDR(ifp->if_sadl),
946 ina->x_host.c_host, ifp->if_addrlen);
947 error = sip_init(sc);
948 break;
949 }
950 #endif /* NS */
951 default:
952 error = sip_init(sc);
953 break;
954 }
955 break;
956
957 case SIOCSIFMTU:
958 if (ifr->ifr_mtu > ETHERMTU)
959 error = EINVAL;
960 else
961 ifp->if_mtu = ifr->ifr_mtu;
962 break;
963
964 case SIOCSIFFLAGS:
965 if ((ifp->if_flags & IFF_UP) == 0 &&
966 (ifp->if_flags & IFF_RUNNING) != 0) {
967 /*
968 * If interface is marked down and it is running, then
969 * stop it.
970 */
971 sip_stop(sc, 1);
972 } else if ((ifp->if_flags & IFF_UP) != 0 &&
973 (ifp->if_flags & IFF_RUNNING) == 0) {
974 /*
975 * If interfase it marked up and it is stopped, then
976 * start it.
977 */
978 error = sip_init(sc);
979 } else if ((ifp->if_flags & IFF_UP) != 0) {
980 /*
981 * Reset the interface to pick up changes in any other
982 * flags that affect the hardware state.
983 */
984 error = sip_init(sc);
985 }
986 break;
987
988 case SIOCADDMULTI:
989 case SIOCDELMULTI:
990 error = (cmd == SIOCADDMULTI) ?
991 ether_addmulti(ifr, &sc->sc_ethercom) :
992 ether_delmulti(ifr, &sc->sc_ethercom);
993
994 if (error == ENETRESET) {
995 /*
996 * Multicast list has changed; set the hardware filter
997 * accordingly.
998 */
999 (*sc->sc_model->sip_variant->sipv_set_filter)(sc);
1000 error = 0;
1001 }
1002 break;
1003
1004 case SIOCSIFMEDIA:
1005 case SIOCGIFMEDIA:
1006 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1007 break;
1008
1009 default:
1010 error = EINVAL;
1011 break;
1012 }
1013
1014 /* Try to get more packets going. */
1015 sip_start(ifp);
1016
1017 splx(s);
1018 return (error);
1019 }
1020
1021 /*
1022 * sip_intr:
1023 *
1024 * Interrupt service routine.
1025 */
1026 int
1027 sip_intr(arg)
1028 void *arg;
1029 {
1030 struct sip_softc *sc = arg;
1031 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1032 u_int32_t isr;
1033 int handled = 0;
1034
1035 for (;;) {
1036 /* Reading clears interrupt. */
1037 isr = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ISR);
1038 if ((isr & sc->sc_imr) == 0)
1039 break;
1040
1041 handled = 1;
1042
1043 if (isr & (ISR_RXORN|ISR_RXIDLE|ISR_RXDESC)) {
1044 /* Grab any new packets. */
1045 sip_rxintr(sc);
1046
1047 if (isr & ISR_RXORN) {
1048 printf("%s: receive FIFO overrun\n",
1049 sc->sc_dev.dv_xname);
1050
1051 /* XXX adjust rx_drain_thresh? */
1052 }
1053
1054 if (isr & ISR_RXIDLE) {
1055 printf("%s: receive ring overrun\n",
1056 sc->sc_dev.dv_xname);
1057
1058 /* Get the receive process going again. */
1059 bus_space_write_4(sc->sc_st, sc->sc_sh,
1060 SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
1061 bus_space_write_4(sc->sc_st, sc->sc_sh,
1062 SIP_CR, CR_RXE);
1063 }
1064 }
1065
1066 if (isr & (ISR_TXURN|ISR_TXDESC)) {
1067 /* Sweep up transmit descriptors. */
1068 sip_txintr(sc);
1069
1070 if (isr & ISR_TXURN) {
1071 u_int32_t thresh;
1072
1073 printf("%s: transmit FIFO underrun",
1074 sc->sc_dev.dv_xname);
1075
1076 thresh = sc->sc_tx_drain_thresh + 1;
1077 if (thresh <= TXCFG_DRTH &&
1078 (thresh * 32) <= (SIP_TXFIFO_SIZE -
1079 (sc->sc_tx_fill_thresh * 32))) {
1080 printf("; increasing Tx drain "
1081 "threshold to %u bytes\n",
1082 thresh * 32);
1083 sc->sc_tx_drain_thresh = thresh;
1084 (void) sip_init(sc);
1085 } else {
1086 (void) sip_init(sc);
1087 printf("\n");
1088 }
1089 }
1090 }
1091
1092 if (sc->sc_imr & (ISR_PAUSE_END|ISR_PAUSE_ST)) {
1093 if (isr & ISR_PAUSE_ST) {
1094 sc->sc_flags |= SIPF_PAUSED;
1095 ifp->if_flags |= IFF_OACTIVE;
1096 }
1097 if (isr & ISR_PAUSE_END) {
1098 sc->sc_flags &= ~SIPF_PAUSED;
1099 ifp->if_flags &= ~IFF_OACTIVE;
1100 }
1101 }
1102
1103 if (isr & ISR_HIBERR) {
1104 #define PRINTERR(bit, str) \
1105 if (isr & (bit)) \
1106 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1107 PRINTERR(ISR_DPERR, "parity error");
1108 PRINTERR(ISR_SSERR, "system error");
1109 PRINTERR(ISR_RMABT, "master abort");
1110 PRINTERR(ISR_RTABT, "target abort");
1111 PRINTERR(ISR_RXSOVR, "receive status FIFO overrun");
1112 (void) sip_init(sc);
1113 #undef PRINTERR
1114 }
1115 }
1116
1117 /* Try to get more packets going. */
1118 sip_start(ifp);
1119
1120 return (handled);
1121 }
1122
1123 /*
1124 * sip_txintr:
1125 *
1126 * Helper; handle transmit interrupts.
1127 */
1128 void
1129 sip_txintr(sc)
1130 struct sip_softc *sc;
1131 {
1132 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1133 struct sip_txsoft *txs;
1134 u_int32_t cmdsts;
1135
1136 if ((sc->sc_flags & SIPF_PAUSED) == 0)
1137 ifp->if_flags &= ~IFF_OACTIVE;
1138
1139 /*
1140 * Go through our Tx list and free mbufs for those
1141 * frames which have been transmitted.
1142 */
1143 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1144 SIP_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1145 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1146
1147 cmdsts = le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1148 if (cmdsts & CMDSTS_OWN)
1149 break;
1150
1151 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1152
1153 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1154
1155 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1156 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1157 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1158 m_freem(txs->txs_mbuf);
1159 txs->txs_mbuf = NULL;
1160
1161 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1162
1163 /*
1164 * Check for errors and collisions.
1165 */
1166 if (cmdsts &
1167 (CMDSTS_Tx_TXA|CMDSTS_Tx_TFU|CMDSTS_Tx_ED|CMDSTS_Tx_EC)) {
1168 ifp->if_opackets++;
1169 if (cmdsts & CMDSTS_Tx_EC)
1170 ifp->if_collisions += 16;
1171 if (ifp->if_flags & IFF_DEBUG) {
1172 if (cmdsts & CMDSTS_Tx_ED)
1173 printf("%s: excessive deferral\n",
1174 sc->sc_dev.dv_xname);
1175 if (cmdsts & CMDSTS_Tx_EC)
1176 printf("%s: excessive collisions\n",
1177 sc->sc_dev.dv_xname);
1178 }
1179 } else {
1180 /* Packet was transmitted successfully. */
1181 ifp->if_opackets++;
1182 ifp->if_collisions += CMDSTS_COLLISIONS(cmdsts);
1183 }
1184 }
1185
1186 /*
1187 * If there are no more pending transmissions, cancel the watchdog
1188 * timer.
1189 */
1190 if (txs == NULL)
1191 ifp->if_timer = 0;
1192 }
1193
1194 /*
1195 * sip_rxintr:
1196 *
1197 * Helper; handle receive interrupts.
1198 */
1199 void
1200 sip_rxintr(sc)
1201 struct sip_softc *sc;
1202 {
1203 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1204 struct ether_header *eh;
1205 struct sip_rxsoft *rxs;
1206 struct mbuf *m;
1207 u_int32_t cmdsts;
1208 int i, len;
1209
1210 for (i = sc->sc_rxptr;; i = SIP_NEXTRX(i)) {
1211 rxs = &sc->sc_rxsoft[i];
1212
1213 SIP_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1214
1215 cmdsts = le32toh(sc->sc_rxdescs[i].sipd_cmdsts);
1216
1217 /*
1218 * NOTE: OWN is set if owned by _consumer_. We're the
1219 * consumer of the receive ring, so if the bit is clear,
1220 * we have processed all of the packets.
1221 */
1222 if ((cmdsts & CMDSTS_OWN) == 0) {
1223 /*
1224 * We have processed all of the receive buffers.
1225 */
1226 break;
1227 }
1228
1229 /*
1230 * If any collisions were seen on the wire, count one.
1231 */
1232 if (cmdsts & CMDSTS_Rx_COL)
1233 ifp->if_collisions++;
1234
1235 /*
1236 * If an error occurred, update stats, clear the status
1237 * word, and leave the packet buffer in place. It will
1238 * simply be reused the next time the ring comes around.
1239 */
1240 if (cmdsts & (CMDSTS_Rx_RXA|CMDSTS_Rx_LONG|CMDSTS_Rx_RUNT|
1241 CMDSTS_Rx_ISE|CMDSTS_Rx_CRCE|CMDSTS_Rx_FAE)) {
1242 ifp->if_ierrors++;
1243 if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
1244 (cmdsts & CMDSTS_Rx_RXO) == 0) {
1245 /* Receive overrun handled elsewhere. */
1246 printf("%s: receive descriptor error\n",
1247 sc->sc_dev.dv_xname);
1248 }
1249 #define PRINTERR(bit, str) \
1250 if (cmdsts & (bit)) \
1251 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1252 PRINTERR(CMDSTS_Rx_LONG, "packet too long");
1253 PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
1254 PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
1255 PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
1256 PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
1257 #undef PRINTERR
1258 SIP_INIT_RXDESC(sc, i);
1259 continue;
1260 }
1261
1262 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1263 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1264
1265 /*
1266 * No errors; receive the packet. Note, the SiS 900
1267 * includes the CRC with every packet; trim it.
1268 */
1269 len = CMDSTS_SIZE(cmdsts) - ETHER_CRC_LEN;
1270
1271 #ifdef __NO_STRICT_ALIGNMENT
1272 /*
1273 * If the packet is small enough to fit in a
1274 * single header mbuf, allocate one and copy
1275 * the data into it. This greatly reduces
1276 * memory consumption when we receive lots
1277 * of small packets.
1278 *
1279 * Otherwise, we add a new buffer to the receive
1280 * chain. If this fails, we drop the packet and
1281 * recycle the old buffer.
1282 */
1283 if (sip_copy_small != 0 && len <= MHLEN) {
1284 MGETHDR(m, M_DONTWAIT, MT_DATA);
1285 if (m == NULL)
1286 goto dropit;
1287 memcpy(mtod(m, caddr_t),
1288 mtod(rxs->rxs_mbuf, caddr_t), len);
1289 SIP_INIT_RXDESC(sc, i);
1290 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1291 rxs->rxs_dmamap->dm_mapsize,
1292 BUS_DMASYNC_PREREAD);
1293 } else {
1294 m = rxs->rxs_mbuf;
1295 if (sip_add_rxbuf(sc, i) != 0) {
1296 dropit:
1297 ifp->if_ierrors++;
1298 SIP_INIT_RXDESC(sc, i);
1299 bus_dmamap_sync(sc->sc_dmat,
1300 rxs->rxs_dmamap, 0,
1301 rxs->rxs_dmamap->dm_mapsize,
1302 BUS_DMASYNC_PREREAD);
1303 continue;
1304 }
1305 }
1306 #else
1307 /*
1308 * The SiS 900's receive buffers must be 4-byte aligned.
1309 * But this means that the data after the Ethernet header
1310 * is misaligned. We must allocate a new buffer and
1311 * copy the data, shifted forward 2 bytes.
1312 */
1313 MGETHDR(m, M_DONTWAIT, MT_DATA);
1314 if (m == NULL) {
1315 dropit:
1316 ifp->if_ierrors++;
1317 SIP_INIT_RXDESC(sc, i);
1318 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1319 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1320 continue;
1321 }
1322 if (len > (MHLEN - 2)) {
1323 MCLGET(m, M_DONTWAIT);
1324 if ((m->m_flags & M_EXT) == 0) {
1325 m_freem(m);
1326 goto dropit;
1327 }
1328 }
1329 m->m_data += 2;
1330
1331 /*
1332 * Note that we use clusters for incoming frames, so the
1333 * buffer is virtually contiguous.
1334 */
1335 memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1336
1337 /* Allow the receive descriptor to continue using its mbuf. */
1338 SIP_INIT_RXDESC(sc, i);
1339 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1340 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1341 #endif /* __NO_STRICT_ALIGNMENT */
1342
1343 ifp->if_ipackets++;
1344 eh = mtod(m, struct ether_header *);
1345 m->m_pkthdr.rcvif = ifp;
1346 m->m_pkthdr.len = m->m_len = len;
1347
1348 #if NBPFILTER > 0
1349 /*
1350 * Pass this up to any BPF listeners, but only
1351 * pass if up the stack if it's for us.
1352 */
1353 if (ifp->if_bpf) {
1354 bpf_mtap(ifp->if_bpf, m);
1355 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
1356 (cmdsts & CMDSTS_Rx_DEST) == CMDSTS_Rx_DEST_REJ) {
1357 m_freem(m);
1358 continue;
1359 }
1360 }
1361 #endif /* NBPFILTER > 0 */
1362
1363 /* Pass it on. */
1364 (*ifp->if_input)(ifp, m);
1365 }
1366
1367 /* Update the receive pointer. */
1368 sc->sc_rxptr = i;
1369 }
1370
1371 /*
1372 * sip_tick:
1373 *
1374 * One second timer, used to tick the MII.
1375 */
1376 void
1377 sip_tick(arg)
1378 void *arg;
1379 {
1380 struct sip_softc *sc = arg;
1381 int s;
1382
1383 s = splnet();
1384 mii_tick(&sc->sc_mii);
1385 splx(s);
1386
1387 callout_reset(&sc->sc_tick_ch, hz, sip_tick, sc);
1388 }
1389
1390 /*
1391 * sip_reset:
1392 *
1393 * Perform a soft reset on the SiS 900.
1394 */
1395 void
1396 sip_reset(sc)
1397 struct sip_softc *sc;
1398 {
1399 bus_space_tag_t st = sc->sc_st;
1400 bus_space_handle_t sh = sc->sc_sh;
1401 int i;
1402
1403 bus_space_write_4(st, sh, SIP_CR, CR_RST);
1404
1405 for (i = 0; i < SIP_TIMEOUT; i++) {
1406 if ((bus_space_read_4(st, sh, SIP_CR) & CR_RST) == 0)
1407 break;
1408 delay(2);
1409 }
1410
1411 if (i == SIP_TIMEOUT)
1412 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1413
1414 delay(1000);
1415 }
1416
1417 /*
1418 * sip_init:
1419 *
1420 * Initialize the interface. Must be called at splnet().
1421 */
1422 int
1423 sip_init(sc)
1424 struct sip_softc *sc;
1425 {
1426 bus_space_tag_t st = sc->sc_st;
1427 bus_space_handle_t sh = sc->sc_sh;
1428 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1429 struct sip_txsoft *txs;
1430 struct sip_rxsoft *rxs;
1431 struct sip_desc *sipd;
1432 u_int32_t cfg;
1433 int i, error = 0;
1434
1435 /*
1436 * Cancel any pending I/O.
1437 */
1438 sip_stop(sc, 0);
1439
1440 /*
1441 * Reset the chip to a known state.
1442 */
1443 sip_reset(sc);
1444
1445 if ( sc->sc_model->sip_vendor == PCI_VENDOR_NS
1446 && sc->sc_model->sip_product == PCI_PRODUCT_NS_DP83815) {
1447 /*
1448 * DP83815 manual, page 78:
1449 * 4.4 Recommended Registers Configuration
1450 * For optimum performance of the DP83815, version noted
1451 * as DP83815CVNG (SRR = 203h), the listed register
1452 * modifications must be followed in sequence...
1453 *
1454 * It's not clear if this should be 302h or 203h because that
1455 * chip name is listed as SRR 302h in the description of the
1456 * SRR register. However, my revision 302h DP83815 on the
1457 * Netgear FA311 purchased in 02/2001 needs these settings
1458 * to avoid tons of errors in AcceptPerfectMatch (non-
1459 * IFF_PROMISC) mode. I do not know if other revisions need
1460 * this set or not. [briggs -- 09 March 2001]
1461 *
1462 * Note that only the low-order 12 bits of 0xe4 are documented
1463 * and that this sets reserved bits in that register.
1464 */
1465 cfg = bus_space_read_4(st, sh, SIP_NS_SRR);
1466 if (cfg == 0x302) {
1467 bus_space_write_4(st, sh, 0x00cc, 0x0001);
1468 bus_space_write_4(st, sh, 0x00e4, 0x189C);
1469 bus_space_write_4(st, sh, 0x00fc, 0x0000);
1470 bus_space_write_4(st, sh, 0x00f4, 0x5040);
1471 bus_space_write_4(st, sh, 0x00f8, 0x008c);
1472 }
1473 }
1474
1475 /*
1476 * Initialize the transmit descriptor ring.
1477 */
1478 for (i = 0; i < SIP_NTXDESC; i++) {
1479 sipd = &sc->sc_txdescs[i];
1480 memset(sipd, 0, sizeof(struct sip_desc));
1481 sipd->sipd_link = htole32(SIP_CDTXADDR(sc, SIP_NEXTTX(i)));
1482 }
1483 SIP_CDTXSYNC(sc, 0, SIP_NTXDESC,
1484 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1485 sc->sc_txfree = SIP_NTXDESC;
1486 sc->sc_txnext = 0;
1487
1488 /*
1489 * Initialize the transmit job descriptors.
1490 */
1491 SIMPLEQ_INIT(&sc->sc_txfreeq);
1492 SIMPLEQ_INIT(&sc->sc_txdirtyq);
1493 for (i = 0; i < SIP_TXQUEUELEN; i++) {
1494 txs = &sc->sc_txsoft[i];
1495 txs->txs_mbuf = NULL;
1496 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1497 }
1498
1499 /*
1500 * Initialize the receive descriptor and receive job
1501 * descriptor rings.
1502 */
1503 for (i = 0; i < SIP_NRXDESC; i++) {
1504 rxs = &sc->sc_rxsoft[i];
1505 if (rxs->rxs_mbuf == NULL) {
1506 if ((error = sip_add_rxbuf(sc, i)) != 0) {
1507 printf("%s: unable to allocate or map rx "
1508 "buffer %d, error = %d\n",
1509 sc->sc_dev.dv_xname, i, error);
1510 /*
1511 * XXX Should attempt to run with fewer receive
1512 * XXX buffers instead of just failing.
1513 */
1514 sip_rxdrain(sc);
1515 goto out;
1516 }
1517 }
1518 }
1519 sc->sc_rxptr = 0;
1520
1521 /*
1522 * Initialize the configuration register: aggressive PCI
1523 * bus request algorithm, default backoff, default OW timer,
1524 * default parity error detection.
1525 */
1526 cfg = 0;
1527 #if BYTE_ORDER == BIG_ENDIAN
1528 /*
1529 * ...descriptors in big-endian mode.
1530 */
1531 #if 0
1532 /* "Big endian mode" does not work properly. */
1533 cfg |= CFG_BEM;
1534 #endif
1535 #endif
1536 bus_space_write_4(st, sh, SIP_CFG, cfg);
1537
1538 /*
1539 * Initialize the transmit fill and drain thresholds if
1540 * we have never done so.
1541 */
1542 if (sc->sc_tx_fill_thresh == 0) {
1543 /*
1544 * XXX This value should be tuned. This is the
1545 * minimum (32 bytes), and we may be able to
1546 * improve performance by increasing it.
1547 */
1548 sc->sc_tx_fill_thresh = 1;
1549 }
1550 if (sc->sc_tx_drain_thresh == 0) {
1551 /*
1552 * Start at a drain threshold of 512 bytes. We will
1553 * increase it if a DMA underrun occurs.
1554 *
1555 * XXX The minimum value of this variable should be
1556 * tuned. We may be able to improve performance
1557 * by starting with a lower value. That, however,
1558 * may trash the first few outgoing packets if the
1559 * PCI bus is saturated.
1560 */
1561 sc->sc_tx_drain_thresh = 512 / 32;
1562 }
1563
1564 /*
1565 * Initialize the prototype TXCFG register.
1566 */
1567 sc->sc_txcfg = TXCFG_ATP | TXCFG_MXDMA_512 |
1568 (sc->sc_tx_fill_thresh << TXCFG_FLTH_SHIFT) |
1569 sc->sc_tx_drain_thresh;
1570 bus_space_write_4(st, sh, SIP_TXCFG, sc->sc_txcfg);
1571
1572 /*
1573 * Initialize the receive drain threshold if we have never
1574 * done so.
1575 */
1576 if (sc->sc_rx_drain_thresh == 0) {
1577 /*
1578 * XXX This value should be tuned. This is set to the
1579 * maximum of 248 bytes, and we may be able to improve
1580 * performance by decreasing it (although we should never
1581 * set this value lower than 2; 14 bytes are required to
1582 * filter the packet).
1583 */
1584 sc->sc_rx_drain_thresh = RXCFG_DRTH >> RXCFG_DRTH_SHIFT;
1585 }
1586
1587 /*
1588 * Initialize the prototype RXCFG register.
1589 */
1590 sc->sc_rxcfg = RXCFG_MXDMA_512 |
1591 (sc->sc_rx_drain_thresh << RXCFG_DRTH_SHIFT);
1592 bus_space_write_4(st, sh, SIP_RXCFG, sc->sc_rxcfg);
1593
1594 /* Set up the receive filter. */
1595 (*sc->sc_model->sip_variant->sipv_set_filter)(sc);
1596
1597 /*
1598 * Give the transmit and receive rings to the chip.
1599 */
1600 bus_space_write_4(st, sh, SIP_TXDP, SIP_CDTXADDR(sc, sc->sc_txnext));
1601 bus_space_write_4(st, sh, SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
1602
1603 /*
1604 * Initialize the interrupt mask.
1605 */
1606 sc->sc_imr = ISR_DPERR|ISR_SSERR|ISR_RMABT|ISR_RTABT|ISR_RXSOVR|
1607 ISR_TXURN|ISR_TXDESC|ISR_RXORN|ISR_RXIDLE|ISR_RXDESC;
1608 bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
1609
1610 /*
1611 * Set the current media. Do this after initializing the prototype
1612 * IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
1613 * control.
1614 */
1615 mii_mediachg(&sc->sc_mii);
1616
1617 /*
1618 * Enable interrupts.
1619 */
1620 bus_space_write_4(st, sh, SIP_IER, IER_IE);
1621
1622 /*
1623 * Start the transmit and receive processes.
1624 */
1625 bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
1626
1627 /*
1628 * Start the one second MII clock.
1629 */
1630 callout_reset(&sc->sc_tick_ch, hz, sip_tick, sc);
1631
1632 /*
1633 * ...all done!
1634 */
1635 ifp->if_flags |= IFF_RUNNING;
1636 ifp->if_flags &= ~IFF_OACTIVE;
1637
1638 out:
1639 if (error)
1640 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1641 return (error);
1642 }
1643
1644 /*
1645 * sip_drain:
1646 *
1647 * Drain the receive queue.
1648 */
1649 void
1650 sip_rxdrain(sc)
1651 struct sip_softc *sc;
1652 {
1653 struct sip_rxsoft *rxs;
1654 int i;
1655
1656 for (i = 0; i < SIP_NRXDESC; i++) {
1657 rxs = &sc->sc_rxsoft[i];
1658 if (rxs->rxs_mbuf != NULL) {
1659 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1660 m_freem(rxs->rxs_mbuf);
1661 rxs->rxs_mbuf = NULL;
1662 }
1663 }
1664 }
1665
1666 /*
1667 * sip_stop:
1668 *
1669 * Stop transmission on the interface.
1670 */
1671 void
1672 sip_stop(sc, drain)
1673 struct sip_softc *sc;
1674 {
1675 bus_space_tag_t st = sc->sc_st;
1676 bus_space_handle_t sh = sc->sc_sh;
1677 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1678 struct sip_txsoft *txs;
1679 u_int32_t cmdsts = 0; /* DEBUG */
1680
1681 /*
1682 * Stop the one second clock.
1683 */
1684 callout_stop(&sc->sc_tick_ch);
1685
1686 /* Down the MII. */
1687 mii_down(&sc->sc_mii);
1688
1689 /*
1690 * Disable interrupts.
1691 */
1692 bus_space_write_4(st, sh, SIP_IER, 0);
1693
1694 /*
1695 * Stop receiver and transmitter.
1696 */
1697 bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
1698
1699 /*
1700 * Release any queued transmit buffers.
1701 */
1702 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1703 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1704 SIMPLEQ_NEXT(txs, txs_q) == NULL &&
1705 (le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts) &
1706 CMDSTS_INTR) == 0)
1707 printf("%s: sip_stop: last descriptor does not "
1708 "have INTR bit set\n", sc->sc_dev.dv_xname);
1709 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1710 #ifdef DIAGNOSTIC
1711 if (txs->txs_mbuf == NULL) {
1712 printf("%s: dirty txsoft with no mbuf chain\n",
1713 sc->sc_dev.dv_xname);
1714 panic("sip_stop");
1715 }
1716 #endif
1717 cmdsts |= /* DEBUG */
1718 le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1719 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1720 m_freem(txs->txs_mbuf);
1721 txs->txs_mbuf = NULL;
1722 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1723 }
1724
1725 if (drain) {
1726 /*
1727 * Release the receive buffers.
1728 */
1729 sip_rxdrain(sc);
1730 }
1731
1732 /*
1733 * Mark the interface down and cancel the watchdog timer.
1734 */
1735 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1736 ifp->if_timer = 0;
1737
1738 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1739 (cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != SIP_NTXDESC)
1740 printf("%s: sip_stop: no INTR bits set in dirty tx "
1741 "descriptors\n", sc->sc_dev.dv_xname);
1742 }
1743
1744 /*
1745 * sip_read_eeprom:
1746 *
1747 * Read data from the serial EEPROM.
1748 */
1749 void
1750 sip_read_eeprom(sc, word, wordcnt, data)
1751 struct sip_softc *sc;
1752 int word, wordcnt;
1753 u_int16_t *data;
1754 {
1755 bus_space_tag_t st = sc->sc_st;
1756 bus_space_handle_t sh = sc->sc_sh;
1757 u_int16_t reg;
1758 int i, x;
1759
1760 for (i = 0; i < wordcnt; i++) {
1761 /* Send CHIP SELECT. */
1762 reg = EROMAR_EECS;
1763 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1764
1765 /* Shift in the READ opcode. */
1766 for (x = 3; x > 0; x--) {
1767 if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
1768 reg |= EROMAR_EEDI;
1769 else
1770 reg &= ~EROMAR_EEDI;
1771 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1772 bus_space_write_4(st, sh, SIP_EROMAR,
1773 reg | EROMAR_EESK);
1774 delay(4);
1775 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1776 delay(4);
1777 }
1778
1779 /* Shift in address. */
1780 for (x = 6; x > 0; x--) {
1781 if ((word + i) & (1 << (x - 1)))
1782 reg |= EROMAR_EEDI;
1783 else
1784 reg &= ~EROMAR_EEDI;
1785 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1786 bus_space_write_4(st, sh, SIP_EROMAR,
1787 reg | EROMAR_EESK);
1788 delay(4);
1789 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1790 delay(4);
1791 }
1792
1793 /* Shift out data. */
1794 reg = EROMAR_EECS;
1795 data[i] = 0;
1796 for (x = 16; x > 0; x--) {
1797 bus_space_write_4(st, sh, SIP_EROMAR,
1798 reg | EROMAR_EESK);
1799 delay(4);
1800 if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
1801 data[i] |= (1 << (x - 1));
1802 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1803 delay(4);
1804 }
1805
1806 /* Clear CHIP SELECT. */
1807 bus_space_write_4(st, sh, SIP_EROMAR, 0);
1808 delay(4);
1809 }
1810 }
1811
1812 /*
1813 * sip_add_rxbuf:
1814 *
1815 * Add a receive buffer to the indicated descriptor.
1816 */
1817 int
1818 sip_add_rxbuf(sc, idx)
1819 struct sip_softc *sc;
1820 int idx;
1821 {
1822 struct sip_rxsoft *rxs = &sc->sc_rxsoft[idx];
1823 struct mbuf *m;
1824 int error;
1825
1826 MGETHDR(m, M_DONTWAIT, MT_DATA);
1827 if (m == NULL)
1828 return (ENOBUFS);
1829
1830 MCLGET(m, M_DONTWAIT);
1831 if ((m->m_flags & M_EXT) == 0) {
1832 m_freem(m);
1833 return (ENOBUFS);
1834 }
1835
1836 if (rxs->rxs_mbuf != NULL)
1837 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1838
1839 rxs->rxs_mbuf = m;
1840
1841 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1842 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1843 if (error) {
1844 printf("%s: can't load rx DMA map %d, error = %d\n",
1845 sc->sc_dev.dv_xname, idx, error);
1846 panic("sip_add_rxbuf"); /* XXX */
1847 }
1848
1849 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1850 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1851
1852 SIP_INIT_RXDESC(sc, idx);
1853
1854 return (0);
1855 }
1856
1857 /*
1858 * sip_sis900_set_filter:
1859 *
1860 * Set up the receive filter.
1861 */
1862 void
1863 sip_sis900_set_filter(sc)
1864 struct sip_softc *sc;
1865 {
1866 bus_space_tag_t st = sc->sc_st;
1867 bus_space_handle_t sh = sc->sc_sh;
1868 struct ethercom *ec = &sc->sc_ethercom;
1869 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1870 struct ether_multi *enm;
1871 u_int8_t *cp;
1872 struct ether_multistep step;
1873 u_int32_t crc, mchash[8];
1874
1875 /*
1876 * Initialize the prototype RFCR.
1877 */
1878 sc->sc_rfcr = RFCR_RFEN;
1879 if (ifp->if_flags & IFF_BROADCAST)
1880 sc->sc_rfcr |= RFCR_AAB;
1881 if (ifp->if_flags & IFF_PROMISC) {
1882 sc->sc_rfcr |= RFCR_AAP;
1883 goto allmulti;
1884 }
1885
1886 /*
1887 * Set up the multicast address filter by passing all multicast
1888 * addresses through a CRC generator, and then using the high-order
1889 * 6 bits as an index into the 128 bit multicast hash table (only
1890 * the lower 16 bits of each 32 bit multicast hash register are
1891 * valid). The high order bits select the register, while the
1892 * rest of the bits select the bit within the register.
1893 */
1894
1895 memset(mchash, 0, sizeof(mchash));
1896
1897 ETHER_FIRST_MULTI(step, ec, enm);
1898 while (enm != NULL) {
1899 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1900 /*
1901 * We must listen to a range of multicast addresses.
1902 * For now, just accept all multicasts, rather than
1903 * trying to set only those filter bits needed to match
1904 * the range. (At this time, the only use of address
1905 * ranges is for IP multicast routing, for which the
1906 * range is big enough to require all bits set.)
1907 */
1908 goto allmulti;
1909 }
1910
1911 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1912
1913 /* Just want the 7 most significant bits. */
1914 crc >>= 25;
1915
1916 /* Set the corresponding bit in the hash table. */
1917 mchash[crc >> 4] |= 1 << (crc & 0xf);
1918
1919 ETHER_NEXT_MULTI(step, enm);
1920 }
1921
1922 ifp->if_flags &= ~IFF_ALLMULTI;
1923 goto setit;
1924
1925 allmulti:
1926 ifp->if_flags |= IFF_ALLMULTI;
1927 sc->sc_rfcr |= RFCR_AAM;
1928
1929 setit:
1930 #define FILTER_EMIT(addr, data) \
1931 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
1932 delay(1); \
1933 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
1934 delay(1)
1935
1936 /*
1937 * Disable receive filter, and program the node address.
1938 */
1939 cp = LLADDR(ifp->if_sadl);
1940 FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
1941 FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
1942 FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
1943
1944 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1945 /*
1946 * Program the multicast hash table.
1947 */
1948 FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
1949 FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
1950 FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
1951 FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
1952 FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
1953 FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
1954 FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
1955 FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
1956 }
1957 #undef FILTER_EMIT
1958
1959 /*
1960 * Re-enable the receiver filter.
1961 */
1962 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
1963 }
1964
1965 /*
1966 * sip_dp83815_set_filter:
1967 *
1968 * Set up the receive filter.
1969 */
1970 void
1971 sip_dp83815_set_filter(sc)
1972 struct sip_softc *sc;
1973 {
1974 bus_space_tag_t st = sc->sc_st;
1975 bus_space_handle_t sh = sc->sc_sh;
1976 struct ethercom *ec = &sc->sc_ethercom;
1977 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1978 struct ether_multi *enm;
1979 u_int8_t *cp;
1980 struct ether_multistep step;
1981 u_int32_t crc;
1982 u_int16_t mchash[32];
1983 int i;
1984
1985 /*
1986 * Initialize the prototype RFCR.
1987 * Enable the receive filter, and accept ARP
1988 * and on Perfect (destination address) Match
1989 * If IFF_BROADCAST, also accept all broadcast packets.
1990 * If IFF_PROMISC, accept all unicast packets (and later, set
1991 * IFF_ALLMULTI and accept all multicast, too).
1992 */
1993 sc->sc_rfcr = RFCR_RFEN | RFCR_AARP | RFCR_APM;
1994 if (ifp->if_flags & IFF_BROADCAST)
1995 sc->sc_rfcr |= RFCR_AAB;
1996 if (ifp->if_flags & IFF_PROMISC) {
1997 sc->sc_rfcr |= RFCR_AAP;
1998 goto allmulti;
1999 }
2000
2001 /*
2002 * Set up the multicast address filter by passing all multicast
2003 * addresses through a CRC generator, and then using the high-order
2004 * 9 bits as an index into the 512 bit multicast hash table. The
2005 * high-order bits select the slot, while the rest of the bits
2006 * select the bit within the slot. Note that only the low 16-bits
2007 * of each filter word are used, and there are 64 filter words.
2008 */
2009
2010 memset(mchash, 0, sizeof(mchash));
2011
2012 ifp->if_flags &= ~IFF_ALLMULTI;
2013 ETHER_FIRST_MULTI(step, ec, enm);
2014 if (enm != NULL) {
2015 while (enm != NULL) {
2016 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
2017 ETHER_ADDR_LEN)) {
2018 /*
2019 * We must listen to a range of multicast addresses.
2020 * For now, just accept all multicasts, rather than
2021 * trying to set only those filter bits needed to match
2022 * the range. (At this time, the only use of address
2023 * ranges is for IP multicast routing, for which the
2024 * range is big enough to require all bits set.)
2025 */
2026 goto allmulti;
2027 }
2028
2029 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
2030
2031 /* Just want the 9 most significant bits. */
2032 crc >>= 23;
2033
2034 /* Set the corresponding bit in the hash table. */
2035 mchash[crc >> 4] |= 1 << (crc & 0xf);
2036
2037 ETHER_NEXT_MULTI(step, enm);
2038 }
2039
2040 sc->sc_rfcr |= RFCR_MHEN;
2041 }
2042 goto setit;
2043
2044 allmulti:
2045 ifp->if_flags |= IFF_ALLMULTI;
2046 sc->sc_rfcr |= RFCR_AAM;
2047
2048 setit:
2049 #define FILTER_EMIT(addr, data) \
2050 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
2051 delay(1); \
2052 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
2053 delay(1);
2054
2055 /*
2056 * Disable receive filter, and program the node address.
2057 */
2058 cp = LLADDR(ifp->if_sadl);
2059 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH0, (cp[1] << 8) | cp[0]);
2060 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH2, (cp[3] << 8) | cp[2]);
2061 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH4, (cp[5] << 8) | cp[4]);
2062
2063 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2064 /*
2065 * Program the multicast hash table.
2066 */
2067 for (i = 0; i < 32; i++) {
2068 FILTER_EMIT(RFCR_NS_RFADDR_FILTMEM + (i * 2),
2069 mchash[i] & 0xffff);
2070 }
2071 }
2072 #undef FILTER_EMIT
2073
2074 /*
2075 * Re-enable the receiver filter.
2076 */
2077 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
2078 }
2079
2080 /*
2081 * sip_sis900_mii_readreg: [mii interface function]
2082 *
2083 * Read a PHY register on the MII.
2084 */
2085 int
2086 sip_sis900_mii_readreg(self, phy, reg)
2087 struct device *self;
2088 int phy, reg;
2089 {
2090 struct sip_softc *sc = (struct sip_softc *) self;
2091 u_int32_t enphy;
2092
2093 /*
2094 * The SiS 900 has only an internal PHY on the MII. Only allow
2095 * MII address 0.
2096 */
2097 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2098 return (0);
2099
2100 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2101 (phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
2102 ENPHY_RWCMD | ENPHY_ACCESS);
2103 do {
2104 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2105 } while (enphy & ENPHY_ACCESS);
2106 return ((enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT);
2107 }
2108
2109 /*
2110 * sip_sis900_mii_writereg: [mii interface function]
2111 *
2112 * Write a PHY register on the MII.
2113 */
2114 void
2115 sip_sis900_mii_writereg(self, phy, reg, val)
2116 struct device *self;
2117 int phy, reg, val;
2118 {
2119 struct sip_softc *sc = (struct sip_softc *) self;
2120 u_int32_t enphy;
2121
2122 /*
2123 * The SiS 900 has only an internal PHY on the MII. Only allow
2124 * MII address 0.
2125 */
2126 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2127 return;
2128
2129 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2130 (val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
2131 (reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
2132 do {
2133 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2134 } while (enphy & ENPHY_ACCESS);
2135 }
2136
2137 /*
2138 * sip_sis900_mii_statchg: [mii interface function]
2139 *
2140 * Callback from MII layer when media changes.
2141 */
2142 void
2143 sip_sis900_mii_statchg(self)
2144 struct device *self;
2145 {
2146 struct sip_softc *sc = (struct sip_softc *) self;
2147 u_int32_t flowctl;
2148
2149 /*
2150 * Update TXCFG for full-duplex operation.
2151 */
2152 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2153 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2154 else
2155 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2156
2157 /*
2158 * Update RXCFG for full-duplex or loopback.
2159 */
2160 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2161 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2162 sc->sc_rxcfg |= RXCFG_ATX;
2163 else
2164 sc->sc_rxcfg &= ~RXCFG_ATX;
2165
2166 /*
2167 * Update IMR for use of 802.3x flow control.
2168 */
2169 if ((sc->sc_mii.mii_media_active & IFM_FLOW) != 0) {
2170 sc->sc_imr |= (ISR_PAUSE_END|ISR_PAUSE_ST);
2171 flowctl = FLOWCTL_FLOWEN;
2172 } else {
2173 sc->sc_imr &= ~(ISR_PAUSE_END|ISR_PAUSE_ST);
2174 flowctl = 0;
2175 }
2176
2177 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2178 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2179 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
2180 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
2181 }
2182
2183 /*
2184 * sip_dp83815_mii_readreg: [mii interface function]
2185 *
2186 * Read a PHY register on the MII.
2187 */
2188 int
2189 sip_dp83815_mii_readreg(self, phy, reg)
2190 struct device *self;
2191 int phy, reg;
2192 {
2193 struct sip_softc *sc = (struct sip_softc *) self;
2194 u_int32_t val;
2195
2196 /*
2197 * The DP83815 only has an internal PHY. Only allow
2198 * MII address 0.
2199 */
2200 if (phy != 0)
2201 return (0);
2202
2203 /*
2204 * Apparently, after a reset, the DP83815 can take a while
2205 * to respond. During this recovery period, the BMSR returns
2206 * a value of 0. Catch this -- it's not supposed to happen
2207 * (the BMSR has some hardcoded-to-1 bits), and wait for the
2208 * PHY to come back to life.
2209 *
2210 * This works out because the BMSR is the first register
2211 * read during the PHY probe process.
2212 */
2213 do {
2214 val = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg));
2215 } while (reg == MII_BMSR && val == 0);
2216
2217 return (val & 0xffff);
2218 }
2219
2220 /*
2221 * sip_dp83815_mii_writereg: [mii interface function]
2222 *
2223 * Write a PHY register to the MII.
2224 */
2225 void
2226 sip_dp83815_mii_writereg(self, phy, reg, val)
2227 struct device *self;
2228 int phy, reg, val;
2229 {
2230 struct sip_softc *sc = (struct sip_softc *) self;
2231
2232 /*
2233 * The DP83815 only has an internal PHY. Only allow
2234 * MII address 0.
2235 */
2236 if (phy != 0)
2237 return;
2238
2239 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg), val);
2240 }
2241
2242 /*
2243 * sip_dp83815_mii_statchg: [mii interface function]
2244 *
2245 * Callback from MII layer when media changes.
2246 */
2247 void
2248 sip_dp83815_mii_statchg(self)
2249 struct device *self;
2250 {
2251 struct sip_softc *sc = (struct sip_softc *) self;
2252
2253 /*
2254 * Update TXCFG for full-duplex operation.
2255 */
2256 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2257 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2258 else
2259 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2260
2261 /*
2262 * Update RXCFG for full-duplex or loopback.
2263 */
2264 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2265 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2266 sc->sc_rxcfg |= RXCFG_ATX;
2267 else
2268 sc->sc_rxcfg &= ~RXCFG_ATX;
2269
2270 /*
2271 * XXX 802.3x flow control.
2272 */
2273
2274 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2275 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2276 }
2277
2278 void
2279 sip_sis900_read_macaddr(sc, enaddr)
2280 struct sip_softc *sc;
2281 u_int8_t *enaddr;
2282 {
2283 u_int16_t myea[ETHER_ADDR_LEN / 2];
2284
2285 sip_read_eeprom(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
2286 sizeof(myea) / sizeof(myea[0]), myea);
2287
2288 enaddr[0] = myea[0] & 0xff;
2289 enaddr[1] = myea[0] >> 8;
2290 enaddr[2] = myea[1] & 0xff;
2291 enaddr[3] = myea[1] >> 8;
2292 enaddr[4] = myea[2] & 0xff;
2293 enaddr[5] = myea[2] >> 8;
2294 }
2295
2296 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
2297 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
2298
2299 void
2300 sip_dp83815_read_macaddr(sc, enaddr)
2301 struct sip_softc *sc;
2302 u_int8_t *enaddr;
2303 {
2304 u_int16_t eeprom_data[SIP_DP83815_EEPROM_LENGTH / 2], *ea;
2305 u_int8_t cksum, *e, match;
2306 int i;
2307
2308 sip_read_eeprom(sc, 0, sizeof(eeprom_data) / sizeof(eeprom_data[0]),
2309 eeprom_data);
2310
2311 match = eeprom_data[SIP_DP83815_EEPROM_CHECKSUM/2] >> 8;
2312 match = ~(match - 1);
2313
2314 cksum = 0x55;
2315 e = (u_int8_t *) eeprom_data;
2316 for (i=0 ; i<SIP_DP83815_EEPROM_CHECKSUM ; i++) {
2317 cksum += *e++;
2318 }
2319 if (cksum != match) {
2320 printf("%s: Checksum (%x) mismatch (%x)",
2321 sc->sc_dev.dv_xname, cksum, match);
2322 }
2323
2324 /*
2325 * Unrolled because it makes slightly more sense this way.
2326 * The DP83815 stores the MAC address in bit 0 of word 6
2327 * through bit 15 of word 8.
2328 */
2329 ea = &eeprom_data[6];
2330 enaddr[0] = ((*ea & 0x1) << 7);
2331 ea++;
2332 enaddr[0] |= ((*ea & 0xFE00) >> 9);
2333 enaddr[1] = ((*ea & 0x1FE) >> 1);
2334 enaddr[2] = ((*ea & 0x1) << 7);
2335 ea++;
2336 enaddr[2] |= ((*ea & 0xFE00) >> 9);
2337 enaddr[3] = ((*ea & 0x1FE) >> 1);
2338 enaddr[4] = ((*ea & 0x1) << 7);
2339 ea++;
2340 enaddr[4] |= ((*ea & 0xFE00) >> 9);
2341 enaddr[5] = ((*ea & 0x1FE) >> 1);
2342
2343 /*
2344 * In case that's not weird enough, we also need to reverse
2345 * the bits in each byte. This all actually makes more sense
2346 * if you think about the EEPROM storage as an array of bits
2347 * being shifted into bytes, but that's not how we're looking
2348 * at it here...
2349 */
2350 for (i=0 ; i<6 ; i++)
2351 enaddr[i] = bbr(enaddr[i]);
2352 }
2353
2354 /*
2355 * sip_mediastatus: [ifmedia interface function]
2356 *
2357 * Get the current interface media status.
2358 */
2359 void
2360 sip_mediastatus(ifp, ifmr)
2361 struct ifnet *ifp;
2362 struct ifmediareq *ifmr;
2363 {
2364 struct sip_softc *sc = ifp->if_softc;
2365
2366 mii_pollstat(&sc->sc_mii);
2367 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2368 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2369 }
2370
2371 /*
2372 * sip_mediachange: [ifmedia interface function]
2373 *
2374 * Set hardware to newly-selected media.
2375 */
2376 int
2377 sip_mediachange(ifp)
2378 struct ifnet *ifp;
2379 {
2380 struct sip_softc *sc = ifp->if_softc;
2381
2382 if (ifp->if_flags & IFF_UP)
2383 mii_mediachg(&sc->sc_mii);
2384 return (0);
2385 }
2386