if_bge.c revision 149780
1/*-
2 * Copyright (c) 2001 Wind River Systems
3 * Copyright (c) 1997, 1998, 1999, 2001
4 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/bge/if_bge.c 149780 2005-09-04 06:35:59Z pjd $");
36
37/*
38 * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
39 *
40 * The Broadcom BCM5700 is based on technology originally developed by
41 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
42 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
43 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
44 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
45 * frames, highly configurable RX filtering, and 16 RX and TX queues
46 * (which, along with RX filter rules, can be used for QOS applications).
47 * Other features, such as TCP segmentation, may be available as part
48 * of value-added firmware updates. Unlike the Tigon I and Tigon II,
49 * firmware images can be stored in hardware and need not be compiled
50 * into the driver.
51 *
52 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
53 * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
54 *
55 * The BCM5701 is a single-chip solution incorporating both the BCM5700
56 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
57 * does not support external SSRAM.
58 *
59 * Broadcom also produces a variation of the BCM5700 under the "Altima"
60 * brand name, which is functionally similar but lacks PCI-X support.
61 *
62 * Without external SSRAM, you can only have at most 4 TX rings,
63 * and the use of the mini RX ring is disabled. This seems to imply
64 * that these features are simply not available on the BCM5701. As a
65 * result, this driver does not implement any support for the mini RX
66 * ring.
67 */
68
69#include <sys/param.h>
70#include <sys/endian.h>
71#include <sys/systm.h>
72#include <sys/sockio.h>
73#include <sys/mbuf.h>
74#include <sys/malloc.h>
75#include <sys/kernel.h>
76#include <sys/module.h>
77#include <sys/socket.h>
78#include <sys/queue.h>
79
80#include <net/if.h>
81#include <net/if_arp.h>
82#include <net/ethernet.h>
83#include <net/if_dl.h>
84#include <net/if_media.h>
85
86#include <net/bpf.h>
87
88#include <net/if_types.h>
89#include <net/if_vlan_var.h>
90
91#include <netinet/in_systm.h>
92#include <netinet/in.h>
93#include <netinet/ip.h>
94
95#include <machine/clock.h>      /* for DELAY */
96#include <machine/bus.h>
97#include <machine/resource.h>
98#include <sys/bus.h>
99#include <sys/rman.h>
100
101#include <dev/mii/mii.h>
102#include <dev/mii/miivar.h>
103#include "miidevs.h"
104#include <dev/mii/brgphyreg.h>
105
106#include <dev/pci/pcireg.h>
107#include <dev/pci/pcivar.h>
108
109#include <dev/bge/if_bgereg.h>
110
111#include "opt_bge.h"
112
113#define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
114
115MODULE_DEPEND(bge, pci, 1, 1, 1);
116MODULE_DEPEND(bge, ether, 1, 1, 1);
117MODULE_DEPEND(bge, miibus, 1, 1, 1);
118
119/* "controller miibus0" required.  See GENERIC if you get errors here. */
120#include "miibus_if.h"
121
122/*
123 * Various supported device vendors/types and their names. Note: the
124 * spec seems to indicate that the hardware still has Alteon's vendor
125 * ID burned into it, though it will always be overriden by the vendor
126 * ID in the EEPROM. Just to be safe, we cover all possibilities.
127 */
128#define BGE_DEVDESC_MAX		64	/* Maximum device description length */
129
130static struct bge_type bge_devs[] = {
131	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
132		"Broadcom BCM5700 Gigabit Ethernet" },
133	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
134		"Broadcom BCM5701 Gigabit Ethernet" },
135	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
136		"Broadcom BCM5700 Gigabit Ethernet" },
137	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
138		"Broadcom BCM5701 Gigabit Ethernet" },
139	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
140		"Broadcom BCM5702 Gigabit Ethernet" },
141	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
142		"Broadcom BCM5702X Gigabit Ethernet" },
143	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
144		"Broadcom BCM5703 Gigabit Ethernet" },
145	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
146		"Broadcom BCM5703X Gigabit Ethernet" },
147	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
148		"Broadcom BCM5704C Dual Gigabit Ethernet" },
149	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
150		"Broadcom BCM5704S Dual Gigabit Ethernet" },
151	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
152		"Broadcom BCM5705 Gigabit Ethernet" },
153	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705K,
154		"Broadcom BCM5705K Gigabit Ethernet" },
155	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
156		"Broadcom BCM5705M Gigabit Ethernet" },
157	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
158		"Broadcom BCM5705M Gigabit Ethernet" },
159	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5714C,
160		"Broadcom BCM5714C Gigabit Ethernet" },
161	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5721,
162		"Broadcom BCM5721 Gigabit Ethernet" },
163	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750,
164		"Broadcom BCM5750 Gigabit Ethernet" },
165	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5750M,
166		"Broadcom BCM5750M Gigabit Ethernet" },
167	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751,
168		"Broadcom BCM5751 Gigabit Ethernet" },
169	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5751M,
170		"Broadcom BCM5751M Gigabit Ethernet" },
171	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
172		"Broadcom BCM5782 Gigabit Ethernet" },
173	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
174		"Broadcom BCM5788 Gigabit Ethernet" },
175	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5789,
176		"Broadcom BCM5789 Gigabit Ethernet" },
177	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
178		"Broadcom BCM5901 Fast Ethernet" },
179	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
180		"Broadcom BCM5901A2 Fast Ethernet" },
181	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
182		"SysKonnect Gigabit Ethernet" },
183	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
184		"Altima AC1000 Gigabit Ethernet" },
185	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
186		"Altima AC1002 Gigabit Ethernet" },
187	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
188		"Altima AC9100 Gigabit Ethernet" },
189	{ 0, 0, NULL }
190};
191
192static int bge_probe		(device_t);
193static int bge_attach		(device_t);
194static int bge_detach		(device_t);
195static void bge_release_resources
196				(struct bge_softc *);
197static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
198static void bge_dma_map_tx_desc	(void *, bus_dma_segment_t *, int,
199				    bus_size_t, int);
200static int bge_dma_alloc	(device_t);
201static void bge_dma_free	(struct bge_softc *);
202
203static void bge_txeof		(struct bge_softc *);
204static void bge_rxeof		(struct bge_softc *);
205
206static void bge_tick_locked	(struct bge_softc *);
207static void bge_tick		(void *);
208static void bge_stats_update	(struct bge_softc *);
209static void bge_stats_update_regs
210				(struct bge_softc *);
211static int bge_encap		(struct bge_softc *, struct mbuf *,
212					u_int32_t *);
213
214static void bge_intr		(void *);
215static void bge_start_locked	(struct ifnet *);
216static void bge_start		(struct ifnet *);
217static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
218static void bge_init_locked	(struct bge_softc *);
219static void bge_init		(void *);
220static void bge_stop		(struct bge_softc *);
221static void bge_watchdog		(struct ifnet *);
222static void bge_shutdown		(device_t);
223static int bge_ifmedia_upd	(struct ifnet *);
224static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
225
226static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
227static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
228
229static void bge_setmulti	(struct bge_softc *);
230
231static void bge_handle_events	(struct bge_softc *);
232static int bge_alloc_jumbo_mem	(struct bge_softc *);
233static void bge_free_jumbo_mem	(struct bge_softc *);
234static void *bge_jalloc		(struct bge_softc *);
235static void bge_jfree		(void *, void *);
236static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
237static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
238static int bge_init_rx_ring_std	(struct bge_softc *);
239static void bge_free_rx_ring_std	(struct bge_softc *);
240static int bge_init_rx_ring_jumbo	(struct bge_softc *);
241static void bge_free_rx_ring_jumbo	(struct bge_softc *);
242static void bge_free_tx_ring	(struct bge_softc *);
243static int bge_init_tx_ring	(struct bge_softc *);
244
245static int bge_chipinit		(struct bge_softc *);
246static int bge_blockinit	(struct bge_softc *);
247
248#ifdef notdef
249static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
250static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
251static void bge_vpd_read	(struct bge_softc *);
252#endif
253
254static u_int32_t bge_readmem_ind
255				(struct bge_softc *, int);
256static void bge_writemem_ind	(struct bge_softc *, int, int);
257#ifdef notdef
258static u_int32_t bge_readreg_ind
259				(struct bge_softc *, int);
260#endif
261static void bge_writereg_ind	(struct bge_softc *, int, int);
262
263static int bge_miibus_readreg	(device_t, int, int);
264static int bge_miibus_writereg	(device_t, int, int, int);
265static void bge_miibus_statchg	(device_t);
266
267static void bge_reset		(struct bge_softc *);
268
269static device_method_t bge_methods[] = {
270	/* Device interface */
271	DEVMETHOD(device_probe,		bge_probe),
272	DEVMETHOD(device_attach,	bge_attach),
273	DEVMETHOD(device_detach,	bge_detach),
274	DEVMETHOD(device_shutdown,	bge_shutdown),
275
276	/* bus interface */
277	DEVMETHOD(bus_print_child,	bus_generic_print_child),
278	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
279
280	/* MII interface */
281	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
282	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
283	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
284
285	{ 0, 0 }
286};
287
288static driver_t bge_driver = {
289	"bge",
290	bge_methods,
291	sizeof(struct bge_softc)
292};
293
294static devclass_t bge_devclass;
295
296DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
297DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
298
299static u_int32_t
300bge_readmem_ind(sc, off)
301	struct bge_softc *sc;
302	int off;
303{
304	device_t dev;
305
306	dev = sc->bge_dev;
307
308	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
309	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
310}
311
312static void
313bge_writemem_ind(sc, off, val)
314	struct bge_softc *sc;
315	int off, val;
316{
317	device_t dev;
318
319	dev = sc->bge_dev;
320
321	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
322	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
323
324	return;
325}
326
327#ifdef notdef
328static u_int32_t
329bge_readreg_ind(sc, off)
330	struct bge_softc *sc;
331	int off;
332{
333	device_t dev;
334
335	dev = sc->bge_dev;
336
337	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
338	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
339}
340#endif
341
342static void
343bge_writereg_ind(sc, off, val)
344	struct bge_softc *sc;
345	int off, val;
346{
347	device_t dev;
348
349	dev = sc->bge_dev;
350
351	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
352	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
353
354	return;
355}
356
357/*
358 * Map a single buffer address.
359 */
360
361static void
362bge_dma_map_addr(arg, segs, nseg, error)
363	void *arg;
364	bus_dma_segment_t *segs;
365	int nseg;
366	int error;
367{
368	struct bge_dmamap_arg *ctx;
369
370	if (error)
371		return;
372
373	ctx = arg;
374
375	if (nseg > ctx->bge_maxsegs) {
376		ctx->bge_maxsegs = 0;
377		return;
378	}
379
380	ctx->bge_busaddr = segs->ds_addr;
381
382	return;
383}
384
385/*
386 * Map an mbuf chain into an TX ring.
387 */
388
389static void
390bge_dma_map_tx_desc(arg, segs, nseg, mapsize, error)
391	void *arg;
392	bus_dma_segment_t *segs;
393	int nseg;
394	bus_size_t mapsize;
395	int error;
396{
397	struct bge_dmamap_arg *ctx;
398	struct bge_tx_bd *d = NULL;
399	int i = 0, idx;
400
401	if (error)
402		return;
403
404	ctx = arg;
405
406	/* Signal error to caller if there's too many segments */
407	if (nseg > ctx->bge_maxsegs) {
408		ctx->bge_maxsegs = 0;
409		return;
410	}
411
412	idx = ctx->bge_idx;
413	while(1) {
414		d = &ctx->bge_ring[idx];
415		d->bge_addr.bge_addr_lo =
416		    htole32(BGE_ADDR_LO(segs[i].ds_addr));
417		d->bge_addr.bge_addr_hi =
418		    htole32(BGE_ADDR_HI(segs[i].ds_addr));
419		d->bge_len = htole16(segs[i].ds_len);
420		d->bge_flags = htole16(ctx->bge_flags);
421		i++;
422		if (i == nseg)
423			break;
424		BGE_INC(idx, BGE_TX_RING_CNT);
425	}
426
427	d->bge_flags |= htole16(BGE_TXBDFLAG_END);
428	ctx->bge_maxsegs = nseg;
429	ctx->bge_idx = idx;
430
431	return;
432}
433
434
435#ifdef notdef
436static u_int8_t
437bge_vpd_readbyte(sc, addr)
438	struct bge_softc *sc;
439	int addr;
440{
441	int i;
442	device_t dev;
443	u_int32_t val;
444
445	dev = sc->bge_dev;
446	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
447	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
448		DELAY(10);
449		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
450			break;
451	}
452
453	if (i == BGE_TIMEOUT) {
454		printf("bge%d: VPD read timed out\n", sc->bge_unit);
455		return(0);
456	}
457
458	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
459
460	return((val >> ((addr % 4) * 8)) & 0xFF);
461}
462
463static void
464bge_vpd_read_res(sc, res, addr)
465	struct bge_softc *sc;
466	struct vpd_res *res;
467	int addr;
468{
469	int i;
470	u_int8_t *ptr;
471
472	ptr = (u_int8_t *)res;
473	for (i = 0; i < sizeof(struct vpd_res); i++)
474		ptr[i] = bge_vpd_readbyte(sc, i + addr);
475
476	return;
477}
478
479static void
480bge_vpd_read(sc)
481	struct bge_softc *sc;
482{
483	int pos = 0, i;
484	struct vpd_res res;
485
486	if (sc->bge_vpd_prodname != NULL)
487		free(sc->bge_vpd_prodname, M_DEVBUF);
488	if (sc->bge_vpd_readonly != NULL)
489		free(sc->bge_vpd_readonly, M_DEVBUF);
490	sc->bge_vpd_prodname = NULL;
491	sc->bge_vpd_readonly = NULL;
492
493	bge_vpd_read_res(sc, &res, pos);
494
495	if (res.vr_id != VPD_RES_ID) {
496		printf("bge%d: bad VPD resource id: expected %x got %x\n",
497			sc->bge_unit, VPD_RES_ID, res.vr_id);
498		return;
499	}
500
501	pos += sizeof(res);
502	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
503	for (i = 0; i < res.vr_len; i++)
504		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
505	sc->bge_vpd_prodname[i] = '\0';
506	pos += i;
507
508	bge_vpd_read_res(sc, &res, pos);
509
510	if (res.vr_id != VPD_RES_READ) {
511		printf("bge%d: bad VPD resource id: expected %x got %x\n",
512		    sc->bge_unit, VPD_RES_READ, res.vr_id);
513		return;
514	}
515
516	pos += sizeof(res);
517	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
518	for (i = 0; i < res.vr_len + 1; i++)
519		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
520
521	return;
522}
523#endif
524
525/*
526 * Read a byte of data stored in the EEPROM at address 'addr.' The
527 * BCM570x supports both the traditional bitbang interface and an
528 * auto access interface for reading the EEPROM. We use the auto
529 * access method.
530 */
531static u_int8_t
532bge_eeprom_getbyte(sc, addr, dest)
533	struct bge_softc *sc;
534	int addr;
535	u_int8_t *dest;
536{
537	int i;
538	u_int32_t byte = 0;
539
540	/*
541	 * Enable use of auto EEPROM access so we can avoid
542	 * having to use the bitbang method.
543	 */
544	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
545
546	/* Reset the EEPROM, load the clock period. */
547	CSR_WRITE_4(sc, BGE_EE_ADDR,
548	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
549	DELAY(20);
550
551	/* Issue the read EEPROM command. */
552	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
553
554	/* Wait for completion */
555	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
556		DELAY(10);
557		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
558			break;
559	}
560
561	if (i == BGE_TIMEOUT) {
562		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
563		return(0);
564	}
565
566	/* Get result. */
567	byte = CSR_READ_4(sc, BGE_EE_DATA);
568
569	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
570
571	return(0);
572}
573
574/*
575 * Read a sequence of bytes from the EEPROM.
576 */
577static int
578bge_read_eeprom(sc, dest, off, cnt)
579	struct bge_softc *sc;
580	caddr_t dest;
581	int off;
582	int cnt;
583{
584	int err = 0, i;
585	u_int8_t byte = 0;
586
587	for (i = 0; i < cnt; i++) {
588		err = bge_eeprom_getbyte(sc, off + i, &byte);
589		if (err)
590			break;
591		*(dest + i) = byte;
592	}
593
594	return(err ? 1 : 0);
595}
596
597static int
598bge_miibus_readreg(dev, phy, reg)
599	device_t dev;
600	int phy, reg;
601{
602	struct bge_softc *sc;
603	u_int32_t val, autopoll;
604	int i;
605
606	sc = device_get_softc(dev);
607
608	/*
609	 * Broadcom's own driver always assumes the internal
610	 * PHY is at GMII address 1. On some chips, the PHY responds
611	 * to accesses at all addresses, which could cause us to
612	 * bogusly attach the PHY 32 times at probe type. Always
613	 * restricting the lookup to address 1 is simpler than
614	 * trying to figure out which chips revisions should be
615	 * special-cased.
616	 */
617	if (phy != 1)
618		return(0);
619
620	/* Reading with autopolling on may trigger PCI errors */
621	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
622	if (autopoll & BGE_MIMODE_AUTOPOLL) {
623		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
624		DELAY(40);
625	}
626
627	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
628	    BGE_MIPHY(phy)|BGE_MIREG(reg));
629
630	for (i = 0; i < BGE_TIMEOUT; i++) {
631		val = CSR_READ_4(sc, BGE_MI_COMM);
632		if (!(val & BGE_MICOMM_BUSY))
633			break;
634	}
635
636	if (i == BGE_TIMEOUT) {
637		printf("bge%d: PHY read timed out\n", sc->bge_unit);
638		val = 0;
639		goto done;
640	}
641
642	val = CSR_READ_4(sc, BGE_MI_COMM);
643
644done:
645	if (autopoll & BGE_MIMODE_AUTOPOLL) {
646		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
647		DELAY(40);
648	}
649
650	if (val & BGE_MICOMM_READFAIL)
651		return(0);
652
653	return(val & 0xFFFF);
654}
655
656static int
657bge_miibus_writereg(dev, phy, reg, val)
658	device_t dev;
659	int phy, reg, val;
660{
661	struct bge_softc *sc;
662	u_int32_t autopoll;
663	int i;
664
665	sc = device_get_softc(dev);
666
667	/* Reading with autopolling on may trigger PCI errors */
668	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
669	if (autopoll & BGE_MIMODE_AUTOPOLL) {
670		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
671		DELAY(40);
672	}
673
674	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
675	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
676
677	for (i = 0; i < BGE_TIMEOUT; i++) {
678		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
679			break;
680	}
681
682	if (autopoll & BGE_MIMODE_AUTOPOLL) {
683		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
684		DELAY(40);
685	}
686
687	if (i == BGE_TIMEOUT) {
688		printf("bge%d: PHY read timed out\n", sc->bge_unit);
689		return(0);
690	}
691
692	return(0);
693}
694
695static void
696bge_miibus_statchg(dev)
697	device_t dev;
698{
699	struct bge_softc *sc;
700	struct mii_data *mii;
701
702	sc = device_get_softc(dev);
703	mii = device_get_softc(sc->bge_miibus);
704
705	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
706	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
707		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
708	} else {
709		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
710	}
711
712	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
713		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
714	} else {
715		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
716	}
717
718	return;
719}
720
721/*
722 * Handle events that have triggered interrupts.
723 */
724static void
725bge_handle_events(sc)
726	struct bge_softc		*sc;
727{
728
729	return;
730}
731
732/*
733 * Memory management for jumbo frames.
734 */
735
736static int
737bge_alloc_jumbo_mem(sc)
738	struct bge_softc		*sc;
739{
740	caddr_t			ptr;
741	register int		i, error;
742	struct bge_jpool_entry   *entry;
743
744	/* Create tag for jumbo buffer block */
745
746	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
747	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
748	    NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL,
749	    &sc->bge_cdata.bge_jumbo_tag);
750
751	if (error) {
752		printf("bge%d: could not allocate jumbo dma tag\n",
753		    sc->bge_unit);
754		return (ENOMEM);
755	}
756
757	/* Allocate DMA'able memory for jumbo buffer block */
758
759	error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag,
760	    (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT,
761	    &sc->bge_cdata.bge_jumbo_map);
762
763	if (error)
764		return (ENOMEM);
765
766	SLIST_INIT(&sc->bge_jfree_listhead);
767	SLIST_INIT(&sc->bge_jinuse_listhead);
768
769	/*
770	 * Now divide it up into 9K pieces and save the addresses
771	 * in an array.
772	 */
773	ptr = sc->bge_ldata.bge_jumbo_buf;
774	for (i = 0; i < BGE_JSLOTS; i++) {
775		sc->bge_cdata.bge_jslots[i] = ptr;
776		ptr += BGE_JLEN;
777		entry = malloc(sizeof(struct bge_jpool_entry),
778		    M_DEVBUF, M_NOWAIT);
779		if (entry == NULL) {
780			bge_free_jumbo_mem(sc);
781			sc->bge_ldata.bge_jumbo_buf = NULL;
782			printf("bge%d: no memory for jumbo "
783			    "buffer queue!\n", sc->bge_unit);
784			return(ENOBUFS);
785		}
786		entry->slot = i;
787		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
788		    entry, jpool_entries);
789	}
790
791	return(0);
792}
793
794static void
795bge_free_jumbo_mem(sc)
796	struct bge_softc *sc;
797{
798	int i;
799	struct bge_jpool_entry *entry;
800
801	for (i = 0; i < BGE_JSLOTS; i++) {
802		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
803		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
804		free(entry, M_DEVBUF);
805	}
806
807	/* Destroy jumbo buffer block */
808
809	if (sc->bge_ldata.bge_rx_jumbo_ring)
810		bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag,
811		    sc->bge_ldata.bge_jumbo_buf,
812		    sc->bge_cdata.bge_jumbo_map);
813
814	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
815		bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag,
816		    sc->bge_cdata.bge_jumbo_map);
817
818	if (sc->bge_cdata.bge_jumbo_tag)
819		bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag);
820
821	return;
822}
823
824/*
825 * Allocate a jumbo buffer.
826 */
827static void *
828bge_jalloc(sc)
829	struct bge_softc		*sc;
830{
831	struct bge_jpool_entry   *entry;
832
833	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
834
835	if (entry == NULL) {
836		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
837		return(NULL);
838	}
839
840	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
841	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
842	return(sc->bge_cdata.bge_jslots[entry->slot]);
843}
844
845/*
846 * Release a jumbo buffer.
847 */
848static void
849bge_jfree(buf, args)
850	void *buf;
851	void *args;
852{
853	struct bge_jpool_entry *entry;
854	struct bge_softc *sc;
855	int i;
856
857	/* Extract the softc struct pointer. */
858	sc = (struct bge_softc *)args;
859
860	if (sc == NULL)
861		panic("bge_jfree: can't find softc pointer!");
862
863	/* calculate the slot this buffer belongs to */
864
865	i = ((vm_offset_t)buf
866	     - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN;
867
868	if ((i < 0) || (i >= BGE_JSLOTS))
869		panic("bge_jfree: asked to free buffer that we don't manage!");
870
871	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
872	if (entry == NULL)
873		panic("bge_jfree: buffer not in use!");
874	entry->slot = i;
875	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
876	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
877
878	return;
879}
880
881
882/*
883 * Intialize a standard receive ring descriptor.
884 */
885static int
886bge_newbuf_std(sc, i, m)
887	struct bge_softc	*sc;
888	int			i;
889	struct mbuf		*m;
890{
891	struct mbuf		*m_new = NULL;
892	struct bge_rx_bd	*r;
893	struct bge_dmamap_arg	ctx;
894	int			error;
895
896	if (m == NULL) {
897		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
898		if (m_new == NULL) {
899			return(ENOBUFS);
900		}
901
902		MCLGET(m_new, M_DONTWAIT);
903		if (!(m_new->m_flags & M_EXT)) {
904			m_freem(m_new);
905			return(ENOBUFS);
906		}
907		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
908	} else {
909		m_new = m;
910		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
911		m_new->m_data = m_new->m_ext.ext_buf;
912	}
913
914	if (!sc->bge_rx_alignment_bug)
915		m_adj(m_new, ETHER_ALIGN);
916	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
917	r = &sc->bge_ldata.bge_rx_std_ring[i];
918	ctx.bge_maxsegs = 1;
919	ctx.sc = sc;
920	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
921	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
922	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
923	if (error || ctx.bge_maxsegs == 0) {
924		if (m == NULL)
925			m_freem(m_new);
926		return(ENOMEM);
927	}
928	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
929	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
930	r->bge_flags = htole16(BGE_RXBDFLAG_END);
931	r->bge_len = htole16(m_new->m_len);
932	r->bge_idx = htole16(i);
933
934	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
935	    sc->bge_cdata.bge_rx_std_dmamap[i],
936	    BUS_DMASYNC_PREREAD);
937
938	return(0);
939}
940
941/*
942 * Initialize a jumbo receive ring descriptor. This allocates
943 * a jumbo buffer from the pool managed internally by the driver.
944 */
945static int
946bge_newbuf_jumbo(sc, i, m)
947	struct bge_softc *sc;
948	int i;
949	struct mbuf *m;
950{
951	struct mbuf *m_new = NULL;
952	struct bge_rx_bd *r;
953	struct bge_dmamap_arg ctx;
954	int error;
955
956	if (m == NULL) {
957		caddr_t			*buf = NULL;
958
959		/* Allocate the mbuf. */
960		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
961		if (m_new == NULL) {
962			return(ENOBUFS);
963		}
964
965		/* Allocate the jumbo buffer */
966		buf = bge_jalloc(sc);
967		if (buf == NULL) {
968			m_freem(m_new);
969			printf("bge%d: jumbo allocation failed "
970			    "-- packet dropped!\n", sc->bge_unit);
971			return(ENOBUFS);
972		}
973
974		/* Attach the buffer to the mbuf. */
975		m_new->m_data = (void *) buf;
976		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
977		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
978		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
979	} else {
980		m_new = m;
981		m_new->m_data = m_new->m_ext.ext_buf;
982		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
983	}
984
985	if (!sc->bge_rx_alignment_bug)
986		m_adj(m_new, ETHER_ALIGN);
987	/* Set up the descriptor. */
988	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
989	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
990	ctx.bge_maxsegs = 1;
991	ctx.sc = sc;
992	error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo,
993	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *),
994	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
995	if (error || ctx.bge_maxsegs == 0) {
996		if (m == NULL)
997			m_freem(m_new);
998		return(ENOMEM);
999	}
1000	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
1001	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
1002	r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING);
1003	r->bge_len = htole16(m_new->m_len);
1004	r->bge_idx = htole16(i);
1005
1006	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1007	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1008	    BUS_DMASYNC_PREREAD);
1009
1010	return(0);
1011}
1012
1013/*
1014 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1015 * that's 1MB or memory, which is a lot. For now, we fill only the first
1016 * 256 ring entries and hope that our CPU is fast enough to keep up with
1017 * the NIC.
1018 */
1019static int
1020bge_init_rx_ring_std(sc)
1021	struct bge_softc *sc;
1022{
1023	int i;
1024
1025	for (i = 0; i < BGE_SSLOTS; i++) {
1026		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
1027			return(ENOBUFS);
1028	};
1029
1030	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1031	    sc->bge_cdata.bge_rx_std_ring_map,
1032	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1033
1034	sc->bge_std = i - 1;
1035	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1036
1037	return(0);
1038}
1039
1040static void
1041bge_free_rx_ring_std(sc)
1042	struct bge_softc *sc;
1043{
1044	int i;
1045
1046	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1047		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1048			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1049			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1050			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1051			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1052		}
1053		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
1054		    sizeof(struct bge_rx_bd));
1055	}
1056
1057	return;
1058}
1059
1060static int
1061bge_init_rx_ring_jumbo(sc)
1062	struct bge_softc *sc;
1063{
1064	int i;
1065	struct bge_rcb *rcb;
1066
1067	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1068		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1069			return(ENOBUFS);
1070	};
1071
1072	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1073	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1074	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1075
1076	sc->bge_jumbo = i - 1;
1077
1078	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1079	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
1080	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1081
1082	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1083
1084	return(0);
1085}
1086
1087static void
1088bge_free_rx_ring_jumbo(sc)
1089	struct bge_softc *sc;
1090{
1091	int i;
1092
1093	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1094		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1095			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1096			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1097			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1098			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1099		}
1100		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
1101		    sizeof(struct bge_rx_bd));
1102	}
1103
1104	return;
1105}
1106
1107static void
1108bge_free_tx_ring(sc)
1109	struct bge_softc *sc;
1110{
1111	int i;
1112
1113	if (sc->bge_ldata.bge_tx_ring == NULL)
1114		return;
1115
1116	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1117		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1118			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1119			sc->bge_cdata.bge_tx_chain[i] = NULL;
1120			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1121			    sc->bge_cdata.bge_tx_dmamap[i]);
1122		}
1123		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
1124		    sizeof(struct bge_tx_bd));
1125	}
1126
1127	return;
1128}
1129
1130static int
1131bge_init_tx_ring(sc)
1132	struct bge_softc *sc;
1133{
1134	sc->bge_txcnt = 0;
1135	sc->bge_tx_saved_considx = 0;
1136
1137	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1138	/* 5700 b2 errata */
1139	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1140		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1141
1142	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1143	/* 5700 b2 errata */
1144	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1145		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1146
1147	return(0);
1148}
1149
1150static void
1151bge_setmulti(sc)
1152	struct bge_softc *sc;
1153{
1154	struct ifnet *ifp;
1155	struct ifmultiaddr *ifma;
1156	u_int32_t hashes[4] = { 0, 0, 0, 0 };
1157	int h, i;
1158
1159	BGE_LOCK_ASSERT(sc);
1160
1161	ifp = sc->bge_ifp;
1162
1163	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1164		for (i = 0; i < 4; i++)
1165			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1166		return;
1167	}
1168
1169	/* First, zot all the existing filters. */
1170	for (i = 0; i < 4; i++)
1171		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1172
1173	/* Now program new ones. */
1174	IF_ADDR_LOCK(ifp);
1175	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1176		if (ifma->ifma_addr->sa_family != AF_LINK)
1177			continue;
1178		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
1179		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
1180		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1181	}
1182	IF_ADDR_UNLOCK(ifp);
1183
1184	for (i = 0; i < 4; i++)
1185		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1186
1187	return;
1188}
1189
1190/*
1191 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1192 * self-test results.
1193 */
1194static int
1195bge_chipinit(sc)
1196	struct bge_softc *sc;
1197{
1198	int			i;
1199	u_int32_t		dma_rw_ctl;
1200
1201	/* Set endianness before we access any non-PCI registers. */
1202#if BYTE_ORDER == BIG_ENDIAN
1203	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
1204	    BGE_BIGENDIAN_INIT, 4);
1205#else
1206	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
1207	    BGE_LITTLEENDIAN_INIT, 4);
1208#endif
1209
1210	/*
1211	 * Check the 'ROM failed' bit on the RX CPU to see if
1212	 * self-tests passed.
1213	 */
1214	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1215		printf("bge%d: RX CPU self-diagnostics failed!\n",
1216		    sc->bge_unit);
1217		return(ENODEV);
1218	}
1219
1220	/* Clear the MAC control register */
1221	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1222
1223	/*
1224	 * Clear the MAC statistics block in the NIC's
1225	 * internal memory.
1226	 */
1227	for (i = BGE_STATS_BLOCK;
1228	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
1229		BGE_MEMWIN_WRITE(sc, i, 0);
1230
1231	for (i = BGE_STATUS_BLOCK;
1232	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
1233		BGE_MEMWIN_WRITE(sc, i, 0);
1234
1235	/* Set up the PCI DMA control register. */
1236	if (sc->bge_pcie) {
1237		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1238		    (0xf << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1239		    (0x2 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1240	} else if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
1241	    BGE_PCISTATE_PCI_BUSMODE) {
1242		/* Conventional PCI bus */
1243		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1244		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1245		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1246		    (0x0F);
1247	} else {
1248		/* PCI-X bus */
1249		/*
1250		 * The 5704 uses a different encoding of read/write
1251		 * watermarks.
1252		 */
1253		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1254			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1255			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1256			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1257		else
1258			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1259			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1260			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1261			    (0x0F);
1262
1263		/*
1264		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
1265		 * for hardware bugs.
1266		 */
1267		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1268		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1269			u_int32_t tmp;
1270
1271			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
1272			if (tmp == 0x6 || tmp == 0x7)
1273				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
1274		}
1275	}
1276
1277	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1278	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1279	    sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1280	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
1281		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1282	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1283
1284	/*
1285	 * Set up general mode register.
1286	 */
1287	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
1288	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1289	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1290	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
1291
1292	/*
1293	 * Disable memory write invalidate.  Apparently it is not supported
1294	 * properly by these devices.
1295	 */
1296	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
1297
1298#ifdef __brokenalpha__
1299	/*
1300	 * Must insure that we do not cross an 8K (bytes) boundary
1301	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1302	 * restriction on some ALPHA platforms with early revision
1303	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1304	 */
1305	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
1306	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
1307#endif
1308
1309	/* Set the timer prescaler (always 66Mhz) */
1310	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1311
1312	return(0);
1313}
1314
1315static int
1316bge_blockinit(sc)
1317	struct bge_softc *sc;
1318{
1319	struct bge_rcb *rcb;
1320	volatile struct bge_rcb *vrcb;
1321	int i;
1322
1323	/*
1324	 * Initialize the memory window pointer register so that
1325	 * we can access the first 32K of internal NIC RAM. This will
1326	 * allow us to set up the TX send ring RCBs and the RX return
1327	 * ring RCBs, plus other things which live in NIC memory.
1328	 */
1329	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1330
1331	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1332
1333	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1334	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1335		/* Configure mbuf memory pool */
1336		if (sc->bge_extram) {
1337			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1338			    BGE_EXT_SSRAM);
1339			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1340				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1341			else
1342				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1343		} else {
1344			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1345			    BGE_BUFFPOOL_1);
1346			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1347				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1348			else
1349				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1350		}
1351
1352		/* Configure DMA resource pool */
1353		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1354		    BGE_DMA_DESCRIPTORS);
1355		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1356	}
1357
1358	/* Configure mbuf pool watermarks */
1359	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1360	    sc->bge_asicrev == BGE_ASICREV_BCM5750) {
1361		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1362		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1363	} else {
1364		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1365		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1366	}
1367	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1368
1369	/* Configure DMA resource watermarks */
1370	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1371	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1372
1373	/* Enable buffer manager */
1374	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1375	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1376		CSR_WRITE_4(sc, BGE_BMAN_MODE,
1377		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
1378
1379		/* Poll for buffer manager start indication */
1380		for (i = 0; i < BGE_TIMEOUT; i++) {
1381			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1382				break;
1383			DELAY(10);
1384		}
1385
1386		if (i == BGE_TIMEOUT) {
1387			printf("bge%d: buffer manager failed to start\n",
1388			    sc->bge_unit);
1389			return(ENXIO);
1390		}
1391	}
1392
1393	/* Enable flow-through queues */
1394	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1395	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1396
1397	/* Wait until queue initialization is complete */
1398	for (i = 0; i < BGE_TIMEOUT; i++) {
1399		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1400			break;
1401		DELAY(10);
1402	}
1403
1404	if (i == BGE_TIMEOUT) {
1405		printf("bge%d: flow-through queue init failed\n",
1406		    sc->bge_unit);
1407		return(ENXIO);
1408	}
1409
1410	/* Initialize the standard RX ring control block */
1411	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1412	rcb->bge_hostaddr.bge_addr_lo =
1413	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1414	rcb->bge_hostaddr.bge_addr_hi =
1415	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1416	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1417	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1418	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
1419	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
1420		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1421	else
1422		rcb->bge_maxlen_flags =
1423		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1424	if (sc->bge_extram)
1425		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
1426	else
1427		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1428	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1429	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1430
1431	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1432	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1433
1434	/*
1435	 * Initialize the jumbo RX ring control block
1436	 * We set the 'ring disabled' bit in the flags
1437	 * field until we're actually ready to start
1438	 * using this ring (i.e. once we set the MTU
1439	 * high enough to require it).
1440	 */
1441	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1442	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1443		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1444
1445		rcb->bge_hostaddr.bge_addr_lo =
1446		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1447		rcb->bge_hostaddr.bge_addr_hi =
1448		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1449		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1450		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1451		    BUS_DMASYNC_PREREAD);
1452		rcb->bge_maxlen_flags =
1453		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
1454		    BGE_RCB_FLAG_RING_DISABLED);
1455		if (sc->bge_extram)
1456			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1457		else
1458			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1459		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1460		    rcb->bge_hostaddr.bge_addr_hi);
1461		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1462		    rcb->bge_hostaddr.bge_addr_lo);
1463
1464		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1465		    rcb->bge_maxlen_flags);
1466		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1467
1468		/* Set up dummy disabled mini ring RCB */
1469		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
1470		rcb->bge_maxlen_flags =
1471		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
1472		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
1473		    rcb->bge_maxlen_flags);
1474	}
1475
1476	/*
1477	 * Set the BD ring replentish thresholds. The recommended
1478	 * values are 1/8th the number of descriptors allocated to
1479	 * each ring.
1480	 */
1481	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
1482	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1483
1484	/*
1485	 * Disable all unused send rings by setting the 'ring disabled'
1486	 * bit in the flags field of all the TX send ring control blocks.
1487	 * These are located in NIC memory.
1488	 */
1489	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1490	    BGE_SEND_RING_RCB);
1491	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1492		vrcb->bge_maxlen_flags =
1493		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
1494		vrcb->bge_nicaddr = 0;
1495		vrcb++;
1496	}
1497
1498	/* Configure TX RCB 0 (we use only the first ring) */
1499	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1500	    BGE_SEND_RING_RCB);
1501	vrcb->bge_hostaddr.bge_addr_lo =
1502	    htole32(BGE_ADDR_LO(sc->bge_ldata.bge_tx_ring_paddr));
1503	vrcb->bge_hostaddr.bge_addr_hi =
1504	    htole32(BGE_ADDR_HI(sc->bge_ldata.bge_tx_ring_paddr));
1505	vrcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
1506	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1507	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1508		vrcb->bge_maxlen_flags =
1509		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0);
1510
1511	/* Disable all unused RX return rings */
1512	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1513	    BGE_RX_RETURN_RING_RCB);
1514	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1515		vrcb->bge_hostaddr.bge_addr_hi = 0;
1516		vrcb->bge_hostaddr.bge_addr_lo = 0;
1517		vrcb->bge_maxlen_flags =
1518		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1519		    BGE_RCB_FLAG_RING_DISABLED);
1520		vrcb->bge_nicaddr = 0;
1521		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
1522		    (i * (sizeof(u_int64_t))), 0);
1523		vrcb++;
1524	}
1525
1526	/* Initialize RX ring indexes */
1527	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1528	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1529	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1530
1531	/*
1532	 * Set up RX return ring 0
1533	 * Note that the NIC address for RX return rings is 0x00000000.
1534	 * The return rings live entirely within the host, so the
1535	 * nicaddr field in the RCB isn't used.
1536	 */
1537	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1538	    BGE_RX_RETURN_RING_RCB);
1539	vrcb->bge_hostaddr.bge_addr_lo =
1540	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_return_ring_paddr);
1541	vrcb->bge_hostaddr.bge_addr_hi =
1542	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_return_ring_paddr);
1543	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
1544	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
1545	vrcb->bge_nicaddr = 0x00000000;
1546	vrcb->bge_maxlen_flags =
1547	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0);
1548
1549	/* Set random backoff seed for TX */
1550	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1551	    IFP2ENADDR(sc->bge_ifp)[0] + IFP2ENADDR(sc->bge_ifp)[1] +
1552	    IFP2ENADDR(sc->bge_ifp)[2] + IFP2ENADDR(sc->bge_ifp)[3] +
1553	    IFP2ENADDR(sc->bge_ifp)[4] + IFP2ENADDR(sc->bge_ifp)[5] +
1554	    BGE_TX_BACKOFF_SEED_MASK);
1555
1556	/* Set inter-packet gap */
1557	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1558
1559	/*
1560	 * Specify which ring to use for packets that don't match
1561	 * any RX rules.
1562	 */
1563	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1564
1565	/*
1566	 * Configure number of RX lists. One interrupt distribution
1567	 * list, sixteen active lists, one bad frames class.
1568	 */
1569	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1570
1571	/* Inialize RX list placement stats mask. */
1572	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1573	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1574
1575	/* Disable host coalescing until we get it set up */
1576	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1577
1578	/* Poll to make sure it's shut down. */
1579	for (i = 0; i < BGE_TIMEOUT; i++) {
1580		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1581			break;
1582		DELAY(10);
1583	}
1584
1585	if (i == BGE_TIMEOUT) {
1586		printf("bge%d: host coalescing engine failed to idle\n",
1587		    sc->bge_unit);
1588		return(ENXIO);
1589	}
1590
1591	/* Set up host coalescing defaults */
1592	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1593	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1594	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1595	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1596	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1597	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1598		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1599		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1600	}
1601	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
1602	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
1603
1604	/* Set up address of statistics block */
1605	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1606	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
1607		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1608		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
1609		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1610		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
1611		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1612		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1613		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1614	}
1615
1616	/* Set up address of status block */
1617	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1618	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
1619	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1620	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1621	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1622	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
1623	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1624	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
1625
1626	/* Turn on host coalescing state machine */
1627	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1628
1629	/* Turn on RX BD completion state machine and enable attentions */
1630	CSR_WRITE_4(sc, BGE_RBDC_MODE,
1631	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1632
1633	/* Turn on RX list placement state machine */
1634	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1635
1636	/* Turn on RX list selector state machine. */
1637	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1638	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1639		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1640
1641	/* Turn on DMA, clear stats */
1642	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
1643	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
1644	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
1645	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1646	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1647
1648	/* Set misc. local control, enable interrupts on attentions */
1649	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1650
1651#ifdef notdef
1652	/* Assert GPIO pins for PHY reset */
1653	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1654	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1655	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1656	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1657#endif
1658
1659	/* Turn on DMA completion state machine */
1660	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1661	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1662		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1663
1664	/* Turn on write DMA state machine */
1665	CSR_WRITE_4(sc, BGE_WDMA_MODE,
1666	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
1667
1668	/* Turn on read DMA state machine */
1669	CSR_WRITE_4(sc, BGE_RDMA_MODE,
1670	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
1671
1672	/* Turn on RX data completion state machine */
1673	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1674
1675	/* Turn on RX BD initiator state machine */
1676	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1677
1678	/* Turn on RX data and RX BD initiator state machine */
1679	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1680
1681	/* Turn on Mbuf cluster free state machine */
1682	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1683	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1684		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1685
1686	/* Turn on send BD completion state machine */
1687	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1688
1689	/* Turn on send data completion state machine */
1690	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1691
1692	/* Turn on send data initiator state machine */
1693	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1694
1695	/* Turn on send BD initiator state machine */
1696	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1697
1698	/* Turn on send BD selector state machine */
1699	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1700
1701	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1702	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1703	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1704
1705	/* ack/clear link change events */
1706	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1707	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1708	    BGE_MACSTAT_LINK_CHANGED);
1709	CSR_WRITE_4(sc, BGE_MI_STS, 0);
1710
1711	/* Enable PHY auto polling (for MII/GMII only) */
1712	if (sc->bge_tbi) {
1713		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1714	} else {
1715		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1716		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1717			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1718			    BGE_EVTENB_MI_INTERRUPT);
1719	}
1720
1721	/* Enable link state change attentions. */
1722	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1723
1724	return(0);
1725}
1726
1727/*
1728 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1729 * against our list and return its name if we find a match. Note
1730 * that since the Broadcom controller contains VPD support, we
1731 * can get the device name string from the controller itself instead
1732 * of the compiled-in string. This is a little slow, but it guarantees
1733 * we'll always announce the right product name.
1734 */
1735static int
1736bge_probe(dev)
1737	device_t dev;
1738{
1739	struct bge_type *t;
1740	struct bge_softc *sc;
1741	char *descbuf;
1742
1743	t = bge_devs;
1744
1745	sc = device_get_softc(dev);
1746	bzero(sc, sizeof(struct bge_softc));
1747	sc->bge_unit = device_get_unit(dev);
1748	sc->bge_dev = dev;
1749
1750	while(t->bge_name != NULL) {
1751		if ((pci_get_vendor(dev) == t->bge_vid) &&
1752		    (pci_get_device(dev) == t->bge_did)) {
1753#ifdef notdef
1754			bge_vpd_read(sc);
1755			device_set_desc(dev, sc->bge_vpd_prodname);
1756#endif
1757			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1758			if (descbuf == NULL)
1759				return(ENOMEM);
1760			snprintf(descbuf, BGE_DEVDESC_MAX,
1761			    "%s, ASIC rev. %#04x", t->bge_name,
1762			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1763			device_set_desc_copy(dev, descbuf);
1764			if (pci_get_subvendor(dev) == DELL_VENDORID)
1765				sc->bge_no_3_led = 1;
1766			free(descbuf, M_TEMP);
1767			return(0);
1768		}
1769		t++;
1770	}
1771
1772	return(ENXIO);
1773}
1774
1775static void
1776bge_dma_free(sc)
1777	struct bge_softc *sc;
1778{
1779	int i;
1780
1781
1782	/* Destroy DMA maps for RX buffers */
1783
1784	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1785		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1786			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1787			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1788	}
1789
1790	/* Destroy DMA maps for jumbo RX buffers */
1791
1792	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1793		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1794			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1795			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1796	}
1797
1798	/* Destroy DMA maps for TX buffers */
1799
1800	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1801		if (sc->bge_cdata.bge_tx_dmamap[i])
1802			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1803			    sc->bge_cdata.bge_tx_dmamap[i]);
1804	}
1805
1806	if (sc->bge_cdata.bge_mtag)
1807		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1808
1809
1810	/* Destroy standard RX ring */
1811
1812	if (sc->bge_ldata.bge_rx_std_ring)
1813		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1814		    sc->bge_ldata.bge_rx_std_ring,
1815		    sc->bge_cdata.bge_rx_std_ring_map);
1816
1817	if (sc->bge_cdata.bge_rx_std_ring_map) {
1818		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1819		    sc->bge_cdata.bge_rx_std_ring_map);
1820		bus_dmamap_destroy(sc->bge_cdata.bge_rx_std_ring_tag,
1821		    sc->bge_cdata.bge_rx_std_ring_map);
1822	}
1823
1824	if (sc->bge_cdata.bge_rx_std_ring_tag)
1825		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1826
1827	/* Destroy jumbo RX ring */
1828
1829	if (sc->bge_ldata.bge_rx_jumbo_ring)
1830		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1831		    sc->bge_ldata.bge_rx_jumbo_ring,
1832		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1833
1834	if (sc->bge_cdata.bge_rx_jumbo_ring_map) {
1835		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1836		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1837		bus_dmamap_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1838		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1839	}
1840
1841	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1842		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1843
1844	/* Destroy RX return ring */
1845
1846	if (sc->bge_ldata.bge_rx_return_ring)
1847		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1848		    sc->bge_ldata.bge_rx_return_ring,
1849		    sc->bge_cdata.bge_rx_return_ring_map);
1850
1851	if (sc->bge_cdata.bge_rx_return_ring_map) {
1852		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1853		    sc->bge_cdata.bge_rx_return_ring_map);
1854		bus_dmamap_destroy(sc->bge_cdata.bge_rx_return_ring_tag,
1855		    sc->bge_cdata.bge_rx_return_ring_map);
1856	}
1857
1858	if (sc->bge_cdata.bge_rx_return_ring_tag)
1859		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1860
1861	/* Destroy TX ring */
1862
1863	if (sc->bge_ldata.bge_tx_ring)
1864		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1865		    sc->bge_ldata.bge_tx_ring,
1866		    sc->bge_cdata.bge_tx_ring_map);
1867
1868	if (sc->bge_cdata.bge_tx_ring_map) {
1869		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1870		    sc->bge_cdata.bge_tx_ring_map);
1871		bus_dmamap_destroy(sc->bge_cdata.bge_tx_ring_tag,
1872		    sc->bge_cdata.bge_tx_ring_map);
1873	}
1874
1875	if (sc->bge_cdata.bge_tx_ring_tag)
1876		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1877
1878	/* Destroy status block */
1879
1880	if (sc->bge_ldata.bge_status_block)
1881		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1882		    sc->bge_ldata.bge_status_block,
1883		    sc->bge_cdata.bge_status_map);
1884
1885	if (sc->bge_cdata.bge_status_map) {
1886		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1887		    sc->bge_cdata.bge_status_map);
1888		bus_dmamap_destroy(sc->bge_cdata.bge_status_tag,
1889		    sc->bge_cdata.bge_status_map);
1890	}
1891
1892	if (sc->bge_cdata.bge_status_tag)
1893		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1894
1895	/* Destroy statistics block */
1896
1897	if (sc->bge_ldata.bge_stats)
1898		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1899		    sc->bge_ldata.bge_stats,
1900		    sc->bge_cdata.bge_stats_map);
1901
1902	if (sc->bge_cdata.bge_stats_map) {
1903		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1904		    sc->bge_cdata.bge_stats_map);
1905		bus_dmamap_destroy(sc->bge_cdata.bge_stats_tag,
1906		    sc->bge_cdata.bge_stats_map);
1907	}
1908
1909	if (sc->bge_cdata.bge_stats_tag)
1910		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1911
1912	/* Destroy the parent tag */
1913
1914	if (sc->bge_cdata.bge_parent_tag)
1915		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1916
1917	return;
1918}
1919
1920static int
1921bge_dma_alloc(dev)
1922	device_t dev;
1923{
1924	struct bge_softc *sc;
1925	int nseg, i, error;
1926	struct bge_dmamap_arg ctx;
1927
1928	sc = device_get_softc(dev);
1929
1930	/*
1931	 * Allocate the parent bus DMA tag appropriate for PCI.
1932	 */
1933#define BGE_NSEG_NEW 32
1934	error = bus_dma_tag_create(NULL,	/* parent */
1935			PAGE_SIZE, 0,		/* alignment, boundary */
1936			BUS_SPACE_MAXADDR,	/* lowaddr */
1937			BUS_SPACE_MAXADDR,	/* highaddr */
1938			NULL, NULL,		/* filter, filterarg */
1939			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1940			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1941			0,			/* flags */
1942			NULL, NULL,		/* lockfunc, lockarg */
1943			&sc->bge_cdata.bge_parent_tag);
1944
1945	/*
1946	 * Create tag for RX mbufs.
1947	 */
1948	nseg = 32;
1949	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1950	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1951	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL,
1952	    &sc->bge_cdata.bge_mtag);
1953
1954	if (error) {
1955		device_printf(dev, "could not allocate dma tag\n");
1956		return (ENOMEM);
1957	}
1958
1959	/* Create DMA maps for RX buffers */
1960
1961	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1962		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1963			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1964		if (error) {
1965			device_printf(dev, "can't create DMA map for RX\n");
1966			return(ENOMEM);
1967		}
1968	}
1969
1970	/* Create DMA maps for TX buffers */
1971
1972	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1973		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1974			    &sc->bge_cdata.bge_tx_dmamap[i]);
1975		if (error) {
1976			device_printf(dev, "can't create DMA map for RX\n");
1977			return(ENOMEM);
1978		}
1979	}
1980
1981	/* Create tag for standard RX ring */
1982
1983	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1984	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1985	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1986	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1987
1988	if (error) {
1989		device_printf(dev, "could not allocate dma tag\n");
1990		return (ENOMEM);
1991	}
1992
1993	/* Allocate DMA'able memory for standard RX ring */
1994
1995	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1996	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1997	    &sc->bge_cdata.bge_rx_std_ring_map);
1998	if (error)
1999		return (ENOMEM);
2000
2001	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
2002
2003	/* Load the address of the standard RX ring */
2004
2005	ctx.bge_maxsegs = 1;
2006	ctx.sc = sc;
2007
2008	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
2009	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
2010	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2011
2012	if (error)
2013		return (ENOMEM);
2014
2015	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2016
2017	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2018	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2019
2020		/*
2021		 * Create tag for jumbo mbufs.
2022		 * This is really a bit of a kludge. We allocate a special
2023		 * jumbo buffer pool which (thanks to the way our DMA
2024		 * memory allocation works) will consist of contiguous
2025		 * pages. This means that even though a jumbo buffer might
2026		 * be larger than a page size, we don't really need to
2027		 * map it into more than one DMA segment. However, the
2028		 * default mbuf tag will result in multi-segment mappings,
2029		 * so we have to create a special jumbo mbuf tag that
2030		 * lets us get away with mapping the jumbo buffers as
2031		 * a single segment. I think eventually the driver should
2032		 * be changed so that it uses ordinary mbufs and cluster
2033		 * buffers, i.e. jumbo frames can span multiple DMA
2034		 * descriptors. But that's a project for another day.
2035		 */
2036
2037		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2038		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2039		    NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL,
2040		    &sc->bge_cdata.bge_mtag_jumbo);
2041
2042		if (error) {
2043			device_printf(dev, "could not allocate dma tag\n");
2044			return (ENOMEM);
2045		}
2046
2047		/* Create tag for jumbo RX ring */
2048
2049		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2050		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2051		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2052		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2053
2054		if (error) {
2055			device_printf(dev, "could not allocate dma tag\n");
2056			return (ENOMEM);
2057		}
2058
2059		/* Allocate DMA'able memory for jumbo RX ring */
2060
2061		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2062		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT,
2063		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2064		if (error)
2065			return (ENOMEM);
2066
2067		bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring,
2068		    BGE_JUMBO_RX_RING_SZ);
2069
2070		/* Load the address of the jumbo RX ring */
2071
2072		ctx.bge_maxsegs = 1;
2073		ctx.sc = sc;
2074
2075		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2076		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2077		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2078		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2079
2080		if (error)
2081			return (ENOMEM);
2082
2083		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2084
2085		/* Create DMA maps for jumbo RX buffers */
2086
2087		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2088			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2089				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2090			if (error) {
2091				device_printf(dev,
2092				    "can't create DMA map for RX\n");
2093				return(ENOMEM);
2094			}
2095		}
2096
2097	}
2098
2099	/* Create tag for RX return ring */
2100
2101	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2102	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2103	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2104	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2105
2106	if (error) {
2107		device_printf(dev, "could not allocate dma tag\n");
2108		return (ENOMEM);
2109	}
2110
2111	/* Allocate DMA'able memory for RX return ring */
2112
2113	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2114	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2115	    &sc->bge_cdata.bge_rx_return_ring_map);
2116	if (error)
2117		return (ENOMEM);
2118
2119	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2120	    BGE_RX_RTN_RING_SZ(sc));
2121
2122	/* Load the address of the RX return ring */
2123
2124	ctx.bge_maxsegs = 1;
2125	ctx.sc = sc;
2126
2127	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2128	    sc->bge_cdata.bge_rx_return_ring_map,
2129	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2130	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2131
2132	if (error)
2133		return (ENOMEM);
2134
2135	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2136
2137	/* Create tag for TX ring */
2138
2139	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2140	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2141	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2142	    &sc->bge_cdata.bge_tx_ring_tag);
2143
2144	if (error) {
2145		device_printf(dev, "could not allocate dma tag\n");
2146		return (ENOMEM);
2147	}
2148
2149	/* Allocate DMA'able memory for TX ring */
2150
2151	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2152	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2153	    &sc->bge_cdata.bge_tx_ring_map);
2154	if (error)
2155		return (ENOMEM);
2156
2157	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2158
2159	/* Load the address of the TX ring */
2160
2161	ctx.bge_maxsegs = 1;
2162	ctx.sc = sc;
2163
2164	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2165	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2166	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2167
2168	if (error)
2169		return (ENOMEM);
2170
2171	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2172
2173	/* Create tag for status block */
2174
2175	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2176	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2177	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2178	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2179
2180	if (error) {
2181		device_printf(dev, "could not allocate dma tag\n");
2182		return (ENOMEM);
2183	}
2184
2185	/* Allocate DMA'able memory for status block */
2186
2187	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2188	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2189	    &sc->bge_cdata.bge_status_map);
2190	if (error)
2191		return (ENOMEM);
2192
2193	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2194
2195	/* Load the address of the status block */
2196
2197	ctx.sc = sc;
2198	ctx.bge_maxsegs = 1;
2199
2200	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2201	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2202	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2203
2204	if (error)
2205		return (ENOMEM);
2206
2207	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2208
2209	/* Create tag for statistics block */
2210
2211	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2212	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2213	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2214	    &sc->bge_cdata.bge_stats_tag);
2215
2216	if (error) {
2217		device_printf(dev, "could not allocate dma tag\n");
2218		return (ENOMEM);
2219	}
2220
2221	/* Allocate DMA'able memory for statistics block */
2222
2223	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2224	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2225	    &sc->bge_cdata.bge_stats_map);
2226	if (error)
2227		return (ENOMEM);
2228
2229	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2230
2231	/* Load the address of the statstics block */
2232
2233	ctx.sc = sc;
2234	ctx.bge_maxsegs = 1;
2235
2236	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2237	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2238	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2239
2240	if (error)
2241		return (ENOMEM);
2242
2243	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2244
2245	return(0);
2246}
2247
2248static int
2249bge_attach(dev)
2250	device_t dev;
2251{
2252	struct ifnet *ifp;
2253	struct bge_softc *sc;
2254	u_int32_t hwcfg = 0;
2255	u_int32_t mac_tmp = 0;
2256	u_char eaddr[6];
2257	int unit, error = 0, rid;
2258
2259	sc = device_get_softc(dev);
2260	unit = device_get_unit(dev);
2261	sc->bge_dev = dev;
2262	sc->bge_unit = unit;
2263
2264	/*
2265	 * Map control/status registers.
2266	 */
2267	pci_enable_busmaster(dev);
2268
2269	rid = BGE_PCI_BAR0;
2270	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2271	    RF_ACTIVE|PCI_RF_DENSE);
2272
2273	if (sc->bge_res == NULL) {
2274		printf ("bge%d: couldn't map memory\n", unit);
2275		error = ENXIO;
2276		goto fail;
2277	}
2278
2279	sc->bge_btag = rman_get_bustag(sc->bge_res);
2280	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
2281	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
2282
2283	/* Allocate interrupt */
2284	rid = 0;
2285
2286	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2287	    RF_SHAREABLE | RF_ACTIVE);
2288
2289	if (sc->bge_irq == NULL) {
2290		printf("bge%d: couldn't map interrupt\n", unit);
2291		error = ENXIO;
2292		goto fail;
2293	}
2294
2295	sc->bge_unit = unit;
2296
2297	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2298
2299	/* Save ASIC rev. */
2300
2301	sc->bge_chipid =
2302	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2303	    BGE_PCIMISCCTL_ASICREV;
2304	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2305	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2306
2307	/*
2308	 * Treat the 5714 like the 5750 until we have more info
2309	 * on this chip.
2310	 */
2311	if (sc->bge_asicrev == BGE_ASICREV_BCM5714)
2312		sc->bge_asicrev = BGE_ASICREV_BCM5750;
2313
2314	/*
2315	 * XXX: Broadcom Linux driver.  Not in specs or eratta.
2316	 * PCI-Express?
2317	 */
2318	if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2319		u_int32_t v;
2320
2321		v = pci_read_config(dev, BGE_PCI_MSI_CAPID, 4);
2322		if (((v >> 8) & 0xff) == BGE_PCIE_CAPID_REG) {
2323			v = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2324			if ((v & 0xff) == BGE_PCIE_CAPID)
2325				sc->bge_pcie = 1;
2326		}
2327	}
2328
2329	/* Try to reset the chip. */
2330	bge_reset(sc);
2331
2332	if (bge_chipinit(sc)) {
2333		printf("bge%d: chip initialization failed\n", sc->bge_unit);
2334		bge_release_resources(sc);
2335		error = ENXIO;
2336		goto fail;
2337	}
2338
2339	/*
2340	 * Get station address from the EEPROM.
2341	 */
2342	mac_tmp = bge_readmem_ind(sc, 0x0c14);
2343	if ((mac_tmp >> 16) == 0x484b) {
2344		eaddr[0] = (u_char)(mac_tmp >> 8);
2345		eaddr[1] = (u_char)mac_tmp;
2346		mac_tmp = bge_readmem_ind(sc, 0x0c18);
2347		eaddr[2] = (u_char)(mac_tmp >> 24);
2348		eaddr[3] = (u_char)(mac_tmp >> 16);
2349		eaddr[4] = (u_char)(mac_tmp >> 8);
2350		eaddr[5] = (u_char)mac_tmp;
2351	} else if (bge_read_eeprom(sc, eaddr,
2352	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2353		printf("bge%d: failed to read station address\n", unit);
2354		bge_release_resources(sc);
2355		error = ENXIO;
2356		goto fail;
2357	}
2358
2359	/* 5705 limits RX return ring to 512 entries. */
2360	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
2361	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
2362		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2363	else
2364		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2365
2366	if (bge_dma_alloc(dev)) {
2367		printf ("bge%d: failed to allocate DMA resources\n",
2368		    sc->bge_unit);
2369		bge_release_resources(sc);
2370		error = ENXIO;
2371		goto fail;
2372	}
2373
2374	/*
2375	 * Try to allocate memory for jumbo buffers.
2376	 * The 5705 does not appear to support jumbo frames.
2377	 */
2378	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2379	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2380		if (bge_alloc_jumbo_mem(sc)) {
2381			printf("bge%d: jumbo buffer allocation "
2382			    "failed\n", sc->bge_unit);
2383			bge_release_resources(sc);
2384			error = ENXIO;
2385			goto fail;
2386		}
2387	}
2388
2389	/* Set default tuneable values. */
2390	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2391	sc->bge_rx_coal_ticks = 150;
2392	sc->bge_tx_coal_ticks = 150;
2393	sc->bge_rx_max_coal_bds = 64;
2394	sc->bge_tx_max_coal_bds = 128;
2395
2396	/* Set up ifnet structure */
2397	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2398	if (ifp == NULL) {
2399		printf("bge%d: failed to if_alloc()\n", sc->bge_unit);
2400		bge_release_resources(sc);
2401		error = ENXIO;
2402		goto fail;
2403	}
2404	ifp->if_softc = sc;
2405	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2406	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2407	ifp->if_ioctl = bge_ioctl;
2408	ifp->if_start = bge_start;
2409	ifp->if_watchdog = bge_watchdog;
2410	ifp->if_init = bge_init;
2411	ifp->if_mtu = ETHERMTU;
2412	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
2413	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
2414	IFQ_SET_READY(&ifp->if_snd);
2415	ifp->if_hwassist = BGE_CSUM_FEATURES;
2416	/* NB: the code for RX csum offload is disabled for now */
2417	ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_VLAN_HWTAGGING |
2418	    IFCAP_VLAN_MTU;
2419	ifp->if_capenable = ifp->if_capabilities;
2420
2421	/*
2422	 * Figure out what sort of media we have by checking the
2423	 * hardware config word in the first 32k of NIC internal memory,
2424	 * or fall back to examining the EEPROM if necessary.
2425	 * Note: on some BCM5700 cards, this value appears to be unset.
2426	 * If that's the case, we have to rely on identifying the NIC
2427	 * by its PCI subsystem ID, as we do below for the SysKonnect
2428	 * SK-9D41.
2429	 */
2430	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
2431		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2432	else {
2433		bge_read_eeprom(sc, (caddr_t)&hwcfg,
2434				BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
2435		hwcfg = ntohl(hwcfg);
2436	}
2437
2438	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2439		sc->bge_tbi = 1;
2440
2441	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2442	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2443		sc->bge_tbi = 1;
2444
2445	if (sc->bge_tbi) {
2446		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
2447		    bge_ifmedia_upd, bge_ifmedia_sts);
2448		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2449		ifmedia_add(&sc->bge_ifmedia,
2450		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2451		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2452		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2453		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
2454	} else {
2455		/*
2456		 * Do transceiver setup.
2457		 */
2458		if (mii_phy_probe(dev, &sc->bge_miibus,
2459		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2460			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
2461			bge_release_resources(sc);
2462			bge_free_jumbo_mem(sc);
2463			if_free(ifp);
2464			error = ENXIO;
2465			goto fail;
2466		}
2467	}
2468
2469	/*
2470	 * When using the BCM5701 in PCI-X mode, data corruption has
2471	 * been observed in the first few bytes of some received packets.
2472	 * Aligning the packet buffer in memory eliminates the corruption.
2473	 * Unfortunately, this misaligns the packet payloads.  On platforms
2474	 * which do not support unaligned accesses, we will realign the
2475	 * payloads by copying the received packets.
2476	 */
2477	switch (sc->bge_chipid) {
2478	case BGE_CHIPID_BCM5701_A0:
2479	case BGE_CHIPID_BCM5701_B0:
2480	case BGE_CHIPID_BCM5701_B2:
2481	case BGE_CHIPID_BCM5701_B5:
2482		/* If in PCI-X mode, work around the alignment bug. */
2483		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2484		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2485		    BGE_PCISTATE_PCI_BUSSPEED)
2486			sc->bge_rx_alignment_bug = 1;
2487		break;
2488	}
2489
2490	/*
2491	 * Call MI attach routine.
2492	 */
2493	ether_ifattach(ifp, eaddr);
2494	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
2495
2496	/*
2497	 * Hookup IRQ last.
2498	 */
2499	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2500	   bge_intr, sc, &sc->bge_intrhand);
2501
2502	if (error) {
2503		bge_detach(dev);
2504		printf("bge%d: couldn't set up irq\n", unit);
2505	}
2506
2507fail:
2508	return(error);
2509}
2510
2511static int
2512bge_detach(dev)
2513	device_t dev;
2514{
2515	struct bge_softc *sc;
2516	struct ifnet *ifp;
2517
2518	sc = device_get_softc(dev);
2519	ifp = sc->bge_ifp;
2520
2521	BGE_LOCK(sc);
2522	bge_stop(sc);
2523	bge_reset(sc);
2524	BGE_UNLOCK(sc);
2525
2526	ether_ifdetach(ifp);
2527	if_free(ifp);
2528
2529	if (sc->bge_tbi) {
2530		ifmedia_removeall(&sc->bge_ifmedia);
2531	} else {
2532		bus_generic_detach(dev);
2533		device_delete_child(dev, sc->bge_miibus);
2534	}
2535
2536	bge_release_resources(sc);
2537	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2538	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2539		bge_free_jumbo_mem(sc);
2540
2541	return(0);
2542}
2543
2544static void
2545bge_release_resources(sc)
2546	struct bge_softc *sc;
2547{
2548	device_t dev;
2549
2550	dev = sc->bge_dev;
2551
2552	if (sc->bge_vpd_prodname != NULL)
2553		free(sc->bge_vpd_prodname, M_DEVBUF);
2554
2555	if (sc->bge_vpd_readonly != NULL)
2556		free(sc->bge_vpd_readonly, M_DEVBUF);
2557
2558	if (sc->bge_intrhand != NULL)
2559		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
2560
2561	if (sc->bge_irq != NULL)
2562		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
2563
2564	if (sc->bge_res != NULL)
2565		bus_release_resource(dev, SYS_RES_MEMORY,
2566		    BGE_PCI_BAR0, sc->bge_res);
2567
2568	bge_dma_free(sc);
2569
2570	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
2571		BGE_LOCK_DESTROY(sc);
2572
2573	return;
2574}
2575
2576static void
2577bge_reset(sc)
2578	struct bge_softc *sc;
2579{
2580	device_t dev;
2581	u_int32_t cachesize, command, pcistate, reset;
2582	int i, val = 0;
2583
2584	dev = sc->bge_dev;
2585
2586	/* Save some important PCI state. */
2587	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2588	command = pci_read_config(dev, BGE_PCI_CMD, 4);
2589	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2590
2591	pci_write_config(dev, BGE_PCI_MISC_CTL,
2592	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2593	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
2594
2595	reset = BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1);
2596
2597	/* XXX: Broadcom Linux driver. */
2598	if (sc->bge_pcie) {
2599		if (CSR_READ_4(sc, 0x7e2c) == 0x60)	/* PCIE 1.0 */
2600			CSR_WRITE_4(sc, 0x7e2c, 0x20);
2601		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2602			/* Prevent PCIE link training during global reset */
2603			CSR_WRITE_4(sc, BGE_MISC_CFG, (1<<29));
2604			reset |= (1<<29);
2605		}
2606	}
2607
2608	/* Issue global reset */
2609	bge_writereg_ind(sc, BGE_MISC_CFG, reset);
2610
2611	DELAY(1000);
2612
2613	/* XXX: Broadcom Linux driver. */
2614	if (sc->bge_pcie) {
2615		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2616			uint32_t v;
2617
2618			DELAY(500000); /* wait for link training to complete */
2619			v = pci_read_config(dev, 0xc4, 4);
2620			pci_write_config(dev, 0xc4, v | (1<<15), 4);
2621		}
2622		/* Set PCIE max payload size and clear error status. */
2623		pci_write_config(dev, 0xd8, 0xf5000, 4);
2624	}
2625
2626	/* Reset some of the PCI state that got zapped by reset */
2627	pci_write_config(dev, BGE_PCI_MISC_CTL,
2628	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2629	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
2630	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2631	pci_write_config(dev, BGE_PCI_CMD, command, 4);
2632	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
2633
2634	/* Enable memory arbiter. */
2635	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2636	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
2637		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2638
2639	/*
2640	 * Prevent PXE restart: write a magic number to the
2641	 * general communications memory at 0xB50.
2642	 */
2643	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2644	/*
2645	 * Poll the value location we just wrote until
2646	 * we see the 1's complement of the magic number.
2647	 * This indicates that the firmware initialization
2648	 * is complete.
2649	 */
2650	for (i = 0; i < BGE_TIMEOUT; i++) {
2651		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2652		if (val == ~BGE_MAGIC_NUMBER)
2653			break;
2654		DELAY(10);
2655	}
2656
2657	if (i == BGE_TIMEOUT) {
2658		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
2659		return;
2660	}
2661
2662	/*
2663	 * XXX Wait for the value of the PCISTATE register to
2664	 * return to its original pre-reset state. This is a
2665	 * fairly good indicator of reset completion. If we don't
2666	 * wait for the reset to fully complete, trying to read
2667	 * from the device's non-PCI registers may yield garbage
2668	 * results.
2669	 */
2670	for (i = 0; i < BGE_TIMEOUT; i++) {
2671		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2672			break;
2673		DELAY(10);
2674	}
2675
2676	/* Fix up byte swapping */
2677	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
2678	    BGE_MODECTL_BYTESWAP_DATA);
2679
2680	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2681
2682	/*
2683	 * The 5704 in TBI mode apparently needs some special
2684	 * adjustment to insure the SERDES drive level is set
2685	 * to 1.2V.
2686	 */
2687	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 && sc->bge_tbi) {
2688		uint32_t serdescfg;
2689		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
2690		serdescfg = (serdescfg & ~0xFFF) | 0x880;
2691		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
2692	}
2693
2694	/* XXX: Broadcom Linux driver. */
2695	if (sc->bge_pcie && sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2696		uint32_t v;
2697
2698		v = CSR_READ_4(sc, 0x7c00);
2699		CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
2700	}
2701	DELAY(10000);
2702
2703	return;
2704}
2705
2706/*
2707 * Frame reception handling. This is called if there's a frame
2708 * on the receive return list.
2709 *
2710 * Note: we have to be able to handle two possibilities here:
2711 * 1) the frame is from the jumbo recieve ring
2712 * 2) the frame is from the standard receive ring
2713 */
2714
2715static void
2716bge_rxeof(sc)
2717	struct bge_softc *sc;
2718{
2719	struct ifnet *ifp;
2720	int stdcnt = 0, jumbocnt = 0;
2721
2722	BGE_LOCK_ASSERT(sc);
2723
2724	ifp = sc->bge_ifp;
2725
2726	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2727	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTWRITE);
2728	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2729	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
2730	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2731	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2732		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2733		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2734		    BUS_DMASYNC_POSTREAD);
2735	}
2736
2737	while(sc->bge_rx_saved_considx !=
2738	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
2739		struct bge_rx_bd	*cur_rx;
2740		u_int32_t		rxidx;
2741		struct ether_header	*eh;
2742		struct mbuf		*m = NULL;
2743		u_int16_t		vlan_tag = 0;
2744		int			have_tag = 0;
2745
2746		cur_rx =
2747	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
2748
2749		rxidx = cur_rx->bge_idx;
2750		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
2751
2752		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2753			have_tag = 1;
2754			vlan_tag = cur_rx->bge_vlan_tag;
2755		}
2756
2757		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2758			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2759			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2760			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2761			    BUS_DMASYNC_POSTREAD);
2762			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2763			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
2764			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
2765			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
2766			jumbocnt++;
2767			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2768				ifp->if_ierrors++;
2769				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2770				continue;
2771			}
2772			if (bge_newbuf_jumbo(sc,
2773			    sc->bge_jumbo, NULL) == ENOBUFS) {
2774				ifp->if_ierrors++;
2775				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2776				continue;
2777			}
2778		} else {
2779			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2780			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2781			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2782			    BUS_DMASYNC_POSTREAD);
2783			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2784			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
2785			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
2786			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
2787			stdcnt++;
2788			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2789				ifp->if_ierrors++;
2790				bge_newbuf_std(sc, sc->bge_std, m);
2791				continue;
2792			}
2793			if (bge_newbuf_std(sc, sc->bge_std,
2794			    NULL) == ENOBUFS) {
2795				ifp->if_ierrors++;
2796				bge_newbuf_std(sc, sc->bge_std, m);
2797				continue;
2798			}
2799		}
2800
2801		ifp->if_ipackets++;
2802#ifndef __i386__
2803		/*
2804		 * The i386 allows unaligned accesses, but for other
2805		 * platforms we must make sure the payload is aligned.
2806		 */
2807		if (sc->bge_rx_alignment_bug) {
2808			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2809			    cur_rx->bge_len);
2810			m->m_data += ETHER_ALIGN;
2811		}
2812#endif
2813		eh = mtod(m, struct ether_header *);
2814		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2815		m->m_pkthdr.rcvif = ifp;
2816
2817#if 0 /* currently broken for some packets, possibly related to TCP options */
2818		if (ifp->if_capenable & IFCAP_RXCSUM) {
2819			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2820			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
2821				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2822			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2823				m->m_pkthdr.csum_data =
2824				    cur_rx->bge_tcp_udp_csum;
2825				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2826			}
2827		}
2828#endif
2829
2830		/*
2831		 * If we received a packet with a vlan tag,
2832		 * attach that information to the packet.
2833		 */
2834		if (have_tag)
2835			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
2836
2837		BGE_UNLOCK(sc);
2838		(*ifp->if_input)(ifp, m);
2839		BGE_LOCK(sc);
2840	}
2841
2842	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2843	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
2844	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2845	    sc->bge_cdata.bge_rx_std_ring_map,
2846	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_PREWRITE);
2847	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2848	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2849		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2850		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2851		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2852	}
2853
2854	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
2855	if (stdcnt)
2856		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
2857	if (jumbocnt)
2858		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
2859
2860	return;
2861}
2862
2863static void
2864bge_txeof(sc)
2865	struct bge_softc *sc;
2866{
2867	struct bge_tx_bd *cur_tx = NULL;
2868	struct ifnet *ifp;
2869
2870	BGE_LOCK_ASSERT(sc);
2871
2872	ifp = sc->bge_ifp;
2873
2874	/*
2875	 * Go through our tx ring and free mbufs for those
2876	 * frames that have been sent.
2877	 */
2878	while (sc->bge_tx_saved_considx !=
2879	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
2880		u_int32_t		idx = 0;
2881
2882		idx = sc->bge_tx_saved_considx;
2883		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
2884		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
2885			ifp->if_opackets++;
2886		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2887			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2888			sc->bge_cdata.bge_tx_chain[idx] = NULL;
2889			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2890			    sc->bge_cdata.bge_tx_dmamap[idx]);
2891		}
2892		sc->bge_txcnt--;
2893		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
2894		ifp->if_timer = 0;
2895	}
2896
2897	if (cur_tx != NULL)
2898		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2899
2900	return;
2901}
2902
2903static void
2904bge_intr(xsc)
2905	void *xsc;
2906{
2907	struct bge_softc *sc;
2908	struct ifnet *ifp;
2909	u_int32_t statusword;
2910	u_int32_t status, mimode;
2911
2912	sc = xsc;
2913	ifp = sc->bge_ifp;
2914
2915	BGE_LOCK(sc);
2916
2917	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2918	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTWRITE);
2919
2920	statusword =
2921	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2922
2923#ifdef notdef
2924	/* Avoid this for now -- checking this register is expensive. */
2925	/* Make sure this is really our interrupt. */
2926	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
2927		return;
2928#endif
2929	/* Ack interrupt and stop others from occuring. */
2930	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2931
2932	/*
2933	 * Process link state changes.
2934	 * Grrr. The link status word in the status block does
2935	 * not work correctly on the BCM5700 rev AX and BX chips,
2936	 * according to all available information. Hence, we have
2937	 * to enable MII interrupts in order to properly obtain
2938	 * async link changes. Unfortunately, this also means that
2939	 * we have to read the MAC status register to detect link
2940	 * changes, thereby adding an additional register access to
2941	 * the interrupt handler.
2942	 */
2943
2944	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2945
2946		status = CSR_READ_4(sc, BGE_MAC_STS);
2947		if (status & BGE_MACSTAT_MI_INTERRUPT) {
2948			sc->bge_link = 0;
2949			callout_stop(&sc->bge_stat_ch);
2950			bge_tick_locked(sc);
2951			/* Clear the interrupt */
2952			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2953			    BGE_EVTENB_MI_INTERRUPT);
2954			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2955			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2956			    BRGPHY_INTRS);
2957		}
2958	} else {
2959		if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED) {
2960			/*
2961			 * Sometimes PCS encoding errors are detected in
2962			 * TBI mode (on fiber NICs), and for some reason
2963			 * the chip will signal them as link changes.
2964			 * If we get a link change event, but the 'PCS
2965			 * encoding error' bit in the MAC status register
2966			 * is set, don't bother doing a link check.
2967			 * This avoids spurious "gigabit link up" messages
2968			 * that sometimes appear on fiber NICs during
2969			 * periods of heavy traffic. (There should be no
2970			 * effect on copper NICs.)
2971			 *
2972			 * If we do have a copper NIC (bge_tbi == 0) then
2973			 * check that the AUTOPOLL bit is set before
2974			 * processing the event as a real link change.
2975			 * Turning AUTOPOLL on and off in the MII read/write
2976			 * functions will often trigger a link status
2977			 * interrupt for no reason.
2978			 */
2979			status = CSR_READ_4(sc, BGE_MAC_STS);
2980			mimode = CSR_READ_4(sc, BGE_MI_MODE);
2981			if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR|
2982			    BGE_MACSTAT_MI_COMPLETE)) && (!sc->bge_tbi &&
2983			    (mimode & BGE_MIMODE_AUTOPOLL))) {
2984				sc->bge_link = 0;
2985				callout_stop(&sc->bge_stat_ch);
2986				bge_tick_locked(sc);
2987			}
2988			/* Clear the interrupt */
2989			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
2990			    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
2991			    BGE_MACSTAT_LINK_CHANGED);
2992
2993			/* Force flush the status block cached by PCI bridge */
2994			CSR_READ_4(sc, BGE_MBX_IRQ0_LO);
2995		}
2996	}
2997
2998	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2999		/* Check RX return ring producer/consumer */
3000		bge_rxeof(sc);
3001
3002		/* Check TX ring producer/consumer */
3003		bge_txeof(sc);
3004	}
3005
3006	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3007	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
3008
3009	bge_handle_events(sc);
3010
3011	/* Re-enable interrupts. */
3012	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3013
3014	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3015	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3016		bge_start_locked(ifp);
3017
3018	BGE_UNLOCK(sc);
3019
3020	return;
3021}
3022
3023static void
3024bge_tick_locked(sc)
3025	struct bge_softc *sc;
3026{
3027	struct mii_data *mii = NULL;
3028	struct ifmedia *ifm = NULL;
3029	struct ifnet *ifp;
3030
3031	ifp = sc->bge_ifp;
3032
3033	BGE_LOCK_ASSERT(sc);
3034
3035	if (sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3036	    sc->bge_asicrev == BGE_ASICREV_BCM5750)
3037		bge_stats_update_regs(sc);
3038	else
3039		bge_stats_update(sc);
3040	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
3041	if (sc->bge_link)
3042		return;
3043
3044	if (sc->bge_tbi) {
3045		ifm = &sc->bge_ifmedia;
3046		if (CSR_READ_4(sc, BGE_MAC_STS) &
3047		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
3048			sc->bge_link++;
3049			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
3050				BGE_CLRBIT(sc, BGE_MAC_MODE,
3051				    BGE_MACMODE_TBI_SEND_CFGS);
3052			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
3053			if (bootverbose)
3054				printf("bge%d: gigabit link up\n",
3055				    sc->bge_unit);
3056			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3057				bge_start_locked(ifp);
3058		}
3059		return;
3060	}
3061
3062	mii = device_get_softc(sc->bge_miibus);
3063	mii_tick(mii);
3064
3065	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
3066	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
3067		sc->bge_link++;
3068		if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
3069		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) &&
3070		    bootverbose)
3071			printf("bge%d: gigabit link up\n", sc->bge_unit);
3072		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3073			bge_start_locked(ifp);
3074	}
3075
3076	return;
3077}
3078
3079static void
3080bge_tick(xsc)
3081	void *xsc;
3082{
3083	struct bge_softc *sc;
3084
3085	sc = xsc;
3086
3087	BGE_LOCK(sc);
3088	bge_tick_locked(sc);
3089	BGE_UNLOCK(sc);
3090}
3091
3092static void
3093bge_stats_update_regs(sc)
3094	struct bge_softc *sc;
3095{
3096	struct ifnet *ifp;
3097	struct bge_mac_stats_regs stats;
3098	u_int32_t *s;
3099	int i;
3100
3101	ifp = sc->bge_ifp;
3102
3103	s = (u_int32_t *)&stats;
3104	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
3105		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
3106		s++;
3107	}
3108
3109	ifp->if_collisions +=
3110	   (stats.dot3StatsSingleCollisionFrames +
3111	   stats.dot3StatsMultipleCollisionFrames +
3112	   stats.dot3StatsExcessiveCollisions +
3113	   stats.dot3StatsLateCollisions) -
3114	   ifp->if_collisions;
3115
3116	return;
3117}
3118
3119static void
3120bge_stats_update(sc)
3121	struct bge_softc *sc;
3122{
3123	struct ifnet *ifp;
3124	struct bge_stats *stats;
3125
3126	ifp = sc->bge_ifp;
3127
3128	stats = (struct bge_stats *)(sc->bge_vhandle +
3129	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
3130
3131	ifp->if_collisions +=
3132	   (stats->txstats.dot3StatsSingleCollisionFrames.bge_addr_lo +
3133	   stats->txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo +
3134	   stats->txstats.dot3StatsExcessiveCollisions.bge_addr_lo +
3135	   stats->txstats.dot3StatsLateCollisions.bge_addr_lo) -
3136	   ifp->if_collisions;
3137
3138#ifdef notdef
3139	ifp->if_collisions +=
3140	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
3141	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
3142	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
3143	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
3144	   ifp->if_collisions;
3145#endif
3146
3147	return;
3148}
3149
3150/*
3151 * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
3152 * pointers to descriptors.
3153 */
3154static int
3155bge_encap(sc, m_head, txidx)
3156	struct bge_softc *sc;
3157	struct mbuf *m_head;
3158	u_int32_t *txidx;
3159{
3160	struct bge_tx_bd	*f = NULL;
3161	u_int16_t		csum_flags = 0;
3162	struct m_tag		*mtag;
3163	struct bge_dmamap_arg	ctx;
3164	bus_dmamap_t		map;
3165	int			error;
3166
3167
3168	if (m_head->m_pkthdr.csum_flags) {
3169		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
3170			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3171		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
3172			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3173		if (m_head->m_flags & M_LASTFRAG)
3174			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
3175		else if (m_head->m_flags & M_FRAG)
3176			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
3177	}
3178
3179	mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m_head);
3180
3181	ctx.sc = sc;
3182	ctx.bge_idx = *txidx;
3183	ctx.bge_ring = sc->bge_ldata.bge_tx_ring;
3184	ctx.bge_flags = csum_flags;
3185	/*
3186	 * Sanity check: avoid coming within 16 descriptors
3187	 * of the end of the ring.
3188	 */
3189	ctx.bge_maxsegs = (BGE_TX_RING_CNT - sc->bge_txcnt) - 16;
3190
3191	map = sc->bge_cdata.bge_tx_dmamap[*txidx];
3192	error = bus_dmamap_load_mbuf(sc->bge_cdata.bge_mtag, map,
3193	    m_head, bge_dma_map_tx_desc, &ctx, BUS_DMA_NOWAIT);
3194
3195	if (error || ctx.bge_maxsegs == 0 /*||
3196	    ctx.bge_idx == sc->bge_tx_saved_considx*/)
3197		return (ENOBUFS);
3198
3199	/*
3200	 * Insure that the map for this transmission
3201	 * is placed at the array index of the last descriptor
3202	 * in this chain.
3203	 */
3204	sc->bge_cdata.bge_tx_dmamap[*txidx] =
3205	    sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx];
3206	sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx] = map;
3207	sc->bge_cdata.bge_tx_chain[ctx.bge_idx] = m_head;
3208	sc->bge_txcnt += ctx.bge_maxsegs;
3209	f = &sc->bge_ldata.bge_tx_ring[*txidx];
3210	if (mtag != NULL) {
3211		f->bge_flags |= htole16(BGE_TXBDFLAG_VLAN_TAG);
3212		f->bge_vlan_tag = htole16(VLAN_TAG_VALUE(mtag));
3213	} else {
3214		f->bge_vlan_tag = 0;
3215	}
3216
3217	BGE_INC(ctx.bge_idx, BGE_TX_RING_CNT);
3218	*txidx = ctx.bge_idx;
3219
3220	return(0);
3221}
3222
3223/*
3224 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3225 * to the mbuf data regions directly in the transmit descriptors.
3226 */
3227static void
3228bge_start_locked(ifp)
3229	struct ifnet *ifp;
3230{
3231	struct bge_softc *sc;
3232	struct mbuf *m_head = NULL;
3233	u_int32_t prodidx = 0;
3234	int count = 0;
3235
3236	sc = ifp->if_softc;
3237
3238	if (!sc->bge_link && IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3239		return;
3240
3241	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
3242
3243	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
3244		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
3245		if (m_head == NULL)
3246			break;
3247
3248		/*
3249		 * XXX
3250		 * The code inside the if() block is never reached since we
3251		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3252		 * requests to checksum TCP/UDP in a fragmented packet.
3253		 *
3254		 * XXX
3255		 * safety overkill.  If this is a fragmented packet chain
3256		 * with delayed TCP/UDP checksums, then only encapsulate
3257		 * it if we have enough descriptors to handle the entire
3258		 * chain at once.
3259		 * (paranoia -- may not actually be needed)
3260		 */
3261		if (m_head->m_flags & M_FIRSTFRAG &&
3262		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
3263			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
3264			    m_head->m_pkthdr.csum_data + 16) {
3265				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3266				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3267				break;
3268			}
3269		}
3270
3271		/*
3272		 * Pack the data into the transmit ring. If we
3273		 * don't have room, set the OACTIVE flag and wait
3274		 * for the NIC to drain the ring.
3275		 */
3276		if (bge_encap(sc, m_head, &prodidx)) {
3277			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3278			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3279			break;
3280		}
3281		++count;
3282
3283		/*
3284		 * If there's a BPF listener, bounce a copy of this frame
3285		 * to him.
3286		 */
3287		BPF_MTAP(ifp, m_head);
3288	}
3289
3290	if (count == 0) {
3291		/* no packets were dequeued */
3292		return;
3293	}
3294
3295	/* Transmit */
3296	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3297	/* 5700 b2 errata */
3298	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
3299		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3300
3301	/*
3302	 * Set a timeout in case the chip goes out to lunch.
3303	 */
3304	ifp->if_timer = 5;
3305
3306	return;
3307}
3308
3309/*
3310 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3311 * to the mbuf data regions directly in the transmit descriptors.
3312 */
3313static void
3314bge_start(ifp)
3315	struct ifnet *ifp;
3316{
3317	struct bge_softc *sc;
3318
3319	sc = ifp->if_softc;
3320	BGE_LOCK(sc);
3321	bge_start_locked(ifp);
3322	BGE_UNLOCK(sc);
3323}
3324
3325static void
3326bge_init_locked(sc)
3327	struct bge_softc *sc;
3328{
3329	struct ifnet *ifp;
3330	u_int16_t *m;
3331
3332	BGE_LOCK_ASSERT(sc);
3333
3334	ifp = sc->bge_ifp;
3335
3336	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3337		return;
3338
3339	/* Cancel pending I/O and flush buffers. */
3340	bge_stop(sc);
3341	bge_reset(sc);
3342	bge_chipinit(sc);
3343
3344	/*
3345	 * Init the various state machines, ring
3346	 * control blocks and firmware.
3347	 */
3348	if (bge_blockinit(sc)) {
3349		printf("bge%d: initialization failure\n", sc->bge_unit);
3350		return;
3351	}
3352
3353	ifp = sc->bge_ifp;
3354
3355	/* Specify MTU. */
3356	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3357	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
3358
3359	/* Load our MAC address. */
3360	m = (u_int16_t *)&IFP2ENADDR(sc->bge_ifp)[0];
3361	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3362	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3363
3364	/* Enable or disable promiscuous mode as needed. */
3365	if (ifp->if_flags & IFF_PROMISC) {
3366		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3367	} else {
3368		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3369	}
3370
3371	/* Program multicast filter. */
3372	bge_setmulti(sc);
3373
3374	/* Init RX ring. */
3375	bge_init_rx_ring_std(sc);
3376
3377	/*
3378	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
3379	 * memory to insure that the chip has in fact read the first
3380	 * entry of the ring.
3381	 */
3382	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
3383		u_int32_t		v, i;
3384		for (i = 0; i < 10; i++) {
3385			DELAY(20);
3386			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
3387			if (v == (MCLBYTES - ETHER_ALIGN))
3388				break;
3389		}
3390		if (i == 10)
3391			printf ("bge%d: 5705 A0 chip failed to load RX ring\n",
3392			    sc->bge_unit);
3393	}
3394
3395	/* Init jumbo RX ring. */
3396	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
3397		bge_init_rx_ring_jumbo(sc);
3398
3399	/* Init our RX return ring index */
3400	sc->bge_rx_saved_considx = 0;
3401
3402	/* Init TX ring. */
3403	bge_init_tx_ring(sc);
3404
3405	/* Turn on transmitter */
3406	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
3407
3408	/* Turn on receiver */
3409	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3410
3411	/* Tell firmware we're alive. */
3412	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3413
3414	/* Enable host interrupts. */
3415	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
3416	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3417	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3418
3419	bge_ifmedia_upd(ifp);
3420
3421	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3422	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3423
3424	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
3425
3426	return;
3427}
3428
3429static void
3430bge_init(xsc)
3431	void *xsc;
3432{
3433	struct bge_softc *sc = xsc;
3434
3435	BGE_LOCK(sc);
3436	bge_init_locked(sc);
3437	BGE_UNLOCK(sc);
3438
3439	return;
3440}
3441
3442/*
3443 * Set media options.
3444 */
3445static int
3446bge_ifmedia_upd(ifp)
3447	struct ifnet *ifp;
3448{
3449	struct bge_softc *sc;
3450	struct mii_data *mii;
3451	struct ifmedia *ifm;
3452
3453	sc = ifp->if_softc;
3454	ifm = &sc->bge_ifmedia;
3455
3456	/* If this is a 1000baseX NIC, enable the TBI port. */
3457	if (sc->bge_tbi) {
3458		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3459			return(EINVAL);
3460		switch(IFM_SUBTYPE(ifm->ifm_media)) {
3461		case IFM_AUTO:
3462#ifndef BGE_FAKE_AUTONEG
3463			/*
3464			 * The BCM5704 ASIC appears to have a special
3465			 * mechanism for programming the autoneg
3466			 * advertisement registers in TBI mode.
3467			 */
3468			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3469				uint32_t sgdig;
3470				CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3471				sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3472				sgdig |= BGE_SGDIGCFG_AUTO|
3473				    BGE_SGDIGCFG_PAUSE_CAP|
3474				    BGE_SGDIGCFG_ASYM_PAUSE;
3475				CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3476				    sgdig|BGE_SGDIGCFG_SEND);
3477				DELAY(5);
3478				CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3479			}
3480#endif
3481			break;
3482		case IFM_1000_SX:
3483			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3484				BGE_CLRBIT(sc, BGE_MAC_MODE,
3485				    BGE_MACMODE_HALF_DUPLEX);
3486			} else {
3487				BGE_SETBIT(sc, BGE_MAC_MODE,
3488				    BGE_MACMODE_HALF_DUPLEX);
3489			}
3490			break;
3491		default:
3492			return(EINVAL);
3493		}
3494		return(0);
3495	}
3496
3497	mii = device_get_softc(sc->bge_miibus);
3498	sc->bge_link = 0;
3499	if (mii->mii_instance) {
3500		struct mii_softc *miisc;
3501		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
3502		    miisc = LIST_NEXT(miisc, mii_list))
3503			mii_phy_reset(miisc);
3504	}
3505	mii_mediachg(mii);
3506
3507	return(0);
3508}
3509
3510/*
3511 * Report current media status.
3512 */
3513static void
3514bge_ifmedia_sts(ifp, ifmr)
3515	struct ifnet *ifp;
3516	struct ifmediareq *ifmr;
3517{
3518	struct bge_softc *sc;
3519	struct mii_data *mii;
3520
3521	sc = ifp->if_softc;
3522
3523	if (sc->bge_tbi) {
3524		ifmr->ifm_status = IFM_AVALID;
3525		ifmr->ifm_active = IFM_ETHER;
3526		if (CSR_READ_4(sc, BGE_MAC_STS) &
3527		    BGE_MACSTAT_TBI_PCS_SYNCHED)
3528			ifmr->ifm_status |= IFM_ACTIVE;
3529		ifmr->ifm_active |= IFM_1000_SX;
3530		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3531			ifmr->ifm_active |= IFM_HDX;
3532		else
3533			ifmr->ifm_active |= IFM_FDX;
3534		return;
3535	}
3536
3537	mii = device_get_softc(sc->bge_miibus);
3538	mii_pollstat(mii);
3539	ifmr->ifm_active = mii->mii_media_active;
3540	ifmr->ifm_status = mii->mii_media_status;
3541
3542	return;
3543}
3544
3545static int
3546bge_ioctl(ifp, command, data)
3547	struct ifnet *ifp;
3548	u_long command;
3549	caddr_t data;
3550{
3551	struct bge_softc *sc = ifp->if_softc;
3552	struct ifreq *ifr = (struct ifreq *) data;
3553	int mask, error = 0;
3554	struct mii_data *mii;
3555
3556	switch(command) {
3557	case SIOCSIFMTU:
3558		/* Disallow jumbo frames on 5705. */
3559		if (((sc->bge_asicrev == BGE_ASICREV_BCM5705 ||
3560		      sc->bge_asicrev == BGE_ASICREV_BCM5750) &&
3561		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
3562			error = EINVAL;
3563		else {
3564			ifp->if_mtu = ifr->ifr_mtu;
3565			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3566			bge_init(sc);
3567		}
3568		break;
3569	case SIOCSIFFLAGS:
3570		BGE_LOCK(sc);
3571		if (ifp->if_flags & IFF_UP) {
3572			/*
3573			 * If only the state of the PROMISC flag changed,
3574			 * then just use the 'set promisc mode' command
3575			 * instead of reinitializing the entire NIC. Doing
3576			 * a full re-init means reloading the firmware and
3577			 * waiting for it to start up, which may take a
3578			 * second or two.
3579			 */
3580			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3581			    ifp->if_flags & IFF_PROMISC &&
3582			    !(sc->bge_if_flags & IFF_PROMISC)) {
3583				BGE_SETBIT(sc, BGE_RX_MODE,
3584				    BGE_RXMODE_RX_PROMISC);
3585			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3586			    !(ifp->if_flags & IFF_PROMISC) &&
3587			    sc->bge_if_flags & IFF_PROMISC) {
3588				BGE_CLRBIT(sc, BGE_RX_MODE,
3589				    BGE_RXMODE_RX_PROMISC);
3590			} else
3591				bge_init_locked(sc);
3592		} else {
3593			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3594				bge_stop(sc);
3595			}
3596		}
3597		sc->bge_if_flags = ifp->if_flags;
3598		BGE_UNLOCK(sc);
3599		error = 0;
3600		break;
3601	case SIOCADDMULTI:
3602	case SIOCDELMULTI:
3603		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3604			BGE_LOCK(sc);
3605			bge_setmulti(sc);
3606			BGE_UNLOCK(sc);
3607			error = 0;
3608		}
3609		break;
3610	case SIOCSIFMEDIA:
3611	case SIOCGIFMEDIA:
3612		if (sc->bge_tbi) {
3613			error = ifmedia_ioctl(ifp, ifr,
3614			    &sc->bge_ifmedia, command);
3615		} else {
3616			mii = device_get_softc(sc->bge_miibus);
3617			error = ifmedia_ioctl(ifp, ifr,
3618			    &mii->mii_media, command);
3619		}
3620		break;
3621	case SIOCSIFCAP:
3622		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3623		/* NB: the code for RX csum offload is disabled for now */
3624		if (mask & IFCAP_TXCSUM) {
3625			ifp->if_capenable ^= IFCAP_TXCSUM;
3626			if (IFCAP_TXCSUM & ifp->if_capenable)
3627				ifp->if_hwassist = BGE_CSUM_FEATURES;
3628			else
3629				ifp->if_hwassist = 0;
3630		}
3631		error = 0;
3632		break;
3633	default:
3634		error = ether_ioctl(ifp, command, data);
3635		break;
3636	}
3637
3638	return(error);
3639}
3640
3641static void
3642bge_watchdog(ifp)
3643	struct ifnet *ifp;
3644{
3645	struct bge_softc *sc;
3646
3647	sc = ifp->if_softc;
3648
3649	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
3650
3651	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3652	bge_init(sc);
3653
3654	ifp->if_oerrors++;
3655
3656	return;
3657}
3658
3659/*
3660 * Stop the adapter and free any mbufs allocated to the
3661 * RX and TX lists.
3662 */
3663static void
3664bge_stop(sc)
3665	struct bge_softc *sc;
3666{
3667	struct ifnet *ifp;
3668	struct ifmedia_entry *ifm;
3669	struct mii_data *mii = NULL;
3670	int mtmp, itmp;
3671
3672	BGE_LOCK_ASSERT(sc);
3673
3674	ifp = sc->bge_ifp;
3675
3676	if (!sc->bge_tbi)
3677		mii = device_get_softc(sc->bge_miibus);
3678
3679	callout_stop(&sc->bge_stat_ch);
3680
3681	/*
3682	 * Disable all of the receiver blocks
3683	 */
3684	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3685	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3686	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3687	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3688	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
3689		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
3690	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3691	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3692	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3693
3694	/*
3695	 * Disable all of the transmit blocks
3696	 */
3697	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3698	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3699	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3700	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3701	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3702	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3703	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
3704		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
3705	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3706
3707	/*
3708	 * Shut down all of the memory managers and related
3709	 * state machines.
3710	 */
3711	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3712	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3713	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3714	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
3715		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
3716	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3717	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3718	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3719	    sc->bge_asicrev != BGE_ASICREV_BCM5750) {
3720		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
3721		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3722	}
3723
3724	/* Disable host interrupts. */
3725	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3726	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
3727
3728	/*
3729	 * Tell firmware we're shutting down.
3730	 */
3731	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3732
3733	/* Free the RX lists. */
3734	bge_free_rx_ring_std(sc);
3735
3736	/* Free jumbo RX list. */
3737	if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
3738	    sc->bge_asicrev != BGE_ASICREV_BCM5750)
3739		bge_free_rx_ring_jumbo(sc);
3740
3741	/* Free TX buffers. */
3742	bge_free_tx_ring(sc);
3743
3744	/*
3745	 * Isolate/power down the PHY, but leave the media selection
3746	 * unchanged so that things will be put back to normal when
3747	 * we bring the interface back up.
3748	 */
3749	if (!sc->bge_tbi) {
3750		itmp = ifp->if_flags;
3751		ifp->if_flags |= IFF_UP;
3752		/*
3753		 * If we are called from bge_detach(), mii is already NULL.
3754		 */
3755		if (mii != NULL) {
3756			ifm = mii->mii_media.ifm_cur;
3757			mtmp = ifm->ifm_media;
3758			ifm->ifm_media = IFM_ETHER|IFM_NONE;
3759			mii_mediachg(mii);
3760			ifm->ifm_media = mtmp;
3761		}
3762		ifp->if_flags = itmp;
3763	}
3764
3765	sc->bge_link = 0;
3766
3767	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
3768
3769	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3770
3771	return;
3772}
3773
3774/*
3775 * Stop all chip I/O so that the kernel's probe routines don't
3776 * get confused by errant DMAs when rebooting.
3777 */
3778static void
3779bge_shutdown(dev)
3780	device_t dev;
3781{
3782	struct bge_softc *sc;
3783
3784	sc = device_get_softc(dev);
3785
3786	BGE_LOCK(sc);
3787	bge_stop(sc);
3788	bge_reset(sc);
3789	BGE_UNLOCK(sc);
3790
3791	return;
3792}
3793