xref: /netbsd-src/sys/dev/marvell/if_gfe.c (revision 0c4ddb1599a0bea866fde8522a74cfbd2f68cd1b)
1 /*	$NetBSD: if_gfe.c,v 1.30 2008/06/10 22:44:07 he Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project by
18  *      Allegro Networks, Inc., and Wasabi Systems, Inc.
19  * 4. The name of Allegro Networks, Inc. may not be used to endorse
20  *    or promote products derived from this software without specific prior
21  *    written permission.
22  * 5. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
27  * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30  * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * if_gfe.c -- GT ethernet MAC driver
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: if_gfe.c,v 1.30 2008/06/10 22:44:07 he Exp $");
46 
47 #include "opt_inet.h"
48 #include "bpfilter.h"
49 
50 #include <sys/param.h>
51 #include <sys/types.h>
52 #include <sys/inttypes.h>
53 #include <sys/queue.h>
54 
55 #include <uvm/uvm_extern.h>
56 
57 #include <sys/callout.h>
58 #include <sys/device.h>
59 #include <sys/errno.h>
60 #include <sys/ioctl.h>
61 #include <sys/mbuf.h>
62 #include <sys/socket.h>
63 
64 #include <sys/bus.h>
65 
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_ether.h>
69 #include <net/if_media.h>
70 
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/if_inarp.h>
74 #endif
75 #if NBPFILTER > 0
76 #include <net/bpf.h>
77 #endif
78 
79 #include <dev/mii/miivar.h>
80 
81 #include <dev/marvell/gtintrreg.h>
82 #include <dev/marvell/gtethreg.h>
83 
84 #include <dev/marvell/gtvar.h>
85 #include <dev/marvell/if_gfevar.h>
86 
87 #define	GE_READ(sc, reg) \
88 	bus_space_read_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg)
89 #define	GE_WRITE(sc, reg, v) \
90 	bus_space_write_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg, (v))
91 
92 #define	GE_DEBUG
93 #if 0
94 #define	GE_NOHASH
95 #define	GE_NORX
96 #endif
97 
98 #ifdef GE_DEBUG
99 #define	GE_DPRINTF(sc, a)	do \
100 				  if ((sc)->sc_ec.ec_if.if_flags & IFF_DEBUG) \
101 				    printf a; \
102 				while (0)
103 #define	GE_FUNC_ENTER(sc, func)	GE_DPRINTF(sc, ("[" func))
104 #define	GE_FUNC_EXIT(sc, str)	GE_DPRINTF(sc, (str "]"))
105 #else
106 #define	GE_DPRINTF(sc, a)	do { } while (0)
107 #define	GE_FUNC_ENTER(sc, func)	do { } while (0)
108 #define	GE_FUNC_EXIT(sc, str)	do { } while (0)
109 #endif
110 enum gfe_whack_op {
111 	GE_WHACK_START,		GE_WHACK_RESTART,
112 	GE_WHACK_CHANGE,	GE_WHACK_STOP
113 };
114 
115 enum gfe_hash_op {
116 	GE_HASH_ADD,		GE_HASH_REMOVE,
117 };
118 
119 #if 1
120 #define	htogt32(a)		htobe32(a)
121 #define	gt32toh(a)		be32toh(a)
122 #else
123 #define	htogt32(a)		htole32(a)
124 #define	gt32toh(a)		le32toh(a)
125 #endif
126 
127 #define GE_RXDSYNC(sc, rxq, n, ops) \
128 	bus_dmamap_sync((sc)->sc_dmat, (rxq)->rxq_desc_mem.gdm_map, \
129 	    (n) * sizeof((rxq)->rxq_descs[0]), sizeof((rxq)->rxq_descs[0]), \
130 	    (ops))
131 #define	GE_RXDPRESYNC(sc, rxq, n) \
132 	GE_RXDSYNC(sc, rxq, n, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)
133 #define	GE_RXDPOSTSYNC(sc, rxq, n) \
134 	GE_RXDSYNC(sc, rxq, n, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)
135 
136 #define GE_TXDSYNC(sc, txq, n, ops) \
137 	bus_dmamap_sync((sc)->sc_dmat, (txq)->txq_desc_mem.gdm_map, \
138 	    (n) * sizeof((txq)->txq_descs[0]), sizeof((txq)->txq_descs[0]), \
139 	    (ops))
140 #define	GE_TXDPRESYNC(sc, txq, n) \
141 	GE_TXDSYNC(sc, txq, n, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)
142 #define	GE_TXDPOSTSYNC(sc, txq, n) \
143 	GE_TXDSYNC(sc, txq, n, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)
144 
145 #define	STATIC
146 
147 STATIC int gfe_match (struct device *, struct cfdata *, void *);
148 STATIC void gfe_attach (struct device *, struct device *, void *);
149 
150 STATIC int gfe_dmamem_alloc(struct gfe_softc *, struct gfe_dmamem *, int,
151 	size_t, int);
152 STATIC void gfe_dmamem_free(struct gfe_softc *, struct gfe_dmamem *);
153 
154 STATIC int gfe_ifioctl (struct ifnet *, u_long, void *);
155 STATIC void gfe_ifstart (struct ifnet *);
156 STATIC void gfe_ifwatchdog (struct ifnet *);
157 
158 STATIC int gfe_mii_read (struct device *, int, int);
159 STATIC void gfe_mii_write (struct device *, int, int, int);
160 STATIC void gfe_mii_statchg (struct device *);
161 
162 STATIC void gfe_tick(void *arg);
163 
164 STATIC void gfe_tx_restart(void *);
165 STATIC int gfe_tx_enqueue(struct gfe_softc *, enum gfe_txprio);
166 STATIC uint32_t gfe_tx_done(struct gfe_softc *, enum gfe_txprio, uint32_t);
167 STATIC void gfe_tx_cleanup(struct gfe_softc *, enum gfe_txprio, int);
168 STATIC int gfe_tx_txqalloc(struct gfe_softc *, enum gfe_txprio);
169 STATIC int gfe_tx_start(struct gfe_softc *, enum gfe_txprio);
170 STATIC void gfe_tx_stop(struct gfe_softc *, enum gfe_whack_op);
171 
172 STATIC void gfe_rx_cleanup(struct gfe_softc *, enum gfe_rxprio);
173 STATIC void gfe_rx_get(struct gfe_softc *, enum gfe_rxprio);
174 STATIC int gfe_rx_prime(struct gfe_softc *);
175 STATIC uint32_t gfe_rx_process(struct gfe_softc *, uint32_t, uint32_t);
176 STATIC int gfe_rx_rxqalloc(struct gfe_softc *, enum gfe_rxprio);
177 STATIC int gfe_rx_rxqinit(struct gfe_softc *, enum gfe_rxprio);
178 STATIC void gfe_rx_stop(struct gfe_softc *, enum gfe_whack_op);
179 
180 STATIC int gfe_intr(void *);
181 
182 STATIC int gfe_whack(struct gfe_softc *, enum gfe_whack_op);
183 
184 STATIC int gfe_hash_compute(struct gfe_softc *, const uint8_t [ETHER_ADDR_LEN]);
185 STATIC int gfe_hash_entry_op(struct gfe_softc *, enum gfe_hash_op,
186 	enum gfe_rxprio, const uint8_t [ETHER_ADDR_LEN]);
187 STATIC int gfe_hash_multichg(struct ethercom *, const struct ether_multi *,
188 	u_long);
189 STATIC int gfe_hash_fill(struct gfe_softc *);
190 STATIC int gfe_hash_alloc(struct gfe_softc *);
191 
192 /* Linkup to the rest of the kernel */
193 CFATTACH_DECL(gfe, sizeof(struct gfe_softc),
194     gfe_match, gfe_attach, NULL, NULL);
195 
196 extern struct cfdriver gfe_cd;
197 
198 int
199 gfe_match(struct device *parent, struct cfdata *cf, void *aux)
200 {
201 	struct gt_softc *gt = (struct gt_softc *) parent;
202 	struct gt_attach_args *ga = aux;
203 	uint8_t enaddr[6];
204 
205 	if (!GT_ETHEROK(gt, ga, &gfe_cd))
206 		return 0;
207 
208 	if (gtget_macaddr(gt, ga->ga_unit, enaddr) < 0)
209 		return 0;
210 
211 	if (enaddr[0] == 0 && enaddr[1] == 0 && enaddr[2] == 0 &&
212 	    enaddr[3] == 0 && enaddr[4] == 0 && enaddr[5] == 0)
213 		return 0;
214 
215 	return 1;
216 }
217 
218 /*
219  * Attach this instance, and then all the sub-devices
220  */
221 void
222 gfe_attach(struct device *parent, struct device *self, void *aux)
223 {
224 	struct gt_attach_args * const ga = aux;
225 	struct gt_softc * const gt = device_private(parent);
226 	struct gfe_softc * const sc = device_private(self);
227 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
228 	uint32_t data;
229 	uint8_t enaddr[6];
230 	int phyaddr;
231 	uint32_t sdcr;
232 	int error;
233 
234 	GT_ETHERFOUND(gt, ga);
235 
236 	sc->sc_gt_memt = ga->ga_memt;
237 	sc->sc_gt_memh = ga->ga_memh;
238 	sc->sc_dmat = ga->ga_dmat;
239 	sc->sc_macno = ga->ga_unit;
240 
241 	if (bus_space_subregion(sc->sc_gt_memt, sc->sc_gt_memh,
242 		    ETH_BASE(sc->sc_macno), ETH_SIZE, &sc->sc_memh)) {
243 		aprint_error(": failed to map registers\n");
244 	}
245 
246 	callout_init(&sc->sc_co, 0);
247 
248 	data = bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, ETH_EPAR);
249 	phyaddr = ETH_EPAR_PhyAD_GET(data, sc->sc_macno);
250 
251 	gtget_macaddr(gt, sc->sc_macno, enaddr);
252 
253 	sc->sc_pcr = GE_READ(sc, EPCR);
254 	sc->sc_pcxr = GE_READ(sc, EPCXR);
255 	sc->sc_intrmask = GE_READ(sc, EIMR) | ETH_IR_MIIPhySTC;
256 
257 	aprint_normal(": address %s", ether_sprintf(enaddr));
258 
259 #if defined(DEBUG)
260 	aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
261 #endif
262 
263 	sc->sc_pcxr &= ~ETH_EPCXR_PRIOrx_Override;
264 	if (device_cfdata(&sc->sc_dev)->cf_flags & 1) {
265 		aprint_normal(", phy %d (rmii)", phyaddr);
266 		sc->sc_pcxr |= ETH_EPCXR_RMIIEn;
267 	} else {
268 		aprint_normal(", phy %d (mii)", phyaddr);
269 		sc->sc_pcxr &= ~ETH_EPCXR_RMIIEn;
270 	}
271 	if (device_cfdata(&sc->sc_dev)->cf_flags & 2)
272 		sc->sc_flags |= GE_NOFREE;
273 	sc->sc_pcxr &= ~(3 << 14);
274 	sc->sc_pcxr |= (ETH_EPCXR_MFL_1536 << 14);
275 
276 	if (sc->sc_pcr & ETH_EPCR_EN) {
277 		int tries = 1000;
278 		/*
279 		 * Abort transmitter and receiver and wait for them to quiese
280 		 */
281 		GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR|ETH_ESDCMR_AT);
282 		do {
283 			delay(100);
284 		} while (tries-- > 0 && (GE_READ(sc, ESDCMR) & (ETH_ESDCMR_AR|ETH_ESDCMR_AT)));
285 	}
286 
287 	sc->sc_pcr &= ~(ETH_EPCR_EN | ETH_EPCR_RBM | ETH_EPCR_PM | ETH_EPCR_PBF);
288 
289 #if defined(DEBUG)
290 	aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
291 #endif
292 
293 	/*
294 	 * Now turn off the GT.  If it didn't quiese, too ***ing bad.
295 	 */
296 	GE_WRITE(sc, EPCR, sc->sc_pcr);
297 	GE_WRITE(sc, EIMR, sc->sc_intrmask);
298 	sdcr = GE_READ(sc, ESDCR);
299 	ETH_ESDCR_BSZ_SET(sdcr, ETH_ESDCR_BSZ_4);
300 	sdcr |= ETH_ESDCR_RIFB;
301 	GE_WRITE(sc, ESDCR, sdcr);
302 	sc->sc_max_frame_length = 1536;
303 
304 	aprint_normal("\n");
305 	sc->sc_mii.mii_ifp = ifp;
306 	sc->sc_mii.mii_readreg = gfe_mii_read;
307 	sc->sc_mii.mii_writereg = gfe_mii_write;
308 	sc->sc_mii.mii_statchg = gfe_mii_statchg;
309 
310 	sc->sc_ec.ec_mii = &sc->sc_mii;
311 	ifmedia_init(&sc->sc_mii.mii_media, 0, ether_mediachange,
312 		ether_mediastatus);
313 
314 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, phyaddr,
315 		MII_OFFSET_ANY, MIIF_NOISOLATE);
316 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
317 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
318 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
319 	} else {
320 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
321 	}
322 
323 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
324 	ifp->if_softc = sc;
325 	/* ifp->if_mowner = &sc->sc_mowner; */
326 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
327 #if 0
328 	ifp->if_flags |= IFF_DEBUG;
329 #endif
330 	ifp->if_ioctl = gfe_ifioctl;
331 	ifp->if_start = gfe_ifstart;
332 	ifp->if_watchdog = gfe_ifwatchdog;
333 
334 	if (sc->sc_flags & GE_NOFREE) {
335 		error = gfe_rx_rxqalloc(sc, GE_RXPRIO_HI);
336 		if (!error)
337 			error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDHI);
338 		if (!error)
339 			error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDLO);
340 		if (!error)
341 			error = gfe_rx_rxqalloc(sc, GE_RXPRIO_LO);
342 		if (!error)
343 			error = gfe_tx_txqalloc(sc, GE_TXPRIO_HI);
344 		if (!error)
345 			error = gfe_hash_alloc(sc);
346 		if (error)
347 			aprint_error(
348 			    "%s: failed to allocate resources: %d\n",
349 			    ifp->if_xname, error);
350 	}
351 
352 	if_attach(ifp);
353 	ether_ifattach(ifp, enaddr);
354 #if NBPFILTER > 0
355 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
356 #endif
357 #if NRND > 0
358 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self), RND_TYPE_NET, 0);
359 #endif
360 	intr_establish(IRQ_ETH0 + sc->sc_macno, IST_LEVEL, IPL_NET,
361 	    gfe_intr, sc);
362 }
363 
364 int
365 gfe_dmamem_alloc(struct gfe_softc *sc, struct gfe_dmamem *gdm, int maxsegs,
366 	size_t size, int flags)
367 {
368 	int error = 0;
369 	GE_FUNC_ENTER(sc, "gfe_dmamem_alloc");
370 
371 	KASSERT(gdm->gdm_kva == NULL);
372 	gdm->gdm_size = size;
373 	gdm->gdm_maxsegs = maxsegs;
374 
375 	error = bus_dmamem_alloc(sc->sc_dmat, gdm->gdm_size, PAGE_SIZE,
376 	    gdm->gdm_size, gdm->gdm_segs, gdm->gdm_maxsegs, &gdm->gdm_nsegs,
377 	    BUS_DMA_NOWAIT);
378 	if (error)
379 		goto fail;
380 
381 	error = bus_dmamem_map(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs,
382 	    gdm->gdm_size, &gdm->gdm_kva, flags | BUS_DMA_NOWAIT);
383 	if (error)
384 		goto fail;
385 
386 	error = bus_dmamap_create(sc->sc_dmat, gdm->gdm_size, gdm->gdm_nsegs,
387 	    gdm->gdm_size, 0, BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT, &gdm->gdm_map);
388 	if (error)
389 		goto fail;
390 
391 	error = bus_dmamap_load(sc->sc_dmat, gdm->gdm_map, gdm->gdm_kva,
392 	    gdm->gdm_size, NULL, BUS_DMA_NOWAIT);
393 	if (error)
394 		goto fail;
395 
396 	/* invalidate from cache */
397 	bus_dmamap_sync(sc->sc_dmat, gdm->gdm_map, 0, gdm->gdm_size,
398 	    BUS_DMASYNC_PREREAD);
399 fail:
400 	if (error) {
401 		gfe_dmamem_free(sc, gdm);
402 		GE_DPRINTF(sc, (":err=%d", error));
403 	}
404 	GE_DPRINTF(sc, (":kva=%p/%#x,map=%p,nsegs=%d,pa=%x/%x",
405 	    gdm->gdm_kva, gdm->gdm_size, gdm->gdm_map, gdm->gdm_map->dm_nsegs,
406 	    gdm->gdm_map->dm_segs->ds_addr, gdm->gdm_map->dm_segs->ds_len));
407 	GE_FUNC_EXIT(sc, "");
408 	return error;
409 }
410 
411 void
412 gfe_dmamem_free(struct gfe_softc *sc, struct gfe_dmamem *gdm)
413 {
414 	GE_FUNC_ENTER(sc, "gfe_dmamem_free");
415 	if (gdm->gdm_map)
416 		bus_dmamap_destroy(sc->sc_dmat, gdm->gdm_map);
417 	if (gdm->gdm_kva)
418 		bus_dmamem_unmap(sc->sc_dmat, gdm->gdm_kva, gdm->gdm_size);
419 	if (gdm->gdm_nsegs > 0)
420 		bus_dmamem_free(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs);
421 	gdm->gdm_map = NULL;
422 	gdm->gdm_kva = NULL;
423 	gdm->gdm_nsegs = 0;
424 	GE_FUNC_EXIT(sc, "");
425 }
426 
427 int
428 gfe_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
429 {
430 	struct gfe_softc * const sc = ifp->if_softc;
431 	struct ifreq *ifr = (struct ifreq *) data;
432 	struct ifaddr *ifa = (struct ifaddr *) data;
433 	int s, error = 0;
434 
435 	GE_FUNC_ENTER(sc, "gfe_ifioctl");
436 	s = splnet();
437 
438 	switch (cmd) {
439 	case SIOCSIFADDR:
440 		ifp->if_flags |= IFF_UP;
441 		switch (ifa->ifa_addr->sa_family) {
442 #ifdef INET
443 		case AF_INET:
444 			error = gfe_whack(sc, GE_WHACK_START);
445 			if (error == 0)
446 				arp_ifinit(ifp, ifa);
447 			break;
448 #endif
449 		default:
450 			error = gfe_whack(sc, GE_WHACK_START);
451 			break;
452 		}
453 		break;
454 
455 	case SIOCSIFFLAGS:
456 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
457 		case IFF_UP|IFF_RUNNING:/* active->active, update */
458 			error = gfe_whack(sc, GE_WHACK_CHANGE);
459 			break;
460 		case IFF_RUNNING:	/* not up, so we stop */
461 			error = gfe_whack(sc, GE_WHACK_STOP);
462 			break;
463 		case IFF_UP:		/* not running, so we start */
464 			error = gfe_whack(sc, GE_WHACK_START);
465 			break;
466 		case 0:			/* idle->idle: do nothing */
467 			break;
468 		}
469 		break;
470 
471 	case SIOCSIFMEDIA:
472 	case SIOCGIFMEDIA:
473 	case SIOCADDMULTI:
474 	case SIOCDELMULTI:
475 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
476 			if (ifp->if_flags & IFF_RUNNING)
477 				error = gfe_whack(sc, GE_WHACK_CHANGE);
478 			else
479 				error = 0;
480 		}
481 		break;
482 
483 	case SIOCSIFMTU:
484 		if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
485 			error = EINVAL;
486 			break;
487 		}
488 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
489 			error = 0;
490 		break;
491 
492 	default:
493 		error = EINVAL;
494 		break;
495 	}
496 	splx(s);
497 	GE_FUNC_EXIT(sc, "");
498 	return error;
499 }
500 
501 void
502 gfe_ifstart(struct ifnet *ifp)
503 {
504 	struct gfe_softc * const sc = ifp->if_softc;
505 	struct mbuf *m;
506 
507 	GE_FUNC_ENTER(sc, "gfe_ifstart");
508 
509 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
510 		GE_FUNC_EXIT(sc, "$");
511 		return;
512 	}
513 
514 	for (;;) {
515 		IF_DEQUEUE(&ifp->if_snd, m);
516 		if (m == NULL) {
517 			ifp->if_flags &= ~IFF_OACTIVE;
518 			GE_FUNC_EXIT(sc, "");
519 			return;
520 		}
521 
522 		/*
523 		 * No space in the pending queue?  try later.
524 		 */
525 		if (IF_QFULL(&sc->sc_txq[GE_TXPRIO_HI].txq_pendq))
526 			break;
527 
528 		/*
529 		 * Try to enqueue a mbuf to the device. If that fails, we
530 		 * can always try to map the next mbuf.
531 		 */
532 		IF_ENQUEUE(&sc->sc_txq[GE_TXPRIO_HI].txq_pendq, m);
533 		GE_DPRINTF(sc, (">"));
534 #ifndef GE_NOTX
535 		(void) gfe_tx_enqueue(sc, GE_TXPRIO_HI);
536 #endif
537 	}
538 
539 	/*
540 	 * Attempt to queue the mbuf for send failed.
541 	 */
542 	IF_PREPEND(&ifp->if_snd, m);
543 	ifp->if_flags |= IFF_OACTIVE;
544 	GE_FUNC_EXIT(sc, "%%");
545 }
546 
547 void
548 gfe_ifwatchdog(struct ifnet *ifp)
549 {
550 	struct gfe_softc * const sc = ifp->if_softc;
551 	struct gfe_txqueue * const txq = &sc->sc_txq[GE_TXPRIO_HI];
552 
553 	GE_FUNC_ENTER(sc, "gfe_ifwatchdog");
554 	printf("%s: device timeout", device_xname(&sc->sc_dev));
555 	if (ifp->if_flags & IFF_RUNNING) {
556 		uint32_t curtxdnum = (bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, txq->txq_ectdp) - txq->txq_desc_busaddr) / sizeof(txq->txq_descs[0]);
557 		GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
558 		GE_TXDPOSTSYNC(sc, txq, curtxdnum);
559 		printf(" (fi=%d(%#x),lo=%d,cur=%d(%#x),icm=%#x) ",
560 		    txq->txq_fi, txq->txq_descs[txq->txq_fi].ed_cmdsts,
561 		    txq->txq_lo, curtxdnum, txq->txq_descs[curtxdnum].ed_cmdsts,
562 		    GE_READ(sc, EICR));
563 		GE_TXDPRESYNC(sc, txq, txq->txq_fi);
564 		GE_TXDPRESYNC(sc, txq, curtxdnum);
565 	}
566 	printf("\n");
567 	ifp->if_oerrors++;
568 	(void) gfe_whack(sc, GE_WHACK_RESTART);
569 	GE_FUNC_EXIT(sc, "");
570 }
571 
572 int
573 gfe_rx_rxqalloc(struct gfe_softc *sc, enum gfe_rxprio rxprio)
574 {
575 	struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
576 	int error;
577 
578 	GE_FUNC_ENTER(sc, "gfe_rx_rxqalloc");
579 	GE_DPRINTF(sc, ("(%d)", rxprio));
580 
581 	error = gfe_dmamem_alloc(sc, &rxq->rxq_desc_mem, 1,
582 	    GE_RXDESC_MEMSIZE, BUS_DMA_NOCACHE);
583 	if (error) {
584 		GE_FUNC_EXIT(sc, "!!");
585 		return error;
586 	}
587 
588 	error = gfe_dmamem_alloc(sc, &rxq->rxq_buf_mem, GE_RXBUF_NSEGS,
589 	    GE_RXBUF_MEMSIZE, 0);
590 	if (error) {
591 		GE_FUNC_EXIT(sc, "!!!");
592 		return error;
593 	}
594 	GE_FUNC_EXIT(sc, "");
595 	return error;
596 }
597 
598 int
599 gfe_rx_rxqinit(struct gfe_softc *sc, enum gfe_rxprio rxprio)
600 {
601 	struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
602 	volatile struct gt_eth_desc *rxd;
603 	const bus_dma_segment_t *ds;
604 	int idx;
605 	bus_addr_t nxtaddr;
606 	bus_size_t boff;
607 
608 	GE_FUNC_ENTER(sc, "gfe_rx_rxqinit");
609 	GE_DPRINTF(sc, ("(%d)", rxprio));
610 
611 	if ((sc->sc_flags & GE_NOFREE) == 0) {
612 		int error = gfe_rx_rxqalloc(sc, rxprio);
613 		if (error) {
614 			GE_FUNC_EXIT(sc, "!");
615 			return error;
616 		}
617 	} else {
618 		KASSERT(rxq->rxq_desc_mem.gdm_kva != NULL);
619 		KASSERT(rxq->rxq_buf_mem.gdm_kva != NULL);
620 	}
621 
622 	memset(rxq->rxq_desc_mem.gdm_kva, 0, GE_RXDESC_MEMSIZE);
623 
624 	rxq->rxq_descs =
625 	    (volatile struct gt_eth_desc *) rxq->rxq_desc_mem.gdm_kva;
626 	rxq->rxq_desc_busaddr = rxq->rxq_desc_mem.gdm_map->dm_segs[0].ds_addr;
627 	rxq->rxq_bufs = (struct gfe_rxbuf *) rxq->rxq_buf_mem.gdm_kva;
628 	rxq->rxq_fi = 0;
629 	rxq->rxq_active = GE_RXDESC_MAX;
630 	for (idx = 0, rxd = rxq->rxq_descs,
631 		boff = 0, ds = rxq->rxq_buf_mem.gdm_map->dm_segs,
632 		nxtaddr = rxq->rxq_desc_busaddr + sizeof(*rxd);
633 	     idx < GE_RXDESC_MAX;
634 	     idx++, rxd++, nxtaddr += sizeof(*rxd)) {
635 		rxd->ed_lencnt = htogt32(GE_RXBUF_SIZE << 16);
636 		rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
637 		rxd->ed_bufptr = htogt32(ds->ds_addr + boff);
638 		/*
639 		 * update the nxtptr to point to the next txd.
640 		 */
641 		if (idx == GE_RXDESC_MAX - 1)
642 			nxtaddr = rxq->rxq_desc_busaddr;
643 		rxd->ed_nxtptr = htogt32(nxtaddr);
644 		boff += GE_RXBUF_SIZE;
645 		if (boff == ds->ds_len) {
646 			ds++;
647 			boff = 0;
648 		}
649 	}
650 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map, 0,
651 			rxq->rxq_desc_mem.gdm_map->dm_mapsize,
652 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
653 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map, 0,
654 			rxq->rxq_buf_mem.gdm_map->dm_mapsize,
655 			BUS_DMASYNC_PREREAD);
656 
657 	rxq->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError;
658 	switch (rxprio) {
659 	case GE_RXPRIO_HI:
660 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
661 		rxq->rxq_efrdp = ETH_EFRDP3(sc->sc_macno);
662 		rxq->rxq_ecrdp = ETH_ECRDP3(sc->sc_macno);
663 		break;
664 	case GE_RXPRIO_MEDHI:
665 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_2|ETH_IR_RxError_2;
666 		rxq->rxq_efrdp = ETH_EFRDP2(sc->sc_macno);
667 		rxq->rxq_ecrdp = ETH_ECRDP2(sc->sc_macno);
668 		break;
669 	case GE_RXPRIO_MEDLO:
670 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_1|ETH_IR_RxError_1;
671 		rxq->rxq_efrdp = ETH_EFRDP1(sc->sc_macno);
672 		rxq->rxq_ecrdp = ETH_ECRDP1(sc->sc_macno);
673 		break;
674 	case GE_RXPRIO_LO:
675 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_0|ETH_IR_RxError_0;
676 		rxq->rxq_efrdp = ETH_EFRDP0(sc->sc_macno);
677 		rxq->rxq_ecrdp = ETH_ECRDP0(sc->sc_macno);
678 		break;
679 	}
680 	GE_FUNC_EXIT(sc, "");
681 	return 0;
682 }
683 
684 void
685 gfe_rx_get(struct gfe_softc *sc, enum gfe_rxprio rxprio)
686 {
687 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
688 	struct gfe_rxqueue * const rxq = &sc->sc_rxq[rxprio];
689 	struct mbuf *m = rxq->rxq_curpkt;
690 
691 	GE_FUNC_ENTER(sc, "gfe_rx_get");
692 	GE_DPRINTF(sc, ("(%d)", rxprio));
693 
694 	while (rxq->rxq_active > 0) {
695 		volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[rxq->rxq_fi];
696 		struct gfe_rxbuf *rxb = &rxq->rxq_bufs[rxq->rxq_fi];
697 		const struct ether_header *eh;
698 		unsigned int cmdsts;
699 		size_t buflen;
700 
701 		GE_RXDPOSTSYNC(sc, rxq, rxq->rxq_fi);
702 		cmdsts = gt32toh(rxd->ed_cmdsts);
703 		GE_DPRINTF(sc, (":%d=%#x", rxq->rxq_fi, cmdsts));
704 		rxq->rxq_cmdsts = cmdsts;
705 		/*
706 		 * Sometimes the GE "forgets" to reset the ownership bit.
707 		 * But if the length has been rewritten, the packet is ours
708 		 * so pretend the O bit is set.
709 		 */
710 		buflen = gt32toh(rxd->ed_lencnt) & 0xffff;
711 		if ((cmdsts & RX_CMD_O) && buflen == 0) {
712 			GE_RXDPRESYNC(sc, rxq, rxq->rxq_fi);
713 			break;
714 		}
715 
716 		/*
717 		 * If this is not a single buffer packet with no errors
718 		 * or for some reason it's bigger than our frame size,
719 		 * ignore it and go to the next packet.
720 		 */
721 		if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
722 			    (RX_CMD_F|RX_CMD_L) ||
723 		    buflen > sc->sc_max_frame_length) {
724 			GE_DPRINTF(sc, ("!"));
725 			--rxq->rxq_active;
726 			ifp->if_ipackets++;
727 			ifp->if_ierrors++;
728 			goto give_it_back;
729 		}
730 
731 		/* CRC is included with the packet; trim it off. */
732 		buflen -= ETHER_CRC_LEN;
733 
734 		if (m == NULL) {
735 			MGETHDR(m, M_DONTWAIT, MT_DATA);
736 			if (m == NULL) {
737 				GE_DPRINTF(sc, ("?"));
738 				break;
739 			}
740 		}
741 		if ((m->m_flags & M_EXT) == 0 && buflen > MHLEN - 2) {
742 			MCLGET(m, M_DONTWAIT);
743 			if ((m->m_flags & M_EXT) == 0) {
744 				GE_DPRINTF(sc, ("?"));
745 				break;
746 			}
747 		}
748 		m->m_data += 2;
749 		m->m_len = 0;
750 		m->m_pkthdr.len = 0;
751 		m->m_pkthdr.rcvif = ifp;
752 		rxq->rxq_cmdsts = cmdsts;
753 		--rxq->rxq_active;
754 
755 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map,
756 		    rxq->rxq_fi * sizeof(*rxb), buflen, BUS_DMASYNC_POSTREAD);
757 
758 		KASSERT(m->m_len == 0 && m->m_pkthdr.len == 0);
759 		memcpy(m->m_data + m->m_len, rxb->rxb_data, buflen);
760 		m->m_len = buflen;
761 		m->m_pkthdr.len = buflen;
762 
763 		ifp->if_ipackets++;
764 #if NBPFILTER > 0
765 		if (ifp->if_bpf != NULL)
766 			bpf_mtap(ifp->if_bpf, m);
767 #endif
768 
769 		eh = (const struct ether_header *) m->m_data;
770 		if ((ifp->if_flags & IFF_PROMISC) ||
771 		    (rxq->rxq_cmdsts & RX_STS_M) == 0 ||
772 		    (rxq->rxq_cmdsts & RX_STS_HE) ||
773 		    (eh->ether_dhost[0] & 1) != 0 ||
774 		    memcmp(eh->ether_dhost, CLLADDR(ifp->if_sadl),
775 			ETHER_ADDR_LEN) == 0) {
776 			(*ifp->if_input)(ifp, m);
777 			m = NULL;
778 			GE_DPRINTF(sc, (">"));
779 		} else {
780 			m->m_len = 0;
781 			m->m_pkthdr.len = 0;
782 			GE_DPRINTF(sc, ("+"));
783 		}
784 		rxq->rxq_cmdsts = 0;
785 
786 	   give_it_back:
787 		rxd->ed_lencnt &= ~0xffff;	/* zero out length */
788 		rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
789 #if 0
790 		GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)",
791 		    rxq->rxq_fi,
792 		    ((unsigned long *)rxd)[0], ((unsigned long *)rxd)[1],
793 		    ((unsigned long *)rxd)[2], ((unsigned long *)rxd)[3]));
794 #endif
795 		GE_RXDPRESYNC(sc, rxq, rxq->rxq_fi);
796 		if (++rxq->rxq_fi == GE_RXDESC_MAX)
797 			rxq->rxq_fi = 0;
798 		rxq->rxq_active++;
799 	}
800 	rxq->rxq_curpkt = m;
801 	GE_FUNC_EXIT(sc, "");
802 }
803 
804 uint32_t
805 gfe_rx_process(struct gfe_softc *sc, uint32_t cause, uint32_t intrmask)
806 {
807 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
808 	struct gfe_rxqueue *rxq;
809 	uint32_t rxbits;
810 #define	RXPRIO_DECODER	0xffffaa50
811 	GE_FUNC_ENTER(sc, "gfe_rx_process");
812 
813 	rxbits = ETH_IR_RxBuffer_GET(cause);
814 	while (rxbits) {
815 		enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
816 		GE_DPRINTF(sc, ("%1x", rxbits));
817 		rxbits &= ~(1 << rxprio);
818 		gfe_rx_get(sc, rxprio);
819 	}
820 
821 	rxbits = ETH_IR_RxError_GET(cause);
822 	while (rxbits) {
823 		enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
824 		uint32_t masks[(GE_RXDESC_MAX + 31) / 32];
825 		int idx;
826 		rxbits &= ~(1 << rxprio);
827 		rxq = &sc->sc_rxq[rxprio];
828 		sc->sc_idlemask |= (rxq->rxq_intrbits & ETH_IR_RxBits);
829 		intrmask &= ~(rxq->rxq_intrbits & ETH_IR_RxBits);
830 		if ((sc->sc_tickflags & GE_TICK_RX_RESTART) == 0) {
831 			sc->sc_tickflags |= GE_TICK_RX_RESTART;
832 			callout_reset(&sc->sc_co, 1, gfe_tick, sc);
833 		}
834 		ifp->if_ierrors++;
835 		GE_DPRINTF(sc, ("%s: rx queue %d filled at %u\n",
836 		    device_xname(&sc->sc_dev), rxprio, rxq->rxq_fi));
837 		memset(masks, 0, sizeof(masks));
838 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
839 		    0, rxq->rxq_desc_mem.gdm_size,
840 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
841 		for (idx = 0; idx < GE_RXDESC_MAX; idx++) {
842 			volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[idx];
843 
844 			if (RX_CMD_O & gt32toh(rxd->ed_cmdsts))
845 				masks[idx/32] |= 1 << (idx & 31);
846 		}
847 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
848 		    0, rxq->rxq_desc_mem.gdm_size,
849 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
850 #if defined(DEBUG)
851 		printf("%s: rx queue %d filled at %u=%#x(%#x/%#x)\n",
852 		    device_xname(&sc->sc_dev), rxprio, rxq->rxq_fi,
853 		    rxq->rxq_cmdsts, masks[0], masks[1]);
854 #endif
855 	}
856 	if ((intrmask & ETH_IR_RxBits) == 0)
857 		intrmask &= ~(ETH_IR_RxBuffer|ETH_IR_RxError);
858 
859 	GE_FUNC_EXIT(sc, "");
860 	return intrmask;
861 }
862 
863 int
864 gfe_rx_prime(struct gfe_softc *sc)
865 {
866 	struct gfe_rxqueue *rxq;
867 	int error;
868 
869 	GE_FUNC_ENTER(sc, "gfe_rx_prime");
870 
871 	error = gfe_rx_rxqinit(sc, GE_RXPRIO_HI);
872 	if (error)
873 		goto bail;
874 	rxq = &sc->sc_rxq[GE_RXPRIO_HI];
875 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
876 		GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
877 		GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
878 	}
879 	sc->sc_intrmask |= rxq->rxq_intrbits;
880 
881 	error = gfe_rx_rxqinit(sc, GE_RXPRIO_MEDHI);
882 	if (error)
883 		goto bail;
884 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
885 		rxq = &sc->sc_rxq[GE_RXPRIO_MEDHI];
886 		GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
887 		GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
888 		sc->sc_intrmask |= rxq->rxq_intrbits;
889 	}
890 
891 	error = gfe_rx_rxqinit(sc, GE_RXPRIO_MEDLO);
892 	if (error)
893 		goto bail;
894 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
895 		rxq = &sc->sc_rxq[GE_RXPRIO_MEDLO];
896 		GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
897 		GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
898 		sc->sc_intrmask |= rxq->rxq_intrbits;
899 	}
900 
901 	error = gfe_rx_rxqinit(sc, GE_RXPRIO_LO);
902 	if (error)
903 		goto bail;
904 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
905 		rxq = &sc->sc_rxq[GE_RXPRIO_LO];
906 		GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
907 		GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
908 		sc->sc_intrmask |= rxq->rxq_intrbits;
909 	}
910 
911   bail:
912 	GE_FUNC_EXIT(sc, "");
913 	return error;
914 }
915 
916 void
917 gfe_rx_cleanup(struct gfe_softc *sc, enum gfe_rxprio rxprio)
918 {
919 	struct gfe_rxqueue *rxq = &sc->sc_rxq[rxprio];
920 	GE_FUNC_ENTER(sc, "gfe_rx_cleanup");
921 	if (rxq == NULL) {
922 		GE_FUNC_EXIT(sc, "");
923 		return;
924 	}
925 
926 	if (rxq->rxq_curpkt)
927 		m_freem(rxq->rxq_curpkt);
928 	if ((sc->sc_flags & GE_NOFREE) == 0) {
929 		gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
930 		gfe_dmamem_free(sc, &rxq->rxq_buf_mem);
931 	}
932 	GE_FUNC_EXIT(sc, "");
933 }
934 
935 void
936 gfe_rx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
937 {
938 	GE_FUNC_ENTER(sc, "gfe_rx_stop");
939 	sc->sc_flags &= ~GE_RXACTIVE;
940 	sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
941 	sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
942 	GE_WRITE(sc, EIMR, sc->sc_intrmask);
943 	GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR);
944 	do {
945 		delay(10);
946 	} while (GE_READ(sc, ESDCMR) & ETH_ESDCMR_AR);
947 	gfe_rx_cleanup(sc, GE_RXPRIO_HI);
948 	gfe_rx_cleanup(sc, GE_RXPRIO_MEDHI);
949 	gfe_rx_cleanup(sc, GE_RXPRIO_MEDLO);
950 	gfe_rx_cleanup(sc, GE_RXPRIO_LO);
951 	GE_FUNC_EXIT(sc, "");
952 }
953 
954 void
955 gfe_tick(void *arg)
956 {
957 	struct gfe_softc * const sc = arg;
958 	uint32_t intrmask;
959 	unsigned int tickflags;
960 	int s;
961 
962 	GE_FUNC_ENTER(sc, "gfe_tick");
963 
964 	s = splnet();
965 
966 	tickflags = sc->sc_tickflags;
967 	sc->sc_tickflags = 0;
968 	intrmask = sc->sc_intrmask;
969 	if (tickflags & GE_TICK_TX_IFSTART)
970 		gfe_ifstart(&sc->sc_ec.ec_if);
971 	if (tickflags & GE_TICK_RX_RESTART) {
972 		intrmask |= sc->sc_idlemask;
973 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_3|ETH_IR_RxError_3)) {
974 			struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_HI];
975 			rxq->rxq_fi = 0;
976 			GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
977 			GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
978 		}
979 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_2|ETH_IR_RxError_2)) {
980 			struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_MEDHI];
981 			rxq->rxq_fi = 0;
982 			GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
983 			GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
984 		}
985 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_1|ETH_IR_RxError_1)) {
986 			struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_MEDLO];
987 			rxq->rxq_fi = 0;
988 			GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
989 			GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
990 		}
991 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_0|ETH_IR_RxError_0)) {
992 			struct gfe_rxqueue *rxq = &sc->sc_rxq[GE_RXPRIO_LO];
993 			rxq->rxq_fi = 0;
994 			GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
995 			GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
996 		}
997 		sc->sc_idlemask = 0;
998 	}
999 	if (intrmask != sc->sc_intrmask) {
1000 		sc->sc_intrmask = intrmask;
1001 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
1002 	}
1003 	gfe_intr(sc);
1004 	splx(s);
1005 
1006 	GE_FUNC_EXIT(sc, "");
1007 }
1008 
1009 int
1010 gfe_tx_enqueue(struct gfe_softc *sc, enum gfe_txprio txprio)
1011 {
1012 	const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
1013 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
1014 	struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1015 	volatile struct gt_eth_desc * const txd = &txq->txq_descs[txq->txq_lo];
1016 	uint32_t intrmask = sc->sc_intrmask;
1017 	size_t buflen;
1018 	struct mbuf *m;
1019 
1020 	GE_FUNC_ENTER(sc, "gfe_tx_enqueue");
1021 
1022 	/*
1023 	 * Anything in the pending queue to enqueue?  if not, punt. Likewise
1024 	 * if the txq is not yet created.
1025 	 * otherwise grab its dmamap.
1026 	 */
1027 	if (txq == NULL || (m = txq->txq_pendq.ifq_head) == NULL) {
1028 		GE_FUNC_EXIT(sc, "-");
1029 		return 0;
1030 	}
1031 
1032 	/*
1033 	 * Have we [over]consumed our limit of descriptors?
1034 	 * Do we have enough free descriptors?
1035 	 */
1036 	if (GE_TXDESC_MAX == txq->txq_nactive + 2) {
1037 		volatile struct gt_eth_desc * const txd2 = &txq->txq_descs[txq->txq_fi];
1038 		uint32_t cmdsts;
1039 		size_t pktlen;
1040 		GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
1041 		cmdsts = gt32toh(txd2->ed_cmdsts);
1042 		if (cmdsts & TX_CMD_O) {
1043 			int nextin;
1044 			/*
1045 			 * Sometime the Discovery forgets to update the
1046 			 * last descriptor.  See if we own the descriptor
1047 			 * after it (since we know we've turned that to
1048 			 * the discovery and if we owned it, the Discovery
1049 			 * gave it back).  If we do, we know the Discovery
1050 			 * gave back this one but forgot to mark it as ours.
1051 			 */
1052 			nextin = txq->txq_fi + 1;
1053 			if (nextin == GE_TXDESC_MAX)
1054 				nextin = 0;
1055 			GE_TXDPOSTSYNC(sc, txq, nextin);
1056 			if (gt32toh(txq->txq_descs[nextin].ed_cmdsts) & TX_CMD_O) {
1057 				GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1058 				GE_TXDPRESYNC(sc, txq, nextin);
1059 				GE_FUNC_EXIT(sc, "@");
1060 				return 0;
1061 			}
1062 #ifdef DEBUG
1063 			printf("%s: txenqueue: transmitter resynced at %d\n",
1064 			    device_xname(&sc->sc_dev), txq->txq_fi);
1065 #endif
1066 		}
1067 		if (++txq->txq_fi == GE_TXDESC_MAX)
1068 			txq->txq_fi = 0;
1069 		txq->txq_inptr = gt32toh(txd2->ed_bufptr) - txq->txq_buf_busaddr;
1070 		pktlen = (gt32toh(txd2->ed_lencnt) >> 16) & 0xffff;
1071 		txq->txq_inptr += roundup(pktlen, dcache_line_size);
1072 		txq->txq_nactive--;
1073 
1074 		/* statistics */
1075 		ifp->if_opackets++;
1076 		if (cmdsts & TX_STS_ES)
1077 			ifp->if_oerrors++;
1078 		GE_DPRINTF(sc, ("%%"));
1079 	}
1080 
1081 	buflen = roundup(m->m_pkthdr.len, dcache_line_size);
1082 
1083 	/*
1084 	 * If this packet would wrap around the end of the buffer, reset back
1085 	 * to the beginning.
1086 	 */
1087 	if (txq->txq_outptr + buflen > GE_TXBUF_SIZE) {
1088 		txq->txq_ei_gapcount += GE_TXBUF_SIZE - txq->txq_outptr;
1089 		txq->txq_outptr = 0;
1090 	}
1091 
1092 	/*
1093 	 * Make sure the output packet doesn't run over the beginning of
1094 	 * what we've already given the GT.
1095 	 */
1096 	if (txq->txq_nactive > 0 && txq->txq_outptr <= txq->txq_inptr &&
1097 	    txq->txq_outptr + buflen > txq->txq_inptr) {
1098 		intrmask |= txq->txq_intrbits &
1099 		    (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow);
1100 		if (sc->sc_intrmask != intrmask) {
1101 			sc->sc_intrmask = intrmask;
1102 			GE_WRITE(sc, EIMR, sc->sc_intrmask);
1103 		}
1104 		GE_FUNC_EXIT(sc, "#");
1105 		return 0;
1106 	}
1107 
1108 	/*
1109 	 * The end-of-list descriptor we put on last time is the starting point
1110 	 * for this packet.  The GT is supposed to terminate list processing on
1111 	 * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
1112 	 * must terminate the list.
1113 	 */
1114 	intrmask = sc->sc_intrmask;
1115 
1116 	m_copydata(m, 0, m->m_pkthdr.len,
1117 	    (char *)txq->txq_buf_mem.gdm_kva + (int)txq->txq_outptr);
1118 	bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1119 	    txq->txq_outptr, buflen, BUS_DMASYNC_PREWRITE);
1120 	txd->ed_bufptr = htogt32(txq->txq_buf_busaddr + txq->txq_outptr);
1121 	txd->ed_lencnt = htogt32(m->m_pkthdr.len << 16);
1122 	GE_TXDPRESYNC(sc, txq, txq->txq_lo);
1123 
1124 	/*
1125 	 * Request a buffer interrupt every 2/3 of the way thru the transmit
1126 	 * buffer.
1127 	 */
1128 	txq->txq_ei_gapcount += buflen;
1129 	if (txq->txq_ei_gapcount > 2 * GE_TXBUF_SIZE / 3) {
1130 		txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST|TX_CMD_EI);
1131 		txq->txq_ei_gapcount = 0;
1132 	} else {
1133 		txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST);
1134 	}
1135 #if 0
1136 	GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)", txq->txq_lo,
1137 	    ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1138 	    ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1139 #endif
1140 	GE_TXDPRESYNC(sc, txq, txq->txq_lo);
1141 
1142 	txq->txq_outptr += buflen;
1143 	/*
1144 	 * Tell the SDMA engine to "Fetch!"
1145 	 */
1146 	GE_WRITE(sc, ESDCMR,
1147 		 txq->txq_esdcmrbits & (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
1148 
1149 	GE_DPRINTF(sc, ("(%d)", txq->txq_lo));
1150 
1151 	/*
1152 	 * Update the last out appropriately.
1153 	 */
1154 	txq->txq_nactive++;
1155 	if (++txq->txq_lo == GE_TXDESC_MAX)
1156 		txq->txq_lo = 0;
1157 
1158 	/*
1159 	 * Move mbuf from the pending queue to the snd queue.
1160 	 */
1161 	IF_DEQUEUE(&txq->txq_pendq, m);
1162 #if NBPFILTER > 0
1163 	if (ifp->if_bpf != NULL)
1164 		bpf_mtap(ifp->if_bpf, m);
1165 #endif
1166 	m_freem(m);
1167 	ifp->if_flags &= ~IFF_OACTIVE;
1168 
1169 	/*
1170 	 * Since we have put an item into the packet queue, we now want
1171 	 * an interrupt when the transmit queue finishes processing the
1172 	 * list.  But only update the mask if needs changing.
1173 	 */
1174 	intrmask |= txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow);
1175 	if (sc->sc_intrmask != intrmask) {
1176 		sc->sc_intrmask = intrmask;
1177 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
1178 	}
1179 	if (ifp->if_timer == 0)
1180 		ifp->if_timer = 5;
1181 	GE_FUNC_EXIT(sc, "*");
1182 	return 1;
1183 }
1184 
1185 uint32_t
1186 gfe_tx_done(struct gfe_softc *sc, enum gfe_txprio txprio, uint32_t intrmask)
1187 {
1188 	struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1189 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
1190 
1191 	GE_FUNC_ENTER(sc, "gfe_tx_done");
1192 
1193 	if (txq == NULL) {
1194 		GE_FUNC_EXIT(sc, "");
1195 		return intrmask;
1196 	}
1197 
1198 	while (txq->txq_nactive > 0) {
1199 		const int dcache_line_size = curcpu()->ci_ci.dcache_line_size;
1200 		volatile struct gt_eth_desc *txd = &txq->txq_descs[txq->txq_fi];
1201 		uint32_t cmdsts;
1202 		size_t pktlen;
1203 
1204 		GE_TXDPOSTSYNC(sc, txq, txq->txq_fi);
1205 		if ((cmdsts = gt32toh(txd->ed_cmdsts)) & TX_CMD_O) {
1206 			int nextin;
1207 
1208 			if (txq->txq_nactive == 1) {
1209 				GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1210 				GE_FUNC_EXIT(sc, "");
1211 				return intrmask;
1212 			}
1213 			/*
1214 			 * Sometimes the Discovery forgets to update the
1215 			 * ownership bit in the descriptor.  See if we own the
1216 			 * descriptor after it (since we know we've turned
1217 			 * that to the Discovery and if we own it now then the
1218 			 * Discovery gave it back).  If we do, we know the
1219 			 * Discovery gave back this one but forgot to mark it
1220 			 * as ours.
1221 			 */
1222 			nextin = txq->txq_fi + 1;
1223 			if (nextin == GE_TXDESC_MAX)
1224 				nextin = 0;
1225 			GE_TXDPOSTSYNC(sc, txq, nextin);
1226 			if (gt32toh(txq->txq_descs[nextin].ed_cmdsts) & TX_CMD_O) {
1227 				GE_TXDPRESYNC(sc, txq, txq->txq_fi);
1228 				GE_TXDPRESYNC(sc, txq, nextin);
1229 				GE_FUNC_EXIT(sc, "");
1230 				return intrmask;
1231 			}
1232 #ifdef DEBUG
1233 			printf("%s: txdone: transmitter resynced at %d\n",
1234 			    device_xname(&sc->sc_dev), txq->txq_fi);
1235 #endif
1236 		}
1237 #if 0
1238 		GE_DPRINTF(sc, ("([%d]<-%08lx.%08lx.%08lx.%08lx)",
1239 		    txq->txq_lo,
1240 		    ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
1241 		    ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
1242 #endif
1243 		GE_DPRINTF(sc, ("(%d)", txq->txq_fi));
1244 		if (++txq->txq_fi == GE_TXDESC_MAX)
1245 			txq->txq_fi = 0;
1246 		txq->txq_inptr = gt32toh(txd->ed_bufptr) - txq->txq_buf_busaddr;
1247 		pktlen = (gt32toh(txd->ed_lencnt) >> 16) & 0xffff;
1248 		bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
1249 		    txq->txq_inptr, pktlen, BUS_DMASYNC_POSTWRITE);
1250 		txq->txq_inptr += roundup(pktlen, dcache_line_size);
1251 
1252 		/* statistics */
1253 		ifp->if_opackets++;
1254 		if (cmdsts & TX_STS_ES)
1255 			ifp->if_oerrors++;
1256 
1257 		/* txd->ed_bufptr = 0; */
1258 
1259 		ifp->if_timer = 5;
1260 		--txq->txq_nactive;
1261 	}
1262 	if (txq->txq_nactive != 0)
1263 		panic("%s: transmit fifo%d empty but active count (%d) > 0!",
1264 		    device_xname(&sc->sc_dev), txprio, txq->txq_nactive);
1265 	ifp->if_timer = 0;
1266 	intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow));
1267 	intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow));
1268 	GE_FUNC_EXIT(sc, "");
1269 	return intrmask;
1270 }
1271 
1272 int
1273 gfe_tx_txqalloc(struct gfe_softc *sc, enum gfe_txprio txprio)
1274 {
1275 	struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1276 	int error;
1277 
1278 	GE_FUNC_ENTER(sc, "gfe_tx_txqalloc");
1279 
1280 	error = gfe_dmamem_alloc(sc, &txq->txq_desc_mem, 1,
1281 	    GE_TXDESC_MEMSIZE, BUS_DMA_NOCACHE);
1282 	if (error) {
1283 		GE_FUNC_EXIT(sc, "");
1284 		return error;
1285 	}
1286 	error = gfe_dmamem_alloc(sc, &txq->txq_buf_mem, 1, GE_TXBUF_SIZE, 0);
1287 	if (error) {
1288 		gfe_dmamem_free(sc, &txq->txq_desc_mem);
1289 		GE_FUNC_EXIT(sc, "");
1290 		return error;
1291 	}
1292 	GE_FUNC_EXIT(sc, "");
1293 	return 0;
1294 }
1295 
1296 int
1297 gfe_tx_start(struct gfe_softc *sc, enum gfe_txprio txprio)
1298 {
1299 	struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1300 	volatile struct gt_eth_desc *txd;
1301 	unsigned int i;
1302 	bus_addr_t addr;
1303 
1304 	GE_FUNC_ENTER(sc, "gfe_tx_start");
1305 
1306 	sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1307 			     ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1308 
1309 	if (sc->sc_flags & GE_NOFREE) {
1310 		KASSERT(txq->txq_desc_mem.gdm_kva != NULL);
1311 		KASSERT(txq->txq_buf_mem.gdm_kva != NULL);
1312 	} else {
1313 		int error = gfe_tx_txqalloc(sc, txprio);
1314 		if (error) {
1315 			GE_FUNC_EXIT(sc, "!");
1316 			return error;
1317 		}
1318 	}
1319 
1320 	txq->txq_descs =
1321 	    (volatile struct gt_eth_desc *) txq->txq_desc_mem.gdm_kva;
1322 	txq->txq_desc_busaddr = txq->txq_desc_mem.gdm_map->dm_segs[0].ds_addr;
1323 	txq->txq_buf_busaddr = txq->txq_buf_mem.gdm_map->dm_segs[0].ds_addr;
1324 
1325 	txq->txq_pendq.ifq_maxlen = 10;
1326 	txq->txq_ei_gapcount = 0;
1327 	txq->txq_nactive = 0;
1328 	txq->txq_fi = 0;
1329 	txq->txq_lo = 0;
1330 	txq->txq_inptr = GE_TXBUF_SIZE;
1331 	txq->txq_outptr = 0;
1332 	for (i = 0, txd = txq->txq_descs,
1333 	     addr = txq->txq_desc_busaddr + sizeof(*txd);
1334 			i < GE_TXDESC_MAX - 1;
1335 			i++, txd++, addr += sizeof(*txd)) {
1336 		/*
1337 		 * update the nxtptr to point to the next txd.
1338 		 */
1339 		txd->ed_cmdsts = 0;
1340 		txd->ed_nxtptr = htogt32(addr);
1341 	}
1342 	txq->txq_descs[GE_TXDESC_MAX-1].ed_nxtptr =
1343 	    htogt32(txq->txq_desc_busaddr);
1344 	bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map, 0,
1345 	    GE_TXDESC_MEMSIZE, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1346 
1347 	switch (txprio) {
1348 	case GE_TXPRIO_HI:
1349 		txq->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
1350 		txq->txq_esdcmrbits = ETH_ESDCMR_TXDH;
1351 		txq->txq_epsrbits = ETH_EPSR_TxHigh;
1352 		txq->txq_ectdp = ETH_ECTDP1(sc->sc_macno);
1353 		GE_WRITE(sc, ECTDP1, txq->txq_desc_busaddr);
1354 		break;
1355 
1356 	case GE_TXPRIO_LO:
1357 		txq->txq_intrbits = ETH_IR_TxEndLow|ETH_IR_TxBufferLow;
1358 		txq->txq_esdcmrbits = ETH_ESDCMR_TXDL;
1359 		txq->txq_epsrbits = ETH_EPSR_TxLow;
1360 		txq->txq_ectdp = ETH_ECTDP0(sc->sc_macno);
1361 		GE_WRITE(sc, ECTDP0, txq->txq_desc_busaddr);
1362 		break;
1363 
1364 	case GE_TXPRIO_NONE:
1365 		break;
1366 	}
1367 #if 0
1368 	GE_DPRINTF(sc, ("(ectdp=%#x", txq->txq_ectdp));
1369 	gt_write(device_parent(&sc->sc_dev), txq->txq_ectdp,
1370 	    txq->txq_desc_busaddr);
1371 	GE_DPRINTF(sc, (")"));
1372 #endif
1373 
1374 	/*
1375 	 * If we are restarting, there may be packets in the pending queue
1376 	 * waiting to be enqueued.  Try enqueuing packets from both priority
1377 	 * queues until the pending queue is empty or there no room for them
1378 	 * on the device.
1379 	 */
1380 	while (gfe_tx_enqueue(sc, txprio))
1381 		continue;
1382 
1383 	GE_FUNC_EXIT(sc, "");
1384 	return 0;
1385 }
1386 
1387 void
1388 gfe_tx_cleanup(struct gfe_softc *sc, enum gfe_txprio txprio, int flush)
1389 {
1390 	struct gfe_txqueue * const txq = &sc->sc_txq[txprio];
1391 
1392 	GE_FUNC_ENTER(sc, "gfe_tx_cleanup");
1393 	if (txq == NULL) {
1394 		GE_FUNC_EXIT(sc, "");
1395 		return;
1396 	}
1397 
1398 	if (!flush) {
1399 		GE_FUNC_EXIT(sc, "");
1400 		return;
1401 	}
1402 
1403 	if ((sc->sc_flags & GE_NOFREE) == 0) {
1404 		gfe_dmamem_free(sc, &txq->txq_desc_mem);
1405 		gfe_dmamem_free(sc, &txq->txq_buf_mem);
1406 	}
1407 	GE_FUNC_EXIT(sc, "-F");
1408 }
1409 
1410 void
1411 gfe_tx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
1412 {
1413 	GE_FUNC_ENTER(sc, "gfe_tx_stop");
1414 
1415 	GE_WRITE(sc, ESDCMR, ETH_ESDCMR_STDH|ETH_ESDCMR_STDL);
1416 
1417 	sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, sc->sc_intrmask);
1418 	sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, sc->sc_intrmask);
1419 	sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
1420 			     ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
1421 
1422 	gfe_tx_cleanup(sc, GE_TXPRIO_HI, op == GE_WHACK_STOP);
1423 	gfe_tx_cleanup(sc, GE_TXPRIO_LO, op == GE_WHACK_STOP);
1424 
1425 	sc->sc_ec.ec_if.if_timer = 0;
1426 	GE_FUNC_EXIT(sc, "");
1427 }
1428 
1429 int
1430 gfe_intr(void *arg)
1431 {
1432 	struct gfe_softc * const sc = arg;
1433 	uint32_t cause;
1434 	uint32_t intrmask = sc->sc_intrmask;
1435 	int claim = 0;
1436 	int cnt;
1437 
1438 	GE_FUNC_ENTER(sc, "gfe_intr");
1439 
1440 	for (cnt = 0; cnt < 4; cnt++) {
1441 		if (sc->sc_intrmask != intrmask) {
1442 			sc->sc_intrmask = intrmask;
1443 			GE_WRITE(sc, EIMR, sc->sc_intrmask);
1444 		}
1445 		cause = GE_READ(sc, EICR);
1446 		cause &= sc->sc_intrmask;
1447 		GE_DPRINTF(sc, (".%#x", cause));
1448 		if (cause == 0)
1449 			break;
1450 
1451 		claim = 1;
1452 
1453 		GE_WRITE(sc, EICR, ~cause);
1454 #ifndef GE_NORX
1455 		if (cause & (ETH_IR_RxBuffer|ETH_IR_RxError))
1456 			intrmask = gfe_rx_process(sc, cause, intrmask);
1457 #endif
1458 
1459 #ifndef GE_NOTX
1460 		if (cause & (ETH_IR_TxBufferHigh|ETH_IR_TxEndHigh))
1461 			intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, intrmask);
1462 		if (cause & (ETH_IR_TxBufferLow|ETH_IR_TxEndLow))
1463 			intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, intrmask);
1464 #endif
1465 		if (cause & ETH_IR_MIIPhySTC) {
1466 			sc->sc_flags |= GE_PHYSTSCHG;
1467 			/* intrmask &= ~ETH_IR_MIIPhySTC; */
1468 		}
1469 	}
1470 
1471 	while (gfe_tx_enqueue(sc, GE_TXPRIO_HI))
1472 		continue;
1473 	while (gfe_tx_enqueue(sc, GE_TXPRIO_LO))
1474 		continue;
1475 
1476 	GE_FUNC_EXIT(sc, "");
1477 	return claim;
1478 }
1479 
1480 int
1481 gfe_mii_read (struct device *self, int phy, int reg)
1482 {
1483 	return gt_mii_read(self, device_parent(self), phy, reg);
1484 }
1485 
1486 void
1487 gfe_mii_write (struct device *self, int phy, int reg, int value)
1488 {
1489 	gt_mii_write(self, device_parent(self), phy, reg, value);
1490 }
1491 
1492 void
1493 gfe_mii_statchg (struct device *self)
1494 {
1495 	/* struct gfe_softc *sc = device_private(self); */
1496 	/* do nothing? */
1497 }
1498 
1499 int
1500 gfe_whack(struct gfe_softc *sc, enum gfe_whack_op op)
1501 {
1502 	int error = 0;
1503 	GE_FUNC_ENTER(sc, "gfe_whack");
1504 
1505 	switch (op) {
1506 	case GE_WHACK_RESTART:
1507 #ifndef GE_NOTX
1508 		gfe_tx_stop(sc, op);
1509 #endif
1510 		/* sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING; */
1511 		/* FALLTHROUGH */
1512 	case GE_WHACK_START:
1513 #ifndef GE_NOHASH
1514 		if (error == 0 && sc->sc_hashtable == NULL) {
1515 			error = gfe_hash_alloc(sc);
1516 			if (error)
1517 				break;
1518 		}
1519 		if (op != GE_WHACK_RESTART)
1520 			gfe_hash_fill(sc);
1521 #endif
1522 #ifndef GE_NORX
1523 		if (op != GE_WHACK_RESTART) {
1524 			error = gfe_rx_prime(sc);
1525 			if (error)
1526 				break;
1527 		}
1528 #endif
1529 #ifndef GE_NOTX
1530 		error = gfe_tx_start(sc, GE_TXPRIO_HI);
1531 		if (error)
1532 			break;
1533 #endif
1534 		sc->sc_ec.ec_if.if_flags |= IFF_RUNNING;
1535 		GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
1536 		GE_WRITE(sc, EPCXR, sc->sc_pcxr);
1537 		GE_WRITE(sc, EICR, 0);
1538 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
1539 #ifndef GE_NOHASH
1540 		GE_WRITE(sc, EHTPR, sc->sc_hash_mem.gdm_map->dm_segs->ds_addr);
1541 #endif
1542 #ifndef GE_NORX
1543 		GE_WRITE(sc, ESDCMR, ETH_ESDCMR_ERD);
1544 		sc->sc_flags |= GE_RXACTIVE;
1545 #endif
1546 		/* FALLTHROUGH */
1547 	case GE_WHACK_CHANGE:
1548 		GE_DPRINTF(sc, ("(pcr=%#x,imr=%#x)",
1549 		    GE_READ(sc, EPCR), GE_READ(sc, EIMR)));
1550 		GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
1551 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
1552 		gfe_ifstart(&sc->sc_ec.ec_if);
1553 		GE_DPRINTF(sc, ("(ectdp0=%#x, ectdp1=%#x)",
1554 		    GE_READ(sc, ECTDP0), GE_READ(sc, ECTDP1)));
1555 		GE_FUNC_EXIT(sc, "");
1556 		return error;
1557 	case GE_WHACK_STOP:
1558 		break;
1559 	}
1560 
1561 #ifdef GE_DEBUG
1562 	if (error)
1563 		GE_DPRINTF(sc, (" failed: %d\n", error));
1564 #endif
1565 	GE_WRITE(sc, EPCR, sc->sc_pcr);
1566 	GE_WRITE(sc, EIMR, 0);
1567 	sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING;
1568 #ifndef GE_NOTX
1569 	gfe_tx_stop(sc, GE_WHACK_STOP);
1570 #endif
1571 #ifndef GE_NORX
1572 	gfe_rx_stop(sc, GE_WHACK_STOP);
1573 #endif
1574 #ifndef GE_NOHASH
1575 	if ((sc->sc_flags & GE_NOFREE) == 0) {
1576 		gfe_dmamem_free(sc, &sc->sc_hash_mem);
1577 		sc->sc_hashtable = NULL;
1578 	}
1579 #endif
1580 
1581 	GE_FUNC_EXIT(sc, "");
1582 	return error;
1583 }
1584 
1585 int
1586 gfe_hash_compute(struct gfe_softc *sc, const uint8_t eaddr[ETHER_ADDR_LEN])
1587 {
1588 	uint32_t w0, add0, add1;
1589 	uint32_t result;
1590 
1591 	GE_FUNC_ENTER(sc, "gfe_hash_compute");
1592 	add0 = ((uint32_t) eaddr[5] <<  0) |
1593 	       ((uint32_t) eaddr[4] <<  8) |
1594 	       ((uint32_t) eaddr[3] << 16);
1595 
1596 	add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
1597 	add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
1598 	add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
1599 
1600 	add1 = ((uint32_t) eaddr[2] <<  0) |
1601 	       ((uint32_t) eaddr[1] <<  8) |
1602 	       ((uint32_t) eaddr[0] << 16);
1603 
1604 	add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
1605 	add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
1606 	add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
1607 
1608 	GE_DPRINTF(sc, ("%s=", ether_sprintf(eaddr)));
1609 	/*
1610 	 * hashResult is the 15 bits Hash entry address.
1611 	 * ethernetADD is a 48 bit number, which is derived from the Ethernet
1612 	 *	MAC address, by nibble swapping in every byte (i.e MAC address
1613 	 *	of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
1614 	 */
1615 
1616 	if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
1617 		/*
1618 		 * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
1619 		 *
1620 		 * hashFunc0 calculates the hashResult in the following manner:
1621 		 *   hashResult[ 8:0] = ethernetADD[14:8,1,0]
1622 		 *		XOR ethernetADD[23:15] XOR ethernetADD[32:24]
1623 		 */
1624 		result = (add0 & 3) | ((add0 >> 6) & ~3);
1625 		result ^= (add0 >> 15) ^ (add1 >>  0);
1626 		result &= 0x1ff;
1627 		/*
1628 		 *   hashResult[14:9] = ethernetADD[7:2]
1629 		 */
1630 		result |= (add0 & ~3) << 7;	/* excess bits will be masked */
1631 		GE_DPRINTF(sc, ("0(%#x)", result & 0x7fff));
1632 	} else {
1633 #define	TRIBITFLIP	073516240	/* yes its in octal */
1634 		/*
1635 		 * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
1636 		 *
1637 		 * hashFunc1 calculates the hashResult in the following manner:
1638 		 *   hashResult[08:00] = ethernetADD[06:14]
1639 		 *		XOR ethernetADD[15:23] XOR ethernetADD[24:32]
1640 		 */
1641 		w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
1642 		/*
1643 		 * Now bitswap those 9 bits
1644 		 */
1645 		result = 0;
1646 		result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
1647 		result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
1648 		result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
1649 
1650 		/*
1651 		 *   hashResult[14:09] = ethernetADD[00:05]
1652 		 */
1653 		result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
1654 		result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
1655 		GE_DPRINTF(sc, ("1(%#x)", result));
1656 	}
1657 	GE_FUNC_EXIT(sc, "");
1658 	return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
1659 }
1660 
1661 int
1662 gfe_hash_entry_op(struct gfe_softc *sc, enum gfe_hash_op op,
1663 	enum gfe_rxprio prio, const uint8_t eaddr[ETHER_ADDR_LEN])
1664 {
1665 	uint64_t he;
1666 	uint64_t *maybe_he_p = NULL;
1667 	int limit;
1668 	int hash;
1669 	int maybe_hash = 0;
1670 
1671 	GE_FUNC_ENTER(sc, "gfe_hash_entry_op");
1672 
1673 	hash = gfe_hash_compute(sc, eaddr);
1674 
1675 	if (sc->sc_hashtable == NULL) {
1676 		panic("%s:%d: hashtable == NULL!", device_xname(&sc->sc_dev),
1677 			__LINE__);
1678 	}
1679 
1680 	/*
1681 	 * Assume we are going to insert so create the hash entry we
1682 	 * are going to insert.  We also use it to match entries we
1683 	 * will be removing.
1684 	 */
1685 	he = ((uint64_t) eaddr[5] << 43) |
1686 	     ((uint64_t) eaddr[4] << 35) |
1687 	     ((uint64_t) eaddr[3] << 27) |
1688 	     ((uint64_t) eaddr[2] << 19) |
1689 	     ((uint64_t) eaddr[1] << 11) |
1690 	     ((uint64_t) eaddr[0] <<  3) |
1691 	     HSH_PRIO_INS(prio) | HSH_V | HSH_R;
1692 
1693 	/*
1694 	 * The GT will search upto 12 entries for a hit, so we must mimic that.
1695 	 */
1696 	hash &= sc->sc_hashmask / sizeof(he);
1697 	for (limit = HSH_LIMIT; limit > 0 ; --limit) {
1698 		/*
1699 		 * Does the GT wrap at the end, stop at the, or overrun the
1700 		 * end?  Assume it wraps for now.  Stash a copy of the
1701 		 * current hash entry.
1702 		 */
1703 		uint64_t *he_p = &sc->sc_hashtable[hash];
1704 		uint64_t thishe = *he_p;
1705 
1706 		/*
1707 		 * If the hash entry isn't valid, that break the chain.  And
1708 		 * this entry a good candidate for reuse.
1709 		 */
1710 		if ((thishe & HSH_V) == 0) {
1711 			maybe_he_p = he_p;
1712 			break;
1713 		}
1714 
1715 		/*
1716 		 * If the hash entry has the same address we are looking for
1717 		 * then ...  if we are removing and the skip bit is set, its
1718 		 * already been removed.  if are adding and the skip bit is
1719 		 * clear, then its already added.  In either return EBUSY
1720 		 * indicating the op has already been done.  Otherwise flip
1721 		 * the skip bit and return 0.
1722 		 */
1723 		if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
1724 			if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
1725 			    ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
1726 				return EBUSY;
1727 			*he_p = thishe ^ HSH_S;
1728 			bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1729 			    hash * sizeof(he), sizeof(he),
1730 			    BUS_DMASYNC_PREWRITE);
1731 			GE_FUNC_EXIT(sc, "^");
1732 			return 0;
1733 		}
1734 
1735 		/*
1736 		 * If we haven't found a slot for the entry and this entry
1737 		 * is currently being skipped, return this entry.
1738 		 */
1739 		if (maybe_he_p == NULL && (thishe & HSH_S)) {
1740 			maybe_he_p = he_p;
1741 			maybe_hash = hash;
1742 		}
1743 
1744 		hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
1745 	}
1746 
1747 	/*
1748 	 * If we got here, then there was no entry to remove.
1749 	 */
1750 	if (op == GE_HASH_REMOVE) {
1751 		GE_FUNC_EXIT(sc, "?");
1752 		return ENOENT;
1753 	}
1754 
1755 	/*
1756 	 * If we couldn't find a slot, return an error.
1757 	 */
1758 	if (maybe_he_p == NULL) {
1759 		GE_FUNC_EXIT(sc, "!");
1760 		return ENOSPC;
1761 	}
1762 
1763 	/* Update the entry.
1764 	 */
1765 	*maybe_he_p = he;
1766 	bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1767 	    maybe_hash * sizeof(he), sizeof(he), BUS_DMASYNC_PREWRITE);
1768 	GE_FUNC_EXIT(sc, "+");
1769 	return 0;
1770 }
1771 
1772 int
1773 gfe_hash_multichg(struct ethercom *ec, const struct ether_multi *enm, u_long cmd)
1774 {
1775 	struct gfe_softc * const sc = ec->ec_if.if_softc;
1776 	int error;
1777 	enum gfe_hash_op op;
1778 	enum gfe_rxprio prio;
1779 
1780 	GE_FUNC_ENTER(sc, "hash_multichg");
1781 	/*
1782 	 * Is this a wildcard entry?  If so and its being removed, recompute.
1783 	 */
1784 	if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
1785 		if (cmd == SIOCDELMULTI) {
1786 			GE_FUNC_EXIT(sc, "");
1787 			return ENETRESET;
1788 		}
1789 
1790 		/*
1791 		 * Switch in
1792 		 */
1793 		sc->sc_flags |= GE_ALLMULTI;
1794 		if ((sc->sc_pcr & ETH_EPCR_PM) == 0) {
1795 			sc->sc_pcr |= ETH_EPCR_PM;
1796 			GE_WRITE(sc, EPCR, sc->sc_pcr);
1797 			GE_FUNC_EXIT(sc, "");
1798 			return 0;
1799 		}
1800 		GE_FUNC_EXIT(sc, "");
1801 		return ENETRESET;
1802 	}
1803 
1804 	prio = GE_RXPRIO_MEDLO;
1805 	op = (cmd == SIOCDELMULTI ? GE_HASH_REMOVE : GE_HASH_ADD);
1806 
1807 	if (sc->sc_hashtable == NULL) {
1808 		GE_FUNC_EXIT(sc, "");
1809 		return 0;
1810 	}
1811 
1812 	error = gfe_hash_entry_op(sc, op, prio, enm->enm_addrlo);
1813 	if (error == EBUSY) {
1814 		printf("%s: multichg: tried to %s %s again\n",
1815 		       device_xname(&sc->sc_dev),
1816 		       cmd == SIOCDELMULTI ? "remove" : "add",
1817 		       ether_sprintf(enm->enm_addrlo));
1818 		GE_FUNC_EXIT(sc, "");
1819 		return 0;
1820 	}
1821 
1822 	if (error == ENOENT) {
1823 		printf("%s: multichg: failed to remove %s: not in table\n",
1824 		       device_xname(&sc->sc_dev),
1825 		       ether_sprintf(enm->enm_addrlo));
1826 		GE_FUNC_EXIT(sc, "");
1827 		return 0;
1828 	}
1829 
1830 	if (error == ENOSPC) {
1831 		printf("%s: multichg: failed to add %s: no space; regenerating table\n",
1832 		       device_xname(&sc->sc_dev),
1833 		       ether_sprintf(enm->enm_addrlo));
1834 		GE_FUNC_EXIT(sc, "");
1835 		return ENETRESET;
1836 	}
1837 	GE_DPRINTF(sc, ("%s: multichg: %s: %s succeeded\n",
1838 	       device_xname(&sc->sc_dev),
1839 	       cmd == SIOCDELMULTI ? "remove" : "add",
1840 	       ether_sprintf(enm->enm_addrlo)));
1841 	GE_FUNC_EXIT(sc, "");
1842 	return 0;
1843 }
1844 
1845 int
1846 gfe_hash_fill(struct gfe_softc *sc)
1847 {
1848 	struct ether_multistep step;
1849 	struct ether_multi *enm;
1850 	int error;
1851 
1852 	GE_FUNC_ENTER(sc, "gfe_hash_fill");
1853 
1854 	error = gfe_hash_entry_op(sc, GE_HASH_ADD, GE_RXPRIO_HI,
1855 	    CLLADDR(sc->sc_ec.ec_if.if_sadl));
1856 	if (error)
1857 		GE_FUNC_EXIT(sc, "!");
1858 		return error;
1859 
1860 	sc->sc_flags &= ~GE_ALLMULTI;
1861 	if ((sc->sc_ec.ec_if.if_flags & IFF_PROMISC) == 0)
1862 		sc->sc_pcr &= ~ETH_EPCR_PM;
1863 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
1864 	while (enm != NULL) {
1865 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1866 			sc->sc_flags |= GE_ALLMULTI;
1867 			sc->sc_pcr |= ETH_EPCR_PM;
1868 		} else {
1869 			error = gfe_hash_entry_op(sc, GE_HASH_ADD,
1870 			    GE_RXPRIO_MEDLO, enm->enm_addrlo);
1871 			if (error == ENOSPC)
1872 				break;
1873 		}
1874 		ETHER_NEXT_MULTI(step, enm);
1875 	}
1876 
1877 	GE_FUNC_EXIT(sc, "");
1878 	return error;
1879 }
1880 
1881 int
1882 gfe_hash_alloc(struct gfe_softc *sc)
1883 {
1884 	int error;
1885 	GE_FUNC_ENTER(sc, "gfe_hash_alloc");
1886 	sc->sc_hashmask = (sc->sc_pcr & ETH_EPCR_HS_512 ? 16 : 256)*1024 - 1;
1887 	error = gfe_dmamem_alloc(sc, &sc->sc_hash_mem, 1, sc->sc_hashmask + 1,
1888 	    BUS_DMA_NOCACHE);
1889 	if (error) {
1890 		printf("%s: failed to allocate %d bytes for hash table: %d\n",
1891 		    device_xname(&sc->sc_dev), sc->sc_hashmask + 1, error);
1892 		GE_FUNC_EXIT(sc, "");
1893 		return error;
1894 	}
1895 	sc->sc_hashtable = (uint64_t *) sc->sc_hash_mem.gdm_kva;
1896 	memset(sc->sc_hashtable, 0, sc->sc_hashmask + 1);
1897 	bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
1898 	    0, sc->sc_hashmask + 1, BUS_DMASYNC_PREWRITE);
1899 	GE_FUNC_EXIT(sc, "");
1900 	return 0;
1901 }
1902