xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 10680:45bda3640f27)
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  *
98479SChenlu.Chen@Sun.COM  * Copyright(c) 1999 - 2009 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 /*
228479SChenlu.Chen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
238118SVasumathi.Sundaram@Sun.COM  * Use is subject to license terms.
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 
477656SSherry.Moore@Sun.COM static char ident[] = "Intel PRO/1000 Ethernet";
483526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
49*10680SMin.Xu@Sun.COM static char e1000g_version[] = "Driver Ver. 5.3.15";
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);
567656SSherry.Moore@Sun.COM static int e1000g_quiesce(dev_info_t *);
573526Sxy150489 
583526Sxy150489 /*
593526Sxy150489  * init and intr routines prototype
603526Sxy150489  */
614919Sxy150489 static int e1000g_resume(dev_info_t *);
624919Sxy150489 static int e1000g_suspend(dev_info_t *);
633526Sxy150489 static uint_t e1000g_intr_pciexpress(caddr_t);
643526Sxy150489 static uint_t e1000g_intr(caddr_t);
653526Sxy150489 static void e1000g_intr_work(struct e1000g *, uint32_t);
663526Sxy150489 #pragma inline(e1000g_intr_work)
673526Sxy150489 static int e1000g_init(struct e1000g *);
684919Sxy150489 static int e1000g_start(struct e1000g *, boolean_t);
694919Sxy150489 static void e1000g_stop(struct e1000g *, boolean_t);
703526Sxy150489 static int e1000g_m_start(void *);
713526Sxy150489 static void e1000g_m_stop(void *);
723526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
733526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
743526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
753526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
766394Scc210113 static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
776394Scc210113     uint_t, const void *);
786394Scc210113 static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
798118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
806394Scc210113 static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
816394Scc210113     const void *);
826394Scc210113 static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t,
838118SVasumathi.Sundaram@Sun.COM     uint_t, void *, uint_t *);
844919Sxy150489 static void e1000g_init_locks(struct e1000g *);
854919Sxy150489 static void e1000g_destroy_locks(struct e1000g *);
864919Sxy150489 static int e1000g_identify_hardware(struct e1000g *);
874919Sxy150489 static int e1000g_regs_map(struct e1000g *);
884919Sxy150489 static int e1000g_set_driver_params(struct e1000g *);
896394Scc210113 static void e1000g_set_bufsize(struct e1000g *);
904919Sxy150489 static int e1000g_register_mac(struct e1000g *);
914919Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *);
924919Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *);
934919Sxy150489 static void e1000g_init_unicst(struct e1000g *);
948275SEric Cheng static int e1000g_unicst_set(struct e1000g *, const uint8_t *, int);
958850SMin.Xu@Sun.COM static int e1000g_alloc_rx_data(struct e1000g *);
969770SChangqing.Li@Sun.COM static void e1000g_release_multicast(struct e1000g *);
97*10680SMin.Xu@Sun.COM static void e1000g_pch_limits(struct e1000g *);
98*10680SMin.Xu@Sun.COM static uint32_t e1000g_mtu2maxframe(uint32_t);
993526Sxy150489 
1003526Sxy150489 /*
1013526Sxy150489  * Local routines
1023526Sxy150489  */
1037656SSherry.Moore@Sun.COM static boolean_t e1000g_reset_adapter(struct e1000g *);
1044919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1054919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1064061Sxy150489 static void e1000g_link_timer(void *);
1074919Sxy150489 static void e1000g_local_timer(void *);
1084061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1093526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1103526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1114919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1124919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1134919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1144919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1154919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1164919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1174919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1184919Sxy150489 static void stop_link_timer(struct e1000g *);
1194919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1204919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
121*10680SMin.Xu@Sun.COM static void e1000g_setup_max_mtu(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 *);
1515273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1525273Sgl147354     const void *impl_data);
1535273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1545273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1556512Ssowmini static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
1566512Ssowmini static void e1000g_param_sync(struct e1000g *);
1577607STed.You@Sun.COM static void e1000g_get_driver_control(struct e1000_hw *);
1587607STed.You@Sun.COM static void e1000g_release_driver_control(struct e1000_hw *);
1597722SShuguo.Yang@Sun.COM static void e1000g_restore_promisc(struct e1000g *Adapter);
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 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
1736512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
1746512Ssowmini };
1756512Ssowmini #define	E1000G_MAX_PRIV_PROPS	\
1766512Ssowmini 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
1776512Ssowmini 
1783526Sxy150489 
1793526Sxy150489 static struct cb_ops cb_ws_ops = {
1803526Sxy150489 	nulldev,		/* cb_open */
1813526Sxy150489 	nulldev,		/* cb_close */
1823526Sxy150489 	nodev,			/* cb_strategy */
1833526Sxy150489 	nodev,			/* cb_print */
1843526Sxy150489 	nodev,			/* cb_dump */
1853526Sxy150489 	nodev,			/* cb_read */
1863526Sxy150489 	nodev,			/* cb_write */
1873526Sxy150489 	nodev,			/* cb_ioctl */
1883526Sxy150489 	nodev,			/* cb_devmap */
1893526Sxy150489 	nodev,			/* cb_mmap */
1903526Sxy150489 	nodev,			/* cb_segmap */
1913526Sxy150489 	nochpoll,		/* cb_chpoll */
1923526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1933526Sxy150489 	NULL,			/* cb_stream */
1943526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1953526Sxy150489 	CB_REV,			/* cb_rev */
1963526Sxy150489 	nodev,			/* cb_aread */
1973526Sxy150489 	nodev			/* cb_awrite */
1983526Sxy150489 };
1993526Sxy150489 
2003526Sxy150489 static struct dev_ops ws_ops = {
2013526Sxy150489 	DEVO_REV,		/* devo_rev */
2023526Sxy150489 	0,			/* devo_refcnt */
2033526Sxy150489 	NULL,			/* devo_getinfo */
2043526Sxy150489 	nulldev,		/* devo_identify */
2053526Sxy150489 	nulldev,		/* devo_probe */
2064919Sxy150489 	e1000g_attach,		/* devo_attach */
2074919Sxy150489 	e1000g_detach,		/* devo_detach */
2083526Sxy150489 	nodev,			/* devo_reset */
2093526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
2103526Sxy150489 	NULL,			/* devo_bus_ops */
2117656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
2127656SSherry.Moore@Sun.COM 	e1000g_quiesce		/* devo_quiesce */
2133526Sxy150489 };
2143526Sxy150489 
2153526Sxy150489 static struct modldrv modldrv = {
2163526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
2173526Sxy150489 	ident,			/* Discription string */
2183526Sxy150489 	&ws_ops,		/* driver ops */
2193526Sxy150489 };
2203526Sxy150489 
2213526Sxy150489 static struct modlinkage modlinkage = {
2223526Sxy150489 	MODREV_1, &modldrv, NULL
2233526Sxy150489 };
2243526Sxy150489 
2254919Sxy150489 /* Access attributes for register mapping */
2264919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
2273526Sxy150489 	DDI_DEVICE_ATTR_V0,
2283526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2293526Sxy150489 	DDI_STRICTORDER_ACC,
2305273Sgl147354 	DDI_FLAGERR_ACC
2313526Sxy150489 };
2323526Sxy150489 
2336394Scc210113 #define	E1000G_M_CALLBACK_FLAGS \
2346394Scc210113 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
2353526Sxy150489 
2363526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2373526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2383526Sxy150489 	e1000g_m_stat,
2393526Sxy150489 	e1000g_m_start,
2403526Sxy150489 	e1000g_m_stop,
2413526Sxy150489 	e1000g_m_promisc,
2423526Sxy150489 	e1000g_m_multicst,
2438275SEric Cheng 	NULL,
2443526Sxy150489 	e1000g_m_tx,
2453526Sxy150489 	e1000g_m_ioctl,
2466394Scc210113 	e1000g_m_getcapab,
2476394Scc210113 	NULL,
2486394Scc210113 	NULL,
2496394Scc210113 	e1000g_m_setprop,
2506394Scc210113 	e1000g_m_getprop
2513526Sxy150489 };
2523526Sxy150489 
2533526Sxy150489 /*
2543526Sxy150489  * Global variables
2553526Sxy150489  */
2563526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2573526Sxy150489 /*
2584894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2594349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2604349Sxy150489  * enabled. If we force the driver to detach while there are still some
2614349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2624349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2634349Sxy150489  * structure will be freed after the driver is detached. However when we
2644349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2654349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2664894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2674894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2684894Syy150190  * after the system board level DR operation. For this reason, the global
2694894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2704349Sxy150489  */
2714894Syy150190 #ifdef __sparc
2724894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2734894Syy150190 #else
2744894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2754894Syy150190 #endif
2764349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2774894Syy150190 
2784349Sxy150489 /*
2798850SMin.Xu@Sun.COM  * The mutex e1000g_rx_detach_lock is defined to protect the processing of
2808850SMin.Xu@Sun.COM  * the private dev_info list, and to serialize the processing of rx buffer
2818850SMin.Xu@Sun.COM  * freeing and rx buffer recycling.
2823526Sxy150489  */
2838850SMin.Xu@Sun.COM kmutex_t e1000g_rx_detach_lock;
2843526Sxy150489 /*
2853526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2863526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2873526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2883526Sxy150489  * resources during the initialization of the instances, then the flag will
2893526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2903526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2913526Sxy150489  */
2923526Sxy150489 krwlock_t e1000g_dma_type_lock;
2933526Sxy150489 
2947133Scc210113 /*
2957133Scc210113  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
2967133Scc210113  * Based on the information from Intel, the 82546 chipset has some hardware
2977133Scc210113  * problem. When one port is being reset and the other port is trying to
2987133Scc210113  * access the eeprom, it could cause system hang or panic. To workaround this
2997133Scc210113  * hardware problem, we use a global mutex to prevent such operations from
3007133Scc210113  * happening simultaneously on different instances. This workaround is applied
3017133Scc210113  * to all the devices supported by this driver.
3027133Scc210113  */
3037133Scc210113 kmutex_t e1000g_nvm_lock;
3043526Sxy150489 
3053526Sxy150489 /*
3063526Sxy150489  * Loadable module configuration entry points for the driver
3073526Sxy150489  */
3083526Sxy150489 
3093526Sxy150489 /*
3104919Sxy150489  * _init - module initialization
3113526Sxy150489  */
3123526Sxy150489 int
3133526Sxy150489 _init(void)
3143526Sxy150489 {
3153526Sxy150489 	int status;
3163526Sxy150489 
3173526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
3183526Sxy150489 	status = mod_install(&modlinkage);
3193526Sxy150489 	if (status != DDI_SUCCESS)
3203526Sxy150489 		mac_fini_ops(&ws_ops);
3213526Sxy150489 	else {
3228850SMin.Xu@Sun.COM 		mutex_init(&e1000g_rx_detach_lock, NULL, MUTEX_DRIVER, NULL);
3233526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
3247133Scc210113 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
3253526Sxy150489 	}
3263526Sxy150489 
3273526Sxy150489 	return (status);
3283526Sxy150489 }
3293526Sxy150489 
3303526Sxy150489 /*
3314919Sxy150489  * _fini - module finalization
3323526Sxy150489  */
3333526Sxy150489 int
3343526Sxy150489 _fini(void)
3353526Sxy150489 {
3363526Sxy150489 	int status;
3373526Sxy150489 
3388850SMin.Xu@Sun.COM 	if (e1000g_mblks_pending != 0)
3393526Sxy150489 		return (EBUSY);
3403526Sxy150489 
3413526Sxy150489 	status = mod_remove(&modlinkage);
3423526Sxy150489 	if (status == DDI_SUCCESS) {
3433526Sxy150489 		mac_fini_ops(&ws_ops);
3444349Sxy150489 
3454349Sxy150489 		if (e1000g_force_detach) {
3464349Sxy150489 			private_devi_list_t *devi_node;
3474349Sxy150489 
3488850SMin.Xu@Sun.COM 			mutex_enter(&e1000g_rx_detach_lock);
3494349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3504349Sxy150489 				devi_node = e1000g_private_devi_list;
3514349Sxy150489 				e1000g_private_devi_list =
3524349Sxy150489 				    e1000g_private_devi_list->next;
3534349Sxy150489 
3544349Sxy150489 				kmem_free(devi_node->priv_dip,
3554349Sxy150489 				    sizeof (struct dev_info));
3564349Sxy150489 				kmem_free(devi_node,
3574349Sxy150489 				    sizeof (private_devi_list_t));
3584349Sxy150489 			}
3598850SMin.Xu@Sun.COM 			mutex_exit(&e1000g_rx_detach_lock);
3604349Sxy150489 		}
3614349Sxy150489 
3628850SMin.Xu@Sun.COM 		mutex_destroy(&e1000g_rx_detach_lock);
3633526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3647133Scc210113 		mutex_destroy(&e1000g_nvm_lock);
3653526Sxy150489 	}
3663526Sxy150489 
3673526Sxy150489 	return (status);
3683526Sxy150489 }
3693526Sxy150489 
3703526Sxy150489 /*
3714919Sxy150489  * _info - module information
3723526Sxy150489  */
3733526Sxy150489 int
3743526Sxy150489 _info(struct modinfo *modinfop)
3753526Sxy150489 {
3763526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3773526Sxy150489 }
3783526Sxy150489 
3793526Sxy150489 /*
3804919Sxy150489  * e1000g_attach - driver attach
3814919Sxy150489  *
3824919Sxy150489  * This function is the device-specific initialization entry
3834919Sxy150489  * point. This entry point is required and must be written.
3844919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3854919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3864919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3874919Sxy150489  * available for use by the driver.
3884919Sxy150489  *
3894919Sxy150489  * The attach() function will be called once for each instance
3904919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
3914919Sxy150489  * Until attach() succeeds, the only driver entry points which
3924919Sxy150489  * may be called are open(9E) and getinfo(9E).
3933526Sxy150489  */
3943526Sxy150489 static int
3954919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
3963526Sxy150489 {
3973526Sxy150489 	struct e1000g *Adapter;
3983526Sxy150489 	struct e1000_hw *hw;
3994919Sxy150489 	struct e1000g_osdep *osdep;
4003526Sxy150489 	int instance;
4013526Sxy150489 
4023526Sxy150489 	switch (cmd) {
4033526Sxy150489 	default:
4043526Sxy150489 		e1000g_log(NULL, CE_WARN,
4054919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
4063526Sxy150489 		return (DDI_FAILURE);
4073526Sxy150489 
4083526Sxy150489 	case DDI_RESUME:
4093526Sxy150489 		return (e1000g_resume(devinfo));
4103526Sxy150489 
4113526Sxy150489 	case DDI_ATTACH:
4123526Sxy150489 		break;
4133526Sxy150489 	}
4143526Sxy150489 
4153526Sxy150489 	/*
4163526Sxy150489 	 * get device instance number
4173526Sxy150489 	 */
4183526Sxy150489 	instance = ddi_get_instance(devinfo);
4193526Sxy150489 
4203526Sxy150489 	/*
4213526Sxy150489 	 * Allocate soft data structure
4223526Sxy150489 	 */
4233526Sxy150489 	Adapter =
4243526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4253526Sxy150489 
4263526Sxy150489 	Adapter->dip = devinfo;
4274919Sxy150489 	Adapter->instance = instance;
4283526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4293526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4303526Sxy150489 
4314919Sxy150489 	hw = &Adapter->shared;
4324919Sxy150489 	osdep = &Adapter->osdep;
4334919Sxy150489 	hw->back = osdep;
4344919Sxy150489 	osdep->adapter = Adapter;
4354919Sxy150489 
4363526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4373526Sxy150489 
4384919Sxy150489 	/*
4395273Sgl147354 	 * Initialize for fma support
4405273Sgl147354 	 */
4415273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4425273Sgl147354 	    0, 0x0f,
4435273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4445273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4455273Sgl147354 	e1000g_fm_init(Adapter);
4465273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4475273Sgl147354 
4485273Sgl147354 	/*
4494919Sxy150489 	 * PCI Configure
4504919Sxy150489 	 */
4514919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4524919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4534919Sxy150489 		goto attach_fail;
4544919Sxy150489 	}
4554919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4564919Sxy150489 
4574919Sxy150489 	/*
4584919Sxy150489 	 * Setup hardware
4594919Sxy150489 	 */
4604919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4614919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4624919Sxy150489 		goto attach_fail;
4634919Sxy150489 	}
4643526Sxy150489 
4653526Sxy150489 	/*
4663526Sxy150489 	 * Map in the device registers.
4673526Sxy150489 	 */
4684919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4694919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4703526Sxy150489 		goto attach_fail;
4713526Sxy150489 	}
4724919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4733526Sxy150489 
4743526Sxy150489 	/*
4753526Sxy150489 	 * Initialize driver parameters
4763526Sxy150489 	 */
4773526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4783526Sxy150489 		goto attach_fail;
4793526Sxy150489 	}
4804919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4813526Sxy150489 
4825273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4835273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4845273Sgl147354 		goto attach_fail;
4855273Sgl147354 	}
4865273Sgl147354 
4873526Sxy150489 	/*
4883526Sxy150489 	 * Initialize interrupts
4893526Sxy150489 	 */
4903526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
4913526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
4923526Sxy150489 		goto attach_fail;
4933526Sxy150489 	}
4944919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
4953526Sxy150489 
4963526Sxy150489 	/*
4973526Sxy150489 	 * Initialize mutex's for this device.
4983526Sxy150489 	 * Do this before enabling the interrupt handler and
4993526Sxy150489 	 * register the softint to avoid the condition where
5003526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5013526Sxy150489 	 */
5023526Sxy150489 	e1000g_init_locks(Adapter);
5033526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5043526Sxy150489 
5053526Sxy150489 	/*
5063526Sxy150489 	 * Initialize Driver Counters
5073526Sxy150489 	 */
5084919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
5093526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5103526Sxy150489 		goto attach_fail;
5113526Sxy150489 	}
5123526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5133526Sxy150489 
5143526Sxy150489 	/*
5153526Sxy150489 	 * Initialize chip hardware and software structures
5163526Sxy150489 	 */
5178479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
5183526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5198479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
5203526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5213526Sxy150489 		goto attach_fail;
5223526Sxy150489 	}
5238479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
5243526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5253526Sxy150489 
5263526Sxy150489 	/*
5273526Sxy150489 	 * Register the driver to the MAC
5283526Sxy150489 	 */
5293526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5303526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5313526Sxy150489 		goto attach_fail;
5323526Sxy150489 	}
5334919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5343526Sxy150489 
5353526Sxy150489 	/*
5363526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5373526Sxy150489 	 * initialized, enable interrupts.
5383526Sxy150489 	 */
5393526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5403526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5413526Sxy150489 		goto attach_fail;
5423526Sxy150489 	}
5434919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5443526Sxy150489 
5454982Syy150190 	/*
5464982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5474982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5484982Syy150190 	 * supports after driver detached.
5494982Syy150190 	 */
5504982Syy150190 	if (e1000g_force_detach) {
5514982Syy150190 		private_devi_list_t *devi_node;
5524982Syy150190 
5534982Syy150190 		Adapter->priv_dip =
5544982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5554982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5564982Syy150190 		    sizeof (struct dev_info));
5574982Syy150190 
5584982Syy150190 		devi_node =
5594982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5604982Syy150190 
5618850SMin.Xu@Sun.COM 		mutex_enter(&e1000g_rx_detach_lock);
5624982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5634982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5648850SMin.Xu@Sun.COM 		devi_node->pending_rx_count = 0;
5658850SMin.Xu@Sun.COM 
5668850SMin.Xu@Sun.COM 		Adapter->priv_devi_node = devi_node;
5678850SMin.Xu@Sun.COM 
5688850SMin.Xu@Sun.COM 		if (e1000g_private_devi_list == NULL) {
5698850SMin.Xu@Sun.COM 			devi_node->prev = NULL;
5708850SMin.Xu@Sun.COM 			devi_node->next = NULL;
5718850SMin.Xu@Sun.COM 			e1000g_private_devi_list = devi_node;
5728850SMin.Xu@Sun.COM 		} else {
5738850SMin.Xu@Sun.COM 			devi_node->prev = NULL;
5748850SMin.Xu@Sun.COM 			devi_node->next = e1000g_private_devi_list;
5758850SMin.Xu@Sun.COM 			e1000g_private_devi_list->prev = devi_node;
5768850SMin.Xu@Sun.COM 			e1000g_private_devi_list = devi_node;
5778850SMin.Xu@Sun.COM 		}
5788850SMin.Xu@Sun.COM 		mutex_exit(&e1000g_rx_detach_lock);
5794982Syy150190 	}
5804982Syy150190 
5813526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5828479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state = E1000G_INITIALIZED;
5833526Sxy150489 
5843526Sxy150489 	return (DDI_SUCCESS);
5853526Sxy150489 
5863526Sxy150489 attach_fail:
5873526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5883526Sxy150489 	return (DDI_FAILURE);
5893526Sxy150489 }
5903526Sxy150489 
5913526Sxy150489 static int
5923526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5933526Sxy150489 {
5944919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5953526Sxy150489 	mac_register_t *mac;
5963526Sxy150489 	int err;
5973526Sxy150489 
5983526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5993526Sxy150489 		return (DDI_FAILURE);
6004919Sxy150489 
6013526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
6023526Sxy150489 	mac->m_driver = Adapter;
6033526Sxy150489 	mac->m_dip = Adapter->dip;
6044919Sxy150489 	mac->m_src_addr = hw->mac.addr;
6053526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
6063526Sxy150489 	mac->m_min_sdu = 0;
6076394Scc210113 	mac->m_max_sdu = Adapter->default_mtu;
6085895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6096512Ssowmini 	mac->m_priv_props = e1000g_priv_props;
6106512Ssowmini 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
6118275SEric Cheng 	mac->m_v12n = MAC_VIRT_LEVEL1;
6124919Sxy150489 
6133526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6143526Sxy150489 	mac_free(mac);
6154919Sxy150489 
6163526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6173526Sxy150489 }
6183526Sxy150489 
6193526Sxy150489 static int
6204919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
6214919Sxy150489 {
6224919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6234919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6244919Sxy150489 
6254919Sxy150489 	/* Get the device id */
6264919Sxy150489 	hw->vendor_id =
6274919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6284919Sxy150489 	hw->device_id =
6294919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6304919Sxy150489 	hw->revision_id =
6314919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6324919Sxy150489 	hw->subsystem_device_id =
6334919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6344919Sxy150489 	hw->subsystem_vendor_id =
6354919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6364919Sxy150489 
6374919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6384919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6394919Sxy150489 		    "MAC type could not be set properly.");
6404919Sxy150489 		return (DDI_FAILURE);
6414919Sxy150489 	}
6424919Sxy150489 
6434919Sxy150489 	return (DDI_SUCCESS);
6444919Sxy150489 }
6454919Sxy150489 
6464919Sxy150489 static int
6474919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6484919Sxy150489 {
6494919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6504919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6514919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6524919Sxy150489 	off_t mem_size;
6534919Sxy150489 
6547607STed.You@Sun.COM 	/* Get size of adapter register memory */
6557607STed.You@Sun.COM 	if (ddi_dev_regsize(devinfo, ADAPTER_REG_SET, &mem_size) !=
6567607STed.You@Sun.COM 	    DDI_SUCCESS) {
6574919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6584919Sxy150489 		    "ddi_dev_regsize for registers failed");
6594919Sxy150489 		return (DDI_FAILURE);
6604919Sxy150489 	}
6614919Sxy150489 
6627607STed.You@Sun.COM 	/* Map adapter register memory */
6637607STed.You@Sun.COM 	if ((ddi_regs_map_setup(devinfo, ADAPTER_REG_SET,
6644919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6654919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6664919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6674919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6684919Sxy150489 		goto regs_map_fail;
6694919Sxy150489 	}
6704919Sxy150489 
6714919Sxy150489 	/* ICH needs to map flash memory */
6727607STed.You@Sun.COM 	if (hw->mac.type == e1000_ich8lan ||
6737607STed.You@Sun.COM 	    hw->mac.type == e1000_ich9lan ||
674*10680SMin.Xu@Sun.COM 	    hw->mac.type == e1000_ich10lan ||
675*10680SMin.Xu@Sun.COM 	    hw->mac.type == e1000_pchlan) {
6764919Sxy150489 		/* get flash size */
6774919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6784919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6794919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6804919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6814919Sxy150489 			goto regs_map_fail;
6824919Sxy150489 		}
6834919Sxy150489 
6844919Sxy150489 		/* map flash in */
6854919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6864919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6874919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6884919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6894919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6904919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6914919Sxy150489 			goto regs_map_fail;
6924919Sxy150489 		}
6934919Sxy150489 	}
6944919Sxy150489 
6954919Sxy150489 	return (DDI_SUCCESS);
6964919Sxy150489 
6974919Sxy150489 regs_map_fail:
6984919Sxy150489 	if (osdep->reg_handle != NULL)
6994919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
7004919Sxy150489 
7014919Sxy150489 	return (DDI_FAILURE);
7024919Sxy150489 }
7034919Sxy150489 
7044919Sxy150489 static int
7053526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
7063526Sxy150489 {
7073526Sxy150489 	struct e1000_hw *hw;
7084919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
7093526Sxy150489 
7104919Sxy150489 	hw = &Adapter->shared;
7114919Sxy150489 
7124919Sxy150489 	/* Set MAC type and initialize hardware functions */
7134919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7144919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7154919Sxy150489 		    "Could not setup hardware functions");
7163526Sxy150489 		return (DDI_FAILURE);
7173526Sxy150489 	}
7183526Sxy150489 
7194919Sxy150489 	/* Get bus information */
7204919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7214919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7224919Sxy150489 		    "Could not get bus information");
7234919Sxy150489 		return (DDI_FAILURE);
7243526Sxy150489 	}
7253526Sxy150489 
7263526Sxy150489 	/* get mem_base addr */
7274919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7284919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7293526Sxy150489 
7303526Sxy150489 	/* get io_base addr */
7314919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7324919Sxy150489 		if (bar64) {
7333526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7344919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7354919Sxy150489 			    PCI_CONF_BASE4);
7363526Sxy150489 		} else {
7373526Sxy150489 			/* normal 32-bit BAR mode */
7384919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7394919Sxy150489 			    PCI_CONF_BASE2);
7403526Sxy150489 		}
7413526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7423526Sxy150489 	} else {
7433526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7443526Sxy150489 		hw->io_base = 0x0;
7453526Sxy150489 	}
7463526Sxy150489 
7474919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7484919Sxy150489 
7494919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7504919Sxy150489 
7516735Scc210113 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
7526735Scc210113 	hw->phy.autoneg_wait_to_complete = B_FALSE;
7533526Sxy150489 
7543526Sxy150489 	/* Adaptive IFS related changes */
7554919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7564919Sxy150489 
7574919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7584919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7594919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7604919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7614919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7624919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7634919Sxy150489 
7644919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7654919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7663526Sxy150489 
7674608Syy150190 #ifdef __sparc
7684608Syy150190 	Adapter->strip_crc = B_TRUE;
7694608Syy150190 #else
7704608Syy150190 	Adapter->strip_crc = B_FALSE;
7714608Syy150190 #endif
7724608Syy150190 
773*10680SMin.Xu@Sun.COM 	/* setup the maximum MTU size of the chip */
774*10680SMin.Xu@Sun.COM 	e1000g_setup_max_mtu(Adapter);
775*10680SMin.Xu@Sun.COM 
7763526Sxy150489 	/* Get conf file properties */
7774919Sxy150489 	e1000g_get_conf(Adapter);
7784919Sxy150489 
7794919Sxy150489 	/* Get speed/duplex settings in conf file */
7804919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7814919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7823526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7833526Sxy150489 
7844919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7853526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7863526Sxy150489 
787*10680SMin.Xu@Sun.COM 	/* enforce PCH limits */
788*10680SMin.Xu@Sun.COM 	e1000g_pch_limits(Adapter);
789*10680SMin.Xu@Sun.COM 
7903526Sxy150489 	/* Set Rx/Tx buffer size */
7916394Scc210113 	e1000g_set_bufsize(Adapter);
7924919Sxy150489 
7934919Sxy150489 	/* Master Latency Timer */
7944919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
7954919Sxy150489 
7963526Sxy150489 	/* copper options */
7976735Scc210113 	if (hw->phy.media_type == e1000_media_type_copper) {
7984919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
7994919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
8004919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
8013526Sxy150489 	}
8023526Sxy150489 
8034919Sxy150489 	/* The initial link state should be "unknown" */
8044061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
8054061Sxy150489 
8065882Syy150190 	/* Initialize rx parameters */
8075882Syy150190 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
8085882Syy150190 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
8095882Syy150190 
8104919Sxy150489 	/* Initialize tx parameters */
8114919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
8124919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
8135882Syy150190 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
8145882Syy150190 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
8154919Sxy150489 
8164919Sxy150489 	/* Initialize rx parameters */
8174919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8184919Sxy150489 
8193526Sxy150489 	return (DDI_SUCCESS);
8203526Sxy150489 }
8213526Sxy150489 
8226394Scc210113 static void
823*10680SMin.Xu@Sun.COM e1000g_setup_max_mtu(struct e1000g *Adapter)
824*10680SMin.Xu@Sun.COM {
825*10680SMin.Xu@Sun.COM 	struct e1000_mac_info *mac = &Adapter->shared.mac;
826*10680SMin.Xu@Sun.COM 	struct e1000_phy_info *phy = &Adapter->shared.phy;
827*10680SMin.Xu@Sun.COM 
828*10680SMin.Xu@Sun.COM 	switch (mac->type) {
829*10680SMin.Xu@Sun.COM 	/* types that do not support jumbo frames */
830*10680SMin.Xu@Sun.COM 	case e1000_ich8lan:
831*10680SMin.Xu@Sun.COM 	case e1000_82573:
832*10680SMin.Xu@Sun.COM 	case e1000_82583:
833*10680SMin.Xu@Sun.COM 		Adapter->max_mtu = ETHERMTU;
834*10680SMin.Xu@Sun.COM 		break;
835*10680SMin.Xu@Sun.COM 	/* ich9 supports jumbo frames except on one phy type */
836*10680SMin.Xu@Sun.COM 	case e1000_ich9lan:
837*10680SMin.Xu@Sun.COM 		if (phy->type == e1000_phy_ife)
838*10680SMin.Xu@Sun.COM 			Adapter->max_mtu = ETHERMTU;
839*10680SMin.Xu@Sun.COM 		else
840*10680SMin.Xu@Sun.COM 			Adapter->max_mtu = MAXIMUM_MTU_9K;
841*10680SMin.Xu@Sun.COM 		break;
842*10680SMin.Xu@Sun.COM 	/* pch can do jumbo frames up to 4K */
843*10680SMin.Xu@Sun.COM 	case e1000_pchlan:
844*10680SMin.Xu@Sun.COM 		Adapter->max_mtu = MAXIMUM_MTU_4K;
845*10680SMin.Xu@Sun.COM 		break;
846*10680SMin.Xu@Sun.COM 	/* types with a special limit */
847*10680SMin.Xu@Sun.COM 	case e1000_82571:
848*10680SMin.Xu@Sun.COM 	case e1000_82572:
849*10680SMin.Xu@Sun.COM 	case e1000_82574:
850*10680SMin.Xu@Sun.COM 	case e1000_80003es2lan:
851*10680SMin.Xu@Sun.COM 	case e1000_ich10lan:
852*10680SMin.Xu@Sun.COM 		Adapter->max_mtu = MAXIMUM_MTU_9K;
853*10680SMin.Xu@Sun.COM 		break;
854*10680SMin.Xu@Sun.COM 	/* default limit is 16K */
855*10680SMin.Xu@Sun.COM 	default:
856*10680SMin.Xu@Sun.COM 		Adapter->max_mtu = FRAME_SIZE_UPTO_16K -
857*10680SMin.Xu@Sun.COM 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
858*10680SMin.Xu@Sun.COM 		    E1000G_IPALIGNPRESERVEROOM;
859*10680SMin.Xu@Sun.COM 		break;
860*10680SMin.Xu@Sun.COM 	}
861*10680SMin.Xu@Sun.COM }
862*10680SMin.Xu@Sun.COM 
863*10680SMin.Xu@Sun.COM static void
8646394Scc210113 e1000g_set_bufsize(struct e1000g *Adapter)
8656394Scc210113 {
8666394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
8676394Scc210113 	uint64_t rx_size;
8686394Scc210113 	uint64_t tx_size;
8696394Scc210113 
8708073SMin.Xu@Sun.COM 	dev_info_t *devinfo = Adapter->dip;
8716394Scc210113 #ifdef __sparc
8726394Scc210113 	ulong_t iommu_pagesize;
8738073SMin.Xu@Sun.COM #endif
8746394Scc210113 	/* Get the system page size */
8756394Scc210113 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
8768073SMin.Xu@Sun.COM 
8778073SMin.Xu@Sun.COM #ifdef __sparc
8786394Scc210113 	iommu_pagesize = dvma_pagesize(devinfo);
8796394Scc210113 	if (iommu_pagesize != 0) {
8806394Scc210113 		if (Adapter->sys_page_sz == iommu_pagesize) {
8816394Scc210113 			if (iommu_pagesize > 0x4000)
8826394Scc210113 				Adapter->sys_page_sz = 0x4000;
8836394Scc210113 		} else {
8846394Scc210113 			if (Adapter->sys_page_sz > iommu_pagesize)
8856394Scc210113 				Adapter->sys_page_sz = iommu_pagesize;
8866394Scc210113 		}
8876394Scc210113 	}
8886986Smx205022 	if (Adapter->lso_enable) {
8896986Smx205022 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
8906986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8916986Smx205022 	} else {
8926986Smx205022 		Adapter->dvma_page_num = Adapter->max_frame_size /
8936986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8946986Smx205022 	}
8956394Scc210113 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
8966394Scc210113 #endif
8976394Scc210113 
8986735Scc210113 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
8996735Scc210113 
9008417SChenlu.Chen@Sun.COM 	if (Adapter->mem_workaround_82546 &&
9018417SChenlu.Chen@Sun.COM 	    ((mac->type == e1000_82545) ||
9028178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546) ||
9038417SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546_rev_3))) {
9046394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
9058178SChenlu.Chen@Sun.COM 	} else {
9068178SChenlu.Chen@Sun.COM 		rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
9078178SChenlu.Chen@Sun.COM 		if ((rx_size > FRAME_SIZE_UPTO_2K) &&
9088178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_4K))
9098178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
9108178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
9118178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_8K))
9128178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
9138178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
9148178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_16K))
9158178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
9168178SChenlu.Chen@Sun.COM 		else
9178178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
9188178SChenlu.Chen@Sun.COM 	}
9196394Scc210113 
9206735Scc210113 	tx_size = Adapter->max_frame_size;
9216394Scc210113 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
9226394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
9236394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
9246394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_8K))
9256394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
9266394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
9276394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_16K))
9286394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
9296394Scc210113 	else
9306394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
9316394Scc210113 
9326394Scc210113 	/*
9336394Scc210113 	 * For Wiseman adapters we have an requirement of having receive
9346394Scc210113 	 * buffers aligned at 256 byte boundary. Since Livengood does not
9356394Scc210113 	 * require this and forcing it for all hardwares will have
9366394Scc210113 	 * performance implications, I am making it applicable only for
9376394Scc210113 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
9386394Scc210113 	 * it is okay to have normal frames...but it does involve a
9396394Scc210113 	 * potential risk where we may loose data if buffer is not
9406394Scc210113 	 * aligned...so all wiseman boards to have 256 byte aligned
9416394Scc210113 	 * buffers
9426394Scc210113 	 */
9436394Scc210113 	if (mac->type < e1000_82543)
9446394Scc210113 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
9456394Scc210113 	else
9466394Scc210113 		Adapter->rx_buf_align = 1;
9476394Scc210113 }
9486394Scc210113 
9493526Sxy150489 /*
9504919Sxy150489  * e1000g_detach - driver detach
9514919Sxy150489  *
9524919Sxy150489  * The detach() function is the complement of the attach routine.
9534919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
9544919Sxy150489  * state  associated  with  a  given  instance of a device node
9554919Sxy150489  * prior to the removal of that instance from the system.
9564919Sxy150489  *
9574919Sxy150489  * The detach() function will be called once for each  instance
9584919Sxy150489  * of the device for which there has been a successful attach()
9594919Sxy150489  * once there are no longer  any  opens  on  the  device.
9604919Sxy150489  *
9614919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
9624919Sxy150489  * driver are freed.
9633526Sxy150489  */
9643526Sxy150489 static int
9654919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
9663526Sxy150489 {
9673526Sxy150489 	struct e1000g *Adapter;
9684982Syy150190 	boolean_t rx_drain;
9693526Sxy150489 
9703526Sxy150489 	switch (cmd) {
9713526Sxy150489 	default:
9723526Sxy150489 		return (DDI_FAILURE);
9733526Sxy150489 
9743526Sxy150489 	case DDI_SUSPEND:
9753526Sxy150489 		return (e1000g_suspend(devinfo));
9763526Sxy150489 
9773526Sxy150489 	case DDI_DETACH:
9783526Sxy150489 		break;
9793526Sxy150489 	}
9803526Sxy150489 
9813526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
9823526Sxy150489 	if (Adapter == NULL)
9833526Sxy150489 		return (DDI_FAILURE);
9843526Sxy150489 
9858275SEric Cheng 	rx_drain = e1000g_rx_drain(Adapter);
9868275SEric Cheng 	if (!rx_drain && !e1000g_force_detach)
9878275SEric Cheng 		return (DDI_FAILURE);
9888275SEric Cheng 
9894919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9904919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
9914919Sxy150489 		return (DDI_FAILURE);
9924919Sxy150489 	}
9934919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
9944919Sxy150489 
9958479SChenlu.Chen@Sun.COM 	ASSERT(!(Adapter->e1000g_state & E1000G_STARTED));
9964982Syy150190 
9978850SMin.Xu@Sun.COM 	if (!e1000g_force_detach && !rx_drain)
9988850SMin.Xu@Sun.COM 		return (DDI_FAILURE);
9993526Sxy150489 
10003526Sxy150489 	e1000g_unattach(devinfo, Adapter);
10013526Sxy150489 
10023526Sxy150489 	return (DDI_SUCCESS);
10033526Sxy150489 }
10043526Sxy150489 
10054982Syy150190 /*
10064982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
10074982Syy150190  */
10088850SMin.Xu@Sun.COM void
10098850SMin.Xu@Sun.COM e1000g_free_priv_devi_node(private_devi_list_t *devi_node)
10104982Syy150190 {
10114982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
10128850SMin.Xu@Sun.COM 	ASSERT(devi_node != NULL);
10138850SMin.Xu@Sun.COM 
10148850SMin.Xu@Sun.COM 	if (devi_node->prev != NULL)
10158850SMin.Xu@Sun.COM 		devi_node->prev->next = devi_node->next;
10168850SMin.Xu@Sun.COM 	if (devi_node->next != NULL)
10178850SMin.Xu@Sun.COM 		devi_node->next->prev = devi_node->prev;
10188850SMin.Xu@Sun.COM 	if (devi_node == e1000g_private_devi_list)
10198850SMin.Xu@Sun.COM 		e1000g_private_devi_list = devi_node->next;
10208850SMin.Xu@Sun.COM 
10218850SMin.Xu@Sun.COM 	kmem_free(devi_node->priv_dip,
10228850SMin.Xu@Sun.COM 	    sizeof (struct dev_info));
10238850SMin.Xu@Sun.COM 	kmem_free(devi_node,
10248850SMin.Xu@Sun.COM 	    sizeof (private_devi_list_t));
10254982Syy150190 }
10264982Syy150190 
10273526Sxy150489 static void
10283526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
10293526Sxy150489 {
10308850SMin.Xu@Sun.COM 	private_devi_list_t *devi_node;
10317133Scc210113 	int result;
10327133Scc210113 
10334919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
10343526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
10353526Sxy150489 	}
10363526Sxy150489 
10374919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
10383526Sxy150489 		(void) mac_unregister(Adapter->mh);
10393526Sxy150489 	}
10403526Sxy150489 
10414919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
10423526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
10433526Sxy150489 	}
10443526Sxy150489 
10454919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10463526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10473526Sxy150489 	}
10483526Sxy150489 
10493526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10503526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10513526Sxy150489 	}
10523526Sxy150489 
10533526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10544919Sxy150489 		stop_link_timer(Adapter);
10557133Scc210113 
10567133Scc210113 		mutex_enter(&e1000g_nvm_lock);
10577133Scc210113 		result = e1000_reset_hw(&Adapter->shared);
10587133Scc210113 		mutex_exit(&e1000g_nvm_lock);
10597133Scc210113 
10607133Scc210113 		if (result != E1000_SUCCESS) {
10615273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10625273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10635273Sgl147354 		}
10643526Sxy150489 	}
10653526Sxy150489 
10669770SChangqing.Li@Sun.COM 	e1000g_release_multicast(Adapter);
10679770SChangqing.Li@Sun.COM 
10684919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10694919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10704919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10714919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10724919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10733526Sxy150489 	}
10743526Sxy150489 
10754919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10764919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10774919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10783526Sxy150489 	}
10793526Sxy150489 
10803526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10813526Sxy150489 		e1000g_destroy_locks(Adapter);
10823526Sxy150489 	}
10833526Sxy150489 
10845273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10855273Sgl147354 		e1000g_fm_fini(Adapter);
10865273Sgl147354 	}
10875273Sgl147354 
10888850SMin.Xu@Sun.COM 	mutex_enter(&e1000g_rx_detach_lock);
10899190SMin.Xu@Sun.COM 	if (e1000g_force_detach && (Adapter->priv_devi_node != NULL)) {
10908850SMin.Xu@Sun.COM 		devi_node = Adapter->priv_devi_node;
10918850SMin.Xu@Sun.COM 		devi_node->flag |= E1000G_PRIV_DEVI_DETACH;
10928850SMin.Xu@Sun.COM 
10938850SMin.Xu@Sun.COM 		if (devi_node->pending_rx_count == 0) {
10948850SMin.Xu@Sun.COM 			e1000g_free_priv_devi_node(devi_node);
10958850SMin.Xu@Sun.COM 		}
10968850SMin.Xu@Sun.COM 	}
10978850SMin.Xu@Sun.COM 	mutex_exit(&e1000g_rx_detach_lock);
10988850SMin.Xu@Sun.COM 
10993526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
11003526Sxy150489 
11013526Sxy150489 	/*
11023526Sxy150489 	 * Another hotplug spec requirement,
11033526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
11043526Sxy150489 	 */
11053526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
11063526Sxy150489 }
11073526Sxy150489 
11083526Sxy150489 static void
11093526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
11103526Sxy150489 {
11113526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11123526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11133526Sxy150489 
11143526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
11153526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11164919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
11173526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11184919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
11193526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11203526Sxy150489 
11213526Sxy150489 	tx_ring = Adapter->tx_ring;
11223526Sxy150489 
11233526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
11243526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11253526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
11263526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11273526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
11283526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11293526Sxy150489 
11303526Sxy150489 	rx_ring = Adapter->rx_ring;
11313526Sxy150489 
11327436STed.You@Sun.COM 	mutex_init(&rx_ring->rx_lock, NULL,
11337436STed.You@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11343526Sxy150489 }
11353526Sxy150489 
11363526Sxy150489 static void
11373526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
11383526Sxy150489 {
11393526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11403526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11413526Sxy150489 
11423526Sxy150489 	tx_ring = Adapter->tx_ring;
11433526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
11443526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
11453526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
11463526Sxy150489 
11473526Sxy150489 	rx_ring = Adapter->rx_ring;
11487436STed.You@Sun.COM 	mutex_destroy(&rx_ring->rx_lock);
11493526Sxy150489 
11504919Sxy150489 	mutex_destroy(&Adapter->link_lock);
11514919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
11523526Sxy150489 	rw_destroy(&Adapter->chip_lock);
1153*10680SMin.Xu@Sun.COM 
1154*10680SMin.Xu@Sun.COM 	/* destory mutex initialized in shared code */
1155*10680SMin.Xu@Sun.COM 	e1000_destroy_hw_mutex(&Adapter->shared);
11563526Sxy150489 }
11573526Sxy150489 
11583526Sxy150489 static int
11593526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11603526Sxy150489 {
11613526Sxy150489 	struct e1000g *Adapter;
11623526Sxy150489 
11633526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11643526Sxy150489 	if (Adapter == NULL)
11658479SChenlu.Chen@Sun.COM 		e1000g_log(Adapter, CE_PANIC,
11668479SChenlu.Chen@Sun.COM 		    "Instance pointer is null\n");
11678479SChenlu.Chen@Sun.COM 
11688479SChenlu.Chen@Sun.COM 	if (Adapter->dip != devinfo)
11698479SChenlu.Chen@Sun.COM 		e1000g_log(Adapter, CE_PANIC,
11708479SChenlu.Chen@Sun.COM 		    "Devinfo is not the same as saved devinfo\n");
11718479SChenlu.Chen@Sun.COM 
11728479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11738479SChenlu.Chen@Sun.COM 
11748479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_STARTED) {
11758479SChenlu.Chen@Sun.COM 		if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
11768479SChenlu.Chen@Sun.COM 			rw_exit(&Adapter->chip_lock);
11778479SChenlu.Chen@Sun.COM 			/*
11788479SChenlu.Chen@Sun.COM 			 * We note the failure, but return success, as the
11798479SChenlu.Chen@Sun.COM 			 * system is still usable without this controller.
11808479SChenlu.Chen@Sun.COM 			 */
11818479SChenlu.Chen@Sun.COM 			e1000g_log(Adapter, CE_WARN,
11828479SChenlu.Chen@Sun.COM 			    "e1000g_resume: failed to restart controller\n");
11838479SChenlu.Chen@Sun.COM 			return (DDI_SUCCESS);
11848479SChenlu.Chen@Sun.COM 		}
11858479SChenlu.Chen@Sun.COM 		/* Enable and start the watchdog timer */
11868479SChenlu.Chen@Sun.COM 		enable_watchdog_timer(Adapter);
11878479SChenlu.Chen@Sun.COM 	}
11888479SChenlu.Chen@Sun.COM 
11898479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state &= ~E1000G_SUSPENDED;
11908479SChenlu.Chen@Sun.COM 
11918479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
11923526Sxy150489 
11933526Sxy150489 	return (DDI_SUCCESS);
11943526Sxy150489 }
11953526Sxy150489 
11963526Sxy150489 static int
11973526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11983526Sxy150489 {
11993526Sxy150489 	struct e1000g *Adapter;
12003526Sxy150489 
12013526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
12023526Sxy150489 	if (Adapter == NULL)
12033526Sxy150489 		return (DDI_FAILURE);
12043526Sxy150489 
12058479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
12068479SChenlu.Chen@Sun.COM 
12078479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state |= E1000G_SUSPENDED;
12088479SChenlu.Chen@Sun.COM 
12098479SChenlu.Chen@Sun.COM 	/* if the port isn't plumbed, we can simply return */
12108479SChenlu.Chen@Sun.COM 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
12118479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
12128479SChenlu.Chen@Sun.COM 		return (DDI_SUCCESS);
12138479SChenlu.Chen@Sun.COM 	}
12148479SChenlu.Chen@Sun.COM 
12158479SChenlu.Chen@Sun.COM 	e1000g_stop(Adapter, B_FALSE);
12168479SChenlu.Chen@Sun.COM 
12178479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
12188479SChenlu.Chen@Sun.COM 
12198479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
12208479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
12218479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
12228479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
12233526Sxy150489 
12243526Sxy150489 	return (DDI_SUCCESS);
12253526Sxy150489 }
12263526Sxy150489 
12273526Sxy150489 static int
12283526Sxy150489 e1000g_init(struct e1000g *Adapter)
12293526Sxy150489 {
12303526Sxy150489 	uint32_t pba;
12314919Sxy150489 	uint32_t high_water;
12323526Sxy150489 	struct e1000_hw *hw;
12334061Sxy150489 	clock_t link_timeout;
12347133Scc210113 	int result;
12353526Sxy150489 
12364919Sxy150489 	hw = &Adapter->shared;
12373526Sxy150489 
12383526Sxy150489 	/*
12393526Sxy150489 	 * reset to put the hardware in a known state
12403526Sxy150489 	 * before we try to do anything with the eeprom
12413526Sxy150489 	 */
12427133Scc210113 	mutex_enter(&e1000g_nvm_lock);
12437133Scc210113 	result = e1000_reset_hw(hw);
12447133Scc210113 	mutex_exit(&e1000g_nvm_lock);
12457133Scc210113 
12467133Scc210113 	if (result != E1000_SUCCESS) {
12475273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12485273Sgl147354 		goto init_fail;
12495273Sgl147354 	}
12503526Sxy150489 
12517133Scc210113 	mutex_enter(&e1000g_nvm_lock);
12527133Scc210113 	result = e1000_validate_nvm_checksum(hw);
12537133Scc210113 	if (result < E1000_SUCCESS) {
12544061Sxy150489 		/*
12554061Sxy150489 		 * Some PCI-E parts fail the first check due to
12564061Sxy150489 		 * the link being in sleep state.  Call it again,
12574061Sxy150489 		 * if it fails a second time its a real issue.
12584061Sxy150489 		 */
12597133Scc210113 		result = e1000_validate_nvm_checksum(hw);
12607133Scc210113 	}
12617133Scc210113 	mutex_exit(&e1000g_nvm_lock);
12627133Scc210113 
12637133Scc210113 	if (result < E1000_SUCCESS) {
12647133Scc210113 		e1000g_log(Adapter, CE_WARN,
12657133Scc210113 		    "Invalid NVM checksum. Please contact "
12667133Scc210113 		    "the vendor to update the NVM.");
12677133Scc210113 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12687133Scc210113 		goto init_fail;
12697133Scc210113 	}
12707133Scc210113 
12717133Scc210113 	result = 0;
12723526Sxy150489 #ifdef __sparc
12733526Sxy150489 	/*
12747607STed.You@Sun.COM 	 * First, we try to get the local ethernet address from OBP. If
12757133Scc210113 	 * failed, then we get it from the EEPROM of NIC card.
12763526Sxy150489 	 */
12777133Scc210113 	result = e1000g_find_mac_address(Adapter);
12787133Scc210113 #endif
12793526Sxy150489 	/* Get the local ethernet address. */
12807133Scc210113 	if (!result) {
12817133Scc210113 		mutex_enter(&e1000g_nvm_lock);
12827140Scc210113 		result = e1000_read_mac_addr(hw);
12837133Scc210113 		mutex_exit(&e1000g_nvm_lock);
12847133Scc210113 	}
12857133Scc210113 
12867133Scc210113 	if (result < E1000_SUCCESS) {
12873526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
12885273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12893526Sxy150489 		goto init_fail;
12903526Sxy150489 	}
12913526Sxy150489 
12923526Sxy150489 	/* check for valid mac address */
12934919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12943526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
12955273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12963526Sxy150489 		goto init_fail;
12973526Sxy150489 	}
12983526Sxy150489 
12994919Sxy150489 	/* Set LAA state for 82571 chipset */
13004919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
13013526Sxy150489 
13023526Sxy150489 	/* Master Latency Timer implementation */
13034919Sxy150489 	if (Adapter->master_latency_timer) {
13044919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
13054919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
13063526Sxy150489 	}
13073526Sxy150489 
13084919Sxy150489 	if (hw->mac.type < e1000_82547) {
13093526Sxy150489 		/*
13103526Sxy150489 		 * Total FIFO is 64K
13113526Sxy150489 		 */
13126735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
13133526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
13143526Sxy150489 		else
13153526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
13167140Scc210113 	} else if ((hw->mac.type == e1000_82571) ||
13177140Scc210113 	    (hw->mac.type == e1000_82572) ||
13187140Scc210113 	    (hw->mac.type == e1000_80003es2lan)) {
13193526Sxy150489 		/*
13203526Sxy150489 		 * Total FIFO is 48K
13213526Sxy150489 		 */
13226735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
13233526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
13243526Sxy150489 		else
13253526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
13267607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82573) {
13277607STed.You@Sun.COM 		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
13287607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82574) {
13297607STed.You@Sun.COM 		/* Keep adapter default: 20K for Rx, 20K for Tx */
13307607STed.You@Sun.COM 		pba = E1000_READ_REG(hw, E1000_PBA);
13314919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
13323526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
13334919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
13347607STed.You@Sun.COM 		pba = E1000_PBA_10K;
13357607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_ich10lan) {
13367607STed.You@Sun.COM 		pba = E1000_PBA_10K;
1337*10680SMin.Xu@Sun.COM 	} else if (hw->mac.type == e1000_pchlan) {
1338*10680SMin.Xu@Sun.COM 		pba = E1000_PBA_26K;
13393526Sxy150489 	} else {
13403526Sxy150489 		/*
13413526Sxy150489 		 * Total FIFO is 40K
13423526Sxy150489 		 */
13436735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
13443526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
13453526Sxy150489 		else
13463526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
13473526Sxy150489 	}
13484919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
13493526Sxy150489 
13503526Sxy150489 	/*
13513526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
13523526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
13533526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
13543526Sxy150489 	 * file.
13553526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
13563526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
13573526Sxy150489 	 * high-water mark.
13584919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
13594919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
13604919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
13614919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
13624919Sxy150489 	 * Rx FIFO size minus one full frame.
13633526Sxy150489 	 */
13644919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
13658479SChenlu.Chen@Sun.COM 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_82574 ||
13668479SChenlu.Chen@Sun.COM 	    hw->mac.type == e1000_ich9lan || hw->mac.type == e1000_ich10lan) ?
13674919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
13686735Scc210113 	    ((pba << 10) - Adapter->max_frame_size)));
13696735Scc210113 
13706735Scc210113 	hw->fc.high_water = high_water & 0xFFF8;
13716735Scc210113 	hw->fc.low_water = hw->fc.high_water - 8;
13724919Sxy150489 
13734919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
13746735Scc210113 		hw->fc.pause_time = 0xFFFF;
13754919Sxy150489 	else
13766735Scc210113 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
13776735Scc210113 	hw->fc.send_xon = B_TRUE;
13783526Sxy150489 
13793526Sxy150489 	/*
13803526Sxy150489 	 * Reset the adapter hardware the second time.
13813526Sxy150489 	 */
13827133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13837133Scc210113 	result = e1000_reset_hw(hw);
13847133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13857133Scc210113 
13867133Scc210113 	if (result != E1000_SUCCESS) {
13875273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13885273Sgl147354 		goto init_fail;
13895273Sgl147354 	}
13903526Sxy150489 
13913526Sxy150489 	/* disable wakeup control by default */
13924919Sxy150489 	if (hw->mac.type >= e1000_82544)
13934919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
13943526Sxy150489 
13958178SChenlu.Chen@Sun.COM 	/*
13968178SChenlu.Chen@Sun.COM 	 * MWI should be disabled on 82546.
13978178SChenlu.Chen@Sun.COM 	 */
13988178SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82546)
13998178SChenlu.Chen@Sun.COM 		e1000_pci_clear_mwi(hw);
14008178SChenlu.Chen@Sun.COM 	else
14018178SChenlu.Chen@Sun.COM 		e1000_pci_set_mwi(hw);
14023526Sxy150489 
14033526Sxy150489 	/*
14043526Sxy150489 	 * Configure/Initialize hardware
14053526Sxy150489 	 */
14067133Scc210113 	mutex_enter(&e1000g_nvm_lock);
14077133Scc210113 	result = e1000_init_hw(hw);
14087133Scc210113 	mutex_exit(&e1000g_nvm_lock);
14097133Scc210113 
14107133Scc210113 	if (result < E1000_SUCCESS) {
14113526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
14125273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
14133526Sxy150489 		goto init_fail;
14143526Sxy150489 	}
14153526Sxy150489 
14167436STed.You@Sun.COM 	/*
14177436STed.You@Sun.COM 	 * Restore LED settings to the default from EEPROM
14187436STed.You@Sun.COM 	 * to meet the standard for Sun platforms.
14197436STed.You@Sun.COM 	 */
142010605SMin.Xu@Sun.COM 	(void) e1000_cleanup_led(hw);
14217436STed.You@Sun.COM 
14223526Sxy150489 	/* Disable Smart Power Down */
14233526Sxy150489 	phy_spd_state(hw, B_FALSE);
14243526Sxy150489 
14255082Syy150190 	/* Make sure driver has control */
14265082Syy150190 	e1000g_get_driver_control(hw);
14275082Syy150190 
14283526Sxy150489 	/*
14293526Sxy150489 	 * Initialize unicast addresses.
14303526Sxy150489 	 */
14313526Sxy150489 	e1000g_init_unicst(Adapter);
14323526Sxy150489 
14333526Sxy150489 	/*
14343526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
14353526Sxy150489 	 * completes  Multicast table will be set
14363526Sxy150489 	 */
1437*10680SMin.Xu@Sun.COM 	e1000_update_mc_addr_list(hw,
1438*10680SMin.Xu@Sun.COM 	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);
14394919Sxy150489 	msec_delay(5);
14403526Sxy150489 
14413526Sxy150489 	/*
14423526Sxy150489 	 * Implement Adaptive IFS
14433526Sxy150489 	 */
14443526Sxy150489 	e1000_reset_adaptive(hw);
14453526Sxy150489 
14463526Sxy150489 	/* Setup Interrupt Throttling Register */
14475882Syy150190 	if (hw->mac.type >= e1000_82540) {
14485882Syy150190 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
14495882Syy150190 	} else
14505882Syy150190 		Adapter->intr_adaptive = B_FALSE;
14513526Sxy150489 
14524061Sxy150489 	/* Start the timer for link setup */
14534919Sxy150489 	if (hw->mac.autoneg)
14544919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
14554061Sxy150489 	else
14564919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
14574919Sxy150489 
14584919Sxy150489 	mutex_enter(&Adapter->link_lock);
14596735Scc210113 	if (hw->phy.autoneg_wait_to_complete) {
14604061Sxy150489 		Adapter->link_complete = B_TRUE;
14613526Sxy150489 	} else {
14624061Sxy150489 		Adapter->link_complete = B_FALSE;
14634061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
14644061Sxy150489 		    (void *)Adapter, link_timeout);
14653526Sxy150489 	}
14664919Sxy150489 	mutex_exit(&Adapter->link_lock);
14673526Sxy150489 
14685082Syy150190 	/* Save the state of the phy */
14695082Syy150190 	e1000g_get_phy_state(Adapter);
14705082Syy150190 
14716512Ssowmini 	e1000g_param_sync(Adapter);
14726512Ssowmini 
14733526Sxy150489 	Adapter->init_count++;
14743526Sxy150489 
14755273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
14765273Sgl147354 		goto init_fail;
14775273Sgl147354 	}
14785273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
14795273Sgl147354 		goto init_fail;
14805273Sgl147354 	}
14815273Sgl147354 
14828275SEric Cheng 	Adapter->poll_mode = e1000g_poll_mode;
14838275SEric Cheng 
14843526Sxy150489 	return (DDI_SUCCESS);
14853526Sxy150489 
14863526Sxy150489 init_fail:
14875273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
14883526Sxy150489 	return (DDI_FAILURE);
14893526Sxy150489 }
14903526Sxy150489 
14918850SMin.Xu@Sun.COM static int
14928850SMin.Xu@Sun.COM e1000g_alloc_rx_data(struct e1000g *Adapter)
14938850SMin.Xu@Sun.COM {
14948850SMin.Xu@Sun.COM 	e1000g_rx_ring_t *rx_ring;
14958850SMin.Xu@Sun.COM 	e1000g_rx_data_t *rx_data;
14968850SMin.Xu@Sun.COM 
14978850SMin.Xu@Sun.COM 	rx_ring = Adapter->rx_ring;
14988850SMin.Xu@Sun.COM 
14998850SMin.Xu@Sun.COM 	rx_data = kmem_zalloc(sizeof (e1000g_rx_data_t), KM_NOSLEEP);
15008850SMin.Xu@Sun.COM 
15018850SMin.Xu@Sun.COM 	if (rx_data == NULL)
15028850SMin.Xu@Sun.COM 		return (DDI_FAILURE);
15038850SMin.Xu@Sun.COM 
15048850SMin.Xu@Sun.COM 	rx_data->priv_devi_node = Adapter->priv_devi_node;
15058850SMin.Xu@Sun.COM 	rx_data->rx_ring = rx_ring;
15068850SMin.Xu@Sun.COM 
15078850SMin.Xu@Sun.COM 	mutex_init(&rx_data->freelist_lock, NULL,
15088850SMin.Xu@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
15098850SMin.Xu@Sun.COM 	mutex_init(&rx_data->recycle_lock, NULL,
15108850SMin.Xu@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
15118850SMin.Xu@Sun.COM 
15128850SMin.Xu@Sun.COM 	rx_ring->rx_data = rx_data;
15138850SMin.Xu@Sun.COM 
15148850SMin.Xu@Sun.COM 	return (DDI_SUCCESS);
15158850SMin.Xu@Sun.COM }
15168850SMin.Xu@Sun.COM 
15178850SMin.Xu@Sun.COM void
15188850SMin.Xu@Sun.COM e1000g_free_rx_pending_buffers(e1000g_rx_data_t *rx_data)
15198850SMin.Xu@Sun.COM {
15208850SMin.Xu@Sun.COM 	rx_sw_packet_t *packet, *next_packet;
15218850SMin.Xu@Sun.COM 
15228850SMin.Xu@Sun.COM 	if (rx_data == NULL)
15238850SMin.Xu@Sun.COM 		return;
15248850SMin.Xu@Sun.COM 
15258850SMin.Xu@Sun.COM 	packet = rx_data->packet_area;
15268850SMin.Xu@Sun.COM 	while (packet != NULL) {
15278850SMin.Xu@Sun.COM 		next_packet = packet->next;
15288850SMin.Xu@Sun.COM 		e1000g_free_rx_sw_packet(packet, B_TRUE);
15298850SMin.Xu@Sun.COM 		packet = next_packet;
15308850SMin.Xu@Sun.COM 	}
15318850SMin.Xu@Sun.COM 	rx_data->packet_area = NULL;
15328850SMin.Xu@Sun.COM }
15338850SMin.Xu@Sun.COM 
15348850SMin.Xu@Sun.COM void
15358850SMin.Xu@Sun.COM e1000g_free_rx_data(e1000g_rx_data_t *rx_data)
15368850SMin.Xu@Sun.COM {
15378850SMin.Xu@Sun.COM 	if (rx_data == NULL)
15388850SMin.Xu@Sun.COM 		return;
15398850SMin.Xu@Sun.COM 
15408850SMin.Xu@Sun.COM 	mutex_destroy(&rx_data->freelist_lock);
15418850SMin.Xu@Sun.COM 	mutex_destroy(&rx_data->recycle_lock);
15428850SMin.Xu@Sun.COM 
15438850SMin.Xu@Sun.COM 	kmem_free(rx_data, sizeof (e1000g_rx_data_t));
15448850SMin.Xu@Sun.COM }
15458850SMin.Xu@Sun.COM 
15463526Sxy150489 /*
15473526Sxy150489  * Check if the link is up
15483526Sxy150489  */
15493526Sxy150489 static boolean_t
15503526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
15513526Sxy150489 {
1552*10680SMin.Xu@Sun.COM 	struct e1000_hw *hw = &Adapter->shared;
1553*10680SMin.Xu@Sun.COM 	boolean_t link_up = B_FALSE;
1554*10680SMin.Xu@Sun.COM 
1555*10680SMin.Xu@Sun.COM 	/*
1556*10680SMin.Xu@Sun.COM 	 * get_link_status is set in the interrupt handler on link-status-change
1557*10680SMin.Xu@Sun.COM 	 * or rx sequence error interrupt.  get_link_status will stay
1558*10680SMin.Xu@Sun.COM 	 * false until the e1000_check_for_link establishes link only
1559*10680SMin.Xu@Sun.COM 	 * for copper adapters.
1560*10680SMin.Xu@Sun.COM 	 */
1561*10680SMin.Xu@Sun.COM 	switch (hw->phy.media_type) {
1562*10680SMin.Xu@Sun.COM 	case e1000_media_type_copper:
1563*10680SMin.Xu@Sun.COM 		if (hw->mac.get_link_status) {
1564*10680SMin.Xu@Sun.COM 			(void) e1000_check_for_link(hw);
1565*10680SMin.Xu@Sun.COM 			link_up = !hw->mac.get_link_status;
1566*10680SMin.Xu@Sun.COM 		} else {
1567*10680SMin.Xu@Sun.COM 			link_up = B_TRUE;
1568*10680SMin.Xu@Sun.COM 		}
1569*10680SMin.Xu@Sun.COM 		break;
1570*10680SMin.Xu@Sun.COM 	case e1000_media_type_fiber:
1571*10680SMin.Xu@Sun.COM 		(void) e1000_check_for_link(hw);
1572*10680SMin.Xu@Sun.COM 		link_up = (E1000_READ_REG(hw, E1000_STATUS) &
1573*10680SMin.Xu@Sun.COM 		    E1000_STATUS_LU);
1574*10680SMin.Xu@Sun.COM 		break;
1575*10680SMin.Xu@Sun.COM 	case e1000_media_type_internal_serdes:
1576*10680SMin.Xu@Sun.COM 		(void) e1000_check_for_link(hw);
1577*10680SMin.Xu@Sun.COM 		link_up = hw->mac.serdes_has_link;
1578*10680SMin.Xu@Sun.COM 		break;
15793526Sxy150489 	}
15803526Sxy150489 
15813526Sxy150489 	return (link_up);
15823526Sxy150489 }
15833526Sxy150489 
15843526Sxy150489 static void
15853526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
15863526Sxy150489 {
15873526Sxy150489 	struct iocblk *iocp;
15883526Sxy150489 	struct e1000g *e1000gp;
15893526Sxy150489 	enum ioc_reply status;
15907426SChenliang.Xu@Sun.COM 
15917426SChenliang.Xu@Sun.COM 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
15923526Sxy150489 	iocp->ioc_error = 0;
15933526Sxy150489 	e1000gp = (struct e1000g *)arg;
15943526Sxy150489 
15953526Sxy150489 	ASSERT(e1000gp);
15963526Sxy150489 	if (e1000gp == NULL) {
15973526Sxy150489 		miocnak(q, mp, 0, EINVAL);
15983526Sxy150489 		return;
15993526Sxy150489 	}
16003526Sxy150489 
16018479SChenlu.Chen@Sun.COM 	rw_enter(&e1000gp->chip_lock, RW_READER);
16028479SChenlu.Chen@Sun.COM 	if (e1000gp->e1000g_state & E1000G_SUSPENDED) {
16038479SChenlu.Chen@Sun.COM 		rw_exit(&e1000gp->chip_lock);
16048479SChenlu.Chen@Sun.COM 		miocnak(q, mp, 0, EINVAL);
16058479SChenlu.Chen@Sun.COM 		return;
16068479SChenlu.Chen@Sun.COM 	}
16078479SChenlu.Chen@Sun.COM 	rw_exit(&e1000gp->chip_lock);
16088479SChenlu.Chen@Sun.COM 
16093526Sxy150489 	switch (iocp->ioc_cmd) {
16103526Sxy150489 
16113526Sxy150489 	case LB_GET_INFO_SIZE:
16123526Sxy150489 	case LB_GET_INFO:
16133526Sxy150489 	case LB_GET_MODE:
16143526Sxy150489 	case LB_SET_MODE:
16153526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
16163526Sxy150489 		break;
16173526Sxy150489 
16183526Sxy150489 
16194919Sxy150489 #ifdef E1000G_DEBUG
16203526Sxy150489 	case E1000G_IOC_REG_PEEK:
16213526Sxy150489 	case E1000G_IOC_REG_POKE:
16223526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
16233526Sxy150489 		break;
16243526Sxy150489 	case E1000G_IOC_CHIP_RESET:
16253526Sxy150489 		e1000gp->reset_count++;
16267656SSherry.Moore@Sun.COM 		if (e1000g_reset_adapter(e1000gp))
16273526Sxy150489 			status = IOC_ACK;
16283526Sxy150489 		else
16293526Sxy150489 			status = IOC_INVAL;
16303526Sxy150489 		break;
16314919Sxy150489 #endif
16323526Sxy150489 	default:
16333526Sxy150489 		status = IOC_INVAL;
16343526Sxy150489 		break;
16353526Sxy150489 	}
16363526Sxy150489 
16373526Sxy150489 	/*
16383526Sxy150489 	 * Decide how to reply
16393526Sxy150489 	 */
16403526Sxy150489 	switch (status) {
16413526Sxy150489 	default:
16423526Sxy150489 	case IOC_INVAL:
16433526Sxy150489 		/*
16443526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
16453526Sxy150489 		 */
16463526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
16474349Sxy150489 		    EINVAL : iocp->ioc_error);
16483526Sxy150489 		break;
16493526Sxy150489 
16503526Sxy150489 	case IOC_DONE:
16513526Sxy150489 		/*
16523526Sxy150489 		 * OK, reply already sent
16533526Sxy150489 		 */
16543526Sxy150489 		break;
16553526Sxy150489 
16563526Sxy150489 	case IOC_ACK:
16573526Sxy150489 		/*
16583526Sxy150489 		 * OK, reply with an ACK
16593526Sxy150489 		 */
16603526Sxy150489 		miocack(q, mp, 0, 0);
16613526Sxy150489 		break;
16623526Sxy150489 
16633526Sxy150489 	case IOC_REPLY:
16643526Sxy150489 		/*
16653526Sxy150489 		 * OK, send prepared reply as ACK or NAK
16663526Sxy150489 		 */
16673526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
16684349Sxy150489 		    M_IOCACK : M_IOCNAK;
16693526Sxy150489 		qreply(q, mp);
16703526Sxy150489 		break;
16713526Sxy150489 	}
16723526Sxy150489 }
16733526Sxy150489 
16748275SEric Cheng /*
16758275SEric Cheng  * The default value of e1000g_poll_mode == 0 assumes that the NIC is
16768275SEric Cheng  * capable of supporting only one interrupt and we shouldn't disable
16778275SEric Cheng  * the physical interrupt. In this case we let the interrupt come and
16788275SEric Cheng  * we queue the packets in the rx ring itself in case we are in polling
16798275SEric Cheng  * mode (better latency but slightly lower performance and a very
16808275SEric Cheng  * high intrrupt count in mpstat which is harmless).
16818275SEric Cheng  *
16828275SEric Cheng  * e1000g_poll_mode == 1 assumes that we have per Rx ring interrupt
16838275SEric Cheng  * which can be disabled in poll mode. This gives better overall
16848275SEric Cheng  * throughput (compared to the mode above), shows very low interrupt
16858275SEric Cheng  * count but has slightly higher latency since we pick the packets when
16868275SEric Cheng  * the poll thread does polling.
16878275SEric Cheng  *
16888275SEric Cheng  * Currently, this flag should be enabled only while doing performance
16898275SEric Cheng  * measurement or when it can be guaranteed that entire NIC going
16908275SEric Cheng  * in poll mode will not harm any traffic like cluster heartbeat etc.
16918275SEric Cheng  */
16928275SEric Cheng int e1000g_poll_mode = 0;
16938275SEric Cheng 
16948275SEric Cheng /*
16958275SEric Cheng  * Called from the upper layers when driver is in polling mode to
16968275SEric Cheng  * pick up any queued packets. Care should be taken to not block
16978275SEric Cheng  * this thread.
16988275SEric Cheng  */
16998275SEric Cheng static mblk_t *e1000g_poll_ring(void *arg, int bytes_to_pickup)
17008275SEric Cheng {
17018275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)arg;
17028275SEric Cheng 	mblk_t			*mp = NULL;
17038275SEric Cheng 	mblk_t			*tail;
17048275SEric Cheng 	struct e1000g 		*adapter;
17058275SEric Cheng 
17068275SEric Cheng 	adapter = rx_ring->adapter;
17078275SEric Cheng 
17088479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
17098479SChenlu.Chen@Sun.COM 
17108479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
17118479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
17128479SChenlu.Chen@Sun.COM 		return (NULL);
17138479SChenlu.Chen@Sun.COM 	}
17148479SChenlu.Chen@Sun.COM 
17158275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
17168833SVenu.Iyer@Sun.COM 	mp = e1000g_receive(rx_ring, &tail, bytes_to_pickup);
17178275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
17188479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
17198275SEric Cheng 	return (mp);
17208275SEric Cheng }
17218275SEric Cheng 
17223526Sxy150489 static int
17233526Sxy150489 e1000g_m_start(void *arg)
17243526Sxy150489 {
17253526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
17263526Sxy150489 
17278479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
17288479SChenlu.Chen@Sun.COM 
17298479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
17308479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
17318479SChenlu.Chen@Sun.COM 		return (ECANCELED);
17328479SChenlu.Chen@Sun.COM 	}
17338479SChenlu.Chen@Sun.COM 
17348479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
17358479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
17368479SChenlu.Chen@Sun.COM 		return (ENOTACTIVE);
17378479SChenlu.Chen@Sun.COM 	}
17388479SChenlu.Chen@Sun.COM 
17398479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state |= E1000G_STARTED;
17408479SChenlu.Chen@Sun.COM 
17418479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
17428479SChenlu.Chen@Sun.COM 
17438479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
17448479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
17458479SChenlu.Chen@Sun.COM 
17468479SChenlu.Chen@Sun.COM 	return (0);
17473526Sxy150489 }
17483526Sxy150489 
17493526Sxy150489 static int
17504919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
17513526Sxy150489 {
17528850SMin.Xu@Sun.COM 	e1000g_rx_data_t *rx_data;
17538850SMin.Xu@Sun.COM 
17544919Sxy150489 	if (global) {
17558850SMin.Xu@Sun.COM 		if (e1000g_alloc_rx_data(Adapter) != DDI_SUCCESS) {
17568850SMin.Xu@Sun.COM 			e1000g_log(Adapter, CE_WARN, "Allocate rx data failed");
17578850SMin.Xu@Sun.COM 			goto start_fail;
17588850SMin.Xu@Sun.COM 		}
17598850SMin.Xu@Sun.COM 
17604919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
17614919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
17624919Sxy150489 			e1000g_log(Adapter, CE_WARN,
17634919Sxy150489 			    "Alloc DMA resources failed");
17648850SMin.Xu@Sun.COM 			goto start_fail;
17654919Sxy150489 		}
17664919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
17674919Sxy150489 	}
17684919Sxy150489 
17693526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
17703526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
17713526Sxy150489 			e1000g_log(Adapter, CE_WARN,
17723526Sxy150489 			    "Adapter initialization failed");
17738850SMin.Xu@Sun.COM 			goto start_fail;
17743526Sxy150489 		}
17753526Sxy150489 	}
17763526Sxy150489 
17774919Sxy150489 	/* Setup and initialize the transmit structures */
17784919Sxy150489 	e1000g_tx_setup(Adapter);
17794919Sxy150489 	msec_delay(5);
17804919Sxy150489 
17814919Sxy150489 	/* Setup and initialize the receive structures */
17824919Sxy150489 	e1000g_rx_setup(Adapter);
17834919Sxy150489 	msec_delay(5);
17844919Sxy150489 
17857722SShuguo.Yang@Sun.COM 	/* Restore the e1000g promiscuous mode */
17867722SShuguo.Yang@Sun.COM 	e1000g_restore_promisc(Adapter);
17877722SShuguo.Yang@Sun.COM 
17884919Sxy150489 	e1000g_mask_interrupt(Adapter);
17893526Sxy150489 
17908479SChenlu.Chen@Sun.COM 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
17918479SChenlu.Chen@Sun.COM 
17925273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
17935273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
17948850SMin.Xu@Sun.COM 		goto start_fail;
17958479SChenlu.Chen@Sun.COM 	}
17968479SChenlu.Chen@Sun.COM 
17978479SChenlu.Chen@Sun.COM 	return (DDI_SUCCESS);
17988850SMin.Xu@Sun.COM 
17998850SMin.Xu@Sun.COM start_fail:
18008850SMin.Xu@Sun.COM 	rx_data = Adapter->rx_ring->rx_data;
18018850SMin.Xu@Sun.COM 
18028850SMin.Xu@Sun.COM 	if (global) {
18038850SMin.Xu@Sun.COM 		e1000g_release_dma_resources(Adapter);
18048850SMin.Xu@Sun.COM 		e1000g_free_rx_pending_buffers(rx_data);
18058850SMin.Xu@Sun.COM 		e1000g_free_rx_data(rx_data);
18068850SMin.Xu@Sun.COM 	}
18078850SMin.Xu@Sun.COM 
18088850SMin.Xu@Sun.COM 	mutex_enter(&e1000g_nvm_lock);
18098850SMin.Xu@Sun.COM 	(void) e1000_reset_hw(&Adapter->shared);
18108850SMin.Xu@Sun.COM 	mutex_exit(&e1000g_nvm_lock);
18118850SMin.Xu@Sun.COM 
18128850SMin.Xu@Sun.COM 	return (DDI_FAILURE);
18133526Sxy150489 }
18143526Sxy150489 
18153526Sxy150489 static void
18163526Sxy150489 e1000g_m_stop(void *arg)
18173526Sxy150489 {
18183526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
18193526Sxy150489 
18208479SChenlu.Chen@Sun.COM 	/* Drain tx sessions */
18218479SChenlu.Chen@Sun.COM 	(void) e1000g_tx_drain(Adapter);
18228479SChenlu.Chen@Sun.COM 
18238479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
18248479SChenlu.Chen@Sun.COM 
18258479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
18268479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
18278479SChenlu.Chen@Sun.COM 		return;
18288479SChenlu.Chen@Sun.COM 	}
18298479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state &= ~E1000G_STARTED;
18304919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
18318479SChenlu.Chen@Sun.COM 
18328479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
18338479SChenlu.Chen@Sun.COM 
18348479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
18358479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
18368479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
18378479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
18383526Sxy150489 }
18393526Sxy150489 
18403526Sxy150489 static void
18414919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
18423526Sxy150489 {
18438850SMin.Xu@Sun.COM 	private_devi_list_t *devi_node;
18448850SMin.Xu@Sun.COM 	e1000g_rx_data_t *rx_data;
18457133Scc210113 	int result;
18467133Scc210113 
18473526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
18483526Sxy150489 
18493526Sxy150489 	/* Stop the chip and release pending resources */
18503526Sxy150489 
18517607STed.You@Sun.COM 	/* Tell firmware driver is no longer in control */
18527607STed.You@Sun.COM 	e1000g_release_driver_control(&Adapter->shared);
18537607STed.You@Sun.COM 
18544919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
18557133Scc210113 
18567133Scc210113 	mutex_enter(&e1000g_nvm_lock);
18577133Scc210113 	result = e1000_reset_hw(&Adapter->shared);
18587133Scc210113 	mutex_exit(&e1000g_nvm_lock);
18597133Scc210113 
18607133Scc210113 	if (result != E1000_SUCCESS) {
18615273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
18625273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
18635273Sgl147354 	}
18643526Sxy150489 
18653526Sxy150489 	/* Release resources still held by the TX descriptors */
18664919Sxy150489 	e1000g_tx_clean(Adapter);
18674061Sxy150489 
18685273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18695273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
18705273Sgl147354 
18714061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
18724919Sxy150489 	e1000g_rx_clean(Adapter);
18734061Sxy150489 
18748850SMin.Xu@Sun.COM 	if (global) {
18754919Sxy150489 		e1000g_release_dma_resources(Adapter);
18768850SMin.Xu@Sun.COM 
18778850SMin.Xu@Sun.COM 		mutex_enter(&e1000g_rx_detach_lock);
18788850SMin.Xu@Sun.COM 		rx_data = Adapter->rx_ring->rx_data;
18798850SMin.Xu@Sun.COM 		rx_data->flag |= E1000G_RX_STOPPED;
18808850SMin.Xu@Sun.COM 
18818850SMin.Xu@Sun.COM 		if (rx_data->pending_count == 0) {
18828850SMin.Xu@Sun.COM 			e1000g_free_rx_pending_buffers(rx_data);
18838850SMin.Xu@Sun.COM 			e1000g_free_rx_data(rx_data);
18848850SMin.Xu@Sun.COM 		} else {
18858850SMin.Xu@Sun.COM 			devi_node = rx_data->priv_devi_node;
18868850SMin.Xu@Sun.COM 			if (devi_node != NULL)
18878850SMin.Xu@Sun.COM 				atomic_inc_32(&devi_node->pending_rx_count);
18888850SMin.Xu@Sun.COM 			else
18898850SMin.Xu@Sun.COM 				atomic_inc_32(&Adapter->pending_rx_count);
18908850SMin.Xu@Sun.COM 		}
18918850SMin.Xu@Sun.COM 		mutex_exit(&e1000g_rx_detach_lock);
18928850SMin.Xu@Sun.COM 	}
18939190SMin.Xu@Sun.COM 
18949190SMin.Xu@Sun.COM 	if (Adapter->link_state == LINK_STATE_UP) {
18959190SMin.Xu@Sun.COM 		Adapter->link_state = LINK_STATE_UNKNOWN;
18969190SMin.Xu@Sun.COM 		mac_link_update(Adapter->mh, Adapter->link_state);
18979190SMin.Xu@Sun.COM 	}
18984061Sxy150489 }
18994061Sxy150489 
19004061Sxy150489 static void
19014919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
19024919Sxy150489 {
19038850SMin.Xu@Sun.COM 	e1000g_rx_data_t *rx_data = Adapter->rx_ring->rx_data;
19048850SMin.Xu@Sun.COM 
19058850SMin.Xu@Sun.COM 	if (rx_data == NULL)
19068850SMin.Xu@Sun.COM 		return;
19078850SMin.Xu@Sun.COM 
19088850SMin.Xu@Sun.COM 	if (rx_data->rx_mblk != NULL) {
19098850SMin.Xu@Sun.COM 		freemsg(rx_data->rx_mblk);
19108850SMin.Xu@Sun.COM 		rx_data->rx_mblk = NULL;
19118850SMin.Xu@Sun.COM 		rx_data->rx_mblk_tail = NULL;
19128850SMin.Xu@Sun.COM 		rx_data->rx_mblk_len = 0;
19134919Sxy150489 	}
19144919Sxy150489 }
19154919Sxy150489 
19164919Sxy150489 static void
19174919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
19184061Sxy150489 {
19194061Sxy150489 	e1000g_tx_ring_t *tx_ring;
19204919Sxy150489 	p_tx_sw_packet_t packet;
19214061Sxy150489 	mblk_t *mp;
19224061Sxy150489 	mblk_t *nmp;
19234061Sxy150489 	uint32_t packet_count;
19244061Sxy150489 
19254061Sxy150489 	tx_ring = Adapter->tx_ring;
19264061Sxy150489 
19273526Sxy150489 	/*
19283526Sxy150489 	 * Here we don't need to protect the lists using
19293526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
19303526Sxy150489 	 * have been protected by the chip_lock.
19313526Sxy150489 	 */
19323526Sxy150489 	mp = NULL;
19333526Sxy150489 	nmp = NULL;
19344061Sxy150489 	packet_count = 0;
19354919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
19363526Sxy150489 	while (packet != NULL) {
19373526Sxy150489 		if (packet->mp != NULL) {
19383526Sxy150489 			/* Assemble the message chain */
19393526Sxy150489 			if (mp == NULL) {
19403526Sxy150489 				mp = packet->mp;
19413526Sxy150489 				nmp = packet->mp;
19423526Sxy150489 			} else {
19433526Sxy150489 				nmp->b_next = packet->mp;
19443526Sxy150489 				nmp = packet->mp;
19453526Sxy150489 			}
19463526Sxy150489 			/* Disconnect the message from the sw packet */
19473526Sxy150489 			packet->mp = NULL;
19483526Sxy150489 		}
19493526Sxy150489 
19504919Sxy150489 		e1000g_free_tx_swpkt(packet);
19514061Sxy150489 		packet_count++;
19523526Sxy150489 
19534919Sxy150489 		packet = (p_tx_sw_packet_t)
19543526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
19553526Sxy150489 	}
19563526Sxy150489 
19575882Syy150190 	if (mp != NULL)
19585882Syy150190 		freemsgchain(mp);
19594061Sxy150489 
19604061Sxy150489 	if (packet_count > 0) {
19614061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
19624061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
19634061Sxy150489 
19644061Sxy150489 		/* Setup TX descriptor pointers */
19654061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
19664061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
19674061Sxy150489 
19684061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
19696735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
19706735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
19713526Sxy150489 	}
19723526Sxy150489 }
19733526Sxy150489 
19743526Sxy150489 static boolean_t
19753526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
19763526Sxy150489 {
19773526Sxy150489 	int i;
19783526Sxy150489 	boolean_t done;
19793526Sxy150489 	e1000g_tx_ring_t *tx_ring;
19803526Sxy150489 
19813526Sxy150489 	tx_ring = Adapter->tx_ring;
19823526Sxy150489 
19833526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
19844919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
19853526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
19863526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
19873526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
19883526Sxy150489 
19893526Sxy150489 		if (done)
19903526Sxy150489 			break;
19913526Sxy150489 
19923526Sxy150489 		msec_delay(1);
19933526Sxy150489 	}
19943526Sxy150489 
19953526Sxy150489 	return (done);
19963526Sxy150489 }
19973526Sxy150489 
19983526Sxy150489 static boolean_t
19993526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
20003526Sxy150489 {
20018850SMin.Xu@Sun.COM 	int i;
20023526Sxy150489 	boolean_t done;
20033526Sxy150489 
20048850SMin.Xu@Sun.COM 	/*
20058850SMin.Xu@Sun.COM 	 * Allow up to RX_DRAIN_TIME for pending received packets to complete.
20068850SMin.Xu@Sun.COM 	 */
20078850SMin.Xu@Sun.COM 	for (i = 0; i < RX_DRAIN_TIME; i++) {
20088850SMin.Xu@Sun.COM 		done = (Adapter->pending_rx_count == 0);
20098850SMin.Xu@Sun.COM 
20108850SMin.Xu@Sun.COM 		if (done)
20118850SMin.Xu@Sun.COM 			break;
20128850SMin.Xu@Sun.COM 
20138850SMin.Xu@Sun.COM 		msec_delay(1);
20148850SMin.Xu@Sun.COM 	}
20153526Sxy150489 
20163526Sxy150489 	return (done);
20173526Sxy150489 }
20183526Sxy150489 
20197656SSherry.Moore@Sun.COM static boolean_t
20207656SSherry.Moore@Sun.COM e1000g_reset_adapter(struct e1000g *Adapter)
20213526Sxy150489 {
20228479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
20238479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
20248479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
20258479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
20268479SChenlu.Chen@Sun.COM 
20278479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
20288479SChenlu.Chen@Sun.COM 
202910434SGuoqing.Zhu@Sun.COM 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
203010434SGuoqing.Zhu@Sun.COM 		rw_exit(&Adapter->chip_lock);
203110434SGuoqing.Zhu@Sun.COM 		return (B_TRUE);
203210434SGuoqing.Zhu@Sun.COM 	}
203310434SGuoqing.Zhu@Sun.COM 
20344919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
20354919Sxy150489 
20368479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
20378479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
20383526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
20398479SChenlu.Chen@Sun.COM 			return (B_FALSE);
20408479SChenlu.Chen@Sun.COM 	}
20418479SChenlu.Chen@Sun.COM 
20428479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
20438479SChenlu.Chen@Sun.COM 
20448479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
20458479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
20463526Sxy150489 
20473526Sxy150489 	return (B_TRUE);
20483526Sxy150489 }
20493526Sxy150489 
20505273Sgl147354 boolean_t
20515273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
20525273Sgl147354 {
20538479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
20548479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
20558479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
20568479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
20578479SChenlu.Chen@Sun.COM 
20588479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
20598479SChenlu.Chen@Sun.COM 
20605273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
20615273Sgl147354 
20625273Sgl147354 	Adapter->init_count = 0;
20635273Sgl147354 
20648479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
20658479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
20665273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
20675273Sgl147354 		return (B_FALSE);
20685273Sgl147354 	}
20695273Sgl147354 
20708479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
20718479SChenlu.Chen@Sun.COM 
20728479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
20738479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
20748479SChenlu.Chen@Sun.COM 
20755273Sgl147354 	return (B_TRUE);
20765273Sgl147354 }
20775273Sgl147354 
20783526Sxy150489 /*
20794919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
20804919Sxy150489  *
20814919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
20824919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
20834919Sxy150489  * bit is set.
20843526Sxy150489  */
20853526Sxy150489 static uint_t
20863526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
20873526Sxy150489 {
20883526Sxy150489 	struct e1000g *Adapter;
20894919Sxy150489 	uint32_t icr;
20903526Sxy150489 
20917426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
20924919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
20934919Sxy150489 
20945273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
20955273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
20965273Sgl147354 
20974919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
20983526Sxy150489 		/*
20993526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
21003526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
21013526Sxy150489 		 * look for work to do.
21023526Sxy150489 		 */
21034919Sxy150489 		e1000g_intr_work(Adapter, icr);
21043526Sxy150489 		return (DDI_INTR_CLAIMED);
21053526Sxy150489 	} else {
21063526Sxy150489 		/*
21073526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
21083526Sxy150489 		 * Don't claim this interrupt, return immediately.
21093526Sxy150489 		 */
21103526Sxy150489 		return (DDI_INTR_UNCLAIMED);
21113526Sxy150489 	}
21123526Sxy150489 }
21133526Sxy150489 
21143526Sxy150489 /*
21154919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
21164919Sxy150489  *
21174919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
21184919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
21194919Sxy150489  * bit is set or not.
21203526Sxy150489  */
21213526Sxy150489 static uint_t
21223526Sxy150489 e1000g_intr(caddr_t arg)
21233526Sxy150489 {
21243526Sxy150489 	struct e1000g *Adapter;
21254919Sxy150489 	uint32_t icr;
21263526Sxy150489 
21277426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
21284919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
21294919Sxy150489 
21305273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
21315273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21325273Sgl147354 
21334919Sxy150489 	if (icr) {
21343526Sxy150489 		/*
21353526Sxy150489 		 * Any bit was set in ICR:
21363526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
21373526Sxy150489 		 * look for work to do.
21383526Sxy150489 		 */
21394919Sxy150489 		e1000g_intr_work(Adapter, icr);
21403526Sxy150489 		return (DDI_INTR_CLAIMED);
21413526Sxy150489 	} else {
21423526Sxy150489 		/*
21433526Sxy150489 		 * No bit was set in ICR:
21443526Sxy150489 		 * Don't claim this interrupt, return immediately.
21453526Sxy150489 		 */
21463526Sxy150489 		return (DDI_INTR_UNCLAIMED);
21473526Sxy150489 	}
21483526Sxy150489 }
21493526Sxy150489 
21503526Sxy150489 /*
21514919Sxy150489  * e1000g_intr_work - actual processing of ISR
21524919Sxy150489  *
21534919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
21544919Sxy150489  * processing routines.
21553526Sxy150489  */
21563526Sxy150489 static void
21574919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
21583526Sxy150489 {
21595882Syy150190 	struct e1000_hw *hw;
21605882Syy150190 	hw = &Adapter->shared;
21615882Syy150190 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
21625882Syy150190 
21635882Syy150190 	Adapter->rx_pkt_cnt = 0;
21645882Syy150190 	Adapter->tx_pkt_cnt = 0;
21655882Syy150190 
21664919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
21678479SChenlu.Chen@Sun.COM 
21688479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
21698479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
21708479SChenlu.Chen@Sun.COM 		return;
21718479SChenlu.Chen@Sun.COM 	}
21724919Sxy150489 	/*
21738479SChenlu.Chen@Sun.COM 	 * Here we need to check the "e1000g_state" flag within the chip_lock to
21744919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
21754919Sxy150489 	 * being reset.
21764919Sxy150489 	 */
21778479SChenlu.Chen@Sun.COM 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
21784919Sxy150489 		rw_exit(&Adapter->chip_lock);
21794919Sxy150489 		return;
21804919Sxy150489 	}
21814919Sxy150489 
21824919Sxy150489 	if (icr & E1000_ICR_RXT0) {
21838833SVenu.Iyer@Sun.COM 		mblk_t			*mp = NULL;
21848833SVenu.Iyer@Sun.COM 		mblk_t			*tail = NULL;
21858275SEric Cheng 		e1000g_rx_ring_t	*rx_ring;
21868275SEric Cheng 
21878275SEric Cheng 		rx_ring = Adapter->rx_ring;
21888275SEric Cheng 		mutex_enter(&rx_ring->rx_lock);
21898275SEric Cheng 		/*
21908833SVenu.Iyer@Sun.COM 		 * Sometimes with legacy interrupts, it possible that
21918833SVenu.Iyer@Sun.COM 		 * there is a single interrupt for Rx/Tx. In which
21928833SVenu.Iyer@Sun.COM 		 * case, if poll flag is set, we shouldn't really
21938833SVenu.Iyer@Sun.COM 		 * be doing Rx processing.
21948275SEric Cheng 		 */
21958833SVenu.Iyer@Sun.COM 		if (!rx_ring->poll_flag)
21968833SVenu.Iyer@Sun.COM 			mp = e1000g_receive(rx_ring, &tail,
21978833SVenu.Iyer@Sun.COM 			    E1000G_CHAIN_NO_LIMIT);
21988833SVenu.Iyer@Sun.COM 		mutex_exit(&rx_ring->rx_lock);
21994919Sxy150489 		rw_exit(&Adapter->chip_lock);
22008833SVenu.Iyer@Sun.COM 		if (mp != NULL)
22018275SEric Cheng 			mac_rx_ring(Adapter->mh, rx_ring->mrh,
22028833SVenu.Iyer@Sun.COM 			    mp, rx_ring->ring_gen_num);
22034919Sxy150489 	} else
22044919Sxy150489 		rw_exit(&Adapter->chip_lock);
22053526Sxy150489 
22065882Syy150190 	if (icr & E1000_ICR_TXDW) {
22075882Syy150190 		if (!Adapter->tx_intr_enable)
22085882Syy150190 			e1000g_clear_tx_interrupt(Adapter);
22095882Syy150190 
22105882Syy150190 		/* Recycle the tx descriptors */
22115882Syy150190 		rw_enter(&Adapter->chip_lock, RW_READER);
22127426SChenliang.Xu@Sun.COM 		(void) e1000g_recycle(tx_ring);
22135882Syy150190 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
22145882Syy150190 		rw_exit(&Adapter->chip_lock);
22155882Syy150190 
22165882Syy150190 		if (tx_ring->resched_needed &&
22175882Syy150190 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
22185882Syy150190 			tx_ring->resched_needed = B_FALSE;
22195882Syy150190 			mac_tx_update(Adapter->mh);
22205882Syy150190 			E1000G_STAT(tx_ring->stat_reschedule);
22215882Syy150190 		}
22225882Syy150190 	}
22235882Syy150190 
22243526Sxy150489 	/*
22253526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
22263526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
22273526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
22283526Sxy150489 	 * are an indication that cable is not connected.
22293526Sxy150489 	 */
22304919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
22314919Sxy150489 	    (icr & E1000_ICR_LSC) ||
22324919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
22334061Sxy150489 		boolean_t link_changed;
22344061Sxy150489 		timeout_id_t tid = 0;
22353526Sxy150489 
22364919Sxy150489 		stop_watchdog_timer(Adapter);
22374919Sxy150489 
22385082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
22395082Syy150190 
22405082Syy150190 		/*
22415082Syy150190 		 * Because we got a link-status-change interrupt, force
22425082Syy150190 		 * e1000_check_for_link() to look at phy
22435082Syy150190 		 */
22445082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
22455082Syy150190 
22464061Sxy150489 		/* e1000g_link_check takes care of link status change */
22474061Sxy150489 		link_changed = e1000g_link_check(Adapter);
22485082Syy150190 
22495082Syy150190 		/* Get new phy state */
22505082Syy150190 		e1000g_get_phy_state(Adapter);
22515082Syy150190 
22524061Sxy150489 		/*
22534061Sxy150489 		 * If the link timer has not timed out, we'll not notify
22544919Sxy150489 		 * the upper layer with any link state until the link is up.
22554061Sxy150489 		 */
22564061Sxy150489 		if (link_changed && !Adapter->link_complete) {
22574061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
22585082Syy150190 				mutex_enter(&Adapter->link_lock);
22594061Sxy150489 				Adapter->link_complete = B_TRUE;
22604061Sxy150489 				tid = Adapter->link_tid;
22614061Sxy150489 				Adapter->link_tid = 0;
22625082Syy150190 				mutex_exit(&Adapter->link_lock);
22634061Sxy150489 			} else {
22644061Sxy150489 				link_changed = B_FALSE;
22654061Sxy150489 			}
22664061Sxy150489 		}
22675082Syy150190 		rw_exit(&Adapter->chip_lock);
22683526Sxy150489 
22694061Sxy150489 		if (link_changed) {
22704061Sxy150489 			if (tid != 0)
22714061Sxy150489 				(void) untimeout(tid);
22724061Sxy150489 
22734139Sxy150489 			/*
22744139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
22757133Scc210113 			 * down event. Stop receiver here and reset in watchdog.
22764139Sxy150489 			 */
22774139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
22787133Scc210113 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
22797133Scc210113 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
22807133Scc210113 				E1000_WRITE_REG(hw, E1000_RCTL,
22817133Scc210113 				    rctl & ~E1000_RCTL_EN);
22827133Scc210113 				e1000g_log(Adapter, CE_WARN,
22837133Scc210113 				    "ESB2 receiver disabled");
22847133Scc210113 				Adapter->esb2_workaround = B_TRUE;
22857133Scc210113 			}
22868850SMin.Xu@Sun.COM 			if (!Adapter->reset_flag)
22878850SMin.Xu@Sun.COM 				mac_link_update(Adapter->mh,
22888850SMin.Xu@Sun.COM 				    Adapter->link_state);
22898850SMin.Xu@Sun.COM 			if (Adapter->link_state == LINK_STATE_UP)
22908850SMin.Xu@Sun.COM 				Adapter->reset_flag = B_FALSE;
22913526Sxy150489 		}
22923526Sxy150489 
22934919Sxy150489 		start_watchdog_timer(Adapter);
22943526Sxy150489 	}
22955882Syy150190 }
22965882Syy150190 
22973526Sxy150489 static void
22983526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
22993526Sxy150489 {
23003526Sxy150489 	struct e1000_hw *hw;
23013526Sxy150489 	int slot;
23023526Sxy150489 
23034919Sxy150489 	hw = &Adapter->shared;
23043526Sxy150489 
23058275SEric Cheng 	if (Adapter->init_count == 0) {
23063526Sxy150489 		/* Initialize the multiple unicast addresses */
23073526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
23083526Sxy150489 
23098275SEric Cheng 		/* Workaround for an erratum of 82571 chipst */
23104919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
23114919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
23123526Sxy150489 			Adapter->unicst_total--;
23133526Sxy150489 
23148275SEric Cheng 		Adapter->unicst_avail = Adapter->unicst_total;
23158275SEric Cheng 
23168275SEric Cheng 		for (slot = 0; slot < Adapter->unicst_total; slot++) {
23178275SEric Cheng 			/* Clear both the flag and MAC address */
23188275SEric Cheng 			Adapter->unicst_addr[slot].reg.high = 0;
23198275SEric Cheng 			Adapter->unicst_addr[slot].reg.low = 0;
23208275SEric Cheng 		}
23213526Sxy150489 	} else {
23228275SEric Cheng 		/* Workaround for an erratum of 82571 chipst */
23234919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
23244919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
23254919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
23263526Sxy150489 
23273526Sxy150489 		/* Re-configure the RAR registers */
23288275SEric Cheng 		for (slot = 0; slot < Adapter->unicst_total; slot++)
23298275SEric Cheng 			if (Adapter->unicst_addr[slot].mac.set == 1)
23308275SEric Cheng 				e1000_rar_set(hw,
23318275SEric Cheng 				    Adapter->unicst_addr[slot].mac.addr, slot);
23323526Sxy150489 	}
23335273Sgl147354 
23345273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
23355273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23363526Sxy150489 }
23373526Sxy150489 
23383526Sxy150489 static int
23393526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
23408275SEric Cheng     int slot)
23413526Sxy150489 {
23423526Sxy150489 	struct e1000_hw *hw;
23433526Sxy150489 
23444919Sxy150489 	hw = &Adapter->shared;
23453526Sxy150489 
23463526Sxy150489 	/*
23473526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
23483526Sxy150489 	 * that requires the receiver to be in reset when any of the
23493526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
23503526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
23513526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
23523526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
23533526Sxy150489 	 * and work around any necessary HW errata.
23543526Sxy150489 	 */
23554919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23564919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
23573526Sxy150489 		e1000_pci_clear_mwi(hw);
23584919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
23594919Sxy150489 		msec_delay(5);
23603526Sxy150489 	}
23618275SEric Cheng 	if (mac_addr == NULL) {
23628275SEric Cheng 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, slot << 1, 0);
23638275SEric Cheng 		E1000_WRITE_FLUSH(hw);
23648275SEric Cheng 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, (slot << 1) + 1, 0);
23658275SEric Cheng 		E1000_WRITE_FLUSH(hw);
23668275SEric Cheng 		/* Clear both the flag and MAC address */
23678275SEric Cheng 		Adapter->unicst_addr[slot].reg.high = 0;
23688275SEric Cheng 		Adapter->unicst_addr[slot].reg.low = 0;
23698275SEric Cheng 	} else {
23708275SEric Cheng 		bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr,
23718275SEric Cheng 		    ETHERADDRL);
23728275SEric Cheng 		e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
23738275SEric Cheng 		Adapter->unicst_addr[slot].mac.set = 1;
23748275SEric Cheng 	}
23758275SEric Cheng 
23768275SEric Cheng 	/* Workaround for an erratum of 82571 chipst */
23773526Sxy150489 	if (slot == 0) {
23784919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
23794919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
23808275SEric Cheng 			if (mac_addr == NULL) {
23818275SEric Cheng 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
23828275SEric Cheng 				    slot << 1, 0);
23838275SEric Cheng 				E1000_WRITE_FLUSH(hw);
23848275SEric Cheng 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
23858275SEric Cheng 				    (slot << 1) + 1, 0);
23868275SEric Cheng 				E1000_WRITE_FLUSH(hw);
23878275SEric Cheng 			} else {
23888275SEric Cheng 				e1000_rar_set(hw, (uint8_t *)mac_addr,
23898275SEric Cheng 				    LAST_RAR_ENTRY);
23908275SEric Cheng 			}
23913526Sxy150489 	}
23923526Sxy150489 
23933526Sxy150489 	/*
23943526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
23953526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
23963526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
23973526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
23983526Sxy150489 	 */
23994919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24004919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24014919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
24024919Sxy150489 		msec_delay(1);
24034919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
24043526Sxy150489 			e1000_pci_set_mwi(hw);
24054919Sxy150489 		e1000g_rx_setup(Adapter);
24063526Sxy150489 	}
24073526Sxy150489 
24085273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
24095273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24105273Sgl147354 		return (EIO);
24115273Sgl147354 	}
24125273Sgl147354 
24133526Sxy150489 	return (0);
24143526Sxy150489 }
24153526Sxy150489 
24163526Sxy150489 static int
24173526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
24183526Sxy150489 {
24194919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
24209770SChangqing.Li@Sun.COM 	struct ether_addr *newtable;
24219770SChangqing.Li@Sun.COM 	size_t new_len;
24229770SChangqing.Li@Sun.COM 	size_t old_len;
24233526Sxy150489 	int res = 0;
24243526Sxy150489 
24253526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
24263526Sxy150489 		res = EINVAL;
24279770SChangqing.Li@Sun.COM 		e1000g_log(Adapter, CE_WARN, "Illegal multicast address");
24283526Sxy150489 		goto done;
24293526Sxy150489 	}
24303526Sxy150489 
24319770SChangqing.Li@Sun.COM 	if (Adapter->mcast_count >= Adapter->mcast_max_num) {
24323526Sxy150489 		res = ENOENT;
24339770SChangqing.Li@Sun.COM 		e1000g_log(Adapter, CE_WARN,
24349770SChangqing.Li@Sun.COM 		    "Adapter requested more than %d mcast addresses",
24359770SChangqing.Li@Sun.COM 		    Adapter->mcast_max_num);
24363526Sxy150489 		goto done;
24373526Sxy150489 	}
24383526Sxy150489 
24399770SChangqing.Li@Sun.COM 
24409770SChangqing.Li@Sun.COM 	if (Adapter->mcast_count == Adapter->mcast_alloc_count) {
24419770SChangqing.Li@Sun.COM 		old_len = Adapter->mcast_alloc_count *
24429770SChangqing.Li@Sun.COM 		    sizeof (struct ether_addr);
24439770SChangqing.Li@Sun.COM 		new_len = (Adapter->mcast_alloc_count + MCAST_ALLOC_SIZE) *
24449770SChangqing.Li@Sun.COM 		    sizeof (struct ether_addr);
24459770SChangqing.Li@Sun.COM 
24469770SChangqing.Li@Sun.COM 		newtable = kmem_alloc(new_len, KM_NOSLEEP);
24479770SChangqing.Li@Sun.COM 		if (newtable == NULL) {
24489770SChangqing.Li@Sun.COM 			res = ENOMEM;
24499770SChangqing.Li@Sun.COM 			e1000g_log(Adapter, CE_WARN,
24509770SChangqing.Li@Sun.COM 			    "Not enough memory to alloc mcast table");
24519770SChangqing.Li@Sun.COM 			goto done;
24529770SChangqing.Li@Sun.COM 		}
24539770SChangqing.Li@Sun.COM 
24549770SChangqing.Li@Sun.COM 		if (Adapter->mcast_table != NULL) {
24559770SChangqing.Li@Sun.COM 			bcopy(Adapter->mcast_table, newtable, old_len);
24569770SChangqing.Li@Sun.COM 			kmem_free(Adapter->mcast_table, old_len);
24579770SChangqing.Li@Sun.COM 		}
24589770SChangqing.Li@Sun.COM 		Adapter->mcast_alloc_count += MCAST_ALLOC_SIZE;
24599770SChangqing.Li@Sun.COM 		Adapter->mcast_table = newtable;
24609770SChangqing.Li@Sun.COM 	}
24619770SChangqing.Li@Sun.COM 
24623526Sxy150489 	bcopy(multiaddr,
24633526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
24643526Sxy150489 	Adapter->mcast_count++;
24653526Sxy150489 
24663526Sxy150489 	/*
24673526Sxy150489 	 * Update the MC table in the hardware
24683526Sxy150489 	 */
24694919Sxy150489 	e1000g_clear_interrupt(Adapter);
24704919Sxy150489 
2471*10680SMin.Xu@Sun.COM 	e1000_update_mc_addr_list(hw,
2472*10680SMin.Xu@Sun.COM 	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);
24734919Sxy150489 
24744919Sxy150489 	e1000g_mask_interrupt(Adapter);
24753526Sxy150489 
24765273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
24775273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24785273Sgl147354 		res = EIO;
24795273Sgl147354 	}
24805273Sgl147354 
24818479SChenlu.Chen@Sun.COM done:
24823526Sxy150489 	return (res);
24833526Sxy150489 }
24843526Sxy150489 
24853526Sxy150489 static int
24863526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
24873526Sxy150489 {
24884919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
24899770SChangqing.Li@Sun.COM 	struct ether_addr *newtable;
24909770SChangqing.Li@Sun.COM 	size_t new_len;
24919770SChangqing.Li@Sun.COM 	size_t old_len;
24923526Sxy150489 	unsigned i;
24933526Sxy150489 
24943526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
24953526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
24963526Sxy150489 		    ETHERADDRL) == 0) {
24973526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
24983526Sxy150489 				Adapter->mcast_table[i - 1] =
24993526Sxy150489 				    Adapter->mcast_table[i];
25003526Sxy150489 			}
25013526Sxy150489 			Adapter->mcast_count--;
25023526Sxy150489 			break;
25033526Sxy150489 		}
25043526Sxy150489 	}
25053526Sxy150489 
25069770SChangqing.Li@Sun.COM 	if ((Adapter->mcast_alloc_count - Adapter->mcast_count) >
25079770SChangqing.Li@Sun.COM 	    MCAST_ALLOC_SIZE) {
25089770SChangqing.Li@Sun.COM 		old_len = Adapter->mcast_alloc_count *
25099770SChangqing.Li@Sun.COM 		    sizeof (struct ether_addr);
25109770SChangqing.Li@Sun.COM 		new_len = (Adapter->mcast_alloc_count - MCAST_ALLOC_SIZE) *
25119770SChangqing.Li@Sun.COM 		    sizeof (struct ether_addr);
25129770SChangqing.Li@Sun.COM 
25139770SChangqing.Li@Sun.COM 		newtable = kmem_alloc(new_len, KM_NOSLEEP);
25149770SChangqing.Li@Sun.COM 		if (newtable != NULL) {
25159770SChangqing.Li@Sun.COM 			bcopy(Adapter->mcast_table, newtable, new_len);
25169770SChangqing.Li@Sun.COM 			kmem_free(Adapter->mcast_table, old_len);
25179770SChangqing.Li@Sun.COM 
25189770SChangqing.Li@Sun.COM 			Adapter->mcast_alloc_count -= MCAST_ALLOC_SIZE;
25199770SChangqing.Li@Sun.COM 			Adapter->mcast_table = newtable;
25209770SChangqing.Li@Sun.COM 		}
25219770SChangqing.Li@Sun.COM 	}
25229770SChangqing.Li@Sun.COM 
25233526Sxy150489 	/*
25243526Sxy150489 	 * Update the MC table in the hardware
25253526Sxy150489 	 */
25264919Sxy150489 	e1000g_clear_interrupt(Adapter);
25274919Sxy150489 
2528*10680SMin.Xu@Sun.COM 	e1000_update_mc_addr_list(hw,
2529*10680SMin.Xu@Sun.COM 	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);
25304919Sxy150489 
25314919Sxy150489 	e1000g_mask_interrupt(Adapter);
25323526Sxy150489 
25335273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
25345273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
25355273Sgl147354 		return (EIO);
25365273Sgl147354 	}
25375273Sgl147354 
25383526Sxy150489 	return (0);
25393526Sxy150489 }
25403526Sxy150489 
25419770SChangqing.Li@Sun.COM static void
25429770SChangqing.Li@Sun.COM e1000g_release_multicast(struct e1000g *Adapter)
25439770SChangqing.Li@Sun.COM {
25449770SChangqing.Li@Sun.COM 	if (Adapter->mcast_table != NULL) {
25459770SChangqing.Li@Sun.COM 		kmem_free(Adapter->mcast_table,
25469770SChangqing.Li@Sun.COM 		    Adapter->mcast_alloc_count * sizeof (struct ether_addr));
25479770SChangqing.Li@Sun.COM 		Adapter->mcast_table = NULL;
25489770SChangqing.Li@Sun.COM 	}
25499770SChangqing.Li@Sun.COM }
25509770SChangqing.Li@Sun.COM 
25513526Sxy150489 int
25523526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
25533526Sxy150489 {
25543526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25558479SChenlu.Chen@Sun.COM 	int result;
25568479SChenlu.Chen@Sun.COM 
25578479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25588479SChenlu.Chen@Sun.COM 
25598479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
25608479SChenlu.Chen@Sun.COM 		result = ECANCELED;
25618479SChenlu.Chen@Sun.COM 		goto done;
25628479SChenlu.Chen@Sun.COM 	}
25638479SChenlu.Chen@Sun.COM 
25648479SChenlu.Chen@Sun.COM 	result = (add) ? multicst_add(Adapter, addr)
25658479SChenlu.Chen@Sun.COM 	    : multicst_remove(Adapter, addr);
25668479SChenlu.Chen@Sun.COM 
25678479SChenlu.Chen@Sun.COM done:
25688479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
25698479SChenlu.Chen@Sun.COM 	return (result);
25708479SChenlu.Chen@Sun.COM 
25713526Sxy150489 }
25723526Sxy150489 
25733526Sxy150489 int
25743526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
25753526Sxy150489 {
25763526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25774919Sxy150489 	uint32_t rctl;
25783526Sxy150489 
25793526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25803526Sxy150489 
25818479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
25828479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
25838479SChenlu.Chen@Sun.COM 		return (ECANCELED);
25848479SChenlu.Chen@Sun.COM 	}
25858479SChenlu.Chen@Sun.COM 
25864919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
25873526Sxy150489 
25883526Sxy150489 	if (on)
25894919Sxy150489 		rctl |=
25903526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
25913526Sxy150489 	else
25924919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
25934919Sxy150489 
25944919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
25953526Sxy150489 
25963526Sxy150489 	Adapter->e1000g_promisc = on;
25973526Sxy150489 
25983526Sxy150489 	rw_exit(&Adapter->chip_lock);
25993526Sxy150489 
26005273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
26015273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
26025273Sgl147354 		return (EIO);
26035273Sgl147354 	}
26045273Sgl147354 
26053526Sxy150489 	return (0);
26063526Sxy150489 }
26073526Sxy150489 
26088275SEric Cheng /*
26098275SEric Cheng  * Entry points to enable and disable interrupts at the granularity of
26108275SEric Cheng  * a group.
26118275SEric Cheng  * Turns the poll_mode for the whole adapter on and off to enable or
26128275SEric Cheng  * override the ring level polling control over the hardware interrupts.
26138275SEric Cheng  */
26148275SEric Cheng static int
26158275SEric Cheng e1000g_rx_group_intr_enable(mac_intr_handle_t arg)
26168275SEric Cheng {
26178275SEric Cheng 	struct e1000g		*adapter = (struct e1000g *)arg;
26188275SEric Cheng 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
26198275SEric Cheng 
26208275SEric Cheng 	/*
26218275SEric Cheng 	 * Later interrupts at the granularity of the this ring will
26228275SEric Cheng 	 * invoke mac_rx() with NULL, indicating the need for another
26238275SEric Cheng 	 * software classification.
26248275SEric Cheng 	 * We have a single ring usable per adapter now, so we only need to
26258275SEric Cheng 	 * reset the rx handle for that one.
26268275SEric Cheng 	 * When more RX rings can be used, we should update each one of them.
26278275SEric Cheng 	 */
26288275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
26298275SEric Cheng 	rx_ring->mrh = NULL;
26308275SEric Cheng 	adapter->poll_mode = B_FALSE;
26318275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
26328275SEric Cheng 	return (0);
26338275SEric Cheng }
26348275SEric Cheng 
26358275SEric Cheng static int
26368275SEric Cheng e1000g_rx_group_intr_disable(mac_intr_handle_t arg)
26378275SEric Cheng {
26388275SEric Cheng 	struct e1000g *adapter = (struct e1000g *)arg;
26398275SEric Cheng 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
26408275SEric Cheng 
26418275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
26428275SEric Cheng 
26438275SEric Cheng 	/*
26448275SEric Cheng 	 * Later interrupts at the granularity of the this ring will
26458275SEric Cheng 	 * invoke mac_rx() with the handle for this ring;
26468275SEric Cheng 	 */
26478275SEric Cheng 	adapter->poll_mode = B_TRUE;
26488275SEric Cheng 	rx_ring->mrh = rx_ring->mrh_init;
26498275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
26508275SEric Cheng 	return (0);
26518275SEric Cheng }
26528275SEric Cheng 
26538275SEric Cheng /*
26548275SEric Cheng  * Entry points to enable and disable interrupts at the granularity of
26558275SEric Cheng  * a ring.
26568275SEric Cheng  * adapter poll_mode controls whether we actually proceed with hardware
26578275SEric Cheng  * interrupt toggling.
26588275SEric Cheng  */
26598275SEric Cheng static int
26608275SEric Cheng e1000g_rx_ring_intr_enable(mac_intr_handle_t intrh)
26618275SEric Cheng {
26628275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
26638275SEric Cheng 	struct e1000g 		*adapter = rx_ring->adapter;
26648275SEric Cheng 	struct e1000_hw 	*hw = &adapter->shared;
26658275SEric Cheng 	uint32_t		intr_mask;
26668275SEric Cheng 
26678479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
26688479SChenlu.Chen@Sun.COM 
26698479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
26708479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
26718479SChenlu.Chen@Sun.COM 		return (0);
26728479SChenlu.Chen@Sun.COM 	}
26738479SChenlu.Chen@Sun.COM 
26748275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
26758275SEric Cheng 	rx_ring->poll_flag = 0;
26768275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
26778275SEric Cheng 
26788833SVenu.Iyer@Sun.COM 	/* Rx interrupt enabling for MSI and legacy */
26798833SVenu.Iyer@Sun.COM 	intr_mask = E1000_READ_REG(hw, E1000_IMS);
26808833SVenu.Iyer@Sun.COM 	intr_mask |= E1000_IMS_RXT0;
26818833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMS, intr_mask);
26828833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
26838833SVenu.Iyer@Sun.COM 
26848833SVenu.Iyer@Sun.COM 	/* Trigger a Rx interrupt to check Rx ring */
26858833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
26868833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
26878479SChenlu.Chen@Sun.COM 
26888479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
26898275SEric Cheng 	return (0);
26908275SEric Cheng }
26918275SEric Cheng 
26928275SEric Cheng static int
26938275SEric Cheng e1000g_rx_ring_intr_disable(mac_intr_handle_t intrh)
26948275SEric Cheng {
26958275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
26968275SEric Cheng 	struct e1000g 		*adapter = rx_ring->adapter;
26978275SEric Cheng 	struct e1000_hw 	*hw = &adapter->shared;
26988275SEric Cheng 
26998479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
27008479SChenlu.Chen@Sun.COM 
27018479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
27028479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
27038479SChenlu.Chen@Sun.COM 		return (0);
27048479SChenlu.Chen@Sun.COM 	}
27058275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
27068275SEric Cheng 	rx_ring->poll_flag = 1;
27078275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
27088275SEric Cheng 
27098833SVenu.Iyer@Sun.COM 	/* Rx interrupt disabling for MSI and legacy */
27108833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMC, E1000_IMS_RXT0);
27118833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
27128479SChenlu.Chen@Sun.COM 
27138479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
27148275SEric Cheng 	return (0);
27158275SEric Cheng }
27168275SEric Cheng 
27178275SEric Cheng /*
27188275SEric Cheng  * e1000g_unicst_find - Find the slot for the specified unicast address
27198275SEric Cheng  */
27208275SEric Cheng static int
27218275SEric Cheng e1000g_unicst_find(struct e1000g *Adapter, const uint8_t *mac_addr)
27228275SEric Cheng {
27238275SEric Cheng 	int slot;
27248275SEric Cheng 
27258275SEric Cheng 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
27268479SChenlu.Chen@Sun.COM 		if ((Adapter->unicst_addr[slot].mac.set == 1) &&
27278479SChenlu.Chen@Sun.COM 		    (bcmp(Adapter->unicst_addr[slot].mac.addr,
27288479SChenlu.Chen@Sun.COM 		    mac_addr, ETHERADDRL) == 0))
27298275SEric Cheng 				return (slot);
27308275SEric Cheng 	}
27318275SEric Cheng 
27328275SEric Cheng 	return (-1);
27338275SEric Cheng }
27348275SEric Cheng 
27358275SEric Cheng /*
27368275SEric Cheng  * Entry points to add and remove a MAC address to a ring group.
27378275SEric Cheng  * The caller takes care of adding and removing the MAC addresses
27388275SEric Cheng  * to the filter via these two routines.
27398275SEric Cheng  */
27408275SEric Cheng 
27418275SEric Cheng static int
27428275SEric Cheng e1000g_addmac(void *arg, const uint8_t *mac_addr)
27438275SEric Cheng {
27448275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
27458400SNicolas.Droux@Sun.COM 	int slot, err;
27468275SEric Cheng 
27478479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
27488479SChenlu.Chen@Sun.COM 
27498479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
27508479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27518479SChenlu.Chen@Sun.COM 		return (ECANCELED);
27528479SChenlu.Chen@Sun.COM 	}
27538275SEric Cheng 
27548275SEric Cheng 	if (e1000g_unicst_find(Adapter, mac_addr) != -1) {
27558275SEric Cheng 		/* The same address is already in slot */
27568479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27578275SEric Cheng 		return (0);
27588275SEric Cheng 	}
27598275SEric Cheng 
27608275SEric Cheng 	if (Adapter->unicst_avail == 0) {
27618275SEric Cheng 		/* no slots available */
27628479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27638275SEric Cheng 		return (ENOSPC);
27648275SEric Cheng 	}
27658275SEric Cheng 
27668275SEric Cheng 	/* Search for a free slot */
27678275SEric Cheng 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
27688275SEric Cheng 		if (Adapter->unicst_addr[slot].mac.set == 0)
27698275SEric Cheng 			break;
27708275SEric Cheng 	}
27718275SEric Cheng 	ASSERT(slot < Adapter->unicst_total);
27728275SEric Cheng 
27738400SNicolas.Droux@Sun.COM 	err = e1000g_unicst_set(Adapter, mac_addr, slot);
27748400SNicolas.Droux@Sun.COM 	if (err == 0)
27758400SNicolas.Droux@Sun.COM 		Adapter->unicst_avail--;
27768275SEric Cheng 
27778479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
27788275SEric Cheng 
27798400SNicolas.Droux@Sun.COM 	return (err);
27808275SEric Cheng }
27818275SEric Cheng 
27828275SEric Cheng static int
27838275SEric Cheng e1000g_remmac(void *arg, const uint8_t *mac_addr)
27848275SEric Cheng {
27858275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
27868400SNicolas.Droux@Sun.COM 	int slot, err;
27878275SEric Cheng 
27888479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
27898479SChenlu.Chen@Sun.COM 
27908479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
27918479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27928479SChenlu.Chen@Sun.COM 		return (ECANCELED);
27938479SChenlu.Chen@Sun.COM 	}
27948275SEric Cheng 
27958275SEric Cheng 	slot = e1000g_unicst_find(Adapter, mac_addr);
27968275SEric Cheng 	if (slot == -1) {
27978479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27988275SEric Cheng 		return (EINVAL);
27998275SEric Cheng 	}
28008275SEric Cheng 
28018275SEric Cheng 	ASSERT(Adapter->unicst_addr[slot].mac.set);
28028275SEric Cheng 
28038275SEric Cheng 	/* Clear this slot */
28048400SNicolas.Droux@Sun.COM 	err = e1000g_unicst_set(Adapter, NULL, slot);
28058400SNicolas.Droux@Sun.COM 	if (err == 0)
28068400SNicolas.Droux@Sun.COM 		Adapter->unicst_avail++;
28078275SEric Cheng 
28088479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
28098275SEric Cheng 
28108400SNicolas.Droux@Sun.COM 	return (err);
28118275SEric Cheng }
28128275SEric Cheng 
28138275SEric Cheng static int
28148275SEric Cheng e1000g_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
28158275SEric Cheng {
28168275SEric Cheng 	e1000g_rx_ring_t *rx_ring = (e1000g_rx_ring_t *)rh;
28178275SEric Cheng 
28188275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
28198275SEric Cheng 	rx_ring->ring_gen_num = mr_gen_num;
28208275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
28218275SEric Cheng 	return (0);
28228275SEric Cheng }
28238275SEric Cheng 
28248275SEric Cheng /*
28258275SEric Cheng  * Callback funtion for MAC layer to register all rings.
28268275SEric Cheng  *
28278275SEric Cheng  * The hardware supports a single group with currently only one ring
28288275SEric Cheng  * available.
28298275SEric Cheng  * Though not offering virtualization ability per se, exposing the
28308275SEric Cheng  * group/ring still enables the polling and interrupt toggling.
28318275SEric Cheng  */
28328275SEric Cheng void
28338275SEric Cheng e1000g_fill_ring(void *arg, mac_ring_type_t rtype, const int grp_index,
28348275SEric Cheng     const int ring_index, mac_ring_info_t *infop, mac_ring_handle_t rh)
28358275SEric Cheng {
28368275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
28378275SEric Cheng 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
28388275SEric Cheng 	mac_intr_t *mintr;
28398275SEric Cheng 
28408275SEric Cheng 	/*
28418275SEric Cheng 	 * We advertised only RX group/rings, so the MAC framework shouldn't
28428275SEric Cheng 	 * ask for any thing else.
28438275SEric Cheng 	 */
28448275SEric Cheng 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0 && ring_index == 0);
28458275SEric Cheng 
28468275SEric Cheng 	rx_ring->mrh = rx_ring->mrh_init = rh;
28478275SEric Cheng 	infop->mri_driver = (mac_ring_driver_t)rx_ring;
28488275SEric Cheng 	infop->mri_start = e1000g_ring_start;
28498275SEric Cheng 	infop->mri_stop = NULL;
28508275SEric Cheng 	infop->mri_poll = e1000g_poll_ring;
28518275SEric Cheng 
28528275SEric Cheng 	/* Ring level interrupts */
28538275SEric Cheng 	mintr = &infop->mri_intr;
28548275SEric Cheng 	mintr->mi_handle = (mac_intr_handle_t)rx_ring;
28558275SEric Cheng 	mintr->mi_enable = e1000g_rx_ring_intr_enable;
28568275SEric Cheng 	mintr->mi_disable = e1000g_rx_ring_intr_disable;
28578275SEric Cheng }
28588275SEric Cheng 
28598275SEric Cheng static void
28608275SEric Cheng e1000g_fill_group(void *arg, mac_ring_type_t rtype, const int grp_index,
28618275SEric Cheng     mac_group_info_t *infop, mac_group_handle_t gh)
28628275SEric Cheng {
28638275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
28648275SEric Cheng 	mac_intr_t *mintr;
28658275SEric Cheng 
28668275SEric Cheng 	/*
28678275SEric Cheng 	 * We advertised a single RX ring. Getting a request for anything else
28688275SEric Cheng 	 * signifies a bug in the MAC framework.
28698275SEric Cheng 	 */
28708275SEric Cheng 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0);
28718275SEric Cheng 
28728275SEric Cheng 	Adapter->rx_group = gh;
28738275SEric Cheng 
28748275SEric Cheng 	infop->mgi_driver = (mac_group_driver_t)Adapter;
28758275SEric Cheng 	infop->mgi_start = NULL;
28768275SEric Cheng 	infop->mgi_stop = NULL;
28778275SEric Cheng 	infop->mgi_addmac = e1000g_addmac;
28788275SEric Cheng 	infop->mgi_remmac = e1000g_remmac;
28798275SEric Cheng 	infop->mgi_count = 1;
28808275SEric Cheng 
28818275SEric Cheng 	/* Group level interrupts */
28828275SEric Cheng 	mintr = &infop->mgi_intr;
28838275SEric Cheng 	mintr->mi_handle = (mac_intr_handle_t)Adapter;
28848275SEric Cheng 	mintr->mi_enable = e1000g_rx_group_intr_enable;
28858275SEric Cheng 	mintr->mi_disable = e1000g_rx_group_intr_disable;
28868275SEric Cheng }
28878275SEric Cheng 
28883526Sxy150489 static boolean_t
28893526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
28903526Sxy150489 {
28913526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
28923526Sxy150489 
28933526Sxy150489 	switch (cap) {
28943526Sxy150489 	case MAC_CAPAB_HCKSUM: {
28953526Sxy150489 		uint32_t *txflags = cap_data;
28966986Smx205022 
28976986Smx205022 		if (Adapter->tx_hcksum_enable)
28986986Smx205022 			*txflags = HCKSUM_IPHDRCKSUM |
28996986Smx205022 			    HCKSUM_INET_PARTIAL;
29006986Smx205022 		else
29013526Sxy150489 			return (B_FALSE);
29023526Sxy150489 		break;
29033526Sxy150489 	}
29046986Smx205022 
29056986Smx205022 	case MAC_CAPAB_LSO: {
29066986Smx205022 		mac_capab_lso_t *cap_lso = cap_data;
29076986Smx205022 
29086986Smx205022 		if (Adapter->lso_enable) {
29096986Smx205022 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
29106986Smx205022 			cap_lso->lso_basic_tcp_ipv4.lso_max =
29116986Smx205022 			    E1000_LSO_MAXLEN;
29126986Smx205022 		} else
29136986Smx205022 			return (B_FALSE);
29146986Smx205022 		break;
29156986Smx205022 	}
29168275SEric Cheng 	case MAC_CAPAB_RINGS: {
29178275SEric Cheng 		mac_capab_rings_t *cap_rings = cap_data;
29188275SEric Cheng 
29198275SEric Cheng 		/* No TX rings exposed yet */
29208275SEric Cheng 		if (cap_rings->mr_type != MAC_RING_TYPE_RX)
29218275SEric Cheng 			return (B_FALSE);
29228275SEric Cheng 
29238275SEric Cheng 		cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
29248275SEric Cheng 		cap_rings->mr_rnum = 1;
29258275SEric Cheng 		cap_rings->mr_gnum = 1;
29268275SEric Cheng 		cap_rings->mr_rget = e1000g_fill_ring;
29278275SEric Cheng 		cap_rings->mr_gget = e1000g_fill_group;
29288275SEric Cheng 		break;
29298275SEric Cheng 	}
29303526Sxy150489 	default:
29313526Sxy150489 		return (B_FALSE);
29323526Sxy150489 	}
29333526Sxy150489 	return (B_TRUE);
29343526Sxy150489 }
29353526Sxy150489 
29366394Scc210113 static boolean_t
29376394Scc210113 e1000g_param_locked(mac_prop_id_t pr_num)
29386394Scc210113 {
29396394Scc210113 	/*
29406394Scc210113 	 * All en_* parameters are locked (read-only) while
29416394Scc210113 	 * the device is in any sort of loopback mode ...
29426394Scc210113 	 */
29436394Scc210113 	switch (pr_num) {
29446789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
29456789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
29466789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
29476789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
29486789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
29496789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
29506789Sam223141 		case MAC_PROP_AUTONEG:
29516789Sam223141 		case MAC_PROP_FLOWCTRL:
29526394Scc210113 			return (B_TRUE);
29536394Scc210113 	}
29546394Scc210113 	return (B_FALSE);
29556394Scc210113 }
29566394Scc210113 
29576394Scc210113 /*
29586394Scc210113  * callback function for set/get of properties
29596394Scc210113  */
29606394Scc210113 static int
29616394Scc210113 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
29626394Scc210113     uint_t pr_valsize, const void *pr_val)
29636394Scc210113 {
29646394Scc210113 	struct e1000g *Adapter = arg;
296510605SMin.Xu@Sun.COM 	struct e1000_hw *hw = &Adapter->shared;
29666735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
29676394Scc210113 	int err = 0;
29686735Scc210113 	link_flowctrl_t flowctrl;
29696512Ssowmini 	uint32_t cur_mtu, new_mtu;
29706394Scc210113 
29716394Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
29728479SChenlu.Chen@Sun.COM 
29738479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
29748479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
29758479SChenlu.Chen@Sun.COM 		return (ECANCELED);
29768479SChenlu.Chen@Sun.COM 	}
29778479SChenlu.Chen@Sun.COM 
29786394Scc210113 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
29796394Scc210113 	    e1000g_param_locked(pr_num)) {
29806394Scc210113 		/*
29816394Scc210113 		 * All en_* parameters are locked (read-only)
29826394Scc210113 		 * while the device is in any sort of loopback mode.
29836394Scc210113 		 */
29846394Scc210113 		rw_exit(&Adapter->chip_lock);
29856394Scc210113 		return (EBUSY);
29866394Scc210113 	}
29876394Scc210113 
29886394Scc210113 	switch (pr_num) {
29896789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
299010605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
299110605SMin.Xu@Sun.COM 				err = ENOTSUP;
299210605SMin.Xu@Sun.COM 				break;
299310605SMin.Xu@Sun.COM 			}
29946394Scc210113 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
29956394Scc210113 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
29966394Scc210113 			goto reset;
29976789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
299810605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
299910605SMin.Xu@Sun.COM 				err = ENOTSUP;
300010605SMin.Xu@Sun.COM 				break;
300110605SMin.Xu@Sun.COM 			}
30026394Scc210113 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
30036394Scc210113 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
30046394Scc210113 			goto reset;
30056789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
300610605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
300710605SMin.Xu@Sun.COM 				err = ENOTSUP;
300810605SMin.Xu@Sun.COM 				break;
300910605SMin.Xu@Sun.COM 			}
30106394Scc210113 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
30116394Scc210113 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
30126394Scc210113 			goto reset;
30136789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
301410605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
301510605SMin.Xu@Sun.COM 				err = ENOTSUP;
301610605SMin.Xu@Sun.COM 				break;
301710605SMin.Xu@Sun.COM 			}
30186394Scc210113 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
30196394Scc210113 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
30206394Scc210113 			goto reset;
30216789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
302210605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
302310605SMin.Xu@Sun.COM 				err = ENOTSUP;
302410605SMin.Xu@Sun.COM 				break;
302510605SMin.Xu@Sun.COM 			}
30266394Scc210113 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
30276394Scc210113 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
30286394Scc210113 			goto reset;
30296789Sam223141 		case MAC_PROP_AUTONEG:
303010605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper) {
303110605SMin.Xu@Sun.COM 				err = ENOTSUP;
303210605SMin.Xu@Sun.COM 				break;
303310605SMin.Xu@Sun.COM 			}
30346394Scc210113 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
30356394Scc210113 			goto reset;
30366789Sam223141 		case MAC_PROP_FLOWCTRL:
30376735Scc210113 			fc->send_xon = B_TRUE;
30386735Scc210113 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
30396735Scc210113 
30406735Scc210113 			switch (flowctrl) {
30416394Scc210113 			default:
30426394Scc210113 				err = EINVAL;
30436394Scc210113 				break;
30446394Scc210113 			case LINK_FLOWCTRL_NONE:
30458539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_none;
30466394Scc210113 				break;
30476394Scc210113 			case LINK_FLOWCTRL_RX:
30488539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_rx_pause;
30496394Scc210113 				break;
30506394Scc210113 			case LINK_FLOWCTRL_TX:
30518539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_tx_pause;
30526394Scc210113 				break;
30536394Scc210113 			case LINK_FLOWCTRL_BI:
30548539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_full;
30556394Scc210113 				break;
30566394Scc210113 			}
30576394Scc210113 reset:
30586394Scc210113 			if (err == 0) {
3059*10680SMin.Xu@Sun.COM 				/* check PCH limits & reset the link */
3060*10680SMin.Xu@Sun.COM 				e1000g_pch_limits(Adapter);
30616394Scc210113 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
30626394Scc210113 					err = EINVAL;
30636394Scc210113 			}
30646394Scc210113 			break;
30656789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
30666789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
30676789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
30686789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
30696789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
30706789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
30718118SVasumathi.Sundaram@Sun.COM 		case MAC_PROP_EN_1000HDX_CAP:
30726789Sam223141 		case MAC_PROP_STATUS:
30736789Sam223141 		case MAC_PROP_SPEED:
30746789Sam223141 		case MAC_PROP_DUPLEX:
30756394Scc210113 			err = ENOTSUP; /* read-only prop. Can't set this. */
30766394Scc210113 			break;
30776789Sam223141 		case MAC_PROP_MTU:
3078*10680SMin.Xu@Sun.COM 			/* adapter must be stopped for an MTU change */
3079*10680SMin.Xu@Sun.COM 			if (Adapter->e1000g_state & E1000G_STARTED) {
3080*10680SMin.Xu@Sun.COM 				err = EBUSY;
3081*10680SMin.Xu@Sun.COM 				break;
3082*10680SMin.Xu@Sun.COM 			}
3083*10680SMin.Xu@Sun.COM 
30846394Scc210113 			cur_mtu = Adapter->default_mtu;
3085*10680SMin.Xu@Sun.COM 
3086*10680SMin.Xu@Sun.COM 			/* get new requested MTU */
30876394Scc210113 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
30886394Scc210113 			if (new_mtu == cur_mtu) {
30896394Scc210113 				err = 0;
30906394Scc210113 				break;
30916394Scc210113 			}
30926394Scc210113 
3093*10680SMin.Xu@Sun.COM 			if ((new_mtu < DEFAULT_MTU) ||
3094*10680SMin.Xu@Sun.COM 			    (new_mtu > Adapter->max_mtu)) {
30956394Scc210113 				err = EINVAL;
30966394Scc210113 				break;
30976394Scc210113 			}
3098*10680SMin.Xu@Sun.COM 
3099*10680SMin.Xu@Sun.COM 			/* inform MAC framework of new MTU */
31006394Scc210113 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
3101*10680SMin.Xu@Sun.COM 
31026394Scc210113 			if (err == 0) {
31036394Scc210113 				Adapter->default_mtu = new_mtu;
3104*10680SMin.Xu@Sun.COM 				Adapter->max_frame_size =
3105*10680SMin.Xu@Sun.COM 				    e1000g_mtu2maxframe(new_mtu);
3106*10680SMin.Xu@Sun.COM 
3107*10680SMin.Xu@Sun.COM 				/*
3108*10680SMin.Xu@Sun.COM 				 * check PCH limits & set buffer sizes to
3109*10680SMin.Xu@Sun.COM 				 * match new MTU
3110*10680SMin.Xu@Sun.COM 				 */
3111*10680SMin.Xu@Sun.COM 				e1000g_pch_limits(Adapter);
31126394Scc210113 				e1000g_set_bufsize(Adapter);
31136394Scc210113 			}
31146394Scc210113 			break;
31156789Sam223141 		case MAC_PROP_PRIVATE:
31166394Scc210113 			err = e1000g_set_priv_prop(Adapter, pr_name,
31176394Scc210113 			    pr_valsize, pr_val);
31186394Scc210113 			break;
31196394Scc210113 		default:
31206394Scc210113 			err = ENOTSUP;
31216394Scc210113 			break;
31226394Scc210113 	}
31236394Scc210113 	rw_exit(&Adapter->chip_lock);
31246394Scc210113 	return (err);
31256394Scc210113 }
31266394Scc210113 
31276394Scc210113 static int
31286394Scc210113 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
31298118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
31306394Scc210113 {
31316394Scc210113 	struct e1000g *Adapter = arg;
31326735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
313310605SMin.Xu@Sun.COM 	struct e1000_hw *hw = &Adapter->shared;
31346512Ssowmini 	int err = 0;
31356735Scc210113 	link_flowctrl_t flowctrl;
31366394Scc210113 	uint64_t tmp = 0;
31376394Scc210113 
31386512Ssowmini 	if (pr_valsize == 0)
31396512Ssowmini 		return (EINVAL);
31406512Ssowmini 
31418118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
31428118SVasumathi.Sundaram@Sun.COM 
31436394Scc210113 	bzero(pr_val, pr_valsize);
31446789Sam223141 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
31456512Ssowmini 		return (e1000g_get_def_val(Adapter, pr_num,
31466512Ssowmini 		    pr_valsize, pr_val));
31476512Ssowmini 	}
31486512Ssowmini 
31496394Scc210113 	switch (pr_num) {
31506789Sam223141 		case MAC_PROP_DUPLEX:
31518118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31526512Ssowmini 			if (pr_valsize >= sizeof (link_duplex_t)) {
31536512Ssowmini 				bcopy(&Adapter->link_duplex, pr_val,
31546512Ssowmini 				    sizeof (link_duplex_t));
31556512Ssowmini 			} else
31566512Ssowmini 				err = EINVAL;
31576394Scc210113 			break;
31586789Sam223141 		case MAC_PROP_SPEED:
31598118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31606394Scc210113 			if (pr_valsize >= sizeof (uint64_t)) {
31616394Scc210113 				tmp = Adapter->link_speed * 1000000ull;
31626394Scc210113 				bcopy(&tmp, pr_val, sizeof (tmp));
31636512Ssowmini 			} else
31646512Ssowmini 				err = EINVAL;
31656394Scc210113 			break;
31666789Sam223141 		case MAC_PROP_AUTONEG:
316710605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
316810605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
31696512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
31706394Scc210113 			break;
31716789Sam223141 		case MAC_PROP_FLOWCTRL:
31726394Scc210113 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
31738479SChenlu.Chen@Sun.COM 				switch (fc->current_mode) {
31746394Scc210113 					case e1000_fc_none:
31756735Scc210113 						flowctrl = LINK_FLOWCTRL_NONE;
31766394Scc210113 						break;
31776394Scc210113 					case e1000_fc_rx_pause:
31786735Scc210113 						flowctrl = LINK_FLOWCTRL_RX;
31796394Scc210113 						break;
31806394Scc210113 					case e1000_fc_tx_pause:
31816735Scc210113 						flowctrl = LINK_FLOWCTRL_TX;
31826394Scc210113 						break;
31836394Scc210113 					case e1000_fc_full:
31846735Scc210113 						flowctrl = LINK_FLOWCTRL_BI;
31856394Scc210113 						break;
31866394Scc210113 				}
31876735Scc210113 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
31886512Ssowmini 			} else
31896512Ssowmini 				err = EINVAL;
31906394Scc210113 			break;
31916789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
31928118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31936512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
31946394Scc210113 			break;
31956789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
319610605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
319710605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
31986512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
31996394Scc210113 			break;
32006789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
32018118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32026512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
32036394Scc210113 			break;
32046789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
32058118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32066512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
32076394Scc210113 			break;
32086789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
32098118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32106512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
32116394Scc210113 			break;
32126789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
321310605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
321410605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
32156512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
32166394Scc210113 			break;
32176789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
32188118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32196512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
32206394Scc210113 			break;
32216789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
322210605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
322310605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
32246512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
32256394Scc210113 			break;
32266789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
32278118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32286512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
32296394Scc210113 			break;
32306789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
323110605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
323210605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
32336512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
32346394Scc210113 			break;
32356789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
32368118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32376512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
32386394Scc210113 			break;
32396789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
324010605SMin.Xu@Sun.COM 			if (hw->phy.media_type != e1000_media_type_copper)
324110605SMin.Xu@Sun.COM 				*perm = MAC_PROP_PERM_READ;
32426512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
32436512Ssowmini 			break;
32446789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
32456789Sam223141 		case MAC_PROP_EN_100T4_CAP:
32468118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
32476512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
32486394Scc210113 			break;
32496789Sam223141 		case MAC_PROP_PRIVATE:
32506394Scc210113 			err = e1000g_get_priv_prop(Adapter, pr_name,
32518118SVasumathi.Sundaram@Sun.COM 			    pr_flags, pr_valsize, pr_val, perm);
32526394Scc210113 			break;
32539514SGirish.Moodalbail@Sun.COM 		case MAC_PROP_MTU: {
32549514SGirish.Moodalbail@Sun.COM 			struct e1000_mac_info *mac = &Adapter->shared.mac;
32559514SGirish.Moodalbail@Sun.COM 			struct e1000_phy_info *phy = &Adapter->shared.phy;
32569514SGirish.Moodalbail@Sun.COM 			mac_propval_range_t range;
32579514SGirish.Moodalbail@Sun.COM 
32589514SGirish.Moodalbail@Sun.COM 			if (!(pr_flags & MAC_PROP_POSSIBLE))
32599514SGirish.Moodalbail@Sun.COM 				return (ENOTSUP);
32609514SGirish.Moodalbail@Sun.COM 			if (pr_valsize < sizeof (mac_propval_range_t))
32619514SGirish.Moodalbail@Sun.COM 				return (EINVAL);
32629514SGirish.Moodalbail@Sun.COM 			range.mpr_count = 1;
32639514SGirish.Moodalbail@Sun.COM 			range.mpr_type = MAC_PROPVAL_UINT32;
32649514SGirish.Moodalbail@Sun.COM 			range.range_uint32[0].mpur_min = DEFAULT_MTU;
3265*10680SMin.Xu@Sun.COM 			range.range_uint32[0].mpur_max = Adapter->max_mtu;
32669514SGirish.Moodalbail@Sun.COM 			/* following MAC type do not support jumbo frames */
32679514SGirish.Moodalbail@Sun.COM 			if ((mac->type == e1000_ich8lan) ||
32689514SGirish.Moodalbail@Sun.COM 			    ((mac->type == e1000_ich9lan) && (phy->type ==
32699514SGirish.Moodalbail@Sun.COM 			    e1000_phy_ife))) {
32709514SGirish.Moodalbail@Sun.COM 				range.range_uint32[0].mpur_max = DEFAULT_MTU;
32719514SGirish.Moodalbail@Sun.COM 			}
32729514SGirish.Moodalbail@Sun.COM 			bcopy(&range, pr_val, sizeof (range));
32739514SGirish.Moodalbail@Sun.COM 			break;
32749514SGirish.Moodalbail@Sun.COM 		}
32756394Scc210113 		default:
32766394Scc210113 			err = ENOTSUP;
32776394Scc210113 			break;
32786394Scc210113 	}
32796394Scc210113 	return (err);
32806394Scc210113 }
32816394Scc210113 
32827426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
32836394Scc210113 static int
32846394Scc210113 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
32856394Scc210113     uint_t pr_valsize, const void *pr_val)
32866394Scc210113 {
32876394Scc210113 	int err = 0;
32886394Scc210113 	long result;
32896394Scc210113 	struct e1000_hw *hw = &Adapter->shared;
32906394Scc210113 
32916394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
32926394Scc210113 		if (pr_val == NULL) {
32936394Scc210113 			err = EINVAL;
32946394Scc210113 			return (err);
32956394Scc210113 		}
32966394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32976394Scc210113 		if (result < MIN_TX_BCOPY_THRESHOLD ||
32986394Scc210113 		    result > MAX_TX_BCOPY_THRESHOLD)
32996394Scc210113 			err = EINVAL;
33006394Scc210113 		else {
33016394Scc210113 			Adapter->tx_bcopy_thresh = (uint32_t)result;
33026394Scc210113 		}
33036394Scc210113 		return (err);
33046394Scc210113 	}
33056394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
33066394Scc210113 		if (pr_val == NULL) {
33076394Scc210113 			err = EINVAL;
33086394Scc210113 			return (err);
33096394Scc210113 		}
33106394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33116394Scc210113 		if (result < 0 || result > 1)
33126394Scc210113 			err = EINVAL;
33136394Scc210113 		else {
33146394Scc210113 			Adapter->tx_intr_enable = (result == 1) ?
33156394Scc210113 			    B_TRUE: B_FALSE;
33166394Scc210113 			if (Adapter->tx_intr_enable)
33176394Scc210113 				e1000g_mask_tx_interrupt(Adapter);
33186394Scc210113 			else
33196394Scc210113 				e1000g_clear_tx_interrupt(Adapter);
33206394Scc210113 			if (e1000g_check_acc_handle(
33216394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
33226394Scc210113 				ddi_fm_service_impact(Adapter->dip,
33236394Scc210113 				    DDI_SERVICE_DEGRADED);
33246394Scc210113 		}
33256394Scc210113 		return (err);
33266394Scc210113 	}
33276394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
33286394Scc210113 		if (pr_val == NULL) {
33296394Scc210113 			err = EINVAL;
33306394Scc210113 			return (err);
33316394Scc210113 		}
33326394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33336394Scc210113 		if (result < MIN_TX_INTR_DELAY ||
33346394Scc210113 		    result > MAX_TX_INTR_DELAY)
33356394Scc210113 			err = EINVAL;
33366394Scc210113 		else {
33376394Scc210113 			Adapter->tx_intr_delay = (uint32_t)result;
33386394Scc210113 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
33396394Scc210113 			if (e1000g_check_acc_handle(
33406394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
33416394Scc210113 				ddi_fm_service_impact(Adapter->dip,
33426394Scc210113 				    DDI_SERVICE_DEGRADED);
33436394Scc210113 		}
33446394Scc210113 		return (err);
33456394Scc210113 	}
33466394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
33476394Scc210113 		if (pr_val == NULL) {
33486394Scc210113 			err = EINVAL;
33496394Scc210113 			return (err);
33506394Scc210113 		}
33516394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33526394Scc210113 		if (result < MIN_TX_INTR_ABS_DELAY ||
33536394Scc210113 		    result > MAX_TX_INTR_ABS_DELAY)
33546394Scc210113 			err = EINVAL;
33556394Scc210113 		else {
33566394Scc210113 			Adapter->tx_intr_abs_delay = (uint32_t)result;
33576394Scc210113 			E1000_WRITE_REG(hw, E1000_TADV,
33586394Scc210113 			    Adapter->tx_intr_abs_delay);
33596394Scc210113 			if (e1000g_check_acc_handle(
33606394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
33616394Scc210113 				ddi_fm_service_impact(Adapter->dip,
33626394Scc210113 				    DDI_SERVICE_DEGRADED);
33636394Scc210113 		}
33646394Scc210113 		return (err);
33656394Scc210113 	}
33666394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
33676394Scc210113 		if (pr_val == NULL) {
33686394Scc210113 			err = EINVAL;
33696394Scc210113 			return (err);
33706394Scc210113 		}
33716394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33726394Scc210113 		if (result < MIN_RX_BCOPY_THRESHOLD ||
33736394Scc210113 		    result > MAX_RX_BCOPY_THRESHOLD)
33746394Scc210113 			err = EINVAL;
33756394Scc210113 		else
33766394Scc210113 			Adapter->rx_bcopy_thresh = (uint32_t)result;
33776394Scc210113 		return (err);
33786394Scc210113 	}
33796394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
33806394Scc210113 		if (pr_val == NULL) {
33816394Scc210113 			err = EINVAL;
33826394Scc210113 			return (err);
33836394Scc210113 		}
33846394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33856394Scc210113 		if (result < MIN_RX_LIMIT_ON_INTR ||
33866394Scc210113 		    result > MAX_RX_LIMIT_ON_INTR)
33876394Scc210113 			err = EINVAL;
33886394Scc210113 		else
33896394Scc210113 			Adapter->rx_limit_onintr = (uint32_t)result;
33906394Scc210113 		return (err);
33916394Scc210113 	}
33926394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
33936394Scc210113 		if (pr_val == NULL) {
33946394Scc210113 			err = EINVAL;
33956394Scc210113 			return (err);
33966394Scc210113 		}
33976394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33986394Scc210113 		if (result < MIN_RX_INTR_DELAY ||
33996394Scc210113 		    result > MAX_RX_INTR_DELAY)
34006394Scc210113 			err = EINVAL;
34016394Scc210113 		else {
34026394Scc210113 			Adapter->rx_intr_delay = (uint32_t)result;
34036394Scc210113 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
34046394Scc210113 			if (e1000g_check_acc_handle(
34056394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
34066394Scc210113 				ddi_fm_service_impact(Adapter->dip,
34076394Scc210113 				    DDI_SERVICE_DEGRADED);
34086394Scc210113 		}
34096394Scc210113 		return (err);
34106394Scc210113 	}
34116394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
34126394Scc210113 		if (pr_val == NULL) {
34136394Scc210113 			err = EINVAL;
34146394Scc210113 			return (err);
34156394Scc210113 		}
34166394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
34176394Scc210113 		if (result < MIN_RX_INTR_ABS_DELAY ||
34186394Scc210113 		    result > MAX_RX_INTR_ABS_DELAY)
34196394Scc210113 			err = EINVAL;
34206394Scc210113 		else {
34216394Scc210113 			Adapter->rx_intr_abs_delay = (uint32_t)result;
34226394Scc210113 			E1000_WRITE_REG(hw, E1000_RADV,
34236394Scc210113 			    Adapter->rx_intr_abs_delay);
34246394Scc210113 			if (e1000g_check_acc_handle(
34256394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
34266394Scc210113 				ddi_fm_service_impact(Adapter->dip,
34276394Scc210113 				    DDI_SERVICE_DEGRADED);
34286394Scc210113 		}
34296394Scc210113 		return (err);
34306394Scc210113 	}
34316394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
34326394Scc210113 		if (pr_val == NULL) {
34336394Scc210113 			err = EINVAL;
34346394Scc210113 			return (err);
34356394Scc210113 		}
34366394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
34376394Scc210113 		if (result < MIN_INTR_THROTTLING ||
34386394Scc210113 		    result > MAX_INTR_THROTTLING)
34396394Scc210113 			err = EINVAL;
34406394Scc210113 		else {
34416394Scc210113 			if (hw->mac.type >= e1000_82540) {
34426394Scc210113 				Adapter->intr_throttling_rate =
34436394Scc210113 				    (uint32_t)result;
34446394Scc210113 				E1000_WRITE_REG(hw, E1000_ITR,
34456394Scc210113 				    Adapter->intr_throttling_rate);
34466394Scc210113 				if (e1000g_check_acc_handle(
34476394Scc210113 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
34486394Scc210113 					ddi_fm_service_impact(Adapter->dip,
34496394Scc210113 					    DDI_SERVICE_DEGRADED);
34506394Scc210113 			} else
34516394Scc210113 				err = EINVAL;
34526394Scc210113 		}
34536394Scc210113 		return (err);
34546394Scc210113 	}
34556394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
34566394Scc210113 		if (pr_val == NULL) {
34576394Scc210113 			err = EINVAL;
34586394Scc210113 			return (err);
34596394Scc210113 		}
34606394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
34616394Scc210113 		if (result < 0 || result > 1)
34626394Scc210113 			err = EINVAL;
34636394Scc210113 		else {
34646394Scc210113 			if (hw->mac.type >= e1000_82540) {
34656394Scc210113 				Adapter->intr_adaptive = (result == 1) ?
34666394Scc210113 				    B_TRUE : B_FALSE;
34676394Scc210113 			} else {
34686394Scc210113 				err = EINVAL;
34696394Scc210113 			}
34706394Scc210113 		}
34716394Scc210113 		return (err);
34726394Scc210113 	}
34736394Scc210113 	return (ENOTSUP);
34746394Scc210113 }
34756394Scc210113 
34766394Scc210113 static int
34776394Scc210113 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
34788118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
34796394Scc210113 {
34806394Scc210113 	int err = ENOTSUP;
34816789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
34826512Ssowmini 	int value;
34836512Ssowmini 
34846512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
34858118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
34866512Ssowmini 		if (is_default)
34876512Ssowmini 			goto done;
34886512Ssowmini 		value = Adapter->param_adv_pause;
34896512Ssowmini 		err = 0;
34906512Ssowmini 		goto done;
34916512Ssowmini 	}
34926512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
34938118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
34946512Ssowmini 		if (is_default)
34956512Ssowmini 			goto done;
34966512Ssowmini 		value = Adapter->param_adv_asym_pause;
34976512Ssowmini 		err = 0;
34986512Ssowmini 		goto done;
34996512Ssowmini 	}
35006394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
35016512Ssowmini 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
35026512Ssowmini 		    Adapter->tx_bcopy_thresh);
35036394Scc210113 		err = 0;
35046394Scc210113 		goto done;
35056394Scc210113 	}
35066394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
35076512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
35086512Ssowmini 		    Adapter->tx_intr_enable);
35096394Scc210113 		err = 0;
35106394Scc210113 		goto done;
35116394Scc210113 	}
35126394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
35136512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
35146512Ssowmini 		    Adapter->tx_intr_delay);
35156394Scc210113 		err = 0;
35166394Scc210113 		goto done;
35176394Scc210113 	}
35186394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
35196512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
35206512Ssowmini 		    Adapter->tx_intr_abs_delay);
35216394Scc210113 		err = 0;
35226394Scc210113 		goto done;
35236394Scc210113 	}
35246394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
35256512Ssowmini 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
35266512Ssowmini 		    Adapter->rx_bcopy_thresh);
35276394Scc210113 		err = 0;
35286394Scc210113 		goto done;
35296394Scc210113 	}
35306394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
35316512Ssowmini 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
35326512Ssowmini 		    Adapter->rx_limit_onintr);
35336394Scc210113 		err = 0;
35346394Scc210113 		goto done;
35356394Scc210113 	}
35366394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
35376512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
35386512Ssowmini 		    Adapter->rx_intr_delay);
35396394Scc210113 		err = 0;
35406394Scc210113 		goto done;
35416394Scc210113 	}
35426394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
35436512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
35446512Ssowmini 		    Adapter->rx_intr_abs_delay);
35456394Scc210113 		err = 0;
35466394Scc210113 		goto done;
35476394Scc210113 	}
35486394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
35496512Ssowmini 		value = (is_default ? DEFAULT_INTR_THROTTLING :
35506512Ssowmini 		    Adapter->intr_throttling_rate);
35516394Scc210113 		err = 0;
35526394Scc210113 		goto done;
35536394Scc210113 	}
35546394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
35556512Ssowmini 		value = (is_default ? 1 : Adapter->intr_adaptive);
35566394Scc210113 		err = 0;
35576394Scc210113 		goto done;
35586394Scc210113 	}
35596394Scc210113 done:
35606394Scc210113 	if (err == 0) {
35616512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
35626394Scc210113 	}
35636394Scc210113 	return (err);
35646394Scc210113 }
35656394Scc210113 
35663526Sxy150489 /*
35674919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
35684919Sxy150489  * This routine gets user-configured values out of the configuration
35694919Sxy150489  * file e1000g.conf.
35704919Sxy150489  *
35714919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
35724919Sxy150489  * default.
35734919Sxy150489  * If user does not configure a value, use the default.
35744919Sxy150489  * If user configures below the minimum, use the minumum.
35754919Sxy150489  * If user configures above the maximum, use the maxumum.
35763526Sxy150489  */
35773526Sxy150489 static void
35784919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
35793526Sxy150489 {
35804919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
35814919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
35824919Sxy150489 
35833526Sxy150489 	/*
35843526Sxy150489 	 * get each configurable property from e1000g.conf
35853526Sxy150489 	 */
35863526Sxy150489 
35873526Sxy150489 	/*
35883526Sxy150489 	 * NumTxDescriptors
35893526Sxy150489 	 */
35904919Sxy150489 	Adapter->tx_desc_num =
35914919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
35924919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
35934919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
35943526Sxy150489 
35953526Sxy150489 	/*
35963526Sxy150489 	 * NumRxDescriptors
35973526Sxy150489 	 */
35984919Sxy150489 	Adapter->rx_desc_num =
35994919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
36004919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
36014919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
36023526Sxy150489 
36033526Sxy150489 	/*
36043526Sxy150489 	 * NumRxFreeList
36053526Sxy150489 	 */
36064919Sxy150489 	Adapter->rx_freelist_num =
36074919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
36084919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
36094919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
36103526Sxy150489 
36113526Sxy150489 	/*
36123526Sxy150489 	 * NumTxPacketList
36133526Sxy150489 	 */
36144919Sxy150489 	Adapter->tx_freelist_num =
36154919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
36164919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
36174919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
36183526Sxy150489 
36193526Sxy150489 	/*
36203526Sxy150489 	 * FlowControl
36213526Sxy150489 	 */
36226735Scc210113 	hw->fc.send_xon = B_TRUE;
36238539SChenlu.Chen@Sun.COM 	hw->fc.requested_mode =
36244919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
36254919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
36263526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
36278539SChenlu.Chen@Sun.COM 	if (hw->fc.requested_mode == 4)
36288539SChenlu.Chen@Sun.COM 		hw->fc.requested_mode = e1000_fc_default;
36293526Sxy150489 
36303526Sxy150489 	/*
36314919Sxy150489 	 * Max Num Receive Packets on Interrupt
36323526Sxy150489 	 */
36334919Sxy150489 	Adapter->rx_limit_onintr =
36344919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
36354919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
36364919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
36373526Sxy150489 
36383526Sxy150489 	/*
36393526Sxy150489 	 * PHY master slave setting
36403526Sxy150489 	 */
36414919Sxy150489 	hw->phy.ms_type =
36424919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
36433526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
36443526Sxy150489 	    e1000_ms_hw_default);
36453526Sxy150489 
36463526Sxy150489 	/*
36473526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
36483526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
36493526Sxy150489 	 */
36504919Sxy150489 	tbi_compatibility =
36514919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
36524919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
36534919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
36543526Sxy150489 
36553526Sxy150489 	/*
36563526Sxy150489 	 * MSI Enable
36573526Sxy150489 	 */
36586986Smx205022 	Adapter->msi_enable =
36594919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
36604919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
36613526Sxy150489 
36623526Sxy150489 	/*
36633526Sxy150489 	 * Interrupt Throttling Rate
36643526Sxy150489 	 */
36653526Sxy150489 	Adapter->intr_throttling_rate =
36664919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
36674919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
36684919Sxy150489 	    DEFAULT_INTR_THROTTLING);
36693526Sxy150489 
36703526Sxy150489 	/*
36713526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
36723526Sxy150489 	 * It is enabled by default
36733526Sxy150489 	 */
36743526Sxy150489 	Adapter->intr_adaptive =
36754919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
36763526Sxy150489 	    B_TRUE : B_FALSE;
36775882Syy150190 
36785882Syy150190 	/*
36796011Ssv141092 	 * Hardware checksum enable/disable parameter
36806011Ssv141092 	 */
36816986Smx205022 	Adapter->tx_hcksum_enable =
36826986Smx205022 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
36836011Ssv141092 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
36846986Smx205022 	/*
36856986Smx205022 	 * Checksum on/off selection via global parameters.
36866986Smx205022 	 *
36876986Smx205022 	 * If the chip is flagged as not capable of (correctly)
36886986Smx205022 	 * handling checksumming, we don't enable it on either
36896986Smx205022 	 * Rx or Tx side.  Otherwise, we take this chip's settings
36906986Smx205022 	 * from the patchable global defaults.
36916986Smx205022 	 *
36926986Smx205022 	 * We advertise our capabilities only if TX offload is
36936986Smx205022 	 * enabled.  On receive, the stack will accept checksummed
36946986Smx205022 	 * packets anyway, even if we haven't said we can deliver
36956986Smx205022 	 * them.
36966986Smx205022 	 */
36976986Smx205022 	switch (hw->mac.type) {
36986986Smx205022 		case e1000_82540:
36996986Smx205022 		case e1000_82544:
37006986Smx205022 		case e1000_82545:
37016986Smx205022 		case e1000_82545_rev_3:
37026986Smx205022 		case e1000_82546:
37036986Smx205022 		case e1000_82546_rev_3:
37046986Smx205022 		case e1000_82571:
37056986Smx205022 		case e1000_82572:
37066986Smx205022 		case e1000_82573:
37076986Smx205022 		case e1000_80003es2lan:
37086986Smx205022 			break;
37096986Smx205022 		/*
37106986Smx205022 		 * For the following Intel PRO/1000 chipsets, we have not
37116986Smx205022 		 * tested the hardware checksum offload capability, so we
37126986Smx205022 		 * disable the capability for them.
37136986Smx205022 		 *	e1000_82542,
37146986Smx205022 		 *	e1000_82543,
37156986Smx205022 		 *	e1000_82541,
37166986Smx205022 		 *	e1000_82541_rev_2,
37176986Smx205022 		 *	e1000_82547,
37186986Smx205022 		 *	e1000_82547_rev_2,
37196986Smx205022 		 */
37206986Smx205022 		default:
37216986Smx205022 			Adapter->tx_hcksum_enable = B_FALSE;
37226986Smx205022 	}
37236986Smx205022 
37246986Smx205022 	/*
37256986Smx205022 	 * Large Send Offloading(LSO) Enable/Disable
37266986Smx205022 	 * If the tx hardware checksum is not enabled, LSO should be
37276986Smx205022 	 * disabled.
37286986Smx205022 	 */
37296986Smx205022 	Adapter->lso_enable =
37306986Smx205022 	    e1000g_get_prop(Adapter, "lso_enable",
37316986Smx205022 	    0, 1, DEFAULT_LSO_ENABLE);
37326986Smx205022 
37336986Smx205022 	switch (hw->mac.type) {
37346986Smx205022 		case e1000_82546:
37356986Smx205022 		case e1000_82546_rev_3:
37366986Smx205022 			if (Adapter->lso_enable)
37376986Smx205022 				Adapter->lso_premature_issue = B_TRUE;
37387426SChenliang.Xu@Sun.COM 			/* FALLTHRU */
37396986Smx205022 		case e1000_82571:
37406986Smx205022 		case e1000_82572:
37416986Smx205022 		case e1000_82573:
37428073SMin.Xu@Sun.COM 		case e1000_80003es2lan:
37436986Smx205022 			break;
37446986Smx205022 		default:
37456986Smx205022 			Adapter->lso_enable = B_FALSE;
37466986Smx205022 	}
37476986Smx205022 
37486986Smx205022 	if (!Adapter->tx_hcksum_enable) {
37496986Smx205022 		Adapter->lso_premature_issue = B_FALSE;
37506986Smx205022 		Adapter->lso_enable = B_FALSE;
37516986Smx205022 	}
37528417SChenlu.Chen@Sun.COM 
37538417SChenlu.Chen@Sun.COM 	/*
37548417SChenlu.Chen@Sun.COM 	 * If mem_workaround_82546 is enabled, the rx buffer allocated by
37558417SChenlu.Chen@Sun.COM 	 * e1000_82545, e1000_82546 and e1000_82546_rev_3
37568417SChenlu.Chen@Sun.COM 	 * will not cross 64k boundary.
37578417SChenlu.Chen@Sun.COM 	 */
37588417SChenlu.Chen@Sun.COM 	Adapter->mem_workaround_82546 =
37598417SChenlu.Chen@Sun.COM 	    e1000g_get_prop(Adapter, "mem_workaround_82546",
37608417SChenlu.Chen@Sun.COM 	    0, 1, DEFAULT_MEM_WORKAROUND_82546);
37619770SChangqing.Li@Sun.COM 
37629770SChangqing.Li@Sun.COM 	/*
37639770SChangqing.Li@Sun.COM 	 * Max number of multicast addresses
37649770SChangqing.Li@Sun.COM 	 */
37659770SChangqing.Li@Sun.COM 	Adapter->mcast_max_num =
37669770SChangqing.Li@Sun.COM 	    e1000g_get_prop(Adapter, "mcast_max_num",
37679770SChangqing.Li@Sun.COM 	    MIN_MCAST_NUM, MAX_MCAST_NUM, hw->mac.mta_reg_count * 32);
37683526Sxy150489 }
37693526Sxy150489 
37703526Sxy150489 /*
37714919Sxy150489  * e1000g_get_prop - routine to read properties
37724919Sxy150489  *
37734919Sxy150489  * Get a user-configure property value out of the configuration
37744919Sxy150489  * file e1000g.conf.
37754919Sxy150489  *
37764919Sxy150489  * Caller provides name of the property, a default value, a minimum
37774919Sxy150489  * value, and a maximum value.
37784919Sxy150489  *
37794919Sxy150489  * Return configured value of the property, with default, minimum and
37804919Sxy150489  * maximum properly applied.
37813526Sxy150489  */
37823526Sxy150489 static int
37834919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
37843526Sxy150489     char *propname,		/* name of the property */
37853526Sxy150489     int minval,			/* minimum acceptable value */
37863526Sxy150489     int maxval,			/* maximim acceptable value */
37873526Sxy150489     int defval)			/* default value */
37883526Sxy150489 {
37893526Sxy150489 	int propval;		/* value returned for requested property */
37903526Sxy150489 	int *props;		/* point to array of properties returned */
37913526Sxy150489 	uint_t nprops;		/* number of property value returned */
37923526Sxy150489 
37933526Sxy150489 	/*
37943526Sxy150489 	 * get the array of properties from the config file
37953526Sxy150489 	 */
37963526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
37973526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
37983526Sxy150489 		/* got some properties, test if we got enough */
37994919Sxy150489 		if (Adapter->instance < nprops) {
38004919Sxy150489 			propval = props[Adapter->instance];
38013526Sxy150489 		} else {
38023526Sxy150489 			/* not enough properties configured */
38033526Sxy150489 			propval = defval;
38044919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
38053526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
38063526Sxy150489 			    " - set to %d\n",
38073526Sxy150489 			    propname, propval);
38083526Sxy150489 		}
38093526Sxy150489 
38103526Sxy150489 		/* free memory allocated for properties */
38113526Sxy150489 		ddi_prop_free(props);
38123526Sxy150489 
38133526Sxy150489 	} else {
38143526Sxy150489 		propval = defval;
38153526Sxy150489 	}
38163526Sxy150489 
38173526Sxy150489 	/*
38183526Sxy150489 	 * enforce limits
38193526Sxy150489 	 */
38203526Sxy150489 	if (propval > maxval) {
38213526Sxy150489 		propval = maxval;
38224919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
38233526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
38243526Sxy150489 		    propname, propval);
38253526Sxy150489 	}
38263526Sxy150489 
38273526Sxy150489 	if (propval < minval) {
38283526Sxy150489 		propval = minval;
38294919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
38303526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
38313526Sxy150489 		    propname, propval);
38323526Sxy150489 	}
38333526Sxy150489 
38343526Sxy150489 	return (propval);
38353526Sxy150489 }
38363526Sxy150489 
38373526Sxy150489 static boolean_t
38384061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
38393526Sxy150489 {
38404061Sxy150489 	uint16_t speed, duplex, phydata;
38414061Sxy150489 	boolean_t link_changed = B_FALSE;
38423526Sxy150489 	struct e1000_hw *hw;
38433526Sxy150489 	uint32_t reg_tarc;
38443526Sxy150489 
38454919Sxy150489 	hw = &Adapter->shared;
38463526Sxy150489 
38473526Sxy150489 	if (e1000g_link_up(Adapter)) {
38483526Sxy150489 		/*
38493526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
38503526Sxy150489 		 */
38514061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
38527426SChenliang.Xu@Sun.COM 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
38534061Sxy150489 			Adapter->link_speed = speed;
38544061Sxy150489 			Adapter->link_duplex = duplex;
38554061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
38564061Sxy150489 			link_changed = B_TRUE;
38574061Sxy150489 
38588850SMin.Xu@Sun.COM 			if (Adapter->link_speed == SPEED_1000)
38598850SMin.Xu@Sun.COM 				Adapter->stall_threshold = TX_STALL_TIME_2S;
38608850SMin.Xu@Sun.COM 			else
38618850SMin.Xu@Sun.COM 				Adapter->stall_threshold = TX_STALL_TIME_8S;
38628850SMin.Xu@Sun.COM 
38634061Sxy150489 			Adapter->tx_link_down_timeout = 0;
38644061Sxy150489 
38654919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
38664919Sxy150489 			    (hw->mac.type == e1000_82572)) {
38676735Scc210113 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
38684061Sxy150489 				if (speed == SPEED_1000)
38694061Sxy150489 					reg_tarc |= (1 << 21);
38704061Sxy150489 				else
38714061Sxy150489 					reg_tarc &= ~(1 << 21);
38726735Scc210113 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
38733526Sxy150489 			}
38743526Sxy150489 		}
38753526Sxy150489 		Adapter->smartspeed = 0;
38763526Sxy150489 	} else {
38774061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
38783526Sxy150489 			Adapter->link_speed = 0;
38793526Sxy150489 			Adapter->link_duplex = 0;
38804061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
38814061Sxy150489 			link_changed = B_TRUE;
38824061Sxy150489 
38833526Sxy150489 			/*
38843526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
38853526Sxy150489 			 * driver loses link disable auto master/slave
38863526Sxy150489 			 * resolution.
38873526Sxy150489 			 */
38884919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
38897426SChenliang.Xu@Sun.COM 				(void) e1000_read_phy_reg(hw,
38903526Sxy150489 				    PHY_1000T_CTRL, &phydata);
38913526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
38927426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
38933526Sxy150489 				    PHY_1000T_CTRL, phydata);
38943526Sxy150489 			}
38953526Sxy150489 		} else {
38963526Sxy150489 			e1000g_smartspeed(Adapter);
38973526Sxy150489 		}
38984061Sxy150489 
38998479SChenlu.Chen@Sun.COM 		if (Adapter->e1000g_state & E1000G_STARTED) {
39004061Sxy150489 			if (Adapter->tx_link_down_timeout <
39014061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
39024061Sxy150489 				Adapter->tx_link_down_timeout++;
39034061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
39044061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
39054919Sxy150489 				e1000g_tx_clean(Adapter);
39064061Sxy150489 				Adapter->tx_link_down_timeout++;
39074061Sxy150489 			}
39084061Sxy150489 		}
39093526Sxy150489 	}
39103526Sxy150489 
39115273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
39125273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
39135273Sgl147354 
39144061Sxy150489 	return (link_changed);
39154061Sxy150489 }
39164061Sxy150489 
39176394Scc210113 /*
39186394Scc210113  * e1000g_reset_link - Using the link properties to setup the link
39196394Scc210113  */
39206394Scc210113 int
39216394Scc210113 e1000g_reset_link(struct e1000g *Adapter)
39226394Scc210113 {
39236394Scc210113 	struct e1000_mac_info *mac;
39246394Scc210113 	struct e1000_phy_info *phy;
392510605SMin.Xu@Sun.COM 	struct e1000_hw *hw;
39266394Scc210113 	boolean_t invalid;
39276394Scc210113 
39286394Scc210113 	mac = &Adapter->shared.mac;
39296394Scc210113 	phy = &Adapter->shared.phy;
393010605SMin.Xu@Sun.COM 	hw = &Adapter->shared;
39316394Scc210113 	invalid = B_FALSE;
39326394Scc210113 
393310605SMin.Xu@Sun.COM 	if (hw->phy.media_type != e1000_media_type_copper)
393410605SMin.Xu@Sun.COM 		goto out;
393510605SMin.Xu@Sun.COM 
39366394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
39376394Scc210113 		mac->autoneg = B_TRUE;
39386394Scc210113 		phy->autoneg_advertised = 0;
39396394Scc210113 
39406394Scc210113 		/*
39416394Scc210113 		 * 1000hdx is not supported for autonegotiation
39426394Scc210113 		 */
39436394Scc210113 		if (Adapter->param_adv_1000fdx == 1)
39446394Scc210113 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
39456394Scc210113 
39466394Scc210113 		if (Adapter->param_adv_100fdx == 1)
39476394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
39486394Scc210113 
39496394Scc210113 		if (Adapter->param_adv_100hdx == 1)
39506394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
39516394Scc210113 
39526394Scc210113 		if (Adapter->param_adv_10fdx == 1)
39536394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
39546394Scc210113 
39556394Scc210113 		if (Adapter->param_adv_10hdx == 1)
39566394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
39576394Scc210113 
39586394Scc210113 		if (phy->autoneg_advertised == 0)
39596394Scc210113 			invalid = B_TRUE;
39606394Scc210113 	} else {
39616394Scc210113 		mac->autoneg = B_FALSE;
39626394Scc210113 
39636394Scc210113 		/*
396410605SMin.Xu@Sun.COM 		 * For Intel copper cards, 1000fdx and 1000hdx are not
396510605SMin.Xu@Sun.COM 		 * supported for forced link
39666394Scc210113 		 */
39676394Scc210113 		if (Adapter->param_adv_100fdx == 1)
39686394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
39696394Scc210113 		else if (Adapter->param_adv_100hdx == 1)
39706394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
39716394Scc210113 		else if (Adapter->param_adv_10fdx == 1)
39726394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
39736394Scc210113 		else if (Adapter->param_adv_10hdx == 1)
39746394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
39756394Scc210113 		else
39766394Scc210113 			invalid = B_TRUE;
39776394Scc210113 
39786394Scc210113 	}
39796394Scc210113 
39806394Scc210113 	if (invalid) {
39816394Scc210113 		e1000g_log(Adapter, CE_WARN,
3982*10680SMin.Xu@Sun.COM 		    "Invalid link settings. Setup link to "
39836394Scc210113 		    "support autonegotiation with all link capabilities.");
39846394Scc210113 		mac->autoneg = B_TRUE;
3985*10680SMin.Xu@Sun.COM 		phy->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
39866394Scc210113 	}
39876394Scc210113 
398810605SMin.Xu@Sun.COM out:
39896394Scc210113 	return (e1000_setup_link(&Adapter->shared));
39906394Scc210113 }
39916394Scc210113 
39924061Sxy150489 static void
39938275SEric Cheng e1000g_timer_tx_resched(struct e1000g *Adapter)
39948275SEric Cheng {
39958275SEric Cheng 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
39968275SEric Cheng 
39978479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_READER);
39988479SChenlu.Chen@Sun.COM 
39998275SEric Cheng 	if (tx_ring->resched_needed &&
40008275SEric Cheng 	    ((ddi_get_lbolt() - tx_ring->resched_timestamp) >
40018275SEric Cheng 	    drv_usectohz(1000000)) &&
40028479SChenlu.Chen@Sun.COM 	    (Adapter->e1000g_state & E1000G_STARTED) &&
40038275SEric Cheng 	    (tx_ring->tbd_avail >= DEFAULT_TX_NO_RESOURCE)) {
40048275SEric Cheng 		tx_ring->resched_needed = B_FALSE;
40058275SEric Cheng 		mac_tx_update(Adapter->mh);
40068275SEric Cheng 		E1000G_STAT(tx_ring->stat_reschedule);
40078275SEric Cheng 		E1000G_STAT(tx_ring->stat_timer_reschedule);
40088275SEric Cheng 	}
40098479SChenlu.Chen@Sun.COM 
40108479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
40118275SEric Cheng }
40128275SEric Cheng 
40138275SEric Cheng static void
40144919Sxy150489 e1000g_local_timer(void *ws)
40154061Sxy150489 {
40164061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
40174061Sxy150489 	struct e1000_hw *hw;
40184061Sxy150489 	e1000g_ether_addr_t ether_addr;
40194061Sxy150489 	boolean_t link_changed;
40204061Sxy150489 
40214919Sxy150489 	hw = &Adapter->shared;
40224919Sxy150489 
40238479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_ERROR) {
40248479SChenlu.Chen@Sun.COM 		rw_enter(&Adapter->chip_lock, RW_WRITER);
40258479SChenlu.Chen@Sun.COM 		Adapter->e1000g_state &= ~E1000G_ERROR;
40268479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
40278479SChenlu.Chen@Sun.COM 
40285273Sgl147354 		Adapter->reset_count++;
40298275SEric Cheng 		if (e1000g_global_reset(Adapter)) {
40305273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
40315273Sgl147354 			    DDI_SERVICE_RESTORED);
40328275SEric Cheng 			e1000g_timer_tx_resched(Adapter);
40338275SEric Cheng 		} else
40345273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
40355273Sgl147354 			    DDI_SERVICE_LOST);
40365273Sgl147354 		return;
40375273Sgl147354 	}
40385273Sgl147354 
40394061Sxy150489 	if (e1000g_stall_check(Adapter)) {
40404919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
40414061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
40425273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
40438479SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
40444061Sxy150489 		Adapter->reset_count++;
40458275SEric Cheng 		if (e1000g_reset_adapter(Adapter)) {
40465273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
40475273Sgl147354 			    DDI_SERVICE_RESTORED);
40488275SEric Cheng 			e1000g_timer_tx_resched(Adapter);
40498479SChenlu.Chen@Sun.COM 		}
40505273Sgl147354 		return;
40514061Sxy150489 	}
40524061Sxy150489 
40534061Sxy150489 	link_changed = B_FALSE;
40545082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
40554061Sxy150489 	if (Adapter->link_complete)
40564061Sxy150489 		link_changed = e1000g_link_check(Adapter);
40575082Syy150190 	rw_exit(&Adapter->chip_lock);
40584061Sxy150489 
40598850SMin.Xu@Sun.COM 	if (link_changed) {
40608850SMin.Xu@Sun.COM 		if (!Adapter->reset_flag)
40618850SMin.Xu@Sun.COM 			mac_link_update(Adapter->mh, Adapter->link_state);
40628850SMin.Xu@Sun.COM 		if (Adapter->link_state == LINK_STATE_UP)
40638850SMin.Xu@Sun.COM 			Adapter->reset_flag = B_FALSE;
40648850SMin.Xu@Sun.COM 	}
40657133Scc210113 	/*
40667133Scc210113 	 * Workaround for esb2. Data stuck in fifo on a link
40677133Scc210113 	 * down event. Reset the adapter to recover it.
40687133Scc210113 	 */
40697133Scc210113 	if (Adapter->esb2_workaround) {
40707133Scc210113 		Adapter->esb2_workaround = B_FALSE;
40717656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
40728479SChenlu.Chen@Sun.COM 		return;
40734139Sxy150489 	}
40744061Sxy150489 
40753526Sxy150489 	/*
40763526Sxy150489 	 * With 82571 controllers, any locally administered address will
40773526Sxy150489 	 * be overwritten when there is a reset on the other port.
40783526Sxy150489 	 * Detect this circumstance and correct it.
40793526Sxy150489 	 */
40804919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
40814919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
40824919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
40834919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
40843526Sxy150489 
40853526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
40863526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
40873526Sxy150489 
40884919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
40894919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
40904919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
40914919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
40924919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
40934919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
40944919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
40953526Sxy150489 		}
40963526Sxy150489 	}
40973526Sxy150489 
40983526Sxy150489 	/*
40994919Sxy150489 	 * Long TTL workaround for 82541/82547
41003526Sxy150489 	 */
41017426SChenliang.Xu@Sun.COM 	(void) e1000_igp_ttl_workaround_82547(hw);
41023526Sxy150489 
41033526Sxy150489 	/*
41043526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
41053526Sxy150489 	 * change the value in steps...
41063526Sxy150489 	 * These properties should only be set for 10/100
41073526Sxy150489 	 */
41086735Scc210113 	if ((hw->phy.media_type == e1000_media_type_copper) &&
41094061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
41104061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
41113526Sxy150489 		e1000_update_adaptive(hw);
41123526Sxy150489 	}
41133526Sxy150489 	/*
41143526Sxy150489 	 * Set Timer Interrupts
41153526Sxy150489 	 */
41164919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
41174919Sxy150489 
41185273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
41195273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
41208275SEric Cheng 	else
41218275SEric Cheng 		e1000g_timer_tx_resched(Adapter);
41225273Sgl147354 
41234919Sxy150489 	restart_watchdog_timer(Adapter);
41243526Sxy150489 }
41253526Sxy150489 
41264061Sxy150489 /*
41274061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
41284061Sxy150489  * is expired, which indicates the completion of the link setup. The link
41294061Sxy150489  * state will not be updated until the link setup is completed. And the
41304061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
41314061Sxy150489  * in this function. It will be updated in the local timer routine or the
41324061Sxy150489  * interrupt service routine after the interface is started (plumbed).
41334061Sxy150489  */
41343526Sxy150489 static void
41354061Sxy150489 e1000g_link_timer(void *arg)
41363526Sxy150489 {
41374061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
41383526Sxy150489 
41394919Sxy150489 	mutex_enter(&Adapter->link_lock);
41404061Sxy150489 	Adapter->link_complete = B_TRUE;
41414061Sxy150489 	Adapter->link_tid = 0;
41424919Sxy150489 	mutex_exit(&Adapter->link_lock);
41433526Sxy150489 }
41443526Sxy150489 
41453526Sxy150489 /*
41464919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
41474919Sxy150489  *
41484919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
41494919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
41503526Sxy150489  */
41513526Sxy150489 static void
41523526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
41533526Sxy150489 {
41543526Sxy150489 	int forced;
41554919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
41564919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
41573526Sxy150489 
41583526Sxy150489 	/*
41593526Sxy150489 	 * get value out of config file
41603526Sxy150489 	 */
41614919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
41623526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
41633526Sxy150489 
41643526Sxy150489 	switch (forced) {
41653526Sxy150489 	case GDIAG_10_HALF:
41663526Sxy150489 		/*
41673526Sxy150489 		 * Disable Auto Negotiation
41683526Sxy150489 		 */
41694919Sxy150489 		mac->autoneg = B_FALSE;
41704919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
41713526Sxy150489 		break;
41723526Sxy150489 	case GDIAG_10_FULL:
41733526Sxy150489 		/*
41743526Sxy150489 		 * Disable Auto Negotiation
41753526Sxy150489 		 */
41764919Sxy150489 		mac->autoneg = B_FALSE;
41774919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
41783526Sxy150489 		break;
41793526Sxy150489 	case GDIAG_100_HALF:
41803526Sxy150489 		/*
41813526Sxy150489 		 * Disable Auto Negotiation
41823526Sxy150489 		 */
41834919Sxy150489 		mac->autoneg = B_FALSE;
41844919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
41853526Sxy150489 		break;
41863526Sxy150489 	case GDIAG_100_FULL:
41873526Sxy150489 		/*
41883526Sxy150489 		 * Disable Auto Negotiation
41893526Sxy150489 		 */
41904919Sxy150489 		mac->autoneg = B_FALSE;
41914919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
41923526Sxy150489 		break;
41933526Sxy150489 	case GDIAG_1000_FULL:
41943526Sxy150489 		/*
41953526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
41963526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
41973526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
41983526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
41993526Sxy150489 		 * we are allowed to link without any negotiation.
42003526Sxy150489 		 */
42014919Sxy150489 		mac->autoneg = B_TRUE;
42024919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
42033526Sxy150489 		break;
42043526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
42054919Sxy150489 		mac->autoneg = B_TRUE;
42064919Sxy150489 		phy->autoneg_advertised =
42074919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
42084349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
42094349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
42103526Sxy150489 		break;
42113526Sxy150489 	}	/* switch */
42123526Sxy150489 }
42133526Sxy150489 
42143526Sxy150489 /*
42154919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
42164919Sxy150489  *
42174919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
42183526Sxy150489  */
42193526Sxy150489 static void
42203526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
42213526Sxy150489 {
42223526Sxy150489 	int max_frame;
42233526Sxy150489 
42243526Sxy150489 	/*
42253526Sxy150489 	 * get value out of config file
42263526Sxy150489 	 */
42274919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
42283526Sxy150489 
42293526Sxy150489 	switch (max_frame) {
42303526Sxy150489 	case 0:
42316394Scc210113 		Adapter->default_mtu = ETHERMTU;
42323526Sxy150489 		break;
42336394Scc210113 	/*
42346394Scc210113 	 * To avoid excessive memory allocation for rx buffers,
42356394Scc210113 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
42366394Scc210113 	 */
42373526Sxy150489 	case 1:
42386394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
42396394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
42406394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
42413526Sxy150489 		break;
42423526Sxy150489 	case 2:
42436394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
42446394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
42456394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
42463526Sxy150489 		break;
42473526Sxy150489 	case 3:
4248*10680SMin.Xu@Sun.COM 		Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
4249*10680SMin.Xu@Sun.COM 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
4250*10680SMin.Xu@Sun.COM 		    E1000G_IPALIGNPRESERVEROOM;
42513526Sxy150489 		break;
42523526Sxy150489 	default:
42536394Scc210113 		Adapter->default_mtu = ETHERMTU;
42543526Sxy150489 		break;
42553526Sxy150489 	}	/* switch */
42563526Sxy150489 
4257*10680SMin.Xu@Sun.COM 	/*
4258*10680SMin.Xu@Sun.COM 	 * If the user configed MTU is larger than the deivce's maximum MTU,
4259*10680SMin.Xu@Sun.COM 	 * the MTU is set to the deivce's maximum value.
4260*10680SMin.Xu@Sun.COM 	 */
4261*10680SMin.Xu@Sun.COM 	if (Adapter->default_mtu > Adapter->max_mtu)
4262*10680SMin.Xu@Sun.COM 		Adapter->default_mtu = Adapter->max_mtu;
4263*10680SMin.Xu@Sun.COM 
4264*10680SMin.Xu@Sun.COM 	Adapter->max_frame_size = e1000g_mtu2maxframe(Adapter->default_mtu);
4265*10680SMin.Xu@Sun.COM }
4266*10680SMin.Xu@Sun.COM 
4267*10680SMin.Xu@Sun.COM /*
4268*10680SMin.Xu@Sun.COM  * e1000g_pch_limits - Apply limits of the PCH silicon type
4269*10680SMin.Xu@Sun.COM  *
4270*10680SMin.Xu@Sun.COM  * At any frame size larger than the ethernet default,
4271*10680SMin.Xu@Sun.COM  * prevent linking at 10/100 speeds.
4272*10680SMin.Xu@Sun.COM  */
4273*10680SMin.Xu@Sun.COM static void
4274*10680SMin.Xu@Sun.COM e1000g_pch_limits(struct e1000g *Adapter)
4275*10680SMin.Xu@Sun.COM {
4276*10680SMin.Xu@Sun.COM 	struct e1000_hw *hw = &Adapter->shared;
4277*10680SMin.Xu@Sun.COM 
4278*10680SMin.Xu@Sun.COM 	/* only applies to PCH silicon type */
4279*10680SMin.Xu@Sun.COM 	if (hw->mac.type != e1000_pchlan)
4280*10680SMin.Xu@Sun.COM 		return;
4281*10680SMin.Xu@Sun.COM 
4282*10680SMin.Xu@Sun.COM 	/* only applies to frames larger than ethernet default */
4283*10680SMin.Xu@Sun.COM 	if (Adapter->max_frame_size > DEFAULT_FRAME_SIZE) {
4284*10680SMin.Xu@Sun.COM 		hw->mac.autoneg = B_TRUE;
4285*10680SMin.Xu@Sun.COM 		hw->phy.autoneg_advertised = ADVERTISE_1000_FULL;
4286*10680SMin.Xu@Sun.COM 
4287*10680SMin.Xu@Sun.COM 		Adapter->param_adv_autoneg = 1;
4288*10680SMin.Xu@Sun.COM 		Adapter->param_adv_1000fdx = 1;
4289*10680SMin.Xu@Sun.COM 
4290*10680SMin.Xu@Sun.COM 		Adapter->param_adv_100fdx = 0;
4291*10680SMin.Xu@Sun.COM 		Adapter->param_adv_100hdx = 0;
4292*10680SMin.Xu@Sun.COM 		Adapter->param_adv_10fdx = 0;
4293*10680SMin.Xu@Sun.COM 		Adapter->param_adv_10hdx = 0;
4294*10680SMin.Xu@Sun.COM 
4295*10680SMin.Xu@Sun.COM 		e1000g_param_sync(Adapter);
4296*10680SMin.Xu@Sun.COM 	}
4297*10680SMin.Xu@Sun.COM }
4298*10680SMin.Xu@Sun.COM 
4299*10680SMin.Xu@Sun.COM /*
4300*10680SMin.Xu@Sun.COM  * e1000g_mtu2maxframe - convert given MTU to maximum frame size
4301*10680SMin.Xu@Sun.COM  */
4302*10680SMin.Xu@Sun.COM static uint32_t
4303*10680SMin.Xu@Sun.COM e1000g_mtu2maxframe(uint32_t mtu)
4304*10680SMin.Xu@Sun.COM {
4305*10680SMin.Xu@Sun.COM 	uint32_t maxframe;
4306*10680SMin.Xu@Sun.COM 
4307*10680SMin.Xu@Sun.COM 	maxframe = mtu + sizeof (struct ether_vlan_header) + ETHERFCSL;
4308*10680SMin.Xu@Sun.COM 
4309*10680SMin.Xu@Sun.COM 	return (maxframe);
43103526Sxy150489 }
43113526Sxy150489 
43123526Sxy150489 static void
43134919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
43143526Sxy150489 {
43154919Sxy150489 	Adapter->watchdog_tid =
43164919Sxy150489 	    timeout(e1000g_local_timer,
43173526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
43183526Sxy150489 }
43194919Sxy150489 #pragma inline(arm_watchdog_timer)
43204919Sxy150489 
43214919Sxy150489 static void
43224919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
43234919Sxy150489 {
43244919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
43254919Sxy150489 
43264919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
43274919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
43284919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
43294919Sxy150489 		arm_watchdog_timer(Adapter);
43304919Sxy150489 	}
43314919Sxy150489 
43324919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
43334919Sxy150489 }
43343526Sxy150489 
43353526Sxy150489 static void
43364919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
43373526Sxy150489 {
43383526Sxy150489 	timeout_id_t tid;
43393526Sxy150489 
43404919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
43414919Sxy150489 
43424919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
43434919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
43444919Sxy150489 	tid = Adapter->watchdog_tid;
43454919Sxy150489 	Adapter->watchdog_tid = 0;
43464919Sxy150489 
43474919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
43483526Sxy150489 
43493526Sxy150489 	if (tid != 0)
43503526Sxy150489 		(void) untimeout(tid);
43513526Sxy150489 }
43523526Sxy150489 
43533526Sxy150489 static void
43544919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
43553526Sxy150489 {
43564919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
43574919Sxy150489 
43584919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
43594919Sxy150489 		if (!Adapter->watchdog_timer_started) {
43604919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
43614919Sxy150489 			arm_watchdog_timer(Adapter);
43623526Sxy150489 		}
43633526Sxy150489 	}
43643526Sxy150489 
43654919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
43664919Sxy150489 }
43674919Sxy150489 
43684919Sxy150489 static void
43694919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
43704919Sxy150489 {
43714919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
43724919Sxy150489 
43734919Sxy150489 	if (Adapter->watchdog_timer_started)
43744919Sxy150489 		arm_watchdog_timer(Adapter);
43754919Sxy150489 
43764919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
43773526Sxy150489 }
43783526Sxy150489 
43793526Sxy150489 static void
43804919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
43813526Sxy150489 {
43824919Sxy150489 	timeout_id_t tid;
43834919Sxy150489 
43844919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
43854919Sxy150489 
43864919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
43874919Sxy150489 	tid = Adapter->watchdog_tid;
43884919Sxy150489 	Adapter->watchdog_tid = 0;
43894919Sxy150489 
43904919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
43914919Sxy150489 
43924919Sxy150489 	if (tid != 0)
43934919Sxy150489 		(void) untimeout(tid);
43943526Sxy150489 }
43953526Sxy150489 
43963526Sxy150489 static void
43974919Sxy150489 stop_link_timer(struct e1000g *Adapter)
43983526Sxy150489 {
43993526Sxy150489 	timeout_id_t tid;
44003526Sxy150489 
44014919Sxy150489 	/* Disable the link timer */
44024919Sxy150489 	mutex_enter(&Adapter->link_lock);
44034919Sxy150489 
44044919Sxy150489 	tid = Adapter->link_tid;
44054919Sxy150489 	Adapter->link_tid = 0;
44064919Sxy150489 
44074919Sxy150489 	mutex_exit(&Adapter->link_lock);
44084919Sxy150489 
44094919Sxy150489 	if (tid != 0)
44104919Sxy150489 		(void) untimeout(tid);
44114919Sxy150489 }
44124919Sxy150489 
44134919Sxy150489 static void
44144919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
44154919Sxy150489 {
44164919Sxy150489 	timeout_id_t tid;
44174919Sxy150489 
44184919Sxy150489 	/* Disable the tx timer for 82547 chipset */
44194919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
44204919Sxy150489 
44214919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
44224919Sxy150489 	tid = tx_ring->timer_id_82547;
44234919Sxy150489 	tx_ring->timer_id_82547 = 0;
44244919Sxy150489 
44254919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
44263526Sxy150489 
44273526Sxy150489 	if (tid != 0)
44283526Sxy150489 		(void) untimeout(tid);
44293526Sxy150489 }
44303526Sxy150489 
44313526Sxy150489 void
44324919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
44333526Sxy150489 {
44344919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
44354919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
44363526Sxy150489 }
44373526Sxy150489 
44383526Sxy150489 void
44394919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
44403526Sxy150489 {
44414919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
44425882Syy150190 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
44435882Syy150190 
44445882Syy150190 	if (Adapter->tx_intr_enable)
44455882Syy150190 		e1000g_mask_tx_interrupt(Adapter);
44463526Sxy150489 }
44473526Sxy150489 
44487656SSherry.Moore@Sun.COM /*
44497656SSherry.Moore@Sun.COM  * This routine is called by e1000g_quiesce(), therefore must not block.
44507656SSherry.Moore@Sun.COM  */
44513526Sxy150489 void
44524919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
44533526Sxy150489 {
44544919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
44553526Sxy150489 }
44563526Sxy150489 
44573526Sxy150489 void
44584919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
44593526Sxy150489 {
44605882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
44613526Sxy150489 }
44623526Sxy150489 
44633526Sxy150489 void
44644919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
44653526Sxy150489 {
44665882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
44673526Sxy150489 }
44683526Sxy150489 
44693526Sxy150489 static void
44704919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
44713526Sxy150489 {
44724919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
44733526Sxy150489 	uint16_t phy_status;
44743526Sxy150489 	uint16_t phy_ctrl;
44753526Sxy150489 
44763526Sxy150489 	/*
44773526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
44783526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
44793526Sxy150489 	 */
44804919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
44814919Sxy150489 	    !hw->mac.autoneg ||
44824919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
44833526Sxy150489 		return;
44843526Sxy150489 
44853526Sxy150489 	/*
44863526Sxy150489 	 * True if this is the first call of this function or after every
44873526Sxy150489 	 * 30 seconds of not having link
44883526Sxy150489 	 */
44894919Sxy150489 	if (Adapter->smartspeed == 0) {
44903526Sxy150489 		/*
44913526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
44923526Sxy150489 		 * assume back-to-back
44933526Sxy150489 		 */
44947426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
44953526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
44963526Sxy150489 			return;
44973526Sxy150489 
44987426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
44993526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
45003526Sxy150489 			return;
45013526Sxy150489 		/*
45023526Sxy150489 		 * We're assuming back-2-back because our status register
45033526Sxy150489 		 * insists! there's a fault in the master/slave
45043526Sxy150489 		 * relationship that was "negotiated"
45053526Sxy150489 		 */
45067426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
45073526Sxy150489 		/*
45083526Sxy150489 		 * Is the phy configured for manual configuration of
45093526Sxy150489 		 * master/slave?
45103526Sxy150489 		 */
45113526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
45123526Sxy150489 			/*
45133526Sxy150489 			 * Yes.  Then disable manual configuration (enable
45143526Sxy150489 			 * auto configuration) of master/slave
45153526Sxy150489 			 */
45163526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
45177426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw,
45183526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
45193526Sxy150489 			/*
45203526Sxy150489 			 * Effectively starting the clock
45213526Sxy150489 			 */
45224919Sxy150489 			Adapter->smartspeed++;
45233526Sxy150489 			/*
45243526Sxy150489 			 * Restart autonegotiation
45253526Sxy150489 			 */
45264919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
45274919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
45283526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
45293526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
45307426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
45314919Sxy150489 				    PHY_CONTROL, phy_ctrl);
45323526Sxy150489 			}
45333526Sxy150489 		}
45343526Sxy150489 		return;
45353526Sxy150489 		/*
45363526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
45373526Sxy150489 		 * you should reset the smartspeed counter once you obtain
45383526Sxy150489 		 * link
45393526Sxy150489 		 */
45404919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
45413526Sxy150489 		/*
45423526Sxy150489 		 * Yes.  Remember, we did at the start determine that
45433526Sxy150489 		 * there's a master/slave configuration fault, so we're
45443526Sxy150489 		 * still assuming there's someone on the other end, but we
45453526Sxy150489 		 * just haven't yet been able to talk to it. We then
45463526Sxy150489 		 * re-enable auto configuration of master/slave to see if
45473526Sxy150489 		 * we're running 2/3 pair cables.
45483526Sxy150489 		 */
45493526Sxy150489 		/*
45503526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
45513526Sxy150489 		 */
45527426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
45533526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
45547426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
45553526Sxy150489 		/*
45563526Sxy150489 		 * Restart autoneg with phy enabled for manual
45573526Sxy150489 		 * configuration of master/slave
45583526Sxy150489 		 */
45594919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
45604919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
45613526Sxy150489 			phy_ctrl |=
45623526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
45637426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
45643526Sxy150489 		}
45653526Sxy150489 		/*
45663526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
45673526Sxy150489 		 * link as a result.
45683526Sxy150489 		 */
45693526Sxy150489 	}
45703526Sxy150489 	/*
45713526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
45723526Sxy150489 	 * seconds)
45733526Sxy150489 	 */
45744919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
45754919Sxy150489 		Adapter->smartspeed = 0;
45763526Sxy150489 }
45773526Sxy150489 
45783526Sxy150489 static boolean_t
45793526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
45803526Sxy150489 {
45813526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
45823526Sxy150489 	const uint8_t addr_test2[6] =
45833526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
45843526Sxy150489 
45853526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
45863526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
45873526Sxy150489 		return (B_FALSE);
45883526Sxy150489 
45893526Sxy150489 	return (B_TRUE);
45903526Sxy150489 }
45913526Sxy150489 
45923526Sxy150489 /*
45934919Sxy150489  * e1000g_stall_check - check for tx stall
45944919Sxy150489  *
45954919Sxy150489  * This function checks if the adapter is stalled (in transmit).
45964919Sxy150489  *
45974919Sxy150489  * It is called each time the watchdog timeout is invoked.
45984919Sxy150489  * If the transmit descriptor reclaim continuously fails,
45994919Sxy150489  * the watchdog value will increment by 1. If the watchdog
46004919Sxy150489  * value exceeds the threshold, the adapter is assumed to
46014919Sxy150489  * have stalled and need to be reset.
46023526Sxy150489  */
46033526Sxy150489 static boolean_t
46043526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
46053526Sxy150489 {
46064919Sxy150489 	e1000g_tx_ring_t *tx_ring;
46074919Sxy150489 
46084919Sxy150489 	tx_ring = Adapter->tx_ring;
46094919Sxy150489 
46104061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
46113526Sxy150489 		return (B_FALSE);
46123526Sxy150489 
46138850SMin.Xu@Sun.COM 	(void) e1000g_recycle(tx_ring);
46148850SMin.Xu@Sun.COM 
46158850SMin.Xu@Sun.COM 	if (Adapter->stall_flag) {
46168850SMin.Xu@Sun.COM 		Adapter->stall_flag = B_FALSE;
46178850SMin.Xu@Sun.COM 		Adapter->reset_flag = B_TRUE;
46188850SMin.Xu@Sun.COM 		return (B_TRUE);
46198850SMin.Xu@Sun.COM 	}
46208850SMin.Xu@Sun.COM 
46218850SMin.Xu@Sun.COM 	return (B_FALSE);
46223526Sxy150489 }
46233526Sxy150489 
46244919Sxy150489 #ifdef E1000G_DEBUG
46253526Sxy150489 static enum ioc_reply
46263526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
46273526Sxy150489 {
46283526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
46293526Sxy150489 	e1000g_peekpoke_t *ppd;
46303526Sxy150489 	uint64_t mem_va;
46313526Sxy150489 	uint64_t maxoff;
46323526Sxy150489 	boolean_t peek;
46333526Sxy150489 
46343526Sxy150489 	switch (iocp->ioc_cmd) {
46353526Sxy150489 
46363526Sxy150489 	case E1000G_IOC_REG_PEEK:
46373526Sxy150489 		peek = B_TRUE;
46383526Sxy150489 		break;
46393526Sxy150489 
46403526Sxy150489 	case E1000G_IOC_REG_POKE:
46413526Sxy150489 		peek = B_FALSE;
46423526Sxy150489 		break;
46433526Sxy150489 
46443526Sxy150489 	deault:
46454919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
46464349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
46474349Sxy150489 		    iocp->ioc_cmd);
46483526Sxy150489 		return (IOC_INVAL);
46493526Sxy150489 	}
46503526Sxy150489 
46513526Sxy150489 	/*
46523526Sxy150489 	 * Validate format of ioctl
46533526Sxy150489 	 */
46543526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
46553526Sxy150489 		return (IOC_INVAL);
46563526Sxy150489 	if (mp->b_cont == NULL)
46573526Sxy150489 		return (IOC_INVAL);
46583526Sxy150489 
46597426SChenliang.Xu@Sun.COM 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
46603526Sxy150489 
46613526Sxy150489 	/*
46623526Sxy150489 	 * Validate request parameters
46633526Sxy150489 	 */
46643526Sxy150489 	switch (ppd->pp_acc_space) {
46653526Sxy150489 
46663526Sxy150489 	default:
46674919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
46684349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
46694349Sxy150489 		    ppd->pp_acc_space);
46703526Sxy150489 		return (IOC_INVAL);
46713526Sxy150489 
46723526Sxy150489 	case E1000G_PP_SPACE_REG:
46733526Sxy150489 		/*
46743526Sxy150489 		 * Memory-mapped I/O space
46753526Sxy150489 		 */
46763526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
46773526Sxy150489 		if (ppd->pp_acc_size != 4)
46783526Sxy150489 			return (IOC_INVAL);
46793526Sxy150489 
46803526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
46813526Sxy150489 			return (IOC_INVAL);
46823526Sxy150489 
46833526Sxy150489 		mem_va = 0;
46843526Sxy150489 		maxoff = 0x10000;
46853526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
46863526Sxy150489 		break;
46873526Sxy150489 
46883526Sxy150489 	case E1000G_PP_SPACE_E1000G:
46893526Sxy150489 		/*
46903526Sxy150489 		 * E1000g data structure!
46913526Sxy150489 		 */
46923526Sxy150489 		mem_va = (uintptr_t)e1000gp;
46933526Sxy150489 		maxoff = sizeof (struct e1000g);
46943526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
46953526Sxy150489 		break;
46963526Sxy150489 
46973526Sxy150489 	}
46983526Sxy150489 
46993526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
47003526Sxy150489 		return (IOC_INVAL);
47013526Sxy150489 
47023526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
47033526Sxy150489 		return (IOC_INVAL);
47043526Sxy150489 
47053526Sxy150489 	/*
47063526Sxy150489 	 * All OK - go!
47073526Sxy150489 	 */
47083526Sxy150489 	ppd->pp_acc_offset += mem_va;
47093526Sxy150489 	(*ppfn)(e1000gp, ppd);
47103526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
47113526Sxy150489 }
47123526Sxy150489 
47133526Sxy150489 static void
47143526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
47153526Sxy150489 {
47163526Sxy150489 	ddi_acc_handle_t handle;
47173526Sxy150489 	uint32_t *regaddr;
47183526Sxy150489 
47194919Sxy150489 	handle = e1000gp->osdep.reg_handle;
47207426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
47217426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
47223526Sxy150489 
47233526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
47243526Sxy150489 }
47253526Sxy150489 
47263526Sxy150489 static void
47273526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
47283526Sxy150489 {
47293526Sxy150489 	ddi_acc_handle_t handle;
47303526Sxy150489 	uint32_t *regaddr;
47313526Sxy150489 	uint32_t value;
47323526Sxy150489 
47334919Sxy150489 	handle = e1000gp->osdep.reg_handle;
47347426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
47357426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
47363526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
47373526Sxy150489 
47383526Sxy150489 	ddi_put32(handle, regaddr, value);
47393526Sxy150489 }
47403526Sxy150489 
47413526Sxy150489 static void
47423526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
47433526Sxy150489 {
47443526Sxy150489 	uint64_t value;
47453526Sxy150489 	void *vaddr;
47463526Sxy150489 
47473526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
47483526Sxy150489 
47493526Sxy150489 	switch (ppd->pp_acc_size) {
47503526Sxy150489 	case 1:
47513526Sxy150489 		value = *(uint8_t *)vaddr;
47523526Sxy150489 		break;
47533526Sxy150489 
47543526Sxy150489 	case 2:
47553526Sxy150489 		value = *(uint16_t *)vaddr;
47563526Sxy150489 		break;
47573526Sxy150489 
47583526Sxy150489 	case 4:
47593526Sxy150489 		value = *(uint32_t *)vaddr;
47603526Sxy150489 		break;
47613526Sxy150489 
47623526Sxy150489 	case 8:
47633526Sxy150489 		value = *(uint64_t *)vaddr;
47643526Sxy150489 		break;
47653526Sxy150489 	}
47663526Sxy150489 
47674919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
47684349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
47694349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
47703526Sxy150489 
47713526Sxy150489 	ppd->pp_acc_data = value;
47723526Sxy150489 }
47733526Sxy150489 
47743526Sxy150489 static void
47753526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
47763526Sxy150489 {
47773526Sxy150489 	uint64_t value;
47783526Sxy150489 	void *vaddr;
47793526Sxy150489 
47803526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
47813526Sxy150489 	value = ppd->pp_acc_data;
47823526Sxy150489 
47834919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
47844349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
47854349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
47863526Sxy150489 
47873526Sxy150489 	switch (ppd->pp_acc_size) {
47883526Sxy150489 	case 1:
47893526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
47903526Sxy150489 		break;
47913526Sxy150489 
47923526Sxy150489 	case 2:
47933526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
47943526Sxy150489 		break;
47953526Sxy150489 
47963526Sxy150489 	case 4:
47973526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
47983526Sxy150489 		break;
47993526Sxy150489 
48003526Sxy150489 	case 8:
48013526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
48023526Sxy150489 		break;
48033526Sxy150489 	}
48043526Sxy150489 }
48054919Sxy150489 #endif
48063526Sxy150489 
48073526Sxy150489 /*
48083526Sxy150489  * Loopback Support
48093526Sxy150489  */
48103526Sxy150489 static lb_property_t lb_normal =
48113526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
48123526Sxy150489 static lb_property_t lb_external1000 =
48133526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
48143526Sxy150489 static lb_property_t lb_external100 =
48153526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
48163526Sxy150489 static lb_property_t lb_external10 =
48173526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
48183526Sxy150489 static lb_property_t lb_phy =
48193526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
48203526Sxy150489 
48213526Sxy150489 static enum ioc_reply
48223526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
48233526Sxy150489 {
48243526Sxy150489 	lb_info_sz_t *lbsp;
48253526Sxy150489 	lb_property_t *lbpp;
48263526Sxy150489 	struct e1000_hw *hw;
48273526Sxy150489 	uint32_t *lbmp;
48283526Sxy150489 	uint32_t size;
48293526Sxy150489 	uint32_t value;
48303526Sxy150489 
48314919Sxy150489 	hw = &Adapter->shared;
48323526Sxy150489 
48333526Sxy150489 	if (mp->b_cont == NULL)
48343526Sxy150489 		return (IOC_INVAL);
48353526Sxy150489 
48367133Scc210113 	if (!e1000g_check_loopback_support(hw)) {
48377133Scc210113 		e1000g_log(NULL, CE_WARN,
48387133Scc210113 		    "Loopback is not supported on e1000g%d", Adapter->instance);
48397133Scc210113 		return (IOC_INVAL);
48407133Scc210113 	}
48417133Scc210113 
48423526Sxy150489 	switch (iocp->ioc_cmd) {
48433526Sxy150489 	default:
48443526Sxy150489 		return (IOC_INVAL);
48453526Sxy150489 
48463526Sxy150489 	case LB_GET_INFO_SIZE:
48473526Sxy150489 		size = sizeof (lb_info_sz_t);
48483526Sxy150489 		if (iocp->ioc_count != size)
48493526Sxy150489 			return (IOC_INVAL);
48503526Sxy150489 
48515082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
48525082Syy150190 		e1000g_get_phy_state(Adapter);
48535082Syy150190 
48545082Syy150190 		/*
48555082Syy150190 		 * Workaround for hardware faults. In order to get a stable
48565082Syy150190 		 * state of phy, we will wait for a specific interval and
48575082Syy150190 		 * try again. The time delay is an experiential value based
48585082Syy150190 		 * on our testing.
48595082Syy150190 		 */
48605082Syy150190 		msec_delay(100);
48615082Syy150190 		e1000g_get_phy_state(Adapter);
48625082Syy150190 		rw_exit(&Adapter->chip_lock);
48633526Sxy150489 
48643526Sxy150489 		value = sizeof (lb_normal);
48655082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
48665082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
48676735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
48686735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
48693526Sxy150489 			value += sizeof (lb_phy);
48704919Sxy150489 			switch (hw->mac.type) {
48713526Sxy150489 			case e1000_82571:
48723526Sxy150489 			case e1000_82572:
48737133Scc210113 			case e1000_80003es2lan:
48743526Sxy150489 				value += sizeof (lb_external1000);
48753526Sxy150489 				break;
48763526Sxy150489 			}
48773526Sxy150489 		}
48785082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
48795082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
48803526Sxy150489 			value += sizeof (lb_external100);
48815082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
48823526Sxy150489 			value += sizeof (lb_external10);
48833526Sxy150489 
48847426SChenliang.Xu@Sun.COM 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
48853526Sxy150489 		*lbsp = value;
48863526Sxy150489 		break;
48873526Sxy150489 
48883526Sxy150489 	case LB_GET_INFO:
48893526Sxy150489 		value = sizeof (lb_normal);
48905082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
48915082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
48926735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
48936735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
48943526Sxy150489 			value += sizeof (lb_phy);
48954919Sxy150489 			switch (hw->mac.type) {
48963526Sxy150489 			case e1000_82571:
48973526Sxy150489 			case e1000_82572:
48987133Scc210113 			case e1000_80003es2lan:
48993526Sxy150489 				value += sizeof (lb_external1000);
49003526Sxy150489 				break;
49013526Sxy150489 			}
49023526Sxy150489 		}
49035082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
49045082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
49053526Sxy150489 			value += sizeof (lb_external100);
49065082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
49073526Sxy150489 			value += sizeof (lb_external10);
49083526Sxy150489 
49093526Sxy150489 		size = value;
49103526Sxy150489 		if (iocp->ioc_count != size)
49113526Sxy150489 			return (IOC_INVAL);
49123526Sxy150489 
49133526Sxy150489 		value = 0;
49147426SChenliang.Xu@Sun.COM 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
49153526Sxy150489 		lbpp[value++] = lb_normal;
49165082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
49175082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
49186735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
49196735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
49203526Sxy150489 			lbpp[value++] = lb_phy;
49214919Sxy150489 			switch (hw->mac.type) {
49223526Sxy150489 			case e1000_82571:
49233526Sxy150489 			case e1000_82572:
49247133Scc210113 			case e1000_80003es2lan:
49253526Sxy150489 				lbpp[value++] = lb_external1000;
49263526Sxy150489 				break;
49273526Sxy150489 			}
49283526Sxy150489 		}
49295082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
49305082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
49313526Sxy150489 			lbpp[value++] = lb_external100;
49325082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
49333526Sxy150489 			lbpp[value++] = lb_external10;
49343526Sxy150489 		break;
49353526Sxy150489 
49363526Sxy150489 	case LB_GET_MODE:
49373526Sxy150489 		size = sizeof (uint32_t);
49383526Sxy150489 		if (iocp->ioc_count != size)
49393526Sxy150489 			return (IOC_INVAL);
49403526Sxy150489 
49417426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
49423526Sxy150489 		*lbmp = Adapter->loopback_mode;
49433526Sxy150489 		break;
49443526Sxy150489 
49453526Sxy150489 	case LB_SET_MODE:
49463526Sxy150489 		size = 0;
49473526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
49483526Sxy150489 			return (IOC_INVAL);
49493526Sxy150489 
49507426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
49513526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
49523526Sxy150489 			return (IOC_INVAL);
49533526Sxy150489 		break;
49543526Sxy150489 	}
49553526Sxy150489 
49563526Sxy150489 	iocp->ioc_count = size;
49573526Sxy150489 	iocp->ioc_error = 0;
49583526Sxy150489 
49595273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
49605273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
49615273Sgl147354 		return (IOC_INVAL);
49625273Sgl147354 	}
49635273Sgl147354 
49643526Sxy150489 	return (IOC_REPLY);
49653526Sxy150489 }
49663526Sxy150489 
49673526Sxy150489 static boolean_t
49687133Scc210113 e1000g_check_loopback_support(struct e1000_hw *hw)
49697133Scc210113 {
49707133Scc210113 	switch (hw->mac.type) {
49717133Scc210113 	case e1000_82540:
49727133Scc210113 	case e1000_82545:
49737133Scc210113 	case e1000_82545_rev_3:
49747133Scc210113 	case e1000_82546:
49757133Scc210113 	case e1000_82546_rev_3:
49767133Scc210113 	case e1000_82541:
49777133Scc210113 	case e1000_82541_rev_2:
49787133Scc210113 	case e1000_82547:
49797133Scc210113 	case e1000_82547_rev_2:
49807133Scc210113 	case e1000_82571:
49817133Scc210113 	case e1000_82572:
49827133Scc210113 	case e1000_82573:
49838479SChenlu.Chen@Sun.COM 	case e1000_82574:
49847133Scc210113 	case e1000_80003es2lan:
49858479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
49868479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
49877133Scc210113 		return (B_TRUE);
49887133Scc210113 	}
49897133Scc210113 	return (B_FALSE);
49907133Scc210113 }
49917133Scc210113 
49927133Scc210113 static boolean_t
49933526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
49943526Sxy150489 {
49953526Sxy150489 	struct e1000_hw *hw;
49963526Sxy150489 	int i, times;
49975082Syy150190 	boolean_t link_up;
49983526Sxy150489 
49993526Sxy150489 	if (mode == Adapter->loopback_mode)
50003526Sxy150489 		return (B_TRUE);
50013526Sxy150489 
50024919Sxy150489 	hw = &Adapter->shared;
50033526Sxy150489 	times = 0;
50043526Sxy150489 
50055082Syy150190 	Adapter->loopback_mode = mode;
50065082Syy150190 
50075082Syy150190 	if (mode == E1000G_LB_NONE) {
50083526Sxy150489 		/* Reset the chip */
50096735Scc210113 		hw->phy.autoneg_wait_to_complete = B_TRUE;
50107656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
50116735Scc210113 		hw->phy.autoneg_wait_to_complete = B_FALSE;
50125082Syy150190 		return (B_TRUE);
50135082Syy150190 	}
50145082Syy150190 
50155082Syy150190 again:
50165082Syy150190 
50175082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
50185082Syy150190 
50195082Syy150190 	switch (mode) {
50205082Syy150190 	default:
50215082Syy150190 		rw_exit(&Adapter->chip_lock);
50225082Syy150190 		return (B_FALSE);
50233526Sxy150489 
50243526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
50253526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
50263526Sxy150489 		break;
50273526Sxy150489 
50283526Sxy150489 	case E1000G_LB_EXTERNAL_100:
50293526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
50303526Sxy150489 		break;
50313526Sxy150489 
50323526Sxy150489 	case E1000G_LB_EXTERNAL_10:
50333526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
50343526Sxy150489 		break;
50353526Sxy150489 
50363526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
50373526Sxy150489 		e1000g_set_internal_loopback(Adapter);
50383526Sxy150489 		break;
50393526Sxy150489 	}
50403526Sxy150489 
50413526Sxy150489 	times++;
50423526Sxy150489 
50435858Scc210113 	rw_exit(&Adapter->chip_lock);
50445858Scc210113 
50455082Syy150190 	/* Wait for link up */
50465082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
50475082Syy150190 		msec_delay(100);
50485082Syy150190 
50495858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
50505858Scc210113 
50515082Syy150190 	link_up = e1000g_link_up(Adapter);
50525082Syy150190 
50535082Syy150190 	rw_exit(&Adapter->chip_lock);
50545082Syy150190 
50555082Syy150190 	if (!link_up) {
50565082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
50575082Syy150190 		    "Failed to get the link up");
50585082Syy150190 		if (times < 2) {
50595082Syy150190 			/* Reset the link */
50604919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
50615082Syy150190 			    "Reset the link ...");
50627656SSherry.Moore@Sun.COM 			(void) e1000g_reset_adapter(Adapter);
50635082Syy150190 			goto again;
50643526Sxy150489 		}
50653526Sxy150489 	}
50663526Sxy150489 
50673526Sxy150489 	return (B_TRUE);
50683526Sxy150489 }
50693526Sxy150489 
50703526Sxy150489 /*
50713526Sxy150489  * The following loopback settings are from Intel's technical
50723526Sxy150489  * document - "How To Loopback". All the register settings and
50733526Sxy150489  * time delay values are directly inherited from the document
50743526Sxy150489  * without more explanations available.
50753526Sxy150489  */
50763526Sxy150489 static void
50773526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
50783526Sxy150489 {
50793526Sxy150489 	struct e1000_hw *hw;
50803526Sxy150489 	uint32_t ctrl;
50813526Sxy150489 	uint32_t status;
50823526Sxy150489 	uint16_t phy_ctrl;
50838479SChenlu.Chen@Sun.COM 	uint16_t phy_reg;
50845082Syy150190 	uint32_t txcw;
50853526Sxy150489 
50864919Sxy150489 	hw = &Adapter->shared;
50873526Sxy150489 
50883526Sxy150489 	/* Disable Smart Power Down */
50893526Sxy150489 	phy_spd_state(hw, B_FALSE);
50903526Sxy150489 
50917426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
50923526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
50933526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
50943526Sxy150489 
50954919Sxy150489 	switch (hw->mac.type) {
50963526Sxy150489 	case e1000_82540:
50973526Sxy150489 	case e1000_82545:
50983526Sxy150489 	case e1000_82545_rev_3:
50993526Sxy150489 	case e1000_82546:
51003526Sxy150489 	case e1000_82546_rev_3:
51013526Sxy150489 	case e1000_82573:
51023526Sxy150489 		/* Auto-MDI/MDIX off */
51037426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
51043526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
51057426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
51064349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
51073526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
51087426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
51094349Sxy150489 		    phy_ctrl | MII_CR_RESET);
51105082Syy150190 		/*
51115082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
51125082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
51135082Syy150190 		 * background.
51145082Syy150190 		 */
51157426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
51167426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
51177426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
51187426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
51193526Sxy150489 		break;
51207133Scc210113 	case e1000_80003es2lan:
51217133Scc210113 		/* Force Link Up */
51227426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
51237426SChenliang.Xu@Sun.COM 		    0x1CC);
51247133Scc210113 		/* Sets PCS loopback at 1Gbs */
51257426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
51267426SChenliang.Xu@Sun.COM 		    0x1046);
51277133Scc210113 		break;
51283526Sxy150489 	}
51293526Sxy150489 
51308479SChenlu.Chen@Sun.COM 	/*
51318479SChenlu.Chen@Sun.COM 	 * The following registers should be set for e1000_phy_bm phy type.
51328479SChenlu.Chen@Sun.COM 	 * e1000_82574, e1000_ich10lan and some e1000_ich9lan use this phy.
51338479SChenlu.Chen@Sun.COM 	 * For others, we do not need to set these registers.
51348479SChenlu.Chen@Sun.COM 	 */
51358479SChenlu.Chen@Sun.COM 	if (hw->phy.type == e1000_phy_bm) {
51368479SChenlu.Chen@Sun.COM 		/* Set Default MAC Interface speed to 1GB */
51378850SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_REG(2, 21), &phy_reg);
51388479SChenlu.Chen@Sun.COM 		phy_reg &= ~0x0007;
51398479SChenlu.Chen@Sun.COM 		phy_reg |= 0x006;
51408850SMin.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_REG(2, 21), phy_reg);
51418479SChenlu.Chen@Sun.COM 		/* Assert SW reset for above settings to take effect */
51428850SMin.Xu@Sun.COM 		(void) e1000_phy_commit(hw);
51438479SChenlu.Chen@Sun.COM 		msec_delay(1);
51448479SChenlu.Chen@Sun.COM 		/* Force Full Duplex */
51458850SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
51468850SMin.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
51478850SMin.Xu@Sun.COM 		    phy_reg | 0x000C);
51488479SChenlu.Chen@Sun.COM 		/* Set Link Up (in force link) */
51498850SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_REG(776, 16), &phy_reg);
51508850SMin.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_REG(776, 16),
51518850SMin.Xu@Sun.COM 		    phy_reg | 0x0040);
51528479SChenlu.Chen@Sun.COM 		/* Force Link */
51538850SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
51548850SMin.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
51558850SMin.Xu@Sun.COM 		    phy_reg | 0x0040);
51568479SChenlu.Chen@Sun.COM 		/* Set Early Link Enable */
51578850SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 20), &phy_reg);
51588850SMin.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 20),
51598850SMin.Xu@Sun.COM 		    phy_reg | 0x0400);
51608479SChenlu.Chen@Sun.COM 	}
51618479SChenlu.Chen@Sun.COM 
51623526Sxy150489 	/* Set loopback */
51637426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
51643526Sxy150489 
51653526Sxy150489 	msec_delay(250);
51663526Sxy150489 
51673526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
51684919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
51693526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
51703526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
51714349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
51724349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
51734349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
51743526Sxy150489 
51754919Sxy150489 	switch (hw->mac.type) {
51763526Sxy150489 	case e1000_82540:
51773526Sxy150489 	case e1000_82545:
51783526Sxy150489 	case e1000_82545_rev_3:
51793526Sxy150489 	case e1000_82546:
51803526Sxy150489 	case e1000_82546_rev_3:
51813526Sxy150489 		/*
51823526Sxy150489 		 * For some serdes we'll need to commit the writes now
51833526Sxy150489 		 * so that the status is updated on link
51843526Sxy150489 		 */
51856735Scc210113 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
51864919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
51873526Sxy150489 			msec_delay(100);
51884919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
51893526Sxy150489 		}
51903526Sxy150489 
51916735Scc210113 		if (hw->phy.media_type == e1000_media_type_copper) {
51923526Sxy150489 			/* Invert Loss of Signal */
51933526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
51943526Sxy150489 		} else {
51953526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
51964919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
51973526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
51983526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
51993526Sxy150489 		}
52003526Sxy150489 		break;
52013526Sxy150489 
52023526Sxy150489 	case e1000_82571:
52033526Sxy150489 	case e1000_82572:
52045082Syy150190 		/*
52055082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
52065082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
52075082Syy150190 		 * using SerDes analog loopback.
52085082Syy150190 		 */
52096735Scc210113 		if (hw->phy.media_type != e1000_media_type_copper) {
52105082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
52115082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
52125082Syy150190 			txcw &= ~((uint32_t)1 << 31);
52135082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
52145082Syy150190 
52155082Syy150190 			/*
52165082Syy150190 			 * Write 0x410 to Serdes Control register
52175082Syy150190 			 * to enable Serdes analog loopback
52185082Syy150190 			 */
52195082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
52205082Syy150190 			msec_delay(10);
52213526Sxy150489 		}
52228479SChenlu.Chen@Sun.COM 
52238479SChenlu.Chen@Sun.COM 		status = E1000_READ_REG(hw, E1000_STATUS);
52248479SChenlu.Chen@Sun.COM 		/* Set ILOS on fiber nic if half duplex is detected */
52258479SChenlu.Chen@Sun.COM 		if ((hw->phy.media_type == e1000_media_type_fiber) &&
52268479SChenlu.Chen@Sun.COM 		    ((status & E1000_STATUS_FD) == 0 ||
52278479SChenlu.Chen@Sun.COM 		    (status & E1000_STATUS_LU) == 0))
52288479SChenlu.Chen@Sun.COM 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
52298479SChenlu.Chen@Sun.COM 		else if (hw->phy.media_type == e1000_media_type_internal_serdes)
52308479SChenlu.Chen@Sun.COM 			ctrl |= E1000_CTRL_SLU;
52313526Sxy150489 		break;
52323526Sxy150489 
52333526Sxy150489 	case e1000_82573:
52343526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
52358479SChenlu.Chen@Sun.COM 		break;
52368479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
52378479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
52388479SChenlu.Chen@Sun.COM 		ctrl |= E1000_CTRL_SLU;
52393526Sxy150489 		break;
52403526Sxy150489 	}
52418479SChenlu.Chen@Sun.COM 	if (hw->phy.type == e1000_phy_bm)
52428479SChenlu.Chen@Sun.COM 		ctrl |= E1000_CTRL_SLU | E1000_CTRL_ILOS;
52438479SChenlu.Chen@Sun.COM 
52448479SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
52453526Sxy150489 }
52463526Sxy150489 
52473526Sxy150489 static void
52483526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
52493526Sxy150489 {
52503526Sxy150489 	struct e1000_hw *hw;
52513526Sxy150489 	uint32_t rctl;
52523526Sxy150489 	uint32_t ctrl_ext;
52533526Sxy150489 	uint32_t ctrl;
52543526Sxy150489 	uint32_t status;
52553526Sxy150489 	uint32_t txcw;
52567133Scc210113 	uint16_t phydata;
52573526Sxy150489 
52584919Sxy150489 	hw = &Adapter->shared;
52593526Sxy150489 
52603526Sxy150489 	/* Disable Smart Power Down */
52613526Sxy150489 	phy_spd_state(hw, B_FALSE);
52623526Sxy150489 
52637133Scc210113 	switch (hw->mac.type) {
52647133Scc210113 	case e1000_82571:
52657133Scc210113 	case e1000_82572:
52667133Scc210113 		switch (hw->phy.media_type) {
52677133Scc210113 		case e1000_media_type_copper:
52687133Scc210113 			/* Force link up (Must be done before the PHY writes) */
52697133Scc210113 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
52707133Scc210113 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
52717133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
52727133Scc210113 
52737133Scc210113 			rctl = E1000_READ_REG(hw, E1000_RCTL);
52747133Scc210113 			rctl |= (E1000_RCTL_EN |
52757133Scc210113 			    E1000_RCTL_SBP |
52767133Scc210113 			    E1000_RCTL_UPE |
52777133Scc210113 			    E1000_RCTL_MPE |
52787133Scc210113 			    E1000_RCTL_LPE |
52797133Scc210113 			    E1000_RCTL_BAM);		/* 0x803E */
52807133Scc210113 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
52817133Scc210113 
52827133Scc210113 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
52837133Scc210113 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
52847133Scc210113 			    E1000_CTRL_EXT_SDP6_DATA |
5285*10680SMin.Xu@Sun.COM 			    E1000_CTRL_EXT_SDP3_DATA |
52867133Scc210113 			    E1000_CTRL_EXT_SDP4_DIR |
52877133Scc210113 			    E1000_CTRL_EXT_SDP6_DIR |
5288*10680SMin.Xu@Sun.COM 			    E1000_CTRL_EXT_SDP3_DIR);	/* 0x0DD0 */
52897133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
52907133Scc210113 
52917133Scc210113 			/*
52927133Scc210113 			 * This sequence tunes the PHY's SDP and no customer
52937133Scc210113 			 * settable values. For background, see comments above
52947133Scc210113 			 * e1000g_set_internal_loopback().
52957133Scc210113 			 */
52967426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
52977133Scc210113 			msec_delay(10);
52987426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
52997426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
53007426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
53017426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
53027426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
53037426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
53047426SChenliang.Xu@Sun.COM 
53057426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
53067426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
53077426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
53087426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
53097426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
53107133Scc210113 
53117133Scc210113 			msec_delay(50);
53127133Scc210113 			break;
53137133Scc210113 		case e1000_media_type_fiber:
53147133Scc210113 		case e1000_media_type_internal_serdes:
53157133Scc210113 			status = E1000_READ_REG(hw, E1000_STATUS);
53167133Scc210113 			if (((status & E1000_STATUS_LU) == 0) ||
53177133Scc210113 			    (hw->phy.media_type ==
53187133Scc210113 			    e1000_media_type_internal_serdes)) {
53197133Scc210113 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
53207133Scc210113 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
53217133Scc210113 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
53227133Scc210113 			}
53237133Scc210113 
53247133Scc210113 			/* Disable autoneg by setting bit 31 of TXCW to zero */
53257133Scc210113 			txcw = E1000_READ_REG(hw, E1000_TXCW);
53267133Scc210113 			txcw &= ~((uint32_t)1 << 31);
53277133Scc210113 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
53287133Scc210113 
53297133Scc210113 			/*
53307133Scc210113 			 * Write 0x410 to Serdes Control register
53317133Scc210113 			 * to enable Serdes analog loopback
53327133Scc210113 			 */
53337133Scc210113 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
53347133Scc210113 			msec_delay(10);
53357133Scc210113 			break;
53367133Scc210113 		default:
53377133Scc210113 			break;
53387133Scc210113 		}
53393526Sxy150489 		break;
53408479SChenlu.Chen@Sun.COM 	case e1000_82574:
53417133Scc210113 	case e1000_80003es2lan:
53428479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
53438479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
53447426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
53457426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
53467426SChenliang.Xu@Sun.COM 		    phydata | (1 << 5));
53477133Scc210113 		Adapter->param_adv_autoneg = 1;
53487133Scc210113 		Adapter->param_adv_1000fdx = 1;
53497426SChenliang.Xu@Sun.COM 		(void) e1000g_reset_link(Adapter);
53503526Sxy150489 		break;
53513526Sxy150489 	}
53523526Sxy150489 }
53533526Sxy150489 
53543526Sxy150489 static void
53553526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
53563526Sxy150489 {
53573526Sxy150489 	struct e1000_hw *hw;
53583526Sxy150489 	uint32_t ctrl;
53593526Sxy150489 	uint16_t phy_ctrl;
53603526Sxy150489 
53614919Sxy150489 	hw = &Adapter->shared;
53623526Sxy150489 
53633526Sxy150489 	/* Disable Smart Power Down */
53643526Sxy150489 	phy_spd_state(hw, B_FALSE);
53653526Sxy150489 
53663526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
53674349Sxy150489 	    MII_CR_SPEED_100);
53683526Sxy150489 
53693526Sxy150489 	/* Force 100/FD, reset PHY */
53707426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
53714349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
53723526Sxy150489 	msec_delay(10);
53733526Sxy150489 
53743526Sxy150489 	/* Force 100/FD */
53757426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
53764349Sxy150489 	    phy_ctrl);			/* 0x2100 */
53773526Sxy150489 	msec_delay(10);
53783526Sxy150489 
53793526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
53804919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
53813526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
53823526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
53834349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
53844349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
53854349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
53864349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
53873526Sxy150489 
53884919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
53893526Sxy150489 }
53903526Sxy150489 
53913526Sxy150489 static void
53923526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
53933526Sxy150489 {
53943526Sxy150489 	struct e1000_hw *hw;
53953526Sxy150489 	uint32_t ctrl;
53963526Sxy150489 	uint16_t phy_ctrl;
53973526Sxy150489 
53984919Sxy150489 	hw = &Adapter->shared;
53993526Sxy150489 
54003526Sxy150489 	/* Disable Smart Power Down */
54013526Sxy150489 	phy_spd_state(hw, B_FALSE);
54023526Sxy150489 
54033526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
54044349Sxy150489 	    MII_CR_SPEED_10);
54053526Sxy150489 
54063526Sxy150489 	/* Force 10/FD, reset PHY */
54077426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
54084349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
54093526Sxy150489 	msec_delay(10);
54103526Sxy150489 
54113526Sxy150489 	/* Force 10/FD */
54127426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
54134349Sxy150489 	    phy_ctrl);			/* 0x0100 */
54143526Sxy150489 	msec_delay(10);
54153526Sxy150489 
54163526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
54174919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
54183526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
54193526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
54204349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
54214349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
54224349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
54234349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
54243526Sxy150489 
54254919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
54263526Sxy150489 }
54273526Sxy150489 
54283526Sxy150489 #ifdef __sparc
54293526Sxy150489 static boolean_t
54303526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
54313526Sxy150489 {
54324919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
54333526Sxy150489 	uchar_t *bytes;
54343526Sxy150489 	struct ether_addr sysaddr;
54353526Sxy150489 	uint_t nelts;
54363526Sxy150489 	int err;
54373526Sxy150489 	boolean_t found = B_FALSE;
54383526Sxy150489 
54393526Sxy150489 	/*
54403526Sxy150489 	 * The "vendor's factory-set address" may already have
54413526Sxy150489 	 * been extracted from the chip, but if the property
54423526Sxy150489 	 * "local-mac-address" is set we use that instead.
54433526Sxy150489 	 *
54443526Sxy150489 	 * We check whether it looks like an array of 6
54453526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
54463526Sxy150489 	 * make sense of it this way, we'll ignore it.
54473526Sxy150489 	 */
54483526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
54493526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
54503526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
54513526Sxy150489 		if (nelts == ETHERADDRL) {
54523526Sxy150489 			while (nelts--)
54534919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
54543526Sxy150489 			found = B_TRUE;
54553526Sxy150489 		}
54563526Sxy150489 		ddi_prop_free(bytes);
54573526Sxy150489 	}
54583526Sxy150489 
54593526Sxy150489 	/*
54603526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
54613526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
54623526Sxy150489 	 */
54633526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
54643526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
54653526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
54663526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
54674919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
54683526Sxy150489 				found = B_TRUE;
54693526Sxy150489 			}
54703526Sxy150489 		}
54713526Sxy150489 		ddi_prop_free(bytes);
54723526Sxy150489 	}
54733526Sxy150489 
54743526Sxy150489 	/*
54753526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
54763526Sxy150489 	 * if we netbooted from this interface), we must use this instead
54773526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
54783526Sxy150489 	 * get confused by the address changing as Solaris takes over!
54793526Sxy150489 	 */
54803526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
54813526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
54823526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
54833526Sxy150489 		if (nelts == ETHERADDRL) {
54843526Sxy150489 			while (nelts--)
54854919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
54863526Sxy150489 			found = B_TRUE;
54873526Sxy150489 		}
54883526Sxy150489 		ddi_prop_free(bytes);
54893526Sxy150489 	}
54903526Sxy150489 
54913526Sxy150489 	if (found) {
54924919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
54933526Sxy150489 		    ETHERADDRL);
54943526Sxy150489 	}
54953526Sxy150489 
54963526Sxy150489 	return (found);
54973526Sxy150489 }
54983526Sxy150489 #endif
54993526Sxy150489 
55003526Sxy150489 static int
55013526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
55023526Sxy150489 {
55033526Sxy150489 	dev_info_t *devinfo;
55043526Sxy150489 	int intr_types;
55053526Sxy150489 	int rc;
55063526Sxy150489 
55073526Sxy150489 	devinfo = Adapter->dip;
55083526Sxy150489 
55093526Sxy150489 	/* Get supported interrupt types */
55103526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
55113526Sxy150489 
55123526Sxy150489 	if (rc != DDI_SUCCESS) {
55134919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55143526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
55153526Sxy150489 		return (DDI_FAILURE);
55163526Sxy150489 	}
55173526Sxy150489 
55183526Sxy150489 	/*
55193526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
55203526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
55213526Sxy150489 	 * that it supports MSI, but in fact has problems.
55223526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
55233526Sxy150489 	 * PCI/PCI-X NICs.
55243526Sxy150489 	 */
55254919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
55266986Smx205022 		Adapter->msi_enable = B_FALSE;
55276986Smx205022 
55286986Smx205022 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
55293526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
55303526Sxy150489 
55313526Sxy150489 		if (rc != DDI_SUCCESS) {
55324919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
55333526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
55343526Sxy150489 		} else {
55353526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
55363526Sxy150489 		}
55373526Sxy150489 	}
55383526Sxy150489 
55393526Sxy150489 	if ((Adapter->intr_type == 0) &&
55403526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
55413526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
55423526Sxy150489 
55433526Sxy150489 		if (rc != DDI_SUCCESS) {
55444919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
55453526Sxy150489 			    "Add Legacy interrupts failed\n");
55463526Sxy150489 			return (DDI_FAILURE);
55473526Sxy150489 		}
55483526Sxy150489 
55493526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
55503526Sxy150489 	}
55513526Sxy150489 
55523526Sxy150489 	if (Adapter->intr_type == 0) {
55534919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
55543526Sxy150489 		    "No interrupts registered\n");
55553526Sxy150489 		return (DDI_FAILURE);
55563526Sxy150489 	}
55573526Sxy150489 
55583526Sxy150489 	return (DDI_SUCCESS);
55593526Sxy150489 }
55603526Sxy150489 
55613526Sxy150489 /*
55623526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
55633526Sxy150489  */
55643526Sxy150489 static int
55653526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
55663526Sxy150489 {
55673526Sxy150489 	dev_info_t *devinfo;
55683526Sxy150489 	int count, avail, actual;
55693526Sxy150489 	int x, y, rc, inum = 0;
55703526Sxy150489 	int flag;
55713526Sxy150489 	ddi_intr_handler_t *intr_handler;
55723526Sxy150489 
55733526Sxy150489 	devinfo = Adapter->dip;
55743526Sxy150489 
55753526Sxy150489 	/* get number of interrupts */
55763526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
55773526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
55784919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
55793526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
55803526Sxy150489 		    rc, count);
55813526Sxy150489 		return (DDI_FAILURE);
55823526Sxy150489 	}
55833526Sxy150489 
55843526Sxy150489 	/* get number of available interrupts */
55853526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
55863526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
55874919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
55883526Sxy150489 		    "Get interrupt available number failed. "
55893526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
55903526Sxy150489 		return (DDI_FAILURE);
55913526Sxy150489 	}
55923526Sxy150489 
55933526Sxy150489 	if (avail < count) {
55944919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
55953526Sxy150489 		    "Interrupts count: %d, available: %d\n",
55963526Sxy150489 		    count, avail);
55973526Sxy150489 	}
55983526Sxy150489 
55993526Sxy150489 	/* Allocate an array of interrupt handles */
56003526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
56013526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
56023526Sxy150489 
56033526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
56043526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
56053526Sxy150489 
56063526Sxy150489 	/* call ddi_intr_alloc() */
56073526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
56083526Sxy150489 	    count, &actual, flag);
56093526Sxy150489 
56103526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
56114919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
56123526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
56133526Sxy150489 
56143526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
56153526Sxy150489 		return (DDI_FAILURE);
56163526Sxy150489 	}
56173526Sxy150489 
56183526Sxy150489 	if (actual < count) {
56194919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
56203526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
56213526Sxy150489 		    count, actual);
56223526Sxy150489 	}
56233526Sxy150489 
56243526Sxy150489 	Adapter->intr_cnt = actual;
56253526Sxy150489 
56263526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
56273526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
56283526Sxy150489 
56293526Sxy150489 	if (rc != DDI_SUCCESS) {
56304919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
56313526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
56323526Sxy150489 
56333526Sxy150489 		/* Free already allocated intr */
56343526Sxy150489 		for (y = 0; y < actual; y++)
56353526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
56363526Sxy150489 
56373526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
56383526Sxy150489 		return (DDI_FAILURE);
56393526Sxy150489 	}
56403526Sxy150489 
56413526Sxy150489 	/*
56423526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
56433526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
56443526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
56453526Sxy150489 	 * devices.
56463526Sxy150489 	 */
56474919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
56483526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
56493526Sxy150489 	else
56503526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
56513526Sxy150489 
56523526Sxy150489 	/* Call ddi_intr_add_handler() */
56533526Sxy150489 	for (x = 0; x < actual; x++) {
56543526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
56553526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
56563526Sxy150489 
56573526Sxy150489 		if (rc != DDI_SUCCESS) {
56584919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
56593526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
56603526Sxy150489 
56613526Sxy150489 			/* Remove already added handler */
56623526Sxy150489 			for (y = 0; y < x; y++)
56633526Sxy150489 				(void) ddi_intr_remove_handler(
56643526Sxy150489 				    Adapter->htable[y]);
56653526Sxy150489 
56663526Sxy150489 			/* Free already allocated intr */
56673526Sxy150489 			for (y = 0; y < actual; y++)
56683526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
56693526Sxy150489 
56703526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
56713526Sxy150489 			return (DDI_FAILURE);
56723526Sxy150489 		}
56733526Sxy150489 	}
56743526Sxy150489 
56753526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
56763526Sxy150489 
56773526Sxy150489 	if (rc != DDI_SUCCESS) {
56784919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
56793526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
56803526Sxy150489 
56813526Sxy150489 		/* Free already allocated intr */
56823526Sxy150489 		for (y = 0; y < actual; y++) {
56833526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
56843526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
56853526Sxy150489 		}
56863526Sxy150489 
56873526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
56883526Sxy150489 		return (DDI_FAILURE);
56893526Sxy150489 	}
56903526Sxy150489 
56913526Sxy150489 	return (DDI_SUCCESS);
56923526Sxy150489 }
56933526Sxy150489 
56943526Sxy150489 static int
56953526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
56963526Sxy150489 {
56973526Sxy150489 	int x;
56983526Sxy150489 	int rc;
56993526Sxy150489 
57003526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
57013526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
57023526Sxy150489 		if (rc != DDI_SUCCESS) {
57034919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57043526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
57053526Sxy150489 			return (DDI_FAILURE);
57063526Sxy150489 		}
57073526Sxy150489 
57083526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
57093526Sxy150489 		if (rc != DDI_SUCCESS) {
57104919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57113526Sxy150489 			    "Free intr failed: %d\n", rc);
57123526Sxy150489 			return (DDI_FAILURE);
57133526Sxy150489 		}
57143526Sxy150489 	}
57153526Sxy150489 
57163526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
57173526Sxy150489 
57183526Sxy150489 	return (DDI_SUCCESS);
57193526Sxy150489 }
57203526Sxy150489 
57213526Sxy150489 static int
57223526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
57233526Sxy150489 {
57243526Sxy150489 	int x;
57253526Sxy150489 	int rc;
57263526Sxy150489 
57273526Sxy150489 	/* Enable interrupts */
57283526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
57293526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
57303526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
57313526Sxy150489 		    Adapter->intr_cnt);
57323526Sxy150489 		if (rc != DDI_SUCCESS) {
57334919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57343526Sxy150489 			    "Enable block intr failed: %d\n", rc);
57353526Sxy150489 			return (DDI_FAILURE);
57363526Sxy150489 		}
57373526Sxy150489 	} else {
57383526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
57393526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
57403526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
57413526Sxy150489 			if (rc != DDI_SUCCESS) {
57424919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57433526Sxy150489 				    "Enable intr failed: %d\n", rc);
57443526Sxy150489 				return (DDI_FAILURE);
57453526Sxy150489 			}
57463526Sxy150489 		}
57473526Sxy150489 	}
57483526Sxy150489 
57493526Sxy150489 	return (DDI_SUCCESS);
57503526Sxy150489 }
57513526Sxy150489 
57523526Sxy150489 static int
57533526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
57543526Sxy150489 {
57553526Sxy150489 	int x;
57563526Sxy150489 	int rc;
57573526Sxy150489 
57583526Sxy150489 	/* Disable all interrupts */
57593526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
57603526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
57613526Sxy150489 		    Adapter->intr_cnt);
57623526Sxy150489 		if (rc != DDI_SUCCESS) {
57634919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57643526Sxy150489 			    "Disable block intr failed: %d\n", rc);
57653526Sxy150489 			return (DDI_FAILURE);
57663526Sxy150489 		}
57673526Sxy150489 	} else {
57683526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
57693526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
57703526Sxy150489 			if (rc != DDI_SUCCESS) {
57714919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
57723526Sxy150489 				    "Disable intr failed: %d\n", rc);
57733526Sxy150489 				return (DDI_FAILURE);
57743526Sxy150489 			}
57753526Sxy150489 		}
57763526Sxy150489 	}
57773526Sxy150489 
57783526Sxy150489 	return (DDI_SUCCESS);
57793526Sxy150489 }
57805082Syy150190 
57815082Syy150190 /*
57825082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
57835082Syy150190  */
57845082Syy150190 static void
57855082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
57865082Syy150190 {
57875082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
57885082Syy150190 
578910605SMin.Xu@Sun.COM 	if (hw->phy.media_type == e1000_media_type_copper) {
579010605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
579110605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
579210605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
579310605SMin.Xu@Sun.COM 		    &Adapter->phy_an_adv);
579410605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP,
579510605SMin.Xu@Sun.COM 		    &Adapter->phy_an_exp);
579610605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS,
579710605SMin.Xu@Sun.COM 		    &Adapter->phy_ext_status);
579810605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL,
579910605SMin.Xu@Sun.COM 		    &Adapter->phy_1000t_ctrl);
580010605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
580110605SMin.Xu@Sun.COM 		    &Adapter->phy_1000t_status);
580210605SMin.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY,
580310605SMin.Xu@Sun.COM 		    &Adapter->phy_lp_able);
580410605SMin.Xu@Sun.COM 
580510605SMin.Xu@Sun.COM 		Adapter->param_autoneg_cap =
580610605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
580710605SMin.Xu@Sun.COM 		Adapter->param_pause_cap =
580810605SMin.Xu@Sun.COM 		    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
580910605SMin.Xu@Sun.COM 		Adapter->param_asym_pause_cap =
581010605SMin.Xu@Sun.COM 		    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
581110605SMin.Xu@Sun.COM 		Adapter->param_1000fdx_cap =
581210605SMin.Xu@Sun.COM 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
581310605SMin.Xu@Sun.COM 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
581410605SMin.Xu@Sun.COM 		Adapter->param_1000hdx_cap =
581510605SMin.Xu@Sun.COM 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
581610605SMin.Xu@Sun.COM 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
581710605SMin.Xu@Sun.COM 		Adapter->param_100t4_cap =
581810605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
581910605SMin.Xu@Sun.COM 		Adapter->param_100fdx_cap =
582010605SMin.Xu@Sun.COM 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
582110605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
582210605SMin.Xu@Sun.COM 		Adapter->param_100hdx_cap =
582310605SMin.Xu@Sun.COM 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
582410605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
582510605SMin.Xu@Sun.COM 		Adapter->param_10fdx_cap =
582610605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
582710605SMin.Xu@Sun.COM 		Adapter->param_10hdx_cap =
582810605SMin.Xu@Sun.COM 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
582910605SMin.Xu@Sun.COM 
583010605SMin.Xu@Sun.COM 		Adapter->param_adv_autoneg = hw->mac.autoneg;
583110605SMin.Xu@Sun.COM 		Adapter->param_adv_pause =
583210605SMin.Xu@Sun.COM 		    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
583310605SMin.Xu@Sun.COM 		Adapter->param_adv_asym_pause =
583410605SMin.Xu@Sun.COM 		    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
583510605SMin.Xu@Sun.COM 		Adapter->param_adv_1000hdx =
583610605SMin.Xu@Sun.COM 		    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
583710605SMin.Xu@Sun.COM 		Adapter->param_adv_100t4 =
583810605SMin.Xu@Sun.COM 		    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
583910605SMin.Xu@Sun.COM 		if (Adapter->param_adv_autoneg == 1) {
584010605SMin.Xu@Sun.COM 			Adapter->param_adv_1000fdx =
584110605SMin.Xu@Sun.COM 			    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS)
584210605SMin.Xu@Sun.COM 			    ? 1 : 0;
584310605SMin.Xu@Sun.COM 			Adapter->param_adv_100fdx =
584410605SMin.Xu@Sun.COM 			    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS)
584510605SMin.Xu@Sun.COM 			    ? 1 : 0;
584610605SMin.Xu@Sun.COM 			Adapter->param_adv_100hdx =
584710605SMin.Xu@Sun.COM 			    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS)
584810605SMin.Xu@Sun.COM 			    ? 1 : 0;
584910605SMin.Xu@Sun.COM 			Adapter->param_adv_10fdx =
585010605SMin.Xu@Sun.COM 			    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
585110605SMin.Xu@Sun.COM 			Adapter->param_adv_10hdx =
585210605SMin.Xu@Sun.COM 			    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
585310605SMin.Xu@Sun.COM 		}
585410605SMin.Xu@Sun.COM 
585510605SMin.Xu@Sun.COM 		Adapter->param_lp_autoneg =
585610605SMin.Xu@Sun.COM 		    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
585710605SMin.Xu@Sun.COM 		Adapter->param_lp_pause =
585810605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
585910605SMin.Xu@Sun.COM 		Adapter->param_lp_asym_pause =
586010605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
586110605SMin.Xu@Sun.COM 		Adapter->param_lp_1000fdx =
586210605SMin.Xu@Sun.COM 		    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
586310605SMin.Xu@Sun.COM 		Adapter->param_lp_1000hdx =
586410605SMin.Xu@Sun.COM 		    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
586510605SMin.Xu@Sun.COM 		Adapter->param_lp_100t4 =
586610605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
586710605SMin.Xu@Sun.COM 		Adapter->param_lp_100fdx =
586810605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
586910605SMin.Xu@Sun.COM 		Adapter->param_lp_100hdx =
587010605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
587110605SMin.Xu@Sun.COM 		Adapter->param_lp_10fdx =
587210605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
587310605SMin.Xu@Sun.COM 		Adapter->param_lp_10hdx =
587410605SMin.Xu@Sun.COM 		    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
587510605SMin.Xu@Sun.COM 	} else {
587610605SMin.Xu@Sun.COM 		/*
587710605SMin.Xu@Sun.COM 		 * 1Gig Fiber adapter only offers 1Gig Full Duplex. Meaning,
587810605SMin.Xu@Sun.COM 		 * it can only work with 1Gig Full Duplex Link Partner.
587910605SMin.Xu@Sun.COM 		 */
588010605SMin.Xu@Sun.COM 		Adapter->param_autoneg_cap = 0;
588110605SMin.Xu@Sun.COM 		Adapter->param_pause_cap = 1;
588210605SMin.Xu@Sun.COM 		Adapter->param_asym_pause_cap = 1;
588310605SMin.Xu@Sun.COM 		Adapter->param_1000fdx_cap = 1;
588410605SMin.Xu@Sun.COM 		Adapter->param_1000hdx_cap = 0;
588510605SMin.Xu@Sun.COM 		Adapter->param_100t4_cap = 0;
588610605SMin.Xu@Sun.COM 		Adapter->param_100fdx_cap = 0;
588710605SMin.Xu@Sun.COM 		Adapter->param_100hdx_cap = 0;
588810605SMin.Xu@Sun.COM 		Adapter->param_10fdx_cap = 0;
588910605SMin.Xu@Sun.COM 		Adapter->param_10hdx_cap = 0;
589010605SMin.Xu@Sun.COM 
589110605SMin.Xu@Sun.COM 		Adapter->param_adv_autoneg = 0;
589210605SMin.Xu@Sun.COM 		Adapter->param_adv_pause = 1;
589310605SMin.Xu@Sun.COM 		Adapter->param_adv_asym_pause = 1;
589410605SMin.Xu@Sun.COM 		Adapter->param_adv_1000fdx = 1;
589510605SMin.Xu@Sun.COM 		Adapter->param_adv_1000hdx = 0;
589610605SMin.Xu@Sun.COM 		Adapter->param_adv_100t4 = 0;
589710605SMin.Xu@Sun.COM 		Adapter->param_adv_100fdx = 0;
589810605SMin.Xu@Sun.COM 		Adapter->param_adv_100hdx = 0;
589910605SMin.Xu@Sun.COM 		Adapter->param_adv_10fdx = 0;
590010605SMin.Xu@Sun.COM 		Adapter->param_adv_10hdx = 0;
590110605SMin.Xu@Sun.COM 
590210605SMin.Xu@Sun.COM 		Adapter->param_lp_autoneg = 0;
590310605SMin.Xu@Sun.COM 		Adapter->param_lp_pause = 0;
590410605SMin.Xu@Sun.COM 		Adapter->param_lp_asym_pause = 0;
590510605SMin.Xu@Sun.COM 		Adapter->param_lp_1000fdx = 0;
590610605SMin.Xu@Sun.COM 		Adapter->param_lp_1000hdx = 0;
590710605SMin.Xu@Sun.COM 		Adapter->param_lp_100t4 = 0;
590810605SMin.Xu@Sun.COM 		Adapter->param_lp_100fdx = 0;
590910605SMin.Xu@Sun.COM 		Adapter->param_lp_100hdx = 0;
591010605SMin.Xu@Sun.COM 		Adapter->param_lp_10fdx = 0;
591110605SMin.Xu@Sun.COM 		Adapter->param_lp_10hdx = 0;
591210605SMin.Xu@Sun.COM 	}
59135082Syy150190 }
59145273Sgl147354 
59155273Sgl147354 /*
59165273Sgl147354  * FMA support
59175273Sgl147354  */
59185273Sgl147354 
59195273Sgl147354 int
59205273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
59215273Sgl147354 {
59225273Sgl147354 	ddi_fm_error_t de;
59235273Sgl147354 
59245273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
59255273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
59265273Sgl147354 	return (de.fme_status);
59275273Sgl147354 }
59285273Sgl147354 
59295273Sgl147354 int
59305273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
59315273Sgl147354 {
59325273Sgl147354 	ddi_fm_error_t de;
59335273Sgl147354 
59345273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
59355273Sgl147354 	return (de.fme_status);
59365273Sgl147354 }
59375273Sgl147354 
59385273Sgl147354 /*
59395273Sgl147354  * The IO fault service error handling callback function
59405273Sgl147354  */
59417426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
59425273Sgl147354 static int
59435273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
59445273Sgl147354 {
59455273Sgl147354 	/*
59465273Sgl147354 	 * as the driver can always deal with an error in any dma or
59475273Sgl147354 	 * access handle, we can just return the fme_status value.
59485273Sgl147354 	 */
59495273Sgl147354 	pci_ereport_post(dip, err, NULL);
59505273Sgl147354 	return (err->fme_status);
59515273Sgl147354 }
59525273Sgl147354 
59535273Sgl147354 static void
59545273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
59555273Sgl147354 {
59565273Sgl147354 	ddi_iblock_cookie_t iblk;
59575273Sgl147354 	int fma_acc_flag, fma_dma_flag;
59585273Sgl147354 
59595273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
59605273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
59615273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
59625273Sgl147354 		fma_acc_flag = 1;
59635273Sgl147354 	} else {
59645273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
59655273Sgl147354 		fma_acc_flag = 0;
59665273Sgl147354 	}
59675273Sgl147354 
59685273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
59695273Sgl147354 		fma_dma_flag = 1;
59705273Sgl147354 	} else {
59715273Sgl147354 		fma_dma_flag = 0;
59725273Sgl147354 	}
59735273Sgl147354 
59745273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
59755273Sgl147354 
59765273Sgl147354 	if (Adapter->fm_capabilities) {
59775273Sgl147354 
59785273Sgl147354 		/* Register capabilities with IO Fault Services */
59795273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
59805273Sgl147354 
59815273Sgl147354 		/*
59825273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
59835273Sgl147354 		 */
59845273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
59855273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
59865273Sgl147354 			pci_ereport_setup(Adapter->dip);
59875273Sgl147354 
59885273Sgl147354 		/*
59895273Sgl147354 		 * Register error callback if error callback capable
59905273Sgl147354 		 */
59915273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
59925273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
59935273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
59945273Sgl147354 	}
59955273Sgl147354 }
59965273Sgl147354 
59975273Sgl147354 static void
59985273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
59995273Sgl147354 {
60005273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
60015273Sgl147354 	if (Adapter->fm_capabilities) {
60025273Sgl147354 
60035273Sgl147354 		/*
60045273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
60055273Sgl147354 		 */
60065273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
60075273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
60085273Sgl147354 			pci_ereport_teardown(Adapter->dip);
60095273Sgl147354 
60105273Sgl147354 		/*
60115273Sgl147354 		 * Un-register error callback if error callback capable
60125273Sgl147354 		 */
60135273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
60145273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
60155273Sgl147354 
60165273Sgl147354 		/* Unregister from IO Fault Services */
60178949SChangqing.Li@Sun.COM 		mutex_enter(&e1000g_rx_detach_lock);
60185273Sgl147354 		ddi_fm_fini(Adapter->dip);
60198949SChangqing.Li@Sun.COM 		if (Adapter->priv_dip != NULL) {
60208949SChangqing.Li@Sun.COM 			DEVI(Adapter->priv_dip)->devi_fmhdl = NULL;
60218949SChangqing.Li@Sun.COM 		}
60228949SChangqing.Li@Sun.COM 		mutex_exit(&e1000g_rx_detach_lock);
60235273Sgl147354 	}
60245273Sgl147354 }
60255273Sgl147354 
60265273Sgl147354 void
60275273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
60285273Sgl147354 {
60295273Sgl147354 	uint64_t ena;
60305273Sgl147354 	char buf[FM_MAX_CLASS];
60315273Sgl147354 
60325273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
60335273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
60345273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
60355273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
60365273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
60375273Sgl147354 	}
60385273Sgl147354 }
60396512Ssowmini 
60407656SSherry.Moore@Sun.COM /*
60417656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
60427656SSherry.Moore@Sun.COM  *
60437656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
60447656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
60457656SSherry.Moore@Sun.COM  * blocked.
60467656SSherry.Moore@Sun.COM  *
60477656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
60487656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
60497656SSherry.Moore@Sun.COM  */
60507656SSherry.Moore@Sun.COM static int
60517656SSherry.Moore@Sun.COM e1000g_quiesce(dev_info_t *devinfo)
60527656SSherry.Moore@Sun.COM {
60537656SSherry.Moore@Sun.COM 	struct e1000g *Adapter;
60547656SSherry.Moore@Sun.COM 
60557656SSherry.Moore@Sun.COM 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
60567656SSherry.Moore@Sun.COM 
60577656SSherry.Moore@Sun.COM 	if (Adapter == NULL)
60587656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
60597656SSherry.Moore@Sun.COM 
60607656SSherry.Moore@Sun.COM 	e1000g_clear_all_interrupts(Adapter);
60617656SSherry.Moore@Sun.COM 
60627656SSherry.Moore@Sun.COM 	(void) e1000_reset_hw(&Adapter->shared);
60637656SSherry.Moore@Sun.COM 
60647656SSherry.Moore@Sun.COM 	/* Setup our HW Tx Head & Tail descriptor pointers */
60657656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
60667656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
60677656SSherry.Moore@Sun.COM 
60687656SSherry.Moore@Sun.COM 	/* Setup our HW Rx Head & Tail descriptor pointers */
60697656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDH(0), 0);
60707656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDT(0), 0);
60717656SSherry.Moore@Sun.COM 
60727656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
60737656SSherry.Moore@Sun.COM }
60747656SSherry.Moore@Sun.COM 
60756512Ssowmini static int
60766512Ssowmini e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
60776512Ssowmini     uint_t pr_valsize, void *pr_val)
60786512Ssowmini {
60796512Ssowmini 	link_flowctrl_t fl;
608010605SMin.Xu@Sun.COM 	struct e1000_hw *hw = &Adapter->shared;
60816512Ssowmini 	int err = 0;
60826512Ssowmini 
60836512Ssowmini 	ASSERT(pr_valsize > 0);
60846512Ssowmini 	switch (pr_num) {
60856789Sam223141 	case MAC_PROP_AUTONEG:
608610605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
608710605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 0;
608810605SMin.Xu@Sun.COM 		else
608910605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
609010605SMin.Xu@Sun.COM 			    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS)
609110605SMin.Xu@Sun.COM 			    ? 1 : 0);
60926512Ssowmini 		break;
60936789Sam223141 	case MAC_PROP_FLOWCTRL:
60946512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
60956512Ssowmini 			return (EINVAL);
60966512Ssowmini 		fl = LINK_FLOWCTRL_BI;
60976512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
60986512Ssowmini 		break;
60996789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
61006789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
610110605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
610210605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 1;
610310605SMin.Xu@Sun.COM 		else
610410605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
610510605SMin.Xu@Sun.COM 			    ((Adapter->phy_ext_status &
610610605SMin.Xu@Sun.COM 			    IEEE_ESR_1000T_FD_CAPS) ||
610710605SMin.Xu@Sun.COM 			    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS))
610810605SMin.Xu@Sun.COM 			    ? 1 : 0;
61096512Ssowmini 		break;
61106789Sam223141 	case MAC_PROP_ADV_1000HDX_CAP:
61116789Sam223141 	case MAC_PROP_EN_1000HDX_CAP:
611210605SMin.Xu@Sun.COM 		*(uint8_t *)pr_val = 0;
61136512Ssowmini 		break;
61146789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
61156789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
611610605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
611710605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 0;
611810605SMin.Xu@Sun.COM 		else
611910605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
612010605SMin.Xu@Sun.COM 			    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
612110605SMin.Xu@Sun.COM 			    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
612210605SMin.Xu@Sun.COM 			    ? 1 : 0;
61237426SChenliang.Xu@Sun.COM 		break;
61246789Sam223141 	case MAC_PROP_ADV_100HDX_CAP:
61256789Sam223141 	case MAC_PROP_EN_100HDX_CAP:
612610605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
612710605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 0;
612810605SMin.Xu@Sun.COM 		else
612910605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
613010605SMin.Xu@Sun.COM 			    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
613110605SMin.Xu@Sun.COM 			    (Adapter->phy_status & MII_SR_100T2_HD_CAPS))
613210605SMin.Xu@Sun.COM 			    ? 1 : 0;
61336512Ssowmini 		break;
61346789Sam223141 	case MAC_PROP_ADV_10FDX_CAP:
61356789Sam223141 	case MAC_PROP_EN_10FDX_CAP:
613610605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
613710605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 0;
613810605SMin.Xu@Sun.COM 		else
613910605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
614010605SMin.Xu@Sun.COM 			    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
61416512Ssowmini 		break;
61426789Sam223141 	case MAC_PROP_ADV_10HDX_CAP:
61436789Sam223141 	case MAC_PROP_EN_10HDX_CAP:
614410605SMin.Xu@Sun.COM 		if (hw->phy.media_type != e1000_media_type_copper)
614510605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val = 0;
614610605SMin.Xu@Sun.COM 		else
614710605SMin.Xu@Sun.COM 			*(uint8_t *)pr_val =
614810605SMin.Xu@Sun.COM 			    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
61496512Ssowmini 		break;
61506512Ssowmini 	default:
61516512Ssowmini 		err = ENOTSUP;
61526512Ssowmini 		break;
61536512Ssowmini 	}
61546512Ssowmini 	return (err);
61556512Ssowmini }
61566512Ssowmini 
61576512Ssowmini /*
61586512Ssowmini  * synchronize the adv* and en* parameters.
61596512Ssowmini  *
61606512Ssowmini  * See comments in <sys/dld.h> for details of the *_en_*
61616512Ssowmini  * parameters. The usage of ndd for setting adv parameters will
61626512Ssowmini  * synchronize all the en parameters with the e1000g parameters,
61637607STed.You@Sun.COM  * implicitly disabling any settings made via dladm.
61646512Ssowmini  */
61656512Ssowmini static void
61666512Ssowmini e1000g_param_sync(struct e1000g *Adapter)
61676512Ssowmini {
61686512Ssowmini 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
61696512Ssowmini 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
61706512Ssowmini 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
61716512Ssowmini 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
61726512Ssowmini 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
61736512Ssowmini 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
61746512Ssowmini }
61757607STed.You@Sun.COM 
61767607STed.You@Sun.COM /*
61777607STed.You@Sun.COM  * e1000g_get_driver_control - tell manageability firmware that the driver
61787607STed.You@Sun.COM  * has control.
61797607STed.You@Sun.COM  */
61807607STed.You@Sun.COM static void
61817607STed.You@Sun.COM e1000g_get_driver_control(struct e1000_hw *hw)
61827607STed.You@Sun.COM {
61837607STed.You@Sun.COM 	uint32_t ctrl_ext;
61847607STed.You@Sun.COM 	uint32_t swsm;
61857607STed.You@Sun.COM 
61867607STed.You@Sun.COM 	/* tell manageability firmware the driver has taken over */
61877607STed.You@Sun.COM 	switch (hw->mac.type) {
61887607STed.You@Sun.COM 	case e1000_82573:
61897607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
61907607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
61917607STed.You@Sun.COM 		break;
61927607STed.You@Sun.COM 	case e1000_82571:
61937607STed.You@Sun.COM 	case e1000_82572:
61947607STed.You@Sun.COM 	case e1000_82574:
61957607STed.You@Sun.COM 	case e1000_80003es2lan:
61967607STed.You@Sun.COM 	case e1000_ich8lan:
61977607STed.You@Sun.COM 	case e1000_ich9lan:
61987607STed.You@Sun.COM 	case e1000_ich10lan:
6199*10680SMin.Xu@Sun.COM 	case e1000_pchlan:
62007607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
62017607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
62027607STed.You@Sun.COM 		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
62037607STed.You@Sun.COM 		break;
62047607STed.You@Sun.COM 	default:
62057607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
62067607STed.You@Sun.COM 		break;
62077607STed.You@Sun.COM 	}
62087607STed.You@Sun.COM }
62097607STed.You@Sun.COM 
62107607STed.You@Sun.COM /*
62117607STed.You@Sun.COM  * e1000g_release_driver_control - tell manageability firmware that the driver
62127607STed.You@Sun.COM  * has released control.
62137607STed.You@Sun.COM  */
62147607STed.You@Sun.COM static void
62157607STed.You@Sun.COM e1000g_release_driver_control(struct e1000_hw *hw)
62167607STed.You@Sun.COM {
62177607STed.You@Sun.COM 	uint32_t ctrl_ext;
62187607STed.You@Sun.COM 	uint32_t swsm;
62197607STed.You@Sun.COM 
62207607STed.You@Sun.COM 	/* tell manageability firmware the driver has released control */
62217607STed.You@Sun.COM 	switch (hw->mac.type) {
62227607STed.You@Sun.COM 	case e1000_82573:
62237607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
62247607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
62257607STed.You@Sun.COM 		break;
62267607STed.You@Sun.COM 	case e1000_82571:
62277607STed.You@Sun.COM 	case e1000_82572:
62287607STed.You@Sun.COM 	case e1000_82574:
62297607STed.You@Sun.COM 	case e1000_80003es2lan:
62307607STed.You@Sun.COM 	case e1000_ich8lan:
62317607STed.You@Sun.COM 	case e1000_ich9lan:
62327607STed.You@Sun.COM 	case e1000_ich10lan:
6233*10680SMin.Xu@Sun.COM 	case e1000_pchlan:
62347607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
62357607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
62367607STed.You@Sun.COM 		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
62377607STed.You@Sun.COM 		break;
62387607STed.You@Sun.COM 	default:
62397607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
62407607STed.You@Sun.COM 		break;
62417607STed.You@Sun.COM 	}
62427607STed.You@Sun.COM }
62437722SShuguo.Yang@Sun.COM 
62447722SShuguo.Yang@Sun.COM /*
62457722SShuguo.Yang@Sun.COM  * Restore e1000g promiscuous mode.
62467722SShuguo.Yang@Sun.COM  */
62477722SShuguo.Yang@Sun.COM static void
62487722SShuguo.Yang@Sun.COM e1000g_restore_promisc(struct e1000g *Adapter)
62497722SShuguo.Yang@Sun.COM {
62507722SShuguo.Yang@Sun.COM 	if (Adapter->e1000g_promisc) {
62517722SShuguo.Yang@Sun.COM 		uint32_t rctl;
62527722SShuguo.Yang@Sun.COM 
62537722SShuguo.Yang@Sun.COM 		rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
62547722SShuguo.Yang@Sun.COM 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
62557722SShuguo.Yang@Sun.COM 		E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
62567722SShuguo.Yang@Sun.COM 	}
62577722SShuguo.Yang@Sun.COM }
6258