dwc_gmac.c revision 1.93 1 1.93 skrll /* $NetBSD: dwc_gmac.c,v 1.93 2024/08/10 12:16:47 skrll Exp $ */
2 1.18 jmcneill
3 1.1 martin /*-
4 1.1 martin * Copyright (c) 2013, 2014 The NetBSD Foundation, Inc.
5 1.1 martin * All rights reserved.
6 1.1 martin *
7 1.1 martin * This code is derived from software contributed to The NetBSD Foundation
8 1.1 martin * by Matt Thomas of 3am Software Foundry and Martin Husemann.
9 1.1 martin *
10 1.1 martin * Redistribution and use in source and binary forms, with or without
11 1.1 martin * modification, are permitted provided that the following conditions
12 1.1 martin * are met:
13 1.1 martin * 1. Redistributions of source code must retain the above copyright
14 1.1 martin * notice, this list of conditions and the following disclaimer.
15 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 martin * notice, this list of conditions and the following disclaimer in the
17 1.1 martin * documentation and/or other materials provided with the distribution.
18 1.1 martin *
19 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.1 martin */
31 1.1 martin
32 1.1 martin /*
33 1.1 martin * This driver supports the Synopsis Designware GMAC core, as found
34 1.1 martin * on Allwinner A20 cores and others.
35 1.1 martin *
36 1.1 martin * Real documentation seems to not be available, the marketing product
37 1.1 martin * documents could be found here:
38 1.1 martin *
39 1.1 martin * http://www.synopsys.com/dw/ipdir.php?ds=dwc_ether_mac10_100_1000_unive
40 1.1 martin */
41 1.1 martin
42 1.1 martin #include <sys/cdefs.h>
43 1.1 martin
44 1.93 skrll __KERNEL_RCSID(1, "$NetBSD: dwc_gmac.c,v 1.93 2024/08/10 12:16:47 skrll Exp $");
45 1.7 martin
46 1.7 martin /* #define DWC_GMAC_DEBUG 1 */
47 1.1 martin
48 1.38 skrll #ifdef _KERNEL_OPT
49 1.1 martin #include "opt_inet.h"
50 1.38 skrll #endif
51 1.1 martin
52 1.1 martin #include <sys/param.h>
53 1.1 martin #include <sys/bus.h>
54 1.1 martin #include <sys/device.h>
55 1.1 martin #include <sys/intr.h>
56 1.1 martin #include <sys/systm.h>
57 1.1 martin #include <sys/sockio.h>
58 1.29 jmcneill #include <sys/cprng.h>
59 1.63 msaitoh #include <sys/rndsource.h>
60 1.1 martin
61 1.1 martin #include <net/if.h>
62 1.1 martin #include <net/if_ether.h>
63 1.1 martin #include <net/if_media.h>
64 1.1 martin #include <net/bpf.h>
65 1.1 martin #ifdef INET
66 1.1 martin #include <netinet/if_inarp.h>
67 1.1 martin #endif
68 1.1 martin
69 1.1 martin #include <dev/mii/miivar.h>
70 1.1 martin
71 1.1 martin #include <dev/ic/dwc_gmac_reg.h>
72 1.1 martin #include <dev/ic/dwc_gmac_var.h>
73 1.1 martin
74 1.56 msaitoh static int dwc_gmac_miibus_read_reg(device_t, int, int, uint16_t *);
75 1.56 msaitoh static int dwc_gmac_miibus_write_reg(device_t, int, int, uint16_t);
76 1.1 martin static void dwc_gmac_miibus_statchg(struct ifnet *);
77 1.1 martin
78 1.61 msaitoh static int dwc_gmac_reset(struct dwc_gmac_softc *);
79 1.79 mrg static void dwc_gmac_write_hwaddr(struct dwc_gmac_softc *, uint8_t[ETHER_ADDR_LEN]);
80 1.61 msaitoh static int dwc_gmac_alloc_dma_rings(struct dwc_gmac_softc *);
81 1.61 msaitoh static void dwc_gmac_free_dma_rings(struct dwc_gmac_softc *);
82 1.61 msaitoh static int dwc_gmac_alloc_rx_ring(struct dwc_gmac_softc *, struct dwc_gmac_rx_ring *);
83 1.61 msaitoh static void dwc_gmac_reset_rx_ring(struct dwc_gmac_softc *, struct dwc_gmac_rx_ring *);
84 1.61 msaitoh static void dwc_gmac_free_rx_ring(struct dwc_gmac_softc *, struct dwc_gmac_rx_ring *);
85 1.61 msaitoh static int dwc_gmac_alloc_tx_ring(struct dwc_gmac_softc *, struct dwc_gmac_tx_ring *);
86 1.61 msaitoh static void dwc_gmac_reset_tx_ring(struct dwc_gmac_softc *, struct dwc_gmac_tx_ring *);
87 1.61 msaitoh static void dwc_gmac_free_tx_ring(struct dwc_gmac_softc *, struct dwc_gmac_tx_ring *);
88 1.61 msaitoh static void dwc_gmac_txdesc_sync(struct dwc_gmac_softc *, int, int, int);
89 1.61 msaitoh static int dwc_gmac_init(struct ifnet *);
90 1.61 msaitoh static int dwc_gmac_init_locked(struct ifnet *);
91 1.61 msaitoh static void dwc_gmac_stop(struct ifnet *, int);
92 1.61 msaitoh static void dwc_gmac_stop_locked(struct ifnet *, int);
93 1.61 msaitoh static void dwc_gmac_start(struct ifnet *);
94 1.61 msaitoh static void dwc_gmac_start_locked(struct ifnet *);
95 1.61 msaitoh static int dwc_gmac_queue(struct dwc_gmac_softc *, struct mbuf *);
96 1.1 martin static int dwc_gmac_ioctl(struct ifnet *, u_long, void *);
97 1.61 msaitoh static void dwc_gmac_tx_intr(struct dwc_gmac_softc *);
98 1.61 msaitoh static void dwc_gmac_rx_intr(struct dwc_gmac_softc *);
99 1.61 msaitoh static void dwc_gmac_setmulti(struct dwc_gmac_softc *);
100 1.22 martin static int dwc_gmac_ifflags_cb(struct ethercom *);
101 1.55 martin static void dwc_gmac_desc_set_owned_by_dev(struct dwc_gmac_dev_dmadesc *);
102 1.55 martin static int dwc_gmac_desc_is_owned_by_dev(struct dwc_gmac_dev_dmadesc *);
103 1.55 martin static void dwc_gmac_desc_std_set_len(struct dwc_gmac_dev_dmadesc *, int);
104 1.55 martin static uint32_t dwc_gmac_desc_std_get_len(struct dwc_gmac_dev_dmadesc *);
105 1.55 martin static void dwc_gmac_desc_std_tx_init_flags(struct dwc_gmac_dev_dmadesc *);
106 1.55 martin static void dwc_gmac_desc_std_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *);
107 1.55 martin static void dwc_gmac_desc_std_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *);
108 1.55 martin static void dwc_gmac_desc_std_rx_init_flags(struct dwc_gmac_dev_dmadesc *);
109 1.55 martin static int dwc_gmac_desc_std_rx_has_error(struct dwc_gmac_dev_dmadesc *);
110 1.55 martin static void dwc_gmac_desc_enh_set_len(struct dwc_gmac_dev_dmadesc *, int);
111 1.55 martin static uint32_t dwc_gmac_desc_enh_get_len(struct dwc_gmac_dev_dmadesc *);
112 1.55 martin static void dwc_gmac_desc_enh_tx_init_flags(struct dwc_gmac_dev_dmadesc *);
113 1.55 martin static void dwc_gmac_desc_enh_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *);
114 1.55 martin static void dwc_gmac_desc_enh_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *);
115 1.55 martin static void dwc_gmac_desc_enh_rx_init_flags(struct dwc_gmac_dev_dmadesc *);
116 1.55 martin static int dwc_gmac_desc_enh_rx_has_error(struct dwc_gmac_dev_dmadesc *);
117 1.55 martin
118 1.55 martin static const struct dwc_gmac_desc_methods desc_methods_standard = {
119 1.55 martin .tx_init_flags = dwc_gmac_desc_std_tx_init_flags,
120 1.55 martin .tx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
121 1.55 martin .tx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
122 1.55 martin .tx_set_len = dwc_gmac_desc_std_set_len,
123 1.55 martin .tx_set_first_frag = dwc_gmac_desc_std_tx_set_first_frag,
124 1.55 martin .tx_set_last_frag = dwc_gmac_desc_std_tx_set_last_frag,
125 1.55 martin .rx_init_flags = dwc_gmac_desc_std_rx_init_flags,
126 1.55 martin .rx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
127 1.55 martin .rx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
128 1.55 martin .rx_set_len = dwc_gmac_desc_std_set_len,
129 1.55 martin .rx_get_len = dwc_gmac_desc_std_get_len,
130 1.55 martin .rx_has_error = dwc_gmac_desc_std_rx_has_error
131 1.55 martin };
132 1.55 martin
133 1.55 martin static const struct dwc_gmac_desc_methods desc_methods_enhanced = {
134 1.55 martin .tx_init_flags = dwc_gmac_desc_enh_tx_init_flags,
135 1.55 martin .tx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
136 1.55 martin .tx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
137 1.55 martin .tx_set_len = dwc_gmac_desc_enh_set_len,
138 1.55 martin .tx_set_first_frag = dwc_gmac_desc_enh_tx_set_first_frag,
139 1.55 martin .tx_set_last_frag = dwc_gmac_desc_enh_tx_set_last_frag,
140 1.55 martin .rx_init_flags = dwc_gmac_desc_enh_rx_init_flags,
141 1.55 martin .rx_set_owned_by_dev = dwc_gmac_desc_set_owned_by_dev,
142 1.55 martin .rx_is_owned_by_dev = dwc_gmac_desc_is_owned_by_dev,
143 1.55 martin .rx_set_len = dwc_gmac_desc_enh_set_len,
144 1.55 martin .rx_get_len = dwc_gmac_desc_enh_get_len,
145 1.55 martin .rx_has_error = dwc_gmac_desc_enh_rx_has_error
146 1.55 martin };
147 1.55 martin
148 1.1 martin
149 1.82 skrll #define TX_DESC_OFFSET(N) ((AWGE_RX_RING_COUNT + (N)) \
150 1.82 skrll * sizeof(struct dwc_gmac_dev_dmadesc))
151 1.82 skrll #define TX_NEXT(N) (((N) + 1) & (AWGE_TX_RING_COUNT - 1))
152 1.1 martin
153 1.82 skrll #define RX_DESC_OFFSET(N) ((N) * sizeof(struct dwc_gmac_dev_dmadesc))
154 1.82 skrll #define RX_NEXT(N) (((N) + 1) & (AWGE_RX_RING_COUNT - 1))
155 1.8 martin
156 1.8 martin
157 1.8 martin
158 1.61 msaitoh #define GMAC_DEF_DMA_INT_MASK (GMAC_DMA_INT_TIE | GMAC_DMA_INT_RIE | \
159 1.61 msaitoh GMAC_DMA_INT_NIE | GMAC_DMA_INT_AIE | \
160 1.61 msaitoh GMAC_DMA_INT_FBE | GMAC_DMA_INT_UNE)
161 1.61 msaitoh
162 1.61 msaitoh #define GMAC_DMA_INT_ERRORS (GMAC_DMA_INT_AIE | GMAC_DMA_INT_ERE | \
163 1.61 msaitoh GMAC_DMA_INT_FBE | \
164 1.61 msaitoh GMAC_DMA_INT_RWE | GMAC_DMA_INT_RUE | \
165 1.61 msaitoh GMAC_DMA_INT_UNE | GMAC_DMA_INT_OVE | \
166 1.10 martin GMAC_DMA_INT_TJE)
167 1.8 martin
168 1.8 martin #define AWIN_DEF_MAC_INTRMASK \
169 1.8 martin (AWIN_GMAC_MAC_INT_TSI | AWIN_GMAC_MAC_INT_ANEG | \
170 1.55 martin AWIN_GMAC_MAC_INT_LINKCHG)
171 1.1 martin
172 1.7 martin #ifdef DWC_GMAC_DEBUG
173 1.61 msaitoh static void dwc_gmac_dump_dma(struct dwc_gmac_softc *);
174 1.61 msaitoh static void dwc_gmac_dump_tx_desc(struct dwc_gmac_softc *);
175 1.61 msaitoh static void dwc_gmac_dump_rx_desc(struct dwc_gmac_softc *);
176 1.61 msaitoh static void dwc_dump_and_abort(struct dwc_gmac_softc *, const char *);
177 1.61 msaitoh static void dwc_dump_status(struct dwc_gmac_softc *);
178 1.61 msaitoh static void dwc_gmac_dump_ffilt(struct dwc_gmac_softc *, uint32_t);
179 1.7 martin #endif
180 1.7 martin
181 1.51 jmcneill int
182 1.57 martin dwc_gmac_attach(struct dwc_gmac_softc *sc, int phy_id, uint32_t mii_clk)
183 1.1 martin {
184 1.1 martin uint8_t enaddr[ETHER_ADDR_LEN];
185 1.91 skrll uint32_t maclo, machi, hwft;
186 1.1 martin struct mii_data * const mii = &sc->sc_mii;
187 1.1 martin struct ifnet * const ifp = &sc->sc_ec.ec_if;
188 1.5 martin prop_dictionary_t dict;
189 1.1 martin
190 1.1 martin mutex_init(&sc->sc_mdio_lock, MUTEX_DEFAULT, IPL_NET);
191 1.3 martin sc->sc_mii_clk = mii_clk & 7;
192 1.1 martin
193 1.5 martin dict = device_properties(sc->sc_dev);
194 1.5 martin prop_data_t ea = dict ? prop_dictionary_get(dict, "mac-address") : NULL;
195 1.5 martin if (ea != NULL) {
196 1.5 martin /*
197 1.75 andvar * If the MAC address is overridden by a device property,
198 1.5 martin * use that.
199 1.5 martin */
200 1.5 martin KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
201 1.5 martin KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
202 1.71 jmcneill memcpy(enaddr, prop_data_value(ea), ETHER_ADDR_LEN);
203 1.5 martin } else {
204 1.5 martin /*
205 1.5 martin * If we did not get an externaly configure address,
206 1.5 martin * try to read one from the current filter setup,
207 1.5 martin * before resetting the chip.
208 1.5 martin */
209 1.8 martin maclo = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
210 1.8 martin AWIN_GMAC_MAC_ADDR0LO);
211 1.8 martin machi = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
212 1.8 martin AWIN_GMAC_MAC_ADDR0HI);
213 1.14 jmcneill
214 1.14 jmcneill if (maclo == 0xffffffff && (machi & 0xffff) == 0xffff) {
215 1.29 jmcneill /* fake MAC address */
216 1.29 jmcneill maclo = 0x00f2 | (cprng_strong32() << 16);
217 1.29 jmcneill machi = cprng_strong32();
218 1.14 jmcneill }
219 1.14 jmcneill
220 1.1 martin enaddr[0] = maclo & 0x0ff;
221 1.1 martin enaddr[1] = (maclo >> 8) & 0x0ff;
222 1.1 martin enaddr[2] = (maclo >> 16) & 0x0ff;
223 1.1 martin enaddr[3] = (maclo >> 24) & 0x0ff;
224 1.1 martin enaddr[4] = machi & 0x0ff;
225 1.1 martin enaddr[5] = (machi >> 8) & 0x0ff;
226 1.1 martin }
227 1.1 martin
228 1.91 skrll const uint32_t ver =
229 1.91 skrll bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_VERSION);
230 1.91 skrll const uint32_t snpsver =
231 1.91 skrll __SHIFTOUT(ver, AWIN_GMAC_MAC_VERSION_SNPSVER_MASK);
232 1.91 skrll aprint_normal_dev(sc->sc_dev, "Core version: %08x\n", snpsver);
233 1.55 martin
234 1.1 martin /*
235 1.21 joerg * Init chip and do initial setup
236 1.1 martin */
237 1.1 martin if (dwc_gmac_reset(sc) != 0)
238 1.51 jmcneill return ENXIO; /* not much to cleanup, haven't attached yet */
239 1.5 martin dwc_gmac_write_hwaddr(sc, enaddr);
240 1.52 sevan aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
241 1.1 martin ether_sprintf(enaddr));
242 1.1 martin
243 1.55 martin hwft = 0;
244 1.91 skrll if (snpsver >= 0x35) {
245 1.55 martin hwft = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
246 1.55 martin AWIN_GMAC_DMA_HWFEATURES);
247 1.55 martin aprint_normal_dev(sc->sc_dev,
248 1.55 martin "HW feature mask: %x\n", hwft);
249 1.55 martin }
250 1.83 skrll
251 1.83 skrll if (sizeof(bus_addr_t) > 4) {
252 1.83 skrll int error = bus_dmatag_subregion(sc->sc_dmat, 0, __MASK(32),
253 1.83 skrll &sc->sc_dmat, BUS_DMA_WAITOK);
254 1.83 skrll if (error != 0) {
255 1.83 skrll aprint_error_dev(sc->sc_dev,
256 1.83 skrll "failed to create DMA subregion\n");
257 1.83 skrll return ENOMEM;
258 1.83 skrll }
259 1.83 skrll }
260 1.83 skrll
261 1.55 martin if (hwft & GMAC_DMA_FEAT_ENHANCED_DESC) {
262 1.55 martin aprint_normal_dev(sc->sc_dev,
263 1.55 martin "Using enhanced descriptor format\n");
264 1.55 martin sc->sc_descm = &desc_methods_enhanced;
265 1.55 martin } else {
266 1.55 martin sc->sc_descm = &desc_methods_standard;
267 1.55 martin }
268 1.70 chs if (hwft & GMAC_DMA_FEAT_RMON) {
269 1.70 chs uint32_t val;
270 1.70 chs
271 1.70 chs /* Mask all MMC interrupts */
272 1.70 chs val = 0xffffffff;
273 1.70 chs bus_space_write_4(sc->sc_bst, sc->sc_bsh,
274 1.70 chs GMAC_MMC_RX_INT_MSK, val);
275 1.70 chs bus_space_write_4(sc->sc_bst, sc->sc_bsh,
276 1.70 chs GMAC_MMC_TX_INT_MSK, val);
277 1.70 chs }
278 1.55 martin
279 1.1 martin /*
280 1.1 martin * Allocate Tx and Rx rings
281 1.1 martin */
282 1.1 martin if (dwc_gmac_alloc_dma_rings(sc) != 0) {
283 1.1 martin aprint_error_dev(sc->sc_dev, "could not allocate DMA rings\n");
284 1.1 martin goto fail;
285 1.1 martin }
286 1.38 skrll
287 1.1 martin if (dwc_gmac_alloc_tx_ring(sc, &sc->sc_txq) != 0) {
288 1.1 martin aprint_error_dev(sc->sc_dev, "could not allocate Tx ring\n");
289 1.1 martin goto fail;
290 1.1 martin }
291 1.1 martin
292 1.1 martin if (dwc_gmac_alloc_rx_ring(sc, &sc->sc_rxq) != 0) {
293 1.1 martin aprint_error_dev(sc->sc_dev, "could not allocate Rx ring\n");
294 1.1 martin goto fail;
295 1.1 martin }
296 1.1 martin
297 1.93 skrll sc->sc_stopping = false;
298 1.93 skrll sc->sc_txbusy = false;
299 1.93 skrll
300 1.93 skrll sc->sc_core_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
301 1.93 skrll sc->sc_intr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
302 1.38 skrll mutex_init(&sc->sc_txq.t_mtx, MUTEX_DEFAULT, IPL_NET);
303 1.38 skrll mutex_init(&sc->sc_rxq.r_mtx, MUTEX_DEFAULT, IPL_NET);
304 1.38 skrll
305 1.1 martin /*
306 1.1 martin * Prepare interface data
307 1.1 martin */
308 1.1 martin ifp->if_softc = sc;
309 1.1 martin strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
310 1.1 martin ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
311 1.43 ozaki ifp->if_extflags = IFEF_MPSAFE;
312 1.1 martin ifp->if_ioctl = dwc_gmac_ioctl;
313 1.1 martin ifp->if_start = dwc_gmac_start;
314 1.1 martin ifp->if_init = dwc_gmac_init;
315 1.1 martin ifp->if_stop = dwc_gmac_stop;
316 1.1 martin IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
317 1.1 martin IFQ_SET_READY(&ifp->if_snd);
318 1.1 martin
319 1.1 martin /*
320 1.1 martin * Attach MII subdevices
321 1.1 martin */
322 1.2 martin sc->sc_ec.ec_mii = &sc->sc_mii;
323 1.1 martin ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
324 1.62 msaitoh mii->mii_ifp = ifp;
325 1.62 msaitoh mii->mii_readreg = dwc_gmac_miibus_read_reg;
326 1.62 msaitoh mii->mii_writereg = dwc_gmac_miibus_write_reg;
327 1.62 msaitoh mii->mii_statchg = dwc_gmac_miibus_statchg;
328 1.62 msaitoh mii_attach(sc->sc_dev, mii, 0xffffffff, phy_id, MII_OFFSET_ANY,
329 1.25 jmcneill MIIF_DOPAUSE);
330 1.1 martin
331 1.62 msaitoh if (LIST_EMPTY(&mii->mii_phys)) {
332 1.62 msaitoh aprint_error_dev(sc->sc_dev, "no PHY found!\n");
333 1.62 msaitoh ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
334 1.62 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
335 1.62 msaitoh } else {
336 1.62 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
337 1.62 msaitoh }
338 1.1 martin
339 1.1 martin /*
340 1.33 tnn * We can support 802.1Q VLAN-sized frames.
341 1.33 tnn */
342 1.33 tnn sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
343 1.33 tnn
344 1.33 tnn /*
345 1.1 martin * Ready, attach interface
346 1.1 martin */
347 1.38 skrll /* Attach the interface. */
348 1.74 riastrad if_initialize(ifp);
349 1.38 skrll sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
350 1.40 ozaki if_deferred_start_init(ifp, NULL);
351 1.1 martin ether_ifattach(ifp, enaddr);
352 1.22 martin ether_set_ifflags_cb(&sc->sc_ec, dwc_gmac_ifflags_cb);
353 1.38 skrll if_register(ifp);
354 1.63 msaitoh rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
355 1.63 msaitoh RND_TYPE_NET, RND_FLAG_DEFAULT);
356 1.1 martin
357 1.1 martin /*
358 1.1 martin * Enable interrupts
359 1.1 martin */
360 1.93 skrll mutex_enter(sc->sc_intr_lock);
361 1.25 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_INTMASK,
362 1.8 martin AWIN_DEF_MAC_INTRMASK);
363 1.8 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_INTENABLE,
364 1.8 martin GMAC_DEF_DMA_INT_MASK);
365 1.93 skrll mutex_exit(sc->sc_intr_lock);
366 1.1 martin
367 1.51 jmcneill return 0;
368 1.51 jmcneill
369 1.1 martin fail:
370 1.1 martin dwc_gmac_free_rx_ring(sc, &sc->sc_rxq);
371 1.1 martin dwc_gmac_free_tx_ring(sc, &sc->sc_txq);
372 1.41 msaitoh dwc_gmac_free_dma_rings(sc);
373 1.41 msaitoh mutex_destroy(&sc->sc_mdio_lock);
374 1.51 jmcneill
375 1.51 jmcneill return ENXIO;
376 1.1 martin }
377 1.1 martin
378 1.1 martin
379 1.1 martin
380 1.1 martin static int
381 1.1 martin dwc_gmac_reset(struct dwc_gmac_softc *sc)
382 1.1 martin {
383 1.1 martin size_t cnt;
384 1.1 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE,
385 1.61 msaitoh bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE)
386 1.61 msaitoh | GMAC_BUSMODE_RESET);
387 1.72 ryo for (cnt = 0; cnt < 30000; cnt++) {
388 1.1 martin if ((bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE)
389 1.1 martin & GMAC_BUSMODE_RESET) == 0)
390 1.1 martin return 0;
391 1.1 martin delay(10);
392 1.1 martin }
393 1.1 martin
394 1.1 martin aprint_error_dev(sc->sc_dev, "reset timed out\n");
395 1.1 martin return EIO;
396 1.1 martin }
397 1.1 martin
398 1.1 martin static void
399 1.1 martin dwc_gmac_write_hwaddr(struct dwc_gmac_softc *sc,
400 1.1 martin uint8_t enaddr[ETHER_ADDR_LEN])
401 1.1 martin {
402 1.49 jmcneill uint32_t hi, lo;
403 1.1 martin
404 1.49 jmcneill hi = enaddr[4] | (enaddr[5] << 8);
405 1.1 martin lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16)
406 1.73 msaitoh | ((uint32_t)enaddr[3] << 24);
407 1.49 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_ADDR0HI, hi);
408 1.1 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_ADDR0LO, lo);
409 1.1 martin }
410 1.1 martin
411 1.1 martin static int
412 1.56 msaitoh dwc_gmac_miibus_read_reg(device_t self, int phy, int reg, uint16_t *val)
413 1.1 martin {
414 1.1 martin struct dwc_gmac_softc * const sc = device_private(self);
415 1.6 martin uint16_t mii;
416 1.1 martin size_t cnt;
417 1.1 martin
418 1.61 msaitoh mii = __SHIFTIN(phy, GMAC_MII_PHY_MASK)
419 1.61 msaitoh | __SHIFTIN(reg, GMAC_MII_REG_MASK)
420 1.61 msaitoh | __SHIFTIN(sc->sc_mii_clk, GMAC_MII_CLKMASK)
421 1.6 martin | GMAC_MII_BUSY;
422 1.1 martin
423 1.1 martin mutex_enter(&sc->sc_mdio_lock);
424 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIADDR, mii);
425 1.1 martin
426 1.1 martin for (cnt = 0; cnt < 1000; cnt++) {
427 1.3 martin if (!(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
428 1.3 martin AWIN_GMAC_MAC_MIIADDR) & GMAC_MII_BUSY)) {
429 1.56 msaitoh *val = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
430 1.3 martin AWIN_GMAC_MAC_MIIDATA);
431 1.1 martin break;
432 1.1 martin }
433 1.1 martin delay(10);
434 1.1 martin }
435 1.1 martin
436 1.1 martin mutex_exit(&sc->sc_mdio_lock);
437 1.1 martin
438 1.56 msaitoh if (cnt >= 1000)
439 1.56 msaitoh return ETIMEDOUT;
440 1.61 msaitoh
441 1.56 msaitoh return 0;
442 1.1 martin }
443 1.1 martin
444 1.56 msaitoh static int
445 1.56 msaitoh dwc_gmac_miibus_write_reg(device_t self, int phy, int reg, uint16_t val)
446 1.1 martin {
447 1.1 martin struct dwc_gmac_softc * const sc = device_private(self);
448 1.6 martin uint16_t mii;
449 1.1 martin size_t cnt;
450 1.1 martin
451 1.61 msaitoh mii = __SHIFTIN(phy, GMAC_MII_PHY_MASK)
452 1.61 msaitoh | __SHIFTIN(reg, GMAC_MII_REG_MASK)
453 1.61 msaitoh | __SHIFTIN(sc->sc_mii_clk, GMAC_MII_CLKMASK)
454 1.6 martin | GMAC_MII_BUSY | GMAC_MII_WRITE;
455 1.1 martin
456 1.1 martin mutex_enter(&sc->sc_mdio_lock);
457 1.1 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIDATA, val);
458 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_MIIADDR, mii);
459 1.1 martin
460 1.1 martin for (cnt = 0; cnt < 1000; cnt++) {
461 1.3 martin if (!(bus_space_read_4(sc->sc_bst, sc->sc_bsh,
462 1.3 martin AWIN_GMAC_MAC_MIIADDR) & GMAC_MII_BUSY))
463 1.1 martin break;
464 1.1 martin delay(10);
465 1.1 martin }
466 1.38 skrll
467 1.1 martin mutex_exit(&sc->sc_mdio_lock);
468 1.56 msaitoh
469 1.56 msaitoh if (cnt >= 1000)
470 1.56 msaitoh return ETIMEDOUT;
471 1.56 msaitoh
472 1.56 msaitoh return 0;
473 1.1 martin }
474 1.1 martin
475 1.1 martin static int
476 1.1 martin dwc_gmac_alloc_rx_ring(struct dwc_gmac_softc *sc,
477 1.1 martin struct dwc_gmac_rx_ring *ring)
478 1.1 martin {
479 1.1 martin struct dwc_gmac_rx_data *data;
480 1.1 martin bus_addr_t physaddr;
481 1.89 skrll const size_t rxringsz = AWGE_RX_RING_COUNT * sizeof(*ring->r_desc);
482 1.1 martin int error, i, next;
483 1.1 martin
484 1.1 martin ring->r_cur = ring->r_next = 0;
485 1.89 skrll memset(ring->r_desc, 0, rxringsz);
486 1.1 martin
487 1.1 martin /*
488 1.1 martin * Pre-allocate Rx buffers and populate Rx ring.
489 1.1 martin */
490 1.1 martin for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
491 1.1 martin struct dwc_gmac_dev_dmadesc *desc;
492 1.1 martin
493 1.1 martin data = &sc->sc_rxq.r_data[i];
494 1.1 martin
495 1.1 martin MGETHDR(data->rd_m, M_DONTWAIT, MT_DATA);
496 1.1 martin if (data->rd_m == NULL) {
497 1.1 martin aprint_error_dev(sc->sc_dev,
498 1.1 martin "could not allocate rx mbuf #%d\n", i);
499 1.1 martin error = ENOMEM;
500 1.1 martin goto fail;
501 1.1 martin }
502 1.1 martin error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
503 1.1 martin MCLBYTES, 0, BUS_DMA_NOWAIT, &data->rd_map);
504 1.1 martin if (error != 0) {
505 1.1 martin aprint_error_dev(sc->sc_dev,
506 1.1 martin "could not create DMA map\n");
507 1.1 martin data->rd_map = NULL;
508 1.1 martin goto fail;
509 1.1 martin }
510 1.1 martin MCLGET(data->rd_m, M_DONTWAIT);
511 1.1 martin if (!(data->rd_m->m_flags & M_EXT)) {
512 1.1 martin aprint_error_dev(sc->sc_dev,
513 1.1 martin "could not allocate mbuf cluster #%d\n", i);
514 1.1 martin error = ENOMEM;
515 1.1 martin goto fail;
516 1.1 martin }
517 1.66 tnn data->rd_m->m_len = data->rd_m->m_pkthdr.len
518 1.66 tnn = data->rd_m->m_ext.ext_size;
519 1.66 tnn if (data->rd_m->m_len > AWGE_MAX_PACKET) {
520 1.66 tnn data->rd_m->m_len = data->rd_m->m_pkthdr.len
521 1.66 tnn = AWGE_MAX_PACKET;
522 1.66 tnn }
523 1.1 martin
524 1.66 tnn error = bus_dmamap_load_mbuf(sc->sc_dmat, data->rd_map,
525 1.66 tnn data->rd_m, BUS_DMA_READ | BUS_DMA_NOWAIT);
526 1.1 martin if (error != 0) {
527 1.1 martin aprint_error_dev(sc->sc_dev,
528 1.1 martin "could not load rx buf DMA map #%d", i);
529 1.1 martin goto fail;
530 1.1 martin }
531 1.66 tnn bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
532 1.66 tnn data->rd_map->dm_mapsize, BUS_DMASYNC_PREREAD);
533 1.1 martin physaddr = data->rd_map->dm_segs[0].ds_addr;
534 1.1 martin
535 1.1 martin desc = &sc->sc_rxq.r_desc[i];
536 1.1 martin desc->ddesc_data = htole32(physaddr);
537 1.8 martin next = RX_NEXT(i);
538 1.38 skrll desc->ddesc_next = htole32(ring->r_physaddr
539 1.1 martin + next * sizeof(*desc));
540 1.55 martin sc->sc_descm->rx_init_flags(desc);
541 1.66 tnn sc->sc_descm->rx_set_len(desc, data->rd_m->m_len);
542 1.55 martin sc->sc_descm->rx_set_owned_by_dev(desc);
543 1.1 martin }
544 1.1 martin
545 1.89 skrll bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
546 1.89 skrll RX_DESC_OFFSET(0),
547 1.82 skrll AWGE_RX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc),
548 1.85 skrll BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
549 1.1 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
550 1.6 martin ring->r_physaddr);
551 1.1 martin
552 1.1 martin return 0;
553 1.1 martin
554 1.1 martin fail:
555 1.1 martin dwc_gmac_free_rx_ring(sc, ring);
556 1.1 martin return error;
557 1.1 martin }
558 1.1 martin
559 1.1 martin static void
560 1.1 martin dwc_gmac_reset_rx_ring(struct dwc_gmac_softc *sc,
561 1.1 martin struct dwc_gmac_rx_ring *ring)
562 1.1 martin {
563 1.1 martin struct dwc_gmac_dev_dmadesc *desc;
564 1.66 tnn struct dwc_gmac_rx_data *data;
565 1.1 martin int i;
566 1.1 martin
567 1.38 skrll mutex_enter(&ring->r_mtx);
568 1.1 martin for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
569 1.1 martin desc = &sc->sc_rxq.r_desc[i];
570 1.66 tnn data = &sc->sc_rxq.r_data[i];
571 1.55 martin sc->sc_descm->rx_init_flags(desc);
572 1.66 tnn sc->sc_descm->rx_set_len(desc, data->rd_m->m_len);
573 1.55 martin sc->sc_descm->rx_set_owned_by_dev(desc);
574 1.1 martin }
575 1.1 martin
576 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map, 0,
577 1.82 skrll AWGE_RX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc),
578 1.61 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
579 1.1 martin
580 1.1 martin ring->r_cur = ring->r_next = 0;
581 1.11 martin /* reset DMA address to start of ring */
582 1.11 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
583 1.11 martin sc->sc_rxq.r_physaddr);
584 1.38 skrll mutex_exit(&ring->r_mtx);
585 1.1 martin }
586 1.1 martin
587 1.1 martin static int
588 1.1 martin dwc_gmac_alloc_dma_rings(struct dwc_gmac_softc *sc)
589 1.1 martin {
590 1.89 skrll const size_t ringsize = AWGE_TOTAL_RING_COUNT *
591 1.1 martin sizeof(struct dwc_gmac_dev_dmadesc);
592 1.1 martin int error, nsegs;
593 1.1 martin void *rings;
594 1.1 martin
595 1.89 skrll error = bus_dmamap_create(sc->sc_dmat, ringsize, 1, ringsize, 0,
596 1.1 martin BUS_DMA_NOWAIT, &sc->sc_dma_ring_map);
597 1.1 martin if (error != 0) {
598 1.1 martin aprint_error_dev(sc->sc_dev,
599 1.1 martin "could not create desc DMA map\n");
600 1.1 martin sc->sc_dma_ring_map = NULL;
601 1.1 martin goto fail;
602 1.1 martin }
603 1.1 martin
604 1.89 skrll error = bus_dmamem_alloc(sc->sc_dmat, ringsize, PAGE_SIZE, 0,
605 1.61 msaitoh &sc->sc_dma_ring_seg, 1, &nsegs, BUS_DMA_NOWAIT |BUS_DMA_COHERENT);
606 1.1 martin if (error != 0) {
607 1.1 martin aprint_error_dev(sc->sc_dev,
608 1.1 martin "could not map DMA memory\n");
609 1.1 martin goto fail;
610 1.1 martin }
611 1.1 martin
612 1.1 martin error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dma_ring_seg, nsegs,
613 1.89 skrll ringsize, &rings, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
614 1.1 martin if (error != 0) {
615 1.1 martin aprint_error_dev(sc->sc_dev,
616 1.1 martin "could not allocate DMA memory\n");
617 1.1 martin goto fail;
618 1.1 martin }
619 1.1 martin
620 1.1 martin error = bus_dmamap_load(sc->sc_dmat, sc->sc_dma_ring_map, rings,
621 1.89 skrll ringsize, NULL, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
622 1.1 martin if (error != 0) {
623 1.1 martin aprint_error_dev(sc->sc_dev,
624 1.1 martin "could not load desc DMA map\n");
625 1.1 martin goto fail;
626 1.1 martin }
627 1.1 martin
628 1.1 martin /* give first AWGE_RX_RING_COUNT to the RX side */
629 1.1 martin sc->sc_rxq.r_desc = rings;
630 1.1 martin sc->sc_rxq.r_physaddr = sc->sc_dma_ring_map->dm_segs[0].ds_addr;
631 1.1 martin
632 1.1 martin /* and next rings to the TX side */
633 1.1 martin sc->sc_txq.t_desc = sc->sc_rxq.r_desc + AWGE_RX_RING_COUNT;
634 1.38 skrll sc->sc_txq.t_physaddr = sc->sc_rxq.r_physaddr +
635 1.82 skrll AWGE_RX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc);
636 1.1 martin
637 1.1 martin return 0;
638 1.1 martin
639 1.1 martin fail:
640 1.1 martin dwc_gmac_free_dma_rings(sc);
641 1.1 martin return error;
642 1.1 martin }
643 1.1 martin
644 1.1 martin static void
645 1.1 martin dwc_gmac_free_dma_rings(struct dwc_gmac_softc *sc)
646 1.1 martin {
647 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map, 0,
648 1.1 martin sc->sc_dma_ring_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
649 1.1 martin bus_dmamap_unload(sc->sc_dmat, sc->sc_dma_ring_map);
650 1.1 martin bus_dmamem_unmap(sc->sc_dmat, sc->sc_rxq.r_desc,
651 1.1 martin AWGE_TOTAL_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc));
652 1.1 martin bus_dmamem_free(sc->sc_dmat, &sc->sc_dma_ring_seg, 1);
653 1.1 martin }
654 1.1 martin
655 1.1 martin static void
656 1.1 martin dwc_gmac_free_rx_ring(struct dwc_gmac_softc *sc, struct dwc_gmac_rx_ring *ring)
657 1.1 martin {
658 1.1 martin struct dwc_gmac_rx_data *data;
659 1.1 martin int i;
660 1.1 martin
661 1.1 martin if (ring->r_desc == NULL)
662 1.1 martin return;
663 1.1 martin
664 1.1 martin for (i = 0; i < AWGE_RX_RING_COUNT; i++) {
665 1.1 martin data = &ring->r_data[i];
666 1.1 martin
667 1.1 martin if (data->rd_map != NULL) {
668 1.1 martin bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
669 1.1 martin AWGE_RX_RING_COUNT
670 1.82 skrll * sizeof(struct dwc_gmac_dev_dmadesc),
671 1.1 martin BUS_DMASYNC_POSTREAD);
672 1.1 martin bus_dmamap_unload(sc->sc_dmat, data->rd_map);
673 1.1 martin bus_dmamap_destroy(sc->sc_dmat, data->rd_map);
674 1.1 martin }
675 1.88 rin m_freem(data->rd_m);
676 1.1 martin }
677 1.1 martin }
678 1.1 martin
679 1.1 martin static int
680 1.1 martin dwc_gmac_alloc_tx_ring(struct dwc_gmac_softc *sc,
681 1.1 martin struct dwc_gmac_tx_ring *ring)
682 1.1 martin {
683 1.1 martin int i, error = 0;
684 1.1 martin
685 1.1 martin ring->t_queued = 0;
686 1.1 martin ring->t_cur = ring->t_next = 0;
687 1.1 martin
688 1.82 skrll memset(ring->t_desc, 0, AWGE_TX_RING_COUNT * sizeof(*ring->t_desc));
689 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
690 1.1 martin TX_DESC_OFFSET(0),
691 1.82 skrll AWGE_TX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc),
692 1.89 skrll BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
693 1.1 martin
694 1.1 martin for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
695 1.1 martin error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
696 1.1 martin AWGE_TX_RING_COUNT, MCLBYTES, 0,
697 1.61 msaitoh BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
698 1.1 martin &ring->t_data[i].td_map);
699 1.1 martin if (error != 0) {
700 1.1 martin aprint_error_dev(sc->sc_dev,
701 1.1 martin "could not create TX DMA map #%d\n", i);
702 1.1 martin ring->t_data[i].td_map = NULL;
703 1.1 martin goto fail;
704 1.1 martin }
705 1.1 martin ring->t_desc[i].ddesc_next = htole32(
706 1.1 martin ring->t_physaddr + sizeof(struct dwc_gmac_dev_dmadesc)
707 1.87 skrll * TX_NEXT(i));
708 1.1 martin }
709 1.89 skrll bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
710 1.89 skrll TX_DESC_OFFSET(0),
711 1.89 skrll AWGE_TX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc),
712 1.89 skrll BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
713 1.1 martin
714 1.1 martin return 0;
715 1.1 martin
716 1.1 martin fail:
717 1.1 martin dwc_gmac_free_tx_ring(sc, ring);
718 1.1 martin return error;
719 1.1 martin }
720 1.1 martin
721 1.1 martin static void
722 1.1 martin dwc_gmac_txdesc_sync(struct dwc_gmac_softc *sc, int start, int end, int ops)
723 1.1 martin {
724 1.64 mrg /* 'end' is pointing one descriptor beyond the last we want to sync */
725 1.1 martin if (end > start) {
726 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
727 1.1 martin TX_DESC_OFFSET(start),
728 1.84 skrll TX_DESC_OFFSET(end) - TX_DESC_OFFSET(start),
729 1.1 martin ops);
730 1.1 martin return;
731 1.1 martin }
732 1.1 martin /* sync from 'start' to end of ring */
733 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
734 1.1 martin TX_DESC_OFFSET(start),
735 1.84 skrll TX_DESC_OFFSET(AWGE_TX_RING_COUNT) - TX_DESC_OFFSET(start),
736 1.1 martin ops);
737 1.47 jmcneill if (TX_DESC_OFFSET(end) - TX_DESC_OFFSET(0) > 0) {
738 1.47 jmcneill /* sync from start of ring to 'end' */
739 1.47 jmcneill bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
740 1.47 jmcneill TX_DESC_OFFSET(0),
741 1.84 skrll TX_DESC_OFFSET(end) - TX_DESC_OFFSET(0),
742 1.47 jmcneill ops);
743 1.47 jmcneill }
744 1.1 martin }
745 1.1 martin
746 1.1 martin static void
747 1.1 martin dwc_gmac_reset_tx_ring(struct dwc_gmac_softc *sc,
748 1.1 martin struct dwc_gmac_tx_ring *ring)
749 1.1 martin {
750 1.1 martin int i;
751 1.1 martin
752 1.38 skrll mutex_enter(&ring->t_mtx);
753 1.1 martin for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
754 1.1 martin struct dwc_gmac_tx_data *data = &ring->t_data[i];
755 1.1 martin
756 1.1 martin if (data->td_m != NULL) {
757 1.1 martin bus_dmamap_sync(sc->sc_dmat, data->td_active,
758 1.1 martin 0, data->td_active->dm_mapsize,
759 1.1 martin BUS_DMASYNC_POSTWRITE);
760 1.1 martin bus_dmamap_unload(sc->sc_dmat, data->td_active);
761 1.1 martin m_freem(data->td_m);
762 1.1 martin data->td_m = NULL;
763 1.1 martin }
764 1.1 martin }
765 1.1 martin
766 1.1 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
767 1.1 martin TX_DESC_OFFSET(0),
768 1.82 skrll AWGE_TX_RING_COUNT * sizeof(struct dwc_gmac_dev_dmadesc),
769 1.61 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
770 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR,
771 1.6 martin sc->sc_txq.t_physaddr);
772 1.1 martin
773 1.1 martin ring->t_queued = 0;
774 1.1 martin ring->t_cur = ring->t_next = 0;
775 1.38 skrll mutex_exit(&ring->t_mtx);
776 1.1 martin }
777 1.1 martin
778 1.1 martin static void
779 1.1 martin dwc_gmac_free_tx_ring(struct dwc_gmac_softc *sc,
780 1.1 martin struct dwc_gmac_tx_ring *ring)
781 1.1 martin {
782 1.1 martin int i;
783 1.1 martin
784 1.1 martin /* unload the maps */
785 1.1 martin for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
786 1.1 martin struct dwc_gmac_tx_data *data = &ring->t_data[i];
787 1.1 martin
788 1.1 martin if (data->td_m != NULL) {
789 1.1 martin bus_dmamap_sync(sc->sc_dmat, data->td_active,
790 1.1 martin 0, data->td_map->dm_mapsize,
791 1.1 martin BUS_DMASYNC_POSTWRITE);
792 1.1 martin bus_dmamap_unload(sc->sc_dmat, data->td_active);
793 1.1 martin m_freem(data->td_m);
794 1.1 martin data->td_m = NULL;
795 1.1 martin }
796 1.1 martin }
797 1.1 martin
798 1.1 martin /* and actually free them */
799 1.1 martin for (i = 0; i < AWGE_TX_RING_COUNT; i++) {
800 1.1 martin struct dwc_gmac_tx_data *data = &ring->t_data[i];
801 1.1 martin
802 1.1 martin bus_dmamap_destroy(sc->sc_dmat, data->td_map);
803 1.1 martin }
804 1.1 martin }
805 1.1 martin
806 1.1 martin static void
807 1.1 martin dwc_gmac_miibus_statchg(struct ifnet *ifp)
808 1.1 martin {
809 1.1 martin struct dwc_gmac_softc * const sc = ifp->if_softc;
810 1.1 martin struct mii_data * const mii = &sc->sc_mii;
811 1.25 jmcneill uint32_t conf, flow;
812 1.1 martin
813 1.1 martin /*
814 1.1 martin * Set MII or GMII interface based on the speed
815 1.38 skrll * negotiated by the PHY.
816 1.9 martin */
817 1.9 martin conf = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_CONF);
818 1.61 msaitoh conf &= ~(AWIN_GMAC_MAC_CONF_FES100 | AWIN_GMAC_MAC_CONF_MIISEL
819 1.61 msaitoh | AWIN_GMAC_MAC_CONF_FULLDPLX);
820 1.11 martin conf |= AWIN_GMAC_MAC_CONF_FRAMEBURST
821 1.11 martin | AWIN_GMAC_MAC_CONF_DISABLERXOWN
822 1.25 jmcneill | AWIN_GMAC_MAC_CONF_DISABLEJABBER
823 1.11 martin | AWIN_GMAC_MAC_CONF_RXENABLE
824 1.11 martin | AWIN_GMAC_MAC_CONF_TXENABLE;
825 1.1 martin switch (IFM_SUBTYPE(mii->mii_media_active)) {
826 1.1 martin case IFM_10_T:
827 1.12 jmcneill conf |= AWIN_GMAC_MAC_CONF_MIISEL;
828 1.9 martin break;
829 1.1 martin case IFM_100_TX:
830 1.12 jmcneill conf |= AWIN_GMAC_MAC_CONF_FES100 |
831 1.12 jmcneill AWIN_GMAC_MAC_CONF_MIISEL;
832 1.1 martin break;
833 1.1 martin case IFM_1000_T:
834 1.1 martin break;
835 1.1 martin }
836 1.46 jmcneill if (sc->sc_set_speed)
837 1.46 jmcneill sc->sc_set_speed(sc, IFM_SUBTYPE(mii->mii_media_active));
838 1.25 jmcneill
839 1.25 jmcneill flow = 0;
840 1.25 jmcneill if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
841 1.9 martin conf |= AWIN_GMAC_MAC_CONF_FULLDPLX;
842 1.25 jmcneill flow |= __SHIFTIN(0x200, AWIN_GMAC_MAC_FLOWCTRL_PAUSE);
843 1.25 jmcneill }
844 1.25 jmcneill if (mii->mii_media_active & IFM_ETH_TXPAUSE) {
845 1.25 jmcneill flow |= AWIN_GMAC_MAC_FLOWCTRL_TFE;
846 1.25 jmcneill }
847 1.25 jmcneill if (mii->mii_media_active & IFM_ETH_RXPAUSE) {
848 1.25 jmcneill flow |= AWIN_GMAC_MAC_FLOWCTRL_RFE;
849 1.25 jmcneill }
850 1.25 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh,
851 1.25 jmcneill AWIN_GMAC_MAC_FLOWCTRL, flow);
852 1.9 martin
853 1.9 martin #ifdef DWC_GMAC_DEBUG
854 1.9 martin aprint_normal_dev(sc->sc_dev,
855 1.9 martin "setting MAC conf register: %08x\n", conf);
856 1.9 martin #endif
857 1.9 martin
858 1.9 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh,
859 1.9 martin AWIN_GMAC_MAC_CONF, conf);
860 1.1 martin }
861 1.1 martin
862 1.1 martin static int
863 1.1 martin dwc_gmac_init(struct ifnet *ifp)
864 1.1 martin {
865 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
866 1.38 skrll
867 1.93 skrll KASSERT(IFNET_LOCKED(ifp));
868 1.93 skrll
869 1.93 skrll mutex_enter(sc->sc_core_lock);
870 1.38 skrll int ret = dwc_gmac_init_locked(ifp);
871 1.93 skrll mutex_exit(sc->sc_core_lock);
872 1.38 skrll
873 1.38 skrll return ret;
874 1.38 skrll }
875 1.38 skrll
876 1.38 skrll static int
877 1.38 skrll dwc_gmac_init_locked(struct ifnet *ifp)
878 1.38 skrll {
879 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
880 1.13 jmcneill uint32_t ffilt;
881 1.1 martin
882 1.93 skrll ASSERT_SLEEPABLE();
883 1.93 skrll KASSERT(IFNET_LOCKED(ifp));
884 1.93 skrll KASSERT(mutex_owned(sc->sc_core_lock));
885 1.93 skrll KASSERT(ifp == &sc->sc_ec.ec_if);
886 1.1 martin
887 1.38 skrll dwc_gmac_stop_locked(ifp, 0);
888 1.1 martin
889 1.1 martin /*
890 1.11 martin * Configure DMA burst/transfer mode and RX/TX priorities.
891 1.11 martin * XXX - the GMAC_BUSMODE_PRIORXTX bits are undocumented.
892 1.11 martin */
893 1.11 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE,
894 1.25 jmcneill GMAC_BUSMODE_FIXEDBURST | GMAC_BUSMODE_4PBL |
895 1.25 jmcneill __SHIFTIN(2, GMAC_BUSMODE_RPBL) |
896 1.25 jmcneill __SHIFTIN(2, GMAC_BUSMODE_PBL));
897 1.11 martin
898 1.11 martin /*
899 1.13 jmcneill * Set up address filter
900 1.11 martin */
901 1.20 jmcneill ffilt = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT);
902 1.20 jmcneill if (ifp->if_flags & IFF_PROMISC) {
903 1.13 jmcneill ffilt |= AWIN_GMAC_MAC_FFILT_PR;
904 1.20 jmcneill } else {
905 1.20 jmcneill ffilt &= ~AWIN_GMAC_MAC_FFILT_PR;
906 1.20 jmcneill }
907 1.20 jmcneill if (ifp->if_flags & IFF_BROADCAST) {
908 1.20 jmcneill ffilt &= ~AWIN_GMAC_MAC_FFILT_DBF;
909 1.20 jmcneill } else {
910 1.20 jmcneill ffilt |= AWIN_GMAC_MAC_FFILT_DBF;
911 1.20 jmcneill }
912 1.13 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT, ffilt);
913 1.11 martin
914 1.11 martin /*
915 1.20 jmcneill * Set up multicast filter
916 1.20 jmcneill */
917 1.20 jmcneill dwc_gmac_setmulti(sc);
918 1.20 jmcneill
919 1.20 jmcneill /*
920 1.6 martin * Set up dma pointer for RX and TX ring
921 1.1 martin */
922 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR,
923 1.6 martin sc->sc_rxq.r_physaddr);
924 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR,
925 1.6 martin sc->sc_txq.t_physaddr);
926 1.6 martin
927 1.6 martin /*
928 1.10 martin * Start RX/TX part
929 1.6 martin */
930 1.46 jmcneill uint32_t opmode = GMAC_DMA_OP_RXSTART | GMAC_DMA_OP_TXSTART;
931 1.46 jmcneill if ((sc->sc_flags & DWC_GMAC_FORCE_THRESH_DMA_MODE) == 0) {
932 1.46 jmcneill opmode |= GMAC_DMA_OP_RXSTOREFORWARD | GMAC_DMA_OP_TXSTOREFORWARD;
933 1.46 jmcneill }
934 1.46 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_OPMODE, opmode);
935 1.90 skrll #ifdef DWC_GMAC_DEBUG
936 1.90 skrll aprint_normal_dev(sc->sc_dev,
937 1.90 skrll "setting DMA opmode register: %08x\n", opmode);
938 1.90 skrll #endif
939 1.1 martin
940 1.93 skrll ifp->if_flags |= IFF_RUNNING;
941 1.93 skrll sc->sc_if_flags = ifp->if_flags;
942 1.93 skrll
943 1.93 skrll mutex_enter(sc->sc_intr_lock);
944 1.38 skrll sc->sc_stopping = false;
945 1.38 skrll
946 1.93 skrll mutex_enter(&sc->sc_txq.t_mtx);
947 1.78 thorpej sc->sc_txbusy = false;
948 1.93 skrll mutex_exit(&sc->sc_txq.t_mtx);
949 1.93 skrll mutex_exit(sc->sc_intr_lock);
950 1.1 martin
951 1.1 martin return 0;
952 1.1 martin }
953 1.1 martin
954 1.1 martin static void
955 1.1 martin dwc_gmac_start(struct ifnet *ifp)
956 1.1 martin {
957 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
958 1.43 ozaki KASSERT(if_is_mpsafe(ifp));
959 1.38 skrll
960 1.93 skrll mutex_enter(sc->sc_intr_lock);
961 1.38 skrll if (!sc->sc_stopping) {
962 1.38 skrll dwc_gmac_start_locked(ifp);
963 1.38 skrll }
964 1.93 skrll mutex_exit(sc->sc_intr_lock);
965 1.38 skrll }
966 1.38 skrll
967 1.38 skrll static void
968 1.38 skrll dwc_gmac_start_locked(struct ifnet *ifp)
969 1.38 skrll {
970 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
971 1.1 martin int old = sc->sc_txq.t_queued;
972 1.30 martin int start = sc->sc_txq.t_cur;
973 1.1 martin struct mbuf *m0;
974 1.1 martin
975 1.93 skrll KASSERT(mutex_owned(sc->sc_intr_lock));
976 1.93 skrll
977 1.93 skrll mutex_enter(&sc->sc_txq.t_mtx);
978 1.93 skrll if (sc->sc_txbusy) {
979 1.93 skrll mutex_exit(&sc->sc_txq.t_mtx);
980 1.1 martin return;
981 1.93 skrll }
982 1.1 martin
983 1.1 martin for (;;) {
984 1.1 martin IFQ_POLL(&ifp->if_snd, m0);
985 1.1 martin if (m0 == NULL)
986 1.1 martin break;
987 1.1 martin if (dwc_gmac_queue(sc, m0) != 0) {
988 1.78 thorpej sc->sc_txbusy = true;
989 1.1 martin break;
990 1.1 martin }
991 1.1 martin IFQ_DEQUEUE(&ifp->if_snd, m0);
992 1.50 msaitoh bpf_mtap(ifp, m0, BPF_D_OUT);
993 1.32 martin if (sc->sc_txq.t_queued == AWGE_TX_RING_COUNT) {
994 1.78 thorpej sc->sc_txbusy = true;
995 1.32 martin break;
996 1.32 martin }
997 1.1 martin }
998 1.1 martin
999 1.1 martin if (sc->sc_txq.t_queued != old) {
1000 1.1 martin /* packets have been queued, kick it off */
1001 1.30 martin dwc_gmac_txdesc_sync(sc, start, sc->sc_txq.t_cur,
1002 1.61 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1003 1.10 martin
1004 1.10 martin #ifdef DWC_GMAC_DEBUG
1005 1.10 martin dwc_dump_status(sc);
1006 1.10 martin #endif
1007 1.55 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh,
1008 1.55 martin AWIN_GMAC_DMA_TXPOLL, ~0U);
1009 1.1 martin }
1010 1.93 skrll mutex_exit(&sc->sc_txq.t_mtx);
1011 1.1 martin }
1012 1.1 martin
1013 1.1 martin static void
1014 1.1 martin dwc_gmac_stop(struct ifnet *ifp, int disable)
1015 1.1 martin {
1016 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
1017 1.1 martin
1018 1.93 skrll mutex_enter(sc->sc_core_lock);
1019 1.38 skrll dwc_gmac_stop_locked(ifp, disable);
1020 1.93 skrll mutex_exit(sc->sc_core_lock);
1021 1.38 skrll }
1022 1.38 skrll
1023 1.38 skrll static void
1024 1.38 skrll dwc_gmac_stop_locked(struct ifnet *ifp, int disable)
1025 1.38 skrll {
1026 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
1027 1.38 skrll
1028 1.93 skrll ASSERT_SLEEPABLE();
1029 1.93 skrll KASSERT(IFNET_LOCKED(ifp));
1030 1.93 skrll KASSERT(mutex_owned(sc->sc_core_lock));
1031 1.93 skrll
1032 1.93 skrll mutex_enter(sc->sc_intr_lock);
1033 1.38 skrll sc->sc_stopping = true;
1034 1.38 skrll
1035 1.93 skrll mutex_enter(&sc->sc_txq.t_mtx);
1036 1.93 skrll sc->sc_txbusy = false;
1037 1.93 skrll mutex_exit(&sc->sc_txq.t_mtx);
1038 1.93 skrll
1039 1.93 skrll mutex_exit(sc->sc_intr_lock);
1040 1.93 skrll
1041 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh,
1042 1.6 martin AWIN_GMAC_DMA_OPMODE,
1043 1.6 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1044 1.62 msaitoh AWIN_GMAC_DMA_OPMODE)
1045 1.61 msaitoh & ~(GMAC_DMA_OP_TXSTART | GMAC_DMA_OP_RXSTART));
1046 1.6 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh,
1047 1.6 martin AWIN_GMAC_DMA_OPMODE,
1048 1.6 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1049 1.62 msaitoh AWIN_GMAC_DMA_OPMODE) | GMAC_DMA_OP_FLUSHTX);
1050 1.6 martin
1051 1.1 martin mii_down(&sc->sc_mii);
1052 1.1 martin dwc_gmac_reset_tx_ring(sc, &sc->sc_txq);
1053 1.1 martin dwc_gmac_reset_rx_ring(sc, &sc->sc_rxq);
1054 1.48 jmcneill
1055 1.78 thorpej ifp->if_flags &= ~IFF_RUNNING;
1056 1.93 skrll sc->sc_if_flags = ifp->if_flags;
1057 1.1 martin }
1058 1.1 martin
1059 1.1 martin /*
1060 1.1 martin * Add m0 to the TX ring
1061 1.1 martin */
1062 1.1 martin static int
1063 1.1 martin dwc_gmac_queue(struct dwc_gmac_softc *sc, struct mbuf *m0)
1064 1.1 martin {
1065 1.1 martin struct dwc_gmac_dev_dmadesc *desc = NULL;
1066 1.1 martin struct dwc_gmac_tx_data *data = NULL;
1067 1.1 martin bus_dmamap_t map;
1068 1.1 martin int error, i, first;
1069 1.1 martin
1070 1.8 martin #ifdef DWC_GMAC_DEBUG
1071 1.8 martin aprint_normal_dev(sc->sc_dev,
1072 1.8 martin "dwc_gmac_queue: adding mbuf chain %p\n", m0);
1073 1.8 martin #endif
1074 1.8 martin
1075 1.1 martin first = sc->sc_txq.t_cur;
1076 1.1 martin map = sc->sc_txq.t_data[first].td_map;
1077 1.1 martin
1078 1.1 martin error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m0,
1079 1.61 msaitoh BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1080 1.1 martin if (error != 0) {
1081 1.1 martin aprint_error_dev(sc->sc_dev, "could not map mbuf "
1082 1.1 martin "(len: %d, error %d)\n", m0->m_pkthdr.len, error);
1083 1.1 martin return error;
1084 1.1 martin }
1085 1.1 martin
1086 1.32 martin if (sc->sc_txq.t_queued + map->dm_nsegs > AWGE_TX_RING_COUNT) {
1087 1.1 martin bus_dmamap_unload(sc->sc_dmat, map);
1088 1.1 martin return ENOBUFS;
1089 1.1 martin }
1090 1.1 martin
1091 1.1 martin for (i = 0; i < map->dm_nsegs; i++) {
1092 1.1 martin data = &sc->sc_txq.t_data[sc->sc_txq.t_cur];
1093 1.8 martin desc = &sc->sc_txq.t_desc[sc->sc_txq.t_cur];
1094 1.8 martin
1095 1.8 martin desc->ddesc_data = htole32(map->dm_segs[i].ds_addr);
1096 1.7 martin
1097 1.7 martin #ifdef DWC_GMAC_DEBUG
1098 1.81 skrll aprint_normal_dev(sc->sc_dev, "enqueuing desc #%d data %08lx "
1099 1.55 martin "len %lu\n", sc->sc_txq.t_cur,
1100 1.7 martin (unsigned long)map->dm_segs[i].ds_addr,
1101 1.55 martin (unsigned long)map->dm_segs[i].ds_len);
1102 1.7 martin #endif
1103 1.7 martin
1104 1.55 martin sc->sc_descm->tx_init_flags(desc);
1105 1.55 martin sc->sc_descm->tx_set_len(desc, map->dm_segs[i].ds_len);
1106 1.55 martin
1107 1.55 martin if (i == 0)
1108 1.55 martin sc->sc_descm->tx_set_first_frag(desc);
1109 1.1 martin
1110 1.1 martin /*
1111 1.1 martin * Defer passing ownership of the first descriptor
1112 1.23 joerg * until we are done.
1113 1.1 martin */
1114 1.55 martin if (i != 0)
1115 1.55 martin sc->sc_descm->tx_set_owned_by_dev(desc);
1116 1.8 martin
1117 1.6 martin sc->sc_txq.t_queued++;
1118 1.8 martin sc->sc_txq.t_cur = TX_NEXT(sc->sc_txq.t_cur);
1119 1.1 martin }
1120 1.1 martin
1121 1.55 martin sc->sc_descm->tx_set_last_frag(desc);
1122 1.1 martin
1123 1.1 martin data->td_m = m0;
1124 1.1 martin data->td_active = map;
1125 1.1 martin
1126 1.89 skrll /* sync the packet buffer */
1127 1.1 martin bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
1128 1.34 jmcneill BUS_DMASYNC_PREWRITE);
1129 1.1 martin
1130 1.89 skrll /* sync the new descriptors - ownership not transferred yet */
1131 1.89 skrll dwc_gmac_txdesc_sync(sc, first, sc->sc_txq.t_cur,
1132 1.89 skrll BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1133 1.89 skrll
1134 1.32 martin /* Pass first to device */
1135 1.55 martin sc->sc_descm->tx_set_owned_by_dev(&sc->sc_txq.t_desc[first]);
1136 1.55 martin
1137 1.1 martin return 0;
1138 1.1 martin }
1139 1.1 martin
1140 1.22 martin /*
1141 1.22 martin * If the interface is up and running, only modify the receive
1142 1.22 martin * filter when setting promiscuous or debug mode. Otherwise fall
1143 1.22 martin * through to ether_ioctl, which will reset the chip.
1144 1.22 martin */
1145 1.22 martin static int
1146 1.22 martin dwc_gmac_ifflags_cb(struct ethercom *ec)
1147 1.22 martin {
1148 1.92 skrll struct ifnet * const ifp = &ec->ec_if;
1149 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
1150 1.38 skrll int ret = 0;
1151 1.38 skrll
1152 1.93 skrll KASSERT(IFNET_LOCKED(ifp));
1153 1.93 skrll mutex_enter(sc->sc_core_lock);
1154 1.93 skrll
1155 1.65 msaitoh u_short change = ifp->if_flags ^ sc->sc_if_flags;
1156 1.38 skrll sc->sc_if_flags = ifp->if_flags;
1157 1.22 martin
1158 1.61 msaitoh if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
1159 1.38 skrll ret = ENETRESET;
1160 1.93 skrll } else if ((change & IFF_PROMISC) != 0) {
1161 1.22 martin dwc_gmac_setmulti(sc);
1162 1.38 skrll }
1163 1.93 skrll
1164 1.93 skrll mutex_exit(sc->sc_core_lock);
1165 1.38 skrll
1166 1.38 skrll return ret;
1167 1.22 martin }
1168 1.22 martin
1169 1.1 martin static int
1170 1.1 martin dwc_gmac_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1171 1.1 martin {
1172 1.92 skrll struct dwc_gmac_softc * const sc = ifp->if_softc;
1173 1.38 skrll int error = 0;
1174 1.38 skrll
1175 1.93 skrll switch (cmd) {
1176 1.93 skrll case SIOCADDMULTI:
1177 1.93 skrll case SIOCDELMULTI:
1178 1.93 skrll break;
1179 1.93 skrll default:
1180 1.93 skrll KASSERT(IFNET_LOCKED(ifp));
1181 1.93 skrll }
1182 1.93 skrll
1183 1.93 skrll const int s = splnet();
1184 1.38 skrll error = ether_ioctl(ifp, cmd, data);
1185 1.38 skrll splx(s);
1186 1.1 martin
1187 1.38 skrll if (error == ENETRESET) {
1188 1.1 martin error = 0;
1189 1.93 skrll if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
1190 1.93 skrll mutex_enter(sc->sc_core_lock);
1191 1.93 skrll if (sc->sc_if_flags & IFF_RUNNING) {
1192 1.93 skrll /*
1193 1.93 skrll * Multicast list has changed; set the hardware
1194 1.93 skrll * filter accordingly.
1195 1.93 skrll */
1196 1.93 skrll dwc_gmac_setmulti(sc);
1197 1.93 skrll }
1198 1.93 skrll mutex_exit(sc->sc_core_lock);
1199 1.22 martin }
1200 1.1 martin }
1201 1.1 martin
1202 1.1 martin return error;
1203 1.1 martin }
1204 1.1 martin
1205 1.8 martin static void
1206 1.8 martin dwc_gmac_tx_intr(struct dwc_gmac_softc *sc)
1207 1.8 martin {
1208 1.92 skrll struct ifnet * const ifp = &sc->sc_ec.ec_if;
1209 1.8 martin struct dwc_gmac_tx_data *data;
1210 1.8 martin struct dwc_gmac_dev_dmadesc *desc;
1211 1.32 martin int i, nsegs;
1212 1.8 martin
1213 1.38 skrll mutex_enter(&sc->sc_txq.t_mtx);
1214 1.38 skrll
1215 1.32 martin for (i = sc->sc_txq.t_next; sc->sc_txq.t_queued > 0; i = TX_NEXT(i)) {
1216 1.8 martin #ifdef DWC_GMAC_DEBUG
1217 1.8 martin aprint_normal_dev(sc->sc_dev,
1218 1.90 skrll "%s: checking desc #%d (t_queued: %d)\n", __func__,
1219 1.8 martin i, sc->sc_txq.t_queued);
1220 1.8 martin #endif
1221 1.8 martin
1222 1.26 martin /*
1223 1.82 skrll * i + 1 does not need to be a valid descriptor,
1224 1.26 martin * this is just a special notion to just sync
1225 1.26 martin * a single tx descriptor (i)
1226 1.26 martin */
1227 1.82 skrll dwc_gmac_txdesc_sync(sc, i, i + 1,
1228 1.61 msaitoh BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1229 1.11 martin
1230 1.32 martin desc = &sc->sc_txq.t_desc[i];
1231 1.55 martin if (sc->sc_descm->tx_is_owned_by_dev(desc))
1232 1.8 martin break;
1233 1.11 martin
1234 1.8 martin data = &sc->sc_txq.t_data[i];
1235 1.8 martin if (data->td_m == NULL)
1236 1.8 martin continue;
1237 1.32 martin
1238 1.69 thorpej if_statinc(ifp, if_opackets);
1239 1.32 martin nsegs = data->td_active->dm_nsegs;
1240 1.8 martin bus_dmamap_sync(sc->sc_dmat, data->td_active, 0,
1241 1.8 martin data->td_active->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1242 1.8 martin bus_dmamap_unload(sc->sc_dmat, data->td_active);
1243 1.8 martin
1244 1.8 martin #ifdef DWC_GMAC_DEBUG
1245 1.8 martin aprint_normal_dev(sc->sc_dev,
1246 1.90 skrll "%s: done with packet at desc #%d, freeing mbuf %p\n",
1247 1.90 skrll __func__, i, data->td_m);
1248 1.8 martin #endif
1249 1.8 martin
1250 1.8 martin m_freem(data->td_m);
1251 1.8 martin data->td_m = NULL;
1252 1.32 martin
1253 1.32 martin sc->sc_txq.t_queued -= nsegs;
1254 1.8 martin }
1255 1.8 martin
1256 1.8 martin sc->sc_txq.t_next = i;
1257 1.8 martin
1258 1.8 martin if (sc->sc_txq.t_queued < AWGE_TX_RING_COUNT) {
1259 1.78 thorpej sc->sc_txbusy = false;
1260 1.8 martin }
1261 1.38 skrll mutex_exit(&sc->sc_txq.t_mtx);
1262 1.8 martin }
1263 1.8 martin
1264 1.8 martin static void
1265 1.8 martin dwc_gmac_rx_intr(struct dwc_gmac_softc *sc)
1266 1.8 martin {
1267 1.92 skrll struct ifnet * const ifp = &sc->sc_ec.ec_if;
1268 1.11 martin struct dwc_gmac_dev_dmadesc *desc;
1269 1.11 martin struct dwc_gmac_rx_data *data;
1270 1.11 martin bus_addr_t physaddr;
1271 1.11 martin struct mbuf *m, *mnew;
1272 1.11 martin int i, len, error;
1273 1.11 martin
1274 1.38 skrll mutex_enter(&sc->sc_rxq.r_mtx);
1275 1.11 martin for (i = sc->sc_rxq.r_cur; ; i = RX_NEXT(i)) {
1276 1.90 skrll #ifdef DWC_GMAC_DEBUG
1277 1.90 skrll aprint_normal_dev(sc->sc_dev, "%s: checking desc #%d\n",
1278 1.90 skrll __func__, i);
1279 1.90 skrll #endif
1280 1.11 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
1281 1.11 martin RX_DESC_OFFSET(i), sizeof(*desc),
1282 1.61 msaitoh BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1283 1.11 martin desc = &sc->sc_rxq.r_desc[i];
1284 1.11 martin data = &sc->sc_rxq.r_data[i];
1285 1.11 martin
1286 1.55 martin if (sc->sc_descm->rx_is_owned_by_dev(desc))
1287 1.11 martin break;
1288 1.11 martin
1289 1.55 martin if (sc->sc_descm->rx_has_error(desc)) {
1290 1.11 martin #ifdef DWC_GMAC_DEBUG
1291 1.15 martin aprint_normal_dev(sc->sc_dev,
1292 1.90 skrll "%s: RX error: status %08x, skipping\n",
1293 1.90 skrll __func__, le32toh(desc->ddesc_status0));
1294 1.11 martin #endif
1295 1.69 thorpej if_statinc(ifp, if_ierrors);
1296 1.11 martin goto skip;
1297 1.11 martin }
1298 1.11 martin
1299 1.55 martin len = sc->sc_descm->rx_get_len(desc);
1300 1.11 martin
1301 1.11 martin #ifdef DWC_GMAC_DEBUG
1302 1.15 martin aprint_normal_dev(sc->sc_dev,
1303 1.90 skrll "%s: device is done with descriptor #%d, len: %d\n",
1304 1.90 skrll __func__, i, len);
1305 1.11 martin #endif
1306 1.11 martin
1307 1.11 martin /*
1308 1.11 martin * Try to get a new mbuf before passing this one
1309 1.11 martin * up, if that fails, drop the packet and reuse
1310 1.11 martin * the existing one.
1311 1.11 martin */
1312 1.11 martin MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1313 1.11 martin if (mnew == NULL) {
1314 1.69 thorpej if_statinc(ifp, if_ierrors);
1315 1.11 martin goto skip;
1316 1.11 martin }
1317 1.11 martin MCLGET(mnew, M_DONTWAIT);
1318 1.11 martin if ((mnew->m_flags & M_EXT) == 0) {
1319 1.11 martin m_freem(mnew);
1320 1.69 thorpej if_statinc(ifp, if_ierrors);
1321 1.11 martin goto skip;
1322 1.11 martin }
1323 1.66 tnn mnew->m_len = mnew->m_pkthdr.len = mnew->m_ext.ext_size;
1324 1.66 tnn if (mnew->m_len > AWGE_MAX_PACKET) {
1325 1.66 tnn mnew->m_len = mnew->m_pkthdr.len = AWGE_MAX_PACKET;
1326 1.66 tnn }
1327 1.11 martin
1328 1.11 martin /* unload old DMA map */
1329 1.11 martin bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
1330 1.11 martin data->rd_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1331 1.11 martin bus_dmamap_unload(sc->sc_dmat, data->rd_map);
1332 1.11 martin
1333 1.11 martin /* and reload with new mbuf */
1334 1.66 tnn error = bus_dmamap_load_mbuf(sc->sc_dmat, data->rd_map,
1335 1.66 tnn mnew, BUS_DMA_READ | BUS_DMA_NOWAIT);
1336 1.11 martin if (error != 0) {
1337 1.11 martin m_freem(mnew);
1338 1.11 martin /* try to reload old mbuf */
1339 1.66 tnn error = bus_dmamap_load_mbuf(sc->sc_dmat, data->rd_map,
1340 1.66 tnn data->rd_m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1341 1.11 martin if (error != 0) {
1342 1.11 martin panic("%s: could not load old rx mbuf",
1343 1.11 martin device_xname(sc->sc_dev));
1344 1.11 martin }
1345 1.69 thorpej if_statinc(ifp, if_ierrors);
1346 1.11 martin goto skip;
1347 1.11 martin }
1348 1.11 martin physaddr = data->rd_map->dm_segs[0].ds_addr;
1349 1.11 martin
1350 1.90 skrll #ifdef DWC_GMAC_DEBUG
1351 1.90 skrll aprint_normal_dev(sc->sc_dev,
1352 1.90 skrll "%s: receiving packet at desc #%d, using mbuf %p\n",
1353 1.90 skrll __func__, i, data->rd_m);
1354 1.90 skrll #endif
1355 1.11 martin /*
1356 1.11 martin * New mbuf loaded, update RX ring and continue
1357 1.11 martin */
1358 1.11 martin m = data->rd_m;
1359 1.11 martin data->rd_m = mnew;
1360 1.11 martin desc->ddesc_data = htole32(physaddr);
1361 1.11 martin
1362 1.11 martin /* finalize mbuf */
1363 1.11 martin m->m_pkthdr.len = m->m_len = len;
1364 1.36 ozaki m_set_rcvif(m, ifp);
1365 1.77 sekiya m->m_flags |= M_HASFCS;
1366 1.11 martin
1367 1.39 skrll if_percpuq_enqueue(sc->sc_ipq, m);
1368 1.11 martin
1369 1.11 martin skip:
1370 1.27 matt bus_dmamap_sync(sc->sc_dmat, data->rd_map, 0,
1371 1.27 matt data->rd_map->dm_mapsize, BUS_DMASYNC_PREREAD);
1372 1.55 martin
1373 1.55 martin sc->sc_descm->rx_init_flags(desc);
1374 1.66 tnn sc->sc_descm->rx_set_len(desc, data->rd_m->m_len);
1375 1.89 skrll
1376 1.89 skrll bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
1377 1.89 skrll RX_DESC_OFFSET(i), sizeof(*desc),
1378 1.89 skrll BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1379 1.89 skrll
1380 1.55 martin sc->sc_descm->rx_set_owned_by_dev(desc);
1381 1.55 martin
1382 1.11 martin bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
1383 1.11 martin RX_DESC_OFFSET(i), sizeof(*desc),
1384 1.61 msaitoh BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1385 1.11 martin }
1386 1.11 martin
1387 1.11 martin /* update RX pointer */
1388 1.11 martin sc->sc_rxq.r_cur = i;
1389 1.11 martin
1390 1.38 skrll mutex_exit(&sc->sc_rxq.r_mtx);
1391 1.8 martin }
1392 1.8 martin
1393 1.20 jmcneill static void
1394 1.20 jmcneill dwc_gmac_setmulti(struct dwc_gmac_softc *sc)
1395 1.20 jmcneill {
1396 1.20 jmcneill struct ifnet * const ifp = &sc->sc_ec.ec_if;
1397 1.20 jmcneill struct ether_multi *enm;
1398 1.20 jmcneill struct ether_multistep step;
1399 1.59 ozaki struct ethercom *ec = &sc->sc_ec;
1400 1.20 jmcneill uint32_t hashes[2] = { 0, 0 };
1401 1.22 martin uint32_t ffilt, h;
1402 1.38 skrll int mcnt;
1403 1.22 martin
1404 1.93 skrll KASSERT(mutex_owned(sc->sc_core_lock));
1405 1.20 jmcneill
1406 1.20 jmcneill ffilt = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT);
1407 1.38 skrll
1408 1.93 skrll if (sc->sc_if_flags & IFF_PROMISC) {
1409 1.22 martin ffilt |= AWIN_GMAC_MAC_FFILT_PR;
1410 1.22 martin goto special_filter;
1411 1.20 jmcneill }
1412 1.20 jmcneill
1413 1.61 msaitoh ffilt &= ~(AWIN_GMAC_MAC_FFILT_PM | AWIN_GMAC_MAC_FFILT_PR);
1414 1.20 jmcneill
1415 1.20 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW, 0);
1416 1.20 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH, 0);
1417 1.20 jmcneill
1418 1.59 ozaki ETHER_LOCK(ec);
1419 1.60 ozaki ec->ec_flags &= ~ETHER_F_ALLMULTI;
1420 1.59 ozaki ETHER_FIRST_MULTI(step, ec, enm);
1421 1.20 jmcneill mcnt = 0;
1422 1.20 jmcneill while (enm != NULL) {
1423 1.20 jmcneill if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1424 1.22 martin ETHER_ADDR_LEN) != 0) {
1425 1.60 ozaki ffilt |= AWIN_GMAC_MAC_FFILT_PM;
1426 1.60 ozaki ec->ec_flags |= ETHER_F_ALLMULTI;
1427 1.59 ozaki ETHER_UNLOCK(ec);
1428 1.22 martin goto special_filter;
1429 1.22 martin }
1430 1.20 jmcneill
1431 1.86 jakllsch h = ~ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
1432 1.20 jmcneill hashes[h >> 5] |= (1 << (h & 0x1f));
1433 1.20 jmcneill
1434 1.20 jmcneill mcnt++;
1435 1.20 jmcneill ETHER_NEXT_MULTI(step, enm);
1436 1.20 jmcneill }
1437 1.59 ozaki ETHER_UNLOCK(ec);
1438 1.20 jmcneill
1439 1.20 jmcneill if (mcnt)
1440 1.20 jmcneill ffilt |= AWIN_GMAC_MAC_FFILT_HMC;
1441 1.20 jmcneill else
1442 1.20 jmcneill ffilt &= ~AWIN_GMAC_MAC_FFILT_HMC;
1443 1.20 jmcneill
1444 1.20 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT, ffilt);
1445 1.20 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW,
1446 1.20 jmcneill hashes[0]);
1447 1.20 jmcneill bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH,
1448 1.20 jmcneill hashes[1]);
1449 1.22 martin
1450 1.22 martin #ifdef DWC_GMAC_DEBUG
1451 1.22 martin dwc_gmac_dump_ffilt(sc, ffilt);
1452 1.22 martin #endif
1453 1.22 martin return;
1454 1.22 martin
1455 1.22 martin special_filter:
1456 1.22 martin #ifdef DWC_GMAC_DEBUG
1457 1.22 martin dwc_gmac_dump_ffilt(sc, ffilt);
1458 1.22 martin #endif
1459 1.22 martin /* no MAC hashes, ALLMULTI or PROMISC */
1460 1.22 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT,
1461 1.22 martin ffilt);
1462 1.22 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTLOW,
1463 1.22 martin 0xffffffff);
1464 1.22 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_HTHIGH,
1465 1.22 martin 0xffffffff);
1466 1.93 skrll sc->sc_if_flags = ifp->if_flags;
1467 1.20 jmcneill }
1468 1.20 jmcneill
1469 1.1 martin int
1470 1.1 martin dwc_gmac_intr(struct dwc_gmac_softc *sc)
1471 1.1 martin {
1472 1.1 martin uint32_t status, dma_status;
1473 1.8 martin int rv = 0;
1474 1.1 martin
1475 1.93 skrll mutex_enter(sc->sc_intr_lock);
1476 1.93 skrll if (sc->sc_stopping) {
1477 1.93 skrll mutex_exit(sc->sc_intr_lock);
1478 1.38 skrll return 0;
1479 1.93 skrll }
1480 1.38 skrll
1481 1.1 martin status = bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_INTR);
1482 1.2 martin if (status & AWIN_GMAC_MII_IRQ) {
1483 1.1 martin (void)bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1484 1.1 martin AWIN_GMAC_MII_STATUS);
1485 1.8 martin rv = 1;
1486 1.2 martin mii_pollstat(&sc->sc_mii);
1487 1.2 martin }
1488 1.1 martin
1489 1.1 martin dma_status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1490 1.1 martin AWIN_GMAC_DMA_STATUS);
1491 1.1 martin
1492 1.61 msaitoh if (dma_status & (GMAC_DMA_INT_NIE | GMAC_DMA_INT_AIE))
1493 1.8 martin rv = 1;
1494 1.1 martin
1495 1.8 martin if (dma_status & GMAC_DMA_INT_TIE)
1496 1.8 martin dwc_gmac_tx_intr(sc);
1497 1.1 martin
1498 1.8 martin if (dma_status & GMAC_DMA_INT_RIE)
1499 1.8 martin dwc_gmac_rx_intr(sc);
1500 1.8 martin
1501 1.8 martin /*
1502 1.8 martin * Check error conditions
1503 1.8 martin */
1504 1.8 martin if (dma_status & GMAC_DMA_INT_ERRORS) {
1505 1.69 thorpej if_statinc(&sc->sc_ec.ec_if, if_oerrors);
1506 1.8 martin #ifdef DWC_GMAC_DEBUG
1507 1.8 martin dwc_dump_and_abort(sc, "interrupt error condition");
1508 1.8 martin #endif
1509 1.8 martin }
1510 1.8 martin
1511 1.63 msaitoh rnd_add_uint32(&sc->rnd_source, dma_status);
1512 1.63 msaitoh
1513 1.8 martin /* ack interrupt */
1514 1.8 martin if (dma_status)
1515 1.8 martin bus_space_write_4(sc->sc_bst, sc->sc_bsh,
1516 1.8 martin AWIN_GMAC_DMA_STATUS, dma_status & GMAC_DMA_INT_MASK);
1517 1.8 martin
1518 1.28 martin /*
1519 1.28 martin * Get more packets
1520 1.28 martin */
1521 1.28 martin if (rv)
1522 1.40 ozaki if_schedule_deferred_start(&sc->sc_ec.ec_if);
1523 1.28 martin
1524 1.93 skrll mutex_exit(sc->sc_intr_lock);
1525 1.93 skrll
1526 1.8 martin return rv;
1527 1.1 martin }
1528 1.7 martin
1529 1.55 martin static void
1530 1.55 martin dwc_gmac_desc_set_owned_by_dev(struct dwc_gmac_dev_dmadesc *desc)
1531 1.55 martin {
1532 1.55 martin
1533 1.55 martin desc->ddesc_status0 |= htole32(DDESC_STATUS_OWNEDBYDEV);
1534 1.55 martin }
1535 1.55 martin
1536 1.55 martin static int
1537 1.55 martin dwc_gmac_desc_is_owned_by_dev(struct dwc_gmac_dev_dmadesc *desc)
1538 1.55 martin {
1539 1.55 martin
1540 1.55 martin return !!(le32toh(desc->ddesc_status0) & DDESC_STATUS_OWNEDBYDEV);
1541 1.55 martin }
1542 1.55 martin
1543 1.55 martin static void
1544 1.55 martin dwc_gmac_desc_std_set_len(struct dwc_gmac_dev_dmadesc *desc, int len)
1545 1.55 martin {
1546 1.55 martin uint32_t cntl = le32toh(desc->ddesc_cntl1);
1547 1.55 martin
1548 1.55 martin desc->ddesc_cntl1 = htole32((cntl & ~DDESC_CNTL_SIZE1MASK) |
1549 1.55 martin __SHIFTIN(len, DDESC_CNTL_SIZE1MASK));
1550 1.55 martin }
1551 1.55 martin
1552 1.55 martin static uint32_t
1553 1.55 martin dwc_gmac_desc_std_get_len(struct dwc_gmac_dev_dmadesc *desc)
1554 1.55 martin {
1555 1.55 martin
1556 1.55 martin return __SHIFTOUT(le32toh(desc->ddesc_status0), DDESC_STATUS_FRMLENMSK);
1557 1.55 martin }
1558 1.55 martin
1559 1.55 martin static void
1560 1.55 martin dwc_gmac_desc_std_tx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
1561 1.55 martin {
1562 1.55 martin
1563 1.55 martin desc->ddesc_status0 = 0;
1564 1.55 martin desc->ddesc_cntl1 = htole32(DDESC_CNTL_TXCHAIN);
1565 1.55 martin }
1566 1.55 martin
1567 1.55 martin static void
1568 1.55 martin dwc_gmac_desc_std_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *desc)
1569 1.55 martin {
1570 1.55 martin uint32_t cntl = le32toh(desc->ddesc_cntl1);
1571 1.55 martin
1572 1.55 martin desc->ddesc_cntl1 = htole32(cntl | DDESC_CNTL_TXFIRST);
1573 1.55 martin }
1574 1.55 martin
1575 1.55 martin static void
1576 1.55 martin dwc_gmac_desc_std_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *desc)
1577 1.55 martin {
1578 1.55 martin uint32_t cntl = le32toh(desc->ddesc_cntl1);
1579 1.55 martin
1580 1.55 martin desc->ddesc_cntl1 = htole32(cntl |
1581 1.55 martin DDESC_CNTL_TXLAST | DDESC_CNTL_TXINT);
1582 1.55 martin }
1583 1.55 martin
1584 1.55 martin static void
1585 1.55 martin dwc_gmac_desc_std_rx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
1586 1.55 martin {
1587 1.55 martin
1588 1.55 martin desc->ddesc_status0 = 0;
1589 1.55 martin desc->ddesc_cntl1 = htole32(DDESC_CNTL_TXCHAIN);
1590 1.55 martin }
1591 1.55 martin
1592 1.55 martin static int
1593 1.55 martin dwc_gmac_desc_std_rx_has_error(struct dwc_gmac_dev_dmadesc *desc) {
1594 1.55 martin return !!(le32toh(desc->ddesc_status0) &
1595 1.55 martin (DDESC_STATUS_RXERROR | DDESC_STATUS_RXTRUNCATED));
1596 1.55 martin }
1597 1.55 martin
1598 1.55 martin static void
1599 1.55 martin dwc_gmac_desc_enh_set_len(struct dwc_gmac_dev_dmadesc *desc, int len)
1600 1.55 martin {
1601 1.55 martin uint32_t tdes1 = le32toh(desc->ddesc_cntl1);
1602 1.55 martin
1603 1.55 martin desc->ddesc_cntl1 = htole32((tdes1 & ~DDESC_DES1_SIZE1MASK) |
1604 1.55 martin __SHIFTIN(len, DDESC_DES1_SIZE1MASK));
1605 1.55 martin }
1606 1.55 martin
1607 1.55 martin static uint32_t
1608 1.55 martin dwc_gmac_desc_enh_get_len(struct dwc_gmac_dev_dmadesc *desc)
1609 1.55 martin {
1610 1.55 martin
1611 1.55 martin return __SHIFTOUT(le32toh(desc->ddesc_status0), DDESC_RDES0_FL);
1612 1.55 martin }
1613 1.55 martin
1614 1.55 martin static void
1615 1.55 martin dwc_gmac_desc_enh_tx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
1616 1.55 martin {
1617 1.55 martin
1618 1.55 martin desc->ddesc_status0 = htole32(DDESC_TDES0_TCH);
1619 1.55 martin desc->ddesc_cntl1 = 0;
1620 1.55 martin }
1621 1.55 martin
1622 1.55 martin static void
1623 1.55 martin dwc_gmac_desc_enh_tx_set_first_frag(struct dwc_gmac_dev_dmadesc *desc)
1624 1.55 martin {
1625 1.55 martin uint32_t tdes0 = le32toh(desc->ddesc_status0);
1626 1.55 martin
1627 1.55 martin desc->ddesc_status0 = htole32(tdes0 | DDESC_TDES0_FS);
1628 1.55 martin }
1629 1.55 martin
1630 1.55 martin static void
1631 1.55 martin dwc_gmac_desc_enh_tx_set_last_frag(struct dwc_gmac_dev_dmadesc *desc)
1632 1.55 martin {
1633 1.55 martin uint32_t tdes0 = le32toh(desc->ddesc_status0);
1634 1.55 martin
1635 1.55 martin desc->ddesc_status0 = htole32(tdes0 | DDESC_TDES0_LS | DDESC_TDES0_IC);
1636 1.55 martin }
1637 1.55 martin
1638 1.55 martin static void
1639 1.55 martin dwc_gmac_desc_enh_rx_init_flags(struct dwc_gmac_dev_dmadesc *desc)
1640 1.55 martin {
1641 1.55 martin
1642 1.55 martin desc->ddesc_status0 = 0;
1643 1.55 martin desc->ddesc_cntl1 = htole32(DDESC_RDES1_RCH);
1644 1.55 martin }
1645 1.55 martin
1646 1.55 martin static int
1647 1.55 martin dwc_gmac_desc_enh_rx_has_error(struct dwc_gmac_dev_dmadesc *desc)
1648 1.55 martin {
1649 1.55 martin
1650 1.55 martin return !!(le32toh(desc->ddesc_status0) &
1651 1.55 martin (DDESC_RDES0_ES | DDESC_RDES0_LE));
1652 1.55 martin }
1653 1.55 martin
1654 1.7 martin #ifdef DWC_GMAC_DEBUG
1655 1.7 martin static void
1656 1.7 martin dwc_gmac_dump_dma(struct dwc_gmac_softc *sc)
1657 1.7 martin {
1658 1.7 martin aprint_normal_dev(sc->sc_dev, "busmode: %08x\n",
1659 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_BUSMODE));
1660 1.7 martin aprint_normal_dev(sc->sc_dev, "tx poll: %08x\n",
1661 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TXPOLL));
1662 1.7 martin aprint_normal_dev(sc->sc_dev, "rx poll: %08x\n",
1663 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RXPOLL));
1664 1.7 martin aprint_normal_dev(sc->sc_dev, "rx descriptors: %08x\n",
1665 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_RX_ADDR));
1666 1.7 martin aprint_normal_dev(sc->sc_dev, "tx descriptors: %08x\n",
1667 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_TX_ADDR));
1668 1.90 skrll aprint_normal_dev(sc->sc_dev, " status: %08x\n",
1669 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_STATUS));
1670 1.7 martin aprint_normal_dev(sc->sc_dev, "op mode: %08x\n",
1671 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_OPMODE));
1672 1.90 skrll aprint_normal_dev(sc->sc_dev, "int en.: %08x\n",
1673 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_INTENABLE));
1674 1.90 skrll aprint_normal_dev(sc->sc_dev, " cur tx: %08x\n",
1675 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_TX_DESC));
1676 1.90 skrll aprint_normal_dev(sc->sc_dev, " cur rx: %08x\n",
1677 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_RX_DESC));
1678 1.90 skrll aprint_normal_dev(sc->sc_dev, "cur txb: %08x\n",
1679 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_TX_BUFADDR));
1680 1.90 skrll aprint_normal_dev(sc->sc_dev, "cur rxb: %08x\n",
1681 1.7 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_DMA_CUR_RX_BUFADDR));
1682 1.7 martin }
1683 1.7 martin
1684 1.7 martin static void
1685 1.7 martin dwc_gmac_dump_tx_desc(struct dwc_gmac_softc *sc)
1686 1.7 martin {
1687 1.89 skrll const size_t descsz = sizeof(struct dwc_gmac_dev_dmadesc);
1688 1.7 martin
1689 1.8 martin aprint_normal_dev(sc->sc_dev, "TX queue: cur=%d, next=%d, queued=%d\n",
1690 1.8 martin sc->sc_txq.t_cur, sc->sc_txq.t_next, sc->sc_txq.t_queued);
1691 1.8 martin aprint_normal_dev(sc->sc_dev, "TX DMA descriptors:\n");
1692 1.89 skrll
1693 1.89 skrll bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
1694 1.89 skrll TX_DESC_OFFSET(0), AWGE_TX_RING_COUNT * descsz,
1695 1.89 skrll BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1696 1.89 skrll
1697 1.90 skrll for (size_t i = 0; i < AWGE_TX_RING_COUNT; i++) {
1698 1.7 martin struct dwc_gmac_dev_dmadesc *desc = &sc->sc_txq.t_desc[i];
1699 1.90 skrll aprint_normal("#%3zu (%08lx): status: %08x cntl: %08x "
1700 1.15 martin "data: %08x next: %08x\n",
1701 1.90 skrll i, sc->sc_txq.t_physaddr + i * descsz,
1702 1.55 martin le32toh(desc->ddesc_status0), le32toh(desc->ddesc_cntl1),
1703 1.7 martin le32toh(desc->ddesc_data), le32toh(desc->ddesc_next));
1704 1.7 martin }
1705 1.7 martin }
1706 1.8 martin
1707 1.8 martin static void
1708 1.11 martin dwc_gmac_dump_rx_desc(struct dwc_gmac_softc *sc)
1709 1.11 martin {
1710 1.89 skrll const size_t descsz = sizeof(struct dwc_gmac_dev_dmadesc);
1711 1.11 martin
1712 1.11 martin aprint_normal_dev(sc->sc_dev, "RX queue: cur=%d, next=%d\n",
1713 1.11 martin sc->sc_rxq.r_cur, sc->sc_rxq.r_next);
1714 1.11 martin aprint_normal_dev(sc->sc_dev, "RX DMA descriptors:\n");
1715 1.89 skrll
1716 1.89 skrll bus_dmamap_sync(sc->sc_dmat, sc->sc_dma_ring_map,
1717 1.89 skrll RX_DESC_OFFSET(0), AWGE_RX_RING_COUNT * descsz,
1718 1.89 skrll BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1719 1.89 skrll
1720 1.90 skrll for (size_t i = 0; i < AWGE_RX_RING_COUNT; i++) {
1721 1.11 martin struct dwc_gmac_dev_dmadesc *desc = &sc->sc_rxq.r_desc[i];
1722 1.90 skrll char buf[200];
1723 1.90 skrll
1724 1.90 skrll if (!sc->sc_descm->rx_is_owned_by_dev(desc)) {
1725 1.90 skrll /* print interrupt state */
1726 1.90 skrll snprintb(buf, sizeof(buf),
1727 1.90 skrll "\177\20"
1728 1.90 skrll "b\x1e" "daff\0"
1729 1.90 skrll "f\x10\xe" "frlen\0"
1730 1.90 skrll "b\x0f" "error\0"
1731 1.90 skrll "b\x0e" "rxtrunc\0" /* descriptor error? */
1732 1.90 skrll "b\x0d" "saff\0"
1733 1.90 skrll "b\x0c" "giantframe\0" /* length error? */
1734 1.90 skrll "b\x0b" "damaged\0"
1735 1.90 skrll "b\x0a" "vlan\0"
1736 1.90 skrll "b\x09" "first\0"
1737 1.90 skrll "b\x08" "last\0"
1738 1.90 skrll "b\x07" "giant\0"
1739 1.90 skrll "b\x06" "collison\0"
1740 1.90 skrll "b\x05" "ether\0"
1741 1.90 skrll "b\x04" "watchdog\0"
1742 1.90 skrll "b\x03" "miierror\0"
1743 1.90 skrll "b\x02" "dribbling\0"
1744 1.90 skrll "b\x01" "crc\0"
1745 1.90 skrll "\0", le32toh(desc->ddesc_status0));
1746 1.90 skrll }
1747 1.90 skrll
1748 1.90 skrll aprint_normal("#%3zu (%08lx): status: %08x cntl: %08x "
1749 1.90 skrll "data: %08x next: %08x %s\n",
1750 1.90 skrll i, sc->sc_rxq.r_physaddr + i * descsz,
1751 1.55 martin le32toh(desc->ddesc_status0), le32toh(desc->ddesc_cntl1),
1752 1.90 skrll le32toh(desc->ddesc_data), le32toh(desc->ddesc_next),
1753 1.90 skrll sc->sc_descm->rx_is_owned_by_dev(desc) ? "" : buf);
1754 1.11 martin }
1755 1.11 martin }
1756 1.11 martin
1757 1.11 martin static void
1758 1.10 martin dwc_dump_status(struct dwc_gmac_softc *sc)
1759 1.8 martin {
1760 1.8 martin uint32_t status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1761 1.87 skrll AWIN_GMAC_MAC_INTR);
1762 1.8 martin uint32_t dma_status = bus_space_read_4(sc->sc_bst, sc->sc_bsh,
1763 1.87 skrll AWIN_GMAC_DMA_STATUS);
1764 1.8 martin char buf[200];
1765 1.8 martin
1766 1.8 martin /* print interrupt state */
1767 1.90 skrll snprintb(buf, sizeof(buf),
1768 1.90 skrll "\177\20"
1769 1.90 skrll "b\x1c" "GPI\0"
1770 1.90 skrll "b\x1b" "GMC\0"
1771 1.90 skrll "b\x1a" "GLI\0"
1772 1.90 skrll "f\x17\x3" "EB\0"
1773 1.90 skrll "f\x14\x3" "TPS\0"
1774 1.90 skrll "f\x11\x3" "RPS\0"
1775 1.90 skrll "b\x10" "NI\0"
1776 1.90 skrll "b\x0f" "AI\0"
1777 1.90 skrll "b\x0e" "ER\0"
1778 1.90 skrll "b\x0d" "FB\0"
1779 1.90 skrll "b\x0a" "ET\0"
1780 1.90 skrll "b\x09" "RW\0"
1781 1.90 skrll "b\x08" "RS\0"
1782 1.90 skrll "b\x07" "RU\0"
1783 1.90 skrll "b\x06" "RI\0"
1784 1.90 skrll "b\x05" "UN\0"
1785 1.90 skrll "b\x04" "OV\0"
1786 1.90 skrll "b\x03" "TJ\0"
1787 1.90 skrll "b\x02" "TU\0"
1788 1.90 skrll "b\x01" "TS\0"
1789 1.90 skrll "b\x00" "TI\0"
1790 1.8 martin "\0", dma_status);
1791 1.10 martin aprint_normal_dev(sc->sc_dev, "INTR status: %08x, DMA status: %s\n",
1792 1.8 martin status, buf);
1793 1.10 martin }
1794 1.8 martin
1795 1.10 martin static void
1796 1.10 martin dwc_dump_and_abort(struct dwc_gmac_softc *sc, const char *msg)
1797 1.10 martin {
1798 1.10 martin dwc_dump_status(sc);
1799 1.22 martin dwc_gmac_dump_ffilt(sc,
1800 1.22 martin bus_space_read_4(sc->sc_bst, sc->sc_bsh, AWIN_GMAC_MAC_FFILT));
1801 1.8 martin dwc_gmac_dump_dma(sc);
1802 1.8 martin dwc_gmac_dump_tx_desc(sc);
1803 1.11 martin dwc_gmac_dump_rx_desc(sc);
1804 1.8 martin
1805 1.21 joerg panic("%s", msg);
1806 1.8 martin }
1807 1.22 martin
1808 1.22 martin static void dwc_gmac_dump_ffilt(struct dwc_gmac_softc *sc, uint32_t ffilt)
1809 1.22 martin {
1810 1.22 martin char buf[200];
1811 1.22 martin
1812 1.22 martin /* print filter setup */
1813 1.22 martin snprintb(buf, sizeof(buf), "\177\20"
1814 1.22 martin "b\x1f""RA\0"
1815 1.22 martin "b\x0a""HPF\0"
1816 1.22 martin "b\x09""SAF\0"
1817 1.22 martin "b\x08""SAIF\0"
1818 1.22 martin "b\x05""DBF\0"
1819 1.22 martin "b\x04""PM\0"
1820 1.22 martin "b\x03""DAIF\0"
1821 1.22 martin "b\x02""HMC\0"
1822 1.22 martin "b\x01""HUC\0"
1823 1.22 martin "b\x00""PR\0"
1824 1.22 martin "\0", ffilt);
1825 1.22 martin aprint_normal_dev(sc->sc_dev, "FFILT: %s\n", buf);
1826 1.22 martin }
1827 1.7 martin #endif
1828