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