xref: /onnv-gate/usr/src/uts/common/io/bge/bge_chip2.c (revision 3907:06a70d1289cb)
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 /*
233390Szh199473  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
241369Sdduvall  * Use is subject to license terms.
251369Sdduvall  */
261369Sdduvall 
271369Sdduvall #pragma ident	"%Z%%M%	%I%	%E% SMI"
281369Sdduvall 
292675Szh199473 #include "bge_impl.h"
301369Sdduvall 
311369Sdduvall #define	PIO_ADDR(bgep, offset)	((void *)((caddr_t)(bgep)->io_regs+(offset)))
321369Sdduvall 
331369Sdduvall /*
341369Sdduvall  * Future features ... ?
351369Sdduvall  */
362135Szh199473 #define	BGE_CFG_IO8	1	/* 8/16-bit cfg space BIS/BIC	*/
371369Sdduvall #define	BGE_IND_IO32	0	/* indirect access code		*/
381369Sdduvall #define	BGE_SEE_IO32	1	/* SEEPROM access code		*/
391369Sdduvall #define	BGE_FLASH_IO32	1	/* FLASH access code		*/
401369Sdduvall 
411369Sdduvall /*
421369Sdduvall  * BGE MSI tunable:
431369Sdduvall  *
441369Sdduvall  * By default MSI is enabled on all supported platforms but it is disabled
451369Sdduvall  * for some Broadcom chips due to known MSI hardware issues. Currently MSI
461369Sdduvall  * is enabled only for 5714C A2 and 5715C A2 broadcom chips.
471369Sdduvall  */
481369Sdduvall #if defined(__sparc)
491369Sdduvall boolean_t bge_enable_msi = B_TRUE;
501369Sdduvall #else
511369Sdduvall boolean_t bge_enable_msi = B_FALSE;
521369Sdduvall #endif
531369Sdduvall 
541369Sdduvall /*
55*3907Szh199473  * PCI-X/PCI-E relaxed ordering tunable for OS/Nexus driver
56*3907Szh199473  */
57*3907Szh199473 boolean_t bge_relaxed_ordering = B_TRUE;
58*3907Szh199473 
59*3907Szh199473 /*
601369Sdduvall  * Property names
611369Sdduvall  */
621369Sdduvall static char knownids_propname[] = "bge-known-subsystems";
631369Sdduvall 
641369Sdduvall /*
651369Sdduvall  * Patchable globals:
661369Sdduvall  *
671369Sdduvall  *	bge_autorecover
681369Sdduvall  *		Enables/disables automatic recovery after fault detection
691369Sdduvall  *
701369Sdduvall  *	bge_mlcr_default
711369Sdduvall  *		Value to program into the MLCR; controls the chip's GPIO pins
721369Sdduvall  *
731369Sdduvall  *	bge_dma_{rd,wr}prio
741369Sdduvall  *		Relative priorities of DMA reads & DMA writes respectively.
751369Sdduvall  *		These may each be patched to any value 0-3.  Equal values
761369Sdduvall  *		will give "fair" (round-robin) arbitration for PCI access.
771369Sdduvall  *		Unequal values will give one or the other function priority.
781369Sdduvall  *
791369Sdduvall  *	bge_dma_rwctrl
801369Sdduvall  *		Value to put in the Read/Write DMA control register.  See
811369Sdduvall  *	        the Broadcom PRM for things you can fiddle with in this
821369Sdduvall  *		register ...
831369Sdduvall  *
841369Sdduvall  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
851369Sdduvall  *		Send/receive interrupt coalescing parameters.  Counts are
861369Sdduvall  *		#s of descriptors, ticks are in microseconds.  *norm* values
871369Sdduvall  *		apply between status updates/interrupts; the *intr* values
881369Sdduvall  *		refer to the 'during-interrupt' versions - see the PRM.
891369Sdduvall  *
901369Sdduvall  *		NOTE: these values have been determined by measurement. They
911369Sdduvall  *		differ significantly from the values recommended in the PRM.
921369Sdduvall  */
931369Sdduvall static uint32_t bge_autorecover = 1;
941369Sdduvall static uint32_t bge_mlcr_default = MLCR_DEFAULT;
951369Sdduvall static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
961369Sdduvall 
971369Sdduvall static uint32_t bge_dma_rdprio = 1;
981369Sdduvall static uint32_t bge_dma_wrprio = 0;
991369Sdduvall static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
1001369Sdduvall static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
1011369Sdduvall static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
1021369Sdduvall static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
1031369Sdduvall 
1041369Sdduvall uint32_t bge_rx_ticks_norm = 128;
1051369Sdduvall uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
1061369Sdduvall uint32_t bge_rx_count_norm = 8;
1071369Sdduvall uint32_t bge_tx_count_norm = 128;
1081369Sdduvall 
1091369Sdduvall static uint32_t bge_rx_ticks_intr = 128;
1101369Sdduvall static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
1111369Sdduvall static uint32_t bge_rx_count_intr = 2;
1121369Sdduvall static uint32_t bge_tx_count_intr = 0;
1131369Sdduvall 
1141369Sdduvall /*
1151369Sdduvall  * Memory pool configuration parameters.
1161369Sdduvall  *
1171369Sdduvall  * These are generally specific to each member of the chip family, since
1181369Sdduvall  * each one may have a different memory size/configuration.
1191369Sdduvall  *
1201369Sdduvall  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
1211369Sdduvall  * the driver from programming the various registers; instead they are left
1221369Sdduvall  * at their hardware defaults.  This is the preferred option for later chips
1231369Sdduvall  * (5705+), whereas the older chips *required* these registers to be set,
1241369Sdduvall  * since the h/w default was 0 ;-(
1251369Sdduvall  */
1261369Sdduvall static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
1271369Sdduvall static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
1281369Sdduvall static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
1291369Sdduvall static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
1301369Sdduvall static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
1311369Sdduvall static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
1321369Sdduvall static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
1331369Sdduvall static uint32_t bge_mbuf_pool_len_5721	= 0;
1341369Sdduvall 
1351369Sdduvall /*
1361369Sdduvall  * Various high and low water marks, thresholds, etc ...
1371369Sdduvall  *
1381369Sdduvall  * Note: these are taken from revision 7 of the PRM, and some are different
1391369Sdduvall  * from both the values in earlier PRMs *and* those determined experimentally
1401369Sdduvall  * and used in earlier versions of this driver ...
1411369Sdduvall  */
1421369Sdduvall static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
1431369Sdduvall static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
1441369Sdduvall static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
1451369Sdduvall 
1461369Sdduvall static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
1471369Sdduvall static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
1481369Sdduvall static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
1491369Sdduvall 
1501369Sdduvall static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
1511369Sdduvall static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
1521369Sdduvall static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
1531369Sdduvall 
1541369Sdduvall static uint32_t	bge_watchdog_count	= 1 << 16;
1551369Sdduvall static uint16_t bge_dma_miss_limit	= 20;
1561369Sdduvall 
1571369Sdduvall static uint32_t bge_stop_start_on_sync	= 0;
1581369Sdduvall 
1591369Sdduvall boolean_t bge_jumbo_enable		= B_TRUE;
1601369Sdduvall static uint32_t bge_default_jumbo_size	= BGE_JUMBO_BUFF_SIZE;
1611369Sdduvall 
1621369Sdduvall /*
1631369Sdduvall  * ========== Low-level chip & ring buffer manipulation ==========
1641369Sdduvall  */
1651369Sdduvall 
1661369Sdduvall #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
1671369Sdduvall 
1681369Sdduvall 
1691369Sdduvall /*
1701369Sdduvall  * Config space read-modify-write routines
1711369Sdduvall  */
1721369Sdduvall 
1731369Sdduvall #if	BGE_CFG_IO8
1741369Sdduvall 
1751369Sdduvall /*
1761369Sdduvall  * 8- and 16-bit set/clr operations are not used; all the config registers
1771369Sdduvall  * that we need to do bit-twiddling on are 32 bits wide.  I'll leave the
1781369Sdduvall  * code here, though, in case we ever find that we do want it after all ...
1791369Sdduvall  */
1801369Sdduvall 
1811369Sdduvall static void bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1821369Sdduvall #pragma	inline(bge_cfg_set8)
1831369Sdduvall 
1841369Sdduvall static void
1851369Sdduvall bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
1861369Sdduvall {
1871369Sdduvall 	uint8_t regval;
1881369Sdduvall 
1891369Sdduvall 	BGE_TRACE(("bge_cfg_set8($%p, 0x%lx, 0x%x)",
1901369Sdduvall 		(void *)bgep, regno, bits));
1911369Sdduvall 
1921369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
1931369Sdduvall 
1941369Sdduvall 	BGE_DEBUG(("bge_cfg_set8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
1951369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
1961369Sdduvall 
1971369Sdduvall 	regval |= bits;
1981369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
1991369Sdduvall }
2001369Sdduvall 
2011369Sdduvall static void bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
2021369Sdduvall #pragma	inline(bge_cfg_clr8)
2031369Sdduvall 
2041369Sdduvall static void
2051369Sdduvall bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
2061369Sdduvall {
2071369Sdduvall 	uint8_t regval;
2081369Sdduvall 
2091369Sdduvall 	BGE_TRACE(("bge_cfg_clr8($%p, 0x%lx, 0x%x)",
2101369Sdduvall 		(void *)bgep, regno, bits));
2111369Sdduvall 
2121369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
2131369Sdduvall 
2141369Sdduvall 	BGE_DEBUG(("bge_cfg_clr8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2151369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2161369Sdduvall 
2171369Sdduvall 	regval &= ~bits;
2181369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
2191369Sdduvall }
2201369Sdduvall 
2211369Sdduvall static void bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2221369Sdduvall #pragma	inline(bge_cfg_set16)
2231369Sdduvall 
2241369Sdduvall static void
2251369Sdduvall bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2261369Sdduvall {
2271369Sdduvall 	uint16_t regval;
2281369Sdduvall 
2291369Sdduvall 	BGE_TRACE(("bge_cfg_set16($%p, 0x%lx, 0x%x)",
2301369Sdduvall 		(void *)bgep, regno, bits));
2311369Sdduvall 
2321369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2331369Sdduvall 
2341369Sdduvall 	BGE_DEBUG(("bge_cfg_set16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2351369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2361369Sdduvall 
2371369Sdduvall 	regval |= bits;
2381369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2391369Sdduvall }
2401369Sdduvall 
2411369Sdduvall static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2421369Sdduvall #pragma	inline(bge_cfg_clr16)
2431369Sdduvall 
2441369Sdduvall static void
2451369Sdduvall bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2461369Sdduvall {
2471369Sdduvall 	uint16_t regval;
2481369Sdduvall 
2491369Sdduvall 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
2501369Sdduvall 		(void *)bgep, regno, bits));
2511369Sdduvall 
2521369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2531369Sdduvall 
2541369Sdduvall 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2551369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2561369Sdduvall 
2571369Sdduvall 	regval &= ~bits;
2581369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2591369Sdduvall }
2601369Sdduvall 
2611369Sdduvall #endif	/* BGE_CFG_IO8 */
2621369Sdduvall 
2631369Sdduvall static void bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2641369Sdduvall #pragma	inline(bge_cfg_set32)
2651369Sdduvall 
2661369Sdduvall static void
2671369Sdduvall bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2681369Sdduvall {
2691369Sdduvall 	uint32_t regval;
2701369Sdduvall 
2711369Sdduvall 	BGE_TRACE(("bge_cfg_set32($%p, 0x%lx, 0x%x)",
2721369Sdduvall 		(void *)bgep, regno, bits));
2731369Sdduvall 
2741369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2751369Sdduvall 
2761369Sdduvall 	BGE_DEBUG(("bge_cfg_set32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2771369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2781369Sdduvall 
2791369Sdduvall 	regval |= bits;
2801369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2811369Sdduvall }
2821369Sdduvall 
2831369Sdduvall static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2841369Sdduvall #pragma	inline(bge_cfg_clr32)
2851369Sdduvall 
2861369Sdduvall static void
2871369Sdduvall bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2881369Sdduvall {
2891369Sdduvall 	uint32_t regval;
2901369Sdduvall 
2911369Sdduvall 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
2921369Sdduvall 		(void *)bgep, regno, bits));
2931369Sdduvall 
2941369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2951369Sdduvall 
2961369Sdduvall 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2971369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2981369Sdduvall 
2991369Sdduvall 	regval &= ~bits;
3001369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
3011369Sdduvall }
3021369Sdduvall 
3031369Sdduvall #if	BGE_IND_IO32
3041369Sdduvall 
3051369Sdduvall /*
3061369Sdduvall  * Indirect access to registers & RISC scratchpads, using config space
3071369Sdduvall  * accesses only.
3081369Sdduvall  *
3091369Sdduvall  * This isn't currently used, but someday we might want to use it for
3101369Sdduvall  * restoring the Subsystem Device/Vendor registers (which aren't directly
3111369Sdduvall  * writable in Config Space), or for downloading firmware into the RISCs
3121369Sdduvall  *
3131369Sdduvall  * In any case there are endian issues to be resolved before this code is
3141369Sdduvall  * enabled; the bizarre way that bytes get twisted by this chip AND by
3151369Sdduvall  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
3161369Sdduvall  * it's been thoroughly tested for all access sizes on all supported
3171369Sdduvall  * architectures (SPARC *and* x86!).
3181369Sdduvall  */
3191369Sdduvall static uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
3201369Sdduvall #pragma	inline(bge_ind_get32)
3211369Sdduvall 
3221369Sdduvall static uint32_t
3231369Sdduvall bge_ind_get32(bge_t *bgep, bge_regno_t regno)
3241369Sdduvall {
3251369Sdduvall 	uint32_t val;
3261369Sdduvall 
3271369Sdduvall 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
3281369Sdduvall 
3291369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3301369Sdduvall 
3311369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3321369Sdduvall 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
3331369Sdduvall 
3341369Sdduvall 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
3351369Sdduvall 		(void *)bgep, regno, val));
3361369Sdduvall 
3371369Sdduvall 	return (val);
3381369Sdduvall }
3391369Sdduvall 
3401369Sdduvall static void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
3411369Sdduvall #pragma	inline(bge_ind_put32)
3421369Sdduvall 
3431369Sdduvall static void
3441369Sdduvall bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
3451369Sdduvall {
3461369Sdduvall 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
3471369Sdduvall 		(void *)bgep, regno, val));
3481369Sdduvall 
3491369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3501369Sdduvall 
3511369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3521369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
3531369Sdduvall }
3541369Sdduvall 
3551369Sdduvall #endif	/* BGE_IND_IO32 */
3561369Sdduvall 
3571369Sdduvall #if	BGE_DEBUGGING
3581369Sdduvall 
3591369Sdduvall static void bge_pci_check(bge_t *bgep);
3601369Sdduvall #pragma	no_inline(bge_pci_check)
3611369Sdduvall 
3621369Sdduvall static void
3631369Sdduvall bge_pci_check(bge_t *bgep)
3641369Sdduvall {
3651369Sdduvall 	uint16_t pcistatus;
3661369Sdduvall 
3671369Sdduvall 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
3681369Sdduvall 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
3691369Sdduvall 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
3701369Sdduvall 			(void *)bgep, pcistatus));
3711369Sdduvall }
3721369Sdduvall 
3731369Sdduvall #endif	/* BGE_DEBUGGING */
3741369Sdduvall 
3751369Sdduvall /*
3761369Sdduvall  * Perform first-stage chip (re-)initialisation, using only config-space
3771369Sdduvall  * accesses:
3781369Sdduvall  *
3791369Sdduvall  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
3801369Sdduvall  *   returning the data in the structure pointed to by <idp>.
3811369Sdduvall  * + Configure the target-mode endianness (swap) options.
3821369Sdduvall  * + Disable interrupts and enable Memory Space accesses.
3831369Sdduvall  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
3841369Sdduvall  *
3851369Sdduvall  * This sequence is adapted from Broadcom document 570X-PG102-R,
3861369Sdduvall  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
3871369Sdduvall  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
3881369Sdduvall  *
3891369Sdduvall  * This function MUST be called before any non-config-space accesses
3901369Sdduvall  * are made; on this first call <enable_dma> is B_FALSE, and it
3911369Sdduvall  * effectively performs steps 3-1(!) of the initialisation sequence
3921369Sdduvall  * (the rest are not required but should be harmless).
3931369Sdduvall  *
3942135Szh199473  * It MUST also be called after a chip reset, as this disables
3951369Sdduvall  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
3961369Sdduvall  * it is effectively performing steps 6-8.
3971369Sdduvall  */
3981369Sdduvall void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
3991369Sdduvall #pragma	no_inline(bge_chip_cfg_init)
4001369Sdduvall 
4011369Sdduvall void
4021369Sdduvall bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
4031369Sdduvall {
4041369Sdduvall 	ddi_acc_handle_t handle;
4051369Sdduvall 	uint16_t command;
4061369Sdduvall 	uint32_t mhcr;
4071369Sdduvall 	uint16_t value16;
4081369Sdduvall 	int i;
4091369Sdduvall 
4101369Sdduvall 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
4111369Sdduvall 		(void *)bgep, (void *)cidp, enable_dma));
4121369Sdduvall 
4131369Sdduvall 	/*
4141369Sdduvall 	 * Step 3: save PCI cache line size and subsystem vendor ID
4151369Sdduvall 	 *
4161369Sdduvall 	 * Read all the config-space registers that characterise the
4171369Sdduvall 	 * chip, specifically vendor/device/revision/subsystem vendor
4181369Sdduvall 	 * and subsystem device id.  We expect (but don't check) that
4191369Sdduvall 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
4201369Sdduvall 	 *
4212135Szh199473 	 * Also save all bus-transaction related registers (cache-line
4221369Sdduvall 	 * size, bus-grant/latency parameters, etc).  Some of these are
4231369Sdduvall 	 * cleared by reset, so we'll have to restore them later.  This
4241369Sdduvall 	 * comes from the Broadcom document 570X-PG102-R ...
4251369Sdduvall 	 *
4261369Sdduvall 	 * Note: Broadcom document 570X-PG102-R seems to be in error
4271369Sdduvall 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
4281369Sdduvall 	 * Subsystem (Device) ID registers, which are the opposite way
4291369Sdduvall 	 * round according to the PCI standard.  For good measure, we
4301369Sdduvall 	 * save/restore both anyway.
4311369Sdduvall 	 */
4321369Sdduvall 	handle = bgep->cfg_handle;
4331369Sdduvall 
4341369Sdduvall 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
4351369Sdduvall 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
4361369Sdduvall 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
4371369Sdduvall 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
4381369Sdduvall 
4391369Sdduvall 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
4401369Sdduvall 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
4411369Sdduvall 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
4421369Sdduvall 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
4431369Sdduvall 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
4441369Sdduvall 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
4451369Sdduvall 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
4461369Sdduvall 
4471369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
4481369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
4491369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
4501369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
4511369Sdduvall 		cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
4521369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
4531369Sdduvall 		cidp->vendor, cidp->device, cidp->revision));
4541369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
4551369Sdduvall 		cidp->subven, cidp->subdev, cidp->asic_rev));
4561369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
4571369Sdduvall 		cidp->clsize, cidp->latency, cidp->command));
4581369Sdduvall 
4591369Sdduvall 	/*
4601369Sdduvall 	 * Step 2 (also step 6): disable and clear interrupts.
4611369Sdduvall 	 * Steps 11-13: configure PIO endianness options, and enable
4621369Sdduvall 	 * indirect register access.  We'll also select any other
4632135Szh199473 	 * options controlled by the MHCR (e.g. tagged status, mask
4641369Sdduvall 	 * interrupt mode) at this stage ...
4651369Sdduvall 	 *
4661369Sdduvall 	 * Note: internally, the chip is 64-bit and BIG-endian, but
4671369Sdduvall 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
4681369Sdduvall 	 * it normally swaps bytes around at the PCI interface.
4691369Sdduvall 	 * However, the PCI host bridge on SPARC systems normally
4701369Sdduvall 	 * swaps the byte lanes around too, since SPARCs are also
4711369Sdduvall 	 * BIG-endian.  So it turns out that on SPARC, the right
4721369Sdduvall 	 * option is to tell the chip to swap (and the host bridge
4731369Sdduvall 	 * will swap back again), whereas on x86 we ask the chip
4741369Sdduvall 	 * NOT to swap, so the natural little-endianness of the
4751369Sdduvall 	 * PCI bus is assumed.  Then the only thing that doesn't
4761369Sdduvall 	 * automatically work right is access to an 8-byte register
4771369Sdduvall 	 * by a little-endian host; but we don't want to set the
4781369Sdduvall 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
4791369Sdduvall 	 * accesses don't go where expected ;-(  So we live with
4801369Sdduvall 	 * that, and perform word-swaps in software in the few cases
4811369Sdduvall 	 * where a chip register is defined as an 8-byte value --
4821369Sdduvall 	 * see the code below for details ...
4831369Sdduvall 	 *
4841369Sdduvall 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
4851369Sdduvall 	 * very clear in the register description in the PRM, but
4861369Sdduvall 	 * Broadcom document 570X-PG104-R page 248 explains a little
4871369Sdduvall 	 * more (under "Broadcom Mask Mode").  The bit changes the way
4881369Sdduvall 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
4891369Sdduvall 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
4901369Sdduvall 	 * way as the 5700 did, which isn't very convenient.  Setting
4911369Sdduvall 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
4921369Sdduvall 	 * bit do just what its name says -- MASK the PCI #INTA output
4931369Sdduvall 	 * (i.e. deassert the signal at the pin) leaving all internal
4941369Sdduvall 	 * state unchanged.  This is much more convenient for our
4951369Sdduvall 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
4961369Sdduvall 	 *
4971369Sdduvall 	 * Note: the inconvenient semantics of the interrupt mailbox
4981369Sdduvall 	 * (nonzero disables and acknowledges/clears the interrupt,
4991369Sdduvall 	 * zero enables AND CLEARS it) would make race conditions
5001369Sdduvall 	 * likely in the interrupt handler:
5011369Sdduvall 	 *
5021369Sdduvall 	 * (1)	acknowledge & disable interrupts
5031369Sdduvall 	 * (2)	while (more to do)
5041369Sdduvall 	 * 		process packets
5051369Sdduvall 	 * (3)	enable interrupts -- also clears pending
5061369Sdduvall 	 *
5071369Sdduvall 	 * If the chip received more packets and internally generated
5081369Sdduvall 	 * an interrupt between the check at (2) and the mbox write
5091369Sdduvall 	 * at (3), this interrupt would be lost :-(
5101369Sdduvall 	 *
5111369Sdduvall 	 * The best way to avoid this is to use TAGGED STATUS mode,
5121369Sdduvall 	 * where the chip includes a unique tag in each status block
5131369Sdduvall 	 * update, and the host, when re-enabling interrupts, passes
5141369Sdduvall 	 * the last tag it saw back to the chip; then the chip can
5151369Sdduvall 	 * see whether the host is truly up to date, and regenerate
5161369Sdduvall 	 * its interrupt if not.
5171369Sdduvall 	 */
5181369Sdduvall 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
5191369Sdduvall 		MHCR_ENABLE_TAGGED_STATUS_MODE |
5201369Sdduvall 		MHCR_MASK_INTERRUPT_MODE |
5211369Sdduvall 		MHCR_CLEAR_INTERRUPT_INTA;
5221369Sdduvall 
5231369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
5241369Sdduvall 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
5251369Sdduvall 
5261369Sdduvall #ifdef	_BIG_ENDIAN
5271369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
5281369Sdduvall #endif	/* _BIG_ENDIAN */
5291369Sdduvall 
5301369Sdduvall 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
5311369Sdduvall 
5321408Srandyf #ifdef BGE_IPMI_ASF
5331408Srandyf 	bgep->asf_wordswapped = B_FALSE;
5341408Srandyf #endif
5351369Sdduvall 	/*
5361369Sdduvall 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
5371369Sdduvall 	 *			 Disable Memory Write/Invalidate
5381369Sdduvall 	 *			 Enable or disable Bus Mastering
5391369Sdduvall 	 *
5401369Sdduvall 	 * Note that all other bits are taken from the original value saved
5411369Sdduvall 	 * the first time through here, rather than from the current register
5421369Sdduvall 	 * value, 'cos that will have been cleared by a soft RESET since.
5431369Sdduvall 	 * In this way we preserve the OBP/nexus-parent's preferred settings
5441369Sdduvall 	 * of the parity-error and system-error enable bits across multiple
5451369Sdduvall 	 * chip RESETs.
5461369Sdduvall 	 */
5471369Sdduvall 	command = bgep->chipid.command | PCI_COMM_MAE;
5481369Sdduvall 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
5491369Sdduvall 	if (enable_dma)
5501369Sdduvall 		command |= PCI_COMM_ME;
5511369Sdduvall 	/*
5521369Sdduvall 	 * on BCM5714 revision A0, false parity error gets generated
5532135Szh199473 	 * due to a logic bug. Provide a workaround by disabling parity
5541369Sdduvall 	 * error.
5551369Sdduvall 	 */
5561369Sdduvall 	if (((cidp->device == DEVICE_ID_5714C) ||
5571369Sdduvall 	    (cidp->device == DEVICE_ID_5714S)) &&
5581369Sdduvall 	    (cidp->revision == REVISION_ID_5714_A0)) {
5591369Sdduvall 		command &= ~PCI_COMM_PARITY_DETECT;
5601369Sdduvall 	}
5611369Sdduvall 	pci_config_put16(handle, PCI_CONF_COMM, command);
5621369Sdduvall 
5631369Sdduvall 	/*
5641369Sdduvall 	 * On some PCI-E device, there were instances when
5651369Sdduvall 	 * the device was still link training.
5661369Sdduvall 	 */
5671369Sdduvall 	if (bgep->chipid.pci_type == BGE_PCI_E) {
5681369Sdduvall 		i = 0;
5691369Sdduvall 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
5701369Sdduvall 		while ((value16 != command) && (i < 100)) {
5711369Sdduvall 			drv_usecwait(200);
5721369Sdduvall 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
5731369Sdduvall 			++i;
5741369Sdduvall 		}
5751369Sdduvall 	}
5761369Sdduvall 
5771369Sdduvall 	/*
5781369Sdduvall 	 * Clear any remaining error status bits
5791369Sdduvall 	 */
5801369Sdduvall 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
5811369Sdduvall 
5821369Sdduvall 	/*
5832073Svivek 	 * Do following if and only if the device is NOT BCM5714C OR
5842073Svivek 	 * BCM5715C
5851369Sdduvall 	 */
5862073Svivek 	if (!((cidp->device == DEVICE_ID_5714C) ||
5872073Svivek 		(cidp->device == DEVICE_ID_5715C))) {
5882073Svivek 		/*
5892073Svivek 		 * Make sure these indirect-access registers are sane
5902073Svivek 		 * rather than random after power-up or reset
5912073Svivek 		 */
5922073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
5932073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
5942073Svivek 	}
5952135Szh199473 	/*
5962135Szh199473 	 * Step 8: Disable PCI-X/PCI-E Relaxed Ordering
5972135Szh199473 	 */
5982135Szh199473 	bge_cfg_clr16(bgep, PCIX_CONF_COMM, PCIX_COMM_RELAXED);
5992135Szh199473 
6002135Szh199473 	if (cidp->pci_type == BGE_PCI_E)
6012135Szh199473 		bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL,
6022135Szh199473 				DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
6031369Sdduvall }
6041369Sdduvall 
6051369Sdduvall #ifdef __amd64
6061369Sdduvall /*
6071369Sdduvall  * Distinguish CPU types
6081369Sdduvall  *
6091369Sdduvall  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
6101369Sdduvall  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
6111369Sdduvall  * for PCI-Express based network interface card. This is the work-around
6121369Sdduvall  * for those nics.
6131369Sdduvall  */
6141369Sdduvall static boolean_t bge_get_em64t_type(void);
6151369Sdduvall #pragma	inline(bge_get_em64t_type)
6161369Sdduvall 
6171369Sdduvall static boolean_t
6181369Sdduvall bge_get_em64t_type(void)
6191369Sdduvall {
6201369Sdduvall 
6211369Sdduvall 	return (x86_vendor == X86_VENDOR_Intel);
6221369Sdduvall }
6231369Sdduvall #endif
6241369Sdduvall 
6251369Sdduvall /*
6261369Sdduvall  * Operating register get/set access routines
6271369Sdduvall  */
6281369Sdduvall 
6291369Sdduvall uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
6301369Sdduvall #pragma	inline(bge_reg_get32)
6311369Sdduvall 
6321369Sdduvall uint32_t
6331369Sdduvall bge_reg_get32(bge_t *bgep, bge_regno_t regno)
6341369Sdduvall {
6351369Sdduvall 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
6361369Sdduvall 		(void *)bgep, regno));
6371369Sdduvall 
6381369Sdduvall 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
6391369Sdduvall }
6401369Sdduvall 
6411369Sdduvall void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
6421369Sdduvall #pragma	inline(bge_reg_put32)
6431369Sdduvall 
6441369Sdduvall void
6451369Sdduvall bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
6461369Sdduvall {
6471369Sdduvall 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
6481369Sdduvall 		(void *)bgep, regno, data));
6491369Sdduvall 
6501369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6511369Sdduvall 	BGE_PCICHK(bgep);
6521369Sdduvall }
6531369Sdduvall 
6541369Sdduvall void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6551369Sdduvall #pragma	inline(bge_reg_set32)
6561369Sdduvall 
6571369Sdduvall void
6581369Sdduvall bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6591369Sdduvall {
6601369Sdduvall 	uint32_t regval;
6611369Sdduvall 
6621369Sdduvall 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
6631369Sdduvall 		(void *)bgep, regno, bits));
6641369Sdduvall 
6651369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6661369Sdduvall 	regval |= bits;
6671369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6681369Sdduvall }
6691369Sdduvall 
6701369Sdduvall void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6711369Sdduvall #pragma	inline(bge_reg_clr32)
6721369Sdduvall 
6731369Sdduvall void
6741369Sdduvall bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6751369Sdduvall {
6761369Sdduvall 	uint32_t regval;
6771369Sdduvall 
6781369Sdduvall 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
6791369Sdduvall 		(void *)bgep, regno, bits));
6801369Sdduvall 
6811369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6821369Sdduvall 	regval &= ~bits;
6831369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6841369Sdduvall }
6851369Sdduvall 
6861369Sdduvall static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
6871369Sdduvall #pragma	inline(bge_reg_get64)
6881369Sdduvall 
6891369Sdduvall static uint64_t
6901369Sdduvall bge_reg_get64(bge_t *bgep, bge_regno_t regno)
6911369Sdduvall {
6921369Sdduvall 	uint64_t regval;
6931369Sdduvall 
6941369Sdduvall #ifdef	__amd64
6951369Sdduvall 	if (bge_get_em64t_type()) {
6961369Sdduvall 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
6971369Sdduvall 		regval <<= 32;
6981369Sdduvall 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
6991369Sdduvall 	} else {
7001369Sdduvall 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
7011369Sdduvall 	}
7021369Sdduvall #else
7031369Sdduvall 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
7041369Sdduvall #endif
7051369Sdduvall 
7061369Sdduvall #ifdef	_LITTLE_ENDIAN
7071369Sdduvall 	regval = (regval >> 32) | (regval << 32);
7081369Sdduvall #endif	/* _LITTLE_ENDIAN */
7091369Sdduvall 
7101369Sdduvall 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
7111369Sdduvall 		(void *)bgep, regno, regval));
7121369Sdduvall 
7131369Sdduvall 	return (regval);
7141369Sdduvall }
7151369Sdduvall 
7161369Sdduvall static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
7171369Sdduvall #pragma	inline(bge_reg_put64)
7181369Sdduvall 
7191369Sdduvall static void
7201369Sdduvall bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
7211369Sdduvall {
7221369Sdduvall 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
7231369Sdduvall 		(void *)bgep, regno, data));
7241369Sdduvall 
7251369Sdduvall #ifdef	_LITTLE_ENDIAN
7261369Sdduvall 	data = ((data >> 32) | (data << 32));
7271369Sdduvall #endif	/* _LITTLE_ENDIAN */
7281369Sdduvall 
7291369Sdduvall #ifdef	__amd64
7301369Sdduvall 	if (bge_get_em64t_type()) {
7311369Sdduvall 		ddi_put32(bgep->io_handle,
7321369Sdduvall 			PIO_ADDR(bgep, regno), (uint32_t)data);
7331369Sdduvall 		BGE_PCICHK(bgep);
7341369Sdduvall 		ddi_put32(bgep->io_handle,
7351369Sdduvall 			PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
7361369Sdduvall 
7371369Sdduvall 	} else {
7381369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7391369Sdduvall 	}
7401369Sdduvall #else
7411369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7421369Sdduvall #endif
7431369Sdduvall 
7441369Sdduvall 	BGE_PCICHK(bgep);
7451369Sdduvall }
7461369Sdduvall 
7471369Sdduvall /*
7481369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
7491369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
7501369Sdduvall  */
7511369Sdduvall static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
7521369Sdduvall #pragma	inline(bge_reg_putrcb)
7531369Sdduvall 
7541369Sdduvall static void
7551369Sdduvall bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
7561369Sdduvall {
7571369Sdduvall 	uint64_t *p;
7581369Sdduvall 
7591369Sdduvall 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
7601369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
7611369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
7621369Sdduvall 
7631369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
7641369Sdduvall 
7651369Sdduvall 	p = (void *)rcbp;
7661369Sdduvall 	bge_reg_put64(bgep, addr, *p++);
7671369Sdduvall 	bge_reg_put64(bgep, addr+8, *p);
7681369Sdduvall }
7691369Sdduvall 
7701369Sdduvall void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
7711369Sdduvall #pragma	inline(bge_mbx_put)
7721369Sdduvall 
7731369Sdduvall void
7741369Sdduvall bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
7751369Sdduvall {
7761369Sdduvall 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
7771369Sdduvall 		(void *)bgep, regno, data));
7781369Sdduvall 
7791369Sdduvall 	/*
7801369Sdduvall 	 * Mailbox registers are nominally 64 bits on the 5701, but
7811369Sdduvall 	 * the MSW isn't used.  On the 5703, they're only 32 bits
7821369Sdduvall 	 * anyway.  So here we just write the lower(!) 32 bits -
7831369Sdduvall 	 * remembering that the chip is big-endian, even though the
7841369Sdduvall 	 * PCI bus is little-endian ...
7851369Sdduvall 	 */
7861369Sdduvall #ifdef	_BIG_ENDIAN
7871369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
7881369Sdduvall #else
7891369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
7901369Sdduvall #endif	/* _BIG_ENDIAN */
7911369Sdduvall 	BGE_PCICHK(bgep);
7921369Sdduvall }
7931369Sdduvall 
7941369Sdduvall #if	BGE_DEBUGGING
7951369Sdduvall 
7961369Sdduvall void bge_led_mark(bge_t *bgep);
7971369Sdduvall #pragma	no_inline(bge_led_mark)
7981369Sdduvall 
7991369Sdduvall void
8001369Sdduvall bge_led_mark(bge_t *bgep)
8011369Sdduvall {
8021369Sdduvall 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
8031369Sdduvall 			    LED_CONTROL_1000MBPS_LED |
8041369Sdduvall 			    LED_CONTROL_100MBPS_LED |
8051369Sdduvall 			    LED_CONTROL_10MBPS_LED;
8061369Sdduvall 
8071369Sdduvall 	/*
8081369Sdduvall 	 * Blink all three LINK LEDs on simultaneously, then all off,
8091369Sdduvall 	 * then restore to automatic hardware control.  This is used
8101369Sdduvall 	 * in laboratory testing to trigger a logic analyser or scope.
8111369Sdduvall 	 */
8121369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8131369Sdduvall 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
8141369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8151369Sdduvall 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
8161369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8171369Sdduvall }
8181369Sdduvall 
8191369Sdduvall #endif	/* BGE_DEBUGGING */
8201369Sdduvall 
8211369Sdduvall /*
8221369Sdduvall  * NIC on-chip memory access routines
8231369Sdduvall  *
8241369Sdduvall  * Only 32K of NIC memory is visible at a time, controlled by the
8251369Sdduvall  * Memory Window Base Address Register (in PCI config space).  Once
8261369Sdduvall  * this is set, the 32K region of NIC-local memory that it refers
8271369Sdduvall  * to can be directly addressed in the upper 32K of the 64K of PCI
8281369Sdduvall  * memory space used for the device.
8291369Sdduvall  */
8301369Sdduvall 
8311369Sdduvall static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
8321369Sdduvall #pragma	inline(bge_nic_setwin)
8331369Sdduvall 
8341369Sdduvall static void
8351369Sdduvall bge_nic_setwin(bge_t *bgep, bge_regno_t base)
8361369Sdduvall {
8372073Svivek 	chip_id_t *cidp;
8382073Svivek 
8391369Sdduvall 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
8401369Sdduvall 		(void *)bgep, base));
8411369Sdduvall 
8421369Sdduvall 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
8432073Svivek 
8442073Svivek 	/*
8452073Svivek 	 * Don't do repeated zero data writes,
8462073Svivek 	 * if the device is BCM5714C/15C.
8472073Svivek 	 */
8482073Svivek 	cidp = &bgep->chipid;
8492073Svivek 	if ((cidp->device == DEVICE_ID_5714C) ||
8502073Svivek 		(cidp->device == DEVICE_ID_5715C)) {
8512073Svivek 		if (bgep->lastWriteZeroData && (base == (bge_regno_t)0))
8522073Svivek 			return;
8532073Svivek 		/* Adjust lastWriteZeroData */
8542073Svivek 		bgep->lastWriteZeroData = ((base == (bge_regno_t)0) ?
8552073Svivek 			B_TRUE : B_FALSE);
8562073Svivek 	}
8571369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
8581369Sdduvall }
8591369Sdduvall 
8601369Sdduvall 
8611369Sdduvall static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
8621369Sdduvall #pragma	inline(bge_nic_get32)
8631369Sdduvall 
8641369Sdduvall static uint32_t
8651369Sdduvall bge_nic_get32(bge_t *bgep, bge_regno_t addr)
8661369Sdduvall {
8671369Sdduvall 	uint32_t data;
8681369Sdduvall 
8691408Srandyf #ifdef BGE_IPMI_ASF
8701408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8711408Srandyf 		/* workaround for word swap error */
8721408Srandyf 		if (addr & 4)
8731408Srandyf 			addr = addr - 4;
8741408Srandyf 		else
8751408Srandyf 			addr = addr + 4;
8761408Srandyf 	}
8771408Srandyf #endif
8781408Srandyf 
8791369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
8801369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
8811369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
8821369Sdduvall 
8831369Sdduvall 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
8841369Sdduvall 
8851369Sdduvall 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
8861369Sdduvall 		(void *)bgep, addr, data));
8871369Sdduvall 
8881369Sdduvall 	return (data);
8891369Sdduvall }
8901369Sdduvall 
8911408Srandyf void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
8921408Srandyf #pragma inline(bge_nic_put32)
8931408Srandyf 
8941408Srandyf void
8951369Sdduvall bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
8961369Sdduvall {
8971369Sdduvall 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
8981369Sdduvall 		(void *)bgep, addr, data));
8991369Sdduvall 
9001408Srandyf #ifdef BGE_IPMI_ASF
9011408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
9021408Srandyf 		/* workaround for word swap error */
9031408Srandyf 		if (addr & 4)
9041408Srandyf 			addr = addr - 4;
9051408Srandyf 		else
9061408Srandyf 			addr = addr + 4;
9071408Srandyf 	}
9081408Srandyf #endif
9091408Srandyf 
9101369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9111369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9121369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9131369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9141369Sdduvall 	BGE_PCICHK(bgep);
9151369Sdduvall }
9161369Sdduvall 
9171369Sdduvall 
9181369Sdduvall static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
9191369Sdduvall #pragma	inline(bge_nic_get64)
9201369Sdduvall 
9211369Sdduvall static uint64_t
9221369Sdduvall bge_nic_get64(bge_t *bgep, bge_regno_t addr)
9231369Sdduvall {
9241369Sdduvall 	uint64_t data;
9251369Sdduvall 
9261369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9271369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9281369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9291369Sdduvall 
9301369Sdduvall #ifdef	__amd64
9311369Sdduvall 		if (bge_get_em64t_type()) {
9321369Sdduvall 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
9331369Sdduvall 			data <<= 32;
9341369Sdduvall 			data |= ddi_get32(bgep->io_handle,
9351369Sdduvall 				PIO_ADDR(bgep, addr + 4));
9361369Sdduvall 		} else {
9371369Sdduvall 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9381369Sdduvall 		}
9391369Sdduvall #else
9401369Sdduvall 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9411369Sdduvall #endif
9421369Sdduvall 
9431369Sdduvall 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
9441369Sdduvall 		(void *)bgep, addr, data));
9451369Sdduvall 
9461369Sdduvall 	return (data);
9471369Sdduvall }
9481369Sdduvall 
9491369Sdduvall static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
9501369Sdduvall #pragma	inline(bge_nic_put64)
9511369Sdduvall 
9521369Sdduvall static void
9531369Sdduvall bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
9541369Sdduvall {
9551369Sdduvall 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
9561369Sdduvall 		(void *)bgep, addr, data));
9571369Sdduvall 
9581369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9591369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9601369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9611369Sdduvall 
9621369Sdduvall #ifdef	__amd64
9631369Sdduvall 	if (bge_get_em64t_type()) {
9641369Sdduvall 		ddi_put32(bgep->io_handle,
9651369Sdduvall 			PIO_ADDR(bgep, addr), (uint32_t)data);
9661369Sdduvall 		BGE_PCICHK(bgep);
9671369Sdduvall 		ddi_put32(bgep->io_handle,
9681369Sdduvall 			PIO_ADDR(bgep, addr + 4), (uint32_t)(data >> 32));
9691369Sdduvall 	} else {
9701369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9711369Sdduvall 	}
9721369Sdduvall #else
9731369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9741369Sdduvall #endif
9751369Sdduvall 
9761369Sdduvall 	BGE_PCICHK(bgep);
9771369Sdduvall }
9781369Sdduvall 
9791369Sdduvall /*
9801369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
9811369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
9821369Sdduvall  */
9831369Sdduvall static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
9841369Sdduvall #pragma	inline(bge_nic_putrcb)
9851369Sdduvall 
9861369Sdduvall static void
9871369Sdduvall bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
9881369Sdduvall {
9891369Sdduvall 	uint64_t *p;
9901369Sdduvall 
9911369Sdduvall 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
9921369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
9931369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
9941369Sdduvall 
9951369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
9961369Sdduvall 
9971369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9981369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9991369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10001369Sdduvall 
10011369Sdduvall 	p = (void *)rcbp;
10021369Sdduvall #ifdef	__amd64
10031369Sdduvall 	if (bge_get_em64t_type()) {
10041369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
10051369Sdduvall 			(uint32_t)(*p));
10061369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
10071369Sdduvall 			(uint32_t)(*p >> 32));
10081369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
10091369Sdduvall 			(uint32_t)(*(p + 1)));
10101369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
10111369Sdduvall 			(uint32_t)(*p >> 32));
10121369Sdduvall 
10131369Sdduvall 	} else {
10141369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10151369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
10161369Sdduvall 	}
10171369Sdduvall #else
10181369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10191369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
10201369Sdduvall #endif
10211369Sdduvall 
10221369Sdduvall 	BGE_PCICHK(bgep);
10231369Sdduvall }
10241369Sdduvall 
10251369Sdduvall static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
10261369Sdduvall #pragma	inline(bge_nic_zero)
10271369Sdduvall 
10281369Sdduvall static void
10291369Sdduvall bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
10301369Sdduvall {
10311369Sdduvall 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
10321369Sdduvall 		(void *)bgep, addr, nbytes));
10331369Sdduvall 
10341369Sdduvall 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
10351369Sdduvall 		((addr+nbytes) & ~MWBAR_GRANULE_MASK));
10361369Sdduvall 
10371369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10381369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10391369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10401369Sdduvall 
10411369Sdduvall 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
10421369Sdduvall 		nbytes, 1, DDI_DATA_SZ08_ACC);
10431369Sdduvall 	BGE_PCICHK(bgep);
10441369Sdduvall }
10451369Sdduvall 
10461369Sdduvall /*
10471369Sdduvall  * MII (PHY) register get/set access routines
10481369Sdduvall  *
10491369Sdduvall  * These use the chip's MII auto-access method, controlled by the
10501369Sdduvall  * MII Communication register at 0x044c, so the CPU doesn't have
10511369Sdduvall  * to fiddle with the individual bits.
10521369Sdduvall  */
10531369Sdduvall 
10541369Sdduvall #undef	BGE_DBG
10551369Sdduvall #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
10561369Sdduvall 
10571369Sdduvall static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
10581369Sdduvall 				uint16_t data, uint32_t cmd);
10591369Sdduvall #pragma	no_inline(bge_mii_access)
10601369Sdduvall 
10611369Sdduvall static uint16_t
10621369Sdduvall bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
10631369Sdduvall {
10641369Sdduvall 	uint32_t timeout;
10651369Sdduvall 	uint32_t regval1;
10661369Sdduvall 	uint32_t regval2;
10671369Sdduvall 
10681369Sdduvall 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
10691369Sdduvall 		(void *)bgep, regno, data, cmd));
10701369Sdduvall 
10711369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
10721369Sdduvall 
10731369Sdduvall 	/*
10741369Sdduvall 	 * Assemble the command ...
10751369Sdduvall 	 */
10761369Sdduvall 	cmd |= data << MI_COMMS_DATA_SHIFT;
10771369Sdduvall 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
10781369Sdduvall 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
10791369Sdduvall 	cmd |= MI_COMMS_START;
10801369Sdduvall 
10811369Sdduvall 	/*
10821369Sdduvall 	 * Wait for any command already in progress ...
10831369Sdduvall 	 *
10841369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
10851369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
10861369Sdduvall 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
10871369Sdduvall 	 * bit set here -- it seems that the chip can initiate MII
10881369Sdduvall 	 * accesses internally, even with polling OFF.
10891369Sdduvall 	 */
10901369Sdduvall 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
10911865Sdilpreet 	for (timeout = 100; ; ) {
10921369Sdduvall 		if ((regval2 & MI_COMMS_START) == 0) {
10931369Sdduvall 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
10941369Sdduvall 			break;
10951369Sdduvall 		}
10961369Sdduvall 		if (--timeout == 0)
10971369Sdduvall 			break;
10981369Sdduvall 		drv_usecwait(10);
10991369Sdduvall 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11001369Sdduvall 	}
11011369Sdduvall 
11021865Sdilpreet 	if (timeout == 0)
11031865Sdilpreet 		return ((uint16_t)~0u);
11041865Sdilpreet 
11051865Sdilpreet 	if (timeout != 100)
11061369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11071369Sdduvall 			"MI_COMMS_START set for %d us; 0x%x->0x%x",
11081865Sdilpreet 			cmd, 10*(100-timeout), regval1, regval2));
11091369Sdduvall 
11101369Sdduvall 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11111369Sdduvall 	for (timeout = 1000; ; ) {
11121369Sdduvall 		if ((regval1 & MI_COMMS_START) == 0)
11131369Sdduvall 			break;
11141369Sdduvall 		if (--timeout == 0)
11151369Sdduvall 			break;
11161369Sdduvall 		drv_usecwait(10);
11171369Sdduvall 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11181369Sdduvall 	}
11191369Sdduvall 
11201369Sdduvall 	/*
11211369Sdduvall 	 * Drop out early if the READ FAILED bit is set -- this chip
11221369Sdduvall 	 * could be a 5703/4S, with a SerDes instead of a PHY!
11231369Sdduvall 	 */
11241369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11251369Sdduvall 		return ((uint16_t)~0u);
11261369Sdduvall 
11271369Sdduvall 	if (timeout == 0)
11281369Sdduvall 		return ((uint16_t)~0u);
11291369Sdduvall 
11301369Sdduvall 	/*
11311369Sdduvall 	 * The PRM says to wait 5us after seeing the START bit clear
11321369Sdduvall 	 * and then re-read the register to get the final value of the
11331369Sdduvall 	 * data field, in order to avoid a race condition where the
11341369Sdduvall 	 * START bit is clear but the data field isn't yet valid.
11351369Sdduvall 	 *
11361369Sdduvall 	 * Note: we don't actually seem to be encounter this race;
11371369Sdduvall 	 * except when the START bit is seen set again (see below),
11381369Sdduvall 	 * the data field doesn't change during this 5us interval.
11391369Sdduvall 	 */
11401369Sdduvall 	drv_usecwait(5);
11411369Sdduvall 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11421369Sdduvall 
11431369Sdduvall 	/*
11441369Sdduvall 	 * Unfortunately, when following the PRMs instructions above,
11451369Sdduvall 	 * we have occasionally seen the START bit set again(!) in the
11461369Sdduvall 	 * value read after the 5us delay. This seems to be due to the
11471369Sdduvall 	 * chip autonomously starting another MII access internally.
11481369Sdduvall 	 * In such cases, the command/data/etc fields relate to the
11491369Sdduvall 	 * internal command, rather than the one that we thought had
11501369Sdduvall 	 * just finished.  So in this case, we fall back to returning
11511369Sdduvall 	 * the data from the original read that showed START clear.
11521369Sdduvall 	 */
11531369Sdduvall 	if (regval2 & MI_COMMS_START) {
11541369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11551369Sdduvall 			"MI_COMMS_START set after transaction; 0x%x->0x%x",
11561369Sdduvall 			cmd, regval1, regval2));
11571369Sdduvall 		regval2 = regval1;
11581369Sdduvall 	}
11591369Sdduvall 
11601369Sdduvall 	if (regval2 & MI_COMMS_START)
11611369Sdduvall 		return ((uint16_t)~0u);
11621369Sdduvall 
11631369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11641369Sdduvall 		return ((uint16_t)~0u);
11651369Sdduvall 
11661369Sdduvall 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
11671369Sdduvall }
11681369Sdduvall 
11691369Sdduvall uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
11701369Sdduvall #pragma	no_inline(bge_mii_get16)
11711369Sdduvall 
11721369Sdduvall uint16_t
11731369Sdduvall bge_mii_get16(bge_t *bgep, bge_regno_t regno)
11741369Sdduvall {
11751369Sdduvall 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
11761369Sdduvall 		(void *)bgep, regno));
11771369Sdduvall 
11781369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11791369Sdduvall 
11801369Sdduvall 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
11811369Sdduvall }
11821369Sdduvall 
11831369Sdduvall void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
11841369Sdduvall #pragma	no_inline(bge_mii_put16)
11851369Sdduvall 
11861369Sdduvall void
11871369Sdduvall bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
11881369Sdduvall {
11891369Sdduvall 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
11901369Sdduvall 		(void *)bgep, regno, data));
11911369Sdduvall 
11921369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11931369Sdduvall 
11941369Sdduvall 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
11951369Sdduvall }
11961369Sdduvall 
11971369Sdduvall #undef	BGE_DBG
11981369Sdduvall #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
11991369Sdduvall 
12001369Sdduvall #if	BGE_SEE_IO32 || BGE_FLASH_IO32
12011369Sdduvall 
12021369Sdduvall /*
12031369Sdduvall  * Basic SEEPROM get/set access routine
12041369Sdduvall  *
12051369Sdduvall  * This uses the chip's SEEPROM auto-access method, controlled by the
12061369Sdduvall  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
12071369Sdduvall  * doesn't have to fiddle with the individual bits.
12081369Sdduvall  *
12091369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
12101369Sdduvall  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
12111369Sdduvall  *
12121369Sdduvall  * Return value:
12131369Sdduvall  *	0 on success,
12141369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
12151369Sdduvall  *	EPROTO on other h/w or s/w errors.
12161369Sdduvall  *
12171369Sdduvall  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
12181369Sdduvall  * from a (successful) SEEPROM_ACCESS_READ.
12191369Sdduvall  */
12201369Sdduvall static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
12211369Sdduvall 				uint32_t *dp);
12221369Sdduvall #pragma	no_inline(bge_seeprom_access)
12231369Sdduvall 
12241369Sdduvall static int
12251369Sdduvall bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
12261369Sdduvall {
12271369Sdduvall 	uint32_t tries;
12281369Sdduvall 	uint32_t regval;
12291369Sdduvall 
12301369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12311369Sdduvall 
12321369Sdduvall 	/*
12331369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
12341369Sdduvall 	 * to specifically enable SEEPROM access (Flash is the default).
12351369Sdduvall 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
12361369Sdduvall 	 * and the NVM control registers don't exist ...
12371369Sdduvall 	 */
12381369Sdduvall 	switch (bgep->chipid.nvtype) {
12391369Sdduvall 	case BGE_NVTYPE_NONE:
12401369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
12411369Sdduvall 		_NOTE(NOTREACHED)
12421369Sdduvall 	case BGE_NVTYPE_SEEPROM:
12431369Sdduvall 		break;
12441369Sdduvall 
12451369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
12461369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
12471369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
12481369Sdduvall 	default:
12491369Sdduvall 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
12501369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
12511369Sdduvall 		break;
12521369Sdduvall 	}
12531369Sdduvall 
12541369Sdduvall 	/*
12551369Sdduvall 	 * Check there's no command in progress.
12561369Sdduvall 	 *
12571369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
12581369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
12591369Sdduvall 	 * Also, to ensure we don't have a conflict with the chip's
12601369Sdduvall 	 * internal firmware or a process accessing the same (shared)
12611369Sdduvall 	 * SEEPROM through the other port of a 5704, we've already
12621369Sdduvall 	 * been through the "software arbitration" protocol.
12631369Sdduvall 	 * So this is just a final consistency check: we shouldn't
12641369Sdduvall 	 * see EITHER the START bit (command started but not complete)
12651369Sdduvall 	 * OR the COMPLETE bit (command completed but not cleared).
12661369Sdduvall 	 */
12671369Sdduvall 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12681369Sdduvall 	if (regval & SEEPROM_ACCESS_START)
12691369Sdduvall 		return (EPROTO);
12701369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE)
12711369Sdduvall 		return (EPROTO);
12721369Sdduvall 
12731369Sdduvall 	/*
12741369Sdduvall 	 * Assemble the command ...
12751369Sdduvall 	 */
12761369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
12771369Sdduvall 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
12781369Sdduvall 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
12791369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
12801369Sdduvall 	cmd |= SEEPROM_ACCESS_START;
12811369Sdduvall 	cmd |= SEEPROM_ACCESS_COMPLETE;
12821369Sdduvall 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
12831369Sdduvall 
12841369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
12851369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
12861369Sdduvall 
12871369Sdduvall 	/*
12881369Sdduvall 	 * By observation, a successful access takes ~20us on a 5703/4,
12891369Sdduvall 	 * but apparently much longer (up to 1000us) on the obsolescent
12901369Sdduvall 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
12911369Sdduvall 	 * timeouts here; but OTOH, we don't want a bogus access to lock
12921369Sdduvall 	 * out interrupts for longer than necessary. So we'll allow up
12931369Sdduvall 	 * to 1000us ...
12941369Sdduvall 	 */
12951369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
12961369Sdduvall 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12971369Sdduvall 		if (regval & SEEPROM_ACCESS_COMPLETE)
12981369Sdduvall 			break;
12991369Sdduvall 		drv_usecwait(1);
13001369Sdduvall 	}
13011369Sdduvall 
13021369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE) {
13031369Sdduvall 		/*
13041369Sdduvall 		 * All OK; read the SEEPROM data register, then write back
13051369Sdduvall 		 * the value read from the address register in order to
13061369Sdduvall 		 * clear the <complete> bit and leave the SEEPROM access
13071369Sdduvall 		 * state machine idle, ready for the next access ...
13081369Sdduvall 		 */
13091369Sdduvall 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
13101369Sdduvall 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
13111369Sdduvall 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
13121369Sdduvall 		return (0);
13131369Sdduvall 	}
13141369Sdduvall 
13151369Sdduvall 	/*
13161369Sdduvall 	 * Hmm ... what happened here?
13171369Sdduvall 	 *
13182135Szh199473 	 * Most likely, the user addressed a non-existent SEEPROM. Or
13191369Sdduvall 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
13201369Sdduvall 	 * and didn't respond to being addressed. Either way, it's left
13211369Sdduvall 	 * the SEEPROM access state machine wedged. So we'll reset it
13221369Sdduvall 	 * before we leave, so it's ready for next time ...
13231369Sdduvall 	 */
13241369Sdduvall 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
13251369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
13261369Sdduvall 	return (ENODATA);
13271369Sdduvall }
13281369Sdduvall 
13291369Sdduvall /*
13301369Sdduvall  * Basic Flash get/set access routine
13311369Sdduvall  *
13321369Sdduvall  * These use the chip's Flash auto-access method, controlled by the
13331369Sdduvall  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
13341369Sdduvall  * fiddle with the individual bits.
13351369Sdduvall  *
13361369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
13371369Sdduvall  * the right to access the Flash, via bge_nvmem_acquire() above.
13381369Sdduvall  *
13391369Sdduvall  * Return value:
13401369Sdduvall  *	0 on success,
13411369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
13421369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
13431369Sdduvall  *
13441369Sdduvall  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
13451369Sdduvall  * from a (successful) NVM_FLASH_CMD_RD.
13461369Sdduvall  */
13471369Sdduvall static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
13481369Sdduvall 				uint32_t *dp);
13491369Sdduvall #pragma	no_inline(bge_flash_access)
13501369Sdduvall 
13511369Sdduvall static int
13521369Sdduvall bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
13531369Sdduvall {
13541369Sdduvall 	uint32_t tries;
13551369Sdduvall 	uint32_t regval;
13561369Sdduvall 
13571369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
13581369Sdduvall 
13591369Sdduvall 	/*
13601369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
13611369Sdduvall 	 * to specifically disable SEEPROM access while accessing Flash.
13621369Sdduvall 	 * The older chips don't support Flash, and the NVM registers don't
13631369Sdduvall 	 * exist, so we shouldn't be here at all!
13641369Sdduvall 	 */
13651369Sdduvall 	switch (bgep->chipid.nvtype) {
13661369Sdduvall 	case BGE_NVTYPE_NONE:
13671369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
13681369Sdduvall 		_NOTE(NOTREACHED)
13691369Sdduvall 	case BGE_NVTYPE_SEEPROM:
13701369Sdduvall 		return (ENODEV);
13711369Sdduvall 
13721369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
13731369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
13741369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
13751369Sdduvall 	default:
13761369Sdduvall 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
13771369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
13781369Sdduvall 		break;
13791369Sdduvall 	}
13801369Sdduvall 
13811369Sdduvall 	/*
13821369Sdduvall 	 * Assemble the command ...
13831369Sdduvall 	 */
13841369Sdduvall 	addr &= NVM_FLASH_ADDR_MASK;
13851369Sdduvall 	cmd |= NVM_FLASH_CMD_DOIT;
13861369Sdduvall 	cmd |= NVM_FLASH_CMD_FIRST;
13871369Sdduvall 	cmd |= NVM_FLASH_CMD_LAST;
13881369Sdduvall 	cmd |= NVM_FLASH_CMD_DONE;
13891369Sdduvall 
13901369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
13911369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
13921369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
13931369Sdduvall 
13941369Sdduvall 	/*
13951369Sdduvall 	 * Allow up to 1000ms ...
13961369Sdduvall 	 */
13971369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
13981369Sdduvall 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
13991369Sdduvall 		if (regval & NVM_FLASH_CMD_DONE)
14001369Sdduvall 			break;
14011369Sdduvall 		drv_usecwait(1);
14021369Sdduvall 	}
14031369Sdduvall 
14041369Sdduvall 	if (regval & NVM_FLASH_CMD_DONE) {
14051369Sdduvall 		/*
14061369Sdduvall 		 * All OK; read the data from the Flash read register
14071369Sdduvall 		 */
14081369Sdduvall 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
14091369Sdduvall 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
14101369Sdduvall 		return (0);
14111369Sdduvall 	}
14121369Sdduvall 
14131369Sdduvall 	/*
14141369Sdduvall 	 * Hmm ... what happened here?
14151369Sdduvall 	 *
14162135Szh199473 	 * Most likely, the user addressed a non-existent Flash. Or
14171369Sdduvall 	 * maybe the Flash was busy internally (e.g. processing a write)
14181369Sdduvall 	 * and didn't respond to being addressed. Either way, there's
14191369Sdduvall 	 * nothing we can here ...
14201369Sdduvall 	 */
14211369Sdduvall 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
14221369Sdduvall 	return (ENODATA);
14231369Sdduvall }
14241369Sdduvall 
14251369Sdduvall /*
14261369Sdduvall  * The next two functions regulate access to the NVram (if fitted).
14271369Sdduvall  *
14281369Sdduvall  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
14291369Sdduvall  * (SPI) interface, but they can be accessed through either port. These
14301369Sdduvall  * are managed by different instance of this driver and have no software
14311369Sdduvall  * state in common.
14321369Sdduvall  *
14331369Sdduvall  * In addition (and even on a single core chip) the chip's internal
14341369Sdduvall  * firmware can access the SEEPROM/Flash, most notably after a RESET
14351369Sdduvall  * when it may download code to run internally.
14361369Sdduvall  *
14371369Sdduvall  * So we need to arbitrate between these various software agents.  For
14381369Sdduvall  * this purpose, the chip provides the Software Arbitration Register,
14391369Sdduvall  * which implements hardware(!) arbitration.
14401369Sdduvall  *
14411369Sdduvall  * This functionality didn't exist on older (5700/5701) chips, so there's
14421369Sdduvall  * nothing we can do by way of arbitration on those; also, if there's no
14431369Sdduvall  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
14441369Sdduvall  * nothing to do.
14451369Sdduvall  *
14461369Sdduvall  * The internal firmware appears to use Request 0, which is the highest
14471369Sdduvall  * priority.  So we'd like to use Request 2, leaving one higher and one
14481369Sdduvall  * lower for any future developments ... but apparently this doesn't
14491369Sdduvall  * always work.  So for now, the code uses Request 1 ;-(
14501369Sdduvall  */
14511369Sdduvall 
14521369Sdduvall #define	NVM_READ_REQ	NVM_READ_REQ1
14531369Sdduvall #define	NVM_RESET_REQ	NVM_RESET_REQ1
14541369Sdduvall #define	NVM_SET_REQ	NVM_SET_REQ1
14551369Sdduvall 
14561369Sdduvall static void bge_nvmem_relinquish(bge_t *bgep);
14571369Sdduvall #pragma	no_inline(bge_nvmem_relinquish)
14581369Sdduvall 
14591369Sdduvall static void
14601369Sdduvall bge_nvmem_relinquish(bge_t *bgep)
14611369Sdduvall {
14621369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
14631369Sdduvall 
14641369Sdduvall 	switch (bgep->chipid.nvtype) {
14651369Sdduvall 	case BGE_NVTYPE_NONE:
14661369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
14671369Sdduvall 		_NOTE(NOTREACHED)
14681369Sdduvall 		return;
14691369Sdduvall 
14701369Sdduvall 	case BGE_NVTYPE_SEEPROM:
14711369Sdduvall 		/*
14721369Sdduvall 		 * No arbitration performed, no release needed
14731369Sdduvall 		 */
14741369Sdduvall 		return;
14751369Sdduvall 
14761369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
14771369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
14781369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
14791369Sdduvall 	default:
14801369Sdduvall 		break;
14811369Sdduvall 	}
14821369Sdduvall 
14831369Sdduvall 	/*
14841369Sdduvall 	 * Our own request should be present (whether or not granted) ...
14851369Sdduvall 	 */
14861865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14871369Sdduvall 
14881369Sdduvall 	/*
14891369Sdduvall 	 * ... this will make it go away.
14901369Sdduvall 	 */
14911369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
14921865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14931369Sdduvall }
14941369Sdduvall 
14951369Sdduvall /*
14961369Sdduvall  * Arbitrate for access to the NVmem, if necessary
14971369Sdduvall  *
14981369Sdduvall  * Return value:
14991369Sdduvall  *	0 on success
15001369Sdduvall  *	EAGAIN if the device is in use (retryable)
15011369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
15021369Sdduvall  */
15031369Sdduvall static int bge_nvmem_acquire(bge_t *bgep);
15041369Sdduvall #pragma	no_inline(bge_nvmem_acquire)
15051369Sdduvall 
15061369Sdduvall static int
15071369Sdduvall bge_nvmem_acquire(bge_t *bgep)
15081369Sdduvall {
15091369Sdduvall 	uint32_t regval;
15101369Sdduvall 	uint32_t tries;
15111369Sdduvall 
15121369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15131369Sdduvall 
15141369Sdduvall 	switch (bgep->chipid.nvtype) {
15151369Sdduvall 	case BGE_NVTYPE_NONE:
15161369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15171369Sdduvall 		/*
15181369Sdduvall 		 * Access denied: no (recognisable) device fitted
15191369Sdduvall 		 */
15201369Sdduvall 		return (ENODEV);
15211369Sdduvall 
15221369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15231369Sdduvall 		/*
15241369Sdduvall 		 * Access granted: no arbitration needed (or possible)
15251369Sdduvall 		 */
15261369Sdduvall 		return (0);
15271369Sdduvall 
15281369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15291369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15301369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15311369Sdduvall 	default:
15321369Sdduvall 		/*
15331369Sdduvall 		 * Access conditional: conduct arbitration protocol
15341369Sdduvall 		 */
15351369Sdduvall 		break;
15361369Sdduvall 	}
15371369Sdduvall 
15381369Sdduvall 	/*
15391369Sdduvall 	 * We're holding the per-port mutex <genlock>, so no-one other
15402135Szh199473 	 * thread can be attempting to access the NVmem through *this*
15411369Sdduvall 	 * port. But it could be in use by the *other* port (of a 5704),
15421369Sdduvall 	 * or by the chip's internal firmware, so we have to go through
15431369Sdduvall 	 * the full (hardware) arbitration protocol ...
15441369Sdduvall 	 *
15451369Sdduvall 	 * Note that *because* we're holding <genlock>, the interrupt handler
15461369Sdduvall 	 * won't be able to progress.  So we're only willing to spin for a
15471369Sdduvall 	 * fairly short time.  Specifically:
15481369Sdduvall 	 *
15491369Sdduvall 	 *	We *must* wait long enough for the hardware to resolve all
15501369Sdduvall 	 *	requests and determine the winner.  Fortunately, this is
15511369Sdduvall 	 *	"almost instantaneous", even as observed by GHz CPUs.
15521369Sdduvall 	 *
15531369Sdduvall 	 *	A successful access by another Solaris thread (via either
15541369Sdduvall 	 *	port) typically takes ~20us.  So waiting a bit longer than
15551369Sdduvall 	 *	that will give a good chance of success, if the other user
15561369Sdduvall 	 *	*is* another thread on the other port.
15571369Sdduvall 	 *
15581369Sdduvall 	 *	However, the internal firmware can hold on to the NVmem
15591369Sdduvall 	 *	for *much* longer: at least 10 milliseconds just after a
15601369Sdduvall 	 *	RESET, and maybe even longer if the NVmem actually contains
15611369Sdduvall 	 *	code to download and run on the internal CPUs.
15621369Sdduvall 	 *
15631369Sdduvall 	 * So, we'll allow 50us; if that's not enough then it's up to the
15641369Sdduvall 	 * caller to retry later (hence the choice of return code EAGAIN).
15651369Sdduvall 	 */
15661369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15671369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
15681369Sdduvall 
15691369Sdduvall 	for (tries = 0; tries < 50; ++tries) {
15701369Sdduvall 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15711369Sdduvall 		if (regval & NVM_WON_REQ1)
15721369Sdduvall 			break;
15731369Sdduvall 		drv_usecwait(1);
15741369Sdduvall 	}
15751369Sdduvall 
15761369Sdduvall 	if (regval & NVM_WON_REQ1) {
15771369Sdduvall 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
15781369Sdduvall 		return (0);
15791369Sdduvall 	}
15801369Sdduvall 
15811369Sdduvall 	/*
15821369Sdduvall 	 * Somebody else must be accessing the NVmem, so abandon our
15831369Sdduvall 	 * attempt take control of it.  The caller can try again later ...
15841369Sdduvall 	 */
15851369Sdduvall 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
15861369Sdduvall 	bge_nvmem_relinquish(bgep);
15871369Sdduvall 	return (EAGAIN);
15881369Sdduvall }
15891369Sdduvall 
15901369Sdduvall /*
15911369Sdduvall  * This code assumes that the GPIO1 bit has been wired up to the NVmem
15921369Sdduvall  * write protect line in such a way that the NVmem is protected when
15931369Sdduvall  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
15941369Sdduvall  * NVmem writable we have to change GPIO1 to an output AND drive it low.
15951369Sdduvall  *
15961369Sdduvall  * Note: there's only one set of GPIO pins on a 5704, even though they
15971369Sdduvall  * can be accessed through either port.  So the chip has to resolve what
15981369Sdduvall  * happens if the two ports program a single pin differently ... the rule
15991369Sdduvall  * it uses is that if the ports disagree about the *direction* of a pin,
16001369Sdduvall  * "output" wins over "input", but if they disagree about its *value* as
16011369Sdduvall  * an output, then the pin is TRISTATED instead!  In such a case, no-one
16021369Sdduvall  * wins, and the external signal does whatever the external circuitry
16031369Sdduvall  * defines as the default -- which we've assumed is the PROTECTED state.
16041369Sdduvall  * So, we always change GPIO1 back to being an *input* whenever we're not
16051369Sdduvall  * specifically using it to unprotect the NVmem. This allows either port
16062135Szh199473  * to update the NVmem, although obviously only one at a time!
16071369Sdduvall  *
16081369Sdduvall  * The caller should hold <genlock> and *also* have already acquired the
16091369Sdduvall  * right to access the NVmem, via bge_nvmem_acquire() above.
16101369Sdduvall  */
16111369Sdduvall static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
16121369Sdduvall #pragma	inline(bge_nvmem_protect)
16131369Sdduvall 
16141369Sdduvall static void
16151369Sdduvall bge_nvmem_protect(bge_t *bgep, boolean_t protect)
16161369Sdduvall {
16171369Sdduvall 	uint32_t regval;
16181369Sdduvall 
16191369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
16201369Sdduvall 
16211369Sdduvall 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
16221369Sdduvall 	if (protect) {
16231369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_1;
16241369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16251369Sdduvall 	} else {
16261369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
16271369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16281369Sdduvall 	}
16291369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
16301369Sdduvall }
16311369Sdduvall 
16321369Sdduvall /*
16331369Sdduvall  * Now put it all together ...
16341369Sdduvall  *
16351369Sdduvall  * Try to acquire control of the NVmem; if successful, then:
16361369Sdduvall  *	unprotect it (if we want to write to it)
16371369Sdduvall  *	perform the requested access
16381369Sdduvall  *	reprotect it (after a write)
16391369Sdduvall  *	relinquish control
16401369Sdduvall  *
16411369Sdduvall  * Return value:
16421369Sdduvall  *	0 on success,
16431369Sdduvall  *	EAGAIN if the device is in use (retryable)
16441369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
16451369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
16461369Sdduvall  *	EPROTO on other h/w or s/w errors.
16471369Sdduvall  */
16481369Sdduvall static int
16491369Sdduvall bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
16501369Sdduvall {
16511369Sdduvall 	int err;
16521369Sdduvall 
16531369Sdduvall 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
16541369Sdduvall 		switch (cmd) {
16551369Sdduvall 		case BGE_SEE_READ:
16561369Sdduvall 			err = bge_seeprom_access(bgep,
16571369Sdduvall 			    SEEPROM_ACCESS_READ, addr, dp);
16581369Sdduvall 			break;
16591369Sdduvall 
16601369Sdduvall 		case BGE_SEE_WRITE:
16611369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16621369Sdduvall 			err = bge_seeprom_access(bgep,
16631369Sdduvall 			    SEEPROM_ACCESS_WRITE, addr, dp);
16641369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16651369Sdduvall 			break;
16661369Sdduvall 
16671369Sdduvall 		case BGE_FLASH_READ:
16681369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16691369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16701369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16711369Sdduvall 				    NVM_ACCESS_ENABLE);
16721369Sdduvall 			}
16731369Sdduvall 			err = bge_flash_access(bgep,
16741369Sdduvall 			    NVM_FLASH_CMD_RD, addr, dp);
16751369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16761369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16771369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16781369Sdduvall 				    NVM_ACCESS_ENABLE);
16791369Sdduvall 			}
16801369Sdduvall 			break;
16811369Sdduvall 
16821369Sdduvall 		case BGE_FLASH_WRITE:
16831369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16841369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16851369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16861369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16871369Sdduvall 			}
16881369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16891369Sdduvall 			err = bge_flash_access(bgep,
16901369Sdduvall 			    NVM_FLASH_CMD_WR, addr, dp);
16911369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16921369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16931369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16941369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16951369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16961369Sdduvall 			}
16971369Sdduvall 
16981369Sdduvall 			break;
16991369Sdduvall 
17001369Sdduvall 		default:
17011369Sdduvall 			_NOTE(NOTREACHED)
17021369Sdduvall 			break;
17031369Sdduvall 		}
17041369Sdduvall 		bge_nvmem_relinquish(bgep);
17051369Sdduvall 	}
17061369Sdduvall 
17071369Sdduvall 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
17081369Sdduvall 	return (err);
17091369Sdduvall }
17101369Sdduvall 
17111369Sdduvall /*
17121369Sdduvall  * Attempt to get a MAC address from the SEEPROM or Flash, if any
17131369Sdduvall  */
17141369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17151369Sdduvall #pragma no_inline(bge_get_nvmac)
17161369Sdduvall 
17171369Sdduvall static uint64_t
17181369Sdduvall bge_get_nvmac(bge_t *bgep)
17191369Sdduvall {
17201369Sdduvall 	uint32_t mac_high;
17211369Sdduvall 	uint32_t mac_low;
17221369Sdduvall 	uint32_t addr;
17231369Sdduvall 	uint32_t cmd;
17241369Sdduvall 	uint64_t mac;
17251369Sdduvall 
17261369Sdduvall 	BGE_TRACE(("bge_get_nvmac($%p)",
17271369Sdduvall 		(void *)bgep));
17281369Sdduvall 
17291369Sdduvall 	switch (bgep->chipid.nvtype) {
17301369Sdduvall 	case BGE_NVTYPE_NONE:
17311369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
17321369Sdduvall 	default:
17331369Sdduvall 		return (0ULL);
17341369Sdduvall 
17351369Sdduvall 	case BGE_NVTYPE_SEEPROM:
17361369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
17371369Sdduvall 		cmd = BGE_SEE_READ;
17381369Sdduvall 		break;
17391369Sdduvall 
17401369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
17411369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
17421369Sdduvall 		cmd = BGE_FLASH_READ;
17431369Sdduvall 		break;
17441369Sdduvall 	}
17451369Sdduvall 
17461369Sdduvall 	addr = NVMEM_DATA_MAC_ADDRESS;
17471369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
17481369Sdduvall 		return (0ULL);
17491369Sdduvall 	addr += 4;
17501369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
17511369Sdduvall 		return (0ULL);
17521369Sdduvall 
17531369Sdduvall 	/*
17541369Sdduvall 	 * The Broadcom chip is natively BIG-endian, so that's how the
17551369Sdduvall 	 * MAC address is represented in NVmem.  We may need to swap it
17561369Sdduvall 	 * around on a little-endian host ...
17571369Sdduvall 	 */
17581369Sdduvall #ifdef	_BIG_ENDIAN
17591369Sdduvall 	mac = mac_high;
17601369Sdduvall 	mac = mac << 32;
17611369Sdduvall 	mac |= mac_low;
17621369Sdduvall #else
17631369Sdduvall 	mac = BGE_BSWAP_32(mac_high);
17641369Sdduvall 	mac = mac << 32;
17651369Sdduvall 	mac |= BGE_BSWAP_32(mac_low);
17661369Sdduvall #endif	/* _BIG_ENDIAN */
17671369Sdduvall 
17681369Sdduvall 	return (mac);
17691369Sdduvall }
17701369Sdduvall 
17711369Sdduvall #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17721369Sdduvall 
17731369Sdduvall /*
17741369Sdduvall  * Dummy version for when we're not supporting NVmem access
17751369Sdduvall  */
17761369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17771369Sdduvall #pragma inline(bge_get_nvmac)
17781369Sdduvall 
17791369Sdduvall static uint64_t
17801369Sdduvall bge_get_nvmac(bge_t *bgep)
17811369Sdduvall {
17821369Sdduvall 	_NOTE(ARGUNUSED(bgep))
17831369Sdduvall 	return (0ULL);
17841369Sdduvall }
17851369Sdduvall 
17861369Sdduvall #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17871369Sdduvall 
17881369Sdduvall /*
17891369Sdduvall  * Determine the type of NVmem that is (or may be) attached to this chip,
17901369Sdduvall  */
17911369Sdduvall static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
17921369Sdduvall #pragma no_inline(bge_nvmem_id)
17931369Sdduvall 
17941369Sdduvall static enum bge_nvmem_type
17951369Sdduvall bge_nvmem_id(bge_t *bgep)
17961369Sdduvall {
17971369Sdduvall 	enum bge_nvmem_type nvtype;
17981369Sdduvall 	uint32_t config1;
17991369Sdduvall 
18001369Sdduvall 	BGE_TRACE(("bge_nvmem_id($%p)",
18011369Sdduvall 		(void *)bgep));
18021369Sdduvall 
18031369Sdduvall 	switch (bgep->chipid.device) {
18041369Sdduvall 	default:
18051369Sdduvall 		/*
18061369Sdduvall 		 * We shouldn't get here; it means we don't recognise
18071369Sdduvall 		 * the chip, which means we don't know how to determine
18081369Sdduvall 		 * what sort of NVmem (if any) it has.  So we'll say
18091369Sdduvall 		 * NONE, to disable the NVmem access code ...
18101369Sdduvall 		 */
18111369Sdduvall 		nvtype = BGE_NVTYPE_NONE;
18121369Sdduvall 		break;
18131369Sdduvall 
18141369Sdduvall 	case DEVICE_ID_5700:
18151369Sdduvall 	case DEVICE_ID_5700x:
18161369Sdduvall 	case DEVICE_ID_5701:
18171369Sdduvall 		/*
18181369Sdduvall 		 * These devices support *only* SEEPROMs
18191369Sdduvall 		 */
18201369Sdduvall 		nvtype = BGE_NVTYPE_SEEPROM;
18211369Sdduvall 		break;
18221369Sdduvall 
18231369Sdduvall 	case DEVICE_ID_5702:
18241369Sdduvall 	case DEVICE_ID_5702fe:
18251369Sdduvall 	case DEVICE_ID_5703C:
18261369Sdduvall 	case DEVICE_ID_5703S:
18271369Sdduvall 	case DEVICE_ID_5704C:
18281369Sdduvall 	case DEVICE_ID_5704S:
18291369Sdduvall 	case DEVICE_ID_5704:
18301369Sdduvall 	case DEVICE_ID_5705M:
18311369Sdduvall 	case DEVICE_ID_5705C:
18323170Sml149210 	case DEVICE_ID_5705_2:
18331369Sdduvall 	case DEVICE_ID_5706:
18341369Sdduvall 	case DEVICE_ID_5782:
18351369Sdduvall 	case DEVICE_ID_5788:
18362135Szh199473 	case DEVICE_ID_5789:
18371369Sdduvall 	case DEVICE_ID_5751:
18381369Sdduvall 	case DEVICE_ID_5751M:
18392675Szh199473 	case DEVICE_ID_5752:
18402675Szh199473 	case DEVICE_ID_5752M:
18413771Sml149210 	case DEVICE_ID_5754:
18421369Sdduvall 	case DEVICE_ID_5721:
18431369Sdduvall 	case DEVICE_ID_5714C:
18441369Sdduvall 	case DEVICE_ID_5714S:
18451369Sdduvall 	case DEVICE_ID_5715C:
18463170Sml149210 	case DEVICE_ID_5715S:
18471369Sdduvall 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
18481369Sdduvall 		if (config1 & NVM_CFG1_FLASH_MODE)
18491369Sdduvall 			if (config1 & NVM_CFG1_BUFFERED_MODE)
18501369Sdduvall 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
18511369Sdduvall 			else
18521369Sdduvall 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
18531369Sdduvall 		else
18541369Sdduvall 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
18551369Sdduvall 		break;
18561369Sdduvall 	}
18571369Sdduvall 
18581369Sdduvall 	return (nvtype);
18591369Sdduvall }
18601369Sdduvall 
18611369Sdduvall #undef	BGE_DBG
18621369Sdduvall #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
18631369Sdduvall 
18641369Sdduvall static void
18651369Sdduvall bge_init_recv_rule(bge_t *bgep)
18661369Sdduvall {
18671369Sdduvall 	bge_recv_rule_t *rulep;
18681369Sdduvall 	uint32_t i;
18691369Sdduvall 
18701369Sdduvall 	/*
18711369Sdduvall 	 * receive rule: direct all TCP traffic to ring RULE_MATCH_TO_RING
18721369Sdduvall 	 * 1. to direct UDP traffic, set:
18731369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18741369Sdduvall 	 * 	rulep->mask_value = RULE_UDP_MASK_VALUE;
18751369Sdduvall 	 * 2. to direct ICMP traffic, set:
18761369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18771369Sdduvall 	 * 	rulep->mask_value = RULE_ICMP_MASK_VALUE;
18781369Sdduvall 	 * 3. to direct traffic by source ip, set:
18791369Sdduvall 	 * 	rulep->control = RULE_SIP_CONTROL;
18801369Sdduvall 	 * 	rulep->mask_value = RULE_SIP_MASK_VALUE;
18811369Sdduvall 	 */
18821369Sdduvall 	rulep = bgep->recv_rules;
18831369Sdduvall 	rulep->control = RULE_PROTO_CONTROL;
18841369Sdduvall 	rulep->mask_value = RULE_TCP_MASK_VALUE;
18851369Sdduvall 
18861369Sdduvall 	/*
18871369Sdduvall 	 * set receive rule registers
18881369Sdduvall 	 */
18891369Sdduvall 	rulep = bgep->recv_rules;
18901369Sdduvall 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
18911369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
18921369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
18931369Sdduvall 	}
18941369Sdduvall }
18951369Sdduvall 
18961369Sdduvall /*
18971369Sdduvall  * Using the values captured by bge_chip_cfg_init(), and additional probes
18981369Sdduvall  * as required, characterise the chip fully: determine the label by which
18991369Sdduvall  * to refer to this chip, the correct settings for various registers, and
19001369Sdduvall  * of course whether the device and/or subsystem are supported!
19011369Sdduvall  */
19021865Sdilpreet int bge_chip_id_init(bge_t *bgep);
19031369Sdduvall #pragma	no_inline(bge_chip_id_init)
19041369Sdduvall 
19051865Sdilpreet int
19061369Sdduvall bge_chip_id_init(bge_t *bgep)
19071369Sdduvall {
19081369Sdduvall 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
19091369Sdduvall 	boolean_t sys_ok;
19101369Sdduvall 	boolean_t dev_ok;
19111369Sdduvall 	chip_id_t *cidp;
19121369Sdduvall 	uint32_t subid;
19131369Sdduvall 	char *devname;
19141369Sdduvall 	char *sysname;
19151369Sdduvall 	int *ids;
19161369Sdduvall 	int err;
19171369Sdduvall 	uint_t i;
19181369Sdduvall 
19191369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_INITIAL);
19201369Sdduvall 
19211369Sdduvall 	sys_ok = dev_ok = B_FALSE;
19221369Sdduvall 	cidp = &bgep->chipid;
19231369Sdduvall 
19241369Sdduvall 	/*
19251369Sdduvall 	 * Check the PCI device ID to determine the generic chip type and
19261369Sdduvall 	 * select parameters that depend on this.
19271369Sdduvall 	 *
19281369Sdduvall 	 * Note: because the SPARC platforms in general don't fit the
19291369Sdduvall 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
19301369Sdduvall 	 * as zero - which is why we use <asic_rev> rather than <revision>
19311369Sdduvall 	 * below ...
19321369Sdduvall 	 *
19331369Sdduvall 	 * Note: in general we can't distinguish between the Copper/SerDes
19341369Sdduvall 	 * versions by ID alone, as some Copper devices (e.g. some but not
19351369Sdduvall 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
19361369Sdduvall 	 * treat them the same here, and the MII code works out the media
19371369Sdduvall 	 * type later on ...
19381369Sdduvall 	 */
19391369Sdduvall 	cidp->mbuf_base = bge_mbuf_pool_base;
19401369Sdduvall 	cidp->mbuf_length = bge_mbuf_pool_len;
19411369Sdduvall 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
19421369Sdduvall 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
19431369Sdduvall 	cidp->pci_type = BGE_PCI_X;
19441369Sdduvall 	cidp->statistic_type = BGE_STAT_BLK;
19451908Sly149593 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
19461908Sly149593 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
19471908Sly149593 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
19481369Sdduvall 
19491369Sdduvall 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
19501369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
19511369Sdduvall 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
19521369Sdduvall 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
19531369Sdduvall 
19541369Sdduvall 	cidp->msi_enabled = B_FALSE;
19551369Sdduvall 
19561369Sdduvall 	switch (cidp->device) {
19571369Sdduvall 	case DEVICE_ID_5700:
19581369Sdduvall 	case DEVICE_ID_5700x:
19591369Sdduvall 		cidp->chip_label = 5700;
19602135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19611369Sdduvall 		break;
19621369Sdduvall 
19631369Sdduvall 	case DEVICE_ID_5701:
19641369Sdduvall 		cidp->chip_label = 5701;
19651369Sdduvall 		dev_ok = B_TRUE;
19662135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19671369Sdduvall 		break;
19681369Sdduvall 
19691369Sdduvall 	case DEVICE_ID_5702:
19701369Sdduvall 	case DEVICE_ID_5702fe:
19711369Sdduvall 		cidp->chip_label = 5702;
19721369Sdduvall 		dev_ok = B_TRUE;
19732135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19742135Szh199473 		cidp->pci_type = BGE_PCI;
19751369Sdduvall 		break;
19761369Sdduvall 
19771369Sdduvall 	case DEVICE_ID_5703C:
19781369Sdduvall 	case DEVICE_ID_5703S:
19791369Sdduvall 	case DEVICE_ID_5703:
19801369Sdduvall 		/*
19811369Sdduvall 		 * Revision A0 of the 5703/5793 had various errata
19821369Sdduvall 		 * that we can't or don't work around, so it's not
19831369Sdduvall 		 * supported, but all later versions are
19841369Sdduvall 		 */
19851369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
19861369Sdduvall 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
19871369Sdduvall 			dev_ok = B_TRUE;
19882135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19891369Sdduvall 		break;
19901369Sdduvall 
19911369Sdduvall 	case DEVICE_ID_5704C:
19921369Sdduvall 	case DEVICE_ID_5704S:
19931369Sdduvall 	case DEVICE_ID_5704:
19941369Sdduvall 		/*
19951369Sdduvall 		 * Revision A0 of the 5704/5794 had various errata
19961369Sdduvall 		 * but we have workarounds, so it *is* supported.
19971369Sdduvall 		 */
19981369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
19991369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
20001369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
20011369Sdduvall 		dev_ok = B_TRUE;
20022135Szh199473 		if (cidp->asic_rev <  MHCR_CHIP_REV_5704_B0)
20032135Szh199473 			cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20041369Sdduvall 		break;
20051369Sdduvall 
20061369Sdduvall 	case DEVICE_ID_5705C:
20071369Sdduvall 	case DEVICE_ID_5705M:
20081369Sdduvall 	case DEVICE_ID_5705MA3:
20091369Sdduvall 	case DEVICE_ID_5705F:
20103170Sml149210 	case DEVICE_ID_5705_2:
20113771Sml149210 	case DEVICE_ID_5754:
20123771Sml149210 		if (cidp->device == DEVICE_ID_5754) {
20133771Sml149210 			cidp->chip_label = 5754;
20143771Sml149210 			cidp->pci_type = BGE_PCI_E;
20153771Sml149210 		} else {
20163771Sml149210 			cidp->chip_label = 5705;
20173771Sml149210 			cidp->pci_type = BGE_PCI;
20183771Sml149210 		}
20191908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20201908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20211908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20221369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20231369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20241369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20251369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20261908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20271369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20282135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20291369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20301369Sdduvall 		dev_ok = B_TRUE;
20311369Sdduvall 		break;
20321369Sdduvall 
20331369Sdduvall 	case DEVICE_ID_5706:
20341369Sdduvall 		cidp->chip_label = 5706;
20351369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20361369Sdduvall 		break;
20371369Sdduvall 
20381369Sdduvall 	case DEVICE_ID_5782:
20391369Sdduvall 		/*
20401369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20411369Sdduvall 		 */
20421369Sdduvall 		cidp->chip_label = 5782;
20431908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20441908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20451908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20461369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20471369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20481369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20491369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20501908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20511369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20522135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20531369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20541369Sdduvall 		dev_ok = B_TRUE;
20551369Sdduvall 		break;
20561369Sdduvall 
20571369Sdduvall 	case DEVICE_ID_5788:
20581369Sdduvall 		/*
20591369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20601369Sdduvall 		 */
20611369Sdduvall 		cidp->chip_label = 5788;
20621908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20631908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20641908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20651369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20661369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20671369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20681369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20691908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20701369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20711369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20721369Sdduvall 		dev_ok = B_TRUE;
20731369Sdduvall 		break;
20741369Sdduvall 
20751369Sdduvall 	case DEVICE_ID_5714C:
20761369Sdduvall 		if (cidp->revision >= REVISION_ID_5714_A2)
20771369Sdduvall 			cidp->msi_enabled = bge_enable_msi;
20781369Sdduvall 		/* FALLTHRU */
20791369Sdduvall 	case DEVICE_ID_5714S:
20801369Sdduvall 		cidp->chip_label = 5714;
20811908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20821908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20831908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20841369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20851369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20861369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20871369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
20881369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
20891369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20901908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20911369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20921369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20931369Sdduvall 		dev_ok = B_TRUE;
20941369Sdduvall 		break;
20951369Sdduvall 
20961369Sdduvall 	case DEVICE_ID_5715C:
20973170Sml149210 	case DEVICE_ID_5715S:
20981369Sdduvall 		cidp->chip_label = 5715;
20991908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21001908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21011908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21021369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21031369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21041369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21051369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
21061369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
21071369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21081908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21091369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21101369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21111908Sly149593 		if (cidp->revision >= REVISION_ID_5715_A2)
21121908Sly149593 			cidp->msi_enabled = bge_enable_msi;
21131369Sdduvall 		dev_ok = B_TRUE;
21141369Sdduvall 		break;
21151369Sdduvall 
21161369Sdduvall 	case DEVICE_ID_5721:
21171369Sdduvall 		cidp->chip_label = 5721;
21181908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21191908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21201908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21211369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21221369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21231369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21241369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21251369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21261908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21271369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21281369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21291369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21301369Sdduvall 		dev_ok = B_TRUE;
21311369Sdduvall 		break;
21321369Sdduvall 
21331369Sdduvall 	case DEVICE_ID_5751:
21341369Sdduvall 	case DEVICE_ID_5751M:
21351369Sdduvall 		cidp->chip_label = 5751;
21361908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21371908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21381908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21391369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21401369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21411369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21421369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21431369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21441908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21451369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21461369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21471369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21481369Sdduvall 		dev_ok = B_TRUE;
21491369Sdduvall 		break;
21501369Sdduvall 
21512675Szh199473 	case DEVICE_ID_5752:
21522675Szh199473 	case DEVICE_ID_5752M:
21532675Szh199473 		cidp->chip_label = 5752;
21542675Szh199473 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21552675Szh199473 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21562675Szh199473 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21572675Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21582675Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21592675Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21602675Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21612675Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21622675Szh199473 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21632675Szh199473 		cidp->pci_type = BGE_PCI_E;
21642675Szh199473 		cidp->statistic_type = BGE_STAT_REG;
21652675Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21662675Szh199473 		dev_ok = B_TRUE;
21672675Szh199473 		break;
21682675Szh199473 
21692135Szh199473 	case DEVICE_ID_5789:
21702135Szh199473 		cidp->chip_label = 5789;
21712135Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21722135Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21732135Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21742135Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21752135Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21762135Szh199473 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
21772135Szh199473 		cidp->pci_type = BGE_PCI_E;
21782135Szh199473 		cidp->statistic_type = BGE_STAT_REG;
21792135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21802135Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21812135Szh199473 		cidp->msi_enabled = B_TRUE;
21822135Szh199473 		dev_ok = B_TRUE;
21832135Szh199473 		break;
21842135Szh199473 
21851369Sdduvall 	}
21861369Sdduvall 
21871369Sdduvall 	/*
21881369Sdduvall 	 * Setup the default jumbo parameter.
21891369Sdduvall 	 */
21901369Sdduvall 	cidp->ethmax_size = ETHERMAX;
21911369Sdduvall 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
21921908Sly149593 	cidp->std_buf_size = BGE_STD_BUFF_SIZE;
21931369Sdduvall 
21941369Sdduvall 	/*
21951369Sdduvall 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
21961369Sdduvall 	 * setup below jumbo specific parameters.
21971908Sly149593 	 *
21981908Sly149593 	 * For BCM5714/5715, there is only one standard receive ring. So the
21991908Sly149593 	 * std buffer size should be set to BGE_JUMBO_BUFF_SIZE when jumbo
22001908Sly149593 	 * feature is enabled.
22011369Sdduvall 	 */
22021369Sdduvall 	if (bge_jumbo_enable &&
22031369Sdduvall 	    !(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
22041369Sdduvall 	    (cidp->default_mtu > BGE_DEFAULT_MTU) &&
22051369Sdduvall 	    (cidp->default_mtu <= BGE_MAXIMUM_MTU)) {
22061908Sly149593 	    if (DEVICE_5714_SERIES_CHIPSETS(bgep)) {
22071908Sly149593 			cidp->mbuf_lo_water_rdma =
22081908Sly149593 			    RDMA_MBUF_LOWAT_5714_JUMBO;
22091908Sly149593 			cidp->mbuf_lo_water_rmac =
22101908Sly149593 			    MAC_RX_MBUF_LOWAT_5714_JUMBO;
22111908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_5714_JUMBO;
22121908Sly149593 			cidp->jumbo_slots = 0;
22131908Sly149593 			cidp->std_buf_size = BGE_JUMBO_BUFF_SIZE;
22141908Sly149593 	    } else {
22151908Sly149593 			cidp->mbuf_lo_water_rdma =
22161908Sly149593 			    RDMA_MBUF_LOWAT_JUMBO;
22171908Sly149593 			cidp->mbuf_lo_water_rmac =
22181908Sly149593 			    MAC_RX_MBUF_LOWAT_JUMBO;
22191908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
22201908Sly149593 			cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
22211908Sly149593 		}
22221369Sdduvall 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
22231369Sdduvall 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
22241369Sdduvall 		cidp->ethmax_size = cidp->default_mtu +
22251369Sdduvall 		    sizeof (struct ether_header);
22261369Sdduvall 	}
22271369Sdduvall 
22281369Sdduvall 	/*
22291369Sdduvall 	 * Identify the NV memory type: SEEPROM or Flash?
22301369Sdduvall 	 */
22311369Sdduvall 	cidp->nvtype = bge_nvmem_id(bgep);
22321369Sdduvall 
22331369Sdduvall 	/*
22341369Sdduvall 	 * Now, we want to check whether this device is part of a
22351369Sdduvall 	 * supported subsystem (e.g., on the motherboard of a Sun
22361369Sdduvall 	 * branded platform).
22371369Sdduvall 	 *
22381369Sdduvall 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
22391369Sdduvall 	 */
22401369Sdduvall 	if (cidp->subven == VENDOR_ID_SUN)
22411369Sdduvall 		sys_ok = B_TRUE;
22421369Sdduvall 
22431369Sdduvall 	/*
22441369Sdduvall 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
22451369Sdduvall 	 * Note: 0x14e41647 should *not* appear in the list, but the code
22461369Sdduvall 	 * doesn't enforce that.
22471369Sdduvall 	 */
22481369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
22491369Sdduvall 		DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
22501369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
22511369Sdduvall 		/*
22521369Sdduvall 		 * Got the list; scan for a matching subsystem vendor/device
22531369Sdduvall 		 */
22541369Sdduvall 		subid = (cidp->subven << 16) | cidp->subdev;
22551369Sdduvall 		while (i--)
22561369Sdduvall 			if (ids[i] == subid)
22571369Sdduvall 				sys_ok = B_TRUE;
22581369Sdduvall 		ddi_prop_free(ids);
22591369Sdduvall 	}
22601369Sdduvall 
22611369Sdduvall 	/*
22621369Sdduvall 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
22631369Sdduvall 	 *
22641369Sdduvall 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
22651369Sdduvall 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
22661369Sdduvall 	 * we have to check specially for the exact device paths to the
22671369Sdduvall 	 * motherboard devices on those platforms ;-(
22681369Sdduvall 	 *
22691369Sdduvall 	 * Note: we can't just use the "supported-subsystems" mechanism
22701369Sdduvall 	 * above, because the entry would have to be 0x14e41647 -- which
22711369Sdduvall 	 * would then accept *any* plugin card that *didn't* contain a
22721369Sdduvall 	 * (valid) SEEPROM ;-(
22731369Sdduvall 	 */
22741369Sdduvall 	sysname = ddi_node_name(ddi_root_node());
22751369Sdduvall 	devname = ddi_pathname(bgep->devinfo, buf);
22761369Sdduvall 	ASSERT(strlen(devname) > 0);
22771369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
22781369Sdduvall 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
22791369Sdduvall 			sys_ok = B_TRUE;
22801369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
22811369Sdduvall 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
22821369Sdduvall 			sys_ok = B_TRUE;
22831369Sdduvall 
22841369Sdduvall 	/*
22851369Sdduvall 	 * Now check what we've discovered: is this truly a supported
22861369Sdduvall 	 * chip on (the motherboard of) a supported platform?
22871369Sdduvall 	 *
22881369Sdduvall 	 * Possible problems here:
22891369Sdduvall 	 * 1)	it's a completely unheard-of chip (e.g. 5761)
22901369Sdduvall 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
22911369Sdduvall 	 * 3)	it's a chip we would support if it were on the motherboard
22921369Sdduvall 	 *	of a Sun platform, but this one isn't ;-(
22931369Sdduvall 	 */
22941369Sdduvall 	if (cidp->chip_label == 0)
22951369Sdduvall 		bge_problem(bgep,
22961369Sdduvall 			"Device 'pci%04x,%04x' not recognized (%d?)",
22971369Sdduvall 			cidp->vendor, cidp->device, cidp->device);
22981369Sdduvall 	else if (!dev_ok)
22991369Sdduvall 		bge_problem(bgep,
23001369Sdduvall 			"Device 'pci%04x,%04x' (%d) revision %d not supported",
23011369Sdduvall 			cidp->vendor, cidp->device, cidp->chip_label,
23021369Sdduvall 			cidp->revision);
23031369Sdduvall #if	BGE_DEBUGGING
23041369Sdduvall 	else if (!sys_ok)
23051369Sdduvall 		bge_problem(bgep,
23061369Sdduvall 			"%d-based subsystem 'pci%04x,%04x' not validated",
23071369Sdduvall 			cidp->chip_label, cidp->subven, cidp->subdev);
23081369Sdduvall #endif
23091369Sdduvall 	else
23101369Sdduvall 		cidp->flags |= CHIP_FLAG_SUPPORTED;
23111865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
23121865Sdilpreet 		return (EIO);
23131865Sdilpreet 	return (0);
23141369Sdduvall }
23151369Sdduvall 
23161369Sdduvall void
23171369Sdduvall bge_chip_msi_trig(bge_t *bgep)
23181369Sdduvall {
23191369Sdduvall 	uint32_t	regval;
23201369Sdduvall 
23211369Sdduvall 	regval = bgep->param_msi_cnt<<4;
23221369Sdduvall 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
23231369Sdduvall 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
23241369Sdduvall }
23251369Sdduvall 
23261369Sdduvall /*
23271369Sdduvall  * Various registers that control the chip's internal engines (state
23281369Sdduvall  * machines) have a <reset> and <enable> bits (fortunately, in the
23291369Sdduvall  * same place in each such register :-).
23301369Sdduvall  *
23311369Sdduvall  * To reset the state machine, the <reset> bit must be written with 1;
23321369Sdduvall  * it will then read back as 1 while the reset is in progress, but
23331369Sdduvall  * self-clear to 0 when the reset completes.
23341369Sdduvall  *
23351369Sdduvall  * To enable a state machine, one must set the <enable> bit, which
23361369Sdduvall  * will continue to read back as 0 until the state machine is running.
23371369Sdduvall  *
23381369Sdduvall  * To disable a state machine, the <enable> bit must be cleared, but
23391369Sdduvall  * it will continue to read back as 1 until the state machine actually
23401369Sdduvall  * stops.
23411369Sdduvall  *
23421369Sdduvall  * This routine implements polling for completion of a reset, enable
23431369Sdduvall  * or disable operation, returning B_TRUE on success (bit reached the
23441369Sdduvall  * required state) or B_FALSE on timeout (200*100us == 20ms).
23451369Sdduvall  */
23461369Sdduvall static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
23471369Sdduvall 					uint32_t mask, uint32_t val);
23481369Sdduvall #pragma	no_inline(bge_chip_poll_engine)
23491369Sdduvall 
23501369Sdduvall static boolean_t
23511369Sdduvall bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
23521369Sdduvall 	uint32_t mask, uint32_t val)
23531369Sdduvall {
23541369Sdduvall 	uint32_t regval;
23551369Sdduvall 	uint32_t n;
23561369Sdduvall 
23571369Sdduvall 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
23581369Sdduvall 		(void *)bgep, regno, mask, val));
23591369Sdduvall 
23601369Sdduvall 	for (n = 200; n; --n) {
23611369Sdduvall 		regval = bge_reg_get32(bgep, regno);
23621369Sdduvall 		if ((regval & mask) == val)
23631369Sdduvall 			return (B_TRUE);
23641369Sdduvall 		drv_usecwait(100);
23651369Sdduvall 	}
23661369Sdduvall 
23671865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_NO_RESPONSE);
23681369Sdduvall 	return (B_FALSE);
23691369Sdduvall }
23701369Sdduvall 
23711369Sdduvall /*
23721369Sdduvall  * Various registers that control the chip's internal engines (state
23731369Sdduvall  * machines) have a <reset> bit (fortunately, in the same place in
23741369Sdduvall  * each such register :-).  To reset the state machine, this bit must
23751369Sdduvall  * be written with 1; it will then read back as 1 while the reset is
23761369Sdduvall  * in progress, but self-clear to 0 when the reset completes.
23771369Sdduvall  *
23781369Sdduvall  * This code sets the bit, then polls for it to read back as zero.
23791369Sdduvall  * The return value is B_TRUE on success (reset bit cleared itself),
23801369Sdduvall  * or B_FALSE if the state machine didn't recover :(
23811369Sdduvall  *
23821369Sdduvall  * NOTE: the Core reset is similar to other resets, except that we
23831369Sdduvall  * can't poll for completion, since the Core reset disables memory
23841369Sdduvall  * access!  So we just have to assume that it will all complete in
23851369Sdduvall  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
23861369Sdduvall  */
23871369Sdduvall static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
23881369Sdduvall #pragma	no_inline(bge_chip_reset_engine)
23891369Sdduvall 
23901369Sdduvall static boolean_t
23911369Sdduvall bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
23921369Sdduvall {
23931369Sdduvall 	uint32_t regval;
23941369Sdduvall 	uint32_t val32;
23951369Sdduvall 
23961369Sdduvall 	regval = bge_reg_get32(bgep, regno);
23971369Sdduvall 
23981369Sdduvall 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
23991369Sdduvall 		(void *)bgep, regno));
24001369Sdduvall 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
24011369Sdduvall 		regno, regval));
24021369Sdduvall 
24031369Sdduvall 	regval |= STATE_MACHINE_RESET_BIT;
24041369Sdduvall 
24051369Sdduvall 	switch (regno) {
24061369Sdduvall 	case MISC_CONFIG_REG:
24071369Sdduvall 		/*
24081369Sdduvall 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
24091369Sdduvall 		 * resetting PCIE block and bringing PCIE link down, bit 29
24101369Sdduvall 		 * in the register needs to be set first, and then set it again
24111369Sdduvall 		 * while the reset bit is written.
24121369Sdduvall 		 * See:P500 of 57xx-PG102-RDS.pdf.
24131369Sdduvall 		 */
24141369Sdduvall 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
24151369Sdduvall 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
24161369Sdduvall 		    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
24171369Sdduvall 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
24181369Sdduvall 			if (bgep->chipid.pci_type == BGE_PCI_E) {
24191369Sdduvall 				if (bgep->chipid.asic_rev ==
24201369Sdduvall 				    MHCR_CHIP_REV_5751_A0 ||
24211369Sdduvall 				    bgep->chipid.asic_rev ==
24221369Sdduvall 				    MHCR_CHIP_REV_5721_A0) {
24231369Sdduvall 					val32 = bge_reg_get32(bgep,
24241369Sdduvall 					    PHY_TEST_CTRL_REG);
24251369Sdduvall 					if (val32 == (PHY_PCIE_SCRAM_MODE |
24261369Sdduvall 					    PHY_PCIE_LTASS_MODE))
24271369Sdduvall 						bge_reg_put32(bgep,
24281369Sdduvall 						    PHY_TEST_CTRL_REG,
24291369Sdduvall 						    PHY_PCIE_SCRAM_MODE);
24301369Sdduvall 					val32 = pci_config_get32
24311369Sdduvall 					    (bgep->cfg_handle,
24321369Sdduvall 					    PCI_CONF_BGE_CLKCTL);
24331369Sdduvall 					val32 |= CLKCTL_PCIE_A0_FIX;
24341369Sdduvall 					pci_config_put32(bgep->cfg_handle,
24351369Sdduvall 					    PCI_CONF_BGE_CLKCTL, val32);
24361369Sdduvall 				}
24371369Sdduvall 				bge_reg_set32(bgep, regno,
24381369Sdduvall 					MISC_CONFIG_GRC_RESET_DISABLE);
24391369Sdduvall 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
24401369Sdduvall 			}
24411369Sdduvall 		}
24421369Sdduvall 
24431369Sdduvall 		/*
24441369Sdduvall 		 * Special case - causes Core reset
24451369Sdduvall 		 *
24461369Sdduvall 		 * On SPARC v9 we want to ensure that we don't start
24471369Sdduvall 		 * timing until the I/O access has actually reached
24481369Sdduvall 		 * the chip, otherwise we might make the next access
24491369Sdduvall 		 * too early.  And we can't just force the write out
24501369Sdduvall 		 * by following it with a read (even to config space)
24511369Sdduvall 		 * because that would cause the fault we're trying
24521369Sdduvall 		 * to avoid.  Hence the need for membar_sync() here.
24531369Sdduvall 		 */
24541369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
24551369Sdduvall #ifdef	__sparcv9
24561369Sdduvall 		membar_sync();
24571369Sdduvall #endif	/* __sparcv9 */
24581369Sdduvall 		/*
24591369Sdduvall 		 * On some platforms,system need about 300us for
24601369Sdduvall 		 * link setup.
24611369Sdduvall 		 */
24621369Sdduvall 		drv_usecwait(300);
24631369Sdduvall 
24641369Sdduvall 		if (bgep->chipid.pci_type == BGE_PCI_E) {
24651369Sdduvall 			/* PCI-E device need more reset time */
24661369Sdduvall 			drv_usecwait(120000);
24671369Sdduvall 
24681369Sdduvall 			/* Set PCIE max payload size and clear error status. */
24692135Szh199473 			if ((bgep->chipid.chip_label == 5721) ||
24702135Szh199473 			    (bgep->chipid.chip_label == 5751) ||
24712675Szh199473 			    (bgep->chipid.chip_label == 5752) ||
24722135Szh199473 			    (bgep->chipid.chip_label == 5789)) {
24731369Sdduvall 				pci_config_put16(bgep->cfg_handle,
24741369Sdduvall 					PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
24751369Sdduvall 				pci_config_put16(bgep->cfg_handle,
24761369Sdduvall 					PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
24771369Sdduvall 			}
24781369Sdduvall 		}
24791369Sdduvall 
24801369Sdduvall 		BGE_PCICHK(bgep);
24811369Sdduvall 		return (B_TRUE);
24821369Sdduvall 
24831369Sdduvall 	default:
24841369Sdduvall 		bge_reg_put32(bgep, regno, regval);
24851369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
24861865Sdilpreet 		    STATE_MACHINE_RESET_BIT, 0));
24871369Sdduvall 	}
24881369Sdduvall }
24891369Sdduvall 
24901369Sdduvall /*
24911369Sdduvall  * Various registers that control the chip's internal engines (state
24921369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
24931369Sdduvall  * each such register :-).  To stop the state machine, this bit must
24941369Sdduvall  * be written with 0, then polled to see when the state machine has
24951369Sdduvall  * actually stopped.
24961369Sdduvall  *
24971369Sdduvall  * The return value is B_TRUE on success (enable bit cleared), or
24981369Sdduvall  * B_FALSE if the state machine didn't stop :(
24991369Sdduvall  */
25001369Sdduvall static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
25011369Sdduvall 						uint32_t morebits);
25021369Sdduvall #pragma	no_inline(bge_chip_disable_engine)
25031369Sdduvall 
25041369Sdduvall static boolean_t
25051369Sdduvall bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
25061369Sdduvall {
25071369Sdduvall 	uint32_t regval;
25081369Sdduvall 
25091369Sdduvall 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
25101369Sdduvall 		(void *)bgep, regno, morebits));
25111369Sdduvall 
25121369Sdduvall 	switch (regno) {
25131369Sdduvall 	case FTQ_RESET_REG:
25141369Sdduvall 		/*
25151369Sdduvall 		 * Not quite like the others; it doesn't
25161369Sdduvall 		 * have an <enable> bit, but instead we
25171369Sdduvall 		 * have to set and then clear all the bits
25181369Sdduvall 		 */
25191369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
25201369Sdduvall 		drv_usecwait(100);
25211369Sdduvall 		bge_reg_put32(bgep, regno, 0);
25221369Sdduvall 		return (B_TRUE);
25231369Sdduvall 
25241369Sdduvall 	default:
25251369Sdduvall 		regval = bge_reg_get32(bgep, regno);
25261369Sdduvall 		regval &= ~STATE_MACHINE_ENABLE_BIT;
25271369Sdduvall 		regval &= ~morebits;
25281369Sdduvall 		bge_reg_put32(bgep, regno, regval);
25291369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
25301865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, 0));
25311369Sdduvall 	}
25321369Sdduvall }
25331369Sdduvall 
25341369Sdduvall /*
25351369Sdduvall  * Various registers that control the chip's internal engines (state
25361369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
25371369Sdduvall  * each such register :-).  To start the state machine, this bit must
25381369Sdduvall  * be written with 1, then polled to see when the state machine has
25391369Sdduvall  * actually started.
25401369Sdduvall  *
25411369Sdduvall  * The return value is B_TRUE on success (enable bit set), or
25421369Sdduvall  * B_FALSE if the state machine didn't start :(
25431369Sdduvall  */
25441369Sdduvall static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
25451369Sdduvall 					uint32_t morebits);
25461369Sdduvall #pragma	no_inline(bge_chip_enable_engine)
25471369Sdduvall 
25481369Sdduvall static boolean_t
25491369Sdduvall bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
25501369Sdduvall {
25511369Sdduvall 	uint32_t regval;
25521369Sdduvall 
25531369Sdduvall 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
25541369Sdduvall 		(void *)bgep, regno, morebits));
25551369Sdduvall 
25561369Sdduvall 	switch (regno) {
25571369Sdduvall 	case FTQ_RESET_REG:
25581369Sdduvall 		/*
25591369Sdduvall 		 * Not quite like the others; it doesn't
25601369Sdduvall 		 * have an <enable> bit, but instead we
25611369Sdduvall 		 * have to set and then clear all the bits
25621369Sdduvall 		 */
25631369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
25641369Sdduvall 		drv_usecwait(100);
25651369Sdduvall 		bge_reg_put32(bgep, regno, 0);
25661369Sdduvall 		return (B_TRUE);
25671369Sdduvall 
25681369Sdduvall 	default:
25691369Sdduvall 		regval = bge_reg_get32(bgep, regno);
25701369Sdduvall 		regval |= STATE_MACHINE_ENABLE_BIT;
25711369Sdduvall 		regval |= morebits;
25721369Sdduvall 		bge_reg_put32(bgep, regno, regval);
25731369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
25741865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
25751369Sdduvall 	}
25761369Sdduvall }
25771369Sdduvall 
25781369Sdduvall /*
25791369Sdduvall  * Reprogram the Ethernet, Transmit, and Receive MAC
25801369Sdduvall  * modes to match the param_* variables
25811369Sdduvall  */
25821369Sdduvall static void bge_sync_mac_modes(bge_t *bgep);
25831369Sdduvall #pragma	no_inline(bge_sync_mac_modes)
25841369Sdduvall 
25851369Sdduvall static void
25861369Sdduvall bge_sync_mac_modes(bge_t *bgep)
25871369Sdduvall {
25881369Sdduvall 	uint32_t macmode;
25891369Sdduvall 	uint32_t regval;
25901369Sdduvall 
25911369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
25921369Sdduvall 
25931369Sdduvall 	/*
25941369Sdduvall 	 * Reprogram the Ethernet MAC mode ...
25951369Sdduvall 	 */
25961369Sdduvall 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
25971369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
25981369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
25991369Sdduvall 		macmode &= ~ETHERNET_MODE_LINK_POLARITY;
26001369Sdduvall 	else
26011369Sdduvall 		macmode |= ETHERNET_MODE_LINK_POLARITY;
26021369Sdduvall 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
26031369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
26041369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
26051369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_TBI;
26061369Sdduvall 	else if (bgep->param_link_speed == 10 || bgep->param_link_speed == 100)
26071369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_MII;
26081369Sdduvall 	else
26091369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
26101369Sdduvall 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
26111369Sdduvall 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
26121369Sdduvall 	else
26131369Sdduvall 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
26141369Sdduvall 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
26151369Sdduvall 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
26161369Sdduvall 	else
26171369Sdduvall 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
26181369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
26191369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
26201369Sdduvall 		(void *)bgep, regval, macmode));
26211369Sdduvall 
26221369Sdduvall 	/*
26231369Sdduvall 	 * ... the Transmit MAC mode ...
26241369Sdduvall 	 */
26251369Sdduvall 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
26261369Sdduvall 	if (bgep->param_link_tx_pause)
26271369Sdduvall 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
26281369Sdduvall 	else
26291369Sdduvall 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
26301369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
26311369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
26321369Sdduvall 		(void *)bgep, regval, macmode));
26331369Sdduvall 
26341369Sdduvall 	/*
26351369Sdduvall 	 * ... and the Receive MAC mode
26361369Sdduvall 	 */
26371369Sdduvall 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
26381369Sdduvall 	if (bgep->param_link_rx_pause)
26391369Sdduvall 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
26401369Sdduvall 	else
26411369Sdduvall 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
26421369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
26431369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
26441369Sdduvall 		(void *)bgep, regval, macmode));
26451369Sdduvall }
26461369Sdduvall 
26471369Sdduvall /*
26481369Sdduvall  * bge_chip_sync() -- program the chip with the unicast MAC address,
26491369Sdduvall  * the multicast hash table, the required level of promiscuity, and
26501369Sdduvall  * the current loopback mode ...
26511369Sdduvall  */
26521408Srandyf #ifdef BGE_IPMI_ASF
26531865Sdilpreet int bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
26541408Srandyf #else
26551865Sdilpreet int bge_chip_sync(bge_t *bgep);
26561408Srandyf #endif
26571369Sdduvall #pragma	no_inline(bge_chip_sync)
26581369Sdduvall 
26591865Sdilpreet int
26601408Srandyf #ifdef BGE_IPMI_ASF
26611408Srandyf bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
26621408Srandyf #else
26631369Sdduvall bge_chip_sync(bge_t *bgep)
26641408Srandyf #endif
26651369Sdduvall {
26661369Sdduvall 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
26671369Sdduvall 	boolean_t promisc;
26681369Sdduvall 	uint64_t macaddr;
26691369Sdduvall 	uint32_t fill;
26702331Skrgopi 	int i, j;
26711865Sdilpreet 	int retval = DDI_SUCCESS;
26721369Sdduvall 
26731369Sdduvall 	BGE_TRACE(("bge_chip_sync($%p)",
26741369Sdduvall 		(void *)bgep));
26751369Sdduvall 
26761369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
26771369Sdduvall 
26781369Sdduvall 	promisc = B_FALSE;
26791369Sdduvall 	fill = ~(uint32_t)0;
26801369Sdduvall 
26811369Sdduvall 	if (bgep->promisc)
26821369Sdduvall 		promisc = B_TRUE;
26831369Sdduvall 	else
26841369Sdduvall 		fill = (uint32_t)0;
26851369Sdduvall 
26861369Sdduvall 	/*
26871369Sdduvall 	 * If the TX/RX MAC engines are already running, we should stop
26881369Sdduvall 	 * them (and reset the RX engine) before changing the parameters.
26891369Sdduvall 	 * If they're not running, this will have no effect ...
26901369Sdduvall 	 *
26911369Sdduvall 	 * NOTE: this is currently disabled by default because stopping
26921369Sdduvall 	 * and restarting the Tx engine may cause an outgoing packet in
26931369Sdduvall 	 * transit to be truncated.  Also, stopping and restarting the
26941369Sdduvall 	 * Rx engine seems to not work correctly on the 5705.  Testing
26951369Sdduvall 	 * has not (yet!) revealed any problems with NOT stopping and
26961369Sdduvall 	 * restarting these engines (and Broadcom say their drivers don't
26971369Sdduvall 	 * do this), but if it is found to cause problems, this variable
26981369Sdduvall 	 * can be patched to re-enable the old behaviour ...
26991369Sdduvall 	 */
27001369Sdduvall 	if (bge_stop_start_on_sync) {
27011408Srandyf #ifdef BGE_IPMI_ASF
27021865Sdilpreet 		if (!bgep->asf_enabled) {
27031865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
27041865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
27051865Sdilpreet 				retval = DDI_FAILURE;
27061408Srandyf 		} else {
27071865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
27081865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
27091865Sdilpreet 				retval = DDI_FAILURE;
27101408Srandyf 		}
27111408Srandyf #else
27121865Sdilpreet 		if (!bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
27131865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
27141865Sdilpreet 			retval = DDI_FAILURE;
27151408Srandyf #endif
27161865Sdilpreet 		if (!bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
27171865Sdilpreet 			retval = DDI_FAILURE;
27181865Sdilpreet 		if (!bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG))
27191865Sdilpreet 			retval = DDI_FAILURE;
27201369Sdduvall 	}
27211369Sdduvall 
27221369Sdduvall 	/*
27231369Sdduvall 	 * Reprogram the hashed multicast address table ...
27241369Sdduvall 	 */
27251369Sdduvall 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
27261369Sdduvall 		bge_reg_put32(bgep, MAC_HASH_REG(i),
27271369Sdduvall 			bgep->mcast_hash[i] | fill);
27281369Sdduvall 
27291408Srandyf #ifdef BGE_IPMI_ASF
27301408Srandyf 	if (!bgep->asf_enabled || !asf_keeplive) {
27311408Srandyf #endif
27321408Srandyf 		/*
27332331Skrgopi 		 * Transform the MAC address(es) from host to chip format, then
27341408Srandyf 		 * reprogram the transmit random backoff seed and the unicast
27351408Srandyf 		 * MAC address(es) ...
27361408Srandyf 		 */
27372331Skrgopi 		for (j = 0; j < MAC_ADDRESS_REGS_MAX; j++) {
27382331Skrgopi 			for (i = 0, fill = 0, macaddr = 0ull;
27392331Skrgopi 			    i < ETHERADDRL; ++i) {
27402331Skrgopi 				macaddr <<= 8;
27412331Skrgopi 				macaddr |= bgep->curr_addr[j].addr[i];
27422331Skrgopi 				fill += bgep->curr_addr[j].addr[i];
27432331Skrgopi 			}
27442331Skrgopi 			bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
27452331Skrgopi 			bge_reg_put64(bgep, MAC_ADDRESS_REG(j), macaddr);
27461408Srandyf 		}
27471408Srandyf 
27481408Srandyf 		BGE_DEBUG(("bge_chip_sync($%p) setting MAC address %012llx",
27491408Srandyf 			(void *)bgep, macaddr));
27501408Srandyf #ifdef BGE_IPMI_ASF
27511369Sdduvall 	}
27521408Srandyf #endif
27531369Sdduvall 
27541369Sdduvall 	/*
27551369Sdduvall 	 * Set or clear the PROMISCUOUS mode bit
27561369Sdduvall 	 */
27571369Sdduvall 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
27581369Sdduvall 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
27591369Sdduvall 
27601369Sdduvall 	/*
27611369Sdduvall 	 * Sync the rest of the MAC modes too ...
27621369Sdduvall 	 */
27631369Sdduvall 	bge_sync_mac_modes(bgep);
27641369Sdduvall 
27651369Sdduvall 	/*
27661369Sdduvall 	 * Restart RX/TX MAC engines if required ...
27671369Sdduvall 	 */
27681369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
27691865Sdilpreet 		if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
27701865Sdilpreet 			retval = DDI_FAILURE;
27711408Srandyf #ifdef BGE_IPMI_ASF
27721865Sdilpreet 		if (!bgep->asf_enabled) {
27731865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
27741865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
27751865Sdilpreet 				retval = DDI_FAILURE;
27761408Srandyf 		} else {
27771865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
27781865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
27791865Sdilpreet 				retval = DDI_FAILURE;
27801408Srandyf 		}
27811408Srandyf #else
27821865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
27831865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
27841865Sdilpreet 			retval = DDI_FAILURE;
27851408Srandyf #endif
27861369Sdduvall 	}
27871865Sdilpreet 	return (retval);
27881369Sdduvall }
27891369Sdduvall 
27901369Sdduvall /*
27911369Sdduvall  * This array defines the sequence of state machine control registers
27921369Sdduvall  * in which the <enable> bit must be cleared to bring the chip to a
27931369Sdduvall  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
27941369Sdduvall  */
27951369Sdduvall static bge_regno_t shutdown_engine_regs[] = {
27961369Sdduvall 	RECEIVE_MAC_MODE_REG,
27971369Sdduvall 	RCV_BD_INITIATOR_MODE_REG,
27981369Sdduvall 	RCV_LIST_PLACEMENT_MODE_REG,
27991369Sdduvall 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
28001369Sdduvall 	RCV_DATA_BD_INITIATOR_MODE_REG,
28011369Sdduvall 	RCV_DATA_COMPLETION_MODE_REG,
28021369Sdduvall 	RCV_BD_COMPLETION_MODE_REG,
28031369Sdduvall 
28041369Sdduvall 	SEND_BD_SELECTOR_MODE_REG,
28051369Sdduvall 	SEND_BD_INITIATOR_MODE_REG,
28061369Sdduvall 	SEND_DATA_INITIATOR_MODE_REG,
28071369Sdduvall 	READ_DMA_MODE_REG,
28081369Sdduvall 	SEND_DATA_COMPLETION_MODE_REG,
28091369Sdduvall 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
28101369Sdduvall 	SEND_BD_COMPLETION_MODE_REG,
28111369Sdduvall 	TRANSMIT_MAC_MODE_REG,
28121369Sdduvall 
28131369Sdduvall 	HOST_COALESCE_MODE_REG,
28141369Sdduvall 	WRITE_DMA_MODE_REG,
28151369Sdduvall 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
28161369Sdduvall 	FTQ_RESET_REG,		/* special - see code	*/
28171369Sdduvall 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
28181369Sdduvall 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
28191369Sdduvall 	BGE_REGNO_NONE		/* terminator		*/
28201369Sdduvall };
28211369Sdduvall 
28221369Sdduvall /*
28231369Sdduvall  * bge_chip_stop() -- stop all chip processing
28241369Sdduvall  *
28251369Sdduvall  * If the <fault> parameter is B_TRUE, we're stopping the chip because
28261369Sdduvall  * we've detected a problem internally; otherwise, this is a normal
28271369Sdduvall  * (clean) stop (at user request i.e. the last STREAM has been closed).
28281369Sdduvall  */
28291369Sdduvall void bge_chip_stop(bge_t *bgep, boolean_t fault);
28301369Sdduvall #pragma	no_inline(bge_chip_stop)
28311369Sdduvall 
28321369Sdduvall void
28331369Sdduvall bge_chip_stop(bge_t *bgep, boolean_t fault)
28341369Sdduvall {
28351369Sdduvall 	bge_regno_t regno;
28361369Sdduvall 	bge_regno_t *rbp;
28371369Sdduvall 	boolean_t ok;
28381369Sdduvall 
28391369Sdduvall 	BGE_TRACE(("bge_chip_stop($%p)",
28401369Sdduvall 		(void *)bgep));
28411369Sdduvall 
28421369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
28431369Sdduvall 
28441369Sdduvall 	rbp = shutdown_engine_regs;
28451369Sdduvall 	/*
28461369Sdduvall 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
28471369Sdduvall 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
28481369Sdduvall 	 * -ory arbiter should not be disabled.
28491369Sdduvall 	 */
28501369Sdduvall 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
28511369Sdduvall 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
28521369Sdduvall 			    ok &= bge_chip_disable_engine(bgep, regno, 0);
28531369Sdduvall 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
28541369Sdduvall 				    (regno != DMA_COMPLETION_MODE_REG) &&
28551369Sdduvall 				    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
28561369Sdduvall 				    (regno != BUFFER_MANAGER_MODE_REG) &&
28571369Sdduvall 				    (regno != MEMORY_ARBITER_MODE_REG))
28581369Sdduvall 					ok &= bge_chip_disable_engine(bgep,
28591369Sdduvall 					    regno, 0);
28601369Sdduvall 	}
28611369Sdduvall 
28621865Sdilpreet 	if (!ok && !fault)
28631865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
28641865Sdilpreet 
28651369Sdduvall 	/*
28661369Sdduvall 	 * Finally, disable (all) MAC events & clear the MAC status
28671369Sdduvall 	 */
28681369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
28691369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
28701369Sdduvall 
28711369Sdduvall 	/*
28721865Sdilpreet 	 * if we're stopping the chip because of a detected fault then do
28731865Sdilpreet 	 * appropriate actions
28741369Sdduvall 	 */
28751865Sdilpreet 	if (fault) {
28761865Sdilpreet 		if (bgep->bge_chip_state != BGE_CHIP_FAULT) {
28771865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_FAULT;
28781865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
28791865Sdilpreet 			if (bgep->bge_dma_error) {
28801865Sdilpreet 				/*
28811865Sdilpreet 				 * need to free buffers in case the fault was
28821865Sdilpreet 				 * due to a memory error in a buffer - got to
28831865Sdilpreet 				 * do a fair bit of tidying first
28841865Sdilpreet 				 */
28851865Sdilpreet 				if (bgep->progress & PROGRESS_KSTATS) {
28861865Sdilpreet 					bge_fini_kstats(bgep);
28871865Sdilpreet 					bgep->progress &= ~PROGRESS_KSTATS;
28881865Sdilpreet 				}
28891865Sdilpreet 				if (bgep->progress & PROGRESS_INTR) {
28901865Sdilpreet 					bge_intr_disable(bgep);
28911865Sdilpreet 					rw_enter(bgep->errlock, RW_WRITER);
28921865Sdilpreet 					bge_fini_rings(bgep);
28931865Sdilpreet 					rw_exit(bgep->errlock);
28941865Sdilpreet 					bgep->progress &= ~PROGRESS_INTR;
28951865Sdilpreet 				}
28961865Sdilpreet 				if (bgep->progress & PROGRESS_BUFS) {
28971865Sdilpreet 					bge_free_bufs(bgep);
28981865Sdilpreet 					bgep->progress &= ~PROGRESS_BUFS;
28991865Sdilpreet 				}
29001865Sdilpreet 				bgep->bge_dma_error = B_FALSE;
29011865Sdilpreet 			}
29021865Sdilpreet 		}
29031865Sdilpreet 	} else
29041369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
29051369Sdduvall }
29061369Sdduvall 
29071369Sdduvall /*
29081369Sdduvall  * Poll for completion of chip's ROM firmware; also, at least on the
29091369Sdduvall  * first time through, find and return the hardware MAC address, if any.
29101369Sdduvall  */
29111369Sdduvall static uint64_t bge_poll_firmware(bge_t *bgep);
29121369Sdduvall #pragma	no_inline(bge_poll_firmware)
29131369Sdduvall 
29141369Sdduvall static uint64_t
29151369Sdduvall bge_poll_firmware(bge_t *bgep)
29161369Sdduvall {
29171369Sdduvall 	uint64_t magic;
29181369Sdduvall 	uint64_t mac;
29191369Sdduvall 	uint32_t gen;
29201369Sdduvall 	uint32_t i;
29211369Sdduvall 
29221369Sdduvall 	/*
29231369Sdduvall 	 * Step 19: poll for firmware completion (GENCOMM port set
29241369Sdduvall 	 * to the ones complement of T3_MAGIC_NUMBER).
29251369Sdduvall 	 *
29261369Sdduvall 	 * While we're at it, we also read the MAC address register;
29272135Szh199473 	 * at some stage the firmware will load this with the
29281369Sdduvall 	 * factory-set value.
29291369Sdduvall 	 *
29301369Sdduvall 	 * When both the magic number and the MAC address are set,
29311369Sdduvall 	 * we're done; but we impose a time limit of one second
29321369Sdduvall 	 * (1000*1000us) in case the firmware fails in some fashion
29331369Sdduvall 	 * or the SEEPROM that provides that MAC address isn't fitted.
29341369Sdduvall 	 *
29351369Sdduvall 	 * After the first time through (chip state != INITIAL), we
29361369Sdduvall 	 * don't need the MAC address to be set (we've already got it
29371369Sdduvall 	 * or not, from the first time), so we don't wait for it, but
29381369Sdduvall 	 * we still have to wait for the T3_MAGIC_NUMBER.
29391369Sdduvall 	 *
29401369Sdduvall 	 * Note: the magic number is only a 32-bit quantity, but the NIC
29411369Sdduvall 	 * memory is 64-bit (and big-endian) internally.  Addressing the
29421369Sdduvall 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
29431369Sdduvall 	 * it work correctly on both big- and little-endian hosts.
29441369Sdduvall 	 */
29451369Sdduvall 	for (i = 0; i < 1000; ++i) {
29461369Sdduvall 		drv_usecwait(1000);
29471369Sdduvall 		gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
29481369Sdduvall 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
29491408Srandyf #ifdef BGE_IPMI_ASF
29501408Srandyf 		if (!bgep->asf_enabled) {
29511408Srandyf #endif
29521408Srandyf 			if (gen != ~T3_MAGIC_NUMBER)
29531408Srandyf 				continue;
29541408Srandyf #ifdef BGE_IPMI_ASF
29551408Srandyf 		}
29561408Srandyf #endif
29571369Sdduvall 		if (mac != 0ULL)
29581369Sdduvall 			break;
29591369Sdduvall 		if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
29601369Sdduvall 			break;
29611369Sdduvall 	}
29621369Sdduvall 
29631369Sdduvall 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
29641369Sdduvall 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
29651369Sdduvall 		(void *)bgep, gen, i));
29661369Sdduvall 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
29671369Sdduvall 		mac, magic));
29681369Sdduvall 
29691369Sdduvall 	return (mac);
29701369Sdduvall }
29711369Sdduvall 
29723390Szh199473 /*
29733390Szh199473  * Maximum times of trying to get the NVRAM access lock
29743390Szh199473  * by calling bge_nvmem_acquire()
29753390Szh199473  */
29763390Szh199473 #define	MAX_TRY_NVMEM_ACQUIRE	10000
29773390Szh199473 
29781408Srandyf #ifdef BGE_IPMI_ASF
29791865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
29801408Srandyf #else
29811865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
29821408Srandyf #endif
29831369Sdduvall #pragma	no_inline(bge_chip_reset)
29841369Sdduvall 
29851865Sdilpreet int
29861408Srandyf #ifdef BGE_IPMI_ASF
29871408Srandyf bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
29881408Srandyf #else
29891369Sdduvall bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
29901408Srandyf #endif
29911369Sdduvall {
29921369Sdduvall 	chip_id_t chipid;
29931369Sdduvall 	uint64_t mac;
29941908Sly149593 	uint64_t magic;
29951369Sdduvall 	uint32_t modeflags;
29961369Sdduvall 	uint32_t mhcr;
29971369Sdduvall 	uint32_t sx0;
29983390Szh199473 	uint32_t i, tries;
29991408Srandyf #ifdef BGE_IPMI_ASF
30001408Srandyf 	uint32_t mailbox;
30011408Srandyf #endif
30021865Sdilpreet 	int retval = DDI_SUCCESS;
30031369Sdduvall 
30041369Sdduvall 	BGE_TRACE(("bge_chip_reset($%p, %d)",
30051369Sdduvall 		(void *)bgep, enable_dma));
30061369Sdduvall 
30071369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
30081369Sdduvall 
30091369Sdduvall 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
30101369Sdduvall 		(void *)bgep, enable_dma, bgep->bge_chip_state));
30111369Sdduvall 
30121369Sdduvall 	/*
30131369Sdduvall 	 * Do we need to stop the chip cleanly before resetting?
30141369Sdduvall 	 */
30151369Sdduvall 	switch (bgep->bge_chip_state) {
30161369Sdduvall 	default:
30171369Sdduvall 		_NOTE(NOTREACHED)
30181865Sdilpreet 		return (DDI_FAILURE);
30191369Sdduvall 
30201369Sdduvall 	case BGE_CHIP_INITIAL:
30211369Sdduvall 	case BGE_CHIP_STOPPED:
30221369Sdduvall 	case BGE_CHIP_RESET:
30231369Sdduvall 		break;
30241369Sdduvall 
30251369Sdduvall 	case BGE_CHIP_RUNNING:
30261369Sdduvall 	case BGE_CHIP_ERROR:
30271369Sdduvall 	case BGE_CHIP_FAULT:
30281369Sdduvall 		bge_chip_stop(bgep, B_FALSE);
30291369Sdduvall 		break;
30301369Sdduvall 	}
30311369Sdduvall 
30321408Srandyf #ifdef BGE_IPMI_ASF
30331408Srandyf 	if (bgep->asf_enabled) {
30341408Srandyf 		if (asf_mode == ASF_MODE_INIT) {
30351408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
30361408Srandyf 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
30371408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
30381408Srandyf 		}
30391408Srandyf 	}
30401408Srandyf #endif
30411369Sdduvall 	/*
30421369Sdduvall 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
30431369Sdduvall 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
30441369Sdduvall 	 *
30451369Sdduvall 	 * Before reset Core clock,it is
30461369Sdduvall 	 * also required to initialize the Memory Arbiter as specified in step9
30471369Sdduvall 	 * and Misc Host Control Register as specified in step-13
30481369Sdduvall 	 * Step 4-5: reset Core clock & wait for completion
30491369Sdduvall 	 * Steps 6-8: are done by bge_chip_cfg_init()
30501908Sly149593 	 * put the T3_MAGIC_NUMBER into the GENCOMM port before reset
30511369Sdduvall 	 */
30521865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
30531865Sdilpreet 		retval = DDI_FAILURE;
30541369Sdduvall 
30551369Sdduvall 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
30561369Sdduvall 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
30571369Sdduvall 	    MHCR_MASK_INTERRUPT_MODE |
30581369Sdduvall 	    MHCR_MASK_PCI_INT_OUTPUT |
30591369Sdduvall 	    MHCR_CLEAR_INTERRUPT_INTA;
30601369Sdduvall #ifdef  _BIG_ENDIAN
30611369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
30621369Sdduvall #endif  /* _BIG_ENDIAN */
30631369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
30641408Srandyf #ifdef BGE_IPMI_ASF
30651408Srandyf 	if (bgep->asf_enabled)
30661408Srandyf 		bgep->asf_wordswapped = B_FALSE;
30671408Srandyf #endif
30682675Szh199473 	/*
30692675Szh199473 	 * NVRAM Corruption Workaround
30702675Szh199473 	 */
30713390Szh199473 	for (tries = 0; tries < MAX_TRY_NVMEM_ACQUIRE; tries++)
30723534Szh199473 		if (bge_nvmem_acquire(bgep) != EAGAIN)
30732675Szh199473 			break;
30743440Szh199473 	if (tries >= MAX_TRY_NVMEM_ACQUIRE)
30752675Szh199473 		BGE_DEBUG(("%s: fail to acquire nvram lock",
30762675Szh199473 			bgep->ifname));
30772675Szh199473 
30781908Sly149593 #ifdef BGE_IPMI_ASF
30791908Sly149593 	if (!bgep->asf_enabled) {
30801908Sly149593 #endif
30811908Sly149593 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
30821908Sly149593 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
30831908Sly149593 #ifdef BGE_IPMI_ASF
30841908Sly149593 	}
30851908Sly149593 #endif
30861908Sly149593 
30871865Sdilpreet 	if (!bge_chip_reset_engine(bgep, MISC_CONFIG_REG))
30881865Sdilpreet 		retval = DDI_FAILURE;
30891369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
30901369Sdduvall 
30911369Sdduvall 	/*
30921369Sdduvall 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
30931369Sdduvall 	 * a bit set to avoid a fifo overflow/underflow bug.
30941369Sdduvall 	 */
30952135Szh199473 	if ((bgep->chipid.chip_label == 5721) ||
30962135Szh199473 		(bgep->chipid.chip_label == 5751) ||
30972675Szh199473 		(bgep->chipid.chip_label == 5752) ||
30982135Szh199473 		(bgep->chipid.chip_label == 5789))
30991369Sdduvall 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
31001369Sdduvall 
31011369Sdduvall 
31021369Sdduvall 	/*
31031369Sdduvall 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
31041369Sdduvall 	 * not be changed.
31051369Sdduvall 	 */
31061865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
31071865Sdilpreet 		retval = DDI_FAILURE;
31081369Sdduvall 
31091369Sdduvall 	/*
31101369Sdduvall 	 * Steps 10-11: configure PIO endianness options and
31111369Sdduvall 	 * enable indirect register access -- already done
31121369Sdduvall 	 * Steps 12-13: enable writing to the PCI state & clock
31131369Sdduvall 	 * control registers -- not required; we aren't going to
31141369Sdduvall 	 * use those features.
31151369Sdduvall 	 * Steps 14-15: Configure DMA endianness options.  See
31161369Sdduvall 	 * the comments on the setting of the MHCR above.
31171369Sdduvall 	 */
31181369Sdduvall #ifdef	_BIG_ENDIAN
31191369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
31201369Sdduvall 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
31211369Sdduvall #else
31221369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
31231369Sdduvall #endif	/* _BIG_ENDIAN */
31241408Srandyf #ifdef BGE_IPMI_ASF
31251408Srandyf 	if (bgep->asf_enabled)
31261408Srandyf 		modeflags |= MODE_HOST_STACK_UP;
31271408Srandyf #endif
31281369Sdduvall 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
31291369Sdduvall 
31301408Srandyf #ifdef BGE_IPMI_ASF
31311408Srandyf 	if (bgep->asf_enabled) {
31321408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
31331408Srandyf 			/* Wait for NVRAM init */
31341408Srandyf 			i = 0;
31351408Srandyf 			drv_usecwait(5000);
31361408Srandyf 			mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
31371408Srandyf 			while ((mailbox != (uint32_t)
31381408Srandyf 				~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
31391408Srandyf 				(i < 10000)) {
31401408Srandyf 				drv_usecwait(100);
31411408Srandyf 				mailbox = bge_nic_get32(bgep,
31421408Srandyf 					BGE_FIRMWARE_MAILBOX);
31431408Srandyf 				i++;
31441408Srandyf 			}
31451408Srandyf 			if (!bgep->asf_newhandshake) {
31461408Srandyf 				if ((asf_mode == ASF_MODE_INIT) ||
31471408Srandyf 					(asf_mode == ASF_MODE_POST_INIT)) {
31481408Srandyf 
31491408Srandyf 					bge_asf_post_reset_old_mode(bgep,
31501408Srandyf 						BGE_INIT_RESET);
31511408Srandyf 				} else {
31521408Srandyf 					bge_asf_post_reset_old_mode(bgep,
31531408Srandyf 						BGE_SHUTDOWN_RESET);
31541408Srandyf 				}
31551408Srandyf 			}
31561408Srandyf 		}
31571408Srandyf 	}
31581408Srandyf #endif
31591369Sdduvall 	/*
31601369Sdduvall 	 * Steps 16-17: poll for firmware completion
31611369Sdduvall 	 */
31621369Sdduvall 	mac = bge_poll_firmware(bgep);
31631369Sdduvall 
31641369Sdduvall 	/*
31651369Sdduvall 	 * Step 18: enable external memory -- doesn't apply.
31661369Sdduvall 	 *
31671369Sdduvall 	 * However we take the opportunity to set the MLCR anyway, as
31681369Sdduvall 	 * this register also controls the SEEPROM auto-access method
31691369Sdduvall 	 * which we may want to use later ...
31701369Sdduvall 	 *
31711369Sdduvall 	 * The proper value here depends on the way the chip is wired
31721369Sdduvall 	 * into the circuit board, as this register *also* controls which
31731369Sdduvall 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
31741369Sdduvall 	 * values driven onto those pins!
31751369Sdduvall 	 *
31761369Sdduvall 	 * See also step 74 in the PRM ...
31771369Sdduvall 	 */
31781369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
31791369Sdduvall 	    bgep->chipid.bge_mlcr_default);
31801369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
31811369Sdduvall 
31821369Sdduvall 	/*
31831369Sdduvall 	 * Step 20: clear the Ethernet MAC mode register
31841369Sdduvall 	 */
31851369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
31861369Sdduvall 
31871369Sdduvall 	/*
31881369Sdduvall 	 * Step 21: restore cache-line-size, latency timer, and
31891369Sdduvall 	 * subsystem ID registers to their original values (not
31901369Sdduvall 	 * those read into the local structure <chipid>, 'cos
31911369Sdduvall 	 * that was after they were cleared by the RESET).
31921369Sdduvall 	 *
31931369Sdduvall 	 * Note: the Subsystem Vendor/Device ID registers are not
31941369Sdduvall 	 * directly writable in config space, so we use the shadow
31951369Sdduvall 	 * copy in "Page Zero" of register space to restore them
31961369Sdduvall 	 * both in one go ...
31971369Sdduvall 	 */
31981369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
31991369Sdduvall 		bgep->chipid.clsize);
32001369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
32011369Sdduvall 		bgep->chipid.latency);
32021369Sdduvall 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
32031369Sdduvall 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
32041369Sdduvall 
32051369Sdduvall 	/*
32061369Sdduvall 	 * The SEND INDEX registers should be reset to zero by the
32071369Sdduvall 	 * global chip reset; if they're not, there'll be trouble
32081865Sdilpreet 	 * later on.
32091369Sdduvall 	 */
32101369Sdduvall 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
32111865Sdilpreet 	if (sx0 != 0) {
32121865Sdilpreet 		BGE_REPORT((bgep, "SEND INDEX - device didn't RESET"));
32131865Sdilpreet 		bge_fm_ereport(bgep, DDI_FM_DEVICE_INVAL_STATE);
32143170Sml149210 		retval = DDI_FAILURE;
32151865Sdilpreet 	}
32161369Sdduvall 
32171369Sdduvall 	/* Enable MSI code */
32181369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
32191369Sdduvall 		bge_reg_set32(bgep, MSI_MODE_REG,
3220*3907Szh199473 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE|MSI_ERROR_ATTENTION);
32211369Sdduvall 
32221369Sdduvall 	/*
32231369Sdduvall 	 * On the first time through, save the factory-set MAC address
32241369Sdduvall 	 * (if any).  If bge_poll_firmware() above didn't return one
32251369Sdduvall 	 * (from a chip register) consider looking in the attached NV
32261369Sdduvall 	 * memory device, if any.  Once we have it, we save it in both
32271369Sdduvall 	 * register-image (64-bit) and byte-array forms.  All-zero and
32281369Sdduvall 	 * all-one addresses are not valid, and we refuse to stash those.
32291369Sdduvall 	 */
32301369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
32311369Sdduvall 		if (mac == 0ULL)
32321369Sdduvall 			mac = bge_get_nvmac(bgep);
32331369Sdduvall 		if (mac != 0ULL && mac != ~0ULL) {
32341369Sdduvall 			bgep->chipid.hw_mac_addr = mac;
32351369Sdduvall 			for (i = ETHERADDRL; i-- != 0; ) {
32361369Sdduvall 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
32371369Sdduvall 				mac >>= 8;
32381369Sdduvall 			}
32392331Skrgopi 			bgep->chipid.vendor_addr.set = B_TRUE;
32401369Sdduvall 		}
32411369Sdduvall 	}
32421369Sdduvall 
32431408Srandyf #ifdef BGE_IPMI_ASF
32441408Srandyf 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
32451408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
32461408Srandyf 			if ((asf_mode == ASF_MODE_INIT) ||
32471408Srandyf 				(asf_mode == ASF_MODE_POST_INIT)) {
32481408Srandyf 
32491408Srandyf 				bge_asf_post_reset_new_mode(bgep,
32501408Srandyf 					BGE_INIT_RESET);
32511408Srandyf 			} else {
32521408Srandyf 				bge_asf_post_reset_new_mode(bgep,
32531408Srandyf 					BGE_SHUTDOWN_RESET);
32541408Srandyf 			}
32551408Srandyf 		}
32561408Srandyf 	}
32571408Srandyf #endif
32581408Srandyf 
32591369Sdduvall 	/*
32601369Sdduvall 	 * Record the new state
32611369Sdduvall 	 */
32621369Sdduvall 	bgep->chip_resets += 1;
32631369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RESET;
32641865Sdilpreet 	return (retval);
32651369Sdduvall }
32661369Sdduvall 
32671369Sdduvall /*
32681369Sdduvall  * bge_chip_start() -- start the chip transmitting and/or receiving,
32691369Sdduvall  * including enabling interrupts
32701369Sdduvall  */
32711865Sdilpreet int bge_chip_start(bge_t *bgep, boolean_t reset_phys);
32721369Sdduvall #pragma	no_inline(bge_chip_start)
32731369Sdduvall 
32741865Sdilpreet int
32751369Sdduvall bge_chip_start(bge_t *bgep, boolean_t reset_phys)
32761369Sdduvall {
32771369Sdduvall 	uint32_t coalmode;
32781369Sdduvall 	uint32_t ledctl;
32791369Sdduvall 	uint32_t mtu;
32801369Sdduvall 	uint32_t maxring;
32813534Szh199473 	uint32_t stats_mask;
32821369Sdduvall 	uint64_t ring;
32831865Sdilpreet 	int retval = DDI_SUCCESS;
32841369Sdduvall 
32851369Sdduvall 	BGE_TRACE(("bge_chip_start($%p)",
32861369Sdduvall 		(void *)bgep));
32871369Sdduvall 
32881369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
32891369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
32901369Sdduvall 
32911369Sdduvall 	/*
32921369Sdduvall 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
32931369Sdduvall 	 * The document specifies 95 separate steps to fully
32941369Sdduvall 	 * initialise the chip!!!!
32951369Sdduvall 	 *
32961369Sdduvall 	 * The reset code above has already got us as far as step
32971369Sdduvall 	 * 21, so we continue with ...
32981369Sdduvall 	 *
32991369Sdduvall 	 * Step 22: clear the MAC statistics block
33001369Sdduvall 	 * (0x0300-0x0aff in NIC-local memory)
33011369Sdduvall 	 */
33021369Sdduvall 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
33031369Sdduvall 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
33041369Sdduvall 		    NIC_MEM_STATISTICS_SIZE);
33051369Sdduvall 
33061369Sdduvall 	/*
33071369Sdduvall 	 * Step 23: clear the status block (in host memory)
33081369Sdduvall 	 */
33091369Sdduvall 	DMA_ZERO(bgep->status_block);
33101369Sdduvall 
33111369Sdduvall 	/*
33121369Sdduvall 	 * Step 24: set DMA read/write control register
33131369Sdduvall 	 */
33141369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
33151369Sdduvall 		bgep->chipid.bge_dma_rwctrl);
33161369Sdduvall 
33171369Sdduvall 	/*
33181369Sdduvall 	 * Step 25: Configure DMA endianness -- already done (16/17)
33191369Sdduvall 	 * Step 26: Configure Host-Based Send Rings
33201369Sdduvall 	 * Step 27: Indicate Host Stack Up
33211369Sdduvall 	 */
33221369Sdduvall 	bge_reg_set32(bgep, MODE_CONTROL_REG,
33231369Sdduvall 		MODE_HOST_SEND_BDS |
33241369Sdduvall 		MODE_HOST_STACK_UP);
33251369Sdduvall 
33261369Sdduvall 	/*
33271369Sdduvall 	 * Step 28: Configure checksum options:
33281611Szh199473 	 *	Solaris supports the hardware default checksum options.
33291611Szh199473 	 *
33301611Szh199473 	 *	Workaround for Incorrect pseudo-header checksum calculation.
33311369Sdduvall 	 */
33322135Szh199473 	if (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)
33331611Szh199473 		bge_reg_set32(bgep, MODE_CONTROL_REG,
33342311Sseb 		    MODE_SEND_NO_PSEUDO_HDR_CSUM);
33351369Sdduvall 
33361369Sdduvall 	/*
33371369Sdduvall 	 * Step 29: configure Timer Prescaler.  The value is always the
33381369Sdduvall 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
33391369Sdduvall 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
33401369Sdduvall 	 * for the whole chip!
33411369Sdduvall 	 */
33421369Sdduvall 	bge_reg_put32(bgep, MISC_CONFIG_REG, MISC_CONFIG_DEFAULT);
33431369Sdduvall 
33441369Sdduvall 	/*
33451369Sdduvall 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
33461369Sdduvall 	 *
33471369Sdduvall 	 * If the mbuf_length is specified as 0, we just leave these at
33481369Sdduvall 	 * their hardware defaults, rather than explicitly setting them.
33491369Sdduvall 	 * As the Broadcom HRM,driver better not change the parameters
33501369Sdduvall 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
33511369Sdduvall 	 */
33521369Sdduvall 	if ((bgep->chipid.mbuf_length != 0) &&
33531369Sdduvall 		(DEVICE_5704_SERIES_CHIPSETS(bgep))) {
33541369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
33551369Sdduvall 				bgep->chipid.mbuf_base);
33561369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
33571369Sdduvall 				bgep->chipid.mbuf_length);
33581369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
33591369Sdduvall 				DMAD_POOL_BASE_DEFAULT);
33601369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
33611369Sdduvall 				DMAD_POOL_LENGTH_DEFAULT);
33621369Sdduvall 	}
33631369Sdduvall 
33641369Sdduvall 	/*
33651369Sdduvall 	 * Step 32: configure MAC memory pool watermarks
33661369Sdduvall 	 */
33671369Sdduvall 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
33681369Sdduvall 		bgep->chipid.mbuf_lo_water_rdma);
33691369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
33701369Sdduvall 		bgep->chipid.mbuf_lo_water_rmac);
33711369Sdduvall 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
33721369Sdduvall 		bgep->chipid.mbuf_hi_water);
33731369Sdduvall 
33741369Sdduvall 	/*
33751369Sdduvall 	 * Step 33: configure DMA resource watermarks
33761369Sdduvall 	 */
33771369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33781369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
33791369Sdduvall 		    bge_dmad_lo_water);
33801369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
33811369Sdduvall 		    bge_dmad_hi_water);
33821369Sdduvall 	}
33831369Sdduvall 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
33841369Sdduvall 
33851369Sdduvall 	/*
33861369Sdduvall 	 * Steps 34-36: enable buffer manager & internal h/w queues
33871369Sdduvall 	 */
33881865Sdilpreet 	if (!bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
33891865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
33901865Sdilpreet 		retval = DDI_FAILURE;
33911865Sdilpreet 	if (!bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0))
33921865Sdilpreet 		retval = DDI_FAILURE;
33931369Sdduvall 
33941369Sdduvall 	/*
33951369Sdduvall 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
33961369Sdduvall 	 */
33971369Sdduvall 	bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
33981369Sdduvall 		&bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
33991369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
34001369Sdduvall 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
34011369Sdduvall 			&bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
34021369Sdduvall 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
34031369Sdduvall 			&bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
34041369Sdduvall 	}
34051369Sdduvall 
34061369Sdduvall 	/*
34071369Sdduvall 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
34081369Sdduvall 	 */
34091369Sdduvall 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
34101369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
34111369Sdduvall 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
34121369Sdduvall 		    bge_replenish_jumbo);
34131369Sdduvall 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
34141369Sdduvall 		    bge_replenish_mini);
34151369Sdduvall 	}
34161369Sdduvall 
34171369Sdduvall 	/*
34181369Sdduvall 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
34191369Sdduvall 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
34201369Sdduvall 	 */
34211369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34221369Sdduvall 		maxring = BGE_SEND_RINGS_MAX;
34231369Sdduvall 	else
34241369Sdduvall 		maxring = BGE_SEND_RINGS_MAX_5705;
34251369Sdduvall 	for (ring = 0; ring < maxring; ++ring) {
34261369Sdduvall 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
34271369Sdduvall 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
34281369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
34291369Sdduvall 			&bgep->send[ring].hw_rcb);
34301369Sdduvall 	}
34311369Sdduvall 
34321369Sdduvall 	/*
34331369Sdduvall 	 * Steps 44-45: initialise Receive Return Rings
34341369Sdduvall 	 * (0x0200-0x02ff in NIC-local memory)
34351369Sdduvall 	 */
34361369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34371369Sdduvall 		maxring = BGE_RECV_RINGS_MAX;
34381369Sdduvall 	else
34391369Sdduvall 		maxring = BGE_RECV_RINGS_MAX_5705;
34401369Sdduvall 	for (ring = 0; ring < maxring; ++ring)
34411369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
34421369Sdduvall 			&bgep->recv[ring].hw_rcb);
34431369Sdduvall 
34441369Sdduvall 	/*
34451369Sdduvall 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
34461369Sdduvall 	 */
34471369Sdduvall 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
34481369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
34491369Sdduvall 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
34501369Sdduvall 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
34511369Sdduvall 	}
34521369Sdduvall 	/*
34531369Sdduvall 	 * Step 47: configure the MAC unicast address
34541369Sdduvall 	 * Step 48: configure the random backoff seed
34551369Sdduvall 	 * Step 96: set up multicast filters
34561369Sdduvall 	 */
34571408Srandyf #ifdef BGE_IPMI_ASF
34581865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE)
34591408Srandyf #else
34601865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE)
34611408Srandyf #endif
34621865Sdilpreet 		retval = DDI_FAILURE;
34631369Sdduvall 
34641369Sdduvall 	/*
34651369Sdduvall 	 * Step 49: configure the MTU
34661369Sdduvall 	 */
34671369Sdduvall 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
34681369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
34691369Sdduvall 
34701369Sdduvall 	/*
34711369Sdduvall 	 * Step 50: configure the IPG et al
34721369Sdduvall 	 */
34731369Sdduvall 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
34741369Sdduvall 
34751369Sdduvall 	/*
34761369Sdduvall 	 * Step 51: configure the default Rx Return Ring
34771369Sdduvall 	 */
34781369Sdduvall 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
34791369Sdduvall 
34801369Sdduvall 	/*
34811369Sdduvall 	 * Steps 52-54: configure Receive List Placement,
34821369Sdduvall 	 * and enable Receive List Placement Statistics
34831369Sdduvall 	 */
34841369Sdduvall 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
34851369Sdduvall 		RCV_LP_CONFIG(bgep->chipid.rx_rings));
34863534Szh199473 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
34873534Szh199473 	case MHCR_CHIP_ASIC_REV_5700:
34883534Szh199473 	case MHCR_CHIP_ASIC_REV_5701:
34893534Szh199473 	case MHCR_CHIP_ASIC_REV_5703:
34903534Szh199473 	case MHCR_CHIP_ASIC_REV_5704:
34913534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
34923534Szh199473 		break;
34933534Szh199473 	case MHCR_CHIP_ASIC_REV_5705:
34943534Szh199473 		break;
34953534Szh199473 	default:
34963534Szh199473 		stats_mask = bge_reg_get32(bgep, RCV_LP_STATS_ENABLE_MASK_REG);
34973534Szh199473 		stats_mask &= ~RCV_LP_STATS_DISABLE_MACTQ;
34983534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, stats_mask);
34993534Szh199473 		break;
35003534Szh199473 	}
35011369Sdduvall 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
35021369Sdduvall 
35031369Sdduvall 	if (bgep->chipid.rx_rings > 1)
35041369Sdduvall 		bge_init_recv_rule(bgep);
35051369Sdduvall 
35061369Sdduvall 	/*
35071369Sdduvall 	 * Steps 55-56: enable Send Data Initiator Statistics
35081369Sdduvall 	 */
35091369Sdduvall 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
35101369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35111369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
35121369Sdduvall 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
35131369Sdduvall 	} else {
35141369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
35151369Sdduvall 		    SEND_INIT_STATS_ENABLE);
35161369Sdduvall 	}
35171369Sdduvall 	/*
35181369Sdduvall 	 * Steps 57-58: stop (?) the Host Coalescing Engine
35191369Sdduvall 	 */
35201865Sdilpreet 	if (!bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0))
35211865Sdilpreet 		retval = DDI_FAILURE;
35221369Sdduvall 
35231369Sdduvall 	/*
35241369Sdduvall 	 * Steps 59-62: initialise Host Coalescing parameters
35251369Sdduvall 	 */
35261369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG, bge_tx_count_norm);
35271369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG, bge_tx_ticks_norm);
35281369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, bge_rx_count_norm);
35291369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, bge_rx_ticks_norm);
35301369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35311369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
35321369Sdduvall 		    bge_tx_count_intr);
35331369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
35341369Sdduvall 		    bge_tx_ticks_intr);
35351369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
35361369Sdduvall 		    bge_rx_count_intr);
35371369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
35381369Sdduvall 		    bge_rx_ticks_intr);
35391369Sdduvall 	}
35401369Sdduvall 
35411369Sdduvall 	/*
35421369Sdduvall 	 * Steps 63-64: initialise status block & statistics
35431369Sdduvall 	 * host memory addresses
35441369Sdduvall 	 * The statistic block does not exist in some chipsets
35451369Sdduvall 	 * Step 65: initialise Statistics Coalescing Tick Counter
35461369Sdduvall 	 */
35471369Sdduvall 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
35481369Sdduvall 		bgep->status_block.cookie.dmac_laddress);
35491369Sdduvall 
35501369Sdduvall 	/*
35511369Sdduvall 	 * Steps 66-67: initialise status block & statistics
35521369Sdduvall 	 * NIC-local memory addresses
35531369Sdduvall 	 */
35541369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35551369Sdduvall 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
35561369Sdduvall 		    bgep->statistics.cookie.dmac_laddress);
35571369Sdduvall 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
35581369Sdduvall 		    STATISTICS_TICKS_DEFAULT);
35591369Sdduvall 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
35601369Sdduvall 		    NIC_MEM_STATUS_BLOCK);
35611369Sdduvall 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
35621369Sdduvall 		    NIC_MEM_STATISTICS);
35631369Sdduvall 	}
35641369Sdduvall 
35651369Sdduvall 	/*
35661369Sdduvall 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
35671369Sdduvall 	 * Completion Engine, the Receive List Placement Engine, and the
35681369Sdduvall 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
35691369Sdduvall 	 * and BCM5715.
35701369Sdduvall 	 */
35711369Sdduvall 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
35721369Sdduvall 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
35731369Sdduvall 		coalmode = COALESCE_64_BYTE_STATUS;
35741369Sdduvall 	else
35751369Sdduvall 		coalmode = 0;
35761865Sdilpreet 	if (!bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode))
35771865Sdilpreet 		retval = DDI_FAILURE;
35781865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
35791865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
35801865Sdilpreet 		retval = DDI_FAILURE;
35811865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0))
35821865Sdilpreet 		retval = DDI_FAILURE;
35831369Sdduvall 
35841369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
35851865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
35861865Sdilpreet 		    STATE_MACHINE_ATTN_ENABLE_BIT))
35871865Sdilpreet 			retval = DDI_FAILURE;
35881369Sdduvall 
35891369Sdduvall 	/*
35901369Sdduvall 	 * Step 72: Enable MAC DMA engines
35911369Sdduvall 	 * Step 73: Clear & enable MAC statistics
35921369Sdduvall 	 */
35931369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
35941369Sdduvall 		ETHERNET_MODE_ENABLE_FHDE |
35951369Sdduvall 		ETHERNET_MODE_ENABLE_RDE |
35961369Sdduvall 		ETHERNET_MODE_ENABLE_TDE);
35971369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
35981369Sdduvall 		ETHERNET_MODE_ENABLE_TX_STATS |
35991369Sdduvall 		ETHERNET_MODE_ENABLE_RX_STATS |
36001369Sdduvall 		ETHERNET_MODE_CLEAR_TX_STATS |
36011369Sdduvall 		ETHERNET_MODE_CLEAR_RX_STATS);
36021369Sdduvall 
36031369Sdduvall 	/*
36041369Sdduvall 	 * Step 74: configure the MLCR (Miscellaneous Local Control
36051369Sdduvall 	 * Register); not required, as we set up the MLCR in step 10
36061369Sdduvall 	 * (part of the reset code) above.
36071369Sdduvall 	 *
36081369Sdduvall 	 * Step 75: clear Interrupt Mailbox 0
36091369Sdduvall 	 */
36101369Sdduvall 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
36111369Sdduvall 
36121369Sdduvall 	/*
36131369Sdduvall 	 * Steps 76-87: Gentlemen, start your engines ...
36141369Sdduvall 	 *
36151369Sdduvall 	 * Enable the DMA Completion Engine, the Write DMA Engine,
36161369Sdduvall 	 * the Read DMA Engine, Receive Data Completion Engine,
36171369Sdduvall 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
36181369Sdduvall 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
36191369Sdduvall 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
36201369Sdduvall 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
36211369Sdduvall 	 *
36221369Sdduvall 	 * Beware exhaust fumes?
36231369Sdduvall 	 */
36241369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
36251865Sdilpreet 		if (!bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0))
36261865Sdilpreet 			retval = DDI_FAILURE;
36271865Sdilpreet 	if (!bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
36281865Sdilpreet 	    (bge_dma_wrprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
36291865Sdilpreet 		retval = DDI_FAILURE;
36301865Sdilpreet 	if (!bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
36311865Sdilpreet 	    (bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
36321865Sdilpreet 		retval = DDI_FAILURE;
36331865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
36341865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36351865Sdilpreet 		retval = DDI_FAILURE;
36361369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
36371865Sdilpreet 		if (!bge_chip_enable_engine(bgep,
36381865Sdilpreet 		    MBUF_CLUSTER_FREE_MODE_REG, 0))
36391865Sdilpreet 			retval = DDI_FAILURE;
36401865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0))
36411865Sdilpreet 		retval = DDI_FAILURE;
36421865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
36431865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36441865Sdilpreet 		retval = DDI_FAILURE;
36451865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
36461865Sdilpreet 	    RCV_BD_DISABLED_RING_ATTN))
36471865Sdilpreet 		retval = DDI_FAILURE;
36481865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
36491865Sdilpreet 	    RCV_DATA_BD_ILL_RING_ATTN))
36501865Sdilpreet 		retval = DDI_FAILURE;
36511865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0))
36521865Sdilpreet 		retval = DDI_FAILURE;
36531865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
36541865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36551865Sdilpreet 		retval = DDI_FAILURE;
36561865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
36571865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36581865Sdilpreet 		retval = DDI_FAILURE;
36591369Sdduvall 
36601369Sdduvall 	/*
36611369Sdduvall 	 * Step 88: download firmware -- doesn't apply
36621369Sdduvall 	 * Steps 89-90: enable Transmit & Receive MAC Engines
36631369Sdduvall 	 */
36641865Sdilpreet 	if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
36651865Sdilpreet 		retval = DDI_FAILURE;
36661408Srandyf #ifdef BGE_IPMI_ASF
36671865Sdilpreet 	if (!bgep->asf_enabled) {
36681865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
36691865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
36701865Sdilpreet 			retval = DDI_FAILURE;
36711408Srandyf 	} else {
36721865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0))
36731865Sdilpreet 			retval = DDI_FAILURE;
36741408Srandyf 	}
36751408Srandyf #else
36761865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
36771865Sdilpreet 	    RECEIVE_MODE_KEEP_VLAN_TAG))
36781865Sdilpreet 		retval = DDI_FAILURE;
36791408Srandyf #endif
36801369Sdduvall 
36811369Sdduvall 	/*
36821369Sdduvall 	 * Step 91: disable auto-polling of PHY status
36831369Sdduvall 	 */
36841369Sdduvall 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
36851369Sdduvall 
36861369Sdduvall 	/*
36871369Sdduvall 	 * Step 92: configure D0 power state (not required)
36881369Sdduvall 	 * Step 93: initialise LED control register ()
36891369Sdduvall 	 */
36901369Sdduvall 	ledctl = LED_CONTROL_DEFAULT;
36911369Sdduvall 	switch (bgep->chipid.device) {
36921369Sdduvall 	case DEVICE_ID_5700:
36931369Sdduvall 	case DEVICE_ID_5700x:
36941369Sdduvall 	case DEVICE_ID_5701:
36951369Sdduvall 		/*
36961369Sdduvall 		 * Switch to 5700 (MAC) mode on these older chips
36971369Sdduvall 		 */
36981369Sdduvall 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
36991369Sdduvall 		ledctl |= LED_CONTROL_LED_MODE_5700;
37001369Sdduvall 		break;
37011369Sdduvall 
37021369Sdduvall 	default:
37031369Sdduvall 		break;
37041369Sdduvall 	}
37051369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
37061369Sdduvall 
37071369Sdduvall 	/*
37081369Sdduvall 	 * Step 94: activate link
37091369Sdduvall 	 */
37101369Sdduvall 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
37111369Sdduvall 
37121369Sdduvall 	/*
37131369Sdduvall 	 * Step 95: set up physical layer (PHY/SerDes)
37141369Sdduvall 	 * restart autoneg (if required)
37151369Sdduvall 	 */
37161369Sdduvall 	if (reset_phys)
37171865Sdilpreet 		if (bge_phys_update(bgep) == DDI_FAILURE)
37181865Sdilpreet 			retval = DDI_FAILURE;
37191369Sdduvall 
37201369Sdduvall 	/*
37211369Sdduvall 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
37221369Sdduvall 	 */
37231369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
37241369Sdduvall 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
37251369Sdduvall 			bgep->buff[ring].rf_next);
37261369Sdduvall 
37271369Sdduvall 	/*
37281369Sdduvall 	 * MSI bits:The least significant MSI 16-bit word.
37291369Sdduvall 	 * ISR will be triggered different.
37301369Sdduvall 	 */
37311369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
37321369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
37331369Sdduvall 
37341369Sdduvall 	/*
37351369Sdduvall 	 * Extra step (DSG): select which interrupts are enabled
37361369Sdduvall 	 *
37371369Sdduvall 	 * Program the Ethernet MAC engine to signal attention on
37381369Sdduvall 	 * Link Change events, then enable interrupts on MAC, DMA,
37391369Sdduvall 	 * and FLOW attention signals.
37401369Sdduvall 	 */
37411369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
37421369Sdduvall 		ETHERNET_EVENT_LINK_INT |
37431369Sdduvall 		ETHERNET_STATUS_PCS_ERROR_INT);
37441408Srandyf #ifdef BGE_IPMI_ASF
37451408Srandyf 	if (bgep->asf_enabled) {
37461408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
37471408Srandyf 			MODE_INT_ON_FLOW_ATTN |
37481408Srandyf 			MODE_INT_ON_DMA_ATTN |
37491408Srandyf 			MODE_HOST_STACK_UP|
37501408Srandyf 			MODE_INT_ON_MAC_ATTN);
37511408Srandyf 	} else {
37521408Srandyf #endif
37531408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
37541408Srandyf 			MODE_INT_ON_FLOW_ATTN |
37551408Srandyf 			MODE_INT_ON_DMA_ATTN |
37561408Srandyf 			MODE_INT_ON_MAC_ATTN);
37571408Srandyf #ifdef BGE_IPMI_ASF
37581408Srandyf 	}
37591408Srandyf #endif
37601369Sdduvall 
37611369Sdduvall 	/*
37621369Sdduvall 	 * Step 97: enable PCI interrupts!!!
37631369Sdduvall 	 */
37641369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
37651369Sdduvall 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
37661369Sdduvall 		    MHCR_MASK_PCI_INT_OUTPUT);
37671369Sdduvall 
37681369Sdduvall 	/*
37691369Sdduvall 	 * All done!
37701369Sdduvall 	 */
37711369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
37721865Sdilpreet 	return (retval);
37731369Sdduvall }
37741369Sdduvall 
37751369Sdduvall 
37761369Sdduvall /*
37771369Sdduvall  * ========== Hardware interrupt handler ==========
37781369Sdduvall  */
37791369Sdduvall 
37801369Sdduvall #undef	BGE_DBG
37811369Sdduvall #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
37821369Sdduvall 
37831369Sdduvall /*
37841369Sdduvall  * Sync the status block, then atomically clear the specified bits in
37851369Sdduvall  * the <flags-and-tag> field of the status block.
37861369Sdduvall  * the <flags> word of the status block, returning the value of the
37871369Sdduvall  * <tag> and the <flags> before the bits were cleared.
37881369Sdduvall  */
37891865Sdilpreet static int bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags);
37901369Sdduvall #pragma	inline(bge_status_sync)
37911369Sdduvall 
37921865Sdilpreet static int
37931865Sdilpreet bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags)
37941369Sdduvall {
37951369Sdduvall 	bge_status_t *bsp;
37961865Sdilpreet 	int retval;
37971369Sdduvall 
37981369Sdduvall 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
37991369Sdduvall 		(void *)bgep, bits));
38001369Sdduvall 
38011369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
38021369Sdduvall 
38031369Sdduvall 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
38041865Sdilpreet 	retval = bge_check_dma_handle(bgep, bgep->status_block.dma_hdl);
38051865Sdilpreet 	if (retval != DDI_FM_OK)
38061865Sdilpreet 		return (retval);
38071865Sdilpreet 
38081369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
38091865Sdilpreet 	*flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
38101369Sdduvall 
38111369Sdduvall 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
38121865Sdilpreet 		(void *)bgep, bits, *flags));
38131865Sdilpreet 
38141865Sdilpreet 	return (retval);
38151369Sdduvall }
38161369Sdduvall 
38171369Sdduvall static void bge_wake_factotum(bge_t *bgep);
38181369Sdduvall #pragma	inline(bge_wake_factotum)
38191369Sdduvall 
38201369Sdduvall static void
38211369Sdduvall bge_wake_factotum(bge_t *bgep)
38221369Sdduvall {
38231369Sdduvall 	mutex_enter(bgep->softintrlock);
38241369Sdduvall 	if (bgep->factotum_flag == 0) {
38251369Sdduvall 		bgep->factotum_flag = 1;
38261369Sdduvall 		ddi_trigger_softintr(bgep->factotum_id);
38271369Sdduvall 	}
38281369Sdduvall 	mutex_exit(bgep->softintrlock);
38291369Sdduvall }
38301369Sdduvall 
38311369Sdduvall /*
38321369Sdduvall  *	bge_intr() -- handle chip interrupts
38331369Sdduvall  */
38341369Sdduvall uint_t bge_intr(caddr_t arg1, caddr_t arg2);
38351369Sdduvall #pragma	no_inline(bge_intr)
38361369Sdduvall 
38371369Sdduvall uint_t
38381369Sdduvall bge_intr(caddr_t arg1, caddr_t arg2)
38391369Sdduvall {
38401369Sdduvall 	bge_t *bgep = (bge_t *)arg1;		/* private device info	*/
38411369Sdduvall 	bge_status_t *bsp;
38421369Sdduvall 	uint64_t flags;
3843*3907Szh199473 	uint32_t regval;
38441369Sdduvall 	uint_t result;
38451865Sdilpreet 	int retval;
38461369Sdduvall 
38471369Sdduvall 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
38481369Sdduvall 
38491369Sdduvall 	/*
38501369Sdduvall 	 * GLD v2 checks that s/w setup is complete before passing
38511369Sdduvall 	 * interrupts to this routine, thus eliminating the old
38521369Sdduvall 	 * (and well-known) race condition around ddi_add_intr()
38531369Sdduvall 	 */
38541369Sdduvall 	ASSERT(bgep->progress & PROGRESS_HWINT);
38551369Sdduvall 
38561369Sdduvall 	result = DDI_INTR_UNCLAIMED;
38571369Sdduvall 	mutex_enter(bgep->genlock);
38581369Sdduvall 
3859*3907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
38601369Sdduvall 		/*
3861*3907Szh199473 		 * Check whether chip's says it's asserting #INTA;
3862*3907Szh199473 		 * if not, don't process or claim the interrupt.
3863*3907Szh199473 		 *
3864*3907Szh199473 		 * Note that the PCI signal is active low, so the
3865*3907Szh199473 		 * bit is *zero* when the interrupt is asserted.
38661369Sdduvall 		 */
3867*3907Szh199473 		regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
3868*3907Szh199473 		if (regval & MLCR_INTA_STATE) {
3869*3907Szh199473 			if (bge_check_acc_handle(bgep, bgep->io_handle)
3870*3907Szh199473 			    != DDI_FM_OK)
38711865Sdilpreet 				goto chip_stop;
3872*3907Szh199473 			mutex_exit(bgep->genlock);
3873*3907Szh199473 			return (result);
38741865Sdilpreet 		}
38751369Sdduvall 
38761369Sdduvall 		/*
3877*3907Szh199473 		 * Block further PCI interrupts ...
3878*3907Szh199473 		 */
3879*3907Szh199473 		bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
3880*3907Szh199473 		    MHCR_MASK_PCI_INT_OUTPUT);
3881*3907Szh199473 
3882*3907Szh199473 	} else {
3883*3907Szh199473 		/*
3884*3907Szh199473 		 * Check MSI status
38851369Sdduvall 		 */
3886*3907Szh199473 		regval = bge_reg_get32(bgep, MSI_STATUS_REG);
3887*3907Szh199473 		if (regval & MSI_ERROR_ATTENTION) {
3888*3907Szh199473 			BGE_REPORT((bgep, "msi error attention,"
3889*3907Szh199473 			    " status=0x%x", regval));
3890*3907Szh199473 			bge_reg_put32(bgep, MSI_STATUS_REG, regval);
3891*3907Szh199473 		}
3892*3907Szh199473 	}
3893*3907Szh199473 
3894*3907Szh199473 	result = DDI_INTR_CLAIMED;
3895*3907Szh199473 
3896*3907Szh199473 	BGE_DEBUG(("bge_intr($%p) ($%p) regval 0x%08x", arg1, arg2, regval));
3897*3907Szh199473 
3898*3907Szh199473 	/*
3899*3907Szh199473 	 * Sync the status block and grab the flags-n-tag from it.
3900*3907Szh199473 	 * We count the number of interrupts where there doesn't
3901*3907Szh199473 	 * seem to have been a DMA update of the status block; if
3902*3907Szh199473 	 * it *has* been updated, the counter will be cleared in
3903*3907Szh199473 	 * the while() loop below ...
3904*3907Szh199473 	 */
3905*3907Szh199473 	bgep->missed_dmas += 1;
3906*3907Szh199473 	bsp = DMA_VPTR(bgep->status_block);
3907*3907Szh199473 	for (;;) {
3908*3907Szh199473 		if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
39091369Sdduvall 			/*
3910*3907Szh199473 			 * bge_chip_stop() may have freed dma area etc
3911*3907Szh199473 			 * while we were in this interrupt handler -
3912*3907Szh199473 			 * better not call bge_status_sync()
39131369Sdduvall 			 */
3914*3907Szh199473 			(void) bge_check_acc_handle(bgep,
3915*3907Szh199473 			    bgep->io_handle);
39161369Sdduvall 			mutex_exit(bgep->genlock);
3917*3907Szh199473 			return (DDI_INTR_CLAIMED);
3918*3907Szh199473 		}
3919*3907Szh199473 		retval = bge_status_sync(bgep, STATUS_FLAG_UPDATED,
3920*3907Szh199473 		    &flags);
3921*3907Szh199473 		if (retval != DDI_FM_OK) {
3922*3907Szh199473 			bgep->bge_dma_error = B_TRUE;
3923*3907Szh199473 			goto chip_stop;
39241369Sdduvall 		}
39251369Sdduvall 
3926*3907Szh199473 		if (!(flags & STATUS_FLAG_UPDATED))
3927*3907Szh199473 			break;
3928*3907Szh199473 
3929*3907Szh199473 		/*
3930*3907Szh199473 		 * Tell the chip that we're processing the interrupt
3931*3907Szh199473 		 */
3932*3907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
3933*3907Szh199473 		    INTERRUPT_MBOX_DISABLE(flags));
3934*3907Szh199473 		if (bge_check_acc_handle(bgep, bgep->io_handle) !=
3935*3907Szh199473 		    DDI_FM_OK)
3936*3907Szh199473 			goto chip_stop;
3937*3907Szh199473 
39381369Sdduvall 		/*
3939*3907Szh199473 		 * Drop the mutex while we:
3940*3907Szh199473 		 * 	Receive any newly-arrived packets
3941*3907Szh199473 		 *	Recycle any newly-finished send buffers
39421369Sdduvall 		 */
3943*3907Szh199473 		bgep->bge_intr_running = B_TRUE;
3944*3907Szh199473 		mutex_exit(bgep->genlock);
3945*3907Szh199473 		bge_receive(bgep, bsp);
3946*3907Szh199473 		bge_recycle(bgep, bsp);
3947*3907Szh199473 		mutex_enter(bgep->genlock);
3948*3907Szh199473 		bgep->bge_intr_running = B_FALSE;
39491369Sdduvall 
39501369Sdduvall 		/*
3951*3907Szh199473 		 * Tell the chip we've finished processing, and
3952*3907Szh199473 		 * give it the tag that we got from the status
3953*3907Szh199473 		 * block earlier, so that it knows just how far
3954*3907Szh199473 		 * we've gone.  If it's got more for us to do,
3955*3907Szh199473 		 * it will now update the status block and try
3956*3907Szh199473 		 * to assert an interrupt (but we've got the
3957*3907Szh199473 		 * #INTA blocked at present).  If we see the
3958*3907Szh199473 		 * update, we'll loop around to do some more.
3959*3907Szh199473 		 * Eventually we'll get out of here ...
3960*3907Szh199473 		 */
3961*3907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
3962*3907Szh199473 		    INTERRUPT_MBOX_ENABLE(flags));
3963*3907Szh199473 		bgep->missed_dmas = 0;
3964*3907Szh199473 	}
3965*3907Szh199473 
3966*3907Szh199473 	/*
3967*3907Szh199473 	 * Check for exceptional conditions that we need to handle
3968*3907Szh199473 	 *
3969*3907Szh199473 	 * Link status changed
3970*3907Szh199473 	 * Status block not updated
3971*3907Szh199473 	 */
3972*3907Szh199473 	if (flags & STATUS_FLAG_LINK_CHANGED)
3973*3907Szh199473 		bge_wake_factotum(bgep);
3974*3907Szh199473 
3975*3907Szh199473 	if (bgep->missed_dmas) {
3976*3907Szh199473 		/*
3977*3907Szh199473 		 * Probably due to the internal status tag not
3978*3907Szh199473 		 * being reset.  Force a status block update now;
3979*3907Szh199473 		 * this should ensure that we get an update and
3980*3907Szh199473 		 * a new interrupt.  After that, we should be in
3981*3907Szh199473 		 * sync again ...
39821369Sdduvall 		 */
3983*3907Szh199473 		BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
3984*3907Szh199473 		    "not updated?", flags));
3985*3907Szh199473 		bgep->missed_updates++;
3986*3907Szh199473 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
3987*3907Szh199473 		    COALESCE_NOW);
3988*3907Szh199473 
3989*3907Szh199473 		if (bgep->missed_dmas >= bge_dma_miss_limit) {
3990*3907Szh199473 			/*
3991*3907Szh199473 			 * If this happens multiple times in a row,
3992*3907Szh199473 			 * it means DMA is just not working.  Maybe
3993*3907Szh199473 			 * the chip's failed, or maybe there's a
3994*3907Szh199473 			 * problem on the PCI bus or in the host-PCI
3995*3907Szh199473 			 * bridge (Tomatillo).
3996*3907Szh199473 			 *
3997*3907Szh199473 			 * At all events, we want to stop further
3998*3907Szh199473 			 * interrupts and let the recovery code take
3999*3907Szh199473 			 * over to see whether anything can be done
4000*3907Szh199473 			 * about it ...
4001*3907Szh199473 			 */
4002*3907Szh199473 			bge_fm_ereport(bgep,
4003*3907Szh199473 			    DDI_FM_DEVICE_BADINT_LIMIT);
4004*3907Szh199473 			goto chip_stop;
40051369Sdduvall 		}
40061369Sdduvall 	}
40071369Sdduvall 
4008*3907Szh199473 	/*
4009*3907Szh199473 	 * Reenable assertion of #INTA, unless there's a DMA fault
4010*3907Szh199473 	 */
4011*3907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
4012*3907Szh199473 		bge_reg_clr32(bgep, PCI_CONF_BGE_MHCR,
4013*3907Szh199473 		    MHCR_MASK_PCI_INT_OUTPUT);
4014*3907Szh199473 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
4015*3907Szh199473 		    DDI_FM_OK)
4016*3907Szh199473 			goto chip_stop;
4017*3907Szh199473 	}
4018*3907Szh199473 
40191865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
40201865Sdilpreet 		goto chip_stop;
40211865Sdilpreet 
40221865Sdilpreet 	mutex_exit(bgep->genlock);
40231865Sdilpreet 	return (result);
40241865Sdilpreet 
40251865Sdilpreet chip_stop:
40261865Sdilpreet #ifdef BGE_IPMI_ASF
40271865Sdilpreet 	if (bgep->asf_enabled && bgep->asf_status == ASF_STAT_RUN) {
40281865Sdilpreet 		/*
40291865Sdilpreet 		 * We must stop ASF heart beat before
40301865Sdilpreet 		 * bge_chip_stop(), otherwise some
40311865Sdilpreet 		 * computers (ex. IBM HS20 blade
40321865Sdilpreet 		 * server) may crash.
40331865Sdilpreet 		 */
40341865Sdilpreet 		bge_asf_update_status(bgep);
40351865Sdilpreet 		bge_asf_stop_timer(bgep);
40361865Sdilpreet 		bgep->asf_status = ASF_STAT_STOP;
40371865Sdilpreet 
40381865Sdilpreet 		bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
40391865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
40401865Sdilpreet 	}
40411865Sdilpreet #endif
40421865Sdilpreet 	bge_chip_stop(bgep, B_TRUE);
40431865Sdilpreet 	(void) bge_check_acc_handle(bgep, bgep->io_handle);
40441369Sdduvall 	mutex_exit(bgep->genlock);
40451369Sdduvall 	return (result);
40461369Sdduvall }
40471369Sdduvall 
40481369Sdduvall /*
40491369Sdduvall  * ========== Factotum, implemented as a softint handler ==========
40501369Sdduvall  */
40511369Sdduvall 
40521369Sdduvall #undef	BGE_DBG
40531369Sdduvall #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
40541369Sdduvall 
40551369Sdduvall static void bge_factotum_error_handler(bge_t *bgep);
40561369Sdduvall #pragma	no_inline(bge_factotum_error_handler)
40571369Sdduvall 
40581369Sdduvall static void
40591369Sdduvall bge_factotum_error_handler(bge_t *bgep)
40601369Sdduvall {
40611369Sdduvall 	uint32_t flow;
40621369Sdduvall 	uint32_t rdma;
40631369Sdduvall 	uint32_t wdma;
40641369Sdduvall 	uint32_t tmac;
40651369Sdduvall 	uint32_t rmac;
40661369Sdduvall 	uint32_t rxrs;
40671369Sdduvall 	uint32_t txrs = 0;
40681369Sdduvall 
40691369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
40701369Sdduvall 
40711369Sdduvall 	/*
40721369Sdduvall 	 * Read all the registers that show the possible
40731369Sdduvall 	 * reasons for the ERROR bit to be asserted
40741369Sdduvall 	 */
40751369Sdduvall 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
40761369Sdduvall 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
40771369Sdduvall 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
40781369Sdduvall 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
40791369Sdduvall 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
40801369Sdduvall 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
40811369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40821369Sdduvall 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
40831369Sdduvall 
40841369Sdduvall 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
40851369Sdduvall 		(void *)bgep, flow, rdma, wdma));
40861369Sdduvall 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
40871369Sdduvall 		(void *)bgep, tmac, rmac, rxrs, txrs));
40881369Sdduvall 
40891369Sdduvall 	/*
40901369Sdduvall 	 * For now, just clear all the errors ...
40911369Sdduvall 	 */
40921369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40931369Sdduvall 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
40941369Sdduvall 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
40951369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
40961369Sdduvall 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
40971369Sdduvall 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
40981369Sdduvall 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
40991369Sdduvall }
41001369Sdduvall 
41011369Sdduvall /*
41021369Sdduvall  * Handler for hardware link state change.
41031369Sdduvall  *
41041369Sdduvall  * When this routine is called, the hardware link state has changed
41051369Sdduvall  * and the new state is reflected in the param_* variables.  Here
41061369Sdduvall  * we must update the softstate, reprogram the MAC to match, and
41071369Sdduvall  * record the change in the log and/or on the console.
41081369Sdduvall  */
41091369Sdduvall static void bge_factotum_link_handler(bge_t *bgep);
41101369Sdduvall #pragma	no_inline(bge_factotum_link_handler)
41111369Sdduvall 
41121369Sdduvall static void
41131369Sdduvall bge_factotum_link_handler(bge_t *bgep)
41141369Sdduvall {
41151369Sdduvall 	void (*logfn)(bge_t *bgep, const char *fmt, ...);
41161369Sdduvall 	const char *msg;
41171369Sdduvall 	hrtime_t deltat;
41181369Sdduvall 
41191369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
41201369Sdduvall 
41211369Sdduvall 	/*
41221369Sdduvall 	 * Update the s/w link_state
41231369Sdduvall 	 */
41241369Sdduvall 	if (bgep->param_link_up)
41251369Sdduvall 		bgep->link_state = LINK_STATE_UP;
41261369Sdduvall 	else
41271369Sdduvall 		bgep->link_state = LINK_STATE_DOWN;
41281369Sdduvall 
41291369Sdduvall 	/*
41301369Sdduvall 	 * Reprogram the MAC modes to match
41311369Sdduvall 	 */
41321369Sdduvall 	bge_sync_mac_modes(bgep);
41331369Sdduvall 
41341369Sdduvall 	/*
41351369Sdduvall 	 * Finally, we have to decide whether to write a message
41361369Sdduvall 	 * on the console or only in the log.  If the PHY has
41371369Sdduvall 	 * been reprogrammed (at user request) "recently", then
41381369Sdduvall 	 * the message only goes in the log.  Otherwise it's an
41391369Sdduvall 	 * "unexpected" event, and it goes on the console as well.
41401369Sdduvall 	 */
41411369Sdduvall 	deltat = bgep->phys_event_time - bgep->phys_write_time;
41421369Sdduvall 	if (deltat > BGE_LINK_SETTLE_TIME)
41431369Sdduvall 		msg = "";
41441369Sdduvall 	else if (bgep->param_link_up)
41451369Sdduvall 		msg = bgep->link_up_msg;
41461369Sdduvall 	else
41471369Sdduvall 		msg = bgep->link_down_msg;
41481369Sdduvall 
41491369Sdduvall 	logfn = (msg == NULL || *msg == '\0') ? bge_notice : bge_log;
41501369Sdduvall 	(*logfn)(bgep, "link %s%s", bgep->link_mode_msg, msg);
41511369Sdduvall }
41521369Sdduvall 
41531865Sdilpreet static boolean_t bge_factotum_link_check(bge_t *bgep, int *dma_state);
41541369Sdduvall #pragma	no_inline(bge_factotum_link_check)
41551369Sdduvall 
41561369Sdduvall static boolean_t
41571865Sdilpreet bge_factotum_link_check(bge_t *bgep, int *dma_state)
41581369Sdduvall {
41591369Sdduvall 	boolean_t check;
41601369Sdduvall 	uint64_t flags;
41611369Sdduvall 	uint32_t tmac_status;
41621369Sdduvall 
41631369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
41641369Sdduvall 
41651369Sdduvall 	/*
41661369Sdduvall 	 * Get & clear the writable status bits in the Tx status register
41671369Sdduvall 	 * (some bits are write-1-to-clear, others are just readonly).
41681369Sdduvall 	 */
41691369Sdduvall 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
41701369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
41711369Sdduvall 
41721369Sdduvall 	/*
41731369Sdduvall 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
41741369Sdduvall 	 */
41751865Sdilpreet 	*dma_state = bge_status_sync(bgep, STATUS_FLAG_ERROR |
41761865Sdilpreet 	    STATUS_FLAG_LINK_CHANGED, &flags);
41771865Sdilpreet 	if (*dma_state != DDI_FM_OK)
41781865Sdilpreet 		return (B_FALSE);
41791369Sdduvall 
41801369Sdduvall 	/*
41811369Sdduvall 	 * Clear any errors flagged in the status block ...
41821369Sdduvall 	 */
41831369Sdduvall 	if (flags & STATUS_FLAG_ERROR)
41841369Sdduvall 		bge_factotum_error_handler(bgep);
41851369Sdduvall 
41861369Sdduvall 	/*
41871369Sdduvall 	 * We need to check the link status if:
41881369Sdduvall 	 *	the status block says there's been a link change
41891369Sdduvall 	 *	or there's any discrepancy between the various
41901369Sdduvall 	 *	flags indicating the link state (link_state,
41911369Sdduvall 	 *	param_link_up, and the LINK STATE bit in the
41921369Sdduvall 	 *	Transmit MAC status register).
41931369Sdduvall 	 */
41941369Sdduvall 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
41951369Sdduvall 	switch (bgep->link_state) {
41961369Sdduvall 	case LINK_STATE_UP:
41971369Sdduvall 		check |= (bgep->param_link_up == B_FALSE);
41981369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
41991369Sdduvall 		break;
42001369Sdduvall 
42011369Sdduvall 	case LINK_STATE_DOWN:
42021369Sdduvall 		check |= (bgep->param_link_up != B_FALSE);
42031369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
42041369Sdduvall 		break;
42051369Sdduvall 
42061369Sdduvall 	default:
42071369Sdduvall 		check = B_TRUE;
42081369Sdduvall 		break;
42091369Sdduvall 	}
42101369Sdduvall 
42111369Sdduvall 	/*
42121369Sdduvall 	 * If <check> is false, we're sure the link hasn't changed.
42131369Sdduvall 	 * If true, however, it's not yet definitive; we have to call
42141369Sdduvall 	 * bge_phys_check() to determine whether the link has settled
42151369Sdduvall 	 * into a new state yet ... and if it has, then call the link
42161369Sdduvall 	 * state change handler.But when the chip is 5700 in Dell 6650
42171369Sdduvall 	 * ,even if check is false, the link may have changed.So we
42181369Sdduvall 	 * have to call bge_phys_check() to determine the link state.
42191369Sdduvall 	 */
42201369Sdduvall 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
42211369Sdduvall 		check = bge_phys_check(bgep);
42221369Sdduvall 		if (check)
42231369Sdduvall 			bge_factotum_link_handler(bgep);
42241369Sdduvall 	}
42251369Sdduvall 
42261369Sdduvall 	return (check);
42271369Sdduvall }
42281369Sdduvall 
42291369Sdduvall /*
42301369Sdduvall  * Factotum routine to check for Tx stall, using the 'watchdog' counter
42311369Sdduvall  */
42321369Sdduvall static boolean_t bge_factotum_stall_check(bge_t *bgep);
42331369Sdduvall #pragma	no_inline(bge_factotum_stall_check)
42341369Sdduvall 
42351369Sdduvall static boolean_t
42361369Sdduvall bge_factotum_stall_check(bge_t *bgep)
42371369Sdduvall {
42381369Sdduvall 	uint32_t dogval;
42391369Sdduvall 
42401369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
42411369Sdduvall 
42421369Sdduvall 	/*
42431369Sdduvall 	 * Specific check for Tx stall ...
42441369Sdduvall 	 *
42451369Sdduvall 	 * The 'watchdog' counter is incremented whenever a packet
42461369Sdduvall 	 * is queued, reset to 1 when some (but not all) buffers
42471369Sdduvall 	 * are reclaimed, reset to 0 (disabled) when all buffers
42481369Sdduvall 	 * are reclaimed, and shifted left here.  If it exceeds the
42491369Sdduvall 	 * threshold value, the chip is assumed to have stalled and
42501369Sdduvall 	 * is put into the ERROR state.  The factotum will then reset
42511369Sdduvall 	 * it on the next pass.
42521369Sdduvall 	 *
42531369Sdduvall 	 * All of which should ensure that we don't get into a state
42541369Sdduvall 	 * where packets are left pending indefinitely!
42551369Sdduvall 	 */
42561369Sdduvall 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
42571369Sdduvall 	if (dogval < bge_watchdog_count)
42581369Sdduvall 		return (B_FALSE);
42591369Sdduvall 
42601369Sdduvall 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
42611865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_STALL);
42621369Sdduvall 	return (B_TRUE);
42631369Sdduvall }
42641369Sdduvall 
42651369Sdduvall /*
42661369Sdduvall  * The factotum is woken up when there's something to do that we'd rather
42671369Sdduvall  * not do from inside a hardware interrupt handler or high-level cyclic.
42681369Sdduvall  * Its two main tasks are:
42691369Sdduvall  *	reset & restart the chip after an error
42701369Sdduvall  *	check the link status whenever necessary
42711369Sdduvall  */
42721369Sdduvall uint_t bge_chip_factotum(caddr_t arg);
42731369Sdduvall #pragma	no_inline(bge_chip_factotum)
42741369Sdduvall 
42751369Sdduvall uint_t
42761369Sdduvall bge_chip_factotum(caddr_t arg)
42771369Sdduvall {
42781369Sdduvall 	bge_t *bgep;
42791369Sdduvall 	uint_t result;
42801369Sdduvall 	boolean_t error;
42811369Sdduvall 	boolean_t linkchg;
42821865Sdilpreet 	int dma_state;
42831369Sdduvall 
42841369Sdduvall 	bgep = (bge_t *)arg;
42851369Sdduvall 
42861369Sdduvall 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
42871369Sdduvall 
42881369Sdduvall 	mutex_enter(bgep->softintrlock);
42891369Sdduvall 	if (bgep->factotum_flag == 0) {
42901369Sdduvall 		mutex_exit(bgep->softintrlock);
42911369Sdduvall 		return (DDI_INTR_UNCLAIMED);
42921369Sdduvall 	}
42931504Sly149593 	bgep->factotum_flag = 0;
42941369Sdduvall 	mutex_exit(bgep->softintrlock);
42951369Sdduvall 
42961369Sdduvall 	result = DDI_INTR_CLAIMED;
42971369Sdduvall 	error = B_FALSE;
42981369Sdduvall 	linkchg = B_FALSE;
42991369Sdduvall 
43001369Sdduvall 	mutex_enter(bgep->genlock);
43011369Sdduvall 	switch (bgep->bge_chip_state) {
43021369Sdduvall 	default:
43031369Sdduvall 		break;
43041369Sdduvall 
43051369Sdduvall 	case BGE_CHIP_RUNNING:
43061865Sdilpreet 		linkchg = bge_factotum_link_check(bgep, &dma_state);
43071369Sdduvall 		error = bge_factotum_stall_check(bgep);
43081865Sdilpreet 		if (dma_state != DDI_FM_OK) {
43091865Sdilpreet 			bgep->bge_dma_error = B_TRUE;
43101865Sdilpreet 			error = B_TRUE;
43111865Sdilpreet 		}
43121865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
43131865Sdilpreet 			error = B_TRUE;
43141865Sdilpreet 		if (error)
43151865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_ERROR;
43161369Sdduvall 		break;
43171369Sdduvall 
43181369Sdduvall 	case BGE_CHIP_ERROR:
43191369Sdduvall 		error = B_TRUE;
43201369Sdduvall 		break;
43211369Sdduvall 
43221369Sdduvall 	case BGE_CHIP_FAULT:
43231369Sdduvall 		/*
43241369Sdduvall 		 * Fault detected, time to reset ...
43251369Sdduvall 		 */
43261369Sdduvall 		if (bge_autorecover) {
43271865Sdilpreet 			if (!(bgep->progress & PROGRESS_BUFS)) {
43281865Sdilpreet 				/*
43291865Sdilpreet 				 * if we can't allocate the ring buffers,
43301865Sdilpreet 				 * try later
43311865Sdilpreet 				 */
43321865Sdilpreet 				if (bge_alloc_bufs(bgep) != DDI_SUCCESS) {
43331865Sdilpreet 					mutex_exit(bgep->genlock);
43341865Sdilpreet 					return (result);
43351865Sdilpreet 				}
43361865Sdilpreet 				bgep->progress |= PROGRESS_BUFS;
43371865Sdilpreet 			}
43381865Sdilpreet 			if (!(bgep->progress & PROGRESS_INTR)) {
43391865Sdilpreet 				bge_init_rings(bgep);
43401865Sdilpreet 				bge_intr_enable(bgep);
43411865Sdilpreet 				bgep->progress |= PROGRESS_INTR;
43421865Sdilpreet 			}
43431865Sdilpreet 			if (!(bgep->progress & PROGRESS_KSTATS)) {
43441865Sdilpreet 				bge_init_kstats(bgep,
43451865Sdilpreet 				    ddi_get_instance(bgep->devinfo));
43461865Sdilpreet 				bgep->progress |= PROGRESS_KSTATS;
43471865Sdilpreet 			}
43481865Sdilpreet 
43491369Sdduvall 			BGE_REPORT((bgep, "automatic recovery activated"));
43501865Sdilpreet 
43511865Sdilpreet 			if (bge_restart(bgep, B_FALSE) != DDI_SUCCESS) {
43521865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43531865Sdilpreet 				error = B_TRUE;
43541865Sdilpreet 			}
43551865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
43561865Sdilpreet 			    DDI_FM_OK) {
43571865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43581865Sdilpreet 				error = B_TRUE;
43591865Sdilpreet 			}
43601865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
43611865Sdilpreet 			    DDI_FM_OK) {
43621865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43631865Sdilpreet 				error = B_TRUE;
43641865Sdilpreet 			}
43651865Sdilpreet 			if (error == B_FALSE) {
43661408Srandyf #ifdef BGE_IPMI_ASF
43671865Sdilpreet 				if (bgep->asf_enabled &&
43681865Sdilpreet 				    bgep->asf_status != ASF_STAT_RUN) {
43691408Srandyf 					bgep->asf_timeout_id = timeout(
43701865Sdilpreet 					    bge_asf_heartbeat, (void *)bgep,
43711865Sdilpreet 					    drv_usectohz(
43721865Sdilpreet 					    BGE_ASF_HEARTBEAT_INTERVAL));
43731408Srandyf 					bgep->asf_status = ASF_STAT_RUN;
43741408Srandyf 				}
43751865Sdilpreet #endif
43761865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
43771865Sdilpreet 				    DDI_SERVICE_RESTORED);
43781408Srandyf 			}
43791369Sdduvall 		}
43801369Sdduvall 		break;
43811369Sdduvall 	}
43821369Sdduvall 
43831865Sdilpreet 
43841369Sdduvall 	/*
43851369Sdduvall 	 * If an error is detected, stop the chip now, marking it as
43861369Sdduvall 	 * faulty, so that it will be reset next time through ...
43871865Sdilpreet 	 *
43881865Sdilpreet 	 * Note that if intr_running is set, then bge_intr() has dropped
43891865Sdilpreet 	 * genlock to call bge_receive/bge_recycle. Can't stop the chip at
43901865Sdilpreet 	 * this point so have to wait until the next time the factotum runs.
43911369Sdduvall 	 */
43921865Sdilpreet 	if (error && !bgep->bge_intr_running) {
43931408Srandyf #ifdef BGE_IPMI_ASF
43941408Srandyf 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
43951408Srandyf 			/*
43961408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
43971408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
43981408Srandyf 			 * may crash.
43991408Srandyf 			 */
44001408Srandyf 			bge_asf_update_status(bgep);
44011408Srandyf 			bge_asf_stop_timer(bgep);
44021408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
44031408Srandyf 
44041408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
44051865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
44061408Srandyf 		}
44071408Srandyf #endif
44081369Sdduvall 		bge_chip_stop(bgep, B_TRUE);
44091865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
44101408Srandyf 	}
44111369Sdduvall 	mutex_exit(bgep->genlock);
44121369Sdduvall 
44131369Sdduvall 	/*
44141369Sdduvall 	 * If the link state changed, tell the world about it.
44151369Sdduvall 	 * Note: can't do this while still holding the mutex.
44161369Sdduvall 	 */
44171369Sdduvall 	if (linkchg)
44182311Sseb 		mac_link_update(bgep->mh, bgep->link_state);
44191369Sdduvall 
44201369Sdduvall 	return (result);
44211369Sdduvall }
44221369Sdduvall 
44231369Sdduvall /*
44241369Sdduvall  * High-level cyclic handler
44251369Sdduvall  *
44261369Sdduvall  * This routine schedules a (low-level) softint callback to the
44271369Sdduvall  * factotum, and prods the chip to update the status block (which
44281369Sdduvall  * will cause a hardware interrupt when complete).
44291369Sdduvall  */
44301369Sdduvall void bge_chip_cyclic(void *arg);
44311369Sdduvall #pragma	no_inline(bge_chip_cyclic)
44321369Sdduvall 
44331369Sdduvall void
44341369Sdduvall bge_chip_cyclic(void *arg)
44351369Sdduvall {
44361369Sdduvall 	bge_t *bgep;
44371369Sdduvall 
44381369Sdduvall 	bgep = arg;
44391369Sdduvall 
44401369Sdduvall 	switch (bgep->bge_chip_state) {
44411369Sdduvall 	default:
44421369Sdduvall 		return;
44431369Sdduvall 
44441369Sdduvall 	case BGE_CHIP_RUNNING:
44451369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
44461865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
44471865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
44481865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
44491369Sdduvall 		break;
44501369Sdduvall 
44511369Sdduvall 	case BGE_CHIP_FAULT:
44521369Sdduvall 	case BGE_CHIP_ERROR:
44531369Sdduvall 		break;
44541369Sdduvall 	}
44551369Sdduvall 
44561369Sdduvall 	bge_wake_factotum(bgep);
44571369Sdduvall }
44581369Sdduvall 
44591369Sdduvall 
44601369Sdduvall /*
44611369Sdduvall  * ========== Ioctl subfunctions ==========
44621369Sdduvall  */
44631369Sdduvall 
44641369Sdduvall #undef	BGE_DBG
44651369Sdduvall #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
44661369Sdduvall 
44671369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
44681369Sdduvall 
44691369Sdduvall static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
44701369Sdduvall #pragma	no_inline(bge_chip_peek_cfg)
44711369Sdduvall 
44721369Sdduvall static void
44731369Sdduvall bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
44741369Sdduvall {
44751369Sdduvall 	uint64_t regval;
44761369Sdduvall 	uint64_t regno;
44771369Sdduvall 
44781369Sdduvall 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
44791369Sdduvall 		(void *)bgep, (void *)ppd));
44801369Sdduvall 
44811369Sdduvall 	regno = ppd->pp_acc_offset;
44821369Sdduvall 
44831369Sdduvall 	switch (ppd->pp_acc_size) {
44841369Sdduvall 	case 1:
44851369Sdduvall 		regval = pci_config_get8(bgep->cfg_handle, regno);
44861369Sdduvall 		break;
44871369Sdduvall 
44881369Sdduvall 	case 2:
44891369Sdduvall 		regval = pci_config_get16(bgep->cfg_handle, regno);
44901369Sdduvall 		break;
44911369Sdduvall 
44921369Sdduvall 	case 4:
44931369Sdduvall 		regval = pci_config_get32(bgep->cfg_handle, regno);
44941369Sdduvall 		break;
44951369Sdduvall 
44961369Sdduvall 	case 8:
44971369Sdduvall 		regval = pci_config_get64(bgep->cfg_handle, regno);
44981369Sdduvall 		break;
44991369Sdduvall 	}
45001369Sdduvall 
45011369Sdduvall 	ppd->pp_acc_data = regval;
45021369Sdduvall }
45031369Sdduvall 
45041369Sdduvall static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
45051369Sdduvall #pragma	no_inline(bge_chip_poke_cfg)
45061369Sdduvall 
45071369Sdduvall static void
45081369Sdduvall bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
45091369Sdduvall {
45101369Sdduvall 	uint64_t regval;
45111369Sdduvall 	uint64_t regno;
45121369Sdduvall 
45131369Sdduvall 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
45141369Sdduvall 		(void *)bgep, (void *)ppd));
45151369Sdduvall 
45161369Sdduvall 	regno = ppd->pp_acc_offset;
45171369Sdduvall 	regval = ppd->pp_acc_data;
45181369Sdduvall 
45191369Sdduvall 	switch (ppd->pp_acc_size) {
45201369Sdduvall 	case 1:
45211369Sdduvall 		pci_config_put8(bgep->cfg_handle, regno, regval);
45221369Sdduvall 		break;
45231369Sdduvall 
45241369Sdduvall 	case 2:
45251369Sdduvall 		pci_config_put16(bgep->cfg_handle, regno, regval);
45261369Sdduvall 		break;
45271369Sdduvall 
45281369Sdduvall 	case 4:
45291369Sdduvall 		pci_config_put32(bgep->cfg_handle, regno, regval);
45301369Sdduvall 		break;
45311369Sdduvall 
45321369Sdduvall 	case 8:
45331369Sdduvall 		pci_config_put64(bgep->cfg_handle, regno, regval);
45341369Sdduvall 		break;
45351369Sdduvall 	}
45361369Sdduvall }
45371369Sdduvall 
45381369Sdduvall static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
45391369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
45401369Sdduvall 
45411369Sdduvall static void
45421369Sdduvall bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
45431369Sdduvall {
45441369Sdduvall 	uint64_t regval;
45451369Sdduvall 	void *regaddr;
45461369Sdduvall 
45471369Sdduvall 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
45481369Sdduvall 		(void *)bgep, (void *)ppd));
45491369Sdduvall 
45501369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
45511369Sdduvall 
45521369Sdduvall 	switch (ppd->pp_acc_size) {
45531369Sdduvall 	case 1:
45541369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
45551369Sdduvall 		break;
45561369Sdduvall 
45571369Sdduvall 	case 2:
45581369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
45591369Sdduvall 		break;
45601369Sdduvall 
45611369Sdduvall 	case 4:
45621369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
45631369Sdduvall 		break;
45641369Sdduvall 
45651369Sdduvall 	case 8:
45661369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
45671369Sdduvall 		break;
45681369Sdduvall 	}
45691369Sdduvall 
45701369Sdduvall 	ppd->pp_acc_data = regval;
45711369Sdduvall }
45721369Sdduvall 
45731369Sdduvall static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
45741369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
45751369Sdduvall 
45761369Sdduvall static void
45771369Sdduvall bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
45781369Sdduvall {
45791369Sdduvall 	uint64_t regval;
45801369Sdduvall 	void *regaddr;
45811369Sdduvall 
45821369Sdduvall 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
45831369Sdduvall 		(void *)bgep, (void *)ppd));
45841369Sdduvall 
45851369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
45861369Sdduvall 	regval = ppd->pp_acc_data;
45871369Sdduvall 
45881369Sdduvall 	switch (ppd->pp_acc_size) {
45891369Sdduvall 	case 1:
45901369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
45911369Sdduvall 		break;
45921369Sdduvall 
45931369Sdduvall 	case 2:
45941369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
45951369Sdduvall 		break;
45961369Sdduvall 
45971369Sdduvall 	case 4:
45981369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
45991369Sdduvall 		break;
46001369Sdduvall 
46011369Sdduvall 	case 8:
46021369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
46031369Sdduvall 		break;
46041369Sdduvall 	}
46051369Sdduvall 	BGE_PCICHK(bgep);
46061369Sdduvall }
46071369Sdduvall 
46081369Sdduvall static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
46091369Sdduvall #pragma	no_inline(bge_chip_peek_nic)
46101369Sdduvall 
46111369Sdduvall static void
46121369Sdduvall bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
46131369Sdduvall {
46141369Sdduvall 	uint64_t regoff;
46151369Sdduvall 	uint64_t regval;
46161369Sdduvall 	void *regaddr;
46171369Sdduvall 
46181369Sdduvall 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
46191369Sdduvall 		(void *)bgep, (void *)ppd));
46201369Sdduvall 
46211369Sdduvall 	regoff = ppd->pp_acc_offset;
46221369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
46231369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
46241369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
46251369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
46261369Sdduvall 
46271369Sdduvall 	switch (ppd->pp_acc_size) {
46281369Sdduvall 	case 1:
46291369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
46301369Sdduvall 		break;
46311369Sdduvall 
46321369Sdduvall 	case 2:
46331369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
46341369Sdduvall 		break;
46351369Sdduvall 
46361369Sdduvall 	case 4:
46371369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
46381369Sdduvall 		break;
46391369Sdduvall 
46401369Sdduvall 	case 8:
46411369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
46421369Sdduvall 		break;
46431369Sdduvall 	}
46441369Sdduvall 
46451369Sdduvall 	ppd->pp_acc_data = regval;
46461369Sdduvall }
46471369Sdduvall 
46481369Sdduvall static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
46491369Sdduvall #pragma	no_inline(bge_chip_poke_nic)
46501369Sdduvall 
46511369Sdduvall static void
46521369Sdduvall bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
46531369Sdduvall {
46541369Sdduvall 	uint64_t regoff;
46551369Sdduvall 	uint64_t regval;
46561369Sdduvall 	void *regaddr;
46571369Sdduvall 
46581369Sdduvall 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
46591369Sdduvall 		(void *)bgep, (void *)ppd));
46601369Sdduvall 
46611369Sdduvall 	regoff = ppd->pp_acc_offset;
46621369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
46631369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
46641369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
46651369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
46661369Sdduvall 	regval = ppd->pp_acc_data;
46671369Sdduvall 
46681369Sdduvall 	switch (ppd->pp_acc_size) {
46691369Sdduvall 	case 1:
46701369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
46711369Sdduvall 		break;
46721369Sdduvall 
46731369Sdduvall 	case 2:
46741369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
46751369Sdduvall 		break;
46761369Sdduvall 
46771369Sdduvall 	case 4:
46781369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
46791369Sdduvall 		break;
46801369Sdduvall 
46811369Sdduvall 	case 8:
46821369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
46831369Sdduvall 		break;
46841369Sdduvall 	}
46851369Sdduvall 	BGE_PCICHK(bgep);
46861369Sdduvall }
46871369Sdduvall 
46881369Sdduvall static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
46891369Sdduvall #pragma	no_inline(bge_chip_peek_mii)
46901369Sdduvall 
46911369Sdduvall static void
46921369Sdduvall bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
46931369Sdduvall {
46941369Sdduvall 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
46951369Sdduvall 		(void *)bgep, (void *)ppd));
46961369Sdduvall 
46971369Sdduvall 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
46981369Sdduvall }
46991369Sdduvall 
47001369Sdduvall static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
47011369Sdduvall #pragma	no_inline(bge_chip_poke_mii)
47021369Sdduvall 
47031369Sdduvall static void
47041369Sdduvall bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
47051369Sdduvall {
47061369Sdduvall 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
47071369Sdduvall 		(void *)bgep, (void *)ppd));
47081369Sdduvall 
47091369Sdduvall 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
47101369Sdduvall }
47111369Sdduvall 
47121369Sdduvall #if	BGE_SEE_IO32
47131369Sdduvall 
47141369Sdduvall static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
47151369Sdduvall #pragma	no_inline(bge_chip_peek_seeprom)
47161369Sdduvall 
47171369Sdduvall static void
47181369Sdduvall bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
47191369Sdduvall {
47201369Sdduvall 	uint32_t data;
47211369Sdduvall 	int err;
47221369Sdduvall 
47231369Sdduvall 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
47241369Sdduvall 		(void *)bgep, (void *)ppd));
47251369Sdduvall 
47261369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
47271369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
47281369Sdduvall }
47291369Sdduvall 
47301369Sdduvall static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
47311369Sdduvall #pragma	no_inline(bge_chip_poke_seeprom)
47321369Sdduvall 
47331369Sdduvall static void
47341369Sdduvall bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
47351369Sdduvall {
47361369Sdduvall 	uint32_t data;
47371369Sdduvall 
47381369Sdduvall 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
47391369Sdduvall 		(void *)bgep, (void *)ppd));
47401369Sdduvall 
47411369Sdduvall 	data = ppd->pp_acc_data;
47421369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
47431369Sdduvall }
47441369Sdduvall #endif	/* BGE_SEE_IO32 */
47451369Sdduvall 
47461369Sdduvall #if	BGE_FLASH_IO32
47471369Sdduvall 
47481369Sdduvall static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
47491369Sdduvall #pragma	no_inline(bge_chip_peek_flash)
47501369Sdduvall 
47511369Sdduvall static void
47521369Sdduvall bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
47531369Sdduvall {
47541369Sdduvall 	uint32_t data;
47551369Sdduvall 	int err;
47561369Sdduvall 
47571369Sdduvall 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
47581369Sdduvall 		(void *)bgep, (void *)ppd));
47591369Sdduvall 
47601369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
47611369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
47621369Sdduvall }
47631369Sdduvall 
47641369Sdduvall static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
47651369Sdduvall #pragma	no_inline(bge_chip_poke_flash)
47661369Sdduvall 
47671369Sdduvall static void
47681369Sdduvall bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
47691369Sdduvall {
47701369Sdduvall 	uint32_t data;
47711369Sdduvall 
47721369Sdduvall 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
47731369Sdduvall 		(void *)bgep, (void *)ppd));
47741369Sdduvall 
47751369Sdduvall 	data = ppd->pp_acc_data;
47761369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
47771369Sdduvall 	    ppd->pp_acc_offset, &data);
47781369Sdduvall }
47791369Sdduvall #endif	/* BGE_FLASH_IO32 */
47801369Sdduvall 
47811369Sdduvall static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
47821369Sdduvall #pragma	no_inline(bge_chip_peek_mem)
47831369Sdduvall 
47841369Sdduvall static void
47851369Sdduvall bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
47861369Sdduvall {
47871369Sdduvall 	uint64_t regval;
47881369Sdduvall 	void *vaddr;
47891369Sdduvall 
47901369Sdduvall 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
47911369Sdduvall 		(void *)bgep, (void *)ppd));
47921369Sdduvall 
47931369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
47941369Sdduvall 
47951369Sdduvall 	switch (ppd->pp_acc_size) {
47961369Sdduvall 	case 1:
47971369Sdduvall 		regval = *(uint8_t *)vaddr;
47981369Sdduvall 		break;
47991369Sdduvall 
48001369Sdduvall 	case 2:
48011369Sdduvall 		regval = *(uint16_t *)vaddr;
48021369Sdduvall 		break;
48031369Sdduvall 
48041369Sdduvall 	case 4:
48051369Sdduvall 		regval = *(uint32_t *)vaddr;
48061369Sdduvall 		break;
48071369Sdduvall 
48081369Sdduvall 	case 8:
48091369Sdduvall 		regval = *(uint64_t *)vaddr;
48101369Sdduvall 		break;
48111369Sdduvall 	}
48121369Sdduvall 
48131369Sdduvall 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
48141369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
48151369Sdduvall 
48161369Sdduvall 	ppd->pp_acc_data = regval;
48171369Sdduvall }
48181369Sdduvall 
48191369Sdduvall static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
48201369Sdduvall #pragma	no_inline(bge_chip_poke_mem)
48211369Sdduvall 
48221369Sdduvall static void
48231369Sdduvall bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
48241369Sdduvall {
48251369Sdduvall 	uint64_t regval;
48261369Sdduvall 	void *vaddr;
48271369Sdduvall 
48281369Sdduvall 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
48291369Sdduvall 		(void *)bgep, (void *)ppd));
48301369Sdduvall 
48311369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
48321369Sdduvall 	regval = ppd->pp_acc_data;
48331369Sdduvall 
48341369Sdduvall 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
48351369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
48361369Sdduvall 
48371369Sdduvall 	switch (ppd->pp_acc_size) {
48381369Sdduvall 	case 1:
48391369Sdduvall 		*(uint8_t *)vaddr = (uint8_t)regval;
48401369Sdduvall 		break;
48411369Sdduvall 
48421369Sdduvall 	case 2:
48431369Sdduvall 		*(uint16_t *)vaddr = (uint16_t)regval;
48441369Sdduvall 		break;
48451369Sdduvall 
48461369Sdduvall 	case 4:
48471369Sdduvall 		*(uint32_t *)vaddr = (uint32_t)regval;
48481369Sdduvall 		break;
48491369Sdduvall 
48501369Sdduvall 	case 8:
48511369Sdduvall 		*(uint64_t *)vaddr = (uint64_t)regval;
48521369Sdduvall 		break;
48531369Sdduvall 	}
48541369Sdduvall }
48551369Sdduvall 
48561369Sdduvall static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
48571369Sdduvall 					struct iocblk *iocp);
48581369Sdduvall #pragma	no_inline(bge_pp_ioctl)
48591369Sdduvall 
48601369Sdduvall static enum ioc_reply
48611369Sdduvall bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
48621369Sdduvall {
48631369Sdduvall 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
48641369Sdduvall 	bge_peekpoke_t *ppd;
48651369Sdduvall 	dma_area_t *areap;
48661369Sdduvall 	uint64_t sizemask;
48671369Sdduvall 	uint64_t mem_va;
48681369Sdduvall 	uint64_t maxoff;
48691369Sdduvall 	boolean_t peek;
48701369Sdduvall 
48711369Sdduvall 	switch (cmd) {
48721369Sdduvall 	default:
48731369Sdduvall 		/* NOTREACHED */
48741369Sdduvall 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
48751369Sdduvall 		return (IOC_INVAL);
48761369Sdduvall 
48771369Sdduvall 	case BGE_PEEK:
48781369Sdduvall 		peek = B_TRUE;
48791369Sdduvall 		break;
48801369Sdduvall 
48811369Sdduvall 	case BGE_POKE:
48821369Sdduvall 		peek = B_FALSE;
48831369Sdduvall 		break;
48841369Sdduvall 	}
48851369Sdduvall 
48861369Sdduvall 	/*
48871369Sdduvall 	 * Validate format of ioctl
48881369Sdduvall 	 */
48891369Sdduvall 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
48901369Sdduvall 		return (IOC_INVAL);
48911369Sdduvall 	if (mp->b_cont == NULL)
48921369Sdduvall 		return (IOC_INVAL);
48931369Sdduvall 	ppd = (bge_peekpoke_t *)mp->b_cont->b_rptr;
48941369Sdduvall 
48951369Sdduvall 	/*
48961369Sdduvall 	 * Validate request parameters
48971369Sdduvall 	 */
48981369Sdduvall 	switch (ppd->pp_acc_space) {
48991369Sdduvall 	default:
49001369Sdduvall 		return (IOC_INVAL);
49011369Sdduvall 
49021369Sdduvall 	case BGE_PP_SPACE_CFG:
49031369Sdduvall 		/*
49041369Sdduvall 		 * Config space
49051369Sdduvall 		 */
49061369Sdduvall 		sizemask = 8|4|2|1;
49071369Sdduvall 		mem_va = 0;
49081369Sdduvall 		maxoff = PCI_CONF_HDR_SIZE;
49091369Sdduvall 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
49101369Sdduvall 		break;
49111369Sdduvall 
49121369Sdduvall 	case BGE_PP_SPACE_REG:
49131369Sdduvall 		/*
49141369Sdduvall 		 * Memory-mapped I/O space
49151369Sdduvall 		 */
49161369Sdduvall 		sizemask = 8|4|2|1;
49171369Sdduvall 		mem_va = 0;
49181369Sdduvall 		maxoff = RIAAR_REGISTER_MAX;
49191369Sdduvall 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
49201369Sdduvall 		break;
49211369Sdduvall 
49221369Sdduvall 	case BGE_PP_SPACE_NIC:
49231369Sdduvall 		/*
49241369Sdduvall 		 * NIC on-chip memory
49251369Sdduvall 		 */
49261369Sdduvall 		sizemask = 8|4|2|1;
49271369Sdduvall 		mem_va = 0;
49281369Sdduvall 		maxoff = MWBAR_ONCHIP_MAX;
49291369Sdduvall 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
49301369Sdduvall 		break;
49311369Sdduvall 
49321369Sdduvall 	case BGE_PP_SPACE_MII:
49331369Sdduvall 		/*
49341369Sdduvall 		 * PHY's MII registers
49351369Sdduvall 		 * NB: all PHY registers are two bytes, but the
49361369Sdduvall 		 * addresses increment in ones (word addressing).
49371369Sdduvall 		 * So we scale the address here, then undo the
49381369Sdduvall 		 * transformation inside the peek/poke functions.
49391369Sdduvall 		 */
49401369Sdduvall 		ppd->pp_acc_offset *= 2;
49411369Sdduvall 		sizemask = 2;
49421369Sdduvall 		mem_va = 0;
49431369Sdduvall 		maxoff = (MII_MAXREG+1)*2;
49441369Sdduvall 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
49451369Sdduvall 		break;
49461369Sdduvall 
49471369Sdduvall #if	BGE_SEE_IO32
49481369Sdduvall 	case BGE_PP_SPACE_SEEPROM:
49491369Sdduvall 		/*
49501369Sdduvall 		 * Attached SEEPROM(s), if any.
49511369Sdduvall 		 * NB: we use the high-order bits of the 'address' as
49521369Sdduvall 		 * a device select to accommodate multiple SEEPROMS,
49531369Sdduvall 		 * If each one is the maximum size (64kbytes), this
49541369Sdduvall 		 * makes them appear contiguous.  Otherwise, there may
49551369Sdduvall 		 * be holes in the mapping.  ENxS doesn't have any
49561369Sdduvall 		 * SEEPROMs anyway ...
49571369Sdduvall 		 */
49581369Sdduvall 		sizemask = 4;
49591369Sdduvall 		mem_va = 0;
49601369Sdduvall 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
49611369Sdduvall 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
49621369Sdduvall 		break;
49631369Sdduvall #endif	/* BGE_SEE_IO32 */
49641369Sdduvall 
49651369Sdduvall #if	BGE_FLASH_IO32
49661369Sdduvall 	case BGE_PP_SPACE_FLASH:
49671369Sdduvall 		/*
49681369Sdduvall 		 * Attached Flash device (if any); a maximum of one device
49691369Sdduvall 		 * is currently supported.  But it can be up to 1MB (unlike
49701369Sdduvall 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
49711369Sdduvall 		 */
49721369Sdduvall 		sizemask = 4;
49731369Sdduvall 		mem_va = 0;
49741369Sdduvall 		maxoff = NVM_FLASH_ADDR_MASK;
49751369Sdduvall 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
49761369Sdduvall 		break;
49771369Sdduvall #endif	/* BGE_FLASH_IO32 */
49781369Sdduvall 
49791369Sdduvall 	case BGE_PP_SPACE_BGE:
49801369Sdduvall 		/*
49811369Sdduvall 		 * BGE data structure!
49821369Sdduvall 		 */
49831369Sdduvall 		sizemask = 8|4|2|1;
49841369Sdduvall 		mem_va = (uintptr_t)bgep;
49851369Sdduvall 		maxoff = sizeof (*bgep);
49861369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
49871369Sdduvall 		break;
49881369Sdduvall 
49891369Sdduvall 	case BGE_PP_SPACE_STATUS:
49901369Sdduvall 	case BGE_PP_SPACE_STATISTICS:
49911369Sdduvall 	case BGE_PP_SPACE_TXDESC:
49921369Sdduvall 	case BGE_PP_SPACE_TXBUFF:
49931369Sdduvall 	case BGE_PP_SPACE_RXDESC:
49941369Sdduvall 	case BGE_PP_SPACE_RXBUFF:
49951369Sdduvall 		/*
49961369Sdduvall 		 * Various DMA_AREAs
49971369Sdduvall 		 */
49981369Sdduvall 		switch (ppd->pp_acc_space) {
49991369Sdduvall 		case BGE_PP_SPACE_TXDESC:
50001369Sdduvall 			areap = &bgep->tx_desc;
50011369Sdduvall 			break;
50021369Sdduvall 		case BGE_PP_SPACE_TXBUFF:
50031369Sdduvall 			areap = &bgep->tx_buff[0];
50041369Sdduvall 			break;
50051369Sdduvall 		case BGE_PP_SPACE_RXDESC:
50061369Sdduvall 			areap = &bgep->rx_desc[0];
50071369Sdduvall 			break;
50081369Sdduvall 		case BGE_PP_SPACE_RXBUFF:
50091369Sdduvall 			areap = &bgep->rx_buff[0];
50101369Sdduvall 			break;
50111369Sdduvall 		case BGE_PP_SPACE_STATUS:
50121369Sdduvall 			areap = &bgep->status_block;
50131369Sdduvall 			break;
50141369Sdduvall 		case BGE_PP_SPACE_STATISTICS:
50151369Sdduvall 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
50161369Sdduvall 				areap = &bgep->statistics;
50171369Sdduvall 			break;
50181369Sdduvall 		}
50191369Sdduvall 
50201369Sdduvall 		sizemask = 8|4|2|1;
50211369Sdduvall 		mem_va = (uintptr_t)areap->mem_va;
50221369Sdduvall 		maxoff = areap->alength;
50231369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
50241369Sdduvall 		break;
50251369Sdduvall 	}
50261369Sdduvall 
50271369Sdduvall 	switch (ppd->pp_acc_size) {
50281369Sdduvall 	default:
50291369Sdduvall 		return (IOC_INVAL);
50301369Sdduvall 
50311369Sdduvall 	case 8:
50321369Sdduvall 	case 4:
50331369Sdduvall 	case 2:
50341369Sdduvall 	case 1:
50351369Sdduvall 		if ((ppd->pp_acc_size & sizemask) == 0)
50361369Sdduvall 			return (IOC_INVAL);
50371369Sdduvall 		break;
50381369Sdduvall 	}
50391369Sdduvall 
50401369Sdduvall 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
50411369Sdduvall 		return (IOC_INVAL);
50421369Sdduvall 
50431369Sdduvall 	if (ppd->pp_acc_offset >= maxoff)
50441369Sdduvall 		return (IOC_INVAL);
50451369Sdduvall 
50461369Sdduvall 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
50471369Sdduvall 		return (IOC_INVAL);
50481369Sdduvall 
50491369Sdduvall 	/*
50501369Sdduvall 	 * All OK - go do it!
50511369Sdduvall 	 */
50521369Sdduvall 	ppd->pp_acc_offset += mem_va;
50531369Sdduvall 	(*ppfn)(bgep, ppd);
50541369Sdduvall 	return (peek ? IOC_REPLY : IOC_ACK);
50551369Sdduvall }
50561369Sdduvall 
50571369Sdduvall static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
50581369Sdduvall 					struct iocblk *iocp);
50591369Sdduvall #pragma	no_inline(bge_diag_ioctl)
50601369Sdduvall 
50611369Sdduvall static enum ioc_reply
50621369Sdduvall bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
50631369Sdduvall {
50641369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
50651369Sdduvall 
50661369Sdduvall 	switch (cmd) {
50671369Sdduvall 	default:
50681369Sdduvall 		/* NOTREACHED */
50691369Sdduvall 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
50701369Sdduvall 		return (IOC_INVAL);
50711369Sdduvall 
50721369Sdduvall 	case BGE_DIAG:
50731369Sdduvall 		/*
50741369Sdduvall 		 * Currently a no-op
50751369Sdduvall 		 */
50761369Sdduvall 		return (IOC_ACK);
50771369Sdduvall 
50781369Sdduvall 	case BGE_PEEK:
50791369Sdduvall 	case BGE_POKE:
50801369Sdduvall 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
50811369Sdduvall 
50821369Sdduvall 	case BGE_PHY_RESET:
50831369Sdduvall 		return (IOC_RESTART_ACK);
50841369Sdduvall 
50851369Sdduvall 	case BGE_SOFT_RESET:
50861369Sdduvall 	case BGE_HARD_RESET:
50871369Sdduvall 		/*
50881369Sdduvall 		 * Reset and reinitialise the 570x hardware
50891369Sdduvall 		 */
50901865Sdilpreet 		(void) bge_restart(bgep, cmd == BGE_HARD_RESET);
50911369Sdduvall 		return (IOC_ACK);
50921369Sdduvall 	}
50931369Sdduvall 
50941369Sdduvall 	/* NOTREACHED */
50951369Sdduvall }
50961369Sdduvall 
50971369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
50981369Sdduvall 
50991369Sdduvall static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51001369Sdduvall 				    struct iocblk *iocp);
51011369Sdduvall #pragma	no_inline(bge_mii_ioctl)
51021369Sdduvall 
51031369Sdduvall static enum ioc_reply
51041369Sdduvall bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51051369Sdduvall {
51061369Sdduvall 	struct bge_mii_rw *miirwp;
51071369Sdduvall 
51081369Sdduvall 	/*
51091369Sdduvall 	 * Validate format of ioctl
51101369Sdduvall 	 */
51111369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
51121369Sdduvall 		return (IOC_INVAL);
51131369Sdduvall 	if (mp->b_cont == NULL)
51141369Sdduvall 		return (IOC_INVAL);
51151369Sdduvall 	miirwp = (struct bge_mii_rw *)mp->b_cont->b_rptr;
51161369Sdduvall 
51171369Sdduvall 	/*
51181369Sdduvall 	 * Validate request parameters ...
51191369Sdduvall 	 */
51201369Sdduvall 	if (miirwp->mii_reg > MII_MAXREG)
51211369Sdduvall 		return (IOC_INVAL);
51221369Sdduvall 
51231369Sdduvall 	switch (cmd) {
51241369Sdduvall 	default:
51251369Sdduvall 		/* NOTREACHED */
51261369Sdduvall 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
51271369Sdduvall 		return (IOC_INVAL);
51281369Sdduvall 
51291369Sdduvall 	case BGE_MII_READ:
51301369Sdduvall 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
51311369Sdduvall 		return (IOC_REPLY);
51321369Sdduvall 
51331369Sdduvall 	case BGE_MII_WRITE:
51341369Sdduvall 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
51351369Sdduvall 		return (IOC_ACK);
51361369Sdduvall 	}
51371369Sdduvall 
51381369Sdduvall 	/* NOTREACHED */
51391369Sdduvall }
51401369Sdduvall 
51411369Sdduvall #if	BGE_SEE_IO32
51421369Sdduvall 
51431369Sdduvall static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51441369Sdduvall 				    struct iocblk *iocp);
51451369Sdduvall #pragma	no_inline(bge_see_ioctl)
51461369Sdduvall 
51471369Sdduvall static enum ioc_reply
51481369Sdduvall bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51491369Sdduvall {
51501369Sdduvall 	struct bge_see_rw *seerwp;
51511369Sdduvall 
51521369Sdduvall 	/*
51531369Sdduvall 	 * Validate format of ioctl
51541369Sdduvall 	 */
51551369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
51561369Sdduvall 		return (IOC_INVAL);
51571369Sdduvall 	if (mp->b_cont == NULL)
51581369Sdduvall 		return (IOC_INVAL);
51591369Sdduvall 	seerwp = (struct bge_see_rw *)mp->b_cont->b_rptr;
51601369Sdduvall 
51611369Sdduvall 	/*
51621369Sdduvall 	 * Validate request parameters ...
51631369Sdduvall 	 */
51641369Sdduvall 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
51651369Sdduvall 		return (IOC_INVAL);
51661369Sdduvall 
51671369Sdduvall 	switch (cmd) {
51681369Sdduvall 	default:
51691369Sdduvall 		/* NOTREACHED */
51701369Sdduvall 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
51711369Sdduvall 		return (IOC_INVAL);
51721369Sdduvall 
51731369Sdduvall 	case BGE_SEE_READ:
51741369Sdduvall 	case BGE_SEE_WRITE:
51751369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
51761369Sdduvall 		    seerwp->see_addr, &seerwp->see_data);
51771369Sdduvall 		return (IOC_REPLY);
51781369Sdduvall 	}
51791369Sdduvall 
51801369Sdduvall 	/* NOTREACHED */
51811369Sdduvall }
51821369Sdduvall 
51831369Sdduvall #endif	/* BGE_SEE_IO32 */
51841369Sdduvall 
51851369Sdduvall #if	BGE_FLASH_IO32
51861369Sdduvall 
51871369Sdduvall static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51881369Sdduvall 				    struct iocblk *iocp);
51891369Sdduvall #pragma	no_inline(bge_flash_ioctl)
51901369Sdduvall 
51911369Sdduvall static enum ioc_reply
51921369Sdduvall bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51931369Sdduvall {
51941369Sdduvall 	struct bge_flash_rw *flashrwp;
51951369Sdduvall 
51961369Sdduvall 	/*
51971369Sdduvall 	 * Validate format of ioctl
51981369Sdduvall 	 */
51991369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
52001369Sdduvall 		return (IOC_INVAL);
52011369Sdduvall 	if (mp->b_cont == NULL)
52021369Sdduvall 		return (IOC_INVAL);
52031369Sdduvall 	flashrwp = (struct bge_flash_rw *)mp->b_cont->b_rptr;
52041369Sdduvall 
52051369Sdduvall 	/*
52061369Sdduvall 	 * Validate request parameters ...
52071369Sdduvall 	 */
52081369Sdduvall 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
52091369Sdduvall 		return (IOC_INVAL);
52101369Sdduvall 
52111369Sdduvall 	switch (cmd) {
52121369Sdduvall 	default:
52131369Sdduvall 		/* NOTREACHED */
52141369Sdduvall 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
52151369Sdduvall 		return (IOC_INVAL);
52161369Sdduvall 
52171369Sdduvall 	case BGE_FLASH_READ:
52181369Sdduvall 	case BGE_FLASH_WRITE:
52191369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
52201369Sdduvall 		    flashrwp->flash_addr, &flashrwp->flash_data);
52211369Sdduvall 		return (IOC_REPLY);
52221369Sdduvall 	}
52231369Sdduvall 
52241369Sdduvall 	/* NOTREACHED */
52251369Sdduvall }
52261369Sdduvall 
52271369Sdduvall #endif	/* BGE_FLASH_IO32 */
52281369Sdduvall 
52291369Sdduvall enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
52301369Sdduvall 				struct iocblk *iocp);
52311369Sdduvall #pragma	no_inline(bge_chip_ioctl)
52321369Sdduvall 
52331369Sdduvall enum ioc_reply
52341369Sdduvall bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
52351369Sdduvall {
52361369Sdduvall 	int cmd;
52371369Sdduvall 
52381369Sdduvall 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
52391369Sdduvall 		(void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
52401369Sdduvall 
52411369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
52421369Sdduvall 
52431369Sdduvall 	cmd = iocp->ioc_cmd;
52441369Sdduvall 	switch (cmd) {
52451369Sdduvall 	default:
52461369Sdduvall 		/* NOTREACHED */
52471369Sdduvall 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
52481369Sdduvall 		return (IOC_INVAL);
52491369Sdduvall 
52501369Sdduvall 	case BGE_DIAG:
52511369Sdduvall 	case BGE_PEEK:
52521369Sdduvall 	case BGE_POKE:
52531369Sdduvall 	case BGE_PHY_RESET:
52541369Sdduvall 	case BGE_SOFT_RESET:
52551369Sdduvall 	case BGE_HARD_RESET:
52561369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
52571369Sdduvall 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
52581369Sdduvall #else
52591369Sdduvall 		return (IOC_INVAL);
52601369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
52611369Sdduvall 
52621369Sdduvall 	case BGE_MII_READ:
52631369Sdduvall 	case BGE_MII_WRITE:
52641369Sdduvall 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
52651369Sdduvall 
52661369Sdduvall #if	BGE_SEE_IO32
52671369Sdduvall 	case BGE_SEE_READ:
52681369Sdduvall 	case BGE_SEE_WRITE:
52691369Sdduvall 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
52701369Sdduvall #endif	/* BGE_SEE_IO32 */
52711369Sdduvall 
52721369Sdduvall #if	BGE_FLASH_IO32
52731369Sdduvall 	case BGE_FLASH_READ:
52741369Sdduvall 	case BGE_FLASH_WRITE:
52751369Sdduvall 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
52761369Sdduvall #endif	/* BGE_FLASH_IO32 */
52771369Sdduvall 	}
52781369Sdduvall 
52791369Sdduvall 	/* NOTREACHED */
52801369Sdduvall }
52811369Sdduvall 
52821369Sdduvall void
52831369Sdduvall bge_chip_blank(void *arg, time_t ticks, uint_t count)
52841369Sdduvall {
52851369Sdduvall 	bge_t *bgep = arg;
52861369Sdduvall 
52871865Sdilpreet 	mutex_enter(bgep->genlock);
52881369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
52891369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
52901865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
52911865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
52921865Sdilpreet 	mutex_exit(bgep->genlock);
52931369Sdduvall }
52941408Srandyf 
52951408Srandyf #ifdef BGE_IPMI_ASF
52961408Srandyf 
52971408Srandyf uint32_t
52981408Srandyf bge_nic_read32(bge_t *bgep, bge_regno_t addr)
52991408Srandyf {
53001408Srandyf 	uint32_t data;
53011408Srandyf 
53021408Srandyf 	if (!bgep->asf_wordswapped) {
53031408Srandyf 		/* a workaround word swap error */
53041408Srandyf 		if (addr & 4)
53051408Srandyf 			addr = addr - 4;
53061408Srandyf 		else
53071408Srandyf 			addr = addr + 4;
53081408Srandyf 	}
53091408Srandyf 
53101408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
53111408Srandyf 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
53121408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
53131408Srandyf 
53141408Srandyf 	return (data);
53151408Srandyf }
53161408Srandyf 
53171408Srandyf 
53181408Srandyf void
53191408Srandyf bge_asf_update_status(bge_t *bgep)
53201408Srandyf {
53211408Srandyf 	uint32_t event;
53221408Srandyf 
53231408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
53241408Srandyf 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
53251408Srandyf 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
53261408Srandyf 
53271408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
53281408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
53291408Srandyf }
53301408Srandyf 
53311408Srandyf 
53321408Srandyf /*
53331408Srandyf  * The driver is supposed to notify ASF that the OS is still running
53341408Srandyf  * every three seconds, otherwise the management server may attempt
53351408Srandyf  * to reboot the machine.  If it hasn't actually failed, this is
53362135Szh199473  * not a desirable result.  However, this isn't running as a real-time
53371408Srandyf  * thread, and even if it were, it might not be able to generate the
53381408Srandyf  * heartbeat in a timely manner due to system load.  As it isn't a
53391408Srandyf  * significant strain on the machine, we will set the interval to half
53401408Srandyf  * of the required value.
53411408Srandyf  */
53421408Srandyf void
53431865Sdilpreet bge_asf_heartbeat(void *arg)
53441408Srandyf {
53451865Sdilpreet 	bge_t *bgep = (bge_t *)arg;
53461865Sdilpreet 
53471865Sdilpreet 	mutex_enter(bgep->genlock);
53481408Srandyf 	bge_asf_update_status((bge_t *)bgep);
53491865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
53501865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
53511865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
53521865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
53531865Sdilpreet 	mutex_exit(bgep->genlock);
53541408Srandyf 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
53551408Srandyf 		drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
53561408Srandyf }
53571408Srandyf 
53581408Srandyf 
53591408Srandyf void
53601408Srandyf bge_asf_stop_timer(bge_t *bgep)
53611408Srandyf {
53621408Srandyf 	timeout_id_t tmp_id = 0;
53631408Srandyf 
53641408Srandyf 	while ((bgep->asf_timeout_id != 0) &&
53651408Srandyf 		(tmp_id != bgep->asf_timeout_id)) {
53661408Srandyf 		tmp_id = bgep->asf_timeout_id;
53671408Srandyf 		(void) untimeout(tmp_id);
53681408Srandyf 	}
53691408Srandyf 	bgep->asf_timeout_id = 0;
53701408Srandyf }
53711408Srandyf 
53721408Srandyf 
53731408Srandyf 
53741408Srandyf /*
53752135Szh199473  * This function should be placed at the earliest position of bge_attach().
53761408Srandyf  */
53771408Srandyf void
53781408Srandyf bge_asf_get_config(bge_t *bgep)
53791408Srandyf {
53801408Srandyf 	uint32_t nicsig;
53811408Srandyf 	uint32_t niccfg;
53821408Srandyf 
53831408Srandyf 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
53841408Srandyf 	if (nicsig == BGE_NIC_DATA_SIG) {
53851408Srandyf 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
53861408Srandyf 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
53871408Srandyf 			/*
53881408Srandyf 			 * Here, we don't consider BAXTER, because BGE haven't
53891408Srandyf 			 * supported BAXTER (that is 5752). Also, as I know,
53901408Srandyf 			 * BAXTER doesn't support ASF feature.
53911408Srandyf 			 */
53921408Srandyf 			bgep->asf_enabled = B_TRUE;
53931408Srandyf 		else
53941408Srandyf 			bgep->asf_enabled = B_FALSE;
53951408Srandyf 	} else
53961408Srandyf 		bgep->asf_enabled = B_FALSE;
53971408Srandyf }
53981408Srandyf 
53991408Srandyf 
54001408Srandyf void
54011408Srandyf bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
54021408Srandyf {
54031408Srandyf 	uint32_t tries;
54041408Srandyf 	uint32_t event;
54051408Srandyf 
54061408Srandyf 	ASSERT(bgep->asf_enabled);
54071408Srandyf 
54081408Srandyf 	/* Issues "pause firmware" command and wait for ACK */
54091408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
54101408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
54111408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
54121408Srandyf 
54131408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
54141408Srandyf 	tries = 0;
54151408Srandyf 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
54161408Srandyf 		drv_usecwait(1);
54171408Srandyf 		tries ++;
54181408Srandyf 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
54191408Srandyf 	}
54201408Srandyf 
54211408Srandyf 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
54221408Srandyf 		BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
54231408Srandyf 
54241408Srandyf 	if (bgep->asf_newhandshake) {
54251408Srandyf 		switch (mode) {
54261408Srandyf 		case BGE_INIT_RESET:
54271408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54281408Srandyf 				BGE_DRV_STATE_START);
54291408Srandyf 			break;
54301408Srandyf 		case BGE_SHUTDOWN_RESET:
54311408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54321408Srandyf 				BGE_DRV_STATE_UNLOAD);
54331408Srandyf 			break;
54341408Srandyf 		case BGE_SUSPEND_RESET:
54351408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54361408Srandyf 				BGE_DRV_STATE_SUSPEND);
54371408Srandyf 			break;
54381408Srandyf 		default:
54391408Srandyf 			break;
54401408Srandyf 		}
54411408Srandyf 	}
54421408Srandyf }
54431408Srandyf 
54441408Srandyf 
54451408Srandyf void
54461408Srandyf bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
54471408Srandyf {
54481408Srandyf 	switch (mode) {
54491408Srandyf 	case BGE_INIT_RESET:
54501408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54511408Srandyf 			BGE_DRV_STATE_START);
54521408Srandyf 		break;
54531408Srandyf 	case BGE_SHUTDOWN_RESET:
54541408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54551408Srandyf 			BGE_DRV_STATE_UNLOAD);
54561408Srandyf 		break;
54571408Srandyf 	case BGE_SUSPEND_RESET:
54581408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54591408Srandyf 			BGE_DRV_STATE_SUSPEND);
54601408Srandyf 		break;
54611408Srandyf 	default:
54621408Srandyf 		break;
54631408Srandyf 	}
54641408Srandyf }
54651408Srandyf 
54661408Srandyf 
54671408Srandyf void
54681408Srandyf bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
54691408Srandyf {
54701408Srandyf 	switch (mode) {
54711408Srandyf 	case BGE_INIT_RESET:
54721408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54731408Srandyf 			BGE_DRV_STATE_START_DONE);
54741408Srandyf 		break;
54751408Srandyf 	case BGE_SHUTDOWN_RESET:
54761408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54771408Srandyf 			BGE_DRV_STATE_UNLOAD_DONE);
54781408Srandyf 		break;
54791408Srandyf 	default:
54801408Srandyf 		break;
54811408Srandyf 	}
54821408Srandyf }
54831408Srandyf 
54841408Srandyf #endif /* BGE_IPMI_ASF */
5485