epe.c revision 1.38.2.2 1 /* $NetBSD: epe.c,v 1.38.2.2 2020/04/08 14:07:29 martin Exp $ */
2
3 /*
4 * Copyright (c) 2004 Jesse Off
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: epe.c,v 1.38.2.2 2020/04/08 14:07:29 martin Exp $");
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ioctl.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/time.h>
40 #include <sys/device.h>
41 #include <uvm/uvm_extern.h>
42
43 #include <sys/bus.h>
44 #include <machine/intr.h>
45
46 #include <arm/cpufunc.h>
47
48 #include <arm/ep93xx/epsocvar.h>
49 #include <arm/ep93xx/ep93xxvar.h>
50
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_types.h>
54 #include <net/if_media.h>
55 #include <net/if_ether.h>
56 #include <net/bpf.h>
57
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/if_inarp.h>
67 #endif
68
69 #include <arm/ep93xx/ep93xxreg.h>
70 #include <arm/ep93xx/epereg.h>
71 #include <arm/ep93xx/epevar.h>
72
73 #define DEFAULT_MDCDIV 32
74
75 #ifndef EPE_FAST
76 #define EPE_FAST
77 #endif
78
79 #ifndef EPE_FAST
80 #define EPE_READ(x) \
81 bus_space_read_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x))
82 #define EPE_WRITE(x, y) \
83 bus_space_write_4(sc->sc_iot, sc->sc_ioh, (EPE_ ## x), (y))
84 #define CTRLPAGE_DMASYNC(x, y, z) \
85 bus_dmamap_sync(sc->sc_dmat, sc->ctrlpage_dmamap, (x), (y), (z))
86 #else
87 #define EPE_READ(x) *(volatile uint32_t *) \
88 (EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x))
89 #define EPE_WRITE(x, y) *(volatile uint32_t *) \
90 (EP93XX_AHB_VBASE + EP93XX_AHB_EPE + (EPE_ ## x)) = y
91 #define CTRLPAGE_DMASYNC(x, y, z)
92 #endif /* ! EPE_FAST */
93
94 static int epe_match(device_t , cfdata_t, void *);
95 static void epe_attach(device_t, device_t, void *);
96 static void epe_init(struct epe_softc *);
97 static int epe_intr(void* arg);
98 static int epe_gctx(struct epe_softc *);
99 int epe_mii_readreg (device_t, int, int, uint16_t *);
100 int epe_mii_writereg (device_t, int, int, uint16_t);
101 void epe_statchg (struct ifnet *);
102 void epe_tick (void *);
103 static int epe_ifioctl (struct ifnet *, u_long, void *);
104 static void epe_ifstart (struct ifnet *);
105 static void epe_ifwatchdog (struct ifnet *);
106 static int epe_ifinit (struct ifnet *);
107 static void epe_ifstop (struct ifnet *, int);
108 static void epe_setaddr (struct ifnet *);
109
110 CFATTACH_DECL_NEW(epe, sizeof(struct epe_softc),
111 epe_match, epe_attach, NULL, NULL);
112
113 static int
114 epe_match(device_t parent, cfdata_t match, void *aux)
115 {
116 return 2;
117 }
118
119 static void
120 epe_attach(device_t parent, device_t self, void *aux)
121 {
122 struct epe_softc *sc = device_private(self);
123 struct epsoc_attach_args *sa;
124 prop_data_t enaddr;
125
126 aprint_normal("\n");
127 sa = aux;
128 sc->sc_dev = self;
129 sc->sc_iot = sa->sa_iot;
130 sc->sc_intr = sa->sa_intr;
131 sc->sc_dmat = sa->sa_dmat;
132
133 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size,
134 0, &sc->sc_ioh))
135 panic("%s: Cannot map registers", device_xname(self));
136
137 /* Fetch the Ethernet address from property if set. */
138 enaddr = prop_dictionary_get(device_properties(self), "mac-address");
139 if (enaddr != NULL) {
140 KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
141 KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
142 memcpy(sc->sc_enaddr, prop_data_data_nocopy(enaddr),
143 ETHER_ADDR_LEN);
144 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPE_AFP, 0);
145 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
146 sc->sc_enaddr, ETHER_ADDR_LEN);
147 }
148
149 ep93xx_intr_establish(sc->sc_intr, IPL_NET, epe_intr, sc);
150 epe_init(sc);
151 }
152
153 static int
154 epe_gctx(struct epe_softc *sc)
155 {
156 struct ifnet * ifp = &sc->sc_ec.ec_if;
157 uint32_t *cur, ndq = 0;
158
159 /* Handle transmit completions */
160 cur = (uint32_t *)(EPE_READ(TXStsQCurAdd) -
161 sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
162
163 if (sc->TXStsQ_cur != cur) {
164 CTRLPAGE_DMASYNC(TX_QLEN * 2 * sizeof(uint32_t),
165 TX_QLEN * sizeof(uint32_t), BUS_DMASYNC_PREREAD);
166 } else
167 return 0;
168
169 do {
170 uint32_t tbi = *sc->TXStsQ_cur & 0x7fff;
171 struct mbuf *m = sc->txq[tbi].m;
172
173 if ((*sc->TXStsQ_cur & TXStsQ_TxWE) == 0)
174 if_statinc(ifp, if_oerrors);
175
176 bus_dmamap_unload(sc->sc_dmat, sc->txq[tbi].m_dmamap);
177 m_freem(m);
178 do {
179 sc->txq[tbi].m = NULL;
180 ndq++;
181 tbi = (tbi + 1) % TX_QLEN;
182 } while (sc->txq[tbi].m == m);
183
184 if_statinc(ifp, if_opackets);
185 sc->TXStsQ_cur++;
186 if (sc->TXStsQ_cur >= sc->TXStsQ + TX_QLEN) {
187 sc->TXStsQ_cur = sc->TXStsQ;
188 }
189 } while (sc->TXStsQ_cur != cur);
190
191 sc->TXDQ_avail += ndq;
192 if (ifp->if_flags & IFF_OACTIVE) {
193 ifp->if_flags &= ~IFF_OACTIVE;
194 /* Disable end-of-tx-chain interrupt */
195 EPE_WRITE(IntEn, IntEn_REOFIE);
196 }
197 return ndq;
198 }
199
200 static int
201 epe_intr(void *arg)
202 {
203 struct epe_softc *sc = (struct epe_softc *)arg;
204 struct ifnet * ifp = &sc->sc_ec.ec_if;
205 uint32_t ndq = 0, irq, *cur;
206
207 irq = EPE_READ(IntStsC);
208 begin:
209 cur = (uint32_t *)(EPE_READ(RXStsQCurAdd) -
210 sc->ctrlpage_dsaddr + (char*)sc->ctrlpage);
211 CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
212 RX_QLEN * 4 * sizeof(uint32_t),
213 BUS_DMASYNC_PREREAD);
214 while (sc->RXStsQ_cur != cur) {
215 if ((sc->RXStsQ_cur[0] & (RXStsQ_RWE | RXStsQ_RFP |RXStsQ_EOB))
216 == (RXStsQ_RWE | RXStsQ_RFP | RXStsQ_EOB)) {
217 uint32_t bi = (sc->RXStsQ_cur[1] >> 16) & 0x7fff;
218 uint32_t fl = sc->RXStsQ_cur[1] & 0xffff;
219 struct mbuf *m;
220
221 MGETHDR(m, M_DONTWAIT, MT_DATA);
222 if (m != NULL) MCLGET(m, M_DONTWAIT);
223 if (m != NULL && (m->m_flags & M_EXT)) {
224 bus_dmamap_unload(sc->sc_dmat,
225 sc->rxq[bi].m_dmamap);
226 m_set_rcvif(sc->rxq[bi].m, ifp);
227 sc->rxq[bi].m->m_pkthdr.len =
228 sc->rxq[bi].m->m_len = fl;
229 if_percpuq_enqueue(ifp->if_percpuq,
230 sc->rxq[bi].m);
231 sc->rxq[bi].m = m;
232 bus_dmamap_load(sc->sc_dmat,
233 sc->rxq[bi].m_dmamap,
234 m->m_ext.ext_buf, MCLBYTES,
235 NULL, BUS_DMA_NOWAIT);
236 sc->RXDQ[bi * 2] =
237 sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr;
238 } else {
239 /* Drop packets until we can get replacement
240 * empty mbufs for the RXDQ.
241 */
242 if (m != NULL)
243 m_freem(m);
244
245 if_statinc(ifp, if_ierrors);
246 }
247 } else
248 if_statinc(ifp, if_ierrors);
249
250 ndq++;
251
252 sc->RXStsQ_cur += 2;
253 if (sc->RXStsQ_cur >= sc->RXStsQ + (RX_QLEN * 2))
254 sc->RXStsQ_cur = sc->RXStsQ;
255 }
256
257 if (ndq > 0) {
258 ifp->if_ipackets += ndq;
259 CTRLPAGE_DMASYNC(TX_QLEN * 3 * sizeof(uint32_t),
260 RX_QLEN * 4 * sizeof(uint32_t),
261 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
262 EPE_WRITE(RXStsEnq, ndq);
263 EPE_WRITE(RXDEnq, ndq);
264 ndq = 0;
265 }
266
267 if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
268 if_schedule_deferred_start(ifp);
269 }
270
271 irq = EPE_READ(IntStsC);
272 if ((irq & (IntSts_RxSQ | IntSts_ECI)) != 0)
273 goto begin;
274
275 return 1;
276 }
277
278
279 static void
280 epe_init(struct epe_softc *sc)
281 {
282 bus_dma_segment_t segs;
283 char *addr;
284 int rsegs, err, i;
285 struct ifnet * ifp = &sc->sc_ec.ec_if;
286 struct mii_data *mii = &sc->sc_mii;
287 int mdcdiv = DEFAULT_MDCDIV;
288
289 callout_init(&sc->epe_tick_ch, 0);
290
291 /* Select primary Individual Address in Address Filter Pointer */
292 EPE_WRITE(AFP, 0);
293 /* Read ethernet MAC, should already be set by bootrom */
294 bus_space_read_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
295 sc->sc_enaddr, ETHER_ADDR_LEN);
296 aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
297 ether_sprintf(sc->sc_enaddr));
298
299 /* Soft Reset the MAC */
300 EPE_WRITE(SelfCtl, SelfCtl_RESET);
301 while (EPE_READ(SelfCtl) & SelfCtl_RESET)
302 ;
303
304 /* suggested magic initialization values from datasheet */
305 EPE_WRITE(RXBufThrshld, 0x800040);
306 EPE_WRITE(TXBufThrshld, 0x200010);
307 EPE_WRITE(RXStsThrshld, 0x40002);
308 EPE_WRITE(TXStsThrshld, 0x40002);
309 EPE_WRITE(RXDThrshld, 0x40002);
310 EPE_WRITE(TXDThrshld, 0x40002);
311
312 /* Allocate a page of memory for descriptor and status queues */
313 err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
314 &segs, 1, &rsegs, BUS_DMA_WAITOK);
315 if (err == 0) {
316 err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
317 &sc->ctrlpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
318 }
319 if (err == 0) {
320 err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
321 0, BUS_DMA_WAITOK, &sc->ctrlpage_dmamap);
322 }
323 if (err == 0) {
324 err = bus_dmamap_load(sc->sc_dmat, sc->ctrlpage_dmamap,
325 sc->ctrlpage, PAGE_SIZE, NULL, BUS_DMA_WAITOK);
326 }
327 if (err != 0) {
328 panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
329 }
330 sc->ctrlpage_dsaddr = sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
331 memset(sc->ctrlpage, 0, PAGE_SIZE);
332
333 /* Set up pointers to start of each queue in kernel addr space.
334 * Each descriptor queue or status queue entry uses 2 words
335 */
336 sc->TXDQ = (uint32_t *)sc->ctrlpage;
337 sc->TXDQ_cur = sc->TXDQ;
338 sc->TXDQ_avail = TX_QLEN - 1;
339 sc->TXStsQ = &sc->TXDQ[TX_QLEN * 2];
340 sc->TXStsQ_cur = sc->TXStsQ;
341 sc->RXDQ = &sc->TXStsQ[TX_QLEN];
342 sc->RXStsQ = &sc->RXDQ[RX_QLEN * 2];
343 sc->RXStsQ_cur = sc->RXStsQ;
344
345 /* Program each queue's start addr, cur addr, and len registers
346 * with the physical addresses.
347 */
348 addr = (char *)sc->ctrlpage_dmamap->dm_segs[0].ds_addr;
349 EPE_WRITE(TXDQBAdd, (uint32_t)addr);
350 EPE_WRITE(TXDQCurAdd, (uint32_t)addr);
351 EPE_WRITE(TXDQBLen, TX_QLEN * 2 * sizeof(uint32_t));
352
353 addr += (sc->TXStsQ - sc->TXDQ) * sizeof(uint32_t);
354 EPE_WRITE(TXStsQBAdd, (uint32_t)addr);
355 EPE_WRITE(TXStsQCurAdd, (uint32_t)addr);
356 EPE_WRITE(TXStsQBLen, TX_QLEN * sizeof(uint32_t));
357
358 addr += (sc->RXDQ - sc->TXStsQ) * sizeof(uint32_t);
359 EPE_WRITE(RXDQBAdd, (uint32_t)addr);
360 EPE_WRITE(RXDCurAdd, (uint32_t)addr);
361 EPE_WRITE(RXDQBLen, RX_QLEN * 2 * sizeof(uint32_t));
362
363 addr += (sc->RXStsQ - sc->RXDQ) * sizeof(uint32_t);
364 EPE_WRITE(RXStsQBAdd, (uint32_t)addr);
365 EPE_WRITE(RXStsQCurAdd, (uint32_t)addr);
366 EPE_WRITE(RXStsQBLen, RX_QLEN * 2 * sizeof(uint32_t));
367
368 /* Populate the RXDQ with mbufs */
369 for (i = 0; i < RX_QLEN; i++) {
370 struct mbuf *m;
371
372 bus_dmamap_create(sc->sc_dmat, MCLBYTES, TX_QLEN/4, MCLBYTES,
373 0, BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
374 MGETHDR(m, M_WAIT, MT_DATA);
375 MCLGET(m, M_WAIT);
376 sc->rxq[i].m = m;
377 bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
378 m->m_ext.ext_buf, MCLBYTES, NULL, BUS_DMA_WAITOK);
379
380 sc->RXDQ[i * 2] = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr;
381 sc->RXDQ[i * 2 + 1] = (i << 16) | MCLBYTES;
382 bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
383 MCLBYTES, BUS_DMASYNC_PREREAD);
384 }
385
386 for (i = 0; i < TX_QLEN; i++) {
387 bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
388 (BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
389 &sc->txq[i].m_dmamap);
390 sc->txq[i].m = NULL;
391 sc->TXDQ[i * 2 + 1] = (i << 16);
392 }
393
394 /* Divide HCLK by 32 for MDC clock */
395 if (device_cfdata(sc->sc_dev)->cf_flags)
396 mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
397 EPE_WRITE(SelfCtl, (SelfCtl_MDCDIV(mdcdiv) | SelfCtl_PSPRS));
398
399 mii->mii_ifp = ifp;
400 mii->mii_readreg = epe_mii_readreg;
401 mii->mii_writereg = epe_mii_writereg;
402 mii->mii_statchg = epe_statchg;
403 sc->sc_ec.ec_mii = mii;
404 ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
405 ether_mediastatus);
406 mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
407 MII_OFFSET_ANY, 0);
408 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
409
410 EPE_WRITE(BMCtl, BMCtl_RxEn | BMCtl_TxEn);
411 EPE_WRITE(IntEn, IntEn_REOFIE);
412 /* maximum valid max frame length */
413 EPE_WRITE(MaxFrmLen, (0x7ff << 16) | MHLEN);
414 /* wait for receiver ready */
415 while ((EPE_READ(BMSts) & BMSts_RxAct) == 0)
416 continue;
417 /* enqueue the entries in RXStsQ and RXDQ */
418 CTRLPAGE_DMASYNC(0, sc->ctrlpage_dmamap->dm_mapsize,
419 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
420 EPE_WRITE(RXDEnq, RX_QLEN - 1);
421 EPE_WRITE(RXStsEnq, RX_QLEN - 1);
422
423 /*
424 * We can support 802.1Q VLAN-sized frames.
425 */
426 sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
427
428 strcpy(ifp->if_xname, device_xname(sc->sc_dev));
429 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
430 ifp->if_ioctl = epe_ifioctl;
431 ifp->if_start = epe_ifstart;
432 ifp->if_watchdog = epe_ifwatchdog;
433 ifp->if_init = epe_ifinit;
434 ifp->if_stop = epe_ifstop;
435 ifp->if_timer = 0;
436 ifp->if_softc = sc;
437 IFQ_SET_READY(&ifp->if_snd);
438 if_attach(ifp);
439 if_deferred_start_init(ifp, NULL);
440 ether_ifattach(ifp, (sc)->sc_enaddr);
441 }
442
443 int
444 epe_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
445 {
446 uint32_t d;
447
448 d = EPE_READ(SelfCtl);
449 EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
450 EPE_WRITE(MIICmd, (MIICmd_READ | (phy << 5) | reg));
451 while (EPE_READ(MIISts) & MIISts_BUSY)
452 ;
453 *val = EPE_READ(MIIData) & 0xffff;
454 EPE_WRITE(SelfCtl, d); /* restore old value */
455 return 0;
456 }
457
458 int
459 epe_mii_writereg(device_t self, int phy, int reg, uint16_t val)
460 {
461 uint32_t d;
462
463 d = EPE_READ(SelfCtl);
464 EPE_WRITE(SelfCtl, d & ~SelfCtl_PSPRS); /* no preamble suppress */
465 EPE_WRITE(MIIData, val);
466 EPE_WRITE(MIICmd, (MIICmd_WRITE | (phy << 5) | reg));
467 while (EPE_READ(MIISts) & MIISts_BUSY)
468 ;
469 EPE_WRITE(SelfCtl, d); /* restore old value */
470
471 return 0;
472 }
473
474 void
475 epe_statchg(struct ifnet *ifp)
476 {
477 struct epe_softc *sc = ifp->if_softc;
478 uint32_t reg;
479
480 /*
481 * We must keep the MAC and the PHY in sync as
482 * to the status of full-duplex!
483 */
484 reg = EPE_READ(TestCtl);
485 if (sc->sc_mii.mii_media_active & IFM_FDX)
486 reg |= TestCtl_MFDX;
487 else
488 reg &= ~TestCtl_MFDX;
489 EPE_WRITE(TestCtl, reg);
490 }
491
492 void
493 epe_tick(void *arg)
494 {
495 struct epe_softc* sc = (struct epe_softc *)arg;
496 struct ifnet * ifp = &sc->sc_ec.ec_if;
497 int s;
498 uint32_t misses;
499
500 if_statadd(ifp, if_collisions, EPE_READ(TXCollCnt));
501 /* These misses are ok, they will happen if the RAM/CPU can't keep up */
502 misses = EPE_READ(RXMissCnt);
503 if (misses > 0)
504 printf("%s: %d rx misses\n", device_xname(sc->sc_dev), misses);
505
506 s = splnet();
507 if (epe_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0) {
508 epe_ifstart(ifp);
509 }
510 splx(s);
511
512 mii_tick(&sc->sc_mii);
513 callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
514 }
515
516
517 static int
518 epe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
519 {
520 int s, error;
521
522 s = splnet();
523 error = ether_ioctl(ifp, cmd, data);
524 if (error == ENETRESET) {
525 if (ifp->if_flags & IFF_RUNNING)
526 epe_setaddr(ifp);
527 error = 0;
528 }
529 splx(s);
530 return error;
531 }
532
533 static void
534 epe_ifstart(struct ifnet *ifp)
535 {
536 struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
537 struct mbuf *m;
538 bus_dma_segment_t *segs;
539 int s, bi, err, nsegs, ndq;
540
541 s = splnet();
542 start:
543 ndq = 0;
544 if (sc->TXDQ_avail == 0) {
545 if (epe_gctx(sc) == 0) {
546 /* Enable End-Of-TX-Chain interrupt */
547 EPE_WRITE(IntEn, IntEn_REOFIE | IntEn_ECIE);
548 ifp->if_flags |= IFF_OACTIVE;
549 ifp->if_timer = 10;
550 splx(s);
551 return;
552 }
553 }
554
555 bi = sc->TXDQ_cur - sc->TXDQ;
556
557 IFQ_POLL(&ifp->if_snd, m);
558 if (m == NULL) {
559 splx(s);
560 return;
561 }
562 more:
563 if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
564 BUS_DMA_NOWAIT)) ||
565 sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
566 sc->txq[bi].m_dmamap->dm_nsegs > (sc->TXDQ_avail - ndq)) {
567 /* Copy entire mbuf chain to new and 32-bit aligned storage */
568 struct mbuf *mn;
569
570 if (err == 0)
571 bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
572
573 MGETHDR(mn, M_DONTWAIT, MT_DATA);
574 if (mn == NULL) goto stop;
575 if (m->m_pkthdr.len > (MHLEN & (~0x3))) {
576 MCLGET(mn, M_DONTWAIT);
577 if ((mn->m_flags & M_EXT) == 0) {
578 m_freem(mn);
579 goto stop;
580 }
581 }
582 mn->m_data = (void *)(((uint32_t)mn->m_data + 0x3) & (~0x3));
583 m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
584 mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
585 IFQ_DEQUEUE(&ifp->if_snd, m);
586 m_freem(m);
587 m = mn;
588 bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
589 BUS_DMA_NOWAIT);
590 } else {
591 IFQ_DEQUEUE(&ifp->if_snd, m);
592 }
593
594 bpf_mtap(ifp, m, BPF_D_OUT);
595
596 nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
597 segs = sc->txq[bi].m_dmamap->dm_segs;
598 bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
599 sc->txq[bi].m_dmamap->dm_mapsize,
600 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
601
602 /* XXX: This driver hasn't been tested w/nsegs > 1 */
603 while (nsegs > 0) {
604 nsegs--;
605 sc->txq[bi].m = m;
606 sc->TXDQ[bi * 2] = segs->ds_addr;
607 if (nsegs == 0)
608 sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16) |
609 (1 << 31);
610 else
611 sc->TXDQ[bi * 2 + 1] = segs->ds_len | (bi << 16);
612 segs++;
613 bi = (bi + 1) % TX_QLEN;
614 ndq++;
615 }
616
617
618 /*
619 * Enqueue another. Don't do more than half the available
620 * descriptors before telling the MAC about them
621 */
622 if ((sc->TXDQ_avail - ndq) > 0 && ndq < TX_QLEN / 2) {
623 IFQ_POLL(&ifp->if_snd, m);
624 if (m != NULL)
625 goto more;
626 }
627 stop:
628 if (ndq > 0) {
629 sc->TXDQ_avail -= ndq;
630 sc->TXDQ_cur = &sc->TXDQ[bi];
631 CTRLPAGE_DMASYNC(0, TX_QLEN * 2 * sizeof(uint32_t),
632 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
633 EPE_WRITE(TXDEnq, ndq);
634 }
635
636 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
637 goto start;
638
639 splx(s);
640 return;
641 }
642
643 static void
644 epe_ifwatchdog(struct ifnet *ifp)
645 {
646 struct epe_softc *sc = (struct epe_softc *)ifp->if_softc;
647
648 if ((ifp->if_flags & IFF_RUNNING) == 0)
649 return;
650 printf("%s: device timeout, BMCtl = 0x%08x, BMSts = 0x%08x\n",
651 device_xname(sc->sc_dev), EPE_READ(BMCtl), EPE_READ(BMSts));
652 }
653
654 static int
655 epe_ifinit(struct ifnet *ifp)
656 {
657 struct epe_softc *sc = ifp->if_softc;
658 int rc, s = splnet();
659
660 callout_stop(&sc->epe_tick_ch);
661 EPE_WRITE(RXCtl, RXCtl_IA0 | RXCtl_BA | RXCtl_RCRCA | RXCtl_SRxON);
662 EPE_WRITE(TXCtl, TXCtl_STxON);
663 EPE_WRITE(GIIntMsk, GIIntMsk_INT); /* start interrupting */
664
665 if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
666 rc = 0;
667 else if (rc != 0)
668 goto out;
669
670 callout_reset(&sc->epe_tick_ch, hz, epe_tick, sc);
671 ifp->if_flags |= IFF_RUNNING;
672 out:
673 splx(s);
674 return 0;
675 }
676
677 static void
678 epe_ifstop(struct ifnet *ifp, int disable)
679 {
680 struct epe_softc *sc = ifp->if_softc;
681
682
683 EPE_WRITE(RXCtl, 0);
684 EPE_WRITE(TXCtl, 0);
685 EPE_WRITE(GIIntMsk, 0);
686 callout_stop(&sc->epe_tick_ch);
687
688 /* Down the MII. */
689 mii_down(&sc->sc_mii);
690
691 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
692 ifp->if_timer = 0;
693 sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
694 }
695
696 static void
697 epe_setaddr(struct ifnet *ifp)
698 {
699 struct epe_softc *sc = ifp->if_softc;
700 struct ethercom *ec = &sc->sc_ec;
701 struct ether_multi *enm;
702 struct ether_multistep step;
703 uint8_t ias[2][ETHER_ADDR_LEN];
704 uint32_t h, nma = 0, hashes[2] = { 0, 0 };
705 uint32_t rxctl = EPE_READ(RXCtl);
706
707 /* disable receiver temporarily */
708 EPE_WRITE(RXCtl, rxctl & ~RXCtl_SRxON);
709
710 rxctl &= ~(RXCtl_MA | RXCtl_PA | RXCtl_IA2 | RXCtl_IA3);
711
712 if (ifp->if_flags & IFF_PROMISC)
713 rxctl |= RXCtl_PA;
714
715 ifp->if_flags &= ~IFF_ALLMULTI;
716
717 ETHER_LOCK(ec);
718 ETHER_FIRST_MULTI(step, ec, enm);
719 while (enm != NULL) {
720 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
721 /*
722 * We must listen to a range of multicast addresses.
723 * For now, just accept all multicasts, rather than
724 * trying to set only those filter bits needed to match
725 * the range. (At this time, the only use of address
726 * ranges is for IP multicast routing, for which the
727 * range is big enough to require all bits set.)
728 */
729 rxctl &= ~(RXCtl_IA2 | RXCtl_IA3);
730 rxctl |= RXCtl_MA;
731 hashes[0] = 0xffffffffUL;
732 hashes[1] = 0xffffffffUL;
733 ifp->if_flags |= IFF_ALLMULTI;
734 break;
735 }
736
737 if (nma < 2) {
738 /* We can program 2 perfect address filters for mcast */
739 memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
740 rxctl |= (1 << (nma + 2));
741 } else {
742 /*
743 * XXX: Datasheet is not very clear here, I'm not sure
744 * if I'm doing this right. --joff
745 */
746 h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
747
748 /* Just want the 6 most-significant bits. */
749 h = h >> 26;
750
751 hashes[ h / 32 ] |= (1 << (h % 32));
752 rxctl |= RXCtl_MA;
753 }
754 ETHER_NEXT_MULTI(step, enm);
755 nma++;
756 }
757 ETHER_UNLOCK(ec);
758
759 EPE_WRITE(AFP, 0);
760 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
761 sc->sc_enaddr, ETHER_ADDR_LEN);
762 if (rxctl & RXCtl_IA2) {
763 EPE_WRITE(AFP, 2);
764 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
765 ias[0], ETHER_ADDR_LEN);
766 }
767 if (rxctl & RXCtl_IA3) {
768 EPE_WRITE(AFP, 3);
769 bus_space_write_region_1(sc->sc_iot, sc->sc_ioh, EPE_IndAd,
770 ias[1], ETHER_ADDR_LEN);
771 }
772 if (hashes[0] != 0 && hashes[1] != 0) {
773 EPE_WRITE(AFP, 7);
774 EPE_WRITE(HashTbl, hashes[0]);
775 EPE_WRITE(HashTbl + 4, hashes[1]);
776 }
777 EPE_WRITE(RXCtl, rxctl);
778 }
779