167196661SRafal Jaworowski /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4d1d3233eSRafal Jaworowski * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski
5d1d3233eSRafal Jaworowski * Copyright (C) 2006-2007 Semihalf, Piotr Kruszynski
667196661SRafal Jaworowski * All rights reserved.
767196661SRafal Jaworowski *
867196661SRafal Jaworowski * Redistribution and use in source and binary forms, with or without
967196661SRafal Jaworowski * modification, are permitted provided that the following conditions
1067196661SRafal Jaworowski * are met:
1167196661SRafal Jaworowski * 1. Redistributions of source code must retain the above copyright
1267196661SRafal Jaworowski * notice, this list of conditions and the following disclaimer.
1367196661SRafal Jaworowski * 2. Redistributions in binary form must reproduce the above copyright
1467196661SRafal Jaworowski * notice, this list of conditions and the following disclaimer in the
1567196661SRafal Jaworowski * documentation and/or other materials provided with the distribution.
1667196661SRafal Jaworowski *
1767196661SRafal Jaworowski * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1867196661SRafal Jaworowski * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1967196661SRafal Jaworowski * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
2067196661SRafal Jaworowski * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2167196661SRafal Jaworowski * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2267196661SRafal Jaworowski * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2367196661SRafal Jaworowski * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2467196661SRafal Jaworowski * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2567196661SRafal Jaworowski * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2667196661SRafal Jaworowski * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2767196661SRafal Jaworowski */
2867196661SRafal Jaworowski
2967196661SRafal Jaworowski /*
3067196661SRafal Jaworowski * Freescale integrated Three-Speed Ethernet Controller (TSEC) driver.
3167196661SRafal Jaworowski */
3267196661SRafal Jaworowski #include <sys/cdefs.h>
33bd37530eSRafal Jaworowski #ifdef HAVE_KERNEL_OPTION_HEADERS
34bd37530eSRafal Jaworowski #include "opt_device_polling.h"
35bd37530eSRafal Jaworowski #endif
36bd37530eSRafal Jaworowski
3767196661SRafal Jaworowski #include <sys/param.h>
3867196661SRafal Jaworowski #include <sys/systm.h>
39321e12c8SRafal Jaworowski #include <sys/bus.h>
4067196661SRafal Jaworowski #include <sys/endian.h>
4167196661SRafal Jaworowski #include <sys/mbuf.h>
4267196661SRafal Jaworowski #include <sys/kernel.h>
4367196661SRafal Jaworowski #include <sys/module.h>
4467196661SRafal Jaworowski #include <sys/socket.h>
45321e12c8SRafal Jaworowski #include <sys/sockio.h>
4667196661SRafal Jaworowski #include <sys/sysctl.h>
4767196661SRafal Jaworowski
48321e12c8SRafal Jaworowski #include <net/bpf.h>
49321e12c8SRafal Jaworowski #include <net/ethernet.h>
5067196661SRafal Jaworowski #include <net/if.h>
5176039bc8SGleb Smirnoff #include <net/if_var.h>
52321e12c8SRafal Jaworowski #include <net/if_arp.h>
5367196661SRafal Jaworowski #include <net/if_dl.h>
5467196661SRafal Jaworowski #include <net/if_media.h>
5567196661SRafal Jaworowski #include <net/if_types.h>
5667196661SRafal Jaworowski #include <net/if_vlan_var.h>
5767196661SRafal Jaworowski
58bd37530eSRafal Jaworowski #include <netinet/in_systm.h>
59bd37530eSRafal Jaworowski #include <netinet/in.h>
60bd37530eSRafal Jaworowski #include <netinet/ip.h>
61bd37530eSRafal Jaworowski
62321e12c8SRafal Jaworowski #include <machine/bus.h>
63321e12c8SRafal Jaworowski
6467196661SRafal Jaworowski #include <dev/mii/mii.h>
6567196661SRafal Jaworowski #include <dev/mii/miivar.h>
6667196661SRafal Jaworowski
6767196661SRafal Jaworowski #include <dev/tsec/if_tsec.h>
6867196661SRafal Jaworowski #include <dev/tsec/if_tsecreg.h>
6967196661SRafal Jaworowski
70321e12c8SRafal Jaworowski static int tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag,
71321e12c8SRafal Jaworowski bus_dmamap_t *dmap, bus_size_t dsize, void **vaddr, void *raddr,
72321e12c8SRafal Jaworowski const char *dname);
7367196661SRafal Jaworowski static void tsec_dma_ctl(struct tsec_softc *sc, int state);
7447842ecfSJustin Hibbits static void tsec_encap(if_t ifp, struct tsec_softc *sc,
752c0dbbcbSJustin Hibbits struct mbuf *m0, uint16_t fcb_flags, int *start_tx);
76321e12c8SRafal Jaworowski static void tsec_free_dma(struct tsec_softc *sc);
77321e12c8SRafal Jaworowski static void tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr);
7847842ecfSJustin Hibbits static int tsec_ifmedia_upd(if_t ifp);
7947842ecfSJustin Hibbits static void tsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr);
8067196661SRafal Jaworowski static int tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map,
8167196661SRafal Jaworowski struct mbuf **mbufp, uint32_t *paddr);
8267196661SRafal Jaworowski static void tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs,
8367196661SRafal Jaworowski int nseg, int error);
84321e12c8SRafal Jaworowski static void tsec_intrs_ctl(struct tsec_softc *sc, int state);
85321e12c8SRafal Jaworowski static void tsec_init(void *xsc);
86321e12c8SRafal Jaworowski static void tsec_init_locked(struct tsec_softc *sc);
8747842ecfSJustin Hibbits static int tsec_ioctl(if_t ifp, u_long command, caddr_t data);
88321e12c8SRafal Jaworowski static void tsec_reset_mac(struct tsec_softc *sc);
89321e12c8SRafal Jaworowski static void tsec_setfilter(struct tsec_softc *sc);
90321e12c8SRafal Jaworowski static void tsec_set_mac_address(struct tsec_softc *sc);
9147842ecfSJustin Hibbits static void tsec_start(if_t ifp);
9247842ecfSJustin Hibbits static void tsec_start_locked(if_t ifp);
9367196661SRafal Jaworowski static void tsec_stop(struct tsec_softc *sc);
9467196661SRafal Jaworowski static void tsec_tick(void *arg);
95321e12c8SRafal Jaworowski static void tsec_watchdog(struct tsec_softc *sc);
96bd37530eSRafal Jaworowski static void tsec_add_sysctls(struct tsec_softc *sc);
97bd37530eSRafal Jaworowski static int tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS);
98bd37530eSRafal Jaworowski static int tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS);
99bd37530eSRafal Jaworowski static void tsec_set_rxic(struct tsec_softc *sc);
100bd37530eSRafal Jaworowski static void tsec_set_txic(struct tsec_softc *sc);
1011abcdbd1SAttilio Rao static int tsec_receive_intr_locked(struct tsec_softc *sc, int count);
102bd37530eSRafal Jaworowski static void tsec_transmit_intr_locked(struct tsec_softc *sc);
103bd37530eSRafal Jaworowski static void tsec_error_intr_locked(struct tsec_softc *sc, int count);
104bd37530eSRafal Jaworowski static void tsec_offload_setup(struct tsec_softc *sc);
105bd37530eSRafal Jaworowski static void tsec_offload_process_frame(struct tsec_softc *sc,
106bd37530eSRafal Jaworowski struct mbuf *m);
107bd37530eSRafal Jaworowski static void tsec_setup_multicast(struct tsec_softc *sc);
108bd37530eSRafal Jaworowski static int tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu);
10967196661SRafal Jaworowski
1103e38757dSJohn Baldwin DRIVER_MODULE(miibus, tsec, miibus_driver, 0, 0);
11167196661SRafal Jaworowski MODULE_DEPEND(tsec, ether, 1, 1, 1);
11267196661SRafal Jaworowski MODULE_DEPEND(tsec, miibus, 1, 1, 1);
11367196661SRafal Jaworowski
114629aa519SNathan Whitehorn struct mtx tsec_phy_mtx;
115629aa519SNathan Whitehorn
116321e12c8SRafal Jaworowski int
tsec_attach(struct tsec_softc * sc)117321e12c8SRafal Jaworowski tsec_attach(struct tsec_softc *sc)
11867196661SRafal Jaworowski {
119321e12c8SRafal Jaworowski uint8_t hwaddr[ETHER_ADDR_LEN];
12047842ecfSJustin Hibbits if_t ifp;
121321e12c8SRafal Jaworowski int error = 0;
122ecb1ab17SRafal Jaworowski int i;
12367196661SRafal Jaworowski
124629aa519SNathan Whitehorn /* Initialize global (because potentially shared) MII lock */
125629aa519SNathan Whitehorn if (!mtx_initialized(&tsec_phy_mtx))
126629aa519SNathan Whitehorn mtx_init(&tsec_phy_mtx, "tsec mii", NULL, MTX_DEF);
127629aa519SNathan Whitehorn
128321e12c8SRafal Jaworowski /* Reset all TSEC counters */
129321e12c8SRafal Jaworowski TSEC_TX_RX_COUNTERS_INIT(sc);
130321e12c8SRafal Jaworowski
131321e12c8SRafal Jaworowski /* Stop DMA engine if enabled by firmware */
132321e12c8SRafal Jaworowski tsec_dma_ctl(sc, 0);
133321e12c8SRafal Jaworowski
134321e12c8SRafal Jaworowski /* Reset MAC */
135321e12c8SRafal Jaworowski tsec_reset_mac(sc);
136321e12c8SRafal Jaworowski
137321e12c8SRafal Jaworowski /* Disable interrupts for now */
138321e12c8SRafal Jaworowski tsec_intrs_ctl(sc, 0);
139321e12c8SRafal Jaworowski
140bd37530eSRafal Jaworowski /* Configure defaults for interrupts coalescing */
141bd37530eSRafal Jaworowski sc->rx_ic_time = 768;
142bd37530eSRafal Jaworowski sc->rx_ic_count = 16;
143bd37530eSRafal Jaworowski sc->tx_ic_time = 768;
144bd37530eSRafal Jaworowski sc->tx_ic_count = 16;
145bd37530eSRafal Jaworowski tsec_set_rxic(sc);
146bd37530eSRafal Jaworowski tsec_set_txic(sc);
147bd37530eSRafal Jaworowski tsec_add_sysctls(sc);
148bd37530eSRafal Jaworowski
149321e12c8SRafal Jaworowski /* Allocate a busdma tag and DMA safe memory for TX descriptors. */
150bd37530eSRafal Jaworowski error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_tx_dtag,
151bd37530eSRafal Jaworowski &sc->tsec_tx_dmap, sizeof(*sc->tsec_tx_vaddr) * TSEC_TX_NUM_DESC,
152321e12c8SRafal Jaworowski (void **)&sc->tsec_tx_vaddr, &sc->tsec_tx_raddr, "TX");
153bd37530eSRafal Jaworowski
154321e12c8SRafal Jaworowski if (error) {
155321e12c8SRafal Jaworowski tsec_detach(sc);
156321e12c8SRafal Jaworowski return (ENXIO);
157ecb1ab17SRafal Jaworowski }
158ecb1ab17SRafal Jaworowski
159321e12c8SRafal Jaworowski /* Allocate a busdma tag and DMA safe memory for RX descriptors. */
160bd37530eSRafal Jaworowski error = tsec_alloc_dma_desc(sc->dev, &sc->tsec_rx_dtag,
161bd37530eSRafal Jaworowski &sc->tsec_rx_dmap, sizeof(*sc->tsec_rx_vaddr) * TSEC_RX_NUM_DESC,
162321e12c8SRafal Jaworowski (void **)&sc->tsec_rx_vaddr, &sc->tsec_rx_raddr, "RX");
163321e12c8SRafal Jaworowski if (error) {
164321e12c8SRafal Jaworowski tsec_detach(sc);
165321e12c8SRafal Jaworowski return (ENXIO);
166321e12c8SRafal Jaworowski }
16767196661SRafal Jaworowski
168321e12c8SRafal Jaworowski /* Allocate a busdma tag for TX mbufs. */
169321e12c8SRafal Jaworowski error = bus_dma_tag_create(NULL, /* parent */
170321e12c8SRafal Jaworowski TSEC_TXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
171321e12c8SRafal Jaworowski BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
172321e12c8SRafal Jaworowski BUS_SPACE_MAXADDR, /* highaddr */
173321e12c8SRafal Jaworowski NULL, NULL, /* filtfunc, filtfuncarg */
174321e12c8SRafal Jaworowski MCLBYTES * (TSEC_TX_NUM_DESC - 1), /* maxsize */
1752c0dbbcbSJustin Hibbits TSEC_TX_MAX_DMA_SEGS, /* nsegments */
176321e12c8SRafal Jaworowski MCLBYTES, 0, /* maxsegsz, flags */
177321e12c8SRafal Jaworowski NULL, NULL, /* lockfunc, lockfuncarg */
178321e12c8SRafal Jaworowski &sc->tsec_tx_mtag); /* dmat */
179321e12c8SRafal Jaworowski if (error) {
18064f90c9dSRafal Jaworowski device_printf(sc->dev, "failed to allocate busdma tag "
18164f90c9dSRafal Jaworowski "(tx mbufs)\n");
182321e12c8SRafal Jaworowski tsec_detach(sc);
183321e12c8SRafal Jaworowski return (ENXIO);
184321e12c8SRafal Jaworowski }
185321e12c8SRafal Jaworowski
186321e12c8SRafal Jaworowski /* Allocate a busdma tag for RX mbufs. */
187321e12c8SRafal Jaworowski error = bus_dma_tag_create(NULL, /* parent */
188321e12c8SRafal Jaworowski TSEC_RXBUFFER_ALIGNMENT, 0, /* alignment, boundary */
189321e12c8SRafal Jaworowski BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
190321e12c8SRafal Jaworowski BUS_SPACE_MAXADDR, /* highaddr */
191321e12c8SRafal Jaworowski NULL, NULL, /* filtfunc, filtfuncarg */
192321e12c8SRafal Jaworowski MCLBYTES, /* maxsize */
193321e12c8SRafal Jaworowski 1, /* nsegments */
194321e12c8SRafal Jaworowski MCLBYTES, 0, /* maxsegsz, flags */
195321e12c8SRafal Jaworowski NULL, NULL, /* lockfunc, lockfuncarg */
196321e12c8SRafal Jaworowski &sc->tsec_rx_mtag); /* dmat */
197321e12c8SRafal Jaworowski if (error) {
19864f90c9dSRafal Jaworowski device_printf(sc->dev, "failed to allocate busdma tag "
19964f90c9dSRafal Jaworowski "(rx mbufs)\n");
200321e12c8SRafal Jaworowski tsec_detach(sc);
201321e12c8SRafal Jaworowski return (ENXIO);
202321e12c8SRafal Jaworowski }
203321e12c8SRafal Jaworowski
204321e12c8SRafal Jaworowski /* Create TX busdma maps */
205321e12c8SRafal Jaworowski for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
2062c0dbbcbSJustin Hibbits error = bus_dmamap_create(sc->tsec_tx_mtag, 0,
2072c0dbbcbSJustin Hibbits &sc->tx_bufmap[i].map);
208321e12c8SRafal Jaworowski if (error) {
209321e12c8SRafal Jaworowski device_printf(sc->dev, "failed to init TX ring\n");
210321e12c8SRafal Jaworowski tsec_detach(sc);
211321e12c8SRafal Jaworowski return (ENXIO);
212321e12c8SRafal Jaworowski }
2132c0dbbcbSJustin Hibbits sc->tx_bufmap[i].map_initialized = 1;
214321e12c8SRafal Jaworowski }
215321e12c8SRafal Jaworowski
216321e12c8SRafal Jaworowski /* Create RX busdma maps and zero mbuf handlers */
217321e12c8SRafal Jaworowski for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
21864f90c9dSRafal Jaworowski error = bus_dmamap_create(sc->tsec_rx_mtag, 0,
21964f90c9dSRafal Jaworowski &sc->rx_data[i].map);
220321e12c8SRafal Jaworowski if (error) {
221321e12c8SRafal Jaworowski device_printf(sc->dev, "failed to init RX ring\n");
222321e12c8SRafal Jaworowski tsec_detach(sc);
223321e12c8SRafal Jaworowski return (ENXIO);
224321e12c8SRafal Jaworowski }
225321e12c8SRafal Jaworowski sc->rx_data[i].mbuf = NULL;
226321e12c8SRafal Jaworowski }
227321e12c8SRafal Jaworowski
228321e12c8SRafal Jaworowski /* Create mbufs for RX buffers */
229321e12c8SRafal Jaworowski for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
230321e12c8SRafal Jaworowski error = tsec_new_rxbuf(sc->tsec_rx_mtag, sc->rx_data[i].map,
231321e12c8SRafal Jaworowski &sc->rx_data[i].mbuf, &sc->rx_data[i].paddr);
232321e12c8SRafal Jaworowski if (error) {
23364f90c9dSRafal Jaworowski device_printf(sc->dev, "can't load rx DMA map %d, "
23464f90c9dSRafal Jaworowski "error = %d\n", i, error);
235321e12c8SRafal Jaworowski tsec_detach(sc);
236321e12c8SRafal Jaworowski return (error);
237321e12c8SRafal Jaworowski }
238321e12c8SRafal Jaworowski }
239321e12c8SRafal Jaworowski
240321e12c8SRafal Jaworowski /* Create network interface for upper layers */
241321e12c8SRafal Jaworowski ifp = sc->tsec_ifp = if_alloc(IFT_ETHER);
24247842ecfSJustin Hibbits if_setsoftc(ifp, sc);
243321e12c8SRafal Jaworowski if_initname(ifp, device_get_name(sc->dev), device_get_unit(sc->dev));
24447842ecfSJustin Hibbits if_setflags(ifp, IFF_SIMPLEX | IFF_MULTICAST | IFF_BROADCAST);
24547842ecfSJustin Hibbits if_setinitfn(ifp, tsec_init);
24647842ecfSJustin Hibbits if_setstartfn(ifp, tsec_start);
24747842ecfSJustin Hibbits if_setioctlfn(ifp, tsec_ioctl);
248321e12c8SRafal Jaworowski
24947842ecfSJustin Hibbits if_setsendqlen(ifp, TSEC_TX_NUM_DESC - 1);
25047842ecfSJustin Hibbits if_setsendqready(ifp);
251321e12c8SRafal Jaworowski
25247842ecfSJustin Hibbits if_setcapabilities(ifp, IFCAP_VLAN_MTU);
253bd37530eSRafal Jaworowski if (sc->is_etsec)
25447842ecfSJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_HWCSUM, 0);
255bd37530eSRafal Jaworowski
25647842ecfSJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
257321e12c8SRafal Jaworowski
258bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
259bd37530eSRafal Jaworowski /* Advertise that polling is supported */
26047842ecfSJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
261bd37530eSRafal Jaworowski #endif
262bd37530eSRafal Jaworowski
2638e5d93dbSMarius Strobl /* Attach PHY(s) */
2648e5d93dbSMarius Strobl error = mii_attach(sc->dev, &sc->tsec_miibus, ifp, tsec_ifmedia_upd,
2658e5d93dbSMarius Strobl tsec_ifmedia_sts, BMSR_DEFCAPMASK, sc->phyaddr, MII_OFFSET_ANY,
2668e5d93dbSMarius Strobl 0);
267321e12c8SRafal Jaworowski if (error) {
2688e5d93dbSMarius Strobl device_printf(sc->dev, "attaching PHYs failed\n");
269321e12c8SRafal Jaworowski if_free(ifp);
270321e12c8SRafal Jaworowski sc->tsec_ifp = NULL;
271321e12c8SRafal Jaworowski tsec_detach(sc);
272321e12c8SRafal Jaworowski return (error);
273321e12c8SRafal Jaworowski }
274321e12c8SRafal Jaworowski sc->tsec_mii = device_get_softc(sc->tsec_miibus);
275321e12c8SRafal Jaworowski
276321e12c8SRafal Jaworowski /* Set MAC address */
277321e12c8SRafal Jaworowski tsec_get_hwaddr(sc, hwaddr);
278321e12c8SRafal Jaworowski ether_ifattach(ifp, hwaddr);
279321e12c8SRafal Jaworowski
280321e12c8SRafal Jaworowski return (0);
281321e12c8SRafal Jaworowski }
282321e12c8SRafal Jaworowski
283321e12c8SRafal Jaworowski int
tsec_detach(struct tsec_softc * sc)284321e12c8SRafal Jaworowski tsec_detach(struct tsec_softc *sc)
285321e12c8SRafal Jaworowski {
286321e12c8SRafal Jaworowski
28733518175SAndrew Thompson if (sc->tsec_ifp != NULL) {
288bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
289d8b78838SJustin Hibbits if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING)
290bd37530eSRafal Jaworowski ether_poll_deregister(sc->tsec_ifp);
291bd37530eSRafal Jaworowski #endif
292bd37530eSRafal Jaworowski
293321e12c8SRafal Jaworowski /* Stop TSEC controller and free TX queue */
29433518175SAndrew Thompson if (sc->sc_rres)
295321e12c8SRafal Jaworowski tsec_shutdown(sc->dev);
296321e12c8SRafal Jaworowski
297321e12c8SRafal Jaworowski /* Detach network interface */
298321e12c8SRafal Jaworowski ether_ifdetach(sc->tsec_ifp);
299321e12c8SRafal Jaworowski if_free(sc->tsec_ifp);
300321e12c8SRafal Jaworowski sc->tsec_ifp = NULL;
301321e12c8SRafal Jaworowski }
302321e12c8SRafal Jaworowski
303321e12c8SRafal Jaworowski /* Free DMA resources */
304321e12c8SRafal Jaworowski tsec_free_dma(sc);
305321e12c8SRafal Jaworowski
306321e12c8SRafal Jaworowski return (0);
307321e12c8SRafal Jaworowski }
308321e12c8SRafal Jaworowski
309661ee6eeSRafal Jaworowski int
tsec_shutdown(device_t dev)310321e12c8SRafal Jaworowski tsec_shutdown(device_t dev)
311321e12c8SRafal Jaworowski {
312321e12c8SRafal Jaworowski struct tsec_softc *sc;
313321e12c8SRafal Jaworowski
314321e12c8SRafal Jaworowski sc = device_get_softc(dev);
315321e12c8SRafal Jaworowski
316321e12c8SRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
317321e12c8SRafal Jaworowski tsec_stop(sc);
318321e12c8SRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
319661ee6eeSRafal Jaworowski return (0);
320321e12c8SRafal Jaworowski }
321321e12c8SRafal Jaworowski
322321e12c8SRafal Jaworowski int
tsec_suspend(device_t dev)323321e12c8SRafal Jaworowski tsec_suspend(device_t dev)
324321e12c8SRafal Jaworowski {
325321e12c8SRafal Jaworowski
326321e12c8SRafal Jaworowski /* TODO not implemented! */
327321e12c8SRafal Jaworowski return (0);
328321e12c8SRafal Jaworowski }
329321e12c8SRafal Jaworowski
330321e12c8SRafal Jaworowski int
tsec_resume(device_t dev)331321e12c8SRafal Jaworowski tsec_resume(device_t dev)
332321e12c8SRafal Jaworowski {
333321e12c8SRafal Jaworowski
334321e12c8SRafal Jaworowski /* TODO not implemented! */
335321e12c8SRafal Jaworowski return (0);
33667196661SRafal Jaworowski }
33767196661SRafal Jaworowski
33867196661SRafal Jaworowski static void
tsec_init(void * xsc)33967196661SRafal Jaworowski tsec_init(void *xsc)
34067196661SRafal Jaworowski {
34167196661SRafal Jaworowski struct tsec_softc *sc = xsc;
34267196661SRafal Jaworowski
34367196661SRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
34467196661SRafal Jaworowski tsec_init_locked(sc);
34567196661SRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
34667196661SRafal Jaworowski }
34767196661SRafal Jaworowski
34888011b59SJustin Hibbits static int
tsec_mii_wait(struct tsec_softc * sc,uint32_t flags)34988011b59SJustin Hibbits tsec_mii_wait(struct tsec_softc *sc, uint32_t flags)
35088011b59SJustin Hibbits {
35188011b59SJustin Hibbits int timeout;
35288011b59SJustin Hibbits
35388011b59SJustin Hibbits /*
3545c0b62dcSGordon Bergling * The status indicators are not set immediately after a command.
35588011b59SJustin Hibbits * Discard the first value.
35688011b59SJustin Hibbits */
35788011b59SJustin Hibbits TSEC_PHY_READ(sc, TSEC_REG_MIIMIND);
35888011b59SJustin Hibbits
35988011b59SJustin Hibbits timeout = TSEC_READ_RETRY;
36088011b59SJustin Hibbits while ((TSEC_PHY_READ(sc, TSEC_REG_MIIMIND) & flags) && --timeout)
36188011b59SJustin Hibbits DELAY(TSEC_READ_DELAY);
36288011b59SJustin Hibbits
36388011b59SJustin Hibbits return (timeout == 0);
36488011b59SJustin Hibbits }
36588011b59SJustin Hibbits
36667196661SRafal Jaworowski static void
tsec_init_locked(struct tsec_softc * sc)36767196661SRafal Jaworowski tsec_init_locked(struct tsec_softc *sc)
36867196661SRafal Jaworowski {
36967196661SRafal Jaworowski struct tsec_desc *tx_desc = sc->tsec_tx_vaddr;
37067196661SRafal Jaworowski struct tsec_desc *rx_desc = sc->tsec_rx_vaddr;
37147842ecfSJustin Hibbits if_t ifp = sc->tsec_ifp;
37288011b59SJustin Hibbits uint32_t val, i;
37388011b59SJustin Hibbits int timeout;
37467196661SRafal Jaworowski
37547842ecfSJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
376afceeed7SAndrew Thompson return;
377afceeed7SAndrew Thompson
37867196661SRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
37967196661SRafal Jaworowski tsec_stop(sc);
38067196661SRafal Jaworowski
38167196661SRafal Jaworowski /*
38267196661SRafal Jaworowski * These steps are according to the MPC8555E PowerQUICCIII RM:
38367196661SRafal Jaworowski * 14.7 Initialization/Application Information
38467196661SRafal Jaworowski */
38567196661SRafal Jaworowski
38667196661SRafal Jaworowski /* Step 1: soft reset MAC */
38767196661SRafal Jaworowski tsec_reset_mac(sc);
38867196661SRafal Jaworowski
38967196661SRafal Jaworowski /* Step 2: Initialize MACCFG2 */
39067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG2,
39167196661SRafal Jaworowski TSEC_MACCFG2_FULLDUPLEX | /* Full Duplex = 1 */
39267196661SRafal Jaworowski TSEC_MACCFG2_PADCRC | /* PAD/CRC append */
39367196661SRafal Jaworowski TSEC_MACCFG2_GMII | /* I/F Mode bit */
39467196661SRafal Jaworowski TSEC_MACCFG2_PRECNT /* Preamble count = 7 */
39567196661SRafal Jaworowski );
39667196661SRafal Jaworowski
39767196661SRafal Jaworowski /* Step 3: Initialize ECNTRL
39867196661SRafal Jaworowski * While the documentation states that R100M is ignored if RPM is
39967196661SRafal Jaworowski * not set, it does seem to be needed to get the orange boxes to
40067196661SRafal Jaworowski * work (which have a Marvell 88E1111 PHY). Go figure.
40167196661SRafal Jaworowski */
40267196661SRafal Jaworowski
40367196661SRafal Jaworowski /*
40467196661SRafal Jaworowski * XXX kludge - use circumstancial evidence to program ECNTRL
40567196661SRafal Jaworowski * correctly. Ideally we need some board information to guide
40667196661SRafal Jaworowski * us here.
40767196661SRafal Jaworowski */
40867196661SRafal Jaworowski i = TSEC_READ(sc, TSEC_REG_ID2);
40967196661SRafal Jaworowski val = (i & 0xffff)
41067196661SRafal Jaworowski ? (TSEC_ECNTRL_TBIM | TSEC_ECNTRL_SGMIIM) /* Sumatra */
41167196661SRafal Jaworowski : TSEC_ECNTRL_R100M; /* Orange + CDS */
41267196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_ECNTRL, TSEC_ECNTRL_STEN | val);
41367196661SRafal Jaworowski
41467196661SRafal Jaworowski /* Step 4: Initialize MAC station address */
41567196661SRafal Jaworowski tsec_set_mac_address(sc);
41667196661SRafal Jaworowski
41767196661SRafal Jaworowski /*
41867196661SRafal Jaworowski * Step 5: Assign a Physical address to the TBI so as to not conflict
41967196661SRafal Jaworowski * with the external PHY physical address
42067196661SRafal Jaworowski */
42167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TBIPA, 5);
42267196661SRafal Jaworowski
423629aa519SNathan Whitehorn TSEC_PHY_LOCK(sc);
424629aa519SNathan Whitehorn
42567196661SRafal Jaworowski /* Step 6: Reset the management interface */
426629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_RESETMGMT);
42767196661SRafal Jaworowski
42867196661SRafal Jaworowski /* Step 7: Setup the MII Mgmt clock speed */
429629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCFG, TSEC_MIIMCFG_CLKDIV28);
43067196661SRafal Jaworowski
43167196661SRafal Jaworowski /* Step 8: Read MII Mgmt indicator register and check for Busy = 0 */
43288011b59SJustin Hibbits timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY);
43388011b59SJustin Hibbits
43488011b59SJustin Hibbits TSEC_PHY_UNLOCK(sc);
43588011b59SJustin Hibbits if (timeout) {
43667196661SRafal Jaworowski if_printf(ifp, "tsec_init_locked(): Mgmt busy timeout\n");
43767196661SRafal Jaworowski return;
43867196661SRafal Jaworowski }
43967196661SRafal Jaworowski
44067196661SRafal Jaworowski /* Step 9: Setup the MII Mgmt */
44167196661SRafal Jaworowski mii_mediachg(sc->tsec_mii);
44267196661SRafal Jaworowski
44367196661SRafal Jaworowski /* Step 10: Clear IEVENT register */
44467196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT, 0xffffffff);
44567196661SRafal Jaworowski
446bd37530eSRafal Jaworowski /* Step 11: Enable interrupts */
447bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
448bd37530eSRafal Jaworowski /*
449bd37530eSRafal Jaworowski * ...only if polling is not turned on. Disable interrupts explicitly
450bd37530eSRafal Jaworowski * if polling is enabled.
451bd37530eSRafal Jaworowski */
45247842ecfSJustin Hibbits if (if_getcapenable(ifp) & IFCAP_POLLING )
453bd37530eSRafal Jaworowski tsec_intrs_ctl(sc, 0);
454bd37530eSRafal Jaworowski else
455bd37530eSRafal Jaworowski #endif /* DEVICE_POLLING */
45667196661SRafal Jaworowski tsec_intrs_ctl(sc, 1);
45767196661SRafal Jaworowski
45867196661SRafal Jaworowski /* Step 12: Initialize IADDRn */
45967196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR0, 0);
46067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR1, 0);
46167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR2, 0);
46267196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR3, 0);
46367196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR4, 0);
46467196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR5, 0);
46567196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR6, 0);
46667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IADDR7, 0);
46767196661SRafal Jaworowski
46867196661SRafal Jaworowski /* Step 13: Initialize GADDRn */
46967196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR0, 0);
47067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR1, 0);
47167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR2, 0);
47267196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR3, 0);
47367196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR4, 0);
47467196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR5, 0);
47567196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR6, 0);
47667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR7, 0);
47767196661SRafal Jaworowski
47867196661SRafal Jaworowski /* Step 14: Initialize RCTRL */
47967196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RCTRL, 0);
48067196661SRafal Jaworowski
48167196661SRafal Jaworowski /* Step 15: Initialize DMACTRL */
48267196661SRafal Jaworowski tsec_dma_ctl(sc, 1);
48367196661SRafal Jaworowski
48467196661SRafal Jaworowski /* Step 16: Initialize FIFO_PAUSE_CTRL */
48567196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_FIFO_PAUSE_CTRL, TSEC_FIFO_PAUSE_CTRL_EN);
48667196661SRafal Jaworowski
48767196661SRafal Jaworowski /*
48867196661SRafal Jaworowski * Step 17: Initialize transmit/receive descriptor rings.
48967196661SRafal Jaworowski * Initialize TBASE and RBASE.
49067196661SRafal Jaworowski */
49167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TBASE, sc->tsec_tx_raddr);
49267196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RBASE, sc->tsec_rx_raddr);
49367196661SRafal Jaworowski
49467196661SRafal Jaworowski for (i = 0; i < TSEC_TX_NUM_DESC; i++) {
49567196661SRafal Jaworowski tx_desc[i].bufptr = 0;
49667196661SRafal Jaworowski tx_desc[i].length = 0;
49764f90c9dSRafal Jaworowski tx_desc[i].flags = ((i == TSEC_TX_NUM_DESC - 1) ?
49864f90c9dSRafal Jaworowski TSEC_TXBD_W : 0);
49967196661SRafal Jaworowski }
500321e12c8SRafal Jaworowski bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
501321e12c8SRafal Jaworowski BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
50267196661SRafal Jaworowski
50367196661SRafal Jaworowski for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
50467196661SRafal Jaworowski rx_desc[i].bufptr = sc->rx_data[i].paddr;
50567196661SRafal Jaworowski rx_desc[i].length = 0;
50667196661SRafal Jaworowski rx_desc[i].flags = TSEC_RXBD_E | TSEC_RXBD_I |
50767196661SRafal Jaworowski ((i == TSEC_RX_NUM_DESC - 1) ? TSEC_RXBD_W : 0);
50867196661SRafal Jaworowski }
509bd37530eSRafal Jaworowski bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
510bd37530eSRafal Jaworowski BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
51167196661SRafal Jaworowski
512bd37530eSRafal Jaworowski /* Step 18: Initialize the maximum receive buffer length */
513bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MRBLR, MCLBYTES);
51467196661SRafal Jaworowski
515bd37530eSRafal Jaworowski /* Step 19: Configure ethernet frame sizes */
516bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MINFLR, TSEC_MIN_FRAME_SIZE);
51747842ecfSJustin Hibbits tsec_set_mtu(sc, if_getmtu(ifp));
518bd37530eSRafal Jaworowski
519bd37530eSRafal Jaworowski /* Step 20: Enable Rx and RxBD sdata snooping */
52067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_ATTR, TSEC_ATTR_RDSEN | TSEC_ATTR_RBDSEN);
52167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_ATTRELI, 0);
52267196661SRafal Jaworowski
523bd37530eSRafal Jaworowski /* Step 21: Reset collision counters in hardware */
52467196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
52567196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
52667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
52767196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
52867196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
52967196661SRafal Jaworowski
530bd37530eSRafal Jaworowski /* Step 22: Mask all CAM interrupts */
53167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_CAM1, 0xffffffff);
53267196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_CAM2, 0xffffffff);
53367196661SRafal Jaworowski
534bd37530eSRafal Jaworowski /* Step 23: Enable Rx and Tx */
53567196661SRafal Jaworowski val = TSEC_READ(sc, TSEC_REG_MACCFG1);
53667196661SRafal Jaworowski val |= (TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
53767196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG1, val);
53867196661SRafal Jaworowski
539bd37530eSRafal Jaworowski /* Step 24: Reset TSEC counters for Tx and Rx rings */
54067196661SRafal Jaworowski TSEC_TX_RX_COUNTERS_INIT(sc);
54167196661SRafal Jaworowski
542bd37530eSRafal Jaworowski /* Step 25: Setup TCP/IP Off-Load engine */
543bd37530eSRafal Jaworowski if (sc->is_etsec)
544bd37530eSRafal Jaworowski tsec_offload_setup(sc);
545bd37530eSRafal Jaworowski
546bd37530eSRafal Jaworowski /* Step 26: Setup multicast filters */
547bd37530eSRafal Jaworowski tsec_setup_multicast(sc);
548bd37530eSRafal Jaworowski
549bd37530eSRafal Jaworowski /* Step 27: Activate network interface */
55047842ecfSJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
55147842ecfSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
55247842ecfSJustin Hibbits sc->tsec_if_flags = if_getflags(ifp);
5535432bd9fSRafal Jaworowski sc->tsec_watchdog = 0;
554772619e1SRafal Jaworowski
555772619e1SRafal Jaworowski /* Schedule watchdog timeout */
5565432bd9fSRafal Jaworowski callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
55767196661SRafal Jaworowski }
55867196661SRafal Jaworowski
55967196661SRafal Jaworowski static void
tsec_set_mac_address(struct tsec_softc * sc)56067196661SRafal Jaworowski tsec_set_mac_address(struct tsec_softc *sc)
56167196661SRafal Jaworowski {
56267196661SRafal Jaworowski uint32_t macbuf[2] = { 0, 0 };
56364f90c9dSRafal Jaworowski char *macbufp, *curmac;
564321e12c8SRafal Jaworowski int i;
56567196661SRafal Jaworowski
56667196661SRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
56767196661SRafal Jaworowski
56867196661SRafal Jaworowski KASSERT((ETHER_ADDR_LEN <= sizeof(macbuf)),
56938f004fbSJustin Hibbits ("tsec_set_mac_address: (%d <= %zd", ETHER_ADDR_LEN,
57064f90c9dSRafal Jaworowski sizeof(macbuf)));
57167196661SRafal Jaworowski
57267196661SRafal Jaworowski macbufp = (char *)macbuf;
57347842ecfSJustin Hibbits curmac = (char *)if_getlladdr(sc->tsec_ifp);
57467196661SRafal Jaworowski
57567196661SRafal Jaworowski /* Correct order of MAC address bytes */
57667196661SRafal Jaworowski for (i = 1; i <= ETHER_ADDR_LEN; i++)
57767196661SRafal Jaworowski macbufp[ETHER_ADDR_LEN-i] = curmac[i-1];
57867196661SRafal Jaworowski
57967196661SRafal Jaworowski /* Initialize MAC station address MACSTNADDR2 and MACSTNADDR1 */
58067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACSTNADDR2, macbuf[1]);
58167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACSTNADDR1, macbuf[0]);
58267196661SRafal Jaworowski }
58367196661SRafal Jaworowski
58467196661SRafal Jaworowski /*
58567196661SRafal Jaworowski * DMA control function, if argument state is:
58667196661SRafal Jaworowski * 0 - DMA engine will be disabled
58767196661SRafal Jaworowski * 1 - DMA engine will be enabled
58867196661SRafal Jaworowski */
58967196661SRafal Jaworowski static void
tsec_dma_ctl(struct tsec_softc * sc,int state)59067196661SRafal Jaworowski tsec_dma_ctl(struct tsec_softc *sc, int state)
59167196661SRafal Jaworowski {
59267196661SRafal Jaworowski device_t dev;
59364f90c9dSRafal Jaworowski uint32_t dma_flags, timeout;
59467196661SRafal Jaworowski
59567196661SRafal Jaworowski dev = sc->dev;
59667196661SRafal Jaworowski
59767196661SRafal Jaworowski dma_flags = TSEC_READ(sc, TSEC_REG_DMACTRL);
59867196661SRafal Jaworowski
59967196661SRafal Jaworowski switch (state) {
60067196661SRafal Jaworowski case 0:
60167196661SRafal Jaworowski /* Temporarily clear stop graceful stop bits. */
60267196661SRafal Jaworowski tsec_dma_ctl(sc, 1000);
60367196661SRafal Jaworowski
60467196661SRafal Jaworowski /* Set it again */
60567196661SRafal Jaworowski dma_flags |= (TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
60667196661SRafal Jaworowski break;
60767196661SRafal Jaworowski case 1000:
60867196661SRafal Jaworowski case 1:
60967196661SRafal Jaworowski /* Set write with response (WWR), wait (WOP) and snoop bits */
61067196661SRafal Jaworowski dma_flags |= (TSEC_DMACTRL_TDSEN | TSEC_DMACTRL_TBDSEN |
61167196661SRafal Jaworowski DMACTRL_WWR | DMACTRL_WOP);
61267196661SRafal Jaworowski
61367196661SRafal Jaworowski /* Clear graceful stop bits */
61467196661SRafal Jaworowski dma_flags &= ~(TSEC_DMACTRL_GRS | TSEC_DMACTRL_GTS);
61567196661SRafal Jaworowski break;
61667196661SRafal Jaworowski default:
61767196661SRafal Jaworowski device_printf(dev, "tsec_dma_ctl(): unknown state value: %d\n",
61867196661SRafal Jaworowski state);
61967196661SRafal Jaworowski }
62067196661SRafal Jaworowski
62167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_DMACTRL, dma_flags);
62267196661SRafal Jaworowski
62367196661SRafal Jaworowski switch (state) {
62467196661SRafal Jaworowski case 0:
62567196661SRafal Jaworowski /* Wait for DMA stop */
62667196661SRafal Jaworowski timeout = TSEC_READ_RETRY;
62767196661SRafal Jaworowski while (--timeout && (!(TSEC_READ(sc, TSEC_REG_IEVENT) &
62867196661SRafal Jaworowski (TSEC_IEVENT_GRSC | TSEC_IEVENT_GTSC))))
62967196661SRafal Jaworowski DELAY(TSEC_READ_DELAY);
63067196661SRafal Jaworowski
63167196661SRafal Jaworowski if (timeout == 0)
63267196661SRafal Jaworowski device_printf(dev, "tsec_dma_ctl(): timeout!\n");
63367196661SRafal Jaworowski break;
63467196661SRafal Jaworowski case 1:
63567196661SRafal Jaworowski /* Restart transmission function */
63667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
63767196661SRafal Jaworowski }
63867196661SRafal Jaworowski }
63967196661SRafal Jaworowski
64067196661SRafal Jaworowski /*
64167196661SRafal Jaworowski * Interrupts control function, if argument state is:
64267196661SRafal Jaworowski * 0 - all TSEC interrupts will be masked
64367196661SRafal Jaworowski * 1 - all TSEC interrupts will be unmasked
64467196661SRafal Jaworowski */
64567196661SRafal Jaworowski static void
tsec_intrs_ctl(struct tsec_softc * sc,int state)64667196661SRafal Jaworowski tsec_intrs_ctl(struct tsec_softc *sc, int state)
64767196661SRafal Jaworowski {
64867196661SRafal Jaworowski device_t dev;
64967196661SRafal Jaworowski
65067196661SRafal Jaworowski dev = sc->dev;
65167196661SRafal Jaworowski
65267196661SRafal Jaworowski switch (state) {
65367196661SRafal Jaworowski case 0:
65467196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IMASK, 0);
65567196661SRafal Jaworowski break;
65667196661SRafal Jaworowski case 1:
65764f90c9dSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IMASK, TSEC_IMASK_BREN |
65864f90c9dSRafal Jaworowski TSEC_IMASK_RXCEN | TSEC_IMASK_BSYEN | TSEC_IMASK_EBERREN |
65964f90c9dSRafal Jaworowski TSEC_IMASK_BTEN | TSEC_IMASK_TXEEN | TSEC_IMASK_TXBEN |
66064f90c9dSRafal Jaworowski TSEC_IMASK_TXFEN | TSEC_IMASK_XFUNEN | TSEC_IMASK_RXFEN);
66167196661SRafal Jaworowski break;
66267196661SRafal Jaworowski default:
66367196661SRafal Jaworowski device_printf(dev, "tsec_intrs_ctl(): unknown state value: %d\n",
66467196661SRafal Jaworowski state);
66567196661SRafal Jaworowski }
66667196661SRafal Jaworowski }
66767196661SRafal Jaworowski
66867196661SRafal Jaworowski static void
tsec_reset_mac(struct tsec_softc * sc)66967196661SRafal Jaworowski tsec_reset_mac(struct tsec_softc *sc)
67067196661SRafal Jaworowski {
67167196661SRafal Jaworowski uint32_t maccfg1_flags;
67267196661SRafal Jaworowski
67367196661SRafal Jaworowski /* Set soft reset bit */
67467196661SRafal Jaworowski maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
67567196661SRafal Jaworowski maccfg1_flags |= TSEC_MACCFG1_SOFT_RESET;
67667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
67767196661SRafal Jaworowski
67867196661SRafal Jaworowski /* Clear soft reset bit */
67967196661SRafal Jaworowski maccfg1_flags = TSEC_READ(sc, TSEC_REG_MACCFG1);
68067196661SRafal Jaworowski maccfg1_flags &= ~TSEC_MACCFG1_SOFT_RESET;
68167196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG1, maccfg1_flags);
68267196661SRafal Jaworowski }
68367196661SRafal Jaworowski
68467196661SRafal Jaworowski static void
tsec_watchdog(struct tsec_softc * sc)685772619e1SRafal Jaworowski tsec_watchdog(struct tsec_softc *sc)
68667196661SRafal Jaworowski {
68747842ecfSJustin Hibbits if_t ifp;
68867196661SRafal Jaworowski
689772619e1SRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
69067196661SRafal Jaworowski
6915432bd9fSRafal Jaworowski if (sc->tsec_watchdog == 0 || --sc->tsec_watchdog > 0)
692772619e1SRafal Jaworowski return;
693772619e1SRafal Jaworowski
694772619e1SRafal Jaworowski ifp = sc->tsec_ifp;
695c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
69667196661SRafal Jaworowski if_printf(ifp, "watchdog timeout\n");
69767196661SRafal Jaworowski
69867196661SRafal Jaworowski tsec_stop(sc);
69967196661SRafal Jaworowski tsec_init_locked(sc);
70067196661SRafal Jaworowski }
70167196661SRafal Jaworowski
70267196661SRafal Jaworowski static void
tsec_start(if_t ifp)70347842ecfSJustin Hibbits tsec_start(if_t ifp)
70467196661SRafal Jaworowski {
70547842ecfSJustin Hibbits struct tsec_softc *sc = if_getsoftc(ifp);
70667196661SRafal Jaworowski
70767196661SRafal Jaworowski TSEC_TRANSMIT_LOCK(sc);
70867196661SRafal Jaworowski tsec_start_locked(ifp);
70967196661SRafal Jaworowski TSEC_TRANSMIT_UNLOCK(sc);
71067196661SRafal Jaworowski }
71167196661SRafal Jaworowski
71267196661SRafal Jaworowski static void
tsec_start_locked(if_t ifp)71347842ecfSJustin Hibbits tsec_start_locked(if_t ifp)
71467196661SRafal Jaworowski {
71567196661SRafal Jaworowski struct tsec_softc *sc;
7162c0dbbcbSJustin Hibbits struct mbuf *m0;
717bd37530eSRafal Jaworowski struct tsec_tx_fcb *tx_fcb;
7182c0dbbcbSJustin Hibbits int csum_flags;
7192c0dbbcbSJustin Hibbits int start_tx;
7202c0dbbcbSJustin Hibbits uint16_t fcb_flags;
72167196661SRafal Jaworowski
72247842ecfSJustin Hibbits sc = if_getsoftc(ifp);
7232c0dbbcbSJustin Hibbits start_tx = 0;
72467196661SRafal Jaworowski
72567196661SRafal Jaworowski TSEC_TRANSMIT_LOCK_ASSERT(sc);
72667196661SRafal Jaworowski
72767196661SRafal Jaworowski if (sc->tsec_link == 0)
72867196661SRafal Jaworowski return;
72967196661SRafal Jaworowski
73064f90c9dSRafal Jaworowski bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
73164f90c9dSRafal Jaworowski BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
73267196661SRafal Jaworowski
7332c0dbbcbSJustin Hibbits for (;;) {
7342c0dbbcbSJustin Hibbits if (TSEC_FREE_TX_DESC(sc) < TSEC_TX_MAX_DMA_SEGS) {
7352c0dbbcbSJustin Hibbits /* No free descriptors */
73647842ecfSJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
7372c0dbbcbSJustin Hibbits break;
7382c0dbbcbSJustin Hibbits }
7392c0dbbcbSJustin Hibbits
74067196661SRafal Jaworowski /* Get packet from the queue */
74147842ecfSJustin Hibbits m0 = if_dequeue(ifp);
74267196661SRafal Jaworowski if (m0 == NULL)
74367196661SRafal Jaworowski break;
74467196661SRafal Jaworowski
745bd37530eSRafal Jaworowski /* Insert TCP/IP Off-load frame control block */
7462c0dbbcbSJustin Hibbits fcb_flags = 0;
747bd37530eSRafal Jaworowski csum_flags = m0->m_pkthdr.csum_flags;
748bd37530eSRafal Jaworowski if (csum_flags) {
749c6499eccSGleb Smirnoff M_PREPEND(m0, sizeof(struct tsec_tx_fcb), M_NOWAIT);
750bd37530eSRafal Jaworowski if (m0 == NULL)
751bd37530eSRafal Jaworowski break;
752bd37530eSRafal Jaworowski
753bd37530eSRafal Jaworowski if (csum_flags & CSUM_IP)
7542c0dbbcbSJustin Hibbits fcb_flags |= TSEC_TX_FCB_IP4 |
755bd37530eSRafal Jaworowski TSEC_TX_FCB_CSUM_IP;
756bd37530eSRafal Jaworowski
757bd37530eSRafal Jaworowski if (csum_flags & CSUM_TCP)
7582c0dbbcbSJustin Hibbits fcb_flags |= TSEC_TX_FCB_TCP |
759bd37530eSRafal Jaworowski TSEC_TX_FCB_CSUM_TCP_UDP;
760bd37530eSRafal Jaworowski
761bd37530eSRafal Jaworowski if (csum_flags & CSUM_UDP)
7622c0dbbcbSJustin Hibbits fcb_flags |= TSEC_TX_FCB_UDP |
763bd37530eSRafal Jaworowski TSEC_TX_FCB_CSUM_TCP_UDP;
764bd37530eSRafal Jaworowski
7652c0dbbcbSJustin Hibbits tx_fcb = mtod(m0, struct tsec_tx_fcb *);
7662c0dbbcbSJustin Hibbits tx_fcb->flags = fcb_flags;
7672c0dbbcbSJustin Hibbits tx_fcb->l3_offset = ETHER_HDR_LEN;
7682c0dbbcbSJustin Hibbits tx_fcb->l4_offset = sizeof(struct ip);
769bd37530eSRafal Jaworowski }
770bd37530eSRafal Jaworowski
7712c0dbbcbSJustin Hibbits tsec_encap(ifp, sc, m0, fcb_flags, &start_tx);
77267196661SRafal Jaworowski }
77364f90c9dSRafal Jaworowski bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
77464f90c9dSRafal Jaworowski BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
77567196661SRafal Jaworowski
7762c0dbbcbSJustin Hibbits if (start_tx) {
77767196661SRafal Jaworowski /* Enable transmitter and watchdog timer */
77867196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
7795432bd9fSRafal Jaworowski sc->tsec_watchdog = 5;
78067196661SRafal Jaworowski }
78167196661SRafal Jaworowski }
78267196661SRafal Jaworowski
7832c0dbbcbSJustin Hibbits static void
tsec_encap(if_t ifp,struct tsec_softc * sc,struct mbuf * m0,uint16_t fcb_flags,int * start_tx)78447842ecfSJustin Hibbits tsec_encap(if_t ifp, struct tsec_softc *sc, struct mbuf *m0,
7852c0dbbcbSJustin Hibbits uint16_t fcb_flags, int *start_tx)
78667196661SRafal Jaworowski {
7872c0dbbcbSJustin Hibbits bus_dma_segment_t segs[TSEC_TX_MAX_DMA_SEGS];
7882c0dbbcbSJustin Hibbits int error, i, nsegs;
7892c0dbbcbSJustin Hibbits struct tsec_bufmap *tx_bufmap;
7902c0dbbcbSJustin Hibbits uint32_t tx_idx;
7912c0dbbcbSJustin Hibbits uint16_t flags;
79267196661SRafal Jaworowski
79367196661SRafal Jaworowski TSEC_TRANSMIT_LOCK_ASSERT(sc);
79467196661SRafal Jaworowski
7952c0dbbcbSJustin Hibbits tx_idx = sc->tx_idx_head;
7962c0dbbcbSJustin Hibbits tx_bufmap = &sc->tx_bufmap[tx_idx];
79767196661SRafal Jaworowski
79867196661SRafal Jaworowski /* Create mapping in DMA memory */
7992c0dbbcbSJustin Hibbits error = bus_dmamap_load_mbuf_sg(sc->tsec_tx_mtag, tx_bufmap->map, m0,
8002c0dbbcbSJustin Hibbits segs, &nsegs, BUS_DMA_NOWAIT);
8012c0dbbcbSJustin Hibbits if (error == EFBIG) {
8022c0dbbcbSJustin Hibbits /* Too many segments! Defrag and try again. */
8032c0dbbcbSJustin Hibbits struct mbuf *m = m_defrag(m0, M_NOWAIT);
8042c0dbbcbSJustin Hibbits
8052c0dbbcbSJustin Hibbits if (m == NULL) {
8062c0dbbcbSJustin Hibbits m_freem(m0);
8072c0dbbcbSJustin Hibbits return;
80867196661SRafal Jaworowski }
8092c0dbbcbSJustin Hibbits m0 = m;
8102c0dbbcbSJustin Hibbits error = bus_dmamap_load_mbuf_sg(sc->tsec_tx_mtag,
8112c0dbbcbSJustin Hibbits tx_bufmap->map, m0, segs, &nsegs, BUS_DMA_NOWAIT);
8122c0dbbcbSJustin Hibbits }
8132c0dbbcbSJustin Hibbits if (error != 0) {
8142c0dbbcbSJustin Hibbits /* Give up. */
8152c0dbbcbSJustin Hibbits m_freem(m0);
8162c0dbbcbSJustin Hibbits return;
8172c0dbbcbSJustin Hibbits }
81867196661SRafal Jaworowski
8192c0dbbcbSJustin Hibbits bus_dmamap_sync(sc->tsec_tx_mtag, tx_bufmap->map,
8202c0dbbcbSJustin Hibbits BUS_DMASYNC_PREWRITE);
8212c0dbbcbSJustin Hibbits tx_bufmap->mbuf = m0;
82267196661SRafal Jaworowski
8232c0dbbcbSJustin Hibbits /*
8242c0dbbcbSJustin Hibbits * Fill in the TX descriptors back to front so that READY bit in first
8252c0dbbcbSJustin Hibbits * descriptor is set last.
8262c0dbbcbSJustin Hibbits */
8272c0dbbcbSJustin Hibbits tx_idx = (tx_idx + (uint32_t)nsegs) & (TSEC_TX_NUM_DESC - 1);
8282c0dbbcbSJustin Hibbits sc->tx_idx_head = tx_idx;
8292c0dbbcbSJustin Hibbits flags = TSEC_TXBD_L | TSEC_TXBD_I | TSEC_TXBD_R | TSEC_TXBD_TC;
8302c0dbbcbSJustin Hibbits for (i = nsegs - 1; i >= 0; i--) {
8312c0dbbcbSJustin Hibbits struct tsec_desc *tx_desc;
832bd37530eSRafal Jaworowski
8332c0dbbcbSJustin Hibbits tx_idx = (tx_idx - 1) & (TSEC_TX_NUM_DESC - 1);
8342c0dbbcbSJustin Hibbits tx_desc = &sc->tsec_tx_vaddr[tx_idx];
8352c0dbbcbSJustin Hibbits tx_desc->length = segs[i].ds_len;
8362c0dbbcbSJustin Hibbits tx_desc->bufptr = segs[i].ds_addr;
83767196661SRafal Jaworowski
8382c0dbbcbSJustin Hibbits if (i == 0) {
8392c0dbbcbSJustin Hibbits wmb();
8402c0dbbcbSJustin Hibbits
8412c0dbbcbSJustin Hibbits if (fcb_flags != 0)
8422c0dbbcbSJustin Hibbits flags |= TSEC_TXBD_TOE;
8432c0dbbcbSJustin Hibbits }
84467196661SRafal Jaworowski
845bd37530eSRafal Jaworowski /*
846bd37530eSRafal Jaworowski * Set flags:
847bd37530eSRafal Jaworowski * - wrap
848bd37530eSRafal Jaworowski * - checksum
849bd37530eSRafal Jaworowski * - ready to send
850bd37530eSRafal Jaworowski * - transmit the CRC sequence after the last data byte
851bd37530eSRafal Jaworowski * - interrupt after the last buffer
852bd37530eSRafal Jaworowski */
8532c0dbbcbSJustin Hibbits tx_desc->flags = (tx_idx == (TSEC_TX_NUM_DESC - 1) ?
8542c0dbbcbSJustin Hibbits TSEC_TXBD_W : 0) | flags;
8552c0dbbcbSJustin Hibbits
8562c0dbbcbSJustin Hibbits flags &= ~(TSEC_TXBD_L | TSEC_TXBD_I);
85767196661SRafal Jaworowski }
85867196661SRafal Jaworowski
8592c0dbbcbSJustin Hibbits BPF_MTAP(ifp, m0);
8602c0dbbcbSJustin Hibbits *start_tx = 1;
86167196661SRafal Jaworowski }
86267196661SRafal Jaworowski
86367196661SRafal Jaworowski static void
tsec_setfilter(struct tsec_softc * sc)86467196661SRafal Jaworowski tsec_setfilter(struct tsec_softc *sc)
86567196661SRafal Jaworowski {
86647842ecfSJustin Hibbits if_t ifp;
86767196661SRafal Jaworowski uint32_t flags;
86867196661SRafal Jaworowski
86967196661SRafal Jaworowski ifp = sc->tsec_ifp;
87067196661SRafal Jaworowski flags = TSEC_READ(sc, TSEC_REG_RCTRL);
87167196661SRafal Jaworowski
87267196661SRafal Jaworowski /* Promiscuous mode */
87347842ecfSJustin Hibbits if (if_getflags(ifp) & IFF_PROMISC)
87467196661SRafal Jaworowski flags |= TSEC_RCTRL_PROM;
87567196661SRafal Jaworowski else
87667196661SRafal Jaworowski flags &= ~TSEC_RCTRL_PROM;
87767196661SRafal Jaworowski
87867196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RCTRL, flags);
87967196661SRafal Jaworowski }
88067196661SRafal Jaworowski
881bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
882bd37530eSRafal Jaworowski static poll_handler_t tsec_poll;
883bd37530eSRafal Jaworowski
8841abcdbd1SAttilio Rao static int
tsec_poll(if_t ifp,enum poll_cmd cmd,int count)88547842ecfSJustin Hibbits tsec_poll(if_t ifp, enum poll_cmd cmd, int count)
886bd37530eSRafal Jaworowski {
887bd37530eSRafal Jaworowski uint32_t ie;
88847842ecfSJustin Hibbits struct tsec_softc *sc = if_getsoftc(ifp);
8891abcdbd1SAttilio Rao int rx_npkts;
8901abcdbd1SAttilio Rao
8911abcdbd1SAttilio Rao rx_npkts = 0;
892bd37530eSRafal Jaworowski
893bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
89447842ecfSJustin Hibbits if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
895bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
8961abcdbd1SAttilio Rao return (rx_npkts);
897bd37530eSRafal Jaworowski }
898bd37530eSRafal Jaworowski
899bd37530eSRafal Jaworowski if (cmd == POLL_AND_CHECK_STATUS) {
9000390701aSRafal Jaworowski tsec_error_intr_locked(sc, count);
901bd37530eSRafal Jaworowski
902bd37530eSRafal Jaworowski /* Clear all events reported */
9030390701aSRafal Jaworowski ie = TSEC_READ(sc, TSEC_REG_IEVENT);
904bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT, ie);
905bd37530eSRafal Jaworowski }
906bd37530eSRafal Jaworowski
907bd37530eSRafal Jaworowski tsec_transmit_intr_locked(sc);
908bd37530eSRafal Jaworowski
909bd37530eSRafal Jaworowski TSEC_GLOBAL_TO_RECEIVE_LOCK(sc);
910bd37530eSRafal Jaworowski
9111abcdbd1SAttilio Rao rx_npkts = tsec_receive_intr_locked(sc, count);
912bd37530eSRafal Jaworowski
913bd37530eSRafal Jaworowski TSEC_RECEIVE_UNLOCK(sc);
9141abcdbd1SAttilio Rao
9151abcdbd1SAttilio Rao return (rx_npkts);
916bd37530eSRafal Jaworowski }
917bd37530eSRafal Jaworowski #endif /* DEVICE_POLLING */
918bd37530eSRafal Jaworowski
91967196661SRafal Jaworowski static int
tsec_ioctl(if_t ifp,u_long command,caddr_t data)92047842ecfSJustin Hibbits tsec_ioctl(if_t ifp, u_long command, caddr_t data)
92167196661SRafal Jaworowski {
92247842ecfSJustin Hibbits struct tsec_softc *sc = if_getsoftc(ifp);
92367196661SRafal Jaworowski struct ifreq *ifr = (struct ifreq *)data;
924bd37530eSRafal Jaworowski int mask, error = 0;
92567196661SRafal Jaworowski
92667196661SRafal Jaworowski switch (command) {
927bd37530eSRafal Jaworowski case SIOCSIFMTU:
928bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
929bd37530eSRafal Jaworowski if (tsec_set_mtu(sc, ifr->ifr_mtu))
93047842ecfSJustin Hibbits if_setmtu(ifp, ifr->ifr_mtu);
931bd37530eSRafal Jaworowski else
932bd37530eSRafal Jaworowski error = EINVAL;
933bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
934bd37530eSRafal Jaworowski break;
93567196661SRafal Jaworowski case SIOCSIFFLAGS:
93667196661SRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
93747842ecfSJustin Hibbits if (if_getflags(ifp) & IFF_UP) {
93847842ecfSJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
93947842ecfSJustin Hibbits if ((sc->tsec_if_flags ^ if_getflags(ifp)) &
940bd37530eSRafal Jaworowski IFF_PROMISC)
94167196661SRafal Jaworowski tsec_setfilter(sc);
942bd37530eSRafal Jaworowski
94347842ecfSJustin Hibbits if ((sc->tsec_if_flags ^ if_getflags(ifp)) &
944bd37530eSRafal Jaworowski IFF_ALLMULTI)
945bd37530eSRafal Jaworowski tsec_setup_multicast(sc);
94667196661SRafal Jaworowski } else
94767196661SRafal Jaworowski tsec_init_locked(sc);
94847842ecfSJustin Hibbits } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
94967196661SRafal Jaworowski tsec_stop(sc);
950321e12c8SRafal Jaworowski
95147842ecfSJustin Hibbits sc->tsec_if_flags = if_getflags(ifp);
95267196661SRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
95367196661SRafal Jaworowski break;
954bd37530eSRafal Jaworowski case SIOCADDMULTI:
955bd37530eSRafal Jaworowski case SIOCDELMULTI:
95647842ecfSJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
957bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
958bd37530eSRafal Jaworowski tsec_setup_multicast(sc);
959bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
960bd37530eSRafal Jaworowski }
96167196661SRafal Jaworowski case SIOCGIFMEDIA:
96267196661SRafal Jaworowski case SIOCSIFMEDIA:
963bd37530eSRafal Jaworowski error = ifmedia_ioctl(ifp, ifr, &sc->tsec_mii->mii_media,
964bd37530eSRafal Jaworowski command);
96567196661SRafal Jaworowski break;
966bd37530eSRafal Jaworowski case SIOCSIFCAP:
96747842ecfSJustin Hibbits mask = if_getcapenable(ifp) ^ ifr->ifr_reqcap;
968bd37530eSRafal Jaworowski if ((mask & IFCAP_HWCSUM) && sc->is_etsec) {
969bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
97047842ecfSJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_HWCSUM);
97147842ecfSJustin Hibbits if_setcapenablebit(ifp, IFCAP_HWCSUM & ifr->ifr_reqcap, 0);
972bd37530eSRafal Jaworowski tsec_offload_setup(sc);
973bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
974bd37530eSRafal Jaworowski }
975bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
976bd37530eSRafal Jaworowski if (mask & IFCAP_POLLING) {
977bd37530eSRafal Jaworowski if (ifr->ifr_reqcap & IFCAP_POLLING) {
978bd37530eSRafal Jaworowski error = ether_poll_register(tsec_poll, ifp);
979bd37530eSRafal Jaworowski if (error)
980bd37530eSRafal Jaworowski return (error);
981bd37530eSRafal Jaworowski
982bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
983bd37530eSRafal Jaworowski /* Disable interrupts */
984bd37530eSRafal Jaworowski tsec_intrs_ctl(sc, 0);
98547842ecfSJustin Hibbits if_setcapenablebit(ifp, IFCAP_POLLING, 0);
986bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
987bd37530eSRafal Jaworowski } else {
988bd37530eSRafal Jaworowski error = ether_poll_deregister(ifp);
989bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
990bd37530eSRafal Jaworowski /* Enable interrupts */
991bd37530eSRafal Jaworowski tsec_intrs_ctl(sc, 1);
99247842ecfSJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_POLLING);
993bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
994bd37530eSRafal Jaworowski }
995bd37530eSRafal Jaworowski }
996bd37530eSRafal Jaworowski #endif
997bd37530eSRafal Jaworowski break;
998bd37530eSRafal Jaworowski
99967196661SRafal Jaworowski default:
100067196661SRafal Jaworowski error = ether_ioctl(ifp, command, data);
100167196661SRafal Jaworowski }
100267196661SRafal Jaworowski
100367196661SRafal Jaworowski /* Flush buffers if not empty */
100447842ecfSJustin Hibbits if (if_getflags(ifp) & IFF_UP)
100567196661SRafal Jaworowski tsec_start(ifp);
100667196661SRafal Jaworowski return (error);
100767196661SRafal Jaworowski }
100867196661SRafal Jaworowski
100967196661SRafal Jaworowski static int
tsec_ifmedia_upd(if_t ifp)101047842ecfSJustin Hibbits tsec_ifmedia_upd(if_t ifp)
101167196661SRafal Jaworowski {
101247842ecfSJustin Hibbits struct tsec_softc *sc = if_getsoftc(ifp);
101367196661SRafal Jaworowski struct mii_data *mii;
101467196661SRafal Jaworowski
101567196661SRafal Jaworowski TSEC_TRANSMIT_LOCK(sc);
101667196661SRafal Jaworowski
101767196661SRafal Jaworowski mii = sc->tsec_mii;
101867196661SRafal Jaworowski mii_mediachg(mii);
101967196661SRafal Jaworowski
102067196661SRafal Jaworowski TSEC_TRANSMIT_UNLOCK(sc);
102167196661SRafal Jaworowski return (0);
102267196661SRafal Jaworowski }
102367196661SRafal Jaworowski
102467196661SRafal Jaworowski static void
tsec_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)102547842ecfSJustin Hibbits tsec_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
102667196661SRafal Jaworowski {
102747842ecfSJustin Hibbits struct tsec_softc *sc = if_getsoftc(ifp);
102867196661SRafal Jaworowski struct mii_data *mii;
102967196661SRafal Jaworowski
103067196661SRafal Jaworowski TSEC_TRANSMIT_LOCK(sc);
103167196661SRafal Jaworowski
103267196661SRafal Jaworowski mii = sc->tsec_mii;
103367196661SRafal Jaworowski mii_pollstat(mii);
103467196661SRafal Jaworowski
103567196661SRafal Jaworowski ifmr->ifm_active = mii->mii_media_active;
103667196661SRafal Jaworowski ifmr->ifm_status = mii->mii_media_status;
103767196661SRafal Jaworowski
103867196661SRafal Jaworowski TSEC_TRANSMIT_UNLOCK(sc);
103967196661SRafal Jaworowski }
104067196661SRafal Jaworowski
104167196661SRafal Jaworowski static int
tsec_new_rxbuf(bus_dma_tag_t tag,bus_dmamap_t map,struct mbuf ** mbufp,uint32_t * paddr)104267196661SRafal Jaworowski tsec_new_rxbuf(bus_dma_tag_t tag, bus_dmamap_t map, struct mbuf **mbufp,
104367196661SRafal Jaworowski uint32_t *paddr)
104467196661SRafal Jaworowski {
104567196661SRafal Jaworowski struct mbuf *new_mbuf;
104667196661SRafal Jaworowski bus_dma_segment_t seg[1];
1047bd37530eSRafal Jaworowski int error, nsegs;
104867196661SRafal Jaworowski
104967196661SRafal Jaworowski KASSERT(mbufp != NULL, ("NULL mbuf pointer!"));
105067196661SRafal Jaworowski
1051c6499eccSGleb Smirnoff new_mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MCLBYTES);
105267196661SRafal Jaworowski if (new_mbuf == NULL)
105367196661SRafal Jaworowski return (ENOBUFS);
105467196661SRafal Jaworowski new_mbuf->m_len = new_mbuf->m_pkthdr.len = new_mbuf->m_ext.ext_size;
105567196661SRafal Jaworowski
105667196661SRafal Jaworowski if (*mbufp) {
105767196661SRafal Jaworowski bus_dmamap_sync(tag, map, BUS_DMASYNC_POSTREAD);
105867196661SRafal Jaworowski bus_dmamap_unload(tag, map);
105967196661SRafal Jaworowski }
106067196661SRafal Jaworowski
106167196661SRafal Jaworowski error = bus_dmamap_load_mbuf_sg(tag, map, new_mbuf, seg, &nsegs,
106267196661SRafal Jaworowski BUS_DMA_NOWAIT);
106367196661SRafal Jaworowski KASSERT(nsegs == 1, ("Too many segments returned!"));
106467196661SRafal Jaworowski if (nsegs != 1 || error)
106567196661SRafal Jaworowski panic("tsec_new_rxbuf(): nsegs(%d), error(%d)", nsegs, error);
106667196661SRafal Jaworowski
106767196661SRafal Jaworowski #if 0
106867196661SRafal Jaworowski if (error) {
106967196661SRafal Jaworowski printf("tsec: bus_dmamap_load_mbuf_sg() returned: %d!\n",
107067196661SRafal Jaworowski error);
107167196661SRafal Jaworowski m_freem(new_mbuf);
107267196661SRafal Jaworowski return (ENOBUFS);
107367196661SRafal Jaworowski }
107467196661SRafal Jaworowski #endif
107567196661SRafal Jaworowski
107667196661SRafal Jaworowski #if 0
107767196661SRafal Jaworowski KASSERT(((seg->ds_addr) & (TSEC_RXBUFFER_ALIGNMENT-1)) == 0,
107867196661SRafal Jaworowski ("Wrong alignment of RX buffer!"));
107967196661SRafal Jaworowski #endif
108067196661SRafal Jaworowski bus_dmamap_sync(tag, map, BUS_DMASYNC_PREREAD);
108167196661SRafal Jaworowski
108267196661SRafal Jaworowski (*mbufp) = new_mbuf;
108367196661SRafal Jaworowski (*paddr) = seg->ds_addr;
108467196661SRafal Jaworowski return (0);
108567196661SRafal Jaworowski }
108667196661SRafal Jaworowski
108767196661SRafal Jaworowski static void
tsec_map_dma_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)108867196661SRafal Jaworowski tsec_map_dma_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
108967196661SRafal Jaworowski {
109067196661SRafal Jaworowski u_int32_t *paddr;
109167196661SRafal Jaworowski
109267196661SRafal Jaworowski KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
109367196661SRafal Jaworowski paddr = arg;
109467196661SRafal Jaworowski *paddr = segs->ds_addr;
109567196661SRafal Jaworowski }
109667196661SRafal Jaworowski
109767196661SRafal Jaworowski static int
tsec_alloc_dma_desc(device_t dev,bus_dma_tag_t * dtag,bus_dmamap_t * dmap,bus_size_t dsize,void ** vaddr,void * raddr,const char * dname)109867196661SRafal Jaworowski tsec_alloc_dma_desc(device_t dev, bus_dma_tag_t *dtag, bus_dmamap_t *dmap,
109967196661SRafal Jaworowski bus_size_t dsize, void **vaddr, void *raddr, const char *dname)
110067196661SRafal Jaworowski {
110167196661SRafal Jaworowski int error;
110267196661SRafal Jaworowski
110367196661SRafal Jaworowski /* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
110467196661SRafal Jaworowski error = bus_dma_tag_create(NULL, /* parent */
110567196661SRafal Jaworowski PAGE_SIZE, 0, /* alignment, boundary */
110667196661SRafal Jaworowski BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
110767196661SRafal Jaworowski BUS_SPACE_MAXADDR, /* highaddr */
110867196661SRafal Jaworowski NULL, NULL, /* filtfunc, filtfuncarg */
110967196661SRafal Jaworowski dsize, 1, /* maxsize, nsegments */
111067196661SRafal Jaworowski dsize, 0, /* maxsegsz, flags */
111167196661SRafal Jaworowski NULL, NULL, /* lockfunc, lockfuncarg */
111267196661SRafal Jaworowski dtag); /* dmat */
111367196661SRafal Jaworowski
111467196661SRafal Jaworowski if (error) {
111564f90c9dSRafal Jaworowski device_printf(dev, "failed to allocate busdma %s tag\n",
111664f90c9dSRafal Jaworowski dname);
111767196661SRafal Jaworowski (*vaddr) = NULL;
111867196661SRafal Jaworowski return (ENXIO);
111967196661SRafal Jaworowski }
112067196661SRafal Jaworowski
112167196661SRafal Jaworowski error = bus_dmamem_alloc(*dtag, vaddr, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
112267196661SRafal Jaworowski dmap);
112367196661SRafal Jaworowski if (error) {
112467196661SRafal Jaworowski device_printf(dev, "failed to allocate %s DMA safe memory\n",
112567196661SRafal Jaworowski dname);
112667196661SRafal Jaworowski bus_dma_tag_destroy(*dtag);
112767196661SRafal Jaworowski (*vaddr) = NULL;
112867196661SRafal Jaworowski return (ENXIO);
112967196661SRafal Jaworowski }
113067196661SRafal Jaworowski
113164f90c9dSRafal Jaworowski error = bus_dmamap_load(*dtag, *dmap, *vaddr, dsize,
113264f90c9dSRafal Jaworowski tsec_map_dma_addr, raddr, BUS_DMA_NOWAIT);
113367196661SRafal Jaworowski if (error) {
113464f90c9dSRafal Jaworowski device_printf(dev, "cannot get address of the %s "
113564f90c9dSRafal Jaworowski "descriptors\n", dname);
113667196661SRafal Jaworowski bus_dmamem_free(*dtag, *vaddr, *dmap);
113767196661SRafal Jaworowski bus_dma_tag_destroy(*dtag);
113867196661SRafal Jaworowski (*vaddr) = NULL;
113967196661SRafal Jaworowski return (ENXIO);
114067196661SRafal Jaworowski }
114167196661SRafal Jaworowski
114267196661SRafal Jaworowski return (0);
114367196661SRafal Jaworowski }
114467196661SRafal Jaworowski
114567196661SRafal Jaworowski static void
tsec_free_dma_desc(bus_dma_tag_t dtag,bus_dmamap_t dmap,void * vaddr)114667196661SRafal Jaworowski tsec_free_dma_desc(bus_dma_tag_t dtag, bus_dmamap_t dmap, void *vaddr)
114767196661SRafal Jaworowski {
114867196661SRafal Jaworowski
114967196661SRafal Jaworowski if (vaddr == NULL)
115067196661SRafal Jaworowski return;
115167196661SRafal Jaworowski
115267196661SRafal Jaworowski /* Unmap descriptors from DMA memory */
115364f90c9dSRafal Jaworowski bus_dmamap_sync(dtag, dmap, BUS_DMASYNC_POSTREAD |
115464f90c9dSRafal Jaworowski BUS_DMASYNC_POSTWRITE);
115567196661SRafal Jaworowski bus_dmamap_unload(dtag, dmap);
115667196661SRafal Jaworowski
115767196661SRafal Jaworowski /* Free descriptors memory */
115867196661SRafal Jaworowski bus_dmamem_free(dtag, vaddr, dmap);
115967196661SRafal Jaworowski
116067196661SRafal Jaworowski /* Destroy descriptors tag */
116167196661SRafal Jaworowski bus_dma_tag_destroy(dtag);
116267196661SRafal Jaworowski }
116367196661SRafal Jaworowski
116467196661SRafal Jaworowski static void
tsec_free_dma(struct tsec_softc * sc)116567196661SRafal Jaworowski tsec_free_dma(struct tsec_softc *sc)
116667196661SRafal Jaworowski {
116767196661SRafal Jaworowski int i;
116867196661SRafal Jaworowski
116967196661SRafal Jaworowski /* Free TX maps */
117067196661SRafal Jaworowski for (i = 0; i < TSEC_TX_NUM_DESC; i++)
11712c0dbbcbSJustin Hibbits if (sc->tx_bufmap[i].map_initialized)
117264f90c9dSRafal Jaworowski bus_dmamap_destroy(sc->tsec_tx_mtag,
11732c0dbbcbSJustin Hibbits sc->tx_bufmap[i].map);
117464f90c9dSRafal Jaworowski /* Destroy tag for TX mbufs */
117567196661SRafal Jaworowski bus_dma_tag_destroy(sc->tsec_tx_mtag);
117667196661SRafal Jaworowski
117767196661SRafal Jaworowski /* Free RX mbufs and maps */
117867196661SRafal Jaworowski for (i = 0; i < TSEC_RX_NUM_DESC; i++) {
117967196661SRafal Jaworowski if (sc->rx_data[i].mbuf) {
118067196661SRafal Jaworowski /* Unload buffer from DMA */
118167196661SRafal Jaworowski bus_dmamap_sync(sc->tsec_rx_mtag, sc->rx_data[i].map,
118267196661SRafal Jaworowski BUS_DMASYNC_POSTREAD);
118364f90c9dSRafal Jaworowski bus_dmamap_unload(sc->tsec_rx_mtag,
118464f90c9dSRafal Jaworowski sc->rx_data[i].map);
118567196661SRafal Jaworowski
118667196661SRafal Jaworowski /* Free buffer */
118767196661SRafal Jaworowski m_freem(sc->rx_data[i].mbuf);
118867196661SRafal Jaworowski }
118967196661SRafal Jaworowski /* Destroy map for this buffer */
119067196661SRafal Jaworowski if (sc->rx_data[i].map != NULL)
119167196661SRafal Jaworowski bus_dmamap_destroy(sc->tsec_rx_mtag,
119267196661SRafal Jaworowski sc->rx_data[i].map);
119367196661SRafal Jaworowski }
119464f90c9dSRafal Jaworowski /* Destroy tag for RX mbufs */
119567196661SRafal Jaworowski bus_dma_tag_destroy(sc->tsec_rx_mtag);
119667196661SRafal Jaworowski
119767196661SRafal Jaworowski /* Unload TX/RX descriptors */
119867196661SRafal Jaworowski tsec_free_dma_desc(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
119967196661SRafal Jaworowski sc->tsec_tx_vaddr);
120067196661SRafal Jaworowski tsec_free_dma_desc(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
120167196661SRafal Jaworowski sc->tsec_rx_vaddr);
120267196661SRafal Jaworowski }
120367196661SRafal Jaworowski
120467196661SRafal Jaworowski static void
tsec_stop(struct tsec_softc * sc)120567196661SRafal Jaworowski tsec_stop(struct tsec_softc *sc)
120667196661SRafal Jaworowski {
120747842ecfSJustin Hibbits if_t ifp;
120867196661SRafal Jaworowski uint32_t tmpval;
120967196661SRafal Jaworowski
121067196661SRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
121167196661SRafal Jaworowski
121267196661SRafal Jaworowski ifp = sc->tsec_ifp;
121367196661SRafal Jaworowski
121467196661SRafal Jaworowski /* Disable interface and watchdog timer */
121564f90c9dSRafal Jaworowski callout_stop(&sc->tsec_callout);
121647842ecfSJustin Hibbits if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
12175432bd9fSRafal Jaworowski sc->tsec_watchdog = 0;
121867196661SRafal Jaworowski
121967196661SRafal Jaworowski /* Disable all interrupts and stop DMA */
122067196661SRafal Jaworowski tsec_intrs_ctl(sc, 0);
122167196661SRafal Jaworowski tsec_dma_ctl(sc, 0);
122267196661SRafal Jaworowski
122367196661SRafal Jaworowski /* Remove pending data from TX queue */
12242c0dbbcbSJustin Hibbits while (sc->tx_idx_tail != sc->tx_idx_head) {
12252c0dbbcbSJustin Hibbits bus_dmamap_sync(sc->tsec_tx_mtag,
12262c0dbbcbSJustin Hibbits sc->tx_bufmap[sc->tx_idx_tail].map,
1227bd37530eSRafal Jaworowski BUS_DMASYNC_POSTWRITE);
12282c0dbbcbSJustin Hibbits bus_dmamap_unload(sc->tsec_tx_mtag,
12292c0dbbcbSJustin Hibbits sc->tx_bufmap[sc->tx_idx_tail].map);
12302c0dbbcbSJustin Hibbits m_freem(sc->tx_bufmap[sc->tx_idx_tail].mbuf);
12312c0dbbcbSJustin Hibbits sc->tx_idx_tail = (sc->tx_idx_tail + 1)
12322c0dbbcbSJustin Hibbits & (TSEC_TX_NUM_DESC - 1);
123367196661SRafal Jaworowski }
123467196661SRafal Jaworowski
1235bd37530eSRafal Jaworowski /* Disable RX and TX */
123667196661SRafal Jaworowski tmpval = TSEC_READ(sc, TSEC_REG_MACCFG1);
123767196661SRafal Jaworowski tmpval &= ~(TSEC_MACCFG1_RX_EN | TSEC_MACCFG1_TX_EN);
123867196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG1, tmpval);
123967196661SRafal Jaworowski DELAY(10);
124067196661SRafal Jaworowski }
124167196661SRafal Jaworowski
1242bd37530eSRafal Jaworowski static void
tsec_tick(void * arg)1243bd37530eSRafal Jaworowski tsec_tick(void *arg)
124467196661SRafal Jaworowski {
124567196661SRafal Jaworowski struct tsec_softc *sc = arg;
124647842ecfSJustin Hibbits if_t ifp;
1247bd37530eSRafal Jaworowski int link;
1248bd37530eSRafal Jaworowski
1249bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
1250bd37530eSRafal Jaworowski
1251bd37530eSRafal Jaworowski tsec_watchdog(sc);
1252bd37530eSRafal Jaworowski
1253bd37530eSRafal Jaworowski ifp = sc->tsec_ifp;
1254bd37530eSRafal Jaworowski link = sc->tsec_link;
1255bd37530eSRafal Jaworowski
1256bd37530eSRafal Jaworowski mii_tick(sc->tsec_mii);
1257bd37530eSRafal Jaworowski
1258bd37530eSRafal Jaworowski if (link == 0 && sc->tsec_link == 1 &&
125947842ecfSJustin Hibbits (!if_sendq_empty(ifp)))
1260bd37530eSRafal Jaworowski tsec_start_locked(ifp);
1261bd37530eSRafal Jaworowski
1262bd37530eSRafal Jaworowski /* Schedule another timeout one second from now. */
1263bd37530eSRafal Jaworowski callout_reset(&sc->tsec_callout, hz, tsec_tick, sc);
1264bd37530eSRafal Jaworowski
1265bd37530eSRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
1266bd37530eSRafal Jaworowski }
1267bd37530eSRafal Jaworowski
1268bd37530eSRafal Jaworowski /*
1269bd37530eSRafal Jaworowski * This is the core RX routine. It replenishes mbufs in the descriptor and
1270bd37530eSRafal Jaworowski * sends data which have been dma'ed into host memory to upper layer.
1271bd37530eSRafal Jaworowski *
1272bd37530eSRafal Jaworowski * Loops at most count times if count is > 0, or until done if count < 0.
1273bd37530eSRafal Jaworowski */
12741abcdbd1SAttilio Rao static int
tsec_receive_intr_locked(struct tsec_softc * sc,int count)1275bd37530eSRafal Jaworowski tsec_receive_intr_locked(struct tsec_softc *sc, int count)
1276bd37530eSRafal Jaworowski {
127767196661SRafal Jaworowski struct tsec_desc *rx_desc;
127847842ecfSJustin Hibbits if_t ifp;
127967196661SRafal Jaworowski struct rx_data_type *rx_data;
128067196661SRafal Jaworowski struct mbuf *m;
128167196661SRafal Jaworowski uint32_t i;
12821abcdbd1SAttilio Rao int c, rx_npkts;
128367196661SRafal Jaworowski uint16_t flags;
1284bd37530eSRafal Jaworowski
1285bd37530eSRafal Jaworowski TSEC_RECEIVE_LOCK_ASSERT(sc);
128667196661SRafal Jaworowski
128767196661SRafal Jaworowski ifp = sc->tsec_ifp;
128867196661SRafal Jaworowski rx_data = sc->rx_data;
12891abcdbd1SAttilio Rao rx_npkts = 0;
129067196661SRafal Jaworowski
1291bd37530eSRafal Jaworowski bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1292bd37530eSRafal Jaworowski BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
129367196661SRafal Jaworowski
1294bd37530eSRafal Jaworowski for (c = 0; ; c++) {
1295bd37530eSRafal Jaworowski if (count >= 0 && count-- == 0)
1296bd37530eSRafal Jaworowski break;
129767196661SRafal Jaworowski
129867196661SRafal Jaworowski rx_desc = TSEC_GET_CUR_RX_DESC(sc);
129967196661SRafal Jaworowski flags = rx_desc->flags;
130067196661SRafal Jaworowski
130167196661SRafal Jaworowski /* Check if there is anything to receive */
1302bd37530eSRafal Jaworowski if ((flags & TSEC_RXBD_E) || (c >= TSEC_RX_NUM_DESC)) {
130367196661SRafal Jaworowski /*
130467196661SRafal Jaworowski * Avoid generating another interrupt
130567196661SRafal Jaworowski */
130667196661SRafal Jaworowski if (flags & TSEC_RXBD_E)
130767196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT,
130867196661SRafal Jaworowski TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
130967196661SRafal Jaworowski /*
131067196661SRafal Jaworowski * We didn't consume current descriptor and have to
131167196661SRafal Jaworowski * return it to the queue
131267196661SRafal Jaworowski */
131367196661SRafal Jaworowski TSEC_BACK_CUR_RX_DESC(sc);
131467196661SRafal Jaworowski break;
131567196661SRafal Jaworowski }
131667196661SRafal Jaworowski
131767196661SRafal Jaworowski if (flags & (TSEC_RXBD_LG | TSEC_RXBD_SH | TSEC_RXBD_NO |
131867196661SRafal Jaworowski TSEC_RXBD_CR | TSEC_RXBD_OV | TSEC_RXBD_TR)) {
131967196661SRafal Jaworowski rx_desc->length = 0;
1320bd37530eSRafal Jaworowski rx_desc->flags = (rx_desc->flags &
1321bd37530eSRafal Jaworowski ~TSEC_RXBD_ZEROONINIT) | TSEC_RXBD_E | TSEC_RXBD_I;
1322bd37530eSRafal Jaworowski
1323bd37530eSRafal Jaworowski if (sc->frame != NULL) {
1324bd37530eSRafal Jaworowski m_free(sc->frame);
1325bd37530eSRafal Jaworowski sc->frame = NULL;
1326bd37530eSRafal Jaworowski }
1327bd37530eSRafal Jaworowski
132867196661SRafal Jaworowski continue;
132967196661SRafal Jaworowski }
133067196661SRafal Jaworowski
133167196661SRafal Jaworowski /* Ok... process frame */
133267196661SRafal Jaworowski i = TSEC_GET_CUR_RX_DESC_CNT(sc);
133367196661SRafal Jaworowski m = rx_data[i].mbuf;
1334bd37530eSRafal Jaworowski m->m_len = rx_desc->length;
1335bd37530eSRafal Jaworowski
1336bd37530eSRafal Jaworowski if (sc->frame != NULL) {
1337bd37530eSRafal Jaworowski if ((flags & TSEC_RXBD_L) != 0)
1338bd37530eSRafal Jaworowski m->m_len -= m_length(sc->frame, NULL);
1339bd37530eSRafal Jaworowski
1340bd37530eSRafal Jaworowski m->m_flags &= ~M_PKTHDR;
1341bd37530eSRafal Jaworowski m_cat(sc->frame, m);
1342bd37530eSRafal Jaworowski } else {
1343bd37530eSRafal Jaworowski sc->frame = m;
1344bd37530eSRafal Jaworowski }
1345bd37530eSRafal Jaworowski
1346bd37530eSRafal Jaworowski m = NULL;
1347bd37530eSRafal Jaworowski
1348bd37530eSRafal Jaworowski if ((flags & TSEC_RXBD_L) != 0) {
1349bd37530eSRafal Jaworowski m = sc->frame;
1350bd37530eSRafal Jaworowski sc->frame = NULL;
1351bd37530eSRafal Jaworowski }
135267196661SRafal Jaworowski
135367196661SRafal Jaworowski if (tsec_new_rxbuf(sc->tsec_rx_mtag, rx_data[i].map,
135467196661SRafal Jaworowski &rx_data[i].mbuf, &rx_data[i].paddr)) {
13552c0dbbcbSJustin Hibbits if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1356ab160495SRafal Jaworowski /*
1357ab160495SRafal Jaworowski * We ran out of mbufs; didn't consume current
1358ab160495SRafal Jaworowski * descriptor and have to return it to the queue.
1359ab160495SRafal Jaworowski */
1360ab160495SRafal Jaworowski TSEC_BACK_CUR_RX_DESC(sc);
1361ab160495SRafal Jaworowski break;
136267196661SRafal Jaworowski }
1363bd37530eSRafal Jaworowski
1364bd37530eSRafal Jaworowski /* Attach new buffer to descriptor and clear flags */
136567196661SRafal Jaworowski rx_desc->bufptr = rx_data[i].paddr;
136667196661SRafal Jaworowski rx_desc->length = 0;
136767196661SRafal Jaworowski rx_desc->flags = (rx_desc->flags & ~TSEC_RXBD_ZEROONINIT) |
136867196661SRafal Jaworowski TSEC_RXBD_E | TSEC_RXBD_I;
136967196661SRafal Jaworowski
1370bd37530eSRafal Jaworowski if (m != NULL) {
137167196661SRafal Jaworowski m->m_pkthdr.rcvif = ifp;
137267196661SRafal Jaworowski
1373bd37530eSRafal Jaworowski m_fixhdr(m);
1374bd37530eSRafal Jaworowski m_adj(m, -ETHER_CRC_LEN);
137567196661SRafal Jaworowski
1376bd37530eSRafal Jaworowski if (sc->is_etsec)
1377bd37530eSRafal Jaworowski tsec_offload_process_frame(sc, m);
137867196661SRafal Jaworowski
137967196661SRafal Jaworowski TSEC_RECEIVE_UNLOCK(sc);
138047842ecfSJustin Hibbits if_input(ifp, m);
1381bd37530eSRafal Jaworowski TSEC_RECEIVE_LOCK(sc);
13821abcdbd1SAttilio Rao rx_npkts++;
1383bd37530eSRafal Jaworowski }
1384bd37530eSRafal Jaworowski }
138567196661SRafal Jaworowski
1386bd37530eSRafal Jaworowski bus_dmamap_sync(sc->tsec_rx_dtag, sc->tsec_rx_dmap,
1387bd37530eSRafal Jaworowski BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1388371bf7ccSRafal Jaworowski
1389371bf7ccSRafal Jaworowski /*
1390371bf7ccSRafal Jaworowski * Make sure TSEC receiver is not halted.
1391371bf7ccSRafal Jaworowski *
1392371bf7ccSRafal Jaworowski * Various conditions can stop the TSEC receiver, but not all are
1393371bf7ccSRafal Jaworowski * signaled and handled by error interrupt, so make sure the receiver
1394371bf7ccSRafal Jaworowski * is running. Writing to TSEC_REG_RSTAT restarts the receiver when
1395371bf7ccSRafal Jaworowski * halted, and is harmless if already running.
1396371bf7ccSRafal Jaworowski */
1397371bf7ccSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RSTAT, TSEC_RSTAT_QHLT);
13981abcdbd1SAttilio Rao return (rx_npkts);
139967196661SRafal Jaworowski }
140067196661SRafal Jaworowski
1401321e12c8SRafal Jaworowski void
tsec_receive_intr(void * arg)1402bd37530eSRafal Jaworowski tsec_receive_intr(void *arg)
140367196661SRafal Jaworowski {
140467196661SRafal Jaworowski struct tsec_softc *sc = arg;
1405bd37530eSRafal Jaworowski
1406bd37530eSRafal Jaworowski TSEC_RECEIVE_LOCK(sc);
1407bd37530eSRafal Jaworowski
1408bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
1409d8b78838SJustin Hibbits if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING) {
1410bd37530eSRafal Jaworowski TSEC_RECEIVE_UNLOCK(sc);
1411bd37530eSRafal Jaworowski return;
1412bd37530eSRafal Jaworowski }
1413bd37530eSRafal Jaworowski #endif
1414bd37530eSRafal Jaworowski
1415bd37530eSRafal Jaworowski /* Confirm the interrupt was received by driver */
1416bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXB | TSEC_IEVENT_RXF);
1417bd37530eSRafal Jaworowski tsec_receive_intr_locked(sc, -1);
1418bd37530eSRafal Jaworowski
1419bd37530eSRafal Jaworowski TSEC_RECEIVE_UNLOCK(sc);
1420bd37530eSRafal Jaworowski }
1421bd37530eSRafal Jaworowski
1422bd37530eSRafal Jaworowski static void
tsec_transmit_intr_locked(struct tsec_softc * sc)1423bd37530eSRafal Jaworowski tsec_transmit_intr_locked(struct tsec_softc *sc)
1424bd37530eSRafal Jaworowski {
142547842ecfSJustin Hibbits if_t ifp;
14262c0dbbcbSJustin Hibbits uint32_t tx_idx;
142767196661SRafal Jaworowski
1428bd37530eSRafal Jaworowski TSEC_TRANSMIT_LOCK_ASSERT(sc);
1429bd37530eSRafal Jaworowski
143067196661SRafal Jaworowski ifp = sc->tsec_ifp;
143167196661SRafal Jaworowski
143267196661SRafal Jaworowski /* Update collision statistics */
1433c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS, TSEC_READ(sc, TSEC_REG_MON_TNCL));
143467196661SRafal Jaworowski
143567196661SRafal Jaworowski /* Reset collision counters in hardware */
143667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TSCL, 0);
143767196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TMCL, 0);
143867196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TLCL, 0);
143967196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TXCL, 0);
144067196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MON_TNCL, 0);
144167196661SRafal Jaworowski
1442321e12c8SRafal Jaworowski bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1443321e12c8SRafal Jaworowski BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
144467196661SRafal Jaworowski
14452c0dbbcbSJustin Hibbits tx_idx = sc->tx_idx_tail;
14462c0dbbcbSJustin Hibbits while (tx_idx != sc->tx_idx_head) {
14472c0dbbcbSJustin Hibbits struct tsec_desc *tx_desc;
14482c0dbbcbSJustin Hibbits struct tsec_bufmap *tx_bufmap;
14492c0dbbcbSJustin Hibbits
14502c0dbbcbSJustin Hibbits tx_desc = &sc->tsec_tx_vaddr[tx_idx];
145167196661SRafal Jaworowski if (tx_desc->flags & TSEC_TXBD_R) {
145267196661SRafal Jaworowski break;
145367196661SRafal Jaworowski }
145467196661SRafal Jaworowski
14552c0dbbcbSJustin Hibbits tx_bufmap = &sc->tx_bufmap[tx_idx];
14562c0dbbcbSJustin Hibbits tx_idx = (tx_idx + 1) & (TSEC_TX_NUM_DESC - 1);
14572c0dbbcbSJustin Hibbits if (tx_bufmap->mbuf == NULL)
145867196661SRafal Jaworowski continue;
145967196661SRafal Jaworowski
146067196661SRafal Jaworowski /*
146167196661SRafal Jaworowski * This is the last buf in this packet, so unmap and free it.
146267196661SRafal Jaworowski */
14632c0dbbcbSJustin Hibbits bus_dmamap_sync(sc->tsec_tx_mtag, tx_bufmap->map,
146464f90c9dSRafal Jaworowski BUS_DMASYNC_POSTWRITE);
14652c0dbbcbSJustin Hibbits bus_dmamap_unload(sc->tsec_tx_mtag, tx_bufmap->map);
14662c0dbbcbSJustin Hibbits m_freem(tx_bufmap->mbuf);
14672c0dbbcbSJustin Hibbits tx_bufmap->mbuf = NULL;
146867196661SRafal Jaworowski
1469c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
147067196661SRafal Jaworowski }
14712c0dbbcbSJustin Hibbits sc->tx_idx_tail = tx_idx;
1472bd37530eSRafal Jaworowski bus_dmamap_sync(sc->tsec_tx_dtag, sc->tsec_tx_dmap,
1473bd37530eSRafal Jaworowski BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
147467196661SRafal Jaworowski
147547842ecfSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
147667196661SRafal Jaworowski tsec_start_locked(ifp);
147767196661SRafal Jaworowski
14782c0dbbcbSJustin Hibbits if (sc->tx_idx_tail == sc->tx_idx_head)
14795432bd9fSRafal Jaworowski sc->tsec_watchdog = 0;
148067196661SRafal Jaworowski }
148167196661SRafal Jaworowski
1482321e12c8SRafal Jaworowski void
tsec_transmit_intr(void * arg)1483bd37530eSRafal Jaworowski tsec_transmit_intr(void *arg)
148467196661SRafal Jaworowski {
148567196661SRafal Jaworowski struct tsec_softc *sc = arg;
1486bd37530eSRafal Jaworowski
1487bd37530eSRafal Jaworowski TSEC_TRANSMIT_LOCK(sc);
1488bd37530eSRafal Jaworowski
1489bd37530eSRafal Jaworowski #ifdef DEVICE_POLLING
1490d8b78838SJustin Hibbits if (if_getcapenable(sc->tsec_ifp) & IFCAP_POLLING) {
1491bd37530eSRafal Jaworowski TSEC_TRANSMIT_UNLOCK(sc);
1492bd37530eSRafal Jaworowski return;
1493bd37530eSRafal Jaworowski }
1494bd37530eSRafal Jaworowski #endif
1495bd37530eSRafal Jaworowski /* Confirm the interrupt was received by driver */
1496bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_TXB | TSEC_IEVENT_TXF);
1497bd37530eSRafal Jaworowski tsec_transmit_intr_locked(sc);
1498bd37530eSRafal Jaworowski
1499bd37530eSRafal Jaworowski TSEC_TRANSMIT_UNLOCK(sc);
1500bd37530eSRafal Jaworowski }
1501bd37530eSRafal Jaworowski
1502bd37530eSRafal Jaworowski static void
tsec_error_intr_locked(struct tsec_softc * sc,int count)1503bd37530eSRafal Jaworowski tsec_error_intr_locked(struct tsec_softc *sc, int count)
1504bd37530eSRafal Jaworowski {
150547842ecfSJustin Hibbits if_t ifp;
150667196661SRafal Jaworowski uint32_t eflags;
150767196661SRafal Jaworowski
1508bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
1509bd37530eSRafal Jaworowski
151067196661SRafal Jaworowski ifp = sc->tsec_ifp;
151167196661SRafal Jaworowski
151267196661SRafal Jaworowski eflags = TSEC_READ(sc, TSEC_REG_IEVENT);
151367196661SRafal Jaworowski
151467196661SRafal Jaworowski /* Clear events bits in hardware */
151567196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_IEVENT, TSEC_IEVENT_RXC | TSEC_IEVENT_BSY |
151667196661SRafal Jaworowski TSEC_IEVENT_EBERR | TSEC_IEVENT_MSRO | TSEC_IEVENT_BABT |
151767196661SRafal Jaworowski TSEC_IEVENT_TXC | TSEC_IEVENT_TXE | TSEC_IEVENT_LC |
151867196661SRafal Jaworowski TSEC_IEVENT_CRL | TSEC_IEVENT_XFUN);
151967196661SRafal Jaworowski
152067196661SRafal Jaworowski /* Check transmitter errors */
152167196661SRafal Jaworowski if (eflags & TSEC_IEVENT_TXE) {
1522c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
152367196661SRafal Jaworowski
152467196661SRafal Jaworowski if (eflags & TSEC_IEVENT_LC)
1525c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
152667196661SRafal Jaworowski
152767196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TSTAT, TSEC_TSTAT_THLT);
152867196661SRafal Jaworowski }
152967196661SRafal Jaworowski
15302c0dbbcbSJustin Hibbits /* Check for discarded frame due to a lack of buffers */
153167196661SRafal Jaworowski if (eflags & TSEC_IEVENT_BSY) {
1532c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
153367196661SRafal Jaworowski }
1534bd37530eSRafal Jaworowski
153547842ecfSJustin Hibbits if (if_getflags(ifp) & IFF_DEBUG)
1536bd37530eSRafal Jaworowski if_printf(ifp, "tsec_error_intr(): event flags: 0x%x\n",
1537bd37530eSRafal Jaworowski eflags);
1538bd37530eSRafal Jaworowski
1539bd37530eSRafal Jaworowski if (eflags & TSEC_IEVENT_EBERR) {
1540bd37530eSRafal Jaworowski if_printf(ifp, "System bus error occurred during"
1541bd37530eSRafal Jaworowski "DMA transaction (flags: 0x%x)\n", eflags);
1542bd37530eSRafal Jaworowski tsec_init_locked(sc);
1543bd37530eSRafal Jaworowski }
1544bd37530eSRafal Jaworowski
1545bd37530eSRafal Jaworowski if (eflags & TSEC_IEVENT_BABT)
1546c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1547bd37530eSRafal Jaworowski
154867196661SRafal Jaworowski if (eflags & TSEC_IEVENT_BABR)
1549c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
155067196661SRafal Jaworowski }
155167196661SRafal Jaworowski
1552bd37530eSRafal Jaworowski void
tsec_error_intr(void * arg)1553bd37530eSRafal Jaworowski tsec_error_intr(void *arg)
155467196661SRafal Jaworowski {
1555bd37530eSRafal Jaworowski struct tsec_softc *sc = arg;
155667196661SRafal Jaworowski
1557772619e1SRafal Jaworowski TSEC_GLOBAL_LOCK(sc);
1558bd37530eSRafal Jaworowski tsec_error_intr_locked(sc, -1);
1559772619e1SRafal Jaworowski TSEC_GLOBAL_UNLOCK(sc);
156067196661SRafal Jaworowski }
156167196661SRafal Jaworowski
1562321e12c8SRafal Jaworowski int
tsec_miibus_readreg(device_t dev,int phy,int reg)156367196661SRafal Jaworowski tsec_miibus_readreg(device_t dev, int phy, int reg)
156467196661SRafal Jaworowski {
156567196661SRafal Jaworowski struct tsec_softc *sc;
156688011b59SJustin Hibbits int timeout;
1567629aa519SNathan Whitehorn int rv;
156867196661SRafal Jaworowski
1569aa15e881SRafal Jaworowski sc = device_get_softc(dev);
157067196661SRafal Jaworowski
1571629aa519SNathan Whitehorn TSEC_PHY_LOCK();
1572629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1573629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, 0);
1574629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCOM, TSEC_MIIMCOM_READCYCLE);
157567196661SRafal Jaworowski
157688011b59SJustin Hibbits timeout = tsec_mii_wait(sc, TSEC_MIIMIND_NOTVALID | TSEC_MIIMIND_BUSY);
157788011b59SJustin Hibbits rv = TSEC_PHY_READ(sc, TSEC_REG_MIIMSTAT);
157888011b59SJustin Hibbits TSEC_PHY_UNLOCK();
157967196661SRafal Jaworowski
158072b58db8SJustin Hibbits if (timeout)
158167196661SRafal Jaworowski device_printf(dev, "Timeout while reading from PHY!\n");
158267196661SRafal Jaworowski
1583629aa519SNathan Whitehorn return (rv);
158467196661SRafal Jaworowski }
158567196661SRafal Jaworowski
1586661ee6eeSRafal Jaworowski int
tsec_miibus_writereg(device_t dev,int phy,int reg,int value)158767196661SRafal Jaworowski tsec_miibus_writereg(device_t dev, int phy, int reg, int value)
158867196661SRafal Jaworowski {
158967196661SRafal Jaworowski struct tsec_softc *sc;
159088011b59SJustin Hibbits int timeout;
159167196661SRafal Jaworowski
1592aa15e881SRafal Jaworowski sc = device_get_softc(dev);
159367196661SRafal Jaworowski
1594629aa519SNathan Whitehorn TSEC_PHY_LOCK();
1595629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMADD, (phy << 8) | reg);
1596629aa519SNathan Whitehorn TSEC_PHY_WRITE(sc, TSEC_REG_MIIMCON, value);
159788011b59SJustin Hibbits timeout = tsec_mii_wait(sc, TSEC_MIIMIND_BUSY);
1598629aa519SNathan Whitehorn TSEC_PHY_UNLOCK();
159967196661SRafal Jaworowski
160072b58db8SJustin Hibbits if (timeout)
160167196661SRafal Jaworowski device_printf(dev, "Timeout while writing to PHY!\n");
1602661ee6eeSRafal Jaworowski
1603661ee6eeSRafal Jaworowski return (0);
160467196661SRafal Jaworowski }
160567196661SRafal Jaworowski
1606321e12c8SRafal Jaworowski void
tsec_miibus_statchg(device_t dev)160767196661SRafal Jaworowski tsec_miibus_statchg(device_t dev)
160867196661SRafal Jaworowski {
160967196661SRafal Jaworowski struct tsec_softc *sc;
161067196661SRafal Jaworowski struct mii_data *mii;
161167196661SRafal Jaworowski uint32_t ecntrl, id, tmp;
161267196661SRafal Jaworowski int link;
161367196661SRafal Jaworowski
161467196661SRafal Jaworowski sc = device_get_softc(dev);
161567196661SRafal Jaworowski mii = sc->tsec_mii;
161667196661SRafal Jaworowski link = ((mii->mii_media_status & IFM_ACTIVE) ? 1 : 0);
161767196661SRafal Jaworowski
161867196661SRafal Jaworowski tmp = TSEC_READ(sc, TSEC_REG_MACCFG2) & ~TSEC_MACCFG2_IF;
161967196661SRafal Jaworowski
162067196661SRafal Jaworowski if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
162167196661SRafal Jaworowski tmp |= TSEC_MACCFG2_FULLDUPLEX;
162267196661SRafal Jaworowski else
162367196661SRafal Jaworowski tmp &= ~TSEC_MACCFG2_FULLDUPLEX;
162467196661SRafal Jaworowski
162567196661SRafal Jaworowski switch (IFM_SUBTYPE(mii->mii_media_active)) {
162667196661SRafal Jaworowski case IFM_1000_T:
162767196661SRafal Jaworowski case IFM_1000_SX:
162867196661SRafal Jaworowski tmp |= TSEC_MACCFG2_GMII;
162967196661SRafal Jaworowski sc->tsec_link = link;
163067196661SRafal Jaworowski break;
163167196661SRafal Jaworowski case IFM_100_TX:
163267196661SRafal Jaworowski case IFM_10_T:
163367196661SRafal Jaworowski tmp |= TSEC_MACCFG2_MII;
163467196661SRafal Jaworowski sc->tsec_link = link;
163567196661SRafal Jaworowski break;
163667196661SRafal Jaworowski case IFM_NONE:
163767196661SRafal Jaworowski if (link)
163864f90c9dSRafal Jaworowski device_printf(dev, "No speed selected but link "
163964f90c9dSRafal Jaworowski "active!\n");
164067196661SRafal Jaworowski sc->tsec_link = 0;
164167196661SRafal Jaworowski return;
164267196661SRafal Jaworowski default:
164367196661SRafal Jaworowski sc->tsec_link = 0;
164467196661SRafal Jaworowski device_printf(dev, "Unknown speed (%d), link %s!\n",
164567196661SRafal Jaworowski IFM_SUBTYPE(mii->mii_media_active),
164667196661SRafal Jaworowski ((link) ? "up" : "down"));
164767196661SRafal Jaworowski return;
164867196661SRafal Jaworowski }
164967196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MACCFG2, tmp);
165067196661SRafal Jaworowski
165167196661SRafal Jaworowski /* XXX kludge - use circumstantial evidence for reduced mode. */
165267196661SRafal Jaworowski id = TSEC_READ(sc, TSEC_REG_ID2);
165367196661SRafal Jaworowski if (id & 0xffff) {
165467196661SRafal Jaworowski ecntrl = TSEC_READ(sc, TSEC_REG_ECNTRL) & ~TSEC_ECNTRL_R100M;
165567196661SRafal Jaworowski ecntrl |= (tmp & TSEC_MACCFG2_MII) ? TSEC_ECNTRL_R100M : 0;
165667196661SRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_ECNTRL, ecntrl);
165767196661SRafal Jaworowski }
165867196661SRafal Jaworowski }
1659bd37530eSRafal Jaworowski
1660bd37530eSRafal Jaworowski static void
tsec_add_sysctls(struct tsec_softc * sc)1661bd37530eSRafal Jaworowski tsec_add_sysctls(struct tsec_softc *sc)
1662bd37530eSRafal Jaworowski {
1663bd37530eSRafal Jaworowski struct sysctl_ctx_list *ctx;
1664bd37530eSRafal Jaworowski struct sysctl_oid_list *children;
1665bd37530eSRafal Jaworowski struct sysctl_oid *tree;
1666bd37530eSRafal Jaworowski
1667bd37530eSRafal Jaworowski ctx = device_get_sysctl_ctx(sc->dev);
1668bd37530eSRafal Jaworowski children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
1669bd37530eSRafal Jaworowski tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "int_coal",
16707029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "TSEC Interrupts coalescing");
1671bd37530eSRafal Jaworowski children = SYSCTL_CHILDREN(tree);
1672bd37530eSRafal Jaworowski
1673bd37530eSRafal Jaworowski SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_time",
16747029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_RX,
16757029da5cSPawel Biernacki tsec_sysctl_ic_time, "I", "IC RX time threshold (0-65535)");
1676bd37530eSRafal Jaworowski SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_count",
16777029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_RX,
16787029da5cSPawel Biernacki tsec_sysctl_ic_count, "I", "IC RX frame count threshold (0-255)");
1679bd37530eSRafal Jaworowski
1680bd37530eSRafal Jaworowski SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_time",
16817029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_TX,
16827029da5cSPawel Biernacki tsec_sysctl_ic_time, "I", "IC TX time threshold (0-65535)");
1683bd37530eSRafal Jaworowski SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_count",
16847029da5cSPawel Biernacki CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, TSEC_IC_TX,
16857029da5cSPawel Biernacki tsec_sysctl_ic_count, "I", "IC TX frame count threshold (0-255)");
1686bd37530eSRafal Jaworowski }
1687bd37530eSRafal Jaworowski
1688bd37530eSRafal Jaworowski /*
1689bd37530eSRafal Jaworowski * With Interrupt Coalescing (IC) active, a transmit/receive frame
1690bd37530eSRafal Jaworowski * interrupt is raised either upon:
1691bd37530eSRafal Jaworowski *
1692bd37530eSRafal Jaworowski * - threshold-defined period of time elapsed, or
1693bd37530eSRafal Jaworowski * - threshold-defined number of frames is received/transmitted,
1694bd37530eSRafal Jaworowski * whichever occurs first.
1695bd37530eSRafal Jaworowski *
1696bd37530eSRafal Jaworowski * The following sysctls regulate IC behaviour (for TX/RX separately):
1697bd37530eSRafal Jaworowski *
1698bd37530eSRafal Jaworowski * dev.tsec.<unit>.int_coal.rx_time
1699bd37530eSRafal Jaworowski * dev.tsec.<unit>.int_coal.rx_count
1700bd37530eSRafal Jaworowski * dev.tsec.<unit>.int_coal.tx_time
1701bd37530eSRafal Jaworowski * dev.tsec.<unit>.int_coal.tx_count
1702bd37530eSRafal Jaworowski *
1703bd37530eSRafal Jaworowski * Values:
1704bd37530eSRafal Jaworowski *
1705bd37530eSRafal Jaworowski * - 0 for either time or count disables IC on the given TX/RX path
1706bd37530eSRafal Jaworowski *
1707bd37530eSRafal Jaworowski * - count: 1-255 (expresses frame count number; note that value of 1 is
1708bd37530eSRafal Jaworowski * effectively IC off)
1709bd37530eSRafal Jaworowski *
1710bd37530eSRafal Jaworowski * - time: 1-65535 (value corresponds to a real time period and is
1711bd37530eSRafal Jaworowski * expressed in units equivalent to 64 TSEC interface clocks, i.e. one timer
1712bd37530eSRafal Jaworowski * threshold unit is 26.5 us, 2.56 us, or 512 ns, corresponding to 10 Mbps,
1713bd37530eSRafal Jaworowski * 100 Mbps, or 1Gbps, respectively. For detailed discussion consult the
1714bd37530eSRafal Jaworowski * TSEC reference manual.
1715bd37530eSRafal Jaworowski */
1716bd37530eSRafal Jaworowski static int
tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS)1717bd37530eSRafal Jaworowski tsec_sysctl_ic_time(SYSCTL_HANDLER_ARGS)
1718bd37530eSRafal Jaworowski {
1719bd37530eSRafal Jaworowski int error;
1720bd37530eSRafal Jaworowski uint32_t time;
1721bd37530eSRafal Jaworowski struct tsec_softc *sc = (struct tsec_softc *)arg1;
1722bd37530eSRafal Jaworowski
1723bd37530eSRafal Jaworowski time = (arg2 == TSEC_IC_RX) ? sc->rx_ic_time : sc->tx_ic_time;
1724bd37530eSRafal Jaworowski
1725bd37530eSRafal Jaworowski error = sysctl_handle_int(oidp, &time, 0, req);
1726bd37530eSRafal Jaworowski if (error != 0)
1727bd37530eSRafal Jaworowski return (error);
1728bd37530eSRafal Jaworowski
1729bd37530eSRafal Jaworowski if (time > 65535)
1730bd37530eSRafal Jaworowski return (EINVAL);
1731bd37530eSRafal Jaworowski
1732bd37530eSRafal Jaworowski TSEC_IC_LOCK(sc);
1733bd37530eSRafal Jaworowski if (arg2 == TSEC_IC_RX) {
1734bd37530eSRafal Jaworowski sc->rx_ic_time = time;
1735bd37530eSRafal Jaworowski tsec_set_rxic(sc);
1736bd37530eSRafal Jaworowski } else {
1737bd37530eSRafal Jaworowski sc->tx_ic_time = time;
1738bd37530eSRafal Jaworowski tsec_set_txic(sc);
1739bd37530eSRafal Jaworowski }
1740bd37530eSRafal Jaworowski TSEC_IC_UNLOCK(sc);
1741bd37530eSRafal Jaworowski
1742bd37530eSRafal Jaworowski return (0);
1743bd37530eSRafal Jaworowski }
1744bd37530eSRafal Jaworowski
1745bd37530eSRafal Jaworowski static int
tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS)1746bd37530eSRafal Jaworowski tsec_sysctl_ic_count(SYSCTL_HANDLER_ARGS)
1747bd37530eSRafal Jaworowski {
1748bd37530eSRafal Jaworowski int error;
1749bd37530eSRafal Jaworowski uint32_t count;
1750bd37530eSRafal Jaworowski struct tsec_softc *sc = (struct tsec_softc *)arg1;
1751bd37530eSRafal Jaworowski
1752bd37530eSRafal Jaworowski count = (arg2 == TSEC_IC_RX) ? sc->rx_ic_count : sc->tx_ic_count;
1753bd37530eSRafal Jaworowski
1754bd37530eSRafal Jaworowski error = sysctl_handle_int(oidp, &count, 0, req);
1755bd37530eSRafal Jaworowski if (error != 0)
1756bd37530eSRafal Jaworowski return (error);
1757bd37530eSRafal Jaworowski
1758bd37530eSRafal Jaworowski if (count > 255)
1759bd37530eSRafal Jaworowski return (EINVAL);
1760bd37530eSRafal Jaworowski
1761bd37530eSRafal Jaworowski TSEC_IC_LOCK(sc);
1762bd37530eSRafal Jaworowski if (arg2 == TSEC_IC_RX) {
1763bd37530eSRafal Jaworowski sc->rx_ic_count = count;
1764bd37530eSRafal Jaworowski tsec_set_rxic(sc);
1765bd37530eSRafal Jaworowski } else {
1766bd37530eSRafal Jaworowski sc->tx_ic_count = count;
1767bd37530eSRafal Jaworowski tsec_set_txic(sc);
1768bd37530eSRafal Jaworowski }
1769bd37530eSRafal Jaworowski TSEC_IC_UNLOCK(sc);
1770bd37530eSRafal Jaworowski
1771bd37530eSRafal Jaworowski return (0);
1772bd37530eSRafal Jaworowski }
1773bd37530eSRafal Jaworowski
1774bd37530eSRafal Jaworowski static void
tsec_set_rxic(struct tsec_softc * sc)1775bd37530eSRafal Jaworowski tsec_set_rxic(struct tsec_softc *sc)
1776bd37530eSRafal Jaworowski {
1777bd37530eSRafal Jaworowski uint32_t rxic_val;
1778bd37530eSRafal Jaworowski
1779bd37530eSRafal Jaworowski if (sc->rx_ic_count == 0 || sc->rx_ic_time == 0)
1780bd37530eSRafal Jaworowski /* Disable RX IC */
1781bd37530eSRafal Jaworowski rxic_val = 0;
1782bd37530eSRafal Jaworowski else {
1783bd37530eSRafal Jaworowski rxic_val = 0x80000000;
1784bd37530eSRafal Jaworowski rxic_val |= (sc->rx_ic_count << 21);
1785bd37530eSRafal Jaworowski rxic_val |= sc->rx_ic_time;
1786bd37530eSRafal Jaworowski }
1787bd37530eSRafal Jaworowski
1788bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RXIC, rxic_val);
1789bd37530eSRafal Jaworowski }
1790bd37530eSRafal Jaworowski
1791bd37530eSRafal Jaworowski static void
tsec_set_txic(struct tsec_softc * sc)1792bd37530eSRafal Jaworowski tsec_set_txic(struct tsec_softc *sc)
1793bd37530eSRafal Jaworowski {
1794bd37530eSRafal Jaworowski uint32_t txic_val;
1795bd37530eSRafal Jaworowski
1796bd37530eSRafal Jaworowski if (sc->tx_ic_count == 0 || sc->tx_ic_time == 0)
1797bd37530eSRafal Jaworowski /* Disable TX IC */
1798bd37530eSRafal Jaworowski txic_val = 0;
1799bd37530eSRafal Jaworowski else {
1800bd37530eSRafal Jaworowski txic_val = 0x80000000;
1801bd37530eSRafal Jaworowski txic_val |= (sc->tx_ic_count << 21);
1802bd37530eSRafal Jaworowski txic_val |= sc->tx_ic_time;
1803bd37530eSRafal Jaworowski }
1804bd37530eSRafal Jaworowski
1805bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TXIC, txic_val);
1806bd37530eSRafal Jaworowski }
1807bd37530eSRafal Jaworowski
1808bd37530eSRafal Jaworowski static void
tsec_offload_setup(struct tsec_softc * sc)1809bd37530eSRafal Jaworowski tsec_offload_setup(struct tsec_softc *sc)
1810bd37530eSRafal Jaworowski {
181147842ecfSJustin Hibbits if_t ifp = sc->tsec_ifp;
1812bd37530eSRafal Jaworowski uint32_t reg;
1813bd37530eSRafal Jaworowski
1814bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
1815bd37530eSRafal Jaworowski
1816bd37530eSRafal Jaworowski reg = TSEC_READ(sc, TSEC_REG_TCTRL);
1817bd37530eSRafal Jaworowski reg |= TSEC_TCTRL_IPCSEN | TSEC_TCTRL_TUCSEN;
1818bd37530eSRafal Jaworowski
181947842ecfSJustin Hibbits if (if_getcapenable(ifp) & IFCAP_TXCSUM)
182047842ecfSJustin Hibbits if_sethwassist(ifp, TSEC_CHECKSUM_FEATURES);
1821bd37530eSRafal Jaworowski else
182247842ecfSJustin Hibbits if_sethwassist(ifp, 0);
1823bd37530eSRafal Jaworowski
1824bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_TCTRL, reg);
1825bd37530eSRafal Jaworowski
1826bd37530eSRafal Jaworowski reg = TSEC_READ(sc, TSEC_REG_RCTRL);
1827bd37530eSRafal Jaworowski reg &= ~(TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN | TSEC_RCTRL_PRSDEP);
1828bd37530eSRafal Jaworowski reg |= TSEC_RCTRL_PRSDEP_PARSE_L2 | TSEC_RCTRL_VLEX;
1829bd37530eSRafal Jaworowski
183047842ecfSJustin Hibbits if (if_getcapenable(ifp) & IFCAP_RXCSUM)
1831bd37530eSRafal Jaworowski reg |= TSEC_RCTRL_IPCSEN | TSEC_RCTRL_TUCSEN |
1832bd37530eSRafal Jaworowski TSEC_RCTRL_PRSDEP_PARSE_L234;
1833bd37530eSRafal Jaworowski
1834bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_RCTRL, reg);
1835bd37530eSRafal Jaworowski }
1836bd37530eSRafal Jaworowski
1837bd37530eSRafal Jaworowski static void
tsec_offload_process_frame(struct tsec_softc * sc,struct mbuf * m)1838bd37530eSRafal Jaworowski tsec_offload_process_frame(struct tsec_softc *sc, struct mbuf *m)
1839bd37530eSRafal Jaworowski {
1840bd37530eSRafal Jaworowski struct tsec_rx_fcb rx_fcb;
1841bd37530eSRafal Jaworowski int csum_flags = 0;
1842bd37530eSRafal Jaworowski int protocol, flags;
1843bd37530eSRafal Jaworowski
1844bd37530eSRafal Jaworowski TSEC_RECEIVE_LOCK_ASSERT(sc);
1845bd37530eSRafal Jaworowski
1846bd37530eSRafal Jaworowski m_copydata(m, 0, sizeof(struct tsec_rx_fcb), (caddr_t)(&rx_fcb));
1847bd37530eSRafal Jaworowski flags = rx_fcb.flags;
1848bd37530eSRafal Jaworowski protocol = rx_fcb.protocol;
1849bd37530eSRafal Jaworowski
1850bd37530eSRafal Jaworowski if (TSEC_RX_FCB_IP_CSUM_CHECKED(flags)) {
1851bd37530eSRafal Jaworowski csum_flags |= CSUM_IP_CHECKED;
1852bd37530eSRafal Jaworowski
1853bd37530eSRafal Jaworowski if ((flags & TSEC_RX_FCB_IP_CSUM_ERROR) == 0)
1854bd37530eSRafal Jaworowski csum_flags |= CSUM_IP_VALID;
1855bd37530eSRafal Jaworowski }
1856bd37530eSRafal Jaworowski
1857bd37530eSRafal Jaworowski if ((protocol == IPPROTO_TCP || protocol == IPPROTO_UDP) &&
1858bd37530eSRafal Jaworowski TSEC_RX_FCB_TCP_UDP_CSUM_CHECKED(flags) &&
1859bd37530eSRafal Jaworowski (flags & TSEC_RX_FCB_TCP_UDP_CSUM_ERROR) == 0) {
1860bd37530eSRafal Jaworowski csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1861bd37530eSRafal Jaworowski m->m_pkthdr.csum_data = 0xFFFF;
1862bd37530eSRafal Jaworowski }
1863bd37530eSRafal Jaworowski
1864bd37530eSRafal Jaworowski m->m_pkthdr.csum_flags = csum_flags;
1865bd37530eSRafal Jaworowski
1866bd37530eSRafal Jaworowski if (flags & TSEC_RX_FCB_VLAN) {
1867bd37530eSRafal Jaworowski m->m_pkthdr.ether_vtag = rx_fcb.vlan;
1868bd37530eSRafal Jaworowski m->m_flags |= M_VLANTAG;
1869bd37530eSRafal Jaworowski }
1870bd37530eSRafal Jaworowski
1871bd37530eSRafal Jaworowski m_adj(m, sizeof(struct tsec_rx_fcb));
1872bd37530eSRafal Jaworowski }
1873bd37530eSRafal Jaworowski
18745c973840SGleb Smirnoff static u_int
tsec_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)18755c973840SGleb Smirnoff tsec_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
18765c973840SGleb Smirnoff {
18775c973840SGleb Smirnoff uint32_t h, *hashtable = arg;
18785c973840SGleb Smirnoff
18795c973840SGleb Smirnoff h = (ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 24) & 0xFF;
18805c973840SGleb Smirnoff hashtable[(h >> 5)] |= 1 << (0x1F - (h & 0x1F));
18815c973840SGleb Smirnoff
18825c973840SGleb Smirnoff return (1);
18835c973840SGleb Smirnoff }
18845c973840SGleb Smirnoff
1885bd37530eSRafal Jaworowski static void
tsec_setup_multicast(struct tsec_softc * sc)1886bd37530eSRafal Jaworowski tsec_setup_multicast(struct tsec_softc *sc)
1887bd37530eSRafal Jaworowski {
1888bd37530eSRafal Jaworowski uint32_t hashtable[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
188947842ecfSJustin Hibbits if_t ifp = sc->tsec_ifp;
1890bd37530eSRafal Jaworowski int i;
1891bd37530eSRafal Jaworowski
1892bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
1893bd37530eSRafal Jaworowski
189447842ecfSJustin Hibbits if (if_getflags(ifp) & IFF_ALLMULTI) {
1895bd37530eSRafal Jaworowski for (i = 0; i < 8; i++)
1896bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR(i), 0xFFFFFFFF);
1897bd37530eSRafal Jaworowski
1898bd37530eSRafal Jaworowski return;
1899bd37530eSRafal Jaworowski }
1900bd37530eSRafal Jaworowski
19015c973840SGleb Smirnoff if_foreach_llmaddr(ifp, tsec_hash_maddr, &hashtable);
1902bd37530eSRafal Jaworowski
1903bd37530eSRafal Jaworowski for (i = 0; i < 8; i++)
1904bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_GADDR(i), hashtable[i]);
1905bd37530eSRafal Jaworowski }
1906bd37530eSRafal Jaworowski
1907bd37530eSRafal Jaworowski static int
tsec_set_mtu(struct tsec_softc * sc,unsigned int mtu)1908bd37530eSRafal Jaworowski tsec_set_mtu(struct tsec_softc *sc, unsigned int mtu)
1909bd37530eSRafal Jaworowski {
1910bd37530eSRafal Jaworowski
1911bd37530eSRafal Jaworowski mtu += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
1912bd37530eSRafal Jaworowski
1913bd37530eSRafal Jaworowski TSEC_GLOBAL_LOCK_ASSERT(sc);
1914bd37530eSRafal Jaworowski
1915bd37530eSRafal Jaworowski if (mtu >= TSEC_MIN_FRAME_SIZE && mtu <= TSEC_MAX_FRAME_SIZE) {
1916bd37530eSRafal Jaworowski TSEC_WRITE(sc, TSEC_REG_MAXFRM, mtu);
1917bd37530eSRafal Jaworowski return (mtu);
1918bd37530eSRafal Jaworowski }
1919bd37530eSRafal Jaworowski
1920bd37530eSRafal Jaworowski return (0);
1921bd37530eSRafal Jaworowski }
1922