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