xref: /onnv-gate/usr/src/uts/common/io/bge/bge_chip2.c (revision 6546:327ec011df73)
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 /*
235903Ssowmini  * Copyright 2008 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	*/
373918Sml149210 #define	BGE_IND_IO32	1	/* 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 /*
553907Szh199473  * PCI-X/PCI-E relaxed ordering tunable for OS/Nexus driver
563907Szh199473  */
573907Szh199473 boolean_t bge_relaxed_ordering = B_TRUE;
583907Szh199473 
593907Szh199473 /*
601369Sdduvall  * Property names
611369Sdduvall  */
621369Sdduvall static char knownids_propname[] = "bge-known-subsystems";
631369Sdduvall 
641369Sdduvall /*
651369Sdduvall  * Patchable globals:
661369Sdduvall  *
671369Sdduvall  *	bge_autorecover
681369Sdduvall  *		Enables/disables automatic recovery after fault detection
691369Sdduvall  *
701369Sdduvall  *	bge_mlcr_default
711369Sdduvall  *		Value to program into the MLCR; controls the chip's GPIO pins
721369Sdduvall  *
731369Sdduvall  *	bge_dma_{rd,wr}prio
741369Sdduvall  *		Relative priorities of DMA reads & DMA writes respectively.
751369Sdduvall  *		These may each be patched to any value 0-3.  Equal values
761369Sdduvall  *		will give "fair" (round-robin) arbitration for PCI access.
771369Sdduvall  *		Unequal values will give one or the other function priority.
781369Sdduvall  *
791369Sdduvall  *	bge_dma_rwctrl
801369Sdduvall  *		Value to put in the Read/Write DMA control register.  See
811369Sdduvall  *	        the Broadcom PRM for things you can fiddle with in this
821369Sdduvall  *		register ...
831369Sdduvall  *
841369Sdduvall  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
851369Sdduvall  *		Send/receive interrupt coalescing parameters.  Counts are
861369Sdduvall  *		#s of descriptors, ticks are in microseconds.  *norm* values
871369Sdduvall  *		apply between status updates/interrupts; the *intr* values
881369Sdduvall  *		refer to the 'during-interrupt' versions - see the PRM.
891369Sdduvall  *
901369Sdduvall  *		NOTE: these values have been determined by measurement. They
911369Sdduvall  *		differ significantly from the values recommended in the PRM.
921369Sdduvall  */
931369Sdduvall static uint32_t bge_autorecover = 1;
941369Sdduvall static uint32_t bge_mlcr_default = MLCR_DEFAULT;
951369Sdduvall static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
961369Sdduvall 
971369Sdduvall static uint32_t bge_dma_rdprio = 1;
981369Sdduvall static uint32_t bge_dma_wrprio = 0;
991369Sdduvall static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
1001369Sdduvall static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
1011369Sdduvall static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
1021369Sdduvall static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
1031369Sdduvall 
1041369Sdduvall uint32_t bge_rx_ticks_norm = 128;
1051369Sdduvall uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
1061369Sdduvall uint32_t bge_rx_count_norm = 8;
1071369Sdduvall uint32_t bge_tx_count_norm = 128;
1081369Sdduvall 
1091369Sdduvall static uint32_t bge_rx_ticks_intr = 128;
1101369Sdduvall static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
1111369Sdduvall static uint32_t bge_rx_count_intr = 2;
1121369Sdduvall static uint32_t bge_tx_count_intr = 0;
1131369Sdduvall 
1141369Sdduvall /*
1151369Sdduvall  * Memory pool configuration parameters.
1161369Sdduvall  *
1171369Sdduvall  * These are generally specific to each member of the chip family, since
1181369Sdduvall  * each one may have a different memory size/configuration.
1191369Sdduvall  *
1201369Sdduvall  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
1211369Sdduvall  * the driver from programming the various registers; instead they are left
1221369Sdduvall  * at their hardware defaults.  This is the preferred option for later chips
1231369Sdduvall  * (5705+), whereas the older chips *required* these registers to be set,
1241369Sdduvall  * since the h/w default was 0 ;-(
1251369Sdduvall  */
1261369Sdduvall static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
1271369Sdduvall static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
1281369Sdduvall static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
1291369Sdduvall static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
1301369Sdduvall static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
1311369Sdduvall static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
1321369Sdduvall static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
1331369Sdduvall static uint32_t bge_mbuf_pool_len_5721	= 0;
1341369Sdduvall 
1351369Sdduvall /*
1361369Sdduvall  * Various high and low water marks, thresholds, etc ...
1371369Sdduvall  *
1381369Sdduvall  * Note: these are taken from revision 7 of the PRM, and some are different
1391369Sdduvall  * from both the values in earlier PRMs *and* those determined experimentally
1401369Sdduvall  * and used in earlier versions of this driver ...
1411369Sdduvall  */
1421369Sdduvall static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
1431369Sdduvall static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
1441369Sdduvall static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
1451369Sdduvall 
1461369Sdduvall static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
1471369Sdduvall static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
1481369Sdduvall static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
1491369Sdduvall 
1501369Sdduvall static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
1511369Sdduvall static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
1521369Sdduvall static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
1531369Sdduvall 
1541369Sdduvall static uint32_t	bge_watchdog_count	= 1 << 16;
1551369Sdduvall static uint16_t bge_dma_miss_limit	= 20;
1561369Sdduvall 
1571369Sdduvall static uint32_t bge_stop_start_on_sync	= 0;
1581369Sdduvall 
1591369Sdduvall boolean_t bge_jumbo_enable		= B_TRUE;
1601369Sdduvall static uint32_t bge_default_jumbo_size	= BGE_JUMBO_BUFF_SIZE;
1611369Sdduvall 
1621369Sdduvall /*
1633918Sml149210  * bge_intr_max_loop controls the maximum loop number within bge_intr.
1643918Sml149210  * When loading NIC with heavy network traffic, it is useful.
1653918Sml149210  * Increasing this value could have positive effect to throughput,
1663918Sml149210  * but it might also increase ticks of a bge ISR stick on CPU, which might
1673918Sml149210  * lead to bad UI interactive experience. So tune this with caution.
1683918Sml149210  */
1693918Sml149210 static int bge_intr_max_loop = 1;
1703918Sml149210 
1713918Sml149210 /*
1721369Sdduvall  * ========== Low-level chip & ring buffer manipulation ==========
1731369Sdduvall  */
1741369Sdduvall 
1751369Sdduvall #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
1761369Sdduvall 
1771369Sdduvall 
1781369Sdduvall /*
1791369Sdduvall  * Config space read-modify-write routines
1801369Sdduvall  */
1811369Sdduvall 
1821369Sdduvall #if	BGE_CFG_IO8
1831369Sdduvall 
1841369Sdduvall /*
1851369Sdduvall  * 8- and 16-bit set/clr operations are not used; all the config registers
1861369Sdduvall  * that we need to do bit-twiddling on are 32 bits wide.  I'll leave the
1871369Sdduvall  * code here, though, in case we ever find that we do want it after all ...
1881369Sdduvall  */
1891369Sdduvall 
1901369Sdduvall static void bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1911369Sdduvall #pragma	inline(bge_cfg_set8)
1921369Sdduvall 
1931369Sdduvall static void
1941369Sdduvall bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
1951369Sdduvall {
1961369Sdduvall 	uint8_t regval;
1971369Sdduvall 
1981369Sdduvall 	BGE_TRACE(("bge_cfg_set8($%p, 0x%lx, 0x%x)",
1994588Sml149210 	    (void *)bgep, regno, bits));
2001369Sdduvall 
2011369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
2021369Sdduvall 
2031369Sdduvall 	BGE_DEBUG(("bge_cfg_set8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2044588Sml149210 	    (void *)bgep, regno, bits, regval, regval | bits));
2051369Sdduvall 
2061369Sdduvall 	regval |= bits;
2071369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
2081369Sdduvall }
2091369Sdduvall 
2101369Sdduvall static void bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
2111369Sdduvall #pragma	inline(bge_cfg_clr8)
2121369Sdduvall 
2131369Sdduvall static void
2141369Sdduvall bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
2151369Sdduvall {
2161369Sdduvall 	uint8_t regval;
2171369Sdduvall 
2181369Sdduvall 	BGE_TRACE(("bge_cfg_clr8($%p, 0x%lx, 0x%x)",
2194588Sml149210 	    (void *)bgep, regno, bits));
2201369Sdduvall 
2211369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
2221369Sdduvall 
2231369Sdduvall 	BGE_DEBUG(("bge_cfg_clr8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2244588Sml149210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
2251369Sdduvall 
2261369Sdduvall 	regval &= ~bits;
2271369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
2281369Sdduvall }
2291369Sdduvall 
2301369Sdduvall static void bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2311369Sdduvall #pragma	inline(bge_cfg_set16)
2321369Sdduvall 
2331369Sdduvall static void
2341369Sdduvall bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2351369Sdduvall {
2361369Sdduvall 	uint16_t regval;
2371369Sdduvall 
2381369Sdduvall 	BGE_TRACE(("bge_cfg_set16($%p, 0x%lx, 0x%x)",
2394588Sml149210 	    (void *)bgep, regno, bits));
2401369Sdduvall 
2411369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2421369Sdduvall 
2431369Sdduvall 	BGE_DEBUG(("bge_cfg_set16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2444588Sml149210 	    (void *)bgep, regno, bits, regval, regval | bits));
2451369Sdduvall 
2461369Sdduvall 	regval |= bits;
2471369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2481369Sdduvall }
2491369Sdduvall 
2501369Sdduvall static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2511369Sdduvall #pragma	inline(bge_cfg_clr16)
2521369Sdduvall 
2531369Sdduvall static void
2541369Sdduvall bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2551369Sdduvall {
2561369Sdduvall 	uint16_t regval;
2571369Sdduvall 
2581369Sdduvall 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
2594588Sml149210 	    (void *)bgep, regno, bits));
2601369Sdduvall 
2611369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2621369Sdduvall 
2631369Sdduvall 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2644588Sml149210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
2651369Sdduvall 
2661369Sdduvall 	regval &= ~bits;
2671369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2681369Sdduvall }
2691369Sdduvall 
2701369Sdduvall #endif	/* BGE_CFG_IO8 */
2711369Sdduvall 
2721369Sdduvall static void bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2731369Sdduvall #pragma	inline(bge_cfg_set32)
2741369Sdduvall 
2751369Sdduvall static void
2761369Sdduvall bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2771369Sdduvall {
2781369Sdduvall 	uint32_t regval;
2791369Sdduvall 
2801369Sdduvall 	BGE_TRACE(("bge_cfg_set32($%p, 0x%lx, 0x%x)",
2814588Sml149210 	    (void *)bgep, regno, bits));
2821369Sdduvall 
2831369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2841369Sdduvall 
2851369Sdduvall 	BGE_DEBUG(("bge_cfg_set32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2864588Sml149210 	    (void *)bgep, regno, bits, regval, regval | bits));
2871369Sdduvall 
2881369Sdduvall 	regval |= bits;
2891369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2901369Sdduvall }
2911369Sdduvall 
2921369Sdduvall static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2931369Sdduvall #pragma	inline(bge_cfg_clr32)
2941369Sdduvall 
2951369Sdduvall static void
2961369Sdduvall bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2971369Sdduvall {
2981369Sdduvall 	uint32_t regval;
2991369Sdduvall 
3001369Sdduvall 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
3014588Sml149210 	    (void *)bgep, regno, bits));
3021369Sdduvall 
3031369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
3041369Sdduvall 
3051369Sdduvall 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
3064588Sml149210 	    (void *)bgep, regno, bits, regval, regval & ~bits));
3071369Sdduvall 
3081369Sdduvall 	regval &= ~bits;
3091369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
3101369Sdduvall }
3111369Sdduvall 
3121369Sdduvall #if	BGE_IND_IO32
3131369Sdduvall 
3141369Sdduvall /*
3151369Sdduvall  * Indirect access to registers & RISC scratchpads, using config space
3161369Sdduvall  * accesses only.
3171369Sdduvall  *
3181369Sdduvall  * This isn't currently used, but someday we might want to use it for
3191369Sdduvall  * restoring the Subsystem Device/Vendor registers (which aren't directly
3201369Sdduvall  * writable in Config Space), or for downloading firmware into the RISCs
3211369Sdduvall  *
3221369Sdduvall  * In any case there are endian issues to be resolved before this code is
3231369Sdduvall  * enabled; the bizarre way that bytes get twisted by this chip AND by
3241369Sdduvall  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
3251369Sdduvall  * it's been thoroughly tested for all access sizes on all supported
3261369Sdduvall  * architectures (SPARC *and* x86!).
3271369Sdduvall  */
3283918Sml149210 uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
3291369Sdduvall #pragma	inline(bge_ind_get32)
3301369Sdduvall 
3313918Sml149210 uint32_t
3321369Sdduvall bge_ind_get32(bge_t *bgep, bge_regno_t regno)
3331369Sdduvall {
3341369Sdduvall 	uint32_t val;
3351369Sdduvall 
3361369Sdduvall 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
3371369Sdduvall 
3381369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3391369Sdduvall 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
3401369Sdduvall 
3411369Sdduvall 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
3424588Sml149210 	    (void *)bgep, regno, val));
3431369Sdduvall 
3443918Sml149210 	val = LE_32(val);
3453918Sml149210 
3461369Sdduvall 	return (val);
3471369Sdduvall }
3481369Sdduvall 
3493918Sml149210 void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
3501369Sdduvall #pragma	inline(bge_ind_put32)
3511369Sdduvall 
3523918Sml149210 void
3531369Sdduvall bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
3541369Sdduvall {
3551369Sdduvall 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
3564588Sml149210 	    (void *)bgep, regno, val));
3571369Sdduvall 
3583918Sml149210 	val = LE_32(val);
3591369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3601369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
3611369Sdduvall }
3621369Sdduvall 
3631369Sdduvall #endif	/* BGE_IND_IO32 */
3641369Sdduvall 
3651369Sdduvall #if	BGE_DEBUGGING
3661369Sdduvall 
3671369Sdduvall static void bge_pci_check(bge_t *bgep);
3681369Sdduvall #pragma	no_inline(bge_pci_check)
3691369Sdduvall 
3701369Sdduvall static void
3711369Sdduvall bge_pci_check(bge_t *bgep)
3721369Sdduvall {
3731369Sdduvall 	uint16_t pcistatus;
3741369Sdduvall 
3751369Sdduvall 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
3761369Sdduvall 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
3771369Sdduvall 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
3784588Sml149210 		    (void *)bgep, pcistatus));
3791369Sdduvall }
3801369Sdduvall 
3811369Sdduvall #endif	/* BGE_DEBUGGING */
3821369Sdduvall 
3831369Sdduvall /*
3841369Sdduvall  * Perform first-stage chip (re-)initialisation, using only config-space
3851369Sdduvall  * accesses:
3861369Sdduvall  *
3871369Sdduvall  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
3881369Sdduvall  *   returning the data in the structure pointed to by <idp>.
3891369Sdduvall  * + Configure the target-mode endianness (swap) options.
3901369Sdduvall  * + Disable interrupts and enable Memory Space accesses.
3911369Sdduvall  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
3921369Sdduvall  *
3931369Sdduvall  * This sequence is adapted from Broadcom document 570X-PG102-R,
3941369Sdduvall  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
3951369Sdduvall  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
3961369Sdduvall  *
3971369Sdduvall  * This function MUST be called before any non-config-space accesses
3981369Sdduvall  * are made; on this first call <enable_dma> is B_FALSE, and it
3991369Sdduvall  * effectively performs steps 3-1(!) of the initialisation sequence
4001369Sdduvall  * (the rest are not required but should be harmless).
4011369Sdduvall  *
4022135Szh199473  * It MUST also be called after a chip reset, as this disables
4031369Sdduvall  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
4041369Sdduvall  * it is effectively performing steps 6-8.
4051369Sdduvall  */
4061369Sdduvall void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
4071369Sdduvall #pragma	no_inline(bge_chip_cfg_init)
4081369Sdduvall 
4091369Sdduvall void
4101369Sdduvall bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
4111369Sdduvall {
4121369Sdduvall 	ddi_acc_handle_t handle;
4131369Sdduvall 	uint16_t command;
4141369Sdduvall 	uint32_t mhcr;
4151369Sdduvall 	uint16_t value16;
4161369Sdduvall 	int i;
4171369Sdduvall 
4181369Sdduvall 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
4194588Sml149210 	    (void *)bgep, (void *)cidp, enable_dma));
4201369Sdduvall 
4211369Sdduvall 	/*
4221369Sdduvall 	 * Step 3: save PCI cache line size and subsystem vendor ID
4231369Sdduvall 	 *
4241369Sdduvall 	 * Read all the config-space registers that characterise the
4251369Sdduvall 	 * chip, specifically vendor/device/revision/subsystem vendor
4261369Sdduvall 	 * and subsystem device id.  We expect (but don't check) that
4271369Sdduvall 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
4281369Sdduvall 	 *
4292135Szh199473 	 * Also save all bus-transaction related registers (cache-line
4301369Sdduvall 	 * size, bus-grant/latency parameters, etc).  Some of these are
4311369Sdduvall 	 * cleared by reset, so we'll have to restore them later.  This
4321369Sdduvall 	 * comes from the Broadcom document 570X-PG102-R ...
4331369Sdduvall 	 *
4341369Sdduvall 	 * Note: Broadcom document 570X-PG102-R seems to be in error
4351369Sdduvall 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
4361369Sdduvall 	 * Subsystem (Device) ID registers, which are the opposite way
4371369Sdduvall 	 * round according to the PCI standard.  For good measure, we
4381369Sdduvall 	 * save/restore both anyway.
4391369Sdduvall 	 */
4401369Sdduvall 	handle = bgep->cfg_handle;
4411369Sdduvall 
4421369Sdduvall 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
4431369Sdduvall 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
4441369Sdduvall 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
4451369Sdduvall 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
4461369Sdduvall 
4471369Sdduvall 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
4481369Sdduvall 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
4491369Sdduvall 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
4501369Sdduvall 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
4511369Sdduvall 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
4521369Sdduvall 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
4531369Sdduvall 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
4541369Sdduvall 
4551369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
4564588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
4574588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
4584588Sml149210 	    cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
4594588Sml149210 	    cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
4601369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
4614588Sml149210 	    cidp->vendor, cidp->device, cidp->revision));
4621369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
4634588Sml149210 	    cidp->subven, cidp->subdev, cidp->asic_rev));
4641369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
4654588Sml149210 	    cidp->clsize, cidp->latency, cidp->command));
4661369Sdduvall 
4671369Sdduvall 	/*
4681369Sdduvall 	 * Step 2 (also step 6): disable and clear interrupts.
4691369Sdduvall 	 * Steps 11-13: configure PIO endianness options, and enable
4701369Sdduvall 	 * indirect register access.  We'll also select any other
4712135Szh199473 	 * options controlled by the MHCR (e.g. tagged status, mask
4721369Sdduvall 	 * interrupt mode) at this stage ...
4731369Sdduvall 	 *
4741369Sdduvall 	 * Note: internally, the chip is 64-bit and BIG-endian, but
4751369Sdduvall 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
4761369Sdduvall 	 * it normally swaps bytes around at the PCI interface.
4771369Sdduvall 	 * However, the PCI host bridge on SPARC systems normally
4781369Sdduvall 	 * swaps the byte lanes around too, since SPARCs are also
4791369Sdduvall 	 * BIG-endian.  So it turns out that on SPARC, the right
4801369Sdduvall 	 * option is to tell the chip to swap (and the host bridge
4811369Sdduvall 	 * will swap back again), whereas on x86 we ask the chip
4821369Sdduvall 	 * NOT to swap, so the natural little-endianness of the
4831369Sdduvall 	 * PCI bus is assumed.  Then the only thing that doesn't
4841369Sdduvall 	 * automatically work right is access to an 8-byte register
4851369Sdduvall 	 * by a little-endian host; but we don't want to set the
4861369Sdduvall 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
4871369Sdduvall 	 * accesses don't go where expected ;-(  So we live with
4881369Sdduvall 	 * that, and perform word-swaps in software in the few cases
4891369Sdduvall 	 * where a chip register is defined as an 8-byte value --
4901369Sdduvall 	 * see the code below for details ...
4911369Sdduvall 	 *
4921369Sdduvall 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
4931369Sdduvall 	 * very clear in the register description in the PRM, but
4941369Sdduvall 	 * Broadcom document 570X-PG104-R page 248 explains a little
4951369Sdduvall 	 * more (under "Broadcom Mask Mode").  The bit changes the way
4961369Sdduvall 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
4971369Sdduvall 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
4981369Sdduvall 	 * way as the 5700 did, which isn't very convenient.  Setting
4991369Sdduvall 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
5001369Sdduvall 	 * bit do just what its name says -- MASK the PCI #INTA output
5011369Sdduvall 	 * (i.e. deassert the signal at the pin) leaving all internal
5021369Sdduvall 	 * state unchanged.  This is much more convenient for our
5031369Sdduvall 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
5041369Sdduvall 	 *
5051369Sdduvall 	 * Note: the inconvenient semantics of the interrupt mailbox
5061369Sdduvall 	 * (nonzero disables and acknowledges/clears the interrupt,
5071369Sdduvall 	 * zero enables AND CLEARS it) would make race conditions
5081369Sdduvall 	 * likely in the interrupt handler:
5091369Sdduvall 	 *
5101369Sdduvall 	 * (1)	acknowledge & disable interrupts
5111369Sdduvall 	 * (2)	while (more to do)
5121369Sdduvall 	 * 		process packets
5131369Sdduvall 	 * (3)	enable interrupts -- also clears pending
5141369Sdduvall 	 *
5151369Sdduvall 	 * If the chip received more packets and internally generated
5161369Sdduvall 	 * an interrupt between the check at (2) and the mbox write
5171369Sdduvall 	 * at (3), this interrupt would be lost :-(
5181369Sdduvall 	 *
5191369Sdduvall 	 * The best way to avoid this is to use TAGGED STATUS mode,
5201369Sdduvall 	 * where the chip includes a unique tag in each status block
5211369Sdduvall 	 * update, and the host, when re-enabling interrupts, passes
5221369Sdduvall 	 * the last tag it saw back to the chip; then the chip can
5231369Sdduvall 	 * see whether the host is truly up to date, and regenerate
5241369Sdduvall 	 * its interrupt if not.
5251369Sdduvall 	 */
5261369Sdduvall 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
5274588Sml149210 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
5284588Sml149210 	    MHCR_MASK_INTERRUPT_MODE |
5294588Sml149210 	    MHCR_CLEAR_INTERRUPT_INTA;
5301369Sdduvall 
5311369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
5321369Sdduvall 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
5331369Sdduvall 
5341369Sdduvall #ifdef	_BIG_ENDIAN
5351369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
5361369Sdduvall #endif	/* _BIG_ENDIAN */
5371369Sdduvall 
5381369Sdduvall 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
5391369Sdduvall 
5401408Srandyf #ifdef BGE_IPMI_ASF
5411408Srandyf 	bgep->asf_wordswapped = B_FALSE;
5421408Srandyf #endif
5431369Sdduvall 	/*
5441369Sdduvall 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
5451369Sdduvall 	 *			 Disable Memory Write/Invalidate
5461369Sdduvall 	 *			 Enable or disable Bus Mastering
5471369Sdduvall 	 *
5481369Sdduvall 	 * Note that all other bits are taken from the original value saved
5491369Sdduvall 	 * the first time through here, rather than from the current register
5501369Sdduvall 	 * value, 'cos that will have been cleared by a soft RESET since.
5511369Sdduvall 	 * In this way we preserve the OBP/nexus-parent's preferred settings
5521369Sdduvall 	 * of the parity-error and system-error enable bits across multiple
5531369Sdduvall 	 * chip RESETs.
5541369Sdduvall 	 */
5551369Sdduvall 	command = bgep->chipid.command | PCI_COMM_MAE;
5561369Sdduvall 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
5571369Sdduvall 	if (enable_dma)
5581369Sdduvall 		command |= PCI_COMM_ME;
5591369Sdduvall 	/*
5601369Sdduvall 	 * on BCM5714 revision A0, false parity error gets generated
5612135Szh199473 	 * due to a logic bug. Provide a workaround by disabling parity
5621369Sdduvall 	 * error.
5631369Sdduvall 	 */
5641369Sdduvall 	if (((cidp->device == DEVICE_ID_5714C) ||
5651369Sdduvall 	    (cidp->device == DEVICE_ID_5714S)) &&
5661369Sdduvall 	    (cidp->revision == REVISION_ID_5714_A0)) {
5671369Sdduvall 		command &= ~PCI_COMM_PARITY_DETECT;
5681369Sdduvall 	}
5691369Sdduvall 	pci_config_put16(handle, PCI_CONF_COMM, command);
5701369Sdduvall 
5711369Sdduvall 	/*
5721369Sdduvall 	 * On some PCI-E device, there were instances when
5731369Sdduvall 	 * the device was still link training.
5741369Sdduvall 	 */
5751369Sdduvall 	if (bgep->chipid.pci_type == BGE_PCI_E) {
5761369Sdduvall 		i = 0;
5771369Sdduvall 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
5781369Sdduvall 		while ((value16 != command) && (i < 100)) {
5791369Sdduvall 			drv_usecwait(200);
5801369Sdduvall 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
5811369Sdduvall 			++i;
5821369Sdduvall 		}
5831369Sdduvall 	}
5841369Sdduvall 
5851369Sdduvall 	/*
5861369Sdduvall 	 * Clear any remaining error status bits
5871369Sdduvall 	 */
5881369Sdduvall 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
5891369Sdduvall 
5901369Sdduvall 	/*
5912073Svivek 	 * Do following if and only if the device is NOT BCM5714C OR
5922073Svivek 	 * BCM5715C
5931369Sdduvall 	 */
5942073Svivek 	if (!((cidp->device == DEVICE_ID_5714C) ||
5954588Sml149210 	    (cidp->device == DEVICE_ID_5715C))) {
5962073Svivek 		/*
5972073Svivek 		 * Make sure these indirect-access registers are sane
5982073Svivek 		 * rather than random after power-up or reset
5992073Svivek 		 */
6002073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
6012073Svivek 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
6022073Svivek 	}
6032135Szh199473 	/*
6042135Szh199473 	 * Step 8: Disable PCI-X/PCI-E Relaxed Ordering
6052135Szh199473 	 */
6062135Szh199473 	bge_cfg_clr16(bgep, PCIX_CONF_COMM, PCIX_COMM_RELAXED);
6072135Szh199473 
6082135Szh199473 	if (cidp->pci_type == BGE_PCI_E)
6092135Szh199473 		bge_cfg_clr16(bgep, PCI_CONF_DEV_CTRL,
6104588Sml149210 		    DEV_CTRL_NO_SNOOP | DEV_CTRL_RELAXED);
6111369Sdduvall }
6121369Sdduvall 
6131369Sdduvall #ifdef __amd64
6141369Sdduvall /*
6151369Sdduvall  * Distinguish CPU types
6161369Sdduvall  *
6171369Sdduvall  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
6181369Sdduvall  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
6191369Sdduvall  * for PCI-Express based network interface card. This is the work-around
6201369Sdduvall  * for those nics.
6211369Sdduvall  */
6221369Sdduvall static boolean_t bge_get_em64t_type(void);
6231369Sdduvall #pragma	inline(bge_get_em64t_type)
6241369Sdduvall 
6251369Sdduvall static boolean_t
6261369Sdduvall bge_get_em64t_type(void)
6271369Sdduvall {
6281369Sdduvall 
6291369Sdduvall 	return (x86_vendor == X86_VENDOR_Intel);
6301369Sdduvall }
6311369Sdduvall #endif
6321369Sdduvall 
6331369Sdduvall /*
6341369Sdduvall  * Operating register get/set access routines
6351369Sdduvall  */
6361369Sdduvall 
6371369Sdduvall uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
6381369Sdduvall #pragma	inline(bge_reg_get32)
6391369Sdduvall 
6401369Sdduvall uint32_t
6411369Sdduvall bge_reg_get32(bge_t *bgep, bge_regno_t regno)
6421369Sdduvall {
6431369Sdduvall 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
6444588Sml149210 	    (void *)bgep, regno));
6451369Sdduvall 
6461369Sdduvall 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
6471369Sdduvall }
6481369Sdduvall 
6491369Sdduvall void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
6501369Sdduvall #pragma	inline(bge_reg_put32)
6511369Sdduvall 
6521369Sdduvall void
6531369Sdduvall bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
6541369Sdduvall {
6551369Sdduvall 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
6564588Sml149210 	    (void *)bgep, regno, data));
6571369Sdduvall 
6581369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6591369Sdduvall 	BGE_PCICHK(bgep);
6601369Sdduvall }
6611369Sdduvall 
6621369Sdduvall void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6631369Sdduvall #pragma	inline(bge_reg_set32)
6641369Sdduvall 
6651369Sdduvall void
6661369Sdduvall bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6671369Sdduvall {
6681369Sdduvall 	uint32_t regval;
6691369Sdduvall 
6701369Sdduvall 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
6714588Sml149210 	    (void *)bgep, regno, bits));
6721369Sdduvall 
6731369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6741369Sdduvall 	regval |= bits;
6751369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6761369Sdduvall }
6771369Sdduvall 
6781369Sdduvall void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6791369Sdduvall #pragma	inline(bge_reg_clr32)
6801369Sdduvall 
6811369Sdduvall void
6821369Sdduvall bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6831369Sdduvall {
6841369Sdduvall 	uint32_t regval;
6851369Sdduvall 
6861369Sdduvall 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
6874588Sml149210 	    (void *)bgep, regno, bits));
6881369Sdduvall 
6891369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6901369Sdduvall 	regval &= ~bits;
6911369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6921369Sdduvall }
6931369Sdduvall 
6941369Sdduvall static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
6951369Sdduvall #pragma	inline(bge_reg_get64)
6961369Sdduvall 
6971369Sdduvall static uint64_t
6981369Sdduvall bge_reg_get64(bge_t *bgep, bge_regno_t regno)
6991369Sdduvall {
7001369Sdduvall 	uint64_t regval;
7011369Sdduvall 
7021369Sdduvall #ifdef	__amd64
7031369Sdduvall 	if (bge_get_em64t_type()) {
7041369Sdduvall 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
7051369Sdduvall 		regval <<= 32;
7061369Sdduvall 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
7071369Sdduvall 	} else {
7081369Sdduvall 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
7091369Sdduvall 	}
7101369Sdduvall #else
7111369Sdduvall 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
7121369Sdduvall #endif
7131369Sdduvall 
7141369Sdduvall #ifdef	_LITTLE_ENDIAN
7151369Sdduvall 	regval = (regval >> 32) | (regval << 32);
7161369Sdduvall #endif	/* _LITTLE_ENDIAN */
7171369Sdduvall 
7181369Sdduvall 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
7194588Sml149210 	    (void *)bgep, regno, regval));
7201369Sdduvall 
7211369Sdduvall 	return (regval);
7221369Sdduvall }
7231369Sdduvall 
7241369Sdduvall static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
7251369Sdduvall #pragma	inline(bge_reg_put64)
7261369Sdduvall 
7271369Sdduvall static void
7281369Sdduvall bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
7291369Sdduvall {
7301369Sdduvall 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
7314588Sml149210 	    (void *)bgep, regno, data));
7321369Sdduvall 
7331369Sdduvall #ifdef	_LITTLE_ENDIAN
7341369Sdduvall 	data = ((data >> 32) | (data << 32));
7351369Sdduvall #endif	/* _LITTLE_ENDIAN */
7361369Sdduvall 
7371369Sdduvall #ifdef	__amd64
7381369Sdduvall 	if (bge_get_em64t_type()) {
7391369Sdduvall 		ddi_put32(bgep->io_handle,
7404588Sml149210 		    PIO_ADDR(bgep, regno), (uint32_t)data);
7411369Sdduvall 		BGE_PCICHK(bgep);
7421369Sdduvall 		ddi_put32(bgep->io_handle,
7434588Sml149210 		    PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
7441369Sdduvall 
7451369Sdduvall 	} else {
7461369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7471369Sdduvall 	}
7481369Sdduvall #else
7491369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7501369Sdduvall #endif
7511369Sdduvall 
7521369Sdduvall 	BGE_PCICHK(bgep);
7531369Sdduvall }
7541369Sdduvall 
7551369Sdduvall /*
7561369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
7571369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
7581369Sdduvall  */
7591369Sdduvall static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
7601369Sdduvall #pragma	inline(bge_reg_putrcb)
7611369Sdduvall 
7621369Sdduvall static void
7631369Sdduvall bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
7641369Sdduvall {
7651369Sdduvall 	uint64_t *p;
7661369Sdduvall 
7671369Sdduvall 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
7684588Sml149210 	    (void *)bgep, addr, rcbp->host_ring_addr,
7694588Sml149210 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
7701369Sdduvall 
7711369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
7721369Sdduvall 
7731369Sdduvall 	p = (void *)rcbp;
7741369Sdduvall 	bge_reg_put64(bgep, addr, *p++);
7751369Sdduvall 	bge_reg_put64(bgep, addr+8, *p);
7761369Sdduvall }
7771369Sdduvall 
7781369Sdduvall void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
7791369Sdduvall #pragma	inline(bge_mbx_put)
7801369Sdduvall 
7811369Sdduvall void
7821369Sdduvall bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
7831369Sdduvall {
7841369Sdduvall 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
7854588Sml149210 	    (void *)bgep, regno, data));
7861369Sdduvall 
7871369Sdduvall 	/*
7881369Sdduvall 	 * Mailbox registers are nominally 64 bits on the 5701, but
7891369Sdduvall 	 * the MSW isn't used.  On the 5703, they're only 32 bits
7901369Sdduvall 	 * anyway.  So here we just write the lower(!) 32 bits -
7911369Sdduvall 	 * remembering that the chip is big-endian, even though the
7921369Sdduvall 	 * PCI bus is little-endian ...
7931369Sdduvall 	 */
7941369Sdduvall #ifdef	_BIG_ENDIAN
7951369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
7961369Sdduvall #else
7971369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
7981369Sdduvall #endif	/* _BIG_ENDIAN */
7991369Sdduvall 	BGE_PCICHK(bgep);
8001369Sdduvall }
8011369Sdduvall 
802*6546Sgh162552 uint32_t bge_mbx_get(bge_t *bgep, bge_regno_t regno);
803*6546Sgh162552 #pragma inline(bge_mbx_get)
804*6546Sgh162552 
805*6546Sgh162552 uint32_t
806*6546Sgh162552 bge_mbx_get(bge_t *bgep, bge_regno_t regno)
807*6546Sgh162552 {
808*6546Sgh162552 	uint32_t val32;
809*6546Sgh162552 
810*6546Sgh162552 	BGE_TRACE(("bge_mbx_get($%p, 0x%lx)",
811*6546Sgh162552 	    (void *)bgep, regno));
812*6546Sgh162552 
813*6546Sgh162552 #ifdef	_BIG_ENDIAN
814*6546Sgh162552 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno+4));
815*6546Sgh162552 #else
816*6546Sgh162552 	val32 = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
817*6546Sgh162552 #endif	/* _BIG_ENDIAN */
818*6546Sgh162552 	BGE_PCICHK(bgep);
819*6546Sgh162552 
820*6546Sgh162552 	return (val32);
821*6546Sgh162552 }
822*6546Sgh162552 
823*6546Sgh162552 
8241369Sdduvall #if	BGE_DEBUGGING
8251369Sdduvall 
8261369Sdduvall void bge_led_mark(bge_t *bgep);
8271369Sdduvall #pragma	no_inline(bge_led_mark)
8281369Sdduvall 
8291369Sdduvall void
8301369Sdduvall bge_led_mark(bge_t *bgep)
8311369Sdduvall {
8321369Sdduvall 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
8334588Sml149210 	    LED_CONTROL_1000MBPS_LED |
8344588Sml149210 	    LED_CONTROL_100MBPS_LED |
8354588Sml149210 	    LED_CONTROL_10MBPS_LED;
8361369Sdduvall 
8371369Sdduvall 	/*
8381369Sdduvall 	 * Blink all three LINK LEDs on simultaneously, then all off,
8391369Sdduvall 	 * then restore to automatic hardware control.  This is used
8401369Sdduvall 	 * in laboratory testing to trigger a logic analyser or scope.
8411369Sdduvall 	 */
8421369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8431369Sdduvall 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
8441369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8451369Sdduvall 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
8461369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8471369Sdduvall }
8481369Sdduvall 
8491369Sdduvall #endif	/* BGE_DEBUGGING */
8501369Sdduvall 
8511369Sdduvall /*
8521369Sdduvall  * NIC on-chip memory access routines
8531369Sdduvall  *
8541369Sdduvall  * Only 32K of NIC memory is visible at a time, controlled by the
8551369Sdduvall  * Memory Window Base Address Register (in PCI config space).  Once
8561369Sdduvall  * this is set, the 32K region of NIC-local memory that it refers
8571369Sdduvall  * to can be directly addressed in the upper 32K of the 64K of PCI
8581369Sdduvall  * memory space used for the device.
8591369Sdduvall  */
8601369Sdduvall 
8611369Sdduvall static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
8621369Sdduvall #pragma	inline(bge_nic_setwin)
8631369Sdduvall 
8641369Sdduvall static void
8651369Sdduvall bge_nic_setwin(bge_t *bgep, bge_regno_t base)
8661369Sdduvall {
8672073Svivek 	chip_id_t *cidp;
8682073Svivek 
8691369Sdduvall 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
8704588Sml149210 	    (void *)bgep, base));
8711369Sdduvall 
8721369Sdduvall 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
8732073Svivek 
8742073Svivek 	/*
8752073Svivek 	 * Don't do repeated zero data writes,
8762073Svivek 	 * if the device is BCM5714C/15C.
8772073Svivek 	 */
8782073Svivek 	cidp = &bgep->chipid;
8792073Svivek 	if ((cidp->device == DEVICE_ID_5714C) ||
8804588Sml149210 	    (cidp->device == DEVICE_ID_5715C)) {
8812073Svivek 		if (bgep->lastWriteZeroData && (base == (bge_regno_t)0))
8822073Svivek 			return;
8832073Svivek 		/* Adjust lastWriteZeroData */
8842073Svivek 		bgep->lastWriteZeroData = ((base == (bge_regno_t)0) ?
8854588Sml149210 		    B_TRUE : B_FALSE);
8862073Svivek 	}
8871369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
8881369Sdduvall }
8891369Sdduvall 
8901369Sdduvall static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
8911369Sdduvall #pragma	inline(bge_nic_get32)
8921369Sdduvall 
8931369Sdduvall static uint32_t
8941369Sdduvall bge_nic_get32(bge_t *bgep, bge_regno_t addr)
8951369Sdduvall {
8961369Sdduvall 	uint32_t data;
8971369Sdduvall 
8983918Sml149210 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
8991408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
9001408Srandyf 		/* workaround for word swap error */
9011408Srandyf 		if (addr & 4)
9021408Srandyf 			addr = addr - 4;
9031408Srandyf 		else
9041408Srandyf 			addr = addr + 4;
9051408Srandyf 	}
9061408Srandyf #endif
9071408Srandyf 
9083918Sml149210 #ifdef __sparc
9093918Sml149210 	data = bge_nic_read32(bgep, addr);
9103918Sml149210 #else
9111369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9121369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9131369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9141369Sdduvall 
9151369Sdduvall 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
9163918Sml149210 #endif
9171369Sdduvall 
9181369Sdduvall 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
9194588Sml149210 	    (void *)bgep, addr, data));
9201369Sdduvall 
9211369Sdduvall 	return (data);
9221369Sdduvall }
9231369Sdduvall 
9241408Srandyf void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
9251408Srandyf #pragma inline(bge_nic_put32)
9261408Srandyf 
9271408Srandyf void
9281369Sdduvall bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
9291369Sdduvall {
9301369Sdduvall 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
9314588Sml149210 	    (void *)bgep, addr, data));
9321369Sdduvall 
9333918Sml149210 #if defined(BGE_IPMI_ASF) && !defined(__sparc)
9341408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
9351408Srandyf 		/* workaround for word swap error */
9361408Srandyf 		if (addr & 4)
9371408Srandyf 			addr = addr - 4;
9381408Srandyf 		else
9391408Srandyf 			addr = addr + 4;
9401408Srandyf 	}
9411408Srandyf #endif
9421408Srandyf 
9433918Sml149210 #ifdef __sparc
9443918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
9453918Sml149210 	data = LE_32(data);
9463918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR, data);
9473918Sml149210 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
9483918Sml149210 #else
9491369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9501369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9511369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9521369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9531369Sdduvall 	BGE_PCICHK(bgep);
9543918Sml149210 #endif
9551369Sdduvall }
9561369Sdduvall 
9571369Sdduvall static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
9581369Sdduvall #pragma	inline(bge_nic_get64)
9591369Sdduvall 
9601369Sdduvall static uint64_t
9611369Sdduvall bge_nic_get64(bge_t *bgep, bge_regno_t addr)
9621369Sdduvall {
9631369Sdduvall 	uint64_t data;
9641369Sdduvall 
9651369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9661369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9671369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9681369Sdduvall 
9691369Sdduvall #ifdef	__amd64
9701369Sdduvall 		if (bge_get_em64t_type()) {
9711369Sdduvall 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
9721369Sdduvall 			data <<= 32;
9731369Sdduvall 			data |= ddi_get32(bgep->io_handle,
9744588Sml149210 			    PIO_ADDR(bgep, addr + 4));
9751369Sdduvall 		} else {
9761369Sdduvall 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9771369Sdduvall 		}
9781369Sdduvall #else
9791369Sdduvall 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9801369Sdduvall #endif
9811369Sdduvall 
9821369Sdduvall 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
9834588Sml149210 	    (void *)bgep, addr, data));
9841369Sdduvall 
9851369Sdduvall 	return (data);
9861369Sdduvall }
9871369Sdduvall 
9881369Sdduvall static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
9891369Sdduvall #pragma	inline(bge_nic_put64)
9901369Sdduvall 
9911369Sdduvall static void
9921369Sdduvall bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
9931369Sdduvall {
9941369Sdduvall 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
9954588Sml149210 	    (void *)bgep, addr, data));
9961369Sdduvall 
9971369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9981369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9991369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10001369Sdduvall 
10011369Sdduvall #ifdef	__amd64
10021369Sdduvall 	if (bge_get_em64t_type()) {
10031369Sdduvall 		ddi_put32(bgep->io_handle,
10044588Sml149210 		    PIO_ADDR(bgep, addr), (uint32_t)data);
10051369Sdduvall 		BGE_PCICHK(bgep);
10061369Sdduvall 		ddi_put32(bgep->io_handle,
10074588Sml149210 		    PIO_ADDR(bgep, addr + 4), (uint32_t)(data >> 32));
10081369Sdduvall 	} else {
10091369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
10101369Sdduvall 	}
10111369Sdduvall #else
10121369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
10131369Sdduvall #endif
10141369Sdduvall 
10151369Sdduvall 	BGE_PCICHK(bgep);
10161369Sdduvall }
10171369Sdduvall 
10181369Sdduvall /*
10191369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
10201369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
10211369Sdduvall  */
10221369Sdduvall static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
10231369Sdduvall #pragma	inline(bge_nic_putrcb)
10241369Sdduvall 
10251369Sdduvall static void
10261369Sdduvall bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
10271369Sdduvall {
10281369Sdduvall 	uint64_t *p;
10291369Sdduvall 
10301369Sdduvall 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
10314588Sml149210 	    (void *)bgep, addr, rcbp->host_ring_addr,
10324588Sml149210 	    rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
10331369Sdduvall 
10341369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
10351369Sdduvall 
10361369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10371369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10381369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10391369Sdduvall 
10401369Sdduvall 	p = (void *)rcbp;
10411369Sdduvall #ifdef	__amd64
10421369Sdduvall 	if (bge_get_em64t_type()) {
10431369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
10444588Sml149210 		    (uint32_t)(*p));
10451369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
10464588Sml149210 		    (uint32_t)(*p >> 32));
10471369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
10484588Sml149210 		    (uint32_t)(*(p + 1)));
10491369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
10504588Sml149210 		    (uint32_t)(*p >> 32));
10511369Sdduvall 
10521369Sdduvall 	} else {
10531369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10541369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
10551369Sdduvall 	}
10561369Sdduvall #else
10571369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
10581369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
10591369Sdduvall #endif
10601369Sdduvall 
10611369Sdduvall 	BGE_PCICHK(bgep);
10621369Sdduvall }
10631369Sdduvall 
10641369Sdduvall static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
10651369Sdduvall #pragma	inline(bge_nic_zero)
10661369Sdduvall 
10671369Sdduvall static void
10681369Sdduvall bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
10691369Sdduvall {
10701369Sdduvall 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
10714588Sml149210 	    (void *)bgep, addr, nbytes));
10721369Sdduvall 
10731369Sdduvall 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
10744588Sml149210 	    ((addr+nbytes) & ~MWBAR_GRANULE_MASK));
10751369Sdduvall 
10761369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10771369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10781369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10791369Sdduvall 
10801369Sdduvall 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
10814588Sml149210 	    nbytes, 1, DDI_DATA_SZ08_ACC);
10821369Sdduvall 	BGE_PCICHK(bgep);
10831369Sdduvall }
10841369Sdduvall 
10851369Sdduvall /*
10861369Sdduvall  * MII (PHY) register get/set access routines
10871369Sdduvall  *
10881369Sdduvall  * These use the chip's MII auto-access method, controlled by the
10891369Sdduvall  * MII Communication register at 0x044c, so the CPU doesn't have
10901369Sdduvall  * to fiddle with the individual bits.
10911369Sdduvall  */
10921369Sdduvall 
10931369Sdduvall #undef	BGE_DBG
10941369Sdduvall #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
10951369Sdduvall 
10961369Sdduvall static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
10971369Sdduvall 				uint16_t data, uint32_t cmd);
10981369Sdduvall #pragma	no_inline(bge_mii_access)
10991369Sdduvall 
11001369Sdduvall static uint16_t
11011369Sdduvall bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
11021369Sdduvall {
11031369Sdduvall 	uint32_t timeout;
11041369Sdduvall 	uint32_t regval1;
11051369Sdduvall 	uint32_t regval2;
11061369Sdduvall 
11071369Sdduvall 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
11084588Sml149210 	    (void *)bgep, regno, data, cmd));
11091369Sdduvall 
11101369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11111369Sdduvall 
11121369Sdduvall 	/*
11131369Sdduvall 	 * Assemble the command ...
11141369Sdduvall 	 */
11151369Sdduvall 	cmd |= data << MI_COMMS_DATA_SHIFT;
11161369Sdduvall 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
11171369Sdduvall 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
11181369Sdduvall 	cmd |= MI_COMMS_START;
11191369Sdduvall 
11201369Sdduvall 	/*
11211369Sdduvall 	 * Wait for any command already in progress ...
11221369Sdduvall 	 *
11231369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
11241369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
11251369Sdduvall 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
11261369Sdduvall 	 * bit set here -- it seems that the chip can initiate MII
11271369Sdduvall 	 * accesses internally, even with polling OFF.
11281369Sdduvall 	 */
11291369Sdduvall 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11301865Sdilpreet 	for (timeout = 100; ; ) {
11311369Sdduvall 		if ((regval2 & MI_COMMS_START) == 0) {
11321369Sdduvall 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
11331369Sdduvall 			break;
11341369Sdduvall 		}
11351369Sdduvall 		if (--timeout == 0)
11361369Sdduvall 			break;
11371369Sdduvall 		drv_usecwait(10);
11381369Sdduvall 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11391369Sdduvall 	}
11401369Sdduvall 
11411865Sdilpreet 	if (timeout == 0)
11421865Sdilpreet 		return ((uint16_t)~0u);
11431865Sdilpreet 
11441865Sdilpreet 	if (timeout != 100)
11451369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11464588Sml149210 		    "MI_COMMS_START set for %d us; 0x%x->0x%x",
11474588Sml149210 		    cmd, 10*(100-timeout), regval1, regval2));
11481369Sdduvall 
11491369Sdduvall 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11501369Sdduvall 	for (timeout = 1000; ; ) {
11511369Sdduvall 		if ((regval1 & MI_COMMS_START) == 0)
11521369Sdduvall 			break;
11531369Sdduvall 		if (--timeout == 0)
11541369Sdduvall 			break;
11551369Sdduvall 		drv_usecwait(10);
11561369Sdduvall 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
11571369Sdduvall 	}
11581369Sdduvall 
11591369Sdduvall 	/*
11601369Sdduvall 	 * Drop out early if the READ FAILED bit is set -- this chip
11611369Sdduvall 	 * could be a 5703/4S, with a SerDes instead of a PHY!
11621369Sdduvall 	 */
11631369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11641369Sdduvall 		return ((uint16_t)~0u);
11651369Sdduvall 
11661369Sdduvall 	if (timeout == 0)
11671369Sdduvall 		return ((uint16_t)~0u);
11681369Sdduvall 
11691369Sdduvall 	/*
11701369Sdduvall 	 * The PRM says to wait 5us after seeing the START bit clear
11711369Sdduvall 	 * and then re-read the register to get the final value of the
11721369Sdduvall 	 * data field, in order to avoid a race condition where the
11731369Sdduvall 	 * START bit is clear but the data field isn't yet valid.
11741369Sdduvall 	 *
11751369Sdduvall 	 * Note: we don't actually seem to be encounter this race;
11761369Sdduvall 	 * except when the START bit is seen set again (see below),
11771369Sdduvall 	 * the data field doesn't change during this 5us interval.
11781369Sdduvall 	 */
11791369Sdduvall 	drv_usecwait(5);
11801369Sdduvall 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11811369Sdduvall 
11821369Sdduvall 	/*
11831369Sdduvall 	 * Unfortunately, when following the PRMs instructions above,
11841369Sdduvall 	 * we have occasionally seen the START bit set again(!) in the
11851369Sdduvall 	 * value read after the 5us delay. This seems to be due to the
11861369Sdduvall 	 * chip autonomously starting another MII access internally.
11871369Sdduvall 	 * In such cases, the command/data/etc fields relate to the
11881369Sdduvall 	 * internal command, rather than the one that we thought had
11891369Sdduvall 	 * just finished.  So in this case, we fall back to returning
11901369Sdduvall 	 * the data from the original read that showed START clear.
11911369Sdduvall 	 */
11921369Sdduvall 	if (regval2 & MI_COMMS_START) {
11931369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11944588Sml149210 		    "MI_COMMS_START set after transaction; 0x%x->0x%x",
11954588Sml149210 		    cmd, regval1, regval2));
11961369Sdduvall 		regval2 = regval1;
11971369Sdduvall 	}
11981369Sdduvall 
11991369Sdduvall 	if (regval2 & MI_COMMS_START)
12001369Sdduvall 		return ((uint16_t)~0u);
12011369Sdduvall 
12021369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
12031369Sdduvall 		return ((uint16_t)~0u);
12041369Sdduvall 
12051369Sdduvall 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
12061369Sdduvall }
12071369Sdduvall 
12081369Sdduvall uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
12091369Sdduvall #pragma	no_inline(bge_mii_get16)
12101369Sdduvall 
12111369Sdduvall uint16_t
12121369Sdduvall bge_mii_get16(bge_t *bgep, bge_regno_t regno)
12131369Sdduvall {
12141369Sdduvall 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
12154588Sml149210 	    (void *)bgep, regno));
12161369Sdduvall 
12171369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12181369Sdduvall 
12191369Sdduvall 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
12201369Sdduvall }
12211369Sdduvall 
12221369Sdduvall void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
12231369Sdduvall #pragma	no_inline(bge_mii_put16)
12241369Sdduvall 
12251369Sdduvall void
12261369Sdduvall bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
12271369Sdduvall {
12281369Sdduvall 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
12294588Sml149210 	    (void *)bgep, regno, data));
12301369Sdduvall 
12311369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12321369Sdduvall 
12331369Sdduvall 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
12341369Sdduvall }
12351369Sdduvall 
12361369Sdduvall #undef	BGE_DBG
12371369Sdduvall #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
12381369Sdduvall 
12391369Sdduvall #if	BGE_SEE_IO32 || BGE_FLASH_IO32
12401369Sdduvall 
12411369Sdduvall /*
12421369Sdduvall  * Basic SEEPROM get/set access routine
12431369Sdduvall  *
12441369Sdduvall  * This uses the chip's SEEPROM auto-access method, controlled by the
12451369Sdduvall  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
12461369Sdduvall  * doesn't have to fiddle with the individual bits.
12471369Sdduvall  *
12481369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
12491369Sdduvall  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
12501369Sdduvall  *
12511369Sdduvall  * Return value:
12521369Sdduvall  *	0 on success,
12531369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
12541369Sdduvall  *	EPROTO on other h/w or s/w errors.
12551369Sdduvall  *
12561369Sdduvall  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
12571369Sdduvall  * from a (successful) SEEPROM_ACCESS_READ.
12581369Sdduvall  */
12591369Sdduvall static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
12601369Sdduvall 				uint32_t *dp);
12611369Sdduvall #pragma	no_inline(bge_seeprom_access)
12621369Sdduvall 
12631369Sdduvall static int
12641369Sdduvall bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
12651369Sdduvall {
12661369Sdduvall 	uint32_t tries;
12671369Sdduvall 	uint32_t regval;
12681369Sdduvall 
12691369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12701369Sdduvall 
12711369Sdduvall 	/*
12721369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
12731369Sdduvall 	 * to specifically enable SEEPROM access (Flash is the default).
12741369Sdduvall 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
12751369Sdduvall 	 * and the NVM control registers don't exist ...
12761369Sdduvall 	 */
12771369Sdduvall 	switch (bgep->chipid.nvtype) {
12781369Sdduvall 	case BGE_NVTYPE_NONE:
12791369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
12801369Sdduvall 		_NOTE(NOTREACHED)
12811369Sdduvall 	case BGE_NVTYPE_SEEPROM:
12821369Sdduvall 		break;
12831369Sdduvall 
12841369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
12851369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
12861369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
12871369Sdduvall 	default:
12881369Sdduvall 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
12894588Sml149210 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
12901369Sdduvall 		break;
12911369Sdduvall 	}
12921369Sdduvall 
12931369Sdduvall 	/*
12941369Sdduvall 	 * Check there's no command in progress.
12951369Sdduvall 	 *
12961369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
12971369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
12981369Sdduvall 	 * Also, to ensure we don't have a conflict with the chip's
12991369Sdduvall 	 * internal firmware or a process accessing the same (shared)
13001369Sdduvall 	 * SEEPROM through the other port of a 5704, we've already
13011369Sdduvall 	 * been through the "software arbitration" protocol.
13021369Sdduvall 	 * So this is just a final consistency check: we shouldn't
13031369Sdduvall 	 * see EITHER the START bit (command started but not complete)
13041369Sdduvall 	 * OR the COMPLETE bit (command completed but not cleared).
13051369Sdduvall 	 */
13061369Sdduvall 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
13071369Sdduvall 	if (regval & SEEPROM_ACCESS_START)
13081369Sdduvall 		return (EPROTO);
13091369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE)
13101369Sdduvall 		return (EPROTO);
13111369Sdduvall 
13121369Sdduvall 	/*
13131369Sdduvall 	 * Assemble the command ...
13141369Sdduvall 	 */
13151369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
13161369Sdduvall 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
13171369Sdduvall 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
13181369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
13191369Sdduvall 	cmd |= SEEPROM_ACCESS_START;
13201369Sdduvall 	cmd |= SEEPROM_ACCESS_COMPLETE;
13211369Sdduvall 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
13221369Sdduvall 
13231369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
13241369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
13251369Sdduvall 
13261369Sdduvall 	/*
13271369Sdduvall 	 * By observation, a successful access takes ~20us on a 5703/4,
13281369Sdduvall 	 * but apparently much longer (up to 1000us) on the obsolescent
13291369Sdduvall 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
13301369Sdduvall 	 * timeouts here; but OTOH, we don't want a bogus access to lock
13311369Sdduvall 	 * out interrupts for longer than necessary. So we'll allow up
13321369Sdduvall 	 * to 1000us ...
13331369Sdduvall 	 */
13341369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
13351369Sdduvall 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
13361369Sdduvall 		if (regval & SEEPROM_ACCESS_COMPLETE)
13371369Sdduvall 			break;
13381369Sdduvall 		drv_usecwait(1);
13391369Sdduvall 	}
13401369Sdduvall 
13411369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE) {
13421369Sdduvall 		/*
13431369Sdduvall 		 * All OK; read the SEEPROM data register, then write back
13441369Sdduvall 		 * the value read from the address register in order to
13451369Sdduvall 		 * clear the <complete> bit and leave the SEEPROM access
13461369Sdduvall 		 * state machine idle, ready for the next access ...
13471369Sdduvall 		 */
13481369Sdduvall 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
13491369Sdduvall 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
13501369Sdduvall 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
13511369Sdduvall 		return (0);
13521369Sdduvall 	}
13531369Sdduvall 
13541369Sdduvall 	/*
13551369Sdduvall 	 * Hmm ... what happened here?
13561369Sdduvall 	 *
13572135Szh199473 	 * Most likely, the user addressed a non-existent SEEPROM. Or
13581369Sdduvall 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
13591369Sdduvall 	 * and didn't respond to being addressed. Either way, it's left
13601369Sdduvall 	 * the SEEPROM access state machine wedged. So we'll reset it
13611369Sdduvall 	 * before we leave, so it's ready for next time ...
13621369Sdduvall 	 */
13631369Sdduvall 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
13641369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
13651369Sdduvall 	return (ENODATA);
13661369Sdduvall }
13671369Sdduvall 
13681369Sdduvall /*
13691369Sdduvall  * Basic Flash get/set access routine
13701369Sdduvall  *
13711369Sdduvall  * These use the chip's Flash auto-access method, controlled by the
13721369Sdduvall  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
13731369Sdduvall  * fiddle with the individual bits.
13741369Sdduvall  *
13751369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
13761369Sdduvall  * the right to access the Flash, via bge_nvmem_acquire() above.
13771369Sdduvall  *
13781369Sdduvall  * Return value:
13791369Sdduvall  *	0 on success,
13801369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
13811369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
13821369Sdduvall  *
13831369Sdduvall  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
13841369Sdduvall  * from a (successful) NVM_FLASH_CMD_RD.
13851369Sdduvall  */
13861369Sdduvall static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
13871369Sdduvall 				uint32_t *dp);
13881369Sdduvall #pragma	no_inline(bge_flash_access)
13891369Sdduvall 
13901369Sdduvall static int
13911369Sdduvall bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
13921369Sdduvall {
13931369Sdduvall 	uint32_t tries;
13941369Sdduvall 	uint32_t regval;
13951369Sdduvall 
13961369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
13971369Sdduvall 
13981369Sdduvall 	/*
13991369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
14001369Sdduvall 	 * to specifically disable SEEPROM access while accessing Flash.
14011369Sdduvall 	 * The older chips don't support Flash, and the NVM registers don't
14021369Sdduvall 	 * exist, so we shouldn't be here at all!
14031369Sdduvall 	 */
14041369Sdduvall 	switch (bgep->chipid.nvtype) {
14051369Sdduvall 	case BGE_NVTYPE_NONE:
14061369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
14071369Sdduvall 		_NOTE(NOTREACHED)
14081369Sdduvall 	case BGE_NVTYPE_SEEPROM:
14091369Sdduvall 		return (ENODEV);
14101369Sdduvall 
14111369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
14121369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
14131369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
14141369Sdduvall 	default:
14151369Sdduvall 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
14164588Sml149210 		    NVM_CFG1_LEGACY_SEEPROM_MODE);
14171369Sdduvall 		break;
14181369Sdduvall 	}
14191369Sdduvall 
14201369Sdduvall 	/*
14211369Sdduvall 	 * Assemble the command ...
14221369Sdduvall 	 */
14231369Sdduvall 	addr &= NVM_FLASH_ADDR_MASK;
14241369Sdduvall 	cmd |= NVM_FLASH_CMD_DOIT;
14251369Sdduvall 	cmd |= NVM_FLASH_CMD_FIRST;
14261369Sdduvall 	cmd |= NVM_FLASH_CMD_LAST;
14271369Sdduvall 	cmd |= NVM_FLASH_CMD_DONE;
14281369Sdduvall 
14291369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
14301369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
14311369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
14321369Sdduvall 
14331369Sdduvall 	/*
14341369Sdduvall 	 * Allow up to 1000ms ...
14351369Sdduvall 	 */
14361369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
14371369Sdduvall 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
14381369Sdduvall 		if (regval & NVM_FLASH_CMD_DONE)
14391369Sdduvall 			break;
14401369Sdduvall 		drv_usecwait(1);
14411369Sdduvall 	}
14421369Sdduvall 
14431369Sdduvall 	if (regval & NVM_FLASH_CMD_DONE) {
14441369Sdduvall 		/*
14451369Sdduvall 		 * All OK; read the data from the Flash read register
14461369Sdduvall 		 */
14471369Sdduvall 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
14481369Sdduvall 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
14491369Sdduvall 		return (0);
14501369Sdduvall 	}
14511369Sdduvall 
14521369Sdduvall 	/*
14531369Sdduvall 	 * Hmm ... what happened here?
14541369Sdduvall 	 *
14552135Szh199473 	 * Most likely, the user addressed a non-existent Flash. Or
14561369Sdduvall 	 * maybe the Flash was busy internally (e.g. processing a write)
14571369Sdduvall 	 * and didn't respond to being addressed. Either way, there's
14581369Sdduvall 	 * nothing we can here ...
14591369Sdduvall 	 */
14601369Sdduvall 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
14611369Sdduvall 	return (ENODATA);
14621369Sdduvall }
14631369Sdduvall 
14641369Sdduvall /*
14651369Sdduvall  * The next two functions regulate access to the NVram (if fitted).
14661369Sdduvall  *
14671369Sdduvall  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
14681369Sdduvall  * (SPI) interface, but they can be accessed through either port. These
14691369Sdduvall  * are managed by different instance of this driver and have no software
14701369Sdduvall  * state in common.
14711369Sdduvall  *
14721369Sdduvall  * In addition (and even on a single core chip) the chip's internal
14731369Sdduvall  * firmware can access the SEEPROM/Flash, most notably after a RESET
14741369Sdduvall  * when it may download code to run internally.
14751369Sdduvall  *
14761369Sdduvall  * So we need to arbitrate between these various software agents.  For
14771369Sdduvall  * this purpose, the chip provides the Software Arbitration Register,
14781369Sdduvall  * which implements hardware(!) arbitration.
14791369Sdduvall  *
14801369Sdduvall  * This functionality didn't exist on older (5700/5701) chips, so there's
14811369Sdduvall  * nothing we can do by way of arbitration on those; also, if there's no
14821369Sdduvall  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
14831369Sdduvall  * nothing to do.
14841369Sdduvall  *
14851369Sdduvall  * The internal firmware appears to use Request 0, which is the highest
14861369Sdduvall  * priority.  So we'd like to use Request 2, leaving one higher and one
14871369Sdduvall  * lower for any future developments ... but apparently this doesn't
14881369Sdduvall  * always work.  So for now, the code uses Request 1 ;-(
14891369Sdduvall  */
14901369Sdduvall 
14911369Sdduvall #define	NVM_READ_REQ	NVM_READ_REQ1
14921369Sdduvall #define	NVM_RESET_REQ	NVM_RESET_REQ1
14931369Sdduvall #define	NVM_SET_REQ	NVM_SET_REQ1
14941369Sdduvall 
14951369Sdduvall static void bge_nvmem_relinquish(bge_t *bgep);
14961369Sdduvall #pragma	no_inline(bge_nvmem_relinquish)
14971369Sdduvall 
14981369Sdduvall static void
14991369Sdduvall bge_nvmem_relinquish(bge_t *bgep)
15001369Sdduvall {
15011369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15021369Sdduvall 
15031369Sdduvall 	switch (bgep->chipid.nvtype) {
15041369Sdduvall 	case BGE_NVTYPE_NONE:
15051369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15061369Sdduvall 		_NOTE(NOTREACHED)
15071369Sdduvall 		return;
15081369Sdduvall 
15091369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15101369Sdduvall 		/*
15111369Sdduvall 		 * No arbitration performed, no release needed
15121369Sdduvall 		 */
15131369Sdduvall 		return;
15141369Sdduvall 
15151369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15161369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15171369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15181369Sdduvall 	default:
15191369Sdduvall 		break;
15201369Sdduvall 	}
15211369Sdduvall 
15221369Sdduvall 	/*
15231369Sdduvall 	 * Our own request should be present (whether or not granted) ...
15241369Sdduvall 	 */
15251865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15261369Sdduvall 
15271369Sdduvall 	/*
15281369Sdduvall 	 * ... this will make it go away.
15291369Sdduvall 	 */
15301369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
15311865Sdilpreet 	(void) bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15321369Sdduvall }
15331369Sdduvall 
15341369Sdduvall /*
15351369Sdduvall  * Arbitrate for access to the NVmem, if necessary
15361369Sdduvall  *
15371369Sdduvall  * Return value:
15381369Sdduvall  *	0 on success
15391369Sdduvall  *	EAGAIN if the device is in use (retryable)
15401369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
15411369Sdduvall  */
15421369Sdduvall static int bge_nvmem_acquire(bge_t *bgep);
15431369Sdduvall #pragma	no_inline(bge_nvmem_acquire)
15441369Sdduvall 
15451369Sdduvall static int
15461369Sdduvall bge_nvmem_acquire(bge_t *bgep)
15471369Sdduvall {
15481369Sdduvall 	uint32_t regval;
15491369Sdduvall 	uint32_t tries;
15501369Sdduvall 
15511369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
15521369Sdduvall 
15531369Sdduvall 	switch (bgep->chipid.nvtype) {
15541369Sdduvall 	case BGE_NVTYPE_NONE:
15551369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15561369Sdduvall 		/*
15571369Sdduvall 		 * Access denied: no (recognisable) device fitted
15581369Sdduvall 		 */
15591369Sdduvall 		return (ENODEV);
15601369Sdduvall 
15611369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15621369Sdduvall 		/*
15631369Sdduvall 		 * Access granted: no arbitration needed (or possible)
15641369Sdduvall 		 */
15651369Sdduvall 		return (0);
15661369Sdduvall 
15671369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15681369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15691369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15701369Sdduvall 	default:
15711369Sdduvall 		/*
15721369Sdduvall 		 * Access conditional: conduct arbitration protocol
15731369Sdduvall 		 */
15741369Sdduvall 		break;
15751369Sdduvall 	}
15761369Sdduvall 
15771369Sdduvall 	/*
15781369Sdduvall 	 * We're holding the per-port mutex <genlock>, so no-one other
15792135Szh199473 	 * thread can be attempting to access the NVmem through *this*
15801369Sdduvall 	 * port. But it could be in use by the *other* port (of a 5704),
15811369Sdduvall 	 * or by the chip's internal firmware, so we have to go through
15821369Sdduvall 	 * the full (hardware) arbitration protocol ...
15831369Sdduvall 	 *
15841369Sdduvall 	 * Note that *because* we're holding <genlock>, the interrupt handler
15851369Sdduvall 	 * won't be able to progress.  So we're only willing to spin for a
15861369Sdduvall 	 * fairly short time.  Specifically:
15871369Sdduvall 	 *
15881369Sdduvall 	 *	We *must* wait long enough for the hardware to resolve all
15891369Sdduvall 	 *	requests and determine the winner.  Fortunately, this is
15901369Sdduvall 	 *	"almost instantaneous", even as observed by GHz CPUs.
15911369Sdduvall 	 *
15921369Sdduvall 	 *	A successful access by another Solaris thread (via either
15931369Sdduvall 	 *	port) typically takes ~20us.  So waiting a bit longer than
15941369Sdduvall 	 *	that will give a good chance of success, if the other user
15951369Sdduvall 	 *	*is* another thread on the other port.
15961369Sdduvall 	 *
15971369Sdduvall 	 *	However, the internal firmware can hold on to the NVmem
15981369Sdduvall 	 *	for *much* longer: at least 10 milliseconds just after a
15991369Sdduvall 	 *	RESET, and maybe even longer if the NVmem actually contains
16001369Sdduvall 	 *	code to download and run on the internal CPUs.
16011369Sdduvall 	 *
16021369Sdduvall 	 * So, we'll allow 50us; if that's not enough then it's up to the
16031369Sdduvall 	 * caller to retry later (hence the choice of return code EAGAIN).
16041369Sdduvall 	 */
16051369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
16061369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
16071369Sdduvall 
16081369Sdduvall 	for (tries = 0; tries < 50; ++tries) {
16091369Sdduvall 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
16101369Sdduvall 		if (regval & NVM_WON_REQ1)
16111369Sdduvall 			break;
16121369Sdduvall 		drv_usecwait(1);
16131369Sdduvall 	}
16141369Sdduvall 
16151369Sdduvall 	if (regval & NVM_WON_REQ1) {
16161369Sdduvall 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
16171369Sdduvall 		return (0);
16181369Sdduvall 	}
16191369Sdduvall 
16201369Sdduvall 	/*
16211369Sdduvall 	 * Somebody else must be accessing the NVmem, so abandon our
16221369Sdduvall 	 * attempt take control of it.  The caller can try again later ...
16231369Sdduvall 	 */
16241369Sdduvall 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
16251369Sdduvall 	bge_nvmem_relinquish(bgep);
16261369Sdduvall 	return (EAGAIN);
16271369Sdduvall }
16281369Sdduvall 
16291369Sdduvall /*
16301369Sdduvall  * This code assumes that the GPIO1 bit has been wired up to the NVmem
16311369Sdduvall  * write protect line in such a way that the NVmem is protected when
16321369Sdduvall  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
16331369Sdduvall  * NVmem writable we have to change GPIO1 to an output AND drive it low.
16341369Sdduvall  *
16351369Sdduvall  * Note: there's only one set of GPIO pins on a 5704, even though they
16361369Sdduvall  * can be accessed through either port.  So the chip has to resolve what
16371369Sdduvall  * happens if the two ports program a single pin differently ... the rule
16381369Sdduvall  * it uses is that if the ports disagree about the *direction* of a pin,
16391369Sdduvall  * "output" wins over "input", but if they disagree about its *value* as
16401369Sdduvall  * an output, then the pin is TRISTATED instead!  In such a case, no-one
16411369Sdduvall  * wins, and the external signal does whatever the external circuitry
16421369Sdduvall  * defines as the default -- which we've assumed is the PROTECTED state.
16431369Sdduvall  * So, we always change GPIO1 back to being an *input* whenever we're not
16441369Sdduvall  * specifically using it to unprotect the NVmem. This allows either port
16452135Szh199473  * to update the NVmem, although obviously only one at a time!
16461369Sdduvall  *
16471369Sdduvall  * The caller should hold <genlock> and *also* have already acquired the
16481369Sdduvall  * right to access the NVmem, via bge_nvmem_acquire() above.
16491369Sdduvall  */
16501369Sdduvall static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
16511369Sdduvall #pragma	inline(bge_nvmem_protect)
16521369Sdduvall 
16531369Sdduvall static void
16541369Sdduvall bge_nvmem_protect(bge_t *bgep, boolean_t protect)
16551369Sdduvall {
16561369Sdduvall 	uint32_t regval;
16571369Sdduvall 
16581369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
16591369Sdduvall 
16601369Sdduvall 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
16611369Sdduvall 	if (protect) {
16621369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_1;
16631369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16641369Sdduvall 	} else {
16651369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
16661369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16671369Sdduvall 	}
16681369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
16691369Sdduvall }
16701369Sdduvall 
16711369Sdduvall /*
16721369Sdduvall  * Now put it all together ...
16731369Sdduvall  *
16741369Sdduvall  * Try to acquire control of the NVmem; if successful, then:
16751369Sdduvall  *	unprotect it (if we want to write to it)
16761369Sdduvall  *	perform the requested access
16771369Sdduvall  *	reprotect it (after a write)
16781369Sdduvall  *	relinquish control
16791369Sdduvall  *
16801369Sdduvall  * Return value:
16811369Sdduvall  *	0 on success,
16821369Sdduvall  *	EAGAIN if the device is in use (retryable)
16831369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
16841369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
16851369Sdduvall  *	EPROTO on other h/w or s/w errors.
16861369Sdduvall  */
16871369Sdduvall static int
16881369Sdduvall bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
16891369Sdduvall {
16901369Sdduvall 	int err;
16911369Sdduvall 
16921369Sdduvall 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
16931369Sdduvall 		switch (cmd) {
16941369Sdduvall 		case BGE_SEE_READ:
16951369Sdduvall 			err = bge_seeprom_access(bgep,
16961369Sdduvall 			    SEEPROM_ACCESS_READ, addr, dp);
16971369Sdduvall 			break;
16981369Sdduvall 
16991369Sdduvall 		case BGE_SEE_WRITE:
17001369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
17011369Sdduvall 			err = bge_seeprom_access(bgep,
17021369Sdduvall 			    SEEPROM_ACCESS_WRITE, addr, dp);
17031369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
17041369Sdduvall 			break;
17051369Sdduvall 
17061369Sdduvall 		case BGE_FLASH_READ:
17071369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17081369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17091369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
17101369Sdduvall 				    NVM_ACCESS_ENABLE);
17111369Sdduvall 			}
17121369Sdduvall 			err = bge_flash_access(bgep,
17131369Sdduvall 			    NVM_FLASH_CMD_RD, addr, dp);
17141369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17151369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17161369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
17171369Sdduvall 				    NVM_ACCESS_ENABLE);
17181369Sdduvall 			}
17191369Sdduvall 			break;
17201369Sdduvall 
17211369Sdduvall 		case BGE_FLASH_WRITE:
17221369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17231369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17241369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
17251369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
17261369Sdduvall 			}
17271369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
17281369Sdduvall 			err = bge_flash_access(bgep,
17291369Sdduvall 			    NVM_FLASH_CMD_WR, addr, dp);
17301369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
17311369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
17321369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
17331369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
17341369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
17351369Sdduvall 			}
17361369Sdduvall 
17371369Sdduvall 			break;
17381369Sdduvall 
17391369Sdduvall 		default:
17401369Sdduvall 			_NOTE(NOTREACHED)
17411369Sdduvall 			break;
17421369Sdduvall 		}
17431369Sdduvall 		bge_nvmem_relinquish(bgep);
17441369Sdduvall 	}
17451369Sdduvall 
17461369Sdduvall 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
17471369Sdduvall 	return (err);
17481369Sdduvall }
17491369Sdduvall 
17501369Sdduvall /*
17511369Sdduvall  * Attempt to get a MAC address from the SEEPROM or Flash, if any
17521369Sdduvall  */
17531369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17541369Sdduvall #pragma no_inline(bge_get_nvmac)
17551369Sdduvall 
17561369Sdduvall static uint64_t
17571369Sdduvall bge_get_nvmac(bge_t *bgep)
17581369Sdduvall {
17591369Sdduvall 	uint32_t mac_high;
17601369Sdduvall 	uint32_t mac_low;
17611369Sdduvall 	uint32_t addr;
17621369Sdduvall 	uint32_t cmd;
17631369Sdduvall 	uint64_t mac;
17641369Sdduvall 
17651369Sdduvall 	BGE_TRACE(("bge_get_nvmac($%p)",
17664588Sml149210 	    (void *)bgep));
17671369Sdduvall 
17681369Sdduvall 	switch (bgep->chipid.nvtype) {
17691369Sdduvall 	case BGE_NVTYPE_NONE:
17701369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
17711369Sdduvall 	default:
17721369Sdduvall 		return (0ULL);
17731369Sdduvall 
17741369Sdduvall 	case BGE_NVTYPE_SEEPROM:
17751369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
17761369Sdduvall 		cmd = BGE_SEE_READ;
17771369Sdduvall 		break;
17781369Sdduvall 
17791369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
17801369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
17811369Sdduvall 		cmd = BGE_FLASH_READ;
17821369Sdduvall 		break;
17831369Sdduvall 	}
17841369Sdduvall 
17851369Sdduvall 	addr = NVMEM_DATA_MAC_ADDRESS;
17861369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
17871369Sdduvall 		return (0ULL);
17881369Sdduvall 	addr += 4;
17891369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
17901369Sdduvall 		return (0ULL);
17911369Sdduvall 
17921369Sdduvall 	/*
17931369Sdduvall 	 * The Broadcom chip is natively BIG-endian, so that's how the
17941369Sdduvall 	 * MAC address is represented in NVmem.  We may need to swap it
17951369Sdduvall 	 * around on a little-endian host ...
17961369Sdduvall 	 */
17971369Sdduvall #ifdef	_BIG_ENDIAN
17981369Sdduvall 	mac = mac_high;
17991369Sdduvall 	mac = mac << 32;
18001369Sdduvall 	mac |= mac_low;
18011369Sdduvall #else
18021369Sdduvall 	mac = BGE_BSWAP_32(mac_high);
18031369Sdduvall 	mac = mac << 32;
18041369Sdduvall 	mac |= BGE_BSWAP_32(mac_low);
18051369Sdduvall #endif	/* _BIG_ENDIAN */
18061369Sdduvall 
18071369Sdduvall 	return (mac);
18081369Sdduvall }
18091369Sdduvall 
18101369Sdduvall #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
18111369Sdduvall 
18121369Sdduvall /*
18131369Sdduvall  * Dummy version for when we're not supporting NVmem access
18141369Sdduvall  */
18151369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
18161369Sdduvall #pragma inline(bge_get_nvmac)
18171369Sdduvall 
18181369Sdduvall static uint64_t
18191369Sdduvall bge_get_nvmac(bge_t *bgep)
18201369Sdduvall {
18211369Sdduvall 	_NOTE(ARGUNUSED(bgep))
18221369Sdduvall 	return (0ULL);
18231369Sdduvall }
18241369Sdduvall 
18251369Sdduvall #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
18261369Sdduvall 
18271369Sdduvall /*
18281369Sdduvall  * Determine the type of NVmem that is (or may be) attached to this chip,
18291369Sdduvall  */
18301369Sdduvall static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
18311369Sdduvall #pragma no_inline(bge_nvmem_id)
18321369Sdduvall 
18331369Sdduvall static enum bge_nvmem_type
18341369Sdduvall bge_nvmem_id(bge_t *bgep)
18351369Sdduvall {
18361369Sdduvall 	enum bge_nvmem_type nvtype;
18371369Sdduvall 	uint32_t config1;
18381369Sdduvall 
18391369Sdduvall 	BGE_TRACE(("bge_nvmem_id($%p)",
18404588Sml149210 	    (void *)bgep));
18411369Sdduvall 
18421369Sdduvall 	switch (bgep->chipid.device) {
18431369Sdduvall 	default:
18441369Sdduvall 		/*
18451369Sdduvall 		 * We shouldn't get here; it means we don't recognise
18461369Sdduvall 		 * the chip, which means we don't know how to determine
18471369Sdduvall 		 * what sort of NVmem (if any) it has.  So we'll say
18481369Sdduvall 		 * NONE, to disable the NVmem access code ...
18491369Sdduvall 		 */
18501369Sdduvall 		nvtype = BGE_NVTYPE_NONE;
18511369Sdduvall 		break;
18521369Sdduvall 
18531369Sdduvall 	case DEVICE_ID_5700:
18541369Sdduvall 	case DEVICE_ID_5700x:
18551369Sdduvall 	case DEVICE_ID_5701:
18561369Sdduvall 		/*
18571369Sdduvall 		 * These devices support *only* SEEPROMs
18581369Sdduvall 		 */
18591369Sdduvall 		nvtype = BGE_NVTYPE_SEEPROM;
18601369Sdduvall 		break;
18611369Sdduvall 
18621369Sdduvall 	case DEVICE_ID_5702:
18631369Sdduvall 	case DEVICE_ID_5702fe:
18641369Sdduvall 	case DEVICE_ID_5703C:
18651369Sdduvall 	case DEVICE_ID_5703S:
18661369Sdduvall 	case DEVICE_ID_5704C:
18671369Sdduvall 	case DEVICE_ID_5704S:
18681369Sdduvall 	case DEVICE_ID_5704:
18691369Sdduvall 	case DEVICE_ID_5705M:
18701369Sdduvall 	case DEVICE_ID_5705C:
18713170Sml149210 	case DEVICE_ID_5705_2:
18721369Sdduvall 	case DEVICE_ID_5706:
18731369Sdduvall 	case DEVICE_ID_5782:
18741369Sdduvall 	case DEVICE_ID_5788:
18752135Szh199473 	case DEVICE_ID_5789:
18761369Sdduvall 	case DEVICE_ID_5751:
18771369Sdduvall 	case DEVICE_ID_5751M:
18782675Szh199473 	case DEVICE_ID_5752:
18792675Szh199473 	case DEVICE_ID_5752M:
18803771Sml149210 	case DEVICE_ID_5754:
18814330Sml149210 	case DEVICE_ID_5755:
1882*6546Sgh162552 	case DEVICE_ID_5755M:
18831369Sdduvall 	case DEVICE_ID_5721:
18841369Sdduvall 	case DEVICE_ID_5714C:
18851369Sdduvall 	case DEVICE_ID_5714S:
18861369Sdduvall 	case DEVICE_ID_5715C:
18873170Sml149210 	case DEVICE_ID_5715S:
18881369Sdduvall 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
18891369Sdduvall 		if (config1 & NVM_CFG1_FLASH_MODE)
18901369Sdduvall 			if (config1 & NVM_CFG1_BUFFERED_MODE)
18911369Sdduvall 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
18921369Sdduvall 			else
18931369Sdduvall 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
18941369Sdduvall 		else
18951369Sdduvall 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
18961369Sdduvall 		break;
18971369Sdduvall 	}
18981369Sdduvall 
18991369Sdduvall 	return (nvtype);
19001369Sdduvall }
19011369Sdduvall 
19021369Sdduvall #undef	BGE_DBG
19031369Sdduvall #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
19041369Sdduvall 
19051369Sdduvall static void
19061369Sdduvall bge_init_recv_rule(bge_t *bgep)
19071369Sdduvall {
19081369Sdduvall 	bge_recv_rule_t *rulep;
19091369Sdduvall 	uint32_t i;
19101369Sdduvall 
19111369Sdduvall 	/*
19121369Sdduvall 	 * receive rule: direct all TCP traffic to ring RULE_MATCH_TO_RING
19131369Sdduvall 	 * 1. to direct UDP traffic, set:
19141369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
19151369Sdduvall 	 * 	rulep->mask_value = RULE_UDP_MASK_VALUE;
19161369Sdduvall 	 * 2. to direct ICMP traffic, set:
19171369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
19181369Sdduvall 	 * 	rulep->mask_value = RULE_ICMP_MASK_VALUE;
19191369Sdduvall 	 * 3. to direct traffic by source ip, set:
19201369Sdduvall 	 * 	rulep->control = RULE_SIP_CONTROL;
19211369Sdduvall 	 * 	rulep->mask_value = RULE_SIP_MASK_VALUE;
19221369Sdduvall 	 */
19231369Sdduvall 	rulep = bgep->recv_rules;
19241369Sdduvall 	rulep->control = RULE_PROTO_CONTROL;
19251369Sdduvall 	rulep->mask_value = RULE_TCP_MASK_VALUE;
19261369Sdduvall 
19271369Sdduvall 	/*
19281369Sdduvall 	 * set receive rule registers
19291369Sdduvall 	 */
19301369Sdduvall 	rulep = bgep->recv_rules;
19311369Sdduvall 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
19321369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
19331369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
19341369Sdduvall 	}
19351369Sdduvall }
19361369Sdduvall 
19371369Sdduvall /*
19381369Sdduvall  * Using the values captured by bge_chip_cfg_init(), and additional probes
19391369Sdduvall  * as required, characterise the chip fully: determine the label by which
19401369Sdduvall  * to refer to this chip, the correct settings for various registers, and
19411369Sdduvall  * of course whether the device and/or subsystem are supported!
19421369Sdduvall  */
19431865Sdilpreet int bge_chip_id_init(bge_t *bgep);
19441369Sdduvall #pragma	no_inline(bge_chip_id_init)
19451369Sdduvall 
19461865Sdilpreet int
19471369Sdduvall bge_chip_id_init(bge_t *bgep)
19481369Sdduvall {
19491369Sdduvall 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
19501369Sdduvall 	boolean_t sys_ok;
19511369Sdduvall 	boolean_t dev_ok;
19521369Sdduvall 	chip_id_t *cidp;
19531369Sdduvall 	uint32_t subid;
19541369Sdduvall 	char *devname;
19551369Sdduvall 	char *sysname;
19561369Sdduvall 	int *ids;
19571369Sdduvall 	int err;
19581369Sdduvall 	uint_t i;
19591369Sdduvall 
19601369Sdduvall 	sys_ok = dev_ok = B_FALSE;
19611369Sdduvall 	cidp = &bgep->chipid;
19621369Sdduvall 
19631369Sdduvall 	/*
19641369Sdduvall 	 * Check the PCI device ID to determine the generic chip type and
19651369Sdduvall 	 * select parameters that depend on this.
19661369Sdduvall 	 *
19671369Sdduvall 	 * Note: because the SPARC platforms in general don't fit the
19681369Sdduvall 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
19691369Sdduvall 	 * as zero - which is why we use <asic_rev> rather than <revision>
19701369Sdduvall 	 * below ...
19711369Sdduvall 	 *
19721369Sdduvall 	 * Note: in general we can't distinguish between the Copper/SerDes
19731369Sdduvall 	 * versions by ID alone, as some Copper devices (e.g. some but not
19741369Sdduvall 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
19751369Sdduvall 	 * treat them the same here, and the MII code works out the media
19761369Sdduvall 	 * type later on ...
19771369Sdduvall 	 */
19781369Sdduvall 	cidp->mbuf_base = bge_mbuf_pool_base;
19791369Sdduvall 	cidp->mbuf_length = bge_mbuf_pool_len;
19801369Sdduvall 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
19811369Sdduvall 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
19821369Sdduvall 	cidp->pci_type = BGE_PCI_X;
19831369Sdduvall 	cidp->statistic_type = BGE_STAT_BLK;
19841908Sly149593 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
19851908Sly149593 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
19861908Sly149593 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
19875903Ssowmini 	cidp->rx_ticks_norm = bge_rx_ticks_norm;
19885903Ssowmini 	cidp->rx_count_norm = bge_rx_count_norm;
19891369Sdduvall 
19901369Sdduvall 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
19911369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
19921369Sdduvall 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
19931369Sdduvall 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
19941369Sdduvall 
19951369Sdduvall 	cidp->msi_enabled = B_FALSE;
19961369Sdduvall 
19971369Sdduvall 	switch (cidp->device) {
19981369Sdduvall 	case DEVICE_ID_5700:
19991369Sdduvall 	case DEVICE_ID_5700x:
20001369Sdduvall 		cidp->chip_label = 5700;
20012135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20021369Sdduvall 		break;
20031369Sdduvall 
20041369Sdduvall 	case DEVICE_ID_5701:
20051369Sdduvall 		cidp->chip_label = 5701;
20061369Sdduvall 		dev_ok = B_TRUE;
20072135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20081369Sdduvall 		break;
20091369Sdduvall 
20101369Sdduvall 	case DEVICE_ID_5702:
20111369Sdduvall 	case DEVICE_ID_5702fe:
20121369Sdduvall 		cidp->chip_label = 5702;
20131369Sdduvall 		dev_ok = B_TRUE;
20142135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20152135Szh199473 		cidp->pci_type = BGE_PCI;
20161369Sdduvall 		break;
20171369Sdduvall 
20181369Sdduvall 	case DEVICE_ID_5703C:
20191369Sdduvall 	case DEVICE_ID_5703S:
20201369Sdduvall 	case DEVICE_ID_5703:
20211369Sdduvall 		/*
20221369Sdduvall 		 * Revision A0 of the 5703/5793 had various errata
20231369Sdduvall 		 * that we can't or don't work around, so it's not
20241369Sdduvall 		 * supported, but all later versions are
20251369Sdduvall 		 */
20261369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
20271369Sdduvall 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
20281369Sdduvall 			dev_ok = B_TRUE;
20292135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20301369Sdduvall 		break;
20311369Sdduvall 
20321369Sdduvall 	case DEVICE_ID_5704C:
20331369Sdduvall 	case DEVICE_ID_5704S:
20341369Sdduvall 	case DEVICE_ID_5704:
20351369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
20361369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
20371369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
20381369Sdduvall 		dev_ok = B_TRUE;
20396133Sgh162552 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20401369Sdduvall 		break;
20411369Sdduvall 
20421369Sdduvall 	case DEVICE_ID_5705C:
20431369Sdduvall 	case DEVICE_ID_5705M:
20441369Sdduvall 	case DEVICE_ID_5705MA3:
20451369Sdduvall 	case DEVICE_ID_5705F:
20463170Sml149210 	case DEVICE_ID_5705_2:
20473771Sml149210 	case DEVICE_ID_5754:
20483771Sml149210 		if (cidp->device == DEVICE_ID_5754) {
20493771Sml149210 			cidp->chip_label = 5754;
20503771Sml149210 			cidp->pci_type = BGE_PCI_E;
20513771Sml149210 		} else {
20523771Sml149210 			cidp->chip_label = 5705;
20533771Sml149210 			cidp->pci_type = BGE_PCI;
20543771Sml149210 		}
20551908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20561908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20571908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20581369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20591369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20601369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20611369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20621908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20631369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20642135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
20651369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20661369Sdduvall 		dev_ok = B_TRUE;
20671369Sdduvall 		break;
20681369Sdduvall 
20694588Sml149210 	case DEVICE_ID_5753:
20704588Sml149210 		cidp->chip_label = 5753;
20714588Sml149210 		cidp->pci_type = BGE_PCI_E;
20724588Sml149210 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20734588Sml149210 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20744588Sml149210 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20754588Sml149210 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20764588Sml149210 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20774588Sml149210 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20784588Sml149210 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
20794588Sml149210 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20804588Sml149210 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20814588Sml149210 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20824588Sml149210 		cidp->statistic_type = BGE_STAT_REG;
20834588Sml149210 		dev_ok = B_TRUE;
20844588Sml149210 		break;
20854588Sml149210 
20864330Sml149210 	case DEVICE_ID_5755:
2087*6546Sgh162552 	case DEVICE_ID_5755M:
20884330Sml149210 		cidp->chip_label = 5755;
20894330Sml149210 		cidp->pci_type = BGE_PCI_E;
20904330Sml149210 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
20914330Sml149210 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
20924330Sml149210 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
20934330Sml149210 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20944330Sml149210 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20954330Sml149210 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20964330Sml149210 		cidp->bge_mlcr_default |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
20974330Sml149210 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20984330Sml149210 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
20994330Sml149210 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21004330Sml149210 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21014330Sml149210 		cidp->statistic_type = BGE_STAT_REG;
21024330Sml149210 		dev_ok = B_TRUE;
21034330Sml149210 		break;
21044330Sml149210 
21051369Sdduvall 	case DEVICE_ID_5706:
21061369Sdduvall 		cidp->chip_label = 5706;
21071369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21081369Sdduvall 		break;
21091369Sdduvall 
21101369Sdduvall 	case DEVICE_ID_5782:
21111369Sdduvall 		/*
21121369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
21131369Sdduvall 		 */
21141369Sdduvall 		cidp->chip_label = 5782;
21151908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21161908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21171908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21181369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21191369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21201369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21211369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21221908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21231369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21242135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
21251369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21261369Sdduvall 		dev_ok = B_TRUE;
21271369Sdduvall 		break;
21281369Sdduvall 
21291369Sdduvall 	case DEVICE_ID_5788:
21301369Sdduvall 		/*
21311369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
21321369Sdduvall 		 */
21331369Sdduvall 		cidp->chip_label = 5788;
21341908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21351908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21361908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21371369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
21381369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
21391369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
21401369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21411908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21421369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21431369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
21441369Sdduvall 		dev_ok = B_TRUE;
21451369Sdduvall 		break;
21461369Sdduvall 
21471369Sdduvall 	case DEVICE_ID_5714C:
21481369Sdduvall 		if (cidp->revision >= REVISION_ID_5714_A2)
21491369Sdduvall 			cidp->msi_enabled = bge_enable_msi;
21501369Sdduvall 		/* FALLTHRU */
21511369Sdduvall 	case DEVICE_ID_5714S:
21521369Sdduvall 		cidp->chip_label = 5714;
21531908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21541908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21551908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21561369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21571369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21581369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21591369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
21601369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
21611369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21621908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21631369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21641369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21651369Sdduvall 		dev_ok = B_TRUE;
21661369Sdduvall 		break;
21671369Sdduvall 
21681369Sdduvall 	case DEVICE_ID_5715C:
21693170Sml149210 	case DEVICE_ID_5715S:
21701369Sdduvall 		cidp->chip_label = 5715;
21711908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21721908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21731908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21741369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21751369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21761369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21771369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
21781369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
21791369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21801908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21811369Sdduvall 		cidp->pci_type = BGE_PCI_E;
21821369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
21831908Sly149593 		if (cidp->revision >= REVISION_ID_5715_A2)
21841908Sly149593 			cidp->msi_enabled = bge_enable_msi;
21851369Sdduvall 		dev_ok = B_TRUE;
21861369Sdduvall 		break;
21871369Sdduvall 
21881369Sdduvall 	case DEVICE_ID_5721:
21891369Sdduvall 		cidp->chip_label = 5721;
21901908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
21911908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
21921908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
21931369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
21941369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
21951369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
21961369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
21971369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
21981908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
21991369Sdduvall 		cidp->pci_type = BGE_PCI_E;
22001369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
22011369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22021369Sdduvall 		dev_ok = B_TRUE;
22031369Sdduvall 		break;
22041369Sdduvall 
22051369Sdduvall 	case DEVICE_ID_5751:
22061369Sdduvall 	case DEVICE_ID_5751M:
22071369Sdduvall 		cidp->chip_label = 5751;
22081908Sly149593 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22091908Sly149593 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22101908Sly149593 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22111369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
22121369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
22131369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
22141369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
22151369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22161908Sly149593 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22171369Sdduvall 		cidp->pci_type = BGE_PCI_E;
22181369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
22191369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22201369Sdduvall 		dev_ok = B_TRUE;
22211369Sdduvall 		break;
22221369Sdduvall 
22232675Szh199473 	case DEVICE_ID_5752:
22242675Szh199473 	case DEVICE_ID_5752M:
22252675Szh199473 		cidp->chip_label = 5752;
22262675Szh199473 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_5705;
22272675Szh199473 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_5705;
22282675Szh199473 		cidp->mbuf_hi_water = MBUF_HIWAT_5705;
22292675Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
22302675Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
22312675Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
22322675Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
22332675Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22342675Szh199473 		cidp->tx_rings = BGE_SEND_RINGS_MAX_5705;
22352675Szh199473 		cidp->pci_type = BGE_PCI_E;
22362675Szh199473 		cidp->statistic_type = BGE_STAT_REG;
22372675Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22382675Szh199473 		dev_ok = B_TRUE;
22392675Szh199473 		break;
22402675Szh199473 
22412135Szh199473 	case DEVICE_ID_5789:
22422135Szh199473 		cidp->chip_label = 5789;
22432135Szh199473 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
22442135Szh199473 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
22452135Szh199473 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
22462135Szh199473 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
22472135Szh199473 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
22482135Szh199473 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
22492135Szh199473 		cidp->pci_type = BGE_PCI_E;
22502135Szh199473 		cidp->statistic_type = BGE_STAT_REG;
22512135Szh199473 		cidp->flags |= CHIP_FLAG_PARTIAL_CSUM;
22522135Szh199473 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
22532135Szh199473 		cidp->msi_enabled = B_TRUE;
22542135Szh199473 		dev_ok = B_TRUE;
22552135Szh199473 		break;
22562135Szh199473 
22571369Sdduvall 	}
22581369Sdduvall 
22591369Sdduvall 	/*
22601369Sdduvall 	 * Setup the default jumbo parameter.
22611369Sdduvall 	 */
22621369Sdduvall 	cidp->ethmax_size = ETHERMAX;
22631369Sdduvall 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
22641908Sly149593 	cidp->std_buf_size = BGE_STD_BUFF_SIZE;
22651369Sdduvall 
22661369Sdduvall 	/*
22671369Sdduvall 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
22681369Sdduvall 	 * setup below jumbo specific parameters.
22691908Sly149593 	 *
22701908Sly149593 	 * For BCM5714/5715, there is only one standard receive ring. So the
22711908Sly149593 	 * std buffer size should be set to BGE_JUMBO_BUFF_SIZE when jumbo
22721908Sly149593 	 * feature is enabled.
22731369Sdduvall 	 */
22741369Sdduvall 	if (bge_jumbo_enable &&
22751369Sdduvall 	    !(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
22761369Sdduvall 	    (cidp->default_mtu > BGE_DEFAULT_MTU) &&
22771369Sdduvall 	    (cidp->default_mtu <= BGE_MAXIMUM_MTU)) {
22784588Sml149210 		if (DEVICE_5714_SERIES_CHIPSETS(bgep)) {
22791908Sly149593 			cidp->mbuf_lo_water_rdma =
22801908Sly149593 			    RDMA_MBUF_LOWAT_5714_JUMBO;
22811908Sly149593 			cidp->mbuf_lo_water_rmac =
22821908Sly149593 			    MAC_RX_MBUF_LOWAT_5714_JUMBO;
22831908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_5714_JUMBO;
22841908Sly149593 			cidp->jumbo_slots = 0;
22851908Sly149593 			cidp->std_buf_size = BGE_JUMBO_BUFF_SIZE;
22864588Sml149210 		} else {
22871908Sly149593 			cidp->mbuf_lo_water_rdma =
22881908Sly149593 			    RDMA_MBUF_LOWAT_JUMBO;
22891908Sly149593 			cidp->mbuf_lo_water_rmac =
22901908Sly149593 			    MAC_RX_MBUF_LOWAT_JUMBO;
22911908Sly149593 			cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
22921908Sly149593 			cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
22931908Sly149593 		}
22941369Sdduvall 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
22951369Sdduvall 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
22961369Sdduvall 		cidp->ethmax_size = cidp->default_mtu +
22971369Sdduvall 		    sizeof (struct ether_header);
22981369Sdduvall 	}
22991369Sdduvall 
23001369Sdduvall 	/*
23011369Sdduvall 	 * Identify the NV memory type: SEEPROM or Flash?
23021369Sdduvall 	 */
23031369Sdduvall 	cidp->nvtype = bge_nvmem_id(bgep);
23041369Sdduvall 
23051369Sdduvall 	/*
23061369Sdduvall 	 * Now, we want to check whether this device is part of a
23071369Sdduvall 	 * supported subsystem (e.g., on the motherboard of a Sun
23081369Sdduvall 	 * branded platform).
23091369Sdduvall 	 *
23101369Sdduvall 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
23111369Sdduvall 	 */
23121369Sdduvall 	if (cidp->subven == VENDOR_ID_SUN)
23131369Sdduvall 		sys_ok = B_TRUE;
23141369Sdduvall 
23151369Sdduvall 	/*
23161369Sdduvall 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
23171369Sdduvall 	 * Note: 0x14e41647 should *not* appear in the list, but the code
23181369Sdduvall 	 * doesn't enforce that.
23191369Sdduvall 	 */
23201369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
23214588Sml149210 	    DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
23221369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
23231369Sdduvall 		/*
23241369Sdduvall 		 * Got the list; scan for a matching subsystem vendor/device
23251369Sdduvall 		 */
23261369Sdduvall 		subid = (cidp->subven << 16) | cidp->subdev;
23271369Sdduvall 		while (i--)
23281369Sdduvall 			if (ids[i] == subid)
23291369Sdduvall 				sys_ok = B_TRUE;
23301369Sdduvall 		ddi_prop_free(ids);
23311369Sdduvall 	}
23321369Sdduvall 
23331369Sdduvall 	/*
23341369Sdduvall 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
23351369Sdduvall 	 *
23361369Sdduvall 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
23371369Sdduvall 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
23381369Sdduvall 	 * we have to check specially for the exact device paths to the
23391369Sdduvall 	 * motherboard devices on those platforms ;-(
23401369Sdduvall 	 *
23411369Sdduvall 	 * Note: we can't just use the "supported-subsystems" mechanism
23421369Sdduvall 	 * above, because the entry would have to be 0x14e41647 -- which
23431369Sdduvall 	 * would then accept *any* plugin card that *didn't* contain a
23441369Sdduvall 	 * (valid) SEEPROM ;-(
23451369Sdduvall 	 */
23461369Sdduvall 	sysname = ddi_node_name(ddi_root_node());
23471369Sdduvall 	devname = ddi_pathname(bgep->devinfo, buf);
23481369Sdduvall 	ASSERT(strlen(devname) > 0);
23491369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
23501369Sdduvall 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
23511369Sdduvall 			sys_ok = B_TRUE;
23521369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
23531369Sdduvall 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
23541369Sdduvall 			sys_ok = B_TRUE;
23551369Sdduvall 
23561369Sdduvall 	/*
23571369Sdduvall 	 * Now check what we've discovered: is this truly a supported
23581369Sdduvall 	 * chip on (the motherboard of) a supported platform?
23591369Sdduvall 	 *
23601369Sdduvall 	 * Possible problems here:
23611369Sdduvall 	 * 1)	it's a completely unheard-of chip (e.g. 5761)
23621369Sdduvall 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
23631369Sdduvall 	 * 3)	it's a chip we would support if it were on the motherboard
23641369Sdduvall 	 *	of a Sun platform, but this one isn't ;-(
23651369Sdduvall 	 */
23661369Sdduvall 	if (cidp->chip_label == 0)
23671369Sdduvall 		bge_problem(bgep,
23684588Sml149210 		    "Device 'pci%04x,%04x' not recognized (%d?)",
23694588Sml149210 		    cidp->vendor, cidp->device, cidp->device);
23701369Sdduvall 	else if (!dev_ok)
23711369Sdduvall 		bge_problem(bgep,
23724588Sml149210 		    "Device 'pci%04x,%04x' (%d) revision %d not supported",
23734588Sml149210 		    cidp->vendor, cidp->device, cidp->chip_label,
23744588Sml149210 		    cidp->revision);
23751369Sdduvall #if	BGE_DEBUGGING
23761369Sdduvall 	else if (!sys_ok)
23771369Sdduvall 		bge_problem(bgep,
23784588Sml149210 		    "%d-based subsystem 'pci%04x,%04x' not validated",
23794588Sml149210 		    cidp->chip_label, cidp->subven, cidp->subdev);
23801369Sdduvall #endif
23811369Sdduvall 	else
23821369Sdduvall 		cidp->flags |= CHIP_FLAG_SUPPORTED;
23831865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
23841865Sdilpreet 		return (EIO);
23851865Sdilpreet 	return (0);
23861369Sdduvall }
23871369Sdduvall 
23881369Sdduvall void
23891369Sdduvall bge_chip_msi_trig(bge_t *bgep)
23901369Sdduvall {
23911369Sdduvall 	uint32_t	regval;
23921369Sdduvall 
23931369Sdduvall 	regval = bgep->param_msi_cnt<<4;
23941369Sdduvall 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
23951369Sdduvall 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
23961369Sdduvall }
23971369Sdduvall 
23981369Sdduvall /*
23991369Sdduvall  * Various registers that control the chip's internal engines (state
24001369Sdduvall  * machines) have a <reset> and <enable> bits (fortunately, in the
24011369Sdduvall  * same place in each such register :-).
24021369Sdduvall  *
24031369Sdduvall  * To reset the state machine, the <reset> bit must be written with 1;
24041369Sdduvall  * it will then read back as 1 while the reset is in progress, but
24051369Sdduvall  * self-clear to 0 when the reset completes.
24061369Sdduvall  *
24071369Sdduvall  * To enable a state machine, one must set the <enable> bit, which
24081369Sdduvall  * will continue to read back as 0 until the state machine is running.
24091369Sdduvall  *
24101369Sdduvall  * To disable a state machine, the <enable> bit must be cleared, but
24111369Sdduvall  * it will continue to read back as 1 until the state machine actually
24121369Sdduvall  * stops.
24131369Sdduvall  *
24141369Sdduvall  * This routine implements polling for completion of a reset, enable
24151369Sdduvall  * or disable operation, returning B_TRUE on success (bit reached the
24161369Sdduvall  * required state) or B_FALSE on timeout (200*100us == 20ms).
24171369Sdduvall  */
24181369Sdduvall static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
24191369Sdduvall 					uint32_t mask, uint32_t val);
24201369Sdduvall #pragma	no_inline(bge_chip_poll_engine)
24211369Sdduvall 
24221369Sdduvall static boolean_t
24231369Sdduvall bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
24241369Sdduvall 	uint32_t mask, uint32_t val)
24251369Sdduvall {
24261369Sdduvall 	uint32_t regval;
24271369Sdduvall 	uint32_t n;
24281369Sdduvall 
24291369Sdduvall 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
24304588Sml149210 	    (void *)bgep, regno, mask, val));
24311369Sdduvall 
24321369Sdduvall 	for (n = 200; n; --n) {
24331369Sdduvall 		regval = bge_reg_get32(bgep, regno);
24341369Sdduvall 		if ((regval & mask) == val)
24351369Sdduvall 			return (B_TRUE);
24361369Sdduvall 		drv_usecwait(100);
24371369Sdduvall 	}
24381369Sdduvall 
24391865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_NO_RESPONSE);
24401369Sdduvall 	return (B_FALSE);
24411369Sdduvall }
24421369Sdduvall 
24431369Sdduvall /*
24441369Sdduvall  * Various registers that control the chip's internal engines (state
24451369Sdduvall  * machines) have a <reset> bit (fortunately, in the same place in
24461369Sdduvall  * each such register :-).  To reset the state machine, this bit must
24471369Sdduvall  * be written with 1; it will then read back as 1 while the reset is
24481369Sdduvall  * in progress, but self-clear to 0 when the reset completes.
24491369Sdduvall  *
24501369Sdduvall  * This code sets the bit, then polls for it to read back as zero.
24511369Sdduvall  * The return value is B_TRUE on success (reset bit cleared itself),
24521369Sdduvall  * or B_FALSE if the state machine didn't recover :(
24531369Sdduvall  *
24541369Sdduvall  * NOTE: the Core reset is similar to other resets, except that we
24551369Sdduvall  * can't poll for completion, since the Core reset disables memory
24561369Sdduvall  * access!  So we just have to assume that it will all complete in
24571369Sdduvall  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
24581369Sdduvall  */
24591369Sdduvall static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
24601369Sdduvall #pragma	no_inline(bge_chip_reset_engine)
24611369Sdduvall 
24621369Sdduvall static boolean_t
24631369Sdduvall bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
24641369Sdduvall {
24651369Sdduvall 	uint32_t regval;
24661369Sdduvall 	uint32_t val32;
24671369Sdduvall 
24681369Sdduvall 	regval = bge_reg_get32(bgep, regno);
24691369Sdduvall 
24701369Sdduvall 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
24714588Sml149210 	    (void *)bgep, regno));
24721369Sdduvall 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
24734588Sml149210 	    regno, regval));
24741369Sdduvall 
24751369Sdduvall 	regval |= STATE_MACHINE_RESET_BIT;
24761369Sdduvall 
24771369Sdduvall 	switch (regno) {
24781369Sdduvall 	case MISC_CONFIG_REG:
24791369Sdduvall 		/*
24801369Sdduvall 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
24811369Sdduvall 		 * resetting PCIE block and bringing PCIE link down, bit 29
24821369Sdduvall 		 * in the register needs to be set first, and then set it again
24831369Sdduvall 		 * while the reset bit is written.
24841369Sdduvall 		 * See:P500 of 57xx-PG102-RDS.pdf.
24851369Sdduvall 		 */
24861369Sdduvall 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
24871369Sdduvall 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
24881369Sdduvall 		    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
24891369Sdduvall 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
24901369Sdduvall 			if (bgep->chipid.pci_type == BGE_PCI_E) {
24911369Sdduvall 				if (bgep->chipid.asic_rev ==
24921369Sdduvall 				    MHCR_CHIP_REV_5751_A0 ||
24931369Sdduvall 				    bgep->chipid.asic_rev ==
24944330Sml149210 				    MHCR_CHIP_REV_5721_A0 ||
24954330Sml149210 				    bgep->chipid.asic_rev ==
24964330Sml149210 				    MHCR_CHIP_REV_5755_A0) {
24971369Sdduvall 					val32 = bge_reg_get32(bgep,
24981369Sdduvall 					    PHY_TEST_CTRL_REG);
24991369Sdduvall 					if (val32 == (PHY_PCIE_SCRAM_MODE |
25001369Sdduvall 					    PHY_PCIE_LTASS_MODE))
25011369Sdduvall 						bge_reg_put32(bgep,
25021369Sdduvall 						    PHY_TEST_CTRL_REG,
25031369Sdduvall 						    PHY_PCIE_SCRAM_MODE);
25041369Sdduvall 					val32 = pci_config_get32
25051369Sdduvall 					    (bgep->cfg_handle,
25061369Sdduvall 					    PCI_CONF_BGE_CLKCTL);
25071369Sdduvall 					val32 |= CLKCTL_PCIE_A0_FIX;
25081369Sdduvall 					pci_config_put32(bgep->cfg_handle,
25091369Sdduvall 					    PCI_CONF_BGE_CLKCTL, val32);
25101369Sdduvall 				}
25111369Sdduvall 				bge_reg_set32(bgep, regno,
25124588Sml149210 				    MISC_CONFIG_GRC_RESET_DISABLE);
25131369Sdduvall 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
25141369Sdduvall 			}
25151369Sdduvall 		}
25161369Sdduvall 
25171369Sdduvall 		/*
25181369Sdduvall 		 * Special case - causes Core reset
25191369Sdduvall 		 *
25201369Sdduvall 		 * On SPARC v9 we want to ensure that we don't start
25211369Sdduvall 		 * timing until the I/O access has actually reached
25221369Sdduvall 		 * the chip, otherwise we might make the next access
25231369Sdduvall 		 * too early.  And we can't just force the write out
25241369Sdduvall 		 * by following it with a read (even to config space)
25251369Sdduvall 		 * because that would cause the fault we're trying
25261369Sdduvall 		 * to avoid.  Hence the need for membar_sync() here.
25271369Sdduvall 		 */
25281369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
25291369Sdduvall #ifdef	__sparcv9
25301369Sdduvall 		membar_sync();
25311369Sdduvall #endif	/* __sparcv9 */
25321369Sdduvall 		/*
25331369Sdduvall 		 * On some platforms,system need about 300us for
25341369Sdduvall 		 * link setup.
25351369Sdduvall 		 */
25361369Sdduvall 		drv_usecwait(300);
25371369Sdduvall 
25381369Sdduvall 		if (bgep->chipid.pci_type == BGE_PCI_E) {
25391369Sdduvall 			/* PCI-E device need more reset time */
25401369Sdduvall 			drv_usecwait(120000);
25411369Sdduvall 
25421369Sdduvall 			/* Set PCIE max payload size and clear error status. */
25432135Szh199473 			if ((bgep->chipid.chip_label == 5721) ||
25442135Szh199473 			    (bgep->chipid.chip_label == 5751) ||
25452675Szh199473 			    (bgep->chipid.chip_label == 5752) ||
25462135Szh199473 			    (bgep->chipid.chip_label == 5789)) {
25471369Sdduvall 				pci_config_put16(bgep->cfg_handle,
25484588Sml149210 				    PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
25491369Sdduvall 				pci_config_put16(bgep->cfg_handle,
25504588Sml149210 				    PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
25511369Sdduvall 			}
25521369Sdduvall 		}
25531369Sdduvall 
25541369Sdduvall 		BGE_PCICHK(bgep);
25551369Sdduvall 		return (B_TRUE);
25561369Sdduvall 
25571369Sdduvall 	default:
25581369Sdduvall 		bge_reg_put32(bgep, regno, regval);
25591369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
25601865Sdilpreet 		    STATE_MACHINE_RESET_BIT, 0));
25611369Sdduvall 	}
25621369Sdduvall }
25631369Sdduvall 
25641369Sdduvall /*
25651369Sdduvall  * Various registers that control the chip's internal engines (state
25661369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
25671369Sdduvall  * each such register :-).  To stop the state machine, this bit must
25681369Sdduvall  * be written with 0, then polled to see when the state machine has
25691369Sdduvall  * actually stopped.
25701369Sdduvall  *
25711369Sdduvall  * The return value is B_TRUE on success (enable bit cleared), or
25721369Sdduvall  * B_FALSE if the state machine didn't stop :(
25731369Sdduvall  */
25741369Sdduvall static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
25751369Sdduvall 						uint32_t morebits);
25761369Sdduvall #pragma	no_inline(bge_chip_disable_engine)
25771369Sdduvall 
25781369Sdduvall static boolean_t
25791369Sdduvall bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
25801369Sdduvall {
25811369Sdduvall 	uint32_t regval;
25821369Sdduvall 
25831369Sdduvall 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
25844588Sml149210 	    (void *)bgep, regno, morebits));
25851369Sdduvall 
25861369Sdduvall 	switch (regno) {
25871369Sdduvall 	case FTQ_RESET_REG:
25881369Sdduvall 		/*
25893918Sml149210 		 * For Schumacher's bugfix CR6490108
25903918Sml149210 		 */
25913918Sml149210 #ifdef BGE_IPMI_ASF
25923918Sml149210 #ifdef BGE_NETCONSOLE
25933918Sml149210 		if (bgep->asf_enabled)
25943918Sml149210 			return (B_TRUE);
25953918Sml149210 #endif
25963918Sml149210 #endif
25973918Sml149210 		/*
25981369Sdduvall 		 * Not quite like the others; it doesn't
25991369Sdduvall 		 * have an <enable> bit, but instead we
26001369Sdduvall 		 * have to set and then clear all the bits
26011369Sdduvall 		 */
26021369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
26031369Sdduvall 		drv_usecwait(100);
26041369Sdduvall 		bge_reg_put32(bgep, regno, 0);
26051369Sdduvall 		return (B_TRUE);
26061369Sdduvall 
26071369Sdduvall 	default:
26081369Sdduvall 		regval = bge_reg_get32(bgep, regno);
26091369Sdduvall 		regval &= ~STATE_MACHINE_ENABLE_BIT;
26101369Sdduvall 		regval &= ~morebits;
26111369Sdduvall 		bge_reg_put32(bgep, regno, regval);
26121369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
26131865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, 0));
26141369Sdduvall 	}
26151369Sdduvall }
26161369Sdduvall 
26171369Sdduvall /*
26181369Sdduvall  * Various registers that control the chip's internal engines (state
26191369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
26201369Sdduvall  * each such register :-).  To start the state machine, this bit must
26211369Sdduvall  * be written with 1, then polled to see when the state machine has
26221369Sdduvall  * actually started.
26231369Sdduvall  *
26241369Sdduvall  * The return value is B_TRUE on success (enable bit set), or
26251369Sdduvall  * B_FALSE if the state machine didn't start :(
26261369Sdduvall  */
26271369Sdduvall static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
26281369Sdduvall 					uint32_t morebits);
26291369Sdduvall #pragma	no_inline(bge_chip_enable_engine)
26301369Sdduvall 
26311369Sdduvall static boolean_t
26321369Sdduvall bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
26331369Sdduvall {
26341369Sdduvall 	uint32_t regval;
26351369Sdduvall 
26361369Sdduvall 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
26374588Sml149210 	    (void *)bgep, regno, morebits));
26381369Sdduvall 
26391369Sdduvall 	switch (regno) {
26401369Sdduvall 	case FTQ_RESET_REG:
26413918Sml149210 #ifdef BGE_IPMI_ASF
26423918Sml149210 #ifdef BGE_NETCONSOLE
26433918Sml149210 		if (bgep->asf_enabled)
26443918Sml149210 			return (B_TRUE);
26453918Sml149210 #endif
26463918Sml149210 #endif
26471369Sdduvall 		/*
26481369Sdduvall 		 * Not quite like the others; it doesn't
26491369Sdduvall 		 * have an <enable> bit, but instead we
26501369Sdduvall 		 * have to set and then clear all the bits
26511369Sdduvall 		 */
26521369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
26531369Sdduvall 		drv_usecwait(100);
26541369Sdduvall 		bge_reg_put32(bgep, regno, 0);
26551369Sdduvall 		return (B_TRUE);
26561369Sdduvall 
26571369Sdduvall 	default:
26581369Sdduvall 		regval = bge_reg_get32(bgep, regno);
26591369Sdduvall 		regval |= STATE_MACHINE_ENABLE_BIT;
26601369Sdduvall 		regval |= morebits;
26611369Sdduvall 		bge_reg_put32(bgep, regno, regval);
26621369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
26631865Sdilpreet 		    STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
26641369Sdduvall 	}
26651369Sdduvall }
26661369Sdduvall 
26671369Sdduvall /*
26681369Sdduvall  * Reprogram the Ethernet, Transmit, and Receive MAC
26691369Sdduvall  * modes to match the param_* variables
26701369Sdduvall  */
26715903Ssowmini void bge_sync_mac_modes(bge_t *bgep);
26721369Sdduvall #pragma	no_inline(bge_sync_mac_modes)
26731369Sdduvall 
26745903Ssowmini void
26751369Sdduvall bge_sync_mac_modes(bge_t *bgep)
26761369Sdduvall {
26771369Sdduvall 	uint32_t macmode;
26781369Sdduvall 	uint32_t regval;
26791369Sdduvall 
26801369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
26811369Sdduvall 
26821369Sdduvall 	/*
26831369Sdduvall 	 * Reprogram the Ethernet MAC mode ...
26841369Sdduvall 	 */
26851369Sdduvall 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
26861369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
26874588Sml149210 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
26881369Sdduvall 		macmode &= ~ETHERNET_MODE_LINK_POLARITY;
26891369Sdduvall 	else
26901369Sdduvall 		macmode |= ETHERNET_MODE_LINK_POLARITY;
26911369Sdduvall 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
26921369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
26934588Sml149210 	    (bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
26941369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_TBI;
26951369Sdduvall 	else if (bgep->param_link_speed == 10 || bgep->param_link_speed == 100)
26961369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_MII;
26971369Sdduvall 	else
26981369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
26991369Sdduvall 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
27001369Sdduvall 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
27011369Sdduvall 	else
27021369Sdduvall 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
27031369Sdduvall 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
27041369Sdduvall 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
27051369Sdduvall 	else
27061369Sdduvall 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
27071369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
27081369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
27094588Sml149210 	    (void *)bgep, regval, macmode));
27101369Sdduvall 
27111369Sdduvall 	/*
27121369Sdduvall 	 * ... the Transmit MAC mode ...
27131369Sdduvall 	 */
27141369Sdduvall 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
27151369Sdduvall 	if (bgep->param_link_tx_pause)
27161369Sdduvall 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
27171369Sdduvall 	else
27181369Sdduvall 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
27191369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
27201369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
27214588Sml149210 	    (void *)bgep, regval, macmode));
27221369Sdduvall 
27231369Sdduvall 	/*
27241369Sdduvall 	 * ... and the Receive MAC mode
27251369Sdduvall 	 */
27261369Sdduvall 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
27271369Sdduvall 	if (bgep->param_link_rx_pause)
27281369Sdduvall 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
27291369Sdduvall 	else
27301369Sdduvall 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
27311369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
27321369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
27334588Sml149210 	    (void *)bgep, regval, macmode));
27341369Sdduvall }
27351369Sdduvall 
27361369Sdduvall /*
27371369Sdduvall  * bge_chip_sync() -- program the chip with the unicast MAC address,
27381369Sdduvall  * the multicast hash table, the required level of promiscuity, and
27391369Sdduvall  * the current loopback mode ...
27401369Sdduvall  */
27411408Srandyf #ifdef BGE_IPMI_ASF
27421865Sdilpreet int bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
27431408Srandyf #else
27441865Sdilpreet int bge_chip_sync(bge_t *bgep);
27451408Srandyf #endif
27461369Sdduvall #pragma	no_inline(bge_chip_sync)
27471369Sdduvall 
27481865Sdilpreet int
27491408Srandyf #ifdef BGE_IPMI_ASF
27501408Srandyf bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
27511408Srandyf #else
27521369Sdduvall bge_chip_sync(bge_t *bgep)
27531408Srandyf #endif
27541369Sdduvall {
27551369Sdduvall 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
27561369Sdduvall 	boolean_t promisc;
27571369Sdduvall 	uint64_t macaddr;
27581369Sdduvall 	uint32_t fill;
27592331Skrgopi 	int i, j;
27601865Sdilpreet 	int retval = DDI_SUCCESS;
27611369Sdduvall 
27621369Sdduvall 	BGE_TRACE(("bge_chip_sync($%p)",
27635903Ssowmini 	    (void *)bgep));
27641369Sdduvall 
27651369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
27661369Sdduvall 
27671369Sdduvall 	promisc = B_FALSE;
27681369Sdduvall 	fill = ~(uint32_t)0;
27691369Sdduvall 
27701369Sdduvall 	if (bgep->promisc)
27711369Sdduvall 		promisc = B_TRUE;
27721369Sdduvall 	else
27731369Sdduvall 		fill = (uint32_t)0;
27741369Sdduvall 
27751369Sdduvall 	/*
27761369Sdduvall 	 * If the TX/RX MAC engines are already running, we should stop
27771369Sdduvall 	 * them (and reset the RX engine) before changing the parameters.
27781369Sdduvall 	 * If they're not running, this will have no effect ...
27791369Sdduvall 	 *
27801369Sdduvall 	 * NOTE: this is currently disabled by default because stopping
27811369Sdduvall 	 * and restarting the Tx engine may cause an outgoing packet in
27821369Sdduvall 	 * transit to be truncated.  Also, stopping and restarting the
27831369Sdduvall 	 * Rx engine seems to not work correctly on the 5705.  Testing
27841369Sdduvall 	 * has not (yet!) revealed any problems with NOT stopping and
27851369Sdduvall 	 * restarting these engines (and Broadcom say their drivers don't
27861369Sdduvall 	 * do this), but if it is found to cause problems, this variable
27871369Sdduvall 	 * can be patched to re-enable the old behaviour ...
27881369Sdduvall 	 */
27891369Sdduvall 	if (bge_stop_start_on_sync) {
27901408Srandyf #ifdef BGE_IPMI_ASF
27911865Sdilpreet 		if (!bgep->asf_enabled) {
27921865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
27931865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
27941865Sdilpreet 				retval = DDI_FAILURE;
27951408Srandyf 		} else {
27961865Sdilpreet 			if (!bge_chip_disable_engine(bgep,
27971865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
27981865Sdilpreet 				retval = DDI_FAILURE;
27991408Srandyf 		}
28001408Srandyf #else
28011865Sdilpreet 		if (!bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
28021865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
28031865Sdilpreet 			retval = DDI_FAILURE;
28041408Srandyf #endif
28051865Sdilpreet 		if (!bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
28061865Sdilpreet 			retval = DDI_FAILURE;
28071865Sdilpreet 		if (!bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG))
28081865Sdilpreet 			retval = DDI_FAILURE;
28091369Sdduvall 	}
28101369Sdduvall 
28111369Sdduvall 	/*
28121369Sdduvall 	 * Reprogram the hashed multicast address table ...
28131369Sdduvall 	 */
28141369Sdduvall 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
2815*6546Sgh162552 		bge_reg_put32(bgep, MAC_HASH_REG(i), 0);
2816*6546Sgh162552 
2817*6546Sgh162552 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
28181369Sdduvall 		bge_reg_put32(bgep, MAC_HASH_REG(i),
28191369Sdduvall 			bgep->mcast_hash[i] | fill);
28201369Sdduvall 
28211408Srandyf #ifdef BGE_IPMI_ASF
28221408Srandyf 	if (!bgep->asf_enabled || !asf_keeplive) {
28231408Srandyf #endif
28241408Srandyf 		/*
28252331Skrgopi 		 * Transform the MAC address(es) from host to chip format, then
28261408Srandyf 		 * reprogram the transmit random backoff seed and the unicast
28271408Srandyf 		 * MAC address(es) ...
28281408Srandyf 		 */
28292331Skrgopi 		for (j = 0; j < MAC_ADDRESS_REGS_MAX; j++) {
28302331Skrgopi 			for (i = 0, fill = 0, macaddr = 0ull;
28312331Skrgopi 			    i < ETHERADDRL; ++i) {
28322331Skrgopi 				macaddr <<= 8;
28332331Skrgopi 				macaddr |= bgep->curr_addr[j].addr[i];
28342331Skrgopi 				fill += bgep->curr_addr[j].addr[i];
28352331Skrgopi 			}
28362331Skrgopi 			bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
28372331Skrgopi 			bge_reg_put64(bgep, MAC_ADDRESS_REG(j), macaddr);
28381408Srandyf 		}
28391408Srandyf 
28401408Srandyf 		BGE_DEBUG(("bge_chip_sync($%p) setting MAC address %012llx",
28411408Srandyf 			(void *)bgep, macaddr));
28421408Srandyf #ifdef BGE_IPMI_ASF
28431369Sdduvall 	}
28441408Srandyf #endif
28451369Sdduvall 
28461369Sdduvall 	/*
28471369Sdduvall 	 * Set or clear the PROMISCUOUS mode bit
28481369Sdduvall 	 */
28491369Sdduvall 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
28501369Sdduvall 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
28511369Sdduvall 
28521369Sdduvall 	/*
28531369Sdduvall 	 * Sync the rest of the MAC modes too ...
28541369Sdduvall 	 */
28551369Sdduvall 	bge_sync_mac_modes(bgep);
28561369Sdduvall 
28571369Sdduvall 	/*
28581369Sdduvall 	 * Restart RX/TX MAC engines if required ...
28591369Sdduvall 	 */
28601369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
28611865Sdilpreet 		if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
28621865Sdilpreet 			retval = DDI_FAILURE;
28631408Srandyf #ifdef BGE_IPMI_ASF
28641865Sdilpreet 		if (!bgep->asf_enabled) {
28651865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
28661865Sdilpreet 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG))
28671865Sdilpreet 				retval = DDI_FAILURE;
28681408Srandyf 		} else {
28691865Sdilpreet 			if (!bge_chip_enable_engine(bgep,
28701865Sdilpreet 			    RECEIVE_MAC_MODE_REG, 0))
28711865Sdilpreet 				retval = DDI_FAILURE;
28721408Srandyf 		}
28731408Srandyf #else
28741865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
28751865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
28761865Sdilpreet 			retval = DDI_FAILURE;
28771408Srandyf #endif
28781369Sdduvall 	}
28791865Sdilpreet 	return (retval);
28801369Sdduvall }
28811369Sdduvall 
28821369Sdduvall /*
28831369Sdduvall  * This array defines the sequence of state machine control registers
28841369Sdduvall  * in which the <enable> bit must be cleared to bring the chip to a
28851369Sdduvall  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
28861369Sdduvall  */
28871369Sdduvall static bge_regno_t shutdown_engine_regs[] = {
28881369Sdduvall 	RECEIVE_MAC_MODE_REG,
28891369Sdduvall 	RCV_BD_INITIATOR_MODE_REG,
28901369Sdduvall 	RCV_LIST_PLACEMENT_MODE_REG,
28911369Sdduvall 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
28921369Sdduvall 	RCV_DATA_BD_INITIATOR_MODE_REG,
28931369Sdduvall 	RCV_DATA_COMPLETION_MODE_REG,
28941369Sdduvall 	RCV_BD_COMPLETION_MODE_REG,
28951369Sdduvall 
28961369Sdduvall 	SEND_BD_SELECTOR_MODE_REG,
28971369Sdduvall 	SEND_BD_INITIATOR_MODE_REG,
28981369Sdduvall 	SEND_DATA_INITIATOR_MODE_REG,
28991369Sdduvall 	READ_DMA_MODE_REG,
29001369Sdduvall 	SEND_DATA_COMPLETION_MODE_REG,
29011369Sdduvall 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
29021369Sdduvall 	SEND_BD_COMPLETION_MODE_REG,
29031369Sdduvall 	TRANSMIT_MAC_MODE_REG,
29041369Sdduvall 
29051369Sdduvall 	HOST_COALESCE_MODE_REG,
29061369Sdduvall 	WRITE_DMA_MODE_REG,
29071369Sdduvall 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
29081369Sdduvall 	FTQ_RESET_REG,		/* special - see code	*/
29091369Sdduvall 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
29101369Sdduvall 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
29111369Sdduvall 	BGE_REGNO_NONE		/* terminator		*/
29121369Sdduvall };
29131369Sdduvall 
29141369Sdduvall /*
29151369Sdduvall  * bge_chip_stop() -- stop all chip processing
29161369Sdduvall  *
29171369Sdduvall  * If the <fault> parameter is B_TRUE, we're stopping the chip because
29181369Sdduvall  * we've detected a problem internally; otherwise, this is a normal
29191369Sdduvall  * (clean) stop (at user request i.e. the last STREAM has been closed).
29201369Sdduvall  */
29211369Sdduvall void bge_chip_stop(bge_t *bgep, boolean_t fault);
29221369Sdduvall #pragma	no_inline(bge_chip_stop)
29231369Sdduvall 
29241369Sdduvall void
29251369Sdduvall bge_chip_stop(bge_t *bgep, boolean_t fault)
29261369Sdduvall {
29271369Sdduvall 	bge_regno_t regno;
29281369Sdduvall 	bge_regno_t *rbp;
29291369Sdduvall 	boolean_t ok;
29301369Sdduvall 
29311369Sdduvall 	BGE_TRACE(("bge_chip_stop($%p)",
29324588Sml149210 	    (void *)bgep));
29331369Sdduvall 
29341369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
29351369Sdduvall 
29361369Sdduvall 	rbp = shutdown_engine_regs;
29371369Sdduvall 	/*
29381369Sdduvall 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
29391369Sdduvall 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
29401369Sdduvall 	 * -ory arbiter should not be disabled.
29411369Sdduvall 	 */
29421369Sdduvall 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
29431369Sdduvall 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
29444588Sml149210 				ok &= bge_chip_disable_engine(bgep, regno, 0);
29451369Sdduvall 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
29464588Sml149210 			    (regno != DMA_COMPLETION_MODE_REG) &&
29474588Sml149210 			    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
29484588Sml149210 			    (regno != BUFFER_MANAGER_MODE_REG) &&
29494588Sml149210 			    (regno != MEMORY_ARBITER_MODE_REG))
29504588Sml149210 				ok &= bge_chip_disable_engine(bgep,
29514588Sml149210 				    regno, 0);
29521369Sdduvall 	}
29531369Sdduvall 
29541865Sdilpreet 	if (!ok && !fault)
29551865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
29561865Sdilpreet 
29571369Sdduvall 	/*
29581369Sdduvall 	 * Finally, disable (all) MAC events & clear the MAC status
29591369Sdduvall 	 */
29601369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
29611369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
29621369Sdduvall 
29631369Sdduvall 	/*
29641865Sdilpreet 	 * if we're stopping the chip because of a detected fault then do
29651865Sdilpreet 	 * appropriate actions
29661369Sdduvall 	 */
29671865Sdilpreet 	if (fault) {
29681865Sdilpreet 		if (bgep->bge_chip_state != BGE_CHIP_FAULT) {
29691865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_FAULT;
29705903Ssowmini 			if (!bgep->manual_reset)
29715903Ssowmini 				ddi_fm_service_impact(bgep->devinfo,
29725903Ssowmini 				    DDI_SERVICE_LOST);
29731865Sdilpreet 			if (bgep->bge_dma_error) {
29741865Sdilpreet 				/*
29751865Sdilpreet 				 * need to free buffers in case the fault was
29761865Sdilpreet 				 * due to a memory error in a buffer - got to
29771865Sdilpreet 				 * do a fair bit of tidying first
29781865Sdilpreet 				 */
29791865Sdilpreet 				if (bgep->progress & PROGRESS_KSTATS) {
29801865Sdilpreet 					bge_fini_kstats(bgep);
29811865Sdilpreet 					bgep->progress &= ~PROGRESS_KSTATS;
29821865Sdilpreet 				}
29831865Sdilpreet 				if (bgep->progress & PROGRESS_INTR) {
29841865Sdilpreet 					bge_intr_disable(bgep);
29851865Sdilpreet 					rw_enter(bgep->errlock, RW_WRITER);
29861865Sdilpreet 					bge_fini_rings(bgep);
29871865Sdilpreet 					rw_exit(bgep->errlock);
29881865Sdilpreet 					bgep->progress &= ~PROGRESS_INTR;
29891865Sdilpreet 				}
29901865Sdilpreet 				if (bgep->progress & PROGRESS_BUFS) {
29911865Sdilpreet 					bge_free_bufs(bgep);
29921865Sdilpreet 					bgep->progress &= ~PROGRESS_BUFS;
29931865Sdilpreet 				}
29941865Sdilpreet 				bgep->bge_dma_error = B_FALSE;
29951865Sdilpreet 			}
29961865Sdilpreet 		}
29971865Sdilpreet 	} else
29981369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
29991369Sdduvall }
30001369Sdduvall 
30011369Sdduvall /*
30021369Sdduvall  * Poll for completion of chip's ROM firmware; also, at least on the
30031369Sdduvall  * first time through, find and return the hardware MAC address, if any.
30041369Sdduvall  */
30051369Sdduvall static uint64_t bge_poll_firmware(bge_t *bgep);
30061369Sdduvall #pragma	no_inline(bge_poll_firmware)
30071369Sdduvall 
30081369Sdduvall static uint64_t
30091369Sdduvall bge_poll_firmware(bge_t *bgep)
30101369Sdduvall {
30111369Sdduvall 	uint64_t magic;
30121369Sdduvall 	uint64_t mac;
30131369Sdduvall 	uint32_t gen;
30141369Sdduvall 	uint32_t i;
30151369Sdduvall 
30161369Sdduvall 	/*
30171369Sdduvall 	 * Step 19: poll for firmware completion (GENCOMM port set
30181369Sdduvall 	 * to the ones complement of T3_MAGIC_NUMBER).
30191369Sdduvall 	 *
30201369Sdduvall 	 * While we're at it, we also read the MAC address register;
30212135Szh199473 	 * at some stage the firmware will load this with the
30221369Sdduvall 	 * factory-set value.
30231369Sdduvall 	 *
30241369Sdduvall 	 * When both the magic number and the MAC address are set,
30251369Sdduvall 	 * we're done; but we impose a time limit of one second
30261369Sdduvall 	 * (1000*1000us) in case the firmware fails in some fashion
30271369Sdduvall 	 * or the SEEPROM that provides that MAC address isn't fitted.
30281369Sdduvall 	 *
30291369Sdduvall 	 * After the first time through (chip state != INITIAL), we
30301369Sdduvall 	 * don't need the MAC address to be set (we've already got it
30311369Sdduvall 	 * or not, from the first time), so we don't wait for it, but
30321369Sdduvall 	 * we still have to wait for the T3_MAGIC_NUMBER.
30331369Sdduvall 	 *
30341369Sdduvall 	 * Note: the magic number is only a 32-bit quantity, but the NIC
30351369Sdduvall 	 * memory is 64-bit (and big-endian) internally.  Addressing the
30361369Sdduvall 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
30371369Sdduvall 	 * it work correctly on both big- and little-endian hosts.
30381369Sdduvall 	 */
30391369Sdduvall 	for (i = 0; i < 1000; ++i) {
30401369Sdduvall 		drv_usecwait(1000);
30411369Sdduvall 		gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
30421369Sdduvall 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
30431408Srandyf #ifdef BGE_IPMI_ASF
30441408Srandyf 		if (!bgep->asf_enabled) {
30451408Srandyf #endif
30461408Srandyf 			if (gen != ~T3_MAGIC_NUMBER)
30471408Srandyf 				continue;
30481408Srandyf #ifdef BGE_IPMI_ASF
30491408Srandyf 		}
30501408Srandyf #endif
30511369Sdduvall 		if (mac != 0ULL)
30521369Sdduvall 			break;
30531369Sdduvall 		if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
30541369Sdduvall 			break;
30551369Sdduvall 	}
30561369Sdduvall 
30571369Sdduvall 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
30581369Sdduvall 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
30594588Sml149210 	    (void *)bgep, gen, i));
30601369Sdduvall 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
30614588Sml149210 	    mac, magic));
30621369Sdduvall 
30631369Sdduvall 	return (mac);
30641369Sdduvall }
30651369Sdduvall 
30663390Szh199473 /*
30673390Szh199473  * Maximum times of trying to get the NVRAM access lock
30683390Szh199473  * by calling bge_nvmem_acquire()
30693390Szh199473  */
30703390Szh199473 #define	MAX_TRY_NVMEM_ACQUIRE	10000
30713390Szh199473 
30721408Srandyf #ifdef BGE_IPMI_ASF
30731865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
30741408Srandyf #else
30751865Sdilpreet int bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
30761408Srandyf #endif
30771369Sdduvall #pragma	no_inline(bge_chip_reset)
30781369Sdduvall 
30791865Sdilpreet int
30801408Srandyf #ifdef BGE_IPMI_ASF
30811408Srandyf bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
30821408Srandyf #else
30831369Sdduvall bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
30841408Srandyf #endif
30851369Sdduvall {
30861369Sdduvall 	chip_id_t chipid;
30871369Sdduvall 	uint64_t mac;
30881908Sly149593 	uint64_t magic;
30891369Sdduvall 	uint32_t modeflags;
30901369Sdduvall 	uint32_t mhcr;
30911369Sdduvall 	uint32_t sx0;
30923390Szh199473 	uint32_t i, tries;
30931408Srandyf #ifdef BGE_IPMI_ASF
30941408Srandyf 	uint32_t mailbox;
30951408Srandyf #endif
30961865Sdilpreet 	int retval = DDI_SUCCESS;
30971369Sdduvall 
30981369Sdduvall 	BGE_TRACE(("bge_chip_reset($%p, %d)",
30991369Sdduvall 		(void *)bgep, enable_dma));
31001369Sdduvall 
31011369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
31021369Sdduvall 
31031369Sdduvall 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
31041369Sdduvall 		(void *)bgep, enable_dma, bgep->bge_chip_state));
31051369Sdduvall 
31061369Sdduvall 	/*
31071369Sdduvall 	 * Do we need to stop the chip cleanly before resetting?
31081369Sdduvall 	 */
31091369Sdduvall 	switch (bgep->bge_chip_state) {
31101369Sdduvall 	default:
31111369Sdduvall 		_NOTE(NOTREACHED)
31121865Sdilpreet 		return (DDI_FAILURE);
31131369Sdduvall 
31141369Sdduvall 	case BGE_CHIP_INITIAL:
31151369Sdduvall 	case BGE_CHIP_STOPPED:
31161369Sdduvall 	case BGE_CHIP_RESET:
31171369Sdduvall 		break;
31181369Sdduvall 
31191369Sdduvall 	case BGE_CHIP_RUNNING:
31201369Sdduvall 	case BGE_CHIP_ERROR:
31211369Sdduvall 	case BGE_CHIP_FAULT:
31221369Sdduvall 		bge_chip_stop(bgep, B_FALSE);
31231369Sdduvall 		break;
31241369Sdduvall 	}
31251369Sdduvall 
31261408Srandyf #ifdef BGE_IPMI_ASF
31271408Srandyf 	if (bgep->asf_enabled) {
31283918Sml149210 #ifdef __sparc
31293918Sml149210 		mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
31303918Sml149210 			MHCR_ENABLE_TAGGED_STATUS_MODE |
31313918Sml149210 			MHCR_MASK_INTERRUPT_MODE |
31323918Sml149210 			MHCR_MASK_PCI_INT_OUTPUT |
31333918Sml149210 			MHCR_CLEAR_INTERRUPT_INTA |
31343918Sml149210 			MHCR_ENABLE_ENDIAN_WORD_SWAP |
31353918Sml149210 			MHCR_ENABLE_ENDIAN_BYTE_SWAP;
31363918Sml149210 		pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
31373918Sml149210 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
31383918Sml149210 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG) |
31393918Sml149210 			MEMORY_ARBITER_ENABLE);
31403918Sml149210 #endif
31411408Srandyf 		if (asf_mode == ASF_MODE_INIT) {
31421408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
31431408Srandyf 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
31441408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
31451408Srandyf 		}
31461408Srandyf 	}
31471408Srandyf #endif
31481369Sdduvall 	/*
31491369Sdduvall 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
31501369Sdduvall 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
31511369Sdduvall 	 *
31521369Sdduvall 	 * Before reset Core clock,it is
31531369Sdduvall 	 * also required to initialize the Memory Arbiter as specified in step9
31541369Sdduvall 	 * and Misc Host Control Register as specified in step-13
31551369Sdduvall 	 * Step 4-5: reset Core clock & wait for completion
31561369Sdduvall 	 * Steps 6-8: are done by bge_chip_cfg_init()
31571908Sly149593 	 * put the T3_MAGIC_NUMBER into the GENCOMM port before reset
31581369Sdduvall 	 */
31591865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
31601865Sdilpreet 		retval = DDI_FAILURE;
31611369Sdduvall 
31621369Sdduvall 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
31631369Sdduvall 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
31641369Sdduvall 	    MHCR_MASK_INTERRUPT_MODE |
31651369Sdduvall 	    MHCR_MASK_PCI_INT_OUTPUT |
31661369Sdduvall 	    MHCR_CLEAR_INTERRUPT_INTA;
31671369Sdduvall #ifdef  _BIG_ENDIAN
31681369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
31691369Sdduvall #endif  /* _BIG_ENDIAN */
31701369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
31711408Srandyf #ifdef BGE_IPMI_ASF
31721408Srandyf 	if (bgep->asf_enabled)
31731408Srandyf 		bgep->asf_wordswapped = B_FALSE;
31741408Srandyf #endif
31752675Szh199473 	/*
31762675Szh199473 	 * NVRAM Corruption Workaround
31772675Szh199473 	 */
31783390Szh199473 	for (tries = 0; tries < MAX_TRY_NVMEM_ACQUIRE; tries++)
31793534Szh199473 		if (bge_nvmem_acquire(bgep) != EAGAIN)
31802675Szh199473 			break;
31813440Szh199473 	if (tries >= MAX_TRY_NVMEM_ACQUIRE)
31822675Szh199473 		BGE_DEBUG(("%s: fail to acquire nvram lock",
31832675Szh199473 			bgep->ifname));
31842675Szh199473 
31851908Sly149593 #ifdef BGE_IPMI_ASF
31861908Sly149593 	if (!bgep->asf_enabled) {
31871908Sly149593 #endif
31881908Sly149593 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
31891908Sly149593 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
31901908Sly149593 #ifdef BGE_IPMI_ASF
31911908Sly149593 	}
31921908Sly149593 #endif
31931908Sly149593 
31941865Sdilpreet 	if (!bge_chip_reset_engine(bgep, MISC_CONFIG_REG))
31951865Sdilpreet 		retval = DDI_FAILURE;
31961369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
31971369Sdduvall 
31981369Sdduvall 	/*
31991369Sdduvall 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
32001369Sdduvall 	 * a bit set to avoid a fifo overflow/underflow bug.
32011369Sdduvall 	 */
32022135Szh199473 	if ((bgep->chipid.chip_label == 5721) ||
32032135Szh199473 		(bgep->chipid.chip_label == 5751) ||
32042675Szh199473 		(bgep->chipid.chip_label == 5752) ||
32054330Sml149210 		(bgep->chipid.chip_label == 5755) ||
32062135Szh199473 		(bgep->chipid.chip_label == 5789))
32071369Sdduvall 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
32081369Sdduvall 
32091369Sdduvall 
32101369Sdduvall 	/*
32111369Sdduvall 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
32121369Sdduvall 	 * not be changed.
32131369Sdduvall 	 */
32141865Sdilpreet 	if (!bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0))
32151865Sdilpreet 		retval = DDI_FAILURE;
32161369Sdduvall 
32171369Sdduvall 	/*
32181369Sdduvall 	 * Steps 10-11: configure PIO endianness options and
32191369Sdduvall 	 * enable indirect register access -- already done
32201369Sdduvall 	 * Steps 12-13: enable writing to the PCI state & clock
32211369Sdduvall 	 * control registers -- not required; we aren't going to
32221369Sdduvall 	 * use those features.
32231369Sdduvall 	 * Steps 14-15: Configure DMA endianness options.  See
32241369Sdduvall 	 * the comments on the setting of the MHCR above.
32251369Sdduvall 	 */
32261369Sdduvall #ifdef	_BIG_ENDIAN
32271369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
32281369Sdduvall 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
32291369Sdduvall #else
32301369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
32311369Sdduvall #endif	/* _BIG_ENDIAN */
32321408Srandyf #ifdef BGE_IPMI_ASF
32331408Srandyf 	if (bgep->asf_enabled)
32341408Srandyf 		modeflags |= MODE_HOST_STACK_UP;
32351408Srandyf #endif
32361369Sdduvall 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
32371369Sdduvall 
32381408Srandyf #ifdef BGE_IPMI_ASF
32391408Srandyf 	if (bgep->asf_enabled) {
32403918Sml149210 #ifdef __sparc
32413918Sml149210 		bge_reg_put32(bgep, MEMORY_ARBITER_MODE_REG,
32423918Sml149210 			MEMORY_ARBITER_ENABLE |
32433918Sml149210 			bge_reg_get32(bgep, MEMORY_ARBITER_MODE_REG));
32443918Sml149210 #endif
32453918Sml149210 
32463918Sml149210 #ifdef  BGE_NETCONSOLE
32473918Sml149210 		if (!bgep->asf_newhandshake) {
32483918Sml149210 			if ((asf_mode == ASF_MODE_INIT) ||
32493918Sml149210 			(asf_mode == ASF_MODE_POST_INIT)) {
32503918Sml149210 				bge_asf_post_reset_old_mode(bgep,
32513918Sml149210 					BGE_INIT_RESET);
32523918Sml149210 			} else {
32533918Sml149210 				bge_asf_post_reset_old_mode(bgep,
32543918Sml149210 					BGE_SHUTDOWN_RESET);
32551408Srandyf 			}
32561408Srandyf 		}
32573918Sml149210 #endif
32583918Sml149210 
32593918Sml149210 		/* Wait for NVRAM init */
32603918Sml149210 		i = 0;
32613918Sml149210 		drv_usecwait(5000);
32623918Sml149210 		mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
32633918Sml149210 
32643918Sml149210 		while ((mailbox != (uint32_t)
32653918Sml149210 			~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
32663918Sml149210 			(i < 10000)) {
32673918Sml149210 			drv_usecwait(100);
32683918Sml149210 			mailbox = bge_nic_get32(bgep,
32693918Sml149210 				BGE_FIRMWARE_MAILBOX);
32703918Sml149210 			i++;
32713918Sml149210 		}
32723918Sml149210 
32733918Sml149210 #ifndef BGE_NETCONSOLE
32743918Sml149210 		if (!bgep->asf_newhandshake) {
32753918Sml149210 			if ((asf_mode == ASF_MODE_INIT) ||
32763918Sml149210 				(asf_mode == ASF_MODE_POST_INIT)) {
32773918Sml149210 
32783918Sml149210 				bge_asf_post_reset_old_mode(bgep,
32793918Sml149210 					BGE_INIT_RESET);
32803918Sml149210 			} else {
32813918Sml149210 				bge_asf_post_reset_old_mode(bgep,
32823918Sml149210 					BGE_SHUTDOWN_RESET);
32833918Sml149210 			}
32843918Sml149210 		}
32853918Sml149210 #endif
32861408Srandyf 	}
32871408Srandyf #endif
32881369Sdduvall 	/*
32891369Sdduvall 	 * Steps 16-17: poll for firmware completion
32901369Sdduvall 	 */
32911369Sdduvall 	mac = bge_poll_firmware(bgep);
32921369Sdduvall 
32931369Sdduvall 	/*
32941369Sdduvall 	 * Step 18: enable external memory -- doesn't apply.
32951369Sdduvall 	 *
32961369Sdduvall 	 * However we take the opportunity to set the MLCR anyway, as
32971369Sdduvall 	 * this register also controls the SEEPROM auto-access method
32981369Sdduvall 	 * which we may want to use later ...
32991369Sdduvall 	 *
33001369Sdduvall 	 * The proper value here depends on the way the chip is wired
33011369Sdduvall 	 * into the circuit board, as this register *also* controls which
33021369Sdduvall 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
33031369Sdduvall 	 * values driven onto those pins!
33041369Sdduvall 	 *
33051369Sdduvall 	 * See also step 74 in the PRM ...
33061369Sdduvall 	 */
33071369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
33081369Sdduvall 	    bgep->chipid.bge_mlcr_default);
33091369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
33101369Sdduvall 
33111369Sdduvall 	/*
33121369Sdduvall 	 * Step 20: clear the Ethernet MAC mode register
33131369Sdduvall 	 */
33141369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
33151369Sdduvall 
33161369Sdduvall 	/*
33171369Sdduvall 	 * Step 21: restore cache-line-size, latency timer, and
33181369Sdduvall 	 * subsystem ID registers to their original values (not
33191369Sdduvall 	 * those read into the local structure <chipid>, 'cos
33201369Sdduvall 	 * that was after they were cleared by the RESET).
33211369Sdduvall 	 *
33221369Sdduvall 	 * Note: the Subsystem Vendor/Device ID registers are not
33231369Sdduvall 	 * directly writable in config space, so we use the shadow
33241369Sdduvall 	 * copy in "Page Zero" of register space to restore them
33251369Sdduvall 	 * both in one go ...
33261369Sdduvall 	 */
33271369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
33281369Sdduvall 		bgep->chipid.clsize);
33291369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
33301369Sdduvall 		bgep->chipid.latency);
33311369Sdduvall 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
33321369Sdduvall 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
33331369Sdduvall 
33341369Sdduvall 	/*
33351369Sdduvall 	 * The SEND INDEX registers should be reset to zero by the
33361369Sdduvall 	 * global chip reset; if they're not, there'll be trouble
33371865Sdilpreet 	 * later on.
33381369Sdduvall 	 */
33391369Sdduvall 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
33401865Sdilpreet 	if (sx0 != 0) {
33411865Sdilpreet 		BGE_REPORT((bgep, "SEND INDEX - device didn't RESET"));
33421865Sdilpreet 		bge_fm_ereport(bgep, DDI_FM_DEVICE_INVAL_STATE);
33433170Sml149210 		retval = DDI_FAILURE;
33441865Sdilpreet 	}
33451369Sdduvall 
33461369Sdduvall 	/* Enable MSI code */
33471369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
33481369Sdduvall 		bge_reg_set32(bgep, MSI_MODE_REG,
33493907Szh199473 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE|MSI_ERROR_ATTENTION);
33501369Sdduvall 
33511369Sdduvall 	/*
33521369Sdduvall 	 * On the first time through, save the factory-set MAC address
33531369Sdduvall 	 * (if any).  If bge_poll_firmware() above didn't return one
33541369Sdduvall 	 * (from a chip register) consider looking in the attached NV
33551369Sdduvall 	 * memory device, if any.  Once we have it, we save it in both
33561369Sdduvall 	 * register-image (64-bit) and byte-array forms.  All-zero and
33571369Sdduvall 	 * all-one addresses are not valid, and we refuse to stash those.
33581369Sdduvall 	 */
33591369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
33601369Sdduvall 		if (mac == 0ULL)
33611369Sdduvall 			mac = bge_get_nvmac(bgep);
33621369Sdduvall 		if (mac != 0ULL && mac != ~0ULL) {
33631369Sdduvall 			bgep->chipid.hw_mac_addr = mac;
33641369Sdduvall 			for (i = ETHERADDRL; i-- != 0; ) {
33651369Sdduvall 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
33661369Sdduvall 				mac >>= 8;
33671369Sdduvall 			}
33682331Skrgopi 			bgep->chipid.vendor_addr.set = B_TRUE;
33691369Sdduvall 		}
33701369Sdduvall 	}
33711369Sdduvall 
33721408Srandyf #ifdef BGE_IPMI_ASF
33731408Srandyf 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
33741408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
33751408Srandyf 			if ((asf_mode == ASF_MODE_INIT) ||
33761408Srandyf 				(asf_mode == ASF_MODE_POST_INIT)) {
33771408Srandyf 
33781408Srandyf 				bge_asf_post_reset_new_mode(bgep,
33791408Srandyf 					BGE_INIT_RESET);
33801408Srandyf 			} else {
33811408Srandyf 				bge_asf_post_reset_new_mode(bgep,
33821408Srandyf 					BGE_SHUTDOWN_RESET);
33831408Srandyf 			}
33841408Srandyf 		}
33851408Srandyf 	}
33861408Srandyf #endif
33871408Srandyf 
33881369Sdduvall 	/*
33891369Sdduvall 	 * Record the new state
33901369Sdduvall 	 */
33911369Sdduvall 	bgep->chip_resets += 1;
33921369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RESET;
33931865Sdilpreet 	return (retval);
33941369Sdduvall }
33951369Sdduvall 
33961369Sdduvall /*
33971369Sdduvall  * bge_chip_start() -- start the chip transmitting and/or receiving,
33981369Sdduvall  * including enabling interrupts
33991369Sdduvall  */
34001865Sdilpreet int bge_chip_start(bge_t *bgep, boolean_t reset_phys);
34011369Sdduvall #pragma	no_inline(bge_chip_start)
34021369Sdduvall 
34031865Sdilpreet int
34041369Sdduvall bge_chip_start(bge_t *bgep, boolean_t reset_phys)
34051369Sdduvall {
34061369Sdduvall 	uint32_t coalmode;
34071369Sdduvall 	uint32_t ledctl;
34081369Sdduvall 	uint32_t mtu;
34091369Sdduvall 	uint32_t maxring;
34103534Szh199473 	uint32_t stats_mask;
34114330Sml149210 	uint32_t dma_wrprio;
34121369Sdduvall 	uint64_t ring;
34131865Sdilpreet 	int retval = DDI_SUCCESS;
34141369Sdduvall 
34151369Sdduvall 	BGE_TRACE(("bge_chip_start($%p)",
34164588Sml149210 	    (void *)bgep));
34171369Sdduvall 
34181369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
34191369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
34201369Sdduvall 
34211369Sdduvall 	/*
34221369Sdduvall 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
34231369Sdduvall 	 * The document specifies 95 separate steps to fully
34241369Sdduvall 	 * initialise the chip!!!!
34251369Sdduvall 	 *
34261369Sdduvall 	 * The reset code above has already got us as far as step
34271369Sdduvall 	 * 21, so we continue with ...
34281369Sdduvall 	 *
34291369Sdduvall 	 * Step 22: clear the MAC statistics block
34301369Sdduvall 	 * (0x0300-0x0aff in NIC-local memory)
34311369Sdduvall 	 */
34321369Sdduvall 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
34331369Sdduvall 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
34341369Sdduvall 		    NIC_MEM_STATISTICS_SIZE);
34351369Sdduvall 
34361369Sdduvall 	/*
34371369Sdduvall 	 * Step 23: clear the status block (in host memory)
34381369Sdduvall 	 */
34391369Sdduvall 	DMA_ZERO(bgep->status_block);
34401369Sdduvall 
34411369Sdduvall 	/*
34421369Sdduvall 	 * Step 24: set DMA read/write control register
34431369Sdduvall 	 */
34441369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
34454588Sml149210 	    bgep->chipid.bge_dma_rwctrl);
34461369Sdduvall 
34471369Sdduvall 	/*
34481369Sdduvall 	 * Step 25: Configure DMA endianness -- already done (16/17)
34491369Sdduvall 	 * Step 26: Configure Host-Based Send Rings
34501369Sdduvall 	 * Step 27: Indicate Host Stack Up
34511369Sdduvall 	 */
34521369Sdduvall 	bge_reg_set32(bgep, MODE_CONTROL_REG,
34534588Sml149210 	    MODE_HOST_SEND_BDS |
34544588Sml149210 	    MODE_HOST_STACK_UP);
34551369Sdduvall 
34561369Sdduvall 	/*
34571369Sdduvall 	 * Step 28: Configure checksum options:
34581611Szh199473 	 *	Solaris supports the hardware default checksum options.
34591611Szh199473 	 *
34601611Szh199473 	 *	Workaround for Incorrect pseudo-header checksum calculation.
34611369Sdduvall 	 */
34622135Szh199473 	if (bgep->chipid.flags & CHIP_FLAG_PARTIAL_CSUM)
34631611Szh199473 		bge_reg_set32(bgep, MODE_CONTROL_REG,
34642311Sseb 		    MODE_SEND_NO_PSEUDO_HDR_CSUM);
34651369Sdduvall 
34661369Sdduvall 	/*
34671369Sdduvall 	 * Step 29: configure Timer Prescaler.  The value is always the
34681369Sdduvall 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
34691369Sdduvall 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
34701369Sdduvall 	 * for the whole chip!
34711369Sdduvall 	 */
34721369Sdduvall 	bge_reg_put32(bgep, MISC_CONFIG_REG, MISC_CONFIG_DEFAULT);
34731369Sdduvall 
34741369Sdduvall 	/*
34751369Sdduvall 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
34761369Sdduvall 	 *
34771369Sdduvall 	 * If the mbuf_length is specified as 0, we just leave these at
34781369Sdduvall 	 * their hardware defaults, rather than explicitly setting them.
34791369Sdduvall 	 * As the Broadcom HRM,driver better not change the parameters
34801369Sdduvall 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
34811369Sdduvall 	 */
34821369Sdduvall 	if ((bgep->chipid.mbuf_length != 0) &&
34834588Sml149210 	    (DEVICE_5704_SERIES_CHIPSETS(bgep))) {
34841369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
34854588Sml149210 			    bgep->chipid.mbuf_base);
34861369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
34874588Sml149210 			    bgep->chipid.mbuf_length);
34881369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
34894588Sml149210 			    DMAD_POOL_BASE_DEFAULT);
34901369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
34914588Sml149210 			    DMAD_POOL_LENGTH_DEFAULT);
34921369Sdduvall 	}
34931369Sdduvall 
34941369Sdduvall 	/*
34951369Sdduvall 	 * Step 32: configure MAC memory pool watermarks
34961369Sdduvall 	 */
34971369Sdduvall 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
34984588Sml149210 	    bgep->chipid.mbuf_lo_water_rdma);
34991369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
35004588Sml149210 	    bgep->chipid.mbuf_lo_water_rmac);
35011369Sdduvall 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
35024588Sml149210 	    bgep->chipid.mbuf_hi_water);
35031369Sdduvall 
35041369Sdduvall 	/*
35051369Sdduvall 	 * Step 33: configure DMA resource watermarks
35061369Sdduvall 	 */
35071369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35081369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
35091369Sdduvall 		    bge_dmad_lo_water);
35101369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
35111369Sdduvall 		    bge_dmad_hi_water);
35121369Sdduvall 	}
35131369Sdduvall 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
35141369Sdduvall 
35151369Sdduvall 	/*
35161369Sdduvall 	 * Steps 34-36: enable buffer manager & internal h/w queues
35171369Sdduvall 	 */
35181865Sdilpreet 	if (!bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
35191865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
35201865Sdilpreet 		retval = DDI_FAILURE;
35211865Sdilpreet 	if (!bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0))
35221865Sdilpreet 		retval = DDI_FAILURE;
35231369Sdduvall 
35241369Sdduvall 	/*
35251369Sdduvall 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
35261369Sdduvall 	 */
35271369Sdduvall 	bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
35284588Sml149210 	    &bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
35291369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35301369Sdduvall 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
35314588Sml149210 		    &bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
35321369Sdduvall 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
35334588Sml149210 		    &bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
35341369Sdduvall 	}
35351369Sdduvall 
35361369Sdduvall 	/*
35371369Sdduvall 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
35381369Sdduvall 	 */
35391369Sdduvall 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
35401369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35411369Sdduvall 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
35421369Sdduvall 		    bge_replenish_jumbo);
35431369Sdduvall 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
35441369Sdduvall 		    bge_replenish_mini);
35451369Sdduvall 	}
35461369Sdduvall 
35471369Sdduvall 	/*
35481369Sdduvall 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
35491369Sdduvall 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
35501369Sdduvall 	 */
35511369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
35521369Sdduvall 		maxring = BGE_SEND_RINGS_MAX;
35531369Sdduvall 	else
35541369Sdduvall 		maxring = BGE_SEND_RINGS_MAX_5705;
35551369Sdduvall 	for (ring = 0; ring < maxring; ++ring) {
35561369Sdduvall 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
35571369Sdduvall 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
35581369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
35594588Sml149210 		    &bgep->send[ring].hw_rcb);
35601369Sdduvall 	}
35611369Sdduvall 
35621369Sdduvall 	/*
35631369Sdduvall 	 * Steps 44-45: initialise Receive Return Rings
35641369Sdduvall 	 * (0x0200-0x02ff in NIC-local memory)
35651369Sdduvall 	 */
35661369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
35671369Sdduvall 		maxring = BGE_RECV_RINGS_MAX;
35681369Sdduvall 	else
35691369Sdduvall 		maxring = BGE_RECV_RINGS_MAX_5705;
35701369Sdduvall 	for (ring = 0; ring < maxring; ++ring)
35711369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
35724588Sml149210 		    &bgep->recv[ring].hw_rcb);
35731369Sdduvall 
35741369Sdduvall 	/*
35751369Sdduvall 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
35761369Sdduvall 	 */
35771369Sdduvall 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
35781369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
35791369Sdduvall 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
35801369Sdduvall 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
35811369Sdduvall 	}
35821369Sdduvall 	/*
35831369Sdduvall 	 * Step 47: configure the MAC unicast address
35841369Sdduvall 	 * Step 48: configure the random backoff seed
35851369Sdduvall 	 * Step 96: set up multicast filters
35861369Sdduvall 	 */
35871408Srandyf #ifdef BGE_IPMI_ASF
35881865Sdilpreet 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE)
35891408Srandyf #else
35901865Sdilpreet 	if (bge_chip_sync(bgep) == DDI_FAILURE)
35911408Srandyf #endif
35921865Sdilpreet 		retval = DDI_FAILURE;
35931369Sdduvall 
35941369Sdduvall 	/*
35951369Sdduvall 	 * Step 49: configure the MTU
35961369Sdduvall 	 */
35971369Sdduvall 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
35981369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
35991369Sdduvall 
36001369Sdduvall 	/*
36011369Sdduvall 	 * Step 50: configure the IPG et al
36021369Sdduvall 	 */
36031369Sdduvall 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
36041369Sdduvall 
36051369Sdduvall 	/*
36061369Sdduvall 	 * Step 51: configure the default Rx Return Ring
36071369Sdduvall 	 */
36081369Sdduvall 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
36091369Sdduvall 
36101369Sdduvall 	/*
36111369Sdduvall 	 * Steps 52-54: configure Receive List Placement,
36121369Sdduvall 	 * and enable Receive List Placement Statistics
36131369Sdduvall 	 */
36141369Sdduvall 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
36154588Sml149210 	    RCV_LP_CONFIG(bgep->chipid.rx_rings));
36163534Szh199473 	switch (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev)) {
36173534Szh199473 	case MHCR_CHIP_ASIC_REV_5700:
36183534Szh199473 	case MHCR_CHIP_ASIC_REV_5701:
36193534Szh199473 	case MHCR_CHIP_ASIC_REV_5703:
36203534Szh199473 	case MHCR_CHIP_ASIC_REV_5704:
36213534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
36223534Szh199473 		break;
36233534Szh199473 	case MHCR_CHIP_ASIC_REV_5705:
36243534Szh199473 		break;
36253534Szh199473 	default:
36263534Szh199473 		stats_mask = bge_reg_get32(bgep, RCV_LP_STATS_ENABLE_MASK_REG);
36273534Szh199473 		stats_mask &= ~RCV_LP_STATS_DISABLE_MACTQ;
36283534Szh199473 		bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, stats_mask);
36293534Szh199473 		break;
36303534Szh199473 	}
36311369Sdduvall 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
36321369Sdduvall 
36331369Sdduvall 	if (bgep->chipid.rx_rings > 1)
36341369Sdduvall 		bge_init_recv_rule(bgep);
36351369Sdduvall 
36361369Sdduvall 	/*
36371369Sdduvall 	 * Steps 55-56: enable Send Data Initiator Statistics
36381369Sdduvall 	 */
36391369Sdduvall 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
36401369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
36411369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
36421369Sdduvall 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
36431369Sdduvall 	} else {
36441369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
36451369Sdduvall 		    SEND_INIT_STATS_ENABLE);
36461369Sdduvall 	}
36471369Sdduvall 	/*
36481369Sdduvall 	 * Steps 57-58: stop (?) the Host Coalescing Engine
36491369Sdduvall 	 */
36501865Sdilpreet 	if (!bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0))
36511865Sdilpreet 		retval = DDI_FAILURE;
36521369Sdduvall 
36531369Sdduvall 	/*
36541369Sdduvall 	 * Steps 59-62: initialise Host Coalescing parameters
36551369Sdduvall 	 */
36561369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG, bge_tx_count_norm);
36571369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG, bge_tx_ticks_norm);
36581369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, bge_rx_count_norm);
36591369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, bge_rx_ticks_norm);
36601369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
36611369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
36621369Sdduvall 		    bge_tx_count_intr);
36631369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
36641369Sdduvall 		    bge_tx_ticks_intr);
36651369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
36661369Sdduvall 		    bge_rx_count_intr);
36671369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
36681369Sdduvall 		    bge_rx_ticks_intr);
36691369Sdduvall 	}
36701369Sdduvall 
36711369Sdduvall 	/*
36721369Sdduvall 	 * Steps 63-64: initialise status block & statistics
36731369Sdduvall 	 * host memory addresses
36741369Sdduvall 	 * The statistic block does not exist in some chipsets
36751369Sdduvall 	 * Step 65: initialise Statistics Coalescing Tick Counter
36761369Sdduvall 	 */
36771369Sdduvall 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
36784588Sml149210 	    bgep->status_block.cookie.dmac_laddress);
36791369Sdduvall 
36801369Sdduvall 	/*
36811369Sdduvall 	 * Steps 66-67: initialise status block & statistics
36821369Sdduvall 	 * NIC-local memory addresses
36831369Sdduvall 	 */
36841369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
36851369Sdduvall 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
36861369Sdduvall 		    bgep->statistics.cookie.dmac_laddress);
36871369Sdduvall 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
36881369Sdduvall 		    STATISTICS_TICKS_DEFAULT);
36891369Sdduvall 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
36901369Sdduvall 		    NIC_MEM_STATUS_BLOCK);
36911369Sdduvall 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
36921369Sdduvall 		    NIC_MEM_STATISTICS);
36931369Sdduvall 	}
36941369Sdduvall 
36951369Sdduvall 	/*
36961369Sdduvall 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
36971369Sdduvall 	 * Completion Engine, the Receive List Placement Engine, and the
36981369Sdduvall 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
36991369Sdduvall 	 * and BCM5715.
37001369Sdduvall 	 */
37011369Sdduvall 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
37021369Sdduvall 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
37031369Sdduvall 		coalmode = COALESCE_64_BYTE_STATUS;
37041369Sdduvall 	else
37051369Sdduvall 		coalmode = 0;
37061865Sdilpreet 	if (!bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode))
37071865Sdilpreet 		retval = DDI_FAILURE;
37081865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
37091865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
37101865Sdilpreet 		retval = DDI_FAILURE;
37111865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0))
37121865Sdilpreet 		retval = DDI_FAILURE;
37131369Sdduvall 
37141369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
37151865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
37161865Sdilpreet 		    STATE_MACHINE_ATTN_ENABLE_BIT))
37171865Sdilpreet 			retval = DDI_FAILURE;
37181369Sdduvall 
37191369Sdduvall 	/*
37201369Sdduvall 	 * Step 72: Enable MAC DMA engines
37211369Sdduvall 	 * Step 73: Clear & enable MAC statistics
37221369Sdduvall 	 */
37231369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
37244588Sml149210 	    ETHERNET_MODE_ENABLE_FHDE |
37254588Sml149210 	    ETHERNET_MODE_ENABLE_RDE |
37264588Sml149210 	    ETHERNET_MODE_ENABLE_TDE);
37271369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
37284588Sml149210 	    ETHERNET_MODE_ENABLE_TX_STATS |
37294588Sml149210 	    ETHERNET_MODE_ENABLE_RX_STATS |
37304588Sml149210 	    ETHERNET_MODE_CLEAR_TX_STATS |
37314588Sml149210 	    ETHERNET_MODE_CLEAR_RX_STATS);
37321369Sdduvall 
37331369Sdduvall 	/*
37341369Sdduvall 	 * Step 74: configure the MLCR (Miscellaneous Local Control
37351369Sdduvall 	 * Register); not required, as we set up the MLCR in step 10
37361369Sdduvall 	 * (part of the reset code) above.
37371369Sdduvall 	 *
37381369Sdduvall 	 * Step 75: clear Interrupt Mailbox 0
37391369Sdduvall 	 */
37401369Sdduvall 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
37411369Sdduvall 
37421369Sdduvall 	/*
37431369Sdduvall 	 * Steps 76-87: Gentlemen, start your engines ...
37441369Sdduvall 	 *
37451369Sdduvall 	 * Enable the DMA Completion Engine, the Write DMA Engine,
37461369Sdduvall 	 * the Read DMA Engine, Receive Data Completion Engine,
37471369Sdduvall 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
37481369Sdduvall 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
37491369Sdduvall 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
37501369Sdduvall 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
37511369Sdduvall 	 *
37521369Sdduvall 	 * Beware exhaust fumes?
37531369Sdduvall 	 */
37541369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
37551865Sdilpreet 		if (!bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0))
37561865Sdilpreet 			retval = DDI_FAILURE;
37574330Sml149210 	dma_wrprio = (bge_dma_wrprio << DMA_PRIORITY_SHIFT) |
37584588Sml149210 	    ALL_DMA_ATTN_BITS;
37594330Sml149210 	if (MHCR_CHIP_ASIC_REV(bgep->chipid.asic_rev) ==
37604588Sml149210 	    MHCR_CHIP_ASIC_REV_5755) {
37614330Sml149210 		dma_wrprio |= DMA_STATUS_TAG_FIX_CQ12384;
37624330Sml149210 	}
37631865Sdilpreet 	if (!bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
37644588Sml149210 	    dma_wrprio))
37651865Sdilpreet 		retval = DDI_FAILURE;
37661865Sdilpreet 	if (!bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
37671865Sdilpreet 	    (bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS))
37681865Sdilpreet 		retval = DDI_FAILURE;
37691865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
37701865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
37711865Sdilpreet 		retval = DDI_FAILURE;
37721369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
37731865Sdilpreet 		if (!bge_chip_enable_engine(bgep,
37741865Sdilpreet 		    MBUF_CLUSTER_FREE_MODE_REG, 0))
37751865Sdilpreet 			retval = DDI_FAILURE;
37761865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0))
37771865Sdilpreet 		retval = DDI_FAILURE;
37781865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
37791865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
37801865Sdilpreet 		retval = DDI_FAILURE;
37811865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
37821865Sdilpreet 	    RCV_BD_DISABLED_RING_ATTN))
37831865Sdilpreet 		retval = DDI_FAILURE;
37841865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
37851865Sdilpreet 	    RCV_DATA_BD_ILL_RING_ATTN))
37861865Sdilpreet 		retval = DDI_FAILURE;
37871865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0))
37881865Sdilpreet 		retval = DDI_FAILURE;
37891865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
37901865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
37911865Sdilpreet 		retval = DDI_FAILURE;
37921865Sdilpreet 	if (!bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
37931865Sdilpreet 	    STATE_MACHINE_ATTN_ENABLE_BIT))
37941865Sdilpreet 		retval = DDI_FAILURE;
37951369Sdduvall 
37961369Sdduvall 	/*
37971369Sdduvall 	 * Step 88: download firmware -- doesn't apply
37981369Sdduvall 	 * Steps 89-90: enable Transmit & Receive MAC Engines
37991369Sdduvall 	 */
38001865Sdilpreet 	if (!bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0))
38011865Sdilpreet 		retval = DDI_FAILURE;
38021408Srandyf #ifdef BGE_IPMI_ASF
38031865Sdilpreet 	if (!bgep->asf_enabled) {
38041865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
38051865Sdilpreet 		    RECEIVE_MODE_KEEP_VLAN_TAG))
38061865Sdilpreet 			retval = DDI_FAILURE;
38071408Srandyf 	} else {
38081865Sdilpreet 		if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0))
38091865Sdilpreet 			retval = DDI_FAILURE;
38101408Srandyf 	}
38111408Srandyf #else
38121865Sdilpreet 	if (!bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
38131865Sdilpreet 	    RECEIVE_MODE_KEEP_VLAN_TAG))
38141865Sdilpreet 		retval = DDI_FAILURE;
38151408Srandyf #endif
38161369Sdduvall 
38171369Sdduvall 	/*
38181369Sdduvall 	 * Step 91: disable auto-polling of PHY status
38191369Sdduvall 	 */
38201369Sdduvall 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
38211369Sdduvall 
38221369Sdduvall 	/*
38231369Sdduvall 	 * Step 92: configure D0 power state (not required)
38241369Sdduvall 	 * Step 93: initialise LED control register ()
38251369Sdduvall 	 */
38261369Sdduvall 	ledctl = LED_CONTROL_DEFAULT;
38271369Sdduvall 	switch (bgep->chipid.device) {
38281369Sdduvall 	case DEVICE_ID_5700:
38291369Sdduvall 	case DEVICE_ID_5700x:
38301369Sdduvall 	case DEVICE_ID_5701:
38311369Sdduvall 		/*
38321369Sdduvall 		 * Switch to 5700 (MAC) mode on these older chips
38331369Sdduvall 		 */
38341369Sdduvall 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
38351369Sdduvall 		ledctl |= LED_CONTROL_LED_MODE_5700;
38361369Sdduvall 		break;
38371369Sdduvall 
38381369Sdduvall 	default:
38391369Sdduvall 		break;
38401369Sdduvall 	}
38411369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
38421369Sdduvall 
38431369Sdduvall 	/*
38441369Sdduvall 	 * Step 94: activate link
38451369Sdduvall 	 */
38461369Sdduvall 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
38471369Sdduvall 
38481369Sdduvall 	/*
38491369Sdduvall 	 * Step 95: set up physical layer (PHY/SerDes)
38501369Sdduvall 	 * restart autoneg (if required)
38511369Sdduvall 	 */
38521369Sdduvall 	if (reset_phys)
38531865Sdilpreet 		if (bge_phys_update(bgep) == DDI_FAILURE)
38541865Sdilpreet 			retval = DDI_FAILURE;
38551369Sdduvall 
38561369Sdduvall 	/*
38571369Sdduvall 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
38581369Sdduvall 	 */
38591369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
38601369Sdduvall 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
38614588Sml149210 		    bgep->buff[ring].rf_next);
38621369Sdduvall 
38631369Sdduvall 	/*
38641369Sdduvall 	 * MSI bits:The least significant MSI 16-bit word.
38651369Sdduvall 	 * ISR will be triggered different.
38661369Sdduvall 	 */
38671369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
38681369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
38691369Sdduvall 
38701369Sdduvall 	/*
38711369Sdduvall 	 * Extra step (DSG): select which interrupts are enabled
38721369Sdduvall 	 *
38731369Sdduvall 	 * Program the Ethernet MAC engine to signal attention on
38741369Sdduvall 	 * Link Change events, then enable interrupts on MAC, DMA,
38751369Sdduvall 	 * and FLOW attention signals.
38761369Sdduvall 	 */
38771369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
38784588Sml149210 	    ETHERNET_EVENT_LINK_INT |
38794588Sml149210 	    ETHERNET_STATUS_PCS_ERROR_INT);
38801408Srandyf #ifdef BGE_IPMI_ASF
38811408Srandyf 	if (bgep->asf_enabled) {
38821408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
38834588Sml149210 		    MODE_INT_ON_FLOW_ATTN |
38844588Sml149210 		    MODE_INT_ON_DMA_ATTN |
38854588Sml149210 		    MODE_HOST_STACK_UP|
38864588Sml149210 		    MODE_INT_ON_MAC_ATTN);
38871408Srandyf 	} else {
38881408Srandyf #endif
38891408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
38904588Sml149210 		    MODE_INT_ON_FLOW_ATTN |
38914588Sml149210 		    MODE_INT_ON_DMA_ATTN |
38924588Sml149210 		    MODE_INT_ON_MAC_ATTN);
38931408Srandyf #ifdef BGE_IPMI_ASF
38941408Srandyf 	}
38951408Srandyf #endif
38961369Sdduvall 
38971369Sdduvall 	/*
38981369Sdduvall 	 * Step 97: enable PCI interrupts!!!
38991369Sdduvall 	 */
39001369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
39011369Sdduvall 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
39021369Sdduvall 		    MHCR_MASK_PCI_INT_OUTPUT);
39031369Sdduvall 
39041369Sdduvall 	/*
39051369Sdduvall 	 * All done!
39061369Sdduvall 	 */
39071369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
39081865Sdilpreet 	return (retval);
39091369Sdduvall }
39101369Sdduvall 
39111369Sdduvall 
39121369Sdduvall /*
39131369Sdduvall  * ========== Hardware interrupt handler ==========
39141369Sdduvall  */
39151369Sdduvall 
39161369Sdduvall #undef	BGE_DBG
39171369Sdduvall #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
39181369Sdduvall 
39191369Sdduvall /*
39201369Sdduvall  * Sync the status block, then atomically clear the specified bits in
39211369Sdduvall  * the <flags-and-tag> field of the status block.
39221369Sdduvall  * the <flags> word of the status block, returning the value of the
39231369Sdduvall  * <tag> and the <flags> before the bits were cleared.
39241369Sdduvall  */
39251865Sdilpreet static int bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags);
39261369Sdduvall #pragma	inline(bge_status_sync)
39271369Sdduvall 
39281865Sdilpreet static int
39291865Sdilpreet bge_status_sync(bge_t *bgep, uint64_t bits, uint64_t *flags)
39301369Sdduvall {
39311369Sdduvall 	bge_status_t *bsp;
39321865Sdilpreet 	int retval;
39331369Sdduvall 
39341369Sdduvall 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
39354588Sml149210 	    (void *)bgep, bits));
39361369Sdduvall 
39371369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
39381369Sdduvall 
39391369Sdduvall 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
39401865Sdilpreet 	retval = bge_check_dma_handle(bgep, bgep->status_block.dma_hdl);
39411865Sdilpreet 	if (retval != DDI_FM_OK)
39421865Sdilpreet 		return (retval);
39431865Sdilpreet 
39441369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
39451865Sdilpreet 	*flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
39461369Sdduvall 
39471369Sdduvall 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
39484588Sml149210 	    (void *)bgep, bits, *flags));
39491865Sdilpreet 
39501865Sdilpreet 	return (retval);
39511369Sdduvall }
39521369Sdduvall 
39535903Ssowmini void bge_wake_factotum(bge_t *bgep);
39541369Sdduvall #pragma	inline(bge_wake_factotum)
39551369Sdduvall 
39565903Ssowmini void
39571369Sdduvall bge_wake_factotum(bge_t *bgep)
39581369Sdduvall {
39591369Sdduvall 	mutex_enter(bgep->softintrlock);
39601369Sdduvall 	if (bgep->factotum_flag == 0) {
39611369Sdduvall 		bgep->factotum_flag = 1;
39621369Sdduvall 		ddi_trigger_softintr(bgep->factotum_id);
39631369Sdduvall 	}
39641369Sdduvall 	mutex_exit(bgep->softintrlock);
39651369Sdduvall }
39661369Sdduvall 
39671369Sdduvall /*
39681369Sdduvall  *	bge_intr() -- handle chip interrupts
39691369Sdduvall  */
39701369Sdduvall uint_t bge_intr(caddr_t arg1, caddr_t arg2);
39711369Sdduvall #pragma	no_inline(bge_intr)
39721369Sdduvall 
39731369Sdduvall uint_t
39741369Sdduvall bge_intr(caddr_t arg1, caddr_t arg2)
39751369Sdduvall {
39761369Sdduvall 	bge_t *bgep = (bge_t *)arg1;		/* private device info	*/
39771369Sdduvall 	bge_status_t *bsp;
39781369Sdduvall 	uint64_t flags;
39793907Szh199473 	uint32_t regval;
39801369Sdduvall 	uint_t result;
39813918Sml149210 	int retval, loop_cnt = 0;
39821369Sdduvall 
39831369Sdduvall 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
39841369Sdduvall 
39851369Sdduvall 	/*
39861369Sdduvall 	 * GLD v2 checks that s/w setup is complete before passing
39871369Sdduvall 	 * interrupts to this routine, thus eliminating the old
39881369Sdduvall 	 * (and well-known) race condition around ddi_add_intr()
39891369Sdduvall 	 */
39901369Sdduvall 	ASSERT(bgep->progress & PROGRESS_HWINT);
39911369Sdduvall 
39921369Sdduvall 	result = DDI_INTR_UNCLAIMED;
39931369Sdduvall 	mutex_enter(bgep->genlock);
39941369Sdduvall 
39953907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
39961369Sdduvall 		/*
39973907Szh199473 		 * Check whether chip's says it's asserting #INTA;
39983907Szh199473 		 * if not, don't process or claim the interrupt.
39993907Szh199473 		 *
40003907Szh199473 		 * Note that the PCI signal is active low, so the
40013907Szh199473 		 * bit is *zero* when the interrupt is asserted.
40021369Sdduvall 		 */
40033907Szh199473 		regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
40043907Szh199473 		if (regval & MLCR_INTA_STATE) {
40053907Szh199473 			if (bge_check_acc_handle(bgep, bgep->io_handle)
40063907Szh199473 			    != DDI_FM_OK)
40071865Sdilpreet 				goto chip_stop;
40083907Szh199473 			mutex_exit(bgep->genlock);
40093907Szh199473 			return (result);
40101865Sdilpreet 		}
40111369Sdduvall 
40121369Sdduvall 		/*
40133907Szh199473 		 * Block further PCI interrupts ...
40143907Szh199473 		 */
40153907Szh199473 		bge_reg_set32(bgep, PCI_CONF_BGE_MHCR,
40163907Szh199473 		    MHCR_MASK_PCI_INT_OUTPUT);
40173907Szh199473 
40183907Szh199473 	} else {
40193907Szh199473 		/*
40203907Szh199473 		 * Check MSI status
40211369Sdduvall 		 */
40223907Szh199473 		regval = bge_reg_get32(bgep, MSI_STATUS_REG);
40233907Szh199473 		if (regval & MSI_ERROR_ATTENTION) {
40243907Szh199473 			BGE_REPORT((bgep, "msi error attention,"
40253907Szh199473 			    " status=0x%x", regval));
40263907Szh199473 			bge_reg_put32(bgep, MSI_STATUS_REG, regval);
40273907Szh199473 		}
40283907Szh199473 	}
40293907Szh199473 
40303907Szh199473 	result = DDI_INTR_CLAIMED;
40313907Szh199473 
40323907Szh199473 	BGE_DEBUG(("bge_intr($%p) ($%p) regval 0x%08x", arg1, arg2, regval));
40333907Szh199473 
40343907Szh199473 	/*
40353907Szh199473 	 * Sync the status block and grab the flags-n-tag from it.
40363907Szh199473 	 * We count the number of interrupts where there doesn't
40373907Szh199473 	 * seem to have been a DMA update of the status block; if
40383907Szh199473 	 * it *has* been updated, the counter will be cleared in
40393907Szh199473 	 * the while() loop below ...
40403907Szh199473 	 */
40413907Szh199473 	bgep->missed_dmas += 1;
40423907Szh199473 	bsp = DMA_VPTR(bgep->status_block);
40433918Sml149210 	for (loop_cnt = 0; loop_cnt < bge_intr_max_loop; loop_cnt++) {
40443907Szh199473 		if (bgep->bge_chip_state != BGE_CHIP_RUNNING) {
40451369Sdduvall 			/*
40463907Szh199473 			 * bge_chip_stop() may have freed dma area etc
40473907Szh199473 			 * while we were in this interrupt handler -
40483907Szh199473 			 * better not call bge_status_sync()
40491369Sdduvall 			 */
40503907Szh199473 			(void) bge_check_acc_handle(bgep,
40513907Szh199473 			    bgep->io_handle);
40521369Sdduvall 			mutex_exit(bgep->genlock);
40533907Szh199473 			return (DDI_INTR_CLAIMED);
40543907Szh199473 		}
40553907Szh199473 		retval = bge_status_sync(bgep, STATUS_FLAG_UPDATED,
40563907Szh199473 		    &flags);
40573907Szh199473 		if (retval != DDI_FM_OK) {
40583907Szh199473 			bgep->bge_dma_error = B_TRUE;
40593907Szh199473 			goto chip_stop;
40601369Sdduvall 		}
40611369Sdduvall 
40623907Szh199473 		if (!(flags & STATUS_FLAG_UPDATED))
40633907Szh199473 			break;
40643907Szh199473 
40653907Szh199473 		/*
40663907Szh199473 		 * Tell the chip that we're processing the interrupt
40673907Szh199473 		 */
40683907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
40693907Szh199473 		    INTERRUPT_MBOX_DISABLE(flags));
40703907Szh199473 		if (bge_check_acc_handle(bgep, bgep->io_handle) !=
40713907Szh199473 		    DDI_FM_OK)
40723907Szh199473 			goto chip_stop;
40733907Szh199473 
40741369Sdduvall 		/*
40753907Szh199473 		 * Drop the mutex while we:
40763907Szh199473 		 * 	Receive any newly-arrived packets
40773907Szh199473 		 *	Recycle any newly-finished send buffers
40781369Sdduvall 		 */
40793907Szh199473 		bgep->bge_intr_running = B_TRUE;
40803907Szh199473 		mutex_exit(bgep->genlock);
40813907Szh199473 		bge_receive(bgep, bsp);
40823907Szh199473 		bge_recycle(bgep, bsp);
40833907Szh199473 		mutex_enter(bgep->genlock);
40843907Szh199473 		bgep->bge_intr_running = B_FALSE;
40851369Sdduvall 
40861369Sdduvall 		/*
40873907Szh199473 		 * Tell the chip we've finished processing, and
40883907Szh199473 		 * give it the tag that we got from the status
40893907Szh199473 		 * block earlier, so that it knows just how far
40903907Szh199473 		 * we've gone.  If it's got more for us to do,
40913907Szh199473 		 * it will now update the status block and try
40923907Szh199473 		 * to assert an interrupt (but we've got the
40933907Szh199473 		 * #INTA blocked at present).  If we see the
40943907Szh199473 		 * update, we'll loop around to do some more.
40953907Szh199473 		 * Eventually we'll get out of here ...
40963907Szh199473 		 */
40973907Szh199473 		bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
40983907Szh199473 		    INTERRUPT_MBOX_ENABLE(flags));
4099*6546Sgh162552 		if (bgep->chipid.pci_type == BGE_PCI_E)
4100*6546Sgh162552 			(void) bge_mbx_get(bgep, INTERRUPT_MBOX_0_REG);
41013907Szh199473 		bgep->missed_dmas = 0;
41023907Szh199473 	}
41033907Szh199473 
41043907Szh199473 	/*
41053907Szh199473 	 * Check for exceptional conditions that we need to handle
41063907Szh199473 	 *
41073907Szh199473 	 * Link status changed
41083907Szh199473 	 * Status block not updated
41093907Szh199473 	 */
41103907Szh199473 	if (flags & STATUS_FLAG_LINK_CHANGED)
41113907Szh199473 		bge_wake_factotum(bgep);
41123907Szh199473 
41133907Szh199473 	if (bgep->missed_dmas) {
41143907Szh199473 		/*
41153907Szh199473 		 * Probably due to the internal status tag not
41163907Szh199473 		 * being reset.  Force a status block update now;
41173907Szh199473 		 * this should ensure that we get an update and
41183907Szh199473 		 * a new interrupt.  After that, we should be in
41193907Szh199473 		 * sync again ...
41201369Sdduvall 		 */
41213907Szh199473 		BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
41223907Szh199473 		    "not updated?", flags));
41233907Szh199473 		bgep->missed_updates++;
41243907Szh199473 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
41253907Szh199473 		    COALESCE_NOW);
41263907Szh199473 
41273907Szh199473 		if (bgep->missed_dmas >= bge_dma_miss_limit) {
41283907Szh199473 			/*
41293907Szh199473 			 * If this happens multiple times in a row,
41303907Szh199473 			 * it means DMA is just not working.  Maybe
41313907Szh199473 			 * the chip's failed, or maybe there's a
41323907Szh199473 			 * problem on the PCI bus or in the host-PCI
41333907Szh199473 			 * bridge (Tomatillo).
41343907Szh199473 			 *
41353907Szh199473 			 * At all events, we want to stop further
41363907Szh199473 			 * interrupts and let the recovery code take
41373907Szh199473 			 * over to see whether anything can be done
41383907Szh199473 			 * about it ...
41393907Szh199473 			 */
41403907Szh199473 			bge_fm_ereport(bgep,
41413907Szh199473 			    DDI_FM_DEVICE_BADINT_LIMIT);
41423907Szh199473 			goto chip_stop;
41431369Sdduvall 		}
41441369Sdduvall 	}
41451369Sdduvall 
41463907Szh199473 	/*
41473907Szh199473 	 * Reenable assertion of #INTA, unless there's a DMA fault
41483907Szh199473 	 */
41493907Szh199473 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED) {
41503907Szh199473 		bge_reg_clr32(bgep, PCI_CONF_BGE_MHCR,
41513907Szh199473 		    MHCR_MASK_PCI_INT_OUTPUT);
41523907Szh199473 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
41533907Szh199473 		    DDI_FM_OK)
41543907Szh199473 			goto chip_stop;
41553907Szh199473 	}
41563907Szh199473 
41571865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
41581865Sdilpreet 		goto chip_stop;
41591865Sdilpreet 
41601865Sdilpreet 	mutex_exit(bgep->genlock);
41611865Sdilpreet 	return (result);
41621865Sdilpreet 
41631865Sdilpreet chip_stop:
41641865Sdilpreet #ifdef BGE_IPMI_ASF
41651865Sdilpreet 	if (bgep->asf_enabled && bgep->asf_status == ASF_STAT_RUN) {
41661865Sdilpreet 		/*
41671865Sdilpreet 		 * We must stop ASF heart beat before
41681865Sdilpreet 		 * bge_chip_stop(), otherwise some
41691865Sdilpreet 		 * computers (ex. IBM HS20 blade
41701865Sdilpreet 		 * server) may crash.
41711865Sdilpreet 		 */
41721865Sdilpreet 		bge_asf_update_status(bgep);
41731865Sdilpreet 		bge_asf_stop_timer(bgep);
41741865Sdilpreet 		bgep->asf_status = ASF_STAT_STOP;
41751865Sdilpreet 
41761865Sdilpreet 		bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
41771865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
41781865Sdilpreet 	}
41791865Sdilpreet #endif
41801865Sdilpreet 	bge_chip_stop(bgep, B_TRUE);
41811865Sdilpreet 	(void) bge_check_acc_handle(bgep, bgep->io_handle);
41821369Sdduvall 	mutex_exit(bgep->genlock);
41831369Sdduvall 	return (result);
41841369Sdduvall }
41851369Sdduvall 
41861369Sdduvall /*
41871369Sdduvall  * ========== Factotum, implemented as a softint handler ==========
41881369Sdduvall  */
41891369Sdduvall 
41901369Sdduvall #undef	BGE_DBG
41911369Sdduvall #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
41921369Sdduvall 
41931369Sdduvall static void bge_factotum_error_handler(bge_t *bgep);
41941369Sdduvall #pragma	no_inline(bge_factotum_error_handler)
41951369Sdduvall 
41961369Sdduvall static void
41971369Sdduvall bge_factotum_error_handler(bge_t *bgep)
41981369Sdduvall {
41991369Sdduvall 	uint32_t flow;
42001369Sdduvall 	uint32_t rdma;
42011369Sdduvall 	uint32_t wdma;
42021369Sdduvall 	uint32_t tmac;
42031369Sdduvall 	uint32_t rmac;
42041369Sdduvall 	uint32_t rxrs;
42051369Sdduvall 	uint32_t txrs = 0;
42061369Sdduvall 
42071369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
42081369Sdduvall 
42091369Sdduvall 	/*
42101369Sdduvall 	 * Read all the registers that show the possible
42111369Sdduvall 	 * reasons for the ERROR bit to be asserted
42121369Sdduvall 	 */
42131369Sdduvall 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
42141369Sdduvall 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
42151369Sdduvall 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
42161369Sdduvall 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
42171369Sdduvall 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
42181369Sdduvall 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
42191369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
42201369Sdduvall 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
42211369Sdduvall 
42221369Sdduvall 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
42234588Sml149210 	    (void *)bgep, flow, rdma, wdma));
42241369Sdduvall 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
42254588Sml149210 	    (void *)bgep, tmac, rmac, rxrs, txrs));
42261369Sdduvall 
42271369Sdduvall 	/*
42281369Sdduvall 	 * For now, just clear all the errors ...
42291369Sdduvall 	 */
42301369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
42311369Sdduvall 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
42321369Sdduvall 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
42331369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
42341369Sdduvall 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
42351369Sdduvall 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
42361369Sdduvall 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
42371369Sdduvall }
42381369Sdduvall 
42391369Sdduvall /*
42401369Sdduvall  * Handler for hardware link state change.
42411369Sdduvall  *
42421369Sdduvall  * When this routine is called, the hardware link state has changed
42431369Sdduvall  * and the new state is reflected in the param_* variables.  Here
42444403Sgd78059  * we must update the softstate and reprogram the MAC to match.
42451369Sdduvall  */
42461369Sdduvall static void bge_factotum_link_handler(bge_t *bgep);
42471369Sdduvall #pragma	no_inline(bge_factotum_link_handler)
42481369Sdduvall 
42491369Sdduvall static void
42501369Sdduvall bge_factotum_link_handler(bge_t *bgep)
42511369Sdduvall {
42521369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
42531369Sdduvall 
42541369Sdduvall 	/*
42551369Sdduvall 	 * Update the s/w link_state
42561369Sdduvall 	 */
42571369Sdduvall 	if (bgep->param_link_up)
42581369Sdduvall 		bgep->link_state = LINK_STATE_UP;
42591369Sdduvall 	else
42601369Sdduvall 		bgep->link_state = LINK_STATE_DOWN;
42611369Sdduvall 
42621369Sdduvall 	/*
42631369Sdduvall 	 * Reprogram the MAC modes to match
42641369Sdduvall 	 */
42651369Sdduvall 	bge_sync_mac_modes(bgep);
42661369Sdduvall }
42671369Sdduvall 
42681865Sdilpreet static boolean_t bge_factotum_link_check(bge_t *bgep, int *dma_state);
42691369Sdduvall #pragma	no_inline(bge_factotum_link_check)
42701369Sdduvall 
42711369Sdduvall static boolean_t
42721865Sdilpreet bge_factotum_link_check(bge_t *bgep, int *dma_state)
42731369Sdduvall {
42741369Sdduvall 	boolean_t check;
42751369Sdduvall 	uint64_t flags;
42761369Sdduvall 	uint32_t tmac_status;
42771369Sdduvall 
42781369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
42791369Sdduvall 
42801369Sdduvall 	/*
42811369Sdduvall 	 * Get & clear the writable status bits in the Tx status register
42821369Sdduvall 	 * (some bits are write-1-to-clear, others are just readonly).
42831369Sdduvall 	 */
42841369Sdduvall 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
42851369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
42861369Sdduvall 
42871369Sdduvall 	/*
42881369Sdduvall 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
42891369Sdduvall 	 */
42901865Sdilpreet 	*dma_state = bge_status_sync(bgep, STATUS_FLAG_ERROR |
42911865Sdilpreet 	    STATUS_FLAG_LINK_CHANGED, &flags);
42921865Sdilpreet 	if (*dma_state != DDI_FM_OK)
42931865Sdilpreet 		return (B_FALSE);
42941369Sdduvall 
42951369Sdduvall 	/*
42961369Sdduvall 	 * Clear any errors flagged in the status block ...
42971369Sdduvall 	 */
42981369Sdduvall 	if (flags & STATUS_FLAG_ERROR)
42991369Sdduvall 		bge_factotum_error_handler(bgep);
43001369Sdduvall 
43011369Sdduvall 	/*
43021369Sdduvall 	 * We need to check the link status if:
43031369Sdduvall 	 *	the status block says there's been a link change
43041369Sdduvall 	 *	or there's any discrepancy between the various
43051369Sdduvall 	 *	flags indicating the link state (link_state,
43061369Sdduvall 	 *	param_link_up, and the LINK STATE bit in the
43071369Sdduvall 	 *	Transmit MAC status register).
43081369Sdduvall 	 */
43091369Sdduvall 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
43101369Sdduvall 	switch (bgep->link_state) {
43111369Sdduvall 	case LINK_STATE_UP:
43121369Sdduvall 		check |= (bgep->param_link_up == B_FALSE);
43131369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
43141369Sdduvall 		break;
43151369Sdduvall 
43161369Sdduvall 	case LINK_STATE_DOWN:
43171369Sdduvall 		check |= (bgep->param_link_up != B_FALSE);
43181369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
43191369Sdduvall 		break;
43201369Sdduvall 
43211369Sdduvall 	default:
43221369Sdduvall 		check = B_TRUE;
43231369Sdduvall 		break;
43241369Sdduvall 	}
43251369Sdduvall 
43261369Sdduvall 	/*
43271369Sdduvall 	 * If <check> is false, we're sure the link hasn't changed.
43281369Sdduvall 	 * If true, however, it's not yet definitive; we have to call
43291369Sdduvall 	 * bge_phys_check() to determine whether the link has settled
43301369Sdduvall 	 * into a new state yet ... and if it has, then call the link
43311369Sdduvall 	 * state change handler.But when the chip is 5700 in Dell 6650
43321369Sdduvall 	 * ,even if check is false, the link may have changed.So we
43331369Sdduvall 	 * have to call bge_phys_check() to determine the link state.
43341369Sdduvall 	 */
43351369Sdduvall 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
43361369Sdduvall 		check = bge_phys_check(bgep);
43371369Sdduvall 		if (check)
43381369Sdduvall 			bge_factotum_link_handler(bgep);
43391369Sdduvall 	}
43401369Sdduvall 
43411369Sdduvall 	return (check);
43421369Sdduvall }
43431369Sdduvall 
43441369Sdduvall /*
43451369Sdduvall  * Factotum routine to check for Tx stall, using the 'watchdog' counter
43461369Sdduvall  */
43471369Sdduvall static boolean_t bge_factotum_stall_check(bge_t *bgep);
43481369Sdduvall #pragma	no_inline(bge_factotum_stall_check)
43491369Sdduvall 
43501369Sdduvall static boolean_t
43511369Sdduvall bge_factotum_stall_check(bge_t *bgep)
43521369Sdduvall {
43531369Sdduvall 	uint32_t dogval;
43541369Sdduvall 
43551369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
43561369Sdduvall 
43571369Sdduvall 	/*
43581369Sdduvall 	 * Specific check for Tx stall ...
43591369Sdduvall 	 *
43601369Sdduvall 	 * The 'watchdog' counter is incremented whenever a packet
43611369Sdduvall 	 * is queued, reset to 1 when some (but not all) buffers
43621369Sdduvall 	 * are reclaimed, reset to 0 (disabled) when all buffers
43631369Sdduvall 	 * are reclaimed, and shifted left here.  If it exceeds the
43641369Sdduvall 	 * threshold value, the chip is assumed to have stalled and
43651369Sdduvall 	 * is put into the ERROR state.  The factotum will then reset
43661369Sdduvall 	 * it on the next pass.
43671369Sdduvall 	 *
43681369Sdduvall 	 * All of which should ensure that we don't get into a state
43691369Sdduvall 	 * where packets are left pending indefinitely!
43701369Sdduvall 	 */
43711369Sdduvall 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
43721369Sdduvall 	if (dogval < bge_watchdog_count)
43731369Sdduvall 		return (B_FALSE);
43741369Sdduvall 
43753918Sml149210 #if !defined(BGE_NETCONSOLE)
43761369Sdduvall 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
43773918Sml149210 #endif
43781865Sdilpreet 	bge_fm_ereport(bgep, DDI_FM_DEVICE_STALL);
43791369Sdduvall 	return (B_TRUE);
43801369Sdduvall }
43811369Sdduvall 
43821369Sdduvall /*
43831369Sdduvall  * The factotum is woken up when there's something to do that we'd rather
43841369Sdduvall  * not do from inside a hardware interrupt handler or high-level cyclic.
43851369Sdduvall  * Its two main tasks are:
43861369Sdduvall  *	reset & restart the chip after an error
43871369Sdduvall  *	check the link status whenever necessary
43881369Sdduvall  */
43891369Sdduvall uint_t bge_chip_factotum(caddr_t arg);
43901369Sdduvall #pragma	no_inline(bge_chip_factotum)
43911369Sdduvall 
43921369Sdduvall uint_t
43931369Sdduvall bge_chip_factotum(caddr_t arg)
43941369Sdduvall {
43951369Sdduvall 	bge_t *bgep;
43961369Sdduvall 	uint_t result;
43971369Sdduvall 	boolean_t error;
43981369Sdduvall 	boolean_t linkchg;
43991865Sdilpreet 	int dma_state;
44001369Sdduvall 
44011369Sdduvall 	bgep = (bge_t *)arg;
44021369Sdduvall 
44031369Sdduvall 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
44041369Sdduvall 
44051369Sdduvall 	mutex_enter(bgep->softintrlock);
44061369Sdduvall 	if (bgep->factotum_flag == 0) {
44071369Sdduvall 		mutex_exit(bgep->softintrlock);
44081369Sdduvall 		return (DDI_INTR_UNCLAIMED);
44091369Sdduvall 	}
44101504Sly149593 	bgep->factotum_flag = 0;
44111369Sdduvall 	mutex_exit(bgep->softintrlock);
44121369Sdduvall 
44131369Sdduvall 	result = DDI_INTR_CLAIMED;
44141369Sdduvall 	error = B_FALSE;
44151369Sdduvall 	linkchg = B_FALSE;
44161369Sdduvall 
44171369Sdduvall 	mutex_enter(bgep->genlock);
44181369Sdduvall 	switch (bgep->bge_chip_state) {
44191369Sdduvall 	default:
44201369Sdduvall 		break;
44211369Sdduvall 
44221369Sdduvall 	case BGE_CHIP_RUNNING:
44231865Sdilpreet 		linkchg = bge_factotum_link_check(bgep, &dma_state);
44241369Sdduvall 		error = bge_factotum_stall_check(bgep);
44251865Sdilpreet 		if (dma_state != DDI_FM_OK) {
44261865Sdilpreet 			bgep->bge_dma_error = B_TRUE;
44271865Sdilpreet 			error = B_TRUE;
44281865Sdilpreet 		}
44291865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
44301865Sdilpreet 			error = B_TRUE;
44311865Sdilpreet 		if (error)
44321865Sdilpreet 			bgep->bge_chip_state = BGE_CHIP_ERROR;
44331369Sdduvall 		break;
44341369Sdduvall 
44351369Sdduvall 	case BGE_CHIP_ERROR:
44361369Sdduvall 		error = B_TRUE;
44371369Sdduvall 		break;
44381369Sdduvall 
44391369Sdduvall 	case BGE_CHIP_FAULT:
44401369Sdduvall 		/*
44411369Sdduvall 		 * Fault detected, time to reset ...
44421369Sdduvall 		 */
44431369Sdduvall 		if (bge_autorecover) {
44441865Sdilpreet 			if (!(bgep->progress & PROGRESS_BUFS)) {
44451865Sdilpreet 				/*
44461865Sdilpreet 				 * if we can't allocate the ring buffers,
44471865Sdilpreet 				 * try later
44481865Sdilpreet 				 */
44491865Sdilpreet 				if (bge_alloc_bufs(bgep) != DDI_SUCCESS) {
44501865Sdilpreet 					mutex_exit(bgep->genlock);
44511865Sdilpreet 					return (result);
44521865Sdilpreet 				}
44531865Sdilpreet 				bgep->progress |= PROGRESS_BUFS;
44541865Sdilpreet 			}
44551865Sdilpreet 			if (!(bgep->progress & PROGRESS_INTR)) {
44561865Sdilpreet 				bge_init_rings(bgep);
44571865Sdilpreet 				bge_intr_enable(bgep);
44581865Sdilpreet 				bgep->progress |= PROGRESS_INTR;
44591865Sdilpreet 			}
44601865Sdilpreet 			if (!(bgep->progress & PROGRESS_KSTATS)) {
44611865Sdilpreet 				bge_init_kstats(bgep,
44621865Sdilpreet 				    ddi_get_instance(bgep->devinfo));
44631865Sdilpreet 				bgep->progress |= PROGRESS_KSTATS;
44641865Sdilpreet 			}
44651865Sdilpreet 
44661369Sdduvall 			BGE_REPORT((bgep, "automatic recovery activated"));
44671865Sdilpreet 
44681865Sdilpreet 			if (bge_restart(bgep, B_FALSE) != DDI_SUCCESS) {
44691865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
44701865Sdilpreet 				error = B_TRUE;
44711865Sdilpreet 			}
44721865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->cfg_handle) !=
44731865Sdilpreet 			    DDI_FM_OK) {
44741865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
44751865Sdilpreet 				error = B_TRUE;
44761865Sdilpreet 			}
44771865Sdilpreet 			if (bge_check_acc_handle(bgep, bgep->io_handle) !=
44781865Sdilpreet 			    DDI_FM_OK) {
44791865Sdilpreet 				bgep->bge_chip_state = BGE_CHIP_ERROR;
44801865Sdilpreet 				error = B_TRUE;
44811865Sdilpreet 			}
44821865Sdilpreet 			if (error == B_FALSE) {
44831408Srandyf #ifdef BGE_IPMI_ASF
44841865Sdilpreet 				if (bgep->asf_enabled &&
44851865Sdilpreet 				    bgep->asf_status != ASF_STAT_RUN) {
44861408Srandyf 					bgep->asf_timeout_id = timeout(
44871865Sdilpreet 					    bge_asf_heartbeat, (void *)bgep,
44881865Sdilpreet 					    drv_usectohz(
44891865Sdilpreet 					    BGE_ASF_HEARTBEAT_INTERVAL));
44901408Srandyf 					bgep->asf_status = ASF_STAT_RUN;
44911408Srandyf 				}
44921865Sdilpreet #endif
44935903Ssowmini 				if (!bgep->manual_reset) {
44945903Ssowmini 					ddi_fm_service_impact(bgep->devinfo,
44955903Ssowmini 					    DDI_SERVICE_RESTORED);
44965903Ssowmini 				}
44971408Srandyf 			}
44981369Sdduvall 		}
44991369Sdduvall 		break;
45001369Sdduvall 	}
45011369Sdduvall 
45021865Sdilpreet 
45031369Sdduvall 	/*
45041369Sdduvall 	 * If an error is detected, stop the chip now, marking it as
45051369Sdduvall 	 * faulty, so that it will be reset next time through ...
45061865Sdilpreet 	 *
45071865Sdilpreet 	 * Note that if intr_running is set, then bge_intr() has dropped
45081865Sdilpreet 	 * genlock to call bge_receive/bge_recycle. Can't stop the chip at
45091865Sdilpreet 	 * this point so have to wait until the next time the factotum runs.
45101369Sdduvall 	 */
45111865Sdilpreet 	if (error && !bgep->bge_intr_running) {
45121408Srandyf #ifdef BGE_IPMI_ASF
45131408Srandyf 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
45141408Srandyf 			/*
45151408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
45161408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
45171408Srandyf 			 * may crash.
45181408Srandyf 			 */
45191408Srandyf 			bge_asf_update_status(bgep);
45201408Srandyf 			bge_asf_stop_timer(bgep);
45211408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
45221408Srandyf 
45231408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
45241865Sdilpreet 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
45251408Srandyf 		}
45261408Srandyf #endif
45271369Sdduvall 		bge_chip_stop(bgep, B_TRUE);
45281865Sdilpreet 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
45291408Srandyf 	}
45301369Sdduvall 	mutex_exit(bgep->genlock);
45311369Sdduvall 
45321369Sdduvall 	/*
45331369Sdduvall 	 * If the link state changed, tell the world about it.
45341369Sdduvall 	 * Note: can't do this while still holding the mutex.
45351369Sdduvall 	 */
4536*6546Sgh162552 	if (bgep->link_update_timer == BGE_LINK_UPDATE_TIMEOUT &&
4537*6546Sgh162552 	    bgep->link_state != LINK_STATE_UNKNOWN)
4538*6546Sgh162552 		linkchg = B_TRUE;
4539*6546Sgh162552 	else if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT &&
4540*6546Sgh162552 	    bgep->link_state == LINK_STATE_DOWN)
4541*6546Sgh162552 		linkchg = B_FALSE;
4542*6546Sgh162552 
4543*6546Sgh162552 	if (linkchg) {
45442311Sseb 		mac_link_update(bgep->mh, bgep->link_state);
4545*6546Sgh162552 		bgep->link_update_timer = BGE_LINK_UPDATE_DONE;
4546*6546Sgh162552 	}
45475903Ssowmini 	if (bgep->manual_reset) {
45485903Ssowmini 		bgep->manual_reset = B_FALSE;
45495903Ssowmini 	}
45501369Sdduvall 
45511369Sdduvall 	return (result);
45521369Sdduvall }
45531369Sdduvall 
45541369Sdduvall /*
45551369Sdduvall  * High-level cyclic handler
45561369Sdduvall  *
45571369Sdduvall  * This routine schedules a (low-level) softint callback to the
45581369Sdduvall  * factotum, and prods the chip to update the status block (which
45591369Sdduvall  * will cause a hardware interrupt when complete).
45601369Sdduvall  */
45611369Sdduvall void bge_chip_cyclic(void *arg);
45621369Sdduvall #pragma	no_inline(bge_chip_cyclic)
45631369Sdduvall 
45641369Sdduvall void
45651369Sdduvall bge_chip_cyclic(void *arg)
45661369Sdduvall {
45671369Sdduvall 	bge_t *bgep;
45681369Sdduvall 
45691369Sdduvall 	bgep = arg;
45701369Sdduvall 
45711369Sdduvall 	switch (bgep->bge_chip_state) {
45721369Sdduvall 	default:
45731369Sdduvall 		return;
45741369Sdduvall 
45751369Sdduvall 	case BGE_CHIP_RUNNING:
45761369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
45771865Sdilpreet 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
45781865Sdilpreet 			ddi_fm_service_impact(bgep->devinfo,
45791865Sdilpreet 			    DDI_SERVICE_UNAFFECTED);
4580*6546Sgh162552 
4581*6546Sgh162552 		if (bgep->link_update_timer < BGE_LINK_UPDATE_TIMEOUT)
4582*6546Sgh162552 			bgep->link_update_timer++;
4583*6546Sgh162552 
45841369Sdduvall 		break;
45851369Sdduvall 
45861369Sdduvall 	case BGE_CHIP_FAULT:
45871369Sdduvall 	case BGE_CHIP_ERROR:
45881369Sdduvall 		break;
45891369Sdduvall 	}
45901369Sdduvall 
45911369Sdduvall 	bge_wake_factotum(bgep);
45921369Sdduvall }
45931369Sdduvall 
45941369Sdduvall 
45951369Sdduvall /*
45961369Sdduvall  * ========== Ioctl subfunctions ==========
45971369Sdduvall  */
45981369Sdduvall 
45991369Sdduvall #undef	BGE_DBG
46001369Sdduvall #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
46011369Sdduvall 
46021369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
46031369Sdduvall 
46041369Sdduvall static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
46051369Sdduvall #pragma	no_inline(bge_chip_peek_cfg)
46061369Sdduvall 
46071369Sdduvall static void
46081369Sdduvall bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
46091369Sdduvall {
46101369Sdduvall 	uint64_t regval;
46111369Sdduvall 	uint64_t regno;
46121369Sdduvall 
46131369Sdduvall 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
46144588Sml149210 	    (void *)bgep, (void *)ppd));
46151369Sdduvall 
46161369Sdduvall 	regno = ppd->pp_acc_offset;
46171369Sdduvall 
46181369Sdduvall 	switch (ppd->pp_acc_size) {
46191369Sdduvall 	case 1:
46201369Sdduvall 		regval = pci_config_get8(bgep->cfg_handle, regno);
46211369Sdduvall 		break;
46221369Sdduvall 
46231369Sdduvall 	case 2:
46241369Sdduvall 		regval = pci_config_get16(bgep->cfg_handle, regno);
46251369Sdduvall 		break;
46261369Sdduvall 
46271369Sdduvall 	case 4:
46281369Sdduvall 		regval = pci_config_get32(bgep->cfg_handle, regno);
46291369Sdduvall 		break;
46301369Sdduvall 
46311369Sdduvall 	case 8:
46321369Sdduvall 		regval = pci_config_get64(bgep->cfg_handle, regno);
46331369Sdduvall 		break;
46341369Sdduvall 	}
46351369Sdduvall 
46361369Sdduvall 	ppd->pp_acc_data = regval;
46371369Sdduvall }
46381369Sdduvall 
46391369Sdduvall static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
46401369Sdduvall #pragma	no_inline(bge_chip_poke_cfg)
46411369Sdduvall 
46421369Sdduvall static void
46431369Sdduvall bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
46441369Sdduvall {
46451369Sdduvall 	uint64_t regval;
46461369Sdduvall 	uint64_t regno;
46471369Sdduvall 
46481369Sdduvall 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
46494588Sml149210 	    (void *)bgep, (void *)ppd));
46501369Sdduvall 
46511369Sdduvall 	regno = ppd->pp_acc_offset;
46521369Sdduvall 	regval = ppd->pp_acc_data;
46531369Sdduvall 
46541369Sdduvall 	switch (ppd->pp_acc_size) {
46551369Sdduvall 	case 1:
46561369Sdduvall 		pci_config_put8(bgep->cfg_handle, regno, regval);
46571369Sdduvall 		break;
46581369Sdduvall 
46591369Sdduvall 	case 2:
46601369Sdduvall 		pci_config_put16(bgep->cfg_handle, regno, regval);
46611369Sdduvall 		break;
46621369Sdduvall 
46631369Sdduvall 	case 4:
46641369Sdduvall 		pci_config_put32(bgep->cfg_handle, regno, regval);
46651369Sdduvall 		break;
46661369Sdduvall 
46671369Sdduvall 	case 8:
46681369Sdduvall 		pci_config_put64(bgep->cfg_handle, regno, regval);
46691369Sdduvall 		break;
46701369Sdduvall 	}
46711369Sdduvall }
46721369Sdduvall 
46731369Sdduvall static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
46741369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
46751369Sdduvall 
46761369Sdduvall static void
46771369Sdduvall bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
46781369Sdduvall {
46791369Sdduvall 	uint64_t regval;
46801369Sdduvall 	void *regaddr;
46811369Sdduvall 
46821369Sdduvall 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
46834588Sml149210 	    (void *)bgep, (void *)ppd));
46841369Sdduvall 
46851369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
46861369Sdduvall 
46871369Sdduvall 	switch (ppd->pp_acc_size) {
46881369Sdduvall 	case 1:
46891369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
46901369Sdduvall 		break;
46911369Sdduvall 
46921369Sdduvall 	case 2:
46931369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
46941369Sdduvall 		break;
46951369Sdduvall 
46961369Sdduvall 	case 4:
46971369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
46981369Sdduvall 		break;
46991369Sdduvall 
47001369Sdduvall 	case 8:
47011369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
47021369Sdduvall 		break;
47031369Sdduvall 	}
47041369Sdduvall 
47051369Sdduvall 	ppd->pp_acc_data = regval;
47061369Sdduvall }
47071369Sdduvall 
47081369Sdduvall static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
47091369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
47101369Sdduvall 
47111369Sdduvall static void
47121369Sdduvall bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
47131369Sdduvall {
47141369Sdduvall 	uint64_t regval;
47151369Sdduvall 	void *regaddr;
47161369Sdduvall 
47171369Sdduvall 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
47184588Sml149210 	    (void *)bgep, (void *)ppd));
47191369Sdduvall 
47201369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
47211369Sdduvall 	regval = ppd->pp_acc_data;
47221369Sdduvall 
47231369Sdduvall 	switch (ppd->pp_acc_size) {
47241369Sdduvall 	case 1:
47251369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
47261369Sdduvall 		break;
47271369Sdduvall 
47281369Sdduvall 	case 2:
47291369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
47301369Sdduvall 		break;
47311369Sdduvall 
47321369Sdduvall 	case 4:
47331369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
47341369Sdduvall 		break;
47351369Sdduvall 
47361369Sdduvall 	case 8:
47371369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
47381369Sdduvall 		break;
47391369Sdduvall 	}
47401369Sdduvall 	BGE_PCICHK(bgep);
47411369Sdduvall }
47421369Sdduvall 
47431369Sdduvall static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
47441369Sdduvall #pragma	no_inline(bge_chip_peek_nic)
47451369Sdduvall 
47461369Sdduvall static void
47471369Sdduvall bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
47481369Sdduvall {
47491369Sdduvall 	uint64_t regoff;
47501369Sdduvall 	uint64_t regval;
47511369Sdduvall 	void *regaddr;
47521369Sdduvall 
47531369Sdduvall 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
47544588Sml149210 	    (void *)bgep, (void *)ppd));
47551369Sdduvall 
47561369Sdduvall 	regoff = ppd->pp_acc_offset;
47571369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
47581369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
47591369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
47601369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
47611369Sdduvall 
47621369Sdduvall 	switch (ppd->pp_acc_size) {
47631369Sdduvall 	case 1:
47641369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
47651369Sdduvall 		break;
47661369Sdduvall 
47671369Sdduvall 	case 2:
47681369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
47691369Sdduvall 		break;
47701369Sdduvall 
47711369Sdduvall 	case 4:
47721369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
47731369Sdduvall 		break;
47741369Sdduvall 
47751369Sdduvall 	case 8:
47761369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
47771369Sdduvall 		break;
47781369Sdduvall 	}
47791369Sdduvall 
47801369Sdduvall 	ppd->pp_acc_data = regval;
47811369Sdduvall }
47821369Sdduvall 
47831369Sdduvall static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
47841369Sdduvall #pragma	no_inline(bge_chip_poke_nic)
47851369Sdduvall 
47861369Sdduvall static void
47871369Sdduvall bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
47881369Sdduvall {
47891369Sdduvall 	uint64_t regoff;
47901369Sdduvall 	uint64_t regval;
47911369Sdduvall 	void *regaddr;
47921369Sdduvall 
47931369Sdduvall 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
47944588Sml149210 	    (void *)bgep, (void *)ppd));
47951369Sdduvall 
47961369Sdduvall 	regoff = ppd->pp_acc_offset;
47971369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
47981369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
47991369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
48001369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
48011369Sdduvall 	regval = ppd->pp_acc_data;
48021369Sdduvall 
48031369Sdduvall 	switch (ppd->pp_acc_size) {
48041369Sdduvall 	case 1:
48051369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
48061369Sdduvall 		break;
48071369Sdduvall 
48081369Sdduvall 	case 2:
48091369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
48101369Sdduvall 		break;
48111369Sdduvall 
48121369Sdduvall 	case 4:
48131369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
48141369Sdduvall 		break;
48151369Sdduvall 
48161369Sdduvall 	case 8:
48171369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
48181369Sdduvall 		break;
48191369Sdduvall 	}
48201369Sdduvall 	BGE_PCICHK(bgep);
48211369Sdduvall }
48221369Sdduvall 
48231369Sdduvall static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
48241369Sdduvall #pragma	no_inline(bge_chip_peek_mii)
48251369Sdduvall 
48261369Sdduvall static void
48271369Sdduvall bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
48281369Sdduvall {
48291369Sdduvall 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
48304588Sml149210 	    (void *)bgep, (void *)ppd));
48311369Sdduvall 
48321369Sdduvall 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
48331369Sdduvall }
48341369Sdduvall 
48351369Sdduvall static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
48361369Sdduvall #pragma	no_inline(bge_chip_poke_mii)
48371369Sdduvall 
48381369Sdduvall static void
48391369Sdduvall bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
48401369Sdduvall {
48411369Sdduvall 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
48424588Sml149210 	    (void *)bgep, (void *)ppd));
48431369Sdduvall 
48441369Sdduvall 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
48451369Sdduvall }
48461369Sdduvall 
48471369Sdduvall #if	BGE_SEE_IO32
48481369Sdduvall 
48491369Sdduvall static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
48501369Sdduvall #pragma	no_inline(bge_chip_peek_seeprom)
48511369Sdduvall 
48521369Sdduvall static void
48531369Sdduvall bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
48541369Sdduvall {
48551369Sdduvall 	uint32_t data;
48561369Sdduvall 	int err;
48571369Sdduvall 
48581369Sdduvall 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
48594588Sml149210 	    (void *)bgep, (void *)ppd));
48601369Sdduvall 
48611369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
48621369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
48631369Sdduvall }
48641369Sdduvall 
48651369Sdduvall static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
48661369Sdduvall #pragma	no_inline(bge_chip_poke_seeprom)
48671369Sdduvall 
48681369Sdduvall static void
48691369Sdduvall bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
48701369Sdduvall {
48711369Sdduvall 	uint32_t data;
48721369Sdduvall 
48731369Sdduvall 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
48744588Sml149210 	    (void *)bgep, (void *)ppd));
48751369Sdduvall 
48761369Sdduvall 	data = ppd->pp_acc_data;
48771369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
48781369Sdduvall }
48791369Sdduvall #endif	/* BGE_SEE_IO32 */
48801369Sdduvall 
48811369Sdduvall #if	BGE_FLASH_IO32
48821369Sdduvall 
48831369Sdduvall static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
48841369Sdduvall #pragma	no_inline(bge_chip_peek_flash)
48851369Sdduvall 
48861369Sdduvall static void
48871369Sdduvall bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
48881369Sdduvall {
48891369Sdduvall 	uint32_t data;
48901369Sdduvall 	int err;
48911369Sdduvall 
48921369Sdduvall 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
48934588Sml149210 	    (void *)bgep, (void *)ppd));
48941369Sdduvall 
48951369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
48961369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
48971369Sdduvall }
48981369Sdduvall 
48991369Sdduvall static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
49001369Sdduvall #pragma	no_inline(bge_chip_poke_flash)
49011369Sdduvall 
49021369Sdduvall static void
49031369Sdduvall bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
49041369Sdduvall {
49051369Sdduvall 	uint32_t data;
49061369Sdduvall 
49071369Sdduvall 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
49084588Sml149210 	    (void *)bgep, (void *)ppd));
49091369Sdduvall 
49101369Sdduvall 	data = ppd->pp_acc_data;
49111369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
49121369Sdduvall 	    ppd->pp_acc_offset, &data);
49131369Sdduvall }
49141369Sdduvall #endif	/* BGE_FLASH_IO32 */
49151369Sdduvall 
49161369Sdduvall static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
49171369Sdduvall #pragma	no_inline(bge_chip_peek_mem)
49181369Sdduvall 
49191369Sdduvall static void
49201369Sdduvall bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
49211369Sdduvall {
49221369Sdduvall 	uint64_t regval;
49231369Sdduvall 	void *vaddr;
49241369Sdduvall 
49251369Sdduvall 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
49264588Sml149210 	    (void *)bgep, (void *)ppd));
49271369Sdduvall 
49281369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
49291369Sdduvall 
49301369Sdduvall 	switch (ppd->pp_acc_size) {
49311369Sdduvall 	case 1:
49321369Sdduvall 		regval = *(uint8_t *)vaddr;
49331369Sdduvall 		break;
49341369Sdduvall 
49351369Sdduvall 	case 2:
49361369Sdduvall 		regval = *(uint16_t *)vaddr;
49371369Sdduvall 		break;
49381369Sdduvall 
49391369Sdduvall 	case 4:
49401369Sdduvall 		regval = *(uint32_t *)vaddr;
49411369Sdduvall 		break;
49421369Sdduvall 
49431369Sdduvall 	case 8:
49441369Sdduvall 		regval = *(uint64_t *)vaddr;
49451369Sdduvall 		break;
49461369Sdduvall 	}
49471369Sdduvall 
49481369Sdduvall 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
49494588Sml149210 	    (void *)bgep, (void *)ppd, regval, vaddr));
49501369Sdduvall 
49511369Sdduvall 	ppd->pp_acc_data = regval;
49521369Sdduvall }
49531369Sdduvall 
49541369Sdduvall static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
49551369Sdduvall #pragma	no_inline(bge_chip_poke_mem)
49561369Sdduvall 
49571369Sdduvall static void
49581369Sdduvall bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
49591369Sdduvall {
49601369Sdduvall 	uint64_t regval;
49611369Sdduvall 	void *vaddr;
49621369Sdduvall 
49631369Sdduvall 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
49644588Sml149210 	    (void *)bgep, (void *)ppd));
49651369Sdduvall 
49661369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
49671369Sdduvall 	regval = ppd->pp_acc_data;
49681369Sdduvall 
49691369Sdduvall 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
49704588Sml149210 	    (void *)bgep, (void *)ppd, regval, vaddr));
49711369Sdduvall 
49721369Sdduvall 	switch (ppd->pp_acc_size) {
49731369Sdduvall 	case 1:
49741369Sdduvall 		*(uint8_t *)vaddr = (uint8_t)regval;
49751369Sdduvall 		break;
49761369Sdduvall 
49771369Sdduvall 	case 2:
49781369Sdduvall 		*(uint16_t *)vaddr = (uint16_t)regval;
49791369Sdduvall 		break;
49801369Sdduvall 
49811369Sdduvall 	case 4:
49821369Sdduvall 		*(uint32_t *)vaddr = (uint32_t)regval;
49831369Sdduvall 		break;
49841369Sdduvall 
49851369Sdduvall 	case 8:
49861369Sdduvall 		*(uint64_t *)vaddr = (uint64_t)regval;
49871369Sdduvall 		break;
49881369Sdduvall 	}
49891369Sdduvall }
49901369Sdduvall 
49911369Sdduvall static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
49921369Sdduvall 					struct iocblk *iocp);
49931369Sdduvall #pragma	no_inline(bge_pp_ioctl)
49941369Sdduvall 
49951369Sdduvall static enum ioc_reply
49961369Sdduvall bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
49971369Sdduvall {
49981369Sdduvall 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
49991369Sdduvall 	bge_peekpoke_t *ppd;
50001369Sdduvall 	dma_area_t *areap;
50011369Sdduvall 	uint64_t sizemask;
50021369Sdduvall 	uint64_t mem_va;
50031369Sdduvall 	uint64_t maxoff;
50041369Sdduvall 	boolean_t peek;
50051369Sdduvall 
50061369Sdduvall 	switch (cmd) {
50071369Sdduvall 	default:
50081369Sdduvall 		/* NOTREACHED */
50091369Sdduvall 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
50101369Sdduvall 		return (IOC_INVAL);
50111369Sdduvall 
50121369Sdduvall 	case BGE_PEEK:
50131369Sdduvall 		peek = B_TRUE;
50141369Sdduvall 		break;
50151369Sdduvall 
50161369Sdduvall 	case BGE_POKE:
50171369Sdduvall 		peek = B_FALSE;
50181369Sdduvall 		break;
50191369Sdduvall 	}
50201369Sdduvall 
50211369Sdduvall 	/*
50221369Sdduvall 	 * Validate format of ioctl
50231369Sdduvall 	 */
50241369Sdduvall 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
50251369Sdduvall 		return (IOC_INVAL);
50261369Sdduvall 	if (mp->b_cont == NULL)
50271369Sdduvall 		return (IOC_INVAL);
50281369Sdduvall 	ppd = (bge_peekpoke_t *)mp->b_cont->b_rptr;
50291369Sdduvall 
50301369Sdduvall 	/*
50311369Sdduvall 	 * Validate request parameters
50321369Sdduvall 	 */
50331369Sdduvall 	switch (ppd->pp_acc_space) {
50341369Sdduvall 	default:
50351369Sdduvall 		return (IOC_INVAL);
50361369Sdduvall 
50371369Sdduvall 	case BGE_PP_SPACE_CFG:
50381369Sdduvall 		/*
50391369Sdduvall 		 * Config space
50401369Sdduvall 		 */
50411369Sdduvall 		sizemask = 8|4|2|1;
50421369Sdduvall 		mem_va = 0;
50431369Sdduvall 		maxoff = PCI_CONF_HDR_SIZE;
50441369Sdduvall 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
50451369Sdduvall 		break;
50461369Sdduvall 
50471369Sdduvall 	case BGE_PP_SPACE_REG:
50481369Sdduvall 		/*
50491369Sdduvall 		 * Memory-mapped I/O space
50501369Sdduvall 		 */
50511369Sdduvall 		sizemask = 8|4|2|1;
50521369Sdduvall 		mem_va = 0;
50531369Sdduvall 		maxoff = RIAAR_REGISTER_MAX;
50541369Sdduvall 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
50551369Sdduvall 		break;
50561369Sdduvall 
50571369Sdduvall 	case BGE_PP_SPACE_NIC:
50581369Sdduvall 		/*
50591369Sdduvall 		 * NIC on-chip memory
50601369Sdduvall 		 */
50611369Sdduvall 		sizemask = 8|4|2|1;
50621369Sdduvall 		mem_va = 0;
50631369Sdduvall 		maxoff = MWBAR_ONCHIP_MAX;
50641369Sdduvall 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
50651369Sdduvall 		break;
50661369Sdduvall 
50671369Sdduvall 	case BGE_PP_SPACE_MII:
50681369Sdduvall 		/*
50691369Sdduvall 		 * PHY's MII registers
50701369Sdduvall 		 * NB: all PHY registers are two bytes, but the
50711369Sdduvall 		 * addresses increment in ones (word addressing).
50721369Sdduvall 		 * So we scale the address here, then undo the
50731369Sdduvall 		 * transformation inside the peek/poke functions.
50741369Sdduvall 		 */
50751369Sdduvall 		ppd->pp_acc_offset *= 2;
50761369Sdduvall 		sizemask = 2;
50771369Sdduvall 		mem_va = 0;
50781369Sdduvall 		maxoff = (MII_MAXREG+1)*2;
50791369Sdduvall 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
50801369Sdduvall 		break;
50811369Sdduvall 
50821369Sdduvall #if	BGE_SEE_IO32
50831369Sdduvall 	case BGE_PP_SPACE_SEEPROM:
50841369Sdduvall 		/*
50851369Sdduvall 		 * Attached SEEPROM(s), if any.
50861369Sdduvall 		 * NB: we use the high-order bits of the 'address' as
50871369Sdduvall 		 * a device select to accommodate multiple SEEPROMS,
50881369Sdduvall 		 * If each one is the maximum size (64kbytes), this
50891369Sdduvall 		 * makes them appear contiguous.  Otherwise, there may
50901369Sdduvall 		 * be holes in the mapping.  ENxS doesn't have any
50911369Sdduvall 		 * SEEPROMs anyway ...
50921369Sdduvall 		 */
50931369Sdduvall 		sizemask = 4;
50941369Sdduvall 		mem_va = 0;
50951369Sdduvall 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
50961369Sdduvall 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
50971369Sdduvall 		break;
50981369Sdduvall #endif	/* BGE_SEE_IO32 */
50991369Sdduvall 
51001369Sdduvall #if	BGE_FLASH_IO32
51011369Sdduvall 	case BGE_PP_SPACE_FLASH:
51021369Sdduvall 		/*
51031369Sdduvall 		 * Attached Flash device (if any); a maximum of one device
51041369Sdduvall 		 * is currently supported.  But it can be up to 1MB (unlike
51051369Sdduvall 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
51061369Sdduvall 		 */
51071369Sdduvall 		sizemask = 4;
51081369Sdduvall 		mem_va = 0;
51091369Sdduvall 		maxoff = NVM_FLASH_ADDR_MASK;
51101369Sdduvall 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
51111369Sdduvall 		break;
51121369Sdduvall #endif	/* BGE_FLASH_IO32 */
51131369Sdduvall 
51141369Sdduvall 	case BGE_PP_SPACE_BGE:
51151369Sdduvall 		/*
51161369Sdduvall 		 * BGE data structure!
51171369Sdduvall 		 */
51181369Sdduvall 		sizemask = 8|4|2|1;
51191369Sdduvall 		mem_va = (uintptr_t)bgep;
51201369Sdduvall 		maxoff = sizeof (*bgep);
51211369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
51221369Sdduvall 		break;
51231369Sdduvall 
51241369Sdduvall 	case BGE_PP_SPACE_STATUS:
51251369Sdduvall 	case BGE_PP_SPACE_STATISTICS:
51261369Sdduvall 	case BGE_PP_SPACE_TXDESC:
51271369Sdduvall 	case BGE_PP_SPACE_TXBUFF:
51281369Sdduvall 	case BGE_PP_SPACE_RXDESC:
51291369Sdduvall 	case BGE_PP_SPACE_RXBUFF:
51301369Sdduvall 		/*
51311369Sdduvall 		 * Various DMA_AREAs
51321369Sdduvall 		 */
51331369Sdduvall 		switch (ppd->pp_acc_space) {
51341369Sdduvall 		case BGE_PP_SPACE_TXDESC:
51351369Sdduvall 			areap = &bgep->tx_desc;
51361369Sdduvall 			break;
51371369Sdduvall 		case BGE_PP_SPACE_TXBUFF:
51381369Sdduvall 			areap = &bgep->tx_buff[0];
51391369Sdduvall 			break;
51401369Sdduvall 		case BGE_PP_SPACE_RXDESC:
51411369Sdduvall 			areap = &bgep->rx_desc[0];
51421369Sdduvall 			break;
51431369Sdduvall 		case BGE_PP_SPACE_RXBUFF:
51441369Sdduvall 			areap = &bgep->rx_buff[0];
51451369Sdduvall 			break;
51461369Sdduvall 		case BGE_PP_SPACE_STATUS:
51471369Sdduvall 			areap = &bgep->status_block;
51481369Sdduvall 			break;
51491369Sdduvall 		case BGE_PP_SPACE_STATISTICS:
51501369Sdduvall 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
51511369Sdduvall 				areap = &bgep->statistics;
51521369Sdduvall 			break;
51531369Sdduvall 		}
51541369Sdduvall 
51551369Sdduvall 		sizemask = 8|4|2|1;
51561369Sdduvall 		mem_va = (uintptr_t)areap->mem_va;
51571369Sdduvall 		maxoff = areap->alength;
51581369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
51591369Sdduvall 		break;
51601369Sdduvall 	}
51611369Sdduvall 
51621369Sdduvall 	switch (ppd->pp_acc_size) {
51631369Sdduvall 	default:
51641369Sdduvall 		return (IOC_INVAL);
51651369Sdduvall 
51661369Sdduvall 	case 8:
51671369Sdduvall 	case 4:
51681369Sdduvall 	case 2:
51691369Sdduvall 	case 1:
51701369Sdduvall 		if ((ppd->pp_acc_size & sizemask) == 0)
51711369Sdduvall 			return (IOC_INVAL);
51721369Sdduvall 		break;
51731369Sdduvall 	}
51741369Sdduvall 
51751369Sdduvall 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
51761369Sdduvall 		return (IOC_INVAL);
51771369Sdduvall 
51781369Sdduvall 	if (ppd->pp_acc_offset >= maxoff)
51791369Sdduvall 		return (IOC_INVAL);
51801369Sdduvall 
51811369Sdduvall 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
51821369Sdduvall 		return (IOC_INVAL);
51831369Sdduvall 
51841369Sdduvall 	/*
51851369Sdduvall 	 * All OK - go do it!
51861369Sdduvall 	 */
51871369Sdduvall 	ppd->pp_acc_offset += mem_va;
51881369Sdduvall 	(*ppfn)(bgep, ppd);
51891369Sdduvall 	return (peek ? IOC_REPLY : IOC_ACK);
51901369Sdduvall }
51911369Sdduvall 
51921369Sdduvall static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
51931369Sdduvall 					struct iocblk *iocp);
51941369Sdduvall #pragma	no_inline(bge_diag_ioctl)
51951369Sdduvall 
51961369Sdduvall static enum ioc_reply
51971369Sdduvall bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
51981369Sdduvall {
51991369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
52001369Sdduvall 
52011369Sdduvall 	switch (cmd) {
52021369Sdduvall 	default:
52031369Sdduvall 		/* NOTREACHED */
52041369Sdduvall 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
52051369Sdduvall 		return (IOC_INVAL);
52061369Sdduvall 
52071369Sdduvall 	case BGE_DIAG:
52081369Sdduvall 		/*
52091369Sdduvall 		 * Currently a no-op
52101369Sdduvall 		 */
52111369Sdduvall 		return (IOC_ACK);
52121369Sdduvall 
52131369Sdduvall 	case BGE_PEEK:
52141369Sdduvall 	case BGE_POKE:
52151369Sdduvall 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
52161369Sdduvall 
52171369Sdduvall 	case BGE_PHY_RESET:
52181369Sdduvall 		return (IOC_RESTART_ACK);
52191369Sdduvall 
52201369Sdduvall 	case BGE_SOFT_RESET:
52211369Sdduvall 	case BGE_HARD_RESET:
52221369Sdduvall 		/*
52231369Sdduvall 		 * Reset and reinitialise the 570x hardware
52241369Sdduvall 		 */
52253918Sml149210 		bgep->bge_chip_state = BGE_CHIP_FAULT;
52263918Sml149210 		ddi_trigger_softintr(bgep->factotum_id);
52271865Sdilpreet 		(void) bge_restart(bgep, cmd == BGE_HARD_RESET);
52281369Sdduvall 		return (IOC_ACK);
52291369Sdduvall 	}
52301369Sdduvall 
52311369Sdduvall 	/* NOTREACHED */
52321369Sdduvall }
52331369Sdduvall 
52341369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
52351369Sdduvall 
52361369Sdduvall static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
52371369Sdduvall 				    struct iocblk *iocp);
52381369Sdduvall #pragma	no_inline(bge_mii_ioctl)
52391369Sdduvall 
52401369Sdduvall static enum ioc_reply
52411369Sdduvall bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
52421369Sdduvall {
52431369Sdduvall 	struct bge_mii_rw *miirwp;
52441369Sdduvall 
52451369Sdduvall 	/*
52461369Sdduvall 	 * Validate format of ioctl
52471369Sdduvall 	 */
52481369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
52491369Sdduvall 		return (IOC_INVAL);
52501369Sdduvall 	if (mp->b_cont == NULL)
52511369Sdduvall 		return (IOC_INVAL);
52521369Sdduvall 	miirwp = (struct bge_mii_rw *)mp->b_cont->b_rptr;
52531369Sdduvall 
52541369Sdduvall 	/*
52551369Sdduvall 	 * Validate request parameters ...
52561369Sdduvall 	 */
52571369Sdduvall 	if (miirwp->mii_reg > MII_MAXREG)
52581369Sdduvall 		return (IOC_INVAL);
52591369Sdduvall 
52601369Sdduvall 	switch (cmd) {
52611369Sdduvall 	default:
52621369Sdduvall 		/* NOTREACHED */
52631369Sdduvall 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
52641369Sdduvall 		return (IOC_INVAL);
52651369Sdduvall 
52661369Sdduvall 	case BGE_MII_READ:
52671369Sdduvall 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
52681369Sdduvall 		return (IOC_REPLY);
52691369Sdduvall 
52701369Sdduvall 	case BGE_MII_WRITE:
52711369Sdduvall 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
52721369Sdduvall 		return (IOC_ACK);
52731369Sdduvall 	}
52741369Sdduvall 
52751369Sdduvall 	/* NOTREACHED */
52761369Sdduvall }
52771369Sdduvall 
52781369Sdduvall #if	BGE_SEE_IO32
52791369Sdduvall 
52801369Sdduvall static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
52811369Sdduvall 				    struct iocblk *iocp);
52821369Sdduvall #pragma	no_inline(bge_see_ioctl)
52831369Sdduvall 
52841369Sdduvall static enum ioc_reply
52851369Sdduvall bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
52861369Sdduvall {
52871369Sdduvall 	struct bge_see_rw *seerwp;
52881369Sdduvall 
52891369Sdduvall 	/*
52901369Sdduvall 	 * Validate format of ioctl
52911369Sdduvall 	 */
52921369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
52931369Sdduvall 		return (IOC_INVAL);
52941369Sdduvall 	if (mp->b_cont == NULL)
52951369Sdduvall 		return (IOC_INVAL);
52961369Sdduvall 	seerwp = (struct bge_see_rw *)mp->b_cont->b_rptr;
52971369Sdduvall 
52981369Sdduvall 	/*
52991369Sdduvall 	 * Validate request parameters ...
53001369Sdduvall 	 */
53011369Sdduvall 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
53021369Sdduvall 		return (IOC_INVAL);
53031369Sdduvall 
53041369Sdduvall 	switch (cmd) {
53051369Sdduvall 	default:
53061369Sdduvall 		/* NOTREACHED */
53071369Sdduvall 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
53081369Sdduvall 		return (IOC_INVAL);
53091369Sdduvall 
53101369Sdduvall 	case BGE_SEE_READ:
53111369Sdduvall 	case BGE_SEE_WRITE:
53121369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
53131369Sdduvall 		    seerwp->see_addr, &seerwp->see_data);
53141369Sdduvall 		return (IOC_REPLY);
53151369Sdduvall 	}
53161369Sdduvall 
53171369Sdduvall 	/* NOTREACHED */
53181369Sdduvall }
53191369Sdduvall 
53201369Sdduvall #endif	/* BGE_SEE_IO32 */
53211369Sdduvall 
53221369Sdduvall #if	BGE_FLASH_IO32
53231369Sdduvall 
53241369Sdduvall static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
53251369Sdduvall 				    struct iocblk *iocp);
53261369Sdduvall #pragma	no_inline(bge_flash_ioctl)
53271369Sdduvall 
53281369Sdduvall static enum ioc_reply
53291369Sdduvall bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
53301369Sdduvall {
53311369Sdduvall 	struct bge_flash_rw *flashrwp;
53321369Sdduvall 
53331369Sdduvall 	/*
53341369Sdduvall 	 * Validate format of ioctl
53351369Sdduvall 	 */
53361369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
53371369Sdduvall 		return (IOC_INVAL);
53381369Sdduvall 	if (mp->b_cont == NULL)
53391369Sdduvall 		return (IOC_INVAL);
53401369Sdduvall 	flashrwp = (struct bge_flash_rw *)mp->b_cont->b_rptr;
53411369Sdduvall 
53421369Sdduvall 	/*
53431369Sdduvall 	 * Validate request parameters ...
53441369Sdduvall 	 */
53451369Sdduvall 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
53461369Sdduvall 		return (IOC_INVAL);
53471369Sdduvall 
53481369Sdduvall 	switch (cmd) {
53491369Sdduvall 	default:
53501369Sdduvall 		/* NOTREACHED */
53511369Sdduvall 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
53521369Sdduvall 		return (IOC_INVAL);
53531369Sdduvall 
53541369Sdduvall 	case BGE_FLASH_READ:
53551369Sdduvall 	case BGE_FLASH_WRITE:
53561369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
53571369Sdduvall 		    flashrwp->flash_addr, &flashrwp->flash_data);
53581369Sdduvall 		return (IOC_REPLY);
53591369Sdduvall 	}
53601369Sdduvall 
53611369Sdduvall 	/* NOTREACHED */
53621369Sdduvall }
53631369Sdduvall 
53641369Sdduvall #endif	/* BGE_FLASH_IO32 */
53651369Sdduvall 
53661369Sdduvall enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
53671369Sdduvall 				struct iocblk *iocp);
53681369Sdduvall #pragma	no_inline(bge_chip_ioctl)
53691369Sdduvall 
53701369Sdduvall enum ioc_reply
53711369Sdduvall bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
53721369Sdduvall {
53731369Sdduvall 	int cmd;
53741369Sdduvall 
53751369Sdduvall 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
53764588Sml149210 	    (void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
53771369Sdduvall 
53781369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
53791369Sdduvall 
53801369Sdduvall 	cmd = iocp->ioc_cmd;
53811369Sdduvall 	switch (cmd) {
53821369Sdduvall 	default:
53831369Sdduvall 		/* NOTREACHED */
53841369Sdduvall 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
53851369Sdduvall 		return (IOC_INVAL);
53861369Sdduvall 
53871369Sdduvall 	case BGE_DIAG:
53881369Sdduvall 	case BGE_PEEK:
53891369Sdduvall 	case BGE_POKE:
53901369Sdduvall 	case BGE_PHY_RESET:
53911369Sdduvall 	case BGE_SOFT_RESET:
53921369Sdduvall 	case BGE_HARD_RESET:
53931369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
53941369Sdduvall 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
53951369Sdduvall #else
53961369Sdduvall 		return (IOC_INVAL);
53971369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
53981369Sdduvall 
53991369Sdduvall 	case BGE_MII_READ:
54001369Sdduvall 	case BGE_MII_WRITE:
54011369Sdduvall 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
54021369Sdduvall 
54031369Sdduvall #if	BGE_SEE_IO32
54041369Sdduvall 	case BGE_SEE_READ:
54051369Sdduvall 	case BGE_SEE_WRITE:
54061369Sdduvall 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
54071369Sdduvall #endif	/* BGE_SEE_IO32 */
54081369Sdduvall 
54091369Sdduvall #if	BGE_FLASH_IO32
54101369Sdduvall 	case BGE_FLASH_READ:
54111369Sdduvall 	case BGE_FLASH_WRITE:
54121369Sdduvall 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
54131369Sdduvall #endif	/* BGE_FLASH_IO32 */
54141369Sdduvall 	}
54151369Sdduvall 
54161369Sdduvall 	/* NOTREACHED */
54171369Sdduvall }
54181369Sdduvall 
54191369Sdduvall void
54201369Sdduvall bge_chip_blank(void *arg, time_t ticks, uint_t count)
54211369Sdduvall {
54221369Sdduvall 	bge_t *bgep = arg;
54231369Sdduvall 
54241865Sdilpreet 	mutex_enter(bgep->genlock);
54251369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
54261369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
54271865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
54281865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
54291865Sdilpreet 	mutex_exit(bgep->genlock);
54301369Sdduvall }
54311408Srandyf 
54321408Srandyf #ifdef BGE_IPMI_ASF
54331408Srandyf 
54341408Srandyf uint32_t
54351408Srandyf bge_nic_read32(bge_t *bgep, bge_regno_t addr)
54361408Srandyf {
54371408Srandyf 	uint32_t data;
54381408Srandyf 
54393918Sml149210 #ifndef __sparc
54401408Srandyf 	if (!bgep->asf_wordswapped) {
54411408Srandyf 		/* a workaround word swap error */
54421408Srandyf 		if (addr & 4)
54431408Srandyf 			addr = addr - 4;
54441408Srandyf 		else
54451408Srandyf 			addr = addr + 4;
54461408Srandyf 	}
54473918Sml149210 #endif
54481408Srandyf 
54491408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
54501408Srandyf 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
54511408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
54521408Srandyf 
54533918Sml149210 	data = LE_32(data);
54541408Srandyf 	return (data);
54551408Srandyf }
54561408Srandyf 
54571408Srandyf void
54581408Srandyf bge_asf_update_status(bge_t *bgep)
54591408Srandyf {
54601408Srandyf 	uint32_t event;
54611408Srandyf 
54621408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
54631408Srandyf 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
54641408Srandyf 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
54651408Srandyf 
54661408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
54671408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
54681408Srandyf }
54691408Srandyf 
54701408Srandyf 
54711408Srandyf /*
54721408Srandyf  * The driver is supposed to notify ASF that the OS is still running
54731408Srandyf  * every three seconds, otherwise the management server may attempt
54741408Srandyf  * to reboot the machine.  If it hasn't actually failed, this is
54752135Szh199473  * not a desirable result.  However, this isn't running as a real-time
54761408Srandyf  * thread, and even if it were, it might not be able to generate the
54771408Srandyf  * heartbeat in a timely manner due to system load.  As it isn't a
54781408Srandyf  * significant strain on the machine, we will set the interval to half
54791408Srandyf  * of the required value.
54801408Srandyf  */
54811408Srandyf void
54821865Sdilpreet bge_asf_heartbeat(void *arg)
54831408Srandyf {
54841865Sdilpreet 	bge_t *bgep = (bge_t *)arg;
54851865Sdilpreet 
54861865Sdilpreet 	mutex_enter(bgep->genlock);
54871408Srandyf 	bge_asf_update_status((bge_t *)bgep);
54881865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
54891865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
54901865Sdilpreet 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
54911865Sdilpreet 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
54921865Sdilpreet 	mutex_exit(bgep->genlock);
54931408Srandyf 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
54944588Sml149210 	    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
54951408Srandyf }
54961408Srandyf 
54971408Srandyf 
54981408Srandyf void
54991408Srandyf bge_asf_stop_timer(bge_t *bgep)
55001408Srandyf {
55011408Srandyf 	timeout_id_t tmp_id = 0;
55021408Srandyf 
55031408Srandyf 	while ((bgep->asf_timeout_id != 0) &&
55044588Sml149210 	    (tmp_id != bgep->asf_timeout_id)) {
55051408Srandyf 		tmp_id = bgep->asf_timeout_id;
55061408Srandyf 		(void) untimeout(tmp_id);
55071408Srandyf 	}
55081408Srandyf 	bgep->asf_timeout_id = 0;
55091408Srandyf }
55101408Srandyf 
55111408Srandyf 
55121408Srandyf 
55131408Srandyf /*
55142135Szh199473  * This function should be placed at the earliest position of bge_attach().
55151408Srandyf  */
55161408Srandyf void
55171408Srandyf bge_asf_get_config(bge_t *bgep)
55181408Srandyf {
55191408Srandyf 	uint32_t nicsig;
55201408Srandyf 	uint32_t niccfg;
55211408Srandyf 
55223918Sml149210 	bgep->asf_enabled = B_FALSE;
55231408Srandyf 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
55241408Srandyf 	if (nicsig == BGE_NIC_DATA_SIG) {
55251408Srandyf 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
55261408Srandyf 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
55271408Srandyf 			/*
55281408Srandyf 			 * Here, we don't consider BAXTER, because BGE haven't
55291408Srandyf 			 * supported BAXTER (that is 5752). Also, as I know,
55301408Srandyf 			 * BAXTER doesn't support ASF feature.
55311408Srandyf 			 */
55321408Srandyf 			bgep->asf_enabled = B_TRUE;
55331408Srandyf 		else
55341408Srandyf 			bgep->asf_enabled = B_FALSE;
55351408Srandyf 	} else
55361408Srandyf 		bgep->asf_enabled = B_FALSE;
55371408Srandyf }
55381408Srandyf 
55391408Srandyf 
55401408Srandyf void
55411408Srandyf bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
55421408Srandyf {
55431408Srandyf 	uint32_t tries;
55441408Srandyf 	uint32_t event;
55451408Srandyf 
55461408Srandyf 	ASSERT(bgep->asf_enabled);
55471408Srandyf 
55481408Srandyf 	/* Issues "pause firmware" command and wait for ACK */
55491408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
55501408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
55511408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
55521408Srandyf 
55531408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
55541408Srandyf 	tries = 0;
55551408Srandyf 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
55561408Srandyf 		drv_usecwait(1);
55571408Srandyf 		tries ++;
55581408Srandyf 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
55591408Srandyf 	}
55601408Srandyf 
55611408Srandyf 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
55624588Sml149210 	    BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
55631408Srandyf 
55641408Srandyf 	if (bgep->asf_newhandshake) {
55651408Srandyf 		switch (mode) {
55661408Srandyf 		case BGE_INIT_RESET:
55671408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55684588Sml149210 			    BGE_DRV_STATE_START);
55691408Srandyf 			break;
55701408Srandyf 		case BGE_SHUTDOWN_RESET:
55711408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55724588Sml149210 			    BGE_DRV_STATE_UNLOAD);
55731408Srandyf 			break;
55741408Srandyf 		case BGE_SUSPEND_RESET:
55751408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55764588Sml149210 			    BGE_DRV_STATE_SUSPEND);
55771408Srandyf 			break;
55781408Srandyf 		default:
55791408Srandyf 			break;
55801408Srandyf 		}
55811408Srandyf 	}
55821408Srandyf }
55831408Srandyf 
55841408Srandyf 
55851408Srandyf void
55861408Srandyf bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
55871408Srandyf {
55881408Srandyf 	switch (mode) {
55891408Srandyf 	case BGE_INIT_RESET:
55901408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55914588Sml149210 		    BGE_DRV_STATE_START);
55921408Srandyf 		break;
55931408Srandyf 	case BGE_SHUTDOWN_RESET:
55941408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55954588Sml149210 		    BGE_DRV_STATE_UNLOAD);
55961408Srandyf 		break;
55971408Srandyf 	case BGE_SUSPEND_RESET:
55981408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
55994588Sml149210 		    BGE_DRV_STATE_SUSPEND);
56001408Srandyf 		break;
56011408Srandyf 	default:
56021408Srandyf 		break;
56031408Srandyf 	}
56041408Srandyf }
56051408Srandyf 
56061408Srandyf 
56071408Srandyf void
56081408Srandyf bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
56091408Srandyf {
56101408Srandyf 	switch (mode) {
56111408Srandyf 	case BGE_INIT_RESET:
56121408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
56134588Sml149210 		    BGE_DRV_STATE_START_DONE);
56141408Srandyf 		break;
56151408Srandyf 	case BGE_SHUTDOWN_RESET:
56161408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
56174588Sml149210 		    BGE_DRV_STATE_UNLOAD_DONE);
56181408Srandyf 		break;
56191408Srandyf 	default:
56201408Srandyf 		break;
56211408Srandyf 	}
56221408Srandyf }
56231408Srandyf 
56241408Srandyf #endif /* BGE_IPMI_ASF */
5625