xref: /netbsd-src/sys/dev/ic/gem.c (revision b78992537496bc71ee3d761f9fe0be0fc0a9a001)
1 /*	$NetBSD: gem.c,v 1.78 2008/09/15 19:50:28 jdc Exp $ */
2 
3 /*
4  *
5  * Copyright (C) 2001 Eduardo Horvath.
6  * Copyright (c) 2001-2003 Thomas Moestl
7  * All rights reserved.
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 /*
34  * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
35  * See `GEM Gigabit Ethernet ASIC Specification'
36  *   http://www.sun.com/processors/manuals/ge.pdf
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: gem.c,v 1.78 2008/09/15 19:50:28 jdc Exp $");
41 
42 #include "opt_inet.h"
43 #include "bpfilter.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/callout.h>
48 #include <sys/mbuf.h>
49 #include <sys/syslog.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/errno.h>
55 #include <sys/device.h>
56 
57 #include <machine/endian.h>
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <net/if.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_ether.h>
65 
66 #ifdef INET
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/tcp.h>
72 #include <netinet/udp.h>
73 #endif
74 
75 #if NBPFILTER > 0
76 #include <net/bpf.h>
77 #endif
78 
79 #include <sys/bus.h>
80 #include <sys/intr.h>
81 
82 #include <dev/mii/mii.h>
83 #include <dev/mii/miivar.h>
84 #include <dev/mii/mii_bitbang.h>
85 
86 #include <dev/ic/gemreg.h>
87 #include <dev/ic/gemvar.h>
88 
89 #define TRIES	10000
90 
91 static void	gem_start(struct ifnet *);
92 static void	gem_stop(struct ifnet *, int);
93 int		gem_ioctl(struct ifnet *, u_long, void *);
94 void		gem_tick(void *);
95 void		gem_watchdog(struct ifnet *);
96 void		gem_shutdown(void *);
97 void		gem_pcs_start(struct gem_softc *sc);
98 void		gem_pcs_stop(struct gem_softc *sc, int);
99 int		gem_init(struct ifnet *);
100 void		gem_init_regs(struct gem_softc *sc);
101 static int	gem_ringsize(int sz);
102 static int	gem_meminit(struct gem_softc *);
103 void		gem_mifinit(struct gem_softc *);
104 static int	gem_bitwait(struct gem_softc *sc, bus_space_handle_t, int,
105 		    u_int32_t, u_int32_t);
106 void		gem_reset(struct gem_softc *);
107 int		gem_reset_rx(struct gem_softc *sc);
108 static void	gem_reset_rxdma(struct gem_softc *sc);
109 static void	gem_rx_common(struct gem_softc *sc);
110 int		gem_reset_tx(struct gem_softc *sc);
111 int		gem_disable_rx(struct gem_softc *sc);
112 int		gem_disable_tx(struct gem_softc *sc);
113 static void	gem_rxdrain(struct gem_softc *sc);
114 int		gem_add_rxbuf(struct gem_softc *sc, int idx);
115 void		gem_setladrf(struct gem_softc *);
116 
117 /* MII methods & callbacks */
118 static int	gem_mii_readreg(struct device *, int, int);
119 static void	gem_mii_writereg(struct device *, int, int, int);
120 static void	gem_mii_statchg(struct device *);
121 
122 void		gem_statuschange(struct gem_softc *);
123 
124 int		gem_ser_mediachange(struct ifnet *);
125 void		gem_ser_mediastatus(struct ifnet *, struct ifmediareq *);
126 
127 struct mbuf	*gem_get(struct gem_softc *, int, int);
128 int		gem_put(struct gem_softc *, int, struct mbuf *);
129 void		gem_read(struct gem_softc *, int, int);
130 int		gem_pint(struct gem_softc *);
131 int		gem_eint(struct gem_softc *, u_int);
132 int		gem_rint(struct gem_softc *);
133 int		gem_tint(struct gem_softc *);
134 void		gem_power(int, void *);
135 
136 #ifdef GEM_DEBUG
137 static void gem_txsoft_print(const struct gem_softc *, int, int);
138 #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
139 				printf x
140 #else
141 #define	DPRINTF(sc, x)	/* nothing */
142 #endif
143 
144 #define ETHER_MIN_TX (ETHERMIN + sizeof(struct ether_header))
145 
146 
147 /*
148  * gem_attach:
149  *
150  *	Attach a Gem interface to the system.
151  */
152 void
153 gem_attach(sc, enaddr)
154 	struct gem_softc *sc;
155 	const uint8_t *enaddr;
156 {
157 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
158 	struct mii_data *mii = &sc->sc_mii;
159 	bus_space_tag_t t = sc->sc_bustag;
160 	bus_space_handle_t h = sc->sc_h1;
161 	struct ifmedia_entry *ifm;
162 	int i, error;
163 	u_int32_t v;
164 	char *nullbuf;
165 
166 	/* Make sure the chip is stopped. */
167 	ifp->if_softc = sc;
168 	gem_reset(sc);
169 
170 	/*
171 	 * Allocate the control data structures, and create and load the
172 	 * DMA map for it. gem_control_data is 9216 bytes, we have space for
173 	 * the padding buffer in the bus_dmamem_alloc()'d memory.
174 	 */
175 	if ((error = bus_dmamem_alloc(sc->sc_dmatag,
176 	    sizeof(struct gem_control_data) + ETHER_MIN_TX, PAGE_SIZE,
177 	    0, &sc->sc_cdseg, 1, &sc->sc_cdnseg, 0)) != 0) {
178 		aprint_error_dev(&sc->sc_dev,
179 		   "unable to allocate control data, error = %d\n",
180 		    error);
181 		goto fail_0;
182 	}
183 
184 	/* XXX should map this in with correct endianness */
185 	if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
186 	    sizeof(struct gem_control_data), (void **)&sc->sc_control_data,
187 	    BUS_DMA_COHERENT)) != 0) {
188 		aprint_error_dev(&sc->sc_dev, "unable to map control data, error = %d\n",
189 		    error);
190 		goto fail_1;
191 	}
192 
193 	nullbuf =
194 	    (char *)sc->sc_control_data + sizeof(struct gem_control_data);
195 
196 	if ((error = bus_dmamap_create(sc->sc_dmatag,
197 	    sizeof(struct gem_control_data), 1,
198 	    sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
199 		aprint_error_dev(&sc->sc_dev, "unable to create control data DMA map, "
200 		    "error = %d\n", error);
201 		goto fail_2;
202 	}
203 
204 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
205 	    sc->sc_control_data, sizeof(struct gem_control_data), NULL,
206 	    0)) != 0) {
207 		aprint_error_dev(&sc->sc_dev,
208 		    "unable to load control data DMA map, error = %d\n",
209 		    error);
210 		goto fail_3;
211 	}
212 
213 	memset(nullbuf, 0, ETHER_MIN_TX);
214 	if ((error = bus_dmamap_create(sc->sc_dmatag,
215 	    ETHER_MIN_TX, 1, ETHER_MIN_TX, 0, 0, &sc->sc_nulldmamap)) != 0) {
216 		aprint_error_dev(&sc->sc_dev, "unable to create padding DMA map, "
217 		    "error = %d\n", error);
218 		goto fail_4;
219 	}
220 
221 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_nulldmamap,
222 	    nullbuf, ETHER_MIN_TX, NULL, 0)) != 0) {
223 		aprint_error_dev(&sc->sc_dev,
224 		    "unable to load padding DMA map, error = %d\n",
225 		    error);
226 		goto fail_5;
227 	}
228 
229 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_nulldmamap, 0, ETHER_MIN_TX,
230 	    BUS_DMASYNC_PREWRITE);
231 
232 	/*
233 	 * Initialize the transmit job descriptors.
234 	 */
235 	SIMPLEQ_INIT(&sc->sc_txfreeq);
236 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
237 
238 	/*
239 	 * Create the transmit buffer DMA maps.
240 	 */
241 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
242 		struct gem_txsoft *txs;
243 
244 		txs = &sc->sc_txsoft[i];
245 		txs->txs_mbuf = NULL;
246 		if ((error = bus_dmamap_create(sc->sc_dmatag,
247 		    ETHER_MAX_LEN_JUMBO, GEM_NTXSEGS,
248 		    ETHER_MAX_LEN_JUMBO, 0, 0,
249 		    &txs->txs_dmamap)) != 0) {
250 			aprint_error_dev(&sc->sc_dev, "unable to create tx DMA map %d, "
251 			    "error = %d\n", i, error);
252 			goto fail_6;
253 		}
254 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
255 	}
256 
257 	/*
258 	 * Create the receive buffer DMA maps.
259 	 */
260 	for (i = 0; i < GEM_NRXDESC; i++) {
261 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
262 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
263 			aprint_error_dev(&sc->sc_dev, "unable to create rx DMA map %d, "
264 			    "error = %d\n", i, error);
265 			goto fail_7;
266 		}
267 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
268 	}
269 
270 	/* Initialize ifmedia structures and MII info */
271 	mii->mii_ifp = ifp;
272 	mii->mii_readreg = gem_mii_readreg;
273 	mii->mii_writereg = gem_mii_writereg;
274 	mii->mii_statchg = gem_mii_statchg;
275 
276 	sc->sc_ethercom.ec_mii = mii;
277 
278 	/*
279 	 * Initialization based  on `GEM Gigabit Ethernet ASIC Specification'
280 	 * Section 3.2.1 `Initialization Sequence'.
281 	 * However, we can't assume SERDES or Serialink if neither
282 	 * GEM_MIF_CONFIG_MDI0 nor GEM_MIF_CONFIG_MDI1 are set
283 	 * being set, as both are set on Sun X1141A (with SERDES).  So,
284 	 * we rely on our bus attachment setting GEM_SERDES or GEM_SERIAL.
285 	 * Also, for Apple variants with 2 PHY's, we prefer the external
286 	 * PHY over the internal PHY.
287 	 */
288 	gem_mifinit(sc);
289 
290 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0) {
291 		ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
292 		    ether_mediastatus);
293 		mii_attach(&sc->sc_dev, mii, 0xffffffff,
294 		    MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
295 		if (LIST_EMPTY(&mii->mii_phys)) {
296 				/* No PHY attached */
297 				aprint_error_dev(&sc->sc_dev, "PHY probe failed\n");
298 				goto fail_7;
299 		} else {
300 			struct mii_softc *child;
301 
302 			/*
303 			 * Walk along the list of attached MII devices and
304 			 * establish an `MII instance' to `PHY number'
305 			 * mapping.
306 			 */
307 			LIST_FOREACH(child, &mii->mii_phys, mii_list) {
308 				/*
309 				 * Note: we support just one PHY: the internal
310 				 * or external MII is already selected for us
311 				 * by the GEM_MIF_CONFIG  register.
312 				 */
313 				if (child->mii_phy > 1 || child->mii_inst > 0) {
314 					aprint_error_dev(&sc->sc_dev,
315 					    "cannot accommodate MII device"
316 					    " %s at PHY %d, instance %d\n",
317 					       device_xname(child->mii_dev),
318 					       child->mii_phy, child->mii_inst);
319 					continue;
320 				}
321 				sc->sc_phys[child->mii_inst] = child->mii_phy;
322 			}
323 
324 			/*
325 			 * Now select and activate the PHY we will use.
326 			 *
327 			 * The order of preference is External (MDI1),
328 			 * then Internal (MDI0),
329 			 */
330 			if (sc->sc_phys[1]) {
331 #ifdef GEM_DEBUG
332 				aprint_debug_dev(&sc->sc_dev, "using external PHY\n");
333 #endif
334 				sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
335 			} else {
336 #ifdef GEM_DEBUG
337 				aprint_debug_dev(&sc->sc_dev, "using internal PHY\n");
338 				sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
339 #endif
340 			}
341 			bus_space_write_4(t, h, GEM_MIF_CONFIG,
342 			    sc->sc_mif_config);
343 			if (sc->sc_variant != GEM_SUN_ERI)
344 				bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
345 				    GEM_MII_DATAPATH_MII);
346 
347 			/*
348 			 * XXX - we can really do the following ONLY if the
349 			 * PHY indeed has the auto negotiation capability!!
350 			 */
351 			ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
352 		}
353 	} else {
354 		ifmedia_init(&mii->mii_media, IFM_IMASK, gem_ser_mediachange,
355 		    gem_ser_mediastatus);
356 		/* SERDES or Serialink */
357 		if (sc->sc_flags & GEM_SERDES) {
358 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
359 			    GEM_MII_DATAPATH_SERDES);
360 		} else {
361 			sc->sc_flags |= GEM_SERIAL;
362 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
363 			    GEM_MII_DATAPATH_SERIAL);
364 		}
365 
366 		aprint_normal_dev(&sc->sc_dev, "using external PCS %s: ",
367 		    sc->sc_flags & GEM_SERDES ? "SERDES" : "Serialink");
368 
369 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO, 0, NULL);
370 		/* Check for FDX and HDX capabilities */
371 		sc->sc_mii_anar = bus_space_read_4(t, h, GEM_MII_ANAR);
372 		if (sc->sc_mii_anar & GEM_MII_ANEG_FUL_DUPLX) {
373 			ifmedia_add(&sc->sc_mii.mii_media,
374 			    IFM_ETHER|IFM_1000_SX|IFM_MANUAL|IFM_FDX, 0, NULL);
375 			aprint_normal("1000baseSX-FDX, ");
376 		}
377 		if (sc->sc_mii_anar & GEM_MII_ANEG_HLF_DUPLX) {
378 			ifmedia_add(&sc->sc_mii.mii_media,
379 			    IFM_ETHER|IFM_1000_SX|IFM_MANUAL|IFM_HDX, 0, NULL);
380 			aprint_normal("1000baseSX-HDX, ");
381 		}
382 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
383 		sc->sc_mii_media = IFM_AUTO;
384 		aprint_normal("auto\n");
385 
386 		gem_pcs_stop(sc, 1);
387 	}
388 
389 	/*
390 	 * From this point forward, the attachment cannot fail.  A failure
391 	 * before this point releases all resources that may have been
392 	 * allocated.
393 	 */
394 
395 	/* Announce ourselves. */
396 	aprint_normal_dev(&sc->sc_dev, "Ethernet address %s",
397 	    ether_sprintf(enaddr));
398 
399 	/* Get RX FIFO size */
400 	sc->sc_rxfifosize = 64 *
401 	    bus_space_read_4(t, h, GEM_RX_FIFO_SIZE);
402 	aprint_normal(", %uKB RX fifo", sc->sc_rxfifosize / 1024);
403 
404 	/* Get TX FIFO size */
405 	v = bus_space_read_4(t, h, GEM_TX_FIFO_SIZE);
406 	aprint_normal(", %uKB TX fifo\n", v / 16);
407 
408 	/* Initialize ifnet structure. */
409 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
410 	ifp->if_softc = sc;
411 	ifp->if_flags =
412 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
413 	sc->sc_if_flags = ifp->if_flags;
414 	/*
415 	 * The GEM hardware supports basic TCP checksum offloading only.
416 	 * Several (all?) revisions (Sun rev. 01 and Apple rev. 00 and 80)
417 	 * have bugs in the receive checksum, so don't enable it for now.
418 	if ((GEM_IS_SUN(sc) && sc->sc_chiprev != 1) ||
419 	    (GEM_IS_APPLE(sc) &&
420 	    (sc->sc_chiprev != 0 && sc->sc_chiprev != 0x80)))
421 		ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Rx;
422 	*/
423 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Tx;
424 	ifp->if_start = gem_start;
425 	ifp->if_ioctl = gem_ioctl;
426 	ifp->if_watchdog = gem_watchdog;
427 	ifp->if_stop = gem_stop;
428 	ifp->if_init = gem_init;
429 	IFQ_SET_READY(&ifp->if_snd);
430 
431 	/*
432 	 * If we support GigE media, we support jumbo frames too.
433 	 * Unless we are Apple.
434 	 */
435 	TAILQ_FOREACH(ifm, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
436 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_T ||
437 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_SX ||
438 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_LX ||
439 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_1000_CX) {
440 			if (!GEM_IS_APPLE(sc))
441 				sc->sc_ethercom.ec_capabilities
442 				    |= ETHERCAP_JUMBO_MTU;
443 			sc->sc_flags |= GEM_GIGABIT;
444 			break;
445 		}
446 	}
447 
448 	/* claim 802.1q capability */
449 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
450 
451 	/* Attach the interface. */
452 	if_attach(ifp);
453 	ether_ifattach(ifp, enaddr);
454 
455 	sc->sc_sh = shutdownhook_establish(gem_shutdown, sc);
456 	if (sc->sc_sh == NULL)
457 		panic("gem_config: can't establish shutdownhook");
458 
459 #if NRND > 0
460 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
461 			  RND_TYPE_NET, 0);
462 #endif
463 
464 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
465 	    NULL, device_xname(&sc->sc_dev), "interrupts");
466 #ifdef GEM_COUNTERS
467 	evcnt_attach_dynamic(&sc->sc_ev_txint, EVCNT_TYPE_INTR,
468 	    &sc->sc_ev_intr, device_xname(&sc->sc_dev), "tx interrupts");
469 	evcnt_attach_dynamic(&sc->sc_ev_rxint, EVCNT_TYPE_INTR,
470 	    &sc->sc_ev_intr, device_xname(&sc->sc_dev), "rx interrupts");
471 	evcnt_attach_dynamic(&sc->sc_ev_rxfull, EVCNT_TYPE_INTR,
472 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx ring full");
473 	evcnt_attach_dynamic(&sc->sc_ev_rxnobuf, EVCNT_TYPE_INTR,
474 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx malloc failure");
475 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[0], EVCNT_TYPE_INTR,
476 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx 0desc");
477 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[1], EVCNT_TYPE_INTR,
478 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx 1desc");
479 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[2], EVCNT_TYPE_INTR,
480 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx 2desc");
481 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[3], EVCNT_TYPE_INTR,
482 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx 3desc");
483 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[4], EVCNT_TYPE_INTR,
484 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx >3desc");
485 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[5], EVCNT_TYPE_INTR,
486 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx >7desc");
487 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[6], EVCNT_TYPE_INTR,
488 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx >15desc");
489 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[7], EVCNT_TYPE_INTR,
490 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx >31desc");
491 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[8], EVCNT_TYPE_INTR,
492 	    &sc->sc_ev_rxint, device_xname(&sc->sc_dev), "rx >63desc");
493 #endif
494 
495 #if notyet
496 	/*
497 	 * Add a suspend hook to make sure we come back up after a
498 	 * resume.
499 	 */
500 	sc->sc_powerhook = powerhook_establish(device_xname(&sc->sc_dev),
501 	    gem_power, sc);
502 	if (sc->sc_powerhook == NULL)
503 		aprint_error_dev(&sc->sc_dev, "WARNING: unable to establish power hook\n");
504 #endif
505 
506 	callout_init(&sc->sc_tick_ch, 0);
507 	return;
508 
509 	/*
510 	 * Free any resources we've allocated during the failed attach
511 	 * attempt.  Do this in reverse order and fall through.
512 	 */
513  fail_7:
514 	for (i = 0; i < GEM_NRXDESC; i++) {
515 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
516 			bus_dmamap_destroy(sc->sc_dmatag,
517 			    sc->sc_rxsoft[i].rxs_dmamap);
518 	}
519  fail_6:
520 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
521 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
522 			bus_dmamap_destroy(sc->sc_dmatag,
523 			    sc->sc_txsoft[i].txs_dmamap);
524 	}
525 	bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
526  fail_5:
527 	bus_dmamap_destroy(sc->sc_dmatag, sc->sc_nulldmamap);
528  fail_4:
529 	bus_dmamem_unmap(sc->sc_dmatag, (void *)nullbuf, ETHER_MIN_TX);
530  fail_3:
531 	bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
532  fail_2:
533 	bus_dmamem_unmap(sc->sc_dmatag, (void *)sc->sc_control_data,
534 	    sizeof(struct gem_control_data));
535  fail_1:
536 	bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
537  fail_0:
538 	return;
539 }
540 
541 
542 void
543 gem_tick(arg)
544 	void *arg;
545 {
546 	struct gem_softc *sc = arg;
547 	int s;
548 
549 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0) {
550 		/*
551 		 * We have to reset everything if we failed to get a
552 		 * PCS interrupt.  Restarting the callout is handled
553 		 * in gem_pcs_start().
554 		 */
555 		gem_init(&sc->sc_ethercom.ec_if);
556 	} else {
557 		s = splnet();
558 		mii_tick(&sc->sc_mii);
559 		splx(s);
560 		callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
561 	}
562 }
563 
564 static int
565 gem_bitwait(sc, h, r, clr, set)
566 	struct gem_softc *sc;
567 	bus_space_handle_t h;
568 	int r;
569 	u_int32_t clr;
570 	u_int32_t set;
571 {
572 	int i;
573 	u_int32_t reg;
574 
575 	for (i = TRIES; i--; DELAY(100)) {
576 		reg = bus_space_read_4(sc->sc_bustag, h, r);
577 		if ((reg & clr) == 0 && (reg & set) == set)
578 			return (1);
579 	}
580 	return (0);
581 }
582 
583 void
584 gem_reset(sc)
585 	struct gem_softc *sc;
586 {
587 	bus_space_tag_t t = sc->sc_bustag;
588 	bus_space_handle_t h = sc->sc_h2;
589 	int s;
590 
591 	s = splnet();
592 	DPRINTF(sc, ("%s: gem_reset\n", device_xname(&sc->sc_dev)));
593 	gem_reset_rx(sc);
594 	gem_reset_tx(sc);
595 
596 	/* Do a full reset */
597 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX|GEM_RESET_TX);
598 	if (!gem_bitwait(sc, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
599 		aprint_error_dev(&sc->sc_dev, "cannot reset device\n");
600 	splx(s);
601 }
602 
603 
604 /*
605  * gem_rxdrain:
606  *
607  *	Drain the receive queue.
608  */
609 static void
610 gem_rxdrain(struct gem_softc *sc)
611 {
612 	struct gem_rxsoft *rxs;
613 	int i;
614 
615 	for (i = 0; i < GEM_NRXDESC; i++) {
616 		rxs = &sc->sc_rxsoft[i];
617 		if (rxs->rxs_mbuf != NULL) {
618 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
619 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
620 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
621 			m_freem(rxs->rxs_mbuf);
622 			rxs->rxs_mbuf = NULL;
623 		}
624 	}
625 }
626 
627 /*
628  * Reset the whole thing.
629  */
630 static void
631 gem_stop(struct ifnet *ifp, int disable)
632 {
633 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
634 	struct gem_txsoft *txs;
635 
636 	DPRINTF(sc, ("%s: gem_stop\n", device_xname(&sc->sc_dev)));
637 
638 	callout_stop(&sc->sc_tick_ch);
639 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
640 		gem_pcs_stop(sc, disable);
641 	else
642 		mii_down(&sc->sc_mii);
643 
644 	/* XXX - Should we reset these instead? */
645 	gem_disable_tx(sc);
646 	gem_disable_rx(sc);
647 
648 	/*
649 	 * Release any queued transmit buffers.
650 	 */
651 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
652 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
653 		if (txs->txs_mbuf != NULL) {
654 			bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 0,
655 			    txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
656 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
657 			m_freem(txs->txs_mbuf);
658 			txs->txs_mbuf = NULL;
659 		}
660 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
661 	}
662 
663 	/*
664 	 * Mark the interface down and cancel the watchdog timer.
665 	 */
666 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
667 	sc->sc_if_flags = ifp->if_flags;
668 	ifp->if_timer = 0;
669 
670 	if (disable)
671 		gem_rxdrain(sc);
672 }
673 
674 
675 /*
676  * Reset the receiver
677  */
678 int
679 gem_reset_rx(struct gem_softc *sc)
680 {
681 	bus_space_tag_t t = sc->sc_bustag;
682 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
683 
684 	/*
685 	 * Resetting while DMA is in progress can cause a bus hang, so we
686 	 * disable DMA first.
687 	 */
688 	gem_disable_rx(sc);
689 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
690 	bus_space_barrier(t, h, GEM_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
691 	/* Wait till it finishes */
692 	if (!gem_bitwait(sc, h, GEM_RX_CONFIG, 1, 0))
693 		aprint_error_dev(&sc->sc_dev, "cannot disable read dma\n");
694 
695 	/* Finally, reset the ERX */
696 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_RX);
697 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
698 	/* Wait till it finishes */
699 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_RX, 0)) {
700 		aprint_error_dev(&sc->sc_dev, "cannot reset receiver\n");
701 		return (1);
702 	}
703 	return (0);
704 }
705 
706 
707 /*
708  * Reset the receiver DMA engine.
709  *
710  * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW
711  * etc in order to reset the receiver DMA engine only and not do a full
712  * reset which amongst others also downs the link and clears the FIFOs.
713  */
714 static void
715 gem_reset_rxdma(struct gem_softc *sc)
716 {
717 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
718 	bus_space_tag_t t = sc->sc_bustag;
719 	bus_space_handle_t h = sc->sc_h1;
720 	int i;
721 
722 	if (gem_reset_rx(sc) != 0) {
723 		gem_init(ifp);
724 		return;
725 	}
726 	for (i = 0; i < GEM_NRXDESC; i++)
727 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL)
728 			GEM_UPDATE_RXDESC(sc, i);
729 	sc->sc_rxptr = 0;
730 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
731 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
732 
733 	/* Reprogram Descriptor Ring Base Addresses */
734 	/* NOTE: we use only 32-bit DMA addresses here. */
735 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
736 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
737 
738 	/* Redo ERX Configuration */
739 	gem_rx_common(sc);
740 
741 	/* Give the reciever a swift kick */
742 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC - 4);
743 }
744 
745 /*
746  * Common RX configuration for gem_init() and gem_reset_rxdma().
747  */
748 static void
749 gem_rx_common(struct gem_softc *sc)
750 {
751 	bus_space_tag_t t = sc->sc_bustag;
752 	bus_space_handle_t h = sc->sc_h1;
753 	u_int32_t v;
754 
755 	/* Encode Receive Descriptor ring size: four possible values */
756 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
757 
758 	/* Set receive h/w checksum offset */
759 #ifdef INET
760 	v |= (ETHER_HDR_LEN + sizeof(struct ip) +
761 	    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
762 	    ETHER_VLAN_ENCAP_LEN : 0)) << GEM_RX_CONFIG_CXM_START_SHFT;
763 #endif
764 
765 	/* Enable RX DMA */
766 	bus_space_write_4(t, h, GEM_RX_CONFIG,
767 	    v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
768 	    (2 << GEM_RX_CONFIG_FBOFF_SHFT) | GEM_RX_CONFIG_RXDMA_EN);
769 
770 	/*
771 	 * The following value is for an OFF Threshold of about 3/4 full
772 	 * and an ON Threshold of 1/4 full.
773 	 */
774 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
775 	    (3 * sc->sc_rxfifosize / 256) |
776 	    ((sc->sc_rxfifosize / 256) << 12));
777 	bus_space_write_4(t, h, GEM_RX_BLANKING,
778 	    (6 << GEM_RX_BLANKING_TIME_SHIFT) | 6);
779 }
780 
781 /*
782  * Reset the transmitter
783  */
784 int
785 gem_reset_tx(struct gem_softc *sc)
786 {
787 	bus_space_tag_t t = sc->sc_bustag;
788 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
789 
790 	/*
791 	 * Resetting while DMA is in progress can cause a bus hang, so we
792 	 * disable DMA first.
793 	 */
794 	gem_disable_tx(sc);
795 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
796 	bus_space_barrier(t, h, GEM_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
797 	/* Wait till it finishes */
798 	if (!gem_bitwait(sc, h, GEM_TX_CONFIG, 1, 0))
799 		aprint_error_dev(&sc->sc_dev, "cannot disable read dma\n");
800 	/* Wait 5ms extra. */
801 	delay(5000);
802 
803 	/* Finally, reset the ETX */
804 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_TX);
805 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
806 	/* Wait till it finishes */
807 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_TX, 0)) {
808 		aprint_error_dev(&sc->sc_dev, "cannot reset receiver\n");
809 		return (1);
810 	}
811 	return (0);
812 }
813 
814 /*
815  * disable receiver.
816  */
817 int
818 gem_disable_rx(struct gem_softc *sc)
819 {
820 	bus_space_tag_t t = sc->sc_bustag;
821 	bus_space_handle_t h = sc->sc_h1;
822 	u_int32_t cfg;
823 
824 	/* Flip the enable bit */
825 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
826 	cfg &= ~GEM_MAC_RX_ENABLE;
827 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
828 	bus_space_barrier(t, h, GEM_MAC_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
829 	/* Wait for it to finish */
830 	return (gem_bitwait(sc, h, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
831 }
832 
833 /*
834  * disable transmitter.
835  */
836 int
837 gem_disable_tx(struct gem_softc *sc)
838 {
839 	bus_space_tag_t t = sc->sc_bustag;
840 	bus_space_handle_t h = sc->sc_h1;
841 	u_int32_t cfg;
842 
843 	/* Flip the enable bit */
844 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
845 	cfg &= ~GEM_MAC_TX_ENABLE;
846 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
847 	bus_space_barrier(t, h, GEM_MAC_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
848 	/* Wait for it to finish */
849 	return (gem_bitwait(sc, h, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
850 }
851 
852 /*
853  * Initialize interface.
854  */
855 int
856 gem_meminit(struct gem_softc *sc)
857 {
858 	struct gem_rxsoft *rxs;
859 	int i, error;
860 
861 	/*
862 	 * Initialize the transmit descriptor ring.
863 	 */
864 	memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
865 	for (i = 0; i < GEM_NTXDESC; i++) {
866 		sc->sc_txdescs[i].gd_flags = 0;
867 		sc->sc_txdescs[i].gd_addr = 0;
868 	}
869 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
870 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
871 	sc->sc_txfree = GEM_NTXDESC-1;
872 	sc->sc_txnext = 0;
873 	sc->sc_txwin = 0;
874 
875 	/*
876 	 * Initialize the receive descriptor and receive job
877 	 * descriptor rings.
878 	 */
879 	for (i = 0; i < GEM_NRXDESC; i++) {
880 		rxs = &sc->sc_rxsoft[i];
881 		if (rxs->rxs_mbuf == NULL) {
882 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
883 				aprint_error_dev(&sc->sc_dev, "unable to allocate or map rx "
884 				    "buffer %d, error = %d\n",
885 				    i, error);
886 				/*
887 				 * XXX Should attempt to run with fewer receive
888 				 * XXX buffers instead of just failing.
889 				 */
890 				gem_rxdrain(sc);
891 				return (1);
892 			}
893 		} else
894 			GEM_INIT_RXDESC(sc, i);
895 	}
896 	sc->sc_rxptr = 0;
897 	sc->sc_meminited = 1;
898 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
899 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
900 
901 	return (0);
902 }
903 
904 static int
905 gem_ringsize(int sz)
906 {
907 	switch (sz) {
908 	case 32:
909 		return GEM_RING_SZ_32;
910 	case 64:
911 		return GEM_RING_SZ_64;
912 	case 128:
913 		return GEM_RING_SZ_128;
914 	case 256:
915 		return GEM_RING_SZ_256;
916 	case 512:
917 		return GEM_RING_SZ_512;
918 	case 1024:
919 		return GEM_RING_SZ_1024;
920 	case 2048:
921 		return GEM_RING_SZ_2048;
922 	case 4096:
923 		return GEM_RING_SZ_4096;
924 	case 8192:
925 		return GEM_RING_SZ_8192;
926 	default:
927 		printf("gem: invalid Receive Descriptor ring size %d\n", sz);
928 		return GEM_RING_SZ_32;
929 	}
930 }
931 
932 
933 /*
934  * Start PCS
935  */
936 void
937 gem_pcs_start(struct gem_softc *sc)
938 {
939 	bus_space_tag_t t = sc->sc_bustag;
940 	bus_space_handle_t h = sc->sc_h1;
941 	uint32_t v;
942 
943 #ifdef GEM_DEBUG
944 	aprint_debug_dev(&sc->sc_dev, "gem_pcs_start()\n");
945 #endif
946 
947 	/*
948 	 * Set up.  We must disable the MII before modifying the
949 	 * GEM_MII_ANAR register
950 	 */
951 	if (sc->sc_flags & GEM_SERDES) {
952 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
953 		    GEM_MII_DATAPATH_SERDES);
954 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
955 		    GEM_MII_SLINK_LOOPBACK);
956 	} else {
957 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
958 		    GEM_MII_DATAPATH_SERIAL);
959 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL, 0);
960 	}
961 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
962 	v = bus_space_read_4(t, h, GEM_MII_ANAR);
963 	v |= (GEM_MII_ANEG_SYM_PAUSE | GEM_MII_ANEG_ASYM_PAUSE);
964 	if (sc->sc_mii_media == IFM_AUTO)
965 		v |= (GEM_MII_ANEG_FUL_DUPLX | GEM_MII_ANEG_HLF_DUPLX);
966 	else if (sc->sc_mii_media == IFM_FDX) {
967 		v |= GEM_MII_ANEG_FUL_DUPLX;
968 		v &= ~GEM_MII_ANEG_HLF_DUPLX;
969 	} else if (sc->sc_mii_media == IFM_HDX) {
970 		v &= ~GEM_MII_ANEG_FUL_DUPLX;
971 		v |= GEM_MII_ANEG_HLF_DUPLX;
972 	}
973 
974 	/* Configure link. */
975 	bus_space_write_4(t, h, GEM_MII_ANAR, v);
976 	bus_space_write_4(t, h, GEM_MII_CONTROL,
977 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
978 	bus_space_write_4(t, h, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE);
979 	gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_ANEG_CPT);
980 
981 	/* Start the 10 second timer */
982 	callout_reset(&sc->sc_tick_ch, hz * 10, gem_tick, sc);
983 }
984 
985 /*
986  * Stop PCS
987  */
988 void
989 gem_pcs_stop(struct gem_softc *sc, int disable)
990 {
991 	bus_space_tag_t t = sc->sc_bustag;
992 	bus_space_handle_t h = sc->sc_h1;
993 
994 #ifdef GEM_DEBUG
995 	aprint_debug_dev(&sc->sc_dev, "gem_pcs_stop()\n");
996 #endif
997 
998 	/* Tell link partner that we're going away */
999 	bus_space_write_4(t, h, GEM_MII_ANAR, GEM_MII_ANEG_RF);
1000 
1001 	/*
1002 	 * Disable PCS MII.  The documentation suggests that setting
1003 	 * GEM_MII_CONFIG_ENABLE to zero and then restarting auto-
1004 	 * negotiation will shut down the link.  However, it appears
1005 	 * that we also need to unset the datapath mode.
1006 	 */
1007 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
1008 	bus_space_write_4(t, h, GEM_MII_CONTROL,
1009 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
1010 	bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE, GEM_MII_DATAPATH_MII);
1011 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
1012 
1013 	if (disable) {
1014 		if (sc->sc_flags & GEM_SERDES)
1015 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
1016 				GEM_MII_SLINK_POWER_OFF);
1017 		else
1018 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
1019 			    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_POWER_OFF);
1020 	}
1021 
1022 	sc->sc_flags &= ~GEM_LINK;
1023 	sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
1024 	sc->sc_mii.mii_media_status = IFM_AVALID;
1025 }
1026 
1027 
1028 /*
1029  * Initialization of interface; set up initialization block
1030  * and transmit/receive descriptor rings.
1031  */
1032 int
1033 gem_init(struct ifnet *ifp)
1034 {
1035 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
1036 	bus_space_tag_t t = sc->sc_bustag;
1037 	bus_space_handle_t h = sc->sc_h1;
1038 	int rc = 0, s;
1039 	u_int max_frame_size;
1040 	u_int32_t v;
1041 
1042 	s = splnet();
1043 
1044 	DPRINTF(sc, ("%s: gem_init: calling stop\n", device_xname(&sc->sc_dev)));
1045 	/*
1046 	 * Initialization sequence. The numbered steps below correspond
1047 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
1048 	 * Channel Engine manual (part of the PCIO manual).
1049 	 * See also the STP2002-STQ document from Sun Microsystems.
1050 	 */
1051 
1052 	/* step 1 & 2. Reset the Ethernet Channel */
1053 	gem_stop(ifp, 0);
1054 	gem_reset(sc);
1055 	DPRINTF(sc, ("%s: gem_init: restarting\n", device_xname(&sc->sc_dev)));
1056 
1057 	/* Re-initialize the MIF */
1058 	gem_mifinit(sc);
1059 
1060 	/* Set up correct datapath for non-SERDES/Serialink */
1061 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0 &&
1062 	    sc->sc_variant != GEM_SUN_ERI)
1063 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
1064 		    GEM_MII_DATAPATH_MII);
1065 
1066 	/* Call MI reset function if any */
1067 	if (sc->sc_hwreset)
1068 		(*sc->sc_hwreset)(sc);
1069 
1070 	/* step 3. Setup data structures in host memory */
1071 	if (gem_meminit(sc) != 0)
1072 		return 1;
1073 
1074 	/* step 4. TX MAC registers & counters */
1075 	gem_init_regs(sc);
1076 	max_frame_size = max(sc->sc_ethercom.ec_if.if_mtu, ETHERMTU);
1077 	max_frame_size += ETHER_HDR_LEN + ETHER_CRC_LEN;
1078 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
1079 		max_frame_size += ETHER_VLAN_ENCAP_LEN;
1080 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
1081 	    max_frame_size|/* burst size */(0x2000<<16));
1082 
1083 	/* step 5. RX MAC registers & counters */
1084 	gem_setladrf(sc);
1085 
1086 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
1087 	/* NOTE: we use only 32-bit DMA addresses here. */
1088 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0);
1089 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
1090 
1091 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0);
1092 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
1093 
1094 	/* step 8. Global Configuration & Interrupt Mask */
1095 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
1096 		v = GEM_INTR_PCS;
1097 	else
1098 		v = GEM_INTR_MIF;
1099 	bus_space_write_4(t, h, GEM_INTMASK,
1100 		      ~(GEM_INTR_TX_INTME |
1101 			GEM_INTR_TX_EMPTY |
1102 			GEM_INTR_TX_MAC |
1103 			GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF|
1104 			GEM_INTR_RX_TAG_ERR | GEM_INTR_MAC_CONTROL|
1105 			GEM_INTR_BERR | v));
1106 	bus_space_write_4(t, h, GEM_MAC_RX_MASK,
1107 			GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
1108 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXX */
1109 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK,
1110 	    GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME);
1111 
1112 	/* step 9. ETX Configuration: use mostly default values */
1113 
1114 	/* Enable TX DMA */
1115 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
1116 	bus_space_write_4(t, h, GEM_TX_CONFIG,
1117 		v|GEM_TX_CONFIG_TXDMA_EN|
1118 		((0x4FF<<10)&GEM_TX_CONFIG_TXFIFO_TH));
1119 	bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
1120 
1121 	/* step 10. ERX Configuration */
1122 	gem_rx_common(sc);
1123 
1124 	/* step 11. Configure Media */
1125 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0 &&
1126 	    (rc = mii_ifmedia_change(&sc->sc_mii)) != 0)
1127 		goto out;
1128 
1129 	/* step 12. RX_MAC Configuration Register */
1130 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
1131 	v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC;
1132 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
1133 
1134 	/* step 14. Issue Transmit Pending command */
1135 
1136 	/* Call MI initialization function if any */
1137 	if (sc->sc_hwinit)
1138 		(*sc->sc_hwinit)(sc);
1139 
1140 
1141 	/* step 15.  Give the reciever a swift kick */
1142 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
1143 
1144 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
1145 		/* Configure PCS */
1146 		gem_pcs_start(sc);
1147 	else
1148 		/* Start the one second timer. */
1149 		callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc);
1150 
1151 	sc->sc_flags &= ~GEM_LINK;
1152 	ifp->if_flags |= IFF_RUNNING;
1153 	ifp->if_flags &= ~IFF_OACTIVE;
1154 	ifp->if_timer = 0;
1155 	sc->sc_if_flags = ifp->if_flags;
1156 out:
1157 	splx(s);
1158 
1159 	return (0);
1160 }
1161 
1162 void
1163 gem_init_regs(struct gem_softc *sc)
1164 {
1165 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1166 	bus_space_tag_t t = sc->sc_bustag;
1167 	bus_space_handle_t h = sc->sc_h1;
1168 	const u_char *laddr = CLLADDR(ifp->if_sadl);
1169 	u_int32_t v;
1170 
1171 	/* These regs are not cleared on reset */
1172 	if (!sc->sc_inited) {
1173 
1174 		/* Load recommended values */
1175 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0x00);
1176 		bus_space_write_4(t, h, GEM_MAC_IPG1, 0x08);
1177 		bus_space_write_4(t, h, GEM_MAC_IPG2, 0x04);
1178 
1179 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
1180 		/* Max frame and max burst size */
1181 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
1182 		    ETHER_MAX_LEN | (0x2000<<16));
1183 
1184 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x07);
1185 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x04);
1186 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
1187 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
1188 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
1189 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
1190 
1191 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
1192 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
1193 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
1194 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
1195 
1196 		/* MAC control addr set to 01:80:c2:00:00:01 */
1197 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
1198 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
1199 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
1200 
1201 		/* MAC filter addr set to 0:0:0:0:0:0 */
1202 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
1203 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
1204 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
1205 
1206 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
1207 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
1208 
1209 		sc->sc_inited = 1;
1210 	}
1211 
1212 	/* Counters need to be zeroed */
1213 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
1214 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
1215 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
1216 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
1217 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
1218 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
1219 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
1220 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
1221 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
1222 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
1223 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
1224 
1225 	/* Set XOFF PAUSE time. */
1226 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
1227 
1228 	/*
1229 	 * Set the internal arbitration to "infinite" bursts of the
1230 	 * maximum length of 31 * 64 bytes so DMA transfers aren't
1231 	 * split up in cache line size chunks. This greatly improves
1232 	 * especially RX performance.
1233 	 * Enable silicon bug workarounds for the Apple variants.
1234 	 */
1235 	bus_space_write_4(t, h, GEM_CONFIG,
1236 	    GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT |
1237 	    ((sc->sc_flags & GEM_PCI) ?
1238 	    GEM_CONFIG_BURST_INF : GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ?
1239 	    GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0));
1240 
1241 	/*
1242 	 * Set the station address.
1243 	 */
1244 	bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
1245 	bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
1246 	bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
1247 
1248 	/*
1249 	 * Enable MII outputs.  Enable GMII if there is a gigabit PHY.
1250 	 */
1251 	sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG);
1252 	v = GEM_MAC_XIF_TX_MII_ENA;
1253 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)  {
1254 		if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
1255 			v |= GEM_MAC_XIF_FDPLX_LED;
1256 				if (sc->sc_flags & GEM_GIGABIT)
1257 					v |= GEM_MAC_XIF_GMII_MODE;
1258 		}
1259 	} else {
1260 		v |= GEM_MAC_XIF_GMII_MODE;
1261 	}
1262 	bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
1263 }
1264 
1265 #ifdef GEM_DEBUG
1266 static void
1267 gem_txsoft_print(const struct gem_softc *sc, int firstdesc, int lastdesc)
1268 {
1269 	int i;
1270 
1271 	for (i = firstdesc;; i = GEM_NEXTTX(i)) {
1272 		printf("descriptor %d:\t", i);
1273 		printf("gd_flags:   0x%016" PRIx64 "\t",
1274 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
1275 		printf("gd_addr: 0x%016" PRIx64 "\n",
1276 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
1277 		if (i == lastdesc)
1278 			break;
1279 	}
1280 }
1281 #endif
1282 
1283 static void
1284 gem_start(ifp)
1285 	struct ifnet *ifp;
1286 {
1287 	struct gem_softc *sc = (struct gem_softc *)ifp->if_softc;
1288 	struct mbuf *m0, *m;
1289 	struct gem_txsoft *txs;
1290 	bus_dmamap_t dmamap;
1291 	int error, firsttx, nexttx = -1, lasttx = -1, ofree, seg;
1292 	uint64_t flags = 0;
1293 
1294 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1295 		return;
1296 
1297 	/*
1298 	 * Remember the previous number of free descriptors and
1299 	 * the first descriptor we'll use.
1300 	 */
1301 	ofree = sc->sc_txfree;
1302 	firsttx = sc->sc_txnext;
1303 
1304 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
1305 	    device_xname(&sc->sc_dev), ofree, firsttx));
1306 
1307 	/*
1308 	 * Loop through the send queue, setting up transmit descriptors
1309 	 * until we drain the queue, or use up all available transmit
1310 	 * descriptors.
1311 	 */
1312 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
1313 	    sc->sc_txfree != 0) {
1314 		/*
1315 		 * Grab a packet off the queue.
1316 		 */
1317 		IFQ_POLL(&ifp->if_snd, m0);
1318 		if (m0 == NULL)
1319 			break;
1320 		m = NULL;
1321 
1322 		dmamap = txs->txs_dmamap;
1323 
1324 		/*
1325 		 * Load the DMA map.  If this fails, the packet either
1326 		 * didn't fit in the alloted number of segments, or we were
1327 		 * short on resources.  In this case, we'll copy and try
1328 		 * again.
1329 		 */
1330 		if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
1331 		      BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0 ||
1332 		      (m0->m_pkthdr.len < ETHER_MIN_TX &&
1333 		       dmamap->dm_nsegs == GEM_NTXSEGS)) {
1334 			if (m0->m_pkthdr.len > MCLBYTES) {
1335 				aprint_error_dev(&sc->sc_dev, "unable to allocate jumbo Tx "
1336 				    "cluster\n");
1337 				IFQ_DEQUEUE(&ifp->if_snd, m0);
1338 				m_freem(m0);
1339 				continue;
1340 			}
1341 			MGETHDR(m, M_DONTWAIT, MT_DATA);
1342 			if (m == NULL) {
1343 				aprint_error_dev(&sc->sc_dev, "unable to allocate Tx mbuf\n");
1344 				break;
1345 			}
1346 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
1347 			if (m0->m_pkthdr.len > MHLEN) {
1348 				MCLGET(m, M_DONTWAIT);
1349 				if ((m->m_flags & M_EXT) == 0) {
1350 					aprint_error_dev(&sc->sc_dev, "unable to allocate Tx "
1351 					    "cluster\n");
1352 					m_freem(m);
1353 					break;
1354 				}
1355 			}
1356 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
1357 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
1358 			error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
1359 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1360 			if (error) {
1361 				aprint_error_dev(&sc->sc_dev, "unable to load Tx buffer, "
1362 				    "error = %d\n", error);
1363 				break;
1364 			}
1365 		}
1366 
1367 		/*
1368 		 * Ensure we have enough descriptors free to describe
1369 		 * the packet.
1370 		 */
1371 		if (dmamap->dm_nsegs > ((m0->m_pkthdr.len < ETHER_MIN_TX) ?
1372 		     (sc->sc_txfree - 1) : sc->sc_txfree)) {
1373 			/*
1374 			 * Not enough free descriptors to transmit this
1375 			 * packet.  We haven't committed to anything yet,
1376 			 * so just unload the DMA map, put the packet
1377 			 * back on the queue, and punt.  Notify the upper
1378 			 * layer that there are no more slots left.
1379 			 *
1380 			 * XXX We could allocate an mbuf and copy, but
1381 			 * XXX it is worth it?
1382 			 */
1383 			ifp->if_flags |= IFF_OACTIVE;
1384 			sc->sc_if_flags = ifp->if_flags;
1385 			bus_dmamap_unload(sc->sc_dmatag, dmamap);
1386 			if (m != NULL)
1387 				m_freem(m);
1388 			break;
1389 		}
1390 
1391 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1392 		if (m != NULL) {
1393 			m_freem(m0);
1394 			m0 = m;
1395 		}
1396 
1397 		/*
1398 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
1399 		 */
1400 
1401 		/* Sync the DMA map. */
1402 		bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
1403 		    BUS_DMASYNC_PREWRITE);
1404 
1405 		/*
1406 		 * Initialize the transmit descriptors.
1407 		 */
1408 		for (nexttx = sc->sc_txnext, seg = 0;
1409 		     seg < dmamap->dm_nsegs;
1410 		     seg++, nexttx = GEM_NEXTTX(nexttx)) {
1411 
1412 			/*
1413 			 * If this is the first descriptor we're
1414 			 * enqueueing, set the start of packet flag,
1415 			 * and the checksum stuff if we want the hardware
1416 			 * to do it.
1417 			 */
1418 			sc->sc_txdescs[nexttx].gd_addr =
1419 			    GEM_DMA_WRITE(sc, dmamap->dm_segs[seg].ds_addr);
1420 			flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
1421 			if (nexttx == firsttx) {
1422 				flags |= GEM_TD_START_OF_PACKET;
1423 				if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
1424 					sc->sc_txwin = 0;
1425 					flags |= GEM_TD_INTERRUPT_ME;
1426 				}
1427 
1428 #ifdef INET
1429 				/* h/w checksum */
1430 				if (ifp->if_csum_flags_tx & M_CSUM_TCPv4 &&
1431 				    m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
1432 					struct ether_header *eh;
1433 					uint16_t offset, start;
1434 
1435 					eh = mtod(m0, struct ether_header *);
1436 					switch (ntohs(eh->ether_type)) {
1437 					case ETHERTYPE_IP:
1438 						start = ETHER_HDR_LEN;
1439 						break;
1440 					case ETHERTYPE_VLAN:
1441 						start = ETHER_HDR_LEN +
1442 							ETHER_VLAN_ENCAP_LEN;
1443 						break;
1444 					default:
1445 						/* unsupported, drop it */
1446 						m_free(m0);
1447 						continue;
1448 					}
1449 					start += M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
1450 					offset = M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data) + start;
1451 					flags |= (start <<
1452 						  GEM_TD_CXSUM_STARTSHFT) |
1453 						 (offset <<
1454 						  GEM_TD_CXSUM_STUFFSHFT) |
1455 						 GEM_TD_CXSUM_ENABLE;
1456 				}
1457 #endif
1458 			}
1459 			if (seg == dmamap->dm_nsegs - 1) {
1460 				flags |= GEM_TD_END_OF_PACKET;
1461 			} else {
1462 				/* last flag set outside of loop */
1463 				sc->sc_txdescs[nexttx].gd_flags =
1464 					GEM_DMA_WRITE(sc, flags);
1465 			}
1466 			lasttx = nexttx;
1467 		}
1468 		if (m0->m_pkthdr.len < ETHER_MIN_TX) {
1469 			/* add padding buffer at end of chain */
1470 			flags &= ~GEM_TD_END_OF_PACKET;
1471 			sc->sc_txdescs[lasttx].gd_flags =
1472 			    GEM_DMA_WRITE(sc, flags);
1473 
1474 			sc->sc_txdescs[nexttx].gd_addr =
1475 			    GEM_DMA_WRITE(sc,
1476 			    sc->sc_nulldmamap->dm_segs[0].ds_addr);
1477 			flags = ((ETHER_MIN_TX - m0->m_pkthdr.len) &
1478 			    GEM_TD_BUFSIZE) | GEM_TD_END_OF_PACKET;
1479 			lasttx = nexttx;
1480 			nexttx = GEM_NEXTTX(nexttx);
1481 			seg++;
1482 		}
1483 		sc->sc_txdescs[lasttx].gd_flags = GEM_DMA_WRITE(sc, flags);
1484 
1485 		KASSERT(lasttx != -1);
1486 
1487 		/*
1488 		 * Store a pointer to the packet so we can free it later,
1489 		 * and remember what txdirty will be once the packet is
1490 		 * done.
1491 		 */
1492 		txs->txs_mbuf = m0;
1493 		txs->txs_firstdesc = sc->sc_txnext;
1494 		txs->txs_lastdesc = lasttx;
1495 		txs->txs_ndescs = seg;
1496 
1497 #ifdef GEM_DEBUG
1498 		if (ifp->if_flags & IFF_DEBUG) {
1499 			printf("     gem_start %p transmit chain:\n", txs);
1500 			gem_txsoft_print(sc, txs->txs_firstdesc,
1501 			    txs->txs_lastdesc);
1502 		}
1503 #endif
1504 
1505 		/* Sync the descriptors we're using. */
1506 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
1507 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1508 
1509 		/* Advance the tx pointer. */
1510 		sc->sc_txfree -= txs->txs_ndescs;
1511 		sc->sc_txnext = nexttx;
1512 
1513 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
1514 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
1515 
1516 #if NBPFILTER > 0
1517 		/*
1518 		 * Pass the packet to any BPF listeners.
1519 		 */
1520 		if (ifp->if_bpf)
1521 			bpf_mtap(ifp->if_bpf, m0);
1522 #endif /* NBPFILTER > 0 */
1523 	}
1524 
1525 	if (txs == NULL || sc->sc_txfree == 0) {
1526 		/* No more slots left; notify upper layer. */
1527 		ifp->if_flags |= IFF_OACTIVE;
1528 		sc->sc_if_flags = ifp->if_flags;
1529 	}
1530 
1531 	if (sc->sc_txfree != ofree) {
1532 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
1533 		    device_xname(&sc->sc_dev), lasttx, firsttx));
1534 		/*
1535 		 * The entire packet chain is set up.
1536 		 * Kick the transmitter.
1537 		 */
1538 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
1539 			device_xname(&sc->sc_dev), nexttx));
1540 		bus_space_write_4(sc->sc_bustag, sc->sc_h1, GEM_TX_KICK,
1541 			sc->sc_txnext);
1542 
1543 		/* Set a watchdog timer in case the chip flakes out. */
1544 		ifp->if_timer = 5;
1545 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
1546 			device_xname(&sc->sc_dev), ifp->if_timer));
1547 	}
1548 }
1549 
1550 /*
1551  * Transmit interrupt.
1552  */
1553 int
1554 gem_tint(sc)
1555 	struct gem_softc *sc;
1556 {
1557 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1558 	bus_space_tag_t t = sc->sc_bustag;
1559 	bus_space_handle_t mac = sc->sc_h1;
1560 	struct gem_txsoft *txs;
1561 	int txlast;
1562 	int progress = 0;
1563 	u_int32_t v;
1564 
1565 	DPRINTF(sc, ("%s: gem_tint\n", device_xname(&sc->sc_dev)));
1566 
1567 	/* Unload collision counters ... */
1568 	v = bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
1569 	    bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
1570 	ifp->if_collisions += v +
1571 	    bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
1572 	    bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT);
1573 	ifp->if_oerrors += v;
1574 
1575 	/* ... then clear the hardware counters. */
1576 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
1577 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
1578 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
1579 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
1580 
1581 	/*
1582 	 * Go through our Tx list and free mbufs for those
1583 	 * frames that have been transmitted.
1584 	 */
1585 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
1586 		/*
1587 		 * In theory, we could harvest some descriptors before
1588 		 * the ring is empty, but that's a bit complicated.
1589 		 *
1590 		 * GEM_TX_COMPLETION points to the last descriptor
1591 		 * processed +1.
1592 		 *
1593 		 * Let's assume that the NIC writes back to the Tx
1594 		 * descriptors before it updates the completion
1595 		 * register.  If the NIC has posted writes to the
1596 		 * Tx descriptors, PCI ordering requires that the
1597 		 * posted writes flush to RAM before the register-read
1598 		 * finishes.  So let's read the completion register,
1599 		 * before syncing the descriptors, so that we
1600 		 * examine Tx descriptors that are at least as
1601 		 * current as the completion register.
1602 		 */
1603 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
1604 		DPRINTF(sc,
1605 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
1606 				txs->txs_lastdesc, txlast));
1607 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
1608 			if (txlast >= txs->txs_firstdesc &&
1609 			    txlast <= txs->txs_lastdesc)
1610 				break;
1611 		} else if (txlast >= txs->txs_firstdesc ||
1612 			   txlast <= txs->txs_lastdesc)
1613 			break;
1614 
1615 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
1616 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1617 
1618 #ifdef GEM_DEBUG	/* XXX DMA synchronization? */
1619 		if (ifp->if_flags & IFF_DEBUG) {
1620 			printf("    txsoft %p transmit chain:\n", txs);
1621 			gem_txsoft_print(sc, txs->txs_firstdesc,
1622 			    txs->txs_lastdesc);
1623 		}
1624 #endif
1625 
1626 
1627 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
1628 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
1629 
1630 		sc->sc_txfree += txs->txs_ndescs;
1631 
1632 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
1633 		    0, txs->txs_dmamap->dm_mapsize,
1634 		    BUS_DMASYNC_POSTWRITE);
1635 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
1636 		if (txs->txs_mbuf != NULL) {
1637 			m_freem(txs->txs_mbuf);
1638 			txs->txs_mbuf = NULL;
1639 		}
1640 
1641 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
1642 
1643 		ifp->if_opackets++;
1644 		progress = 1;
1645 	}
1646 
1647 #if 0
1648 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
1649 		"GEM_TX_DATA_PTR %" PRIx64 "GEM_TX_COMPLETION %" PRIx32 "\n",
1650 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_STATE_MACHINE),
1651 		((uint64_t)bus_space_read_4(sc->sc_bustag, sc->sc_h1,
1652 			GEM_TX_DATA_PTR_HI) << 32) |
1653 			     bus_space_read_4(sc->sc_bustag, sc->sc_h1,
1654 			GEM_TX_DATA_PTR_LO),
1655 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_COMPLETION)));
1656 #endif
1657 
1658 	if (progress) {
1659 		if (sc->sc_txfree == GEM_NTXDESC - 1)
1660 			sc->sc_txwin = 0;
1661 
1662 		/* Freed some descriptors, so reset IFF_OACTIVE and restart. */
1663 		ifp->if_flags &= ~IFF_OACTIVE;
1664 		sc->sc_if_flags = ifp->if_flags;
1665 		ifp->if_timer = SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
1666 		gem_start(ifp);
1667 	}
1668 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
1669 		device_xname(&sc->sc_dev), ifp->if_timer));
1670 
1671 	return (1);
1672 }
1673 
1674 /*
1675  * Receive interrupt.
1676  */
1677 int
1678 gem_rint(sc)
1679 	struct gem_softc *sc;
1680 {
1681 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1682 	bus_space_tag_t t = sc->sc_bustag;
1683 	bus_space_handle_t h = sc->sc_h1;
1684 	struct gem_rxsoft *rxs;
1685 	struct mbuf *m;
1686 	u_int64_t rxstat;
1687 	u_int32_t rxcomp;
1688 	int i, len, progress = 0;
1689 
1690 	DPRINTF(sc, ("%s: gem_rint\n", device_xname(&sc->sc_dev)));
1691 
1692 	/*
1693 	 * Ignore spurious interrupt that sometimes occurs before
1694 	 * we are set up when we network boot.
1695 	 */
1696 	if (!sc->sc_meminited)
1697 		return 1;
1698 
1699 	/*
1700 	 * Read the completion register once.  This limits
1701 	 * how long the following loop can execute.
1702 	 */
1703 	rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
1704 
1705 	/*
1706 	 * XXX Read the lastrx only once at the top for speed.
1707 	 */
1708 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
1709 		sc->sc_rxptr, rxcomp));
1710 
1711 	/*
1712 	 * Go into the loop at least once.
1713 	 */
1714 	for (i = sc->sc_rxptr; i == sc->sc_rxptr || i != rxcomp;
1715 	     i = GEM_NEXTRX(i)) {
1716 		rxs = &sc->sc_rxsoft[i];
1717 
1718 		GEM_CDRXSYNC(sc, i,
1719 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1720 
1721 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
1722 
1723 		if (rxstat & GEM_RD_OWN) {
1724 			GEM_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
1725 			/*
1726 			 * We have processed all of the receive buffers.
1727 			 */
1728 			break;
1729 		}
1730 
1731 		progress++;
1732 		ifp->if_ipackets++;
1733 
1734 		if (rxstat & GEM_RD_BAD_CRC) {
1735 			ifp->if_ierrors++;
1736 			aprint_error_dev(&sc->sc_dev, "receive error: CRC error\n");
1737 			GEM_INIT_RXDESC(sc, i);
1738 			continue;
1739 		}
1740 
1741 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1742 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1743 #ifdef GEM_DEBUG
1744 		if (ifp->if_flags & IFF_DEBUG) {
1745 			printf("    rxsoft %p descriptor %d: ", rxs, i);
1746 			printf("gd_flags: 0x%016llx\t", (long long)
1747 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
1748 			printf("gd_addr: 0x%016llx\n", (long long)
1749 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
1750 		}
1751 #endif
1752 
1753 		/* No errors; receive the packet. */
1754 		len = GEM_RD_BUFLEN(rxstat);
1755 
1756 		/*
1757 		 * Allocate a new mbuf cluster.  If that fails, we are
1758 		 * out of memory, and must drop the packet and recycle
1759 		 * the buffer that's already attached to this descriptor.
1760 		 */
1761 		m = rxs->rxs_mbuf;
1762 		if (gem_add_rxbuf(sc, i) != 0) {
1763 			GEM_COUNTER_INCR(sc, sc_ev_rxnobuf);
1764 			ifp->if_ierrors++;
1765 			GEM_INIT_RXDESC(sc, i);
1766 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1767 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1768 			continue;
1769 		}
1770 		m->m_data += 2; /* We're already off by two */
1771 
1772 		m->m_pkthdr.rcvif = ifp;
1773 		m->m_pkthdr.len = m->m_len = len;
1774 
1775 #if NBPFILTER > 0
1776 		/*
1777 		 * Pass this up to any BPF listeners, but only
1778 		 * pass it up the stack if it's for us.
1779 		 */
1780 		if (ifp->if_bpf)
1781 			bpf_mtap(ifp->if_bpf, m);
1782 #endif /* NBPFILTER > 0 */
1783 
1784 #ifdef INET
1785 		/* hardware checksum */
1786 		if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
1787 			struct ether_header *eh;
1788 			struct ip *ip;
1789 			int32_t hlen, pktlen;
1790 
1791 			if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
1792 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN -
1793 					 ETHER_VLAN_ENCAP_LEN;
1794 				eh = (struct ether_header *) (mtod(m, char *) +
1795 					ETHER_VLAN_ENCAP_LEN);
1796 			} else {
1797 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN;
1798 				eh = mtod(m, struct ether_header *);
1799 			}
1800 			if (ntohs(eh->ether_type) != ETHERTYPE_IP)
1801 				goto swcsum;
1802 			ip = (struct ip *) ((char *)eh + ETHER_HDR_LEN);
1803 
1804 			/* IPv4 only */
1805 			if (ip->ip_v != IPVERSION)
1806 				goto swcsum;
1807 
1808 			hlen = ip->ip_hl << 2;
1809 			if (hlen < sizeof(struct ip))
1810 				goto swcsum;
1811 
1812 			/*
1813 			 * bail if too short, has random trailing garbage,
1814 			 * truncated, fragment, or has ethernet pad.
1815 			 */
1816 			if ((ntohs(ip->ip_len) < hlen) ||
1817 			    (ntohs(ip->ip_len) != pktlen) ||
1818 			    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)))
1819 				goto swcsum;
1820 
1821 			switch (ip->ip_p) {
1822 			case IPPROTO_TCP:
1823 				if (! (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
1824 					goto swcsum;
1825 				if (pktlen < (hlen + sizeof(struct tcphdr)))
1826 					goto swcsum;
1827 				m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1828 				break;
1829 			case IPPROTO_UDP:
1830 				/* FALLTHROUGH */
1831 			default:
1832 				goto swcsum;
1833 			}
1834 
1835 			/* the uncomplemented sum is expected */
1836 			m->m_pkthdr.csum_data = (~rxstat) & GEM_RD_CHECKSUM;
1837 
1838 			/* if the pkt had ip options, we have to deduct them */
1839 			if (hlen > sizeof(struct ip)) {
1840 				uint16_t *opts;
1841 				uint32_t optsum, temp;
1842 
1843 				optsum = 0;
1844 				temp = hlen - sizeof(struct ip);
1845 				opts = (uint16_t *) ((char *) ip +
1846 					sizeof(struct ip));
1847 
1848 				while (temp > 1) {
1849 					optsum += ntohs(*opts++);
1850 					temp -= 2;
1851 				}
1852 				while (optsum >> 16)
1853 					optsum = (optsum >> 16) +
1854 						 (optsum & 0xffff);
1855 
1856 				/* Deduct ip opts sum from hwsum (rfc 1624). */
1857 				m->m_pkthdr.csum_data =
1858 					~((~m->m_pkthdr.csum_data) - ~optsum);
1859 
1860 				while (m->m_pkthdr.csum_data >> 16)
1861 					m->m_pkthdr.csum_data =
1862 						(m->m_pkthdr.csum_data >> 16) +
1863 						(m->m_pkthdr.csum_data &
1864 						 0xffff);
1865 			}
1866 
1867 			m->m_pkthdr.csum_flags |= M_CSUM_DATA |
1868 						  M_CSUM_NO_PSEUDOHDR;
1869 		} else
1870 swcsum:
1871 			m->m_pkthdr.csum_flags = 0;
1872 #endif
1873 		/* Pass it on. */
1874 		(*ifp->if_input)(ifp, m);
1875 	}
1876 
1877 	if (progress) {
1878 		/* Update the receive pointer. */
1879 		if (i == sc->sc_rxptr) {
1880 			GEM_COUNTER_INCR(sc, sc_ev_rxfull);
1881 #ifdef GEM_DEBUG
1882 			if (ifp->if_flags & IFF_DEBUG)
1883 				printf("%s: rint: ring wrap\n",
1884 				    device_xname(&sc->sc_dev));
1885 #endif
1886 		}
1887 		sc->sc_rxptr = i;
1888 		bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
1889 	}
1890 #ifdef GEM_COUNTERS
1891 	if (progress <= 4) {
1892 		GEM_COUNTER_INCR(sc, sc_ev_rxhist[progress]);
1893 	} else if (progress < 32) {
1894 		if (progress < 16)
1895 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[5]);
1896 		else
1897 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[6]);
1898 
1899 	} else {
1900 		if (progress < 64)
1901 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[7]);
1902 		else
1903 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[8]);
1904 	}
1905 #endif
1906 
1907 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
1908 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
1909 
1910 	/* Read error counters ... */
1911 	ifp->if_ierrors +=
1912 	    bus_space_read_4(t, h, GEM_MAC_RX_LEN_ERR_CNT) +
1913 	    bus_space_read_4(t, h, GEM_MAC_RX_ALIGN_ERR) +
1914 	    bus_space_read_4(t, h, GEM_MAC_RX_CRC_ERR_CNT) +
1915 	    bus_space_read_4(t, h, GEM_MAC_RX_CODE_VIOL);
1916 
1917 	/* ... then clear the hardware counters. */
1918 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
1919 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
1920 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
1921 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
1922 
1923 	return (1);
1924 }
1925 
1926 
1927 /*
1928  * gem_add_rxbuf:
1929  *
1930  *	Add a receive buffer to the indicated descriptor.
1931  */
1932 int
1933 gem_add_rxbuf(struct gem_softc *sc, int idx)
1934 {
1935 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
1936 	struct mbuf *m;
1937 	int error;
1938 
1939 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1940 	if (m == NULL)
1941 		return (ENOBUFS);
1942 
1943 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
1944 	MCLGET(m, M_DONTWAIT);
1945 	if ((m->m_flags & M_EXT) == 0) {
1946 		m_freem(m);
1947 		return (ENOBUFS);
1948 	}
1949 
1950 #ifdef GEM_DEBUG
1951 /* bzero the packet to check DMA */
1952 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
1953 #endif
1954 
1955 	if (rxs->rxs_mbuf != NULL)
1956 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
1957 
1958 	rxs->rxs_mbuf = m;
1959 
1960 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
1961 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1962 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
1963 	if (error) {
1964 		aprint_error_dev(&sc->sc_dev, "can't load rx DMA map %d, error = %d\n",
1965 		    idx, error);
1966 		panic("gem_add_rxbuf");	/* XXX */
1967 	}
1968 
1969 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
1970 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1971 
1972 	GEM_INIT_RXDESC(sc, idx);
1973 
1974 	return (0);
1975 }
1976 
1977 
1978 int
1979 gem_eint(struct gem_softc *sc, u_int status)
1980 {
1981 	char bits[128];
1982 	u_int32_t r, v;
1983 
1984 	if ((status & GEM_INTR_MIF) != 0) {
1985 		printf("%s: XXXlink status changed\n", device_xname(&sc->sc_dev));
1986 		return (1);
1987 	}
1988 
1989 	if ((status & GEM_INTR_RX_TAG_ERR) != 0) {
1990 		gem_reset_rxdma(sc);
1991 		return (1);
1992 	}
1993 
1994 	if (status & GEM_INTR_BERR) {
1995 		if (sc->sc_flags & GEM_PCI)
1996 			r = GEM_ERROR_STATUS;
1997 		else
1998 			r = GEM_SBUS_ERROR_STATUS;
1999 		bus_space_read_4(sc->sc_bustag, sc->sc_h2, r);
2000 		v = bus_space_read_4(sc->sc_bustag, sc->sc_h2, r);
2001 		aprint_error_dev(&sc->sc_dev, "bus error interrupt: 0x%02x\n",
2002 		    v);
2003 		return (1);
2004 	}
2005 
2006 	printf("%s: status=%s\n", device_xname(&sc->sc_dev),
2007 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits)));
2008 	return (1);
2009 }
2010 
2011 
2012 /*
2013  * PCS interrupts.
2014  * We should receive these when the link status changes, but sometimes
2015  * we don't receive them for link up.  We compensate for this in the
2016  * gem_tick() callout.
2017  */
2018 int
2019 gem_pint(struct gem_softc *sc)
2020 {
2021 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2022 	bus_space_tag_t t = sc->sc_bustag;
2023 	bus_space_handle_t h = sc->sc_h1;
2024 	u_int32_t v, v2;
2025 
2026 	/*
2027 	 * Clear the PCS interrupt from GEM_STATUS.  The PCS register is
2028 	 * latched, so we have to read it twice.  There is only one bit in
2029 	 * use, so the value is meaningless.
2030 	 */
2031 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
2032 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
2033 
2034 	if ((ifp->if_flags & IFF_UP) == 0)
2035 		return 1;
2036 
2037 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)
2038 		return 1;
2039 
2040 	v = bus_space_read_4(t, h, GEM_MII_STATUS);
2041 	/* If we see remote fault, our link partner is probably going away */
2042 	if ((v & GEM_MII_STATUS_REM_FLT) != 0) {
2043 		gem_bitwait(sc, h, GEM_MII_STATUS, GEM_MII_STATUS_REM_FLT, 0);
2044 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
2045 	/* Otherwise, we may need to wait after auto-negotiation completes */
2046 	} else if ((v & (GEM_MII_STATUS_LINK_STS | GEM_MII_STATUS_ANEG_CPT)) ==
2047 	    GEM_MII_STATUS_ANEG_CPT) {
2048 		gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_LINK_STS);
2049 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
2050 	}
2051 	if ((v & GEM_MII_STATUS_LINK_STS) != 0) {
2052 		if (sc->sc_flags & GEM_LINK) {
2053 			return 1;
2054 		}
2055 		callout_stop(&sc->sc_tick_ch);
2056 		v = bus_space_read_4(t, h, GEM_MII_ANAR);
2057 		v2 = bus_space_read_4(t, h, GEM_MII_ANLPAR);
2058 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_1000_SX;
2059 		sc->sc_mii.mii_media_status = IFM_AVALID | IFM_ACTIVE;
2060 		v &= v2;
2061 		if (v & GEM_MII_ANEG_FUL_DUPLX) {
2062 			sc->sc_mii.mii_media_active |= IFM_FDX;
2063 #ifdef GEM_DEBUG
2064 			aprint_debug_dev(&sc->sc_dev, "link up: full duplex\n");
2065 #endif
2066 		} else if (v & GEM_MII_ANEG_HLF_DUPLX) {
2067 			sc->sc_mii.mii_media_active |= IFM_HDX;
2068 #ifdef GEM_DEBUG
2069 			aprint_debug_dev(&sc->sc_dev, "link up: half duplex\n");
2070 #endif
2071 		} else {
2072 #ifdef GEM_DEBUG
2073 			aprint_debug_dev(&sc->sc_dev, "duplex mismatch\n");
2074 #endif
2075 		}
2076 		gem_statuschange(sc);
2077 	} else {
2078 		if ((sc->sc_flags & GEM_LINK) == 0) {
2079 			return 1;
2080 		}
2081 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
2082 		sc->sc_mii.mii_media_status = IFM_AVALID;
2083 #ifdef GEM_DEBUG
2084 			aprint_debug_dev(&sc->sc_dev, "link down\n");
2085 #endif
2086 		gem_statuschange(sc);
2087 
2088 		/* Start the 10 second timer */
2089 		callout_reset(&sc->sc_tick_ch, hz * 10, gem_tick, sc);
2090 	}
2091 	return 1;
2092 }
2093 
2094 
2095 
2096 int
2097 gem_intr(v)
2098 	void *v;
2099 {
2100 	struct gem_softc *sc = (struct gem_softc *)v;
2101 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2102 	bus_space_tag_t t = sc->sc_bustag;
2103 	bus_space_handle_t h = sc->sc_h1;
2104 	u_int32_t status;
2105 	int r = 0;
2106 #ifdef GEM_DEBUG
2107 	char bits[128];
2108 #endif
2109 
2110 	/* XXX We should probably mask out interrupts until we're done */
2111 
2112 	sc->sc_ev_intr.ev_count++;
2113 
2114 	status = bus_space_read_4(t, h, GEM_STATUS);
2115 	DPRINTF(sc, ("%s: gem_intr: cplt 0x%x status %s\n",
2116 		device_xname(&sc->sc_dev), (status >> 19),
2117 		bitmask_snprintf(status, GEM_INTR_BITS, bits, sizeof(bits))));
2118 
2119 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
2120 		r |= gem_eint(sc, status);
2121 
2122 	/* We don't bother with GEM_INTR_TX_DONE */
2123 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) {
2124 		GEM_COUNTER_INCR(sc, sc_ev_txint);
2125 		r |= gem_tint(sc);
2126 	}
2127 
2128 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) {
2129 		GEM_COUNTER_INCR(sc, sc_ev_rxint);
2130 		r |= gem_rint(sc);
2131 	}
2132 
2133 	/* We should eventually do more than just print out error stats. */
2134 	if (status & GEM_INTR_TX_MAC) {
2135 		int txstat = bus_space_read_4(t, h, GEM_MAC_TX_STATUS);
2136 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
2137 			printf("%s: MAC tx fault, status %x\n",
2138 			    device_xname(&sc->sc_dev), txstat);
2139 		if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
2140 			gem_init(ifp);
2141 	}
2142 	if (status & GEM_INTR_RX_MAC) {
2143 		int rxstat = bus_space_read_4(t, h, GEM_MAC_RX_STATUS);
2144 		/*
2145 		 * At least with GEM_SUN_GEM and some GEM_SUN_ERI
2146 		 * revisions GEM_MAC_RX_OVERFLOW happen often due to a
2147 		 * silicon bug so handle them silently. Moreover, it's
2148 		 * likely that the receiver has hung so we reset it.
2149 		 */
2150 		if (rxstat & GEM_MAC_RX_OVERFLOW) {
2151 			ifp->if_ierrors++;
2152 			gem_reset_rxdma(sc);
2153 		} else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
2154 			printf("%s: MAC rx fault, status 0x%02x\n",
2155 			    device_xname(&sc->sc_dev), rxstat);
2156 	}
2157 	if (status & GEM_INTR_PCS) {
2158 		r |= gem_pint(sc);
2159 	}
2160 
2161 /* Do we need to do anything with these?
2162 	if ((status & GEM_MAC_CONTROL_STATUS) != 0) {
2163 		status2 = bus_read_4(sc->sc_res[0], GEM_MAC_CONTROL_STATUS);
2164 		if ((status2 & GEM_MAC_PAUSED) != 0)
2165 			aprintf_debug_dev(&sc->sc_dev, "PAUSE received (%d slots)\n",
2166 			    GEM_MAC_PAUSE_TIME(status2));
2167 		if ((status2 & GEM_MAC_PAUSE) != 0)
2168 			aprintf_debug_dev(&sc->sc_dev, "transited to PAUSE state\n");
2169 		if ((status2 & GEM_MAC_RESUME) != 0)
2170 			aprintf_debug_dev(&sc->sc_dev, "transited to non-PAUSE state\n");
2171 	}
2172 	if ((status & GEM_INTR_MIF) != 0)
2173 		aprintf_debug_dev(&sc->sc_dev, "MIF interrupt\n");
2174 */
2175 #if NRND > 0
2176 	rnd_add_uint32(&sc->rnd_source, status);
2177 #endif
2178 	return (r);
2179 }
2180 
2181 
2182 void
2183 gem_watchdog(ifp)
2184 	struct ifnet *ifp;
2185 {
2186 	struct gem_softc *sc = ifp->if_softc;
2187 
2188 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
2189 		"GEM_MAC_RX_CONFIG %x\n",
2190 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_RX_CONFIG),
2191 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_STATUS),
2192 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_CONFIG)));
2193 
2194 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
2195 	++ifp->if_oerrors;
2196 
2197 	/* Try to get more packets going. */
2198 	gem_start(ifp);
2199 }
2200 
2201 /*
2202  * Initialize the MII Management Interface
2203  */
2204 void
2205 gem_mifinit(sc)
2206 	struct gem_softc *sc;
2207 {
2208 	bus_space_tag_t t = sc->sc_bustag;
2209 	bus_space_handle_t mif = sc->sc_h1;
2210 
2211 	/* Configure the MIF in frame mode */
2212 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
2213 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
2214 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
2215 }
2216 
2217 /*
2218  * MII interface
2219  *
2220  * The GEM MII interface supports at least three different operating modes:
2221  *
2222  * Bitbang mode is implemented using data, clock and output enable registers.
2223  *
2224  * Frame mode is implemented by loading a complete frame into the frame
2225  * register and polling the valid bit for completion.
2226  *
2227  * Polling mode uses the frame register but completion is indicated by
2228  * an interrupt.
2229  *
2230  */
2231 static int
2232 gem_mii_readreg(self, phy, reg)
2233 	struct device *self;
2234 	int phy, reg;
2235 {
2236 	struct gem_softc *sc = (void *)self;
2237 	bus_space_tag_t t = sc->sc_bustag;
2238 	bus_space_handle_t mif = sc->sc_h1;
2239 	int n;
2240 	u_int32_t v;
2241 
2242 #ifdef GEM_DEBUG1
2243 	if (sc->sc_debug)
2244 		printf("gem_mii_readreg: PHY %d reg %d\n", phy, reg);
2245 #endif
2246 
2247 	/* Construct the frame command */
2248 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
2249 		GEM_MIF_FRAME_READ;
2250 
2251 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
2252 	for (n = 0; n < 100; n++) {
2253 		DELAY(1);
2254 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
2255 		if (v & GEM_MIF_FRAME_TA0)
2256 			return (v & GEM_MIF_FRAME_DATA);
2257 	}
2258 
2259 	printf("%s: mii_read timeout\n", device_xname(&sc->sc_dev));
2260 	return (0);
2261 }
2262 
2263 static void
2264 gem_mii_writereg(self, phy, reg, val)
2265 	struct device *self;
2266 	int phy, reg, val;
2267 {
2268 	struct gem_softc *sc = (void *)self;
2269 	bus_space_tag_t t = sc->sc_bustag;
2270 	bus_space_handle_t mif = sc->sc_h1;
2271 	int n;
2272 	u_int32_t v;
2273 
2274 #ifdef GEM_DEBUG1
2275 	if (sc->sc_debug)
2276 		printf("gem_mii_writereg: PHY %d reg %d val %x\n",
2277 			phy, reg, val);
2278 #endif
2279 
2280 	/* Construct the frame command */
2281 	v = GEM_MIF_FRAME_WRITE			|
2282 	    (phy << GEM_MIF_PHY_SHIFT)		|
2283 	    (reg << GEM_MIF_REG_SHIFT)		|
2284 	    (val & GEM_MIF_FRAME_DATA);
2285 
2286 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
2287 	for (n = 0; n < 100; n++) {
2288 		DELAY(1);
2289 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
2290 		if (v & GEM_MIF_FRAME_TA0)
2291 			return;
2292 	}
2293 
2294 	printf("%s: mii_write timeout\n", device_xname(&sc->sc_dev));
2295 }
2296 
2297 static void
2298 gem_mii_statchg(dev)
2299 	struct device *dev;
2300 {
2301 	struct gem_softc *sc = (void *)dev;
2302 #ifdef GEM_DEBUG
2303 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
2304 #endif
2305 
2306 #ifdef GEM_DEBUG
2307 	if (sc->sc_debug)
2308 		printf("gem_mii_statchg: status change: phy = %d\n",
2309 			sc->sc_phys[instance]);
2310 #endif
2311 	gem_statuschange(sc);
2312 }
2313 
2314 /*
2315  * Common status change for gem_mii_statchg() and gem_pint()
2316  */
2317 void
2318 gem_statuschange(struct gem_softc* sc)
2319 {
2320 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2321 	bus_space_tag_t t = sc->sc_bustag;
2322 	bus_space_handle_t mac = sc->sc_h1;
2323 	int gigabit;
2324 	u_int32_t rxcfg, txcfg, v;
2325 
2326 	if ((sc->sc_mii.mii_media_status & IFM_ACTIVE) != 0 &&
2327 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_NONE)
2328 		sc->sc_flags |= GEM_LINK;
2329 	else
2330 		sc->sc_flags &= ~GEM_LINK;
2331 
2332 	if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
2333 		gigabit = 1;
2334 	else
2335 		gigabit = 0;
2336 
2337 	/*
2338 	 * The configuration done here corresponds to the steps F) and
2339 	 * G) and as far as enabling of RX and TX MAC goes also step H)
2340 	 * of the initialization sequence outlined in section 3.2.1 of
2341 	 * the GEM Gigabit Ethernet ASIC Specification.
2342 	 */
2343 
2344 	rxcfg = bus_space_read_4(t, mac, GEM_MAC_RX_CONFIG);
2345 	rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE);
2346 	txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT;
2347 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
2348 		txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS;
2349 	else if (gigabit) {
2350 		rxcfg |= GEM_MAC_RX_CARR_EXTEND;
2351 		txcfg |= GEM_MAC_RX_CARR_EXTEND;
2352 	}
2353 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
2354 	bus_space_barrier(t, mac, GEM_MAC_TX_CONFIG, 4,
2355 	    BUS_SPACE_BARRIER_WRITE);
2356 	if (!gem_bitwait(sc, mac, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
2357 		aprint_normal_dev(&sc->sc_dev, "cannot disable TX MAC\n");
2358 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, txcfg);
2359 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, 0);
2360 	bus_space_barrier(t, mac, GEM_MAC_RX_CONFIG, 4,
2361 	    BUS_SPACE_BARRIER_WRITE);
2362 	if (!gem_bitwait(sc, mac, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
2363 		aprint_normal_dev(&sc->sc_dev, "cannot disable RX MAC\n");
2364 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, rxcfg);
2365 
2366 	v = bus_space_read_4(t, mac, GEM_MAC_CONTROL_CONFIG) &
2367 	    ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE);
2368 	bus_space_write_4(t, mac, GEM_MAC_CONTROL_CONFIG, v);
2369 
2370 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) == 0 &&
2371 	    gigabit != 0)
2372 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
2373 		    GEM_MAC_SLOT_TIME_CARR_EXTEND);
2374 	else
2375 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
2376 		    GEM_MAC_SLOT_TIME_NORMAL);
2377 
2378 	/* XIF Configuration */
2379 	if (sc->sc_flags & GEM_LINK)
2380 		v = GEM_MAC_XIF_LINK_LED;
2381 	else
2382 		v = 0;
2383 	v |= GEM_MAC_XIF_TX_MII_ENA;
2384 
2385 	/* If an external transceiver is connected, enable its MII drivers */
2386 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
2387 	if ((sc->sc_flags &(GEM_SERDES | GEM_SERIAL)) == 0) {
2388 		if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
2389 			/* External MII needs echo disable if half duplex. */
2390 			if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) &
2391 			    IFM_FDX) != 0)
2392 				/* turn on full duplex LED */
2393 				v |= GEM_MAC_XIF_FDPLX_LED;
2394 			else
2395 				/* half duplex -- disable echo */
2396 				v |= GEM_MAC_XIF_ECHO_DISABL;
2397 			if (gigabit)
2398 				v |= GEM_MAC_XIF_GMII_MODE;
2399 			else
2400 				v &= ~GEM_MAC_XIF_GMII_MODE;
2401 		} else
2402 			/* Internal MII needs buf enable */
2403 			v |= GEM_MAC_XIF_MII_BUF_ENA;
2404 	} else {
2405 		if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
2406 			v |= GEM_MAC_XIF_FDPLX_LED;
2407 		v |= GEM_MAC_XIF_GMII_MODE;
2408 	}
2409 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
2410 
2411 	if ((ifp->if_flags & IFF_RUNNING) != 0 &&
2412 	    (sc->sc_flags & GEM_LINK) != 0) {
2413 		bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG,
2414 		    txcfg | GEM_MAC_TX_ENABLE);
2415 		bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG,
2416 		    rxcfg | GEM_MAC_RX_ENABLE);
2417 	}
2418 }
2419 
2420 int
2421 gem_ser_mediachange(struct ifnet *ifp)
2422 {
2423 	struct gem_softc *sc = ifp->if_softc;
2424 	u_int s, t;
2425 
2426 	if (IFM_TYPE(sc->sc_mii.mii_media.ifm_media) != IFM_ETHER)
2427 		return EINVAL;
2428 
2429 	s = IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media);
2430 	if (s == IFM_AUTO) {
2431 		if (sc->sc_mii_media != s) {
2432 #ifdef GEM_DEBUG
2433 			aprint_debug_dev(&sc->sc_dev, "setting media to auto\n");
2434 #endif
2435 			sc->sc_mii_media = s;
2436 			if (ifp->if_flags & IFF_UP) {
2437 				gem_pcs_stop(sc, 0);
2438 				gem_pcs_start(sc);
2439 			}
2440 		}
2441 		return 0;
2442 	}
2443 	if (s == IFM_1000_SX) {
2444 		t = IFM_OPTIONS(sc->sc_mii.mii_media.ifm_media);
2445 		if (t == IFM_FDX || t == IFM_HDX) {
2446 			if (sc->sc_mii_media != t) {
2447 				sc->sc_mii_media = t;
2448 #ifdef GEM_DEBUG
2449 				aprint_debug_dev(&sc->sc_dev,
2450 				    "setting media to 1000baseSX-%s\n",
2451 				    t == IFM_FDX ? "FDX" : "HDX");
2452 #endif
2453 				if (ifp->if_flags & IFF_UP) {
2454 					gem_pcs_stop(sc, 0);
2455 					gem_pcs_start(sc);
2456 				}
2457 			}
2458 			return 0;
2459 		}
2460 	}
2461 	return EINVAL;
2462 }
2463 
2464 void
2465 gem_ser_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2466 {
2467 	struct gem_softc *sc = ifp->if_softc;
2468 
2469 	if ((ifp->if_flags & IFF_UP) == 0)
2470 		return;
2471 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
2472 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
2473 }
2474 
2475 /*
2476  * Process an ioctl request.
2477  */
2478 int
2479 gem_ioctl(ifp, cmd, data)
2480 	struct ifnet *ifp;
2481 	u_long cmd;
2482 	void *data;
2483 {
2484 	struct gem_softc *sc = ifp->if_softc;
2485 	int s, error = 0;
2486 
2487 	s = splnet();
2488 
2489 	switch (cmd) {
2490 	case SIOCSIFFLAGS:
2491 #define RESETIGN (IFF_CANTCHANGE|IFF_DEBUG)
2492 		if (((ifp->if_flags & (IFF_UP|IFF_RUNNING))
2493 		    == (IFF_UP|IFF_RUNNING))
2494 		    && ((ifp->if_flags & (~RESETIGN))
2495 		    == (sc->sc_if_flags & (~RESETIGN)))) {
2496 			gem_setladrf(sc);
2497 			break;
2498 		}
2499 #undef RESETIGN
2500 		/*FALLTHROUGH*/
2501 	default:
2502 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
2503 			break;
2504 
2505 		error = 0;
2506 
2507 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
2508 			;
2509 		else if (ifp->if_flags & IFF_RUNNING) {
2510 			/*
2511 			 * Multicast list has changed; set the hardware filter
2512 			 * accordingly.
2513 			 */
2514 			gem_setladrf(sc);
2515 		}
2516 		break;
2517 	}
2518 
2519 	/* Try to get things going again */
2520 	if (ifp->if_flags & IFF_UP)
2521 		gem_start(ifp);
2522 	splx(s);
2523 	return (error);
2524 }
2525 
2526 
2527 void
2528 gem_shutdown(arg)
2529 	void *arg;
2530 {
2531 	struct gem_softc *sc = (struct gem_softc *)arg;
2532 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2533 
2534 	gem_stop(ifp, 1);
2535 }
2536 
2537 /*
2538  * Set up the logical address filter.
2539  */
2540 void
2541 gem_setladrf(sc)
2542 	struct gem_softc *sc;
2543 {
2544 	struct ethercom *ec = &sc->sc_ethercom;
2545 	struct ifnet *ifp = &ec->ec_if;
2546 	struct ether_multi *enm;
2547 	struct ether_multistep step;
2548 	bus_space_tag_t t = sc->sc_bustag;
2549 	bus_space_handle_t h = sc->sc_h1;
2550 	u_int32_t crc;
2551 	u_int32_t hash[16];
2552 	u_int32_t v;
2553 	int i;
2554 
2555 	/* Get current RX configuration */
2556 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
2557 
2558 	/*
2559 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
2560 	 * and hash filter.  Depending on the case, the right bit will be
2561 	 * enabled.
2562 	 */
2563 	v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER|
2564 	    GEM_MAC_RX_PROMISC_GRP);
2565 
2566 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
2567 		/* Turn on promiscuous mode */
2568 		v |= GEM_MAC_RX_PROMISCUOUS;
2569 		ifp->if_flags |= IFF_ALLMULTI;
2570 		goto chipit;
2571 	}
2572 
2573 	/*
2574 	 * Set up multicast address filter by passing all multicast addresses
2575 	 * through a crc generator, and then using the high order 8 bits as an
2576 	 * index into the 256 bit logical address filter.  The high order 4
2577 	 * bits selects the word, while the other 4 bits select the bit within
2578 	 * the word (where bit 0 is the MSB).
2579 	 */
2580 
2581 	/* Clear hash table */
2582 	memset(hash, 0, sizeof(hash));
2583 
2584 	ETHER_FIRST_MULTI(step, ec, enm);
2585 	while (enm != NULL) {
2586 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2587 			/*
2588 			 * We must listen to a range of multicast addresses.
2589 			 * For now, just accept all multicasts, rather than
2590 			 * trying to set only those filter bits needed to match
2591 			 * the range.  (At this time, the only use of address
2592 			 * ranges is for IP multicast routing, for which the
2593 			 * range is big enough to require all bits set.)
2594 			 * XXX should use the address filters for this
2595 			 */
2596 			ifp->if_flags |= IFF_ALLMULTI;
2597 			v |= GEM_MAC_RX_PROMISC_GRP;
2598 			goto chipit;
2599 		}
2600 
2601 		/* Get the LE CRC32 of the address */
2602 		crc = ether_crc32_le(enm->enm_addrlo, sizeof(enm->enm_addrlo));
2603 
2604 		/* Just want the 8 most significant bits. */
2605 		crc >>= 24;
2606 
2607 		/* Set the corresponding bit in the filter. */
2608 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
2609 
2610 		ETHER_NEXT_MULTI(step, enm);
2611 	}
2612 
2613 	v |= GEM_MAC_RX_HASH_FILTER;
2614 	ifp->if_flags &= ~IFF_ALLMULTI;
2615 
2616 	/* Now load the hash table into the chip (if we are using it) */
2617 	for (i = 0; i < 16; i++) {
2618 		bus_space_write_4(t, h,
2619 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
2620 		    hash[i]);
2621 	}
2622 
2623 chipit:
2624 	sc->sc_if_flags = ifp->if_flags;
2625 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
2626 }
2627 
2628 #if notyet
2629 
2630 /*
2631  * gem_power:
2632  *
2633  *	Power management (suspend/resume) hook.
2634  */
2635 void
2636 gem_power(why, arg)
2637 	int why;
2638 	void *arg;
2639 {
2640 	struct gem_softc *sc = arg;
2641 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2642 	int s;
2643 
2644 	s = splnet();
2645 	switch (why) {
2646 	case PWR_SUSPEND:
2647 	case PWR_STANDBY:
2648 		gem_stop(ifp, 1);
2649 		if (sc->sc_power != NULL)
2650 			(*sc->sc_power)(sc, why);
2651 		break;
2652 	case PWR_RESUME:
2653 		if (ifp->if_flags & IFF_UP) {
2654 			if (sc->sc_power != NULL)
2655 				(*sc->sc_power)(sc, why);
2656 			gem_init(ifp);
2657 		}
2658 		break;
2659 	case PWR_SOFTSUSPEND:
2660 	case PWR_SOFTSTANDBY:
2661 	case PWR_SOFTRESUME:
2662 		break;
2663 	}
2664 	splx(s);
2665 }
2666 #endif
2667