xref: /netbsd-src/sys/dev/pci/if_bge.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: if_bge.c,v 1.195 2011/05/02 09:03:10 jym Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Wind River Systems
5  * Copyright (c) 1997, 1998, 1999, 2001
6  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Bill Paul.
19  * 4. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33  * THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * $FreeBSD: if_bge.c,v 1.13 2002/04/04 06:01:31 wpaul Exp $
36  */
37 
38 /*
39  * Broadcom BCM570x family gigabit ethernet driver for NetBSD.
40  *
41  * NetBSD version by:
42  *
43  *	Frank van der Linden <fvdl@wasabisystems.com>
44  *	Jason Thorpe <thorpej@wasabisystems.com>
45  *	Jonathan Stone <jonathan@dsg.stanford.edu>
46  *
47  * Originally written for FreeBSD by Bill Paul <wpaul@windriver.com>
48  * Senior Engineer, Wind River Systems
49  */
50 
51 /*
52  * The Broadcom BCM5700 is based on technology originally developed by
53  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
54  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
55  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
56  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
57  * frames, highly configurable RX filtering, and 16 RX and TX queues
58  * (which, along with RX filter rules, can be used for QOS applications).
59  * Other features, such as TCP segmentation, may be available as part
60  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
61  * firmware images can be stored in hardware and need not be compiled
62  * into the driver.
63  *
64  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
65  * function in a 32-bit/64-bit 33/66MHz bus, or a 64-bit/133MHz bus.
66  *
67  * The BCM5701 is a single-chip solution incorporating both the BCM5700
68  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
69  * does not support external SSRAM.
70  *
71  * Broadcom also produces a variation of the BCM5700 under the "Altima"
72  * brand name, which is functionally similar but lacks PCI-X support.
73  *
74  * Without external SSRAM, you can only have at most 4 TX rings,
75  * and the use of the mini RX ring is disabled. This seems to imply
76  * that these features are simply not available on the BCM5701. As a
77  * result, this driver does not implement any support for the mini RX
78  * ring.
79  */
80 
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: if_bge.c,v 1.195 2011/05/02 09:03:10 jym Exp $");
83 
84 #include "vlan.h"
85 #include "rnd.h"
86 
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/callout.h>
90 #include <sys/sockio.h>
91 #include <sys/mbuf.h>
92 #include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #include <sys/device.h>
95 #include <sys/socket.h>
96 #include <sys/sysctl.h>
97 
98 #include <net/if.h>
99 #include <net/if_dl.h>
100 #include <net/if_media.h>
101 #include <net/if_ether.h>
102 
103 #if NRND > 0
104 #include <sys/rnd.h>
105 #endif
106 
107 #ifdef INET
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #endif
113 
114 /* Headers for TCP  Segmentation Offload (TSO) */
115 #include <netinet/in_systm.h>		/* n_time for <netinet/ip.h>... */
116 #include <netinet/in.h>			/* ip_{src,dst}, for <netinet/ip.h> */
117 #include <netinet/ip.h>			/* for struct ip */
118 #include <netinet/tcp.h>		/* for struct tcphdr */
119 
120 
121 #include <net/bpf.h>
122 
123 #include <dev/pci/pcireg.h>
124 #include <dev/pci/pcivar.h>
125 #include <dev/pci/pcidevs.h>
126 
127 #include <dev/mii/mii.h>
128 #include <dev/mii/miivar.h>
129 #include <dev/mii/miidevs.h>
130 #include <dev/mii/brgphyreg.h>
131 
132 #include <dev/pci/if_bgereg.h>
133 #include <dev/pci/if_bgevar.h>
134 
135 #include <prop/proplib.h>
136 
137 #define ETHER_MIN_NOPAD (ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
138 
139 
140 /*
141  * Tunable thresholds for rx-side bge interrupt mitigation.
142  */
143 
144 /*
145  * The pairs of values below were obtained from empirical measurement
146  * on bcm5700 rev B2; they ar designed to give roughly 1 receive
147  * interrupt for every N packets received, where N is, approximately,
148  * the second value (rx_max_bds) in each pair.  The values are chosen
149  * such that moving from one pair to the succeeding pair was observed
150  * to roughly halve interrupt rate under sustained input packet load.
151  * The values were empirically chosen to avoid overflowing internal
152  * limits on the  bcm5700: increasing rx_ticks much beyond 600
153  * results in internal wrapping and higher interrupt rates.
154  * The limit of 46 frames was chosen to match NFS workloads.
155  *
156  * These values also work well on bcm5701, bcm5704C, and (less
157  * tested) bcm5703.  On other chipsets, (including the Altima chip
158  * family), the larger values may overflow internal chip limits,
159  * leading to increasing interrupt rates rather than lower interrupt
160  * rates.
161  *
162  * Applications using heavy interrupt mitigation (interrupting every
163  * 32 or 46 frames) in both directions may need to increase the TCP
164  * windowsize to above 131072 bytes (e.g., to 199608 bytes) to sustain
165  * full link bandwidth, due to ACKs and window updates lingering
166  * in the RX queue during the 30-to-40-frame interrupt-mitigation window.
167  */
168 static const struct bge_load_rx_thresh {
169 	int rx_ticks;
170 	int rx_max_bds; }
171 bge_rx_threshes[] = {
172 	{ 32,   2 },
173 	{ 50,   4 },
174 	{ 100,  8 },
175 	{ 192, 16 },
176 	{ 416, 32 },
177 	{ 598, 46 }
178 };
179 #define NBGE_RX_THRESH (sizeof(bge_rx_threshes) / sizeof(bge_rx_threshes[0]))
180 
181 /* XXX patchable; should be sysctl'able */
182 static int bge_auto_thresh = 1;
183 static int bge_rx_thresh_lvl;
184 
185 static int bge_rxthresh_nodenum;
186 
187 typedef int (*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
188 
189 static int bge_probe(device_t, cfdata_t, void *);
190 static void bge_attach(device_t, device_t, void *);
191 static void bge_release_resources(struct bge_softc *);
192 
193 static int bge_get_eaddr_fw(struct bge_softc *, uint8_t[]);
194 static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
195 static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
196 static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
197 static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
198 
199 static void bge_txeof(struct bge_softc *);
200 static void bge_rxeof(struct bge_softc *);
201 
202 static void bge_asf_driver_up (struct bge_softc *);
203 static void bge_tick(void *);
204 static void bge_stats_update(struct bge_softc *);
205 static void bge_stats_update_regs(struct bge_softc *);
206 static int bge_encap(struct bge_softc *, struct mbuf *, uint32_t *);
207 
208 static int bge_intr(void *);
209 static void bge_start(struct ifnet *);
210 static int bge_ifflags_cb(struct ethercom *);
211 static int bge_ioctl(struct ifnet *, u_long, void *);
212 static int bge_init(struct ifnet *);
213 static void bge_stop(struct ifnet *, int);
214 static void bge_watchdog(struct ifnet *);
215 static int bge_ifmedia_upd(struct ifnet *);
216 static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
217 
218 static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
219 static int bge_read_nvram(struct bge_softc *, uint8_t *, int, int);
220 
221 static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
222 static int bge_read_eeprom(struct bge_softc *, void *, int, int);
223 static void bge_setmulti(struct bge_softc *);
224 
225 static void bge_handle_events(struct bge_softc *);
226 static int bge_alloc_jumbo_mem(struct bge_softc *);
227 #if 0 /* XXX */
228 static void bge_free_jumbo_mem(struct bge_softc *);
229 #endif
230 static void *bge_jalloc(struct bge_softc *);
231 static void bge_jfree(struct mbuf *, void *, size_t, void *);
232 static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *,
233 			       bus_dmamap_t);
234 static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
235 static int bge_init_rx_ring_std(struct bge_softc *);
236 static void bge_free_rx_ring_std(struct bge_softc *);
237 static int bge_init_rx_ring_jumbo(struct bge_softc *);
238 static void bge_free_rx_ring_jumbo(struct bge_softc *);
239 static void bge_free_tx_ring(struct bge_softc *);
240 static int bge_init_tx_ring(struct bge_softc *);
241 
242 static int bge_chipinit(struct bge_softc *);
243 static int bge_blockinit(struct bge_softc *);
244 static int bge_setpowerstate(struct bge_softc *, int);
245 static uint32_t bge_readmem_ind(struct bge_softc *, int);
246 static void bge_writemem_ind(struct bge_softc *, int, int);
247 static void bge_writembx(struct bge_softc *, int, int);
248 static void bge_writemem_direct(struct bge_softc *, int, int);
249 static void bge_writereg_ind(struct bge_softc *, int, int);
250 static void bge_set_max_readrq(struct bge_softc *);
251 
252 static int bge_miibus_readreg(device_t, int, int);
253 static void bge_miibus_writereg(device_t, int, int, int);
254 static void bge_miibus_statchg(device_t);
255 
256 #define	BGE_RESET_START 1
257 #define	BGE_RESET_STOP  2
258 static void bge_sig_post_reset(struct bge_softc *, int);
259 static void bge_sig_legacy(struct bge_softc *, int);
260 static void bge_sig_pre_reset(struct bge_softc *, int);
261 static void bge_stop_fw(struct bge_softc *);
262 static int bge_reset(struct bge_softc *);
263 static void bge_link_upd(struct bge_softc *);
264 static void sysctl_bge_init(struct bge_softc *);
265 static int sysctl_bge_verify(SYSCTLFN_PROTO);
266 
267 #ifdef BGE_DEBUG
268 #define DPRINTF(x)	if (bgedebug) printf x
269 #define DPRINTFN(n,x)	if (bgedebug >= (n)) printf x
270 #define BGE_TSO_PRINTF(x)  do { if (bge_tso_debug) printf x ;} while (0)
271 int	bgedebug = 0;
272 int	bge_tso_debug = 0;
273 void		bge_debug_info(struct bge_softc *);
274 #else
275 #define DPRINTF(x)
276 #define DPRINTFN(n,x)
277 #define BGE_TSO_PRINTF(x)
278 #endif
279 
280 #ifdef BGE_EVENT_COUNTERS
281 #define	BGE_EVCNT_INCR(ev)	(ev).ev_count++
282 #define	BGE_EVCNT_ADD(ev, val)	(ev).ev_count += (val)
283 #define	BGE_EVCNT_UPD(ev, val)	(ev).ev_count = (val)
284 #else
285 #define	BGE_EVCNT_INCR(ev)	/* nothing */
286 #define	BGE_EVCNT_ADD(ev, val)	/* nothing */
287 #define	BGE_EVCNT_UPD(ev, val)	/* nothing */
288 #endif
289 
290 static const struct bge_product {
291 	pci_vendor_id_t		bp_vendor;
292 	pci_product_id_t	bp_product;
293 	const char		*bp_name;
294 } bge_products[] = {
295 	/*
296 	 * The BCM5700 documentation seems to indicate that the hardware
297 	 * still has the Alteon vendor ID burned into it, though it
298 	 * should always be overridden by the value in the EEPROM.  We'll
299 	 * check for it anyway.
300 	 */
301 	{ PCI_VENDOR_ALTEON,
302 	  PCI_PRODUCT_ALTEON_BCM5700,
303 	  "Broadcom BCM5700 Gigabit Ethernet",
304 	  },
305 	{ PCI_VENDOR_ALTEON,
306 	  PCI_PRODUCT_ALTEON_BCM5701,
307 	  "Broadcom BCM5701 Gigabit Ethernet",
308 	  },
309 	{ PCI_VENDOR_ALTIMA,
310 	  PCI_PRODUCT_ALTIMA_AC1000,
311 	  "Altima AC1000 Gigabit Ethernet",
312 	  },
313 	{ PCI_VENDOR_ALTIMA,
314 	  PCI_PRODUCT_ALTIMA_AC1001,
315 	  "Altima AC1001 Gigabit Ethernet",
316 	   },
317 	{ PCI_VENDOR_ALTIMA,
318 	  PCI_PRODUCT_ALTIMA_AC9100,
319 	  "Altima AC9100 Gigabit Ethernet",
320 	  },
321 	{ PCI_VENDOR_BROADCOM,
322 	  PCI_PRODUCT_BROADCOM_BCM5700,
323 	  "Broadcom BCM5700 Gigabit Ethernet",
324 	  },
325 	{ PCI_VENDOR_BROADCOM,
326 	  PCI_PRODUCT_BROADCOM_BCM5701,
327 	  "Broadcom BCM5701 Gigabit Ethernet",
328 	  },
329 	{ PCI_VENDOR_BROADCOM,
330 	  PCI_PRODUCT_BROADCOM_BCM5702,
331 	  "Broadcom BCM5702 Gigabit Ethernet",
332 	  },
333 	{ PCI_VENDOR_BROADCOM,
334 	  PCI_PRODUCT_BROADCOM_BCM5702X,
335 	  "Broadcom BCM5702X Gigabit Ethernet" },
336 	{ PCI_VENDOR_BROADCOM,
337 	  PCI_PRODUCT_BROADCOM_BCM5703,
338 	  "Broadcom BCM5703 Gigabit Ethernet",
339 	  },
340 	{ PCI_VENDOR_BROADCOM,
341 	  PCI_PRODUCT_BROADCOM_BCM5703X,
342 	  "Broadcom BCM5703X Gigabit Ethernet",
343 	  },
344 	{ PCI_VENDOR_BROADCOM,
345 	  PCI_PRODUCT_BROADCOM_BCM5703_ALT,
346 	  "Broadcom BCM5703 Gigabit Ethernet",
347 	  },
348 	{ PCI_VENDOR_BROADCOM,
349 	  PCI_PRODUCT_BROADCOM_BCM5704C,
350 	  "Broadcom BCM5704C Dual Gigabit Ethernet",
351 	  },
352 	{ PCI_VENDOR_BROADCOM,
353 	  PCI_PRODUCT_BROADCOM_BCM5704S,
354 	  "Broadcom BCM5704S Dual Gigabit Ethernet",
355 	  },
356 	{ PCI_VENDOR_BROADCOM,
357 	  PCI_PRODUCT_BROADCOM_BCM5705,
358 	  "Broadcom BCM5705 Gigabit Ethernet",
359 	  },
360 	{ PCI_VENDOR_BROADCOM,
361 	  PCI_PRODUCT_BROADCOM_BCM5705F,
362 	  "Broadcom BCM5705F Gigabit Ethernet",
363 	  },
364 	{ PCI_VENDOR_BROADCOM,
365 	  PCI_PRODUCT_BROADCOM_BCM5705K,
366 	  "Broadcom BCM5705K Gigabit Ethernet",
367 	  },
368 	{ PCI_VENDOR_BROADCOM,
369 	  PCI_PRODUCT_BROADCOM_BCM5705M,
370 	  "Broadcom BCM5705M Gigabit Ethernet",
371 	  },
372 	{ PCI_VENDOR_BROADCOM,
373 	  PCI_PRODUCT_BROADCOM_BCM5705M_ALT,
374 	  "Broadcom BCM5705M Gigabit Ethernet",
375 	  },
376 	{ PCI_VENDOR_BROADCOM,
377 	  PCI_PRODUCT_BROADCOM_BCM5714,
378 	  "Broadcom BCM5714 Gigabit Ethernet",
379 	  },
380 	{ PCI_VENDOR_BROADCOM,
381 	  PCI_PRODUCT_BROADCOM_BCM5714S,
382 	  "Broadcom BCM5714S Gigabit Ethernet",
383 	  },
384 	{ PCI_VENDOR_BROADCOM,
385 	  PCI_PRODUCT_BROADCOM_BCM5715,
386 	  "Broadcom BCM5715 Gigabit Ethernet",
387 	  },
388 	{ PCI_VENDOR_BROADCOM,
389 	  PCI_PRODUCT_BROADCOM_BCM5715S,
390 	  "Broadcom BCM5715S Gigabit Ethernet",
391 	  },
392 	{ PCI_VENDOR_BROADCOM,
393 	  PCI_PRODUCT_BROADCOM_BCM5717,
394 	  "Broadcom BCM5717 Gigabit Ethernet",
395 	  },
396 	{ PCI_VENDOR_BROADCOM,
397 	  PCI_PRODUCT_BROADCOM_BCM5718,
398 	  "Broadcom BCM5718 Gigabit Ethernet",
399 	  },
400 	{ PCI_VENDOR_BROADCOM,
401 	  PCI_PRODUCT_BROADCOM_BCM5720,
402 	  "Broadcom BCM5720 Gigabit Ethernet",
403 	  },
404 	{ PCI_VENDOR_BROADCOM,
405 	  PCI_PRODUCT_BROADCOM_BCM5721,
406 	  "Broadcom BCM5721 Gigabit Ethernet",
407 	  },
408 	{ PCI_VENDOR_BROADCOM,
409 	  PCI_PRODUCT_BROADCOM_BCM5722,
410 	  "Broadcom BCM5722 Gigabit Ethernet",
411 	  },
412 	{ PCI_VENDOR_BROADCOM,
413 	  PCI_PRODUCT_BROADCOM_BCM5723,
414 	  "Broadcom BCM5723 Gigabit Ethernet",
415 	  },
416 	{ PCI_VENDOR_BROADCOM,
417 	  PCI_PRODUCT_BROADCOM_BCM5724,
418 	  "Broadcom BCM5724 Gigabit Ethernet",
419 	  },
420 	{ PCI_VENDOR_BROADCOM,
421 	  PCI_PRODUCT_BROADCOM_BCM5750,
422 	  "Broadcom BCM5750 Gigabit Ethernet",
423 	  },
424 	{ PCI_VENDOR_BROADCOM,
425 	  PCI_PRODUCT_BROADCOM_BCM5750M,
426 	  "Broadcom BCM5750M Gigabit Ethernet",
427 	  },
428 	{ PCI_VENDOR_BROADCOM,
429 	  PCI_PRODUCT_BROADCOM_BCM5751,
430 	  "Broadcom BCM5751 Gigabit Ethernet",
431 	  },
432 	{ PCI_VENDOR_BROADCOM,
433 	  PCI_PRODUCT_BROADCOM_BCM5751F,
434 	  "Broadcom BCM5751F Gigabit Ethernet",
435 	  },
436 	{ PCI_VENDOR_BROADCOM,
437 	  PCI_PRODUCT_BROADCOM_BCM5751M,
438 	  "Broadcom BCM5751M Gigabit Ethernet",
439 	  },
440 	{ PCI_VENDOR_BROADCOM,
441 	  PCI_PRODUCT_BROADCOM_BCM5752,
442 	  "Broadcom BCM5752 Gigabit Ethernet",
443 	  },
444 	{ PCI_VENDOR_BROADCOM,
445 	  PCI_PRODUCT_BROADCOM_BCM5752M,
446 	  "Broadcom BCM5752M Gigabit Ethernet",
447 	  },
448 	{ PCI_VENDOR_BROADCOM,
449 	  PCI_PRODUCT_BROADCOM_BCM5753,
450 	  "Broadcom BCM5753 Gigabit Ethernet",
451 	  },
452 	{ PCI_VENDOR_BROADCOM,
453 	  PCI_PRODUCT_BROADCOM_BCM5753F,
454 	  "Broadcom BCM5753F Gigabit Ethernet",
455 	  },
456 	{ PCI_VENDOR_BROADCOM,
457 	  PCI_PRODUCT_BROADCOM_BCM5753M,
458 	  "Broadcom BCM5753M Gigabit Ethernet",
459 	  },
460 	{ PCI_VENDOR_BROADCOM,
461 	  PCI_PRODUCT_BROADCOM_BCM5754,
462 	  "Broadcom BCM5754 Gigabit Ethernet",
463 	},
464 	{ PCI_VENDOR_BROADCOM,
465 	  PCI_PRODUCT_BROADCOM_BCM5754M,
466 	  "Broadcom BCM5754M Gigabit Ethernet",
467 	},
468 	{ PCI_VENDOR_BROADCOM,
469 	  PCI_PRODUCT_BROADCOM_BCM5755,
470 	  "Broadcom BCM5755 Gigabit Ethernet",
471 	},
472 	{ PCI_VENDOR_BROADCOM,
473 	  PCI_PRODUCT_BROADCOM_BCM5755M,
474 	  "Broadcom BCM5755M Gigabit Ethernet",
475 	},
476 	{ PCI_VENDOR_BROADCOM,
477 	  PCI_PRODUCT_BROADCOM_BCM5756,
478 	  "Broadcom BCM5756 Gigabit Ethernet",
479 	},
480 	{ PCI_VENDOR_BROADCOM,
481 	  PCI_PRODUCT_BROADCOM_BCM5761,
482 	  "Broadcom BCM5761 Gigabit Ethernet",
483 	},
484 	{ PCI_VENDOR_BROADCOM,
485 	  PCI_PRODUCT_BROADCOM_BCM5761E,
486 	  "Broadcom BCM5761E Gigabit Ethernet",
487 	},
488 	{ PCI_VENDOR_BROADCOM,
489 	  PCI_PRODUCT_BROADCOM_BCM5761S,
490 	  "Broadcom BCM5761S Gigabit Ethernet",
491 	},
492 	{ PCI_VENDOR_BROADCOM,
493 	  PCI_PRODUCT_BROADCOM_BCM5761SE,
494 	  "Broadcom BCM5761SE Gigabit Ethernet",
495 	},
496 	{ PCI_VENDOR_BROADCOM,
497 	  PCI_PRODUCT_BROADCOM_BCM5764,
498 	  "Broadcom BCM5764 Gigabit Ethernet",
499 	  },
500 	{ PCI_VENDOR_BROADCOM,
501 	  PCI_PRODUCT_BROADCOM_BCM5780,
502 	  "Broadcom BCM5780 Gigabit Ethernet",
503 	  },
504 	{ PCI_VENDOR_BROADCOM,
505 	  PCI_PRODUCT_BROADCOM_BCM5780S,
506 	  "Broadcom BCM5780S Gigabit Ethernet",
507 	  },
508 	{ PCI_VENDOR_BROADCOM,
509 	  PCI_PRODUCT_BROADCOM_BCM5781,
510 	  "Broadcom BCM5781 Gigabit Ethernet",
511 	  },
512 	{ PCI_VENDOR_BROADCOM,
513 	  PCI_PRODUCT_BROADCOM_BCM5782,
514 	  "Broadcom BCM5782 Gigabit Ethernet",
515 	},
516 	{ PCI_VENDOR_BROADCOM,
517 	  PCI_PRODUCT_BROADCOM_BCM5784M,
518 	  "BCM5784M NetLink 1000baseT Ethernet",
519 	},
520 	{ PCI_VENDOR_BROADCOM,
521 	  PCI_PRODUCT_BROADCOM_BCM5786,
522 	  "Broadcom BCM5786 Gigabit Ethernet",
523 	},
524 	{ PCI_VENDOR_BROADCOM,
525 	  PCI_PRODUCT_BROADCOM_BCM5787,
526 	  "Broadcom BCM5787 Gigabit Ethernet",
527 	},
528 	{ PCI_VENDOR_BROADCOM,
529 	  PCI_PRODUCT_BROADCOM_BCM5787M,
530 	  "Broadcom BCM5787M Gigabit Ethernet",
531 	},
532 	{ PCI_VENDOR_BROADCOM,
533 	  PCI_PRODUCT_BROADCOM_BCM5788,
534 	  "Broadcom BCM5788 Gigabit Ethernet",
535 	  },
536 	{ PCI_VENDOR_BROADCOM,
537 	  PCI_PRODUCT_BROADCOM_BCM5789,
538 	  "Broadcom BCM5789 Gigabit Ethernet",
539 	  },
540 	{ PCI_VENDOR_BROADCOM,
541 	  PCI_PRODUCT_BROADCOM_BCM5901,
542 	  "Broadcom BCM5901 Fast Ethernet",
543 	  },
544 	{ PCI_VENDOR_BROADCOM,
545 	  PCI_PRODUCT_BROADCOM_BCM5901A2,
546 	  "Broadcom BCM5901A2 Fast Ethernet",
547 	  },
548 	{ PCI_VENDOR_BROADCOM,
549 	  PCI_PRODUCT_BROADCOM_BCM5903M,
550 	  "Broadcom BCM5903M Fast Ethernet",
551 	  },
552 	{ PCI_VENDOR_BROADCOM,
553 	  PCI_PRODUCT_BROADCOM_BCM5906,
554 	  "Broadcom BCM5906 Fast Ethernet",
555 	  },
556 	{ PCI_VENDOR_BROADCOM,
557 	  PCI_PRODUCT_BROADCOM_BCM5906M,
558 	  "Broadcom BCM5906M Fast Ethernet",
559 	  },
560 	{ PCI_VENDOR_BROADCOM,
561 	  PCI_PRODUCT_BROADCOM_BCM57760,
562 	  "Broadcom BCM57760 Fast Ethernet",
563 	  },
564 	{ PCI_VENDOR_BROADCOM,
565 	  PCI_PRODUCT_BROADCOM_BCM57761,
566 	  "Broadcom BCM57761 Fast Ethernet",
567 	  },
568 	{ PCI_VENDOR_BROADCOM,
569 	  PCI_PRODUCT_BROADCOM_BCM57765,
570 	  "Broadcom BCM57765 Fast Ethernet",
571 	  },
572 	{ PCI_VENDOR_BROADCOM,
573 	  PCI_PRODUCT_BROADCOM_BCM57780,
574 	  "Broadcom BCM57780 Fast Ethernet",
575 	  },
576 	{ PCI_VENDOR_BROADCOM,
577 	  PCI_PRODUCT_BROADCOM_BCM57781,
578 	  "Broadcom BCM57781 Fast Ethernet",
579 	  },
580 	{ PCI_VENDOR_BROADCOM,
581 	  PCI_PRODUCT_BROADCOM_BCM57785,
582 	  "Broadcom BCM57785 Fast Ethernet",
583 	  },
584 	{ PCI_VENDOR_BROADCOM,
585 	  PCI_PRODUCT_BROADCOM_BCM57788,
586 	  "Broadcom BCM57788 Fast Ethernet",
587 	  },
588 	{ PCI_VENDOR_BROADCOM,
589 	  PCI_PRODUCT_BROADCOM_BCM57790,
590 	  "Broadcom BCM57790 Fast Ethernet",
591 	  },
592 	{ PCI_VENDOR_BROADCOM,
593 	  PCI_PRODUCT_BROADCOM_BCM57791,
594 	  "Broadcom BCM57791 Fast Ethernet",
595 	  },
596 	{ PCI_VENDOR_BROADCOM,
597 	  PCI_PRODUCT_BROADCOM_BCM57795,
598 	  "Broadcom BCM57795 Fast Ethernet",
599 	  },
600 	{ PCI_VENDOR_SCHNEIDERKOCH,
601 	  PCI_PRODUCT_SCHNEIDERKOCH_SK_9DX1,
602 	  "SysKonnect SK-9Dx1 Gigabit Ethernet",
603 	  },
604 	{ PCI_VENDOR_3COM,
605 	  PCI_PRODUCT_3COM_3C996,
606 	  "3Com 3c996 Gigabit Ethernet",
607 	  },
608 	{ 0,
609 	  0,
610 	  NULL },
611 };
612 
613 /*
614  * XXX: how to handle variants based on 5750 and derivatives:
615  * 5750 5751, 5721, possibly 5714, 5752, and 5708?, which
616  * in general behave like a 5705, except with additional quirks.
617  * This driver's current handling of the 5721 is wrong;
618  * how we map ASIC revision to "quirks" needs more thought.
619  * (defined here until the thought is done).
620  */
621 #define BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_5700_FAMILY)
622 #define BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_5714_FAMILY)
623 #define BGE_IS_5705_PLUS(sc)	((sc)->bge_flags & BGE_5705_PLUS)
624 #define BGE_IS_5750_OR_BEYOND(sc)	((sc)->bge_flags & BGE_5750_PLUS)
625 #define BGE_IS_5755_PLUS(sc)	((sc)->bge_flags & BGE_5755_PLUS)
626 #define BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_JUMBO_CAPABLE)
627 
628 static const struct bge_revision {
629 	uint32_t		br_chipid;
630 	const char		*br_name;
631 } bge_revisions[] = {
632 	{ BGE_CHIPID_BCM5700_A0, "BCM5700 A0" },
633 	{ BGE_CHIPID_BCM5700_A1, "BCM5700 A1" },
634 	{ BGE_CHIPID_BCM5700_B0, "BCM5700 B0" },
635 	{ BGE_CHIPID_BCM5700_B1, "BCM5700 B1" },
636 	{ BGE_CHIPID_BCM5700_B2, "BCM5700 B2" },
637 	{ BGE_CHIPID_BCM5700_B3, "BCM5700 B3" },
638 	/* This is treated like a BCM5700 Bx */
639 	{ BGE_CHIPID_BCM5700_ALTIMA, "BCM5700 Altima" },
640 	{ BGE_CHIPID_BCM5700_C0, "BCM5700 C0" },
641 	{ BGE_CHIPID_BCM5701_A0, "BCM5701 A0" },
642 	{ BGE_CHIPID_BCM5701_B0, "BCM5701 B0" },
643 	{ BGE_CHIPID_BCM5701_B2, "BCM5701 B2" },
644 	{ BGE_CHIPID_BCM5701_B5, "BCM5701 B5" },
645 	{ BGE_CHIPID_BCM5703_A0, "BCM5702/5703 A0" },
646 	{ BGE_CHIPID_BCM5703_A1, "BCM5702/5703 A1" },
647 	{ BGE_CHIPID_BCM5703_A2, "BCM5702/5703 A2" },
648 	{ BGE_CHIPID_BCM5703_A3, "BCM5702/5703 A3" },
649 	{ BGE_CHIPID_BCM5703_B0, "BCM5702/5703 B0" },
650 	{ BGE_CHIPID_BCM5704_A0, "BCM5704 A0" },
651 	{ BGE_CHIPID_BCM5704_A1, "BCM5704 A1" },
652 	{ BGE_CHIPID_BCM5704_A2, "BCM5704 A2" },
653 	{ BGE_CHIPID_BCM5704_A3, "BCM5704 A3" },
654 	{ BGE_CHIPID_BCM5704_B0, "BCM5704 B0" },
655 	{ BGE_CHIPID_BCM5705_A0, "BCM5705 A0" },
656 	{ BGE_CHIPID_BCM5705_A1, "BCM5705 A1" },
657 	{ BGE_CHIPID_BCM5705_A2, "BCM5705 A2" },
658 	{ BGE_CHIPID_BCM5705_A3, "BCM5705 A3" },
659 	{ BGE_CHIPID_BCM5750_A0, "BCM5750 A0" },
660 	{ BGE_CHIPID_BCM5750_A1, "BCM5750 A1" },
661 	{ BGE_CHIPID_BCM5750_A3, "BCM5750 A3" },
662 	{ BGE_CHIPID_BCM5750_B0, "BCM5750 B0" },
663 	{ BGE_CHIPID_BCM5750_B1, "BCM5750 B1" },
664 	{ BGE_CHIPID_BCM5750_C0, "BCM5750 C0" },
665 	{ BGE_CHIPID_BCM5750_C1, "BCM5750 C1" },
666 	{ BGE_CHIPID_BCM5750_C2, "BCM5750 C2" },
667 	{ BGE_CHIPID_BCM5752_A0, "BCM5752 A0" },
668 	{ BGE_CHIPID_BCM5752_A1, "BCM5752 A1" },
669 	{ BGE_CHIPID_BCM5752_A2, "BCM5752 A2" },
670 	{ BGE_CHIPID_BCM5714_A0, "BCM5714 A0" },
671 	{ BGE_CHIPID_BCM5714_B0, "BCM5714 B0" },
672 	{ BGE_CHIPID_BCM5714_B3, "BCM5714 B3" },
673 	{ BGE_CHIPID_BCM5715_A0, "BCM5715 A0" },
674 	{ BGE_CHIPID_BCM5715_A1, "BCM5715 A1" },
675 	{ BGE_CHIPID_BCM5715_A3, "BCM5715 A3" },
676 	{ BGE_CHIPID_BCM5755_A0, "BCM5755 A0" },
677 	{ BGE_CHIPID_BCM5755_A1, "BCM5755 A1" },
678 	{ BGE_CHIPID_BCM5755_A2, "BCM5755 A2" },
679 	{ BGE_CHIPID_BCM5755_C0, "BCM5755 C0" },
680 	{ BGE_CHIPID_BCM5761_A0, "BCM5761 A0" },
681 	{ BGE_CHIPID_BCM5761_A1, "BCM5761 A1" },
682 	{ BGE_CHIPID_BCM5784_A0, "BCM5784 A0" },
683 	{ BGE_CHIPID_BCM5784_A1, "BCM5784 A1" },
684 	/* 5754 and 5787 share the same ASIC ID */
685 	{ BGE_CHIPID_BCM5787_A0, "BCM5754/5787 A0" },
686 	{ BGE_CHIPID_BCM5787_A1, "BCM5754/5787 A1" },
687 	{ BGE_CHIPID_BCM5787_A2, "BCM5754/5787 A2" },
688 	{ BGE_CHIPID_BCM5906_A1, "BCM5906 A1" },
689 	{ BGE_CHIPID_BCM5906_A2, "BCM5906 A2" },
690 	{ BGE_CHIPID_BCM57780_A0, "BCM57780 A0" },
691 	{ BGE_CHIPID_BCM57780_A1, "BCM57780 A1" },
692 
693 	{ 0, NULL }
694 };
695 
696 /*
697  * Some defaults for major revisions, so that newer steppings
698  * that we don't know about have a shot at working.
699  */
700 static const struct bge_revision bge_majorrevs[] = {
701 	{ BGE_ASICREV_BCM5700, "unknown BCM5700" },
702 	{ BGE_ASICREV_BCM5701, "unknown BCM5701" },
703 	{ BGE_ASICREV_BCM5703, "unknown BCM5703" },
704 	{ BGE_ASICREV_BCM5704, "unknown BCM5704" },
705 	{ BGE_ASICREV_BCM5705, "unknown BCM5705" },
706 	{ BGE_ASICREV_BCM5750, "unknown BCM5750" },
707 	{ BGE_ASICREV_BCM5714_A0, "unknown BCM5714" },
708 	{ BGE_ASICREV_BCM5752, "unknown BCM5752" },
709 	{ BGE_ASICREV_BCM5780, "unknown BCM5780" },
710 	{ BGE_ASICREV_BCM5714, "unknown BCM5714" },
711 	{ BGE_ASICREV_BCM5755, "unknown BCM5755" },
712 	{ BGE_ASICREV_BCM5761, "unknown BCM5761" },
713 	{ BGE_ASICREV_BCM5784, "unknown BCM5784" },
714 	{ BGE_ASICREV_BCM5785, "unknown BCM5785" },
715 	/* 5754 and 5787 share the same ASIC ID */
716 	{ BGE_ASICREV_BCM5787, "unknown BCM5754/5787" },
717 	{ BGE_ASICREV_BCM5906, "unknown BCM5906" },
718 	{ BGE_ASICREV_BCM57780, "unknown BCM57780" },
719 	{ BGE_ASICREV_BCM5717, "unknown BCM5717" },
720 	{ BGE_ASICREV_BCM57765, "unknown BCM57765" },
721 
722 	{ 0, NULL }
723 };
724 
725 static int bge_allow_asf = 1;
726 
727 CFATTACH_DECL_NEW(bge, sizeof(struct bge_softc),
728     bge_probe, bge_attach, NULL, NULL);
729 
730 static uint32_t
731 bge_readmem_ind(struct bge_softc *sc, int off)
732 {
733 	pcireg_t val;
734 
735 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, off);
736 	val = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_DATA);
737 	return val;
738 }
739 
740 static void
741 bge_writemem_ind(struct bge_softc *sc, int off, int val)
742 {
743 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, off);
744 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_DATA, val);
745 }
746 
747 /*
748  * PCI Express only
749  */
750 static void
751 bge_set_max_readrq(struct bge_softc *sc)
752 {
753 	pcireg_t val;
754 
755 	val = pci_conf_read(sc->sc_pc, sc->sc_pcitag, sc->bge_pciecap
756 	    + PCI_PCIE_DCSR);
757 	if ((val & PCI_PCIE_DCSR_MAX_READ_REQ) !=
758 	    BGE_PCIE_DEVCTL_MAX_READRQ_4096) {
759 		aprint_verbose_dev(sc->bge_dev,
760 		    "adjust device control 0x%04x ", val);
761 		val &= ~PCI_PCIE_DCSR_MAX_READ_REQ;
762 		val |= BGE_PCIE_DEVCTL_MAX_READRQ_4096;
763 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, sc->bge_pciecap
764 		    + PCI_PCIE_DCSR, val);
765 		aprint_verbose("-> 0x%04x\n", val);
766 	}
767 }
768 
769 #ifdef notdef
770 static uint32_t
771 bge_readreg_ind(struct bge_softc *sc, int off)
772 {
773 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_BASEADDR, off);
774 	return (pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_DATA));
775 }
776 #endif
777 
778 static void
779 bge_writereg_ind(struct bge_softc *sc, int off, int val)
780 {
781 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_BASEADDR, off);
782 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_DATA, val);
783 }
784 
785 static void
786 bge_writemem_direct(struct bge_softc *sc, int off, int val)
787 {
788 	CSR_WRITE_4(sc, off, val);
789 }
790 
791 static void
792 bge_writembx(struct bge_softc *sc, int off, int val)
793 {
794 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
795 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
796 
797 	CSR_WRITE_4(sc, off, val);
798 }
799 
800 static uint8_t
801 bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
802 {
803 	uint32_t access, byte = 0;
804 	int i;
805 
806 	/* Lock. */
807 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
808 	for (i = 0; i < 8000; i++) {
809 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
810 			break;
811 		DELAY(20);
812 	}
813 	if (i == 8000)
814 		return 1;
815 
816 	/* Enable access. */
817 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
818 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
819 
820 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
821 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
822 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
823 		DELAY(10);
824 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
825 			DELAY(10);
826 			break;
827 		}
828 	}
829 
830 	if (i == BGE_TIMEOUT * 10) {
831 		aprint_error_dev(sc->bge_dev, "nvram read timed out\n");
832 		return 1;
833 	}
834 
835 	/* Get result. */
836 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
837 
838 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
839 
840 	/* Disable access. */
841 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
842 
843 	/* Unlock. */
844 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
845 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
846 
847 	return 0;
848 }
849 
850 /*
851  * Read a sequence of bytes from NVRAM.
852  */
853 static int
854 bge_read_nvram(struct bge_softc *sc, uint8_t *dest, int off, int cnt)
855 {
856 	int err = 0, i;
857 	uint8_t byte = 0;
858 
859 	if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)
860 		return 1;
861 
862 	for (i = 0; i < cnt; i++) {
863 		err = bge_nvram_getbyte(sc, off + i, &byte);
864 		if (err)
865 			break;
866 		*(dest + i) = byte;
867 	}
868 
869 	return (err ? 1 : 0);
870 }
871 
872 /*
873  * Read a byte of data stored in the EEPROM at address 'addr.' The
874  * BCM570x supports both the traditional bitbang interface and an
875  * auto access interface for reading the EEPROM. We use the auto
876  * access method.
877  */
878 static uint8_t
879 bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
880 {
881 	int i;
882 	uint32_t byte = 0;
883 
884 	/*
885 	 * Enable use of auto EEPROM access so we can avoid
886 	 * having to use the bitbang method.
887 	 */
888 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
889 
890 	/* Reset the EEPROM, load the clock period. */
891 	CSR_WRITE_4(sc, BGE_EE_ADDR,
892 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
893 	DELAY(20);
894 
895 	/* Issue the read EEPROM command. */
896 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
897 
898 	/* Wait for completion */
899 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
900 		DELAY(10);
901 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
902 			break;
903 	}
904 
905 	if (i == BGE_TIMEOUT * 10) {
906 		aprint_error_dev(sc->bge_dev, "eeprom read timed out\n");
907 		return 1;
908 	}
909 
910 	/* Get result. */
911 	byte = CSR_READ_4(sc, BGE_EE_DATA);
912 
913 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
914 
915 	return 0;
916 }
917 
918 /*
919  * Read a sequence of bytes from the EEPROM.
920  */
921 static int
922 bge_read_eeprom(struct bge_softc *sc, void *destv, int off, int cnt)
923 {
924 	int err = 0, i;
925 	uint8_t byte = 0;
926 	char *dest = destv;
927 
928 	for (i = 0; i < cnt; i++) {
929 		err = bge_eeprom_getbyte(sc, off + i, &byte);
930 		if (err)
931 			break;
932 		*(dest + i) = byte;
933 	}
934 
935 	return (err ? 1 : 0);
936 }
937 
938 static int
939 bge_miibus_readreg(device_t dev, int phy, int reg)
940 {
941 	struct bge_softc *sc = device_private(dev);
942 	uint32_t val;
943 	uint32_t autopoll;
944 	int i;
945 
946 	/*
947 	 * Broadcom's own driver always assumes the internal
948 	 * PHY is at GMII address 1. On some chips, the PHY responds
949 	 * to accesses at all addresses, which could cause us to
950 	 * bogusly attach the PHY 32 times at probe type. Always
951 	 * restricting the lookup to address 1 is simpler than
952 	 * trying to figure out which chips revisions should be
953 	 * special-cased.
954 	 */
955 	if (phy != 1)
956 		return 0;
957 
958 	/* Reading with autopolling on may trigger PCI errors */
959 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
960 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
961 		BGE_STS_CLRBIT(sc, BGE_STS_AUTOPOLL);
962 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
963 		DELAY(40);
964 	}
965 
966 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
967 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
968 
969 	for (i = 0; i < BGE_TIMEOUT; i++) {
970 		val = CSR_READ_4(sc, BGE_MI_COMM);
971 		if (!(val & BGE_MICOMM_BUSY))
972 			break;
973 		delay(10);
974 	}
975 
976 	if (i == BGE_TIMEOUT) {
977 		aprint_error_dev(sc->bge_dev, "PHY read timed out\n");
978 		val = 0;
979 		goto done;
980 	}
981 
982 	val = CSR_READ_4(sc, BGE_MI_COMM);
983 
984 done:
985 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
986 		BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
987 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
988 		DELAY(40);
989 	}
990 
991 	if (val & BGE_MICOMM_READFAIL)
992 		return 0;
993 
994 	return (val & 0xFFFF);
995 }
996 
997 static void
998 bge_miibus_writereg(device_t dev, int phy, int reg, int val)
999 {
1000 	struct bge_softc *sc = device_private(dev);
1001 	uint32_t autopoll;
1002 	int i;
1003 
1004 	if (phy!=1) {
1005 		return;
1006 	}
1007 
1008 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906 &&
1009 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL)) {
1010 		return;
1011 	}
1012 
1013 	/* Reading with autopolling on may trigger PCI errors */
1014 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
1015 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
1016 		delay(40);
1017 		BGE_STS_CLRBIT(sc, BGE_STS_AUTOPOLL);
1018 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
1019 		delay(10); /* 40 usec is supposed to be adequate */
1020 	}
1021 
1022 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
1023 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
1024 
1025 	for (i = 0; i < BGE_TIMEOUT; i++) {
1026 		delay(10);
1027 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
1028 			delay(5);
1029 			CSR_READ_4(sc, BGE_MI_COMM);
1030 			break;
1031 		}
1032 	}
1033 
1034 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
1035 		BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
1036 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
1037 		delay(40);
1038 	}
1039 
1040 	if (i == BGE_TIMEOUT)
1041 		aprint_error_dev(sc->bge_dev, "PHY read timed out\n");
1042 }
1043 
1044 static void
1045 bge_miibus_statchg(device_t dev)
1046 {
1047 	struct bge_softc *sc = device_private(dev);
1048 	struct mii_data *mii = &sc->bge_mii;
1049 
1050 	/*
1051 	 * Get flow control negotiation result.
1052 	 */
1053 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
1054 	    (mii->mii_media_active & IFM_ETH_FMASK) != sc->bge_flowflags) {
1055 		sc->bge_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
1056 		mii->mii_media_active &= ~IFM_ETH_FMASK;
1057 	}
1058 
1059 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
1060 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1061 	    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1062 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
1063 	else
1064 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
1065 
1066 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
1067 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
1068 	else
1069 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
1070 
1071 	/*
1072 	 * 802.3x flow control
1073 	 */
1074 	if (sc->bge_flowflags & IFM_ETH_RXPAUSE)
1075 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
1076 	else
1077 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
1078 
1079 	if (sc->bge_flowflags & IFM_ETH_TXPAUSE)
1080 		BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
1081 	else
1082 		BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
1083 }
1084 
1085 /*
1086  * Update rx threshold levels to values in a particular slot
1087  * of the interrupt-mitigation table bge_rx_threshes.
1088  */
1089 static void
1090 bge_set_thresh(struct ifnet *ifp, int lvl)
1091 {
1092 	struct bge_softc *sc = ifp->if_softc;
1093 	int s;
1094 
1095 	/* For now, just save the new Rx-intr thresholds and record
1096 	 * that a threshold update is pending.  Updating the hardware
1097 	 * registers here (even at splhigh()) is observed to
1098 	 * occasionaly cause glitches where Rx-interrupts are not
1099 	 * honoured for up to 10 seconds. jonathan@NetBSD.org, 2003-04-05
1100 	 */
1101 	s = splnet();
1102 	sc->bge_rx_coal_ticks = bge_rx_threshes[lvl].rx_ticks;
1103 	sc->bge_rx_max_coal_bds = bge_rx_threshes[lvl].rx_max_bds;
1104 	sc->bge_pending_rxintr_change = 1;
1105 	splx(s);
1106 
1107 	 return;
1108 }
1109 
1110 
1111 /*
1112  * Update Rx thresholds of all bge devices
1113  */
1114 static void
1115 bge_update_all_threshes(int lvl)
1116 {
1117 	struct ifnet *ifp;
1118 	const char * const namebuf = "bge";
1119 	int namelen;
1120 
1121 	if (lvl < 0)
1122 		lvl = 0;
1123 	else if (lvl >= NBGE_RX_THRESH)
1124 		lvl = NBGE_RX_THRESH - 1;
1125 
1126 	namelen = strlen(namebuf);
1127 	/*
1128 	 * Now search all the interfaces for this name/number
1129 	 */
1130 	IFNET_FOREACH(ifp) {
1131 		if (strncmp(ifp->if_xname, namebuf, namelen) != 0)
1132 		      continue;
1133 		/* We got a match: update if doing auto-threshold-tuning */
1134 		if (bge_auto_thresh)
1135 			bge_set_thresh(ifp, lvl);
1136 	}
1137 }
1138 
1139 /*
1140  * Handle events that have triggered interrupts.
1141  */
1142 static void
1143 bge_handle_events(struct bge_softc *sc)
1144 {
1145 
1146 	return;
1147 }
1148 
1149 /*
1150  * Memory management for jumbo frames.
1151  */
1152 
1153 static int
1154 bge_alloc_jumbo_mem(struct bge_softc *sc)
1155 {
1156 	char *ptr, *kva;
1157 	bus_dma_segment_t	seg;
1158 	int		i, rseg, state, error;
1159 	struct bge_jpool_entry   *entry;
1160 
1161 	state = error = 0;
1162 
1163 	/* Grab a big chunk o' storage. */
1164 	if (bus_dmamem_alloc(sc->bge_dmatag, BGE_JMEM, PAGE_SIZE, 0,
1165 	     &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1166 		aprint_error_dev(sc->bge_dev, "can't alloc rx buffers\n");
1167 		return ENOBUFS;
1168 	}
1169 
1170 	state = 1;
1171 	if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg, BGE_JMEM, (void **)&kva,
1172 	    BUS_DMA_NOWAIT)) {
1173 		aprint_error_dev(sc->bge_dev,
1174 		    "can't map DMA buffers (%d bytes)\n", (int)BGE_JMEM);
1175 		error = ENOBUFS;
1176 		goto out;
1177 	}
1178 
1179 	state = 2;
1180 	if (bus_dmamap_create(sc->bge_dmatag, BGE_JMEM, 1, BGE_JMEM, 0,
1181 	    BUS_DMA_NOWAIT, &sc->bge_cdata.bge_rx_jumbo_map)) {
1182 		aprint_error_dev(sc->bge_dev, "can't create DMA map\n");
1183 		error = ENOBUFS;
1184 		goto out;
1185 	}
1186 
1187 	state = 3;
1188 	if (bus_dmamap_load(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
1189 	    kva, BGE_JMEM, NULL, BUS_DMA_NOWAIT)) {
1190 		aprint_error_dev(sc->bge_dev, "can't load DMA map\n");
1191 		error = ENOBUFS;
1192 		goto out;
1193 	}
1194 
1195 	state = 4;
1196 	sc->bge_cdata.bge_jumbo_buf = (void *)kva;
1197 	DPRINTFN(1,("bge_jumbo_buf = %p\n", sc->bge_cdata.bge_jumbo_buf));
1198 
1199 	SLIST_INIT(&sc->bge_jfree_listhead);
1200 	SLIST_INIT(&sc->bge_jinuse_listhead);
1201 
1202 	/*
1203 	 * Now divide it up into 9K pieces and save the addresses
1204 	 * in an array.
1205 	 */
1206 	ptr = sc->bge_cdata.bge_jumbo_buf;
1207 	for (i = 0; i < BGE_JSLOTS; i++) {
1208 		sc->bge_cdata.bge_jslots[i] = ptr;
1209 		ptr += BGE_JLEN;
1210 		entry = malloc(sizeof(struct bge_jpool_entry),
1211 		    M_DEVBUF, M_NOWAIT);
1212 		if (entry == NULL) {
1213 			aprint_error_dev(sc->bge_dev,
1214 			    "no memory for jumbo buffer queue!\n");
1215 			error = ENOBUFS;
1216 			goto out;
1217 		}
1218 		entry->slot = i;
1219 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
1220 				 entry, jpool_entries);
1221 	}
1222 out:
1223 	if (error != 0) {
1224 		switch (state) {
1225 		case 4:
1226 			bus_dmamap_unload(sc->bge_dmatag,
1227 			    sc->bge_cdata.bge_rx_jumbo_map);
1228 		case 3:
1229 			bus_dmamap_destroy(sc->bge_dmatag,
1230 			    sc->bge_cdata.bge_rx_jumbo_map);
1231 		case 2:
1232 			bus_dmamem_unmap(sc->bge_dmatag, kva, BGE_JMEM);
1233 		case 1:
1234 			bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
1235 			break;
1236 		default:
1237 			break;
1238 		}
1239 	}
1240 
1241 	return error;
1242 }
1243 
1244 /*
1245  * Allocate a jumbo buffer.
1246  */
1247 static void *
1248 bge_jalloc(struct bge_softc *sc)
1249 {
1250 	struct bge_jpool_entry   *entry;
1251 
1252 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
1253 
1254 	if (entry == NULL) {
1255 		aprint_error_dev(sc->bge_dev, "no free jumbo buffers\n");
1256 		return NULL;
1257 	}
1258 
1259 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
1260 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
1261 	return (sc->bge_cdata.bge_jslots[entry->slot]);
1262 }
1263 
1264 /*
1265  * Release a jumbo buffer.
1266  */
1267 static void
1268 bge_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1269 {
1270 	struct bge_jpool_entry *entry;
1271 	struct bge_softc *sc;
1272 	int i, s;
1273 
1274 	/* Extract the softc struct pointer. */
1275 	sc = (struct bge_softc *)arg;
1276 
1277 	if (sc == NULL)
1278 		panic("bge_jfree: can't find softc pointer!");
1279 
1280 	/* calculate the slot this buffer belongs to */
1281 
1282 	i = ((char *)buf
1283 	     - (char *)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
1284 
1285 	if ((i < 0) || (i >= BGE_JSLOTS))
1286 		panic("bge_jfree: asked to free buffer that we don't manage!");
1287 
1288 	s = splvm();
1289 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
1290 	if (entry == NULL)
1291 		panic("bge_jfree: buffer not in use!");
1292 	entry->slot = i;
1293 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
1294 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
1295 
1296 	if (__predict_true(m != NULL))
1297   		pool_cache_put(mb_cache, m);
1298 	splx(s);
1299 }
1300 
1301 
1302 /*
1303  * Initialize a standard receive ring descriptor.
1304  */
1305 static int
1306 bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m,
1307     bus_dmamap_t dmamap)
1308 {
1309 	struct mbuf		*m_new = NULL;
1310 	struct bge_rx_bd	*r;
1311 	int			error;
1312 
1313 	if (dmamap == NULL) {
1314 		error = bus_dmamap_create(sc->bge_dmatag, MCLBYTES, 1,
1315 		    MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap);
1316 		if (error != 0)
1317 			return error;
1318 	}
1319 
1320 	sc->bge_cdata.bge_rx_std_map[i] = dmamap;
1321 
1322 	if (m == NULL) {
1323 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1324 		if (m_new == NULL)
1325 			return ENOBUFS;
1326 
1327 		MCLGET(m_new, M_DONTWAIT);
1328 		if (!(m_new->m_flags & M_EXT)) {
1329 			m_freem(m_new);
1330 			return ENOBUFS;
1331 		}
1332 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1333 
1334 	} else {
1335 		m_new = m;
1336 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1337 		m_new->m_data = m_new->m_ext.ext_buf;
1338 	}
1339 	if (!(sc->bge_flags & BGE_RX_ALIGNBUG))
1340 	    m_adj(m_new, ETHER_ALIGN);
1341 	if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_new,
1342 	    BUS_DMA_READ|BUS_DMA_NOWAIT))
1343 		return ENOBUFS;
1344 	bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
1345 	    BUS_DMASYNC_PREREAD);
1346 
1347 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
1348 	r = &sc->bge_rdata->bge_rx_std_ring[i];
1349 	BGE_HOSTADDR(r->bge_addr, dmamap->dm_segs[0].ds_addr);
1350 	r->bge_flags = BGE_RXBDFLAG_END;
1351 	r->bge_len = m_new->m_len;
1352 	r->bge_idx = i;
1353 
1354 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1355 	    offsetof(struct bge_ring_data, bge_rx_std_ring) +
1356 		i * sizeof (struct bge_rx_bd),
1357 	    sizeof (struct bge_rx_bd),
1358 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1359 
1360 	return 0;
1361 }
1362 
1363 /*
1364  * Initialize a jumbo receive ring descriptor. This allocates
1365  * a jumbo buffer from the pool managed internally by the driver.
1366  */
1367 static int
1368 bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
1369 {
1370 	struct mbuf *m_new = NULL;
1371 	struct bge_rx_bd *r;
1372 	void *buf = NULL;
1373 
1374 	if (m == NULL) {
1375 
1376 		/* Allocate the mbuf. */
1377 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1378 		if (m_new == NULL)
1379 			return ENOBUFS;
1380 
1381 		/* Allocate the jumbo buffer */
1382 		buf = bge_jalloc(sc);
1383 		if (buf == NULL) {
1384 			m_freem(m_new);
1385 			aprint_error_dev(sc->bge_dev,
1386 			    "jumbo allocation failed -- packet dropped!\n");
1387 			return ENOBUFS;
1388 		}
1389 
1390 		/* Attach the buffer to the mbuf. */
1391 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
1392 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, M_DEVBUF,
1393 		    bge_jfree, sc);
1394 		m_new->m_flags |= M_EXT_RW;
1395 	} else {
1396 		m_new = m;
1397 		buf = m_new->m_data = m_new->m_ext.ext_buf;
1398 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
1399 	}
1400 	if (!(sc->bge_flags & BGE_RX_ALIGNBUG))
1401 	    m_adj(m_new, ETHER_ALIGN);
1402 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
1403 	    mtod(m_new, char *) - (char *)sc->bge_cdata.bge_jumbo_buf, BGE_JLEN,
1404 	    BUS_DMASYNC_PREREAD);
1405 	/* Set up the descriptor. */
1406 	r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
1407 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
1408 	BGE_HOSTADDR(r->bge_addr, BGE_JUMBO_DMA_ADDR(sc, m_new));
1409 	r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
1410 	r->bge_len = m_new->m_len;
1411 	r->bge_idx = i;
1412 
1413 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1414 	    offsetof(struct bge_ring_data, bge_rx_jumbo_ring) +
1415 		i * sizeof (struct bge_rx_bd),
1416 	    sizeof (struct bge_rx_bd),
1417 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1418 
1419 	return 0;
1420 }
1421 
1422 /*
1423  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1424  * that's 1MB or memory, which is a lot. For now, we fill only the first
1425  * 256 ring entries and hope that our CPU is fast enough to keep up with
1426  * the NIC.
1427  */
1428 static int
1429 bge_init_rx_ring_std(struct bge_softc *sc)
1430 {
1431 	int i;
1432 
1433 	if (sc->bge_flags & BGE_RXRING_VALID)
1434 		return 0;
1435 
1436 	for (i = 0; i < BGE_SSLOTS; i++) {
1437 		if (bge_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
1438 			return ENOBUFS;
1439 	}
1440 
1441 	sc->bge_std = i - 1;
1442 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1443 
1444 	sc->bge_flags |= BGE_RXRING_VALID;
1445 
1446 	return 0;
1447 }
1448 
1449 static void
1450 bge_free_rx_ring_std(struct bge_softc *sc)
1451 {
1452 	int i;
1453 
1454 	if (!(sc->bge_flags & BGE_RXRING_VALID))
1455 		return;
1456 
1457 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1458 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1459 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1460 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1461 			bus_dmamap_destroy(sc->bge_dmatag,
1462 			    sc->bge_cdata.bge_rx_std_map[i]);
1463 		}
1464 		memset((char *)&sc->bge_rdata->bge_rx_std_ring[i], 0,
1465 		    sizeof(struct bge_rx_bd));
1466 	}
1467 
1468 	sc->bge_flags &= ~BGE_RXRING_VALID;
1469 }
1470 
1471 static int
1472 bge_init_rx_ring_jumbo(struct bge_softc *sc)
1473 {
1474 	int i;
1475 	volatile struct bge_rcb *rcb;
1476 
1477 	if (sc->bge_flags & BGE_JUMBO_RXRING_VALID)
1478 		return 0;
1479 
1480 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1481 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1482 			return ENOBUFS;
1483 	};
1484 
1485 	sc->bge_jumbo = i - 1;
1486 	sc->bge_flags |= BGE_JUMBO_RXRING_VALID;
1487 
1488 	rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1489 	rcb->bge_maxlen_flags = 0;
1490 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1491 
1492 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1493 
1494 	return 0;
1495 }
1496 
1497 static void
1498 bge_free_rx_ring_jumbo(struct bge_softc *sc)
1499 {
1500 	int i;
1501 
1502 	if (!(sc->bge_flags & BGE_JUMBO_RXRING_VALID))
1503 		return;
1504 
1505 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1506 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1507 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1508 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1509 		}
1510 		memset((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i], 0,
1511 		    sizeof(struct bge_rx_bd));
1512 	}
1513 
1514 	sc->bge_flags &= ~BGE_JUMBO_RXRING_VALID;
1515 }
1516 
1517 static void
1518 bge_free_tx_ring(struct bge_softc *sc)
1519 {
1520 	int i, freed;
1521 	struct txdmamap_pool_entry *dma;
1522 
1523 	if (!(sc->bge_flags & BGE_TXRING_VALID))
1524 		return;
1525 
1526 	freed = 0;
1527 
1528 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1529 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1530 			freed++;
1531 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1532 			sc->bge_cdata.bge_tx_chain[i] = NULL;
1533 			SLIST_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
1534 					    link);
1535 			sc->txdma[i] = 0;
1536 		}
1537 		memset((char *)&sc->bge_rdata->bge_tx_ring[i], 0,
1538 		    sizeof(struct bge_tx_bd));
1539 	}
1540 
1541 	while ((dma = SLIST_FIRST(&sc->txdma_list))) {
1542 		SLIST_REMOVE_HEAD(&sc->txdma_list, link);
1543 		bus_dmamap_destroy(sc->bge_dmatag, dma->dmamap);
1544 		free(dma, M_DEVBUF);
1545 	}
1546 
1547 	sc->bge_flags &= ~BGE_TXRING_VALID;
1548 }
1549 
1550 static int
1551 bge_init_tx_ring(struct bge_softc *sc)
1552 {
1553 	int i;
1554 	bus_dmamap_t dmamap;
1555 	struct txdmamap_pool_entry *dma;
1556 
1557 	if (sc->bge_flags & BGE_TXRING_VALID)
1558 		return 0;
1559 
1560 	sc->bge_txcnt = 0;
1561 	sc->bge_tx_saved_considx = 0;
1562 
1563 	/* Initialize transmit producer index for host-memory send ring. */
1564 	sc->bge_tx_prodidx = 0;
1565 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1566 	/* 5700 b2 errata */
1567 	if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
1568 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1569 
1570 	/* NIC-memory send ring not used; initialize to zero. */
1571 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1572 	/* 5700 b2 errata */
1573 	if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
1574 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1575 
1576 	SLIST_INIT(&sc->txdma_list);
1577 	for (i = 0; i < BGE_RSLOTS; i++) {
1578 		if (bus_dmamap_create(sc->bge_dmatag, BGE_TXDMA_MAX,
1579 		    BGE_NTXSEG, ETHER_MAX_LEN_JUMBO, 0, BUS_DMA_NOWAIT,
1580 		    &dmamap))
1581 			return ENOBUFS;
1582 		if (dmamap == NULL)
1583 			panic("dmamap NULL in bge_init_tx_ring");
1584 		dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
1585 		if (dma == NULL) {
1586 			aprint_error_dev(sc->bge_dev,
1587 			    "can't alloc txdmamap_pool_entry\n");
1588 			bus_dmamap_destroy(sc->bge_dmatag, dmamap);
1589 			return ENOMEM;
1590 		}
1591 		dma->dmamap = dmamap;
1592 		SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
1593 	}
1594 
1595 	sc->bge_flags |= BGE_TXRING_VALID;
1596 
1597 	return 0;
1598 }
1599 
1600 static void
1601 bge_setmulti(struct bge_softc *sc)
1602 {
1603 	struct ethercom		*ac = &sc->ethercom;
1604 	struct ifnet		*ifp = &ac->ec_if;
1605 	struct ether_multi	*enm;
1606 	struct ether_multistep  step;
1607 	uint32_t		hashes[4] = { 0, 0, 0, 0 };
1608 	uint32_t		h;
1609 	int			i;
1610 
1611 	if (ifp->if_flags & IFF_PROMISC)
1612 		goto allmulti;
1613 
1614 	/* Now program new ones. */
1615 	ETHER_FIRST_MULTI(step, ac, enm);
1616 	while (enm != NULL) {
1617 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1618 			/*
1619 			 * We must listen to a range of multicast addresses.
1620 			 * For now, just accept all multicasts, rather than
1621 			 * trying to set only those filter bits needed to match
1622 			 * the range.  (At this time, the only use of address
1623 			 * ranges is for IP multicast routing, for which the
1624 			 * range is big enough to require all bits set.)
1625 			 */
1626 			goto allmulti;
1627 		}
1628 
1629 		h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1630 
1631 		/* Just want the 7 least-significant bits. */
1632 		h &= 0x7f;
1633 
1634 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1635 		ETHER_NEXT_MULTI(step, enm);
1636 	}
1637 
1638 	ifp->if_flags &= ~IFF_ALLMULTI;
1639 	goto setit;
1640 
1641  allmulti:
1642 	ifp->if_flags |= IFF_ALLMULTI;
1643 	hashes[0] = hashes[1] = hashes[2] = hashes[3] = 0xffffffff;
1644 
1645  setit:
1646 	for (i = 0; i < 4; i++)
1647 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1648 }
1649 
1650 static void
1651 bge_sig_pre_reset(struct bge_softc *sc, int type)
1652 {
1653 	/*
1654 	 * Some chips don't like this so only do this if ASF is enabled
1655 	 */
1656 	if (sc->bge_asf_mode)
1657 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
1658 
1659 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1660 		switch (type) {
1661 		case BGE_RESET_START:
1662 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
1663 			break;
1664 		case BGE_RESET_STOP:
1665 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
1666 			break;
1667 		}
1668 	}
1669 }
1670 
1671 static void
1672 bge_sig_post_reset(struct bge_softc *sc, int type)
1673 {
1674 
1675 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1676 		switch (type) {
1677 		case BGE_RESET_START:
1678 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
1679 			/* START DONE */
1680 			break;
1681 		case BGE_RESET_STOP:
1682 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
1683 			break;
1684 		}
1685 	}
1686 }
1687 
1688 static void
1689 bge_sig_legacy(struct bge_softc *sc, int type)
1690 {
1691 
1692 	if (sc->bge_asf_mode) {
1693 		switch (type) {
1694 		case BGE_RESET_START:
1695 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
1696 			break;
1697 		case BGE_RESET_STOP:
1698 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
1699 			break;
1700 		}
1701 	}
1702 }
1703 
1704 static void
1705 bge_stop_fw(struct bge_softc *sc)
1706 {
1707 	int i;
1708 
1709 	if (sc->bge_asf_mode) {
1710 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
1711 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
1712 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
1713 
1714 		for (i = 0; i < 100; i++) {
1715 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
1716 				break;
1717 			DELAY(10);
1718 		}
1719 	}
1720 }
1721 
1722 static int
1723 bge_poll_fw(struct bge_softc *sc)
1724 {
1725 	uint32_t val;
1726 	int i;
1727 
1728 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
1729 		for (i = 0; i < BGE_TIMEOUT; i++) {
1730 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
1731 			if (val & BGE_VCPU_STATUS_INIT_DONE)
1732 				break;
1733 			DELAY(100);
1734 		}
1735 		if (i >= BGE_TIMEOUT) {
1736 			aprint_error_dev(sc->bge_dev, "reset timed out\n");
1737 			return -1;
1738 		}
1739 	} else if ((sc->bge_flags & BGE_NO_EEPROM) == 0) {
1740 		/*
1741 		 * Poll the value location we just wrote until
1742 		 * we see the 1's complement of the magic number.
1743 		 * This indicates that the firmware initialization
1744 		 * is complete.
1745 		 * XXX 1000ms for Flash and 10000ms for SEEPROM.
1746 		 */
1747 		for (i = 0; i < BGE_TIMEOUT; i++) {
1748 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
1749 			if (val == ~BGE_MAGIC_NUMBER)
1750 				break;
1751 			DELAY(10);
1752 		}
1753 
1754 		if (i >= BGE_TIMEOUT) {
1755 			aprint_error_dev(sc->bge_dev,
1756 			    "firmware handshake timed out, val = %x\n", val);
1757 			return -1;
1758 		}
1759 	}
1760 
1761 	return 0;
1762 }
1763 
1764 /*
1765  * Do endian, PCI and DMA initialization. Also check the on-board ROM
1766  * self-test results.
1767  */
1768 static int
1769 bge_chipinit(struct bge_softc *sc)
1770 {
1771 	int i;
1772 	uint32_t dma_rw_ctl;
1773 
1774 	/* Set endianness before we access any non-PCI registers. */
1775 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
1776 	    BGE_INIT);
1777 
1778 	/* Set power state to D0. */
1779 	bge_setpowerstate(sc, 0);
1780 
1781 	/* Clear the MAC control register */
1782 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1783 
1784 	/*
1785 	 * Clear the MAC statistics block in the NIC's
1786 	 * internal memory.
1787 	 */
1788 	for (i = BGE_STATS_BLOCK;
1789 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1790 		BGE_MEMWIN_WRITE(sc->sc_pc, sc->sc_pcitag, i, 0);
1791 
1792 	for (i = BGE_STATUS_BLOCK;
1793 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1794 		BGE_MEMWIN_WRITE(sc->sc_pc, sc->sc_pcitag, i, 0);
1795 
1796 	/* Set up the PCI DMA control register. */
1797 	dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD;
1798 	if (sc->bge_flags & BGE_PCIE) {
1799 		/* Read watermark not used, 128 bytes for write. */
1800 		DPRINTFN(4, ("(%s: PCI-Express DMA setting)\n",
1801 		    device_xname(sc->bge_dev)));
1802 		dma_rw_ctl |= (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1803 	} else if (sc->bge_flags & BGE_PCIX) {
1804 	  	DPRINTFN(4, ("(:%s: PCI-X DMA setting)\n",
1805 		    device_xname(sc->bge_dev)));
1806 		/* PCI-X bus */
1807 		if (BGE_IS_5714_FAMILY(sc)) {
1808 			/* 256 bytes for read and write. */
1809 			dma_rw_ctl |= (0x02 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1810 			    (0x02 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1811 
1812 			if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5780)
1813 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1814 			else
1815 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1816 		} else if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
1817 			/* 1536 bytes for read, 384 bytes for write. */
1818 			dma_rw_ctl |=
1819 			  (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1820 			  (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1821 		} else {
1822 			/* 384 bytes for read and write. */
1823 			dma_rw_ctl |= (0x03 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1824 			    (0x03 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1825 			    (0x0F);
1826 		}
1827 
1828 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
1829 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
1830 			uint32_t tmp;
1831 
1832 			/* Set ONEDMA_ATONCE for hardware workaround. */
1833 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
1834 			if (tmp == 6 || tmp == 7)
1835 				dma_rw_ctl |=
1836 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1837 
1838 			/* Set PCI-X DMA write workaround. */
1839 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1840 		}
1841 	} else {
1842 		/* Conventional PCI bus: 256 bytes for read and write. */
1843 	  	DPRINTFN(4, ("(%s: PCI 2.2 DMA setting)\n",
1844 		    device_xname(sc->bge_dev)));
1845 		dma_rw_ctl |= (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1846 		   (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1847 		if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5705 &&
1848 		    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5750)
1849 			dma_rw_ctl |= 0x0F;
1850 	}
1851 
1852 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
1853 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701)
1854 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1855 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1856 
1857 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
1858 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
1859 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1860 
1861 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_DMA_RW_CTL,
1862 	    dma_rw_ctl);
1863 
1864 	/*
1865 	 * Set up general mode register.
1866 	 */
1867 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
1868 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1869 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
1870 
1871 	/*
1872 	 * BCM5701 B5 have a bug causing data corruption when using
1873 	 * 64-bit DMA reads, which can be terminated early and then
1874 	 * completed later as 32-bit accesses, in combination with
1875 	 * certain bridges.
1876 	 */
1877 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 &&
1878 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
1879 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
1880 
1881 	/*
1882 	 * Tell the firmware the driver is running
1883 	 */
1884 	if (sc->bge_asf_mode & ASF_STACKUP)
1885 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
1886 
1887 	/*
1888 	 * Disable memory write invalidate.  Apparently it is not supported
1889 	 * properly by these devices.
1890 	 */
1891 	PCI_CLRBIT(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG,
1892 		   PCI_COMMAND_INVALIDATE_ENABLE);
1893 
1894 #ifdef __brokenalpha__
1895 	/*
1896 	 * Must insure that we do not cross an 8K (bytes) boundary
1897 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1898 	 * restriction on some ALPHA platforms with early revision
1899 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1900 	 */
1901 	PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4);
1902 #endif
1903 
1904 	/* Set the timer prescaler (always 66MHz) */
1905 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1906 
1907 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
1908 		DELAY(40);	/* XXX */
1909 
1910 		/* Put PHY into ready state */
1911 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1912 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1913 		DELAY(40);
1914 	}
1915 
1916 	return 0;
1917 }
1918 
1919 static int
1920 bge_blockinit(struct bge_softc *sc)
1921 {
1922 	volatile struct bge_rcb	 *rcb;
1923 	bus_size_t rcb_addr;
1924 	int i;
1925 	struct ifnet *ifp = &sc->ethercom.ec_if;
1926 	bge_hostaddr taddr;
1927 	uint32_t val;
1928 
1929 	/*
1930 	 * Initialize the memory window pointer register so that
1931 	 * we can access the first 32K of internal NIC RAM. This will
1932 	 * allow us to set up the TX send ring RCBs and the RX return
1933 	 * ring RCBs, plus other things which live in NIC memory.
1934 	 */
1935 
1936 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, 0);
1937 
1938 	/* Step 33: Configure mbuf memory pool */
1939 	if (BGE_IS_5700_FAMILY(sc)) {
1940 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1941 		    BGE_BUFFPOOL_1);
1942 
1943 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
1944 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1945 		else
1946 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1947 
1948 		/* Configure DMA resource pool */
1949 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1950 		    BGE_DMA_DESCRIPTORS);
1951 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1952 	}
1953 
1954 	/* Step 35: Configure mbuf pool watermarks */
1955 #ifdef ORIG_WPAUL_VALUES
1956 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
1957 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
1958 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
1959 #else
1960 
1961 	/* new broadcom docs strongly recommend these: */
1962 	if (!BGE_IS_5705_PLUS(sc)) {
1963 		if (ifp->if_mtu > ETHER_MAX_LEN) {
1964 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1965 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1966 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1967 		} else {
1968 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 304);
1969 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 152);
1970 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 380);
1971 		}
1972 	} else if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
1973 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1974 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
1975 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
1976 	} else {
1977 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1978 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1979 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1980 	}
1981 #endif
1982 
1983 	/* Step 36: Configure DMA resource watermarks */
1984 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1985 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1986 
1987 	/* Step 38: Enable buffer manager */
1988 	CSR_WRITE_4(sc, BGE_BMAN_MODE,
1989 	    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
1990 
1991 	/* Step 39: Poll for buffer manager start indication */
1992 	for (i = 0; i < BGE_TIMEOUT * 2; i++) {
1993 		if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1994 			break;
1995 		DELAY(10);
1996 	}
1997 
1998 	if (i == BGE_TIMEOUT * 2) {
1999 		aprint_error_dev(sc->bge_dev,
2000 		    "buffer manager failed to start\n");
2001 		return ENXIO;
2002 	}
2003 
2004 	/* Step 40: Enable flow-through queues */
2005 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
2006 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
2007 
2008 	/* Wait until queue initialization is complete */
2009 	for (i = 0; i < BGE_TIMEOUT * 2; i++) {
2010 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
2011 			break;
2012 		DELAY(10);
2013 	}
2014 
2015 	if (i == BGE_TIMEOUT * 2) {
2016 		aprint_error_dev(sc->bge_dev,
2017 		    "flow-through queue init failed\n");
2018 		return ENXIO;
2019 	}
2020 
2021 	/* Step 41: Initialize the standard RX ring control block */
2022 	rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
2023 	BGE_HOSTADDR(rcb->bge_hostaddr, BGE_RING_DMA_ADDR(sc, bge_rx_std_ring));
2024 	if (BGE_IS_5705_PLUS(sc))
2025 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
2026 	else
2027 		rcb->bge_maxlen_flags =
2028 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
2029 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
2030 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
2031 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
2032 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
2033 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
2034 
2035 	/*
2036 	 * Step 42: Initialize the jumbo RX ring control block
2037 	 * We set the 'ring disabled' bit in the flags
2038 	 * field until we're actually ready to start
2039 	 * using this ring (i.e. once we set the MTU
2040 	 * high enough to require it).
2041 	 */
2042 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2043 		rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
2044 		BGE_HOSTADDR(rcb->bge_hostaddr,
2045 		    BGE_RING_DMA_ADDR(sc, bge_rx_jumbo_ring));
2046 		rcb->bge_maxlen_flags =
2047 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
2048 			BGE_RCB_FLAG_RING_DISABLED);
2049 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
2050 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
2051 		    rcb->bge_hostaddr.bge_addr_hi);
2052 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
2053 		    rcb->bge_hostaddr.bge_addr_lo);
2054 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
2055 		    rcb->bge_maxlen_flags);
2056 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
2057 
2058 		/* Set up dummy disabled mini ring RCB */
2059 		rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
2060 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
2061 		    BGE_RCB_FLAG_RING_DISABLED);
2062 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
2063 		    rcb->bge_maxlen_flags);
2064 
2065 		bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2066 		    offsetof(struct bge_ring_data, bge_info),
2067 		    sizeof (struct bge_gib),
2068 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2069 	}
2070 
2071 	/*
2072 	 * Set the BD ring replenish thresholds. The recommended
2073 	 * values are 1/8th the number of descriptors allocated to
2074 	 * each ring.
2075 	 */
2076 	i = BGE_STD_RX_RING_CNT / 8;
2077 
2078 	/*
2079 	 * Use a value of 8 for the following chips to workaround HW errata.
2080 	 * Some of these chips have been added based on empirical
2081 	 * evidence (they don't work unless this is done).
2082 	 */
2083 	if (BGE_IS_5705_PLUS(sc))
2084 		i = 8;
2085 
2086 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, i);
2087 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT / 8);
2088 
2089 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5717 ||
2090 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57765) {
2091 		CSR_WRITE_4(sc, BGE_STD_REPL_LWM, 4);
2092 		CSR_WRITE_4(sc, BGE_JUMBO_REPL_LWM, 4);
2093 	}
2094 
2095 	/*
2096 	 * Disable all unused send rings by setting the 'ring disabled'
2097 	 * bit in the flags field of all the TX send ring control blocks.
2098 	 * These are located in NIC memory.
2099 	 */
2100 	rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2101 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
2102 		RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
2103 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2104 		RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
2105 		rcb_addr += sizeof(struct bge_rcb);
2106 	}
2107 
2108 	/* Configure TX RCB 0 (we use only the first ring) */
2109 	rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2110 	BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_tx_ring));
2111 	RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2112 	RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2113 	RCB_WRITE_4(sc, rcb_addr, bge_nicaddr,
2114 		    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2115 	if (BGE_IS_5700_FAMILY(sc))
2116 		RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
2117 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
2118 
2119 	/* Disable all unused RX return rings */
2120 	rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2121 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
2122 		RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, 0);
2123 		RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, 0);
2124 		RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
2125 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
2126 			BGE_RCB_FLAG_RING_DISABLED));
2127 		RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
2128 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
2129 		    (i * (sizeof(uint64_t))), 0);
2130 		rcb_addr += sizeof(struct bge_rcb);
2131 	}
2132 
2133 	/* Initialize RX ring indexes */
2134 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
2135 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
2136 	bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
2137 
2138 	/*
2139 	 * Set up RX return ring 0
2140 	 * Note that the NIC address for RX return rings is 0x00000000.
2141 	 * The return rings live entirely within the host, so the
2142 	 * nicaddr field in the RCB isn't used.
2143 	 */
2144 	rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2145 	BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_rx_return_ring));
2146 	RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2147 	RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2148 	RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0x00000000);
2149 	RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
2150 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
2151 
2152 	/* Set random backoff seed for TX */
2153 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
2154 	    CLLADDR(ifp->if_sadl)[0] + CLLADDR(ifp->if_sadl)[1] +
2155 	    CLLADDR(ifp->if_sadl)[2] + CLLADDR(ifp->if_sadl)[3] +
2156 	    CLLADDR(ifp->if_sadl)[4] + CLLADDR(ifp->if_sadl)[5] +
2157 	    BGE_TX_BACKOFF_SEED_MASK);
2158 
2159 	/* Set inter-packet gap */
2160 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
2161 
2162 	/*
2163 	 * Specify which ring to use for packets that don't match
2164 	 * any RX rules.
2165 	 */
2166 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
2167 
2168 	/*
2169 	 * Configure number of RX lists. One interrupt distribution
2170 	 * list, sixteen active lists, one bad frames class.
2171 	 */
2172 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
2173 
2174 	/* Inialize RX list placement stats mask. */
2175 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
2176 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
2177 
2178 	/* Disable host coalescing until we get it set up */
2179 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
2180 
2181 	/* Poll to make sure it's shut down. */
2182 	for (i = 0; i < BGE_TIMEOUT * 2; i++) {
2183 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
2184 			break;
2185 		DELAY(10);
2186 	}
2187 
2188 	if (i == BGE_TIMEOUT * 2) {
2189 		aprint_error_dev(sc->bge_dev,
2190 		    "host coalescing engine failed to idle\n");
2191 		return ENXIO;
2192 	}
2193 
2194 	/* Set up host coalescing defaults */
2195 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
2196 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
2197 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
2198 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
2199 	if (BGE_IS_5700_FAMILY(sc)) {
2200 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
2201 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
2202 	}
2203 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
2204 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
2205 
2206 	/* Set up address of statistics block */
2207 	if (BGE_IS_5700_FAMILY(sc)) {
2208 		BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_info.bge_stats));
2209 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
2210 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
2211 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, taddr.bge_addr_hi);
2212 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, taddr.bge_addr_lo);
2213 	}
2214 
2215 	/* Set up address of status block */
2216 	BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_status_block));
2217 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
2218 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, taddr.bge_addr_hi);
2219 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, taddr.bge_addr_lo);
2220 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
2221 	sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
2222 
2223 	/* Turn on host coalescing state machine */
2224 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
2225 
2226 	/* Turn on RX BD completion state machine and enable attentions */
2227 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
2228 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
2229 
2230 	/* Turn on RX list placement state machine */
2231 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
2232 
2233 	/* Turn on RX list selector state machine. */
2234 	if (BGE_IS_5700_FAMILY(sc))
2235 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
2236 
2237 	val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2238 	    BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2239 	    BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2240 	    BGE_MACMODE_FRMHDR_DMA_ENB;
2241 
2242 	if (sc->bge_flags & BGE_PHY_FIBER_TBI)
2243 		val |= BGE_PORTMODE_TBI;
2244 	else if (sc->bge_flags & BGE_PHY_FIBER_MII)
2245 		val |= BGE_PORTMODE_GMII;
2246 	else
2247 		val |= BGE_PORTMODE_MII;
2248 
2249 	/* Turn on DMA, clear stats */
2250 	CSR_WRITE_4(sc, BGE_MAC_MODE, val);
2251 
2252 	/* Set misc. local control, enable interrupts on attentions */
2253 	sc->bge_local_ctrl_reg = BGE_MLC_INTR_ONATTN | BGE_MLC_AUTO_EEPROM;
2254 
2255 #ifdef notdef
2256 	/* Assert GPIO pins for PHY reset */
2257 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
2258 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
2259 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
2260 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
2261 #endif
2262 
2263 #if defined(not_quite_yet)
2264 	/* Linux driver enables enable gpio pin #1 on 5700s */
2265 	if (sc->bge_chipid == BGE_CHIPID_BCM5700) {
2266 		sc->bge_local_ctrl_reg |=
2267 		  (BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUTEN1);
2268 	}
2269 #endif
2270 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
2271 
2272 	/* Turn on DMA completion state machine */
2273 	if (BGE_IS_5700_FAMILY(sc))
2274 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
2275 
2276 	/* Turn on write DMA state machine */
2277 	{
2278 		uint32_t bge_wdma_mode =
2279 			BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
2280 
2281 		/* Enable host coalescing bug fix; see Linux tg3.c */
2282 		if (BGE_IS_5755_PLUS(sc))
2283 			bge_wdma_mode |= BGE_WDMAMODE_STATUS_TAG_FIX;
2284 
2285 		CSR_WRITE_4(sc, BGE_WDMA_MODE, bge_wdma_mode);
2286 	}
2287 
2288 	/* Turn on read DMA state machine */
2289 	{
2290 		uint32_t dma_read_modebits;
2291 
2292 		dma_read_modebits =
2293 		  BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
2294 
2295 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2296 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5785 ||
2297 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57780)
2298 			dma_read_modebits |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2299 			    BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2300 			    BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
2301 
2302 		if (sc->bge_flags & BGE_PCIE)
2303 			dma_read_modebits |= BGE_RDMA_MODE_FIFO_LONG_BURST;
2304 		if (sc->bge_flags & BGE_TSO)
2305 			dma_read_modebits |= BGE_RDMAMODE_TSO4_ENABLE;
2306 		CSR_WRITE_4(sc, BGE_RDMA_MODE, dma_read_modebits);
2307 		delay(40);
2308 	}
2309 
2310 	/* Turn on RX data completion state machine */
2311 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
2312 
2313 	/* Turn on RX BD initiator state machine */
2314 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
2315 
2316 	/* Turn on RX data and RX BD initiator state machine */
2317 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
2318 
2319 	/* Turn on Mbuf cluster free state machine */
2320 	if (BGE_IS_5700_FAMILY(sc))
2321 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
2322 
2323 	/* Turn on send BD completion state machine */
2324 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
2325 
2326 	/* Turn on send data completion state machine */
2327 	val = BGE_SDCMODE_ENABLE;
2328 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761)
2329 		val |= BGE_SDCMODE_CDELAY;
2330 	CSR_WRITE_4(sc, BGE_SDC_MODE, val);
2331 
2332 	/* Turn on send data initiator state machine */
2333 	if (sc->bge_flags & BGE_TSO) {
2334 		/* XXX: magic value from Linux driver */
2335 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE | 0x08);
2336 	} else
2337 		CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
2338 
2339 	/* Turn on send BD initiator state machine */
2340 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
2341 
2342 	/* Turn on send BD selector state machine */
2343 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
2344 
2345 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
2346 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
2347 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
2348 
2349 	/* ack/clear link change events */
2350 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2351 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2352 	    BGE_MACSTAT_LINK_CHANGED);
2353 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
2354 
2355 	/* Enable PHY auto polling (for MII/GMII only) */
2356 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
2357 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2358 	} else {
2359 		BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
2360 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
2361 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700)
2362 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2363 			    BGE_EVTENB_MI_INTERRUPT);
2364 	}
2365 
2366 	/*
2367 	 * Clear any pending link state attention.
2368 	 * Otherwise some link state change events may be lost until attention
2369 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
2370 	 * It's not necessary on newer BCM chips - perhaps enabling link
2371 	 * state change attentions implies clearing pending attention.
2372 	 */
2373 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2374 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2375 	    BGE_MACSTAT_LINK_CHANGED);
2376 
2377 	/* Enable link state change attentions. */
2378 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
2379 
2380 	return 0;
2381 }
2382 
2383 static const struct bge_revision *
2384 bge_lookup_rev(uint32_t chipid)
2385 {
2386 	const struct bge_revision *br;
2387 
2388 	for (br = bge_revisions; br->br_name != NULL; br++) {
2389 		if (br->br_chipid == chipid)
2390 			return br;
2391 	}
2392 
2393 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
2394 		if (br->br_chipid == BGE_ASICREV(chipid))
2395 			return br;
2396 	}
2397 
2398 	return NULL;
2399 }
2400 
2401 static const struct bge_product *
2402 bge_lookup(const struct pci_attach_args *pa)
2403 {
2404 	const struct bge_product *bp;
2405 
2406 	for (bp = bge_products; bp->bp_name != NULL; bp++) {
2407 		if (PCI_VENDOR(pa->pa_id) == bp->bp_vendor &&
2408 		    PCI_PRODUCT(pa->pa_id) == bp->bp_product)
2409 			return bp;
2410 	}
2411 
2412 	return NULL;
2413 }
2414 
2415 static int
2416 bge_setpowerstate(struct bge_softc *sc, int powerlevel)
2417 {
2418 #ifdef NOTYET
2419 	uint32_t pm_ctl = 0;
2420 
2421 	/* XXX FIXME: make sure indirect accesses enabled? */
2422 	pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_MISC_CTL, 4);
2423 	pm_ctl |= BGE_PCIMISCCTL_INDIRECT_ACCESS;
2424 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, pm_ctl, 4);
2425 
2426 	/* clear the PME_assert bit and power state bits, enable PME */
2427 	pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_PWRMGMT_CMD, 2);
2428 	pm_ctl &= ~PCIM_PSTAT_DMASK;
2429 	pm_ctl |= (1 << 8);
2430 
2431 	if (powerlevel == 0) {
2432 		pm_ctl |= PCIM_PSTAT_D0;
2433 		pci_write_config(sc->bge_dev, BGE_PCI_PWRMGMT_CMD,
2434 		    pm_ctl, 2);
2435 		DELAY(10000);
2436 		CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
2437 		DELAY(10000);
2438 
2439 #ifdef NOTYET
2440 		/* XXX FIXME: write 0x02 to phy aux_Ctrl reg */
2441 		bge_miibus_writereg(sc->bge_dev, 1, 0x18, 0x02);
2442 #endif
2443 		DELAY(40); DELAY(40); DELAY(40);
2444 		DELAY(10000);	/* above not quite adequate on 5700 */
2445 		return 0;
2446 	}
2447 
2448 
2449 	/*
2450 	 * Entering ACPI power states D1-D3 is achieved by wiggling
2451 	 * GMII gpio pins. Example code assumes all hardware vendors
2452 	 * followed Broadcom's sample pcb layout. Until we verify that
2453 	 * for all supported OEM cards, states D1-D3 are  unsupported.
2454 	 */
2455 	aprint_error_dev(sc->bge_dev,
2456 	    "power state %d unimplemented; check GPIO pins\n",
2457 	    powerlevel);
2458 #endif
2459 	return EOPNOTSUPP;
2460 }
2461 
2462 
2463 /*
2464  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2465  * against our list and return its name if we find a match. Note
2466  * that since the Broadcom controller contains VPD support, we
2467  * can get the device name string from the controller itself instead
2468  * of the compiled-in string. This is a little slow, but it guarantees
2469  * we'll always announce the right product name.
2470  */
2471 static int
2472 bge_probe(device_t parent, cfdata_t match, void *aux)
2473 {
2474 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
2475 
2476 	if (bge_lookup(pa) != NULL)
2477 		return 1;
2478 
2479 	return 0;
2480 }
2481 
2482 static void
2483 bge_attach(device_t parent, device_t self, void *aux)
2484 {
2485 	struct bge_softc	*sc = device_private(self);
2486 	struct pci_attach_args	*pa = aux;
2487 	prop_dictionary_t dict;
2488 	const struct bge_product *bp;
2489 	const struct bge_revision *br;
2490 	pci_chipset_tag_t	pc;
2491 	pci_intr_handle_t	ih;
2492 	const char		*intrstr = NULL;
2493 	bus_dma_segment_t	seg;
2494 	int			rseg;
2495 	uint32_t		hwcfg = 0;
2496 	uint32_t		command;
2497 	struct ifnet		*ifp;
2498 	uint32_t		misccfg;
2499 	void *			kva;
2500 	u_char			eaddr[ETHER_ADDR_LEN];
2501 	pcireg_t		memtype, subid;
2502 	bus_addr_t		memaddr;
2503 	bus_size_t		memsize;
2504 	uint32_t		pm_ctl;
2505 	bool			no_seeprom;
2506 
2507 	bp = bge_lookup(pa);
2508 	KASSERT(bp != NULL);
2509 
2510 	sc->sc_pc = pa->pa_pc;
2511 	sc->sc_pcitag = pa->pa_tag;
2512 	sc->bge_dev = self;
2513 
2514 	pc = sc->sc_pc;
2515 	subid = pci_conf_read(pc, sc->sc_pcitag, PCI_SUBSYS_ID_REG);
2516 
2517 	aprint_naive(": Ethernet controller\n");
2518 	aprint_normal(": %s\n", bp->bp_name);
2519 
2520 	/*
2521 	 * Map control/status registers.
2522 	 */
2523 	DPRINTFN(5, ("Map control/status regs\n"));
2524 	command = pci_conf_read(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
2525 	command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
2526 	pci_conf_write(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, command);
2527 	command = pci_conf_read(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
2528 
2529 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
2530 		aprint_error_dev(sc->bge_dev,
2531 		    "failed to enable memory mapping!\n");
2532 		return;
2533 	}
2534 
2535 	DPRINTFN(5, ("pci_mem_find\n"));
2536 	memtype = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, BGE_PCI_BAR0);
2537 	switch (memtype) {
2538 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
2539 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
2540 		if (pci_mapreg_map(pa, BGE_PCI_BAR0,
2541 		    memtype, 0, &sc->bge_btag, &sc->bge_bhandle,
2542 		    &memaddr, &memsize) == 0)
2543 			break;
2544 	default:
2545 		aprint_error_dev(sc->bge_dev, "can't find mem space\n");
2546 		return;
2547 	}
2548 
2549 	DPRINTFN(5, ("pci_intr_map\n"));
2550 	if (pci_intr_map(pa, &ih)) {
2551 		aprint_error_dev(sc->bge_dev, "couldn't map interrupt\n");
2552 		return;
2553 	}
2554 
2555 	DPRINTFN(5, ("pci_intr_string\n"));
2556 	intrstr = pci_intr_string(pc, ih);
2557 
2558 	DPRINTFN(5, ("pci_intr_establish\n"));
2559 	sc->bge_intrhand = pci_intr_establish(pc, ih, IPL_NET, bge_intr, sc);
2560 
2561 	if (sc->bge_intrhand == NULL) {
2562 		aprint_error_dev(sc->bge_dev,
2563 		    "couldn't establish interrupt%s%s\n",
2564 		    intrstr ? " at " : "", intrstr ? intrstr : "");
2565 		return;
2566 	}
2567 	aprint_normal_dev(sc->bge_dev, "interrupting at %s\n", intrstr);
2568 
2569 	/*
2570 	 * Kludge for 5700 Bx bug: a hardware bug (PCIX byte enable?)
2571 	 * can clobber the chip's PCI config-space power control registers,
2572 	 * leaving the card in D3 powersave state.
2573 	 * We do not have memory-mapped registers in this state,
2574 	 * so force device into D0 state before starting initialization.
2575 	 */
2576 	pm_ctl = pci_conf_read(pc, sc->sc_pcitag, BGE_PCI_PWRMGMT_CMD);
2577 	pm_ctl &= ~(PCI_PWR_D0|PCI_PWR_D1|PCI_PWR_D2|PCI_PWR_D3);
2578 	pm_ctl |= (1 << 8) | PCI_PWR_D0 ; /* D0 state */
2579 	pci_conf_write(pc, sc->sc_pcitag, BGE_PCI_PWRMGMT_CMD, pm_ctl);
2580 	DELAY(1000);	/* 27 usec is allegedly sufficent */
2581 
2582 	/*
2583 	 * Save ASIC rev.
2584 	 */
2585 	sc->bge_chipid =
2586 	    pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL)
2587 		>> BGE_PCIMISCCTL_ASICREV_SHIFT;
2588 
2589 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
2590 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5717 ||
2591 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5718 ||
2592 		    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5724)
2593 			sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2594 			    BGE_PCI_GEN2_PRODID_ASICREV);
2595 		else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57761 ||
2596 			 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57765 ||
2597 			 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57781 ||
2598 			 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57785 ||
2599 			 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57791 ||
2600 			 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57795)
2601 			sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2602 			    BGE_PCI_GEN15_PRODID_ASICREV);
2603 		else
2604 			sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2605 			    BGE_PCI_PRODID_ASICREV);
2606 	}
2607 
2608 	if (pci_get_capability(sc->sc_pc, sc->sc_pcitag, PCI_CAP_PCIEXPRESS,
2609 	        &sc->bge_pciecap, NULL) != 0) {
2610 		/* PCIe */
2611 		sc->bge_flags |= BGE_PCIE;
2612 		bge_set_max_readrq(sc);
2613 	} else if ((pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_PCISTATE) &
2614 		BGE_PCISTATE_PCI_BUSMODE) == 0) {
2615 		/* PCI-X */
2616 		sc->bge_flags |= BGE_PCIX;
2617 		if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PCIX,
2618 			&sc->bge_pcixcap, NULL) == 0)
2619 			aprint_error_dev(sc->bge_dev,
2620 			    "unable to find PCIX capability\n");
2621 	}
2622 
2623 	/* chipid */
2624 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2625 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 ||
2626 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
2627 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
2628 		sc->bge_flags |= BGE_5700_FAMILY;
2629 
2630 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5714_A0 ||
2631 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5780 ||
2632 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5714)
2633 		sc->bge_flags |= BGE_5714_FAMILY;
2634 
2635 	/* Intentionally exclude BGE_ASICREV_BCM5906 */
2636 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5717 ||
2637 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5755 ||
2638 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761 ||
2639 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2640 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5785 ||
2641 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5787 ||
2642 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57765 ||
2643 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57780)
2644 		sc->bge_flags |= BGE_5755_PLUS;
2645 
2646 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5750 ||
2647 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5752 ||
2648 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906 ||
2649 	    BGE_IS_5755_PLUS(sc) ||
2650 	    BGE_IS_5714_FAMILY(sc))
2651 		sc->bge_flags |= BGE_5750_PLUS;
2652 
2653 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 ||
2654 	    BGE_IS_5750_OR_BEYOND(sc))
2655 		sc->bge_flags |= BGE_5705_PLUS;
2656 
2657 	/*
2658 	 * When using the BCM5701 in PCI-X mode, data corruption has
2659 	 * been observed in the first few bytes of some received packets.
2660 	 * Aligning the packet buffer in memory eliminates the corruption.
2661 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2662 	 * which do not support unaligned accesses, we will realign the
2663 	 * payloads by copying the received packets.
2664 	 */
2665 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 &&
2666 	    sc->bge_flags & BGE_PCIX)
2667 		sc->bge_flags |= BGE_RX_ALIGNBUG;
2668 
2669 	if (BGE_IS_5700_FAMILY(sc))
2670 		sc->bge_flags |= BGE_JUMBO_CAPABLE;
2671 
2672 	if ((BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2673 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701) &&
2674 	    PCI_VENDOR(subid) == PCI_VENDOR_DELL)
2675 		sc->bge_flags |= BGE_NO_3LED;
2676 
2677 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG);
2678 	misccfg &= BGE_MISCCFG_BOARD_ID_MASK;
2679 
2680 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2681 	    (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
2682 	     misccfg == BGE_MISCCFG_BOARD_ID_5788M))
2683 		sc->bge_flags |= BGE_IS_5788;
2684 
2685 	/*
2686 	 * Some controllers seem to require a special firmware to use
2687 	 * TSO. But the firmware is not available to FreeBSD and Linux
2688 	 * claims that the TSO performed by the firmware is slower than
2689 	 * hardware based TSO. Moreover the firmware based TSO has one
2690 	 * known bug which can't handle TSO if ethernet header + IP/TCP
2691 	 * header is greater than 80 bytes. The workaround for the TSO
2692 	 * bug exist but it seems it's too expensive than not using
2693 	 * TSO at all. Some hardwares also have the TSO bug so limit
2694 	 * the TSO to the controllers that are not affected TSO issues
2695 	 * (e.g. 5755 or higher).
2696 	 */
2697 	if (BGE_IS_5755_PLUS(sc)) {
2698 		/*
2699 		 * BCM5754 and BCM5787 shares the same ASIC id so
2700 		 * explicit device id check is required.
2701 		 */
2702 		if ((PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5754) &&
2703 		    (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5754M))
2704 			sc->bge_flags |= BGE_TSO;
2705 	}
2706 
2707 	if ((BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 &&
2708 	     (misccfg == 0x4000 || misccfg == 0x8000)) ||
2709 	    (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2710 	     PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
2711 	     (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5901 ||
2712 	      PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5901A2 ||
2713 	      PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5705F)) ||
2714 	    (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
2715 	     (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5751F ||
2716 	      PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5753F ||
2717 	      PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5787F)) ||
2718 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57790 ||
2719 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
2720 		sc->bge_flags |= BGE_10_100_ONLY;
2721 
2722 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2723 	    (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2724 	     (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2725 	      sc->bge_chipid != BGE_CHIPID_BCM5705_A1)) ||
2726 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
2727 		sc->bge_flags |= BGE_NO_ETH_WIRE_SPEED;
2728 
2729 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
2730 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2731 		sc->bge_flags |= BGE_PHY_CRC_BUG;
2732 	if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5703_AX ||
2733 	    BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5704_AX)
2734 		sc->bge_flags |= BGE_PHY_ADC_BUG;
2735 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2736 		sc->bge_flags |= BGE_PHY_5704_A0_BUG;
2737 
2738 	if (BGE_IS_5705_PLUS(sc) &&
2739 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906 &&
2740 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5717 &&
2741 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5785 &&
2742 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57765 &&
2743 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57780) {
2744 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5755 ||
2745 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761 ||
2746 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2747 		    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5787) {
2748 			if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5722 &&
2749 			    PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5756)
2750 				sc->bge_flags |= BGE_PHY_JITTER_BUG;
2751 			if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5755M)
2752 				sc->bge_flags |= BGE_PHY_ADJUST_TRIM;
2753 		} else if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)
2754 			sc->bge_flags |= BGE_PHY_BER_BUG;
2755 	}
2756 
2757 	/*
2758 	 * SEEPROM check.
2759 	 * First check if firmware knows we do not have SEEPROM.
2760 	 */
2761 	if (prop_dictionary_get_bool(device_properties(self),
2762 	     "without-seeprom", &no_seeprom) && no_seeprom)
2763 	 	sc->bge_flags |= BGE_NO_EEPROM;
2764 
2765 	/* Now check the 'ROM failed' bit on the RX CPU */
2766 	else if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL)
2767 		sc->bge_flags |= BGE_NO_EEPROM;
2768 
2769 	/* Try to reset the chip. */
2770 	DPRINTFN(5, ("bge_reset\n"));
2771 	bge_reset(sc);
2772 
2773 	sc->bge_asf_mode = 0;
2774 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2775 	    == BGE_MAGIC_NUMBER)) {
2776 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
2777 		    & BGE_HWCFG_ASF) {
2778 			sc->bge_asf_mode |= ASF_ENABLE;
2779 			sc->bge_asf_mode |= ASF_STACKUP;
2780 			if (BGE_IS_5750_OR_BEYOND(sc)) {
2781 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
2782 			}
2783 		}
2784 	}
2785 
2786 	/* Try to reset the chip again the nice way. */
2787 	bge_stop_fw(sc);
2788 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
2789 	if (bge_reset(sc))
2790 		aprint_error_dev(sc->bge_dev, "chip reset failed\n");
2791 
2792 	bge_sig_legacy(sc, BGE_RESET_STOP);
2793 	bge_sig_post_reset(sc, BGE_RESET_STOP);
2794 
2795 	if (bge_chipinit(sc)) {
2796 		aprint_error_dev(sc->bge_dev, "chip initialization failed\n");
2797 		bge_release_resources(sc);
2798 		return;
2799 	}
2800 
2801 	/*
2802 	 * Get station address from the EEPROM
2803 	 */
2804 	if (bge_get_eaddr(sc, eaddr)) {
2805 		aprint_error_dev(sc->bge_dev,
2806 		    "failed to read station address\n");
2807 		bge_release_resources(sc);
2808 		return;
2809 	}
2810 
2811 	br = bge_lookup_rev(sc->bge_chipid);
2812 
2813 	if (br == NULL) {
2814 		aprint_normal_dev(sc->bge_dev, "unknown ASIC (0x%x)",
2815 		    sc->bge_chipid);
2816 	} else {
2817 		aprint_normal_dev(sc->bge_dev, "ASIC %s (0x%x)",
2818 		    br->br_name, sc->bge_chipid);
2819 	}
2820 	aprint_normal(", Ethernet address %s\n", ether_sprintf(eaddr));
2821 
2822 	/* Allocate the general information block and ring buffers. */
2823 	if (pci_dma64_available(pa))
2824 		sc->bge_dmatag = pa->pa_dmat64;
2825 	else
2826 		sc->bge_dmatag = pa->pa_dmat;
2827 	DPRINTFN(5, ("bus_dmamem_alloc\n"));
2828 	if (bus_dmamem_alloc(sc->bge_dmatag, sizeof(struct bge_ring_data),
2829 			     PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
2830 		aprint_error_dev(sc->bge_dev, "can't alloc rx buffers\n");
2831 		return;
2832 	}
2833 	DPRINTFN(5, ("bus_dmamem_map\n"));
2834 	if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg,
2835 			   sizeof(struct bge_ring_data), &kva,
2836 			   BUS_DMA_NOWAIT)) {
2837 		aprint_error_dev(sc->bge_dev,
2838 		    "can't map DMA buffers (%zu bytes)\n",
2839 		    sizeof(struct bge_ring_data));
2840 		bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2841 		return;
2842 	}
2843 	DPRINTFN(5, ("bus_dmamem_create\n"));
2844 	if (bus_dmamap_create(sc->bge_dmatag, sizeof(struct bge_ring_data), 1,
2845 	    sizeof(struct bge_ring_data), 0,
2846 	    BUS_DMA_NOWAIT, &sc->bge_ring_map)) {
2847 		aprint_error_dev(sc->bge_dev, "can't create DMA map\n");
2848 		bus_dmamem_unmap(sc->bge_dmatag, kva,
2849 				 sizeof(struct bge_ring_data));
2850 		bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2851 		return;
2852 	}
2853 	DPRINTFN(5, ("bus_dmamem_load\n"));
2854 	if (bus_dmamap_load(sc->bge_dmatag, sc->bge_ring_map, kva,
2855 			    sizeof(struct bge_ring_data), NULL,
2856 			    BUS_DMA_NOWAIT)) {
2857 		bus_dmamap_destroy(sc->bge_dmatag, sc->bge_ring_map);
2858 		bus_dmamem_unmap(sc->bge_dmatag, kva,
2859 				 sizeof(struct bge_ring_data));
2860 		bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2861 		return;
2862 	}
2863 
2864 	DPRINTFN(5, ("bzero\n"));
2865 	sc->bge_rdata = (struct bge_ring_data *)kva;
2866 
2867 	memset(sc->bge_rdata, 0, sizeof(struct bge_ring_data));
2868 
2869 	/* Try to allocate memory for jumbo buffers. */
2870 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2871 		if (bge_alloc_jumbo_mem(sc)) {
2872 			aprint_error_dev(sc->bge_dev,
2873 			    "jumbo buffer allocation failed\n");
2874 		} else
2875 			sc->ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
2876 	}
2877 
2878 	/* Set default tuneable values. */
2879 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2880 	sc->bge_rx_coal_ticks = 150;
2881 	sc->bge_rx_max_coal_bds = 64;
2882 #ifdef ORIG_WPAUL_VALUES
2883 	sc->bge_tx_coal_ticks = 150;
2884 	sc->bge_tx_max_coal_bds = 128;
2885 #else
2886 	sc->bge_tx_coal_ticks = 300;
2887 	sc->bge_tx_max_coal_bds = 400;
2888 #endif
2889 	if (BGE_IS_5705_PLUS(sc)) {
2890 		sc->bge_tx_coal_ticks = (12 * 5);
2891 		sc->bge_tx_max_coal_bds = (12 * 5);
2892 			aprint_verbose_dev(sc->bge_dev,
2893 			    "setting short Tx thresholds\n");
2894 	}
2895 
2896 	if (BGE_IS_5705_PLUS(sc))
2897 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2898 	else
2899 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2900 
2901 	/* Set up ifnet structure */
2902 	ifp = &sc->ethercom.ec_if;
2903 	ifp->if_softc = sc;
2904 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2905 	ifp->if_ioctl = bge_ioctl;
2906 	ifp->if_stop = bge_stop;
2907 	ifp->if_start = bge_start;
2908 	ifp->if_init = bge_init;
2909 	ifp->if_watchdog = bge_watchdog;
2910 	IFQ_SET_MAXLEN(&ifp->if_snd, max(BGE_TX_RING_CNT - 1, IFQ_MAXLEN));
2911 	IFQ_SET_READY(&ifp->if_snd);
2912 	DPRINTFN(5, ("strcpy if_xname\n"));
2913 	strcpy(ifp->if_xname, device_xname(sc->bge_dev));
2914 
2915 	if (sc->bge_chipid != BGE_CHIPID_BCM5700_B0)
2916 		sc->ethercom.ec_if.if_capabilities |=
2917 		    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
2918 #if 1	/* XXX TCP/UDP checksum offload breaks with pf(4) */
2919 		sc->ethercom.ec_if.if_capabilities |=
2920 		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
2921 		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
2922 #endif
2923 	sc->ethercom.ec_capabilities |=
2924 	    ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU;
2925 
2926 	if (sc->bge_flags & BGE_TSO)
2927 		sc->ethercom.ec_if.if_capabilities |= IFCAP_TSOv4;
2928 
2929 	/*
2930 	 * Do MII setup.
2931 	 */
2932 	DPRINTFN(5, ("mii setup\n"));
2933 	sc->bge_mii.mii_ifp = ifp;
2934 	sc->bge_mii.mii_readreg = bge_miibus_readreg;
2935 	sc->bge_mii.mii_writereg = bge_miibus_writereg;
2936 	sc->bge_mii.mii_statchg = bge_miibus_statchg;
2937 
2938 	/*
2939 	 * Figure out what sort of media we have by checking the
2940 	 * hardware config word in the first 32k of NIC internal memory,
2941 	 * or fall back to the config word in the EEPROM. Note: on some BCM5700
2942 	 * cards, this value appears to be unset. If that's the
2943 	 * case, we have to rely on identifying the NIC by its PCI
2944 	 * subsystem ID, as we do below for the SysKonnect SK-9D41.
2945 	 */
2946 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2947 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2948 	} else if (!(sc->bge_flags & BGE_NO_EEPROM)) {
2949 		bge_read_eeprom(sc, (void *)&hwcfg,
2950 		    BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
2951 		hwcfg = be32toh(hwcfg);
2952 	}
2953 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2954 	if (PCI_PRODUCT(pa->pa_id) == SK_SUBSYSID_9D41 ||
2955 	    (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
2956 		if (BGE_IS_5714_FAMILY(sc))
2957 		    sc->bge_flags |= BGE_PHY_FIBER_MII;
2958 		else
2959 		    sc->bge_flags |= BGE_PHY_FIBER_TBI;
2960 	}
2961 
2962 	/* set phyflags and chipid before mii_attach() */
2963 	dict = device_properties(self);
2964 	prop_dictionary_set_uint32(dict, "phyflags", sc->bge_flags);
2965 	prop_dictionary_set_uint32(dict, "chipid", sc->bge_chipid);
2966 
2967 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
2968 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
2969 		    bge_ifmedia_sts);
2970 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER |IFM_1000_SX, 0, NULL);
2971 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX|IFM_FDX,
2972 			    0, NULL);
2973 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
2974 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2975 		/* Pretend the user requested this setting */
2976 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
2977 	} else {
2978 		/*
2979 		 * Do transceiver setup and tell the firmware the
2980 		 * driver is down so we can try to get access the
2981 		 * probe if ASF is running.  Retry a couple of times
2982 		 * if we get a conflict with the ASF firmware accessing
2983 		 * the PHY.
2984 		 */
2985 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2986 		bge_asf_driver_up(sc);
2987 
2988 		ifmedia_init(&sc->bge_mii.mii_media, 0, bge_ifmedia_upd,
2989 			     bge_ifmedia_sts);
2990 		mii_attach(sc->bge_dev, &sc->bge_mii, 0xffffffff,
2991 			   MII_PHY_ANY, MII_OFFSET_ANY,
2992 			   MIIF_FORCEANEG|MIIF_DOPAUSE);
2993 
2994 		if (LIST_EMPTY(&sc->bge_mii.mii_phys)) {
2995 			aprint_error_dev(sc->bge_dev, "no PHY found!\n");
2996 			ifmedia_add(&sc->bge_mii.mii_media,
2997 				    IFM_ETHER|IFM_MANUAL, 0, NULL);
2998 			ifmedia_set(&sc->bge_mii.mii_media,
2999 				    IFM_ETHER|IFM_MANUAL);
3000 		} else
3001 			ifmedia_set(&sc->bge_mii.mii_media,
3002 				    IFM_ETHER|IFM_AUTO);
3003 
3004 		/*
3005 		 * Now tell the firmware we are going up after probing the PHY
3006 		 */
3007 		if (sc->bge_asf_mode & ASF_STACKUP)
3008 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3009 	}
3010 
3011 	/*
3012 	 * Call MI attach routine.
3013 	 */
3014 	DPRINTFN(5, ("if_attach\n"));
3015 	if_attach(ifp);
3016 	DPRINTFN(5, ("ether_ifattach\n"));
3017 	ether_ifattach(ifp, eaddr);
3018 	ether_set_ifflags_cb(&sc->ethercom, bge_ifflags_cb);
3019 #if NRND > 0
3020 	rnd_attach_source(&sc->rnd_source, device_xname(sc->bge_dev),
3021 		RND_TYPE_NET, 0);
3022 #endif
3023 #ifdef BGE_EVENT_COUNTERS
3024 	/*
3025 	 * Attach event counters.
3026 	 */
3027 	evcnt_attach_dynamic(&sc->bge_ev_intr, EVCNT_TYPE_INTR,
3028 	    NULL, device_xname(sc->bge_dev), "intr");
3029 	evcnt_attach_dynamic(&sc->bge_ev_tx_xoff, EVCNT_TYPE_MISC,
3030 	    NULL, device_xname(sc->bge_dev), "tx_xoff");
3031 	evcnt_attach_dynamic(&sc->bge_ev_tx_xon, EVCNT_TYPE_MISC,
3032 	    NULL, device_xname(sc->bge_dev), "tx_xon");
3033 	evcnt_attach_dynamic(&sc->bge_ev_rx_xoff, EVCNT_TYPE_MISC,
3034 	    NULL, device_xname(sc->bge_dev), "rx_xoff");
3035 	evcnt_attach_dynamic(&sc->bge_ev_rx_xon, EVCNT_TYPE_MISC,
3036 	    NULL, device_xname(sc->bge_dev), "rx_xon");
3037 	evcnt_attach_dynamic(&sc->bge_ev_rx_macctl, EVCNT_TYPE_MISC,
3038 	    NULL, device_xname(sc->bge_dev), "rx_macctl");
3039 	evcnt_attach_dynamic(&sc->bge_ev_xoffentered, EVCNT_TYPE_MISC,
3040 	    NULL, device_xname(sc->bge_dev), "xoffentered");
3041 #endif /* BGE_EVENT_COUNTERS */
3042 	DPRINTFN(5, ("callout_init\n"));
3043 	callout_init(&sc->bge_timeout, 0);
3044 
3045 	if (pmf_device_register(self, NULL, NULL))
3046 		pmf_class_network_register(self, ifp);
3047 	else
3048 		aprint_error_dev(self, "couldn't establish power handler\n");
3049 
3050 	sysctl_bge_init(sc);
3051 
3052 #ifdef BGE_DEBUG
3053 	bge_debug_info(sc);
3054 #endif
3055 }
3056 
3057 static void
3058 bge_release_resources(struct bge_softc *sc)
3059 {
3060 	if (sc->bge_vpd_prodname != NULL)
3061 		free(sc->bge_vpd_prodname, M_DEVBUF);
3062 
3063 	if (sc->bge_vpd_readonly != NULL)
3064 		free(sc->bge_vpd_readonly, M_DEVBUF);
3065 }
3066 
3067 static int
3068 bge_reset(struct bge_softc *sc)
3069 {
3070 	uint32_t cachesize, command, pcistate, marbmode;
3071 #if 0
3072 	uint32_t new_pcistate;
3073 #endif
3074 	pcireg_t devctl, reg;
3075 	int i, val;
3076 	void (*write_op)(struct bge_softc *, int, int);
3077 
3078 	if (BGE_IS_5750_OR_BEYOND(sc) && !BGE_IS_5714_FAMILY(sc)
3079 	    && (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)) {
3080 	    	if (sc->bge_flags & BGE_PCIE)
3081 			write_op = bge_writemem_direct;
3082 		else
3083 			write_op = bge_writemem_ind;
3084 	} else
3085 		write_op = bge_writereg_ind;
3086 
3087 	/* Save some important PCI state. */
3088 	cachesize = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CACHESZ);
3089 	command = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CMD);
3090 	pcistate = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_PCISTATE);
3091 
3092 	/* Step 5a: Enable memory arbiter. */
3093 	marbmode = 0;
3094 	if (BGE_IS_5714_FAMILY(sc))
3095 		marbmode = CSR_READ_4(sc, BGE_MARB_MODE);
3096 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | marbmode);
3097 
3098 	/* Step 5b-5d: */
3099 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
3100 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3101 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW);
3102 
3103 	/* XXX ???: Disable fastboot on controllers that support it. */
3104 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5752 ||
3105 	    BGE_IS_5755_PLUS(sc))
3106 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0);
3107 
3108 	/*
3109 	 * Step 6: Write the magic number to SRAM at offset 0xB50.
3110 	 * When firmware finishes its initialization it will
3111 	 * write ~BGE_MAGIC_NUMBER to the same location.
3112 	 */
3113 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
3114 
3115 	/* Step 7: */
3116 	val = BGE_MISCCFG_RESET_CORE_CLOCKS | (65<<1);
3117 	/*
3118 	 * XXX: from FreeBSD/Linux; no documentation
3119 	 */
3120 	if (sc->bge_flags & BGE_PCIE) {
3121 		if (CSR_READ_4(sc, BGE_PCIE_CTL1) == 0x60)
3122 			/* PCI Express 1.0 system */
3123 			CSR_WRITE_4(sc, BGE_PCIE_CTL1, 0x20);
3124 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3125 			/*
3126 			 * Prevent PCI Express link training
3127 			 * during global reset.
3128 			 */
3129 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
3130 			val |= (1<<29);
3131 		}
3132 	}
3133 
3134 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
3135 		i = CSR_READ_4(sc, BGE_VCPU_STATUS);
3136 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
3137 		    i | BGE_VCPU_STATUS_DRV_RESET);
3138 		i = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
3139 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
3140 		    i & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
3141 	}
3142 
3143 	/*
3144 	 * Set GPHY Power Down Override to leave GPHY
3145 	 * powered up in D0 uninitialized.
3146 	 */
3147 	if (BGE_IS_5705_PLUS(sc))
3148 		val |= BGE_MISCCFG_KEEP_GPHY_POWER;
3149 
3150 	/* XXX 5721, 5751 and 5752 */
3151 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5750)
3152 		val |= BGE_MISCCFG_GRC_RESET_DISABLE;
3153 
3154 	/* Issue global reset */
3155 	write_op(sc, BGE_MISC_CFG, val);
3156 
3157 	/* Step 8: wait for complete */
3158 	if (sc->bge_flags & BGE_PCIE)
3159 		delay(100*1000); /* too big */
3160 	else
3161 		delay(100);
3162 
3163 	/* From Linux: dummy read to flush PCI posted writes */
3164 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CMD);
3165 
3166 	/* Step 9-10: Reset some of the PCI state that got zapped by reset */
3167 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
3168 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
3169 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW
3170 		| BGE_PCIMISCCTL_CLOCKCTL_RW);
3171 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CMD, command);
3172 	write_op(sc, BGE_MISC_CFG, (65 << 1));
3173 
3174 	/* Step 11: disable PCI-X Relaxed Ordering. */
3175 	if (sc->bge_flags & BGE_PCIX) {
3176 		reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, sc->bge_pcixcap
3177 		    + PCI_PCIX_CMD);
3178 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, sc->bge_pcixcap
3179 		    + PCI_PCIX_CMD, reg & ~PCI_PCIX_CMD_RELAXED_ORDER);
3180 	}
3181 
3182 	if (sc->bge_flags & BGE_PCIE) {
3183 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
3184 			DELAY(500000);
3185 			/* XXX: Magic Numbers */
3186 			reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
3187 			    BGE_PCI_UNKNOWN0);
3188 			pci_conf_write(sc->sc_pc, sc->sc_pcitag,
3189 			    BGE_PCI_UNKNOWN0,
3190 			    reg | (1 << 15));
3191 		}
3192 		devctl = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
3193 		    sc->bge_pciecap + PCI_PCIE_DCSR);
3194 		/* Clear enable no snoop and disable relaxed ordering. */
3195 		devctl &= ~(0x0010 | PCI_PCIE_DCSR_ENA_NO_SNOOP);
3196 		/* Set PCIE max payload size to 128. */
3197 		devctl &= ~(0x00e0);
3198 		/* Clear device status register. Write 1b to clear */
3199 		devctl |= PCI_PCIE_DCSR_URD | PCI_PCIE_DCSR_FED
3200 		    | PCI_PCIE_DCSR_NFED | PCI_PCIE_DCSR_CED;
3201 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
3202 		    sc->bge_pciecap + PCI_PCIE_DCSR, devctl);
3203 	}
3204 
3205 	/* Step 12: Enable memory arbiter. */
3206 	marbmode = 0;
3207 	if (BGE_IS_5714_FAMILY(sc))
3208 		marbmode = CSR_READ_4(sc, BGE_MARB_MODE);
3209 	CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | marbmode);
3210 
3211 	/* Step 17: Poll until the firmware initialization is complete */
3212 	bge_poll_fw(sc);
3213 
3214 	/* XXX 5721, 5751 and 5752 */
3215 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5750) {
3216 		/* Step 19: */
3217 		BGE_SETBIT(sc, BGE_TLP_CONTROL_REG, 1 << 29 | 1 << 25);
3218 		/* Step 20: */
3219 		BGE_SETBIT(sc, BGE_TLP_CONTROL_REG, BGE_TLP_DATA_FIFO_PROTECT);
3220 	}
3221 
3222 	/*
3223 	 * Step 18: wirte mac mode
3224 	 * XXX Write 0x0c for 5703S and 5704S
3225 	 */
3226 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
3227 
3228 
3229 	/* Step 21: 5822 B0 errata */
3230 	if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5704_BX) {
3231 		pcireg_t msidata;
3232 
3233 		msidata = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
3234 		    BGE_PCI_MSI_DATA);
3235 		msidata |= ((1 << 13 | 1 << 12 | 1 << 10) << 16);
3236 		pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MSI_DATA,
3237 		    msidata);
3238 	}
3239 
3240 	/* Step 23: restore cache line size */
3241 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CACHESZ, cachesize);
3242 
3243 #if 0
3244 	/*
3245 	 * XXX Wait for the value of the PCISTATE register to
3246 	 * return to its original pre-reset state. This is a
3247 	 * fairly good indicator of reset completion. If we don't
3248 	 * wait for the reset to fully complete, trying to read
3249 	 * from the device's non-PCI registers may yield garbage
3250 	 * results.
3251 	 */
3252 	for (i = 0; i < BGE_TIMEOUT; i++) {
3253 		new_pcistate = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
3254 		    BGE_PCI_PCISTATE);
3255 		if ((new_pcistate & ~BGE_PCISTATE_RESERVED) ==
3256 		    (pcistate & ~BGE_PCISTATE_RESERVED))
3257 			break;
3258 		DELAY(10);
3259 	}
3260 	if ((new_pcistate & ~BGE_PCISTATE_RESERVED) !=
3261 	    (pcistate & ~BGE_PCISTATE_RESERVED)) {
3262 		aprint_error_dev(sc->bge_dev, "pcistate failed to revert\n");
3263 	}
3264 #endif
3265 
3266 	/* Step 28: Fix up byte swapping */
3267 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS);
3268 
3269 	/* Tell the ASF firmware we are up */
3270 	if (sc->bge_asf_mode & ASF_STACKUP)
3271 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3272 
3273 	/*
3274 	 * The 5704 in TBI mode apparently needs some special
3275 	 * adjustment to insure the SERDES drive level is set
3276 	 * to 1.2V.
3277 	 */
3278 	if (sc->bge_flags & BGE_PHY_FIBER_TBI &&
3279 	    BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
3280 		uint32_t serdescfg;
3281 
3282 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
3283 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
3284 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
3285 	}
3286 
3287 	if (sc->bge_flags & BGE_PCIE &&
3288 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3289 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5717 &&
3290 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5785 &&
3291 	    BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57765) {
3292 		uint32_t v;
3293 
3294 		/* Enable PCI Express bug fix */
3295 		v = CSR_READ_4(sc, 0x7c00);
3296 		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
3297 	}
3298 	DELAY(10000);
3299 
3300 	return 0;
3301 }
3302 
3303 /*
3304  * Frame reception handling. This is called if there's a frame
3305  * on the receive return list.
3306  *
3307  * Note: we have to be able to handle two possibilities here:
3308  * 1) the frame is from the jumbo receive ring
3309  * 2) the frame is from the standard receive ring
3310  */
3311 
3312 static void
3313 bge_rxeof(struct bge_softc *sc)
3314 {
3315 	struct ifnet *ifp;
3316 	uint16_t rx_prod, rx_cons;
3317 	int stdcnt = 0, jumbocnt = 0;
3318 	bus_dmamap_t dmamap;
3319 	bus_addr_t offset, toff;
3320 	bus_size_t tlen;
3321 	int tosync;
3322 
3323 	rx_cons = sc->bge_rx_saved_considx;
3324 	rx_prod = sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx;
3325 
3326 	/* Nothing to do */
3327 	if (rx_cons == rx_prod)
3328 		return;
3329 
3330 	ifp = &sc->ethercom.ec_if;
3331 
3332 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3333 	    offsetof(struct bge_ring_data, bge_status_block),
3334 	    sizeof (struct bge_status_block),
3335 	    BUS_DMASYNC_POSTREAD);
3336 
3337 	offset = offsetof(struct bge_ring_data, bge_rx_return_ring);
3338 	tosync = rx_prod - rx_cons;
3339 
3340 #if NRND > 0
3341 	if (tosync != 0 && RND_ENABLED(&sc->rnd_source))
3342 		rnd_add_uint32(&sc->rnd_source, tosync);
3343 #endif
3344 
3345 	toff = offset + (rx_cons * sizeof (struct bge_rx_bd));
3346 
3347 	if (tosync < 0) {
3348 		tlen = (sc->bge_return_ring_cnt - rx_cons) *
3349 		    sizeof (struct bge_rx_bd);
3350 		bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3351 		    toff, tlen, BUS_DMASYNC_POSTREAD);
3352 		tosync = -tosync;
3353 	}
3354 
3355 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3356 	    offset, tosync * sizeof (struct bge_rx_bd),
3357 	    BUS_DMASYNC_POSTREAD);
3358 
3359 	while (rx_cons != rx_prod) {
3360 		struct bge_rx_bd	*cur_rx;
3361 		uint32_t		rxidx;
3362 		struct mbuf		*m = NULL;
3363 
3364 		cur_rx = &sc->bge_rdata->bge_rx_return_ring[rx_cons];
3365 
3366 		rxidx = cur_rx->bge_idx;
3367 		BGE_INC(rx_cons, sc->bge_return_ring_cnt);
3368 
3369 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
3370 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3371 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
3372 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
3373 			jumbocnt++;
3374 			bus_dmamap_sync(sc->bge_dmatag,
3375 			    sc->bge_cdata.bge_rx_jumbo_map,
3376 			    mtod(m, char *) - (char *)sc->bge_cdata.bge_jumbo_buf,
3377 			    BGE_JLEN, BUS_DMASYNC_POSTREAD);
3378 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3379 				ifp->if_ierrors++;
3380 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3381 				continue;
3382 			}
3383 			if (bge_newbuf_jumbo(sc, sc->bge_jumbo,
3384 					     NULL)== ENOBUFS) {
3385 				ifp->if_ierrors++;
3386 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3387 				continue;
3388 			}
3389 		} else {
3390 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3391 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
3392 
3393 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
3394 			stdcnt++;
3395 			dmamap = sc->bge_cdata.bge_rx_std_map[rxidx];
3396 			sc->bge_cdata.bge_rx_std_map[rxidx] = 0;
3397 			bus_dmamap_sync(sc->bge_dmatag, dmamap, 0,
3398 			    dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
3399 			bus_dmamap_unload(sc->bge_dmatag, dmamap);
3400 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3401 				ifp->if_ierrors++;
3402 				bge_newbuf_std(sc, sc->bge_std, m, dmamap);
3403 				continue;
3404 			}
3405 			if (bge_newbuf_std(sc, sc->bge_std,
3406 			    NULL, dmamap) == ENOBUFS) {
3407 				ifp->if_ierrors++;
3408 				bge_newbuf_std(sc, sc->bge_std, m, dmamap);
3409 				continue;
3410 			}
3411 		}
3412 
3413 		ifp->if_ipackets++;
3414 #ifndef __NO_STRICT_ALIGNMENT
3415 		/*
3416 		 * XXX: if the 5701 PCIX-Rx-DMA workaround is in effect,
3417 		 * the Rx buffer has the layer-2 header unaligned.
3418 		 * If our CPU requires alignment, re-align by copying.
3419 		 */
3420 		if (sc->bge_flags & BGE_RX_ALIGNBUG) {
3421 			memmove(mtod(m, char *) + ETHER_ALIGN, m->m_data,
3422 				cur_rx->bge_len);
3423 			m->m_data += ETHER_ALIGN;
3424 		}
3425 #endif
3426 
3427 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
3428 		m->m_pkthdr.rcvif = ifp;
3429 
3430 		/*
3431 		 * Handle BPF listeners. Let the BPF user see the packet.
3432 		 */
3433 		bpf_mtap(ifp, m);
3434 
3435 		m->m_pkthdr.csum_flags = M_CSUM_IPv4;
3436 
3437 		if ((cur_rx->bge_ip_csum ^ 0xffff) != 0)
3438 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
3439 		/*
3440 		 * Rx transport checksum-offload may also
3441 		 * have bugs with packets which, when transmitted,
3442 		 * were `runts' requiring padding.
3443 		 */
3444 		if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3445 		    (/* (sc->_bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||*/
3446 		     m->m_pkthdr.len >= ETHER_MIN_NOPAD)) {
3447 			m->m_pkthdr.csum_data =
3448 			    cur_rx->bge_tcp_udp_csum;
3449 			m->m_pkthdr.csum_flags |=
3450 			    (M_CSUM_TCPv4|M_CSUM_UDPv4|
3451 			     M_CSUM_DATA);
3452 		}
3453 
3454 		/*
3455 		 * If we received a packet with a vlan tag, pass it
3456 		 * to vlan_input() instead of ether_input().
3457 		 */
3458 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
3459 			VLAN_INPUT_TAG(ifp, m, cur_rx->bge_vlan_tag, continue);
3460 		}
3461 
3462 		(*ifp->if_input)(ifp, m);
3463 	}
3464 
3465 	sc->bge_rx_saved_considx = rx_cons;
3466 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
3467 	if (stdcnt)
3468 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
3469 	if (jumbocnt)
3470 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
3471 }
3472 
3473 static void
3474 bge_txeof(struct bge_softc *sc)
3475 {
3476 	struct bge_tx_bd *cur_tx = NULL;
3477 	struct ifnet *ifp;
3478 	struct txdmamap_pool_entry *dma;
3479 	bus_addr_t offset, toff;
3480 	bus_size_t tlen;
3481 	int tosync;
3482 	struct mbuf *m;
3483 
3484 	ifp = &sc->ethercom.ec_if;
3485 
3486 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3487 	    offsetof(struct bge_ring_data, bge_status_block),
3488 	    sizeof (struct bge_status_block),
3489 	    BUS_DMASYNC_POSTREAD);
3490 
3491 	offset = offsetof(struct bge_ring_data, bge_tx_ring);
3492 	tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx -
3493 	    sc->bge_tx_saved_considx;
3494 
3495 #if NRND > 0
3496 	if (tosync != 0 && RND_ENABLED(&sc->rnd_source))
3497 		rnd_add_uint32(&sc->rnd_source, tosync);
3498 #endif
3499 
3500 	toff = offset + (sc->bge_tx_saved_considx * sizeof (struct bge_tx_bd));
3501 
3502 	if (tosync < 0) {
3503 		tlen = (BGE_TX_RING_CNT - sc->bge_tx_saved_considx) *
3504 		    sizeof (struct bge_tx_bd);
3505 		bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3506 		    toff, tlen, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3507 		tosync = -tosync;
3508 	}
3509 
3510 	bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3511 	    offset, tosync * sizeof (struct bge_tx_bd),
3512 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3513 
3514 	/*
3515 	 * Go through our tx ring and free mbufs for those
3516 	 * frames that have been sent.
3517 	 */
3518 	while (sc->bge_tx_saved_considx !=
3519 	    sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
3520 		uint32_t		idx = 0;
3521 
3522 		idx = sc->bge_tx_saved_considx;
3523 		cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
3524 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
3525 			ifp->if_opackets++;
3526 		m = sc->bge_cdata.bge_tx_chain[idx];
3527 		if (m != NULL) {
3528 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
3529 			dma = sc->txdma[idx];
3530 			bus_dmamap_sync(sc->bge_dmatag, dma->dmamap, 0,
3531 			    dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
3532 			bus_dmamap_unload(sc->bge_dmatag, dma->dmamap);
3533 			SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
3534 			sc->txdma[idx] = NULL;
3535 
3536 			m_freem(m);
3537 		}
3538 		sc->bge_txcnt--;
3539 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
3540 		ifp->if_timer = 0;
3541 	}
3542 
3543 	if (cur_tx != NULL)
3544 		ifp->if_flags &= ~IFF_OACTIVE;
3545 }
3546 
3547 static int
3548 bge_intr(void *xsc)
3549 {
3550 	struct bge_softc *sc;
3551 	struct ifnet *ifp;
3552 	uint32_t statusword;
3553 
3554 	sc = xsc;
3555 	ifp = &sc->ethercom.ec_if;
3556 
3557 	/* It is possible for the interrupt to arrive before
3558 	 * the status block is updated prior to the interrupt.
3559 	 * Reading the PCI State register will confirm whether the
3560 	 * interrupt is ours and will flush the status block.
3561 	 */
3562 
3563 	/* read status word from status block */
3564 	statusword = sc->bge_rdata->bge_status_block.bge_status;
3565 
3566 	if ((statusword & BGE_STATFLAG_UPDATED) ||
3567 	    (!(CSR_READ_4(sc, BGE_PCI_PCISTATE) & BGE_PCISTATE_INTR_NOT_ACTIVE))) {
3568 		/* Ack interrupt and stop others from occuring. */
3569 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
3570 
3571 		BGE_EVCNT_INCR(sc->bge_ev_intr);
3572 
3573 		/* clear status word */
3574 		sc->bge_rdata->bge_status_block.bge_status = 0;
3575 
3576 		if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
3577 		    statusword & BGE_STATFLAG_LINKSTATE_CHANGED ||
3578 		    BGE_STS_BIT(sc, BGE_STS_LINK_EVT))
3579 			bge_link_upd(sc);
3580 
3581 		if (ifp->if_flags & IFF_RUNNING) {
3582 			/* Check RX return ring producer/consumer */
3583 			bge_rxeof(sc);
3584 
3585 			/* Check TX ring producer/consumer */
3586 			bge_txeof(sc);
3587 		}
3588 
3589 		if (sc->bge_pending_rxintr_change) {
3590 			uint32_t rx_ticks = sc->bge_rx_coal_ticks;
3591 			uint32_t rx_bds = sc->bge_rx_max_coal_bds;
3592 			uint32_t junk;
3593 
3594 			CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, rx_ticks);
3595 			DELAY(10);
3596 			junk = CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
3597 
3598 			CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, rx_bds);
3599 			DELAY(10);
3600 			junk = CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
3601 
3602 			sc->bge_pending_rxintr_change = 0;
3603 		}
3604 		bge_handle_events(sc);
3605 
3606 		/* Re-enable interrupts. */
3607 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3608 
3609 		if (ifp->if_flags & IFF_RUNNING && !IFQ_IS_EMPTY(&ifp->if_snd))
3610 			bge_start(ifp);
3611 
3612 		return 1;
3613 	} else
3614 		return 0;
3615 }
3616 
3617 static void
3618 bge_asf_driver_up(struct bge_softc *sc)
3619 {
3620 	if (sc->bge_asf_mode & ASF_STACKUP) {
3621 		/* Send ASF heartbeat aprox. every 2s */
3622 		if (sc->bge_asf_count)
3623 			sc->bge_asf_count --;
3624 		else {
3625 			sc->bge_asf_count = 2;
3626 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
3627 			    BGE_FW_DRV_ALIVE);
3628 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
3629 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
3630 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
3631 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
3632 		}
3633 	}
3634 }
3635 
3636 static void
3637 bge_tick(void *xsc)
3638 {
3639 	struct bge_softc *sc = xsc;
3640 	struct mii_data *mii = &sc->bge_mii;
3641 	int s;
3642 
3643 	s = splnet();
3644 
3645 	if (BGE_IS_5705_PLUS(sc))
3646 		bge_stats_update_regs(sc);
3647 	else
3648 		bge_stats_update(sc);
3649 
3650 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
3651 		/*
3652 		 * Since in TBI mode auto-polling can't be used we should poll
3653 		 * link status manually. Here we register pending link event
3654 		 * and trigger interrupt.
3655 		 */
3656 		BGE_STS_SETBIT(sc, BGE_STS_LINK_EVT);
3657 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
3658 	} else {
3659 		/*
3660 		 * Do not touch PHY if we have link up. This could break
3661 		 * IPMI/ASF mode or produce extra input errors.
3662 		 * (extra input errors was reported for bcm5701 & bcm5704).
3663 		 */
3664 		if (!BGE_STS_BIT(sc, BGE_STS_LINK))
3665 			mii_tick(mii);
3666 	}
3667 
3668 	callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
3669 
3670 	splx(s);
3671 }
3672 
3673 static void
3674 bge_stats_update_regs(struct bge_softc *sc)
3675 {
3676 	struct ifnet *ifp = &sc->ethercom.ec_if;
3677 
3678 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
3679 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
3680 
3681 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3682 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
3683 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
3684 }
3685 
3686 static void
3687 bge_stats_update(struct bge_softc *sc)
3688 {
3689 	struct ifnet *ifp = &sc->ethercom.ec_if;
3690 	bus_size_t stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3691 
3692 #define READ_STAT(sc, stats, stat) \
3693 	  CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
3694 
3695 	ifp->if_collisions +=
3696 	  (READ_STAT(sc, stats, dot3StatsSingleCollisionFrames.bge_addr_lo) +
3697 	   READ_STAT(sc, stats, dot3StatsMultipleCollisionFrames.bge_addr_lo) +
3698 	   READ_STAT(sc, stats, dot3StatsExcessiveCollisions.bge_addr_lo) +
3699 	   READ_STAT(sc, stats, dot3StatsLateCollisions.bge_addr_lo)) -
3700 	  ifp->if_collisions;
3701 
3702 	BGE_EVCNT_UPD(sc->bge_ev_tx_xoff,
3703 		      READ_STAT(sc, stats, outXoffSent.bge_addr_lo));
3704 	BGE_EVCNT_UPD(sc->bge_ev_tx_xon,
3705 		      READ_STAT(sc, stats, outXonSent.bge_addr_lo));
3706 	BGE_EVCNT_UPD(sc->bge_ev_rx_xoff,
3707 		      READ_STAT(sc, stats,
3708 		      		xoffPauseFramesReceived.bge_addr_lo));
3709 	BGE_EVCNT_UPD(sc->bge_ev_rx_xon,
3710 		      READ_STAT(sc, stats, xonPauseFramesReceived.bge_addr_lo));
3711 	BGE_EVCNT_UPD(sc->bge_ev_rx_macctl,
3712 		      READ_STAT(sc, stats,
3713 		      		macControlFramesReceived.bge_addr_lo));
3714 	BGE_EVCNT_UPD(sc->bge_ev_xoffentered,
3715 		      READ_STAT(sc, stats, xoffStateEntered.bge_addr_lo));
3716 
3717 #undef READ_STAT
3718 
3719 #ifdef notdef
3720 	ifp->if_collisions +=
3721 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
3722 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
3723 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
3724 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
3725 	   ifp->if_collisions;
3726 #endif
3727 }
3728 
3729 /*
3730  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3731  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3732  * but when such padded frames employ the  bge IP/TCP checksum offload,
3733  * the hardware checksum assist gives incorrect results (possibly
3734  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3735  * If we pad such runts with zeros, the onboard checksum comes out correct.
3736  */
3737 static inline int
3738 bge_cksum_pad(struct mbuf *pkt)
3739 {
3740 	struct mbuf *last = NULL;
3741 	int padlen;
3742 
3743 	padlen = ETHER_MIN_NOPAD - pkt->m_pkthdr.len;
3744 
3745 	/* if there's only the packet-header and we can pad there, use it. */
3746 	if (pkt->m_pkthdr.len == pkt->m_len &&
3747 	    M_TRAILINGSPACE(pkt) >= padlen) {
3748 		last = pkt;
3749 	} else {
3750 		/*
3751 		 * Walk packet chain to find last mbuf. We will either
3752 		 * pad there, or append a new mbuf and pad it
3753 		 * (thus perhaps avoiding the bcm5700 dma-min bug).
3754 		 */
3755 		for (last = pkt; last->m_next != NULL; last = last->m_next) {
3756 	      	       continue; /* do nothing */
3757 		}
3758 
3759 		/* `last' now points to last in chain. */
3760 		if (M_TRAILINGSPACE(last) < padlen) {
3761 			/* Allocate new empty mbuf, pad it. Compact later. */
3762 			struct mbuf *n;
3763 			MGET(n, M_DONTWAIT, MT_DATA);
3764 			if (n == NULL)
3765 				return ENOBUFS;
3766 			n->m_len = 0;
3767 			last->m_next = n;
3768 			last = n;
3769 		}
3770 	}
3771 
3772 	KDASSERT(!M_READONLY(last));
3773 	KDASSERT(M_TRAILINGSPACE(last) >= padlen);
3774 
3775 	/* Now zero the pad area, to avoid the bge cksum-assist bug */
3776 	memset(mtod(last, char *) + last->m_len, 0, padlen);
3777 	last->m_len += padlen;
3778 	pkt->m_pkthdr.len += padlen;
3779 	return 0;
3780 }
3781 
3782 /*
3783  * Compact outbound packets to avoid bug with DMA segments less than 8 bytes.
3784  */
3785 static inline int
3786 bge_compact_dma_runt(struct mbuf *pkt)
3787 {
3788 	struct mbuf	*m, *prev;
3789 	int 		totlen, prevlen;
3790 
3791 	prev = NULL;
3792 	totlen = 0;
3793 	prevlen = -1;
3794 
3795 	for (m = pkt; m != NULL; prev = m,m = m->m_next) {
3796 		int mlen = m->m_len;
3797 		int shortfall = 8 - mlen ;
3798 
3799 		totlen += mlen;
3800 		if (mlen == 0) {
3801 			continue;
3802 		}
3803 		if (mlen >= 8)
3804 			continue;
3805 
3806 		/* If we get here, mbuf data is too small for DMA engine.
3807 		 * Try to fix by shuffling data to prev or next in chain.
3808 		 * If that fails, do a compacting deep-copy of the whole chain.
3809 		 */
3810 
3811 		/* Internal frag. If fits in prev, copy it there. */
3812 		if (prev && M_TRAILINGSPACE(prev) >= m->m_len) {
3813 		  	memcpy(prev->m_data + prev->m_len, m->m_data, mlen);
3814 			prev->m_len += mlen;
3815 			m->m_len = 0;
3816 			/* XXX stitch chain */
3817 			prev->m_next = m_free(m);
3818 			m = prev;
3819 			continue;
3820 		}
3821 		else if (m->m_next != NULL &&
3822 			     M_TRAILINGSPACE(m) >= shortfall &&
3823 			     m->m_next->m_len >= (8 + shortfall)) {
3824 		    /* m is writable and have enough data in next, pull up. */
3825 
3826 		  	memcpy(m->m_data + m->m_len, m->m_next->m_data,
3827 			    shortfall);
3828 			m->m_len += shortfall;
3829 			m->m_next->m_len -= shortfall;
3830 			m->m_next->m_data += shortfall;
3831 		}
3832 		else if (m->m_next == NULL || 1) {
3833 		  	/* Got a runt at the very end of the packet.
3834 			 * borrow data from the tail of the preceding mbuf and
3835 			 * update its length in-place. (The original data is still
3836 			 * valid, so we can do this even if prev is not writable.)
3837 			 */
3838 
3839 			/* if we'd make prev a runt, just move all of its data. */
3840 			KASSERT(prev != NULL /*, ("runt but null PREV")*/);
3841 			KASSERT(prev->m_len >= 8 /*, ("runt prev")*/);
3842 
3843 			if ((prev->m_len - shortfall) < 8)
3844 				shortfall = prev->m_len;
3845 
3846 #ifdef notyet	/* just do the safe slow thing for now */
3847 			if (!M_READONLY(m)) {
3848 				if (M_LEADINGSPACE(m) < shorfall) {
3849 					void *m_dat;
3850 					m_dat = (m->m_flags & M_PKTHDR) ?
3851 					  m->m_pktdat : m->dat;
3852 					memmove(m_dat, mtod(m, void*), m->m_len);
3853 					m->m_data = m_dat;
3854 				    }
3855 			} else
3856 #endif	/* just do the safe slow thing */
3857 			{
3858 				struct mbuf * n = NULL;
3859 				int newprevlen = prev->m_len - shortfall;
3860 
3861 				MGET(n, M_NOWAIT, MT_DATA);
3862 				if (n == NULL)
3863 				   return ENOBUFS;
3864 				KASSERT(m->m_len + shortfall < MLEN
3865 					/*,
3866 					  ("runt %d +prev %d too big\n", m->m_len, shortfall)*/);
3867 
3868 				/* first copy the data we're stealing from prev */
3869 				memcpy(n->m_data, prev->m_data + newprevlen,
3870 				    shortfall);
3871 
3872 				/* update prev->m_len accordingly */
3873 				prev->m_len -= shortfall;
3874 
3875 				/* copy data from runt m */
3876 				memcpy(n->m_data + shortfall, m->m_data,
3877 				    m->m_len);
3878 
3879 				/* n holds what we stole from prev, plus m */
3880 				n->m_len = shortfall + m->m_len;
3881 
3882 				/* stitch n into chain and free m */
3883 				n->m_next = m->m_next;
3884 				prev->m_next = n;
3885 				/* KASSERT(m->m_next == NULL); */
3886 				m->m_next = NULL;
3887 				m_free(m);
3888 				m = n;	/* for continuing loop */
3889 			}
3890 		}
3891 		prevlen = m->m_len;
3892 	}
3893 	return 0;
3894 }
3895 
3896 /*
3897  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
3898  * pointers to descriptors.
3899  */
3900 static int
3901 bge_encap(struct bge_softc *sc, struct mbuf *m_head, uint32_t *txidx)
3902 {
3903 	struct bge_tx_bd	*f = NULL;
3904 	uint32_t		frag, cur;
3905 	uint16_t		csum_flags = 0;
3906 	uint16_t		txbd_tso_flags = 0;
3907 	struct txdmamap_pool_entry *dma;
3908 	bus_dmamap_t dmamap;
3909 	int			i = 0;
3910 	struct m_tag		*mtag;
3911 	int			use_tso, maxsegsize, error;
3912 
3913 	cur = frag = *txidx;
3914 
3915 	if (m_head->m_pkthdr.csum_flags) {
3916 		if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
3917 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3918 		if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))
3919 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3920 	}
3921 
3922 	/*
3923 	 * If we were asked to do an outboard checksum, and the NIC
3924 	 * has the bug where it sometimes adds in the Ethernet padding,
3925 	 * explicitly pad with zeros so the cksum will be correct either way.
3926 	 * (For now, do this for all chip versions, until newer
3927 	 * are confirmed to not require the workaround.)
3928 	 */
3929 	if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) == 0 ||
3930 #ifdef notyet
3931 	    (sc->bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||
3932 #endif
3933 	    m_head->m_pkthdr.len >= ETHER_MIN_NOPAD)
3934 		goto check_dma_bug;
3935 
3936 	if (bge_cksum_pad(m_head) != 0)
3937 	    return ENOBUFS;
3938 
3939 check_dma_bug:
3940 	if (!(BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX))
3941 		goto doit;
3942 
3943 	/*
3944 	 * bcm5700 Revision B silicon cannot handle DMA descriptors with
3945 	 * less than eight bytes.  If we encounter a teeny mbuf
3946 	 * at the end of a chain, we can pad.  Otherwise, copy.
3947 	 */
3948 	if (bge_compact_dma_runt(m_head) != 0)
3949 		return ENOBUFS;
3950 
3951 doit:
3952 	dma = SLIST_FIRST(&sc->txdma_list);
3953 	if (dma == NULL)
3954 		return ENOBUFS;
3955 	dmamap = dma->dmamap;
3956 
3957 	/*
3958 	 * Set up any necessary TSO state before we start packing...
3959 	 */
3960 	use_tso = (m_head->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0;
3961 	if (!use_tso) {
3962 		maxsegsize = 0;
3963 	} else {	/* TSO setup */
3964 		unsigned  mss;
3965 		struct ether_header *eh;
3966 		unsigned ip_tcp_hlen, iptcp_opt_words, tcp_seg_flags, offset;
3967 		struct mbuf * m0 = m_head;
3968 		struct ip *ip;
3969 		struct tcphdr *th;
3970 		int iphl, hlen;
3971 
3972 		/*
3973 		 * XXX It would be nice if the mbuf pkthdr had offset
3974 		 * fields for the protocol headers.
3975 		 */
3976 
3977 		eh = mtod(m0, struct ether_header *);
3978 		switch (htons(eh->ether_type)) {
3979 		case ETHERTYPE_IP:
3980 			offset = ETHER_HDR_LEN;
3981 			break;
3982 
3983 		case ETHERTYPE_VLAN:
3984 			offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3985 			break;
3986 
3987 		default:
3988 			/*
3989 			 * Don't support this protocol or encapsulation.
3990 			 */
3991 			return ENOBUFS;
3992 		}
3993 
3994 		/*
3995 		 * TCP/IP headers are in the first mbuf; we can do
3996 		 * this the easy way.
3997 		 */
3998 		iphl = M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
3999 		hlen = iphl + offset;
4000 		if (__predict_false(m0->m_len <
4001 				    (hlen + sizeof(struct tcphdr)))) {
4002 
4003 			aprint_debug_dev(sc->bge_dev,
4004 			    "TSO: hard case m0->m_len == %d < ip/tcp hlen %zd,"
4005 			    "not handled yet\n",
4006 			     m0->m_len, hlen+ sizeof(struct tcphdr));
4007 #ifdef NOTYET
4008 			/*
4009 			 * XXX jonathan@NetBSD.org: untested.
4010 			 * how to force  this branch to be taken?
4011 			 */
4012 			BGE_EVCNT_INCR(&sc->sc_ev_txtsopain);
4013 
4014 			m_copydata(m0, offset, sizeof(ip), &ip);
4015 			m_copydata(m0, hlen, sizeof(th), &th);
4016 
4017 			ip.ip_len = 0;
4018 
4019 			m_copyback(m0, hlen + offsetof(struct ip, ip_len),
4020 			    sizeof(ip.ip_len), &ip.ip_len);
4021 
4022 			th.th_sum = in_cksum_phdr(ip.ip_src.s_addr,
4023 			    ip.ip_dst.s_addr, htons(IPPROTO_TCP));
4024 
4025 			m_copyback(m0, hlen + offsetof(struct tcphdr, th_sum),
4026 			    sizeof(th.th_sum), &th.th_sum);
4027 
4028 			hlen += th.th_off << 2;
4029 			iptcp_opt_words	= hlen;
4030 #else
4031 			/*
4032 			 * if_wm "hard" case not yet supported, can we not
4033 			 * mandate it out of existence?
4034 			 */
4035 			(void) ip; (void)th; (void) ip_tcp_hlen;
4036 
4037 			return ENOBUFS;
4038 #endif
4039 		} else {
4040 			ip = (struct ip *) (mtod(m0, char *) + offset);
4041 			th = (struct tcphdr *) (mtod(m0, char *) + hlen);
4042 			ip_tcp_hlen = iphl +  (th->th_off << 2);
4043 
4044 			/* Total IP/TCP options, in 32-bit words */
4045 			iptcp_opt_words = (ip_tcp_hlen
4046 					   - sizeof(struct tcphdr)
4047 					   - sizeof(struct ip)) >> 2;
4048 		}
4049 		if (BGE_IS_5750_OR_BEYOND(sc)) {
4050 			th->th_sum = 0;
4051 			csum_flags &= ~(BGE_TXBDFLAG_TCP_UDP_CSUM);
4052 		} else {
4053 			/*
4054 			 * XXX jonathan@NetBSD.org: 5705 untested.
4055 			 * Requires TSO firmware patch for 5701/5703/5704.
4056 			 */
4057 			th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
4058 			    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
4059 		}
4060 
4061 		mss = m_head->m_pkthdr.segsz;
4062 		txbd_tso_flags |=
4063 		    BGE_TXBDFLAG_CPU_PRE_DMA |
4064 		    BGE_TXBDFLAG_CPU_POST_DMA;
4065 
4066 		/*
4067 		 * Our NIC TSO-assist assumes TSO has standard, optionless
4068 		 * IPv4 and TCP headers, which total 40 bytes. By default,
4069 		 * the NIC copies 40 bytes of IP/TCP header from the
4070 		 * supplied header into the IP/TCP header portion of
4071 		 * each post-TSO-segment. If the supplied packet has IP or
4072 		 * TCP options, we need to tell the NIC to copy those extra
4073 		 * bytes into each  post-TSO header, in addition to the normal
4074 		 * 40-byte IP/TCP header (and to leave space accordingly).
4075 		 * Unfortunately, the driver encoding of option length
4076 		 * varies across different ASIC families.
4077 		 */
4078 		tcp_seg_flags = 0;
4079 		if (iptcp_opt_words) {
4080 			if (BGE_IS_5705_PLUS(sc)) {
4081 				tcp_seg_flags =
4082 					iptcp_opt_words << 11;
4083 			} else {
4084 				txbd_tso_flags |=
4085 					iptcp_opt_words << 12;
4086 			}
4087 		}
4088 		maxsegsize = mss | tcp_seg_flags;
4089 		ip->ip_len = htons(mss + ip_tcp_hlen);
4090 
4091 	}	/* TSO setup */
4092 
4093 	/*
4094 	 * Start packing the mbufs in this chain into
4095 	 * the fragment pointers. Stop when we run out
4096 	 * of fragments or hit the end of the mbuf chain.
4097 	 */
4098 	error = bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_head,
4099 	    BUS_DMA_NOWAIT);
4100 	if (error)
4101 		return ENOBUFS;
4102 	/*
4103 	 * Sanity check: avoid coming within 16 descriptors
4104 	 * of the end of the ring.
4105 	 */
4106 	if (dmamap->dm_nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
4107 		BGE_TSO_PRINTF(("%s: "
4108 		    " dmamap_load_mbuf too close to ring wrap\n",
4109 		    device_xname(sc->bge_dev)));
4110 		goto fail_unload;
4111 	}
4112 
4113 	mtag = sc->ethercom.ec_nvlans ?
4114 	    m_tag_find(m_head, PACKET_TAG_VLAN, NULL) : NULL;
4115 
4116 
4117 	/* Iterate over dmap-map fragments. */
4118 	for (i = 0; i < dmamap->dm_nsegs; i++) {
4119 		f = &sc->bge_rdata->bge_tx_ring[frag];
4120 		if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
4121 			break;
4122 
4123 		BGE_HOSTADDR(f->bge_addr, dmamap->dm_segs[i].ds_addr);
4124 		f->bge_len = dmamap->dm_segs[i].ds_len;
4125 
4126 		/*
4127 		 * For 5751 and follow-ons, for TSO we must turn
4128 		 * off checksum-assist flag in the tx-descr, and
4129 		 * supply the ASIC-revision-specific encoding
4130 		 * of TSO flags and segsize.
4131 		 */
4132 		if (use_tso) {
4133 			if (BGE_IS_5750_OR_BEYOND(sc) || i == 0) {
4134 				f->bge_rsvd = maxsegsize;
4135 				f->bge_flags = csum_flags | txbd_tso_flags;
4136 			} else {
4137 				f->bge_rsvd = 0;
4138 				f->bge_flags =
4139 				  (csum_flags | txbd_tso_flags) & 0x0fff;
4140 			}
4141 		} else {
4142 			f->bge_rsvd = 0;
4143 			f->bge_flags = csum_flags;
4144 		}
4145 
4146 		if (mtag != NULL) {
4147 			f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
4148 			f->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
4149 		} else {
4150 			f->bge_vlan_tag = 0;
4151 		}
4152 		cur = frag;
4153 		BGE_INC(frag, BGE_TX_RING_CNT);
4154 	}
4155 
4156 	if (i < dmamap->dm_nsegs) {
4157 		BGE_TSO_PRINTF(("%s: reached %d < dm_nsegs %d\n",
4158 		    device_xname(sc->bge_dev), i, dmamap->dm_nsegs));
4159 		goto fail_unload;
4160 	}
4161 
4162 	bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
4163 	    BUS_DMASYNC_PREWRITE);
4164 
4165 	if (frag == sc->bge_tx_saved_considx) {
4166 		BGE_TSO_PRINTF(("%s: frag %d = wrapped id %d?\n",
4167 		    device_xname(sc->bge_dev), frag, sc->bge_tx_saved_considx));
4168 
4169 		goto fail_unload;
4170 	}
4171 
4172 	sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
4173 	sc->bge_cdata.bge_tx_chain[cur] = m_head;
4174 	SLIST_REMOVE_HEAD(&sc->txdma_list, link);
4175 	sc->txdma[cur] = dma;
4176 	sc->bge_txcnt += dmamap->dm_nsegs;
4177 
4178 	*txidx = frag;
4179 
4180 	return 0;
4181 
4182 fail_unload:
4183 	bus_dmamap_unload(sc->bge_dmatag, dmamap);
4184 
4185 	return ENOBUFS;
4186 }
4187 
4188 /*
4189  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
4190  * to the mbuf data regions directly in the transmit descriptors.
4191  */
4192 static void
4193 bge_start(struct ifnet *ifp)
4194 {
4195 	struct bge_softc *sc;
4196 	struct mbuf *m_head = NULL;
4197 	uint32_t prodidx;
4198 	int pkts = 0;
4199 
4200 	sc = ifp->if_softc;
4201 
4202 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
4203 		return;
4204 
4205 	prodidx = sc->bge_tx_prodidx;
4206 
4207 	while (sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
4208 		IFQ_POLL(&ifp->if_snd, m_head);
4209 		if (m_head == NULL)
4210 			break;
4211 
4212 #if 0
4213 		/*
4214 		 * XXX
4215 		 * safety overkill.  If this is a fragmented packet chain
4216 		 * with delayed TCP/UDP checksums, then only encapsulate
4217 		 * it if we have enough descriptors to handle the entire
4218 		 * chain at once.
4219 		 * (paranoia -- may not actually be needed)
4220 		 */
4221 		if (m_head->m_flags & M_FIRSTFRAG &&
4222 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
4223 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
4224 			    M_CSUM_DATA_IPv4_OFFSET(m_head->m_pkthdr.csum_data) + 16) {
4225 				ifp->if_flags |= IFF_OACTIVE;
4226 				break;
4227 			}
4228 		}
4229 #endif
4230 
4231 		/*
4232 		 * Pack the data into the transmit ring. If we
4233 		 * don't have room, set the OACTIVE flag and wait
4234 		 * for the NIC to drain the ring.
4235 		 */
4236 		if (bge_encap(sc, m_head, &prodidx)) {
4237 			ifp->if_flags |= IFF_OACTIVE;
4238 			break;
4239 		}
4240 
4241 		/* now we are committed to transmit the packet */
4242 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
4243 		pkts++;
4244 
4245 		/*
4246 		 * If there's a BPF listener, bounce a copy of this frame
4247 		 * to him.
4248 		 */
4249 		bpf_mtap(ifp, m_head);
4250 	}
4251 	if (pkts == 0)
4252 		return;
4253 
4254 	/* Transmit */
4255 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
4256 	/* 5700 b2 errata */
4257 	if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
4258 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
4259 
4260 	sc->bge_tx_prodidx = prodidx;
4261 
4262 	/*
4263 	 * Set a timeout in case the chip goes out to lunch.
4264 	 */
4265 	ifp->if_timer = 5;
4266 }
4267 
4268 static int
4269 bge_init(struct ifnet *ifp)
4270 {
4271 	struct bge_softc *sc = ifp->if_softc;
4272 	const uint16_t *m;
4273 	int s, error = 0;
4274 
4275 	s = splnet();
4276 
4277 	ifp = &sc->ethercom.ec_if;
4278 
4279 	/* Cancel pending I/O and flush buffers. */
4280 	bge_stop(ifp, 0);
4281 
4282 	bge_stop_fw(sc);
4283 	bge_sig_pre_reset(sc, BGE_RESET_START);
4284 	bge_reset(sc);
4285 	bge_sig_legacy(sc, BGE_RESET_START);
4286 	bge_sig_post_reset(sc, BGE_RESET_START);
4287 
4288 	bge_chipinit(sc);
4289 
4290 	/*
4291 	 * Init the various state machines, ring
4292 	 * control blocks and firmware.
4293 	 */
4294 	error = bge_blockinit(sc);
4295 	if (error != 0) {
4296 		aprint_error_dev(sc->bge_dev, "initialization error %d\n",
4297 		    error);
4298 		splx(s);
4299 		return error;
4300 	}
4301 
4302 	ifp = &sc->ethercom.ec_if;
4303 
4304 	/* Specify MTU. */
4305 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4306 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
4307 
4308 	/* Load our MAC address. */
4309 	m = (const uint16_t *)&(CLLADDR(ifp->if_sadl)[0]);
4310 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
4311 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
4312 
4313 	/* Enable or disable promiscuous mode as needed. */
4314 	if (ifp->if_flags & IFF_PROMISC)
4315 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4316 	else
4317 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4318 
4319 	/* Program multicast filter. */
4320 	bge_setmulti(sc);
4321 
4322 	/* Init RX ring. */
4323 	bge_init_rx_ring_std(sc);
4324 
4325 	/*
4326 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
4327 	 * memory to insure that the chip has in fact read the first
4328 	 * entry of the ring.
4329 	 */
4330 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
4331 		uint32_t		v, i;
4332 		for (i = 0; i < 10; i++) {
4333 			DELAY(20);
4334 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
4335 			if (v == (MCLBYTES - ETHER_ALIGN))
4336 				break;
4337 		}
4338 		if (i == 10)
4339 			aprint_error_dev(sc->bge_dev,
4340 			    "5705 A0 chip failed to load RX ring\n");
4341 	}
4342 
4343 	/* Init jumbo RX ring. */
4344 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
4345 		bge_init_rx_ring_jumbo(sc);
4346 
4347 	/* Init our RX return ring index */
4348 	sc->bge_rx_saved_considx = 0;
4349 
4350 	/* Init TX ring. */
4351 	bge_init_tx_ring(sc);
4352 
4353 	/* Turn on transmitter */
4354 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
4355 
4356 	/* Turn on receiver */
4357 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
4358 
4359 	CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4360 
4361 	/* Tell firmware we're alive. */
4362 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4363 
4364 	/* Enable host interrupts. */
4365 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
4366 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
4367 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4368 
4369 	if ((error = bge_ifmedia_upd(ifp)) != 0)
4370 		goto out;
4371 
4372 	ifp->if_flags |= IFF_RUNNING;
4373 	ifp->if_flags &= ~IFF_OACTIVE;
4374 
4375 	callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
4376 
4377 out:
4378 	sc->bge_if_flags = ifp->if_flags;
4379 	splx(s);
4380 
4381 	return error;
4382 }
4383 
4384 /*
4385  * Set media options.
4386  */
4387 static int
4388 bge_ifmedia_upd(struct ifnet *ifp)
4389 {
4390 	struct bge_softc *sc = ifp->if_softc;
4391 	struct mii_data *mii = &sc->bge_mii;
4392 	struct ifmedia *ifm = &sc->bge_ifmedia;
4393 	int rc;
4394 
4395 	/* If this is a 1000baseX NIC, enable the TBI port. */
4396 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4397 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
4398 			return EINVAL;
4399 		switch (IFM_SUBTYPE(ifm->ifm_media)) {
4400 		case IFM_AUTO:
4401 			/*
4402 			 * The BCM5704 ASIC appears to have a special
4403 			 * mechanism for programming the autoneg
4404 			 * advertisement registers in TBI mode.
4405 			 */
4406 			if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
4407 				uint32_t sgdig;
4408 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
4409 				if (sgdig & BGE_SGDIGSTS_DONE) {
4410 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4411 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4412 					sgdig |= BGE_SGDIGCFG_AUTO |
4413 					    BGE_SGDIGCFG_PAUSE_CAP |
4414 					    BGE_SGDIGCFG_ASYM_PAUSE;
4415 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4416 					    sgdig | BGE_SGDIGCFG_SEND);
4417 					DELAY(5);
4418 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4419 				}
4420 			}
4421 			break;
4422 		case IFM_1000_SX:
4423 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
4424 				BGE_CLRBIT(sc, BGE_MAC_MODE,
4425 				    BGE_MACMODE_HALF_DUPLEX);
4426 			} else {
4427 				BGE_SETBIT(sc, BGE_MAC_MODE,
4428 				    BGE_MACMODE_HALF_DUPLEX);
4429 			}
4430 			break;
4431 		default:
4432 			return EINVAL;
4433 		}
4434 		/* XXX 802.3x flow control for 1000BASE-SX */
4435 		return 0;
4436 	}
4437 
4438 	BGE_STS_SETBIT(sc, BGE_STS_LINK_EVT);
4439 	if ((rc = mii_mediachg(mii)) == ENXIO)
4440 		return 0;
4441 
4442 	/*
4443 	 * Force an interrupt so that we will call bge_link_upd
4444 	 * if needed and clear any pending link state attention.
4445 	 * Without this we are not getting any further interrupts
4446 	 * for link state changes and thus will not UP the link and
4447 	 * not be able to send in bge_start. The only way to get
4448 	 * things working was to receive a packet and get a RX intr.
4449 	 */
4450 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
4451 	    sc->bge_flags & BGE_IS_5788)
4452 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4453 	else
4454 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4455 
4456 	return rc;
4457 }
4458 
4459 /*
4460  * Report current media status.
4461  */
4462 static void
4463 bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
4464 {
4465 	struct bge_softc *sc = ifp->if_softc;
4466 	struct mii_data *mii = &sc->bge_mii;
4467 
4468 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4469 		ifmr->ifm_status = IFM_AVALID;
4470 		ifmr->ifm_active = IFM_ETHER;
4471 		if (CSR_READ_4(sc, BGE_MAC_STS) &
4472 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
4473 			ifmr->ifm_status |= IFM_ACTIVE;
4474 		ifmr->ifm_active |= IFM_1000_SX;
4475 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
4476 			ifmr->ifm_active |= IFM_HDX;
4477 		else
4478 			ifmr->ifm_active |= IFM_FDX;
4479 		return;
4480 	}
4481 
4482 	mii_pollstat(mii);
4483 	ifmr->ifm_status = mii->mii_media_status;
4484 	ifmr->ifm_active = (mii->mii_media_active & ~IFM_ETH_FMASK) |
4485 	    sc->bge_flowflags;
4486 }
4487 
4488 static int
4489 bge_ifflags_cb(struct ethercom *ec)
4490 {
4491 	struct ifnet *ifp = &ec->ec_if;
4492 	struct bge_softc *sc = ifp->if_softc;
4493 	int change = ifp->if_flags ^ sc->bge_if_flags;
4494 
4495 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
4496 		return ENETRESET;
4497 	else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) == 0)
4498 		return 0;
4499 
4500 	if ((ifp->if_flags & IFF_PROMISC) == 0)
4501 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4502 	else
4503 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4504 
4505 	bge_setmulti(sc);
4506 
4507 	sc->bge_if_flags = ifp->if_flags;
4508 	return 0;
4509 }
4510 
4511 static int
4512 bge_ioctl(struct ifnet *ifp, u_long command, void *data)
4513 {
4514 	struct bge_softc *sc = ifp->if_softc;
4515 	struct ifreq *ifr = (struct ifreq *) data;
4516 	int s, error = 0;
4517 	struct mii_data *mii;
4518 
4519 	s = splnet();
4520 
4521 	switch (command) {
4522 	case SIOCSIFMEDIA:
4523 		/* XXX Flow control is not supported for 1000BASE-SX */
4524 		if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4525 			ifr->ifr_media &= ~IFM_ETH_FMASK;
4526 			sc->bge_flowflags = 0;
4527 		}
4528 
4529 		/* Flow control requires full-duplex mode. */
4530 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
4531 		    (ifr->ifr_media & IFM_FDX) == 0) {
4532 		    	ifr->ifr_media &= ~IFM_ETH_FMASK;
4533 		}
4534 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
4535 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
4536 				/* We can do both TXPAUSE and RXPAUSE. */
4537 				ifr->ifr_media |=
4538 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
4539 			}
4540 			sc->bge_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
4541 		}
4542 		/* FALLTHROUGH */
4543 	case SIOCGIFMEDIA:
4544 		if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4545 			error = ifmedia_ioctl(ifp, ifr, &sc->bge_ifmedia,
4546 			    command);
4547 		} else {
4548 			mii = &sc->bge_mii;
4549 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
4550 			    command);
4551 		}
4552 		break;
4553 	default:
4554 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
4555 			break;
4556 
4557 		error = 0;
4558 
4559 		if (command != SIOCADDMULTI && command != SIOCDELMULTI)
4560 			;
4561 		else if (ifp->if_flags & IFF_RUNNING)
4562 			bge_setmulti(sc);
4563 		break;
4564 	}
4565 
4566 	splx(s);
4567 
4568 	return error;
4569 }
4570 
4571 static void
4572 bge_watchdog(struct ifnet *ifp)
4573 {
4574 	struct bge_softc *sc;
4575 
4576 	sc = ifp->if_softc;
4577 
4578 	aprint_error_dev(sc->bge_dev, "watchdog timeout -- resetting\n");
4579 
4580 	ifp->if_flags &= ~IFF_RUNNING;
4581 	bge_init(ifp);
4582 
4583 	ifp->if_oerrors++;
4584 }
4585 
4586 static void
4587 bge_stop_block(struct bge_softc *sc, bus_addr_t reg, uint32_t bit)
4588 {
4589 	int i;
4590 
4591 	BGE_CLRBIT(sc, reg, bit);
4592 
4593 	for (i = 0; i < 1000; i++) {
4594 		if ((CSR_READ_4(sc, reg) & bit) == 0)
4595 			return;
4596 		delay(100);
4597 	}
4598 
4599 	/*
4600 	 * Doesn't print only when the register is BGE_SRS_MODE. It occurs
4601 	 * on some environment (and once after boot?)
4602 	 */
4603 	if (reg != BGE_SRS_MODE)
4604 		aprint_error_dev(sc->bge_dev,
4605 		    "block failed to stop: reg 0x%lx, bit 0x%08x\n",
4606 		    (u_long)reg, bit);
4607 }
4608 
4609 /*
4610  * Stop the adapter and free any mbufs allocated to the
4611  * RX and TX lists.
4612  */
4613 static void
4614 bge_stop(struct ifnet *ifp, int disable)
4615 {
4616 	struct bge_softc *sc = ifp->if_softc;
4617 
4618 	callout_stop(&sc->bge_timeout);
4619 
4620 	/*
4621 	 * Tell firmware we're shutting down.
4622 	 */
4623 	bge_stop_fw(sc);
4624 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
4625 
4626 	/* Disable host interrupts. */
4627 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
4628 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4629 
4630 	/*
4631 	 * Disable all of the receiver blocks
4632 	 */
4633 	bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
4634 	bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
4635 	bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
4636 	if (BGE_IS_5700_FAMILY(sc))
4637 		bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
4638 	bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
4639 	bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
4640 	bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
4641 
4642 	/*
4643 	 * Disable all of the transmit blocks
4644 	 */
4645 	bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
4646 	bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
4647 	bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
4648 	bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
4649 	bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
4650 	if (BGE_IS_5700_FAMILY(sc))
4651 		bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
4652 	bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
4653 
4654 	/*
4655 	 * Shut down all of the memory managers and related
4656 	 * state machines.
4657 	 */
4658 	bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
4659 	bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
4660 	if (BGE_IS_5700_FAMILY(sc))
4661 		bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
4662 
4663 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
4664 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
4665 
4666 	if (BGE_IS_5700_FAMILY(sc)) {
4667 		bge_stop_block(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
4668 		bge_stop_block(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4669 	}
4670 
4671 	bge_reset(sc);
4672 	bge_sig_legacy(sc, BGE_RESET_STOP);
4673 	bge_sig_post_reset(sc, BGE_RESET_STOP);
4674 
4675 	/*
4676 	 * Keep the ASF firmware running if up.
4677 	 */
4678 	if (sc->bge_asf_mode & ASF_STACKUP)
4679 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4680 	else
4681 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4682 
4683 	/* Free the RX lists. */
4684 	bge_free_rx_ring_std(sc);
4685 
4686 	/* Free jumbo RX list. */
4687 	if (BGE_IS_JUMBO_CAPABLE(sc))
4688 		bge_free_rx_ring_jumbo(sc);
4689 
4690 	/* Free TX buffers. */
4691 	bge_free_tx_ring(sc);
4692 
4693 	/*
4694 	 * Isolate/power down the PHY.
4695 	 */
4696 	if (!(sc->bge_flags & BGE_PHY_FIBER_TBI))
4697 		mii_down(&sc->bge_mii);
4698 
4699 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
4700 
4701 	/* Clear MAC's link state (PHY may still have link UP). */
4702 	BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4703 
4704 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
4705 }
4706 
4707 static void
4708 bge_link_upd(struct bge_softc *sc)
4709 {
4710 	struct ifnet *ifp = &sc->ethercom.ec_if;
4711 	struct mii_data *mii = &sc->bge_mii;
4712 	uint32_t status;
4713 	int link;
4714 
4715 	/* Clear 'pending link event' flag */
4716 	BGE_STS_CLRBIT(sc, BGE_STS_LINK_EVT);
4717 
4718 	/*
4719 	 * Process link state changes.
4720 	 * Grrr. The link status word in the status block does
4721 	 * not work correctly on the BCM5700 rev AX and BX chips,
4722 	 * according to all available information. Hence, we have
4723 	 * to enable MII interrupts in order to properly obtain
4724 	 * async link changes. Unfortunately, this also means that
4725 	 * we have to read the MAC status register to detect link
4726 	 * changes, thereby adding an additional register access to
4727 	 * the interrupt handler.
4728 	 */
4729 
4730 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700) {
4731 		status = CSR_READ_4(sc, BGE_MAC_STS);
4732 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
4733 			mii_pollstat(mii);
4734 
4735 			if (!BGE_STS_BIT(sc, BGE_STS_LINK) &&
4736 			    mii->mii_media_status & IFM_ACTIVE &&
4737 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
4738 				BGE_STS_SETBIT(sc, BGE_STS_LINK);
4739 			else if (BGE_STS_BIT(sc, BGE_STS_LINK) &&
4740 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
4741 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE))
4742 				BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4743 
4744 			/* Clear the interrupt */
4745 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4746 			    BGE_EVTENB_MI_INTERRUPT);
4747 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4748 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4749 			    BRGPHY_INTRS);
4750 		}
4751 		return;
4752 	}
4753 
4754 	if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4755 		status = CSR_READ_4(sc, BGE_MAC_STS);
4756 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
4757 			if (!BGE_STS_BIT(sc, BGE_STS_LINK)) {
4758 				BGE_STS_SETBIT(sc, BGE_STS_LINK);
4759 				if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
4760 					BGE_CLRBIT(sc, BGE_MAC_MODE,
4761 					    BGE_MACMODE_TBI_SEND_CFGS);
4762 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
4763 				if_link_state_change(ifp, LINK_STATE_UP);
4764 			}
4765 		} else if (BGE_STS_BIT(sc, BGE_STS_LINK)) {
4766 			BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4767 			if_link_state_change(ifp, LINK_STATE_DOWN);
4768 		}
4769 	/*
4770 	 * Discard link events for MII/GMII cards if MI auto-polling disabled.
4771 	 * This should not happen since mii callouts are locked now, but
4772 	 * we keep this check for debug.
4773 	 */
4774 	} else if (BGE_STS_BIT(sc, BGE_STS_AUTOPOLL)) {
4775 		/*
4776 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED
4777 		 * bit in status word always set. Workaround this bug by
4778 		 * reading PHY link status directly.
4779 		 */
4780 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK)?
4781 		    BGE_STS_LINK : 0;
4782 
4783 		if (BGE_STS_BIT(sc, BGE_STS_LINK) != link) {
4784 			mii_pollstat(mii);
4785 
4786 			if (!BGE_STS_BIT(sc, BGE_STS_LINK) &&
4787 			    mii->mii_media_status & IFM_ACTIVE &&
4788 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
4789 				BGE_STS_SETBIT(sc, BGE_STS_LINK);
4790 			else if (BGE_STS_BIT(sc, BGE_STS_LINK) &&
4791 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
4792 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE))
4793 				BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4794 		}
4795 	}
4796 
4797 	/* Clear the attention */
4798 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
4799 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
4800 	    BGE_MACSTAT_LINK_CHANGED);
4801 }
4802 
4803 static int
4804 sysctl_bge_verify(SYSCTLFN_ARGS)
4805 {
4806 	int error, t;
4807 	struct sysctlnode node;
4808 
4809 	node = *rnode;
4810 	t = *(int*)rnode->sysctl_data;
4811 	node.sysctl_data = &t;
4812 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
4813 	if (error || newp == NULL)
4814 		return error;
4815 
4816 #if 0
4817 	DPRINTF2(("%s: t = %d, nodenum = %d, rnodenum = %d\n", __func__, t,
4818 	    node.sysctl_num, rnode->sysctl_num));
4819 #endif
4820 
4821 	if (node.sysctl_num == bge_rxthresh_nodenum) {
4822 		if (t < 0 || t >= NBGE_RX_THRESH)
4823 			return EINVAL;
4824 		bge_update_all_threshes(t);
4825 	} else
4826 		return EINVAL;
4827 
4828 	*(int*)rnode->sysctl_data = t;
4829 
4830 	return 0;
4831 }
4832 
4833 /*
4834  * Set up sysctl(3) MIB, hw.bge.*.
4835  */
4836 static void
4837 sysctl_bge_init(struct bge_softc *sc)
4838 {
4839 	int rc, bge_root_num;
4840 	const struct sysctlnode *node;
4841 
4842 	if ((rc = sysctl_createv(&sc->bge_log, 0, NULL, NULL,
4843 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
4844 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
4845 		goto err;
4846 	}
4847 
4848 	if ((rc = sysctl_createv(&sc->bge_log, 0, NULL, &node,
4849 	    0, CTLTYPE_NODE, "bge",
4850 	    SYSCTL_DESCR("BGE interface controls"),
4851 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
4852 		goto err;
4853 	}
4854 
4855 	bge_root_num = node->sysctl_num;
4856 
4857 	/* BGE Rx interrupt mitigation level */
4858 	if ((rc = sysctl_createv(&sc->bge_log, 0, NULL, &node,
4859 	    CTLFLAG_READWRITE,
4860 	    CTLTYPE_INT, "rx_lvl",
4861 	    SYSCTL_DESCR("BGE receive interrupt mitigation level"),
4862 	    sysctl_bge_verify, 0,
4863 	    &bge_rx_thresh_lvl,
4864 	    0, CTL_HW, bge_root_num, CTL_CREATE,
4865 	    CTL_EOL)) != 0) {
4866 		goto err;
4867 	}
4868 
4869 	bge_rxthresh_nodenum = node->sysctl_num;
4870 
4871 	return;
4872 
4873 err:
4874 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
4875 }
4876 
4877 #ifdef BGE_DEBUG
4878 void
4879 bge_debug_info(struct bge_softc *sc)
4880 {
4881 
4882 	printf("Hardware Flags:\n");
4883 	if (BGE_IS_5755_PLUS(sc))
4884 		printf(" - 5755 Plus\n");
4885 	if (BGE_IS_5750_OR_BEYOND(sc))
4886 		printf(" - 5750 Plus\n");
4887 	if (BGE_IS_5705_PLUS(sc))
4888 		printf(" - 5705 Plus\n");
4889 	if (BGE_IS_5714_FAMILY(sc))
4890 		printf(" - 5714 Family\n");
4891 	if (BGE_IS_5700_FAMILY(sc))
4892 		printf(" - 5700 Family\n");
4893 	if (sc->bge_flags & BGE_IS_5788)
4894 		printf(" - 5788\n");
4895 	if (sc->bge_flags & BGE_JUMBO_CAPABLE)
4896 		printf(" - Supports Jumbo Frames\n");
4897 	if (sc->bge_flags & BGE_NO_EEPROM)
4898 		printf(" - No EEPROM\n");
4899 	if (sc->bge_flags & BGE_PCIX)
4900 		printf(" - PCI-X Bus\n");
4901 	if (sc->bge_flags & BGE_PCIE)
4902 		printf(" - PCI Express Bus\n");
4903 	if (sc->bge_flags & BGE_NO_3LED)
4904 		printf(" - No 3 LEDs\n");
4905 	if (sc->bge_flags & BGE_RX_ALIGNBUG)
4906 		printf(" - RX Alignment Bug\n");
4907 	if (sc->bge_flags & BGE_TSO)
4908 		printf(" - TSO\n");
4909 }
4910 #endif /* BGE_DEBUG */
4911 
4912 static int
4913 bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
4914 {
4915 	prop_dictionary_t dict;
4916 	prop_data_t ea;
4917 
4918 	if ((sc->bge_flags & BGE_NO_EEPROM) == 0)
4919 		return 1;
4920 
4921 	dict = device_properties(sc->bge_dev);
4922 	ea = prop_dictionary_get(dict, "mac-address");
4923 	if (ea != NULL) {
4924 		KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
4925 		KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
4926 		memcpy(ether_addr, prop_data_data_nocopy(ea), ETHER_ADDR_LEN);
4927 		return 0;
4928 	}
4929 
4930 	return 1;
4931 }
4932 
4933 static int
4934 bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
4935 {
4936 	uint32_t mac_addr;
4937 
4938 	mac_addr = bge_readmem_ind(sc, 0x0c14);
4939 	if ((mac_addr >> 16) == 0x484b) {
4940 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
4941 		ether_addr[1] = (uint8_t)mac_addr;
4942 		mac_addr = bge_readmem_ind(sc, 0x0c18);
4943 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
4944 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
4945 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
4946 		ether_addr[5] = (uint8_t)mac_addr;
4947 		return 0;
4948 	}
4949 	return 1;
4950 }
4951 
4952 static int
4953 bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
4954 {
4955 	int mac_offset = BGE_EE_MAC_OFFSET;
4956 
4957 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
4958 		mac_offset = BGE_EE_MAC_OFFSET_5906;
4959 
4960 	return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
4961 	    ETHER_ADDR_LEN));
4962 }
4963 
4964 static int
4965 bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
4966 {
4967 
4968 	if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
4969 		return 1;
4970 
4971 	return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4972 	   ETHER_ADDR_LEN));
4973 }
4974 
4975 static int
4976 bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
4977 {
4978 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
4979 		/* NOTE: Order is critical */
4980 		bge_get_eaddr_fw,
4981 		bge_get_eaddr_mem,
4982 		bge_get_eaddr_nvram,
4983 		bge_get_eaddr_eeprom,
4984 		NULL
4985 	};
4986 	const bge_eaddr_fcn_t *func;
4987 
4988 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
4989 		if ((*func)(sc, eaddr) == 0)
4990 			break;
4991 	}
4992 	return (*func == NULL ? ENXIO : 0);
4993 }
4994