if_cemac.c revision 1.46 1 1.46 thorpej /* $NetBSD: if_cemac.c,v 1.46 2025/10/12 23:30:13 thorpej Exp $ */
2 1.1 hkenken
3 1.1 hkenken /*
4 1.1 hkenken * Copyright (c) 2015 Genetec Corporation. All rights reserved.
5 1.1 hkenken * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1 hkenken *
7 1.1 hkenken * Based on arch/arm/at91/at91emac.c
8 1.1 hkenken *
9 1.1 hkenken * Copyright (c) 2007 Embedtronics Oy
10 1.1 hkenken * All rights reserved.
11 1.1 hkenken *
12 1.1 hkenken * Copyright (c) 2004 Jesse Off
13 1.1 hkenken * All rights reserved.
14 1.1 hkenken *
15 1.1 hkenken * Redistribution and use in source and binary forms, with or without
16 1.1 hkenken * modification, are permitted provided that the following conditions
17 1.1 hkenken * are met:
18 1.1 hkenken * 1. Redistributions of source code must retain the above copyright
19 1.1 hkenken * notice, this list of conditions and the following disclaimer.
20 1.1 hkenken * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 hkenken * notice, this list of conditions and the following disclaimer in the
22 1.1 hkenken * documentation and/or other materials provided with the distribution.
23 1.1 hkenken *
24 1.1 hkenken * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.1 hkenken * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.1 hkenken * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.1 hkenken * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.1 hkenken * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1 hkenken * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1 hkenken * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.1 hkenken * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.1 hkenken * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.1 hkenken * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.1 hkenken * POSSIBILITY OF SUCH DAMAGE.
35 1.1 hkenken */
36 1.1 hkenken
37 1.1 hkenken /*
38 1.1 hkenken * Cadence EMAC/GEM ethernet controller IP driver
39 1.1 hkenken * used by arm/at91, arm/zynq SoC
40 1.1 hkenken */
41 1.1 hkenken
42 1.43 skrll /*
43 1.43 skrll * Lock order:
44 1.43 skrll *
45 1.43 skrll * IFNET_LOCK -> sc_mcast_lock
46 1.43 skrll * IFNET_LOCK -> sc_intr_lock
47 1.43 skrll */
48 1.43 skrll
49 1.43 skrll
50 1.1 hkenken #include <sys/cdefs.h>
51 1.46 thorpej __KERNEL_RCSID(0, "$NetBSD: if_cemac.c,v 1.46 2025/10/12 23:30:13 thorpej Exp $");
52 1.1 hkenken
53 1.32 skrll #include <sys/param.h>
54 1.1 hkenken #include <sys/types.h>
55 1.32 skrll
56 1.32 skrll #include <sys/bus.h>
57 1.32 skrll #include <sys/device.h>
58 1.1 hkenken #include <sys/kernel.h>
59 1.1 hkenken #include <sys/proc.h>
60 1.32 skrll #include <sys/systm.h>
61 1.1 hkenken #include <sys/time.h>
62 1.1 hkenken
63 1.1 hkenken #include <net/if.h>
64 1.1 hkenken #include <net/if_dl.h>
65 1.1 hkenken #include <net/if_types.h>
66 1.1 hkenken #include <net/if_media.h>
67 1.1 hkenken #include <net/if_ether.h>
68 1.12 msaitoh #include <net/bpf.h>
69 1.1 hkenken
70 1.1 hkenken #include <dev/mii/mii.h>
71 1.1 hkenken #include <dev/mii/miivar.h>
72 1.1 hkenken
73 1.1 hkenken #ifdef INET
74 1.1 hkenken #include <netinet/in.h>
75 1.1 hkenken #include <netinet/in_systm.h>
76 1.1 hkenken #include <netinet/in_var.h>
77 1.1 hkenken #include <netinet/ip.h>
78 1.1 hkenken #include <netinet/if_inarp.h>
79 1.1 hkenken #endif
80 1.1 hkenken
81 1.1 hkenken #include <dev/cadence/cemacreg.h>
82 1.1 hkenken #include <dev/cadence/if_cemacvar.h>
83 1.1 hkenken
84 1.43 skrll #ifndef CEMAC_WATCHDOG_TIMEOUT
85 1.43 skrll #define CEMAC_WATCHDOG_TIMEOUT 5
86 1.43 skrll #endif
87 1.43 skrll static int cemac_watchdog_timeout = CEMAC_WATCHDOG_TIMEOUT;
88 1.43 skrll
89 1.1 hkenken #define DEFAULT_MDCDIV 32
90 1.1 hkenken
91 1.1 hkenken #define CEMAC_READ(x) \
92 1.1 hkenken bus_space_read_4(sc->sc_iot, sc->sc_ioh, (x))
93 1.1 hkenken #define CEMAC_WRITE(x, y) \
94 1.1 hkenken bus_space_write_4(sc->sc_iot, sc->sc_ioh, (x), (y))
95 1.1 hkenken #define CEMAC_GEM_WRITE(x, y) \
96 1.39 skrll do { \
97 1.39 skrll if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) \
98 1.39 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, (GEM_##x), (y)); \
99 1.39 skrll else \
100 1.39 skrll bus_space_write_4(sc->sc_iot, sc->sc_ioh, (ETH_##x), (y)); \
101 1.39 skrll } while(0)
102 1.1 hkenken
103 1.1 hkenken static void cemac_init(struct cemac_softc *);
104 1.1 hkenken static int cemac_gctx(struct cemac_softc *);
105 1.1 hkenken static int cemac_mediachange(struct ifnet *);
106 1.1 hkenken static void cemac_mediastatus(struct ifnet *, struct ifmediareq *);
107 1.15 msaitoh static int cemac_mii_readreg(device_t, int, int, uint16_t *);
108 1.15 msaitoh static int cemac_mii_writereg(device_t, int, int, uint16_t);
109 1.1 hkenken static void cemac_statchg(struct ifnet *);
110 1.1 hkenken static void cemac_tick(void *);
111 1.1 hkenken static int cemac_ifioctl(struct ifnet *, u_long, void *);
112 1.1 hkenken static void cemac_ifstart(struct ifnet *);
113 1.43 skrll static void cemac_ifstart_locked(struct ifnet *);
114 1.1 hkenken static void cemac_ifwatchdog(struct ifnet *);
115 1.1 hkenken static int cemac_ifinit(struct ifnet *);
116 1.1 hkenken static void cemac_ifstop(struct ifnet *, int);
117 1.1 hkenken static void cemac_setaddr(struct ifnet *);
118 1.1 hkenken
119 1.1 hkenken #ifdef CEMAC_DEBUG
120 1.1 hkenken int cemac_debug = CEMAC_DEBUG;
121 1.19 msaitoh #define DPRINTFN(n, fmt) if (cemac_debug >= (n)) printf fmt
122 1.1 hkenken #else
123 1.19 msaitoh #define DPRINTFN(n, fmt)
124 1.1 hkenken #endif
125 1.1 hkenken
126 1.43 skrll /*
127 1.43 skrll * Perform an interface watchdog reset.
128 1.43 skrll */
129 1.43 skrll static void
130 1.43 skrll cemac_handle_reset_work(struct work *work, void *arg)
131 1.43 skrll {
132 1.43 skrll struct cemac_softc * const sc = arg;
133 1.43 skrll struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
134 1.43 skrll
135 1.43 skrll printf("%s: watchdog timeout -- resetting\n", ifp->if_xname);
136 1.43 skrll
137 1.43 skrll /* Don't want ioctl operations to happen */
138 1.43 skrll IFNET_LOCK(ifp);
139 1.43 skrll
140 1.43 skrll /* reset the interface. */
141 1.43 skrll cemac_ifinit(ifp);
142 1.43 skrll
143 1.43 skrll IFNET_UNLOCK(ifp);
144 1.43 skrll
145 1.43 skrll /*
146 1.43 skrll * There are still some upper layer processing which call
147 1.43 skrll * ifp->if_start(). e.g. ALTQ or one CPU system
148 1.43 skrll */
149 1.43 skrll /* Try to get more packets going. */
150 1.43 skrll ifp->if_start(ifp);
151 1.43 skrll
152 1.43 skrll atomic_store_relaxed(&sc->sc_reset_pending, 0);
153 1.43 skrll }
154 1.43 skrll
155 1.43 skrll
156 1.1 hkenken void
157 1.33 skrll cemac_attach_common(struct cemac_softc *sc)
158 1.1 hkenken {
159 1.33 skrll uint32_t u;
160 1.1 hkenken
161 1.1 hkenken aprint_naive("\n");
162 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
163 1.1 hkenken aprint_normal(": Cadence Gigabit Ethernet Controller\n");
164 1.1 hkenken else
165 1.1 hkenken aprint_normal(": Cadence Ethernet Controller\n");
166 1.1 hkenken
167 1.1 hkenken /* configure emac: */
168 1.1 hkenken CEMAC_WRITE(ETH_CTL, 0); // disable everything
169 1.1 hkenken CEMAC_WRITE(ETH_IDR, -1); // disable interrupts
170 1.1 hkenken CEMAC_WRITE(ETH_RBQP, 0); // clear receive
171 1.1 hkenken CEMAC_WRITE(ETH_TBQP, 0); // clear transmit
172 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
173 1.1 hkenken CEMAC_WRITE(ETH_CFG,
174 1.1 hkenken GEM_CFG_CLK_64 | GEM_CFG_GEN | ETH_CFG_SPD | ETH_CFG_FD);
175 1.1 hkenken else
176 1.1 hkenken CEMAC_WRITE(ETH_CFG,
177 1.1 hkenken ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
178 1.1 hkenken //CEMAC_WRITE(ETH_TCR, 0); // send nothing
179 1.1 hkenken //(void)CEMAC_READ(ETH_ISR);
180 1.1 hkenken u = CEMAC_READ(ETH_TSR);
181 1.1 hkenken CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
182 1.1 hkenken | ETH_TSR_IDLE | ETH_TSR_RLE
183 1.19 msaitoh | ETH_TSR_COL | ETH_TSR_OVR)));
184 1.1 hkenken u = CEMAC_READ(ETH_RSR);
185 1.19 msaitoh CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
186 1.1 hkenken
187 1.1 hkenken /* Fetch the Ethernet address from property if set. */
188 1.46 thorpej if (! ether_getaddr(sc->sc_dev, sc->sc_enaddr)) {
189 1.1 hkenken static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
190 1.1 hkenken 0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
191 1.1 hkenken };
192 1.1 hkenken memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
193 1.1 hkenken }
194 1.1 hkenken
195 1.1 hkenken cemac_init(sc);
196 1.1 hkenken }
197 1.1 hkenken
198 1.1 hkenken static int
199 1.1 hkenken cemac_gctx(struct cemac_softc *sc)
200 1.1 hkenken {
201 1.1 hkenken uint32_t tsr;
202 1.1 hkenken
203 1.1 hkenken tsr = CEMAC_READ(ETH_TSR);
204 1.1 hkenken if (!ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
205 1.1 hkenken // no space left
206 1.1 hkenken if (!(tsr & ETH_TSR_BNQ))
207 1.1 hkenken return 0;
208 1.1 hkenken } else {
209 1.1 hkenken if (tsr & GEM_TSR_TXGO)
210 1.1 hkenken return 0;
211 1.1 hkenken }
212 1.1 hkenken CEMAC_WRITE(ETH_TSR, tsr);
213 1.1 hkenken
214 1.1 hkenken // free sent frames
215 1.1 hkenken while (sc->txqc > (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM) ? 0 :
216 1.1 hkenken (tsr & ETH_TSR_IDLE ? 0 : 1))) {
217 1.1 hkenken int bi = sc->txqi % TX_QLEN;
218 1.1 hkenken
219 1.1 hkenken DPRINTFN(3,("%s: TDSC[%i].Addr 0x%08x\n",
220 1.1 hkenken __FUNCTION__, bi, sc->TDSC[bi].Addr));
221 1.1 hkenken DPRINTFN(3,("%s: TDSC[%i].Info 0x%08x\n",
222 1.1 hkenken __FUNCTION__, bi, sc->TDSC[bi].Info));
223 1.1 hkenken
224 1.1 hkenken bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
225 1.1 hkenken sc->txq[bi].m->m_pkthdr.len, BUS_DMASYNC_POSTWRITE);
226 1.1 hkenken bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
227 1.1 hkenken m_freem(sc->txq[bi].m);
228 1.1 hkenken DPRINTFN(2,("%s: freed idx #%i mbuf %p (txqc=%i)\n",
229 1.1 hkenken __FUNCTION__, bi, sc->txq[bi].m, sc->txqc));
230 1.1 hkenken sc->txq[bi].m = NULL;
231 1.1 hkenken sc->txqi = (bi + 1) % TX_QLEN;
232 1.1 hkenken sc->txqc--;
233 1.1 hkenken }
234 1.1 hkenken
235 1.1 hkenken // mark we're free
236 1.42 skrll if (sc->sc_txbusy) {
237 1.42 skrll sc->sc_txbusy = false;
238 1.1 hkenken /* Disable transmit-buffer-free interrupt */
239 1.1 hkenken /*CEMAC_WRITE(ETH_IDR, ETH_ISR_TBRE);*/
240 1.1 hkenken }
241 1.1 hkenken
242 1.1 hkenken return 1;
243 1.1 hkenken }
244 1.1 hkenken
245 1.1 hkenken int
246 1.1 hkenken cemac_intr(void *arg)
247 1.1 hkenken {
248 1.35 skrll struct cemac_softc * const sc = arg;
249 1.41 skrll struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
250 1.1 hkenken uint32_t imr, isr, ctl;
251 1.1 hkenken #ifdef CEMAC_DEBUG
252 1.1 hkenken uint32_t rsr;
253 1.1 hkenken #endif
254 1.1 hkenken int bi;
255 1.1 hkenken
256 1.43 skrll mutex_enter(sc->sc_intr_lock);
257 1.43 skrll if (sc->sc_stopping) {
258 1.43 skrll mutex_exit(sc->sc_intr_lock);
259 1.43 skrll return 0;
260 1.43 skrll }
261 1.43 skrll
262 1.1 hkenken imr = ~CEMAC_READ(ETH_IMR);
263 1.19 msaitoh if (!(imr & (ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE |
264 1.19 msaitoh ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM))) {
265 1.1 hkenken // interrupt not enabled, can't be us
266 1.43 skrll mutex_exit(sc->sc_intr_lock);
267 1.1 hkenken return 0;
268 1.1 hkenken }
269 1.1 hkenken
270 1.1 hkenken isr = CEMAC_READ(ETH_ISR);
271 1.1 hkenken CEMAC_WRITE(ETH_ISR, isr);
272 1.1 hkenken isr &= imr;
273 1.43 skrll
274 1.43 skrll if (isr == 0) {
275 1.43 skrll mutex_exit(sc->sc_intr_lock);
276 1.43 skrll return 0;
277 1.43 skrll }
278 1.43 skrll
279 1.1 hkenken #ifdef CEMAC_DEBUG
280 1.1 hkenken rsr = CEMAC_READ(ETH_RSR); // get receive status register
281 1.1 hkenken #endif
282 1.39 skrll DPRINTFN(2, ("%s: isr=0x%08X rsr=0x%08X imr=0x%08X\n", __FUNCTION__,
283 1.39 skrll isr, rsr, imr));
284 1.1 hkenken
285 1.22 thorpej net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
286 1.39 skrll // out of receive buffers
287 1.39 skrll if (isr & ETH_ISR_RBNA) {
288 1.39 skrll // clear interrupt
289 1.39 skrll CEMAC_WRITE(ETH_RSR, ETH_RSR_BNA);
290 1.39 skrll
291 1.39 skrll ctl = CEMAC_READ(ETH_CTL);
292 1.39 skrll // disable receiver
293 1.39 skrll CEMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
294 1.39 skrll // clear BNA bit
295 1.39 skrll CEMAC_WRITE(ETH_RSR, ETH_RSR_BNA);
296 1.39 skrll // re-enable receiver
297 1.39 skrll CEMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
298 1.39 skrll
299 1.27 riastrad if_statinc_ref(ifp, nsr, if_ierrors);
300 1.27 riastrad if_statinc_ref(ifp, nsr, if_ipackets);
301 1.1 hkenken DPRINTFN(1,("%s: out of receive buffers\n", __FUNCTION__));
302 1.1 hkenken }
303 1.1 hkenken if (isr & ETH_ISR_ROVR) {
304 1.39 skrll // clear interrupt
305 1.39 skrll CEMAC_WRITE(ETH_RSR, ETH_RSR_OVR);
306 1.27 riastrad if_statinc_ref(ifp, nsr, if_ierrors);
307 1.27 riastrad if_statinc_ref(ifp, nsr, if_ipackets);
308 1.1 hkenken DPRINTFN(1,("%s: receive overrun\n", __FUNCTION__));
309 1.1 hkenken }
310 1.1 hkenken
311 1.39 skrll // packet has been received!
312 1.39 skrll if (isr & ETH_ISR_RCOM) {
313 1.1 hkenken uint32_t nfo;
314 1.39 skrll DPRINTFN(2,("#2 RDSC[%i].INFO=0x%08X\n", sc->rxqi % RX_QLEN,
315 1.39 skrll sc->RDSC[sc->rxqi % RX_QLEN].Info));
316 1.1 hkenken while (sc->RDSC[(bi = sc->rxqi % RX_QLEN)].Addr & ETH_RDSC_F_USED) {
317 1.7 rjs int fl, csum;
318 1.1 hkenken struct mbuf *m;
319 1.1 hkenken
320 1.1 hkenken nfo = sc->RDSC[bi].Info;
321 1.20 msaitoh fl = (nfo & ETH_RDSC_I_LEN) - 4;
322 1.1 hkenken DPRINTFN(2,("## nfo=0x%08X\n", nfo));
323 1.1 hkenken
324 1.1 hkenken MGETHDR(m, M_DONTWAIT, MT_DATA);
325 1.34 skrll if (m != NULL)
326 1.38 skrll MCLGET(m, M_DONTWAIT);
327 1.1 hkenken if (m != NULL && (m->m_flags & M_EXT)) {
328 1.39 skrll bus_dmamap_sync(sc->sc_dmat,
329 1.39 skrll sc->rxq[bi].m_dmamap, 0, MCLBYTES,
330 1.39 skrll BUS_DMASYNC_POSTREAD);
331 1.1 hkenken bus_dmamap_unload(sc->sc_dmat,
332 1.1 hkenken sc->rxq[bi].m_dmamap);
333 1.9 ozaki m_set_rcvif(sc->rxq[bi].m, ifp);
334 1.1 hkenken sc->rxq[bi].m->m_pkthdr.len =
335 1.1 hkenken sc->rxq[bi].m->m_len = fl;
336 1.7 rjs switch (nfo & ETH_RDSC_I_CHKSUM) {
337 1.7 rjs case ETH_RDSC_I_CHKSUM_IP:
338 1.7 rjs csum = M_CSUM_IPv4;
339 1.7 rjs break;
340 1.7 rjs case ETH_RDSC_I_CHKSUM_UDP:
341 1.7 rjs csum = M_CSUM_IPv4 | M_CSUM_UDPv4 |
342 1.7 rjs M_CSUM_UDPv6;
343 1.7 rjs break;
344 1.7 rjs case ETH_RDSC_I_CHKSUM_TCP:
345 1.7 rjs csum = M_CSUM_IPv4 | M_CSUM_TCPv4 |
346 1.7 rjs M_CSUM_TCPv6;
347 1.7 rjs break;
348 1.7 rjs default:
349 1.7 rjs csum = 0;
350 1.7 rjs break;
351 1.7 rjs }
352 1.7 rjs sc->rxq[bi].m->m_pkthdr.csum_flags = csum;
353 1.1 hkenken DPRINTFN(2,("received %u bytes packet\n", fl));
354 1.20 msaitoh if_percpuq_enqueue(ifp->if_percpuq,
355 1.8 ozaki sc->rxq[bi].m);
356 1.1 hkenken if (mtod(m, intptr_t) & 3)
357 1.1 hkenken m_adj(m, mtod(m, intptr_t) & 3);
358 1.1 hkenken sc->rxq[bi].m = m;
359 1.1 hkenken bus_dmamap_load(sc->sc_dmat,
360 1.39 skrll sc->rxq[bi].m_dmamap, m->m_ext.ext_buf,
361 1.39 skrll MCLBYTES, NULL, BUS_DMA_NOWAIT);
362 1.39 skrll bus_dmamap_sync(sc->sc_dmat,
363 1.39 skrll sc->rxq[bi].m_dmamap, 0, MCLBYTES,
364 1.39 skrll BUS_DMASYNC_PREREAD);
365 1.1 hkenken sc->RDSC[bi].Info = 0;
366 1.1 hkenken sc->RDSC[bi].Addr =
367 1.39 skrll sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr
368 1.39 skrll | (bi == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
369 1.1 hkenken } else {
370 1.1 hkenken /* Drop packets until we can get replacement
371 1.1 hkenken * empty mbufs for the RXDQ.
372 1.1 hkenken */
373 1.28 rin m_freem(m);
374 1.27 riastrad if_statinc_ref(ifp, nsr, if_ierrors);
375 1.1 hkenken }
376 1.1 hkenken sc->rxqi++;
377 1.1 hkenken }
378 1.1 hkenken }
379 1.1 hkenken
380 1.22 thorpej IF_STAT_PUTREF(ifp);
381 1.22 thorpej
382 1.11 ozaki if (cemac_gctx(sc) > 0)
383 1.11 ozaki if_schedule_deferred_start(ifp);
384 1.1 hkenken #if 0 // reloop
385 1.1 hkenken irq = CEMAC_READ(IntStsC);
386 1.19 msaitoh if ((irq & (IntSts_RxSQ | IntSts_ECI)) != 0)
387 1.1 hkenken goto begin;
388 1.1 hkenken #endif
389 1.1 hkenken
390 1.43 skrll mutex_exit(sc->sc_intr_lock);
391 1.43 skrll
392 1.29 skrll return 1;
393 1.1 hkenken }
394 1.1 hkenken
395 1.1 hkenken
396 1.43 skrll static int
397 1.43 skrll cemac_ifflags_cb(struct ethercom *ec)
398 1.43 skrll {
399 1.43 skrll struct ifnet * const ifp = &ec->ec_if;
400 1.43 skrll struct cemac_softc * const sc = ifp->if_softc;
401 1.43 skrll int ret = 0;
402 1.43 skrll
403 1.43 skrll KASSERT(IFNET_LOCKED(ifp));
404 1.43 skrll mutex_enter(sc->sc_mcast_lock);
405 1.43 skrll
406 1.43 skrll u_short change = ifp->if_flags ^ sc->sc_if_flags;
407 1.43 skrll sc->sc_if_flags = ifp->if_flags;
408 1.43 skrll
409 1.43 skrll if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
410 1.43 skrll ret = ENETRESET;
411 1.43 skrll } else if ((change & IFF_PROMISC) != 0) {
412 1.43 skrll if ((sc->sc_if_flags & IFF_RUNNING) != 0)
413 1.43 skrll cemac_setaddr(ifp);
414 1.43 skrll }
415 1.43 skrll mutex_exit(sc->sc_mcast_lock);
416 1.43 skrll
417 1.43 skrll return ret;
418 1.43 skrll }
419 1.43 skrll
420 1.1 hkenken static void
421 1.1 hkenken cemac_init(struct cemac_softc *sc)
422 1.1 hkenken {
423 1.1 hkenken bus_dma_segment_t segs;
424 1.1 hkenken int rsegs, err, i;
425 1.41 skrll struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
426 1.19 msaitoh struct mii_data * const mii = &sc->sc_mii;
427 1.1 hkenken uint32_t u;
428 1.1 hkenken #if 0
429 1.1 hkenken int mdcdiv = DEFAULT_MDCDIV;
430 1.1 hkenken #endif
431 1.1 hkenken
432 1.43 skrll callout_init(&sc->cemac_tick_ch, CALLOUT_MPSAFE);
433 1.43 skrll callout_setfunc(&sc->cemac_tick_ch, cemac_tick, sc);
434 1.1 hkenken
435 1.1 hkenken // ok...
436 1.1 hkenken CEMAC_WRITE(ETH_CTL, ETH_CTL_MPE); // disable everything
437 1.1 hkenken CEMAC_WRITE(ETH_IDR, -1); // disable interrupts
438 1.1 hkenken CEMAC_WRITE(ETH_RBQP, 0); // clear receive
439 1.1 hkenken CEMAC_WRITE(ETH_TBQP, 0); // clear transmit
440 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
441 1.1 hkenken CEMAC_WRITE(ETH_CFG,
442 1.1 hkenken GEM_CFG_CLK_64 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
443 1.1 hkenken else
444 1.1 hkenken CEMAC_WRITE(ETH_CFG,
445 1.1 hkenken ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
446 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
447 1.1 hkenken CEMAC_WRITE(GEM_DMA_CFG,
448 1.1 hkenken __SHIFTIN((MCLBYTES + 63) / 64, GEM_DMA_CFG_RX_BUF_SIZE) |
449 1.1 hkenken __SHIFTIN(3, GEM_DMA_CFG_RX_PKTBUF_MEMSZ_SEL) |
450 1.1 hkenken GEM_DMA_CFG_TX_PKTBUF_MEMSZ_SEL |
451 1.1 hkenken __SHIFTIN(16, GEM_DMA_CFG_AHB_FIXED_BURST_LEN) |
452 1.1 hkenken GEM_DMA_CFG_DISC_WHEN_NO_AHB);
453 1.1 hkenken }
454 1.1 hkenken // CEMAC_WRITE(ETH_TCR, 0); // send nothing
455 1.1 hkenken // (void)CEMAC_READ(ETH_ISR);
456 1.1 hkenken u = CEMAC_READ(ETH_TSR);
457 1.1 hkenken CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
458 1.1 hkenken | ETH_TSR_IDLE | ETH_TSR_RLE
459 1.19 msaitoh | ETH_TSR_COL | ETH_TSR_OVR)));
460 1.1 hkenken u = CEMAC_READ(ETH_RSR);
461 1.19 msaitoh CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
462 1.1 hkenken
463 1.1 hkenken #if 0
464 1.1 hkenken if (device_cfdata(sc->sc_dev)->cf_flags)
465 1.1 hkenken mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
466 1.1 hkenken #endif
467 1.1 hkenken /* set ethernet address */
468 1.1 hkenken CEMAC_GEM_WRITE(SA1L, (sc->sc_enaddr[3] << 24)
469 1.1 hkenken | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
470 1.1 hkenken | (sc->sc_enaddr[0]));
471 1.1 hkenken CEMAC_GEM_WRITE(SA1H, (sc->sc_enaddr[5] << 8)
472 1.1 hkenken | (sc->sc_enaddr[4]));
473 1.1 hkenken CEMAC_GEM_WRITE(SA2L, 0);
474 1.1 hkenken CEMAC_GEM_WRITE(SA2H, 0);
475 1.1 hkenken CEMAC_GEM_WRITE(SA3L, 0);
476 1.1 hkenken CEMAC_GEM_WRITE(SA3H, 0);
477 1.1 hkenken CEMAC_GEM_WRITE(SA4L, 0);
478 1.1 hkenken CEMAC_GEM_WRITE(SA4H, 0);
479 1.1 hkenken
480 1.43 skrll char wqname[MAXCOMLEN];
481 1.43 skrll snprintf(wqname, sizeof(wqname), "%sReset", device_xname(sc->sc_dev));
482 1.43 skrll int error = workqueue_create(&sc->sc_reset_wq, wqname,
483 1.43 skrll cemac_handle_reset_work, sc, PRI_NONE, IPL_SOFTCLOCK,
484 1.43 skrll WQ_MPSAFE);
485 1.43 skrll if (error) {
486 1.43 skrll aprint_error_dev(sc->sc_dev,
487 1.43 skrll "unable to create reset workqueue\n");
488 1.43 skrll return;
489 1.43 skrll }
490 1.43 skrll
491 1.37 skrll /* Allocate memory for receive queue descriptors */
492 1.36 skrll sc->rbqlen = roundup(ETH_DSC_SIZE * (RX_QLEN + 1) * 2, PAGE_SIZE);
493 1.1 hkenken DPRINTFN(1,("%s: rbqlen=%i\n", __FUNCTION__, sc->rbqlen));
494 1.1 hkenken
495 1.39 skrll // see EMAC errata why forced to 16384 byte boundary
496 1.1 hkenken err = bus_dmamem_alloc(sc->sc_dmat, sc->rbqlen, 0,
497 1.39 skrll MAX(16384, PAGE_SIZE), &segs, 1, &rsegs, BUS_DMA_WAITOK);
498 1.1 hkenken if (err == 0) {
499 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
500 1.1 hkenken err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->rbqlen,
501 1.19 msaitoh &sc->rbqpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
502 1.1 hkenken }
503 1.1 hkenken if (err == 0) {
504 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
505 1.1 hkenken err = bus_dmamap_create(sc->sc_dmat, sc->rbqlen, 1,
506 1.1 hkenken sc->rbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
507 1.1 hkenken &sc->rbqpage_dmamap);
508 1.1 hkenken }
509 1.1 hkenken if (err == 0) {
510 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
511 1.1 hkenken err = bus_dmamap_load(sc->sc_dmat, sc->rbqpage_dmamap,
512 1.1 hkenken sc->rbqpage, sc->rbqlen, NULL, BUS_DMA_WAITOK);
513 1.1 hkenken }
514 1.1 hkenken if (err != 0)
515 1.1 hkenken panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
516 1.1 hkenken
517 1.1 hkenken sc->rbqpage_dsaddr = sc->rbqpage_dmamap->dm_segs[0].ds_addr;
518 1.1 hkenken memset(sc->rbqpage, 0, sc->rbqlen);
519 1.1 hkenken
520 1.37 skrll /* Allocate memory for transmit queue descriptors */
521 1.36 skrll sc->tbqlen = roundup(ETH_DSC_SIZE * (TX_QLEN + 1) * 2, PAGE_SIZE);
522 1.1 hkenken DPRINTFN(1,("%s: tbqlen=%i\n", __FUNCTION__, sc->tbqlen));
523 1.1 hkenken
524 1.39 skrll // see EMAC errata why forced to 16384 byte boundary
525 1.1 hkenken err = bus_dmamem_alloc(sc->sc_dmat, sc->tbqlen, 0,
526 1.39 skrll MAX(16384, PAGE_SIZE), &segs, 1, &rsegs, BUS_DMA_WAITOK);
527 1.1 hkenken if (err == 0) {
528 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
529 1.1 hkenken err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->tbqlen,
530 1.19 msaitoh &sc->tbqpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
531 1.1 hkenken }
532 1.1 hkenken if (err == 0) {
533 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
534 1.1 hkenken err = bus_dmamap_create(sc->sc_dmat, sc->tbqlen, 1,
535 1.1 hkenken sc->tbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
536 1.1 hkenken &sc->tbqpage_dmamap);
537 1.1 hkenken }
538 1.1 hkenken if (err == 0) {
539 1.1 hkenken DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
540 1.1 hkenken err = bus_dmamap_load(sc->sc_dmat, sc->tbqpage_dmamap,
541 1.1 hkenken sc->tbqpage, sc->tbqlen, NULL, BUS_DMA_WAITOK);
542 1.1 hkenken }
543 1.1 hkenken if (err != 0)
544 1.1 hkenken panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
545 1.1 hkenken
546 1.1 hkenken sc->tbqpage_dsaddr = sc->tbqpage_dmamap->dm_segs[0].ds_addr;
547 1.1 hkenken memset(sc->tbqpage, 0, sc->tbqlen);
548 1.1 hkenken
549 1.1 hkenken /* Set up pointers to start of each queue in kernel addr space.
550 1.1 hkenken * Each descriptor queue or status queue entry uses 2 words
551 1.1 hkenken */
552 1.1 hkenken sc->RDSC = (void *)sc->rbqpage;
553 1.1 hkenken sc->TDSC = (void *)sc->tbqpage;
554 1.1 hkenken
555 1.1 hkenken /* init TX queue */
556 1.1 hkenken for (i = 0; i < TX_QLEN; i++) {
557 1.1 hkenken sc->TDSC[i].Addr = 0;
558 1.1 hkenken sc->TDSC[i].Info = ETH_TDSC_I_USED |
559 1.1 hkenken (i == (TX_QLEN - 1) ? ETH_TDSC_I_WRAP : 0);
560 1.1 hkenken }
561 1.1 hkenken
562 1.1 hkenken /* Populate the RXQ with mbufs */
563 1.1 hkenken sc->rxqi = 0;
564 1.19 msaitoh for (i = 0; i < RX_QLEN; i++) {
565 1.1 hkenken struct mbuf *m;
566 1.1 hkenken
567 1.39 skrll err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
568 1.39 skrll PAGE_SIZE, BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
569 1.1 hkenken if (err) {
570 1.39 skrll panic("%s: dmamap_create failed: %i\n", __FUNCTION__,
571 1.39 skrll err);
572 1.1 hkenken }
573 1.1 hkenken MGETHDR(m, M_WAIT, MT_DATA);
574 1.1 hkenken MCLGET(m, M_WAIT);
575 1.1 hkenken sc->rxq[i].m = m;
576 1.1 hkenken if (mtod(m, intptr_t) & 3) {
577 1.1 hkenken m_adj(m, mtod(m, intptr_t) & 3);
578 1.1 hkenken }
579 1.1 hkenken err = bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
580 1.1 hkenken m->m_ext.ext_buf, MCLBYTES, NULL,
581 1.1 hkenken BUS_DMA_WAITOK);
582 1.1 hkenken if (err) {
583 1.1 hkenken panic("%s: dmamap_load failed: %i\n", __FUNCTION__, err);
584 1.1 hkenken }
585 1.1 hkenken sc->RDSC[i].Addr = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr
586 1.1 hkenken | (i == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
587 1.1 hkenken sc->RDSC[i].Info = 0;
588 1.1 hkenken bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
589 1.1 hkenken MCLBYTES, BUS_DMASYNC_PREREAD);
590 1.1 hkenken }
591 1.1 hkenken
592 1.1 hkenken /* prepare transmit queue */
593 1.1 hkenken for (i = 0; i < TX_QLEN; i++) {
594 1.1 hkenken err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
595 1.1 hkenken (BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
596 1.1 hkenken &sc->txq[i].m_dmamap);
597 1.1 hkenken if (err)
598 1.1 hkenken panic("ARGH #1");
599 1.1 hkenken sc->txq[i].m = NULL;
600 1.1 hkenken }
601 1.1 hkenken
602 1.1 hkenken /* Program each queue's start addr, cur addr, and len registers
603 1.1 hkenken * with the physical addresses.
604 1.1 hkenken */
605 1.1 hkenken CEMAC_WRITE(ETH_RBQP, (uint32_t)sc->rbqpage_dsaddr);
606 1.1 hkenken CEMAC_WRITE(ETH_TBQP, (uint32_t)sc->tbqpage_dsaddr);
607 1.1 hkenken
608 1.43 skrll sc->sc_mcast_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
609 1.43 skrll sc->sc_intr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
610 1.43 skrll
611 1.1 hkenken /* Divide HCLK by 32 for MDC clock */
612 1.19 msaitoh sc->sc_ethercom.ec_mii = mii;
613 1.19 msaitoh mii->mii_ifp = ifp;
614 1.19 msaitoh mii->mii_readreg = cemac_mii_readreg;
615 1.19 msaitoh mii->mii_writereg = cemac_mii_writereg;
616 1.19 msaitoh mii->mii_statchg = cemac_statchg;
617 1.19 msaitoh ifmedia_init(&mii->mii_media, IFM_IMASK, cemac_mediachange,
618 1.1 hkenken cemac_mediastatus);
619 1.45 lloyd mii_attach(sc->sc_dev, mii, 0xffffffff, sc->sc_phyno, MII_OFFSET_ANY, 0);
620 1.19 msaitoh ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
621 1.1 hkenken
622 1.1 hkenken #if 0
623 1.1 hkenken // enable / disable interrupts
624 1.1 hkenken CEMAC_WRITE(ETH_IDR, -1);
625 1.1 hkenken CEMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
626 1.1 hkenken | ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM);
627 1.1 hkenken // (void)CEMAC_READ(ETH_ISR); // why
628 1.1 hkenken
629 1.1 hkenken // enable transmitter / receiver
630 1.1 hkenken CEMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
631 1.1 hkenken | ETH_CTL_CSR | ETH_CTL_MPE);
632 1.1 hkenken #endif
633 1.1 hkenken /*
634 1.7 rjs * We can support hardware checksumming.
635 1.7 rjs */
636 1.7 rjs ifp->if_capabilities |=
637 1.19 msaitoh IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
638 1.7 rjs IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
639 1.7 rjs IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
640 1.7 rjs IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
641 1.7 rjs IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx;
642 1.7 rjs
643 1.7 rjs /*
644 1.1 hkenken * We can support 802.1Q VLAN-sized frames.
645 1.1 hkenken */
646 1.1 hkenken sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
647 1.1 hkenken
648 1.1 hkenken strcpy(ifp->if_xname, device_xname(sc->sc_dev));
649 1.20 msaitoh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
650 1.43 skrll ifp->if_extflags = IFEF_MPSAFE;
651 1.20 msaitoh ifp->if_ioctl = cemac_ifioctl;
652 1.20 msaitoh ifp->if_start = cemac_ifstart;
653 1.20 msaitoh ifp->if_watchdog = cemac_ifwatchdog;
654 1.20 msaitoh ifp->if_init = cemac_ifinit;
655 1.20 msaitoh ifp->if_stop = cemac_ifstop;
656 1.1 hkenken ifp->if_softc = sc;
657 1.20 msaitoh IFQ_SET_READY(&ifp->if_snd);
658 1.20 msaitoh if_attach(ifp);
659 1.11 ozaki if_deferred_start_init(ifp, NULL);
660 1.20 msaitoh ether_ifattach(ifp, (sc)->sc_enaddr);
661 1.43 skrll ether_set_ifflags_cb(&sc->sc_ethercom, cemac_ifflags_cb);
662 1.1 hkenken }
663 1.1 hkenken
664 1.1 hkenken static int
665 1.1 hkenken cemac_mediachange(struct ifnet *ifp)
666 1.1 hkenken {
667 1.1 hkenken if (ifp->if_flags & IFF_UP)
668 1.1 hkenken cemac_ifinit(ifp);
669 1.29 skrll return 0;
670 1.1 hkenken }
671 1.1 hkenken
672 1.1 hkenken static void
673 1.1 hkenken cemac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
674 1.1 hkenken {
675 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
676 1.1 hkenken
677 1.1 hkenken mii_pollstat(&sc->sc_mii);
678 1.1 hkenken ifmr->ifm_active = sc->sc_mii.mii_media_active;
679 1.1 hkenken ifmr->ifm_status = sc->sc_mii.mii_media_status;
680 1.1 hkenken }
681 1.1 hkenken
682 1.1 hkenken
683 1.1 hkenken static int
684 1.15 msaitoh cemac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
685 1.1 hkenken {
686 1.35 skrll struct cemac_softc * const sc = device_private(self);
687 1.1 hkenken
688 1.1 hkenken CEMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_RD
689 1.1 hkenken | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
690 1.1 hkenken | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
691 1.1 hkenken | ETH_MAN_CODE_IEEE802_3));
692 1.19 msaitoh while (!(CEMAC_READ(ETH_SR) & ETH_SR_IDLE))
693 1.19 msaitoh ;
694 1.1 hkenken
695 1.15 msaitoh *val = CEMAC_READ(ETH_MAN) & ETH_MAN_DATA;
696 1.15 msaitoh return 0;
697 1.1 hkenken }
698 1.1 hkenken
699 1.15 msaitoh static int
700 1.15 msaitoh cemac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
701 1.1 hkenken {
702 1.35 skrll struct cemac_softc * const sc = device_private(self);
703 1.1 hkenken
704 1.1 hkenken CEMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_WR
705 1.1 hkenken | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
706 1.1 hkenken | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
707 1.1 hkenken | ETH_MAN_CODE_IEEE802_3
708 1.1 hkenken | (val & ETH_MAN_DATA)));
709 1.19 msaitoh while (!(CEMAC_READ(ETH_SR) & ETH_SR_IDLE))
710 1.19 msaitoh ;
711 1.15 msaitoh
712 1.15 msaitoh return 0;
713 1.1 hkenken }
714 1.1 hkenken
715 1.1 hkenken
716 1.1 hkenken static void
717 1.1 hkenken cemac_statchg(struct ifnet *ifp)
718 1.1 hkenken {
719 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
720 1.1 hkenken struct mii_data *mii = &sc->sc_mii;
721 1.20 msaitoh uint32_t reg;
722 1.1 hkenken
723 1.20 msaitoh /*
724 1.20 msaitoh * We must keep the MAC and the PHY in sync as
725 1.20 msaitoh * to the status of full-duplex!
726 1.20 msaitoh */
727 1.1 hkenken reg = CEMAC_READ(ETH_CFG);
728 1.1 hkenken reg &= ~ETH_CFG_FD;
729 1.20 msaitoh if (sc->sc_mii.mii_media_active & IFM_FDX)
730 1.20 msaitoh reg |= ETH_CFG_FD;
731 1.1 hkenken
732 1.1 hkenken reg &= ~ETH_CFG_SPD;
733 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
734 1.1 hkenken reg &= ~GEM_CFG_GEN;
735 1.1 hkenken switch (IFM_SUBTYPE(mii->mii_media_active)) {
736 1.1 hkenken case IFM_10_T:
737 1.1 hkenken break;
738 1.1 hkenken case IFM_100_TX:
739 1.1 hkenken reg |= ETH_CFG_SPD;
740 1.1 hkenken break;
741 1.1 hkenken case IFM_1000_T:
742 1.1 hkenken reg |= ETH_CFG_SPD | GEM_CFG_GEN;
743 1.1 hkenken break;
744 1.1 hkenken default:
745 1.1 hkenken break;
746 1.1 hkenken }
747 1.1 hkenken CEMAC_WRITE(ETH_CFG, reg);
748 1.1 hkenken }
749 1.1 hkenken
750 1.43 skrll static bool
751 1.43 skrll cemac_watchdog_check(struct cemac_softc * const sc)
752 1.43 skrll {
753 1.43 skrll
754 1.43 skrll KASSERT(mutex_owned(sc->sc_intr_lock));
755 1.43 skrll
756 1.43 skrll if (!sc->sc_tx_sending)
757 1.43 skrll return true;
758 1.43 skrll
759 1.43 skrll if (time_uptime - sc->sc_tx_lastsent <= cemac_watchdog_timeout)
760 1.43 skrll return true;
761 1.43 skrll
762 1.43 skrll return false;
763 1.43 skrll }
764 1.43 skrll
765 1.43 skrll static bool
766 1.43 skrll cemac_watchdog_tick(struct ifnet *ifp)
767 1.43 skrll {
768 1.43 skrll struct cemac_softc * const sc = ifp->if_softc;
769 1.43 skrll
770 1.43 skrll KASSERT(mutex_owned(sc->sc_intr_lock));
771 1.43 skrll
772 1.43 skrll if (!sc->sc_trigger_reset && cemac_watchdog_check(sc))
773 1.43 skrll return true;
774 1.43 skrll
775 1.43 skrll if (atomic_swap_uint(&sc->sc_reset_pending, 1) == 0)
776 1.43 skrll workqueue_enqueue(sc->sc_reset_wq, &sc->sc_reset_work, NULL);
777 1.43 skrll
778 1.43 skrll return false;
779 1.43 skrll }
780 1.43 skrll
781 1.43 skrll
782 1.1 hkenken static void
783 1.1 hkenken cemac_tick(void *arg)
784 1.1 hkenken {
785 1.35 skrll struct cemac_softc * const sc = arg;
786 1.41 skrll struct ifnet * const ifp = &sc->sc_ethercom.ec_if;
787 1.43 skrll
788 1.43 skrll mutex_enter(sc->sc_intr_lock);
789 1.43 skrll if (sc->sc_stopping) {
790 1.43 skrll mutex_exit(sc->sc_intr_lock);
791 1.43 skrll return;
792 1.43 skrll }
793 1.1 hkenken
794 1.3 rjs if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
795 1.22 thorpej if_statadd(ifp, if_collisions,
796 1.22 thorpej CEMAC_READ(GEM_SCOL) + CEMAC_READ(GEM_MCOL));
797 1.3 rjs else
798 1.22 thorpej if_statadd(ifp, if_collisions,
799 1.22 thorpej CEMAC_READ(ETH_SCOL) + CEMAC_READ(ETH_MCOL));
800 1.3 rjs
801 1.1 hkenken /* These misses are ok, they will happen if the RAM/CPU can't keep up */
802 1.1 hkenken if (!ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
803 1.1 hkenken uint32_t misses = CEMAC_READ(ETH_DRFC);
804 1.1 hkenken if (misses > 0)
805 1.4 rjs aprint_normal_ifnet(ifp, "%d rx misses\n", misses);
806 1.1 hkenken }
807 1.1 hkenken
808 1.43 skrll mii_tick(&sc->sc_mii);
809 1.43 skrll
810 1.43 skrll const bool ok = cemac_watchdog_tick(ifp);
811 1.43 skrll if (ok)
812 1.43 skrll callout_schedule(&sc->cemac_tick_ch, hz);
813 1.1 hkenken
814 1.43 skrll mutex_exit(sc->sc_intr_lock);
815 1.1 hkenken }
816 1.1 hkenken
817 1.1 hkenken
818 1.1 hkenken static int
819 1.1 hkenken cemac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
820 1.1 hkenken {
821 1.43 skrll struct cemac_softc * const sc = ifp->if_softc;
822 1.43 skrll int error;
823 1.1 hkenken
824 1.43 skrll switch (cmd) {
825 1.43 skrll case SIOCADDMULTI:
826 1.43 skrll case SIOCDELMULTI:
827 1.43 skrll break;
828 1.43 skrll default:
829 1.43 skrll KASSERT(IFNET_LOCKED(ifp));
830 1.43 skrll }
831 1.7 rjs
832 1.43 skrll const int s = splnet();
833 1.43 skrll error = ether_ioctl(ifp, cmd, data);
834 1.1 hkenken splx(s);
835 1.43 skrll
836 1.43 skrll if (error == ENETRESET) {
837 1.43 skrll error = 0;
838 1.43 skrll
839 1.43 skrll if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
840 1.43 skrll mutex_enter(sc->sc_mcast_lock);
841 1.43 skrll if ((sc->sc_if_flags & IFF_RUNNING) != 0)
842 1.43 skrll cemac_setaddr(ifp);
843 1.43 skrll
844 1.43 skrll mutex_exit(sc->sc_mcast_lock);
845 1.43 skrll }
846 1.43 skrll }
847 1.43 skrll
848 1.1 hkenken return error;
849 1.1 hkenken }
850 1.1 hkenken
851 1.43 skrll
852 1.43 skrll
853 1.1 hkenken static void
854 1.1 hkenken cemac_ifstart(struct ifnet *ifp)
855 1.1 hkenken {
856 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
857 1.43 skrll KASSERT(if_is_mpsafe(ifp));
858 1.43 skrll
859 1.43 skrll mutex_enter(sc->sc_intr_lock);
860 1.43 skrll if (!sc->sc_stopping) {
861 1.43 skrll cemac_ifstart_locked(ifp);
862 1.43 skrll }
863 1.43 skrll mutex_exit(sc->sc_intr_lock);
864 1.43 skrll }
865 1.43 skrll
866 1.43 skrll static void
867 1.43 skrll cemac_ifstart_locked(struct ifnet *ifp)
868 1.43 skrll {
869 1.43 skrll struct cemac_softc * const sc = ifp->if_softc;
870 1.1 hkenken struct mbuf *m;
871 1.1 hkenken bus_dma_segment_t *segs;
872 1.43 skrll int bi, err, nsegs;
873 1.43 skrll
874 1.43 skrll KASSERT(mutex_owned(sc->sc_intr_lock));
875 1.1 hkenken
876 1.1 hkenken start:
877 1.1 hkenken if (cemac_gctx(sc) == 0) {
878 1.1 hkenken /* Enable transmit-buffer-free interrupt */
879 1.1 hkenken CEMAC_WRITE(ETH_IER, ETH_ISR_TBRE);
880 1.42 skrll sc->sc_txbusy = true;
881 1.1 hkenken return;
882 1.1 hkenken }
883 1.1 hkenken
884 1.1 hkenken IFQ_POLL(&ifp->if_snd, m);
885 1.1 hkenken if (m == NULL) {
886 1.1 hkenken return;
887 1.1 hkenken }
888 1.1 hkenken
889 1.1 hkenken bi = (sc->txqi + sc->txqc) % TX_QLEN;
890 1.1 hkenken if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
891 1.1 hkenken BUS_DMA_NOWAIT)) ||
892 1.1 hkenken sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
893 1.1 hkenken sc->txq[bi].m_dmamap->dm_nsegs > 1) {
894 1.1 hkenken /* Copy entire mbuf chain to new single */
895 1.1 hkenken struct mbuf *mn;
896 1.1 hkenken
897 1.1 hkenken if (err == 0)
898 1.1 hkenken bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
899 1.1 hkenken
900 1.1 hkenken MGETHDR(mn, M_DONTWAIT, MT_DATA);
901 1.34 skrll if (mn == NULL)
902 1.43 skrll return;
903 1.1 hkenken if (m->m_pkthdr.len > MHLEN) {
904 1.1 hkenken MCLGET(mn, M_DONTWAIT);
905 1.1 hkenken if ((mn->m_flags & M_EXT) == 0) {
906 1.1 hkenken m_freem(mn);
907 1.43 skrll return;
908 1.1 hkenken }
909 1.1 hkenken }
910 1.1 hkenken m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
911 1.1 hkenken mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
912 1.1 hkenken IFQ_DEQUEUE(&ifp->if_snd, m);
913 1.1 hkenken m_freem(m);
914 1.1 hkenken m = mn;
915 1.1 hkenken bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
916 1.1 hkenken BUS_DMA_NOWAIT);
917 1.1 hkenken } else {
918 1.1 hkenken IFQ_DEQUEUE(&ifp->if_snd, m);
919 1.1 hkenken }
920 1.1 hkenken
921 1.13 msaitoh bpf_mtap(ifp, m, BPF_D_OUT);
922 1.1 hkenken
923 1.1 hkenken nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
924 1.1 hkenken segs = sc->txq[bi].m_dmamap->dm_segs;
925 1.1 hkenken if (nsegs > 1)
926 1.1 hkenken panic("#### ARGH #2");
927 1.1 hkenken
928 1.1 hkenken sc->txq[bi].m = m;
929 1.1 hkenken sc->txqc++;
930 1.1 hkenken
931 1.39 skrll DPRINTFN(2,("%s: start sending idx #%i mbuf %p (txqc=%i, phys %p), "
932 1.39 skrll "len=%u\n", __FUNCTION__, bi, sc->txq[bi].m, sc->txqc,
933 1.39 skrll (void *)segs->ds_addr, (unsigned)m->m_pkthdr.len));
934 1.1 hkenken #ifdef DIAGNOSTIC
935 1.1 hkenken if (sc->txqc > TX_QLEN)
936 1.1 hkenken panic("%s: txqc %i > %i", __FUNCTION__, sc->txqc, TX_QLEN);
937 1.1 hkenken #endif
938 1.1 hkenken
939 1.1 hkenken bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
940 1.39 skrll sc->txq[bi].m_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
941 1.1 hkenken
942 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
943 1.1 hkenken sc->TDSC[bi].Addr = segs->ds_addr;
944 1.39 skrll sc->TDSC[bi].Info =
945 1.39 skrll __SHIFTIN(m->m_pkthdr.len, ETH_TDSC_I_LEN) |
946 1.39 skrll ETH_TDSC_I_LAST_BUF |
947 1.39 skrll (bi == (TX_QLEN - 1) ? ETH_TDSC_I_WRAP : 0);
948 1.1 hkenken
949 1.1 hkenken DPRINTFN(3,("%s: TDSC[%i].Addr 0x%08x\n",
950 1.1 hkenken __FUNCTION__, bi, sc->TDSC[bi].Addr));
951 1.1 hkenken DPRINTFN(3,("%s: TDSC[%i].Info 0x%08x\n",
952 1.1 hkenken __FUNCTION__, bi, sc->TDSC[bi].Info));
953 1.1 hkenken
954 1.1 hkenken uint32_t ctl = CEMAC_READ(ETH_CTL) | GEM_CTL_STARTTX;
955 1.1 hkenken CEMAC_WRITE(ETH_CTL, ctl);
956 1.39 skrll DPRINTFN(3,("%s: ETH_CTL 0x%08x\n", __FUNCTION__,
957 1.39 skrll CEMAC_READ(ETH_CTL)));
958 1.1 hkenken } else {
959 1.1 hkenken CEMAC_WRITE(ETH_TAR, segs->ds_addr);
960 1.1 hkenken CEMAC_WRITE(ETH_TCR, m->m_pkthdr.len);
961 1.1 hkenken }
962 1.43 skrll sc->sc_tx_lastsent = time_uptime;
963 1.43 skrll
964 1.1 hkenken if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
965 1.1 hkenken goto start;
966 1.1 hkenken
967 1.1 hkenken return;
968 1.1 hkenken }
969 1.1 hkenken
970 1.1 hkenken static void
971 1.1 hkenken cemac_ifwatchdog(struct ifnet *ifp)
972 1.1 hkenken {
973 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
974 1.1 hkenken
975 1.1 hkenken if ((ifp->if_flags & IFF_RUNNING) == 0)
976 1.1 hkenken return;
977 1.5 rjs aprint_error_ifnet(ifp, "device timeout, CTL = 0x%08x, CFG = 0x%08x\n",
978 1.39 skrll CEMAC_READ(ETH_CTL), CEMAC_READ(ETH_CFG));
979 1.1 hkenken }
980 1.1 hkenken
981 1.1 hkenken static int
982 1.1 hkenken cemac_ifinit(struct ifnet *ifp)
983 1.1 hkenken {
984 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
985 1.7 rjs uint32_t dma, cfg;
986 1.1 hkenken
987 1.43 skrll ASSERT_SLEEPABLE();
988 1.43 skrll KASSERT(IFNET_LOCKED(ifp));
989 1.43 skrll
990 1.43 skrll /* Cancel pending I/O and flush buffers. */
991 1.43 skrll cemac_ifstop(ifp, 0);
992 1.1 hkenken
993 1.7 rjs if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
994 1.7 rjs
995 1.7 rjs if (ifp->if_capenable &
996 1.7 rjs (IFCAP_CSUM_IPv4_Tx |
997 1.7 rjs IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx |
998 1.7 rjs IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx)) {
999 1.7 rjs dma = CEMAC_READ(GEM_DMA_CFG);
1000 1.7 rjs dma |= GEM_DMA_CFG_CHKSUM_GEN_OFFLOAD_EN;
1001 1.7 rjs CEMAC_WRITE(GEM_DMA_CFG, dma);
1002 1.7 rjs }
1003 1.7 rjs if (ifp->if_capenable &
1004 1.7 rjs (IFCAP_CSUM_IPv4_Rx |
1005 1.7 rjs IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1006 1.7 rjs IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)) {
1007 1.7 rjs cfg = CEMAC_READ(ETH_CFG);
1008 1.40 skrll cfg |= GEM_CFG_RXCOEN;
1009 1.7 rjs CEMAC_WRITE(ETH_CFG, cfg);
1010 1.7 rjs }
1011 1.7 rjs }
1012 1.7 rjs
1013 1.1 hkenken // enable interrupts
1014 1.1 hkenken CEMAC_WRITE(ETH_IDR, -1);
1015 1.1 hkenken CEMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
1016 1.1 hkenken | ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM);
1017 1.1 hkenken
1018 1.1 hkenken // enable transmitter / receiver
1019 1.1 hkenken CEMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
1020 1.1 hkenken | ETH_CTL_CSR | ETH_CTL_MPE);
1021 1.1 hkenken
1022 1.1 hkenken mii_mediachg(&sc->sc_mii);
1023 1.1 hkenken callout_reset(&sc->cemac_tick_ch, hz, cemac_tick, sc);
1024 1.20 msaitoh ifp->if_flags |= IFF_RUNNING;
1025 1.43 skrll
1026 1.43 skrll mutex_enter(sc->sc_intr_lock);
1027 1.43 skrll sc->sc_stopping = false;
1028 1.43 skrll mutex_exit(sc->sc_intr_lock);
1029 1.43 skrll
1030 1.1 hkenken return 0;
1031 1.1 hkenken }
1032 1.1 hkenken
1033 1.1 hkenken static void
1034 1.1 hkenken cemac_ifstop(struct ifnet *ifp, int disable)
1035 1.1 hkenken {
1036 1.1 hkenken // uint32_t u;
1037 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
1038 1.1 hkenken
1039 1.43 skrll ASSERT_SLEEPABLE();
1040 1.43 skrll KASSERT(IFNET_LOCKED(ifp));
1041 1.43 skrll
1042 1.43 skrll ifp->if_flags &= ~IFF_RUNNING;
1043 1.43 skrll
1044 1.43 skrll mutex_enter(sc->sc_mcast_lock);
1045 1.43 skrll sc->sc_if_flags = ifp->if_flags;
1046 1.43 skrll mutex_exit(sc->sc_mcast_lock);
1047 1.43 skrll
1048 1.43 skrll mutex_enter(sc->sc_intr_lock);
1049 1.43 skrll sc->sc_stopping = true;
1050 1.43 skrll mutex_exit(sc->sc_intr_lock);
1051 1.43 skrll
1052 1.1 hkenken #if 0
1053 1.1 hkenken CEMAC_WRITE(ETH_CTL, ETH_CTL_MPE); // disable everything
1054 1.1 hkenken CEMAC_WRITE(ETH_IDR, -1); // disable interrupts
1055 1.1 hkenken // CEMAC_WRITE(ETH_RBQP, 0); // clear receive
1056 1.1 hkenken if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
1057 1.1 hkenken CEMAC_WRITE(ETH_CFG,
1058 1.1 hkenken GEM_CFG_CLK_64 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
1059 1.1 hkenken else
1060 1.1 hkenken CEMAC_WRITE(ETH_CFG,
1061 1.1 hkenken ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
1062 1.1 hkenken // CEMAC_WRITE(ETH_TCR, 0); // send nothing
1063 1.1 hkenken // (void)CEMAC_READ(ETH_ISR);
1064 1.1 hkenken u = CEMAC_READ(ETH_TSR);
1065 1.1 hkenken CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
1066 1.1 hkenken | ETH_TSR_IDLE | ETH_TSR_RLE
1067 1.19 msaitoh | ETH_TSR_COL | ETH_TSR_OVR)));
1068 1.1 hkenken u = CEMAC_READ(ETH_RSR);
1069 1.19 msaitoh CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
1070 1.1 hkenken #endif
1071 1.43 skrll callout_halt(&sc->cemac_tick_ch, NULL);
1072 1.1 hkenken
1073 1.1 hkenken /* Down the MII. */
1074 1.1 hkenken mii_down(&sc->sc_mii);
1075 1.1 hkenken
1076 1.25 thorpej ifp->if_flags &= ~IFF_RUNNING;
1077 1.42 skrll sc->sc_txbusy = false;
1078 1.44 skrll sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
1079 1.1 hkenken }
1080 1.1 hkenken
1081 1.1 hkenken static void
1082 1.1 hkenken cemac_setaddr(struct ifnet *ifp)
1083 1.1 hkenken {
1084 1.35 skrll struct cemac_softc * const sc = ifp->if_softc;
1085 1.19 msaitoh struct ethercom *ec = &sc->sc_ethercom;
1086 1.1 hkenken struct ether_multi *enm;
1087 1.1 hkenken struct ether_multistep step;
1088 1.1 hkenken uint8_t ias[3][ETHER_ADDR_LEN];
1089 1.1 hkenken uint32_t h, nma = 0, hashes[2] = { 0, 0 };
1090 1.1 hkenken uint32_t ctl = CEMAC_READ(ETH_CTL);
1091 1.1 hkenken uint32_t cfg = CEMAC_READ(ETH_CFG);
1092 1.1 hkenken
1093 1.43 skrll KASSERT(mutex_owned(sc->sc_mcast_lock));
1094 1.43 skrll
1095 1.1 hkenken /* disable receiver temporarily */
1096 1.1 hkenken CEMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
1097 1.1 hkenken
1098 1.1 hkenken cfg &= ~(ETH_CFG_MTI | ETH_CFG_UNI | ETH_CFG_CAF | ETH_CFG_UNI);
1099 1.1 hkenken
1100 1.43 skrll if (sc->sc_if_flags & IFF_PROMISC) {
1101 1.20 msaitoh cfg |= ETH_CFG_CAF;
1102 1.1 hkenken } else {
1103 1.1 hkenken cfg &= ~ETH_CFG_CAF;
1104 1.1 hkenken }
1105 1.1 hkenken
1106 1.1 hkenken // ETH_CFG_BIG?
1107 1.1 hkenken
1108 1.43 skrll ETHER_LOCK(ec);
1109 1.43 skrll ec->ec_flags &= ~ETHER_F_ALLMULTI;
1110 1.1 hkenken
1111 1.19 msaitoh ETHER_FIRST_MULTI(step, ec, enm);
1112 1.1 hkenken while (enm != NULL) {
1113 1.1 hkenken if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1114 1.1 hkenken /*
1115 1.1 hkenken * We must listen to a range of multicast addresses.
1116 1.1 hkenken * For now, just accept all multicasts, rather than
1117 1.1 hkenken * trying to set only those filter bits needed to match
1118 1.1 hkenken * the range. (At this time, the only use of address
1119 1.1 hkenken * ranges is for IP multicast routing, for which the
1120 1.1 hkenken * range is big enough to require all bits set.)
1121 1.1 hkenken */
1122 1.6 rjs cfg |= ETH_CFG_MTI;
1123 1.1 hkenken hashes[0] = 0xffffffffUL;
1124 1.1 hkenken hashes[1] = 0xffffffffUL;
1125 1.1 hkenken nma = 0;
1126 1.43 skrll ec->ec_flags |= ETHER_F_ALLMULTI;
1127 1.1 hkenken break;
1128 1.1 hkenken }
1129 1.1 hkenken
1130 1.1 hkenken if (nma < 3) {
1131 1.1 hkenken /* We can program 3 perfect address filters for mcast */
1132 1.1 hkenken memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
1133 1.1 hkenken } else {
1134 1.1 hkenken /*
1135 1.1 hkenken * XXX: Datasheet is not very clear here, I'm not sure
1136 1.1 hkenken * if I'm doing this right. --joff
1137 1.1 hkenken */
1138 1.1 hkenken h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1139 1.1 hkenken
1140 1.1 hkenken /* Just want the 6 most-significant bits. */
1141 1.1 hkenken h = h >> 26;
1142 1.6 rjs #if 0
1143 1.1 hkenken hashes[h / 32] |= (1 << (h % 32));
1144 1.6 rjs #else
1145 1.6 rjs hashes[0] = 0xffffffffUL;
1146 1.6 rjs hashes[1] = 0xffffffffUL;
1147 1.6 rjs #endif
1148 1.1 hkenken cfg |= ETH_CFG_MTI;
1149 1.1 hkenken }
1150 1.1 hkenken ETHER_NEXT_MULTI(step, enm);
1151 1.1 hkenken nma++;
1152 1.1 hkenken }
1153 1.21 msaitoh ETHER_UNLOCK(ec);
1154 1.1 hkenken
1155 1.1 hkenken // program...
1156 1.1 hkenken DPRINTFN(1,("%s: en0 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
1157 1.1 hkenken sc->sc_enaddr[0], sc->sc_enaddr[1], sc->sc_enaddr[2],
1158 1.1 hkenken sc->sc_enaddr[3], sc->sc_enaddr[4], sc->sc_enaddr[5]));
1159 1.1 hkenken CEMAC_GEM_WRITE(SA1L, (sc->sc_enaddr[3] << 24)
1160 1.1 hkenken | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
1161 1.1 hkenken | (sc->sc_enaddr[0]));
1162 1.1 hkenken CEMAC_GEM_WRITE(SA1H, (sc->sc_enaddr[5] << 8)
1163 1.1 hkenken | (sc->sc_enaddr[4]));
1164 1.6 rjs if (nma > 0) {
1165 1.39 skrll DPRINTFN(1,("%s: en1 %02x:%02x:%02x:%02x:%02x:%02x\n",
1166 1.39 skrll __FUNCTION__,
1167 1.39 skrll ias[0][0], ias[0][1], ias[0][2],
1168 1.39 skrll ias[0][3], ias[0][4], ias[0][5]));
1169 1.1 hkenken CEMAC_WRITE(ETH_SA2L, (ias[0][3] << 24)
1170 1.1 hkenken | (ias[0][2] << 16) | (ias[0][1] << 8)
1171 1.1 hkenken | (ias[0][0]));
1172 1.1 hkenken CEMAC_WRITE(ETH_SA2H, (ias[0][4] << 8)
1173 1.1 hkenken | (ias[0][5]));
1174 1.1 hkenken }
1175 1.6 rjs if (nma > 1) {
1176 1.39 skrll DPRINTFN(1,("%s: en2 %02x:%02x:%02x:%02x:%02x:%02x\n",
1177 1.39 skrll __FUNCTION__,
1178 1.39 skrll ias[1][0], ias[1][1], ias[1][2],
1179 1.39 skrll ias[1][3], ias[1][4], ias[1][5]));
1180 1.1 hkenken CEMAC_WRITE(ETH_SA3L, (ias[1][3] << 24)
1181 1.1 hkenken | (ias[1][2] << 16) | (ias[1][1] << 8)
1182 1.1 hkenken | (ias[1][0]));
1183 1.1 hkenken CEMAC_WRITE(ETH_SA3H, (ias[1][4] << 8)
1184 1.1 hkenken | (ias[1][5]));
1185 1.1 hkenken }
1186 1.6 rjs if (nma > 2) {
1187 1.39 skrll DPRINTFN(1,("%s: en3 %02x:%02x:%02x:%02x:%02x:%02x\n",
1188 1.39 skrll __FUNCTION__,
1189 1.39 skrll ias[2][0], ias[2][1], ias[2][2],
1190 1.39 skrll ias[2][3], ias[2][4], ias[2][5]));
1191 1.6 rjs CEMAC_WRITE(ETH_SA4L, (ias[2][3] << 24)
1192 1.1 hkenken | (ias[2][2] << 16) | (ias[2][1] << 8)
1193 1.1 hkenken | (ias[2][0]));
1194 1.6 rjs CEMAC_WRITE(ETH_SA4H, (ias[2][4] << 8)
1195 1.1 hkenken | (ias[2][5]));
1196 1.1 hkenken }
1197 1.1 hkenken CEMAC_GEM_WRITE(HSH, hashes[0]);
1198 1.1 hkenken CEMAC_GEM_WRITE(HSL, hashes[1]);
1199 1.1 hkenken CEMAC_WRITE(ETH_CFG, cfg);
1200 1.1 hkenken CEMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
1201 1.1 hkenken }
1202