if_stge.c revision 1.41 1 /* $NetBSD: if_stge.c,v 1.41 2008/02/07 01:21:57 dyoung 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 * Device driver for the Sundance Tech. TC9021 10/100/1000
41 * Ethernet controller.
42 */
43
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: if_stge.c,v 1.41 2008/02/07 01:21:57 dyoung Exp $");
46
47 #include "bpfilter.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/callout.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
59 #include <sys/queue.h>
60
61 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
62
63 #include <net/if.h>
64 #include <net/if_dl.h>
65 #include <net/if_media.h>
66 #include <net/if_ether.h>
67
68 #if NBPFILTER > 0
69 #include <net/bpf.h>
70 #endif
71
72 #include <sys/bus.h>
73 #include <sys/intr.h>
74
75 #include <dev/mii/mii.h>
76 #include <dev/mii/miivar.h>
77 #include <dev/mii/mii_bitbang.h>
78
79 #include <dev/pci/pcireg.h>
80 #include <dev/pci/pcivar.h>
81 #include <dev/pci/pcidevs.h>
82
83 #include <dev/pci/if_stgereg.h>
84
85 /* #define STGE_CU_BUG 1 */
86 #define STGE_VLAN_UNTAG 1
87 /* #define STGE_VLAN_CFI 1 */
88
89 /*
90 * Transmit descriptor list size.
91 */
92 #define STGE_NTXDESC 256
93 #define STGE_NTXDESC_MASK (STGE_NTXDESC - 1)
94 #define STGE_NEXTTX(x) (((x) + 1) & STGE_NTXDESC_MASK)
95
96 /*
97 * Receive descriptor list size.
98 */
99 #define STGE_NRXDESC 256
100 #define STGE_NRXDESC_MASK (STGE_NRXDESC - 1)
101 #define STGE_NEXTRX(x) (((x) + 1) & STGE_NRXDESC_MASK)
102
103 /*
104 * Only interrupt every N frames. Must be a power-of-two.
105 */
106 #define STGE_TXINTR_SPACING 16
107 #define STGE_TXINTR_SPACING_MASK (STGE_TXINTR_SPACING - 1)
108
109 /*
110 * Control structures are DMA'd to the TC9021 chip. We allocate them in
111 * a single clump that maps to a single DMA segment to make several things
112 * easier.
113 */
114 struct stge_control_data {
115 /*
116 * The transmit descriptors.
117 */
118 struct stge_tfd scd_txdescs[STGE_NTXDESC];
119
120 /*
121 * The receive descriptors.
122 */
123 struct stge_rfd scd_rxdescs[STGE_NRXDESC];
124 };
125
126 #define STGE_CDOFF(x) offsetof(struct stge_control_data, x)
127 #define STGE_CDTXOFF(x) STGE_CDOFF(scd_txdescs[(x)])
128 #define STGE_CDRXOFF(x) STGE_CDOFF(scd_rxdescs[(x)])
129
130 /*
131 * Software state for transmit and receive jobs.
132 */
133 struct stge_descsoft {
134 struct mbuf *ds_mbuf; /* head of our mbuf chain */
135 bus_dmamap_t ds_dmamap; /* our DMA map */
136 };
137
138 /*
139 * Software state per device.
140 */
141 struct stge_softc {
142 struct device sc_dev; /* generic device information */
143 bus_space_tag_t sc_st; /* bus space tag */
144 bus_space_handle_t sc_sh; /* bus space handle */
145 bus_dma_tag_t sc_dmat; /* bus DMA tag */
146 struct ethercom sc_ethercom; /* ethernet common data */
147 void *sc_sdhook; /* shutdown hook */
148 int sc_rev; /* silicon revision */
149
150 void *sc_ih; /* interrupt cookie */
151
152 struct mii_data sc_mii; /* MII/media information */
153
154 callout_t sc_tick_ch; /* tick callout */
155
156 bus_dmamap_t sc_cddmamap; /* control data DMA map */
157 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr
158
159 /*
160 * Software state for transmit and receive descriptors.
161 */
162 struct stge_descsoft sc_txsoft[STGE_NTXDESC];
163 struct stge_descsoft sc_rxsoft[STGE_NRXDESC];
164
165 /*
166 * Control data structures.
167 */
168 struct stge_control_data *sc_control_data;
169 #define sc_txdescs sc_control_data->scd_txdescs
170 #define sc_rxdescs sc_control_data->scd_rxdescs
171
172 #ifdef STGE_EVENT_COUNTERS
173 /*
174 * Event counters.
175 */
176 struct evcnt sc_ev_txstall; /* Tx stalled */
177 struct evcnt sc_ev_txdmaintr; /* Tx DMA interrupts */
178 struct evcnt sc_ev_txindintr; /* Tx Indicate interrupts */
179 struct evcnt sc_ev_rxintr; /* Rx interrupts */
180
181 struct evcnt sc_ev_txseg1; /* Tx packets w/ 1 segment */
182 struct evcnt sc_ev_txseg2; /* Tx packets w/ 2 segments */
183 struct evcnt sc_ev_txseg3; /* Tx packets w/ 3 segments */
184 struct evcnt sc_ev_txseg4; /* Tx packets w/ 4 segments */
185 struct evcnt sc_ev_txseg5; /* Tx packets w/ 5 segments */
186 struct evcnt sc_ev_txsegmore; /* Tx packets w/ more than 5 segments */
187 struct evcnt sc_ev_txcopy; /* Tx packets that we had to copy */
188
189 struct evcnt sc_ev_rxipsum; /* IP checksums checked in-bound */
190 struct evcnt sc_ev_rxtcpsum; /* TCP checksums checked in-bound */
191 struct evcnt sc_ev_rxudpsum; /* UDP checksums checked in-bound */
192
193 struct evcnt sc_ev_txipsum; /* IP checksums comp. out-bound */
194 struct evcnt sc_ev_txtcpsum; /* TCP checksums comp. out-bound */
195 struct evcnt sc_ev_txudpsum; /* UDP checksums comp. out-bound */
196 #endif /* STGE_EVENT_COUNTERS */
197
198 int sc_txpending; /* number of Tx requests pending */
199 int sc_txdirty; /* first dirty Tx descriptor */
200 int sc_txlast; /* last used Tx descriptor */
201
202 int sc_rxptr; /* next ready Rx descriptor/descsoft */
203 int sc_rxdiscard;
204 int sc_rxlen;
205 struct mbuf *sc_rxhead;
206 struct mbuf *sc_rxtail;
207 struct mbuf **sc_rxtailp;
208
209 int sc_txthresh; /* Tx threshold */
210 uint32_t sc_usefiber:1; /* if we're fiber */
211 uint32_t sc_stge1023:1; /* are we a 1023 */
212 uint32_t sc_DMACtrl; /* prototype DMACtrl register */
213 uint32_t sc_MACCtrl; /* prototype MacCtrl register */
214 uint16_t sc_IntEnable; /* prototype IntEnable register */
215 uint16_t sc_ReceiveMode; /* prototype ReceiveMode register */
216 uint8_t sc_PhyCtrl; /* prototype PhyCtrl register */
217 };
218
219 #define STGE_RXCHAIN_RESET(sc) \
220 do { \
221 (sc)->sc_rxtailp = &(sc)->sc_rxhead; \
222 *(sc)->sc_rxtailp = NULL; \
223 (sc)->sc_rxlen = 0; \
224 } while (/*CONSTCOND*/0)
225
226 #define STGE_RXCHAIN_LINK(sc, m) \
227 do { \
228 *(sc)->sc_rxtailp = (sc)->sc_rxtail = (m); \
229 (sc)->sc_rxtailp = &(m)->m_next; \
230 } while (/*CONSTCOND*/0)
231
232 #ifdef STGE_EVENT_COUNTERS
233 #define STGE_EVCNT_INCR(ev) (ev)->ev_count++
234 #else
235 #define STGE_EVCNT_INCR(ev) /* nothing */
236 #endif
237
238 #define STGE_CDTXADDR(sc, x) ((sc)->sc_cddma + STGE_CDTXOFF((x)))
239 #define STGE_CDRXADDR(sc, x) ((sc)->sc_cddma + STGE_CDRXOFF((x)))
240
241 #define STGE_CDTXSYNC(sc, x, ops) \
242 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
243 STGE_CDTXOFF((x)), sizeof(struct stge_tfd), (ops))
244
245 #define STGE_CDRXSYNC(sc, x, ops) \
246 bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap, \
247 STGE_CDRXOFF((x)), sizeof(struct stge_rfd), (ops))
248
249 #define STGE_INIT_RXDESC(sc, x) \
250 do { \
251 struct stge_descsoft *__ds = &(sc)->sc_rxsoft[(x)]; \
252 struct stge_rfd *__rfd = &(sc)->sc_rxdescs[(x)]; \
253 \
254 /* \
255 * Note: We scoot the packet forward 2 bytes in the buffer \
256 * so that the payload after the Ethernet header is aligned \
257 * to a 4-byte boundary. \
258 */ \
259 __rfd->rfd_frag.frag_word0 = \
260 htole64(FRAG_ADDR(__ds->ds_dmamap->dm_segs[0].ds_addr + 2) |\
261 FRAG_LEN(MCLBYTES - 2)); \
262 __rfd->rfd_next = \
263 htole64((uint64_t)STGE_CDRXADDR((sc), STGE_NEXTRX((x)))); \
264 __rfd->rfd_status = 0; \
265 STGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
266 } while (/*CONSTCOND*/0)
267
268 #define STGE_TIMEOUT 1000
269
270 static void stge_start(struct ifnet *);
271 static void stge_watchdog(struct ifnet *);
272 static int stge_ioctl(struct ifnet *, u_long, void *);
273 static int stge_init(struct ifnet *);
274 static void stge_stop(struct ifnet *, int);
275
276 static void stge_shutdown(void *);
277
278 static void stge_reset(struct stge_softc *);
279 static void stge_rxdrain(struct stge_softc *);
280 static int stge_add_rxbuf(struct stge_softc *, int);
281 static void stge_read_eeprom(struct stge_softc *, int, uint16_t *);
282 static void stge_tick(void *);
283
284 static void stge_stats_update(struct stge_softc *);
285
286 static void stge_set_filter(struct stge_softc *);
287
288 static int stge_intr(void *);
289 static void stge_txintr(struct stge_softc *);
290 static void stge_rxintr(struct stge_softc *);
291
292 static int stge_mii_readreg(struct device *, int, int);
293 static void stge_mii_writereg(struct device *, int, int, int);
294 static void stge_mii_statchg(struct device *);
295
296 static int stge_match(struct device *, struct cfdata *, void *);
297 static void stge_attach(struct device *, struct device *, void *);
298
299 int stge_copy_small = 0;
300
301 CFATTACH_DECL(stge, sizeof(struct stge_softc),
302 stge_match, stge_attach, NULL, NULL);
303
304 static uint32_t stge_mii_bitbang_read(struct device *);
305 static void stge_mii_bitbang_write(struct device *, uint32_t);
306
307 static const struct mii_bitbang_ops stge_mii_bitbang_ops = {
308 stge_mii_bitbang_read,
309 stge_mii_bitbang_write,
310 {
311 PC_MgmtData, /* MII_BIT_MDO */
312 PC_MgmtData, /* MII_BIT_MDI */
313 PC_MgmtClk, /* MII_BIT_MDC */
314 PC_MgmtDir, /* MII_BIT_DIR_HOST_PHY */
315 0, /* MII_BIT_DIR_PHY_HOST */
316 }
317 };
318
319 /*
320 * Devices supported by this driver.
321 */
322 static const struct stge_product {
323 pci_vendor_id_t stge_vendor;
324 pci_product_id_t stge_product;
325 const char *stge_name;
326 } stge_products[] = {
327 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST1023,
328 "Sundance ST-1023 Gigabit Ethernet" },
329
330 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_SUNDANCETI_ST2021,
331 "Sundance ST-2021 Gigabit Ethernet" },
332
333 { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021,
334 "Tamarack TC9021 Gigabit Ethernet" },
335
336 { PCI_VENDOR_TAMARACK, PCI_PRODUCT_TAMARACK_TC9021_ALT,
337 "Tamarack TC9021 Gigabit Ethernet" },
338
339 /*
340 * The Sundance sample boards use the Sundance vendor ID,
341 * but the Tamarack product ID.
342 */
343 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021,
344 "Sundance TC9021 Gigabit Ethernet" },
345
346 { PCI_VENDOR_SUNDANCETI, PCI_PRODUCT_TAMARACK_TC9021_ALT,
347 "Sundance TC9021 Gigabit Ethernet" },
348
349 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DL4000,
350 "D-Link DL-4000 Gigabit Ethernet" },
351
352 { PCI_VENDOR_ANTARES, PCI_PRODUCT_ANTARES_TC9021,
353 "Antares Gigabit Ethernet" },
354
355 { 0, 0,
356 NULL },
357 };
358
359 static const struct stge_product *
360 stge_lookup(const struct pci_attach_args *pa)
361 {
362 const struct stge_product *sp;
363
364 for (sp = stge_products; sp->stge_name != NULL; sp++) {
365 if (PCI_VENDOR(pa->pa_id) == sp->stge_vendor &&
366 PCI_PRODUCT(pa->pa_id) == sp->stge_product)
367 return (sp);
368 }
369 return (NULL);
370 }
371
372 static int
373 stge_match(struct device *parent, struct cfdata *cf,
374 void *aux)
375 {
376 struct pci_attach_args *pa = aux;
377
378 if (stge_lookup(pa) != NULL)
379 return (1);
380
381 return (0);
382 }
383
384 static void
385 stge_attach(struct device *parent, struct device *self, void *aux)
386 {
387 struct stge_softc *sc = (struct stge_softc *) self;
388 struct pci_attach_args *pa = aux;
389 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
390 pci_chipset_tag_t pc = pa->pa_pc;
391 pci_intr_handle_t ih;
392 const char *intrstr = NULL;
393 bus_space_tag_t iot, memt;
394 bus_space_handle_t ioh, memh;
395 bus_dma_segment_t seg;
396 int ioh_valid, memh_valid;
397 int i, rseg, error;
398 const struct stge_product *sp;
399 uint8_t enaddr[ETHER_ADDR_LEN];
400
401 callout_init(&sc->sc_tick_ch, 0);
402
403 sp = stge_lookup(pa);
404 if (sp == NULL) {
405 printf("\n");
406 panic("ste_attach: impossible");
407 }
408
409 sc->sc_rev = PCI_REVISION(pa->pa_class);
410
411 printf(": %s, rev. %d\n", sp->stge_name, sc->sc_rev);
412
413 /*
414 * Map the device.
415 */
416 ioh_valid = (pci_mapreg_map(pa, STGE_PCI_IOBA,
417 PCI_MAPREG_TYPE_IO, 0,
418 &iot, &ioh, NULL, NULL) == 0);
419 memh_valid = (pci_mapreg_map(pa, STGE_PCI_MMBA,
420 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
421 &memt, &memh, NULL, NULL) == 0);
422
423 if (memh_valid) {
424 sc->sc_st = memt;
425 sc->sc_sh = memh;
426 } else if (ioh_valid) {
427 sc->sc_st = iot;
428 sc->sc_sh = ioh;
429 } else {
430 printf("%s: unable to map device registers\n",
431 sc->sc_dev.dv_xname);
432 return;
433 }
434
435 sc->sc_dmat = pa->pa_dmat;
436
437 /* Enable bus mastering. */
438 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
439 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
440 PCI_COMMAND_MASTER_ENABLE);
441
442 /* power up chip */
443 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
444 NULL)) && error != EOPNOTSUPP) {
445 aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
446 error);
447 return;
448 }
449 /*
450 * Map and establish our interrupt.
451 */
452 if (pci_intr_map(pa, &ih)) {
453 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
454 return;
455 }
456 intrstr = pci_intr_string(pc, ih);
457 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, stge_intr, sc);
458 if (sc->sc_ih == NULL) {
459 printf("%s: unable to establish interrupt",
460 sc->sc_dev.dv_xname);
461 if (intrstr != NULL)
462 printf(" at %s", intrstr);
463 printf("\n");
464 return;
465 }
466 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
467
468 /*
469 * Allocate the control data structures, and create and load the
470 * DMA map for it.
471 */
472 if ((error = bus_dmamem_alloc(sc->sc_dmat,
473 sizeof(struct stge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
474 0)) != 0) {
475 printf("%s: unable to allocate control data, error = %d\n",
476 sc->sc_dev.dv_xname, error);
477 goto fail_0;
478 }
479
480 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
481 sizeof(struct stge_control_data), (void **)&sc->sc_control_data,
482 BUS_DMA_COHERENT)) != 0) {
483 printf("%s: unable to map control data, error = %d\n",
484 sc->sc_dev.dv_xname, error);
485 goto fail_1;
486 }
487
488 if ((error = bus_dmamap_create(sc->sc_dmat,
489 sizeof(struct stge_control_data), 1,
490 sizeof(struct stge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
491 printf("%s: unable to create control data DMA map, "
492 "error = %d\n", sc->sc_dev.dv_xname, error);
493 goto fail_2;
494 }
495
496 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
497 sc->sc_control_data, sizeof(struct stge_control_data), NULL,
498 0)) != 0) {
499 printf("%s: unable to load control data DMA map, error = %d\n",
500 sc->sc_dev.dv_xname, error);
501 goto fail_3;
502 }
503
504 /*
505 * Create the transmit buffer DMA maps. Note that rev B.3
506 * and earlier seem to have a bug regarding multi-fragment
507 * packets. We need to limit the number of Tx segments on
508 * such chips to 1.
509 */
510 for (i = 0; i < STGE_NTXDESC; i++) {
511 if ((error = bus_dmamap_create(sc->sc_dmat,
512 ETHER_MAX_LEN_JUMBO, STGE_NTXFRAGS, MCLBYTES, 0, 0,
513 &sc->sc_txsoft[i].ds_dmamap)) != 0) {
514 printf("%s: unable to create tx DMA map %d, "
515 "error = %d\n", sc->sc_dev.dv_xname, i, error);
516 goto fail_4;
517 }
518 }
519
520 /*
521 * Create the receive buffer DMA maps.
522 */
523 for (i = 0; i < STGE_NRXDESC; i++) {
524 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
525 MCLBYTES, 0, 0, &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
526 printf("%s: unable to create rx DMA map %d, "
527 "error = %d\n", sc->sc_dev.dv_xname, i, error);
528 goto fail_5;
529 }
530 sc->sc_rxsoft[i].ds_mbuf = NULL;
531 }
532
533 /*
534 * Determine if we're copper or fiber. It affects how we
535 * reset the card.
536 */
537 if (bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
538 AC_PhyMedia)
539 sc->sc_usefiber = 1;
540 else
541 sc->sc_usefiber = 0;
542
543 /*
544 * Reset the chip to a known state.
545 */
546 stge_reset(sc);
547
548 /*
549 * Reading the station address from the EEPROM doesn't seem
550 * to work, at least on my sample boards. Instead, since
551 * the reset sequence does AutoInit, read it from the station
552 * address registers. For Sundance 1023 you can only read it
553 * from EEPROM.
554 */
555 if (sp->stge_product != PCI_PRODUCT_SUNDANCETI_ST1023) {
556 enaddr[0] = bus_space_read_2(sc->sc_st, sc->sc_sh,
557 STGE_StationAddress0) & 0xff;
558 enaddr[1] = bus_space_read_2(sc->sc_st, sc->sc_sh,
559 STGE_StationAddress0) >> 8;
560 enaddr[2] = bus_space_read_2(sc->sc_st, sc->sc_sh,
561 STGE_StationAddress1) & 0xff;
562 enaddr[3] = bus_space_read_2(sc->sc_st, sc->sc_sh,
563 STGE_StationAddress1) >> 8;
564 enaddr[4] = bus_space_read_2(sc->sc_st, sc->sc_sh,
565 STGE_StationAddress2) & 0xff;
566 enaddr[5] = bus_space_read_2(sc->sc_st, sc->sc_sh,
567 STGE_StationAddress2) >> 8;
568 sc->sc_stge1023 = 0;
569 } else {
570 uint16_t myaddr[ETHER_ADDR_LEN / 2];
571 for (i = 0; i <ETHER_ADDR_LEN / 2; i++) {
572 stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i,
573 &myaddr[i]);
574 myaddr[i] = le16toh(myaddr[i]);
575 }
576 (void)memcpy(enaddr, myaddr, sizeof(enaddr));
577 sc->sc_stge1023 = 1;
578 }
579
580 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
581 ether_sprintf(enaddr));
582
583 /*
584 * Read some important bits from the PhyCtrl register.
585 */
586 sc->sc_PhyCtrl = bus_space_read_1(sc->sc_st, sc->sc_sh,
587 STGE_PhyCtrl) & (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
588
589 /*
590 * Initialize our media structures and probe the MII.
591 */
592 sc->sc_mii.mii_ifp = ifp;
593 sc->sc_mii.mii_readreg = stge_mii_readreg;
594 sc->sc_mii.mii_writereg = stge_mii_writereg;
595 sc->sc_mii.mii_statchg = stge_mii_statchg;
596 sc->sc_ethercom.ec_mii = &sc->sc_mii;
597 ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ether_mediachange,
598 ether_mediastatus);
599 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
600 MII_OFFSET_ANY, MIIF_DOPAUSE);
601 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
602 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
603 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
604 } else
605 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
606
607 ifp = &sc->sc_ethercom.ec_if;
608 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
609 ifp->if_softc = sc;
610 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
611 ifp->if_ioctl = stge_ioctl;
612 ifp->if_start = stge_start;
613 ifp->if_watchdog = stge_watchdog;
614 ifp->if_init = stge_init;
615 ifp->if_stop = stge_stop;
616 IFQ_SET_READY(&ifp->if_snd);
617
618 /*
619 * The manual recommends disabling early transmit, so we
620 * do. It's disabled anyway, if using IP checksumming,
621 * since the entire packet must be in the FIFO in order
622 * for the chip to perform the checksum.
623 */
624 sc->sc_txthresh = 0x0fff;
625
626 /*
627 * Disable MWI if the PCI layer tells us to.
628 */
629 sc->sc_DMACtrl = 0;
630 if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
631 sc->sc_DMACtrl |= DMAC_MWIDisable;
632
633 /*
634 * We can support 802.1Q VLAN-sized frames and jumbo
635 * Ethernet frames.
636 *
637 * XXX Figure out how to do hw-assisted VLAN tagging in
638 * XXX a reasonable way on this chip.
639 */
640 sc->sc_ethercom.ec_capabilities |=
641 ETHERCAP_VLAN_MTU | /* XXX ETHERCAP_JUMBO_MTU | */
642 ETHERCAP_VLAN_HWTAGGING;
643
644 /*
645 * We can do IPv4/TCPv4/UDPv4 checksums in hardware.
646 */
647 sc->sc_ethercom.ec_if.if_capabilities |=
648 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
649 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
650 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
651
652 /*
653 * Attach the interface.
654 */
655 if_attach(ifp);
656 ether_ifattach(ifp, enaddr);
657
658 #ifdef STGE_EVENT_COUNTERS
659 /*
660 * Attach event counters.
661 */
662 evcnt_attach_dynamic(&sc->sc_ev_txstall, EVCNT_TYPE_MISC,
663 NULL, sc->sc_dev.dv_xname, "txstall");
664 evcnt_attach_dynamic(&sc->sc_ev_txdmaintr, EVCNT_TYPE_INTR,
665 NULL, sc->sc_dev.dv_xname, "txdmaintr");
666 evcnt_attach_dynamic(&sc->sc_ev_txindintr, EVCNT_TYPE_INTR,
667 NULL, sc->sc_dev.dv_xname, "txindintr");
668 evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
669 NULL, sc->sc_dev.dv_xname, "rxintr");
670
671 evcnt_attach_dynamic(&sc->sc_ev_txseg1, EVCNT_TYPE_MISC,
672 NULL, sc->sc_dev.dv_xname, "txseg1");
673 evcnt_attach_dynamic(&sc->sc_ev_txseg2, EVCNT_TYPE_MISC,
674 NULL, sc->sc_dev.dv_xname, "txseg2");
675 evcnt_attach_dynamic(&sc->sc_ev_txseg3, EVCNT_TYPE_MISC,
676 NULL, sc->sc_dev.dv_xname, "txseg3");
677 evcnt_attach_dynamic(&sc->sc_ev_txseg4, EVCNT_TYPE_MISC,
678 NULL, sc->sc_dev.dv_xname, "txseg4");
679 evcnt_attach_dynamic(&sc->sc_ev_txseg5, EVCNT_TYPE_MISC,
680 NULL, sc->sc_dev.dv_xname, "txseg5");
681 evcnt_attach_dynamic(&sc->sc_ev_txsegmore, EVCNT_TYPE_MISC,
682 NULL, sc->sc_dev.dv_xname, "txsegmore");
683 evcnt_attach_dynamic(&sc->sc_ev_txcopy, EVCNT_TYPE_MISC,
684 NULL, sc->sc_dev.dv_xname, "txcopy");
685
686 evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
687 NULL, sc->sc_dev.dv_xname, "rxipsum");
688 evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
689 NULL, sc->sc_dev.dv_xname, "rxtcpsum");
690 evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
691 NULL, sc->sc_dev.dv_xname, "rxudpsum");
692 evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
693 NULL, sc->sc_dev.dv_xname, "txipsum");
694 evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
695 NULL, sc->sc_dev.dv_xname, "txtcpsum");
696 evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
697 NULL, sc->sc_dev.dv_xname, "txudpsum");
698 #endif /* STGE_EVENT_COUNTERS */
699
700 /*
701 * Make sure the interface is shutdown during reboot.
702 */
703 sc->sc_sdhook = shutdownhook_establish(stge_shutdown, sc);
704 if (sc->sc_sdhook == NULL)
705 printf("%s: WARNING: unable to establish shutdown hook\n",
706 sc->sc_dev.dv_xname);
707 return;
708
709 /*
710 * Free any resources we've allocated during the failed attach
711 * attempt. Do this in reverse order and fall through.
712 */
713 fail_5:
714 for (i = 0; i < STGE_NRXDESC; i++) {
715 if (sc->sc_rxsoft[i].ds_dmamap != NULL)
716 bus_dmamap_destroy(sc->sc_dmat,
717 sc->sc_rxsoft[i].ds_dmamap);
718 }
719 fail_4:
720 for (i = 0; i < STGE_NTXDESC; i++) {
721 if (sc->sc_txsoft[i].ds_dmamap != NULL)
722 bus_dmamap_destroy(sc->sc_dmat,
723 sc->sc_txsoft[i].ds_dmamap);
724 }
725 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
726 fail_3:
727 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
728 fail_2:
729 bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
730 sizeof(struct stge_control_data));
731 fail_1:
732 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
733 fail_0:
734 return;
735 }
736
737 /*
738 * stge_shutdown:
739 *
740 * Make sure the interface is stopped at reboot time.
741 */
742 static void
743 stge_shutdown(void *arg)
744 {
745 struct stge_softc *sc = arg;
746
747 stge_stop(&sc->sc_ethercom.ec_if, 1);
748 }
749
750 static void
751 stge_dma_wait(struct stge_softc *sc)
752 {
753 int i;
754
755 for (i = 0; i < STGE_TIMEOUT; i++) {
756 delay(2);
757 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl) &
758 DMAC_TxDMAInProg) == 0)
759 break;
760 }
761
762 if (i == STGE_TIMEOUT)
763 printf("%s: DMA wait timed out\n", sc->sc_dev.dv_xname);
764 }
765
766 /*
767 * stge_start: [ifnet interface function]
768 *
769 * Start packet transmission on the interface.
770 */
771 static void
772 stge_start(struct ifnet *ifp)
773 {
774 struct stge_softc *sc = ifp->if_softc;
775 struct mbuf *m0;
776 struct stge_descsoft *ds;
777 struct stge_tfd *tfd;
778 bus_dmamap_t dmamap;
779 int error, firsttx, nexttx, opending, seg, totlen;
780 uint64_t csum_flags;
781
782 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
783 return;
784
785 /*
786 * Remember the previous number of pending transmissions
787 * and the first descriptor we will use.
788 */
789 opending = sc->sc_txpending;
790 firsttx = STGE_NEXTTX(sc->sc_txlast);
791
792 /*
793 * Loop through the send queue, setting up transmit descriptors
794 * until we drain the queue, or use up all available transmit
795 * descriptors.
796 */
797 for (;;) {
798 struct m_tag *mtag;
799 uint64_t tfc;
800
801 /*
802 * Grab a packet off the queue.
803 */
804 IFQ_POLL(&ifp->if_snd, m0);
805 if (m0 == NULL)
806 break;
807
808 /*
809 * Leave one unused descriptor at the end of the
810 * list to prevent wrapping completely around.
811 */
812 if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
813 STGE_EVCNT_INCR(&sc->sc_ev_txstall);
814 break;
815 }
816
817 /*
818 * See if we have any VLAN stuff.
819 */
820 mtag = VLAN_OUTPUT_TAG(&sc->sc_ethercom, m0);
821
822 /*
823 * Get the last and next available transmit descriptor.
824 */
825 nexttx = STGE_NEXTTX(sc->sc_txlast);
826 tfd = &sc->sc_txdescs[nexttx];
827 ds = &sc->sc_txsoft[nexttx];
828
829 dmamap = ds->ds_dmamap;
830
831 /*
832 * Load the DMA map. If this fails, the packet either
833 * didn't fit in the alloted number of segments, or we
834 * were short on resources. For the too-many-segments
835 * case, we simply report an error and drop the packet,
836 * since we can't sanely copy a jumbo packet to a single
837 * buffer.
838 */
839 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
840 BUS_DMA_NOWAIT);
841 if (error) {
842 if (error == EFBIG) {
843 printf("%s: Tx packet consumes too many "
844 "DMA segments, dropping...\n",
845 sc->sc_dev.dv_xname);
846 IFQ_DEQUEUE(&ifp->if_snd, m0);
847 m_freem(m0);
848 continue;
849 }
850 /*
851 * Short on resources, just stop for now.
852 */
853 break;
854 }
855
856 IFQ_DEQUEUE(&ifp->if_snd, m0);
857
858 /*
859 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
860 */
861
862 /* Sync the DMA map. */
863 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
864 BUS_DMASYNC_PREWRITE);
865
866 /* Initialize the fragment list. */
867 for (totlen = 0, seg = 0; seg < dmamap->dm_nsegs; seg++) {
868 tfd->tfd_frags[seg].frag_word0 =
869 htole64(FRAG_ADDR(dmamap->dm_segs[seg].ds_addr) |
870 FRAG_LEN(dmamap->dm_segs[seg].ds_len));
871 totlen += dmamap->dm_segs[seg].ds_len;
872 }
873
874 #ifdef STGE_EVENT_COUNTERS
875 switch (dmamap->dm_nsegs) {
876 case 1:
877 STGE_EVCNT_INCR(&sc->sc_ev_txseg1);
878 break;
879 case 2:
880 STGE_EVCNT_INCR(&sc->sc_ev_txseg2);
881 break;
882 case 3:
883 STGE_EVCNT_INCR(&sc->sc_ev_txseg3);
884 break;
885 case 4:
886 STGE_EVCNT_INCR(&sc->sc_ev_txseg4);
887 break;
888 case 5:
889 STGE_EVCNT_INCR(&sc->sc_ev_txseg5);
890 break;
891 default:
892 STGE_EVCNT_INCR(&sc->sc_ev_txsegmore);
893 break;
894 }
895 #endif /* STGE_EVENT_COUNTERS */
896
897 /*
898 * Initialize checksumming flags in the descriptor.
899 * Byte-swap constants so the compiler can optimize.
900 */
901 csum_flags = 0;
902 if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
903 STGE_EVCNT_INCR(&sc->sc_ev_txipsum);
904 csum_flags |= TFD_IPChecksumEnable;
905 }
906
907 if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
908 STGE_EVCNT_INCR(&sc->sc_ev_txtcpsum);
909 csum_flags |= TFD_TCPChecksumEnable;
910 } else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
911 STGE_EVCNT_INCR(&sc->sc_ev_txudpsum);
912 csum_flags |= TFD_UDPChecksumEnable;
913 }
914
915 /*
916 * Initialize the descriptor and give it to the chip.
917 * Check to see if we have a VLAN tag to insert.
918 */
919
920 tfc = TFD_FrameId(nexttx) | TFD_WordAlign(/*totlen & */3) |
921 TFD_FragCount(seg) | csum_flags |
922 (((nexttx & STGE_TXINTR_SPACING_MASK) == 0) ?
923 TFD_TxDMAIndicate : 0);
924 if (mtag) {
925 #if 0
926 struct ether_header *eh =
927 mtod(m0, struct ether_header *);
928 u_int16_t etype = ntohs(eh->ether_type);
929 printf("%s: xmit (tag %d) etype %x\n",
930 ifp->if_xname, *mtod(n, int *), etype);
931 #endif
932 tfc |= TFD_VLANTagInsert |
933 #ifdef STGE_VLAN_CFI
934 TFD_CFI |
935 #endif
936 TFD_VID(VLAN_TAG_VALUE(mtag));
937 }
938 tfd->tfd_control = htole64(tfc);
939
940 /* Sync the descriptor. */
941 STGE_CDTXSYNC(sc, nexttx,
942 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
943
944 /*
945 * Kick the transmit DMA logic.
946 */
947 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_DMACtrl,
948 sc->sc_DMACtrl | DMAC_TxDMAPollNow);
949
950 /*
951 * Store a pointer to the packet so we can free it later.
952 */
953 ds->ds_mbuf = m0;
954
955 /* Advance the tx pointer. */
956 sc->sc_txpending++;
957 sc->sc_txlast = nexttx;
958
959 #if NBPFILTER > 0
960 /*
961 * Pass the packet to any BPF listeners.
962 */
963 if (ifp->if_bpf)
964 bpf_mtap(ifp->if_bpf, m0);
965 #endif /* NBPFILTER > 0 */
966 }
967
968 if (sc->sc_txpending == (STGE_NTXDESC - 1)) {
969 /* No more slots left; notify upper layer. */
970 ifp->if_flags |= IFF_OACTIVE;
971 }
972
973 if (sc->sc_txpending != opending) {
974 /*
975 * We enqueued packets. If the transmitter was idle,
976 * reset the txdirty pointer.
977 */
978 if (opending == 0)
979 sc->sc_txdirty = firsttx;
980
981 /* Set a watchdog timer in case the chip flakes out. */
982 ifp->if_timer = 5;
983 }
984 }
985
986 /*
987 * stge_watchdog: [ifnet interface function]
988 *
989 * Watchdog timer handler.
990 */
991 static void
992 stge_watchdog(struct ifnet *ifp)
993 {
994 struct stge_softc *sc = ifp->if_softc;
995
996 /*
997 * Sweep up first, since we don't interrupt every frame.
998 */
999 stge_txintr(sc);
1000 if (sc->sc_txpending != 0) {
1001 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1002 ifp->if_oerrors++;
1003
1004 (void) stge_init(ifp);
1005
1006 /* Try to get more packets going. */
1007 stge_start(ifp);
1008 }
1009 }
1010
1011 /*
1012 * stge_ioctl: [ifnet interface function]
1013 *
1014 * Handle control requests from the operator.
1015 */
1016 static int
1017 stge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1018 {
1019 struct stge_softc *sc = ifp->if_softc;
1020 int s, error;
1021
1022 s = splnet();
1023
1024 error = ether_ioctl(ifp, cmd, data);
1025 if (error == ENETRESET) {
1026 error = 0;
1027
1028 if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
1029 ;
1030 else if (ifp->if_flags & IFF_RUNNING) {
1031 /*
1032 * Multicast list has changed; set the hardware filter
1033 * accordingly.
1034 */
1035 stge_set_filter(sc);
1036 }
1037 }
1038
1039 /* Try to get more packets going. */
1040 stge_start(ifp);
1041
1042 splx(s);
1043 return (error);
1044 }
1045
1046 /*
1047 * stge_intr:
1048 *
1049 * Interrupt service routine.
1050 */
1051 static int
1052 stge_intr(void *arg)
1053 {
1054 struct stge_softc *sc = arg;
1055 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1056 uint32_t txstat;
1057 int wantinit;
1058 uint16_t isr;
1059
1060 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatus) &
1061 IS_InterruptStatus) == 0)
1062 return (0);
1063
1064 for (wantinit = 0; wantinit == 0;) {
1065 isr = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_IntStatusAck);
1066 if ((isr & sc->sc_IntEnable) == 0)
1067 break;
1068
1069 /* Host interface errors. */
1070 if (isr & IS_HostError) {
1071 printf("%s: Host interface error\n",
1072 sc->sc_dev.dv_xname);
1073 wantinit = 1;
1074 continue;
1075 }
1076
1077 /* Receive interrupts. */
1078 if (isr & (IS_RxDMAComplete|IS_RFDListEnd)) {
1079 STGE_EVCNT_INCR(&sc->sc_ev_rxintr);
1080 stge_rxintr(sc);
1081 if (isr & IS_RFDListEnd) {
1082 printf("%s: receive ring overflow\n",
1083 sc->sc_dev.dv_xname);
1084 /*
1085 * XXX Should try to recover from this
1086 * XXX more gracefully.
1087 */
1088 wantinit = 1;
1089 }
1090 }
1091
1092 /* Transmit interrupts. */
1093 if (isr & (IS_TxDMAComplete|IS_TxComplete)) {
1094 #ifdef STGE_EVENT_COUNTERS
1095 if (isr & IS_TxDMAComplete)
1096 STGE_EVCNT_INCR(&sc->sc_ev_txdmaintr);
1097 #endif
1098 stge_txintr(sc);
1099 }
1100
1101 /* Statistics overflow. */
1102 if (isr & IS_UpdateStats)
1103 stge_stats_update(sc);
1104
1105 /* Transmission errors. */
1106 if (isr & IS_TxComplete) {
1107 STGE_EVCNT_INCR(&sc->sc_ev_txindintr);
1108 for (;;) {
1109 txstat = bus_space_read_4(sc->sc_st, sc->sc_sh,
1110 STGE_TxStatus);
1111 if ((txstat & TS_TxComplete) == 0)
1112 break;
1113 if (txstat & TS_TxUnderrun) {
1114 sc->sc_txthresh++;
1115 if (sc->sc_txthresh > 0x0fff)
1116 sc->sc_txthresh = 0x0fff;
1117 printf("%s: transmit underrun, new "
1118 "threshold: %d bytes\n",
1119 sc->sc_dev.dv_xname,
1120 sc->sc_txthresh << 5);
1121 }
1122 if (txstat & TS_MaxCollisions)
1123 printf("%s: excessive collisions\n",
1124 sc->sc_dev.dv_xname);
1125 }
1126 wantinit = 1;
1127 }
1128
1129 }
1130
1131 if (wantinit)
1132 stge_init(ifp);
1133
1134 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable,
1135 sc->sc_IntEnable);
1136
1137 /* Try to get more packets going. */
1138 stge_start(ifp);
1139
1140 return (1);
1141 }
1142
1143 /*
1144 * stge_txintr:
1145 *
1146 * Helper; handle transmit interrupts.
1147 */
1148 static void
1149 stge_txintr(struct stge_softc *sc)
1150 {
1151 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1152 struct stge_descsoft *ds;
1153 uint64_t control;
1154 int i;
1155
1156 ifp->if_flags &= ~IFF_OACTIVE;
1157
1158 /*
1159 * Go through our Tx list and free mbufs for those
1160 * frames which have been transmitted.
1161 */
1162 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
1163 i = STGE_NEXTTX(i), sc->sc_txpending--) {
1164 ds = &sc->sc_txsoft[i];
1165
1166 STGE_CDTXSYNC(sc, i,
1167 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1168
1169 control = le64toh(sc->sc_txdescs[i].tfd_control);
1170 if ((control & TFD_TFDDone) == 0)
1171 break;
1172
1173 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
1174 0, ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1175 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1176 m_freem(ds->ds_mbuf);
1177 ds->ds_mbuf = NULL;
1178 }
1179
1180 /* Update the dirty transmit buffer pointer. */
1181 sc->sc_txdirty = i;
1182
1183 /*
1184 * If there are no more pending transmissions, cancel the watchdog
1185 * timer.
1186 */
1187 if (sc->sc_txpending == 0)
1188 ifp->if_timer = 0;
1189 }
1190
1191 /*
1192 * stge_rxintr:
1193 *
1194 * Helper; handle receive interrupts.
1195 */
1196 static void
1197 stge_rxintr(struct stge_softc *sc)
1198 {
1199 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1200 struct stge_descsoft *ds;
1201 struct mbuf *m, *tailm;
1202 uint64_t status;
1203 int i, len;
1204
1205 for (i = sc->sc_rxptr;; i = STGE_NEXTRX(i)) {
1206 ds = &sc->sc_rxsoft[i];
1207
1208 STGE_CDRXSYNC(sc, i,
1209 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1210
1211 status = le64toh(sc->sc_rxdescs[i].rfd_status);
1212
1213 if ((status & RFD_RFDDone) == 0)
1214 break;
1215
1216 if (__predict_false(sc->sc_rxdiscard)) {
1217 STGE_INIT_RXDESC(sc, i);
1218 if (status & RFD_FrameEnd) {
1219 /* Reset our state. */
1220 sc->sc_rxdiscard = 0;
1221 }
1222 continue;
1223 }
1224
1225 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1226 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1227
1228 m = ds->ds_mbuf;
1229
1230 /*
1231 * Add a new receive buffer to the ring.
1232 */
1233 if (stge_add_rxbuf(sc, i) != 0) {
1234 /*
1235 * Failed, throw away what we've done so
1236 * far, and discard the rest of the packet.
1237 */
1238 ifp->if_ierrors++;
1239 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1240 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1241 STGE_INIT_RXDESC(sc, i);
1242 if ((status & RFD_FrameEnd) == 0)
1243 sc->sc_rxdiscard = 1;
1244 if (sc->sc_rxhead != NULL)
1245 m_freem(sc->sc_rxhead);
1246 STGE_RXCHAIN_RESET(sc);
1247 continue;
1248 }
1249
1250 #ifdef DIAGNOSTIC
1251 if (status & RFD_FrameStart) {
1252 KASSERT(sc->sc_rxhead == NULL);
1253 KASSERT(sc->sc_rxtailp == &sc->sc_rxhead);
1254 }
1255 #endif
1256
1257 STGE_RXCHAIN_LINK(sc, m);
1258
1259 /*
1260 * If this is not the end of the packet, keep
1261 * looking.
1262 */
1263 if ((status & RFD_FrameEnd) == 0) {
1264 sc->sc_rxlen += m->m_len;
1265 continue;
1266 }
1267
1268 /*
1269 * Okay, we have the entire packet now...
1270 */
1271 *sc->sc_rxtailp = NULL;
1272 m = sc->sc_rxhead;
1273 tailm = sc->sc_rxtail;
1274
1275 STGE_RXCHAIN_RESET(sc);
1276
1277 /*
1278 * If the packet had an error, drop it. Note we
1279 * count the error later in the periodic stats update.
1280 */
1281 if (status & (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1282 RFD_RxAlignmentError | RFD_RxFCSError |
1283 RFD_RxLengthError)) {
1284 m_freem(m);
1285 continue;
1286 }
1287
1288 /*
1289 * No errors.
1290 *
1291 * Note we have configured the chip to not include
1292 * the CRC at the end of the packet.
1293 */
1294 len = RFD_RxDMAFrameLen(status);
1295 tailm->m_len = len - sc->sc_rxlen;
1296
1297 /*
1298 * If the packet is small enough to fit in a
1299 * single header mbuf, allocate one and copy
1300 * the data into it. This greatly reduces
1301 * memory consumption when we receive lots
1302 * of small packets.
1303 */
1304 if (stge_copy_small != 0 && len <= (MHLEN - 2)) {
1305 struct mbuf *nm;
1306 MGETHDR(nm, M_DONTWAIT, MT_DATA);
1307 if (nm == NULL) {
1308 ifp->if_ierrors++;
1309 m_freem(m);
1310 continue;
1311 }
1312 nm->m_data += 2;
1313 nm->m_pkthdr.len = nm->m_len = len;
1314 m_copydata(m, 0, len, mtod(nm, void *));
1315 m_freem(m);
1316 m = nm;
1317 }
1318
1319 /*
1320 * Set the incoming checksum information for the packet.
1321 */
1322 if (status & RFD_IPDetected) {
1323 STGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
1324 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1325 if (status & RFD_IPError)
1326 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
1327 if (status & RFD_TCPDetected) {
1328 STGE_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
1329 m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
1330 if (status & RFD_TCPError)
1331 m->m_pkthdr.csum_flags |=
1332 M_CSUM_TCP_UDP_BAD;
1333 } else if (status & RFD_UDPDetected) {
1334 STGE_EVCNT_INCR(&sc->sc_ev_rxudpsum);
1335 m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
1336 if (status & RFD_UDPError)
1337 m->m_pkthdr.csum_flags |=
1338 M_CSUM_TCP_UDP_BAD;
1339 }
1340 }
1341
1342 m->m_pkthdr.rcvif = ifp;
1343 m->m_pkthdr.len = len;
1344
1345 #if NBPFILTER > 0
1346 /*
1347 * Pass this up to any BPF listeners, but only
1348 * pass if up the stack if it's for us.
1349 */
1350 if (ifp->if_bpf)
1351 bpf_mtap(ifp->if_bpf, m);
1352 #endif /* NBPFILTER > 0 */
1353 #ifdef STGE_VLAN_UNTAG
1354 /*
1355 * Check for VLAN tagged packets
1356 */
1357 if (status & RFD_VLANDetected)
1358 VLAN_INPUT_TAG(ifp, m, RFD_TCI(status), continue);
1359
1360 #endif
1361 #if 0
1362 if (status & RFD_VLANDetected) {
1363 struct ether_header *eh;
1364 u_int16_t etype;
1365
1366 eh = mtod(m, struct ether_header *);
1367 etype = ntohs(eh->ether_type);
1368 printf("%s: VLANtag detected (TCI %d) etype %x\n",
1369 ifp->if_xname, (u_int16_t) RFD_TCI(status),
1370 etype);
1371 }
1372 #endif
1373 /* Pass it on. */
1374 (*ifp->if_input)(ifp, m);
1375 }
1376
1377 /* Update the receive pointer. */
1378 sc->sc_rxptr = i;
1379 }
1380
1381 /*
1382 * stge_tick:
1383 *
1384 * One second timer, used to tick the MII.
1385 */
1386 static void
1387 stge_tick(void *arg)
1388 {
1389 struct stge_softc *sc = arg;
1390 int s;
1391
1392 s = splnet();
1393 mii_tick(&sc->sc_mii);
1394 stge_stats_update(sc);
1395 splx(s);
1396
1397 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1398 }
1399
1400 /*
1401 * stge_stats_update:
1402 *
1403 * Read the TC9021 statistics counters.
1404 */
1405 static void
1406 stge_stats_update(struct stge_softc *sc)
1407 {
1408 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1409 bus_space_tag_t st = sc->sc_st;
1410 bus_space_handle_t sh = sc->sc_sh;
1411
1412 (void) bus_space_read_4(st, sh, STGE_OctetRcvOk);
1413
1414 ifp->if_ipackets +=
1415 bus_space_read_4(st, sh, STGE_FramesRcvdOk);
1416
1417 ifp->if_ierrors +=
1418 (u_int) bus_space_read_2(st, sh, STGE_FramesLostRxErrors);
1419
1420 (void) bus_space_read_4(st, sh, STGE_OctetXmtdOk);
1421
1422 ifp->if_opackets +=
1423 bus_space_read_4(st, sh, STGE_FramesXmtdOk);
1424
1425 ifp->if_collisions +=
1426 bus_space_read_4(st, sh, STGE_LateCollisions) +
1427 bus_space_read_4(st, sh, STGE_MultiColFrames) +
1428 bus_space_read_4(st, sh, STGE_SingleColFrames);
1429
1430 ifp->if_oerrors +=
1431 (u_int) bus_space_read_2(st, sh, STGE_FramesAbortXSColls) +
1432 (u_int) bus_space_read_2(st, sh, STGE_FramesWEXDeferal);
1433 }
1434
1435 /*
1436 * stge_reset:
1437 *
1438 * Perform a soft reset on the TC9021.
1439 */
1440 static void
1441 stge_reset(struct stge_softc *sc)
1442 {
1443 uint32_t ac;
1444 int i;
1445
1446 ac = bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl);
1447
1448 /*
1449 * Only assert RstOut if we're fiber. We need GMII clocks
1450 * to be present in order for the reset to complete on fiber
1451 * cards.
1452 */
1453 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl,
1454 ac | AC_GlobalReset | AC_RxReset | AC_TxReset |
1455 AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
1456 (sc->sc_usefiber ? AC_RstOut : 0));
1457
1458 delay(50000);
1459
1460 for (i = 0; i < STGE_TIMEOUT; i++) {
1461 delay(5000);
1462 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, STGE_AsicCtrl) &
1463 AC_ResetBusy) == 0)
1464 break;
1465 }
1466
1467 if (i == STGE_TIMEOUT)
1468 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
1469
1470 delay(1000);
1471 }
1472
1473 /*
1474 * stge_init: [ ifnet interface function ]
1475 *
1476 * Initialize the interface. Must be called at splnet().
1477 */
1478 static int
1479 stge_init(struct ifnet *ifp)
1480 {
1481 struct stge_softc *sc = ifp->if_softc;
1482 bus_space_tag_t st = sc->sc_st;
1483 bus_space_handle_t sh = sc->sc_sh;
1484 struct stge_descsoft *ds;
1485 int i, error = 0;
1486
1487 /*
1488 * Cancel any pending I/O.
1489 */
1490 stge_stop(ifp, 0);
1491
1492 /*
1493 * Reset the chip to a known state.
1494 */
1495 stge_reset(sc);
1496
1497 /*
1498 * Initialize the transmit descriptor ring.
1499 */
1500 memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
1501 for (i = 0; i < STGE_NTXDESC; i++) {
1502 sc->sc_txdescs[i].tfd_next = htole64(
1503 STGE_CDTXADDR(sc, STGE_NEXTTX(i)));
1504 sc->sc_txdescs[i].tfd_control = htole64(TFD_TFDDone);
1505 }
1506 sc->sc_txpending = 0;
1507 sc->sc_txdirty = 0;
1508 sc->sc_txlast = STGE_NTXDESC - 1;
1509
1510 /*
1511 * Initialize the receive descriptor and receive job
1512 * descriptor rings.
1513 */
1514 for (i = 0; i < STGE_NRXDESC; i++) {
1515 ds = &sc->sc_rxsoft[i];
1516 if (ds->ds_mbuf == NULL) {
1517 if ((error = stge_add_rxbuf(sc, i)) != 0) {
1518 printf("%s: unable to allocate or map rx "
1519 "buffer %d, error = %d\n",
1520 sc->sc_dev.dv_xname, i, error);
1521 /*
1522 * XXX Should attempt to run with fewer receive
1523 * XXX buffers instead of just failing.
1524 */
1525 stge_rxdrain(sc);
1526 goto out;
1527 }
1528 } else
1529 STGE_INIT_RXDESC(sc, i);
1530 }
1531 sc->sc_rxptr = 0;
1532 sc->sc_rxdiscard = 0;
1533 STGE_RXCHAIN_RESET(sc);
1534
1535 /* Set the station address. */
1536 for (i = 0; i < 6; i++)
1537 bus_space_write_1(st, sh, STGE_StationAddress0 + i,
1538 CLLADDR(ifp->if_sadl)[i]);
1539
1540 /*
1541 * Set the statistics masks. Disable all the RMON stats,
1542 * and disable selected stats in the non-RMON stats registers.
1543 */
1544 bus_space_write_4(st, sh, STGE_RMONStatisticsMask, 0xffffffff);
1545 bus_space_write_4(st, sh, STGE_StatisticsMask,
1546 (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
1547 (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
1548 (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
1549 (1U << 21));
1550
1551 /* Set up the receive filter. */
1552 stge_set_filter(sc);
1553
1554 /*
1555 * Give the transmit and receive ring to the chip.
1556 */
1557 bus_space_write_4(st, sh, STGE_TFDListPtrHi, 0); /* NOTE: 32-bit DMA */
1558 bus_space_write_4(st, sh, STGE_TFDListPtrLo,
1559 STGE_CDTXADDR(sc, sc->sc_txdirty));
1560
1561 bus_space_write_4(st, sh, STGE_RFDListPtrHi, 0); /* NOTE: 32-bit DMA */
1562 bus_space_write_4(st, sh, STGE_RFDListPtrLo,
1563 STGE_CDRXADDR(sc, sc->sc_rxptr));
1564
1565 /*
1566 * Initialize the Tx auto-poll period. It's OK to make this number
1567 * large (255 is the max, but we use 127) -- we explicitly kick the
1568 * transmit engine when there's actually a packet.
1569 */
1570 bus_space_write_1(st, sh, STGE_TxDMAPollPeriod, 127);
1571
1572 /* ..and the Rx auto-poll period. */
1573 bus_space_write_1(st, sh, STGE_RxDMAPollPeriod, 64);
1574
1575 /* Initialize the Tx start threshold. */
1576 bus_space_write_2(st, sh, STGE_TxStartThresh, sc->sc_txthresh);
1577
1578 /* RX DMA thresholds, from linux */
1579 bus_space_write_1(st, sh, STGE_RxDMABurstThresh, 0x30);
1580 bus_space_write_1(st, sh, STGE_RxDMAUrgentThresh, 0x30);
1581
1582 /*
1583 * Initialize the Rx DMA interrupt control register. We
1584 * request an interrupt after every incoming packet, but
1585 * defer it for 32us (64 * 512 ns). When the number of
1586 * interrupts pending reaches 8, we stop deferring the
1587 * interrupt, and signal it immediately.
1588 */
1589 bus_space_write_4(st, sh, STGE_RxDMAIntCtrl,
1590 RDIC_RxFrameCount(8) | RDIC_RxDMAWaitTime(512));
1591
1592 /*
1593 * Initialize the interrupt mask.
1594 */
1595 sc->sc_IntEnable = IS_HostError | IS_TxComplete | IS_UpdateStats |
1596 IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd;
1597 bus_space_write_2(st, sh, STGE_IntStatus, 0xffff);
1598 bus_space_write_2(st, sh, STGE_IntEnable, sc->sc_IntEnable);
1599
1600 /*
1601 * Configure the DMA engine.
1602 * XXX Should auto-tune TxBurstLimit.
1603 */
1604 bus_space_write_4(st, sh, STGE_DMACtrl, sc->sc_DMACtrl |
1605 DMAC_TxBurstLimit(3));
1606
1607 /*
1608 * Send a PAUSE frame when we reach 29,696 bytes in the Rx
1609 * FIFO, and send an un-PAUSE frame when the FIFO is totally
1610 * empty again.
1611 */
1612 bus_space_write_2(st, sh, STGE_FlowOnTresh, 29696 / 16);
1613 bus_space_write_2(st, sh, STGE_FlowOffThresh, 0);
1614
1615 /*
1616 * Set the maximum frame size.
1617 */
1618 bus_space_write_2(st, sh, STGE_MaxFrameSize,
1619 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1620 ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
1621 ETHER_VLAN_ENCAP_LEN : 0));
1622
1623 /*
1624 * Initialize MacCtrl -- do it before setting the media,
1625 * as setting the media will actually program the register.
1626 *
1627 * Note: We have to poke the IFS value before poking
1628 * anything else.
1629 */
1630 sc->sc_MACCtrl = MC_IFSSelect(0);
1631 bus_space_write_4(st, sh, STGE_MACCtrl, sc->sc_MACCtrl);
1632 sc->sc_MACCtrl |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
1633 #ifdef STGE_VLAN_UNTAG
1634 sc->sc_MACCtrl |= MC_AutoVLANuntagging;
1635 #endif
1636
1637 if (sc->sc_rev >= 6) { /* >= B.2 */
1638 /* Multi-frag frame bug work-around. */
1639 bus_space_write_2(st, sh, STGE_DebugCtrl,
1640 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0200);
1641
1642 /* Tx Poll Now bug work-around. */
1643 bus_space_write_2(st, sh, STGE_DebugCtrl,
1644 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0010);
1645 /* XXX ? from linux */
1646 bus_space_write_2(st, sh, STGE_DebugCtrl,
1647 bus_space_read_2(st, sh, STGE_DebugCtrl) | 0x0020);
1648 }
1649
1650 /*
1651 * Set the current media.
1652 */
1653 if ((error = ether_mediachange(ifp)) != 0)
1654 goto out;
1655
1656 /*
1657 * Start the one second MII clock.
1658 */
1659 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1660
1661 /*
1662 * ...all done!
1663 */
1664 ifp->if_flags |= IFF_RUNNING;
1665 ifp->if_flags &= ~IFF_OACTIVE;
1666
1667 out:
1668 if (error)
1669 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1670 return (error);
1671 }
1672
1673 /*
1674 * stge_drain:
1675 *
1676 * Drain the receive queue.
1677 */
1678 static void
1679 stge_rxdrain(struct stge_softc *sc)
1680 {
1681 struct stge_descsoft *ds;
1682 int i;
1683
1684 for (i = 0; i < STGE_NRXDESC; i++) {
1685 ds = &sc->sc_rxsoft[i];
1686 if (ds->ds_mbuf != NULL) {
1687 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1688 ds->ds_mbuf->m_next = NULL;
1689 m_freem(ds->ds_mbuf);
1690 ds->ds_mbuf = NULL;
1691 }
1692 }
1693 }
1694
1695 /*
1696 * stge_stop: [ ifnet interface function ]
1697 *
1698 * Stop transmission on the interface.
1699 */
1700 static void
1701 stge_stop(struct ifnet *ifp, int disable)
1702 {
1703 struct stge_softc *sc = ifp->if_softc;
1704 struct stge_descsoft *ds;
1705 int i;
1706
1707 /*
1708 * Stop the one second clock.
1709 */
1710 callout_stop(&sc->sc_tick_ch);
1711
1712 /* Down the MII. */
1713 mii_down(&sc->sc_mii);
1714
1715 /*
1716 * Disable interrupts.
1717 */
1718 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_IntEnable, 0);
1719
1720 /*
1721 * Stop receiver, transmitter, and stats update.
1722 */
1723 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl,
1724 MC_StatisticsDisable | MC_TxDisable | MC_RxDisable);
1725
1726 /*
1727 * Stop the transmit and receive DMA.
1728 */
1729 stge_dma_wait(sc);
1730 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrHi, 0);
1731 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_TFDListPtrLo, 0);
1732 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrHi, 0);
1733 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_RFDListPtrLo, 0);
1734
1735 /*
1736 * Release any queued transmit buffers.
1737 */
1738 for (i = 0; i < STGE_NTXDESC; i++) {
1739 ds = &sc->sc_txsoft[i];
1740 if (ds->ds_mbuf != NULL) {
1741 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1742 m_freem(ds->ds_mbuf);
1743 ds->ds_mbuf = NULL;
1744 }
1745 }
1746
1747 if (disable)
1748 stge_rxdrain(sc);
1749
1750 /*
1751 * Mark the interface down and cancel the watchdog timer.
1752 */
1753 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1754 ifp->if_timer = 0;
1755 }
1756
1757 static int
1758 stge_eeprom_wait(struct stge_softc *sc)
1759 {
1760 int i;
1761
1762 for (i = 0; i < STGE_TIMEOUT; i++) {
1763 delay(1000);
1764 if ((bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl) &
1765 EC_EepromBusy) == 0)
1766 return (0);
1767 }
1768 return (1);
1769 }
1770
1771 /*
1772 * stge_read_eeprom:
1773 *
1774 * Read data from the serial EEPROM.
1775 */
1776 static void
1777 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
1778 {
1779
1780 if (stge_eeprom_wait(sc))
1781 printf("%s: EEPROM failed to come ready\n",
1782 sc->sc_dev.dv_xname);
1783
1784 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_EepromCtrl,
1785 EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
1786 if (stge_eeprom_wait(sc))
1787 printf("%s: EEPROM read timed out\n",
1788 sc->sc_dev.dv_xname);
1789 *data = bus_space_read_2(sc->sc_st, sc->sc_sh, STGE_EepromData);
1790 }
1791
1792 /*
1793 * stge_add_rxbuf:
1794 *
1795 * Add a receive buffer to the indicated descriptor.
1796 */
1797 static int
1798 stge_add_rxbuf(struct stge_softc *sc, int idx)
1799 {
1800 struct stge_descsoft *ds = &sc->sc_rxsoft[idx];
1801 struct mbuf *m;
1802 int error;
1803
1804 MGETHDR(m, M_DONTWAIT, MT_DATA);
1805 if (m == NULL)
1806 return (ENOBUFS);
1807
1808 MCLGET(m, M_DONTWAIT);
1809 if ((m->m_flags & M_EXT) == 0) {
1810 m_freem(m);
1811 return (ENOBUFS);
1812 }
1813
1814 m->m_data = m->m_ext.ext_buf + 2;
1815 m->m_len = MCLBYTES - 2;
1816
1817 if (ds->ds_mbuf != NULL)
1818 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1819
1820 ds->ds_mbuf = m;
1821
1822 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1823 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1824 if (error) {
1825 printf("%s: can't load rx DMA map %d, error = %d\n",
1826 sc->sc_dev.dv_xname, idx, error);
1827 panic("stge_add_rxbuf"); /* XXX */
1828 }
1829
1830 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1831 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1832
1833 STGE_INIT_RXDESC(sc, idx);
1834
1835 return (0);
1836 }
1837
1838 /*
1839 * stge_set_filter:
1840 *
1841 * Set up the receive filter.
1842 */
1843 static void
1844 stge_set_filter(struct stge_softc *sc)
1845 {
1846 struct ethercom *ec = &sc->sc_ethercom;
1847 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1848 struct ether_multi *enm;
1849 struct ether_multistep step;
1850 uint32_t crc;
1851 uint32_t mchash[2];
1852
1853 sc->sc_ReceiveMode = RM_ReceiveUnicast;
1854 if (ifp->if_flags & IFF_BROADCAST)
1855 sc->sc_ReceiveMode |= RM_ReceiveBroadcast;
1856
1857 /* XXX: ST1023 only works in promiscuous mode */
1858 if (sc->sc_stge1023)
1859 ifp->if_flags |= IFF_PROMISC;
1860
1861 if (ifp->if_flags & IFF_PROMISC) {
1862 sc->sc_ReceiveMode |= RM_ReceiveAllFrames;
1863 goto allmulti;
1864 }
1865
1866 /*
1867 * Set up the multicast address filter by passing all multicast
1868 * addresses through a CRC generator, and then using the low-order
1869 * 6 bits as an index into the 64 bit multicast hash table. The
1870 * high order bits select the register, while the rest of the bits
1871 * select the bit within the register.
1872 */
1873
1874 memset(mchash, 0, sizeof(mchash));
1875
1876 ETHER_FIRST_MULTI(step, ec, enm);
1877 if (enm == NULL)
1878 goto done;
1879
1880 while (enm != NULL) {
1881 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1882 /*
1883 * We must listen to a range of multicast addresses.
1884 * For now, just accept all multicasts, rather than
1885 * trying to set only those filter bits needed to match
1886 * the range. (At this time, the only use of address
1887 * ranges is for IP multicast routing, for which the
1888 * range is big enough to require all bits set.)
1889 */
1890 goto allmulti;
1891 }
1892
1893 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1894
1895 /* Just want the 6 least significant bits. */
1896 crc &= 0x3f;
1897
1898 /* Set the corresponding bit in the hash table. */
1899 mchash[crc >> 5] |= 1 << (crc & 0x1f);
1900
1901 ETHER_NEXT_MULTI(step, enm);
1902 }
1903
1904 sc->sc_ReceiveMode |= RM_ReceiveMulticastHash;
1905
1906 ifp->if_flags &= ~IFF_ALLMULTI;
1907 goto done;
1908
1909 allmulti:
1910 ifp->if_flags |= IFF_ALLMULTI;
1911 sc->sc_ReceiveMode |= RM_ReceiveMulticast;
1912
1913 done:
1914 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1915 /*
1916 * Program the multicast hash table.
1917 */
1918 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable0,
1919 mchash[0]);
1920 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_HashTable1,
1921 mchash[1]);
1922 }
1923
1924 bus_space_write_2(sc->sc_st, sc->sc_sh, STGE_ReceiveMode,
1925 sc->sc_ReceiveMode);
1926 }
1927
1928 /*
1929 * stge_mii_readreg: [mii interface function]
1930 *
1931 * Read a PHY register on the MII of the TC9021.
1932 */
1933 static int
1934 stge_mii_readreg(struct device *self, int phy, int reg)
1935 {
1936
1937 return (mii_bitbang_readreg(self, &stge_mii_bitbang_ops, phy, reg));
1938 }
1939
1940 /*
1941 * stge_mii_writereg: [mii interface function]
1942 *
1943 * Write a PHY register on the MII of the TC9021.
1944 */
1945 static void
1946 stge_mii_writereg(struct device *self, int phy, int reg, int val)
1947 {
1948
1949 mii_bitbang_writereg(self, &stge_mii_bitbang_ops, phy, reg, val);
1950 }
1951
1952 /*
1953 * stge_mii_statchg: [mii interface function]
1954 *
1955 * Callback from MII layer when media changes.
1956 */
1957 static void
1958 stge_mii_statchg(struct device *self)
1959 {
1960 struct stge_softc *sc = (struct stge_softc *) self;
1961
1962 if (sc->sc_mii.mii_media_active & IFM_FDX)
1963 sc->sc_MACCtrl |= MC_DuplexSelect;
1964 else
1965 sc->sc_MACCtrl &= ~MC_DuplexSelect;
1966
1967 /* XXX 802.1x flow-control? */
1968
1969 bus_space_write_4(sc->sc_st, sc->sc_sh, STGE_MACCtrl, sc->sc_MACCtrl);
1970 }
1971
1972 /*
1973 * sste_mii_bitbang_read: [mii bit-bang interface function]
1974 *
1975 * Read the MII serial port for the MII bit-bang module.
1976 */
1977 static uint32_t
1978 stge_mii_bitbang_read(struct device *self)
1979 {
1980 struct stge_softc *sc = (void *) self;
1981
1982 return (bus_space_read_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl));
1983 }
1984
1985 /*
1986 * stge_mii_bitbang_write: [mii big-bang interface function]
1987 *
1988 * Write the MII serial port for the MII bit-bang module.
1989 */
1990 static void
1991 stge_mii_bitbang_write(struct device *self, uint32_t val)
1992 {
1993 struct stge_softc *sc = (void *) self;
1994
1995 bus_space_write_1(sc->sc_st, sc->sc_sh, STGE_PhyCtrl,
1996 val | sc->sc_PhyCtrl);
1997 }
1998