if_sip.c revision 1.39 1 /* $NetBSD: if_sip.c,v 1.39 2001/07/08 16:56:50 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1999 Network Computer, Inc.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of Network Computer, Inc. nor the names of its
52 * contributors may be used to endorse or promote products derived
53 * from this software without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY NETWORK COMPUTER, INC. AND CONTRIBUTORS
56 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
59 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65 * POSSIBILITY OF SUCH DAMAGE.
66 */
67
68 /*
69 * Device driver for the Silicon Integrated Systems SiS 900,
70 * SiS 7016 10/100, National Semiconductor DP83815 10/100, and
71 * National Semiconductor DP83820 10/100/1000 PCI Ethernet
72 * controllers.
73 *
74 * Originally written to support the SiS 900 by Jason R. Thorpe for
75 * Network Computer, Inc.
76 *
77 * TODO:
78 *
79 * - Support the 10-bit interface on the DP83820 (for fiber).
80 *
81 * - Reduce the interrupt load.
82 */
83
84 #include "bpfilter.h"
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/callout.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/kernel.h>
92 #include <sys/socket.h>
93 #include <sys/ioctl.h>
94 #include <sys/errno.h>
95 #include <sys/device.h>
96 #include <sys/queue.h>
97
98 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
99
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_media.h>
103 #include <net/if_ether.h>
104
105 #if NBPFILTER > 0
106 #include <net/bpf.h>
107 #endif
108
109 #include <machine/bus.h>
110 #include <machine/intr.h>
111 #include <machine/endian.h>
112
113 #include <dev/mii/mii.h>
114 #include <dev/mii/miivar.h>
115 #ifdef DP83820
116 #include <dev/mii/mii_bitbang.h>
117 #endif /* DP83820 */
118
119 #include <dev/pci/pcireg.h>
120 #include <dev/pci/pcivar.h>
121 #include <dev/pci/pcidevs.h>
122
123 #include <dev/pci/if_sipreg.h>
124
125 #ifdef DP83820 /* DP83820 Gigabit Ethernet */
126 #define SIP_DECL(x) __CONCAT(gsip_,x)
127 #else /* SiS900 and DP83815 */
128 #define SIP_DECL(x) __CONCAT(sip_,x)
129 #endif
130
131 #define SIP_STR(x) __STRING(SIP_DECL(x))
132
133 /*
134 * Transmit descriptor list size. This is arbitrary, but allocate
135 * enough descriptors for 128 pending transmissions, and 8 segments
136 * per packet. This MUST work out to a power of 2.
137 */
138 #define SIP_NTXSEGS 8
139
140 #define SIP_TXQUEUELEN 256
141 #define SIP_NTXDESC (SIP_TXQUEUELEN * SIP_NTXSEGS)
142 #define SIP_NTXDESC_MASK (SIP_NTXDESC - 1)
143 #define SIP_NEXTTX(x) (((x) + 1) & SIP_NTXDESC_MASK)
144
145 /*
146 * Receive descriptor list size. We have one Rx buffer per incoming
147 * packet, so this logic is a little simpler.
148 *
149 * Actually, on the DP83820, we allow the packet to consume more than
150 * one buffer, in order to support jumbo Ethernet frames. In that
151 * case, a packet may consume up to 5 buffers (assuming a 2048 byte
152 * mbuf cluster). 256 receive buffers is only 51 maximum size packets,
153 * so we'd better be quick about handling receive interrupts.
154 */
155 #if defined(DP83820)
156 #define SIP_NRXDESC 256
157 #else
158 #define SIP_NRXDESC 128
159 #endif /* DP83820 */
160 #define SIP_NRXDESC_MASK (SIP_NRXDESC - 1)
161 #define SIP_NEXTRX(x) (((x) + 1) & SIP_NRXDESC_MASK)
162
163 /*
164 * Control structures are DMA'd to the SiS900 chip. We allocate them in
165 * a single clump that maps to a single DMA segment to make several things
166 * easier.
167 */
168 struct sip_control_data {
169 /*
170 * The transmit descriptors.
171 */
172 struct sip_desc scd_txdescs[SIP_NTXDESC];
173
174 /*
175 * The receive descriptors.
176 */
177 struct sip_desc scd_rxdescs[SIP_NRXDESC];
178 };
179
180 #define SIP_CDOFF(x) offsetof(struct sip_control_data, x)
181 #define SIP_CDTXOFF(x) SIP_CDOFF(scd_txdescs[(x)])
182 #define SIP_CDRXOFF(x) SIP_CDOFF(scd_rxdescs[(x)])
183
184 /*
185 * Software state for transmit jobs.
186 */
187 struct sip_txsoft {
188 struct mbuf *txs_mbuf; /* head of our mbuf chain */
189 bus_dmamap_t txs_dmamap; /* our DMA map */
190 int txs_firstdesc; /* first descriptor in packet */
191 int txs_lastdesc; /* last descriptor in packet */
192 SIMPLEQ_ENTRY(sip_txsoft) txs_q;
193 };
194
195 SIMPLEQ_HEAD(sip_txsq, sip_txsoft);
196
197 /*
198 * Software state for receive jobs.
199 */
200 struct sip_rxsoft {
201 struct mbuf *rxs_mbuf; /* head of our mbuf chain */
202 bus_dmamap_t rxs_dmamap; /* our DMA map */
203 };
204
205 /*
206 * Software state per device.
207 */
208 struct sip_softc {
209 struct device sc_dev; /* generic device information */
210 bus_space_tag_t sc_st; /* bus space tag */
211 bus_space_handle_t sc_sh; /* bus space handle */
212 bus_dma_tag_t sc_dmat; /* bus DMA tag */
213 struct ethercom sc_ethercom; /* ethernet common data */
214 void *sc_sdhook; /* shutdown hook */
215
216 const struct sip_product *sc_model; /* which model are we? */
217
218 void *sc_ih; /* interrupt cookie */
219
220 struct mii_data sc_mii; /* MII/media information */
221
222 struct callout sc_tick_ch; /* tick callout */
223
224 bus_dmamap_t sc_cddmamap; /* control data DMA map */
225 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
226
227 /*
228 * Software state for transmit and receive descriptors.
229 */
230 struct sip_txsoft sc_txsoft[SIP_TXQUEUELEN];
231 struct sip_rxsoft sc_rxsoft[SIP_NRXDESC];
232
233 /*
234 * Control data structures.
235 */
236 struct sip_control_data *sc_control_data;
237 #define sc_txdescs sc_control_data->scd_txdescs
238 #define sc_rxdescs sc_control_data->scd_rxdescs
239
240 #ifdef SIP_EVENT_COUNTERS
241 /*
242 * Event counters.
243 */
244 struct evcnt sc_ev_txsstall; /* Tx stalled due to no txs */
245 struct evcnt sc_ev_txdstall; /* Tx stalled due to no txd */
246 struct evcnt sc_ev_txintr; /* Tx interrupts */
247 struct evcnt sc_ev_rxintr; /* Rx interrupts */
248 #ifdef DP83820
249 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
250 struct evcnt sc_ev_rxtcpsum; /* TCP checksums checked in-bound */
251 struct evcnt sc_ev_rxudpsum; /* UDP checksums checked in-boudn */
252 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
253 struct evcnt sc_ev_txtcpsum; /* TCP checksums comp. out-bound */
254 struct evcnt sc_ev_txudpsum; /* UDP checksums comp. out-bound */
255 #endif /* DP83820 */
256 #endif /* SIP_EVENT_COUNTERS */
257
258 u_int32_t sc_txcfg; /* prototype TXCFG register */
259 u_int32_t sc_rxcfg; /* prototype RXCFG register */
260 u_int32_t sc_imr; /* prototype IMR register */
261 u_int32_t sc_rfcr; /* prototype RFCR register */
262
263 u_int32_t sc_cfg; /* prototype CFG register */
264
265 #ifdef DP83820
266 u_int32_t sc_gpior; /* prototype GPIOR register */
267 #endif /* DP83820 */
268
269 u_int32_t sc_tx_fill_thresh; /* transmit fill threshold */
270 u_int32_t sc_tx_drain_thresh; /* transmit drain threshold */
271
272 u_int32_t sc_rx_drain_thresh; /* receive drain threshold */
273
274 int sc_flags; /* misc. flags; see below */
275
276 int sc_txfree; /* number of free Tx descriptors */
277 int sc_txnext; /* next ready Tx descriptor */
278
279 struct sip_txsq sc_txfreeq; /* free Tx descsofts */
280 struct sip_txsq sc_txdirtyq; /* dirty Tx descsofts */
281
282 int sc_rxptr; /* next ready Rx descriptor/descsoft */
283 #if defined(DP83820)
284 int sc_rxdiscard;
285 int sc_rxlen;
286 struct mbuf *sc_rxhead;
287 struct mbuf *sc_rxtail;
288 struct mbuf **sc_rxtailp;
289 #endif /* DP83820 */
290 };
291
292 /* sc_flags */
293 #define SIPF_PAUSED 0x00000001 /* paused (802.3x flow control) */
294
295 #ifdef DP83820
296 #define SIP_RXCHAIN_RESET(sc) \
297 do { \
298 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
299 *(sc)->sc_rxtailp = NULL; \
300 (sc)->sc_rxlen = 0; \
301 } while (/*CONSTCOND*/0)
302
303 #define SIP_RXCHAIN_LINK(sc, m) \
304 do { \
305 *(sc)->sc_rxtailp = sc->sc_rxtail = (m); \
306 (sc)->sc_rxtailp = &(m)->m_next; \
307 } while (/*CONSTCOND*/0)
308 #endif /* DP83820 */
309
310 #ifdef SIP_EVENT_COUNTERS
311 #define SIP_EVCNT_INCR(ev) (ev)->ev_count++
312 #else
313 #define SIP_EVCNT_INCR(ev) /* nothing */
314 #endif
315
316 #define SIP_CDTXADDR(sc, x) ((sc)->sc_cddma + SIP_CDTXOFF((x)))
317 #define SIP_CDRXADDR(sc, x) ((sc)->sc_cddma + SIP_CDRXOFF((x)))
318
319 #define SIP_CDTXSYNC(sc, x, n, ops) \
320 do { \
321 int __x, __n; \
322 \
323 __x = (x); \
324 __n = (n); \
325 \
326 /* If it will wrap around, sync to the end of the ring. */ \
327 if ((__x + __n) > SIP_NTXDESC) { \
328 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
329 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * \
330 (SIP_NTXDESC - __x), (ops)); \
331 __n -= (SIP_NTXDESC - __x); \
332 __x = 0; \
333 } \
334 \
335 /* Now sync whatever is left. */ \
336 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
337 SIP_CDTXOFF(__x), sizeof(struct sip_desc) * __n, (ops)); \
338 } while (0)
339
340 #define SIP_CDRXSYNC(sc, x, ops) \
341 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
342 SIP_CDRXOFF((x)), sizeof(struct sip_desc), (ops))
343
344 #ifdef DP83820
345 #define SIP_INIT_RXDESC_EXTSTS __sipd->sipd_extsts = 0;
346 #define SIP_RXBUF_LEN (MCLBYTES - 4)
347 #else
348 #define SIP_INIT_RXDESC_EXTSTS /* nothing */
349 #define SIP_RXBUF_LEN (MCLBYTES - 1) /* field width */
350 #endif
351 #define SIP_INIT_RXDESC(sc, x) \
352 do { \
353 struct sip_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)]; \
354 struct sip_desc *__sipd = &(sc)->sc_rxdescs[(x)]; \
355 \
356 __sipd->sipd_link = \
357 htole32(SIP_CDRXADDR((sc), SIP_NEXTRX((x)))); \
358 __sipd->sipd_bufptr = \
359 htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr); \
360 __sipd->sipd_cmdsts = htole32(CMDSTS_INTR | \
361 (SIP_RXBUF_LEN & CMDSTS_SIZE_MASK)); \
362 SIP_INIT_RXDESC_EXTSTS \
363 SIP_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
364 } while (0)
365
366 #define SIP_TIMEOUT 1000
367
368 void SIP_DECL(start)(struct ifnet *);
369 void SIP_DECL(watchdog)(struct ifnet *);
370 int SIP_DECL(ioctl)(struct ifnet *, u_long, caddr_t);
371 int SIP_DECL(init)(struct ifnet *);
372 void SIP_DECL(stop)(struct ifnet *, int);
373
374 void SIP_DECL(shutdown)(void *);
375
376 void SIP_DECL(reset)(struct sip_softc *);
377 void SIP_DECL(rxdrain)(struct sip_softc *);
378 int SIP_DECL(add_rxbuf)(struct sip_softc *, int);
379 void SIP_DECL(read_eeprom)(struct sip_softc *, int, int, u_int16_t *);
380 void SIP_DECL(tick)(void *);
381
382 #if !defined(DP83820)
383 void SIP_DECL(sis900_set_filter)(struct sip_softc *);
384 #endif /* ! DP83820 */
385 void SIP_DECL(dp83815_set_filter)(struct sip_softc *);
386
387 #if defined(DP83820)
388 void SIP_DECL(dp83820_read_macaddr)(struct sip_softc *, u_int8_t *);
389 #else
390 void SIP_DECL(sis900_read_macaddr)(struct sip_softc *, u_int8_t *);
391 void SIP_DECL(dp83815_read_macaddr)(struct sip_softc *, u_int8_t *);
392 #endif /* DP83820 */
393
394 int SIP_DECL(intr)(void *);
395 void SIP_DECL(txintr)(struct sip_softc *);
396 void SIP_DECL(rxintr)(struct sip_softc *);
397
398 #if defined(DP83820)
399 int SIP_DECL(dp83820_mii_readreg)(struct device *, int, int);
400 void SIP_DECL(dp83820_mii_writereg)(struct device *, int, int, int);
401 void SIP_DECL(dp83820_mii_statchg)(struct device *);
402 #else
403 int SIP_DECL(sis900_mii_readreg)(struct device *, int, int);
404 void SIP_DECL(sis900_mii_writereg)(struct device *, int, int, int);
405 void SIP_DECL(sis900_mii_statchg)(struct device *);
406
407 int SIP_DECL(dp83815_mii_readreg)(struct device *, int, int);
408 void SIP_DECL(dp83815_mii_writereg)(struct device *, int, int, int);
409 void SIP_DECL(dp83815_mii_statchg)(struct device *);
410 #endif /* DP83820 */
411
412 int SIP_DECL(mediachange)(struct ifnet *);
413 void SIP_DECL(mediastatus)(struct ifnet *, struct ifmediareq *);
414
415 int SIP_DECL(match)(struct device *, struct cfdata *, void *);
416 void SIP_DECL(attach)(struct device *, struct device *, void *);
417
418 int SIP_DECL(copy_small) = 0;
419
420 struct cfattach SIP_DECL(ca) = {
421 sizeof(struct sip_softc), SIP_DECL(match), SIP_DECL(attach),
422 };
423
424 /*
425 * Descriptions of the variants of the SiS900.
426 */
427 struct sip_variant {
428 int (*sipv_mii_readreg)(struct device *, int, int);
429 void (*sipv_mii_writereg)(struct device *, int, int, int);
430 void (*sipv_mii_statchg)(struct device *);
431 void (*sipv_set_filter)(struct sip_softc *);
432 void (*sipv_read_macaddr)(struct sip_softc *, u_int8_t *);
433 };
434
435 #if defined(DP83820)
436 u_int32_t SIP_DECL(dp83820_mii_bitbang_read)(struct device *);
437 void SIP_DECL(dp83820_mii_bitbang_write)(struct device *, u_int32_t);
438
439 const struct mii_bitbang_ops SIP_DECL(dp83820_mii_bitbang_ops) = {
440 SIP_DECL(dp83820_mii_bitbang_read),
441 SIP_DECL(dp83820_mii_bitbang_write),
442 {
443 EROMAR_MDIO, /* MII_BIT_MDO */
444 EROMAR_MDIO, /* MII_BIT_MDI */
445 EROMAR_MDC, /* MII_BIT_MDC */
446 EROMAR_MDDIR, /* MII_BIT_DIR_HOST_PHY */
447 0, /* MII_BIT_DIR_PHY_HOST */
448 }
449 };
450 #endif /* DP83820 */
451
452 #if defined(DP83820)
453 const struct sip_variant SIP_DECL(variant_dp83820) = {
454 SIP_DECL(dp83820_mii_readreg),
455 SIP_DECL(dp83820_mii_writereg),
456 SIP_DECL(dp83820_mii_statchg),
457 SIP_DECL(dp83815_set_filter),
458 SIP_DECL(dp83820_read_macaddr),
459 };
460 #else
461 const struct sip_variant SIP_DECL(variant_sis900) = {
462 SIP_DECL(sis900_mii_readreg),
463 SIP_DECL(sis900_mii_writereg),
464 SIP_DECL(sis900_mii_statchg),
465 SIP_DECL(sis900_set_filter),
466 SIP_DECL(sis900_read_macaddr),
467 };
468
469 const struct sip_variant SIP_DECL(variant_dp83815) = {
470 SIP_DECL(dp83815_mii_readreg),
471 SIP_DECL(dp83815_mii_writereg),
472 SIP_DECL(dp83815_mii_statchg),
473 SIP_DECL(dp83815_set_filter),
474 SIP_DECL(dp83815_read_macaddr),
475 };
476 #endif /* DP83820 */
477
478 /*
479 * Devices supported by this driver.
480 */
481 const struct sip_product {
482 pci_vendor_id_t sip_vendor;
483 pci_product_id_t sip_product;
484 const char *sip_name;
485 const struct sip_variant *sip_variant;
486 } SIP_DECL(products)[] = {
487 #if defined(DP83820)
488 { PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83820,
489 "NatSemi DP83820 Gigabit Ethernet",
490 &SIP_DECL(variant_dp83820) },
491 #else
492 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_900,
493 "SiS 900 10/100 Ethernet",
494 &SIP_DECL(variant_sis900) },
495 { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_7016,
496 "SiS 7016 10/100 Ethernet",
497 &SIP_DECL(variant_sis900) },
498
499 { PCI_VENDOR_NS, PCI_PRODUCT_NS_DP83815,
500 "NatSemi DP83815 10/100 Ethernet",
501 &SIP_DECL(variant_dp83815) },
502 #endif /* DP83820 */
503
504 { 0, 0,
505 NULL,
506 NULL },
507 };
508
509 static const struct sip_product *
510 SIP_DECL(lookup)(const struct pci_attach_args *pa)
511 {
512 const struct sip_product *sip;
513
514 for (sip = SIP_DECL(products); sip->sip_name != NULL; sip++) {
515 if (PCI_VENDOR(pa->pa_id) == sip->sip_vendor &&
516 PCI_PRODUCT(pa->pa_id) == sip->sip_product)
517 return (sip);
518 }
519 return (NULL);
520 }
521
522 int
523 SIP_DECL(match)(struct device *parent, struct cfdata *cf, void *aux)
524 {
525 struct pci_attach_args *pa = aux;
526
527 if (SIP_DECL(lookup)(pa) != NULL)
528 return (1);
529
530 return (0);
531 }
532
533 void
534 SIP_DECL(attach)(struct device *parent, struct device *self, void *aux)
535 {
536 struct sip_softc *sc = (struct sip_softc *) self;
537 struct pci_attach_args *pa = aux;
538 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
539 pci_chipset_tag_t pc = pa->pa_pc;
540 pci_intr_handle_t ih;
541 const char *intrstr = NULL;
542 bus_space_tag_t iot, memt;
543 bus_space_handle_t ioh, memh;
544 bus_dma_segment_t seg;
545 int ioh_valid, memh_valid;
546 int i, rseg, error;
547 const struct sip_product *sip;
548 pcireg_t pmode;
549 u_int8_t enaddr[ETHER_ADDR_LEN];
550 int pmreg;
551 #ifdef DP83820
552 pcireg_t memtype;
553 u_int32_t reg;
554 #endif /* DP83820 */
555
556 callout_init(&sc->sc_tick_ch);
557
558 sip = SIP_DECL(lookup)(pa);
559 if (sip == NULL) {
560 printf("\n");
561 panic(SIP_STR(attach) ": impossible");
562 }
563
564 printf(": %s\n", sip->sip_name);
565
566 sc->sc_model = sip;
567
568 /*
569 * Map the device.
570 */
571 ioh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGIOA,
572 PCI_MAPREG_TYPE_IO, 0,
573 &iot, &ioh, NULL, NULL) == 0);
574 #ifdef DP83820
575 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SIP_PCI_CFGMA);
576 switch (memtype) {
577 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
578 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
579 memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
580 memtype, 0, &memt, &memh, NULL, NULL) == 0);
581 break;
582 default:
583 memh_valid = 0;
584 }
585 #else
586 memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
587 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
588 &memt, &memh, NULL, NULL) == 0);
589 #endif /* DP83820 */
590
591 if (memh_valid) {
592 sc->sc_st = memt;
593 sc->sc_sh = memh;
594 } else if (ioh_valid) {
595 sc->sc_st = iot;
596 sc->sc_sh = ioh;
597 } else {
598 printf("%s: unable to map device registers\n",
599 sc->sc_dev.dv_xname);
600 return;
601 }
602
603 sc->sc_dmat = pa->pa_dmat;
604
605 /* Enable bus mastering. */
606 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
607 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
608 PCI_COMMAND_MASTER_ENABLE);
609
610 /* Get it out of power save mode if needed. */
611 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
612 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
613 if (pmode == 3) {
614 /*
615 * The card has lost all configuration data in
616 * this state, so punt.
617 */
618 printf("%s: unable to wake up from power state D3\n",
619 sc->sc_dev.dv_xname);
620 return;
621 }
622 if (pmode != 0) {
623 printf("%s: waking up from power state D%d\n",
624 sc->sc_dev.dv_xname, pmode);
625 pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
626 }
627 }
628
629 /*
630 * Map and establish our interrupt.
631 */
632 if (pci_intr_map(pa, &ih)) {
633 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
634 return;
635 }
636 intrstr = pci_intr_string(pc, ih);
637 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, SIP_DECL(intr), sc);
638 if (sc->sc_ih == NULL) {
639 printf("%s: unable to establish interrupt",
640 sc->sc_dev.dv_xname);
641 if (intrstr != NULL)
642 printf(" at %s", intrstr);
643 printf("\n");
644 return;
645 }
646 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
647
648 SIMPLEQ_INIT(&sc->sc_txfreeq);
649 SIMPLEQ_INIT(&sc->sc_txdirtyq);
650
651 /*
652 * Allocate the control data structures, and create and load the
653 * DMA map for it.
654 */
655 if ((error = bus_dmamem_alloc(sc->sc_dmat,
656 sizeof(struct sip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
657 0)) != 0) {
658 printf("%s: unable to allocate control data, error = %d\n",
659 sc->sc_dev.dv_xname, error);
660 goto fail_0;
661 }
662
663 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
664 sizeof(struct sip_control_data), (caddr_t *)&sc->sc_control_data,
665 BUS_DMA_COHERENT)) != 0) {
666 printf("%s: unable to map control data, error = %d\n",
667 sc->sc_dev.dv_xname, error);
668 goto fail_1;
669 }
670
671 if ((error = bus_dmamap_create(sc->sc_dmat,
672 sizeof(struct sip_control_data), 1,
673 sizeof(struct sip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
674 printf("%s: unable to create control data DMA map, "
675 "error = %d\n", sc->sc_dev.dv_xname, error);
676 goto fail_2;
677 }
678
679 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
680 sc->sc_control_data, sizeof(struct sip_control_data), NULL,
681 0)) != 0) {
682 printf("%s: unable to load control data DMA map, error = %d\n",
683 sc->sc_dev.dv_xname, error);
684 goto fail_3;
685 }
686
687 /*
688 * Create the transmit buffer DMA maps.
689 */
690 for (i = 0; i < SIP_TXQUEUELEN; i++) {
691 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
692 SIP_NTXSEGS, MCLBYTES, 0, 0,
693 &sc->sc_txsoft[i].txs_dmamap)) != 0) {
694 printf("%s: unable to create tx DMA map %d, "
695 "error = %d\n", sc->sc_dev.dv_xname, i, error);
696 goto fail_4;
697 }
698 }
699
700 /*
701 * Create the receive buffer DMA maps.
702 */
703 for (i = 0; i < SIP_NRXDESC; i++) {
704 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
705 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
706 printf("%s: unable to create rx DMA map %d, "
707 "error = %d\n", sc->sc_dev.dv_xname, i, error);
708 goto fail_5;
709 }
710 sc->sc_rxsoft[i].rxs_mbuf = NULL;
711 }
712
713 /*
714 * Reset the chip to a known state.
715 */
716 SIP_DECL(reset)(sc);
717
718 /*
719 * Read the Ethernet address from the EEPROM. This might
720 * also fetch other stuff from the EEPROM and stash it
721 * in the softc.
722 */
723 sc->sc_cfg = 0;
724 (*sip->sip_variant->sipv_read_macaddr)(sc, enaddr);
725
726 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
727 ether_sprintf(enaddr));
728
729 /*
730 * Initialize the configuration register: aggressive PCI
731 * bus request algorithm, default backoff, default OW timer,
732 * default parity error detection.
733 *
734 * NOTE: "Big endian mode" is useless on the SiS900 and
735 * friends -- it affects packet data, not descriptors.
736 */
737 #ifdef DP83820
738 reg = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CFG);
739 if (reg & CFG_PCI64_DET) {
740 printf("%s: 64-bit PCI slot detected\n", sc->sc_dev.dv_xname);
741 /*
742 * XXX Need some PCI flags indicating support for
743 * XXX 64-bit addressing (SAC or DAC) and 64-bit
744 * XXX data path.
745 */
746 }
747 if (sc->sc_cfg & (CFG_TBI_EN|CFG_EXT_125)) {
748 const char *sep = "";
749 printf("%s: using ", sc->sc_dev.dv_xname);
750 if (sc->sc_cfg & CFG_EXT_125) {
751 printf("%s125MHz clock", sep);
752 sep = ", ";
753 }
754 if (sc->sc_cfg & CFG_TBI_EN) {
755 printf("%sten-bit interface", sep);
756 sep = ", ";
757 }
758 printf("\n");
759 }
760 if ((pa->pa_flags & PCI_FLAGS_MRM_OKAY) == 0)
761 sc->sc_cfg |= CFG_MRM_DIS;
762 if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
763 sc->sc_cfg |= CFG_MWI_DIS;
764
765 /*
766 * Use the extended descriptor format on the DP83820. This
767 * gives us an interface to VLAN tagging and IPv4/TCP/UDP
768 * checksumming.
769 */
770 sc->sc_cfg |= CFG_EXTSTS_EN;
771 #endif /* DP83820 */
772
773 /*
774 * Initialize our media structures and probe the MII.
775 */
776 sc->sc_mii.mii_ifp = ifp;
777 sc->sc_mii.mii_readreg = sip->sip_variant->sipv_mii_readreg;
778 sc->sc_mii.mii_writereg = sip->sip_variant->sipv_mii_writereg;
779 sc->sc_mii.mii_statchg = sip->sip_variant->sipv_mii_statchg;
780 ifmedia_init(&sc->sc_mii.mii_media, 0, SIP_DECL(mediachange),
781 SIP_DECL(mediastatus));
782 #ifdef DP83820
783 if (sc->sc_cfg & CFG_TBI_EN) {
784 /* Using ten-bit interface. */
785 printf("%s: TBI -- FIXME\n", sc->sc_dev.dv_xname);
786 } else {
787 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
788 MII_OFFSET_ANY, 0);
789 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
790 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE,
791 0, NULL);
792 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
793 } else
794 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
795 }
796 #else
797 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
798 MII_OFFSET_ANY, 0);
799 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
800 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
801 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
802 } else
803 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
804 #endif /* DP83820 */
805
806 ifp = &sc->sc_ethercom.ec_if;
807 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
808 ifp->if_softc = sc;
809 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
810 ifp->if_ioctl = SIP_DECL(ioctl);
811 ifp->if_start = SIP_DECL(start);
812 ifp->if_watchdog = SIP_DECL(watchdog);
813 ifp->if_init = SIP_DECL(init);
814 ifp->if_stop = SIP_DECL(stop);
815 IFQ_SET_READY(&ifp->if_snd);
816
817 /*
818 * We can support 802.1Q VLAN-sized frames.
819 */
820 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
821
822 #ifdef DP83820
823 /*
824 * And the DP83820 can do VLAN tagging in hardware, and
825 * support the jumbo Ethernet MTU.
826 */
827 sc->sc_ethercom.ec_capabilities |=
828 ETHERCAP_VLAN_HWTAGGING | ETHERCAP_JUMBO_MTU;
829
830 /*
831 * The DP83820 can do IPv4, TCPv4, and UDPv4 checksums
832 * in hardware.
833 */
834 ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
835 IFCAP_CSUM_UDPv4;
836 #endif /* DP83820 */
837
838 /*
839 * Attach the interface.
840 */
841 if_attach(ifp);
842 ether_ifattach(ifp, enaddr);
843
844 #ifdef SIP_EVENT_COUNTERS
845 /*
846 * Attach event counters.
847 */
848 evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
849 NULL, sc->sc_dev.dv_xname, "txsstall");
850 evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
851 NULL, sc->sc_dev.dv_xname, "txdstall");
852 evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
853 NULL, sc->sc_dev.dv_xname, "txintr");
854 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
855 NULL, sc->sc_dev.dv_xname, "rxintr");
856 #ifdef DP83820
857 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
858 NULL, sc->sc_dev.dv_xname, "rxipsum");
859 evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
860 NULL, sc->sc_dev.dv_xname, "rxtcpsum");
861 evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
862 NULL, sc->sc_dev.dv_xname, "rxudpsum");
863 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
864 NULL, sc->sc_dev.dv_xname, "txipsum");
865 evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
866 NULL, sc->sc_dev.dv_xname, "txtcpsum");
867 evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
868 NULL, sc->sc_dev.dv_xname, "txudpsum");
869 #endif /* DP83820 */
870 #endif /* SIP_EVENT_COUNTERS */
871
872 /*
873 * Make sure the interface is shutdown during reboot.
874 */
875 sc->sc_sdhook = shutdownhook_establish(SIP_DECL(shutdown), sc);
876 if (sc->sc_sdhook == NULL)
877 printf("%s: WARNING: unable to establish shutdown hook\n",
878 sc->sc_dev.dv_xname);
879 return;
880
881 /*
882 * Free any resources we've allocated during the failed attach
883 * attempt. Do this in reverse order and fall through.
884 */
885 fail_5:
886 for (i = 0; i < SIP_NRXDESC; i++) {
887 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
888 bus_dmamap_destroy(sc->sc_dmat,
889 sc->sc_rxsoft[i].rxs_dmamap);
890 }
891 fail_4:
892 for (i = 0; i < SIP_TXQUEUELEN; i++) {
893 if (sc->sc_txsoft[i].txs_dmamap != NULL)
894 bus_dmamap_destroy(sc->sc_dmat,
895 sc->sc_txsoft[i].txs_dmamap);
896 }
897 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
898 fail_3:
899 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
900 fail_2:
901 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
902 sizeof(struct sip_control_data));
903 fail_1:
904 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
905 fail_0:
906 return;
907 }
908
909 /*
910 * sip_shutdown:
911 *
912 * Make sure the interface is stopped at reboot time.
913 */
914 void
915 SIP_DECL(shutdown)(void *arg)
916 {
917 struct sip_softc *sc = arg;
918
919 SIP_DECL(stop)(&sc->sc_ethercom.ec_if, 1);
920 }
921
922 /*
923 * sip_start: [ifnet interface function]
924 *
925 * Start packet transmission on the interface.
926 */
927 void
928 SIP_DECL(start)(struct ifnet *ifp)
929 {
930 struct sip_softc *sc = ifp->if_softc;
931 struct mbuf *m0, *m;
932 struct sip_txsoft *txs;
933 bus_dmamap_t dmamap;
934 int error, firsttx, nexttx, lasttx, ofree, seg;
935 #ifdef DP83820
936 u_int32_t extsts;
937 #endif
938
939 /*
940 * If we've been told to pause, don't transmit any more packets.
941 */
942 if (sc->sc_flags & SIPF_PAUSED)
943 ifp->if_flags |= IFF_OACTIVE;
944
945 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
946 return;
947
948 /*
949 * Remember the previous number of free descriptors and
950 * the first descriptor we'll use.
951 */
952 ofree = sc->sc_txfree;
953 firsttx = sc->sc_txnext;
954
955 /*
956 * Loop through the send queue, setting up transmit descriptors
957 * until we drain the queue, or use up all available transmit
958 * descriptors.
959 */
960 for (;;) {
961 /* Get a work queue entry. */
962 if ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) == NULL) {
963 SIP_EVCNT_INCR(&sc->sc_ev_txsstall);
964 break;
965 }
966
967 /*
968 * Grab a packet off the queue.
969 */
970 IFQ_POLL(&ifp->if_snd, m0);
971 if (m0 == NULL)
972 break;
973 #ifndef DP83820
974 m = NULL;
975 #endif
976
977 dmamap = txs->txs_dmamap;
978
979 #ifdef DP83820
980 /*
981 * Load the DMA map. If this fails, the packet either
982 * didn't fit in the allotted number of segments, or we
983 * were short on resources. For the too-many-segments
984 * case, we simply report an error and drop the packet,
985 * since we can't sanely copy a jumbo packet to a single
986 * buffer.
987 */
988 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
989 BUS_DMA_NOWAIT);
990 if (error) {
991 if (error == EFBIG) {
992 printf("%s: Tx packet consumes too many "
993 "DMA segments, dropping...\n",
994 sc->sc_dev.dv_xname);
995 IFQ_DEQUEUE(&ifp->if_snd, m0);
996 m_freem(m0);
997 continue;
998 }
999 /*
1000 * Short on resources, just stop for now.
1001 */
1002 break;
1003 }
1004 #else /* DP83820 */
1005 /*
1006 * Load the DMA map. If this fails, the packet either
1007 * didn't fit in the alloted number of segments, or we
1008 * were short on resources. In this case, we'll copy
1009 * and try again.
1010 */
1011 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
1012 BUS_DMA_NOWAIT) != 0) {
1013 MGETHDR(m, M_DONTWAIT, MT_DATA);
1014 if (m == NULL) {
1015 printf("%s: unable to allocate Tx mbuf\n",
1016 sc->sc_dev.dv_xname);
1017 break;
1018 }
1019 if (m0->m_pkthdr.len > MHLEN) {
1020 MCLGET(m, M_DONTWAIT);
1021 if ((m->m_flags & M_EXT) == 0) {
1022 printf("%s: unable to allocate Tx "
1023 "cluster\n", sc->sc_dev.dv_xname);
1024 m_freem(m);
1025 break;
1026 }
1027 }
1028 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1029 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1030 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
1031 m, BUS_DMA_NOWAIT);
1032 if (error) {
1033 printf("%s: unable to load Tx buffer, "
1034 "error = %d\n", sc->sc_dev.dv_xname, error);
1035 break;
1036 }
1037 }
1038 #endif /* DP83820 */
1039
1040 /*
1041 * Ensure we have enough descriptors free to describe
1042 * the packet. Note, we always reserve one descriptor
1043 * at the end of the ring as a termination point, to
1044 * prevent wrap-around.
1045 */
1046 if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
1047 /*
1048 * Not enough free descriptors to transmit this
1049 * packet. We haven't committed anything yet,
1050 * so just unload the DMA map, put the packet
1051 * back on the queue, and punt. Notify the upper
1052 * layer that there are not more slots left.
1053 *
1054 * XXX We could allocate an mbuf and copy, but
1055 * XXX is it worth it?
1056 */
1057 ifp->if_flags |= IFF_OACTIVE;
1058 bus_dmamap_unload(sc->sc_dmat, dmamap);
1059 #ifndef DP83820
1060 if (m != NULL)
1061 m_freem(m);
1062 #endif
1063 SIP_EVCNT_INCR(&sc->sc_ev_txdstall);
1064 break;
1065 }
1066
1067 IFQ_DEQUEUE(&ifp->if_snd, m0);
1068 #ifndef DP83820
1069 if (m != NULL) {
1070 m_freem(m0);
1071 m0 = m;
1072 }
1073 #endif
1074
1075 /*
1076 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1077 */
1078
1079 /* Sync the DMA map. */
1080 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
1081 BUS_DMASYNC_PREWRITE);
1082
1083 /*
1084 * Initialize the transmit descriptors.
1085 */
1086 for (nexttx = sc->sc_txnext, seg = 0;
1087 seg < dmamap->dm_nsegs;
1088 seg++, nexttx = SIP_NEXTTX(nexttx)) {
1089 /*
1090 * If this is the first descriptor we're
1091 * enqueueing, don't set the OWN bit just
1092 * yet. That could cause a race condition.
1093 * We'll do it below.
1094 */
1095 sc->sc_txdescs[nexttx].sipd_bufptr =
1096 htole32(dmamap->dm_segs[seg].ds_addr);
1097 sc->sc_txdescs[nexttx].sipd_cmdsts =
1098 htole32((nexttx == firsttx ? 0 : CMDSTS_OWN) |
1099 CMDSTS_MORE | dmamap->dm_segs[seg].ds_len);
1100 #ifdef DP83820
1101 sc->sc_txdescs[nexttx].sipd_extsts = 0;
1102 #endif /* DP83820 */
1103 lasttx = nexttx;
1104 }
1105
1106 /* Clear the MORE bit on the last segment. */
1107 sc->sc_txdescs[lasttx].sipd_cmdsts &= htole32(~CMDSTS_MORE);
1108
1109 #ifdef DP83820
1110 /*
1111 * If VLANs are enabled and the packet has a VLAN tag, set
1112 * up the descriptor to encapsulate the packet for us.
1113 *
1114 * This apparently has to be on the last descriptor of
1115 * the packet.
1116 */
1117 if (sc->sc_ethercom.ec_nvlans != 0 &&
1118 (m = m_aux_find(m0, AF_LINK, ETHERTYPE_VLAN)) != NULL) {
1119 sc->sc_txdescs[lasttx].sipd_extsts |=
1120 htole32(EXTSTS_VPKT |
1121 htons(*mtod(m, int *) & EXTSTS_VTCI));
1122 }
1123
1124 /*
1125 * If the upper-layer has requested IPv4/TCPv4/UDPv4
1126 * checksumming, set up the descriptor to do this work
1127 * for us.
1128 *
1129 * This apparently has to be on the first descriptor of
1130 * the packet.
1131 *
1132 * Byte-swap constants so the compiler can optimize.
1133 */
1134 extsts = 0;
1135 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
1136 KDASSERT(ifp->if_capenable & IFCAP_CSUM_IPv4);
1137 SIP_EVCNT_INCR(&sc->sc_ev_txipsum);
1138 extsts |= htole32(EXTSTS_IPPKT);
1139 }
1140 if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
1141 KDASSERT(ifp->if_capenable & IFCAP_CSUM_TCPv4);
1142 SIP_EVCNT_INCR(&sc->sc_ev_txtcpsum);
1143 extsts |= htole32(EXTSTS_TCPPKT);
1144 } else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
1145 KDASSERT(ifp->if_capenable & IFCAP_CSUM_UDPv4);
1146 SIP_EVCNT_INCR(&sc->sc_ev_txudpsum);
1147 extsts |= htole32(EXTSTS_UDPPKT);
1148 }
1149 sc->sc_txdescs[sc->sc_txnext].sipd_extsts |= extsts;
1150 #endif /* DP83820 */
1151
1152 /* Sync the descriptors we're using. */
1153 SIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
1154 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1155
1156 /*
1157 * Store a pointer to the packet so we can free it later,
1158 * and remember what txdirty will be once the packet is
1159 * done.
1160 */
1161 txs->txs_mbuf = m0;
1162 txs->txs_firstdesc = sc->sc_txnext;
1163 txs->txs_lastdesc = lasttx;
1164
1165 /* Advance the tx pointer. */
1166 sc->sc_txfree -= dmamap->dm_nsegs;
1167 sc->sc_txnext = nexttx;
1168
1169 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
1170 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1171
1172 #if NBPFILTER > 0
1173 /*
1174 * Pass the packet to any BPF listeners.
1175 */
1176 if (ifp->if_bpf)
1177 bpf_mtap(ifp->if_bpf, m0);
1178 #endif /* NBPFILTER > 0 */
1179 }
1180
1181 if (txs == NULL || sc->sc_txfree == 0) {
1182 /* No more slots left; notify upper layer. */
1183 ifp->if_flags |= IFF_OACTIVE;
1184 }
1185
1186 if (sc->sc_txfree != ofree) {
1187 /*
1188 * Cause a descriptor interrupt to happen on the
1189 * last packet we enqueued.
1190 */
1191 sc->sc_txdescs[lasttx].sipd_cmdsts |= htole32(CMDSTS_INTR);
1192 SIP_CDTXSYNC(sc, lasttx, 1,
1193 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1194
1195 /*
1196 * The entire packet chain is set up. Give the
1197 * first descrptor to the chip now.
1198 */
1199 sc->sc_txdescs[firsttx].sipd_cmdsts |= htole32(CMDSTS_OWN);
1200 SIP_CDTXSYNC(sc, firsttx, 1,
1201 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1202
1203 /*
1204 * Start the transmit process. Note, the manual says
1205 * that if there are no pending transmissions in the
1206 * chip's internal queue (indicated by TXE being clear),
1207 * then the driver software must set the TXDP to the
1208 * first descriptor to be transmitted. However, if we
1209 * do this, it causes serious performance degredation on
1210 * the DP83820 under load, not setting TXDP doesn't seem
1211 * to adversely affect the SiS 900 or DP83815.
1212 *
1213 * Well, I guess it wouldn't be the first time a manual
1214 * has lied -- and they could be speaking of the NULL-
1215 * terminated descriptor list case, rather than OWN-
1216 * terminated rings.
1217 */
1218 #if 0
1219 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR) &
1220 CR_TXE) == 0) {
1221 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP,
1222 SIP_CDTXADDR(sc, firsttx));
1223 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
1224 }
1225 #else
1226 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
1227 #endif
1228
1229 /* Set a watchdog timer in case the chip flakes out. */
1230 ifp->if_timer = 5;
1231 }
1232 }
1233
1234 /*
1235 * sip_watchdog: [ifnet interface function]
1236 *
1237 * Watchdog timer handler.
1238 */
1239 void
1240 SIP_DECL(watchdog)(struct ifnet *ifp)
1241 {
1242 struct sip_softc *sc = ifp->if_softc;
1243
1244 /*
1245 * The chip seems to ignore the CMDSTS_INTR bit sometimes!
1246 * If we get a timeout, try and sweep up transmit descriptors.
1247 * If we manage to sweep them all up, ignore the lack of
1248 * interrupt.
1249 */
1250 SIP_DECL(txintr)(sc);
1251
1252 if (sc->sc_txfree != SIP_NTXDESC) {
1253 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1254 ifp->if_oerrors++;
1255
1256 /* Reset the interface. */
1257 (void) SIP_DECL(init)(ifp);
1258 } else if (ifp->if_flags & IFF_DEBUG)
1259 printf("%s: recovered from device timeout\n",
1260 sc->sc_dev.dv_xname);
1261
1262 /* Try to get more packets going. */
1263 SIP_DECL(start)(ifp);
1264 }
1265
1266 /*
1267 * sip_ioctl: [ifnet interface function]
1268 *
1269 * Handle control requests from the operator.
1270 */
1271 int
1272 SIP_DECL(ioctl)(struct ifnet *ifp, u_long cmd, caddr_t data)
1273 {
1274 struct sip_softc *sc = ifp->if_softc;
1275 struct ifreq *ifr = (struct ifreq *)data;
1276 int s, error;
1277
1278 s = splnet();
1279
1280 switch (cmd) {
1281 case SIOCSIFMEDIA:
1282 case SIOCGIFMEDIA:
1283 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1284 break;
1285
1286 default:
1287 error = ether_ioctl(ifp, cmd, data);
1288 if (error == ENETRESET) {
1289 /*
1290 * Multicast list has changed; set the hardware filter
1291 * accordingly.
1292 */
1293 (*sc->sc_model->sip_variant->sipv_set_filter)(sc);
1294 error = 0;
1295 }
1296 break;
1297 }
1298
1299 /* Try to get more packets going. */
1300 SIP_DECL(start)(ifp);
1301
1302 splx(s);
1303 return (error);
1304 }
1305
1306 /*
1307 * sip_intr:
1308 *
1309 * Interrupt service routine.
1310 */
1311 int
1312 SIP_DECL(intr)(void *arg)
1313 {
1314 struct sip_softc *sc = arg;
1315 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1316 u_int32_t isr;
1317 int handled = 0;
1318
1319 for (;;) {
1320 /* Reading clears interrupt. */
1321 isr = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ISR);
1322 if ((isr & sc->sc_imr) == 0)
1323 break;
1324
1325 handled = 1;
1326
1327 if (isr & (ISR_RXORN|ISR_RXIDLE|ISR_RXDESC)) {
1328 SIP_EVCNT_INCR(&sc->sc_ev_rxintr);
1329
1330 /* Grab any new packets. */
1331 SIP_DECL(rxintr)(sc);
1332
1333 if (isr & ISR_RXORN) {
1334 printf("%s: receive FIFO overrun\n",
1335 sc->sc_dev.dv_xname);
1336
1337 /* XXX adjust rx_drain_thresh? */
1338 }
1339
1340 if (isr & ISR_RXIDLE) {
1341 printf("%s: receive ring overrun\n",
1342 sc->sc_dev.dv_xname);
1343
1344 /* Get the receive process going again. */
1345 bus_space_write_4(sc->sc_st, sc->sc_sh,
1346 SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
1347 bus_space_write_4(sc->sc_st, sc->sc_sh,
1348 SIP_CR, CR_RXE);
1349 }
1350 }
1351
1352 if (isr & (ISR_TXURN|ISR_TXDESC)) {
1353 SIP_EVCNT_INCR(&sc->sc_ev_txintr);
1354
1355 /* Sweep up transmit descriptors. */
1356 SIP_DECL(txintr)(sc);
1357
1358 if (isr & ISR_TXURN) {
1359 u_int32_t thresh;
1360
1361 printf("%s: transmit FIFO underrun",
1362 sc->sc_dev.dv_xname);
1363
1364 thresh = sc->sc_tx_drain_thresh + 1;
1365 if (thresh <= TXCFG_DRTH &&
1366 (thresh * 32) <= (SIP_TXFIFO_SIZE -
1367 (sc->sc_tx_fill_thresh * 32))) {
1368 printf("; increasing Tx drain "
1369 "threshold to %u bytes\n",
1370 thresh * 32);
1371 sc->sc_tx_drain_thresh = thresh;
1372 (void) SIP_DECL(init)(ifp);
1373 } else {
1374 (void) SIP_DECL(init)(ifp);
1375 printf("\n");
1376 }
1377 }
1378 }
1379
1380 #if !defined(DP83820)
1381 if (sc->sc_imr & (ISR_PAUSE_END|ISR_PAUSE_ST)) {
1382 if (isr & ISR_PAUSE_ST) {
1383 sc->sc_flags |= SIPF_PAUSED;
1384 ifp->if_flags |= IFF_OACTIVE;
1385 }
1386 if (isr & ISR_PAUSE_END) {
1387 sc->sc_flags &= ~SIPF_PAUSED;
1388 ifp->if_flags &= ~IFF_OACTIVE;
1389 }
1390 }
1391 #endif /* ! DP83820 */
1392
1393 if (isr & ISR_HIBERR) {
1394 #define PRINTERR(bit, str) \
1395 if (isr & (bit)) \
1396 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1397 PRINTERR(ISR_DPERR, "parity error");
1398 PRINTERR(ISR_SSERR, "system error");
1399 PRINTERR(ISR_RMABT, "master abort");
1400 PRINTERR(ISR_RTABT, "target abort");
1401 PRINTERR(ISR_RXSOVR, "receive status FIFO overrun");
1402 (void) SIP_DECL(init)(ifp);
1403 #undef PRINTERR
1404 }
1405 }
1406
1407 /* Try to get more packets going. */
1408 SIP_DECL(start)(ifp);
1409
1410 return (handled);
1411 }
1412
1413 /*
1414 * sip_txintr:
1415 *
1416 * Helper; handle transmit interrupts.
1417 */
1418 void
1419 SIP_DECL(txintr)(struct sip_softc *sc)
1420 {
1421 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1422 struct sip_txsoft *txs;
1423 u_int32_t cmdsts;
1424
1425 if ((sc->sc_flags & SIPF_PAUSED) == 0)
1426 ifp->if_flags &= ~IFF_OACTIVE;
1427
1428 /*
1429 * Go through our Tx list and free mbufs for those
1430 * frames which have been transmitted.
1431 */
1432 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1433 SIP_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
1434 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1435
1436 cmdsts = le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
1437 if (cmdsts & CMDSTS_OWN)
1438 break;
1439
1440 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
1441
1442 sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
1443
1444 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
1445 0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1446 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
1447 m_freem(txs->txs_mbuf);
1448 txs->txs_mbuf = NULL;
1449
1450 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1451
1452 /*
1453 * Check for errors and collisions.
1454 */
1455 if (cmdsts &
1456 (CMDSTS_Tx_TXA|CMDSTS_Tx_TFU|CMDSTS_Tx_ED|CMDSTS_Tx_EC)) {
1457 ifp->if_oerrors++;
1458 if (cmdsts & CMDSTS_Tx_EC)
1459 ifp->if_collisions += 16;
1460 if (ifp->if_flags & IFF_DEBUG) {
1461 if (cmdsts & CMDSTS_Tx_ED)
1462 printf("%s: excessive deferral\n",
1463 sc->sc_dev.dv_xname);
1464 if (cmdsts & CMDSTS_Tx_EC)
1465 printf("%s: excessive collisions\n",
1466 sc->sc_dev.dv_xname);
1467 }
1468 } else {
1469 /* Packet was transmitted successfully. */
1470 ifp->if_opackets++;
1471 ifp->if_collisions += CMDSTS_COLLISIONS(cmdsts);
1472 }
1473 }
1474
1475 /*
1476 * If there are no more pending transmissions, cancel the watchdog
1477 * timer.
1478 */
1479 if (txs == NULL)
1480 ifp->if_timer = 0;
1481 }
1482
1483 #if defined(DP83820)
1484 /*
1485 * sip_rxintr:
1486 *
1487 * Helper; handle receive interrupts.
1488 */
1489 void
1490 SIP_DECL(rxintr)(struct sip_softc *sc)
1491 {
1492 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1493 struct sip_rxsoft *rxs;
1494 struct mbuf *m, *tailm;
1495 u_int32_t cmdsts, extsts;
1496 int i, len;
1497
1498 for (i = sc->sc_rxptr;; i = SIP_NEXTRX(i)) {
1499 rxs = &sc->sc_rxsoft[i];
1500
1501 SIP_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1502
1503 cmdsts = le32toh(sc->sc_rxdescs[i].sipd_cmdsts);
1504 extsts = le32toh(sc->sc_rxdescs[i].sipd_extsts);
1505
1506 /*
1507 * NOTE: OWN is set if owned by _consumer_. We're the
1508 * consumer of the receive ring, so if the bit is clear,
1509 * we have processed all of the packets.
1510 */
1511 if ((cmdsts & CMDSTS_OWN) == 0) {
1512 /*
1513 * We have processed all of the receive buffers.
1514 */
1515 break;
1516 }
1517
1518 if (__predict_false(sc->sc_rxdiscard)) {
1519 SIP_INIT_RXDESC(sc, i);
1520 if ((cmdsts & CMDSTS_MORE) == 0) {
1521 /* Reset our state. */
1522 sc->sc_rxdiscard = 0;
1523 }
1524 continue;
1525 }
1526
1527 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1528 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1529
1530 m = rxs->rxs_mbuf;
1531
1532 /*
1533 * Add a new receive buffer to the ring.
1534 */
1535 if (SIP_DECL(add_rxbuf)(sc, i) != 0) {
1536 /*
1537 * Failed, throw away what we've done so
1538 * far, and discard the rest of the packet.
1539 */
1540 ifp->if_ierrors++;
1541 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1542 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1543 SIP_INIT_RXDESC(sc, i);
1544 if (cmdsts & CMDSTS_MORE)
1545 sc->sc_rxdiscard = 1;
1546 if (sc->sc_rxhead != NULL)
1547 m_freem(sc->sc_rxhead);
1548 SIP_RXCHAIN_RESET(sc);
1549 continue;
1550 }
1551
1552 SIP_RXCHAIN_LINK(sc, m);
1553
1554 /*
1555 * If this is not the end of the packet, keep
1556 * looking.
1557 */
1558 if (cmdsts & CMDSTS_MORE) {
1559 sc->sc_rxlen += m->m_len;
1560 continue;
1561 }
1562
1563 /*
1564 * Okay, we have the entire packet now...
1565 */
1566 *sc->sc_rxtailp = NULL;
1567 m = sc->sc_rxhead;
1568 tailm = sc->sc_rxtail;
1569
1570 SIP_RXCHAIN_RESET(sc);
1571
1572 /*
1573 * If an error occurred, update stats and drop the packet.
1574 */
1575 if (cmdsts & (CMDSTS_Rx_RXA|CMDSTS_Rx_RUNT|
1576 CMDSTS_Rx_ISE|CMDSTS_Rx_CRCE|CMDSTS_Rx_FAE)) {
1577 ifp->if_ierrors++;
1578 if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
1579 (cmdsts & CMDSTS_Rx_RXO) == 0) {
1580 /* Receive overrun handled elsewhere. */
1581 printf("%s: receive descriptor error\n",
1582 sc->sc_dev.dv_xname);
1583 }
1584 #define PRINTERR(bit, str) \
1585 if (cmdsts & (bit)) \
1586 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1587 PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
1588 PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
1589 PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
1590 PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
1591 #undef PRINTERR
1592 m_freem(m);
1593 continue;
1594 }
1595
1596 /*
1597 * No errors. Reset receive state.
1598 *
1599 * Note, the DP83820 includes the CRC with
1600 * every packet.
1601 */
1602 len = CMDSTS_SIZE(cmdsts);
1603 tailm->m_len = len - sc->sc_rxlen;
1604
1605 /*
1606 * If the packet is small enough to fit in a
1607 * single header mbuf, allocate one and copy
1608 * the data into it. This greatly reduces
1609 * memory consumption when we receive lots
1610 * of small packets.
1611 */
1612 if (SIP_DECL(copy_small) != 0 && len <= (MHLEN - 2)) {
1613 struct mbuf *nm;
1614 MGETHDR(nm, M_DONTWAIT, MT_DATA);
1615 if (nm == NULL) {
1616 ifp->if_ierrors++;
1617 m_freem(m);
1618 continue;
1619 }
1620 nm->m_data += 2;
1621 nm->m_pkthdr.len = nm->m_len = len;
1622 m_copydata(m, 0, len, mtod(nm, caddr_t));
1623 m_freem(m);
1624 m = nm;
1625 }
1626 #ifndef __NO_STRICT_ALIGNMENT
1627 else {
1628 /*
1629 * The DP83820's receive buffers must be 4-byte
1630 * aligned. But this means that the data after
1631 * the Ethernet header is misaligned. To compensate,
1632 * we have artificially shortened the buffer size
1633 * in the descriptor, and we do an overlapping copy
1634 * of the data two bytes further in (in the first
1635 * buffer of the chain only).
1636 */
1637 memmove(mtod(m, caddr_t) + 2, mtod(m, caddr_t),
1638 m->m_len);
1639 m->m_data += 2;
1640 }
1641 #endif /* ! __NO_STRICT_ALIGNMENT */
1642
1643 ifp->if_ipackets++;
1644 m->m_flags |= M_HASFCS;
1645 m->m_pkthdr.rcvif = ifp;
1646 m->m_pkthdr.len = len;
1647
1648 #if NBPFILTER > 0
1649 /*
1650 * Pass this up to any BPF listeners, but only
1651 * pass if up the stack if it's for us.
1652 */
1653 if (ifp->if_bpf)
1654 bpf_mtap(ifp->if_bpf, m);
1655 #endif /* NBPFILTER > 0 */
1656
1657 /*
1658 * If VLANs are enabled, VLAN packets have been unwrapped
1659 * for us. Associate the tag with the packet.
1660 */
1661 if (sc->sc_ethercom.ec_nvlans != 0 &&
1662 (extsts & EXTSTS_VPKT) != 0) {
1663 struct mbuf *vtag;
1664
1665 vtag = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
1666 if (vtag == NULL) {
1667 printf("%s: unable to allocate VLAN tag\n",
1668 sc->sc_dev.dv_xname);
1669 m_freem(m);
1670 continue;
1671 }
1672
1673 *mtod(vtag, int *) = ntohs(extsts & EXTSTS_VTCI);
1674 vtag->m_len = sizeof(int);
1675 }
1676
1677 /*
1678 * Set the incoming checksum information for the
1679 * packet.
1680 */
1681 if ((extsts & EXTSTS_IPPKT) != 0) {
1682 SIP_EVCNT_INCR(&sc->sc_ev_rxipsum);
1683 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1684 if (extsts & EXTSTS_Rx_IPERR)
1685 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1686 if (extsts & EXTSTS_TCPPKT) {
1687 SIP_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
1688 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1689 if (extsts & EXTSTS_Rx_TCPERR)
1690 m->m_pkthdr.csum_flags |=
1691 M_CSUM_TCP_UDP_BAD;
1692 } else if (extsts & EXTSTS_UDPPKT) {
1693 SIP_EVCNT_INCR(&sc->sc_ev_rxudpsum);
1694 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1695 if (extsts & EXTSTS_Rx_UDPERR)
1696 m->m_pkthdr.csum_flags |=
1697 M_CSUM_TCP_UDP_BAD;
1698 }
1699 }
1700
1701 /* Pass it on. */
1702 (*ifp->if_input)(ifp, m);
1703 }
1704
1705 /* Update the receive pointer. */
1706 sc->sc_rxptr = i;
1707 }
1708 #else /* ! DP83820 */
1709 /*
1710 * sip_rxintr:
1711 *
1712 * Helper; handle receive interrupts.
1713 */
1714 void
1715 SIP_DECL(rxintr)(struct sip_softc *sc)
1716 {
1717 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1718 struct sip_rxsoft *rxs;
1719 struct mbuf *m;
1720 u_int32_t cmdsts;
1721 int i, len;
1722
1723 for (i = sc->sc_rxptr;; i = SIP_NEXTRX(i)) {
1724 rxs = &sc->sc_rxsoft[i];
1725
1726 SIP_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1727
1728 cmdsts = le32toh(sc->sc_rxdescs[i].sipd_cmdsts);
1729
1730 /*
1731 * NOTE: OWN is set if owned by _consumer_. We're the
1732 * consumer of the receive ring, so if the bit is clear,
1733 * we have processed all of the packets.
1734 */
1735 if ((cmdsts & CMDSTS_OWN) == 0) {
1736 /*
1737 * We have processed all of the receive buffers.
1738 */
1739 break;
1740 }
1741
1742 /*
1743 * If any collisions were seen on the wire, count one.
1744 */
1745 if (cmdsts & CMDSTS_Rx_COL)
1746 ifp->if_collisions++;
1747
1748 /*
1749 * If an error occurred, update stats, clear the status
1750 * word, and leave the packet buffer in place. It will
1751 * simply be reused the next time the ring comes around.
1752 */
1753 if (cmdsts & (CMDSTS_Rx_RXA|CMDSTS_Rx_RUNT|
1754 CMDSTS_Rx_ISE|CMDSTS_Rx_CRCE|CMDSTS_Rx_FAE)) {
1755 ifp->if_ierrors++;
1756 if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
1757 (cmdsts & CMDSTS_Rx_RXO) == 0) {
1758 /* Receive overrun handled elsewhere. */
1759 printf("%s: receive descriptor error\n",
1760 sc->sc_dev.dv_xname);
1761 }
1762 #define PRINTERR(bit, str) \
1763 if (cmdsts & (bit)) \
1764 printf("%s: %s\n", sc->sc_dev.dv_xname, str)
1765 PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
1766 PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
1767 PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
1768 PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
1769 #undef PRINTERR
1770 SIP_INIT_RXDESC(sc, i);
1771 continue;
1772 }
1773
1774 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1775 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1776
1777 /*
1778 * No errors; receive the packet. Note, the SiS 900
1779 * includes the CRC with every packet.
1780 */
1781 len = CMDSTS_SIZE(cmdsts);
1782
1783 #ifdef __NO_STRICT_ALIGNMENT
1784 /*
1785 * If the packet is small enough to fit in a
1786 * single header mbuf, allocate one and copy
1787 * the data into it. This greatly reduces
1788 * memory consumption when we receive lots
1789 * of small packets.
1790 *
1791 * Otherwise, we add a new buffer to the receive
1792 * chain. If this fails, we drop the packet and
1793 * recycle the old buffer.
1794 */
1795 if (SIP_DECL(copy_small) != 0 && len <= MHLEN) {
1796 MGETHDR(m, M_DONTWAIT, MT_DATA);
1797 if (m == NULL)
1798 goto dropit;
1799 memcpy(mtod(m, caddr_t),
1800 mtod(rxs->rxs_mbuf, caddr_t), len);
1801 SIP_INIT_RXDESC(sc, i);
1802 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1803 rxs->rxs_dmamap->dm_mapsize,
1804 BUS_DMASYNC_PREREAD);
1805 } else {
1806 m = rxs->rxs_mbuf;
1807 if (SIP_DECL(add_rxbuf)(sc, i) != 0) {
1808 dropit:
1809 ifp->if_ierrors++;
1810 SIP_INIT_RXDESC(sc, i);
1811 bus_dmamap_sync(sc->sc_dmat,
1812 rxs->rxs_dmamap, 0,
1813 rxs->rxs_dmamap->dm_mapsize,
1814 BUS_DMASYNC_PREREAD);
1815 continue;
1816 }
1817 }
1818 #else
1819 /*
1820 * The SiS 900's receive buffers must be 4-byte aligned.
1821 * But this means that the data after the Ethernet header
1822 * is misaligned. We must allocate a new buffer and
1823 * copy the data, shifted forward 2 bytes.
1824 */
1825 MGETHDR(m, M_DONTWAIT, MT_DATA);
1826 if (m == NULL) {
1827 dropit:
1828 ifp->if_ierrors++;
1829 SIP_INIT_RXDESC(sc, i);
1830 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1831 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1832 continue;
1833 }
1834 if (len > (MHLEN - 2)) {
1835 MCLGET(m, M_DONTWAIT);
1836 if ((m->m_flags & M_EXT) == 0) {
1837 m_freem(m);
1838 goto dropit;
1839 }
1840 }
1841 m->m_data += 2;
1842
1843 /*
1844 * Note that we use clusters for incoming frames, so the
1845 * buffer is virtually contiguous.
1846 */
1847 memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
1848
1849 /* Allow the receive descriptor to continue using its mbuf. */
1850 SIP_INIT_RXDESC(sc, i);
1851 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
1852 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1853 #endif /* __NO_STRICT_ALIGNMENT */
1854
1855 ifp->if_ipackets++;
1856 m->m_flags |= M_HASFCS;
1857 m->m_pkthdr.rcvif = ifp;
1858 m->m_pkthdr.len = m->m_len = len;
1859
1860 #if NBPFILTER > 0
1861 /*
1862 * Pass this up to any BPF listeners, but only
1863 * pass if up the stack if it's for us.
1864 */
1865 if (ifp->if_bpf)
1866 bpf_mtap(ifp->if_bpf, m);
1867 #endif /* NBPFILTER > 0 */
1868
1869 /* Pass it on. */
1870 (*ifp->if_input)(ifp, m);
1871 }
1872
1873 /* Update the receive pointer. */
1874 sc->sc_rxptr = i;
1875 }
1876 #endif /* DP83820 */
1877
1878 /*
1879 * sip_tick:
1880 *
1881 * One second timer, used to tick the MII.
1882 */
1883 void
1884 SIP_DECL(tick)(void *arg)
1885 {
1886 struct sip_softc *sc = arg;
1887 int s;
1888
1889 s = splnet();
1890 mii_tick(&sc->sc_mii);
1891 splx(s);
1892
1893 callout_reset(&sc->sc_tick_ch, hz, SIP_DECL(tick), sc);
1894 }
1895
1896 /*
1897 * sip_reset:
1898 *
1899 * Perform a soft reset on the SiS 900.
1900 */
1901 void
1902 SIP_DECL(reset)(struct sip_softc *sc)
1903 {
1904 bus_space_tag_t st = sc->sc_st;
1905 bus_space_handle_t sh = sc->sc_sh;
1906 int i;
1907
1908 bus_space_write_4(st, sh, SIP_CR, CR_RST);
1909
1910 for (i = 0; i < SIP_TIMEOUT; i++) {
1911 if ((bus_space_read_4(st, sh, SIP_CR) & CR_RST) == 0)
1912 break;
1913 delay(2);
1914 }
1915
1916 if (i == SIP_TIMEOUT)
1917 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1918
1919 delay(1000);
1920
1921 #ifdef DP83820
1922 /*
1923 * Set the general purpose I/O bits. Do it here in case we
1924 * need to have GPIO set up to talk to the media interface.
1925 */
1926 bus_space_write_4(st, sh, SIP_GPIOR, sc->sc_gpior);
1927 delay(1000);
1928 #endif /* DP83820 */
1929 }
1930
1931 /*
1932 * sip_init: [ ifnet interface function ]
1933 *
1934 * Initialize the interface. Must be called at splnet().
1935 */
1936 int
1937 SIP_DECL(init)(struct ifnet *ifp)
1938 {
1939 struct sip_softc *sc = ifp->if_softc;
1940 bus_space_tag_t st = sc->sc_st;
1941 bus_space_handle_t sh = sc->sc_sh;
1942 struct sip_txsoft *txs;
1943 struct sip_rxsoft *rxs;
1944 struct sip_desc *sipd;
1945 u_int32_t reg;
1946 int i, error = 0;
1947
1948 /*
1949 * Cancel any pending I/O.
1950 */
1951 SIP_DECL(stop)(ifp, 0);
1952
1953 /*
1954 * Reset the chip to a known state.
1955 */
1956 SIP_DECL(reset)(sc);
1957
1958 #if !defined(DP83820)
1959 if (sc->sc_model->sip_vendor == PCI_VENDOR_NS &&
1960 sc->sc_model->sip_product == PCI_PRODUCT_NS_DP83815) {
1961 /*
1962 * DP83815 manual, page 78:
1963 * 4.4 Recommended Registers Configuration
1964 * For optimum performance of the DP83815, version noted
1965 * as DP83815CVNG (SRR = 203h), the listed register
1966 * modifications must be followed in sequence...
1967 *
1968 * It's not clear if this should be 302h or 203h because that
1969 * chip name is listed as SRR 302h in the description of the
1970 * SRR register. However, my revision 302h DP83815 on the
1971 * Netgear FA311 purchased in 02/2001 needs these settings
1972 * to avoid tons of errors in AcceptPerfectMatch (non-
1973 * IFF_PROMISC) mode. I do not know if other revisions need
1974 * this set or not. [briggs -- 09 March 2001]
1975 *
1976 * Note that only the low-order 12 bits of 0xe4 are documented
1977 * and that this sets reserved bits in that register.
1978 */
1979 reg = bus_space_read_4(st, sh, SIP_NS_SRR);
1980 if (reg == 0x302) {
1981 bus_space_write_4(st, sh, 0x00cc, 0x0001);
1982 bus_space_write_4(st, sh, 0x00e4, 0x189C);
1983 bus_space_write_4(st, sh, 0x00fc, 0x0000);
1984 bus_space_write_4(st, sh, 0x00f4, 0x5040);
1985 bus_space_write_4(st, sh, 0x00f8, 0x008c);
1986 }
1987 }
1988 #endif /* ! DP83820 */
1989
1990 /*
1991 * Initialize the transmit descriptor ring.
1992 */
1993 for (i = 0; i < SIP_NTXDESC; i++) {
1994 sipd = &sc->sc_txdescs[i];
1995 memset(sipd, 0, sizeof(struct sip_desc));
1996 sipd->sipd_link = htole32(SIP_CDTXADDR(sc, SIP_NEXTTX(i)));
1997 }
1998 SIP_CDTXSYNC(sc, 0, SIP_NTXDESC,
1999 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2000 sc->sc_txfree = SIP_NTXDESC;
2001 sc->sc_txnext = 0;
2002
2003 /*
2004 * Initialize the transmit job descriptors.
2005 */
2006 SIMPLEQ_INIT(&sc->sc_txfreeq);
2007 SIMPLEQ_INIT(&sc->sc_txdirtyq);
2008 for (i = 0; i < SIP_TXQUEUELEN; i++) {
2009 txs = &sc->sc_txsoft[i];
2010 txs->txs_mbuf = NULL;
2011 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
2012 }
2013
2014 /*
2015 * Initialize the receive descriptor and receive job
2016 * descriptor rings.
2017 */
2018 for (i = 0; i < SIP_NRXDESC; i++) {
2019 rxs = &sc->sc_rxsoft[i];
2020 if (rxs->rxs_mbuf == NULL) {
2021 if ((error = SIP_DECL(add_rxbuf)(sc, i)) != 0) {
2022 printf("%s: unable to allocate or map rx "
2023 "buffer %d, error = %d\n",
2024 sc->sc_dev.dv_xname, i, error);
2025 /*
2026 * XXX Should attempt to run with fewer receive
2027 * XXX buffers instead of just failing.
2028 */
2029 SIP_DECL(rxdrain)(sc);
2030 goto out;
2031 }
2032 }
2033 }
2034 sc->sc_rxptr = 0;
2035 #ifdef DP83820
2036 sc->sc_rxdiscard = 0;
2037 SIP_RXCHAIN_RESET(sc);
2038 #endif /* DP83820 */
2039
2040 /*
2041 * Set the configuration register; it's already initialized
2042 * in sip_attach().
2043 */
2044 bus_space_write_4(st, sh, SIP_CFG, sc->sc_cfg);
2045
2046 /*
2047 * Initialize the transmit fill and drain thresholds if
2048 * we have never done so.
2049 */
2050 if (sc->sc_tx_fill_thresh == 0) {
2051 /*
2052 * XXX This value should be tuned. This is the
2053 * minimum (32 bytes), and we may be able to
2054 * improve performance by increasing it.
2055 */
2056 sc->sc_tx_fill_thresh = 1;
2057 }
2058 if (sc->sc_tx_drain_thresh == 0) {
2059 /*
2060 * Start at a drain threshold of 512 bytes. We will
2061 * increase it if a DMA underrun occurs.
2062 *
2063 * XXX The minimum value of this variable should be
2064 * tuned. We may be able to improve performance
2065 * by starting with a lower value. That, however,
2066 * may trash the first few outgoing packets if the
2067 * PCI bus is saturated.
2068 */
2069 sc->sc_tx_drain_thresh = 512 / 32;
2070 }
2071
2072 /*
2073 * Initialize the prototype TXCFG register.
2074 */
2075 sc->sc_txcfg = TXCFG_ATP | TXCFG_MXDMA_512 |
2076 (sc->sc_tx_fill_thresh << TXCFG_FLTH_SHIFT) |
2077 sc->sc_tx_drain_thresh;
2078 bus_space_write_4(st, sh, SIP_TXCFG, sc->sc_txcfg);
2079
2080 /*
2081 * Initialize the receive drain threshold if we have never
2082 * done so.
2083 */
2084 if (sc->sc_rx_drain_thresh == 0) {
2085 /*
2086 * XXX This value should be tuned. This is set to the
2087 * maximum of 248 bytes, and we may be able to improve
2088 * performance by decreasing it (although we should never
2089 * set this value lower than 2; 14 bytes are required to
2090 * filter the packet).
2091 */
2092 sc->sc_rx_drain_thresh = RXCFG_DRTH >> RXCFG_DRTH_SHIFT;
2093 }
2094
2095 /*
2096 * Initialize the prototype RXCFG register.
2097 */
2098 sc->sc_rxcfg = RXCFG_MXDMA_512 |
2099 (sc->sc_rx_drain_thresh << RXCFG_DRTH_SHIFT);
2100 bus_space_write_4(st, sh, SIP_RXCFG, sc->sc_rxcfg);
2101
2102 /* Set up the receive filter. */
2103 (*sc->sc_model->sip_variant->sipv_set_filter)(sc);
2104
2105 #ifdef DP83820
2106 /*
2107 * Initialize the VLAN/IP receive control register.
2108 * We enable checksum computation on all incoming
2109 * packets, and do not reject packets w/ bad checksums.
2110 */
2111 reg = 0;
2112 if (ifp->if_capenable &
2113 (IFCAP_CSUM_IPv4|IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
2114 reg |= VRCR_IPEN;
2115 if (sc->sc_ethercom.ec_nvlans != 0)
2116 reg |= VRCR_VTDEN|VRCR_VTREN;
2117 bus_space_write_4(st, sh, SIP_VRCR, reg);
2118
2119 /*
2120 * Initialize the VLAN/IP transmit control register.
2121 * We enable outgoing checksum computation on a
2122 * per-packet basis.
2123 */
2124 reg = 0;
2125 if (ifp->if_capenable &
2126 (IFCAP_CSUM_IPv4|IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
2127 reg |= VTCR_PPCHK;
2128 if (sc->sc_ethercom.ec_nvlans != 0)
2129 reg |= VTCR_VPPTI;
2130 bus_space_write_4(st, sh, SIP_VTCR, reg);
2131
2132 /*
2133 * If we're using VLANs, initialize the VLAN data register.
2134 * To understand why we bswap the VLAN Ethertype, see section
2135 * 4.2.36 of the DP83820 manual.
2136 */
2137 if (sc->sc_ethercom.ec_nvlans != 0)
2138 bus_space_write_4(st, sh, SIP_VDR, bswap16(ETHERTYPE_VLAN));
2139 #endif /* DP83820 */
2140
2141 /*
2142 * Give the transmit and receive rings to the chip.
2143 */
2144 bus_space_write_4(st, sh, SIP_TXDP, SIP_CDTXADDR(sc, sc->sc_txnext));
2145 bus_space_write_4(st, sh, SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
2146
2147 /*
2148 * Initialize the interrupt mask.
2149 */
2150 sc->sc_imr = ISR_DPERR|ISR_SSERR|ISR_RMABT|ISR_RTABT|ISR_RXSOVR|
2151 ISR_TXURN|ISR_TXDESC|ISR_RXORN|ISR_RXIDLE|ISR_RXDESC;
2152 bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
2153
2154 /*
2155 * Set the current media. Do this after initializing the prototype
2156 * IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
2157 * control.
2158 */
2159 mii_mediachg(&sc->sc_mii);
2160
2161 /*
2162 * Enable interrupts.
2163 */
2164 bus_space_write_4(st, sh, SIP_IER, IER_IE);
2165
2166 /*
2167 * Start the transmit and receive processes.
2168 */
2169 bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
2170
2171 /*
2172 * Start the one second MII clock.
2173 */
2174 callout_reset(&sc->sc_tick_ch, hz, SIP_DECL(tick), sc);
2175
2176 /*
2177 * ...all done!
2178 */
2179 ifp->if_flags |= IFF_RUNNING;
2180 ifp->if_flags &= ~IFF_OACTIVE;
2181
2182 out:
2183 if (error)
2184 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
2185 return (error);
2186 }
2187
2188 /*
2189 * sip_drain:
2190 *
2191 * Drain the receive queue.
2192 */
2193 void
2194 SIP_DECL(rxdrain)(struct sip_softc *sc)
2195 {
2196 struct sip_rxsoft *rxs;
2197 int i;
2198
2199 for (i = 0; i < SIP_NRXDESC; i++) {
2200 rxs = &sc->sc_rxsoft[i];
2201 if (rxs->rxs_mbuf != NULL) {
2202 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2203 m_freem(rxs->rxs_mbuf);
2204 rxs->rxs_mbuf = NULL;
2205 }
2206 }
2207 }
2208
2209 /*
2210 * sip_stop: [ ifnet interface function ]
2211 *
2212 * Stop transmission on the interface.
2213 */
2214 void
2215 SIP_DECL(stop)(struct ifnet *ifp, int disable)
2216 {
2217 struct sip_softc *sc = ifp->if_softc;
2218 bus_space_tag_t st = sc->sc_st;
2219 bus_space_handle_t sh = sc->sc_sh;
2220 struct sip_txsoft *txs;
2221 u_int32_t cmdsts = 0; /* DEBUG */
2222
2223 /*
2224 * Stop the one second clock.
2225 */
2226 callout_stop(&sc->sc_tick_ch);
2227
2228 /* Down the MII. */
2229 mii_down(&sc->sc_mii);
2230
2231 /*
2232 * Disable interrupts.
2233 */
2234 bus_space_write_4(st, sh, SIP_IER, 0);
2235
2236 /*
2237 * Stop receiver and transmitter.
2238 */
2239 bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
2240
2241 /*
2242 * Release any queued transmit buffers.
2243 */
2244 while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
2245 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
2246 SIMPLEQ_NEXT(txs, txs_q) == NULL &&
2247 (le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts) &
2248 CMDSTS_INTR) == 0)
2249 printf("%s: sip_stop: last descriptor does not "
2250 "have INTR bit set\n", sc->sc_dev.dv_xname);
2251 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
2252 #ifdef DIAGNOSTIC
2253 if (txs->txs_mbuf == NULL) {
2254 printf("%s: dirty txsoft with no mbuf chain\n",
2255 sc->sc_dev.dv_xname);
2256 panic("sip_stop");
2257 }
2258 #endif
2259 cmdsts |= /* DEBUG */
2260 le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
2261 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
2262 m_freem(txs->txs_mbuf);
2263 txs->txs_mbuf = NULL;
2264 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
2265 }
2266
2267 if (disable)
2268 SIP_DECL(rxdrain)(sc);
2269
2270 /*
2271 * Mark the interface down and cancel the watchdog timer.
2272 */
2273 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2274 ifp->if_timer = 0;
2275
2276 if ((ifp->if_flags & IFF_DEBUG) != 0 &&
2277 (cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != SIP_NTXDESC)
2278 printf("%s: sip_stop: no INTR bits set in dirty tx "
2279 "descriptors\n", sc->sc_dev.dv_xname);
2280 }
2281
2282 /*
2283 * sip_read_eeprom:
2284 *
2285 * Read data from the serial EEPROM.
2286 */
2287 void
2288 SIP_DECL(read_eeprom)(struct sip_softc *sc, int word, int wordcnt,
2289 u_int16_t *data)
2290 {
2291 bus_space_tag_t st = sc->sc_st;
2292 bus_space_handle_t sh = sc->sc_sh;
2293 u_int16_t reg;
2294 int i, x;
2295
2296 for (i = 0; i < wordcnt; i++) {
2297 /* Send CHIP SELECT. */
2298 reg = EROMAR_EECS;
2299 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2300
2301 /* Shift in the READ opcode. */
2302 for (x = 3; x > 0; x--) {
2303 if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
2304 reg |= EROMAR_EEDI;
2305 else
2306 reg &= ~EROMAR_EEDI;
2307 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2308 bus_space_write_4(st, sh, SIP_EROMAR,
2309 reg | EROMAR_EESK);
2310 delay(4);
2311 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2312 delay(4);
2313 }
2314
2315 /* Shift in address. */
2316 for (x = 6; x > 0; x--) {
2317 if ((word + i) & (1 << (x - 1)))
2318 reg |= EROMAR_EEDI;
2319 else
2320 reg &= ~EROMAR_EEDI;
2321 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2322 bus_space_write_4(st, sh, SIP_EROMAR,
2323 reg | EROMAR_EESK);
2324 delay(4);
2325 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2326 delay(4);
2327 }
2328
2329 /* Shift out data. */
2330 reg = EROMAR_EECS;
2331 data[i] = 0;
2332 for (x = 16; x > 0; x--) {
2333 bus_space_write_4(st, sh, SIP_EROMAR,
2334 reg | EROMAR_EESK);
2335 delay(4);
2336 if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
2337 data[i] |= (1 << (x - 1));
2338 bus_space_write_4(st, sh, SIP_EROMAR, reg);
2339 delay(4);
2340 }
2341
2342 /* Clear CHIP SELECT. */
2343 bus_space_write_4(st, sh, SIP_EROMAR, 0);
2344 delay(4);
2345 }
2346 }
2347
2348 /*
2349 * sip_add_rxbuf:
2350 *
2351 * Add a receive buffer to the indicated descriptor.
2352 */
2353 int
2354 SIP_DECL(add_rxbuf)(struct sip_softc *sc, int idx)
2355 {
2356 struct sip_rxsoft *rxs = &sc->sc_rxsoft[idx];
2357 struct mbuf *m;
2358 int error;
2359
2360 MGETHDR(m, M_DONTWAIT, MT_DATA);
2361 if (m == NULL)
2362 return (ENOBUFS);
2363
2364 MCLGET(m, M_DONTWAIT);
2365 if ((m->m_flags & M_EXT) == 0) {
2366 m_freem(m);
2367 return (ENOBUFS);
2368 }
2369
2370 #if defined(DP83820)
2371 m->m_len = SIP_RXBUF_LEN;
2372 #endif /* DP83820 */
2373
2374 if (rxs->rxs_mbuf != NULL)
2375 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
2376
2377 rxs->rxs_mbuf = m;
2378
2379 error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
2380 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
2381 if (error) {
2382 printf("%s: can't load rx DMA map %d, error = %d\n",
2383 sc->sc_dev.dv_xname, idx, error);
2384 panic("sip_add_rxbuf"); /* XXX */
2385 }
2386
2387 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
2388 rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2389
2390 SIP_INIT_RXDESC(sc, idx);
2391
2392 return (0);
2393 }
2394
2395 #if !defined(DP83820)
2396 /*
2397 * sip_sis900_set_filter:
2398 *
2399 * Set up the receive filter.
2400 */
2401 void
2402 SIP_DECL(sis900_set_filter)(struct sip_softc *sc)
2403 {
2404 bus_space_tag_t st = sc->sc_st;
2405 bus_space_handle_t sh = sc->sc_sh;
2406 struct ethercom *ec = &sc->sc_ethercom;
2407 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2408 struct ether_multi *enm;
2409 u_int8_t *cp;
2410 struct ether_multistep step;
2411 u_int32_t crc, mchash[8];
2412
2413 /*
2414 * Initialize the prototype RFCR.
2415 */
2416 sc->sc_rfcr = RFCR_RFEN;
2417 if (ifp->if_flags & IFF_BROADCAST)
2418 sc->sc_rfcr |= RFCR_AAB;
2419 if (ifp->if_flags & IFF_PROMISC) {
2420 sc->sc_rfcr |= RFCR_AAP;
2421 goto allmulti;
2422 }
2423
2424 /*
2425 * Set up the multicast address filter by passing all multicast
2426 * addresses through a CRC generator, and then using the high-order
2427 * 6 bits as an index into the 128 bit multicast hash table (only
2428 * the lower 16 bits of each 32 bit multicast hash register are
2429 * valid). The high order bits select the register, while the
2430 * rest of the bits select the bit within the register.
2431 */
2432
2433 memset(mchash, 0, sizeof(mchash));
2434
2435 ETHER_FIRST_MULTI(step, ec, enm);
2436 while (enm != NULL) {
2437 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2438 /*
2439 * We must listen to a range of multicast addresses.
2440 * For now, just accept all multicasts, rather than
2441 * trying to set only those filter bits needed to match
2442 * the range. (At this time, the only use of address
2443 * ranges is for IP multicast routing, for which the
2444 * range is big enough to require all bits set.)
2445 */
2446 goto allmulti;
2447 }
2448
2449 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
2450
2451 /* Just want the 7 most significant bits. */
2452 crc >>= 25;
2453
2454 /* Set the corresponding bit in the hash table. */
2455 mchash[crc >> 4] |= 1 << (crc & 0xf);
2456
2457 ETHER_NEXT_MULTI(step, enm);
2458 }
2459
2460 ifp->if_flags &= ~IFF_ALLMULTI;
2461 goto setit;
2462
2463 allmulti:
2464 ifp->if_flags |= IFF_ALLMULTI;
2465 sc->sc_rfcr |= RFCR_AAM;
2466
2467 setit:
2468 #define FILTER_EMIT(addr, data) \
2469 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
2470 delay(1); \
2471 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
2472 delay(1)
2473
2474 /*
2475 * Disable receive filter, and program the node address.
2476 */
2477 cp = LLADDR(ifp->if_sadl);
2478 FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
2479 FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
2480 FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
2481
2482 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2483 /*
2484 * Program the multicast hash table.
2485 */
2486 FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
2487 FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
2488 FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
2489 FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
2490 FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
2491 FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
2492 FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
2493 FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
2494 }
2495 #undef FILTER_EMIT
2496
2497 /*
2498 * Re-enable the receiver filter.
2499 */
2500 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
2501 }
2502 #endif /* ! DP83820 */
2503
2504 /*
2505 * sip_dp83815_set_filter:
2506 *
2507 * Set up the receive filter.
2508 */
2509 void
2510 SIP_DECL(dp83815_set_filter)(struct sip_softc *sc)
2511 {
2512 bus_space_tag_t st = sc->sc_st;
2513 bus_space_handle_t sh = sc->sc_sh;
2514 struct ethercom *ec = &sc->sc_ethercom;
2515 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2516 struct ether_multi *enm;
2517 u_int8_t *cp;
2518 struct ether_multistep step;
2519 u_int32_t crc, hash, slot, bit;
2520 #ifdef DP83820
2521 #define MCHASH_NWORDS 128
2522 #else
2523 #define MCHASH_NWORDS 32
2524 #endif /* DP83820 */
2525 u_int16_t mchash[MCHASH_NWORDS];
2526 int i;
2527
2528 /*
2529 * Initialize the prototype RFCR.
2530 * Enable the receive filter, and accept on
2531 * Perfect (destination address) Match
2532 * If IFF_BROADCAST, also accept all broadcast packets.
2533 * If IFF_PROMISC, accept all unicast packets (and later, set
2534 * IFF_ALLMULTI and accept all multicast, too).
2535 */
2536 sc->sc_rfcr = RFCR_RFEN | RFCR_APM;
2537 if (ifp->if_flags & IFF_BROADCAST)
2538 sc->sc_rfcr |= RFCR_AAB;
2539 if (ifp->if_flags & IFF_PROMISC) {
2540 sc->sc_rfcr |= RFCR_AAP;
2541 goto allmulti;
2542 }
2543
2544 #ifdef DP83820
2545 /*
2546 * Set up the DP83820 multicast address filter by passing all multicast
2547 * addresses through a CRC generator, and then using the high-order
2548 * 11 bits as an index into the 2048 bit multicast hash table. The
2549 * high-order 7 bits select the slot, while the low-order 4 bits
2550 * select the bit within the slot. Note that only the low 16-bits
2551 * of each filter word are used, and there are 128 filter words.
2552 */
2553 #else
2554 /*
2555 * Set up the DP83815 multicast address filter by passing all multicast
2556 * addresses through a CRC generator, and then using the high-order
2557 * 9 bits as an index into the 512 bit multicast hash table. The
2558 * high-order 5 bits select the slot, while the low-order 4 bits
2559 * select the bit within the slot. Note that only the low 16-bits
2560 * of each filter word are used, and there are 32 filter words.
2561 */
2562 #endif /* DP83820 */
2563
2564 memset(mchash, 0, sizeof(mchash));
2565
2566 ifp->if_flags &= ~IFF_ALLMULTI;
2567 ETHER_FIRST_MULTI(step, ec, enm);
2568 if (enm == NULL)
2569 goto setit;
2570 while (enm != NULL) {
2571 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2572 /*
2573 * We must listen to a range of multicast addresses.
2574 * For now, just accept all multicasts, rather than
2575 * trying to set only those filter bits needed to match
2576 * the range. (At this time, the only use of address
2577 * ranges is for IP multicast routing, for which the
2578 * range is big enough to require all bits set.)
2579 */
2580 goto allmulti;
2581 }
2582
2583 #ifdef DP83820
2584 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
2585
2586 /* Just want the 11 most significant bits. */
2587 hash = crc >> 21;
2588 #else
2589 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
2590
2591 /* Just want the 9 most significant bits. */
2592 hash = crc >> 23;
2593 #endif /* DP83820 */
2594 slot = hash >> 4;
2595 bit = hash & 0xf;
2596
2597 /* Set the corresponding bit in the hash table. */
2598 mchash[slot] |= 1 << bit;
2599
2600 ETHER_NEXT_MULTI(step, enm);
2601 }
2602 sc->sc_rfcr |= RFCR_MHEN;
2603 goto setit;
2604
2605 allmulti:
2606 ifp->if_flags |= IFF_ALLMULTI;
2607 sc->sc_rfcr |= RFCR_AAM;
2608
2609 setit:
2610 #define FILTER_EMIT(addr, data) \
2611 bus_space_write_4(st, sh, SIP_RFCR, (addr)); \
2612 delay(1); \
2613 bus_space_write_4(st, sh, SIP_RFDR, (data)); \
2614 delay(1)
2615
2616 /*
2617 * Disable receive filter, and program the node address.
2618 */
2619 cp = LLADDR(ifp->if_sadl);
2620 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH0, (cp[1] << 8) | cp[0]);
2621 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH2, (cp[3] << 8) | cp[2]);
2622 FILTER_EMIT(RFCR_NS_RFADDR_PMATCH4, (cp[5] << 8) | cp[4]);
2623
2624 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2625 /*
2626 * Program the multicast hash table.
2627 */
2628 for (i = 0; i < MCHASH_NWORDS; i++) {
2629 FILTER_EMIT(RFCR_NS_RFADDR_FILTMEM + (i * 2),
2630 mchash[i]);
2631 }
2632 }
2633 #undef FILTER_EMIT
2634 #undef MCHASH_NWORDS
2635
2636 /*
2637 * Re-enable the receiver filter.
2638 */
2639 bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
2640 }
2641
2642 #if defined(DP83820)
2643 /*
2644 * sip_dp83820_mii_readreg: [mii interface function]
2645 *
2646 * Read a PHY register on the MII of the DP83820.
2647 */
2648 int
2649 SIP_DECL(dp83820_mii_readreg)(struct device *self, int phy, int reg)
2650 {
2651
2652 return (mii_bitbang_readreg(self, &SIP_DECL(dp83820_mii_bitbang_ops),
2653 phy, reg));
2654 }
2655
2656 /*
2657 * sip_dp83820_mii_writereg: [mii interface function]
2658 *
2659 * Write a PHY register on the MII of the DP83820.
2660 */
2661 void
2662 SIP_DECL(dp83820_mii_writereg)(struct device *self, int phy, int reg, int val)
2663 {
2664
2665 mii_bitbang_writereg(self, &SIP_DECL(dp83820_mii_bitbang_ops),
2666 phy, reg, val);
2667 }
2668
2669 /*
2670 * sip_dp83815_mii_statchg: [mii interface function]
2671 *
2672 * Callback from MII layer when media changes.
2673 */
2674 void
2675 SIP_DECL(dp83820_mii_statchg)(struct device *self)
2676 {
2677 struct sip_softc *sc = (struct sip_softc *) self;
2678 u_int32_t cfg;
2679
2680 /*
2681 * Update TXCFG for full-duplex operation.
2682 */
2683 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2684 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2685 else
2686 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2687
2688 /*
2689 * Update RXCFG for full-duplex or loopback.
2690 */
2691 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2692 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2693 sc->sc_rxcfg |= RXCFG_ATX;
2694 else
2695 sc->sc_rxcfg &= ~RXCFG_ATX;
2696
2697 /*
2698 * Update CFG for MII/GMII.
2699 */
2700 if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
2701 cfg = sc->sc_cfg | CFG_MODE_1000;
2702 else
2703 cfg = sc->sc_cfg;
2704
2705 /*
2706 * XXX 802.3x flow control.
2707 */
2708
2709 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CFG, cfg);
2710 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2711 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2712 }
2713
2714 /*
2715 * sip_dp83820_mii_bitbang_read: [mii bit-bang interface function]
2716 *
2717 * Read the MII serial port for the MII bit-bang module.
2718 */
2719 u_int32_t
2720 SIP_DECL(dp83820_mii_bitbang_read)(struct device *self)
2721 {
2722 struct sip_softc *sc = (void *) self;
2723
2724 return (bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_EROMAR));
2725 }
2726
2727 /*
2728 * sip_dp83820_mii_bitbang_write: [mii big-bang interface function]
2729 *
2730 * Write the MII serial port for the MII bit-bang module.
2731 */
2732 void
2733 SIP_DECL(dp83820_mii_bitbang_write)(struct device *self, u_int32_t val)
2734 {
2735 struct sip_softc *sc = (void *) self;
2736
2737 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_EROMAR, val);
2738 }
2739 #else /* ! DP83820 */
2740 /*
2741 * sip_sis900_mii_readreg: [mii interface function]
2742 *
2743 * Read a PHY register on the MII.
2744 */
2745 int
2746 SIP_DECL(sis900_mii_readreg)(struct device *self, int phy, int reg)
2747 {
2748 struct sip_softc *sc = (struct sip_softc *) self;
2749 u_int32_t enphy;
2750
2751 /*
2752 * The SiS 900 has only an internal PHY on the MII. Only allow
2753 * MII address 0.
2754 */
2755 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2756 return (0);
2757
2758 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2759 (phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
2760 ENPHY_RWCMD | ENPHY_ACCESS);
2761 do {
2762 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2763 } while (enphy & ENPHY_ACCESS);
2764 return ((enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT);
2765 }
2766
2767 /*
2768 * sip_sis900_mii_writereg: [mii interface function]
2769 *
2770 * Write a PHY register on the MII.
2771 */
2772 void
2773 SIP_DECL(sis900_mii_writereg)(struct device *self, int phy, int reg, int val)
2774 {
2775 struct sip_softc *sc = (struct sip_softc *) self;
2776 u_int32_t enphy;
2777
2778 /*
2779 * The SiS 900 has only an internal PHY on the MII. Only allow
2780 * MII address 0.
2781 */
2782 if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
2783 return;
2784
2785 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
2786 (val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
2787 (reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
2788 do {
2789 enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
2790 } while (enphy & ENPHY_ACCESS);
2791 }
2792
2793 /*
2794 * sip_sis900_mii_statchg: [mii interface function]
2795 *
2796 * Callback from MII layer when media changes.
2797 */
2798 void
2799 SIP_DECL(sis900_mii_statchg)(struct device *self)
2800 {
2801 struct sip_softc *sc = (struct sip_softc *) self;
2802 u_int32_t flowctl;
2803
2804 /*
2805 * Update TXCFG for full-duplex operation.
2806 */
2807 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2808 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2809 else
2810 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2811
2812 /*
2813 * Update RXCFG for full-duplex or loopback.
2814 */
2815 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2816 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2817 sc->sc_rxcfg |= RXCFG_ATX;
2818 else
2819 sc->sc_rxcfg &= ~RXCFG_ATX;
2820
2821 /*
2822 * Update IMR for use of 802.3x flow control.
2823 */
2824 if ((sc->sc_mii.mii_media_active & IFM_FLOW) != 0) {
2825 sc->sc_imr |= (ISR_PAUSE_END|ISR_PAUSE_ST);
2826 flowctl = FLOWCTL_FLOWEN;
2827 } else {
2828 sc->sc_imr &= ~(ISR_PAUSE_END|ISR_PAUSE_ST);
2829 flowctl = 0;
2830 }
2831
2832 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2833 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2834 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
2835 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
2836 }
2837
2838 /*
2839 * sip_dp83815_mii_readreg: [mii interface function]
2840 *
2841 * Read a PHY register on the MII.
2842 */
2843 int
2844 SIP_DECL(dp83815_mii_readreg)(struct device *self, int phy, int reg)
2845 {
2846 struct sip_softc *sc = (struct sip_softc *) self;
2847 u_int32_t val;
2848
2849 /*
2850 * The DP83815 only has an internal PHY. Only allow
2851 * MII address 0.
2852 */
2853 if (phy != 0)
2854 return (0);
2855
2856 /*
2857 * Apparently, after a reset, the DP83815 can take a while
2858 * to respond. During this recovery period, the BMSR returns
2859 * a value of 0. Catch this -- it's not supposed to happen
2860 * (the BMSR has some hardcoded-to-1 bits), and wait for the
2861 * PHY to come back to life.
2862 *
2863 * This works out because the BMSR is the first register
2864 * read during the PHY probe process.
2865 */
2866 do {
2867 val = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg));
2868 } while (reg == MII_BMSR && val == 0);
2869
2870 return (val & 0xffff);
2871 }
2872
2873 /*
2874 * sip_dp83815_mii_writereg: [mii interface function]
2875 *
2876 * Write a PHY register to the MII.
2877 */
2878 void
2879 SIP_DECL(dp83815_mii_writereg)(struct device *self, int phy, int reg, int val)
2880 {
2881 struct sip_softc *sc = (struct sip_softc *) self;
2882
2883 /*
2884 * The DP83815 only has an internal PHY. Only allow
2885 * MII address 0.
2886 */
2887 if (phy != 0)
2888 return;
2889
2890 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg), val);
2891 }
2892
2893 /*
2894 * sip_dp83815_mii_statchg: [mii interface function]
2895 *
2896 * Callback from MII layer when media changes.
2897 */
2898 void
2899 SIP_DECL(dp83815_mii_statchg)(struct device *self)
2900 {
2901 struct sip_softc *sc = (struct sip_softc *) self;
2902
2903 /*
2904 * Update TXCFG for full-duplex operation.
2905 */
2906 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
2907 sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
2908 else
2909 sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
2910
2911 /*
2912 * Update RXCFG for full-duplex or loopback.
2913 */
2914 if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
2915 IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
2916 sc->sc_rxcfg |= RXCFG_ATX;
2917 else
2918 sc->sc_rxcfg &= ~RXCFG_ATX;
2919
2920 /*
2921 * XXX 802.3x flow control.
2922 */
2923
2924 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
2925 bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
2926 }
2927 #endif /* DP83820 */
2928
2929 #if defined(DP83820)
2930 void
2931 SIP_DECL(dp83820_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
2932 {
2933 u_int16_t eeprom_data[SIP_DP83820_EEPROM_LENGTH / 2];
2934 u_int8_t cksum, *e, match;
2935 int i;
2936
2937 /*
2938 * EEPROM data format for the DP83820 can be found in
2939 * the DP83820 manual, section 4.2.4.
2940 */
2941
2942 SIP_DECL(read_eeprom)(sc, 0,
2943 sizeof(eeprom_data) / sizeof(eeprom_data[0]), eeprom_data);
2944
2945 match = eeprom_data[SIP_DP83820_EEPROM_CHECKSUM / 2] >> 8;
2946 match = ~(match - 1);
2947
2948 cksum = 0x55;
2949 e = (u_int8_t *) eeprom_data;
2950 for (i = 0; i < SIP_DP83820_EEPROM_CHECKSUM; i++)
2951 cksum += *e++;
2952
2953 if (cksum != match)
2954 printf("%s: Checksum (%x) mismatch (%x)",
2955 sc->sc_dev.dv_xname, cksum, match);
2956
2957 enaddr[0] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] & 0xff;
2958 enaddr[1] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] >> 8;
2959 enaddr[2] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] & 0xff;
2960 enaddr[3] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] >> 8;
2961 enaddr[4] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] & 0xff;
2962 enaddr[5] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] >> 8;
2963
2964 /* Get the GPIOR bits. */
2965 sc->sc_gpior = eeprom_data[0x04];
2966
2967 /* Get various CFG related bits. */
2968 if ((eeprom_data[0x05] >> 0) & 1)
2969 sc->sc_cfg |= CFG_EXT_125;
2970 if ((eeprom_data[0x05] >> 9) & 1)
2971 sc->sc_cfg |= CFG_TBI_EN;
2972 }
2973 #else /* ! DP83820 */
2974 void
2975 SIP_DECL(sis900_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
2976 {
2977 u_int16_t myea[ETHER_ADDR_LEN / 2];
2978
2979 SIP_DECL(read_eeprom)(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
2980 sizeof(myea) / sizeof(myea[0]), myea);
2981
2982 enaddr[0] = myea[0] & 0xff;
2983 enaddr[1] = myea[0] >> 8;
2984 enaddr[2] = myea[1] & 0xff;
2985 enaddr[3] = myea[1] >> 8;
2986 enaddr[4] = myea[2] & 0xff;
2987 enaddr[5] = myea[2] >> 8;
2988 }
2989
2990 /* Table and macro to bit-reverse an octet. */
2991 static const u_int8_t bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
2992 #define bbr(v) ((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
2993
2994 void
2995 SIP_DECL(dp83815_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
2996 {
2997 u_int16_t eeprom_data[SIP_DP83815_EEPROM_LENGTH / 2], *ea;
2998 u_int8_t cksum, *e, match;
2999 int i;
3000
3001 SIP_DECL(read_eeprom)(sc, 0, sizeof(eeprom_data) /
3002 sizeof(eeprom_data[0]), eeprom_data);
3003
3004 match = eeprom_data[SIP_DP83815_EEPROM_CHECKSUM/2] >> 8;
3005 match = ~(match - 1);
3006
3007 cksum = 0x55;
3008 e = (u_int8_t *) eeprom_data;
3009 for (i=0 ; i<SIP_DP83815_EEPROM_CHECKSUM ; i++) {
3010 cksum += *e++;
3011 }
3012 if (cksum != match) {
3013 printf("%s: Checksum (%x) mismatch (%x)",
3014 sc->sc_dev.dv_xname, cksum, match);
3015 }
3016
3017 /*
3018 * Unrolled because it makes slightly more sense this way.
3019 * The DP83815 stores the MAC address in bit 0 of word 6
3020 * through bit 15 of word 8.
3021 */
3022 ea = &eeprom_data[6];
3023 enaddr[0] = ((*ea & 0x1) << 7);
3024 ea++;
3025 enaddr[0] |= ((*ea & 0xFE00) >> 9);
3026 enaddr[1] = ((*ea & 0x1FE) >> 1);
3027 enaddr[2] = ((*ea & 0x1) << 7);
3028 ea++;
3029 enaddr[2] |= ((*ea & 0xFE00) >> 9);
3030 enaddr[3] = ((*ea & 0x1FE) >> 1);
3031 enaddr[4] = ((*ea & 0x1) << 7);
3032 ea++;
3033 enaddr[4] |= ((*ea & 0xFE00) >> 9);
3034 enaddr[5] = ((*ea & 0x1FE) >> 1);
3035
3036 /*
3037 * In case that's not weird enough, we also need to reverse
3038 * the bits in each byte. This all actually makes more sense
3039 * if you think about the EEPROM storage as an array of bits
3040 * being shifted into bytes, but that's not how we're looking
3041 * at it here...
3042 */
3043 for (i = 0; i < 6 ;i++)
3044 enaddr[i] = bbr(enaddr[i]);
3045 }
3046 #endif /* DP83820 */
3047
3048 /*
3049 * sip_mediastatus: [ifmedia interface function]
3050 *
3051 * Get the current interface media status.
3052 */
3053 void
3054 SIP_DECL(mediastatus)(struct ifnet *ifp, struct ifmediareq *ifmr)
3055 {
3056 struct sip_softc *sc = ifp->if_softc;
3057
3058 mii_pollstat(&sc->sc_mii);
3059 ifmr->ifm_status = sc->sc_mii.mii_media_status;
3060 ifmr->ifm_active = sc->sc_mii.mii_media_active;
3061 }
3062
3063 /*
3064 * sip_mediachange: [ifmedia interface function]
3065 *
3066 * Set hardware to newly-selected media.
3067 */
3068 int
3069 SIP_DECL(mediachange)(struct ifnet *ifp)
3070 {
3071 struct sip_softc *sc = ifp->if_softc;
3072
3073 if (ifp->if_flags & IFF_UP)
3074 mii_mediachg(&sc->sc_mii);
3075 return (0);
3076 }
3077