xref: /titanic_41/usr/src/uts/common/io/bge/bge_chip2.c (revision a7df97bafe5a886e6ddb3fe8170b2d547cd3b029)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "bge_impl.h"
28 
29 #define	PIO_ADDR(bgep, offset)	((void *)((caddr_t)(bgep)->io_regs+(offset)))
30 
31 /*
32  * Future features ... ?
33  */
34 #define	BGE_CFG_IO8	1	/* 8/16-bit cfg space BIS/BIC	*/
35 #define	BGE_IND_IO32	1	/* indirect access code		*/
36 #define	BGE_SEE_IO32	1	/* SEEPROM access code		*/
37 #define	BGE_FLASH_IO32	1	/* FLASH access code		*/
38 
39 /*
40  * BGE MSI tunable:
41  *
42  * By default MSI is enabled on all supported platforms but it is disabled
43  * for some Broadcom chips due to known MSI hardware issues. Currently MSI
44  * is enabled only for 5714C A2 and 5715C A2 broadcom chips.
45  */
46 boolean_t bge_enable_msi = B_TRUE;
47 
48 /*
49  * PCI-X/PCI-E relaxed ordering tunable for OS/Nexus driver
50  */
51 boolean_t bge_relaxed_ordering = B_TRUE;
52 
53 /*
54  * Property names
55  */
56 static char knownids_propname[] = "bge-known-subsystems";
57 
58 /*
59  * Patchable globals:
60  *
61  *	bge_autorecover
62  *		Enables/disables automatic recovery after fault detection
63  *
64  *	bge_mlcr_default
65  *		Value to program into the MLCR; controls the chip's GPIO pins
66  *
67  *	bge_dma_{rd,wr}prio
68  *		Relative priorities of DMA reads & DMA writes respectively.
69  *		These may each be patched to any value 0-3.  Equal values
70  *		will give "fair" (round-robin) arbitration for PCI access.
71  *		Unequal values will give one or the other function priority.
72  *
73  *	bge_dma_rwctrl
74  *		Value to put in the Read/Write DMA control register.  See
75  *	        the Broadcom PRM for things you can fiddle with in this
76  *		register ...
77  *
78  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
79  *		Send/receive interrupt coalescing parameters.  Counts are
80  *		#s of descriptors, ticks are in microseconds.  *norm* values
81  *		apply between status updates/interrupts; the *intr* values
82  *		refer to the 'during-interrupt' versions - see the PRM.
83  *
84  *		NOTE: these values have been determined by measurement. They
85  *		differ significantly from the values recommended in the PRM.
86  */
87 static uint32_t bge_autorecover = 1;
88 static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
89 
90 static uint32_t bge_dma_rdprio = 1;
91 static uint32_t bge_dma_wrprio = 0;
92 static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
93 static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
94 static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
95 static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
96 
97 uint32_t bge_rx_ticks_norm = 128;
98 uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
99 uint32_t bge_rx_count_norm = 8;
100 uint32_t bge_tx_count_norm = 128;
101 
102 static uint32_t bge_rx_ticks_intr = 128;
103 static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
104 static uint32_t bge_rx_count_intr = 2;
105 static uint32_t bge_tx_count_intr = 0;
106 
107 /*
108  * Memory pool configuration parameters.
109  *
110  * These are generally specific to each member of the chip family, since
111  * each one may have a different memory size/configuration.
112  *
113  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
114  * the driver from programming the various registers; instead they are left
115  * at their hardware defaults.  This is the preferred option for later chips
116  * (5705+), whereas the older chips *required* these registers to be set,
117  * since the h/w default was 0 ;-(
118  */
119 static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
120 static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
121 static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
122 static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
123 static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
124 static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
125 static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
126 static uint32_t bge_mbuf_pool_len_5721	= 0;
127 
128 /*
129  * Various high and low water marks, thresholds, etc ...
130  *
131  * Note: these are taken from revision 7 of the PRM, and some are different
132  * from both the values in earlier PRMs *and* those determined experimentally
133  * and used in earlier versions of this driver ...
134  */
135 static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
136 static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
137 static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
138 
139 static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
140 static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
141 static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
142 
143 static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
144 static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
145 static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
146 
147 static uint32_t	bge_watchdog_count	= 1 << 16;
148 static uint16_t bge_dma_miss_limit	= 20;
149 
150 static uint32_t bge_stop_start_on_sync	= 0;
151 
152 /*
153  * bge_intr_max_loop controls the maximum loop number within bge_intr.
154  * When loading NIC with heavy network traffic, it is useful.
155  * Increasing this value could have positive effect to throughput,
156  * but it might also increase ticks of a bge ISR stick on CPU, which might
157  * lead to bad UI interactive experience. So tune this with caution.
158  */
159 static int bge_intr_max_loop = 1;
160 
161 /*
162  * ========== Low-level chip & ring buffer manipulation ==========
163  */
164 
165 #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
166 
167 
168 /*
169  * Config space read-modify-write routines
170  */
171 
172 #if	BGE_CFG_IO8
173 
174 static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
175 #pragma	inline(bge_cfg_clr16)
176 
177 static void
178 bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
179 {
180 	uint16_t regval;
181 
182 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
183 	    (void *)bgep, regno, bits));
184 
185 	regval = pci_config_get16(bgep->cfg_handle, regno);
186 
187 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
188 	    (void *)bgep, regno, bits, regval, regval & ~bits));
189 
190 	regval &= ~bits;
191 	pci_config_put16(bgep->cfg_handle, regno, regval);
192 }
193 
194 #endif	/* BGE_CFG_IO8 */
195 
196 static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
197 #pragma	inline(bge_cfg_clr32)
198 
199 static void
200 bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
201 {
202 	uint32_t regval;
203 
204 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
205 	    (void *)bgep, regno, bits));
206 
207 	regval = pci_config_get32(bgep->cfg_handle, regno);
208 
209 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
211 
212 	regval &= ~bits;
213 	pci_config_put32(bgep->cfg_handle, regno, regval);
214 }
215 
216 #if	BGE_IND_IO32
217 
218 /*
219  * Indirect access to registers & RISC scratchpads, using config space
220  * accesses only.
221  *
222  * This isn't currently used, but someday we might want to use it for
223  * restoring the Subsystem Device/Vendor registers (which aren't directly
224  * writable in Config Space), or for downloading firmware into the RISCs
225  *
226  * In any case there are endian issues to be resolved before this code is
227  * enabled; the bizarre way that bytes get twisted by this chip AND by
228  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
229  * it's been thoroughly tested for all access sizes on all supported
230  * architectures (SPARC *and* x86!).
231  */
232 uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
233 #pragma	inline(bge_ind_get32)
234 
235 uint32_t
236 bge_ind_get32(bge_t *bgep, bge_regno_t regno)
237 {
238 	uint32_t val;
239 
240 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
241 
242 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
243 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
244 
245 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
246 	    (void *)bgep, regno, val));
247 
248 	val = LE_32(val);
249 
250 	return (val);
251 }
252 
253 void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
254 #pragma	inline(bge_ind_put32)
255 
256 void
257 bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
258 {
259 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
260 	    (void *)bgep, regno, val));
261 
262 	val = LE_32(val);
263 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
264 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
265 }
266 
267 #endif	/* BGE_IND_IO32 */
268 
269 #if	BGE_DEBUGGING
270 
271 static void bge_pci_check(bge_t *bgep);
272 #pragma	no_inline(bge_pci_check)
273 
274 static void
275 bge_pci_check(bge_t *bgep)
276 {
277 	uint16_t pcistatus;
278 
279 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
280 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
281 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
282 		    (void *)bgep, pcistatus));
283 }
284 
285 #endif	/* BGE_DEBUGGING */
286 
287 /*
288  * Perform first-stage chip (re-)initialisation, using only config-space
289  * accesses:
290  *
291  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
292  *   returning the data in the structure pointed to by <idp>.
293  * + Configure the target-mode endianness (swap) options.
294  * + Disable interrupts and enable Memory Space accesses.
295  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
296  *
297  * This sequence is adapted from Broadcom document 570X-PG102-R,
298  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
299  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
300  *
301  * This function MUST be called before any non-config-space accesses
302  * are made; on this first call <enable_dma> is B_FALSE, and it
303  * effectively performs steps 3-1(!) of the initialisation sequence
304  * (the rest are not required but should be harmless).
305  *
306  * It MUST also be called after a chip reset, as this disables
307  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
308  * it is effectively performing steps 6-8.
309  */
310 void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
311 #pragma	no_inline(bge_chip_cfg_init)
312 
313 void
314 bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
315 {
316 	ddi_acc_handle_t handle;
317 	uint16_t command;
318 	uint32_t mhcr;
319 	uint16_t value16;
320 	int i;
321 
322 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
323 	    (void *)bgep, (void *)cidp, enable_dma));
324 
325 	/*
326 	 * Step 3: save PCI cache line size and subsystem vendor ID
327 	 *
328 	 * Read all the config-space registers that characterise the
329 	 * chip, specifically vendor/device/revision/subsystem vendor
330 	 * and subsystem device id.  We expect (but don't check) that
331 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
332 	 *
333 	 * Also save all bus-transaction related registers (cache-line
334 	 * size, bus-grant/latency parameters, etc).  Some of these are
335 	 * cleared by reset, so we'll have to restore them later.  This
336 	 * comes from the Broadcom document 570X-PG102-R ...
337 	 *
338 	 * Note: Broadcom document 570X-PG102-R seems to be in error
339 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
340 	 * Subsystem (Device) ID registers, which are the opposite way
341 	 * round according to the PCI standard.  For good measure, we
342 	 * save/restore both anyway.
343 	 */
344 	handle = bgep->cfg_handle;
345 
346 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
347 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
348 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
349 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
350 
351 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
352 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
353 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
354 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
355 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
356 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
357 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
358 
359 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
360 	    cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
361 	    cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
362 	    cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
363 	    cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
364 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
365 	    cidp->vendor, cidp->device, cidp->revision));
366 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
367 	    cidp->subven, cidp->subdev, cidp->asic_rev));
368 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
369 	    cidp->clsize, cidp->latency, cidp->command));
370 
371 	/*
372 	 * Step 2 (also step 6): disable and clear interrupts.
373 	 * Steps 11-13: configure PIO endianness options, and enable
374 	 * indirect register access.  We'll also select any other
375 	 * options controlled by the MHCR (e.g. tagged status, mask
376 	 * interrupt mode) at this stage ...
377 	 *
378 	 * Note: internally, the chip is 64-bit and BIG-endian, but
379 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
380 	 * it normally swaps bytes around at the PCI interface.
381 	 * However, the PCI host bridge on SPARC systems normally
382 	 * swaps the byte lanes around too, since SPARCs are also
383 	 * BIG-endian.  So it turns out that on SPARC, the right
384 	 * option is to tell the chip to swap (and the host bridge
385 	 * will swap back again), whereas on x86 we ask the chip
386 	 * NOT to swap, so the natural little-endianness of the
387 	 * PCI bus is assumed.  Then the only thing that doesn't
388 	 * automatically work right is access to an 8-byte register
389 	 * by a little-endian host; but we don't want to set the
390 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
391 	 * accesses don't go where expected ;-(  So we live with
392 	 * that, and perform word-swaps in software in the few cases
393 	 * where a chip register is defined as an 8-byte value --
394 	 * see the code below for details ...
395 	 *
396 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
397 	 * very clear in the register description in the PRM, but
398 	 * Broadcom document 570X-PG104-R page 248 explains a little
399 	 * more (under "Broadcom Mask Mode").  The bit changes the way
400 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
401 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
402 	 * way as the 5700 did, which isn't very convenient.  Setting
403 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
404 	 * bit do just what its name says -- MASK the PCI #INTA output
405 	 * (i.e. deassert the signal at the pin) leaving all internal
406 	 * state unchanged.  This is much more convenient for our
407 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
408 	 *
409 	 * Note: the inconvenient semantics of the interrupt mailbox
410 	 * (nonzero disables and acknowledges/clears the interrupt,
411 	 * zero enables AND CLEARS it) would make race conditions
412 	 * likely in the interrupt handler:
413 	 *
414 	 * (1)	acknowledge & disable interrupts
415 	 * (2)	while (more to do)
416 	 * 		process packets
417 	 * (3)	enable interrupts -- also clears pending
418 	 *
419 	 * If the chip received more packets and internally generated
420 	 * an interrupt between the check at (2) and the mbox write
421 	 * at (3), this interrupt would be lost :-(
422 	 *
423 	 * The best way to avoid this is to use TAGGED STATUS mode,
424 	 * where the chip includes a unique tag in each status block
425 	 * update, and the host, when re-enabling interrupts, passes
426 	 * the last tag it saw back to the chip; then the chip can
427 	 * see whether the host is truly up to date, and regenerate
428 	 * its interrupt if not.
429 	 */
430 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
431 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
432 	    MHCR_MASK_INTERRUPT_MODE |
433 	    MHCR_CLEAR_INTERRUPT_INTA;
434 
435 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
436 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
437 
438 #ifdef	_BIG_ENDIAN
439 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
440 #endif	/* _BIG_ENDIAN */
441 
442 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
443 
444 #ifdef BGE_IPMI_ASF
445 	bgep->asf_wordswapped = B_FALSE;
446 #endif
447 	/*
448 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
449 	 *			 Disable Memory Write/Invalidate
450 	 *			 Enable or disable Bus Mastering
451 	 *
452 	 * Note that all other bits are taken from the original value saved
453 	 * the first time through here, rather than from the current register
454 	 * value, 'cos that will have been cleared by a soft RESET since.
455 	 * In this way we preserve the OBP/nexus-parent's preferred settings
456 	 * of the parity-error and system-error enable bits across multiple
457 	 * chip RESETs.
458 	 */
459 	command = bgep->chipid.command | PCI_COMM_MAE;
460 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
461 	if (enable_dma)
462 		command |= PCI_COMM_ME;
463 	/*
464 	 * on BCM5714 revision A0, false parity error gets generated
465 	 * due to a logic bug. Provide a workaround by disabling parity
466 	 * error.
467 	 */
468 	if (((cidp->device == DEVICE_ID_5714C) ||
469 	    (cidp->device == DEVICE_ID_5714S)) &&
470 	    (cidp->revision == REVISION_ID_5714_A0)) {
471 		command &= ~PCI_COMM_PARITY_DETECT;
472 	}
473 	pci_config_put16(handle, PCI_CONF_COMM, command);
474 
475 	/*
476 	 * On some PCI-E device, there were instances when
477 	 * the device was still link training.
478 	 */
479 	if (bgep->chipid.pci_type == BGE_PCI_E) {
480 		i = 0;
481 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
482 		while ((value16 != command) && (i < 100)) {
483 			drv_usecwait(200);
484 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
485 			++i;
486 		}
487 	}
488 
489 	/*
490 	 * Clear any remaining error status bits
491 	 */
492 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
493 
494 	/*
495 	 * Do following if and only if the device is NOT BCM5714C OR
496 	 * BCM5715C
497 	 */
498 	if (!((cidp->device == DEVICE_ID_5714C) ||
499 	    (cidp->device == DEVICE_ID_5715C))) {
500 		/*
501 		 * Make sure these indirect-access registers are sane
502 		 * rather than random after power-up or reset
503 		 */
504 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
505 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
506 	}
507 	/*
508 	 * Step 8: Disable PCI-X/PCI-E Relaxed Ordering
509 	 */
510 	bge_cfg_clr16(bgep, PCIX_CONF_COMM, PCIX_COMM_RELAXED);
511 
512 	if (cidp->pci_type == BGE_PCI_E) {
513 		if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
514 			bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL_5723,
515 			    DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
516 		} else
517 			bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL,
518 			    DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
519 	}
520 }
521 
522 #ifdef __amd64
523 /*
524  * Distinguish CPU types
525  *
526  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
527  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
528  * for PCI-Express based network interface card. This is the work-around
529  * for those nics.
530  */
531 static boolean_t bge_get_em64t_type(void);
532 #pragma	inline(bge_get_em64t_type)
533 
534 static boolean_t
535 bge_get_em64t_type(void)
536 {
537 
538 	return (x86_vendor == X86_VENDOR_Intel);
539 }
540 #endif
541 
542 /*
543  * Operating register get/set access routines
544  */
545 
546 uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
547 #pragma	inline(bge_reg_get32)
548 
549 uint32_t
550 bge_reg_get32(bge_t *bgep, bge_regno_t regno)
551 {
552 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
553 	    (void *)bgep, regno));
554 
555 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
556 }
557 
558 void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
559 #pragma	inline(bge_reg_put32)
560 
561 void
562 bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
563 {
564 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
565 	    (void *)bgep, regno, data));
566 
567 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
568 	BGE_PCICHK(bgep);
569 }
570 
571 void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
572 #pragma	inline(bge_reg_set32)
573 
574 void
575 bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
576 {
577 	uint32_t regval;
578 
579 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
580 	    (void *)bgep, regno, bits));
581 
582 	regval = bge_reg_get32(bgep, regno);
583 	regval |= bits;
584 	bge_reg_put32(bgep, regno, regval);
585 }
586 
587 void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
588 #pragma	inline(bge_reg_clr32)
589 
590 void
591 bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
592 {
593 	uint32_t regval;
594 
595 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
596 	    (void *)bgep, regno, bits));
597 
598 	regval = bge_reg_get32(bgep, regno);
599 	regval &= ~bits;
600 	bge_reg_put32(bgep, regno, regval);
601 }
602 
603 static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
604 #pragma	inline(bge_reg_get64)
605 
606 static uint64_t
607 bge_reg_get64(bge_t *bgep, bge_regno_t regno)
608 {
609 	uint64_t regval;
610 
611 #ifdef	__amd64
612 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type()) {
613 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
614 		regval <<= 32;
615 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
616 	} else {
617 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
618 	}
619 #elif defined(__sparc)
620 	if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
621 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
622 		regval <<= 32;
623 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
624 	} else {
625 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
626 	}
627 #else
628 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
629 #endif
630 
631 #ifdef	_LITTLE_ENDIAN
632 	regval = (regval >> 32) | (regval << 32);
633 #endif	/* _LITTLE_ENDIAN */
634 
635 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
636 	    (void *)bgep, regno, regval));
637 
638 	return (regval);
639 }
640 
641 static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
642 #pragma	inline(bge_reg_put64)
643 
644 static void
645 bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
646 {
647 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
648 	    (void *)bgep, regno, data));
649 
650 #ifdef	_LITTLE_ENDIAN
651 	data = ((data >> 32) | (data << 32));
652 #endif	/* _LITTLE_ENDIAN */
653 
654 #ifdef	__amd64
655 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type()) {
656 		ddi_put32(bgep->io_handle,
657 		    PIO_ADDR(bgep, regno), (uint32_t)data);
658 		BGE_PCICHK(bgep);
659 		ddi_put32(bgep->io_handle,
660 		    PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
661 
662 	} else {
663 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
664 	}
665 #elif defined(__sparc)
666 	if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
667 		ddi_put32(bgep->io_handle,
668 		    PIO_ADDR(bgep, regno + 4), (uint32_t)data);
669 		BGE_PCICHK(bgep);
670 		ddi_put32(bgep->io_handle,
671 		    PIO_ADDR(bgep, regno), (uint32_t)(data >> 32));
672 	} else {
673 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
674 	}
675 #else
676 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
677 #endif
678 
679 	BGE_PCICHK(bgep);
680 }
681 
682 /*
683  * The DDI doesn't provide get/put functions for 128 bit data
684  * so we put RCBs out as two 64-bit chunks instead.
685  */
686 static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
687 #pragma	inline(bge_reg_putrcb)
688 
689 static void
690 bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
691 {
692 	uint64_t *p;
693 
694 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
695 	    (void *)bgep, addr, rcbp->host_ring_addr,
696 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
697 
698 	ASSERT((addr % sizeof (*rcbp)) == 0);
699 
700 	p = (void *)rcbp;
701 	bge_reg_put64(bgep, addr, *p++);
702 	bge_reg_put64(bgep, addr+8, *p);
703 }
704 
705 void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
706 #pragma	inline(bge_mbx_put)
707 
708 void
709 bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
710 {
711 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
712 		regno += INTERRUPT_LP_MBOX_0_REG - INTERRUPT_MBOX_0_REG + 4;
713 
714 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
715 	    (void *)bgep, regno, data));
716 
717 	/*
718 	 * Mailbox registers are nominally 64 bits on the 5701, but
719 	 * the MSW isn't used.  On the 5703, they're only 32 bits
720 	 * anyway.  So here we just write the lower(!) 32 bits -
721 	 * remembering that the chip is big-endian, even though the
722 	 * PCI bus is little-endian ...
723 	 */
724 #ifdef	_BIG_ENDIAN
725 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
726 #else
727 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
728 #endif	/* _BIG_ENDIAN */
729 	BGE_PCICHK(bgep);
730 }
731 
732 uint32_t bge_mbx_get(bge_t *bgep, bge_regno_t regno);
733 #pragma inline(bge_mbx_get)
734 
735 uint32_t
736 bge_mbx_get(bge_t *bgep, bge_regno_t regno)
737 {
738 	uint32_t val32;
739 
740 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
741 		regno += INTERRUPT_LP_MBOX_0_REG - INTERRUPT_MBOX_0_REG + 4;
742 
743 	BGE_TRACE(("bge_mbx_get($%p, 0x%lx)",
744 	    (void *)bgep, regno));
745 
746 #ifdef	_BIG_ENDIAN
747 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno+4));
748 #else
749 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
750 #endif	/* _BIG_ENDIAN */
751 	BGE_PCICHK(bgep);
752 
753 	return (val32);
754 }
755 
756 
757 #if	BGE_DEBUGGING
758 
759 void bge_led_mark(bge_t *bgep);
760 #pragma	no_inline(bge_led_mark)
761 
762 void
763 bge_led_mark(bge_t *bgep)
764 {
765 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
766 	    LED_CONTROL_1000MBPS_LED |
767 	    LED_CONTROL_100MBPS_LED |
768 	    LED_CONTROL_10MBPS_LED;
769 
770 	/*
771 	 * Blink all three LINK LEDs on simultaneously, then all off,
772 	 * then restore to automatic hardware control.  This is used
773 	 * in laboratory testing to trigger a logic analyser or scope.
774 	 */
775 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
776 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
777 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
778 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
779 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
780 }
781 
782 #endif	/* BGE_DEBUGGING */
783 
784 /*
785  * NIC on-chip memory access routines
786  *
787  * Only 32K of NIC memory is visible at a time, controlled by the
788  * Memory Window Base Address Register (in PCI config space).  Once
789  * this is set, the 32K region of NIC-local memory that it refers
790  * to can be directly addressed in the upper 32K of the 64K of PCI
791  * memory space used for the device.
792  */
793 
794 static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
795 #pragma	inline(bge_nic_setwin)
796 
797 static void
798 bge_nic_setwin(bge_t *bgep, bge_regno_t base)
799 {
800 	chip_id_t *cidp;
801 
802 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
803 	    (void *)bgep, base));
804 
805 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
806 
807 	/*
808 	 * Don't do repeated zero data writes,
809 	 * if the device is BCM5714C/15C.
810 	 */
811 	cidp = &bgep->chipid;
812 	if ((cidp->device == DEVICE_ID_5714C) ||
813 	    (cidp->device == DEVICE_ID_5715C)) {
814 		if (bgep->lastWriteZeroData && (base == (bge_regno_t)0))
815 			return;
816 		/* Adjust lastWriteZeroData */
817 		bgep->lastWriteZeroData = ((base == (bge_regno_t)0) ?
818 		    B_TRUE : B_FALSE);
819 	}
820 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
821 }
822 
823 static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
824 #pragma	inline(bge_nic_get32)
825 
826 static uint32_t
827 bge_nic_get32(bge_t *bgep, bge_regno_t addr)
828 {
829 	uint32_t data;
830 
831 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
832 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
833 		/* workaround for word swap error */
834 		if (addr & 4)
835 			addr = addr - 4;
836 		else
837 			addr = addr + 4;
838 	}
839 #endif
840 
841 #ifdef __sparc
842 	data = bge_nic_read32(bgep, addr);
843 #else
844 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
845 	addr &= MWBAR_GRANULE_MASK;
846 	addr += NIC_MEM_WINDOW_OFFSET;
847 
848 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
849 #endif
850 
851 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
852 	    (void *)bgep, addr, data));
853 
854 	return (data);
855 }
856 
857 void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
858 #pragma inline(bge_nic_put32)
859 
860 void
861 bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
862 {
863 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
864 	    (void *)bgep, addr, data));
865 
866 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
867 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
868 		/* workaround for word swap error */
869 		if (addr & 4)
870 			addr = addr - 4;
871 		else
872 			addr = addr + 4;
873 	}
874 #endif
875 
876 #ifdef __sparc
877 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
878 	data = LE_32(data);
879 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR, data);
880 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
881 #else
882 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
883 	addr &= MWBAR_GRANULE_MASK;
884 	addr += NIC_MEM_WINDOW_OFFSET;
885 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
886 	BGE_PCICHK(bgep);
887 #endif
888 }
889 
890 static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
891 #pragma	inline(bge_nic_get64)
892 
893 static uint64_t
894 bge_nic_get64(bge_t *bgep, bge_regno_t addr)
895 {
896 	uint64_t data;
897 
898 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
899 	addr &= MWBAR_GRANULE_MASK;
900 	addr += NIC_MEM_WINDOW_OFFSET;
901 
902 #ifdef	__amd64
903 		if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type()) {
904 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
905 			data <<= 32;
906 			data |= ddi_get32(bgep->io_handle,
907 			    PIO_ADDR(bgep, addr + 4));
908 		} else {
909 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
910 		}
911 #elif defined(__sparc)
912 		if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
913 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
914 			data <<= 32;
915 			data |= ddi_get32(bgep->io_handle,
916 			    PIO_ADDR(bgep, addr + 4));
917 		} else {
918 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
919 		}
920 #else
921 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
922 #endif
923 
924 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
925 	    (void *)bgep, addr, data));
926 
927 	return (data);
928 }
929 
930 static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
931 #pragma	inline(bge_nic_put64)
932 
933 static void
934 bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
935 {
936 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
937 	    (void *)bgep, addr, data));
938 
939 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
940 	addr &= MWBAR_GRANULE_MASK;
941 	addr += NIC_MEM_WINDOW_OFFSET;
942 
943 #ifdef	__amd64
944 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type()) {
945 		ddi_put32(bgep->io_handle,
946 		    PIO_ADDR(bgep, addr), (uint32_t)data);
947 		BGE_PCICHK(bgep);
948 		ddi_put32(bgep->io_handle,
949 		    PIO_ADDR(bgep, addr + 4), (uint32_t)(data >> 32));
950 	} else {
951 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
952 	}
953 #elif defined(__sparc)
954 	if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
955 		ddi_put32(bgep->io_handle,
956 		    PIO_ADDR(bgep, addr + 4), (uint32_t)data);
957 		BGE_PCICHK(bgep);
958 		ddi_put32(bgep->io_handle,
959 		    PIO_ADDR(bgep, addr), (uint32_t)(data >> 32));
960 	} else {
961 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
962 	}
963 #else
964 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
965 #endif
966 
967 	BGE_PCICHK(bgep);
968 }
969 
970 /*
971  * The DDI doesn't provide get/put functions for 128 bit data
972  * so we put RCBs out as two 64-bit chunks instead.
973  */
974 static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
975 #pragma	inline(bge_nic_putrcb)
976 
977 static void
978 bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
979 {
980 	uint64_t *p;
981 
982 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
983 	    (void *)bgep, addr, rcbp->host_ring_addr,
984 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
985 
986 	ASSERT((addr % sizeof (*rcbp)) == 0);
987 
988 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
989 	addr &= MWBAR_GRANULE_MASK;
990 	addr += NIC_MEM_WINDOW_OFFSET;
991 
992 	p = (void *)rcbp;
993 #ifdef	__amd64
994 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type()) {
995 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
996 		    (uint32_t)(*p));
997 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
998 		    (uint32_t)(*p >> 32));
999 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
1000 		    (uint32_t)(*(p + 1)));
1001 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
1002 		    (uint32_t)(*p >> 32));
1003 
1004 	} else {
1005 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
1006 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
1007 	}
1008 #elif defined(__sparc)
1009 	if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
1010 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
1011 		    (uint32_t)(*p));
1012 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
1013 		    (uint32_t)(*p >> 32));
1014 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
1015 		    (uint32_t)(*(p + 1)));
1016 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
1017 		    (uint32_t)(*p >> 32));
1018 	} else {
1019 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
1020 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
1021 	}
1022 #else
1023 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
1024 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
1025 #endif
1026 
1027 	BGE_PCICHK(bgep);
1028 }
1029 
1030 static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
1031 #pragma	inline(bge_nic_zero)
1032 
1033 static void
1034 bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
1035 {
1036 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
1037 	    (void *)bgep, addr, nbytes));
1038 
1039 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
1040 	    ((addr+nbytes) & ~MWBAR_GRANULE_MASK));
1041 
1042 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
1043 	addr &= MWBAR_GRANULE_MASK;
1044 	addr += NIC_MEM_WINDOW_OFFSET;
1045 
1046 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
1047 	    nbytes, 1, DDI_DATA_SZ08_ACC);
1048 	BGE_PCICHK(bgep);
1049 }
1050 
1051 /*
1052  * MII (PHY) register get/set access routines
1053  *
1054  * These use the chip's MII auto-access method, controlled by the
1055  * MII Communication register at 0x044c, so the CPU doesn't have
1056  * to fiddle with the individual bits.
1057  */
1058 
1059 #undef	BGE_DBG
1060 #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
1061 
1062 static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
1063 				uint16_t data, uint32_t cmd);
1064 #pragma	no_inline(bge_mii_access)
1065 
1066 static uint16_t
1067 bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
1068 {
1069 	uint32_t timeout;
1070 	uint32_t regval1;
1071 	uint32_t regval2;
1072 
1073 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
1074 	    (void *)bgep, regno, data, cmd));
1075 
1076 	ASSERT(mutex_owned(bgep->genlock));
1077 
1078 	/*
1079 	 * Assemble the command ...
1080 	 */
1081 	cmd |= data << MI_COMMS_DATA_SHIFT;
1082 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
1083 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
1084 	cmd |= MI_COMMS_START;
1085 
1086 	/*
1087 	 * Wait for any command already in progress ...
1088 	 *
1089 	 * Note: this *shouldn't* ever find that there is a command
1090 	 * in progress, because we already hold the <genlock> mutex.
1091 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
1092 	 * bit set here -- it seems that the chip can initiate MII
1093 	 * accesses internally, even with polling OFF.
1094 	 */
1095 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
1096 	for (timeout = 100; ; ) {
1097 		if ((regval2 & MI_COMMS_START) == 0) {
1098 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
1099 			break;
1100 		}
1101 		if (--timeout == 0)
1102 			break;
1103 		drv_usecwait(10);
1104 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
1105 	}
1106 
1107 	if (timeout == 0)
1108 		return ((uint16_t)~0u);
1109 
1110 	if (timeout != 100)
1111 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
1112 		    "MI_COMMS_START set for %d us; 0x%x->0x%x",
1113 		    cmd, 10*(100-timeout), regval1, regval2));
1114 
1115 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
1116 	for (timeout = 1000; ; ) {
1117 		if ((regval1 & MI_COMMS_START) == 0)
1118 			break;
1119 		if (--timeout == 0)
1120 			break;
1121 		drv_usecwait(10);
1122 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
1123 	}
1124 
1125 	/*
1126 	 * Drop out early if the READ FAILED bit is set -- this chip
1127 	 * could be a 5703/4S, with a SerDes instead of a PHY!
1128 	 */
1129 	if (regval2 & MI_COMMS_READ_FAILED)
1130 		return ((uint16_t)~0u);
1131 
1132 	if (timeout == 0)
1133 		return ((uint16_t)~0u);
1134 
1135 	/*
1136 	 * The PRM says to wait 5us after seeing the START bit clear
1137 	 * and then re-read the register to get the final value of the
1138 	 * data field, in order to avoid a race condition where the
1139 	 * START bit is clear but the data field isn't yet valid.
1140 	 *
1141 	 * Note: we don't actually seem to be encounter this race;
1142 	 * except when the START bit is seen set again (see below),
1143 	 * the data field doesn't change during this 5us interval.
1144 	 */
1145 	drv_usecwait(5);
1146 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
1147 
1148 	/*
1149 	 * Unfortunately, when following the PRMs instructions above,
1150 	 * we have occasionally seen the START bit set again(!) in the
1151 	 * value read after the 5us delay. This seems to be due to the
1152 	 * chip autonomously starting another MII access internally.
1153 	 * In such cases, the command/data/etc fields relate to the
1154 	 * internal command, rather than the one that we thought had
1155 	 * just finished.  So in this case, we fall back to returning
1156 	 * the data from the original read that showed START clear.
1157 	 */
1158 	if (regval2 & MI_COMMS_START) {
1159 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
1160 		    "MI_COMMS_START set after transaction; 0x%x->0x%x",
1161 		    cmd, regval1, regval2));
1162 		regval2 = regval1;
1163 	}
1164 
1165 	if (regval2 & MI_COMMS_START)
1166 		return ((uint16_t)~0u);
1167 
1168 	if (regval2 & MI_COMMS_READ_FAILED)
1169 		return ((uint16_t)~0u);
1170 
1171 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
1172 }
1173 
1174 uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
1175 #pragma	no_inline(bge_mii_get16)
1176 
1177 uint16_t
1178 bge_mii_get16(bge_t *bgep, bge_regno_t regno)
1179 {
1180 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
1181 	    (void *)bgep, regno));
1182 
1183 	ASSERT(mutex_owned(bgep->genlock));
1184 
1185 	if (DEVICE_5906_SERIES_CHIPSETS(bgep) && ((regno == MII_AUX_CONTROL) ||
1186 	    (regno == MII_MSCONTROL)))
1187 		return (0);
1188 
1189 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
1190 }
1191 
1192 void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
1193 #pragma	no_inline(bge_mii_put16)
1194 
1195 void
1196 bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
1197 {
1198 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
1199 	    (void *)bgep, regno, data));
1200 
1201 	ASSERT(mutex_owned(bgep->genlock));
1202 
1203 	if (DEVICE_5906_SERIES_CHIPSETS(bgep) && ((regno == MII_AUX_CONTROL) ||
1204 	    (regno == MII_MSCONTROL)))
1205 		return;
1206 
1207 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
1208 }
1209 
1210 #undef	BGE_DBG
1211 #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
1212 
1213 #if	BGE_SEE_IO32 || BGE_FLASH_IO32
1214 
1215 /*
1216  * Basic SEEPROM get/set access routine
1217  *
1218  * This uses the chip's SEEPROM auto-access method, controlled by the
1219  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
1220  * doesn't have to fiddle with the individual bits.
1221  *
1222  * The caller should hold <genlock> and *also* have already acquired
1223  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
1224  *
1225  * Return value:
1226  *	0 on success,
1227  *	ENODATA on access timeout (maybe retryable: device may just be busy)
1228  *	EPROTO on other h/w or s/w errors.
1229  *
1230  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
1231  * from a (successful) SEEPROM_ACCESS_READ.
1232  */
1233 static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
1234 				uint32_t *dp);
1235 #pragma	no_inline(bge_seeprom_access)
1236 
1237 static int
1238 bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
1239 {
1240 	uint32_t tries;
1241 	uint32_t regval;
1242 
1243 	ASSERT(mutex_owned(bgep->genlock));
1244 
1245 	/*
1246 	 * On the newer chips that support both SEEPROM & Flash, we need
1247 	 * to specifically enable SEEPROM access (Flash is the default).
1248 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
1249 	 * and the NVM control registers don't exist ...
1250 	 */
1251 	switch (bgep->chipid.nvtype) {
1252 	case BGE_NVTYPE_NONE:
1253 	case BGE_NVTYPE_UNKNOWN:
1254 		_NOTE(NOTREACHED)
1255 	case BGE_NVTYPE_SEEPROM:
1256 		break;
1257 
1258 	case BGE_NVTYPE_LEGACY_SEEPROM:
1259 	case BGE_NVTYPE_UNBUFFERED_FLASH:
1260 	case BGE_NVTYPE_BUFFERED_FLASH:
1261 	default:
1262 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
1263 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
1264 		break;
1265 	}
1266 
1267 	/*
1268 	 * Check there's no command in progress.
1269 	 *
1270 	 * Note: this *shouldn't* ever find that there is a command
1271 	 * in progress, because we already hold the <genlock> mutex.
1272 	 * Also, to ensure we don't have a conflict with the chip's
1273 	 * internal firmware or a process accessing the same (shared)
1274 	 * SEEPROM through the other port of a 5704, we've already
1275 	 * been through the "software arbitration" protocol.
1276 	 * So this is just a final consistency check: we shouldn't
1277 	 * see EITHER the START bit (command started but not complete)
1278 	 * OR the COMPLETE bit (command completed but not cleared).
1279 	 */
1280 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
1281 	if (regval & SEEPROM_ACCESS_START)
1282 		return (EPROTO);
1283 	if (regval & SEEPROM_ACCESS_COMPLETE)
1284 		return (EPROTO);
1285 
1286 	/*
1287 	 * Assemble the command ...
1288 	 */
1289 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
1290 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
1291 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
1292 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
1293 	cmd |= SEEPROM_ACCESS_START;
1294 	cmd |= SEEPROM_ACCESS_COMPLETE;
1295 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
1296 
1297 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
1298 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
1299 
1300 	/*
1301 	 * By observation, a successful access takes ~20us on a 5703/4,
1302 	 * but apparently much longer (up to 1000us) on the obsolescent
1303 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
1304 	 * timeouts here; but OTOH, we don't want a bogus access to lock
1305 	 * out interrupts for longer than necessary. So we'll allow up
1306 	 * to 1000us ...
1307 	 */
1308 	for (tries = 0; tries < 1000; ++tries) {
1309 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
1310 		if (regval & SEEPROM_ACCESS_COMPLETE)
1311 			break;
1312 		drv_usecwait(1);
1313 	}
1314 
1315 	if (regval & SEEPROM_ACCESS_COMPLETE) {
1316 		/*
1317 		 * All OK; read the SEEPROM data register, then write back
1318 		 * the value read from the address register in order to
1319 		 * clear the <complete> bit and leave the SEEPROM access
1320 		 * state machine idle, ready for the next access ...
1321 		 */
1322 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
1323 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
1324 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
1325 		return (0);
1326 	}
1327 
1328 	/*
1329 	 * Hmm ... what happened here?
1330 	 *
1331 	 * Most likely, the user addressed a non-existent SEEPROM. Or
1332 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
1333 	 * and didn't respond to being addressed. Either way, it's left
1334 	 * the SEEPROM access state machine wedged. So we'll reset it
1335 	 * before we leave, so it's ready for next time ...
1336 	 */
1337 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
1338 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
1339 	return (ENODATA);
1340 }
1341 
1342 /*
1343  * Basic Flash get/set access routine
1344  *
1345  * These use the chip's Flash auto-access method, controlled by the
1346  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
1347  * fiddle with the individual bits.
1348  *
1349  * The caller should hold <genlock> and *also* have already acquired
1350  * the right to access the Flash, via bge_nvmem_acquire() above.
1351  *
1352  * Return value:
1353  *	0 on success,
1354  *	ENODATA on access timeout (maybe retryable: device may just be busy)
1355  *	ENODEV if the NVmem device is missing or otherwise unusable
1356  *
1357  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
1358  * from a (successful) NVM_FLASH_CMD_RD.
1359  */
1360 static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
1361 				uint32_t *dp);
1362 #pragma	no_inline(bge_flash_access)
1363 
1364 static int
1365 bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
1366 {
1367 	uint32_t tries;
1368 	uint32_t regval;
1369 
1370 	ASSERT(mutex_owned(bgep->genlock));
1371 
1372 	/*
1373 	 * On the newer chips that support both SEEPROM & Flash, we need
1374 	 * to specifically disable SEEPROM access while accessing Flash.
1375 	 * The older chips don't support Flash, and the NVM registers don't
1376 	 * exist, so we shouldn't be here at all!
1377 	 */
1378 	switch (bgep->chipid.nvtype) {
1379 	case BGE_NVTYPE_NONE:
1380 	case BGE_NVTYPE_UNKNOWN:
1381 		_NOTE(NOTREACHED)
1382 	case BGE_NVTYPE_SEEPROM:
1383 		return (ENODEV);
1384 
1385 	case BGE_NVTYPE_LEGACY_SEEPROM:
1386 	case BGE_NVTYPE_UNBUFFERED_FLASH:
1387 	case BGE_NVTYPE_BUFFERED_FLASH:
1388 	default:
1389 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
1390 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
1391 		break;
1392 	}
1393 
1394 	/*
1395 	 * Assemble the command ...
1396 	 */
1397 	addr &= NVM_FLASH_ADDR_MASK;
1398 	cmd |= NVM_FLASH_CMD_DOIT;
1399 	cmd |= NVM_FLASH_CMD_FIRST;
1400 	cmd |= NVM_FLASH_CMD_LAST;
1401 	cmd |= NVM_FLASH_CMD_DONE;
1402 
1403 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
1404 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
1405 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
1406 
1407 	/*
1408 	 * Allow up to 1000ms ...
1409 	 */
1410 	for (tries = 0; tries < 1000; ++tries) {
1411 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
1412 		if (regval & NVM_FLASH_CMD_DONE)
1413 			break;
1414 		drv_usecwait(1);
1415 	}
1416 
1417 	if (regval & NVM_FLASH_CMD_DONE) {
1418 		/*
1419 		 * All OK; read the data from the Flash read register
1420 		 */
1421 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
1422 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
1423 		return (0);
1424 	}
1425 
1426 	/*
1427 	 * Hmm ... what happened here?
1428 	 *
1429 	 * Most likely, the user addressed a non-existent Flash. Or
1430 	 * maybe the Flash was busy internally (e.g. processing a write)
1431 	 * and didn't respond to being addressed. Either way, there's
1432 	 * nothing we can here ...
1433 	 */
1434 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
1435 	return (ENODATA);
1436 }
1437 
1438 /*
1439  * The next two functions regulate access to the NVram (if fitted).
1440  *
1441  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
1442  * (SPI) interface, but they can be accessed through either port. These
1443  * are managed by different instance of this driver and have no software
1444  * state in common.
1445  *
1446  * In addition (and even on a single core chip) the chip's internal
1447  * firmware can access the SEEPROM/Flash, most notably after a RESET
1448  * when it may download code to run internally.
1449  *
1450  * So we need to arbitrate between these various software agents.  For
1451  * this purpose, the chip provides the Software Arbitration Register,
1452  * which implements hardware(!) arbitration.
1453  *
1454  * This functionality didn't exist on older (5700/5701) chips, so there's
1455  * nothing we can do by way of arbitration on those; also, if there's no
1456  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
1457  * nothing to do.
1458  *
1459  * The internal firmware appears to use Request 0, which is the highest
1460  * priority.  So we'd like to use Request 2, leaving one higher and one
1461  * lower for any future developments ... but apparently this doesn't
1462  * always work.  So for now, the code uses Request 1 ;-(
1463  */
1464 
1465 #define	NVM_READ_REQ	NVM_READ_REQ1
1466 #define	NVM_RESET_REQ	NVM_RESET_REQ1
1467 #define	NVM_SET_REQ	NVM_SET_REQ1
1468 
1469 static void bge_nvmem_relinquish(bge_t *bgep);
1470 #pragma	no_inline(bge_nvmem_relinquish)
1471 
1472 static void
1473 bge_nvmem_relinquish(bge_t *bgep)
1474 {
1475 	ASSERT(mutex_owned(bgep->genlock));
1476 
1477 	switch (bgep->chipid.nvtype) {
1478 	case BGE_NVTYPE_NONE:
1479 	case BGE_NVTYPE_UNKNOWN:
1480 		_NOTE(NOTREACHED)
1481 		return;
1482 
1483 	case BGE_NVTYPE_SEEPROM:
1484 		/*
1485 		 * No arbitration performed, no release needed
1486 		 */
1487 		return;
1488 
1489 	case BGE_NVTYPE_LEGACY_SEEPROM:
1490 	case BGE_NVTYPE_UNBUFFERED_FLASH:
1491 	case BGE_NVTYPE_BUFFERED_FLASH:
1492 	default:
1493 		break;
1494 	}
1495 
1496 	/*
1497 	 * Our own request should be present (whether or not granted) ...
1498 	 */
1499 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
1500 
1501 	/*
1502 	 * ... this will make it go away.
1503 	 */
1504 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
1505 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
1506 }
1507 
1508 /*
1509  * Arbitrate for access to the NVmem, if necessary
1510  *
1511  * Return value:
1512  *	0 on success
1513  *	EAGAIN if the device is in use (retryable)
1514  *	ENODEV if the NVmem device is missing or otherwise unusable
1515  */
1516 static int bge_nvmem_acquire(bge_t *bgep);
1517 #pragma	no_inline(bge_nvmem_acquire)
1518 
1519 static int
1520 bge_nvmem_acquire(bge_t *bgep)
1521 {
1522 	uint32_t regval;
1523 	uint32_t tries;
1524 
1525 	ASSERT(mutex_owned(bgep->genlock));
1526 
1527 	switch (bgep->chipid.nvtype) {
1528 	case BGE_NVTYPE_NONE:
1529 	case BGE_NVTYPE_UNKNOWN:
1530 		/*
1531 		 * Access denied: no (recognisable) device fitted
1532 		 */
1533 		return (ENODEV);
1534 
1535 	case BGE_NVTYPE_SEEPROM:
1536 		/*
1537 		 * Access granted: no arbitration needed (or possible)
1538 		 */
1539 		return (0);
1540 
1541 	case BGE_NVTYPE_LEGACY_SEEPROM:
1542 	case BGE_NVTYPE_UNBUFFERED_FLASH:
1543 	case BGE_NVTYPE_BUFFERED_FLASH:
1544 	default:
1545 		/*
1546 		 * Access conditional: conduct arbitration protocol
1547 		 */
1548 		break;
1549 	}
1550 
1551 	/*
1552 	 * We're holding the per-port mutex <genlock>, so no-one other
1553 	 * thread can be attempting to access the NVmem through *this*
1554 	 * port. But it could be in use by the *other* port (of a 5704),
1555 	 * or by the chip's internal firmware, so we have to go through
1556 	 * the full (hardware) arbitration protocol ...
1557 	 *
1558 	 * Note that *because* we're holding <genlock>, the interrupt handler
1559 	 * won't be able to progress.  So we're only willing to spin for a
1560 	 * fairly short time.  Specifically:
1561 	 *
1562 	 *	We *must* wait long enough for the hardware to resolve all
1563 	 *	requests and determine the winner.  Fortunately, this is
1564 	 *	"almost instantaneous", even as observed by GHz CPUs.
1565 	 *
1566 	 *	A successful access by another Solaris thread (via either
1567 	 *	port) typically takes ~20us.  So waiting a bit longer than
1568 	 *	that will give a good chance of success, if the other user
1569 	 *	*is* another thread on the other port.
1570 	 *
1571 	 *	However, the internal firmware can hold on to the NVmem
1572 	 *	for *much* longer: at least 10 milliseconds just after a
1573 	 *	RESET, and maybe even longer if the NVmem actually contains
1574 	 *	code to download and run on the internal CPUs.
1575 	 *
1576 	 * So, we'll allow 50us; if that's not enough then it's up to the
1577 	 * caller to retry later (hence the choice of return code EAGAIN).
1578 	 */
1579 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
1580 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
1581 
1582 	for (tries = 0; tries < 50; ++tries) {
1583 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
1584 		if (regval & NVM_WON_REQ1)
1585 			break;
1586 		drv_usecwait(1);
1587 	}
1588 
1589 	if (regval & NVM_WON_REQ1) {
1590 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
1591 		return (0);
1592 	}
1593 
1594 	/*
1595 	 * Somebody else must be accessing the NVmem, so abandon our
1596 	 * attempt take control of it.  The caller can try again later ...
1597 	 */
1598 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
1599 	bge_nvmem_relinquish(bgep);
1600 	return (EAGAIN);
1601 }
1602 
1603 /*
1604  * This code assumes that the GPIO1 bit has been wired up to the NVmem
1605  * write protect line in such a way that the NVmem is protected when
1606  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
1607  * NVmem writable we have to change GPIO1 to an output AND drive it low.
1608  *
1609  * Note: there's only one set of GPIO pins on a 5704, even though they
1610  * can be accessed through either port.  So the chip has to resolve what
1611  * happens if the two ports program a single pin differently ... the rule
1612  * it uses is that if the ports disagree about the *direction* of a pin,
1613  * "output" wins over "input", but if they disagree about its *value* as
1614  * an output, then the pin is TRISTATED instead!  In such a case, no-one
1615  * wins, and the external signal does whatever the external circuitry
1616  * defines as the default -- which we've assumed is the PROTECTED state.
1617  * So, we always change GPIO1 back to being an *input* whenever we're not
1618  * specifically using it to unprotect the NVmem. This allows either port
1619  * to update the NVmem, although obviously only one at a time!
1620  *
1621  * The caller should hold <genlock> and *also* have already acquired the
1622  * right to access the NVmem, via bge_nvmem_acquire() above.
1623  */
1624 static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
1625 #pragma	inline(bge_nvmem_protect)
1626 
1627 static void
1628 bge_nvmem_protect(bge_t *bgep, boolean_t protect)
1629 {
1630 	uint32_t regval;
1631 
1632 	ASSERT(mutex_owned(bgep->genlock));
1633 
1634 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
1635 	if (protect) {
1636 		regval |= MLCR_MISC_PINS_OUTPUT_1;
1637 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
1638 	} else {
1639 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
1640 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
1641 	}
1642 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
1643 }
1644 
1645 /*
1646  * Now put it all together ...
1647  *
1648  * Try to acquire control of the NVmem; if successful, then:
1649  *	unprotect it (if we want to write to it)
1650  *	perform the requested access
1651  *	reprotect it (after a write)
1652  *	relinquish control
1653  *
1654  * Return value:
1655  *	0 on success,
1656  *	EAGAIN if the device is in use (retryable)
1657  *	ENODATA on access timeout (maybe retryable: device may just be busy)
1658  *	ENODEV if the NVmem device is missing or otherwise unusable
1659  *	EPROTO on other h/w or s/w errors.
1660  */
1661 static int
1662 bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
1663 {
1664 	int err;
1665 
1666 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
1667 		switch (cmd) {
1668 		case BGE_SEE_READ:
1669 			err = bge_seeprom_access(bgep,
1670 			    SEEPROM_ACCESS_READ, addr, dp);
1671 			break;
1672 
1673 		case BGE_SEE_WRITE:
1674 			bge_nvmem_protect(bgep, B_FALSE);
1675 			err = bge_seeprom_access(bgep,
1676 			    SEEPROM_ACCESS_WRITE, addr, dp);
1677 			bge_nvmem_protect(bgep, B_TRUE);
1678 			break;
1679 
1680 		case BGE_FLASH_READ:
1681 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
1682 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
1683 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
1684 				bge_reg_set32(bgep, NVM_ACCESS_REG,
1685 				    NVM_ACCESS_ENABLE);
1686 			}
1687 			err = bge_flash_access(bgep,
1688 			    NVM_FLASH_CMD_RD, addr, dp);
1689 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
1690 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
1691 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
1692 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
1693 				    NVM_ACCESS_ENABLE);
1694 			}
1695 			break;
1696 
1697 		case BGE_FLASH_WRITE:
1698 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
1699 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
1700 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
1701 				bge_reg_set32(bgep, NVM_ACCESS_REG,
1702 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
1703 			}
1704 			bge_nvmem_protect(bgep, B_FALSE);
1705 			err = bge_flash_access(bgep,
1706 			    NVM_FLASH_CMD_WR, addr, dp);
1707 			bge_nvmem_protect(bgep, B_TRUE);
1708 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
1709 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
1710 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
1711 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
1712 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
1713 			}
1714 
1715 			break;
1716 
1717 		default:
1718 			_NOTE(NOTREACHED)
1719 			break;
1720 		}
1721 		bge_nvmem_relinquish(bgep);
1722 	}
1723 
1724 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
1725 	return (err);
1726 }
1727 
1728 /*
1729  * Attempt to get a MAC address from the SEEPROM or Flash, if any
1730  */
1731 static uint64_t bge_get_nvmac(bge_t *bgep);
1732 #pragma no_inline(bge_get_nvmac)
1733 
1734 static uint64_t
1735 bge_get_nvmac(bge_t *bgep)
1736 {
1737 	uint32_t mac_high;
1738 	uint32_t mac_low;
1739 	uint32_t addr;
1740 	uint32_t cmd;
1741 	uint64_t mac;
1742 
1743 	BGE_TRACE(("bge_get_nvmac($%p)",
1744 	    (void *)bgep));
1745 
1746 	switch (bgep->chipid.nvtype) {
1747 	case BGE_NVTYPE_NONE:
1748 	case BGE_NVTYPE_UNKNOWN:
1749 	default:
1750 		return (0ULL);
1751 
1752 	case BGE_NVTYPE_SEEPROM:
1753 	case BGE_NVTYPE_LEGACY_SEEPROM:
1754 		cmd = BGE_SEE_READ;
1755 		break;
1756 
1757 	case BGE_NVTYPE_UNBUFFERED_FLASH:
1758 	case BGE_NVTYPE_BUFFERED_FLASH:
1759 		cmd = BGE_FLASH_READ;
1760 		break;
1761 	}
1762 
1763 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
1764 		addr = NVMEM_DATA_MAC_ADDRESS_5906;
1765 	else
1766 		addr = NVMEM_DATA_MAC_ADDRESS;
1767 
1768 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
1769 		return (0ULL);
1770 	addr += 4;
1771 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
1772 		return (0ULL);
1773 
1774 	/*
1775 	 * The Broadcom chip is natively BIG-endian, so that's how the
1776 	 * MAC address is represented in NVmem.  We may need to swap it
1777 	 * around on a little-endian host ...
1778 	 */
1779 #ifdef	_BIG_ENDIAN
1780 	mac = mac_high;
1781 	mac = mac << 32;
1782 	mac |= mac_low;
1783 #else
1784 	mac = BGE_BSWAP_32(mac_high);
1785 	mac = mac << 32;
1786 	mac |= BGE_BSWAP_32(mac_low);
1787 #endif	/* _BIG_ENDIAN */
1788 
1789 	return (mac);
1790 }
1791 
1792 #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
1793 
1794 /*
1795  * Dummy version for when we're not supporting NVmem access
1796  */
1797 static uint64_t bge_get_nvmac(bge_t *bgep);
1798 #pragma inline(bge_get_nvmac)
1799 
1800 static uint64_t
1801 bge_get_nvmac(bge_t *bgep)
1802 {
1803 	_NOTE(ARGUNUSED(bgep))
1804 	return (0ULL);
1805 }
1806 
1807 #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
1808 
1809 /*
1810  * Determine the type of NVmem that is (or may be) attached to this chip,
1811  */
1812 static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
1813 #pragma no_inline(bge_nvmem_id)
1814 
1815 static enum bge_nvmem_type
1816 bge_nvmem_id(bge_t *bgep)
1817 {
1818 	enum bge_nvmem_type nvtype;
1819 	uint32_t config1;
1820 
1821 	BGE_TRACE(("bge_nvmem_id($%p)",
1822 	    (void *)bgep));
1823 
1824 	switch (bgep->chipid.device) {
1825 	default:
1826 		/*
1827 		 * We shouldn't get here; it means we don't recognise
1828 		 * the chip, which means we don't know how to determine
1829 		 * what sort of NVmem (if any) it has.  So we'll say
1830 		 * NONE, to disable the NVmem access code ...
1831 		 */
1832 		nvtype = BGE_NVTYPE_NONE;
1833 		break;
1834 
1835 	case DEVICE_ID_5700:
1836 	case DEVICE_ID_5700x:
1837 	case DEVICE_ID_5701:
1838 		/*
1839 		 * These devices support *only* SEEPROMs
1840 		 */
1841 		nvtype = BGE_NVTYPE_SEEPROM;
1842 		break;
1843 
1844 	case DEVICE_ID_5702:
1845 	case DEVICE_ID_5702fe:
1846 	case DEVICE_ID_5703C:
1847 	case DEVICE_ID_5703S:
1848 	case DEVICE_ID_5704C:
1849 	case DEVICE_ID_5704S:
1850 	case DEVICE_ID_5704:
1851 	case DEVICE_ID_5705M:
1852 	case DEVICE_ID_5705C:
1853 	case DEVICE_ID_5705_2:
1854 	case DEVICE_ID_5780:
1855 	case DEVICE_ID_5782:
1856 	case DEVICE_ID_5787:
1857 	case DEVICE_ID_5787M:
1858 	case DEVICE_ID_5788:
1859 	case DEVICE_ID_5789:
1860 	case DEVICE_ID_5751:
1861 	case DEVICE_ID_5751M:
1862 	case DEVICE_ID_5752:
1863 	case DEVICE_ID_5752M:
1864 	case DEVICE_ID_5754:
1865 	case DEVICE_ID_5755:
1866 	case DEVICE_ID_5755M:
1867 	case DEVICE_ID_5756M:
1868 	case DEVICE_ID_5721:
1869 	case DEVICE_ID_5722:
1870 	case DEVICE_ID_5723:
1871 	case DEVICE_ID_5761:
1872 	case DEVICE_ID_5761E:
1873 	case DEVICE_ID_5714C:
1874 	case DEVICE_ID_5714S:
1875 	case DEVICE_ID_5715C:
1876 	case DEVICE_ID_5715S:
1877 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
1878 		if (config1 & NVM_CFG1_FLASH_MODE)
1879 			if (config1 & NVM_CFG1_BUFFERED_MODE)
1880 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
1881 			else
1882 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
1883 		else
1884 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
1885 		break;
1886 	case DEVICE_ID_5906:
1887 	case DEVICE_ID_5906M:
1888 		nvtype = BGE_NVTYPE_BUFFERED_FLASH;
1889 		break;
1890 	}
1891 
1892 	return (nvtype);
1893 }
1894 
1895 #undef	BGE_DBG
1896 #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
1897 
1898 static void
1899 bge_init_recv_rule(bge_t *bgep)
1900 {
1901 	bge_recv_rule_t *rulep = bgep->recv_rules;
1902 	uint32_t i;
1903 
1904 	/*
1905 	 * Initialize receive rule registers.
1906 	 * Note that rules may persist across each bge_m_start/stop() call.
1907 	 */
1908 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
1909 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
1910 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
1911 	}
1912 }
1913 
1914 /*
1915  * Using the values captured by bge_chip_cfg_init(), and additional probes
1916  * as required, characterise the chip fully: determine the label by which
1917  * to refer to this chip, the correct settings for various registers, and
1918  * of course whether the device and/or subsystem are supported!
1919  */
1920 int bge_chip_id_init(bge_t *bgep);
1921 #pragma	no_inline(bge_chip_id_init)
1922 
1923 int
1924 bge_chip_id_init(bge_t *bgep)
1925 {
1926 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
1927 	boolean_t sys_ok;
1928 	boolean_t dev_ok;
1929 	chip_id_t *cidp;
1930 	uint32_t subid;
1931 	char *devname;
1932 	char *sysname;
1933 	int *ids;
1934 	int err;
1935 	uint_t i;
1936 
1937 	sys_ok = dev_ok = B_FALSE;
1938 	cidp = &bgep->chipid;
1939 
1940 	/*
1941 	 * Check the PCI device ID to determine the generic chip type and
1942 	 * select parameters that depend on this.
1943 	 *
1944 	 * Note: because the SPARC platforms in general don't fit the
1945 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
1946 	 * as zero - which is why we use <asic_rev> rather than <revision>
1947 	 * below ...
1948 	 *
1949 	 * Note: in general we can't distinguish between the Copper/SerDes
1950 	 * versions by ID alone, as some Copper devices (e.g. some but not
1951 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
1952 	 * treat them the same here, and the MII code works out the media
1953 	 * type later on ...
1954 	 */
1955 	cidp->mbuf_base = bge_mbuf_pool_base;
1956 	cidp->mbuf_length = bge_mbuf_pool_len;
1957 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
1958 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
1959 	cidp->pci_type = BGE_PCI_X;
1960 	cidp->statistic_type = BGE_STAT_BLK;
1961 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
1962 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
1963 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
1964 	cidp->rx_ticks_norm = bge_rx_ticks_norm;
1965 	cidp->rx_count_norm = bge_rx_count_norm;
1966 	cidp->tx_ticks_norm = bge_tx_ticks_norm;
1967 	cidp->tx_count_norm = bge_tx_count_norm;
1968 
1969 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
1970 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
1971 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
1972 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
1973 
1974 	cidp->msi_enabled = B_FALSE;
1975 
1976 	switch (cidp->device) {
1977 	case DEVICE_ID_5700:
1978 	case DEVICE_ID_5700x:
1979 		cidp->chip_label = 5700;
1980 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
1981 		break;
1982 
1983 	case DEVICE_ID_5701:
1984 		cidp->chip_label = 5701;
1985 		dev_ok = B_TRUE;
1986 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
1987 		break;
1988 
1989 	case DEVICE_ID_5702:
1990 	case DEVICE_ID_5702fe:
1991 		cidp->chip_label = 5702;
1992 		dev_ok = B_TRUE;
1993 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
1994 		cidp->pci_type = BGE_PCI;
1995 		break;
1996 
1997 	case DEVICE_ID_5703C:
1998 	case DEVICE_ID_5703S:
1999 	case DEVICE_ID_5703:
2000 		/*
2001 		 * Revision A0 of the 5703/5793 had various errata
2002 		 * that we can't or don't work around, so it's not
2003 		 * supported, but all later versions are
2004 		 */
2005 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
2006 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
2007 			dev_ok = B_TRUE;
2008 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2009 		break;
2010 
2011 	case DEVICE_ID_5704C:
2012 	case DEVICE_ID_5704S:
2013 	case DEVICE_ID_5704:
2014 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
2015 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
2016 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
2017 		dev_ok = B_TRUE;
2018 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2019 		break;
2020 
2021 	case DEVICE_ID_5705C:
2022 	case DEVICE_ID_5705M:
2023 	case DEVICE_ID_5705MA3:
2024 	case DEVICE_ID_5705F:
2025 	case DEVICE_ID_5705_2:
2026 	case DEVICE_ID_5754:
2027 		if (cidp->device == DEVICE_ID_5754) {
2028 			cidp->chip_label = 5754;
2029 			cidp->pci_type = BGE_PCI_E;
2030 		} else {
2031 			cidp->chip_label = 5705;
2032 			cidp->pci_type = BGE_PCI;
2033 		}
2034 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2035 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2036 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2037 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2038 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2039 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2040 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2041 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2042 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2043 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2044 		cidp->statistic_type = BGE_STAT_REG;
2045 		dev_ok = B_TRUE;
2046 		break;
2047 
2048 	case DEVICE_ID_5906:
2049 	case DEVICE_ID_5906M:
2050 		cidp->chip_label = 5906;
2051 		cidp->pci_type = BGE_PCI_E;
2052 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5906;
2053 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5906;
2054 		cidp->mbuf_hi_water = MBUF_HIWAT_5906;
2055 		cidp->mbuf_base = bge_mbuf_pool_base;
2056 		cidp->mbuf_length = bge_mbuf_pool_len;
2057 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2058 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2059 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2060 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2061 		cidp->statistic_type = BGE_STAT_REG;
2062 		dev_ok = B_TRUE;
2063 		break;
2064 
2065 	case DEVICE_ID_5753:
2066 		cidp->chip_label = 5753;
2067 		cidp->pci_type = BGE_PCI_E;
2068 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2069 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2070 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2071 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2072 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2073 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2074 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2075 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2076 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2077 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2078 		cidp->statistic_type = BGE_STAT_REG;
2079 		dev_ok = B_TRUE;
2080 		break;
2081 
2082 	case DEVICE_ID_5755:
2083 	case DEVICE_ID_5755M:
2084 		cidp->chip_label = 5755;
2085 		cidp->pci_type = BGE_PCI_E;
2086 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2087 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2088 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2089 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2090 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2091 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2092 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2093 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2094 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2095 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2096 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2097 		cidp->statistic_type = BGE_STAT_REG;
2098 		dev_ok = B_TRUE;
2099 		break;
2100 
2101 	case DEVICE_ID_5756M:
2102 		/*
2103 		 * This is nearly identical to the 5755M.
2104 		 * (Actually reports the 5755 chip ID.)
2105 		 */
2106 		cidp->chip_label = 5756;
2107 		cidp->pci_type = BGE_PCI_E;
2108 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2109 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2110 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2111 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2112 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2113 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2114 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2115 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2116 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2117 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2118 		cidp->statistic_type = BGE_STAT_REG;
2119 		dev_ok = B_TRUE;
2120 		break;
2121 
2122 	case DEVICE_ID_5787:
2123 	case DEVICE_ID_5787M:
2124 		cidp->chip_label = 5787;
2125 		cidp->pci_type = BGE_PCI_E;
2126 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2127 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2128 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2129 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2130 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2131 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2132 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2133 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2134 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2135 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2136 		cidp->statistic_type = BGE_STAT_REG;
2137 		dev_ok = B_TRUE;
2138 		break;
2139 
2140 	case DEVICE_ID_5723:
2141 	case DEVICE_ID_5761:
2142 	case DEVICE_ID_5761E:
2143 		cidp->chip_label = cidp->device == DEVICE_ID_5723 ? 5723: 5761;
2144 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
2145 		cidp->msi_enabled = bge_enable_msi;
2146 		cidp->pci_type = BGE_PCI_E;
2147 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2148 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2149 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2150 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2151 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2152 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2153 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2154 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2155 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2156 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2157 		cidp->statistic_type = BGE_STAT_REG;
2158 		dev_ok = B_TRUE;
2159 		break;
2160 
2161 	/* PCI-X device, identical to 5714 */
2162 	case DEVICE_ID_5780:
2163 		cidp->chip_label = 5780;
2164 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2165 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2166 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2167 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2168 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2169 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2170 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2171 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2172 		cidp->statistic_type = BGE_STAT_REG;
2173 		dev_ok = B_TRUE;
2174 		break;
2175 
2176 	case DEVICE_ID_5782:
2177 		/*
2178 		 * Apart from the label, we treat this as a 5705(?)
2179 		 */
2180 		cidp->chip_label = 5782;
2181 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2182 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2183 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2184 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2185 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2186 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2187 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2188 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2189 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2190 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2191 		cidp->statistic_type = BGE_STAT_REG;
2192 		dev_ok = B_TRUE;
2193 		break;
2194 
2195 	case DEVICE_ID_5788:
2196 		/*
2197 		 * Apart from the label, we treat this as a 5705(?)
2198 		 */
2199 		cidp->chip_label = 5788;
2200 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2201 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2202 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2203 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2204 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2205 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2206 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2207 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2208 		cidp->statistic_type = BGE_STAT_REG;
2209 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2210 		dev_ok = B_TRUE;
2211 		break;
2212 
2213 	case DEVICE_ID_5714C:
2214 		if (cidp->revision >= REVISION_ID_5714_A2)
2215 			cidp->msi_enabled = bge_enable_msi;
2216 		/* FALLTHRU */
2217 	case DEVICE_ID_5714S:
2218 		cidp->chip_label = 5714;
2219 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2220 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2221 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2222 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2223 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2224 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2225 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
2226 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
2227 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2228 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2229 		cidp->pci_type = BGE_PCI_E;
2230 		cidp->statistic_type = BGE_STAT_REG;
2231 		dev_ok = B_TRUE;
2232 		break;
2233 
2234 	case DEVICE_ID_5715C:
2235 	case DEVICE_ID_5715S:
2236 		cidp->chip_label = 5715;
2237 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2238 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2239 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2240 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2241 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2242 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2243 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
2244 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
2245 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2246 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2247 		cidp->pci_type = BGE_PCI_E;
2248 		cidp->statistic_type = BGE_STAT_REG;
2249 		if (cidp->revision >= REVISION_ID_5715_A2)
2250 			cidp->msi_enabled = bge_enable_msi;
2251 		dev_ok = B_TRUE;
2252 		break;
2253 
2254 	case DEVICE_ID_5721:
2255 		cidp->chip_label = 5721;
2256 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2257 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2258 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2259 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2260 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2261 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2262 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
2263 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2264 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2265 		cidp->pci_type = BGE_PCI_E;
2266 		cidp->statistic_type = BGE_STAT_REG;
2267 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2268 		dev_ok = B_TRUE;
2269 		break;
2270 
2271 	case DEVICE_ID_5722:
2272 		cidp->chip_label = 5722;
2273 		cidp->pci_type = BGE_PCI_E;
2274 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2275 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2276 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2277 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
2278 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
2279 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
2280 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
2281 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2282 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2283 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2284 		cidp->statistic_type = BGE_STAT_REG;
2285 		dev_ok = B_TRUE;
2286 		break;
2287 
2288 	case DEVICE_ID_5751:
2289 	case DEVICE_ID_5751M:
2290 		cidp->chip_label = 5751;
2291 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2292 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2293 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2294 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2295 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2296 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2297 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
2298 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2299 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2300 		cidp->pci_type = BGE_PCI_E;
2301 		cidp->statistic_type = BGE_STAT_REG;
2302 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2303 		dev_ok = B_TRUE;
2304 		break;
2305 
2306 	case DEVICE_ID_5752:
2307 	case DEVICE_ID_5752M:
2308 		cidp->chip_label = 5752;
2309 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
2310 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
2311 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
2312 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2313 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2314 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2315 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
2316 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2317 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
2318 		cidp->pci_type = BGE_PCI_E;
2319 		cidp->statistic_type = BGE_STAT_REG;
2320 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2321 		dev_ok = B_TRUE;
2322 		break;
2323 
2324 	case DEVICE_ID_5789:
2325 		cidp->chip_label = 5789;
2326 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
2327 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
2328 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
2329 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
2330 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
2331 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
2332 		cidp->pci_type = BGE_PCI_E;
2333 		cidp->statistic_type = BGE_STAT_REG;
2334 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
2335 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
2336 		cidp->msi_enabled = B_TRUE;
2337 		dev_ok = B_TRUE;
2338 		break;
2339 
2340 	}
2341 
2342 	/*
2343 	 * Setup the default jumbo parameter.
2344 	 */
2345 	cidp->ethmax_size = ETHERMAX;
2346 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
2347 	cidp->std_buf_size = BGE_STD_BUFF_SIZE;
2348 
2349 	/*
2350 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
2351 	 * setup below jumbo specific parameters.
2352 	 *
2353 	 * For BCM5714/5715, there is only one standard receive ring. So the
2354 	 * std buffer size should be set to BGE_JUMBO_BUFF_SIZE when jumbo
2355 	 * feature is enabled.
2356 	 */
2357 	if (!(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
2358 	    (cidp->default_mtu > BGE_DEFAULT_MTU) &&
2359 	    (cidp->default_mtu <= BGE_MAXIMUM_MTU)) {
2360 		if (DEVICE_5714_SERIES_CHIPSETS(bgep)) {
2361 			cidp->mbuf_lo_water_rdma =
2362 			    RDMA_MBUF_LOWAT_5714_JUMBO;
2363 			cidp->mbuf_lo_water_rmac =
2364 			    MAC_RX_MBUF_LOWAT_5714_JUMBO;
2365 			cidp->mbuf_hi_water = MBUF_HIWAT_5714_JUMBO;
2366 			cidp->jumbo_slots = 0;
2367 			cidp->std_buf_size = BGE_JUMBO_BUFF_SIZE;
2368 		} else {
2369 			cidp->mbuf_lo_water_rdma =
2370 			    RDMA_MBUF_LOWAT_JUMBO;
2371 			cidp->mbuf_lo_water_rmac =
2372 			    MAC_RX_MBUF_LOWAT_JUMBO;
2373 			cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
2374 			cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
2375 		}
2376 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
2377 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
2378 		cidp->ethmax_size = cidp->default_mtu +
2379 		    sizeof (struct ether_header);
2380 	}
2381 
2382 	/*
2383 	 * Identify the NV memory type: SEEPROM or Flash?
2384 	 */
2385 	cidp->nvtype = bge_nvmem_id(bgep);
2386 
2387 	/*
2388 	 * Now, we want to check whether this device is part of a
2389 	 * supported subsystem (e.g., on the motherboard of a Sun
2390 	 * branded platform).
2391 	 *
2392 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
2393 	 */
2394 	if (cidp->subven == VENDOR_ID_SUN)
2395 		sys_ok = B_TRUE;
2396 
2397 	/*
2398 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
2399 	 * Note: 0x14e41647 should *not* appear in the list, but the code
2400 	 * doesn't enforce that.
2401 	 */
2402 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
2403 	    DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
2404 	if (err == DDI_PROP_SUCCESS) {
2405 		/*
2406 		 * Got the list; scan for a matching subsystem vendor/device
2407 		 */
2408 		subid = (cidp->subven << 16) | cidp->subdev;
2409 		while (i--)
2410 			if (ids[i] == subid)
2411 				sys_ok = B_TRUE;
2412 		ddi_prop_free(ids);
2413 	}
2414 
2415 	/*
2416 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
2417 	 *
2418 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
2419 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
2420 	 * we have to check specially for the exact device paths to the
2421 	 * motherboard devices on those platforms ;-(
2422 	 *
2423 	 * Note: we can't just use the "supported-subsystems" mechanism
2424 	 * above, because the entry would have to be 0x14e41647 -- which
2425 	 * would then accept *any* plugin card that *didn't* contain a
2426 	 * (valid) SEEPROM ;-(
2427 	 */
2428 	sysname = ddi_node_name(ddi_root_node());
2429 	devname = ddi_pathname(bgep->devinfo, buf);
2430 	ASSERT(strlen(devname) > 0);
2431 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
2432 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
2433 			sys_ok = B_TRUE;
2434 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
2435 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
2436 			sys_ok = B_TRUE;
2437 
2438 	/*
2439 	 * Now check what we've discovered: is this truly a supported
2440 	 * chip on (the motherboard of) a supported platform?
2441 	 *
2442 	 * Possible problems here:
2443 	 * 1)	it's a completely unheard-of chip
2444 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
2445 	 * 3)	it's a chip we would support if it were on the motherboard
2446 	 *	of a Sun platform, but this one isn't ;-(
2447 	 */
2448 	if (cidp->chip_label == 0)
2449 		bge_problem(bgep,
2450 		    "Device 'pci%04x,%04x' not recognized (%d?)",
2451 		    cidp->vendor, cidp->device, cidp->device);
2452 	else if (!dev_ok)
2453 		bge_problem(bgep,
2454 		    "Device 'pci%04x,%04x' (%d) revision %d not supported",
2455 		    cidp->vendor, cidp->device, cidp->chip_label,
2456 		    cidp->revision);
2457 #if	BGE_DEBUGGING
2458 	else if (!sys_ok)
2459 		bge_problem(bgep,
2460 		    "%d-based subsystem 'pci%04x,%04x' not validated",
2461 		    cidp->chip_label, cidp->subven, cidp->subdev);
2462 #endif
2463 	else
2464 		cidp->flags |= CHIP_FLAG_SUPPORTED;
2465 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
2466 		return (EIO);
2467 	return (0);
2468 }
2469 
2470 void
2471 bge_chip_msi_trig(bge_t *bgep)
2472 {
2473 	uint32_t	regval;
2474 
2475 	regval = bgep->param_msi_cnt<<4;
2476 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
2477 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
2478 }
2479 
2480 /*
2481  * Various registers that control the chip's internal engines (state
2482  * machines) have a <reset> and <enable> bits (fortunately, in the
2483  * same place in each such register :-).
2484  *
2485  * To reset the state machine, the <reset> bit must be written with 1;
2486  * it will then read back as 1 while the reset is in progress, but
2487  * self-clear to 0 when the reset completes.
2488  *
2489  * To enable a state machine, one must set the <enable> bit, which
2490  * will continue to read back as 0 until the state machine is running.
2491  *
2492  * To disable a state machine, the <enable> bit must be cleared, but
2493  * it will continue to read back as 1 until the state machine actually
2494  * stops.
2495  *
2496  * This routine implements polling for completion of a reset, enable
2497  * or disable operation, returning B_TRUE on success (bit reached the
2498  * required state) or B_FALSE on timeout (200*100us == 20ms).
2499  */
2500 static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
2501 					uint32_t mask, uint32_t val);
2502 #pragma	no_inline(bge_chip_poll_engine)
2503 
2504 static boolean_t
2505 bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
2506 	uint32_t mask, uint32_t val)
2507 {
2508 	uint32_t regval;
2509 	uint32_t n;
2510 
2511 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
2512 	    (void *)bgep, regno, mask, val));
2513 
2514 	for (n = 200; n; --n) {
2515 		regval = bge_reg_get32(bgep, regno);
2516 		if ((regval & mask) == val)
2517 			return (B_TRUE);
2518 		drv_usecwait(100);
2519 	}
2520 
2521 	bge_fm_ereport(bgep, DDI_FM_DEVICE_NO_RESPONSE);
2522 	return (B_FALSE);
2523 }
2524 
2525 /*
2526  * Various registers that control the chip's internal engines (state
2527  * machines) have a <reset> bit (fortunately, in the same place in
2528  * each such register :-).  To reset the state machine, this bit must
2529  * be written with 1; it will then read back as 1 while the reset is
2530  * in progress, but self-clear to 0 when the reset completes.
2531  *
2532  * This code sets the bit, then polls for it to read back as zero.
2533  * The return value is B_TRUE on success (reset bit cleared itself),
2534  * or B_FALSE if the state machine didn't recover :(
2535  *
2536  * NOTE: the Core reset is similar to other resets, except that we
2537  * can't poll for completion, since the Core reset disables memory
2538  * access!  So we just have to assume that it will all complete in
2539  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
2540  */
2541 static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
2542 #pragma	no_inline(bge_chip_reset_engine)
2543 
2544 static boolean_t
2545 bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
2546 {
2547 	uint32_t regval;
2548 	uint32_t val32;
2549 
2550 	regval = bge_reg_get32(bgep, regno);
2551 
2552 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
2553 	    (void *)bgep, regno));
2554 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
2555 	    regno, regval));
2556 
2557 	regval |= STATE_MACHINE_RESET_BIT;
2558 
2559 	switch (regno) {
2560 	case MISC_CONFIG_REG:
2561 		/*
2562 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
2563 		 * resetting PCIE block and bringing PCIE link down, bit 29
2564 		 * in the register needs to be set first, and then set it again
2565 		 * while the reset bit is written.
2566 		 * See:P500 of 57xx-PG102-RDS.pdf.
2567 		 */
2568 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
2569 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
2570 		    DEVICE_5723_SERIES_CHIPSETS(bgep)||
2571 		    DEVICE_5714_SERIES_CHIPSETS(bgep)||
2572 		    DEVICE_5906_SERIES_CHIPSETS(bgep)) {
2573 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
2574 			if (bgep->chipid.pci_type == BGE_PCI_E) {
2575 				if (bgep->chipid.asic_rev ==
2576 				    MHCR_CHIP_REV_5751_A0 ||
2577 				    bgep->chipid.asic_rev ==
2578 				    MHCR_CHIP_REV_5721_A0 ||
2579 				    bgep->chipid.asic_rev ==
2580 				    MHCR_CHIP_REV_5755_A0) {
2581 					val32 = bge_reg_get32(bgep,
2582 					    PHY_TEST_CTRL_REG);
2583 					if (val32 == (PHY_PCIE_SCRAM_MODE |
2584 					    PHY_PCIE_LTASS_MODE))
2585 						bge_reg_put32(bgep,
2586 						    PHY_TEST_CTRL_REG,
2587 						    PHY_PCIE_SCRAM_MODE);
2588 					val32 = pci_config_get32
2589 					    (bgep->cfg_handle,
2590 					    PCI_CONF_BGE_CLKCTL);
2591 					val32 |= CLKCTL_PCIE_A0_FIX;
2592 					pci_config_put32(bgep->cfg_handle,
2593 					    PCI_CONF_BGE_CLKCTL, val32);
2594 				}
2595 				bge_reg_set32(bgep, regno,
2596 				    MISC_CONFIG_GRC_RESET_DISABLE);
2597 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
2598 			}
2599 		}
2600 
2601 		/*
2602 		 * Special case - causes Core reset
2603 		 *
2604 		 * On SPARC v9 we want to ensure that we don't start
2605 		 * timing until the I/O access has actually reached
2606 		 * the chip, otherwise we might make the next access
2607 		 * too early.  And we can't just force the write out
2608 		 * by following it with a read (even to config space)
2609 		 * because that would cause the fault we're trying
2610 		 * to avoid.  Hence the need for membar_sync() here.
2611 		 */
2612 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
2613 #ifdef	__sparcv9
2614 		membar_sync();
2615 #endif	/* __sparcv9 */
2616 		/*
2617 		 * On some platforms,system need about 300us for
2618 		 * link setup.
2619 		 */
2620 		drv_usecwait(300);
2621 		if (DEVICE_5906_SERIES_CHIPSETS(bgep)) {
2622 			bge_reg_set32(bgep, VCPU_STATUS_REG, VCPU_DRV_RESET);
2623 			bge_reg_clr32(
2624 			    bgep, VCPU_EXT_CTL, VCPU_EXT_CTL_HALF);
2625 		}
2626 
2627 		if (bgep->chipid.pci_type == BGE_PCI_E) {
2628 			/* PCI-E device need more reset time */
2629 			drv_usecwait(120000);
2630 
2631 			/* Set PCIE max payload size and clear error status. */
2632 			if ((bgep->chipid.chip_label == 5721) ||
2633 			    (bgep->chipid.chip_label == 5751) ||
2634 			    (bgep->chipid.chip_label == 5752) ||
2635 			    (bgep->chipid.chip_label == 5789) ||
2636 			    (bgep->chipid.chip_label == 5906)) {
2637 				pci_config_put16(bgep->cfg_handle,
2638 				    PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
2639 				pci_config_put16(bgep->cfg_handle,
2640 				    PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
2641 			}
2642 
2643 			if ((bgep->chipid.chip_label == 5723) ||
2644 			    (bgep->chipid.chip_label == 5761)) {
2645 				pci_config_put16(bgep->cfg_handle,
2646 				    PCI_CONF_DEV_CTRL_5723, READ_REQ_SIZE_MAX);
2647 				pci_config_put16(bgep->cfg_handle,
2648 				    PCI_CONF_DEV_STUS_5723, DEVICE_ERROR_STUS);
2649 			}
2650 		}
2651 
2652 		BGE_PCICHK(bgep);
2653 		return (B_TRUE);
2654 
2655 	default:
2656 		bge_reg_put32(bgep, regno, regval);
2657 		return (bge_chip_poll_engine(bgep, regno,
2658 		    STATE_MACHINE_RESET_BIT, 0));
2659 	}
2660 }
2661 
2662 /*
2663  * Various registers that control the chip's internal engines (state
2664  * machines) have an <enable> bit (fortunately, in the same place in
2665  * each such register :-).  To stop the state machine, this bit must
2666  * be written with 0, then polled to see when the state machine has
2667  * actually stopped.
2668  *
2669  * The return value is B_TRUE on success (enable bit cleared), or
2670  * B_FALSE if the state machine didn't stop :(
2671  */
2672 static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
2673 						uint32_t morebits);
2674 #pragma	no_inline(bge_chip_disable_engine)
2675 
2676 static boolean_t
2677 bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
2678 {
2679 	uint32_t regval;
2680 
2681 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
2682 	    (void *)bgep, regno, morebits));
2683 
2684 	switch (regno) {
2685 	case FTQ_RESET_REG:
2686 		/*
2687 		 * For Schumacher's bugfix CR6490108
2688 		 */
2689 #ifdef BGE_IPMI_ASF
2690 #ifdef BGE_NETCONSOLE
2691 		if (bgep->asf_enabled)
2692 			return (B_TRUE);
2693 #endif
2694 #endif
2695 		/*
2696 		 * Not quite like the others; it doesn't
2697 		 * have an <enable> bit, but instead we
2698 		 * have to set and then clear all the bits
2699 		 */
2700 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
2701 		drv_usecwait(100);
2702 		bge_reg_put32(bgep, regno, 0);
2703 		return (B_TRUE);
2704 
2705 	default:
2706 		regval = bge_reg_get32(bgep, regno);
2707 		regval &= ~STATE_MACHINE_ENABLE_BIT;
2708 		regval &= ~morebits;
2709 		bge_reg_put32(bgep, regno, regval);
2710 		return (bge_chip_poll_engine(bgep, regno,
2711 		    STATE_MACHINE_ENABLE_BIT, 0));
2712 	}
2713 }
2714 
2715 /*
2716  * Various registers that control the chip's internal engines (state
2717  * machines) have an <enable> bit (fortunately, in the same place in
2718  * each such register :-).  To start the state machine, this bit must
2719  * be written with 1, then polled to see when the state machine has
2720  * actually started.
2721  *
2722  * The return value is B_TRUE on success (enable bit set), or
2723  * B_FALSE if the state machine didn't start :(
2724  */
2725 static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
2726 					uint32_t morebits);
2727 #pragma	no_inline(bge_chip_enable_engine)
2728 
2729 static boolean_t
2730 bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
2731 {
2732 	uint32_t regval;
2733 
2734 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
2735 	    (void *)bgep, regno, morebits));
2736 
2737 	switch (regno) {
2738 	case FTQ_RESET_REG:
2739 #ifdef BGE_IPMI_ASF
2740 #ifdef BGE_NETCONSOLE
2741 		if (bgep->asf_enabled)
2742 			return (B_TRUE);
2743 #endif
2744 #endif
2745 		/*
2746 		 * Not quite like the others; it doesn't
2747 		 * have an <enable> bit, but instead we
2748 		 * have to set and then clear all the bits
2749 		 */
2750 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
2751 		drv_usecwait(100);
2752 		bge_reg_put32(bgep, regno, 0);
2753 		return (B_TRUE);
2754 
2755 	default:
2756 		regval = bge_reg_get32(bgep, regno);
2757 		regval |= STATE_MACHINE_ENABLE_BIT;
2758 		regval |= morebits;
2759 		bge_reg_put32(bgep, regno, regval);
2760 		return (bge_chip_poll_engine(bgep, regno,
2761 		    STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
2762 	}
2763 }
2764 
2765 /*
2766  * Reprogram the Ethernet, Transmit, and Receive MAC
2767  * modes to match the param_* variables
2768  */
2769 void bge_sync_mac_modes(bge_t *bgep);
2770 #pragma	no_inline(bge_sync_mac_modes)
2771 
2772 void
2773 bge_sync_mac_modes(bge_t *bgep)
2774 {
2775 	uint32_t macmode;
2776 	uint32_t regval;
2777 
2778 	ASSERT(mutex_owned(bgep->genlock));
2779 
2780 	/*
2781 	 * Reprogram the Ethernet MAC mode ...
2782 	 */
2783 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
2784 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
2785 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
2786 		if (DEVICE_5714_SERIES_CHIPSETS(bgep))
2787 			macmode |= ETHERNET_MODE_LINK_POLARITY;
2788 		else
2789 			macmode &= ~ETHERNET_MODE_LINK_POLARITY;
2790 	else
2791 		macmode |= ETHERNET_MODE_LINK_POLARITY;
2792 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
2793 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
2794 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC)) {
2795 		if (DEVICE_5714_SERIES_CHIPSETS(bgep))
2796 			macmode |= ETHERNET_MODE_PORTMODE_GMII;
2797 		else
2798 			macmode |= ETHERNET_MODE_PORTMODE_TBI;
2799 	} else if (bgep->param_link_speed == 10 ||
2800 	    bgep->param_link_speed == 100)
2801 		macmode |= ETHERNET_MODE_PORTMODE_MII;
2802 	else
2803 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
2804 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
2805 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
2806 	else
2807 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
2808 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
2809 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
2810 	else
2811 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
2812 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
2813 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
2814 	    (void *)bgep, regval, macmode));
2815 
2816 	/*
2817 	 * ... the Transmit MAC mode ...
2818 	 */
2819 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
2820 	if (bgep->param_link_tx_pause)
2821 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
2822 	else
2823 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
2824 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
2825 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
2826 	    (void *)bgep, regval, macmode));
2827 
2828 	/*
2829 	 * ... and the Receive MAC mode
2830 	 */
2831 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
2832 	if (bgep->param_link_rx_pause)
2833 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
2834 	else
2835 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
2836 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
2837 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
2838 	    (void *)bgep, regval, macmode));
2839 }
2840 
2841 /*
2842  * bge_chip_sync() -- program the chip with the unicast MAC address,
2843  * the multicast hash table, the required level of promiscuity, and
2844  * the current loopback mode ...
2845  */
2846 #ifdef BGE_IPMI_ASF
2847 int bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
2848 #else
2849 int bge_chip_sync(bge_t *bgep);
2850 #endif
2851 #pragma	no_inline(bge_chip_sync)
2852 
2853 int
2854 #ifdef BGE_IPMI_ASF
2855 bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
2856 #else
2857 bge_chip_sync(bge_t *bgep)
2858 #endif
2859 {
2860 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
2861 	boolean_t promisc;
2862 	uint64_t macaddr;
2863 	uint32_t fill = 0;
2864 	int i, j;
2865 	int retval = DDI_SUCCESS;
2866 
2867 	BGE_TRACE(("bge_chip_sync($%p)",
2868 	    (void *)bgep));
2869 
2870 	ASSERT(mutex_owned(bgep->genlock));
2871 
2872 	promisc = B_FALSE;
2873 	fill = ~(uint32_t)0;
2874 
2875 	if (bgep->promisc)
2876 		promisc = B_TRUE;
2877 	else
2878 		fill = (uint32_t)0;
2879 
2880 	/*
2881 	 * If the TX/RX MAC engines are already running, we should stop
2882 	 * them (and reset the RX engine) before changing the parameters.
2883 	 * If they're not running, this will have no effect ...
2884 	 *
2885 	 * NOTE: this is currently disabled by default because stopping
2886 	 * and restarting the Tx engine may cause an outgoing packet in
2887 	 * transit to be truncated.  Also, stopping and restarting the
2888 	 * Rx engine seems to not work correctly on the 5705.  Testing
2889 	 * has not (yet!) revealed any problems with NOT stopping and
2890 	 * restarting these engines (and Broadcom say their drivers don't
2891 	 * do this), but if it is found to cause problems, this variable
2892 	 * can be patched to re-enable the old behaviour ...
2893 	 */
2894 	if (bge_stop_start_on_sync) {
2895 #ifdef BGE_IPMI_ASF
2896 		if (!bgep->asf_enabled) {
2897 			if (!bge_chip_disable_engine(bgep,
2898 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
2899 				retval = DDI_FAILURE;
2900 		} else {
2901 			if (!bge_chip_disable_engine(bgep,
2902 			    RECEIVE_MAC_MODE_REG, 0))
2903 				retval = DDI_FAILURE;
2904 		}
2905 #else
2906 		if (!bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
2907 		    RECEIVE_MODE_KEEP_VLAN_TAG))
2908 			retval = DDI_FAILURE;
2909 #endif
2910 		if (!bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
2911 			retval = DDI_FAILURE;
2912 		if (!bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG))
2913 			retval = DDI_FAILURE;
2914 	}
2915 
2916 	/*
2917 	 * Reprogram the hashed multicast address table ...
2918 	 */
2919 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
2920 		bge_reg_put32(bgep, MAC_HASH_REG(i), 0);
2921 
2922 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
2923 		bge_reg_put32(bgep, MAC_HASH_REG(i),
2924 			bgep->mcast_hash[i] | fill);
2925 
2926 #ifdef BGE_IPMI_ASF
2927 	if (!bgep->asf_enabled || !asf_keeplive) {
2928 #endif
2929 		/*
2930 		 * Transform the MAC address(es) from host to chip format, then
2931 		 * reprogram the transmit random backoff seed and the unicast
2932 		 * MAC address(es) ...
2933 		 */
2934 		for (j = 0; j < MAC_ADDRESS_REGS_MAX; j++) {
2935 			for (i = 0, macaddr = 0ull;
2936 			    i < ETHERADDRL; ++i) {
2937 				macaddr <<= 8;
2938 				macaddr |= bgep->curr_addr[j].addr[i];
2939 			}
2940 			fill += (macaddr >> 16) + (macaddr & 0xffffffff);
2941 			bge_reg_put64(bgep, MAC_ADDRESS_REG(j), macaddr);
2942 
2943 			BGE_DEBUG(("bge_chip_sync($%p) "
2944 			    "setting MAC address %012llx",
2945 			    (void *)bgep, macaddr));
2946 		}
2947 #ifdef BGE_IPMI_ASF
2948 	}
2949 #endif
2950 	/*
2951 	 * Set random seed of backoff interval
2952 	 *   - Writing zero means no backoff interval
2953 	 */
2954 	fill = ((fill >> 20) + (fill >> 10) + fill) & 0x3ff;
2955 	if (fill == 0)
2956 		fill = 1;
2957 	bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
2958 
2959 	/*
2960 	 * Set or clear the PROMISCUOUS mode bit
2961 	 */
2962 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
2963 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
2964 
2965 	/*
2966 	 * Sync the rest of the MAC modes too ...
2967 	 */
2968 	bge_sync_mac_modes(bgep);
2969 
2970 	/*
2971 	 * Restart RX/TX MAC engines if required ...
2972 	 */
2973 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
2974 		if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
2975 			retval = DDI_FAILURE;
2976 #ifdef BGE_IPMI_ASF
2977 		if (!bgep->asf_enabled) {
2978 			if (!bge_chip_enable_engine(bgep,
2979 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
2980 				retval = DDI_FAILURE;
2981 		} else {
2982 			if (!bge_chip_enable_engine(bgep,
2983 			    RECEIVE_MAC_MODE_REG, 0))
2984 				retval = DDI_FAILURE;
2985 		}
2986 #else
2987 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
2988 		    RECEIVE_MODE_KEEP_VLAN_TAG))
2989 			retval = DDI_FAILURE;
2990 #endif
2991 	}
2992 	return (retval);
2993 }
2994 
2995 /*
2996  * This array defines the sequence of state machine control registers
2997  * in which the <enable> bit must be cleared to bring the chip to a
2998  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
2999  */
3000 static bge_regno_t shutdown_engine_regs[] = {
3001 	RECEIVE_MAC_MODE_REG,
3002 	RCV_BD_INITIATOR_MODE_REG,
3003 	RCV_LIST_PLACEMENT_MODE_REG,
3004 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
3005 	RCV_DATA_BD_INITIATOR_MODE_REG,
3006 	RCV_DATA_COMPLETION_MODE_REG,
3007 	RCV_BD_COMPLETION_MODE_REG,
3008 
3009 	SEND_BD_SELECTOR_MODE_REG,
3010 	SEND_BD_INITIATOR_MODE_REG,
3011 	SEND_DATA_INITIATOR_MODE_REG,
3012 	READ_DMA_MODE_REG,
3013 	SEND_DATA_COMPLETION_MODE_REG,
3014 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
3015 	SEND_BD_COMPLETION_MODE_REG,
3016 	TRANSMIT_MAC_MODE_REG,
3017 
3018 	HOST_COALESCE_MODE_REG,
3019 	WRITE_DMA_MODE_REG,
3020 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
3021 	FTQ_RESET_REG,		/* special - see code	*/
3022 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
3023 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
3024 	BGE_REGNO_NONE		/* terminator		*/
3025 };
3026 
3027 #ifndef __sparc
3028 static bge_regno_t quiesce_regs[] = {
3029 	READ_DMA_MODE_REG,
3030 	DMA_COMPLETION_MODE_REG,
3031 	WRITE_DMA_MODE_REG,
3032 	BGE_REGNO_NONE
3033 };
3034 
3035 void bge_chip_stop_nonblocking(bge_t *bgep);
3036 #pragma no_inline(bge_chip_stop_nonblocking)
3037 
3038 /*
3039  * This function is called by bge_quiesce(). We
3040  * turn off all the DMA engines here.
3041  */
3042 void
3043 bge_chip_stop_nonblocking(bge_t *bgep)
3044 {
3045 	bge_regno_t *rbp;
3046 
3047 	/*
3048 	 * Flag that no more activity may be initiated
3049 	 */
3050 	bgep->progress &= ~PROGRESS_READY;
3051 
3052 	rbp = quiesce_regs;
3053 	while (*rbp != BGE_REGNO_NONE) {
3054 		(void) bge_chip_disable_engine(bgep, *rbp, 0);
3055 		++rbp;
3056 	}
3057 
3058 	bgep->bge_chip_state = BGE_CHIP_STOPPED;
3059 }
3060 
3061 #endif
3062 
3063 /*
3064  * bge_chip_stop() -- stop all chip processing
3065  *
3066  * If the <fault> parameter is B_TRUE, we're stopping the chip because
3067  * we've detected a problem internally; otherwise, this is a normal
3068  * (clean) stop (at user request i.e. the last STREAM has been closed).
3069  */
3070 void bge_chip_stop(bge_t *bgep, boolean_t fault);
3071 #pragma	no_inline(bge_chip_stop)
3072 
3073 void
3074 bge_chip_stop(bge_t *bgep, boolean_t fault)
3075 {
3076 	bge_regno_t regno;
3077 	bge_regno_t *rbp;
3078 	boolean_t ok;
3079 
3080 	BGE_TRACE(("bge_chip_stop($%p)",
3081 	    (void *)bgep));
3082 
3083 	ASSERT(mutex_owned(bgep->genlock));
3084 
3085 	rbp = shutdown_engine_regs;
3086 	/*
3087 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
3088 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
3089 	 * -ory arbiter should not be disabled.
3090 	 */
3091 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
3092 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3093 				ok &= bge_chip_disable_engine(bgep, regno, 0);
3094 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
3095 			    (regno != DMA_COMPLETION_MODE_REG) &&
3096 			    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
3097 			    (regno != BUFFER_MANAGER_MODE_REG) &&
3098 			    (regno != MEMORY_ARBITER_MODE_REG))
3099 				ok &= bge_chip_disable_engine(bgep,
3100 				    regno, 0);
3101 	}
3102 
3103 	if (!ok && !fault)
3104 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
3105 
3106 	/*
3107 	 * Finally, disable (all) MAC events & clear the MAC status
3108 	 */
3109 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
3110 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
3111 
3112 	/*
3113 	 * if we're stopping the chip because of a detected fault then do
3114 	 * appropriate actions
3115 	 */
3116 	if (fault) {
3117 		if (bgep->bge_chip_state != BGE_CHIP_FAULT) {
3118 			bgep->bge_chip_state = BGE_CHIP_FAULT;
3119 			if (!bgep->manual_reset)
3120 				ddi_fm_service_impact(bgep->devinfo,
3121 				    DDI_SERVICE_LOST);
3122 			if (bgep->bge_dma_error) {
3123 				/*
3124 				 * need to free buffers in case the fault was
3125 				 * due to a memory error in a buffer - got to
3126 				 * do a fair bit of tidying first
3127 				 */
3128 				if (bgep->progress & PROGRESS_KSTATS) {
3129 					bge_fini_kstats(bgep);
3130 					bgep->progress &= ~PROGRESS_KSTATS;
3131 				}
3132 				if (bgep->progress & PROGRESS_INTR) {
3133 					bge_intr_disable(bgep);
3134 					rw_enter(bgep->errlock, RW_WRITER);
3135 					bge_fini_rings(bgep);
3136 					rw_exit(bgep->errlock);
3137 					bgep->progress &= ~PROGRESS_INTR;
3138 				}
3139 				if (bgep->progress & PROGRESS_BUFS) {
3140 					bge_free_bufs(bgep);
3141 					bgep->progress &= ~PROGRESS_BUFS;
3142 				}
3143 				bgep->bge_dma_error = B_FALSE;
3144 			}
3145 		}
3146 	} else
3147 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
3148 }
3149 
3150 /*
3151  * Poll for completion of chip's ROM firmware; also, at least on the
3152  * first time through, find and return the hardware MAC address, if any.
3153  */
3154 static uint64_t bge_poll_firmware(bge_t *bgep);
3155 #pragma	no_inline(bge_poll_firmware)
3156 
3157 static uint64_t
3158 bge_poll_firmware(bge_t *bgep)
3159 {
3160 	uint64_t magic;
3161 	uint64_t mac;
3162 	uint32_t gen, val;
3163 	uint32_t i;
3164 
3165 	/*
3166 	 * Step 19: poll for firmware completion (GENCOMM port set
3167 	 * to the ones complement of T3_MAGIC_NUMBER).
3168 	 *
3169 	 * While we're at it, we also read the MAC address register;
3170 	 * at some stage the firmware will load this with the
3171 	 * factory-set value.
3172 	 *
3173 	 * When both the magic number and the MAC address are set,
3174 	 * we're done; but we impose a time limit of one second
3175 	 * (1000*1000us) in case the firmware fails in some fashion
3176 	 * or the SEEPROM that provides that MAC address isn't fitted.
3177 	 *
3178 	 * After the first time through (chip state != INITIAL), we
3179 	 * don't need the MAC address to be set (we've already got it
3180 	 * or not, from the first time), so we don't wait for it, but
3181 	 * we still have to wait for the T3_MAGIC_NUMBER.
3182 	 *
3183 	 * Note: the magic number is only a 32-bit quantity, but the NIC
3184 	 * memory is 64-bit (and big-endian) internally.  Addressing the
3185 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
3186 	 * it work correctly on both big- and little-endian hosts.
3187 	 */
3188 	if (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
3189 	    MHCR_CHIP_ASIC_REV_5906) {
3190 		for (i = 0; i < 1000; ++i) {
3191 			drv_usecwait(1000);
3192 			val = bge_reg_get32(bgep, VCPU_STATUS_REG);
3193 			if (val & VCPU_INIT_DONE)
3194 				break;
3195 		}
3196 		BGE_DEBUG(("bge_poll_firmware($%p): return after %d loops",
3197 		    (void *)bgep, i));
3198 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
3199 	} else {
3200 		for (i = 0; i < 1000; ++i) {
3201 			drv_usecwait(1000);
3202 			gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
3203 			if (i == 0 && DEVICE_5704_SERIES_CHIPSETS(bgep))
3204 				drv_usecwait(100000);
3205 			mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
3206 #ifdef BGE_IPMI_ASF
3207 			if (!bgep->asf_enabled) {
3208 #endif
3209 				if (gen != ~T3_MAGIC_NUMBER)
3210 					continue;
3211 #ifdef BGE_IPMI_ASF
3212 			}
3213 #endif
3214 			if (mac != 0ULL)
3215 				break;
3216 			if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
3217 				break;
3218 		}
3219 	}
3220 
3221 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
3222 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
3223 	    (void *)bgep, gen, i));
3224 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
3225 	    mac, magic));
3226 
3227 	return (mac);
3228 }
3229 
3230 /*
3231  * Maximum times of trying to get the NVRAM access lock
3232  * by calling bge_nvmem_acquire()
3233  */
3234 #define	MAX_TRY_NVMEM_ACQUIRE	10000
3235 
3236 #ifdef BGE_IPMI_ASF
3237 int bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
3238 #else
3239 int bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
3240 #endif
3241 #pragma	no_inline(bge_chip_reset)
3242 
3243 int
3244 #ifdef BGE_IPMI_ASF
3245 bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
3246 #else
3247 bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
3248 #endif
3249 {
3250 	chip_id_t chipid;
3251 	uint64_t mac;
3252 	uint64_t magic;
3253 	uint32_t modeflags;
3254 	uint32_t mhcr;
3255 	uint32_t sx0;
3256 	uint32_t i, tries;
3257 #ifdef BGE_IPMI_ASF
3258 	uint32_t mailbox;
3259 #endif
3260 	int retval = DDI_SUCCESS;
3261 
3262 	BGE_TRACE(("bge_chip_reset($%p, %d)",
3263 		(void *)bgep, enable_dma));
3264 
3265 	ASSERT(mutex_owned(bgep->genlock));
3266 
3267 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
3268 		(void *)bgep, enable_dma, bgep->bge_chip_state));
3269 
3270 	/*
3271 	 * Do we need to stop the chip cleanly before resetting?
3272 	 */
3273 	switch (bgep->bge_chip_state) {
3274 	default:
3275 		_NOTE(NOTREACHED)
3276 		return (DDI_FAILURE);
3277 
3278 	case BGE_CHIP_INITIAL:
3279 	case BGE_CHIP_STOPPED:
3280 	case BGE_CHIP_RESET:
3281 		break;
3282 
3283 	case BGE_CHIP_RUNNING:
3284 	case BGE_CHIP_ERROR:
3285 	case BGE_CHIP_FAULT:
3286 		bge_chip_stop(bgep, B_FALSE);
3287 		break;
3288 	}
3289 
3290 #ifdef BGE_IPMI_ASF
3291 	if (bgep->asf_enabled) {
3292 #ifdef __sparc
3293 		mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
3294 			MHCR_ENABLE_TAGGED_STATUS_MODE |
3295 			MHCR_MASK_INTERRUPT_MODE |
3296 			MHCR_MASK_PCI_INT_OUTPUT |
3297 			MHCR_CLEAR_INTERRUPT_INTA |
3298 			MHCR_ENABLE_ENDIAN_WORD_SWAP |
3299 			MHCR_ENABLE_ENDIAN_BYTE_SWAP;
3300 		pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
3301 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
3302 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG) |
3303 			MEMORY_ARBITER_ENABLE);
3304 #endif
3305 		if (asf_mode == ASF_MODE_INIT) {
3306 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
3307 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
3308 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
3309 		}
3310 	}
3311 #endif
3312 	/*
3313 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
3314 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
3315 	 *
3316 	 * Before reset Core clock,it is
3317 	 * also required to initialize the Memory Arbiter as specified in step9
3318 	 * and Misc Host Control Register as specified in step-13
3319 	 * Step 4-5: reset Core clock & wait for completion
3320 	 * Steps 6-8: are done by bge_chip_cfg_init()
3321 	 * put the T3_MAGIC_NUMBER into the GENCOMM port before reset
3322 	 */
3323 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
3324 		retval = DDI_FAILURE;
3325 
3326 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
3327 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
3328 	    MHCR_MASK_INTERRUPT_MODE |
3329 	    MHCR_MASK_PCI_INT_OUTPUT |
3330 	    MHCR_CLEAR_INTERRUPT_INTA;
3331 #ifdef  _BIG_ENDIAN
3332 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
3333 #endif  /* _BIG_ENDIAN */
3334 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
3335 #ifdef BGE_IPMI_ASF
3336 	if (bgep->asf_enabled)
3337 		bgep->asf_wordswapped = B_FALSE;
3338 #endif
3339 	/*
3340 	 * NVRAM Corruption Workaround
3341 	 */
3342 	for (tries = 0; tries < MAX_TRY_NVMEM_ACQUIRE; tries++)
3343 		if (bge_nvmem_acquire(bgep) != EAGAIN)
3344 			break;
3345 	if (tries >= MAX_TRY_NVMEM_ACQUIRE)
3346 		BGE_DEBUG(("%s: fail to acquire nvram lock",
3347 			bgep->ifname));
3348 
3349 #ifdef BGE_IPMI_ASF
3350 	if (!bgep->asf_enabled) {
3351 #endif
3352 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
3353 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
3354 #ifdef BGE_IPMI_ASF
3355 	}
3356 #endif
3357 
3358 	if (!bge_chip_reset_engine(bgep, MISC_CONFIG_REG))
3359 		retval = DDI_FAILURE;
3360 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
3361 
3362 	/*
3363 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
3364 	 * a bit set to avoid a fifo overflow/underflow bug.
3365 	 */
3366 	if ((bgep->chipid.chip_label == 5721) ||
3367 		(bgep->chipid.chip_label == 5751) ||
3368 		(bgep->chipid.chip_label == 5752) ||
3369 		(bgep->chipid.chip_label == 5755) ||
3370 		(bgep->chipid.chip_label == 5756) ||
3371 		(bgep->chipid.chip_label == 5789) ||
3372 		(bgep->chipid.chip_label == 5906))
3373 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
3374 
3375 
3376 	/*
3377 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
3378 	 * not be changed.
3379 	 */
3380 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
3381 		retval = DDI_FAILURE;
3382 
3383 	/*
3384 	 * Steps 10-11: configure PIO endianness options and
3385 	 * enable indirect register access -- already done
3386 	 * Steps 12-13: enable writing to the PCI state & clock
3387 	 * control registers -- not required; we aren't going to
3388 	 * use those features.
3389 	 * Steps 14-15: Configure DMA endianness options.  See
3390 	 * the comments on the setting of the MHCR above.
3391 	 */
3392 #ifdef	_BIG_ENDIAN
3393 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
3394 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
3395 #else
3396 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
3397 #endif	/* _BIG_ENDIAN */
3398 #ifdef BGE_IPMI_ASF
3399 	if (bgep->asf_enabled)
3400 		modeflags |= MODE_HOST_STACK_UP;
3401 #endif
3402 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
3403 
3404 #ifdef BGE_IPMI_ASF
3405 	if (bgep->asf_enabled) {
3406 #ifdef __sparc
3407 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
3408 			MEMORY_ARBITER_ENABLE |
3409 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG));
3410 #endif
3411 
3412 #ifdef  BGE_NETCONSOLE
3413 		if (!bgep->asf_newhandshake) {
3414 			if ((asf_mode == ASF_MODE_INIT) ||
3415 			(asf_mode == ASF_MODE_POST_INIT)) {
3416 				bge_asf_post_reset_old_mode(bgep,
3417 					BGE_INIT_RESET);
3418 			} else {
3419 				bge_asf_post_reset_old_mode(bgep,
3420 					BGE_SHUTDOWN_RESET);
3421 			}
3422 		}
3423 #endif
3424 
3425 		/* Wait for NVRAM init */
3426 		i = 0;
3427 		drv_usecwait(5000);
3428 		mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
3429 
3430 		while ((mailbox != (uint32_t)
3431 			~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
3432 			(i < 10000)) {
3433 			drv_usecwait(100);
3434 			mailbox = bge_nic_get32(bgep,
3435 				BGE_FIRMWARE_MAILBOX);
3436 			i++;
3437 		}
3438 
3439 #ifndef BGE_NETCONSOLE
3440 		if (!bgep->asf_newhandshake) {
3441 			if ((asf_mode == ASF_MODE_INIT) ||
3442 				(asf_mode == ASF_MODE_POST_INIT)) {
3443 
3444 				bge_asf_post_reset_old_mode(bgep,
3445 					BGE_INIT_RESET);
3446 			} else {
3447 				bge_asf_post_reset_old_mode(bgep,
3448 					BGE_SHUTDOWN_RESET);
3449 			}
3450 		}
3451 #endif
3452 	}
3453 #endif
3454 	/*
3455 	 * Steps 16-17: poll for firmware completion
3456 	 */
3457 	mac = bge_poll_firmware(bgep);
3458 
3459 	/*
3460 	 * Step 18: enable external memory -- doesn't apply.
3461 	 *
3462 	 * However we take the opportunity to set the MLCR anyway, as
3463 	 * this register also controls the SEEPROM auto-access method
3464 	 * which we may want to use later ...
3465 	 *
3466 	 * The proper value here depends on the way the chip is wired
3467 	 * into the circuit board, as this register *also* controls which
3468 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
3469 	 * values driven onto those pins!
3470 	 *
3471 	 * See also step 74 in the PRM ...
3472 	 */
3473 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
3474 	    bgep->chipid.bge_mlcr_default);
3475 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
3476 
3477 	/*
3478 	 * Step 20: clear the Ethernet MAC mode register
3479 	 */
3480 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
3481 
3482 	/*
3483 	 * Step 21: restore cache-line-size, latency timer, and
3484 	 * subsystem ID registers to their original values (not
3485 	 * those read into the local structure <chipid>, 'cos
3486 	 * that was after they were cleared by the RESET).
3487 	 *
3488 	 * Note: the Subsystem Vendor/Device ID registers are not
3489 	 * directly writable in config space, so we use the shadow
3490 	 * copy in "Page Zero" of register space to restore them
3491 	 * both in one go ...
3492 	 */
3493 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
3494 		bgep->chipid.clsize);
3495 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
3496 		bgep->chipid.latency);
3497 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
3498 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
3499 
3500 	/*
3501 	 * The SEND INDEX registers should be reset to zero by the
3502 	 * global chip reset; if they're not, there'll be trouble
3503 	 * later on.
3504 	 */
3505 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
3506 	if (sx0 != 0) {
3507 		BGE_REPORT((bgep, "SEND INDEX - device didn't RESET"));
3508 		bge_fm_ereport(bgep, DDI_FM_DEVICE_INVAL_STATE);
3509 		retval = DDI_FAILURE;
3510 	}
3511 
3512 	/* Enable MSI code */
3513 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
3514 		bge_reg_set32(bgep, MSI_MODE_REG,
3515 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE|MSI_ERROR_ATTENTION);
3516 
3517 	/*
3518 	 * On the first time through, save the factory-set MAC address
3519 	 * (if any).  If bge_poll_firmware() above didn't return one
3520 	 * (from a chip register) consider looking in the attached NV
3521 	 * memory device, if any.  Once we have it, we save it in both
3522 	 * register-image (64-bit) and byte-array forms.  All-zero and
3523 	 * all-one addresses are not valid, and we refuse to stash those.
3524 	 */
3525 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
3526 		if (mac == 0ULL)
3527 			mac = bge_get_nvmac(bgep);
3528 		if (mac != 0ULL && mac != ~0ULL) {
3529 			bgep->chipid.hw_mac_addr = mac;
3530 			for (i = ETHERADDRL; i-- != 0; ) {
3531 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
3532 				mac >>= 8;
3533 			}
3534 			bgep->chipid.vendor_addr.set = B_TRUE;
3535 		}
3536 	}
3537 
3538 #ifdef BGE_IPMI_ASF
3539 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
3540 		if (asf_mode != ASF_MODE_NONE) {
3541 			if ((asf_mode == ASF_MODE_INIT) ||
3542 				(asf_mode == ASF_MODE_POST_INIT)) {
3543 
3544 				bge_asf_post_reset_new_mode(bgep,
3545 					BGE_INIT_RESET);
3546 			} else {
3547 				bge_asf_post_reset_new_mode(bgep,
3548 					BGE_SHUTDOWN_RESET);
3549 			}
3550 		}
3551 	}
3552 #endif
3553 
3554 	/*
3555 	 * Record the new state
3556 	 */
3557 	bgep->chip_resets += 1;
3558 	bgep->bge_chip_state = BGE_CHIP_RESET;
3559 	return (retval);
3560 }
3561 
3562 /*
3563  * bge_chip_start() -- start the chip transmitting and/or receiving,
3564  * including enabling interrupts
3565  */
3566 int bge_chip_start(bge_t *bgep, boolean_t reset_phys);
3567 #pragma	no_inline(bge_chip_start)
3568 
3569 void
3570 bge_chip_coalesce_update(bge_t *bgep)
3571 {
3572 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG,
3573 	    bgep->chipid.tx_count_norm);
3574 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG,
3575 	    bgep->chipid.tx_ticks_norm);
3576 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG,
3577 	    bgep->chipid.rx_count_norm);
3578 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG,
3579 	    bgep->chipid.rx_ticks_norm);
3580 }
3581 
3582 int
3583 bge_chip_start(bge_t *bgep, boolean_t reset_phys)
3584 {
3585 	uint32_t coalmode;
3586 	uint32_t ledctl;
3587 	uint32_t mtu;
3588 	uint32_t maxring;
3589 	uint32_t stats_mask;
3590 	uint32_t dma_wrprio;
3591 	uint64_t ring;
3592 	int retval = DDI_SUCCESS;
3593 
3594 	BGE_TRACE(("bge_chip_start($%p)",
3595 	    (void *)bgep));
3596 
3597 	ASSERT(mutex_owned(bgep->genlock));
3598 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
3599 
3600 	/*
3601 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
3602 	 * The document specifies 95 separate steps to fully
3603 	 * initialise the chip!!!!
3604 	 *
3605 	 * The reset code above has already got us as far as step
3606 	 * 21, so we continue with ...
3607 	 *
3608 	 * Step 22: clear the MAC statistics block
3609 	 * (0x0300-0x0aff in NIC-local memory)
3610 	 */
3611 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
3612 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
3613 		    NIC_MEM_STATISTICS_SIZE);
3614 
3615 	/*
3616 	 * Step 23: clear the status block (in host memory)
3617 	 */
3618 	DMA_ZERO(bgep->status_block);
3619 
3620 	/*
3621 	 * Step 24: set DMA read/write control register
3622 	 */
3623 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
3624 	    bgep->chipid.bge_dma_rwctrl);
3625 
3626 	/*
3627 	 * Step 25: Configure DMA endianness -- already done (16/17)
3628 	 * Step 26: Configure Host-Based Send Rings
3629 	 * Step 27: Indicate Host Stack Up
3630 	 */
3631 	bge_reg_set32(bgep, MODE_CONTROL_REG,
3632 	    MODE_HOST_SEND_BDS |
3633 	    MODE_HOST_STACK_UP);
3634 
3635 	/*
3636 	 * Step 28: Configure checksum options:
3637 	 *	Solaris supports the hardware default checksum options.
3638 	 *
3639 	 *	Workaround for Incorrect pseudo-header checksum calculation.
3640 	 */
3641 	if (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)
3642 		bge_reg_set32(bgep, MODE_CONTROL_REG,
3643 		    MODE_SEND_NO_PSEUDO_HDR_CSUM);
3644 
3645 	/*
3646 	 * Step 29: configure Timer Prescaler.  The value is always the
3647 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
3648 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
3649 	 * for the whole chip!
3650 	 */
3651 	bge_reg_put32(bgep, MISC_CONFIG_REG, MISC_CONFIG_DEFAULT);
3652 
3653 	if (DEVICE_5906_SERIES_CHIPSETS(bgep)) {
3654 		drv_usecwait(40);
3655 		/* put PHY into ready state */
3656 		bge_reg_clr32(bgep, MISC_CONFIG_REG, MISC_CONFIG_EPHY_IDDQ);
3657 		(void) bge_reg_get32(bgep, MISC_CONFIG_REG); /* flush */
3658 		drv_usecwait(40);
3659 	}
3660 
3661 	/*
3662 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
3663 	 *
3664 	 * If the mbuf_length is specified as 0, we just leave these at
3665 	 * their hardware defaults, rather than explicitly setting them.
3666 	 * As the Broadcom HRM,driver better not change the parameters
3667 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
3668 	 */
3669 	if ((bgep->chipid.mbuf_length != 0) &&
3670 	    (DEVICE_5704_SERIES_CHIPSETS(bgep))) {
3671 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
3672 			    bgep->chipid.mbuf_base);
3673 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
3674 			    bgep->chipid.mbuf_length);
3675 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
3676 			    DMAD_POOL_BASE_DEFAULT);
3677 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
3678 			    DMAD_POOL_LENGTH_DEFAULT);
3679 	}
3680 
3681 	/*
3682 	 * Step 32: configure MAC memory pool watermarks
3683 	 */
3684 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
3685 	    bgep->chipid.mbuf_lo_water_rdma);
3686 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
3687 	    bgep->chipid.mbuf_lo_water_rmac);
3688 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
3689 	    bgep->chipid.mbuf_hi_water);
3690 
3691 	/*
3692 	 * Step 33: configure DMA resource watermarks
3693 	 */
3694 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3695 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
3696 		    bge_dmad_lo_water);
3697 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
3698 		    bge_dmad_hi_water);
3699 	}
3700 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
3701 
3702 	/*
3703 	 * Steps 34-36: enable buffer manager & internal h/w queues
3704 	 */
3705 	if (!bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
3706 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3707 		retval = DDI_FAILURE;
3708 	if (!bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0))
3709 		retval = DDI_FAILURE;
3710 
3711 	/*
3712 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
3713 	 */
3714 	bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
3715 	    &bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
3716 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3717 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
3718 		    &bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
3719 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
3720 		    &bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
3721 	}
3722 
3723 	/*
3724 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
3725 	 */
3726 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
3727 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3728 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
3729 		    bge_replenish_jumbo);
3730 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
3731 		    bge_replenish_mini);
3732 	}
3733 
3734 	/*
3735 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
3736 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
3737 	 */
3738 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3739 		maxring = BGE_SEND_RINGS_MAX;
3740 	else
3741 		maxring = BGE_SEND_RINGS_MAX_5705;
3742 	for (ring = 0; ring < maxring; ++ring) {
3743 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
3744 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
3745 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
3746 		    &bgep->send[ring].hw_rcb);
3747 	}
3748 
3749 	/*
3750 	 * Steps 44-45: initialise Receive Return Rings
3751 	 * (0x0200-0x02ff in NIC-local memory)
3752 	 */
3753 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3754 		maxring = BGE_RECV_RINGS_MAX;
3755 	else
3756 		maxring = BGE_RECV_RINGS_MAX_5705;
3757 	for (ring = 0; ring < maxring; ++ring)
3758 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
3759 		    &bgep->recv[ring].hw_rcb);
3760 
3761 	/*
3762 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
3763 	 */
3764 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
3765 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3766 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
3767 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
3768 	}
3769 	/*
3770 	 * Step 47: configure the MAC unicast address
3771 	 * Step 48: configure the random backoff seed
3772 	 * Step 96: set up multicast filters
3773 	 */
3774 #ifdef BGE_IPMI_ASF
3775 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE)
3776 #else
3777 	if (bge_chip_sync(bgep) == DDI_FAILURE)
3778 #endif
3779 		retval = DDI_FAILURE;
3780 
3781 	/*
3782 	 * Step 49: configure the MTU
3783 	 */
3784 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
3785 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
3786 
3787 	/*
3788 	 * Step 50: configure the IPG et al
3789 	 */
3790 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
3791 
3792 	/*
3793 	 * Step 51: configure the default Rx Return Ring
3794 	 */
3795 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
3796 
3797 	/*
3798 	 * Steps 52-54: configure Receive List Placement,
3799 	 * and enable Receive List Placement Statistics
3800 	 */
3801 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
3802 	    RCV_LP_CONFIG(bgep->chipid.rx_rings));
3803 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
3804 	case MHCR_CHIP_ASIC_REV_5700:
3805 	case MHCR_CHIP_ASIC_REV_5701:
3806 	case MHCR_CHIP_ASIC_REV_5703:
3807 	case MHCR_CHIP_ASIC_REV_5704:
3808 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
3809 		break;
3810 	case MHCR_CHIP_ASIC_REV_5705:
3811 		break;
3812 	default:
3813 		stats_mask = bge_reg_get32(bgep, RCV_LP_STATS_ENABLE_MASK_REG);
3814 		stats_mask &= ~RCV_LP_STATS_DISABLE_MACTQ;
3815 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, stats_mask);
3816 		break;
3817 	}
3818 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
3819 
3820 	if (bgep->chipid.rx_rings > 1)
3821 		bge_init_recv_rule(bgep);
3822 
3823 	/*
3824 	 * Steps 55-56: enable Send Data Initiator Statistics
3825 	 */
3826 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
3827 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3828 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
3829 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
3830 	} else {
3831 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
3832 		    SEND_INIT_STATS_ENABLE);
3833 	}
3834 	/*
3835 	 * Steps 57-58: stop (?) the Host Coalescing Engine
3836 	 */
3837 	if (!bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0))
3838 		retval = DDI_FAILURE;
3839 
3840 	/*
3841 	 * Steps 59-62: initialise Host Coalescing parameters
3842 	 */
3843 	bge_chip_coalesce_update(bgep);
3844 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3845 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
3846 		    bge_tx_count_intr);
3847 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
3848 		    bge_tx_ticks_intr);
3849 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
3850 		    bge_rx_count_intr);
3851 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
3852 		    bge_rx_ticks_intr);
3853 	}
3854 
3855 	/*
3856 	 * Steps 63-64: initialise status block & statistics
3857 	 * host memory addresses
3858 	 * The statistic block does not exist in some chipsets
3859 	 * Step 65: initialise Statistics Coalescing Tick Counter
3860 	 */
3861 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
3862 	    bgep->status_block.cookie.dmac_laddress);
3863 
3864 	/*
3865 	 * Steps 66-67: initialise status block & statistics
3866 	 * NIC-local memory addresses
3867 	 */
3868 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
3869 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
3870 		    bgep->statistics.cookie.dmac_laddress);
3871 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
3872 		    STATISTICS_TICKS_DEFAULT);
3873 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
3874 		    NIC_MEM_STATUS_BLOCK);
3875 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
3876 		    NIC_MEM_STATISTICS);
3877 	}
3878 
3879 	/*
3880 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
3881 	 * Completion Engine, the Receive List Placement Engine, and the
3882 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
3883 	 * and BCM5715.
3884 	 */
3885 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
3886 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
3887 		coalmode = COALESCE_64_BYTE_STATUS;
3888 	else
3889 		coalmode = 0;
3890 	if (!bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode))
3891 		retval = DDI_FAILURE;
3892 	if (!bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
3893 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3894 		retval = DDI_FAILURE;
3895 	if (!bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0))
3896 		retval = DDI_FAILURE;
3897 
3898 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3899 		if (!bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
3900 		    STATE_MACHINE_ATTN_ENABLE_BIT))
3901 			retval = DDI_FAILURE;
3902 
3903 	/*
3904 	 * Step 72: Enable MAC DMA engines
3905 	 * Step 73: Clear & enable MAC statistics
3906 	 */
3907 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
3908 	    ETHERNET_MODE_ENABLE_FHDE |
3909 	    ETHERNET_MODE_ENABLE_RDE |
3910 	    ETHERNET_MODE_ENABLE_TDE);
3911 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
3912 	    ETHERNET_MODE_ENABLE_TX_STATS |
3913 	    ETHERNET_MODE_ENABLE_RX_STATS |
3914 	    ETHERNET_MODE_CLEAR_TX_STATS |
3915 	    ETHERNET_MODE_CLEAR_RX_STATS);
3916 
3917 	/*
3918 	 * Step 74: configure the MLCR (Miscellaneous Local Control
3919 	 * Register); not required, as we set up the MLCR in step 10
3920 	 * (part of the reset code) above.
3921 	 *
3922 	 * Step 75: clear Interrupt Mailbox 0
3923 	 */
3924 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
3925 
3926 	/*
3927 	 * Steps 76-87: Gentlemen, start your engines ...
3928 	 *
3929 	 * Enable the DMA Completion Engine, the Write DMA Engine,
3930 	 * the Read DMA Engine, Receive Data Completion Engine,
3931 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
3932 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
3933 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
3934 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
3935 	 *
3936 	 * Beware exhaust fumes?
3937 	 */
3938 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3939 		if (!bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0))
3940 			retval = DDI_FAILURE;
3941 	dma_wrprio = (bge_dma_wrprio << DMA_PRIORITY_SHIFT) |
3942 	    ALL_DMA_ATTN_BITS;
3943 	if ((MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
3944 	    MHCR_CHIP_ASIC_REV_5755) ||
3945 	    (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
3946 	    MHCR_CHIP_ASIC_REV_5906)) {
3947 		dma_wrprio |= DMA_STATUS_TAG_FIX_CQ12384;
3948 	}
3949 	if (!bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
3950 	    dma_wrprio))
3951 		retval = DDI_FAILURE;
3952 	if (!bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
3953 	    (bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
3954 		retval = DDI_FAILURE;
3955 	if (!bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
3956 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3957 		retval = DDI_FAILURE;
3958 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
3959 		if (!bge_chip_enable_engine(bgep,
3960 		    MBUF_CLUSTER_FREE_MODE_REG, 0))
3961 			retval = DDI_FAILURE;
3962 	if (!bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0))
3963 		retval = DDI_FAILURE;
3964 	if (!bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
3965 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3966 		retval = DDI_FAILURE;
3967 	if (!bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
3968 	    RCV_BD_DISABLED_RING_ATTN))
3969 		retval = DDI_FAILURE;
3970 	if (!bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
3971 	    RCV_DATA_BD_ILL_RING_ATTN))
3972 		retval = DDI_FAILURE;
3973 	if (!bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0))
3974 		retval = DDI_FAILURE;
3975 	if (!bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
3976 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3977 		retval = DDI_FAILURE;
3978 	if (!bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
3979 	    STATE_MACHINE_ATTN_ENABLE_BIT))
3980 		retval = DDI_FAILURE;
3981 
3982 	/*
3983 	 * Step 88: download firmware -- doesn't apply
3984 	 * Steps 89-90: enable Transmit & Receive MAC Engines
3985 	 */
3986 	if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
3987 		retval = DDI_FAILURE;
3988 #ifdef BGE_IPMI_ASF
3989 	if (!bgep->asf_enabled) {
3990 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
3991 		    RECEIVE_MODE_KEEP_VLAN_TAG))
3992 			retval = DDI_FAILURE;
3993 	} else {
3994 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0))
3995 			retval = DDI_FAILURE;
3996 	}
3997 #else
3998 	if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
3999 	    RECEIVE_MODE_KEEP_VLAN_TAG))
4000 		retval = DDI_FAILURE;
4001 #endif
4002 
4003 	/*
4004 	 * Step 91: disable auto-polling of PHY status
4005 	 */
4006 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
4007 
4008 	/*
4009 	 * Step 92: configure D0 power state (not required)
4010 	 * Step 93: initialise LED control register ()
4011 	 */
4012 	ledctl = LED_CONTROL_DEFAULT;
4013 	switch (bgep->chipid.device) {
4014 	case DEVICE_ID_5700:
4015 	case DEVICE_ID_5700x:
4016 	case DEVICE_ID_5701:
4017 		/*
4018 		 * Switch to 5700 (MAC) mode on these older chips
4019 		 */
4020 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
4021 		ledctl |= LED_CONTROL_LED_MODE_5700;
4022 		break;
4023 
4024 	default:
4025 		break;
4026 	}
4027 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
4028 
4029 	/*
4030 	 * Step 94: activate link
4031 	 */
4032 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
4033 
4034 	/*
4035 	 * Step 95: set up physical layer (PHY/SerDes)
4036 	 * restart autoneg (if required)
4037 	 */
4038 	if (reset_phys)
4039 		if (bge_phys_update(bgep) == DDI_FAILURE)
4040 			retval = DDI_FAILURE;
4041 
4042 	/*
4043 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
4044 	 */
4045 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
4046 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
4047 		    bgep->buff[ring].rf_next);
4048 
4049 	/*
4050 	 * MSI bits:The least significant MSI 16-bit word.
4051 	 * ISR will be triggered different.
4052 	 */
4053 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
4054 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
4055 
4056 	/*
4057 	 * Extra step (DSG): select which interrupts are enabled
4058 	 *
4059 	 * Program the Ethernet MAC engine to signal attention on
4060 	 * Link Change events, then enable interrupts on MAC, DMA,
4061 	 * and FLOW attention signals.
4062 	 */
4063 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
4064 	    ETHERNET_EVENT_LINK_INT |
4065 	    ETHERNET_STATUS_PCS_ERROR_INT);
4066 #ifdef BGE_IPMI_ASF
4067 	if (bgep->asf_enabled) {
4068 		bge_reg_set32(bgep, MODE_CONTROL_REG,
4069 		    MODE_INT_ON_FLOW_ATTN |
4070 		    MODE_INT_ON_DMA_ATTN |
4071 		    MODE_HOST_STACK_UP|
4072 		    MODE_INT_ON_MAC_ATTN);
4073 	} else {
4074 #endif
4075 		bge_reg_set32(bgep, MODE_CONTROL_REG,
4076 		    MODE_INT_ON_FLOW_ATTN |
4077 		    MODE_INT_ON_DMA_ATTN |
4078 		    MODE_INT_ON_MAC_ATTN);
4079 #ifdef BGE_IPMI_ASF
4080 	}
4081 #endif
4082 
4083 	/*
4084 	 * Step 97: enable PCI interrupts!!!
4085 	 */
4086 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
4087 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
4088 		    MHCR_MASK_PCI_INT_OUTPUT);
4089 
4090 	/*
4091 	 * All done!
4092 	 */
4093 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
4094 	return (retval);
4095 }
4096 
4097 
4098 /*
4099  * ========== Hardware interrupt handler ==========
4100  */
4101 
4102 #undef	BGE_DBG
4103 #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
4104 
4105 /*
4106  * Sync the status block, then atomically clear the specified bits in
4107  * the <flags-and-tag> field of the status block.
4108  * the <flags> word of the status block, returning the value of the
4109  * <tag> and the <flags> before the bits were cleared.
4110  */
4111 static int bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags);
4112 #pragma	inline(bge_status_sync)
4113 
4114 static int
4115 bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags)
4116 {
4117 	bge_status_t *bsp;
4118 	int retval;
4119 
4120 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
4121 	    (void *)bgep, bits));
4122 
4123 	ASSERT(bgep->bge_guard == BGE_GUARD);
4124 
4125 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
4126 	retval = bge_check_dma_handle(bgep, bgep->status_block.dma_hdl);
4127 	if (retval != DDI_FM_OK)
4128 		return (retval);
4129 
4130 	bsp = DMA_VPTR(bgep->status_block);
4131 	*flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
4132 
4133 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
4134 	    (void *)bgep, bits, *flags));
4135 
4136 	return (retval);
4137 }
4138 
4139 void bge_wake_factotum(bge_t *bgep);
4140 #pragma	inline(bge_wake_factotum)
4141 
4142 void
4143 bge_wake_factotum(bge_t *bgep)
4144 {
4145 	mutex_enter(bgep->softintrlock);
4146 	if (bgep->factotum_flag == 0) {
4147 		bgep->factotum_flag = 1;
4148 		ddi_trigger_softintr(bgep->factotum_id);
4149 	}
4150 	mutex_exit(bgep->softintrlock);
4151 }
4152 
4153 /*
4154  *	bge_intr() -- handle chip interrupts
4155  */
4156 uint_t bge_intr(caddr_t arg1, caddr_t arg2);
4157 #pragma	no_inline(bge_intr)
4158 
4159 uint_t
4160 bge_intr(caddr_t arg1, caddr_t arg2)
4161 {
4162 	bge_t *bgep = (void *)arg1;		/* private device info	*/
4163 	bge_status_t *bsp;
4164 	uint64_t flags;
4165 	uint32_t regval;
4166 	uint_t result;
4167 	int retval, loop_cnt = 0;
4168 
4169 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
4170 
4171 	/*
4172 	 * GLD v2 checks that s/w setup is complete before passing
4173 	 * interrupts to this routine, thus eliminating the old
4174 	 * (and well-known) race condition around ddi_add_intr()
4175 	 */
4176 	ASSERT(bgep->progress & PROGRESS_HWINT);
4177 
4178 	result = DDI_INTR_UNCLAIMED;
4179 	mutex_enter(bgep->genlock);
4180 
4181 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
4182 		/*
4183 		 * Check whether chip's says it's asserting #INTA;
4184 		 * if not, don't process or claim the interrupt.
4185 		 *
4186 		 * Note that the PCI signal is active low, so the
4187 		 * bit is *zero* when the interrupt is asserted.
4188 		 */
4189 		regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
4190 		if (regval & MLCR_INTA_STATE) {
4191 			if (bge_check_acc_handle(bgep, bgep->io_handle)
4192 			    != DDI_FM_OK)
4193 				goto chip_stop;
4194 			mutex_exit(bgep->genlock);
4195 			return (result);
4196 		}
4197 
4198 		/*
4199 		 * Block further PCI interrupts ...
4200 		 */
4201 		bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
4202 		    MHCR_MASK_PCI_INT_OUTPUT);
4203 
4204 	} else {
4205 		/*
4206 		 * Check MSI status
4207 		 */
4208 		regval = bge_reg_get32(bgep, MSI_STATUS_REG);
4209 		if (regval & MSI_ERROR_ATTENTION) {
4210 			BGE_REPORT((bgep, "msi error attention,"
4211 			    " status=0x%x", regval));
4212 			bge_reg_put32(bgep, MSI_STATUS_REG, regval);
4213 		}
4214 	}
4215 
4216 	result = DDI_INTR_CLAIMED;
4217 
4218 	BGE_DEBUG(("bge_intr($%p) ($%p) regval 0x%08x", arg1, arg2, regval));
4219 
4220 	/*
4221 	 * Sync the status block and grab the flags-n-tag from it.
4222 	 * We count the number of interrupts where there doesn't
4223 	 * seem to have been a DMA update of the status block; if
4224 	 * it *has* been updated, the counter will be cleared in
4225 	 * the while() loop below ...
4226 	 */
4227 	bgep->missed_dmas += 1;
4228 	bsp = DMA_VPTR(bgep->status_block);
4229 	for (loop_cnt = 0; loop_cnt < bge_intr_max_loop; loop_cnt++) {
4230 		if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
4231 			/*
4232 			 * bge_chip_stop() may have freed dma area etc
4233 			 * while we were in this interrupt handler -
4234 			 * better not call bge_status_sync()
4235 			 */
4236 			(void) bge_check_acc_handle(bgep,
4237 			    bgep->io_handle);
4238 			mutex_exit(bgep->genlock);
4239 			return (DDI_INTR_CLAIMED);
4240 		}
4241 		retval = bge_status_sync(bgep, STATUS_FLAG_UPDATED,
4242 		    &flags);
4243 		if (retval != DDI_FM_OK) {
4244 			bgep->bge_dma_error = B_TRUE;
4245 			goto chip_stop;
4246 		}
4247 
4248 		if (!(flags & STATUS_FLAG_UPDATED))
4249 			break;
4250 
4251 		/*
4252 		 * Tell the chip that we're processing the interrupt
4253 		 */
4254 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
4255 		    INTERRUPT_MBOX_DISABLE(flags));
4256 		if (bge_check_acc_handle(bgep, bgep->io_handle) !=
4257 		    DDI_FM_OK)
4258 			goto chip_stop;
4259 
4260 		/*
4261 		 * Drop the mutex while we:
4262 		 * 	Receive any newly-arrived packets
4263 		 *	Recycle any newly-finished send buffers
4264 		 */
4265 		bgep->bge_intr_running = B_TRUE;
4266 		mutex_exit(bgep->genlock);
4267 		bge_receive(bgep, bsp);
4268 		bge_recycle(bgep, bsp);
4269 		mutex_enter(bgep->genlock);
4270 		bgep->bge_intr_running = B_FALSE;
4271 
4272 		/*
4273 		 * Tell the chip we've finished processing, and
4274 		 * give it the tag that we got from the status
4275 		 * block earlier, so that it knows just how far
4276 		 * we've gone.  If it's got more for us to do,
4277 		 * it will now update the status block and try
4278 		 * to assert an interrupt (but we've got the
4279 		 * #INTA blocked at present).  If we see the
4280 		 * update, we'll loop around to do some more.
4281 		 * Eventually we'll get out of here ...
4282 		 */
4283 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
4284 		    INTERRUPT_MBOX_ENABLE(flags));
4285 		if (bgep->chipid.pci_type == BGE_PCI_E)
4286 			(void) bge_mbx_get(bgep, INTERRUPT_MBOX_0_REG);
4287 		bgep->missed_dmas = 0;
4288 	}
4289 
4290 	/*
4291 	 * Check for exceptional conditions that we need to handle
4292 	 *
4293 	 * Link status changed
4294 	 * Status block not updated
4295 	 */
4296 	if (flags & STATUS_FLAG_LINK_CHANGED)
4297 		bge_wake_factotum(bgep);
4298 
4299 	if (bgep->missed_dmas) {
4300 		/*
4301 		 * Probably due to the internal status tag not
4302 		 * being reset.  Force a status block update now;
4303 		 * this should ensure that we get an update and
4304 		 * a new interrupt.  After that, we should be in
4305 		 * sync again ...
4306 		 */
4307 		BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
4308 		    "not updated?", flags));
4309 		bgep->missed_updates++;
4310 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
4311 		    COALESCE_NOW);
4312 
4313 		if (bgep->missed_dmas >= bge_dma_miss_limit) {
4314 			/*
4315 			 * If this happens multiple times in a row,
4316 			 * it means DMA is just not working.  Maybe
4317 			 * the chip's failed, or maybe there's a
4318 			 * problem on the PCI bus or in the host-PCI
4319 			 * bridge (Tomatillo).
4320 			 *
4321 			 * At all events, we want to stop further
4322 			 * interrupts and let the recovery code take
4323 			 * over to see whether anything can be done
4324 			 * about it ...
4325 			 */
4326 			bge_fm_ereport(bgep,
4327 			    DDI_FM_DEVICE_BADINT_LIMIT);
4328 			goto chip_stop;
4329 		}
4330 	}
4331 
4332 	/*
4333 	 * Reenable assertion of #INTA, unless there's a DMA fault
4334 	 */
4335 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
4336 		bge_reg_clr32(bgep, PCI_CONF_BGE_MHCR,
4337 		    MHCR_MASK_PCI_INT_OUTPUT);
4338 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
4339 		    DDI_FM_OK)
4340 			goto chip_stop;
4341 	}
4342 
4343 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
4344 		goto chip_stop;
4345 
4346 	mutex_exit(bgep->genlock);
4347 	return (result);
4348 
4349 chip_stop:
4350 #ifdef BGE_IPMI_ASF
4351 	if (bgep->asf_enabled && bgep->asf_status == ASF_STAT_RUN) {
4352 		/*
4353 		 * We must stop ASF heart beat before
4354 		 * bge_chip_stop(), otherwise some
4355 		 * computers (ex. IBM HS20 blade
4356 		 * server) may crash.
4357 		 */
4358 		bge_asf_update_status(bgep);
4359 		bge_asf_stop_timer(bgep);
4360 		bgep->asf_status = ASF_STAT_STOP;
4361 
4362 		bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
4363 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
4364 	}
4365 #endif
4366 	bge_chip_stop(bgep, B_TRUE);
4367 	(void) bge_check_acc_handle(bgep, bgep->io_handle);
4368 	mutex_exit(bgep->genlock);
4369 	return (result);
4370 }
4371 
4372 /*
4373  * ========== Factotum, implemented as a softint handler ==========
4374  */
4375 
4376 #undef	BGE_DBG
4377 #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
4378 
4379 static void bge_factotum_error_handler(bge_t *bgep);
4380 #pragma	no_inline(bge_factotum_error_handler)
4381 
4382 static void
4383 bge_factotum_error_handler(bge_t *bgep)
4384 {
4385 	uint32_t flow;
4386 	uint32_t rdma;
4387 	uint32_t wdma;
4388 	uint32_t tmac;
4389 	uint32_t rmac;
4390 	uint32_t rxrs;
4391 	uint32_t txrs = 0;
4392 
4393 	ASSERT(mutex_owned(bgep->genlock));
4394 
4395 	/*
4396 	 * Read all the registers that show the possible
4397 	 * reasons for the ERROR bit to be asserted
4398 	 */
4399 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
4400 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
4401 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
4402 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
4403 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
4404 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
4405 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
4406 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
4407 
4408 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
4409 	    (void *)bgep, flow, rdma, wdma));
4410 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
4411 	    (void *)bgep, tmac, rmac, rxrs, txrs));
4412 
4413 	/*
4414 	 * For now, just clear all the errors ...
4415 	 */
4416 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
4417 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
4418 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
4419 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
4420 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
4421 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
4422 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
4423 }
4424 
4425 /*
4426  * Handler for hardware link state change.
4427  *
4428  * When this routine is called, the hardware link state has changed
4429  * and the new state is reflected in the param_* variables.  Here
4430  * we must update the softstate and reprogram the MAC to match.
4431  */
4432 static void bge_factotum_link_handler(bge_t *bgep);
4433 #pragma	no_inline(bge_factotum_link_handler)
4434 
4435 static void
4436 bge_factotum_link_handler(bge_t *bgep)
4437 {
4438 	ASSERT(mutex_owned(bgep->genlock));
4439 
4440 	/*
4441 	 * Update the s/w link_state
4442 	 */
4443 	if (bgep->param_link_up)
4444 		bgep->link_state = LINK_STATE_UP;
4445 	else
4446 		bgep->link_state = LINK_STATE_DOWN;
4447 
4448 	/*
4449 	 * Reprogram the MAC modes to match
4450 	 */
4451 	bge_sync_mac_modes(bgep);
4452 }
4453 
4454 static boolean_t bge_factotum_link_check(bge_t *bgep, int *dma_state);
4455 #pragma	no_inline(bge_factotum_link_check)
4456 
4457 static boolean_t
4458 bge_factotum_link_check(bge_t *bgep, int *dma_state)
4459 {
4460 	boolean_t check;
4461 	uint64_t flags;
4462 	uint32_t tmac_status;
4463 
4464 	ASSERT(mutex_owned(bgep->genlock));
4465 
4466 	/*
4467 	 * Get & clear the writable status bits in the Tx status register
4468 	 * (some bits are write-1-to-clear, others are just readonly).
4469 	 */
4470 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
4471 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
4472 
4473 	/*
4474 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
4475 	 */
4476 	*dma_state = bge_status_sync(bgep, STATUS_FLAG_ERROR |
4477 	    STATUS_FLAG_LINK_CHANGED, &flags);
4478 	if (*dma_state != DDI_FM_OK)
4479 		return (B_FALSE);
4480 
4481 	/*
4482 	 * Clear any errors flagged in the status block ...
4483 	 */
4484 	if (flags & STATUS_FLAG_ERROR)
4485 		bge_factotum_error_handler(bgep);
4486 
4487 	/*
4488 	 * We need to check the link status if:
4489 	 *	the status block says there's been a link change
4490 	 *	or there's any discrepancy between the various
4491 	 *	flags indicating the link state (link_state,
4492 	 *	param_link_up, and the LINK STATE bit in the
4493 	 *	Transmit MAC status register).
4494 	 */
4495 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
4496 	switch (bgep->link_state) {
4497 	case LINK_STATE_UP:
4498 		check |= (bgep->param_link_up == B_FALSE);
4499 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
4500 		break;
4501 
4502 	case LINK_STATE_DOWN:
4503 		check |= (bgep->param_link_up != B_FALSE);
4504 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
4505 		break;
4506 
4507 	default:
4508 		check = B_TRUE;
4509 		break;
4510 	}
4511 
4512 	/*
4513 	 * If <check> is false, we're sure the link hasn't changed.
4514 	 * If true, however, it's not yet definitive; we have to call
4515 	 * bge_phys_check() to determine whether the link has settled
4516 	 * into a new state yet ... and if it has, then call the link
4517 	 * state change handler.But when the chip is 5700 in Dell 6650
4518 	 * ,even if check is false, the link may have changed.So we
4519 	 * have to call bge_phys_check() to determine the link state.
4520 	 */
4521 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
4522 		check = bge_phys_check(bgep);
4523 		if (check)
4524 			bge_factotum_link_handler(bgep);
4525 	}
4526 
4527 	return (check);
4528 }
4529 
4530 /*
4531  * Factotum routine to check for Tx stall, using the 'watchdog' counter
4532  */
4533 static boolean_t bge_factotum_stall_check(bge_t *bgep);
4534 #pragma	no_inline(bge_factotum_stall_check)
4535 
4536 static boolean_t
4537 bge_factotum_stall_check(bge_t *bgep)
4538 {
4539 	uint32_t dogval;
4540 
4541 	ASSERT(mutex_owned(bgep->genlock));
4542 
4543 	/*
4544 	 * Specific check for Tx stall ...
4545 	 *
4546 	 * The 'watchdog' counter is incremented whenever a packet
4547 	 * is queued, reset to 1 when some (but not all) buffers
4548 	 * are reclaimed, reset to 0 (disabled) when all buffers
4549 	 * are reclaimed, and shifted left here.  If it exceeds the
4550 	 * threshold value, the chip is assumed to have stalled and
4551 	 * is put into the ERROR state.  The factotum will then reset
4552 	 * it on the next pass.
4553 	 *
4554 	 * All of which should ensure that we don't get into a state
4555 	 * where packets are left pending indefinitely!
4556 	 */
4557 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
4558 	if (dogval < bge_watchdog_count)
4559 		return (B_FALSE);
4560 
4561 #if !defined(BGE_NETCONSOLE)
4562 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
4563 #endif
4564 	bge_fm_ereport(bgep, DDI_FM_DEVICE_STALL);
4565 	return (B_TRUE);
4566 }
4567 
4568 /*
4569  * The factotum is woken up when there's something to do that we'd rather
4570  * not do from inside a hardware interrupt handler or high-level cyclic.
4571  * Its two main tasks are:
4572  *	reset & restart the chip after an error
4573  *	check the link status whenever necessary
4574  */
4575 uint_t bge_chip_factotum(caddr_t arg);
4576 #pragma	no_inline(bge_chip_factotum)
4577 
4578 uint_t
4579 bge_chip_factotum(caddr_t arg)
4580 {
4581 	bge_t *bgep;
4582 	uint_t result;
4583 	boolean_t error;
4584 	boolean_t linkchg;
4585 	int dma_state;
4586 
4587 	bgep = (void *)arg;
4588 
4589 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
4590 
4591 	mutex_enter(bgep->softintrlock);
4592 	if (bgep->factotum_flag == 0) {
4593 		mutex_exit(bgep->softintrlock);
4594 		return (DDI_INTR_UNCLAIMED);
4595 	}
4596 	bgep->factotum_flag = 0;
4597 	mutex_exit(bgep->softintrlock);
4598 
4599 	result = DDI_INTR_CLAIMED;
4600 	error = B_FALSE;
4601 	linkchg = B_FALSE;
4602 
4603 	mutex_enter(bgep->genlock);
4604 	switch (bgep->bge_chip_state) {
4605 	default:
4606 		break;
4607 
4608 	case BGE_CHIP_RUNNING:
4609 		linkchg = bge_factotum_link_check(bgep, &dma_state);
4610 		error = bge_factotum_stall_check(bgep);
4611 		if (dma_state != DDI_FM_OK) {
4612 			bgep->bge_dma_error = B_TRUE;
4613 			error = B_TRUE;
4614 		}
4615 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
4616 			error = B_TRUE;
4617 		if (error)
4618 			bgep->bge_chip_state = BGE_CHIP_ERROR;
4619 		break;
4620 
4621 	case BGE_CHIP_ERROR:
4622 		error = B_TRUE;
4623 		break;
4624 
4625 	case BGE_CHIP_FAULT:
4626 		/*
4627 		 * Fault detected, time to reset ...
4628 		 */
4629 		if (bge_autorecover) {
4630 			if (!(bgep->progress & PROGRESS_BUFS)) {
4631 				/*
4632 				 * if we can't allocate the ring buffers,
4633 				 * try later
4634 				 */
4635 				if (bge_alloc_bufs(bgep) != DDI_SUCCESS) {
4636 					mutex_exit(bgep->genlock);
4637 					return (result);
4638 				}
4639 				bgep->progress |= PROGRESS_BUFS;
4640 			}
4641 			if (!(bgep->progress & PROGRESS_INTR)) {
4642 				bge_init_rings(bgep);
4643 				bge_intr_enable(bgep);
4644 				bgep->progress |= PROGRESS_INTR;
4645 			}
4646 			if (!(bgep->progress & PROGRESS_KSTATS)) {
4647 				bge_init_kstats(bgep,
4648 				    ddi_get_instance(bgep->devinfo));
4649 				bgep->progress |= PROGRESS_KSTATS;
4650 			}
4651 
4652 			BGE_REPORT((bgep, "automatic recovery activated"));
4653 
4654 			if (bge_restart(bgep, B_FALSE) != DDI_SUCCESS) {
4655 				bgep->bge_chip_state = BGE_CHIP_ERROR;
4656 				error = B_TRUE;
4657 			}
4658 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
4659 			    DDI_FM_OK) {
4660 				bgep->bge_chip_state = BGE_CHIP_ERROR;
4661 				error = B_TRUE;
4662 			}
4663 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
4664 			    DDI_FM_OK) {
4665 				bgep->bge_chip_state = BGE_CHIP_ERROR;
4666 				error = B_TRUE;
4667 			}
4668 			if (error == B_FALSE) {
4669 #ifdef BGE_IPMI_ASF
4670 				if (bgep->asf_enabled &&
4671 				    bgep->asf_status != ASF_STAT_RUN) {
4672 					bgep->asf_timeout_id = timeout(
4673 					    bge_asf_heartbeat, (void *)bgep,
4674 					    drv_usectohz(
4675 					    BGE_ASF_HEARTBEAT_INTERVAL));
4676 					bgep->asf_status = ASF_STAT_RUN;
4677 				}
4678 #endif
4679 				if (!bgep->manual_reset) {
4680 					ddi_fm_service_impact(bgep->devinfo,
4681 					    DDI_SERVICE_RESTORED);
4682 				}
4683 			}
4684 		}
4685 		break;
4686 	}
4687 
4688 
4689 	/*
4690 	 * If an error is detected, stop the chip now, marking it as
4691 	 * faulty, so that it will be reset next time through ...
4692 	 *
4693 	 * Note that if intr_running is set, then bge_intr() has dropped
4694 	 * genlock to call bge_receive/bge_recycle. Can't stop the chip at
4695 	 * this point so have to wait until the next time the factotum runs.
4696 	 */
4697 	if (error && !bgep->bge_intr_running) {
4698 #ifdef BGE_IPMI_ASF
4699 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
4700 			/*
4701 			 * We must stop ASF heart beat before bge_chip_stop(),
4702 			 * otherwise some computers (ex. IBM HS20 blade server)
4703 			 * may crash.
4704 			 */
4705 			bge_asf_update_status(bgep);
4706 			bge_asf_stop_timer(bgep);
4707 			bgep->asf_status = ASF_STAT_STOP;
4708 
4709 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
4710 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
4711 		}
4712 #endif
4713 		bge_chip_stop(bgep, B_TRUE);
4714 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
4715 	}
4716 	mutex_exit(bgep->genlock);
4717 
4718 	/*
4719 	 * If the link state changed, tell the world about it.
4720 	 * Note: can't do this while still holding the mutex.
4721 	 */
4722 	if (bgep->link_update_timer == BGE_LINK_UPDATE_TIMEOUT &&
4723 	    bgep->link_state != LINK_STATE_UNKNOWN)
4724 		linkchg = B_TRUE;
4725 	else if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT &&
4726 	    bgep->link_state == LINK_STATE_DOWN)
4727 		linkchg = B_FALSE;
4728 
4729 	if (linkchg) {
4730 		mac_link_update(bgep->mh, bgep->link_state);
4731 		bgep->link_update_timer = BGE_LINK_UPDATE_DONE;
4732 	}
4733 	if (bgep->manual_reset) {
4734 		bgep->manual_reset = B_FALSE;
4735 	}
4736 
4737 	return (result);
4738 }
4739 
4740 /*
4741  * High-level cyclic handler
4742  *
4743  * This routine schedules a (low-level) softint callback to the
4744  * factotum, and prods the chip to update the status block (which
4745  * will cause a hardware interrupt when complete).
4746  */
4747 void bge_chip_cyclic(void *arg);
4748 #pragma	no_inline(bge_chip_cyclic)
4749 
4750 void
4751 bge_chip_cyclic(void *arg)
4752 {
4753 	bge_t *bgep;
4754 
4755 	bgep = arg;
4756 
4757 	switch (bgep->bge_chip_state) {
4758 	default:
4759 		return;
4760 
4761 	case BGE_CHIP_RUNNING:
4762 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
4763 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
4764 			ddi_fm_service_impact(bgep->devinfo,
4765 			    DDI_SERVICE_UNAFFECTED);
4766 
4767 		if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT)
4768 			bgep->link_update_timer++;
4769 
4770 		break;
4771 
4772 	case BGE_CHIP_FAULT:
4773 	case BGE_CHIP_ERROR:
4774 		break;
4775 	}
4776 
4777 	bge_wake_factotum(bgep);
4778 }
4779 
4780 
4781 /*
4782  * ========== Ioctl subfunctions ==========
4783  */
4784 
4785 #undef	BGE_DBG
4786 #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
4787 
4788 #if	BGE_DEBUGGING || BGE_DO_PPIO
4789 
4790 static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
4791 #pragma	no_inline(bge_chip_peek_cfg)
4792 
4793 static void
4794 bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
4795 {
4796 	uint64_t regval;
4797 	uint64_t regno;
4798 
4799 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
4800 	    (void *)bgep, (void *)ppd));
4801 
4802 	regno = ppd->pp_acc_offset;
4803 
4804 	switch (ppd->pp_acc_size) {
4805 	case 1:
4806 		regval = pci_config_get8(bgep->cfg_handle, regno);
4807 		break;
4808 
4809 	case 2:
4810 		regval = pci_config_get16(bgep->cfg_handle, regno);
4811 		break;
4812 
4813 	case 4:
4814 		regval = pci_config_get32(bgep->cfg_handle, regno);
4815 		break;
4816 
4817 	case 8:
4818 		regval = pci_config_get64(bgep->cfg_handle, regno);
4819 		break;
4820 	}
4821 
4822 	ppd->pp_acc_data = regval;
4823 }
4824 
4825 static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
4826 #pragma	no_inline(bge_chip_poke_cfg)
4827 
4828 static void
4829 bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
4830 {
4831 	uint64_t regval;
4832 	uint64_t regno;
4833 
4834 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
4835 	    (void *)bgep, (void *)ppd));
4836 
4837 	regno = ppd->pp_acc_offset;
4838 	regval = ppd->pp_acc_data;
4839 
4840 	switch (ppd->pp_acc_size) {
4841 	case 1:
4842 		pci_config_put8(bgep->cfg_handle, regno, regval);
4843 		break;
4844 
4845 	case 2:
4846 		pci_config_put16(bgep->cfg_handle, regno, regval);
4847 		break;
4848 
4849 	case 4:
4850 		pci_config_put32(bgep->cfg_handle, regno, regval);
4851 		break;
4852 
4853 	case 8:
4854 		pci_config_put64(bgep->cfg_handle, regno, regval);
4855 		break;
4856 	}
4857 }
4858 
4859 static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
4860 #pragma	no_inline(bge_chip_peek_reg)
4861 
4862 static void
4863 bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
4864 {
4865 	uint64_t regval;
4866 	void *regaddr;
4867 
4868 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
4869 	    (void *)bgep, (void *)ppd));
4870 
4871 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
4872 
4873 	switch (ppd->pp_acc_size) {
4874 	case 1:
4875 		regval = ddi_get8(bgep->io_handle, regaddr);
4876 		break;
4877 
4878 	case 2:
4879 		regval = ddi_get16(bgep->io_handle, regaddr);
4880 		break;
4881 
4882 	case 4:
4883 		regval = ddi_get32(bgep->io_handle, regaddr);
4884 		break;
4885 
4886 	case 8:
4887 		regval = ddi_get64(bgep->io_handle, regaddr);
4888 		break;
4889 	}
4890 
4891 	ppd->pp_acc_data = regval;
4892 }
4893 
4894 static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
4895 #pragma	no_inline(bge_chip_peek_reg)
4896 
4897 static void
4898 bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
4899 {
4900 	uint64_t regval;
4901 	void *regaddr;
4902 
4903 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
4904 	    (void *)bgep, (void *)ppd));
4905 
4906 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
4907 	regval = ppd->pp_acc_data;
4908 
4909 	switch (ppd->pp_acc_size) {
4910 	case 1:
4911 		ddi_put8(bgep->io_handle, regaddr, regval);
4912 		break;
4913 
4914 	case 2:
4915 		ddi_put16(bgep->io_handle, regaddr, regval);
4916 		break;
4917 
4918 	case 4:
4919 		ddi_put32(bgep->io_handle, regaddr, regval);
4920 		break;
4921 
4922 	case 8:
4923 		ddi_put64(bgep->io_handle, regaddr, regval);
4924 		break;
4925 	}
4926 	BGE_PCICHK(bgep);
4927 }
4928 
4929 static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
4930 #pragma	no_inline(bge_chip_peek_nic)
4931 
4932 static void
4933 bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
4934 {
4935 	uint64_t regoff;
4936 	uint64_t regval;
4937 	void *regaddr;
4938 
4939 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
4940 	    (void *)bgep, (void *)ppd));
4941 
4942 	regoff = ppd->pp_acc_offset;
4943 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
4944 	regoff &= MWBAR_GRANULE_MASK;
4945 	regoff += NIC_MEM_WINDOW_OFFSET;
4946 	regaddr = PIO_ADDR(bgep, regoff);
4947 
4948 	switch (ppd->pp_acc_size) {
4949 	case 1:
4950 		regval = ddi_get8(bgep->io_handle, regaddr);
4951 		break;
4952 
4953 	case 2:
4954 		regval = ddi_get16(bgep->io_handle, regaddr);
4955 		break;
4956 
4957 	case 4:
4958 		regval = ddi_get32(bgep->io_handle, regaddr);
4959 		break;
4960 
4961 	case 8:
4962 		regval = ddi_get64(bgep->io_handle, regaddr);
4963 		break;
4964 	}
4965 
4966 	ppd->pp_acc_data = regval;
4967 }
4968 
4969 static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
4970 #pragma	no_inline(bge_chip_poke_nic)
4971 
4972 static void
4973 bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
4974 {
4975 	uint64_t regoff;
4976 	uint64_t regval;
4977 	void *regaddr;
4978 
4979 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
4980 	    (void *)bgep, (void *)ppd));
4981 
4982 	regoff = ppd->pp_acc_offset;
4983 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
4984 	regoff &= MWBAR_GRANULE_MASK;
4985 	regoff += NIC_MEM_WINDOW_OFFSET;
4986 	regaddr = PIO_ADDR(bgep, regoff);
4987 	regval = ppd->pp_acc_data;
4988 
4989 	switch (ppd->pp_acc_size) {
4990 	case 1:
4991 		ddi_put8(bgep->io_handle, regaddr, regval);
4992 		break;
4993 
4994 	case 2:
4995 		ddi_put16(bgep->io_handle, regaddr, regval);
4996 		break;
4997 
4998 	case 4:
4999 		ddi_put32(bgep->io_handle, regaddr, regval);
5000 		break;
5001 
5002 	case 8:
5003 		ddi_put64(bgep->io_handle, regaddr, regval);
5004 		break;
5005 	}
5006 	BGE_PCICHK(bgep);
5007 }
5008 
5009 static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
5010 #pragma	no_inline(bge_chip_peek_mii)
5011 
5012 static void
5013 bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
5014 {
5015 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
5016 	    (void *)bgep, (void *)ppd));
5017 
5018 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
5019 }
5020 
5021 static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
5022 #pragma	no_inline(bge_chip_poke_mii)
5023 
5024 static void
5025 bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
5026 {
5027 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
5028 	    (void *)bgep, (void *)ppd));
5029 
5030 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
5031 }
5032 
5033 #if	BGE_SEE_IO32
5034 
5035 static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
5036 #pragma	no_inline(bge_chip_peek_seeprom)
5037 
5038 static void
5039 bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
5040 {
5041 	uint32_t data;
5042 	int err;
5043 
5044 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
5045 	    (void *)bgep, (void *)ppd));
5046 
5047 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
5048 	ppd->pp_acc_data = err ? ~0ull : data;
5049 }
5050 
5051 static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
5052 #pragma	no_inline(bge_chip_poke_seeprom)
5053 
5054 static void
5055 bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
5056 {
5057 	uint32_t data;
5058 
5059 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
5060 	    (void *)bgep, (void *)ppd));
5061 
5062 	data = ppd->pp_acc_data;
5063 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
5064 }
5065 #endif	/* BGE_SEE_IO32 */
5066 
5067 #if	BGE_FLASH_IO32
5068 
5069 static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
5070 #pragma	no_inline(bge_chip_peek_flash)
5071 
5072 static void
5073 bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
5074 {
5075 	uint32_t data;
5076 	int err;
5077 
5078 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
5079 	    (void *)bgep, (void *)ppd));
5080 
5081 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
5082 	ppd->pp_acc_data = err ? ~0ull : data;
5083 }
5084 
5085 static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
5086 #pragma	no_inline(bge_chip_poke_flash)
5087 
5088 static void
5089 bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
5090 {
5091 	uint32_t data;
5092 
5093 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
5094 	    (void *)bgep, (void *)ppd));
5095 
5096 	data = ppd->pp_acc_data;
5097 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
5098 	    ppd->pp_acc_offset, &data);
5099 }
5100 #endif	/* BGE_FLASH_IO32 */
5101 
5102 static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
5103 #pragma	no_inline(bge_chip_peek_mem)
5104 
5105 static void
5106 bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
5107 {
5108 	uint64_t regval;
5109 	void *vaddr;
5110 
5111 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
5112 	    (void *)bgep, (void *)ppd));
5113 
5114 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
5115 
5116 	switch (ppd->pp_acc_size) {
5117 	case 1:
5118 		regval = *(uint8_t *)vaddr;
5119 		break;
5120 
5121 	case 2:
5122 		regval = *(uint16_t *)vaddr;
5123 		break;
5124 
5125 	case 4:
5126 		regval = *(uint32_t *)vaddr;
5127 		break;
5128 
5129 	case 8:
5130 		regval = *(uint64_t *)vaddr;
5131 		break;
5132 	}
5133 
5134 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
5135 	    (void *)bgep, (void *)ppd, regval, vaddr));
5136 
5137 	ppd->pp_acc_data = regval;
5138 }
5139 
5140 static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
5141 #pragma	no_inline(bge_chip_poke_mem)
5142 
5143 static void
5144 bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
5145 {
5146 	uint64_t regval;
5147 	void *vaddr;
5148 
5149 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
5150 	    (void *)bgep, (void *)ppd));
5151 
5152 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
5153 	regval = ppd->pp_acc_data;
5154 
5155 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
5156 	    (void *)bgep, (void *)ppd, regval, vaddr));
5157 
5158 	switch (ppd->pp_acc_size) {
5159 	case 1:
5160 		*(uint8_t *)vaddr = (uint8_t)regval;
5161 		break;
5162 
5163 	case 2:
5164 		*(uint16_t *)vaddr = (uint16_t)regval;
5165 		break;
5166 
5167 	case 4:
5168 		*(uint32_t *)vaddr = (uint32_t)regval;
5169 		break;
5170 
5171 	case 8:
5172 		*(uint64_t *)vaddr = (uint64_t)regval;
5173 		break;
5174 	}
5175 }
5176 
5177 static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
5178 					struct iocblk *iocp);
5179 #pragma	no_inline(bge_pp_ioctl)
5180 
5181 static enum ioc_reply
5182 bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
5183 {
5184 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
5185 	bge_peekpoke_t *ppd;
5186 	dma_area_t *areap;
5187 	uint64_t sizemask;
5188 	uint64_t mem_va;
5189 	uint64_t maxoff;
5190 	boolean_t peek;
5191 
5192 	switch (cmd) {
5193 	default:
5194 		/* NOTREACHED */
5195 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
5196 		return (IOC_INVAL);
5197 
5198 	case BGE_PEEK:
5199 		peek = B_TRUE;
5200 		break;
5201 
5202 	case BGE_POKE:
5203 		peek = B_FALSE;
5204 		break;
5205 	}
5206 
5207 	/*
5208 	 * Validate format of ioctl
5209 	 */
5210 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
5211 		return (IOC_INVAL);
5212 	if (mp->b_cont == NULL)
5213 		return (IOC_INVAL);
5214 	ppd = (void *)mp->b_cont->b_rptr;
5215 
5216 	/*
5217 	 * Validate request parameters
5218 	 */
5219 	switch (ppd->pp_acc_space) {
5220 	default:
5221 		return (IOC_INVAL);
5222 
5223 	case BGE_PP_SPACE_CFG:
5224 		/*
5225 		 * Config space
5226 		 */
5227 		sizemask = 8|4|2|1;
5228 		mem_va = 0;
5229 		maxoff = PCI_CONF_HDR_SIZE;
5230 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
5231 		break;
5232 
5233 	case BGE_PP_SPACE_REG:
5234 		/*
5235 		 * Memory-mapped I/O space
5236 		 */
5237 		sizemask = 8|4|2|1;
5238 		mem_va = 0;
5239 		maxoff = RIAAR_REGISTER_MAX;
5240 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
5241 		break;
5242 
5243 	case BGE_PP_SPACE_NIC:
5244 		/*
5245 		 * NIC on-chip memory
5246 		 */
5247 		sizemask = 8|4|2|1;
5248 		mem_va = 0;
5249 		maxoff = MWBAR_ONCHIP_MAX;
5250 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
5251 		break;
5252 
5253 	case BGE_PP_SPACE_MII:
5254 		/*
5255 		 * PHY's MII registers
5256 		 * NB: all PHY registers are two bytes, but the
5257 		 * addresses increment in ones (word addressing).
5258 		 * So we scale the address here, then undo the
5259 		 * transformation inside the peek/poke functions.
5260 		 */
5261 		ppd->pp_acc_offset *= 2;
5262 		sizemask = 2;
5263 		mem_va = 0;
5264 		maxoff = (MII_MAXREG+1)*2;
5265 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
5266 		break;
5267 
5268 #if	BGE_SEE_IO32
5269 	case BGE_PP_SPACE_SEEPROM:
5270 		/*
5271 		 * Attached SEEPROM(s), if any.
5272 		 * NB: we use the high-order bits of the 'address' as
5273 		 * a device select to accommodate multiple SEEPROMS,
5274 		 * If each one is the maximum size (64kbytes), this
5275 		 * makes them appear contiguous.  Otherwise, there may
5276 		 * be holes in the mapping.  ENxS doesn't have any
5277 		 * SEEPROMs anyway ...
5278 		 */
5279 		sizemask = 4;
5280 		mem_va = 0;
5281 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
5282 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
5283 		break;
5284 #endif	/* BGE_SEE_IO32 */
5285 
5286 #if	BGE_FLASH_IO32
5287 	case BGE_PP_SPACE_FLASH:
5288 		/*
5289 		 * Attached Flash device (if any); a maximum of one device
5290 		 * is currently supported.  But it can be up to 1MB (unlike
5291 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
5292 		 */
5293 		sizemask = 4;
5294 		mem_va = 0;
5295 		maxoff = NVM_FLASH_ADDR_MASK;
5296 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
5297 		break;
5298 #endif	/* BGE_FLASH_IO32 */
5299 
5300 	case BGE_PP_SPACE_BGE:
5301 		/*
5302 		 * BGE data structure!
5303 		 */
5304 		sizemask = 8|4|2|1;
5305 		mem_va = (uintptr_t)bgep;
5306 		maxoff = sizeof (*bgep);
5307 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
5308 		break;
5309 
5310 	case BGE_PP_SPACE_STATUS:
5311 	case BGE_PP_SPACE_STATISTICS:
5312 	case BGE_PP_SPACE_TXDESC:
5313 	case BGE_PP_SPACE_TXBUFF:
5314 	case BGE_PP_SPACE_RXDESC:
5315 	case BGE_PP_SPACE_RXBUFF:
5316 		/*
5317 		 * Various DMA_AREAs
5318 		 */
5319 		switch (ppd->pp_acc_space) {
5320 		case BGE_PP_SPACE_TXDESC:
5321 			areap = &bgep->tx_desc;
5322 			break;
5323 		case BGE_PP_SPACE_TXBUFF:
5324 			areap = &bgep->tx_buff[0];
5325 			break;
5326 		case BGE_PP_SPACE_RXDESC:
5327 			areap = &bgep->rx_desc[0];
5328 			break;
5329 		case BGE_PP_SPACE_RXBUFF:
5330 			areap = &bgep->rx_buff[0];
5331 			break;
5332 		case BGE_PP_SPACE_STATUS:
5333 			areap = &bgep->status_block;
5334 			break;
5335 		case BGE_PP_SPACE_STATISTICS:
5336 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
5337 				areap = &bgep->statistics;
5338 			break;
5339 		}
5340 
5341 		sizemask = 8|4|2|1;
5342 		mem_va = (uintptr_t)areap->mem_va;
5343 		maxoff = areap->alength;
5344 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
5345 		break;
5346 	}
5347 
5348 	switch (ppd->pp_acc_size) {
5349 	default:
5350 		return (IOC_INVAL);
5351 
5352 	case 8:
5353 	case 4:
5354 	case 2:
5355 	case 1:
5356 		if ((ppd->pp_acc_size & sizemask) == 0)
5357 			return (IOC_INVAL);
5358 		break;
5359 	}
5360 
5361 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
5362 		return (IOC_INVAL);
5363 
5364 	if (ppd->pp_acc_offset >= maxoff)
5365 		return (IOC_INVAL);
5366 
5367 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
5368 		return (IOC_INVAL);
5369 
5370 	/*
5371 	 * All OK - go do it!
5372 	 */
5373 	ppd->pp_acc_offset += mem_va;
5374 	(*ppfn)(bgep, ppd);
5375 	return (peek ? IOC_REPLY : IOC_ACK);
5376 }
5377 
5378 static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
5379 					struct iocblk *iocp);
5380 #pragma	no_inline(bge_diag_ioctl)
5381 
5382 static enum ioc_reply
5383 bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
5384 {
5385 	ASSERT(mutex_owned(bgep->genlock));
5386 
5387 	switch (cmd) {
5388 	default:
5389 		/* NOTREACHED */
5390 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
5391 		return (IOC_INVAL);
5392 
5393 	case BGE_DIAG:
5394 		/*
5395 		 * Currently a no-op
5396 		 */
5397 		return (IOC_ACK);
5398 
5399 	case BGE_PEEK:
5400 	case BGE_POKE:
5401 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
5402 
5403 	case BGE_PHY_RESET:
5404 		return (IOC_RESTART_ACK);
5405 
5406 	case BGE_SOFT_RESET:
5407 	case BGE_HARD_RESET:
5408 		/*
5409 		 * Reset and reinitialise the 570x hardware
5410 		 */
5411 		bgep->bge_chip_state = BGE_CHIP_FAULT;
5412 		ddi_trigger_softintr(bgep->factotum_id);
5413 		(void) bge_restart(bgep, cmd == BGE_HARD_RESET);
5414 		return (IOC_ACK);
5415 	}
5416 
5417 	/* NOTREACHED */
5418 }
5419 
5420 #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
5421 
5422 static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
5423 				    struct iocblk *iocp);
5424 #pragma	no_inline(bge_mii_ioctl)
5425 
5426 static enum ioc_reply
5427 bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
5428 {
5429 	struct bge_mii_rw *miirwp;
5430 
5431 	/*
5432 	 * Validate format of ioctl
5433 	 */
5434 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
5435 		return (IOC_INVAL);
5436 	if (mp->b_cont == NULL)
5437 		return (IOC_INVAL);
5438 	miirwp = (void *)mp->b_cont->b_rptr;
5439 
5440 	/*
5441 	 * Validate request parameters ...
5442 	 */
5443 	if (miirwp->mii_reg > MII_MAXREG)
5444 		return (IOC_INVAL);
5445 
5446 	switch (cmd) {
5447 	default:
5448 		/* NOTREACHED */
5449 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
5450 		return (IOC_INVAL);
5451 
5452 	case BGE_MII_READ:
5453 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
5454 		return (IOC_REPLY);
5455 
5456 	case BGE_MII_WRITE:
5457 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
5458 		return (IOC_ACK);
5459 	}
5460 
5461 	/* NOTREACHED */
5462 }
5463 
5464 #if	BGE_SEE_IO32
5465 
5466 static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
5467 				    struct iocblk *iocp);
5468 #pragma	no_inline(bge_see_ioctl)
5469 
5470 static enum ioc_reply
5471 bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
5472 {
5473 	struct bge_see_rw *seerwp;
5474 
5475 	/*
5476 	 * Validate format of ioctl
5477 	 */
5478 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
5479 		return (IOC_INVAL);
5480 	if (mp->b_cont == NULL)
5481 		return (IOC_INVAL);
5482 	seerwp = (void *)mp->b_cont->b_rptr;
5483 
5484 	/*
5485 	 * Validate request parameters ...
5486 	 */
5487 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
5488 		return (IOC_INVAL);
5489 
5490 	switch (cmd) {
5491 	default:
5492 		/* NOTREACHED */
5493 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
5494 		return (IOC_INVAL);
5495 
5496 	case BGE_SEE_READ:
5497 	case BGE_SEE_WRITE:
5498 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
5499 		    seerwp->see_addr, &seerwp->see_data);
5500 		return (IOC_REPLY);
5501 	}
5502 
5503 	/* NOTREACHED */
5504 }
5505 
5506 #endif	/* BGE_SEE_IO32 */
5507 
5508 #if	BGE_FLASH_IO32
5509 
5510 static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
5511 				    struct iocblk *iocp);
5512 #pragma	no_inline(bge_flash_ioctl)
5513 
5514 static enum ioc_reply
5515 bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
5516 {
5517 	struct bge_flash_rw *flashrwp;
5518 
5519 	/*
5520 	 * Validate format of ioctl
5521 	 */
5522 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
5523 		return (IOC_INVAL);
5524 	if (mp->b_cont == NULL)
5525 		return (IOC_INVAL);
5526 	flashrwp = (void *)mp->b_cont->b_rptr;
5527 
5528 	/*
5529 	 * Validate request parameters ...
5530 	 */
5531 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
5532 		return (IOC_INVAL);
5533 
5534 	switch (cmd) {
5535 	default:
5536 		/* NOTREACHED */
5537 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
5538 		return (IOC_INVAL);
5539 
5540 	case BGE_FLASH_READ:
5541 	case BGE_FLASH_WRITE:
5542 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
5543 		    flashrwp->flash_addr, &flashrwp->flash_data);
5544 		return (IOC_REPLY);
5545 	}
5546 
5547 	/* NOTREACHED */
5548 }
5549 
5550 #endif	/* BGE_FLASH_IO32 */
5551 
5552 enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
5553 				struct iocblk *iocp);
5554 #pragma	no_inline(bge_chip_ioctl)
5555 
5556 enum ioc_reply
5557 bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
5558 {
5559 	int cmd;
5560 
5561 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
5562 	    (void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
5563 
5564 	ASSERT(mutex_owned(bgep->genlock));
5565 
5566 	cmd = iocp->ioc_cmd;
5567 	switch (cmd) {
5568 	default:
5569 		/* NOTREACHED */
5570 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
5571 		return (IOC_INVAL);
5572 
5573 	case BGE_DIAG:
5574 	case BGE_PEEK:
5575 	case BGE_POKE:
5576 	case BGE_PHY_RESET:
5577 	case BGE_SOFT_RESET:
5578 	case BGE_HARD_RESET:
5579 #if	BGE_DEBUGGING || BGE_DO_PPIO
5580 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
5581 #else
5582 		return (IOC_INVAL);
5583 #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
5584 
5585 	case BGE_MII_READ:
5586 	case BGE_MII_WRITE:
5587 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
5588 
5589 #if	BGE_SEE_IO32
5590 	case BGE_SEE_READ:
5591 	case BGE_SEE_WRITE:
5592 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
5593 #endif	/* BGE_SEE_IO32 */
5594 
5595 #if	BGE_FLASH_IO32
5596 	case BGE_FLASH_READ:
5597 	case BGE_FLASH_WRITE:
5598 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
5599 #endif	/* BGE_FLASH_IO32 */
5600 	}
5601 
5602 	/* NOTREACHED */
5603 }
5604 
5605 /* ARGSUSED */
5606 void
5607 bge_chip_blank(void *arg, time_t ticks, uint_t count, int flag)
5608 {
5609 	recv_ring_t *rrp = arg;
5610 	bge_t *bgep = rrp->bgep;
5611 
5612 	mutex_enter(bgep->genlock);
5613 	rrp->poll_flag = flag;
5614 #ifdef NOT_YET
5615 	/*
5616 	 * XXX-Sunay: Since most broadcom cards support only one
5617 	 * interrupt but multiple rx rings, we can't disable the
5618 	 * physical interrupt. This need to be done via capability
5619 	 * negotiation depending on the NIC.
5620 	 */
5621 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
5622 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
5623 #endif
5624 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
5625 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
5626 	mutex_exit(bgep->genlock);
5627 }
5628 
5629 #ifdef BGE_IPMI_ASF
5630 
5631 uint32_t
5632 bge_nic_read32(bge_t *bgep, bge_regno_t addr)
5633 {
5634 	uint32_t data;
5635 
5636 #ifndef __sparc
5637 	if (!bgep->asf_wordswapped) {
5638 		/* a workaround word swap error */
5639 		if (addr & 4)
5640 			addr = addr - 4;
5641 		else
5642 			addr = addr + 4;
5643 	}
5644 #endif
5645 
5646 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
5647 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
5648 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
5649 
5650 	data = LE_32(data);
5651 	return (data);
5652 }
5653 
5654 void
5655 bge_asf_update_status(bge_t *bgep)
5656 {
5657 	uint32_t event;
5658 
5659 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
5660 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
5661 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
5662 
5663 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
5664 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
5665 }
5666 
5667 
5668 /*
5669  * The driver is supposed to notify ASF that the OS is still running
5670  * every three seconds, otherwise the management server may attempt
5671  * to reboot the machine.  If it hasn't actually failed, this is
5672  * not a desirable result.  However, this isn't running as a real-time
5673  * thread, and even if it were, it might not be able to generate the
5674  * heartbeat in a timely manner due to system load.  As it isn't a
5675  * significant strain on the machine, we will set the interval to half
5676  * of the required value.
5677  */
5678 void
5679 bge_asf_heartbeat(void *arg)
5680 {
5681 	bge_t *bgep = (bge_t *)arg;
5682 
5683 	mutex_enter(bgep->genlock);
5684 	bge_asf_update_status((bge_t *)bgep);
5685 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
5686 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5687 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
5688 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
5689 	mutex_exit(bgep->genlock);
5690 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
5691 	    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
5692 }
5693 
5694 
5695 void
5696 bge_asf_stop_timer(bge_t *bgep)
5697 {
5698 	timeout_id_t tmp_id = 0;
5699 
5700 	while ((bgep->asf_timeout_id != 0) &&
5701 	    (tmp_id != bgep->asf_timeout_id)) {
5702 		tmp_id = bgep->asf_timeout_id;
5703 		(void) untimeout(tmp_id);
5704 	}
5705 	bgep->asf_timeout_id = 0;
5706 }
5707 
5708 
5709 
5710 /*
5711  * This function should be placed at the earliest position of bge_attach().
5712  */
5713 void
5714 bge_asf_get_config(bge_t *bgep)
5715 {
5716 	uint32_t nicsig;
5717 	uint32_t niccfg;
5718 
5719 	bgep->asf_enabled = B_FALSE;
5720 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
5721 	if (nicsig == BGE_NIC_DATA_SIG) {
5722 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
5723 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
5724 			/*
5725 			 * Here, we don't consider BAXTER, because BGE haven't
5726 			 * supported BAXTER (that is 5752). Also, as I know,
5727 			 * BAXTER doesn't support ASF feature.
5728 			 */
5729 			bgep->asf_enabled = B_TRUE;
5730 		else
5731 			bgep->asf_enabled = B_FALSE;
5732 	} else
5733 		bgep->asf_enabled = B_FALSE;
5734 }
5735 
5736 
5737 void
5738 bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
5739 {
5740 	uint32_t tries;
5741 	uint32_t event;
5742 
5743 	ASSERT(bgep->asf_enabled);
5744 
5745 	/* Issues "pause firmware" command and wait for ACK */
5746 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
5747 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
5748 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
5749 
5750 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
5751 	tries = 0;
5752 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
5753 		drv_usecwait(1);
5754 		tries ++;
5755 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
5756 	}
5757 
5758 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
5759 	    BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
5760 
5761 	if (bgep->asf_newhandshake) {
5762 		switch (mode) {
5763 		case BGE_INIT_RESET:
5764 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5765 			    BGE_DRV_STATE_START);
5766 			break;
5767 		case BGE_SHUTDOWN_RESET:
5768 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5769 			    BGE_DRV_STATE_UNLOAD);
5770 			break;
5771 		case BGE_SUSPEND_RESET:
5772 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5773 			    BGE_DRV_STATE_SUSPEND);
5774 			break;
5775 		default:
5776 			break;
5777 		}
5778 	}
5779 }
5780 
5781 
5782 void
5783 bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
5784 {
5785 	switch (mode) {
5786 	case BGE_INIT_RESET:
5787 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5788 		    BGE_DRV_STATE_START);
5789 		break;
5790 	case BGE_SHUTDOWN_RESET:
5791 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5792 		    BGE_DRV_STATE_UNLOAD);
5793 		break;
5794 	case BGE_SUSPEND_RESET:
5795 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5796 		    BGE_DRV_STATE_SUSPEND);
5797 		break;
5798 	default:
5799 		break;
5800 	}
5801 }
5802 
5803 
5804 void
5805 bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
5806 {
5807 	switch (mode) {
5808 	case BGE_INIT_RESET:
5809 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5810 		    BGE_DRV_STATE_START_DONE);
5811 		break;
5812 	case BGE_SHUTDOWN_RESET:
5813 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
5814 		    BGE_DRV_STATE_UNLOAD_DONE);
5815 		break;
5816 	default:
5817 		break;
5818 	}
5819 }
5820 
5821 #endif /* BGE_IPMI_ASF */
5822