smc83c170.c revision 1.16 1 /* $NetBSD: smc83c170.c,v 1.16 1999/05/18 23:52:56 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_buflength;
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 }
742
743 /* Update the recieve pointer. */
744 sc->sc_rxptr = i;
745
746 /*
747 * Check for receive queue underflow.
748 */
749 if (intstat & INTSTAT_RQE) {
750 printf("%s: receiver queue empty\n",
751 sc->sc_dev.dv_xname);
752 /*
753 * Ring is already built; just restart the
754 * receiver.
755 */
756 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_PRCDAR,
757 EPIC_CDRXADDR(sc, sc->sc_rxptr));
758 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_COMMAND,
759 COMMAND_RXQUEUED | COMMAND_START_RX);
760 }
761 }
762
763 /*
764 * Check for transmission complete interrupts.
765 */
766 if (intstat & (INTSTAT_TXC | INTSTAT_TXU)) {
767 ifp->if_flags &= ~IFF_OACTIVE;
768 for (i = sc->sc_txdirty; sc->sc_txpending != 0;
769 i = EPIC_NEXTTX(i), sc->sc_txpending--) {
770 txd = EPIC_CDTX(sc, i);
771 ds = EPIC_DSTX(sc, i);
772
773 EPIC_CDTXSYNC(sc, i,
774 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
775
776 if (txd->et_txstatus & ET_TXSTAT_OWNER)
777 break;
778
779 EPIC_CDFLSYNC(sc, i, BUS_DMASYNC_POSTWRITE);
780
781 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
782 0, ds->ds_dmamap->dm_mapsize,
783 BUS_DMASYNC_POSTWRITE);
784 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
785 m_freem(ds->ds_mbuf);
786 ds->ds_mbuf = NULL;
787
788 /*
789 * Check for errors and collisions.
790 */
791 if ((txd->et_txstatus & ET_TXSTAT_PACKETTX) == 0)
792 ifp->if_oerrors++;
793 else
794 ifp->if_opackets++;
795 ifp->if_collisions +=
796 TXSTAT_COLLISIONS(txd->et_txstatus);
797 if (txd->et_txstatus & ET_TXSTAT_CARSENSELOST)
798 printf("%s: lost carrier\n",
799 sc->sc_dev.dv_xname);
800 }
801
802 /* Update the dirty transmit buffer pointer. */
803 sc->sc_txdirty = i;
804
805 /*
806 * Cancel the watchdog timer if there are no pending
807 * transmissions.
808 */
809 if (sc->sc_txpending == 0)
810 ifp->if_timer = 0;
811
812 /*
813 * Kick the transmitter after a DMA underrun.
814 */
815 if (intstat & INTSTAT_TXU) {
816 printf("%s: transmit underrun\n", sc->sc_dev.dv_xname);
817 bus_space_write_4(sc->sc_st, sc->sc_sh,
818 EPIC_COMMAND, COMMAND_TXUGO);
819 if (sc->sc_txpending)
820 bus_space_write_4(sc->sc_st, sc->sc_sh,
821 EPIC_COMMAND, COMMAND_TXQUEUED);
822 }
823
824 /*
825 * Try to get more packets going.
826 */
827 epic_start(ifp);
828 }
829
830 /*
831 * Check for fatal interrupts.
832 */
833 if (intstat & INTSTAT_FATAL_INT) {
834 printf("%s: fatal error, resetting\n", sc->sc_dev.dv_xname);
835 epic_init(sc);
836 }
837
838 /*
839 * Check for more interrupts.
840 */
841 goto top;
842 }
843
844 /*
845 * One second timer, used to tick the MII.
846 */
847 void
848 epic_tick(arg)
849 void *arg;
850 {
851 struct epic_softc *sc = arg;
852 int s;
853
854 s = splnet();
855 mii_tick(&sc->sc_mii);
856 splx(s);
857
858 timeout(epic_tick, sc, hz);
859 }
860
861 /*
862 * Fixup the clock source on the EPIC.
863 */
864 void
865 epic_fixup_clock_source(sc)
866 struct epic_softc *sc;
867 {
868 int i;
869
870 /*
871 * According to SMC Application Note 7-15, the EPIC's clock
872 * source is incorrect following a reset. This manifests itself
873 * as failure to recognize when host software has written to
874 * a register on the EPIC. The appnote recommends issuing at
875 * least 16 consecutive writes to the CLOCK TEST bit to correctly
876 * configure the clock source.
877 */
878 for (i = 0; i < 16; i++)
879 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TEST,
880 TEST_CLOCKTEST);
881 }
882
883 /*
884 * Perform a soft reset on the EPIC.
885 */
886 void
887 epic_reset(sc)
888 struct epic_softc *sc;
889 {
890
891 epic_fixup_clock_source(sc);
892
893 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, 0);
894 delay(100);
895 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_GENCTL, GENCTL_SOFTRESET);
896 delay(100);
897
898 epic_fixup_clock_source(sc);
899 }
900
901 /*
902 * Initialize the interface. Must be called at splnet().
903 */
904 void
905 epic_init(sc)
906 struct epic_softc *sc;
907 {
908 bus_space_tag_t st = sc->sc_st;
909 bus_space_handle_t sh = sc->sc_sh;
910 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
911 u_int8_t *enaddr = LLADDR(ifp->if_sadl);
912 struct epic_txdesc *txd;
913 u_int32_t genctl, reg0;
914 int i;
915
916 /*
917 * Cancel any pending I/O.
918 */
919 epic_stop(sc);
920
921 /*
922 * Reset the EPIC to a known state.
923 */
924 epic_reset(sc);
925
926 /*
927 * Magical mystery initialization.
928 */
929 bus_space_write_4(st, sh, EPIC_TXTEST, 0);
930
931 /*
932 * Initialize the EPIC genctl register:
933 *
934 * - 64 byte receive FIFO threshold
935 * - automatic advance to next receive frame
936 */
937 genctl = GENCTL_RX_FIFO_THRESH0 | GENCTL_ONECOPY;
938 bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
939
940 /*
941 * Reset the MII bus and PHY.
942 */
943 reg0 = bus_space_read_4(st, sh, EPIC_NVCTL);
944 bus_space_write_4(st, sh, EPIC_NVCTL, reg0 | NVCTL_GPIO1 | NVCTL_GPOE1);
945 bus_space_write_4(st, sh, EPIC_MIICFG, MIICFG_ENASER);
946 bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_RESET_PHY);
947 delay(100);
948 bus_space_write_4(st, sh, EPIC_GENCTL, genctl);
949 delay(100);
950 bus_space_write_4(st, sh, EPIC_NVCTL, reg0);
951
952 /*
953 * Initialize Ethernet address.
954 */
955 reg0 = enaddr[1] << 8 | enaddr[0];
956 bus_space_write_4(st, sh, EPIC_LAN0, reg0);
957 reg0 = enaddr[3] << 8 | enaddr[2];
958 bus_space_write_4(st, sh, EPIC_LAN1, reg0);
959 reg0 = enaddr[5] << 8 | enaddr[4];
960 bus_space_write_4(st, sh, EPIC_LAN2, reg0);
961
962 /*
963 * Initialize receive control. Remember the external buffer
964 * size setting.
965 */
966 reg0 = bus_space_read_4(st, sh, EPIC_RXCON) &
967 (RXCON_EXTBUFSIZESEL1 | RXCON_EXTBUFSIZESEL0);
968 reg0 |= (RXCON_RXMULTICAST | RXCON_RXBROADCAST);
969 if (ifp->if_flags & IFF_PROMISC)
970 reg0 |= RXCON_PROMISCMODE;
971 bus_space_write_4(st, sh, EPIC_RXCON, reg0);
972
973 /* Set the current media. */
974 mii_mediachg(&sc->sc_mii);
975
976 /* Set up the multicast hash table. */
977 epic_set_mchash(sc);
978
979 /*
980 * Initialize the transmit descriptor ring. txlast is initialized
981 * to the end of the list so that it will wrap around to the first
982 * descriptor when the first packet is transmitted.
983 */
984 for (i = 0; i < EPIC_NTXDESC; i++) {
985 txd = EPIC_CDTX(sc, i);
986 memset(txd, 0, sizeof(struct epic_txdesc));
987 txd->et_bufaddr = EPIC_CDFLADDR(sc, i);
988 txd->et_nextdesc = EPIC_CDTXADDR(sc, EPIC_NEXTTX(i));
989 EPIC_CDTXSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
990 }
991 sc->sc_txpending = 0;
992 sc->sc_txdirty = 0;
993 sc->sc_txlast = EPIC_NTXDESC - 1;
994
995 /*
996 * Initialize the receive descriptor ring. The buffers are
997 * already allocated.
998 */
999 for (i = 0; i < EPIC_NRXDESC; i++)
1000 EPIC_INIT_RXDESC(sc, i);
1001 sc->sc_rxptr = 0;
1002
1003 /*
1004 * Initialize the interrupt mask and enable interrupts.
1005 */
1006 bus_space_write_4(st, sh, EPIC_INTMASK, INTMASK);
1007 bus_space_write_4(st, sh, EPIC_GENCTL, genctl | GENCTL_INTENA);
1008
1009 /*
1010 * Give the transmit and receive rings to the EPIC.
1011 */
1012 bus_space_write_4(st, sh, EPIC_PTCDAR,
1013 EPIC_CDTXADDR(sc, EPIC_NEXTTX(sc->sc_txlast)));
1014 bus_space_write_4(st, sh, EPIC_PRCDAR,
1015 EPIC_CDRXADDR(sc, sc->sc_rxptr));
1016
1017 /*
1018 * Set the EPIC in motion.
1019 */
1020 bus_space_write_4(st, sh, EPIC_COMMAND,
1021 COMMAND_RXQUEUED | COMMAND_START_RX);
1022
1023 /*
1024 * ...all done!
1025 */
1026 ifp->if_flags |= IFF_RUNNING;
1027 ifp->if_flags &= ~IFF_OACTIVE;
1028
1029 /*
1030 * Start the one second clock.
1031 */
1032 timeout(epic_tick, sc, hz);
1033
1034 /*
1035 * Attempt to start output on the interface.
1036 */
1037 epic_start(ifp);
1038 }
1039
1040 /*
1041 * Stop transmission on the interface.
1042 */
1043 void
1044 epic_stop(sc)
1045 struct epic_softc *sc;
1046 {
1047 bus_space_tag_t st = sc->sc_st;
1048 bus_space_handle_t sh = sc->sc_sh;
1049 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1050 struct epic_descsoft *ds;
1051 u_int32_t reg;
1052 int i;
1053
1054 /*
1055 * Stop the one second clock.
1056 */
1057 untimeout(epic_tick, sc);
1058
1059 /* Paranoia... */
1060 epic_fixup_clock_source(sc);
1061
1062 /*
1063 * Disable interrupts.
1064 */
1065 reg = bus_space_read_4(st, sh, EPIC_GENCTL);
1066 bus_space_write_4(st, sh, EPIC_GENCTL, reg & ~GENCTL_INTENA);
1067 bus_space_write_4(st, sh, EPIC_INTMASK, 0);
1068
1069 /*
1070 * Stop the DMA engine and take the receiver off-line.
1071 */
1072 bus_space_write_4(st, sh, EPIC_COMMAND, COMMAND_STOP_RDMA |
1073 COMMAND_STOP_TDMA | COMMAND_STOP_RX);
1074
1075 /*
1076 * Release any queued transmit buffers.
1077 */
1078 for (i = 0; i < EPIC_NTXDESC; i++) {
1079 ds = EPIC_DSTX(sc, i);
1080 if (ds->ds_mbuf != NULL) {
1081 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1082 m_freem(ds->ds_mbuf);
1083 ds->ds_mbuf = NULL;
1084 }
1085 }
1086
1087 /*
1088 * Mark the interface down and cancel the watchdog timer.
1089 */
1090 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1091 ifp->if_timer = 0;
1092 }
1093
1094 /*
1095 * Read the EPIC Serial EEPROM.
1096 */
1097 void
1098 epic_read_eeprom(sc, word, wordcnt, data)
1099 struct epic_softc *sc;
1100 int word, wordcnt;
1101 u_int16_t *data;
1102 {
1103 bus_space_tag_t st = sc->sc_st;
1104 bus_space_handle_t sh = sc->sc_sh;
1105 u_int16_t reg;
1106 int i, x;
1107
1108 #define EEPROM_WAIT_READY(st, sh) \
1109 while ((bus_space_read_4((st), (sh), EPIC_EECTL) & EECTL_EERDY) == 0) \
1110 /* nothing */
1111
1112 /*
1113 * Enable the EEPROM.
1114 */
1115 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1116 EEPROM_WAIT_READY(st, sh);
1117
1118 for (i = 0; i < wordcnt; i++) {
1119 /* Send CHIP SELECT for one clock tick. */
1120 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE|EECTL_EECS);
1121 EEPROM_WAIT_READY(st, sh);
1122
1123 /* Shift in the READ opcode. */
1124 for (x = 3; x > 0; x--) {
1125 reg = EECTL_ENABLE|EECTL_EECS;
1126 if (EPIC_EEPROM_OPC_READ & (1 << (x - 1)))
1127 reg |= EECTL_EEDI;
1128 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1129 EEPROM_WAIT_READY(st, sh);
1130 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1131 EEPROM_WAIT_READY(st, sh);
1132 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1133 EEPROM_WAIT_READY(st, sh);
1134 }
1135
1136 /* Shift in address. */
1137 for (x = 6; x > 0; x--) {
1138 reg = EECTL_ENABLE|EECTL_EECS;
1139 if ((word + i) & (1 << (x - 1)))
1140 reg |= EECTL_EEDI;
1141 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1142 EEPROM_WAIT_READY(st, sh);
1143 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1144 EEPROM_WAIT_READY(st, sh);
1145 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1146 EEPROM_WAIT_READY(st, sh);
1147 }
1148
1149 /* Shift out data. */
1150 reg = EECTL_ENABLE|EECTL_EECS;
1151 data[i] = 0;
1152 for (x = 16; x > 0; x--) {
1153 bus_space_write_4(st, sh, EPIC_EECTL, reg|EECTL_EESK);
1154 EEPROM_WAIT_READY(st, sh);
1155 if (bus_space_read_4(st, sh, EPIC_EECTL) & EECTL_EEDO)
1156 data[i] |= (1 << (x - 1));
1157 bus_space_write_4(st, sh, EPIC_EECTL, reg);
1158 EEPROM_WAIT_READY(st, sh);
1159 }
1160
1161 /* Clear CHIP SELECT. */
1162 bus_space_write_4(st, sh, EPIC_EECTL, EECTL_ENABLE);
1163 EEPROM_WAIT_READY(st, sh);
1164 }
1165
1166 /*
1167 * Disable the EEPROM.
1168 */
1169 bus_space_write_4(st, sh, EPIC_EECTL, 0);
1170
1171 #undef EEPROM_WAIT_READY
1172 }
1173
1174 /*
1175 * Add a receive buffer to the indicated descriptor.
1176 */
1177 int
1178 epic_add_rxbuf(sc, idx)
1179 struct epic_softc *sc;
1180 int idx;
1181 {
1182 struct epic_descsoft *ds = EPIC_DSRX(sc, idx);
1183 struct mbuf *m;
1184 int error;
1185
1186 MGETHDR(m, M_DONTWAIT, MT_DATA);
1187 if (m == NULL)
1188 return (ENOBUFS);
1189
1190 MCLGET(m, M_DONTWAIT);
1191 if ((m->m_flags & M_EXT) == 0) {
1192 m_freem(m);
1193 return (ENOBUFS);
1194 }
1195
1196 if (ds->ds_mbuf != NULL)
1197 bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
1198
1199 ds->ds_mbuf = m;
1200
1201 error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
1202 m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
1203 if (error) {
1204 printf("%s: can't load rx DMA map %d, error = %d\n",
1205 sc->sc_dev.dv_xname, idx, error);
1206 panic("epic_add_rxbuf"); /* XXX */
1207 }
1208
1209 bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
1210 ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1211
1212 EPIC_INIT_RXDESC(sc, idx);
1213
1214 return (0);
1215 }
1216
1217 /*
1218 * Set the EPIC multicast hash table.
1219 *
1220 * NOTE: We rely on a recently-updated mii_media_active here!
1221 */
1222 void
1223 epic_set_mchash(sc)
1224 struct epic_softc *sc;
1225 {
1226 struct ethercom *ec = &sc->sc_ethercom;
1227 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1228 struct ether_multi *enm;
1229 struct ether_multistep step;
1230 u_int8_t *cp;
1231 u_int32_t crc, mchash[4];
1232 int len;
1233 static const u_int32_t crctab[] = {
1234 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1235 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1236 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1237 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1238 };
1239
1240 /*
1241 * Set up the multicast address filter by passing all multicast
1242 * addresses through a CRC generator, and then using the high-order
1243 * 6 bits as an index into the 64 bit multicast hash table (only
1244 * the lower 16 bits of each 32 bit multicast hash register are
1245 * valid). The high order bit selects the register, while the
1246 * rest of the bits select the bit within the register.
1247 */
1248
1249 if (ifp->if_flags & IFF_PROMISC)
1250 goto allmulti;
1251
1252 if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
1253 /* XXX hardware bug in 10Mbps mode. */
1254 goto allmulti;
1255 }
1256
1257 mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0;
1258
1259 ETHER_FIRST_MULTI(step, ec, enm);
1260 while (enm != NULL) {
1261 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1262 /*
1263 * We must listen to a range of multicast addresses.
1264 * For now, just accept all multicasts, rather than
1265 * trying to set only those filter bits needed to match
1266 * the range. (At this time, the only use of address
1267 * ranges is for IP multicast routing, for which the
1268 * range is big enough to require all bits set.)
1269 */
1270 goto allmulti;
1271 }
1272
1273 cp = enm->enm_addrlo;
1274 crc = 0xffffffff;
1275 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1276 crc ^= *cp++;
1277 crc = (crc >> 4) ^ crctab[crc & 0xf];
1278 crc = (crc >> 4) ^ crctab[crc & 0xf];
1279 }
1280 /* Just want the 6 most significant bits. */
1281 crc >>= 26;
1282
1283 /* Set the corresponding bit in the hash table. */
1284 mchash[crc >> 4] |= 1 << (crc & 0xf);
1285
1286 ETHER_NEXT_MULTI(step, enm);
1287 }
1288
1289 ifp->if_flags &= ~IFF_ALLMULTI;
1290 goto sethash;
1291
1292 allmulti:
1293 ifp->if_flags |= IFF_ALLMULTI;
1294 mchash[0] = mchash[1] = mchash[2] = mchash[3] = 0xffff;
1295
1296 sethash:
1297 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC0, mchash[0]);
1298 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC1, mchash[1]);
1299 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC2, mchash[2]);
1300 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MC3, mchash[3]);
1301 }
1302
1303 /*
1304 * Wait for the MII to become ready.
1305 */
1306 int
1307 epic_mii_wait(sc, rw)
1308 struct epic_softc *sc;
1309 u_int32_t rw;
1310 {
1311 int i;
1312
1313 for (i = 0; i < 50; i++) {
1314 if ((bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL) & rw)
1315 == 0)
1316 break;
1317 delay(2);
1318 }
1319 if (i == 50) {
1320 printf("%s: MII timed out\n", sc->sc_dev.dv_xname);
1321 return (1);
1322 }
1323
1324 return (0);
1325 }
1326
1327 /*
1328 * Read from the MII.
1329 */
1330 int
1331 epic_mii_read(self, phy, reg)
1332 struct device *self;
1333 int phy, reg;
1334 {
1335 struct epic_softc *sc = (struct epic_softc *)self;
1336
1337 if (epic_mii_wait(sc, MMCTL_WRITE))
1338 return (0);
1339
1340 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1341 MMCTL_ARG(phy, reg, MMCTL_READ));
1342
1343 if (epic_mii_wait(sc, MMCTL_READ))
1344 return (0);
1345
1346 return (bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA) &
1347 MMDATA_MASK);
1348 }
1349
1350 /*
1351 * Write to the MII.
1352 */
1353 void
1354 epic_mii_write(self, phy, reg, val)
1355 struct device *self;
1356 int phy, reg, val;
1357 {
1358 struct epic_softc *sc = (struct epic_softc *)self;
1359
1360 if (epic_mii_wait(sc, MMCTL_WRITE))
1361 return;
1362
1363 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMDATA, val);
1364 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_MMCTL,
1365 MMCTL_ARG(phy, reg, MMCTL_WRITE));
1366 }
1367
1368 /*
1369 * Callback from PHY when media changes.
1370 */
1371 void
1372 epic_statchg(self)
1373 struct device *self;
1374 {
1375 struct epic_softc *sc = (struct epic_softc *)self;
1376 u_int32_t txcon;
1377
1378 /*
1379 * Update loopback bits in TXCON to reflect duplex mode.
1380 */
1381 txcon = bus_space_read_4(sc->sc_st, sc->sc_sh, EPIC_TXCON);
1382 if (sc->sc_mii.mii_media_active & IFM_FDX)
1383 txcon |= (TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
1384 else
1385 txcon &= ~(TXCON_LOOPBACK_D1|TXCON_LOOPBACK_D2);
1386 bus_space_write_4(sc->sc_st, sc->sc_sh, EPIC_TXCON, txcon);
1387
1388 /*
1389 * There is a multicast filter bug in 10Mbps mode. Kick the
1390 * multicast filter in case the speed changed.
1391 */
1392 epic_set_mchash(sc);
1393
1394 /* XXX Update ifp->if_baudrate */
1395 }
1396
1397 /*
1398 * Callback from ifmedia to request current media status.
1399 */
1400 void
1401 epic_mediastatus(ifp, ifmr)
1402 struct ifnet *ifp;
1403 struct ifmediareq *ifmr;
1404 {
1405 struct epic_softc *sc = ifp->if_softc;
1406
1407 mii_pollstat(&sc->sc_mii);
1408 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1409 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1410 }
1411
1412 /*
1413 * Callback from ifmedia to request new media setting.
1414 */
1415 int
1416 epic_mediachange(ifp)
1417 struct ifnet *ifp;
1418 {
1419 struct epic_softc *sc = ifp->if_softc;
1420
1421 if (ifp->if_flags & IFF_UP)
1422 mii_mediachg(&sc->sc_mii);
1423 return (0);
1424 }
1425