xref: /onnv-gate/usr/src/uts/common/io/bge/bge_chip2.c (revision 3534:ca275a2f676c)
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 /*
551369Sdduvall  * Property names
561369Sdduvall  */
571369Sdduvall static char knownids_propname[] = "bge-known-subsystems";
581369Sdduvall 
591369Sdduvall /*
601369Sdduvall  * Patchable globals:
611369Sdduvall  *
621369Sdduvall  *	bge_autorecover
631369Sdduvall  *		Enables/disables automatic recovery after fault detection
641369Sdduvall  *
651369Sdduvall  *	bge_mlcr_default
661369Sdduvall  *		Value to program into the MLCR; controls the chip's GPIO pins
671369Sdduvall  *
681369Sdduvall  *	bge_dma_{rd,wr}prio
691369Sdduvall  *		Relative priorities of DMA reads & DMA writes respectively.
701369Sdduvall  *		These may each be patched to any value 0-3.  Equal values
711369Sdduvall  *		will give "fair" (round-robin) arbitration for PCI access.
721369Sdduvall  *		Unequal values will give one or the other function priority.
731369Sdduvall  *
741369Sdduvall  *	bge_dma_rwctrl
751369Sdduvall  *		Value to put in the Read/Write DMA control register.  See
761369Sdduvall  *	        the Broadcom PRM for things you can fiddle with in this
771369Sdduvall  *		register ...
781369Sdduvall  *
791369Sdduvall  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
801369Sdduvall  *		Send/receive interrupt coalescing parameters.  Counts are
811369Sdduvall  *		#s of descriptors, ticks are in microseconds.  *norm* values
821369Sdduvall  *		apply between status updates/interrupts; the *intr* values
831369Sdduvall  *		refer to the 'during-interrupt' versions - see the PRM.
841369Sdduvall  *
851369Sdduvall  *		NOTE: these values have been determined by measurement. They
861369Sdduvall  *		differ significantly from the values recommended in the PRM.
871369Sdduvall  */
881369Sdduvall static uint32_t bge_autorecover = 1;
891369Sdduvall static uint32_t bge_mlcr_default = MLCR_DEFAULT;
901369Sdduvall static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
911369Sdduvall 
921369Sdduvall static uint32_t bge_dma_rdprio = 1;
931369Sdduvall static uint32_t bge_dma_wrprio = 0;
941369Sdduvall static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
951369Sdduvall static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
961369Sdduvall static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
971369Sdduvall static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
981369Sdduvall 
991369Sdduvall uint32_t bge_rx_ticks_norm = 128;
1001369Sdduvall uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
1011369Sdduvall uint32_t bge_rx_count_norm = 8;
1021369Sdduvall uint32_t bge_tx_count_norm = 128;
1031369Sdduvall 
1041369Sdduvall static uint32_t bge_rx_ticks_intr = 128;
1051369Sdduvall static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
1061369Sdduvall static uint32_t bge_rx_count_intr = 2;
1071369Sdduvall static uint32_t bge_tx_count_intr = 0;
1081369Sdduvall 
1091369Sdduvall /*
1101369Sdduvall  * Memory pool configuration parameters.
1111369Sdduvall  *
1121369Sdduvall  * These are generally specific to each member of the chip family, since
1131369Sdduvall  * each one may have a different memory size/configuration.
1141369Sdduvall  *
1151369Sdduvall  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
1161369Sdduvall  * the driver from programming the various registers; instead they are left
1171369Sdduvall  * at their hardware defaults.  This is the preferred option for later chips
1181369Sdduvall  * (5705+), whereas the older chips *required* these registers to be set,
1191369Sdduvall  * since the h/w default was 0 ;-(
1201369Sdduvall  */
1211369Sdduvall static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
1221369Sdduvall static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
1231369Sdduvall static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
1241369Sdduvall static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
1251369Sdduvall static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
1261369Sdduvall static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
1271369Sdduvall static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
1281369Sdduvall static uint32_t bge_mbuf_pool_len_5721	= 0;
1291369Sdduvall 
1301369Sdduvall /*
1311369Sdduvall  * Various high and low water marks, thresholds, etc ...
1321369Sdduvall  *
1331369Sdduvall  * Note: these are taken from revision 7 of the PRM, and some are different
1341369Sdduvall  * from both the values in earlier PRMs *and* those determined experimentally
1351369Sdduvall  * and used in earlier versions of this driver ...
1361369Sdduvall  */
1371369Sdduvall static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
1381369Sdduvall static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
1391369Sdduvall static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
1401369Sdduvall 
1411369Sdduvall static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
1421369Sdduvall static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
1431369Sdduvall static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
1441369Sdduvall 
1451369Sdduvall static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
1461369Sdduvall static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
1471369Sdduvall static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
1481369Sdduvall 
1491369Sdduvall static uint32_t	bge_watchdog_count	= 1 << 16;
1501369Sdduvall static uint16_t bge_dma_miss_limit	= 20;
1511369Sdduvall 
1521369Sdduvall static uint32_t bge_stop_start_on_sync	= 0;
1531369Sdduvall 
1541369Sdduvall boolean_t bge_jumbo_enable		= B_TRUE;
1551369Sdduvall static uint32_t bge_default_jumbo_size	= BGE_JUMBO_BUFF_SIZE;
1561369Sdduvall 
1571369Sdduvall /*
1581369Sdduvall  * ========== Low-level chip & ring buffer manipulation ==========
1591369Sdduvall  */
1601369Sdduvall 
1611369Sdduvall #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
1621369Sdduvall 
1631369Sdduvall 
1641369Sdduvall /*
1651369Sdduvall  * Config space read-modify-write routines
1661369Sdduvall  */
1671369Sdduvall 
1681369Sdduvall #if	BGE_CFG_IO8
1691369Sdduvall 
1701369Sdduvall /*
1711369Sdduvall  * 8- and 16-bit set/clr operations are not used; all the config registers
1721369Sdduvall  * that we need to do bit-twiddling on are 32 bits wide.  I'll leave the
1731369Sdduvall  * code here, though, in case we ever find that we do want it after all ...
1741369Sdduvall  */
1751369Sdduvall 
1761369Sdduvall static void bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1771369Sdduvall #pragma	inline(bge_cfg_set8)
1781369Sdduvall 
1791369Sdduvall static void
1801369Sdduvall bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
1811369Sdduvall {
1821369Sdduvall 	uint8_t regval;
1831369Sdduvall 
1841369Sdduvall 	BGE_TRACE(("bge_cfg_set8($%p, 0x%lx, 0x%x)",
1851369Sdduvall 		(void *)bgep, regno, bits));
1861369Sdduvall 
1871369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
1881369Sdduvall 
1891369Sdduvall 	BGE_DEBUG(("bge_cfg_set8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
1901369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
1911369Sdduvall 
1921369Sdduvall 	regval |= bits;
1931369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
1941369Sdduvall }
1951369Sdduvall 
1961369Sdduvall static void bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1971369Sdduvall #pragma	inline(bge_cfg_clr8)
1981369Sdduvall 
1991369Sdduvall static void
2001369Sdduvall bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
2011369Sdduvall {
2021369Sdduvall 	uint8_t regval;
2031369Sdduvall 
2041369Sdduvall 	BGE_TRACE(("bge_cfg_clr8($%p, 0x%lx, 0x%x)",
2051369Sdduvall 		(void *)bgep, regno, bits));
2061369Sdduvall 
2071369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
2081369Sdduvall 
2091369Sdduvall 	BGE_DEBUG(("bge_cfg_clr8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2101369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2111369Sdduvall 
2121369Sdduvall 	regval &= ~bits;
2131369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
2141369Sdduvall }
2151369Sdduvall 
2161369Sdduvall static void bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2171369Sdduvall #pragma	inline(bge_cfg_set16)
2181369Sdduvall 
2191369Sdduvall static void
2201369Sdduvall bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2211369Sdduvall {
2221369Sdduvall 	uint16_t regval;
2231369Sdduvall 
2241369Sdduvall 	BGE_TRACE(("bge_cfg_set16($%p, 0x%lx, 0x%x)",
2251369Sdduvall 		(void *)bgep, regno, bits));
2261369Sdduvall 
2271369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2281369Sdduvall 
2291369Sdduvall 	BGE_DEBUG(("bge_cfg_set16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2301369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2311369Sdduvall 
2321369Sdduvall 	regval |= bits;
2331369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2341369Sdduvall }
2351369Sdduvall 
2361369Sdduvall static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2371369Sdduvall #pragma	inline(bge_cfg_clr16)
2381369Sdduvall 
2391369Sdduvall static void
2401369Sdduvall bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2411369Sdduvall {
2421369Sdduvall 	uint16_t regval;
2431369Sdduvall 
2441369Sdduvall 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
2451369Sdduvall 		(void *)bgep, regno, bits));
2461369Sdduvall 
2471369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2481369Sdduvall 
2491369Sdduvall 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2501369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2511369Sdduvall 
2521369Sdduvall 	regval &= ~bits;
2531369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2541369Sdduvall }
2551369Sdduvall 
2561369Sdduvall #endif	/* BGE_CFG_IO8 */
2571369Sdduvall 
2581369Sdduvall static void bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2591369Sdduvall #pragma	inline(bge_cfg_set32)
2601369Sdduvall 
2611369Sdduvall static void
2621369Sdduvall bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2631369Sdduvall {
2641369Sdduvall 	uint32_t regval;
2651369Sdduvall 
2661369Sdduvall 	BGE_TRACE(("bge_cfg_set32($%p, 0x%lx, 0x%x)",
2671369Sdduvall 		(void *)bgep, regno, bits));
2681369Sdduvall 
2691369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2701369Sdduvall 
2711369Sdduvall 	BGE_DEBUG(("bge_cfg_set32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2721369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2731369Sdduvall 
2741369Sdduvall 	regval |= bits;
2751369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2761369Sdduvall }
2771369Sdduvall 
2781369Sdduvall static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2791369Sdduvall #pragma	inline(bge_cfg_clr32)
2801369Sdduvall 
2811369Sdduvall static void
2821369Sdduvall bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2831369Sdduvall {
2841369Sdduvall 	uint32_t regval;
2851369Sdduvall 
2861369Sdduvall 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
2871369Sdduvall 		(void *)bgep, regno, bits));
2881369Sdduvall 
2891369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2901369Sdduvall 
2911369Sdduvall 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2921369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2931369Sdduvall 
2941369Sdduvall 	regval &= ~bits;
2951369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2961369Sdduvall }
2971369Sdduvall 
2981369Sdduvall #if	BGE_IND_IO32
2991369Sdduvall 
3001369Sdduvall /*
3011369Sdduvall  * Indirect access to registers & RISC scratchpads, using config space
3021369Sdduvall  * accesses only.
3031369Sdduvall  *
3041369Sdduvall  * This isn't currently used, but someday we might want to use it for
3051369Sdduvall  * restoring the Subsystem Device/Vendor registers (which aren't directly
3061369Sdduvall  * writable in Config Space), or for downloading firmware into the RISCs
3071369Sdduvall  *
3081369Sdduvall  * In any case there are endian issues to be resolved before this code is
3091369Sdduvall  * enabled; the bizarre way that bytes get twisted by this chip AND by
3101369Sdduvall  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
3111369Sdduvall  * it's been thoroughly tested for all access sizes on all supported
3121369Sdduvall  * architectures (SPARC *and* x86!).
3131369Sdduvall  */
3141369Sdduvall static uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
3151369Sdduvall #pragma	inline(bge_ind_get32)
3161369Sdduvall 
3171369Sdduvall static uint32_t
3181369Sdduvall bge_ind_get32(bge_t *bgep, bge_regno_t regno)
3191369Sdduvall {
3201369Sdduvall 	uint32_t val;
3211369Sdduvall 
3221369Sdduvall 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
3231369Sdduvall 
3241369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3251369Sdduvall 
3261369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3271369Sdduvall 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
3281369Sdduvall 
3291369Sdduvall 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
3301369Sdduvall 		(void *)bgep, regno, val));
3311369Sdduvall 
3321369Sdduvall 	return (val);
3331369Sdduvall }
3341369Sdduvall 
3351369Sdduvall static void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
3361369Sdduvall #pragma	inline(bge_ind_put32)
3371369Sdduvall 
3381369Sdduvall static void
3391369Sdduvall bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
3401369Sdduvall {
3411369Sdduvall 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
3421369Sdduvall 		(void *)bgep, regno, val));
3431369Sdduvall 
3441369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3451369Sdduvall 
3461369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3471369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
3481369Sdduvall }
3491369Sdduvall 
3501369Sdduvall #endif	/* BGE_IND_IO32 */
3511369Sdduvall 
3521369Sdduvall #if	BGE_DEBUGGING
3531369Sdduvall 
3541369Sdduvall static void bge_pci_check(bge_t *bgep);
3551369Sdduvall #pragma	no_inline(bge_pci_check)
3561369Sdduvall 
3571369Sdduvall static void
3581369Sdduvall bge_pci_check(bge_t *bgep)
3591369Sdduvall {
3601369Sdduvall 	uint16_t pcistatus;
3611369Sdduvall 
3621369Sdduvall 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
3631369Sdduvall 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
3641369Sdduvall 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
3651369Sdduvall 			(void *)bgep, pcistatus));
3661369Sdduvall }
3671369Sdduvall 
3681369Sdduvall #endif	/* BGE_DEBUGGING */
3691369Sdduvall 
3701369Sdduvall /*
3711369Sdduvall  * Perform first-stage chip (re-)initialisation, using only config-space
3721369Sdduvall  * accesses:
3731369Sdduvall  *
3741369Sdduvall  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
3751369Sdduvall  *   returning the data in the structure pointed to by <idp>.
3761369Sdduvall  * + Configure the target-mode endianness (swap) options.
3771369Sdduvall  * + Disable interrupts and enable Memory Space accesses.
3781369Sdduvall  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
3791369Sdduvall  *
3801369Sdduvall  * This sequence is adapted from Broadcom document 570X-PG102-R,
3811369Sdduvall  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
3821369Sdduvall  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
3831369Sdduvall  *
3841369Sdduvall  * This function MUST be called before any non-config-space accesses
3851369Sdduvall  * are made; on this first call <enable_dma> is B_FALSE, and it
3861369Sdduvall  * effectively performs steps 3-1(!) of the initialisation sequence
3871369Sdduvall  * (the rest are not required but should be harmless).
3881369Sdduvall  *
3892135Szh199473  * It MUST also be called after a chip reset, as this disables
3901369Sdduvall  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
3911369Sdduvall  * it is effectively performing steps 6-8.
3921369Sdduvall  */
3931369Sdduvall void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
3941369Sdduvall #pragma	no_inline(bge_chip_cfg_init)
3951369Sdduvall 
3961369Sdduvall void
3971369Sdduvall bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
3981369Sdduvall {
3991369Sdduvall 	ddi_acc_handle_t handle;
4001369Sdduvall 	uint16_t command;
4011369Sdduvall 	uint32_t mhcr;
4021369Sdduvall 	uint16_t value16;
4031369Sdduvall 	int i;
4041369Sdduvall 
4051369Sdduvall 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
4061369Sdduvall 		(void *)bgep, (void *)cidp, enable_dma));
4071369Sdduvall 
4081369Sdduvall 	/*
4091369Sdduvall 	 * Step 3: save PCI cache line size and subsystem vendor ID
4101369Sdduvall 	 *
4111369Sdduvall 	 * Read all the config-space registers that characterise the
4121369Sdduvall 	 * chip, specifically vendor/device/revision/subsystem vendor
4131369Sdduvall 	 * and subsystem device id.  We expect (but don't check) that
4141369Sdduvall 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
4151369Sdduvall 	 *
4162135Szh199473 	 * Also save all bus-transaction related registers (cache-line
4171369Sdduvall 	 * size, bus-grant/latency parameters, etc).  Some of these are
4181369Sdduvall 	 * cleared by reset, so we'll have to restore them later.  This
4191369Sdduvall 	 * comes from the Broadcom document 570X-PG102-R ...
4201369Sdduvall 	 *
4211369Sdduvall 	 * Note: Broadcom document 570X-PG102-R seems to be in error
4221369Sdduvall 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
4231369Sdduvall 	 * Subsystem (Device) ID registers, which are the opposite way
4241369Sdduvall 	 * round according to the PCI standard.  For good measure, we
4251369Sdduvall 	 * save/restore both anyway.
4261369Sdduvall 	 */
4271369Sdduvall 	handle = bgep->cfg_handle;
4281369Sdduvall 
4291369Sdduvall 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
4301369Sdduvall 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
4311369Sdduvall 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
4321369Sdduvall 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
4331369Sdduvall 
4341369Sdduvall 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
4351369Sdduvall 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
4361369Sdduvall 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
4371369Sdduvall 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
4381369Sdduvall 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
4391369Sdduvall 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
4401369Sdduvall 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
4411369Sdduvall 
4421369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
4431369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
4441369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
4451369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
4461369Sdduvall 		cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
4471369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
4481369Sdduvall 		cidp->vendor, cidp->device, cidp->revision));
4491369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
4501369Sdduvall 		cidp->subven, cidp->subdev, cidp->asic_rev));
4511369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
4521369Sdduvall 		cidp->clsize, cidp->latency, cidp->command));
4531369Sdduvall 
4541369Sdduvall 	/*
4551369Sdduvall 	 * Step 2 (also step 6): disable and clear interrupts.
4561369Sdduvall 	 * Steps 11-13: configure PIO endianness options, and enable
4571369Sdduvall 	 * indirect register access.  We'll also select any other
4582135Szh199473 	 * options controlled by the MHCR (e.g. tagged status, mask
4591369Sdduvall 	 * interrupt mode) at this stage ...
4601369Sdduvall 	 *
4611369Sdduvall 	 * Note: internally, the chip is 64-bit and BIG-endian, but
4621369Sdduvall 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
4631369Sdduvall 	 * it normally swaps bytes around at the PCI interface.
4641369Sdduvall 	 * However, the PCI host bridge on SPARC systems normally
4651369Sdduvall 	 * swaps the byte lanes around too, since SPARCs are also
4661369Sdduvall 	 * BIG-endian.  So it turns out that on SPARC, the right
4671369Sdduvall 	 * option is to tell the chip to swap (and the host bridge
4681369Sdduvall 	 * will swap back again), whereas on x86 we ask the chip
4691369Sdduvall 	 * NOT to swap, so the natural little-endianness of the
4701369Sdduvall 	 * PCI bus is assumed.  Then the only thing that doesn't
4711369Sdduvall 	 * automatically work right is access to an 8-byte register
4721369Sdduvall 	 * by a little-endian host; but we don't want to set the
4731369Sdduvall 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
4741369Sdduvall 	 * accesses don't go where expected ;-(  So we live with
4751369Sdduvall 	 * that, and perform word-swaps in software in the few cases
4761369Sdduvall 	 * where a chip register is defined as an 8-byte value --
4771369Sdduvall 	 * see the code below for details ...
4781369Sdduvall 	 *
4791369Sdduvall 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
4801369Sdduvall 	 * very clear in the register description in the PRM, but
4811369Sdduvall 	 * Broadcom document 570X-PG104-R page 248 explains a little
4821369Sdduvall 	 * more (under "Broadcom Mask Mode").  The bit changes the way
4831369Sdduvall 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
4841369Sdduvall 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
4851369Sdduvall 	 * way as the 5700 did, which isn't very convenient.  Setting
4861369Sdduvall 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
4871369Sdduvall 	 * bit do just what its name says -- MASK the PCI #INTA output
4881369Sdduvall 	 * (i.e. deassert the signal at the pin) leaving all internal
4891369Sdduvall 	 * state unchanged.  This is much more convenient for our
4901369Sdduvall 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
4911369Sdduvall 	 *
4921369Sdduvall 	 * Note: the inconvenient semantics of the interrupt mailbox
4931369Sdduvall 	 * (nonzero disables and acknowledges/clears the interrupt,
4941369Sdduvall 	 * zero enables AND CLEARS it) would make race conditions
4951369Sdduvall 	 * likely in the interrupt handler:
4961369Sdduvall 	 *
4971369Sdduvall 	 * (1)	acknowledge & disable interrupts
4981369Sdduvall 	 * (2)	while (more to do)
4991369Sdduvall 	 * 		process packets
5001369Sdduvall 	 * (3)	enable interrupts -- also clears pending
5011369Sdduvall 	 *
5021369Sdduvall 	 * If the chip received more packets and internally generated
5031369Sdduvall 	 * an interrupt between the check at (2) and the mbox write
5041369Sdduvall 	 * at (3), this interrupt would be lost :-(
5051369Sdduvall 	 *
5061369Sdduvall 	 * The best way to avoid this is to use TAGGED STATUS mode,
5071369Sdduvall 	 * where the chip includes a unique tag in each status block
5081369Sdduvall 	 * update, and the host, when re-enabling interrupts, passes
5091369Sdduvall 	 * the last tag it saw back to the chip; then the chip can
5101369Sdduvall 	 * see whether the host is truly up to date, and regenerate
5111369Sdduvall 	 * its interrupt if not.
5121369Sdduvall 	 */
5131369Sdduvall 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
5141369Sdduvall 		MHCR_ENABLE_TAGGED_STATUS_MODE |
5151369Sdduvall 		MHCR_MASK_INTERRUPT_MODE |
5161369Sdduvall 		MHCR_CLEAR_INTERRUPT_INTA;
5171369Sdduvall 
5181369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
5191369Sdduvall 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
5201369Sdduvall 
5211369Sdduvall #ifdef	_BIG_ENDIAN
5221369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
5231369Sdduvall #endif	/* _BIG_ENDIAN */
5241369Sdduvall 
5251369Sdduvall 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
5261369Sdduvall 
5271408Srandyf #ifdef BGE_IPMI_ASF
5281408Srandyf 	bgep->asf_wordswapped = B_FALSE;
5291408Srandyf #endif
5301369Sdduvall 	/*
5311369Sdduvall 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
5321369Sdduvall 	 *			 Disable Memory Write/Invalidate
5331369Sdduvall 	 *			 Enable or disable Bus Mastering
5341369Sdduvall 	 *
5351369Sdduvall 	 * Note that all other bits are taken from the original value saved
5361369Sdduvall 	 * the first time through here, rather than from the current register
5371369Sdduvall 	 * value, 'cos that will have been cleared by a soft RESET since.
5381369Sdduvall 	 * In this way we preserve the OBP/nexus-parent's preferred settings
5391369Sdduvall 	 * of the parity-error and system-error enable bits across multiple
5401369Sdduvall 	 * chip RESETs.
5411369Sdduvall 	 */
5421369Sdduvall 	command = bgep->chipid.command | PCI_COMM_MAE;
5431369Sdduvall 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
5441369Sdduvall 	if (enable_dma)
5451369Sdduvall 		command |= PCI_COMM_ME;
5461369Sdduvall 	/*
5471369Sdduvall 	 * on BCM5714 revision A0, false parity error gets generated
5482135Szh199473 	 * due to a logic bug. Provide a workaround by disabling parity
5491369Sdduvall 	 * error.
5501369Sdduvall 	 */
5511369Sdduvall 	if (((cidp->device == DEVICE_ID_5714C) ||
5521369Sdduvall 	    (cidp->device == DEVICE_ID_5714S)) &&
5531369Sdduvall 	    (cidp->revision == REVISION_ID_5714_A0)) {
5541369Sdduvall 		command &= ~PCI_COMM_PARITY_DETECT;
5551369Sdduvall 	}
5561369Sdduvall 	pci_config_put16(handle, PCI_CONF_COMM, command);
5571369Sdduvall 
5581369Sdduvall 	/*
5591369Sdduvall 	 * On some PCI-E device, there were instances when
5601369Sdduvall 	 * the device was still link training.
5611369Sdduvall 	 */
5621369Sdduvall 	if (bgep->chipid.pci_type == BGE_PCI_E) {
5631369Sdduvall 		i = 0;
5641369Sdduvall 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
5651369Sdduvall 		while ((value16 != command) && (i < 100)) {
5661369Sdduvall 			drv_usecwait(200);
5671369Sdduvall 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
5681369Sdduvall 			++i;
5691369Sdduvall 		}
5701369Sdduvall 	}
5711369Sdduvall 
5721369Sdduvall 	/*
5731369Sdduvall 	 * Clear any remaining error status bits
5741369Sdduvall 	 */
5751369Sdduvall 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
5761369Sdduvall 
5771369Sdduvall 	/*
5782073Svivek 	 * Do following if and only if the device is NOT BCM5714C OR
5792073Svivek 	 * BCM5715C
5801369Sdduvall 	 */
5812073Svivek 	if (!((cidp->device == DEVICE_ID_5714C) ||
5822073Svivek 		(cidp->device == DEVICE_ID_5715C))) {
5832073Svivek 		/*
5842073Svivek 		 * Make sure these indirect-access registers are sane
5852073Svivek 		 * rather than random after power-up or reset
5862073Svivek 		 */
5872073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
5882073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
5892073Svivek 	}
5902135Szh199473 	/*
5912135Szh199473 	 * Step 8: Disable PCI-X/PCI-E Relaxed Ordering
5922135Szh199473 	 */
5932135Szh199473 	bge_cfg_clr16(bgep, PCIX_CONF_COMM, PCIX_COMM_RELAXED);
5942135Szh199473 
5952135Szh199473 	if (cidp->pci_type == BGE_PCI_E)
5962135Szh199473 		bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL,
5972135Szh199473 				DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
5981369Sdduvall }
5991369Sdduvall 
6001369Sdduvall #ifdef __amd64
6011369Sdduvall /*
6021369Sdduvall  * Distinguish CPU types
6031369Sdduvall  *
6041369Sdduvall  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
6051369Sdduvall  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
6061369Sdduvall  * for PCI-Express based network interface card. This is the work-around
6071369Sdduvall  * for those nics.
6081369Sdduvall  */
6091369Sdduvall static boolean_t bge_get_em64t_type(void);
6101369Sdduvall #pragma	inline(bge_get_em64t_type)
6111369Sdduvall 
6121369Sdduvall static boolean_t
6131369Sdduvall bge_get_em64t_type(void)
6141369Sdduvall {
6151369Sdduvall 
6161369Sdduvall 	return (x86_vendor == X86_VENDOR_Intel);
6171369Sdduvall }
6181369Sdduvall #endif
6191369Sdduvall 
6201369Sdduvall /*
6211369Sdduvall  * Operating register get/set access routines
6221369Sdduvall  */
6231369Sdduvall 
6241369Sdduvall uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
6251369Sdduvall #pragma	inline(bge_reg_get32)
6261369Sdduvall 
6271369Sdduvall uint32_t
6281369Sdduvall bge_reg_get32(bge_t *bgep, bge_regno_t regno)
6291369Sdduvall {
6301369Sdduvall 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
6311369Sdduvall 		(void *)bgep, regno));
6321369Sdduvall 
6331369Sdduvall 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
6341369Sdduvall }
6351369Sdduvall 
6361369Sdduvall void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
6371369Sdduvall #pragma	inline(bge_reg_put32)
6381369Sdduvall 
6391369Sdduvall void
6401369Sdduvall bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
6411369Sdduvall {
6421369Sdduvall 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
6431369Sdduvall 		(void *)bgep, regno, data));
6441369Sdduvall 
6451369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6461369Sdduvall 	BGE_PCICHK(bgep);
6471369Sdduvall }
6481369Sdduvall 
6491369Sdduvall void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6501369Sdduvall #pragma	inline(bge_reg_set32)
6511369Sdduvall 
6521369Sdduvall void
6531369Sdduvall bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6541369Sdduvall {
6551369Sdduvall 	uint32_t regval;
6561369Sdduvall 
6571369Sdduvall 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
6581369Sdduvall 		(void *)bgep, regno, bits));
6591369Sdduvall 
6601369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6611369Sdduvall 	regval |= bits;
6621369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6631369Sdduvall }
6641369Sdduvall 
6651369Sdduvall void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6661369Sdduvall #pragma	inline(bge_reg_clr32)
6671369Sdduvall 
6681369Sdduvall void
6691369Sdduvall bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6701369Sdduvall {
6711369Sdduvall 	uint32_t regval;
6721369Sdduvall 
6731369Sdduvall 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
6741369Sdduvall 		(void *)bgep, regno, bits));
6751369Sdduvall 
6761369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6771369Sdduvall 	regval &= ~bits;
6781369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6791369Sdduvall }
6801369Sdduvall 
6811369Sdduvall static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
6821369Sdduvall #pragma	inline(bge_reg_get64)
6831369Sdduvall 
6841369Sdduvall static uint64_t
6851369Sdduvall bge_reg_get64(bge_t *bgep, bge_regno_t regno)
6861369Sdduvall {
6871369Sdduvall 	uint64_t regval;
6881369Sdduvall 
6891369Sdduvall #ifdef	__amd64
6901369Sdduvall 	if (bge_get_em64t_type()) {
6911369Sdduvall 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
6921369Sdduvall 		regval <<= 32;
6931369Sdduvall 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
6941369Sdduvall 	} else {
6951369Sdduvall 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6961369Sdduvall 	}
6971369Sdduvall #else
6981369Sdduvall 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6991369Sdduvall #endif
7001369Sdduvall 
7011369Sdduvall #ifdef	_LITTLE_ENDIAN
7021369Sdduvall 	regval = (regval >> 32) | (regval << 32);
7031369Sdduvall #endif	/* _LITTLE_ENDIAN */
7041369Sdduvall 
7051369Sdduvall 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
7061369Sdduvall 		(void *)bgep, regno, regval));
7071369Sdduvall 
7081369Sdduvall 	return (regval);
7091369Sdduvall }
7101369Sdduvall 
7111369Sdduvall static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
7121369Sdduvall #pragma	inline(bge_reg_put64)
7131369Sdduvall 
7141369Sdduvall static void
7151369Sdduvall bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
7161369Sdduvall {
7171369Sdduvall 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
7181369Sdduvall 		(void *)bgep, regno, data));
7191369Sdduvall 
7201369Sdduvall #ifdef	_LITTLE_ENDIAN
7211369Sdduvall 	data = ((data >> 32) | (data << 32));
7221369Sdduvall #endif	/* _LITTLE_ENDIAN */
7231369Sdduvall 
7241369Sdduvall #ifdef	__amd64
7251369Sdduvall 	if (bge_get_em64t_type()) {
7261369Sdduvall 		ddi_put32(bgep->io_handle,
7271369Sdduvall 			PIO_ADDR(bgep, regno), (uint32_t)data);
7281369Sdduvall 		BGE_PCICHK(bgep);
7291369Sdduvall 		ddi_put32(bgep->io_handle,
7301369Sdduvall 			PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
7311369Sdduvall 
7321369Sdduvall 	} else {
7331369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7341369Sdduvall 	}
7351369Sdduvall #else
7361369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7371369Sdduvall #endif
7381369Sdduvall 
7391369Sdduvall 	BGE_PCICHK(bgep);
7401369Sdduvall }
7411369Sdduvall 
7421369Sdduvall /*
7431369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
7441369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
7451369Sdduvall  */
7461369Sdduvall static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
7471369Sdduvall #pragma	inline(bge_reg_putrcb)
7481369Sdduvall 
7491369Sdduvall static void
7501369Sdduvall bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
7511369Sdduvall {
7521369Sdduvall 	uint64_t *p;
7531369Sdduvall 
7541369Sdduvall 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
7551369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
7561369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
7571369Sdduvall 
7581369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
7591369Sdduvall 
7601369Sdduvall 	p = (void *)rcbp;
7611369Sdduvall 	bge_reg_put64(bgep, addr, *p++);
7621369Sdduvall 	bge_reg_put64(bgep, addr+8, *p);
7631369Sdduvall }
7641369Sdduvall 
7651369Sdduvall void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
7661369Sdduvall #pragma	inline(bge_mbx_put)
7671369Sdduvall 
7681369Sdduvall void
7691369Sdduvall bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
7701369Sdduvall {
7711369Sdduvall 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
7721369Sdduvall 		(void *)bgep, regno, data));
7731369Sdduvall 
7741369Sdduvall 	/*
7751369Sdduvall 	 * Mailbox registers are nominally 64 bits on the 5701, but
7761369Sdduvall 	 * the MSW isn't used.  On the 5703, they're only 32 bits
7771369Sdduvall 	 * anyway.  So here we just write the lower(!) 32 bits -
7781369Sdduvall 	 * remembering that the chip is big-endian, even though the
7791369Sdduvall 	 * PCI bus is little-endian ...
7801369Sdduvall 	 */
7811369Sdduvall #ifdef	_BIG_ENDIAN
7821369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
7831369Sdduvall #else
7841369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
7851369Sdduvall #endif	/* _BIG_ENDIAN */
7861369Sdduvall 	BGE_PCICHK(bgep);
7871369Sdduvall }
7881369Sdduvall 
7891369Sdduvall #if	BGE_DEBUGGING
7901369Sdduvall 
7911369Sdduvall void bge_led_mark(bge_t *bgep);
7921369Sdduvall #pragma	no_inline(bge_led_mark)
7931369Sdduvall 
7941369Sdduvall void
7951369Sdduvall bge_led_mark(bge_t *bgep)
7961369Sdduvall {
7971369Sdduvall 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
7981369Sdduvall 			    LED_CONTROL_1000MBPS_LED |
7991369Sdduvall 			    LED_CONTROL_100MBPS_LED |
8001369Sdduvall 			    LED_CONTROL_10MBPS_LED;
8011369Sdduvall 
8021369Sdduvall 	/*
8031369Sdduvall 	 * Blink all three LINK LEDs on simultaneously, then all off,
8041369Sdduvall 	 * then restore to automatic hardware control.  This is used
8051369Sdduvall 	 * in laboratory testing to trigger a logic analyser or scope.
8061369Sdduvall 	 */
8071369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8081369Sdduvall 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
8091369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8101369Sdduvall 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
8111369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8121369Sdduvall }
8131369Sdduvall 
8141369Sdduvall #endif	/* BGE_DEBUGGING */
8151369Sdduvall 
8161369Sdduvall /*
8171369Sdduvall  * NIC on-chip memory access routines
8181369Sdduvall  *
8191369Sdduvall  * Only 32K of NIC memory is visible at a time, controlled by the
8201369Sdduvall  * Memory Window Base Address Register (in PCI config space).  Once
8211369Sdduvall  * this is set, the 32K region of NIC-local memory that it refers
8221369Sdduvall  * to can be directly addressed in the upper 32K of the 64K of PCI
8231369Sdduvall  * memory space used for the device.
8241369Sdduvall  */
8251369Sdduvall 
8261369Sdduvall static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
8271369Sdduvall #pragma	inline(bge_nic_setwin)
8281369Sdduvall 
8291369Sdduvall static void
8301369Sdduvall bge_nic_setwin(bge_t *bgep, bge_regno_t base)
8311369Sdduvall {
8322073Svivek 	chip_id_t *cidp;
8332073Svivek 
8341369Sdduvall 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
8351369Sdduvall 		(void *)bgep, base));
8361369Sdduvall 
8371369Sdduvall 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
8382073Svivek 
8392073Svivek 	/*
8402073Svivek 	 * Don't do repeated zero data writes,
8412073Svivek 	 * if the device is BCM5714C/15C.
8422073Svivek 	 */
8432073Svivek 	cidp = &bgep->chipid;
8442073Svivek 	if ((cidp->device == DEVICE_ID_5714C) ||
8452073Svivek 		(cidp->device == DEVICE_ID_5715C)) {
8462073Svivek 		if (bgep->lastWriteZeroData && (base == (bge_regno_t)0))
8472073Svivek 			return;
8482073Svivek 		/* Adjust lastWriteZeroData */
8492073Svivek 		bgep->lastWriteZeroData = ((base == (bge_regno_t)0) ?
8502073Svivek 			B_TRUE : B_FALSE);
8512073Svivek 	}
8521369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
8531369Sdduvall }
8541369Sdduvall 
8551369Sdduvall 
8561369Sdduvall static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
8571369Sdduvall #pragma	inline(bge_nic_get32)
8581369Sdduvall 
8591369Sdduvall static uint32_t
8601369Sdduvall bge_nic_get32(bge_t *bgep, bge_regno_t addr)
8611369Sdduvall {
8621369Sdduvall 	uint32_t data;
8631369Sdduvall 
8641408Srandyf #ifdef BGE_IPMI_ASF
8651408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8661408Srandyf 		/* workaround for word swap error */
8671408Srandyf 		if (addr & 4)
8681408Srandyf 			addr = addr - 4;
8691408Srandyf 		else
8701408Srandyf 			addr = addr + 4;
8711408Srandyf 	}
8721408Srandyf #endif
8731408Srandyf 
8741369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
8751369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
8761369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
8771369Sdduvall 
8781369Sdduvall 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
8791369Sdduvall 
8801369Sdduvall 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
8811369Sdduvall 		(void *)bgep, addr, data));
8821369Sdduvall 
8831369Sdduvall 	return (data);
8841369Sdduvall }
8851369Sdduvall 
8861408Srandyf void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
8871408Srandyf #pragma inline(bge_nic_put32)
8881408Srandyf 
8891408Srandyf void
8901369Sdduvall bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
8911369Sdduvall {
8921369Sdduvall 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
8931369Sdduvall 		(void *)bgep, addr, data));
8941369Sdduvall 
8951408Srandyf #ifdef BGE_IPMI_ASF
8961408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8971408Srandyf 		/* workaround for word swap error */
8981408Srandyf 		if (addr & 4)
8991408Srandyf 			addr = addr - 4;
9001408Srandyf 		else
9011408Srandyf 			addr = addr + 4;
9021408Srandyf 	}
9031408Srandyf #endif
9041408Srandyf 
9051369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9061369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9071369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9081369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9091369Sdduvall 	BGE_PCICHK(bgep);
9101369Sdduvall }
9111369Sdduvall 
9121369Sdduvall 
9131369Sdduvall static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
9141369Sdduvall #pragma	inline(bge_nic_get64)
9151369Sdduvall 
9161369Sdduvall static uint64_t
9171369Sdduvall bge_nic_get64(bge_t *bgep, bge_regno_t addr)
9181369Sdduvall {
9191369Sdduvall 	uint64_t data;
9201369Sdduvall 
9211369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9221369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9231369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9241369Sdduvall 
9251369Sdduvall #ifdef	__amd64
9261369Sdduvall 		if (bge_get_em64t_type()) {
9271369Sdduvall 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
9281369Sdduvall 			data <<= 32;
9291369Sdduvall 			data |= ddi_get32(bgep->io_handle,
9301369Sdduvall 				PIO_ADDR(bgep, addr + 4));
9311369Sdduvall 		} else {
9321369Sdduvall 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9331369Sdduvall 		}
9341369Sdduvall #else
9351369Sdduvall 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9361369Sdduvall #endif
9371369Sdduvall 
9381369Sdduvall 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
9391369Sdduvall 		(void *)bgep, addr, data));
9401369Sdduvall 
9411369Sdduvall 	return (data);
9421369Sdduvall }
9431369Sdduvall 
9441369Sdduvall static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
9451369Sdduvall #pragma	inline(bge_nic_put64)
9461369Sdduvall 
9471369Sdduvall static void
9481369Sdduvall bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
9491369Sdduvall {
9501369Sdduvall 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
9511369Sdduvall 		(void *)bgep, addr, data));
9521369Sdduvall 
9531369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9541369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9551369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9561369Sdduvall 
9571369Sdduvall #ifdef	__amd64
9581369Sdduvall 	if (bge_get_em64t_type()) {
9591369Sdduvall 		ddi_put32(bgep->io_handle,
9601369Sdduvall 			PIO_ADDR(bgep, addr), (uint32_t)data);
9611369Sdduvall 		BGE_PCICHK(bgep);
9621369Sdduvall 		ddi_put32(bgep->io_handle,
9631369Sdduvall 			PIO_ADDR(bgep, addr + 4), (uint32_t)(data >> 32));
9641369Sdduvall 	} else {
9651369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9661369Sdduvall 	}
9671369Sdduvall #else
9681369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9691369Sdduvall #endif
9701369Sdduvall 
9711369Sdduvall 	BGE_PCICHK(bgep);
9721369Sdduvall }
9731369Sdduvall 
9741369Sdduvall /*
9751369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
9761369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
9771369Sdduvall  */
9781369Sdduvall static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
9791369Sdduvall #pragma	inline(bge_nic_putrcb)
9801369Sdduvall 
9811369Sdduvall static void
9821369Sdduvall bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
9831369Sdduvall {
9841369Sdduvall 	uint64_t *p;
9851369Sdduvall 
9861369Sdduvall 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
9871369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
9881369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
9891369Sdduvall 
9901369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
9911369Sdduvall 
9921369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9931369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9941369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9951369Sdduvall 
9961369Sdduvall 	p = (void *)rcbp;
9971369Sdduvall #ifdef	__amd64
9981369Sdduvall 	if (bge_get_em64t_type()) {
9991369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
10001369Sdduvall 			(uint32_t)(*p));
10011369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
10021369Sdduvall 			(uint32_t)(*p >> 32));
10031369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
10041369Sdduvall 			(uint32_t)(*(p + 1)));
10051369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
10061369Sdduvall 			(uint32_t)(*p >> 32));
10071369Sdduvall 
10081369Sdduvall 	} else {
10091369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10101369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
10111369Sdduvall 	}
10121369Sdduvall #else
10131369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10141369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
10151369Sdduvall #endif
10161369Sdduvall 
10171369Sdduvall 	BGE_PCICHK(bgep);
10181369Sdduvall }
10191369Sdduvall 
10201369Sdduvall static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
10211369Sdduvall #pragma	inline(bge_nic_zero)
10221369Sdduvall 
10231369Sdduvall static void
10241369Sdduvall bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
10251369Sdduvall {
10261369Sdduvall 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
10271369Sdduvall 		(void *)bgep, addr, nbytes));
10281369Sdduvall 
10291369Sdduvall 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
10301369Sdduvall 		((addr+nbytes) & ~MWBAR_GRANULE_MASK));
10311369Sdduvall 
10321369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10331369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10341369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10351369Sdduvall 
10361369Sdduvall 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
10371369Sdduvall 		nbytes, 1, DDI_DATA_SZ08_ACC);
10381369Sdduvall 	BGE_PCICHK(bgep);
10391369Sdduvall }
10401369Sdduvall 
10411369Sdduvall /*
10421369Sdduvall  * MII (PHY) register get/set access routines
10431369Sdduvall  *
10441369Sdduvall  * These use the chip's MII auto-access method, controlled by the
10451369Sdduvall  * MII Communication register at 0x044c, so the CPU doesn't have
10461369Sdduvall  * to fiddle with the individual bits.
10471369Sdduvall  */
10481369Sdduvall 
10491369Sdduvall #undef	BGE_DBG
10501369Sdduvall #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
10511369Sdduvall 
10521369Sdduvall static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
10531369Sdduvall 				uint16_t data, uint32_t cmd);
10541369Sdduvall #pragma	no_inline(bge_mii_access)
10551369Sdduvall 
10561369Sdduvall static uint16_t
10571369Sdduvall bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
10581369Sdduvall {
10591369Sdduvall 	uint32_t timeout;
10601369Sdduvall 	uint32_t regval1;
10611369Sdduvall 	uint32_t regval2;
10621369Sdduvall 
10631369Sdduvall 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
10641369Sdduvall 		(void *)bgep, regno, data, cmd));
10651369Sdduvall 
10661369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
10671369Sdduvall 
10681369Sdduvall 	/*
10691369Sdduvall 	 * Assemble the command ...
10701369Sdduvall 	 */
10711369Sdduvall 	cmd |= data << MI_COMMS_DATA_SHIFT;
10721369Sdduvall 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
10731369Sdduvall 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
10741369Sdduvall 	cmd |= MI_COMMS_START;
10751369Sdduvall 
10761369Sdduvall 	/*
10771369Sdduvall 	 * Wait for any command already in progress ...
10781369Sdduvall 	 *
10791369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
10801369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
10811369Sdduvall 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
10821369Sdduvall 	 * bit set here -- it seems that the chip can initiate MII
10831369Sdduvall 	 * accesses internally, even with polling OFF.
10841369Sdduvall 	 */
10851369Sdduvall 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
10861865Sdilpreet 	for (timeout = 100; ; ) {
10871369Sdduvall 		if ((regval2 & MI_COMMS_START) == 0) {
10881369Sdduvall 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
10891369Sdduvall 			break;
10901369Sdduvall 		}
10911369Sdduvall 		if (--timeout == 0)
10921369Sdduvall 			break;
10931369Sdduvall 		drv_usecwait(10);
10941369Sdduvall 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
10951369Sdduvall 	}
10961369Sdduvall 
10971865Sdilpreet 	if (timeout == 0)
10981865Sdilpreet 		return ((uint16_t)~0u);
10991865Sdilpreet 
11001865Sdilpreet 	if (timeout != 100)
11011369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11021369Sdduvall 			"MI_COMMS_START set for %d us; 0x%x->0x%x",
11031865Sdilpreet 			cmd, 10*(100-timeout), regval1, regval2));
11041369Sdduvall 
11051369Sdduvall 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11061369Sdduvall 	for (timeout = 1000; ; ) {
11071369Sdduvall 		if ((regval1 & MI_COMMS_START) == 0)
11081369Sdduvall 			break;
11091369Sdduvall 		if (--timeout == 0)
11101369Sdduvall 			break;
11111369Sdduvall 		drv_usecwait(10);
11121369Sdduvall 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11131369Sdduvall 	}
11141369Sdduvall 
11151369Sdduvall 	/*
11161369Sdduvall 	 * Drop out early if the READ FAILED bit is set -- this chip
11171369Sdduvall 	 * could be a 5703/4S, with a SerDes instead of a PHY!
11181369Sdduvall 	 */
11191369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11201369Sdduvall 		return ((uint16_t)~0u);
11211369Sdduvall 
11221369Sdduvall 	if (timeout == 0)
11231369Sdduvall 		return ((uint16_t)~0u);
11241369Sdduvall 
11251369Sdduvall 	/*
11261369Sdduvall 	 * The PRM says to wait 5us after seeing the START bit clear
11271369Sdduvall 	 * and then re-read the register to get the final value of the
11281369Sdduvall 	 * data field, in order to avoid a race condition where the
11291369Sdduvall 	 * START bit is clear but the data field isn't yet valid.
11301369Sdduvall 	 *
11311369Sdduvall 	 * Note: we don't actually seem to be encounter this race;
11321369Sdduvall 	 * except when the START bit is seen set again (see below),
11331369Sdduvall 	 * the data field doesn't change during this 5us interval.
11341369Sdduvall 	 */
11351369Sdduvall 	drv_usecwait(5);
11361369Sdduvall 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11371369Sdduvall 
11381369Sdduvall 	/*
11391369Sdduvall 	 * Unfortunately, when following the PRMs instructions above,
11401369Sdduvall 	 * we have occasionally seen the START bit set again(!) in the
11411369Sdduvall 	 * value read after the 5us delay. This seems to be due to the
11421369Sdduvall 	 * chip autonomously starting another MII access internally.
11431369Sdduvall 	 * In such cases, the command/data/etc fields relate to the
11441369Sdduvall 	 * internal command, rather than the one that we thought had
11451369Sdduvall 	 * just finished.  So in this case, we fall back to returning
11461369Sdduvall 	 * the data from the original read that showed START clear.
11471369Sdduvall 	 */
11481369Sdduvall 	if (regval2 & MI_COMMS_START) {
11491369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11501369Sdduvall 			"MI_COMMS_START set after transaction; 0x%x->0x%x",
11511369Sdduvall 			cmd, regval1, regval2));
11521369Sdduvall 		regval2 = regval1;
11531369Sdduvall 	}
11541369Sdduvall 
11551369Sdduvall 	if (regval2 & MI_COMMS_START)
11561369Sdduvall 		return ((uint16_t)~0u);
11571369Sdduvall 
11581369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11591369Sdduvall 		return ((uint16_t)~0u);
11601369Sdduvall 
11611369Sdduvall 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
11621369Sdduvall }
11631369Sdduvall 
11641369Sdduvall uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
11651369Sdduvall #pragma	no_inline(bge_mii_get16)
11661369Sdduvall 
11671369Sdduvall uint16_t
11681369Sdduvall bge_mii_get16(bge_t *bgep, bge_regno_t regno)
11691369Sdduvall {
11701369Sdduvall 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
11711369Sdduvall 		(void *)bgep, regno));
11721369Sdduvall 
11731369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11741369Sdduvall 
11751369Sdduvall 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
11761369Sdduvall }
11771369Sdduvall 
11781369Sdduvall void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
11791369Sdduvall #pragma	no_inline(bge_mii_put16)
11801369Sdduvall 
11811369Sdduvall void
11821369Sdduvall bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
11831369Sdduvall {
11841369Sdduvall 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
11851369Sdduvall 		(void *)bgep, regno, data));
11861369Sdduvall 
11871369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11881369Sdduvall 
11891369Sdduvall 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
11901369Sdduvall }
11911369Sdduvall 
11921369Sdduvall #undef	BGE_DBG
11931369Sdduvall #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
11941369Sdduvall 
11951369Sdduvall #if	BGE_SEE_IO32 || BGE_FLASH_IO32
11961369Sdduvall 
11971369Sdduvall /*
11981369Sdduvall  * Basic SEEPROM get/set access routine
11991369Sdduvall  *
12001369Sdduvall  * This uses the chip's SEEPROM auto-access method, controlled by the
12011369Sdduvall  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
12021369Sdduvall  * doesn't have to fiddle with the individual bits.
12031369Sdduvall  *
12041369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
12051369Sdduvall  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
12061369Sdduvall  *
12071369Sdduvall  * Return value:
12081369Sdduvall  *	0 on success,
12091369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
12101369Sdduvall  *	EPROTO on other h/w or s/w errors.
12111369Sdduvall  *
12121369Sdduvall  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
12131369Sdduvall  * from a (successful) SEEPROM_ACCESS_READ.
12141369Sdduvall  */
12151369Sdduvall static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
12161369Sdduvall 				uint32_t *dp);
12171369Sdduvall #pragma	no_inline(bge_seeprom_access)
12181369Sdduvall 
12191369Sdduvall static int
12201369Sdduvall bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
12211369Sdduvall {
12221369Sdduvall 	uint32_t tries;
12231369Sdduvall 	uint32_t regval;
12241369Sdduvall 
12251369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12261369Sdduvall 
12271369Sdduvall 	/*
12281369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
12291369Sdduvall 	 * to specifically enable SEEPROM access (Flash is the default).
12301369Sdduvall 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
12311369Sdduvall 	 * and the NVM control registers don't exist ...
12321369Sdduvall 	 */
12331369Sdduvall 	switch (bgep->chipid.nvtype) {
12341369Sdduvall 	case BGE_NVTYPE_NONE:
12351369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
12361369Sdduvall 		_NOTE(NOTREACHED)
12371369Sdduvall 	case BGE_NVTYPE_SEEPROM:
12381369Sdduvall 		break;
12391369Sdduvall 
12401369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
12411369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
12421369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
12431369Sdduvall 	default:
12441369Sdduvall 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
12451369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
12461369Sdduvall 		break;
12471369Sdduvall 	}
12481369Sdduvall 
12491369Sdduvall 	/*
12501369Sdduvall 	 * Check there's no command in progress.
12511369Sdduvall 	 *
12521369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
12531369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
12541369Sdduvall 	 * Also, to ensure we don't have a conflict with the chip's
12551369Sdduvall 	 * internal firmware or a process accessing the same (shared)
12561369Sdduvall 	 * SEEPROM through the other port of a 5704, we've already
12571369Sdduvall 	 * been through the "software arbitration" protocol.
12581369Sdduvall 	 * So this is just a final consistency check: we shouldn't
12591369Sdduvall 	 * see EITHER the START bit (command started but not complete)
12601369Sdduvall 	 * OR the COMPLETE bit (command completed but not cleared).
12611369Sdduvall 	 */
12621369Sdduvall 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12631369Sdduvall 	if (regval & SEEPROM_ACCESS_START)
12641369Sdduvall 		return (EPROTO);
12651369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE)
12661369Sdduvall 		return (EPROTO);
12671369Sdduvall 
12681369Sdduvall 	/*
12691369Sdduvall 	 * Assemble the command ...
12701369Sdduvall 	 */
12711369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
12721369Sdduvall 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
12731369Sdduvall 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
12741369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
12751369Sdduvall 	cmd |= SEEPROM_ACCESS_START;
12761369Sdduvall 	cmd |= SEEPROM_ACCESS_COMPLETE;
12771369Sdduvall 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
12781369Sdduvall 
12791369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
12801369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
12811369Sdduvall 
12821369Sdduvall 	/*
12831369Sdduvall 	 * By observation, a successful access takes ~20us on a 5703/4,
12841369Sdduvall 	 * but apparently much longer (up to 1000us) on the obsolescent
12851369Sdduvall 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
12861369Sdduvall 	 * timeouts here; but OTOH, we don't want a bogus access to lock
12871369Sdduvall 	 * out interrupts for longer than necessary. So we'll allow up
12881369Sdduvall 	 * to 1000us ...
12891369Sdduvall 	 */
12901369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
12911369Sdduvall 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12921369Sdduvall 		if (regval & SEEPROM_ACCESS_COMPLETE)
12931369Sdduvall 			break;
12941369Sdduvall 		drv_usecwait(1);
12951369Sdduvall 	}
12961369Sdduvall 
12971369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE) {
12981369Sdduvall 		/*
12991369Sdduvall 		 * All OK; read the SEEPROM data register, then write back
13001369Sdduvall 		 * the value read from the address register in order to
13011369Sdduvall 		 * clear the <complete> bit and leave the SEEPROM access
13021369Sdduvall 		 * state machine idle, ready for the next access ...
13031369Sdduvall 		 */
13041369Sdduvall 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
13051369Sdduvall 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
13061369Sdduvall 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
13071369Sdduvall 		return (0);
13081369Sdduvall 	}
13091369Sdduvall 
13101369Sdduvall 	/*
13111369Sdduvall 	 * Hmm ... what happened here?
13121369Sdduvall 	 *
13132135Szh199473 	 * Most likely, the user addressed a non-existent SEEPROM. Or
13141369Sdduvall 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
13151369Sdduvall 	 * and didn't respond to being addressed. Either way, it's left
13161369Sdduvall 	 * the SEEPROM access state machine wedged. So we'll reset it
13171369Sdduvall 	 * before we leave, so it's ready for next time ...
13181369Sdduvall 	 */
13191369Sdduvall 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
13201369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
13211369Sdduvall 	return (ENODATA);
13221369Sdduvall }
13231369Sdduvall 
13241369Sdduvall /*
13251369Sdduvall  * Basic Flash get/set access routine
13261369Sdduvall  *
13271369Sdduvall  * These use the chip's Flash auto-access method, controlled by the
13281369Sdduvall  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
13291369Sdduvall  * fiddle with the individual bits.
13301369Sdduvall  *
13311369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
13321369Sdduvall  * the right to access the Flash, via bge_nvmem_acquire() above.
13331369Sdduvall  *
13341369Sdduvall  * Return value:
13351369Sdduvall  *	0 on success,
13361369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
13371369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
13381369Sdduvall  *
13391369Sdduvall  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
13401369Sdduvall  * from a (successful) NVM_FLASH_CMD_RD.
13411369Sdduvall  */
13421369Sdduvall static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
13431369Sdduvall 				uint32_t *dp);
13441369Sdduvall #pragma	no_inline(bge_flash_access)
13451369Sdduvall 
13461369Sdduvall static int
13471369Sdduvall bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
13481369Sdduvall {
13491369Sdduvall 	uint32_t tries;
13501369Sdduvall 	uint32_t regval;
13511369Sdduvall 
13521369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
13531369Sdduvall 
13541369Sdduvall 	/*
13551369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
13561369Sdduvall 	 * to specifically disable SEEPROM access while accessing Flash.
13571369Sdduvall 	 * The older chips don't support Flash, and the NVM registers don't
13581369Sdduvall 	 * exist, so we shouldn't be here at all!
13591369Sdduvall 	 */
13601369Sdduvall 	switch (bgep->chipid.nvtype) {
13611369Sdduvall 	case BGE_NVTYPE_NONE:
13621369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
13631369Sdduvall 		_NOTE(NOTREACHED)
13641369Sdduvall 	case BGE_NVTYPE_SEEPROM:
13651369Sdduvall 		return (ENODEV);
13661369Sdduvall 
13671369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
13681369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
13691369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
13701369Sdduvall 	default:
13711369Sdduvall 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
13721369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
13731369Sdduvall 		break;
13741369Sdduvall 	}
13751369Sdduvall 
13761369Sdduvall 	/*
13771369Sdduvall 	 * Assemble the command ...
13781369Sdduvall 	 */
13791369Sdduvall 	addr &= NVM_FLASH_ADDR_MASK;
13801369Sdduvall 	cmd |= NVM_FLASH_CMD_DOIT;
13811369Sdduvall 	cmd |= NVM_FLASH_CMD_FIRST;
13821369Sdduvall 	cmd |= NVM_FLASH_CMD_LAST;
13831369Sdduvall 	cmd |= NVM_FLASH_CMD_DONE;
13841369Sdduvall 
13851369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
13861369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
13871369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
13881369Sdduvall 
13891369Sdduvall 	/*
13901369Sdduvall 	 * Allow up to 1000ms ...
13911369Sdduvall 	 */
13921369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
13931369Sdduvall 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
13941369Sdduvall 		if (regval & NVM_FLASH_CMD_DONE)
13951369Sdduvall 			break;
13961369Sdduvall 		drv_usecwait(1);
13971369Sdduvall 	}
13981369Sdduvall 
13991369Sdduvall 	if (regval & NVM_FLASH_CMD_DONE) {
14001369Sdduvall 		/*
14011369Sdduvall 		 * All OK; read the data from the Flash read register
14021369Sdduvall 		 */
14031369Sdduvall 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
14041369Sdduvall 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
14051369Sdduvall 		return (0);
14061369Sdduvall 	}
14071369Sdduvall 
14081369Sdduvall 	/*
14091369Sdduvall 	 * Hmm ... what happened here?
14101369Sdduvall 	 *
14112135Szh199473 	 * Most likely, the user addressed a non-existent Flash. Or
14121369Sdduvall 	 * maybe the Flash was busy internally (e.g. processing a write)
14131369Sdduvall 	 * and didn't respond to being addressed. Either way, there's
14141369Sdduvall 	 * nothing we can here ...
14151369Sdduvall 	 */
14161369Sdduvall 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
14171369Sdduvall 	return (ENODATA);
14181369Sdduvall }
14191369Sdduvall 
14201369Sdduvall /*
14211369Sdduvall  * The next two functions regulate access to the NVram (if fitted).
14221369Sdduvall  *
14231369Sdduvall  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
14241369Sdduvall  * (SPI) interface, but they can be accessed through either port. These
14251369Sdduvall  * are managed by different instance of this driver and have no software
14261369Sdduvall  * state in common.
14271369Sdduvall  *
14281369Sdduvall  * In addition (and even on a single core chip) the chip's internal
14291369Sdduvall  * firmware can access the SEEPROM/Flash, most notably after a RESET
14301369Sdduvall  * when it may download code to run internally.
14311369Sdduvall  *
14321369Sdduvall  * So we need to arbitrate between these various software agents.  For
14331369Sdduvall  * this purpose, the chip provides the Software Arbitration Register,
14341369Sdduvall  * which implements hardware(!) arbitration.
14351369Sdduvall  *
14361369Sdduvall  * This functionality didn't exist on older (5700/5701) chips, so there's
14371369Sdduvall  * nothing we can do by way of arbitration on those; also, if there's no
14381369Sdduvall  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
14391369Sdduvall  * nothing to do.
14401369Sdduvall  *
14411369Sdduvall  * The internal firmware appears to use Request 0, which is the highest
14421369Sdduvall  * priority.  So we'd like to use Request 2, leaving one higher and one
14431369Sdduvall  * lower for any future developments ... but apparently this doesn't
14441369Sdduvall  * always work.  So for now, the code uses Request 1 ;-(
14451369Sdduvall  */
14461369Sdduvall 
14471369Sdduvall #define	NVM_READ_REQ	NVM_READ_REQ1
14481369Sdduvall #define	NVM_RESET_REQ	NVM_RESET_REQ1
14491369Sdduvall #define	NVM_SET_REQ	NVM_SET_REQ1
14501369Sdduvall 
14511369Sdduvall static void bge_nvmem_relinquish(bge_t *bgep);
14521369Sdduvall #pragma	no_inline(bge_nvmem_relinquish)
14531369Sdduvall 
14541369Sdduvall static void
14551369Sdduvall bge_nvmem_relinquish(bge_t *bgep)
14561369Sdduvall {
14571369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
14581369Sdduvall 
14591369Sdduvall 	switch (bgep->chipid.nvtype) {
14601369Sdduvall 	case BGE_NVTYPE_NONE:
14611369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
14621369Sdduvall 		_NOTE(NOTREACHED)
14631369Sdduvall 		return;
14641369Sdduvall 
14651369Sdduvall 	case BGE_NVTYPE_SEEPROM:
14661369Sdduvall 		/*
14671369Sdduvall 		 * No arbitration performed, no release needed
14681369Sdduvall 		 */
14691369Sdduvall 		return;
14701369Sdduvall 
14711369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
14721369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
14731369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
14741369Sdduvall 	default:
14751369Sdduvall 		break;
14761369Sdduvall 	}
14771369Sdduvall 
14781369Sdduvall 	/*
14791369Sdduvall 	 * Our own request should be present (whether or not granted) ...
14801369Sdduvall 	 */
14811865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14821369Sdduvall 
14831369Sdduvall 	/*
14841369Sdduvall 	 * ... this will make it go away.
14851369Sdduvall 	 */
14861369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
14871865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14881369Sdduvall }
14891369Sdduvall 
14901369Sdduvall /*
14911369Sdduvall  * Arbitrate for access to the NVmem, if necessary
14921369Sdduvall  *
14931369Sdduvall  * Return value:
14941369Sdduvall  *	0 on success
14951369Sdduvall  *	EAGAIN if the device is in use (retryable)
14961369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
14971369Sdduvall  */
14981369Sdduvall static int bge_nvmem_acquire(bge_t *bgep);
14991369Sdduvall #pragma	no_inline(bge_nvmem_acquire)
15001369Sdduvall 
15011369Sdduvall static int
15021369Sdduvall bge_nvmem_acquire(bge_t *bgep)
15031369Sdduvall {
15041369Sdduvall 	uint32_t regval;
15051369Sdduvall 	uint32_t tries;
15061369Sdduvall 
15071369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15081369Sdduvall 
15091369Sdduvall 	switch (bgep->chipid.nvtype) {
15101369Sdduvall 	case BGE_NVTYPE_NONE:
15111369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15121369Sdduvall 		/*
15131369Sdduvall 		 * Access denied: no (recognisable) device fitted
15141369Sdduvall 		 */
15151369Sdduvall 		return (ENODEV);
15161369Sdduvall 
15171369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15181369Sdduvall 		/*
15191369Sdduvall 		 * Access granted: no arbitration needed (or possible)
15201369Sdduvall 		 */
15211369Sdduvall 		return (0);
15221369Sdduvall 
15231369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15241369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15251369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15261369Sdduvall 	default:
15271369Sdduvall 		/*
15281369Sdduvall 		 * Access conditional: conduct arbitration protocol
15291369Sdduvall 		 */
15301369Sdduvall 		break;
15311369Sdduvall 	}
15321369Sdduvall 
15331369Sdduvall 	/*
15341369Sdduvall 	 * We're holding the per-port mutex <genlock>, so no-one other
15352135Szh199473 	 * thread can be attempting to access the NVmem through *this*
15361369Sdduvall 	 * port. But it could be in use by the *other* port (of a 5704),
15371369Sdduvall 	 * or by the chip's internal firmware, so we have to go through
15381369Sdduvall 	 * the full (hardware) arbitration protocol ...
15391369Sdduvall 	 *
15401369Sdduvall 	 * Note that *because* we're holding <genlock>, the interrupt handler
15411369Sdduvall 	 * won't be able to progress.  So we're only willing to spin for a
15421369Sdduvall 	 * fairly short time.  Specifically:
15431369Sdduvall 	 *
15441369Sdduvall 	 *	We *must* wait long enough for the hardware to resolve all
15451369Sdduvall 	 *	requests and determine the winner.  Fortunately, this is
15461369Sdduvall 	 *	"almost instantaneous", even as observed by GHz CPUs.
15471369Sdduvall 	 *
15481369Sdduvall 	 *	A successful access by another Solaris thread (via either
15491369Sdduvall 	 *	port) typically takes ~20us.  So waiting a bit longer than
15501369Sdduvall 	 *	that will give a good chance of success, if the other user
15511369Sdduvall 	 *	*is* another thread on the other port.
15521369Sdduvall 	 *
15531369Sdduvall 	 *	However, the internal firmware can hold on to the NVmem
15541369Sdduvall 	 *	for *much* longer: at least 10 milliseconds just after a
15551369Sdduvall 	 *	RESET, and maybe even longer if the NVmem actually contains
15561369Sdduvall 	 *	code to download and run on the internal CPUs.
15571369Sdduvall 	 *
15581369Sdduvall 	 * So, we'll allow 50us; if that's not enough then it's up to the
15591369Sdduvall 	 * caller to retry later (hence the choice of return code EAGAIN).
15601369Sdduvall 	 */
15611369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15621369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
15631369Sdduvall 
15641369Sdduvall 	for (tries = 0; tries < 50; ++tries) {
15651369Sdduvall 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15661369Sdduvall 		if (regval & NVM_WON_REQ1)
15671369Sdduvall 			break;
15681369Sdduvall 		drv_usecwait(1);
15691369Sdduvall 	}
15701369Sdduvall 
15711369Sdduvall 	if (regval & NVM_WON_REQ1) {
15721369Sdduvall 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
15731369Sdduvall 		return (0);
15741369Sdduvall 	}
15751369Sdduvall 
15761369Sdduvall 	/*
15771369Sdduvall 	 * Somebody else must be accessing the NVmem, so abandon our
15781369Sdduvall 	 * attempt take control of it.  The caller can try again later ...
15791369Sdduvall 	 */
15801369Sdduvall 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
15811369Sdduvall 	bge_nvmem_relinquish(bgep);
15821369Sdduvall 	return (EAGAIN);
15831369Sdduvall }
15841369Sdduvall 
15851369Sdduvall /*
15861369Sdduvall  * This code assumes that the GPIO1 bit has been wired up to the NVmem
15871369Sdduvall  * write protect line in such a way that the NVmem is protected when
15881369Sdduvall  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
15891369Sdduvall  * NVmem writable we have to change GPIO1 to an output AND drive it low.
15901369Sdduvall  *
15911369Sdduvall  * Note: there's only one set of GPIO pins on a 5704, even though they
15921369Sdduvall  * can be accessed through either port.  So the chip has to resolve what
15931369Sdduvall  * happens if the two ports program a single pin differently ... the rule
15941369Sdduvall  * it uses is that if the ports disagree about the *direction* of a pin,
15951369Sdduvall  * "output" wins over "input", but if they disagree about its *value* as
15961369Sdduvall  * an output, then the pin is TRISTATED instead!  In such a case, no-one
15971369Sdduvall  * wins, and the external signal does whatever the external circuitry
15981369Sdduvall  * defines as the default -- which we've assumed is the PROTECTED state.
15991369Sdduvall  * So, we always change GPIO1 back to being an *input* whenever we're not
16001369Sdduvall  * specifically using it to unprotect the NVmem. This allows either port
16012135Szh199473  * to update the NVmem, although obviously only one at a time!
16021369Sdduvall  *
16031369Sdduvall  * The caller should hold <genlock> and *also* have already acquired the
16041369Sdduvall  * right to access the NVmem, via bge_nvmem_acquire() above.
16051369Sdduvall  */
16061369Sdduvall static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
16071369Sdduvall #pragma	inline(bge_nvmem_protect)
16081369Sdduvall 
16091369Sdduvall static void
16101369Sdduvall bge_nvmem_protect(bge_t *bgep, boolean_t protect)
16111369Sdduvall {
16121369Sdduvall 	uint32_t regval;
16131369Sdduvall 
16141369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
16151369Sdduvall 
16161369Sdduvall 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
16171369Sdduvall 	if (protect) {
16181369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_1;
16191369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16201369Sdduvall 	} else {
16211369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
16221369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16231369Sdduvall 	}
16241369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
16251369Sdduvall }
16261369Sdduvall 
16271369Sdduvall /*
16281369Sdduvall  * Now put it all together ...
16291369Sdduvall  *
16301369Sdduvall  * Try to acquire control of the NVmem; if successful, then:
16311369Sdduvall  *	unprotect it (if we want to write to it)
16321369Sdduvall  *	perform the requested access
16331369Sdduvall  *	reprotect it (after a write)
16341369Sdduvall  *	relinquish control
16351369Sdduvall  *
16361369Sdduvall  * Return value:
16371369Sdduvall  *	0 on success,
16381369Sdduvall  *	EAGAIN if the device is in use (retryable)
16391369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
16401369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
16411369Sdduvall  *	EPROTO on other h/w or s/w errors.
16421369Sdduvall  */
16431369Sdduvall static int
16441369Sdduvall bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
16451369Sdduvall {
16461369Sdduvall 	int err;
16471369Sdduvall 
16481369Sdduvall 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
16491369Sdduvall 		switch (cmd) {
16501369Sdduvall 		case BGE_SEE_READ:
16511369Sdduvall 			err = bge_seeprom_access(bgep,
16521369Sdduvall 			    SEEPROM_ACCESS_READ, addr, dp);
16531369Sdduvall 			break;
16541369Sdduvall 
16551369Sdduvall 		case BGE_SEE_WRITE:
16561369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16571369Sdduvall 			err = bge_seeprom_access(bgep,
16581369Sdduvall 			    SEEPROM_ACCESS_WRITE, addr, dp);
16591369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16601369Sdduvall 			break;
16611369Sdduvall 
16621369Sdduvall 		case BGE_FLASH_READ:
16631369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16641369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16651369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16661369Sdduvall 				    NVM_ACCESS_ENABLE);
16671369Sdduvall 			}
16681369Sdduvall 			err = bge_flash_access(bgep,
16691369Sdduvall 			    NVM_FLASH_CMD_RD, addr, dp);
16701369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16711369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16721369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16731369Sdduvall 				    NVM_ACCESS_ENABLE);
16741369Sdduvall 			}
16751369Sdduvall 			break;
16761369Sdduvall 
16771369Sdduvall 		case BGE_FLASH_WRITE:
16781369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16791369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16801369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16811369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16821369Sdduvall 			}
16831369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16841369Sdduvall 			err = bge_flash_access(bgep,
16851369Sdduvall 			    NVM_FLASH_CMD_WR, addr, dp);
16861369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16871369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16881369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16891369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16901369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16911369Sdduvall 			}
16921369Sdduvall 
16931369Sdduvall 			break;
16941369Sdduvall 
16951369Sdduvall 		default:
16961369Sdduvall 			_NOTE(NOTREACHED)
16971369Sdduvall 			break;
16981369Sdduvall 		}
16991369Sdduvall 		bge_nvmem_relinquish(bgep);
17001369Sdduvall 	}
17011369Sdduvall 
17021369Sdduvall 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
17031369Sdduvall 	return (err);
17041369Sdduvall }
17051369Sdduvall 
17061369Sdduvall /*
17071369Sdduvall  * Attempt to get a MAC address from the SEEPROM or Flash, if any
17081369Sdduvall  */
17091369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17101369Sdduvall #pragma no_inline(bge_get_nvmac)
17111369Sdduvall 
17121369Sdduvall static uint64_t
17131369Sdduvall bge_get_nvmac(bge_t *bgep)
17141369Sdduvall {
17151369Sdduvall 	uint32_t mac_high;
17161369Sdduvall 	uint32_t mac_low;
17171369Sdduvall 	uint32_t addr;
17181369Sdduvall 	uint32_t cmd;
17191369Sdduvall 	uint64_t mac;
17201369Sdduvall 
17211369Sdduvall 	BGE_TRACE(("bge_get_nvmac($%p)",
17221369Sdduvall 		(void *)bgep));
17231369Sdduvall 
17241369Sdduvall 	switch (bgep->chipid.nvtype) {
17251369Sdduvall 	case BGE_NVTYPE_NONE:
17261369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
17271369Sdduvall 	default:
17281369Sdduvall 		return (0ULL);
17291369Sdduvall 
17301369Sdduvall 	case BGE_NVTYPE_SEEPROM:
17311369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
17321369Sdduvall 		cmd = BGE_SEE_READ;
17331369Sdduvall 		break;
17341369Sdduvall 
17351369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
17361369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
17371369Sdduvall 		cmd = BGE_FLASH_READ;
17381369Sdduvall 		break;
17391369Sdduvall 	}
17401369Sdduvall 
17411369Sdduvall 	addr = NVMEM_DATA_MAC_ADDRESS;
17421369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
17431369Sdduvall 		return (0ULL);
17441369Sdduvall 	addr += 4;
17451369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
17461369Sdduvall 		return (0ULL);
17471369Sdduvall 
17481369Sdduvall 	/*
17491369Sdduvall 	 * The Broadcom chip is natively BIG-endian, so that's how the
17501369Sdduvall 	 * MAC address is represented in NVmem.  We may need to swap it
17511369Sdduvall 	 * around on a little-endian host ...
17521369Sdduvall 	 */
17531369Sdduvall #ifdef	_BIG_ENDIAN
17541369Sdduvall 	mac = mac_high;
17551369Sdduvall 	mac = mac << 32;
17561369Sdduvall 	mac |= mac_low;
17571369Sdduvall #else
17581369Sdduvall 	mac = BGE_BSWAP_32(mac_high);
17591369Sdduvall 	mac = mac << 32;
17601369Sdduvall 	mac |= BGE_BSWAP_32(mac_low);
17611369Sdduvall #endif	/* _BIG_ENDIAN */
17621369Sdduvall 
17631369Sdduvall 	return (mac);
17641369Sdduvall }
17651369Sdduvall 
17661369Sdduvall #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17671369Sdduvall 
17681369Sdduvall /*
17691369Sdduvall  * Dummy version for when we're not supporting NVmem access
17701369Sdduvall  */
17711369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17721369Sdduvall #pragma inline(bge_get_nvmac)
17731369Sdduvall 
17741369Sdduvall static uint64_t
17751369Sdduvall bge_get_nvmac(bge_t *bgep)
17761369Sdduvall {
17771369Sdduvall 	_NOTE(ARGUNUSED(bgep))
17781369Sdduvall 	return (0ULL);
17791369Sdduvall }
17801369Sdduvall 
17811369Sdduvall #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17821369Sdduvall 
17831369Sdduvall /*
17841369Sdduvall  * Determine the type of NVmem that is (or may be) attached to this chip,
17851369Sdduvall  */
17861369Sdduvall static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
17871369Sdduvall #pragma no_inline(bge_nvmem_id)
17881369Sdduvall 
17891369Sdduvall static enum bge_nvmem_type
17901369Sdduvall bge_nvmem_id(bge_t *bgep)
17911369Sdduvall {
17921369Sdduvall 	enum bge_nvmem_type nvtype;
17931369Sdduvall 	uint32_t config1;
17941369Sdduvall 
17951369Sdduvall 	BGE_TRACE(("bge_nvmem_id($%p)",
17961369Sdduvall 		(void *)bgep));
17971369Sdduvall 
17981369Sdduvall 	switch (bgep->chipid.device) {
17991369Sdduvall 	default:
18001369Sdduvall 		/*
18011369Sdduvall 		 * We shouldn't get here; it means we don't recognise
18021369Sdduvall 		 * the chip, which means we don't know how to determine
18031369Sdduvall 		 * what sort of NVmem (if any) it has.  So we'll say
18041369Sdduvall 		 * NONE, to disable the NVmem access code ...
18051369Sdduvall 		 */
18061369Sdduvall 		nvtype = BGE_NVTYPE_NONE;
18071369Sdduvall 		break;
18081369Sdduvall 
18091369Sdduvall 	case DEVICE_ID_5700:
18101369Sdduvall 	case DEVICE_ID_5700x:
18111369Sdduvall 	case DEVICE_ID_5701:
18121369Sdduvall 		/*
18131369Sdduvall 		 * These devices support *only* SEEPROMs
18141369Sdduvall 		 */
18151369Sdduvall 		nvtype = BGE_NVTYPE_SEEPROM;
18161369Sdduvall 		break;
18171369Sdduvall 
18181369Sdduvall 	case DEVICE_ID_5702:
18191369Sdduvall 	case DEVICE_ID_5702fe:
18201369Sdduvall 	case DEVICE_ID_5703C:
18211369Sdduvall 	case DEVICE_ID_5703S:
18221369Sdduvall 	case DEVICE_ID_5704C:
18231369Sdduvall 	case DEVICE_ID_5704S:
18241369Sdduvall 	case DEVICE_ID_5704:
18251369Sdduvall 	case DEVICE_ID_5705M:
18261369Sdduvall 	case DEVICE_ID_5705C:
18273170Sml149210 	case DEVICE_ID_5705_2:
18281369Sdduvall 	case DEVICE_ID_5706:
18291369Sdduvall 	case DEVICE_ID_5782:
18301369Sdduvall 	case DEVICE_ID_5788:
18312135Szh199473 	case DEVICE_ID_5789:
18321369Sdduvall 	case DEVICE_ID_5751:
18331369Sdduvall 	case DEVICE_ID_5751M:
18342675Szh199473 	case DEVICE_ID_5752:
18352675Szh199473 	case DEVICE_ID_5752M:
18361369Sdduvall 	case DEVICE_ID_5721:
18371369Sdduvall 	case DEVICE_ID_5714C:
18381369Sdduvall 	case DEVICE_ID_5714S:
18391369Sdduvall 	case DEVICE_ID_5715C:
18403170Sml149210 	case DEVICE_ID_5715S:
18411369Sdduvall 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
18421369Sdduvall 		if (config1 & NVM_CFG1_FLASH_MODE)
18431369Sdduvall 			if (config1 & NVM_CFG1_BUFFERED_MODE)
18441369Sdduvall 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
18451369Sdduvall 			else
18461369Sdduvall 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
18471369Sdduvall 		else
18481369Sdduvall 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
18491369Sdduvall 		break;
18501369Sdduvall 	}
18511369Sdduvall 
18521369Sdduvall 	return (nvtype);
18531369Sdduvall }
18541369Sdduvall 
18551369Sdduvall #undef	BGE_DBG
18561369Sdduvall #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
18571369Sdduvall 
18581369Sdduvall static void
18591369Sdduvall bge_init_recv_rule(bge_t *bgep)
18601369Sdduvall {
18611369Sdduvall 	bge_recv_rule_t *rulep;
18621369Sdduvall 	uint32_t i;
18631369Sdduvall 
18641369Sdduvall 	/*
18651369Sdduvall 	 * receive rule: direct all TCP traffic to ring RULE_MATCH_TO_RING
18661369Sdduvall 	 * 1. to direct UDP traffic, set:
18671369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18681369Sdduvall 	 * 	rulep->mask_value = RULE_UDP_MASK_VALUE;
18691369Sdduvall 	 * 2. to direct ICMP traffic, set:
18701369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18711369Sdduvall 	 * 	rulep->mask_value = RULE_ICMP_MASK_VALUE;
18721369Sdduvall 	 * 3. to direct traffic by source ip, set:
18731369Sdduvall 	 * 	rulep->control = RULE_SIP_CONTROL;
18741369Sdduvall 	 * 	rulep->mask_value = RULE_SIP_MASK_VALUE;
18751369Sdduvall 	 */
18761369Sdduvall 	rulep = bgep->recv_rules;
18771369Sdduvall 	rulep->control = RULE_PROTO_CONTROL;
18781369Sdduvall 	rulep->mask_value = RULE_TCP_MASK_VALUE;
18791369Sdduvall 
18801369Sdduvall 	/*
18811369Sdduvall 	 * set receive rule registers
18821369Sdduvall 	 */
18831369Sdduvall 	rulep = bgep->recv_rules;
18841369Sdduvall 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
18851369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
18861369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
18871369Sdduvall 	}
18881369Sdduvall }
18891369Sdduvall 
18901369Sdduvall /*
18911369Sdduvall  * Using the values captured by bge_chip_cfg_init(), and additional probes
18921369Sdduvall  * as required, characterise the chip fully: determine the label by which
18931369Sdduvall  * to refer to this chip, the correct settings for various registers, and
18941369Sdduvall  * of course whether the device and/or subsystem are supported!
18951369Sdduvall  */
18961865Sdilpreet int bge_chip_id_init(bge_t *bgep);
18971369Sdduvall #pragma	no_inline(bge_chip_id_init)
18981369Sdduvall 
18991865Sdilpreet int
19001369Sdduvall bge_chip_id_init(bge_t *bgep)
19011369Sdduvall {
19021369Sdduvall 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
19031369Sdduvall 	boolean_t sys_ok;
19041369Sdduvall 	boolean_t dev_ok;
19051369Sdduvall 	chip_id_t *cidp;
19061369Sdduvall 	uint32_t subid;
19071369Sdduvall 	char *devname;
19081369Sdduvall 	char *sysname;
19091369Sdduvall 	int *ids;
19101369Sdduvall 	int err;
19111369Sdduvall 	uint_t i;
19121369Sdduvall 
19131369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_INITIAL);
19141369Sdduvall 
19151369Sdduvall 	sys_ok = dev_ok = B_FALSE;
19161369Sdduvall 	cidp = &bgep->chipid;
19171369Sdduvall 
19181369Sdduvall 	/*
19191369Sdduvall 	 * Check the PCI device ID to determine the generic chip type and
19201369Sdduvall 	 * select parameters that depend on this.
19211369Sdduvall 	 *
19221369Sdduvall 	 * Note: because the SPARC platforms in general don't fit the
19231369Sdduvall 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
19241369Sdduvall 	 * as zero - which is why we use <asic_rev> rather than <revision>
19251369Sdduvall 	 * below ...
19261369Sdduvall 	 *
19271369Sdduvall 	 * Note: in general we can't distinguish between the Copper/SerDes
19281369Sdduvall 	 * versions by ID alone, as some Copper devices (e.g. some but not
19291369Sdduvall 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
19301369Sdduvall 	 * treat them the same here, and the MII code works out the media
19311369Sdduvall 	 * type later on ...
19321369Sdduvall 	 */
19331369Sdduvall 	cidp->mbuf_base = bge_mbuf_pool_base;
19341369Sdduvall 	cidp->mbuf_length = bge_mbuf_pool_len;
19351369Sdduvall 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
19361369Sdduvall 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
19371369Sdduvall 	cidp->pci_type = BGE_PCI_X;
19381369Sdduvall 	cidp->statistic_type = BGE_STAT_BLK;
19391908Sly149593 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
19401908Sly149593 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
19411908Sly149593 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
19421369Sdduvall 
19431369Sdduvall 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
19441369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
19451369Sdduvall 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
19461369Sdduvall 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
19471369Sdduvall 
19481369Sdduvall 	cidp->msi_enabled = B_FALSE;
19491369Sdduvall 
19501369Sdduvall 	switch (cidp->device) {
19511369Sdduvall 	case DEVICE_ID_5700:
19521369Sdduvall 	case DEVICE_ID_5700x:
19531369Sdduvall 		cidp->chip_label = 5700;
19542135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19551369Sdduvall 		break;
19561369Sdduvall 
19571369Sdduvall 	case DEVICE_ID_5701:
19581369Sdduvall 		cidp->chip_label = 5701;
19591369Sdduvall 		dev_ok = B_TRUE;
19602135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19611369Sdduvall 		break;
19621369Sdduvall 
19631369Sdduvall 	case DEVICE_ID_5702:
19641369Sdduvall 	case DEVICE_ID_5702fe:
19651369Sdduvall 		cidp->chip_label = 5702;
19661369Sdduvall 		dev_ok = B_TRUE;
19672135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19682135Szh199473 		cidp->pci_type = BGE_PCI;
19691369Sdduvall 		break;
19701369Sdduvall 
19711369Sdduvall 	case DEVICE_ID_5703C:
19721369Sdduvall 	case DEVICE_ID_5703S:
19731369Sdduvall 	case DEVICE_ID_5703:
19741369Sdduvall 		/*
19751369Sdduvall 		 * Revision A0 of the 5703/5793 had various errata
19761369Sdduvall 		 * that we can't or don't work around, so it's not
19771369Sdduvall 		 * supported, but all later versions are
19781369Sdduvall 		 */
19791369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
19801369Sdduvall 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
19811369Sdduvall 			dev_ok = B_TRUE;
19822135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19831369Sdduvall 		break;
19841369Sdduvall 
19851369Sdduvall 	case DEVICE_ID_5704C:
19861369Sdduvall 	case DEVICE_ID_5704S:
19871369Sdduvall 	case DEVICE_ID_5704:
19881369Sdduvall 		/*
19891369Sdduvall 		 * Revision A0 of the 5704/5794 had various errata
19901369Sdduvall 		 * but we have workarounds, so it *is* supported.
19911369Sdduvall 		 */
19921369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
19931369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
19941369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
19951369Sdduvall 		dev_ok = B_TRUE;
19962135Szh199473 		if (cidp->asic_rev <  MHCR_CHIP_REV_5704_B0)
19972135Szh199473 			cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
19981369Sdduvall 		break;
19991369Sdduvall 
20001369Sdduvall 	case DEVICE_ID_5705C:
20011369Sdduvall 	case DEVICE_ID_5705M:
20021369Sdduvall 	case DEVICE_ID_5705MA3:
20031369Sdduvall 	case DEVICE_ID_5705F:
20043170Sml149210 	case DEVICE_ID_5705_2:
20051369Sdduvall 		cidp->chip_label = 5705;
20061908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20071908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20081908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20091369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20101369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20111369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20121369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20131908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20141369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20152135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20162135Szh199473 		cidp->pci_type = BGE_PCI;
20171369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20181369Sdduvall 		dev_ok = B_TRUE;
20191369Sdduvall 		break;
20201369Sdduvall 
20211369Sdduvall 	case DEVICE_ID_5706:
20221369Sdduvall 		cidp->chip_label = 5706;
20231369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20241369Sdduvall 		break;
20251369Sdduvall 
20261369Sdduvall 	case DEVICE_ID_5782:
20271369Sdduvall 		/*
20281369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20291369Sdduvall 		 */
20301369Sdduvall 		cidp->chip_label = 5782;
20311908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20321908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20331908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20341369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20351369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20361369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20371369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20381908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20391369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20402135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20411369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20421369Sdduvall 		dev_ok = B_TRUE;
20431369Sdduvall 		break;
20441369Sdduvall 
20451369Sdduvall 	case DEVICE_ID_5788:
20461369Sdduvall 		/*
20471369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20481369Sdduvall 		 */
20491369Sdduvall 		cidp->chip_label = 5788;
20501908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20511908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20521908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20531369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20541369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20551369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20561369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20571908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20581369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20591369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20601369Sdduvall 		dev_ok = B_TRUE;
20611369Sdduvall 		break;
20621369Sdduvall 
20631369Sdduvall 	case DEVICE_ID_5714C:
20641369Sdduvall 		if (cidp->revision >= REVISION_ID_5714_A2)
20651369Sdduvall 			cidp->msi_enabled = bge_enable_msi;
20661369Sdduvall 		/* FALLTHRU */
20671369Sdduvall 	case DEVICE_ID_5714S:
20681369Sdduvall 		cidp->chip_label = 5714;
20691908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20701908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20711908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20721369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20731369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20741369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20751369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
20761369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
20771369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20781908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20791369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20801369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20811369Sdduvall 		dev_ok = B_TRUE;
20821369Sdduvall 		break;
20831369Sdduvall 
20841369Sdduvall 	case DEVICE_ID_5715C:
20853170Sml149210 	case DEVICE_ID_5715S:
20861369Sdduvall 		cidp->chip_label = 5715;
20871908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20881908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20891908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20901369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20911369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20921369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20931369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
20941369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
20951369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20961908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20971369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20981369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20991908Sly149593 		if (cidp->revision >= REVISION_ID_5715_A2)
21001908Sly149593 			cidp->msi_enabled = bge_enable_msi;
21011369Sdduvall 		dev_ok = B_TRUE;
21021369Sdduvall 		break;
21031369Sdduvall 
21041369Sdduvall 	case DEVICE_ID_5721:
21051369Sdduvall 		cidp->chip_label = 5721;
21061908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21071908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21081908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21091369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21101369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21111369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21121369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21131369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21141908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21151369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21161369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21171369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21181369Sdduvall 		dev_ok = B_TRUE;
21191369Sdduvall 		break;
21201369Sdduvall 
21211369Sdduvall 	case DEVICE_ID_5751:
21221369Sdduvall 	case DEVICE_ID_5751M:
21231369Sdduvall 		cidp->chip_label = 5751;
21241908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21251908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21261908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21271369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21281369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21291369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21301369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21311369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21321908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21331369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21341369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21351369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21361369Sdduvall 		dev_ok = B_TRUE;
21371369Sdduvall 		break;
21381369Sdduvall 
21392675Szh199473 	case DEVICE_ID_5752:
21402675Szh199473 	case DEVICE_ID_5752M:
21412675Szh199473 		cidp->chip_label = 5752;
21422675Szh199473 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21432675Szh199473 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21442675Szh199473 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21452675Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21462675Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21472675Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21482675Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21492675Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21502675Szh199473 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21512675Szh199473 		cidp->pci_type = BGE_PCI_E;
21522675Szh199473 		cidp->statistic_type = BGE_STAT_REG;
21532675Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21542675Szh199473 		dev_ok = B_TRUE;
21552675Szh199473 		break;
21562675Szh199473 
21572135Szh199473 	case DEVICE_ID_5789:
21582135Szh199473 		cidp->chip_label = 5789;
21592135Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21602135Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21612135Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21622135Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21632135Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21642135Szh199473 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
21652135Szh199473 		cidp->pci_type = BGE_PCI_E;
21662135Szh199473 		cidp->statistic_type = BGE_STAT_REG;
21672135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21682135Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21692135Szh199473 		cidp->msi_enabled = B_TRUE;
21702135Szh199473 		dev_ok = B_TRUE;
21712135Szh199473 		break;
21722135Szh199473 
21731369Sdduvall 	}
21741369Sdduvall 
21751369Sdduvall 	/*
21761369Sdduvall 	 * Setup the default jumbo parameter.
21771369Sdduvall 	 */
21781369Sdduvall 	cidp->ethmax_size = ETHERMAX;
21791369Sdduvall 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
21801908Sly149593 	cidp->std_buf_size = BGE_STD_BUFF_SIZE;
21811369Sdduvall 
21821369Sdduvall 	/*
21831369Sdduvall 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
21841369Sdduvall 	 * setup below jumbo specific parameters.
21851908Sly149593 	 *
21861908Sly149593 	 * For BCM5714/5715, there is only one standard receive ring. So the
21871908Sly149593 	 * std buffer size should be set to BGE_JUMBO_BUFF_SIZE when jumbo
21881908Sly149593 	 * feature is enabled.
21891369Sdduvall 	 */
21901369Sdduvall 	if (bge_jumbo_enable &&
21911369Sdduvall 	    !(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
21921369Sdduvall 	    (cidp->default_mtu > BGE_DEFAULT_MTU) &&
21931369Sdduvall 	    (cidp->default_mtu <= BGE_MAXIMUM_MTU)) {
21941908Sly149593 	    if (DEVICE_5714_SERIES_CHIPSETS(bgep)) {
21951908Sly149593 			cidp->mbuf_lo_water_rdma =
21961908Sly149593 			    RDMA_MBUF_LOWAT_5714_JUMBO;
21971908Sly149593 			cidp->mbuf_lo_water_rmac =
21981908Sly149593 			    MAC_RX_MBUF_LOWAT_5714_JUMBO;
21991908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_5714_JUMBO;
22001908Sly149593 			cidp->jumbo_slots = 0;
22011908Sly149593 			cidp->std_buf_size = BGE_JUMBO_BUFF_SIZE;
22021908Sly149593 	    } else {
22031908Sly149593 			cidp->mbuf_lo_water_rdma =
22041908Sly149593 			    RDMA_MBUF_LOWAT_JUMBO;
22051908Sly149593 			cidp->mbuf_lo_water_rmac =
22061908Sly149593 			    MAC_RX_MBUF_LOWAT_JUMBO;
22071908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
22081908Sly149593 			cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
22091908Sly149593 		}
22101369Sdduvall 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
22111369Sdduvall 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
22121369Sdduvall 		cidp->ethmax_size = cidp->default_mtu +
22131369Sdduvall 		    sizeof (struct ether_header);
22141369Sdduvall 	}
22151369Sdduvall 
22161369Sdduvall 	/*
22171369Sdduvall 	 * Identify the NV memory type: SEEPROM or Flash?
22181369Sdduvall 	 */
22191369Sdduvall 	cidp->nvtype = bge_nvmem_id(bgep);
22201369Sdduvall 
22211369Sdduvall 	/*
22221369Sdduvall 	 * Now, we want to check whether this device is part of a
22231369Sdduvall 	 * supported subsystem (e.g., on the motherboard of a Sun
22241369Sdduvall 	 * branded platform).
22251369Sdduvall 	 *
22261369Sdduvall 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
22271369Sdduvall 	 */
22281369Sdduvall 	if (cidp->subven == VENDOR_ID_SUN)
22291369Sdduvall 		sys_ok = B_TRUE;
22301369Sdduvall 
22311369Sdduvall 	/*
22321369Sdduvall 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
22331369Sdduvall 	 * Note: 0x14e41647 should *not* appear in the list, but the code
22341369Sdduvall 	 * doesn't enforce that.
22351369Sdduvall 	 */
22361369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
22371369Sdduvall 		DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
22381369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
22391369Sdduvall 		/*
22401369Sdduvall 		 * Got the list; scan for a matching subsystem vendor/device
22411369Sdduvall 		 */
22421369Sdduvall 		subid = (cidp->subven << 16) | cidp->subdev;
22431369Sdduvall 		while (i--)
22441369Sdduvall 			if (ids[i] == subid)
22451369Sdduvall 				sys_ok = B_TRUE;
22461369Sdduvall 		ddi_prop_free(ids);
22471369Sdduvall 	}
22481369Sdduvall 
22491369Sdduvall 	/*
22501369Sdduvall 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
22511369Sdduvall 	 *
22521369Sdduvall 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
22531369Sdduvall 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
22541369Sdduvall 	 * we have to check specially for the exact device paths to the
22551369Sdduvall 	 * motherboard devices on those platforms ;-(
22561369Sdduvall 	 *
22571369Sdduvall 	 * Note: we can't just use the "supported-subsystems" mechanism
22581369Sdduvall 	 * above, because the entry would have to be 0x14e41647 -- which
22591369Sdduvall 	 * would then accept *any* plugin card that *didn't* contain a
22601369Sdduvall 	 * (valid) SEEPROM ;-(
22611369Sdduvall 	 */
22621369Sdduvall 	sysname = ddi_node_name(ddi_root_node());
22631369Sdduvall 	devname = ddi_pathname(bgep->devinfo, buf);
22641369Sdduvall 	ASSERT(strlen(devname) > 0);
22651369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
22661369Sdduvall 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
22671369Sdduvall 			sys_ok = B_TRUE;
22681369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
22691369Sdduvall 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
22701369Sdduvall 			sys_ok = B_TRUE;
22711369Sdduvall 
22721369Sdduvall 	/*
22731369Sdduvall 	 * Now check what we've discovered: is this truly a supported
22741369Sdduvall 	 * chip on (the motherboard of) a supported platform?
22751369Sdduvall 	 *
22761369Sdduvall 	 * Possible problems here:
22771369Sdduvall 	 * 1)	it's a completely unheard-of chip (e.g. 5761)
22781369Sdduvall 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
22791369Sdduvall 	 * 3)	it's a chip we would support if it were on the motherboard
22801369Sdduvall 	 *	of a Sun platform, but this one isn't ;-(
22811369Sdduvall 	 */
22821369Sdduvall 	if (cidp->chip_label == 0)
22831369Sdduvall 		bge_problem(bgep,
22841369Sdduvall 			"Device 'pci%04x,%04x' not recognized (%d?)",
22851369Sdduvall 			cidp->vendor, cidp->device, cidp->device);
22861369Sdduvall 	else if (!dev_ok)
22871369Sdduvall 		bge_problem(bgep,
22881369Sdduvall 			"Device 'pci%04x,%04x' (%d) revision %d not supported",
22891369Sdduvall 			cidp->vendor, cidp->device, cidp->chip_label,
22901369Sdduvall 			cidp->revision);
22911369Sdduvall #if	BGE_DEBUGGING
22921369Sdduvall 	else if (!sys_ok)
22931369Sdduvall 		bge_problem(bgep,
22941369Sdduvall 			"%d-based subsystem 'pci%04x,%04x' not validated",
22951369Sdduvall 			cidp->chip_label, cidp->subven, cidp->subdev);
22961369Sdduvall #endif
22971369Sdduvall 	else
22981369Sdduvall 		cidp->flags |= CHIP_FLAG_SUPPORTED;
22991865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
23001865Sdilpreet 		return (EIO);
23011865Sdilpreet 	return (0);
23021369Sdduvall }
23031369Sdduvall 
23041369Sdduvall void
23051369Sdduvall bge_chip_msi_trig(bge_t *bgep)
23061369Sdduvall {
23071369Sdduvall 	uint32_t	regval;
23081369Sdduvall 
23091369Sdduvall 	regval = bgep->param_msi_cnt<<4;
23101369Sdduvall 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
23111369Sdduvall 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
23121369Sdduvall }
23131369Sdduvall 
23141369Sdduvall /*
23151369Sdduvall  * Various registers that control the chip's internal engines (state
23161369Sdduvall  * machines) have a <reset> and <enable> bits (fortunately, in the
23171369Sdduvall  * same place in each such register :-).
23181369Sdduvall  *
23191369Sdduvall  * To reset the state machine, the <reset> bit must be written with 1;
23201369Sdduvall  * it will then read back as 1 while the reset is in progress, but
23211369Sdduvall  * self-clear to 0 when the reset completes.
23221369Sdduvall  *
23231369Sdduvall  * To enable a state machine, one must set the <enable> bit, which
23241369Sdduvall  * will continue to read back as 0 until the state machine is running.
23251369Sdduvall  *
23261369Sdduvall  * To disable a state machine, the <enable> bit must be cleared, but
23271369Sdduvall  * it will continue to read back as 1 until the state machine actually
23281369Sdduvall  * stops.
23291369Sdduvall  *
23301369Sdduvall  * This routine implements polling for completion of a reset, enable
23311369Sdduvall  * or disable operation, returning B_TRUE on success (bit reached the
23321369Sdduvall  * required state) or B_FALSE on timeout (200*100us == 20ms).
23331369Sdduvall  */
23341369Sdduvall static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
23351369Sdduvall 					uint32_t mask, uint32_t val);
23361369Sdduvall #pragma	no_inline(bge_chip_poll_engine)
23371369Sdduvall 
23381369Sdduvall static boolean_t
23391369Sdduvall bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
23401369Sdduvall 	uint32_t mask, uint32_t val)
23411369Sdduvall {
23421369Sdduvall 	uint32_t regval;
23431369Sdduvall 	uint32_t n;
23441369Sdduvall 
23451369Sdduvall 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
23461369Sdduvall 		(void *)bgep, regno, mask, val));
23471369Sdduvall 
23481369Sdduvall 	for (n = 200; n; --n) {
23491369Sdduvall 		regval = bge_reg_get32(bgep, regno);
23501369Sdduvall 		if ((regval & mask) == val)
23511369Sdduvall 			return (B_TRUE);
23521369Sdduvall 		drv_usecwait(100);
23531369Sdduvall 	}
23541369Sdduvall 
23551865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_NO_RESPONSE);
23561369Sdduvall 	return (B_FALSE);
23571369Sdduvall }
23581369Sdduvall 
23591369Sdduvall /*
23601369Sdduvall  * Various registers that control the chip's internal engines (state
23611369Sdduvall  * machines) have a <reset> bit (fortunately, in the same place in
23621369Sdduvall  * each such register :-).  To reset the state machine, this bit must
23631369Sdduvall  * be written with 1; it will then read back as 1 while the reset is
23641369Sdduvall  * in progress, but self-clear to 0 when the reset completes.
23651369Sdduvall  *
23661369Sdduvall  * This code sets the bit, then polls for it to read back as zero.
23671369Sdduvall  * The return value is B_TRUE on success (reset bit cleared itself),
23681369Sdduvall  * or B_FALSE if the state machine didn't recover :(
23691369Sdduvall  *
23701369Sdduvall  * NOTE: the Core reset is similar to other resets, except that we
23711369Sdduvall  * can't poll for completion, since the Core reset disables memory
23721369Sdduvall  * access!  So we just have to assume that it will all complete in
23731369Sdduvall  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
23741369Sdduvall  */
23751369Sdduvall static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
23761369Sdduvall #pragma	no_inline(bge_chip_reset_engine)
23771369Sdduvall 
23781369Sdduvall static boolean_t
23791369Sdduvall bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
23801369Sdduvall {
23811369Sdduvall 	uint32_t regval;
23821369Sdduvall 	uint32_t val32;
23831369Sdduvall 
23841369Sdduvall 	regval = bge_reg_get32(bgep, regno);
23851369Sdduvall 
23861369Sdduvall 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
23871369Sdduvall 		(void *)bgep, regno));
23881369Sdduvall 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
23891369Sdduvall 		regno, regval));
23901369Sdduvall 
23911369Sdduvall 	regval |= STATE_MACHINE_RESET_BIT;
23921369Sdduvall 
23931369Sdduvall 	switch (regno) {
23941369Sdduvall 	case MISC_CONFIG_REG:
23951369Sdduvall 		/*
23961369Sdduvall 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
23971369Sdduvall 		 * resetting PCIE block and bringing PCIE link down, bit 29
23981369Sdduvall 		 * in the register needs to be set first, and then set it again
23991369Sdduvall 		 * while the reset bit is written.
24001369Sdduvall 		 * See:P500 of 57xx-PG102-RDS.pdf.
24011369Sdduvall 		 */
24021369Sdduvall 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
24031369Sdduvall 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
24041369Sdduvall 		    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
24051369Sdduvall 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
24061369Sdduvall 			if (bgep->chipid.pci_type == BGE_PCI_E) {
24071369Sdduvall 				if (bgep->chipid.asic_rev ==
24081369Sdduvall 				    MHCR_CHIP_REV_5751_A0 ||
24091369Sdduvall 				    bgep->chipid.asic_rev ==
24101369Sdduvall 				    MHCR_CHIP_REV_5721_A0) {
24111369Sdduvall 					val32 = bge_reg_get32(bgep,
24121369Sdduvall 					    PHY_TEST_CTRL_REG);
24131369Sdduvall 					if (val32 == (PHY_PCIE_SCRAM_MODE |
24141369Sdduvall 					    PHY_PCIE_LTASS_MODE))
24151369Sdduvall 						bge_reg_put32(bgep,
24161369Sdduvall 						    PHY_TEST_CTRL_REG,
24171369Sdduvall 						    PHY_PCIE_SCRAM_MODE);
24181369Sdduvall 					val32 = pci_config_get32
24191369Sdduvall 					    (bgep->cfg_handle,
24201369Sdduvall 					    PCI_CONF_BGE_CLKCTL);
24211369Sdduvall 					val32 |= CLKCTL_PCIE_A0_FIX;
24221369Sdduvall 					pci_config_put32(bgep->cfg_handle,
24231369Sdduvall 					    PCI_CONF_BGE_CLKCTL, val32);
24241369Sdduvall 				}
24251369Sdduvall 				bge_reg_set32(bgep, regno,
24261369Sdduvall 					MISC_CONFIG_GRC_RESET_DISABLE);
24271369Sdduvall 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
24281369Sdduvall 			}
24291369Sdduvall 		}
24301369Sdduvall 
24311369Sdduvall 		/*
24321369Sdduvall 		 * Special case - causes Core reset
24331369Sdduvall 		 *
24341369Sdduvall 		 * On SPARC v9 we want to ensure that we don't start
24351369Sdduvall 		 * timing until the I/O access has actually reached
24361369Sdduvall 		 * the chip, otherwise we might make the next access
24371369Sdduvall 		 * too early.  And we can't just force the write out
24381369Sdduvall 		 * by following it with a read (even to config space)
24391369Sdduvall 		 * because that would cause the fault we're trying
24401369Sdduvall 		 * to avoid.  Hence the need for membar_sync() here.
24411369Sdduvall 		 */
24421369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
24431369Sdduvall #ifdef	__sparcv9
24441369Sdduvall 		membar_sync();
24451369Sdduvall #endif	/* __sparcv9 */
24461369Sdduvall 		/*
24471369Sdduvall 		 * On some platforms,system need about 300us for
24481369Sdduvall 		 * link setup.
24491369Sdduvall 		 */
24501369Sdduvall 		drv_usecwait(300);
24511369Sdduvall 
24521369Sdduvall 		if (bgep->chipid.pci_type == BGE_PCI_E) {
24531369Sdduvall 			/* PCI-E device need more reset time */
24541369Sdduvall 			drv_usecwait(120000);
24551369Sdduvall 
24561369Sdduvall 			/* Set PCIE max payload size and clear error status. */
24572135Szh199473 			if ((bgep->chipid.chip_label == 5721) ||
24582135Szh199473 			    (bgep->chipid.chip_label == 5751) ||
24592675Szh199473 			    (bgep->chipid.chip_label == 5752) ||
24602135Szh199473 			    (bgep->chipid.chip_label == 5789)) {
24611369Sdduvall 				pci_config_put16(bgep->cfg_handle,
24621369Sdduvall 					PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
24631369Sdduvall 				pci_config_put16(bgep->cfg_handle,
24641369Sdduvall 					PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
24651369Sdduvall 			}
24661369Sdduvall 		}
24671369Sdduvall 
24681369Sdduvall 		BGE_PCICHK(bgep);
24691369Sdduvall 		return (B_TRUE);
24701369Sdduvall 
24711369Sdduvall 	default:
24721369Sdduvall 		bge_reg_put32(bgep, regno, regval);
24731369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
24741865Sdilpreet 		    STATE_MACHINE_RESET_BIT, 0));
24751369Sdduvall 	}
24761369Sdduvall }
24771369Sdduvall 
24781369Sdduvall /*
24791369Sdduvall  * Various registers that control the chip's internal engines (state
24801369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
24811369Sdduvall  * each such register :-).  To stop the state machine, this bit must
24821369Sdduvall  * be written with 0, then polled to see when the state machine has
24831369Sdduvall  * actually stopped.
24841369Sdduvall  *
24851369Sdduvall  * The return value is B_TRUE on success (enable bit cleared), or
24861369Sdduvall  * B_FALSE if the state machine didn't stop :(
24871369Sdduvall  */
24881369Sdduvall static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
24891369Sdduvall 						uint32_t morebits);
24901369Sdduvall #pragma	no_inline(bge_chip_disable_engine)
24911369Sdduvall 
24921369Sdduvall static boolean_t
24931369Sdduvall bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
24941369Sdduvall {
24951369Sdduvall 	uint32_t regval;
24961369Sdduvall 
24971369Sdduvall 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
24981369Sdduvall 		(void *)bgep, regno, morebits));
24991369Sdduvall 
25001369Sdduvall 	switch (regno) {
25011369Sdduvall 	case FTQ_RESET_REG:
25021369Sdduvall 		/*
25031369Sdduvall 		 * Not quite like the others; it doesn't
25041369Sdduvall 		 * have an <enable> bit, but instead we
25051369Sdduvall 		 * have to set and then clear all the bits
25061369Sdduvall 		 */
25071369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
25081369Sdduvall 		drv_usecwait(100);
25091369Sdduvall 		bge_reg_put32(bgep, regno, 0);
25101369Sdduvall 		return (B_TRUE);
25111369Sdduvall 
25121369Sdduvall 	default:
25131369Sdduvall 		regval = bge_reg_get32(bgep, regno);
25141369Sdduvall 		regval &= ~STATE_MACHINE_ENABLE_BIT;
25151369Sdduvall 		regval &= ~morebits;
25161369Sdduvall 		bge_reg_put32(bgep, regno, regval);
25171369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
25181865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, 0));
25191369Sdduvall 	}
25201369Sdduvall }
25211369Sdduvall 
25221369Sdduvall /*
25231369Sdduvall  * Various registers that control the chip's internal engines (state
25241369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
25251369Sdduvall  * each such register :-).  To start the state machine, this bit must
25261369Sdduvall  * be written with 1, then polled to see when the state machine has
25271369Sdduvall  * actually started.
25281369Sdduvall  *
25291369Sdduvall  * The return value is B_TRUE on success (enable bit set), or
25301369Sdduvall  * B_FALSE if the state machine didn't start :(
25311369Sdduvall  */
25321369Sdduvall static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
25331369Sdduvall 					uint32_t morebits);
25341369Sdduvall #pragma	no_inline(bge_chip_enable_engine)
25351369Sdduvall 
25361369Sdduvall static boolean_t
25371369Sdduvall bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
25381369Sdduvall {
25391369Sdduvall 	uint32_t regval;
25401369Sdduvall 
25411369Sdduvall 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
25421369Sdduvall 		(void *)bgep, regno, morebits));
25431369Sdduvall 
25441369Sdduvall 	switch (regno) {
25451369Sdduvall 	case FTQ_RESET_REG:
25461369Sdduvall 		/*
25471369Sdduvall 		 * Not quite like the others; it doesn't
25481369Sdduvall 		 * have an <enable> bit, but instead we
25491369Sdduvall 		 * have to set and then clear all the bits
25501369Sdduvall 		 */
25511369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
25521369Sdduvall 		drv_usecwait(100);
25531369Sdduvall 		bge_reg_put32(bgep, regno, 0);
25541369Sdduvall 		return (B_TRUE);
25551369Sdduvall 
25561369Sdduvall 	default:
25571369Sdduvall 		regval = bge_reg_get32(bgep, regno);
25581369Sdduvall 		regval |= STATE_MACHINE_ENABLE_BIT;
25591369Sdduvall 		regval |= morebits;
25601369Sdduvall 		bge_reg_put32(bgep, regno, regval);
25611369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
25621865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
25631369Sdduvall 	}
25641369Sdduvall }
25651369Sdduvall 
25661369Sdduvall /*
25671369Sdduvall  * Reprogram the Ethernet, Transmit, and Receive MAC
25681369Sdduvall  * modes to match the param_* variables
25691369Sdduvall  */
25701369Sdduvall static void bge_sync_mac_modes(bge_t *bgep);
25711369Sdduvall #pragma	no_inline(bge_sync_mac_modes)
25721369Sdduvall 
25731369Sdduvall static void
25741369Sdduvall bge_sync_mac_modes(bge_t *bgep)
25751369Sdduvall {
25761369Sdduvall 	uint32_t macmode;
25771369Sdduvall 	uint32_t regval;
25781369Sdduvall 
25791369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
25801369Sdduvall 
25811369Sdduvall 	/*
25821369Sdduvall 	 * Reprogram the Ethernet MAC mode ...
25831369Sdduvall 	 */
25841369Sdduvall 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
25851369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
25861369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
25871369Sdduvall 		macmode &= ~ETHERNET_MODE_LINK_POLARITY;
25881369Sdduvall 	else
25891369Sdduvall 		macmode |= ETHERNET_MODE_LINK_POLARITY;
25901369Sdduvall 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
25911369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
25921369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
25931369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_TBI;
25941369Sdduvall 	else if (bgep->param_link_speed == 10 || bgep->param_link_speed == 100)
25951369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_MII;
25961369Sdduvall 	else
25971369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
25981369Sdduvall 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
25991369Sdduvall 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
26001369Sdduvall 	else
26011369Sdduvall 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
26021369Sdduvall 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
26031369Sdduvall 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
26041369Sdduvall 	else
26051369Sdduvall 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
26061369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
26071369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
26081369Sdduvall 		(void *)bgep, regval, macmode));
26091369Sdduvall 
26101369Sdduvall 	/*
26111369Sdduvall 	 * ... the Transmit MAC mode ...
26121369Sdduvall 	 */
26131369Sdduvall 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
26141369Sdduvall 	if (bgep->param_link_tx_pause)
26151369Sdduvall 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
26161369Sdduvall 	else
26171369Sdduvall 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
26181369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
26191369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
26201369Sdduvall 		(void *)bgep, regval, macmode));
26211369Sdduvall 
26221369Sdduvall 	/*
26231369Sdduvall 	 * ... and the Receive MAC mode
26241369Sdduvall 	 */
26251369Sdduvall 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
26261369Sdduvall 	if (bgep->param_link_rx_pause)
26271369Sdduvall 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
26281369Sdduvall 	else
26291369Sdduvall 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
26301369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
26311369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
26321369Sdduvall 		(void *)bgep, regval, macmode));
26331369Sdduvall }
26341369Sdduvall 
26351369Sdduvall /*
26361369Sdduvall  * bge_chip_sync() -- program the chip with the unicast MAC address,
26371369Sdduvall  * the multicast hash table, the required level of promiscuity, and
26381369Sdduvall  * the current loopback mode ...
26391369Sdduvall  */
26401408Srandyf #ifdef BGE_IPMI_ASF
26411865Sdilpreet int bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
26421408Srandyf #else
26431865Sdilpreet int bge_chip_sync(bge_t *bgep);
26441408Srandyf #endif
26451369Sdduvall #pragma	no_inline(bge_chip_sync)
26461369Sdduvall 
26471865Sdilpreet int
26481408Srandyf #ifdef BGE_IPMI_ASF
26491408Srandyf bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
26501408Srandyf #else
26511369Sdduvall bge_chip_sync(bge_t *bgep)
26521408Srandyf #endif
26531369Sdduvall {
26541369Sdduvall 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
26551369Sdduvall 	boolean_t promisc;
26561369Sdduvall 	uint64_t macaddr;
26571369Sdduvall 	uint32_t fill;
26582331Skrgopi 	int i, j;
26591865Sdilpreet 	int retval = DDI_SUCCESS;
26601369Sdduvall 
26611369Sdduvall 	BGE_TRACE(("bge_chip_sync($%p)",
26621369Sdduvall 		(void *)bgep));
26631369Sdduvall 
26641369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
26651369Sdduvall 
26661369Sdduvall 	promisc = B_FALSE;
26671369Sdduvall 	fill = ~(uint32_t)0;
26681369Sdduvall 
26691369Sdduvall 	if (bgep->promisc)
26701369Sdduvall 		promisc = B_TRUE;
26711369Sdduvall 	else
26721369Sdduvall 		fill = (uint32_t)0;
26731369Sdduvall 
26741369Sdduvall 	/*
26751369Sdduvall 	 * If the TX/RX MAC engines are already running, we should stop
26761369Sdduvall 	 * them (and reset the RX engine) before changing the parameters.
26771369Sdduvall 	 * If they're not running, this will have no effect ...
26781369Sdduvall 	 *
26791369Sdduvall 	 * NOTE: this is currently disabled by default because stopping
26801369Sdduvall 	 * and restarting the Tx engine may cause an outgoing packet in
26811369Sdduvall 	 * transit to be truncated.  Also, stopping and restarting the
26821369Sdduvall 	 * Rx engine seems to not work correctly on the 5705.  Testing
26831369Sdduvall 	 * has not (yet!) revealed any problems with NOT stopping and
26841369Sdduvall 	 * restarting these engines (and Broadcom say their drivers don't
26851369Sdduvall 	 * do this), but if it is found to cause problems, this variable
26861369Sdduvall 	 * can be patched to re-enable the old behaviour ...
26871369Sdduvall 	 */
26881369Sdduvall 	if (bge_stop_start_on_sync) {
26891408Srandyf #ifdef BGE_IPMI_ASF
26901865Sdilpreet 		if (!bgep->asf_enabled) {
26911865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
26921865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
26931865Sdilpreet 				retval = DDI_FAILURE;
26941408Srandyf 		} else {
26951865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
26961865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
26971865Sdilpreet 				retval = DDI_FAILURE;
26981408Srandyf 		}
26991408Srandyf #else
27001865Sdilpreet 		if (!bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
27011865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
27021865Sdilpreet 			retval = DDI_FAILURE;
27031408Srandyf #endif
27041865Sdilpreet 		if (!bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
27051865Sdilpreet 			retval = DDI_FAILURE;
27061865Sdilpreet 		if (!bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG))
27071865Sdilpreet 			retval = DDI_FAILURE;
27081369Sdduvall 	}
27091369Sdduvall 
27101369Sdduvall 	/*
27111369Sdduvall 	 * Reprogram the hashed multicast address table ...
27121369Sdduvall 	 */
27131369Sdduvall 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
27141369Sdduvall 		bge_reg_put32(bgep, MAC_HASH_REG(i),
27151369Sdduvall 			bgep->mcast_hash[i] | fill);
27161369Sdduvall 
27171408Srandyf #ifdef BGE_IPMI_ASF
27181408Srandyf 	if (!bgep->asf_enabled || !asf_keeplive) {
27191408Srandyf #endif
27201408Srandyf 		/*
27212331Skrgopi 		 * Transform the MAC address(es) from host to chip format, then
27221408Srandyf 		 * reprogram the transmit random backoff seed and the unicast
27231408Srandyf 		 * MAC address(es) ...
27241408Srandyf 		 */
27252331Skrgopi 		for (j = 0; j < MAC_ADDRESS_REGS_MAX; j++) {
27262331Skrgopi 			for (i = 0, fill = 0, macaddr = 0ull;
27272331Skrgopi 			    i < ETHERADDRL; ++i) {
27282331Skrgopi 				macaddr <<= 8;
27292331Skrgopi 				macaddr |= bgep->curr_addr[j].addr[i];
27302331Skrgopi 				fill += bgep->curr_addr[j].addr[i];
27312331Skrgopi 			}
27322331Skrgopi 			bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
27332331Skrgopi 			bge_reg_put64(bgep, MAC_ADDRESS_REG(j), macaddr);
27341408Srandyf 		}
27351408Srandyf 
27361408Srandyf 		BGE_DEBUG(("bge_chip_sync($%p) setting MAC address %012llx",
27371408Srandyf 			(void *)bgep, macaddr));
27381408Srandyf #ifdef BGE_IPMI_ASF
27391369Sdduvall 	}
27401408Srandyf #endif
27411369Sdduvall 
27421369Sdduvall 	/*
27431369Sdduvall 	 * Set or clear the PROMISCUOUS mode bit
27441369Sdduvall 	 */
27451369Sdduvall 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
27461369Sdduvall 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
27471369Sdduvall 
27481369Sdduvall 	/*
27491369Sdduvall 	 * Sync the rest of the MAC modes too ...
27501369Sdduvall 	 */
27511369Sdduvall 	bge_sync_mac_modes(bgep);
27521369Sdduvall 
27531369Sdduvall 	/*
27541369Sdduvall 	 * Restart RX/TX MAC engines if required ...
27551369Sdduvall 	 */
27561369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
27571865Sdilpreet 		if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
27581865Sdilpreet 			retval = DDI_FAILURE;
27591408Srandyf #ifdef BGE_IPMI_ASF
27601865Sdilpreet 		if (!bgep->asf_enabled) {
27611865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
27621865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
27631865Sdilpreet 				retval = DDI_FAILURE;
27641408Srandyf 		} else {
27651865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
27661865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
27671865Sdilpreet 				retval = DDI_FAILURE;
27681408Srandyf 		}
27691408Srandyf #else
27701865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
27711865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
27721865Sdilpreet 			retval = DDI_FAILURE;
27731408Srandyf #endif
27741369Sdduvall 	}
27751865Sdilpreet 	return (retval);
27761369Sdduvall }
27771369Sdduvall 
27781369Sdduvall /*
27791369Sdduvall  * This array defines the sequence of state machine control registers
27801369Sdduvall  * in which the <enable> bit must be cleared to bring the chip to a
27811369Sdduvall  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
27821369Sdduvall  */
27831369Sdduvall static bge_regno_t shutdown_engine_regs[] = {
27841369Sdduvall 	RECEIVE_MAC_MODE_REG,
27851369Sdduvall 	RCV_BD_INITIATOR_MODE_REG,
27861369Sdduvall 	RCV_LIST_PLACEMENT_MODE_REG,
27871369Sdduvall 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
27881369Sdduvall 	RCV_DATA_BD_INITIATOR_MODE_REG,
27891369Sdduvall 	RCV_DATA_COMPLETION_MODE_REG,
27901369Sdduvall 	RCV_BD_COMPLETION_MODE_REG,
27911369Sdduvall 
27921369Sdduvall 	SEND_BD_SELECTOR_MODE_REG,
27931369Sdduvall 	SEND_BD_INITIATOR_MODE_REG,
27941369Sdduvall 	SEND_DATA_INITIATOR_MODE_REG,
27951369Sdduvall 	READ_DMA_MODE_REG,
27961369Sdduvall 	SEND_DATA_COMPLETION_MODE_REG,
27971369Sdduvall 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
27981369Sdduvall 	SEND_BD_COMPLETION_MODE_REG,
27991369Sdduvall 	TRANSMIT_MAC_MODE_REG,
28001369Sdduvall 
28011369Sdduvall 	HOST_COALESCE_MODE_REG,
28021369Sdduvall 	WRITE_DMA_MODE_REG,
28031369Sdduvall 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
28041369Sdduvall 	FTQ_RESET_REG,		/* special - see code	*/
28051369Sdduvall 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
28061369Sdduvall 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
28071369Sdduvall 	BGE_REGNO_NONE		/* terminator		*/
28081369Sdduvall };
28091369Sdduvall 
28101369Sdduvall /*
28111369Sdduvall  * bge_chip_stop() -- stop all chip processing
28121369Sdduvall  *
28131369Sdduvall  * If the <fault> parameter is B_TRUE, we're stopping the chip because
28141369Sdduvall  * we've detected a problem internally; otherwise, this is a normal
28151369Sdduvall  * (clean) stop (at user request i.e. the last STREAM has been closed).
28161369Sdduvall  */
28171369Sdduvall void bge_chip_stop(bge_t *bgep, boolean_t fault);
28181369Sdduvall #pragma	no_inline(bge_chip_stop)
28191369Sdduvall 
28201369Sdduvall void
28211369Sdduvall bge_chip_stop(bge_t *bgep, boolean_t fault)
28221369Sdduvall {
28231369Sdduvall 	bge_regno_t regno;
28241369Sdduvall 	bge_regno_t *rbp;
28251369Sdduvall 	boolean_t ok;
28261369Sdduvall 
28271369Sdduvall 	BGE_TRACE(("bge_chip_stop($%p)",
28281369Sdduvall 		(void *)bgep));
28291369Sdduvall 
28301369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
28311369Sdduvall 
28321369Sdduvall 	rbp = shutdown_engine_regs;
28331369Sdduvall 	/*
28341369Sdduvall 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
28351369Sdduvall 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
28361369Sdduvall 	 * -ory arbiter should not be disabled.
28371369Sdduvall 	 */
28381369Sdduvall 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
28391369Sdduvall 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
28401369Sdduvall 			    ok &= bge_chip_disable_engine(bgep, regno, 0);
28411369Sdduvall 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
28421369Sdduvall 				    (regno != DMA_COMPLETION_MODE_REG) &&
28431369Sdduvall 				    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
28441369Sdduvall 				    (regno != BUFFER_MANAGER_MODE_REG) &&
28451369Sdduvall 				    (regno != MEMORY_ARBITER_MODE_REG))
28461369Sdduvall 					ok &= bge_chip_disable_engine(bgep,
28471369Sdduvall 					    regno, 0);
28481369Sdduvall 	}
28491369Sdduvall 
28501865Sdilpreet 	if (!ok && !fault)
28511865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
28521865Sdilpreet 
28531369Sdduvall 	/*
28541369Sdduvall 	 * Finally, disable (all) MAC events & clear the MAC status
28551369Sdduvall 	 */
28561369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
28571369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
28581369Sdduvall 
28591369Sdduvall 	/*
28601865Sdilpreet 	 * if we're stopping the chip because of a detected fault then do
28611865Sdilpreet 	 * appropriate actions
28621369Sdduvall 	 */
28631865Sdilpreet 	if (fault) {
28641865Sdilpreet 		if (bgep->bge_chip_state != BGE_CHIP_FAULT) {
28651865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_FAULT;
28661865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
28671865Sdilpreet 			if (bgep->bge_dma_error) {
28681865Sdilpreet 				/*
28691865Sdilpreet 				 * need to free buffers in case the fault was
28701865Sdilpreet 				 * due to a memory error in a buffer - got to
28711865Sdilpreet 				 * do a fair bit of tidying first
28721865Sdilpreet 				 */
28731865Sdilpreet 				if (bgep->progress & PROGRESS_KSTATS) {
28741865Sdilpreet 					bge_fini_kstats(bgep);
28751865Sdilpreet 					bgep->progress &= ~PROGRESS_KSTATS;
28761865Sdilpreet 				}
28771865Sdilpreet 				if (bgep->progress & PROGRESS_INTR) {
28781865Sdilpreet 					bge_intr_disable(bgep);
28791865Sdilpreet 					rw_enter(bgep->errlock, RW_WRITER);
28801865Sdilpreet 					bge_fini_rings(bgep);
28811865Sdilpreet 					rw_exit(bgep->errlock);
28821865Sdilpreet 					bgep->progress &= ~PROGRESS_INTR;
28831865Sdilpreet 				}
28841865Sdilpreet 				if (bgep->progress & PROGRESS_BUFS) {
28851865Sdilpreet 					bge_free_bufs(bgep);
28861865Sdilpreet 					bgep->progress &= ~PROGRESS_BUFS;
28871865Sdilpreet 				}
28881865Sdilpreet 				bgep->bge_dma_error = B_FALSE;
28891865Sdilpreet 			}
28901865Sdilpreet 		}
28911865Sdilpreet 	} else
28921369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
28931369Sdduvall }
28941369Sdduvall 
28951369Sdduvall /*
28961369Sdduvall  * Poll for completion of chip's ROM firmware; also, at least on the
28971369Sdduvall  * first time through, find and return the hardware MAC address, if any.
28981369Sdduvall  */
28991369Sdduvall static uint64_t bge_poll_firmware(bge_t *bgep);
29001369Sdduvall #pragma	no_inline(bge_poll_firmware)
29011369Sdduvall 
29021369Sdduvall static uint64_t
29031369Sdduvall bge_poll_firmware(bge_t *bgep)
29041369Sdduvall {
29051369Sdduvall 	uint64_t magic;
29061369Sdduvall 	uint64_t mac;
29071369Sdduvall 	uint32_t gen;
29081369Sdduvall 	uint32_t i;
29091369Sdduvall 
29101369Sdduvall 	/*
29111369Sdduvall 	 * Step 19: poll for firmware completion (GENCOMM port set
29121369Sdduvall 	 * to the ones complement of T3_MAGIC_NUMBER).
29131369Sdduvall 	 *
29141369Sdduvall 	 * While we're at it, we also read the MAC address register;
29152135Szh199473 	 * at some stage the firmware will load this with the
29161369Sdduvall 	 * factory-set value.
29171369Sdduvall 	 *
29181369Sdduvall 	 * When both the magic number and the MAC address are set,
29191369Sdduvall 	 * we're done; but we impose a time limit of one second
29201369Sdduvall 	 * (1000*1000us) in case the firmware fails in some fashion
29211369Sdduvall 	 * or the SEEPROM that provides that MAC address isn't fitted.
29221369Sdduvall 	 *
29231369Sdduvall 	 * After the first time through (chip state != INITIAL), we
29241369Sdduvall 	 * don't need the MAC address to be set (we've already got it
29251369Sdduvall 	 * or not, from the first time), so we don't wait for it, but
29261369Sdduvall 	 * we still have to wait for the T3_MAGIC_NUMBER.
29271369Sdduvall 	 *
29281369Sdduvall 	 * Note: the magic number is only a 32-bit quantity, but the NIC
29291369Sdduvall 	 * memory is 64-bit (and big-endian) internally.  Addressing the
29301369Sdduvall 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
29311369Sdduvall 	 * it work correctly on both big- and little-endian hosts.
29321369Sdduvall 	 */
29331369Sdduvall 	for (i = 0; i < 1000; ++i) {
29341369Sdduvall 		drv_usecwait(1000);
29351369Sdduvall 		gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
29361369Sdduvall 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
29371408Srandyf #ifdef BGE_IPMI_ASF
29381408Srandyf 		if (!bgep->asf_enabled) {
29391408Srandyf #endif
29401408Srandyf 			if (gen != ~T3_MAGIC_NUMBER)
29411408Srandyf 				continue;
29421408Srandyf #ifdef BGE_IPMI_ASF
29431408Srandyf 		}
29441408Srandyf #endif
29451369Sdduvall 		if (mac != 0ULL)
29461369Sdduvall 			break;
29471369Sdduvall 		if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
29481369Sdduvall 			break;
29491369Sdduvall 	}
29501369Sdduvall 
29511369Sdduvall 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
29521369Sdduvall 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
29531369Sdduvall 		(void *)bgep, gen, i));
29541369Sdduvall 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
29551369Sdduvall 		mac, magic));
29561369Sdduvall 
29571369Sdduvall 	return (mac);
29581369Sdduvall }
29591369Sdduvall 
29603390Szh199473 /*
29613390Szh199473  * Maximum times of trying to get the NVRAM access lock
29623390Szh199473  * by calling bge_nvmem_acquire()
29633390Szh199473  */
29643390Szh199473 #define	MAX_TRY_NVMEM_ACQUIRE	10000
29653390Szh199473 
29661408Srandyf #ifdef BGE_IPMI_ASF
29671865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
29681408Srandyf #else
29691865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
29701408Srandyf #endif
29711369Sdduvall #pragma	no_inline(bge_chip_reset)
29721369Sdduvall 
29731865Sdilpreet int
29741408Srandyf #ifdef BGE_IPMI_ASF
29751408Srandyf bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
29761408Srandyf #else
29771369Sdduvall bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
29781408Srandyf #endif
29791369Sdduvall {
29801369Sdduvall 	chip_id_t chipid;
29811369Sdduvall 	uint64_t mac;
29821908Sly149593 	uint64_t magic;
29831369Sdduvall 	uint32_t modeflags;
29841369Sdduvall 	uint32_t mhcr;
29851369Sdduvall 	uint32_t sx0;
29863390Szh199473 	uint32_t i, tries;
29871408Srandyf #ifdef BGE_IPMI_ASF
29881408Srandyf 	uint32_t mailbox;
29891408Srandyf #endif
29901865Sdilpreet 	int retval = DDI_SUCCESS;
29911369Sdduvall 
29921369Sdduvall 	BGE_TRACE(("bge_chip_reset($%p, %d)",
29931369Sdduvall 		(void *)bgep, enable_dma));
29941369Sdduvall 
29951369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
29961369Sdduvall 
29971369Sdduvall 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
29981369Sdduvall 		(void *)bgep, enable_dma, bgep->bge_chip_state));
29991369Sdduvall 
30001369Sdduvall 	/*
30011369Sdduvall 	 * Do we need to stop the chip cleanly before resetting?
30021369Sdduvall 	 */
30031369Sdduvall 	switch (bgep->bge_chip_state) {
30041369Sdduvall 	default:
30051369Sdduvall 		_NOTE(NOTREACHED)
30061865Sdilpreet 		return (DDI_FAILURE);
30071369Sdduvall 
30081369Sdduvall 	case BGE_CHIP_INITIAL:
30091369Sdduvall 	case BGE_CHIP_STOPPED:
30101369Sdduvall 	case BGE_CHIP_RESET:
30111369Sdduvall 		break;
30121369Sdduvall 
30131369Sdduvall 	case BGE_CHIP_RUNNING:
30141369Sdduvall 	case BGE_CHIP_ERROR:
30151369Sdduvall 	case BGE_CHIP_FAULT:
30161369Sdduvall 		bge_chip_stop(bgep, B_FALSE);
30171369Sdduvall 		break;
30181369Sdduvall 	}
30191369Sdduvall 
30201408Srandyf #ifdef BGE_IPMI_ASF
30211408Srandyf 	if (bgep->asf_enabled) {
30221408Srandyf 		if (asf_mode == ASF_MODE_INIT) {
30231408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
30241408Srandyf 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
30251408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
30261408Srandyf 		}
30271408Srandyf 	}
30281408Srandyf #endif
30291369Sdduvall 	/*
30301369Sdduvall 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
30311369Sdduvall 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
30321369Sdduvall 	 *
30331369Sdduvall 	 * Before reset Core clock,it is
30341369Sdduvall 	 * also required to initialize the Memory Arbiter as specified in step9
30351369Sdduvall 	 * and Misc Host Control Register as specified in step-13
30361369Sdduvall 	 * Step 4-5: reset Core clock & wait for completion
30371369Sdduvall 	 * Steps 6-8: are done by bge_chip_cfg_init()
30381908Sly149593 	 * put the T3_MAGIC_NUMBER into the GENCOMM port before reset
30391369Sdduvall 	 */
30401865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
30411865Sdilpreet 		retval = DDI_FAILURE;
30421369Sdduvall 
30431369Sdduvall 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
30441369Sdduvall 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
30451369Sdduvall 	    MHCR_MASK_INTERRUPT_MODE |
30461369Sdduvall 	    MHCR_MASK_PCI_INT_OUTPUT |
30471369Sdduvall 	    MHCR_CLEAR_INTERRUPT_INTA;
30481369Sdduvall #ifdef  _BIG_ENDIAN
30491369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
30501369Sdduvall #endif  /* _BIG_ENDIAN */
30511369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
30521408Srandyf #ifdef BGE_IPMI_ASF
30531408Srandyf 	if (bgep->asf_enabled)
30541408Srandyf 		bgep->asf_wordswapped = B_FALSE;
30551408Srandyf #endif
30562675Szh199473 	/*
30572675Szh199473 	 * NVRAM Corruption Workaround
30582675Szh199473 	 */
30593390Szh199473 	for (tries = 0; tries < MAX_TRY_NVMEM_ACQUIRE; tries++)
3060*3534Szh199473 		if (bge_nvmem_acquire(bgep) != EAGAIN)
30612675Szh199473 			break;
30623440Szh199473 	if (tries >= MAX_TRY_NVMEM_ACQUIRE)
30632675Szh199473 		BGE_DEBUG(("%s: fail to acquire nvram lock",
30642675Szh199473 			bgep->ifname));
30652675Szh199473 
30661908Sly149593 #ifdef BGE_IPMI_ASF
30671908Sly149593 	if (!bgep->asf_enabled) {
30681908Sly149593 #endif
30691908Sly149593 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
30701908Sly149593 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
30711908Sly149593 #ifdef BGE_IPMI_ASF
30721908Sly149593 	}
30731908Sly149593 #endif
30741908Sly149593 
30751865Sdilpreet 	if (!bge_chip_reset_engine(bgep, MISC_CONFIG_REG))
30761865Sdilpreet 		retval = DDI_FAILURE;
30771369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
30781369Sdduvall 
30791369Sdduvall 	/*
30801369Sdduvall 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
30811369Sdduvall 	 * a bit set to avoid a fifo overflow/underflow bug.
30821369Sdduvall 	 */
30832135Szh199473 	if ((bgep->chipid.chip_label == 5721) ||
30842135Szh199473 		(bgep->chipid.chip_label == 5751) ||
30852675Szh199473 		(bgep->chipid.chip_label == 5752) ||
30862135Szh199473 		(bgep->chipid.chip_label == 5789))
30871369Sdduvall 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
30881369Sdduvall 
30891369Sdduvall 
30901369Sdduvall 	/*
30911369Sdduvall 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
30921369Sdduvall 	 * not be changed.
30931369Sdduvall 	 */
30941865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
30951865Sdilpreet 		retval = DDI_FAILURE;
30961369Sdduvall 
30971369Sdduvall 	/*
30981369Sdduvall 	 * Steps 10-11: configure PIO endianness options and
30991369Sdduvall 	 * enable indirect register access -- already done
31001369Sdduvall 	 * Steps 12-13: enable writing to the PCI state & clock
31011369Sdduvall 	 * control registers -- not required; we aren't going to
31021369Sdduvall 	 * use those features.
31031369Sdduvall 	 * Steps 14-15: Configure DMA endianness options.  See
31041369Sdduvall 	 * the comments on the setting of the MHCR above.
31051369Sdduvall 	 */
31061369Sdduvall #ifdef	_BIG_ENDIAN
31071369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
31081369Sdduvall 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
31091369Sdduvall #else
31101369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
31111369Sdduvall #endif	/* _BIG_ENDIAN */
31121408Srandyf #ifdef BGE_IPMI_ASF
31131408Srandyf 	if (bgep->asf_enabled)
31141408Srandyf 		modeflags |= MODE_HOST_STACK_UP;
31151408Srandyf #endif
31161369Sdduvall 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
31171369Sdduvall 
31181408Srandyf #ifdef BGE_IPMI_ASF
31191408Srandyf 	if (bgep->asf_enabled) {
31201408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
31211408Srandyf 			/* Wait for NVRAM init */
31221408Srandyf 			i = 0;
31231408Srandyf 			drv_usecwait(5000);
31241408Srandyf 			mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
31251408Srandyf 			while ((mailbox != (uint32_t)
31261408Srandyf 				~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
31271408Srandyf 				(i < 10000)) {
31281408Srandyf 				drv_usecwait(100);
31291408Srandyf 				mailbox = bge_nic_get32(bgep,
31301408Srandyf 					BGE_FIRMWARE_MAILBOX);
31311408Srandyf 				i++;
31321408Srandyf 			}
31331408Srandyf 			if (!bgep->asf_newhandshake) {
31341408Srandyf 				if ((asf_mode == ASF_MODE_INIT) ||
31351408Srandyf 					(asf_mode == ASF_MODE_POST_INIT)) {
31361408Srandyf 
31371408Srandyf 					bge_asf_post_reset_old_mode(bgep,
31381408Srandyf 						BGE_INIT_RESET);
31391408Srandyf 				} else {
31401408Srandyf 					bge_asf_post_reset_old_mode(bgep,
31411408Srandyf 						BGE_SHUTDOWN_RESET);
31421408Srandyf 				}
31431408Srandyf 			}
31441408Srandyf 		}
31451408Srandyf 	}
31461408Srandyf #endif
31471369Sdduvall 	/*
31481369Sdduvall 	 * Steps 16-17: poll for firmware completion
31491369Sdduvall 	 */
31501369Sdduvall 	mac = bge_poll_firmware(bgep);
31511369Sdduvall 
31521369Sdduvall 	/*
31531369Sdduvall 	 * Step 18: enable external memory -- doesn't apply.
31541369Sdduvall 	 *
31551369Sdduvall 	 * However we take the opportunity to set the MLCR anyway, as
31561369Sdduvall 	 * this register also controls the SEEPROM auto-access method
31571369Sdduvall 	 * which we may want to use later ...
31581369Sdduvall 	 *
31591369Sdduvall 	 * The proper value here depends on the way the chip is wired
31601369Sdduvall 	 * into the circuit board, as this register *also* controls which
31611369Sdduvall 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
31621369Sdduvall 	 * values driven onto those pins!
31631369Sdduvall 	 *
31641369Sdduvall 	 * See also step 74 in the PRM ...
31651369Sdduvall 	 */
31661369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
31671369Sdduvall 	    bgep->chipid.bge_mlcr_default);
31681369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
31691369Sdduvall 
31701369Sdduvall 	/*
31711369Sdduvall 	 * Step 20: clear the Ethernet MAC mode register
31721369Sdduvall 	 */
31731369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
31741369Sdduvall 
31751369Sdduvall 	/*
31761369Sdduvall 	 * Step 21: restore cache-line-size, latency timer, and
31771369Sdduvall 	 * subsystem ID registers to their original values (not
31781369Sdduvall 	 * those read into the local structure <chipid>, 'cos
31791369Sdduvall 	 * that was after they were cleared by the RESET).
31801369Sdduvall 	 *
31811369Sdduvall 	 * Note: the Subsystem Vendor/Device ID registers are not
31821369Sdduvall 	 * directly writable in config space, so we use the shadow
31831369Sdduvall 	 * copy in "Page Zero" of register space to restore them
31841369Sdduvall 	 * both in one go ...
31851369Sdduvall 	 */
31861369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
31871369Sdduvall 		bgep->chipid.clsize);
31881369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
31891369Sdduvall 		bgep->chipid.latency);
31901369Sdduvall 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
31911369Sdduvall 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
31921369Sdduvall 
31931369Sdduvall 	/*
31941369Sdduvall 	 * The SEND INDEX registers should be reset to zero by the
31951369Sdduvall 	 * global chip reset; if they're not, there'll be trouble
31961865Sdilpreet 	 * later on.
31971369Sdduvall 	 */
31981369Sdduvall 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
31991865Sdilpreet 	if (sx0 != 0) {
32001865Sdilpreet 		BGE_REPORT((bgep, "SEND INDEX - device didn't RESET"));
32011865Sdilpreet 		bge_fm_ereport(bgep, DDI_FM_DEVICE_INVAL_STATE);
32023170Sml149210 		retval = DDI_FAILURE;
32031865Sdilpreet 	}
32041369Sdduvall 
32051369Sdduvall 	/* Enable MSI code */
32061369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
32071369Sdduvall 		bge_reg_set32(bgep, MSI_MODE_REG,
32081369Sdduvall 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE);
32091369Sdduvall 
32101369Sdduvall 	/*
32111369Sdduvall 	 * On the first time through, save the factory-set MAC address
32121369Sdduvall 	 * (if any).  If bge_poll_firmware() above didn't return one
32131369Sdduvall 	 * (from a chip register) consider looking in the attached NV
32141369Sdduvall 	 * memory device, if any.  Once we have it, we save it in both
32151369Sdduvall 	 * register-image (64-bit) and byte-array forms.  All-zero and
32161369Sdduvall 	 * all-one addresses are not valid, and we refuse to stash those.
32171369Sdduvall 	 */
32181369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
32191369Sdduvall 		if (mac == 0ULL)
32201369Sdduvall 			mac = bge_get_nvmac(bgep);
32211369Sdduvall 		if (mac != 0ULL && mac != ~0ULL) {
32221369Sdduvall 			bgep->chipid.hw_mac_addr = mac;
32231369Sdduvall 			for (i = ETHERADDRL; i-- != 0; ) {
32241369Sdduvall 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
32251369Sdduvall 				mac >>= 8;
32261369Sdduvall 			}
32272331Skrgopi 			bgep->chipid.vendor_addr.set = B_TRUE;
32281369Sdduvall 		}
32291369Sdduvall 	}
32301369Sdduvall 
32311408Srandyf #ifdef BGE_IPMI_ASF
32321408Srandyf 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
32331408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
32341408Srandyf 			if ((asf_mode == ASF_MODE_INIT) ||
32351408Srandyf 				(asf_mode == ASF_MODE_POST_INIT)) {
32361408Srandyf 
32371408Srandyf 				bge_asf_post_reset_new_mode(bgep,
32381408Srandyf 					BGE_INIT_RESET);
32391408Srandyf 			} else {
32401408Srandyf 				bge_asf_post_reset_new_mode(bgep,
32411408Srandyf 					BGE_SHUTDOWN_RESET);
32421408Srandyf 			}
32431408Srandyf 		}
32441408Srandyf 	}
32451408Srandyf #endif
32461408Srandyf 
32471369Sdduvall 	/*
32481369Sdduvall 	 * Record the new state
32491369Sdduvall 	 */
32501369Sdduvall 	bgep->chip_resets += 1;
32511369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RESET;
32521865Sdilpreet 	return (retval);
32531369Sdduvall }
32541369Sdduvall 
32551369Sdduvall /*
32561369Sdduvall  * bge_chip_start() -- start the chip transmitting and/or receiving,
32571369Sdduvall  * including enabling interrupts
32581369Sdduvall  */
32591865Sdilpreet int bge_chip_start(bge_t *bgep, boolean_t reset_phys);
32601369Sdduvall #pragma	no_inline(bge_chip_start)
32611369Sdduvall 
32621865Sdilpreet int
32631369Sdduvall bge_chip_start(bge_t *bgep, boolean_t reset_phys)
32641369Sdduvall {
32651369Sdduvall 	uint32_t coalmode;
32661369Sdduvall 	uint32_t ledctl;
32671369Sdduvall 	uint32_t mtu;
32681369Sdduvall 	uint32_t maxring;
3269*3534Szh199473 	uint32_t stats_mask;
32701369Sdduvall 	uint64_t ring;
32711865Sdilpreet 	int retval = DDI_SUCCESS;
32721369Sdduvall 
32731369Sdduvall 	BGE_TRACE(("bge_chip_start($%p)",
32741369Sdduvall 		(void *)bgep));
32751369Sdduvall 
32761369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
32771369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
32781369Sdduvall 
32791369Sdduvall 	/*
32801369Sdduvall 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
32811369Sdduvall 	 * The document specifies 95 separate steps to fully
32821369Sdduvall 	 * initialise the chip!!!!
32831369Sdduvall 	 *
32841369Sdduvall 	 * The reset code above has already got us as far as step
32851369Sdduvall 	 * 21, so we continue with ...
32861369Sdduvall 	 *
32871369Sdduvall 	 * Step 22: clear the MAC statistics block
32881369Sdduvall 	 * (0x0300-0x0aff in NIC-local memory)
32891369Sdduvall 	 */
32901369Sdduvall 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
32911369Sdduvall 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
32921369Sdduvall 		    NIC_MEM_STATISTICS_SIZE);
32931369Sdduvall 
32941369Sdduvall 	/*
32951369Sdduvall 	 * Step 23: clear the status block (in host memory)
32961369Sdduvall 	 */
32971369Sdduvall 	DMA_ZERO(bgep->status_block);
32981369Sdduvall 
32991369Sdduvall 	/*
33001369Sdduvall 	 * Step 24: set DMA read/write control register
33011369Sdduvall 	 */
33021369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
33031369Sdduvall 		bgep->chipid.bge_dma_rwctrl);
33041369Sdduvall 
33051369Sdduvall 	/*
33061369Sdduvall 	 * Step 25: Configure DMA endianness -- already done (16/17)
33071369Sdduvall 	 * Step 26: Configure Host-Based Send Rings
33081369Sdduvall 	 * Step 27: Indicate Host Stack Up
33091369Sdduvall 	 */
33101369Sdduvall 	bge_reg_set32(bgep, MODE_CONTROL_REG,
33111369Sdduvall 		MODE_HOST_SEND_BDS |
33121369Sdduvall 		MODE_HOST_STACK_UP);
33131369Sdduvall 
33141369Sdduvall 	/*
33151369Sdduvall 	 * Step 28: Configure checksum options:
33161611Szh199473 	 *	Solaris supports the hardware default checksum options.
33171611Szh199473 	 *
33181611Szh199473 	 *	Workaround for Incorrect pseudo-header checksum calculation.
33191369Sdduvall 	 */
33202135Szh199473 	if (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)
33211611Szh199473 		bge_reg_set32(bgep, MODE_CONTROL_REG,
33222311Sseb 		    MODE_SEND_NO_PSEUDO_HDR_CSUM);
33231369Sdduvall 
33241369Sdduvall 	/*
33251369Sdduvall 	 * Step 29: configure Timer Prescaler.  The value is always the
33261369Sdduvall 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
33271369Sdduvall 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
33281369Sdduvall 	 * for the whole chip!
33291369Sdduvall 	 */
33301369Sdduvall 	bge_reg_put32(bgep, MISC_CONFIG_REG, MISC_CONFIG_DEFAULT);
33311369Sdduvall 
33321369Sdduvall 	/*
33331369Sdduvall 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
33341369Sdduvall 	 *
33351369Sdduvall 	 * If the mbuf_length is specified as 0, we just leave these at
33361369Sdduvall 	 * their hardware defaults, rather than explicitly setting them.
33371369Sdduvall 	 * As the Broadcom HRM,driver better not change the parameters
33381369Sdduvall 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
33391369Sdduvall 	 */
33401369Sdduvall 	if ((bgep->chipid.mbuf_length != 0) &&
33411369Sdduvall 		(DEVICE_5704_SERIES_CHIPSETS(bgep))) {
33421369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
33431369Sdduvall 				bgep->chipid.mbuf_base);
33441369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
33451369Sdduvall 				bgep->chipid.mbuf_length);
33461369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
33471369Sdduvall 				DMAD_POOL_BASE_DEFAULT);
33481369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
33491369Sdduvall 				DMAD_POOL_LENGTH_DEFAULT);
33501369Sdduvall 	}
33511369Sdduvall 
33521369Sdduvall 	/*
33531369Sdduvall 	 * Step 32: configure MAC memory pool watermarks
33541369Sdduvall 	 */
33551369Sdduvall 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
33561369Sdduvall 		bgep->chipid.mbuf_lo_water_rdma);
33571369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
33581369Sdduvall 		bgep->chipid.mbuf_lo_water_rmac);
33591369Sdduvall 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
33601369Sdduvall 		bgep->chipid.mbuf_hi_water);
33611369Sdduvall 
33621369Sdduvall 	/*
33631369Sdduvall 	 * Step 33: configure DMA resource watermarks
33641369Sdduvall 	 */
33651369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33661369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
33671369Sdduvall 		    bge_dmad_lo_water);
33681369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
33691369Sdduvall 		    bge_dmad_hi_water);
33701369Sdduvall 	}
33711369Sdduvall 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
33721369Sdduvall 
33731369Sdduvall 	/*
33741369Sdduvall 	 * Steps 34-36: enable buffer manager & internal h/w queues
33751369Sdduvall 	 */
33761865Sdilpreet 	if (!bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
33771865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
33781865Sdilpreet 		retval = DDI_FAILURE;
33791865Sdilpreet 	if (!bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0))
33801865Sdilpreet 		retval = DDI_FAILURE;
33811369Sdduvall 
33821369Sdduvall 	/*
33831369Sdduvall 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
33841369Sdduvall 	 */
33851369Sdduvall 	bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
33861369Sdduvall 		&bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
33871369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33881369Sdduvall 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
33891369Sdduvall 			&bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
33901369Sdduvall 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
33911369Sdduvall 			&bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
33921369Sdduvall 	}
33931369Sdduvall 
33941369Sdduvall 	/*
33951369Sdduvall 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
33961369Sdduvall 	 */
33971369Sdduvall 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
33981369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33991369Sdduvall 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
34001369Sdduvall 		    bge_replenish_jumbo);
34011369Sdduvall 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
34021369Sdduvall 		    bge_replenish_mini);
34031369Sdduvall 	}
34041369Sdduvall 
34051369Sdduvall 	/*
34061369Sdduvall 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
34071369Sdduvall 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
34081369Sdduvall 	 */
34091369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34101369Sdduvall 		maxring = BGE_SEND_RINGS_MAX;
34111369Sdduvall 	else
34121369Sdduvall 		maxring = BGE_SEND_RINGS_MAX_5705;
34131369Sdduvall 	for (ring = 0; ring < maxring; ++ring) {
34141369Sdduvall 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
34151369Sdduvall 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
34161369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
34171369Sdduvall 			&bgep->send[ring].hw_rcb);
34181369Sdduvall 	}
34191369Sdduvall 
34201369Sdduvall 	/*
34211369Sdduvall 	 * Steps 44-45: initialise Receive Return Rings
34221369Sdduvall 	 * (0x0200-0x02ff in NIC-local memory)
34231369Sdduvall 	 */
34241369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34251369Sdduvall 		maxring = BGE_RECV_RINGS_MAX;
34261369Sdduvall 	else
34271369Sdduvall 		maxring = BGE_RECV_RINGS_MAX_5705;
34281369Sdduvall 	for (ring = 0; ring < maxring; ++ring)
34291369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
34301369Sdduvall 			&bgep->recv[ring].hw_rcb);
34311369Sdduvall 
34321369Sdduvall 	/*
34331369Sdduvall 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
34341369Sdduvall 	 */
34351369Sdduvall 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
34361369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
34371369Sdduvall 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
34381369Sdduvall 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
34391369Sdduvall 	}
34401369Sdduvall 	/*
34411369Sdduvall 	 * Step 47: configure the MAC unicast address
34421369Sdduvall 	 * Step 48: configure the random backoff seed
34431369Sdduvall 	 * Step 96: set up multicast filters
34441369Sdduvall 	 */
34451408Srandyf #ifdef BGE_IPMI_ASF
34461865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE)
34471408Srandyf #else
34481865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE)
34491408Srandyf #endif
34501865Sdilpreet 		retval = DDI_FAILURE;
34511369Sdduvall 
34521369Sdduvall 	/*
34531369Sdduvall 	 * Step 49: configure the MTU
34541369Sdduvall 	 */
34551369Sdduvall 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
34561369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
34571369Sdduvall 
34581369Sdduvall 	/*
34591369Sdduvall 	 * Step 50: configure the IPG et al
34601369Sdduvall 	 */
34611369Sdduvall 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
34621369Sdduvall 
34631369Sdduvall 	/*
34641369Sdduvall 	 * Step 51: configure the default Rx Return Ring
34651369Sdduvall 	 */
34661369Sdduvall 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
34671369Sdduvall 
34681369Sdduvall 	/*
34691369Sdduvall 	 * Steps 52-54: configure Receive List Placement,
34701369Sdduvall 	 * and enable Receive List Placement Statistics
34711369Sdduvall 	 */
34721369Sdduvall 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
34731369Sdduvall 		RCV_LP_CONFIG(bgep->chipid.rx_rings));
3474*3534Szh199473 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
3475*3534Szh199473 	case MHCR_CHIP_ASIC_REV_5700:
3476*3534Szh199473 	case MHCR_CHIP_ASIC_REV_5701:
3477*3534Szh199473 	case MHCR_CHIP_ASIC_REV_5703:
3478*3534Szh199473 	case MHCR_CHIP_ASIC_REV_5704:
3479*3534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
3480*3534Szh199473 		break;
3481*3534Szh199473 	case MHCR_CHIP_ASIC_REV_5705:
3482*3534Szh199473 		break;
3483*3534Szh199473 	default:
3484*3534Szh199473 		stats_mask = bge_reg_get32(bgep, RCV_LP_STATS_ENABLE_MASK_REG);
3485*3534Szh199473 		stats_mask &= ~RCV_LP_STATS_DISABLE_MACTQ;
3486*3534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, stats_mask);
3487*3534Szh199473 		break;
3488*3534Szh199473 	}
34891369Sdduvall 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
34901369Sdduvall 
34911369Sdduvall 	if (bgep->chipid.rx_rings > 1)
34921369Sdduvall 		bge_init_recv_rule(bgep);
34931369Sdduvall 
34941369Sdduvall 	/*
34951369Sdduvall 	 * Steps 55-56: enable Send Data Initiator Statistics
34961369Sdduvall 	 */
34971369Sdduvall 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
34981369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
34991369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
35001369Sdduvall 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
35011369Sdduvall 	} else {
35021369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
35031369Sdduvall 		    SEND_INIT_STATS_ENABLE);
35041369Sdduvall 	}
35051369Sdduvall 	/*
35061369Sdduvall 	 * Steps 57-58: stop (?) the Host Coalescing Engine
35071369Sdduvall 	 */
35081865Sdilpreet 	if (!bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0))
35091865Sdilpreet 		retval = DDI_FAILURE;
35101369Sdduvall 
35111369Sdduvall 	/*
35121369Sdduvall 	 * Steps 59-62: initialise Host Coalescing parameters
35131369Sdduvall 	 */
35141369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG, bge_tx_count_norm);
35151369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG, bge_tx_ticks_norm);
35161369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, bge_rx_count_norm);
35171369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, bge_rx_ticks_norm);
35181369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35191369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
35201369Sdduvall 		    bge_tx_count_intr);
35211369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
35221369Sdduvall 		    bge_tx_ticks_intr);
35231369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
35241369Sdduvall 		    bge_rx_count_intr);
35251369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
35261369Sdduvall 		    bge_rx_ticks_intr);
35271369Sdduvall 	}
35281369Sdduvall 
35291369Sdduvall 	/*
35301369Sdduvall 	 * Steps 63-64: initialise status block & statistics
35311369Sdduvall 	 * host memory addresses
35321369Sdduvall 	 * The statistic block does not exist in some chipsets
35331369Sdduvall 	 * Step 65: initialise Statistics Coalescing Tick Counter
35341369Sdduvall 	 */
35351369Sdduvall 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
35361369Sdduvall 		bgep->status_block.cookie.dmac_laddress);
35371369Sdduvall 
35381369Sdduvall 	/*
35391369Sdduvall 	 * Steps 66-67: initialise status block & statistics
35401369Sdduvall 	 * NIC-local memory addresses
35411369Sdduvall 	 */
35421369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35431369Sdduvall 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
35441369Sdduvall 		    bgep->statistics.cookie.dmac_laddress);
35451369Sdduvall 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
35461369Sdduvall 		    STATISTICS_TICKS_DEFAULT);
35471369Sdduvall 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
35481369Sdduvall 		    NIC_MEM_STATUS_BLOCK);
35491369Sdduvall 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
35501369Sdduvall 		    NIC_MEM_STATISTICS);
35511369Sdduvall 	}
35521369Sdduvall 
35531369Sdduvall 	/*
35541369Sdduvall 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
35551369Sdduvall 	 * Completion Engine, the Receive List Placement Engine, and the
35561369Sdduvall 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
35571369Sdduvall 	 * and BCM5715.
35581369Sdduvall 	 */
35591369Sdduvall 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
35601369Sdduvall 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
35611369Sdduvall 		coalmode = COALESCE_64_BYTE_STATUS;
35621369Sdduvall 	else
35631369Sdduvall 		coalmode = 0;
35641865Sdilpreet 	if (!bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode))
35651865Sdilpreet 		retval = DDI_FAILURE;
35661865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
35671865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
35681865Sdilpreet 		retval = DDI_FAILURE;
35691865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0))
35701865Sdilpreet 		retval = DDI_FAILURE;
35711369Sdduvall 
35721369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
35731865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
35741865Sdilpreet 		    STATE_MACHINE_ATTN_ENABLE_BIT))
35751865Sdilpreet 			retval = DDI_FAILURE;
35761369Sdduvall 
35771369Sdduvall 	/*
35781369Sdduvall 	 * Step 72: Enable MAC DMA engines
35791369Sdduvall 	 * Step 73: Clear & enable MAC statistics
35801369Sdduvall 	 */
35811369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
35821369Sdduvall 		ETHERNET_MODE_ENABLE_FHDE |
35831369Sdduvall 		ETHERNET_MODE_ENABLE_RDE |
35841369Sdduvall 		ETHERNET_MODE_ENABLE_TDE);
35851369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
35861369Sdduvall 		ETHERNET_MODE_ENABLE_TX_STATS |
35871369Sdduvall 		ETHERNET_MODE_ENABLE_RX_STATS |
35881369Sdduvall 		ETHERNET_MODE_CLEAR_TX_STATS |
35891369Sdduvall 		ETHERNET_MODE_CLEAR_RX_STATS);
35901369Sdduvall 
35911369Sdduvall 	/*
35921369Sdduvall 	 * Step 74: configure the MLCR (Miscellaneous Local Control
35931369Sdduvall 	 * Register); not required, as we set up the MLCR in step 10
35941369Sdduvall 	 * (part of the reset code) above.
35951369Sdduvall 	 *
35961369Sdduvall 	 * Step 75: clear Interrupt Mailbox 0
35971369Sdduvall 	 */
35981369Sdduvall 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
35991369Sdduvall 
36001369Sdduvall 	/*
36011369Sdduvall 	 * Steps 76-87: Gentlemen, start your engines ...
36021369Sdduvall 	 *
36031369Sdduvall 	 * Enable the DMA Completion Engine, the Write DMA Engine,
36041369Sdduvall 	 * the Read DMA Engine, Receive Data Completion Engine,
36051369Sdduvall 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
36061369Sdduvall 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
36071369Sdduvall 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
36081369Sdduvall 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
36091369Sdduvall 	 *
36101369Sdduvall 	 * Beware exhaust fumes?
36111369Sdduvall 	 */
36121369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
36131865Sdilpreet 		if (!bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0))
36141865Sdilpreet 			retval = DDI_FAILURE;
36151865Sdilpreet 	if (!bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
36161865Sdilpreet 	    (bge_dma_wrprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
36171865Sdilpreet 		retval = DDI_FAILURE;
36181865Sdilpreet 	if (!bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
36191865Sdilpreet 	    (bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
36201865Sdilpreet 		retval = DDI_FAILURE;
36211865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
36221865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36231865Sdilpreet 		retval = DDI_FAILURE;
36241369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
36251865Sdilpreet 		if (!bge_chip_enable_engine(bgep,
36261865Sdilpreet 		    MBUF_CLUSTER_FREE_MODE_REG, 0))
36271865Sdilpreet 			retval = DDI_FAILURE;
36281865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0))
36291865Sdilpreet 		retval = DDI_FAILURE;
36301865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
36311865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36321865Sdilpreet 		retval = DDI_FAILURE;
36331865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
36341865Sdilpreet 	    RCV_BD_DISABLED_RING_ATTN))
36351865Sdilpreet 		retval = DDI_FAILURE;
36361865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
36371865Sdilpreet 	    RCV_DATA_BD_ILL_RING_ATTN))
36381865Sdilpreet 		retval = DDI_FAILURE;
36391865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0))
36401865Sdilpreet 		retval = DDI_FAILURE;
36411865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
36421865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36431865Sdilpreet 		retval = DDI_FAILURE;
36441865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
36451865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
36461865Sdilpreet 		retval = DDI_FAILURE;
36471369Sdduvall 
36481369Sdduvall 	/*
36491369Sdduvall 	 * Step 88: download firmware -- doesn't apply
36501369Sdduvall 	 * Steps 89-90: enable Transmit & Receive MAC Engines
36511369Sdduvall 	 */
36521865Sdilpreet 	if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
36531865Sdilpreet 		retval = DDI_FAILURE;
36541408Srandyf #ifdef BGE_IPMI_ASF
36551865Sdilpreet 	if (!bgep->asf_enabled) {
36561865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
36571865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
36581865Sdilpreet 			retval = DDI_FAILURE;
36591408Srandyf 	} else {
36601865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0))
36611865Sdilpreet 			retval = DDI_FAILURE;
36621408Srandyf 	}
36631408Srandyf #else
36641865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
36651865Sdilpreet 	    RECEIVE_MODE_KEEP_VLAN_TAG))
36661865Sdilpreet 		retval = DDI_FAILURE;
36671408Srandyf #endif
36681369Sdduvall 
36691369Sdduvall 	/*
36701369Sdduvall 	 * Step 91: disable auto-polling of PHY status
36711369Sdduvall 	 */
36721369Sdduvall 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
36731369Sdduvall 
36741369Sdduvall 	/*
36751369Sdduvall 	 * Step 92: configure D0 power state (not required)
36761369Sdduvall 	 * Step 93: initialise LED control register ()
36771369Sdduvall 	 */
36781369Sdduvall 	ledctl = LED_CONTROL_DEFAULT;
36791369Sdduvall 	switch (bgep->chipid.device) {
36801369Sdduvall 	case DEVICE_ID_5700:
36811369Sdduvall 	case DEVICE_ID_5700x:
36821369Sdduvall 	case DEVICE_ID_5701:
36831369Sdduvall 		/*
36841369Sdduvall 		 * Switch to 5700 (MAC) mode on these older chips
36851369Sdduvall 		 */
36861369Sdduvall 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
36871369Sdduvall 		ledctl |= LED_CONTROL_LED_MODE_5700;
36881369Sdduvall 		break;
36891369Sdduvall 
36901369Sdduvall 	default:
36911369Sdduvall 		break;
36921369Sdduvall 	}
36931369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
36941369Sdduvall 
36951369Sdduvall 	/*
36961369Sdduvall 	 * Step 94: activate link
36971369Sdduvall 	 */
36981369Sdduvall 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
36991369Sdduvall 
37001369Sdduvall 	/*
37011369Sdduvall 	 * Step 95: set up physical layer (PHY/SerDes)
37021369Sdduvall 	 * restart autoneg (if required)
37031369Sdduvall 	 */
37041369Sdduvall 	if (reset_phys)
37051865Sdilpreet 		if (bge_phys_update(bgep) == DDI_FAILURE)
37061865Sdilpreet 			retval = DDI_FAILURE;
37071369Sdduvall 
37081369Sdduvall 	/*
37091369Sdduvall 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
37101369Sdduvall 	 */
37111369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
37121369Sdduvall 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
37131369Sdduvall 			bgep->buff[ring].rf_next);
37141369Sdduvall 
37151369Sdduvall 	/*
37161369Sdduvall 	 * MSI bits:The least significant MSI 16-bit word.
37171369Sdduvall 	 * ISR will be triggered different.
37181369Sdduvall 	 */
37191369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
37201369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
37211369Sdduvall 
37221369Sdduvall 	/*
37231369Sdduvall 	 * Extra step (DSG): select which interrupts are enabled
37241369Sdduvall 	 *
37251369Sdduvall 	 * Program the Ethernet MAC engine to signal attention on
37261369Sdduvall 	 * Link Change events, then enable interrupts on MAC, DMA,
37271369Sdduvall 	 * and FLOW attention signals.
37281369Sdduvall 	 */
37291369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
37301369Sdduvall 		ETHERNET_EVENT_LINK_INT |
37311369Sdduvall 		ETHERNET_STATUS_PCS_ERROR_INT);
37321408Srandyf #ifdef BGE_IPMI_ASF
37331408Srandyf 	if (bgep->asf_enabled) {
37341408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
37351408Srandyf 			MODE_INT_ON_FLOW_ATTN |
37361408Srandyf 			MODE_INT_ON_DMA_ATTN |
37371408Srandyf 			MODE_HOST_STACK_UP|
37381408Srandyf 			MODE_INT_ON_MAC_ATTN);
37391408Srandyf 	} else {
37401408Srandyf #endif
37411408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
37421408Srandyf 			MODE_INT_ON_FLOW_ATTN |
37431408Srandyf 			MODE_INT_ON_DMA_ATTN |
37441408Srandyf 			MODE_INT_ON_MAC_ATTN);
37451408Srandyf #ifdef BGE_IPMI_ASF
37461408Srandyf 	}
37471408Srandyf #endif
37481369Sdduvall 
37491369Sdduvall 	/*
37501369Sdduvall 	 * Step 97: enable PCI interrupts!!!
37511369Sdduvall 	 */
37521369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
37531369Sdduvall 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
37541369Sdduvall 		    MHCR_MASK_PCI_INT_OUTPUT);
37551369Sdduvall 
37561369Sdduvall 	/*
37571369Sdduvall 	 * All done!
37581369Sdduvall 	 */
37591369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
37601865Sdilpreet 	return (retval);
37611369Sdduvall }
37621369Sdduvall 
37631369Sdduvall 
37641369Sdduvall /*
37651369Sdduvall  * ========== Hardware interrupt handler ==========
37661369Sdduvall  */
37671369Sdduvall 
37681369Sdduvall #undef	BGE_DBG
37691369Sdduvall #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
37701369Sdduvall 
37711369Sdduvall /*
37721369Sdduvall  * Sync the status block, then atomically clear the specified bits in
37731369Sdduvall  * the <flags-and-tag> field of the status block.
37741369Sdduvall  * the <flags> word of the status block, returning the value of the
37751369Sdduvall  * <tag> and the <flags> before the bits were cleared.
37761369Sdduvall  */
37771865Sdilpreet static int bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags);
37781369Sdduvall #pragma	inline(bge_status_sync)
37791369Sdduvall 
37801865Sdilpreet static int
37811865Sdilpreet bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags)
37821369Sdduvall {
37831369Sdduvall 	bge_status_t *bsp;
37841865Sdilpreet 	int retval;
37851369Sdduvall 
37861369Sdduvall 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
37871369Sdduvall 		(void *)bgep, bits));
37881369Sdduvall 
37891369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
37901369Sdduvall 
37911369Sdduvall 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
37921865Sdilpreet 	retval = bge_check_dma_handle(bgep, bgep->status_block.dma_hdl);
37931865Sdilpreet 	if (retval != DDI_FM_OK)
37941865Sdilpreet 		return (retval);
37951865Sdilpreet 
37961369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
37971865Sdilpreet 	*flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
37981369Sdduvall 
37991369Sdduvall 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
38001865Sdilpreet 		(void *)bgep, bits, *flags));
38011865Sdilpreet 
38021865Sdilpreet 	return (retval);
38031369Sdduvall }
38041369Sdduvall 
38051369Sdduvall static void bge_wake_factotum(bge_t *bgep);
38061369Sdduvall #pragma	inline(bge_wake_factotum)
38071369Sdduvall 
38081369Sdduvall static void
38091369Sdduvall bge_wake_factotum(bge_t *bgep)
38101369Sdduvall {
38111369Sdduvall 	mutex_enter(bgep->softintrlock);
38121369Sdduvall 	if (bgep->factotum_flag == 0) {
38131369Sdduvall 		bgep->factotum_flag = 1;
38141369Sdduvall 		ddi_trigger_softintr(bgep->factotum_id);
38151369Sdduvall 	}
38161369Sdduvall 	mutex_exit(bgep->softintrlock);
38171369Sdduvall }
38181369Sdduvall 
38191369Sdduvall /*
38201369Sdduvall  *	bge_intr() -- handle chip interrupts
38211369Sdduvall  */
38221369Sdduvall uint_t bge_intr(caddr_t arg1, caddr_t arg2);
38231369Sdduvall #pragma	no_inline(bge_intr)
38241369Sdduvall 
38251369Sdduvall uint_t
38261369Sdduvall bge_intr(caddr_t arg1, caddr_t arg2)
38271369Sdduvall {
38281369Sdduvall 	bge_t *bgep = (bge_t *)arg1;		/* private device info	*/
38291369Sdduvall 	bge_status_t *bsp;
38301369Sdduvall 	uint64_t flags;
38311369Sdduvall 	uint32_t mlcr = 0;
38321369Sdduvall 	uint_t result;
38331865Sdilpreet 	int retval;
38341369Sdduvall 
38351369Sdduvall 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
38361369Sdduvall 
38371369Sdduvall 	/*
38381369Sdduvall 	 * GLD v2 checks that s/w setup is complete before passing
38391369Sdduvall 	 * interrupts to this routine, thus eliminating the old
38401369Sdduvall 	 * (and well-known) race condition around ddi_add_intr()
38411369Sdduvall 	 */
38421369Sdduvall 	ASSERT(bgep->progress & PROGRESS_HWINT);
38431369Sdduvall 
38441369Sdduvall 	/*
38451369Sdduvall 	 * Check whether chip's says it's asserting #INTA;
38461369Sdduvall 	 * if not, don't process or claim the interrupt.
38471369Sdduvall 	 *
38481369Sdduvall 	 * Note that the PCI signal is active low, so the
38491369Sdduvall 	 * bit is *zero* when the interrupt is asserted.
38501369Sdduvall 	 */
38511369Sdduvall 	result = DDI_INTR_UNCLAIMED;
38521369Sdduvall 	mutex_enter(bgep->genlock);
38531369Sdduvall 
38541369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
38551369Sdduvall 		mlcr = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
38561369Sdduvall 
38571369Sdduvall 	BGE_DEBUG(("bge_intr($%p) ($%p) mlcr 0x%08x", arg1, arg2, mlcr));
38581369Sdduvall 
38591369Sdduvall 	if ((mlcr & MLCR_INTA_STATE) == 0) {
38601369Sdduvall 		/*
38611369Sdduvall 		 * Block further PCI interrupts ...
38621369Sdduvall 		 */
38631369Sdduvall 		result = DDI_INTR_CLAIMED;
38641369Sdduvall 
38651865Sdilpreet 		if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
38661908Sly149593 			bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
38671369Sdduvall 				MHCR_MASK_PCI_INT_OUTPUT);
38681865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
38691865Sdilpreet 			    DDI_FM_OK)
38701865Sdilpreet 				goto chip_stop;
38711865Sdilpreet 		}
38721369Sdduvall 
38731369Sdduvall 		/*
38741369Sdduvall 		 * Sync the status block and grab the flags-n-tag from it.
38751369Sdduvall 		 * We count the number of interrupts where there doesn't
38761369Sdduvall 		 * seem to have been a DMA update of the status block; if
38771369Sdduvall 		 * it *has* been updated, the counter will be cleared in
38781369Sdduvall 		 * the while() loop below ...
38791369Sdduvall 		 */
38801369Sdduvall 		bgep->missed_dmas += 1;
38811369Sdduvall 		bsp = DMA_VPTR(bgep->status_block);
38821865Sdilpreet 		for (;;) {
38831865Sdilpreet 			if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
38841865Sdilpreet 				/*
38851865Sdilpreet 				 * bge_chip_stop() may have freed dma area etc
38861865Sdilpreet 				 * while we were in this interrupt handler -
38871865Sdilpreet 				 * better not call bge_status_sync()
38881865Sdilpreet 				 */
38891865Sdilpreet 				(void) bge_check_acc_handle(bgep,
38901865Sdilpreet 				    bgep->io_handle);
38911865Sdilpreet 				mutex_exit(bgep->genlock);
38921865Sdilpreet 				return (DDI_INTR_CLAIMED);
38931865Sdilpreet 			}
38941865Sdilpreet 			retval = bge_status_sync(bgep, STATUS_FLAG_UPDATED,
38951865Sdilpreet 			    &flags);
38961865Sdilpreet 			if (retval != DDI_FM_OK) {
38971865Sdilpreet 				bgep->bge_dma_error = B_TRUE;
38981865Sdilpreet 				goto chip_stop;
38991865Sdilpreet 			}
39001865Sdilpreet 
39011865Sdilpreet 			if (!(flags & STATUS_FLAG_UPDATED))
39021865Sdilpreet 				break;
39031865Sdilpreet 
39041369Sdduvall 			/*
39051369Sdduvall 			 * Tell the chip that we're processing the interrupt
39061369Sdduvall 			 */
39071369Sdduvall 			bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
39081369Sdduvall 				INTERRUPT_MBOX_DISABLE(flags));
39091865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
39101865Sdilpreet 			    DDI_FM_OK)
39111865Sdilpreet 				goto chip_stop;
39121369Sdduvall 
39131369Sdduvall 			/*
39141369Sdduvall 			 * Drop the mutex while we:
39151369Sdduvall 			 * 	Receive any newly-arrived packets
39161369Sdduvall 			 *	Recycle any newly-finished send buffers
39171369Sdduvall 			 */
39181865Sdilpreet 			bgep->bge_intr_running = B_TRUE;
39191369Sdduvall 			mutex_exit(bgep->genlock);
39201369Sdduvall 			bge_receive(bgep, bsp);
39211369Sdduvall 			bge_recycle(bgep, bsp);
39221369Sdduvall 			mutex_enter(bgep->genlock);
39231865Sdilpreet 			bgep->bge_intr_running = B_FALSE;
39241369Sdduvall 
39251369Sdduvall 			/*
39261369Sdduvall 			 * Tell the chip we've finished processing, and
39271369Sdduvall 			 * give it the tag that we got from the status
39281369Sdduvall 			 * block earlier, so that it knows just how far
39291369Sdduvall 			 * we've gone.  If it's got more for us to do,
39301369Sdduvall 			 * it will now update the status block and try
39311369Sdduvall 			 * to assert an interrupt (but we've got the
39321369Sdduvall 			 * #INTA blocked at present).  If we see the
39331369Sdduvall 			 * update, we'll loop around to do some more.
39341369Sdduvall 			 * Eventually we'll get out of here ...
39351369Sdduvall 			 */
39361369Sdduvall 			bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
39371369Sdduvall 				INTERRUPT_MBOX_ENABLE(flags));
39381369Sdduvall 			bgep->missed_dmas = 0;
39391369Sdduvall 		}
39401369Sdduvall 
39411369Sdduvall 		/*
39421369Sdduvall 		 * Check for exceptional conditions that we need to handle
39431369Sdduvall 		 *
39441369Sdduvall 		 * Link status changed
39451369Sdduvall 		 * Status block not updated
39461369Sdduvall 		 */
39471369Sdduvall 		if (flags & STATUS_FLAG_LINK_CHANGED)
39481369Sdduvall 			bge_wake_factotum(bgep);
39491369Sdduvall 
39501369Sdduvall 		if (bgep->missed_dmas) {
39511369Sdduvall 			/*
39521369Sdduvall 			 * Probably due to the internal status tag not
39531369Sdduvall 			 * being reset.  Force a status block update now;
39541369Sdduvall 			 * this should ensure that we get an update and
39551369Sdduvall 			 * a new interrupt.  After that, we should be in
39561369Sdduvall 			 * sync again ...
39571369Sdduvall 			 */
39581369Sdduvall 			BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
39591369Sdduvall 				"not updated?", flags));
39601369Sdduvall 			bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
39611369Sdduvall 				COALESCE_NOW);
39621369Sdduvall 
39631369Sdduvall 			if (bgep->missed_dmas >= bge_dma_miss_limit) {
39641369Sdduvall 				/*
39651369Sdduvall 				 * If this happens multiple times in a row,
39661369Sdduvall 				 * it means DMA is just not working.  Maybe
39671369Sdduvall 				 * the chip's failed, or maybe there's a
39681369Sdduvall 				 * problem on the PCI bus or in the host-PCI
39691369Sdduvall 				 * bridge (Tomatillo).
39701369Sdduvall 				 *
39711369Sdduvall 				 * At all events, we want to stop further
39721369Sdduvall 				 * interrupts and let the recovery code take
39731369Sdduvall 				 * over to see whether anything can be done
39741369Sdduvall 				 * about it ...
39751369Sdduvall 				 */
39761865Sdilpreet 				bge_fm_ereport(bgep,
39771865Sdilpreet 				    DDI_FM_DEVICE_BADINT_LIMIT);
39781865Sdilpreet 				goto chip_stop;
39791369Sdduvall 			}
39801369Sdduvall 		}
39811369Sdduvall 
39821369Sdduvall 		/*
39831369Sdduvall 		 * Reenable assertion of #INTA, unless there's a DMA fault
39841369Sdduvall 		 */
39851369Sdduvall 		if (result == DDI_INTR_CLAIMED) {
39861865Sdilpreet 			if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
39871908Sly149593 				bge_reg_clr32(bgep, PCI_CONF_BGE_MHCR,
39881369Sdduvall 					MHCR_MASK_PCI_INT_OUTPUT);
39891865Sdilpreet 				if (bge_check_acc_handle(bgep,
39901865Sdilpreet 				    bgep->cfg_handle) != DDI_FM_OK)
39911865Sdilpreet 					goto chip_stop;
39921865Sdilpreet 			}
39931369Sdduvall 		}
39941369Sdduvall 	}
39951369Sdduvall 
39961865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
39971865Sdilpreet 		goto chip_stop;
39981865Sdilpreet 
39991865Sdilpreet 	mutex_exit(bgep->genlock);
40001865Sdilpreet 	return (result);
40011865Sdilpreet 
40021865Sdilpreet chip_stop:
40031865Sdilpreet #ifdef BGE_IPMI_ASF
40041865Sdilpreet 	if (bgep->asf_enabled && bgep->asf_status == ASF_STAT_RUN) {
40051865Sdilpreet 		/*
40061865Sdilpreet 		 * We must stop ASF heart beat before
40071865Sdilpreet 		 * bge_chip_stop(), otherwise some
40081865Sdilpreet 		 * computers (ex. IBM HS20 blade
40091865Sdilpreet 		 * server) may crash.
40101865Sdilpreet 		 */
40111865Sdilpreet 		bge_asf_update_status(bgep);
40121865Sdilpreet 		bge_asf_stop_timer(bgep);
40131865Sdilpreet 		bgep->asf_status = ASF_STAT_STOP;
40141865Sdilpreet 
40151865Sdilpreet 		bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
40161865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
40171865Sdilpreet 	}
40181865Sdilpreet #endif
40191865Sdilpreet 	bge_chip_stop(bgep, B_TRUE);
40201865Sdilpreet 	(void) bge_check_acc_handle(bgep, bgep->io_handle);
40211369Sdduvall 	mutex_exit(bgep->genlock);
40221369Sdduvall 	return (result);
40231369Sdduvall }
40241369Sdduvall 
40251369Sdduvall /*
40261369Sdduvall  * ========== Factotum, implemented as a softint handler ==========
40271369Sdduvall  */
40281369Sdduvall 
40291369Sdduvall #undef	BGE_DBG
40301369Sdduvall #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
40311369Sdduvall 
40321369Sdduvall static void bge_factotum_error_handler(bge_t *bgep);
40331369Sdduvall #pragma	no_inline(bge_factotum_error_handler)
40341369Sdduvall 
40351369Sdduvall static void
40361369Sdduvall bge_factotum_error_handler(bge_t *bgep)
40371369Sdduvall {
40381369Sdduvall 	uint32_t flow;
40391369Sdduvall 	uint32_t rdma;
40401369Sdduvall 	uint32_t wdma;
40411369Sdduvall 	uint32_t tmac;
40421369Sdduvall 	uint32_t rmac;
40431369Sdduvall 	uint32_t rxrs;
40441369Sdduvall 	uint32_t txrs = 0;
40451369Sdduvall 
40461369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
40471369Sdduvall 
40481369Sdduvall 	/*
40491369Sdduvall 	 * Read all the registers that show the possible
40501369Sdduvall 	 * reasons for the ERROR bit to be asserted
40511369Sdduvall 	 */
40521369Sdduvall 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
40531369Sdduvall 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
40541369Sdduvall 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
40551369Sdduvall 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
40561369Sdduvall 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
40571369Sdduvall 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
40581369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40591369Sdduvall 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
40601369Sdduvall 
40611369Sdduvall 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
40621369Sdduvall 		(void *)bgep, flow, rdma, wdma));
40631369Sdduvall 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
40641369Sdduvall 		(void *)bgep, tmac, rmac, rxrs, txrs));
40651369Sdduvall 
40661369Sdduvall 	/*
40671369Sdduvall 	 * For now, just clear all the errors ...
40681369Sdduvall 	 */
40691369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
40701369Sdduvall 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
40711369Sdduvall 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
40721369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
40731369Sdduvall 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
40741369Sdduvall 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
40751369Sdduvall 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
40761369Sdduvall }
40771369Sdduvall 
40781369Sdduvall /*
40791369Sdduvall  * Handler for hardware link state change.
40801369Sdduvall  *
40811369Sdduvall  * When this routine is called, the hardware link state has changed
40821369Sdduvall  * and the new state is reflected in the param_* variables.  Here
40831369Sdduvall  * we must update the softstate, reprogram the MAC to match, and
40841369Sdduvall  * record the change in the log and/or on the console.
40851369Sdduvall  */
40861369Sdduvall static void bge_factotum_link_handler(bge_t *bgep);
40871369Sdduvall #pragma	no_inline(bge_factotum_link_handler)
40881369Sdduvall 
40891369Sdduvall static void
40901369Sdduvall bge_factotum_link_handler(bge_t *bgep)
40911369Sdduvall {
40921369Sdduvall 	void (*logfn)(bge_t *bgep, const char *fmt, ...);
40931369Sdduvall 	const char *msg;
40941369Sdduvall 	hrtime_t deltat;
40951369Sdduvall 
40961369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
40971369Sdduvall 
40981369Sdduvall 	/*
40991369Sdduvall 	 * Update the s/w link_state
41001369Sdduvall 	 */
41011369Sdduvall 	if (bgep->param_link_up)
41021369Sdduvall 		bgep->link_state = LINK_STATE_UP;
41031369Sdduvall 	else
41041369Sdduvall 		bgep->link_state = LINK_STATE_DOWN;
41051369Sdduvall 
41061369Sdduvall 	/*
41071369Sdduvall 	 * Reprogram the MAC modes to match
41081369Sdduvall 	 */
41091369Sdduvall 	bge_sync_mac_modes(bgep);
41101369Sdduvall 
41111369Sdduvall 	/*
41121369Sdduvall 	 * Finally, we have to decide whether to write a message
41131369Sdduvall 	 * on the console or only in the log.  If the PHY has
41141369Sdduvall 	 * been reprogrammed (at user request) "recently", then
41151369Sdduvall 	 * the message only goes in the log.  Otherwise it's an
41161369Sdduvall 	 * "unexpected" event, and it goes on the console as well.
41171369Sdduvall 	 */
41181369Sdduvall 	deltat = bgep->phys_event_time - bgep->phys_write_time;
41191369Sdduvall 	if (deltat > BGE_LINK_SETTLE_TIME)
41201369Sdduvall 		msg = "";
41211369Sdduvall 	else if (bgep->param_link_up)
41221369Sdduvall 		msg = bgep->link_up_msg;
41231369Sdduvall 	else
41241369Sdduvall 		msg = bgep->link_down_msg;
41251369Sdduvall 
41261369Sdduvall 	logfn = (msg == NULL || *msg == '\0') ? bge_notice : bge_log;
41271369Sdduvall 	(*logfn)(bgep, "link %s%s", bgep->link_mode_msg, msg);
41281369Sdduvall }
41291369Sdduvall 
41301865Sdilpreet static boolean_t bge_factotum_link_check(bge_t *bgep, int *dma_state);
41311369Sdduvall #pragma	no_inline(bge_factotum_link_check)
41321369Sdduvall 
41331369Sdduvall static boolean_t
41341865Sdilpreet bge_factotum_link_check(bge_t *bgep, int *dma_state)
41351369Sdduvall {
41361369Sdduvall 	boolean_t check;
41371369Sdduvall 	uint64_t flags;
41381369Sdduvall 	uint32_t tmac_status;
41391369Sdduvall 
41401369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
41411369Sdduvall 
41421369Sdduvall 	/*
41431369Sdduvall 	 * Get & clear the writable status bits in the Tx status register
41441369Sdduvall 	 * (some bits are write-1-to-clear, others are just readonly).
41451369Sdduvall 	 */
41461369Sdduvall 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
41471369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
41481369Sdduvall 
41491369Sdduvall 	/*
41501369Sdduvall 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
41511369Sdduvall 	 */
41521865Sdilpreet 	*dma_state = bge_status_sync(bgep, STATUS_FLAG_ERROR |
41531865Sdilpreet 	    STATUS_FLAG_LINK_CHANGED, &flags);
41541865Sdilpreet 	if (*dma_state != DDI_FM_OK)
41551865Sdilpreet 		return (B_FALSE);
41561369Sdduvall 
41571369Sdduvall 	/*
41581369Sdduvall 	 * Clear any errors flagged in the status block ...
41591369Sdduvall 	 */
41601369Sdduvall 	if (flags & STATUS_FLAG_ERROR)
41611369Sdduvall 		bge_factotum_error_handler(bgep);
41621369Sdduvall 
41631369Sdduvall 	/*
41641369Sdduvall 	 * We need to check the link status if:
41651369Sdduvall 	 *	the status block says there's been a link change
41661369Sdduvall 	 *	or there's any discrepancy between the various
41671369Sdduvall 	 *	flags indicating the link state (link_state,
41681369Sdduvall 	 *	param_link_up, and the LINK STATE bit in the
41691369Sdduvall 	 *	Transmit MAC status register).
41701369Sdduvall 	 */
41711369Sdduvall 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
41721369Sdduvall 	switch (bgep->link_state) {
41731369Sdduvall 	case LINK_STATE_UP:
41741369Sdduvall 		check |= (bgep->param_link_up == B_FALSE);
41751369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
41761369Sdduvall 		break;
41771369Sdduvall 
41781369Sdduvall 	case LINK_STATE_DOWN:
41791369Sdduvall 		check |= (bgep->param_link_up != B_FALSE);
41801369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
41811369Sdduvall 		break;
41821369Sdduvall 
41831369Sdduvall 	default:
41841369Sdduvall 		check = B_TRUE;
41851369Sdduvall 		break;
41861369Sdduvall 	}
41871369Sdduvall 
41881369Sdduvall 	/*
41891369Sdduvall 	 * If <check> is false, we're sure the link hasn't changed.
41901369Sdduvall 	 * If true, however, it's not yet definitive; we have to call
41911369Sdduvall 	 * bge_phys_check() to determine whether the link has settled
41921369Sdduvall 	 * into a new state yet ... and if it has, then call the link
41931369Sdduvall 	 * state change handler.But when the chip is 5700 in Dell 6650
41941369Sdduvall 	 * ,even if check is false, the link may have changed.So we
41951369Sdduvall 	 * have to call bge_phys_check() to determine the link state.
41961369Sdduvall 	 */
41971369Sdduvall 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
41981369Sdduvall 		check = bge_phys_check(bgep);
41991369Sdduvall 		if (check)
42001369Sdduvall 			bge_factotum_link_handler(bgep);
42011369Sdduvall 	}
42021369Sdduvall 
42031369Sdduvall 	return (check);
42041369Sdduvall }
42051369Sdduvall 
42061369Sdduvall /*
42071369Sdduvall  * Factotum routine to check for Tx stall, using the 'watchdog' counter
42081369Sdduvall  */
42091369Sdduvall static boolean_t bge_factotum_stall_check(bge_t *bgep);
42101369Sdduvall #pragma	no_inline(bge_factotum_stall_check)
42111369Sdduvall 
42121369Sdduvall static boolean_t
42131369Sdduvall bge_factotum_stall_check(bge_t *bgep)
42141369Sdduvall {
42151369Sdduvall 	uint32_t dogval;
42161369Sdduvall 
42171369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
42181369Sdduvall 
42191369Sdduvall 	/*
42201369Sdduvall 	 * Specific check for Tx stall ...
42211369Sdduvall 	 *
42221369Sdduvall 	 * The 'watchdog' counter is incremented whenever a packet
42231369Sdduvall 	 * is queued, reset to 1 when some (but not all) buffers
42241369Sdduvall 	 * are reclaimed, reset to 0 (disabled) when all buffers
42251369Sdduvall 	 * are reclaimed, and shifted left here.  If it exceeds the
42261369Sdduvall 	 * threshold value, the chip is assumed to have stalled and
42271369Sdduvall 	 * is put into the ERROR state.  The factotum will then reset
42281369Sdduvall 	 * it on the next pass.
42291369Sdduvall 	 *
42301369Sdduvall 	 * All of which should ensure that we don't get into a state
42311369Sdduvall 	 * where packets are left pending indefinitely!
42321369Sdduvall 	 */
42331369Sdduvall 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
42341369Sdduvall 	if (dogval < bge_watchdog_count)
42351369Sdduvall 		return (B_FALSE);
42361369Sdduvall 
42371369Sdduvall 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
42381865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_STALL);
42391369Sdduvall 	return (B_TRUE);
42401369Sdduvall }
42411369Sdduvall 
42421369Sdduvall /*
42431369Sdduvall  * The factotum is woken up when there's something to do that we'd rather
42441369Sdduvall  * not do from inside a hardware interrupt handler or high-level cyclic.
42451369Sdduvall  * Its two main tasks are:
42461369Sdduvall  *	reset & restart the chip after an error
42471369Sdduvall  *	check the link status whenever necessary
42481369Sdduvall  */
42491369Sdduvall uint_t bge_chip_factotum(caddr_t arg);
42501369Sdduvall #pragma	no_inline(bge_chip_factotum)
42511369Sdduvall 
42521369Sdduvall uint_t
42531369Sdduvall bge_chip_factotum(caddr_t arg)
42541369Sdduvall {
42551369Sdduvall 	bge_t *bgep;
42561369Sdduvall 	uint_t result;
42571369Sdduvall 	boolean_t error;
42581369Sdduvall 	boolean_t linkchg;
42591865Sdilpreet 	int dma_state;
42601369Sdduvall 
42611369Sdduvall 	bgep = (bge_t *)arg;
42621369Sdduvall 
42631369Sdduvall 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
42641369Sdduvall 
42651369Sdduvall 	mutex_enter(bgep->softintrlock);
42661369Sdduvall 	if (bgep->factotum_flag == 0) {
42671369Sdduvall 		mutex_exit(bgep->softintrlock);
42681369Sdduvall 		return (DDI_INTR_UNCLAIMED);
42691369Sdduvall 	}
42701504Sly149593 	bgep->factotum_flag = 0;
42711369Sdduvall 	mutex_exit(bgep->softintrlock);
42721369Sdduvall 
42731369Sdduvall 	result = DDI_INTR_CLAIMED;
42741369Sdduvall 	error = B_FALSE;
42751369Sdduvall 	linkchg = B_FALSE;
42761369Sdduvall 
42771369Sdduvall 	mutex_enter(bgep->genlock);
42781369Sdduvall 	switch (bgep->bge_chip_state) {
42791369Sdduvall 	default:
42801369Sdduvall 		break;
42811369Sdduvall 
42821369Sdduvall 	case BGE_CHIP_RUNNING:
42831865Sdilpreet 		linkchg = bge_factotum_link_check(bgep, &dma_state);
42841369Sdduvall 		error = bge_factotum_stall_check(bgep);
42851865Sdilpreet 		if (dma_state != DDI_FM_OK) {
42861865Sdilpreet 			bgep->bge_dma_error = B_TRUE;
42871865Sdilpreet 			error = B_TRUE;
42881865Sdilpreet 		}
42891865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
42901865Sdilpreet 			error = B_TRUE;
42911865Sdilpreet 		if (error)
42921865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_ERROR;
42931369Sdduvall 		break;
42941369Sdduvall 
42951369Sdduvall 	case BGE_CHIP_ERROR:
42961369Sdduvall 		error = B_TRUE;
42971369Sdduvall 		break;
42981369Sdduvall 
42991369Sdduvall 	case BGE_CHIP_FAULT:
43001369Sdduvall 		/*
43011369Sdduvall 		 * Fault detected, time to reset ...
43021369Sdduvall 		 */
43031369Sdduvall 		if (bge_autorecover) {
43041865Sdilpreet 			if (!(bgep->progress & PROGRESS_BUFS)) {
43051865Sdilpreet 				/*
43061865Sdilpreet 				 * if we can't allocate the ring buffers,
43071865Sdilpreet 				 * try later
43081865Sdilpreet 				 */
43091865Sdilpreet 				if (bge_alloc_bufs(bgep) != DDI_SUCCESS) {
43101865Sdilpreet 					mutex_exit(bgep->genlock);
43111865Sdilpreet 					return (result);
43121865Sdilpreet 				}
43131865Sdilpreet 				bgep->progress |= PROGRESS_BUFS;
43141865Sdilpreet 			}
43151865Sdilpreet 			if (!(bgep->progress & PROGRESS_INTR)) {
43161865Sdilpreet 				bge_init_rings(bgep);
43171865Sdilpreet 				bge_intr_enable(bgep);
43181865Sdilpreet 				bgep->progress |= PROGRESS_INTR;
43191865Sdilpreet 			}
43201865Sdilpreet 			if (!(bgep->progress & PROGRESS_KSTATS)) {
43211865Sdilpreet 				bge_init_kstats(bgep,
43221865Sdilpreet 				    ddi_get_instance(bgep->devinfo));
43231865Sdilpreet 				bgep->progress |= PROGRESS_KSTATS;
43241865Sdilpreet 			}
43251865Sdilpreet 
43261369Sdduvall 			BGE_REPORT((bgep, "automatic recovery activated"));
43271865Sdilpreet 
43281865Sdilpreet 			if (bge_restart(bgep, B_FALSE) != DDI_SUCCESS) {
43291865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43301865Sdilpreet 				error = B_TRUE;
43311865Sdilpreet 			}
43321865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
43331865Sdilpreet 			    DDI_FM_OK) {
43341865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43351865Sdilpreet 				error = B_TRUE;
43361865Sdilpreet 			}
43371865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
43381865Sdilpreet 			    DDI_FM_OK) {
43391865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
43401865Sdilpreet 				error = B_TRUE;
43411865Sdilpreet 			}
43421865Sdilpreet 			if (error == B_FALSE) {
43431408Srandyf #ifdef BGE_IPMI_ASF
43441865Sdilpreet 				if (bgep->asf_enabled &&
43451865Sdilpreet 				    bgep->asf_status != ASF_STAT_RUN) {
43461408Srandyf 					bgep->asf_timeout_id = timeout(
43471865Sdilpreet 					    bge_asf_heartbeat, (void *)bgep,
43481865Sdilpreet 					    drv_usectohz(
43491865Sdilpreet 					    BGE_ASF_HEARTBEAT_INTERVAL));
43501408Srandyf 					bgep->asf_status = ASF_STAT_RUN;
43511408Srandyf 				}
43521865Sdilpreet #endif
43531865Sdilpreet 				ddi_fm_service_impact(bgep->devinfo,
43541865Sdilpreet 				    DDI_SERVICE_RESTORED);
43551408Srandyf 			}
43561369Sdduvall 		}
43571369Sdduvall 		break;
43581369Sdduvall 	}
43591369Sdduvall 
43601865Sdilpreet 
43611369Sdduvall 	/*
43621369Sdduvall 	 * If an error is detected, stop the chip now, marking it as
43631369Sdduvall 	 * faulty, so that it will be reset next time through ...
43641865Sdilpreet 	 *
43651865Sdilpreet 	 * Note that if intr_running is set, then bge_intr() has dropped
43661865Sdilpreet 	 * genlock to call bge_receive/bge_recycle. Can't stop the chip at
43671865Sdilpreet 	 * this point so have to wait until the next time the factotum runs.
43681369Sdduvall 	 */
43691865Sdilpreet 	if (error && !bgep->bge_intr_running) {
43701408Srandyf #ifdef BGE_IPMI_ASF
43711408Srandyf 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
43721408Srandyf 			/*
43731408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
43741408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
43751408Srandyf 			 * may crash.
43761408Srandyf 			 */
43771408Srandyf 			bge_asf_update_status(bgep);
43781408Srandyf 			bge_asf_stop_timer(bgep);
43791408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
43801408Srandyf 
43811408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
43821865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
43831408Srandyf 		}
43841408Srandyf #endif
43851369Sdduvall 		bge_chip_stop(bgep, B_TRUE);
43861865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
43871408Srandyf 	}
43881369Sdduvall 	mutex_exit(bgep->genlock);
43891369Sdduvall 
43901369Sdduvall 	/*
43911369Sdduvall 	 * If the link state changed, tell the world about it.
43921369Sdduvall 	 * Note: can't do this while still holding the mutex.
43931369Sdduvall 	 */
43941369Sdduvall 	if (linkchg)
43952311Sseb 		mac_link_update(bgep->mh, bgep->link_state);
43961369Sdduvall 
43971369Sdduvall 	return (result);
43981369Sdduvall }
43991369Sdduvall 
44001369Sdduvall /*
44011369Sdduvall  * High-level cyclic handler
44021369Sdduvall  *
44031369Sdduvall  * This routine schedules a (low-level) softint callback to the
44041369Sdduvall  * factotum, and prods the chip to update the status block (which
44051369Sdduvall  * will cause a hardware interrupt when complete).
44061369Sdduvall  */
44071369Sdduvall void bge_chip_cyclic(void *arg);
44081369Sdduvall #pragma	no_inline(bge_chip_cyclic)
44091369Sdduvall 
44101369Sdduvall void
44111369Sdduvall bge_chip_cyclic(void *arg)
44121369Sdduvall {
44131369Sdduvall 	bge_t *bgep;
44141369Sdduvall 
44151369Sdduvall 	bgep = arg;
44161369Sdduvall 
44171369Sdduvall 	switch (bgep->bge_chip_state) {
44181369Sdduvall 	default:
44191369Sdduvall 		return;
44201369Sdduvall 
44211369Sdduvall 	case BGE_CHIP_RUNNING:
44221369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
44231865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
44241865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
44251865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
44261369Sdduvall 		break;
44271369Sdduvall 
44281369Sdduvall 	case BGE_CHIP_FAULT:
44291369Sdduvall 	case BGE_CHIP_ERROR:
44301369Sdduvall 		break;
44311369Sdduvall 	}
44321369Sdduvall 
44331369Sdduvall 	bge_wake_factotum(bgep);
44341369Sdduvall }
44351369Sdduvall 
44361369Sdduvall 
44371369Sdduvall /*
44381369Sdduvall  * ========== Ioctl subfunctions ==========
44391369Sdduvall  */
44401369Sdduvall 
44411369Sdduvall #undef	BGE_DBG
44421369Sdduvall #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
44431369Sdduvall 
44441369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
44451369Sdduvall 
44461369Sdduvall static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
44471369Sdduvall #pragma	no_inline(bge_chip_peek_cfg)
44481369Sdduvall 
44491369Sdduvall static void
44501369Sdduvall bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
44511369Sdduvall {
44521369Sdduvall 	uint64_t regval;
44531369Sdduvall 	uint64_t regno;
44541369Sdduvall 
44551369Sdduvall 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
44561369Sdduvall 		(void *)bgep, (void *)ppd));
44571369Sdduvall 
44581369Sdduvall 	regno = ppd->pp_acc_offset;
44591369Sdduvall 
44601369Sdduvall 	switch (ppd->pp_acc_size) {
44611369Sdduvall 	case 1:
44621369Sdduvall 		regval = pci_config_get8(bgep->cfg_handle, regno);
44631369Sdduvall 		break;
44641369Sdduvall 
44651369Sdduvall 	case 2:
44661369Sdduvall 		regval = pci_config_get16(bgep->cfg_handle, regno);
44671369Sdduvall 		break;
44681369Sdduvall 
44691369Sdduvall 	case 4:
44701369Sdduvall 		regval = pci_config_get32(bgep->cfg_handle, regno);
44711369Sdduvall 		break;
44721369Sdduvall 
44731369Sdduvall 	case 8:
44741369Sdduvall 		regval = pci_config_get64(bgep->cfg_handle, regno);
44751369Sdduvall 		break;
44761369Sdduvall 	}
44771369Sdduvall 
44781369Sdduvall 	ppd->pp_acc_data = regval;
44791369Sdduvall }
44801369Sdduvall 
44811369Sdduvall static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
44821369Sdduvall #pragma	no_inline(bge_chip_poke_cfg)
44831369Sdduvall 
44841369Sdduvall static void
44851369Sdduvall bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
44861369Sdduvall {
44871369Sdduvall 	uint64_t regval;
44881369Sdduvall 	uint64_t regno;
44891369Sdduvall 
44901369Sdduvall 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
44911369Sdduvall 		(void *)bgep, (void *)ppd));
44921369Sdduvall 
44931369Sdduvall 	regno = ppd->pp_acc_offset;
44941369Sdduvall 	regval = ppd->pp_acc_data;
44951369Sdduvall 
44961369Sdduvall 	switch (ppd->pp_acc_size) {
44971369Sdduvall 	case 1:
44981369Sdduvall 		pci_config_put8(bgep->cfg_handle, regno, regval);
44991369Sdduvall 		break;
45001369Sdduvall 
45011369Sdduvall 	case 2:
45021369Sdduvall 		pci_config_put16(bgep->cfg_handle, regno, regval);
45031369Sdduvall 		break;
45041369Sdduvall 
45051369Sdduvall 	case 4:
45061369Sdduvall 		pci_config_put32(bgep->cfg_handle, regno, regval);
45071369Sdduvall 		break;
45081369Sdduvall 
45091369Sdduvall 	case 8:
45101369Sdduvall 		pci_config_put64(bgep->cfg_handle, regno, regval);
45111369Sdduvall 		break;
45121369Sdduvall 	}
45131369Sdduvall }
45141369Sdduvall 
45151369Sdduvall static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
45161369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
45171369Sdduvall 
45181369Sdduvall static void
45191369Sdduvall bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
45201369Sdduvall {
45211369Sdduvall 	uint64_t regval;
45221369Sdduvall 	void *regaddr;
45231369Sdduvall 
45241369Sdduvall 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
45251369Sdduvall 		(void *)bgep, (void *)ppd));
45261369Sdduvall 
45271369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
45281369Sdduvall 
45291369Sdduvall 	switch (ppd->pp_acc_size) {
45301369Sdduvall 	case 1:
45311369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
45321369Sdduvall 		break;
45331369Sdduvall 
45341369Sdduvall 	case 2:
45351369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
45361369Sdduvall 		break;
45371369Sdduvall 
45381369Sdduvall 	case 4:
45391369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
45401369Sdduvall 		break;
45411369Sdduvall 
45421369Sdduvall 	case 8:
45431369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
45441369Sdduvall 		break;
45451369Sdduvall 	}
45461369Sdduvall 
45471369Sdduvall 	ppd->pp_acc_data = regval;
45481369Sdduvall }
45491369Sdduvall 
45501369Sdduvall static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
45511369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
45521369Sdduvall 
45531369Sdduvall static void
45541369Sdduvall bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
45551369Sdduvall {
45561369Sdduvall 	uint64_t regval;
45571369Sdduvall 	void *regaddr;
45581369Sdduvall 
45591369Sdduvall 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
45601369Sdduvall 		(void *)bgep, (void *)ppd));
45611369Sdduvall 
45621369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
45631369Sdduvall 	regval = ppd->pp_acc_data;
45641369Sdduvall 
45651369Sdduvall 	switch (ppd->pp_acc_size) {
45661369Sdduvall 	case 1:
45671369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
45681369Sdduvall 		break;
45691369Sdduvall 
45701369Sdduvall 	case 2:
45711369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
45721369Sdduvall 		break;
45731369Sdduvall 
45741369Sdduvall 	case 4:
45751369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
45761369Sdduvall 		break;
45771369Sdduvall 
45781369Sdduvall 	case 8:
45791369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
45801369Sdduvall 		break;
45811369Sdduvall 	}
45821369Sdduvall 	BGE_PCICHK(bgep);
45831369Sdduvall }
45841369Sdduvall 
45851369Sdduvall static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
45861369Sdduvall #pragma	no_inline(bge_chip_peek_nic)
45871369Sdduvall 
45881369Sdduvall static void
45891369Sdduvall bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
45901369Sdduvall {
45911369Sdduvall 	uint64_t regoff;
45921369Sdduvall 	uint64_t regval;
45931369Sdduvall 	void *regaddr;
45941369Sdduvall 
45951369Sdduvall 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
45961369Sdduvall 		(void *)bgep, (void *)ppd));
45971369Sdduvall 
45981369Sdduvall 	regoff = ppd->pp_acc_offset;
45991369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
46001369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
46011369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
46021369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
46031369Sdduvall 
46041369Sdduvall 	switch (ppd->pp_acc_size) {
46051369Sdduvall 	case 1:
46061369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
46071369Sdduvall 		break;
46081369Sdduvall 
46091369Sdduvall 	case 2:
46101369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
46111369Sdduvall 		break;
46121369Sdduvall 
46131369Sdduvall 	case 4:
46141369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
46151369Sdduvall 		break;
46161369Sdduvall 
46171369Sdduvall 	case 8:
46181369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
46191369Sdduvall 		break;
46201369Sdduvall 	}
46211369Sdduvall 
46221369Sdduvall 	ppd->pp_acc_data = regval;
46231369Sdduvall }
46241369Sdduvall 
46251369Sdduvall static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
46261369Sdduvall #pragma	no_inline(bge_chip_poke_nic)
46271369Sdduvall 
46281369Sdduvall static void
46291369Sdduvall bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
46301369Sdduvall {
46311369Sdduvall 	uint64_t regoff;
46321369Sdduvall 	uint64_t regval;
46331369Sdduvall 	void *regaddr;
46341369Sdduvall 
46351369Sdduvall 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
46361369Sdduvall 		(void *)bgep, (void *)ppd));
46371369Sdduvall 
46381369Sdduvall 	regoff = ppd->pp_acc_offset;
46391369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
46401369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
46411369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
46421369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
46431369Sdduvall 	regval = ppd->pp_acc_data;
46441369Sdduvall 
46451369Sdduvall 	switch (ppd->pp_acc_size) {
46461369Sdduvall 	case 1:
46471369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
46481369Sdduvall 		break;
46491369Sdduvall 
46501369Sdduvall 	case 2:
46511369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
46521369Sdduvall 		break;
46531369Sdduvall 
46541369Sdduvall 	case 4:
46551369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
46561369Sdduvall 		break;
46571369Sdduvall 
46581369Sdduvall 	case 8:
46591369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
46601369Sdduvall 		break;
46611369Sdduvall 	}
46621369Sdduvall 	BGE_PCICHK(bgep);
46631369Sdduvall }
46641369Sdduvall 
46651369Sdduvall static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
46661369Sdduvall #pragma	no_inline(bge_chip_peek_mii)
46671369Sdduvall 
46681369Sdduvall static void
46691369Sdduvall bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
46701369Sdduvall {
46711369Sdduvall 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
46721369Sdduvall 		(void *)bgep, (void *)ppd));
46731369Sdduvall 
46741369Sdduvall 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
46751369Sdduvall }
46761369Sdduvall 
46771369Sdduvall static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
46781369Sdduvall #pragma	no_inline(bge_chip_poke_mii)
46791369Sdduvall 
46801369Sdduvall static void
46811369Sdduvall bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
46821369Sdduvall {
46831369Sdduvall 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
46841369Sdduvall 		(void *)bgep, (void *)ppd));
46851369Sdduvall 
46861369Sdduvall 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
46871369Sdduvall }
46881369Sdduvall 
46891369Sdduvall #if	BGE_SEE_IO32
46901369Sdduvall 
46911369Sdduvall static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
46921369Sdduvall #pragma	no_inline(bge_chip_peek_seeprom)
46931369Sdduvall 
46941369Sdduvall static void
46951369Sdduvall bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
46961369Sdduvall {
46971369Sdduvall 	uint32_t data;
46981369Sdduvall 	int err;
46991369Sdduvall 
47001369Sdduvall 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
47011369Sdduvall 		(void *)bgep, (void *)ppd));
47021369Sdduvall 
47031369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
47041369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
47051369Sdduvall }
47061369Sdduvall 
47071369Sdduvall static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
47081369Sdduvall #pragma	no_inline(bge_chip_poke_seeprom)
47091369Sdduvall 
47101369Sdduvall static void
47111369Sdduvall bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
47121369Sdduvall {
47131369Sdduvall 	uint32_t data;
47141369Sdduvall 
47151369Sdduvall 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
47161369Sdduvall 		(void *)bgep, (void *)ppd));
47171369Sdduvall 
47181369Sdduvall 	data = ppd->pp_acc_data;
47191369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
47201369Sdduvall }
47211369Sdduvall #endif	/* BGE_SEE_IO32 */
47221369Sdduvall 
47231369Sdduvall #if	BGE_FLASH_IO32
47241369Sdduvall 
47251369Sdduvall static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
47261369Sdduvall #pragma	no_inline(bge_chip_peek_flash)
47271369Sdduvall 
47281369Sdduvall static void
47291369Sdduvall bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
47301369Sdduvall {
47311369Sdduvall 	uint32_t data;
47321369Sdduvall 	int err;
47331369Sdduvall 
47341369Sdduvall 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
47351369Sdduvall 		(void *)bgep, (void *)ppd));
47361369Sdduvall 
47371369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
47381369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
47391369Sdduvall }
47401369Sdduvall 
47411369Sdduvall static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
47421369Sdduvall #pragma	no_inline(bge_chip_poke_flash)
47431369Sdduvall 
47441369Sdduvall static void
47451369Sdduvall bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
47461369Sdduvall {
47471369Sdduvall 	uint32_t data;
47481369Sdduvall 
47491369Sdduvall 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
47501369Sdduvall 		(void *)bgep, (void *)ppd));
47511369Sdduvall 
47521369Sdduvall 	data = ppd->pp_acc_data;
47531369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
47541369Sdduvall 	    ppd->pp_acc_offset, &data);
47551369Sdduvall }
47561369Sdduvall #endif	/* BGE_FLASH_IO32 */
47571369Sdduvall 
47581369Sdduvall static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
47591369Sdduvall #pragma	no_inline(bge_chip_peek_mem)
47601369Sdduvall 
47611369Sdduvall static void
47621369Sdduvall bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
47631369Sdduvall {
47641369Sdduvall 	uint64_t regval;
47651369Sdduvall 	void *vaddr;
47661369Sdduvall 
47671369Sdduvall 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
47681369Sdduvall 		(void *)bgep, (void *)ppd));
47691369Sdduvall 
47701369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
47711369Sdduvall 
47721369Sdduvall 	switch (ppd->pp_acc_size) {
47731369Sdduvall 	case 1:
47741369Sdduvall 		regval = *(uint8_t *)vaddr;
47751369Sdduvall 		break;
47761369Sdduvall 
47771369Sdduvall 	case 2:
47781369Sdduvall 		regval = *(uint16_t *)vaddr;
47791369Sdduvall 		break;
47801369Sdduvall 
47811369Sdduvall 	case 4:
47821369Sdduvall 		regval = *(uint32_t *)vaddr;
47831369Sdduvall 		break;
47841369Sdduvall 
47851369Sdduvall 	case 8:
47861369Sdduvall 		regval = *(uint64_t *)vaddr;
47871369Sdduvall 		break;
47881369Sdduvall 	}
47891369Sdduvall 
47901369Sdduvall 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
47911369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
47921369Sdduvall 
47931369Sdduvall 	ppd->pp_acc_data = regval;
47941369Sdduvall }
47951369Sdduvall 
47961369Sdduvall static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
47971369Sdduvall #pragma	no_inline(bge_chip_poke_mem)
47981369Sdduvall 
47991369Sdduvall static void
48001369Sdduvall bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
48011369Sdduvall {
48021369Sdduvall 	uint64_t regval;
48031369Sdduvall 	void *vaddr;
48041369Sdduvall 
48051369Sdduvall 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
48061369Sdduvall 		(void *)bgep, (void *)ppd));
48071369Sdduvall 
48081369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
48091369Sdduvall 	regval = ppd->pp_acc_data;
48101369Sdduvall 
48111369Sdduvall 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
48121369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
48131369Sdduvall 
48141369Sdduvall 	switch (ppd->pp_acc_size) {
48151369Sdduvall 	case 1:
48161369Sdduvall 		*(uint8_t *)vaddr = (uint8_t)regval;
48171369Sdduvall 		break;
48181369Sdduvall 
48191369Sdduvall 	case 2:
48201369Sdduvall 		*(uint16_t *)vaddr = (uint16_t)regval;
48211369Sdduvall 		break;
48221369Sdduvall 
48231369Sdduvall 	case 4:
48241369Sdduvall 		*(uint32_t *)vaddr = (uint32_t)regval;
48251369Sdduvall 		break;
48261369Sdduvall 
48271369Sdduvall 	case 8:
48281369Sdduvall 		*(uint64_t *)vaddr = (uint64_t)regval;
48291369Sdduvall 		break;
48301369Sdduvall 	}
48311369Sdduvall }
48321369Sdduvall 
48331369Sdduvall static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
48341369Sdduvall 					struct iocblk *iocp);
48351369Sdduvall #pragma	no_inline(bge_pp_ioctl)
48361369Sdduvall 
48371369Sdduvall static enum ioc_reply
48381369Sdduvall bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
48391369Sdduvall {
48401369Sdduvall 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
48411369Sdduvall 	bge_peekpoke_t *ppd;
48421369Sdduvall 	dma_area_t *areap;
48431369Sdduvall 	uint64_t sizemask;
48441369Sdduvall 	uint64_t mem_va;
48451369Sdduvall 	uint64_t maxoff;
48461369Sdduvall 	boolean_t peek;
48471369Sdduvall 
48481369Sdduvall 	switch (cmd) {
48491369Sdduvall 	default:
48501369Sdduvall 		/* NOTREACHED */
48511369Sdduvall 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
48521369Sdduvall 		return (IOC_INVAL);
48531369Sdduvall 
48541369Sdduvall 	case BGE_PEEK:
48551369Sdduvall 		peek = B_TRUE;
48561369Sdduvall 		break;
48571369Sdduvall 
48581369Sdduvall 	case BGE_POKE:
48591369Sdduvall 		peek = B_FALSE;
48601369Sdduvall 		break;
48611369Sdduvall 	}
48621369Sdduvall 
48631369Sdduvall 	/*
48641369Sdduvall 	 * Validate format of ioctl
48651369Sdduvall 	 */
48661369Sdduvall 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
48671369Sdduvall 		return (IOC_INVAL);
48681369Sdduvall 	if (mp->b_cont == NULL)
48691369Sdduvall 		return (IOC_INVAL);
48701369Sdduvall 	ppd = (bge_peekpoke_t *)mp->b_cont->b_rptr;
48711369Sdduvall 
48721369Sdduvall 	/*
48731369Sdduvall 	 * Validate request parameters
48741369Sdduvall 	 */
48751369Sdduvall 	switch (ppd->pp_acc_space) {
48761369Sdduvall 	default:
48771369Sdduvall 		return (IOC_INVAL);
48781369Sdduvall 
48791369Sdduvall 	case BGE_PP_SPACE_CFG:
48801369Sdduvall 		/*
48811369Sdduvall 		 * Config space
48821369Sdduvall 		 */
48831369Sdduvall 		sizemask = 8|4|2|1;
48841369Sdduvall 		mem_va = 0;
48851369Sdduvall 		maxoff = PCI_CONF_HDR_SIZE;
48861369Sdduvall 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
48871369Sdduvall 		break;
48881369Sdduvall 
48891369Sdduvall 	case BGE_PP_SPACE_REG:
48901369Sdduvall 		/*
48911369Sdduvall 		 * Memory-mapped I/O space
48921369Sdduvall 		 */
48931369Sdduvall 		sizemask = 8|4|2|1;
48941369Sdduvall 		mem_va = 0;
48951369Sdduvall 		maxoff = RIAAR_REGISTER_MAX;
48961369Sdduvall 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
48971369Sdduvall 		break;
48981369Sdduvall 
48991369Sdduvall 	case BGE_PP_SPACE_NIC:
49001369Sdduvall 		/*
49011369Sdduvall 		 * NIC on-chip memory
49021369Sdduvall 		 */
49031369Sdduvall 		sizemask = 8|4|2|1;
49041369Sdduvall 		mem_va = 0;
49051369Sdduvall 		maxoff = MWBAR_ONCHIP_MAX;
49061369Sdduvall 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
49071369Sdduvall 		break;
49081369Sdduvall 
49091369Sdduvall 	case BGE_PP_SPACE_MII:
49101369Sdduvall 		/*
49111369Sdduvall 		 * PHY's MII registers
49121369Sdduvall 		 * NB: all PHY registers are two bytes, but the
49131369Sdduvall 		 * addresses increment in ones (word addressing).
49141369Sdduvall 		 * So we scale the address here, then undo the
49151369Sdduvall 		 * transformation inside the peek/poke functions.
49161369Sdduvall 		 */
49171369Sdduvall 		ppd->pp_acc_offset *= 2;
49181369Sdduvall 		sizemask = 2;
49191369Sdduvall 		mem_va = 0;
49201369Sdduvall 		maxoff = (MII_MAXREG+1)*2;
49211369Sdduvall 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
49221369Sdduvall 		break;
49231369Sdduvall 
49241369Sdduvall #if	BGE_SEE_IO32
49251369Sdduvall 	case BGE_PP_SPACE_SEEPROM:
49261369Sdduvall 		/*
49271369Sdduvall 		 * Attached SEEPROM(s), if any.
49281369Sdduvall 		 * NB: we use the high-order bits of the 'address' as
49291369Sdduvall 		 * a device select to accommodate multiple SEEPROMS,
49301369Sdduvall 		 * If each one is the maximum size (64kbytes), this
49311369Sdduvall 		 * makes them appear contiguous.  Otherwise, there may
49321369Sdduvall 		 * be holes in the mapping.  ENxS doesn't have any
49331369Sdduvall 		 * SEEPROMs anyway ...
49341369Sdduvall 		 */
49351369Sdduvall 		sizemask = 4;
49361369Sdduvall 		mem_va = 0;
49371369Sdduvall 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
49381369Sdduvall 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
49391369Sdduvall 		break;
49401369Sdduvall #endif	/* BGE_SEE_IO32 */
49411369Sdduvall 
49421369Sdduvall #if	BGE_FLASH_IO32
49431369Sdduvall 	case BGE_PP_SPACE_FLASH:
49441369Sdduvall 		/*
49451369Sdduvall 		 * Attached Flash device (if any); a maximum of one device
49461369Sdduvall 		 * is currently supported.  But it can be up to 1MB (unlike
49471369Sdduvall 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
49481369Sdduvall 		 */
49491369Sdduvall 		sizemask = 4;
49501369Sdduvall 		mem_va = 0;
49511369Sdduvall 		maxoff = NVM_FLASH_ADDR_MASK;
49521369Sdduvall 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
49531369Sdduvall 		break;
49541369Sdduvall #endif	/* BGE_FLASH_IO32 */
49551369Sdduvall 
49561369Sdduvall 	case BGE_PP_SPACE_BGE:
49571369Sdduvall 		/*
49581369Sdduvall 		 * BGE data structure!
49591369Sdduvall 		 */
49601369Sdduvall 		sizemask = 8|4|2|1;
49611369Sdduvall 		mem_va = (uintptr_t)bgep;
49621369Sdduvall 		maxoff = sizeof (*bgep);
49631369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
49641369Sdduvall 		break;
49651369Sdduvall 
49661369Sdduvall 	case BGE_PP_SPACE_STATUS:
49671369Sdduvall 	case BGE_PP_SPACE_STATISTICS:
49681369Sdduvall 	case BGE_PP_SPACE_TXDESC:
49691369Sdduvall 	case BGE_PP_SPACE_TXBUFF:
49701369Sdduvall 	case BGE_PP_SPACE_RXDESC:
49711369Sdduvall 	case BGE_PP_SPACE_RXBUFF:
49721369Sdduvall 		/*
49731369Sdduvall 		 * Various DMA_AREAs
49741369Sdduvall 		 */
49751369Sdduvall 		switch (ppd->pp_acc_space) {
49761369Sdduvall 		case BGE_PP_SPACE_TXDESC:
49771369Sdduvall 			areap = &bgep->tx_desc;
49781369Sdduvall 			break;
49791369Sdduvall 		case BGE_PP_SPACE_TXBUFF:
49801369Sdduvall 			areap = &bgep->tx_buff[0];
49811369Sdduvall 			break;
49821369Sdduvall 		case BGE_PP_SPACE_RXDESC:
49831369Sdduvall 			areap = &bgep->rx_desc[0];
49841369Sdduvall 			break;
49851369Sdduvall 		case BGE_PP_SPACE_RXBUFF:
49861369Sdduvall 			areap = &bgep->rx_buff[0];
49871369Sdduvall 			break;
49881369Sdduvall 		case BGE_PP_SPACE_STATUS:
49891369Sdduvall 			areap = &bgep->status_block;
49901369Sdduvall 			break;
49911369Sdduvall 		case BGE_PP_SPACE_STATISTICS:
49921369Sdduvall 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
49931369Sdduvall 				areap = &bgep->statistics;
49941369Sdduvall 			break;
49951369Sdduvall 		}
49961369Sdduvall 
49971369Sdduvall 		sizemask = 8|4|2|1;
49981369Sdduvall 		mem_va = (uintptr_t)areap->mem_va;
49991369Sdduvall 		maxoff = areap->alength;
50001369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
50011369Sdduvall 		break;
50021369Sdduvall 	}
50031369Sdduvall 
50041369Sdduvall 	switch (ppd->pp_acc_size) {
50051369Sdduvall 	default:
50061369Sdduvall 		return (IOC_INVAL);
50071369Sdduvall 
50081369Sdduvall 	case 8:
50091369Sdduvall 	case 4:
50101369Sdduvall 	case 2:
50111369Sdduvall 	case 1:
50121369Sdduvall 		if ((ppd->pp_acc_size & sizemask) == 0)
50131369Sdduvall 			return (IOC_INVAL);
50141369Sdduvall 		break;
50151369Sdduvall 	}
50161369Sdduvall 
50171369Sdduvall 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
50181369Sdduvall 		return (IOC_INVAL);
50191369Sdduvall 
50201369Sdduvall 	if (ppd->pp_acc_offset >= maxoff)
50211369Sdduvall 		return (IOC_INVAL);
50221369Sdduvall 
50231369Sdduvall 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
50241369Sdduvall 		return (IOC_INVAL);
50251369Sdduvall 
50261369Sdduvall 	/*
50271369Sdduvall 	 * All OK - go do it!
50281369Sdduvall 	 */
50291369Sdduvall 	ppd->pp_acc_offset += mem_va;
50301369Sdduvall 	(*ppfn)(bgep, ppd);
50311369Sdduvall 	return (peek ? IOC_REPLY : IOC_ACK);
50321369Sdduvall }
50331369Sdduvall 
50341369Sdduvall static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
50351369Sdduvall 					struct iocblk *iocp);
50361369Sdduvall #pragma	no_inline(bge_diag_ioctl)
50371369Sdduvall 
50381369Sdduvall static enum ioc_reply
50391369Sdduvall bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
50401369Sdduvall {
50411369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
50421369Sdduvall 
50431369Sdduvall 	switch (cmd) {
50441369Sdduvall 	default:
50451369Sdduvall 		/* NOTREACHED */
50461369Sdduvall 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
50471369Sdduvall 		return (IOC_INVAL);
50481369Sdduvall 
50491369Sdduvall 	case BGE_DIAG:
50501369Sdduvall 		/*
50511369Sdduvall 		 * Currently a no-op
50521369Sdduvall 		 */
50531369Sdduvall 		return (IOC_ACK);
50541369Sdduvall 
50551369Sdduvall 	case BGE_PEEK:
50561369Sdduvall 	case BGE_POKE:
50571369Sdduvall 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
50581369Sdduvall 
50591369Sdduvall 	case BGE_PHY_RESET:
50601369Sdduvall 		return (IOC_RESTART_ACK);
50611369Sdduvall 
50621369Sdduvall 	case BGE_SOFT_RESET:
50631369Sdduvall 	case BGE_HARD_RESET:
50641369Sdduvall 		/*
50651369Sdduvall 		 * Reset and reinitialise the 570x hardware
50661369Sdduvall 		 */
50671865Sdilpreet 		(void) bge_restart(bgep, cmd == BGE_HARD_RESET);
50681369Sdduvall 		return (IOC_ACK);
50691369Sdduvall 	}
50701369Sdduvall 
50711369Sdduvall 	/* NOTREACHED */
50721369Sdduvall }
50731369Sdduvall 
50741369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
50751369Sdduvall 
50761369Sdduvall static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
50771369Sdduvall 				    struct iocblk *iocp);
50781369Sdduvall #pragma	no_inline(bge_mii_ioctl)
50791369Sdduvall 
50801369Sdduvall static enum ioc_reply
50811369Sdduvall bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
50821369Sdduvall {
50831369Sdduvall 	struct bge_mii_rw *miirwp;
50841369Sdduvall 
50851369Sdduvall 	/*
50861369Sdduvall 	 * Validate format of ioctl
50871369Sdduvall 	 */
50881369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
50891369Sdduvall 		return (IOC_INVAL);
50901369Sdduvall 	if (mp->b_cont == NULL)
50911369Sdduvall 		return (IOC_INVAL);
50921369Sdduvall 	miirwp = (struct bge_mii_rw *)mp->b_cont->b_rptr;
50931369Sdduvall 
50941369Sdduvall 	/*
50951369Sdduvall 	 * Validate request parameters ...
50961369Sdduvall 	 */
50971369Sdduvall 	if (miirwp->mii_reg > MII_MAXREG)
50981369Sdduvall 		return (IOC_INVAL);
50991369Sdduvall 
51001369Sdduvall 	switch (cmd) {
51011369Sdduvall 	default:
51021369Sdduvall 		/* NOTREACHED */
51031369Sdduvall 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
51041369Sdduvall 		return (IOC_INVAL);
51051369Sdduvall 
51061369Sdduvall 	case BGE_MII_READ:
51071369Sdduvall 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
51081369Sdduvall 		return (IOC_REPLY);
51091369Sdduvall 
51101369Sdduvall 	case BGE_MII_WRITE:
51111369Sdduvall 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
51121369Sdduvall 		return (IOC_ACK);
51131369Sdduvall 	}
51141369Sdduvall 
51151369Sdduvall 	/* NOTREACHED */
51161369Sdduvall }
51171369Sdduvall 
51181369Sdduvall #if	BGE_SEE_IO32
51191369Sdduvall 
51201369Sdduvall static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51211369Sdduvall 				    struct iocblk *iocp);
51221369Sdduvall #pragma	no_inline(bge_see_ioctl)
51231369Sdduvall 
51241369Sdduvall static enum ioc_reply
51251369Sdduvall bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51261369Sdduvall {
51271369Sdduvall 	struct bge_see_rw *seerwp;
51281369Sdduvall 
51291369Sdduvall 	/*
51301369Sdduvall 	 * Validate format of ioctl
51311369Sdduvall 	 */
51321369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
51331369Sdduvall 		return (IOC_INVAL);
51341369Sdduvall 	if (mp->b_cont == NULL)
51351369Sdduvall 		return (IOC_INVAL);
51361369Sdduvall 	seerwp = (struct bge_see_rw *)mp->b_cont->b_rptr;
51371369Sdduvall 
51381369Sdduvall 	/*
51391369Sdduvall 	 * Validate request parameters ...
51401369Sdduvall 	 */
51411369Sdduvall 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
51421369Sdduvall 		return (IOC_INVAL);
51431369Sdduvall 
51441369Sdduvall 	switch (cmd) {
51451369Sdduvall 	default:
51461369Sdduvall 		/* NOTREACHED */
51471369Sdduvall 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
51481369Sdduvall 		return (IOC_INVAL);
51491369Sdduvall 
51501369Sdduvall 	case BGE_SEE_READ:
51511369Sdduvall 	case BGE_SEE_WRITE:
51521369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
51531369Sdduvall 		    seerwp->see_addr, &seerwp->see_data);
51541369Sdduvall 		return (IOC_REPLY);
51551369Sdduvall 	}
51561369Sdduvall 
51571369Sdduvall 	/* NOTREACHED */
51581369Sdduvall }
51591369Sdduvall 
51601369Sdduvall #endif	/* BGE_SEE_IO32 */
51611369Sdduvall 
51621369Sdduvall #if	BGE_FLASH_IO32
51631369Sdduvall 
51641369Sdduvall static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51651369Sdduvall 				    struct iocblk *iocp);
51661369Sdduvall #pragma	no_inline(bge_flash_ioctl)
51671369Sdduvall 
51681369Sdduvall static enum ioc_reply
51691369Sdduvall bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51701369Sdduvall {
51711369Sdduvall 	struct bge_flash_rw *flashrwp;
51721369Sdduvall 
51731369Sdduvall 	/*
51741369Sdduvall 	 * Validate format of ioctl
51751369Sdduvall 	 */
51761369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
51771369Sdduvall 		return (IOC_INVAL);
51781369Sdduvall 	if (mp->b_cont == NULL)
51791369Sdduvall 		return (IOC_INVAL);
51801369Sdduvall 	flashrwp = (struct bge_flash_rw *)mp->b_cont->b_rptr;
51811369Sdduvall 
51821369Sdduvall 	/*
51831369Sdduvall 	 * Validate request parameters ...
51841369Sdduvall 	 */
51851369Sdduvall 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
51861369Sdduvall 		return (IOC_INVAL);
51871369Sdduvall 
51881369Sdduvall 	switch (cmd) {
51891369Sdduvall 	default:
51901369Sdduvall 		/* NOTREACHED */
51911369Sdduvall 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
51921369Sdduvall 		return (IOC_INVAL);
51931369Sdduvall 
51941369Sdduvall 	case BGE_FLASH_READ:
51951369Sdduvall 	case BGE_FLASH_WRITE:
51961369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
51971369Sdduvall 		    flashrwp->flash_addr, &flashrwp->flash_data);
51981369Sdduvall 		return (IOC_REPLY);
51991369Sdduvall 	}
52001369Sdduvall 
52011369Sdduvall 	/* NOTREACHED */
52021369Sdduvall }
52031369Sdduvall 
52041369Sdduvall #endif	/* BGE_FLASH_IO32 */
52051369Sdduvall 
52061369Sdduvall enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
52071369Sdduvall 				struct iocblk *iocp);
52081369Sdduvall #pragma	no_inline(bge_chip_ioctl)
52091369Sdduvall 
52101369Sdduvall enum ioc_reply
52111369Sdduvall bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
52121369Sdduvall {
52131369Sdduvall 	int cmd;
52141369Sdduvall 
52151369Sdduvall 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
52161369Sdduvall 		(void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
52171369Sdduvall 
52181369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
52191369Sdduvall 
52201369Sdduvall 	cmd = iocp->ioc_cmd;
52211369Sdduvall 	switch (cmd) {
52221369Sdduvall 	default:
52231369Sdduvall 		/* NOTREACHED */
52241369Sdduvall 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
52251369Sdduvall 		return (IOC_INVAL);
52261369Sdduvall 
52271369Sdduvall 	case BGE_DIAG:
52281369Sdduvall 	case BGE_PEEK:
52291369Sdduvall 	case BGE_POKE:
52301369Sdduvall 	case BGE_PHY_RESET:
52311369Sdduvall 	case BGE_SOFT_RESET:
52321369Sdduvall 	case BGE_HARD_RESET:
52331369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
52341369Sdduvall 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
52351369Sdduvall #else
52361369Sdduvall 		return (IOC_INVAL);
52371369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
52381369Sdduvall 
52391369Sdduvall 	case BGE_MII_READ:
52401369Sdduvall 	case BGE_MII_WRITE:
52411369Sdduvall 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
52421369Sdduvall 
52431369Sdduvall #if	BGE_SEE_IO32
52441369Sdduvall 	case BGE_SEE_READ:
52451369Sdduvall 	case BGE_SEE_WRITE:
52461369Sdduvall 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
52471369Sdduvall #endif	/* BGE_SEE_IO32 */
52481369Sdduvall 
52491369Sdduvall #if	BGE_FLASH_IO32
52501369Sdduvall 	case BGE_FLASH_READ:
52511369Sdduvall 	case BGE_FLASH_WRITE:
52521369Sdduvall 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
52531369Sdduvall #endif	/* BGE_FLASH_IO32 */
52541369Sdduvall 	}
52551369Sdduvall 
52561369Sdduvall 	/* NOTREACHED */
52571369Sdduvall }
52581369Sdduvall 
52591369Sdduvall void
52601369Sdduvall bge_chip_blank(void *arg, time_t ticks, uint_t count)
52611369Sdduvall {
52621369Sdduvall 	bge_t *bgep = arg;
52631369Sdduvall 
52641865Sdilpreet 	mutex_enter(bgep->genlock);
52651369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
52661369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
52671865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
52681865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
52691865Sdilpreet 	mutex_exit(bgep->genlock);
52701369Sdduvall }
52711408Srandyf 
52721408Srandyf #ifdef BGE_IPMI_ASF
52731408Srandyf 
52741408Srandyf uint32_t
52751408Srandyf bge_nic_read32(bge_t *bgep, bge_regno_t addr)
52761408Srandyf {
52771408Srandyf 	uint32_t data;
52781408Srandyf 
52791408Srandyf 	if (!bgep->asf_wordswapped) {
52801408Srandyf 		/* a workaround word swap error */
52811408Srandyf 		if (addr & 4)
52821408Srandyf 			addr = addr - 4;
52831408Srandyf 		else
52841408Srandyf 			addr = addr + 4;
52851408Srandyf 	}
52861408Srandyf 
52871408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
52881408Srandyf 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
52891408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
52901408Srandyf 
52911408Srandyf 	return (data);
52921408Srandyf }
52931408Srandyf 
52941408Srandyf 
52951408Srandyf void
52961408Srandyf bge_asf_update_status(bge_t *bgep)
52971408Srandyf {
52981408Srandyf 	uint32_t event;
52991408Srandyf 
53001408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
53011408Srandyf 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
53021408Srandyf 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
53031408Srandyf 
53041408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
53051408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
53061408Srandyf }
53071408Srandyf 
53081408Srandyf 
53091408Srandyf /*
53101408Srandyf  * The driver is supposed to notify ASF that the OS is still running
53111408Srandyf  * every three seconds, otherwise the management server may attempt
53121408Srandyf  * to reboot the machine.  If it hasn't actually failed, this is
53132135Szh199473  * not a desirable result.  However, this isn't running as a real-time
53141408Srandyf  * thread, and even if it were, it might not be able to generate the
53151408Srandyf  * heartbeat in a timely manner due to system load.  As it isn't a
53161408Srandyf  * significant strain on the machine, we will set the interval to half
53171408Srandyf  * of the required value.
53181408Srandyf  */
53191408Srandyf void
53201865Sdilpreet bge_asf_heartbeat(void *arg)
53211408Srandyf {
53221865Sdilpreet 	bge_t *bgep = (bge_t *)arg;
53231865Sdilpreet 
53241865Sdilpreet 	mutex_enter(bgep->genlock);
53251408Srandyf 	bge_asf_update_status((bge_t *)bgep);
53261865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
53271865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
53281865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
53291865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
53301865Sdilpreet 	mutex_exit(bgep->genlock);
53311408Srandyf 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
53321408Srandyf 		drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
53331408Srandyf }
53341408Srandyf 
53351408Srandyf 
53361408Srandyf void
53371408Srandyf bge_asf_stop_timer(bge_t *bgep)
53381408Srandyf {
53391408Srandyf 	timeout_id_t tmp_id = 0;
53401408Srandyf 
53411408Srandyf 	while ((bgep->asf_timeout_id != 0) &&
53421408Srandyf 		(tmp_id != bgep->asf_timeout_id)) {
53431408Srandyf 		tmp_id = bgep->asf_timeout_id;
53441408Srandyf 		(void) untimeout(tmp_id);
53451408Srandyf 	}
53461408Srandyf 	bgep->asf_timeout_id = 0;
53471408Srandyf }
53481408Srandyf 
53491408Srandyf 
53501408Srandyf 
53511408Srandyf /*
53522135Szh199473  * This function should be placed at the earliest position of bge_attach().
53531408Srandyf  */
53541408Srandyf void
53551408Srandyf bge_asf_get_config(bge_t *bgep)
53561408Srandyf {
53571408Srandyf 	uint32_t nicsig;
53581408Srandyf 	uint32_t niccfg;
53591408Srandyf 
53601408Srandyf 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
53611408Srandyf 	if (nicsig == BGE_NIC_DATA_SIG) {
53621408Srandyf 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
53631408Srandyf 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
53641408Srandyf 			/*
53651408Srandyf 			 * Here, we don't consider BAXTER, because BGE haven't
53661408Srandyf 			 * supported BAXTER (that is 5752). Also, as I know,
53671408Srandyf 			 * BAXTER doesn't support ASF feature.
53681408Srandyf 			 */
53691408Srandyf 			bgep->asf_enabled = B_TRUE;
53701408Srandyf 		else
53711408Srandyf 			bgep->asf_enabled = B_FALSE;
53721408Srandyf 	} else
53731408Srandyf 		bgep->asf_enabled = B_FALSE;
53741408Srandyf }
53751408Srandyf 
53761408Srandyf 
53771408Srandyf void
53781408Srandyf bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
53791408Srandyf {
53801408Srandyf 	uint32_t tries;
53811408Srandyf 	uint32_t event;
53821408Srandyf 
53831408Srandyf 	ASSERT(bgep->asf_enabled);
53841408Srandyf 
53851408Srandyf 	/* Issues "pause firmware" command and wait for ACK */
53861408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
53871408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
53881408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
53891408Srandyf 
53901408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
53911408Srandyf 	tries = 0;
53921408Srandyf 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
53931408Srandyf 		drv_usecwait(1);
53941408Srandyf 		tries ++;
53951408Srandyf 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
53961408Srandyf 	}
53971408Srandyf 
53981408Srandyf 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
53991408Srandyf 		BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
54001408Srandyf 
54011408Srandyf 	if (bgep->asf_newhandshake) {
54021408Srandyf 		switch (mode) {
54031408Srandyf 		case BGE_INIT_RESET:
54041408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54051408Srandyf 				BGE_DRV_STATE_START);
54061408Srandyf 			break;
54071408Srandyf 		case BGE_SHUTDOWN_RESET:
54081408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54091408Srandyf 				BGE_DRV_STATE_UNLOAD);
54101408Srandyf 			break;
54111408Srandyf 		case BGE_SUSPEND_RESET:
54121408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54131408Srandyf 				BGE_DRV_STATE_SUSPEND);
54141408Srandyf 			break;
54151408Srandyf 		default:
54161408Srandyf 			break;
54171408Srandyf 		}
54181408Srandyf 	}
54191408Srandyf }
54201408Srandyf 
54211408Srandyf 
54221408Srandyf void
54231408Srandyf bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
54241408Srandyf {
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 void
54451408Srandyf bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
54461408Srandyf {
54471408Srandyf 	switch (mode) {
54481408Srandyf 	case BGE_INIT_RESET:
54491408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54501408Srandyf 			BGE_DRV_STATE_START_DONE);
54511408Srandyf 		break;
54521408Srandyf 	case BGE_SHUTDOWN_RESET:
54531408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
54541408Srandyf 			BGE_DRV_STATE_UNLOAD_DONE);
54551408Srandyf 		break;
54561408Srandyf 	default:
54571408Srandyf 		break;
54581408Srandyf 	}
54591408Srandyf }
54601408Srandyf 
54611408Srandyf #endif /* BGE_IPMI_ASF */
5462