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