xref: /onnv-gate/usr/src/uts/common/io/bge/bge_chip2.c (revision 12854:d1953f8e5b5f)
11369Sdduvall /*
21369Sdduvall  * CDDL HEADER START
31369Sdduvall  *
41369Sdduvall  * The contents of this file are subject to the terms of the
51369Sdduvall  * Common Development and Distribution License (the "License").
61369Sdduvall  * You may not use this file except in compliance with the License.
71369Sdduvall  *
81369Sdduvall  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91369Sdduvall  * or http://www.opensolaris.org/os/licensing.
101369Sdduvall  * See the License for the specific language governing permissions
111369Sdduvall  * and limitations under the License.
121369Sdduvall  *
131369Sdduvall  * When distributing Covered Code, include this CDDL HEADER in each
141369Sdduvall  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151369Sdduvall  * If applicable, add the following below this CDDL HEADER, with the
161369Sdduvall  * fields enclosed by brackets "[]" replaced with your own identifying
171369Sdduvall  * information: Portions Copyright [yyyy] [name of copyright owner]
181369Sdduvall  *
191369Sdduvall  * CDDL HEADER END
201369Sdduvall  */
211369Sdduvall 
221369Sdduvall /*
2312331SYong.Tan@Sun.COM  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
241369Sdduvall  */
251369Sdduvall 
262675Szh199473 #include "bge_impl.h"
271369Sdduvall 
281369Sdduvall #define	PIO_ADDR(bgep, offset)	((void *)((caddr_t)(bgep)->io_regs+(offset)))
291369Sdduvall 
301369Sdduvall /*
311369Sdduvall  * Future features ... ?
321369Sdduvall  */
332135Szh199473 #define	BGE_CFG_IO8	1	/* 8/16-bit cfg space BIS/BIC	*/
343918Sml149210 #define	BGE_IND_IO32	1	/* indirect access code		*/
351369Sdduvall #define	BGE_SEE_IO32	1	/* SEEPROM access code		*/
361369Sdduvall #define	BGE_FLASH_IO32	1	/* FLASH access code		*/
371369Sdduvall 
381369Sdduvall /*
391369Sdduvall  * BGE MSI tunable:
401369Sdduvall  *
411369Sdduvall  * By default MSI is enabled on all supported platforms but it is disabled
421369Sdduvall  * for some Broadcom chips due to known MSI hardware issues. Currently MSI
431369Sdduvall  * is enabled only for 5714C A2 and 5715C A2 broadcom chips.
441369Sdduvall  */
451369Sdduvall boolean_t bge_enable_msi = B_TRUE;
461369Sdduvall 
471369Sdduvall /*
483907Szh199473  * PCI-X/PCI-E relaxed ordering tunable for OS/Nexus driver
493907Szh199473  */
503907Szh199473 boolean_t bge_relaxed_ordering = B_TRUE;
513907Szh199473 
523907Szh199473 /*
531369Sdduvall  * Property names
541369Sdduvall  */
551369Sdduvall static char knownids_propname[] = "bge-known-subsystems";
561369Sdduvall 
571369Sdduvall /*
581369Sdduvall  * Patchable globals:
591369Sdduvall  *
601369Sdduvall  *	bge_autorecover
611369Sdduvall  *		Enables/disables automatic recovery after fault detection
621369Sdduvall  *
631369Sdduvall  *	bge_mlcr_default
641369Sdduvall  *		Value to program into the MLCR; controls the chip's GPIO pins
651369Sdduvall  *
661369Sdduvall  *	bge_dma_{rd,wr}prio
671369Sdduvall  *		Relative priorities of DMA reads & DMA writes respectively.
681369Sdduvall  *		These may each be patched to any value 0-3.  Equal values
691369Sdduvall  *		will give "fair" (round-robin) arbitration for PCI access.
701369Sdduvall  *		Unequal values will give one or the other function priority.
711369Sdduvall  *
721369Sdduvall  *	bge_dma_rwctrl
731369Sdduvall  *		Value to put in the Read/Write DMA control register.  See
741369Sdduvall  *	        the Broadcom PRM for things you can fiddle with in this
751369Sdduvall  *		register ...
761369Sdduvall  *
771369Sdduvall  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
781369Sdduvall  *		Send/receive interrupt coalescing parameters.  Counts are
791369Sdduvall  *		#s of descriptors, ticks are in microseconds.  *norm* values
801369Sdduvall  *		apply between status updates/interrupts; the *intr* values
811369Sdduvall  *		refer to the 'during-interrupt' versions - see the PRM.
821369Sdduvall  *
831369Sdduvall  *		NOTE: these values have been determined by measurement. They
841369Sdduvall  *		differ significantly from the values recommended in the PRM.
851369Sdduvall  */
861369Sdduvall static uint32_t bge_autorecover = 1;
871369Sdduvall static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
881369Sdduvall 
891369Sdduvall static uint32_t bge_dma_rdprio = 1;
901369Sdduvall static uint32_t bge_dma_wrprio = 0;
911369Sdduvall static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
921369Sdduvall static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
931369Sdduvall static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
941369Sdduvall static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
951369Sdduvall 
961369Sdduvall uint32_t bge_rx_ticks_norm = 128;
971369Sdduvall uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
981369Sdduvall uint32_t bge_rx_count_norm = 8;
991369Sdduvall uint32_t bge_tx_count_norm = 128;
1001369Sdduvall 
1011369Sdduvall static uint32_t bge_rx_ticks_intr = 128;
1021369Sdduvall static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
1031369Sdduvall static uint32_t bge_rx_count_intr = 2;
1041369Sdduvall static uint32_t bge_tx_count_intr = 0;
1051369Sdduvall 
1061369Sdduvall /*
1071369Sdduvall  * Memory pool configuration parameters.
1081369Sdduvall  *
1091369Sdduvall  * These are generally specific to each member of the chip family, since
1101369Sdduvall  * each one may have a different memory size/configuration.
1111369Sdduvall  *
1121369Sdduvall  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
1131369Sdduvall  * the driver from programming the various registers; instead they are left
1141369Sdduvall  * at their hardware defaults.  This is the preferred option for later chips
1151369Sdduvall  * (5705+), whereas the older chips *required* these registers to be set,
1161369Sdduvall  * since the h/w default was 0 ;-(
1171369Sdduvall  */
1181369Sdduvall static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
1191369Sdduvall static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
1201369Sdduvall static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
1211369Sdduvall static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
1221369Sdduvall static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
1231369Sdduvall static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
1241369Sdduvall static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
1251369Sdduvall static uint32_t bge_mbuf_pool_len_5721	= 0;
1261369Sdduvall 
1271369Sdduvall /*
1281369Sdduvall  * Various high and low water marks, thresholds, etc ...
1291369Sdduvall  *
1301369Sdduvall  * Note: these are taken from revision 7 of the PRM, and some are different
1311369Sdduvall  * from both the values in earlier PRMs *and* those determined experimentally
1321369Sdduvall  * and used in earlier versions of this driver ...
1331369Sdduvall  */
1341369Sdduvall static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
1351369Sdduvall static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
1361369Sdduvall static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
1371369Sdduvall 
1381369Sdduvall static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
1391369Sdduvall static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
1401369Sdduvall static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
1411369Sdduvall 
1421369Sdduvall static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
1431369Sdduvall static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
1441369Sdduvall static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
1451369Sdduvall 
1461369Sdduvall static uint32_t	bge_watchdog_count	= 1 << 16;
1471369Sdduvall static uint16_t bge_dma_miss_limit	= 20;
1481369Sdduvall 
1491369Sdduvall static uint32_t bge_stop_start_on_sync	= 0;
1501369Sdduvall 
1511369Sdduvall /*
1523918Sml149210  * bge_intr_max_loop controls the maximum loop number within bge_intr.
1533918Sml149210  * When loading NIC with heavy network traffic, it is useful.
1543918Sml149210  * Increasing this value could have positive effect to throughput,
1553918Sml149210  * but it might also increase ticks of a bge ISR stick on CPU, which might
1563918Sml149210  * lead to bad UI interactive experience. So tune this with caution.
1573918Sml149210  */
1583918Sml149210 static int bge_intr_max_loop = 1;
1593918Sml149210 
1603918Sml149210 /*
1611369Sdduvall  * ========== Low-level chip & ring buffer manipulation ==========
1621369Sdduvall  */
1631369Sdduvall 
1641369Sdduvall #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
1651369Sdduvall 
1661369Sdduvall 
1671369Sdduvall /*
1681369Sdduvall  * Config space read-modify-write routines
1691369Sdduvall  */
1701369Sdduvall 
1711369Sdduvall #if	BGE_CFG_IO8
1721369Sdduvall 
1731369Sdduvall static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
1741369Sdduvall #pragma	inline(bge_cfg_clr16)
1751369Sdduvall 
1761369Sdduvall static void
bge_cfg_clr16(bge_t * bgep,bge_regno_t regno,uint16_t bits)1771369Sdduvall bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
1781369Sdduvall {
1791369Sdduvall 	uint16_t regval;
1801369Sdduvall 
1811369Sdduvall 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
1824588Sml149210 	    (void *)bgep, regno, bits));
1831369Sdduvall 
1841369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
1851369Sdduvall 
1861369Sdduvall 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
1874588Sml149210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
1881369Sdduvall 
1891369Sdduvall 	regval &= ~bits;
1901369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
1911369Sdduvall }
1921369Sdduvall 
1931369Sdduvall #endif	/* BGE_CFG_IO8 */
1941369Sdduvall 
1951369Sdduvall static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
1961369Sdduvall #pragma	inline(bge_cfg_clr32)
1971369Sdduvall 
1981369Sdduvall static void
bge_cfg_clr32(bge_t * bgep,bge_regno_t regno,uint32_t bits)1991369Sdduvall bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2001369Sdduvall {
2011369Sdduvall 	uint32_t regval;
2021369Sdduvall 
2031369Sdduvall 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
2044588Sml149210 	    (void *)bgep, regno, bits));
2051369Sdduvall 
2061369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2071369Sdduvall 
2081369Sdduvall 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2094588Sml149210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
2101369Sdduvall 
2111369Sdduvall 	regval &= ~bits;
2121369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2131369Sdduvall }
2141369Sdduvall 
2151369Sdduvall #if	BGE_IND_IO32
2161369Sdduvall 
2171369Sdduvall /*
2181369Sdduvall  * Indirect access to registers & RISC scratchpads, using config space
2191369Sdduvall  * accesses only.
2201369Sdduvall  *
2211369Sdduvall  * This isn't currently used, but someday we might want to use it for
2221369Sdduvall  * restoring the Subsystem Device/Vendor registers (which aren't directly
2231369Sdduvall  * writable in Config Space), or for downloading firmware into the RISCs
2241369Sdduvall  *
2251369Sdduvall  * In any case there are endian issues to be resolved before this code is
2261369Sdduvall  * enabled; the bizarre way that bytes get twisted by this chip AND by
2271369Sdduvall  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
2281369Sdduvall  * it's been thoroughly tested for all access sizes on all supported
2291369Sdduvall  * architectures (SPARC *and* x86!).
2301369Sdduvall  */
2313918Sml149210 uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
2321369Sdduvall #pragma	inline(bge_ind_get32)
2331369Sdduvall 
2343918Sml149210 uint32_t
bge_ind_get32(bge_t * bgep,bge_regno_t regno)2351369Sdduvall bge_ind_get32(bge_t *bgep, bge_regno_t regno)
2361369Sdduvall {
2371369Sdduvall 	uint32_t val;
2381369Sdduvall 
2391369Sdduvall 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
2401369Sdduvall 
24111968SYong.Tan@Sun.COM #ifdef __sparc
24211968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
24311968SYong.Tan@Sun.COM 		regno = LE_32(regno);
24411968SYong.Tan@Sun.COM #endif
2451369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
2461369Sdduvall 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
2471369Sdduvall 
2481369Sdduvall 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
2494588Sml149210 	    (void *)bgep, regno, val));
2501369Sdduvall 
2513918Sml149210 	val = LE_32(val);
2523918Sml149210 
2531369Sdduvall 	return (val);
2541369Sdduvall }
2551369Sdduvall 
2563918Sml149210 void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
2571369Sdduvall #pragma	inline(bge_ind_put32)
2581369Sdduvall 
2593918Sml149210 void
bge_ind_put32(bge_t * bgep,bge_regno_t regno,uint32_t val)2601369Sdduvall bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
2611369Sdduvall {
2621369Sdduvall 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
2634588Sml149210 	    (void *)bgep, regno, val));
2641369Sdduvall 
2653918Sml149210 	val = LE_32(val);
26611968SYong.Tan@Sun.COM #ifdef __sparc
26711968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
26811968SYong.Tan@Sun.COM 		regno = LE_32(regno);
26911968SYong.Tan@Sun.COM #endif
2701369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
2711369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
2721369Sdduvall }
2731369Sdduvall 
2741369Sdduvall #endif	/* BGE_IND_IO32 */
2751369Sdduvall 
2761369Sdduvall #if	BGE_DEBUGGING
2771369Sdduvall 
2781369Sdduvall static void bge_pci_check(bge_t *bgep);
2791369Sdduvall #pragma	no_inline(bge_pci_check)
2801369Sdduvall 
2811369Sdduvall static void
bge_pci_check(bge_t * bgep)2821369Sdduvall bge_pci_check(bge_t *bgep)
2831369Sdduvall {
2841369Sdduvall 	uint16_t pcistatus;
2851369Sdduvall 
2861369Sdduvall 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
2871369Sdduvall 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
2881369Sdduvall 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
2894588Sml149210 		    (void *)bgep, pcistatus));
2901369Sdduvall }
2911369Sdduvall 
2921369Sdduvall #endif	/* BGE_DEBUGGING */
2931369Sdduvall 
2941369Sdduvall /*
2951369Sdduvall  * Perform first-stage chip (re-)initialisation, using only config-space
2961369Sdduvall  * accesses:
2971369Sdduvall  *
2981369Sdduvall  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
2991369Sdduvall  *   returning the data in the structure pointed to by <idp>.
3001369Sdduvall  * + Configure the target-mode endianness (swap) options.
3011369Sdduvall  * + Disable interrupts and enable Memory Space accesses.
3021369Sdduvall  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
3031369Sdduvall  *
3041369Sdduvall  * This sequence is adapted from Broadcom document 570X-PG102-R,
3051369Sdduvall  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
3061369Sdduvall  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
3071369Sdduvall  *
3081369Sdduvall  * This function MUST be called before any non-config-space accesses
3091369Sdduvall  * are made; on this first call <enable_dma> is B_FALSE, and it
3101369Sdduvall  * effectively performs steps 3-1(!) of the initialisation sequence
3111369Sdduvall  * (the rest are not required but should be harmless).
3121369Sdduvall  *
3132135Szh199473  * It MUST also be called after a chip reset, as this disables
3141369Sdduvall  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
3151369Sdduvall  * it is effectively performing steps 6-8.
3161369Sdduvall  */
3171369Sdduvall void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
3181369Sdduvall #pragma	no_inline(bge_chip_cfg_init)
3191369Sdduvall 
3201369Sdduvall void
bge_chip_cfg_init(bge_t * bgep,chip_id_t * cidp,boolean_t enable_dma)3211369Sdduvall bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
3221369Sdduvall {
3231369Sdduvall 	ddi_acc_handle_t handle;
3241369Sdduvall 	uint16_t command;
3251369Sdduvall 	uint32_t mhcr;
3261369Sdduvall 	uint16_t value16;
3271369Sdduvall 	int i;
3281369Sdduvall 
3291369Sdduvall 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
3304588Sml149210 	    (void *)bgep, (void *)cidp, enable_dma));
3311369Sdduvall 
3321369Sdduvall 	/*
3331369Sdduvall 	 * Step 3: save PCI cache line size and subsystem vendor ID
3341369Sdduvall 	 *
3351369Sdduvall 	 * Read all the config-space registers that characterise the
3361369Sdduvall 	 * chip, specifically vendor/device/revision/subsystem vendor
3371369Sdduvall 	 * and subsystem device id.  We expect (but don't check) that
3381369Sdduvall 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
3391369Sdduvall 	 *
3402135Szh199473 	 * Also save all bus-transaction related registers (cache-line
3411369Sdduvall 	 * size, bus-grant/latency parameters, etc).  Some of these are
3421369Sdduvall 	 * cleared by reset, so we'll have to restore them later.  This
3431369Sdduvall 	 * comes from the Broadcom document 570X-PG102-R ...
3441369Sdduvall 	 *
3451369Sdduvall 	 * Note: Broadcom document 570X-PG102-R seems to be in error
3461369Sdduvall 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
3471369Sdduvall 	 * Subsystem (Device) ID registers, which are the opposite way
3481369Sdduvall 	 * round according to the PCI standard.  For good measure, we
3491369Sdduvall 	 * save/restore both anyway.
3501369Sdduvall 	 */
3511369Sdduvall 	handle = bgep->cfg_handle;
3521369Sdduvall 
35311968SYong.Tan@Sun.COM 	/*
35411968SYong.Tan@Sun.COM 	 * For some chipsets (e.g., BCM5718), if MHCR_ENABLE_ENDIAN_BYTE_SWAP
35511968SYong.Tan@Sun.COM 	 * has been set in PCI_CONF_COMM already, we need to write the
35611968SYong.Tan@Sun.COM 	 * byte-swapped value to it. So we just write zero first for simplicity.
35711968SYong.Tan@Sun.COM 	 */
35811968SYong.Tan@Sun.COM 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
35911968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
36011968SYong.Tan@Sun.COM 		pci_config_put32(handle, PCI_CONF_BGE_MHCR, 0);
3611369Sdduvall 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
3621369Sdduvall 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
3631369Sdduvall 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
3641369Sdduvall 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
3651369Sdduvall 
3661369Sdduvall 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
3671369Sdduvall 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
3681369Sdduvall 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
3691369Sdduvall 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
3701369Sdduvall 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
3711369Sdduvall 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
3721369Sdduvall 
3731369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
3744588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
3754588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
3764588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
3774588Sml149210 	    cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
3781369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
3794588Sml149210 	    cidp->vendor, cidp->device, cidp->revision));
3801369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
3814588Sml149210 	    cidp->subven, cidp->subdev, cidp->asic_rev));
3821369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
3834588Sml149210 	    cidp->clsize, cidp->latency, cidp->command));
3841369Sdduvall 
3851369Sdduvall 	/*
3861369Sdduvall 	 * Step 2 (also step 6): disable and clear interrupts.
3871369Sdduvall 	 * Steps 11-13: configure PIO endianness options, and enable
3881369Sdduvall 	 * indirect register access.  We'll also select any other
3892135Szh199473 	 * options controlled by the MHCR (e.g. tagged status, mask
3901369Sdduvall 	 * interrupt mode) at this stage ...
3911369Sdduvall 	 *
3921369Sdduvall 	 * Note: internally, the chip is 64-bit and BIG-endian, but
3931369Sdduvall 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
3941369Sdduvall 	 * it normally swaps bytes around at the PCI interface.
3951369Sdduvall 	 * However, the PCI host bridge on SPARC systems normally
3961369Sdduvall 	 * swaps the byte lanes around too, since SPARCs are also
3971369Sdduvall 	 * BIG-endian.  So it turns out that on SPARC, the right
3981369Sdduvall 	 * option is to tell the chip to swap (and the host bridge
3991369Sdduvall 	 * will swap back again), whereas on x86 we ask the chip
4001369Sdduvall 	 * NOT to swap, so the natural little-endianness of the
4011369Sdduvall 	 * PCI bus is assumed.  Then the only thing that doesn't
4021369Sdduvall 	 * automatically work right is access to an 8-byte register
4031369Sdduvall 	 * by a little-endian host; but we don't want to set the
4041369Sdduvall 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
4051369Sdduvall 	 * accesses don't go where expected ;-(  So we live with
4061369Sdduvall 	 * that, and perform word-swaps in software in the few cases
4071369Sdduvall 	 * where a chip register is defined as an 8-byte value --
4081369Sdduvall 	 * see the code below for details ...
4091369Sdduvall 	 *
4101369Sdduvall 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
4111369Sdduvall 	 * very clear in the register description in the PRM, but
4121369Sdduvall 	 * Broadcom document 570X-PG104-R page 248 explains a little
4131369Sdduvall 	 * more (under "Broadcom Mask Mode").  The bit changes the way
4141369Sdduvall 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
4151369Sdduvall 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
4161369Sdduvall 	 * way as the 5700 did, which isn't very convenient.  Setting
4171369Sdduvall 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
4181369Sdduvall 	 * bit do just what its name says -- MASK the PCI #INTA output
4191369Sdduvall 	 * (i.e. deassert the signal at the pin) leaving all internal
4201369Sdduvall 	 * state unchanged.  This is much more convenient for our
4211369Sdduvall 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
4221369Sdduvall 	 *
4231369Sdduvall 	 * Note: the inconvenient semantics of the interrupt mailbox
4241369Sdduvall 	 * (nonzero disables and acknowledges/clears the interrupt,
4251369Sdduvall 	 * zero enables AND CLEARS it) would make race conditions
4261369Sdduvall 	 * likely in the interrupt handler:
4271369Sdduvall 	 *
4281369Sdduvall 	 * (1)	acknowledge & disable interrupts
4291369Sdduvall 	 * (2)	while (more to do)
4301369Sdduvall 	 * 		process packets
4311369Sdduvall 	 * (3)	enable interrupts -- also clears pending
4321369Sdduvall 	 *
4331369Sdduvall 	 * If the chip received more packets and internally generated
4341369Sdduvall 	 * an interrupt between the check at (2) and the mbox write
4351369Sdduvall 	 * at (3), this interrupt would be lost :-(
4361369Sdduvall 	 *
4371369Sdduvall 	 * The best way to avoid this is to use TAGGED STATUS mode,
4381369Sdduvall 	 * where the chip includes a unique tag in each status block
4391369Sdduvall 	 * update, and the host, when re-enabling interrupts, passes
4401369Sdduvall 	 * the last tag it saw back to the chip; then the chip can
4411369Sdduvall 	 * see whether the host is truly up to date, and regenerate
4421369Sdduvall 	 * its interrupt if not.
4431369Sdduvall 	 */
4441369Sdduvall 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
4454588Sml149210 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
4464588Sml149210 	    MHCR_MASK_INTERRUPT_MODE |
4474588Sml149210 	    MHCR_CLEAR_INTERRUPT_INTA;
4481369Sdduvall 
4491369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
4501369Sdduvall 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
4511369Sdduvall 
4521369Sdduvall #ifdef	_BIG_ENDIAN
4531369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
4541369Sdduvall #endif	/* _BIG_ENDIAN */
4551369Sdduvall 
45611968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
45711968SYong.Tan@Sun.COM 		pci_config_put32(handle, PCI_CONF_BGE_MHCR, 0);
4581369Sdduvall 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
4591369Sdduvall 
4601408Srandyf #ifdef BGE_IPMI_ASF
4611408Srandyf 	bgep->asf_wordswapped = B_FALSE;
4621408Srandyf #endif
4631369Sdduvall 	/*
4641369Sdduvall 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
4651369Sdduvall 	 *			 Disable Memory Write/Invalidate
4661369Sdduvall 	 *			 Enable or disable Bus Mastering
4671369Sdduvall 	 *
4681369Sdduvall 	 * Note that all other bits are taken from the original value saved
4691369Sdduvall 	 * the first time through here, rather than from the current register
4701369Sdduvall 	 * value, 'cos that will have been cleared by a soft RESET since.
4711369Sdduvall 	 * In this way we preserve the OBP/nexus-parent's preferred settings
4721369Sdduvall 	 * of the parity-error and system-error enable bits across multiple
4731369Sdduvall 	 * chip RESETs.
4741369Sdduvall 	 */
4751369Sdduvall 	command = bgep->chipid.command | PCI_COMM_MAE;
4761369Sdduvall 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
4771369Sdduvall 	if (enable_dma)
4781369Sdduvall 		command |= PCI_COMM_ME;
4791369Sdduvall 	/*
4801369Sdduvall 	 * on BCM5714 revision A0, false parity error gets generated
4812135Szh199473 	 * due to a logic bug. Provide a workaround by disabling parity
4821369Sdduvall 	 * error.
4831369Sdduvall 	 */
4841369Sdduvall 	if (((cidp->device == DEVICE_ID_5714C) ||
4851369Sdduvall 	    (cidp->device == DEVICE_ID_5714S)) &&
4861369Sdduvall 	    (cidp->revision == REVISION_ID_5714_A0)) {
4871369Sdduvall 		command &= ~PCI_COMM_PARITY_DETECT;
4881369Sdduvall 	}
4891369Sdduvall 	pci_config_put16(handle, PCI_CONF_COMM, command);
4901369Sdduvall 
4911369Sdduvall 	/*
4921369Sdduvall 	 * On some PCI-E device, there were instances when
4931369Sdduvall 	 * the device was still link training.
4941369Sdduvall 	 */
4951369Sdduvall 	if (bgep->chipid.pci_type == BGE_PCI_E) {
4961369Sdduvall 		i = 0;
4971369Sdduvall 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
4981369Sdduvall 		while ((value16 != command) && (i < 100)) {
4991369Sdduvall 			drv_usecwait(200);
5001369Sdduvall 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
5011369Sdduvall 			++i;
5021369Sdduvall 		}
5031369Sdduvall 	}
5041369Sdduvall 
5051369Sdduvall 	/*
5061369Sdduvall 	 * Clear any remaining error status bits
5071369Sdduvall 	 */
5081369Sdduvall 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
5091369Sdduvall 
5101369Sdduvall 	/*
5112073Svivek 	 * Do following if and only if the device is NOT BCM5714C OR
5122073Svivek 	 * BCM5715C
5131369Sdduvall 	 */
5142073Svivek 	if (!((cidp->device == DEVICE_ID_5714C) ||
5154588Sml149210 	    (cidp->device == DEVICE_ID_5715C))) {
5162073Svivek 		/*
5172073Svivek 		 * Make sure these indirect-access registers are sane
5182073Svivek 		 * rather than random after power-up or reset
5192073Svivek 		 */
5202073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
5212073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
5222073Svivek 	}
5232135Szh199473 	/*
5242135Szh199473 	 * Step 8: Disable PCI-X/PCI-E Relaxed Ordering
5252135Szh199473 	 */
5262135Szh199473 	bge_cfg_clr16(bgep, PCIX_CONF_COMM, PCIX_COMM_RELAXED);
5272135Szh199473 
5289042SYong.Tan@Sun.COM 	if (cidp->pci_type == BGE_PCI_E) {
5299042SYong.Tan@Sun.COM 		if (DEVICE_5723_SERIES_CHIPSETS(bgep)) {
5309042SYong.Tan@Sun.COM 			bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL_5723,
5319042SYong.Tan@Sun.COM 			    DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
5329042SYong.Tan@Sun.COM 		} else
5339042SYong.Tan@Sun.COM 			bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL,
5349042SYong.Tan@Sun.COM 			    DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
5359042SYong.Tan@Sun.COM 	}
5361369Sdduvall }
5371369Sdduvall 
5381369Sdduvall #ifdef __amd64
5391369Sdduvall /*
5401369Sdduvall  * Distinguish CPU types
5411369Sdduvall  *
5421369Sdduvall  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
5431369Sdduvall  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
5441369Sdduvall  * for PCI-Express based network interface card. This is the work-around
5451369Sdduvall  * for those nics.
5461369Sdduvall  */
5471369Sdduvall static boolean_t bge_get_em64t_type(void);
5481369Sdduvall #pragma	inline(bge_get_em64t_type)
5491369Sdduvall 
5501369Sdduvall static boolean_t
bge_get_em64t_type(void)5511369Sdduvall bge_get_em64t_type(void)
5521369Sdduvall {
5531369Sdduvall 
5541369Sdduvall 	return (x86_vendor == X86_VENDOR_Intel);
5551369Sdduvall }
5561369Sdduvall #endif
5571369Sdduvall 
5581369Sdduvall /*
5591369Sdduvall  * Operating register get/set access routines
5601369Sdduvall  */
5611369Sdduvall 
5621369Sdduvall uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
5631369Sdduvall #pragma	inline(bge_reg_get32)
5641369Sdduvall 
5651369Sdduvall uint32_t
bge_reg_get32(bge_t * bgep,bge_regno_t regno)5661369Sdduvall bge_reg_get32(bge_t *bgep, bge_regno_t regno)
5671369Sdduvall {
5681369Sdduvall 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
5694588Sml149210 	    (void *)bgep, regno));
5701369Sdduvall 
5711369Sdduvall 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
5721369Sdduvall }
5731369Sdduvall 
5741369Sdduvall void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
5751369Sdduvall #pragma	inline(bge_reg_put32)
5761369Sdduvall 
5771369Sdduvall void
bge_reg_put32(bge_t * bgep,bge_regno_t regno,uint32_t data)5781369Sdduvall bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
5791369Sdduvall {
5801369Sdduvall 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
5814588Sml149210 	    (void *)bgep, regno, data));
5821369Sdduvall 
5831369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
5841369Sdduvall 	BGE_PCICHK(bgep);
5851369Sdduvall }
5861369Sdduvall 
5871369Sdduvall void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
5881369Sdduvall #pragma	inline(bge_reg_set32)
5891369Sdduvall 
5901369Sdduvall void
bge_reg_set32(bge_t * bgep,bge_regno_t regno,uint32_t bits)5911369Sdduvall bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
5921369Sdduvall {
5931369Sdduvall 	uint32_t regval;
5941369Sdduvall 
5951369Sdduvall 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
5964588Sml149210 	    (void *)bgep, regno, bits));
5971369Sdduvall 
5981369Sdduvall 	regval = bge_reg_get32(bgep, regno);
5991369Sdduvall 	regval |= bits;
6001369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6011369Sdduvall }
6021369Sdduvall 
6031369Sdduvall void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6041369Sdduvall #pragma	inline(bge_reg_clr32)
6051369Sdduvall 
6061369Sdduvall void
bge_reg_clr32(bge_t * bgep,bge_regno_t regno,uint32_t bits)6071369Sdduvall bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6081369Sdduvall {
6091369Sdduvall 	uint32_t regval;
6101369Sdduvall 
6111369Sdduvall 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
6124588Sml149210 	    (void *)bgep, regno, bits));
6131369Sdduvall 
6141369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6151369Sdduvall 	regval &= ~bits;
6161369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6171369Sdduvall }
6181369Sdduvall 
6191369Sdduvall static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
6201369Sdduvall #pragma	inline(bge_reg_get64)
6211369Sdduvall 
6221369Sdduvall static uint64_t
bge_reg_get64(bge_t * bgep,bge_regno_t regno)6231369Sdduvall bge_reg_get64(bge_t *bgep, bge_regno_t regno)
6241369Sdduvall {
6251369Sdduvall 	uint64_t regval;
6261369Sdduvall 
6271369Sdduvall #ifdef	__amd64
62811968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type() ||
62911968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
6301369Sdduvall 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
6311369Sdduvall 		regval <<= 32;
6321369Sdduvall 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
6331369Sdduvall 	} else {
6341369Sdduvall 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6351369Sdduvall 	}
6369042SYong.Tan@Sun.COM #elif defined(__sparc)
63711968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
63811968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
6399042SYong.Tan@Sun.COM 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
6409042SYong.Tan@Sun.COM 		regval <<= 32;
6419042SYong.Tan@Sun.COM 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
6429042SYong.Tan@Sun.COM 	} else {
6439042SYong.Tan@Sun.COM 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6449042SYong.Tan@Sun.COM 	}
6451369Sdduvall #else
6461369Sdduvall 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6471369Sdduvall #endif
6481369Sdduvall 
6491369Sdduvall #ifdef	_LITTLE_ENDIAN
6501369Sdduvall 	regval = (regval >> 32) | (regval << 32);
6511369Sdduvall #endif	/* _LITTLE_ENDIAN */
6521369Sdduvall 
6531369Sdduvall 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
6544588Sml149210 	    (void *)bgep, regno, regval));
6551369Sdduvall 
6561369Sdduvall 	return (regval);
6571369Sdduvall }
6581369Sdduvall 
6591369Sdduvall static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
6601369Sdduvall #pragma	inline(bge_reg_put64)
6611369Sdduvall 
6621369Sdduvall static void
bge_reg_put64(bge_t * bgep,bge_regno_t regno,uint64_t data)6631369Sdduvall bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
6641369Sdduvall {
6651369Sdduvall 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
6664588Sml149210 	    (void *)bgep, regno, data));
6671369Sdduvall 
6681369Sdduvall #ifdef	_LITTLE_ENDIAN
6691369Sdduvall 	data = ((data >> 32) | (data << 32));
6701369Sdduvall #endif	/* _LITTLE_ENDIAN */
6711369Sdduvall 
6721369Sdduvall #ifdef	__amd64
67311968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type() ||
67411968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
6751369Sdduvall 		ddi_put32(bgep->io_handle,
6764588Sml149210 		    PIO_ADDR(bgep, regno), (uint32_t)data);
6771369Sdduvall 		BGE_PCICHK(bgep);
6781369Sdduvall 		ddi_put32(bgep->io_handle,
6794588Sml149210 		    PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
6801369Sdduvall 
6811369Sdduvall 	} else {
6821369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6831369Sdduvall 	}
6849042SYong.Tan@Sun.COM #elif defined(__sparc)
68511968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
68611968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
6879042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle,
6889042SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, regno + 4), (uint32_t)data);
6899042SYong.Tan@Sun.COM 		BGE_PCICHK(bgep);
6909042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle,
6919042SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, regno), (uint32_t)(data >> 32));
6929042SYong.Tan@Sun.COM 	} else {
6939042SYong.Tan@Sun.COM 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6949042SYong.Tan@Sun.COM 	}
6951369Sdduvall #else
6961369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6971369Sdduvall #endif
6981369Sdduvall 
6991369Sdduvall 	BGE_PCICHK(bgep);
7001369Sdduvall }
7011369Sdduvall 
7021369Sdduvall /*
7031369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
7041369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
7051369Sdduvall  */
7061369Sdduvall static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
7071369Sdduvall #pragma	inline(bge_reg_putrcb)
7081369Sdduvall 
7091369Sdduvall static void
bge_reg_putrcb(bge_t * bgep,bge_regno_t addr,bge_rcb_t * rcbp)7101369Sdduvall bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
7111369Sdduvall {
7121369Sdduvall 	uint64_t *p;
7131369Sdduvall 
7141369Sdduvall 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
7154588Sml149210 	    (void *)bgep, addr, rcbp->host_ring_addr,
7164588Sml149210 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
7171369Sdduvall 
7181369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
7191369Sdduvall 
7201369Sdduvall 	p = (void *)rcbp;
7211369Sdduvall 	bge_reg_put64(bgep, addr, *p++);
7221369Sdduvall 	bge_reg_put64(bgep, addr+8, *p);
7231369Sdduvall }
7241369Sdduvall 
7251369Sdduvall void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
7261369Sdduvall #pragma	inline(bge_mbx_put)
7271369Sdduvall 
7281369Sdduvall void
bge_mbx_put(bge_t * bgep,bge_regno_t regno,uint64_t data)7291369Sdduvall bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
7301369Sdduvall {
7317678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
7327678SYong.Tan@Sun.COM 		regno += INTERRUPT_LP_MBOX_0_REG - INTERRUPT_MBOX_0_REG + 4;
7337678SYong.Tan@Sun.COM 
7341369Sdduvall 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
7354588Sml149210 	    (void *)bgep, regno, data));
7361369Sdduvall 
7371369Sdduvall 	/*
7381369Sdduvall 	 * Mailbox registers are nominally 64 bits on the 5701, but
7391369Sdduvall 	 * the MSW isn't used.  On the 5703, they're only 32 bits
7401369Sdduvall 	 * anyway.  So here we just write the lower(!) 32 bits -
7411369Sdduvall 	 * remembering that the chip is big-endian, even though the
7421369Sdduvall 	 * PCI bus is little-endian ...
7431369Sdduvall 	 */
7441369Sdduvall #ifdef	_BIG_ENDIAN
7451369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
7461369Sdduvall #else
7471369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
7481369Sdduvall #endif	/* _BIG_ENDIAN */
7491369Sdduvall 	BGE_PCICHK(bgep);
7501369Sdduvall }
7511369Sdduvall 
7526546Sgh162552 uint32_t bge_mbx_get(bge_t *bgep, bge_regno_t regno);
7536546Sgh162552 #pragma inline(bge_mbx_get)
7546546Sgh162552 
7556546Sgh162552 uint32_t
bge_mbx_get(bge_t * bgep,bge_regno_t regno)7566546Sgh162552 bge_mbx_get(bge_t *bgep, bge_regno_t regno)
7576546Sgh162552 {
7586546Sgh162552 	uint32_t val32;
7596546Sgh162552 
7607678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
7617678SYong.Tan@Sun.COM 		regno += INTERRUPT_LP_MBOX_0_REG - INTERRUPT_MBOX_0_REG + 4;
7627678SYong.Tan@Sun.COM 
7636546Sgh162552 	BGE_TRACE(("bge_mbx_get($%p, 0x%lx)",
7646546Sgh162552 	    (void *)bgep, regno));
7656546Sgh162552 
7666546Sgh162552 #ifdef	_BIG_ENDIAN
7676546Sgh162552 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno+4));
7686546Sgh162552 #else
7696546Sgh162552 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
7706546Sgh162552 #endif	/* _BIG_ENDIAN */
7716546Sgh162552 	BGE_PCICHK(bgep);
7726546Sgh162552 
77311968SYong.Tan@Sun.COM 	BGE_DEBUG(("bge_mbx_get($%p, 0x%lx) => 0x%08x",
77411968SYong.Tan@Sun.COM 	    (void *)bgep, regno, val32));
77511968SYong.Tan@Sun.COM 
7766546Sgh162552 	return (val32);
7776546Sgh162552 }
7786546Sgh162552 
7796546Sgh162552 
7801369Sdduvall #if	BGE_DEBUGGING
7811369Sdduvall 
7821369Sdduvall void bge_led_mark(bge_t *bgep);
7831369Sdduvall #pragma	no_inline(bge_led_mark)
7841369Sdduvall 
7851369Sdduvall void
bge_led_mark(bge_t * bgep)7861369Sdduvall bge_led_mark(bge_t *bgep)
7871369Sdduvall {
7881369Sdduvall 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
7894588Sml149210 	    LED_CONTROL_1000MBPS_LED |
7904588Sml149210 	    LED_CONTROL_100MBPS_LED |
7914588Sml149210 	    LED_CONTROL_10MBPS_LED;
7921369Sdduvall 
7931369Sdduvall 	/*
7941369Sdduvall 	 * Blink all three LINK LEDs on simultaneously, then all off,
7951369Sdduvall 	 * then restore to automatic hardware control.  This is used
7961369Sdduvall 	 * in laboratory testing to trigger a logic analyser or scope.
7971369Sdduvall 	 */
7981369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
7991369Sdduvall 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
8001369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8011369Sdduvall 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
8021369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8031369Sdduvall }
8041369Sdduvall 
8051369Sdduvall #endif	/* BGE_DEBUGGING */
8061369Sdduvall 
8071369Sdduvall /*
8081369Sdduvall  * NIC on-chip memory access routines
8091369Sdduvall  *
8101369Sdduvall  * Only 32K of NIC memory is visible at a time, controlled by the
8111369Sdduvall  * Memory Window Base Address Register (in PCI config space).  Once
8121369Sdduvall  * this is set, the 32K region of NIC-local memory that it refers
8131369Sdduvall  * to can be directly addressed in the upper 32K of the 64K of PCI
8141369Sdduvall  * memory space used for the device.
8151369Sdduvall  */
8161369Sdduvall 
8171369Sdduvall static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
8181369Sdduvall #pragma	inline(bge_nic_setwin)
8191369Sdduvall 
8201369Sdduvall static void
bge_nic_setwin(bge_t * bgep,bge_regno_t base)8211369Sdduvall bge_nic_setwin(bge_t *bgep, bge_regno_t base)
8221369Sdduvall {
8232073Svivek 	chip_id_t *cidp;
8242073Svivek 
8251369Sdduvall 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
8264588Sml149210 	    (void *)bgep, base));
8271369Sdduvall 
8281369Sdduvall 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
8292073Svivek 
8302073Svivek 	/*
8312073Svivek 	 * Don't do repeated zero data writes,
8322073Svivek 	 * if the device is BCM5714C/15C.
8332073Svivek 	 */
8342073Svivek 	cidp = &bgep->chipid;
8352073Svivek 	if ((cidp->device == DEVICE_ID_5714C) ||
8364588Sml149210 	    (cidp->device == DEVICE_ID_5715C)) {
8372073Svivek 		if (bgep->lastWriteZeroData && (base == (bge_regno_t)0))
8382073Svivek 			return;
8392073Svivek 		/* Adjust lastWriteZeroData */
8402073Svivek 		bgep->lastWriteZeroData = ((base == (bge_regno_t)0) ?
8414588Sml149210 		    B_TRUE : B_FALSE);
8422073Svivek 	}
84311968SYong.Tan@Sun.COM #ifdef __sparc
84411968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
84511968SYong.Tan@Sun.COM 		base = LE_32(base);
84611968SYong.Tan@Sun.COM #endif
8471369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
8481369Sdduvall }
8491369Sdduvall 
8501369Sdduvall static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
8511369Sdduvall #pragma	inline(bge_nic_get32)
8521369Sdduvall 
8531369Sdduvall static uint32_t
bge_nic_get32(bge_t * bgep,bge_regno_t addr)8541369Sdduvall bge_nic_get32(bge_t *bgep, bge_regno_t addr)
8551369Sdduvall {
8561369Sdduvall 	uint32_t data;
8571369Sdduvall 
8583918Sml149210 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
8591408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8601408Srandyf 		/* workaround for word swap error */
8611408Srandyf 		if (addr & 4)
8621408Srandyf 			addr = addr - 4;
8631408Srandyf 		else
8641408Srandyf 			addr = addr + 4;
8651408Srandyf 	}
8661408Srandyf #endif
8671408Srandyf 
8683918Sml149210 #ifdef __sparc
8693918Sml149210 	data = bge_nic_read32(bgep, addr);
8703918Sml149210 #else
8711369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
8721369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
8731369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
8741369Sdduvall 
8751369Sdduvall 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
8763918Sml149210 #endif
8771369Sdduvall 
8781369Sdduvall 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
8794588Sml149210 	    (void *)bgep, addr, data));
8801369Sdduvall 
8811369Sdduvall 	return (data);
8821369Sdduvall }
8831369Sdduvall 
8841408Srandyf void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
8851408Srandyf #pragma inline(bge_nic_put32)
8861408Srandyf 
8871408Srandyf void
bge_nic_put32(bge_t * bgep,bge_regno_t addr,uint32_t data)8881369Sdduvall bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
8891369Sdduvall {
8901369Sdduvall 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
8914588Sml149210 	    (void *)bgep, addr, data));
8921369Sdduvall 
8933918Sml149210 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
8941408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8951408Srandyf 		/* workaround for word swap error */
8961408Srandyf 		if (addr & 4)
8971408Srandyf 			addr = addr - 4;
8981408Srandyf 		else
8991408Srandyf 			addr = addr + 4;
9001408Srandyf 	}
9011408Srandyf #endif
9021408Srandyf 
9033918Sml149210 #ifdef __sparc
90411968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
90511968SYong.Tan@Sun.COM 		addr = LE_32(addr);
9063918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
9073918Sml149210 	data = LE_32(data);
9083918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR, data);
9093918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
9103918Sml149210 #else
9111369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9121369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9131369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9141369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9151369Sdduvall 	BGE_PCICHK(bgep);
9163918Sml149210 #endif
9171369Sdduvall }
9181369Sdduvall 
9191369Sdduvall static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
9201369Sdduvall #pragma	inline(bge_nic_get64)
9211369Sdduvall 
9221369Sdduvall static uint64_t
bge_nic_get64(bge_t * bgep,bge_regno_t addr)9231369Sdduvall bge_nic_get64(bge_t *bgep, bge_regno_t addr)
9241369Sdduvall {
9251369Sdduvall 	uint64_t data;
9261369Sdduvall 
9271369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9281369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9291369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9301369Sdduvall 
9311369Sdduvall #ifdef	__amd64
93211968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type() ||
93311968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
93411968SYong.Tan@Sun.COM 		data = ddi_get32(bgep->io_handle,
93511968SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr + 4));
93611968SYong.Tan@Sun.COM 		data <<= 32;
93711968SYong.Tan@Sun.COM 		data |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
93811968SYong.Tan@Sun.COM 	} else {
93912331SYong.Tan@Sun.COM 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
94011968SYong.Tan@Sun.COM 	}
9419042SYong.Tan@Sun.COM #elif defined(__sparc)
94211968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
94311968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
94411968SYong.Tan@Sun.COM 		data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
94511968SYong.Tan@Sun.COM 		data <<= 32;
94611968SYong.Tan@Sun.COM 		data |= ddi_get32(bgep->io_handle,
94711968SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr + 4));
94811968SYong.Tan@Sun.COM 	} else {
94911968SYong.Tan@Sun.COM 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
95012331SYong.Tan@Sun.COM 	}
9511369Sdduvall #else
95212331SYong.Tan@Sun.COM 	data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9531369Sdduvall #endif
9541369Sdduvall 
9551369Sdduvall 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
9564588Sml149210 	    (void *)bgep, addr, data));
9571369Sdduvall 
9581369Sdduvall 	return (data);
9591369Sdduvall }
9601369Sdduvall 
9611369Sdduvall static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
9621369Sdduvall #pragma	inline(bge_nic_put64)
9631369Sdduvall 
9641369Sdduvall static void
bge_nic_put64(bge_t * bgep,bge_regno_t addr,uint64_t data)9651369Sdduvall bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
9661369Sdduvall {
9671369Sdduvall 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
9684588Sml149210 	    (void *)bgep, addr, data));
9691369Sdduvall 
9701369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9711369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9721369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9731369Sdduvall 
9741369Sdduvall #ifdef	__amd64
97511968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type() ||
97611968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
9771369Sdduvall 		ddi_put32(bgep->io_handle,
97811968SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr + 4), (uint32_t)data);
9791369Sdduvall 		BGE_PCICHK(bgep);
9801369Sdduvall 		ddi_put32(bgep->io_handle,
98111968SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr), (uint32_t)(data >> 32));
9821369Sdduvall 	} else {
9831369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9841369Sdduvall 	}
9859042SYong.Tan@Sun.COM #elif defined(__sparc)
98611968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
98711968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
9889042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle,
9899042SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr + 4), (uint32_t)data);
9909042SYong.Tan@Sun.COM 		BGE_PCICHK(bgep);
9919042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle,
9929042SYong.Tan@Sun.COM 		    PIO_ADDR(bgep, addr), (uint32_t)(data >> 32));
9939042SYong.Tan@Sun.COM 	} else {
9949042SYong.Tan@Sun.COM 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9959042SYong.Tan@Sun.COM 	}
9961369Sdduvall #else
9971369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9981369Sdduvall #endif
9991369Sdduvall 
10001369Sdduvall 	BGE_PCICHK(bgep);
10011369Sdduvall }
10021369Sdduvall 
10031369Sdduvall /*
10041369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
10051369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
10061369Sdduvall  */
10071369Sdduvall static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
10081369Sdduvall #pragma	inline(bge_nic_putrcb)
10091369Sdduvall 
10101369Sdduvall static void
bge_nic_putrcb(bge_t * bgep,bge_regno_t addr,bge_rcb_t * rcbp)10111369Sdduvall bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
10121369Sdduvall {
10131369Sdduvall 	uint64_t *p;
10141369Sdduvall 
10151369Sdduvall 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
10164588Sml149210 	    (void *)bgep, addr, rcbp->host_ring_addr,
10174588Sml149210 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
10181369Sdduvall 
10191369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
10201369Sdduvall 
10211369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10221369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10231369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10241369Sdduvall 
10251369Sdduvall 	p = (void *)rcbp;
10261369Sdduvall #ifdef	__amd64
102711968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) || bge_get_em64t_type() ||
102811968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
10291369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
10304588Sml149210 		    (uint32_t)(*p));
10311369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
103211968SYong.Tan@Sun.COM 		    (uint32_t)(*p++ >> 32));
10331369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
103411968SYong.Tan@Sun.COM 		    (uint32_t)(*p));
10351369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
10364588Sml149210 		    (uint32_t)(*p >> 32));
10371369Sdduvall 
10381369Sdduvall 	} else {
10391369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10401369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
10411369Sdduvall 	}
10429042SYong.Tan@Sun.COM #elif defined(__sparc)
104311968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
104411968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep)) {
10459042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
10469042SYong.Tan@Sun.COM 		    (uint32_t)(*p));
10479042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
104811968SYong.Tan@Sun.COM 		    (uint32_t)(*p++ >> 32));
10499042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
105011968SYong.Tan@Sun.COM 		    (uint32_t)(*p));
10519042SYong.Tan@Sun.COM 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
10529042SYong.Tan@Sun.COM 		    (uint32_t)(*p >> 32));
10539042SYong.Tan@Sun.COM 	} else {
10549042SYong.Tan@Sun.COM 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10559042SYong.Tan@Sun.COM 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
10569042SYong.Tan@Sun.COM 	}
10571369Sdduvall #else
10581369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10591369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
10601369Sdduvall #endif
10611369Sdduvall 
10621369Sdduvall 	BGE_PCICHK(bgep);
10631369Sdduvall }
10641369Sdduvall 
10651369Sdduvall static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
10661369Sdduvall #pragma	inline(bge_nic_zero)
10671369Sdduvall 
10681369Sdduvall static void
bge_nic_zero(bge_t * bgep,bge_regno_t addr,uint32_t nbytes)10691369Sdduvall bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
10701369Sdduvall {
10711369Sdduvall 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
10724588Sml149210 	    (void *)bgep, addr, nbytes));
10731369Sdduvall 
10741369Sdduvall 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
10754588Sml149210 	    ((addr+nbytes) & ~MWBAR_GRANULE_MASK));
10761369Sdduvall 
10771369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10781369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10791369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10801369Sdduvall 
10811369Sdduvall 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
10824588Sml149210 	    nbytes, 1, DDI_DATA_SZ08_ACC);
10831369Sdduvall 	BGE_PCICHK(bgep);
10841369Sdduvall }
10851369Sdduvall 
10861369Sdduvall /*
10871369Sdduvall  * MII (PHY) register get/set access routines
10881369Sdduvall  *
10891369Sdduvall  * These use the chip's MII auto-access method, controlled by the
10901369Sdduvall  * MII Communication register at 0x044c, so the CPU doesn't have
10911369Sdduvall  * to fiddle with the individual bits.
10921369Sdduvall  */
10931369Sdduvall 
10941369Sdduvall #undef	BGE_DBG
10951369Sdduvall #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
10961369Sdduvall 
10971369Sdduvall static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
10981369Sdduvall 				uint16_t data, uint32_t cmd);
10991369Sdduvall #pragma	no_inline(bge_mii_access)
11001369Sdduvall 
11011369Sdduvall static uint16_t
bge_mii_access(bge_t * bgep,bge_regno_t regno,uint16_t data,uint32_t cmd)11021369Sdduvall bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
11031369Sdduvall {
11041369Sdduvall 	uint32_t timeout;
11051369Sdduvall 	uint32_t regval1;
11061369Sdduvall 	uint32_t regval2;
11071369Sdduvall 
11081369Sdduvall 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
11094588Sml149210 	    (void *)bgep, regno, data, cmd));
11101369Sdduvall 
11111369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11121369Sdduvall 
11131369Sdduvall 	/*
11141369Sdduvall 	 * Assemble the command ...
11151369Sdduvall 	 */
11161369Sdduvall 	cmd |= data << MI_COMMS_DATA_SHIFT;
11171369Sdduvall 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
11181369Sdduvall 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
11191369Sdduvall 	cmd |= MI_COMMS_START;
11201369Sdduvall 
11211369Sdduvall 	/*
11221369Sdduvall 	 * Wait for any command already in progress ...
11231369Sdduvall 	 *
11241369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
11251369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
11261369Sdduvall 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
11271369Sdduvall 	 * bit set here -- it seems that the chip can initiate MII
11281369Sdduvall 	 * accesses internally, even with polling OFF.
11291369Sdduvall 	 */
11301369Sdduvall 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11311865Sdilpreet 	for (timeout = 100; ; ) {
11321369Sdduvall 		if ((regval2 & MI_COMMS_START) == 0) {
11331369Sdduvall 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
11341369Sdduvall 			break;
11351369Sdduvall 		}
11361369Sdduvall 		if (--timeout == 0)
11371369Sdduvall 			break;
11381369Sdduvall 		drv_usecwait(10);
11391369Sdduvall 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11401369Sdduvall 	}
11411369Sdduvall 
11421865Sdilpreet 	if (timeout == 0)
11431865Sdilpreet 		return ((uint16_t)~0u);
11441865Sdilpreet 
11451865Sdilpreet 	if (timeout != 100)
11461369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11474588Sml149210 		    "MI_COMMS_START set for %d us; 0x%x->0x%x",
11484588Sml149210 		    cmd, 10*(100-timeout), regval1, regval2));
11491369Sdduvall 
11501369Sdduvall 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11511369Sdduvall 	for (timeout = 1000; ; ) {
11521369Sdduvall 		if ((regval1 & MI_COMMS_START) == 0)
11531369Sdduvall 			break;
11541369Sdduvall 		if (--timeout == 0)
11551369Sdduvall 			break;
11561369Sdduvall 		drv_usecwait(10);
11571369Sdduvall 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11581369Sdduvall 	}
11591369Sdduvall 
11601369Sdduvall 	/*
11611369Sdduvall 	 * Drop out early if the READ FAILED bit is set -- this chip
11621369Sdduvall 	 * could be a 5703/4S, with a SerDes instead of a PHY!
11631369Sdduvall 	 */
11641369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11651369Sdduvall 		return ((uint16_t)~0u);
11661369Sdduvall 
11671369Sdduvall 	if (timeout == 0)
11681369Sdduvall 		return ((uint16_t)~0u);
11691369Sdduvall 
11701369Sdduvall 	/*
11711369Sdduvall 	 * The PRM says to wait 5us after seeing the START bit clear
11721369Sdduvall 	 * and then re-read the register to get the final value of the
11731369Sdduvall 	 * data field, in order to avoid a race condition where the
11741369Sdduvall 	 * START bit is clear but the data field isn't yet valid.
11751369Sdduvall 	 *
11761369Sdduvall 	 * Note: we don't actually seem to be encounter this race;
11771369Sdduvall 	 * except when the START bit is seen set again (see below),
11781369Sdduvall 	 * the data field doesn't change during this 5us interval.
11791369Sdduvall 	 */
11801369Sdduvall 	drv_usecwait(5);
11811369Sdduvall 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11821369Sdduvall 
11831369Sdduvall 	/*
11841369Sdduvall 	 * Unfortunately, when following the PRMs instructions above,
11851369Sdduvall 	 * we have occasionally seen the START bit set again(!) in the
11861369Sdduvall 	 * value read after the 5us delay. This seems to be due to the
11871369Sdduvall 	 * chip autonomously starting another MII access internally.
11881369Sdduvall 	 * In such cases, the command/data/etc fields relate to the
11891369Sdduvall 	 * internal command, rather than the one that we thought had
11901369Sdduvall 	 * just finished.  So in this case, we fall back to returning
11911369Sdduvall 	 * the data from the original read that showed START clear.
11921369Sdduvall 	 */
11931369Sdduvall 	if (regval2 & MI_COMMS_START) {
11941369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11954588Sml149210 		    "MI_COMMS_START set after transaction; 0x%x->0x%x",
11964588Sml149210 		    cmd, regval1, regval2));
11971369Sdduvall 		regval2 = regval1;
11981369Sdduvall 	}
11991369Sdduvall 
12001369Sdduvall 	if (regval2 & MI_COMMS_START)
12011369Sdduvall 		return ((uint16_t)~0u);
12021369Sdduvall 
12031369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
12041369Sdduvall 		return ((uint16_t)~0u);
12051369Sdduvall 
12061369Sdduvall 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
12071369Sdduvall }
12081369Sdduvall 
12091369Sdduvall uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
12101369Sdduvall #pragma	no_inline(bge_mii_get16)
12111369Sdduvall 
12121369Sdduvall uint16_t
bge_mii_get16(bge_t * bgep,bge_regno_t regno)12131369Sdduvall bge_mii_get16(bge_t *bgep, bge_regno_t regno)
12141369Sdduvall {
12151369Sdduvall 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
12164588Sml149210 	    (void *)bgep, regno));
12171369Sdduvall 
12181369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12191369Sdduvall 
12207678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep) && ((regno == MII_AUX_CONTROL) ||
12219860Sgdamore@opensolaris.org 	    (regno == MII_MSCONTROL)))
12227678SYong.Tan@Sun.COM 		return (0);
12237678SYong.Tan@Sun.COM 
12241369Sdduvall 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
12251369Sdduvall }
12261369Sdduvall 
12271369Sdduvall void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
12281369Sdduvall #pragma	no_inline(bge_mii_put16)
12291369Sdduvall 
12301369Sdduvall void
bge_mii_put16(bge_t * bgep,bge_regno_t regno,uint16_t data)12311369Sdduvall bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
12321369Sdduvall {
12331369Sdduvall 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
12344588Sml149210 	    (void *)bgep, regno, data));
12351369Sdduvall 
12361369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12371369Sdduvall 
12387678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep) && ((regno == MII_AUX_CONTROL) ||
12399860Sgdamore@opensolaris.org 	    (regno == MII_MSCONTROL)))
12407678SYong.Tan@Sun.COM 		return;
12417678SYong.Tan@Sun.COM 
12421369Sdduvall 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
12431369Sdduvall }
12441369Sdduvall 
12451369Sdduvall #undef	BGE_DBG
12461369Sdduvall #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
12471369Sdduvall 
12481369Sdduvall #if	BGE_SEE_IO32 || BGE_FLASH_IO32
12491369Sdduvall 
12501369Sdduvall /*
12511369Sdduvall  * Basic SEEPROM get/set access routine
12521369Sdduvall  *
12531369Sdduvall  * This uses the chip's SEEPROM auto-access method, controlled by the
12541369Sdduvall  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
12551369Sdduvall  * doesn't have to fiddle with the individual bits.
12561369Sdduvall  *
12571369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
12581369Sdduvall  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
12591369Sdduvall  *
12601369Sdduvall  * Return value:
12611369Sdduvall  *	0 on success,
12621369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
12631369Sdduvall  *	EPROTO on other h/w or s/w errors.
12641369Sdduvall  *
12651369Sdduvall  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
12661369Sdduvall  * from a (successful) SEEPROM_ACCESS_READ.
12671369Sdduvall  */
12681369Sdduvall static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
12691369Sdduvall 				uint32_t *dp);
12701369Sdduvall #pragma	no_inline(bge_seeprom_access)
12711369Sdduvall 
12721369Sdduvall static int
bge_seeprom_access(bge_t * bgep,uint32_t cmd,bge_regno_t addr,uint32_t * dp)12731369Sdduvall bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
12741369Sdduvall {
12751369Sdduvall 	uint32_t tries;
12761369Sdduvall 	uint32_t regval;
12771369Sdduvall 
12781369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12791369Sdduvall 
12801369Sdduvall 	/*
12811369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
12821369Sdduvall 	 * to specifically enable SEEPROM access (Flash is the default).
12831369Sdduvall 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
12841369Sdduvall 	 * and the NVM control registers don't exist ...
12851369Sdduvall 	 */
12861369Sdduvall 	switch (bgep->chipid.nvtype) {
12871369Sdduvall 	case BGE_NVTYPE_NONE:
12881369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
12891369Sdduvall 		_NOTE(NOTREACHED)
12901369Sdduvall 	case BGE_NVTYPE_SEEPROM:
12911369Sdduvall 		break;
12921369Sdduvall 
12931369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
12941369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
12951369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
12961369Sdduvall 	default:
12971369Sdduvall 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
12984588Sml149210 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
12991369Sdduvall 		break;
13001369Sdduvall 	}
13011369Sdduvall 
13021369Sdduvall 	/*
13031369Sdduvall 	 * Check there's no command in progress.
13041369Sdduvall 	 *
13051369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
13061369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
13071369Sdduvall 	 * Also, to ensure we don't have a conflict with the chip's
13081369Sdduvall 	 * internal firmware or a process accessing the same (shared)
13091369Sdduvall 	 * SEEPROM through the other port of a 5704, we've already
13101369Sdduvall 	 * been through the "software arbitration" protocol.
13111369Sdduvall 	 * So this is just a final consistency check: we shouldn't
13121369Sdduvall 	 * see EITHER the START bit (command started but not complete)
13131369Sdduvall 	 * OR the COMPLETE bit (command completed but not cleared).
13141369Sdduvall 	 */
13151369Sdduvall 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
13161369Sdduvall 	if (regval & SEEPROM_ACCESS_START)
13171369Sdduvall 		return (EPROTO);
13181369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE)
13191369Sdduvall 		return (EPROTO);
13201369Sdduvall 
13211369Sdduvall 	/*
13221369Sdduvall 	 * Assemble the command ...
13231369Sdduvall 	 */
13241369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
13251369Sdduvall 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
13261369Sdduvall 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
13271369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
13281369Sdduvall 	cmd |= SEEPROM_ACCESS_START;
13291369Sdduvall 	cmd |= SEEPROM_ACCESS_COMPLETE;
13301369Sdduvall 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
13311369Sdduvall 
13321369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
13331369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
13341369Sdduvall 
13351369Sdduvall 	/*
13361369Sdduvall 	 * By observation, a successful access takes ~20us on a 5703/4,
13371369Sdduvall 	 * but apparently much longer (up to 1000us) on the obsolescent
13381369Sdduvall 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
13391369Sdduvall 	 * timeouts here; but OTOH, we don't want a bogus access to lock
13401369Sdduvall 	 * out interrupts for longer than necessary. So we'll allow up
13411369Sdduvall 	 * to 1000us ...
13421369Sdduvall 	 */
13431369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
13441369Sdduvall 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
13451369Sdduvall 		if (regval & SEEPROM_ACCESS_COMPLETE)
13461369Sdduvall 			break;
13471369Sdduvall 		drv_usecwait(1);
13481369Sdduvall 	}
13491369Sdduvall 
13501369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE) {
13511369Sdduvall 		/*
13521369Sdduvall 		 * All OK; read the SEEPROM data register, then write back
13531369Sdduvall 		 * the value read from the address register in order to
13541369Sdduvall 		 * clear the <complete> bit and leave the SEEPROM access
13551369Sdduvall 		 * state machine idle, ready for the next access ...
13561369Sdduvall 		 */
13571369Sdduvall 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
13581369Sdduvall 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
13591369Sdduvall 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
13601369Sdduvall 		return (0);
13611369Sdduvall 	}
13621369Sdduvall 
13631369Sdduvall 	/*
13641369Sdduvall 	 * Hmm ... what happened here?
13651369Sdduvall 	 *
13662135Szh199473 	 * Most likely, the user addressed a non-existent SEEPROM. Or
13671369Sdduvall 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
13681369Sdduvall 	 * and didn't respond to being addressed. Either way, it's left
13691369Sdduvall 	 * the SEEPROM access state machine wedged. So we'll reset it
13701369Sdduvall 	 * before we leave, so it's ready for next time ...
13711369Sdduvall 	 */
13721369Sdduvall 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
13731369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
13741369Sdduvall 	return (ENODATA);
13751369Sdduvall }
13761369Sdduvall 
13771369Sdduvall /*
13781369Sdduvall  * Basic Flash get/set access routine
13791369Sdduvall  *
13801369Sdduvall  * These use the chip's Flash auto-access method, controlled by the
13811369Sdduvall  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
13821369Sdduvall  * fiddle with the individual bits.
13831369Sdduvall  *
13841369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
13851369Sdduvall  * the right to access the Flash, via bge_nvmem_acquire() above.
13861369Sdduvall  *
13871369Sdduvall  * Return value:
13881369Sdduvall  *	0 on success,
13891369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
13901369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
13911369Sdduvall  *
13921369Sdduvall  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
13931369Sdduvall  * from a (successful) NVM_FLASH_CMD_RD.
13941369Sdduvall  */
13951369Sdduvall static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
13961369Sdduvall 				uint32_t *dp);
13971369Sdduvall #pragma	no_inline(bge_flash_access)
13981369Sdduvall 
13991369Sdduvall static int
bge_flash_access(bge_t * bgep,uint32_t cmd,bge_regno_t addr,uint32_t * dp)14001369Sdduvall bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
14011369Sdduvall {
14021369Sdduvall 	uint32_t tries;
14031369Sdduvall 	uint32_t regval;
14041369Sdduvall 
14051369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
14061369Sdduvall 
14071369Sdduvall 	/*
14081369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
14091369Sdduvall 	 * to specifically disable SEEPROM access while accessing Flash.
14101369Sdduvall 	 * The older chips don't support Flash, and the NVM registers don't
14111369Sdduvall 	 * exist, so we shouldn't be here at all!
14121369Sdduvall 	 */
14131369Sdduvall 	switch (bgep->chipid.nvtype) {
14141369Sdduvall 	case BGE_NVTYPE_NONE:
14151369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
14161369Sdduvall 		_NOTE(NOTREACHED)
14171369Sdduvall 	case BGE_NVTYPE_SEEPROM:
14181369Sdduvall 		return (ENODEV);
14191369Sdduvall 
14201369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
14211369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
14221369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
14231369Sdduvall 	default:
14241369Sdduvall 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
14254588Sml149210 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
14261369Sdduvall 		break;
14271369Sdduvall 	}
14281369Sdduvall 
14291369Sdduvall 	/*
14301369Sdduvall 	 * Assemble the command ...
14311369Sdduvall 	 */
14321369Sdduvall 	addr &= NVM_FLASH_ADDR_MASK;
14331369Sdduvall 	cmd |= NVM_FLASH_CMD_DOIT;
14341369Sdduvall 	cmd |= NVM_FLASH_CMD_FIRST;
14351369Sdduvall 	cmd |= NVM_FLASH_CMD_LAST;
14361369Sdduvall 	cmd |= NVM_FLASH_CMD_DONE;
14371369Sdduvall 
14381369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
14391369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
14401369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
14411369Sdduvall 
14421369Sdduvall 	/*
14431369Sdduvall 	 * Allow up to 1000ms ...
14441369Sdduvall 	 */
14451369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
14461369Sdduvall 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
14471369Sdduvall 		if (regval & NVM_FLASH_CMD_DONE)
14481369Sdduvall 			break;
14491369Sdduvall 		drv_usecwait(1);
14501369Sdduvall 	}
14511369Sdduvall 
14521369Sdduvall 	if (regval & NVM_FLASH_CMD_DONE) {
14531369Sdduvall 		/*
14541369Sdduvall 		 * All OK; read the data from the Flash read register
14551369Sdduvall 		 */
14561369Sdduvall 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
14571369Sdduvall 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
14581369Sdduvall 		return (0);
14591369Sdduvall 	}
14601369Sdduvall 
14611369Sdduvall 	/*
14621369Sdduvall 	 * Hmm ... what happened here?
14631369Sdduvall 	 *
14642135Szh199473 	 * Most likely, the user addressed a non-existent Flash. Or
14651369Sdduvall 	 * maybe the Flash was busy internally (e.g. processing a write)
14661369Sdduvall 	 * and didn't respond to being addressed. Either way, there's
14671369Sdduvall 	 * nothing we can here ...
14681369Sdduvall 	 */
14691369Sdduvall 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
14701369Sdduvall 	return (ENODATA);
14711369Sdduvall }
14721369Sdduvall 
14731369Sdduvall /*
14741369Sdduvall  * The next two functions regulate access to the NVram (if fitted).
14751369Sdduvall  *
14761369Sdduvall  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
14771369Sdduvall  * (SPI) interface, but they can be accessed through either port. These
14781369Sdduvall  * are managed by different instance of this driver and have no software
14791369Sdduvall  * state in common.
14801369Sdduvall  *
14811369Sdduvall  * In addition (and even on a single core chip) the chip's internal
14821369Sdduvall  * firmware can access the SEEPROM/Flash, most notably after a RESET
14831369Sdduvall  * when it may download code to run internally.
14841369Sdduvall  *
14851369Sdduvall  * So we need to arbitrate between these various software agents.  For
14861369Sdduvall  * this purpose, the chip provides the Software Arbitration Register,
14871369Sdduvall  * which implements hardware(!) arbitration.
14881369Sdduvall  *
14891369Sdduvall  * This functionality didn't exist on older (5700/5701) chips, so there's
14901369Sdduvall  * nothing we can do by way of arbitration on those; also, if there's no
14911369Sdduvall  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
14921369Sdduvall  * nothing to do.
14931369Sdduvall  *
14941369Sdduvall  * The internal firmware appears to use Request 0, which is the highest
14951369Sdduvall  * priority.  So we'd like to use Request 2, leaving one higher and one
14961369Sdduvall  * lower for any future developments ... but apparently this doesn't
14971369Sdduvall  * always work.  So for now, the code uses Request 1 ;-(
14981369Sdduvall  */
14991369Sdduvall 
15001369Sdduvall #define	NVM_READ_REQ	NVM_READ_REQ1
15011369Sdduvall #define	NVM_RESET_REQ	NVM_RESET_REQ1
15021369Sdduvall #define	NVM_SET_REQ	NVM_SET_REQ1
15031369Sdduvall 
15041369Sdduvall static void bge_nvmem_relinquish(bge_t *bgep);
15051369Sdduvall #pragma	no_inline(bge_nvmem_relinquish)
15061369Sdduvall 
15071369Sdduvall static void
bge_nvmem_relinquish(bge_t * bgep)15081369Sdduvall bge_nvmem_relinquish(bge_t *bgep)
15091369Sdduvall {
15101369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15111369Sdduvall 
15121369Sdduvall 	switch (bgep->chipid.nvtype) {
15131369Sdduvall 	case BGE_NVTYPE_NONE:
15141369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15151369Sdduvall 		_NOTE(NOTREACHED)
15161369Sdduvall 		return;
15171369Sdduvall 
15181369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15191369Sdduvall 		/*
15201369Sdduvall 		 * No arbitration performed, no release needed
15211369Sdduvall 		 */
15221369Sdduvall 		return;
15231369Sdduvall 
15241369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15251369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15261369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15271369Sdduvall 	default:
15281369Sdduvall 		break;
15291369Sdduvall 	}
15301369Sdduvall 
15311369Sdduvall 	/*
15321369Sdduvall 	 * Our own request should be present (whether or not granted) ...
15331369Sdduvall 	 */
15341865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15351369Sdduvall 
15361369Sdduvall 	/*
15371369Sdduvall 	 * ... this will make it go away.
15381369Sdduvall 	 */
15391369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
15401865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15411369Sdduvall }
15421369Sdduvall 
15431369Sdduvall /*
15441369Sdduvall  * Arbitrate for access to the NVmem, if necessary
15451369Sdduvall  *
15461369Sdduvall  * Return value:
15471369Sdduvall  *	0 on success
15481369Sdduvall  *	EAGAIN if the device is in use (retryable)
15491369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
15501369Sdduvall  */
15511369Sdduvall static int bge_nvmem_acquire(bge_t *bgep);
15521369Sdduvall #pragma	no_inline(bge_nvmem_acquire)
15531369Sdduvall 
15541369Sdduvall static int
bge_nvmem_acquire(bge_t * bgep)15551369Sdduvall bge_nvmem_acquire(bge_t *bgep)
15561369Sdduvall {
15571369Sdduvall 	uint32_t regval;
15581369Sdduvall 	uint32_t tries;
15591369Sdduvall 
15601369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15611369Sdduvall 
15621369Sdduvall 	switch (bgep->chipid.nvtype) {
15631369Sdduvall 	case BGE_NVTYPE_NONE:
15641369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15651369Sdduvall 		/*
15661369Sdduvall 		 * Access denied: no (recognisable) device fitted
15671369Sdduvall 		 */
15681369Sdduvall 		return (ENODEV);
15691369Sdduvall 
15701369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15711369Sdduvall 		/*
15721369Sdduvall 		 * Access granted: no arbitration needed (or possible)
15731369Sdduvall 		 */
15741369Sdduvall 		return (0);
15751369Sdduvall 
15761369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15771369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15781369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15791369Sdduvall 	default:
15801369Sdduvall 		/*
15811369Sdduvall 		 * Access conditional: conduct arbitration protocol
15821369Sdduvall 		 */
15831369Sdduvall 		break;
15841369Sdduvall 	}
15851369Sdduvall 
15861369Sdduvall 	/*
15871369Sdduvall 	 * We're holding the per-port mutex <genlock>, so no-one other
15882135Szh199473 	 * thread can be attempting to access the NVmem through *this*
15891369Sdduvall 	 * port. But it could be in use by the *other* port (of a 5704),
15901369Sdduvall 	 * or by the chip's internal firmware, so we have to go through
15911369Sdduvall 	 * the full (hardware) arbitration protocol ...
15921369Sdduvall 	 *
15931369Sdduvall 	 * Note that *because* we're holding <genlock>, the interrupt handler
15941369Sdduvall 	 * won't be able to progress.  So we're only willing to spin for a
15951369Sdduvall 	 * fairly short time.  Specifically:
15961369Sdduvall 	 *
15971369Sdduvall 	 *	We *must* wait long enough for the hardware to resolve all
15981369Sdduvall 	 *	requests and determine the winner.  Fortunately, this is
15991369Sdduvall 	 *	"almost instantaneous", even as observed by GHz CPUs.
16001369Sdduvall 	 *
16011369Sdduvall 	 *	A successful access by another Solaris thread (via either
16021369Sdduvall 	 *	port) typically takes ~20us.  So waiting a bit longer than
16031369Sdduvall 	 *	that will give a good chance of success, if the other user
16041369Sdduvall 	 *	*is* another thread on the other port.
16051369Sdduvall 	 *
16061369Sdduvall 	 *	However, the internal firmware can hold on to the NVmem
16071369Sdduvall 	 *	for *much* longer: at least 10 milliseconds just after a
16081369Sdduvall 	 *	RESET, and maybe even longer if the NVmem actually contains
16091369Sdduvall 	 *	code to download and run on the internal CPUs.
16101369Sdduvall 	 *
16111369Sdduvall 	 * So, we'll allow 50us; if that's not enough then it's up to the
16121369Sdduvall 	 * caller to retry later (hence the choice of return code EAGAIN).
16131369Sdduvall 	 */
16141369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
16151369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
16161369Sdduvall 
16171369Sdduvall 	for (tries = 0; tries < 50; ++tries) {
16181369Sdduvall 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
16191369Sdduvall 		if (regval & NVM_WON_REQ1)
16201369Sdduvall 			break;
16211369Sdduvall 		drv_usecwait(1);
16221369Sdduvall 	}
16231369Sdduvall 
16241369Sdduvall 	if (regval & NVM_WON_REQ1) {
16251369Sdduvall 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
16261369Sdduvall 		return (0);
16271369Sdduvall 	}
16281369Sdduvall 
16291369Sdduvall 	/*
16301369Sdduvall 	 * Somebody else must be accessing the NVmem, so abandon our
16311369Sdduvall 	 * attempt take control of it.  The caller can try again later ...
16321369Sdduvall 	 */
16331369Sdduvall 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
16341369Sdduvall 	bge_nvmem_relinquish(bgep);
16351369Sdduvall 	return (EAGAIN);
16361369Sdduvall }
16371369Sdduvall 
16381369Sdduvall /*
16391369Sdduvall  * This code assumes that the GPIO1 bit has been wired up to the NVmem
16401369Sdduvall  * write protect line in such a way that the NVmem is protected when
16411369Sdduvall  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
16421369Sdduvall  * NVmem writable we have to change GPIO1 to an output AND drive it low.
16431369Sdduvall  *
16441369Sdduvall  * Note: there's only one set of GPIO pins on a 5704, even though they
16451369Sdduvall  * can be accessed through either port.  So the chip has to resolve what
16461369Sdduvall  * happens if the two ports program a single pin differently ... the rule
16471369Sdduvall  * it uses is that if the ports disagree about the *direction* of a pin,
16481369Sdduvall  * "output" wins over "input", but if they disagree about its *value* as
16491369Sdduvall  * an output, then the pin is TRISTATED instead!  In such a case, no-one
16501369Sdduvall  * wins, and the external signal does whatever the external circuitry
16511369Sdduvall  * defines as the default -- which we've assumed is the PROTECTED state.
16521369Sdduvall  * So, we always change GPIO1 back to being an *input* whenever we're not
16531369Sdduvall  * specifically using it to unprotect the NVmem. This allows either port
16542135Szh199473  * to update the NVmem, although obviously only one at a time!
16551369Sdduvall  *
16561369Sdduvall  * The caller should hold <genlock> and *also* have already acquired the
16571369Sdduvall  * right to access the NVmem, via bge_nvmem_acquire() above.
16581369Sdduvall  */
16591369Sdduvall static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
16601369Sdduvall #pragma	inline(bge_nvmem_protect)
16611369Sdduvall 
16621369Sdduvall static void
bge_nvmem_protect(bge_t * bgep,boolean_t protect)16631369Sdduvall bge_nvmem_protect(bge_t *bgep, boolean_t protect)
16641369Sdduvall {
16651369Sdduvall 	uint32_t regval;
16661369Sdduvall 
16671369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
16681369Sdduvall 
16691369Sdduvall 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
16701369Sdduvall 	if (protect) {
16711369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_1;
16721369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16731369Sdduvall 	} else {
16741369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
16751369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16761369Sdduvall 	}
16771369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
16781369Sdduvall }
16791369Sdduvall 
16801369Sdduvall /*
16811369Sdduvall  * Now put it all together ...
16821369Sdduvall  *
16831369Sdduvall  * Try to acquire control of the NVmem; if successful, then:
16841369Sdduvall  *	unprotect it (if we want to write to it)
16851369Sdduvall  *	perform the requested access
16861369Sdduvall  *	reprotect it (after a write)
16871369Sdduvall  *	relinquish control
16881369Sdduvall  *
16891369Sdduvall  * Return value:
16901369Sdduvall  *	0 on success,
16911369Sdduvall  *	EAGAIN if the device is in use (retryable)
16921369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
16931369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
16941369Sdduvall  *	EPROTO on other h/w or s/w errors.
16951369Sdduvall  */
16961369Sdduvall static int
bge_nvmem_rw32(bge_t * bgep,uint32_t cmd,bge_regno_t addr,uint32_t * dp)16971369Sdduvall bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
16981369Sdduvall {
16991369Sdduvall 	int err;
17001369Sdduvall 
17011369Sdduvall 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
17021369Sdduvall 		switch (cmd) {
17031369Sdduvall 		case BGE_SEE_READ:
17041369Sdduvall 			err = bge_seeprom_access(bgep,
17051369Sdduvall 			    SEEPROM_ACCESS_READ, addr, dp);
17061369Sdduvall 			break;
17071369Sdduvall 
17081369Sdduvall 		case BGE_SEE_WRITE:
17091369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
17101369Sdduvall 			err = bge_seeprom_access(bgep,
17111369Sdduvall 			    SEEPROM_ACCESS_WRITE, addr, dp);
17121369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
17131369Sdduvall 			break;
17141369Sdduvall 
17151369Sdduvall 		case BGE_FLASH_READ:
17161369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17179042SYong.Tan@Sun.COM 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
171811968SYong.Tan@Sun.COM 			    DEVICE_5717_SERIES_CHIPSETS(bgep) ||
17191369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17201369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
17211369Sdduvall 				    NVM_ACCESS_ENABLE);
17221369Sdduvall 			}
17231369Sdduvall 			err = bge_flash_access(bgep,
17241369Sdduvall 			    NVM_FLASH_CMD_RD, addr, dp);
17251369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17269042SYong.Tan@Sun.COM 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
172711968SYong.Tan@Sun.COM 			    DEVICE_5717_SERIES_CHIPSETS(bgep) ||
17281369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17291369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
17301369Sdduvall 				    NVM_ACCESS_ENABLE);
17311369Sdduvall 			}
17321369Sdduvall 			break;
17331369Sdduvall 
17341369Sdduvall 		case BGE_FLASH_WRITE:
17351369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17369042SYong.Tan@Sun.COM 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
173711968SYong.Tan@Sun.COM 			    DEVICE_5717_SERIES_CHIPSETS(bgep) ||
17381369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17391369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
17401369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
17411369Sdduvall 			}
17421369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
17431369Sdduvall 			err = bge_flash_access(bgep,
17441369Sdduvall 			    NVM_FLASH_CMD_WR, addr, dp);
17451369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
17461369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17479042SYong.Tan@Sun.COM 			    DEVICE_5723_SERIES_CHIPSETS(bgep) ||
174811968SYong.Tan@Sun.COM 			    DEVICE_5717_SERIES_CHIPSETS(bgep) ||
17491369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17501369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
17511369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
17521369Sdduvall 			}
17531369Sdduvall 
17541369Sdduvall 			break;
17551369Sdduvall 
17561369Sdduvall 		default:
17571369Sdduvall 			_NOTE(NOTREACHED)
17581369Sdduvall 			break;
17591369Sdduvall 		}
17601369Sdduvall 		bge_nvmem_relinquish(bgep);
17611369Sdduvall 	}
17621369Sdduvall 
17631369Sdduvall 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
17641369Sdduvall 	return (err);
17651369Sdduvall }
17661369Sdduvall 
17671369Sdduvall /*
17681369Sdduvall  * Attempt to get a MAC address from the SEEPROM or Flash, if any
17691369Sdduvall  */
17701369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17711369Sdduvall #pragma no_inline(bge_get_nvmac)
17721369Sdduvall 
17731369Sdduvall static uint64_t
bge_get_nvmac(bge_t * bgep)17741369Sdduvall bge_get_nvmac(bge_t *bgep)
17751369Sdduvall {
17761369Sdduvall 	uint32_t mac_high;
17771369Sdduvall 	uint32_t mac_low;
17781369Sdduvall 	uint32_t addr;
17791369Sdduvall 	uint32_t cmd;
17801369Sdduvall 	uint64_t mac;
17811369Sdduvall 
17821369Sdduvall 	BGE_TRACE(("bge_get_nvmac($%p)",
17834588Sml149210 	    (void *)bgep));
17841369Sdduvall 
17851369Sdduvall 	switch (bgep->chipid.nvtype) {
17861369Sdduvall 	case BGE_NVTYPE_NONE:
17871369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
17881369Sdduvall 	default:
17891369Sdduvall 		return (0ULL);
17901369Sdduvall 
17911369Sdduvall 	case BGE_NVTYPE_SEEPROM:
17921369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
17931369Sdduvall 		cmd = BGE_SEE_READ;
17941369Sdduvall 		break;
17951369Sdduvall 
17961369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
17971369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
17981369Sdduvall 		cmd = BGE_FLASH_READ;
17991369Sdduvall 		break;
18001369Sdduvall 	}
18011369Sdduvall 
18027678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep))
18037678SYong.Tan@Sun.COM 		addr = NVMEM_DATA_MAC_ADDRESS_5906;
18047678SYong.Tan@Sun.COM 	else
18057678SYong.Tan@Sun.COM 		addr = NVMEM_DATA_MAC_ADDRESS;
18067678SYong.Tan@Sun.COM 
18071369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
18081369Sdduvall 		return (0ULL);
18091369Sdduvall 	addr += 4;
18101369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
18111369Sdduvall 		return (0ULL);
18121369Sdduvall 
18131369Sdduvall 	/*
18141369Sdduvall 	 * The Broadcom chip is natively BIG-endian, so that's how the
18151369Sdduvall 	 * MAC address is represented in NVmem.  We may need to swap it
18161369Sdduvall 	 * around on a little-endian host ...
18171369Sdduvall 	 */
18181369Sdduvall #ifdef	_BIG_ENDIAN
18191369Sdduvall 	mac = mac_high;
18201369Sdduvall 	mac = mac << 32;
18211369Sdduvall 	mac |= mac_low;
18221369Sdduvall #else
18231369Sdduvall 	mac = BGE_BSWAP_32(mac_high);
18241369Sdduvall 	mac = mac << 32;
18251369Sdduvall 	mac |= BGE_BSWAP_32(mac_low);
18261369Sdduvall #endif	/* _BIG_ENDIAN */
18271369Sdduvall 
18281369Sdduvall 	return (mac);
18291369Sdduvall }
18301369Sdduvall 
18311369Sdduvall #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
18321369Sdduvall 
18331369Sdduvall /*
18341369Sdduvall  * Dummy version for when we're not supporting NVmem access
18351369Sdduvall  */
18361369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
18371369Sdduvall #pragma inline(bge_get_nvmac)
18381369Sdduvall 
18391369Sdduvall static uint64_t
bge_get_nvmac(bge_t * bgep)18401369Sdduvall bge_get_nvmac(bge_t *bgep)
18411369Sdduvall {
18421369Sdduvall 	_NOTE(ARGUNUSED(bgep))
18431369Sdduvall 	return (0ULL);
18441369Sdduvall }
18451369Sdduvall 
18461369Sdduvall #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
18471369Sdduvall 
18481369Sdduvall /*
18491369Sdduvall  * Determine the type of NVmem that is (or may be) attached to this chip,
18501369Sdduvall  */
18511369Sdduvall static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
18521369Sdduvall #pragma no_inline(bge_nvmem_id)
18531369Sdduvall 
18541369Sdduvall static enum bge_nvmem_type
bge_nvmem_id(bge_t * bgep)18551369Sdduvall bge_nvmem_id(bge_t *bgep)
18561369Sdduvall {
18571369Sdduvall 	enum bge_nvmem_type nvtype;
18581369Sdduvall 	uint32_t config1;
18591369Sdduvall 
18601369Sdduvall 	BGE_TRACE(("bge_nvmem_id($%p)",
18614588Sml149210 	    (void *)bgep));
18621369Sdduvall 
18631369Sdduvall 	switch (bgep->chipid.device) {
18641369Sdduvall 	default:
18651369Sdduvall 		/*
18661369Sdduvall 		 * We shouldn't get here; it means we don't recognise
18671369Sdduvall 		 * the chip, which means we don't know how to determine
18681369Sdduvall 		 * what sort of NVmem (if any) it has.  So we'll say
18691369Sdduvall 		 * NONE, to disable the NVmem access code ...
18701369Sdduvall 		 */
18711369Sdduvall 		nvtype = BGE_NVTYPE_NONE;
18721369Sdduvall 		break;
18731369Sdduvall 
18741369Sdduvall 	case DEVICE_ID_5700:
18751369Sdduvall 	case DEVICE_ID_5700x:
18761369Sdduvall 	case DEVICE_ID_5701:
18771369Sdduvall 		/*
18781369Sdduvall 		 * These devices support *only* SEEPROMs
18791369Sdduvall 		 */
18801369Sdduvall 		nvtype = BGE_NVTYPE_SEEPROM;
18811369Sdduvall 		break;
18821369Sdduvall 
18831369Sdduvall 	case DEVICE_ID_5702:
18841369Sdduvall 	case DEVICE_ID_5702fe:
18851369Sdduvall 	case DEVICE_ID_5703C:
18861369Sdduvall 	case DEVICE_ID_5703S:
18871369Sdduvall 	case DEVICE_ID_5704C:
18881369Sdduvall 	case DEVICE_ID_5704S:
18891369Sdduvall 	case DEVICE_ID_5704:
18901369Sdduvall 	case DEVICE_ID_5705M:
18911369Sdduvall 	case DEVICE_ID_5705C:
18923170Sml149210 	case DEVICE_ID_5705_2:
1893*12854SYong.Tan@Sun.COM 	case DEVICE_ID_5717:
189411968SYong.Tan@Sun.COM 	case DEVICE_ID_5718:
1895*12854SYong.Tan@Sun.COM 	case DEVICE_ID_5724:
1896*12854SYong.Tan@Sun.COM 	case DEVICE_ID_57780:
18977871SGarrett.Damore@Sun.COM 	case DEVICE_ID_5780:
18981369Sdduvall 	case DEVICE_ID_5782:
189911479SYong.Tan@Sun.COM 	case DEVICE_ID_5785:
19006989Sml40262 	case DEVICE_ID_5787:
19016989Sml40262 	case DEVICE_ID_5787M:
19021369Sdduvall 	case DEVICE_ID_5788:
19032135Szh199473 	case DEVICE_ID_5789:
19041369Sdduvall 	case DEVICE_ID_5751:
19051369Sdduvall 	case DEVICE_ID_5751M:
19062675Szh199473 	case DEVICE_ID_5752:
19072675Szh199473 	case DEVICE_ID_5752M:
19083771Sml149210 	case DEVICE_ID_5754:
19094330Sml149210 	case DEVICE_ID_5755:
19106546Sgh162552 	case DEVICE_ID_5755M:
19118207SGordon.Ross@Sun.COM 	case DEVICE_ID_5756M:
19121369Sdduvall 	case DEVICE_ID_5721:
19137316SCrisson.Hu@Sun.COM 	case DEVICE_ID_5722:
19149042SYong.Tan@Sun.COM 	case DEVICE_ID_5723:
19159165SYong.Tan@Sun.COM 	case DEVICE_ID_5761:
19169165SYong.Tan@Sun.COM 	case DEVICE_ID_5761E:
191710862SYong.Tan@Sun.COM 	case DEVICE_ID_5764:
19181369Sdduvall 	case DEVICE_ID_5714C:
19191369Sdduvall 	case DEVICE_ID_5714S:
19201369Sdduvall 	case DEVICE_ID_5715C:
19213170Sml149210 	case DEVICE_ID_5715S:
19221369Sdduvall 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
19231369Sdduvall 		if (config1 & NVM_CFG1_FLASH_MODE)
19241369Sdduvall 			if (config1 & NVM_CFG1_BUFFERED_MODE)
19251369Sdduvall 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
19261369Sdduvall 			else
19271369Sdduvall 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
19281369Sdduvall 		else
19291369Sdduvall 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
19301369Sdduvall 		break;
19317678SYong.Tan@Sun.COM 	case DEVICE_ID_5906:
19327678SYong.Tan@Sun.COM 	case DEVICE_ID_5906M:
19337678SYong.Tan@Sun.COM 		nvtype = BGE_NVTYPE_BUFFERED_FLASH;
19347678SYong.Tan@Sun.COM 		break;
19351369Sdduvall 	}
19361369Sdduvall 
19371369Sdduvall 	return (nvtype);
19381369Sdduvall }
19391369Sdduvall 
19401369Sdduvall #undef	BGE_DBG
19411369Sdduvall #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
19421369Sdduvall 
19431369Sdduvall static void
bge_init_recv_rule(bge_t * bgep)19441369Sdduvall bge_init_recv_rule(bge_t *bgep)
19451369Sdduvall {
19468275SEric Cheng 	bge_recv_rule_t *rulep = bgep->recv_rules;
19471369Sdduvall 	uint32_t i;
19481369Sdduvall 
19491369Sdduvall 	/*
19508275SEric Cheng 	 * Initialize receive rule registers.
19518275SEric Cheng 	 * Note that rules may persist across each bge_m_start/stop() call.
19521369Sdduvall 	 */
19531369Sdduvall 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
19541369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
19551369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
19561369Sdduvall 	}
19571369Sdduvall }
19581369Sdduvall 
19591369Sdduvall /*
19601369Sdduvall  * Using the values captured by bge_chip_cfg_init(), and additional probes
19611369Sdduvall  * as required, characterise the chip fully: determine the label by which
19621369Sdduvall  * to refer to this chip, the correct settings for various registers, and
19631369Sdduvall  * of course whether the device and/or subsystem are supported!
19641369Sdduvall  */
19651865Sdilpreet int bge_chip_id_init(bge_t *bgep);
19661369Sdduvall #pragma	no_inline(bge_chip_id_init)
19671369Sdduvall 
19681865Sdilpreet int
bge_chip_id_init(bge_t * bgep)19691369Sdduvall bge_chip_id_init(bge_t *bgep)
19701369Sdduvall {
19711369Sdduvall 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
19721369Sdduvall 	boolean_t sys_ok;
19731369Sdduvall 	boolean_t dev_ok;
19741369Sdduvall 	chip_id_t *cidp;
19751369Sdduvall 	uint32_t subid;
19761369Sdduvall 	char *devname;
19771369Sdduvall 	char *sysname;
19781369Sdduvall 	int *ids;
19791369Sdduvall 	int err;
19801369Sdduvall 	uint_t i;
19811369Sdduvall 
19821369Sdduvall 	sys_ok = dev_ok = B_FALSE;
19831369Sdduvall 	cidp = &bgep->chipid;
19841369Sdduvall 
19851369Sdduvall 	/*
19861369Sdduvall 	 * Check the PCI device ID to determine the generic chip type and
19871369Sdduvall 	 * select parameters that depend on this.
19881369Sdduvall 	 *
19891369Sdduvall 	 * Note: because the SPARC platforms in general don't fit the
19901369Sdduvall 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
19911369Sdduvall 	 * as zero - which is why we use <asic_rev> rather than <revision>
19921369Sdduvall 	 * below ...
19931369Sdduvall 	 *
19941369Sdduvall 	 * Note: in general we can't distinguish between the Copper/SerDes
19951369Sdduvall 	 * versions by ID alone, as some Copper devices (e.g. some but not
19961369Sdduvall 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
19971369Sdduvall 	 * treat them the same here, and the MII code works out the media
19981369Sdduvall 	 * type later on ...
19991369Sdduvall 	 */
20001369Sdduvall 	cidp->mbuf_base = bge_mbuf_pool_base;
20011369Sdduvall 	cidp->mbuf_length = bge_mbuf_pool_len;
20021369Sdduvall 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
20031369Sdduvall 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
20041369Sdduvall 	cidp->pci_type = BGE_PCI_X;
20051369Sdduvall 	cidp->statistic_type = BGE_STAT_BLK;
20061908Sly149593 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
20071908Sly149593 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
20081908Sly149593 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
20095903Ssowmini 	cidp->rx_ticks_norm = bge_rx_ticks_norm;
20105903Ssowmini 	cidp->rx_count_norm = bge_rx_count_norm;
20119731SYong.Tan@Sun.COM 	cidp->tx_ticks_norm = bge_tx_ticks_norm;
20129731SYong.Tan@Sun.COM 	cidp->tx_count_norm = bge_tx_count_norm;
201311968SYong.Tan@Sun.COM 	cidp->mask_pci_int = MHCR_MASK_PCI_INT_OUTPUT;
20141369Sdduvall 
20151369Sdduvall 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
20161369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
20171369Sdduvall 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
20181369Sdduvall 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
20191369Sdduvall 
20201369Sdduvall 	cidp->msi_enabled = B_FALSE;
20211369Sdduvall 
20221369Sdduvall 	switch (cidp->device) {
2023*12854SYong.Tan@Sun.COM 	case DEVICE_ID_5717:
202411968SYong.Tan@Sun.COM 	case DEVICE_ID_5718:
2025*12854SYong.Tan@Sun.COM 	case DEVICE_ID_5724:
2026*12854SYong.Tan@Sun.COM 		if (cidp->device == DEVICE_ID_5717)
2027*12854SYong.Tan@Sun.COM 			cidp->chip_label = 5717;
2028*12854SYong.Tan@Sun.COM 		else if (cidp->device == DEVICE_ID_5718)
2029*12854SYong.Tan@Sun.COM 			cidp->chip_label = 5718;
2030*12854SYong.Tan@Sun.COM 		else
2031*12854SYong.Tan@Sun.COM 			cidp->chip_label = 5724;
203211968SYong.Tan@Sun.COM 		cidp->msi_enabled = bge_enable_msi;
203311968SYong.Tan@Sun.COM #ifdef __sparc
203411968SYong.Tan@Sun.COM 		cidp->mask_pci_int = LE_32(MHCR_MASK_PCI_INT_OUTPUT);
203511968SYong.Tan@Sun.COM #endif
203611968SYong.Tan@Sun.COM 		cidp->bge_dma_rwctrl = LE_32(PDRWCR_VAR_5717);
203711968SYong.Tan@Sun.COM 		cidp->pci_type = BGE_PCI_E;
203811968SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
203911968SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5717;
204011968SYong.Tan@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5717;
204111968SYong.Tan@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
204211968SYong.Tan@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
204311968SYong.Tan@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
204411968SYong.Tan@Sun.COM 		cidp->bge_mlcr_default = MLCR_DEFAULT_5717;
204511968SYong.Tan@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
204611968SYong.Tan@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
204711968SYong.Tan@Sun.COM 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
204811968SYong.Tan@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
204911968SYong.Tan@Sun.COM 		dev_ok = B_TRUE;
205011968SYong.Tan@Sun.COM 		break;
205111968SYong.Tan@Sun.COM 
20521369Sdduvall 	case DEVICE_ID_5700:
20531369Sdduvall 	case DEVICE_ID_5700x:
20541369Sdduvall 		cidp->chip_label = 5700;
20552135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20561369Sdduvall 		break;
20571369Sdduvall 
20581369Sdduvall 	case DEVICE_ID_5701:
20591369Sdduvall 		cidp->chip_label = 5701;
20601369Sdduvall 		dev_ok = B_TRUE;
20612135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20621369Sdduvall 		break;
20631369Sdduvall 
20641369Sdduvall 	case DEVICE_ID_5702:
20651369Sdduvall 	case DEVICE_ID_5702fe:
20661369Sdduvall 		cidp->chip_label = 5702;
20671369Sdduvall 		dev_ok = B_TRUE;
20682135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20692135Szh199473 		cidp->pci_type = BGE_PCI;
20701369Sdduvall 		break;
20711369Sdduvall 
20721369Sdduvall 	case DEVICE_ID_5703C:
20731369Sdduvall 	case DEVICE_ID_5703S:
20741369Sdduvall 	case DEVICE_ID_5703:
20751369Sdduvall 		/*
20761369Sdduvall 		 * Revision A0 of the 5703/5793 had various errata
20771369Sdduvall 		 * that we can't or don't work around, so it's not
20781369Sdduvall 		 * supported, but all later versions are
20791369Sdduvall 		 */
20801369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
20811369Sdduvall 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
20821369Sdduvall 			dev_ok = B_TRUE;
20832135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20841369Sdduvall 		break;
20851369Sdduvall 
20861369Sdduvall 	case DEVICE_ID_5704C:
20871369Sdduvall 	case DEVICE_ID_5704S:
20881369Sdduvall 	case DEVICE_ID_5704:
20891369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
20901369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
20911369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
20921369Sdduvall 		dev_ok = B_TRUE;
20936133Sgh162552 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20941369Sdduvall 		break;
20951369Sdduvall 
20961369Sdduvall 	case DEVICE_ID_5705C:
20971369Sdduvall 	case DEVICE_ID_5705M:
20981369Sdduvall 	case DEVICE_ID_5705MA3:
20991369Sdduvall 	case DEVICE_ID_5705F:
21003170Sml149210 	case DEVICE_ID_5705_2:
21013771Sml149210 	case DEVICE_ID_5754:
21023771Sml149210 		if (cidp->device == DEVICE_ID_5754) {
21033771Sml149210 			cidp->chip_label = 5754;
21043771Sml149210 			cidp->pci_type = BGE_PCI_E;
21053771Sml149210 		} else {
21063771Sml149210 			cidp->chip_label = 5705;
21073771Sml149210 			cidp->pci_type = BGE_PCI;
210810845SYong.Tan@Sun.COM 			cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21093771Sml149210 		}
21101908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21111908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21121908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21131369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21141369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21151369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21161369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21171908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21181369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21191369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21201369Sdduvall 		dev_ok = B_TRUE;
21211369Sdduvall 		break;
21221369Sdduvall 
21237678SYong.Tan@Sun.COM 	case DEVICE_ID_5906:
21247678SYong.Tan@Sun.COM 	case DEVICE_ID_5906M:
21257678SYong.Tan@Sun.COM 		cidp->chip_label = 5906;
21267678SYong.Tan@Sun.COM 		cidp->pci_type = BGE_PCI_E;
21277678SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5906;
21287678SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5906;
21297678SYong.Tan@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5906;
21307678SYong.Tan@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base;
21317678SYong.Tan@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len;
21327678SYong.Tan@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21337678SYong.Tan@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21347678SYong.Tan@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21357678SYong.Tan@Sun.COM 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21367678SYong.Tan@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
21377678SYong.Tan@Sun.COM 		dev_ok = B_TRUE;
21387678SYong.Tan@Sun.COM 		break;
21397678SYong.Tan@Sun.COM 
21404588Sml149210 	case DEVICE_ID_5753:
21414588Sml149210 		cidp->chip_label = 5753;
21424588Sml149210 		cidp->pci_type = BGE_PCI_E;
21434588Sml149210 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21444588Sml149210 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21454588Sml149210 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21464588Sml149210 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21474588Sml149210 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21484588Sml149210 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21494588Sml149210 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
21504588Sml149210 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21514588Sml149210 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21524588Sml149210 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21534588Sml149210 		cidp->statistic_type = BGE_STAT_REG;
21544588Sml149210 		dev_ok = B_TRUE;
21554588Sml149210 		break;
21564588Sml149210 
21574330Sml149210 	case DEVICE_ID_5755:
21586546Sgh162552 	case DEVICE_ID_5755M:
21594330Sml149210 		cidp->chip_label = 5755;
21604330Sml149210 		cidp->pci_type = BGE_PCI_E;
21614330Sml149210 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21624330Sml149210 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21634330Sml149210 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21644330Sml149210 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21654330Sml149210 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21664330Sml149210 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21674330Sml149210 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
21684330Sml149210 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21694330Sml149210 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21704330Sml149210 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
217110845SYong.Tan@Sun.COM 		if (cidp->device == DEVICE_ID_5755M)
217210845SYong.Tan@Sun.COM 			cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21734330Sml149210 		cidp->statistic_type = BGE_STAT_REG;
21744330Sml149210 		dev_ok = B_TRUE;
21754330Sml149210 		break;
21764330Sml149210 
21778207SGordon.Ross@Sun.COM 	case DEVICE_ID_5756M:
21788207SGordon.Ross@Sun.COM 		/*
21798207SGordon.Ross@Sun.COM 		 * This is nearly identical to the 5755M.
21808207SGordon.Ross@Sun.COM 		 * (Actually reports the 5755 chip ID.)
21818207SGordon.Ross@Sun.COM 		 */
21828207SGordon.Ross@Sun.COM 		cidp->chip_label = 5756;
21838207SGordon.Ross@Sun.COM 		cidp->pci_type = BGE_PCI_E;
21848207SGordon.Ross@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21858207SGordon.Ross@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21868207SGordon.Ross@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21878207SGordon.Ross@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21888207SGordon.Ross@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21898207SGordon.Ross@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21908207SGordon.Ross@Sun.COM 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
21918207SGordon.Ross@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21928207SGordon.Ross@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21938207SGordon.Ross@Sun.COM 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21948207SGordon.Ross@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
21958207SGordon.Ross@Sun.COM 		dev_ok = B_TRUE;
21968207SGordon.Ross@Sun.COM 		break;
21978207SGordon.Ross@Sun.COM 
21986989Sml40262 	case DEVICE_ID_5787:
21996989Sml40262 	case DEVICE_ID_5787M:
22006989Sml40262 		cidp->chip_label = 5787;
22016989Sml40262 		cidp->pci_type = BGE_PCI_E;
22026989Sml40262 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22036989Sml40262 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22046989Sml40262 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22056989Sml40262 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
22066989Sml40262 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
22076989Sml40262 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
22086989Sml40262 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
22096989Sml40262 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22106989Sml40262 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22116989Sml40262 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22126989Sml40262 		cidp->statistic_type = BGE_STAT_REG;
22136989Sml40262 		dev_ok = B_TRUE;
22146989Sml40262 		break;
22156989Sml40262 
22169042SYong.Tan@Sun.COM 	case DEVICE_ID_5723:
22179165SYong.Tan@Sun.COM 	case DEVICE_ID_5761:
22189165SYong.Tan@Sun.COM 	case DEVICE_ID_5761E:
2219*12854SYong.Tan@Sun.COM 	case DEVICE_ID_57780:
222010862SYong.Tan@Sun.COM 		cidp->msi_enabled = bge_enable_msi;
222110862SYong.Tan@Sun.COM 		/*
222211479SYong.Tan@Sun.COM 		 * We don't use MSI for BCM5764 and BCM5785, as the
222311479SYong.Tan@Sun.COM 		 * status block may fail to update when the network
222411479SYong.Tan@Sun.COM 		 * traffic is heavy.
222510862SYong.Tan@Sun.COM 		 */
222610862SYong.Tan@Sun.COM 		/* FALLTHRU */
222711479SYong.Tan@Sun.COM 	case DEVICE_ID_5785:
222810862SYong.Tan@Sun.COM 	case DEVICE_ID_5764:
222910862SYong.Tan@Sun.COM 		if (cidp->device == DEVICE_ID_5723)
223010862SYong.Tan@Sun.COM 			cidp->chip_label = 5723;
223110862SYong.Tan@Sun.COM 		else if (cidp->device == DEVICE_ID_5764)
223210862SYong.Tan@Sun.COM 			cidp->chip_label = 5764;
223311479SYong.Tan@Sun.COM 		else if (cidp->device == DEVICE_ID_5785)
223411479SYong.Tan@Sun.COM 			cidp->chip_label = 5785;
2235*12854SYong.Tan@Sun.COM 		else if (cidp->device == DEVICE_ID_57780)
2236*12854SYong.Tan@Sun.COM 			cidp->chip_label = 57780;
223710862SYong.Tan@Sun.COM 		else
223810862SYong.Tan@Sun.COM 			cidp->chip_label = 5761;
22399042SYong.Tan@Sun.COM 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
22409042SYong.Tan@Sun.COM 		cidp->pci_type = BGE_PCI_E;
22419042SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22429042SYong.Tan@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22439042SYong.Tan@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22449042SYong.Tan@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
22459042SYong.Tan@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
22469042SYong.Tan@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
22479042SYong.Tan@Sun.COM 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
22489042SYong.Tan@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22499042SYong.Tan@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22501369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22519042SYong.Tan@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
22529042SYong.Tan@Sun.COM 		dev_ok = B_TRUE;
22531369Sdduvall 		break;
22541369Sdduvall 
22559548SCrisson.Hu@Sun.COM 	/* PCI-X device, identical to 5714 */
22567871SGarrett.Damore@Sun.COM 	case DEVICE_ID_5780:
22577871SGarrett.Damore@Sun.COM 		cidp->chip_label = 5780;
22587871SGarrett.Damore@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22597871SGarrett.Damore@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22607871SGarrett.Damore@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22619548SCrisson.Hu@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
22629548SCrisson.Hu@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
22639548SCrisson.Hu@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
22647871SGarrett.Damore@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22657871SGarrett.Damore@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22667871SGarrett.Damore@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
22677871SGarrett.Damore@Sun.COM 		dev_ok = B_TRUE;
22687871SGarrett.Damore@Sun.COM 		break;
22697871SGarrett.Damore@Sun.COM 
22701369Sdduvall 	case DEVICE_ID_5782:
22711369Sdduvall 		/*
22721369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
22731369Sdduvall 		 */
22741369Sdduvall 		cidp->chip_label = 5782;
22751908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22761908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22771908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22781369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
22791369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
22801369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
22811369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22821908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22831369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22842135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
22851369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
22861369Sdduvall 		dev_ok = B_TRUE;
22871369Sdduvall 		break;
22881369Sdduvall 
22891369Sdduvall 	case DEVICE_ID_5788:
22901369Sdduvall 		/*
22911369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
22921369Sdduvall 		 */
22931369Sdduvall 		cidp->chip_label = 5788;
22941908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22951908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22961908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22971369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
22981369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
22991369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
23001369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23011908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23021369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
23031369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
23041369Sdduvall 		dev_ok = B_TRUE;
23051369Sdduvall 		break;
23061369Sdduvall 
23071369Sdduvall 	case DEVICE_ID_5714C:
23081369Sdduvall 		if (cidp->revision >= REVISION_ID_5714_A2)
23091369Sdduvall 			cidp->msi_enabled = bge_enable_msi;
23101369Sdduvall 		/* FALLTHRU */
23111369Sdduvall 	case DEVICE_ID_5714S:
23121369Sdduvall 		cidp->chip_label = 5714;
23131908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
23141908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
23151908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
23161369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
23171369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
23181369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
23191369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
23201369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
23211369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23221908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23231369Sdduvall 		cidp->pci_type = BGE_PCI_E;
23241369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
23251369Sdduvall 		dev_ok = B_TRUE;
23261369Sdduvall 		break;
23271369Sdduvall 
23281369Sdduvall 	case DEVICE_ID_5715C:
23293170Sml149210 	case DEVICE_ID_5715S:
23301369Sdduvall 		cidp->chip_label = 5715;
23311908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
23321908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
23331908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
23341369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
23351369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
23361369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
23371369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
23381369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
23391369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23401908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23411369Sdduvall 		cidp->pci_type = BGE_PCI_E;
23421369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
23431908Sly149593 		if (cidp->revision >= REVISION_ID_5715_A2)
23441908Sly149593 			cidp->msi_enabled = bge_enable_msi;
23451369Sdduvall 		dev_ok = B_TRUE;
23461369Sdduvall 		break;
23471369Sdduvall 
23481369Sdduvall 	case DEVICE_ID_5721:
23491369Sdduvall 		cidp->chip_label = 5721;
23501908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
23511908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
23521908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
23531369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
23541369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
23551369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
23561369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
23571369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23581908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23591369Sdduvall 		cidp->pci_type = BGE_PCI_E;
23601369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
23611369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
23621369Sdduvall 		dev_ok = B_TRUE;
23631369Sdduvall 		break;
23641369Sdduvall 
23657316SCrisson.Hu@Sun.COM 	case DEVICE_ID_5722:
23667316SCrisson.Hu@Sun.COM 		cidp->chip_label = 5722;
23677316SCrisson.Hu@Sun.COM 		cidp->pci_type = BGE_PCI_E;
23687316SCrisson.Hu@Sun.COM 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
23697316SCrisson.Hu@Sun.COM 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
23707316SCrisson.Hu@Sun.COM 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
23717316SCrisson.Hu@Sun.COM 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
23727316SCrisson.Hu@Sun.COM 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
23737316SCrisson.Hu@Sun.COM 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
23747316SCrisson.Hu@Sun.COM 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
23757316SCrisson.Hu@Sun.COM 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23767316SCrisson.Hu@Sun.COM 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23777316SCrisson.Hu@Sun.COM 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
23787316SCrisson.Hu@Sun.COM 		cidp->statistic_type = BGE_STAT_REG;
23797316SCrisson.Hu@Sun.COM 		dev_ok = B_TRUE;
23807316SCrisson.Hu@Sun.COM 		break;
23817316SCrisson.Hu@Sun.COM 
23821369Sdduvall 	case DEVICE_ID_5751:
23831369Sdduvall 	case DEVICE_ID_5751M:
23841369Sdduvall 		cidp->chip_label = 5751;
23851908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
23861908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
23871908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
23881369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
23891369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
23901369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
23911369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
23921369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
23931908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
23941369Sdduvall 		cidp->pci_type = BGE_PCI_E;
23951369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
23961369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
23971369Sdduvall 		dev_ok = B_TRUE;
23981369Sdduvall 		break;
23991369Sdduvall 
24002675Szh199473 	case DEVICE_ID_5752:
24012675Szh199473 	case DEVICE_ID_5752M:
24022675Szh199473 		cidp->chip_label = 5752;
24032675Szh199473 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
24042675Szh199473 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
24052675Szh199473 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
24062675Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
24072675Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
24082675Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
24092675Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
24102675Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
24112675Szh199473 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
24122675Szh199473 		cidp->pci_type = BGE_PCI_E;
24132675Szh199473 		cidp->statistic_type = BGE_STAT_REG;
24142675Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
24152675Szh199473 		dev_ok = B_TRUE;
24162675Szh199473 		break;
24172675Szh199473 
24182135Szh199473 	case DEVICE_ID_5789:
24192135Szh199473 		cidp->chip_label = 5789;
24202135Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
24212135Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
24222135Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
24232135Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
24242135Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
24252135Szh199473 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
24262135Szh199473 		cidp->pci_type = BGE_PCI_E;
24272135Szh199473 		cidp->statistic_type = BGE_STAT_REG;
24282135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
24292135Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
24302135Szh199473 		cidp->msi_enabled = B_TRUE;
24312135Szh199473 		dev_ok = B_TRUE;
24322135Szh199473 		break;
24332135Szh199473 
24341369Sdduvall 	}
24351369Sdduvall 
24361369Sdduvall 	/*
24371369Sdduvall 	 * Setup the default jumbo parameter.
24381369Sdduvall 	 */
24391369Sdduvall 	cidp->ethmax_size = ETHERMAX;
24401369Sdduvall 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
24411908Sly149593 	cidp->std_buf_size = BGE_STD_BUFF_SIZE;
24421369Sdduvall 
24431369Sdduvall 	/*
24441369Sdduvall 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
24451369Sdduvall 	 * setup below jumbo specific parameters.
24461908Sly149593 	 *
24471908Sly149593 	 * For BCM5714/5715, there is only one standard receive ring. So the
24481908Sly149593 	 * std buffer size should be set to BGE_JUMBO_BUFF_SIZE when jumbo
24491908Sly149593 	 * feature is enabled.
24501369Sdduvall 	 */
245110162SYong.Tan@Sun.COM 	if (!(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
245210464SYong.Tan@Sun.COM 	    (cidp->default_mtu > BGE_DEFAULT_MTU)) {
24534588Sml149210 		if (DEVICE_5714_SERIES_CHIPSETS(bgep)) {
24541908Sly149593 			cidp->mbuf_lo_water_rdma =
24551908Sly149593 			    RDMA_MBUF_LOWAT_5714_JUMBO;
24561908Sly149593 			cidp->mbuf_lo_water_rmac =
24571908Sly149593 			    MAC_RX_MBUF_LOWAT_5714_JUMBO;
24581908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_5714_JUMBO;
24591908Sly149593 			cidp->jumbo_slots = 0;
24601908Sly149593 			cidp->std_buf_size = BGE_JUMBO_BUFF_SIZE;
24614588Sml149210 		} else {
24621908Sly149593 			cidp->mbuf_lo_water_rdma =
24631908Sly149593 			    RDMA_MBUF_LOWAT_JUMBO;
24641908Sly149593 			cidp->mbuf_lo_water_rmac =
24651908Sly149593 			    MAC_RX_MBUF_LOWAT_JUMBO;
24661908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
24671908Sly149593 			cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
24681908Sly149593 		}
24691369Sdduvall 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
24701369Sdduvall 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
24711369Sdduvall 		cidp->ethmax_size = cidp->default_mtu +
24721369Sdduvall 		    sizeof (struct ether_header);
24731369Sdduvall 	}
24741369Sdduvall 
24751369Sdduvall 	/*
24761369Sdduvall 	 * Identify the NV memory type: SEEPROM or Flash?
24771369Sdduvall 	 */
24781369Sdduvall 	cidp->nvtype = bge_nvmem_id(bgep);
24791369Sdduvall 
24801369Sdduvall 	/*
24811369Sdduvall 	 * Now, we want to check whether this device is part of a
24821369Sdduvall 	 * supported subsystem (e.g., on the motherboard of a Sun
24831369Sdduvall 	 * branded platform).
24841369Sdduvall 	 *
24851369Sdduvall 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
24861369Sdduvall 	 */
24871369Sdduvall 	if (cidp->subven == VENDOR_ID_SUN)
24881369Sdduvall 		sys_ok = B_TRUE;
24891369Sdduvall 
24901369Sdduvall 	/*
24911369Sdduvall 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
24921369Sdduvall 	 * Note: 0x14e41647 should *not* appear in the list, but the code
24931369Sdduvall 	 * doesn't enforce that.
24941369Sdduvall 	 */
24951369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
24964588Sml149210 	    DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
24971369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
24981369Sdduvall 		/*
24991369Sdduvall 		 * Got the list; scan for a matching subsystem vendor/device
25001369Sdduvall 		 */
25011369Sdduvall 		subid = (cidp->subven << 16) | cidp->subdev;
25021369Sdduvall 		while (i--)
25031369Sdduvall 			if (ids[i] == subid)
25041369Sdduvall 				sys_ok = B_TRUE;
25051369Sdduvall 		ddi_prop_free(ids);
25061369Sdduvall 	}
25071369Sdduvall 
25081369Sdduvall 	/*
25091369Sdduvall 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
25101369Sdduvall 	 *
25111369Sdduvall 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
25121369Sdduvall 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
25131369Sdduvall 	 * we have to check specially for the exact device paths to the
25141369Sdduvall 	 * motherboard devices on those platforms ;-(
25151369Sdduvall 	 *
25161369Sdduvall 	 * Note: we can't just use the "supported-subsystems" mechanism
25171369Sdduvall 	 * above, because the entry would have to be 0x14e41647 -- which
25181369Sdduvall 	 * would then accept *any* plugin card that *didn't* contain a
25191369Sdduvall 	 * (valid) SEEPROM ;-(
25201369Sdduvall 	 */
25211369Sdduvall 	sysname = ddi_node_name(ddi_root_node());
25221369Sdduvall 	devname = ddi_pathname(bgep->devinfo, buf);
25231369Sdduvall 	ASSERT(strlen(devname) > 0);
25241369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
25251369Sdduvall 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
25261369Sdduvall 			sys_ok = B_TRUE;
25271369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
25281369Sdduvall 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
25291369Sdduvall 			sys_ok = B_TRUE;
25301369Sdduvall 
25311369Sdduvall 	/*
25321369Sdduvall 	 * Now check what we've discovered: is this truly a supported
25331369Sdduvall 	 * chip on (the motherboard of) a supported platform?
25341369Sdduvall 	 *
25351369Sdduvall 	 * Possible problems here:
25369165SYong.Tan@Sun.COM 	 * 1)	it's a completely unheard-of chip
25371369Sdduvall 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
25381369Sdduvall 	 * 3)	it's a chip we would support if it were on the motherboard
25391369Sdduvall 	 *	of a Sun platform, but this one isn't ;-(
25401369Sdduvall 	 */
25411369Sdduvall 	if (cidp->chip_label == 0)
25421369Sdduvall 		bge_problem(bgep,
25434588Sml149210 		    "Device 'pci%04x,%04x' not recognized (%d?)",
25444588Sml149210 		    cidp->vendor, cidp->device, cidp->device);
25451369Sdduvall 	else if (!dev_ok)
25461369Sdduvall 		bge_problem(bgep,
25474588Sml149210 		    "Device 'pci%04x,%04x' (%d) revision %d not supported",
25484588Sml149210 		    cidp->vendor, cidp->device, cidp->chip_label,
25494588Sml149210 		    cidp->revision);
25501369Sdduvall #if	BGE_DEBUGGING
25511369Sdduvall 	else if (!sys_ok)
25521369Sdduvall 		bge_problem(bgep,
25534588Sml149210 		    "%d-based subsystem 'pci%04x,%04x' not validated",
25544588Sml149210 		    cidp->chip_label, cidp->subven, cidp->subdev);
25551369Sdduvall #endif
25561369Sdduvall 	else
25571369Sdduvall 		cidp->flags |= CHIP_FLAG_SUPPORTED;
25581865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
25591865Sdilpreet 		return (EIO);
25601865Sdilpreet 	return (0);
25611369Sdduvall }
25621369Sdduvall 
25631369Sdduvall void
bge_chip_msi_trig(bge_t * bgep)25641369Sdduvall bge_chip_msi_trig(bge_t *bgep)
25651369Sdduvall {
25661369Sdduvall 	uint32_t	regval;
25671369Sdduvall 
25681369Sdduvall 	regval = bgep->param_msi_cnt<<4;
25691369Sdduvall 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
25701369Sdduvall 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
25711369Sdduvall }
25721369Sdduvall 
25731369Sdduvall /*
25741369Sdduvall  * Various registers that control the chip's internal engines (state
25751369Sdduvall  * machines) have a <reset> and <enable> bits (fortunately, in the
25761369Sdduvall  * same place in each such register :-).
25771369Sdduvall  *
25781369Sdduvall  * To reset the state machine, the <reset> bit must be written with 1;
25791369Sdduvall  * it will then read back as 1 while the reset is in progress, but
25801369Sdduvall  * self-clear to 0 when the reset completes.
25811369Sdduvall  *
25821369Sdduvall  * To enable a state machine, one must set the <enable> bit, which
25831369Sdduvall  * will continue to read back as 0 until the state machine is running.
25841369Sdduvall  *
25851369Sdduvall  * To disable a state machine, the <enable> bit must be cleared, but
25861369Sdduvall  * it will continue to read back as 1 until the state machine actually
25871369Sdduvall  * stops.
25881369Sdduvall  *
25891369Sdduvall  * This routine implements polling for completion of a reset, enable
25901369Sdduvall  * or disable operation, returning B_TRUE on success (bit reached the
25911369Sdduvall  * required state) or B_FALSE on timeout (200*100us == 20ms).
25921369Sdduvall  */
25931369Sdduvall static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
25941369Sdduvall 					uint32_t mask, uint32_t val);
25951369Sdduvall #pragma	no_inline(bge_chip_poll_engine)
25961369Sdduvall 
25971369Sdduvall static boolean_t
bge_chip_poll_engine(bge_t * bgep,bge_regno_t regno,uint32_t mask,uint32_t val)25981369Sdduvall bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
25991369Sdduvall 	uint32_t mask, uint32_t val)
26001369Sdduvall {
26011369Sdduvall 	uint32_t regval;
26021369Sdduvall 	uint32_t n;
26031369Sdduvall 
26041369Sdduvall 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
26054588Sml149210 	    (void *)bgep, regno, mask, val));
26061369Sdduvall 
26071369Sdduvall 	for (n = 200; n; --n) {
26081369Sdduvall 		regval = bge_reg_get32(bgep, regno);
26091369Sdduvall 		if ((regval & mask) == val)
26101369Sdduvall 			return (B_TRUE);
26111369Sdduvall 		drv_usecwait(100);
26121369Sdduvall 	}
26131369Sdduvall 
261412673SYong.Tan@Sun.COM 	bge_problem(bgep, "bge_chip_poll_engine failed: regno = 0x%lx", regno);
26151865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_NO_RESPONSE);
26161369Sdduvall 	return (B_FALSE);
26171369Sdduvall }
26181369Sdduvall 
26191369Sdduvall /*
26201369Sdduvall  * Various registers that control the chip's internal engines (state
26211369Sdduvall  * machines) have a <reset> bit (fortunately, in the same place in
26221369Sdduvall  * each such register :-).  To reset the state machine, this bit must
26231369Sdduvall  * be written with 1; it will then read back as 1 while the reset is
26241369Sdduvall  * in progress, but self-clear to 0 when the reset completes.
26251369Sdduvall  *
26261369Sdduvall  * This code sets the bit, then polls for it to read back as zero.
26271369Sdduvall  * The return value is B_TRUE on success (reset bit cleared itself),
26281369Sdduvall  * or B_FALSE if the state machine didn't recover :(
26291369Sdduvall  *
26301369Sdduvall  * NOTE: the Core reset is similar to other resets, except that we
26311369Sdduvall  * can't poll for completion, since the Core reset disables memory
26321369Sdduvall  * access!  So we just have to assume that it will all complete in
26331369Sdduvall  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
26341369Sdduvall  */
26351369Sdduvall static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
26361369Sdduvall #pragma	no_inline(bge_chip_reset_engine)
26371369Sdduvall 
26381369Sdduvall static boolean_t
bge_chip_reset_engine(bge_t * bgep,bge_regno_t regno)26391369Sdduvall bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
26401369Sdduvall {
26411369Sdduvall 	uint32_t regval;
26421369Sdduvall 	uint32_t val32;
26431369Sdduvall 
26441369Sdduvall 	regval = bge_reg_get32(bgep, regno);
26451369Sdduvall 
26461369Sdduvall 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
26474588Sml149210 	    (void *)bgep, regno));
26481369Sdduvall 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
26494588Sml149210 	    regno, regval));
26501369Sdduvall 
26511369Sdduvall 	regval |= STATE_MACHINE_RESET_BIT;
26521369Sdduvall 
26531369Sdduvall 	switch (regno) {
26541369Sdduvall 	case MISC_CONFIG_REG:
26551369Sdduvall 		/*
26561369Sdduvall 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
26571369Sdduvall 		 * resetting PCIE block and bringing PCIE link down, bit 29
26581369Sdduvall 		 * in the register needs to be set first, and then set it again
26591369Sdduvall 		 * while the reset bit is written.
26601369Sdduvall 		 * See:P500 of 57xx-PG102-RDS.pdf.
26611369Sdduvall 		 */
26621369Sdduvall 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
266311968SYong.Tan@Sun.COM 		    DEVICE_5717_SERIES_CHIPSETS(bgep)||
26641369Sdduvall 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
26659042SYong.Tan@Sun.COM 		    DEVICE_5723_SERIES_CHIPSETS(bgep)||
26667678SYong.Tan@Sun.COM 		    DEVICE_5714_SERIES_CHIPSETS(bgep)||
26677678SYong.Tan@Sun.COM 		    DEVICE_5906_SERIES_CHIPSETS(bgep)) {
26681369Sdduvall 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
26691369Sdduvall 			if (bgep->chipid.pci_type == BGE_PCI_E) {
26701369Sdduvall 				if (bgep->chipid.asic_rev ==
26711369Sdduvall 				    MHCR_CHIP_REV_5751_A0 ||
26721369Sdduvall 				    bgep->chipid.asic_rev ==
26734330Sml149210 				    MHCR_CHIP_REV_5721_A0 ||
26744330Sml149210 				    bgep->chipid.asic_rev ==
26754330Sml149210 				    MHCR_CHIP_REV_5755_A0) {
26761369Sdduvall 					val32 = bge_reg_get32(bgep,
26771369Sdduvall 					    PHY_TEST_CTRL_REG);
26781369Sdduvall 					if (val32 == (PHY_PCIE_SCRAM_MODE |
26791369Sdduvall 					    PHY_PCIE_LTASS_MODE))
26801369Sdduvall 						bge_reg_put32(bgep,
26811369Sdduvall 						    PHY_TEST_CTRL_REG,
26821369Sdduvall 						    PHY_PCIE_SCRAM_MODE);
26831369Sdduvall 					val32 = pci_config_get32
26841369Sdduvall 					    (bgep->cfg_handle,
26851369Sdduvall 					    PCI_CONF_BGE_CLKCTL);
26861369Sdduvall 					val32 |= CLKCTL_PCIE_A0_FIX;
26871369Sdduvall 					pci_config_put32(bgep->cfg_handle,
26881369Sdduvall 					    PCI_CONF_BGE_CLKCTL, val32);
26891369Sdduvall 				}
26901369Sdduvall 				bge_reg_set32(bgep, regno,
26914588Sml149210 				    MISC_CONFIG_GRC_RESET_DISABLE);
26921369Sdduvall 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
26931369Sdduvall 			}
26941369Sdduvall 		}
26951369Sdduvall 
26961369Sdduvall 		/*
26971369Sdduvall 		 * Special case - causes Core reset
26981369Sdduvall 		 *
26991369Sdduvall 		 * On SPARC v9 we want to ensure that we don't start
27001369Sdduvall 		 * timing until the I/O access has actually reached
27011369Sdduvall 		 * the chip, otherwise we might make the next access
27021369Sdduvall 		 * too early.  And we can't just force the write out
27031369Sdduvall 		 * by following it with a read (even to config space)
27041369Sdduvall 		 * because that would cause the fault we're trying
27051369Sdduvall 		 * to avoid.  Hence the need for membar_sync() here.
27061369Sdduvall 		 */
27071369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
27081369Sdduvall #ifdef	__sparcv9
27091369Sdduvall 		membar_sync();
27101369Sdduvall #endif	/* __sparcv9 */
27111369Sdduvall 		/*
27121369Sdduvall 		 * On some platforms,system need about 300us for
27131369Sdduvall 		 * link setup.
27141369Sdduvall 		 */
27151369Sdduvall 		drv_usecwait(300);
27167678SYong.Tan@Sun.COM 		if (DEVICE_5906_SERIES_CHIPSETS(bgep)) {
27177678SYong.Tan@Sun.COM 			bge_reg_set32(bgep, VCPU_STATUS_REG, VCPU_DRV_RESET);
27187678SYong.Tan@Sun.COM 			bge_reg_clr32(
27197678SYong.Tan@Sun.COM 			    bgep, VCPU_EXT_CTL, VCPU_EXT_CTL_HALF);
27207678SYong.Tan@Sun.COM 		}
27211369Sdduvall 
27221369Sdduvall 		if (bgep->chipid.pci_type == BGE_PCI_E) {
27231369Sdduvall 			/* PCI-E device need more reset time */
27241369Sdduvall 			drv_usecwait(120000);
27251369Sdduvall 
27261369Sdduvall 			/* Set PCIE max payload size and clear error status. */
27272135Szh199473 			if ((bgep->chipid.chip_label == 5721) ||
27282135Szh199473 			    (bgep->chipid.chip_label == 5751) ||
27292675Szh199473 			    (bgep->chipid.chip_label == 5752) ||
27307678SYong.Tan@Sun.COM 			    (bgep->chipid.chip_label == 5789) ||
27317678SYong.Tan@Sun.COM 			    (bgep->chipid.chip_label == 5906)) {
27321369Sdduvall 				pci_config_put16(bgep->cfg_handle,
27334588Sml149210 				    PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
27341369Sdduvall 				pci_config_put16(bgep->cfg_handle,
27354588Sml149210 				    PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
27361369Sdduvall 			}
27379042SYong.Tan@Sun.COM 
27389165SYong.Tan@Sun.COM 			if ((bgep->chipid.chip_label == 5723) ||
27399165SYong.Tan@Sun.COM 			    (bgep->chipid.chip_label == 5761)) {
27409042SYong.Tan@Sun.COM 				pci_config_put16(bgep->cfg_handle,
27419042SYong.Tan@Sun.COM 				    PCI_CONF_DEV_CTRL_5723, READ_REQ_SIZE_MAX);
27429042SYong.Tan@Sun.COM 				pci_config_put16(bgep->cfg_handle,
27439042SYong.Tan@Sun.COM 				    PCI_CONF_DEV_STUS_5723, DEVICE_ERROR_STUS);
27449042SYong.Tan@Sun.COM 			}
27451369Sdduvall 		}
27461369Sdduvall 
27471369Sdduvall 		BGE_PCICHK(bgep);
27481369Sdduvall 		return (B_TRUE);
27491369Sdduvall 
27501369Sdduvall 	default:
27511369Sdduvall 		bge_reg_put32(bgep, regno, regval);
27521369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
27531865Sdilpreet 		    STATE_MACHINE_RESET_BIT, 0));
27541369Sdduvall 	}
27551369Sdduvall }
27561369Sdduvall 
27571369Sdduvall /*
27581369Sdduvall  * Various registers that control the chip's internal engines (state
27591369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
27601369Sdduvall  * each such register :-).  To stop the state machine, this bit must
27611369Sdduvall  * be written with 0, then polled to see when the state machine has
27621369Sdduvall  * actually stopped.
27631369Sdduvall  *
27641369Sdduvall  * The return value is B_TRUE on success (enable bit cleared), or
27651369Sdduvall  * B_FALSE if the state machine didn't stop :(
27661369Sdduvall  */
27671369Sdduvall static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
27681369Sdduvall 						uint32_t morebits);
27691369Sdduvall #pragma	no_inline(bge_chip_disable_engine)
27701369Sdduvall 
27711369Sdduvall static boolean_t
bge_chip_disable_engine(bge_t * bgep,bge_regno_t regno,uint32_t morebits)27721369Sdduvall bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
27731369Sdduvall {
27741369Sdduvall 	uint32_t regval;
27751369Sdduvall 
27761369Sdduvall 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
27774588Sml149210 	    (void *)bgep, regno, morebits));
27781369Sdduvall 
27791369Sdduvall 	switch (regno) {
27801369Sdduvall 	case FTQ_RESET_REG:
27811369Sdduvall 		/*
27823918Sml149210 		 * For Schumacher's bugfix CR6490108
27833918Sml149210 		 */
27843918Sml149210 #ifdef BGE_IPMI_ASF
27853918Sml149210 #ifdef BGE_NETCONSOLE
27863918Sml149210 		if (bgep->asf_enabled)
27873918Sml149210 			return (B_TRUE);
27883918Sml149210 #endif
27893918Sml149210 #endif
27903918Sml149210 		/*
27911369Sdduvall 		 * Not quite like the others; it doesn't
27921369Sdduvall 		 * have an <enable> bit, but instead we
27931369Sdduvall 		 * have to set and then clear all the bits
27941369Sdduvall 		 */
27951369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
27961369Sdduvall 		drv_usecwait(100);
27971369Sdduvall 		bge_reg_put32(bgep, regno, 0);
27981369Sdduvall 		return (B_TRUE);
27991369Sdduvall 
28001369Sdduvall 	default:
28011369Sdduvall 		regval = bge_reg_get32(bgep, regno);
28021369Sdduvall 		regval &= ~STATE_MACHINE_ENABLE_BIT;
28031369Sdduvall 		regval &= ~morebits;
28041369Sdduvall 		bge_reg_put32(bgep, regno, regval);
28051369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
28061865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, 0));
28071369Sdduvall 	}
28081369Sdduvall }
28091369Sdduvall 
28101369Sdduvall /*
28111369Sdduvall  * Various registers that control the chip's internal engines (state
28121369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
28131369Sdduvall  * each such register :-).  To start the state machine, this bit must
28141369Sdduvall  * be written with 1, then polled to see when the state machine has
28151369Sdduvall  * actually started.
28161369Sdduvall  *
28171369Sdduvall  * The return value is B_TRUE on success (enable bit set), or
28181369Sdduvall  * B_FALSE if the state machine didn't start :(
28191369Sdduvall  */
28201369Sdduvall static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
28211369Sdduvall 					uint32_t morebits);
28221369Sdduvall #pragma	no_inline(bge_chip_enable_engine)
28231369Sdduvall 
28241369Sdduvall static boolean_t
bge_chip_enable_engine(bge_t * bgep,bge_regno_t regno,uint32_t morebits)28251369Sdduvall bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
28261369Sdduvall {
28271369Sdduvall 	uint32_t regval;
28281369Sdduvall 
28291369Sdduvall 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
28304588Sml149210 	    (void *)bgep, regno, morebits));
28311369Sdduvall 
28321369Sdduvall 	switch (regno) {
28331369Sdduvall 	case FTQ_RESET_REG:
28343918Sml149210 #ifdef BGE_IPMI_ASF
28353918Sml149210 #ifdef BGE_NETCONSOLE
28363918Sml149210 		if (bgep->asf_enabled)
28373918Sml149210 			return (B_TRUE);
28383918Sml149210 #endif
28393918Sml149210 #endif
28401369Sdduvall 		/*
28411369Sdduvall 		 * Not quite like the others; it doesn't
28421369Sdduvall 		 * have an <enable> bit, but instead we
28431369Sdduvall 		 * have to set and then clear all the bits
28441369Sdduvall 		 */
28451369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
28461369Sdduvall 		drv_usecwait(100);
28471369Sdduvall 		bge_reg_put32(bgep, regno, 0);
28481369Sdduvall 		return (B_TRUE);
28491369Sdduvall 
28501369Sdduvall 	default:
28511369Sdduvall 		regval = bge_reg_get32(bgep, regno);
28521369Sdduvall 		regval |= STATE_MACHINE_ENABLE_BIT;
28531369Sdduvall 		regval |= morebits;
28541369Sdduvall 		bge_reg_put32(bgep, regno, regval);
28551369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
28561865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
28571369Sdduvall 	}
28581369Sdduvall }
28591369Sdduvall 
28601369Sdduvall /*
28611369Sdduvall  * Reprogram the Ethernet, Transmit, and Receive MAC
28621369Sdduvall  * modes to match the param_* variables
28631369Sdduvall  */
28645903Ssowmini void bge_sync_mac_modes(bge_t *bgep);
28651369Sdduvall #pragma	no_inline(bge_sync_mac_modes)
28661369Sdduvall 
28675903Ssowmini void
bge_sync_mac_modes(bge_t * bgep)28681369Sdduvall bge_sync_mac_modes(bge_t *bgep)
28691369Sdduvall {
28701369Sdduvall 	uint32_t macmode;
28711369Sdduvall 	uint32_t regval;
28721369Sdduvall 
28731369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
28741369Sdduvall 
28751369Sdduvall 	/*
28761369Sdduvall 	 * Reprogram the Ethernet MAC mode ...
28771369Sdduvall 	 */
28781369Sdduvall 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
28791369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
28804588Sml149210 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
28817561SCrisson.Hu@Sun.COM 		if (DEVICE_5714_SERIES_CHIPSETS(bgep))
28827561SCrisson.Hu@Sun.COM 			macmode |= ETHERNET_MODE_LINK_POLARITY;
28837561SCrisson.Hu@Sun.COM 		else
28847561SCrisson.Hu@Sun.COM 			macmode &= ~ETHERNET_MODE_LINK_POLARITY;
28851369Sdduvall 	else
28861369Sdduvall 		macmode |= ETHERNET_MODE_LINK_POLARITY;
28871369Sdduvall 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
28881369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
28897561SCrisson.Hu@Sun.COM 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC)) {
28907561SCrisson.Hu@Sun.COM 		if (DEVICE_5714_SERIES_CHIPSETS(bgep))
28917561SCrisson.Hu@Sun.COM 			macmode |= ETHERNET_MODE_PORTMODE_GMII;
28927561SCrisson.Hu@Sun.COM 		else
28937561SCrisson.Hu@Sun.COM 			macmode |= ETHERNET_MODE_PORTMODE_TBI;
28947561SCrisson.Hu@Sun.COM 	} else if (bgep->param_link_speed == 10 ||
28957561SCrisson.Hu@Sun.COM 	    bgep->param_link_speed == 100)
28961369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_MII;
28971369Sdduvall 	else
28981369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
28991369Sdduvall 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
29001369Sdduvall 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
29011369Sdduvall 	else
29021369Sdduvall 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
29031369Sdduvall 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
29041369Sdduvall 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
29051369Sdduvall 	else
29061369Sdduvall 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
29071369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
29081369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
29094588Sml149210 	    (void *)bgep, regval, macmode));
29101369Sdduvall 
29111369Sdduvall 	/*
29121369Sdduvall 	 * ... the Transmit MAC mode ...
29131369Sdduvall 	 */
29141369Sdduvall 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
29151369Sdduvall 	if (bgep->param_link_tx_pause)
29161369Sdduvall 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
29171369Sdduvall 	else
29181369Sdduvall 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
29191369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
29201369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
29214588Sml149210 	    (void *)bgep, regval, macmode));
29221369Sdduvall 
29231369Sdduvall 	/*
29241369Sdduvall 	 * ... and the Receive MAC mode
29251369Sdduvall 	 */
29261369Sdduvall 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
29271369Sdduvall 	if (bgep->param_link_rx_pause)
29281369Sdduvall 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
29291369Sdduvall 	else
29301369Sdduvall 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
29311369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
29321369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
29334588Sml149210 	    (void *)bgep, regval, macmode));
293411479SYong.Tan@Sun.COM 
293511479SYong.Tan@Sun.COM 	/*
293611479SYong.Tan@Sun.COM 	 * For BCM5785, we need to configure the link status in the MI Status
293711479SYong.Tan@Sun.COM 	 * register with a write command when auto-polling is disabled.
293811479SYong.Tan@Sun.COM 	 */
293911479SYong.Tan@Sun.COM 	if (bgep->chipid.device == DEVICE_ID_5785)
294011479SYong.Tan@Sun.COM 		if (bgep->param_link_speed == 10)
294111479SYong.Tan@Sun.COM 			bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK
294211479SYong.Tan@Sun.COM 			    | MI_STATUS_10MBPS);
294311479SYong.Tan@Sun.COM 		else
294411479SYong.Tan@Sun.COM 			bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
29451369Sdduvall }
29461369Sdduvall 
29471369Sdduvall /*
29481369Sdduvall  * bge_chip_sync() -- program the chip with the unicast MAC address,
29491369Sdduvall  * the multicast hash table, the required level of promiscuity, and
29501369Sdduvall  * the current loopback mode ...
29511369Sdduvall  */
29521408Srandyf #ifdef BGE_IPMI_ASF
29531865Sdilpreet int bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
29541408Srandyf #else
29551865Sdilpreet int bge_chip_sync(bge_t *bgep);
29561408Srandyf #endif
29571369Sdduvall #pragma	no_inline(bge_chip_sync)
29581369Sdduvall 
29591865Sdilpreet int
29601408Srandyf #ifdef BGE_IPMI_ASF
bge_chip_sync(bge_t * bgep,boolean_t asf_keeplive)29611408Srandyf bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
29621408Srandyf #else
29631369Sdduvall bge_chip_sync(bge_t *bgep)
29641408Srandyf #endif
29651369Sdduvall {
29661369Sdduvall 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
29671369Sdduvall 	boolean_t promisc;
29681369Sdduvall 	uint64_t macaddr;
29698922SYong.Tan@Sun.COM 	uint32_t fill = 0;
29702331Skrgopi 	int i, j;
29711865Sdilpreet 	int retval = DDI_SUCCESS;
29721369Sdduvall 
29731369Sdduvall 	BGE_TRACE(("bge_chip_sync($%p)",
29745903Ssowmini 	    (void *)bgep));
29751369Sdduvall 
29761369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
29771369Sdduvall 
29781369Sdduvall 	promisc = B_FALSE;
29791369Sdduvall 	fill = ~(uint32_t)0;
29801369Sdduvall 
29811369Sdduvall 	if (bgep->promisc)
29821369Sdduvall 		promisc = B_TRUE;
29831369Sdduvall 	else
29841369Sdduvall 		fill = (uint32_t)0;
29851369Sdduvall 
29861369Sdduvall 	/*
29871369Sdduvall 	 * If the TX/RX MAC engines are already running, we should stop
29881369Sdduvall 	 * them (and reset the RX engine) before changing the parameters.
29891369Sdduvall 	 * If they're not running, this will have no effect ...
29901369Sdduvall 	 *
29911369Sdduvall 	 * NOTE: this is currently disabled by default because stopping
29921369Sdduvall 	 * and restarting the Tx engine may cause an outgoing packet in
29931369Sdduvall 	 * transit to be truncated.  Also, stopping and restarting the
29941369Sdduvall 	 * Rx engine seems to not work correctly on the 5705.  Testing
29951369Sdduvall 	 * has not (yet!) revealed any problems with NOT stopping and
29961369Sdduvall 	 * restarting these engines (and Broadcom say their drivers don't
29971369Sdduvall 	 * do this), but if it is found to cause problems, this variable
29981369Sdduvall 	 * can be patched to re-enable the old behaviour ...
29991369Sdduvall 	 */
30001369Sdduvall 	if (bge_stop_start_on_sync) {
30011408Srandyf #ifdef BGE_IPMI_ASF
30021865Sdilpreet 		if (!bgep->asf_enabled) {
30031865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
30041865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
30051865Sdilpreet 				retval = DDI_FAILURE;
30061408Srandyf 		} else {
30071865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
30081865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
30091865Sdilpreet 				retval = DDI_FAILURE;
30101408Srandyf 		}
30111408Srandyf #else
30121865Sdilpreet 		if (!bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
30131865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
30141865Sdilpreet 			retval = DDI_FAILURE;
30151408Srandyf #endif
30161865Sdilpreet 		if (!bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
30171865Sdilpreet 			retval = DDI_FAILURE;
30181865Sdilpreet 		if (!bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG))
30191865Sdilpreet 			retval = DDI_FAILURE;
30201369Sdduvall 	}
30211369Sdduvall 
30221369Sdduvall 	/*
30231369Sdduvall 	 * Reprogram the hashed multicast address table ...
30241369Sdduvall 	 */
30251369Sdduvall 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
30266546Sgh162552 		bge_reg_put32(bgep, MAC_HASH_REG(i), 0);
30276546Sgh162552 
30286546Sgh162552 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
30291369Sdduvall 		bge_reg_put32(bgep, MAC_HASH_REG(i),
30301369Sdduvall 			bgep->mcast_hash[i] | fill);
30311369Sdduvall 
30321408Srandyf #ifdef BGE_IPMI_ASF
30331408Srandyf 	if (!bgep->asf_enabled || !asf_keeplive) {
30341408Srandyf #endif
30351408Srandyf 		/*
30362331Skrgopi 		 * Transform the MAC address(es) from host to chip format, then
30371408Srandyf 		 * reprogram the transmit random backoff seed and the unicast
30381408Srandyf 		 * MAC address(es) ...
30391408Srandyf 		 */
30402331Skrgopi 		for (j = 0; j < MAC_ADDRESS_REGS_MAX; j++) {
30418922SYong.Tan@Sun.COM 			for (i = 0, macaddr = 0ull;
30422331Skrgopi 			    i < ETHERADDRL; ++i) {
30432331Skrgopi 				macaddr <<= 8;
30442331Skrgopi 				macaddr |= bgep->curr_addr[j].addr[i];
30452331Skrgopi 			}
30468922SYong.Tan@Sun.COM 			fill += (macaddr >> 16) + (macaddr & 0xffffffff);
30472331Skrgopi 			bge_reg_put64(bgep, MAC_ADDRESS_REG(j), macaddr);
30488275SEric Cheng 
30498275SEric Cheng 			BGE_DEBUG(("bge_chip_sync($%p) "
30508275SEric Cheng 			    "setting MAC address %012llx",
30518275SEric Cheng 			    (void *)bgep, macaddr));
30521408Srandyf 		}
30531408Srandyf #ifdef BGE_IPMI_ASF
30541369Sdduvall 	}
30551408Srandyf #endif
30568922SYong.Tan@Sun.COM 	/*
30578922SYong.Tan@Sun.COM 	 * Set random seed of backoff interval
30588922SYong.Tan@Sun.COM 	 *   - Writing zero means no backoff interval
30598922SYong.Tan@Sun.COM 	 */
30608922SYong.Tan@Sun.COM 	fill = ((fill >> 20) + (fill >> 10) + fill) & 0x3ff;
30618922SYong.Tan@Sun.COM 	if (fill == 0)
30628922SYong.Tan@Sun.COM 		fill = 1;
30638922SYong.Tan@Sun.COM 	bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
30641369Sdduvall 
30651369Sdduvall 	/*
30661369Sdduvall 	 * Set or clear the PROMISCUOUS mode bit
30671369Sdduvall 	 */
30681369Sdduvall 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
30691369Sdduvall 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
30701369Sdduvall 
30711369Sdduvall 	/*
30721369Sdduvall 	 * Sync the rest of the MAC modes too ...
30731369Sdduvall 	 */
30741369Sdduvall 	bge_sync_mac_modes(bgep);
30751369Sdduvall 
30761369Sdduvall 	/*
30771369Sdduvall 	 * Restart RX/TX MAC engines if required ...
30781369Sdduvall 	 */
30791369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
30801865Sdilpreet 		if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
30811865Sdilpreet 			retval = DDI_FAILURE;
30821408Srandyf #ifdef BGE_IPMI_ASF
30831865Sdilpreet 		if (!bgep->asf_enabled) {
30841865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
30851865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
30861865Sdilpreet 				retval = DDI_FAILURE;
30871408Srandyf 		} else {
30881865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
30891865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
30901865Sdilpreet 				retval = DDI_FAILURE;
30911408Srandyf 		}
30921408Srandyf #else
30931865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
30941865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
30951865Sdilpreet 			retval = DDI_FAILURE;
30961408Srandyf #endif
30971369Sdduvall 	}
30981865Sdilpreet 	return (retval);
30991369Sdduvall }
31001369Sdduvall 
31011369Sdduvall /*
31021369Sdduvall  * This array defines the sequence of state machine control registers
31031369Sdduvall  * in which the <enable> bit must be cleared to bring the chip to a
31041369Sdduvall  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
31051369Sdduvall  */
31061369Sdduvall static bge_regno_t shutdown_engine_regs[] = {
31071369Sdduvall 	RECEIVE_MAC_MODE_REG,
31081369Sdduvall 	RCV_BD_INITIATOR_MODE_REG,
31091369Sdduvall 	RCV_LIST_PLACEMENT_MODE_REG,
31101369Sdduvall 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
31111369Sdduvall 	RCV_DATA_BD_INITIATOR_MODE_REG,
31121369Sdduvall 	RCV_DATA_COMPLETION_MODE_REG,
31131369Sdduvall 	RCV_BD_COMPLETION_MODE_REG,
31141369Sdduvall 
31151369Sdduvall 	SEND_BD_SELECTOR_MODE_REG,
31161369Sdduvall 	SEND_BD_INITIATOR_MODE_REG,
31171369Sdduvall 	SEND_DATA_INITIATOR_MODE_REG,
31181369Sdduvall 	READ_DMA_MODE_REG,
31191369Sdduvall 	SEND_DATA_COMPLETION_MODE_REG,
31201369Sdduvall 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
31211369Sdduvall 	SEND_BD_COMPLETION_MODE_REG,
31221369Sdduvall 	TRANSMIT_MAC_MODE_REG,
31231369Sdduvall 
31241369Sdduvall 	HOST_COALESCE_MODE_REG,
31251369Sdduvall 	WRITE_DMA_MODE_REG,
31261369Sdduvall 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
31271369Sdduvall 	FTQ_RESET_REG,		/* special - see code	*/
31281369Sdduvall 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
31291369Sdduvall 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
31301369Sdduvall 	BGE_REGNO_NONE		/* terminator		*/
31311369Sdduvall };
31321369Sdduvall 
31337656SSherry.Moore@Sun.COM #ifndef __sparc
31347656SSherry.Moore@Sun.COM static bge_regno_t quiesce_regs[] = {
31357656SSherry.Moore@Sun.COM 	READ_DMA_MODE_REG,
31367656SSherry.Moore@Sun.COM 	DMA_COMPLETION_MODE_REG,
31377656SSherry.Moore@Sun.COM 	WRITE_DMA_MODE_REG,
31387656SSherry.Moore@Sun.COM 	BGE_REGNO_NONE
31397656SSherry.Moore@Sun.COM };
31407656SSherry.Moore@Sun.COM 
31417656SSherry.Moore@Sun.COM void bge_chip_stop_nonblocking(bge_t *bgep);
31427656SSherry.Moore@Sun.COM #pragma no_inline(bge_chip_stop_nonblocking)
31437656SSherry.Moore@Sun.COM 
31447656SSherry.Moore@Sun.COM /*
31457656SSherry.Moore@Sun.COM  * This function is called by bge_quiesce(). We
31467656SSherry.Moore@Sun.COM  * turn off all the DMA engines here.
31477656SSherry.Moore@Sun.COM  */
31487656SSherry.Moore@Sun.COM void
bge_chip_stop_nonblocking(bge_t * bgep)31497656SSherry.Moore@Sun.COM bge_chip_stop_nonblocking(bge_t *bgep)
31507656SSherry.Moore@Sun.COM {
31517656SSherry.Moore@Sun.COM 	bge_regno_t *rbp;
31527656SSherry.Moore@Sun.COM 
31537656SSherry.Moore@Sun.COM 	/*
31547656SSherry.Moore@Sun.COM 	 * Flag that no more activity may be initiated
31557656SSherry.Moore@Sun.COM 	 */
31567656SSherry.Moore@Sun.COM 	bgep->progress &= ~PROGRESS_READY;
31577656SSherry.Moore@Sun.COM 
31587656SSherry.Moore@Sun.COM 	rbp = quiesce_regs;
31597656SSherry.Moore@Sun.COM 	while (*rbp != BGE_REGNO_NONE) {
31607656SSherry.Moore@Sun.COM 		(void) bge_chip_disable_engine(bgep, *rbp, 0);
31617656SSherry.Moore@Sun.COM 		++rbp;
31627656SSherry.Moore@Sun.COM 	}
31637656SSherry.Moore@Sun.COM 
31647656SSherry.Moore@Sun.COM 	bgep->bge_chip_state = BGE_CHIP_STOPPED;
31657656SSherry.Moore@Sun.COM }
31667656SSherry.Moore@Sun.COM 
31677656SSherry.Moore@Sun.COM #endif
31687656SSherry.Moore@Sun.COM 
31691369Sdduvall /*
31701369Sdduvall  * bge_chip_stop() -- stop all chip processing
31711369Sdduvall  *
31721369Sdduvall  * If the <fault> parameter is B_TRUE, we're stopping the chip because
31731369Sdduvall  * we've detected a problem internally; otherwise, this is a normal
31741369Sdduvall  * (clean) stop (at user request i.e. the last STREAM has been closed).
31751369Sdduvall  */
31761369Sdduvall void bge_chip_stop(bge_t *bgep, boolean_t fault);
31771369Sdduvall #pragma	no_inline(bge_chip_stop)
31781369Sdduvall 
31791369Sdduvall void
bge_chip_stop(bge_t * bgep,boolean_t fault)31801369Sdduvall bge_chip_stop(bge_t *bgep, boolean_t fault)
31811369Sdduvall {
31821369Sdduvall 	bge_regno_t regno;
31831369Sdduvall 	bge_regno_t *rbp;
31841369Sdduvall 	boolean_t ok;
31851369Sdduvall 
31861369Sdduvall 	BGE_TRACE(("bge_chip_stop($%p)",
31874588Sml149210 	    (void *)bgep));
31881369Sdduvall 
31891369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
31901369Sdduvall 
31911369Sdduvall 	rbp = shutdown_engine_regs;
31921369Sdduvall 	/*
31931369Sdduvall 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
31941369Sdduvall 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
31951369Sdduvall 	 * -ory arbiter should not be disabled.
31961369Sdduvall 	 */
31971369Sdduvall 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
31981369Sdduvall 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
31994588Sml149210 				ok &= bge_chip_disable_engine(bgep, regno, 0);
32001369Sdduvall 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
32014588Sml149210 			    (regno != DMA_COMPLETION_MODE_REG) &&
32024588Sml149210 			    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
32034588Sml149210 			    (regno != BUFFER_MANAGER_MODE_REG) &&
32044588Sml149210 			    (regno != MEMORY_ARBITER_MODE_REG))
32054588Sml149210 				ok &= bge_chip_disable_engine(bgep,
32064588Sml149210 				    regno, 0);
32071369Sdduvall 	}
32081369Sdduvall 
32091865Sdilpreet 	if (!ok && !fault)
32101865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
32111865Sdilpreet 
32121369Sdduvall 	/*
32131369Sdduvall 	 * Finally, disable (all) MAC events & clear the MAC status
32141369Sdduvall 	 */
32151369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
32161369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
32171369Sdduvall 
32181369Sdduvall 	/*
32191865Sdilpreet 	 * if we're stopping the chip because of a detected fault then do
32201865Sdilpreet 	 * appropriate actions
32211369Sdduvall 	 */
32221865Sdilpreet 	if (fault) {
32231865Sdilpreet 		if (bgep->bge_chip_state != BGE_CHIP_FAULT) {
32241865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_FAULT;
32255903Ssowmini 			if (!bgep->manual_reset)
32265903Ssowmini 				ddi_fm_service_impact(bgep->devinfo,
32275903Ssowmini 				    DDI_SERVICE_LOST);
32281865Sdilpreet 			if (bgep->bge_dma_error) {
32291865Sdilpreet 				/*
32301865Sdilpreet 				 * need to free buffers in case the fault was
32311865Sdilpreet 				 * due to a memory error in a buffer - got to
32321865Sdilpreet 				 * do a fair bit of tidying first
32331865Sdilpreet 				 */
32341865Sdilpreet 				if (bgep->progress & PROGRESS_KSTATS) {
32351865Sdilpreet 					bge_fini_kstats(bgep);
32361865Sdilpreet 					bgep->progress &= ~PROGRESS_KSTATS;
32371865Sdilpreet 				}
32381865Sdilpreet 				if (bgep->progress & PROGRESS_INTR) {
32391865Sdilpreet 					bge_intr_disable(bgep);
32401865Sdilpreet 					rw_enter(bgep->errlock, RW_WRITER);
32411865Sdilpreet 					bge_fini_rings(bgep);
32421865Sdilpreet 					rw_exit(bgep->errlock);
32431865Sdilpreet 					bgep->progress &= ~PROGRESS_INTR;
32441865Sdilpreet 				}
32451865Sdilpreet 				if (bgep->progress & PROGRESS_BUFS) {
32461865Sdilpreet 					bge_free_bufs(bgep);
32471865Sdilpreet 					bgep->progress &= ~PROGRESS_BUFS;
32481865Sdilpreet 				}
32491865Sdilpreet 				bgep->bge_dma_error = B_FALSE;
32501865Sdilpreet 			}
32511865Sdilpreet 		}
32521865Sdilpreet 	} else
32531369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
32541369Sdduvall }
32551369Sdduvall 
32561369Sdduvall /*
32571369Sdduvall  * Poll for completion of chip's ROM firmware; also, at least on the
32581369Sdduvall  * first time through, find and return the hardware MAC address, if any.
32591369Sdduvall  */
32601369Sdduvall static uint64_t bge_poll_firmware(bge_t *bgep);
32611369Sdduvall #pragma	no_inline(bge_poll_firmware)
32621369Sdduvall 
32631369Sdduvall static uint64_t
bge_poll_firmware(bge_t * bgep)32641369Sdduvall bge_poll_firmware(bge_t *bgep)
32651369Sdduvall {
32661369Sdduvall 	uint64_t magic;
32671369Sdduvall 	uint64_t mac;
32687678SYong.Tan@Sun.COM 	uint32_t gen, val;
32691369Sdduvall 	uint32_t i;
32701369Sdduvall 
32711369Sdduvall 	/*
32721369Sdduvall 	 * Step 19: poll for firmware completion (GENCOMM port set
32731369Sdduvall 	 * to the ones complement of T3_MAGIC_NUMBER).
32741369Sdduvall 	 *
32751369Sdduvall 	 * While we're at it, we also read the MAC address register;
32762135Szh199473 	 * at some stage the firmware will load this with the
32771369Sdduvall 	 * factory-set value.
32781369Sdduvall 	 *
32791369Sdduvall 	 * When both the magic number and the MAC address are set,
32801369Sdduvall 	 * we're done; but we impose a time limit of one second
32811369Sdduvall 	 * (1000*1000us) in case the firmware fails in some fashion
32821369Sdduvall 	 * or the SEEPROM that provides that MAC address isn't fitted.
32831369Sdduvall 	 *
32841369Sdduvall 	 * After the first time through (chip state != INITIAL), we
32851369Sdduvall 	 * don't need the MAC address to be set (we've already got it
32861369Sdduvall 	 * or not, from the first time), so we don't wait for it, but
32871369Sdduvall 	 * we still have to wait for the T3_MAGIC_NUMBER.
32881369Sdduvall 	 *
32891369Sdduvall 	 * Note: the magic number is only a 32-bit quantity, but the NIC
32901369Sdduvall 	 * memory is 64-bit (and big-endian) internally.  Addressing the
32911369Sdduvall 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
32921369Sdduvall 	 * it work correctly on both big- and little-endian hosts.
32931369Sdduvall 	 */
32947678SYong.Tan@Sun.COM 	if (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
32957678SYong.Tan@Sun.COM 	    MHCR_CHIP_ASIC_REV_5906) {
32967678SYong.Tan@Sun.COM 		for (i = 0; i < 1000; ++i) {
32977678SYong.Tan@Sun.COM 			drv_usecwait(1000);
32987678SYong.Tan@Sun.COM 			val = bge_reg_get32(bgep, VCPU_STATUS_REG);
32997678SYong.Tan@Sun.COM 			if (val & VCPU_INIT_DONE)
33007678SYong.Tan@Sun.COM 				break;
33017678SYong.Tan@Sun.COM 		}
33027678SYong.Tan@Sun.COM 		BGE_DEBUG(("bge_poll_firmware($%p): return after %d loops",
33037678SYong.Tan@Sun.COM 		    (void *)bgep, i));
33041369Sdduvall 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
33057678SYong.Tan@Sun.COM 	} else {
33067678SYong.Tan@Sun.COM 		for (i = 0; i < 1000; ++i) {
33077678SYong.Tan@Sun.COM 			drv_usecwait(1000);
33087678SYong.Tan@Sun.COM 			gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
33097678SYong.Tan@Sun.COM 			if (i == 0 && DEVICE_5704_SERIES_CHIPSETS(bgep))
33107678SYong.Tan@Sun.COM 				drv_usecwait(100000);
33117678SYong.Tan@Sun.COM 			mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
33121408Srandyf #ifdef BGE_IPMI_ASF
33137678SYong.Tan@Sun.COM 			if (!bgep->asf_enabled) {
33141408Srandyf #endif
33157678SYong.Tan@Sun.COM 				if (gen != ~T3_MAGIC_NUMBER)
33167678SYong.Tan@Sun.COM 					continue;
33171408Srandyf #ifdef BGE_IPMI_ASF
33187678SYong.Tan@Sun.COM 			}
33191408Srandyf #endif
33207678SYong.Tan@Sun.COM 			if (mac != 0ULL)
33217678SYong.Tan@Sun.COM 				break;
33227678SYong.Tan@Sun.COM 			if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
33237678SYong.Tan@Sun.COM 				break;
33247678SYong.Tan@Sun.COM 		}
33251369Sdduvall 	}
33261369Sdduvall 
33271369Sdduvall 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
33281369Sdduvall 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
33294588Sml149210 	    (void *)bgep, gen, i));
33301369Sdduvall 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
33314588Sml149210 	    mac, magic));
33321369Sdduvall 
33331369Sdduvall 	return (mac);
33341369Sdduvall }
33351369Sdduvall 
33363390Szh199473 /*
33373390Szh199473  * Maximum times of trying to get the NVRAM access lock
33383390Szh199473  * by calling bge_nvmem_acquire()
33393390Szh199473  */
33403390Szh199473 #define	MAX_TRY_NVMEM_ACQUIRE	10000
33413390Szh199473 
33421408Srandyf #ifdef BGE_IPMI_ASF
33431865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
33441408Srandyf #else
33451865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
33461408Srandyf #endif
33471369Sdduvall #pragma	no_inline(bge_chip_reset)
33481369Sdduvall 
33491865Sdilpreet int
33501408Srandyf #ifdef BGE_IPMI_ASF
bge_chip_reset(bge_t * bgep,boolean_t enable_dma,uint_t asf_mode)33511408Srandyf bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
33521408Srandyf #else
33531369Sdduvall bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
33541408Srandyf #endif
33551369Sdduvall {
33561369Sdduvall 	chip_id_t chipid;
33571369Sdduvall 	uint64_t mac;
33581908Sly149593 	uint64_t magic;
33591369Sdduvall 	uint32_t modeflags;
33601369Sdduvall 	uint32_t mhcr;
33611369Sdduvall 	uint32_t sx0;
33623390Szh199473 	uint32_t i, tries;
33631408Srandyf #ifdef BGE_IPMI_ASF
33641408Srandyf 	uint32_t mailbox;
33651408Srandyf #endif
33661865Sdilpreet 	int retval = DDI_SUCCESS;
33671369Sdduvall 
33681369Sdduvall 	BGE_TRACE(("bge_chip_reset($%p, %d)",
33691369Sdduvall 		(void *)bgep, enable_dma));
33701369Sdduvall 
33711369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
33721369Sdduvall 
33731369Sdduvall 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
33741369Sdduvall 		(void *)bgep, enable_dma, bgep->bge_chip_state));
33751369Sdduvall 
33761369Sdduvall 	/*
33771369Sdduvall 	 * Do we need to stop the chip cleanly before resetting?
33781369Sdduvall 	 */
33791369Sdduvall 	switch (bgep->bge_chip_state) {
33801369Sdduvall 	default:
33811369Sdduvall 		_NOTE(NOTREACHED)
33821865Sdilpreet 		return (DDI_FAILURE);
33831369Sdduvall 
33841369Sdduvall 	case BGE_CHIP_INITIAL:
33851369Sdduvall 	case BGE_CHIP_STOPPED:
33861369Sdduvall 	case BGE_CHIP_RESET:
33871369Sdduvall 		break;
33881369Sdduvall 
33891369Sdduvall 	case BGE_CHIP_RUNNING:
33901369Sdduvall 	case BGE_CHIP_ERROR:
33911369Sdduvall 	case BGE_CHIP_FAULT:
33921369Sdduvall 		bge_chip_stop(bgep, B_FALSE);
33931369Sdduvall 		break;
33941369Sdduvall 	}
33951369Sdduvall 
33961408Srandyf #ifdef BGE_IPMI_ASF
33971408Srandyf 	if (bgep->asf_enabled) {
33983918Sml149210 #ifdef __sparc
33993918Sml149210 		mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
34003918Sml149210 			MHCR_ENABLE_TAGGED_STATUS_MODE |
34013918Sml149210 			MHCR_MASK_INTERRUPT_MODE |
34023918Sml149210 			MHCR_MASK_PCI_INT_OUTPUT |
34033918Sml149210 			MHCR_CLEAR_INTERRUPT_INTA |
34043918Sml149210 			MHCR_ENABLE_ENDIAN_WORD_SWAP |
34053918Sml149210 			MHCR_ENABLE_ENDIAN_BYTE_SWAP;
340611968SYong.Tan@Sun.COM 		if (DEVICE_5717_SERIES_CHIPSETS(bgep))
340711968SYong.Tan@Sun.COM 			pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR,
340811968SYong.Tan@Sun.COM 					0);
34093918Sml149210 		pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
34103918Sml149210 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
34113918Sml149210 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG) |
34123918Sml149210 			MEMORY_ARBITER_ENABLE);
34133918Sml149210 #endif
34141408Srandyf 		if (asf_mode == ASF_MODE_INIT) {
34151408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
34161408Srandyf 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
34171408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
34181408Srandyf 		}
34191408Srandyf 	}
34201408Srandyf #endif
34211369Sdduvall 	/*
34221369Sdduvall 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
34231369Sdduvall 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
34241369Sdduvall 	 *
34251369Sdduvall 	 * Before reset Core clock,it is
34261369Sdduvall 	 * also required to initialize the Memory Arbiter as specified in step9
34271369Sdduvall 	 * and Misc Host Control Register as specified in step-13
34281369Sdduvall 	 * Step 4-5: reset Core clock & wait for completion
34291369Sdduvall 	 * Steps 6-8: are done by bge_chip_cfg_init()
34301908Sly149593 	 * put the T3_MAGIC_NUMBER into the GENCOMM port before reset
34311369Sdduvall 	 */
34321865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
34331865Sdilpreet 		retval = DDI_FAILURE;
34341369Sdduvall 
34351369Sdduvall 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
34361369Sdduvall 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
34371369Sdduvall 	    MHCR_MASK_INTERRUPT_MODE |
34381369Sdduvall 	    MHCR_MASK_PCI_INT_OUTPUT |
34391369Sdduvall 	    MHCR_CLEAR_INTERRUPT_INTA;
34401369Sdduvall #ifdef  _BIG_ENDIAN
34411369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
34421369Sdduvall #endif  /* _BIG_ENDIAN */
344311968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
344411968SYong.Tan@Sun.COM 		pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, 0);
34451369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
34461408Srandyf #ifdef BGE_IPMI_ASF
34471408Srandyf 	if (bgep->asf_enabled)
34481408Srandyf 		bgep->asf_wordswapped = B_FALSE;
34491408Srandyf #endif
34502675Szh199473 	/*
34512675Szh199473 	 * NVRAM Corruption Workaround
34522675Szh199473 	 */
34533390Szh199473 	for (tries = 0; tries < MAX_TRY_NVMEM_ACQUIRE; tries++)
34543534Szh199473 		if (bge_nvmem_acquire(bgep) != EAGAIN)
34552675Szh199473 			break;
34563440Szh199473 	if (tries >= MAX_TRY_NVMEM_ACQUIRE)
34572675Szh199473 		BGE_DEBUG(("%s: fail to acquire nvram lock",
34582675Szh199473 			bgep->ifname));
34592675Szh199473 
34601908Sly149593 #ifdef BGE_IPMI_ASF
34611908Sly149593 	if (!bgep->asf_enabled) {
34621908Sly149593 #endif
34631908Sly149593 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
34641908Sly149593 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
34651908Sly149593 #ifdef BGE_IPMI_ASF
34661908Sly149593 	}
34671908Sly149593 #endif
34681908Sly149593 
34691865Sdilpreet 	if (!bge_chip_reset_engine(bgep, MISC_CONFIG_REG))
34701865Sdilpreet 		retval = DDI_FAILURE;
34711369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
34721369Sdduvall 
34731369Sdduvall 	/*
34741369Sdduvall 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
34751369Sdduvall 	 * a bit set to avoid a fifo overflow/underflow bug.
34761369Sdduvall 	 */
34772135Szh199473 	if ((bgep->chipid.chip_label == 5721) ||
34782135Szh199473 		(bgep->chipid.chip_label == 5751) ||
34792675Szh199473 		(bgep->chipid.chip_label == 5752) ||
34804330Sml149210 		(bgep->chipid.chip_label == 5755) ||
34818207SGordon.Ross@Sun.COM 		(bgep->chipid.chip_label == 5756) ||
34827678SYong.Tan@Sun.COM 		(bgep->chipid.chip_label == 5789) ||
34837678SYong.Tan@Sun.COM 		(bgep->chipid.chip_label == 5906))
34841369Sdduvall 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
34851369Sdduvall 
34861369Sdduvall 
34871369Sdduvall 	/*
34881369Sdduvall 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
34891369Sdduvall 	 * not be changed.
34901369Sdduvall 	 */
34911865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
34921865Sdilpreet 		retval = DDI_FAILURE;
34931369Sdduvall 
34941369Sdduvall 	/*
34951369Sdduvall 	 * Steps 10-11: configure PIO endianness options and
34961369Sdduvall 	 * enable indirect register access -- already done
34971369Sdduvall 	 * Steps 12-13: enable writing to the PCI state & clock
34981369Sdduvall 	 * control registers -- not required; we aren't going to
34991369Sdduvall 	 * use those features.
35001369Sdduvall 	 * Steps 14-15: Configure DMA endianness options.  See
35011369Sdduvall 	 * the comments on the setting of the MHCR above.
35021369Sdduvall 	 */
35031369Sdduvall #ifdef	_BIG_ENDIAN
35041369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
35051369Sdduvall 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
35061369Sdduvall #else
35071369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
35081369Sdduvall #endif	/* _BIG_ENDIAN */
35091408Srandyf #ifdef BGE_IPMI_ASF
35101408Srandyf 	if (bgep->asf_enabled)
35111408Srandyf 		modeflags |= MODE_HOST_STACK_UP;
35121408Srandyf #endif
35131369Sdduvall 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
35141369Sdduvall 
35151408Srandyf #ifdef BGE_IPMI_ASF
35161408Srandyf 	if (bgep->asf_enabled) {
35173918Sml149210 #ifdef __sparc
35183918Sml149210 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
35193918Sml149210 			MEMORY_ARBITER_ENABLE |
35203918Sml149210 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG));
35213918Sml149210 #endif
35223918Sml149210 
35233918Sml149210 #ifdef  BGE_NETCONSOLE
35243918Sml149210 		if (!bgep->asf_newhandshake) {
35253918Sml149210 			if ((asf_mode == ASF_MODE_INIT) ||
35263918Sml149210 			(asf_mode == ASF_MODE_POST_INIT)) {
35273918Sml149210 				bge_asf_post_reset_old_mode(bgep,
35283918Sml149210 					BGE_INIT_RESET);
35293918Sml149210 			} else {
35303918Sml149210 				bge_asf_post_reset_old_mode(bgep,
35313918Sml149210 					BGE_SHUTDOWN_RESET);
35321408Srandyf 			}
35331408Srandyf 		}
35343918Sml149210 #endif
35353918Sml149210 
35363918Sml149210 		/* Wait for NVRAM init */
35373918Sml149210 		i = 0;
35383918Sml149210 		drv_usecwait(5000);
35393918Sml149210 		mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
35403918Sml149210 
35413918Sml149210 		while ((mailbox != (uint32_t)
35423918Sml149210 			~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
35433918Sml149210 			(i < 10000)) {
35443918Sml149210 			drv_usecwait(100);
35453918Sml149210 			mailbox = bge_nic_get32(bgep,
35463918Sml149210 				BGE_FIRMWARE_MAILBOX);
35473918Sml149210 			i++;
35483918Sml149210 		}
35493918Sml149210 
35503918Sml149210 #ifndef BGE_NETCONSOLE
35513918Sml149210 		if (!bgep->asf_newhandshake) {
35523918Sml149210 			if ((asf_mode == ASF_MODE_INIT) ||
35533918Sml149210 				(asf_mode == ASF_MODE_POST_INIT)) {
35543918Sml149210 
35553918Sml149210 				bge_asf_post_reset_old_mode(bgep,
35563918Sml149210 					BGE_INIT_RESET);
35573918Sml149210 			} else {
35583918Sml149210 				bge_asf_post_reset_old_mode(bgep,
35593918Sml149210 					BGE_SHUTDOWN_RESET);
35603918Sml149210 			}
35613918Sml149210 		}
35623918Sml149210 #endif
35631408Srandyf 	}
35641408Srandyf #endif
35651369Sdduvall 	/*
35661369Sdduvall 	 * Steps 16-17: poll for firmware completion
35671369Sdduvall 	 */
35681369Sdduvall 	mac = bge_poll_firmware(bgep);
35691369Sdduvall 
35701369Sdduvall 	/*
35711369Sdduvall 	 * Step 18: enable external memory -- doesn't apply.
35721369Sdduvall 	 *
35731369Sdduvall 	 * However we take the opportunity to set the MLCR anyway, as
35741369Sdduvall 	 * this register also controls the SEEPROM auto-access method
35751369Sdduvall 	 * which we may want to use later ...
35761369Sdduvall 	 *
35771369Sdduvall 	 * The proper value here depends on the way the chip is wired
35781369Sdduvall 	 * into the circuit board, as this register *also* controls which
35791369Sdduvall 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
35801369Sdduvall 	 * values driven onto those pins!
35811369Sdduvall 	 *
35821369Sdduvall 	 * See also step 74 in the PRM ...
35831369Sdduvall 	 */
35841369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
35851369Sdduvall 	    bgep->chipid.bge_mlcr_default);
35861369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
35871369Sdduvall 
35881369Sdduvall 	/*
35891369Sdduvall 	 * Step 20: clear the Ethernet MAC mode register
35901369Sdduvall 	 */
35911369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
35921369Sdduvall 
35931369Sdduvall 	/*
35941369Sdduvall 	 * Step 21: restore cache-line-size, latency timer, and
35951369Sdduvall 	 * subsystem ID registers to their original values (not
35961369Sdduvall 	 * those read into the local structure <chipid>, 'cos
35971369Sdduvall 	 * that was after they were cleared by the RESET).
35981369Sdduvall 	 *
35991369Sdduvall 	 * Note: the Subsystem Vendor/Device ID registers are not
36001369Sdduvall 	 * directly writable in config space, so we use the shadow
36011369Sdduvall 	 * copy in "Page Zero" of register space to restore them
36021369Sdduvall 	 * both in one go ...
36031369Sdduvall 	 */
36041369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
36051369Sdduvall 		bgep->chipid.clsize);
36061369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
36071369Sdduvall 		bgep->chipid.latency);
36081369Sdduvall 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
36091369Sdduvall 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
36101369Sdduvall 
36111369Sdduvall 	/*
36121369Sdduvall 	 * The SEND INDEX registers should be reset to zero by the
36131369Sdduvall 	 * global chip reset; if they're not, there'll be trouble
36141865Sdilpreet 	 * later on.
36151369Sdduvall 	 */
36161369Sdduvall 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
36171865Sdilpreet 	if (sx0 != 0) {
36181865Sdilpreet 		BGE_REPORT((bgep, "SEND INDEX - device didn't RESET"));
36191865Sdilpreet 		bge_fm_ereport(bgep, DDI_FM_DEVICE_INVAL_STATE);
36203170Sml149210 		retval = DDI_FAILURE;
36211865Sdilpreet 	}
36221369Sdduvall 
36231369Sdduvall 	/* Enable MSI code */
36241369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
36251369Sdduvall 		bge_reg_set32(bgep, MSI_MODE_REG,
36263907Szh199473 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE|MSI_ERROR_ATTENTION);
36271369Sdduvall 
36281369Sdduvall 	/*
36291369Sdduvall 	 * On the first time through, save the factory-set MAC address
36301369Sdduvall 	 * (if any).  If bge_poll_firmware() above didn't return one
36311369Sdduvall 	 * (from a chip register) consider looking in the attached NV
36321369Sdduvall 	 * memory device, if any.  Once we have it, we save it in both
36331369Sdduvall 	 * register-image (64-bit) and byte-array forms.  All-zero and
36341369Sdduvall 	 * all-one addresses are not valid, and we refuse to stash those.
36351369Sdduvall 	 */
36361369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
36371369Sdduvall 		if (mac == 0ULL)
36381369Sdduvall 			mac = bge_get_nvmac(bgep);
36391369Sdduvall 		if (mac != 0ULL && mac != ~0ULL) {
36401369Sdduvall 			bgep->chipid.hw_mac_addr = mac;
36411369Sdduvall 			for (i = ETHERADDRL; i-- != 0; ) {
36421369Sdduvall 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
36431369Sdduvall 				mac >>= 8;
36441369Sdduvall 			}
36452331Skrgopi 			bgep->chipid.vendor_addr.set = B_TRUE;
36461369Sdduvall 		}
36471369Sdduvall 	}
36481369Sdduvall 
36491408Srandyf #ifdef BGE_IPMI_ASF
36501408Srandyf 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
36511408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
36521408Srandyf 			if ((asf_mode == ASF_MODE_INIT) ||
36531408Srandyf 				(asf_mode == ASF_MODE_POST_INIT)) {
36541408Srandyf 
36551408Srandyf 				bge_asf_post_reset_new_mode(bgep,
36561408Srandyf 					BGE_INIT_RESET);
36571408Srandyf 			} else {
36581408Srandyf 				bge_asf_post_reset_new_mode(bgep,
36591408Srandyf 					BGE_SHUTDOWN_RESET);
36601408Srandyf 			}
36611408Srandyf 		}
36621408Srandyf 	}
36631408Srandyf #endif
36641408Srandyf 
36651369Sdduvall 	/*
36661369Sdduvall 	 * Record the new state
36671369Sdduvall 	 */
36681369Sdduvall 	bgep->chip_resets += 1;
36691369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RESET;
36701865Sdilpreet 	return (retval);
36711369Sdduvall }
36721369Sdduvall 
36731369Sdduvall /*
36741369Sdduvall  * bge_chip_start() -- start the chip transmitting and/or receiving,
36751369Sdduvall  * including enabling interrupts
36761369Sdduvall  */
36771865Sdilpreet int bge_chip_start(bge_t *bgep, boolean_t reset_phys);
36781369Sdduvall #pragma	no_inline(bge_chip_start)
36791369Sdduvall 
36809731SYong.Tan@Sun.COM void
bge_chip_coalesce_update(bge_t * bgep)36819731SYong.Tan@Sun.COM bge_chip_coalesce_update(bge_t *bgep)
36829731SYong.Tan@Sun.COM {
36839731SYong.Tan@Sun.COM 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG,
36849731SYong.Tan@Sun.COM 	    bgep->chipid.tx_count_norm);
36859731SYong.Tan@Sun.COM 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG,
36869731SYong.Tan@Sun.COM 	    bgep->chipid.tx_ticks_norm);
36879731SYong.Tan@Sun.COM 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG,
36889731SYong.Tan@Sun.COM 	    bgep->chipid.rx_count_norm);
36899731SYong.Tan@Sun.COM 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG,
36909731SYong.Tan@Sun.COM 	    bgep->chipid.rx_ticks_norm);
36919731SYong.Tan@Sun.COM }
36929731SYong.Tan@Sun.COM 
36931865Sdilpreet int
bge_chip_start(bge_t * bgep,boolean_t reset_phys)36941369Sdduvall bge_chip_start(bge_t *bgep, boolean_t reset_phys)
36951369Sdduvall {
36961369Sdduvall 	uint32_t coalmode;
36971369Sdduvall 	uint32_t ledctl;
36981369Sdduvall 	uint32_t mtu;
36991369Sdduvall 	uint32_t maxring;
37003534Szh199473 	uint32_t stats_mask;
37014330Sml149210 	uint32_t dma_wrprio;
37021369Sdduvall 	uint64_t ring;
370311968SYong.Tan@Sun.COM 	uint32_t regval;
37041865Sdilpreet 	int retval = DDI_SUCCESS;
37051369Sdduvall 
37061369Sdduvall 	BGE_TRACE(("bge_chip_start($%p)",
37074588Sml149210 	    (void *)bgep));
37081369Sdduvall 
37091369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
37101369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
37111369Sdduvall 
37121369Sdduvall 	/*
37131369Sdduvall 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
37141369Sdduvall 	 * The document specifies 95 separate steps to fully
37151369Sdduvall 	 * initialise the chip!!!!
37161369Sdduvall 	 *
37171369Sdduvall 	 * The reset code above has already got us as far as step
37181369Sdduvall 	 * 21, so we continue with ...
37191369Sdduvall 	 *
37201369Sdduvall 	 * Step 22: clear the MAC statistics block
37211369Sdduvall 	 * (0x0300-0x0aff in NIC-local memory)
37221369Sdduvall 	 */
37231369Sdduvall 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
37241369Sdduvall 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
37251369Sdduvall 		    NIC_MEM_STATISTICS_SIZE);
37261369Sdduvall 
37271369Sdduvall 	/*
37281369Sdduvall 	 * Step 23: clear the status block (in host memory)
37291369Sdduvall 	 */
37301369Sdduvall 	DMA_ZERO(bgep->status_block);
37311369Sdduvall 
37321369Sdduvall 	/*
37331369Sdduvall 	 * Step 24: set DMA read/write control register
37341369Sdduvall 	 */
37351369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
37364588Sml149210 	    bgep->chipid.bge_dma_rwctrl);
37371369Sdduvall 
37381369Sdduvall 	/*
37391369Sdduvall 	 * Step 25: Configure DMA endianness -- already done (16/17)
37401369Sdduvall 	 * Step 26: Configure Host-Based Send Rings
37411369Sdduvall 	 * Step 27: Indicate Host Stack Up
37421369Sdduvall 	 */
37431369Sdduvall 	bge_reg_set32(bgep, MODE_CONTROL_REG,
37444588Sml149210 	    MODE_HOST_SEND_BDS |
37454588Sml149210 	    MODE_HOST_STACK_UP);
37461369Sdduvall 
37471369Sdduvall 	/*
37481369Sdduvall 	 * Step 28: Configure checksum options:
37491611Szh199473 	 *	Solaris supports the hardware default checksum options.
37501611Szh199473 	 *
37511611Szh199473 	 *	Workaround for Incorrect pseudo-header checksum calculation.
37521369Sdduvall 	 */
37532135Szh199473 	if (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)
37541611Szh199473 		bge_reg_set32(bgep, MODE_CONTROL_REG,
37552311Sseb 		    MODE_SEND_NO_PSEUDO_HDR_CSUM);
37561369Sdduvall 
37571369Sdduvall 	/*
37581369Sdduvall 	 * Step 29: configure Timer Prescaler.  The value is always the
37591369Sdduvall 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
37601369Sdduvall 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
37611369Sdduvall 	 * for the whole chip!
37621369Sdduvall 	 */
376311968SYong.Tan@Sun.COM 	regval = bge_reg_get32(bgep, MISC_CONFIG_REG);
376411968SYong.Tan@Sun.COM 	regval = (regval & 0xffffff00) | MISC_CONFIG_DEFAULT;
376511968SYong.Tan@Sun.COM 	bge_reg_put32(bgep, MISC_CONFIG_REG, regval);
37661369Sdduvall 
37677678SYong.Tan@Sun.COM 	if (DEVICE_5906_SERIES_CHIPSETS(bgep)) {
37687678SYong.Tan@Sun.COM 		drv_usecwait(40);
37697678SYong.Tan@Sun.COM 		/* put PHY into ready state */
37707678SYong.Tan@Sun.COM 		bge_reg_clr32(bgep, MISC_CONFIG_REG, MISC_CONFIG_EPHY_IDDQ);
37717678SYong.Tan@Sun.COM 		(void) bge_reg_get32(bgep, MISC_CONFIG_REG); /* flush */
37727678SYong.Tan@Sun.COM 		drv_usecwait(40);
37737678SYong.Tan@Sun.COM 	}
37747678SYong.Tan@Sun.COM 
37751369Sdduvall 	/*
37761369Sdduvall 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
37771369Sdduvall 	 *
37781369Sdduvall 	 * If the mbuf_length is specified as 0, we just leave these at
37791369Sdduvall 	 * their hardware defaults, rather than explicitly setting them.
37801369Sdduvall 	 * As the Broadcom HRM,driver better not change the parameters
37811369Sdduvall 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
37821369Sdduvall 	 */
37831369Sdduvall 	if ((bgep->chipid.mbuf_length != 0) &&
37844588Sml149210 	    (DEVICE_5704_SERIES_CHIPSETS(bgep))) {
37851369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
37864588Sml149210 			    bgep->chipid.mbuf_base);
37871369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
37884588Sml149210 			    bgep->chipid.mbuf_length);
37891369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
37904588Sml149210 			    DMAD_POOL_BASE_DEFAULT);
37911369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
37924588Sml149210 			    DMAD_POOL_LENGTH_DEFAULT);
37931369Sdduvall 	}
37941369Sdduvall 
37951369Sdduvall 	/*
37961369Sdduvall 	 * Step 32: configure MAC memory pool watermarks
37971369Sdduvall 	 */
37981369Sdduvall 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
37994588Sml149210 	    bgep->chipid.mbuf_lo_water_rdma);
38001369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
38014588Sml149210 	    bgep->chipid.mbuf_lo_water_rmac);
38021369Sdduvall 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
38034588Sml149210 	    bgep->chipid.mbuf_hi_water);
38041369Sdduvall 
38051369Sdduvall 	/*
38061369Sdduvall 	 * Step 33: configure DMA resource watermarks
38071369Sdduvall 	 */
38081369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
38091369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
38101369Sdduvall 		    bge_dmad_lo_water);
38111369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
38121369Sdduvall 		    bge_dmad_hi_water);
38131369Sdduvall 	}
38141369Sdduvall 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
38151369Sdduvall 
38161369Sdduvall 	/*
38171369Sdduvall 	 * Steps 34-36: enable buffer manager & internal h/w queues
38181369Sdduvall 	 */
38191865Sdilpreet 	if (!bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
38201865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
38211865Sdilpreet 		retval = DDI_FAILURE;
38221865Sdilpreet 	if (!bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0))
38231865Sdilpreet 		retval = DDI_FAILURE;
38241369Sdduvall 
38251369Sdduvall 	/*
38261369Sdduvall 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
38271369Sdduvall 	 */
382811968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep)) {
382911968SYong.Tan@Sun.COM 		buff_ring_t *brp = &bgep->buff[BGE_STD_BUFF_RING];
383011968SYong.Tan@Sun.COM 		bge_reg_put64(bgep, STD_RCV_BD_RING_RCB_REG,
383111968SYong.Tan@Sun.COM 		    brp->desc.cookie.dmac_laddress);
383211968SYong.Tan@Sun.COM 		bge_reg_put32(bgep, STD_RCV_BD_RING_RCB_REG + 8,
383311968SYong.Tan@Sun.COM 		    (brp->desc.nslots) << 16 | brp->buf[0].size << 2);
383411968SYong.Tan@Sun.COM 		bge_reg_put32(bgep, STD_RCV_BD_RING_RCB_REG + 0xc,
383511968SYong.Tan@Sun.COM 		    NIC_MEM_SHADOW_BUFF_STD_5717);
383611968SYong.Tan@Sun.COM 	} else
383711968SYong.Tan@Sun.COM 		bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
383811968SYong.Tan@Sun.COM 		    &bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
383911968SYong.Tan@Sun.COM 
38401369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
38411369Sdduvall 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
38424588Sml149210 		    &bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
38431369Sdduvall 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
38444588Sml149210 		    &bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
38451369Sdduvall 	}
38461369Sdduvall 
38471369Sdduvall 	/*
38481369Sdduvall 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
38491369Sdduvall 	 */
38501369Sdduvall 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
38511369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
38521369Sdduvall 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
38531369Sdduvall 		    bge_replenish_jumbo);
38541369Sdduvall 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
38551369Sdduvall 		    bge_replenish_mini);
38561369Sdduvall 	}
38571369Sdduvall 
38581369Sdduvall 	/*
38591369Sdduvall 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
38601369Sdduvall 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
38611369Sdduvall 	 */
38621369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
38631369Sdduvall 		maxring = BGE_SEND_RINGS_MAX;
38641369Sdduvall 	else
38651369Sdduvall 		maxring = BGE_SEND_RINGS_MAX_5705;
38661369Sdduvall 	for (ring = 0; ring < maxring; ++ring) {
38671369Sdduvall 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
38681369Sdduvall 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
38691369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
38704588Sml149210 		    &bgep->send[ring].hw_rcb);
38711369Sdduvall 	}
38721369Sdduvall 
38731369Sdduvall 	/*
38741369Sdduvall 	 * Steps 44-45: initialise Receive Return Rings
38751369Sdduvall 	 * (0x0200-0x02ff in NIC-local memory)
38761369Sdduvall 	 */
38771369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
38781369Sdduvall 		maxring = BGE_RECV_RINGS_MAX;
38791369Sdduvall 	else
38801369Sdduvall 		maxring = BGE_RECV_RINGS_MAX_5705;
38811369Sdduvall 	for (ring = 0; ring < maxring; ++ring)
38821369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
38834588Sml149210 		    &bgep->recv[ring].hw_rcb);
38841369Sdduvall 
38851369Sdduvall 	/*
38861369Sdduvall 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
38871369Sdduvall 	 */
38881369Sdduvall 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
38891369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
38901369Sdduvall 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
38911369Sdduvall 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
38921369Sdduvall 	}
38931369Sdduvall 	/*
38941369Sdduvall 	 * Step 47: configure the MAC unicast address
38951369Sdduvall 	 * Step 48: configure the random backoff seed
38961369Sdduvall 	 * Step 96: set up multicast filters
38971369Sdduvall 	 */
38981408Srandyf #ifdef BGE_IPMI_ASF
38991865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE)
39001408Srandyf #else
39011865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE)
39021408Srandyf #endif
39031865Sdilpreet 		retval = DDI_FAILURE;
39041369Sdduvall 
39051369Sdduvall 	/*
39061369Sdduvall 	 * Step 49: configure the MTU
39071369Sdduvall 	 */
39081369Sdduvall 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
39091369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
39101369Sdduvall 
39111369Sdduvall 	/*
39121369Sdduvall 	 * Step 50: configure the IPG et al
39131369Sdduvall 	 */
39141369Sdduvall 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
39151369Sdduvall 
39161369Sdduvall 	/*
39171369Sdduvall 	 * Step 51: configure the default Rx Return Ring
39181369Sdduvall 	 */
39191369Sdduvall 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
39201369Sdduvall 
39211369Sdduvall 	/*
39221369Sdduvall 	 * Steps 52-54: configure Receive List Placement,
39231369Sdduvall 	 * and enable Receive List Placement Statistics
39241369Sdduvall 	 */
39251369Sdduvall 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
39264588Sml149210 	    RCV_LP_CONFIG(bgep->chipid.rx_rings));
39273534Szh199473 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
39283534Szh199473 	case MHCR_CHIP_ASIC_REV_5700:
39293534Szh199473 	case MHCR_CHIP_ASIC_REV_5701:
39303534Szh199473 	case MHCR_CHIP_ASIC_REV_5703:
39313534Szh199473 	case MHCR_CHIP_ASIC_REV_5704:
39323534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
39333534Szh199473 		break;
39343534Szh199473 	case MHCR_CHIP_ASIC_REV_5705:
39353534Szh199473 		break;
39363534Szh199473 	default:
39373534Szh199473 		stats_mask = bge_reg_get32(bgep, RCV_LP_STATS_ENABLE_MASK_REG);
39383534Szh199473 		stats_mask &= ~RCV_LP_STATS_DISABLE_MACTQ;
39393534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, stats_mask);
39403534Szh199473 		break;
39413534Szh199473 	}
39421369Sdduvall 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
39431369Sdduvall 
39441369Sdduvall 	if (bgep->chipid.rx_rings > 1)
39451369Sdduvall 		bge_init_recv_rule(bgep);
39461369Sdduvall 
39471369Sdduvall 	/*
39481369Sdduvall 	 * Steps 55-56: enable Send Data Initiator Statistics
39491369Sdduvall 	 */
39501369Sdduvall 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
39511369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
39521369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
39531369Sdduvall 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
39541369Sdduvall 	} else {
39551369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
39561369Sdduvall 		    SEND_INIT_STATS_ENABLE);
39571369Sdduvall 	}
39581369Sdduvall 	/*
39591369Sdduvall 	 * Steps 57-58: stop (?) the Host Coalescing Engine
39601369Sdduvall 	 */
39611865Sdilpreet 	if (!bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0))
39621865Sdilpreet 		retval = DDI_FAILURE;
39631369Sdduvall 
39641369Sdduvall 	/*
39651369Sdduvall 	 * Steps 59-62: initialise Host Coalescing parameters
39661369Sdduvall 	 */
39679731SYong.Tan@Sun.COM 	bge_chip_coalesce_update(bgep);
39681369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
39691369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
39701369Sdduvall 		    bge_tx_count_intr);
39711369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
39721369Sdduvall 		    bge_tx_ticks_intr);
39731369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
39741369Sdduvall 		    bge_rx_count_intr);
39751369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
39761369Sdduvall 		    bge_rx_ticks_intr);
39771369Sdduvall 	}
39781369Sdduvall 
39791369Sdduvall 	/*
39801369Sdduvall 	 * Steps 63-64: initialise status block & statistics
39811369Sdduvall 	 * host memory addresses
39821369Sdduvall 	 * The statistic block does not exist in some chipsets
39831369Sdduvall 	 * Step 65: initialise Statistics Coalescing Tick Counter
39841369Sdduvall 	 */
39851369Sdduvall 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
39864588Sml149210 	    bgep->status_block.cookie.dmac_laddress);
39871369Sdduvall 
39881369Sdduvall 	/*
39891369Sdduvall 	 * Steps 66-67: initialise status block & statistics
39901369Sdduvall 	 * NIC-local memory addresses
39911369Sdduvall 	 */
39921369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
39931369Sdduvall 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
39941369Sdduvall 		    bgep->statistics.cookie.dmac_laddress);
39951369Sdduvall 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
39961369Sdduvall 		    STATISTICS_TICKS_DEFAULT);
39971369Sdduvall 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
39981369Sdduvall 		    NIC_MEM_STATUS_BLOCK);
39991369Sdduvall 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
40001369Sdduvall 		    NIC_MEM_STATISTICS);
40011369Sdduvall 	}
40021369Sdduvall 
40031369Sdduvall 	/*
40041369Sdduvall 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
40051369Sdduvall 	 * Completion Engine, the Receive List Placement Engine, and the
40061369Sdduvall 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
40071369Sdduvall 	 * and BCM5715.
40081369Sdduvall 	 */
40091369Sdduvall 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
40101369Sdduvall 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
40111369Sdduvall 		coalmode = COALESCE_64_BYTE_STATUS;
40121369Sdduvall 	else
40131369Sdduvall 		coalmode = 0;
401411968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
401511968SYong.Tan@Sun.COM 		coalmode = COALESCE_CLR_TICKS_RX;
40161865Sdilpreet 	if (!bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode))
40171865Sdilpreet 		retval = DDI_FAILURE;
40181865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
40191865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
40201865Sdilpreet 		retval = DDI_FAILURE;
40211865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0))
40221865Sdilpreet 		retval = DDI_FAILURE;
40231369Sdduvall 
40241369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40251865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
40261865Sdilpreet 		    STATE_MACHINE_ATTN_ENABLE_BIT))
40271865Sdilpreet 			retval = DDI_FAILURE;
40281369Sdduvall 
40291369Sdduvall 	/*
40301369Sdduvall 	 * Step 72: Enable MAC DMA engines
40311369Sdduvall 	 * Step 73: Clear & enable MAC statistics
40321369Sdduvall 	 */
40331369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
40344588Sml149210 	    ETHERNET_MODE_ENABLE_FHDE |
40354588Sml149210 	    ETHERNET_MODE_ENABLE_RDE |
40364588Sml149210 	    ETHERNET_MODE_ENABLE_TDE);
40371369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
40384588Sml149210 	    ETHERNET_MODE_ENABLE_TX_STATS |
40394588Sml149210 	    ETHERNET_MODE_ENABLE_RX_STATS |
40404588Sml149210 	    ETHERNET_MODE_CLEAR_TX_STATS |
40414588Sml149210 	    ETHERNET_MODE_CLEAR_RX_STATS);
40421369Sdduvall 
40431369Sdduvall 	/*
40441369Sdduvall 	 * Step 74: configure the MLCR (Miscellaneous Local Control
40451369Sdduvall 	 * Register); not required, as we set up the MLCR in step 10
40461369Sdduvall 	 * (part of the reset code) above.
40471369Sdduvall 	 *
40481369Sdduvall 	 * Step 75: clear Interrupt Mailbox 0
40491369Sdduvall 	 */
40501369Sdduvall 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
40511369Sdduvall 
40521369Sdduvall 	/*
40531369Sdduvall 	 * Steps 76-87: Gentlemen, start your engines ...
40541369Sdduvall 	 *
40551369Sdduvall 	 * Enable the DMA Completion Engine, the Write DMA Engine,
40561369Sdduvall 	 * the Read DMA Engine, Receive Data Completion Engine,
40571369Sdduvall 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
40581369Sdduvall 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
40591369Sdduvall 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
40601369Sdduvall 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
40611369Sdduvall 	 *
40621369Sdduvall 	 * Beware exhaust fumes?
40631369Sdduvall 	 */
40641369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40651865Sdilpreet 		if (!bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0))
40661865Sdilpreet 			retval = DDI_FAILURE;
40674330Sml149210 	dma_wrprio = (bge_dma_wrprio << DMA_PRIORITY_SHIFT) |
40684588Sml149210 	    ALL_DMA_ATTN_BITS;
40697678SYong.Tan@Sun.COM 	if ((MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
40707678SYong.Tan@Sun.COM 	    MHCR_CHIP_ASIC_REV_5755) ||
40717678SYong.Tan@Sun.COM 	    (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
407211968SYong.Tan@Sun.COM 	    MHCR_CHIP_ASIC_REV_5723) ||
407311968SYong.Tan@Sun.COM 	    (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
40747678SYong.Tan@Sun.COM 	    MHCR_CHIP_ASIC_REV_5906)) {
40754330Sml149210 		dma_wrprio |= DMA_STATUS_TAG_FIX_CQ12384;
40764330Sml149210 	}
40771865Sdilpreet 	if (!bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
40784588Sml149210 	    dma_wrprio))
40791865Sdilpreet 		retval = DDI_FAILURE;
408011968SYong.Tan@Sun.COM 	if (DEVICE_5723_SERIES_CHIPSETS(bgep) ||
408111968SYong.Tan@Sun.COM 	    DEVICE_5717_SERIES_CHIPSETS(bgep))
408211479SYong.Tan@Sun.COM 		bge_dma_rdprio = 0;
40831865Sdilpreet 	if (!bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
40841865Sdilpreet 	    (bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
40851865Sdilpreet 		retval = DDI_FAILURE;
40861865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
40871865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
40881865Sdilpreet 		retval = DDI_FAILURE;
40891369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40901865Sdilpreet 		if (!bge_chip_enable_engine(bgep,
40911865Sdilpreet 		    MBUF_CLUSTER_FREE_MODE_REG, 0))
40921865Sdilpreet 			retval = DDI_FAILURE;
40931865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0))
40941865Sdilpreet 		retval = DDI_FAILURE;
40951865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
40961865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
40971865Sdilpreet 		retval = DDI_FAILURE;
40981865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
40991865Sdilpreet 	    RCV_BD_DISABLED_RING_ATTN))
41001865Sdilpreet 		retval = DDI_FAILURE;
41011865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
41021865Sdilpreet 	    RCV_DATA_BD_ILL_RING_ATTN))
41031865Sdilpreet 		retval = DDI_FAILURE;
41041865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0))
41051865Sdilpreet 		retval = DDI_FAILURE;
41061865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
41071865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
41081865Sdilpreet 		retval = DDI_FAILURE;
41091865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
41101865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
41111865Sdilpreet 		retval = DDI_FAILURE;
41121369Sdduvall 
41131369Sdduvall 	/*
41141369Sdduvall 	 * Step 88: download firmware -- doesn't apply
41151369Sdduvall 	 * Steps 89-90: enable Transmit & Receive MAC Engines
41161369Sdduvall 	 */
41171865Sdilpreet 	if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
41181865Sdilpreet 		retval = DDI_FAILURE;
41191408Srandyf #ifdef BGE_IPMI_ASF
41201865Sdilpreet 	if (!bgep->asf_enabled) {
41211865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
41221865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
41231865Sdilpreet 			retval = DDI_FAILURE;
41241408Srandyf 	} else {
41251865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0))
41261865Sdilpreet 			retval = DDI_FAILURE;
41271408Srandyf 	}
41281408Srandyf #else
41291865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
41301865Sdilpreet 	    RECEIVE_MODE_KEEP_VLAN_TAG))
41311865Sdilpreet 		retval = DDI_FAILURE;
41321408Srandyf #endif
41331369Sdduvall 
41341369Sdduvall 	/*
41351369Sdduvall 	 * Step 91: disable auto-polling of PHY status
41361369Sdduvall 	 */
41371369Sdduvall 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
41381369Sdduvall 
41391369Sdduvall 	/*
41401369Sdduvall 	 * Step 92: configure D0 power state (not required)
41411369Sdduvall 	 * Step 93: initialise LED control register ()
41421369Sdduvall 	 */
41431369Sdduvall 	ledctl = LED_CONTROL_DEFAULT;
41441369Sdduvall 	switch (bgep->chipid.device) {
41451369Sdduvall 	case DEVICE_ID_5700:
41461369Sdduvall 	case DEVICE_ID_5700x:
41471369Sdduvall 	case DEVICE_ID_5701:
41481369Sdduvall 		/*
41491369Sdduvall 		 * Switch to 5700 (MAC) mode on these older chips
41501369Sdduvall 		 */
41511369Sdduvall 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
41521369Sdduvall 		ledctl |= LED_CONTROL_LED_MODE_5700;
41531369Sdduvall 		break;
41541369Sdduvall 
41551369Sdduvall 	default:
41561369Sdduvall 		break;
41571369Sdduvall 	}
41581369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
41591369Sdduvall 
41601369Sdduvall 	/*
41611369Sdduvall 	 * Step 94: activate link
41621369Sdduvall 	 */
41631369Sdduvall 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
41641369Sdduvall 
41651369Sdduvall 	/*
41661369Sdduvall 	 * Step 95: set up physical layer (PHY/SerDes)
41671369Sdduvall 	 * restart autoneg (if required)
41681369Sdduvall 	 */
41691369Sdduvall 	if (reset_phys)
41701865Sdilpreet 		if (bge_phys_update(bgep) == DDI_FAILURE)
41711865Sdilpreet 			retval = DDI_FAILURE;
41721369Sdduvall 
41731369Sdduvall 	/*
41741369Sdduvall 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
41751369Sdduvall 	 */
41761369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
41771369Sdduvall 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
41784588Sml149210 		    bgep->buff[ring].rf_next);
41791369Sdduvall 
41801369Sdduvall 	/*
41811369Sdduvall 	 * MSI bits:The least significant MSI 16-bit word.
41821369Sdduvall 	 * ISR will be triggered different.
41831369Sdduvall 	 */
41841369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
41851369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
41861369Sdduvall 
41871369Sdduvall 	/*
41881369Sdduvall 	 * Extra step (DSG): select which interrupts are enabled
41891369Sdduvall 	 *
41901369Sdduvall 	 * Program the Ethernet MAC engine to signal attention on
41911369Sdduvall 	 * Link Change events, then enable interrupts on MAC, DMA,
41921369Sdduvall 	 * and FLOW attention signals.
41931369Sdduvall 	 */
41941369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
41954588Sml149210 	    ETHERNET_EVENT_LINK_INT |
41964588Sml149210 	    ETHERNET_STATUS_PCS_ERROR_INT);
41971408Srandyf #ifdef BGE_IPMI_ASF
41981408Srandyf 	if (bgep->asf_enabled) {
41991408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
42004588Sml149210 		    MODE_INT_ON_FLOW_ATTN |
42014588Sml149210 		    MODE_INT_ON_DMA_ATTN |
42024588Sml149210 		    MODE_HOST_STACK_UP|
42034588Sml149210 		    MODE_INT_ON_MAC_ATTN);
42041408Srandyf 	} else {
42051408Srandyf #endif
42061408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
42074588Sml149210 		    MODE_INT_ON_FLOW_ATTN |
42084588Sml149210 		    MODE_INT_ON_DMA_ATTN |
42094588Sml149210 		    MODE_INT_ON_MAC_ATTN);
42101408Srandyf #ifdef BGE_IPMI_ASF
42111408Srandyf 	}
42121408Srandyf #endif
42131369Sdduvall 
42141369Sdduvall 	/*
42151369Sdduvall 	 * Step 97: enable PCI interrupts!!!
42161369Sdduvall 	 */
42171369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
42181369Sdduvall 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
421911968SYong.Tan@Sun.COM 		    bgep->chipid.mask_pci_int);
42201369Sdduvall 
42211369Sdduvall 	/*
42221369Sdduvall 	 * All done!
42231369Sdduvall 	 */
42241369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
42251865Sdilpreet 	return (retval);
42261369Sdduvall }
42271369Sdduvall 
42281369Sdduvall 
42291369Sdduvall /*
42301369Sdduvall  * ========== Hardware interrupt handler ==========
42311369Sdduvall  */
42321369Sdduvall 
42331369Sdduvall #undef	BGE_DBG
42341369Sdduvall #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
42351369Sdduvall 
42361369Sdduvall /*
42371369Sdduvall  * Sync the status block, then atomically clear the specified bits in
42381369Sdduvall  * the <flags-and-tag> field of the status block.
42391369Sdduvall  * the <flags> word of the status block, returning the value of the
42401369Sdduvall  * <tag> and the <flags> before the bits were cleared.
42411369Sdduvall  */
42421865Sdilpreet static int bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags);
42431369Sdduvall #pragma	inline(bge_status_sync)
42441369Sdduvall 
42451865Sdilpreet static int
bge_status_sync(bge_t * bgep,uint64_t bits,uint64_t * flags)42461865Sdilpreet bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags)
42471369Sdduvall {
42481369Sdduvall 	bge_status_t *bsp;
42491865Sdilpreet 	int retval;
42501369Sdduvall 
42511369Sdduvall 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
42524588Sml149210 	    (void *)bgep, bits));
42531369Sdduvall 
42541369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
42551369Sdduvall 
42561369Sdduvall 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
42571865Sdilpreet 	retval = bge_check_dma_handle(bgep, bgep->status_block.dma_hdl);
42581865Sdilpreet 	if (retval != DDI_FM_OK)
42591865Sdilpreet 		return (retval);
42601865Sdilpreet 
42611369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
42621865Sdilpreet 	*flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
42631369Sdduvall 
42641369Sdduvall 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
42654588Sml149210 	    (void *)bgep, bits, *flags));
42661865Sdilpreet 
42671865Sdilpreet 	return (retval);
42681369Sdduvall }
42691369Sdduvall 
42705903Ssowmini void bge_wake_factotum(bge_t *bgep);
42711369Sdduvall #pragma	inline(bge_wake_factotum)
42721369Sdduvall 
42735903Ssowmini void
bge_wake_factotum(bge_t * bgep)42741369Sdduvall bge_wake_factotum(bge_t *bgep)
42751369Sdduvall {
42761369Sdduvall 	mutex_enter(bgep->softintrlock);
42771369Sdduvall 	if (bgep->factotum_flag == 0) {
42781369Sdduvall 		bgep->factotum_flag = 1;
42791369Sdduvall 		ddi_trigger_softintr(bgep->factotum_id);
42801369Sdduvall 	}
42811369Sdduvall 	mutex_exit(bgep->softintrlock);
42821369Sdduvall }
42831369Sdduvall 
42841369Sdduvall /*
42851369Sdduvall  *	bge_intr() -- handle chip interrupts
42861369Sdduvall  */
42871369Sdduvall uint_t bge_intr(caddr_t arg1, caddr_t arg2);
42881369Sdduvall #pragma	no_inline(bge_intr)
42891369Sdduvall 
42901369Sdduvall uint_t
bge_intr(caddr_t arg1,caddr_t arg2)42911369Sdduvall bge_intr(caddr_t arg1, caddr_t arg2)
42921369Sdduvall {
42937099Syt223700 	bge_t *bgep = (void *)arg1;		/* private device info	*/
42941369Sdduvall 	bge_status_t *bsp;
42951369Sdduvall 	uint64_t flags;
42963907Szh199473 	uint32_t regval;
42971369Sdduvall 	uint_t result;
42983918Sml149210 	int retval, loop_cnt = 0;
42991369Sdduvall 
43001369Sdduvall 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
43011369Sdduvall 
43021369Sdduvall 	/*
43031369Sdduvall 	 * GLD v2 checks that s/w setup is complete before passing
43041369Sdduvall 	 * interrupts to this routine, thus eliminating the old
43051369Sdduvall 	 * (and well-known) race condition around ddi_add_intr()
43061369Sdduvall 	 */
43071369Sdduvall 	ASSERT(bgep->progress & PROGRESS_HWINT);
43081369Sdduvall 
43091369Sdduvall 	result = DDI_INTR_UNCLAIMED;
43101369Sdduvall 	mutex_enter(bgep->genlock);
43111369Sdduvall 
43123907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
43131369Sdduvall 		/*
43143907Szh199473 		 * Check whether chip's says it's asserting #INTA;
43153907Szh199473 		 * if not, don't process or claim the interrupt.
43163907Szh199473 		 *
43173907Szh199473 		 * Note that the PCI signal is active low, so the
43183907Szh199473 		 * bit is *zero* when the interrupt is asserted.
43191369Sdduvall 		 */
43203907Szh199473 		regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
432111968SYong.Tan@Sun.COM 		if (!(DEVICE_5717_SERIES_CHIPSETS(bgep)) &&
432211968SYong.Tan@Sun.COM 		    (regval & MLCR_INTA_STATE)) {
43233907Szh199473 			if (bge_check_acc_handle(bgep, bgep->io_handle)
43243907Szh199473 			    != DDI_FM_OK)
43251865Sdilpreet 				goto chip_stop;
43263907Szh199473 			mutex_exit(bgep->genlock);
43273907Szh199473 			return (result);
43281865Sdilpreet 		}
43291369Sdduvall 
43301369Sdduvall 		/*
43313907Szh199473 		 * Block further PCI interrupts ...
43323907Szh199473 		 */
43333907Szh199473 		bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
433411968SYong.Tan@Sun.COM 		    bgep->chipid.mask_pci_int);
43353907Szh199473 
43363907Szh199473 	} else {
43373907Szh199473 		/*
43383907Szh199473 		 * Check MSI status
43391369Sdduvall 		 */
43403907Szh199473 		regval = bge_reg_get32(bgep, MSI_STATUS_REG);
43413907Szh199473 		if (regval & MSI_ERROR_ATTENTION) {
43423907Szh199473 			BGE_REPORT((bgep, "msi error attention,"
43433907Szh199473 			    " status=0x%x", regval));
43443907Szh199473 			bge_reg_put32(bgep, MSI_STATUS_REG, regval);
43453907Szh199473 		}
43463907Szh199473 	}
43473907Szh199473 
43483907Szh199473 	result = DDI_INTR_CLAIMED;
43493907Szh199473 
43503907Szh199473 	BGE_DEBUG(("bge_intr($%p) ($%p) regval 0x%08x", arg1, arg2, regval));
43513907Szh199473 
43523907Szh199473 	/*
43533907Szh199473 	 * Sync the status block and grab the flags-n-tag from it.
43543907Szh199473 	 * We count the number of interrupts where there doesn't
43553907Szh199473 	 * seem to have been a DMA update of the status block; if
43563907Szh199473 	 * it *has* been updated, the counter will be cleared in
43573907Szh199473 	 * the while() loop below ...
43583907Szh199473 	 */
43593907Szh199473 	bgep->missed_dmas += 1;
43603907Szh199473 	bsp = DMA_VPTR(bgep->status_block);
43613918Sml149210 	for (loop_cnt = 0; loop_cnt < bge_intr_max_loop; loop_cnt++) {
43623907Szh199473 		if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
43631369Sdduvall 			/*
43643907Szh199473 			 * bge_chip_stop() may have freed dma area etc
43653907Szh199473 			 * while we were in this interrupt handler -
43663907Szh199473 			 * better not call bge_status_sync()
43671369Sdduvall 			 */
43683907Szh199473 			(void) bge_check_acc_handle(bgep,
43693907Szh199473 			    bgep->io_handle);
43701369Sdduvall 			mutex_exit(bgep->genlock);
43713907Szh199473 			return (DDI_INTR_CLAIMED);
43723907Szh199473 		}
43733907Szh199473 		retval = bge_status_sync(bgep, STATUS_FLAG_UPDATED,
43743907Szh199473 		    &flags);
43753907Szh199473 		if (retval != DDI_FM_OK) {
43763907Szh199473 			bgep->bge_dma_error = B_TRUE;
43773907Szh199473 			goto chip_stop;
43781369Sdduvall 		}
43791369Sdduvall 
43803907Szh199473 		if (!(flags & STATUS_FLAG_UPDATED))
43813907Szh199473 			break;
43823907Szh199473 
43833907Szh199473 		/*
43843907Szh199473 		 * Tell the chip that we're processing the interrupt
43853907Szh199473 		 */
43863907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
43873907Szh199473 		    INTERRUPT_MBOX_DISABLE(flags));
43883907Szh199473 		if (bge_check_acc_handle(bgep, bgep->io_handle) !=
43893907Szh199473 		    DDI_FM_OK)
43903907Szh199473 			goto chip_stop;
43913907Szh199473 
43921369Sdduvall 		/*
43933907Szh199473 		 * Drop the mutex while we:
43943907Szh199473 		 * 	Receive any newly-arrived packets
43953907Szh199473 		 *	Recycle any newly-finished send buffers
43961369Sdduvall 		 */
43973907Szh199473 		bgep->bge_intr_running = B_TRUE;
43983907Szh199473 		mutex_exit(bgep->genlock);
43993907Szh199473 		bge_receive(bgep, bsp);
440012547SYong.Tan@Sun.COM 		(void) bge_recycle(bgep, bsp);
44013907Szh199473 		mutex_enter(bgep->genlock);
44023907Szh199473 		bgep->bge_intr_running = B_FALSE;
44031369Sdduvall 
44041369Sdduvall 		/*
44053907Szh199473 		 * Tell the chip we've finished processing, and
44063907Szh199473 		 * give it the tag that we got from the status
44073907Szh199473 		 * block earlier, so that it knows just how far
44083907Szh199473 		 * we've gone.  If it's got more for us to do,
44093907Szh199473 		 * it will now update the status block and try
44103907Szh199473 		 * to assert an interrupt (but we've got the
44113907Szh199473 		 * #INTA blocked at present).  If we see the
44123907Szh199473 		 * update, we'll loop around to do some more.
44133907Szh199473 		 * Eventually we'll get out of here ...
44143907Szh199473 		 */
44153907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
44163907Szh199473 		    INTERRUPT_MBOX_ENABLE(flags));
44176546Sgh162552 		if (bgep->chipid.pci_type == BGE_PCI_E)
44186546Sgh162552 			(void) bge_mbx_get(bgep, INTERRUPT_MBOX_0_REG);
44193907Szh199473 		bgep->missed_dmas = 0;
44203907Szh199473 	}
44213907Szh199473 
44223907Szh199473 	/*
44233907Szh199473 	 * Check for exceptional conditions that we need to handle
44243907Szh199473 	 *
44253907Szh199473 	 * Link status changed
44263907Szh199473 	 * Status block not updated
44273907Szh199473 	 */
44283907Szh199473 	if (flags & STATUS_FLAG_LINK_CHANGED)
44293907Szh199473 		bge_wake_factotum(bgep);
44303907Szh199473 
44313907Szh199473 	if (bgep->missed_dmas) {
44323907Szh199473 		/*
44333907Szh199473 		 * Probably due to the internal status tag not
44343907Szh199473 		 * being reset.  Force a status block update now;
44353907Szh199473 		 * this should ensure that we get an update and
44363907Szh199473 		 * a new interrupt.  After that, we should be in
44373907Szh199473 		 * sync again ...
44381369Sdduvall 		 */
44393907Szh199473 		BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
44403907Szh199473 		    "not updated?", flags));
44413907Szh199473 		bgep->missed_updates++;
44423907Szh199473 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
44433907Szh199473 		    COALESCE_NOW);
44443907Szh199473 
44453907Szh199473 		if (bgep->missed_dmas >= bge_dma_miss_limit) {
44463907Szh199473 			/*
44473907Szh199473 			 * If this happens multiple times in a row,
44483907Szh199473 			 * it means DMA is just not working.  Maybe
44493907Szh199473 			 * the chip's failed, or maybe there's a
44503907Szh199473 			 * problem on the PCI bus or in the host-PCI
44513907Szh199473 			 * bridge (Tomatillo).
44523907Szh199473 			 *
44533907Szh199473 			 * At all events, we want to stop further
44543907Szh199473 			 * interrupts and let the recovery code take
44553907Szh199473 			 * over to see whether anything can be done
44563907Szh199473 			 * about it ...
44573907Szh199473 			 */
44583907Szh199473 			bge_fm_ereport(bgep,
44593907Szh199473 			    DDI_FM_DEVICE_BADINT_LIMIT);
44603907Szh199473 			goto chip_stop;
44611369Sdduvall 		}
44621369Sdduvall 	}
44631369Sdduvall 
44643907Szh199473 	/*
44653907Szh199473 	 * Reenable assertion of #INTA, unless there's a DMA fault
44663907Szh199473 	 */
44673907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
44683907Szh199473 		bge_reg_clr32(bgep, PCI_CONF_BGE_MHCR,
446911968SYong.Tan@Sun.COM 		    bgep->chipid.mask_pci_int);
44703907Szh199473 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
44713907Szh199473 		    DDI_FM_OK)
44723907Szh199473 			goto chip_stop;
44733907Szh199473 	}
44743907Szh199473 
44751865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
44761865Sdilpreet 		goto chip_stop;
44771865Sdilpreet 
44781865Sdilpreet 	mutex_exit(bgep->genlock);
44791865Sdilpreet 	return (result);
44801865Sdilpreet 
44811865Sdilpreet chip_stop:
44821865Sdilpreet #ifdef BGE_IPMI_ASF
44831865Sdilpreet 	if (bgep->asf_enabled && bgep->asf_status == ASF_STAT_RUN) {
44841865Sdilpreet 		/*
44851865Sdilpreet 		 * We must stop ASF heart beat before
44861865Sdilpreet 		 * bge_chip_stop(), otherwise some
44871865Sdilpreet 		 * computers (ex. IBM HS20 blade
44881865Sdilpreet 		 * server) may crash.
44891865Sdilpreet 		 */
44901865Sdilpreet 		bge_asf_update_status(bgep);
44911865Sdilpreet 		bge_asf_stop_timer(bgep);
44921865Sdilpreet 		bgep->asf_status = ASF_STAT_STOP;
44931865Sdilpreet 
44941865Sdilpreet 		bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
44951865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
44961865Sdilpreet 	}
44971865Sdilpreet #endif
44981865Sdilpreet 	bge_chip_stop(bgep, B_TRUE);
44991865Sdilpreet 	(void) bge_check_acc_handle(bgep, bgep->io_handle);
45001369Sdduvall 	mutex_exit(bgep->genlock);
45011369Sdduvall 	return (result);
45021369Sdduvall }
45031369Sdduvall 
45041369Sdduvall /*
45051369Sdduvall  * ========== Factotum, implemented as a softint handler ==========
45061369Sdduvall  */
45071369Sdduvall 
45081369Sdduvall #undef	BGE_DBG
45091369Sdduvall #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
45101369Sdduvall 
45111369Sdduvall static void bge_factotum_error_handler(bge_t *bgep);
45121369Sdduvall #pragma	no_inline(bge_factotum_error_handler)
45131369Sdduvall 
45141369Sdduvall static void
bge_factotum_error_handler(bge_t * bgep)45151369Sdduvall bge_factotum_error_handler(bge_t *bgep)
45161369Sdduvall {
45171369Sdduvall 	uint32_t flow;
45181369Sdduvall 	uint32_t rdma;
45191369Sdduvall 	uint32_t wdma;
45201369Sdduvall 	uint32_t tmac;
45211369Sdduvall 	uint32_t rmac;
45221369Sdduvall 	uint32_t rxrs;
45231369Sdduvall 	uint32_t txrs = 0;
45241369Sdduvall 
45251369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
45261369Sdduvall 
45271369Sdduvall 	/*
45281369Sdduvall 	 * Read all the registers that show the possible
45291369Sdduvall 	 * reasons for the ERROR bit to be asserted
45301369Sdduvall 	 */
45311369Sdduvall 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
45321369Sdduvall 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
45331369Sdduvall 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
45341369Sdduvall 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
45351369Sdduvall 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
45361369Sdduvall 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
45371369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
45381369Sdduvall 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
45391369Sdduvall 
45401369Sdduvall 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
45414588Sml149210 	    (void *)bgep, flow, rdma, wdma));
45421369Sdduvall 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
45434588Sml149210 	    (void *)bgep, tmac, rmac, rxrs, txrs));
45441369Sdduvall 
45451369Sdduvall 	/*
45461369Sdduvall 	 * For now, just clear all the errors ...
45471369Sdduvall 	 */
45481369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
45491369Sdduvall 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
45501369Sdduvall 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
45511369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
45521369Sdduvall 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
45531369Sdduvall 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
45541369Sdduvall 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
45551369Sdduvall }
45561369Sdduvall 
45571369Sdduvall /*
45581369Sdduvall  * Handler for hardware link state change.
45591369Sdduvall  *
45601369Sdduvall  * When this routine is called, the hardware link state has changed
45611369Sdduvall  * and the new state is reflected in the param_* variables.  Here
45624403Sgd78059  * we must update the softstate and reprogram the MAC to match.
45631369Sdduvall  */
45641369Sdduvall static void bge_factotum_link_handler(bge_t *bgep);
45651369Sdduvall #pragma	no_inline(bge_factotum_link_handler)
45661369Sdduvall 
45671369Sdduvall static void
bge_factotum_link_handler(bge_t * bgep)45681369Sdduvall bge_factotum_link_handler(bge_t *bgep)
45691369Sdduvall {
45701369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
45711369Sdduvall 
45721369Sdduvall 	/*
45731369Sdduvall 	 * Update the s/w link_state
45741369Sdduvall 	 */
45751369Sdduvall 	if (bgep->param_link_up)
45761369Sdduvall 		bgep->link_state = LINK_STATE_UP;
45771369Sdduvall 	else
45781369Sdduvall 		bgep->link_state = LINK_STATE_DOWN;
45791369Sdduvall 
45801369Sdduvall 	/*
45811369Sdduvall 	 * Reprogram the MAC modes to match
45821369Sdduvall 	 */
45831369Sdduvall 	bge_sync_mac_modes(bgep);
45841369Sdduvall }
45851369Sdduvall 
45861865Sdilpreet static boolean_t bge_factotum_link_check(bge_t *bgep, int *dma_state);
45871369Sdduvall #pragma	no_inline(bge_factotum_link_check)
45881369Sdduvall 
45891369Sdduvall static boolean_t
bge_factotum_link_check(bge_t * bgep,int * dma_state)45901865Sdilpreet bge_factotum_link_check(bge_t *bgep, int *dma_state)
45911369Sdduvall {
45921369Sdduvall 	boolean_t check;
45931369Sdduvall 	uint64_t flags;
45941369Sdduvall 	uint32_t tmac_status;
45951369Sdduvall 
45961369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
45971369Sdduvall 
45981369Sdduvall 	/*
45991369Sdduvall 	 * Get & clear the writable status bits in the Tx status register
46001369Sdduvall 	 * (some bits are write-1-to-clear, others are just readonly).
46011369Sdduvall 	 */
46021369Sdduvall 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
46031369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
46041369Sdduvall 
46051369Sdduvall 	/*
46061369Sdduvall 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
46071369Sdduvall 	 */
46081865Sdilpreet 	*dma_state = bge_status_sync(bgep, STATUS_FLAG_ERROR |
46091865Sdilpreet 	    STATUS_FLAG_LINK_CHANGED, &flags);
46101865Sdilpreet 	if (*dma_state != DDI_FM_OK)
46111865Sdilpreet 		return (B_FALSE);
46121369Sdduvall 
46131369Sdduvall 	/*
46141369Sdduvall 	 * Clear any errors flagged in the status block ...
46151369Sdduvall 	 */
46161369Sdduvall 	if (flags & STATUS_FLAG_ERROR)
46171369Sdduvall 		bge_factotum_error_handler(bgep);
46181369Sdduvall 
46191369Sdduvall 	/*
46201369Sdduvall 	 * We need to check the link status if:
46211369Sdduvall 	 *	the status block says there's been a link change
46221369Sdduvall 	 *	or there's any discrepancy between the various
46231369Sdduvall 	 *	flags indicating the link state (link_state,
46241369Sdduvall 	 *	param_link_up, and the LINK STATE bit in the
46251369Sdduvall 	 *	Transmit MAC status register).
46261369Sdduvall 	 */
46271369Sdduvall 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
46281369Sdduvall 	switch (bgep->link_state) {
46291369Sdduvall 	case LINK_STATE_UP:
46301369Sdduvall 		check |= (bgep->param_link_up == B_FALSE);
46311369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
46321369Sdduvall 		break;
46331369Sdduvall 
46341369Sdduvall 	case LINK_STATE_DOWN:
46351369Sdduvall 		check |= (bgep->param_link_up != B_FALSE);
46361369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
46371369Sdduvall 		break;
46381369Sdduvall 
46391369Sdduvall 	default:
46401369Sdduvall 		check = B_TRUE;
46411369Sdduvall 		break;
46421369Sdduvall 	}
46431369Sdduvall 
46441369Sdduvall 	/*
46451369Sdduvall 	 * If <check> is false, we're sure the link hasn't changed.
46461369Sdduvall 	 * If true, however, it's not yet definitive; we have to call
46471369Sdduvall 	 * bge_phys_check() to determine whether the link has settled
46481369Sdduvall 	 * into a new state yet ... and if it has, then call the link
46491369Sdduvall 	 * state change handler.But when the chip is 5700 in Dell 6650
46501369Sdduvall 	 * ,even if check is false, the link may have changed.So we
46511369Sdduvall 	 * have to call bge_phys_check() to determine the link state.
46521369Sdduvall 	 */
46531369Sdduvall 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
46541369Sdduvall 		check = bge_phys_check(bgep);
46551369Sdduvall 		if (check)
46561369Sdduvall 			bge_factotum_link_handler(bgep);
46571369Sdduvall 	}
46581369Sdduvall 
46591369Sdduvall 	return (check);
46601369Sdduvall }
46611369Sdduvall 
46621369Sdduvall /*
46631369Sdduvall  * Factotum routine to check for Tx stall, using the 'watchdog' counter
46641369Sdduvall  */
46651369Sdduvall static boolean_t bge_factotum_stall_check(bge_t *bgep);
46661369Sdduvall #pragma	no_inline(bge_factotum_stall_check)
46671369Sdduvall 
46681369Sdduvall static boolean_t
bge_factotum_stall_check(bge_t * bgep)46691369Sdduvall bge_factotum_stall_check(bge_t *bgep)
46701369Sdduvall {
46711369Sdduvall 	uint32_t dogval;
467212547SYong.Tan@Sun.COM 	bge_status_t *bsp;
467312547SYong.Tan@Sun.COM 	uint64_t now = gethrtime();
467412547SYong.Tan@Sun.COM 
467512547SYong.Tan@Sun.COM 	if ((now - bgep->timestamp) < BGE_CYCLIC_PERIOD)
467612547SYong.Tan@Sun.COM 		return (B_FALSE);
467712547SYong.Tan@Sun.COM 
467812547SYong.Tan@Sun.COM 	bgep->timestamp = now;
46791369Sdduvall 
46801369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
46811369Sdduvall 
46821369Sdduvall 	/*
46831369Sdduvall 	 * Specific check for Tx stall ...
46841369Sdduvall 	 *
46851369Sdduvall 	 * The 'watchdog' counter is incremented whenever a packet
46861369Sdduvall 	 * is queued, reset to 1 when some (but not all) buffers
46871369Sdduvall 	 * are reclaimed, reset to 0 (disabled) when all buffers
46881369Sdduvall 	 * are reclaimed, and shifted left here.  If it exceeds the
46891369Sdduvall 	 * threshold value, the chip is assumed to have stalled and
46901369Sdduvall 	 * is put into the ERROR state.  The factotum will then reset
46911369Sdduvall 	 * it on the next pass.
46921369Sdduvall 	 *
46931369Sdduvall 	 * All of which should ensure that we don't get into a state
46941369Sdduvall 	 * where packets are left pending indefinitely!
46951369Sdduvall 	 */
46961369Sdduvall 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
469712547SYong.Tan@Sun.COM 	bsp = DMA_VPTR(bgep->status_block);
469812547SYong.Tan@Sun.COM 	if (dogval < bge_watchdog_count || bge_recycle(bgep, bsp))
46991369Sdduvall 		return (B_FALSE);
47001369Sdduvall 
47013918Sml149210 #if !defined(BGE_NETCONSOLE)
47021369Sdduvall 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
47033918Sml149210 #endif
47041865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_STALL);
47051369Sdduvall 	return (B_TRUE);
47061369Sdduvall }
47071369Sdduvall 
47081369Sdduvall /*
47091369Sdduvall  * The factotum is woken up when there's something to do that we'd rather
47101369Sdduvall  * not do from inside a hardware interrupt handler or high-level cyclic.
47111369Sdduvall  * Its two main tasks are:
47121369Sdduvall  *	reset & restart the chip after an error
47131369Sdduvall  *	check the link status whenever necessary
47141369Sdduvall  */
47151369Sdduvall uint_t bge_chip_factotum(caddr_t arg);
47161369Sdduvall #pragma	no_inline(bge_chip_factotum)
47171369Sdduvall 
47181369Sdduvall uint_t
bge_chip_factotum(caddr_t arg)47191369Sdduvall bge_chip_factotum(caddr_t arg)
47201369Sdduvall {
47211369Sdduvall 	bge_t *bgep;
47221369Sdduvall 	uint_t result;
47231369Sdduvall 	boolean_t error;
47241369Sdduvall 	boolean_t linkchg;
47251865Sdilpreet 	int dma_state;
47261369Sdduvall 
47277099Syt223700 	bgep = (void *)arg;
47281369Sdduvall 
47291369Sdduvall 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
47301369Sdduvall 
47311369Sdduvall 	mutex_enter(bgep->softintrlock);
47321369Sdduvall 	if (bgep->factotum_flag == 0) {
47331369Sdduvall 		mutex_exit(bgep->softintrlock);
47341369Sdduvall 		return (DDI_INTR_UNCLAIMED);
47351369Sdduvall 	}
47361504Sly149593 	bgep->factotum_flag = 0;
47371369Sdduvall 	mutex_exit(bgep->softintrlock);
47381369Sdduvall 
47391369Sdduvall 	result = DDI_INTR_CLAIMED;
47401369Sdduvall 	error = B_FALSE;
47411369Sdduvall 	linkchg = B_FALSE;
47421369Sdduvall 
47431369Sdduvall 	mutex_enter(bgep->genlock);
47441369Sdduvall 	switch (bgep->bge_chip_state) {
47451369Sdduvall 	default:
47461369Sdduvall 		break;
47471369Sdduvall 
47481369Sdduvall 	case BGE_CHIP_RUNNING:
47491865Sdilpreet 		linkchg = bge_factotum_link_check(bgep, &dma_state);
47501369Sdduvall 		error = bge_factotum_stall_check(bgep);
47511865Sdilpreet 		if (dma_state != DDI_FM_OK) {
47521865Sdilpreet 			bgep->bge_dma_error = B_TRUE;
47531865Sdilpreet 			error = B_TRUE;
47541865Sdilpreet 		}
47551865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
47561865Sdilpreet 			error = B_TRUE;
47571865Sdilpreet 		if (error)
47581865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_ERROR;
47591369Sdduvall 		break;
47601369Sdduvall 
47611369Sdduvall 	case BGE_CHIP_ERROR:
47621369Sdduvall 		error = B_TRUE;
47631369Sdduvall 		break;
47641369Sdduvall 
47651369Sdduvall 	case BGE_CHIP_FAULT:
47661369Sdduvall 		/*
47671369Sdduvall 		 * Fault detected, time to reset ...
47681369Sdduvall 		 */
47691369Sdduvall 		if (bge_autorecover) {
47701865Sdilpreet 			if (!(bgep->progress & PROGRESS_BUFS)) {
47711865Sdilpreet 				/*
47721865Sdilpreet 				 * if we can't allocate the ring buffers,
47731865Sdilpreet 				 * try later
47741865Sdilpreet 				 */
47751865Sdilpreet 				if (bge_alloc_bufs(bgep) != DDI_SUCCESS) {
47761865Sdilpreet 					mutex_exit(bgep->genlock);
47771865Sdilpreet 					return (result);
47781865Sdilpreet 				}
47791865Sdilpreet 				bgep->progress |= PROGRESS_BUFS;
47801865Sdilpreet 			}
47811865Sdilpreet 			if (!(bgep->progress & PROGRESS_INTR)) {
47821865Sdilpreet 				bge_init_rings(bgep);
47831865Sdilpreet 				bge_intr_enable(bgep);
47841865Sdilpreet 				bgep->progress |= PROGRESS_INTR;
47851865Sdilpreet 			}
47861865Sdilpreet 			if (!(bgep->progress & PROGRESS_KSTATS)) {
47871865Sdilpreet 				bge_init_kstats(bgep,
47881865Sdilpreet 				    ddi_get_instance(bgep->devinfo));
47891865Sdilpreet 				bgep->progress |= PROGRESS_KSTATS;
47901865Sdilpreet 			}
47911865Sdilpreet 
47921369Sdduvall 			BGE_REPORT((bgep, "automatic recovery activated"));
47931865Sdilpreet 
47941865Sdilpreet 			if (bge_restart(bgep, B_FALSE) != DDI_SUCCESS) {
47951865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
47961865Sdilpreet 				error = B_TRUE;
47971865Sdilpreet 			}
47981865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
47991865Sdilpreet 			    DDI_FM_OK) {
48001865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
48011865Sdilpreet 				error = B_TRUE;
48021865Sdilpreet 			}
48031865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
48041865Sdilpreet 			    DDI_FM_OK) {
48051865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
48061865Sdilpreet 				error = B_TRUE;
48071865Sdilpreet 			}
48081865Sdilpreet 			if (error == B_FALSE) {
48091408Srandyf #ifdef BGE_IPMI_ASF
48101865Sdilpreet 				if (bgep->asf_enabled &&
48111865Sdilpreet 				    bgep->asf_status != ASF_STAT_RUN) {
48121408Srandyf 					bgep->asf_timeout_id = timeout(
48131865Sdilpreet 					    bge_asf_heartbeat, (void *)bgep,
48141865Sdilpreet 					    drv_usectohz(
48151865Sdilpreet 					    BGE_ASF_HEARTBEAT_INTERVAL));
48161408Srandyf 					bgep->asf_status = ASF_STAT_RUN;
48171408Srandyf 				}
48181865Sdilpreet #endif
48195903Ssowmini 				if (!bgep->manual_reset) {
48205903Ssowmini 					ddi_fm_service_impact(bgep->devinfo,
48215903Ssowmini 					    DDI_SERVICE_RESTORED);
48225903Ssowmini 				}
48231408Srandyf 			}
48241369Sdduvall 		}
48251369Sdduvall 		break;
48261369Sdduvall 	}
48271369Sdduvall 
48281865Sdilpreet 
48291369Sdduvall 	/*
48301369Sdduvall 	 * If an error is detected, stop the chip now, marking it as
48311369Sdduvall 	 * faulty, so that it will be reset next time through ...
48321865Sdilpreet 	 *
48331865Sdilpreet 	 * Note that if intr_running is set, then bge_intr() has dropped
48341865Sdilpreet 	 * genlock to call bge_receive/bge_recycle. Can't stop the chip at
48351865Sdilpreet 	 * this point so have to wait until the next time the factotum runs.
48361369Sdduvall 	 */
48371865Sdilpreet 	if (error && !bgep->bge_intr_running) {
48381408Srandyf #ifdef BGE_IPMI_ASF
48391408Srandyf 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
48401408Srandyf 			/*
48411408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
48421408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
48431408Srandyf 			 * may crash.
48441408Srandyf 			 */
48451408Srandyf 			bge_asf_update_status(bgep);
48461408Srandyf 			bge_asf_stop_timer(bgep);
48471408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
48481408Srandyf 
48491408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
48501865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
48511408Srandyf 		}
48521408Srandyf #endif
48531369Sdduvall 		bge_chip_stop(bgep, B_TRUE);
48541865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
48551408Srandyf 	}
48561369Sdduvall 	mutex_exit(bgep->genlock);
48571369Sdduvall 
48581369Sdduvall 	/*
48591369Sdduvall 	 * If the link state changed, tell the world about it.
48601369Sdduvall 	 * Note: can't do this while still holding the mutex.
48611369Sdduvall 	 */
48626546Sgh162552 	if (bgep->link_update_timer == BGE_LINK_UPDATE_TIMEOUT &&
48636546Sgh162552 	    bgep->link_state != LINK_STATE_UNKNOWN)
48646546Sgh162552 		linkchg = B_TRUE;
48656546Sgh162552 	else if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT &&
48666546Sgh162552 	    bgep->link_state == LINK_STATE_DOWN)
48676546Sgh162552 		linkchg = B_FALSE;
48686546Sgh162552 
48696546Sgh162552 	if (linkchg) {
48702311Sseb 		mac_link_update(bgep->mh, bgep->link_state);
48716546Sgh162552 		bgep->link_update_timer = BGE_LINK_UPDATE_DONE;
48726546Sgh162552 	}
48735903Ssowmini 	if (bgep->manual_reset) {
48745903Ssowmini 		bgep->manual_reset = B_FALSE;
48755903Ssowmini 	}
48761369Sdduvall 
48771369Sdduvall 	return (result);
48781369Sdduvall }
48791369Sdduvall 
48801369Sdduvall /*
48811369Sdduvall  * High-level cyclic handler
48821369Sdduvall  *
48831369Sdduvall  * This routine schedules a (low-level) softint callback to the
48841369Sdduvall  * factotum, and prods the chip to update the status block (which
48851369Sdduvall  * will cause a hardware interrupt when complete).
48861369Sdduvall  */
48871369Sdduvall void bge_chip_cyclic(void *arg);
48881369Sdduvall #pragma	no_inline(bge_chip_cyclic)
48891369Sdduvall 
48901369Sdduvall void
bge_chip_cyclic(void * arg)48911369Sdduvall bge_chip_cyclic(void *arg)
48921369Sdduvall {
48931369Sdduvall 	bge_t *bgep;
48941369Sdduvall 
48951369Sdduvall 	bgep = arg;
48961369Sdduvall 
48971369Sdduvall 	switch (bgep->bge_chip_state) {
48981369Sdduvall 	default:
48991369Sdduvall 		return;
49001369Sdduvall 
49011369Sdduvall 	case BGE_CHIP_RUNNING:
49021369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
49031865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
49041865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
49051865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
49066546Sgh162552 
49076546Sgh162552 		if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT)
49086546Sgh162552 			bgep->link_update_timer++;
49096546Sgh162552 
49101369Sdduvall 		break;
49111369Sdduvall 
49121369Sdduvall 	case BGE_CHIP_FAULT:
49131369Sdduvall 	case BGE_CHIP_ERROR:
49141369Sdduvall 		break;
49151369Sdduvall 	}
49161369Sdduvall 
49171369Sdduvall 	bge_wake_factotum(bgep);
49181369Sdduvall }
49191369Sdduvall 
49201369Sdduvall 
49211369Sdduvall /*
49221369Sdduvall  * ========== Ioctl subfunctions ==========
49231369Sdduvall  */
49241369Sdduvall 
49251369Sdduvall #undef	BGE_DBG
49261369Sdduvall #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
49271369Sdduvall 
49281369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
49291369Sdduvall 
49301369Sdduvall static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
49311369Sdduvall #pragma	no_inline(bge_chip_peek_cfg)
49321369Sdduvall 
49331369Sdduvall static void
bge_chip_peek_cfg(bge_t * bgep,bge_peekpoke_t * ppd)49341369Sdduvall bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
49351369Sdduvall {
49361369Sdduvall 	uint64_t regval;
49371369Sdduvall 	uint64_t regno;
49381369Sdduvall 
49391369Sdduvall 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
49404588Sml149210 	    (void *)bgep, (void *)ppd));
49411369Sdduvall 
49421369Sdduvall 	regno = ppd->pp_acc_offset;
49431369Sdduvall 
49441369Sdduvall 	switch (ppd->pp_acc_size) {
49451369Sdduvall 	case 1:
49461369Sdduvall 		regval = pci_config_get8(bgep->cfg_handle, regno);
49471369Sdduvall 		break;
49481369Sdduvall 
49491369Sdduvall 	case 2:
49501369Sdduvall 		regval = pci_config_get16(bgep->cfg_handle, regno);
49511369Sdduvall 		break;
49521369Sdduvall 
49531369Sdduvall 	case 4:
49541369Sdduvall 		regval = pci_config_get32(bgep->cfg_handle, regno);
49551369Sdduvall 		break;
49561369Sdduvall 
49571369Sdduvall 	case 8:
49581369Sdduvall 		regval = pci_config_get64(bgep->cfg_handle, regno);
49591369Sdduvall 		break;
49601369Sdduvall 	}
49611369Sdduvall 
49621369Sdduvall 	ppd->pp_acc_data = regval;
49631369Sdduvall }
49641369Sdduvall 
49651369Sdduvall static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
49661369Sdduvall #pragma	no_inline(bge_chip_poke_cfg)
49671369Sdduvall 
49681369Sdduvall static void
bge_chip_poke_cfg(bge_t * bgep,bge_peekpoke_t * ppd)49691369Sdduvall bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
49701369Sdduvall {
49711369Sdduvall 	uint64_t regval;
49721369Sdduvall 	uint64_t regno;
49731369Sdduvall 
49741369Sdduvall 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
49754588Sml149210 	    (void *)bgep, (void *)ppd));
49761369Sdduvall 
49771369Sdduvall 	regno = ppd->pp_acc_offset;
49781369Sdduvall 	regval = ppd->pp_acc_data;
49791369Sdduvall 
49801369Sdduvall 	switch (ppd->pp_acc_size) {
49811369Sdduvall 	case 1:
49821369Sdduvall 		pci_config_put8(bgep->cfg_handle, regno, regval);
49831369Sdduvall 		break;
49841369Sdduvall 
49851369Sdduvall 	case 2:
49861369Sdduvall 		pci_config_put16(bgep->cfg_handle, regno, regval);
49871369Sdduvall 		break;
49881369Sdduvall 
49891369Sdduvall 	case 4:
49901369Sdduvall 		pci_config_put32(bgep->cfg_handle, regno, regval);
49911369Sdduvall 		break;
49921369Sdduvall 
49931369Sdduvall 	case 8:
49941369Sdduvall 		pci_config_put64(bgep->cfg_handle, regno, regval);
49951369Sdduvall 		break;
49961369Sdduvall 	}
49971369Sdduvall }
49981369Sdduvall 
49991369Sdduvall static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
50001369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
50011369Sdduvall 
50021369Sdduvall static void
bge_chip_peek_reg(bge_t * bgep,bge_peekpoke_t * ppd)50031369Sdduvall bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
50041369Sdduvall {
50051369Sdduvall 	uint64_t regval;
50061369Sdduvall 	void *regaddr;
50071369Sdduvall 
50081369Sdduvall 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
50094588Sml149210 	    (void *)bgep, (void *)ppd));
50101369Sdduvall 
50111369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
50121369Sdduvall 
50131369Sdduvall 	switch (ppd->pp_acc_size) {
50141369Sdduvall 	case 1:
50151369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
50161369Sdduvall 		break;
50171369Sdduvall 
50181369Sdduvall 	case 2:
50191369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
50201369Sdduvall 		break;
50211369Sdduvall 
50221369Sdduvall 	case 4:
50231369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
50241369Sdduvall 		break;
50251369Sdduvall 
50261369Sdduvall 	case 8:
50271369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
50281369Sdduvall 		break;
50291369Sdduvall 	}
50301369Sdduvall 
50311369Sdduvall 	ppd->pp_acc_data = regval;
50321369Sdduvall }
50331369Sdduvall 
50341369Sdduvall static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
50351369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
50361369Sdduvall 
50371369Sdduvall static void
bge_chip_poke_reg(bge_t * bgep,bge_peekpoke_t * ppd)50381369Sdduvall bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
50391369Sdduvall {
50401369Sdduvall 	uint64_t regval;
50411369Sdduvall 	void *regaddr;
50421369Sdduvall 
50431369Sdduvall 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
50444588Sml149210 	    (void *)bgep, (void *)ppd));
50451369Sdduvall 
50461369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
50471369Sdduvall 	regval = ppd->pp_acc_data;
50481369Sdduvall 
50491369Sdduvall 	switch (ppd->pp_acc_size) {
50501369Sdduvall 	case 1:
50511369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
50521369Sdduvall 		break;
50531369Sdduvall 
50541369Sdduvall 	case 2:
50551369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
50561369Sdduvall 		break;
50571369Sdduvall 
50581369Sdduvall 	case 4:
50591369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
50601369Sdduvall 		break;
50611369Sdduvall 
50621369Sdduvall 	case 8:
50631369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
50641369Sdduvall 		break;
50651369Sdduvall 	}
50661369Sdduvall 	BGE_PCICHK(bgep);
50671369Sdduvall }
50681369Sdduvall 
50691369Sdduvall static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
50701369Sdduvall #pragma	no_inline(bge_chip_peek_nic)
50711369Sdduvall 
50721369Sdduvall static void
bge_chip_peek_nic(bge_t * bgep,bge_peekpoke_t * ppd)50731369Sdduvall bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
50741369Sdduvall {
50751369Sdduvall 	uint64_t regoff;
50761369Sdduvall 	uint64_t regval;
50771369Sdduvall 	void *regaddr;
50781369Sdduvall 
50791369Sdduvall 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
50804588Sml149210 	    (void *)bgep, (void *)ppd));
50811369Sdduvall 
50821369Sdduvall 	regoff = ppd->pp_acc_offset;
50831369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
50841369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
50851369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
50861369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
50871369Sdduvall 
50881369Sdduvall 	switch (ppd->pp_acc_size) {
50891369Sdduvall 	case 1:
50901369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
50911369Sdduvall 		break;
50921369Sdduvall 
50931369Sdduvall 	case 2:
50941369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
50951369Sdduvall 		break;
50961369Sdduvall 
50971369Sdduvall 	case 4:
50981369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
50991369Sdduvall 		break;
51001369Sdduvall 
51011369Sdduvall 	case 8:
51021369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
51031369Sdduvall 		break;
51041369Sdduvall 	}
51051369Sdduvall 
51061369Sdduvall 	ppd->pp_acc_data = regval;
51071369Sdduvall }
51081369Sdduvall 
51091369Sdduvall static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
51101369Sdduvall #pragma	no_inline(bge_chip_poke_nic)
51111369Sdduvall 
51121369Sdduvall static void
bge_chip_poke_nic(bge_t * bgep,bge_peekpoke_t * ppd)51131369Sdduvall bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
51141369Sdduvall {
51151369Sdduvall 	uint64_t regoff;
51161369Sdduvall 	uint64_t regval;
51171369Sdduvall 	void *regaddr;
51181369Sdduvall 
51191369Sdduvall 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
51204588Sml149210 	    (void *)bgep, (void *)ppd));
51211369Sdduvall 
51221369Sdduvall 	regoff = ppd->pp_acc_offset;
51231369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
51241369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
51251369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
51261369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
51271369Sdduvall 	regval = ppd->pp_acc_data;
51281369Sdduvall 
51291369Sdduvall 	switch (ppd->pp_acc_size) {
51301369Sdduvall 	case 1:
51311369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
51321369Sdduvall 		break;
51331369Sdduvall 
51341369Sdduvall 	case 2:
51351369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
51361369Sdduvall 		break;
51371369Sdduvall 
51381369Sdduvall 	case 4:
51391369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
51401369Sdduvall 		break;
51411369Sdduvall 
51421369Sdduvall 	case 8:
51431369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
51441369Sdduvall 		break;
51451369Sdduvall 	}
51461369Sdduvall 	BGE_PCICHK(bgep);
51471369Sdduvall }
51481369Sdduvall 
51491369Sdduvall static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
51501369Sdduvall #pragma	no_inline(bge_chip_peek_mii)
51511369Sdduvall 
51521369Sdduvall static void
bge_chip_peek_mii(bge_t * bgep,bge_peekpoke_t * ppd)51531369Sdduvall bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
51541369Sdduvall {
51551369Sdduvall 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
51564588Sml149210 	    (void *)bgep, (void *)ppd));
51571369Sdduvall 
51581369Sdduvall 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
51591369Sdduvall }
51601369Sdduvall 
51611369Sdduvall static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
51621369Sdduvall #pragma	no_inline(bge_chip_poke_mii)
51631369Sdduvall 
51641369Sdduvall static void
bge_chip_poke_mii(bge_t * bgep,bge_peekpoke_t * ppd)51651369Sdduvall bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
51661369Sdduvall {
51671369Sdduvall 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
51684588Sml149210 	    (void *)bgep, (void *)ppd));
51691369Sdduvall 
51701369Sdduvall 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
51711369Sdduvall }
51721369Sdduvall 
51731369Sdduvall #if	BGE_SEE_IO32
51741369Sdduvall 
51751369Sdduvall static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
51761369Sdduvall #pragma	no_inline(bge_chip_peek_seeprom)
51771369Sdduvall 
51781369Sdduvall static void
bge_chip_peek_seeprom(bge_t * bgep,bge_peekpoke_t * ppd)51791369Sdduvall bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
51801369Sdduvall {
51811369Sdduvall 	uint32_t data;
51821369Sdduvall 	int err;
51831369Sdduvall 
51841369Sdduvall 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
51854588Sml149210 	    (void *)bgep, (void *)ppd));
51861369Sdduvall 
51871369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
51881369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
51891369Sdduvall }
51901369Sdduvall 
51911369Sdduvall static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
51921369Sdduvall #pragma	no_inline(bge_chip_poke_seeprom)
51931369Sdduvall 
51941369Sdduvall static void
bge_chip_poke_seeprom(bge_t * bgep,bge_peekpoke_t * ppd)51951369Sdduvall bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
51961369Sdduvall {
51971369Sdduvall 	uint32_t data;
51981369Sdduvall 
51991369Sdduvall 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
52004588Sml149210 	    (void *)bgep, (void *)ppd));
52011369Sdduvall 
52021369Sdduvall 	data = ppd->pp_acc_data;
52031369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
52041369Sdduvall }
52051369Sdduvall #endif	/* BGE_SEE_IO32 */
52061369Sdduvall 
52071369Sdduvall #if	BGE_FLASH_IO32
52081369Sdduvall 
52091369Sdduvall static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
52101369Sdduvall #pragma	no_inline(bge_chip_peek_flash)
52111369Sdduvall 
52121369Sdduvall static void
bge_chip_peek_flash(bge_t * bgep,bge_peekpoke_t * ppd)52131369Sdduvall bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
52141369Sdduvall {
52151369Sdduvall 	uint32_t data;
52161369Sdduvall 	int err;
52171369Sdduvall 
52181369Sdduvall 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
52194588Sml149210 	    (void *)bgep, (void *)ppd));
52201369Sdduvall 
52211369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
52221369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
52231369Sdduvall }
52241369Sdduvall 
52251369Sdduvall static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
52261369Sdduvall #pragma	no_inline(bge_chip_poke_flash)
52271369Sdduvall 
52281369Sdduvall static void
bge_chip_poke_flash(bge_t * bgep,bge_peekpoke_t * ppd)52291369Sdduvall bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
52301369Sdduvall {
52311369Sdduvall 	uint32_t data;
52321369Sdduvall 
52331369Sdduvall 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
52344588Sml149210 	    (void *)bgep, (void *)ppd));
52351369Sdduvall 
52361369Sdduvall 	data = ppd->pp_acc_data;
52371369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
52381369Sdduvall 	    ppd->pp_acc_offset, &data);
52391369Sdduvall }
52401369Sdduvall #endif	/* BGE_FLASH_IO32 */
52411369Sdduvall 
52421369Sdduvall static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
52431369Sdduvall #pragma	no_inline(bge_chip_peek_mem)
52441369Sdduvall 
52451369Sdduvall static void
bge_chip_peek_mem(bge_t * bgep,bge_peekpoke_t * ppd)52461369Sdduvall bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
52471369Sdduvall {
52481369Sdduvall 	uint64_t regval;
52491369Sdduvall 	void *vaddr;
52501369Sdduvall 
52511369Sdduvall 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
52524588Sml149210 	    (void *)bgep, (void *)ppd));
52531369Sdduvall 
52541369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
52551369Sdduvall 
52561369Sdduvall 	switch (ppd->pp_acc_size) {
52571369Sdduvall 	case 1:
52581369Sdduvall 		regval = *(uint8_t *)vaddr;
52591369Sdduvall 		break;
52601369Sdduvall 
52611369Sdduvall 	case 2:
52621369Sdduvall 		regval = *(uint16_t *)vaddr;
52631369Sdduvall 		break;
52641369Sdduvall 
52651369Sdduvall 	case 4:
52661369Sdduvall 		regval = *(uint32_t *)vaddr;
52671369Sdduvall 		break;
52681369Sdduvall 
52691369Sdduvall 	case 8:
52701369Sdduvall 		regval = *(uint64_t *)vaddr;
52711369Sdduvall 		break;
52721369Sdduvall 	}
52731369Sdduvall 
52741369Sdduvall 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
52754588Sml149210 	    (void *)bgep, (void *)ppd, regval, vaddr));
52761369Sdduvall 
52771369Sdduvall 	ppd->pp_acc_data = regval;
52781369Sdduvall }
52791369Sdduvall 
52801369Sdduvall static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
52811369Sdduvall #pragma	no_inline(bge_chip_poke_mem)
52821369Sdduvall 
52831369Sdduvall static void
bge_chip_poke_mem(bge_t * bgep,bge_peekpoke_t * ppd)52841369Sdduvall bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
52851369Sdduvall {
52861369Sdduvall 	uint64_t regval;
52871369Sdduvall 	void *vaddr;
52881369Sdduvall 
52891369Sdduvall 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
52904588Sml149210 	    (void *)bgep, (void *)ppd));
52911369Sdduvall 
52921369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
52931369Sdduvall 	regval = ppd->pp_acc_data;
52941369Sdduvall 
52951369Sdduvall 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
52964588Sml149210 	    (void *)bgep, (void *)ppd, regval, vaddr));
52971369Sdduvall 
52981369Sdduvall 	switch (ppd->pp_acc_size) {
52991369Sdduvall 	case 1:
53001369Sdduvall 		*(uint8_t *)vaddr = (uint8_t)regval;
53011369Sdduvall 		break;
53021369Sdduvall 
53031369Sdduvall 	case 2:
53041369Sdduvall 		*(uint16_t *)vaddr = (uint16_t)regval;
53051369Sdduvall 		break;
53061369Sdduvall 
53071369Sdduvall 	case 4:
53081369Sdduvall 		*(uint32_t *)vaddr = (uint32_t)regval;
53091369Sdduvall 		break;
53101369Sdduvall 
53111369Sdduvall 	case 8:
53121369Sdduvall 		*(uint64_t *)vaddr = (uint64_t)regval;
53131369Sdduvall 		break;
53141369Sdduvall 	}
53151369Sdduvall }
53161369Sdduvall 
53171369Sdduvall static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
53181369Sdduvall 					struct iocblk *iocp);
53191369Sdduvall #pragma	no_inline(bge_pp_ioctl)
53201369Sdduvall 
53211369Sdduvall static enum ioc_reply
bge_pp_ioctl(bge_t * bgep,int cmd,mblk_t * mp,struct iocblk * iocp)53221369Sdduvall bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
53231369Sdduvall {
53241369Sdduvall 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
53251369Sdduvall 	bge_peekpoke_t *ppd;
53261369Sdduvall 	dma_area_t *areap;
53271369Sdduvall 	uint64_t sizemask;
53281369Sdduvall 	uint64_t mem_va;
53291369Sdduvall 	uint64_t maxoff;
53301369Sdduvall 	boolean_t peek;
53311369Sdduvall 
53321369Sdduvall 	switch (cmd) {
53331369Sdduvall 	default:
53341369Sdduvall 		/* NOTREACHED */
53351369Sdduvall 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
53361369Sdduvall 		return (IOC_INVAL);
53371369Sdduvall 
53381369Sdduvall 	case BGE_PEEK:
53391369Sdduvall 		peek = B_TRUE;
53401369Sdduvall 		break;
53411369Sdduvall 
53421369Sdduvall 	case BGE_POKE:
53431369Sdduvall 		peek = B_FALSE;
53441369Sdduvall 		break;
53451369Sdduvall 	}
53461369Sdduvall 
53471369Sdduvall 	/*
53481369Sdduvall 	 * Validate format of ioctl
53491369Sdduvall 	 */
53501369Sdduvall 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
53511369Sdduvall 		return (IOC_INVAL);
53521369Sdduvall 	if (mp->b_cont == NULL)
53531369Sdduvall 		return (IOC_INVAL);
53547099Syt223700 	ppd = (void *)mp->b_cont->b_rptr;
53551369Sdduvall 
53561369Sdduvall 	/*
53571369Sdduvall 	 * Validate request parameters
53581369Sdduvall 	 */
53591369Sdduvall 	switch (ppd->pp_acc_space) {
53601369Sdduvall 	default:
53611369Sdduvall 		return (IOC_INVAL);
53621369Sdduvall 
53631369Sdduvall 	case BGE_PP_SPACE_CFG:
53641369Sdduvall 		/*
53651369Sdduvall 		 * Config space
53661369Sdduvall 		 */
53671369Sdduvall 		sizemask = 8|4|2|1;
53681369Sdduvall 		mem_va = 0;
53691369Sdduvall 		maxoff = PCI_CONF_HDR_SIZE;
53701369Sdduvall 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
53711369Sdduvall 		break;
53721369Sdduvall 
53731369Sdduvall 	case BGE_PP_SPACE_REG:
53741369Sdduvall 		/*
53751369Sdduvall 		 * Memory-mapped I/O space
53761369Sdduvall 		 */
53771369Sdduvall 		sizemask = 8|4|2|1;
53781369Sdduvall 		mem_va = 0;
53791369Sdduvall 		maxoff = RIAAR_REGISTER_MAX;
53801369Sdduvall 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
53811369Sdduvall 		break;
53821369Sdduvall 
53831369Sdduvall 	case BGE_PP_SPACE_NIC:
53841369Sdduvall 		/*
53851369Sdduvall 		 * NIC on-chip memory
53861369Sdduvall 		 */
53871369Sdduvall 		sizemask = 8|4|2|1;
53881369Sdduvall 		mem_va = 0;
53891369Sdduvall 		maxoff = MWBAR_ONCHIP_MAX;
53901369Sdduvall 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
53911369Sdduvall 		break;
53921369Sdduvall 
53931369Sdduvall 	case BGE_PP_SPACE_MII:
53941369Sdduvall 		/*
53951369Sdduvall 		 * PHY's MII registers
53961369Sdduvall 		 * NB: all PHY registers are two bytes, but the
53971369Sdduvall 		 * addresses increment in ones (word addressing).
53981369Sdduvall 		 * So we scale the address here, then undo the
53991369Sdduvall 		 * transformation inside the peek/poke functions.
54001369Sdduvall 		 */
54011369Sdduvall 		ppd->pp_acc_offset *= 2;
54021369Sdduvall 		sizemask = 2;
54031369Sdduvall 		mem_va = 0;
54041369Sdduvall 		maxoff = (MII_MAXREG+1)*2;
54051369Sdduvall 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
54061369Sdduvall 		break;
54071369Sdduvall 
54081369Sdduvall #if	BGE_SEE_IO32
54091369Sdduvall 	case BGE_PP_SPACE_SEEPROM:
54101369Sdduvall 		/*
54111369Sdduvall 		 * Attached SEEPROM(s), if any.
54121369Sdduvall 		 * NB: we use the high-order bits of the 'address' as
54131369Sdduvall 		 * a device select to accommodate multiple SEEPROMS,
54141369Sdduvall 		 * If each one is the maximum size (64kbytes), this
54151369Sdduvall 		 * makes them appear contiguous.  Otherwise, there may
54161369Sdduvall 		 * be holes in the mapping.  ENxS doesn't have any
54171369Sdduvall 		 * SEEPROMs anyway ...
54181369Sdduvall 		 */
54191369Sdduvall 		sizemask = 4;
54201369Sdduvall 		mem_va = 0;
54211369Sdduvall 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
54221369Sdduvall 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
54231369Sdduvall 		break;
54241369Sdduvall #endif	/* BGE_SEE_IO32 */
54251369Sdduvall 
54261369Sdduvall #if	BGE_FLASH_IO32
54271369Sdduvall 	case BGE_PP_SPACE_FLASH:
54281369Sdduvall 		/*
54291369Sdduvall 		 * Attached Flash device (if any); a maximum of one device
54301369Sdduvall 		 * is currently supported.  But it can be up to 1MB (unlike
54311369Sdduvall 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
54321369Sdduvall 		 */
54331369Sdduvall 		sizemask = 4;
54341369Sdduvall 		mem_va = 0;
54351369Sdduvall 		maxoff = NVM_FLASH_ADDR_MASK;
54361369Sdduvall 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
54371369Sdduvall 		break;
54381369Sdduvall #endif	/* BGE_FLASH_IO32 */
54391369Sdduvall 
54401369Sdduvall 	case BGE_PP_SPACE_BGE:
54411369Sdduvall 		/*
54421369Sdduvall 		 * BGE data structure!
54431369Sdduvall 		 */
54441369Sdduvall 		sizemask = 8|4|2|1;
54451369Sdduvall 		mem_va = (uintptr_t)bgep;
54461369Sdduvall 		maxoff = sizeof (*bgep);
54471369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
54481369Sdduvall 		break;
54491369Sdduvall 
54501369Sdduvall 	case BGE_PP_SPACE_STATUS:
54511369Sdduvall 	case BGE_PP_SPACE_STATISTICS:
54521369Sdduvall 	case BGE_PP_SPACE_TXDESC:
54531369Sdduvall 	case BGE_PP_SPACE_TXBUFF:
54541369Sdduvall 	case BGE_PP_SPACE_RXDESC:
54551369Sdduvall 	case BGE_PP_SPACE_RXBUFF:
54561369Sdduvall 		/*
54571369Sdduvall 		 * Various DMA_AREAs
54581369Sdduvall 		 */
54591369Sdduvall 		switch (ppd->pp_acc_space) {
54601369Sdduvall 		case BGE_PP_SPACE_TXDESC:
54611369Sdduvall 			areap = &bgep->tx_desc;
54621369Sdduvall 			break;
54631369Sdduvall 		case BGE_PP_SPACE_TXBUFF:
54641369Sdduvall 			areap = &bgep->tx_buff[0];
54651369Sdduvall 			break;
54661369Sdduvall 		case BGE_PP_SPACE_RXDESC:
54671369Sdduvall 			areap = &bgep->rx_desc[0];
54681369Sdduvall 			break;
54691369Sdduvall 		case BGE_PP_SPACE_RXBUFF:
54701369Sdduvall 			areap = &bgep->rx_buff[0];
54711369Sdduvall 			break;
54721369Sdduvall 		case BGE_PP_SPACE_STATUS:
54731369Sdduvall 			areap = &bgep->status_block;
54741369Sdduvall 			break;
54751369Sdduvall 		case BGE_PP_SPACE_STATISTICS:
54761369Sdduvall 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
54771369Sdduvall 				areap = &bgep->statistics;
54781369Sdduvall 			break;
54791369Sdduvall 		}
54801369Sdduvall 
54811369Sdduvall 		sizemask = 8|4|2|1;
54821369Sdduvall 		mem_va = (uintptr_t)areap->mem_va;
54831369Sdduvall 		maxoff = areap->alength;
54841369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
54851369Sdduvall 		break;
54861369Sdduvall 	}
54871369Sdduvall 
54881369Sdduvall 	switch (ppd->pp_acc_size) {
54891369Sdduvall 	default:
54901369Sdduvall 		return (IOC_INVAL);
54911369Sdduvall 
54921369Sdduvall 	case 8:
54931369Sdduvall 	case 4:
54941369Sdduvall 	case 2:
54951369Sdduvall 	case 1:
54961369Sdduvall 		if ((ppd->pp_acc_size & sizemask) == 0)
54971369Sdduvall 			return (IOC_INVAL);
54981369Sdduvall 		break;
54991369Sdduvall 	}
55001369Sdduvall 
55011369Sdduvall 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
55021369Sdduvall 		return (IOC_INVAL);
55031369Sdduvall 
55041369Sdduvall 	if (ppd->pp_acc_offset >= maxoff)
55051369Sdduvall 		return (IOC_INVAL);
55061369Sdduvall 
55071369Sdduvall 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
55081369Sdduvall 		return (IOC_INVAL);
55091369Sdduvall 
55101369Sdduvall 	/*
55111369Sdduvall 	 * All OK - go do it!
55121369Sdduvall 	 */
55131369Sdduvall 	ppd->pp_acc_offset += mem_va;
55141369Sdduvall 	(*ppfn)(bgep, ppd);
55151369Sdduvall 	return (peek ? IOC_REPLY : IOC_ACK);
55161369Sdduvall }
55171369Sdduvall 
55181369Sdduvall static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
55191369Sdduvall 					struct iocblk *iocp);
55201369Sdduvall #pragma	no_inline(bge_diag_ioctl)
55211369Sdduvall 
55221369Sdduvall static enum ioc_reply
bge_diag_ioctl(bge_t * bgep,int cmd,mblk_t * mp,struct iocblk * iocp)55231369Sdduvall bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
55241369Sdduvall {
55251369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
55261369Sdduvall 
55271369Sdduvall 	switch (cmd) {
55281369Sdduvall 	default:
55291369Sdduvall 		/* NOTREACHED */
55301369Sdduvall 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
55311369Sdduvall 		return (IOC_INVAL);
55321369Sdduvall 
55331369Sdduvall 	case BGE_DIAG:
55341369Sdduvall 		/*
55351369Sdduvall 		 * Currently a no-op
55361369Sdduvall 		 */
55371369Sdduvall 		return (IOC_ACK);
55381369Sdduvall 
55391369Sdduvall 	case BGE_PEEK:
55401369Sdduvall 	case BGE_POKE:
55411369Sdduvall 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
55421369Sdduvall 
55431369Sdduvall 	case BGE_PHY_RESET:
55441369Sdduvall 		return (IOC_RESTART_ACK);
55451369Sdduvall 
55461369Sdduvall 	case BGE_SOFT_RESET:
55471369Sdduvall 	case BGE_HARD_RESET:
55481369Sdduvall 		/*
55491369Sdduvall 		 * Reset and reinitialise the 570x hardware
55501369Sdduvall 		 */
55513918Sml149210 		bgep->bge_chip_state = BGE_CHIP_FAULT;
55523918Sml149210 		ddi_trigger_softintr(bgep->factotum_id);
55531865Sdilpreet 		(void) bge_restart(bgep, cmd == BGE_HARD_RESET);
55541369Sdduvall 		return (IOC_ACK);
55551369Sdduvall 	}
55561369Sdduvall 
55571369Sdduvall 	/* NOTREACHED */
55581369Sdduvall }
55591369Sdduvall 
55601369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
55611369Sdduvall 
55621369Sdduvall static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
55631369Sdduvall 				    struct iocblk *iocp);
55641369Sdduvall #pragma	no_inline(bge_mii_ioctl)
55651369Sdduvall 
55661369Sdduvall static enum ioc_reply
bge_mii_ioctl(bge_t * bgep,int cmd,mblk_t * mp,struct iocblk * iocp)55671369Sdduvall bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
55681369Sdduvall {
55691369Sdduvall 	struct bge_mii_rw *miirwp;
55701369Sdduvall 
55711369Sdduvall 	/*
55721369Sdduvall 	 * Validate format of ioctl
55731369Sdduvall 	 */
55741369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
55751369Sdduvall 		return (IOC_INVAL);
55761369Sdduvall 	if (mp->b_cont == NULL)
55771369Sdduvall 		return (IOC_INVAL);
55787099Syt223700 	miirwp = (void *)mp->b_cont->b_rptr;
55791369Sdduvall 
55801369Sdduvall 	/*
55811369Sdduvall 	 * Validate request parameters ...
55821369Sdduvall 	 */
55831369Sdduvall 	if (miirwp->mii_reg > MII_MAXREG)
55841369Sdduvall 		return (IOC_INVAL);
55851369Sdduvall 
55861369Sdduvall 	switch (cmd) {
55871369Sdduvall 	default:
55881369Sdduvall 		/* NOTREACHED */
55891369Sdduvall 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
55901369Sdduvall 		return (IOC_INVAL);
55911369Sdduvall 
55921369Sdduvall 	case BGE_MII_READ:
55931369Sdduvall 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
55941369Sdduvall 		return (IOC_REPLY);
55951369Sdduvall 
55961369Sdduvall 	case BGE_MII_WRITE:
55971369Sdduvall 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
55981369Sdduvall 		return (IOC_ACK);
55991369Sdduvall 	}
56001369Sdduvall 
56011369Sdduvall 	/* NOTREACHED */
56021369Sdduvall }
56031369Sdduvall 
56041369Sdduvall #if	BGE_SEE_IO32
56051369Sdduvall 
56061369Sdduvall static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
56071369Sdduvall 				    struct iocblk *iocp);
56081369Sdduvall #pragma	no_inline(bge_see_ioctl)
56091369Sdduvall 
56101369Sdduvall static enum ioc_reply
bge_see_ioctl(bge_t * bgep,int cmd,mblk_t * mp,struct iocblk * iocp)56111369Sdduvall bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
56121369Sdduvall {
56131369Sdduvall 	struct bge_see_rw *seerwp;
56141369Sdduvall 
56151369Sdduvall 	/*
56161369Sdduvall 	 * Validate format of ioctl
56171369Sdduvall 	 */
56181369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
56191369Sdduvall 		return (IOC_INVAL);
56201369Sdduvall 	if (mp->b_cont == NULL)
56211369Sdduvall 		return (IOC_INVAL);
56227099Syt223700 	seerwp = (void *)mp->b_cont->b_rptr;
56231369Sdduvall 
56241369Sdduvall 	/*
56251369Sdduvall 	 * Validate request parameters ...
56261369Sdduvall 	 */
56271369Sdduvall 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
56281369Sdduvall 		return (IOC_INVAL);
56291369Sdduvall 
56301369Sdduvall 	switch (cmd) {
56311369Sdduvall 	default:
56321369Sdduvall 		/* NOTREACHED */
56331369Sdduvall 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
56341369Sdduvall 		return (IOC_INVAL);
56351369Sdduvall 
56361369Sdduvall 	case BGE_SEE_READ:
56371369Sdduvall 	case BGE_SEE_WRITE:
56381369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
56391369Sdduvall 		    seerwp->see_addr, &seerwp->see_data);
56401369Sdduvall 		return (IOC_REPLY);
56411369Sdduvall 	}
56421369Sdduvall 
56431369Sdduvall 	/* NOTREACHED */
56441369Sdduvall }
56451369Sdduvall 
56461369Sdduvall #endif	/* BGE_SEE_IO32 */
56471369Sdduvall 
56481369Sdduvall #if	BGE_FLASH_IO32
56491369Sdduvall 
56501369Sdduvall static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
56511369Sdduvall 				    struct iocblk *iocp);
56521369Sdduvall #pragma	no_inline(bge_flash_ioctl)
56531369Sdduvall 
56541369Sdduvall static enum ioc_reply
bge_flash_ioctl(bge_t * bgep,int cmd,mblk_t * mp,struct iocblk * iocp)56551369Sdduvall bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
56561369Sdduvall {
56571369Sdduvall 	struct bge_flash_rw *flashrwp;
56581369Sdduvall 
56591369Sdduvall 	/*
56601369Sdduvall 	 * Validate format of ioctl
56611369Sdduvall 	 */
56621369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
56631369Sdduvall 		return (IOC_INVAL);
56641369Sdduvall 	if (mp->b_cont == NULL)
56651369Sdduvall 		return (IOC_INVAL);
56667099Syt223700 	flashrwp = (void *)mp->b_cont->b_rptr;
56671369Sdduvall 
56681369Sdduvall 	/*
56691369Sdduvall 	 * Validate request parameters ...
56701369Sdduvall 	 */
56711369Sdduvall 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
56721369Sdduvall 		return (IOC_INVAL);
56731369Sdduvall 
56741369Sdduvall 	switch (cmd) {
56751369Sdduvall 	default:
56761369Sdduvall 		/* NOTREACHED */
56771369Sdduvall 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
56781369Sdduvall 		return (IOC_INVAL);
56791369Sdduvall 
56801369Sdduvall 	case BGE_FLASH_READ:
56811369Sdduvall 	case BGE_FLASH_WRITE:
56821369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
56831369Sdduvall 		    flashrwp->flash_addr, &flashrwp->flash_data);
56841369Sdduvall 		return (IOC_REPLY);
56851369Sdduvall 	}
56861369Sdduvall 
56871369Sdduvall 	/* NOTREACHED */
56881369Sdduvall }
56891369Sdduvall 
56901369Sdduvall #endif	/* BGE_FLASH_IO32 */
56911369Sdduvall 
56921369Sdduvall enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
56931369Sdduvall 				struct iocblk *iocp);
56941369Sdduvall #pragma	no_inline(bge_chip_ioctl)
56951369Sdduvall 
56961369Sdduvall enum ioc_reply
bge_chip_ioctl(bge_t * bgep,queue_t * wq,mblk_t * mp,struct iocblk * iocp)56971369Sdduvall bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
56981369Sdduvall {
56991369Sdduvall 	int cmd;
57001369Sdduvall 
57011369Sdduvall 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
57024588Sml149210 	    (void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
57031369Sdduvall 
57041369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
57051369Sdduvall 
57061369Sdduvall 	cmd = iocp->ioc_cmd;
57071369Sdduvall 	switch (cmd) {
57081369Sdduvall 	default:
57091369Sdduvall 		/* NOTREACHED */
57101369Sdduvall 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
57111369Sdduvall 		return (IOC_INVAL);
57121369Sdduvall 
57131369Sdduvall 	case BGE_DIAG:
57141369Sdduvall 	case BGE_PEEK:
57151369Sdduvall 	case BGE_POKE:
57161369Sdduvall 	case BGE_PHY_RESET:
57171369Sdduvall 	case BGE_SOFT_RESET:
57181369Sdduvall 	case BGE_HARD_RESET:
57191369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
57201369Sdduvall 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
57211369Sdduvall #else
57221369Sdduvall 		return (IOC_INVAL);
57231369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
57241369Sdduvall 
57251369Sdduvall 	case BGE_MII_READ:
57261369Sdduvall 	case BGE_MII_WRITE:
57271369Sdduvall 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
57281369Sdduvall 
57291369Sdduvall #if	BGE_SEE_IO32
57301369Sdduvall 	case BGE_SEE_READ:
57311369Sdduvall 	case BGE_SEE_WRITE:
57321369Sdduvall 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
57331369Sdduvall #endif	/* BGE_SEE_IO32 */
57341369Sdduvall 
57351369Sdduvall #if	BGE_FLASH_IO32
57361369Sdduvall 	case BGE_FLASH_READ:
57371369Sdduvall 	case BGE_FLASH_WRITE:
57381369Sdduvall 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
57391369Sdduvall #endif	/* BGE_FLASH_IO32 */
57401369Sdduvall 	}
57411369Sdduvall 
57421369Sdduvall 	/* NOTREACHED */
57431369Sdduvall }
57441369Sdduvall 
57458275SEric Cheng /* ARGSUSED */
57461369Sdduvall void
bge_chip_blank(void * arg,time_t ticks,uint_t count,int flag)57478275SEric Cheng bge_chip_blank(void *arg, time_t ticks, uint_t count, int flag)
57481369Sdduvall {
57498275SEric Cheng 	recv_ring_t *rrp = arg;
57508275SEric Cheng 	bge_t *bgep = rrp->bgep;
57511369Sdduvall 
57521865Sdilpreet 	mutex_enter(bgep->genlock);
57538275SEric Cheng 	rrp->poll_flag = flag;
57548275SEric Cheng #ifdef NOT_YET
57558275SEric Cheng 	/*
57568275SEric Cheng 	 * XXX-Sunay: Since most broadcom cards support only one
57578275SEric Cheng 	 * interrupt but multiple rx rings, we can't disable the
57588275SEric Cheng 	 * physical interrupt. This need to be done via capability
57598275SEric Cheng 	 * negotiation depending on the NIC.
57608275SEric Cheng 	 */
57611369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
57621369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
57638275SEric Cheng #endif
57641865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
57651865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
57661865Sdilpreet 	mutex_exit(bgep->genlock);
57671369Sdduvall }
57681408Srandyf 
57691408Srandyf #ifdef BGE_IPMI_ASF
57701408Srandyf 
57711408Srandyf uint32_t
bge_nic_read32(bge_t * bgep,bge_regno_t addr)57721408Srandyf bge_nic_read32(bge_t *bgep, bge_regno_t addr)
57731408Srandyf {
57741408Srandyf 	uint32_t data;
57751408Srandyf 
57763918Sml149210 #ifndef __sparc
57771408Srandyf 	if (!bgep->asf_wordswapped) {
57781408Srandyf 		/* a workaround word swap error */
57791408Srandyf 		if (addr & 4)
57801408Srandyf 			addr = addr - 4;
57811408Srandyf 		else
57821408Srandyf 			addr = addr + 4;
57831408Srandyf 	}
578411968SYong.Tan@Sun.COM #else
578511968SYong.Tan@Sun.COM 	if (DEVICE_5717_SERIES_CHIPSETS(bgep))
578611968SYong.Tan@Sun.COM 		addr = LE_32(addr);
57873918Sml149210 #endif
57881408Srandyf 
57891408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
57901408Srandyf 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
57911408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
57921408Srandyf 
57933918Sml149210 	data = LE_32(data);
579411968SYong.Tan@Sun.COM 
579511968SYong.Tan@Sun.COM 	BGE_DEBUG(("bge_nic_read32($%p, 0x%x) => 0x%x",
579611968SYong.Tan@Sun.COM 	    (void *)bgep, addr, data));
579711968SYong.Tan@Sun.COM 
57981408Srandyf 	return (data);
57991408Srandyf }
58001408Srandyf 
58011408Srandyf void
bge_asf_update_status(bge_t * bgep)58021408Srandyf bge_asf_update_status(bge_t *bgep)
58031408Srandyf {
58041408Srandyf 	uint32_t event;
58051408Srandyf 
58061408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
58071408Srandyf 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
58081408Srandyf 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
58091408Srandyf 
58101408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
58111408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
58121408Srandyf }
58131408Srandyf 
58141408Srandyf 
58151408Srandyf /*
58161408Srandyf  * The driver is supposed to notify ASF that the OS is still running
58171408Srandyf  * every three seconds, otherwise the management server may attempt
58181408Srandyf  * to reboot the machine.  If it hasn't actually failed, this is
58192135Szh199473  * not a desirable result.  However, this isn't running as a real-time
58201408Srandyf  * thread, and even if it were, it might not be able to generate the
58211408Srandyf  * heartbeat in a timely manner due to system load.  As it isn't a
58221408Srandyf  * significant strain on the machine, we will set the interval to half
58231408Srandyf  * of the required value.
58241408Srandyf  */
58251408Srandyf void
bge_asf_heartbeat(void * arg)58261865Sdilpreet bge_asf_heartbeat(void *arg)
58271408Srandyf {
58281865Sdilpreet 	bge_t *bgep = (bge_t *)arg;
58291865Sdilpreet 
58301865Sdilpreet 	mutex_enter(bgep->genlock);
58311408Srandyf 	bge_asf_update_status((bge_t *)bgep);
58321865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
58331865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
58341865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
58351865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
58361865Sdilpreet 	mutex_exit(bgep->genlock);
58371408Srandyf 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
58384588Sml149210 	    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
58391408Srandyf }
58401408Srandyf 
58411408Srandyf 
58421408Srandyf void
bge_asf_stop_timer(bge_t * bgep)58431408Srandyf bge_asf_stop_timer(bge_t *bgep)
58441408Srandyf {
58451408Srandyf 	timeout_id_t tmp_id = 0;
58461408Srandyf 
58471408Srandyf 	while ((bgep->asf_timeout_id != 0) &&
58484588Sml149210 	    (tmp_id != bgep->asf_timeout_id)) {
58491408Srandyf 		tmp_id = bgep->asf_timeout_id;
58501408Srandyf 		(void) untimeout(tmp_id);
58511408Srandyf 	}
58521408Srandyf 	bgep->asf_timeout_id = 0;
58531408Srandyf }
58541408Srandyf 
58551408Srandyf 
58561408Srandyf 
58571408Srandyf /*
58582135Szh199473  * This function should be placed at the earliest position of bge_attach().
58591408Srandyf  */
58601408Srandyf void
bge_asf_get_config(bge_t * bgep)58611408Srandyf bge_asf_get_config(bge_t *bgep)
58621408Srandyf {
58631408Srandyf 	uint32_t nicsig;
58641408Srandyf 	uint32_t niccfg;
58651408Srandyf 
58663918Sml149210 	bgep->asf_enabled = B_FALSE;
58671408Srandyf 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
58681408Srandyf 	if (nicsig == BGE_NIC_DATA_SIG) {
58691408Srandyf 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
58701408Srandyf 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
58711408Srandyf 			/*
58721408Srandyf 			 * Here, we don't consider BAXTER, because BGE haven't
58731408Srandyf 			 * supported BAXTER (that is 5752). Also, as I know,
58741408Srandyf 			 * BAXTER doesn't support ASF feature.
58751408Srandyf 			 */
58761408Srandyf 			bgep->asf_enabled = B_TRUE;
58771408Srandyf 		else
58781408Srandyf 			bgep->asf_enabled = B_FALSE;
58791408Srandyf 	} else
58801408Srandyf 		bgep->asf_enabled = B_FALSE;
58811408Srandyf }
58821408Srandyf 
58831408Srandyf 
58841408Srandyf void
bge_asf_pre_reset_operations(bge_t * bgep,uint32_t mode)58851408Srandyf bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
58861408Srandyf {
58871408Srandyf 	uint32_t tries;
58881408Srandyf 	uint32_t event;
58891408Srandyf 
58901408Srandyf 	ASSERT(bgep->asf_enabled);
58911408Srandyf 
58921408Srandyf 	/* Issues "pause firmware" command and wait for ACK */
58931408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
58941408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
58951408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
58961408Srandyf 
58971408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
58981408Srandyf 	tries = 0;
58991408Srandyf 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
59001408Srandyf 		drv_usecwait(1);
59011408Srandyf 		tries ++;
59021408Srandyf 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
59031408Srandyf 	}
59041408Srandyf 
59051408Srandyf 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
59064588Sml149210 	    BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
59071408Srandyf 
59081408Srandyf 	if (bgep->asf_newhandshake) {
59091408Srandyf 		switch (mode) {
59101408Srandyf 		case BGE_INIT_RESET:
59111408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59124588Sml149210 			    BGE_DRV_STATE_START);
59131408Srandyf 			break;
59141408Srandyf 		case BGE_SHUTDOWN_RESET:
59151408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59164588Sml149210 			    BGE_DRV_STATE_UNLOAD);
59171408Srandyf 			break;
59181408Srandyf 		case BGE_SUSPEND_RESET:
59191408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59204588Sml149210 			    BGE_DRV_STATE_SUSPEND);
59211408Srandyf 			break;
59221408Srandyf 		default:
59231408Srandyf 			break;
59241408Srandyf 		}
59251408Srandyf 	}
59261408Srandyf }
59271408Srandyf 
59281408Srandyf 
59291408Srandyf void
bge_asf_post_reset_old_mode(bge_t * bgep,uint32_t mode)59301408Srandyf bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
59311408Srandyf {
59321408Srandyf 	switch (mode) {
59331408Srandyf 	case BGE_INIT_RESET:
59341408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59354588Sml149210 		    BGE_DRV_STATE_START);
59361408Srandyf 		break;
59371408Srandyf 	case BGE_SHUTDOWN_RESET:
59381408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59394588Sml149210 		    BGE_DRV_STATE_UNLOAD);
59401408Srandyf 		break;
59411408Srandyf 	case BGE_SUSPEND_RESET:
59421408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59434588Sml149210 		    BGE_DRV_STATE_SUSPEND);
59441408Srandyf 		break;
59451408Srandyf 	default:
59461408Srandyf 		break;
59471408Srandyf 	}
59481408Srandyf }
59491408Srandyf 
59501408Srandyf 
59511408Srandyf void
bge_asf_post_reset_new_mode(bge_t * bgep,uint32_t mode)59521408Srandyf bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
59531408Srandyf {
59541408Srandyf 	switch (mode) {
59551408Srandyf 	case BGE_INIT_RESET:
59561408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59574588Sml149210 		    BGE_DRV_STATE_START_DONE);
59581408Srandyf 		break;
59591408Srandyf 	case BGE_SHUTDOWN_RESET:
59601408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
59614588Sml149210 		    BGE_DRV_STATE_UNLOAD_DONE);
59621408Srandyf 		break;
59631408Srandyf 	default:
59641408Srandyf 		break;
59651408Srandyf 	}
59661408Srandyf }
59671408Srandyf 
59681408Srandyf #endif /* BGE_IPMI_ASF */
5969