smc83c170.c revision 1.17 1 /* $NetBSD: smc83c170.c,v 1.17 1999/06/18 17:44:06 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Device driver for the Standard Microsystems Corp. 83C170
42 * Ethernet PCI Integrated Controller (EPIC/100).
43 */
44
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/mbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_media.h>
62 #include <net/if_ether.h>
63
64 #if NBPFILTER > 0
65 #include <net/bpf.h>
66 #endif
67
68 #ifdef INET
69 #include <netinet/in.h>
70 #include <netinet/if_inarp.h>
71 #endif
72
73 #ifdef NS
74 #include <netns/ns.h>
75 #include <netns/ns_if.h>
76 #endif
77
78 #include <machine/bus.h>
79 #include <machine/intr.h>
80
81 #include <dev/mii/miivar.h>
82
83 #include <dev/ic/smc83c170reg.h>
84 #include <dev/ic/smc83c170var.h>
85
86 void epic_start __P((struct ifnet *));
87 void epic_watchdog __P((struct ifnet *));
88 int epic_ioctl __P((struct ifnet *, u_long, caddr_t));
89
90 void epic_shutdown __P((void *));
91
92 void epic_reset __P((struct epic_softc *));
93 void epic_init __P((struct epic_softc *));
94 void epic_stop __P((struct epic_softc *));
95 int epic_add_rxbuf __P((struct epic_softc *, int));
96 void epic_read_eeprom __P((struct epic_softc *, int, int, u_int16_t *));
97 void epic_set_mchash __P((struct epic_softc *));
98 void epic_fixup_clock_source __P((struct epic_softc *));
99 int epic_mii_read __P((struct device *, int, int));
100 void epic_mii_write __P((struct device *, int, int, int));
101 int epic_mii_wait __P((struct epic_softc *, u_int32_t));
102 void epic_tick __P((void *));
103
104 void epic_statchg __P((struct device *));
105 int epic_mediachange __P((struct ifnet *));
106 void epic_mediastatus __P((struct ifnet *, struct ifmediareq *));
107
108 #define INTMASK (INTSTAT_FATAL_INT | INTSTAT_TXU | \
109 INTSTAT_TXC | INTSTAT_RQE | INTSTAT_RCC)
110
111 /*
112 * Attach an EPIC interface to the system.
113 */
114 void
115 epic_attach(sc)
116 struct epic_softc *sc;
117 {
118 bus_space_tag_t st = sc->sc_st;
119 bus_space_handle_t sh = sc->sc_sh;
120 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
121 int i, rseg, error;
122 bus_dma_segment_t seg;
123 u_int8_t enaddr[ETHER_ADDR_LEN], devname[12 + 1];
124 u_int16_t myea[ETHER_ADDR_LEN / 2], mydevname[6];
125
126 /*
127 * Allocate the control data structures, and create and load the
128 * DMA map for it.
129 */
130 if ((error = bus_dmamem_alloc(sc->sc_dmat,
131 sizeof(struct epic_control_data), NBPG, 0, &seg, 1, &rseg,
132 BUS_DMA_NOWAIT)) != 0) {
133 printf("%s: unable to allocate control data, error = %d\n",
134 sc->sc_dev.dv_xname, error);
135 goto fail_0;
136 }
137
138 if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
139 sizeof(struct epic_control_data), (caddr_t *)&sc->sc_control_data,
140 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
141 printf("%s: unable to map control data, error = %d\n",
142 sc->sc_dev.dv_xname, error);
143 goto fail_1;
144 }
145
146 if ((error = bus_dmamap_create(sc->sc_dmat,
147 sizeof(struct epic_control_data), 1,
148 sizeof(struct epic_control_data), 0, BUS_DMA_NOWAIT,
149 &sc->sc_cddmamap)) != 0) {
150 printf("%s: unable to create control data DMA map, "
151 "error = %d\n", sc->sc_dev.dv_xname, error);
152 goto fail_2;
153 }
154
155 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
156 sc->sc_control_data, sizeof(struct epic_control_data), NULL,
157 BUS_DMA_NOWAIT)) != 0) {
158 printf("%s: unable to load control data DMA map, error = %d\n",
159 sc->sc_dev.dv_xname, error);
160 goto fail_3;
161 }
162
163 /*
164 * Create the transmit buffer DMA maps.
165 */
166 for (i = 0; i < EPIC_NTXDESC; i++) {
167 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
168 EPIC_NFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
169 &EPIC_DSTX(sc, i)->ds_dmamap)) != 0) {
170 printf("%s: unable to create tx DMA map %d, "
171 "error = %d\n", sc->sc_dev.dv_xname, i, error);
172 goto fail_4;
173 }
174 }
175
176 /*
177 * Create the recieve buffer DMA maps.
178 */
179 for (i = 0; i < EPIC_NRXDESC; i++) {
180 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
181 MCLBYTES, 0, BUS_DMA_NOWAIT,
182 &EPIC_DSRX(sc, i)->ds_dmamap)) != 0) {
183 printf("%s: unable to create rx DMA map %d, "
184 "error = %d\n", sc->sc_dev.dv_xname, i, error);
185 goto fail_5;
186 }
187 }
188
189 /*
190 * Pre-allocate the receive buffers.
191 */
192 for (i = 0; i < EPIC_NRXDESC; i++) {
193 if ((error = epic_add_rxbuf(sc, i)) != 0) {
194 printf("%s: unable to allocate or map rx buffer %d\n,"
195 " error = %d\n", sc->sc_dev.dv_xname, i, error);
196 goto fail_6;
197 }
198 }
199
200 /*
201 * Bring the chip out of low-power mode and reset it to a known state.
202 */
203 bus_space_write_4(st, sh, EPIC_GENCTL, 0);
204 epic_reset(sc);
205
206 /*
207 * Read the Ethernet address from the EEPROM.
208 */
209 epic_read_eeprom(sc, 0, (sizeof(myea) / sizeof(myea[0])), myea);
210 bcopy(myea, enaddr, sizeof(myea));
211
212 /*
213 * ...and the device name.
214 */
215 epic_read_eeprom(sc, 0x2c, (sizeof(mydevname) / sizeof(mydevname[0])),
216 mydevname);
217 bcopy(mydevname, devname, sizeof(mydevname));
218 devname[sizeof(mydevname)] = '\0';
219 for (i = sizeof(mydevname) - 1; i >= 0; i--) {
220 if (devname[i] == ' ')
221 devname[i] = '\0';
222 else
223 break;
224 }
225
226 printf("%s: %s, Ethernet address %s\n", sc->sc_dev.dv_xname,
227 devname, ether_sprintf(enaddr));
228
229 /*
230 * Initialize our media structures and probe the MII.
231 */
232 sc->sc_mii.mii_ifp = ifp;
233 sc->sc_mii.mii_readreg = epic_mii_read;
234 sc->sc_mii.mii_writereg = epic_mii_write;
235 sc->sc_mii.mii_statchg = epic_statchg;
236 ifmedia_init(&sc->sc_mii.mii_media, 0, epic_mediachange,
237 epic_mediastatus);
238 mii_phy_probe(&sc->sc_dev, &sc->sc_mii, 0xffffffff);
239 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
240 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
241 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
242 } else
243 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
244
245 ifp = &sc->sc_ethercom.ec_if;
246 strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
247 ifp->if_softc = sc;
248 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
249 ifp->if_ioctl = epic_ioctl;
250 ifp->if_start = epic_start;
251 ifp->if_watchdog = epic_watchdog;
252
253 /*
254 * Attach the interface.
255 */
256 if_attach(ifp);
257 ether_ifattach(ifp, enaddr);
258 #if NBPFILTER > 0
259 bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
260 sizeof(struct ether_header));
261 #endif
262
263 /*
264 * Make sure the interface is shutdown during reboot.
265 */
266 sc->sc_sdhook = shutdownhook_establish(epic_shutdown, sc);
267 if (sc->sc_sdhook == NULL)
268 printf("%s: WARNING: unable to establish shutdown hook\n",
269 sc->sc_dev.dv_xname);
270 return;
271
272 /*
273 * Free any resources we've allocated during the failed attach
274 * attempt. Do this in reverse order and fall through.
275 */
276 fail_6:
277 for (i = 0; i < EPIC_NRXDESC; i++) {
278 if (EPIC_DSRX(sc, i)->ds_mbuf != NULL) {
279 bus_dmamap_unload(sc->sc_dmat,
280 EPIC_DSRX(sc, i)->ds_dmamap);
281 m_freem(EPIC_DSRX(sc, i)->ds_mbuf);
282 }
283 }
284 fail_5:
285 for (i = 0; i < EPIC_NRXDESC; i++) {
286 if (EPIC_DSRX(sc, i)->ds_dmamap != NULL)
287 bus_dmamap_destroy(sc->sc_dmat,
288 EPIC_DSRX(sc, i)->ds_dmamap);
289 }
290 fail_4:
291 for (i = 0; i < EPIC_NTXDESC; i++) {
292 if (EPIC_DSTX(sc, i)->ds_dmamap != NULL)
293 bus_dmamap_destroy(sc->sc_dmat,
294 EPIC_DSTX(sc, i)->ds_dmamap);
295 }
296 bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
297 fail_3:
298 bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
299 fail_2:
300 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
301 sizeof(struct epic_control_data));
302 fail_1:
303 bus_dmamem_free(sc->sc_dmat, &seg, rseg);
304 fail_0:
305 return;
306 }
307
308 /*
309 * Shutdown hook. Make sure the interface is stopped at reboot.
310 */
311 void
312 epic_shutdown(arg)
313 void *arg;
314 {
315 struct epic_softc *sc = arg;
316
317 epic_stop(sc);
318 }
319
320 /*
321 * Start packet transmission on the interface.
322 * [ifnet interface function]
323 */
324 void
325 epic_start(ifp)
326 struct ifnet *ifp;
327 {
328 struct epic_softc *sc = ifp->if_softc;
329 struct mbuf *m0, *m;
330 struct epic_txdesc *txd;
331 struct epic_descsoft *ds;
332 struct epic_fraglist *fr;
333 bus_dmamap_t dmamap;
334 int error, firsttx, nexttx, opending, seg;
335
336 /*
337 * Remember the previous txpending and the first transmit
338 * descriptor we use.
339 */
340 opending = sc->sc_txpending;
341 firsttx = EPIC_NEXTTX(sc->sc_txlast);
342
343 /*
344 * Loop through the send queue, setting up transmit descriptors
345 * until we drain the queue, or use up all available transmit
346 * descriptors.
347 */
348 while (sc->sc_txpending < EPIC_NTXDESC) {
349 /*
350 * Grab a packet off the queue.
351 */
352 IF_DEQUEUE(&ifp->if_snd, m0);
353 if (m0 == NULL)
354 break;
355
356 /*
357 * Get the last and next available transmit descriptor.
358 */
359 nexttx = EPIC_NEXTTX(sc->sc_txlast);
360 txd = EPIC_CDTX(sc, nexttx);
361 fr = EPIC_CDFL(sc, nexttx);
362 ds = EPIC_DSTX(sc, nexttx);
363 dmamap = ds->ds_dmamap;
364
365 /*
366 * Load the DMA map. If this fails, the packet either
367 * didn't fit in the alloted number of frags, or we were
368 * short on resources. In this case, we'll copy and try
369 * again.
370 */
371 if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
372 BUS_DMA_NOWAIT) != 0) {
373 MGETHDR(m, M_DONTWAIT, MT_DATA);
374 if (m == NULL) {
375 printf("%s: unable to allocate Tx mbuf\n",
376 sc->sc_dev.dv_xname);
377 IF_PREPEND(&ifp->if_snd, m0);
378 break;
379 }
380 if (m0->m_pkthdr.len > MHLEN) {
381 MCLGET(m, M_DONTWAIT);
382 if ((m->m_flags & M_EXT) == 0) {
383 printf("%s: unable to allocate Tx "
384 "cluster\n", sc->sc_dev.dv_xname);
385 m_freem(m);
386 IF_PREPEND(&ifp->if_snd, m0);
387 break;
388 }
389 }
390 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
391 m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
392 m_freem(m0);
393 m0 = m;
394 error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
395 m0, BUS_DMA_NOWAIT);
396 if (error) {
397 printf("%s: unable to load Tx buffer, "
398 "error = %d\n", sc->sc_dev.dv_xname, error);
399 IF_PREPEND(&ifp->if_snd, m0);
400 break;
401 }
402 }
403
404 /* Initialize the fraglist. */
405 fr->ef_nfrags = dmamap->dm_nsegs;
406 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
407 fr->ef_frags[seg].ef_addr =
408 dmamap->dm_segs[seg].ds_addr;
409 fr->ef_frags[seg].ef_length =
410 dmamap->dm_segs[seg].ds_len;
411 }
412
413 EPIC_CDFLSYNC(sc, nexttx, BUS_DMASYNC_PREWRITE);
414
415 /* Sync the DMA map. */
416 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
417 BUS_DMASYNC_PREWRITE);
418
419 /*
420 * Store a pointer to the packet so we can free it later.
421 */
422 ds->ds_mbuf = m0;
423
424 /*
425 * Fill in the transmit descriptor. The EPIC doesn't
426 * auto-pad, so we have to do this ourselves.
427 */
428 txd->et_control = ET_TXCTL_LASTDESC | ET_TXCTL_FRAGLIST;
429 txd->et_txlength = max(m0->m_pkthdr.len, ETHER_MIN_LEN);
430
431 /*
432 * If this is the first descriptor we're enqueueing,
433 * don't give it to the EPIC yet. That could cause
434 * a race condition. We'll do it below.
435 */
436 if (nexttx == firsttx)
437 txd->et_txstatus = 0;
438 else
439 txd->et_txstatus = ET_TXSTAT_OWNER;
440
441 EPIC_CDTXSYNC(sc, nexttx,
442 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
443
444 /* Advance the tx pointer. */
445 sc->sc_txpending++;
446 sc->sc_txlast = nexttx;
447
448 #if NBPFILTER > 0
449 /*
450 * Pass the packet to any BPF listeners.
451 */
452 if (ifp->if_bpf)
453 bpf_mtap(ifp->if_bpf, m0);
454 #endif
455 }
456
457 if (sc->sc_txpending == EPIC_NTXDESC) {
458 /* No more slots left; notify upper layer. */
459 ifp->if_flags |= IFF_OACTIVE;
460 }
461
462 if (sc->sc_txpending != opending) {
463 /*
464 * We enqueued packets. If the transmitter was idle,
465 * reset the txdirty pointer.
466 */
467 if (opending == 0)
468 sc->sc_txdirty = firsttx;
469
470 /*
471 * Cause a transmit interrupt to happen on the
472 * last packet we enqueued.
473 */
474 EPIC_CDTX(sc, sc->sc_txlast)->et_control |= ET_TXCTL_IAF;
475 EPIC_CDTXSYNC(sc, sc->sc_txlast,
476 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
477
478 /*
479 * The entire packet chain is set up. Give the
480 * first descriptor to the EPIC now.
481 */
482 EPIC_CDTX(sc, firsttx)->et_txstatus = ET_TXSTAT_OWNER;
483 EPIC_CDTXSYNC(sc, firsttx,
484 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
485
486 /* Start the transmitter. */
487 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
488 COMMAND_TXQUEUED);
489
490 /* Set a watchdog timer in case the chip flakes out. */
491 ifp->if_timer = 5;
492 }
493 }
494
495 /*
496 * Watchdog timer handler.
497 * [ifnet interface function]
498 */
499 void
500 epic_watchdog(ifp)
501 struct ifnet *ifp;
502 {
503 struct epic_softc *sc = ifp->if_softc;
504
505 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
506 ifp->if_oerrors++;
507
508 epic_init(sc);
509 }
510
511 /*
512 * Handle control requests from the operator.
513 * [ifnet interface function]
514 */
515 int
516 epic_ioctl(ifp, cmd, data)
517 struct ifnet *ifp;
518 u_long cmd;
519 caddr_t data;
520 {
521 struct epic_softc *sc = ifp->if_softc;
522 struct ifreq *ifr = (struct ifreq *)data;
523 struct ifaddr *ifa = (struct ifaddr *)data;
524 int s, error = 0;
525
526 s = splnet();
527
528 switch (cmd) {
529 case SIOCSIFADDR:
530 ifp->if_flags |= IFF_UP;
531
532 switch (ifa->ifa_addr->sa_family) {
533 #ifdef INET
534 case AF_INET:
535 epic_init(sc);
536 arp_ifinit(ifp, ifa);
537 break;
538 #endif /* INET */
539 #ifdef NS
540 case AF_NS:
541 {
542 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
543
544 if (ns_nullhost(*ina))
545 ina->x_host = *(union ns_host *)
546 LLADDR(ifp->if_sadl);
547 else
548 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
549 ifp->if_addrlen);
550 /* Set new address. */
551 epic_init(sc);
552 break;
553 }
554 #endif /* NS */
555 default:
556 epic_init(sc);
557 break;
558 }
559 break;
560
561 case SIOCSIFMTU:
562 if (ifr->ifr_mtu > ETHERMTU)
563 error = EINVAL;
564 else
565 ifp->if_mtu = ifr->ifr_mtu;
566 break;
567
568 case SIOCSIFFLAGS:
569 if ((ifp->if_flags & IFF_UP) == 0 &&
570 (ifp->if_flags & IFF_RUNNING) != 0) {
571 /*
572 * If interface is marked down and it is running, then
573 * stop it.
574 */
575 epic_stop(sc);
576 } else if ((ifp->if_flags & IFF_UP) != 0 &&
577 (ifp->if_flags & IFF_RUNNING) == 0) {
578 /*
579 * If interfase it marked up and it is stopped, then
580 * start it.
581 */
582 epic_init(sc);
583 } else if ((ifp->if_flags & IFF_UP) != 0) {
584 /*
585 * Reset the interface to pick up changes in any other
586 * flags that affect the hardware state.
587 */
588 epic_init(sc);
589 }
590 break;
591
592 case SIOCADDMULTI:
593 case SIOCDELMULTI:
594 error = (cmd == SIOCADDMULTI) ?
595 ether_addmulti(ifr, &sc->sc_ethercom) :
596 ether_delmulti(ifr, &sc->sc_ethercom);
597
598 if (error == ENETRESET) {
599 /*
600 * Multicast list has changed; set the hardware filter
601 * accordingly. Update our idea of the current media;
602 * epic_set_mchash() needs to know what it is.
603 */
604 mii_pollstat(&sc->sc_mii);
605 epic_set_mchash(sc);
606 error = 0;
607 }
608 break;
609
610 case SIOCSIFMEDIA:
611 case SIOCGIFMEDIA:
612 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
613 break;
614
615 default:
616 error = EINVAL;
617 break;
618 }
619
620 splx(s);
621 return (error);
622 }
623
624 /*
625 * Interrupt handler.
626 */
627 int
628 epic_intr(arg)
629 void *arg;
630 {
631 struct epic_softc *sc = arg;
632 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
633 struct ether_header *eh;
634 struct epic_rxdesc *rxd;
635 struct epic_txdesc *txd;
636 struct epic_descsoft *ds;
637 struct mbuf *m;
638 u_int32_t intstat;
639 int i, len, claimed = 0;
640
641 top:
642 /*
643 * Get the interrupt status from the EPIC.
644 */
645 intstat = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT);
646 if ((intstat & INTSTAT_INT_ACTV) == 0)
647 return (claimed);
648
649 claimed = 1;
650
651 /*
652 * Acknowledge the interrupt.
653 */
654 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_INTSTAT,
655 intstat & INTMASK);
656
657 /*
658 * Check for receive interrupts.
659 */
660 if (intstat & (INTSTAT_RCC | INTSTAT_RQE)) {
661 for (i = sc->sc_rxptr;; i = EPIC_NEXTRX(i)) {
662 rxd = EPIC_CDRX(sc, i);
663 ds = EPIC_DSRX(sc, i);
664
665 EPIC_CDRXSYNC(sc, i,
666 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
667
668 if (rxd->er_rxstatus & ER_RXSTAT_OWNER) {
669 /*
670 * We have processed all of the
671 * receive buffers.
672 */
673 break;
674 }
675
676 /*
677 * Make sure the packet arrived intact. If an error
678 * occurred, update stats and reset the descriptor.
679 * The buffer will be reused the next time the
680 * descriptor comes up in the ring.
681 */
682 if ((rxd->er_rxstatus & ER_RXSTAT_PKTINTACT) == 0) {
683 if (rxd->er_rxstatus & ER_RXSTAT_CRCERROR)
684 printf("%s: CRC error\n",
685 sc->sc_dev.dv_xname);
686 if (rxd->er_rxstatus & ER_RXSTAT_ALIGNERROR)
687 printf("%s: alignment error\n",
688 sc->sc_dev.dv_xname);
689 ifp->if_ierrors++;
690 EPIC_INIT_RXDESC(sc, i);
691 continue;
692 }
693
694 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
695 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
696
697 /*
698 * Add a new buffer to the receive chain. If this
699 * fails, the old buffer is recycled.
700 */
701 m = ds->ds_mbuf;
702 if (epic_add_rxbuf(sc, i) != 0) {
703 ifp->if_ierrors++;
704 EPIC_INIT_RXDESC(sc, i);
705 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
706 ds->ds_dmamap->dm_mapsize,
707 BUS_DMASYNC_PREREAD);
708 continue;
709 }
710
711 len = rxd->er_rxlength;
712 if (len < sizeof(struct ether_header)) {
713 m_freem(m);
714 continue;
715 }
716
717 m->m_pkthdr.rcvif = ifp;
718 m->m_pkthdr.len = m->m_len = len;
719 eh = mtod(m, struct ether_header *);
720
721 #if NBPFILTER > 0
722 /*
723 * Pass this up to any BPF listeners, but only
724 * pass it up the stack if its for us.
725 */
726 if (ifp->if_bpf) {
727 bpf_mtap(ifp->if_bpf, m);
728 if ((ifp->if_flags & IFF_PROMISC) != 0 &&
729 bcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
730 ETHER_ADDR_LEN) != 0 &&
731 (rxd->er_rxstatus &
732 (ER_RXSTAT_BCAST|ER_RXSTAT_MCAST)) == 0) {
733 m_freem(m);
734 continue;
735 }
736 }
737 #endif /* NPBFILTER > 0 */
738
739 /* Pass it on. */
740 (*ifp->if_input)(ifp, m);
741 ifp->if_ipackets++;
742 }
743
744 /* Update the recieve pointer. */
745 sc->sc_rxptr = i;
746
747 /*
748 * Check for receive queue underflow.
749 */
750 if (intstat & INTSTAT_RQE) {
751 printf("%s: receiver queue empty\n",
752 sc->sc_dev.dv_xname);
753 /*
754 * Ring is already built; just restart the
755 * receiver.
756 */
757 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_PRCDAR,
758 EPIC_CDRXADDR(sc, sc->sc_rxptr));
759 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
760 COMMAND_RXQUEUED | COMMAND_START_RX);
761 }
762 }
763
764 /*
765 * Check for transmission complete interrupts.
766 */
767 if (intstat & (INTSTAT_TXC | INTSTAT_TXU)) {
768 ifp->if_flags &= ~IFF_OACTIVE;
769 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
770 i = EPIC_NEXTTX(i), sc->sc_txpending--) {
771 txd = EPIC_CDTX(sc, i);
772 ds = EPIC_DSTX(sc, i);
773
774 EPIC_CDTXSYNC(sc, i,
775 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
776
777 if (txd->et_txstatus & ET_TXSTAT_OWNER)
778 break;
779
780 EPIC_CDFLSYNC(sc, i, BUS_DMASYNC_POSTWRITE);
781
782 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
783 0, ds->ds_dmamap->dm_mapsize,
784 BUS_DMASYNC_POSTWRITE);
785 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
786 m_freem(ds->ds_mbuf);
787 ds->ds_mbuf = NULL;
788
789 /*
790 * Check for errors and collisions.
791 */
792 if ((txd->et_txstatus & ET_TXSTAT_PACKETTX) == 0)
793 ifp->if_oerrors++;
794 else
795 ifp->if_opackets++;
796 ifp->if_collisions +=
797 TXSTAT_COLLISIONS(txd->et_txstatus);
798 if (txd->et_txstatus & ET_TXSTAT_CARSENSELOST)
799 printf("%s: lost carrier\n",
800 sc->sc_dev.dv_xname);
801 }
802
803 /* Update the dirty transmit buffer pointer. */
804 sc->sc_txdirty = i;
805
806 /*
807 * Cancel the watchdog timer if there are no pending
808 * transmissions.
809 */
810 if (sc->sc_txpending == 0)
811 ifp->if_timer = 0;
812
813 /*
814 * Kick the transmitter after a DMA underrun.
815 */
816 if (intstat & INTSTAT_TXU) {
817 printf("%s: transmit underrun\n", sc->sc_dev.dv_xname);
818 bus_space_write_4(sc->sc_st, sc->sc_sh,
819 EPIC_COMMAND, COMMAND_TXUGO);
820 if (sc->sc_txpending)
821 bus_space_write_4(sc->sc_st, sc->sc_sh,
822 EPIC_COMMAND, COMMAND_TXQUEUED);
823 }
824
825 /*
826 * Try to get more packets going.
827 */
828 epic_start(ifp);
829 }
830
831 /*
832 * Check for fatal interrupts.
833 */
834 if (intstat & INTSTAT_FATAL_INT) {
835 printf("%s: fatal error, resetting\n", sc->sc_dev.dv_xname);
836 epic_init(sc);
837 }
838
839 /*
840 * Check for more interrupts.
841 */
842 goto top;
843 }
844
845 /*
846 * One second timer, used to tick the MII.
847 */
848 void
849 epic_tick(arg)
850 void *arg;
851 {
852 struct epic_softc *sc = arg;
853 int s;
854
855 s = splnet();
856 mii_tick(&sc->sc_mii);
857 splx(s);
858
859 timeout(epic_tick, sc, hz);
860 }
861
862 /*
863 * Fixup the clock source on the EPIC.
864 */
865 void
866 epic_fixup_clock_source(sc)
867 struct epic_softc *sc;
868 {
869 int i;
870
871 /*
872 * According to SMC Application Note 7-15, the EPIC's clock
873 * source is incorrect following a reset. This manifests itself
874 * as failure to recognize when host software has written to
875 * a register on the EPIC. The appnote recommends issuing at
876 * least 16 consecutive writes to the CLOCK TEST bit to correctly
877 * configure the clock source.
878 */
879 for (i = 0; i < 16; i++)
880 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TEST,
881 TEST_CLOCKTEST);
882 }
883
884 /*
885 * Perform a soft reset on the EPIC.
886 */
887 void
888 epic_reset(sc)
889 struct epic_softc *sc;
890 {
891
892 epic_fixup_clock_source(sc);
893
894 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, 0);
895 delay(100);
896 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, GENCTL_SOFTRESET);
897 delay(100);
898
899 epic_fixup_clock_source(sc);
900 }
901
902 /*
903 * Initialize the interface. Must be called at splnet().
904 */
905 void
906 epic_init(sc)
907 struct epic_softc *sc;
908 {
909 bus_space_tag_t st = sc->sc_st;
910 bus_space_handle_t sh = sc->sc_sh;
911 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
912 u_int8_t *enaddr = LLADDR(ifp->if_sadl);
913 struct epic_txdesc *txd;
914 u_int32_t genctl, reg0;
915 int i;
916
917 /*
918 * Cancel any pending I/O.
919 */
920 epic_stop(sc);
921
922 /*
923 * Reset the EPIC to a known state.
924 */
925 epic_reset(sc);
926
927 /*
928 * Magical mystery initialization.
929 */
930 bus_space_write_4(st, sh, EPIC_TXTEST, 0);
931
932 /*
933 * Initialize the EPIC genctl register:
934 *
935 * - 64 byte receive FIFO threshold
936 * - automatic advance to next receive frame
937 */
938 genctl = GENCTL_RX_FIFO_THRESH0 | GENCTL_ONECOPY;
939 bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
940
941 /*
942 * Reset the MII bus and PHY.
943 */
944 reg0 = bus_space_read_4(st, sh, EPIC_NVCTL);
945 bus_space_write_4(st, sh, EPIC_NVCTL, reg0 | NVCTL_GPIO1 | NVCTL_GPOE1);
946 bus_space_write_4(st, sh, EPIC_MIICFG, MIICFG_ENASER);
947 bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_RESET_PHY);
948 delay(100);
949 bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
950 delay(100);
951 bus_space_write_4(st, sh, EPIC_NVCTL, reg0);
952
953 /*
954 * Initialize Ethernet address.
955 */
956 reg0 = enaddr[1] << 8 | enaddr[0];
957 bus_space_write_4(st, sh, EPIC_LAN0, reg0);
958 reg0 = enaddr[3] << 8 | enaddr[2];
959 bus_space_write_4(st, sh, EPIC_LAN1, reg0);
960 reg0 = enaddr[5] << 8 | enaddr[4];
961 bus_space_write_4(st, sh, EPIC_LAN2, reg0);
962
963 /*
964 * Initialize receive control. Remember the external buffer
965 * size setting.
966 */
967 reg0 = bus_space_read_4(st, sh, EPIC_RXCON) &
968 (RXCON_EXTBUFSIZESEL1 | RXCON_EXTBUFSIZESEL0);
969 reg0 |= (RXCON_RXMULTICAST | RXCON_RXBROADCAST);
970 if (ifp->if_flags & IFF_PROMISC)
971 reg0 |= RXCON_PROMISCMODE;
972 bus_space_write_4(st, sh, EPIC_RXCON, reg0);
973
974 /* Set the current media. */
975 mii_mediachg(&sc->sc_mii);
976
977 /* Set up the multicast hash table. */
978 epic_set_mchash(sc);
979
980 /*
981 * Initialize the transmit descriptor ring. txlast is initialized
982 * to the end of the list so that it will wrap around to the first
983 * descriptor when the first packet is transmitted.
984 */
985 for (i = 0; i < EPIC_NTXDESC; i++) {
986 txd = EPIC_CDTX(sc, i);
987 memset(txd, 0, sizeof(struct epic_txdesc));
988 txd->et_bufaddr = EPIC_CDFLADDR(sc, i);
989 txd->et_nextdesc = EPIC_CDTXADDR(sc, EPIC_NEXTTX(i));
990 EPIC_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
991 }
992 sc->sc_txpending = 0;
993 sc->sc_txdirty = 0;
994 sc->sc_txlast = EPIC_NTXDESC - 1;
995
996 /*
997 * Initialize the receive descriptor ring. The buffers are
998 * already allocated.
999 */
1000 for (i = 0; i < EPIC_NRXDESC; i++)
1001 EPIC_INIT_RXDESC(sc, i);
1002 sc->sc_rxptr = 0;
1003
1004 /*
1005 * Initialize the interrupt mask and enable interrupts.
1006 */
1007 bus_space_write_4(st, sh, EPIC_INTMASK, INTMASK);
1008 bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_INTENA);
1009
1010 /*
1011 * Give the transmit and receive rings to the EPIC.
1012 */
1013 bus_space_write_4(st, sh, EPIC_PTCDAR,
1014 EPIC_CDTXADDR(sc, EPIC_NEXTTX(sc->sc_txlast)));
1015 bus_space_write_4(st, sh, EPIC_PRCDAR,
1016 EPIC_CDRXADDR(sc, sc->sc_rxptr));
1017
1018 /*
1019 * Set the EPIC in motion.
1020 */
1021 bus_space_write_4(st, sh, EPIC_COMMAND,
1022 COMMAND_RXQUEUED | COMMAND_START_RX);
1023
1024 /*
1025 * ...all done!
1026 */
1027 ifp->if_flags |= IFF_RUNNING;
1028 ifp->if_flags &= ~IFF_OACTIVE;
1029
1030 /*
1031 * Start the one second clock.
1032 */
1033 timeout(epic_tick, sc, hz);
1034
1035 /*
1036 * Attempt to start output on the interface.
1037 */
1038 epic_start(ifp);
1039 }
1040
1041 /*
1042 * Stop transmission on the interface.
1043 */
1044 void
1045 epic_stop(sc)
1046 struct epic_softc *sc;
1047 {
1048 bus_space_tag_t st = sc->sc_st;
1049 bus_space_handle_t sh = sc->sc_sh;
1050 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1051 struct epic_descsoft *ds;
1052 u_int32_t reg;
1053 int i;
1054
1055 /*
1056 * Stop the one second clock.
1057 */
1058 untimeout(epic_tick, sc);
1059
1060 /* Paranoia... */
1061 epic_fixup_clock_source(sc);
1062
1063 /*
1064 * Disable interrupts.
1065 */
1066 reg = bus_space_read_4(st, sh, EPIC_GENCTL);
1067 bus_space_write_4(st, sh, EPIC_GENCTL, reg & ~GENCTL_INTENA);
1068 bus_space_write_4(st, sh, EPIC_INTMASK, 0);
1069
1070 /*
1071 * Stop the DMA engine and take the receiver off-line.
1072 */
1073 bus_space_write_4(st, sh, EPIC_COMMAND, COMMAND_STOP_RDMA |
1074 COMMAND_STOP_TDMA | COMMAND_STOP_RX);
1075
1076 /*
1077 * Release any queued transmit buffers.
1078 */
1079 for (i = 0; i < EPIC_NTXDESC; i++) {
1080 ds = EPIC_DSTX(sc, i);
1081 if (ds->ds_mbuf != NULL) {
1082 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1083 m_freem(ds->ds_mbuf);
1084 ds->ds_mbuf = NULL;
1085 }
1086 }
1087
1088 /*
1089 * Mark the interface down and cancel the watchdog timer.
1090 */
1091 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1092 ifp->if_timer = 0;
1093 }
1094
1095 /*
1096 * Read the EPIC Serial EEPROM.
1097 */
1098 void
1099 epic_read_eeprom(sc, word, wordcnt, data)
1100 struct epic_softc *sc;
1101 int word, wordcnt;
1102 u_int16_t *data;
1103 {
1104 bus_space_tag_t st = sc->sc_st;
1105 bus_space_handle_t sh = sc->sc_sh;
1106 u_int16_t reg;
1107 int i, x;
1108
1109 #define EEPROM_WAIT_READY(st, sh) \
1110 while ((bus_space_read_4((st), (sh), EPIC_EECTL) & EECTL_EERDY) == 0) \
1111 /* nothing */
1112
1113 /*
1114 * Enable the EEPROM.
1115 */
1116 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1117 EEPROM_WAIT_READY(st, sh);
1118
1119 for (i = 0; i < wordcnt; i++) {
1120 /* Send CHIP SELECT for one clock tick. */
1121 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE|EECTL_EECS);
1122 EEPROM_WAIT_READY(st, sh);
1123
1124 /* Shift in the READ opcode. */
1125 for (x = 3; x > 0; x--) {
1126 reg = EECTL_ENABLE|EECTL_EECS;
1127 if (EPIC_EEPROM_OPC_READ & (1 << (x - 1)))
1128 reg |= EECTL_EEDI;
1129 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1130 EEPROM_WAIT_READY(st, sh);
1131 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1132 EEPROM_WAIT_READY(st, sh);
1133 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1134 EEPROM_WAIT_READY(st, sh);
1135 }
1136
1137 /* Shift in address. */
1138 for (x = 6; x > 0; x--) {
1139 reg = EECTL_ENABLE|EECTL_EECS;
1140 if ((word + i) & (1 << (x - 1)))
1141 reg |= EECTL_EEDI;
1142 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1143 EEPROM_WAIT_READY(st, sh);
1144 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1145 EEPROM_WAIT_READY(st, sh);
1146 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1147 EEPROM_WAIT_READY(st, sh);
1148 }
1149
1150 /* Shift out data. */
1151 reg = EECTL_ENABLE|EECTL_EECS;
1152 data[i] = 0;
1153 for (x = 16; x > 0; x--) {
1154 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1155 EEPROM_WAIT_READY(st, sh);
1156 if (bus_space_read_4(st, sh, EPIC_EECTL) & EECTL_EEDO)
1157 data[i] |= (1 << (x - 1));
1158 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1159 EEPROM_WAIT_READY(st, sh);
1160 }
1161
1162 /* Clear CHIP SELECT. */
1163 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1164 EEPROM_WAIT_READY(st, sh);
1165 }
1166
1167 /*
1168 * Disable the EEPROM.
1169 */
1170 bus_space_write_4(st, sh, EPIC_EECTL, 0);
1171
1172 #undef EEPROM_WAIT_READY
1173 }
1174
1175 /*
1176 * Add a receive buffer to the indicated descriptor.
1177 */
1178 int
1179 epic_add_rxbuf(sc, idx)
1180 struct epic_softc *sc;
1181 int idx;
1182 {
1183 struct epic_descsoft *ds = EPIC_DSRX(sc, idx);
1184 struct mbuf *m;
1185 int error;
1186
1187 MGETHDR(m, M_DONTWAIT, MT_DATA);
1188 if (m == NULL)
1189 return (ENOBUFS);
1190
1191 MCLGET(m, M_DONTWAIT);
1192 if ((m->m_flags & M_EXT) == 0) {
1193 m_freem(m);
1194 return (ENOBUFS);
1195 }
1196
1197 if (ds->ds_mbuf != NULL)
1198 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1199
1200 ds->ds_mbuf = m;
1201
1202 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1203 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1204 if (error) {
1205 printf("%s: can't load rx DMA map %d, error = %d\n",
1206 sc->sc_dev.dv_xname, idx, error);
1207 panic("epic_add_rxbuf"); /* XXX */
1208 }
1209
1210 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1211 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1212
1213 EPIC_INIT_RXDESC(sc, idx);
1214
1215 return (0);
1216 }
1217
1218 /*
1219 * Set the EPIC multicast hash table.
1220 *
1221 * NOTE: We rely on a recently-updated mii_media_active here!
1222 */
1223 void
1224 epic_set_mchash(sc)
1225 struct epic_softc *sc;
1226 {
1227 struct ethercom *ec = &sc->sc_ethercom;
1228 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1229 struct ether_multi *enm;
1230 struct ether_multistep step;
1231 u_int8_t *cp;
1232 u_int32_t crc, mchash[4];
1233 int len;
1234 static const u_int32_t crctab[] = {
1235 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1236 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1237 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1238 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1239 };
1240
1241 /*
1242 * Set up the multicast address filter by passing all multicast
1243 * addresses through a CRC generator, and then using the high-order
1244 * 6 bits as an index into the 64 bit multicast hash table (only
1245 * the lower 16 bits of each 32 bit multicast hash register are
1246 * valid). The high order bit selects the register, while the
1247 * rest of the bits select the bit within the register.
1248 */
1249
1250 if (ifp->if_flags & IFF_PROMISC)
1251 goto allmulti;
1252
1253 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
1254 /* XXX hardware bug in 10Mbps mode. */
1255 goto allmulti;
1256 }
1257
1258 mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0;
1259
1260 ETHER_FIRST_MULTI(step, ec, enm);
1261 while (enm != NULL) {
1262 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1263 /*
1264 * We must listen to a range of multicast addresses.
1265 * For now, just accept all multicasts, rather than
1266 * trying to set only those filter bits needed to match
1267 * the range. (At this time, the only use of address
1268 * ranges is for IP multicast routing, for which the
1269 * range is big enough to require all bits set.)
1270 */
1271 goto allmulti;
1272 }
1273
1274 cp = enm->enm_addrlo;
1275 crc = 0xffffffff;
1276 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1277 crc ^= *cp++;
1278 crc = (crc >> 4) ^ crctab[crc & 0xf];
1279 crc = (crc >> 4) ^ crctab[crc & 0xf];
1280 }
1281 /* Just want the 6 most significant bits. */
1282 crc >>= 26;
1283
1284 /* Set the corresponding bit in the hash table. */
1285 mchash[crc >> 4] |= 1 << (crc & 0xf);
1286
1287 ETHER_NEXT_MULTI(step, enm);
1288 }
1289
1290 ifp->if_flags &= ~IFF_ALLMULTI;
1291 goto sethash;
1292
1293 allmulti:
1294 ifp->if_flags |= IFF_ALLMULTI;
1295 mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0xffff;
1296
1297 sethash:
1298 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC0, mchash[0]);
1299 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC1, mchash[1]);
1300 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC2, mchash[2]);
1301 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC3, mchash[3]);
1302 }
1303
1304 /*
1305 * Wait for the MII to become ready.
1306 */
1307 int
1308 epic_mii_wait(sc, rw)
1309 struct epic_softc *sc;
1310 u_int32_t rw;
1311 {
1312 int i;
1313
1314 for (i = 0; i < 50; i++) {
1315 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL) & rw)
1316 == 0)
1317 break;
1318 delay(2);
1319 }
1320 if (i == 50) {
1321 printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
1322 return (1);
1323 }
1324
1325 return (0);
1326 }
1327
1328 /*
1329 * Read from the MII.
1330 */
1331 int
1332 epic_mii_read(self, phy, reg)
1333 struct device *self;
1334 int phy, reg;
1335 {
1336 struct epic_softc *sc = (struct epic_softc *)self;
1337
1338 if (epic_mii_wait(sc, MMCTL_WRITE))
1339 return (0);
1340
1341 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1342 MMCTL_ARG(phy, reg, MMCTL_READ));
1343
1344 if (epic_mii_wait(sc, MMCTL_READ))
1345 return (0);
1346
1347 return (bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA) &
1348 MMDATA_MASK);
1349 }
1350
1351 /*
1352 * Write to the MII.
1353 */
1354 void
1355 epic_mii_write(self, phy, reg, val)
1356 struct device *self;
1357 int phy, reg, val;
1358 {
1359 struct epic_softc *sc = (struct epic_softc *)self;
1360
1361 if (epic_mii_wait(sc, MMCTL_WRITE))
1362 return;
1363
1364 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA, val);
1365 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1366 MMCTL_ARG(phy, reg, MMCTL_WRITE));
1367 }
1368
1369 /*
1370 * Callback from PHY when media changes.
1371 */
1372 void
1373 epic_statchg(self)
1374 struct device *self;
1375 {
1376 struct epic_softc *sc = (struct epic_softc *)self;
1377 u_int32_t txcon;
1378
1379 /*
1380 * Update loopback bits in TXCON to reflect duplex mode.
1381 */
1382 txcon = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_TXCON);
1383 if (sc->sc_mii.mii_media_active & IFM_FDX)
1384 txcon |= (TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
1385 else
1386 txcon &= ~(TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
1387 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TXCON, txcon);
1388
1389 /*
1390 * There is a multicast filter bug in 10Mbps mode. Kick the
1391 * multicast filter in case the speed changed.
1392 */
1393 epic_set_mchash(sc);
1394
1395 /* XXX Update ifp->if_baudrate */
1396 }
1397
1398 /*
1399 * Callback from ifmedia to request current media status.
1400 */
1401 void
1402 epic_mediastatus(ifp, ifmr)
1403 struct ifnet *ifp;
1404 struct ifmediareq *ifmr;
1405 {
1406 struct epic_softc *sc = ifp->if_softc;
1407
1408 mii_pollstat(&sc->sc_mii);
1409 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1410 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1411 }
1412
1413 /*
1414 * Callback from ifmedia to request new media setting.
1415 */
1416 int
1417 epic_mediachange(ifp)
1418 struct ifnet *ifp;
1419 {
1420 struct epic_softc *sc = ifp->if_softc;
1421
1422 if (ifp->if_flags & IFF_UP)
1423 mii_mediachg(&sc->sc_mii);
1424 return (0);
1425 }
1426