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