if_sip.c revision 1.11.4.9 1 /* $NetBSD: if_sip.c,v 1.11.4.9 2003/09/08 07:08:56 msaitoh 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 /*
1593 * Accept packets >1518 bytes (including FCS) so we can handle
1594 * 802.1q-tagged frames properly.
1595 */
1596 if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1597 sc->sc_rxcfg |= RXCFG_ALP;
1598 bus_space_write_4(st, sh, SIP_RXCFG, sc->sc_rxcfg);
1599
1600 /* Set up the receive filter. */
1601 (*sc->sc_model->sip_variant->sipv_set_filter)(sc);
1602
1603 /*
1604 * Give the transmit and receive rings to the chip.
1605 */
1606 bus_space_write_4(st, sh, SIP_TXDP, SIP_CDTXADDR(sc, sc->sc_txnext));
1607 bus_space_write_4(st, sh, SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
1608
1609 /*
1610 * Initialize the interrupt mask.
1611 */
1612 sc->sc_imr = ISR_DPERR|ISR_SSERR|ISR_RMABT|ISR_RTABT|ISR_RXSOVR|
1613 ISR_TXURN|ISR_TXDESC|ISR_RXORN|ISR_RXIDLE|ISR_RXDESC;
1614 bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
1615
1616 /*
1617 * Set the current media. Do this after initializing the prototype
1618 * IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
1619 * control.
1620 */
1621 mii_mediachg(&sc->sc_mii);
1622
1623 /*
1624 * Enable interrupts.
1625 */
1626 bus_space_write_4(st, sh, SIP_IER, IER_IE);
1627
1628 /*
1629 * Start the transmit and receive processes.
1630 */
1631 bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
1632
1633 /*
1634 * Start the one second MII clock.
1635 */
1636 callout_reset(&sc->sc_tick_ch, hz, sip_tick, sc);
1637
1638 /*
1639 * ...all done!
1640 */
1641 ifp->if_flags |= IFF_RUNNING;
1642 ifp->if_flags &= ~IFF_OACTIVE;
1643
1644 out:
1645 if (error)
1646 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1647 return (error);
1648 }
1649
1650 /*
1651 * sip_drain:
1652 *
1653 * Drain the receive queue.
1654 */
1655 void
1656 sip_rxdrain(sc)
1657 struct sip_softc *sc;
1658 {
1659 struct sip_rxsoft *rxs;
1660 int i;
1661
1662 for (i = 0; i < SIP_NRXDESC; i++) {
1663 rxs = &sc->sc_rxsoft[i];
1664 if (rxs->rxs_mbuf != NULL) {
1665 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1666 m_freem(rxs->rxs_mbuf);
1667 rxs->rxs_mbuf = NULL;
1668 }
1669 }
1670 }
1671
1672 /*
1673 * sip_stop:
1674 *
1675 * Stop transmission on the interface.
1676 */
1677 void
1678 sip_stop(sc, drain)
1679 struct sip_softc *sc;
1680 {
1681 bus_space_tag_t st = sc->sc_st;
1682 bus_space_handle_t sh = sc->sc_sh;
1683 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1684 struct sip_txsoft *txs;
1685 u_int32_t cmdsts = 0; /* DEBUG */
1686
1687 /*
1688 * Stop the one second clock.
1689 */
1690 callout_stop(&sc->sc_tick_ch);
1691
1692 /* Down the MII. */
1693 mii_down(&sc->sc_mii);
1694
1695 /*
1696 * Disable interrupts.
1697 */
1698 bus_space_write_4(st, sh, SIP_IER, 0);
1699
1700 /*
1701 * Stop receiver and transmitter.
1702 */
1703 bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
1704
1705 /*
1706 * Release any queued transmit buffers.
1707 */
1708 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1709 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1710 SIMPLEQ_NEXT(txs, txs_q) == NULL &&
1711 (le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts) &
1712 CMDSTS_INTR) == 0)
1713 printf("%s: sip_stop: last descriptor does not "
1714 "have INTR bit set\n", sc->sc_dev.dv_xname);
1715 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1716 #ifdef DIAGNOSTIC
1717 if (txs->txs_mbuf == NULL) {
1718 printf("%s: dirty txsoft with no mbuf chain\n",
1719 sc->sc_dev.dv_xname);
1720 panic("sip_stop");
1721 }
1722 #endif
1723 cmdsts |= /* DEBUG */
1724 le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1725 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1726 m_freem(txs->txs_mbuf);
1727 txs->txs_mbuf = NULL;
1728 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1729 }
1730
1731 if (drain) {
1732 /*
1733 * Release the receive buffers.
1734 */
1735 sip_rxdrain(sc);
1736 }
1737
1738 /*
1739 * Mark the interface down and cancel the watchdog timer.
1740 */
1741 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1742 ifp->if_timer = 0;
1743
1744 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
1745 (cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != SIP_NTXDESC)
1746 printf("%s: sip_stop: no INTR bits set in dirty tx "
1747 "descriptors\n", sc->sc_dev.dv_xname);
1748 }
1749
1750 /*
1751 * sip_read_eeprom:
1752 *
1753 * Read data from the serial EEPROM.
1754 */
1755 void
1756 sip_read_eeprom(sc, word, wordcnt, data)
1757 struct sip_softc *sc;
1758 int word, wordcnt;
1759 u_int16_t *data;
1760 {
1761 bus_space_tag_t st = sc->sc_st;
1762 bus_space_handle_t sh = sc->sc_sh;
1763 u_int16_t reg;
1764 int i, x;
1765
1766 for (i = 0; i < wordcnt; i++) {
1767 /* Send CHIP SELECT. */
1768 reg = EROMAR_EECS;
1769 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1770
1771 /* Shift in the READ opcode. */
1772 for (x = 3; x > 0; x--) {
1773 if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
1774 reg |= EROMAR_EEDI;
1775 else
1776 reg &= ~EROMAR_EEDI;
1777 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1778 bus_space_write_4(st, sh, SIP_EROMAR,
1779 reg | EROMAR_EESK);
1780 delay(4);
1781 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1782 delay(4);
1783 }
1784
1785 /* Shift in address. */
1786 for (x = 6; x > 0; x--) {
1787 if ((word + i) & (1 << (x - 1)))
1788 reg |= EROMAR_EEDI;
1789 else
1790 reg &= ~EROMAR_EEDI;
1791 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1792 bus_space_write_4(st, sh, SIP_EROMAR,
1793 reg | EROMAR_EESK);
1794 delay(4);
1795 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1796 delay(4);
1797 }
1798
1799 /* Shift out data. */
1800 reg = EROMAR_EECS;
1801 data[i] = 0;
1802 for (x = 16; x > 0; x--) {
1803 bus_space_write_4(st, sh, SIP_EROMAR,
1804 reg | EROMAR_EESK);
1805 delay(4);
1806 if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
1807 data[i] |= (1 << (x - 1));
1808 bus_space_write_4(st, sh, SIP_EROMAR, reg);
1809 delay(4);
1810 }
1811
1812 /* Clear CHIP SELECT. */
1813 bus_space_write_4(st, sh, SIP_EROMAR, 0);
1814 delay(4);
1815 }
1816 }
1817
1818 /*
1819 * sip_add_rxbuf:
1820 *
1821 * Add a receive buffer to the indicated descriptor.
1822 */
1823 int
1824 sip_add_rxbuf(sc, idx)
1825 struct sip_softc *sc;
1826 int idx;
1827 {
1828 struct sip_rxsoft *rxs = &sc->sc_rxsoft[idx];
1829 struct mbuf *m;
1830 int error;
1831
1832 MGETHDR(m, M_DONTWAIT, MT_DATA);
1833 if (m == NULL)
1834 return (ENOBUFS);
1835
1836 MCLGET(m, M_DONTWAIT);
1837 if ((m->m_flags & M_EXT) == 0) {
1838 m_freem(m);
1839 return (ENOBUFS);
1840 }
1841
1842 if (rxs->rxs_mbuf != NULL)
1843 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
1844
1845 rxs->rxs_mbuf = m;
1846
1847 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
1848 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1849 if (error) {
1850 printf("%s: can't load rx DMA map %d, error = %d\n",
1851 sc->sc_dev.dv_xname, idx, error);
1852 panic("sip_add_rxbuf"); /* XXX */
1853 }
1854
1855 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1856 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1857
1858 SIP_INIT_RXDESC(sc, idx);
1859
1860 return (0);
1861 }
1862
1863 /*
1864 * sip_sis900_set_filter:
1865 *
1866 * Set up the receive filter.
1867 */
1868 void
1869 sip_sis900_set_filter(sc)
1870 struct sip_softc *sc;
1871 {
1872 bus_space_tag_t st = sc->sc_st;
1873 bus_space_handle_t sh = sc->sc_sh;
1874 struct ethercom *ec = &sc->sc_ethercom;
1875 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1876 struct ether_multi *enm;
1877 u_int8_t *cp;
1878 struct ether_multistep step;
1879 u_int32_t crc, mchash[8];
1880
1881 /*
1882 * Initialize the prototype RFCR.
1883 */
1884 sc->sc_rfcr = RFCR_RFEN;
1885 if (ifp->if_flags & IFF_BROADCAST)
1886 sc->sc_rfcr |= RFCR_AAB;
1887 if (ifp->if_flags & IFF_PROMISC) {
1888 sc->sc_rfcr |= RFCR_AAP;
1889 goto allmulti;
1890 }
1891
1892 /*
1893 * Set up the multicast address filter by passing all multicast
1894 * addresses through a CRC generator, and then using the high-order
1895 * 6 bits as an index into the 128 bit multicast hash table (only
1896 * the lower 16 bits of each 32 bit multicast hash register are
1897 * valid). The high order bits select the register, while the
1898 * rest of the bits select the bit within the register.
1899 */
1900
1901 memset(mchash, 0, sizeof(mchash));
1902
1903 ETHER_FIRST_MULTI(step, ec, enm);
1904 while (enm != NULL) {
1905 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1906 /*
1907 * We must listen to a range of multicast addresses.
1908 * For now, just accept all multicasts, rather than
1909 * trying to set only those filter bits needed to match
1910 * the range. (At this time, the only use of address
1911 * ranges is for IP multicast routing, for which the
1912 * range is big enough to require all bits set.)
1913 */
1914 goto allmulti;
1915 }
1916
1917 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1918
1919 /* Just want the 7 most significant bits. */
1920 crc >>= 25;
1921
1922 /* Set the corresponding bit in the hash table. */
1923 mchash[crc >> 4] |= 1 << (crc & 0xf);
1924
1925 ETHER_NEXT_MULTI(step, enm);
1926 }
1927
1928 ifp->if_flags &= ~IFF_ALLMULTI;
1929 goto setit;
1930
1931 allmulti:
1932 ifp->if_flags |= IFF_ALLMULTI;
1933 sc->sc_rfcr |= RFCR_AAM;
1934
1935 setit:
1936 #define FILTER_EMIT(addr, data) \
1937 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
1938 delay(1); \
1939 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
1940 delay(1)
1941
1942 /*
1943 * Disable receive filter, and program the node address.
1944 */
1945 cp = LLADDR(ifp->if_sadl);
1946 FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
1947 FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
1948 FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
1949
1950 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1951 /*
1952 * Program the multicast hash table.
1953 */
1954 FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
1955 FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
1956 FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
1957 FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
1958 FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
1959 FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
1960 FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
1961 FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
1962 }
1963 #undef FILTER_EMIT
1964
1965 /*
1966 * Re-enable the receiver filter.
1967 */
1968 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
1969 }
1970
1971 /*
1972 * sip_dp83815_set_filter:
1973 *
1974 * Set up the receive filter.
1975 */
1976 void
1977 sip_dp83815_set_filter(sc)
1978 struct sip_softc *sc;
1979 {
1980 bus_space_tag_t st = sc->sc_st;
1981 bus_space_handle_t sh = sc->sc_sh;
1982 struct ethercom *ec = &sc->sc_ethercom;
1983 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1984 struct ether_multi *enm;
1985 u_int8_t *cp;
1986 struct ether_multistep step;
1987 u_int32_t crc;
1988 u_int16_t mchash[32];
1989 int i;
1990
1991 /*
1992 * Initialize the prototype RFCR.
1993 * Enable the receive filter, and accept ARP
1994 * and on Perfect (destination address) Match
1995 * If IFF_BROADCAST, also accept all broadcast packets.
1996 * If IFF_PROMISC, accept all unicast packets (and later, set
1997 * IFF_ALLMULTI and accept all multicast, too).
1998 */
1999 sc->sc_rfcr = RFCR_RFEN | RFCR_AARP | RFCR_APM;
2000 if (ifp->if_flags & IFF_BROADCAST)
2001 sc->sc_rfcr |= RFCR_AAB;
2002 if (ifp->if_flags & IFF_PROMISC) {
2003 sc->sc_rfcr |= RFCR_AAP;
2004 goto allmulti;
2005 }
2006
2007 /*
2008 * Set up the multicast address filter by passing all multicast
2009 * addresses through a CRC generator, and then using the high-order
2010 * 9 bits as an index into the 512 bit multicast hash table. The
2011 * high-order bits select the slot, while the rest of the bits
2012 * select the bit within the slot. Note that only the low 16-bits
2013 * of each filter word are used, and there are 64 filter words.
2014 */
2015
2016 memset(mchash, 0, sizeof(mchash));
2017
2018 ifp->if_flags &= ~IFF_ALLMULTI;
2019 ETHER_FIRST_MULTI(step, ec, enm);
2020 if (enm != NULL) {
2021 while (enm != NULL) {
2022 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
2023 ETHER_ADDR_LEN)) {
2024 /*
2025 * We must listen to a range of multicast addresses.
2026 * For now, just accept all multicasts, rather than
2027 * trying to set only those filter bits needed to match
2028 * the range. (At this time, the only use of address
2029 * ranges is for IP multicast routing, for which the
2030 * range is big enough to require all bits set.)
2031 */
2032 goto allmulti;
2033 }
2034
2035 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
2036
2037 /* Just want the 9 most significant bits. */
2038 crc >>= 23;
2039
2040 /* Set the corresponding bit in the hash table. */
2041 mchash[crc >> 4] |= 1 << (crc & 0xf);
2042
2043 ETHER_NEXT_MULTI(step, enm);
2044 }
2045
2046 sc->sc_rfcr |= RFCR_MHEN;
2047 }
2048 goto setit;
2049
2050 allmulti:
2051 ifp->if_flags |= IFF_ALLMULTI;
2052 sc->sc_rfcr |= RFCR_AAM;
2053
2054 setit:
2055 #define FILTER_EMIT(addr, data) \
2056 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
2057 delay(1); \
2058 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
2059 delay(1);
2060
2061 /*
2062 * Disable receive filter, and program the node address.
2063 */
2064 cp = LLADDR(ifp->if_sadl);
2065 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH0, (cp[1] << 8) | cp[0]);
2066 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH2, (cp[3] << 8) | cp[2]);
2067 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH4, (cp[5] << 8) | cp[4]);
2068
2069 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2070 /*
2071 * Program the multicast hash table.
2072 */
2073 for (i = 0; i < 32; i++) {
2074 FILTER_EMIT(RFCR_NS_RFADDR_FILTMEM + (i * 2),
2075 mchash[i] & 0xffff);
2076 }
2077 }
2078 #undef FILTER_EMIT
2079
2080 /*
2081 * Re-enable the receiver filter.
2082 */
2083 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
2084 }
2085
2086 /*
2087 * sip_sis900_mii_readreg: [mii interface function]
2088 *
2089 * Read a PHY register on the MII.
2090 */
2091 int
2092 sip_sis900_mii_readreg(self, phy, reg)
2093 struct device *self;
2094 int phy, reg;
2095 {
2096 struct sip_softc *sc = (struct sip_softc *) self;
2097 u_int32_t enphy;
2098
2099 /*
2100 * The SiS 900 has only an internal PHY on the MII. Only allow
2101 * MII address 0.
2102 */
2103 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2104 return (0);
2105
2106 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2107 (phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
2108 ENPHY_RWCMD | ENPHY_ACCESS);
2109 do {
2110 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2111 } while (enphy & ENPHY_ACCESS);
2112 return ((enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT);
2113 }
2114
2115 /*
2116 * sip_sis900_mii_writereg: [mii interface function]
2117 *
2118 * Write a PHY register on the MII.
2119 */
2120 void
2121 sip_sis900_mii_writereg(self, phy, reg, val)
2122 struct device *self;
2123 int phy, reg, val;
2124 {
2125 struct sip_softc *sc = (struct sip_softc *) self;
2126 u_int32_t enphy;
2127
2128 /*
2129 * The SiS 900 has only an internal PHY on the MII. Only allow
2130 * MII address 0.
2131 */
2132 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2133 return;
2134
2135 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2136 (val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
2137 (reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
2138 do {
2139 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2140 } while (enphy & ENPHY_ACCESS);
2141 }
2142
2143 /*
2144 * sip_sis900_mii_statchg: [mii interface function]
2145 *
2146 * Callback from MII layer when media changes.
2147 */
2148 void
2149 sip_sis900_mii_statchg(self)
2150 struct device *self;
2151 {
2152 struct sip_softc *sc = (struct sip_softc *) self;
2153 u_int32_t flowctl;
2154
2155 /*
2156 * Update TXCFG for full-duplex operation.
2157 */
2158 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2159 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2160 else
2161 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2162
2163 /*
2164 * Update RXCFG for full-duplex or loopback.
2165 */
2166 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2167 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2168 sc->sc_rxcfg |= RXCFG_ATX;
2169 else
2170 sc->sc_rxcfg &= ~RXCFG_ATX;
2171
2172 /*
2173 * Update IMR for use of 802.3x flow control.
2174 */
2175 if ((sc->sc_mii.mii_media_active & IFM_FLOW) != 0) {
2176 sc->sc_imr |= (ISR_PAUSE_END|ISR_PAUSE_ST);
2177 flowctl = FLOWCTL_FLOWEN;
2178 } else {
2179 sc->sc_imr &= ~(ISR_PAUSE_END|ISR_PAUSE_ST);
2180 flowctl = 0;
2181 }
2182
2183 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2184 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2185 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
2186 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
2187 }
2188
2189 /*
2190 * sip_dp83815_mii_readreg: [mii interface function]
2191 *
2192 * Read a PHY register on the MII.
2193 */
2194 int
2195 sip_dp83815_mii_readreg(self, phy, reg)
2196 struct device *self;
2197 int phy, reg;
2198 {
2199 struct sip_softc *sc = (struct sip_softc *) self;
2200 u_int32_t val;
2201
2202 /*
2203 * The DP83815 only has an internal PHY. Only allow
2204 * MII address 0.
2205 */
2206 if (phy != 0)
2207 return (0);
2208
2209 /*
2210 * Apparently, after a reset, the DP83815 can take a while
2211 * to respond. During this recovery period, the BMSR returns
2212 * a value of 0. Catch this -- it's not supposed to happen
2213 * (the BMSR has some hardcoded-to-1 bits), and wait for the
2214 * PHY to come back to life.
2215 *
2216 * This works out because the BMSR is the first register
2217 * read during the PHY probe process.
2218 */
2219 do {
2220 val = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg));
2221 } while (reg == MII_BMSR && val == 0);
2222
2223 return (val & 0xffff);
2224 }
2225
2226 /*
2227 * sip_dp83815_mii_writereg: [mii interface function]
2228 *
2229 * Write a PHY register to the MII.
2230 */
2231 void
2232 sip_dp83815_mii_writereg(self, phy, reg, val)
2233 struct device *self;
2234 int phy, reg, val;
2235 {
2236 struct sip_softc *sc = (struct sip_softc *) self;
2237
2238 /*
2239 * The DP83815 only has an internal PHY. Only allow
2240 * MII address 0.
2241 */
2242 if (phy != 0)
2243 return;
2244
2245 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg), val);
2246 }
2247
2248 /*
2249 * sip_dp83815_mii_statchg: [mii interface function]
2250 *
2251 * Callback from MII layer when media changes.
2252 */
2253 void
2254 sip_dp83815_mii_statchg(self)
2255 struct device *self;
2256 {
2257 struct sip_softc *sc = (struct sip_softc *) self;
2258
2259 /*
2260 * Update TXCFG for full-duplex operation.
2261 */
2262 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2263 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2264 else
2265 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2266
2267 /*
2268 * Update RXCFG for full-duplex or loopback.
2269 */
2270 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2271 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2272 sc->sc_rxcfg |= RXCFG_ATX;
2273 else
2274 sc->sc_rxcfg &= ~RXCFG_ATX;
2275
2276 /*
2277 * XXX 802.3x flow control.
2278 */
2279
2280 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2281 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2282 }
2283
2284 void
2285 sip_sis900_read_macaddr(sc, enaddr)
2286 struct sip_softc *sc;
2287 u_int8_t *enaddr;
2288 {
2289 u_int16_t myea[ETHER_ADDR_LEN / 2];
2290
2291 sip_read_eeprom(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
2292 sizeof(myea) / sizeof(myea[0]), myea);
2293
2294 enaddr[0] = myea[0] & 0xff;
2295 enaddr[1] = myea[0] >> 8;
2296 enaddr[2] = myea[1] & 0xff;
2297 enaddr[3] = myea[1] >> 8;
2298 enaddr[4] = myea[2] & 0xff;
2299 enaddr[5] = myea[2] >> 8;
2300 }
2301
2302 static u_char bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
2303 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
2304
2305 void
2306 sip_dp83815_read_macaddr(sc, enaddr)
2307 struct sip_softc *sc;
2308 u_int8_t *enaddr;
2309 {
2310 u_int16_t eeprom_data[SIP_DP83815_EEPROM_LENGTH / 2], *ea;
2311 u_int8_t cksum, *e, match;
2312 int i;
2313
2314 sip_read_eeprom(sc, 0, sizeof(eeprom_data) / sizeof(eeprom_data[0]),
2315 eeprom_data);
2316
2317 match = eeprom_data[SIP_DP83815_EEPROM_CHECKSUM/2] >> 8;
2318 match = ~(match - 1);
2319
2320 cksum = 0x55;
2321 e = (u_int8_t *) eeprom_data;
2322 for (i=0 ; i<SIP_DP83815_EEPROM_CHECKSUM ; i++) {
2323 cksum += *e++;
2324 }
2325 if (cksum != match) {
2326 printf("%s: Checksum (%x) mismatch (%x)",
2327 sc->sc_dev.dv_xname, cksum, match);
2328 }
2329
2330 /*
2331 * Unrolled because it makes slightly more sense this way.
2332 * The DP83815 stores the MAC address in bit 0 of word 6
2333 * through bit 15 of word 8.
2334 */
2335 ea = &eeprom_data[6];
2336 enaddr[0] = ((*ea & 0x1) << 7);
2337 ea++;
2338 enaddr[0] |= ((*ea & 0xFE00) >> 9);
2339 enaddr[1] = ((*ea & 0x1FE) >> 1);
2340 enaddr[2] = ((*ea & 0x1) << 7);
2341 ea++;
2342 enaddr[2] |= ((*ea & 0xFE00) >> 9);
2343 enaddr[3] = ((*ea & 0x1FE) >> 1);
2344 enaddr[4] = ((*ea & 0x1) << 7);
2345 ea++;
2346 enaddr[4] |= ((*ea & 0xFE00) >> 9);
2347 enaddr[5] = ((*ea & 0x1FE) >> 1);
2348
2349 /*
2350 * In case that's not weird enough, we also need to reverse
2351 * the bits in each byte. This all actually makes more sense
2352 * if you think about the EEPROM storage as an array of bits
2353 * being shifted into bytes, but that's not how we're looking
2354 * at it here...
2355 */
2356 for (i=0 ; i<6 ; i++)
2357 enaddr[i] = bbr(enaddr[i]);
2358 }
2359
2360 /*
2361 * sip_mediastatus: [ifmedia interface function]
2362 *
2363 * Get the current interface media status.
2364 */
2365 void
2366 sip_mediastatus(ifp, ifmr)
2367 struct ifnet *ifp;
2368 struct ifmediareq *ifmr;
2369 {
2370 struct sip_softc *sc = ifp->if_softc;
2371
2372 mii_pollstat(&sc->sc_mii);
2373 ifmr->ifm_status = sc->sc_mii.mii_media_status;
2374 ifmr->ifm_active = sc->sc_mii.mii_media_active;
2375 }
2376
2377 /*
2378 * sip_mediachange: [ifmedia interface function]
2379 *
2380 * Set hardware to newly-selected media.
2381 */
2382 int
2383 sip_mediachange(ifp)
2384 struct ifnet *ifp;
2385 {
2386 struct sip_softc *sc = ifp->if_softc;
2387
2388 if (ifp->if_flags & IFF_UP)
2389 mii_mediachg(&sc->sc_mii);
2390 return (0);
2391 }
2392