2 * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
3 * Copyright (C) 2006-2007 Semihalf, Piotr Kruszynski <ppk@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Freescale integrated Three-Speed Ethernet Controller (TSEC) driver.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
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_arp.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
61 #include <machine/bus.h>
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
66 #include <dev/tsec/if_tsec.h>
67 #include <dev/tsec/if_tsecreg.h>
69 static int tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag,
70 bus_dmamap_t *dmap, bus_size_t dsize, void **vaddr, void *raddr,
72 static void tsec_dma_ctl(struct tsec_softc *sc, int state);
73 static int tsec_encap(struct tsec_softc *sc, struct mbuf *m_head,
75 static void tsec_free_dma(struct tsec_softc *sc);
76 static void tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr);
77 static int tsec_ifmedia_upd(struct ifnet *ifp);
78 static void tsec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
79 static int tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map,
80 struct mbuf **mbufp, uint32_t *paddr);
81 static void tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs,
83 static void tsec_intrs_ctl(struct tsec_softc *sc, int state);
84 static void tsec_init(void *xsc);
85 static void tsec_init_locked(struct tsec_softc *sc);
86 static int tsec_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
87 static void tsec_reset_mac(struct tsec_softc *sc);
88 static void tsec_setfilter(struct tsec_softc *sc);
89 static void tsec_set_mac_address(struct tsec_softc *sc);
90 static void tsec_start(struct ifnet *ifp);
91 static void tsec_start_locked(struct ifnet *ifp);
92 static void tsec_stop(struct tsec_softc *sc);
93 static void tsec_tick(void *arg);
94 static void tsec_watchdog(struct tsec_softc *sc);
95 static void tsec_add_sysctls(struct tsec_softc *sc);
96 static int tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS);
97 static int tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS);
98 static void tsec_set_rxic(struct tsec_softc *sc);
99 static void tsec_set_txic(struct tsec_softc *sc);
100 static int tsec_receive_intr_locked(struct tsec_softc *sc, int count);
101 static void tsec_transmit_intr_locked(struct tsec_softc *sc);
102 static void tsec_error_intr_locked(struct tsec_softc *sc, int count);
103 static void tsec_offload_setup(struct tsec_softc *sc);
104 static void tsec_offload_process_frame(struct tsec_softc *sc,
106 static void tsec_setup_multicast(struct tsec_softc *sc);
107 static int tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu);
109 struct tsec_softc *tsec0_sc = NULL; /* XXX ugly hack! */
111 devclass_t tsec_devclass;
112 DRIVER_MODULE(miibus, tsec, miibus_driver, miibus_devclass, 0, 0);
113 MODULE_DEPEND(tsec, ether, 1, 1, 1);
114 MODULE_DEPEND(tsec, miibus, 1, 1, 1);
117 tsec_attach(struct tsec_softc *sc)
119 uint8_t hwaddr[ETHER_ADDR_LEN];
121 bus_dmamap_t *map_ptr;
122 bus_dmamap_t **map_pptr;
126 /* Reset all TSEC counters */
127 TSEC_TX_RX_COUNTERS_INIT(sc);
129 /* Stop DMA engine if enabled by firmware */
135 /* Disable interrupts for now */
136 tsec_intrs_ctl(sc, 0);
138 /* Configure defaults for interrupts coalescing */
139 sc->rx_ic_time = 768;
140 sc->rx_ic_count = 16;
141 sc->tx_ic_time = 768;
142 sc->tx_ic_count = 16;
145 tsec_add_sysctls(sc);
147 /* Allocate a busdma tag and DMA safe memory for TX descriptors. */
148 error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_tx_dtag,
149 &sc->tsec_tx_dmap, sizeof(*sc->tsec_tx_vaddr) * TSEC_TX_NUM_DESC,
150 (void **)&sc->tsec_tx_vaddr, &sc->tsec_tx_raddr, "TX");
157 /* Allocate a busdma tag and DMA safe memory for RX descriptors. */
158 error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_rx_dtag,
159 &sc->tsec_rx_dmap, sizeof(*sc->tsec_rx_vaddr) * TSEC_RX_NUM_DESC,
160 (void **)&sc->tsec_rx_vaddr, &sc->tsec_rx_raddr, "RX");
166 /* Allocate a busdma tag for TX mbufs. */
167 error = bus_dma_tag_create(NULL, /* parent */
168 TSEC_TXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
169 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
170 BUS_SPACE_MAXADDR, /* highaddr */
171 NULL, NULL, /* filtfunc, filtfuncarg */
172 MCLBYTES * (TSEC_TX_NUM_DESC - 1), /* maxsize */
173 TSEC_TX_NUM_DESC - 1, /* nsegments */
174 MCLBYTES, 0, /* maxsegsz, flags */
175 NULL, NULL, /* lockfunc, lockfuncarg */
176 &sc->tsec_tx_mtag); /* dmat */
178 device_printf(sc->dev, "failed to allocate busdma tag "
184 /* Allocate a busdma tag for RX mbufs. */
185 error = bus_dma_tag_create(NULL, /* parent */
186 TSEC_RXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
187 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
188 BUS_SPACE_MAXADDR, /* highaddr */
189 NULL, NULL, /* filtfunc, filtfuncarg */
190 MCLBYTES, /* maxsize */
192 MCLBYTES, 0, /* maxsegsz, flags */
193 NULL, NULL, /* lockfunc, lockfuncarg */
194 &sc->tsec_rx_mtag); /* dmat */
196 device_printf(sc->dev, "failed to allocate busdma tag "
202 /* Create TX busdma maps */
203 map_ptr = sc->tx_map_data;
204 map_pptr = sc->tx_map_unused_data;
206 for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
207 map_pptr[i] = &map_ptr[i];
208 error = bus_dmamap_create(sc->tsec_tx_mtag, 0, map_pptr[i]);
210 device_printf(sc->dev, "failed to init TX ring\n");
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");
249 if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
250 ifp->if_mtu = ETHERMTU;
251 ifp->if_flags = IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST;
252 ifp->if_init = tsec_init;
253 ifp->if_start = tsec_start;
254 ifp->if_ioctl = tsec_ioctl;
256 IFQ_SET_MAXLEN(&ifp->if_snd, TSEC_TX_NUM_DESC - 1);
257 ifp->if_snd.ifq_drv_maxlen = TSEC_TX_NUM_DESC - 1;
258 IFQ_SET_READY(&ifp->if_snd);
260 ifp->if_capabilities = IFCAP_VLAN_MTU;
262 ifp->if_capabilities |= IFCAP_HWCSUM;
264 ifp->if_capenable = ifp->if_capabilities;
266 #ifdef DEVICE_POLLING
267 /* Advertise that polling is supported */
268 ifp->if_capabilities |= IFCAP_POLLING;
272 error = mii_phy_probe(sc->dev, &sc->tsec_miibus, tsec_ifmedia_upd,
275 device_printf(sc->dev, "MII failed to find PHY!\n");
281 sc->tsec_mii = device_get_softc(sc->tsec_miibus);
283 /* Set MAC address */
284 tsec_get_hwaddr(sc, hwaddr);
285 ether_ifattach(ifp, hwaddr);
291 tsec_detach(struct tsec_softc *sc)
294 #ifdef DEVICE_POLLING
295 if (sc->tsec_ifp->if_capenable & IFCAP_POLLING)
296 ether_poll_deregister(sc->tsec_ifp);
299 /* Stop TSEC controller and free TX queue */
300 if (sc->sc_rres && sc->tsec_ifp)
301 tsec_shutdown(sc->dev);
303 /* Detach network interface */
305 ether_ifdetach(sc->tsec_ifp);
306 if_free(sc->tsec_ifp);
310 /* Free DMA resources */
317 tsec_shutdown(device_t dev)
319 struct tsec_softc *sc;
321 sc = device_get_softc(dev);
323 TSEC_GLOBAL_LOCK(sc);
325 TSEC_GLOBAL_UNLOCK(sc);
330 tsec_suspend(device_t dev)
333 /* TODO not implemented! */
338 tsec_resume(device_t dev)
341 /* TODO not implemented! */
348 struct tsec_softc *sc = xsc;
350 TSEC_GLOBAL_LOCK(sc);
351 tsec_init_locked(sc);
352 TSEC_GLOBAL_UNLOCK(sc);
356 tsec_init_locked(struct tsec_softc *sc)
358 struct tsec_desc *tx_desc = sc->tsec_tx_vaddr;
359 struct tsec_desc *rx_desc = sc->tsec_rx_vaddr;
360 struct ifnet *ifp = sc->tsec_ifp;
361 uint32_t timeout, val, i;
363 TSEC_GLOBAL_LOCK_ASSERT(sc);
367 * These steps are according to the MPC8555E PowerQUICCIII RM:
368 * 14.7 Initialization/Application Information
371 /* Step 1: soft reset MAC */
374 /* Step 2: Initialize MACCFG2 */
375 TSEC_WRITE(sc, TSEC_REG_MACCFG2,
376 TSEC_MACCFG2_FULLDUPLEX | /* Full Duplex = 1 */
377 TSEC_MACCFG2_PADCRC | /* PAD/CRC append */
378 TSEC_MACCFG2_GMII | /* I/F Mode bit */
379 TSEC_MACCFG2_PRECNT /* Preamble count = 7 */
382 /* Step 3: Initialize ECNTRL
383 * While the documentation states that R100M is ignored if RPM is
384 * not set, it does seem to be needed to get the orange boxes to
385 * work (which have a Marvell 88E1111 PHY). Go figure.
389 * XXX kludge - use circumstancial evidence to program ECNTRL
390 * correctly. Ideally we need some board information to guide
393 i = TSEC_READ(sc, TSEC_REG_ID2);
395 ? (TSEC_ECNTRL_TBIM | TSEC_ECNTRL_SGMIIM) /* Sumatra */
396 : TSEC_ECNTRL_R100M; /* Orange + CDS */
397 TSEC_WRITE(sc, TSEC_REG_ECNTRL, TSEC_ECNTRL_STEN | val);
399 /* Step 4: Initialize MAC station address */
400 tsec_set_mac_address(sc);
403 * Step 5: Assign a Physical address to the TBI so as to not conflict
404 * with the external PHY physical address
406 TSEC_WRITE(sc, TSEC_REG_TBIPA, 5);
408 /* Step 6: Reset the management interface */
409 TSEC_WRITE(tsec0_sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_RESETMGMT);
411 /* Step 7: Setup the MII Mgmt clock speed */
412 TSEC_WRITE(tsec0_sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_CLKDIV28);
414 /* Step 8: Read MII Mgmt indicator register and check for Busy = 0 */
415 timeout = TSEC_READ_RETRY;
416 while (--timeout && (TSEC_READ(tsec0_sc, TSEC_REG_MIIMIND) &
418 DELAY(TSEC_READ_DELAY);
420 if_printf(ifp, "tsec_init_locked(): Mgmt busy timeout\n");
424 /* Step 9: Setup the MII Mgmt */
425 mii_mediachg(sc->tsec_mii);
427 /* Step 10: Clear IEVENT register */
428 TSEC_WRITE(sc, TSEC_REG_IEVENT, 0xffffffff);
430 /* Step 11: Enable interrupts */
431 #ifdef DEVICE_POLLING
433 * ...only if polling is not turned on. Disable interrupts explicitly
434 * if polling is enabled.
436 if (ifp->if_capenable & IFCAP_POLLING )
437 tsec_intrs_ctl(sc, 0);
439 #endif /* DEVICE_POLLING */
440 tsec_intrs_ctl(sc, 1);
442 /* Step 12: Initialize IADDRn */
443 TSEC_WRITE(sc, TSEC_REG_IADDR0, 0);
444 TSEC_WRITE(sc, TSEC_REG_IADDR1, 0);
445 TSEC_WRITE(sc, TSEC_REG_IADDR2, 0);
446 TSEC_WRITE(sc, TSEC_REG_IADDR3, 0);
447 TSEC_WRITE(sc, TSEC_REG_IADDR4, 0);
448 TSEC_WRITE(sc, TSEC_REG_IADDR5, 0);
449 TSEC_WRITE(sc, TSEC_REG_IADDR6, 0);
450 TSEC_WRITE(sc, TSEC_REG_IADDR7, 0);
452 /* Step 13: Initialize GADDRn */
453 TSEC_WRITE(sc, TSEC_REG_GADDR0, 0);
454 TSEC_WRITE(sc, TSEC_REG_GADDR1, 0);
455 TSEC_WRITE(sc, TSEC_REG_GADDR2, 0);
456 TSEC_WRITE(sc, TSEC_REG_GADDR3, 0);
457 TSEC_WRITE(sc, TSEC_REG_GADDR4, 0);
458 TSEC_WRITE(sc, TSEC_REG_GADDR5, 0);
459 TSEC_WRITE(sc, TSEC_REG_GADDR6, 0);
460 TSEC_WRITE(sc, TSEC_REG_GADDR7, 0);
462 /* Step 14: Initialize RCTRL */
463 TSEC_WRITE(sc, TSEC_REG_RCTRL, 0);
465 /* Step 15: Initialize DMACTRL */
468 /* Step 16: Initialize FIFO_PAUSE_CTRL */
469 TSEC_WRITE(sc, TSEC_REG_FIFO_PAUSE_CTRL, TSEC_FIFO_PAUSE_CTRL_EN);
472 * Step 17: Initialize transmit/receive descriptor rings.
473 * Initialize TBASE and RBASE.
475 TSEC_WRITE(sc, TSEC_REG_TBASE, sc->tsec_tx_raddr);
476 TSEC_WRITE(sc, TSEC_REG_RBASE, sc->tsec_rx_raddr);
478 for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
479 tx_desc[i].bufptr = 0;
480 tx_desc[i].length = 0;
481 tx_desc[i].flags = ((i == TSEC_TX_NUM_DESC - 1) ?
484 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
485 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
487 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
488 rx_desc[i].bufptr = sc->rx_data[i].paddr;
489 rx_desc[i].length = 0;
490 rx_desc[i].flags = TSEC_RXBD_E | TSEC_RXBD_I |
491 ((i == TSEC_RX_NUM_DESC - 1) ? TSEC_RXBD_W : 0);
493 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
494 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
496 /* Step 18: Initialize the maximum receive buffer length */
497 TSEC_WRITE(sc, TSEC_REG_MRBLR, MCLBYTES);
499 /* Step 19: Configure ethernet frame sizes */
500 TSEC_WRITE(sc, TSEC_REG_MINFLR, TSEC_MIN_FRAME_SIZE);
501 tsec_set_mtu(sc, ifp->if_mtu);
503 /* Step 20: Enable Rx and RxBD sdata snooping */
504 TSEC_WRITE(sc, TSEC_REG_ATTR, TSEC_ATTR_RDSEN | TSEC_ATTR_RBDSEN);
505 TSEC_WRITE(sc, TSEC_REG_ATTRELI, 0);
507 /* Step 21: Reset collision counters in hardware */
508 TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
509 TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
510 TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
511 TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
512 TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
514 /* Step 22: Mask all CAM interrupts */
515 TSEC_WRITE(sc, TSEC_REG_MON_CAM1, 0xffffffff);
516 TSEC_WRITE(sc, TSEC_REG_MON_CAM2, 0xffffffff);
518 /* Step 23: Enable Rx and Tx */
519 val = TSEC_READ(sc, TSEC_REG_MACCFG1);
520 val |= (TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
521 TSEC_WRITE(sc, TSEC_REG_MACCFG1, val);
523 /* Step 24: Reset TSEC counters for Tx and Rx rings */
524 TSEC_TX_RX_COUNTERS_INIT(sc);
526 /* Step 25: Setup TCP/IP Off-Load engine */
528 tsec_offload_setup(sc);
530 /* Step 26: Setup multicast filters */
531 tsec_setup_multicast(sc);
533 /* Step 27: Activate network interface */
534 ifp->if_drv_flags |= IFF_DRV_RUNNING;
535 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
536 sc->tsec_if_flags = ifp->if_flags;
537 sc->tsec_watchdog = 0;
539 /* Schedule watchdog timeout */
540 callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
544 tsec_set_mac_address(struct tsec_softc *sc)
546 uint32_t macbuf[2] = { 0, 0 };
547 char *macbufp, *curmac;
550 TSEC_GLOBAL_LOCK_ASSERT(sc);
552 KASSERT((ETHER_ADDR_LEN <= sizeof(macbuf)),
553 ("tsec_set_mac_address: (%d <= %d", ETHER_ADDR_LEN,
556 macbufp = (char *)macbuf;
557 curmac = (char *)IF_LLADDR(sc->tsec_ifp);
559 /* Correct order of MAC address bytes */
560 for (i = 1; i <= ETHER_ADDR_LEN; i++)
561 macbufp[ETHER_ADDR_LEN-i] = curmac[i-1];
563 /* Initialize MAC station address MACSTNADDR2 and MACSTNADDR1 */
564 TSEC_WRITE(sc, TSEC_REG_MACSTNADDR2, macbuf[1]);
565 TSEC_WRITE(sc, TSEC_REG_MACSTNADDR1, macbuf[0]);
569 * DMA control function, if argument state is:
570 * 0 - DMA engine will be disabled
571 * 1 - DMA engine will be enabled
574 tsec_dma_ctl(struct tsec_softc *sc, int state)
577 uint32_t dma_flags, timeout;
581 dma_flags = TSEC_READ(sc, TSEC_REG_DMACTRL);
585 /* Temporarily clear stop graceful stop bits. */
586 tsec_dma_ctl(sc, 1000);
589 dma_flags |= (TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
593 /* Set write with response (WWR), wait (WOP) and snoop bits */
594 dma_flags |= (TSEC_DMACTRL_TDSEN | TSEC_DMACTRL_TBDSEN |
595 DMACTRL_WWR | DMACTRL_WOP);
597 /* Clear graceful stop bits */
598 dma_flags &= ~(TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
601 device_printf(dev, "tsec_dma_ctl(): unknown state value: %d\n",
605 TSEC_WRITE(sc, TSEC_REG_DMACTRL, dma_flags);
609 /* Wait for DMA stop */
610 timeout = TSEC_READ_RETRY;
611 while (--timeout && (!(TSEC_READ(sc, TSEC_REG_IEVENT) &
612 (TSEC_IEVENT_GRSC | TSEC_IEVENT_GTSC))))
613 DELAY(TSEC_READ_DELAY);
616 device_printf(dev, "tsec_dma_ctl(): timeout!\n");
619 /* Restart transmission function */
620 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
625 * Interrupts control function, if argument state is:
626 * 0 - all TSEC interrupts will be masked
627 * 1 - all TSEC interrupts will be unmasked
630 tsec_intrs_ctl(struct tsec_softc *sc, int state)
638 TSEC_WRITE(sc, TSEC_REG_IMASK, 0);
641 TSEC_WRITE(sc, TSEC_REG_IMASK, TSEC_IMASK_BREN |
642 TSEC_IMASK_RXCEN | TSEC_IMASK_BSYEN | TSEC_IMASK_EBERREN |
643 TSEC_IMASK_BTEN | TSEC_IMASK_TXEEN | TSEC_IMASK_TXBEN |
644 TSEC_IMASK_TXFEN | TSEC_IMASK_XFUNEN | TSEC_IMASK_RXFEN);
647 device_printf(dev, "tsec_intrs_ctl(): unknown state value: %d\n",
653 tsec_reset_mac(struct tsec_softc *sc)
655 uint32_t maccfg1_flags;
657 /* Set soft reset bit */
658 maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
659 maccfg1_flags |= TSEC_MACCFG1_SOFT_RESET;
660 TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
662 /* Clear soft reset bit */
663 maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
664 maccfg1_flags &= ~TSEC_MACCFG1_SOFT_RESET;
665 TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
669 tsec_watchdog(struct tsec_softc *sc)
673 TSEC_GLOBAL_LOCK_ASSERT(sc);
675 if (sc->tsec_watchdog == 0 || --sc->tsec_watchdog > 0)
680 if_printf(ifp, "watchdog timeout\n");
683 tsec_init_locked(sc);
687 tsec_start(struct ifnet *ifp)
689 struct tsec_softc *sc = ifp->if_softc;
691 TSEC_TRANSMIT_LOCK(sc);
692 tsec_start_locked(ifp);
693 TSEC_TRANSMIT_UNLOCK(sc);
697 tsec_start_locked(struct ifnet *ifp)
699 struct tsec_softc *sc;
700 struct mbuf *m0, *mtmp;
701 struct tsec_tx_fcb *tx_fcb;
702 unsigned int queued = 0;
703 int csum_flags, fcb_inserted = 0;
707 TSEC_TRANSMIT_LOCK_ASSERT(sc);
709 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
713 if (sc->tsec_link == 0)
716 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
717 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
719 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
720 /* Get packet from the queue */
721 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
725 /* Insert TCP/IP Off-load frame control block */
726 csum_flags = m0->m_pkthdr.csum_flags;
729 M_PREPEND(m0, sizeof(struct tsec_tx_fcb), M_DONTWAIT);
733 tx_fcb = mtod(m0, struct tsec_tx_fcb *);
735 tx_fcb->l3_offset = ETHER_HDR_LEN;
736 tx_fcb->l4_offset = sizeof(struct ip);
738 if (csum_flags & CSUM_IP)
739 tx_fcb->flags |= TSEC_TX_FCB_IP4 |
742 if (csum_flags & CSUM_TCP)
743 tx_fcb->flags |= TSEC_TX_FCB_TCP |
744 TSEC_TX_FCB_CSUM_TCP_UDP;
746 if (csum_flags & CSUM_UDP)
747 tx_fcb->flags |= TSEC_TX_FCB_UDP |
748 TSEC_TX_FCB_CSUM_TCP_UDP;
753 mtmp = m_defrag(m0, M_DONTWAIT);
757 if (tsec_encap(sc, m0, fcb_inserted)) {
758 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
759 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
765 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
766 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
769 /* Enable transmitter and watchdog timer */
770 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
771 sc->tsec_watchdog = 5;
776 tsec_encap(struct tsec_softc *sc, struct mbuf *m0, int fcb_inserted)
778 struct tsec_desc *tx_desc = NULL;
780 bus_dma_segment_t segs[TSEC_TX_NUM_DESC];
782 int csum_flag = 0, error, seg, nsegs;
784 TSEC_TRANSMIT_LOCK_ASSERT(sc);
788 if (TSEC_FREE_TX_DESC(sc) == 0) {
789 /* No free descriptors */
793 /* Fetch unused map */
794 mapp = TSEC_ALLOC_TX_MAP(sc);
796 /* Create mapping in DMA memory */
797 error = bus_dmamap_load_mbuf_sg(sc->tsec_tx_mtag,
798 *mapp, m0, segs, &nsegs, BUS_DMA_NOWAIT);
799 if (error != 0 || nsegs > TSEC_FREE_TX_DESC(sc) || nsegs <= 0) {
800 bus_dmamap_unload(sc->tsec_tx_mtag, *mapp);
801 TSEC_FREE_TX_MAP(sc, mapp);
802 return ((error != 0) ? error : -1);
804 bus_dmamap_sync(sc->tsec_tx_mtag, *mapp, BUS_DMASYNC_PREWRITE);
806 if ((ifp->if_flags & IFF_DEBUG) && (nsegs > 1))
807 if_printf(ifp, "TX buffer has %d segments\n", nsegs);
810 csum_flag = TSEC_TXBD_TOE;
812 /* Everything is ok, now we can send buffers */
813 for (seg = 0; seg < nsegs; seg++) {
814 tx_desc = TSEC_GET_CUR_TX_DESC(sc);
816 tx_desc->length = segs[seg].ds_len;
817 tx_desc->bufptr = segs[seg].ds_addr;
824 * - transmit the CRC sequence after the last data byte
825 * - interrupt after the last buffer
828 (tx_desc->flags & TSEC_TXBD_W) |
829 ((seg == 0) ? csum_flag : 0) | TSEC_TXBD_R | TSEC_TXBD_TC |
830 ((seg == nsegs - 1) ? TSEC_TXBD_L | TSEC_TXBD_I : 0);
833 /* Save mbuf and DMA mapping for release at later stage */
834 TSEC_PUT_TX_MBUF(sc, m0);
835 TSEC_PUT_TX_MAP(sc, mapp);
841 tsec_setfilter(struct tsec_softc *sc)
847 flags = TSEC_READ(sc, TSEC_REG_RCTRL);
849 /* Promiscuous mode */
850 if (ifp->if_flags & IFF_PROMISC)
851 flags |= TSEC_RCTRL_PROM;
853 flags &= ~TSEC_RCTRL_PROM;
855 TSEC_WRITE(sc, TSEC_REG_RCTRL, flags);
858 #ifdef DEVICE_POLLING
859 static poll_handler_t tsec_poll;
862 tsec_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
865 struct tsec_softc *sc = ifp->if_softc;
870 TSEC_GLOBAL_LOCK(sc);
871 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
872 TSEC_GLOBAL_UNLOCK(sc);
876 if (cmd == POLL_AND_CHECK_STATUS) {
877 tsec_error_intr_locked(sc, count);
879 /* Clear all events reported */
880 ie = TSEC_READ(sc, TSEC_REG_IEVENT);
881 TSEC_WRITE(sc, TSEC_REG_IEVENT, ie);
884 tsec_transmit_intr_locked(sc);
886 TSEC_GLOBAL_TO_RECEIVE_LOCK(sc);
888 rx_npkts = tsec_receive_intr_locked(sc, count);
890 TSEC_RECEIVE_UNLOCK(sc);
894 #endif /* DEVICE_POLLING */
897 tsec_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
899 struct tsec_softc *sc = ifp->if_softc;
900 struct ifreq *ifr = (struct ifreq *)data;
908 TSEC_GLOBAL_LOCK(sc);
909 if (tsec_set_mtu(sc, ifr->ifr_mtu))
910 ifp->if_mtu = ifr->ifr_mtu;
913 TSEC_GLOBAL_UNLOCK(sc);
916 TSEC_GLOBAL_LOCK(sc);
917 if (ifp->if_flags & IFF_UP) {
918 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
919 if ((sc->tsec_if_flags ^ ifp->if_flags) &
923 if ((sc->tsec_if_flags ^ ifp->if_flags) &
925 tsec_setup_multicast(sc);
927 tsec_init_locked(sc);
928 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
931 sc->tsec_if_flags = ifp->if_flags;
932 TSEC_GLOBAL_UNLOCK(sc);
936 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
937 TSEC_GLOBAL_LOCK(sc);
938 tsec_setup_multicast(sc);
939 TSEC_GLOBAL_UNLOCK(sc);
943 error = ifmedia_ioctl(ifp, ifr, &sc->tsec_mii->mii_media,
947 mask = ifp->if_capenable ^ ifr->ifr_reqcap;
948 if ((mask & IFCAP_HWCSUM) && sc->is_etsec) {
949 TSEC_GLOBAL_LOCK(sc);
950 ifp->if_capenable &= ~IFCAP_HWCSUM;
951 ifp->if_capenable |= IFCAP_HWCSUM & ifr->ifr_reqcap;
952 tsec_offload_setup(sc);
953 TSEC_GLOBAL_UNLOCK(sc);
955 #ifdef DEVICE_POLLING
956 if (mask & IFCAP_POLLING) {
957 if (ifr->ifr_reqcap & IFCAP_POLLING) {
958 error = ether_poll_register(tsec_poll, ifp);
962 TSEC_GLOBAL_LOCK(sc);
963 /* Disable interrupts */
964 tsec_intrs_ctl(sc, 0);
965 ifp->if_capenable |= IFCAP_POLLING;
966 TSEC_GLOBAL_UNLOCK(sc);
968 error = ether_poll_deregister(ifp);
969 TSEC_GLOBAL_LOCK(sc);
970 /* Enable interrupts */
971 tsec_intrs_ctl(sc, 1);
972 ifp->if_capenable &= ~IFCAP_POLLING;
973 TSEC_GLOBAL_UNLOCK(sc);
980 error = ether_ioctl(ifp, command, data);
983 /* Flush buffers if not empty */
984 if (ifp->if_flags & IFF_UP)
990 tsec_ifmedia_upd(struct ifnet *ifp)
992 struct tsec_softc *sc = ifp->if_softc;
993 struct mii_data *mii;
995 TSEC_TRANSMIT_LOCK(sc);
1000 TSEC_TRANSMIT_UNLOCK(sc);
1005 tsec_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1007 struct tsec_softc *sc = ifp->if_softc;
1008 struct mii_data *mii;
1010 TSEC_TRANSMIT_LOCK(sc);
1015 ifmr->ifm_active = mii->mii_media_active;
1016 ifmr->ifm_status = mii->mii_media_status;
1018 TSEC_TRANSMIT_UNLOCK(sc);
1022 tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map, struct mbuf **mbufp,
1025 struct mbuf *new_mbuf;
1026 bus_dma_segment_t seg[1];
1029 KASSERT(mbufp != NULL, ("NULL mbuf pointer!"));
1031 new_mbuf = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MCLBYTES);
1032 if (new_mbuf == NULL)
1034 new_mbuf->m_len = new_mbuf->m_pkthdr.len = new_mbuf->m_ext.ext_size;
1037 bus_dmamap_sync(tag, map, BUS_DMASYNC_POSTREAD);
1038 bus_dmamap_unload(tag, map);
1041 error = bus_dmamap_load_mbuf_sg(tag, map, new_mbuf, seg, &nsegs,
1043 KASSERT(nsegs == 1, ("Too many segments returned!"));
1044 if (nsegs != 1 || error)
1045 panic("tsec_new_rxbuf(): nsegs(%d), error(%d)", nsegs, error);
1049 printf("tsec: bus_dmamap_load_mbuf_sg() returned: %d!\n",
1057 KASSERT(((seg->ds_addr) & (TSEC_RXBUFFER_ALIGNMENT-1)) == 0,
1058 ("Wrong alignment of RX buffer!"));
1060 bus_dmamap_sync(tag, map, BUS_DMASYNC_PREREAD);
1062 (*mbufp) = new_mbuf;
1063 (*paddr) = seg->ds_addr;
1068 tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1072 KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
1074 *paddr = segs->ds_addr;
1078 tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag, bus_dmamap_t *dmap,
1079 bus_size_t dsize, void **vaddr, void *raddr, const char *dname)
1083 /* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
1084 error = bus_dma_tag_create(NULL, /* parent */
1085 PAGE_SIZE, 0, /* alignment, boundary */
1086 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1087 BUS_SPACE_MAXADDR, /* highaddr */
1088 NULL, NULL, /* filtfunc, filtfuncarg */
1089 dsize, 1, /* maxsize, nsegments */
1090 dsize, 0, /* maxsegsz, flags */
1091 NULL, NULL, /* lockfunc, lockfuncarg */
1095 device_printf(dev, "failed to allocate busdma %s tag\n",
1101 error = bus_dmamem_alloc(*dtag, vaddr, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1104 device_printf(dev, "failed to allocate %s DMA safe memory\n",
1106 bus_dma_tag_destroy(*dtag);
1111 error = bus_dmamap_load(*dtag, *dmap, *vaddr, dsize,
1112 tsec_map_dma_addr, raddr, BUS_DMA_NOWAIT);
1114 device_printf(dev, "cannot get address of the %s "
1115 "descriptors\n", dname);
1116 bus_dmamem_free(*dtag, *vaddr, *dmap);
1117 bus_dma_tag_destroy(*dtag);
1126 tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr)
1132 /* Unmap descriptors from DMA memory */
1133 bus_dmamap_sync(dtag, dmap, BUS_DMASYNC_POSTREAD |
1134 BUS_DMASYNC_POSTWRITE);
1135 bus_dmamap_unload(dtag, dmap);
1137 /* Free descriptors memory */
1138 bus_dmamem_free(dtag, vaddr, dmap);
1140 /* Destroy descriptors tag */
1141 bus_dma_tag_destroy(dtag);
1145 tsec_free_dma(struct tsec_softc *sc)
1150 for (i = 0; i < TSEC_TX_NUM_DESC; i++)
1151 if (sc->tx_map_data[i] != NULL)
1152 bus_dmamap_destroy(sc->tsec_tx_mtag,
1153 sc->tx_map_data[i]);
1154 /* Destroy tag for TX mbufs */
1155 bus_dma_tag_destroy(sc->tsec_tx_mtag);
1157 /* Free RX mbufs and maps */
1158 for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
1159 if (sc->rx_data[i].mbuf) {
1160 /* Unload buffer from DMA */
1161 bus_dmamap_sync(sc->tsec_rx_mtag, sc->rx_data[i].map,
1162 BUS_DMASYNC_POSTREAD);
1163 bus_dmamap_unload(sc->tsec_rx_mtag,
1164 sc->rx_data[i].map);
1167 m_freem(sc->rx_data[i].mbuf);
1169 /* Destroy map for this buffer */
1170 if (sc->rx_data[i].map != NULL)
1171 bus_dmamap_destroy(sc->tsec_rx_mtag,
1172 sc->rx_data[i].map);
1174 /* Destroy tag for RX mbufs */
1175 bus_dma_tag_destroy(sc->tsec_rx_mtag);
1177 /* Unload TX/RX descriptors */
1178 tsec_free_dma_desc(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1180 tsec_free_dma_desc(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1185 tsec_stop(struct tsec_softc *sc)
1192 TSEC_GLOBAL_LOCK_ASSERT(sc);
1196 /* Disable interface and watchdog timer */
1197 callout_stop(&sc->tsec_callout);
1198 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1199 sc->tsec_watchdog = 0;
1201 /* Disable all interrupts and stop DMA */
1202 tsec_intrs_ctl(sc, 0);
1203 tsec_dma_ctl(sc, 0);
1205 /* Remove pending data from TX queue */
1206 while (!TSEC_EMPTYQ_TX_MBUF(sc)) {
1207 m0 = TSEC_GET_TX_MBUF(sc);
1208 mapp = TSEC_GET_TX_MAP(sc);
1210 bus_dmamap_sync(sc->tsec_tx_mtag, *mapp,
1211 BUS_DMASYNC_POSTWRITE);
1212 bus_dmamap_unload(sc->tsec_tx_mtag, *mapp);
1214 TSEC_FREE_TX_MAP(sc, mapp);
1218 /* Disable RX and TX */
1219 tmpval = TSEC_READ(sc, TSEC_REG_MACCFG1);
1220 tmpval &= ~(TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
1221 TSEC_WRITE(sc, TSEC_REG_MACCFG1, tmpval);
1226 tsec_tick(void *arg)
1228 struct tsec_softc *sc = arg;
1232 TSEC_GLOBAL_LOCK(sc);
1237 link = sc->tsec_link;
1239 mii_tick(sc->tsec_mii);
1241 if (link == 0 && sc->tsec_link == 1 &&
1242 (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)))
1243 tsec_start_locked(ifp);
1245 /* Schedule another timeout one second from now. */
1246 callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
1248 TSEC_GLOBAL_UNLOCK(sc);
1252 * This is the core RX routine. It replenishes mbufs in the descriptor and
1253 * sends data which have been dma'ed into host memory to upper layer.
1255 * Loops at most count times if count is > 0, or until done if count < 0.
1258 tsec_receive_intr_locked(struct tsec_softc *sc, int count)
1260 struct tsec_desc *rx_desc;
1262 struct rx_data_type *rx_data;
1269 TSEC_RECEIVE_LOCK_ASSERT(sc);
1272 rx_data = sc->rx_data;
1276 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1277 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1279 for (c = 0; ; c++) {
1280 if (count >= 0 && count-- == 0)
1283 rx_desc = TSEC_GET_CUR_RX_DESC(sc);
1284 flags = rx_desc->flags;
1286 /* Check if there is anything to receive */
1287 if ((flags & TSEC_RXBD_E) || (c >= TSEC_RX_NUM_DESC)) {
1289 * Avoid generating another interrupt
1291 if (flags & TSEC_RXBD_E)
1292 TSEC_WRITE(sc, TSEC_REG_IEVENT,
1293 TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
1295 * We didn't consume current descriptor and have to
1296 * return it to the queue
1298 TSEC_BACK_CUR_RX_DESC(sc);
1302 if (flags & (TSEC_RXBD_LG | TSEC_RXBD_SH | TSEC_RXBD_NO |
1303 TSEC_RXBD_CR | TSEC_RXBD_OV | TSEC_RXBD_TR)) {
1305 rx_desc->length = 0;
1306 rx_desc->flags = (rx_desc->flags &
1307 ~TSEC_RXBD_ZEROONINIT) | TSEC_RXBD_E | TSEC_RXBD_I;
1309 if (sc->frame != NULL) {
1317 /* Ok... process frame */
1318 i = TSEC_GET_CUR_RX_DESC_CNT(sc);
1319 m = rx_data[i].mbuf;
1320 m->m_len = rx_desc->length;
1322 if (sc->frame != NULL) {
1323 if ((flags & TSEC_RXBD_L) != 0)
1324 m->m_len -= m_length(sc->frame, NULL);
1326 m->m_flags &= ~M_PKTHDR;
1327 m_cat(sc->frame, m);
1334 if ((flags & TSEC_RXBD_L) != 0) {
1339 if (tsec_new_rxbuf(sc->tsec_rx_mtag, rx_data[i].map,
1340 &rx_data[i].mbuf, &rx_data[i].paddr)) {
1343 * We ran out of mbufs; didn't consume current
1344 * descriptor and have to return it to the queue.
1346 TSEC_BACK_CUR_RX_DESC(sc);
1350 /* Attach new buffer to descriptor and clear flags */
1351 rx_desc->bufptr = rx_data[i].paddr;
1352 rx_desc->length = 0;
1353 rx_desc->flags = (rx_desc->flags & ~TSEC_RXBD_ZEROONINIT) |
1354 TSEC_RXBD_E | TSEC_RXBD_I;
1357 m->m_pkthdr.rcvif = ifp;
1360 m_adj(m, -ETHER_CRC_LEN);
1363 tsec_offload_process_frame(sc, m);
1365 TSEC_RECEIVE_UNLOCK(sc);
1366 (*ifp->if_input)(ifp, m);
1367 TSEC_RECEIVE_LOCK(sc);
1372 bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1373 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1376 * Make sure TSEC receiver is not halted.
1378 * Various conditions can stop the TSEC receiver, but not all are
1379 * signaled and handled by error interrupt, so make sure the receiver
1380 * is running. Writing to TSEC_REG_RSTAT restarts the receiver when
1381 * halted, and is harmless if already running.
1383 TSEC_WRITE(sc, TSEC_REG_RSTAT, TSEC_RSTAT_QHLT);
1388 tsec_receive_intr(void *arg)
1390 struct tsec_softc *sc = arg;
1392 TSEC_RECEIVE_LOCK(sc);
1394 #ifdef DEVICE_POLLING
1395 if (sc->tsec_ifp->if_capenable & IFCAP_POLLING) {
1396 TSEC_RECEIVE_UNLOCK(sc);
1401 /* Confirm the interrupt was received by driver */
1402 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
1403 tsec_receive_intr_locked(sc, -1);
1405 TSEC_RECEIVE_UNLOCK(sc);
1409 tsec_transmit_intr_locked(struct tsec_softc *sc)
1411 struct tsec_desc *tx_desc;
1417 TSEC_TRANSMIT_LOCK_ASSERT(sc);
1421 /* Update collision statistics */
1422 ifp->if_collisions += TSEC_READ(sc, TSEC_REG_MON_TNCL);
1424 /* Reset collision counters in hardware */
1425 TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
1426 TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
1427 TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
1428 TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
1429 TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
1431 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1432 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1434 while (TSEC_CUR_DIFF_DIRTY_TX_DESC(sc)) {
1435 tx_desc = TSEC_GET_DIRTY_TX_DESC(sc);
1436 if (tx_desc->flags & TSEC_TXBD_R) {
1437 TSEC_BACK_DIRTY_TX_DESC(sc);
1441 if ((tx_desc->flags & TSEC_TXBD_L) == 0)
1445 * This is the last buf in this packet, so unmap and free it.
1447 m0 = TSEC_GET_TX_MBUF(sc);
1448 mapp = TSEC_GET_TX_MAP(sc);
1450 bus_dmamap_sync(sc->tsec_tx_mtag, *mapp,
1451 BUS_DMASYNC_POSTWRITE);
1452 bus_dmamap_unload(sc->tsec_tx_mtag, *mapp);
1454 TSEC_FREE_TX_MAP(sc, mapp);
1460 bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1461 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1464 /* Now send anything that was pending */
1465 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1466 tsec_start_locked(ifp);
1468 /* Stop wathdog if all sent */
1469 if (TSEC_EMPTYQ_TX_MBUF(sc))
1470 sc->tsec_watchdog = 0;
1475 tsec_transmit_intr(void *arg)
1477 struct tsec_softc *sc = arg;
1479 TSEC_TRANSMIT_LOCK(sc);
1481 #ifdef DEVICE_POLLING
1482 if (sc->tsec_ifp->if_capenable & IFCAP_POLLING) {
1483 TSEC_TRANSMIT_UNLOCK(sc);
1487 /* Confirm the interrupt was received by driver */
1488 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_TXB | TSEC_IEVENT_TXF);
1489 tsec_transmit_intr_locked(sc);
1491 TSEC_TRANSMIT_UNLOCK(sc);
1495 tsec_error_intr_locked(struct tsec_softc *sc, int count)
1500 TSEC_GLOBAL_LOCK_ASSERT(sc);
1504 eflags = TSEC_READ(sc, TSEC_REG_IEVENT);
1506 /* Clear events bits in hardware */
1507 TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXC | TSEC_IEVENT_BSY |
1508 TSEC_IEVENT_EBERR | TSEC_IEVENT_MSRO | TSEC_IEVENT_BABT |
1509 TSEC_IEVENT_TXC | TSEC_IEVENT_TXE | TSEC_IEVENT_LC |
1510 TSEC_IEVENT_CRL | TSEC_IEVENT_XFUN);
1512 /* Check transmitter errors */
1513 if (eflags & TSEC_IEVENT_TXE) {
1516 if (eflags & TSEC_IEVENT_LC)
1517 ifp->if_collisions++;
1519 TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
1522 /* Check receiver errors */
1523 if (eflags & TSEC_IEVENT_BSY) {
1527 /* Get data from RX buffers */
1528 tsec_receive_intr_locked(sc, count);
1531 if (ifp->if_flags & IFF_DEBUG)
1532 if_printf(ifp, "tsec_error_intr(): event flags: 0x%x\n",
1535 if (eflags & TSEC_IEVENT_EBERR) {
1536 if_printf(ifp, "System bus error occurred during"
1537 "DMA transaction (flags: 0x%x)\n", eflags);
1538 tsec_init_locked(sc);
1541 if (eflags & TSEC_IEVENT_BABT)
1544 if (eflags & TSEC_IEVENT_BABR)
1549 tsec_error_intr(void *arg)
1551 struct tsec_softc *sc = arg;
1553 TSEC_GLOBAL_LOCK(sc);
1554 tsec_error_intr_locked(sc, -1);
1555 TSEC_GLOBAL_UNLOCK(sc);
1559 tsec_miibus_readreg(device_t dev, int phy, int reg)
1561 struct tsec_softc *sc;
1564 sc = device_get_softc(dev);
1566 if (device_get_unit(dev) != phy)
1571 TSEC_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1572 TSEC_WRITE(sc, TSEC_REG_MIIMCOM, 0);
1573 TSEC_WRITE(sc, TSEC_REG_MIIMCOM, TSEC_MIIMCOM_READCYCLE);
1575 timeout = TSEC_READ_RETRY;
1576 while (--timeout && TSEC_READ(sc, TSEC_REG_MIIMIND) &
1577 (TSEC_MIIMIND_NOTVALID | TSEC_MIIMIND_BUSY))
1578 DELAY(TSEC_READ_DELAY);
1581 device_printf(dev, "Timeout while reading from PHY!\n");
1583 return (TSEC_READ(sc, TSEC_REG_MIIMSTAT));
1587 tsec_miibus_writereg(device_t dev, int phy, int reg, int value)
1589 struct tsec_softc *sc;
1592 sc = device_get_softc(dev);
1594 if (device_get_unit(dev) != phy)
1595 device_printf(dev, "Trying to write to an alien PHY(%d)\n",
1600 TSEC_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1601 TSEC_WRITE(sc, TSEC_REG_MIIMCON, value);
1603 timeout = TSEC_READ_RETRY;
1604 while (--timeout && (TSEC_READ(sc, TSEC_REG_MIIMIND) &
1606 DELAY(TSEC_READ_DELAY);
1609 device_printf(dev, "Timeout while writing to PHY!\n");
1615 tsec_miibus_statchg(device_t dev)
1617 struct tsec_softc *sc;
1618 struct mii_data *mii;
1619 uint32_t ecntrl, id, tmp;
1622 sc = device_get_softc(dev);
1624 link = ((mii->mii_media_status & IFM_ACTIVE) ? 1 : 0);
1626 tmp = TSEC_READ(sc, TSEC_REG_MACCFG2) & ~TSEC_MACCFG2_IF;
1628 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
1629 tmp |= TSEC_MACCFG2_FULLDUPLEX;
1631 tmp &= ~TSEC_MACCFG2_FULLDUPLEX;
1633 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1636 tmp |= TSEC_MACCFG2_GMII;
1637 sc->tsec_link = link;
1641 tmp |= TSEC_MACCFG2_MII;
1642 sc->tsec_link = link;
1646 device_printf(dev, "No speed selected but link "
1652 device_printf(dev, "Unknown speed (%d), link %s!\n",
1653 IFM_SUBTYPE(mii->mii_media_active),
1654 ((link) ? "up" : "down"));
1657 TSEC_WRITE(sc, TSEC_REG_MACCFG2, tmp);
1659 /* XXX kludge - use circumstantial evidence for reduced mode. */
1660 id = TSEC_READ(sc, TSEC_REG_ID2);
1662 ecntrl = TSEC_READ(sc, TSEC_REG_ECNTRL) & ~TSEC_ECNTRL_R100M;
1663 ecntrl |= (tmp & TSEC_MACCFG2_MII) ? TSEC_ECNTRL_R100M : 0;
1664 TSEC_WRITE(sc, TSEC_REG_ECNTRL, ecntrl);
1669 tsec_add_sysctls(struct tsec_softc *sc)
1671 struct sysctl_ctx_list *ctx;
1672 struct sysctl_oid_list *children;
1673 struct sysctl_oid *tree;
1675 ctx = device_get_sysctl_ctx(sc->dev);
1676 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
1677 tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "int_coal",
1678 CTLFLAG_RD, 0, "TSEC Interrupts coalescing");
1679 children = SYSCTL_CHILDREN(tree);
1681 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_time",
1682 CTLTYPE_UINT | CTLFLAG_RW, sc, TSEC_IC_RX, tsec_sysctl_ic_time,
1683 "I", "IC RX time threshold (0-65535)");
1684 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_count",
1685 CTLTYPE_UINT | CTLFLAG_RW, sc, TSEC_IC_RX, tsec_sysctl_ic_count,
1686 "I", "IC RX frame count threshold (0-255)");
1688 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_time",
1689 CTLTYPE_UINT | CTLFLAG_RW, sc, TSEC_IC_TX, tsec_sysctl_ic_time,
1690 "I", "IC TX time threshold (0-65535)");
1691 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_count",
1692 CTLTYPE_UINT | CTLFLAG_RW, sc, TSEC_IC_TX, tsec_sysctl_ic_count,
1693 "I", "IC TX frame count threshold (0-255)");
1697 * With Interrupt Coalescing (IC) active, a transmit/receive frame
1698 * interrupt is raised either upon:
1700 * - threshold-defined period of time elapsed, or
1701 * - threshold-defined number of frames is received/transmitted,
1702 * whichever occurs first.
1704 * The following sysctls regulate IC behaviour (for TX/RX separately):
1706 * dev.tsec.<unit>.int_coal.rx_time
1707 * dev.tsec.<unit>.int_coal.rx_count
1708 * dev.tsec.<unit>.int_coal.tx_time
1709 * dev.tsec.<unit>.int_coal.tx_count
1713 * - 0 for either time or count disables IC on the given TX/RX path
1715 * - count: 1-255 (expresses frame count number; note that value of 1 is
1716 * effectively IC off)
1718 * - time: 1-65535 (value corresponds to a real time period and is
1719 * expressed in units equivalent to 64 TSEC interface clocks, i.e. one timer
1720 * threshold unit is 26.5 us, 2.56 us, or 512 ns, corresponding to 10 Mbps,
1721 * 100 Mbps, or 1Gbps, respectively. For detailed discussion consult the
1722 * TSEC reference manual.
1725 tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS)
1729 struct tsec_softc *sc = (struct tsec_softc *)arg1;
1731 time = (arg2 == TSEC_IC_RX) ? sc->rx_ic_time : sc->tx_ic_time;
1733 error = sysctl_handle_int(oidp, &time, 0, req);
1741 if (arg2 == TSEC_IC_RX) {
1742 sc->rx_ic_time = time;
1745 sc->tx_ic_time = time;
1754 tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS)
1758 struct tsec_softc *sc = (struct tsec_softc *)arg1;
1760 count = (arg2 == TSEC_IC_RX) ? sc->rx_ic_count : sc->tx_ic_count;
1762 error = sysctl_handle_int(oidp, &count, 0, req);
1770 if (arg2 == TSEC_IC_RX) {
1771 sc->rx_ic_count = count;
1774 sc->tx_ic_count = count;
1783 tsec_set_rxic(struct tsec_softc *sc)
1787 if (sc->rx_ic_count == 0 || sc->rx_ic_time == 0)
1791 rxic_val = 0x80000000;
1792 rxic_val |= (sc->rx_ic_count << 21);
1793 rxic_val |= sc->rx_ic_time;
1796 TSEC_WRITE(sc, TSEC_REG_RXIC, rxic_val);
1800 tsec_set_txic(struct tsec_softc *sc)
1804 if (sc->tx_ic_count == 0 || sc->tx_ic_time == 0)
1808 txic_val = 0x80000000;
1809 txic_val |= (sc->tx_ic_count << 21);
1810 txic_val |= sc->tx_ic_time;
1813 TSEC_WRITE(sc, TSEC_REG_TXIC, txic_val);
1817 tsec_offload_setup(struct tsec_softc *sc)
1819 struct ifnet *ifp = sc->tsec_ifp;
1822 TSEC_GLOBAL_LOCK_ASSERT(sc);
1824 reg = TSEC_READ(sc, TSEC_REG_TCTRL);
1825 reg |= TSEC_TCTRL_IPCSEN | TSEC_TCTRL_TUCSEN;
1827 if (ifp->if_capenable & IFCAP_TXCSUM)
1828 ifp->if_hwassist = TSEC_CHECKSUM_FEATURES;
1830 ifp->if_hwassist = 0;
1832 TSEC_WRITE(sc, TSEC_REG_TCTRL, reg);
1834 reg = TSEC_READ(sc, TSEC_REG_RCTRL);
1835 reg &= ~(TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN | TSEC_RCTRL_PRSDEP);
1836 reg |= TSEC_RCTRL_PRSDEP_PARSE_L2 | TSEC_RCTRL_VLEX;
1838 if (ifp->if_capenable & IFCAP_RXCSUM)
1839 reg |= TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN |
1840 TSEC_RCTRL_PRSDEP_PARSE_L234;
1842 TSEC_WRITE(sc, TSEC_REG_RCTRL, reg);
1847 tsec_offload_process_frame(struct tsec_softc *sc, struct mbuf *m)
1849 struct tsec_rx_fcb rx_fcb;
1851 int protocol, flags;
1853 TSEC_RECEIVE_LOCK_ASSERT(sc);
1855 m_copydata(m, 0, sizeof(struct tsec_rx_fcb), (caddr_t)(&rx_fcb));
1856 flags = rx_fcb.flags;
1857 protocol = rx_fcb.protocol;
1859 if (TSEC_RX_FCB_IP_CSUM_CHECKED(flags)) {
1860 csum_flags |= CSUM_IP_CHECKED;
1862 if ((flags & TSEC_RX_FCB_IP_CSUM_ERROR) == 0)
1863 csum_flags |= CSUM_IP_VALID;
1866 if ((protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) &&
1867 TSEC_RX_FCB_TCP_UDP_CSUM_CHECKED(flags) &&
1868 (flags & TSEC_RX_FCB_TCP_UDP_CSUM_ERROR) == 0) {
1870 csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1871 m->m_pkthdr.csum_data = 0xFFFF;
1874 m->m_pkthdr.csum_flags = csum_flags;
1876 if (flags & TSEC_RX_FCB_VLAN) {
1877 m->m_pkthdr.ether_vtag = rx_fcb.vlan;
1878 m->m_flags |= M_VLANTAG;
1881 m_adj(m, sizeof(struct tsec_rx_fcb));
1885 tsec_setup_multicast(struct tsec_softc *sc)
1887 uint32_t hashtable[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
1888 struct ifnet *ifp = sc->tsec_ifp;
1889 struct ifmultiaddr *ifma;
1893 TSEC_GLOBAL_LOCK_ASSERT(sc);
1895 if (ifp->if_flags & IFF_ALLMULTI) {
1896 for (i = 0; i < 8; i++)
1897 TSEC_WRITE(sc, TSEC_REG_GADDR(i), 0xFFFFFFFF);
1902 if_maddr_rlock(ifp);
1903 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1905 if (ifma->ifma_addr->sa_family != AF_LINK)
1908 h = (ether_crc32_be(LLADDR((struct sockaddr_dl *)
1909 ifma->ifma_addr), ETHER_ADDR_LEN) >> 24) & 0xFF;
1911 hashtable[(h >> 5)] |= 1 << (0x1F - (h & 0x1F));
1913 if_maddr_runlock(ifp);
1915 for (i = 0; i < 8; i++)
1916 TSEC_WRITE(sc, TSEC_REG_GADDR(i), hashtable[i]);
1920 tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu)
1923 mtu += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
1925 TSEC_GLOBAL_LOCK_ASSERT(sc);
1927 if (mtu >= TSEC_MIN_FRAME_SIZE && mtu <= TSEC_MAX_FRAME_SIZE) {
1928 TSEC_WRITE(sc, TSEC_REG_MAXFRM, mtu);