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