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