xref: /freebsd/sys/dev/gem/if_gem.c (revision b5a8f767a62e0253ce02878cd6d69ea7f9574d1a)
1 /*
2  * Copyright (C) 2001 Eduardo Horvath.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	from: NetBSD: gem.c,v 1.9 2001/10/21 20:45:15 thorpej Exp
27  *
28  * $FreeBSD$
29  */
30 
31 /*
32  * Driver for Sun GEM ethernet controllers.
33  */
34 
35 #define	GEM_DEBUG
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/callout.h>
41 #include <sys/mbuf.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 
47 #include <net/ethernet.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 
53 #include <machine/bus.h>
54 
55 #include <dev/mii/mii.h>
56 #include <dev/mii/miivar.h>
57 
58 #include <gem/if_gemreg.h>
59 #include <gem/if_gemvar.h>
60 
61 #define TRIES	10000
62 
63 static void	gem_start __P((struct ifnet *));
64 static void	gem_stop __P((struct ifnet *, int));
65 static int	gem_ioctl __P((struct ifnet *, u_long, caddr_t));
66 static void	gem_cddma_callback __P((void *, bus_dma_segment_t *, int, int));
67 static void	gem_rxdma_callback __P((void *, bus_dma_segment_t *, int, int));
68 static void	gem_txdma_callback __P((void *, bus_dma_segment_t *, int, int));
69 static void	gem_tick __P((void *));
70 static void	gem_watchdog __P((struct ifnet *));
71 static void	gem_init __P((void *));
72 static void	gem_init_regs __P((struct gem_softc *sc));
73 static int	gem_ringsize __P((int sz));
74 static int	gem_meminit __P((struct gem_softc *));
75 static int	gem_dmamap_load_mbuf __P((struct gem_softc *, struct mbuf *,
76     bus_dmamap_callback_t *,  struct gem_txjob *, int));
77 static void	gem_dmamap_unload_mbuf __P((struct gem_softc *,
78     struct gem_txjob *));
79 static void	gem_dmamap_commit_mbuf __P((struct gem_softc *,
80     struct gem_txjob *));
81 static void	gem_mifinit __P((struct gem_softc *));
82 static int	gem_bitwait __P((struct gem_softc *sc, bus_addr_t r,
83     u_int32_t clr, u_int32_t set));
84 static int	gem_reset_rx __P((struct gem_softc *));
85 static int	gem_reset_tx __P((struct gem_softc *));
86 static int	gem_disable_rx __P((struct gem_softc *));
87 static int	gem_disable_tx __P((struct gem_softc *));
88 static void	gem_rxdrain __P((struct gem_softc *));
89 static int	gem_add_rxbuf __P((struct gem_softc *, int));
90 static void	gem_setladrf __P((struct gem_softc *));
91 
92 struct mbuf	*gem_get __P((struct gem_softc *, int, int));
93 static void	gem_eint __P((struct gem_softc *, u_int));
94 static void	gem_rint __P((struct gem_softc *));
95 static void	gem_tint __P((struct gem_softc *));
96 #ifdef notyet
97 static void	gem_power __P((int, void *));
98 #endif
99 
100 devclass_t gem_devclass;
101 DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0);
102 MODULE_DEPEND(gem, miibus, 1, 1, 1);
103 
104 #ifdef GEM_DEBUG
105 #define	DPRINTF(sc, x)	if ((sc)->sc_arpcom.ac_if.if_flags & IFF_DEBUG) \
106 				printf x
107 #include <sys/ktr.h>
108 #define	KTR_GEM		KTR_CT2
109 #else
110 #define	DPRINTF(sc, x)	/* nothing */
111 #endif
112 
113 #define	GEM_NSEGS GEM_NTXSEGS
114 
115 /*
116  * gem_attach:
117  *
118  *	Attach a Gem interface to the system.
119  */
120 int
121 gem_attach(sc)
122 	struct gem_softc *sc;
123 {
124 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
125 	struct mii_softc *child;
126 	int i, error;
127 
128 	/* Make sure the chip is stopped. */
129 	ifp->if_softc = sc;
130 	gem_reset(sc);
131 
132 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
133 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, GEM_NSEGS,
134 	    BUS_SPACE_MAXSIZE_32BIT, 0, &sc->sc_pdmatag);
135 	if (error)
136 		return (error);
137 
138 	error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0,
139 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE,
140 	    GEM_NSEGS, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW,
141 	    &sc->sc_dmatag);
142 	if (error)
143 		goto fail_0;
144 
145 	error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0,
146 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
147 	    sizeof(struct gem_control_data), 1,
148 	    sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW,
149 	    &sc->sc_cdmatag);
150 	if (error)
151 		goto fail_1;
152 
153 	/*
154 	 * Allocate the control data structures, and create and load the
155 	 * DMA map for it.
156 	 */
157 	if ((error = bus_dmamem_alloc(sc->sc_cdmatag,
158 	    (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) {
159 		device_printf(sc->sc_dev, "unable to allocate control data,"
160 		    " error = %d\n", error);
161 		goto fail_2;
162 	}
163 
164 	sc->sc_cddma = 0;
165 	if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap,
166 	    sc->sc_control_data, sizeof(struct gem_control_data),
167 	    gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) {
168 		device_printf(sc->sc_dev, "unable to load control data DMA "
169 		    "map, error = %d\n", error);
170 		goto fail_3;
171 	}
172 
173 	/*
174 	 * Initialize the transmit job descriptors.
175 	 */
176 	STAILQ_INIT(&sc->sc_txfreeq);
177 	STAILQ_INIT(&sc->sc_txdirtyq);
178 
179 	/*
180 	 * Create the transmit buffer DMA maps.
181 	 */
182 	error = ENOMEM;
183 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
184 		struct gem_txsoft *txs;
185 
186 		txs = &sc->sc_txsoft[i];
187 		txs->txs_mbuf = NULL;
188 		txs->txs_ndescs = 0;
189 		if ((error = bus_dmamap_create(sc->sc_dmatag, 0,
190 		    &txs->txs_dmamap)) != 0) {
191 			device_printf(sc->sc_dev, "unable to create tx DMA map "
192 			    "%d, error = %d\n", i, error);
193 			goto fail_4;
194 		}
195 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
196 	}
197 
198 	/*
199 	 * Create the receive buffer DMA maps.
200 	 */
201 	for (i = 0; i < GEM_NRXDESC; i++) {
202 		if ((error = bus_dmamap_create(sc->sc_dmatag, 0,
203 		    &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
204 			device_printf(sc->sc_dev, "unable to create rx DMA map "
205 			    "%d, error = %d\n", i, error);
206 			goto fail_5;
207 		}
208 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
209 	}
210 
211 
212 	gem_mifinit(sc);
213 
214 	if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange,
215 	    gem_mediastatus)) != 0) {
216 		device_printf(sc->sc_dev, "phy probe failed: %d\n", error);
217 		goto fail_5;
218 	}
219 	sc->sc_mii = device_get_softc(sc->sc_miibus);
220 
221 	/*
222 	 * From this point forward, the attachment cannot fail.  A failure
223 	 * before this point releases all resources that may have been
224 	 * allocated.
225 	 */
226 
227 	/* Announce ourselves. */
228 	device_printf(sc->sc_dev, "Ethernet address:");
229 	for (i = 0; i < 6; i++)
230 		printf("%c%02x", i > 0 ? ':' : ' ', sc->sc_arpcom.ac_enaddr[i]);
231 	printf("\n");
232 
233 	/* Initialize ifnet structure. */
234 	ifp->if_softc = sc;
235 	ifp->if_unit = device_get_unit(sc->sc_dev);
236 	ifp->if_name = "gem";
237 	ifp->if_mtu = ETHERMTU;
238 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239 	ifp->if_start = gem_start;
240 	ifp->if_ioctl = gem_ioctl;
241 	ifp->if_watchdog = gem_watchdog;
242 	ifp->if_init = gem_init;
243 	ifp->if_output = ether_output;
244 	ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN;
245 	/*
246 	 * Walk along the list of attached MII devices and
247 	 * establish an `MII instance' to `phy number'
248 	 * mapping. We'll use this mapping in media change
249 	 * requests to determine which phy to use to program
250 	 * the MIF configuration register.
251 	 */
252 	for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL;
253 	     child = LIST_NEXT(child, mii_list)) {
254 		/*
255 		 * Note: we support just two PHYs: the built-in
256 		 * internal device and an external on the MII
257 		 * connector.
258 		 */
259 		if (child->mii_phy > 1 || child->mii_inst > 1) {
260 			device_printf(sc->sc_dev, "cannot accomodate "
261 			    "MII device %s at phy %d, instance %d\n",
262 			    device_get_name(child->mii_dev),
263 			    child->mii_phy, child->mii_inst);
264 			continue;
265 		}
266 
267 		sc->sc_phys[child->mii_inst] = child->mii_phy;
268 	}
269 
270 	/*
271 	 * Now select and activate the PHY we will use.
272 	 *
273 	 * The order of preference is External (MDI1),
274 	 * Internal (MDI0), Serial Link (no MII).
275 	 */
276 	if (sc->sc_phys[1]) {
277 #ifdef GEM_DEBUG
278 		printf("using external phy\n");
279 #endif
280 		sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
281 	} else {
282 #ifdef GEM_DEBUG
283 		printf("using internal phy\n");
284 #endif
285 		sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
286 	}
287 	bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG,
288 	    sc->sc_mif_config);
289 	/* Attach the interface. */
290 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
291 
292 #if notyet
293 	/*
294 	 * Add a suspend hook to make sure we come back up after a
295 	 * resume.
296 	 */
297 	sc->sc_powerhook = powerhook_establish(gem_power, sc);
298 	if (sc->sc_powerhook == NULL)
299 		device_printf(sc->sc_dev, "WARNING: unable to establish power "
300 		    "hook\n");
301 #endif
302 
303 	callout_init(&sc->sc_tick_ch, 0);
304 	return (0);
305 
306 	/*
307 	 * Free any resources we've allocated during the failed attach
308 	 * attempt.  Do this in reverse order and fall through.
309 	 */
310 fail_5:
311 	for (i = 0; i < GEM_NRXDESC; i++) {
312 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
313 			bus_dmamap_destroy(sc->sc_dmatag,
314 			    sc->sc_rxsoft[i].rxs_dmamap);
315 	}
316 fail_4:
317 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
318 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
319 			bus_dmamap_destroy(sc->sc_dmatag,
320 			    sc->sc_txsoft[i].txs_dmamap);
321 	}
322 	bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
323 fail_3:
324 	bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data,
325 	    sc->sc_cddmamap);
326 fail_2:
327 	bus_dma_tag_destroy(sc->sc_cdmatag);
328 fail_1:
329 	bus_dma_tag_destroy(sc->sc_dmatag);
330 fail_0:
331 	bus_dma_tag_destroy(sc->sc_pdmatag);
332 	return (error);
333 }
334 
335 static void
336 gem_cddma_callback(xsc, segs, nsegs, error)
337 	void *xsc;
338 	bus_dma_segment_t *segs;
339 	int nsegs;
340 	int error;
341 {
342 	struct gem_softc *sc = (struct gem_softc *)xsc;
343 
344 	if (error != 0)
345 		return;
346 	if (nsegs != 1) {
347 		/* can't happen... */
348 		panic("gem_cddma_callback: bad control buffer segment count");
349 	}
350 	sc->sc_cddma = segs[0].ds_addr;
351 }
352 
353 static void
354 gem_rxdma_callback(xsc, segs, nsegs, error)
355 	void *xsc;
356 	bus_dma_segment_t *segs;
357 	int nsegs;
358 	int error;
359 {
360 	struct gem_rxsoft *rxs = (struct gem_rxsoft *)xsc;
361 
362 	if (error != 0)
363 		return;
364 	if (nsegs != 1) {
365 		/* can't happen... */
366 		panic("gem_rxdma_callback: bad control buffer segment count");
367 	}
368 	rxs->rxs_paddr = segs[0].ds_addr;
369 }
370 
371 /*
372  * This is called multiple times in our version of dmamap_load_mbuf, but should
373  * be fit for a generic version that only calls it once.
374  */
375 static void
376 gem_txdma_callback(xsc, segs, nsegs, error)
377 	void *xsc;
378 	bus_dma_segment_t *segs;
379 	int nsegs;
380 	int error;
381 {
382 	struct gem_txdma *tx = (struct gem_txdma *)xsc;
383 	int seg;
384 
385 	tx->txd_error = error;
386 	if (error != 0)
387 		return;
388 	tx->txd_nsegs = nsegs;
389 
390 	/*
391 	 * Initialize the transmit descriptors.
392 	 */
393 	for (seg = 0; seg < nsegs;
394 	     seg++, tx->txd_nexttx = GEM_NEXTTX(tx->txd_nexttx)) {
395 		uint64_t flags;
396 
397 		DPRINTF(tx->txd_sc, ("txdma_cb: mapping seg %d (txd %d), len "
398 		    "%lx, addr %#lx (%#lx)\n",  seg, tx->txd_nexttx,
399 		    segs[seg].ds_len, segs[seg].ds_addr,
400 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr)));
401 		CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len "
402 		    "%lx, addr %#lx (%#lx)",  seg, tx->txd_nexttx,
403 		    segs[seg].ds_len, segs[seg].ds_addr,
404 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr));
405 		/*
406 		 * If this is the first descriptor we're
407 		 * enqueueing, set the start of packet flag,
408 		 * and the checksum stuff if we want the hardware
409 		 * to do it.
410 		 */
411 		tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_addr =
412 		    GEM_DMA_WRITE(tx->txd_sc, segs[seg].ds_addr);
413 		flags = segs[seg].ds_len & GEM_TD_BUFSIZE;
414 		if ((tx->txd_flags & GTXD_FIRST) != 0 && seg == 0) {
415 			CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, "
416 			    "tx %d", seg, tx->txd_nexttx);
417 			flags |= GEM_TD_START_OF_PACKET;
418 		}
419 		if ((tx->txd_flags & GTXD_LAST) != 0 && seg == nsegs - 1) {
420 			CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, "
421 			    "tx %d", seg, tx->txd_nexttx);
422 			flags |= GEM_TD_END_OF_PACKET;
423 		}
424 		tx->txd_sc->sc_txdescs[tx->txd_nexttx].gd_flags =
425 		    GEM_DMA_WRITE(tx->txd_sc, flags);
426 		tx->txd_lasttx = tx->txd_nexttx;
427 	}
428 }
429 
430 static void
431 gem_tick(arg)
432 	void *arg;
433 {
434 	struct gem_softc *sc = arg;
435 	int s;
436 
437 	s = splnet();
438 	mii_tick(sc->sc_mii);
439 	splx(s);
440 
441 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
442 }
443 
444 static int
445 gem_bitwait(sc, r, clr, set)
446 	struct gem_softc *sc;
447 	bus_addr_t r;
448 	u_int32_t clr;
449 	u_int32_t set;
450 {
451 	int i;
452 	u_int32_t reg;
453 
454 	for (i = TRIES; i--; DELAY(100)) {
455 		reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r);
456 		if ((r & clr) == 0 && (r & set) == set)
457 			return (1);
458 	}
459 	return (0);
460 }
461 
462 void
463 gem_reset(sc)
464 	struct gem_softc *sc;
465 {
466 	bus_space_tag_t t = sc->sc_bustag;
467 	bus_space_handle_t h = sc->sc_h;
468 	int s;
469 
470 	s = splnet();
471 	DPRINTF(sc, ("%s: gem_reset\n", device_get_name(sc->sc_dev)));
472 	CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev));
473 	gem_reset_rx(sc);
474 	gem_reset_tx(sc);
475 
476 	/* Do a full reset */
477 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX);
478 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
479 		device_printf(sc->sc_dev, "cannot reset device\n");
480 	splx(s);
481 }
482 
483 
484 /*
485  * gem_rxdrain:
486  *
487  *	Drain the receive queue.
488  */
489 static void
490 gem_rxdrain(sc)
491 	struct gem_softc *sc;
492 {
493 	struct gem_rxsoft *rxs;
494 	int i;
495 
496 	for (i = 0; i < GEM_NRXDESC; i++) {
497 		rxs = &sc->sc_rxsoft[i];
498 		if (rxs->rxs_mbuf != NULL) {
499 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
500 			m_freem(rxs->rxs_mbuf);
501 			rxs->rxs_mbuf = NULL;
502 		}
503 	}
504 }
505 
506 /*
507  * Reset the whole thing.
508  */
509 static void
510 gem_stop(ifp, disable)
511 	struct ifnet *ifp;
512 	int disable;
513 {
514 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
515 	struct gem_txsoft *txs;
516 
517 	DPRINTF(sc, ("%s: gem_stop\n", device_get_name(sc->sc_dev)));
518 	CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev));
519 
520 	callout_stop(&sc->sc_tick_ch);
521 
522 	/* XXX - Should we reset these instead? */
523 	gem_disable_tx(sc);
524 	gem_disable_rx(sc);
525 
526 	/*
527 	 * Release any queued transmit buffers.
528 	 */
529 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
530 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
531 		if (txs->txs_ndescs != 0) {
532 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
533 			if (txs->txs_mbuf != NULL) {
534 				m_freem(txs->txs_mbuf);
535 				txs->txs_mbuf = NULL;
536 			}
537 		}
538 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
539 	}
540 
541 	if (disable)
542 		gem_rxdrain(sc);
543 
544 	/*
545 	 * Mark the interface down and cancel the watchdog timer.
546 	 */
547 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
548 	ifp->if_timer = 0;
549 }
550 
551 /*
552  * Reset the receiver
553  */
554 int
555 gem_reset_rx(sc)
556 	struct gem_softc *sc;
557 {
558 	bus_space_tag_t t = sc->sc_bustag;
559 	bus_space_handle_t h = sc->sc_h;
560 
561 	/*
562 	 * Resetting while DMA is in progress can cause a bus hang, so we
563 	 * disable DMA first.
564 	 */
565 	gem_disable_rx(sc);
566 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
567 	/* Wait till it finishes */
568 	if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0))
569 		device_printf(sc->sc_dev, "cannot disable read dma\n");
570 
571 	/* Wait 5ms extra. */
572 	DELAY(5000);
573 
574 	/* Finally, reset the ERX */
575 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX);
576 	/* Wait till it finishes */
577 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
578 		device_printf(sc->sc_dev, "cannot reset receiver\n");
579 		return (1);
580 	}
581 	return (0);
582 }
583 
584 
585 /*
586  * Reset the transmitter
587  */
588 static int
589 gem_reset_tx(sc)
590 	struct gem_softc *sc;
591 {
592 	bus_space_tag_t t = sc->sc_bustag;
593 	bus_space_handle_t h = sc->sc_h;
594 	int i;
595 
596 	/*
597 	 * Resetting while DMA is in progress can cause a bus hang, so we
598 	 * disable DMA first.
599 	 */
600 	gem_disable_tx(sc);
601 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
602 	/* Wait till it finishes */
603 	if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0))
604 		device_printf(sc->sc_dev, "cannot disable read dma\n");
605 
606 	/* Wait 5ms extra. */
607 	DELAY(5000);
608 
609 	/* Finally, reset the ETX */
610 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX);
611 	/* Wait till it finishes */
612 	for (i = TRIES; i--; DELAY(100))
613 		if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0)
614 			break;
615 	if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) {
616 		device_printf(sc->sc_dev, "cannot reset receiver\n");
617 		return (1);
618 	}
619 	return (0);
620 }
621 
622 /*
623  * disable receiver.
624  */
625 static int
626 gem_disable_rx(sc)
627 	struct gem_softc *sc;
628 {
629 	bus_space_tag_t t = sc->sc_bustag;
630 	bus_space_handle_t h = sc->sc_h;
631 	u_int32_t cfg;
632 
633 	/* Flip the enable bit */
634 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
635 	cfg &= ~GEM_MAC_RX_ENABLE;
636 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
637 
638 	/* Wait for it to finish */
639 	return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
640 }
641 
642 /*
643  * disable transmitter.
644  */
645 static int
646 gem_disable_tx(sc)
647 	struct gem_softc *sc;
648 {
649 	bus_space_tag_t t = sc->sc_bustag;
650 	bus_space_handle_t h = sc->sc_h;
651 	u_int32_t cfg;
652 
653 	/* Flip the enable bit */
654 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
655 	cfg &= ~GEM_MAC_TX_ENABLE;
656 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
657 
658 	/* Wait for it to finish */
659 	return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
660 }
661 
662 /*
663  * Initialize interface.
664  */
665 static int
666 gem_meminit(sc)
667 	struct gem_softc *sc;
668 {
669 	struct gem_rxsoft *rxs;
670 	int i, error;
671 
672 	/*
673 	 * Initialize the transmit descriptor ring.
674 	 */
675 	memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
676 	for (i = 0; i < GEM_NTXDESC; i++) {
677 		sc->sc_txdescs[i].gd_flags = 0;
678 		sc->sc_txdescs[i].gd_addr = 0;
679 	}
680 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
681 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
682 	sc->sc_txfree = GEM_NTXDESC;
683 	sc->sc_txnext = 0;
684 
685 	/*
686 	 * Initialize the receive descriptor and receive job
687 	 * descriptor rings.
688 	 */
689 	for (i = 0; i < GEM_NRXDESC; i++) {
690 		rxs = &sc->sc_rxsoft[i];
691 		if (rxs->rxs_mbuf == NULL) {
692 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
693 				device_printf(sc->sc_dev, "unable to "
694 				    "allocate or map rx buffer %d, error = "
695 				    "%d\n", i, error);
696 				/*
697 				 * XXX Should attempt to run with fewer receive
698 				 * XXX buffers instead of just failing.
699 				 */
700 				gem_rxdrain(sc);
701 				return (1);
702 			}
703 		} else
704 			GEM_INIT_RXDESC(sc, i);
705 	}
706 	sc->sc_rxptr = 0;
707 
708 	return (0);
709 }
710 
711 static int
712 gem_ringsize(sz)
713 	int sz;
714 {
715 	int v = 0;
716 
717 	switch (sz) {
718 	case 32:
719 		v = GEM_RING_SZ_32;
720 		break;
721 	case 64:
722 		v = GEM_RING_SZ_64;
723 		break;
724 	case 128:
725 		v = GEM_RING_SZ_128;
726 		break;
727 	case 256:
728 		v = GEM_RING_SZ_256;
729 		break;
730 	case 512:
731 		v = GEM_RING_SZ_512;
732 		break;
733 	case 1024:
734 		v = GEM_RING_SZ_1024;
735 		break;
736 	case 2048:
737 		v = GEM_RING_SZ_2048;
738 		break;
739 	case 4096:
740 		v = GEM_RING_SZ_4096;
741 		break;
742 	case 8192:
743 		v = GEM_RING_SZ_8192;
744 		break;
745 	default:
746 		printf("gem: invalid Receive Descriptor ring size\n");
747 		break;
748 	}
749 	return (v);
750 }
751 
752 /*
753  * Initialization of interface; set up initialization block
754  * and transmit/receive descriptor rings.
755  */
756 static void
757 gem_init(xsc)
758 	void *xsc;
759 {
760 	struct gem_softc *sc = (struct gem_softc *)xsc;
761 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
762 	bus_space_tag_t t = sc->sc_bustag;
763 	bus_space_handle_t h = sc->sc_h;
764 	int s;
765 	u_int32_t v;
766 
767 	s = splnet();
768 
769 	DPRINTF(sc, ("%s: gem_init: calling stop\n", device_get_name(sc->sc_dev)));
770 	CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev));
771 	/*
772 	 * Initialization sequence. The numbered steps below correspond
773 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
774 	 * Channel Engine manual (part of the PCIO manual).
775 	 * See also the STP2002-STQ document from Sun Microsystems.
776 	 */
777 
778 	/* step 1 & 2. Reset the Ethernet Channel */
779 	gem_stop(&sc->sc_arpcom.ac_if, 0);
780 	gem_reset(sc);
781 	DPRINTF(sc, ("%s: gem_init: restarting\n", device_get_name(sc->sc_dev)));
782 	CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev));
783 
784 	/* Re-initialize the MIF */
785 	gem_mifinit(sc);
786 
787 	/* Call MI reset function if any */
788 	if (sc->sc_hwreset)
789 		(*sc->sc_hwreset)(sc);
790 
791 	/* step 3. Setup data structures in host memory */
792 	gem_meminit(sc);
793 
794 	/* step 4. TX MAC registers & counters */
795 	gem_init_regs(sc);
796 	/* XXX: VLAN code from NetBSD temporarily removed. */
797 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
798             (ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16));
799 
800 	/* step 5. RX MAC registers & counters */
801 	gem_setladrf(sc);
802 
803 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
804 	/* NOTE: we use only 32-bit DMA addresses here. */
805 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
806 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
807 
808 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
809 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
810 	DPRINTF(sc, ("loading rx ring %lx, tx ring %lx, cddma %lx\n",
811 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma));
812 	CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx",
813 	    GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma);
814 
815 	/* step 8. Global Configuration & Interrupt Mask */
816 	bus_space_write_4(t, h, GEM_INTMASK,
817 		      ~(GEM_INTR_TX_INTME|
818 			GEM_INTR_TX_EMPTY|
819 			GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF|
820 			GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS|
821 			GEM_INTR_MAC_CONTROL|GEM_INTR_MIF|
822 			GEM_INTR_BERR));
823 	bus_space_write_4(t, h, GEM_MAC_RX_MASK, 0); /* XXXX */
824 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */
825 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */
826 
827 	/* step 9. ETX Configuration: use mostly default values */
828 
829 	/* Enable DMA */
830 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
831 	bus_space_write_4(t, h, GEM_TX_CONFIG,
832 		v|GEM_TX_CONFIG_TXDMA_EN|
833 		((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH));
834 
835 	/* step 10. ERX Configuration */
836 
837 	/* Encode Receive Descriptor ring size: four possible values */
838 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
839 
840 	/* Enable DMA */
841 	bus_space_write_4(t, h, GEM_RX_CONFIG,
842 		v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)|
843 		(2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN|
844 		(0<<GEM_RX_CONFIG_CXM_START_SHFT));
845 	/*
846 	 * The following value is for an OFF Threshold of about 15.5 Kbytes
847 	 * and an ON Threshold of 4K bytes.
848 	 */
849 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH, 0xf8 | (0x40 << 12));
850 	bus_space_write_4(t, h, GEM_RX_BLANKING, (2<<12)|6);
851 
852 	/* step 11. Configure Media */
853 	(void)gem_mii_statchg(sc->sc_dev);
854 
855 	/* step 12. RX_MAC Configuration Register */
856 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
857 	v |= GEM_MAC_RX_ENABLE;
858 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
859 
860 	/* step 14. Issue Transmit Pending command */
861 
862 	/* Call MI initialization function if any */
863 	if (sc->sc_hwinit)
864 		(*sc->sc_hwinit)(sc);
865 
866 	/* step 15.  Give the reciever a swift kick */
867 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
868 
869 	/* Start the one second timer. */
870 	callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
871 
872 	ifp->if_flags |= IFF_RUNNING;
873 	ifp->if_flags &= ~IFF_OACTIVE;
874 	ifp->if_timer = 0;
875 	sc->sc_flags = ifp->if_flags;
876 	splx(s);
877 }
878 
879 /*
880  * XXX: This is really a substitute for bus_dmamap_load_mbuf(), which FreeBSD
881  * does not yet have, with some adaptions for this driver.
882  * Some changes are mandated by the fact that multiple maps may needed to map
883  * a single mbuf.
884  * It should be removed once generic support is available.
885  *
886  * This is derived from NetBSD (syssrc/sys/arch/sparc64/sparc64/machdep.c), for
887  * a copyright notice see sparc64/sparc64/bus_machdep.c.
888  *
889  * Not every error condition is passed to the callback in this version, and the
890  * callback may be called more than once.
891  * It also gropes in the entails of the callback arg...
892  */
893 static int
894 gem_dmamap_load_mbuf(sc, m0, cb, txj, flags)
895 	struct gem_softc *sc;
896 	struct mbuf *m0;
897 	bus_dmamap_callback_t *cb;
898 	struct gem_txjob *txj;
899 	int flags;
900 {
901 	struct gem_txdma txd;
902 	struct gem_txsoft *txs;
903 	struct mbuf *m;
904 	void  *vaddr;
905 	int error, first = 1, len, totlen;
906 
907 	if ((m0->m_flags & M_PKTHDR) == 0)
908 		panic("gem_dmamap_load_mbuf: no packet header");
909 	totlen = m0->m_pkthdr.len;
910 	len = 0;
911 	txd.txd_sc = sc;
912 	txd.txd_nexttx = txj->txj_nexttx;
913 	txj->txj_nsegs = 0;
914 	STAILQ_INIT(&txj->txj_txsq);
915 	m = m0;
916 	while (m != NULL && len < totlen) {
917 		if (m->m_len == 0)
918 			continue;
919 		/* Get a work queue entry. */
920 		if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) {
921 			/*
922 			 * Ran out of descriptors, return a value that
923 			 * cannot be returned by bus_dmamap_load to notify
924 			 * the caller.
925 			 */
926 			error = -1;
927 			goto fail;
928 		}
929 		len += m->m_len;
930 		txd.txd_flags = first ? GTXD_FIRST : 0;
931 		if (m->m_next == NULL || len >= totlen)
932 			txd.txd_flags |= GTXD_LAST;
933 		vaddr = mtod(m, void *);
934 		error = bus_dmamap_load(sc->sc_dmatag, txs->txs_dmamap, vaddr,
935 		    m->m_len, cb, &txd, flags);
936 		if (error != 0 || txd.txd_error != 0)
937 			goto fail;
938 		/* Sync the DMA map. */
939 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
940 		    BUS_DMASYNC_PREWRITE);
941 		m = m->m_next;
942 		/*
943 		 * Store a pointer to the packet so we can free it later,
944 		 * and remember what txdirty will be once the packet is
945 		 * done.
946 		 */
947 		txs->txs_mbuf = first ? m0 : NULL;
948 		txs->txs_firstdesc = txj->txj_nexttx;
949 		txs->txs_lastdesc = txd.txd_lasttx;
950 		txs->txs_ndescs = txd.txd_nsegs;
951 		CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, "
952 		    "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc,
953 		    txs->txs_ndescs);
954 		STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
955 		STAILQ_INSERT_TAIL(&txj->txj_txsq, txs, txs_q);
956 		txj->txj_nexttx = txd.txd_nexttx;
957 		txj->txj_nsegs += txd.txd_nsegs;
958 		first = 0;
959 	}
960 	txj->txj_lasttx = txd.txd_lasttx;
961 	return (0);
962 
963 fail:
964 	CTR1(KTR_GEM, "gem_dmamap_load_mbuf failed (%d)", error);
965 	gem_dmamap_unload_mbuf(sc, txj);
966 	return (error);
967 }
968 
969 /*
970  * Unload an mbuf using the txd the information was placed in.
971  * The tx interrupt code frees the tx segments one by one, because the txd is
972  * not available any more.
973  */
974 static void
975 gem_dmamap_unload_mbuf(sc, txj)
976 	struct gem_softc *sc;
977 	struct gem_txjob *txj;
978 {
979 	struct gem_txsoft *txs;
980 
981 	/* Readd the removed descriptors and unload the segments. */
982 	while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) {
983 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
984 		STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q);
985 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
986 	}
987 }
988 
989 static void
990 gem_dmamap_commit_mbuf(sc, txj)
991 	struct gem_softc *sc;
992 	struct gem_txjob *txj;
993 {
994 	struct gem_txsoft *txs;
995 
996 	/* Commit the txjob by transfering the txsoft's to the txdirtyq. */
997 	while ((txs = STAILQ_FIRST(&txj->txj_txsq)) != NULL) {
998 		STAILQ_REMOVE_HEAD(&txj->txj_txsq, txs_q);
999 		STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1000 	}
1001 }
1002 
1003 static void
1004 gem_init_regs(sc)
1005 	struct gem_softc *sc;
1006 {
1007 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1008 	bus_space_tag_t t = sc->sc_bustag;
1009 	bus_space_handle_t h = sc->sc_h;
1010 
1011 	/* These regs are not cleared on reset */
1012 	sc->sc_inited = 0;
1013 	if (!sc->sc_inited) {
1014 
1015 		/* Wooo.  Magic values. */
1016 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0);
1017 		bus_space_write_4(t, h, GEM_MAC_IPG1, 8);
1018 		bus_space_write_4(t, h, GEM_MAC_IPG2, 4);
1019 
1020 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
1021 		/* Max frame and max burst size */
1022 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
1023 			(ifp->if_mtu+18) | (0x2000<<16)/* Burst size */);
1024 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7);
1025 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4);
1026 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
1027 		/* Dunno.... */
1028 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
1029 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
1030 			((sc->sc_arpcom.ac_enaddr[5]<<8)|
1031 			  sc->sc_arpcom.ac_enaddr[4])&0x3ff);
1032 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
1033 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
1034 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
1035 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
1036 		/* MAC control addr set to 0:1:c2:0:1:80 */
1037 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
1038 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
1039 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
1040 
1041 		/* MAC filter addr set to 0:0:0:0:0:0 */
1042 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
1043 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
1044 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
1045 
1046 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
1047 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
1048 
1049 		sc->sc_inited = 1;
1050 	}
1051 
1052 	/* Counters need to be zeroed */
1053 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
1054 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
1055 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
1056 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
1057 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
1058 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
1059 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
1060 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
1061 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
1062 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
1063 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
1064 
1065 	/* Un-pause stuff */
1066 #if 0
1067 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
1068 #else
1069 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0);
1070 #endif
1071 
1072 	/*
1073 	 * Set the station address.
1074 	 */
1075 	bus_space_write_4(t, h, GEM_MAC_ADDR0,
1076 		(sc->sc_arpcom.ac_enaddr[4]<<8) | sc->sc_arpcom.ac_enaddr[5]);
1077 	bus_space_write_4(t, h, GEM_MAC_ADDR1,
1078 		(sc->sc_arpcom.ac_enaddr[2]<<8) | sc->sc_arpcom.ac_enaddr[3]);
1079 	bus_space_write_4(t, h, GEM_MAC_ADDR2,
1080 		(sc->sc_arpcom.ac_enaddr[0]<<8) | sc->sc_arpcom.ac_enaddr[1]);
1081 }
1082 
1083 static void
1084 gem_start(ifp)
1085 	struct ifnet *ifp;
1086 {
1087 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
1088 	struct mbuf *m0 = NULL, *m;
1089 	struct gem_txjob txj;
1090 	int firsttx, ofree, seg, ntx, txmfail;
1091 
1092 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1093 		return;
1094 
1095 	/*
1096 	 * Remember the previous number of free descriptors and
1097 	 * the first descriptor we'll use.
1098 	 */
1099 	ofree = sc->sc_txfree;
1100 	firsttx = sc->sc_txnext;
1101 
1102 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
1103 	    device_get_name(sc->sc_dev), ofree, firsttx));
1104 	CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d",
1105 	    device_get_name(sc->sc_dev), ofree, firsttx);
1106 
1107 	txj.txj_nexttx = firsttx;
1108 	txj.txj_lasttx = 0;
1109 	/*
1110 	 * Loop through the send queue, setting up transmit descriptors
1111 	 * until we drain the queue, or use up all available transmit
1112 	 * descriptors.
1113 	 */
1114 	txmfail = 0;
1115 	for (ntx = 0;; ntx++) {
1116 		/*
1117 		 * Grab a packet off the queue.
1118 		 */
1119 		IF_DEQUEUE(&ifp->if_snd, m0);
1120 		if (m0 == NULL)
1121 			break;
1122 		m = NULL;
1123 
1124 		/*
1125 		 * Load the DMA map.  If this fails, the packet either
1126 		 * didn't fit in the alloted number of segments, or we were
1127 		 * short on resources.  In this case, we'll copy and try
1128 		 * again.
1129 		 */
1130 		txmfail = gem_dmamap_load_mbuf(sc, m0,
1131 		    gem_txdma_callback, &txj, BUS_DMA_NOWAIT);
1132 		if (txmfail == -1) {
1133 			IF_PREPEND(&ifp->if_snd, m0);
1134 			break;
1135 		}
1136 		if (txmfail > 0) {
1137 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1138 			if (m == NULL) {
1139 				device_printf(sc->sc_dev, "unable to "
1140 				    "allocate Tx mbuf\n");
1141 				/* Failed; requeue. */
1142 				IF_PREPEND(&ifp->if_snd, m0);
1143 				break;
1144 			}
1145 			if (m0->m_pkthdr.len > MHLEN) {
1146 				MCLGET(m, M_DONTWAIT);
1147 				if ((m->m_flags & M_EXT) == 0) {
1148 					device_printf(sc->sc_dev, "unable to "
1149 					    "allocate Tx cluster\n");
1150 					IF_PREPEND(&ifp->if_snd, m0);
1151 					m_freem(m);
1152 					break;
1153 				}
1154 			}
1155 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
1156 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1157 			txmfail = gem_dmamap_load_mbuf(sc, m,
1158 			    gem_txdma_callback, &txj, BUS_DMA_NOWAIT);
1159 			if (txmfail != 0) {
1160 				if (txmfail > 0) {
1161 					device_printf(sc->sc_dev, "unable to "
1162 					    "load Tx buffer, error = %d\n",
1163 					    txmfail);
1164 				}
1165 				m_freem(m);
1166 				IF_PREPEND(&ifp->if_snd, m0);
1167 				break;
1168 			}
1169 		}
1170 
1171 		/*
1172 		 * Ensure we have enough descriptors free to describe
1173 		 * the packet.  Note, we always reserve one descriptor
1174 		 * at the end of the ring as a termination point, to
1175 		 * prevent wrap-around.
1176 		 */
1177 		if (txj.txj_nsegs > (sc->sc_txfree - 1)) {
1178 			/*
1179 			 * Not enough free descriptors to transmit this
1180 			 * packet.  We haven't committed to anything yet,
1181 			 * so just unload the DMA map, put the packet
1182 			 * back on the queue, and punt.  Notify the upper
1183 			 * layer that there are no more slots left.
1184 			 *
1185 			 * XXX We could allocate an mbuf and copy, but
1186 			 * XXX it is worth it?
1187 			 */
1188 			ifp->if_flags |= IFF_OACTIVE;
1189 			gem_dmamap_unload_mbuf(sc, &txj);
1190 			if (m != NULL)
1191 				m_freem(m);
1192 			IF_PREPEND(&ifp->if_snd, m0);
1193 			break;
1194 		}
1195 
1196 		if (m != NULL)
1197 			m_freem(m0);
1198 
1199 		/*
1200 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1201 		 */
1202 
1203 #ifdef GEM_DEBUG
1204 		if (ifp->if_flags & IFF_DEBUG) {
1205 			printf("     gem_start %p transmit chain:\n",
1206 			    STAILQ_FIRST(&txj.txj_txsq));
1207 			for (seg = sc->sc_txnext;; seg = GEM_NEXTTX(seg)) {
1208 				printf("descriptor %d:\t", seg);
1209 				printf("gd_flags:   0x%016llx\t", (long long)
1210 					GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_flags));
1211 				printf("gd_addr: 0x%016llx\n", (long long)
1212 					GEM_DMA_READ(sc, sc->sc_txdescs[seg].gd_addr));
1213 				if (seg == txj.txj_lasttx)
1214 					break;
1215 			}
1216 		}
1217 #endif
1218 
1219 		/* Sync the descriptors we're using. */
1220 		GEM_CDTXSYNC(sc, sc->sc_txnext, txj.txj_nsegs,
1221 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1222 
1223 		/* Advance the tx pointer. */
1224 		sc->sc_txfree -= txj.txj_nsegs;
1225 		sc->sc_txnext = txj.txj_nexttx;
1226 
1227 		gem_dmamap_commit_mbuf(sc, &txj);
1228 	}
1229 
1230 	if (txmfail == -1 || sc->sc_txfree == 0) {
1231 		ifp->if_flags |= IFF_OACTIVE;
1232 		/* No more slots left; notify upper layer. */
1233 	}
1234 
1235 	if (ntx > 0) {
1236 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
1237 		    device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx));
1238 		CTR3(KTR_GEM, "%s: packets enqueued, IC on %d, OWN on %d",
1239 		    device_get_name(sc->sc_dev), txj.txj_lasttx, firsttx);
1240 		/*
1241 		 * The entire packet chain is set up.
1242 		 * Kick the transmitter.
1243 		 */
1244 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
1245 			device_get_name(sc->sc_dev), txj.txj_nexttx));
1246 		CTR3(KTR_GEM, "%s: gem_start: kicking tx %d=%d",
1247 		    device_get_name(sc->sc_dev), txj.txj_nexttx,
1248 		    sc->sc_txnext);
1249 		bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK,
1250 			sc->sc_txnext);
1251 
1252 		/* Set a watchdog timer in case the chip flakes out. */
1253 		ifp->if_timer = 5;
1254 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
1255 			device_get_name(sc->sc_dev), ifp->if_timer));
1256 		CTR2(KTR_GEM, "%s: gem_start: watchdog %d",
1257 			device_get_name(sc->sc_dev), ifp->if_timer);
1258 	}
1259 }
1260 
1261 /*
1262  * Transmit interrupt.
1263  */
1264 static void
1265 gem_tint(sc)
1266 	struct gem_softc *sc;
1267 {
1268 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1269 	bus_space_tag_t t = sc->sc_bustag;
1270 	bus_space_handle_t mac = sc->sc_h;
1271 	struct gem_txsoft *txs;
1272 	int txlast;
1273 
1274 
1275 	DPRINTF(sc, ("%s: gem_tint\n", device_get_name(sc->sc_dev)));
1276 	CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev));
1277 
1278 	/*
1279 	 * Unload collision counters
1280 	 */
1281 	ifp->if_collisions +=
1282 		bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
1283 		bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) +
1284 		bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
1285 		bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
1286 
1287 	/*
1288 	 * then clear the hardware counters.
1289 	 */
1290 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
1291 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
1292 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
1293 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
1294 
1295 	/*
1296 	 * Go through our Tx list and free mbufs for those
1297 	 * frames that have been transmitted.
1298 	 */
1299 	while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1300 		GEM_CDTXSYNC(sc, txs->txs_lastdesc,
1301 		    txs->txs_ndescs,
1302 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1303 
1304 #ifdef GEM_DEBUG
1305 		if (ifp->if_flags & IFF_DEBUG) {
1306 			int i;
1307 			printf("    txsoft %p transmit chain:\n", txs);
1308 			for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) {
1309 				printf("descriptor %d: ", i);
1310 				printf("gd_flags: 0x%016llx\t", (long long)
1311 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1312 				printf("gd_addr: 0x%016llx\n", (long long)
1313 					GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
1314 				if (i == txs->txs_lastdesc)
1315 					break;
1316 			}
1317 		}
1318 #endif
1319 
1320 		/*
1321 		 * In theory, we could harveast some descriptors before
1322 		 * the ring is empty, but that's a bit complicated.
1323 		 *
1324 		 * GEM_TX_COMPLETION points to the last descriptor
1325 		 * processed +1.
1326 		 */
1327 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
1328 		DPRINTF(sc,
1329 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
1330 				txs->txs_lastdesc, txlast));
1331 		CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, "
1332 		    "txs->txs_lastdesc = %d, txlast = %d",
1333 		    txs->txs_firstdesc, txs->txs_lastdesc, txlast);
1334 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
1335 			if ((txlast >= txs->txs_firstdesc) &&
1336 				(txlast <= txs->txs_lastdesc))
1337 				break;
1338 		} else {
1339 			/* Ick -- this command wraps */
1340 			if ((txlast >= txs->txs_firstdesc) ||
1341 				(txlast <= txs->txs_lastdesc))
1342 				break;
1343 		}
1344 
1345 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
1346 		CTR0(KTR_GEM, "gem_tint: releasing a desc");
1347 		STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1348 
1349 		sc->sc_txfree += txs->txs_ndescs;
1350 
1351 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
1352 		    BUS_DMASYNC_POSTWRITE);
1353 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
1354 		if (txs->txs_mbuf != NULL) {
1355 			m_freem(txs->txs_mbuf);
1356 			txs->txs_mbuf = NULL;
1357 		}
1358 
1359 		STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1360 
1361 		ifp->if_opackets++;
1362 	}
1363 
1364 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
1365 		"GEM_TX_DATA_PTR %llx "
1366 		"GEM_TX_COMPLETION %x\n",
1367 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
1368 		((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
1369 			GEM_TX_DATA_PTR_HI) << 32) |
1370 			     bus_space_read_4(sc->sc_bustag, sc->sc_h,
1371 			GEM_TX_DATA_PTR_LO),
1372 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)));
1373 	CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x "
1374 		"GEM_TX_DATA_PTR %llx "
1375 		"GEM_TX_COMPLETION %x",
1376 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE),
1377 		((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h,
1378 			GEM_TX_DATA_PTR_HI) << 32) |
1379 			     bus_space_read_4(sc->sc_bustag, sc->sc_h,
1380 			GEM_TX_DATA_PTR_LO),
1381 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION));
1382 
1383 	if (STAILQ_FIRST(&sc->sc_txdirtyq) == NULL)
1384 		ifp->if_timer = 0;
1385 
1386 
1387 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
1388 		device_get_name(sc->sc_dev), ifp->if_timer));
1389 	CTR2(KTR_GEM, "%s: gem_tint: watchdog %d",
1390 		device_get_name(sc->sc_dev), ifp->if_timer);
1391 
1392 	/* Freed some descriptors, so reset IFF_OACTIVE and restart. */
1393 	ifp->if_flags &= ~IFF_OACTIVE;
1394 	gem_start(ifp);
1395 }
1396 
1397 /*
1398  * Receive interrupt.
1399  */
1400 static void
1401 gem_rint(sc)
1402 	struct gem_softc *sc;
1403 {
1404 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1405 	bus_space_tag_t t = sc->sc_bustag;
1406 	bus_space_handle_t h = sc->sc_h;
1407 	struct ether_header *eh;
1408 	struct gem_rxsoft *rxs;
1409 	struct mbuf *m;
1410 	u_int64_t rxstat;
1411 	int i, len;
1412 
1413 	DPRINTF(sc, ("%s: gem_rint\n", device_get_name(sc->sc_dev)));
1414 	CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev));
1415 	/*
1416 	 * XXXX Read the lastrx only once at the top for speed.
1417 	 */
1418 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
1419 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1420 	CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d",
1421 	    sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION));
1422 	for (i = sc->sc_rxptr; i != bus_space_read_4(t, h, GEM_RX_COMPLETION);
1423 	     i = GEM_NEXTRX(i)) {
1424 		rxs = &sc->sc_rxsoft[i];
1425 
1426 		GEM_CDRXSYNC(sc, i,
1427 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1428 
1429 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
1430 
1431 		if (rxstat & GEM_RD_OWN) {
1432 			printf("gem_rint: completed descriptor "
1433 				"still owned %d\n", i);
1434 			/*
1435 			 * We have processed all of the receive buffers.
1436 			 */
1437 			break;
1438 		}
1439 
1440 		if (rxstat & GEM_RD_BAD_CRC) {
1441 			device_printf(sc->sc_dev, "receive error: CRC error\n");
1442 			GEM_INIT_RXDESC(sc, i);
1443 			continue;
1444 		}
1445 
1446 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap,
1447 		    BUS_DMASYNC_POSTREAD);
1448 #ifdef GEM_DEBUG
1449 		if (ifp->if_flags & IFF_DEBUG) {
1450 			printf("    rxsoft %p descriptor %d: ", rxs, i);
1451 			printf("gd_flags: 0x%016llx\t", (long long)
1452 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
1453 			printf("gd_addr: 0x%016llx\n", (long long)
1454 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
1455 		}
1456 #endif
1457 
1458 		/*
1459 		 * No errors; receive the packet.  Note the Gem
1460 		 * includes the CRC with every packet.
1461 		 */
1462 		len = GEM_RD_BUFLEN(rxstat);
1463 
1464 		/*
1465 		 * Allocate a new mbuf cluster.  If that fails, we are
1466 		 * out of memory, and must drop the packet and recycle
1467 		 * the buffer that's already attached to this descriptor.
1468 		 */
1469 		m = rxs->rxs_mbuf;
1470 		if (gem_add_rxbuf(sc, i) != 0) {
1471 			ifp->if_ierrors++;
1472 			GEM_INIT_RXDESC(sc, i);
1473 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap,
1474 			    BUS_DMASYNC_PREREAD);
1475 			continue;
1476 		}
1477 		m->m_data += 2; /* We're already off by two */
1478 
1479 		ifp->if_ipackets++;
1480 		eh = mtod(m, struct ether_header *);
1481 		m->m_pkthdr.rcvif = ifp;
1482 		m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN;
1483 		m_adj(m, sizeof(struct ether_header));
1484 
1485 		/* Pass it on. */
1486 		ether_input(ifp, eh, m);
1487 	}
1488 
1489 	/* Update the receive pointer. */
1490 	sc->sc_rxptr = i;
1491 	bus_space_write_4(t, h, GEM_RX_KICK, i);
1492 
1493 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
1494 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1495 	CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d",
1496 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION));
1497 
1498 }
1499 
1500 
1501 /*
1502  * gem_add_rxbuf:
1503  *
1504  *	Add a receive buffer to the indicated descriptor.
1505  */
1506 static int
1507 gem_add_rxbuf(sc, idx)
1508 	struct gem_softc *sc;
1509 	int idx;
1510 {
1511 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
1512 	struct mbuf *m;
1513 	int error;
1514 
1515 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1516 	if (m == NULL)
1517 		return (ENOBUFS);
1518 
1519 	MCLGET(m, M_DONTWAIT);
1520 	if ((m->m_flags & M_EXT) == 0) {
1521 		m_freem(m);
1522 		return (ENOBUFS);
1523 	}
1524 
1525 #ifdef GEM_DEBUG
1526 	/* bzero the packet to check dma */
1527 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
1528 #endif
1529 
1530 	if (rxs->rxs_mbuf != NULL)
1531 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
1532 
1533 	rxs->rxs_mbuf = m;
1534 
1535 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
1536 	    m->m_ext.ext_buf, m->m_ext.ext_size, gem_rxdma_callback, rxs,
1537 	    BUS_DMA_NOWAIT);
1538 	if (error != 0 || rxs->rxs_paddr == 0) {
1539 		device_printf(sc->sc_dev, "can't load rx DMA map %d, error = "
1540 		    "%d\n", idx, error);
1541 		panic("gem_add_rxbuf");	/* XXX */
1542 	}
1543 
1544 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD);
1545 
1546 	GEM_INIT_RXDESC(sc, idx);
1547 
1548 	return (0);
1549 }
1550 
1551 
1552 static void
1553 gem_eint(sc, status)
1554 	struct gem_softc *sc;
1555 	u_int status;
1556 {
1557 
1558 	if ((status & GEM_INTR_MIF) != 0) {
1559 		device_printf(sc->sc_dev, "XXXlink status changed\n");
1560 		return;
1561 	}
1562 
1563 	device_printf(sc->sc_dev, "status=%x\n", status);
1564 }
1565 
1566 
1567 void
1568 gem_intr(v)
1569 	void *v;
1570 {
1571 	struct gem_softc *sc = (struct gem_softc *)v;
1572 	bus_space_tag_t t = sc->sc_bustag;
1573 	bus_space_handle_t seb = sc->sc_h;
1574 	u_int32_t status;
1575 
1576 	status = bus_space_read_4(t, seb, GEM_STATUS);
1577 	DPRINTF(sc, ("%s: gem_intr: cplt %x, status %x\n",
1578 		device_get_name(sc->sc_dev), (status>>19),
1579 		(u_int)status));
1580 	CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x",
1581 		device_get_name(sc->sc_dev), (status>>19),
1582 		(u_int)status);
1583 
1584 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
1585 		gem_eint(sc, status);
1586 
1587 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0)
1588 		gem_tint(sc);
1589 
1590 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0)
1591 		gem_rint(sc);
1592 
1593 	/* We should eventually do more than just print out error stats. */
1594 	if (status & GEM_INTR_TX_MAC) {
1595 		int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS);
1596 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
1597 			printf("MAC tx fault, status %x\n", txstat);
1598 	}
1599 	if (status & GEM_INTR_RX_MAC) {
1600 		int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS);
1601 		if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
1602 			printf("MAC rx fault, status %x\n", rxstat);
1603 	}
1604 }
1605 
1606 
1607 static void
1608 gem_watchdog(ifp)
1609 	struct ifnet *ifp;
1610 {
1611 	struct gem_softc *sc = ifp->if_softc;
1612 
1613 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
1614 		"GEM_MAC_RX_CONFIG %x\n",
1615 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
1616 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
1617 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)));
1618 	CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
1619 		"GEM_MAC_RX_CONFIG %x",
1620 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG),
1621 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS),
1622 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG));
1623 	CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x "
1624 		"GEM_MAC_TX_CONFIG %x",
1625 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG),
1626 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_STATUS),
1627 		bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG));
1628 
1629 	device_printf(sc->sc_dev, "device timeout\n");
1630 	++ifp->if_oerrors;
1631 
1632 	/* Try to get more packets going. */
1633 	gem_start(ifp);
1634 }
1635 
1636 /*
1637  * Initialize the MII Management Interface
1638  */
1639 static void
1640 gem_mifinit(sc)
1641 	struct gem_softc *sc;
1642 {
1643 	bus_space_tag_t t = sc->sc_bustag;
1644 	bus_space_handle_t mif = sc->sc_h;
1645 
1646 	/* Configure the MIF in frame mode */
1647 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1648 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
1649 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
1650 }
1651 
1652 /*
1653  * MII interface
1654  *
1655  * The GEM MII interface supports at least three different operating modes:
1656  *
1657  * Bitbang mode is implemented using data, clock and output enable registers.
1658  *
1659  * Frame mode is implemented by loading a complete frame into the frame
1660  * register and polling the valid bit for completion.
1661  *
1662  * Polling mode uses the frame register but completion is indicated by
1663  * an interrupt.
1664  *
1665  */
1666 int
1667 gem_mii_readreg(dev, phy, reg)
1668 	device_t dev;
1669 	int phy, reg;
1670 {
1671 	struct gem_softc *sc = device_get_softc(dev);
1672 	bus_space_tag_t t = sc->sc_bustag;
1673 	bus_space_handle_t mif = sc->sc_h;
1674 	int n;
1675 	u_int32_t v;
1676 
1677 #ifdef GEM_DEBUG_PHY
1678 	printf("gem_mii_readreg: phy %d reg %d\n", phy, reg);
1679 #endif
1680 
1681 #if 0
1682 	/* Select the desired PHY in the MIF configuration register */
1683 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1684 	/* Clear PHY select bit */
1685 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
1686 	if (phy == GEM_PHYAD_EXTERNAL)
1687 		/* Set PHY select bit to get at external device */
1688 		v |= GEM_MIF_CONFIG_PHY_SEL;
1689 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1690 #endif
1691 
1692 	/* Construct the frame command */
1693 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
1694 		GEM_MIF_FRAME_READ;
1695 
1696 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1697 	for (n = 0; n < 100; n++) {
1698 		DELAY(1);
1699 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1700 		if (v & GEM_MIF_FRAME_TA0)
1701 			return (v & GEM_MIF_FRAME_DATA);
1702 	}
1703 
1704 	device_printf(sc->sc_dev, "mii_read timeout\n");
1705 	return (0);
1706 }
1707 
1708 int
1709 gem_mii_writereg(dev, phy, reg, val)
1710 	device_t dev;
1711 	int phy, reg, val;
1712 {
1713 	struct gem_softc *sc = device_get_softc(dev);
1714 	bus_space_tag_t t = sc->sc_bustag;
1715 	bus_space_handle_t mif = sc->sc_h;
1716 	int n;
1717 	u_int32_t v;
1718 
1719 #ifdef GEM_DEBUG_PHY
1720 	printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val);
1721 #endif
1722 
1723 #if 0
1724 	/* Select the desired PHY in the MIF configuration register */
1725 	v = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
1726 	/* Clear PHY select bit */
1727 	v &= ~GEM_MIF_CONFIG_PHY_SEL;
1728 	if (phy == GEM_PHYAD_EXTERNAL)
1729 		/* Set PHY select bit to get at external device */
1730 		v |= GEM_MIF_CONFIG_PHY_SEL;
1731 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, v);
1732 #endif
1733 	/* Construct the frame command */
1734 	v = GEM_MIF_FRAME_WRITE			|
1735 	    (phy << GEM_MIF_PHY_SHIFT)		|
1736 	    (reg << GEM_MIF_REG_SHIFT)		|
1737 	    (val & GEM_MIF_FRAME_DATA);
1738 
1739 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
1740 	for (n = 0; n < 100; n++) {
1741 		DELAY(1);
1742 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
1743 		if (v & GEM_MIF_FRAME_TA0)
1744 			return (1);
1745 	}
1746 
1747 	device_printf(sc->sc_dev, "mii_write timeout\n");
1748 	return (0);
1749 }
1750 
1751 void
1752 gem_mii_statchg(dev)
1753 	device_t dev;
1754 {
1755 	struct gem_softc *sc = device_get_softc(dev);
1756 #ifdef GEM_DEBUG
1757 	int instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media);
1758 #endif
1759 	bus_space_tag_t t = sc->sc_bustag;
1760 	bus_space_handle_t mac = sc->sc_h;
1761 	u_int32_t v;
1762 
1763 #ifdef GEM_DEBUG
1764 	if (sc->sc_debug)
1765 		printf("gem_mii_statchg: status change: phy = %d\n",
1766 			sc->sc_phys[instance]);
1767 #endif
1768 
1769 	/* Set tx full duplex options */
1770 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
1771 	DELAY(10000); /* reg must be cleared and delay before changing. */
1772 	v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT|
1773 		GEM_MAC_TX_ENABLE;
1774 	if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) {
1775 		v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS;
1776 	}
1777 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v);
1778 
1779 	/* XIF Configuration */
1780  /* We should really calculate all this rather than rely on defaults */
1781 	v = bus_space_read_4(t, mac, GEM_MAC_XIF_CONFIG);
1782 	v = GEM_MAC_XIF_LINK_LED;
1783 	v |= GEM_MAC_XIF_TX_MII_ENA;
1784 	/* If an external transceiver is connected, enable its MII drivers */
1785 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
1786 	if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
1787 		/* External MII needs echo disable if half duplex. */
1788 		if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0)
1789 			/* turn on full duplex LED */
1790 			v |= GEM_MAC_XIF_FDPLX_LED;
1791  			else
1792 	 			/* half duplex -- disable echo */
1793 		 		v |= GEM_MAC_XIF_ECHO_DISABL;
1794 	} else {
1795 		/* Internal MII needs buf enable */
1796 		v |= GEM_MAC_XIF_MII_BUF_ENA;
1797 	}
1798 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
1799 }
1800 
1801 int
1802 gem_mediachange(ifp)
1803 	struct ifnet *ifp;
1804 {
1805 	struct gem_softc *sc = ifp->if_softc;
1806 
1807 	/* XXX Add support for serial media. */
1808 
1809 	return (mii_mediachg(sc->sc_mii));
1810 }
1811 
1812 void
1813 gem_mediastatus(ifp, ifmr)
1814 	struct ifnet *ifp;
1815 	struct ifmediareq *ifmr;
1816 {
1817 	struct gem_softc *sc = ifp->if_softc;
1818 
1819 	if ((ifp->if_flags & IFF_UP) == 0)
1820 		return;
1821 
1822 	mii_pollstat(sc->sc_mii);
1823 	ifmr->ifm_active = sc->sc_mii->mii_media_active;
1824 	ifmr->ifm_status = sc->sc_mii->mii_media_status;
1825 }
1826 
1827 /*
1828  * Process an ioctl request.
1829  */
1830 static int
1831 gem_ioctl(ifp, cmd, data)
1832 	struct ifnet *ifp;
1833 	u_long cmd;
1834 	caddr_t data;
1835 {
1836 	struct gem_softc *sc = ifp->if_softc;
1837 	struct ifreq *ifr = (struct ifreq *)data;
1838 	int s, error = 0;
1839 
1840 	switch (cmd) {
1841 	case SIOCSIFADDR:
1842 	case SIOCGIFADDR:
1843 	case SIOCSIFMTU:
1844 		error = ether_ioctl(ifp, cmd, data);
1845 		break;
1846 	case SIOCSIFFLAGS:
1847 		if (ifp->if_flags & IFF_UP) {
1848 			if ((sc->sc_flags ^ ifp->if_flags) == IFF_PROMISC)
1849 				gem_setladrf(sc);
1850 			else
1851 				gem_init(sc);
1852 		} else {
1853 			if (ifp->if_flags & IFF_RUNNING)
1854 				gem_stop(ifp, 0);
1855 		}
1856 		sc->sc_flags = ifp->if_flags;
1857 		error = 0;
1858 		break;
1859 	case SIOCADDMULTI:
1860 	case SIOCDELMULTI:
1861 		gem_setladrf(sc);
1862 		error = 0;
1863 		break;
1864 	case SIOCGIFMEDIA:
1865 	case SIOCSIFMEDIA:
1866 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd);
1867 		break;
1868 	default:
1869 		error = ENOTTY;
1870 		break;
1871 	}
1872 
1873 	/* Try to get things going again */
1874 	if (ifp->if_flags & IFF_UP)
1875 		gem_start(ifp);
1876 	splx(s);
1877 	return (error);
1878 }
1879 
1880 /*
1881  * Set up the logical address filter.
1882  */
1883 static void
1884 gem_setladrf(sc)
1885 	struct gem_softc *sc;
1886 {
1887 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1888 	struct ifmultiaddr *inm;
1889 	struct sockaddr_dl *sdl;
1890 	bus_space_tag_t t = sc->sc_bustag;
1891 	bus_space_handle_t h = sc->sc_h;
1892 	u_char *cp;
1893 	u_int32_t crc;
1894 	u_int32_t hash[16];
1895 	u_int32_t v;
1896 	int len;
1897 
1898 	/* Clear hash table */
1899 	memset(hash, 0, sizeof(hash));
1900 
1901 	/* Get current RX configuration */
1902 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
1903 
1904 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
1905 		/* Turn on promiscuous mode; turn off the hash filter */
1906 		v |= GEM_MAC_RX_PROMISCUOUS;
1907 		v &= ~GEM_MAC_RX_HASH_FILTER;
1908 		;
1909 		goto chipit;
1910 	}
1911 	if ((ifp->if_flags & IFF_ALLMULTI) != 0) {
1912 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1913 		ifp->if_flags |= IFF_ALLMULTI;
1914 		goto chipit;
1915 	}
1916 
1917 	/* Turn off promiscuous mode; turn on the hash filter */
1918 	v &= ~GEM_MAC_RX_PROMISCUOUS;
1919 	v |= GEM_MAC_RX_HASH_FILTER;
1920 
1921 	/*
1922 	 * Set up multicast address filter by passing all multicast addresses
1923 	 * through a crc generator, and then using the high order 6 bits as an
1924 	 * index into the 256 bit logical address filter.  The high order bit
1925 	 * selects the word, while the rest of the bits select the bit within
1926 	 * the word.
1927 	 */
1928 
1929 	TAILQ_FOREACH(inm, &sc->sc_arpcom.ac_if.if_multiaddrs, ifma_link) {
1930 		if (inm->ifma_addr->sa_family != AF_LINK)
1931 			continue;
1932 		sdl = (struct sockaddr_dl *)inm->ifma_addr;
1933 		cp = LLADDR(sdl);
1934 		crc = 0xffffffff;
1935 		for (len = sdl->sdl_alen; --len >= 0;) {
1936 			int octet = *cp++;
1937 			int i;
1938 
1939 #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
1940 			for (i = 0; i < 8; i++) {
1941 				if ((crc & 1) ^ (octet & 1)) {
1942 					crc >>= 1;
1943 					crc ^= MC_POLY_LE;
1944 				} else {
1945 					crc >>= 1;
1946 				}
1947 				octet >>= 1;
1948 			}
1949 		}
1950 		/* Just want the 8 most significant bits. */
1951 		crc >>= 24;
1952 
1953 		/* Set the corresponding bit in the filter. */
1954 		hash[crc >> 4] |= 1 << (crc & 0xf);
1955 	}
1956 
1957 chipit:
1958 	/* Now load the hash table into the chip */
1959 	bus_space_write_4(t, h, GEM_MAC_HASH0, hash[0]);
1960 	bus_space_write_4(t, h, GEM_MAC_HASH1, hash[1]);
1961 	bus_space_write_4(t, h, GEM_MAC_HASH2, hash[2]);
1962 	bus_space_write_4(t, h, GEM_MAC_HASH3, hash[3]);
1963 	bus_space_write_4(t, h, GEM_MAC_HASH4, hash[4]);
1964 	bus_space_write_4(t, h, GEM_MAC_HASH5, hash[5]);
1965 	bus_space_write_4(t, h, GEM_MAC_HASH6, hash[6]);
1966 	bus_space_write_4(t, h, GEM_MAC_HASH7, hash[7]);
1967 	bus_space_write_4(t, h, GEM_MAC_HASH8, hash[8]);
1968 	bus_space_write_4(t, h, GEM_MAC_HASH9, hash[9]);
1969 	bus_space_write_4(t, h, GEM_MAC_HASH10, hash[10]);
1970 	bus_space_write_4(t, h, GEM_MAC_HASH11, hash[11]);
1971 	bus_space_write_4(t, h, GEM_MAC_HASH12, hash[12]);
1972 	bus_space_write_4(t, h, GEM_MAC_HASH13, hash[13]);
1973 	bus_space_write_4(t, h, GEM_MAC_HASH14, hash[14]);
1974 	bus_space_write_4(t, h, GEM_MAC_HASH15, hash[15]);
1975 
1976 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
1977 }
1978 
1979 #if notyet
1980 
1981 /*
1982  * gem_power:
1983  *
1984  *	Power management (suspend/resume) hook.
1985  */
1986 void
1987 static gem_power(why, arg)
1988 	int why;
1989 	void *arg;
1990 {
1991 	struct gem_softc *sc = arg;
1992 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1993 	int s;
1994 
1995 	s = splnet();
1996 	switch (why) {
1997 	case PWR_SUSPEND:
1998 	case PWR_STANDBY:
1999 		gem_stop(ifp, 1);
2000 		if (sc->sc_power != NULL)
2001 			(*sc->sc_power)(sc, why);
2002 		break;
2003 	case PWR_RESUME:
2004 		if (ifp->if_flags & IFF_UP) {
2005 			if (sc->sc_power != NULL)
2006 				(*sc->sc_power)(sc, why);
2007 			gem_init(ifp);
2008 		}
2009 		break;
2010 	case PWR_SOFTSUSPEND:
2011 	case PWR_SOFTSTANDBY:
2012 	case PWR_SOFTRESUME:
2013 		break;
2014 	}
2015 	splx(s);
2016 }
2017 #endif
2018