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