2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski
5 * Copyright (C) 2006-2007 Semihalf, Piotr Kruszynski
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Freescale integrated Three-Speed Ethernet Controller (TSEC) driver.
32 #include <sys/cdefs.h>
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_device_polling.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/endian.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/sysctl.h>
49 #include <net/ethernet.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56 #include <net/if_vlan_var.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
62 #include <machine/bus.h>
64 #include <dev/mii/mii.h>
65 #include <dev/mii/miivar.h>
67 #include <dev/tsec/if_tsec.h>
68 #include <dev/tsec/if_tsecreg.h>
70 static int tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag,
71 bus_dmamap_t *dmap, bus_size_t dsize, void **vaddr, void *raddr,
73 static void tsec_dma_ctl(struct tsec_softc *sc, int state);
74 static void tsec_encap(if_t ifp, struct tsec_softc *sc,
75 struct mbuf *m0, uint16_t fcb_flags, int *start_tx);
76 static void tsec_free_dma(struct tsec_softc *sc);
77 static void tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr);
78 static int tsec_ifmedia_upd(if_t ifp);
79 static void tsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr);
80 static int tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map,
81 struct mbuf **mbufp, uint32_t *paddr);
82 static void tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs,
84 static void tsec_intrs_ctl(struct tsec_softc *sc, int state);
85 static void tsec_init(void *xsc);
86 static void tsec_init_locked(struct tsec_softc *sc);
87 static int tsec_ioctl(if_t ifp, u_long command, caddr_t data);
88 static void tsec_reset_mac(struct tsec_softc *sc);
89 static void tsec_setfilter(struct tsec_softc *sc);
90 static void tsec_set_mac_address(struct tsec_softc *sc);
91 static void tsec_start(if_t ifp);
92 static void tsec_start_locked(if_t ifp);
93 static void tsec_stop(struct tsec_softc *sc);
94 static void tsec_tick(void *arg);
95 static void tsec_watchdog(struct tsec_softc *sc);
96 static void tsec_add_sysctls(struct tsec_softc *sc);
97 static int tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS);
98 static int tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS);
99 static void tsec_set_rxic(struct tsec_softc *sc);
100 static void tsec_set_txic(struct tsec_softc *sc);
101 static int tsec_receive_intr_locked(struct tsec_softc *sc, int count);
102 static void tsec_transmit_intr_locked(struct tsec_softc *sc);
103 static void tsec_error_intr_locked(struct tsec_softc *sc, int count);
104 static void tsec_offload_setup(struct tsec_softc *sc);
105 static void tsec_offload_process_frame(struct tsec_softc *sc,
107 static void tsec_setup_multicast(struct tsec_softc *sc);
108 static int tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu);
110 DRIVER_MODULE(miibus, tsec, miibus_driver, 0, 0);
111 MODULE_DEPEND(tsec, ether, 1, 1, 1);
112 MODULE_DEPEND(tsec, miibus, 1, 1, 1);
114 struct mtx tsec_phy_mtx;
117 tsec_attach(struct tsec_softc *sc)
119 uint8_t hwaddr[ETHER_ADDR_LEN];
124 /* Initialize global (because potentially shared) MII lock */
125 if (!mtx_initialized(&tsec_phy_mtx))
126 mtx_init(&tsec_phy_mtx, "tsec mii", NULL, MTX_DEF);
128 /* Reset all TSEC counters */
129 TSEC_TX_RX_COUNTERS_INIT(sc);
131 /* Stop DMA engine if enabled by firmware */
137 /* Disable interrupts for now */
138 tsec_intrs_ctl(sc, 0);
140 /* Configure defaults for interrupts coalescing */
141 sc->rx_ic_time = 768;
142 sc->rx_ic_count = 16;
143 sc->tx_ic_time = 768;
144 sc->tx_ic_count = 16;
147 tsec_add_sysctls(sc);
149 /* Allocate a busdma tag and DMA safe memory for TX descriptors. */
150 error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_tx_dtag,
151 &sc->tsec_tx_dmap, sizeof(*sc->tsec_tx_vaddr) * TSEC_TX_NUM_DESC,
152 (void **)&sc->tsec_tx_vaddr, &sc->tsec_tx_raddr, "TX");
159 /* Allocate a busdma tag and DMA safe memory for RX descriptors. */
160 error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_rx_dtag,
161 &sc->tsec_rx_dmap, sizeof(*sc->tsec_rx_vaddr) * TSEC_RX_NUM_DESC,
162 (void **)&sc->tsec_rx_vaddr, &sc->tsec_rx_raddr, "RX");
168 /* Allocate a busdma tag for TX mbufs. */
169 error = bus_dma_tag_create(NULL, /* parent */
170 TSEC_TXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
171 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
172 BUS_SPACE_MAXADDR, /* highaddr */
173 NULL, NULL, /* filtfunc, filtfuncarg */
174 MCLBYTES * (TSEC_TX_NUM_DESC - 1), /* maxsize */
175 TSEC_TX_MAX_DMA_SEGS, /* nsegments */
176 MCLBYTES, 0, /* maxsegsz, flags */
177 NULL, NULL, /* lockfunc, lockfuncarg */
178 &sc->tsec_tx_mtag); /* dmat */
180 device_printf(sc->dev, "failed to allocate busdma tag "
186 /* Allocate a busdma tag for RX mbufs. */
187 error = bus_dma_tag_create(NULL, /* parent */
188 TSEC_RXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
189 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
190 BUS_SPACE_MAXADDR, /* highaddr */
191 NULL, NULL, /* filtfunc, filtfuncarg */
192 MCLBYTES, /* maxsize */
194 MCLBYTES, 0, /* maxsegsz, flags */
195 NULL, NULL, /* lockfunc, lockfuncarg */
196 &sc->tsec_rx_mtag); /* dmat */
198 device_printf(sc->dev, "failed to allocate busdma tag "
204 /* Create TX busdma maps */
205 for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
206 error = bus_dmamap_create(sc->tsec_tx_mtag, 0,
207 &sc->tx_bufmap[i].map);
209 device_printf(sc->dev, "failed to init TX ring\n");
213 sc->tx_bufmap[i].map_initialized = 1;
216 /* Create RX busdma maps and zero mbuf handlers */
217 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
218 error = bus_dmamap_create(sc->tsec_rx_mtag, 0,
219 &sc->rx_data[i].map);
221 device_printf(sc->dev, "failed to init RX ring\n");
225 sc->rx_data[i].mbuf = NULL;
228 /* Create mbufs for RX buffers */
229 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
230 error = tsec_new_rxbuf(sc->tsec_rx_mtag, sc->rx_data[i].map,
231 &sc->rx_data[i].mbuf, &sc->rx_data[i].paddr);
233 device_printf(sc->dev, "can't load rx DMA map %d, "
234 "error = %d\n", i, error);
240 /* Create network interface for upper layers */
241 ifp = sc->tsec_ifp = if_alloc(IFT_ETHER);
243 device_printf(sc->dev, "if_alloc() failed\n");
248 if_setsoftc(ifp, sc);
249 if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
250 if_setflags(ifp, IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST);
251 if_setinitfn(ifp, tsec_init);
252 if_setstartfn(ifp, tsec_start);
253 if_setioctlfn(ifp, tsec_ioctl);
255 if_setsendqlen(ifp, TSEC_TX_NUM_DESC - 1);
256 if_setsendqready(ifp);
258 if_setcapabilities(ifp, IFCAP_VLAN_MTU);
260 if_setcapabilitiesbit(ifp, IFCAP_HWCSUM, 0);
262 if_setcapenable(ifp, if_getcapabilities(ifp));
264 #ifdef DEVICE_POLLING
265 /* Advertise that polling is supported */
266 if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
270 error = mii_attach(sc->dev, &sc->tsec_miibus, ifp, tsec_ifmedia_upd,
271 tsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->phyaddr, MII_OFFSET_ANY,
274 device_printf(sc->dev, "attaching PHYs failed\n");
280 sc->tsec_mii = device_get_softc(sc->tsec_miibus);
282 /* Set MAC address */
283 tsec_get_hwaddr(sc, hwaddr);
284 ether_ifattach(ifp, hwaddr);
290 tsec_detach(struct tsec_softc *sc)
293 if (sc->tsec_ifp != NULL) {
294 #ifdef DEVICE_POLLING
295 if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING)
296 ether_poll_deregister(sc->tsec_ifp);
299 /* Stop TSEC controller and free TX queue */
301 tsec_shutdown(sc->dev);
303 /* Detach network interface */
304 ether_ifdetach(sc->tsec_ifp);
305 if_free(sc->tsec_ifp);
309 /* Free DMA resources */
316 tsec_shutdown(device_t dev)
318 struct tsec_softc *sc;
320 sc = device_get_softc(dev);
322 TSEC_GLOBAL_LOCK(sc);
324 TSEC_GLOBAL_UNLOCK(sc);
329 tsec_suspend(device_t dev)
332 /* TODO not implemented! */
337 tsec_resume(device_t dev)
340 /* TODO not implemented! */
347 struct tsec_softc *sc = xsc;
349 TSEC_GLOBAL_LOCK(sc);
350 tsec_init_locked(sc);
351 TSEC_GLOBAL_UNLOCK(sc);
355 tsec_mii_wait(struct tsec_softc *sc, uint32_t flags)
360 * The status indicators are not set immediately after a command.
361 * Discard the first value.
363 TSEC_PHY_READ(sc, TSEC_REG_MIIMIND);
365 timeout = TSEC_READ_RETRY;
366 while ((TSEC_PHY_READ(sc, TSEC_REG_MIIMIND) & flags) && --timeout)
367 DELAY(TSEC_READ_DELAY);
369 return (timeout == 0);
373 tsec_init_locked(struct tsec_softc *sc)
375 struct tsec_desc *tx_desc = sc->tsec_tx_vaddr;
376 struct tsec_desc *rx_desc = sc->tsec_rx_vaddr;
377 if_t ifp = sc->tsec_ifp;
381 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
384 TSEC_GLOBAL_LOCK_ASSERT(sc);
388 * These steps are according to the MPC8555E PowerQUICCIII RM:
389 * 14.7 Initialization/Application Information
392 /* Step 1: soft reset MAC */
395 /* Step 2: Initialize MACCFG2 */
396 TSEC_WRITE(sc, TSEC_REG_MACCFG2,
397 TSEC_MACCFG2_FULLDUPLEX | /* Full Duplex = 1 */
398 TSEC_MACCFG2_PADCRC | /* PAD/CRC append */
399 TSEC_MACCFG2_GMII | /* I/F Mode bit */
400 TSEC_MACCFG2_PRECNT /* Preamble count = 7 */
403 /* Step 3: Initialize ECNTRL
404 * While the documentation states that R100M is ignored if RPM is
405 * not set, it does seem to be needed to get the orange boxes to
406 * work (which have a Marvell 88E1111 PHY). Go figure.
410 * XXX kludge - use circumstancial evidence to program ECNTRL
411 * correctly. Ideally we need some board information to guide
414 i = TSEC_READ(sc, TSEC_REG_ID2);
416 ? (TSEC_ECNTRL_TBIM | TSEC_ECNTRL_SGMIIM) /* Sumatra */
417 : TSEC_ECNTRL_R100M; /* Orange + CDS */
418 TSEC_WRITE(sc, TSEC_REG_ECNTRL, TSEC_ECNTRL_STEN | val);
420 /* Step 4: Initialize MAC station address */
421 tsec_set_mac_address(sc);
424 * Step 5: Assign a Physical address to the TBI so as to not conflict
425 * with the external PHY physical address
427 TSEC_WRITE(sc, TSEC_REG_TBIPA, 5);
431 /* Step 6: Reset the management interface */
432 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_RESETMGMT);
434 /* Step 7: Setup the MII Mgmt clock speed */
435 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_CLKDIV28);
437 /* Step 8: Read MII Mgmt indicator register and check for Busy = 0 */
438 timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY);
442 if_printf(ifp, "tsec_init_locked(): Mgmt busy timeout\n");
446 /* Step 9: Setup the MII Mgmt */
447 mii_mediachg(sc->tsec_mii);
449 /* Step 10: Clear IEVENT register */
450 TSEC_WRITE(sc, TSEC_REG_IEVENT, 0xffffffff);
452 /* Step 11: Enable interrupts */
453 #ifdef DEVICE_POLLING
455 * ...only if polling is not turned on. Disable interrupts explicitly
456 * if polling is enabled.
458 if (if_getcapenable(ifp) & IFCAP_POLLING )
459 tsec_intrs_ctl(sc, 0);
461 #endif /* DEVICE_POLLING */
462 tsec_intrs_ctl(sc, 1);
464 /* Step 12: Initialize IADDRn */
465 TSEC_WRITE(sc, TSEC_REG_IADDR0, 0);
466 TSEC_WRITE(sc, TSEC_REG_IADDR1, 0);
467 TSEC_WRITE(sc, TSEC_REG_IADDR2, 0);
468 TSEC_WRITE(sc, TSEC_REG_IADDR3, 0);
469 TSEC_WRITE(sc, TSEC_REG_IADDR4, 0);
470 TSEC_WRITE(sc, TSEC_REG_IADDR5, 0);
471 TSEC_WRITE(sc, TSEC_REG_IADDR6, 0);
472 TSEC_WRITE(sc, TSEC_REG_IADDR7, 0);
474 /* Step 13: Initialize GADDRn */
475 TSEC_WRITE(sc, TSEC_REG_GADDR0, 0);
476 TSEC_WRITE(sc, TSEC_REG_GADDR1, 0);
477 TSEC_WRITE(sc, TSEC_REG_GADDR2, 0);
478 TSEC_WRITE(sc, TSEC_REG_GADDR3, 0);
479 TSEC_WRITE(sc, TSEC_REG_GADDR4, 0);
480 TSEC_WRITE(sc, TSEC_REG_GADDR5, 0);
481 TSEC_WRITE(sc, TSEC_REG_GADDR6, 0);
482 TSEC_WRITE(sc, TSEC_REG_GADDR7, 0);
484 /* Step 14: Initialize RCTRL */
485 TSEC_WRITE(sc, TSEC_REG_RCTRL, 0);
487 /* Step 15: Initialize DMACTRL */
490 /* Step 16: Initialize FIFO_PAUSE_CTRL */
491 TSEC_WRITE(sc, TSEC_REG_FIFO_PAUSE_CTRL, TSEC_FIFO_PAUSE_CTRL_EN);
494 * Step 17: Initialize transmit/receive descriptor rings.
495 * Initialize TBASE and RBASE.
497 TSEC_WRITE(sc, TSEC_REG_TBASE, sc->tsec_tx_raddr);
498 TSEC_WRITE(sc, TSEC_REG_RBASE, sc->tsec_rx_raddr);
500 for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
501 tx_desc[i].bufptr = 0;
502 tx_desc[i].length = 0;
503 tx_desc[i].flags = ((i == TSEC_TX_NUM_DESC - 1) ?
506 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
507 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
509 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
510 rx_desc[i].bufptr = sc->rx_data[i].paddr;
511 rx_desc[i].length = 0;
512 rx_desc[i].flags = TSEC_RXBD_E | TSEC_RXBD_I |
513 ((i == TSEC_RX_NUM_DESC - 1) ? TSEC_RXBD_W : 0);
515 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
516 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
518 /* Step 18: Initialize the maximum receive buffer length */
519 TSEC_WRITE(sc, TSEC_REG_MRBLR, MCLBYTES);
521 /* Step 19: Configure ethernet frame sizes */
522 TSEC_WRITE(sc, TSEC_REG_MINFLR, TSEC_MIN_FRAME_SIZE);
523 tsec_set_mtu(sc, if_getmtu(ifp));
525 /* Step 20: Enable Rx and RxBD sdata snooping */
526 TSEC_WRITE(sc, TSEC_REG_ATTR, TSEC_ATTR_RDSEN | TSEC_ATTR_RBDSEN);
527 TSEC_WRITE(sc, TSEC_REG_ATTRELI, 0);
529 /* Step 21: Reset collision counters in hardware */
530 TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
531 TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
532 TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
533 TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
534 TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
536 /* Step 22: Mask all CAM interrupts */
537 TSEC_WRITE(sc, TSEC_REG_MON_CAM1, 0xffffffff);
538 TSEC_WRITE(sc, TSEC_REG_MON_CAM2, 0xffffffff);
540 /* Step 23: Enable Rx and Tx */
541 val = TSEC_READ(sc, TSEC_REG_MACCFG1);
542 val |= (TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
543 TSEC_WRITE(sc, TSEC_REG_MACCFG1, val);
545 /* Step 24: Reset TSEC counters for Tx and Rx rings */
546 TSEC_TX_RX_COUNTERS_INIT(sc);
548 /* Step 25: Setup TCP/IP Off-Load engine */
550 tsec_offload_setup(sc);
552 /* Step 26: Setup multicast filters */
553 tsec_setup_multicast(sc);
555 /* Step 27: Activate network interface */
556 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
557 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
558 sc->tsec_if_flags = if_getflags(ifp);
559 sc->tsec_watchdog = 0;
561 /* Schedule watchdog timeout */
562 callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
566 tsec_set_mac_address(struct tsec_softc *sc)
568 uint32_t macbuf[2] = { 0, 0 };
569 char *macbufp, *curmac;
572 TSEC_GLOBAL_LOCK_ASSERT(sc);
574 KASSERT((ETHER_ADDR_LEN <= sizeof(macbuf)),
575 ("tsec_set_mac_address: (%d <= %zd", ETHER_ADDR_LEN,
578 macbufp = (char *)macbuf;
579 curmac = (char *)if_getlladdr(sc->tsec_ifp);
581 /* Correct order of MAC address bytes */
582 for (i = 1; i <= ETHER_ADDR_LEN; i++)
583 macbufp[ETHER_ADDR_LEN-i] = curmac[i-1];
585 /* Initialize MAC station address MACSTNADDR2 and MACSTNADDR1 */
586 TSEC_WRITE(sc, TSEC_REG_MACSTNADDR2, macbuf[1]);
587 TSEC_WRITE(sc, TSEC_REG_MACSTNADDR1, macbuf[0]);
591 * DMA control function, if argument state is:
592 * 0 - DMA engine will be disabled
593 * 1 - DMA engine will be enabled
596 tsec_dma_ctl(struct tsec_softc *sc, int state)
599 uint32_t dma_flags, timeout;
603 dma_flags = TSEC_READ(sc, TSEC_REG_DMACTRL);
607 /* Temporarily clear stop graceful stop bits. */
608 tsec_dma_ctl(sc, 1000);
611 dma_flags |= (TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
615 /* Set write with response (WWR), wait (WOP) and snoop bits */
616 dma_flags |= (TSEC_DMACTRL_TDSEN | TSEC_DMACTRL_TBDSEN |
617 DMACTRL_WWR | DMACTRL_WOP);
619 /* Clear graceful stop bits */
620 dma_flags &= ~(TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
623 device_printf(dev, "tsec_dma_ctl(): unknown state value: %d\n",
627 TSEC_WRITE(sc, TSEC_REG_DMACTRL, dma_flags);
631 /* Wait for DMA stop */
632 timeout = TSEC_READ_RETRY;
633 while (--timeout && (!(TSEC_READ(sc, TSEC_REG_IEVENT) &
634 (TSEC_IEVENT_GRSC | TSEC_IEVENT_GTSC))))
635 DELAY(TSEC_READ_DELAY);
638 device_printf(dev, "tsec_dma_ctl(): timeout!\n");
641 /* Restart transmission function */
642 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
647 * Interrupts control function, if argument state is:
648 * 0 - all TSEC interrupts will be masked
649 * 1 - all TSEC interrupts will be unmasked
652 tsec_intrs_ctl(struct tsec_softc *sc, int state)
660 TSEC_WRITE(sc, TSEC_REG_IMASK, 0);
663 TSEC_WRITE(sc, TSEC_REG_IMASK, TSEC_IMASK_BREN |
664 TSEC_IMASK_RXCEN | TSEC_IMASK_BSYEN | TSEC_IMASK_EBERREN |
665 TSEC_IMASK_BTEN | TSEC_IMASK_TXEEN | TSEC_IMASK_TXBEN |
666 TSEC_IMASK_TXFEN | TSEC_IMASK_XFUNEN | TSEC_IMASK_RXFEN);
669 device_printf(dev, "tsec_intrs_ctl(): unknown state value: %d\n",
675 tsec_reset_mac(struct tsec_softc *sc)
677 uint32_t maccfg1_flags;
679 /* Set soft reset bit */
680 maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
681 maccfg1_flags |= TSEC_MACCFG1_SOFT_RESET;
682 TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
684 /* Clear soft reset bit */
685 maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
686 maccfg1_flags &= ~TSEC_MACCFG1_SOFT_RESET;
687 TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
691 tsec_watchdog(struct tsec_softc *sc)
695 TSEC_GLOBAL_LOCK_ASSERT(sc);
697 if (sc->tsec_watchdog == 0 || --sc->tsec_watchdog > 0)
701 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
702 if_printf(ifp, "watchdog timeout\n");
705 tsec_init_locked(sc);
711 struct tsec_softc *sc = if_getsoftc(ifp);
713 TSEC_TRANSMIT_LOCK(sc);
714 tsec_start_locked(ifp);
715 TSEC_TRANSMIT_UNLOCK(sc);
719 tsec_start_locked(if_t ifp)
721 struct tsec_softc *sc;
723 struct tsec_tx_fcb *tx_fcb;
728 sc = if_getsoftc(ifp);
731 TSEC_TRANSMIT_LOCK_ASSERT(sc);
733 if (sc->tsec_link == 0)
736 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
737 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
740 if (TSEC_FREE_TX_DESC(sc) < TSEC_TX_MAX_DMA_SEGS) {
741 /* No free descriptors */
742 if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
746 /* Get packet from the queue */
747 m0 = if_dequeue(ifp);
751 /* Insert TCP/IP Off-load frame control block */
753 csum_flags = m0->m_pkthdr.csum_flags;
755 M_PREPEND(m0, sizeof(struct tsec_tx_fcb), M_NOWAIT);
759 if (csum_flags & CSUM_IP)
760 fcb_flags |= TSEC_TX_FCB_IP4 |
763 if (csum_flags & CSUM_TCP)
764 fcb_flags |= TSEC_TX_FCB_TCP |
765 TSEC_TX_FCB_CSUM_TCP_UDP;
767 if (csum_flags & CSUM_UDP)
768 fcb_flags |= TSEC_TX_FCB_UDP |
769 TSEC_TX_FCB_CSUM_TCP_UDP;
771 tx_fcb = mtod(m0, struct tsec_tx_fcb *);
772 tx_fcb->flags = fcb_flags;
773 tx_fcb->l3_offset = ETHER_HDR_LEN;
774 tx_fcb->l4_offset = sizeof(struct ip);
777 tsec_encap(ifp, sc, m0, fcb_flags, &start_tx);
779 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
780 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
783 /* Enable transmitter and watchdog timer */
784 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
785 sc->tsec_watchdog = 5;
790 tsec_encap(if_t ifp, struct tsec_softc *sc, struct mbuf *m0,
791 uint16_t fcb_flags, int *start_tx)
793 bus_dma_segment_t segs[TSEC_TX_MAX_DMA_SEGS];
795 struct tsec_bufmap *tx_bufmap;
799 TSEC_TRANSMIT_LOCK_ASSERT(sc);
801 tx_idx = sc->tx_idx_head;
802 tx_bufmap = &sc->tx_bufmap[tx_idx];
804 /* Create mapping in DMA memory */
805 error = bus_dmamap_load_mbuf_sg(sc->tsec_tx_mtag, tx_bufmap->map, m0,
806 segs, &nsegs, BUS_DMA_NOWAIT);
807 if (error == EFBIG) {
808 /* Too many segments! Defrag and try again. */
809 struct mbuf *m = m_defrag(m0, M_NOWAIT);
816 error = bus_dmamap_load_mbuf_sg(sc->tsec_tx_mtag,
817 tx_bufmap->map, m0, segs, &nsegs, BUS_DMA_NOWAIT);
825 bus_dmamap_sync(sc->tsec_tx_mtag, tx_bufmap->map,
826 BUS_DMASYNC_PREWRITE);
827 tx_bufmap->mbuf = m0;
830 * Fill in the TX descriptors back to front so that READY bit in first
831 * descriptor is set last.
833 tx_idx = (tx_idx + (uint32_t)nsegs) & (TSEC_TX_NUM_DESC - 1);
834 sc->tx_idx_head = tx_idx;
835 flags = TSEC_TXBD_L | TSEC_TXBD_I | TSEC_TXBD_R | TSEC_TXBD_TC;
836 for (i = nsegs - 1; i >= 0; i--) {
837 struct tsec_desc *tx_desc;
839 tx_idx = (tx_idx - 1) & (TSEC_TX_NUM_DESC - 1);
840 tx_desc = &sc->tsec_tx_vaddr[tx_idx];
841 tx_desc->length = segs[i].ds_len;
842 tx_desc->bufptr = segs[i].ds_addr;
848 flags |= TSEC_TXBD_TOE;
856 * - transmit the CRC sequence after the last data byte
857 * - interrupt after the last buffer
859 tx_desc->flags = (tx_idx == (TSEC_TX_NUM_DESC - 1) ?
860 TSEC_TXBD_W : 0) | flags;
862 flags &= ~(TSEC_TXBD_L | TSEC_TXBD_I);
870 tsec_setfilter(struct tsec_softc *sc)
876 flags = TSEC_READ(sc, TSEC_REG_RCTRL);
878 /* Promiscuous mode */
879 if (if_getflags(ifp) & IFF_PROMISC)
880 flags |= TSEC_RCTRL_PROM;
882 flags &= ~TSEC_RCTRL_PROM;
884 TSEC_WRITE(sc, TSEC_REG_RCTRL, flags);
887 #ifdef DEVICE_POLLING
888 static poll_handler_t tsec_poll;
891 tsec_poll(if_t ifp, enum poll_cmd cmd, int count)
894 struct tsec_softc *sc = if_getsoftc(ifp);
899 TSEC_GLOBAL_LOCK(sc);
900 if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
901 TSEC_GLOBAL_UNLOCK(sc);
905 if (cmd == POLL_AND_CHECK_STATUS) {
906 tsec_error_intr_locked(sc, count);
908 /* Clear all events reported */
909 ie = TSEC_READ(sc, TSEC_REG_IEVENT);
910 TSEC_WRITE(sc, TSEC_REG_IEVENT, ie);
913 tsec_transmit_intr_locked(sc);
915 TSEC_GLOBAL_TO_RECEIVE_LOCK(sc);
917 rx_npkts = tsec_receive_intr_locked(sc, count);
919 TSEC_RECEIVE_UNLOCK(sc);
923 #endif /* DEVICE_POLLING */
926 tsec_ioctl(if_t ifp, u_long command, caddr_t data)
928 struct tsec_softc *sc = if_getsoftc(ifp);
929 struct ifreq *ifr = (struct ifreq *)data;
934 TSEC_GLOBAL_LOCK(sc);
935 if (tsec_set_mtu(sc, ifr->ifr_mtu))
936 if_setmtu(ifp, ifr->ifr_mtu);
939 TSEC_GLOBAL_UNLOCK(sc);
942 TSEC_GLOBAL_LOCK(sc);
943 if (if_getflags(ifp) & IFF_UP) {
944 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
945 if ((sc->tsec_if_flags ^ if_getflags(ifp)) &
949 if ((sc->tsec_if_flags ^ if_getflags(ifp)) &
951 tsec_setup_multicast(sc);
953 tsec_init_locked(sc);
954 } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
957 sc->tsec_if_flags = if_getflags(ifp);
958 TSEC_GLOBAL_UNLOCK(sc);
962 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
963 TSEC_GLOBAL_LOCK(sc);
964 tsec_setup_multicast(sc);
965 TSEC_GLOBAL_UNLOCK(sc);
969 error = ifmedia_ioctl(ifp, ifr, &sc->tsec_mii->mii_media,
973 mask = if_getcapenable(ifp) ^ ifr->ifr_reqcap;
974 if ((mask & IFCAP_HWCSUM) && sc->is_etsec) {
975 TSEC_GLOBAL_LOCK(sc);
976 if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
977 if_setcapenablebit(ifp, IFCAP_HWCSUM & ifr->ifr_reqcap, 0);
978 tsec_offload_setup(sc);
979 TSEC_GLOBAL_UNLOCK(sc);
981 #ifdef DEVICE_POLLING
982 if (mask & IFCAP_POLLING) {
983 if (ifr->ifr_reqcap & IFCAP_POLLING) {
984 error = ether_poll_register(tsec_poll, ifp);
988 TSEC_GLOBAL_LOCK(sc);
989 /* Disable interrupts */
990 tsec_intrs_ctl(sc, 0);
991 if_setcapenablebit(ifp, IFCAP_POLLING, 0);
992 TSEC_GLOBAL_UNLOCK(sc);
994 error = ether_poll_deregister(ifp);
995 TSEC_GLOBAL_LOCK(sc);
996 /* Enable interrupts */
997 tsec_intrs_ctl(sc, 1);
998 if_setcapenablebit(ifp, 0, IFCAP_POLLING);
999 TSEC_GLOBAL_UNLOCK(sc);
1006 error = ether_ioctl(ifp, command, data);
1009 /* Flush buffers if not empty */
1010 if (if_getflags(ifp) & IFF_UP)
1016 tsec_ifmedia_upd(if_t ifp)
1018 struct tsec_softc *sc = if_getsoftc(ifp);
1019 struct mii_data *mii;
1021 TSEC_TRANSMIT_LOCK(sc);
1026 TSEC_TRANSMIT_UNLOCK(sc);
1031 tsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
1033 struct tsec_softc *sc = if_getsoftc(ifp);
1034 struct mii_data *mii;
1036 TSEC_TRANSMIT_LOCK(sc);
1041 ifmr->ifm_active = mii->mii_media_active;
1042 ifmr->ifm_status = mii->mii_media_status;
1044 TSEC_TRANSMIT_UNLOCK(sc);
1048 tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map, struct mbuf **mbufp,
1051 struct mbuf *new_mbuf;
1052 bus_dma_segment_t seg[1];
1055 KASSERT(mbufp != NULL, ("NULL mbuf pointer!"));
1057 new_mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MCLBYTES);
1058 if (new_mbuf == NULL)
1060 new_mbuf->m_len = new_mbuf->m_pkthdr.len = new_mbuf->m_ext.ext_size;
1063 bus_dmamap_sync(tag, map, BUS_DMASYNC_POSTREAD);
1064 bus_dmamap_unload(tag, map);
1067 error = bus_dmamap_load_mbuf_sg(tag, map, new_mbuf, seg, &nsegs,
1069 KASSERT(nsegs == 1, ("Too many segments returned!"));
1070 if (nsegs != 1 || error)
1071 panic("tsec_new_rxbuf(): nsegs(%d), error(%d)", nsegs, error);
1075 printf("tsec: bus_dmamap_load_mbuf_sg() returned: %d!\n",
1083 KASSERT(((seg->ds_addr) & (TSEC_RXBUFFER_ALIGNMENT-1)) == 0,
1084 ("Wrong alignment of RX buffer!"));
1086 bus_dmamap_sync(tag, map, BUS_DMASYNC_PREREAD);
1088 (*mbufp) = new_mbuf;
1089 (*paddr) = seg->ds_addr;
1094 tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1098 KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
1100 *paddr = segs->ds_addr;
1104 tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag, bus_dmamap_t *dmap,
1105 bus_size_t dsize, void **vaddr, void *raddr, const char *dname)
1109 /* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
1110 error = bus_dma_tag_create(NULL, /* parent */
1111 PAGE_SIZE, 0, /* alignment, boundary */
1112 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1113 BUS_SPACE_MAXADDR, /* highaddr */
1114 NULL, NULL, /* filtfunc, filtfuncarg */
1115 dsize, 1, /* maxsize, nsegments */
1116 dsize, 0, /* maxsegsz, flags */
1117 NULL, NULL, /* lockfunc, lockfuncarg */
1121 device_printf(dev, "failed to allocate busdma %s tag\n",
1127 error = bus_dmamem_alloc(*dtag, vaddr, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1130 device_printf(dev, "failed to allocate %s DMA safe memory\n",
1132 bus_dma_tag_destroy(*dtag);
1137 error = bus_dmamap_load(*dtag, *dmap, *vaddr, dsize,
1138 tsec_map_dma_addr, raddr, BUS_DMA_NOWAIT);
1140 device_printf(dev, "cannot get address of the %s "
1141 "descriptors\n", dname);
1142 bus_dmamem_free(*dtag, *vaddr, *dmap);
1143 bus_dma_tag_destroy(*dtag);
1152 tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr)
1158 /* Unmap descriptors from DMA memory */
1159 bus_dmamap_sync(dtag, dmap, BUS_DMASYNC_POSTREAD |
1160 BUS_DMASYNC_POSTWRITE);
1161 bus_dmamap_unload(dtag, dmap);
1163 /* Free descriptors memory */
1164 bus_dmamem_free(dtag, vaddr, dmap);
1166 /* Destroy descriptors tag */
1167 bus_dma_tag_destroy(dtag);
1171 tsec_free_dma(struct tsec_softc *sc)
1176 for (i = 0; i < TSEC_TX_NUM_DESC; i++)
1177 if (sc->tx_bufmap[i].map_initialized)
1178 bus_dmamap_destroy(sc->tsec_tx_mtag,
1179 sc->tx_bufmap[i].map);
1180 /* Destroy tag for TX mbufs */
1181 bus_dma_tag_destroy(sc->tsec_tx_mtag);
1183 /* Free RX mbufs and maps */
1184 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
1185 if (sc->rx_data[i].mbuf) {
1186 /* Unload buffer from DMA */
1187 bus_dmamap_sync(sc->tsec_rx_mtag, sc->rx_data[i].map,
1188 BUS_DMASYNC_POSTREAD);
1189 bus_dmamap_unload(sc->tsec_rx_mtag,
1190 sc->rx_data[i].map);
1193 m_freem(sc->rx_data[i].mbuf);
1195 /* Destroy map for this buffer */
1196 if (sc->rx_data[i].map != NULL)
1197 bus_dmamap_destroy(sc->tsec_rx_mtag,
1198 sc->rx_data[i].map);
1200 /* Destroy tag for RX mbufs */
1201 bus_dma_tag_destroy(sc->tsec_rx_mtag);
1203 /* Unload TX/RX descriptors */
1204 tsec_free_dma_desc(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1206 tsec_free_dma_desc(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1211 tsec_stop(struct tsec_softc *sc)
1216 TSEC_GLOBAL_LOCK_ASSERT(sc);
1220 /* Disable interface and watchdog timer */
1221 callout_stop(&sc->tsec_callout);
1222 if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1223 sc->tsec_watchdog = 0;
1225 /* Disable all interrupts and stop DMA */
1226 tsec_intrs_ctl(sc, 0);
1227 tsec_dma_ctl(sc, 0);
1229 /* Remove pending data from TX queue */
1230 while (sc->tx_idx_tail != sc->tx_idx_head) {
1231 bus_dmamap_sync(sc->tsec_tx_mtag,
1232 sc->tx_bufmap[sc->tx_idx_tail].map,
1233 BUS_DMASYNC_POSTWRITE);
1234 bus_dmamap_unload(sc->tsec_tx_mtag,
1235 sc->tx_bufmap[sc->tx_idx_tail].map);
1236 m_freem(sc->tx_bufmap[sc->tx_idx_tail].mbuf);
1237 sc->tx_idx_tail = (sc->tx_idx_tail + 1)
1238 & (TSEC_TX_NUM_DESC - 1);
1241 /* Disable RX and TX */
1242 tmpval = TSEC_READ(sc, TSEC_REG_MACCFG1);
1243 tmpval &= ~(TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
1244 TSEC_WRITE(sc, TSEC_REG_MACCFG1, tmpval);
1249 tsec_tick(void *arg)
1251 struct tsec_softc *sc = arg;
1255 TSEC_GLOBAL_LOCK(sc);
1260 link = sc->tsec_link;
1262 mii_tick(sc->tsec_mii);
1264 if (link == 0 && sc->tsec_link == 1 &&
1265 (!if_sendq_empty(ifp)))
1266 tsec_start_locked(ifp);
1268 /* Schedule another timeout one second from now. */
1269 callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
1271 TSEC_GLOBAL_UNLOCK(sc);
1275 * This is the core RX routine. It replenishes mbufs in the descriptor and
1276 * sends data which have been dma'ed into host memory to upper layer.
1278 * Loops at most count times if count is > 0, or until done if count < 0.
1281 tsec_receive_intr_locked(struct tsec_softc *sc, int count)
1283 struct tsec_desc *rx_desc;
1285 struct rx_data_type *rx_data;
1291 TSEC_RECEIVE_LOCK_ASSERT(sc);
1294 rx_data = sc->rx_data;
1297 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1298 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1300 for (c = 0; ; c++) {
1301 if (count >= 0 && count-- == 0)
1304 rx_desc = TSEC_GET_CUR_RX_DESC(sc);
1305 flags = rx_desc->flags;
1307 /* Check if there is anything to receive */
1308 if ((flags & TSEC_RXBD_E) || (c >= TSEC_RX_NUM_DESC)) {
1310 * Avoid generating another interrupt
1312 if (flags & TSEC_RXBD_E)
1313 TSEC_WRITE(sc, TSEC_REG_IEVENT,
1314 TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
1316 * We didn't consume current descriptor and have to
1317 * return it to the queue
1319 TSEC_BACK_CUR_RX_DESC(sc);
1323 if (flags & (TSEC_RXBD_LG | TSEC_RXBD_SH | TSEC_RXBD_NO |
1324 TSEC_RXBD_CR | TSEC_RXBD_OV | TSEC_RXBD_TR)) {
1325 rx_desc->length = 0;
1326 rx_desc->flags = (rx_desc->flags &
1327 ~TSEC_RXBD_ZEROONINIT) | TSEC_RXBD_E | TSEC_RXBD_I;
1329 if (sc->frame != NULL) {
1337 /* Ok... process frame */
1338 i = TSEC_GET_CUR_RX_DESC_CNT(sc);
1339 m = rx_data[i].mbuf;
1340 m->m_len = rx_desc->length;
1342 if (sc->frame != NULL) {
1343 if ((flags & TSEC_RXBD_L) != 0)
1344 m->m_len -= m_length(sc->frame, NULL);
1346 m->m_flags &= ~M_PKTHDR;
1347 m_cat(sc->frame, m);
1354 if ((flags & TSEC_RXBD_L) != 0) {
1359 if (tsec_new_rxbuf(sc->tsec_rx_mtag, rx_data[i].map,
1360 &rx_data[i].mbuf, &rx_data[i].paddr)) {
1361 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1363 * We ran out of mbufs; didn't consume current
1364 * descriptor and have to return it to the queue.
1366 TSEC_BACK_CUR_RX_DESC(sc);
1370 /* Attach new buffer to descriptor and clear flags */
1371 rx_desc->bufptr = rx_data[i].paddr;
1372 rx_desc->length = 0;
1373 rx_desc->flags = (rx_desc->flags & ~TSEC_RXBD_ZEROONINIT) |
1374 TSEC_RXBD_E | TSEC_RXBD_I;
1377 m->m_pkthdr.rcvif = ifp;
1380 m_adj(m, -ETHER_CRC_LEN);
1383 tsec_offload_process_frame(sc, m);
1385 TSEC_RECEIVE_UNLOCK(sc);
1387 TSEC_RECEIVE_LOCK(sc);
1392 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1393 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1396 * Make sure TSEC receiver is not halted.
1398 * Various conditions can stop the TSEC receiver, but not all are
1399 * signaled and handled by error interrupt, so make sure the receiver
1400 * is running. Writing to TSEC_REG_RSTAT restarts the receiver when
1401 * halted, and is harmless if already running.
1403 TSEC_WRITE(sc, TSEC_REG_RSTAT, TSEC_RSTAT_QHLT);
1408 tsec_receive_intr(void *arg)
1410 struct tsec_softc *sc = arg;
1412 TSEC_RECEIVE_LOCK(sc);
1414 #ifdef DEVICE_POLLING
1415 if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING) {
1416 TSEC_RECEIVE_UNLOCK(sc);
1421 /* Confirm the interrupt was received by driver */
1422 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
1423 tsec_receive_intr_locked(sc, -1);
1425 TSEC_RECEIVE_UNLOCK(sc);
1429 tsec_transmit_intr_locked(struct tsec_softc *sc)
1434 TSEC_TRANSMIT_LOCK_ASSERT(sc);
1438 /* Update collision statistics */
1439 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, TSEC_READ(sc, TSEC_REG_MON_TNCL));
1441 /* Reset collision counters in hardware */
1442 TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
1443 TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
1444 TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
1445 TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
1446 TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
1448 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1449 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1451 tx_idx = sc->tx_idx_tail;
1452 while (tx_idx != sc->tx_idx_head) {
1453 struct tsec_desc *tx_desc;
1454 struct tsec_bufmap *tx_bufmap;
1456 tx_desc = &sc->tsec_tx_vaddr[tx_idx];
1457 if (tx_desc->flags & TSEC_TXBD_R) {
1461 tx_bufmap = &sc->tx_bufmap[tx_idx];
1462 tx_idx = (tx_idx + 1) & (TSEC_TX_NUM_DESC - 1);
1463 if (tx_bufmap->mbuf == NULL)
1467 * This is the last buf in this packet, so unmap and free it.
1469 bus_dmamap_sync(sc->tsec_tx_mtag, tx_bufmap->map,
1470 BUS_DMASYNC_POSTWRITE);
1471 bus_dmamap_unload(sc->tsec_tx_mtag, tx_bufmap->map);
1472 m_freem(tx_bufmap->mbuf);
1473 tx_bufmap->mbuf = NULL;
1475 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1477 sc->tx_idx_tail = tx_idx;
1478 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1479 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1481 if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1482 tsec_start_locked(ifp);
1484 if (sc->tx_idx_tail == sc->tx_idx_head)
1485 sc->tsec_watchdog = 0;
1489 tsec_transmit_intr(void *arg)
1491 struct tsec_softc *sc = arg;
1493 TSEC_TRANSMIT_LOCK(sc);
1495 #ifdef DEVICE_POLLING
1496 if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING) {
1497 TSEC_TRANSMIT_UNLOCK(sc);
1501 /* Confirm the interrupt was received by driver */
1502 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_TXB | TSEC_IEVENT_TXF);
1503 tsec_transmit_intr_locked(sc);
1505 TSEC_TRANSMIT_UNLOCK(sc);
1509 tsec_error_intr_locked(struct tsec_softc *sc, int count)
1514 TSEC_GLOBAL_LOCK_ASSERT(sc);
1518 eflags = TSEC_READ(sc, TSEC_REG_IEVENT);
1520 /* Clear events bits in hardware */
1521 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXC | TSEC_IEVENT_BSY |
1522 TSEC_IEVENT_EBERR | TSEC_IEVENT_MSRO | TSEC_IEVENT_BABT |
1523 TSEC_IEVENT_TXC | TSEC_IEVENT_TXE | TSEC_IEVENT_LC |
1524 TSEC_IEVENT_CRL | TSEC_IEVENT_XFUN);
1526 /* Check transmitter errors */
1527 if (eflags & TSEC_IEVENT_TXE) {
1528 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1530 if (eflags & TSEC_IEVENT_LC)
1531 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1533 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
1536 /* Check for discarded frame due to a lack of buffers */
1537 if (eflags & TSEC_IEVENT_BSY) {
1538 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1541 if (if_getflags(ifp) & IFF_DEBUG)
1542 if_printf(ifp, "tsec_error_intr(): event flags: 0x%x\n",
1545 if (eflags & TSEC_IEVENT_EBERR) {
1546 if_printf(ifp, "System bus error occurred during"
1547 "DMA transaction (flags: 0x%x)\n", eflags);
1548 tsec_init_locked(sc);
1551 if (eflags & TSEC_IEVENT_BABT)
1552 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1554 if (eflags & TSEC_IEVENT_BABR)
1555 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1559 tsec_error_intr(void *arg)
1561 struct tsec_softc *sc = arg;
1563 TSEC_GLOBAL_LOCK(sc);
1564 tsec_error_intr_locked(sc, -1);
1565 TSEC_GLOBAL_UNLOCK(sc);
1569 tsec_miibus_readreg(device_t dev, int phy, int reg)
1571 struct tsec_softc *sc;
1575 sc = device_get_softc(dev);
1578 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1579 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, 0);
1580 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, TSEC_MIIMCOM_READCYCLE);
1582 timeout = tsec_mii_wait(sc, TSEC_MIIMIND_NOTVALID | TSEC_MIIMIND_BUSY);
1583 rv = TSEC_PHY_READ(sc, TSEC_REG_MIIMSTAT);
1587 device_printf(dev, "Timeout while reading from PHY!\n");
1593 tsec_miibus_writereg(device_t dev, int phy, int reg, int value)
1595 struct tsec_softc *sc;
1598 sc = device_get_softc(dev);
1601 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1602 TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCON, value);
1603 timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY);
1607 device_printf(dev, "Timeout while writing to PHY!\n");
1613 tsec_miibus_statchg(device_t dev)
1615 struct tsec_softc *sc;
1616 struct mii_data *mii;
1617 uint32_t ecntrl, id, tmp;
1620 sc = device_get_softc(dev);
1622 link = ((mii->mii_media_status & IFM_ACTIVE) ? 1 : 0);
1624 tmp = TSEC_READ(sc, TSEC_REG_MACCFG2) & ~TSEC_MACCFG2_IF;
1626 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
1627 tmp |= TSEC_MACCFG2_FULLDUPLEX;
1629 tmp &= ~TSEC_MACCFG2_FULLDUPLEX;
1631 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1634 tmp |= TSEC_MACCFG2_GMII;
1635 sc->tsec_link = link;
1639 tmp |= TSEC_MACCFG2_MII;
1640 sc->tsec_link = link;
1644 device_printf(dev, "No speed selected but link "
1650 device_printf(dev, "Unknown speed (%d), link %s!\n",
1651 IFM_SUBTYPE(mii->mii_media_active),
1652 ((link) ? "up" : "down"));
1655 TSEC_WRITE(sc, TSEC_REG_MACCFG2, tmp);
1657 /* XXX kludge - use circumstantial evidence for reduced mode. */
1658 id = TSEC_READ(sc, TSEC_REG_ID2);
1660 ecntrl = TSEC_READ(sc, TSEC_REG_ECNTRL) & ~TSEC_ECNTRL_R100M;
1661 ecntrl |= (tmp & TSEC_MACCFG2_MII) ? TSEC_ECNTRL_R100M : 0;
1662 TSEC_WRITE(sc, TSEC_REG_ECNTRL, ecntrl);
1667 tsec_add_sysctls(struct tsec_softc *sc)
1669 struct sysctl_ctx_list *ctx;
1670 struct sysctl_oid_list *children;
1671 struct sysctl_oid *tree;
1673 ctx = device_get_sysctl_ctx(sc->dev);
1674 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
1675 tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "int_coal",
1676 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "TSEC Interrupts coalescing");
1677 children = SYSCTL_CHILDREN(tree);
1679 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_time",
1680 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_RX,
1681 tsec_sysctl_ic_time, "I", "IC RX time threshold (0-65535)");
1682 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_count",
1683 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_RX,
1684 tsec_sysctl_ic_count, "I", "IC RX frame count threshold (0-255)");
1686 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_time",
1687 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_TX,
1688 tsec_sysctl_ic_time, "I", "IC TX time threshold (0-65535)");
1689 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_count",
1690 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_TX,
1691 tsec_sysctl_ic_count, "I", "IC TX frame count threshold (0-255)");
1695 * With Interrupt Coalescing (IC) active, a transmit/receive frame
1696 * interrupt is raised either upon:
1698 * - threshold-defined period of time elapsed, or
1699 * - threshold-defined number of frames is received/transmitted,
1700 * whichever occurs first.
1702 * The following sysctls regulate IC behaviour (for TX/RX separately):
1704 * dev.tsec.<unit>.int_coal.rx_time
1705 * dev.tsec.<unit>.int_coal.rx_count
1706 * dev.tsec.<unit>.int_coal.tx_time
1707 * dev.tsec.<unit>.int_coal.tx_count
1711 * - 0 for either time or count disables IC on the given TX/RX path
1713 * - count: 1-255 (expresses frame count number; note that value of 1 is
1714 * effectively IC off)
1716 * - time: 1-65535 (value corresponds to a real time period and is
1717 * expressed in units equivalent to 64 TSEC interface clocks, i.e. one timer
1718 * threshold unit is 26.5 us, 2.56 us, or 512 ns, corresponding to 10 Mbps,
1719 * 100 Mbps, or 1Gbps, respectively. For detailed discussion consult the
1720 * TSEC reference manual.
1723 tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS)
1727 struct tsec_softc *sc = (struct tsec_softc *)arg1;
1729 time = (arg2 == TSEC_IC_RX) ? sc->rx_ic_time : sc->tx_ic_time;
1731 error = sysctl_handle_int(oidp, &time, 0, req);
1739 if (arg2 == TSEC_IC_RX) {
1740 sc->rx_ic_time = time;
1743 sc->tx_ic_time = time;
1752 tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS)
1756 struct tsec_softc *sc = (struct tsec_softc *)arg1;
1758 count = (arg2 == TSEC_IC_RX) ? sc->rx_ic_count : sc->tx_ic_count;
1760 error = sysctl_handle_int(oidp, &count, 0, req);
1768 if (arg2 == TSEC_IC_RX) {
1769 sc->rx_ic_count = count;
1772 sc->tx_ic_count = count;
1781 tsec_set_rxic(struct tsec_softc *sc)
1785 if (sc->rx_ic_count == 0 || sc->rx_ic_time == 0)
1789 rxic_val = 0x80000000;
1790 rxic_val |= (sc->rx_ic_count << 21);
1791 rxic_val |= sc->rx_ic_time;
1794 TSEC_WRITE(sc, TSEC_REG_RXIC, rxic_val);
1798 tsec_set_txic(struct tsec_softc *sc)
1802 if (sc->tx_ic_count == 0 || sc->tx_ic_time == 0)
1806 txic_val = 0x80000000;
1807 txic_val |= (sc->tx_ic_count << 21);
1808 txic_val |= sc->tx_ic_time;
1811 TSEC_WRITE(sc, TSEC_REG_TXIC, txic_val);
1815 tsec_offload_setup(struct tsec_softc *sc)
1817 if_t ifp = sc->tsec_ifp;
1820 TSEC_GLOBAL_LOCK_ASSERT(sc);
1822 reg = TSEC_READ(sc, TSEC_REG_TCTRL);
1823 reg |= TSEC_TCTRL_IPCSEN | TSEC_TCTRL_TUCSEN;
1825 if (if_getcapenable(ifp) & IFCAP_TXCSUM)
1826 if_sethwassist(ifp, TSEC_CHECKSUM_FEATURES);
1828 if_sethwassist(ifp, 0);
1830 TSEC_WRITE(sc, TSEC_REG_TCTRL, reg);
1832 reg = TSEC_READ(sc, TSEC_REG_RCTRL);
1833 reg &= ~(TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN | TSEC_RCTRL_PRSDEP);
1834 reg |= TSEC_RCTRL_PRSDEP_PARSE_L2 | TSEC_RCTRL_VLEX;
1836 if (if_getcapenable(ifp) & IFCAP_RXCSUM)
1837 reg |= TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN |
1838 TSEC_RCTRL_PRSDEP_PARSE_L234;
1840 TSEC_WRITE(sc, TSEC_REG_RCTRL, reg);
1844 tsec_offload_process_frame(struct tsec_softc *sc, struct mbuf *m)
1846 struct tsec_rx_fcb rx_fcb;
1848 int protocol, flags;
1850 TSEC_RECEIVE_LOCK_ASSERT(sc);
1852 m_copydata(m, 0, sizeof(struct tsec_rx_fcb), (caddr_t)(&rx_fcb));
1853 flags = rx_fcb.flags;
1854 protocol = rx_fcb.protocol;
1856 if (TSEC_RX_FCB_IP_CSUM_CHECKED(flags)) {
1857 csum_flags |= CSUM_IP_CHECKED;
1859 if ((flags & TSEC_RX_FCB_IP_CSUM_ERROR) == 0)
1860 csum_flags |= CSUM_IP_VALID;
1863 if ((protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) &&
1864 TSEC_RX_FCB_TCP_UDP_CSUM_CHECKED(flags) &&
1865 (flags & TSEC_RX_FCB_TCP_UDP_CSUM_ERROR) == 0) {
1866 csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1867 m->m_pkthdr.csum_data = 0xFFFF;
1870 m->m_pkthdr.csum_flags = csum_flags;
1872 if (flags & TSEC_RX_FCB_VLAN) {
1873 m->m_pkthdr.ether_vtag = rx_fcb.vlan;
1874 m->m_flags |= M_VLANTAG;
1877 m_adj(m, sizeof(struct tsec_rx_fcb));
1881 tsec_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
1883 uint32_t h, *hashtable = arg;
1885 h = (ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 24) & 0xFF;
1886 hashtable[(h >> 5)] |= 1 << (0x1F - (h & 0x1F));
1892 tsec_setup_multicast(struct tsec_softc *sc)
1894 uint32_t hashtable[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1895 if_t ifp = sc->tsec_ifp;
1898 TSEC_GLOBAL_LOCK_ASSERT(sc);
1900 if (if_getflags(ifp) & IFF_ALLMULTI) {
1901 for (i = 0; i < 8; i++)
1902 TSEC_WRITE(sc, TSEC_REG_GADDR(i), 0xFFFFFFFF);
1907 if_foreach_llmaddr(ifp, tsec_hash_maddr, &hashtable);
1909 for (i = 0; i < 8; i++)
1910 TSEC_WRITE(sc, TSEC_REG_GADDR(i), hashtable[i]);
1914 tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu)
1917 mtu += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
1919 TSEC_GLOBAL_LOCK_ASSERT(sc);
1921 if (mtu >= TSEC_MIN_FRAME_SIZE && mtu <= TSEC_MAX_FRAME_SIZE) {
1922 TSEC_WRITE(sc, TSEC_REG_MAXFRM, mtu);