xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 7607:49c460c685fa)
13526Sxy150489 /*
23526Sxy150489  * This file is provided under a CDDLv1 license.  When using or
33526Sxy150489  * redistributing this file, you may do so under this license.
43526Sxy150489  * In redistributing this file this license must be included
53526Sxy150489  * and no other modification of this header file is permitted.
63526Sxy150489  *
73526Sxy150489  * CDDL LICENSE SUMMARY
83526Sxy150489  *
95882Syy150190  * Copyright(c) 1999 - 2008 Intel Corporation. All rights reserved.
103526Sxy150489  *
113526Sxy150489  * The contents of this file are subject to the terms of Version
123526Sxy150489  * 1.0 of the Common Development and Distribution License (the "License").
133526Sxy150489  *
143526Sxy150489  * You should have received a copy of the License with this software.
153526Sxy150489  * You can obtain a copy of the License at
163526Sxy150489  *	http://www.opensolaris.org/os/licensing.
173526Sxy150489  * See the License for the specific language governing permissions
183526Sxy150489  * and limitations under the License.
193526Sxy150489  */
203526Sxy150489 
213526Sxy150489 /*
225858Scc210113  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237133Scc210113  * Use is subject to license terms of the CDDLv1.
243526Sxy150489  */
253526Sxy150489 
263526Sxy150489 /*
273526Sxy150489  * **********************************************************************
283526Sxy150489  *									*
293526Sxy150489  * Module Name:								*
303526Sxy150489  *   e1000g_main.c							*
313526Sxy150489  *									*
323526Sxy150489  * Abstract:								*
334919Sxy150489  *   This file contains the interface routines for the solaris OS.	*
344919Sxy150489  *   It has all DDI entry point routines and GLD entry point routines.	*
353526Sxy150489  *									*
364919Sxy150489  *   This file also contains routines that take care of initialization	*
374919Sxy150489  *   uninit routine and interrupt routine.				*
383526Sxy150489  *									*
393526Sxy150489  * **********************************************************************
403526Sxy150489  */
413526Sxy150489 
423526Sxy150489 #include <sys/dlpi.h>
433526Sxy150489 #include <sys/mac.h>
443526Sxy150489 #include "e1000g_sw.h"
453526Sxy150489 #include "e1000g_debug.h"
463526Sxy150489 
47*7607STed.You@Sun.COM static char ident[] = "Intel PRO/1000 Ethernet 5.2.13";
483526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
49*7607STed.You@Sun.COM static char e1000g_version[] = "Driver Ver. 5.2.13";
503526Sxy150489 
513526Sxy150489 /*
523526Sxy150489  * Proto types for DDI entry points
533526Sxy150489  */
544919Sxy150489 static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
554919Sxy150489 static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
563526Sxy150489 
573526Sxy150489 /*
583526Sxy150489  * init and intr routines prototype
593526Sxy150489  */
604919Sxy150489 static int e1000g_resume(dev_info_t *);
614919Sxy150489 static int e1000g_suspend(dev_info_t *);
623526Sxy150489 static uint_t e1000g_intr_pciexpress(caddr_t);
633526Sxy150489 static uint_t e1000g_intr(caddr_t);
643526Sxy150489 static void e1000g_intr_work(struct e1000g *, uint32_t);
653526Sxy150489 #pragma inline(e1000g_intr_work)
665882Syy150190 static uint32_t e1000g_get_itr(uint32_t, uint32_t, uint32_t);
675882Syy150190 #pragma inline(e1000g_get_itr)
683526Sxy150489 static int e1000g_init(struct e1000g *);
694919Sxy150489 static int e1000g_start(struct e1000g *, boolean_t);
704919Sxy150489 static void e1000g_stop(struct e1000g *, boolean_t);
713526Sxy150489 static int e1000g_m_start(void *);
723526Sxy150489 static void e1000g_m_stop(void *);
733526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
743526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
753526Sxy150489 static int e1000g_m_unicst(void *, const uint8_t *);
763526Sxy150489 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
773526Sxy150489 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
783526Sxy150489 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
793526Sxy150489 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
803526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
813526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
826394Scc210113 static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
836394Scc210113     uint_t, const void *);
846394Scc210113 static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
856512Ssowmini     uint_t, uint_t, void *);
866394Scc210113 static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
876394Scc210113     const void *);
886394Scc210113 static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t,
896512Ssowmini     uint_t, void *);
904919Sxy150489 static void e1000g_init_locks(struct e1000g *);
914919Sxy150489 static void e1000g_destroy_locks(struct e1000g *);
924919Sxy150489 static int e1000g_identify_hardware(struct e1000g *);
934919Sxy150489 static int e1000g_regs_map(struct e1000g *);
944919Sxy150489 static int e1000g_set_driver_params(struct e1000g *);
956394Scc210113 static void e1000g_set_bufsize(struct e1000g *);
964919Sxy150489 static int e1000g_register_mac(struct e1000g *);
974919Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *);
984919Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *);
994919Sxy150489 static void e1000g_init_unicst(struct e1000g *);
1003526Sxy150489 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
1013526Sxy150489 
1023526Sxy150489 /*
1033526Sxy150489  * Local routines
1043526Sxy150489  */
1054919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1064919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1074061Sxy150489 static void e1000g_link_timer(void *);
1084919Sxy150489 static void e1000g_local_timer(void *);
1094061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1103526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1113526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1124919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1134919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1144919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1154919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1164919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1174919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1184919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1194919Sxy150489 static void stop_link_timer(struct e1000g *);
1204919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1214919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
1224919Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *);
1234919Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
1243526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1254919Sxy150489 #ifdef E1000G_DEBUG
1264919Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
1274919Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
1284919Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
1294919Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
1304919Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
1314919Sxy150489     struct iocblk *, mblk_t *);
1324919Sxy150489 #endif
1334919Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
1344919Sxy150489     struct iocblk *, mblk_t *);
1357133Scc210113 static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
1364919Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
1374919Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *);
1384919Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *);
1394919Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *);
1404919Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *);
1414919Sxy150489 static int e1000g_add_intrs(struct e1000g *);
1424919Sxy150489 static int e1000g_intr_add(struct e1000g *, int);
1434919Sxy150489 static int e1000g_rem_intrs(struct e1000g *);
1444919Sxy150489 static int e1000g_enable_intrs(struct e1000g *);
1454919Sxy150489 static int e1000g_disable_intrs(struct e1000g *);
1464919Sxy150489 static boolean_t e1000g_link_up(struct e1000g *);
1473526Sxy150489 #ifdef __sparc
1484919Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *);
1493526Sxy150489 #endif
1505082Syy150190 static void e1000g_get_phy_state(struct e1000g *);
1514982Syy150190 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
1525273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1535273Sgl147354     const void *impl_data);
1545273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1555273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1566512Ssowmini static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
1576512Ssowmini static void e1000g_param_sync(struct e1000g *);
158*7607STed.You@Sun.COM static void e1000g_get_driver_control(struct e1000_hw *);
159*7607STed.You@Sun.COM static void e1000g_release_driver_control(struct e1000_hw *);
1606512Ssowmini 
1616512Ssowmini mac_priv_prop_t e1000g_priv_props[] = {
1626512Ssowmini 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
1636512Ssowmini 	{"_tx_interrupt_enable", MAC_PROP_PERM_RW},
1646512Ssowmini 	{"_tx_intr_delay", MAC_PROP_PERM_RW},
1656512Ssowmini 	{"_tx_intr_abs_delay", MAC_PROP_PERM_RW},
1666512Ssowmini 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
1676512Ssowmini 	{"_max_num_rcv_packets", MAC_PROP_PERM_RW},
1686512Ssowmini 	{"_rx_intr_delay", MAC_PROP_PERM_RW},
1696512Ssowmini 	{"_rx_intr_abs_delay", MAC_PROP_PERM_RW},
1706512Ssowmini 	{"_intr_throttling_rate", MAC_PROP_PERM_RW},
1716512Ssowmini 	{"_intr_adaptive", MAC_PROP_PERM_RW},
1726512Ssowmini 	{"_tx_recycle_thresh", MAC_PROP_PERM_RW},
1736512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
1746512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
1756512Ssowmini 	{"_tx_recycle_num", MAC_PROP_PERM_RW}
1766512Ssowmini };
1776512Ssowmini #define	E1000G_MAX_PRIV_PROPS	\
1786512Ssowmini 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
1796512Ssowmini 
1803526Sxy150489 
1813526Sxy150489 static struct cb_ops cb_ws_ops = {
1823526Sxy150489 	nulldev,		/* cb_open */
1833526Sxy150489 	nulldev,		/* cb_close */
1843526Sxy150489 	nodev,			/* cb_strategy */
1853526Sxy150489 	nodev,			/* cb_print */
1863526Sxy150489 	nodev,			/* cb_dump */
1873526Sxy150489 	nodev,			/* cb_read */
1883526Sxy150489 	nodev,			/* cb_write */
1893526Sxy150489 	nodev,			/* cb_ioctl */
1903526Sxy150489 	nodev,			/* cb_devmap */
1913526Sxy150489 	nodev,			/* cb_mmap */
1923526Sxy150489 	nodev,			/* cb_segmap */
1933526Sxy150489 	nochpoll,		/* cb_chpoll */
1943526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1953526Sxy150489 	NULL,			/* cb_stream */
1963526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1973526Sxy150489 	CB_REV,			/* cb_rev */
1983526Sxy150489 	nodev,			/* cb_aread */
1993526Sxy150489 	nodev			/* cb_awrite */
2003526Sxy150489 };
2013526Sxy150489 
2023526Sxy150489 static struct dev_ops ws_ops = {
2033526Sxy150489 	DEVO_REV,		/* devo_rev */
2043526Sxy150489 	0,			/* devo_refcnt */
2053526Sxy150489 	NULL,			/* devo_getinfo */
2063526Sxy150489 	nulldev,		/* devo_identify */
2073526Sxy150489 	nulldev,		/* devo_probe */
2084919Sxy150489 	e1000g_attach,		/* devo_attach */
2094919Sxy150489 	e1000g_detach,		/* devo_detach */
2103526Sxy150489 	nodev,			/* devo_reset */
2113526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
2123526Sxy150489 	NULL,			/* devo_bus_ops */
2133526Sxy150489 	ddi_power		/* devo_power */
2143526Sxy150489 };
2153526Sxy150489 
2163526Sxy150489 static struct modldrv modldrv = {
2173526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
2183526Sxy150489 	ident,			/* Discription string */
2193526Sxy150489 	&ws_ops,		/* driver ops */
2203526Sxy150489 };
2213526Sxy150489 
2223526Sxy150489 static struct modlinkage modlinkage = {
2233526Sxy150489 	MODREV_1, &modldrv, NULL
2243526Sxy150489 };
2253526Sxy150489 
2264919Sxy150489 /* Access attributes for register mapping */
2274919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
2283526Sxy150489 	DDI_DEVICE_ATTR_V0,
2293526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2303526Sxy150489 	DDI_STRICTORDER_ACC,
2315273Sgl147354 	DDI_FLAGERR_ACC
2323526Sxy150489 };
2333526Sxy150489 
2346394Scc210113 #define	E1000G_M_CALLBACK_FLAGS \
2356394Scc210113 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
2363526Sxy150489 
2373526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2383526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2393526Sxy150489 	e1000g_m_stat,
2403526Sxy150489 	e1000g_m_start,
2413526Sxy150489 	e1000g_m_stop,
2423526Sxy150489 	e1000g_m_promisc,
2433526Sxy150489 	e1000g_m_multicst,
2443526Sxy150489 	e1000g_m_unicst,
2453526Sxy150489 	e1000g_m_tx,
2465882Syy150190 	NULL,
2473526Sxy150489 	e1000g_m_ioctl,
2486394Scc210113 	e1000g_m_getcapab,
2496394Scc210113 	NULL,
2506394Scc210113 	NULL,
2516394Scc210113 	e1000g_m_setprop,
2526394Scc210113 	e1000g_m_getprop
2533526Sxy150489 };
2543526Sxy150489 
2553526Sxy150489 /*
2563526Sxy150489  * Global variables
2573526Sxy150489  */
2583526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2593526Sxy150489 /*
2604894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2614349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2624349Sxy150489  * enabled. If we force the driver to detach while there are still some
2634349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2644349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2654349Sxy150489  * structure will be freed after the driver is detached. However when we
2664349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2674349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2684894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2694894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2704894Syy150190  * after the system board level DR operation. For this reason, the global
2714894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2724349Sxy150489  */
2734894Syy150190 #ifdef __sparc
2744894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2754894Syy150190 #else
2764894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2774894Syy150190 #endif
2784349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2794894Syy150190 
2804349Sxy150489 /*
2813526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2823526Sxy150489  * and the rx packets release in detach processing to make them mutually
2833526Sxy150489  * exclusive.
2843526Sxy150489  * The rx recycling processes different rx packets in different threads,
2853526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2863526Sxy150489  * recycling threads.
2873526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2883526Sxy150489  * it mutually exclusive with the rx recycling.
2893526Sxy150489  */
2903526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2913526Sxy150489 /*
2923526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2933526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2943526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2953526Sxy150489  * resources during the initialization of the instances, then the flag will
2963526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2973526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2983526Sxy150489  */
2993526Sxy150489 krwlock_t e1000g_dma_type_lock;
3003526Sxy150489 
3017133Scc210113 /*
3027133Scc210113  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
3037133Scc210113  * Based on the information from Intel, the 82546 chipset has some hardware
3047133Scc210113  * problem. When one port is being reset and the other port is trying to
3057133Scc210113  * access the eeprom, it could cause system hang or panic. To workaround this
3067133Scc210113  * hardware problem, we use a global mutex to prevent such operations from
3077133Scc210113  * happening simultaneously on different instances. This workaround is applied
3087133Scc210113  * to all the devices supported by this driver.
3097133Scc210113  */
3107133Scc210113 kmutex_t e1000g_nvm_lock;
3113526Sxy150489 
3123526Sxy150489 /*
3133526Sxy150489  * Loadable module configuration entry points for the driver
3143526Sxy150489  */
3153526Sxy150489 
3163526Sxy150489 /*
3174919Sxy150489  * _init - module initialization
3183526Sxy150489  */
3193526Sxy150489 int
3203526Sxy150489 _init(void)
3213526Sxy150489 {
3223526Sxy150489 	int status;
3233526Sxy150489 
3243526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
3253526Sxy150489 	status = mod_install(&modlinkage);
3263526Sxy150489 	if (status != DDI_SUCCESS)
3273526Sxy150489 		mac_fini_ops(&ws_ops);
3283526Sxy150489 	else {
3293526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
3303526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
3317133Scc210113 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
3323526Sxy150489 	}
3333526Sxy150489 
3343526Sxy150489 	return (status);
3353526Sxy150489 }
3363526Sxy150489 
3373526Sxy150489 /*
3384919Sxy150489  * _fini - module finalization
3393526Sxy150489  */
3403526Sxy150489 int
3413526Sxy150489 _fini(void)
3423526Sxy150489 {
3433526Sxy150489 	int status;
3443526Sxy150489 
3453526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3463526Sxy150489 	if (e1000g_mblks_pending != 0) {
3473526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3483526Sxy150489 		return (EBUSY);
3493526Sxy150489 	}
3503526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3513526Sxy150489 
3523526Sxy150489 	status = mod_remove(&modlinkage);
3533526Sxy150489 	if (status == DDI_SUCCESS) {
3543526Sxy150489 		mac_fini_ops(&ws_ops);
3554349Sxy150489 
3564349Sxy150489 		if (e1000g_force_detach) {
3574349Sxy150489 			private_devi_list_t *devi_node;
3584349Sxy150489 
3594349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3604349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3614349Sxy150489 				devi_node = e1000g_private_devi_list;
3624349Sxy150489 				e1000g_private_devi_list =
3634349Sxy150489 				    e1000g_private_devi_list->next;
3644349Sxy150489 
3654349Sxy150489 				kmem_free(devi_node->priv_dip,
3664349Sxy150489 				    sizeof (struct dev_info));
3674349Sxy150489 				kmem_free(devi_node,
3684349Sxy150489 				    sizeof (private_devi_list_t));
3694349Sxy150489 			}
3704349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3714349Sxy150489 		}
3724349Sxy150489 
3733526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3743526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3757133Scc210113 		mutex_destroy(&e1000g_nvm_lock);
3763526Sxy150489 	}
3773526Sxy150489 
3783526Sxy150489 	return (status);
3793526Sxy150489 }
3803526Sxy150489 
3813526Sxy150489 /*
3824919Sxy150489  * _info - module information
3833526Sxy150489  */
3843526Sxy150489 int
3853526Sxy150489 _info(struct modinfo *modinfop)
3863526Sxy150489 {
3873526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3883526Sxy150489 }
3893526Sxy150489 
3903526Sxy150489 /*
3914919Sxy150489  * e1000g_attach - driver attach
3924919Sxy150489  *
3934919Sxy150489  * This function is the device-specific initialization entry
3944919Sxy150489  * point. This entry point is required and must be written.
3954919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3964919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3974919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3984919Sxy150489  * available for use by the driver.
3994919Sxy150489  *
4004919Sxy150489  * The attach() function will be called once for each instance
4014919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
4024919Sxy150489  * Until attach() succeeds, the only driver entry points which
4034919Sxy150489  * may be called are open(9E) and getinfo(9E).
4043526Sxy150489  */
4053526Sxy150489 static int
4064919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
4073526Sxy150489 {
4083526Sxy150489 	struct e1000g *Adapter;
4093526Sxy150489 	struct e1000_hw *hw;
4104919Sxy150489 	struct e1000g_osdep *osdep;
4113526Sxy150489 	int instance;
4123526Sxy150489 
4133526Sxy150489 	switch (cmd) {
4143526Sxy150489 	default:
4153526Sxy150489 		e1000g_log(NULL, CE_WARN,
4164919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
4173526Sxy150489 		return (DDI_FAILURE);
4183526Sxy150489 
4193526Sxy150489 	case DDI_RESUME:
4203526Sxy150489 		return (e1000g_resume(devinfo));
4213526Sxy150489 
4223526Sxy150489 	case DDI_ATTACH:
4233526Sxy150489 		break;
4243526Sxy150489 	}
4253526Sxy150489 
4263526Sxy150489 	/*
4273526Sxy150489 	 * get device instance number
4283526Sxy150489 	 */
4293526Sxy150489 	instance = ddi_get_instance(devinfo);
4303526Sxy150489 
4313526Sxy150489 	/*
4323526Sxy150489 	 * Allocate soft data structure
4333526Sxy150489 	 */
4343526Sxy150489 	Adapter =
4353526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4363526Sxy150489 
4373526Sxy150489 	Adapter->dip = devinfo;
4384919Sxy150489 	Adapter->instance = instance;
4393526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4403526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4413526Sxy150489 
4424919Sxy150489 	hw = &Adapter->shared;
4434919Sxy150489 	osdep = &Adapter->osdep;
4444919Sxy150489 	hw->back = osdep;
4454919Sxy150489 	osdep->adapter = Adapter;
4464919Sxy150489 
4473526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4483526Sxy150489 
4494919Sxy150489 	/*
4505273Sgl147354 	 * Initialize for fma support
4515273Sgl147354 	 */
4525273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4535273Sgl147354 	    0, 0x0f,
4545273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4555273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4565273Sgl147354 	e1000g_fm_init(Adapter);
4575273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4585273Sgl147354 
4595273Sgl147354 	/*
4604919Sxy150489 	 * PCI Configure
4614919Sxy150489 	 */
4624919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4634919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4644919Sxy150489 		goto attach_fail;
4654919Sxy150489 	}
4664919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4674919Sxy150489 
4684919Sxy150489 	/*
4694919Sxy150489 	 * Setup hardware
4704919Sxy150489 	 */
4714919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4724919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4734919Sxy150489 		goto attach_fail;
4744919Sxy150489 	}
4753526Sxy150489 
4763526Sxy150489 	/*
4773526Sxy150489 	 * Map in the device registers.
4783526Sxy150489 	 */
4794919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4804919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4813526Sxy150489 		goto attach_fail;
4823526Sxy150489 	}
4834919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4843526Sxy150489 
4853526Sxy150489 	/*
4863526Sxy150489 	 * Initialize driver parameters
4873526Sxy150489 	 */
4883526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4893526Sxy150489 		goto attach_fail;
4903526Sxy150489 	}
4914919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4923526Sxy150489 
4935273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4945273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4955273Sgl147354 		goto attach_fail;
4965273Sgl147354 	}
4975273Sgl147354 
4983526Sxy150489 	/*
4993526Sxy150489 	 * Initialize interrupts
5003526Sxy150489 	 */
5013526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
5023526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
5033526Sxy150489 		goto attach_fail;
5043526Sxy150489 	}
5054919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
5063526Sxy150489 
5073526Sxy150489 	/*
5083526Sxy150489 	 * Initialize mutex's for this device.
5093526Sxy150489 	 * Do this before enabling the interrupt handler and
5103526Sxy150489 	 * register the softint to avoid the condition where
5113526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5123526Sxy150489 	 */
5133526Sxy150489 	e1000g_init_locks(Adapter);
5143526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5153526Sxy150489 
5163526Sxy150489 	/*
5173526Sxy150489 	 * Initialize Driver Counters
5183526Sxy150489 	 */
5194919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
5203526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5213526Sxy150489 		goto attach_fail;
5223526Sxy150489 	}
5233526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5243526Sxy150489 
5253526Sxy150489 	/*
5263526Sxy150489 	 * Initialize chip hardware and software structures
5273526Sxy150489 	 */
5283526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5293526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5303526Sxy150489 		goto attach_fail;
5313526Sxy150489 	}
5323526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5333526Sxy150489 
5343526Sxy150489 	/*
5353526Sxy150489 	 * Register the driver to the MAC
5363526Sxy150489 	 */
5373526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5383526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5393526Sxy150489 		goto attach_fail;
5403526Sxy150489 	}
5414919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5423526Sxy150489 
5433526Sxy150489 	/*
5443526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5453526Sxy150489 	 * initialized, enable interrupts.
5463526Sxy150489 	 */
5473526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5483526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5493526Sxy150489 		goto attach_fail;
5503526Sxy150489 	}
5514919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5523526Sxy150489 
5534982Syy150190 	/*
5544982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5554982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5564982Syy150190 	 * supports after driver detached.
5574982Syy150190 	 */
5584982Syy150190 	if (e1000g_force_detach) {
5594982Syy150190 		private_devi_list_t *devi_node;
5604982Syy150190 
5614982Syy150190 		Adapter->priv_dip =
5624982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5634982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5644982Syy150190 		    sizeof (struct dev_info));
5654982Syy150190 
5664982Syy150190 		devi_node =
5674982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5684982Syy150190 
5694982Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
5704982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5714982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5724982Syy150190 		devi_node->next = e1000g_private_devi_list;
5734982Syy150190 		e1000g_private_devi_list = devi_node;
5744982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
5754982Syy150190 	}
5764982Syy150190 
5773526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5783526Sxy150489 
5793526Sxy150489 	return (DDI_SUCCESS);
5803526Sxy150489 
5813526Sxy150489 attach_fail:
5823526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5833526Sxy150489 	return (DDI_FAILURE);
5843526Sxy150489 }
5853526Sxy150489 
5863526Sxy150489 static int
5873526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5883526Sxy150489 {
5894919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5903526Sxy150489 	mac_register_t *mac;
5913526Sxy150489 	int err;
5923526Sxy150489 
5933526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5943526Sxy150489 		return (DDI_FAILURE);
5954919Sxy150489 
5963526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
5973526Sxy150489 	mac->m_driver = Adapter;
5983526Sxy150489 	mac->m_dip = Adapter->dip;
5994919Sxy150489 	mac->m_src_addr = hw->mac.addr;
6003526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
6013526Sxy150489 	mac->m_min_sdu = 0;
6026394Scc210113 	mac->m_max_sdu = Adapter->default_mtu;
6035895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6046512Ssowmini 	mac->m_priv_props = e1000g_priv_props;
6056512Ssowmini 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
6064919Sxy150489 
6073526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6083526Sxy150489 	mac_free(mac);
6094919Sxy150489 
6103526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6113526Sxy150489 }
6123526Sxy150489 
6133526Sxy150489 static int
6144919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
6154919Sxy150489 {
6164919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6174919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6184919Sxy150489 
6194919Sxy150489 	/* Get the device id */
6204919Sxy150489 	hw->vendor_id =
6214919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6224919Sxy150489 	hw->device_id =
6234919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6244919Sxy150489 	hw->revision_id =
6254919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6264919Sxy150489 	hw->subsystem_device_id =
6274919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6284919Sxy150489 	hw->subsystem_vendor_id =
6294919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6304919Sxy150489 
6314919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6324919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6334919Sxy150489 		    "MAC type could not be set properly.");
6344919Sxy150489 		return (DDI_FAILURE);
6354919Sxy150489 	}
6364919Sxy150489 
6374919Sxy150489 	return (DDI_SUCCESS);
6384919Sxy150489 }
6394919Sxy150489 
6404919Sxy150489 static int
6414919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6424919Sxy150489 {
6434919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6444919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6454919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6464919Sxy150489 	off_t mem_size;
6474919Sxy150489 
648*7607STed.You@Sun.COM 	/* Get size of adapter register memory */
649*7607STed.You@Sun.COM 	if (ddi_dev_regsize(devinfo, ADAPTER_REG_SET, &mem_size) !=
650*7607STed.You@Sun.COM 	    DDI_SUCCESS) {
6514919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6524919Sxy150489 		    "ddi_dev_regsize for registers failed");
6534919Sxy150489 		return (DDI_FAILURE);
6544919Sxy150489 	}
6554919Sxy150489 
656*7607STed.You@Sun.COM 	/* Map adapter register memory */
657*7607STed.You@Sun.COM 	if ((ddi_regs_map_setup(devinfo, ADAPTER_REG_SET,
6584919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6594919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6604919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6614919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6624919Sxy150489 		goto regs_map_fail;
6634919Sxy150489 	}
6644919Sxy150489 
6654919Sxy150489 	/* ICH needs to map flash memory */
666*7607STed.You@Sun.COM 	if (hw->mac.type == e1000_ich8lan ||
667*7607STed.You@Sun.COM 	    hw->mac.type == e1000_ich9lan ||
668*7607STed.You@Sun.COM 	    hw->mac.type == e1000_ich10lan) {
6694919Sxy150489 		/* get flash size */
6704919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6714919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6724919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6734919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6744919Sxy150489 			goto regs_map_fail;
6754919Sxy150489 		}
6764919Sxy150489 
6774919Sxy150489 		/* map flash in */
6784919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6794919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6804919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6814919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6824919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6834919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6844919Sxy150489 			goto regs_map_fail;
6854919Sxy150489 		}
6864919Sxy150489 	}
6874919Sxy150489 
6884919Sxy150489 	return (DDI_SUCCESS);
6894919Sxy150489 
6904919Sxy150489 regs_map_fail:
6914919Sxy150489 	if (osdep->reg_handle != NULL)
6924919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
6934919Sxy150489 
6944919Sxy150489 	return (DDI_FAILURE);
6954919Sxy150489 }
6964919Sxy150489 
6974919Sxy150489 static int
6983526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
6993526Sxy150489 {
7003526Sxy150489 	struct e1000_hw *hw;
7014919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
7023526Sxy150489 
7034919Sxy150489 	hw = &Adapter->shared;
7044919Sxy150489 
7054919Sxy150489 	/* Set MAC type and initialize hardware functions */
7064919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7074919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7084919Sxy150489 		    "Could not setup hardware functions");
7093526Sxy150489 		return (DDI_FAILURE);
7103526Sxy150489 	}
7113526Sxy150489 
7124919Sxy150489 	/* Get bus information */
7134919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7144919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7154919Sxy150489 		    "Could not get bus information");
7164919Sxy150489 		return (DDI_FAILURE);
7173526Sxy150489 	}
7183526Sxy150489 
7193526Sxy150489 	/* get mem_base addr */
7204919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7214919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7223526Sxy150489 
7233526Sxy150489 	/* get io_base addr */
7244919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7254919Sxy150489 		if (bar64) {
7263526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7274919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7284919Sxy150489 			    PCI_CONF_BASE4);
7293526Sxy150489 		} else {
7303526Sxy150489 			/* normal 32-bit BAR mode */
7314919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7324919Sxy150489 			    PCI_CONF_BASE2);
7333526Sxy150489 		}
7343526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7353526Sxy150489 	} else {
7363526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7373526Sxy150489 		hw->io_base = 0x0;
7383526Sxy150489 	}
7393526Sxy150489 
7404919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7414919Sxy150489 
7424919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7434919Sxy150489 
7446735Scc210113 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
7456735Scc210113 	hw->phy.autoneg_wait_to_complete = B_FALSE;
7463526Sxy150489 
7473526Sxy150489 	/* Adaptive IFS related changes */
7484919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7494919Sxy150489 
7504919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7514919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7524919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7534919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7544919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7554919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7564919Sxy150489 
7574919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7584919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7593526Sxy150489 
7604608Syy150190 #ifdef __sparc
7614608Syy150190 	Adapter->strip_crc = B_TRUE;
7624608Syy150190 #else
7634608Syy150190 	Adapter->strip_crc = B_FALSE;
7644608Syy150190 #endif
7654608Syy150190 
7663526Sxy150489 	/* Get conf file properties */
7674919Sxy150489 	e1000g_get_conf(Adapter);
7684919Sxy150489 
7694919Sxy150489 	/* Get speed/duplex settings in conf file */
7704919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7714919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7723526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7733526Sxy150489 
7744919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7753526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7763526Sxy150489 
7773526Sxy150489 	/* Set Rx/Tx buffer size */
7786394Scc210113 	e1000g_set_bufsize(Adapter);
7794919Sxy150489 
7804919Sxy150489 	/* Master Latency Timer */
7814919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
7824919Sxy150489 
7833526Sxy150489 	/* copper options */
7846735Scc210113 	if (hw->phy.media_type == e1000_media_type_copper) {
7854919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
7864919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
7874919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
7883526Sxy150489 	}
7893526Sxy150489 
7904919Sxy150489 	/* The initial link state should be "unknown" */
7914061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
7924061Sxy150489 
7935882Syy150190 	/* Initialize rx parameters */
7945882Syy150190 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
7955882Syy150190 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
7965882Syy150190 
7974919Sxy150489 	/* Initialize tx parameters */
7984919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
7994919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
8005882Syy150190 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
8015882Syy150190 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
8024919Sxy150489 
8034919Sxy150489 	/* Initialize rx parameters */
8044919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8054919Sxy150489 
8063526Sxy150489 	return (DDI_SUCCESS);
8073526Sxy150489 }
8083526Sxy150489 
8096394Scc210113 static void
8106394Scc210113 e1000g_set_bufsize(struct e1000g *Adapter)
8116394Scc210113 {
8126394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
8136394Scc210113 	uint64_t rx_size;
8146394Scc210113 	uint64_t tx_size;
8156394Scc210113 
8166394Scc210113 #ifdef __sparc
8176394Scc210113 	dev_info_t *devinfo = Adapter->dip;
8186394Scc210113 	ulong_t iommu_pagesize;
8196394Scc210113 
8206394Scc210113 	/* Get the system page size */
8216394Scc210113 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
8226394Scc210113 	iommu_pagesize = dvma_pagesize(devinfo);
8236394Scc210113 	if (iommu_pagesize != 0) {
8246394Scc210113 		if (Adapter->sys_page_sz == iommu_pagesize) {
8256394Scc210113 			if (iommu_pagesize > 0x4000)
8266394Scc210113 				Adapter->sys_page_sz = 0x4000;
8276394Scc210113 		} else {
8286394Scc210113 			if (Adapter->sys_page_sz > iommu_pagesize)
8296394Scc210113 				Adapter->sys_page_sz = iommu_pagesize;
8306394Scc210113 		}
8316394Scc210113 	}
8326986Smx205022 	if (Adapter->lso_enable) {
8336986Smx205022 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
8346986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8356986Smx205022 	} else {
8366986Smx205022 		Adapter->dvma_page_num = Adapter->max_frame_size /
8376986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8386986Smx205022 	}
8396394Scc210113 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
8406394Scc210113 #endif
8416394Scc210113 
8426735Scc210113 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
8436735Scc210113 
8446735Scc210113 	rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
8456394Scc210113 	if ((rx_size > FRAME_SIZE_UPTO_2K) && (rx_size <= FRAME_SIZE_UPTO_4K))
8466394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
8476394Scc210113 	else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
8486394Scc210113 	    (rx_size <= FRAME_SIZE_UPTO_8K))
8496394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
8506394Scc210113 	else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
8516394Scc210113 	    (rx_size <= FRAME_SIZE_UPTO_16K))
8526394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
8536394Scc210113 	else
8546394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
8556394Scc210113 
8566735Scc210113 	tx_size = Adapter->max_frame_size;
8576394Scc210113 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
8586394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
8596394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
8606394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_8K))
8616394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
8626394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
8636394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_16K))
8646394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
8656394Scc210113 	else
8666394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
8676394Scc210113 
8686394Scc210113 	/*
8696394Scc210113 	 * For Wiseman adapters we have an requirement of having receive
8706394Scc210113 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8716394Scc210113 	 * require this and forcing it for all hardwares will have
8726394Scc210113 	 * performance implications, I am making it applicable only for
8736394Scc210113 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8746394Scc210113 	 * it is okay to have normal frames...but it does involve a
8756394Scc210113 	 * potential risk where we may loose data if buffer is not
8766394Scc210113 	 * aligned...so all wiseman boards to have 256 byte aligned
8776394Scc210113 	 * buffers
8786394Scc210113 	 */
8796394Scc210113 	if (mac->type < e1000_82543)
8806394Scc210113 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
8816394Scc210113 	else
8826394Scc210113 		Adapter->rx_buf_align = 1;
8836394Scc210113 }
8846394Scc210113 
8853526Sxy150489 /*
8864919Sxy150489  * e1000g_detach - driver detach
8874919Sxy150489  *
8884919Sxy150489  * The detach() function is the complement of the attach routine.
8894919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
8904919Sxy150489  * state  associated  with  a  given  instance of a device node
8914919Sxy150489  * prior to the removal of that instance from the system.
8924919Sxy150489  *
8934919Sxy150489  * The detach() function will be called once for each  instance
8944919Sxy150489  * of the device for which there has been a successful attach()
8954919Sxy150489  * once there are no longer  any  opens  on  the  device.
8964919Sxy150489  *
8974919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
8984919Sxy150489  * driver are freed.
8993526Sxy150489  */
9003526Sxy150489 static int
9014919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
9023526Sxy150489 {
9033526Sxy150489 	struct e1000g *Adapter;
9044982Syy150190 	boolean_t rx_drain;
9053526Sxy150489 
9063526Sxy150489 	switch (cmd) {
9073526Sxy150489 	default:
9083526Sxy150489 		return (DDI_FAILURE);
9093526Sxy150489 
9103526Sxy150489 	case DDI_SUSPEND:
9113526Sxy150489 		return (e1000g_suspend(devinfo));
9123526Sxy150489 
9133526Sxy150489 	case DDI_DETACH:
9143526Sxy150489 		break;
9153526Sxy150489 	}
9163526Sxy150489 
9173526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
9183526Sxy150489 	if (Adapter == NULL)
9193526Sxy150489 		return (DDI_FAILURE);
9203526Sxy150489 
9214919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9224919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
9234919Sxy150489 		return (DDI_FAILURE);
9244919Sxy150489 	}
9254919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
9264919Sxy150489 
9275273Sgl147354 
9285273Sgl147354 	if (Adapter->chip_state != E1000G_STOP)
9294919Sxy150489 		e1000g_stop(Adapter, B_TRUE);
9303526Sxy150489 
9314982Syy150190 	rx_drain = e1000g_rx_drain(Adapter);
9324982Syy150190 
9334982Syy150190 	/*
9344982Syy150190 	 * If e1000g_force_detach is enabled, driver detach is safe.
9354982Syy150190 	 * We will let e1000g_free_priv_devi_node routine determine
9364982Syy150190 	 * whether we need to free the priv_dip entry for current
9374982Syy150190 	 * driver instance.
9384982Syy150190 	 */
9394982Syy150190 	if (e1000g_force_detach) {
9404982Syy150190 		e1000g_free_priv_devi_node(Adapter, rx_drain);
9414982Syy150190 	} else {
9424982Syy150190 		if (!rx_drain)
9433526Sxy150489 			return (DDI_FAILURE);
9443526Sxy150489 	}
9453526Sxy150489 
9463526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9473526Sxy150489 
9483526Sxy150489 	return (DDI_SUCCESS);
9493526Sxy150489 }
9503526Sxy150489 
9514982Syy150190 /*
9524982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
9534982Syy150190  *
9544982Syy150190  * If free_flag is true, that indicates the upper layer is not holding
9554982Syy150190  * the rx buffers, we could free the priv_dip entry safely.
9564982Syy150190  *
9574982Syy150190  * Otherwise, we have to keep this entry even after driver detached,
9584982Syy150190  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
9594982Syy150190  * so that driver could free it while all of rx buffers are returned
9604982Syy150190  * by upper layer later.
9614982Syy150190  */
9624982Syy150190 static void
9634982Syy150190 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
9644982Syy150190 {
9654982Syy150190 	private_devi_list_t *devi_node, *devi_del;
9664982Syy150190 
9674982Syy150190 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
9684982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
9694982Syy150190 	ASSERT(Adapter->priv_dip != NULL);
9704982Syy150190 
9714982Syy150190 	devi_node = e1000g_private_devi_list;
9724982Syy150190 	if (devi_node->priv_dip == Adapter->priv_dip) {
9734982Syy150190 		if (free_flag) {
9744982Syy150190 			e1000g_private_devi_list =
9754982Syy150190 			    devi_node->next;
9764982Syy150190 			kmem_free(devi_node->priv_dip,
9774982Syy150190 			    sizeof (struct dev_info));
9784982Syy150190 			kmem_free(devi_node,
9794982Syy150190 			    sizeof (private_devi_list_t));
9804982Syy150190 		} else {
9814982Syy150190 			ASSERT(e1000g_mblks_pending != 0);
9824982Syy150190 			devi_node->flag =
9834982Syy150190 			    E1000G_PRIV_DEVI_DETACH;
9844982Syy150190 		}
9854982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
9864982Syy150190 		return;
9874982Syy150190 	}
9884982Syy150190 
9894982Syy150190 	devi_node = e1000g_private_devi_list;
9904982Syy150190 	while (devi_node->next != NULL) {
9914982Syy150190 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
9924982Syy150190 			if (free_flag) {
9934982Syy150190 				devi_del = devi_node->next;
9944982Syy150190 				devi_node->next = devi_del->next;
9954982Syy150190 				kmem_free(devi_del->priv_dip,
9964982Syy150190 				    sizeof (struct dev_info));
9974982Syy150190 				kmem_free(devi_del,
9984982Syy150190 				    sizeof (private_devi_list_t));
9994982Syy150190 			} else {
10004982Syy150190 				ASSERT(e1000g_mblks_pending != 0);
10014982Syy150190 				devi_node->next->flag =
10024982Syy150190 				    E1000G_PRIV_DEVI_DETACH;
10034982Syy150190 			}
10044982Syy150190 			break;
10054982Syy150190 		}
10064982Syy150190 		devi_node = devi_node->next;
10074982Syy150190 	}
10084982Syy150190 	rw_exit(&e1000g_rx_detach_lock);
10094982Syy150190 }
10104982Syy150190 
10113526Sxy150489 static void
10123526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
10133526Sxy150489 {
10147133Scc210113 	int result;
10157133Scc210113 
10164919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
10173526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
10183526Sxy150489 	}
10193526Sxy150489 
10204919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
10213526Sxy150489 		(void) mac_unregister(Adapter->mh);
10223526Sxy150489 	}
10233526Sxy150489 
10244919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
10253526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
10263526Sxy150489 	}
10273526Sxy150489 
10284919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10293526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10303526Sxy150489 	}
10313526Sxy150489 
10323526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10333526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10343526Sxy150489 	}
10353526Sxy150489 
10363526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10374919Sxy150489 		stop_link_timer(Adapter);
10387133Scc210113 
10397133Scc210113 		mutex_enter(&e1000g_nvm_lock);
10407133Scc210113 		result = e1000_reset_hw(&Adapter->shared);
10417133Scc210113 		mutex_exit(&e1000g_nvm_lock);
10427133Scc210113 
10437133Scc210113 		if (result != E1000_SUCCESS) {
10445273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10455273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10465273Sgl147354 		}
10473526Sxy150489 	}
10483526Sxy150489 
10494919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10504919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10514919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10524919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10534919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10543526Sxy150489 	}
10553526Sxy150489 
10564919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10574919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10584919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10593526Sxy150489 	}
10603526Sxy150489 
10613526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10623526Sxy150489 		e1000g_destroy_locks(Adapter);
10633526Sxy150489 	}
10643526Sxy150489 
10655273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10665273Sgl147354 		e1000g_fm_fini(Adapter);
10675273Sgl147354 	}
10685273Sgl147354 
10694919Sxy150489 	e1000_remove_device(&Adapter->shared);
10704919Sxy150489 
10713526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
10723526Sxy150489 
10733526Sxy150489 	/*
10743526Sxy150489 	 * Another hotplug spec requirement,
10753526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
10763526Sxy150489 	 */
10773526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
10783526Sxy150489 }
10793526Sxy150489 
10803526Sxy150489 static void
10813526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
10823526Sxy150489 {
10833526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10843526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10853526Sxy150489 
10863526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
10873526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10884919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
10893526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10904919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
10913526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10923526Sxy150489 
10933526Sxy150489 	tx_ring = Adapter->tx_ring;
10943526Sxy150489 
10953526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
10963526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10973526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
10983526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10993526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
11003526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11013526Sxy150489 
11023526Sxy150489 	rx_ring = Adapter->rx_ring;
11033526Sxy150489 
11047436STed.You@Sun.COM 	mutex_init(&rx_ring->rx_lock, NULL,
11057436STed.You@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11063526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
11073526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11083526Sxy150489 }
11093526Sxy150489 
11103526Sxy150489 static void
11113526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
11123526Sxy150489 {
11133526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11143526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11153526Sxy150489 
11163526Sxy150489 	tx_ring = Adapter->tx_ring;
11173526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
11183526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
11193526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
11203526Sxy150489 
11213526Sxy150489 	rx_ring = Adapter->rx_ring;
11227436STed.You@Sun.COM 	mutex_destroy(&rx_ring->rx_lock);
11233526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
11243526Sxy150489 
11254919Sxy150489 	mutex_destroy(&Adapter->link_lock);
11264919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
11273526Sxy150489 	rw_destroy(&Adapter->chip_lock);
11283526Sxy150489 }
11293526Sxy150489 
11303526Sxy150489 static int
11313526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11323526Sxy150489 {
11333526Sxy150489 	struct e1000g *Adapter;
11343526Sxy150489 
11353526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11363526Sxy150489 	if (Adapter == NULL)
11373526Sxy150489 		return (DDI_FAILURE);
11383526Sxy150489 
11394919Sxy150489 	if (e1000g_start(Adapter, B_TRUE))
11403526Sxy150489 		return (DDI_FAILURE);
11413526Sxy150489 
11423526Sxy150489 	return (DDI_SUCCESS);
11433526Sxy150489 }
11443526Sxy150489 
11453526Sxy150489 static int
11463526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11473526Sxy150489 {
11483526Sxy150489 	struct e1000g *Adapter;
11493526Sxy150489 
11503526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11513526Sxy150489 	if (Adapter == NULL)
11523526Sxy150489 		return (DDI_FAILURE);
11533526Sxy150489 
11544919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
11553526Sxy150489 
11563526Sxy150489 	return (DDI_SUCCESS);
11573526Sxy150489 }
11583526Sxy150489 
11593526Sxy150489 static int
11603526Sxy150489 e1000g_init(struct e1000g *Adapter)
11613526Sxy150489 {
11623526Sxy150489 	uint32_t pba;
11634919Sxy150489 	uint32_t high_water;
11643526Sxy150489 	struct e1000_hw *hw;
11654061Sxy150489 	clock_t link_timeout;
11667133Scc210113 	int result;
11673526Sxy150489 
11684919Sxy150489 	hw = &Adapter->shared;
11693526Sxy150489 
11703526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11713526Sxy150489 
11723526Sxy150489 	/*
11733526Sxy150489 	 * reset to put the hardware in a known state
11743526Sxy150489 	 * before we try to do anything with the eeprom
11753526Sxy150489 	 */
11767133Scc210113 	mutex_enter(&e1000g_nvm_lock);
11777133Scc210113 	result = e1000_reset_hw(hw);
11787133Scc210113 	mutex_exit(&e1000g_nvm_lock);
11797133Scc210113 
11807133Scc210113 	if (result != E1000_SUCCESS) {
11815273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11825273Sgl147354 		goto init_fail;
11835273Sgl147354 	}
11843526Sxy150489 
11857133Scc210113 	mutex_enter(&e1000g_nvm_lock);
11867133Scc210113 	result = e1000_validate_nvm_checksum(hw);
11877133Scc210113 	if (result < E1000_SUCCESS) {
11884061Sxy150489 		/*
11894061Sxy150489 		 * Some PCI-E parts fail the first check due to
11904061Sxy150489 		 * the link being in sleep state.  Call it again,
11914061Sxy150489 		 * if it fails a second time its a real issue.
11924061Sxy150489 		 */
11937133Scc210113 		result = e1000_validate_nvm_checksum(hw);
11947133Scc210113 	}
11957133Scc210113 	mutex_exit(&e1000g_nvm_lock);
11967133Scc210113 
11977133Scc210113 	if (result < E1000_SUCCESS) {
11987133Scc210113 		e1000g_log(Adapter, CE_WARN,
11997133Scc210113 		    "Invalid NVM checksum. Please contact "
12007133Scc210113 		    "the vendor to update the NVM.");
12017133Scc210113 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12027133Scc210113 		goto init_fail;
12037133Scc210113 	}
12047133Scc210113 
12057133Scc210113 	result = 0;
12063526Sxy150489 #ifdef __sparc
12073526Sxy150489 	/*
1208*7607STed.You@Sun.COM 	 * First, we try to get the local ethernet address from OBP. If
12097133Scc210113 	 * failed, then we get it from the EEPROM of NIC card.
12103526Sxy150489 	 */
12117133Scc210113 	result = e1000g_find_mac_address(Adapter);
12127133Scc210113 #endif
12133526Sxy150489 	/* Get the local ethernet address. */
12147133Scc210113 	if (!result) {
12157133Scc210113 		mutex_enter(&e1000g_nvm_lock);
12167140Scc210113 		result = e1000_read_mac_addr(hw);
12177133Scc210113 		mutex_exit(&e1000g_nvm_lock);
12187133Scc210113 	}
12197133Scc210113 
12207133Scc210113 	if (result < E1000_SUCCESS) {
12213526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
12225273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12233526Sxy150489 		goto init_fail;
12243526Sxy150489 	}
12253526Sxy150489 
12263526Sxy150489 	/* check for valid mac address */
12274919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12283526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
12295273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12303526Sxy150489 		goto init_fail;
12313526Sxy150489 	}
12323526Sxy150489 
12334919Sxy150489 	/* Set LAA state for 82571 chipset */
12344919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
12353526Sxy150489 
12363526Sxy150489 	/* Master Latency Timer implementation */
12374919Sxy150489 	if (Adapter->master_latency_timer) {
12384919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
12394919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
12403526Sxy150489 	}
12413526Sxy150489 
12424919Sxy150489 	if (hw->mac.type < e1000_82547) {
12433526Sxy150489 		/*
12443526Sxy150489 		 * Total FIFO is 64K
12453526Sxy150489 		 */
12466735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12473526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
12483526Sxy150489 		else
12493526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
12507140Scc210113 	} else if ((hw->mac.type == e1000_82571) ||
12517140Scc210113 	    (hw->mac.type == e1000_82572) ||
12527140Scc210113 	    (hw->mac.type == e1000_80003es2lan)) {
12533526Sxy150489 		/*
12543526Sxy150489 		 * Total FIFO is 48K
12553526Sxy150489 		 */
12566735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12573526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
12583526Sxy150489 		else
12593526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
1260*7607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82573) {
1261*7607STed.You@Sun.COM 		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
1262*7607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82574) {
1263*7607STed.You@Sun.COM 		/* Keep adapter default: 20K for Rx, 20K for Tx */
1264*7607STed.You@Sun.COM 		pba = E1000_READ_REG(hw, E1000_PBA);
12654919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
12663526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
12674919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
1268*7607STed.You@Sun.COM 		pba = E1000_PBA_10K;
1269*7607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_ich10lan) {
1270*7607STed.You@Sun.COM 		pba = E1000_PBA_10K;
12713526Sxy150489 	} else {
12723526Sxy150489 		/*
12733526Sxy150489 		 * Total FIFO is 40K
12743526Sxy150489 		 */
12756735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12763526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
12773526Sxy150489 		else
12783526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
12793526Sxy150489 	}
12804919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
12813526Sxy150489 
12823526Sxy150489 	/*
12833526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
12843526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
12853526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
12863526Sxy150489 	 * file.
12873526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
12883526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
12893526Sxy150489 	 * high-water mark.
12904919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
12914919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
12924919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
12934919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
12944919Sxy150489 	 * Rx FIFO size minus one full frame.
12953526Sxy150489 	 */
12964919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
1297*7607STed.You@Sun.COM 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_ich9lan ||
1298*7607STed.You@Sun.COM 	    hw->mac.type == e1000_ich10lan) ?
12994919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
13006735Scc210113 	    ((pba << 10) - Adapter->max_frame_size)));
13016735Scc210113 
13026735Scc210113 	hw->fc.high_water = high_water & 0xFFF8;
13036735Scc210113 	hw->fc.low_water = hw->fc.high_water - 8;
13044919Sxy150489 
13054919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
13066735Scc210113 		hw->fc.pause_time = 0xFFFF;
13074919Sxy150489 	else
13086735Scc210113 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
13096735Scc210113 	hw->fc.send_xon = B_TRUE;
13103526Sxy150489 
13113526Sxy150489 	/*
13123526Sxy150489 	 * Reset the adapter hardware the second time.
13133526Sxy150489 	 */
13147133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13157133Scc210113 	result = e1000_reset_hw(hw);
13167133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13177133Scc210113 
13187133Scc210113 	if (result != E1000_SUCCESS) {
13195273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13205273Sgl147354 		goto init_fail;
13215273Sgl147354 	}
13223526Sxy150489 
13233526Sxy150489 	/* disable wakeup control by default */
13244919Sxy150489 	if (hw->mac.type >= e1000_82544)
13254919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
13263526Sxy150489 
13273526Sxy150489 	/* MWI setup */
13284919Sxy150489 	e1000_pci_set_mwi(hw);
13293526Sxy150489 
13303526Sxy150489 	/*
13313526Sxy150489 	 * Configure/Initialize hardware
13323526Sxy150489 	 */
13337133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13347133Scc210113 	result = e1000_init_hw(hw);
13357133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13367133Scc210113 
13377133Scc210113 	if (result < E1000_SUCCESS) {
13383526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
13395273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13403526Sxy150489 		goto init_fail;
13413526Sxy150489 	}
13423526Sxy150489 
13437436STed.You@Sun.COM 	/*
13447436STed.You@Sun.COM 	 * Restore LED settings to the default from EEPROM
13457436STed.You@Sun.COM 	 * to meet the standard for Sun platforms.
13467436STed.You@Sun.COM 	 */
13477436STed.You@Sun.COM 	(void) e1000_cleanup_led(hw);
13487436STed.You@Sun.COM 
13493526Sxy150489 	/* Disable Smart Power Down */
13503526Sxy150489 	phy_spd_state(hw, B_FALSE);
13513526Sxy150489 
13525082Syy150190 	/* Make sure driver has control */
13535082Syy150190 	e1000g_get_driver_control(hw);
13545082Syy150190 
13553526Sxy150489 	/*
13563526Sxy150489 	 * Initialize unicast addresses.
13573526Sxy150489 	 */
13583526Sxy150489 	e1000g_init_unicst(Adapter);
13593526Sxy150489 
13603526Sxy150489 	/*
13613526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
13623526Sxy150489 	 * completes  Multicast table will be set
13633526Sxy150489 	 */
13644919Sxy150489 	e1000g_setup_multicast(Adapter);
13654919Sxy150489 	msec_delay(5);
13663526Sxy150489 
13673526Sxy150489 	/*
13683526Sxy150489 	 * Implement Adaptive IFS
13693526Sxy150489 	 */
13703526Sxy150489 	e1000_reset_adaptive(hw);
13713526Sxy150489 
13723526Sxy150489 	/* Setup Interrupt Throttling Register */
13735882Syy150190 	if (hw->mac.type >= e1000_82540) {
13745882Syy150190 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
13755882Syy150190 	} else
13765882Syy150190 		Adapter->intr_adaptive = B_FALSE;
13773526Sxy150489 
13784061Sxy150489 	/* Start the timer for link setup */
13794919Sxy150489 	if (hw->mac.autoneg)
13804919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
13814061Sxy150489 	else
13824919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
13834919Sxy150489 
13844919Sxy150489 	mutex_enter(&Adapter->link_lock);
13856735Scc210113 	if (hw->phy.autoneg_wait_to_complete) {
13864061Sxy150489 		Adapter->link_complete = B_TRUE;
13873526Sxy150489 	} else {
13884061Sxy150489 		Adapter->link_complete = B_FALSE;
13894061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
13904061Sxy150489 		    (void *)Adapter, link_timeout);
13913526Sxy150489 	}
13924919Sxy150489 	mutex_exit(&Adapter->link_lock);
13933526Sxy150489 
13943526Sxy150489 	/* Enable PCI-Ex master */
13954919Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
13963526Sxy150489 		e1000_enable_pciex_master(hw);
13973526Sxy150489 	}
13983526Sxy150489 
13995082Syy150190 	/* Save the state of the phy */
14005082Syy150190 	e1000g_get_phy_state(Adapter);
14015082Syy150190 
14026512Ssowmini 	e1000g_param_sync(Adapter);
14036512Ssowmini 
14043526Sxy150489 	Adapter->init_count++;
14053526Sxy150489 
14065273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
14075273Sgl147354 		goto init_fail;
14085273Sgl147354 	}
14095273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
14105273Sgl147354 		goto init_fail;
14115273Sgl147354 	}
14125273Sgl147354 
14133526Sxy150489 	rw_exit(&Adapter->chip_lock);
14143526Sxy150489 
14153526Sxy150489 	return (DDI_SUCCESS);
14163526Sxy150489 
14173526Sxy150489 init_fail:
14183526Sxy150489 	rw_exit(&Adapter->chip_lock);
14195273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
14203526Sxy150489 	return (DDI_FAILURE);
14213526Sxy150489 }
14223526Sxy150489 
14233526Sxy150489 /*
14243526Sxy150489  * Check if the link is up
14253526Sxy150489  */
14263526Sxy150489 static boolean_t
14273526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
14283526Sxy150489 {
14293526Sxy150489 	struct e1000_hw *hw;
14303526Sxy150489 	boolean_t link_up;
14313526Sxy150489 
14324919Sxy150489 	hw = &Adapter->shared;
14333526Sxy150489 
14347426SChenliang.Xu@Sun.COM 	(void) e1000_check_for_link(hw);
14353526Sxy150489 
14364919Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
14374919Sxy150489 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
14386735Scc210113 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
14394919Sxy150489 	    (hw->mac.serdes_has_link))) {
14403526Sxy150489 		link_up = B_TRUE;
14413526Sxy150489 	} else {
14423526Sxy150489 		link_up = B_FALSE;
14433526Sxy150489 	}
14443526Sxy150489 
14453526Sxy150489 	return (link_up);
14463526Sxy150489 }
14473526Sxy150489 
14483526Sxy150489 static void
14493526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
14503526Sxy150489 {
14513526Sxy150489 	struct iocblk *iocp;
14523526Sxy150489 	struct e1000g *e1000gp;
14533526Sxy150489 	enum ioc_reply status;
14547426SChenliang.Xu@Sun.COM 
14557426SChenliang.Xu@Sun.COM 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
14563526Sxy150489 	iocp->ioc_error = 0;
14573526Sxy150489 	e1000gp = (struct e1000g *)arg;
14583526Sxy150489 
14593526Sxy150489 	ASSERT(e1000gp);
14603526Sxy150489 	if (e1000gp == NULL) {
14613526Sxy150489 		miocnak(q, mp, 0, EINVAL);
14623526Sxy150489 		return;
14633526Sxy150489 	}
14643526Sxy150489 
14653526Sxy150489 	switch (iocp->ioc_cmd) {
14663526Sxy150489 
14673526Sxy150489 	case LB_GET_INFO_SIZE:
14683526Sxy150489 	case LB_GET_INFO:
14693526Sxy150489 	case LB_GET_MODE:
14703526Sxy150489 	case LB_SET_MODE:
14713526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
14723526Sxy150489 		break;
14733526Sxy150489 
14743526Sxy150489 
14754919Sxy150489 #ifdef E1000G_DEBUG
14763526Sxy150489 	case E1000G_IOC_REG_PEEK:
14773526Sxy150489 	case E1000G_IOC_REG_POKE:
14783526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
14793526Sxy150489 		break;
14803526Sxy150489 	case E1000G_IOC_CHIP_RESET:
14813526Sxy150489 		e1000gp->reset_count++;
14823526Sxy150489 		if (e1000g_reset(e1000gp))
14833526Sxy150489 			status = IOC_ACK;
14843526Sxy150489 		else
14853526Sxy150489 			status = IOC_INVAL;
14863526Sxy150489 		break;
14874919Sxy150489 #endif
14883526Sxy150489 	default:
14893526Sxy150489 		status = IOC_INVAL;
14903526Sxy150489 		break;
14913526Sxy150489 	}
14923526Sxy150489 
14933526Sxy150489 	/*
14943526Sxy150489 	 * Decide how to reply
14953526Sxy150489 	 */
14963526Sxy150489 	switch (status) {
14973526Sxy150489 	default:
14983526Sxy150489 	case IOC_INVAL:
14993526Sxy150489 		/*
15003526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
15013526Sxy150489 		 */
15023526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
15034349Sxy150489 		    EINVAL : iocp->ioc_error);
15043526Sxy150489 		break;
15053526Sxy150489 
15063526Sxy150489 	case IOC_DONE:
15073526Sxy150489 		/*
15083526Sxy150489 		 * OK, reply already sent
15093526Sxy150489 		 */
15103526Sxy150489 		break;
15113526Sxy150489 
15123526Sxy150489 	case IOC_ACK:
15133526Sxy150489 		/*
15143526Sxy150489 		 * OK, reply with an ACK
15153526Sxy150489 		 */
15163526Sxy150489 		miocack(q, mp, 0, 0);
15173526Sxy150489 		break;
15183526Sxy150489 
15193526Sxy150489 	case IOC_REPLY:
15203526Sxy150489 		/*
15213526Sxy150489 		 * OK, send prepared reply as ACK or NAK
15223526Sxy150489 		 */
15233526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
15244349Sxy150489 		    M_IOCACK : M_IOCNAK;
15253526Sxy150489 		qreply(q, mp);
15263526Sxy150489 		break;
15273526Sxy150489 	}
15283526Sxy150489 }
15293526Sxy150489 
15303526Sxy150489 static int
15313526Sxy150489 e1000g_m_start(void *arg)
15323526Sxy150489 {
15333526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15343526Sxy150489 
15354919Sxy150489 	return (e1000g_start(Adapter, B_TRUE));
15363526Sxy150489 }
15373526Sxy150489 
15383526Sxy150489 static int
15394919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
15403526Sxy150489 {
15414919Sxy150489 	if (global) {
15424919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
15434919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
15444919Sxy150489 			e1000g_log(Adapter, CE_WARN,
15454919Sxy150489 			    "Alloc DMA resources failed");
15464919Sxy150489 			return (ENOTACTIVE);
15474919Sxy150489 		}
15484919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
15494919Sxy150489 	}
15504919Sxy150489 
15513526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
15523526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
15533526Sxy150489 			e1000g_log(Adapter, CE_WARN,
15543526Sxy150489 			    "Adapter initialization failed");
15554919Sxy150489 			if (global)
15564919Sxy150489 				e1000g_release_dma_resources(Adapter);
15573526Sxy150489 			return (ENOTACTIVE);
15583526Sxy150489 		}
15593526Sxy150489 	}
15603526Sxy150489 
15613526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15623526Sxy150489 
15634919Sxy150489 	/* Setup and initialize the transmit structures */
15644919Sxy150489 	e1000g_tx_setup(Adapter);
15654919Sxy150489 	msec_delay(5);
15664919Sxy150489 
15674919Sxy150489 	/* Setup and initialize the receive structures */
15684919Sxy150489 	e1000g_rx_setup(Adapter);
15694919Sxy150489 	msec_delay(5);
15704919Sxy150489 
15714919Sxy150489 	e1000g_mask_interrupt(Adapter);
15723526Sxy150489 
15735273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
15745273Sgl147354 		rw_exit(&Adapter->chip_lock);
15755273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
15765273Sgl147354 		return (ENOTACTIVE);
15775273Sgl147354 	}
15785273Sgl147354 
15795273Sgl147354 	Adapter->chip_state = E1000G_START;
15803526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
15813526Sxy150489 
15823526Sxy150489 	rw_exit(&Adapter->chip_lock);
15833526Sxy150489 
15844919Sxy150489 	/* Enable and start the watchdog timer */
15854919Sxy150489 	enable_watchdog_timer(Adapter);
15864919Sxy150489 
15873526Sxy150489 	return (0);
15883526Sxy150489 }
15893526Sxy150489 
15903526Sxy150489 static void
15913526Sxy150489 e1000g_m_stop(void *arg)
15923526Sxy150489 {
15933526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15943526Sxy150489 
15954919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
15963526Sxy150489 }
15973526Sxy150489 
15983526Sxy150489 static void
15994919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
16003526Sxy150489 {
16017133Scc210113 	int result;
16027133Scc210113 
16033526Sxy150489 	/* Set stop flags */
16043526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16053526Sxy150489 
16065273Sgl147354 	Adapter->chip_state = E1000G_STOP;
16073526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
16083526Sxy150489 
16093526Sxy150489 	rw_exit(&Adapter->chip_lock);
16103526Sxy150489 
16113526Sxy150489 	/* Drain tx sessions */
16123526Sxy150489 	(void) e1000g_tx_drain(Adapter);
16133526Sxy150489 
16144919Sxy150489 	/* Disable and stop all the timers */
16154919Sxy150489 	disable_watchdog_timer(Adapter);
16164919Sxy150489 	stop_link_timer(Adapter);
16174919Sxy150489 	stop_82547_timer(Adapter->tx_ring);
16184061Sxy150489 
16193526Sxy150489 	/* Stop the chip and release pending resources */
16203526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16213526Sxy150489 
1622*7607STed.You@Sun.COM 	/* Tell firmware driver is no longer in control */
1623*7607STed.You@Sun.COM 	e1000g_release_driver_control(&Adapter->shared);
1624*7607STed.You@Sun.COM 
16254919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
16267133Scc210113 
16277133Scc210113 	mutex_enter(&e1000g_nvm_lock);
16287133Scc210113 	result = e1000_reset_hw(&Adapter->shared);
16297133Scc210113 	mutex_exit(&e1000g_nvm_lock);
16307133Scc210113 
16317133Scc210113 	if (result != E1000_SUCCESS) {
16325273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
16335273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16345273Sgl147354 	}
16353526Sxy150489 
16363526Sxy150489 	/* Release resources still held by the TX descriptors */
16374919Sxy150489 	e1000g_tx_clean(Adapter);
16384061Sxy150489 
16395273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
16405273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16415273Sgl147354 
16424061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
16434919Sxy150489 	e1000g_rx_clean(Adapter);
16444061Sxy150489 
16454061Sxy150489 	rw_exit(&Adapter->chip_lock);
16464919Sxy150489 
16474919Sxy150489 	if (global)
16484919Sxy150489 		e1000g_release_dma_resources(Adapter);
16494061Sxy150489 }
16504061Sxy150489 
16514061Sxy150489 static void
16524919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
16534919Sxy150489 {
16544919Sxy150489 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
16554919Sxy150489 
16564919Sxy150489 	if (rx_ring->rx_mblk != NULL) {
16574919Sxy150489 		freemsg(rx_ring->rx_mblk);
16584919Sxy150489 		rx_ring->rx_mblk = NULL;
16594919Sxy150489 		rx_ring->rx_mblk_tail = NULL;
16604919Sxy150489 		rx_ring->rx_mblk_len = 0;
16614919Sxy150489 	}
16624919Sxy150489 }
16634919Sxy150489 
16644919Sxy150489 static void
16654919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
16664061Sxy150489 {
16674061Sxy150489 	e1000g_tx_ring_t *tx_ring;
16684919Sxy150489 	p_tx_sw_packet_t packet;
16694061Sxy150489 	mblk_t *mp;
16704061Sxy150489 	mblk_t *nmp;
16714061Sxy150489 	uint32_t packet_count;
16724061Sxy150489 
16734061Sxy150489 	tx_ring = Adapter->tx_ring;
16744061Sxy150489 
16753526Sxy150489 	/*
16763526Sxy150489 	 * Here we don't need to protect the lists using
16773526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
16783526Sxy150489 	 * have been protected by the chip_lock.
16793526Sxy150489 	 */
16803526Sxy150489 	mp = NULL;
16813526Sxy150489 	nmp = NULL;
16824061Sxy150489 	packet_count = 0;
16834919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
16843526Sxy150489 	while (packet != NULL) {
16853526Sxy150489 		if (packet->mp != NULL) {
16863526Sxy150489 			/* Assemble the message chain */
16873526Sxy150489 			if (mp == NULL) {
16883526Sxy150489 				mp = packet->mp;
16893526Sxy150489 				nmp = packet->mp;
16903526Sxy150489 			} else {
16913526Sxy150489 				nmp->b_next = packet->mp;
16923526Sxy150489 				nmp = packet->mp;
16933526Sxy150489 			}
16943526Sxy150489 			/* Disconnect the message from the sw packet */
16953526Sxy150489 			packet->mp = NULL;
16963526Sxy150489 		}
16973526Sxy150489 
16984919Sxy150489 		e1000g_free_tx_swpkt(packet);
16994061Sxy150489 		packet_count++;
17003526Sxy150489 
17014919Sxy150489 		packet = (p_tx_sw_packet_t)
17023526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
17033526Sxy150489 	}
17043526Sxy150489 
17055882Syy150190 	if (mp != NULL)
17065882Syy150190 		freemsgchain(mp);
17074061Sxy150489 
17084061Sxy150489 	if (packet_count > 0) {
17094061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
17104061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
17114061Sxy150489 
17124061Sxy150489 		/* Setup TX descriptor pointers */
17134061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
17144061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
17154061Sxy150489 
17164061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
17176735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
17186735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
17193526Sxy150489 	}
17203526Sxy150489 }
17213526Sxy150489 
17223526Sxy150489 static boolean_t
17233526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
17243526Sxy150489 {
17253526Sxy150489 	int i;
17263526Sxy150489 	boolean_t done;
17273526Sxy150489 	e1000g_tx_ring_t *tx_ring;
17283526Sxy150489 
17293526Sxy150489 	tx_ring = Adapter->tx_ring;
17303526Sxy150489 
17313526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
17324919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
17333526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
17343526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
17353526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
17363526Sxy150489 
17373526Sxy150489 		if (done)
17383526Sxy150489 			break;
17393526Sxy150489 
17403526Sxy150489 		msec_delay(1);
17413526Sxy150489 	}
17423526Sxy150489 
17433526Sxy150489 	return (done);
17443526Sxy150489 }
17453526Sxy150489 
17463526Sxy150489 static boolean_t
17473526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
17483526Sxy150489 {
17494919Sxy150489 	e1000g_rx_ring_t *rx_ring;
17504919Sxy150489 	p_rx_sw_packet_t packet;
17513526Sxy150489 	boolean_t done;
17523526Sxy150489 
17534919Sxy150489 	rx_ring = Adapter->rx_ring;
17544919Sxy150489 	done = B_TRUE;
17554919Sxy150489 
17564919Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
17574919Sxy150489 
17584919Sxy150489 	while (rx_ring->pending_list != NULL) {
17594919Sxy150489 		packet = rx_ring->pending_list;
17604919Sxy150489 		rx_ring->pending_list =
17614919Sxy150489 		    rx_ring->pending_list->next;
17624919Sxy150489 
17634919Sxy150489 		if (packet->flag == E1000G_RX_SW_STOP) {
17644919Sxy150489 			packet->flag = E1000G_RX_SW_DETACH;
17654919Sxy150489 			done = B_FALSE;
17664919Sxy150489 		} else {
17674919Sxy150489 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
17684919Sxy150489 			ASSERT(packet->mp == NULL);
17694919Sxy150489 			e1000g_free_rx_sw_packet(packet);
17704919Sxy150489 		}
17714919Sxy150489 	}
17724919Sxy150489 
17734919Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
17743526Sxy150489 
17753526Sxy150489 	return (done);
17763526Sxy150489 }
17773526Sxy150489 
17784061Sxy150489 boolean_t
17793526Sxy150489 e1000g_reset(struct e1000g *Adapter)
17803526Sxy150489 {
17814919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
17824919Sxy150489 
17834919Sxy150489 	if (e1000g_start(Adapter, B_FALSE)) {
17843526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
17853526Sxy150489 		return (B_FALSE);
17863526Sxy150489 	}
17873526Sxy150489 
17883526Sxy150489 	return (B_TRUE);
17893526Sxy150489 }
17903526Sxy150489 
17915273Sgl147354 boolean_t
17925273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
17935273Sgl147354 {
17945273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
17955273Sgl147354 
17965273Sgl147354 	Adapter->init_count = 0;
17975273Sgl147354 
17985273Sgl147354 	if (e1000g_start(Adapter, B_TRUE)) {
17995273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
18005273Sgl147354 		return (B_FALSE);
18015273Sgl147354 	}
18025273Sgl147354 
18035273Sgl147354 	return (B_TRUE);
18045273Sgl147354 }
18055273Sgl147354 
18063526Sxy150489 /*
18074919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
18084919Sxy150489  *
18094919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
18104919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
18114919Sxy150489  * bit is set.
18123526Sxy150489  */
18133526Sxy150489 static uint_t
18143526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
18153526Sxy150489 {
18163526Sxy150489 	struct e1000g *Adapter;
18174919Sxy150489 	uint32_t icr;
18183526Sxy150489 
18197426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
18204919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18214919Sxy150489 
18225273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18235273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18245273Sgl147354 
18254919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
18263526Sxy150489 		/*
18273526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
18283526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18293526Sxy150489 		 * look for work to do.
18303526Sxy150489 		 */
18314919Sxy150489 		e1000g_intr_work(Adapter, icr);
18323526Sxy150489 		return (DDI_INTR_CLAIMED);
18333526Sxy150489 	} else {
18343526Sxy150489 		/*
18353526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
18363526Sxy150489 		 * Don't claim this interrupt, return immediately.
18373526Sxy150489 		 */
18383526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18393526Sxy150489 	}
18403526Sxy150489 }
18413526Sxy150489 
18423526Sxy150489 /*
18434919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
18444919Sxy150489  *
18454919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
18464919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
18474919Sxy150489  * bit is set or not.
18483526Sxy150489  */
18493526Sxy150489 static uint_t
18503526Sxy150489 e1000g_intr(caddr_t arg)
18513526Sxy150489 {
18523526Sxy150489 	struct e1000g *Adapter;
18534919Sxy150489 	uint32_t icr;
18543526Sxy150489 
18557426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
18564919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18574919Sxy150489 
18585273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18595273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18605273Sgl147354 
18614919Sxy150489 	if (icr) {
18623526Sxy150489 		/*
18633526Sxy150489 		 * Any bit was set in ICR:
18643526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18653526Sxy150489 		 * look for work to do.
18663526Sxy150489 		 */
18674919Sxy150489 		e1000g_intr_work(Adapter, icr);
18683526Sxy150489 		return (DDI_INTR_CLAIMED);
18693526Sxy150489 	} else {
18703526Sxy150489 		/*
18713526Sxy150489 		 * No bit was set in ICR:
18723526Sxy150489 		 * Don't claim this interrupt, return immediately.
18733526Sxy150489 		 */
18743526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18753526Sxy150489 	}
18763526Sxy150489 }
18773526Sxy150489 
18783526Sxy150489 /*
18794919Sxy150489  * e1000g_intr_work - actual processing of ISR
18804919Sxy150489  *
18814919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
18824919Sxy150489  * processing routines.
18833526Sxy150489  */
18843526Sxy150489 static void
18854919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
18863526Sxy150489 {
18875882Syy150190 	struct e1000_hw *hw;
18885882Syy150190 	hw = &Adapter->shared;
18895882Syy150190 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
18905882Syy150190 	uint32_t itr;
18915882Syy150190 
18925882Syy150190 	Adapter->rx_pkt_cnt = 0;
18935882Syy150190 	Adapter->tx_pkt_cnt = 0;
18945882Syy150190 
18954919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
18964919Sxy150489 	/*
18975273Sgl147354 	 * Here we need to check the "chip_state" flag within the chip_lock to
18984919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
18994919Sxy150489 	 * being reset.
19004919Sxy150489 	 */
19015273Sgl147354 	if (Adapter->chip_state != E1000G_START) {
19024919Sxy150489 		rw_exit(&Adapter->chip_lock);
19034919Sxy150489 		return;
19044919Sxy150489 	}
19054919Sxy150489 
19064919Sxy150489 	if (icr & E1000_ICR_RXT0) {
19073526Sxy150489 		mblk_t *mp;
19083526Sxy150489 
19096735Scc210113 		mutex_enter(&Adapter->rx_ring->rx_lock);
19104919Sxy150489 		mp = e1000g_receive(Adapter);
19116735Scc210113 		mutex_exit(&Adapter->rx_ring->rx_lock);
19124919Sxy150489 
19134919Sxy150489 		rw_exit(&Adapter->chip_lock);
19144919Sxy150489 
19154919Sxy150489 		if (mp != NULL)
19164919Sxy150489 			mac_rx(Adapter->mh, Adapter->mrh, mp);
19174919Sxy150489 	} else
19184919Sxy150489 		rw_exit(&Adapter->chip_lock);
19193526Sxy150489 
19205882Syy150190 	if (icr & E1000_ICR_TXDW) {
19215882Syy150190 		if (!Adapter->tx_intr_enable)
19225882Syy150190 			e1000g_clear_tx_interrupt(Adapter);
19235882Syy150190 
19245882Syy150190 		/* Recycle the tx descriptors */
19255882Syy150190 		rw_enter(&Adapter->chip_lock, RW_READER);
19267426SChenliang.Xu@Sun.COM 		(void) e1000g_recycle(tx_ring);
19275882Syy150190 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
19285882Syy150190 		rw_exit(&Adapter->chip_lock);
19295882Syy150190 
19305882Syy150190 		/* Schedule the re-transmit */
19315882Syy150190 		if (tx_ring->resched_needed &&
19325882Syy150190 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
19335882Syy150190 			tx_ring->resched_needed = B_FALSE;
19345882Syy150190 			mac_tx_update(Adapter->mh);
19355882Syy150190 			E1000G_STAT(tx_ring->stat_reschedule);
19365882Syy150190 		}
19375882Syy150190 	}
19385882Syy150190 
19395882Syy150190 	if (Adapter->intr_adaptive) {
19405882Syy150190 		itr = e1000g_get_itr(Adapter->rx_pkt_cnt, Adapter->tx_pkt_cnt,
19415882Syy150190 		    Adapter->intr_throttling_rate);
19425882Syy150190 		if (itr) {
19435882Syy150190 			E1000_WRITE_REG(hw, E1000_ITR, itr);
19445882Syy150190 			Adapter->intr_throttling_rate = itr;
19455882Syy150190 		}
19465882Syy150190 	}
19475882Syy150190 
19483526Sxy150489 	/*
19493526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
19503526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
19513526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
19523526Sxy150489 	 * are an indication that cable is not connected.
19533526Sxy150489 	 */
19544919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
19554919Sxy150489 	    (icr & E1000_ICR_LSC) ||
19564919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
19574061Sxy150489 		boolean_t link_changed;
19584061Sxy150489 		timeout_id_t tid = 0;
19593526Sxy150489 
19604919Sxy150489 		stop_watchdog_timer(Adapter);
19614919Sxy150489 
19625082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
19635082Syy150190 
19645082Syy150190 		/*
19655082Syy150190 		 * Because we got a link-status-change interrupt, force
19665082Syy150190 		 * e1000_check_for_link() to look at phy
19675082Syy150190 		 */
19685082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
19695082Syy150190 
19704061Sxy150489 		/* e1000g_link_check takes care of link status change */
19714061Sxy150489 		link_changed = e1000g_link_check(Adapter);
19725082Syy150190 
19735082Syy150190 		/* Get new phy state */
19745082Syy150190 		e1000g_get_phy_state(Adapter);
19755082Syy150190 
19764061Sxy150489 		/*
19774061Sxy150489 		 * If the link timer has not timed out, we'll not notify
19784919Sxy150489 		 * the upper layer with any link state until the link is up.
19794061Sxy150489 		 */
19804061Sxy150489 		if (link_changed && !Adapter->link_complete) {
19814061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
19825082Syy150190 				mutex_enter(&Adapter->link_lock);
19834061Sxy150489 				Adapter->link_complete = B_TRUE;
19844061Sxy150489 				tid = Adapter->link_tid;
19854061Sxy150489 				Adapter->link_tid = 0;
19865082Syy150190 				mutex_exit(&Adapter->link_lock);
19874061Sxy150489 			} else {
19884061Sxy150489 				link_changed = B_FALSE;
19894061Sxy150489 			}
19904061Sxy150489 		}
19915082Syy150190 		rw_exit(&Adapter->chip_lock);
19923526Sxy150489 
19934061Sxy150489 		if (link_changed) {
19944061Sxy150489 			if (tid != 0)
19954061Sxy150489 				(void) untimeout(tid);
19964061Sxy150489 
19974139Sxy150489 			/*
19984139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
19997133Scc210113 			 * down event. Stop receiver here and reset in watchdog.
20004139Sxy150489 			 */
20014139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
20027133Scc210113 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
20037133Scc210113 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
20047133Scc210113 				E1000_WRITE_REG(hw, E1000_RCTL,
20057133Scc210113 				    rctl & ~E1000_RCTL_EN);
20067133Scc210113 				e1000g_log(Adapter, CE_WARN,
20077133Scc210113 				    "ESB2 receiver disabled");
20087133Scc210113 				Adapter->esb2_workaround = B_TRUE;
20097133Scc210113 			}
20104139Sxy150489 
20114061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
20123526Sxy150489 		}
20133526Sxy150489 
20144919Sxy150489 		start_watchdog_timer(Adapter);
20153526Sxy150489 	}
20165882Syy150190 }
20175882Syy150190 
20185882Syy150190 static uint32_t
20195882Syy150190 e1000g_get_itr(uint32_t rx_packet, uint32_t tx_packet, uint32_t cur_itr)
20205882Syy150190 {
20215882Syy150190 	uint32_t new_itr;
20225882Syy150190 
20235882Syy150190 	/*
20245882Syy150190 	 * Determine a propper itr according to rx/tx packet count
20255882Syy150190 	 * per interrupt, the value of itr are based on document
20265882Syy150190 	 * and testing.
20275882Syy150190 	 */
20285882Syy150190 	if ((rx_packet < DEFAULT_INTR_PACKET_LOW) ||
20295882Syy150190 	    (tx_packet < DEFAULT_INTR_PACKET_LOW)) {
20305882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20315882Syy150190 		goto itr_done;
20323526Sxy150489 	}
20335882Syy150190 	if ((rx_packet > DEFAULT_INTR_PACKET_HIGH) ||
20345882Syy150190 	    (tx_packet > DEFAULT_INTR_PACKET_HIGH)) {
20355882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20365882Syy150190 		goto itr_done;
20375882Syy150190 	}
20385882Syy150190 	if (cur_itr < DEFAULT_INTR_THROTTLING_HIGH) {
20395882Syy150190 		new_itr = cur_itr + (DEFAULT_INTR_THROTTLING_HIGH >> 2);
20405882Syy150190 		if (new_itr > DEFAULT_INTR_THROTTLING_HIGH)
20415882Syy150190 			new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20425882Syy150190 	} else
20435882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20445882Syy150190 
20455882Syy150190 itr_done:
20465882Syy150190 	if (cur_itr == new_itr)
20475882Syy150190 		return (0);
20485882Syy150190 	else
20495882Syy150190 		return (new_itr);
20503526Sxy150489 }
20513526Sxy150489 
20523526Sxy150489 static void
20533526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
20543526Sxy150489 {
20553526Sxy150489 	struct e1000_hw *hw;
20563526Sxy150489 	int slot;
20573526Sxy150489 
20584919Sxy150489 	hw = &Adapter->shared;
20593526Sxy150489 
20605273Sgl147354 	if (!Adapter->unicst_init) {
20613526Sxy150489 		/* Initialize the multiple unicast addresses */
20623526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
20633526Sxy150489 
20644919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20654919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20663526Sxy150489 			Adapter->unicst_total--;
20673526Sxy150489 
20683526Sxy150489 		Adapter->unicst_avail = Adapter->unicst_total - 1;
20693526Sxy150489 
20703526Sxy150489 		/* Store the default mac address */
20714919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20724919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20734919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20744919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20754919Sxy150489 
20764919Sxy150489 		bcopy(hw->mac.addr, Adapter->unicst_addr[0].mac.addr,
20773526Sxy150489 		    ETHERADDRL);
20783526Sxy150489 		Adapter->unicst_addr[0].mac.set = 1;
20793526Sxy150489 
20803526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20813526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 0;
20825273Sgl147354 
20835273Sgl147354 		Adapter->unicst_init = B_TRUE;
20843526Sxy150489 	} else {
20853526Sxy150489 		/* Recover the default mac address */
20864919Sxy150489 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac.addr,
20873526Sxy150489 		    ETHERADDRL);
20883526Sxy150489 
20893526Sxy150489 		/* Store the default mac address */
20904919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20914919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20924919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20934919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20943526Sxy150489 
20953526Sxy150489 		/* Re-configure the RAR registers */
20963526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20973526Sxy150489 			e1000_rar_set(hw,
20983526Sxy150489 			    Adapter->unicst_addr[slot].mac.addr, slot);
20993526Sxy150489 	}
21005273Sgl147354 
21015273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
21025273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21033526Sxy150489 }
21043526Sxy150489 
21053526Sxy150489 static int
21063526Sxy150489 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
21073526Sxy150489 {
21083526Sxy150489 	struct e1000g *Adapter;
21093526Sxy150489 
21103526Sxy150489 	Adapter = (struct e1000g *)arg;
21113526Sxy150489 
21123526Sxy150489 	/* Store the default MAC address */
21134919Sxy150489 	bcopy(mac_addr, Adapter->shared.mac.addr, ETHERADDRL);
21143526Sxy150489 
21153526Sxy150489 	/* Set MAC address in address slot 0, which is the default address */
21163526Sxy150489 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
21173526Sxy150489 }
21183526Sxy150489 
21193526Sxy150489 static int
21203526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
21213526Sxy150489     mac_addr_slot_t slot)
21223526Sxy150489 {
21233526Sxy150489 	struct e1000_hw *hw;
21243526Sxy150489 
21254919Sxy150489 	hw = &Adapter->shared;
21263526Sxy150489 
21273526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21283526Sxy150489 
21293526Sxy150489 	/*
21303526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
21313526Sxy150489 	 * that requires the receiver to be in reset when any of the
21323526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
21333526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
21343526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
21353526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
21363526Sxy150489 	 * and work around any necessary HW errata.
21373526Sxy150489 	 */
21384919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21394919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21403526Sxy150489 		e1000_pci_clear_mwi(hw);
21414919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
21424919Sxy150489 		msec_delay(5);
21433526Sxy150489 	}
21443526Sxy150489 
21453526Sxy150489 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
21463526Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
21473526Sxy150489 
21483526Sxy150489 	if (slot == 0) {
21494919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21504919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21514919Sxy150489 			e1000_rar_set(hw, (uint8_t *)mac_addr, LAST_RAR_ENTRY);
21523526Sxy150489 	}
21533526Sxy150489 
21543526Sxy150489 	/*
21553526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
21563526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
21573526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
21583526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
21593526Sxy150489 	 */
21604919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21614919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21624919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
21634919Sxy150489 		msec_delay(1);
21644919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
21653526Sxy150489 			e1000_pci_set_mwi(hw);
21664919Sxy150489 		e1000g_rx_setup(Adapter);
21673526Sxy150489 	}
21683526Sxy150489 
21693526Sxy150489 	rw_exit(&Adapter->chip_lock);
21703526Sxy150489 
21715273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
21725273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21735273Sgl147354 		return (EIO);
21745273Sgl147354 	}
21755273Sgl147354 
21763526Sxy150489 	return (0);
21773526Sxy150489 }
21783526Sxy150489 
21793526Sxy150489 /*
21803526Sxy150489  * e1000g_m_unicst_add() - will find an unused address slot, set the
21813526Sxy150489  * address value to the one specified, reserve that slot and enable
21823526Sxy150489  * the NIC to start filtering on the new MAC address.
21833526Sxy150489  * Returns 0 on success.
21843526Sxy150489  */
21853526Sxy150489 static int
21863526Sxy150489 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
21873526Sxy150489 {
21883526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
21893526Sxy150489 	mac_addr_slot_t slot;
21903526Sxy150489 	int err;
21913526Sxy150489 
21923526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
21933526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
21943526Sxy150489 		return (EINVAL);
21953526Sxy150489 
21963526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21973526Sxy150489 	if (Adapter->unicst_avail == 0) {
21983526Sxy150489 		/* no slots available */
21993526Sxy150489 		rw_exit(&Adapter->chip_lock);
22003526Sxy150489 		return (ENOSPC);
22013526Sxy150489 	}
22023526Sxy150489 
22033526Sxy150489 	/*
22043526Sxy150489 	 * Primary/default address is in slot 0. The next addresses
22053526Sxy150489 	 * are the multiple MAC addresses. So multiple MAC address 0
22063526Sxy150489 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
22073526Sxy150489 	 * MAC address resides in slot 1.
22083526Sxy150489 	 */
22093526Sxy150489 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
22103526Sxy150489 		if (Adapter->unicst_addr[slot].mac.set == 0) {
22113526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
22123526Sxy150489 			break;
22133526Sxy150489 		}
22143526Sxy150489 	}
22153526Sxy150489 
22163526Sxy150489 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
22173526Sxy150489 
22183526Sxy150489 	Adapter->unicst_avail--;
22193526Sxy150489 	rw_exit(&Adapter->chip_lock);
22203526Sxy150489 
22213526Sxy150489 	maddr->mma_slot = slot;
22223526Sxy150489 
22233526Sxy150489 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
22243526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_WRITER);
22253526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22263526Sxy150489 		Adapter->unicst_avail++;
22273526Sxy150489 		rw_exit(&Adapter->chip_lock);
22283526Sxy150489 	}
22293526Sxy150489 
22303526Sxy150489 	return (err);
22313526Sxy150489 }
22323526Sxy150489 
22333526Sxy150489 /*
22343526Sxy150489  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
22353526Sxy150489  * call to e1000g_m_unicst_add(). The slot number that was returned in
22363526Sxy150489  * e1000g_m_unicst_add() is passed in the call to remove the address.
22373526Sxy150489  * Returns 0 on success.
22383526Sxy150489  */
22393526Sxy150489 static int
22403526Sxy150489 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
22413526Sxy150489 {
22423526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22433526Sxy150489 	int err;
22443526Sxy150489 
22453526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22463526Sxy150489 		return (EINVAL);
22473526Sxy150489 
22483526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22493526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22503526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22513526Sxy150489 		Adapter->unicst_avail++;
22523526Sxy150489 		rw_exit(&Adapter->chip_lock);
22533526Sxy150489 
22543526Sxy150489 		/* Copy the default address to the passed slot */
22557426SChenliang.Xu@Sun.COM 		if ((err = e1000g_unicst_set(Adapter,
22567426SChenliang.Xu@Sun.COM 		    Adapter->unicst_addr[0].mac.addr, slot)) != 0) {
22573526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_WRITER);
22583526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
22593526Sxy150489 			Adapter->unicst_avail--;
22603526Sxy150489 			rw_exit(&Adapter->chip_lock);
22613526Sxy150489 		}
22623526Sxy150489 		return (err);
22633526Sxy150489 	}
22643526Sxy150489 	rw_exit(&Adapter->chip_lock);
22653526Sxy150489 
22663526Sxy150489 	return (EINVAL);
22673526Sxy150489 }
22683526Sxy150489 
22693526Sxy150489 /*
22703526Sxy150489  * e1000g_m_unicst_modify() - modifies the value of an address that
22713526Sxy150489  * has been added by e1000g_m_unicst_add(). The new address, address
22723526Sxy150489  * length and the slot number that was returned in the call to add
22733526Sxy150489  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
22743526Sxy150489  * set to 0. Returns 0 on success.
22753526Sxy150489  */
22763526Sxy150489 static int
22773526Sxy150489 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
22783526Sxy150489 {
22793526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22803526Sxy150489 	mac_addr_slot_t slot;
22813526Sxy150489 
22823526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
22833526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
22843526Sxy150489 		return (EINVAL);
22853526Sxy150489 
22863526Sxy150489 	slot = maddr->mma_slot;
22873526Sxy150489 
22883526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22893526Sxy150489 		return (EINVAL);
22903526Sxy150489 
22913526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22923526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22933526Sxy150489 		rw_exit(&Adapter->chip_lock);
22943526Sxy150489 
22953526Sxy150489 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
22963526Sxy150489 	}
22973526Sxy150489 	rw_exit(&Adapter->chip_lock);
22983526Sxy150489 
22993526Sxy150489 	return (EINVAL);
23003526Sxy150489 }
23013526Sxy150489 
23023526Sxy150489 /*
23033526Sxy150489  * e1000g_m_unicst_get() - will get the MAC address and all other
23043526Sxy150489  * information related to the address slot passed in mac_multi_addr_t.
23053526Sxy150489  * mma_flags should be set to 0 in the call.
23063526Sxy150489  * On return, mma_flags can take the following values:
23073526Sxy150489  * 1) MMAC_SLOT_UNUSED
23083526Sxy150489  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
23093526Sxy150489  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
23103526Sxy150489  * 4) MMAC_SLOT_USED
23113526Sxy150489  */
23123526Sxy150489 static int
23133526Sxy150489 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
23143526Sxy150489 {
23153526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
23163526Sxy150489 	mac_addr_slot_t slot;
23173526Sxy150489 
23183526Sxy150489 	slot = maddr->mma_slot;
23193526Sxy150489 
23203526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
23213526Sxy150489 		return (EINVAL);
23223526Sxy150489 
23233526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23243526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
23253526Sxy150489 		bcopy(Adapter->unicst_addr[slot].mac.addr,
23263526Sxy150489 		    maddr->mma_addr, ETHERADDRL);
23273526Sxy150489 		maddr->mma_flags = MMAC_SLOT_USED;
23283526Sxy150489 	} else {
23293526Sxy150489 		maddr->mma_flags = MMAC_SLOT_UNUSED;
23303526Sxy150489 	}
23313526Sxy150489 	rw_exit(&Adapter->chip_lock);
23323526Sxy150489 
23333526Sxy150489 	return (0);
23343526Sxy150489 }
23353526Sxy150489 
23363526Sxy150489 static int
23373526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
23383526Sxy150489 {
23394919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23403526Sxy150489 	int res = 0;
23413526Sxy150489 
23423526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23433526Sxy150489 
23443526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
23453526Sxy150489 		res = EINVAL;
23463526Sxy150489 		goto done;
23473526Sxy150489 	}
23483526Sxy150489 
23493526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
23503526Sxy150489 		res = ENOENT;
23513526Sxy150489 		goto done;
23523526Sxy150489 	}
23533526Sxy150489 
23543526Sxy150489 	bcopy(multiaddr,
23553526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
23563526Sxy150489 	Adapter->mcast_count++;
23573526Sxy150489 
23583526Sxy150489 	/*
23593526Sxy150489 	 * Update the MC table in the hardware
23603526Sxy150489 	 */
23614919Sxy150489 	e1000g_clear_interrupt(Adapter);
23624919Sxy150489 
23634919Sxy150489 	e1000g_setup_multicast(Adapter);
23644919Sxy150489 
23654919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23664919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23674919Sxy150489 		e1000g_rx_setup(Adapter);
23684919Sxy150489 
23694919Sxy150489 	e1000g_mask_interrupt(Adapter);
23703526Sxy150489 
23713526Sxy150489 done:
23723526Sxy150489 	rw_exit(&Adapter->chip_lock);
23735273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23745273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23755273Sgl147354 		res = EIO;
23765273Sgl147354 	}
23775273Sgl147354 
23783526Sxy150489 	return (res);
23793526Sxy150489 }
23803526Sxy150489 
23813526Sxy150489 static int
23823526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
23833526Sxy150489 {
23844919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23853526Sxy150489 	unsigned i;
23863526Sxy150489 
23873526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23883526Sxy150489 
23893526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
23903526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
23913526Sxy150489 		    ETHERADDRL) == 0) {
23923526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
23933526Sxy150489 				Adapter->mcast_table[i - 1] =
23943526Sxy150489 				    Adapter->mcast_table[i];
23953526Sxy150489 			}
23963526Sxy150489 			Adapter->mcast_count--;
23973526Sxy150489 			break;
23983526Sxy150489 		}
23993526Sxy150489 	}
24003526Sxy150489 
24013526Sxy150489 	/*
24023526Sxy150489 	 * Update the MC table in the hardware
24033526Sxy150489 	 */
24044919Sxy150489 	e1000g_clear_interrupt(Adapter);
24054919Sxy150489 
24064919Sxy150489 	e1000g_setup_multicast(Adapter);
24074919Sxy150489 
24084919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24094919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
24104919Sxy150489 		e1000g_rx_setup(Adapter);
24114919Sxy150489 
24124919Sxy150489 	e1000g_mask_interrupt(Adapter);
24133526Sxy150489 
24143526Sxy150489 done:
24153526Sxy150489 	rw_exit(&Adapter->chip_lock);
24165273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
24175273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24185273Sgl147354 		return (EIO);
24195273Sgl147354 	}
24205273Sgl147354 
24213526Sxy150489 	return (0);
24223526Sxy150489 }
24233526Sxy150489 
24244919Sxy150489 /*
24254919Sxy150489  * e1000g_setup_multicast - setup multicast data structures
24264919Sxy150489  *
24274919Sxy150489  * This routine initializes all of the multicast related structures.
24284919Sxy150489  */
24294919Sxy150489 void
24304919Sxy150489 e1000g_setup_multicast(struct e1000g *Adapter)
24314919Sxy150489 {
24324919Sxy150489 	uint8_t *mc_addr_list;
24334919Sxy150489 	uint32_t mc_addr_count;
24344919Sxy150489 	uint32_t rctl;
24354919Sxy150489 	struct e1000_hw *hw;
24364919Sxy150489 
24374919Sxy150489 	hw = &Adapter->shared;
24384919Sxy150489 
24394919Sxy150489 	/*
24404919Sxy150489 	 * The e1000g has the ability to do perfect filtering of 16
24414919Sxy150489 	 * addresses. The driver uses one of the e1000g's 16 receive
24424919Sxy150489 	 * address registers for its node/network/mac/individual address.
24434919Sxy150489 	 * So, we have room for up to 15 multicast addresses in the CAM,
24444919Sxy150489 	 * additional MC addresses are handled by the MTA (Multicast Table
24454919Sxy150489 	 * Array)
24464919Sxy150489 	 */
24474919Sxy150489 
24484919Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
24494919Sxy150489 
24504919Sxy150489 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
24514919Sxy150489 
24524919Sxy150489 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
24534919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
24544919Sxy150489 		    "Adapter requested more than %d MC Addresses.\n",
24554919Sxy150489 		    MAX_NUM_MULTICAST_ADDRESSES);
24564919Sxy150489 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
24574919Sxy150489 	} else {
24584919Sxy150489 		/*
24594919Sxy150489 		 * Set the number of MC addresses that we are being
24604919Sxy150489 		 * requested to use
24614919Sxy150489 		 */
24624919Sxy150489 		mc_addr_count = Adapter->mcast_count;
24634919Sxy150489 	}
24644919Sxy150489 	/*
24654919Sxy150489 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
24664919Sxy150489 	 * hang  while writing to the receive address registers if the receiver
24674919Sxy150489 	 * is not in reset before writing to the registers. Updating the RAR
24684919Sxy150489 	 * is done during the setting up of the multicast table, hence the
24694919Sxy150489 	 * receiver has to be put in reset before updating the multicast table
24704919Sxy150489 	 * and then taken out of reset at the end
24714919Sxy150489 	 */
24724919Sxy150489 	/*
24734919Sxy150489 	 * if WMI was enabled then dis able it before issueing the global
24744919Sxy150489 	 * reset to the hardware.
24754919Sxy150489 	 */
24764919Sxy150489 	/*
24774919Sxy150489 	 * Only required for WISEMAN_2_0
24784919Sxy150489 	 */
24794919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24804919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24814919Sxy150489 		e1000_pci_clear_mwi(hw);
24824919Sxy150489 		/*
24834919Sxy150489 		 * The e1000g must be in reset before changing any RA
24844919Sxy150489 		 * registers. Reset receive unit.  The chip will remain in
24854919Sxy150489 		 * the reset state until software explicitly restarts it.
24864919Sxy150489 		 */
24874919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
24884919Sxy150489 		/* Allow receiver time to go in to reset */
24894919Sxy150489 		msec_delay(5);
24904919Sxy150489 	}
24916735Scc210113 
24926735Scc210113 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
24934919Sxy150489 	    Adapter->unicst_total, hw->mac.rar_entry_count);
24944919Sxy150489 
24954919Sxy150489 	/*
24964919Sxy150489 	 * Only for Wiseman_2_0
24974919Sxy150489 	 * If MWI was enabled then re-enable it after issueing (as we
24984919Sxy150489 	 * disabled it up there) the receive reset command.
24994919Sxy150489 	 * Wainwright does not have a receive reset command and only thing
25004919Sxy150489 	 * close to it is global reset which will require tx setup also
25014919Sxy150489 	 */
25024919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
25034919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
25044919Sxy150489 		/*
25054919Sxy150489 		 * if WMI was enabled then reenable it after issueing the
25064919Sxy150489 		 * global or receive reset to the hardware.
25074919Sxy150489 		 */
25084919Sxy150489 
25094919Sxy150489 		/*
25104919Sxy150489 		 * Take receiver out of reset
25114919Sxy150489 		 * clear E1000_RCTL_RST bit (and all others)
25124919Sxy150489 		 */
25134919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
25144919Sxy150489 		msec_delay(5);
25154919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
25164919Sxy150489 			e1000_pci_set_mwi(hw);
25174919Sxy150489 	}
25184919Sxy150489 
25194919Sxy150489 	/*
25204919Sxy150489 	 * Restore original value
25214919Sxy150489 	 */
25224919Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
25234919Sxy150489 }
25244919Sxy150489 
25253526Sxy150489 int
25263526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
25273526Sxy150489 {
25283526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25293526Sxy150489 
25303526Sxy150489 	return ((add) ? multicst_add(Adapter, addr)
25314349Sxy150489 	    : multicst_remove(Adapter, addr));
25323526Sxy150489 }
25333526Sxy150489 
25343526Sxy150489 int
25353526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
25363526Sxy150489 {
25373526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25384919Sxy150489 	uint32_t rctl;
25393526Sxy150489 
25403526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25413526Sxy150489 
25424919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
25433526Sxy150489 
25443526Sxy150489 	if (on)
25454919Sxy150489 		rctl |=
25463526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
25473526Sxy150489 	else
25484919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
25494919Sxy150489 
25504919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
25513526Sxy150489 
25523526Sxy150489 	Adapter->e1000g_promisc = on;
25533526Sxy150489 
25543526Sxy150489 	rw_exit(&Adapter->chip_lock);
25553526Sxy150489 
25565273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
25575273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
25585273Sgl147354 		return (EIO);
25595273Sgl147354 	}
25605273Sgl147354 
25613526Sxy150489 	return (0);
25623526Sxy150489 }
25633526Sxy150489 
25643526Sxy150489 static boolean_t
25653526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
25663526Sxy150489 {
25673526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25683526Sxy150489 
25693526Sxy150489 	switch (cap) {
25703526Sxy150489 	case MAC_CAPAB_HCKSUM: {
25713526Sxy150489 		uint32_t *txflags = cap_data;
25726986Smx205022 
25736986Smx205022 		if (Adapter->tx_hcksum_enable)
25746986Smx205022 			*txflags = HCKSUM_IPHDRCKSUM |
25756986Smx205022 			    HCKSUM_INET_PARTIAL;
25766986Smx205022 		else
25773526Sxy150489 			return (B_FALSE);
25783526Sxy150489 		break;
25793526Sxy150489 	}
25803526Sxy150489 	case MAC_CAPAB_POLL:
25813526Sxy150489 		/*
25823526Sxy150489 		 * There's nothing for us to fill in, simply returning
25833526Sxy150489 		 * B_TRUE stating that we support polling is sufficient.
25843526Sxy150489 		 */
25853526Sxy150489 		break;
25863526Sxy150489 
25873526Sxy150489 	case MAC_CAPAB_MULTIADDRESS: {
25883526Sxy150489 		multiaddress_capab_t *mmacp = cap_data;
25893526Sxy150489 
25903526Sxy150489 		/*
25913526Sxy150489 		 * The number of MAC addresses made available by
25923526Sxy150489 		 * this capability is one less than the total as
25933526Sxy150489 		 * the primary address in slot 0 is counted in
25943526Sxy150489 		 * the total.
25953526Sxy150489 		 */
25963526Sxy150489 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
25973526Sxy150489 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
25983526Sxy150489 		/* No multiple factory addresses, set mma_flag to 0 */
25993526Sxy150489 		mmacp->maddr_flag = 0;
26003526Sxy150489 		mmacp->maddr_handle = Adapter;
26013526Sxy150489 		mmacp->maddr_add = e1000g_m_unicst_add;
26023526Sxy150489 		mmacp->maddr_remove = e1000g_m_unicst_remove;
26033526Sxy150489 		mmacp->maddr_modify = e1000g_m_unicst_modify;
26043526Sxy150489 		mmacp->maddr_get = e1000g_m_unicst_get;
26053526Sxy150489 		mmacp->maddr_reserve = NULL;
26063526Sxy150489 		break;
26073526Sxy150489 	}
26086986Smx205022 
26096986Smx205022 	case MAC_CAPAB_LSO: {
26106986Smx205022 		mac_capab_lso_t *cap_lso = cap_data;
26116986Smx205022 
26126986Smx205022 		if (Adapter->lso_enable) {
26136986Smx205022 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
26146986Smx205022 			cap_lso->lso_basic_tcp_ipv4.lso_max =
26156986Smx205022 			    E1000_LSO_MAXLEN;
26166986Smx205022 		} else
26176986Smx205022 			return (B_FALSE);
26186986Smx205022 		break;
26196986Smx205022 	}
26206986Smx205022 
26213526Sxy150489 	default:
26223526Sxy150489 		return (B_FALSE);
26233526Sxy150489 	}
26243526Sxy150489 	return (B_TRUE);
26253526Sxy150489 }
26263526Sxy150489 
26276394Scc210113 static boolean_t
26286394Scc210113 e1000g_param_locked(mac_prop_id_t pr_num)
26296394Scc210113 {
26306394Scc210113 	/*
26316394Scc210113 	 * All en_* parameters are locked (read-only) while
26326394Scc210113 	 * the device is in any sort of loopback mode ...
26336394Scc210113 	 */
26346394Scc210113 	switch (pr_num) {
26356789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
26366789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
26376789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
26386789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
26396789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
26406789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
26416789Sam223141 		case MAC_PROP_AUTONEG:
26426789Sam223141 		case MAC_PROP_FLOWCTRL:
26436394Scc210113 			return (B_TRUE);
26446394Scc210113 	}
26456394Scc210113 	return (B_FALSE);
26466394Scc210113 }
26476394Scc210113 
26486394Scc210113 /*
26496394Scc210113  * callback function for set/get of properties
26506394Scc210113  */
26516394Scc210113 static int
26526394Scc210113 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
26536394Scc210113     uint_t pr_valsize, const void *pr_val)
26546394Scc210113 {
26556394Scc210113 	struct e1000g *Adapter = arg;
26566394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
26576394Scc210113 	struct e1000_phy_info *phy = &Adapter->shared.phy;
26586735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
26596394Scc210113 	int err = 0;
26606735Scc210113 	link_flowctrl_t flowctrl;
26616512Ssowmini 	uint32_t cur_mtu, new_mtu;
26626394Scc210113 	uint64_t tmp = 0;
26636394Scc210113 
26646394Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
26656394Scc210113 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
26666394Scc210113 	    e1000g_param_locked(pr_num)) {
26676394Scc210113 		/*
26686394Scc210113 		 * All en_* parameters are locked (read-only)
26696394Scc210113 		 * while the device is in any sort of loopback mode.
26706394Scc210113 		 */
26716394Scc210113 		rw_exit(&Adapter->chip_lock);
26726394Scc210113 		return (EBUSY);
26736394Scc210113 	}
26746394Scc210113 
26756394Scc210113 	switch (pr_num) {
26766789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
26776394Scc210113 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
26786394Scc210113 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
26796394Scc210113 			goto reset;
26806789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
26816394Scc210113 			Adapter->param_en_1000hdx = *(uint8_t *)pr_val;
26826394Scc210113 			Adapter->param_adv_1000hdx = *(uint8_t *)pr_val;
26836394Scc210113 			goto reset;
26846789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
26856394Scc210113 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
26866394Scc210113 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
26876394Scc210113 			goto reset;
26886789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
26896394Scc210113 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
26906394Scc210113 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
26916394Scc210113 			goto reset;
26926789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
26936394Scc210113 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
26946394Scc210113 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
26956394Scc210113 			goto reset;
26966789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
26976394Scc210113 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
26986394Scc210113 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
26996394Scc210113 			goto reset;
27006789Sam223141 		case MAC_PROP_AUTONEG:
27016394Scc210113 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
27026394Scc210113 			goto reset;
27036789Sam223141 		case MAC_PROP_FLOWCTRL:
27046735Scc210113 			fc->send_xon = B_TRUE;
27056735Scc210113 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
27066735Scc210113 
27076735Scc210113 			switch (flowctrl) {
27086394Scc210113 			default:
27096394Scc210113 				err = EINVAL;
27106394Scc210113 				break;
27116394Scc210113 			case LINK_FLOWCTRL_NONE:
27126735Scc210113 				fc->type = e1000_fc_none;
27136394Scc210113 				break;
27146394Scc210113 			case LINK_FLOWCTRL_RX:
27156735Scc210113 				fc->type = e1000_fc_rx_pause;
27166394Scc210113 				break;
27176394Scc210113 			case LINK_FLOWCTRL_TX:
27186735Scc210113 				fc->type = e1000_fc_tx_pause;
27196394Scc210113 				break;
27206394Scc210113 			case LINK_FLOWCTRL_BI:
27216735Scc210113 				fc->type = e1000_fc_full;
27226394Scc210113 				break;
27236394Scc210113 			}
27246394Scc210113 reset:
27256394Scc210113 			if (err == 0) {
27266394Scc210113 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
27276394Scc210113 					err = EINVAL;
27286394Scc210113 			}
27296394Scc210113 			break;
27306789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
27316789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
27326789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
27336789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
27346789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
27356789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
27366789Sam223141 		case MAC_PROP_STATUS:
27376789Sam223141 		case MAC_PROP_SPEED:
27386789Sam223141 		case MAC_PROP_DUPLEX:
27396394Scc210113 			err = ENOTSUP; /* read-only prop. Can't set this. */
27406394Scc210113 			break;
27416789Sam223141 		case MAC_PROP_MTU:
27426394Scc210113 			cur_mtu = Adapter->default_mtu;
27436394Scc210113 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
27446394Scc210113 			if (new_mtu == cur_mtu) {
27456394Scc210113 				err = 0;
27466394Scc210113 				break;
27476394Scc210113 			}
27486394Scc210113 
27496394Scc210113 			tmp = new_mtu + sizeof (struct ether_vlan_header) +
27506394Scc210113 			    ETHERFCSL;
27516394Scc210113 			if ((tmp < DEFAULT_FRAME_SIZE) ||
27526394Scc210113 			    (tmp > MAXIMUM_FRAME_SIZE)) {
27536394Scc210113 				err = EINVAL;
27546394Scc210113 				break;
27556394Scc210113 			}
27566394Scc210113 
2757*7607STed.You@Sun.COM 			/* ich8 does not support jumbo frames */
27586394Scc210113 			if ((mac->type == e1000_ich8lan) &&
27596394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27606394Scc210113 				err = EINVAL;
27616394Scc210113 				break;
27626394Scc210113 			}
27636394Scc210113 			/* ich9 does not do jumbo frames on one phy type */
27646394Scc210113 			if ((mac->type == e1000_ich9lan) &&
27656394Scc210113 			    (phy->type == e1000_phy_ife) &&
27666394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27676394Scc210113 				err = EINVAL;
27686394Scc210113 				break;
27696394Scc210113 			}
27706394Scc210113 			if (Adapter->chip_state != E1000G_STOP) {
27716394Scc210113 				err = EBUSY;
27726394Scc210113 				break;
27736394Scc210113 			}
27746394Scc210113 
27756394Scc210113 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
27766394Scc210113 			if (err == 0) {
27777426SChenliang.Xu@Sun.COM 				Adapter->max_frame_size = (uint32_t)tmp;
27786394Scc210113 				Adapter->default_mtu = new_mtu;
27796394Scc210113 				e1000g_set_bufsize(Adapter);
27806394Scc210113 			}
27816394Scc210113 			break;
27826789Sam223141 		case MAC_PROP_PRIVATE:
27836394Scc210113 			err = e1000g_set_priv_prop(Adapter, pr_name,
27846394Scc210113 			    pr_valsize, pr_val);
27856394Scc210113 			break;
27866394Scc210113 		default:
27876394Scc210113 			err = ENOTSUP;
27886394Scc210113 			break;
27896394Scc210113 	}
27906394Scc210113 	rw_exit(&Adapter->chip_lock);
27916394Scc210113 	return (err);
27926394Scc210113 }
27936394Scc210113 
27946394Scc210113 static int
27956394Scc210113 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
27966512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
27976394Scc210113 {
27986394Scc210113 	struct e1000g *Adapter = arg;
27996735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
28006512Ssowmini 	int err = 0;
28016735Scc210113 	link_flowctrl_t flowctrl;
28026394Scc210113 	uint64_t tmp = 0;
28036394Scc210113 
28046512Ssowmini 	if (pr_valsize == 0)
28056512Ssowmini 		return (EINVAL);
28066512Ssowmini 
28076394Scc210113 	bzero(pr_val, pr_valsize);
28086789Sam223141 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
28096512Ssowmini 		return (e1000g_get_def_val(Adapter, pr_num,
28106512Ssowmini 		    pr_valsize, pr_val));
28116512Ssowmini 	}
28126512Ssowmini 
28136394Scc210113 	switch (pr_num) {
28146789Sam223141 		case MAC_PROP_DUPLEX:
28156512Ssowmini 			if (pr_valsize >= sizeof (link_duplex_t)) {
28166512Ssowmini 				bcopy(&Adapter->link_duplex, pr_val,
28176512Ssowmini 				    sizeof (link_duplex_t));
28186512Ssowmini 			} else
28196512Ssowmini 				err = EINVAL;
28206394Scc210113 			break;
28216789Sam223141 		case MAC_PROP_SPEED:
28226394Scc210113 			if (pr_valsize >= sizeof (uint64_t)) {
28236394Scc210113 				tmp = Adapter->link_speed * 1000000ull;
28246394Scc210113 				bcopy(&tmp, pr_val, sizeof (tmp));
28256512Ssowmini 			} else
28266512Ssowmini 				err = EINVAL;
28276394Scc210113 			break;
28286789Sam223141 		case MAC_PROP_AUTONEG:
28296512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
28306394Scc210113 			break;
28316789Sam223141 		case MAC_PROP_FLOWCTRL:
28326394Scc210113 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
28336735Scc210113 				switch (fc->type) {
28346394Scc210113 					case e1000_fc_none:
28356735Scc210113 						flowctrl = LINK_FLOWCTRL_NONE;
28366394Scc210113 						break;
28376394Scc210113 					case e1000_fc_rx_pause:
28386735Scc210113 						flowctrl = LINK_FLOWCTRL_RX;
28396394Scc210113 						break;
28406394Scc210113 					case e1000_fc_tx_pause:
28416735Scc210113 						flowctrl = LINK_FLOWCTRL_TX;
28426394Scc210113 						break;
28436394Scc210113 					case e1000_fc_full:
28446735Scc210113 						flowctrl = LINK_FLOWCTRL_BI;
28456394Scc210113 						break;
28466394Scc210113 				}
28476735Scc210113 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
28486512Ssowmini 			} else
28496512Ssowmini 				err = EINVAL;
28506394Scc210113 			break;
28516789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
28526512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
28536394Scc210113 			break;
28546789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
28556512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
28566394Scc210113 			break;
28576789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
28586512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
28596394Scc210113 			break;
28606789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
28616512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
28626394Scc210113 			break;
28636789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
28646512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
28656394Scc210113 			break;
28666789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
28676512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
28686394Scc210113 			break;
28696789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
28706512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
28716394Scc210113 			break;
28726789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
28736512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
28746394Scc210113 			break;
28756789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
28766512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
28776394Scc210113 			break;
28786789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
28796512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
28806394Scc210113 			break;
28816789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
28826512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
28836394Scc210113 			break;
28846789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
28856512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
28866512Ssowmini 			break;
28876789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
28886789Sam223141 		case MAC_PROP_EN_100T4_CAP:
28896512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
28906394Scc210113 			break;
28916789Sam223141 		case MAC_PROP_PRIVATE:
28926394Scc210113 			err = e1000g_get_priv_prop(Adapter, pr_name,
28936512Ssowmini 			    pr_flags, pr_valsize, pr_val);
28946394Scc210113 			break;
28956394Scc210113 		default:
28966394Scc210113 			err = ENOTSUP;
28976394Scc210113 			break;
28986394Scc210113 	}
28996394Scc210113 	return (err);
29006394Scc210113 }
29016394Scc210113 
29027426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
29036394Scc210113 static int
29046394Scc210113 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
29056394Scc210113     uint_t pr_valsize, const void *pr_val)
29066394Scc210113 {
29076394Scc210113 	int err = 0;
29086394Scc210113 	long result;
29096394Scc210113 	struct e1000_hw *hw = &Adapter->shared;
29106394Scc210113 
29116394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
29126394Scc210113 		if (pr_val == NULL) {
29136394Scc210113 			err = EINVAL;
29146394Scc210113 			return (err);
29156394Scc210113 		}
29166394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29176394Scc210113 		if (result < MIN_TX_BCOPY_THRESHOLD ||
29186394Scc210113 		    result > MAX_TX_BCOPY_THRESHOLD)
29196394Scc210113 			err = EINVAL;
29206394Scc210113 		else {
29216394Scc210113 			Adapter->tx_bcopy_thresh = (uint32_t)result;
29226394Scc210113 		}
29236394Scc210113 		return (err);
29246394Scc210113 	}
29256394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
29266394Scc210113 		if (pr_val == NULL) {
29276394Scc210113 			err = EINVAL;
29286394Scc210113 			return (err);
29296394Scc210113 		}
29306394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29316394Scc210113 		if (result < 0 || result > 1)
29326394Scc210113 			err = EINVAL;
29336394Scc210113 		else {
29346394Scc210113 			Adapter->tx_intr_enable = (result == 1) ?
29356394Scc210113 			    B_TRUE: B_FALSE;
29366394Scc210113 			if (Adapter->tx_intr_enable)
29376394Scc210113 				e1000g_mask_tx_interrupt(Adapter);
29386394Scc210113 			else
29396394Scc210113 				e1000g_clear_tx_interrupt(Adapter);
29406394Scc210113 			if (e1000g_check_acc_handle(
29416394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29426394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29436394Scc210113 				    DDI_SERVICE_DEGRADED);
29446394Scc210113 		}
29456394Scc210113 		return (err);
29466394Scc210113 	}
29476394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
29486394Scc210113 		if (pr_val == NULL) {
29496394Scc210113 			err = EINVAL;
29506394Scc210113 			return (err);
29516394Scc210113 		}
29526394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29536394Scc210113 		if (result < MIN_TX_INTR_DELAY ||
29546394Scc210113 		    result > MAX_TX_INTR_DELAY)
29556394Scc210113 			err = EINVAL;
29566394Scc210113 		else {
29576394Scc210113 			Adapter->tx_intr_delay = (uint32_t)result;
29586394Scc210113 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
29596394Scc210113 			if (e1000g_check_acc_handle(
29606394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29616394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29626394Scc210113 				    DDI_SERVICE_DEGRADED);
29636394Scc210113 		}
29646394Scc210113 		return (err);
29656394Scc210113 	}
29666394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
29676394Scc210113 		if (pr_val == NULL) {
29686394Scc210113 			err = EINVAL;
29696394Scc210113 			return (err);
29706394Scc210113 		}
29716394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29726394Scc210113 		if (result < MIN_TX_INTR_ABS_DELAY ||
29736394Scc210113 		    result > MAX_TX_INTR_ABS_DELAY)
29746394Scc210113 			err = EINVAL;
29756394Scc210113 		else {
29766394Scc210113 			Adapter->tx_intr_abs_delay = (uint32_t)result;
29776394Scc210113 			E1000_WRITE_REG(hw, E1000_TADV,
29786394Scc210113 			    Adapter->tx_intr_abs_delay);
29796394Scc210113 			if (e1000g_check_acc_handle(
29806394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29816394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29826394Scc210113 				    DDI_SERVICE_DEGRADED);
29836394Scc210113 		}
29846394Scc210113 		return (err);
29856394Scc210113 	}
29866394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
29876394Scc210113 		if (pr_val == NULL) {
29886394Scc210113 			err = EINVAL;
29896394Scc210113 			return (err);
29906394Scc210113 		}
29916394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29926394Scc210113 		if (result < MIN_RX_BCOPY_THRESHOLD ||
29936394Scc210113 		    result > MAX_RX_BCOPY_THRESHOLD)
29946394Scc210113 			err = EINVAL;
29956394Scc210113 		else
29966394Scc210113 			Adapter->rx_bcopy_thresh = (uint32_t)result;
29976394Scc210113 		return (err);
29986394Scc210113 	}
29996394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
30006394Scc210113 		if (pr_val == NULL) {
30016394Scc210113 			err = EINVAL;
30026394Scc210113 			return (err);
30036394Scc210113 		}
30046394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30056394Scc210113 		if (result < MIN_RX_LIMIT_ON_INTR ||
30066394Scc210113 		    result > MAX_RX_LIMIT_ON_INTR)
30076394Scc210113 			err = EINVAL;
30086394Scc210113 		else
30096394Scc210113 			Adapter->rx_limit_onintr = (uint32_t)result;
30106394Scc210113 		return (err);
30116394Scc210113 	}
30126394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
30136394Scc210113 		if (pr_val == NULL) {
30146394Scc210113 			err = EINVAL;
30156394Scc210113 			return (err);
30166394Scc210113 		}
30176394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30186394Scc210113 		if (result < MIN_RX_INTR_DELAY ||
30196394Scc210113 		    result > MAX_RX_INTR_DELAY)
30206394Scc210113 			err = EINVAL;
30216394Scc210113 		else {
30226394Scc210113 			Adapter->rx_intr_delay = (uint32_t)result;
30236394Scc210113 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
30246394Scc210113 			if (e1000g_check_acc_handle(
30256394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30266394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30276394Scc210113 				    DDI_SERVICE_DEGRADED);
30286394Scc210113 		}
30296394Scc210113 		return (err);
30306394Scc210113 	}
30316394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
30326394Scc210113 		if (pr_val == NULL) {
30336394Scc210113 			err = EINVAL;
30346394Scc210113 			return (err);
30356394Scc210113 		}
30366394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30376394Scc210113 		if (result < MIN_RX_INTR_ABS_DELAY ||
30386394Scc210113 		    result > MAX_RX_INTR_ABS_DELAY)
30396394Scc210113 			err = EINVAL;
30406394Scc210113 		else {
30416394Scc210113 			Adapter->rx_intr_abs_delay = (uint32_t)result;
30426394Scc210113 			E1000_WRITE_REG(hw, E1000_RADV,
30436394Scc210113 			    Adapter->rx_intr_abs_delay);
30446394Scc210113 			if (e1000g_check_acc_handle(
30456394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30466394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30476394Scc210113 				    DDI_SERVICE_DEGRADED);
30486394Scc210113 		}
30496394Scc210113 		return (err);
30506394Scc210113 	}
30516394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
30526394Scc210113 		if (pr_val == NULL) {
30536394Scc210113 			err = EINVAL;
30546394Scc210113 			return (err);
30556394Scc210113 		}
30566394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30576394Scc210113 		if (result < MIN_INTR_THROTTLING ||
30586394Scc210113 		    result > MAX_INTR_THROTTLING)
30596394Scc210113 			err = EINVAL;
30606394Scc210113 		else {
30616394Scc210113 			if (hw->mac.type >= e1000_82540) {
30626394Scc210113 				Adapter->intr_throttling_rate =
30636394Scc210113 				    (uint32_t)result;
30646394Scc210113 				E1000_WRITE_REG(hw, E1000_ITR,
30656394Scc210113 				    Adapter->intr_throttling_rate);
30666394Scc210113 				if (e1000g_check_acc_handle(
30676394Scc210113 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
30686394Scc210113 					ddi_fm_service_impact(Adapter->dip,
30696394Scc210113 					    DDI_SERVICE_DEGRADED);
30706394Scc210113 			} else
30716394Scc210113 				err = EINVAL;
30726394Scc210113 		}
30736394Scc210113 		return (err);
30746394Scc210113 	}
30756394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
30766394Scc210113 		if (pr_val == NULL) {
30776394Scc210113 			err = EINVAL;
30786394Scc210113 			return (err);
30796394Scc210113 		}
30806394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30816394Scc210113 		if (result < 0 || result > 1)
30826394Scc210113 			err = EINVAL;
30836394Scc210113 		else {
30846394Scc210113 			if (hw->mac.type >= e1000_82540) {
30856394Scc210113 				Adapter->intr_adaptive = (result == 1) ?
30866394Scc210113 				    B_TRUE : B_FALSE;
30876394Scc210113 			} else {
30886394Scc210113 				err = EINVAL;
30896394Scc210113 			}
30906394Scc210113 		}
30916394Scc210113 		return (err);
30926394Scc210113 	}
30936394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
30946394Scc210113 		if (pr_val == NULL) {
30956394Scc210113 			err = EINVAL;
30966394Scc210113 			return (err);
30976394Scc210113 		}
30986394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30996394Scc210113 		if (result < MIN_TX_RECYCLE_THRESHOLD ||
31006394Scc210113 		    result > MAX_TX_RECYCLE_THRESHOLD)
31016394Scc210113 			err = EINVAL;
31026394Scc210113 		else
31036394Scc210113 			Adapter->tx_recycle_thresh = (uint32_t)result;
31046394Scc210113 		return (err);
31056394Scc210113 	}
31066394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 0) {
31076394Scc210113 		if (pr_val == NULL) {
31086394Scc210113 			err = EINVAL;
31096394Scc210113 			return (err);
31106394Scc210113 		}
31116394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31126394Scc210113 		if (result < MIN_TX_RECYCLE_NUM ||
31136394Scc210113 		    result > MAX_TX_RECYCLE_NUM)
31146394Scc210113 			err = EINVAL;
31156394Scc210113 		else
31166394Scc210113 			Adapter->tx_recycle_num = (uint32_t)result;
31176394Scc210113 		return (err);
31186394Scc210113 	}
31196394Scc210113 	return (ENOTSUP);
31206394Scc210113 }
31216394Scc210113 
31226394Scc210113 static int
31236394Scc210113 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
31246512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
31256394Scc210113 {
31266394Scc210113 	int err = ENOTSUP;
31276789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
31286512Ssowmini 	int value;
31296512Ssowmini 
31306512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
31316512Ssowmini 		if (is_default)
31326512Ssowmini 			goto done;
31336512Ssowmini 		value = Adapter->param_adv_pause;
31346512Ssowmini 		err = 0;
31356512Ssowmini 		goto done;
31366512Ssowmini 	}
31376512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
31386512Ssowmini 		if (is_default)
31396512Ssowmini 			goto done;
31406512Ssowmini 		value = Adapter->param_adv_asym_pause;
31416512Ssowmini 		err = 0;
31426512Ssowmini 		goto done;
31436512Ssowmini 	}
31446394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
31456512Ssowmini 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
31466512Ssowmini 		    Adapter->tx_bcopy_thresh);
31476394Scc210113 		err = 0;
31486394Scc210113 		goto done;
31496394Scc210113 	}
31506394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
31516512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
31526512Ssowmini 		    Adapter->tx_intr_enable);
31536394Scc210113 		err = 0;
31546394Scc210113 		goto done;
31556394Scc210113 	}
31566394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
31576512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
31586512Ssowmini 		    Adapter->tx_intr_delay);
31596394Scc210113 		err = 0;
31606394Scc210113 		goto done;
31616394Scc210113 	}
31626394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
31636512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
31646512Ssowmini 		    Adapter->tx_intr_abs_delay);
31656394Scc210113 		err = 0;
31666394Scc210113 		goto done;
31676394Scc210113 	}
31686394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
31696512Ssowmini 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
31706512Ssowmini 		    Adapter->rx_bcopy_thresh);
31716394Scc210113 		err = 0;
31726394Scc210113 		goto done;
31736394Scc210113 	}
31746394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
31756512Ssowmini 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
31766512Ssowmini 		    Adapter->rx_limit_onintr);
31776394Scc210113 		err = 0;
31786394Scc210113 		goto done;
31796394Scc210113 	}
31806394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
31816512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
31826512Ssowmini 		    Adapter->rx_intr_delay);
31836394Scc210113 		err = 0;
31846394Scc210113 		goto done;
31856394Scc210113 	}
31866394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
31876512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
31886512Ssowmini 		    Adapter->rx_intr_abs_delay);
31896394Scc210113 		err = 0;
31906394Scc210113 		goto done;
31916394Scc210113 	}
31926394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
31936512Ssowmini 		value = (is_default ? DEFAULT_INTR_THROTTLING :
31946512Ssowmini 		    Adapter->intr_throttling_rate);
31956394Scc210113 		err = 0;
31966394Scc210113 		goto done;
31976394Scc210113 	}
31986394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
31996512Ssowmini 		value = (is_default ? 1 : Adapter->intr_adaptive);
32006394Scc210113 		err = 0;
32016394Scc210113 		goto done;
32026394Scc210113 	}
32036394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
32046512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_THRESHOLD :
32056512Ssowmini 		    Adapter->tx_recycle_thresh);
32066394Scc210113 		err = 0;
32076394Scc210113 		goto done;
32086394Scc210113 	}
32096394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 0) {
32106512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_NUM :
32116512Ssowmini 		    Adapter->tx_recycle_num);
32126394Scc210113 		err = 0;
32136394Scc210113 		goto done;
32146394Scc210113 	}
32156394Scc210113 done:
32166394Scc210113 	if (err == 0) {
32176512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
32186394Scc210113 	}
32196394Scc210113 	return (err);
32206394Scc210113 }
32216394Scc210113 
32223526Sxy150489 /*
32234919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
32244919Sxy150489  * This routine gets user-configured values out of the configuration
32254919Sxy150489  * file e1000g.conf.
32264919Sxy150489  *
32274919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
32284919Sxy150489  * default.
32294919Sxy150489  * If user does not configure a value, use the default.
32304919Sxy150489  * If user configures below the minimum, use the minumum.
32314919Sxy150489  * If user configures above the maximum, use the maxumum.
32323526Sxy150489  */
32333526Sxy150489 static void
32344919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
32353526Sxy150489 {
32364919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
32374919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
32384919Sxy150489 
32393526Sxy150489 	/*
32403526Sxy150489 	 * get each configurable property from e1000g.conf
32413526Sxy150489 	 */
32423526Sxy150489 
32433526Sxy150489 	/*
32443526Sxy150489 	 * NumTxDescriptors
32453526Sxy150489 	 */
32464919Sxy150489 	Adapter->tx_desc_num =
32474919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
32484919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
32494919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
32503526Sxy150489 
32513526Sxy150489 	/*
32523526Sxy150489 	 * NumRxDescriptors
32533526Sxy150489 	 */
32544919Sxy150489 	Adapter->rx_desc_num =
32554919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
32564919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
32574919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
32583526Sxy150489 
32593526Sxy150489 	/*
32603526Sxy150489 	 * NumRxFreeList
32613526Sxy150489 	 */
32624919Sxy150489 	Adapter->rx_freelist_num =
32634919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
32644919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
32654919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
32663526Sxy150489 
32673526Sxy150489 	/*
32683526Sxy150489 	 * NumTxPacketList
32693526Sxy150489 	 */
32704919Sxy150489 	Adapter->tx_freelist_num =
32714919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
32724919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
32734919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
32743526Sxy150489 
32753526Sxy150489 	/*
32763526Sxy150489 	 * FlowControl
32773526Sxy150489 	 */
32786735Scc210113 	hw->fc.send_xon = B_TRUE;
32796735Scc210113 	hw->fc.type =
32804919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
32814919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
32823526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
32836735Scc210113 	if (hw->fc.type == 4)
32846735Scc210113 		hw->fc.type = e1000_fc_default;
32853526Sxy150489 
32863526Sxy150489 	/*
32874919Sxy150489 	 * Max Num Receive Packets on Interrupt
32883526Sxy150489 	 */
32894919Sxy150489 	Adapter->rx_limit_onintr =
32904919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
32914919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
32924919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
32933526Sxy150489 
32943526Sxy150489 	/*
32953526Sxy150489 	 * PHY master slave setting
32963526Sxy150489 	 */
32974919Sxy150489 	hw->phy.ms_type =
32984919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
32993526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
33003526Sxy150489 	    e1000_ms_hw_default);
33013526Sxy150489 
33023526Sxy150489 	/*
33033526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
33043526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
33053526Sxy150489 	 */
33064919Sxy150489 	tbi_compatibility =
33074919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
33084919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
33094919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
33103526Sxy150489 
33113526Sxy150489 	/*
33123526Sxy150489 	 * MSI Enable
33133526Sxy150489 	 */
33146986Smx205022 	Adapter->msi_enable =
33154919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
33164919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
33173526Sxy150489 
33183526Sxy150489 	/*
33193526Sxy150489 	 * Interrupt Throttling Rate
33203526Sxy150489 	 */
33213526Sxy150489 	Adapter->intr_throttling_rate =
33224919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
33234919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
33244919Sxy150489 	    DEFAULT_INTR_THROTTLING);
33253526Sxy150489 
33263526Sxy150489 	/*
33273526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
33283526Sxy150489 	 * It is enabled by default
33293526Sxy150489 	 */
33303526Sxy150489 	Adapter->intr_adaptive =
33314919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
33323526Sxy150489 	    B_TRUE : B_FALSE;
33335882Syy150190 
33345882Syy150190 	/*
33355882Syy150190 	 * Tx recycle threshold
33365882Syy150190 	 */
33375882Syy150190 	Adapter->tx_recycle_thresh =
33385882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_thresh",
33395882Syy150190 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
33405882Syy150190 	    DEFAULT_TX_RECYCLE_THRESHOLD);
33415882Syy150190 
33425882Syy150190 	/*
33435882Syy150190 	 * Tx recycle descriptor number
33445882Syy150190 	 */
33455882Syy150190 	Adapter->tx_recycle_num =
33465882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_num",
33475882Syy150190 	    MIN_TX_RECYCLE_NUM, MAX_TX_RECYCLE_NUM,
33485882Syy150190 	    DEFAULT_TX_RECYCLE_NUM);
33496011Ssv141092 
33506011Ssv141092 	/*
33516011Ssv141092 	 * Hardware checksum enable/disable parameter
33526011Ssv141092 	 */
33536986Smx205022 	Adapter->tx_hcksum_enable =
33546986Smx205022 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
33556011Ssv141092 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
33566986Smx205022 	/*
33576986Smx205022 	 * Checksum on/off selection via global parameters.
33586986Smx205022 	 *
33596986Smx205022 	 * If the chip is flagged as not capable of (correctly)
33606986Smx205022 	 * handling checksumming, we don't enable it on either
33616986Smx205022 	 * Rx or Tx side.  Otherwise, we take this chip's settings
33626986Smx205022 	 * from the patchable global defaults.
33636986Smx205022 	 *
33646986Smx205022 	 * We advertise our capabilities only if TX offload is
33656986Smx205022 	 * enabled.  On receive, the stack will accept checksummed
33666986Smx205022 	 * packets anyway, even if we haven't said we can deliver
33676986Smx205022 	 * them.
33686986Smx205022 	 */
33696986Smx205022 	switch (hw->mac.type) {
33706986Smx205022 		case e1000_82540:
33716986Smx205022 		case e1000_82544:
33726986Smx205022 		case e1000_82545:
33736986Smx205022 		case e1000_82545_rev_3:
33746986Smx205022 		case e1000_82546:
33756986Smx205022 		case e1000_82546_rev_3:
33766986Smx205022 		case e1000_82571:
33776986Smx205022 		case e1000_82572:
33786986Smx205022 		case e1000_82573:
33796986Smx205022 		case e1000_80003es2lan:
33806986Smx205022 			break;
33816986Smx205022 		/*
33826986Smx205022 		 * For the following Intel PRO/1000 chipsets, we have not
33836986Smx205022 		 * tested the hardware checksum offload capability, so we
33846986Smx205022 		 * disable the capability for them.
33856986Smx205022 		 *	e1000_82542,
33866986Smx205022 		 *	e1000_82543,
33876986Smx205022 		 *	e1000_82541,
33886986Smx205022 		 *	e1000_82541_rev_2,
33896986Smx205022 		 *	e1000_82547,
33906986Smx205022 		 *	e1000_82547_rev_2,
33916986Smx205022 		 */
33926986Smx205022 		default:
33936986Smx205022 			Adapter->tx_hcksum_enable = B_FALSE;
33946986Smx205022 	}
33956986Smx205022 
33966986Smx205022 	/*
33976986Smx205022 	 * Large Send Offloading(LSO) Enable/Disable
33986986Smx205022 	 * If the tx hardware checksum is not enabled, LSO should be
33996986Smx205022 	 * disabled.
34006986Smx205022 	 */
34016986Smx205022 	Adapter->lso_enable =
34026986Smx205022 	    e1000g_get_prop(Adapter, "lso_enable",
34036986Smx205022 	    0, 1, DEFAULT_LSO_ENABLE);
34046986Smx205022 
34056986Smx205022 	switch (hw->mac.type) {
34066986Smx205022 		case e1000_82546:
34076986Smx205022 		case e1000_82546_rev_3:
34086986Smx205022 			if (Adapter->lso_enable)
34096986Smx205022 				Adapter->lso_premature_issue = B_TRUE;
34107426SChenliang.Xu@Sun.COM 			/* FALLTHRU */
34116986Smx205022 		case e1000_82571:
34126986Smx205022 		case e1000_82572:
34136986Smx205022 		case e1000_82573:
34146986Smx205022 			break;
34156986Smx205022 		default:
34166986Smx205022 			Adapter->lso_enable = B_FALSE;
34176986Smx205022 	}
34186986Smx205022 
34196986Smx205022 	if (!Adapter->tx_hcksum_enable) {
34206986Smx205022 		Adapter->lso_premature_issue = B_FALSE;
34216986Smx205022 		Adapter->lso_enable = B_FALSE;
34226986Smx205022 	}
34233526Sxy150489 }
34243526Sxy150489 
34253526Sxy150489 /*
34264919Sxy150489  * e1000g_get_prop - routine to read properties
34274919Sxy150489  *
34284919Sxy150489  * Get a user-configure property value out of the configuration
34294919Sxy150489  * file e1000g.conf.
34304919Sxy150489  *
34314919Sxy150489  * Caller provides name of the property, a default value, a minimum
34324919Sxy150489  * value, and a maximum value.
34334919Sxy150489  *
34344919Sxy150489  * Return configured value of the property, with default, minimum and
34354919Sxy150489  * maximum properly applied.
34363526Sxy150489  */
34373526Sxy150489 static int
34384919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
34393526Sxy150489     char *propname,		/* name of the property */
34403526Sxy150489     int minval,			/* minimum acceptable value */
34413526Sxy150489     int maxval,			/* maximim acceptable value */
34423526Sxy150489     int defval)			/* default value */
34433526Sxy150489 {
34443526Sxy150489 	int propval;		/* value returned for requested property */
34453526Sxy150489 	int *props;		/* point to array of properties returned */
34463526Sxy150489 	uint_t nprops;		/* number of property value returned */
34473526Sxy150489 
34483526Sxy150489 	/*
34493526Sxy150489 	 * get the array of properties from the config file
34503526Sxy150489 	 */
34513526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
34523526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
34533526Sxy150489 		/* got some properties, test if we got enough */
34544919Sxy150489 		if (Adapter->instance < nprops) {
34554919Sxy150489 			propval = props[Adapter->instance];
34563526Sxy150489 		} else {
34573526Sxy150489 			/* not enough properties configured */
34583526Sxy150489 			propval = defval;
34594919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34603526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
34613526Sxy150489 			    " - set to %d\n",
34623526Sxy150489 			    propname, propval);
34633526Sxy150489 		}
34643526Sxy150489 
34653526Sxy150489 		/* free memory allocated for properties */
34663526Sxy150489 		ddi_prop_free(props);
34673526Sxy150489 
34683526Sxy150489 	} else {
34693526Sxy150489 		propval = defval;
34703526Sxy150489 	}
34713526Sxy150489 
34723526Sxy150489 	/*
34733526Sxy150489 	 * enforce limits
34743526Sxy150489 	 */
34753526Sxy150489 	if (propval > maxval) {
34763526Sxy150489 		propval = maxval;
34774919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34783526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
34793526Sxy150489 		    propname, propval);
34803526Sxy150489 	}
34813526Sxy150489 
34823526Sxy150489 	if (propval < minval) {
34833526Sxy150489 		propval = minval;
34844919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34853526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
34863526Sxy150489 		    propname, propval);
34873526Sxy150489 	}
34883526Sxy150489 
34893526Sxy150489 	return (propval);
34903526Sxy150489 }
34913526Sxy150489 
34923526Sxy150489 static boolean_t
34934061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
34943526Sxy150489 {
34954061Sxy150489 	uint16_t speed, duplex, phydata;
34964061Sxy150489 	boolean_t link_changed = B_FALSE;
34973526Sxy150489 	struct e1000_hw *hw;
34983526Sxy150489 	uint32_t reg_tarc;
34993526Sxy150489 
35004919Sxy150489 	hw = &Adapter->shared;
35013526Sxy150489 
35023526Sxy150489 	if (e1000g_link_up(Adapter)) {
35033526Sxy150489 		/*
35043526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
35053526Sxy150489 		 */
35064061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
35077426SChenliang.Xu@Sun.COM 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
35084061Sxy150489 			Adapter->link_speed = speed;
35094061Sxy150489 			Adapter->link_duplex = duplex;
35104061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
35114061Sxy150489 			link_changed = B_TRUE;
35124061Sxy150489 
35134061Sxy150489 			Adapter->tx_link_down_timeout = 0;
35144061Sxy150489 
35154919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
35164919Sxy150489 			    (hw->mac.type == e1000_82572)) {
35176735Scc210113 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
35184061Sxy150489 				if (speed == SPEED_1000)
35194061Sxy150489 					reg_tarc |= (1 << 21);
35204061Sxy150489 				else
35214061Sxy150489 					reg_tarc &= ~(1 << 21);
35226735Scc210113 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
35233526Sxy150489 			}
35243526Sxy150489 		}
35253526Sxy150489 		Adapter->smartspeed = 0;
35263526Sxy150489 	} else {
35274061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
35283526Sxy150489 			Adapter->link_speed = 0;
35293526Sxy150489 			Adapter->link_duplex = 0;
35304061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
35314061Sxy150489 			link_changed = B_TRUE;
35324061Sxy150489 
35333526Sxy150489 			/*
35343526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
35353526Sxy150489 			 * driver loses link disable auto master/slave
35363526Sxy150489 			 * resolution.
35373526Sxy150489 			 */
35384919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
35397426SChenliang.Xu@Sun.COM 				(void) e1000_read_phy_reg(hw,
35403526Sxy150489 				    PHY_1000T_CTRL, &phydata);
35413526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
35427426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
35433526Sxy150489 				    PHY_1000T_CTRL, phydata);
35443526Sxy150489 			}
35453526Sxy150489 		} else {
35463526Sxy150489 			e1000g_smartspeed(Adapter);
35473526Sxy150489 		}
35484061Sxy150489 
35495273Sgl147354 		if (Adapter->chip_state == E1000G_START) {
35504061Sxy150489 			if (Adapter->tx_link_down_timeout <
35514061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35524061Sxy150489 				Adapter->tx_link_down_timeout++;
35534061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
35544061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35554919Sxy150489 				e1000g_tx_clean(Adapter);
35564061Sxy150489 				Adapter->tx_link_down_timeout++;
35574061Sxy150489 			}
35584061Sxy150489 		}
35593526Sxy150489 	}
35603526Sxy150489 
35615273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
35625273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
35635273Sgl147354 
35644061Sxy150489 	return (link_changed);
35654061Sxy150489 }
35664061Sxy150489 
35676394Scc210113 /*
35686394Scc210113  * e1000g_reset_link - Using the link properties to setup the link
35696394Scc210113  */
35706394Scc210113 int
35716394Scc210113 e1000g_reset_link(struct e1000g *Adapter)
35726394Scc210113 {
35736394Scc210113 	struct e1000_mac_info *mac;
35746394Scc210113 	struct e1000_phy_info *phy;
35756394Scc210113 	boolean_t invalid;
35766394Scc210113 
35776394Scc210113 	mac = &Adapter->shared.mac;
35786394Scc210113 	phy = &Adapter->shared.phy;
35796394Scc210113 	invalid = B_FALSE;
35806394Scc210113 
35816394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
35826394Scc210113 		mac->autoneg = B_TRUE;
35836394Scc210113 		phy->autoneg_advertised = 0;
35846394Scc210113 
35856394Scc210113 		/*
35866394Scc210113 		 * 1000hdx is not supported for autonegotiation
35876394Scc210113 		 */
35886394Scc210113 		if (Adapter->param_adv_1000fdx == 1)
35896394Scc210113 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
35906394Scc210113 
35916394Scc210113 		if (Adapter->param_adv_100fdx == 1)
35926394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
35936394Scc210113 
35946394Scc210113 		if (Adapter->param_adv_100hdx == 1)
35956394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
35966394Scc210113 
35976394Scc210113 		if (Adapter->param_adv_10fdx == 1)
35986394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
35996394Scc210113 
36006394Scc210113 		if (Adapter->param_adv_10hdx == 1)
36016394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
36026394Scc210113 
36036394Scc210113 		if (phy->autoneg_advertised == 0)
36046394Scc210113 			invalid = B_TRUE;
36056394Scc210113 	} else {
36066394Scc210113 		mac->autoneg = B_FALSE;
36076394Scc210113 
36086394Scc210113 		/*
36096394Scc210113 		 * 1000fdx and 1000hdx are not supported for forced link
36106394Scc210113 		 */
36116394Scc210113 		if (Adapter->param_adv_100fdx == 1)
36126394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
36136394Scc210113 		else if (Adapter->param_adv_100hdx == 1)
36146394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
36156394Scc210113 		else if (Adapter->param_adv_10fdx == 1)
36166394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
36176394Scc210113 		else if (Adapter->param_adv_10hdx == 1)
36186394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
36196394Scc210113 		else
36206394Scc210113 			invalid = B_TRUE;
36216394Scc210113 
36226394Scc210113 	}
36236394Scc210113 
36246394Scc210113 	if (invalid) {
36256394Scc210113 		e1000g_log(Adapter, CE_WARN,
36266394Scc210113 		    "Invalid link sets. Setup link to"
36276394Scc210113 		    "support autonegotiation with all link capabilities.");
36286394Scc210113 		mac->autoneg = B_TRUE;
36296394Scc210113 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
36306394Scc210113 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
36316394Scc210113 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
36326394Scc210113 	}
36336394Scc210113 
36346394Scc210113 	return (e1000_setup_link(&Adapter->shared));
36356394Scc210113 }
36366394Scc210113 
36374061Sxy150489 static void
36384919Sxy150489 e1000g_local_timer(void *ws)
36394061Sxy150489 {
36404061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
36414061Sxy150489 	struct e1000_hw *hw;
36424061Sxy150489 	e1000g_ether_addr_t ether_addr;
36434061Sxy150489 	boolean_t link_changed;
36444061Sxy150489 
36454919Sxy150489 	hw = &Adapter->shared;
36464919Sxy150489 
36475273Sgl147354 	if (Adapter->chip_state == E1000G_ERROR) {
36485273Sgl147354 		Adapter->reset_count++;
36495273Sgl147354 		if (e1000g_global_reset(Adapter))
36505273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36515273Sgl147354 			    DDI_SERVICE_RESTORED);
36525273Sgl147354 		else
36535273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36545273Sgl147354 			    DDI_SERVICE_LOST);
36555273Sgl147354 		return;
36565273Sgl147354 	}
36575273Sgl147354 
36584061Sxy150489 	if (e1000g_stall_check(Adapter)) {
36594919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
36604061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
36615273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
36624061Sxy150489 		Adapter->reset_count++;
36635273Sgl147354 		if (e1000g_reset(Adapter))
36645273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36655273Sgl147354 			    DDI_SERVICE_RESTORED);
36665273Sgl147354 		else
36675273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36685273Sgl147354 			    DDI_SERVICE_LOST);
36695273Sgl147354 		return;
36704061Sxy150489 	}
36714061Sxy150489 
36724061Sxy150489 	link_changed = B_FALSE;
36735082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
36744061Sxy150489 	if (Adapter->link_complete)
36754061Sxy150489 		link_changed = e1000g_link_check(Adapter);
36765082Syy150190 	rw_exit(&Adapter->chip_lock);
36774061Sxy150489 
36787133Scc210113 	if (link_changed)
36794061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
36807133Scc210113 
36817133Scc210113 	/*
36827133Scc210113 	 * Workaround for esb2. Data stuck in fifo on a link
36837133Scc210113 	 * down event. Reset the adapter to recover it.
36847133Scc210113 	 */
36857133Scc210113 	if (Adapter->esb2_workaround) {
36867133Scc210113 		Adapter->esb2_workaround = B_FALSE;
36877133Scc210113 		(void) e1000g_reset(Adapter);
36884139Sxy150489 	}
36894061Sxy150489 
36903526Sxy150489 	/*
36913526Sxy150489 	 * With 82571 controllers, any locally administered address will
36923526Sxy150489 	 * be overwritten when there is a reset on the other port.
36933526Sxy150489 	 * Detect this circumstance and correct it.
36943526Sxy150489 	 */
36954919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
36964919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
36974919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
36984919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
36993526Sxy150489 
37003526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
37013526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
37023526Sxy150489 
37034919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
37044919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
37054919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
37064919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
37074919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
37084919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
37094919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
37103526Sxy150489 		}
37113526Sxy150489 	}
37123526Sxy150489 
37133526Sxy150489 	/*
37144919Sxy150489 	 * Long TTL workaround for 82541/82547
37153526Sxy150489 	 */
37167426SChenliang.Xu@Sun.COM 	(void) e1000_igp_ttl_workaround_82547(hw);
37173526Sxy150489 
37183526Sxy150489 	/*
37193526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
37203526Sxy150489 	 * change the value in steps...
37213526Sxy150489 	 * These properties should only be set for 10/100
37223526Sxy150489 	 */
37236735Scc210113 	if ((hw->phy.media_type == e1000_media_type_copper) &&
37244061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
37254061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
37263526Sxy150489 		e1000_update_adaptive(hw);
37273526Sxy150489 	}
37283526Sxy150489 	/*
37293526Sxy150489 	 * Set Timer Interrupts
37303526Sxy150489 	 */
37314919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
37324919Sxy150489 
37335273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
37345273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
37355273Sgl147354 
37364919Sxy150489 	restart_watchdog_timer(Adapter);
37373526Sxy150489 }
37383526Sxy150489 
37394061Sxy150489 /*
37404061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
37414061Sxy150489  * is expired, which indicates the completion of the link setup. The link
37424061Sxy150489  * state will not be updated until the link setup is completed. And the
37434061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
37444061Sxy150489  * in this function. It will be updated in the local timer routine or the
37454061Sxy150489  * interrupt service routine after the interface is started (plumbed).
37464061Sxy150489  */
37473526Sxy150489 static void
37484061Sxy150489 e1000g_link_timer(void *arg)
37493526Sxy150489 {
37504061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
37513526Sxy150489 
37524919Sxy150489 	mutex_enter(&Adapter->link_lock);
37534061Sxy150489 	Adapter->link_complete = B_TRUE;
37544061Sxy150489 	Adapter->link_tid = 0;
37554919Sxy150489 	mutex_exit(&Adapter->link_lock);
37563526Sxy150489 }
37573526Sxy150489 
37583526Sxy150489 /*
37594919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
37604919Sxy150489  *
37614919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
37624919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
37633526Sxy150489  */
37643526Sxy150489 static void
37653526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
37663526Sxy150489 {
37673526Sxy150489 	int forced;
37684919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
37694919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
37703526Sxy150489 
37713526Sxy150489 	/*
37723526Sxy150489 	 * get value out of config file
37733526Sxy150489 	 */
37744919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
37753526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
37763526Sxy150489 
37773526Sxy150489 	switch (forced) {
37783526Sxy150489 	case GDIAG_10_HALF:
37793526Sxy150489 		/*
37803526Sxy150489 		 * Disable Auto Negotiation
37813526Sxy150489 		 */
37824919Sxy150489 		mac->autoneg = B_FALSE;
37834919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
37843526Sxy150489 		break;
37853526Sxy150489 	case GDIAG_10_FULL:
37863526Sxy150489 		/*
37873526Sxy150489 		 * Disable Auto Negotiation
37883526Sxy150489 		 */
37894919Sxy150489 		mac->autoneg = B_FALSE;
37904919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
37913526Sxy150489 		break;
37923526Sxy150489 	case GDIAG_100_HALF:
37933526Sxy150489 		/*
37943526Sxy150489 		 * Disable Auto Negotiation
37953526Sxy150489 		 */
37964919Sxy150489 		mac->autoneg = B_FALSE;
37974919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
37983526Sxy150489 		break;
37993526Sxy150489 	case GDIAG_100_FULL:
38003526Sxy150489 		/*
38013526Sxy150489 		 * Disable Auto Negotiation
38023526Sxy150489 		 */
38034919Sxy150489 		mac->autoneg = B_FALSE;
38044919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
38053526Sxy150489 		break;
38063526Sxy150489 	case GDIAG_1000_FULL:
38073526Sxy150489 		/*
38083526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
38093526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
38103526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
38113526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
38123526Sxy150489 		 * we are allowed to link without any negotiation.
38133526Sxy150489 		 */
38144919Sxy150489 		mac->autoneg = B_TRUE;
38154919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
38163526Sxy150489 		break;
38173526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
38184919Sxy150489 		mac->autoneg = B_TRUE;
38194919Sxy150489 		phy->autoneg_advertised =
38204919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
38214349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
38224349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
38233526Sxy150489 		break;
38243526Sxy150489 	}	/* switch */
38253526Sxy150489 }
38263526Sxy150489 
38273526Sxy150489 /*
38284919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
38294919Sxy150489  *
38304919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
38313526Sxy150489  */
38323526Sxy150489 static void
38333526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
38343526Sxy150489 {
38353526Sxy150489 	int max_frame;
38364919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
38374919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
38383526Sxy150489 
38393526Sxy150489 	/*
38403526Sxy150489 	 * get value out of config file
38413526Sxy150489 	 */
38424919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
38433526Sxy150489 
38443526Sxy150489 	switch (max_frame) {
38453526Sxy150489 	case 0:
38466394Scc210113 		Adapter->default_mtu = ETHERMTU;
38473526Sxy150489 		break;
38486394Scc210113 	/*
38496394Scc210113 	 * To avoid excessive memory allocation for rx buffers,
38506394Scc210113 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
38516394Scc210113 	 */
38523526Sxy150489 	case 1:
38536394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
38546394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38556394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38563526Sxy150489 		break;
38573526Sxy150489 	case 2:
38586394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
38596394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38606394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38613526Sxy150489 		break;
38623526Sxy150489 	case 3:
38636394Scc210113 		if (mac->type >= e1000_82571)
38646394Scc210113 			Adapter->default_mtu = MAXIMUM_MTU;
38653526Sxy150489 		else
38666394Scc210113 			Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
38676394Scc210113 			    sizeof (struct ether_vlan_header) - ETHERFCSL -
38686394Scc210113 			    E1000G_IPALIGNPRESERVEROOM;
38693526Sxy150489 		break;
38703526Sxy150489 	default:
38716394Scc210113 		Adapter->default_mtu = ETHERMTU;
38723526Sxy150489 		break;
38733526Sxy150489 	}	/* switch */
38743526Sxy150489 
38756735Scc210113 	Adapter->max_frame_size = Adapter->default_mtu +
38766394Scc210113 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
38776394Scc210113 
38783526Sxy150489 	/* ich8 does not do jumbo frames */
38794919Sxy150489 	if (mac->type == e1000_ich8lan) {
38807133Scc210113 		Adapter->max_frame_size = ETHERMTU +
38817133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
38824919Sxy150489 	}
38834919Sxy150489 
38844919Sxy150489 	/* ich9 does not do jumbo frames on one phy type */
38854919Sxy150489 	if ((mac->type == e1000_ich9lan) &&
38864919Sxy150489 	    (phy->type == e1000_phy_ife)) {
38877133Scc210113 		Adapter->max_frame_size = ETHERMTU +
38887133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
38893526Sxy150489 	}
38903526Sxy150489 }
38913526Sxy150489 
38923526Sxy150489 static void
38934919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
38943526Sxy150489 {
38954919Sxy150489 	Adapter->watchdog_tid =
38964919Sxy150489 	    timeout(e1000g_local_timer,
38973526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
38983526Sxy150489 }
38994919Sxy150489 #pragma inline(arm_watchdog_timer)
39004919Sxy150489 
39014919Sxy150489 static void
39024919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
39034919Sxy150489 {
39044919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39054919Sxy150489 
39064919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
39074919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
39084919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
39094919Sxy150489 		arm_watchdog_timer(Adapter);
39104919Sxy150489 	}
39114919Sxy150489 
39124919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39134919Sxy150489 }
39143526Sxy150489 
39153526Sxy150489 static void
39164919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
39173526Sxy150489 {
39183526Sxy150489 	timeout_id_t tid;
39193526Sxy150489 
39204919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39214919Sxy150489 
39224919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
39234919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
39244919Sxy150489 	tid = Adapter->watchdog_tid;
39254919Sxy150489 	Adapter->watchdog_tid = 0;
39264919Sxy150489 
39274919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39283526Sxy150489 
39293526Sxy150489 	if (tid != 0)
39303526Sxy150489 		(void) untimeout(tid);
39313526Sxy150489 }
39323526Sxy150489 
39333526Sxy150489 static void
39344919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
39353526Sxy150489 {
39364919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39374919Sxy150489 
39384919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
39394919Sxy150489 		if (!Adapter->watchdog_timer_started) {
39404919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
39414919Sxy150489 			arm_watchdog_timer(Adapter);
39423526Sxy150489 		}
39433526Sxy150489 	}
39443526Sxy150489 
39454919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39464919Sxy150489 }
39474919Sxy150489 
39484919Sxy150489 static void
39494919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
39504919Sxy150489 {
39514919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39524919Sxy150489 
39534919Sxy150489 	if (Adapter->watchdog_timer_started)
39544919Sxy150489 		arm_watchdog_timer(Adapter);
39554919Sxy150489 
39564919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39573526Sxy150489 }
39583526Sxy150489 
39593526Sxy150489 static void
39604919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
39613526Sxy150489 {
39624919Sxy150489 	timeout_id_t tid;
39634919Sxy150489 
39644919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39654919Sxy150489 
39664919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
39674919Sxy150489 	tid = Adapter->watchdog_tid;
39684919Sxy150489 	Adapter->watchdog_tid = 0;
39694919Sxy150489 
39704919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39714919Sxy150489 
39724919Sxy150489 	if (tid != 0)
39734919Sxy150489 		(void) untimeout(tid);
39743526Sxy150489 }
39753526Sxy150489 
39763526Sxy150489 static void
39774919Sxy150489 stop_link_timer(struct e1000g *Adapter)
39783526Sxy150489 {
39793526Sxy150489 	timeout_id_t tid;
39803526Sxy150489 
39814919Sxy150489 	/* Disable the link timer */
39824919Sxy150489 	mutex_enter(&Adapter->link_lock);
39834919Sxy150489 
39844919Sxy150489 	tid = Adapter->link_tid;
39854919Sxy150489 	Adapter->link_tid = 0;
39864919Sxy150489 
39874919Sxy150489 	mutex_exit(&Adapter->link_lock);
39884919Sxy150489 
39894919Sxy150489 	if (tid != 0)
39904919Sxy150489 		(void) untimeout(tid);
39914919Sxy150489 }
39924919Sxy150489 
39934919Sxy150489 static void
39944919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
39954919Sxy150489 {
39964919Sxy150489 	timeout_id_t tid;
39974919Sxy150489 
39984919Sxy150489 	/* Disable the tx timer for 82547 chipset */
39994919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
40004919Sxy150489 
40014919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
40024919Sxy150489 	tid = tx_ring->timer_id_82547;
40034919Sxy150489 	tx_ring->timer_id_82547 = 0;
40044919Sxy150489 
40054919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
40063526Sxy150489 
40073526Sxy150489 	if (tid != 0)
40083526Sxy150489 		(void) untimeout(tid);
40093526Sxy150489 }
40103526Sxy150489 
40113526Sxy150489 void
40124919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
40133526Sxy150489 {
40144919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
40154919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
40163526Sxy150489 }
40173526Sxy150489 
40183526Sxy150489 void
40194919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
40203526Sxy150489 {
40214919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
40225882Syy150190 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
40235882Syy150190 
40245882Syy150190 	if (Adapter->tx_intr_enable)
40255882Syy150190 		e1000g_mask_tx_interrupt(Adapter);
40263526Sxy150489 }
40273526Sxy150489 
40283526Sxy150489 void
40294919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
40303526Sxy150489 {
40314919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
40323526Sxy150489 }
40333526Sxy150489 
40343526Sxy150489 void
40354919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
40363526Sxy150489 {
40375882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
40383526Sxy150489 }
40393526Sxy150489 
40403526Sxy150489 void
40414919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
40423526Sxy150489 {
40435882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
40443526Sxy150489 }
40453526Sxy150489 
40463526Sxy150489 static void
40474919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
40483526Sxy150489 {
40494919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
40503526Sxy150489 	uint16_t phy_status;
40513526Sxy150489 	uint16_t phy_ctrl;
40523526Sxy150489 
40533526Sxy150489 	/*
40543526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
40553526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
40563526Sxy150489 	 */
40574919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
40584919Sxy150489 	    !hw->mac.autoneg ||
40594919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
40603526Sxy150489 		return;
40613526Sxy150489 
40623526Sxy150489 	/*
40633526Sxy150489 	 * True if this is the first call of this function or after every
40643526Sxy150489 	 * 30 seconds of not having link
40653526Sxy150489 	 */
40664919Sxy150489 	if (Adapter->smartspeed == 0) {
40673526Sxy150489 		/*
40683526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
40693526Sxy150489 		 * assume back-to-back
40703526Sxy150489 		 */
40717426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
40723526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
40733526Sxy150489 			return;
40743526Sxy150489 
40757426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
40763526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
40773526Sxy150489 			return;
40783526Sxy150489 		/*
40793526Sxy150489 		 * We're assuming back-2-back because our status register
40803526Sxy150489 		 * insists! there's a fault in the master/slave
40813526Sxy150489 		 * relationship that was "negotiated"
40823526Sxy150489 		 */
40837426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
40843526Sxy150489 		/*
40853526Sxy150489 		 * Is the phy configured for manual configuration of
40863526Sxy150489 		 * master/slave?
40873526Sxy150489 		 */
40883526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
40893526Sxy150489 			/*
40903526Sxy150489 			 * Yes.  Then disable manual configuration (enable
40913526Sxy150489 			 * auto configuration) of master/slave
40923526Sxy150489 			 */
40933526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
40947426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw,
40953526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
40963526Sxy150489 			/*
40973526Sxy150489 			 * Effectively starting the clock
40983526Sxy150489 			 */
40994919Sxy150489 			Adapter->smartspeed++;
41003526Sxy150489 			/*
41013526Sxy150489 			 * Restart autonegotiation
41023526Sxy150489 			 */
41034919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
41044919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
41053526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
41063526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
41077426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
41084919Sxy150489 				    PHY_CONTROL, phy_ctrl);
41093526Sxy150489 			}
41103526Sxy150489 		}
41113526Sxy150489 		return;
41123526Sxy150489 		/*
41133526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
41143526Sxy150489 		 * you should reset the smartspeed counter once you obtain
41153526Sxy150489 		 * link
41163526Sxy150489 		 */
41174919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
41183526Sxy150489 		/*
41193526Sxy150489 		 * Yes.  Remember, we did at the start determine that
41203526Sxy150489 		 * there's a master/slave configuration fault, so we're
41213526Sxy150489 		 * still assuming there's someone on the other end, but we
41223526Sxy150489 		 * just haven't yet been able to talk to it. We then
41233526Sxy150489 		 * re-enable auto configuration of master/slave to see if
41243526Sxy150489 		 * we're running 2/3 pair cables.
41253526Sxy150489 		 */
41263526Sxy150489 		/*
41273526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
41283526Sxy150489 		 */
41297426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
41303526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
41317426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
41323526Sxy150489 		/*
41333526Sxy150489 		 * Restart autoneg with phy enabled for manual
41343526Sxy150489 		 * configuration of master/slave
41353526Sxy150489 		 */
41364919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
41374919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
41383526Sxy150489 			phy_ctrl |=
41393526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
41407426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
41413526Sxy150489 		}
41423526Sxy150489 		/*
41433526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
41443526Sxy150489 		 * link as a result.
41453526Sxy150489 		 */
41463526Sxy150489 	}
41473526Sxy150489 	/*
41483526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
41493526Sxy150489 	 * seconds)
41503526Sxy150489 	 */
41514919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
41524919Sxy150489 		Adapter->smartspeed = 0;
41533526Sxy150489 }
41543526Sxy150489 
41553526Sxy150489 static boolean_t
41563526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
41573526Sxy150489 {
41583526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
41593526Sxy150489 	const uint8_t addr_test2[6] =
41603526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
41613526Sxy150489 
41623526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
41633526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
41643526Sxy150489 		return (B_FALSE);
41653526Sxy150489 
41663526Sxy150489 	return (B_TRUE);
41673526Sxy150489 }
41683526Sxy150489 
41693526Sxy150489 /*
41704919Sxy150489  * e1000g_stall_check - check for tx stall
41714919Sxy150489  *
41724919Sxy150489  * This function checks if the adapter is stalled (in transmit).
41734919Sxy150489  *
41744919Sxy150489  * It is called each time the watchdog timeout is invoked.
41754919Sxy150489  * If the transmit descriptor reclaim continuously fails,
41764919Sxy150489  * the watchdog value will increment by 1. If the watchdog
41774919Sxy150489  * value exceeds the threshold, the adapter is assumed to
41784919Sxy150489  * have stalled and need to be reset.
41793526Sxy150489  */
41803526Sxy150489 static boolean_t
41813526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
41823526Sxy150489 {
41834919Sxy150489 	e1000g_tx_ring_t *tx_ring;
41844919Sxy150489 
41854919Sxy150489 	tx_ring = Adapter->tx_ring;
41864919Sxy150489 
41874061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
41883526Sxy150489 		return (B_FALSE);
41893526Sxy150489 
41904919Sxy150489 	if (tx_ring->recycle_fail > 0)
41914919Sxy150489 		tx_ring->stall_watchdog++;
41923526Sxy150489 	else
41934919Sxy150489 		tx_ring->stall_watchdog = 0;
41944919Sxy150489 
41954919Sxy150489 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
41963526Sxy150489 		return (B_FALSE);
41973526Sxy150489 
41984919Sxy150489 	tx_ring->stall_watchdog = 0;
41994919Sxy150489 	tx_ring->recycle_fail = 0;
42004919Sxy150489 
42013526Sxy150489 	return (B_TRUE);
42023526Sxy150489 }
42033526Sxy150489 
42044919Sxy150489 #ifdef E1000G_DEBUG
42053526Sxy150489 static enum ioc_reply
42063526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
42073526Sxy150489 {
42083526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
42093526Sxy150489 	e1000g_peekpoke_t *ppd;
42103526Sxy150489 	uint64_t mem_va;
42113526Sxy150489 	uint64_t maxoff;
42123526Sxy150489 	boolean_t peek;
42133526Sxy150489 
42143526Sxy150489 	switch (iocp->ioc_cmd) {
42153526Sxy150489 
42163526Sxy150489 	case E1000G_IOC_REG_PEEK:
42173526Sxy150489 		peek = B_TRUE;
42183526Sxy150489 		break;
42193526Sxy150489 
42203526Sxy150489 	case E1000G_IOC_REG_POKE:
42213526Sxy150489 		peek = B_FALSE;
42223526Sxy150489 		break;
42233526Sxy150489 
42243526Sxy150489 	deault:
42254919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42264349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
42274349Sxy150489 		    iocp->ioc_cmd);
42283526Sxy150489 		return (IOC_INVAL);
42293526Sxy150489 	}
42303526Sxy150489 
42313526Sxy150489 	/*
42323526Sxy150489 	 * Validate format of ioctl
42333526Sxy150489 	 */
42343526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
42353526Sxy150489 		return (IOC_INVAL);
42363526Sxy150489 	if (mp->b_cont == NULL)
42373526Sxy150489 		return (IOC_INVAL);
42383526Sxy150489 
42397426SChenliang.Xu@Sun.COM 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
42403526Sxy150489 
42413526Sxy150489 	/*
42423526Sxy150489 	 * Validate request parameters
42433526Sxy150489 	 */
42443526Sxy150489 	switch (ppd->pp_acc_space) {
42453526Sxy150489 
42463526Sxy150489 	default:
42474919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42484349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
42494349Sxy150489 		    ppd->pp_acc_space);
42503526Sxy150489 		return (IOC_INVAL);
42513526Sxy150489 
42523526Sxy150489 	case E1000G_PP_SPACE_REG:
42533526Sxy150489 		/*
42543526Sxy150489 		 * Memory-mapped I/O space
42553526Sxy150489 		 */
42563526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
42573526Sxy150489 		if (ppd->pp_acc_size != 4)
42583526Sxy150489 			return (IOC_INVAL);
42593526Sxy150489 
42603526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
42613526Sxy150489 			return (IOC_INVAL);
42623526Sxy150489 
42633526Sxy150489 		mem_va = 0;
42643526Sxy150489 		maxoff = 0x10000;
42653526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
42663526Sxy150489 		break;
42673526Sxy150489 
42683526Sxy150489 	case E1000G_PP_SPACE_E1000G:
42693526Sxy150489 		/*
42703526Sxy150489 		 * E1000g data structure!
42713526Sxy150489 		 */
42723526Sxy150489 		mem_va = (uintptr_t)e1000gp;
42733526Sxy150489 		maxoff = sizeof (struct e1000g);
42743526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
42753526Sxy150489 		break;
42763526Sxy150489 
42773526Sxy150489 	}
42783526Sxy150489 
42793526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
42803526Sxy150489 		return (IOC_INVAL);
42813526Sxy150489 
42823526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
42833526Sxy150489 		return (IOC_INVAL);
42843526Sxy150489 
42853526Sxy150489 	/*
42863526Sxy150489 	 * All OK - go!
42873526Sxy150489 	 */
42883526Sxy150489 	ppd->pp_acc_offset += mem_va;
42893526Sxy150489 	(*ppfn)(e1000gp, ppd);
42903526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
42913526Sxy150489 }
42923526Sxy150489 
42933526Sxy150489 static void
42943526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
42953526Sxy150489 {
42963526Sxy150489 	ddi_acc_handle_t handle;
42973526Sxy150489 	uint32_t *regaddr;
42983526Sxy150489 
42994919Sxy150489 	handle = e1000gp->osdep.reg_handle;
43007426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
43017426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
43023526Sxy150489 
43033526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
43043526Sxy150489 }
43053526Sxy150489 
43063526Sxy150489 static void
43073526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43083526Sxy150489 {
43093526Sxy150489 	ddi_acc_handle_t handle;
43103526Sxy150489 	uint32_t *regaddr;
43113526Sxy150489 	uint32_t value;
43123526Sxy150489 
43134919Sxy150489 	handle = e1000gp->osdep.reg_handle;
43147426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
43157426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
43163526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
43173526Sxy150489 
43183526Sxy150489 	ddi_put32(handle, regaddr, value);
43193526Sxy150489 }
43203526Sxy150489 
43213526Sxy150489 static void
43223526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43233526Sxy150489 {
43243526Sxy150489 	uint64_t value;
43253526Sxy150489 	void *vaddr;
43263526Sxy150489 
43273526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
43283526Sxy150489 
43293526Sxy150489 	switch (ppd->pp_acc_size) {
43303526Sxy150489 	case 1:
43313526Sxy150489 		value = *(uint8_t *)vaddr;
43323526Sxy150489 		break;
43333526Sxy150489 
43343526Sxy150489 	case 2:
43353526Sxy150489 		value = *(uint16_t *)vaddr;
43363526Sxy150489 		break;
43373526Sxy150489 
43383526Sxy150489 	case 4:
43393526Sxy150489 		value = *(uint32_t *)vaddr;
43403526Sxy150489 		break;
43413526Sxy150489 
43423526Sxy150489 	case 8:
43433526Sxy150489 		value = *(uint64_t *)vaddr;
43443526Sxy150489 		break;
43453526Sxy150489 	}
43463526Sxy150489 
43474919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
43484349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
43494349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
43503526Sxy150489 
43513526Sxy150489 	ppd->pp_acc_data = value;
43523526Sxy150489 }
43533526Sxy150489 
43543526Sxy150489 static void
43553526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43563526Sxy150489 {
43573526Sxy150489 	uint64_t value;
43583526Sxy150489 	void *vaddr;
43593526Sxy150489 
43603526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
43613526Sxy150489 	value = ppd->pp_acc_data;
43623526Sxy150489 
43634919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
43644349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
43654349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
43663526Sxy150489 
43673526Sxy150489 	switch (ppd->pp_acc_size) {
43683526Sxy150489 	case 1:
43693526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
43703526Sxy150489 		break;
43713526Sxy150489 
43723526Sxy150489 	case 2:
43733526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
43743526Sxy150489 		break;
43753526Sxy150489 
43763526Sxy150489 	case 4:
43773526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
43783526Sxy150489 		break;
43793526Sxy150489 
43803526Sxy150489 	case 8:
43813526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
43823526Sxy150489 		break;
43833526Sxy150489 	}
43843526Sxy150489 }
43854919Sxy150489 #endif
43863526Sxy150489 
43873526Sxy150489 /*
43883526Sxy150489  * Loopback Support
43893526Sxy150489  */
43903526Sxy150489 static lb_property_t lb_normal =
43913526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
43923526Sxy150489 static lb_property_t lb_external1000 =
43933526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
43943526Sxy150489 static lb_property_t lb_external100 =
43953526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
43963526Sxy150489 static lb_property_t lb_external10 =
43973526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
43983526Sxy150489 static lb_property_t lb_phy =
43993526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
44003526Sxy150489 
44013526Sxy150489 static enum ioc_reply
44023526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
44033526Sxy150489 {
44043526Sxy150489 	lb_info_sz_t *lbsp;
44053526Sxy150489 	lb_property_t *lbpp;
44063526Sxy150489 	struct e1000_hw *hw;
44073526Sxy150489 	uint32_t *lbmp;
44083526Sxy150489 	uint32_t size;
44093526Sxy150489 	uint32_t value;
44103526Sxy150489 
44114919Sxy150489 	hw = &Adapter->shared;
44123526Sxy150489 
44133526Sxy150489 	if (mp->b_cont == NULL)
44143526Sxy150489 		return (IOC_INVAL);
44153526Sxy150489 
44167133Scc210113 	if (!e1000g_check_loopback_support(hw)) {
44177133Scc210113 		e1000g_log(NULL, CE_WARN,
44187133Scc210113 		    "Loopback is not supported on e1000g%d", Adapter->instance);
44197133Scc210113 		return (IOC_INVAL);
44207133Scc210113 	}
44217133Scc210113 
44223526Sxy150489 	switch (iocp->ioc_cmd) {
44233526Sxy150489 	default:
44243526Sxy150489 		return (IOC_INVAL);
44253526Sxy150489 
44263526Sxy150489 	case LB_GET_INFO_SIZE:
44273526Sxy150489 		size = sizeof (lb_info_sz_t);
44283526Sxy150489 		if (iocp->ioc_count != size)
44293526Sxy150489 			return (IOC_INVAL);
44303526Sxy150489 
44315082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
44325082Syy150190 		e1000g_get_phy_state(Adapter);
44335082Syy150190 
44345082Syy150190 		/*
44355082Syy150190 		 * Workaround for hardware faults. In order to get a stable
44365082Syy150190 		 * state of phy, we will wait for a specific interval and
44375082Syy150190 		 * try again. The time delay is an experiential value based
44385082Syy150190 		 * on our testing.
44395082Syy150190 		 */
44405082Syy150190 		msec_delay(100);
44415082Syy150190 		e1000g_get_phy_state(Adapter);
44425082Syy150190 		rw_exit(&Adapter->chip_lock);
44433526Sxy150489 
44443526Sxy150489 		value = sizeof (lb_normal);
44455082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44465082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44476735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44486735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44493526Sxy150489 			value += sizeof (lb_phy);
44504919Sxy150489 			switch (hw->mac.type) {
44513526Sxy150489 			case e1000_82571:
44523526Sxy150489 			case e1000_82572:
44537133Scc210113 			case e1000_80003es2lan:
44543526Sxy150489 				value += sizeof (lb_external1000);
44553526Sxy150489 				break;
44563526Sxy150489 			}
44573526Sxy150489 		}
44585082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
44595082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
44603526Sxy150489 			value += sizeof (lb_external100);
44615082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
44623526Sxy150489 			value += sizeof (lb_external10);
44633526Sxy150489 
44647426SChenliang.Xu@Sun.COM 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
44653526Sxy150489 		*lbsp = value;
44663526Sxy150489 		break;
44673526Sxy150489 
44683526Sxy150489 	case LB_GET_INFO:
44693526Sxy150489 		value = sizeof (lb_normal);
44705082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44715082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44726735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44736735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44743526Sxy150489 			value += sizeof (lb_phy);
44754919Sxy150489 			switch (hw->mac.type) {
44763526Sxy150489 			case e1000_82571:
44773526Sxy150489 			case e1000_82572:
44787133Scc210113 			case e1000_80003es2lan:
44793526Sxy150489 				value += sizeof (lb_external1000);
44803526Sxy150489 				break;
44813526Sxy150489 			}
44823526Sxy150489 		}
44835082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
44845082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
44853526Sxy150489 			value += sizeof (lb_external100);
44865082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
44873526Sxy150489 			value += sizeof (lb_external10);
44883526Sxy150489 
44893526Sxy150489 		size = value;
44903526Sxy150489 		if (iocp->ioc_count != size)
44913526Sxy150489 			return (IOC_INVAL);
44923526Sxy150489 
44933526Sxy150489 		value = 0;
44947426SChenliang.Xu@Sun.COM 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
44953526Sxy150489 		lbpp[value++] = lb_normal;
44965082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44975082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44986735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44996735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
45003526Sxy150489 			lbpp[value++] = lb_phy;
45014919Sxy150489 			switch (hw->mac.type) {
45023526Sxy150489 			case e1000_82571:
45033526Sxy150489 			case e1000_82572:
45047133Scc210113 			case e1000_80003es2lan:
45053526Sxy150489 				lbpp[value++] = lb_external1000;
45063526Sxy150489 				break;
45073526Sxy150489 			}
45083526Sxy150489 		}
45095082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
45105082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
45113526Sxy150489 			lbpp[value++] = lb_external100;
45125082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
45133526Sxy150489 			lbpp[value++] = lb_external10;
45143526Sxy150489 		break;
45153526Sxy150489 
45163526Sxy150489 	case LB_GET_MODE:
45173526Sxy150489 		size = sizeof (uint32_t);
45183526Sxy150489 		if (iocp->ioc_count != size)
45193526Sxy150489 			return (IOC_INVAL);
45203526Sxy150489 
45217426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45223526Sxy150489 		*lbmp = Adapter->loopback_mode;
45233526Sxy150489 		break;
45243526Sxy150489 
45253526Sxy150489 	case LB_SET_MODE:
45263526Sxy150489 		size = 0;
45273526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
45283526Sxy150489 			return (IOC_INVAL);
45293526Sxy150489 
45307426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45313526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
45323526Sxy150489 			return (IOC_INVAL);
45333526Sxy150489 		break;
45343526Sxy150489 	}
45353526Sxy150489 
45363526Sxy150489 	iocp->ioc_count = size;
45373526Sxy150489 	iocp->ioc_error = 0;
45383526Sxy150489 
45395273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
45405273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
45415273Sgl147354 		return (IOC_INVAL);
45425273Sgl147354 	}
45435273Sgl147354 
45443526Sxy150489 	return (IOC_REPLY);
45453526Sxy150489 }
45463526Sxy150489 
45473526Sxy150489 static boolean_t
45487133Scc210113 e1000g_check_loopback_support(struct e1000_hw *hw)
45497133Scc210113 {
45507133Scc210113 	switch (hw->mac.type) {
45517133Scc210113 	case e1000_82540:
45527133Scc210113 	case e1000_82545:
45537133Scc210113 	case e1000_82545_rev_3:
45547133Scc210113 	case e1000_82546:
45557133Scc210113 	case e1000_82546_rev_3:
45567133Scc210113 	case e1000_82541:
45577133Scc210113 	case e1000_82541_rev_2:
45587133Scc210113 	case e1000_82547:
45597133Scc210113 	case e1000_82547_rev_2:
45607133Scc210113 	case e1000_82571:
45617133Scc210113 	case e1000_82572:
45627133Scc210113 	case e1000_82573:
45637133Scc210113 	case e1000_80003es2lan:
45647133Scc210113 		return (B_TRUE);
45657133Scc210113 	}
45667133Scc210113 	return (B_FALSE);
45677133Scc210113 }
45687133Scc210113 
45697133Scc210113 static boolean_t
45703526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
45713526Sxy150489 {
45723526Sxy150489 	struct e1000_hw *hw;
45733526Sxy150489 	int i, times;
45745082Syy150190 	boolean_t link_up;
45753526Sxy150489 
45763526Sxy150489 	if (mode == Adapter->loopback_mode)
45773526Sxy150489 		return (B_TRUE);
45783526Sxy150489 
45794919Sxy150489 	hw = &Adapter->shared;
45803526Sxy150489 	times = 0;
45813526Sxy150489 
45825082Syy150190 	Adapter->loopback_mode = mode;
45835082Syy150190 
45845082Syy150190 	if (mode == E1000G_LB_NONE) {
45853526Sxy150489 		/* Reset the chip */
45866735Scc210113 		hw->phy.autoneg_wait_to_complete = B_TRUE;
45873526Sxy150489 		(void) e1000g_reset(Adapter);
45886735Scc210113 		hw->phy.autoneg_wait_to_complete = B_FALSE;
45895082Syy150190 		return (B_TRUE);
45905082Syy150190 	}
45915082Syy150190 
45925082Syy150190 again:
45935082Syy150190 
45945082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
45955082Syy150190 
45965082Syy150190 	switch (mode) {
45975082Syy150190 	default:
45985082Syy150190 		rw_exit(&Adapter->chip_lock);
45995082Syy150190 		return (B_FALSE);
46003526Sxy150489 
46013526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
46023526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
46033526Sxy150489 		break;
46043526Sxy150489 
46053526Sxy150489 	case E1000G_LB_EXTERNAL_100:
46063526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
46073526Sxy150489 		break;
46083526Sxy150489 
46093526Sxy150489 	case E1000G_LB_EXTERNAL_10:
46103526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
46113526Sxy150489 		break;
46123526Sxy150489 
46133526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
46143526Sxy150489 		e1000g_set_internal_loopback(Adapter);
46153526Sxy150489 		break;
46163526Sxy150489 	}
46173526Sxy150489 
46183526Sxy150489 	times++;
46193526Sxy150489 
46205858Scc210113 	rw_exit(&Adapter->chip_lock);
46215858Scc210113 
46225082Syy150190 	/* Wait for link up */
46235082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
46245082Syy150190 		msec_delay(100);
46255082Syy150190 
46265858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
46275858Scc210113 
46285082Syy150190 	link_up = e1000g_link_up(Adapter);
46295082Syy150190 
46305082Syy150190 	rw_exit(&Adapter->chip_lock);
46315082Syy150190 
46325082Syy150190 	if (!link_up) {
46335082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46345082Syy150190 		    "Failed to get the link up");
46355082Syy150190 		if (times < 2) {
46365082Syy150190 			/* Reset the link */
46374919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46385082Syy150190 			    "Reset the link ...");
46395082Syy150190 			(void) e1000g_reset(Adapter);
46405082Syy150190 			goto again;
46413526Sxy150489 		}
46423526Sxy150489 	}
46433526Sxy150489 
46443526Sxy150489 	return (B_TRUE);
46453526Sxy150489 }
46463526Sxy150489 
46473526Sxy150489 /*
46483526Sxy150489  * The following loopback settings are from Intel's technical
46493526Sxy150489  * document - "How To Loopback". All the register settings and
46503526Sxy150489  * time delay values are directly inherited from the document
46513526Sxy150489  * without more explanations available.
46523526Sxy150489  */
46533526Sxy150489 static void
46543526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
46553526Sxy150489 {
46563526Sxy150489 	struct e1000_hw *hw;
46573526Sxy150489 	uint32_t ctrl;
46583526Sxy150489 	uint32_t status;
46593526Sxy150489 	uint16_t phy_ctrl;
46605082Syy150190 	uint32_t txcw;
46613526Sxy150489 
46624919Sxy150489 	hw = &Adapter->shared;
46633526Sxy150489 
46643526Sxy150489 	/* Disable Smart Power Down */
46653526Sxy150489 	phy_spd_state(hw, B_FALSE);
46663526Sxy150489 
46677426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
46683526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
46693526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
46703526Sxy150489 
46714919Sxy150489 	switch (hw->mac.type) {
46723526Sxy150489 	case e1000_82540:
46733526Sxy150489 	case e1000_82545:
46743526Sxy150489 	case e1000_82545_rev_3:
46753526Sxy150489 	case e1000_82546:
46763526Sxy150489 	case e1000_82546_rev_3:
46773526Sxy150489 	case e1000_82573:
46783526Sxy150489 		/* Auto-MDI/MDIX off */
46797426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
46803526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
46817426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
46824349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
46833526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
46847426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
46854349Sxy150489 		    phy_ctrl | MII_CR_RESET);
46865082Syy150190 		/*
46875082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
46885082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
46895082Syy150190 		 * background.
46905082Syy150190 		 */
46917426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
46927426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
46937426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
46947426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
46953526Sxy150489 		break;
46967133Scc210113 	case e1000_80003es2lan:
46977133Scc210113 		/* Force Link Up */
46987426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
46997426SChenliang.Xu@Sun.COM 		    0x1CC);
47007133Scc210113 		/* Sets PCS loopback at 1Gbs */
47017426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
47027426SChenliang.Xu@Sun.COM 		    0x1046);
47037133Scc210113 		break;
47043526Sxy150489 	}
47053526Sxy150489 
47063526Sxy150489 	/* Set loopback */
47077426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
47083526Sxy150489 
47093526Sxy150489 	msec_delay(250);
47103526Sxy150489 
47113526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
47124919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
47133526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
47143526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
47154349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
47164349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
47174349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
47183526Sxy150489 
47194919Sxy150489 	switch (hw->mac.type) {
47203526Sxy150489 	case e1000_82540:
47213526Sxy150489 	case e1000_82545:
47223526Sxy150489 	case e1000_82545_rev_3:
47233526Sxy150489 	case e1000_82546:
47243526Sxy150489 	case e1000_82546_rev_3:
47253526Sxy150489 		/*
47263526Sxy150489 		 * For some serdes we'll need to commit the writes now
47273526Sxy150489 		 * so that the status is updated on link
47283526Sxy150489 		 */
47296735Scc210113 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
47304919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47313526Sxy150489 			msec_delay(100);
47324919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
47333526Sxy150489 		}
47343526Sxy150489 
47356735Scc210113 		if (hw->phy.media_type == e1000_media_type_copper) {
47363526Sxy150489 			/* Invert Loss of Signal */
47373526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
47383526Sxy150489 		} else {
47393526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
47404919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47413526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
47423526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
47433526Sxy150489 		}
47447133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47453526Sxy150489 		break;
47463526Sxy150489 
47473526Sxy150489 	case e1000_82571:
47483526Sxy150489 	case e1000_82572:
47495082Syy150190 		/*
47505082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
47515082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
47525082Syy150190 		 * using SerDes analog loopback.
47535082Syy150190 		 */
47546735Scc210113 		if (hw->phy.media_type != e1000_media_type_copper) {
47554919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47565082Syy150190 			/* Set ILOS on fiber nic if half duplex is detected */
47575082Syy150190 			if (((status & E1000_STATUS_LU) == 0) ||
47585082Syy150190 			    ((status & E1000_STATUS_FD) == 0) ||
47596735Scc210113 			    (hw->phy.media_type ==
47605082Syy150190 			    e1000_media_type_internal_serdes))
47613526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
47625082Syy150190 
47635082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
47645082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
47655082Syy150190 			txcw &= ~((uint32_t)1 << 31);
47665082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
47675082Syy150190 
47685082Syy150190 			/*
47695082Syy150190 			 * Write 0x410 to Serdes Control register
47705082Syy150190 			 * to enable Serdes analog loopback
47715082Syy150190 			 */
47725082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
47735082Syy150190 			msec_delay(10);
47743526Sxy150489 		}
47757133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47763526Sxy150489 		break;
47773526Sxy150489 
47783526Sxy150489 	case e1000_82573:
47793526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
47807133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47813526Sxy150489 		break;
47823526Sxy150489 	}
47833526Sxy150489 }
47843526Sxy150489 
47853526Sxy150489 static void
47863526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
47873526Sxy150489 {
47883526Sxy150489 	struct e1000_hw *hw;
47893526Sxy150489 	uint32_t rctl;
47903526Sxy150489 	uint32_t ctrl_ext;
47913526Sxy150489 	uint32_t ctrl;
47923526Sxy150489 	uint32_t status;
47933526Sxy150489 	uint32_t txcw;
47947133Scc210113 	uint16_t phydata;
47953526Sxy150489 
47964919Sxy150489 	hw = &Adapter->shared;
47973526Sxy150489 
47983526Sxy150489 	/* Disable Smart Power Down */
47993526Sxy150489 	phy_spd_state(hw, B_FALSE);
48003526Sxy150489 
48017133Scc210113 	switch (hw->mac.type) {
48027133Scc210113 	case e1000_82571:
48037133Scc210113 	case e1000_82572:
48047133Scc210113 		switch (hw->phy.media_type) {
48057133Scc210113 		case e1000_media_type_copper:
48067133Scc210113 			/* Force link up (Must be done before the PHY writes) */
48077133Scc210113 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
48087133Scc210113 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
48097133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48107133Scc210113 
48117133Scc210113 			rctl = E1000_READ_REG(hw, E1000_RCTL);
48127133Scc210113 			rctl |= (E1000_RCTL_EN |
48137133Scc210113 			    E1000_RCTL_SBP |
48147133Scc210113 			    E1000_RCTL_UPE |
48157133Scc210113 			    E1000_RCTL_MPE |
48167133Scc210113 			    E1000_RCTL_LPE |
48177133Scc210113 			    E1000_RCTL_BAM);		/* 0x803E */
48187133Scc210113 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
48197133Scc210113 
48207133Scc210113 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
48217133Scc210113 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
48227133Scc210113 			    E1000_CTRL_EXT_SDP6_DATA |
48237133Scc210113 			    E1000_CTRL_EXT_SDP7_DATA |
48247133Scc210113 			    E1000_CTRL_EXT_SDP4_DIR |
48257133Scc210113 			    E1000_CTRL_EXT_SDP6_DIR |
48267133Scc210113 			    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
48277133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
48287133Scc210113 
48297133Scc210113 			/*
48307133Scc210113 			 * This sequence tunes the PHY's SDP and no customer
48317133Scc210113 			 * settable values. For background, see comments above
48327133Scc210113 			 * e1000g_set_internal_loopback().
48337133Scc210113 			 */
48347426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
48357133Scc210113 			msec_delay(10);
48367426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
48377426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
48387426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
48397426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
48407426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
48417426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
48427426SChenliang.Xu@Sun.COM 
48437426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
48447426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
48457426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
48467426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
48477426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
48487133Scc210113 
48497133Scc210113 			msec_delay(50);
48507133Scc210113 			break;
48517133Scc210113 		case e1000_media_type_fiber:
48527133Scc210113 		case e1000_media_type_internal_serdes:
48537133Scc210113 			status = E1000_READ_REG(hw, E1000_STATUS);
48547133Scc210113 			if (((status & E1000_STATUS_LU) == 0) ||
48557133Scc210113 			    (hw->phy.media_type ==
48567133Scc210113 			    e1000_media_type_internal_serdes)) {
48577133Scc210113 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
48587133Scc210113 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
48597133Scc210113 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48607133Scc210113 			}
48617133Scc210113 
48627133Scc210113 			/* Disable autoneg by setting bit 31 of TXCW to zero */
48637133Scc210113 			txcw = E1000_READ_REG(hw, E1000_TXCW);
48647133Scc210113 			txcw &= ~((uint32_t)1 << 31);
48657133Scc210113 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
48667133Scc210113 
48677133Scc210113 			/*
48687133Scc210113 			 * Write 0x410 to Serdes Control register
48697133Scc210113 			 * to enable Serdes analog loopback
48707133Scc210113 			 */
48717133Scc210113 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
48727133Scc210113 			msec_delay(10);
48737133Scc210113 			break;
48747133Scc210113 		default:
48757133Scc210113 			break;
48767133Scc210113 		}
48773526Sxy150489 		break;
48787133Scc210113 	case e1000_80003es2lan:
48797426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
48807426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
48817426SChenliang.Xu@Sun.COM 		    phydata | (1 << 5));
48827133Scc210113 		Adapter->param_adv_autoneg = 1;
48837133Scc210113 		Adapter->param_adv_1000fdx = 1;
48847426SChenliang.Xu@Sun.COM 		(void) e1000g_reset_link(Adapter);
48853526Sxy150489 		break;
48863526Sxy150489 	}
48873526Sxy150489 }
48883526Sxy150489 
48893526Sxy150489 static void
48903526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
48913526Sxy150489 {
48923526Sxy150489 	struct e1000_hw *hw;
48933526Sxy150489 	uint32_t ctrl;
48943526Sxy150489 	uint16_t phy_ctrl;
48953526Sxy150489 
48964919Sxy150489 	hw = &Adapter->shared;
48973526Sxy150489 
48983526Sxy150489 	/* Disable Smart Power Down */
48993526Sxy150489 	phy_spd_state(hw, B_FALSE);
49003526Sxy150489 
49013526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
49024349Sxy150489 	    MII_CR_SPEED_100);
49033526Sxy150489 
49043526Sxy150489 	/* Force 100/FD, reset PHY */
49057426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49064349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
49073526Sxy150489 	msec_delay(10);
49083526Sxy150489 
49093526Sxy150489 	/* Force 100/FD */
49107426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49114349Sxy150489 	    phy_ctrl);			/* 0x2100 */
49123526Sxy150489 	msec_delay(10);
49133526Sxy150489 
49143526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
49154919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49163526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49173526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
49184349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
49194349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49204349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
49214349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49223526Sxy150489 
49234919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49243526Sxy150489 }
49253526Sxy150489 
49263526Sxy150489 static void
49273526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
49283526Sxy150489 {
49293526Sxy150489 	struct e1000_hw *hw;
49303526Sxy150489 	uint32_t ctrl;
49313526Sxy150489 	uint16_t phy_ctrl;
49323526Sxy150489 
49334919Sxy150489 	hw = &Adapter->shared;
49343526Sxy150489 
49353526Sxy150489 	/* Disable Smart Power Down */
49363526Sxy150489 	phy_spd_state(hw, B_FALSE);
49373526Sxy150489 
49383526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
49394349Sxy150489 	    MII_CR_SPEED_10);
49403526Sxy150489 
49413526Sxy150489 	/* Force 10/FD, reset PHY */
49427426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49434349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
49443526Sxy150489 	msec_delay(10);
49453526Sxy150489 
49463526Sxy150489 	/* Force 10/FD */
49477426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49484349Sxy150489 	    phy_ctrl);			/* 0x0100 */
49493526Sxy150489 	msec_delay(10);
49503526Sxy150489 
49513526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
49524919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49533526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49543526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
49554349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
49564349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49574349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
49584349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49593526Sxy150489 
49604919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49613526Sxy150489 }
49623526Sxy150489 
49633526Sxy150489 #ifdef __sparc
49643526Sxy150489 static boolean_t
49653526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
49663526Sxy150489 {
49674919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
49683526Sxy150489 	uchar_t *bytes;
49693526Sxy150489 	struct ether_addr sysaddr;
49703526Sxy150489 	uint_t nelts;
49713526Sxy150489 	int err;
49723526Sxy150489 	boolean_t found = B_FALSE;
49733526Sxy150489 
49743526Sxy150489 	/*
49753526Sxy150489 	 * The "vendor's factory-set address" may already have
49763526Sxy150489 	 * been extracted from the chip, but if the property
49773526Sxy150489 	 * "local-mac-address" is set we use that instead.
49783526Sxy150489 	 *
49793526Sxy150489 	 * We check whether it looks like an array of 6
49803526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
49813526Sxy150489 	 * make sense of it this way, we'll ignore it.
49823526Sxy150489 	 */
49833526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
49843526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
49853526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
49863526Sxy150489 		if (nelts == ETHERADDRL) {
49873526Sxy150489 			while (nelts--)
49884919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
49893526Sxy150489 			found = B_TRUE;
49903526Sxy150489 		}
49913526Sxy150489 		ddi_prop_free(bytes);
49923526Sxy150489 	}
49933526Sxy150489 
49943526Sxy150489 	/*
49953526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
49963526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
49973526Sxy150489 	 */
49983526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
49993526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
50003526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
50013526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
50024919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
50033526Sxy150489 				found = B_TRUE;
50043526Sxy150489 			}
50053526Sxy150489 		}
50063526Sxy150489 		ddi_prop_free(bytes);
50073526Sxy150489 	}
50083526Sxy150489 
50093526Sxy150489 	/*
50103526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
50113526Sxy150489 	 * if we netbooted from this interface), we must use this instead
50123526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
50133526Sxy150489 	 * get confused by the address changing as Solaris takes over!
50143526Sxy150489 	 */
50153526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
50163526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
50173526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
50183526Sxy150489 		if (nelts == ETHERADDRL) {
50193526Sxy150489 			while (nelts--)
50204919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
50213526Sxy150489 			found = B_TRUE;
50223526Sxy150489 		}
50233526Sxy150489 		ddi_prop_free(bytes);
50243526Sxy150489 	}
50253526Sxy150489 
50263526Sxy150489 	if (found) {
50274919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
50283526Sxy150489 		    ETHERADDRL);
50293526Sxy150489 	}
50303526Sxy150489 
50313526Sxy150489 	return (found);
50323526Sxy150489 }
50333526Sxy150489 #endif
50343526Sxy150489 
50353526Sxy150489 static int
50363526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
50373526Sxy150489 {
50383526Sxy150489 	dev_info_t *devinfo;
50393526Sxy150489 	int intr_types;
50403526Sxy150489 	int rc;
50413526Sxy150489 
50423526Sxy150489 	devinfo = Adapter->dip;
50433526Sxy150489 
50443526Sxy150489 	/* Get supported interrupt types */
50453526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
50463526Sxy150489 
50473526Sxy150489 	if (rc != DDI_SUCCESS) {
50484919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
50493526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
50503526Sxy150489 		return (DDI_FAILURE);
50513526Sxy150489 	}
50523526Sxy150489 
50533526Sxy150489 	/*
50543526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
50553526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
50563526Sxy150489 	 * that it supports MSI, but in fact has problems.
50573526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
50583526Sxy150489 	 * PCI/PCI-X NICs.
50593526Sxy150489 	 */
50604919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
50616986Smx205022 		Adapter->msi_enable = B_FALSE;
50626986Smx205022 
50636986Smx205022 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
50643526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
50653526Sxy150489 
50663526Sxy150489 		if (rc != DDI_SUCCESS) {
50674919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50683526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
50693526Sxy150489 		} else {
50703526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
50713526Sxy150489 		}
50723526Sxy150489 	}
50733526Sxy150489 
50743526Sxy150489 	if ((Adapter->intr_type == 0) &&
50753526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
50763526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
50773526Sxy150489 
50783526Sxy150489 		if (rc != DDI_SUCCESS) {
50794919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50803526Sxy150489 			    "Add Legacy interrupts failed\n");
50813526Sxy150489 			return (DDI_FAILURE);
50823526Sxy150489 		}
50833526Sxy150489 
50843526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
50853526Sxy150489 	}
50863526Sxy150489 
50873526Sxy150489 	if (Adapter->intr_type == 0) {
50884919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50893526Sxy150489 		    "No interrupts registered\n");
50903526Sxy150489 		return (DDI_FAILURE);
50913526Sxy150489 	}
50923526Sxy150489 
50933526Sxy150489 	return (DDI_SUCCESS);
50943526Sxy150489 }
50953526Sxy150489 
50963526Sxy150489 /*
50973526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
50983526Sxy150489  */
50993526Sxy150489 static int
51003526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
51013526Sxy150489 {
51023526Sxy150489 	dev_info_t *devinfo;
51033526Sxy150489 	int count, avail, actual;
51043526Sxy150489 	int x, y, rc, inum = 0;
51053526Sxy150489 	int flag;
51063526Sxy150489 	ddi_intr_handler_t *intr_handler;
51073526Sxy150489 
51083526Sxy150489 	devinfo = Adapter->dip;
51093526Sxy150489 
51103526Sxy150489 	/* get number of interrupts */
51113526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
51123526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
51134919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51143526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
51153526Sxy150489 		    rc, count);
51163526Sxy150489 		return (DDI_FAILURE);
51173526Sxy150489 	}
51183526Sxy150489 
51193526Sxy150489 	/* get number of available interrupts */
51203526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
51213526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
51224919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51233526Sxy150489 		    "Get interrupt available number failed. "
51243526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
51253526Sxy150489 		return (DDI_FAILURE);
51263526Sxy150489 	}
51273526Sxy150489 
51283526Sxy150489 	if (avail < count) {
51294919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51303526Sxy150489 		    "Interrupts count: %d, available: %d\n",
51313526Sxy150489 		    count, avail);
51323526Sxy150489 	}
51333526Sxy150489 
51343526Sxy150489 	/* Allocate an array of interrupt handles */
51353526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
51363526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
51373526Sxy150489 
51383526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
51393526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
51403526Sxy150489 
51413526Sxy150489 	/* call ddi_intr_alloc() */
51423526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
51433526Sxy150489 	    count, &actual, flag);
51443526Sxy150489 
51453526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
51464919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51473526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
51483526Sxy150489 
51493526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
51503526Sxy150489 		return (DDI_FAILURE);
51513526Sxy150489 	}
51523526Sxy150489 
51533526Sxy150489 	if (actual < count) {
51544919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51553526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
51563526Sxy150489 		    count, actual);
51573526Sxy150489 	}
51583526Sxy150489 
51593526Sxy150489 	Adapter->intr_cnt = actual;
51603526Sxy150489 
51613526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
51623526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
51633526Sxy150489 
51643526Sxy150489 	if (rc != DDI_SUCCESS) {
51654919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51663526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
51673526Sxy150489 
51683526Sxy150489 		/* Free already allocated intr */
51693526Sxy150489 		for (y = 0; y < actual; y++)
51703526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
51713526Sxy150489 
51723526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
51733526Sxy150489 		return (DDI_FAILURE);
51743526Sxy150489 	}
51753526Sxy150489 
51763526Sxy150489 	/*
51773526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
51783526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
51793526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
51803526Sxy150489 	 * devices.
51813526Sxy150489 	 */
51824919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
51833526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
51843526Sxy150489 	else
51853526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
51863526Sxy150489 
51873526Sxy150489 	/* Call ddi_intr_add_handler() */
51883526Sxy150489 	for (x = 0; x < actual; x++) {
51893526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
51903526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
51913526Sxy150489 
51923526Sxy150489 		if (rc != DDI_SUCCESS) {
51934919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51943526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
51953526Sxy150489 
51963526Sxy150489 			/* Remove already added handler */
51973526Sxy150489 			for (y = 0; y < x; y++)
51983526Sxy150489 				(void) ddi_intr_remove_handler(
51993526Sxy150489 				    Adapter->htable[y]);
52003526Sxy150489 
52013526Sxy150489 			/* Free already allocated intr */
52023526Sxy150489 			for (y = 0; y < actual; y++)
52033526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
52043526Sxy150489 
52053526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
52063526Sxy150489 			return (DDI_FAILURE);
52073526Sxy150489 		}
52083526Sxy150489 	}
52093526Sxy150489 
52103526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
52113526Sxy150489 
52123526Sxy150489 	if (rc != DDI_SUCCESS) {
52134919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52143526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
52153526Sxy150489 
52163526Sxy150489 		/* Free already allocated intr */
52173526Sxy150489 		for (y = 0; y < actual; y++) {
52183526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
52193526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
52203526Sxy150489 		}
52213526Sxy150489 
52223526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
52233526Sxy150489 		return (DDI_FAILURE);
52243526Sxy150489 	}
52253526Sxy150489 
52263526Sxy150489 	return (DDI_SUCCESS);
52273526Sxy150489 }
52283526Sxy150489 
52293526Sxy150489 static int
52303526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
52313526Sxy150489 {
52323526Sxy150489 	int x;
52333526Sxy150489 	int rc;
52343526Sxy150489 
52353526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
52363526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
52373526Sxy150489 		if (rc != DDI_SUCCESS) {
52384919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52393526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
52403526Sxy150489 			return (DDI_FAILURE);
52413526Sxy150489 		}
52423526Sxy150489 
52433526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
52443526Sxy150489 		if (rc != DDI_SUCCESS) {
52454919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52463526Sxy150489 			    "Free intr failed: %d\n", rc);
52473526Sxy150489 			return (DDI_FAILURE);
52483526Sxy150489 		}
52493526Sxy150489 	}
52503526Sxy150489 
52513526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
52523526Sxy150489 
52533526Sxy150489 	return (DDI_SUCCESS);
52543526Sxy150489 }
52553526Sxy150489 
52563526Sxy150489 static int
52573526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
52583526Sxy150489 {
52593526Sxy150489 	int x;
52603526Sxy150489 	int rc;
52613526Sxy150489 
52623526Sxy150489 	/* Enable interrupts */
52633526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
52643526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
52653526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
52663526Sxy150489 		    Adapter->intr_cnt);
52673526Sxy150489 		if (rc != DDI_SUCCESS) {
52684919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52693526Sxy150489 			    "Enable block intr failed: %d\n", rc);
52703526Sxy150489 			return (DDI_FAILURE);
52713526Sxy150489 		}
52723526Sxy150489 	} else {
52733526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
52743526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
52753526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
52763526Sxy150489 			if (rc != DDI_SUCCESS) {
52774919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52783526Sxy150489 				    "Enable intr failed: %d\n", rc);
52793526Sxy150489 				return (DDI_FAILURE);
52803526Sxy150489 			}
52813526Sxy150489 		}
52823526Sxy150489 	}
52833526Sxy150489 
52843526Sxy150489 	return (DDI_SUCCESS);
52853526Sxy150489 }
52863526Sxy150489 
52873526Sxy150489 static int
52883526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
52893526Sxy150489 {
52903526Sxy150489 	int x;
52913526Sxy150489 	int rc;
52923526Sxy150489 
52933526Sxy150489 	/* Disable all interrupts */
52943526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
52953526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
52963526Sxy150489 		    Adapter->intr_cnt);
52973526Sxy150489 		if (rc != DDI_SUCCESS) {
52984919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52993526Sxy150489 			    "Disable block intr failed: %d\n", rc);
53003526Sxy150489 			return (DDI_FAILURE);
53013526Sxy150489 		}
53023526Sxy150489 	} else {
53033526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
53043526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
53053526Sxy150489 			if (rc != DDI_SUCCESS) {
53064919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53073526Sxy150489 				    "Disable intr failed: %d\n", rc);
53083526Sxy150489 				return (DDI_FAILURE);
53093526Sxy150489 			}
53103526Sxy150489 		}
53113526Sxy150489 	}
53123526Sxy150489 
53133526Sxy150489 	return (DDI_SUCCESS);
53143526Sxy150489 }
53155082Syy150190 
53165082Syy150190 /*
53175082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
53185082Syy150190  */
53195082Syy150190 static void
53205082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
53215082Syy150190 {
53225082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
53235082Syy150190 
53247426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
53257426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
53267426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
53277426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
53287426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
53297426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
53307426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
53317426SChenliang.Xu@Sun.COM 	    &Adapter->phy_1000t_status);
53327426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
53336394Scc210113 
53346394Scc210113 	Adapter->param_autoneg_cap =
53356394Scc210113 	    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
53366394Scc210113 	Adapter->param_pause_cap =
53376394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
53386394Scc210113 	Adapter->param_asym_pause_cap =
53396394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
53406394Scc210113 	Adapter->param_1000fdx_cap =
53416394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
53426394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
53436394Scc210113 	Adapter->param_1000hdx_cap =
53446394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
53456394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
53466394Scc210113 	Adapter->param_100t4_cap =
53476394Scc210113 	    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
53486394Scc210113 	Adapter->param_100fdx_cap =
53496394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
53506394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
53516394Scc210113 	Adapter->param_100hdx_cap =
53526394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
53536394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
53546394Scc210113 	Adapter->param_10fdx_cap =
53556394Scc210113 	    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
53566394Scc210113 	Adapter->param_10hdx_cap =
53576394Scc210113 	    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
53586394Scc210113 
53596394Scc210113 	Adapter->param_adv_autoneg = hw->mac.autoneg;
53606394Scc210113 	Adapter->param_adv_pause =
53616394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
53626394Scc210113 	Adapter->param_adv_asym_pause =
53636394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
53646394Scc210113 	Adapter->param_adv_1000hdx =
53656394Scc210113 	    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
53666394Scc210113 	Adapter->param_adv_100t4 =
53676394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
53686394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
53696394Scc210113 		Adapter->param_adv_1000fdx =
53706394Scc210113 		    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
53716394Scc210113 		Adapter->param_adv_100fdx =
53726394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
53736394Scc210113 		Adapter->param_adv_100hdx =
53746394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
53756394Scc210113 		Adapter->param_adv_10fdx =
53766394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
53776394Scc210113 		Adapter->param_adv_10hdx =
53786394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
53796394Scc210113 	}
53806394Scc210113 
53816394Scc210113 	Adapter->param_lp_autoneg =
53826394Scc210113 	    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
53836394Scc210113 	Adapter->param_lp_pause =
53846394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
53856394Scc210113 	Adapter->param_lp_asym_pause =
53866394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
53876394Scc210113 	Adapter->param_lp_1000fdx =
53886394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
53896394Scc210113 	Adapter->param_lp_1000hdx =
53906394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
53916394Scc210113 	Adapter->param_lp_100t4 =
53926394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
53936394Scc210113 	Adapter->param_lp_100fdx =
53946394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
53956394Scc210113 	Adapter->param_lp_100hdx =
53966394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
53976394Scc210113 	Adapter->param_lp_10fdx =
53986394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
53996394Scc210113 	Adapter->param_lp_10hdx =
54006394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
54015082Syy150190 }
54025273Sgl147354 
54035273Sgl147354 /*
54045273Sgl147354  * FMA support
54055273Sgl147354  */
54065273Sgl147354 
54075273Sgl147354 int
54085273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
54095273Sgl147354 {
54105273Sgl147354 	ddi_fm_error_t de;
54115273Sgl147354 
54125273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
54135273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
54145273Sgl147354 	return (de.fme_status);
54155273Sgl147354 }
54165273Sgl147354 
54175273Sgl147354 int
54185273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
54195273Sgl147354 {
54205273Sgl147354 	ddi_fm_error_t de;
54215273Sgl147354 
54225273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
54235273Sgl147354 	return (de.fme_status);
54245273Sgl147354 }
54255273Sgl147354 
54265273Sgl147354 /*
54275273Sgl147354  * The IO fault service error handling callback function
54285273Sgl147354  */
54297426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
54305273Sgl147354 static int
54315273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
54325273Sgl147354 {
54335273Sgl147354 	/*
54345273Sgl147354 	 * as the driver can always deal with an error in any dma or
54355273Sgl147354 	 * access handle, we can just return the fme_status value.
54365273Sgl147354 	 */
54375273Sgl147354 	pci_ereport_post(dip, err, NULL);
54385273Sgl147354 	return (err->fme_status);
54395273Sgl147354 }
54405273Sgl147354 
54415273Sgl147354 static void
54425273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
54435273Sgl147354 {
54445273Sgl147354 	ddi_iblock_cookie_t iblk;
54455273Sgl147354 	int fma_acc_flag, fma_dma_flag;
54465273Sgl147354 
54475273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
54485273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
54495273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
54505273Sgl147354 		fma_acc_flag = 1;
54515273Sgl147354 	} else {
54525273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
54535273Sgl147354 		fma_acc_flag = 0;
54545273Sgl147354 	}
54555273Sgl147354 
54565273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
54575273Sgl147354 		fma_dma_flag = 1;
54585273Sgl147354 	} else {
54595273Sgl147354 		fma_dma_flag = 0;
54605273Sgl147354 	}
54615273Sgl147354 
54625273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
54635273Sgl147354 
54645273Sgl147354 	if (Adapter->fm_capabilities) {
54655273Sgl147354 
54665273Sgl147354 		/* Register capabilities with IO Fault Services */
54675273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
54685273Sgl147354 
54695273Sgl147354 		/*
54705273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
54715273Sgl147354 		 */
54725273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
54735273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54745273Sgl147354 			pci_ereport_setup(Adapter->dip);
54755273Sgl147354 
54765273Sgl147354 		/*
54775273Sgl147354 		 * Register error callback if error callback capable
54785273Sgl147354 		 */
54795273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54805273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
54815273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
54825273Sgl147354 	}
54835273Sgl147354 }
54845273Sgl147354 
54855273Sgl147354 static void
54865273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
54875273Sgl147354 {
54885273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
54895273Sgl147354 	if (Adapter->fm_capabilities) {
54905273Sgl147354 
54915273Sgl147354 		/*
54925273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
54935273Sgl147354 		 */
54945273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
54955273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54965273Sgl147354 			pci_ereport_teardown(Adapter->dip);
54975273Sgl147354 
54985273Sgl147354 		/*
54995273Sgl147354 		 * Un-register error callback if error callback capable
55005273Sgl147354 		 */
55015273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
55025273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
55035273Sgl147354 
55045273Sgl147354 		/* Unregister from IO Fault Services */
55055273Sgl147354 		ddi_fm_fini(Adapter->dip);
55065273Sgl147354 	}
55075273Sgl147354 }
55085273Sgl147354 
55095273Sgl147354 void
55105273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
55115273Sgl147354 {
55125273Sgl147354 	uint64_t ena;
55135273Sgl147354 	char buf[FM_MAX_CLASS];
55145273Sgl147354 
55155273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
55165273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
55175273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
55185273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
55195273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
55205273Sgl147354 	}
55215273Sgl147354 }
55226512Ssowmini 
55236512Ssowmini static int
55246512Ssowmini e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
55256512Ssowmini     uint_t pr_valsize, void *pr_val)
55266512Ssowmini {
55276512Ssowmini 	link_flowctrl_t fl;
55286512Ssowmini 	int err = 0;
55296512Ssowmini 
55306512Ssowmini 	ASSERT(pr_valsize > 0);
55316512Ssowmini 	switch (pr_num) {
55326789Sam223141 	case MAC_PROP_AUTONEG:
55336512Ssowmini 		*(uint8_t *)pr_val =
55346512Ssowmini 		    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0);
55356512Ssowmini 		break;
55366789Sam223141 	case MAC_PROP_FLOWCTRL:
55376512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
55386512Ssowmini 			return (EINVAL);
55396512Ssowmini 		fl = LINK_FLOWCTRL_BI;
55406512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
55416512Ssowmini 		break;
55426789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
55436789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
55446512Ssowmini 		*(uint8_t *)pr_val =
55456512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
55466512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
55476512Ssowmini 		break;
55486789Sam223141 	case MAC_PROP_ADV_1000HDX_CAP:
55496789Sam223141 	case MAC_PROP_EN_1000HDX_CAP:
55506512Ssowmini 		*(uint8_t *)pr_val =
55516512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
55526512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
55536512Ssowmini 		break;
55546789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
55556789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
55566512Ssowmini 		*(uint8_t *)pr_val =
55576512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
55586512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
55597426SChenliang.Xu@Sun.COM 		break;
55606789Sam223141 	case MAC_PROP_ADV_100HDX_CAP:
55616789Sam223141 	case MAC_PROP_EN_100HDX_CAP:
55626512Ssowmini 		*(uint8_t *)pr_val =
55636512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
55646512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
55656512Ssowmini 		break;
55666789Sam223141 	case MAC_PROP_ADV_10FDX_CAP:
55676789Sam223141 	case MAC_PROP_EN_10FDX_CAP:
55686512Ssowmini 		*(uint8_t *)pr_val =
55696512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
55706512Ssowmini 		break;
55716789Sam223141 	case MAC_PROP_ADV_10HDX_CAP:
55726789Sam223141 	case MAC_PROP_EN_10HDX_CAP:
55736512Ssowmini 		*(uint8_t *)pr_val =
55746512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
55756512Ssowmini 		break;
55766512Ssowmini 	default:
55776512Ssowmini 		err = ENOTSUP;
55786512Ssowmini 		break;
55796512Ssowmini 	}
55806512Ssowmini 	return (err);
55816512Ssowmini }
55826512Ssowmini 
55836512Ssowmini /*
55846512Ssowmini  * synchronize the adv* and en* parameters.
55856512Ssowmini  *
55866512Ssowmini  * See comments in <sys/dld.h> for details of the *_en_*
55876512Ssowmini  * parameters. The usage of ndd for setting adv parameters will
55886512Ssowmini  * synchronize all the en parameters with the e1000g parameters,
5589*7607STed.You@Sun.COM  * implicitly disabling any settings made via dladm.
55906512Ssowmini  */
55916512Ssowmini static void
55926512Ssowmini e1000g_param_sync(struct e1000g *Adapter)
55936512Ssowmini {
55946512Ssowmini 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
55956512Ssowmini 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
55966512Ssowmini 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
55976512Ssowmini 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
55986512Ssowmini 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
55996512Ssowmini 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
56006512Ssowmini }
5601*7607STed.You@Sun.COM 
5602*7607STed.You@Sun.COM /*
5603*7607STed.You@Sun.COM  * e1000g_get_driver_control - tell manageability firmware that the driver
5604*7607STed.You@Sun.COM  * has control.
5605*7607STed.You@Sun.COM  */
5606*7607STed.You@Sun.COM static void
5607*7607STed.You@Sun.COM e1000g_get_driver_control(struct e1000_hw *hw)
5608*7607STed.You@Sun.COM {
5609*7607STed.You@Sun.COM 	uint32_t ctrl_ext;
5610*7607STed.You@Sun.COM 	uint32_t swsm;
5611*7607STed.You@Sun.COM 
5612*7607STed.You@Sun.COM 	/* tell manageability firmware the driver has taken over */
5613*7607STed.You@Sun.COM 	switch (hw->mac.type) {
5614*7607STed.You@Sun.COM 	case e1000_82573:
5615*7607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
5616*7607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
5617*7607STed.You@Sun.COM 		break;
5618*7607STed.You@Sun.COM 	case e1000_82571:
5619*7607STed.You@Sun.COM 	case e1000_82572:
5620*7607STed.You@Sun.COM 	case e1000_82574:
5621*7607STed.You@Sun.COM 	case e1000_80003es2lan:
5622*7607STed.You@Sun.COM 	case e1000_ich8lan:
5623*7607STed.You@Sun.COM 	case e1000_ich9lan:
5624*7607STed.You@Sun.COM 	case e1000_ich10lan:
5625*7607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
5626*7607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
5627*7607STed.You@Sun.COM 		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
5628*7607STed.You@Sun.COM 		break;
5629*7607STed.You@Sun.COM 	default:
5630*7607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
5631*7607STed.You@Sun.COM 		break;
5632*7607STed.You@Sun.COM 	}
5633*7607STed.You@Sun.COM }
5634*7607STed.You@Sun.COM 
5635*7607STed.You@Sun.COM /*
5636*7607STed.You@Sun.COM  * e1000g_release_driver_control - tell manageability firmware that the driver
5637*7607STed.You@Sun.COM  * has released control.
5638*7607STed.You@Sun.COM  */
5639*7607STed.You@Sun.COM static void
5640*7607STed.You@Sun.COM e1000g_release_driver_control(struct e1000_hw *hw)
5641*7607STed.You@Sun.COM {
5642*7607STed.You@Sun.COM 	uint32_t ctrl_ext;
5643*7607STed.You@Sun.COM 	uint32_t swsm;
5644*7607STed.You@Sun.COM 
5645*7607STed.You@Sun.COM 	/* tell manageability firmware the driver has released control */
5646*7607STed.You@Sun.COM 	switch (hw->mac.type) {
5647*7607STed.You@Sun.COM 	case e1000_82573:
5648*7607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
5649*7607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
5650*7607STed.You@Sun.COM 		break;
5651*7607STed.You@Sun.COM 	case e1000_82571:
5652*7607STed.You@Sun.COM 	case e1000_82572:
5653*7607STed.You@Sun.COM 	case e1000_82574:
5654*7607STed.You@Sun.COM 	case e1000_80003es2lan:
5655*7607STed.You@Sun.COM 	case e1000_ich8lan:
5656*7607STed.You@Sun.COM 	case e1000_ich9lan:
5657*7607STed.You@Sun.COM 	case e1000_ich10lan:
5658*7607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
5659*7607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
5660*7607STed.You@Sun.COM 		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
5661*7607STed.You@Sun.COM 		break;
5662*7607STed.You@Sun.COM 	default:
5663*7607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
5664*7607STed.You@Sun.COM 		break;
5665*7607STed.You@Sun.COM 	}
5666*7607STed.You@Sun.COM }
5667