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