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