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