xref: /netbsd-src/sys/dev/sbus/be.c (revision 6a1508dad3515842aa76bf5ec8fc2daab5f5af02)
1 /*	$NetBSD: be.c,v 1.61 2008/12/26 18:51:19 macallan Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. The name of the authors may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: be.c,v 1.61 2008/12/26 18:51:19 macallan Exp $");
61 
62 #include "opt_ddb.h"
63 #include "opt_inet.h"
64 #include "bpfilter.h"
65 #include "rnd.h"
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/callout.h>
70 #include <sys/kernel.h>
71 #include <sys/errno.h>
72 #include <sys/ioctl.h>
73 #include <sys/mbuf.h>
74 #include <sys/socket.h>
75 #include <sys/syslog.h>
76 #include <sys/device.h>
77 #include <sys/malloc.h>
78 #if NRND > 0
79 #include <sys/rnd.h>
80 #endif
81 
82 #include <net/if.h>
83 #include <net/if_dl.h>
84 #include <net/if_types.h>
85 #include <net/netisr.h>
86 #include <net/if_media.h>
87 #include <net/if_ether.h>
88 
89 #ifdef INET
90 #include <netinet/in.h>
91 #include <netinet/if_inarp.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #endif
96 
97 
98 #if NBPFILTER > 0
99 #include <net/bpf.h>
100 #include <net/bpfdesc.h>
101 #endif
102 
103 #include <sys/bus.h>
104 #include <sys/intr.h>
105 #include <machine/autoconf.h>
106 
107 #include <dev/sbus/sbusvar.h>
108 
109 #include <dev/mii/mii.h>
110 #include <dev/mii/miivar.h>
111 
112 #include <dev/sbus/qecreg.h>
113 #include <dev/sbus/qecvar.h>
114 #include <dev/sbus/bereg.h>
115 
116 struct be_softc {
117 	struct	device	sc_dev;
118 	struct	sbusdev sc_sd;		/* sbus device */
119 	bus_space_tag_t	sc_bustag;	/* bus & DMA tags */
120 	bus_dma_tag_t	sc_dmatag;
121 	bus_dmamap_t	sc_dmamap;
122 	struct	ethercom sc_ethercom;
123 	/*struct	ifmedia sc_ifmedia;	-* interface media */
124 	struct mii_data	sc_mii;		/* MII media control */
125 #define sc_media	sc_mii.mii_media/* shorthand */
126 	int		sc_phys[2];	/* MII instance -> phy */
127 
128 	struct callout sc_tick_ch;
129 
130 	/*
131 	 * Some `mii_softc' items we need to emulate MII operation
132 	 * for our internal transceiver.
133 	 */
134 	int		sc_mii_inst;	/* instance of internal phy */
135 	int		sc_mii_active;	/* currently active medium */
136 	int		sc_mii_ticks;	/* tick counter */
137 	int		sc_mii_flags;	/* phy status flags */
138 #define MIIF_HAVELINK	0x04000000
139 	int		sc_intphy_curspeed;	/* Established link speed */
140 
141 	struct	qec_softc *sc_qec;	/* QEC parent */
142 
143 	bus_space_handle_t	sc_qr;	/* QEC registers */
144 	bus_space_handle_t	sc_br;	/* BE registers */
145 	bus_space_handle_t	sc_cr;	/* channel registers */
146 	bus_space_handle_t	sc_tr;	/* transceiver registers */
147 
148 	u_int	sc_rev;
149 
150 	int	sc_channel;		/* channel number */
151 	int	sc_burst;
152 
153 	struct  qec_ring	sc_rb;	/* Packet Ring Buffer */
154 
155 	/* MAC address */
156 	u_int8_t sc_enaddr[6];
157 #ifdef BEDEBUG
158 	int	sc_debug;
159 #endif
160 };
161 
162 int	bematch(struct device *, struct cfdata *, void *);
163 void	beattach(struct device *, struct device *, void *);
164 
165 void	beinit(struct be_softc *);
166 void	bestart(struct ifnet *);
167 void	bestop(struct be_softc *);
168 void	bewatchdog(struct ifnet *);
169 int	beioctl(struct ifnet *, u_long, void *);
170 void	bereset(struct be_softc *);
171 
172 int	beintr(void *);
173 int	berint(struct be_softc *);
174 int	betint(struct be_softc *);
175 int	beqint(struct be_softc *, u_int32_t);
176 int	beeint(struct be_softc *, u_int32_t);
177 
178 static void	be_read(struct be_softc *, int, int);
179 static int	be_put(struct be_softc *, int, struct mbuf *);
180 static struct mbuf *be_get(struct be_softc *, int, int);
181 
182 void	be_pal_gate(struct be_softc *, int);
183 
184 /* ifmedia callbacks */
185 void	be_ifmedia_sts(struct ifnet *, struct ifmediareq *);
186 int	be_ifmedia_upd(struct ifnet *);
187 
188 void	be_mcreset(struct be_softc *);
189 
190 /* MII methods & callbacks */
191 static int	be_mii_readreg(struct device *, int, int);
192 static void	be_mii_writereg(struct device *, int, int, int);
193 static void	be_mii_statchg(struct device *);
194 
195 /* MII helpers */
196 static void	be_mii_sync(struct be_softc *);
197 static void	be_mii_sendbits(struct be_softc *, int, u_int32_t, int);
198 static int	be_mii_reset(struct be_softc *, int);
199 static int	be_tcvr_read_bit(struct be_softc *, int);
200 static void	be_tcvr_write_bit(struct be_softc *, int, int);
201 
202 void	be_tick(void *);
203 void	be_intphy_auto(struct be_softc *);
204 void	be_intphy_status(struct be_softc *);
205 int	be_intphy_service(struct be_softc *, struct mii_data *, int);
206 
207 
208 CFATTACH_DECL(be, sizeof(struct be_softc),
209     bematch, beattach, NULL, NULL);
210 
211 int
212 bematch(parent, cf, aux)
213 	struct device *parent;
214 	struct cfdata *cf;
215 	void *aux;
216 {
217 	struct sbus_attach_args *sa = aux;
218 
219 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
220 }
221 
222 void
223 beattach(parent, self, aux)
224 	struct device *parent, *self;
225 	void *aux;
226 {
227 	struct sbus_attach_args *sa = aux;
228 	struct qec_softc *qec = (struct qec_softc *)parent;
229 	struct be_softc *sc = (struct be_softc *)self;
230 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
231 	struct mii_data *mii = &sc->sc_mii;
232 	struct mii_softc *child;
233 	int node = sa->sa_node;
234 	bus_dma_tag_t dmatag = sa->sa_dmatag;
235 	bus_dma_segment_t seg;
236 	bus_size_t size;
237 	int instance;
238 	int rseg, error;
239 	u_int32_t v;
240 
241 	if (sa->sa_nreg < 3) {
242 		printf("%s: only %d register sets\n",
243 			device_xname(self), sa->sa_nreg);
244 		return;
245 	}
246 
247 	if (bus_space_map(sa->sa_bustag,
248 			  (bus_addr_t)BUS_ADDR(
249 				sa->sa_reg[0].oa_space,
250 				sa->sa_reg[0].oa_base),
251 			  (bus_size_t)sa->sa_reg[0].oa_size,
252 			  0, &sc->sc_cr) != 0) {
253 		printf("beattach: cannot map registers\n");
254 		return;
255 	}
256 
257 	if (bus_space_map(sa->sa_bustag,
258 			  (bus_addr_t)BUS_ADDR(
259 				sa->sa_reg[1].oa_space,
260 				sa->sa_reg[1].oa_base),
261 			  (bus_size_t)sa->sa_reg[1].oa_size,
262 			  0, &sc->sc_br) != 0) {
263 		printf("beattach: cannot map registers\n");
264 		return;
265 	}
266 
267 	if (bus_space_map(sa->sa_bustag,
268 			  (bus_addr_t)BUS_ADDR(
269 				sa->sa_reg[2].oa_space,
270 				sa->sa_reg[2].oa_base),
271 			  (bus_size_t)sa->sa_reg[2].oa_size,
272 			  0, &sc->sc_tr) != 0) {
273 		printf("beattach: cannot map registers\n");
274 		return;
275 	}
276 
277 	sc->sc_bustag = sa->sa_bustag;
278 	sc->sc_qec = qec;
279 	sc->sc_qr = qec->sc_regs;
280 
281 	sc->sc_rev = prom_getpropint(node, "board-version", -1);
282 	printf(" rev %x", sc->sc_rev);
283 
284 	callout_init(&sc->sc_tick_ch, 0);
285 
286 	bestop(sc);
287 
288 	sc->sc_channel = prom_getpropint(node, "channel#", -1);
289 	if (sc->sc_channel == -1)
290 		sc->sc_channel = 0;
291 
292 	sc->sc_burst = prom_getpropint(node, "burst-sizes", -1);
293 	if (sc->sc_burst == -1)
294 		sc->sc_burst = qec->sc_burst;
295 
296 	/* Clamp at parent's burst sizes */
297 	sc->sc_burst &= qec->sc_burst;
298 
299 	/* Establish interrupt handler */
300 	if (sa->sa_nintr)
301 		(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
302 					 beintr, sc);
303 
304 	prom_getether(node, sc->sc_enaddr);
305 	printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
306 
307 	/*
308 	 * Allocate descriptor ring and buffers.
309 	 */
310 
311 	/* for now, allocate as many bufs as there are ring descriptors */
312 	sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
313 	sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
314 
315 	size =	QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
316 		QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
317 		sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
318 		sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
319 
320 	/* Get a DMA handle */
321 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
322 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
323 		aprint_error_dev(self, "DMA map create error %d\n", error);
324 		return;
325 	}
326 
327 	/* Allocate DMA buffer */
328 	if ((error = bus_dmamem_alloc(sa->sa_dmatag, size, 0, 0,
329 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
330 		aprint_error_dev(self, "DMA buffer alloc error %d\n",
331 			error);
332 		return;
333 	}
334 
335 	/* Map DMA memory in CPU addressable space */
336 	if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
337 			            &sc->sc_rb.rb_membase,
338 			            BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
339 		aprint_error_dev(self, "DMA buffer map error %d\n",
340 			error);
341 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
342 		return;
343 	}
344 
345 	/* Load the buffer */
346 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
347 				     sc->sc_rb.rb_membase, size, NULL,
348 				     BUS_DMA_NOWAIT)) != 0) {
349 		aprint_error_dev(self, "DMA buffer map load error %d\n",
350 			error);
351 		bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
352 		bus_dmamem_free(dmatag, &seg, rseg);
353 		return;
354 	}
355 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
356 
357 	/*
358 	 * Initialize our media structures and MII info.
359 	 */
360 	mii->mii_ifp = ifp;
361 	mii->mii_readreg = be_mii_readreg;
362 	mii->mii_writereg = be_mii_writereg;
363 	mii->mii_statchg = be_mii_statchg;
364 
365 	ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
366 
367 	/*
368 	 * Initialize transceiver and determine which PHY connection to use.
369 	 */
370 	be_mii_sync(sc);
371 	v = bus_space_read_4(sc->sc_bustag, sc->sc_tr, BE_TRI_MGMTPAL);
372 
373 	instance = 0;
374 
375 	if ((v & MGMT_PAL_EXT_MDIO) != 0) {
376 
377 		mii_attach(&sc->sc_dev, mii, 0xffffffff, BE_PHY_EXTERNAL,
378 		    MII_OFFSET_ANY, 0);
379 
380 		child = LIST_FIRST(&mii->mii_phys);
381 		if (child == NULL) {
382 			/* No PHY attached */
383 			ifmedia_add(&sc->sc_media,
384 				    IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance),
385 				    0, NULL);
386 			ifmedia_set(&sc->sc_media,
387 				   IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance));
388 		} else {
389 			/*
390 			 * Note: we support just one PHY on the external
391 			 * MII connector.
392 			 */
393 #ifdef DIAGNOSTIC
394 			if (LIST_NEXT(child, mii_list) != NULL) {
395 				aprint_error_dev(&sc->sc_dev, "spurious MII device %s attached\n",
396 				       device_xname(child->mii_dev));
397 			}
398 #endif
399 			if (child->mii_phy != BE_PHY_EXTERNAL ||
400 			    child->mii_inst > 0) {
401 				aprint_error_dev(&sc->sc_dev, "cannot accommodate MII device %s"
402 				       " at phy %d, instance %d\n",
403 				       device_xname(child->mii_dev),
404 				       child->mii_phy, child->mii_inst);
405 			} else {
406 				sc->sc_phys[instance] = child->mii_phy;
407 			}
408 
409 			/*
410 			 * XXX - we can really do the following ONLY if the
411 			 * phy indeed has the auto negotiation capability!!
412 			 */
413 			ifmedia_set(&sc->sc_media,
414 				   IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
415 
416 			/* Mark our current media setting */
417 			be_pal_gate(sc, BE_PHY_EXTERNAL);
418 			instance++;
419 		}
420 
421 	}
422 
423 	if ((v & MGMT_PAL_INT_MDIO) != 0) {
424 		/*
425 		 * The be internal phy looks vaguely like MII hardware,
426 		 * but not enough to be able to use the MII device
427 		 * layer. Hence, we have to take care of media selection
428 		 * ourselves.
429 		 */
430 
431 		sc->sc_mii_inst = instance;
432 		sc->sc_phys[instance] = BE_PHY_INTERNAL;
433 
434 		/* Use `ifm_data' to store BMCR bits */
435 		ifmedia_add(&sc->sc_media,
436 			    IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,instance),
437 			    0, NULL);
438 		ifmedia_add(&sc->sc_media,
439 			    IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,0,instance),
440 			    BMCR_S100, NULL);
441 		ifmedia_add(&sc->sc_media,
442 			    IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance),
443 			    0, NULL);
444 
445 		printf("on-board transceiver at %s: 10baseT, 100baseTX, auto\n",
446 			device_xname(self));
447 
448 		be_mii_reset(sc, BE_PHY_INTERNAL);
449 		/* Only set default medium here if there's no external PHY */
450 		if (instance == 0) {
451 			be_pal_gate(sc, BE_PHY_INTERNAL);
452 			ifmedia_set(&sc->sc_media,
453 				   IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
454 		} else
455 			be_mii_writereg((void *)sc,
456 				BE_PHY_INTERNAL, MII_BMCR, BMCR_ISO);
457 	}
458 
459 	memcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
460 	ifp->if_softc = sc;
461 	ifp->if_start = bestart;
462 	ifp->if_ioctl = beioctl;
463 	ifp->if_watchdog = bewatchdog;
464 	ifp->if_flags =
465 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
466 	IFQ_SET_READY(&ifp->if_snd);
467 
468 	/* claim 802.1q capability */
469 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
470 
471 	/* Attach the interface. */
472 	if_attach(ifp);
473 	ether_ifattach(ifp, sc->sc_enaddr);
474 }
475 
476 
477 /*
478  * Routine to copy from mbuf chain to transmit buffer in
479  * network buffer memory.
480  */
481 static inline int
482 be_put(sc, idx, m)
483 	struct be_softc *sc;
484 	int idx;
485 	struct mbuf *m;
486 {
487 	struct mbuf *n;
488 	int len, tlen = 0, boff = 0;
489 	void *bp;
490 
491 	bp = (char *)sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
492 
493 	for (; m; m = n) {
494 		len = m->m_len;
495 		if (len == 0) {
496 			MFREE(m, n);
497 			continue;
498 		}
499 		memcpy((char *)bp + boff, mtod(m, void *), len);
500 		boff += len;
501 		tlen += len;
502 		MFREE(m, n);
503 	}
504 	return (tlen);
505 }
506 
507 /*
508  * Pull data off an interface.
509  * Len is the length of data, with local net header stripped.
510  * We copy the data into mbufs.  When full cluster sized units are present,
511  * we copy into clusters.
512  */
513 static inline struct mbuf *
514 be_get(sc, idx, totlen)
515 	struct be_softc *sc;
516 	int idx, totlen;
517 {
518 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
519 	struct mbuf *m;
520 	struct mbuf *top, **mp;
521 	int len, pad, boff = 0;
522 	void *bp;
523 
524 	bp = (char *)sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
525 
526 	MGETHDR(m, M_DONTWAIT, MT_DATA);
527 	if (m == NULL)
528 		return (NULL);
529 	m->m_pkthdr.rcvif = ifp;
530 	m->m_pkthdr.len = totlen;
531 
532 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
533 	m->m_data += pad;
534 	len = MHLEN - pad;
535 	top = NULL;
536 	mp = &top;
537 
538 	while (totlen > 0) {
539 		if (top) {
540 			MGET(m, M_DONTWAIT, MT_DATA);
541 			if (m == NULL) {
542 				m_freem(top);
543 				return (NULL);
544 			}
545 			len = MLEN;
546 		}
547 		if (top && totlen >= MINCLSIZE) {
548 			MCLGET(m, M_DONTWAIT);
549 			if (m->m_flags & M_EXT)
550 				len = MCLBYTES;
551 		}
552 		m->m_len = len = min(totlen, len);
553 		memcpy(mtod(m, void *), (char *)bp + boff, len);
554 		boff += len;
555 		totlen -= len;
556 		*mp = m;
557 		mp = &m->m_next;
558 	}
559 
560 	return (top);
561 }
562 
563 /*
564  * Pass a packet to the higher levels.
565  */
566 static inline void
567 be_read(sc, idx, len)
568 	struct be_softc *sc;
569 	int idx, len;
570 {
571 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
572 	struct mbuf *m;
573 
574 	if (len <= sizeof(struct ether_header) ||
575 	    len > ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) {
576 #ifdef BEDEBUG
577 		if (sc->sc_debug)
578 			printf("%s: invalid packet size %d; dropping\n",
579 				ifp->if_xname, len);
580 #endif
581 		ifp->if_ierrors++;
582 		return;
583 	}
584 
585 	/*
586 	 * Pull packet off interface.
587 	 */
588 	m = be_get(sc, idx, len);
589 	if (m == NULL) {
590 		ifp->if_ierrors++;
591 		return;
592 	}
593 	ifp->if_ipackets++;
594 
595 #if NBPFILTER > 0
596 	/*
597 	 * Check if there's a BPF listener on this interface.
598 	 * If so, hand off the raw packet to BPF.
599 	 */
600 	if (ifp->if_bpf)
601 		bpf_mtap(ifp->if_bpf, m);
602 #endif
603 	/* Pass the packet up. */
604 	(*ifp->if_input)(ifp, m);
605 }
606 
607 /*
608  * Start output on interface.
609  * We make two assumptions here:
610  *  1) that the current priority is set to splnet _before_ this code
611  *     is called *and* is returned to the appropriate priority after
612  *     return
613  *  2) that the IFF_OACTIVE flag is checked before this code is called
614  *     (i.e. that the output part of the interface is idle)
615  */
616 void
617 bestart(ifp)
618 	struct ifnet *ifp;
619 {
620 	struct be_softc *sc = (struct be_softc *)ifp->if_softc;
621 	struct qec_xd *txd = sc->sc_rb.rb_txd;
622 	struct mbuf *m;
623 	unsigned int bix, len;
624 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
625 
626 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
627 		return;
628 
629 	bix = sc->sc_rb.rb_tdhead;
630 
631 	for (;;) {
632 		IFQ_DEQUEUE(&ifp->if_snd, m);
633 		if (m == 0)
634 			break;
635 
636 #if NBPFILTER > 0
637 		/*
638 		 * If BPF is listening on this interface, let it see the
639 		 * packet before we commit it to the wire.
640 		 */
641 		if (ifp->if_bpf)
642 			bpf_mtap(ifp->if_bpf, m);
643 #endif
644 
645 		/*
646 		 * Copy the mbuf chain into the transmit buffer.
647 		 */
648 		len = be_put(sc, bix, m);
649 
650 		/*
651 		 * Initialize transmit registers and start transmission
652 		 */
653 		txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
654 				    (len & QEC_XD_LENGTH);
655 		bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
656 				  BE_CR_CTRL_TWAKEUP);
657 
658 		if (++bix == QEC_XD_RING_MAXSIZE)
659 			bix = 0;
660 
661 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
662 			ifp->if_flags |= IFF_OACTIVE;
663 			break;
664 		}
665 	}
666 
667 	sc->sc_rb.rb_tdhead = bix;
668 }
669 
670 void
671 bestop(sc)
672 	struct be_softc *sc;
673 {
674 	int n;
675 	bus_space_tag_t t = sc->sc_bustag;
676 	bus_space_handle_t br = sc->sc_br;
677 
678 	callout_stop(&sc->sc_tick_ch);
679 
680 	/* Down the MII. */
681 	mii_down(&sc->sc_mii);
682 	(void)be_intphy_service(sc, &sc->sc_mii, MII_DOWN);
683 
684 	/* Stop the transmitter */
685 	bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
686 	for (n = 32; n > 0; n--) {
687 		if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
688 			break;
689 		DELAY(20);
690 	}
691 
692 	/* Stop the receiver */
693 	bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
694 	for (n = 32; n > 0; n--) {
695 		if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
696 			break;
697 		DELAY(20);
698 	}
699 }
700 
701 /*
702  * Reset interface.
703  */
704 void
705 bereset(sc)
706 	struct be_softc *sc;
707 {
708 	int s;
709 
710 	s = splnet();
711 	bestop(sc);
712 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
713 		beinit(sc);
714 	splx(s);
715 }
716 
717 void
718 bewatchdog(ifp)
719 	struct ifnet *ifp;
720 {
721 	struct be_softc *sc = ifp->if_softc;
722 
723 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
724 	++sc->sc_ethercom.ec_if.if_oerrors;
725 
726 	bereset(sc);
727 }
728 
729 int
730 beintr(v)
731 	void *v;
732 {
733 	struct be_softc *sc = (struct be_softc *)v;
734 	bus_space_tag_t t = sc->sc_bustag;
735 	u_int32_t whyq, whyb, whyc;
736 	int r = 0;
737 
738 	/* Read QEC status, channel status and BE status */
739 	whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
740 	whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
741 	whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
742 
743 	if (whyq & QEC_STAT_BM)
744 		r |= beeint(sc, whyb);
745 
746 	if (whyq & QEC_STAT_ER)
747 		r |= beqint(sc, whyc);
748 
749 	if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
750 		r |= betint(sc);
751 
752 	if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
753 		r |= berint(sc);
754 
755 	return (r);
756 }
757 
758 /*
759  * QEC Interrupt.
760  */
761 int
762 beqint(sc, why)
763 	struct be_softc *sc;
764 	u_int32_t why;
765 {
766 	int r = 0, rst = 0;
767 
768 	if (why & BE_CR_STAT_TXIRQ)
769 		r |= 1;
770 	if (why & BE_CR_STAT_RXIRQ)
771 		r |= 1;
772 
773 	if (why & BE_CR_STAT_BERROR) {
774 		r |= 1;
775 		rst = 1;
776 		aprint_error_dev(&sc->sc_dev, "bigmac error\n");
777 	}
778 
779 	if (why & BE_CR_STAT_TXDERR) {
780 		r |= 1;
781 		rst = 1;
782 		aprint_error_dev(&sc->sc_dev, "bogus tx descriptor\n");
783 	}
784 
785 	if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
786 		r |= 1;
787 		rst = 1;
788 		aprint_error_dev(&sc->sc_dev, "tx DMA error ( ");
789 		if (why & BE_CR_STAT_TXLERR)
790 			printf("Late ");
791 		if (why & BE_CR_STAT_TXPERR)
792 			printf("Parity ");
793 		if (why & BE_CR_STAT_TXSERR)
794 			printf("Generic ");
795 		printf(")\n");
796 	}
797 
798 	if (why & BE_CR_STAT_RXDROP) {
799 		r |= 1;
800 		rst = 1;
801 		aprint_error_dev(&sc->sc_dev, "out of rx descriptors\n");
802 	}
803 
804 	if (why & BE_CR_STAT_RXSMALL) {
805 		r |= 1;
806 		rst = 1;
807 		aprint_error_dev(&sc->sc_dev, "rx descriptor too small\n");
808 	}
809 
810 	if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
811 		r |= 1;
812 		rst = 1;
813 		aprint_error_dev(&sc->sc_dev, "rx DMA error ( ");
814 		if (why & BE_CR_STAT_RXLERR)
815 			printf("Late ");
816 		if (why & BE_CR_STAT_RXPERR)
817 			printf("Parity ");
818 		if (why & BE_CR_STAT_RXSERR)
819 			printf("Generic ");
820 		printf(")\n");
821 	}
822 
823 	if (!r) {
824 		rst = 1;
825 		aprint_error_dev(&sc->sc_dev, "unexpected error interrupt %08x\n",
826 			why);
827 	}
828 
829 	if (rst) {
830 		printf("%s: resetting\n", device_xname(&sc->sc_dev));
831 		bereset(sc);
832 	}
833 
834 	return (r);
835 }
836 
837 /*
838  * Error interrupt.
839  */
840 int
841 beeint(sc, why)
842 	struct be_softc *sc;
843 	u_int32_t why;
844 {
845 	int r = 0, rst = 0;
846 
847 	if (why & BE_BR_STAT_RFIFOVF) {
848 		r |= 1;
849 		rst = 1;
850 		aprint_error_dev(&sc->sc_dev, "receive fifo overrun\n");
851 	}
852 	if (why & BE_BR_STAT_TFIFO_UND) {
853 		r |= 1;
854 		rst = 1;
855 		aprint_error_dev(&sc->sc_dev, "transmit fifo underrun\n");
856 	}
857 	if (why & BE_BR_STAT_MAXPKTERR) {
858 		r |= 1;
859 		rst = 1;
860 		aprint_error_dev(&sc->sc_dev, "max packet size error\n");
861 	}
862 
863 	if (!r) {
864 		rst = 1;
865 		aprint_error_dev(&sc->sc_dev, "unexpected error interrupt %08x\n",
866 			why);
867 	}
868 
869 	if (rst) {
870 		printf("%s: resetting\n", device_xname(&sc->sc_dev));
871 		bereset(sc);
872 	}
873 
874 	return (r);
875 }
876 
877 /*
878  * Transmit interrupt.
879  */
880 int
881 betint(sc)
882 	struct be_softc *sc;
883 {
884 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
885 	bus_space_tag_t t = sc->sc_bustag;
886 	bus_space_handle_t br = sc->sc_br;
887 	unsigned int bix, txflags;
888 
889 	/*
890 	 * Unload collision counters
891 	 */
892 	ifp->if_collisions +=
893 		bus_space_read_4(t, br, BE_BRI_NCCNT) +
894 		bus_space_read_4(t, br, BE_BRI_FCCNT) +
895 		bus_space_read_4(t, br, BE_BRI_EXCNT) +
896 		bus_space_read_4(t, br, BE_BRI_LTCNT);
897 
898 	/*
899 	 * the clear the hardware counters
900 	 */
901 	bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
902 	bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
903 	bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
904 	bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
905 
906 	bix = sc->sc_rb.rb_tdtail;
907 
908 	for (;;) {
909 		if (sc->sc_rb.rb_td_nbusy <= 0)
910 			break;
911 
912 		txflags = sc->sc_rb.rb_txd[bix].xd_flags;
913 
914 		if (txflags & QEC_XD_OWN)
915 			break;
916 
917 		ifp->if_flags &= ~IFF_OACTIVE;
918 		ifp->if_opackets++;
919 
920 		if (++bix == QEC_XD_RING_MAXSIZE)
921 			bix = 0;
922 
923 		--sc->sc_rb.rb_td_nbusy;
924 	}
925 
926 	sc->sc_rb.rb_tdtail = bix;
927 
928 	bestart(ifp);
929 
930 	if (sc->sc_rb.rb_td_nbusy == 0)
931 		ifp->if_timer = 0;
932 
933 	return (1);
934 }
935 
936 /*
937  * Receive interrupt.
938  */
939 int
940 berint(sc)
941 	struct be_softc *sc;
942 {
943 	struct qec_xd *xd = sc->sc_rb.rb_rxd;
944 	unsigned int bix, len;
945 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
946 
947 	bix = sc->sc_rb.rb_rdtail;
948 
949 	/*
950 	 * Process all buffers with valid data.
951 	 */
952 	for (;;) {
953 		len = xd[bix].xd_flags;
954 		if (len & QEC_XD_OWN)
955 			break;
956 
957 		len &= QEC_XD_LENGTH;
958 		be_read(sc, bix, len);
959 
960 		/* ... */
961 		xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
962 			QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
963 
964 		if (++bix == QEC_XD_RING_MAXSIZE)
965 			bix = 0;
966 	}
967 
968 	sc->sc_rb.rb_rdtail = bix;
969 
970 	return (1);
971 }
972 
973 int
974 beioctl(struct ifnet *ifp, u_long cmd, void *data)
975 {
976 	struct be_softc *sc = ifp->if_softc;
977 	struct ifaddr *ifa = (struct ifaddr *)data;
978 	struct ifreq *ifr = (struct ifreq *)data;
979 	int s, error = 0;
980 
981 	s = splnet();
982 
983 	switch (cmd) {
984 	case SIOCINITIFADDR:
985 		ifp->if_flags |= IFF_UP;
986 		beinit(sc);
987 		switch (ifa->ifa_addr->sa_family) {
988 #ifdef INET
989 		case AF_INET:
990 			arp_ifinit(ifp, ifa);
991 			break;
992 #endif /* INET */
993 		default:
994 			break;
995 		}
996 		break;
997 
998 	case SIOCSIFFLAGS:
999 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1000 			break;
1001 		/* XXX re-use ether_ioctl() */
1002 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1003 		case IFF_RUNNING:
1004 			/*
1005 			 * If interface is marked down and it is running, then
1006 			 * stop it.
1007 			 */
1008 			bestop(sc);
1009 			ifp->if_flags &= ~IFF_RUNNING;
1010 			break;
1011 		case IFF_UP:
1012 			/*
1013 			 * If interface is marked up and it is stopped, then
1014 			 * start it.
1015 			 */
1016 			beinit(sc);
1017 			break;
1018 		default:
1019 			/*
1020 			 * Reset the interface to pick up changes in any other
1021 			 * flags that affect hardware registers.
1022 			 */
1023 			bestop(sc);
1024 			beinit(sc);
1025 			break;
1026 		}
1027 #ifdef BEDEBUG
1028 		if (ifp->if_flags & IFF_DEBUG)
1029 			sc->sc_debug = 1;
1030 		else
1031 			sc->sc_debug = 0;
1032 #endif
1033 		break;
1034 
1035 	case SIOCADDMULTI:
1036 	case SIOCDELMULTI:
1037 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1038 			/*
1039 			 * Multicast list has changed; set the hardware filter
1040 			 * accordingly.
1041 			 */
1042 			if (ifp->if_flags & IFF_RUNNING)
1043 				be_mcreset(sc);
1044 			error = 0;
1045 		}
1046 		break;
1047 	case SIOCGIFMEDIA:
1048 	case SIOCSIFMEDIA:
1049 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1050 		break;
1051 	default:
1052 		error = ether_ioctl(ifp, cmd, data);
1053 		break;
1054 	}
1055 	splx(s);
1056 	return (error);
1057 }
1058 
1059 
1060 void
1061 beinit(sc)
1062 	struct be_softc *sc;
1063 {
1064 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1065 	bus_space_tag_t t = sc->sc_bustag;
1066 	bus_space_handle_t br = sc->sc_br;
1067 	bus_space_handle_t cr = sc->sc_cr;
1068 	struct qec_softc *qec = sc->sc_qec;
1069 	u_int32_t v;
1070 	u_int32_t qecaddr;
1071 	u_int8_t *ea;
1072 	int rc, s;
1073 
1074 	s = splnet();
1075 
1076 	qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1077 
1078 	bestop(sc);
1079 
1080 	ea = sc->sc_enaddr;
1081 	bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1082 	bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1083 	bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1084 
1085 	/* Clear hash table */
1086 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1087 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1088 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1089 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1090 
1091 	/* Re-initialize RX configuration */
1092 	v = BE_BR_RXCFG_FIFO;
1093 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1094 
1095 	be_mcreset(sc);
1096 
1097 	bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1098 
1099 	bus_space_write_4(t, br, BE_BRI_XIFCFG,
1100 			  BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1101 
1102 	bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1103 
1104 	/*
1105 	 * Turn off counter expiration interrupts as well as
1106 	 * 'gotframe' and 'sentframe'
1107 	 */
1108 	bus_space_write_4(t, br, BE_BRI_IMASK,
1109 			  BE_BR_IMASK_GOTFRAME	|
1110 			  BE_BR_IMASK_RCNTEXP	|
1111 			  BE_BR_IMASK_ACNTEXP	|
1112 			  BE_BR_IMASK_CCNTEXP	|
1113 			  BE_BR_IMASK_LCNTEXP	|
1114 			  BE_BR_IMASK_CVCNTEXP	|
1115 			  BE_BR_IMASK_SENTFRAME	|
1116 			  BE_BR_IMASK_NCNTEXP	|
1117 			  BE_BR_IMASK_ECNTEXP	|
1118 			  BE_BR_IMASK_LCCNTEXP	|
1119 			  BE_BR_IMASK_FCNTEXP	|
1120 			  BE_BR_IMASK_DTIMEXP);
1121 
1122 	/* Channel registers: */
1123 	bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1124 	bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1125 
1126 	qecaddr = sc->sc_channel * qec->sc_msize;
1127 	bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1128 	bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1129 	bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1130 	bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1131 
1132 	bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1133 	bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1134 	bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1135 	bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1136 	bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1137 
1138 	/* Set max packet length */
1139 	v = ETHER_MAX_LEN;
1140 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1141 		v += ETHER_VLAN_ENCAP_LEN;
1142 	bus_space_write_4(t, br, BE_BRI_RXMAX, v);
1143 	bus_space_write_4(t, br, BE_BRI_TXMAX, v);
1144 
1145 	/* Enable transmitter */
1146 	bus_space_write_4(t, br, BE_BRI_TXCFG,
1147 			  BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1148 
1149 	/* Enable receiver */
1150 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1151 	v |= BE_BR_RXCFG_FIFO | BE_BR_RXCFG_ENABLE;
1152 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1153 
1154 	if ((rc = be_ifmedia_upd(ifp)) != 0)
1155 		goto out;
1156 
1157 	ifp->if_flags |= IFF_RUNNING;
1158 	ifp->if_flags &= ~IFF_OACTIVE;
1159 
1160 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
1161 out:
1162 	splx(s);
1163 }
1164 
1165 void
1166 be_mcreset(sc)
1167 	struct be_softc *sc;
1168 {
1169 	struct ethercom *ec = &sc->sc_ethercom;
1170 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1171 	bus_space_tag_t t = sc->sc_bustag;
1172 	bus_space_handle_t br = sc->sc_br;
1173 	u_int32_t crc;
1174 	u_int16_t hash[4];
1175 	u_int8_t octet;
1176 	u_int32_t v;
1177 	int i, j;
1178 	struct ether_multi *enm;
1179 	struct ether_multistep step;
1180 
1181 	if (ifp->if_flags & IFF_PROMISC) {
1182 		v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1183 		v |= BE_BR_RXCFG_PMISC;
1184 		bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1185 		return;
1186 	}
1187 
1188 	if (ifp->if_flags & IFF_ALLMULTI) {
1189 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1190 		goto chipit;
1191 	}
1192 
1193 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
1194 
1195 	ETHER_FIRST_MULTI(step, ec, enm);
1196 	while (enm != NULL) {
1197 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1198 			/*
1199 			 * We must listen to a range of multicast
1200 			 * addresses.  For now, just accept all
1201 			 * multicasts, rather than trying to set only
1202 			 * those filter bits needed to match the range.
1203 			 * (At this time, the only use of address
1204 			 * ranges is for IP multicast routing, for
1205 			 * which the range is big enough to require
1206 			 * all bits set.)
1207 			 */
1208 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1209 			ifp->if_flags |= IFF_ALLMULTI;
1210 			goto chipit;
1211 		}
1212 
1213 		crc = 0xffffffff;
1214 
1215 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
1216 			octet = enm->enm_addrlo[i];
1217 
1218 			for (j = 0; j < 8; j++) {
1219 				if ((crc & 1) ^ (octet & 1)) {
1220 					crc >>= 1;
1221 					crc ^= MC_POLY_LE;
1222 				}
1223 				else
1224 					crc >>= 1;
1225 				octet >>= 1;
1226 			}
1227 		}
1228 
1229 		crc >>= 26;
1230 		hash[crc >> 4] |= 1 << (crc & 0xf);
1231 		ETHER_NEXT_MULTI(step, enm);
1232 	}
1233 
1234 	ifp->if_flags &= ~IFF_ALLMULTI;
1235 
1236 chipit:
1237 	/* Enable the hash filter */
1238 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1239 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1240 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1241 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1242 
1243 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1244 	v &= ~BE_BR_RXCFG_PMISC;
1245 	v |= BE_BR_RXCFG_HENABLE;
1246 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1247 }
1248 
1249 /*
1250  * Set the tcvr to an idle state
1251  */
1252 void
1253 be_mii_sync(sc)
1254 	struct be_softc *sc;
1255 {
1256 	bus_space_tag_t t = sc->sc_bustag;
1257 	bus_space_handle_t tr = sc->sc_tr;
1258 	int n = 32;
1259 
1260 	while (n--) {
1261 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1262 				  MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1263 				  MGMT_PAL_OENAB);
1264 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1265 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1266 				  MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1267 				  MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1268 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1269 	}
1270 }
1271 
1272 void
1273 be_pal_gate(sc, phy)
1274 	struct be_softc *sc;
1275 	int phy;
1276 {
1277 	bus_space_tag_t t = sc->sc_bustag;
1278 	bus_space_handle_t tr = sc->sc_tr;
1279 	u_int32_t v;
1280 
1281 	be_mii_sync(sc);
1282 
1283 	v = ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE);
1284 	if (phy == BE_PHY_INTERNAL)
1285 		v &= ~TCVR_PAL_SERIAL;
1286 
1287 	bus_space_write_4(t, tr, BE_TRI_TCVRPAL, v);
1288 	(void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1289 }
1290 
1291 static int
1292 be_tcvr_read_bit(sc, phy)
1293 	struct be_softc *sc;
1294 	int phy;
1295 {
1296 	bus_space_tag_t t = sc->sc_bustag;
1297 	bus_space_handle_t tr = sc->sc_tr;
1298 	int ret;
1299 
1300 	if (phy == BE_PHY_INTERNAL) {
1301 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1302 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1303 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1304 				  MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1305 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1306 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1307 			MGMT_PAL_INT_MDIO) >> MGMT_PAL_INT_MDIO_SHIFT;
1308 	} else {
1309 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
1310 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1311 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1312 			MGMT_PAL_EXT_MDIO) >> MGMT_PAL_EXT_MDIO_SHIFT;
1313 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1314 				  MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1315 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1316 	}
1317 
1318 	return (ret);
1319 }
1320 
1321 static void
1322 be_tcvr_write_bit(sc, phy, bit)
1323 	struct be_softc *sc;
1324 	int phy;
1325 	int bit;
1326 {
1327 	bus_space_tag_t t = sc->sc_bustag;
1328 	bus_space_handle_t tr = sc->sc_tr;
1329 	u_int32_t v;
1330 
1331 	if (phy == BE_PHY_INTERNAL) {
1332 		v = ((bit & 1) << MGMT_PAL_INT_MDIO_SHIFT) |
1333 			MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1334 	} else {
1335 		v = ((bit & 1) << MGMT_PAL_EXT_MDIO_SHIFT)
1336 			| MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1337 	}
1338 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
1339 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1340 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v | MGMT_PAL_DCLOCK);
1341 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1342 }
1343 
1344 static void
1345 be_mii_sendbits(sc, phy, data, nbits)
1346 	struct be_softc *sc;
1347 	int phy;
1348 	u_int32_t data;
1349 	int nbits;
1350 {
1351 	int i;
1352 
1353 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1354 		be_tcvr_write_bit(sc, phy, (data & i) != 0);
1355 	}
1356 }
1357 
1358 static int
1359 be_mii_readreg(self, phy, reg)
1360 	struct device *self;
1361 	int phy, reg;
1362 {
1363 	struct be_softc *sc = (struct be_softc *)self;
1364 	int val = 0, i;
1365 
1366 	/*
1367 	 * Read the PHY register by manually driving the MII control lines.
1368 	 */
1369 	be_mii_sync(sc);
1370 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1371 	be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1372 	be_mii_sendbits(sc, phy, phy, 5);
1373 	be_mii_sendbits(sc, phy, reg, 5);
1374 
1375 	(void) be_tcvr_read_bit(sc, phy);
1376 	(void) be_tcvr_read_bit(sc, phy);
1377 
1378 	for (i = 15; i >= 0; i--)
1379 		val |= (be_tcvr_read_bit(sc, phy) << i);
1380 
1381 	(void) be_tcvr_read_bit(sc, phy);
1382 	(void) be_tcvr_read_bit(sc, phy);
1383 	(void) be_tcvr_read_bit(sc, phy);
1384 
1385 	return (val);
1386 }
1387 
1388 void
1389 be_mii_writereg(self, phy, reg, val)
1390 	struct device *self;
1391 	int phy, reg, val;
1392 {
1393 	struct be_softc *sc = (struct be_softc *)self;
1394 	int i;
1395 
1396 	/*
1397 	 * Write the PHY register by manually driving the MII control lines.
1398 	 */
1399 	be_mii_sync(sc);
1400 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1401 	be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1402 	be_mii_sendbits(sc, phy, phy, 5);
1403 	be_mii_sendbits(sc, phy, reg, 5);
1404 
1405 	be_tcvr_write_bit(sc, phy, 1);
1406 	be_tcvr_write_bit(sc, phy, 0);
1407 
1408 	for (i = 15; i >= 0; i--)
1409 		be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1410 }
1411 
1412 int
1413 be_mii_reset(sc, phy)
1414 	struct be_softc *sc;
1415 	int phy;
1416 {
1417 	int n;
1418 
1419 	be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1420 			BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1421 	be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1422 
1423 	for (n = 16; n >= 0; n--) {
1424 		int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1425 		if ((bmcr & BMCR_RESET) == 0)
1426 			break;
1427 		DELAY(20);
1428 	}
1429 	if (n == 0) {
1430 		aprint_error_dev(&sc->sc_dev, "bmcr reset failed\n");
1431 		return (EIO);
1432 	}
1433 
1434 	return (0);
1435 }
1436 
1437 void
1438 be_tick(arg)
1439 	void	*arg;
1440 {
1441 	struct be_softc *sc = arg;
1442 	int s = splnet();
1443 
1444 	mii_tick(&sc->sc_mii);
1445 	(void)be_intphy_service(sc, &sc->sc_mii, MII_TICK);
1446 
1447 	splx(s);
1448 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
1449 }
1450 
1451 void
1452 be_mii_statchg(self)
1453 	struct device *self;
1454 {
1455 	struct be_softc *sc = (struct be_softc *)self;
1456 	bus_space_tag_t t = sc->sc_bustag;
1457 	bus_space_handle_t br = sc->sc_br;
1458 	u_int instance;
1459 	u_int32_t v;
1460 
1461 	instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1462 #ifdef DIAGNOSTIC
1463 	if (instance > 1)
1464 		panic("be_mii_statchg: instance %d out of range", instance);
1465 #endif
1466 
1467 	/* Update duplex mode in TX configuration */
1468 	v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1469 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1470 		v |= BE_BR_TXCFG_FULLDPLX;
1471 	else
1472 		v &= ~BE_BR_TXCFG_FULLDPLX;
1473 	bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1474 
1475 	/* Change to appropriate gate in transceiver PAL */
1476 	be_pal_gate(sc, sc->sc_phys[instance]);
1477 }
1478 
1479 /*
1480  * Get current media settings.
1481  */
1482 void
1483 be_ifmedia_sts(ifp, ifmr)
1484 	struct ifnet *ifp;
1485 	struct ifmediareq *ifmr;
1486 {
1487 	struct be_softc *sc = ifp->if_softc;
1488 
1489 	mii_pollstat(&sc->sc_mii);
1490 	(void)be_intphy_service(sc, &sc->sc_mii, MII_POLLSTAT);
1491 
1492 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
1493 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
1494 	return;
1495 }
1496 
1497 /*
1498  * Set media options.
1499  */
1500 int
1501 be_ifmedia_upd(ifp)
1502 	struct ifnet *ifp;
1503 {
1504 	struct be_softc *sc = ifp->if_softc;
1505 	int error;
1506 
1507 	if ((error = mii_mediachg(&sc->sc_mii)) == ENXIO)
1508 		error = 0;
1509 	else if (error != 0)
1510 		return error;
1511 
1512 	return (be_intphy_service(sc, &sc->sc_mii, MII_MEDIACHG));
1513 }
1514 
1515 /*
1516  * Service routine for our pseudo-MII internal transceiver.
1517  */
1518 int
1519 be_intphy_service(sc, mii, cmd)
1520 	struct be_softc *sc;
1521 	struct mii_data *mii;
1522 	int cmd;
1523 {
1524 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
1525 	int bmcr, bmsr;
1526 	int error;
1527 
1528 	switch (cmd) {
1529 	case MII_POLLSTAT:
1530 		/*
1531 		 * If we're not polling our PHY instance, just return.
1532 		 */
1533 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
1534 			return (0);
1535 
1536 		break;
1537 
1538 	case MII_MEDIACHG:
1539 
1540 		/*
1541 		 * If the media indicates a different PHY instance,
1542 		 * isolate ourselves.
1543 		 */
1544 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst) {
1545 			bmcr = be_mii_readreg((void *)sc,
1546 				BE_PHY_INTERNAL, MII_BMCR);
1547 			be_mii_writereg((void *)sc,
1548 				BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
1549 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
1550 			sc->sc_intphy_curspeed = 0;
1551 			return (0);
1552 		}
1553 
1554 
1555 		if ((error = be_mii_reset(sc, BE_PHY_INTERNAL)) != 0)
1556 			return (error);
1557 
1558 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1559 
1560 		/*
1561 		 * Select the new mode and take out of isolation
1562 		 */
1563 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
1564 			bmcr |= BMCR_S100;
1565 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_10_T)
1566 			bmcr &= ~BMCR_S100;
1567 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
1568 			if ((sc->sc_mii_flags & MIIF_HAVELINK) != 0) {
1569 				bmcr &= ~BMCR_S100;
1570 				bmcr |= sc->sc_intphy_curspeed;
1571 			} else {
1572 				/* Keep isolated until link is up */
1573 				bmcr |= BMCR_ISO;
1574 				sc->sc_mii_flags |= MIIF_DOINGAUTO;
1575 			}
1576 		}
1577 
1578 		if ((IFM_OPTIONS(ife->ifm_media) & IFM_FDX) != 0)
1579 			bmcr |= BMCR_FDX;
1580 		else
1581 			bmcr &= ~BMCR_FDX;
1582 
1583 		be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1584 		break;
1585 
1586 	case MII_TICK:
1587 		/*
1588 		 * If we're not currently selected, just return.
1589 		 */
1590 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
1591 			return (0);
1592 
1593 		/* Only used for automatic media selection */
1594 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
1595 			return (0);
1596 
1597 		/* Is the interface even up? */
1598 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
1599 			return (0);
1600 
1601 		/*
1602 		 * Check link status; if we don't have a link, try another
1603 		 * speed. We can't detect duplex mode, so half-duplex is
1604 		 * what we have to settle for.
1605 		 */
1606 
1607 		/* Read twice in case the register is latched */
1608 		bmsr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR) |
1609 		       be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR);
1610 
1611 		if ((bmsr & BMSR_LINK) != 0) {
1612 			/* We have a carrier */
1613 			bmcr = be_mii_readreg((void *)sc,
1614 					BE_PHY_INTERNAL, MII_BMCR);
1615 
1616 			if ((sc->sc_mii_flags & MIIF_DOINGAUTO) != 0) {
1617 				bmcr = be_mii_readreg((void *)sc,
1618 						BE_PHY_INTERNAL, MII_BMCR);
1619 
1620 				sc->sc_mii_flags |= MIIF_HAVELINK;
1621 				sc->sc_intphy_curspeed = (bmcr & BMCR_S100);
1622 				sc->sc_mii_flags &= ~MIIF_DOINGAUTO;
1623 
1624 				bmcr &= ~BMCR_ISO;
1625 				be_mii_writereg((void *)sc,
1626 					BE_PHY_INTERNAL, MII_BMCR, bmcr);
1627 
1628 				printf("%s: link up at %s Mbps\n",
1629 					device_xname(&sc->sc_dev),
1630 					(bmcr & BMCR_S100) ? "100" : "10");
1631 			}
1632 			return (0);
1633 		}
1634 
1635 		if ((sc->sc_mii_flags & MIIF_DOINGAUTO) == 0) {
1636 			sc->sc_mii_flags |= MIIF_DOINGAUTO;
1637 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
1638 			sc->sc_intphy_curspeed = 0;
1639 			printf("%s: link down\n", device_xname(&sc->sc_dev));
1640 		}
1641 
1642 		/* Only retry autonegotiation every 5 seconds. */
1643 		if (++sc->sc_mii_ticks < 5)
1644 			return(0);
1645 
1646 		sc->sc_mii_ticks = 0;
1647 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1648 		/* Just flip the fast speed bit */
1649 		bmcr ^= BMCR_S100;
1650 		be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1651 
1652 		break;
1653 
1654 	case MII_DOWN:
1655 		/* Isolate this phy */
1656 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1657 		be_mii_writereg((void *)sc,
1658 				BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
1659 		return (0);
1660 	}
1661 
1662 	/* Update the media status. */
1663 	be_intphy_status(sc);
1664 
1665 	/* Callback if something changed. */
1666 	if (sc->sc_mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
1667 		(*mii->mii_statchg)((struct device *)sc);
1668 		sc->sc_mii_active = mii->mii_media_active;
1669 	}
1670 	return (0);
1671 }
1672 
1673 /*
1674  * Determine status of internal transceiver
1675  */
1676 void
1677 be_intphy_status(sc)
1678 	struct be_softc *sc;
1679 {
1680 	struct mii_data *mii = &sc->sc_mii;
1681 	int media_active, media_status;
1682 	int bmcr, bmsr;
1683 
1684 	media_status = IFM_AVALID;
1685 	media_active = 0;
1686 
1687 	/*
1688 	 * Internal transceiver; do the work here.
1689 	 */
1690 	bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1691 
1692 	switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1693 	case (BMCR_S100 | BMCR_FDX):
1694 		media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1695 		break;
1696 	case BMCR_S100:
1697 		media_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1698 		break;
1699 	case BMCR_FDX:
1700 		media_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1701 		break;
1702 	case 0:
1703 		media_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1704 		break;
1705 	}
1706 
1707 	/* Read twice in case the register is latched */
1708 	bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1709 	       be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1710 	if (bmsr & BMSR_LINK)
1711 		media_status |=  IFM_ACTIVE;
1712 
1713 	mii->mii_media_status = media_status;
1714 	mii->mii_media_active = media_active;
1715 }
1716