xref: /onnv-gate/usr/src/uts/common/io/bge/bge_chip2.c (revision 1611:2ffa29275ba7)
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 /*
231369Sdduvall  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
241369Sdduvall  * Use is subject to license terms.
251369Sdduvall  */
261369Sdduvall 
271369Sdduvall #pragma ident	"%Z%%M%	%I%	%E% SMI"
281369Sdduvall 
291369Sdduvall #include "sys/bge_impl2.h"
301369Sdduvall 
311369Sdduvall #define	PIO_ADDR(bgep, offset)	((void *)((caddr_t)(bgep)->io_regs+(offset)))
321369Sdduvall 
331369Sdduvall /*
341369Sdduvall  * Future features ... ?
351369Sdduvall  */
361369Sdduvall #define	BGE_CFG_IO8	0	/* 8/16-bit cfg space BIS/BIC	*/
371369Sdduvall #define	BGE_IND_IO32	0	/* indirect access code		*/
381369Sdduvall #define	BGE_SEE_IO32	1	/* SEEPROM access code		*/
391369Sdduvall #define	BGE_FLASH_IO32	1	/* FLASH access code		*/
401369Sdduvall 
411369Sdduvall /*
421369Sdduvall  * BGE MSI tunable:
431369Sdduvall  *
441369Sdduvall  * By default MSI is enabled on all supported platforms but it is disabled
451369Sdduvall  * for some Broadcom chips due to known MSI hardware issues. Currently MSI
461369Sdduvall  * is enabled only for 5714C A2 and 5715C A2 broadcom chips.
471369Sdduvall  */
481369Sdduvall #if defined(__sparc)
491369Sdduvall boolean_t bge_enable_msi = B_TRUE;
501369Sdduvall #else
511369Sdduvall boolean_t bge_enable_msi = B_FALSE;
521369Sdduvall #endif
531369Sdduvall 
541369Sdduvall /*
551369Sdduvall  * Property names
561369Sdduvall  */
571369Sdduvall static char knownids_propname[] = "bge-known-subsystems";
581369Sdduvall 
591369Sdduvall /*
601369Sdduvall  * Patchable globals:
611369Sdduvall  *
621369Sdduvall  *	bge_autorecover
631369Sdduvall  *		Enables/disables automatic recovery after fault detection
641369Sdduvall  *
651369Sdduvall  *	bge_mlcr_default
661369Sdduvall  *		Value to program into the MLCR; controls the chip's GPIO pins
671369Sdduvall  *
681369Sdduvall  *	bge_dma_{rd,wr}prio
691369Sdduvall  *		Relative priorities of DMA reads & DMA writes respectively.
701369Sdduvall  *		These may each be patched to any value 0-3.  Equal values
711369Sdduvall  *		will give "fair" (round-robin) arbitration for PCI access.
721369Sdduvall  *		Unequal values will give one or the other function priority.
731369Sdduvall  *
741369Sdduvall  *	bge_dma_rwctrl
751369Sdduvall  *		Value to put in the Read/Write DMA control register.  See
761369Sdduvall  *	        the Broadcom PRM for things you can fiddle with in this
771369Sdduvall  *		register ...
781369Sdduvall  *
791369Sdduvall  *	bge_{tx,rx}_{count,ticks}_{norm,intr}
801369Sdduvall  *		Send/receive interrupt coalescing parameters.  Counts are
811369Sdduvall  *		#s of descriptors, ticks are in microseconds.  *norm* values
821369Sdduvall  *		apply between status updates/interrupts; the *intr* values
831369Sdduvall  *		refer to the 'during-interrupt' versions - see the PRM.
841369Sdduvall  *
851369Sdduvall  *		NOTE: these values have been determined by measurement. They
861369Sdduvall  *		differ significantly from the values recommended in the PRM.
871369Sdduvall  */
881369Sdduvall static uint32_t bge_autorecover = 1;
891369Sdduvall static uint32_t bge_mlcr_default = MLCR_DEFAULT;
901369Sdduvall static uint32_t bge_mlcr_default_5714 = MLCR_DEFAULT_5714;
911369Sdduvall 
921369Sdduvall static uint32_t bge_dma_rdprio = 1;
931369Sdduvall static uint32_t bge_dma_wrprio = 0;
941369Sdduvall static uint32_t bge_dma_rwctrl = PDRWCR_VAR_DEFAULT;
951369Sdduvall static uint32_t bge_dma_rwctrl_5721 = PDRWCR_VAR_5721;
961369Sdduvall static uint32_t bge_dma_rwctrl_5714 = PDRWCR_VAR_5714;
971369Sdduvall static uint32_t bge_dma_rwctrl_5715 = PDRWCR_VAR_5715;
981369Sdduvall 
991369Sdduvall uint32_t bge_rx_ticks_norm = 128;
1001369Sdduvall uint32_t bge_tx_ticks_norm = 2048;		/* 8 for FJ2+ !?!?	*/
1011369Sdduvall uint32_t bge_rx_count_norm = 8;
1021369Sdduvall uint32_t bge_tx_count_norm = 128;
1031369Sdduvall 
1041369Sdduvall static uint32_t bge_rx_ticks_intr = 128;
1051369Sdduvall static uint32_t bge_tx_ticks_intr = 0;		/* 8 for FJ2+ !?!?	*/
1061369Sdduvall static uint32_t bge_rx_count_intr = 2;
1071369Sdduvall static uint32_t bge_tx_count_intr = 0;
1081369Sdduvall 
1091369Sdduvall /*
1101369Sdduvall  * Memory pool configuration parameters.
1111369Sdduvall  *
1121369Sdduvall  * These are generally specific to each member of the chip family, since
1131369Sdduvall  * each one may have a different memory size/configuration.
1141369Sdduvall  *
1151369Sdduvall  * Setting the mbuf pool length for a specific type of chip to 0 inhibits
1161369Sdduvall  * the driver from programming the various registers; instead they are left
1171369Sdduvall  * at their hardware defaults.  This is the preferred option for later chips
1181369Sdduvall  * (5705+), whereas the older chips *required* these registers to be set,
1191369Sdduvall  * since the h/w default was 0 ;-(
1201369Sdduvall  */
1211369Sdduvall static uint32_t bge_mbuf_pool_base	= MBUF_POOL_BASE_DEFAULT;
1221369Sdduvall static uint32_t bge_mbuf_pool_base_5704	= MBUF_POOL_BASE_5704;
1231369Sdduvall static uint32_t bge_mbuf_pool_base_5705	= MBUF_POOL_BASE_5705;
1241369Sdduvall static uint32_t bge_mbuf_pool_base_5721 = MBUF_POOL_BASE_5721;
1251369Sdduvall static uint32_t bge_mbuf_pool_len	= MBUF_POOL_LENGTH_DEFAULT;
1261369Sdduvall static uint32_t bge_mbuf_pool_len_5704	= MBUF_POOL_LENGTH_5704;
1271369Sdduvall static uint32_t bge_mbuf_pool_len_5705	= 0;	/* use h/w default	*/
1281369Sdduvall static uint32_t bge_mbuf_pool_len_5721	= 0;
1291369Sdduvall 
1301369Sdduvall /*
1311369Sdduvall  * Various high and low water marks, thresholds, etc ...
1321369Sdduvall  *
1331369Sdduvall  * Note: these are taken from revision 7 of the PRM, and some are different
1341369Sdduvall  * from both the values in earlier PRMs *and* those determined experimentally
1351369Sdduvall  * and used in earlier versions of this driver ...
1361369Sdduvall  */
1371369Sdduvall static uint32_t bge_mbuf_hi_water	= MBUF_HIWAT_DEFAULT;
1381369Sdduvall static uint32_t bge_mbuf_lo_water_rmac	= MAC_RX_MBUF_LOWAT_DEFAULT;
1391369Sdduvall static uint32_t bge_mbuf_lo_water_rdma	= RDMA_MBUF_LOWAT_DEFAULT;
1401369Sdduvall 
1411369Sdduvall static uint32_t bge_dmad_lo_water	= DMAD_POOL_LOWAT_DEFAULT;
1421369Sdduvall static uint32_t bge_dmad_hi_water	= DMAD_POOL_HIWAT_DEFAULT;
1431369Sdduvall static uint32_t bge_lowat_recv_frames	= LOWAT_MAX_RECV_FRAMES_DEFAULT;
1441369Sdduvall 
1451369Sdduvall static uint32_t bge_replenish_std	= STD_RCV_BD_REPLENISH_DEFAULT;
1461369Sdduvall static uint32_t bge_replenish_mini	= MINI_RCV_BD_REPLENISH_DEFAULT;
1471369Sdduvall static uint32_t bge_replenish_jumbo	= JUMBO_RCV_BD_REPLENISH_DEFAULT;
1481369Sdduvall 
1491369Sdduvall static uint32_t	bge_watchdog_count	= 1 << 16;
1501369Sdduvall static uint16_t bge_dma_miss_limit	= 20;
1511369Sdduvall 
1521369Sdduvall static uint32_t bge_stop_start_on_sync	= 0;
1531369Sdduvall 
1541369Sdduvall boolean_t bge_jumbo_enable		= B_TRUE;
1551369Sdduvall static uint32_t bge_default_jumbo_size	= BGE_JUMBO_BUFF_SIZE;
1561369Sdduvall 
1571369Sdduvall /*
1581369Sdduvall  * ========== Low-level chip & ring buffer manipulation ==========
1591369Sdduvall  */
1601369Sdduvall 
1611369Sdduvall #define	BGE_DBG		BGE_DBG_REGS	/* debug flag for this code	*/
1621369Sdduvall 
1631369Sdduvall 
1641369Sdduvall /*
1651369Sdduvall  * Config space read-modify-write routines
1661369Sdduvall  */
1671369Sdduvall 
1681369Sdduvall #if	BGE_CFG_IO8
1691369Sdduvall 
1701369Sdduvall /*
1711369Sdduvall  * 8- and 16-bit set/clr operations are not used; all the config registers
1721369Sdduvall  * that we need to do bit-twiddling on are 32 bits wide.  I'll leave the
1731369Sdduvall  * code here, though, in case we ever find that we do want it after all ...
1741369Sdduvall  */
1751369Sdduvall 
1761369Sdduvall static void bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1771369Sdduvall #pragma	inline(bge_cfg_set8)
1781369Sdduvall 
1791369Sdduvall static void
1801369Sdduvall bge_cfg_set8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
1811369Sdduvall {
1821369Sdduvall 	uint8_t regval;
1831369Sdduvall 
1841369Sdduvall 	BGE_TRACE(("bge_cfg_set8($%p, 0x%lx, 0x%x)",
1851369Sdduvall 		(void *)bgep, regno, bits));
1861369Sdduvall 
1871369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
1881369Sdduvall 
1891369Sdduvall 	BGE_DEBUG(("bge_cfg_set8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
1901369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
1911369Sdduvall 
1921369Sdduvall 	regval |= bits;
1931369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
1941369Sdduvall }
1951369Sdduvall 
1961369Sdduvall static void bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits);
1971369Sdduvall #pragma	inline(bge_cfg_clr8)
1981369Sdduvall 
1991369Sdduvall static void
2001369Sdduvall bge_cfg_clr8(bge_t *bgep, bge_regno_t regno, uint8_t bits)
2011369Sdduvall {
2021369Sdduvall 	uint8_t regval;
2031369Sdduvall 
2041369Sdduvall 	BGE_TRACE(("bge_cfg_clr8($%p, 0x%lx, 0x%x)",
2051369Sdduvall 		(void *)bgep, regno, bits));
2061369Sdduvall 
2071369Sdduvall 	regval = pci_config_get8(bgep->cfg_handle, regno);
2081369Sdduvall 
2091369Sdduvall 	BGE_DEBUG(("bge_cfg_clr8($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2101369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2111369Sdduvall 
2121369Sdduvall 	regval &= ~bits;
2131369Sdduvall 	pci_config_put8(bgep->cfg_handle, regno, regval);
2141369Sdduvall }
2151369Sdduvall 
2161369Sdduvall static void bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2171369Sdduvall #pragma	inline(bge_cfg_set16)
2181369Sdduvall 
2191369Sdduvall static void
2201369Sdduvall bge_cfg_set16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2211369Sdduvall {
2221369Sdduvall 	uint16_t regval;
2231369Sdduvall 
2241369Sdduvall 	BGE_TRACE(("bge_cfg_set16($%p, 0x%lx, 0x%x)",
2251369Sdduvall 		(void *)bgep, regno, bits));
2261369Sdduvall 
2271369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2281369Sdduvall 
2291369Sdduvall 	BGE_DEBUG(("bge_cfg_set16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2301369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2311369Sdduvall 
2321369Sdduvall 	regval |= bits;
2331369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2341369Sdduvall }
2351369Sdduvall 
2361369Sdduvall static void bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits);
2371369Sdduvall #pragma	inline(bge_cfg_clr16)
2381369Sdduvall 
2391369Sdduvall static void
2401369Sdduvall bge_cfg_clr16(bge_t *bgep, bge_regno_t regno, uint16_t bits)
2411369Sdduvall {
2421369Sdduvall 	uint16_t regval;
2431369Sdduvall 
2441369Sdduvall 	BGE_TRACE(("bge_cfg_clr16($%p, 0x%lx, 0x%x)",
2451369Sdduvall 		(void *)bgep, regno, bits));
2461369Sdduvall 
2471369Sdduvall 	regval = pci_config_get16(bgep->cfg_handle, regno);
2481369Sdduvall 
2491369Sdduvall 	BGE_DEBUG(("bge_cfg_clr16($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2501369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2511369Sdduvall 
2521369Sdduvall 	regval &= ~bits;
2531369Sdduvall 	pci_config_put16(bgep->cfg_handle, regno, regval);
2541369Sdduvall }
2551369Sdduvall 
2561369Sdduvall #endif	/* BGE_CFG_IO8 */
2571369Sdduvall 
2581369Sdduvall static void bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2591369Sdduvall #pragma	inline(bge_cfg_set32)
2601369Sdduvall 
2611369Sdduvall static void
2621369Sdduvall bge_cfg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2631369Sdduvall {
2641369Sdduvall 	uint32_t regval;
2651369Sdduvall 
2661369Sdduvall 	BGE_TRACE(("bge_cfg_set32($%p, 0x%lx, 0x%x)",
2671369Sdduvall 		(void *)bgep, regno, bits));
2681369Sdduvall 
2691369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2701369Sdduvall 
2711369Sdduvall 	BGE_DEBUG(("bge_cfg_set32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2721369Sdduvall 		(void *)bgep, regno, bits, regval, regval | bits));
2731369Sdduvall 
2741369Sdduvall 	regval |= bits;
2751369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2761369Sdduvall }
2771369Sdduvall 
2781369Sdduvall static void bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
2791369Sdduvall #pragma	inline(bge_cfg_clr32)
2801369Sdduvall 
2811369Sdduvall static void
2821369Sdduvall bge_cfg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
2831369Sdduvall {
2841369Sdduvall 	uint32_t regval;
2851369Sdduvall 
2861369Sdduvall 	BGE_TRACE(("bge_cfg_clr32($%p, 0x%lx, 0x%x)",
2871369Sdduvall 		(void *)bgep, regno, bits));
2881369Sdduvall 
2891369Sdduvall 	regval = pci_config_get32(bgep->cfg_handle, regno);
2901369Sdduvall 
2911369Sdduvall 	BGE_DEBUG(("bge_cfg_clr32($%p, 0x%lx, 0x%x): 0x%x => 0x%x",
2921369Sdduvall 		(void *)bgep, regno, bits, regval, regval & ~bits));
2931369Sdduvall 
2941369Sdduvall 	regval &= ~bits;
2951369Sdduvall 	pci_config_put32(bgep->cfg_handle, regno, regval);
2961369Sdduvall }
2971369Sdduvall 
2981369Sdduvall #if	BGE_IND_IO32
2991369Sdduvall 
3001369Sdduvall /*
3011369Sdduvall  * Indirect access to registers & RISC scratchpads, using config space
3021369Sdduvall  * accesses only.
3031369Sdduvall  *
3041369Sdduvall  * This isn't currently used, but someday we might want to use it for
3051369Sdduvall  * restoring the Subsystem Device/Vendor registers (which aren't directly
3061369Sdduvall  * writable in Config Space), or for downloading firmware into the RISCs
3071369Sdduvall  *
3081369Sdduvall  * In any case there are endian issues to be resolved before this code is
3091369Sdduvall  * enabled; the bizarre way that bytes get twisted by this chip AND by
3101369Sdduvall  * the PCI bridge in SPARC systems mean that we shouldn't enable it until
3111369Sdduvall  * it's been thoroughly tested for all access sizes on all supported
3121369Sdduvall  * architectures (SPARC *and* x86!).
3131369Sdduvall  */
3141369Sdduvall static uint32_t bge_ind_get32(bge_t *bgep, bge_regno_t regno);
3151369Sdduvall #pragma	inline(bge_ind_get32)
3161369Sdduvall 
3171369Sdduvall static uint32_t
3181369Sdduvall bge_ind_get32(bge_t *bgep, bge_regno_t regno)
3191369Sdduvall {
3201369Sdduvall 	uint32_t val;
3211369Sdduvall 
3221369Sdduvall 	BGE_TRACE(("bge_ind_get32($%p, 0x%lx)", (void *)bgep, regno));
3231369Sdduvall 
3241369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3251369Sdduvall 
3261369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3271369Sdduvall 	val = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_RIADR);
3281369Sdduvall 
3291369Sdduvall 	BGE_DEBUG(("bge_ind_get32($%p, 0x%lx) => 0x%x",
3301369Sdduvall 		(void *)bgep, regno, val));
3311369Sdduvall 
3321369Sdduvall 	return (val);
3331369Sdduvall }
3341369Sdduvall 
3351369Sdduvall static void bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val);
3361369Sdduvall #pragma	inline(bge_ind_put32)
3371369Sdduvall 
3381369Sdduvall static void
3391369Sdduvall bge_ind_put32(bge_t *bgep, bge_regno_t regno, uint32_t val)
3401369Sdduvall {
3411369Sdduvall 	BGE_TRACE(("bge_ind_put32($%p, 0x%lx, 0x%x)",
3421369Sdduvall 		(void *)bgep, regno, val));
3431369Sdduvall 
3441369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
3451369Sdduvall 
3461369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIAAR, regno);
3471369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_RIADR, val);
3481369Sdduvall }
3491369Sdduvall 
3501369Sdduvall #endif	/* BGE_IND_IO32 */
3511369Sdduvall 
3521369Sdduvall #if	BGE_DEBUGGING
3531369Sdduvall 
3541369Sdduvall static void bge_pci_check(bge_t *bgep);
3551369Sdduvall #pragma	no_inline(bge_pci_check)
3561369Sdduvall 
3571369Sdduvall static void
3581369Sdduvall bge_pci_check(bge_t *bgep)
3591369Sdduvall {
3601369Sdduvall 	uint16_t pcistatus;
3611369Sdduvall 
3621369Sdduvall 	pcistatus = pci_config_get16(bgep->cfg_handle, PCI_CONF_STAT);
3631369Sdduvall 	if ((pcistatus & (PCI_STAT_R_MAST_AB | PCI_STAT_R_TARG_AB)) != 0)
3641369Sdduvall 		BGE_DEBUG(("bge_pci_check($%p): PCI status 0x%x",
3651369Sdduvall 			(void *)bgep, pcistatus));
3661369Sdduvall }
3671369Sdduvall 
3681369Sdduvall #endif	/* BGE_DEBUGGING */
3691369Sdduvall 
3701369Sdduvall /*
3711369Sdduvall  * Perform first-stage chip (re-)initialisation, using only config-space
3721369Sdduvall  * accesses:
3731369Sdduvall  *
3741369Sdduvall  * + Read the vendor/device/revision/subsystem/cache-line-size registers,
3751369Sdduvall  *   returning the data in the structure pointed to by <idp>.
3761369Sdduvall  * + Configure the target-mode endianness (swap) options.
3771369Sdduvall  * + Disable interrupts and enable Memory Space accesses.
3781369Sdduvall  * + Enable or disable Bus Mastering according to the <enable_dma> flag.
3791369Sdduvall  *
3801369Sdduvall  * This sequence is adapted from Broadcom document 570X-PG102-R,
3811369Sdduvall  * page 102, steps 1-3, 6-8 and 11-13.  The omitted parts of the sequence
3821369Sdduvall  * are 4 and 5 (Reset Core and wait) which are handled elsewhere.
3831369Sdduvall  *
3841369Sdduvall  * This function MUST be called before any non-config-space accesses
3851369Sdduvall  * are made; on this first call <enable_dma> is B_FALSE, and it
3861369Sdduvall  * effectively performs steps 3-1(!) of the initialisation sequence
3871369Sdduvall  * (the rest are not required but should be harmless).
3881369Sdduvall  *
3891369Sdduvall  * It MUST also be called also after a chip reset, as this disables
3901369Sdduvall  * Memory Space cycles!  In this case, <enable_dma> is B_TRUE, and
3911369Sdduvall  * it is effectively performing steps 6-8.
3921369Sdduvall  */
3931369Sdduvall void bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma);
3941369Sdduvall #pragma	no_inline(bge_chip_cfg_init)
3951369Sdduvall 
3961369Sdduvall void
3971369Sdduvall bge_chip_cfg_init(bge_t *bgep, chip_id_t *cidp, boolean_t enable_dma)
3981369Sdduvall {
3991369Sdduvall 	ddi_acc_handle_t handle;
4001369Sdduvall 	uint16_t command;
4011369Sdduvall 	uint32_t mhcr;
4021369Sdduvall 	uint16_t value16;
4031369Sdduvall 	int i;
4041369Sdduvall 
4051369Sdduvall 	BGE_TRACE(("bge_chip_cfg_init($%p, $%p, %d)",
4061369Sdduvall 		(void *)bgep, (void *)cidp, enable_dma));
4071369Sdduvall 
4081369Sdduvall 	/*
4091369Sdduvall 	 * Step 3: save PCI cache line size and subsystem vendor ID
4101369Sdduvall 	 *
4111369Sdduvall 	 * Read all the config-space registers that characterise the
4121369Sdduvall 	 * chip, specifically vendor/device/revision/subsystem vendor
4131369Sdduvall 	 * and subsystem device id.  We expect (but don't check) that
4141369Sdduvall 	 * (vendor == VENDOR_ID_BROADCOM) && (device == DEVICE_ID_5704)
4151369Sdduvall 	 *
4161369Sdduvall 	 * Also save all bus-transation related registers (cache-line
4171369Sdduvall 	 * size, bus-grant/latency parameters, etc).  Some of these are
4181369Sdduvall 	 * cleared by reset, so we'll have to restore them later.  This
4191369Sdduvall 	 * comes from the Broadcom document 570X-PG102-R ...
4201369Sdduvall 	 *
4211369Sdduvall 	 * Note: Broadcom document 570X-PG102-R seems to be in error
4221369Sdduvall 	 * here w.r.t. the offsets of the Subsystem Vendor ID and
4231369Sdduvall 	 * Subsystem (Device) ID registers, which are the opposite way
4241369Sdduvall 	 * round according to the PCI standard.  For good measure, we
4251369Sdduvall 	 * save/restore both anyway.
4261369Sdduvall 	 */
4271369Sdduvall 	handle = bgep->cfg_handle;
4281369Sdduvall 
4291369Sdduvall 	mhcr = pci_config_get32(handle, PCI_CONF_BGE_MHCR);
4301369Sdduvall 	cidp->asic_rev = mhcr & MHCR_CHIP_REV_MASK;
4311369Sdduvall 	cidp->businfo = pci_config_get32(handle, PCI_CONF_BGE_PCISTATE);
4321369Sdduvall 	cidp->command = pci_config_get16(handle, PCI_CONF_COMM);
4331369Sdduvall 
4341369Sdduvall 	cidp->vendor = pci_config_get16(handle, PCI_CONF_VENID);
4351369Sdduvall 	cidp->device = pci_config_get16(handle, PCI_CONF_DEVID);
4361369Sdduvall 	cidp->subven = pci_config_get16(handle, PCI_CONF_SUBVENID);
4371369Sdduvall 	cidp->subdev = pci_config_get16(handle, PCI_CONF_SUBSYSID);
4381369Sdduvall 	cidp->revision = pci_config_get8(handle, PCI_CONF_REVID);
4391369Sdduvall 	cidp->clsize = pci_config_get8(handle, PCI_CONF_CACHE_LINESZ);
4401369Sdduvall 	cidp->latency = pci_config_get8(handle, PCI_CONF_LATENCY_TIMER);
4411369Sdduvall 
4421369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: %s bus is %s and %s; #INTA is %s",
4431369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_PCI ? "PCI" : "PCI-X",
4441369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_FAST ? "fast" : "slow",
4451369Sdduvall 		cidp->businfo & PCISTATE_BUS_IS_32_BIT ? "narrow" : "wide",
4461369Sdduvall 		cidp->businfo & PCISTATE_INTA_STATE ? "high" : "low"));
4471369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: vendor 0x%x device 0x%x revision 0x%x",
4481369Sdduvall 		cidp->vendor, cidp->device, cidp->revision));
4491369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: subven 0x%x subdev 0x%x asic_rev 0x%x",
4501369Sdduvall 		cidp->subven, cidp->subdev, cidp->asic_rev));
4511369Sdduvall 	BGE_DEBUG(("bge_chip_cfg_init: clsize %d latency %d command 0x%x",
4521369Sdduvall 		cidp->clsize, cidp->latency, cidp->command));
4531369Sdduvall 
4541369Sdduvall 	/*
4551369Sdduvall 	 * Step 2 (also step 6): disable and clear interrupts.
4561369Sdduvall 	 * Steps 11-13: configure PIO endianness options, and enable
4571369Sdduvall 	 * indirect register access.  We'll also select any other
4581369Sdduvall 	 * options controlled by the MHCR (eg tagged status, mask
4591369Sdduvall 	 * interrupt mode) at this stage ...
4601369Sdduvall 	 *
4611369Sdduvall 	 * Note: internally, the chip is 64-bit and BIG-endian, but
4621369Sdduvall 	 * since it talks to the host over a (LITTLE-endian) PCI bus,
4631369Sdduvall 	 * it normally swaps bytes around at the PCI interface.
4641369Sdduvall 	 * However, the PCI host bridge on SPARC systems normally
4651369Sdduvall 	 * swaps the byte lanes around too, since SPARCs are also
4661369Sdduvall 	 * BIG-endian.  So it turns out that on SPARC, the right
4671369Sdduvall 	 * option is to tell the chip to swap (and the host bridge
4681369Sdduvall 	 * will swap back again), whereas on x86 we ask the chip
4691369Sdduvall 	 * NOT to swap, so the natural little-endianness of the
4701369Sdduvall 	 * PCI bus is assumed.  Then the only thing that doesn't
4711369Sdduvall 	 * automatically work right is access to an 8-byte register
4721369Sdduvall 	 * by a little-endian host; but we don't want to set the
4731369Sdduvall 	 * MHCR_ENABLE_REGISTER_WORD_SWAP bit because then 4-byte
4741369Sdduvall 	 * accesses don't go where expected ;-(  So we live with
4751369Sdduvall 	 * that, and perform word-swaps in software in the few cases
4761369Sdduvall 	 * where a chip register is defined as an 8-byte value --
4771369Sdduvall 	 * see the code below for details ...
4781369Sdduvall 	 *
4791369Sdduvall 	 * Note: the meaning of the 'MASK_INTERRUPT_MODE' bit isn't
4801369Sdduvall 	 * very clear in the register description in the PRM, but
4811369Sdduvall 	 * Broadcom document 570X-PG104-R page 248 explains a little
4821369Sdduvall 	 * more (under "Broadcom Mask Mode").  The bit changes the way
4831369Sdduvall 	 * the MASK_PCI_INT_OUTPUT bit works: with MASK_INTERRUPT_MODE
4841369Sdduvall 	 * clear, the chip interprets MASK_PCI_INT_OUTPUT in the same
4851369Sdduvall 	 * way as the 5700 did, which isn't very convenient.  Setting
4861369Sdduvall 	 * the MASK_INTERRUPT_MODE bit makes the MASK_PCI_INT_OUTPUT
4871369Sdduvall 	 * bit do just what its name says -- MASK the PCI #INTA output
4881369Sdduvall 	 * (i.e. deassert the signal at the pin) leaving all internal
4891369Sdduvall 	 * state unchanged.  This is much more convenient for our
4901369Sdduvall 	 * interrupt handler, so we set MASK_INTERRUPT_MODE here.
4911369Sdduvall 	 *
4921369Sdduvall 	 * Note: the inconvenient semantics of the interrupt mailbox
4931369Sdduvall 	 * (nonzero disables and acknowledges/clears the interrupt,
4941369Sdduvall 	 * zero enables AND CLEARS it) would make race conditions
4951369Sdduvall 	 * likely in the interrupt handler:
4961369Sdduvall 	 *
4971369Sdduvall 	 * (1)	acknowledge & disable interrupts
4981369Sdduvall 	 * (2)	while (more to do)
4991369Sdduvall 	 * 		process packets
5001369Sdduvall 	 * (3)	enable interrupts -- also clears pending
5011369Sdduvall 	 *
5021369Sdduvall 	 * If the chip received more packets and internally generated
5031369Sdduvall 	 * an interrupt between the check at (2) and the mbox write
5041369Sdduvall 	 * at (3), this interrupt would be lost :-(
5051369Sdduvall 	 *
5061369Sdduvall 	 * The best way to avoid this is to use TAGGED STATUS mode,
5071369Sdduvall 	 * where the chip includes a unique tag in each status block
5081369Sdduvall 	 * update, and the host, when re-enabling interrupts, passes
5091369Sdduvall 	 * the last tag it saw back to the chip; then the chip can
5101369Sdduvall 	 * see whether the host is truly up to date, and regenerate
5111369Sdduvall 	 * its interrupt if not.
5121369Sdduvall 	 */
5131369Sdduvall 	mhcr =	MHCR_ENABLE_INDIRECT_ACCESS |
5141369Sdduvall 		MHCR_ENABLE_TAGGED_STATUS_MODE |
5151369Sdduvall 		MHCR_MASK_INTERRUPT_MODE |
5161369Sdduvall 		MHCR_CLEAR_INTERRUPT_INTA;
5171369Sdduvall 
5181369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
5191369Sdduvall 		mhcr |= MHCR_MASK_PCI_INT_OUTPUT;
5201369Sdduvall 
5211369Sdduvall #ifdef	_BIG_ENDIAN
5221369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
5231369Sdduvall #endif	/* _BIG_ENDIAN */
5241369Sdduvall 
5251369Sdduvall 	pci_config_put32(handle, PCI_CONF_BGE_MHCR, mhcr);
5261369Sdduvall 
5271408Srandyf #ifdef BGE_IPMI_ASF
5281408Srandyf 	bgep->asf_wordswapped = B_FALSE;
5291408Srandyf #endif
5301369Sdduvall 	/*
5311369Sdduvall 	 * Step 1 (also step 7): Enable PCI Memory Space accesses
5321369Sdduvall 	 *			 Disable Memory Write/Invalidate
5331369Sdduvall 	 *			 Enable or disable Bus Mastering
5341369Sdduvall 	 *
5351369Sdduvall 	 * Note that all other bits are taken from the original value saved
5361369Sdduvall 	 * the first time through here, rather than from the current register
5371369Sdduvall 	 * value, 'cos that will have been cleared by a soft RESET since.
5381369Sdduvall 	 * In this way we preserve the OBP/nexus-parent's preferred settings
5391369Sdduvall 	 * of the parity-error and system-error enable bits across multiple
5401369Sdduvall 	 * chip RESETs.
5411369Sdduvall 	 *
5421369Sdduvall 	 * Step 8: Disable PCI-X Relaxed Ordering -- doesn't apply
5431369Sdduvall 	 */
5441369Sdduvall 	command = bgep->chipid.command | PCI_COMM_MAE;
5451369Sdduvall 	command &= ~(PCI_COMM_ME|PCI_COMM_MEMWR_INVAL);
5461369Sdduvall 	if (enable_dma)
5471369Sdduvall 		command |= PCI_COMM_ME;
5481369Sdduvall 	/*
5491369Sdduvall 	 * on BCM5714 revision A0, false parity error gets generated
5501369Sdduvall 	 * due to a logic bug. Provide a workaround by disabling parrity
5511369Sdduvall 	 * error.
5521369Sdduvall 	 */
5531369Sdduvall 	if (((cidp->device == DEVICE_ID_5714C) ||
5541369Sdduvall 	    (cidp->device == DEVICE_ID_5714S)) &&
5551369Sdduvall 	    (cidp->revision == REVISION_ID_5714_A0)) {
5561369Sdduvall 		command &= ~PCI_COMM_PARITY_DETECT;
5571369Sdduvall 	}
5581369Sdduvall 	pci_config_put16(handle, PCI_CONF_COMM, command);
5591369Sdduvall 
5601369Sdduvall 	/*
5611369Sdduvall 	 * On some PCI-E device, there were instances when
5621369Sdduvall 	 * the device was still link training.
5631369Sdduvall 	 */
5641369Sdduvall 	if (bgep->chipid.pci_type == BGE_PCI_E) {
5651369Sdduvall 		i = 0;
5661369Sdduvall 		value16 = pci_config_get16(handle, PCI_CONF_COMM);
5671369Sdduvall 		while ((value16 != command) && (i < 100)) {
5681369Sdduvall 			drv_usecwait(200);
5691369Sdduvall 			value16 = pci_config_get16(handle, PCI_CONF_COMM);
5701369Sdduvall 			++i;
5711369Sdduvall 		}
5721369Sdduvall 	}
5731369Sdduvall 
5741369Sdduvall 	/*
5751369Sdduvall 	 * Clear any remaining error status bits
5761369Sdduvall 	 */
5771369Sdduvall 	pci_config_put16(handle, PCI_CONF_STAT, ~0);
5781369Sdduvall 
5791369Sdduvall 	/*
5801369Sdduvall 	 * Make sure these indirect-access registers are sane
5811369Sdduvall 	 * rather than random after power-up or reset
582*1611Szh199473 	 *
583*1611Szh199473 	 * For BCM5714C A3 silicon to avoid resource deadlocking
5841369Sdduvall 	 */
585*1611Szh199473 	if ((cidp->device == DEVICE_ID_5714C) &&
586*1611Szh199473 		(cidp->revision == REVISION_ID_5714_A3)) {
587*1611Szh199473 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0x4900);
588*1611Szh199473 		pci_config_put32(handle, PCI_CONF_BGE_RIADR, 1);
589*1611Szh199473 	} else {
590*1611Szh199473 		pci_config_put32(handle, PCI_CONF_BGE_RIAAR, 0);
591*1611Szh199473 		pci_config_put32(handle, PCI_CONF_BGE_MWBAR, 0);
592*1611Szh199473 	}
5931369Sdduvall }
5941369Sdduvall 
5951369Sdduvall #ifdef __amd64
5961369Sdduvall /*
5971369Sdduvall  * Distinguish CPU types
5981369Sdduvall  *
5991369Sdduvall  * These use to  distinguish AMD64 or Intel EM64T of CPU running mode.
6001369Sdduvall  * If CPU runs on Intel EM64T mode,the 64bit operation cannot works fine
6011369Sdduvall  * for PCI-Express based network interface card. This is the work-around
6021369Sdduvall  * for those nics.
6031369Sdduvall  */
6041369Sdduvall static boolean_t bge_get_em64t_type(void);
6051369Sdduvall #pragma	inline(bge_get_em64t_type)
6061369Sdduvall 
6071369Sdduvall static boolean_t
6081369Sdduvall bge_get_em64t_type(void)
6091369Sdduvall {
6101369Sdduvall 
6111369Sdduvall 	return (x86_vendor == X86_VENDOR_Intel);
6121369Sdduvall }
6131369Sdduvall #endif
6141369Sdduvall 
6151369Sdduvall /*
6161369Sdduvall  * Operating register get/set access routines
6171369Sdduvall  */
6181369Sdduvall 
6191369Sdduvall uint32_t bge_reg_get32(bge_t *bgep, bge_regno_t regno);
6201369Sdduvall #pragma	inline(bge_reg_get32)
6211369Sdduvall 
6221369Sdduvall uint32_t
6231369Sdduvall bge_reg_get32(bge_t *bgep, bge_regno_t regno)
6241369Sdduvall {
6251369Sdduvall 	BGE_TRACE(("bge_reg_get32($%p, 0x%lx)",
6261369Sdduvall 		(void *)bgep, regno));
6271369Sdduvall 
6281369Sdduvall 	return (ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno)));
6291369Sdduvall }
6301369Sdduvall 
6311369Sdduvall void bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data);
6321369Sdduvall #pragma	inline(bge_reg_put32)
6331369Sdduvall 
6341369Sdduvall void
6351369Sdduvall bge_reg_put32(bge_t *bgep, bge_regno_t regno, uint32_t data)
6361369Sdduvall {
6371369Sdduvall 	BGE_TRACE(("bge_reg_put32($%p, 0x%lx, 0x%x)",
6381369Sdduvall 		(void *)bgep, regno, data));
6391369Sdduvall 
6401369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), data);
6411369Sdduvall 	BGE_PCICHK(bgep);
6421369Sdduvall }
6431369Sdduvall 
6441369Sdduvall void bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6451369Sdduvall #pragma	inline(bge_reg_set32)
6461369Sdduvall 
6471369Sdduvall void
6481369Sdduvall bge_reg_set32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6491369Sdduvall {
6501369Sdduvall 	uint32_t regval;
6511369Sdduvall 
6521369Sdduvall 	BGE_TRACE(("bge_reg_set32($%p, 0x%lx, 0x%x)",
6531369Sdduvall 		(void *)bgep, regno, bits));
6541369Sdduvall 
6551369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6561369Sdduvall 	regval |= bits;
6571369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6581369Sdduvall }
6591369Sdduvall 
6601369Sdduvall void bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits);
6611369Sdduvall #pragma	inline(bge_reg_clr32)
6621369Sdduvall 
6631369Sdduvall void
6641369Sdduvall bge_reg_clr32(bge_t *bgep, bge_regno_t regno, uint32_t bits)
6651369Sdduvall {
6661369Sdduvall 	uint32_t regval;
6671369Sdduvall 
6681369Sdduvall 	BGE_TRACE(("bge_reg_clr32($%p, 0x%lx, 0x%x)",
6691369Sdduvall 		(void *)bgep, regno, bits));
6701369Sdduvall 
6711369Sdduvall 	regval = bge_reg_get32(bgep, regno);
6721369Sdduvall 	regval &= ~bits;
6731369Sdduvall 	bge_reg_put32(bgep, regno, regval);
6741369Sdduvall }
6751369Sdduvall 
6761369Sdduvall static uint64_t bge_reg_get64(bge_t *bgep, bge_regno_t regno);
6771369Sdduvall #pragma	inline(bge_reg_get64)
6781369Sdduvall 
6791369Sdduvall static uint64_t
6801369Sdduvall bge_reg_get64(bge_t *bgep, bge_regno_t regno)
6811369Sdduvall {
6821369Sdduvall 	uint64_t regval;
6831369Sdduvall 
6841369Sdduvall #ifdef	__amd64
6851369Sdduvall 	if (bge_get_em64t_type()) {
6861369Sdduvall 		regval = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno + 4));
6871369Sdduvall 		regval <<= 32;
6881369Sdduvall 		regval |= ddi_get32(bgep->io_handle, PIO_ADDR(bgep, regno));
6891369Sdduvall 	} else {
6901369Sdduvall 		regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6911369Sdduvall 	}
6921369Sdduvall #else
6931369Sdduvall 	regval = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, regno));
6941369Sdduvall #endif
6951369Sdduvall 
6961369Sdduvall #ifdef	_LITTLE_ENDIAN
6971369Sdduvall 	regval = (regval >> 32) | (regval << 32);
6981369Sdduvall #endif	/* _LITTLE_ENDIAN */
6991369Sdduvall 
7001369Sdduvall 	BGE_TRACE(("bge_reg_get64($%p, 0x%lx) = 0x%016llx",
7011369Sdduvall 		(void *)bgep, regno, regval));
7021369Sdduvall 
7031369Sdduvall 	return (regval);
7041369Sdduvall }
7051369Sdduvall 
7061369Sdduvall static void bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data);
7071369Sdduvall #pragma	inline(bge_reg_put64)
7081369Sdduvall 
7091369Sdduvall static void
7101369Sdduvall bge_reg_put64(bge_t *bgep, bge_regno_t regno, uint64_t data)
7111369Sdduvall {
7121369Sdduvall 	BGE_TRACE(("bge_reg_put64($%p, 0x%lx, 0x%016llx)",
7131369Sdduvall 		(void *)bgep, regno, data));
7141369Sdduvall 
7151369Sdduvall #ifdef	_LITTLE_ENDIAN
7161369Sdduvall 	data = ((data >> 32) | (data << 32));
7171369Sdduvall #endif	/* _LITTLE_ENDIAN */
7181369Sdduvall 
7191369Sdduvall #ifdef	__amd64
7201369Sdduvall 	if (bge_get_em64t_type()) {
7211369Sdduvall 		ddi_put32(bgep->io_handle,
7221369Sdduvall 			PIO_ADDR(bgep, regno), (uint32_t)data);
7231369Sdduvall 		BGE_PCICHK(bgep);
7241369Sdduvall 		ddi_put32(bgep->io_handle,
7251369Sdduvall 			PIO_ADDR(bgep, regno + 4), (uint32_t)(data >> 32));
7261369Sdduvall 
7271369Sdduvall 	} else {
7281369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7291369Sdduvall 	}
7301369Sdduvall #else
7311369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, regno), data);
7321369Sdduvall #endif
7331369Sdduvall 
7341369Sdduvall 	BGE_PCICHK(bgep);
7351369Sdduvall }
7361369Sdduvall 
7371369Sdduvall /*
7381369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
7391369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
7401369Sdduvall  */
7411369Sdduvall static void bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
7421369Sdduvall #pragma	inline(bge_reg_putrcb)
7431369Sdduvall 
7441369Sdduvall static void
7451369Sdduvall bge_reg_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
7461369Sdduvall {
7471369Sdduvall 	uint64_t *p;
7481369Sdduvall 
7491369Sdduvall 	BGE_TRACE(("bge_reg_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
7501369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
7511369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
7521369Sdduvall 
7531369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
7541369Sdduvall 
7551369Sdduvall 	p = (void *)rcbp;
7561369Sdduvall 	bge_reg_put64(bgep, addr, *p++);
7571369Sdduvall 	bge_reg_put64(bgep, addr+8, *p);
7581369Sdduvall }
7591369Sdduvall 
7601369Sdduvall void bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data);
7611369Sdduvall #pragma	inline(bge_mbx_put)
7621369Sdduvall 
7631369Sdduvall void
7641369Sdduvall bge_mbx_put(bge_t *bgep, bge_regno_t regno, uint64_t data)
7651369Sdduvall {
7661369Sdduvall 	BGE_TRACE(("bge_mbx_put($%p, 0x%lx, 0x%016llx)",
7671369Sdduvall 		(void *)bgep, regno, data));
7681369Sdduvall 
7691369Sdduvall 	/*
7701369Sdduvall 	 * Mailbox registers are nominally 64 bits on the 5701, but
7711369Sdduvall 	 * the MSW isn't used.  On the 5703, they're only 32 bits
7721369Sdduvall 	 * anyway.  So here we just write the lower(!) 32 bits -
7731369Sdduvall 	 * remembering that the chip is big-endian, even though the
7741369Sdduvall 	 * PCI bus is little-endian ...
7751369Sdduvall 	 */
7761369Sdduvall #ifdef	_BIG_ENDIAN
7771369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno+4), (uint32_t)data);
7781369Sdduvall #else
7791369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), (uint32_t)data);
7801369Sdduvall #endif	/* _BIG_ENDIAN */
7811369Sdduvall 	BGE_PCICHK(bgep);
7821369Sdduvall }
7831369Sdduvall 
7841369Sdduvall #if	BGE_DEBUGGING
7851369Sdduvall 
7861369Sdduvall void bge_led_mark(bge_t *bgep);
7871369Sdduvall #pragma	no_inline(bge_led_mark)
7881369Sdduvall 
7891369Sdduvall void
7901369Sdduvall bge_led_mark(bge_t *bgep)
7911369Sdduvall {
7921369Sdduvall 	uint32_t led_ctrl = LED_CONTROL_OVERRIDE_LINK |
7931369Sdduvall 			    LED_CONTROL_1000MBPS_LED |
7941369Sdduvall 			    LED_CONTROL_100MBPS_LED |
7951369Sdduvall 			    LED_CONTROL_10MBPS_LED;
7961369Sdduvall 
7971369Sdduvall 	/*
7981369Sdduvall 	 * Blink all three LINK LEDs on simultaneously, then all off,
7991369Sdduvall 	 * then restore to automatic hardware control.  This is used
8001369Sdduvall 	 * in laboratory testing to trigger a logic analyser or scope.
8011369Sdduvall 	 */
8021369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8031369Sdduvall 	led_ctrl ^= LED_CONTROL_OVERRIDE_LINK;
8041369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8051369Sdduvall 	led_ctrl = LED_CONTROL_OVERRIDE_LINK;
8061369Sdduvall 	bge_reg_clr32(bgep, ETHERNET_MAC_LED_CONTROL_REG, led_ctrl);
8071369Sdduvall }
8081369Sdduvall 
8091369Sdduvall #endif	/* BGE_DEBUGGING */
8101369Sdduvall 
8111369Sdduvall /*
8121369Sdduvall  * NIC on-chip memory access routines
8131369Sdduvall  *
8141369Sdduvall  * Only 32K of NIC memory is visible at a time, controlled by the
8151369Sdduvall  * Memory Window Base Address Register (in PCI config space).  Once
8161369Sdduvall  * this is set, the 32K region of NIC-local memory that it refers
8171369Sdduvall  * to can be directly addressed in the upper 32K of the 64K of PCI
8181369Sdduvall  * memory space used for the device.
8191369Sdduvall  */
8201369Sdduvall 
8211369Sdduvall static void bge_nic_setwin(bge_t *bgep, bge_regno_t base);
8221369Sdduvall #pragma	inline(bge_nic_setwin)
8231369Sdduvall 
8241369Sdduvall static void
8251369Sdduvall bge_nic_setwin(bge_t *bgep, bge_regno_t base)
8261369Sdduvall {
8271369Sdduvall 	BGE_TRACE(("bge_nic_setwin($%p, 0x%lx)",
8281369Sdduvall 		(void *)bgep, base));
8291369Sdduvall 
8301369Sdduvall 	ASSERT((base & MWBAR_GRANULE_MASK) == 0);
8311369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, base);
8321369Sdduvall }
8331369Sdduvall 
8341369Sdduvall 
8351369Sdduvall static uint32_t bge_nic_get32(bge_t *bgep, bge_regno_t addr);
8361369Sdduvall #pragma	inline(bge_nic_get32)
8371369Sdduvall 
8381369Sdduvall static uint32_t
8391369Sdduvall bge_nic_get32(bge_t *bgep, bge_regno_t addr)
8401369Sdduvall {
8411369Sdduvall 	uint32_t data;
8421369Sdduvall 
8431408Srandyf #ifdef BGE_IPMI_ASF
8441408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8451408Srandyf 		/* workaround for word swap error */
8461408Srandyf 		if (addr & 4)
8471408Srandyf 			addr = addr - 4;
8481408Srandyf 		else
8491408Srandyf 			addr = addr + 4;
8501408Srandyf 	}
8511408Srandyf #endif
8521408Srandyf 
8531369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
8541369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
8551369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
8561369Sdduvall 
8571369Sdduvall 	data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
8581369Sdduvall 
8591369Sdduvall 	BGE_TRACE(("bge_nic_get32($%p, 0x%lx) = 0x%08x",
8601369Sdduvall 		(void *)bgep, addr, data));
8611369Sdduvall 
8621369Sdduvall 	return (data);
8631369Sdduvall }
8641369Sdduvall 
8651408Srandyf void bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data);
8661408Srandyf #pragma inline(bge_nic_put32)
8671408Srandyf 
8681408Srandyf void
8691369Sdduvall bge_nic_put32(bge_t *bgep, bge_regno_t addr, uint32_t data)
8701369Sdduvall {
8711369Sdduvall 	BGE_TRACE(("bge_nic_put32($%p, 0x%lx, 0x%08x)",
8721369Sdduvall 		(void *)bgep, addr, data));
8731369Sdduvall 
8741408Srandyf #ifdef BGE_IPMI_ASF
8751408Srandyf 	if (bgep->asf_enabled && !bgep->asf_wordswapped) {
8761408Srandyf 		/* workaround for word swap error */
8771408Srandyf 		if (addr & 4)
8781408Srandyf 			addr = addr - 4;
8791408Srandyf 		else
8801408Srandyf 			addr = addr + 4;
8811408Srandyf 	}
8821408Srandyf #endif
8831408Srandyf 
8841369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
8851369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
8861369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
8871369Sdduvall 	ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr), data);
8881369Sdduvall 	BGE_PCICHK(bgep);
8891369Sdduvall }
8901369Sdduvall 
8911369Sdduvall 
8921369Sdduvall static uint64_t bge_nic_get64(bge_t *bgep, bge_regno_t addr);
8931369Sdduvall #pragma	inline(bge_nic_get64)
8941369Sdduvall 
8951369Sdduvall static uint64_t
8961369Sdduvall bge_nic_get64(bge_t *bgep, bge_regno_t addr)
8971369Sdduvall {
8981369Sdduvall 	uint64_t data;
8991369Sdduvall 
9001369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9011369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9021369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9031369Sdduvall 
9041369Sdduvall #ifdef	__amd64
9051369Sdduvall 		if (bge_get_em64t_type()) {
9061369Sdduvall 			data = ddi_get32(bgep->io_handle, PIO_ADDR(bgep, addr));
9071369Sdduvall 			data <<= 32;
9081369Sdduvall 			data |= ddi_get32(bgep->io_handle,
9091369Sdduvall 				PIO_ADDR(bgep, addr + 4));
9101369Sdduvall 		} else {
9111369Sdduvall 			data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9121369Sdduvall 		}
9131369Sdduvall #else
9141369Sdduvall 		data = ddi_get64(bgep->io_handle, PIO_ADDR(bgep, addr));
9151369Sdduvall #endif
9161369Sdduvall 
9171369Sdduvall 	BGE_TRACE(("bge_nic_get64($%p, 0x%lx) = 0x%016llx",
9181369Sdduvall 		(void *)bgep, addr, data));
9191369Sdduvall 
9201369Sdduvall 	return (data);
9211369Sdduvall }
9221369Sdduvall 
9231369Sdduvall static void bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data);
9241369Sdduvall #pragma	inline(bge_nic_put64)
9251369Sdduvall 
9261369Sdduvall static void
9271369Sdduvall bge_nic_put64(bge_t *bgep, bge_regno_t addr, uint64_t data)
9281369Sdduvall {
9291369Sdduvall 	BGE_TRACE(("bge_nic_put64($%p, 0x%lx, 0x%016llx)",
9301369Sdduvall 		(void *)bgep, addr, data));
9311369Sdduvall 
9321369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9331369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9341369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9351369Sdduvall 
9361369Sdduvall #ifdef	__amd64
9371369Sdduvall 	if (bge_get_em64t_type()) {
9381369Sdduvall 		ddi_put32(bgep->io_handle,
9391369Sdduvall 			PIO_ADDR(bgep, addr), (uint32_t)data);
9401369Sdduvall 		BGE_PCICHK(bgep);
9411369Sdduvall 		ddi_put32(bgep->io_handle,
9421369Sdduvall 			PIO_ADDR(bgep, addr + 4), (uint32_t)(data >> 32));
9431369Sdduvall 	} else {
9441369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9451369Sdduvall 	}
9461369Sdduvall #else
9471369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), data);
9481369Sdduvall #endif
9491369Sdduvall 
9501369Sdduvall 	BGE_PCICHK(bgep);
9511369Sdduvall }
9521369Sdduvall 
9531369Sdduvall /*
9541369Sdduvall  * The DDI doesn't provide get/put functions for 128 bit data
9551369Sdduvall  * so we put RCBs out as two 64-bit chunks instead.
9561369Sdduvall  */
9571369Sdduvall static void bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp);
9581369Sdduvall #pragma	inline(bge_nic_putrcb)
9591369Sdduvall 
9601369Sdduvall static void
9611369Sdduvall bge_nic_putrcb(bge_t *bgep, bge_regno_t addr, bge_rcb_t *rcbp)
9621369Sdduvall {
9631369Sdduvall 	uint64_t *p;
9641369Sdduvall 
9651369Sdduvall 	BGE_TRACE(("bge_nic_putrcb($%p, 0x%lx, 0x%016llx:%04x:%04x:%08x)",
9661369Sdduvall 		(void *)bgep, addr, rcbp->host_ring_addr,
9671369Sdduvall 		rcbp->max_len, rcbp->flags, rcbp->nic_ring_addr));
9681369Sdduvall 
9691369Sdduvall 	ASSERT((addr % sizeof (*rcbp)) == 0);
9701369Sdduvall 
9711369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
9721369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
9731369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
9741369Sdduvall 
9751369Sdduvall 	p = (void *)rcbp;
9761369Sdduvall #ifdef	__amd64
9771369Sdduvall 	if (bge_get_em64t_type()) {
9781369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr),
9791369Sdduvall 			(uint32_t)(*p));
9801369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 4),
9811369Sdduvall 			(uint32_t)(*p >> 32));
9821369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 8),
9831369Sdduvall 			(uint32_t)(*(p + 1)));
9841369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, addr + 12),
9851369Sdduvall 			(uint32_t)(*p >> 32));
9861369Sdduvall 
9871369Sdduvall 	} else {
9881369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
9891369Sdduvall 		ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr+8), *p);
9901369Sdduvall 	}
9911369Sdduvall #else
9921369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr), *p++);
9931369Sdduvall 	ddi_put64(bgep->io_handle, PIO_ADDR(bgep, addr + 8), *p);
9941369Sdduvall #endif
9951369Sdduvall 
9961369Sdduvall 	BGE_PCICHK(bgep);
9971369Sdduvall }
9981369Sdduvall 
9991369Sdduvall static void bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes);
10001369Sdduvall #pragma	inline(bge_nic_zero)
10011369Sdduvall 
10021369Sdduvall static void
10031369Sdduvall bge_nic_zero(bge_t *bgep, bge_regno_t addr, uint32_t nbytes)
10041369Sdduvall {
10051369Sdduvall 	BGE_TRACE(("bge_nic_zero($%p, 0x%lx, 0x%x)",
10061369Sdduvall 		(void *)bgep, addr, nbytes));
10071369Sdduvall 
10081369Sdduvall 	ASSERT((addr & ~MWBAR_GRANULE_MASK) ==
10091369Sdduvall 		((addr+nbytes) & ~MWBAR_GRANULE_MASK));
10101369Sdduvall 
10111369Sdduvall 	bge_nic_setwin(bgep, addr & ~MWBAR_GRANULE_MASK);
10121369Sdduvall 	addr &= MWBAR_GRANULE_MASK;
10131369Sdduvall 	addr += NIC_MEM_WINDOW_OFFSET;
10141369Sdduvall 
10151369Sdduvall 	(void) ddi_device_zero(bgep->io_handle, PIO_ADDR(bgep, addr),
10161369Sdduvall 		nbytes, 1, DDI_DATA_SZ08_ACC);
10171369Sdduvall 	BGE_PCICHK(bgep);
10181369Sdduvall }
10191369Sdduvall 
10201369Sdduvall /*
10211369Sdduvall  * MII (PHY) register get/set access routines
10221369Sdduvall  *
10231369Sdduvall  * These use the chip's MII auto-access method, controlled by the
10241369Sdduvall  * MII Communication register at 0x044c, so the CPU doesn't have
10251369Sdduvall  * to fiddle with the individual bits.
10261369Sdduvall  */
10271369Sdduvall 
10281369Sdduvall #undef	BGE_DBG
10291369Sdduvall #define	BGE_DBG		BGE_DBG_MII	/* debug flag for this code	*/
10301369Sdduvall 
10311369Sdduvall static uint16_t bge_mii_access(bge_t *bgep, bge_regno_t regno,
10321369Sdduvall 				uint16_t data, uint32_t cmd);
10331369Sdduvall #pragma	no_inline(bge_mii_access)
10341369Sdduvall 
10351369Sdduvall static uint16_t
10361369Sdduvall bge_mii_access(bge_t *bgep, bge_regno_t regno, uint16_t data, uint32_t cmd)
10371369Sdduvall {
10381369Sdduvall 	uint32_t timeout;
10391369Sdduvall 	uint32_t regval1;
10401369Sdduvall 	uint32_t regval2;
10411369Sdduvall 
10421369Sdduvall 	BGE_TRACE(("bge_mii_access($%p, 0x%lx, 0x%x, 0x%x)",
10431369Sdduvall 		(void *)bgep, regno, data, cmd));
10441369Sdduvall 
10451369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
10461369Sdduvall 
10471369Sdduvall 	/*
10481369Sdduvall 	 * Assemble the command ...
10491369Sdduvall 	 */
10501369Sdduvall 	cmd |= data << MI_COMMS_DATA_SHIFT;
10511369Sdduvall 	cmd |= regno << MI_COMMS_REGISTER_SHIFT;
10521369Sdduvall 	cmd |= bgep->phy_mii_addr << MI_COMMS_ADDRESS_SHIFT;
10531369Sdduvall 	cmd |= MI_COMMS_START;
10541369Sdduvall 
10551369Sdduvall 	/*
10561369Sdduvall 	 * Wait for any command already in progress ...
10571369Sdduvall 	 *
10581369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
10591369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
10601369Sdduvall 	 * Nonetheless, we have sometimes seen the MI_COMMS_START
10611369Sdduvall 	 * bit set here -- it seems that the chip can initiate MII
10621369Sdduvall 	 * accesses internally, even with polling OFF.
10631369Sdduvall 	 */
10641369Sdduvall 	regval1 = regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
10651369Sdduvall 	for (timeout = 1000; ; ) {
10661369Sdduvall 		if ((regval2 & MI_COMMS_START) == 0) {
10671369Sdduvall 			bge_reg_put32(bgep, MI_COMMS_REG, cmd);
10681369Sdduvall 			break;
10691369Sdduvall 		}
10701369Sdduvall 		if (--timeout == 0)
10711369Sdduvall 			break;
10721369Sdduvall 		drv_usecwait(10);
10731369Sdduvall 		regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
10741369Sdduvall 	}
10751369Sdduvall 
10761369Sdduvall 	if (timeout != 1000)
10771369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
10781369Sdduvall 			"MI_COMMS_START set for %d us; 0x%x->0x%x",
10791369Sdduvall 			cmd, 10*(1000-timeout), regval1, regval2));
10801369Sdduvall 
10811369Sdduvall 	ASSERT(timeout != 0);
10821369Sdduvall 	if (timeout == 0)
10831369Sdduvall 		return ((uint16_t)~0u);
10841369Sdduvall 
10851369Sdduvall 	regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
10861369Sdduvall 	for (timeout = 1000; ; ) {
10871369Sdduvall 		if ((regval1 & MI_COMMS_START) == 0)
10881369Sdduvall 			break;
10891369Sdduvall 		if (--timeout == 0)
10901369Sdduvall 			break;
10911369Sdduvall 		drv_usecwait(10);
10921369Sdduvall 		regval1 = bge_reg_get32(bgep, MI_COMMS_REG);
10931369Sdduvall 	}
10941369Sdduvall 
10951369Sdduvall 	/*
10961369Sdduvall 	 * Drop out early if the READ FAILED bit is set -- this chip
10971369Sdduvall 	 * could be a 5703/4S, with a SerDes instead of a PHY!
10981369Sdduvall 	 */
10991369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11001369Sdduvall 		return ((uint16_t)~0u);
11011369Sdduvall 
11021369Sdduvall 	ASSERT(timeout != 0);
11031369Sdduvall 	if (timeout == 0)
11041369Sdduvall 		return ((uint16_t)~0u);
11051369Sdduvall 
11061369Sdduvall 	/*
11071369Sdduvall 	 * The PRM says to wait 5us after seeing the START bit clear
11081369Sdduvall 	 * and then re-read the register to get the final value of the
11091369Sdduvall 	 * data field, in order to avoid a race condition where the
11101369Sdduvall 	 * START bit is clear but the data field isn't yet valid.
11111369Sdduvall 	 *
11121369Sdduvall 	 * Note: we don't actually seem to be encounter this race;
11131369Sdduvall 	 * except when the START bit is seen set again (see below),
11141369Sdduvall 	 * the data field doesn't change during this 5us interval.
11151369Sdduvall 	 */
11161369Sdduvall 	drv_usecwait(5);
11171369Sdduvall 	regval2 = bge_reg_get32(bgep, MI_COMMS_REG);
11181369Sdduvall 
11191369Sdduvall 	/*
11201369Sdduvall 	 * Unfortunately, when following the PRMs instructions above,
11211369Sdduvall 	 * we have occasionally seen the START bit set again(!) in the
11221369Sdduvall 	 * value read after the 5us delay. This seems to be due to the
11231369Sdduvall 	 * chip autonomously starting another MII access internally.
11241369Sdduvall 	 * In such cases, the command/data/etc fields relate to the
11251369Sdduvall 	 * internal command, rather than the one that we thought had
11261369Sdduvall 	 * just finished.  So in this case, we fall back to returning
11271369Sdduvall 	 * the data from the original read that showed START clear.
11281369Sdduvall 	 */
11291369Sdduvall 	if (regval2 & MI_COMMS_START) {
11301369Sdduvall 		BGE_REPORT((bgep, "bge_mii_access: cmd 0x%x -- "
11311369Sdduvall 			"MI_COMMS_START set after transaction; 0x%x->0x%x",
11321369Sdduvall 			cmd, regval1, regval2));
11331369Sdduvall 		regval2 = regval1;
11341369Sdduvall 	}
11351369Sdduvall 
11361369Sdduvall 	ASSERT((regval2 & MI_COMMS_START) == 0);
11371369Sdduvall 	if (regval2 & MI_COMMS_START)
11381369Sdduvall 		return ((uint16_t)~0u);
11391369Sdduvall 
11401369Sdduvall 	ASSERT((regval2 & MI_COMMS_READ_FAILED) == 0);
11411369Sdduvall 	if (regval2 & MI_COMMS_READ_FAILED)
11421369Sdduvall 		return ((uint16_t)~0u);
11431369Sdduvall 
11441369Sdduvall 	return ((regval2 & MI_COMMS_DATA_MASK) >> MI_COMMS_DATA_SHIFT);
11451369Sdduvall }
11461369Sdduvall 
11471369Sdduvall uint16_t bge_mii_get16(bge_t *bgep, bge_regno_t regno);
11481369Sdduvall #pragma	no_inline(bge_mii_get16)
11491369Sdduvall 
11501369Sdduvall uint16_t
11511369Sdduvall bge_mii_get16(bge_t *bgep, bge_regno_t regno)
11521369Sdduvall {
11531369Sdduvall 	BGE_TRACE(("bge_mii_get16($%p, 0x%lx)",
11541369Sdduvall 		(void *)bgep, regno));
11551369Sdduvall 
11561369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11571369Sdduvall 
11581369Sdduvall 	return (bge_mii_access(bgep, regno, 0, MI_COMMS_COMMAND_READ));
11591369Sdduvall }
11601369Sdduvall 
11611369Sdduvall void bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data);
11621369Sdduvall #pragma	no_inline(bge_mii_put16)
11631369Sdduvall 
11641369Sdduvall void
11651369Sdduvall bge_mii_put16(bge_t *bgep, bge_regno_t regno, uint16_t data)
11661369Sdduvall {
11671369Sdduvall 	BGE_TRACE(("bge_mii_put16($%p, 0x%lx, 0x%x)",
11681369Sdduvall 		(void *)bgep, regno, data));
11691369Sdduvall 
11701369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
11711369Sdduvall 
11721369Sdduvall 	(void) bge_mii_access(bgep, regno, data, MI_COMMS_COMMAND_WRITE);
11731369Sdduvall }
11741369Sdduvall 
11751369Sdduvall #undef	BGE_DBG
11761369Sdduvall #define	BGE_DBG		BGE_DBG_SEEPROM	/* debug flag for this code	*/
11771369Sdduvall 
11781369Sdduvall #if	BGE_SEE_IO32 || BGE_FLASH_IO32
11791369Sdduvall 
11801369Sdduvall /*
11811369Sdduvall  * Basic SEEPROM get/set access routine
11821369Sdduvall  *
11831369Sdduvall  * This uses the chip's SEEPROM auto-access method, controlled by the
11841369Sdduvall  * Serial EEPROM Address/Data Registers at 0x6838/683c, so the CPU
11851369Sdduvall  * doesn't have to fiddle with the individual bits.
11861369Sdduvall  *
11871369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
11881369Sdduvall  * the right to access the SEEPROM, via bge_nvmem_acquire() above.
11891369Sdduvall  *
11901369Sdduvall  * Return value:
11911369Sdduvall  *	0 on success,
11921369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
11931369Sdduvall  *	EPROTO on other h/w or s/w errors.
11941369Sdduvall  *
11951369Sdduvall  * <*dp> is an input to a SEEPROM_ACCESS_WRITE operation, or an output
11961369Sdduvall  * from a (successful) SEEPROM_ACCESS_READ.
11971369Sdduvall  */
11981369Sdduvall static int bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
11991369Sdduvall 				uint32_t *dp);
12001369Sdduvall #pragma	no_inline(bge_seeprom_access)
12011369Sdduvall 
12021369Sdduvall static int
12031369Sdduvall bge_seeprom_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
12041369Sdduvall {
12051369Sdduvall 	uint32_t tries;
12061369Sdduvall 	uint32_t regval;
12071369Sdduvall 
12081369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
12091369Sdduvall 
12101369Sdduvall 	/*
12111369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
12121369Sdduvall 	 * to specifically enable SEEPROM access (Flash is the default).
12131369Sdduvall 	 * On older chips, we don't; SEEPROM is the only NVtype supported,
12141369Sdduvall 	 * and the NVM control registers don't exist ...
12151369Sdduvall 	 */
12161369Sdduvall 	switch (bgep->chipid.nvtype) {
12171369Sdduvall 	case BGE_NVTYPE_NONE:
12181369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
12191369Sdduvall 		_NOTE(NOTREACHED)
12201369Sdduvall 	case BGE_NVTYPE_SEEPROM:
12211369Sdduvall 		break;
12221369Sdduvall 
12231369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
12241369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
12251369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
12261369Sdduvall 	default:
12271369Sdduvall 		bge_reg_set32(bgep, NVM_CONFIG1_REG,
12281369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
12291369Sdduvall 		break;
12301369Sdduvall 	}
12311369Sdduvall 
12321369Sdduvall 	/*
12331369Sdduvall 	 * Check there's no command in progress.
12341369Sdduvall 	 *
12351369Sdduvall 	 * Note: this *shouldn't* ever find that there is a command
12361369Sdduvall 	 * in progress, because we already hold the <genlock> mutex.
12371369Sdduvall 	 * Also, to ensure we don't have a conflict with the chip's
12381369Sdduvall 	 * internal firmware or a process accessing the same (shared)
12391369Sdduvall 	 * SEEPROM through the other port of a 5704, we've already
12401369Sdduvall 	 * been through the "software arbitration" protocol.
12411369Sdduvall 	 * So this is just a final consistency check: we shouldn't
12421369Sdduvall 	 * see EITHER the START bit (command started but not complete)
12431369Sdduvall 	 * OR the COMPLETE bit (command completed but not cleared).
12441369Sdduvall 	 */
12451369Sdduvall 	regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12461369Sdduvall 	if (regval & SEEPROM_ACCESS_START)
12471369Sdduvall 		return (EPROTO);
12481369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE)
12491369Sdduvall 		return (EPROTO);
12501369Sdduvall 
12511369Sdduvall 	/*
12521369Sdduvall 	 * Assemble the command ...
12531369Sdduvall 	 */
12541369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_ADDRESS_MASK;
12551369Sdduvall 	addr >>= SEEPROM_ACCESS_ADDRESS_SIZE;
12561369Sdduvall 	addr <<= SEEPROM_ACCESS_DEVID_SHIFT;
12571369Sdduvall 	cmd |= addr & SEEPROM_ACCESS_DEVID_MASK;
12581369Sdduvall 	cmd |= SEEPROM_ACCESS_START;
12591369Sdduvall 	cmd |= SEEPROM_ACCESS_COMPLETE;
12601369Sdduvall 	cmd |= regval & SEEPROM_ACCESS_HALFCLOCK_MASK;
12611369Sdduvall 
12621369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_DATA_REG, *dp);
12631369Sdduvall 	bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, cmd);
12641369Sdduvall 
12651369Sdduvall 	/*
12661369Sdduvall 	 * By observation, a successful access takes ~20us on a 5703/4,
12671369Sdduvall 	 * but apparently much longer (up to 1000us) on the obsolescent
12681369Sdduvall 	 * BCM5700/BCM5701.  We want to be sure we don't get any false
12691369Sdduvall 	 * timeouts here; but OTOH, we don't want a bogus access to lock
12701369Sdduvall 	 * out interrupts for longer than necessary. So we'll allow up
12711369Sdduvall 	 * to 1000us ...
12721369Sdduvall 	 */
12731369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
12741369Sdduvall 		regval = bge_reg_get32(bgep, SERIAL_EEPROM_ADDRESS_REG);
12751369Sdduvall 		if (regval & SEEPROM_ACCESS_COMPLETE)
12761369Sdduvall 			break;
12771369Sdduvall 		drv_usecwait(1);
12781369Sdduvall 	}
12791369Sdduvall 
12801369Sdduvall 	ASSERT((regval & SEEPROM_ACCESS_START) == 0);
12811369Sdduvall 	if (regval & SEEPROM_ACCESS_COMPLETE) {
12821369Sdduvall 		/*
12831369Sdduvall 		 * All OK; read the SEEPROM data register, then write back
12841369Sdduvall 		 * the value read from the address register in order to
12851369Sdduvall 		 * clear the <complete> bit and leave the SEEPROM access
12861369Sdduvall 		 * state machine idle, ready for the next access ...
12871369Sdduvall 		 */
12881369Sdduvall 		BGE_DEBUG(("bge_seeprom_access: complete after %d us", tries));
12891369Sdduvall 		*dp = bge_reg_get32(bgep, SERIAL_EEPROM_DATA_REG);
12901369Sdduvall 		bge_reg_put32(bgep, SERIAL_EEPROM_ADDRESS_REG, regval);
12911369Sdduvall 		return (0);
12921369Sdduvall 	}
12931369Sdduvall 
12941369Sdduvall 	/*
12951369Sdduvall 	 * Hmm ... what happened here?
12961369Sdduvall 	 *
12971369Sdduvall 	 * Most likely, the user addressed an non-existent SEEPROM. Or
12981369Sdduvall 	 * maybe the SEEPROM was busy internally (e.g. processing a write)
12991369Sdduvall 	 * and didn't respond to being addressed. Either way, it's left
13001369Sdduvall 	 * the SEEPROM access state machine wedged. So we'll reset it
13011369Sdduvall 	 * before we leave, so it's ready for next time ...
13021369Sdduvall 	 */
13031369Sdduvall 	BGE_DEBUG(("bge_seeprom_access: timed out after %d us", tries));
13041369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
13051369Sdduvall 	return (ENODATA);
13061369Sdduvall }
13071369Sdduvall 
13081369Sdduvall /*
13091369Sdduvall  * Basic Flash get/set access routine
13101369Sdduvall  *
13111369Sdduvall  * These use the chip's Flash auto-access method, controlled by the
13121369Sdduvall  * Flash Access Registers at 0x7000-701c, so the CPU doesn't have to
13131369Sdduvall  * fiddle with the individual bits.
13141369Sdduvall  *
13151369Sdduvall  * The caller should hold <genlock> and *also* have already acquired
13161369Sdduvall  * the right to access the Flash, via bge_nvmem_acquire() above.
13171369Sdduvall  *
13181369Sdduvall  * Return value:
13191369Sdduvall  *	0 on success,
13201369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
13211369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
13221369Sdduvall  *
13231369Sdduvall  * <*dp> is an input to a NVM_FLASH_CMD_WR operation, or an output
13241369Sdduvall  * from a (successful) NVM_FLASH_CMD_RD.
13251369Sdduvall  */
13261369Sdduvall static int bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr,
13271369Sdduvall 				uint32_t *dp);
13281369Sdduvall #pragma	no_inline(bge_flash_access)
13291369Sdduvall 
13301369Sdduvall static int
13311369Sdduvall bge_flash_access(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
13321369Sdduvall {
13331369Sdduvall 	uint32_t tries;
13341369Sdduvall 	uint32_t regval;
13351369Sdduvall 
13361369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
13371369Sdduvall 
13381369Sdduvall 	/*
13391369Sdduvall 	 * On the newer chips that support both SEEPROM & Flash, we need
13401369Sdduvall 	 * to specifically disable SEEPROM access while accessing Flash.
13411369Sdduvall 	 * The older chips don't support Flash, and the NVM registers don't
13421369Sdduvall 	 * exist, so we shouldn't be here at all!
13431369Sdduvall 	 */
13441369Sdduvall 	switch (bgep->chipid.nvtype) {
13451369Sdduvall 	case BGE_NVTYPE_NONE:
13461369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
13471369Sdduvall 		_NOTE(NOTREACHED)
13481369Sdduvall 	case BGE_NVTYPE_SEEPROM:
13491369Sdduvall 		return (ENODEV);
13501369Sdduvall 
13511369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
13521369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
13531369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
13541369Sdduvall 	default:
13551369Sdduvall 		bge_reg_clr32(bgep, NVM_CONFIG1_REG,
13561369Sdduvall 				NVM_CFG1_LEGACY_SEEPROM_MODE);
13571369Sdduvall 		break;
13581369Sdduvall 	}
13591369Sdduvall 
13601369Sdduvall 	/*
13611369Sdduvall 	 * Assemble the command ...
13621369Sdduvall 	 */
13631369Sdduvall 	addr &= NVM_FLASH_ADDR_MASK;
13641369Sdduvall 	cmd |= NVM_FLASH_CMD_DOIT;
13651369Sdduvall 	cmd |= NVM_FLASH_CMD_FIRST;
13661369Sdduvall 	cmd |= NVM_FLASH_CMD_LAST;
13671369Sdduvall 	cmd |= NVM_FLASH_CMD_DONE;
13681369Sdduvall 
13691369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_WRITE_REG, *dp);
13701369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_ADDR_REG, addr);
13711369Sdduvall 	bge_reg_put32(bgep, NVM_FLASH_CMD_REG, cmd);
13721369Sdduvall 
13731369Sdduvall 	/*
13741369Sdduvall 	 * Allow up to 1000ms ...
13751369Sdduvall 	 */
13761369Sdduvall 	for (tries = 0; tries < 1000; ++tries) {
13771369Sdduvall 		regval = bge_reg_get32(bgep, NVM_FLASH_CMD_REG);
13781369Sdduvall 		if (regval & NVM_FLASH_CMD_DONE)
13791369Sdduvall 			break;
13801369Sdduvall 		drv_usecwait(1);
13811369Sdduvall 	}
13821369Sdduvall 
13831369Sdduvall 	if (regval & NVM_FLASH_CMD_DONE) {
13841369Sdduvall 		/*
13851369Sdduvall 		 * All OK; read the data from the Flash read register
13861369Sdduvall 		 */
13871369Sdduvall 		BGE_DEBUG(("bge_flash_access: complete after %d us", tries));
13881369Sdduvall 		*dp = bge_reg_get32(bgep, NVM_FLASH_READ_REG);
13891369Sdduvall 		return (0);
13901369Sdduvall 	}
13911369Sdduvall 
13921369Sdduvall 	/*
13931369Sdduvall 	 * Hmm ... what happened here?
13941369Sdduvall 	 *
13951369Sdduvall 	 * Most likely, the user addressed an non-existent Flash. Or
13961369Sdduvall 	 * maybe the Flash was busy internally (e.g. processing a write)
13971369Sdduvall 	 * and didn't respond to being addressed. Either way, there's
13981369Sdduvall 	 * nothing we can here ...
13991369Sdduvall 	 */
14001369Sdduvall 	BGE_DEBUG(("bge_flash_access: timed out after %d us", tries));
14011369Sdduvall 	return (ENODATA);
14021369Sdduvall }
14031369Sdduvall 
14041369Sdduvall /*
14051369Sdduvall  * The next two functions regulate access to the NVram (if fitted).
14061369Sdduvall  *
14071369Sdduvall  * On a 5704 (dual core) chip, there's only one SEEPROM and one Flash
14081369Sdduvall  * (SPI) interface, but they can be accessed through either port. These
14091369Sdduvall  * are managed by different instance of this driver and have no software
14101369Sdduvall  * state in common.
14111369Sdduvall  *
14121369Sdduvall  * In addition (and even on a single core chip) the chip's internal
14131369Sdduvall  * firmware can access the SEEPROM/Flash, most notably after a RESET
14141369Sdduvall  * when it may download code to run internally.
14151369Sdduvall  *
14161369Sdduvall  * So we need to arbitrate between these various software agents.  For
14171369Sdduvall  * this purpose, the chip provides the Software Arbitration Register,
14181369Sdduvall  * which implements hardware(!) arbitration.
14191369Sdduvall  *
14201369Sdduvall  * This functionality didn't exist on older (5700/5701) chips, so there's
14211369Sdduvall  * nothing we can do by way of arbitration on those; also, if there's no
14221369Sdduvall  * SEEPROM/Flash fitted (or we couldn't determine what type), there's also
14231369Sdduvall  * nothing to do.
14241369Sdduvall  *
14251369Sdduvall  * The internal firmware appears to use Request 0, which is the highest
14261369Sdduvall  * priority.  So we'd like to use Request 2, leaving one higher and one
14271369Sdduvall  * lower for any future developments ... but apparently this doesn't
14281369Sdduvall  * always work.  So for now, the code uses Request 1 ;-(
14291369Sdduvall  */
14301369Sdduvall 
14311369Sdduvall #define	NVM_READ_REQ	NVM_READ_REQ1
14321369Sdduvall #define	NVM_RESET_REQ	NVM_RESET_REQ1
14331369Sdduvall #define	NVM_SET_REQ	NVM_SET_REQ1
14341369Sdduvall 
14351369Sdduvall static void bge_nvmem_relinquish(bge_t *bgep);
14361369Sdduvall #pragma	no_inline(bge_nvmem_relinquish)
14371369Sdduvall 
14381369Sdduvall static void
14391369Sdduvall bge_nvmem_relinquish(bge_t *bgep)
14401369Sdduvall {
14411369Sdduvall 	uint32_t regval;
14421369Sdduvall 
14431369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
14441369Sdduvall 
14451369Sdduvall 	switch (bgep->chipid.nvtype) {
14461369Sdduvall 	case BGE_NVTYPE_NONE:
14471369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
14481369Sdduvall 		_NOTE(NOTREACHED)
14491369Sdduvall 		return;
14501369Sdduvall 
14511369Sdduvall 	case BGE_NVTYPE_SEEPROM:
14521369Sdduvall 		/*
14531369Sdduvall 		 * No arbitration performed, no release needed
14541369Sdduvall 		 */
14551369Sdduvall 		return;
14561369Sdduvall 
14571369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
14581369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
14591369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
14601369Sdduvall 	default:
14611369Sdduvall 		break;
14621369Sdduvall 	}
14631369Sdduvall 
14641369Sdduvall 	/*
14651369Sdduvall 	 * Our own request should be present (whether or not granted) ...
14661369Sdduvall 	 */
14671369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14681369Sdduvall 	ASSERT((regval & NVM_READ_REQ) != 0);
14691369Sdduvall 
14701369Sdduvall 	/*
14711369Sdduvall 	 * ... this will make it go away.
14721369Sdduvall 	 */
14731369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_RESET_REQ);
14741369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
14751369Sdduvall 	ASSERT((regval & NVM_READ_REQ) == 0);
14761369Sdduvall }
14771369Sdduvall 
14781369Sdduvall /*
14791369Sdduvall  * Arbitrate for access to the NVmem, if necessary
14801369Sdduvall  *
14811369Sdduvall  * Return value:
14821369Sdduvall  *	0 on success
14831369Sdduvall  *	EAGAIN if the device is in use (retryable)
14841369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
14851369Sdduvall  */
14861369Sdduvall static int bge_nvmem_acquire(bge_t *bgep);
14871369Sdduvall #pragma	no_inline(bge_nvmem_acquire)
14881369Sdduvall 
14891369Sdduvall static int
14901369Sdduvall bge_nvmem_acquire(bge_t *bgep)
14911369Sdduvall {
14921369Sdduvall 	uint32_t regval;
14931369Sdduvall 	uint32_t tries;
14941369Sdduvall 
14951369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
14961369Sdduvall 
14971369Sdduvall 	switch (bgep->chipid.nvtype) {
14981369Sdduvall 	case BGE_NVTYPE_NONE:
14991369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
15001369Sdduvall 		/*
15011369Sdduvall 		 * Access denied: no (recognisable) device fitted
15021369Sdduvall 		 */
15031369Sdduvall 		return (ENODEV);
15041369Sdduvall 
15051369Sdduvall 	case BGE_NVTYPE_SEEPROM:
15061369Sdduvall 		/*
15071369Sdduvall 		 * Access granted: no arbitration needed (or possible)
15081369Sdduvall 		 */
15091369Sdduvall 		return (0);
15101369Sdduvall 
15111369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
15121369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
15131369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
15141369Sdduvall 	default:
15151369Sdduvall 		/*
15161369Sdduvall 		 * Access conditional: conduct arbitration protocol
15171369Sdduvall 		 */
15181369Sdduvall 		break;
15191369Sdduvall 	}
15201369Sdduvall 
15211369Sdduvall 	/*
15221369Sdduvall 	 * We're holding the per-port mutex <genlock>, so no-one other
15231369Sdduvall 	 * threads can be attempting to access the NVmem through *this*
15241369Sdduvall 	 * port. But it could be in use by the *other* port (of a 5704),
15251369Sdduvall 	 * or by the chip's internal firmware, so we have to go through
15261369Sdduvall 	 * the full (hardware) arbitration protocol ...
15271369Sdduvall 	 *
15281369Sdduvall 	 * Note that *because* we're holding <genlock>, the interrupt handler
15291369Sdduvall 	 * won't be able to progress.  So we're only willing to spin for a
15301369Sdduvall 	 * fairly short time.  Specifically:
15311369Sdduvall 	 *
15321369Sdduvall 	 *	We *must* wait long enough for the hardware to resolve all
15331369Sdduvall 	 *	requests and determine the winner.  Fortunately, this is
15341369Sdduvall 	 *	"almost instantaneous", even as observed by GHz CPUs.
15351369Sdduvall 	 *
15361369Sdduvall 	 *	A successful access by another Solaris thread (via either
15371369Sdduvall 	 *	port) typically takes ~20us.  So waiting a bit longer than
15381369Sdduvall 	 *	that will give a good chance of success, if the other user
15391369Sdduvall 	 *	*is* another thread on the other port.
15401369Sdduvall 	 *
15411369Sdduvall 	 *	However, the internal firmware can hold on to the NVmem
15421369Sdduvall 	 *	for *much* longer: at least 10 milliseconds just after a
15431369Sdduvall 	 *	RESET, and maybe even longer if the NVmem actually contains
15441369Sdduvall 	 *	code to download and run on the internal CPUs.
15451369Sdduvall 	 *
15461369Sdduvall 	 * So, we'll allow 50us; if that's not enough then it's up to the
15471369Sdduvall 	 * caller to retry later (hence the choice of return code EAGAIN).
15481369Sdduvall 	 */
15491369Sdduvall 	regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15501369Sdduvall 	ASSERT((regval & NVM_READ_REQ) == 0);
15511369Sdduvall 	bge_reg_put32(bgep, NVM_SW_ARBITRATION_REG, NVM_SET_REQ);
15521369Sdduvall 
15531369Sdduvall 	for (tries = 0; tries < 50; ++tries) {
15541369Sdduvall 		regval = bge_reg_get32(bgep, NVM_SW_ARBITRATION_REG);
15551369Sdduvall 		ASSERT((regval & NVM_READ_REQ) != 0);
15561369Sdduvall 		if (regval & NVM_WON_REQ1)
15571369Sdduvall 			break;
15581369Sdduvall 		drv_usecwait(1);
15591369Sdduvall 	}
15601369Sdduvall 
15611369Sdduvall 	if (regval & NVM_WON_REQ1) {
15621369Sdduvall 		BGE_DEBUG(("bge_nvmem_acquire: won after %d us", tries));
15631369Sdduvall 		return (0);
15641369Sdduvall 	}
15651369Sdduvall 
15661369Sdduvall 	/*
15671369Sdduvall 	 * Somebody else must be accessing the NVmem, so abandon our
15681369Sdduvall 	 * attempt take control of it.  The caller can try again later ...
15691369Sdduvall 	 */
15701369Sdduvall 	BGE_DEBUG(("bge_nvmem_acquire: lost after %d us", tries));
15711369Sdduvall 	bge_nvmem_relinquish(bgep);
15721369Sdduvall 	return (EAGAIN);
15731369Sdduvall }
15741369Sdduvall 
15751369Sdduvall /*
15761369Sdduvall  * This code assumes that the GPIO1 bit has been wired up to the NVmem
15771369Sdduvall  * write protect line in such a way that the NVmem is protected when
15781369Sdduvall  * GPIO1 is an input, or is an output but driven high.  Thus, to make the
15791369Sdduvall  * NVmem writable we have to change GPIO1 to an output AND drive it low.
15801369Sdduvall  *
15811369Sdduvall  * Note: there's only one set of GPIO pins on a 5704, even though they
15821369Sdduvall  * can be accessed through either port.  So the chip has to resolve what
15831369Sdduvall  * happens if the two ports program a single pin differently ... the rule
15841369Sdduvall  * it uses is that if the ports disagree about the *direction* of a pin,
15851369Sdduvall  * "output" wins over "input", but if they disagree about its *value* as
15861369Sdduvall  * an output, then the pin is TRISTATED instead!  In such a case, no-one
15871369Sdduvall  * wins, and the external signal does whatever the external circuitry
15881369Sdduvall  * defines as the default -- which we've assumed is the PROTECTED state.
15891369Sdduvall  * So, we always change GPIO1 back to being an *input* whenever we're not
15901369Sdduvall  * specifically using it to unprotect the NVmem. This allows either port
15911369Sdduvall  * to update the NVmem, although obviously only one at a a time!
15921369Sdduvall  *
15931369Sdduvall  * The caller should hold <genlock> and *also* have already acquired the
15941369Sdduvall  * right to access the NVmem, via bge_nvmem_acquire() above.
15951369Sdduvall  */
15961369Sdduvall static void bge_nvmem_protect(bge_t *bgep, boolean_t protect);
15971369Sdduvall #pragma	inline(bge_nvmem_protect)
15981369Sdduvall 
15991369Sdduvall static void
16001369Sdduvall bge_nvmem_protect(bge_t *bgep, boolean_t protect)
16011369Sdduvall {
16021369Sdduvall 	uint32_t regval;
16031369Sdduvall 
16041369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
16051369Sdduvall 
16061369Sdduvall 	regval = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
16071369Sdduvall 	if (protect) {
16081369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_1;
16091369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16101369Sdduvall 	} else {
16111369Sdduvall 		regval &= ~MLCR_MISC_PINS_OUTPUT_1;
16121369Sdduvall 		regval |= MLCR_MISC_PINS_OUTPUT_ENABLE_1;
16131369Sdduvall 	}
16141369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG, regval);
16151369Sdduvall }
16161369Sdduvall 
16171369Sdduvall /*
16181369Sdduvall  * Now put it all together ...
16191369Sdduvall  *
16201369Sdduvall  * Try to acquire control of the NVmem; if successful, then:
16211369Sdduvall  *	unprotect it (if we want to write to it)
16221369Sdduvall  *	perform the requested access
16231369Sdduvall  *	reprotect it (after a write)
16241369Sdduvall  *	relinquish control
16251369Sdduvall  *
16261369Sdduvall  * Return value:
16271369Sdduvall  *	0 on success,
16281369Sdduvall  *	EAGAIN if the device is in use (retryable)
16291369Sdduvall  *	ENODATA on access timeout (maybe retryable: device may just be busy)
16301369Sdduvall  *	ENODEV if the NVmem device is missing or otherwise unusable
16311369Sdduvall  *	EPROTO on other h/w or s/w errors.
16321369Sdduvall  */
16331369Sdduvall static int
16341369Sdduvall bge_nvmem_rw32(bge_t *bgep, uint32_t cmd, bge_regno_t addr, uint32_t *dp)
16351369Sdduvall {
16361369Sdduvall 	int err;
16371369Sdduvall 
16381369Sdduvall 	if ((err = bge_nvmem_acquire(bgep)) == 0) {
16391369Sdduvall 		switch (cmd) {
16401369Sdduvall 		case BGE_SEE_READ:
16411369Sdduvall 			err = bge_seeprom_access(bgep,
16421369Sdduvall 			    SEEPROM_ACCESS_READ, addr, dp);
16431369Sdduvall 			break;
16441369Sdduvall 
16451369Sdduvall 		case BGE_SEE_WRITE:
16461369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16471369Sdduvall 			err = bge_seeprom_access(bgep,
16481369Sdduvall 			    SEEPROM_ACCESS_WRITE, addr, dp);
16491369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16501369Sdduvall 			break;
16511369Sdduvall 
16521369Sdduvall 		case BGE_FLASH_READ:
16531369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16541369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16551369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16561369Sdduvall 				    NVM_ACCESS_ENABLE);
16571369Sdduvall 			}
16581369Sdduvall 			err = bge_flash_access(bgep,
16591369Sdduvall 			    NVM_FLASH_CMD_RD, addr, dp);
16601369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16611369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16621369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16631369Sdduvall 				    NVM_ACCESS_ENABLE);
16641369Sdduvall 			}
16651369Sdduvall 			break;
16661369Sdduvall 
16671369Sdduvall 		case BGE_FLASH_WRITE:
16681369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16691369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16701369Sdduvall 				bge_reg_set32(bgep, NVM_ACCESS_REG,
16711369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16721369Sdduvall 			}
16731369Sdduvall 			bge_nvmem_protect(bgep, B_FALSE);
16741369Sdduvall 			err = bge_flash_access(bgep,
16751369Sdduvall 			    NVM_FLASH_CMD_WR, addr, dp);
16761369Sdduvall 			bge_nvmem_protect(bgep, B_TRUE);
16771369Sdduvall 			if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
16781369Sdduvall 			    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
16791369Sdduvall 				bge_reg_clr32(bgep, NVM_ACCESS_REG,
16801369Sdduvall 				    NVM_WRITE_ENABLE|NVM_ACCESS_ENABLE);
16811369Sdduvall 			}
16821369Sdduvall 
16831369Sdduvall 			break;
16841369Sdduvall 
16851369Sdduvall 		default:
16861369Sdduvall 			_NOTE(NOTREACHED)
16871369Sdduvall 			break;
16881369Sdduvall 		}
16891369Sdduvall 		bge_nvmem_relinquish(bgep);
16901369Sdduvall 	}
16911369Sdduvall 
16921369Sdduvall 	BGE_DEBUG(("bge_nvmem_rw32: err %d", err));
16931369Sdduvall 	return (err);
16941369Sdduvall }
16951369Sdduvall 
16961369Sdduvall /*
16971369Sdduvall  * Attempt to get a MAC address from the SEEPROM or Flash, if any
16981369Sdduvall  */
16991369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17001369Sdduvall #pragma no_inline(bge_get_nvmac)
17011369Sdduvall 
17021369Sdduvall static uint64_t
17031369Sdduvall bge_get_nvmac(bge_t *bgep)
17041369Sdduvall {
17051369Sdduvall 	uint32_t mac_high;
17061369Sdduvall 	uint32_t mac_low;
17071369Sdduvall 	uint32_t addr;
17081369Sdduvall 	uint32_t cmd;
17091369Sdduvall 	uint64_t mac;
17101369Sdduvall 
17111369Sdduvall 	BGE_TRACE(("bge_get_nvmac($%p)",
17121369Sdduvall 		(void *)bgep));
17131369Sdduvall 
17141369Sdduvall 	switch (bgep->chipid.nvtype) {
17151369Sdduvall 	case BGE_NVTYPE_NONE:
17161369Sdduvall 	case BGE_NVTYPE_UNKNOWN:
17171369Sdduvall 	default:
17181369Sdduvall 		return (0ULL);
17191369Sdduvall 
17201369Sdduvall 	case BGE_NVTYPE_SEEPROM:
17211369Sdduvall 	case BGE_NVTYPE_LEGACY_SEEPROM:
17221369Sdduvall 		cmd = BGE_SEE_READ;
17231369Sdduvall 		break;
17241369Sdduvall 
17251369Sdduvall 	case BGE_NVTYPE_UNBUFFERED_FLASH:
17261369Sdduvall 	case BGE_NVTYPE_BUFFERED_FLASH:
17271369Sdduvall 		cmd = BGE_FLASH_READ;
17281369Sdduvall 		break;
17291369Sdduvall 	}
17301369Sdduvall 
17311369Sdduvall 	addr = NVMEM_DATA_MAC_ADDRESS;
17321369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_high))
17331369Sdduvall 		return (0ULL);
17341369Sdduvall 	addr += 4;
17351369Sdduvall 	if (bge_nvmem_rw32(bgep, cmd, addr, &mac_low))
17361369Sdduvall 		return (0ULL);
17371369Sdduvall 
17381369Sdduvall 	/*
17391369Sdduvall 	 * The Broadcom chip is natively BIG-endian, so that's how the
17401369Sdduvall 	 * MAC address is represented in NVmem.  We may need to swap it
17411369Sdduvall 	 * around on a little-endian host ...
17421369Sdduvall 	 */
17431369Sdduvall #ifdef	_BIG_ENDIAN
17441369Sdduvall 	mac = mac_high;
17451369Sdduvall 	mac = mac << 32;
17461369Sdduvall 	mac |= mac_low;
17471369Sdduvall #else
17481369Sdduvall 	mac = BGE_BSWAP_32(mac_high);
17491369Sdduvall 	mac = mac << 32;
17501369Sdduvall 	mac |= BGE_BSWAP_32(mac_low);
17511369Sdduvall #endif	/* _BIG_ENDIAN */
17521369Sdduvall 
17531369Sdduvall 	return (mac);
17541369Sdduvall }
17551369Sdduvall 
17561369Sdduvall #else	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17571369Sdduvall 
17581369Sdduvall /*
17591369Sdduvall  * Dummy version for when we're not supporting NVmem access
17601369Sdduvall  */
17611369Sdduvall static uint64_t bge_get_nvmac(bge_t *bgep);
17621369Sdduvall #pragma inline(bge_get_nvmac)
17631369Sdduvall 
17641369Sdduvall static uint64_t
17651369Sdduvall bge_get_nvmac(bge_t *bgep)
17661369Sdduvall {
17671369Sdduvall 	_NOTE(ARGUNUSED(bgep))
17681369Sdduvall 	return (0ULL);
17691369Sdduvall }
17701369Sdduvall 
17711369Sdduvall #endif	/* BGE_SEE_IO32 || BGE_FLASH_IO32 */
17721369Sdduvall 
17731369Sdduvall /*
17741369Sdduvall  * Determine the type of NVmem that is (or may be) attached to this chip,
17751369Sdduvall  */
17761369Sdduvall static enum bge_nvmem_type bge_nvmem_id(bge_t *bgep);
17771369Sdduvall #pragma no_inline(bge_nvmem_id)
17781369Sdduvall 
17791369Sdduvall static enum bge_nvmem_type
17801369Sdduvall bge_nvmem_id(bge_t *bgep)
17811369Sdduvall {
17821369Sdduvall 	enum bge_nvmem_type nvtype;
17831369Sdduvall 	uint32_t config1;
17841369Sdduvall 
17851369Sdduvall 	BGE_TRACE(("bge_nvmem_id($%p)",
17861369Sdduvall 		(void *)bgep));
17871369Sdduvall 
17881369Sdduvall 	switch (bgep->chipid.device) {
17891369Sdduvall 	default:
17901369Sdduvall 		/*
17911369Sdduvall 		 * We shouldn't get here; it means we don't recognise
17921369Sdduvall 		 * the chip, which means we don't know how to determine
17931369Sdduvall 		 * what sort of NVmem (if any) it has.  So we'll say
17941369Sdduvall 		 * NONE, to disable the NVmem access code ...
17951369Sdduvall 		 */
17961369Sdduvall 		nvtype = BGE_NVTYPE_NONE;
17971369Sdduvall 		break;
17981369Sdduvall 
17991369Sdduvall 	case DEVICE_ID_5700:
18001369Sdduvall 	case DEVICE_ID_5700x:
18011369Sdduvall 	case DEVICE_ID_5701:
18021369Sdduvall 		/*
18031369Sdduvall 		 * These devices support *only* SEEPROMs
18041369Sdduvall 		 */
18051369Sdduvall 		nvtype = BGE_NVTYPE_SEEPROM;
18061369Sdduvall 		break;
18071369Sdduvall 
18081369Sdduvall 	case DEVICE_ID_5702:
18091369Sdduvall 	case DEVICE_ID_5702fe:
18101369Sdduvall 	case DEVICE_ID_5703C:
18111369Sdduvall 	case DEVICE_ID_5703S:
18121369Sdduvall 	case DEVICE_ID_5704C:
18131369Sdduvall 	case DEVICE_ID_5704S:
18141369Sdduvall 	case DEVICE_ID_5704:
18151369Sdduvall 	case DEVICE_ID_5705M:
18161369Sdduvall 	case DEVICE_ID_5705C:
18171369Sdduvall 	case DEVICE_ID_5706:
18181369Sdduvall 	case DEVICE_ID_5782:
18191369Sdduvall 	case DEVICE_ID_5788:
18201369Sdduvall 	case DEVICE_ID_5751:
18211369Sdduvall 	case DEVICE_ID_5751M:
18221369Sdduvall 	case DEVICE_ID_5721:
18231369Sdduvall 	case DEVICE_ID_5714C:
18241369Sdduvall 	case DEVICE_ID_5714S:
18251369Sdduvall 	case DEVICE_ID_5715C:
18261369Sdduvall 		config1 = bge_reg_get32(bgep, NVM_CONFIG1_REG);
18271369Sdduvall 		if (config1 & NVM_CFG1_FLASH_MODE)
18281369Sdduvall 			if (config1 & NVM_CFG1_BUFFERED_MODE)
18291369Sdduvall 				nvtype = BGE_NVTYPE_BUFFERED_FLASH;
18301369Sdduvall 			else
18311369Sdduvall 				nvtype = BGE_NVTYPE_UNBUFFERED_FLASH;
18321369Sdduvall 		else
18331369Sdduvall 			nvtype = BGE_NVTYPE_LEGACY_SEEPROM;
18341369Sdduvall 		break;
18351369Sdduvall 	}
18361369Sdduvall 
18371369Sdduvall 	return (nvtype);
18381369Sdduvall }
18391369Sdduvall 
18401369Sdduvall #undef	BGE_DBG
18411369Sdduvall #define	BGE_DBG		BGE_DBG_CHIP	/* debug flag for this code	*/
18421369Sdduvall 
18431369Sdduvall static void
18441369Sdduvall bge_init_recv_rule(bge_t *bgep)
18451369Sdduvall {
18461369Sdduvall 	bge_recv_rule_t *rulep;
18471369Sdduvall 	uint32_t i;
18481369Sdduvall 
18491369Sdduvall 	/*
18501369Sdduvall 	 * receive rule: direct all TCP traffic to ring RULE_MATCH_TO_RING
18511369Sdduvall 	 * 1. to direct UDP traffic, set:
18521369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18531369Sdduvall 	 * 	rulep->mask_value = RULE_UDP_MASK_VALUE;
18541369Sdduvall 	 * 2. to direct ICMP traffic, set:
18551369Sdduvall 	 * 	rulep->control = RULE_PROTO_CONTROL;
18561369Sdduvall 	 * 	rulep->mask_value = RULE_ICMP_MASK_VALUE;
18571369Sdduvall 	 * 3. to direct traffic by source ip, set:
18581369Sdduvall 	 * 	rulep->control = RULE_SIP_CONTROL;
18591369Sdduvall 	 * 	rulep->mask_value = RULE_SIP_MASK_VALUE;
18601369Sdduvall 	 */
18611369Sdduvall 	rulep = bgep->recv_rules;
18621369Sdduvall 	rulep->control = RULE_PROTO_CONTROL;
18631369Sdduvall 	rulep->mask_value = RULE_TCP_MASK_VALUE;
18641369Sdduvall 
18651369Sdduvall 	/*
18661369Sdduvall 	 * set receive rule registers
18671369Sdduvall 	 */
18681369Sdduvall 	rulep = bgep->recv_rules;
18691369Sdduvall 	for (i = 0; i < RECV_RULES_NUM_MAX; i++, rulep++) {
18701369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_MASK_REG(i), rulep->mask_value);
18711369Sdduvall 		bge_reg_put32(bgep, RECV_RULE_CONTROL_REG(i), rulep->control);
18721369Sdduvall 	}
18731369Sdduvall }
18741369Sdduvall 
18751369Sdduvall /*
18761369Sdduvall  * Using the values captured by bge_chip_cfg_init(), and additional probes
18771369Sdduvall  * as required, characterise the chip fully: determine the label by which
18781369Sdduvall  * to refer to this chip, the correct settings for various registers, and
18791369Sdduvall  * of course whether the device and/or subsystem are supported!
18801369Sdduvall  */
18811369Sdduvall void bge_chip_id_init(bge_t *bgep);
18821369Sdduvall #pragma	no_inline(bge_chip_id_init)
18831369Sdduvall 
18841369Sdduvall void
18851369Sdduvall bge_chip_id_init(bge_t *bgep)
18861369Sdduvall {
18871369Sdduvall 	char buf[MAXPATHLEN];		/* any risk of stack overflow?	*/
18881369Sdduvall 	boolean_t sys_ok;
18891369Sdduvall 	boolean_t dev_ok;
18901369Sdduvall 	chip_id_t *cidp;
18911369Sdduvall 	uint32_t subid;
18921369Sdduvall 	char *devname;
18931369Sdduvall 	char *sysname;
18941369Sdduvall 	int *ids;
18951369Sdduvall 	int err;
18961369Sdduvall 	uint_t i;
18971369Sdduvall 
18981369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_INITIAL);
18991369Sdduvall 
19001369Sdduvall 	sys_ok = dev_ok = B_FALSE;
19011369Sdduvall 	cidp = &bgep->chipid;
19021369Sdduvall 
19031369Sdduvall 	/*
19041369Sdduvall 	 * Check the PCI device ID to determine the generic chip type and
19051369Sdduvall 	 * select parameters that depend on this.
19061369Sdduvall 	 *
19071369Sdduvall 	 * Note: because the SPARC platforms in general don't fit the
19081369Sdduvall 	 * SEEPROM 'behind' the chip, the PCI revision ID register reads
19091369Sdduvall 	 * as zero - which is why we use <asic_rev> rather than <revision>
19101369Sdduvall 	 * below ...
19111369Sdduvall 	 *
19121369Sdduvall 	 * Note: in general we can't distinguish between the Copper/SerDes
19131369Sdduvall 	 * versions by ID alone, as some Copper devices (e.g. some but not
19141369Sdduvall 	 * all 5703Cs) have the same ID as the SerDes equivalents.  So we
19151369Sdduvall 	 * treat them the same here, and the MII code works out the media
19161369Sdduvall 	 * type later on ...
19171369Sdduvall 	 */
19181369Sdduvall 	cidp->mbuf_base = bge_mbuf_pool_base;
19191369Sdduvall 	cidp->mbuf_length = bge_mbuf_pool_len;
19201369Sdduvall 	cidp->recv_slots = BGE_RECV_SLOTS_USED;
19211369Sdduvall 	cidp->bge_dma_rwctrl = bge_dma_rwctrl;
19221369Sdduvall 	cidp->pci_type = BGE_PCI_X;
19231369Sdduvall 	cidp->statistic_type = BGE_STAT_BLK;
19241369Sdduvall 
19251369Sdduvall 	if (cidp->rx_rings == 0 || cidp->rx_rings > BGE_RECV_RINGS_MAX)
19261369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_DEFAULT;
19271369Sdduvall 	if (cidp->tx_rings == 0 || cidp->tx_rings > BGE_SEND_RINGS_MAX)
19281369Sdduvall 		cidp->tx_rings = BGE_SEND_RINGS_DEFAULT;
19291369Sdduvall 
19301369Sdduvall 	cidp->msi_enabled = B_FALSE;
19311369Sdduvall 
19321369Sdduvall 	switch (cidp->device) {
19331369Sdduvall 	case DEVICE_ID_5700:
19341369Sdduvall 	case DEVICE_ID_5700x:
19351369Sdduvall 		cidp->chip_label = 5700;
19361369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_CSUM;
19371369Sdduvall 		break;
19381369Sdduvall 
19391369Sdduvall 	case DEVICE_ID_5701:
19401369Sdduvall 		cidp->chip_label = 5701;
19411369Sdduvall 		dev_ok = B_TRUE;
19421369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_CSUM;
19431369Sdduvall 		break;
19441369Sdduvall 
19451369Sdduvall 	case DEVICE_ID_5702:
19461369Sdduvall 	case DEVICE_ID_5702fe:
19471369Sdduvall 		cidp->chip_label = 5702;
19481369Sdduvall 		dev_ok = B_TRUE;
19491369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_CSUM;	/* for now	*/
19501369Sdduvall 		break;
19511369Sdduvall 
19521369Sdduvall 	case DEVICE_ID_5703C:
19531369Sdduvall 	case DEVICE_ID_5703S:
19541369Sdduvall 	case DEVICE_ID_5703:
19551369Sdduvall 		/*
19561369Sdduvall 		 * Revision A0 of the 5703/5793 had various errata
19571369Sdduvall 		 * that we can't or don't work around, so it's not
19581369Sdduvall 		 * supported, but all later versions are
19591369Sdduvall 		 */
19601369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5793 : 5703;
19611369Sdduvall 		if (bgep->chipid.asic_rev != MHCR_CHIP_REV_5703_A0)
19621369Sdduvall 			dev_ok = B_TRUE;
19631369Sdduvall 		break;
19641369Sdduvall 
19651369Sdduvall 	case DEVICE_ID_5704C:
19661369Sdduvall 	case DEVICE_ID_5704S:
19671369Sdduvall 	case DEVICE_ID_5704:
19681369Sdduvall 		/*
19691369Sdduvall 		 * Revision A0 of the 5704/5794 had various errata
19701369Sdduvall 		 * but we have workarounds, so it *is* supported.
19711369Sdduvall 		 */
19721369Sdduvall 		cidp->chip_label = cidp->subven == VENDOR_ID_SUN ? 5794 : 5704;
19731369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5704;
19741369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5704;
19751369Sdduvall 		dev_ok = B_TRUE;
19761369Sdduvall 		break;
19771369Sdduvall 
19781369Sdduvall 	case DEVICE_ID_5705C:
19791369Sdduvall 	case DEVICE_ID_5705M:
19801369Sdduvall 	case DEVICE_ID_5705MA3:
19811369Sdduvall 	case DEVICE_ID_5705F:
19821369Sdduvall 		cidp->chip_label = 5705;
19831369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
19841369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
19851369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
19861369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
19871369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
19881369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
19891369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
19901369Sdduvall 		dev_ok = B_TRUE;
19911369Sdduvall 		break;
19921369Sdduvall 
19931369Sdduvall 	case DEVICE_ID_5706:
19941369Sdduvall 		cidp->chip_label = 5706;
19951369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
19961369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_CSUM;	/* for now	*/
19971369Sdduvall 		break;
19981369Sdduvall 
19991369Sdduvall 	case DEVICE_ID_5782:
20001369Sdduvall 		/*
20011369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20021369Sdduvall 		 */
20031369Sdduvall 		cidp->chip_label = 5782;
20041369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20051369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20061369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20071369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20081369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20091369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20101369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20111369Sdduvall 		dev_ok = B_TRUE;
20121369Sdduvall 		break;
20131369Sdduvall 
20141369Sdduvall 	case DEVICE_ID_5788:
20151369Sdduvall 		/*
20161369Sdduvall 		 * Apart from the label, we treat this as a 5705(?)
20171369Sdduvall 		 */
20181369Sdduvall 		cidp->chip_label = 5788;
20191369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5705;
20201369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5705;
20211369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5705;
20221369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20231369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20241369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20251369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20261369Sdduvall 		dev_ok = B_TRUE;
20271369Sdduvall 		break;
20281369Sdduvall 
20291369Sdduvall 	case DEVICE_ID_5714C:
20301369Sdduvall 		if (cidp->revision >= REVISION_ID_5714_A2)
20311369Sdduvall 			cidp->msi_enabled = bge_enable_msi;
20321369Sdduvall 		/* FALLTHRU */
20331369Sdduvall 	case DEVICE_ID_5714S:
20341369Sdduvall 		cidp->chip_label = 5714;
20351369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20361369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20371369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20381369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5714;
20391369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
20401369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20411369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20421369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20431369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20441369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20451369Sdduvall 		dev_ok = B_TRUE;
20461369Sdduvall 		break;
20471369Sdduvall 
20481369Sdduvall 	case DEVICE_ID_5715C:
20491369Sdduvall 		cidp->chip_label = 5715;
20501369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20511369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20521369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20531369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5715;
20541369Sdduvall 		cidp->bge_mlcr_default = bge_mlcr_default_5714;
20551369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20561369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20571369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20581369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20591369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20601369Sdduvall 		dev_ok = B_TRUE;
20611369Sdduvall 		break;
20621369Sdduvall 
20631369Sdduvall 	case DEVICE_ID_5721:
20641369Sdduvall 		cidp->chip_label = 5721;
20651369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20661369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20671369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20681369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
20691369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20701369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20711369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20721369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20731369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20741369Sdduvall 		dev_ok = B_TRUE;
20751369Sdduvall 		break;
20761369Sdduvall 
20771369Sdduvall 	case DEVICE_ID_5751:
20781369Sdduvall 	case DEVICE_ID_5751M:
20791369Sdduvall 		cidp->chip_label = 5751;
20801369Sdduvall 		cidp->mbuf_base = bge_mbuf_pool_base_5721;
20811369Sdduvall 		cidp->mbuf_length = bge_mbuf_pool_len_5721;
20821369Sdduvall 		cidp->recv_slots = BGE_RECV_SLOTS_5721;
20831369Sdduvall 		cidp->bge_dma_rwctrl = bge_dma_rwctrl_5721;
20841369Sdduvall 		cidp->rx_rings = BGE_RECV_RINGS_MAX_5705;
20851369Sdduvall 		cidp->tx_rings = BGE_RECV_RINGS_MAX_5705;
20861369Sdduvall 		cidp->pci_type = BGE_PCI_E;
20871369Sdduvall 		cidp->statistic_type = BGE_STAT_REG;
20881369Sdduvall 		cidp->flags |= CHIP_FLAG_NO_JUMBO;
20891369Sdduvall 		dev_ok = B_TRUE;
20901369Sdduvall 		break;
20911369Sdduvall 
20921369Sdduvall 	}
20931369Sdduvall 
20941369Sdduvall 	/*
20951369Sdduvall 	 * Setup the default jumbo parameter.
20961369Sdduvall 	 */
20971369Sdduvall 	cidp->mbuf_lo_water_rdma = bge_mbuf_lo_water_rdma;
20981369Sdduvall 	cidp->mbuf_lo_water_rmac = bge_mbuf_lo_water_rmac;
20991369Sdduvall 	cidp->mbuf_hi_water = bge_mbuf_hi_water;
21001369Sdduvall 	cidp->ethmax_size = ETHERMAX;
21011369Sdduvall 	cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_DEFAULT;
21021369Sdduvall 
21031369Sdduvall 	/*
21041369Sdduvall 	 * If jumbo is enabled and this kind of chipset supports jumbo feature,
21051369Sdduvall 	 * setup below jumbo specific parameters.
21061369Sdduvall 	 */
21071369Sdduvall 	if (bge_jumbo_enable &&
21081369Sdduvall 	    !(cidp->flags & CHIP_FLAG_NO_JUMBO) &&
21091369Sdduvall 	    (cidp->default_mtu > BGE_DEFAULT_MTU) &&
21101369Sdduvall 	    (cidp->default_mtu <= BGE_MAXIMUM_MTU)) {
21111369Sdduvall 		cidp->mbuf_lo_water_rdma = RDMA_MBUF_LOWAT_JUMBO;
21121369Sdduvall 		cidp->mbuf_lo_water_rmac = MAC_RX_MBUF_LOWAT_JUMBO;
21131369Sdduvall 		cidp->mbuf_hi_water = MBUF_HIWAT_JUMBO;
21141369Sdduvall 		cidp->recv_jumbo_size = BGE_JUMBO_BUFF_SIZE;
21151369Sdduvall 		cidp->snd_buff_size = BGE_SEND_BUFF_SIZE_JUMBO;
21161369Sdduvall 		cidp->jumbo_slots = BGE_JUMBO_SLOTS_USED;
21171369Sdduvall 		cidp->ethmax_size = cidp->default_mtu +
21181369Sdduvall 		    sizeof (struct ether_header);
21191369Sdduvall 	}
21201369Sdduvall 
21211369Sdduvall 	/*
21221369Sdduvall 	 * Identify the NV memory type: SEEPROM or Flash?
21231369Sdduvall 	 */
21241369Sdduvall 	cidp->nvtype = bge_nvmem_id(bgep);
21251369Sdduvall 
21261369Sdduvall 	/*
21271369Sdduvall 	 * Now, we want to check whether this device is part of a
21281369Sdduvall 	 * supported subsystem (e.g., on the motherboard of a Sun
21291369Sdduvall 	 * branded platform).
21301369Sdduvall 	 *
21311369Sdduvall 	 * Rule 1: If the Subsystem Vendor ID is "Sun", then it's OK ;-)
21321369Sdduvall 	 */
21331369Sdduvall 	if (cidp->subven == VENDOR_ID_SUN)
21341369Sdduvall 		sys_ok = B_TRUE;
21351369Sdduvall 
21361369Sdduvall 	/*
21371369Sdduvall 	 * Rule 2: If it's on the list on known subsystems, then it's OK.
21381369Sdduvall 	 * Note: 0x14e41647 should *not* appear in the list, but the code
21391369Sdduvall 	 * doesn't enforce that.
21401369Sdduvall 	 */
21411369Sdduvall 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
21421369Sdduvall 		DDI_PROP_DONTPASS, knownids_propname, &ids, &i);
21431369Sdduvall 	if (err == DDI_PROP_SUCCESS) {
21441369Sdduvall 		/*
21451369Sdduvall 		 * Got the list; scan for a matching subsystem vendor/device
21461369Sdduvall 		 */
21471369Sdduvall 		subid = (cidp->subven << 16) | cidp->subdev;
21481369Sdduvall 		while (i--)
21491369Sdduvall 			if (ids[i] == subid)
21501369Sdduvall 				sys_ok = B_TRUE;
21511369Sdduvall 		ddi_prop_free(ids);
21521369Sdduvall 	}
21531369Sdduvall 
21541369Sdduvall 	/*
21551369Sdduvall 	 * Rule 3: If it's a Taco/ENWS motherboard device, then it's OK
21561369Sdduvall 	 *
21571369Sdduvall 	 * Unfortunately, early SunBlade 1500s and 2500s didn't reprogram
21581369Sdduvall 	 * the Subsystem Vendor ID, so it defaults to Broadcom.  Therefore,
21591369Sdduvall 	 * we have to check specially for the exact device paths to the
21601369Sdduvall 	 * motherboard devices on those platforms ;-(
21611369Sdduvall 	 *
21621369Sdduvall 	 * Note: we can't just use the "supported-subsystems" mechanism
21631369Sdduvall 	 * above, because the entry would have to be 0x14e41647 -- which
21641369Sdduvall 	 * would then accept *any* plugin card that *didn't* contain a
21651369Sdduvall 	 * (valid) SEEPROM ;-(
21661369Sdduvall 	 */
21671369Sdduvall 	sysname = ddi_node_name(ddi_root_node());
21681369Sdduvall 	devname = ddi_pathname(bgep->devinfo, buf);
21691369Sdduvall 	ASSERT(strlen(devname) > 0);
21701369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-1500") == 0)	/* Taco */
21711369Sdduvall 		if (strcmp(devname, "/pci@1f,700000/network@2") == 0)
21721369Sdduvall 			sys_ok = B_TRUE;
21731369Sdduvall 	if (strcmp(sysname, "SUNW,Sun-Blade-2500") == 0)	/* ENWS */
21741369Sdduvall 		if (strcmp(devname, "/pci@1c,600000/network@3") == 0)
21751369Sdduvall 			sys_ok = B_TRUE;
21761369Sdduvall 
21771369Sdduvall 	/*
21781369Sdduvall 	 * Now check what we've discovered: is this truly a supported
21791369Sdduvall 	 * chip on (the motherboard of) a supported platform?
21801369Sdduvall 	 *
21811369Sdduvall 	 * Possible problems here:
21821369Sdduvall 	 * 1)	it's a completely unheard-of chip (e.g. 5761)
21831369Sdduvall 	 * 2)	it's a recognised but unsupported chip (e.g. 5701, 5703C-A0)
21841369Sdduvall 	 * 3)	it's a chip we would support if it were on the motherboard
21851369Sdduvall 	 *	of a Sun platform, but this one isn't ;-(
21861369Sdduvall 	 */
21871369Sdduvall 	if (cidp->chip_label == 0)
21881369Sdduvall 		bge_problem(bgep,
21891369Sdduvall 			"Device 'pci%04x,%04x' not recognized (%d?)",
21901369Sdduvall 			cidp->vendor, cidp->device, cidp->device);
21911369Sdduvall 	else if (!dev_ok)
21921369Sdduvall 		bge_problem(bgep,
21931369Sdduvall 			"Device 'pci%04x,%04x' (%d) revision %d not supported",
21941369Sdduvall 			cidp->vendor, cidp->device, cidp->chip_label,
21951369Sdduvall 			cidp->revision);
21961369Sdduvall #if	BGE_DEBUGGING
21971369Sdduvall 	else if (!sys_ok)
21981369Sdduvall 		bge_problem(bgep,
21991369Sdduvall 			"%d-based subsystem 'pci%04x,%04x' not validated",
22001369Sdduvall 			cidp->chip_label, cidp->subven, cidp->subdev);
22011369Sdduvall #endif
22021369Sdduvall 	else
22031369Sdduvall 		cidp->flags |= CHIP_FLAG_SUPPORTED;
22041369Sdduvall }
22051369Sdduvall 
22061369Sdduvall void
22071369Sdduvall bge_chip_msi_trig(bge_t *bgep)
22081369Sdduvall {
22091369Sdduvall 	uint32_t	regval;
22101369Sdduvall 
22111369Sdduvall 	regval = bgep->param_msi_cnt<<4;
22121369Sdduvall 	bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, regval);
22131369Sdduvall 	BGE_DEBUG(("bge_chip_msi_trig:data = %d", regval));
22141369Sdduvall }
22151369Sdduvall 
22161369Sdduvall /*
22171369Sdduvall  * Various registers that control the chip's internal engines (state
22181369Sdduvall  * machines) have a <reset> and <enable> bits (fortunately, in the
22191369Sdduvall  * same place in each such register :-).
22201369Sdduvall  *
22211369Sdduvall  * To reset the state machine, the <reset> bit must be written with 1;
22221369Sdduvall  * it will then read back as 1 while the reset is in progress, but
22231369Sdduvall  * self-clear to 0 when the reset completes.
22241369Sdduvall  *
22251369Sdduvall  * To enable a state machine, one must set the <enable> bit, which
22261369Sdduvall  * will continue to read back as 0 until the state machine is running.
22271369Sdduvall  *
22281369Sdduvall  * To disable a state machine, the <enable> bit must be cleared, but
22291369Sdduvall  * it will continue to read back as 1 until the state machine actually
22301369Sdduvall  * stops.
22311369Sdduvall  *
22321369Sdduvall  * This routine implements polling for completion of a reset, enable
22331369Sdduvall  * or disable operation, returning B_TRUE on success (bit reached the
22341369Sdduvall  * required state) or B_FALSE on timeout (200*100us == 20ms).
22351369Sdduvall  */
22361369Sdduvall static boolean_t bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
22371369Sdduvall 					uint32_t mask, uint32_t val);
22381369Sdduvall #pragma	no_inline(bge_chip_poll_engine)
22391369Sdduvall 
22401369Sdduvall static boolean_t
22411369Sdduvall bge_chip_poll_engine(bge_t *bgep, bge_regno_t regno,
22421369Sdduvall 	uint32_t mask, uint32_t val)
22431369Sdduvall {
22441369Sdduvall 	uint32_t regval;
22451369Sdduvall 	uint32_t n;
22461369Sdduvall 
22471369Sdduvall 	BGE_TRACE(("bge_chip_poll_engine($%p, 0x%lx, 0x%x, 0x%x)",
22481369Sdduvall 		(void *)bgep, regno, mask, val));
22491369Sdduvall 
22501369Sdduvall 	for (n = 200; n; --n) {
22511369Sdduvall 		regval = bge_reg_get32(bgep, regno);
22521369Sdduvall 		if ((regval & mask) == val)
22531369Sdduvall 			return (B_TRUE);
22541369Sdduvall 		drv_usecwait(100);
22551369Sdduvall 	}
22561369Sdduvall 
22571369Sdduvall 	return (B_FALSE);
22581369Sdduvall }
22591369Sdduvall 
22601369Sdduvall /*
22611369Sdduvall  * Various registers that control the chip's internal engines (state
22621369Sdduvall  * machines) have a <reset> bit (fortunately, in the same place in
22631369Sdduvall  * each such register :-).  To reset the state machine, this bit must
22641369Sdduvall  * be written with 1; it will then read back as 1 while the reset is
22651369Sdduvall  * in progress, but self-clear to 0 when the reset completes.
22661369Sdduvall  *
22671369Sdduvall  * This code sets the bit, then polls for it to read back as zero.
22681369Sdduvall  * The return value is B_TRUE on success (reset bit cleared itself),
22691369Sdduvall  * or B_FALSE if the state machine didn't recover :(
22701369Sdduvall  *
22711369Sdduvall  * NOTE: the Core reset is similar to other resets, except that we
22721369Sdduvall  * can't poll for completion, since the Core reset disables memory
22731369Sdduvall  * access!  So we just have to assume that it will all complete in
22741369Sdduvall  * 100us.  See Broadcom document 570X-PG102-R, p102, steps 4-5.
22751369Sdduvall  */
22761369Sdduvall static boolean_t bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno);
22771369Sdduvall #pragma	no_inline(bge_chip_reset_engine)
22781369Sdduvall 
22791369Sdduvall static boolean_t
22801369Sdduvall bge_chip_reset_engine(bge_t *bgep, bge_regno_t regno)
22811369Sdduvall {
22821369Sdduvall 	uint32_t regval;
22831369Sdduvall 	uint32_t val32;
22841369Sdduvall 
22851369Sdduvall 	regval = bge_reg_get32(bgep, regno);
22861369Sdduvall 
22871369Sdduvall 	BGE_TRACE(("bge_chip_reset_engine($%p, 0x%lx)",
22881369Sdduvall 		(void *)bgep, regno));
22891369Sdduvall 	BGE_DEBUG(("bge_chip_reset_engine: 0x%lx before reset = 0x%08x",
22901369Sdduvall 		regno, regval));
22911369Sdduvall 
22921369Sdduvall 	regval |= STATE_MACHINE_RESET_BIT;
22931369Sdduvall 
22941369Sdduvall 	switch (regno) {
22951369Sdduvall 	case MISC_CONFIG_REG:
22961369Sdduvall 		/*
22971369Sdduvall 		 * BCM5714/5721/5751 pcie chip special case. In order to avoid
22981369Sdduvall 		 * resetting PCIE block and bringing PCIE link down, bit 29
22991369Sdduvall 		 * in the register needs to be set first, and then set it again
23001369Sdduvall 		 * while the reset bit is written.
23011369Sdduvall 		 * See:P500 of 57xx-PG102-RDS.pdf.
23021369Sdduvall 		 */
23031369Sdduvall 		if (DEVICE_5705_SERIES_CHIPSETS(bgep)||
23041369Sdduvall 		    DEVICE_5721_SERIES_CHIPSETS(bgep)||
23051369Sdduvall 		    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
23061369Sdduvall 			regval |= MISC_CONFIG_GPHY_POWERDOWN_OVERRIDE;
23071369Sdduvall 			if (bgep->chipid.pci_type == BGE_PCI_E) {
23081369Sdduvall 				if (bgep->chipid.asic_rev ==
23091369Sdduvall 				    MHCR_CHIP_REV_5751_A0 ||
23101369Sdduvall 				    bgep->chipid.asic_rev ==
23111369Sdduvall 				    MHCR_CHIP_REV_5721_A0) {
23121369Sdduvall 					val32 = bge_reg_get32(bgep,
23131369Sdduvall 					    PHY_TEST_CTRL_REG);
23141369Sdduvall 					if (val32 == (PHY_PCIE_SCRAM_MODE |
23151369Sdduvall 					    PHY_PCIE_LTASS_MODE))
23161369Sdduvall 						bge_reg_put32(bgep,
23171369Sdduvall 						    PHY_TEST_CTRL_REG,
23181369Sdduvall 						    PHY_PCIE_SCRAM_MODE);
23191369Sdduvall 					val32 = pci_config_get32
23201369Sdduvall 					    (bgep->cfg_handle,
23211369Sdduvall 					    PCI_CONF_BGE_CLKCTL);
23221369Sdduvall 					val32 |= CLKCTL_PCIE_A0_FIX;
23231369Sdduvall 					pci_config_put32(bgep->cfg_handle,
23241369Sdduvall 					    PCI_CONF_BGE_CLKCTL, val32);
23251369Sdduvall 				}
23261369Sdduvall 				bge_reg_set32(bgep, regno,
23271369Sdduvall 					MISC_CONFIG_GRC_RESET_DISABLE);
23281369Sdduvall 				regval |= MISC_CONFIG_GRC_RESET_DISABLE;
23291369Sdduvall 			}
23301369Sdduvall 		}
23311369Sdduvall 
23321369Sdduvall 		/*
23331369Sdduvall 		 * Special case - causes Core reset
23341369Sdduvall 		 *
23351369Sdduvall 		 * On SPARC v9 we want to ensure that we don't start
23361369Sdduvall 		 * timing until the I/O access has actually reached
23371369Sdduvall 		 * the chip, otherwise we might make the next access
23381369Sdduvall 		 * too early.  And we can't just force the write out
23391369Sdduvall 		 * by following it with a read (even to config space)
23401369Sdduvall 		 * because that would cause the fault we're trying
23411369Sdduvall 		 * to avoid.  Hence the need for membar_sync() here.
23421369Sdduvall 		 */
23431369Sdduvall 		ddi_put32(bgep->io_handle, PIO_ADDR(bgep, regno), regval);
23441369Sdduvall #ifdef	__sparcv9
23451369Sdduvall 		membar_sync();
23461369Sdduvall #endif	/* __sparcv9 */
23471369Sdduvall 		/*
23481369Sdduvall 		 * On some platforms,system need about 300us for
23491369Sdduvall 		 * link setup.
23501369Sdduvall 		 */
23511369Sdduvall 		drv_usecwait(300);
23521369Sdduvall 
23531369Sdduvall 		if (bgep->chipid.pci_type == BGE_PCI_E) {
23541369Sdduvall 			/* PCI-E device need more reset time */
23551369Sdduvall 			drv_usecwait(120000);
23561369Sdduvall 
23571369Sdduvall 			/* Set PCIE max payload size and clear error status. */
23581369Sdduvall 			if (bgep->chipid.chip_label == 5721 ||
23591369Sdduvall 			    bgep->chipid.chip_label == 5751) {
23601369Sdduvall 				pci_config_put16(bgep->cfg_handle,
23611369Sdduvall 					PCI_CONF_DEV_CTRL, READ_REQ_SIZE_MAX);
23621369Sdduvall 				pci_config_put16(bgep->cfg_handle,
23631369Sdduvall 					PCI_CONF_DEV_STUS, DEVICE_ERROR_STUS);
23641369Sdduvall 			}
23651369Sdduvall 		}
23661369Sdduvall 
23671369Sdduvall 		BGE_PCICHK(bgep);
23681369Sdduvall 		return (B_TRUE);
23691369Sdduvall 
23701369Sdduvall 	default:
23711369Sdduvall 		bge_reg_put32(bgep, regno, regval);
23721369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
23731369Sdduvall 			STATE_MACHINE_RESET_BIT, 0));
23741369Sdduvall 	}
23751369Sdduvall }
23761369Sdduvall 
23771369Sdduvall /*
23781369Sdduvall  * Various registers that control the chip's internal engines (state
23791369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
23801369Sdduvall  * each such register :-).  To stop the state machine, this bit must
23811369Sdduvall  * be written with 0, then polled to see when the state machine has
23821369Sdduvall  * actually stopped.
23831369Sdduvall  *
23841369Sdduvall  * The return value is B_TRUE on success (enable bit cleared), or
23851369Sdduvall  * B_FALSE if the state machine didn't stop :(
23861369Sdduvall  */
23871369Sdduvall static boolean_t bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno,
23881369Sdduvall 						uint32_t morebits);
23891369Sdduvall #pragma	no_inline(bge_chip_disable_engine)
23901369Sdduvall 
23911369Sdduvall static boolean_t
23921369Sdduvall bge_chip_disable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
23931369Sdduvall {
23941369Sdduvall 	uint32_t regval;
23951369Sdduvall 
23961369Sdduvall 	BGE_TRACE(("bge_chip_disable_engine($%p, 0x%lx, 0x%x)",
23971369Sdduvall 		(void *)bgep, regno, morebits));
23981369Sdduvall 
23991369Sdduvall 	switch (regno) {
24001369Sdduvall 	case FTQ_RESET_REG:
24011369Sdduvall 		/*
24021369Sdduvall 		 * Not quite like the others; it doesn't
24031369Sdduvall 		 * have an <enable> bit, but instead we
24041369Sdduvall 		 * have to set and then clear all the bits
24051369Sdduvall 		 */
24061369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
24071369Sdduvall 		drv_usecwait(100);
24081369Sdduvall 		bge_reg_put32(bgep, regno, 0);
24091369Sdduvall 		return (B_TRUE);
24101369Sdduvall 
24111369Sdduvall 	default:
24121369Sdduvall 		regval = bge_reg_get32(bgep, regno);
24131369Sdduvall 		regval &= ~STATE_MACHINE_ENABLE_BIT;
24141369Sdduvall 		regval &= ~morebits;
24151369Sdduvall 		bge_reg_put32(bgep, regno, regval);
24161369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
24171369Sdduvall 			STATE_MACHINE_ENABLE_BIT, 0));
24181369Sdduvall 	}
24191369Sdduvall }
24201369Sdduvall 
24211369Sdduvall /*
24221369Sdduvall  * Various registers that control the chip's internal engines (state
24231369Sdduvall  * machines) have an <enable> bit (fortunately, in the same place in
24241369Sdduvall  * each such register :-).  To start the state machine, this bit must
24251369Sdduvall  * be written with 1, then polled to see when the state machine has
24261369Sdduvall  * actually started.
24271369Sdduvall  *
24281369Sdduvall  * The return value is B_TRUE on success (enable bit set), or
24291369Sdduvall  * B_FALSE if the state machine didn't start :(
24301369Sdduvall  */
24311369Sdduvall static boolean_t bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno,
24321369Sdduvall 					uint32_t morebits);
24331369Sdduvall #pragma	no_inline(bge_chip_enable_engine)
24341369Sdduvall 
24351369Sdduvall static boolean_t
24361369Sdduvall bge_chip_enable_engine(bge_t *bgep, bge_regno_t regno, uint32_t morebits)
24371369Sdduvall {
24381369Sdduvall 	uint32_t regval;
24391369Sdduvall 
24401369Sdduvall 	BGE_TRACE(("bge_chip_enable_engine($%p, 0x%lx, 0x%x)",
24411369Sdduvall 		(void *)bgep, regno, morebits));
24421369Sdduvall 
24431369Sdduvall 	switch (regno) {
24441369Sdduvall 	case FTQ_RESET_REG:
24451369Sdduvall 		/*
24461369Sdduvall 		 * Not quite like the others; it doesn't
24471369Sdduvall 		 * have an <enable> bit, but instead we
24481369Sdduvall 		 * have to set and then clear all the bits
24491369Sdduvall 		 */
24501369Sdduvall 		bge_reg_put32(bgep, regno, ~(uint32_t)0);
24511369Sdduvall 		drv_usecwait(100);
24521369Sdduvall 		bge_reg_put32(bgep, regno, 0);
24531369Sdduvall 		return (B_TRUE);
24541369Sdduvall 
24551369Sdduvall 	default:
24561369Sdduvall 		regval = bge_reg_get32(bgep, regno);
24571369Sdduvall 		regval |= STATE_MACHINE_ENABLE_BIT;
24581369Sdduvall 		regval |= morebits;
24591369Sdduvall 		bge_reg_put32(bgep, regno, regval);
24601369Sdduvall 		return (bge_chip_poll_engine(bgep, regno,
24611369Sdduvall 			STATE_MACHINE_ENABLE_BIT, STATE_MACHINE_ENABLE_BIT));
24621369Sdduvall 	}
24631369Sdduvall }
24641369Sdduvall 
24651369Sdduvall /*
24661369Sdduvall  * Reprogram the Ethernet, Transmit, and Receive MAC
24671369Sdduvall  * modes to match the param_* variables
24681369Sdduvall  */
24691369Sdduvall static void bge_sync_mac_modes(bge_t *bgep);
24701369Sdduvall #pragma	no_inline(bge_sync_mac_modes)
24711369Sdduvall 
24721369Sdduvall static void
24731369Sdduvall bge_sync_mac_modes(bge_t *bgep)
24741369Sdduvall {
24751369Sdduvall 	uint32_t macmode;
24761369Sdduvall 	uint32_t regval;
24771369Sdduvall 
24781369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
24791369Sdduvall 
24801369Sdduvall 	/*
24811369Sdduvall 	 * Reprogram the Ethernet MAC mode ...
24821369Sdduvall 	 */
24831369Sdduvall 	macmode = regval = bge_reg_get32(bgep, ETHERNET_MAC_MODE_REG);
24841369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
24851369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
24861369Sdduvall 		macmode &= ~ETHERNET_MODE_LINK_POLARITY;
24871369Sdduvall 	else
24881369Sdduvall 		macmode |= ETHERNET_MODE_LINK_POLARITY;
24891369Sdduvall 	macmode &= ~ETHERNET_MODE_PORTMODE_MASK;
24901369Sdduvall 	if ((bgep->chipid.flags & CHIP_FLAG_SERDES) &&
24911369Sdduvall 		(bgep->param_loop_mode != BGE_LOOP_INTERNAL_MAC))
24921369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_TBI;
24931369Sdduvall 	else if (bgep->param_link_speed == 10 || bgep->param_link_speed == 100)
24941369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_MII;
24951369Sdduvall 	else
24961369Sdduvall 		macmode |= ETHERNET_MODE_PORTMODE_GMII;
24971369Sdduvall 	if (bgep->param_link_duplex == LINK_DUPLEX_HALF)
24981369Sdduvall 		macmode |= ETHERNET_MODE_HALF_DUPLEX;
24991369Sdduvall 	else
25001369Sdduvall 		macmode &= ~ETHERNET_MODE_HALF_DUPLEX;
25011369Sdduvall 	if (bgep->param_loop_mode == BGE_LOOP_INTERNAL_MAC)
25021369Sdduvall 		macmode |= ETHERNET_MODE_MAC_LOOPBACK;
25031369Sdduvall 	else
25041369Sdduvall 		macmode &= ~ETHERNET_MODE_MAC_LOOPBACK;
25051369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, macmode);
25061369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Ethernet MAC mode 0x%x => 0x%x",
25071369Sdduvall 		(void *)bgep, regval, macmode));
25081369Sdduvall 
25091369Sdduvall 	/*
25101369Sdduvall 	 * ... the Transmit MAC mode ...
25111369Sdduvall 	 */
25121369Sdduvall 	macmode = regval = bge_reg_get32(bgep, TRANSMIT_MAC_MODE_REG);
25131369Sdduvall 	if (bgep->param_link_tx_pause)
25141369Sdduvall 		macmode |= TRANSMIT_MODE_FLOW_CONTROL;
25151369Sdduvall 	else
25161369Sdduvall 		macmode &= ~TRANSMIT_MODE_FLOW_CONTROL;
25171369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_MODE_REG, macmode);
25181369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Transmit MAC mode 0x%x => 0x%x",
25191369Sdduvall 		(void *)bgep, regval, macmode));
25201369Sdduvall 
25211369Sdduvall 	/*
25221369Sdduvall 	 * ... and the Receive MAC mode
25231369Sdduvall 	 */
25241369Sdduvall 	macmode = regval = bge_reg_get32(bgep, RECEIVE_MAC_MODE_REG);
25251369Sdduvall 	if (bgep->param_link_rx_pause)
25261369Sdduvall 		macmode |= RECEIVE_MODE_FLOW_CONTROL;
25271369Sdduvall 	else
25281369Sdduvall 		macmode &= ~RECEIVE_MODE_FLOW_CONTROL;
25291369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_MODE_REG, macmode);
25301369Sdduvall 	BGE_DEBUG(("bge_sync_mac_modes($%p) Receive MAC mode 0x%x => 0x%x",
25311369Sdduvall 		(void *)bgep, regval, macmode));
25321369Sdduvall }
25331369Sdduvall 
25341369Sdduvall /*
25351369Sdduvall  * bge_chip_sync() -- program the chip with the unicast MAC address,
25361369Sdduvall  * the multicast hash table, the required level of promiscuity, and
25371369Sdduvall  * the current loopback mode ...
25381369Sdduvall  */
25391408Srandyf #ifdef BGE_IPMI_ASF
25401408Srandyf void bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive);
25411408Srandyf #else
25421369Sdduvall void bge_chip_sync(bge_t *bgep);
25431408Srandyf #endif
25441369Sdduvall #pragma	no_inline(bge_chip_sync)
25451369Sdduvall 
25461369Sdduvall void
25471408Srandyf #ifdef BGE_IPMI_ASF
25481408Srandyf bge_chip_sync(bge_t *bgep, boolean_t asf_keeplive)
25491408Srandyf #else
25501369Sdduvall bge_chip_sync(bge_t *bgep)
25511408Srandyf #endif
25521369Sdduvall {
25531369Sdduvall 	void (*opfn)(bge_t *bgep, bge_regno_t reg, uint32_t bits);
25541369Sdduvall 	boolean_t promisc;
25551369Sdduvall 	uint64_t macaddr;
25561369Sdduvall 	uint32_t fill;
25571369Sdduvall 	int i;
25581369Sdduvall 
25591369Sdduvall 	BGE_TRACE(("bge_chip_sync($%p)",
25601369Sdduvall 		(void *)bgep));
25611369Sdduvall 
25621369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
25631369Sdduvall 
25641369Sdduvall 	promisc = B_FALSE;
25651369Sdduvall 	fill = ~(uint32_t)0;
25661369Sdduvall 
25671369Sdduvall 	if (bgep->promisc)
25681369Sdduvall 		promisc = B_TRUE;
25691369Sdduvall 	else
25701369Sdduvall 		fill = (uint32_t)0;
25711369Sdduvall 
25721369Sdduvall 	/*
25731369Sdduvall 	 * If the TX/RX MAC engines are already running, we should stop
25741369Sdduvall 	 * them (and reset the RX engine) before changing the parameters.
25751369Sdduvall 	 * If they're not running, this will have no effect ...
25761369Sdduvall 	 *
25771369Sdduvall 	 * NOTE: this is currently disabled by default because stopping
25781369Sdduvall 	 * and restarting the Tx engine may cause an outgoing packet in
25791369Sdduvall 	 * transit to be truncated.  Also, stopping and restarting the
25801369Sdduvall 	 * Rx engine seems to not work correctly on the 5705.  Testing
25811369Sdduvall 	 * has not (yet!) revealed any problems with NOT stopping and
25821369Sdduvall 	 * restarting these engines (and Broadcom say their drivers don't
25831369Sdduvall 	 * do this), but if it is found to cause problems, this variable
25841369Sdduvall 	 * can be patched to re-enable the old behaviour ...
25851369Sdduvall 	 */
25861369Sdduvall 	if (bge_stop_start_on_sync) {
25871408Srandyf #ifdef BGE_IPMI_ASF
25881408Srandyf 		if (bgep->asf_enabled) {
25891408Srandyf 			(void) bge_chip_disable_engine(bgep,
25901408Srandyf 			    RECEIVE_MAC_MODE_REG, 0);
25911408Srandyf 		} else {
25921408Srandyf 			(void) bge_chip_disable_engine(bgep,
25931408Srandyf 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG);
25941408Srandyf 		}
25951408Srandyf #else
25961369Sdduvall 		(void) bge_chip_disable_engine(bgep, RECEIVE_MAC_MODE_REG,
25971369Sdduvall 		    RECEIVE_MODE_KEEP_VLAN_TAG);
25981408Srandyf #endif
25991369Sdduvall 		(void) bge_chip_disable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0);
26001369Sdduvall 		(void) bge_chip_reset_engine(bgep, RECEIVE_MAC_MODE_REG);
26011369Sdduvall 	}
26021369Sdduvall 
26031369Sdduvall 	/*
26041369Sdduvall 	 * Reprogram the hashed multicast address table ...
26051369Sdduvall 	 */
26061369Sdduvall 	for (i = 0; i < BGE_HASH_TABLE_SIZE/32; ++i)
26071369Sdduvall 		bge_reg_put32(bgep, MAC_HASH_REG(i),
26081369Sdduvall 			bgep->mcast_hash[i] | fill);
26091369Sdduvall 
26101408Srandyf #ifdef BGE_IPMI_ASF
26111408Srandyf 	if (!bgep->asf_enabled || !asf_keeplive) {
26121408Srandyf #endif
26131408Srandyf 		/*
26141408Srandyf 		 * Transform the MAC address from host to chip format, then
26151408Srandyf 		 * reprogram the transmit random backoff seed and the unicast
26161408Srandyf 		 * MAC address(es) ...
26171408Srandyf 		 */
26181408Srandyf 		for (i = 0, fill = 0, macaddr = 0ull; i < ETHERADDRL; ++i) {
26191408Srandyf 			macaddr <<= 8;
26201408Srandyf 			macaddr |= bgep->curr_addr.addr[i];
26211408Srandyf 			fill += bgep->curr_addr.addr[i];
26221408Srandyf 		}
26231408Srandyf 		bge_reg_put32(bgep, MAC_TX_RANDOM_BACKOFF_REG, fill);
26241408Srandyf 		for (i = 0; i < MAC_ADDRESS_REGS_MAX; ++i)
26251408Srandyf 			bge_reg_put64(bgep, MAC_ADDRESS_REG(i), macaddr);
26261408Srandyf 
26271408Srandyf 		BGE_DEBUG(("bge_chip_sync($%p) setting MAC address %012llx",
26281408Srandyf 			(void *)bgep, macaddr));
26291408Srandyf #ifdef BGE_IPMI_ASF
26301369Sdduvall 	}
26311408Srandyf #endif
26321369Sdduvall 
26331369Sdduvall 	/*
26341369Sdduvall 	 * Set or clear the PROMISCUOUS mode bit
26351369Sdduvall 	 */
26361369Sdduvall 	opfn = promisc ? bge_reg_set32 : bge_reg_clr32;
26371369Sdduvall 	(*opfn)(bgep, RECEIVE_MAC_MODE_REG, RECEIVE_MODE_PROMISCUOUS);
26381369Sdduvall 
26391369Sdduvall 	/*
26401369Sdduvall 	 * Sync the rest of the MAC modes too ...
26411369Sdduvall 	 */
26421369Sdduvall 	bge_sync_mac_modes(bgep);
26431369Sdduvall 
26441369Sdduvall 	/*
26451369Sdduvall 	 * Restart RX/TX MAC engines if required ...
26461369Sdduvall 	 */
26471369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_RUNNING) {
26481369Sdduvall 		(void) bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0);
26491408Srandyf #ifdef BGE_IPMI_ASF
26501408Srandyf 		if (bgep->asf_enabled) {
26511408Srandyf 			(void) bge_chip_enable_engine(bgep,
26521408Srandyf 			    RECEIVE_MAC_MODE_REG, 0);
26531408Srandyf 		} else {
26541408Srandyf 			(void) bge_chip_enable_engine(bgep,
26551408Srandyf 			    RECEIVE_MAC_MODE_REG, RECEIVE_MODE_KEEP_VLAN_TAG);
26561408Srandyf 		}
26571408Srandyf #else
26581369Sdduvall 		(void) bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
26591369Sdduvall 		    RECEIVE_MODE_KEEP_VLAN_TAG);
26601408Srandyf #endif
26611369Sdduvall 	}
26621369Sdduvall }
26631369Sdduvall 
26641369Sdduvall /*
26651369Sdduvall  * This array defines the sequence of state machine control registers
26661369Sdduvall  * in which the <enable> bit must be cleared to bring the chip to a
26671369Sdduvall  * clean stop.  Taken from Broadcom document 570X-PG102-R, p116.
26681369Sdduvall  */
26691369Sdduvall static bge_regno_t shutdown_engine_regs[] = {
26701369Sdduvall 	RECEIVE_MAC_MODE_REG,
26711369Sdduvall 	RCV_BD_INITIATOR_MODE_REG,
26721369Sdduvall 	RCV_LIST_PLACEMENT_MODE_REG,
26731369Sdduvall 	RCV_LIST_SELECTOR_MODE_REG,		/* BCM5704 series only	*/
26741369Sdduvall 	RCV_DATA_BD_INITIATOR_MODE_REG,
26751369Sdduvall 	RCV_DATA_COMPLETION_MODE_REG,
26761369Sdduvall 	RCV_BD_COMPLETION_MODE_REG,
26771369Sdduvall 
26781369Sdduvall 	SEND_BD_SELECTOR_MODE_REG,
26791369Sdduvall 	SEND_BD_INITIATOR_MODE_REG,
26801369Sdduvall 	SEND_DATA_INITIATOR_MODE_REG,
26811369Sdduvall 	READ_DMA_MODE_REG,
26821369Sdduvall 	SEND_DATA_COMPLETION_MODE_REG,
26831369Sdduvall 	DMA_COMPLETION_MODE_REG,		/* BCM5704 series only	*/
26841369Sdduvall 	SEND_BD_COMPLETION_MODE_REG,
26851369Sdduvall 	TRANSMIT_MAC_MODE_REG,
26861369Sdduvall 
26871369Sdduvall 	HOST_COALESCE_MODE_REG,
26881369Sdduvall 	WRITE_DMA_MODE_REG,
26891369Sdduvall 	MBUF_CLUSTER_FREE_MODE_REG,		/* BCM5704 series only	*/
26901369Sdduvall 	FTQ_RESET_REG,		/* special - see code	*/
26911369Sdduvall 	BUFFER_MANAGER_MODE_REG,		/* BCM5704 series only	*/
26921369Sdduvall 	MEMORY_ARBITER_MODE_REG,		/* BCM5704 series only	*/
26931369Sdduvall 	BGE_REGNO_NONE		/* terminator		*/
26941369Sdduvall };
26951369Sdduvall 
26961369Sdduvall /*
26971369Sdduvall  * bge_chip_stop() -- stop all chip processing
26981369Sdduvall  *
26991369Sdduvall  * If the <fault> parameter is B_TRUE, we're stopping the chip because
27001369Sdduvall  * we've detected a problem internally; otherwise, this is a normal
27011369Sdduvall  * (clean) stop (at user request i.e. the last STREAM has been closed).
27021369Sdduvall  */
27031369Sdduvall void bge_chip_stop(bge_t *bgep, boolean_t fault);
27041369Sdduvall #pragma	no_inline(bge_chip_stop)
27051369Sdduvall 
27061369Sdduvall void
27071369Sdduvall bge_chip_stop(bge_t *bgep, boolean_t fault)
27081369Sdduvall {
27091369Sdduvall 	bge_regno_t regno;
27101369Sdduvall 	bge_regno_t *rbp;
27111369Sdduvall 	boolean_t ok;
27121369Sdduvall 
27131369Sdduvall 	BGE_TRACE(("bge_chip_stop($%p)",
27141369Sdduvall 		(void *)bgep));
27151369Sdduvall 
27161369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
27171369Sdduvall 
27181369Sdduvall 	rbp = shutdown_engine_regs;
27191369Sdduvall 	/*
27201369Sdduvall 	 * When driver try to shutdown the BCM5705/5788/5721/5751/
27211369Sdduvall 	 * 5752/5714 and 5715 chipsets,the buffer manager and the mem
27221369Sdduvall 	 * -ory arbiter should not be disabled.
27231369Sdduvall 	 */
27241369Sdduvall 	for (ok = B_TRUE; (regno = *rbp) != BGE_REGNO_NONE; ++rbp) {
27251369Sdduvall 			if (DEVICE_5704_SERIES_CHIPSETS(bgep))
27261369Sdduvall 			    ok &= bge_chip_disable_engine(bgep, regno, 0);
27271369Sdduvall 			else if ((regno != RCV_LIST_SELECTOR_MODE_REG) &&
27281369Sdduvall 				    (regno != DMA_COMPLETION_MODE_REG) &&
27291369Sdduvall 				    (regno != MBUF_CLUSTER_FREE_MODE_REG)&&
27301369Sdduvall 				    (regno != BUFFER_MANAGER_MODE_REG) &&
27311369Sdduvall 				    (regno != MEMORY_ARBITER_MODE_REG))
27321369Sdduvall 					ok &= bge_chip_disable_engine(bgep,
27331369Sdduvall 					    regno, 0);
27341369Sdduvall 	}
27351369Sdduvall 
27361369Sdduvall 	/*
27371369Sdduvall 	 * Finally, disable (all) MAC events & clear the MAC status
27381369Sdduvall 	 */
27391369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG, 0);
27401369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_STATUS_REG, ~0);
27411369Sdduvall 
27421369Sdduvall 	/*
27431369Sdduvall 	 * Do we need to check whether everything completed OK?
27441369Sdduvall 	 * Probably not ... it always works anyway.
27451369Sdduvall 	 */
27461369Sdduvall 
27471369Sdduvall 	if (fault)
27481369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_FAULT;
27491369Sdduvall 	else
27501369Sdduvall 		bgep->bge_chip_state = BGE_CHIP_STOPPED;
27511369Sdduvall }
27521369Sdduvall 
27531369Sdduvall /*
27541369Sdduvall  * Poll for completion of chip's ROM firmware; also, at least on the
27551369Sdduvall  * first time through, find and return the hardware MAC address, if any.
27561369Sdduvall  */
27571369Sdduvall static uint64_t bge_poll_firmware(bge_t *bgep);
27581369Sdduvall #pragma	no_inline(bge_poll_firmware)
27591369Sdduvall 
27601369Sdduvall static uint64_t
27611369Sdduvall bge_poll_firmware(bge_t *bgep)
27621369Sdduvall {
27631369Sdduvall 	uint64_t magic;
27641369Sdduvall 	uint64_t mac;
27651369Sdduvall 	uint32_t gen;
27661369Sdduvall 	uint32_t i;
27671369Sdduvall 
27681369Sdduvall 	/*
27691369Sdduvall 	 * Step 18: put the T3_MAGIC_NUMBER into the GENCOMM port
27701369Sdduvall 	 *
27711369Sdduvall 	 * Step 19: poll for firmware completion (GENCOMM port set
27721369Sdduvall 	 * to the ones complement of T3_MAGIC_NUMBER).
27731369Sdduvall 	 *
27741369Sdduvall 	 * While we're at it, we also read the MAC address register;
27751369Sdduvall 	 * at some stage the the firmware will load this with the
27761369Sdduvall 	 * factory-set value.
27771369Sdduvall 	 *
27781369Sdduvall 	 * When both the magic number and the MAC address are set,
27791369Sdduvall 	 * we're done; but we impose a time limit of one second
27801369Sdduvall 	 * (1000*1000us) in case the firmware fails in some fashion
27811369Sdduvall 	 * or the SEEPROM that provides that MAC address isn't fitted.
27821369Sdduvall 	 *
27831369Sdduvall 	 * After the first time through (chip state != INITIAL), we
27841369Sdduvall 	 * don't need the MAC address to be set (we've already got it
27851369Sdduvall 	 * or not, from the first time), so we don't wait for it, but
27861369Sdduvall 	 * we still have to wait for the T3_MAGIC_NUMBER.
27871369Sdduvall 	 *
27881369Sdduvall 	 * Note: the magic number is only a 32-bit quantity, but the NIC
27891369Sdduvall 	 * memory is 64-bit (and big-endian) internally.  Addressing the
27901369Sdduvall 	 * GENCOMM word as "the upper half of a 64-bit quantity" makes
27911369Sdduvall 	 * it work correctly on both big- and little-endian hosts.
27921369Sdduvall 	 */
27931408Srandyf #ifdef BGE_IPMI_ASF
27941408Srandyf 	if (!bgep->asf_enabled) {
27951408Srandyf #endif
27961408Srandyf 		magic = (uint64_t)T3_MAGIC_NUMBER << 32;
27971408Srandyf 		bge_nic_put64(bgep, NIC_MEM_GENCOMM, magic);
27981408Srandyf 		BGE_DEBUG(("bge_poll_firmware: put T3 magic 0x%llx in GENCOMM"
27991408Srandyf 			" 0x%lx", magic, NIC_MEM_GENCOMM));
28001408Srandyf #ifdef BGE_IPMI_ASF
28011408Srandyf 	}
28021408Srandyf #endif
28031369Sdduvall 
28041369Sdduvall 	for (i = 0; i < 1000; ++i) {
28051369Sdduvall 		drv_usecwait(1000);
28061369Sdduvall 		gen = bge_nic_get64(bgep, NIC_MEM_GENCOMM) >> 32;
28071369Sdduvall 		mac = bge_reg_get64(bgep, MAC_ADDRESS_REG(0));
28081408Srandyf #ifdef BGE_IPMI_ASF
28091408Srandyf 		if (!bgep->asf_enabled) {
28101408Srandyf #endif
28111408Srandyf 			if (gen != ~T3_MAGIC_NUMBER)
28121408Srandyf 				continue;
28131408Srandyf #ifdef BGE_IPMI_ASF
28141408Srandyf 		}
28151408Srandyf #endif
28161369Sdduvall 		if (mac != 0ULL)
28171369Sdduvall 			break;
28181369Sdduvall 		if (bgep->bge_chip_state != BGE_CHIP_INITIAL)
28191369Sdduvall 			break;
28201369Sdduvall 	}
28211369Sdduvall 
28221369Sdduvall 	magic = bge_nic_get64(bgep, NIC_MEM_GENCOMM);
28231369Sdduvall 	BGE_DEBUG(("bge_poll_firmware($%p): PXE magic 0x%x after %d loops",
28241369Sdduvall 		(void *)bgep, gen, i));
28251369Sdduvall 	BGE_DEBUG(("bge_poll_firmware: MAC %016llx, GENCOMM %016llx",
28261369Sdduvall 		mac, magic));
28271369Sdduvall 
28281369Sdduvall 	return (mac);
28291369Sdduvall }
28301369Sdduvall 
28311408Srandyf #ifdef BGE_IPMI_ASF
28321408Srandyf void bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode);
28331408Srandyf #else
28341369Sdduvall void bge_chip_reset(bge_t *bgep, boolean_t enable_dma);
28351408Srandyf #endif
28361369Sdduvall #pragma	no_inline(bge_chip_reset)
28371369Sdduvall 
28381369Sdduvall void
28391408Srandyf #ifdef BGE_IPMI_ASF
28401408Srandyf bge_chip_reset(bge_t *bgep, boolean_t enable_dma, uint_t asf_mode)
28411408Srandyf #else
28421369Sdduvall bge_chip_reset(bge_t *bgep, boolean_t enable_dma)
28431408Srandyf #endif
28441369Sdduvall {
28451369Sdduvall 	chip_id_t chipid;
28461369Sdduvall 	uint64_t mac;
28471369Sdduvall 	uint32_t modeflags;
28481369Sdduvall 	uint32_t mhcr;
28491369Sdduvall 	uint32_t sx0;
28501369Sdduvall 	uint32_t i;
28511408Srandyf #ifdef BGE_IPMI_ASF
28521408Srandyf 	uint32_t mailbox;
28531408Srandyf #endif
28541369Sdduvall 
28551369Sdduvall 	BGE_TRACE(("bge_chip_reset($%p, %d)",
28561369Sdduvall 		(void *)bgep, enable_dma));
28571369Sdduvall 
28581369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
28591369Sdduvall 
28601369Sdduvall 	BGE_DEBUG(("bge_chip_reset($%p, %d): current state is %d",
28611369Sdduvall 		(void *)bgep, enable_dma, bgep->bge_chip_state));
28621369Sdduvall 
28631369Sdduvall 	/*
28641369Sdduvall 	 * Do we need to stop the chip cleanly before resetting?
28651369Sdduvall 	 */
28661369Sdduvall 	switch (bgep->bge_chip_state) {
28671369Sdduvall 	default:
28681369Sdduvall 		ASSERT(!"can't get here");
28691369Sdduvall 		_NOTE(NOTREACHED)
28701369Sdduvall 		return;
28711369Sdduvall 
28721369Sdduvall 	case BGE_CHIP_INITIAL:
28731369Sdduvall 	case BGE_CHIP_STOPPED:
28741369Sdduvall 	case BGE_CHIP_RESET:
28751369Sdduvall 		break;
28761369Sdduvall 
28771369Sdduvall 	case BGE_CHIP_RUNNING:
28781369Sdduvall 	case BGE_CHIP_ERROR:
28791369Sdduvall 	case BGE_CHIP_FAULT:
28801369Sdduvall 		bge_chip_stop(bgep, B_FALSE);
28811369Sdduvall 		break;
28821369Sdduvall 	}
28831369Sdduvall 
28841408Srandyf #ifdef BGE_IPMI_ASF
28851408Srandyf 	if (bgep->asf_enabled) {
28861408Srandyf 		if (asf_mode == ASF_MODE_INIT) {
28871408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
28881408Srandyf 		} else if (asf_mode == ASF_MODE_SHUTDOWN) {
28891408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
28901408Srandyf 		}
28911408Srandyf 	}
28921408Srandyf #endif
28931369Sdduvall 	/*
28941369Sdduvall 	 * Adapted from Broadcom document 570X-PG102-R, pp 102-116.
28951369Sdduvall 	 * Updated to reflect Broadcom document 570X-PG104-R, pp 146-159.
28961369Sdduvall 	 *
28971369Sdduvall 	 * Before reset Core clock,it is
28981369Sdduvall 	 * also required to initialize the Memory Arbiter as specified in step9
28991369Sdduvall 	 * and Misc Host Control Register as specified in step-13
29001369Sdduvall 	 * Step 4-5: reset Core clock & wait for completion
29011369Sdduvall 	 * Steps 6-8: are done by bge_chip_cfg_init()
29021369Sdduvall 	 */
29031369Sdduvall 	(void) bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0);
29041369Sdduvall 
29051369Sdduvall 	mhcr = MHCR_ENABLE_INDIRECT_ACCESS |
29061369Sdduvall 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
29071369Sdduvall 	    MHCR_MASK_INTERRUPT_MODE |
29081369Sdduvall 	    MHCR_MASK_PCI_INT_OUTPUT |
29091369Sdduvall 	    MHCR_CLEAR_INTERRUPT_INTA;
29101369Sdduvall #ifdef  _BIG_ENDIAN
29111369Sdduvall 	mhcr |= MHCR_ENABLE_ENDIAN_WORD_SWAP | MHCR_ENABLE_ENDIAN_BYTE_SWAP;
29121369Sdduvall #endif  /* _BIG_ENDIAN */
29131369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcr);
29141408Srandyf #ifdef BGE_IPMI_ASF
29151408Srandyf 	if (bgep->asf_enabled)
29161408Srandyf 		bgep->asf_wordswapped = B_FALSE;
29171408Srandyf #endif
29181369Sdduvall 	(void) bge_chip_reset_engine(bgep, MISC_CONFIG_REG);
29191369Sdduvall 	bge_chip_cfg_init(bgep, &chipid, enable_dma);
29201369Sdduvall 
29211369Sdduvall 	/*
29221369Sdduvall 	 * Step 8a: This may belong elsewhere, but BCM5721 needs
29231369Sdduvall 	 * a bit set to avoid a fifo overflow/underflow bug.
29241369Sdduvall 	 */
29251369Sdduvall 	if (bgep->chipid.chip_label == 5721 || bgep->chipid.chip_label == 5751)
29261369Sdduvall 		bge_reg_set32(bgep, TLP_CONTROL_REG, TLP_DATA_FIFO_PROTECT);
29271369Sdduvall 
29281369Sdduvall 
29291369Sdduvall 	/*
29301369Sdduvall 	 * Step 9: enable MAC memory arbiter,bit30 and bit31 of 5714/5715 should
29311369Sdduvall 	 * not be changed.
29321369Sdduvall 	 */
29331369Sdduvall 	(void) bge_chip_enable_engine(bgep, MEMORY_ARBITER_MODE_REG, 0);
29341369Sdduvall 
29351369Sdduvall 	/*
29361369Sdduvall 	 * Steps 10-11: configure PIO endianness options and
29371369Sdduvall 	 * enable indirect register access -- already done
29381369Sdduvall 	 * Steps 12-13: enable writing to the PCI state & clock
29391369Sdduvall 	 * control registers -- not required; we aren't going to
29401369Sdduvall 	 * use those features.
29411369Sdduvall 	 * Steps 14-15: Configure DMA endianness options.  See
29421369Sdduvall 	 * the comments on the setting of the MHCR above.
29431369Sdduvall 	 */
29441369Sdduvall #ifdef	_BIG_ENDIAN
29451369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME |
29461369Sdduvall 		    MODE_WORD_SWAP_NONFRAME | MODE_BYTE_SWAP_NONFRAME;
29471369Sdduvall #else
29481369Sdduvall 	modeflags = MODE_WORD_SWAP_FRAME | MODE_BYTE_SWAP_FRAME;
29491369Sdduvall #endif	/* _BIG_ENDIAN */
29501408Srandyf #ifdef BGE_IPMI_ASF
29511408Srandyf 	if (bgep->asf_enabled)
29521408Srandyf 		modeflags |= MODE_HOST_STACK_UP;
29531408Srandyf #endif
29541369Sdduvall 	bge_reg_put32(bgep, MODE_CONTROL_REG, modeflags);
29551369Sdduvall 
29561408Srandyf #ifdef BGE_IPMI_ASF
29571408Srandyf 	if (bgep->asf_enabled) {
29581408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
29591408Srandyf 			/* Wait for NVRAM init */
29601408Srandyf 			i = 0;
29611408Srandyf 			drv_usecwait(5000);
29621408Srandyf 			mailbox = bge_nic_get32(bgep, BGE_FIRMWARE_MAILBOX);
29631408Srandyf 			while ((mailbox != (uint32_t)
29641408Srandyf 				~BGE_MAGIC_NUM_FIRMWARE_INIT_DONE) &&
29651408Srandyf 				(i < 10000)) {
29661408Srandyf 				drv_usecwait(100);
29671408Srandyf 				mailbox = bge_nic_get32(bgep,
29681408Srandyf 					BGE_FIRMWARE_MAILBOX);
29691408Srandyf 				i++;
29701408Srandyf 			}
29711408Srandyf 			if (!bgep->asf_newhandshake) {
29721408Srandyf 				if ((asf_mode == ASF_MODE_INIT) ||
29731408Srandyf 					(asf_mode == ASF_MODE_POST_INIT)) {
29741408Srandyf 
29751408Srandyf 					bge_asf_post_reset_old_mode(bgep,
29761408Srandyf 						BGE_INIT_RESET);
29771408Srandyf 				} else {
29781408Srandyf 					bge_asf_post_reset_old_mode(bgep,
29791408Srandyf 						BGE_SHUTDOWN_RESET);
29801408Srandyf 				}
29811408Srandyf 			}
29821408Srandyf 		}
29831408Srandyf 	}
29841408Srandyf #endif
29851369Sdduvall 	/*
29861369Sdduvall 	 * Steps 16-17: poll for firmware completion
29871369Sdduvall 	 */
29881369Sdduvall 	mac = bge_poll_firmware(bgep);
29891369Sdduvall 
29901369Sdduvall 	/*
29911369Sdduvall 	 * Step 18: enable external memory -- doesn't apply.
29921369Sdduvall 	 *
29931369Sdduvall 	 * However we take the opportunity to set the MLCR anyway, as
29941369Sdduvall 	 * this register also controls the SEEPROM auto-access method
29951369Sdduvall 	 * which we may want to use later ...
29961369Sdduvall 	 *
29971369Sdduvall 	 * The proper value here depends on the way the chip is wired
29981369Sdduvall 	 * into the circuit board, as this register *also* controls which
29991369Sdduvall 	 * of the "Miscellaneous I/O" pins are driven as outputs and the
30001369Sdduvall 	 * values driven onto those pins!
30011369Sdduvall 	 *
30021369Sdduvall 	 * See also step 74 in the PRM ...
30031369Sdduvall 	 */
30041369Sdduvall 	bge_reg_put32(bgep, MISC_LOCAL_CONTROL_REG,
30051369Sdduvall 	    bgep->chipid.bge_mlcr_default);
30061369Sdduvall 	bge_reg_set32(bgep, SERIAL_EEPROM_ADDRESS_REG, SEEPROM_ACCESS_INIT);
30071369Sdduvall 
30081369Sdduvall 	/*
30091369Sdduvall 	 * Step 20: clear the Ethernet MAC mode register
30101369Sdduvall 	 */
30111369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_MODE_REG, 0);
30121369Sdduvall 
30131369Sdduvall 	/*
30141369Sdduvall 	 * Step 21: restore cache-line-size, latency timer, and
30151369Sdduvall 	 * subsystem ID registers to their original values (not
30161369Sdduvall 	 * those read into the local structure <chipid>, 'cos
30171369Sdduvall 	 * that was after they were cleared by the RESET).
30181369Sdduvall 	 *
30191369Sdduvall 	 * Note: the Subsystem Vendor/Device ID registers are not
30201369Sdduvall 	 * directly writable in config space, so we use the shadow
30211369Sdduvall 	 * copy in "Page Zero" of register space to restore them
30221369Sdduvall 	 * both in one go ...
30231369Sdduvall 	 */
30241369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_CACHE_LINESZ,
30251369Sdduvall 		bgep->chipid.clsize);
30261369Sdduvall 	pci_config_put8(bgep->cfg_handle, PCI_CONF_LATENCY_TIMER,
30271369Sdduvall 		bgep->chipid.latency);
30281369Sdduvall 	bge_reg_put32(bgep, PCI_CONF_SUBVENID,
30291369Sdduvall 		(bgep->chipid.subdev << 16) | bgep->chipid.subven);
30301369Sdduvall 
30311369Sdduvall 	/*
30321369Sdduvall 	 * The SEND INDEX registers should be reset to zero by the
30331369Sdduvall 	 * global chip reset; if they're not, there'll be trouble
30341369Sdduvall 	 * later on -- usually in the form of an ASSERTion failure
30351369Sdduvall 	 * in bge_send.c.  So let's catch it early ...
30361369Sdduvall 	 */
30371369Sdduvall 	sx0 = bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0));
30381369Sdduvall 	if (sx0 != 0)
30391369Sdduvall 		bge_problem(bgep, "send index %d: device didn't RESET!", sx0);
30401369Sdduvall 	ASSERT(sx0 == 0);
30411369Sdduvall 
30421369Sdduvall 	/* Enable MSI code */
30431369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
30441369Sdduvall 		bge_reg_set32(bgep, MSI_MODE_REG,
30451369Sdduvall 		    MSI_PRI_HIGHEST|MSI_MSI_ENABLE);
30461369Sdduvall 
30471369Sdduvall 	/*
30481369Sdduvall 	 * On the first time through, save the factory-set MAC address
30491369Sdduvall 	 * (if any).  If bge_poll_firmware() above didn't return one
30501369Sdduvall 	 * (from a chip register) consider looking in the attached NV
30511369Sdduvall 	 * memory device, if any.  Once we have it, we save it in both
30521369Sdduvall 	 * register-image (64-bit) and byte-array forms.  All-zero and
30531369Sdduvall 	 * all-one addresses are not valid, and we refuse to stash those.
30541369Sdduvall 	 */
30551369Sdduvall 	if (bgep->bge_chip_state == BGE_CHIP_INITIAL) {
30561369Sdduvall 		if (mac == 0ULL)
30571369Sdduvall 			mac = bge_get_nvmac(bgep);
30581369Sdduvall 		if (mac != 0ULL && mac != ~0ULL) {
30591369Sdduvall 			bgep->chipid.hw_mac_addr = mac;
30601369Sdduvall 			for (i = ETHERADDRL; i-- != 0; ) {
30611369Sdduvall 				bgep->chipid.vendor_addr.addr[i] = (uchar_t)mac;
30621369Sdduvall 				mac >>= 8;
30631369Sdduvall 			}
30641369Sdduvall 			bgep->chipid.vendor_addr.set = 1;
30651369Sdduvall 		}
30661369Sdduvall 	}
30671369Sdduvall 
30681408Srandyf #ifdef BGE_IPMI_ASF
30691408Srandyf 	if (bgep->asf_enabled && bgep->asf_newhandshake) {
30701408Srandyf 		if (asf_mode != ASF_MODE_NONE) {
30711408Srandyf 			if ((asf_mode == ASF_MODE_INIT) ||
30721408Srandyf 				(asf_mode == ASF_MODE_POST_INIT)) {
30731408Srandyf 
30741408Srandyf 				bge_asf_post_reset_new_mode(bgep,
30751408Srandyf 					BGE_INIT_RESET);
30761408Srandyf 			} else {
30771408Srandyf 				bge_asf_post_reset_new_mode(bgep,
30781408Srandyf 					BGE_SHUTDOWN_RESET);
30791408Srandyf 			}
30801408Srandyf 		}
30811408Srandyf 	}
30821408Srandyf #endif
30831408Srandyf 
30841369Sdduvall 	/*
30851369Sdduvall 	 * Record the new state
30861369Sdduvall 	 */
30871369Sdduvall 	bgep->chip_resets += 1;
30881369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RESET;
30891369Sdduvall }
30901369Sdduvall 
30911369Sdduvall /*
30921369Sdduvall  * bge_chip_start() -- start the chip transmitting and/or receiving,
30931369Sdduvall  * including enabling interrupts
30941369Sdduvall  */
30951369Sdduvall void bge_chip_start(bge_t *bgep, boolean_t reset_phys);
30961369Sdduvall #pragma	no_inline(bge_chip_start)
30971369Sdduvall 
30981369Sdduvall void
30991369Sdduvall bge_chip_start(bge_t *bgep, boolean_t reset_phys)
31001369Sdduvall {
31011369Sdduvall 	uint32_t coalmode;
31021369Sdduvall 	uint32_t ledctl;
31031369Sdduvall 	uint32_t mtu;
31041369Sdduvall 	uint32_t maxring;
31051369Sdduvall 	uint64_t ring;
31061369Sdduvall 
31071369Sdduvall 	BGE_TRACE(("bge_chip_start($%p)",
31081369Sdduvall 		(void *)bgep));
31091369Sdduvall 
31101369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
31111369Sdduvall 	ASSERT(bgep->bge_chip_state == BGE_CHIP_RESET);
31121369Sdduvall 	ASSERT(bge_reg_get32(bgep, NIC_DIAG_SEND_INDEX_REG(0)) == 0);
31131369Sdduvall 
31141369Sdduvall 	/*
31151369Sdduvall 	 * Taken from Broadcom document 570X-PG102-R, pp 102-116.
31161369Sdduvall 	 * The document specifies 95 separate steps to fully
31171369Sdduvall 	 * initialise the chip!!!!
31181369Sdduvall 	 *
31191369Sdduvall 	 * The reset code above has already got us as far as step
31201369Sdduvall 	 * 21, so we continue with ...
31211369Sdduvall 	 *
31221369Sdduvall 	 * Step 22: clear the MAC statistics block
31231369Sdduvall 	 * (0x0300-0x0aff in NIC-local memory)
31241369Sdduvall 	 */
31251369Sdduvall 	if (bgep->chipid.statistic_type == BGE_STAT_BLK)
31261369Sdduvall 		bge_nic_zero(bgep, NIC_MEM_STATISTICS,
31271369Sdduvall 		    NIC_MEM_STATISTICS_SIZE);
31281369Sdduvall 
31291369Sdduvall 	/*
31301369Sdduvall 	 * Step 23: clear the status block (in host memory)
31311369Sdduvall 	 */
31321369Sdduvall 	DMA_ZERO(bgep->status_block);
31331369Sdduvall 
31341369Sdduvall 	/*
31351369Sdduvall 	 * Step 24: set DMA read/write control register
31361369Sdduvall 	 */
31371369Sdduvall 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_PDRWCR,
31381369Sdduvall 		bgep->chipid.bge_dma_rwctrl);
31391369Sdduvall 
31401369Sdduvall 	/*
31411369Sdduvall 	 * Step 25: Configure DMA endianness -- already done (16/17)
31421369Sdduvall 	 * Step 26: Configure Host-Based Send Rings
31431369Sdduvall 	 * Step 27: Indicate Host Stack Up
31441369Sdduvall 	 */
31451369Sdduvall 	bge_reg_set32(bgep, MODE_CONTROL_REG,
31461369Sdduvall 		MODE_HOST_SEND_BDS |
31471369Sdduvall 		MODE_HOST_STACK_UP);
31481369Sdduvall 
31491369Sdduvall 	/*
31501369Sdduvall 	 * Step 28: Configure checksum options:
3151*1611Szh199473 	 *	Solaris supports the hardware default checksum options.
3152*1611Szh199473 	 *
3153*1611Szh199473 	 *	Workaround for Incorrect pseudo-header checksum calculation.
31541369Sdduvall 	 */
3155*1611Szh199473 	if (bgep->macp->m_info.mi_cksum & HCKSUM_INET_PARTIAL)
3156*1611Szh199473 		bge_reg_set32(bgep, MODE_CONTROL_REG,
3157*1611Szh199473 			MODE_SEND_NO_PSEUDO_HDR_CSUM);
31581369Sdduvall 
31591369Sdduvall 	/*
31601369Sdduvall 	 * Step 29: configure Timer Prescaler.  The value is always the
31611369Sdduvall 	 * same: the Core Clock frequency in MHz (66), minus 1, shifted
31621369Sdduvall 	 * into bits 7-1.  Don't set bit 0, 'cos that's the RESET bit
31631369Sdduvall 	 * for the whole chip!
31641369Sdduvall 	 */
31651369Sdduvall 	bge_reg_put32(bgep, MISC_CONFIG_REG, MISC_CONFIG_DEFAULT);
31661369Sdduvall 
31671369Sdduvall 	/*
31681369Sdduvall 	 * Steps 30-31: Configure MAC local memory pool & DMA pool registers
31691369Sdduvall 	 *
31701369Sdduvall 	 * If the mbuf_length is specified as 0, we just leave these at
31711369Sdduvall 	 * their hardware defaults, rather than explicitly setting them.
31721369Sdduvall 	 * As the Broadcom HRM,driver better not change the parameters
31731369Sdduvall 	 * when the chipsets is 5705/5788/5721/5751/5714 and 5715.
31741369Sdduvall 	 */
31751369Sdduvall 	if ((bgep->chipid.mbuf_length != 0) &&
31761369Sdduvall 		(DEVICE_5704_SERIES_CHIPSETS(bgep))) {
31771369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_BASE_REG,
31781369Sdduvall 				bgep->chipid.mbuf_base);
31791369Sdduvall 			bge_reg_put32(bgep, MBUF_POOL_LENGTH_REG,
31801369Sdduvall 				bgep->chipid.mbuf_length);
31811369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_BASE_REG,
31821369Sdduvall 				DMAD_POOL_BASE_DEFAULT);
31831369Sdduvall 			bge_reg_put32(bgep, DMAD_POOL_LENGTH_REG,
31841369Sdduvall 				DMAD_POOL_LENGTH_DEFAULT);
31851369Sdduvall 	}
31861369Sdduvall 
31871369Sdduvall 	/*
31881369Sdduvall 	 * Step 32: configure MAC memory pool watermarks
31891369Sdduvall 	 */
31901369Sdduvall 	bge_reg_put32(bgep, RDMA_MBUF_LOWAT_REG,
31911369Sdduvall 		bgep->chipid.mbuf_lo_water_rdma);
31921369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MBUF_LOWAT_REG,
31931369Sdduvall 		bgep->chipid.mbuf_lo_water_rmac);
31941369Sdduvall 	bge_reg_put32(bgep, MBUF_HIWAT_REG,
31951369Sdduvall 		bgep->chipid.mbuf_hi_water);
31961369Sdduvall 
31971369Sdduvall 	/*
31981369Sdduvall 	 * Step 33: configure DMA resource watermarks
31991369Sdduvall 	 */
32001369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
32011369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_LOWAT_REG,
32021369Sdduvall 		    bge_dmad_lo_water);
32031369Sdduvall 		bge_reg_put32(bgep, DMAD_POOL_HIWAT_REG,
32041369Sdduvall 		    bge_dmad_hi_water);
32051369Sdduvall 	}
32061369Sdduvall 	bge_reg_put32(bgep, LOWAT_MAX_RECV_FRAMES_REG, bge_lowat_recv_frames);
32071369Sdduvall 
32081369Sdduvall 	/*
32091369Sdduvall 	 * Steps 34-36: enable buffer manager & internal h/w queues
32101369Sdduvall 	 */
32111369Sdduvall 	(void) bge_chip_enable_engine(bgep, BUFFER_MANAGER_MODE_REG,
32121369Sdduvall 		STATE_MACHINE_ATTN_ENABLE_BIT);
32131369Sdduvall 	(void) bge_chip_enable_engine(bgep, FTQ_RESET_REG, 0);
32141369Sdduvall 
32151369Sdduvall 	/*
32161369Sdduvall 	 * Steps 37-39: initialise Receive Buffer (Producer) RCBs
32171369Sdduvall 	 */
32181369Sdduvall 	bge_reg_putrcb(bgep, STD_RCV_BD_RING_RCB_REG,
32191369Sdduvall 		&bgep->buff[BGE_STD_BUFF_RING].hw_rcb);
32201369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
32211369Sdduvall 		bge_reg_putrcb(bgep, JUMBO_RCV_BD_RING_RCB_REG,
32221369Sdduvall 			&bgep->buff[BGE_JUMBO_BUFF_RING].hw_rcb);
32231369Sdduvall 		bge_reg_putrcb(bgep, MINI_RCV_BD_RING_RCB_REG,
32241369Sdduvall 			&bgep->buff[BGE_MINI_BUFF_RING].hw_rcb);
32251369Sdduvall 	}
32261369Sdduvall 
32271369Sdduvall 	/*
32281369Sdduvall 	 * Step 40: set Receive Buffer Descriptor Ring replenish thresholds
32291369Sdduvall 	 */
32301369Sdduvall 	bge_reg_put32(bgep, STD_RCV_BD_REPLENISH_REG, bge_replenish_std);
32311369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
32321369Sdduvall 		bge_reg_put32(bgep, JUMBO_RCV_BD_REPLENISH_REG,
32331369Sdduvall 		    bge_replenish_jumbo);
32341369Sdduvall 		bge_reg_put32(bgep, MINI_RCV_BD_REPLENISH_REG,
32351369Sdduvall 		    bge_replenish_mini);
32361369Sdduvall 	}
32371369Sdduvall 
32381369Sdduvall 	/*
32391369Sdduvall 	 * Steps 41-43: clear Send Ring Producer Indices and initialise
32401369Sdduvall 	 * Send Producer Rings (0x0100-0x01ff in NIC-local memory)
32411369Sdduvall 	 */
32421369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
32431369Sdduvall 		maxring = BGE_SEND_RINGS_MAX;
32441369Sdduvall 	else
32451369Sdduvall 		maxring = BGE_SEND_RINGS_MAX_5705;
32461369Sdduvall 	for (ring = 0; ring < maxring; ++ring) {
32471369Sdduvall 		bge_mbx_put(bgep, SEND_RING_HOST_INDEX_REG(ring), 0);
32481369Sdduvall 		bge_mbx_put(bgep, SEND_RING_NIC_INDEX_REG(ring), 0);
32491369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_SEND_RING(ring),
32501369Sdduvall 			&bgep->send[ring].hw_rcb);
32511369Sdduvall 	}
32521369Sdduvall 
32531369Sdduvall 	/*
32541369Sdduvall 	 * Steps 44-45: initialise Receive Return Rings
32551369Sdduvall 	 * (0x0200-0x02ff in NIC-local memory)
32561369Sdduvall 	 */
32571369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
32581369Sdduvall 		maxring = BGE_RECV_RINGS_MAX;
32591369Sdduvall 	else
32601369Sdduvall 		maxring = BGE_RECV_RINGS_MAX_5705;
32611369Sdduvall 	for (ring = 0; ring < maxring; ++ring)
32621369Sdduvall 		bge_nic_putrcb(bgep, NIC_MEM_RECV_RING(ring),
32631369Sdduvall 			&bgep->recv[ring].hw_rcb);
32641369Sdduvall 
32651369Sdduvall 	/*
32661369Sdduvall 	 * Step 46: initialise Receive Buffer (Producer) Ring indexes
32671369Sdduvall 	 */
32681369Sdduvall 	bge_mbx_put(bgep, RECV_STD_PROD_INDEX_REG, 0);
32691369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
32701369Sdduvall 		bge_mbx_put(bgep, RECV_JUMBO_PROD_INDEX_REG, 0);
32711369Sdduvall 		bge_mbx_put(bgep, RECV_MINI_PROD_INDEX_REG, 0);
32721369Sdduvall 	}
32731369Sdduvall 	/*
32741369Sdduvall 	 * Step 47: configure the MAC unicast address
32751369Sdduvall 	 * Step 48: configure the random backoff seed
32761369Sdduvall 	 * Step 96: set up multicast filters
32771369Sdduvall 	 */
32781408Srandyf #ifdef BGE_IPMI_ASF
32791408Srandyf 	bge_chip_sync(bgep, B_FALSE);
32801408Srandyf #else
32811369Sdduvall 	bge_chip_sync(bgep);
32821408Srandyf #endif
32831369Sdduvall 
32841369Sdduvall 	/*
32851369Sdduvall 	 * Step 49: configure the MTU
32861369Sdduvall 	 */
32871369Sdduvall 	mtu = bgep->chipid.ethmax_size+ETHERFCSL+VLAN_TAGSZ;
32881369Sdduvall 	bge_reg_put32(bgep, MAC_RX_MTU_SIZE_REG, mtu);
32891369Sdduvall 
32901369Sdduvall 	/*
32911369Sdduvall 	 * Step 50: configure the IPG et al
32921369Sdduvall 	 */
32931369Sdduvall 	bge_reg_put32(bgep, MAC_TX_LENGTHS_REG, MAC_TX_LENGTHS_DEFAULT);
32941369Sdduvall 
32951369Sdduvall 	/*
32961369Sdduvall 	 * Step 51: configure the default Rx Return Ring
32971369Sdduvall 	 */
32981369Sdduvall 	bge_reg_put32(bgep, RCV_RULES_CONFIG_REG, RCV_RULES_CONFIG_DEFAULT);
32991369Sdduvall 
33001369Sdduvall 	/*
33011369Sdduvall 	 * Steps 52-54: configure Receive List Placement,
33021369Sdduvall 	 * and enable Receive List Placement Statistics
33031369Sdduvall 	 */
33041369Sdduvall 	bge_reg_put32(bgep, RCV_LP_CONFIG_REG,
33051369Sdduvall 		RCV_LP_CONFIG(bgep->chipid.rx_rings));
33061369Sdduvall 	bge_reg_put32(bgep, RCV_LP_STATS_ENABLE_MASK_REG, ~0);
33071369Sdduvall 	bge_reg_set32(bgep, RCV_LP_STATS_CONTROL_REG, RCV_LP_STATS_ENABLE);
33081369Sdduvall 
33091369Sdduvall 	if (bgep->chipid.rx_rings > 1)
33101369Sdduvall 		bge_init_recv_rule(bgep);
33111369Sdduvall 
33121369Sdduvall 	/*
33131369Sdduvall 	 * Steps 55-56: enable Send Data Initiator Statistics
33141369Sdduvall 	 */
33151369Sdduvall 	bge_reg_put32(bgep, SEND_INIT_STATS_ENABLE_MASK_REG, ~0);
33161369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33171369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
33181369Sdduvall 		    SEND_INIT_STATS_ENABLE | SEND_INIT_STATS_FASTER);
33191369Sdduvall 	} else {
33201369Sdduvall 		bge_reg_put32(bgep, SEND_INIT_STATS_CONTROL_REG,
33211369Sdduvall 		    SEND_INIT_STATS_ENABLE);
33221369Sdduvall 	}
33231369Sdduvall 	/*
33241369Sdduvall 	 * Steps 57-58: stop (?) the Host Coalescing Engine
33251369Sdduvall 	 */
33261369Sdduvall 	(void) bge_chip_disable_engine(bgep, HOST_COALESCE_MODE_REG, ~0);
33271369Sdduvall 
33281369Sdduvall 	/*
33291369Sdduvall 	 * Steps 59-62: initialise Host Coalescing parameters
33301369Sdduvall 	 */
33311369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_MAX_BD_REG, bge_tx_count_norm);
33321369Sdduvall 	bge_reg_put32(bgep, SEND_COALESCE_TICKS_REG, bge_tx_ticks_norm);
33331369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, bge_rx_count_norm);
33341369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, bge_rx_ticks_norm);
33351369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33361369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_BD_REG,
33371369Sdduvall 		    bge_tx_count_intr);
33381369Sdduvall 		bge_reg_put32(bgep, SEND_COALESCE_INT_TICKS_REG,
33391369Sdduvall 		    bge_tx_ticks_intr);
33401369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_BD_REG,
33411369Sdduvall 		    bge_rx_count_intr);
33421369Sdduvall 		bge_reg_put32(bgep, RCV_COALESCE_INT_TICKS_REG,
33431369Sdduvall 		    bge_rx_ticks_intr);
33441369Sdduvall 	}
33451369Sdduvall 
33461369Sdduvall 	/*
33471369Sdduvall 	 * Steps 63-64: initialise status block & statistics
33481369Sdduvall 	 * host memory addresses
33491369Sdduvall 	 * The statistic block does not exist in some chipsets
33501369Sdduvall 	 * Step 65: initialise Statistics Coalescing Tick Counter
33511369Sdduvall 	 */
33521369Sdduvall 	bge_reg_put64(bgep, STATUS_BLOCK_HOST_ADDR_REG,
33531369Sdduvall 		bgep->status_block.cookie.dmac_laddress);
33541369Sdduvall 
33551369Sdduvall 	/*
33561369Sdduvall 	 * Steps 66-67: initialise status block & statistics
33571369Sdduvall 	 * NIC-local memory addresses
33581369Sdduvall 	 */
33591369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep)) {
33601369Sdduvall 		bge_reg_put64(bgep, STATISTICS_HOST_ADDR_REG,
33611369Sdduvall 		    bgep->statistics.cookie.dmac_laddress);
33621369Sdduvall 		bge_reg_put32(bgep, STATISTICS_TICKS_REG,
33631369Sdduvall 		    STATISTICS_TICKS_DEFAULT);
33641369Sdduvall 		bge_reg_put32(bgep, STATUS_BLOCK_BASE_ADDR_REG,
33651369Sdduvall 		    NIC_MEM_STATUS_BLOCK);
33661369Sdduvall 		bge_reg_put32(bgep, STATISTICS_BASE_ADDR_REG,
33671369Sdduvall 		    NIC_MEM_STATISTICS);
33681369Sdduvall 	}
33691369Sdduvall 
33701369Sdduvall 	/*
33711369Sdduvall 	 * Steps 68-71: start the Host Coalescing Engine, the Receive BD
33721369Sdduvall 	 * Completion Engine, the Receive List Placement Engine, and the
33731369Sdduvall 	 * Receive List selector.Pay attention:0x3400 is not exist in BCM5714
33741369Sdduvall 	 * and BCM5715.
33751369Sdduvall 	 */
33761369Sdduvall 	if (bgep->chipid.tx_rings <= COALESCE_64_BYTE_RINGS &&
33771369Sdduvall 	    bgep->chipid.rx_rings <= COALESCE_64_BYTE_RINGS)
33781369Sdduvall 		coalmode = COALESCE_64_BYTE_STATUS;
33791369Sdduvall 	else
33801369Sdduvall 		coalmode = 0;
33811369Sdduvall 	(void) bge_chip_enable_engine(bgep, HOST_COALESCE_MODE_REG, coalmode);
33821369Sdduvall 	(void) bge_chip_enable_engine(bgep, RCV_BD_COMPLETION_MODE_REG,
33831369Sdduvall 	    STATE_MACHINE_ATTN_ENABLE_BIT);
33841369Sdduvall 	(void) bge_chip_enable_engine(bgep, RCV_LIST_PLACEMENT_MODE_REG, 0);
33851369Sdduvall 
33861369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
33871369Sdduvall 		(void) bge_chip_enable_engine(bgep, RCV_LIST_SELECTOR_MODE_REG,
33881369Sdduvall 		    STATE_MACHINE_ATTN_ENABLE_BIT);
33891369Sdduvall 
33901369Sdduvall 	/*
33911369Sdduvall 	 * Step 72: Enable MAC DMA engines
33921369Sdduvall 	 * Step 73: Clear & enable MAC statistics
33931369Sdduvall 	 */
33941369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
33951369Sdduvall 		ETHERNET_MODE_ENABLE_FHDE |
33961369Sdduvall 		ETHERNET_MODE_ENABLE_RDE |
33971369Sdduvall 		ETHERNET_MODE_ENABLE_TDE);
33981369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_MODE_REG,
33991369Sdduvall 		ETHERNET_MODE_ENABLE_TX_STATS |
34001369Sdduvall 		ETHERNET_MODE_ENABLE_RX_STATS |
34011369Sdduvall 		ETHERNET_MODE_CLEAR_TX_STATS |
34021369Sdduvall 		ETHERNET_MODE_CLEAR_RX_STATS);
34031369Sdduvall 
34041369Sdduvall 	/*
34051369Sdduvall 	 * Step 74: configure the MLCR (Miscellaneous Local Control
34061369Sdduvall 	 * Register); not required, as we set up the MLCR in step 10
34071369Sdduvall 	 * (part of the reset code) above.
34081369Sdduvall 	 *
34091369Sdduvall 	 * Step 75: clear Interrupt Mailbox 0
34101369Sdduvall 	 */
34111369Sdduvall 	bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG, 0);
34121369Sdduvall 
34131369Sdduvall 	/*
34141369Sdduvall 	 * Steps 76-87: Gentlemen, start your engines ...
34151369Sdduvall 	 *
34161369Sdduvall 	 * Enable the DMA Completion Engine, the Write DMA Engine,
34171369Sdduvall 	 * the Read DMA Engine, Receive Data Completion Engine,
34181369Sdduvall 	 * the MBuf Cluster Free Engine, the Send Data Completion Engine,
34191369Sdduvall 	 * the Send BD Completion Engine, the Receive BD Initiator Engine,
34201369Sdduvall 	 * the Receive Data Initiator Engine, the Send Data Initiator Engine,
34211369Sdduvall 	 * the Send BD Initiator Engine, and the Send BD Selector Engine.
34221369Sdduvall 	 *
34231369Sdduvall 	 * Beware exhaust fumes?
34241369Sdduvall 	 */
34251369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34261369Sdduvall 		(void) bge_chip_enable_engine(bgep, DMA_COMPLETION_MODE_REG, 0);
34271369Sdduvall 	(void) bge_chip_enable_engine(bgep, WRITE_DMA_MODE_REG,
34281369Sdduvall 		(bge_dma_wrprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS);
34291369Sdduvall 	(void) bge_chip_enable_engine(bgep, READ_DMA_MODE_REG,
34301369Sdduvall 		(bge_dma_rdprio << DMA_PRIORITY_SHIFT) | ALL_DMA_ATTN_BITS);
34311369Sdduvall 	(void) bge_chip_enable_engine(bgep, RCV_DATA_COMPLETION_MODE_REG,
34321369Sdduvall 		STATE_MACHINE_ATTN_ENABLE_BIT);
34331369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
34341369Sdduvall 		(void) bge_chip_enable_engine(bgep,
34351369Sdduvall 		    MBUF_CLUSTER_FREE_MODE_REG, 0);
34361369Sdduvall 	(void) bge_chip_enable_engine(bgep, SEND_DATA_COMPLETION_MODE_REG, 0);
34371369Sdduvall 	(void) bge_chip_enable_engine(bgep, SEND_BD_COMPLETION_MODE_REG,
34381369Sdduvall 		STATE_MACHINE_ATTN_ENABLE_BIT);
34391369Sdduvall 	(void) bge_chip_enable_engine(bgep, RCV_BD_INITIATOR_MODE_REG,
34401369Sdduvall 		RCV_BD_DISABLED_RING_ATTN);
34411369Sdduvall 	(void) bge_chip_enable_engine(bgep, RCV_DATA_BD_INITIATOR_MODE_REG,
34421369Sdduvall 		RCV_DATA_BD_ILL_RING_ATTN);
34431369Sdduvall 	(void) bge_chip_enable_engine(bgep, SEND_DATA_INITIATOR_MODE_REG, 0);
34441369Sdduvall 	(void) bge_chip_enable_engine(bgep, SEND_BD_INITIATOR_MODE_REG,
34451369Sdduvall 		STATE_MACHINE_ATTN_ENABLE_BIT);
34461369Sdduvall 	(void) bge_chip_enable_engine(bgep, SEND_BD_SELECTOR_MODE_REG,
34471369Sdduvall 		STATE_MACHINE_ATTN_ENABLE_BIT);
34481369Sdduvall 
34491369Sdduvall 	/*
34501369Sdduvall 	 * Step 88: download firmware -- doesn't apply
34511369Sdduvall 	 * Steps 89-90: enable Transmit & Receive MAC Engines
34521369Sdduvall 	 */
34531369Sdduvall 	(void) bge_chip_enable_engine(bgep, TRANSMIT_MAC_MODE_REG, 0);
34541408Srandyf #ifdef BGE_IPMI_ASF
34551408Srandyf 	if (bgep->asf_enabled) {
34561408Srandyf 		(void) bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG, 0);
34571408Srandyf 	} else {
34581408Srandyf 		(void) bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
34591408Srandyf 		    RECEIVE_MODE_KEEP_VLAN_TAG);
34601408Srandyf 	}
34611408Srandyf #else
34621369Sdduvall 	(void) bge_chip_enable_engine(bgep, RECEIVE_MAC_MODE_REG,
34631369Sdduvall 	    RECEIVE_MODE_KEEP_VLAN_TAG);
34641408Srandyf #endif
34651369Sdduvall 
34661369Sdduvall 	/*
34671369Sdduvall 	 * Step 91: disable auto-polling of PHY status
34681369Sdduvall 	 */
34691369Sdduvall 	bge_reg_put32(bgep, MI_MODE_REG, MI_MODE_DEFAULT);
34701369Sdduvall 
34711369Sdduvall 	/*
34721369Sdduvall 	 * Step 92: configure D0 power state (not required)
34731369Sdduvall 	 * Step 93: initialise LED control register ()
34741369Sdduvall 	 */
34751369Sdduvall 	ledctl = LED_CONTROL_DEFAULT;
34761369Sdduvall 	switch (bgep->chipid.device) {
34771369Sdduvall 	case DEVICE_ID_5700:
34781369Sdduvall 	case DEVICE_ID_5700x:
34791369Sdduvall 	case DEVICE_ID_5701:
34801369Sdduvall 		/*
34811369Sdduvall 		 * Switch to 5700 (MAC) mode on these older chips
34821369Sdduvall 		 */
34831369Sdduvall 		ledctl &= ~LED_CONTROL_LED_MODE_MASK;
34841369Sdduvall 		ledctl |= LED_CONTROL_LED_MODE_5700;
34851369Sdduvall 		break;
34861369Sdduvall 
34871369Sdduvall 	default:
34881369Sdduvall 		break;
34891369Sdduvall 	}
34901369Sdduvall 	bge_reg_put32(bgep, ETHERNET_MAC_LED_CONTROL_REG, ledctl);
34911369Sdduvall 
34921369Sdduvall 	/*
34931369Sdduvall 	 * Step 94: activate link
34941369Sdduvall 	 */
34951369Sdduvall 	bge_reg_put32(bgep, MI_STATUS_REG, MI_STATUS_LINK);
34961369Sdduvall 
34971369Sdduvall 	/*
34981369Sdduvall 	 * Step 95: set up physical layer (PHY/SerDes)
34991369Sdduvall 	 * restart autoneg (if required)
35001369Sdduvall 	 */
35011369Sdduvall 	if (reset_phys)
35021369Sdduvall 		bge_phys_update(bgep);
35031369Sdduvall 
35041369Sdduvall 	/*
35051369Sdduvall 	 * Extra step (DSG): hand over all the Receive Buffers to the chip
35061369Sdduvall 	 */
35071369Sdduvall 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
35081369Sdduvall 		bge_mbx_put(bgep, bgep->buff[ring].chip_mbx_reg,
35091369Sdduvall 			bgep->buff[ring].rf_next);
35101369Sdduvall 
35111369Sdduvall 	/*
35121369Sdduvall 	 * MSI bits:The least significant MSI 16-bit word.
35131369Sdduvall 	 * ISR will be triggered different.
35141369Sdduvall 	 */
35151369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_MSI)
35161369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, 0x70);
35171369Sdduvall 
35181369Sdduvall 	/*
35191369Sdduvall 	 * Extra step (DSG): select which interrupts are enabled
35201369Sdduvall 	 *
35211369Sdduvall 	 * Program the Ethernet MAC engine to signal attention on
35221369Sdduvall 	 * Link Change events, then enable interrupts on MAC, DMA,
35231369Sdduvall 	 * and FLOW attention signals.
35241369Sdduvall 	 */
35251369Sdduvall 	bge_reg_set32(bgep, ETHERNET_MAC_EVENT_ENABLE_REG,
35261369Sdduvall 		ETHERNET_EVENT_LINK_INT |
35271369Sdduvall 		ETHERNET_STATUS_PCS_ERROR_INT);
35281408Srandyf #ifdef BGE_IPMI_ASF
35291408Srandyf 	if (bgep->asf_enabled) {
35301408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
35311408Srandyf 			MODE_INT_ON_FLOW_ATTN |
35321408Srandyf 			MODE_INT_ON_DMA_ATTN |
35331408Srandyf 			MODE_HOST_STACK_UP|
35341408Srandyf 			MODE_INT_ON_MAC_ATTN);
35351408Srandyf 	} else {
35361408Srandyf #endif
35371408Srandyf 		bge_reg_set32(bgep, MODE_CONTROL_REG,
35381408Srandyf 			MODE_INT_ON_FLOW_ATTN |
35391408Srandyf 			MODE_INT_ON_DMA_ATTN |
35401408Srandyf 			MODE_INT_ON_MAC_ATTN);
35411408Srandyf #ifdef BGE_IPMI_ASF
35421408Srandyf 	}
35431408Srandyf #endif
35441369Sdduvall 
35451369Sdduvall 	/*
35461369Sdduvall 	 * Step 97: enable PCI interrupts!!!
35471369Sdduvall 	 */
35481369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
35491369Sdduvall 		bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
35501369Sdduvall 		    MHCR_MASK_PCI_INT_OUTPUT);
35511369Sdduvall 
35521369Sdduvall 	/*
35531369Sdduvall 	 * All done!
35541369Sdduvall 	 */
35551369Sdduvall 	bgep->bge_chip_state = BGE_CHIP_RUNNING;
35561369Sdduvall }
35571369Sdduvall 
35581369Sdduvall 
35591369Sdduvall /*
35601369Sdduvall  * ========== Hardware interrupt handler ==========
35611369Sdduvall  */
35621369Sdduvall 
35631369Sdduvall #undef	BGE_DBG
35641369Sdduvall #define	BGE_DBG		BGE_DBG_INT	/* debug flag for this code	*/
35651369Sdduvall 
35661369Sdduvall /*
35671369Sdduvall  * Sync the status block, then atomically clear the specified bits in
35681369Sdduvall  * the <flags-and-tag> field of the status block.
35691369Sdduvall  * the <flags> word of the status block, returning the value of the
35701369Sdduvall  * <tag> and the <flags> before the bits were cleared.
35711369Sdduvall  */
35721369Sdduvall static uint64_t bge_status_sync(bge_t *bgep, uint64_t bits);
35731369Sdduvall #pragma	inline(bge_status_sync)
35741369Sdduvall 
35751369Sdduvall static uint64_t
35761369Sdduvall bge_status_sync(bge_t *bgep, uint64_t bits)
35771369Sdduvall {
35781369Sdduvall 	bge_status_t *bsp;
35791369Sdduvall 	uint64_t flags;
35801369Sdduvall 
35811369Sdduvall 	BGE_TRACE(("bge_status_sync($%p, 0x%llx)",
35821369Sdduvall 		(void *)bgep, bits));
35831369Sdduvall 
35841369Sdduvall 	ASSERT(bgep->bge_guard == BGE_GUARD);
35851369Sdduvall 
35861369Sdduvall 	DMA_SYNC(bgep->status_block, DDI_DMA_SYNC_FORKERNEL);
35871369Sdduvall 	bsp = DMA_VPTR(bgep->status_block);
35881369Sdduvall 	flags = bge_atomic_clr64(&bsp->flags_n_tag, bits);
35891369Sdduvall 
35901369Sdduvall 	BGE_DEBUG(("bge_status_sync($%p, 0x%llx) returning 0x%llx",
35911369Sdduvall 		(void *)bgep, bits, flags));
35921369Sdduvall 
35931369Sdduvall 	return (flags);
35941369Sdduvall }
35951369Sdduvall 
35961369Sdduvall static void bge_wake_factotum(bge_t *bgep);
35971369Sdduvall #pragma	inline(bge_wake_factotum)
35981369Sdduvall 
35991369Sdduvall static void
36001369Sdduvall bge_wake_factotum(bge_t *bgep)
36011369Sdduvall {
36021369Sdduvall 	mutex_enter(bgep->softintrlock);
36031369Sdduvall 	if (bgep->factotum_flag == 0) {
36041369Sdduvall 		bgep->factotum_flag = 1;
36051369Sdduvall 		ddi_trigger_softintr(bgep->factotum_id);
36061369Sdduvall 	}
36071369Sdduvall 	mutex_exit(bgep->softintrlock);
36081369Sdduvall }
36091369Sdduvall 
36101369Sdduvall /*
36111369Sdduvall  *	bge_intr() -- handle chip interrupts
36121369Sdduvall  */
36131369Sdduvall uint_t bge_intr(caddr_t arg1, caddr_t arg2);
36141369Sdduvall #pragma	no_inline(bge_intr)
36151369Sdduvall 
36161369Sdduvall uint_t
36171369Sdduvall bge_intr(caddr_t arg1, caddr_t arg2)
36181369Sdduvall {
36191369Sdduvall 	bge_t *bgep = (bge_t *)arg1;		/* private device info	*/
36201369Sdduvall 	bge_status_t *bsp;
36211369Sdduvall 	uint64_t flags;
36221369Sdduvall 	uint32_t mlcr = 0;
36231369Sdduvall 	uint_t result;
36241369Sdduvall 
36251369Sdduvall 	BGE_TRACE(("bge_intr($%p) ($%p)", arg1, arg2));
36261369Sdduvall 
36271369Sdduvall 	/*
36281369Sdduvall 	 * GLD v2 checks that s/w setup is complete before passing
36291369Sdduvall 	 * interrupts to this routine, thus eliminating the old
36301369Sdduvall 	 * (and well-known) race condition around ddi_add_intr()
36311369Sdduvall 	 */
36321369Sdduvall 	ASSERT(bgep->progress & PROGRESS_HWINT);
36331369Sdduvall 
36341369Sdduvall 	/*
36351369Sdduvall 	 * Check whether chip's says it's asserting #INTA;
36361369Sdduvall 	 * if not, don't process or claim the interrupt.
36371369Sdduvall 	 *
36381369Sdduvall 	 * Note that the PCI signal is active low, so the
36391369Sdduvall 	 * bit is *zero* when the interrupt is asserted.
36401369Sdduvall 	 */
36411369Sdduvall 	result = DDI_INTR_UNCLAIMED;
36421369Sdduvall 	mutex_enter(bgep->genlock);
36431369Sdduvall 
36441369Sdduvall 	if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
36451369Sdduvall 		mlcr = bge_reg_get32(bgep, MISC_LOCAL_CONTROL_REG);
36461369Sdduvall 
36471369Sdduvall 	BGE_DEBUG(("bge_intr($%p) ($%p) mlcr 0x%08x", arg1, arg2, mlcr));
36481369Sdduvall 
36491369Sdduvall 	if ((mlcr & MLCR_INTA_STATE) == 0) {
36501369Sdduvall 		/*
36511369Sdduvall 		 * Block further PCI interrupts ...
36521369Sdduvall 		 */
36531369Sdduvall 		result = DDI_INTR_CLAIMED;
36541369Sdduvall 
36551369Sdduvall 		if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
36561369Sdduvall 			bge_cfg_set32(bgep, PCI_CONF_BGE_MHCR,
36571369Sdduvall 				MHCR_MASK_PCI_INT_OUTPUT);
36581369Sdduvall 
36591369Sdduvall 		/*
36601369Sdduvall 		 * Sync the status block and grab the flags-n-tag from it.
36611369Sdduvall 		 * We count the number of interrupts where there doesn't
36621369Sdduvall 		 * seem to have been a DMA update of the status block; if
36631369Sdduvall 		 * it *has* been updated, the counter will be cleared in
36641369Sdduvall 		 * the while() loop below ...
36651369Sdduvall 		 */
36661369Sdduvall 		bgep->missed_dmas += 1;
36671369Sdduvall 		bsp = DMA_VPTR(bgep->status_block);
36681369Sdduvall 		flags = bge_status_sync(bgep, STATUS_FLAG_UPDATED);
36691369Sdduvall 
36701369Sdduvall 		while (flags & STATUS_FLAG_UPDATED) {
36711369Sdduvall 			/*
36721369Sdduvall 			 * Tell the chip that we're processing the interrupt
36731369Sdduvall 			 */
36741369Sdduvall 			bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
36751369Sdduvall 				INTERRUPT_MBOX_DISABLE(flags));
36761369Sdduvall 
36771369Sdduvall 			/*
36781369Sdduvall 			 * Drop the mutex while we:
36791369Sdduvall 			 * 	Receive any newly-arrived packets
36801369Sdduvall 			 *	Recycle any newly-finished send buffers
36811369Sdduvall 			 */
36821369Sdduvall 			mutex_exit(bgep->genlock);
36831369Sdduvall 			bge_receive(bgep, bsp);
36841369Sdduvall 			bge_recycle(bgep, bsp);
36851369Sdduvall 			mutex_enter(bgep->genlock);
36861369Sdduvall 
36871369Sdduvall 			/*
36881369Sdduvall 			 * Tell the chip we've finished processing, and
36891369Sdduvall 			 * give it the tag that we got from the status
36901369Sdduvall 			 * block earlier, so that it knows just how far
36911369Sdduvall 			 * we've gone.  If it's got more for us to do,
36921369Sdduvall 			 * it will now update the status block and try
36931369Sdduvall 			 * to assert an interrupt (but we've got the
36941369Sdduvall 			 * #INTA blocked at present).  If we see the
36951369Sdduvall 			 * update, we'll loop around to do some more.
36961369Sdduvall 			 * Eventually we'll get out of here ...
36971369Sdduvall 			 */
36981369Sdduvall 			bge_mbx_put(bgep, INTERRUPT_MBOX_0_REG,
36991369Sdduvall 				INTERRUPT_MBOX_ENABLE(flags));
37001369Sdduvall 			bgep->missed_dmas = 0;
37011369Sdduvall 			flags = bge_status_sync(bgep, STATUS_FLAG_UPDATED);
37021369Sdduvall 		}
37031369Sdduvall 
37041369Sdduvall 		/*
37051369Sdduvall 		 * Check for exceptional conditions that we need to handle
37061369Sdduvall 		 *
37071369Sdduvall 		 * Link status changed
37081369Sdduvall 		 * Status block not updated
37091369Sdduvall 		 */
37101369Sdduvall 		if (flags & STATUS_FLAG_LINK_CHANGED)
37111369Sdduvall 			bge_wake_factotum(bgep);
37121369Sdduvall 
37131369Sdduvall 		if (bgep->missed_dmas) {
37141369Sdduvall 			/*
37151369Sdduvall 			 * Probably due to the internal status tag not
37161369Sdduvall 			 * being reset.  Force a status block update now;
37171369Sdduvall 			 * this should ensure that we get an update and
37181369Sdduvall 			 * a new interrupt.  After that, we should be in
37191369Sdduvall 			 * sync again ...
37201369Sdduvall 			 */
37211369Sdduvall 			BGE_REPORT((bgep, "interrupt: flags 0x%llx - "
37221369Sdduvall 				"not updated?", flags));
37231369Sdduvall 			bge_reg_set32(bgep, HOST_COALESCE_MODE_REG,
37241369Sdduvall 				COALESCE_NOW);
37251369Sdduvall 
37261369Sdduvall 			if (bgep->missed_dmas >= bge_dma_miss_limit) {
37271369Sdduvall 				/*
37281369Sdduvall 				 * If this happens multiple times in a row,
37291369Sdduvall 				 * it means DMA is just not working.  Maybe
37301369Sdduvall 				 * the chip's failed, or maybe there's a
37311369Sdduvall 				 * problem on the PCI bus or in the host-PCI
37321369Sdduvall 				 * bridge (Tomatillo).
37331369Sdduvall 				 *
37341369Sdduvall 				 * At all events, we want to stop further
37351369Sdduvall 				 * interrupts and let the recovery code take
37361369Sdduvall 				 * over to see whether anything can be done
37371369Sdduvall 				 * about it ...
37381369Sdduvall 				 */
37391408Srandyf #ifdef BGE_IPMI_ASF
37401408Srandyf 				if (bgep->asf_enabled &&
37411408Srandyf 					(bgep->asf_status == ASF_STAT_RUN)) {
37421408Srandyf 					/*
37431408Srandyf 					 * We must stop ASF heart beat before
37441408Srandyf 					 * bge_chip_stop(), otherwise some
37451408Srandyf 					 * computers (ex. IBM HS20 blade
37461408Srandyf 					 * server) may crash.
37471408Srandyf 					 */
37481408Srandyf 					bge_asf_update_status(bgep);
37491408Srandyf 					bge_asf_stop_timer(bgep);
37501408Srandyf 					bgep->asf_status = ASF_STAT_STOP;
37511408Srandyf 
37521408Srandyf 					bge_asf_pre_reset_operations(bgep,
37531408Srandyf 						BGE_INIT_RESET);
37541408Srandyf 				}
37551408Srandyf #endif
37561369Sdduvall 				bge_chip_stop(bgep, B_TRUE);
37571369Sdduvall 				result = DDI_INTR_UNCLAIMED;
37581369Sdduvall 			}
37591369Sdduvall 		}
37601369Sdduvall 
37611369Sdduvall 		/*
37621369Sdduvall 		 * Reenable assertion of #INTA, unless there's a DMA fault
37631369Sdduvall 		 */
37641369Sdduvall 		if (result == DDI_INTR_CLAIMED) {
37651369Sdduvall 			if (bgep->intr_type == DDI_INTR_TYPE_FIXED)
37661369Sdduvall 				bge_cfg_clr32(bgep, PCI_CONF_BGE_MHCR,
37671369Sdduvall 					MHCR_MASK_PCI_INT_OUTPUT);
37681369Sdduvall 		}
37691369Sdduvall 	}
37701369Sdduvall 
37711369Sdduvall 	mutex_exit(bgep->genlock);
37721369Sdduvall 	return (result);
37731369Sdduvall }
37741369Sdduvall 
37751369Sdduvall /*
37761369Sdduvall  * ========== Factotum, implemented as a softint handler ==========
37771369Sdduvall  */
37781369Sdduvall 
37791369Sdduvall #undef	BGE_DBG
37801369Sdduvall #define	BGE_DBG		BGE_DBG_FACT	/* debug flag for this code	*/
37811369Sdduvall 
37821369Sdduvall static void bge_factotum_error_handler(bge_t *bgep);
37831369Sdduvall #pragma	no_inline(bge_factotum_error_handler)
37841369Sdduvall 
37851369Sdduvall static void
37861369Sdduvall bge_factotum_error_handler(bge_t *bgep)
37871369Sdduvall {
37881369Sdduvall 	uint32_t flow;
37891369Sdduvall 	uint32_t rdma;
37901369Sdduvall 	uint32_t wdma;
37911369Sdduvall 	uint32_t tmac;
37921369Sdduvall 	uint32_t rmac;
37931369Sdduvall 	uint32_t rxrs;
37941369Sdduvall 	uint32_t txrs = 0;
37951369Sdduvall 
37961369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
37971369Sdduvall 
37981369Sdduvall 	/*
37991369Sdduvall 	 * Read all the registers that show the possible
38001369Sdduvall 	 * reasons for the ERROR bit to be asserted
38011369Sdduvall 	 */
38021369Sdduvall 	flow = bge_reg_get32(bgep, FLOW_ATTN_REG);
38031369Sdduvall 	rdma = bge_reg_get32(bgep, READ_DMA_STATUS_REG);
38041369Sdduvall 	wdma = bge_reg_get32(bgep, WRITE_DMA_STATUS_REG);
38051369Sdduvall 	tmac = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
38061369Sdduvall 	rmac = bge_reg_get32(bgep, RECEIVE_MAC_STATUS_REG);
38071369Sdduvall 	rxrs = bge_reg_get32(bgep, RX_RISC_STATE_REG);
38081369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
38091369Sdduvall 		txrs = bge_reg_get32(bgep, TX_RISC_STATE_REG);
38101369Sdduvall 
38111369Sdduvall 	BGE_DEBUG(("factotum($%p) flow 0x%x rdma 0x%x wdma 0x%x",
38121369Sdduvall 		(void *)bgep, flow, rdma, wdma));
38131369Sdduvall 	BGE_DEBUG(("factotum($%p) tmac 0x%x rmac 0x%x rxrs 0x%08x txrs 0x%08x",
38141369Sdduvall 		(void *)bgep, tmac, rmac, rxrs, txrs));
38151369Sdduvall 
38161369Sdduvall 	/*
38171369Sdduvall 	 * For now, just clear all the errors ...
38181369Sdduvall 	 */
38191369Sdduvall 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
38201369Sdduvall 		bge_reg_put32(bgep, TX_RISC_STATE_REG, ~0);
38211369Sdduvall 	bge_reg_put32(bgep, RX_RISC_STATE_REG, ~0);
38221369Sdduvall 	bge_reg_put32(bgep, RECEIVE_MAC_STATUS_REG, ~0);
38231369Sdduvall 	bge_reg_put32(bgep, WRITE_DMA_STATUS_REG, ~0);
38241369Sdduvall 	bge_reg_put32(bgep, READ_DMA_STATUS_REG, ~0);
38251369Sdduvall 	bge_reg_put32(bgep, FLOW_ATTN_REG, ~0);
38261369Sdduvall }
38271369Sdduvall 
38281369Sdduvall /*
38291369Sdduvall  * Handler for hardware link state change.
38301369Sdduvall  *
38311369Sdduvall  * When this routine is called, the hardware link state has changed
38321369Sdduvall  * and the new state is reflected in the param_* variables.  Here
38331369Sdduvall  * we must update the softstate, reprogram the MAC to match, and
38341369Sdduvall  * record the change in the log and/or on the console.
38351369Sdduvall  */
38361369Sdduvall static void bge_factotum_link_handler(bge_t *bgep);
38371369Sdduvall #pragma	no_inline(bge_factotum_link_handler)
38381369Sdduvall 
38391369Sdduvall static void
38401369Sdduvall bge_factotum_link_handler(bge_t *bgep)
38411369Sdduvall {
38421369Sdduvall 	void (*logfn)(bge_t *bgep, const char *fmt, ...);
38431369Sdduvall 	const char *msg;
38441369Sdduvall 	hrtime_t deltat;
38451369Sdduvall 
38461369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
38471369Sdduvall 
38481369Sdduvall 	/*
38491369Sdduvall 	 * Update the s/w link_state
38501369Sdduvall 	 */
38511369Sdduvall 	if (bgep->param_link_up)
38521369Sdduvall 		bgep->link_state = LINK_STATE_UP;
38531369Sdduvall 	else
38541369Sdduvall 		bgep->link_state = LINK_STATE_DOWN;
38551369Sdduvall 
38561369Sdduvall 	/*
38571369Sdduvall 	 * Reprogram the MAC modes to match
38581369Sdduvall 	 */
38591369Sdduvall 	bge_sync_mac_modes(bgep);
38601369Sdduvall 
38611369Sdduvall 	/*
38621369Sdduvall 	 * Finally, we have to decide whether to write a message
38631369Sdduvall 	 * on the console or only in the log.  If the PHY has
38641369Sdduvall 	 * been reprogrammed (at user request) "recently", then
38651369Sdduvall 	 * the message only goes in the log.  Otherwise it's an
38661369Sdduvall 	 * "unexpected" event, and it goes on the console as well.
38671369Sdduvall 	 */
38681369Sdduvall 	deltat = bgep->phys_event_time - bgep->phys_write_time;
38691369Sdduvall 	if (deltat > BGE_LINK_SETTLE_TIME)
38701369Sdduvall 		msg = "";
38711369Sdduvall 	else if (bgep->param_link_up)
38721369Sdduvall 		msg = bgep->link_up_msg;
38731369Sdduvall 	else
38741369Sdduvall 		msg = bgep->link_down_msg;
38751369Sdduvall 
38761369Sdduvall 	logfn = (msg == NULL || *msg == '\0') ? bge_notice : bge_log;
38771369Sdduvall 	(*logfn)(bgep, "link %s%s", bgep->link_mode_msg, msg);
38781369Sdduvall }
38791369Sdduvall 
38801369Sdduvall static boolean_t bge_factotum_link_check(bge_t *bgep);
38811369Sdduvall #pragma	no_inline(bge_factotum_link_check)
38821369Sdduvall 
38831369Sdduvall static boolean_t
38841369Sdduvall bge_factotum_link_check(bge_t *bgep)
38851369Sdduvall {
38861369Sdduvall 	boolean_t check;
38871369Sdduvall 	uint64_t flags;
38881369Sdduvall 	uint32_t tmac_status;
38891369Sdduvall 
38901369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
38911369Sdduvall 
38921369Sdduvall 	/*
38931369Sdduvall 	 * Get & clear the writable status bits in the Tx status register
38941369Sdduvall 	 * (some bits are write-1-to-clear, others are just readonly).
38951369Sdduvall 	 */
38961369Sdduvall 	tmac_status = bge_reg_get32(bgep, TRANSMIT_MAC_STATUS_REG);
38971369Sdduvall 	bge_reg_put32(bgep, TRANSMIT_MAC_STATUS_REG, tmac_status);
38981369Sdduvall 
38991369Sdduvall 	/*
39001369Sdduvall 	 * Get & clear the ERROR and LINK_CHANGED bits from the status block
39011369Sdduvall 	 */
39021369Sdduvall 	flags = STATUS_FLAG_ERROR | STATUS_FLAG_LINK_CHANGED;
39031369Sdduvall 	flags = bge_status_sync(bgep, flags);
39041369Sdduvall 
39051369Sdduvall 	/*
39061369Sdduvall 	 * Clear any errors flagged in the status block ...
39071369Sdduvall 	 */
39081369Sdduvall 	if (flags & STATUS_FLAG_ERROR)
39091369Sdduvall 		bge_factotum_error_handler(bgep);
39101369Sdduvall 
39111369Sdduvall 	/*
39121369Sdduvall 	 * We need to check the link status if:
39131369Sdduvall 	 *	the status block says there's been a link change
39141369Sdduvall 	 *	or there's any discrepancy between the various
39151369Sdduvall 	 *	flags indicating the link state (link_state,
39161369Sdduvall 	 *	param_link_up, and the LINK STATE bit in the
39171369Sdduvall 	 *	Transmit MAC status register).
39181369Sdduvall 	 */
39191369Sdduvall 	check = (flags & STATUS_FLAG_LINK_CHANGED) != 0;
39201369Sdduvall 	switch (bgep->link_state) {
39211369Sdduvall 	case LINK_STATE_UP:
39221369Sdduvall 		check |= (bgep->param_link_up == B_FALSE);
39231369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) == 0);
39241369Sdduvall 		break;
39251369Sdduvall 
39261369Sdduvall 	case LINK_STATE_DOWN:
39271369Sdduvall 		check |= (bgep->param_link_up != B_FALSE);
39281369Sdduvall 		check |= ((tmac_status & TRANSMIT_STATUS_LINK_UP) != 0);
39291369Sdduvall 		break;
39301369Sdduvall 
39311369Sdduvall 	default:
39321369Sdduvall 		check = B_TRUE;
39331369Sdduvall 		break;
39341369Sdduvall 	}
39351369Sdduvall 
39361369Sdduvall 	/*
39371369Sdduvall 	 * If <check> is false, we're sure the link hasn't changed.
39381369Sdduvall 	 * If true, however, it's not yet definitive; we have to call
39391369Sdduvall 	 * bge_phys_check() to determine whether the link has settled
39401369Sdduvall 	 * into a new state yet ... and if it has, then call the link
39411369Sdduvall 	 * state change handler.But when the chip is 5700 in Dell 6650
39421369Sdduvall 	 * ,even if check is false, the link may have changed.So we
39431369Sdduvall 	 * have to call bge_phys_check() to determine the link state.
39441369Sdduvall 	 */
39451369Sdduvall 	if (check || bgep->chipid.device == DEVICE_ID_5700) {
39461369Sdduvall 		check = bge_phys_check(bgep);
39471369Sdduvall 		if (check)
39481369Sdduvall 			bge_factotum_link_handler(bgep);
39491369Sdduvall 	}
39501369Sdduvall 
39511369Sdduvall 	return (check);
39521369Sdduvall }
39531369Sdduvall 
39541369Sdduvall /*
39551369Sdduvall  * Factotum routine to check for Tx stall, using the 'watchdog' counter
39561369Sdduvall  */
39571369Sdduvall static boolean_t bge_factotum_stall_check(bge_t *bgep);
39581369Sdduvall #pragma	no_inline(bge_factotum_stall_check)
39591369Sdduvall 
39601369Sdduvall static boolean_t
39611369Sdduvall bge_factotum_stall_check(bge_t *bgep)
39621369Sdduvall {
39631369Sdduvall 	uint32_t dogval;
39641369Sdduvall 
39651369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
39661369Sdduvall 
39671369Sdduvall 	/*
39681369Sdduvall 	 * Specific check for Tx stall ...
39691369Sdduvall 	 *
39701369Sdduvall 	 * The 'watchdog' counter is incremented whenever a packet
39711369Sdduvall 	 * is queued, reset to 1 when some (but not all) buffers
39721369Sdduvall 	 * are reclaimed, reset to 0 (disabled) when all buffers
39731369Sdduvall 	 * are reclaimed, and shifted left here.  If it exceeds the
39741369Sdduvall 	 * threshold value, the chip is assumed to have stalled and
39751369Sdduvall 	 * is put into the ERROR state.  The factotum will then reset
39761369Sdduvall 	 * it on the next pass.
39771369Sdduvall 	 *
39781369Sdduvall 	 * All of which should ensure that we don't get into a state
39791369Sdduvall 	 * where packets are left pending indefinitely!
39801369Sdduvall 	 */
39811369Sdduvall 	dogval = bge_atomic_shl32(&bgep->watchdog, 1);
39821369Sdduvall 	if (dogval < bge_watchdog_count)
39831369Sdduvall 		return (B_FALSE);
39841369Sdduvall 
39851369Sdduvall 	BGE_REPORT((bgep, "Tx stall detected, watchdog code 0x%x", dogval));
39861369Sdduvall 	return (B_TRUE);
39871369Sdduvall }
39881369Sdduvall 
39891369Sdduvall /*
39901369Sdduvall  * The factotum is woken up when there's something to do that we'd rather
39911369Sdduvall  * not do from inside a hardware interrupt handler or high-level cyclic.
39921369Sdduvall  * Its two main tasks are:
39931369Sdduvall  *	reset & restart the chip after an error
39941369Sdduvall  *	check the link status whenever necessary
39951369Sdduvall  */
39961369Sdduvall uint_t bge_chip_factotum(caddr_t arg);
39971369Sdduvall #pragma	no_inline(bge_chip_factotum)
39981369Sdduvall 
39991369Sdduvall uint_t
40001369Sdduvall bge_chip_factotum(caddr_t arg)
40011369Sdduvall {
40021369Sdduvall 	bge_t *bgep;
40031369Sdduvall 	uint_t result;
40041369Sdduvall 	boolean_t error;
40051369Sdduvall 	boolean_t linkchg;
40061369Sdduvall 
40071369Sdduvall 	bgep = (bge_t *)arg;
40081369Sdduvall 
40091369Sdduvall 	BGE_TRACE(("bge_chip_factotum($%p)", (void *)bgep));
40101369Sdduvall 
40111369Sdduvall 	mutex_enter(bgep->softintrlock);
40121369Sdduvall 	if (bgep->factotum_flag == 0) {
40131369Sdduvall 		mutex_exit(bgep->softintrlock);
40141369Sdduvall 		return (DDI_INTR_UNCLAIMED);
40151369Sdduvall 	}
40161504Sly149593 	bgep->factotum_flag = 0;
40171369Sdduvall 	mutex_exit(bgep->softintrlock);
40181369Sdduvall 
40191369Sdduvall 	result = DDI_INTR_CLAIMED;
40201369Sdduvall 	error = B_FALSE;
40211369Sdduvall 	linkchg = B_FALSE;
40221369Sdduvall 
40231369Sdduvall 	mutex_enter(bgep->genlock);
40241369Sdduvall 	switch (bgep->bge_chip_state) {
40251369Sdduvall 	default:
40261369Sdduvall 		break;
40271369Sdduvall 
40281369Sdduvall 	case BGE_CHIP_RUNNING:
40291369Sdduvall 		linkchg = bge_factotum_link_check(bgep);
40301369Sdduvall 		error = bge_factotum_stall_check(bgep);
40311369Sdduvall 		break;
40321369Sdduvall 
40331369Sdduvall 	case BGE_CHIP_ERROR:
40341369Sdduvall 		error = B_TRUE;
40351369Sdduvall 		break;
40361369Sdduvall 
40371369Sdduvall 	case BGE_CHIP_FAULT:
40381369Sdduvall 		/*
40391369Sdduvall 		 * Fault detected, time to reset ...
40401369Sdduvall 		 */
40411369Sdduvall 		if (bge_autorecover) {
40421369Sdduvall 			BGE_REPORT((bgep, "automatic recovery activated"));
40431369Sdduvall 			bge_restart(bgep, B_FALSE);
40441408Srandyf #ifdef BGE_IPMI_ASF
40451408Srandyf 			/*
40461408Srandyf 			 * Start our ASF heartbeat counter as soon as possible.
40471408Srandyf 			 */
40481408Srandyf 			if (bgep->asf_enabled) {
40491408Srandyf 				if (bgep->asf_status != ASF_STAT_RUN) {
40501408Srandyf 					bgep->asf_timeout_id = timeout(
40511408Srandyf 						bge_asf_heartbeat,
40521408Srandyf 						(void *)bgep,
40531408Srandyf 						drv_usectohz(
40541408Srandyf 						BGE_ASF_HEARTBEAT_INTERVAL));
40551408Srandyf 					bgep->asf_status = ASF_STAT_RUN;
40561408Srandyf 				}
40571408Srandyf 			}
40581408Srandyf #endif
40591369Sdduvall 		}
40601369Sdduvall 		break;
40611369Sdduvall 	}
40621369Sdduvall 
40631369Sdduvall 	/*
40641369Sdduvall 	 * If an error is detected, stop the chip now, marking it as
40651369Sdduvall 	 * faulty, so that it will be reset next time through ...
40661369Sdduvall 	 */
40671408Srandyf 	if (error) {
40681408Srandyf #ifdef BGE_IPMI_ASF
40691408Srandyf 		if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
40701408Srandyf 			/*
40711408Srandyf 			 * We must stop ASF heart beat before bge_chip_stop(),
40721408Srandyf 			 * otherwise some computers (ex. IBM HS20 blade server)
40731408Srandyf 			 * may crash.
40741408Srandyf 			 */
40751408Srandyf 			bge_asf_update_status(bgep);
40761408Srandyf 			bge_asf_stop_timer(bgep);
40771408Srandyf 			bgep->asf_status = ASF_STAT_STOP;
40781408Srandyf 
40791408Srandyf 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
40801408Srandyf 		}
40811408Srandyf #endif
40821369Sdduvall 		bge_chip_stop(bgep, B_TRUE);
40831408Srandyf 	}
40841369Sdduvall 	mutex_exit(bgep->genlock);
40851369Sdduvall 
40861369Sdduvall 	/*
40871369Sdduvall 	 * If the link state changed, tell the world about it.
40881369Sdduvall 	 * Note: can't do this while still holding the mutex.
40891369Sdduvall 	 */
40901369Sdduvall 	if (linkchg)
40911369Sdduvall 		mac_link_update(bgep->macp, bgep->link_state);
40921369Sdduvall 
40931369Sdduvall 	return (result);
40941369Sdduvall }
40951369Sdduvall 
40961369Sdduvall /*
40971369Sdduvall  * High-level cyclic handler
40981369Sdduvall  *
40991369Sdduvall  * This routine schedules a (low-level) softint callback to the
41001369Sdduvall  * factotum, and prods the chip to update the status block (which
41011369Sdduvall  * will cause a hardware interrupt when complete).
41021369Sdduvall  */
41031369Sdduvall void bge_chip_cyclic(void *arg);
41041369Sdduvall #pragma	no_inline(bge_chip_cyclic)
41051369Sdduvall 
41061369Sdduvall void
41071369Sdduvall bge_chip_cyclic(void *arg)
41081369Sdduvall {
41091369Sdduvall 	bge_t *bgep;
41101369Sdduvall 
41111369Sdduvall 	bgep = arg;
41121369Sdduvall 
41131369Sdduvall 	switch (bgep->bge_chip_state) {
41141369Sdduvall 	default:
41151369Sdduvall 		return;
41161369Sdduvall 
41171369Sdduvall 	case BGE_CHIP_RUNNING:
41181369Sdduvall 		bge_reg_set32(bgep, HOST_COALESCE_MODE_REG, COALESCE_NOW);
41191369Sdduvall 		break;
41201369Sdduvall 
41211369Sdduvall 	case BGE_CHIP_FAULT:
41221369Sdduvall 	case BGE_CHIP_ERROR:
41231369Sdduvall 		break;
41241369Sdduvall 	}
41251369Sdduvall 
41261369Sdduvall 	bge_wake_factotum(bgep);
41271369Sdduvall }
41281369Sdduvall 
41291369Sdduvall 
41301369Sdduvall /*
41311369Sdduvall  * ========== Ioctl subfunctions ==========
41321369Sdduvall  */
41331369Sdduvall 
41341369Sdduvall #undef	BGE_DBG
41351369Sdduvall #define	BGE_DBG		BGE_DBG_PPIO	/* debug flag for this code	*/
41361369Sdduvall 
41371369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
41381369Sdduvall 
41391369Sdduvall static void bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
41401369Sdduvall #pragma	no_inline(bge_chip_peek_cfg)
41411369Sdduvall 
41421369Sdduvall static void
41431369Sdduvall bge_chip_peek_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
41441369Sdduvall {
41451369Sdduvall 	uint64_t regval;
41461369Sdduvall 	uint64_t regno;
41471369Sdduvall 
41481369Sdduvall 	BGE_TRACE(("bge_chip_peek_cfg($%p, $%p)",
41491369Sdduvall 		(void *)bgep, (void *)ppd));
41501369Sdduvall 
41511369Sdduvall 	regno = ppd->pp_acc_offset;
41521369Sdduvall 
41531369Sdduvall 	switch (ppd->pp_acc_size) {
41541369Sdduvall 	case 1:
41551369Sdduvall 		regval = pci_config_get8(bgep->cfg_handle, regno);
41561369Sdduvall 		break;
41571369Sdduvall 
41581369Sdduvall 	case 2:
41591369Sdduvall 		regval = pci_config_get16(bgep->cfg_handle, regno);
41601369Sdduvall 		break;
41611369Sdduvall 
41621369Sdduvall 	case 4:
41631369Sdduvall 		regval = pci_config_get32(bgep->cfg_handle, regno);
41641369Sdduvall 		break;
41651369Sdduvall 
41661369Sdduvall 	case 8:
41671369Sdduvall 		regval = pci_config_get64(bgep->cfg_handle, regno);
41681369Sdduvall 		break;
41691369Sdduvall 	}
41701369Sdduvall 
41711369Sdduvall 	ppd->pp_acc_data = regval;
41721369Sdduvall }
41731369Sdduvall 
41741369Sdduvall static void bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd);
41751369Sdduvall #pragma	no_inline(bge_chip_poke_cfg)
41761369Sdduvall 
41771369Sdduvall static void
41781369Sdduvall bge_chip_poke_cfg(bge_t *bgep, bge_peekpoke_t *ppd)
41791369Sdduvall {
41801369Sdduvall 	uint64_t regval;
41811369Sdduvall 	uint64_t regno;
41821369Sdduvall 
41831369Sdduvall 	BGE_TRACE(("bge_chip_poke_cfg($%p, $%p)",
41841369Sdduvall 		(void *)bgep, (void *)ppd));
41851369Sdduvall 
41861369Sdduvall 	regno = ppd->pp_acc_offset;
41871369Sdduvall 	regval = ppd->pp_acc_data;
41881369Sdduvall 
41891369Sdduvall 	switch (ppd->pp_acc_size) {
41901369Sdduvall 	case 1:
41911369Sdduvall 		pci_config_put8(bgep->cfg_handle, regno, regval);
41921369Sdduvall 		break;
41931369Sdduvall 
41941369Sdduvall 	case 2:
41951369Sdduvall 		pci_config_put16(bgep->cfg_handle, regno, regval);
41961369Sdduvall 		break;
41971369Sdduvall 
41981369Sdduvall 	case 4:
41991369Sdduvall 		pci_config_put32(bgep->cfg_handle, regno, regval);
42001369Sdduvall 		break;
42011369Sdduvall 
42021369Sdduvall 	case 8:
42031369Sdduvall 		pci_config_put64(bgep->cfg_handle, regno, regval);
42041369Sdduvall 		break;
42051369Sdduvall 	}
42061369Sdduvall }
42071369Sdduvall 
42081369Sdduvall static void bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd);
42091369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
42101369Sdduvall 
42111369Sdduvall static void
42121369Sdduvall bge_chip_peek_reg(bge_t *bgep, bge_peekpoke_t *ppd)
42131369Sdduvall {
42141369Sdduvall 	uint64_t regval;
42151369Sdduvall 	void *regaddr;
42161369Sdduvall 
42171369Sdduvall 	BGE_TRACE(("bge_chip_peek_reg($%p, $%p)",
42181369Sdduvall 		(void *)bgep, (void *)ppd));
42191369Sdduvall 
42201369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
42211369Sdduvall 
42221369Sdduvall 	switch (ppd->pp_acc_size) {
42231369Sdduvall 	case 1:
42241369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
42251369Sdduvall 		break;
42261369Sdduvall 
42271369Sdduvall 	case 2:
42281369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
42291369Sdduvall 		break;
42301369Sdduvall 
42311369Sdduvall 	case 4:
42321369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
42331369Sdduvall 		break;
42341369Sdduvall 
42351369Sdduvall 	case 8:
42361369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
42371369Sdduvall 		break;
42381369Sdduvall 	}
42391369Sdduvall 
42401369Sdduvall 	ppd->pp_acc_data = regval;
42411369Sdduvall }
42421369Sdduvall 
42431369Sdduvall static void bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd);
42441369Sdduvall #pragma	no_inline(bge_chip_peek_reg)
42451369Sdduvall 
42461369Sdduvall static void
42471369Sdduvall bge_chip_poke_reg(bge_t *bgep, bge_peekpoke_t *ppd)
42481369Sdduvall {
42491369Sdduvall 	uint64_t regval;
42501369Sdduvall 	void *regaddr;
42511369Sdduvall 
42521369Sdduvall 	BGE_TRACE(("bge_chip_poke_reg($%p, $%p)",
42531369Sdduvall 		(void *)bgep, (void *)ppd));
42541369Sdduvall 
42551369Sdduvall 	regaddr = PIO_ADDR(bgep, ppd->pp_acc_offset);
42561369Sdduvall 	regval = ppd->pp_acc_data;
42571369Sdduvall 
42581369Sdduvall 	switch (ppd->pp_acc_size) {
42591369Sdduvall 	case 1:
42601369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
42611369Sdduvall 		break;
42621369Sdduvall 
42631369Sdduvall 	case 2:
42641369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
42651369Sdduvall 		break;
42661369Sdduvall 
42671369Sdduvall 	case 4:
42681369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
42691369Sdduvall 		break;
42701369Sdduvall 
42711369Sdduvall 	case 8:
42721369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
42731369Sdduvall 		break;
42741369Sdduvall 	}
42751369Sdduvall 	BGE_PCICHK(bgep);
42761369Sdduvall }
42771369Sdduvall 
42781369Sdduvall static void bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd);
42791369Sdduvall #pragma	no_inline(bge_chip_peek_nic)
42801369Sdduvall 
42811369Sdduvall static void
42821369Sdduvall bge_chip_peek_nic(bge_t *bgep, bge_peekpoke_t *ppd)
42831369Sdduvall {
42841369Sdduvall 	uint64_t regoff;
42851369Sdduvall 	uint64_t regval;
42861369Sdduvall 	void *regaddr;
42871369Sdduvall 
42881369Sdduvall 	BGE_TRACE(("bge_chip_peek_nic($%p, $%p)",
42891369Sdduvall 		(void *)bgep, (void *)ppd));
42901369Sdduvall 
42911369Sdduvall 	regoff = ppd->pp_acc_offset;
42921369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
42931369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
42941369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
42951369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
42961369Sdduvall 
42971369Sdduvall 	switch (ppd->pp_acc_size) {
42981369Sdduvall 	case 1:
42991369Sdduvall 		regval = ddi_get8(bgep->io_handle, regaddr);
43001369Sdduvall 		break;
43011369Sdduvall 
43021369Sdduvall 	case 2:
43031369Sdduvall 		regval = ddi_get16(bgep->io_handle, regaddr);
43041369Sdduvall 		break;
43051369Sdduvall 
43061369Sdduvall 	case 4:
43071369Sdduvall 		regval = ddi_get32(bgep->io_handle, regaddr);
43081369Sdduvall 		break;
43091369Sdduvall 
43101369Sdduvall 	case 8:
43111369Sdduvall 		regval = ddi_get64(bgep->io_handle, regaddr);
43121369Sdduvall 		break;
43131369Sdduvall 	}
43141369Sdduvall 
43151369Sdduvall 	ppd->pp_acc_data = regval;
43161369Sdduvall }
43171369Sdduvall 
43181369Sdduvall static void bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd);
43191369Sdduvall #pragma	no_inline(bge_chip_poke_nic)
43201369Sdduvall 
43211369Sdduvall static void
43221369Sdduvall bge_chip_poke_nic(bge_t *bgep, bge_peekpoke_t *ppd)
43231369Sdduvall {
43241369Sdduvall 	uint64_t regoff;
43251369Sdduvall 	uint64_t regval;
43261369Sdduvall 	void *regaddr;
43271369Sdduvall 
43281369Sdduvall 	BGE_TRACE(("bge_chip_poke_nic($%p, $%p)",
43291369Sdduvall 		(void *)bgep, (void *)ppd));
43301369Sdduvall 
43311369Sdduvall 	regoff = ppd->pp_acc_offset;
43321369Sdduvall 	bge_nic_setwin(bgep, regoff & ~MWBAR_GRANULE_MASK);
43331369Sdduvall 	regoff &= MWBAR_GRANULE_MASK;
43341369Sdduvall 	regoff += NIC_MEM_WINDOW_OFFSET;
43351369Sdduvall 	regaddr = PIO_ADDR(bgep, regoff);
43361369Sdduvall 	regval = ppd->pp_acc_data;
43371369Sdduvall 
43381369Sdduvall 	switch (ppd->pp_acc_size) {
43391369Sdduvall 	case 1:
43401369Sdduvall 		ddi_put8(bgep->io_handle, regaddr, regval);
43411369Sdduvall 		break;
43421369Sdduvall 
43431369Sdduvall 	case 2:
43441369Sdduvall 		ddi_put16(bgep->io_handle, regaddr, regval);
43451369Sdduvall 		break;
43461369Sdduvall 
43471369Sdduvall 	case 4:
43481369Sdduvall 		ddi_put32(bgep->io_handle, regaddr, regval);
43491369Sdduvall 		break;
43501369Sdduvall 
43511369Sdduvall 	case 8:
43521369Sdduvall 		ddi_put64(bgep->io_handle, regaddr, regval);
43531369Sdduvall 		break;
43541369Sdduvall 	}
43551369Sdduvall 	BGE_PCICHK(bgep);
43561369Sdduvall }
43571369Sdduvall 
43581369Sdduvall static void bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd);
43591369Sdduvall #pragma	no_inline(bge_chip_peek_mii)
43601369Sdduvall 
43611369Sdduvall static void
43621369Sdduvall bge_chip_peek_mii(bge_t *bgep, bge_peekpoke_t *ppd)
43631369Sdduvall {
43641369Sdduvall 	BGE_TRACE(("bge_chip_peek_mii($%p, $%p)",
43651369Sdduvall 		(void *)bgep, (void *)ppd));
43661369Sdduvall 
43671369Sdduvall 	ppd->pp_acc_data = bge_mii_get16(bgep, ppd->pp_acc_offset/2);
43681369Sdduvall }
43691369Sdduvall 
43701369Sdduvall static void bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd);
43711369Sdduvall #pragma	no_inline(bge_chip_poke_mii)
43721369Sdduvall 
43731369Sdduvall static void
43741369Sdduvall bge_chip_poke_mii(bge_t *bgep, bge_peekpoke_t *ppd)
43751369Sdduvall {
43761369Sdduvall 	BGE_TRACE(("bge_chip_poke_mii($%p, $%p)",
43771369Sdduvall 		(void *)bgep, (void *)ppd));
43781369Sdduvall 
43791369Sdduvall 	bge_mii_put16(bgep, ppd->pp_acc_offset/2, ppd->pp_acc_data);
43801369Sdduvall }
43811369Sdduvall 
43821369Sdduvall #if	BGE_SEE_IO32
43831369Sdduvall 
43841369Sdduvall static void bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
43851369Sdduvall #pragma	no_inline(bge_chip_peek_seeprom)
43861369Sdduvall 
43871369Sdduvall static void
43881369Sdduvall bge_chip_peek_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
43891369Sdduvall {
43901369Sdduvall 	uint32_t data;
43911369Sdduvall 	int err;
43921369Sdduvall 
43931369Sdduvall 	BGE_TRACE(("bge_chip_peek_seeprom($%p, $%p)",
43941369Sdduvall 		(void *)bgep, (void *)ppd));
43951369Sdduvall 
43961369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_SEE_READ, ppd->pp_acc_offset, &data);
43971369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
43981369Sdduvall }
43991369Sdduvall 
44001369Sdduvall static void bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd);
44011369Sdduvall #pragma	no_inline(bge_chip_poke_seeprom)
44021369Sdduvall 
44031369Sdduvall static void
44041369Sdduvall bge_chip_poke_seeprom(bge_t *bgep, bge_peekpoke_t *ppd)
44051369Sdduvall {
44061369Sdduvall 	uint32_t data;
44071369Sdduvall 
44081369Sdduvall 	BGE_TRACE(("bge_chip_poke_seeprom($%p, $%p)",
44091369Sdduvall 		(void *)bgep, (void *)ppd));
44101369Sdduvall 
44111369Sdduvall 	data = ppd->pp_acc_data;
44121369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_SEE_WRITE, ppd->pp_acc_offset, &data);
44131369Sdduvall }
44141369Sdduvall #endif	/* BGE_SEE_IO32 */
44151369Sdduvall 
44161369Sdduvall #if	BGE_FLASH_IO32
44171369Sdduvall 
44181369Sdduvall static void bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd);
44191369Sdduvall #pragma	no_inline(bge_chip_peek_flash)
44201369Sdduvall 
44211369Sdduvall static void
44221369Sdduvall bge_chip_peek_flash(bge_t *bgep, bge_peekpoke_t *ppd)
44231369Sdduvall {
44241369Sdduvall 	uint32_t data;
44251369Sdduvall 	int err;
44261369Sdduvall 
44271369Sdduvall 	BGE_TRACE(("bge_chip_peek_flash($%p, $%p)",
44281369Sdduvall 		(void *)bgep, (void *)ppd));
44291369Sdduvall 
44301369Sdduvall 	err = bge_nvmem_rw32(bgep, BGE_FLASH_READ, ppd->pp_acc_offset, &data);
44311369Sdduvall 	ppd->pp_acc_data = err ? ~0ull : data;
44321369Sdduvall }
44331369Sdduvall 
44341369Sdduvall static void bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd);
44351369Sdduvall #pragma	no_inline(bge_chip_poke_flash)
44361369Sdduvall 
44371369Sdduvall static void
44381369Sdduvall bge_chip_poke_flash(bge_t *bgep, bge_peekpoke_t *ppd)
44391369Sdduvall {
44401369Sdduvall 	uint32_t data;
44411369Sdduvall 
44421369Sdduvall 	BGE_TRACE(("bge_chip_poke_flash($%p, $%p)",
44431369Sdduvall 		(void *)bgep, (void *)ppd));
44441369Sdduvall 
44451369Sdduvall 	data = ppd->pp_acc_data;
44461369Sdduvall 	(void) bge_nvmem_rw32(bgep, BGE_FLASH_WRITE,
44471369Sdduvall 	    ppd->pp_acc_offset, &data);
44481369Sdduvall }
44491369Sdduvall #endif	/* BGE_FLASH_IO32 */
44501369Sdduvall 
44511369Sdduvall static void bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd);
44521369Sdduvall #pragma	no_inline(bge_chip_peek_mem)
44531369Sdduvall 
44541369Sdduvall static void
44551369Sdduvall bge_chip_peek_mem(bge_t *bgep, bge_peekpoke_t *ppd)
44561369Sdduvall {
44571369Sdduvall 	uint64_t regval;
44581369Sdduvall 	void *vaddr;
44591369Sdduvall 
44601369Sdduvall 	BGE_TRACE(("bge_chip_peek_bge($%p, $%p)",
44611369Sdduvall 		(void *)bgep, (void *)ppd));
44621369Sdduvall 
44631369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
44641369Sdduvall 
44651369Sdduvall 	switch (ppd->pp_acc_size) {
44661369Sdduvall 	case 1:
44671369Sdduvall 		regval = *(uint8_t *)vaddr;
44681369Sdduvall 		break;
44691369Sdduvall 
44701369Sdduvall 	case 2:
44711369Sdduvall 		regval = *(uint16_t *)vaddr;
44721369Sdduvall 		break;
44731369Sdduvall 
44741369Sdduvall 	case 4:
44751369Sdduvall 		regval = *(uint32_t *)vaddr;
44761369Sdduvall 		break;
44771369Sdduvall 
44781369Sdduvall 	case 8:
44791369Sdduvall 		regval = *(uint64_t *)vaddr;
44801369Sdduvall 		break;
44811369Sdduvall 	}
44821369Sdduvall 
44831369Sdduvall 	BGE_DEBUG(("bge_chip_peek_mem($%p, $%p) peeked 0x%llx from $%p",
44841369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
44851369Sdduvall 
44861369Sdduvall 	ppd->pp_acc_data = regval;
44871369Sdduvall }
44881369Sdduvall 
44891369Sdduvall static void bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd);
44901369Sdduvall #pragma	no_inline(bge_chip_poke_mem)
44911369Sdduvall 
44921369Sdduvall static void
44931369Sdduvall bge_chip_poke_mem(bge_t *bgep, bge_peekpoke_t *ppd)
44941369Sdduvall {
44951369Sdduvall 	uint64_t regval;
44961369Sdduvall 	void *vaddr;
44971369Sdduvall 
44981369Sdduvall 	BGE_TRACE(("bge_chip_poke_mem($%p, $%p)",
44991369Sdduvall 		(void *)bgep, (void *)ppd));
45001369Sdduvall 
45011369Sdduvall 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
45021369Sdduvall 	regval = ppd->pp_acc_data;
45031369Sdduvall 
45041369Sdduvall 	BGE_DEBUG(("bge_chip_poke_mem($%p, $%p) poking 0x%llx at $%p",
45051369Sdduvall 		(void *)bgep, (void *)ppd, regval, vaddr));
45061369Sdduvall 
45071369Sdduvall 	switch (ppd->pp_acc_size) {
45081369Sdduvall 	case 1:
45091369Sdduvall 		*(uint8_t *)vaddr = (uint8_t)regval;
45101369Sdduvall 		break;
45111369Sdduvall 
45121369Sdduvall 	case 2:
45131369Sdduvall 		*(uint16_t *)vaddr = (uint16_t)regval;
45141369Sdduvall 		break;
45151369Sdduvall 
45161369Sdduvall 	case 4:
45171369Sdduvall 		*(uint32_t *)vaddr = (uint32_t)regval;
45181369Sdduvall 		break;
45191369Sdduvall 
45201369Sdduvall 	case 8:
45211369Sdduvall 		*(uint64_t *)vaddr = (uint64_t)regval;
45221369Sdduvall 		break;
45231369Sdduvall 	}
45241369Sdduvall }
45251369Sdduvall 
45261369Sdduvall static enum ioc_reply bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
45271369Sdduvall 					struct iocblk *iocp);
45281369Sdduvall #pragma	no_inline(bge_pp_ioctl)
45291369Sdduvall 
45301369Sdduvall static enum ioc_reply
45311369Sdduvall bge_pp_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
45321369Sdduvall {
45331369Sdduvall 	void (*ppfn)(bge_t *bgep, bge_peekpoke_t *ppd);
45341369Sdduvall 	bge_peekpoke_t *ppd;
45351369Sdduvall 	dma_area_t *areap;
45361369Sdduvall 	uint64_t sizemask;
45371369Sdduvall 	uint64_t mem_va;
45381369Sdduvall 	uint64_t maxoff;
45391369Sdduvall 	boolean_t peek;
45401369Sdduvall 
45411369Sdduvall 	switch (cmd) {
45421369Sdduvall 	default:
45431369Sdduvall 		/* NOTREACHED */
45441369Sdduvall 		bge_error(bgep, "bge_pp_ioctl: invalid cmd 0x%x", cmd);
45451369Sdduvall 		return (IOC_INVAL);
45461369Sdduvall 
45471369Sdduvall 	case BGE_PEEK:
45481369Sdduvall 		peek = B_TRUE;
45491369Sdduvall 		break;
45501369Sdduvall 
45511369Sdduvall 	case BGE_POKE:
45521369Sdduvall 		peek = B_FALSE;
45531369Sdduvall 		break;
45541369Sdduvall 	}
45551369Sdduvall 
45561369Sdduvall 	/*
45571369Sdduvall 	 * Validate format of ioctl
45581369Sdduvall 	 */
45591369Sdduvall 	if (iocp->ioc_count != sizeof (bge_peekpoke_t))
45601369Sdduvall 		return (IOC_INVAL);
45611369Sdduvall 	if (mp->b_cont == NULL)
45621369Sdduvall 		return (IOC_INVAL);
45631369Sdduvall 	ppd = (bge_peekpoke_t *)mp->b_cont->b_rptr;
45641369Sdduvall 
45651369Sdduvall 	/*
45661369Sdduvall 	 * Validate request parameters
45671369Sdduvall 	 */
45681369Sdduvall 	switch (ppd->pp_acc_space) {
45691369Sdduvall 	default:
45701369Sdduvall 		return (IOC_INVAL);
45711369Sdduvall 
45721369Sdduvall 	case BGE_PP_SPACE_CFG:
45731369Sdduvall 		/*
45741369Sdduvall 		 * Config space
45751369Sdduvall 		 */
45761369Sdduvall 		sizemask = 8|4|2|1;
45771369Sdduvall 		mem_va = 0;
45781369Sdduvall 		maxoff = PCI_CONF_HDR_SIZE;
45791369Sdduvall 		ppfn = peek ? bge_chip_peek_cfg : bge_chip_poke_cfg;
45801369Sdduvall 		break;
45811369Sdduvall 
45821369Sdduvall 	case BGE_PP_SPACE_REG:
45831369Sdduvall 		/*
45841369Sdduvall 		 * Memory-mapped I/O space
45851369Sdduvall 		 */
45861369Sdduvall 		sizemask = 8|4|2|1;
45871369Sdduvall 		mem_va = 0;
45881369Sdduvall 		maxoff = RIAAR_REGISTER_MAX;
45891369Sdduvall 		ppfn = peek ? bge_chip_peek_reg : bge_chip_poke_reg;
45901369Sdduvall 		break;
45911369Sdduvall 
45921369Sdduvall 	case BGE_PP_SPACE_NIC:
45931369Sdduvall 		/*
45941369Sdduvall 		 * NIC on-chip memory
45951369Sdduvall 		 */
45961369Sdduvall 		sizemask = 8|4|2|1;
45971369Sdduvall 		mem_va = 0;
45981369Sdduvall 		maxoff = MWBAR_ONCHIP_MAX;
45991369Sdduvall 		ppfn = peek ? bge_chip_peek_nic : bge_chip_poke_nic;
46001369Sdduvall 		break;
46011369Sdduvall 
46021369Sdduvall 	case BGE_PP_SPACE_MII:
46031369Sdduvall 		/*
46041369Sdduvall 		 * PHY's MII registers
46051369Sdduvall 		 * NB: all PHY registers are two bytes, but the
46061369Sdduvall 		 * addresses increment in ones (word addressing).
46071369Sdduvall 		 * So we scale the address here, then undo the
46081369Sdduvall 		 * transformation inside the peek/poke functions.
46091369Sdduvall 		 */
46101369Sdduvall 		ppd->pp_acc_offset *= 2;
46111369Sdduvall 		sizemask = 2;
46121369Sdduvall 		mem_va = 0;
46131369Sdduvall 		maxoff = (MII_MAXREG+1)*2;
46141369Sdduvall 		ppfn = peek ? bge_chip_peek_mii : bge_chip_poke_mii;
46151369Sdduvall 		break;
46161369Sdduvall 
46171369Sdduvall #if	BGE_SEE_IO32
46181369Sdduvall 	case BGE_PP_SPACE_SEEPROM:
46191369Sdduvall 		/*
46201369Sdduvall 		 * Attached SEEPROM(s), if any.
46211369Sdduvall 		 * NB: we use the high-order bits of the 'address' as
46221369Sdduvall 		 * a device select to accommodate multiple SEEPROMS,
46231369Sdduvall 		 * If each one is the maximum size (64kbytes), this
46241369Sdduvall 		 * makes them appear contiguous.  Otherwise, there may
46251369Sdduvall 		 * be holes in the mapping.  ENxS doesn't have any
46261369Sdduvall 		 * SEEPROMs anyway ...
46271369Sdduvall 		 */
46281369Sdduvall 		sizemask = 4;
46291369Sdduvall 		mem_va = 0;
46301369Sdduvall 		maxoff = SEEPROM_DEV_AND_ADDR_MASK;
46311369Sdduvall 		ppfn = peek ? bge_chip_peek_seeprom : bge_chip_poke_seeprom;
46321369Sdduvall 		break;
46331369Sdduvall #endif	/* BGE_SEE_IO32 */
46341369Sdduvall 
46351369Sdduvall #if	BGE_FLASH_IO32
46361369Sdduvall 	case BGE_PP_SPACE_FLASH:
46371369Sdduvall 		/*
46381369Sdduvall 		 * Attached Flash device (if any); a maximum of one device
46391369Sdduvall 		 * is currently supported.  But it can be up to 1MB (unlike
46401369Sdduvall 		 * the 64k limit on SEEPROMs) so why would you need more ;-)
46411369Sdduvall 		 */
46421369Sdduvall 		sizemask = 4;
46431369Sdduvall 		mem_va = 0;
46441369Sdduvall 		maxoff = NVM_FLASH_ADDR_MASK;
46451369Sdduvall 		ppfn = peek ? bge_chip_peek_flash : bge_chip_poke_flash;
46461369Sdduvall 		break;
46471369Sdduvall #endif	/* BGE_FLASH_IO32 */
46481369Sdduvall 
46491369Sdduvall 	case BGE_PP_SPACE_BGE:
46501369Sdduvall 		/*
46511369Sdduvall 		 * BGE data structure!
46521369Sdduvall 		 */
46531369Sdduvall 		sizemask = 8|4|2|1;
46541369Sdduvall 		mem_va = (uintptr_t)bgep;
46551369Sdduvall 		maxoff = sizeof (*bgep);
46561369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
46571369Sdduvall 		break;
46581369Sdduvall 
46591369Sdduvall 	case BGE_PP_SPACE_STATUS:
46601369Sdduvall 	case BGE_PP_SPACE_STATISTICS:
46611369Sdduvall 	case BGE_PP_SPACE_TXDESC:
46621369Sdduvall 	case BGE_PP_SPACE_TXBUFF:
46631369Sdduvall 	case BGE_PP_SPACE_RXDESC:
46641369Sdduvall 	case BGE_PP_SPACE_RXBUFF:
46651369Sdduvall 		/*
46661369Sdduvall 		 * Various DMA_AREAs
46671369Sdduvall 		 */
46681369Sdduvall 		switch (ppd->pp_acc_space) {
46691369Sdduvall 		case BGE_PP_SPACE_TXDESC:
46701369Sdduvall 			areap = &bgep->tx_desc;
46711369Sdduvall 			break;
46721369Sdduvall 		case BGE_PP_SPACE_TXBUFF:
46731369Sdduvall 			areap = &bgep->tx_buff[0];
46741369Sdduvall 			break;
46751369Sdduvall 		case BGE_PP_SPACE_RXDESC:
46761369Sdduvall 			areap = &bgep->rx_desc[0];
46771369Sdduvall 			break;
46781369Sdduvall 		case BGE_PP_SPACE_RXBUFF:
46791369Sdduvall 			areap = &bgep->rx_buff[0];
46801369Sdduvall 			break;
46811369Sdduvall 		case BGE_PP_SPACE_STATUS:
46821369Sdduvall 			areap = &bgep->status_block;
46831369Sdduvall 			break;
46841369Sdduvall 		case BGE_PP_SPACE_STATISTICS:
46851369Sdduvall 			if (bgep->chipid.statistic_type == BGE_STAT_BLK)
46861369Sdduvall 				areap = &bgep->statistics;
46871369Sdduvall 			break;
46881369Sdduvall 		}
46891369Sdduvall 
46901369Sdduvall 		sizemask = 8|4|2|1;
46911369Sdduvall 		mem_va = (uintptr_t)areap->mem_va;
46921369Sdduvall 		maxoff = areap->alength;
46931369Sdduvall 		ppfn = peek ? bge_chip_peek_mem : bge_chip_poke_mem;
46941369Sdduvall 		break;
46951369Sdduvall 	}
46961369Sdduvall 
46971369Sdduvall 	switch (ppd->pp_acc_size) {
46981369Sdduvall 	default:
46991369Sdduvall 		return (IOC_INVAL);
47001369Sdduvall 
47011369Sdduvall 	case 8:
47021369Sdduvall 	case 4:
47031369Sdduvall 	case 2:
47041369Sdduvall 	case 1:
47051369Sdduvall 		if ((ppd->pp_acc_size & sizemask) == 0)
47061369Sdduvall 			return (IOC_INVAL);
47071369Sdduvall 		break;
47081369Sdduvall 	}
47091369Sdduvall 
47101369Sdduvall 	if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
47111369Sdduvall 		return (IOC_INVAL);
47121369Sdduvall 
47131369Sdduvall 	if (ppd->pp_acc_offset >= maxoff)
47141369Sdduvall 		return (IOC_INVAL);
47151369Sdduvall 
47161369Sdduvall 	if (ppd->pp_acc_offset+ppd->pp_acc_size > maxoff)
47171369Sdduvall 		return (IOC_INVAL);
47181369Sdduvall 
47191369Sdduvall 	/*
47201369Sdduvall 	 * All OK - go do it!
47211369Sdduvall 	 */
47221369Sdduvall 	ppd->pp_acc_offset += mem_va;
47231369Sdduvall 	(*ppfn)(bgep, ppd);
47241369Sdduvall 	return (peek ? IOC_REPLY : IOC_ACK);
47251369Sdduvall }
47261369Sdduvall 
47271369Sdduvall static enum ioc_reply bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
47281369Sdduvall 					struct iocblk *iocp);
47291369Sdduvall #pragma	no_inline(bge_diag_ioctl)
47301369Sdduvall 
47311369Sdduvall static enum ioc_reply
47321369Sdduvall bge_diag_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
47331369Sdduvall {
47341369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
47351369Sdduvall 
47361369Sdduvall 	switch (cmd) {
47371369Sdduvall 	default:
47381369Sdduvall 		/* NOTREACHED */
47391369Sdduvall 		bge_error(bgep, "bge_diag_ioctl: invalid cmd 0x%x", cmd);
47401369Sdduvall 		return (IOC_INVAL);
47411369Sdduvall 
47421369Sdduvall 	case BGE_DIAG:
47431369Sdduvall 		/*
47441369Sdduvall 		 * Currently a no-op
47451369Sdduvall 		 */
47461369Sdduvall 		return (IOC_ACK);
47471369Sdduvall 
47481369Sdduvall 	case BGE_PEEK:
47491369Sdduvall 	case BGE_POKE:
47501369Sdduvall 		return (bge_pp_ioctl(bgep, cmd, mp, iocp));
47511369Sdduvall 
47521369Sdduvall 	case BGE_PHY_RESET:
47531369Sdduvall 		return (IOC_RESTART_ACK);
47541369Sdduvall 
47551369Sdduvall 	case BGE_SOFT_RESET:
47561369Sdduvall 	case BGE_HARD_RESET:
47571369Sdduvall 		/*
47581369Sdduvall 		 * Reset and reinitialise the 570x hardware
47591369Sdduvall 		 */
47601369Sdduvall 		bge_restart(bgep, cmd == BGE_HARD_RESET);
47611369Sdduvall 		return (IOC_ACK);
47621369Sdduvall 	}
47631369Sdduvall 
47641369Sdduvall 	/* NOTREACHED */
47651369Sdduvall }
47661369Sdduvall 
47671369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
47681369Sdduvall 
47691369Sdduvall static enum ioc_reply bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
47701369Sdduvall 				    struct iocblk *iocp);
47711369Sdduvall #pragma	no_inline(bge_mii_ioctl)
47721369Sdduvall 
47731369Sdduvall static enum ioc_reply
47741369Sdduvall bge_mii_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
47751369Sdduvall {
47761369Sdduvall 	struct bge_mii_rw *miirwp;
47771369Sdduvall 
47781369Sdduvall 	/*
47791369Sdduvall 	 * Validate format of ioctl
47801369Sdduvall 	 */
47811369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_mii_rw))
47821369Sdduvall 		return (IOC_INVAL);
47831369Sdduvall 	if (mp->b_cont == NULL)
47841369Sdduvall 		return (IOC_INVAL);
47851369Sdduvall 	miirwp = (struct bge_mii_rw *)mp->b_cont->b_rptr;
47861369Sdduvall 
47871369Sdduvall 	/*
47881369Sdduvall 	 * Validate request parameters ...
47891369Sdduvall 	 */
47901369Sdduvall 	if (miirwp->mii_reg > MII_MAXREG)
47911369Sdduvall 		return (IOC_INVAL);
47921369Sdduvall 
47931369Sdduvall 	switch (cmd) {
47941369Sdduvall 	default:
47951369Sdduvall 		/* NOTREACHED */
47961369Sdduvall 		bge_error(bgep, "bge_mii_ioctl: invalid cmd 0x%x", cmd);
47971369Sdduvall 		return (IOC_INVAL);
47981369Sdduvall 
47991369Sdduvall 	case BGE_MII_READ:
48001369Sdduvall 		miirwp->mii_data = bge_mii_get16(bgep, miirwp->mii_reg);
48011369Sdduvall 		return (IOC_REPLY);
48021369Sdduvall 
48031369Sdduvall 	case BGE_MII_WRITE:
48041369Sdduvall 		bge_mii_put16(bgep, miirwp->mii_reg, miirwp->mii_data);
48051369Sdduvall 		return (IOC_ACK);
48061369Sdduvall 	}
48071369Sdduvall 
48081369Sdduvall 	/* NOTREACHED */
48091369Sdduvall }
48101369Sdduvall 
48111369Sdduvall #if	BGE_SEE_IO32
48121369Sdduvall 
48131369Sdduvall static enum ioc_reply bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
48141369Sdduvall 				    struct iocblk *iocp);
48151369Sdduvall #pragma	no_inline(bge_see_ioctl)
48161369Sdduvall 
48171369Sdduvall static enum ioc_reply
48181369Sdduvall bge_see_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
48191369Sdduvall {
48201369Sdduvall 	struct bge_see_rw *seerwp;
48211369Sdduvall 
48221369Sdduvall 	/*
48231369Sdduvall 	 * Validate format of ioctl
48241369Sdduvall 	 */
48251369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_see_rw))
48261369Sdduvall 		return (IOC_INVAL);
48271369Sdduvall 	if (mp->b_cont == NULL)
48281369Sdduvall 		return (IOC_INVAL);
48291369Sdduvall 	seerwp = (struct bge_see_rw *)mp->b_cont->b_rptr;
48301369Sdduvall 
48311369Sdduvall 	/*
48321369Sdduvall 	 * Validate request parameters ...
48331369Sdduvall 	 */
48341369Sdduvall 	if (seerwp->see_addr & ~SEEPROM_DEV_AND_ADDR_MASK)
48351369Sdduvall 		return (IOC_INVAL);
48361369Sdduvall 
48371369Sdduvall 	switch (cmd) {
48381369Sdduvall 	default:
48391369Sdduvall 		/* NOTREACHED */
48401369Sdduvall 		bge_error(bgep, "bge_see_ioctl: invalid cmd 0x%x", cmd);
48411369Sdduvall 		return (IOC_INVAL);
48421369Sdduvall 
48431369Sdduvall 	case BGE_SEE_READ:
48441369Sdduvall 	case BGE_SEE_WRITE:
48451369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
48461369Sdduvall 		    seerwp->see_addr, &seerwp->see_data);
48471369Sdduvall 		return (IOC_REPLY);
48481369Sdduvall 	}
48491369Sdduvall 
48501369Sdduvall 	/* NOTREACHED */
48511369Sdduvall }
48521369Sdduvall 
48531369Sdduvall #endif	/* BGE_SEE_IO32 */
48541369Sdduvall 
48551369Sdduvall #if	BGE_FLASH_IO32
48561369Sdduvall 
48571369Sdduvall static enum ioc_reply bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp,
48581369Sdduvall 				    struct iocblk *iocp);
48591369Sdduvall #pragma	no_inline(bge_flash_ioctl)
48601369Sdduvall 
48611369Sdduvall static enum ioc_reply
48621369Sdduvall bge_flash_ioctl(bge_t *bgep, int cmd, mblk_t *mp, struct iocblk *iocp)
48631369Sdduvall {
48641369Sdduvall 	struct bge_flash_rw *flashrwp;
48651369Sdduvall 
48661369Sdduvall 	/*
48671369Sdduvall 	 * Validate format of ioctl
48681369Sdduvall 	 */
48691369Sdduvall 	if (iocp->ioc_count != sizeof (struct bge_flash_rw))
48701369Sdduvall 		return (IOC_INVAL);
48711369Sdduvall 	if (mp->b_cont == NULL)
48721369Sdduvall 		return (IOC_INVAL);
48731369Sdduvall 	flashrwp = (struct bge_flash_rw *)mp->b_cont->b_rptr;
48741369Sdduvall 
48751369Sdduvall 	/*
48761369Sdduvall 	 * Validate request parameters ...
48771369Sdduvall 	 */
48781369Sdduvall 	if (flashrwp->flash_addr & ~NVM_FLASH_ADDR_MASK)
48791369Sdduvall 		return (IOC_INVAL);
48801369Sdduvall 
48811369Sdduvall 	switch (cmd) {
48821369Sdduvall 	default:
48831369Sdduvall 		/* NOTREACHED */
48841369Sdduvall 		bge_error(bgep, "bge_flash_ioctl: invalid cmd 0x%x", cmd);
48851369Sdduvall 		return (IOC_INVAL);
48861369Sdduvall 
48871369Sdduvall 	case BGE_FLASH_READ:
48881369Sdduvall 	case BGE_FLASH_WRITE:
48891369Sdduvall 		iocp->ioc_error = bge_nvmem_rw32(bgep, cmd,
48901369Sdduvall 		    flashrwp->flash_addr, &flashrwp->flash_data);
48911369Sdduvall 		return (IOC_REPLY);
48921369Sdduvall 	}
48931369Sdduvall 
48941369Sdduvall 	/* NOTREACHED */
48951369Sdduvall }
48961369Sdduvall 
48971369Sdduvall #endif	/* BGE_FLASH_IO32 */
48981369Sdduvall 
48991369Sdduvall enum ioc_reply bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp,
49001369Sdduvall 				struct iocblk *iocp);
49011369Sdduvall #pragma	no_inline(bge_chip_ioctl)
49021369Sdduvall 
49031369Sdduvall enum ioc_reply
49041369Sdduvall bge_chip_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
49051369Sdduvall {
49061369Sdduvall 	int cmd;
49071369Sdduvall 
49081369Sdduvall 	BGE_TRACE(("bge_chip_ioctl($%p, $%p, $%p, $%p)",
49091369Sdduvall 		(void *)bgep, (void *)wq, (void *)mp, (void *)iocp));
49101369Sdduvall 
49111369Sdduvall 	ASSERT(mutex_owned(bgep->genlock));
49121369Sdduvall 
49131369Sdduvall 	cmd = iocp->ioc_cmd;
49141369Sdduvall 	switch (cmd) {
49151369Sdduvall 	default:
49161369Sdduvall 		/* NOTREACHED */
49171369Sdduvall 		bge_error(bgep, "bge_chip_ioctl: invalid cmd 0x%x", cmd);
49181369Sdduvall 		return (IOC_INVAL);
49191369Sdduvall 
49201369Sdduvall 	case BGE_DIAG:
49211369Sdduvall 	case BGE_PEEK:
49221369Sdduvall 	case BGE_POKE:
49231369Sdduvall 	case BGE_PHY_RESET:
49241369Sdduvall 	case BGE_SOFT_RESET:
49251369Sdduvall 	case BGE_HARD_RESET:
49261369Sdduvall #if	BGE_DEBUGGING || BGE_DO_PPIO
49271369Sdduvall 		return (bge_diag_ioctl(bgep, cmd, mp, iocp));
49281369Sdduvall #else
49291369Sdduvall 		return (IOC_INVAL);
49301369Sdduvall #endif	/* BGE_DEBUGGING || BGE_DO_PPIO */
49311369Sdduvall 
49321369Sdduvall 	case BGE_MII_READ:
49331369Sdduvall 	case BGE_MII_WRITE:
49341369Sdduvall 		return (bge_mii_ioctl(bgep, cmd, mp, iocp));
49351369Sdduvall 
49361369Sdduvall #if	BGE_SEE_IO32
49371369Sdduvall 	case BGE_SEE_READ:
49381369Sdduvall 	case BGE_SEE_WRITE:
49391369Sdduvall 		return (bge_see_ioctl(bgep, cmd, mp, iocp));
49401369Sdduvall #endif	/* BGE_SEE_IO32 */
49411369Sdduvall 
49421369Sdduvall #if	BGE_FLASH_IO32
49431369Sdduvall 	case BGE_FLASH_READ:
49441369Sdduvall 	case BGE_FLASH_WRITE:
49451369Sdduvall 		return (bge_flash_ioctl(bgep, cmd, mp, iocp));
49461369Sdduvall #endif	/* BGE_FLASH_IO32 */
49471369Sdduvall 	}
49481369Sdduvall 
49491369Sdduvall 	/* NOTREACHED */
49501369Sdduvall }
49511369Sdduvall 
49521369Sdduvall void
49531369Sdduvall bge_chip_blank(void *arg, time_t ticks, uint_t count)
49541369Sdduvall {
49551369Sdduvall 	bge_t *bgep = arg;
49561369Sdduvall 
49571369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_TICKS_REG, ticks);
49581369Sdduvall 	bge_reg_put32(bgep, RCV_COALESCE_MAX_BD_REG, count);
49591369Sdduvall }
49601408Srandyf 
49611408Srandyf #ifdef BGE_IPMI_ASF
49621408Srandyf 
49631408Srandyf uint32_t
49641408Srandyf bge_nic_read32(bge_t *bgep, bge_regno_t addr)
49651408Srandyf {
49661408Srandyf 	uint32_t data;
49671408Srandyf 
49681408Srandyf 	if (!bgep->asf_wordswapped) {
49691408Srandyf 		/* a workaround word swap error */
49701408Srandyf 		if (addr & 4)
49711408Srandyf 			addr = addr - 4;
49721408Srandyf 		else
49731408Srandyf 			addr = addr + 4;
49741408Srandyf 	}
49751408Srandyf 
49761408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, addr);
49771408Srandyf 	data = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MWDAR);
49781408Srandyf 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MWBAR, 0);
49791408Srandyf 
49801408Srandyf 	return (data);
49811408Srandyf }
49821408Srandyf 
49831408Srandyf 
49841408Srandyf void
49851408Srandyf bge_asf_update_status(bge_t *bgep)
49861408Srandyf {
49871408Srandyf 	uint32_t event;
49881408Srandyf 
49891408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_ALIVE);
49901408Srandyf 	bge_nic_put32(bgep, BGE_CMD_LENGTH_MAILBOX, 4);
49911408Srandyf 	bge_nic_put32(bgep, BGE_CMD_DATA_MAILBOX,   3);
49921408Srandyf 
49931408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
49941408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
49951408Srandyf }
49961408Srandyf 
49971408Srandyf 
49981408Srandyf /*
49991408Srandyf  * The driver is supposed to notify ASF that the OS is still running
50001408Srandyf  * every three seconds, otherwise the management server may attempt
50011408Srandyf  * to reboot the machine.  If it hasn't actually failed, this is
50021408Srandyf  * not a desireable result.  However, this isn't running as a real-time
50031408Srandyf  * thread, and even if it were, it might not be able to generate the
50041408Srandyf  * heartbeat in a timely manner due to system load.  As it isn't a
50051408Srandyf  * significant strain on the machine, we will set the interval to half
50061408Srandyf  * of the required value.
50071408Srandyf  */
50081408Srandyf void
50091408Srandyf bge_asf_heartbeat(void *bgep)
50101408Srandyf {
50111408Srandyf 	bge_asf_update_status((bge_t *)bgep);
50121408Srandyf 	((bge_t *)bgep)->asf_timeout_id = timeout(bge_asf_heartbeat, bgep,
50131408Srandyf 		drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
50141408Srandyf }
50151408Srandyf 
50161408Srandyf 
50171408Srandyf void
50181408Srandyf bge_asf_stop_timer(bge_t *bgep)
50191408Srandyf {
50201408Srandyf 	timeout_id_t tmp_id = 0;
50211408Srandyf 
50221408Srandyf 	while ((bgep->asf_timeout_id != 0) &&
50231408Srandyf 		(tmp_id != bgep->asf_timeout_id)) {
50241408Srandyf 		tmp_id = bgep->asf_timeout_id;
50251408Srandyf 		(void) untimeout(tmp_id);
50261408Srandyf 	}
50271408Srandyf 	bgep->asf_timeout_id = 0;
50281408Srandyf }
50291408Srandyf 
50301408Srandyf 
50311408Srandyf 
50321408Srandyf /*
50331408Srandyf  * This function should be placed at the earliest postion of bge_attach().
50341408Srandyf  */
50351408Srandyf void
50361408Srandyf bge_asf_get_config(bge_t *bgep)
50371408Srandyf {
50381408Srandyf 	uint32_t nicsig;
50391408Srandyf 	uint32_t niccfg;
50401408Srandyf 
50411408Srandyf 	nicsig = bge_nic_read32(bgep, BGE_NIC_DATA_SIG_ADDR);
50421408Srandyf 	if (nicsig == BGE_NIC_DATA_SIG) {
50431408Srandyf 		niccfg = bge_nic_read32(bgep, BGE_NIC_DATA_NIC_CFG_ADDR);
50441408Srandyf 		if (niccfg & BGE_NIC_CFG_ENABLE_ASF)
50451408Srandyf 			/*
50461408Srandyf 			 * Here, we don't consider BAXTER, because BGE haven't
50471408Srandyf 			 * supported BAXTER (that is 5752). Also, as I know,
50481408Srandyf 			 * BAXTER doesn't support ASF feature.
50491408Srandyf 			 */
50501408Srandyf 			bgep->asf_enabled = B_TRUE;
50511408Srandyf 		else
50521408Srandyf 			bgep->asf_enabled = B_FALSE;
50531408Srandyf 	} else
50541408Srandyf 		bgep->asf_enabled = B_FALSE;
50551408Srandyf }
50561408Srandyf 
50571408Srandyf 
50581408Srandyf void
50591408Srandyf bge_asf_pre_reset_operations(bge_t *bgep, uint32_t mode)
50601408Srandyf {
50611408Srandyf 	uint32_t tries;
50621408Srandyf 	uint32_t event;
50631408Srandyf 
50641408Srandyf 	ASSERT(bgep->asf_enabled);
50651408Srandyf 
50661408Srandyf 	/* Issues "pause firmware" command and wait for ACK */
50671408Srandyf 	bge_nic_put32(bgep, BGE_CMD_MAILBOX, BGE_CMD_NICDRV_PAUSE_FW);
50681408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
50691408Srandyf 	bge_reg_put32(bgep, RX_RISC_EVENT_REG, event | RRER_ASF_EVENT);
50701408Srandyf 
50711408Srandyf 	event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
50721408Srandyf 	tries = 0;
50731408Srandyf 	while ((event & RRER_ASF_EVENT) && (tries < 100)) {
50741408Srandyf 		drv_usecwait(1);
50751408Srandyf 		tries ++;
50761408Srandyf 		event = bge_reg_get32(bgep, RX_RISC_EVENT_REG);
50771408Srandyf 	}
50781408Srandyf 
50791408Srandyf 	bge_nic_put32(bgep, BGE_FIRMWARE_MAILBOX,
50801408Srandyf 		BGE_MAGIC_NUM_FIRMWARE_INIT_DONE);
50811408Srandyf 
50821408Srandyf 	if (bgep->asf_newhandshake) {
50831408Srandyf 		switch (mode) {
50841408Srandyf 		case BGE_INIT_RESET:
50851408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
50861408Srandyf 				BGE_DRV_STATE_START);
50871408Srandyf 			break;
50881408Srandyf 		case BGE_SHUTDOWN_RESET:
50891408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
50901408Srandyf 				BGE_DRV_STATE_UNLOAD);
50911408Srandyf 			break;
50921408Srandyf 		case BGE_SUSPEND_RESET:
50931408Srandyf 			bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
50941408Srandyf 				BGE_DRV_STATE_SUSPEND);
50951408Srandyf 			break;
50961408Srandyf 		default:
50971408Srandyf 			break;
50981408Srandyf 		}
50991408Srandyf 	}
51001408Srandyf }
51011408Srandyf 
51021408Srandyf 
51031408Srandyf void
51041408Srandyf bge_asf_post_reset_old_mode(bge_t *bgep, uint32_t mode)
51051408Srandyf {
51061408Srandyf 	switch (mode) {
51071408Srandyf 	case BGE_INIT_RESET:
51081408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
51091408Srandyf 			BGE_DRV_STATE_START);
51101408Srandyf 		break;
51111408Srandyf 	case BGE_SHUTDOWN_RESET:
51121408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
51131408Srandyf 			BGE_DRV_STATE_UNLOAD);
51141408Srandyf 		break;
51151408Srandyf 	case BGE_SUSPEND_RESET:
51161408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
51171408Srandyf 			BGE_DRV_STATE_SUSPEND);
51181408Srandyf 		break;
51191408Srandyf 	default:
51201408Srandyf 		break;
51211408Srandyf 	}
51221408Srandyf }
51231408Srandyf 
51241408Srandyf 
51251408Srandyf void
51261408Srandyf bge_asf_post_reset_new_mode(bge_t *bgep, uint32_t mode)
51271408Srandyf {
51281408Srandyf 	switch (mode) {
51291408Srandyf 	case BGE_INIT_RESET:
51301408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
51311408Srandyf 			BGE_DRV_STATE_START_DONE);
51321408Srandyf 		break;
51331408Srandyf 	case BGE_SHUTDOWN_RESET:
51341408Srandyf 		bge_nic_put32(bgep, BGE_DRV_STATE_MAILBOX,
51351408Srandyf 			BGE_DRV_STATE_UNLOAD_DONE);
51361408Srandyf 		break;
51371408Srandyf 	default:
51381408Srandyf 		break;
51391408Srandyf 	}
51401408Srandyf }
51411408Srandyf 
51421408Srandyf #endif /* BGE_IPMI_ASF */
5143