1*100fd80aSthorpej /* $NetBSD: if_temac.c,v 1.20 2022/09/18 15:57:13 thorpej Exp $ */
2e388b581Sfreza
3e388b581Sfreza /*
4e388b581Sfreza * Copyright (c) 2006 Jachym Holecek
5e388b581Sfreza * All rights reserved.
6e388b581Sfreza *
7e388b581Sfreza * Written for DFC Design, s.r.o.
8e388b581Sfreza *
9e388b581Sfreza * Redistribution and use in source and binary forms, with or without
10e388b581Sfreza * modification, are permitted provided that the following conditions
11e388b581Sfreza * are met:
12e388b581Sfreza *
13e388b581Sfreza * 1. Redistributions of source code must retain the above copyright
14e388b581Sfreza * notice, this list of conditions and the following disclaimer.
15e388b581Sfreza *
16e388b581Sfreza * 2. Redistributions in binary form must reproduce the above copyright
17e388b581Sfreza * notice, this list of conditions and the following disclaimer in the
18e388b581Sfreza * documentation and/or other materials provided with the distribution.
19e388b581Sfreza *
20e388b581Sfreza * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21e388b581Sfreza * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22e388b581Sfreza * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23e388b581Sfreza * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24e388b581Sfreza * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25e388b581Sfreza * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26e388b581Sfreza * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27e388b581Sfreza * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28e388b581Sfreza * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29e388b581Sfreza * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30e388b581Sfreza */
31e388b581Sfreza
32e388b581Sfreza /*
33e388b581Sfreza * Driver for Xilinx LocalLink TEMAC as wired on the GSRD platform.
34e388b581Sfreza *
35e388b581Sfreza * TODO:
36e388b581Sfreza * - Optimize
37e388b581Sfreza * - Checksum offload
38e388b581Sfreza * - Address filters
39e388b581Sfreza * - Support jumbo frames
40e388b581Sfreza */
41e388b581Sfreza
42e388b581Sfreza #include <sys/cdefs.h>
43*100fd80aSthorpej __KERNEL_RCSID(0, "$NetBSD: if_temac.c,v 1.20 2022/09/18 15:57:13 thorpej Exp $");
44e388b581Sfreza
45e388b581Sfreza
46e388b581Sfreza #include <sys/param.h>
47e388b581Sfreza #include <sys/systm.h>
48e388b581Sfreza #include <sys/mbuf.h>
49e388b581Sfreza #include <sys/kernel.h>
50e388b581Sfreza #include <sys/socket.h>
51e388b581Sfreza #include <sys/ioctl.h>
52e388b581Sfreza #include <sys/device.h>
53534c8a68Smatt #include <sys/bus.h>
54534c8a68Smatt #include <sys/cpu.h>
55e388b581Sfreza
56e388b581Sfreza #include <uvm/uvm_extern.h>
57e388b581Sfreza
58e388b581Sfreza #include <net/if.h>
59e388b581Sfreza #include <net/if_dl.h>
60e388b581Sfreza #include <net/if_media.h>
61e388b581Sfreza #include <net/if_ether.h>
62e388b581Sfreza
63e388b581Sfreza #include <net/bpf.h>
64e388b581Sfreza
65534c8a68Smatt #include <powerpc/ibm4xx/cpu.h>
66e388b581Sfreza
67e388b581Sfreza #include <evbppc/virtex/idcr.h>
68e388b581Sfreza #include <evbppc/virtex/dev/xcvbusvar.h>
69e388b581Sfreza #include <evbppc/virtex/dev/cdmacreg.h>
70e388b581Sfreza #include <evbppc/virtex/dev/temacreg.h>
71e388b581Sfreza #include <evbppc/virtex/dev/temacvar.h>
72e388b581Sfreza
73e388b581Sfreza #include <dev/mii/miivar.h>
74e388b581Sfreza
75e388b581Sfreza
76e388b581Sfreza /* This is outside of TEMAC's DCR window, we have to hardcode it... */
77e388b581Sfreza #define DCR_ETH_BASE 0x0030
78e388b581Sfreza
79e388b581Sfreza #define TEMAC_REGDEBUG 0
80e388b581Sfreza #define TEMAC_RXDEBUG 0
81e388b581Sfreza #define TEMAC_TXDEBUG 0
82e388b581Sfreza
83e388b581Sfreza #if TEMAC_RXDEBUG > 0 || TEMAC_TXDEBUG > 0
84e388b581Sfreza #define TEMAC_DEBUG 1
85e388b581Sfreza #else
86e388b581Sfreza #define TEMAC_DEBUG 0
87e388b581Sfreza #endif
88e388b581Sfreza
89e388b581Sfreza #if TEMAC_REGDEBUG > 0
90e388b581Sfreza #define TRACEREG(arg) printf arg
91e388b581Sfreza #else
92e388b581Sfreza #define TRACEREG(arg) /* nop */
93e388b581Sfreza #endif
94e388b581Sfreza
95e388b581Sfreza /* DMA control chains take up one (16KB) page. */
96e388b581Sfreza #define TEMAC_NTXDESC 256
97e388b581Sfreza #define TEMAC_NRXDESC 256
98e388b581Sfreza
99e388b581Sfreza #define TEMAC_TXQLEN 64 /* Software Tx queue length */
100e388b581Sfreza #define TEMAC_NTXSEG 16 /* Maximum Tx segments per packet */
101e388b581Sfreza
102e388b581Sfreza #define TEMAC_NRXSEG 1 /* Maximum Rx segments per packet */
103e388b581Sfreza #define TEMAC_RXPERIOD 1 /* Interrupt every N descriptors. */
104e388b581Sfreza #define TEMAC_RXTIMO_HZ 100 /* Rx reaper frequency */
105e388b581Sfreza
106e388b581Sfreza /* Next Tx descriptor and descriptor's offset WRT sc_cdaddr. */
107e388b581Sfreza #define TEMAC_TXSINC(n, i) (((n) + TEMAC_TXQLEN + (i)) % TEMAC_TXQLEN)
108e388b581Sfreza #define TEMAC_TXINC(n, i) (((n) + TEMAC_NTXDESC + (i)) % TEMAC_NTXDESC)
109e388b581Sfreza
110e388b581Sfreza #define TEMAC_TXSNEXT(n) TEMAC_TXSINC((n), 1)
111e388b581Sfreza #define TEMAC_TXNEXT(n) TEMAC_TXINC((n), 1)
112e388b581Sfreza #define TEMAC_TXDOFF(n) (offsetof(struct temac_control, cd_txdesc) + \
113e388b581Sfreza (n) * sizeof(struct cdmac_descr))
114e388b581Sfreza
115e388b581Sfreza /* Next Rx descriptor and descriptor's offset WRT sc_cdaddr. */
116e388b581Sfreza #define TEMAC_RXINC(n, i) (((n) + TEMAC_NRXDESC + (i)) % TEMAC_NRXDESC)
117e388b581Sfreza #define TEMAC_RXNEXT(n) TEMAC_RXINC((n), 1)
118e388b581Sfreza #define TEMAC_RXDOFF(n) (offsetof(struct temac_control, cd_rxdesc) + \
119e388b581Sfreza (n) * sizeof(struct cdmac_descr))
120e388b581Sfreza #define TEMAC_ISINTR(i) (((i) % TEMAC_RXPERIOD) == 0)
121e388b581Sfreza #define TEMAC_ISLAST(i) ((i) == (TEMAC_NRXDESC - 1))
122e388b581Sfreza
123e388b581Sfreza
124e388b581Sfreza struct temac_control {
125e388b581Sfreza struct cdmac_descr cd_txdesc[TEMAC_NTXDESC];
126e388b581Sfreza struct cdmac_descr cd_rxdesc[TEMAC_NRXDESC];
127e388b581Sfreza };
128e388b581Sfreza
129e388b581Sfreza struct temac_txsoft {
130e388b581Sfreza bus_dmamap_t txs_dmap;
131e388b581Sfreza struct mbuf *txs_mbuf;
132e388b581Sfreza int txs_last;
133e388b581Sfreza };
134e388b581Sfreza
135e388b581Sfreza struct temac_rxsoft {
136e388b581Sfreza bus_dmamap_t rxs_dmap;
137e388b581Sfreza struct mbuf *rxs_mbuf;
138e388b581Sfreza };
139e388b581Sfreza
140e388b581Sfreza struct temac_softc {
141534c8a68Smatt device_t sc_dev;
142e388b581Sfreza struct ethercom sc_ec;
143e388b581Sfreza #define sc_if sc_ec.ec_if
144e388b581Sfreza
145e388b581Sfreza /* Peripheral registers */
146e388b581Sfreza bus_space_tag_t sc_iot;
147e388b581Sfreza bus_space_handle_t sc_ioh;
148e388b581Sfreza
149e388b581Sfreza /* CDMAC channel registers */
150e388b581Sfreza bus_space_tag_t sc_dma_rxt;
151e388b581Sfreza bus_space_handle_t sc_dma_rxh; /* Rx channel */
152e388b581Sfreza bus_space_handle_t sc_dma_rsh; /* Rx status */
153e388b581Sfreza
154e388b581Sfreza bus_space_tag_t sc_dma_txt;
155e388b581Sfreza bus_space_handle_t sc_dma_txh; /* Tx channel */
156e388b581Sfreza bus_space_handle_t sc_dma_tsh; /* Tx status */
157e388b581Sfreza
158e388b581Sfreza struct temac_txsoft sc_txsoft[TEMAC_TXQLEN];
159e388b581Sfreza struct temac_rxsoft sc_rxsoft[TEMAC_NRXDESC];
160e388b581Sfreza
161e388b581Sfreza struct callout sc_rx_timo;
162e388b581Sfreza struct callout sc_mii_tick;
163e388b581Sfreza struct mii_data sc_mii;
164e388b581Sfreza
165e388b581Sfreza bus_dmamap_t sc_control_dmap;
166e388b581Sfreza #define sc_cdaddr sc_control_dmap->dm_segs[0].ds_addr
167e388b581Sfreza
168e388b581Sfreza struct temac_control *sc_control_data;
169e388b581Sfreza #define sc_rxdescs sc_control_data->cd_rxdesc
170e388b581Sfreza #define sc_txdescs sc_control_data->cd_txdesc
171e388b581Sfreza
172e388b581Sfreza int sc_txbusy;
173e388b581Sfreza
174e388b581Sfreza int sc_txfree;
175e388b581Sfreza int sc_txcur;
176e388b581Sfreza int sc_txreap;
177e388b581Sfreza
178e388b581Sfreza int sc_rxreap;
179e388b581Sfreza
180e388b581Sfreza int sc_txsfree;
181e388b581Sfreza int sc_txscur;
182e388b581Sfreza int sc_txsreap;
183e388b581Sfreza
184e388b581Sfreza int sc_dead; /* Rx/Tx DMA error (fatal) */
185e388b581Sfreza int sc_rx_drained;
186e388b581Sfreza
187e388b581Sfreza int sc_rx_chan;
188e388b581Sfreza int sc_tx_chan;
189e388b581Sfreza
190e388b581Sfreza void *sc_sdhook;
191e388b581Sfreza void *sc_rx_ih;
192e388b581Sfreza void *sc_tx_ih;
193e388b581Sfreza
194e388b581Sfreza bus_dma_tag_t sc_dmat;
195e388b581Sfreza };
196e388b581Sfreza
197e388b581Sfreza /* Device interface. */
198534c8a68Smatt static void temac_attach(device_t, device_t, void *);
199e388b581Sfreza
200e388b581Sfreza /* Ifnet interface. */
201e388b581Sfreza static int temac_init(struct ifnet *);
20253524e44Schristos static int temac_ioctl(struct ifnet *, u_long, void *);
203e388b581Sfreza static void temac_start(struct ifnet *);
204e388b581Sfreza static void temac_stop(struct ifnet *, int);
205e388b581Sfreza
206e388b581Sfreza /* Media management. */
207e746222fSmsaitoh static int temac_mii_readreg(device_t, int, int, uint16_t *);
208e20bd029Smatt static void temac_mii_statchg(struct ifnet *);
209e388b581Sfreza static void temac_mii_tick(void *);
210e746222fSmsaitoh static int temac_mii_writereg(device_t, int, int, uint16_t);
211e388b581Sfreza
212e388b581Sfreza /* Indirect hooks. */
213e388b581Sfreza static void temac_shutdown(void *);
214e388b581Sfreza static void temac_rx_intr(void *);
215e388b581Sfreza static void temac_tx_intr(void *);
216e388b581Sfreza
217e388b581Sfreza /* Tools. */
218e388b581Sfreza static inline void temac_rxcdsync(struct temac_softc *, int, int, int);
219e388b581Sfreza static inline void temac_txcdsync(struct temac_softc *, int, int, int);
220e388b581Sfreza static void temac_txreap(struct temac_softc *);
221e388b581Sfreza static void temac_rxreap(struct temac_softc *);
222e388b581Sfreza static int temac_rxalloc(struct temac_softc *, int, int);
223e388b581Sfreza static void temac_rxtimo(void *);
224e388b581Sfreza static void temac_rxdrain(struct temac_softc *);
225e388b581Sfreza static void temac_reset(struct temac_softc *);
226e388b581Sfreza static void temac_txkick(struct temac_softc *);
227e388b581Sfreza
228e388b581Sfreza /* Register access. */
229e388b581Sfreza static inline void gmi_write_8(uint32_t, uint32_t, uint32_t);
230e388b581Sfreza static inline void gmi_write_4(uint32_t, uint32_t);
231e388b581Sfreza static inline void gmi_read_8(uint32_t, uint32_t *, uint32_t *);
232e388b581Sfreza static inline uint32_t gmi_read_4(uint32_t);
23360a8b3b4Srin static inline int hif_wait_stat(uint32_t);
234e388b581Sfreza
235e388b581Sfreza #define cdmac_rx_stat(sc) \
236e388b581Sfreza bus_space_read_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0 /* XXX hack */)
237e388b581Sfreza
238e388b581Sfreza #define cdmac_rx_reset(sc) \
239e388b581Sfreza bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rsh, 0, CDMAC_STAT_RESET)
240e388b581Sfreza
241e388b581Sfreza #define cdmac_rx_start(sc, val) \
242e388b581Sfreza bus_space_write_4((sc)->sc_dma_rxt, (sc)->sc_dma_rxh, CDMAC_CURDESC, (val))
243e388b581Sfreza
244e388b581Sfreza #define cdmac_tx_stat(sc) \
245e388b581Sfreza bus_space_read_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0 /* XXX hack */)
246e388b581Sfreza
247e388b581Sfreza #define cdmac_tx_reset(sc) \
248e388b581Sfreza bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_tsh, 0, CDMAC_STAT_RESET)
249e388b581Sfreza
250e388b581Sfreza #define cdmac_tx_start(sc, val) \
251e388b581Sfreza bus_space_write_4((sc)->sc_dma_txt, (sc)->sc_dma_txh, CDMAC_CURDESC, (val))
252e388b581Sfreza
253e388b581Sfreza
254534c8a68Smatt CFATTACH_DECL_NEW(temac, sizeof(struct temac_softc),
255e388b581Sfreza xcvbus_child_match, temac_attach, NULL, NULL);
256e388b581Sfreza
257e388b581Sfreza
258e388b581Sfreza /*
259e388b581Sfreza * Private bus utilities.
260e388b581Sfreza */
261e746222fSmsaitoh static inline int
hif_wait_stat(uint32_t mask)262e388b581Sfreza hif_wait_stat(uint32_t mask)
263e388b581Sfreza {
264e388b581Sfreza int i = 0;
265e746222fSmsaitoh int rv = 0;
266e388b581Sfreza
267e388b581Sfreza while (mask != (mfidcr(IDCR_HIF_STAT) & mask)) {
268e388b581Sfreza if (i++ > 100) {
269e388b581Sfreza printf("%s: timeout waiting for 0x%08x\n",
270e388b581Sfreza __func__, mask);
271e746222fSmsaitoh rv = ETIMEDOUT;
272e388b581Sfreza break;
273e388b581Sfreza }
274e388b581Sfreza delay(5);
275e388b581Sfreza }
276e388b581Sfreza
277e388b581Sfreza TRACEREG(("%s: stat %#08x loops %d\n", __func__, mask, i));
278e746222fSmsaitoh return rv;
279e388b581Sfreza }
280e388b581Sfreza
281e388b581Sfreza static inline void
gmi_write_4(uint32_t addr,uint32_t lo)282e388b581Sfreza gmi_write_4(uint32_t addr, uint32_t lo)
283e388b581Sfreza {
284e388b581Sfreza mtidcr(IDCR_HIF_ARG0, lo);
285e388b581Sfreza mtidcr(IDCR_HIF_CTRL, (addr & HIF_CTRL_GMIADDR) | HIF_CTRL_WRITE);
286e388b581Sfreza hif_wait_stat(HIF_STAT_GMIWR);
287e388b581Sfreza
288e388b581Sfreza TRACEREG(("%s: %#08x <- %#08x\n", __func__, addr, lo));
289e388b581Sfreza }
290e388b581Sfreza
291c622f291Sriastradh static inline void __unused
gmi_write_8(uint32_t addr,uint32_t lo,uint32_t hi)292e388b581Sfreza gmi_write_8(uint32_t addr, uint32_t lo, uint32_t hi)
293e388b581Sfreza {
294e388b581Sfreza mtidcr(IDCR_HIF_ARG1, hi);
295e388b581Sfreza gmi_write_4(addr, lo);
296e388b581Sfreza }
297e388b581Sfreza
298c622f291Sriastradh static inline void __unused
gmi_read_8(uint32_t addr,uint32_t * lo,uint32_t * hi)299e388b581Sfreza gmi_read_8(uint32_t addr, uint32_t *lo, uint32_t *hi)
300e388b581Sfreza {
301e388b581Sfreza *lo = gmi_read_4(addr);
302e388b581Sfreza *hi = mfidcr(IDCR_HIF_ARG1);
303e388b581Sfreza }
304e388b581Sfreza
305e388b581Sfreza static inline uint32_t
gmi_read_4(uint32_t addr)306e388b581Sfreza gmi_read_4(uint32_t addr)
307e388b581Sfreza {
308e388b581Sfreza uint32_t res;
309e388b581Sfreza
310e388b581Sfreza mtidcr(IDCR_HIF_CTRL, addr & HIF_CTRL_GMIADDR);
311e388b581Sfreza hif_wait_stat(HIF_STAT_GMIRR);
312e388b581Sfreza
313e388b581Sfreza res = mfidcr(IDCR_HIF_ARG0);
314e388b581Sfreza TRACEREG(("%s: %#08x -> %#08x\n", __func__, addr, res));
315e388b581Sfreza return (res);
316e388b581Sfreza }
317e388b581Sfreza
318e388b581Sfreza /*
319e388b581Sfreza * Generic device.
320e388b581Sfreza */
321e388b581Sfreza static void
temac_attach(device_t parent,device_t self,void * aux)322534c8a68Smatt temac_attach(device_t parent, device_t self, void *aux)
323e388b581Sfreza {
324e388b581Sfreza struct xcvbus_attach_args *vaa = aux;
325e388b581Sfreza struct ll_dmac *rx = vaa->vaa_rx_dmac;
326e388b581Sfreza struct ll_dmac *tx = vaa->vaa_tx_dmac;
327534c8a68Smatt struct temac_softc *sc = device_private(self);
328e388b581Sfreza struct ifnet *ifp = &sc->sc_if;
329e388b581Sfreza struct mii_data *mii = &sc->sc_mii;
330e388b581Sfreza uint8_t enaddr[ETHER_ADDR_LEN];
331e388b581Sfreza bus_dma_segment_t seg;
332e388b581Sfreza int error, nseg, i;
333534c8a68Smatt const char * const xname = device_xname(self);
334e388b581Sfreza
335534c8a68Smatt aprint_normal(": TEMAC\n"); /* XXX will be LL_TEMAC, PLB_TEMAC */
336e388b581Sfreza
337e388b581Sfreza KASSERT(rx);
338e388b581Sfreza KASSERT(tx);
339e388b581Sfreza
340534c8a68Smatt sc->sc_dev = self;
341e388b581Sfreza sc->sc_dmat = vaa->vaa_dmat;
342e388b581Sfreza sc->sc_dead = 0;
343e388b581Sfreza sc->sc_rx_drained = 1;
344e388b581Sfreza sc->sc_txbusy = 0;
345e388b581Sfreza sc->sc_iot = vaa->vaa_iot;
346e388b581Sfreza sc->sc_dma_rxt = rx->dmac_iot;
347e388b581Sfreza sc->sc_dma_txt = tx->dmac_iot;
348e388b581Sfreza
349e388b581Sfreza /*
350e388b581Sfreza * Map HIF and receive/transmit dmac registers.
351e388b581Sfreza */
352e388b581Sfreza if ((error = bus_space_map(vaa->vaa_iot, vaa->vaa_addr, TEMAC_SIZE, 0,
353e388b581Sfreza &sc->sc_ioh)) != 0) {
354534c8a68Smatt aprint_error_dev(self, "could not map registers\n");
355e388b581Sfreza goto fail_0;
356e388b581Sfreza }
357e388b581Sfreza
358e388b581Sfreza if ((error = bus_space_map(sc->sc_dma_rxt, rx->dmac_ctrl_addr,
359e388b581Sfreza CDMAC_CTRL_SIZE, 0, &sc->sc_dma_rxh)) != 0) {
360534c8a68Smatt aprint_error_dev(self, "could not map Rx control registers\n");
361e388b581Sfreza goto fail_0;
362e388b581Sfreza }
363e388b581Sfreza if ((error = bus_space_map(sc->sc_dma_rxt, rx->dmac_stat_addr,
364e388b581Sfreza CDMAC_STAT_SIZE, 0, &sc->sc_dma_rsh)) != 0) {
365534c8a68Smatt aprint_error_dev(self, "could not map Rx status register\n");
366e388b581Sfreza goto fail_0;
367e388b581Sfreza }
368e388b581Sfreza
369e388b581Sfreza if ((error = bus_space_map(sc->sc_dma_txt, tx->dmac_ctrl_addr,
370e388b581Sfreza CDMAC_CTRL_SIZE, 0, &sc->sc_dma_txh)) != 0) {
371534c8a68Smatt aprint_error_dev(self, "could not map Tx control registers\n");
372e388b581Sfreza goto fail_0;
373e388b581Sfreza }
374e388b581Sfreza if ((error = bus_space_map(sc->sc_dma_txt, tx->dmac_stat_addr,
375e388b581Sfreza CDMAC_STAT_SIZE, 0, &sc->sc_dma_tsh)) != 0) {
376534c8a68Smatt aprint_error_dev(self, "could not map Tx status register\n");
377e388b581Sfreza goto fail_0;
378e388b581Sfreza }
379e388b581Sfreza
380e388b581Sfreza /*
381e388b581Sfreza * Allocate and initialize DMA control chains.
382e388b581Sfreza */
383e388b581Sfreza if ((error = bus_dmamem_alloc(sc->sc_dmat,
384e388b581Sfreza sizeof(struct temac_control), 8, 0, &seg, 1, &nseg, 0)) != 0) {
385534c8a68Smatt aprint_error_dev(self, "could not allocate control data\n");
386e388b581Sfreza goto fail_0;
387e388b581Sfreza }
388e388b581Sfreza
389e388b581Sfreza if ((error = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
390e388b581Sfreza sizeof(struct temac_control),
39153524e44Schristos (void **)&sc->sc_control_data, BUS_DMA_COHERENT)) != 0) {
392534c8a68Smatt aprint_error_dev(self, "could not map control data\n");
393e388b581Sfreza goto fail_1;
394e388b581Sfreza }
395e388b581Sfreza
396e388b581Sfreza if ((error = bus_dmamap_create(sc->sc_dmat,
397e388b581Sfreza sizeof(struct temac_control), 1,
398e388b581Sfreza sizeof(struct temac_control), 0, 0, &sc->sc_control_dmap)) != 0) {
399534c8a68Smatt aprint_error_dev(self,
400534c8a68Smatt "could not create control data DMA map\n");
401e388b581Sfreza goto fail_2;
402e388b581Sfreza }
403e388b581Sfreza
404e388b581Sfreza if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_control_dmap,
405e388b581Sfreza sc->sc_control_data, sizeof(struct temac_control), NULL, 0)) != 0) {
406534c8a68Smatt aprint_error_dev(self, "could not load control data DMA map\n");
407e388b581Sfreza goto fail_3;
408e388b581Sfreza }
409e388b581Sfreza
410e388b581Sfreza /*
411e388b581Sfreza * Link descriptor chains.
412e388b581Sfreza */
413e388b581Sfreza memset(sc->sc_control_data, 0, sizeof(struct temac_control));
414e388b581Sfreza
415e388b581Sfreza for (i = 0; i < TEMAC_NTXDESC; i++) {
416e388b581Sfreza sc->sc_txdescs[i].desc_next = sc->sc_cdaddr +
417e388b581Sfreza TEMAC_TXDOFF(TEMAC_TXNEXT(i));
418e388b581Sfreza sc->sc_txdescs[i].desc_stat = CDMAC_STAT_DONE;
419e388b581Sfreza }
420e388b581Sfreza for (i = 0; i < TEMAC_NRXDESC; i++) {
421e388b581Sfreza sc->sc_rxdescs[i].desc_next = sc->sc_cdaddr +
422e388b581Sfreza TEMAC_RXDOFF(TEMAC_RXNEXT(i));
423e388b581Sfreza sc->sc_txdescs[i].desc_stat = CDMAC_STAT_DONE;
424e388b581Sfreza }
425e388b581Sfreza
426e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap, 0,
427e388b581Sfreza sizeof(struct temac_control),
428e388b581Sfreza BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
429e388b581Sfreza
430e388b581Sfreza /*
431e388b581Sfreza * Initialize software state for transmit/receive jobs.
432e388b581Sfreza */
433e388b581Sfreza for (i = 0; i < TEMAC_TXQLEN; i++) {
434e388b581Sfreza if ((error = bus_dmamap_create(sc->sc_dmat,
435e388b581Sfreza ETHER_MAX_LEN_JUMBO, TEMAC_NTXSEG, ETHER_MAX_LEN_JUMBO,
436e388b581Sfreza 0, 0, &sc->sc_txsoft[i].txs_dmap)) != 0) {
437534c8a68Smatt aprint_error_dev(self,
438534c8a68Smatt "could not create Tx DMA map %d\n",
439534c8a68Smatt i);
440e388b581Sfreza goto fail_4;
441e388b581Sfreza }
442e388b581Sfreza sc->sc_txsoft[i].txs_mbuf = NULL;
443e388b581Sfreza sc->sc_txsoft[i].txs_last = 0;
444e388b581Sfreza }
445e388b581Sfreza
446e388b581Sfreza for (i = 0; i < TEMAC_NRXDESC; i++) {
447e388b581Sfreza if ((error = bus_dmamap_create(sc->sc_dmat,
448e388b581Sfreza MCLBYTES, TEMAC_NRXSEG, MCLBYTES, 0, 0,
449e388b581Sfreza &sc->sc_rxsoft[i].rxs_dmap)) != 0) {
450534c8a68Smatt aprint_error_dev(self,
451534c8a68Smatt "could not create Rx DMA map %d\n", i);
452e388b581Sfreza goto fail_5;
453e388b581Sfreza }
454e388b581Sfreza sc->sc_rxsoft[i].rxs_mbuf = NULL;
455e388b581Sfreza }
456e388b581Sfreza
457e388b581Sfreza /*
458e388b581Sfreza * Setup transfer interrupt handlers.
459e388b581Sfreza */
460e388b581Sfreza error = ENOMEM;
461e388b581Sfreza
462e388b581Sfreza sc->sc_rx_ih = ll_dmac_intr_establish(rx->dmac_chan,
463e388b581Sfreza temac_rx_intr, sc);
464e388b581Sfreza if (sc->sc_rx_ih == NULL) {
465534c8a68Smatt aprint_error_dev(self, "could not establish Rx interrupt\n");
466e388b581Sfreza goto fail_5;
467e388b581Sfreza }
468e388b581Sfreza
469e388b581Sfreza sc->sc_tx_ih = ll_dmac_intr_establish(tx->dmac_chan,
470e388b581Sfreza temac_tx_intr, sc);
471e388b581Sfreza if (sc->sc_tx_ih == NULL) {
472534c8a68Smatt aprint_error_dev(self, "could not establish Tx interrupt\n");
473e388b581Sfreza goto fail_6;
474e388b581Sfreza }
475e388b581Sfreza
476e388b581Sfreza /* XXXFreza: faked, should read unicast address filter. */
477e388b581Sfreza enaddr[0] = 0x00;
478e388b581Sfreza enaddr[1] = 0x11;
479e388b581Sfreza enaddr[2] = 0x17;
480e388b581Sfreza enaddr[3] = 0xff;
481e388b581Sfreza enaddr[4] = 0xff;
482e388b581Sfreza enaddr[5] = 0x01;
483e388b581Sfreza
484e388b581Sfreza /*
485e388b581Sfreza * Initialize the TEMAC.
486e388b581Sfreza */
487e388b581Sfreza temac_reset(sc);
488e388b581Sfreza
489e388b581Sfreza /* Configure MDIO link. */
490e388b581Sfreza gmi_write_4(TEMAC_GMI_MGMTCF, GMI_MGMT_CLKDIV_100MHz | GMI_MGMT_MDIO);
491e388b581Sfreza
492e388b581Sfreza /* Initialize PHY. */
493e388b581Sfreza mii->mii_ifp = ifp;
494e388b581Sfreza mii->mii_readreg = temac_mii_readreg;
495e388b581Sfreza mii->mii_writereg = temac_mii_writereg;
496e388b581Sfreza mii->mii_statchg = temac_mii_statchg;
497b480b622Sdyoung sc->sc_ec.ec_mii = mii;
498b480b622Sdyoung ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
499e388b581Sfreza
500534c8a68Smatt mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
501e388b581Sfreza MII_OFFSET_ANY, 0);
502e388b581Sfreza if (LIST_FIRST(&mii->mii_phys) == NULL) {
503e388b581Sfreza ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
504e388b581Sfreza ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
505e388b581Sfreza } else {
506e388b581Sfreza ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
507e388b581Sfreza }
508e388b581Sfreza
509e388b581Sfreza /* Hold PHY in reset. */
510e388b581Sfreza bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET, TEMAC_RESET_PHY);
511e388b581Sfreza
512e388b581Sfreza /* Reset EMAC. */
513e388b581Sfreza bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET,
514e388b581Sfreza TEMAC_RESET_EMAC);
515e388b581Sfreza delay(10000);
516e388b581Sfreza
517e388b581Sfreza /* Reset peripheral, awakes PHY and EMAC. */
518e388b581Sfreza bus_space_write_4(sc->sc_iot, sc->sc_ioh, TEMAC_RESET,
519e388b581Sfreza TEMAC_RESET_PERIPH);
520e388b581Sfreza delay(40000);
521e388b581Sfreza
522e388b581Sfreza /* (Re-)Configure MDIO link. */
523e388b581Sfreza gmi_write_4(TEMAC_GMI_MGMTCF, GMI_MGMT_CLKDIV_100MHz | GMI_MGMT_MDIO);
524e388b581Sfreza
525e388b581Sfreza /*
526e388b581Sfreza * Hook up with network stack.
527e388b581Sfreza */
528534c8a68Smatt strcpy(ifp->if_xname, xname);
529e388b581Sfreza ifp->if_softc = sc;
530e388b581Sfreza ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
531e388b581Sfreza ifp->if_ioctl = temac_ioctl;
532e388b581Sfreza ifp->if_start = temac_start;
533e388b581Sfreza ifp->if_init = temac_init;
534e388b581Sfreza ifp->if_stop = temac_stop;
535e388b581Sfreza ifp->if_watchdog = NULL;
536e388b581Sfreza IFQ_SET_READY(&ifp->if_snd);
537e388b581Sfreza IFQ_SET_MAXLEN(&ifp->if_snd, TEMAC_TXQLEN);
538e388b581Sfreza
539e388b581Sfreza sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
540e388b581Sfreza
541e388b581Sfreza if_attach(ifp);
542e388b581Sfreza ether_ifattach(ifp, enaddr);
543e388b581Sfreza
544e388b581Sfreza sc->sc_sdhook = shutdownhook_establish(temac_shutdown, sc);
545e388b581Sfreza if (sc->sc_sdhook == NULL)
546534c8a68Smatt aprint_error_dev(self,
547534c8a68Smatt "WARNING: unable to establish shutdown hook\n");
548e388b581Sfreza
549e388b581Sfreza callout_setfunc(&sc->sc_mii_tick, temac_mii_tick, sc);
550e388b581Sfreza callout_setfunc(&sc->sc_rx_timo, temac_rxtimo, sc);
551e388b581Sfreza
552e388b581Sfreza return ;
553e388b581Sfreza
554e388b581Sfreza fail_6:
555e388b581Sfreza ll_dmac_intr_disestablish(rx->dmac_chan, sc->sc_rx_ih);
556e388b581Sfreza i = TEMAC_NRXDESC;
557e388b581Sfreza fail_5:
558e388b581Sfreza for (--i; i >= 0; i--)
559e388b581Sfreza bus_dmamap_destroy(sc->sc_dmat, sc->sc_rxsoft[i].rxs_dmap);
560e388b581Sfreza i = TEMAC_TXQLEN;
561e388b581Sfreza fail_4:
562e388b581Sfreza for (--i; i >= 0; i--)
563e388b581Sfreza bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsoft[i].txs_dmap);
564e388b581Sfreza fail_3:
565e388b581Sfreza bus_dmamap_destroy(sc->sc_dmat, sc->sc_control_dmap);
566e388b581Sfreza fail_2:
56753524e44Schristos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
568e388b581Sfreza sizeof(struct temac_control));
569e388b581Sfreza fail_1:
570e388b581Sfreza bus_dmamem_free(sc->sc_dmat, &seg, nseg);
571e388b581Sfreza fail_0:
572534c8a68Smatt aprint_error_dev(self, "error = %d\n", error);
573e388b581Sfreza }
574e388b581Sfreza
575e388b581Sfreza /*
576e388b581Sfreza * Network device.
577e388b581Sfreza */
578e388b581Sfreza static int
temac_init(struct ifnet * ifp)579e388b581Sfreza temac_init(struct ifnet *ifp)
580e388b581Sfreza {
581e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)ifp->if_softc;
582e388b581Sfreza uint32_t rcr, tcr;
583e388b581Sfreza int i, error;
584e388b581Sfreza
585e388b581Sfreza /* Reset DMA channels. */
586e388b581Sfreza cdmac_tx_reset(sc);
587e388b581Sfreza cdmac_rx_reset(sc);
588e388b581Sfreza
589e388b581Sfreza /* Set current media. */
590b480b622Sdyoung if ((error = ether_mediachange(ifp)) != 0)
591b480b622Sdyoung return error;
592b480b622Sdyoung
593e388b581Sfreza callout_schedule(&sc->sc_mii_tick, hz);
594e388b581Sfreza
595e388b581Sfreza /* Enable EMAC engine. */
596e388b581Sfreza rcr = (gmi_read_4(TEMAC_GMI_RXCF1) | GMI_RX_ENABLE) &
597e388b581Sfreza ~(GMI_RX_JUMBO | GMI_RX_FCS);
598e388b581Sfreza gmi_write_4(TEMAC_GMI_RXCF1, rcr);
599e388b581Sfreza
600e388b581Sfreza tcr = (gmi_read_4(TEMAC_GMI_TXCF) | GMI_TX_ENABLE) &
601e388b581Sfreza ~(GMI_TX_JUMBO | GMI_TX_FCS);
602e388b581Sfreza gmi_write_4(TEMAC_GMI_TXCF, tcr);
603e388b581Sfreza
604e388b581Sfreza /* XXXFreza: Force promiscuous mode, for now. */
605e388b581Sfreza gmi_write_4(TEMAC_GMI_AFM, GMI_AFM_PROMISC);
606e388b581Sfreza ifp->if_flags |= IFF_PROMISC;
607e388b581Sfreza
608e388b581Sfreza /* Rx/Tx queues are drained -- either from attach() or stop(). */
609e388b581Sfreza sc->sc_txsfree = TEMAC_TXQLEN;
610e388b581Sfreza sc->sc_txsreap = 0;
611e388b581Sfreza sc->sc_txscur = 0;
612e388b581Sfreza
613e388b581Sfreza sc->sc_txfree = TEMAC_NTXDESC;
614e388b581Sfreza sc->sc_txreap = 0;
615e388b581Sfreza sc->sc_txcur = 0;
616e388b581Sfreza
617e388b581Sfreza sc->sc_rxreap = 0;
618e388b581Sfreza
619e388b581Sfreza /* Allocate and map receive buffers. */
620e388b581Sfreza if (sc->sc_rx_drained) {
621e388b581Sfreza for (i = 0; i < TEMAC_NRXDESC; i++) {
622e388b581Sfreza if ((error = temac_rxalloc(sc, i, 1)) != 0) {
623534c8a68Smatt aprint_error_dev(sc->sc_dev,
624534c8a68Smatt "failed to allocate Rx descriptor %d\n",
625534c8a68Smatt i);
626e388b581Sfreza temac_rxdrain(sc);
627e388b581Sfreza return (error);
628e388b581Sfreza }
629e388b581Sfreza }
630e388b581Sfreza sc->sc_rx_drained = 0;
631e388b581Sfreza
632e388b581Sfreza temac_rxcdsync(sc, 0, TEMAC_NRXDESC,
633e388b581Sfreza BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
634e388b581Sfreza cdmac_rx_start(sc, sc->sc_cdaddr + TEMAC_RXDOFF(0));
635e388b581Sfreza }
636e388b581Sfreza
637e388b581Sfreza ifp->if_flags |= IFF_RUNNING;
638e388b581Sfreza
639e388b581Sfreza return (0);
640e388b581Sfreza }
641e388b581Sfreza
642e388b581Sfreza static int
temac_ioctl(struct ifnet * ifp,u_long cmd,void * data)64353524e44Schristos temac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
644e388b581Sfreza {
645e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)ifp->if_softc;
646e388b581Sfreza int s, ret;
647e388b581Sfreza
648e388b581Sfreza s = splnet();
649b480b622Sdyoung if (sc->sc_dead)
650e388b581Sfreza ret = EIO;
651b480b622Sdyoung else
652e388b581Sfreza ret = ether_ioctl(ifp, cmd, data);
653e388b581Sfreza splx(s);
654e388b581Sfreza return (ret);
655e388b581Sfreza }
656e388b581Sfreza
657e388b581Sfreza static void
temac_start(struct ifnet * ifp)658e388b581Sfreza temac_start(struct ifnet *ifp)
659e388b581Sfreza {
660e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)ifp->if_softc;
661e388b581Sfreza struct temac_txsoft *txs;
662e388b581Sfreza struct mbuf *m;
663e388b581Sfreza bus_dmamap_t dmap;
664e388b581Sfreza int error, head, nsegs, i;
665e388b581Sfreza
666e388b581Sfreza nsegs = 0;
667e388b581Sfreza head = sc->sc_txcur;
668e388b581Sfreza txs = NULL; /* gcc */
669e388b581Sfreza
670e388b581Sfreza if (sc->sc_dead)
671e388b581Sfreza return;
672e388b581Sfreza
673e388b581Sfreza KASSERT(sc->sc_txfree >= 0);
674e388b581Sfreza KASSERT(sc->sc_txsfree >= 0);
675e388b581Sfreza
676e388b581Sfreza /*
677e388b581Sfreza * Push mbufs into descriptor chain until we drain the interface
678e388b581Sfreza * queue or run out of descriptors. We'll mark the first segment
679e388b581Sfreza * as "done" in hope that we might put CDMAC interrupt above IPL_NET
680e388b581Sfreza * and have it start jobs & mark packets for GC preemtively for
681e388b581Sfreza * us -- creativity due to limitations in CDMAC transfer engine
682e388b581Sfreza * (it really consumes lists, not circular queues, AFAICS).
683e388b581Sfreza *
684e388b581Sfreza * We schedule one interrupt per Tx batch.
685e388b581Sfreza */
686*100fd80aSthorpej while (sc->sc_txsfree) {
687e388b581Sfreza IFQ_POLL(&ifp->if_snd, m);
688e388b581Sfreza if (m == NULL)
689e388b581Sfreza break;
690e388b581Sfreza
691e388b581Sfreza txs = &sc->sc_txsoft[sc->sc_txscur];
692e388b581Sfreza dmap = txs->txs_dmap;
693e388b581Sfreza
694e388b581Sfreza if (txs->txs_mbuf != NULL)
695e388b581Sfreza printf("FOO\n");
696e388b581Sfreza if (txs->txs_last)
697e388b581Sfreza printf("BAR\n");
698e388b581Sfreza
699e388b581Sfreza if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmap, m,
700e388b581Sfreza BUS_DMA_WRITE | BUS_DMA_NOWAIT)) != 0) {
701e388b581Sfreza if (error == EFBIG) {
702534c8a68Smatt aprint_error_dev(sc->sc_dev,
703534c8a68Smatt "Tx consumes too many segments, dropped\n");
704e388b581Sfreza IFQ_DEQUEUE(&ifp->if_snd, m);
705e388b581Sfreza m_freem(m);
706e388b581Sfreza continue;
707e388b581Sfreza } else {
708534c8a68Smatt aprint_debug_dev(sc->sc_dev,
709534c8a68Smatt "Tx stall due to resource shortage\n");
710e388b581Sfreza break;
711e388b581Sfreza }
712e388b581Sfreza }
713e388b581Sfreza
714e388b581Sfreza /*
715*100fd80aSthorpej * If we're short on DMA descriptors; leave this packet
716*100fd80aSthorpej * for later.
717e388b581Sfreza */
718e388b581Sfreza if (dmap->dm_nsegs > sc->sc_txfree) {
719e388b581Sfreza bus_dmamap_unload(sc->sc_dmat, dmap);
720e388b581Sfreza break;
721e388b581Sfreza }
722e388b581Sfreza
723e388b581Sfreza IFQ_DEQUEUE(&ifp->if_snd, m);
724e388b581Sfreza
725e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
726e388b581Sfreza BUS_DMASYNC_PREWRITE);
727e388b581Sfreza txs->txs_mbuf = m;
728e388b581Sfreza
729e388b581Sfreza /*
730e388b581Sfreza * Map the packet into descriptor chain. XXX We'll want
731e388b581Sfreza * to fill checksum offload commands here.
732e388b581Sfreza *
733e388b581Sfreza * We would be in a race if we weren't blocking CDMAC intr
734e388b581Sfreza * at this point -- we need to be locked against txreap()
735e388b581Sfreza * because of dmasync ops.
736e388b581Sfreza */
737e388b581Sfreza
738e388b581Sfreza temac_txcdsync(sc, sc->sc_txcur, dmap->dm_nsegs,
739e388b581Sfreza BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
740e388b581Sfreza
741e388b581Sfreza for (i = 0; i < dmap->dm_nsegs; i++) {
742e388b581Sfreza sc->sc_txdescs[sc->sc_txcur].desc_addr =
743e388b581Sfreza dmap->dm_segs[i].ds_addr;
744e388b581Sfreza sc->sc_txdescs[sc->sc_txcur].desc_size =
745e388b581Sfreza dmap->dm_segs[i].ds_len;
746e388b581Sfreza sc->sc_txdescs[sc->sc_txcur].desc_stat =
747e388b581Sfreza (i == 0 ? CDMAC_STAT_SOP : 0) |
748e388b581Sfreza (i == (dmap->dm_nsegs - 1) ? CDMAC_STAT_EOP : 0);
749e388b581Sfreza
750e388b581Sfreza sc->sc_txcur = TEMAC_TXNEXT(sc->sc_txcur);
751e388b581Sfreza }
752e388b581Sfreza
753e388b581Sfreza sc->sc_txfree -= dmap->dm_nsegs;
754e388b581Sfreza nsegs += dmap->dm_nsegs;
755e388b581Sfreza
756e388b581Sfreza sc->sc_txscur = TEMAC_TXSNEXT(sc->sc_txscur);
757e388b581Sfreza sc->sc_txsfree--;
758e388b581Sfreza }
759e388b581Sfreza
760e388b581Sfreza /* Get data running if we queued any. */
761e388b581Sfreza if (nsegs > 0) {
762e388b581Sfreza int tail = TEMAC_TXINC(sc->sc_txcur, -1);
763e388b581Sfreza
764e388b581Sfreza /* Mark the last packet in this job. */
765e388b581Sfreza txs->txs_last = 1;
766e388b581Sfreza
767e388b581Sfreza /* Mark the last descriptor in this job. */
768e388b581Sfreza sc->sc_txdescs[tail].desc_stat |= CDMAC_STAT_STOP |
769e388b581Sfreza CDMAC_STAT_INTR;
770e388b581Sfreza temac_txcdsync(sc, head, nsegs,
771e388b581Sfreza BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
772e388b581Sfreza
773e388b581Sfreza temac_txkick(sc);
774e388b581Sfreza #if TEMAC_TXDEBUG > 0
775534c8a68Smatt aprint_debug_dev(sc->sc_dev,
776534c8a68Smatt "start: txcur %03d -> %03d, nseg %03d\n",
777534c8a68Smatt head, sc->sc_txcur, nsegs);
778e388b581Sfreza #endif
779e388b581Sfreza }
780e388b581Sfreza }
781e388b581Sfreza
782e388b581Sfreza static void
temac_stop(struct ifnet * ifp,int disable)783e388b581Sfreza temac_stop(struct ifnet *ifp, int disable)
784e388b581Sfreza {
785e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)ifp->if_softc;
786e388b581Sfreza struct temac_txsoft *txs;
787e388b581Sfreza int i;
788e388b581Sfreza
789e388b581Sfreza #if TEMAC_DEBUG > 0
790534c8a68Smatt aprint_debug_dev(sc->sc_dev, "stop\n");
791e388b581Sfreza #endif
792e388b581Sfreza
793e388b581Sfreza /* Down the MII. */
794e388b581Sfreza callout_stop(&sc->sc_mii_tick);
795e388b581Sfreza mii_down(&sc->sc_mii);
796e388b581Sfreza
797e388b581Sfreza /* Stop the engine. */
798e388b581Sfreza temac_reset(sc);
799e388b581Sfreza
800e388b581Sfreza /* Drain buffers queues (unconditionally). */
801e388b581Sfreza temac_rxdrain(sc);
802e388b581Sfreza
803e388b581Sfreza for (i = 0; i < TEMAC_TXQLEN; i++) {
804e388b581Sfreza txs = &sc->sc_txsoft[i];
805e388b581Sfreza
806e388b581Sfreza if (txs->txs_mbuf != NULL) {
807e388b581Sfreza bus_dmamap_unload(sc->sc_dmat, txs->txs_dmap);
808e388b581Sfreza m_freem(txs->txs_mbuf);
809e388b581Sfreza txs->txs_mbuf = NULL;
810e388b581Sfreza txs->txs_last = 0;
811e388b581Sfreza }
812e388b581Sfreza }
813e388b581Sfreza sc->sc_txbusy = 0;
814e388b581Sfreza
815e388b581Sfreza /* Acknowledge we're down. */
816*100fd80aSthorpej ifp->if_flags &= ~IFF_RUNNING;
817e388b581Sfreza }
818e388b581Sfreza
819e388b581Sfreza static int
temac_mii_readreg(device_t self,int phy,int reg,uint16_t * val)820e746222fSmsaitoh temac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
821e388b581Sfreza {
822e746222fSmsaitoh int rv;
823e746222fSmsaitoh
824e388b581Sfreza mtidcr(IDCR_HIF_ARG0, (phy << 5) | reg);
825e388b581Sfreza mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_ADDR);
826e388b581Sfreza
827e746222fSmsaitoh if ((rv = hif_wait_stat(HIF_STAT_MIIRR)) != 0)
828e746222fSmsaitoh return rv;
829e746222fSmsaitoh
830e746222fSmsaitoh *val = mfidcr(IDCR_HIF_ARG0) & 0xffff;
831e746222fSmsaitoh return 0;
832e388b581Sfreza }
833e388b581Sfreza
834e746222fSmsaitoh static int
temac_mii_writereg(device_t self,int phy,int reg,uint16_t val)835e746222fSmsaitoh temac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
836e388b581Sfreza {
837e388b581Sfreza mtidcr(IDCR_HIF_ARG0, val);
838e388b581Sfreza mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_WRVAL | HIF_CTRL_WRITE);
839e388b581Sfreza mtidcr(IDCR_HIF_ARG0, (phy << 5) | reg);
840e388b581Sfreza mtidcr(IDCR_HIF_CTRL, TEMAC_GMI_MII_ADDR | HIF_CTRL_WRITE);
841e746222fSmsaitoh return hif_wait_stat(HIF_STAT_MIIWR);
842e388b581Sfreza }
843e388b581Sfreza
844e388b581Sfreza static void
temac_mii_statchg(struct ifnet * ifp)845e20bd029Smatt temac_mii_statchg(struct ifnet *ifp)
846e388b581Sfreza {
847e20bd029Smatt struct temac_softc *sc = ifp->if_softc;
848e388b581Sfreza uint32_t rcf, tcf, mmc;
849e388b581Sfreza
850e388b581Sfreza /* Full/half duplex link. */
851e388b581Sfreza rcf = gmi_read_4(TEMAC_GMI_RXCF1);
852e388b581Sfreza tcf = gmi_read_4(TEMAC_GMI_TXCF);
853e388b581Sfreza
854e388b581Sfreza if (sc->sc_mii.mii_media_active & IFM_FDX) {
855e388b581Sfreza gmi_write_4(TEMAC_GMI_RXCF1, rcf & ~GMI_RX_HDX);
856e388b581Sfreza gmi_write_4(TEMAC_GMI_TXCF, tcf & ~GMI_TX_HDX);
857e388b581Sfreza } else {
858e388b581Sfreza gmi_write_4(TEMAC_GMI_RXCF1, rcf | GMI_RX_HDX);
859e388b581Sfreza gmi_write_4(TEMAC_GMI_TXCF, tcf | GMI_TX_HDX);
860e388b581Sfreza }
861e388b581Sfreza
862e388b581Sfreza /* Link speed. */
863e388b581Sfreza mmc = gmi_read_4(TEMAC_GMI_MMC) & ~GMI_MMC_SPEED_MASK;
864e388b581Sfreza
865e388b581Sfreza switch (IFM_SUBTYPE(sc->sc_mii.mii_media_active)) {
866e388b581Sfreza case IFM_10_T:
867e388b581Sfreza /*
868e388b581Sfreza * XXXFreza: the GMAC is not happy with 10Mbit ethernet,
869e388b581Sfreza * although the documentation claims it's supported. Maybe
870e388b581Sfreza * it's just my equipment...
871e388b581Sfreza */
872e388b581Sfreza mmc |= GMI_MMC_SPEED_10;
873e388b581Sfreza break;
874e388b581Sfreza case IFM_100_TX:
875e388b581Sfreza mmc |= GMI_MMC_SPEED_100;
876e388b581Sfreza break;
877e388b581Sfreza case IFM_1000_T:
878e388b581Sfreza mmc |= GMI_MMC_SPEED_1000;
879e388b581Sfreza break;
880e388b581Sfreza }
881e388b581Sfreza
882e388b581Sfreza gmi_write_4(TEMAC_GMI_MMC, mmc);
883e388b581Sfreza }
884e388b581Sfreza
885e388b581Sfreza static void
temac_mii_tick(void * arg)886e388b581Sfreza temac_mii_tick(void *arg)
887e388b581Sfreza {
888e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)arg;
889e388b581Sfreza int s;
890e388b581Sfreza
891534c8a68Smatt if (!device_is_active(sc->sc_dev))
892e388b581Sfreza return;
893e388b581Sfreza
894e388b581Sfreza s = splnet();
895e388b581Sfreza mii_tick(&sc->sc_mii);
896e388b581Sfreza splx(s);
897e388b581Sfreza
898e388b581Sfreza callout_schedule(&sc->sc_mii_tick, hz);
899e388b581Sfreza }
900e388b581Sfreza
901e388b581Sfreza /*
902e388b581Sfreza * External hooks.
903e388b581Sfreza */
904e388b581Sfreza static void
temac_shutdown(void * arg)905e388b581Sfreza temac_shutdown(void *arg)
906e388b581Sfreza {
907e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)arg;
908e388b581Sfreza
909e388b581Sfreza temac_reset(sc);
910e388b581Sfreza }
911e388b581Sfreza
912e388b581Sfreza static void
temac_tx_intr(void * arg)913e388b581Sfreza temac_tx_intr(void *arg)
914e388b581Sfreza {
915e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)arg;
916e388b581Sfreza uint32_t stat;
917e388b581Sfreza
918e388b581Sfreza /* XXX: We may need to splnet() here if cdmac(4) changes. */
919e388b581Sfreza
920e388b581Sfreza if ((stat = cdmac_tx_stat(sc)) & CDMAC_STAT_ERROR) {
921534c8a68Smatt aprint_error_dev(sc->sc_dev,
922534c8a68Smatt "transmit DMA is toast (%#08x), halted!\n",
923534c8a68Smatt stat);
924e388b581Sfreza
925e388b581Sfreza /* XXXFreza: how to signal this upstream? */
926e388b581Sfreza temac_stop(&sc->sc_if, 1);
927e388b581Sfreza sc->sc_dead = 1;
928e388b581Sfreza }
929e388b581Sfreza
930e388b581Sfreza #if TEMAC_DEBUG > 0
931534c8a68Smatt aprint_debug_dev(sc->sc_dev, "tx intr 0x%08x\n", stat);
932e388b581Sfreza #endif
933e388b581Sfreza temac_txreap(sc);
934e388b581Sfreza }
935e388b581Sfreza
936e388b581Sfreza static void
temac_rx_intr(void * arg)937e388b581Sfreza temac_rx_intr(void *arg)
938e388b581Sfreza {
939e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)arg;
940e388b581Sfreza uint32_t stat;
941e388b581Sfreza
942e388b581Sfreza /* XXX: We may need to splnet() here if cdmac(4) changes. */
943e388b581Sfreza
944e388b581Sfreza if ((stat = cdmac_rx_stat(sc)) & CDMAC_STAT_ERROR) {
945534c8a68Smatt aprint_error_dev(sc->sc_dev,
946534c8a68Smatt "receive DMA is toast (%#08x), halted!\n",
947534c8a68Smatt stat);
948e388b581Sfreza
949e388b581Sfreza /* XXXFreza: how to signal this upstream? */
950e388b581Sfreza temac_stop(&sc->sc_if, 1);
951e388b581Sfreza sc->sc_dead = 1;
952e388b581Sfreza }
953e388b581Sfreza
954e388b581Sfreza #if TEMAC_DEBUG > 0
955534c8a68Smatt aprint_debug_dev(sc->sc_dev, "rx intr 0x%08x\n", stat);
956e388b581Sfreza #endif
957e388b581Sfreza temac_rxreap(sc);
958e388b581Sfreza }
959e388b581Sfreza
960e388b581Sfreza /*
961e388b581Sfreza * Utils.
962e388b581Sfreza */
963e388b581Sfreza static inline void
temac_txcdsync(struct temac_softc * sc,int first,int cnt,int flag)964e388b581Sfreza temac_txcdsync(struct temac_softc *sc, int first, int cnt, int flag)
965e388b581Sfreza {
966e388b581Sfreza if ((first + cnt) > TEMAC_NTXDESC) {
967e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
968e388b581Sfreza TEMAC_TXDOFF(first),
969e388b581Sfreza sizeof(struct cdmac_descr) * (TEMAC_NTXDESC - first),
970e388b581Sfreza flag);
971e388b581Sfreza cnt = (first + cnt) % TEMAC_NTXDESC;
972e388b581Sfreza first = 0;
973e388b581Sfreza }
974e388b581Sfreza
975e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
976e388b581Sfreza TEMAC_TXDOFF(first),
977e388b581Sfreza sizeof(struct cdmac_descr) * cnt,
978e388b581Sfreza flag);
979e388b581Sfreza }
980e388b581Sfreza
981e388b581Sfreza static inline void
temac_rxcdsync(struct temac_softc * sc,int first,int cnt,int flag)982e388b581Sfreza temac_rxcdsync(struct temac_softc *sc, int first, int cnt, int flag)
983e388b581Sfreza {
984e388b581Sfreza if ((first + cnt) > TEMAC_NRXDESC) {
985e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
986e388b581Sfreza TEMAC_RXDOFF(first),
987e388b581Sfreza sizeof(struct cdmac_descr) * (TEMAC_NRXDESC - first),
988e388b581Sfreza flag);
989e388b581Sfreza cnt = (first + cnt) % TEMAC_NRXDESC;
990e388b581Sfreza first = 0;
991e388b581Sfreza }
992e388b581Sfreza
993e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, sc->sc_control_dmap,
994e388b581Sfreza TEMAC_RXDOFF(first),
995e388b581Sfreza sizeof(struct cdmac_descr) * cnt,
996e388b581Sfreza flag);
997e388b581Sfreza }
998e388b581Sfreza
999e388b581Sfreza static void
temac_txreap(struct temac_softc * sc)1000e388b581Sfreza temac_txreap(struct temac_softc *sc)
1001e388b581Sfreza {
1002e388b581Sfreza struct temac_txsoft *txs;
1003e388b581Sfreza bus_dmamap_t dmap;
1004e388b581Sfreza
1005e388b581Sfreza /*
1006e388b581Sfreza * Transmit interrupts happen on the last descriptor of Tx jobs.
1007e388b581Sfreza * Hence, every time we're called (and we assume txintr is our
1008e388b581Sfreza * only caller!), we reap packets upto and including the one
1009e388b581Sfreza * marked as last-in-batch.
1010e388b581Sfreza *
1011e388b581Sfreza * XXX we rely on that we make EXACTLY one batch per intr, no more
1012e388b581Sfreza */
1013e388b581Sfreza while (sc->sc_txsfree != TEMAC_TXQLEN) {
1014e388b581Sfreza txs = &sc->sc_txsoft[sc->sc_txsreap];
1015e388b581Sfreza dmap = txs->txs_dmap;
1016e388b581Sfreza
1017e388b581Sfreza sc->sc_txreap = TEMAC_TXINC(sc->sc_txreap, dmap->dm_nsegs);
1018e388b581Sfreza sc->sc_txfree += dmap->dm_nsegs;
1019e388b581Sfreza
1020e388b581Sfreza bus_dmamap_unload(sc->sc_dmat, txs->txs_dmap);
1021e388b581Sfreza m_freem(txs->txs_mbuf);
1022e388b581Sfreza txs->txs_mbuf = NULL;
1023e388b581Sfreza
102460a8b3b4Srin if_statinc(&sc->sc_if, if_opackets);
1025e388b581Sfreza
1026e388b581Sfreza sc->sc_txsreap = TEMAC_TXSNEXT(sc->sc_txsreap);
1027e388b581Sfreza sc->sc_txsfree++;
1028e388b581Sfreza
1029e388b581Sfreza if (txs->txs_last) {
1030e388b581Sfreza txs->txs_last = 0;
1031e388b581Sfreza sc->sc_txbusy = 0; /* channel stopped now */
1032e388b581Sfreza
1033e388b581Sfreza temac_txkick(sc);
1034e388b581Sfreza break;
1035e388b581Sfreza }
1036e388b581Sfreza }
1037e388b581Sfreza }
1038e388b581Sfreza
1039e388b581Sfreza static int
temac_rxalloc(struct temac_softc * sc,int which,int verbose)1040e388b581Sfreza temac_rxalloc(struct temac_softc *sc, int which, int verbose)
1041e388b581Sfreza {
1042e388b581Sfreza struct temac_rxsoft *rxs;
1043e388b581Sfreza struct mbuf *m;
1044e388b581Sfreza uint32_t stat;
1045e388b581Sfreza int error;
1046e388b581Sfreza
1047e388b581Sfreza rxs = &sc->sc_rxsoft[which];
1048e388b581Sfreza
1049e388b581Sfreza /* The mbuf itself is not our problem, just clear DMA related stuff. */
1050e388b581Sfreza if (rxs->rxs_mbuf != NULL) {
1051e388b581Sfreza bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmap);
1052e388b581Sfreza rxs->rxs_mbuf = NULL;
1053e388b581Sfreza }
1054e388b581Sfreza
1055e388b581Sfreza /*
1056e388b581Sfreza * We would like to store mbuf and dmap in application specific
1057e388b581Sfreza * fields of the descriptor, but that doesn't work for Rx. Shame
1058e388b581Sfreza * on Xilinx for this (and for the useless timer architecture).
1059e388b581Sfreza *
1060e388b581Sfreza * Hence each descriptor needs its own soft state. We may want
1061e388b581Sfreza * to merge multiple rxs's into a monster mbuf when we support
1062e388b581Sfreza * jumbo frames though. Also, we use single set of indexing
1063e388b581Sfreza * variables for both sc_rxdescs[] and sc_rxsoft[].
1064e388b581Sfreza */
1065e388b581Sfreza MGETHDR(m, M_DONTWAIT, MT_DATA);
1066e388b581Sfreza if (m == NULL) {
1067e388b581Sfreza if (verbose)
1068534c8a68Smatt aprint_debug_dev(sc->sc_dev,
1069534c8a68Smatt "out of Rx header mbufs\n");
1070e388b581Sfreza return (ENOBUFS);
1071e388b581Sfreza }
1072e388b581Sfreza MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
1073e388b581Sfreza
1074e388b581Sfreza MCLGET(m, M_DONTWAIT);
1075e388b581Sfreza if ((m->m_flags & M_EXT) == 0) {
1076e388b581Sfreza if (verbose)
1077534c8a68Smatt aprint_debug_dev(sc->sc_dev,
1078534c8a68Smatt "out of Rx cluster mbufs\n");
1079e388b581Sfreza m_freem(m);
1080e388b581Sfreza return (ENOBUFS);
1081e388b581Sfreza }
1082e388b581Sfreza
1083e388b581Sfreza rxs->rxs_mbuf = m;
1084e388b581Sfreza m->m_pkthdr.len = m->m_len = MCLBYTES;
1085e388b581Sfreza
1086e388b581Sfreza /* Make sure the payload after ethernet header is 4-aligned. */
1087e388b581Sfreza m_adj(m, 2);
1088e388b581Sfreza
1089e388b581Sfreza error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmap, m,
1090e388b581Sfreza BUS_DMA_NOWAIT);
1091e388b581Sfreza if (error) {
1092e388b581Sfreza if (verbose)
1093534c8a68Smatt aprint_debug_dev(sc->sc_dev,
1094534c8a68Smatt "could not map Rx descriptor %d, error = %d\n",
1095534c8a68Smatt which, error);
1096e388b581Sfreza
1097e388b581Sfreza rxs->rxs_mbuf = NULL;
1098e388b581Sfreza m_freem(m);
1099e388b581Sfreza
1100e388b581Sfreza return (error);
1101e388b581Sfreza }
1102e388b581Sfreza
1103534c8a68Smatt stat =
1104e388b581Sfreza (TEMAC_ISINTR(which) ? CDMAC_STAT_INTR : 0) |
1105e388b581Sfreza (TEMAC_ISLAST(which) ? CDMAC_STAT_STOP : 0);
1106e388b581Sfreza
1107e388b581Sfreza bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmap, 0,
1108e388b581Sfreza rxs->rxs_dmap->dm_mapsize, BUS_DMASYNC_PREREAD);
1109e388b581Sfreza
1110e388b581Sfreza /* Descriptor post-sync, if needed, left to the caller. */
1111e388b581Sfreza
1112e388b581Sfreza sc->sc_rxdescs[which].desc_addr = rxs->rxs_dmap->dm_segs[0].ds_addr;
1113e388b581Sfreza sc->sc_rxdescs[which].desc_size = rxs->rxs_dmap->dm_segs[0].ds_len;
1114e388b581Sfreza sc->sc_rxdescs[which].desc_stat = stat;
1115e388b581Sfreza
1116e388b581Sfreza /* Descriptor pre-sync, if needed, left to the caller. */
1117e388b581Sfreza
1118e388b581Sfreza return (0);
1119e388b581Sfreza }
1120e388b581Sfreza
1121e388b581Sfreza static void
temac_rxreap(struct temac_softc * sc)1122e388b581Sfreza temac_rxreap(struct temac_softc *sc)
1123e388b581Sfreza {
1124e388b581Sfreza struct ifnet *ifp = &sc->sc_if;
1125e388b581Sfreza uint32_t stat, rxstat, rxsize;
1126e388b581Sfreza struct mbuf *m;
1127e388b581Sfreza int nseg, head, tail;
1128e388b581Sfreza
1129e388b581Sfreza head = sc->sc_rxreap;
1130e388b581Sfreza tail = 0; /* gcc */
1131e388b581Sfreza nseg = 0;
1132e388b581Sfreza
1133e388b581Sfreza /*
1134e388b581Sfreza * Collect finished entries on the Rx list, kick DMA if we hit
1135e388b581Sfreza * the end. DMA will always stop on the last descriptor in chain,
1136e388b581Sfreza * so it will never hit a reap-in-progress descriptor.
1137e388b581Sfreza */
1138e388b581Sfreza while (1) {
1139e388b581Sfreza /* Maybe we previously failed to refresh this one? */
1140e388b581Sfreza if (sc->sc_rxsoft[sc->sc_rxreap].rxs_mbuf == NULL) {
1141e388b581Sfreza if (temac_rxalloc(sc, sc->sc_rxreap, 0) != 0)
1142e388b581Sfreza break;
1143e388b581Sfreza
1144e388b581Sfreza sc->sc_rxreap = TEMAC_RXNEXT(sc->sc_rxreap);
1145e388b581Sfreza continue;
1146e388b581Sfreza }
1147e388b581Sfreza temac_rxcdsync(sc, sc->sc_rxreap, 1,
1148e388b581Sfreza BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1149e388b581Sfreza
1150e388b581Sfreza stat = sc->sc_rxdescs[sc->sc_rxreap].desc_stat;
1151e388b581Sfreza m = NULL;
1152e388b581Sfreza
1153e388b581Sfreza if ((stat & CDMAC_STAT_DONE) == 0)
1154e388b581Sfreza break;
1155e388b581Sfreza
1156fb553d04Smsaitoh /*
1157fb553d04Smsaitoh * Count any descriptor we've collected, regardless of status.
1158fb553d04Smsaitoh */
1159e388b581Sfreza nseg ++;
1160e388b581Sfreza
1161e388b581Sfreza /* XXXFreza: This won't work for jumbo frames. */
1162e388b581Sfreza
1163e388b581Sfreza if ((stat & (CDMAC_STAT_EOP | CDMAC_STAT_SOP)) !=
1164e388b581Sfreza (CDMAC_STAT_EOP | CDMAC_STAT_SOP)) {
1165534c8a68Smatt aprint_error_dev(sc->sc_dev,
1166534c8a68Smatt "Rx packet doesn't fit in one descriptor, "
1167534c8a68Smatt "stat = %#08x\n", stat);
1168e388b581Sfreza goto badframe;
1169e388b581Sfreza }
1170e388b581Sfreza
1171e388b581Sfreza /* Dissect TEMAC footer if this is end of packet. */
1172e388b581Sfreza rxstat = sc->sc_rxdescs[sc->sc_rxreap].desc_rxstat;
1173e388b581Sfreza rxsize = sc->sc_rxdescs[sc->sc_rxreap].desc_rxsize &
1174e388b581Sfreza RXSIZE_MASK;
1175e388b581Sfreza
1176e388b581Sfreza if ((rxstat & RXSTAT_GOOD) == 0 ||
1177e388b581Sfreza (rxstat & RXSTAT_SICK) != 0) {
1178534c8a68Smatt aprint_error_dev(sc->sc_dev,
1179534c8a68Smatt "corrupt Rx packet, rxstat = %#08x\n",
1180534c8a68Smatt rxstat);
1181e388b581Sfreza goto badframe;
1182e388b581Sfreza }
1183e388b581Sfreza
1184e388b581Sfreza /* We are now bound to succeed. */
1185e388b581Sfreza bus_dmamap_sync(sc->sc_dmat,
1186e388b581Sfreza sc->sc_rxsoft[sc->sc_rxreap].rxs_dmap, 0,
1187e388b581Sfreza sc->sc_rxsoft[sc->sc_rxreap].rxs_dmap->dm_mapsize,
1188e388b581Sfreza BUS_DMASYNC_POSTREAD);
1189e388b581Sfreza
1190e388b581Sfreza m = sc->sc_rxsoft[sc->sc_rxreap].rxs_mbuf;
1191758ba73eSozaki-r m_set_rcvif(m, ifp);
1192e388b581Sfreza m->m_pkthdr.len = m->m_len = rxsize;
1193e388b581Sfreza
1194e388b581Sfreza badframe:
1195e388b581Sfreza /* Get ready for more work. */
1196e388b581Sfreza tail = sc->sc_rxreap;
1197e388b581Sfreza sc->sc_rxreap = TEMAC_RXNEXT(sc->sc_rxreap);
1198e388b581Sfreza
1199e388b581Sfreza /* On failures we reuse the descriptor and go ahead. */
1200e388b581Sfreza if (m == NULL) {
1201e388b581Sfreza sc->sc_rxdescs[tail].desc_stat =
1202e388b581Sfreza (TEMAC_ISINTR(tail) ? CDMAC_STAT_INTR : 0) |
1203e388b581Sfreza (TEMAC_ISLAST(tail) ? CDMAC_STAT_STOP : 0);
1204e388b581Sfreza
1205020429ceSskrll if_statinc(ifp, if_ierrors);
1206e388b581Sfreza continue;
1207e388b581Sfreza }
1208e388b581Sfreza
1209b8256fd8Sozaki-r if_percpuq_enqueue(ifp->if_percpuq, m);
1210e388b581Sfreza
1211e388b581Sfreza /* Refresh descriptor, bail out if we're out of buffers. */
1212e388b581Sfreza if (temac_rxalloc(sc, tail, 1) != 0) {
1213e388b581Sfreza sc->sc_rxreap = TEMAC_RXINC(sc->sc_rxreap, -1);
1214534c8a68Smatt aprint_error_dev(sc->sc_dev, "Rx give up for now\n");
1215e388b581Sfreza break;
1216e388b581Sfreza }
1217e388b581Sfreza }
1218e388b581Sfreza
1219e388b581Sfreza /* We may now have a contiguous ready-to-go chunk of descriptors. */
1220e388b581Sfreza if (nseg > 0) {
1221e388b581Sfreza #if TEMAC_RXDEBUG > 0
1222534c8a68Smatt aprint_debug_dev(sc->sc_dev,
1223534c8a68Smatt "rxreap: rxreap %03d -> %03d, nseg %03d\n",
1224534c8a68Smatt head, sc->sc_rxreap, nseg);
1225e388b581Sfreza #endif
1226e388b581Sfreza temac_rxcdsync(sc, head, nseg,
1227e388b581Sfreza BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1228e388b581Sfreza
1229e388b581Sfreza if (TEMAC_ISLAST(tail))
1230e388b581Sfreza cdmac_rx_start(sc, sc->sc_cdaddr + TEMAC_RXDOFF(0));
1231e388b581Sfreza }
1232e388b581Sfreza
1233e388b581Sfreza /* Ensure maximum Rx latency is kept under control. */
1234e388b581Sfreza callout_schedule(&sc->sc_rx_timo, hz / TEMAC_RXTIMO_HZ);
1235e388b581Sfreza }
1236e388b581Sfreza
1237e388b581Sfreza static void
temac_rxtimo(void * arg)1238e388b581Sfreza temac_rxtimo(void *arg)
1239e388b581Sfreza {
1240e388b581Sfreza struct temac_softc *sc = (struct temac_softc *)arg;
1241e388b581Sfreza int s;
1242e388b581Sfreza
1243e388b581Sfreza /* We run TEMAC_RXTIMO_HZ times/sec to ensure Rx doesn't stall. */
1244e388b581Sfreza s = splnet();
1245e388b581Sfreza temac_rxreap(sc);
1246e388b581Sfreza splx(s);
1247e388b581Sfreza }
1248e388b581Sfreza
1249e388b581Sfreza static void
temac_reset(struct temac_softc * sc)1250e388b581Sfreza temac_reset(struct temac_softc *sc)
1251e388b581Sfreza {
1252e388b581Sfreza uint32_t rcr, tcr;
1253e388b581Sfreza
1254e388b581Sfreza /* Kill CDMAC channels. */
1255e388b581Sfreza cdmac_tx_reset(sc);
1256e388b581Sfreza cdmac_rx_reset(sc);
1257e388b581Sfreza
1258e388b581Sfreza /* Disable receiver. */
1259e388b581Sfreza rcr = gmi_read_4(TEMAC_GMI_RXCF1) & ~GMI_RX_ENABLE;
1260e388b581Sfreza gmi_write_4(TEMAC_GMI_RXCF1, rcr);
1261e388b581Sfreza
1262e388b581Sfreza /* Disable transmitter. */
1263e388b581Sfreza tcr = gmi_read_4(TEMAC_GMI_TXCF) & ~GMI_TX_ENABLE;
1264e388b581Sfreza gmi_write_4(TEMAC_GMI_TXCF, tcr);
1265e388b581Sfreza }
1266e388b581Sfreza
1267e388b581Sfreza static void
temac_rxdrain(struct temac_softc * sc)1268e388b581Sfreza temac_rxdrain(struct temac_softc *sc)
1269e388b581Sfreza {
1270e388b581Sfreza struct temac_rxsoft *rxs;
1271e388b581Sfreza int i;
1272e388b581Sfreza
1273e388b581Sfreza for (i = 0; i < TEMAC_NRXDESC; i++) {
1274e388b581Sfreza rxs = &sc->sc_rxsoft[i];
1275e388b581Sfreza
1276e388b581Sfreza if (rxs->rxs_mbuf != NULL) {
1277e388b581Sfreza bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmap);
1278e388b581Sfreza m_freem(rxs->rxs_mbuf);
1279e388b581Sfreza rxs->rxs_mbuf = NULL;
1280e388b581Sfreza }
1281e388b581Sfreza }
1282e388b581Sfreza
1283e388b581Sfreza sc->sc_rx_drained = 1;
1284e388b581Sfreza }
1285e388b581Sfreza
1286e388b581Sfreza static void
temac_txkick(struct temac_softc * sc)1287e388b581Sfreza temac_txkick(struct temac_softc *sc)
1288e388b581Sfreza {
1289e388b581Sfreza if (sc->sc_txsoft[sc->sc_txsreap].txs_mbuf != NULL &&
1290e388b581Sfreza sc->sc_txbusy == 0) {
1291e388b581Sfreza cdmac_tx_start(sc, sc->sc_cdaddr + TEMAC_TXDOFF(sc->sc_txreap));
1292e388b581Sfreza sc->sc_txbusy = 1;
1293e388b581Sfreza }
1294e388b581Sfreza }
1295