xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 8833:8adf20bc60e3)
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";
498539SChenlu.Chen@Sun.COM static char e1000g_version[] = "Driver Ver. 5.3.3";
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);
953526Sxy150489 
963526Sxy150489 /*
973526Sxy150489  * Local routines
983526Sxy150489  */
997656SSherry.Moore@Sun.COM static boolean_t e1000g_reset_adapter(struct e1000g *);
1004919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1014919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1024061Sxy150489 static void e1000g_link_timer(void *);
1034919Sxy150489 static void e1000g_local_timer(void *);
1044061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1053526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1063526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1074919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1084919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1094919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1104919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1114919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1124919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1134919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1144919Sxy150489 static void stop_link_timer(struct e1000g *);
1154919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1164919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
1174919Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *);
1184919Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
1193526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1204919Sxy150489 #ifdef E1000G_DEBUG
1214919Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
1224919Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
1234919Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
1244919Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
1254919Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
1264919Sxy150489     struct iocblk *, mblk_t *);
1274919Sxy150489 #endif
1284919Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
1294919Sxy150489     struct iocblk *, mblk_t *);
1307133Scc210113 static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
1314919Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
1324919Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *);
1334919Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *);
1344919Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *);
1354919Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *);
1364919Sxy150489 static int e1000g_add_intrs(struct e1000g *);
1374919Sxy150489 static int e1000g_intr_add(struct e1000g *, int);
1384919Sxy150489 static int e1000g_rem_intrs(struct e1000g *);
1394919Sxy150489 static int e1000g_enable_intrs(struct e1000g *);
1404919Sxy150489 static int e1000g_disable_intrs(struct e1000g *);
1414919Sxy150489 static boolean_t e1000g_link_up(struct e1000g *);
1423526Sxy150489 #ifdef __sparc
1434919Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *);
1443526Sxy150489 #endif
1455082Syy150190 static void e1000g_get_phy_state(struct e1000g *);
1464982Syy150190 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
1475273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1485273Sgl147354     const void *impl_data);
1495273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1505273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1516512Ssowmini static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
1526512Ssowmini static void e1000g_param_sync(struct e1000g *);
1537607STed.You@Sun.COM static void e1000g_get_driver_control(struct e1000_hw *);
1547607STed.You@Sun.COM static void e1000g_release_driver_control(struct e1000_hw *);
1557722SShuguo.Yang@Sun.COM static void e1000g_restore_promisc(struct e1000g *Adapter);
1566512Ssowmini 
1576512Ssowmini mac_priv_prop_t e1000g_priv_props[] = {
1586512Ssowmini 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
1596512Ssowmini 	{"_tx_interrupt_enable", MAC_PROP_PERM_RW},
1606512Ssowmini 	{"_tx_intr_delay", MAC_PROP_PERM_RW},
1616512Ssowmini 	{"_tx_intr_abs_delay", MAC_PROP_PERM_RW},
1626512Ssowmini 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
1636512Ssowmini 	{"_max_num_rcv_packets", MAC_PROP_PERM_RW},
1646512Ssowmini 	{"_rx_intr_delay", MAC_PROP_PERM_RW},
1656512Ssowmini 	{"_rx_intr_abs_delay", MAC_PROP_PERM_RW},
1666512Ssowmini 	{"_intr_throttling_rate", MAC_PROP_PERM_RW},
1676512Ssowmini 	{"_intr_adaptive", MAC_PROP_PERM_RW},
1686512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
1696512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
1706512Ssowmini };
1716512Ssowmini #define	E1000G_MAX_PRIV_PROPS	\
1726512Ssowmini 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
1736512Ssowmini 
1743526Sxy150489 
1753526Sxy150489 static struct cb_ops cb_ws_ops = {
1763526Sxy150489 	nulldev,		/* cb_open */
1773526Sxy150489 	nulldev,		/* cb_close */
1783526Sxy150489 	nodev,			/* cb_strategy */
1793526Sxy150489 	nodev,			/* cb_print */
1803526Sxy150489 	nodev,			/* cb_dump */
1813526Sxy150489 	nodev,			/* cb_read */
1823526Sxy150489 	nodev,			/* cb_write */
1833526Sxy150489 	nodev,			/* cb_ioctl */
1843526Sxy150489 	nodev,			/* cb_devmap */
1853526Sxy150489 	nodev,			/* cb_mmap */
1863526Sxy150489 	nodev,			/* cb_segmap */
1873526Sxy150489 	nochpoll,		/* cb_chpoll */
1883526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1893526Sxy150489 	NULL,			/* cb_stream */
1903526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1913526Sxy150489 	CB_REV,			/* cb_rev */
1923526Sxy150489 	nodev,			/* cb_aread */
1933526Sxy150489 	nodev			/* cb_awrite */
1943526Sxy150489 };
1953526Sxy150489 
1963526Sxy150489 static struct dev_ops ws_ops = {
1973526Sxy150489 	DEVO_REV,		/* devo_rev */
1983526Sxy150489 	0,			/* devo_refcnt */
1993526Sxy150489 	NULL,			/* devo_getinfo */
2003526Sxy150489 	nulldev,		/* devo_identify */
2013526Sxy150489 	nulldev,		/* devo_probe */
2024919Sxy150489 	e1000g_attach,		/* devo_attach */
2034919Sxy150489 	e1000g_detach,		/* devo_detach */
2043526Sxy150489 	nodev,			/* devo_reset */
2053526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
2063526Sxy150489 	NULL,			/* devo_bus_ops */
2077656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
2087656SSherry.Moore@Sun.COM 	e1000g_quiesce		/* devo_quiesce */
2093526Sxy150489 };
2103526Sxy150489 
2113526Sxy150489 static struct modldrv modldrv = {
2123526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
2133526Sxy150489 	ident,			/* Discription string */
2143526Sxy150489 	&ws_ops,		/* driver ops */
2153526Sxy150489 };
2163526Sxy150489 
2173526Sxy150489 static struct modlinkage modlinkage = {
2183526Sxy150489 	MODREV_1, &modldrv, NULL
2193526Sxy150489 };
2203526Sxy150489 
2214919Sxy150489 /* Access attributes for register mapping */
2224919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
2233526Sxy150489 	DDI_DEVICE_ATTR_V0,
2243526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2253526Sxy150489 	DDI_STRICTORDER_ACC,
2265273Sgl147354 	DDI_FLAGERR_ACC
2273526Sxy150489 };
2283526Sxy150489 
2296394Scc210113 #define	E1000G_M_CALLBACK_FLAGS \
2306394Scc210113 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
2313526Sxy150489 
2323526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2333526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2343526Sxy150489 	e1000g_m_stat,
2353526Sxy150489 	e1000g_m_start,
2363526Sxy150489 	e1000g_m_stop,
2373526Sxy150489 	e1000g_m_promisc,
2383526Sxy150489 	e1000g_m_multicst,
2398275SEric Cheng 	NULL,
2403526Sxy150489 	e1000g_m_tx,
2413526Sxy150489 	e1000g_m_ioctl,
2426394Scc210113 	e1000g_m_getcapab,
2436394Scc210113 	NULL,
2446394Scc210113 	NULL,
2456394Scc210113 	e1000g_m_setprop,
2466394Scc210113 	e1000g_m_getprop
2473526Sxy150489 };
2483526Sxy150489 
2493526Sxy150489 /*
2503526Sxy150489  * Global variables
2513526Sxy150489  */
2523526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2533526Sxy150489 /*
2544894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2554349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2564349Sxy150489  * enabled. If we force the driver to detach while there are still some
2574349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2584349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2594349Sxy150489  * structure will be freed after the driver is detached. However when we
2604349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2614349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2624894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2634894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2644894Syy150190  * after the system board level DR operation. For this reason, the global
2654894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2664349Sxy150489  */
2674894Syy150190 #ifdef __sparc
2684894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2694894Syy150190 #else
2704894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2714894Syy150190 #endif
2724349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2734894Syy150190 
2744349Sxy150489 /*
2753526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2763526Sxy150489  * and the rx packets release in detach processing to make them mutually
2773526Sxy150489  * exclusive.
2783526Sxy150489  * The rx recycling processes different rx packets in different threads,
2793526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2803526Sxy150489  * recycling threads.
2813526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2823526Sxy150489  * it mutually exclusive with the rx recycling.
2833526Sxy150489  */
2843526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2853526Sxy150489 /*
2863526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2873526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2883526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2893526Sxy150489  * resources during the initialization of the instances, then the flag will
2903526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2913526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2923526Sxy150489  */
2933526Sxy150489 krwlock_t e1000g_dma_type_lock;
2943526Sxy150489 
2957133Scc210113 /*
2967133Scc210113  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
2977133Scc210113  * Based on the information from Intel, the 82546 chipset has some hardware
2987133Scc210113  * problem. When one port is being reset and the other port is trying to
2997133Scc210113  * access the eeprom, it could cause system hang or panic. To workaround this
3007133Scc210113  * hardware problem, we use a global mutex to prevent such operations from
3017133Scc210113  * happening simultaneously on different instances. This workaround is applied
3027133Scc210113  * to all the devices supported by this driver.
3037133Scc210113  */
3047133Scc210113 kmutex_t e1000g_nvm_lock;
3053526Sxy150489 
3063526Sxy150489 /*
3073526Sxy150489  * Loadable module configuration entry points for the driver
3083526Sxy150489  */
3093526Sxy150489 
3103526Sxy150489 /*
3114919Sxy150489  * _init - module initialization
3123526Sxy150489  */
3133526Sxy150489 int
3143526Sxy150489 _init(void)
3153526Sxy150489 {
3163526Sxy150489 	int status;
3173526Sxy150489 
3183526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
3193526Sxy150489 	status = mod_install(&modlinkage);
3203526Sxy150489 	if (status != DDI_SUCCESS)
3213526Sxy150489 		mac_fini_ops(&ws_ops);
3223526Sxy150489 	else {
3233526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
3243526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
3257133Scc210113 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
3263526Sxy150489 	}
3273526Sxy150489 
3283526Sxy150489 	return (status);
3293526Sxy150489 }
3303526Sxy150489 
3313526Sxy150489 /*
3324919Sxy150489  * _fini - module finalization
3333526Sxy150489  */
3343526Sxy150489 int
3353526Sxy150489 _fini(void)
3363526Sxy150489 {
3373526Sxy150489 	int status;
3383526Sxy150489 
3393526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3403526Sxy150489 	if (e1000g_mblks_pending != 0) {
3413526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3423526Sxy150489 		return (EBUSY);
3433526Sxy150489 	}
3443526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3453526Sxy150489 
3463526Sxy150489 	status = mod_remove(&modlinkage);
3473526Sxy150489 	if (status == DDI_SUCCESS) {
3483526Sxy150489 		mac_fini_ops(&ws_ops);
3494349Sxy150489 
3504349Sxy150489 		if (e1000g_force_detach) {
3514349Sxy150489 			private_devi_list_t *devi_node;
3524349Sxy150489 
3534349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3544349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3554349Sxy150489 				devi_node = e1000g_private_devi_list;
3564349Sxy150489 				e1000g_private_devi_list =
3574349Sxy150489 				    e1000g_private_devi_list->next;
3584349Sxy150489 
3594349Sxy150489 				kmem_free(devi_node->priv_dip,
3604349Sxy150489 				    sizeof (struct dev_info));
3614349Sxy150489 				kmem_free(devi_node,
3624349Sxy150489 				    sizeof (private_devi_list_t));
3634349Sxy150489 			}
3644349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3654349Sxy150489 		}
3664349Sxy150489 
3673526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3683526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3697133Scc210113 		mutex_destroy(&e1000g_nvm_lock);
3703526Sxy150489 	}
3713526Sxy150489 
3723526Sxy150489 	return (status);
3733526Sxy150489 }
3743526Sxy150489 
3753526Sxy150489 /*
3764919Sxy150489  * _info - module information
3773526Sxy150489  */
3783526Sxy150489 int
3793526Sxy150489 _info(struct modinfo *modinfop)
3803526Sxy150489 {
3813526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3823526Sxy150489 }
3833526Sxy150489 
3843526Sxy150489 /*
3854919Sxy150489  * e1000g_attach - driver attach
3864919Sxy150489  *
3874919Sxy150489  * This function is the device-specific initialization entry
3884919Sxy150489  * point. This entry point is required and must be written.
3894919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3904919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3914919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3924919Sxy150489  * available for use by the driver.
3934919Sxy150489  *
3944919Sxy150489  * The attach() function will be called once for each instance
3954919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
3964919Sxy150489  * Until attach() succeeds, the only driver entry points which
3974919Sxy150489  * may be called are open(9E) and getinfo(9E).
3983526Sxy150489  */
3993526Sxy150489 static int
4004919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
4013526Sxy150489 {
4023526Sxy150489 	struct e1000g *Adapter;
4033526Sxy150489 	struct e1000_hw *hw;
4044919Sxy150489 	struct e1000g_osdep *osdep;
4053526Sxy150489 	int instance;
4063526Sxy150489 
4073526Sxy150489 	switch (cmd) {
4083526Sxy150489 	default:
4093526Sxy150489 		e1000g_log(NULL, CE_WARN,
4104919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
4113526Sxy150489 		return (DDI_FAILURE);
4123526Sxy150489 
4133526Sxy150489 	case DDI_RESUME:
4143526Sxy150489 		return (e1000g_resume(devinfo));
4153526Sxy150489 
4163526Sxy150489 	case DDI_ATTACH:
4173526Sxy150489 		break;
4183526Sxy150489 	}
4193526Sxy150489 
4203526Sxy150489 	/*
4213526Sxy150489 	 * get device instance number
4223526Sxy150489 	 */
4233526Sxy150489 	instance = ddi_get_instance(devinfo);
4243526Sxy150489 
4253526Sxy150489 	/*
4263526Sxy150489 	 * Allocate soft data structure
4273526Sxy150489 	 */
4283526Sxy150489 	Adapter =
4293526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4303526Sxy150489 
4313526Sxy150489 	Adapter->dip = devinfo;
4324919Sxy150489 	Adapter->instance = instance;
4333526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4343526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4353526Sxy150489 
4364919Sxy150489 	hw = &Adapter->shared;
4374919Sxy150489 	osdep = &Adapter->osdep;
4384919Sxy150489 	hw->back = osdep;
4394919Sxy150489 	osdep->adapter = Adapter;
4404919Sxy150489 
4413526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4423526Sxy150489 
4434919Sxy150489 	/*
4445273Sgl147354 	 * Initialize for fma support
4455273Sgl147354 	 */
4465273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4475273Sgl147354 	    0, 0x0f,
4485273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4495273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4505273Sgl147354 	e1000g_fm_init(Adapter);
4515273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4525273Sgl147354 
4535273Sgl147354 	/*
4544919Sxy150489 	 * PCI Configure
4554919Sxy150489 	 */
4564919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4574919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4584919Sxy150489 		goto attach_fail;
4594919Sxy150489 	}
4604919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4614919Sxy150489 
4624919Sxy150489 	/*
4634919Sxy150489 	 * Setup hardware
4644919Sxy150489 	 */
4654919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4664919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4674919Sxy150489 		goto attach_fail;
4684919Sxy150489 	}
4693526Sxy150489 
4703526Sxy150489 	/*
4713526Sxy150489 	 * Map in the device registers.
4723526Sxy150489 	 */
4734919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4744919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4753526Sxy150489 		goto attach_fail;
4763526Sxy150489 	}
4774919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4783526Sxy150489 
4793526Sxy150489 	/*
4803526Sxy150489 	 * Initialize driver parameters
4813526Sxy150489 	 */
4823526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4833526Sxy150489 		goto attach_fail;
4843526Sxy150489 	}
4854919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4863526Sxy150489 
4875273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4885273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4895273Sgl147354 		goto attach_fail;
4905273Sgl147354 	}
4915273Sgl147354 
4923526Sxy150489 	/*
4933526Sxy150489 	 * Initialize interrupts
4943526Sxy150489 	 */
4953526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
4963526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
4973526Sxy150489 		goto attach_fail;
4983526Sxy150489 	}
4994919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
5003526Sxy150489 
5013526Sxy150489 	/*
5023526Sxy150489 	 * Initialize mutex's for this device.
5033526Sxy150489 	 * Do this before enabling the interrupt handler and
5043526Sxy150489 	 * register the softint to avoid the condition where
5053526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5063526Sxy150489 	 */
5073526Sxy150489 	e1000g_init_locks(Adapter);
5083526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5093526Sxy150489 
5103526Sxy150489 	/*
5113526Sxy150489 	 * Initialize Driver Counters
5123526Sxy150489 	 */
5134919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
5143526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5153526Sxy150489 		goto attach_fail;
5163526Sxy150489 	}
5173526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5183526Sxy150489 
5193526Sxy150489 	/*
5203526Sxy150489 	 * Initialize chip hardware and software structures
5213526Sxy150489 	 */
5228479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
5233526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5248479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
5253526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5263526Sxy150489 		goto attach_fail;
5273526Sxy150489 	}
5288479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
5293526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5303526Sxy150489 
5313526Sxy150489 	/*
5323526Sxy150489 	 * Register the driver to the MAC
5333526Sxy150489 	 */
5343526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5353526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5363526Sxy150489 		goto attach_fail;
5373526Sxy150489 	}
5384919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5393526Sxy150489 
5403526Sxy150489 	/*
5413526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5423526Sxy150489 	 * initialized, enable interrupts.
5433526Sxy150489 	 */
5443526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5453526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5463526Sxy150489 		goto attach_fail;
5473526Sxy150489 	}
5484919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5493526Sxy150489 
5504982Syy150190 	/*
5514982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5524982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5534982Syy150190 	 * supports after driver detached.
5544982Syy150190 	 */
5554982Syy150190 	if (e1000g_force_detach) {
5564982Syy150190 		private_devi_list_t *devi_node;
5574982Syy150190 
5584982Syy150190 		Adapter->priv_dip =
5594982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5604982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5614982Syy150190 		    sizeof (struct dev_info));
5624982Syy150190 
5634982Syy150190 		devi_node =
5644982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5654982Syy150190 
5664982Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
5674982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5684982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5694982Syy150190 		devi_node->next = e1000g_private_devi_list;
5704982Syy150190 		e1000g_private_devi_list = devi_node;
5714982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
5724982Syy150190 	}
5734982Syy150190 
5743526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5758479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state = E1000G_INITIALIZED;
5763526Sxy150489 
5773526Sxy150489 	return (DDI_SUCCESS);
5783526Sxy150489 
5793526Sxy150489 attach_fail:
5803526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5813526Sxy150489 	return (DDI_FAILURE);
5823526Sxy150489 }
5833526Sxy150489 
5843526Sxy150489 static int
5853526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5863526Sxy150489 {
5874919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5883526Sxy150489 	mac_register_t *mac;
5893526Sxy150489 	int err;
5903526Sxy150489 
5913526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5923526Sxy150489 		return (DDI_FAILURE);
5934919Sxy150489 
5943526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
5953526Sxy150489 	mac->m_driver = Adapter;
5963526Sxy150489 	mac->m_dip = Adapter->dip;
5974919Sxy150489 	mac->m_src_addr = hw->mac.addr;
5983526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
5993526Sxy150489 	mac->m_min_sdu = 0;
6006394Scc210113 	mac->m_max_sdu = Adapter->default_mtu;
6015895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6026512Ssowmini 	mac->m_priv_props = e1000g_priv_props;
6036512Ssowmini 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
6048275SEric Cheng 	mac->m_v12n = MAC_VIRT_LEVEL1;
6054919Sxy150489 
6063526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6073526Sxy150489 	mac_free(mac);
6084919Sxy150489 
6093526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6103526Sxy150489 }
6113526Sxy150489 
6123526Sxy150489 static int
6134919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
6144919Sxy150489 {
6154919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6164919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6174919Sxy150489 
6184919Sxy150489 	/* Get the device id */
6194919Sxy150489 	hw->vendor_id =
6204919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6214919Sxy150489 	hw->device_id =
6224919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6234919Sxy150489 	hw->revision_id =
6244919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6254919Sxy150489 	hw->subsystem_device_id =
6264919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6274919Sxy150489 	hw->subsystem_vendor_id =
6284919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6294919Sxy150489 
6304919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6314919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6324919Sxy150489 		    "MAC type could not be set properly.");
6334919Sxy150489 		return (DDI_FAILURE);
6344919Sxy150489 	}
6354919Sxy150489 
6364919Sxy150489 	return (DDI_SUCCESS);
6374919Sxy150489 }
6384919Sxy150489 
6394919Sxy150489 static int
6404919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6414919Sxy150489 {
6424919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6434919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6444919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6454919Sxy150489 	off_t mem_size;
6464919Sxy150489 
6477607STed.You@Sun.COM 	/* Get size of adapter register memory */
6487607STed.You@Sun.COM 	if (ddi_dev_regsize(devinfo, ADAPTER_REG_SET, &mem_size) !=
6497607STed.You@Sun.COM 	    DDI_SUCCESS) {
6504919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6514919Sxy150489 		    "ddi_dev_regsize for registers failed");
6524919Sxy150489 		return (DDI_FAILURE);
6534919Sxy150489 	}
6544919Sxy150489 
6557607STed.You@Sun.COM 	/* Map adapter register memory */
6567607STed.You@Sun.COM 	if ((ddi_regs_map_setup(devinfo, ADAPTER_REG_SET,
6574919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6584919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6594919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6604919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6614919Sxy150489 		goto regs_map_fail;
6624919Sxy150489 	}
6634919Sxy150489 
6644919Sxy150489 	/* ICH needs to map flash memory */
6657607STed.You@Sun.COM 	if (hw->mac.type == e1000_ich8lan ||
6667607STed.You@Sun.COM 	    hw->mac.type == e1000_ich9lan ||
6677607STed.You@Sun.COM 	    hw->mac.type == e1000_ich10lan) {
6684919Sxy150489 		/* get flash size */
6694919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6704919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6714919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6724919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6734919Sxy150489 			goto regs_map_fail;
6744919Sxy150489 		}
6754919Sxy150489 
6764919Sxy150489 		/* map flash in */
6774919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6784919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6794919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6804919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6814919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6824919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6834919Sxy150489 			goto regs_map_fail;
6844919Sxy150489 		}
6854919Sxy150489 	}
6864919Sxy150489 
6874919Sxy150489 	return (DDI_SUCCESS);
6884919Sxy150489 
6894919Sxy150489 regs_map_fail:
6904919Sxy150489 	if (osdep->reg_handle != NULL)
6914919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
6924919Sxy150489 
6934919Sxy150489 	return (DDI_FAILURE);
6944919Sxy150489 }
6954919Sxy150489 
6964919Sxy150489 static int
6973526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
6983526Sxy150489 {
6993526Sxy150489 	struct e1000_hw *hw;
7004919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
7013526Sxy150489 
7024919Sxy150489 	hw = &Adapter->shared;
7034919Sxy150489 
7044919Sxy150489 	/* Set MAC type and initialize hardware functions */
7054919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7064919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7074919Sxy150489 		    "Could not setup hardware functions");
7083526Sxy150489 		return (DDI_FAILURE);
7093526Sxy150489 	}
7103526Sxy150489 
7114919Sxy150489 	/* Get bus information */
7124919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7134919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7144919Sxy150489 		    "Could not get bus information");
7154919Sxy150489 		return (DDI_FAILURE);
7163526Sxy150489 	}
7173526Sxy150489 
7183526Sxy150489 	/* get mem_base addr */
7194919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7204919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7213526Sxy150489 
7223526Sxy150489 	/* get io_base addr */
7234919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7244919Sxy150489 		if (bar64) {
7253526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7264919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7274919Sxy150489 			    PCI_CONF_BASE4);
7283526Sxy150489 		} else {
7293526Sxy150489 			/* normal 32-bit BAR mode */
7304919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7314919Sxy150489 			    PCI_CONF_BASE2);
7323526Sxy150489 		}
7333526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7343526Sxy150489 	} else {
7353526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7363526Sxy150489 		hw->io_base = 0x0;
7373526Sxy150489 	}
7383526Sxy150489 
7394919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7404919Sxy150489 
7414919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7424919Sxy150489 
7436735Scc210113 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
7446735Scc210113 	hw->phy.autoneg_wait_to_complete = B_FALSE;
7453526Sxy150489 
7463526Sxy150489 	/* Adaptive IFS related changes */
7474919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7484919Sxy150489 
7494919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7504919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7514919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7524919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7534919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7544919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7554919Sxy150489 
7564919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7574919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7583526Sxy150489 
7594608Syy150190 #ifdef __sparc
7604608Syy150190 	Adapter->strip_crc = B_TRUE;
7614608Syy150190 #else
7624608Syy150190 	Adapter->strip_crc = B_FALSE;
7634608Syy150190 #endif
7644608Syy150190 
7653526Sxy150489 	/* Get conf file properties */
7664919Sxy150489 	e1000g_get_conf(Adapter);
7674919Sxy150489 
7684919Sxy150489 	/* Get speed/duplex settings in conf file */
7694919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7704919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7713526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7723526Sxy150489 
7734919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7743526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7753526Sxy150489 
7763526Sxy150489 	/* Set Rx/Tx buffer size */
7776394Scc210113 	e1000g_set_bufsize(Adapter);
7784919Sxy150489 
7794919Sxy150489 	/* Master Latency Timer */
7804919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
7814919Sxy150489 
7823526Sxy150489 	/* copper options */
7836735Scc210113 	if (hw->phy.media_type == e1000_media_type_copper) {
7844919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
7854919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
7864919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
7873526Sxy150489 	}
7883526Sxy150489 
7894919Sxy150489 	/* The initial link state should be "unknown" */
7904061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
7914061Sxy150489 
7925882Syy150190 	/* Initialize rx parameters */
7935882Syy150190 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
7945882Syy150190 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
7955882Syy150190 
7964919Sxy150489 	/* Initialize tx parameters */
7974919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
7984919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
7995882Syy150190 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
8005882Syy150190 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
8014919Sxy150489 
8024919Sxy150489 	/* Initialize rx parameters */
8034919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8044919Sxy150489 
8053526Sxy150489 	return (DDI_SUCCESS);
8063526Sxy150489 }
8073526Sxy150489 
8086394Scc210113 static void
8096394Scc210113 e1000g_set_bufsize(struct e1000g *Adapter)
8106394Scc210113 {
8116394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
8126394Scc210113 	uint64_t rx_size;
8136394Scc210113 	uint64_t tx_size;
8146394Scc210113 
8158073SMin.Xu@Sun.COM 	dev_info_t *devinfo = Adapter->dip;
8166394Scc210113 #ifdef __sparc
8176394Scc210113 	ulong_t iommu_pagesize;
8188073SMin.Xu@Sun.COM #endif
8196394Scc210113 	/* Get the system page size */
8206394Scc210113 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
8218073SMin.Xu@Sun.COM 
8228073SMin.Xu@Sun.COM #ifdef __sparc
8236394Scc210113 	iommu_pagesize = dvma_pagesize(devinfo);
8246394Scc210113 	if (iommu_pagesize != 0) {
8256394Scc210113 		if (Adapter->sys_page_sz == iommu_pagesize) {
8266394Scc210113 			if (iommu_pagesize > 0x4000)
8276394Scc210113 				Adapter->sys_page_sz = 0x4000;
8286394Scc210113 		} else {
8296394Scc210113 			if (Adapter->sys_page_sz > iommu_pagesize)
8306394Scc210113 				Adapter->sys_page_sz = iommu_pagesize;
8316394Scc210113 		}
8326394Scc210113 	}
8336986Smx205022 	if (Adapter->lso_enable) {
8346986Smx205022 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
8356986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8366986Smx205022 	} else {
8376986Smx205022 		Adapter->dvma_page_num = Adapter->max_frame_size /
8386986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8396986Smx205022 	}
8406394Scc210113 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
8416394Scc210113 #endif
8426394Scc210113 
8436735Scc210113 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
8446735Scc210113 
8458417SChenlu.Chen@Sun.COM 	if (Adapter->mem_workaround_82546 &&
8468417SChenlu.Chen@Sun.COM 	    ((mac->type == e1000_82545) ||
8478178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546) ||
8488417SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546_rev_3))) {
8496394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
8508178SChenlu.Chen@Sun.COM 	} else {
8518178SChenlu.Chen@Sun.COM 		rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
8528178SChenlu.Chen@Sun.COM 		if ((rx_size > FRAME_SIZE_UPTO_2K) &&
8538178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_4K))
8548178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
8558178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
8568178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_8K))
8578178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
8588178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
8598178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_16K))
8608178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
8618178SChenlu.Chen@Sun.COM 		else
8628178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
8638178SChenlu.Chen@Sun.COM 	}
8646394Scc210113 
8656735Scc210113 	tx_size = Adapter->max_frame_size;
8666394Scc210113 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
8676394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
8686394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
8696394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_8K))
8706394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
8716394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
8726394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_16K))
8736394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
8746394Scc210113 	else
8756394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
8766394Scc210113 
8776394Scc210113 	/*
8786394Scc210113 	 * For Wiseman adapters we have an requirement of having receive
8796394Scc210113 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8806394Scc210113 	 * require this and forcing it for all hardwares will have
8816394Scc210113 	 * performance implications, I am making it applicable only for
8826394Scc210113 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8836394Scc210113 	 * it is okay to have normal frames...but it does involve a
8846394Scc210113 	 * potential risk where we may loose data if buffer is not
8856394Scc210113 	 * aligned...so all wiseman boards to have 256 byte aligned
8866394Scc210113 	 * buffers
8876394Scc210113 	 */
8886394Scc210113 	if (mac->type < e1000_82543)
8896394Scc210113 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
8906394Scc210113 	else
8916394Scc210113 		Adapter->rx_buf_align = 1;
8926394Scc210113 }
8936394Scc210113 
8943526Sxy150489 /*
8954919Sxy150489  * e1000g_detach - driver detach
8964919Sxy150489  *
8974919Sxy150489  * The detach() function is the complement of the attach routine.
8984919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
8994919Sxy150489  * state  associated  with  a  given  instance of a device node
9004919Sxy150489  * prior to the removal of that instance from the system.
9014919Sxy150489  *
9024919Sxy150489  * The detach() function will be called once for each  instance
9034919Sxy150489  * of the device for which there has been a successful attach()
9044919Sxy150489  * once there are no longer  any  opens  on  the  device.
9054919Sxy150489  *
9064919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
9074919Sxy150489  * driver are freed.
9083526Sxy150489  */
9093526Sxy150489 static int
9104919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
9113526Sxy150489 {
9123526Sxy150489 	struct e1000g *Adapter;
9134982Syy150190 	boolean_t rx_drain;
9143526Sxy150489 
9153526Sxy150489 	switch (cmd) {
9163526Sxy150489 	default:
9173526Sxy150489 		return (DDI_FAILURE);
9183526Sxy150489 
9193526Sxy150489 	case DDI_SUSPEND:
9203526Sxy150489 		return (e1000g_suspend(devinfo));
9213526Sxy150489 
9223526Sxy150489 	case DDI_DETACH:
9233526Sxy150489 		break;
9243526Sxy150489 	}
9253526Sxy150489 
9263526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
9273526Sxy150489 	if (Adapter == NULL)
9283526Sxy150489 		return (DDI_FAILURE);
9293526Sxy150489 
9308275SEric Cheng 	rx_drain = e1000g_rx_drain(Adapter);
9318275SEric Cheng 	if (!rx_drain && !e1000g_force_detach)
9328275SEric Cheng 		return (DDI_FAILURE);
9338275SEric Cheng 
9344919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9354919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
9364919Sxy150489 		return (DDI_FAILURE);
9374919Sxy150489 	}
9384919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
9394919Sxy150489 
9408479SChenlu.Chen@Sun.COM 	ASSERT(!(Adapter->e1000g_state & E1000G_STARTED));
9414982Syy150190 
9424982Syy150190 	/*
9434982Syy150190 	 * If e1000g_force_detach is enabled, driver detach is safe.
9444982Syy150190 	 * We will let e1000g_free_priv_devi_node routine determine
9454982Syy150190 	 * whether we need to free the priv_dip entry for current
9464982Syy150190 	 * driver instance.
9474982Syy150190 	 */
9484982Syy150190 	if (e1000g_force_detach) {
9494982Syy150190 		e1000g_free_priv_devi_node(Adapter, rx_drain);
9503526Sxy150489 	}
9513526Sxy150489 
9523526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9533526Sxy150489 
9543526Sxy150489 	return (DDI_SUCCESS);
9553526Sxy150489 }
9563526Sxy150489 
9574982Syy150190 /*
9584982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
9594982Syy150190  *
9604982Syy150190  * If free_flag is true, that indicates the upper layer is not holding
9614982Syy150190  * the rx buffers, we could free the priv_dip entry safely.
9624982Syy150190  *
9634982Syy150190  * Otherwise, we have to keep this entry even after driver detached,
9644982Syy150190  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
9654982Syy150190  * so that driver could free it while all of rx buffers are returned
9664982Syy150190  * by upper layer later.
9674982Syy150190  */
9684982Syy150190 static void
9694982Syy150190 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
9704982Syy150190 {
9714982Syy150190 	private_devi_list_t *devi_node, *devi_del;
9724982Syy150190 
9734982Syy150190 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
9744982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
9754982Syy150190 	ASSERT(Adapter->priv_dip != NULL);
9764982Syy150190 
9774982Syy150190 	devi_node = e1000g_private_devi_list;
9784982Syy150190 	if (devi_node->priv_dip == Adapter->priv_dip) {
9794982Syy150190 		if (free_flag) {
9804982Syy150190 			e1000g_private_devi_list =
9814982Syy150190 			    devi_node->next;
9824982Syy150190 			kmem_free(devi_node->priv_dip,
9834982Syy150190 			    sizeof (struct dev_info));
9844982Syy150190 			kmem_free(devi_node,
9854982Syy150190 			    sizeof (private_devi_list_t));
9864982Syy150190 		} else {
9874982Syy150190 			ASSERT(e1000g_mblks_pending != 0);
9884982Syy150190 			devi_node->flag =
9894982Syy150190 			    E1000G_PRIV_DEVI_DETACH;
9904982Syy150190 		}
9914982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
9924982Syy150190 		return;
9934982Syy150190 	}
9944982Syy150190 
9954982Syy150190 	devi_node = e1000g_private_devi_list;
9964982Syy150190 	while (devi_node->next != NULL) {
9974982Syy150190 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
9984982Syy150190 			if (free_flag) {
9994982Syy150190 				devi_del = devi_node->next;
10004982Syy150190 				devi_node->next = devi_del->next;
10014982Syy150190 				kmem_free(devi_del->priv_dip,
10024982Syy150190 				    sizeof (struct dev_info));
10034982Syy150190 				kmem_free(devi_del,
10044982Syy150190 				    sizeof (private_devi_list_t));
10054982Syy150190 			} else {
10064982Syy150190 				ASSERT(e1000g_mblks_pending != 0);
10074982Syy150190 				devi_node->next->flag =
10084982Syy150190 				    E1000G_PRIV_DEVI_DETACH;
10094982Syy150190 			}
10104982Syy150190 			break;
10114982Syy150190 		}
10124982Syy150190 		devi_node = devi_node->next;
10134982Syy150190 	}
10144982Syy150190 	rw_exit(&e1000g_rx_detach_lock);
10154982Syy150190 }
10164982Syy150190 
10173526Sxy150489 static void
10183526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
10193526Sxy150489 {
10207133Scc210113 	int result;
10217133Scc210113 
10224919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
10233526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
10243526Sxy150489 	}
10253526Sxy150489 
10264919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
10273526Sxy150489 		(void) mac_unregister(Adapter->mh);
10283526Sxy150489 	}
10293526Sxy150489 
10304919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
10313526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
10323526Sxy150489 	}
10333526Sxy150489 
10344919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10353526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10363526Sxy150489 	}
10373526Sxy150489 
10383526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10393526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10403526Sxy150489 	}
10413526Sxy150489 
10423526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10434919Sxy150489 		stop_link_timer(Adapter);
10447133Scc210113 
10457133Scc210113 		mutex_enter(&e1000g_nvm_lock);
10467133Scc210113 		result = e1000_reset_hw(&Adapter->shared);
10477133Scc210113 		mutex_exit(&e1000g_nvm_lock);
10487133Scc210113 
10497133Scc210113 		if (result != E1000_SUCCESS) {
10505273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10515273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10525273Sgl147354 		}
10533526Sxy150489 	}
10543526Sxy150489 
10554919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10564919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10574919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10584919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10594919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10603526Sxy150489 	}
10613526Sxy150489 
10624919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10634919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10644919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10653526Sxy150489 	}
10663526Sxy150489 
10673526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10683526Sxy150489 		e1000g_destroy_locks(Adapter);
10693526Sxy150489 	}
10703526Sxy150489 
10715273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10725273Sgl147354 		e1000g_fm_fini(Adapter);
10735273Sgl147354 	}
10745273Sgl147354 
10753526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
10763526Sxy150489 
10773526Sxy150489 	/*
10783526Sxy150489 	 * Another hotplug spec requirement,
10793526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
10803526Sxy150489 	 */
10813526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
10823526Sxy150489 }
10833526Sxy150489 
10843526Sxy150489 static void
10853526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
10863526Sxy150489 {
10873526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10883526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10893526Sxy150489 
10903526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
10913526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10924919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
10933526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10944919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
10953526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10963526Sxy150489 
10973526Sxy150489 	tx_ring = Adapter->tx_ring;
10983526Sxy150489 
10993526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
11003526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11013526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
11023526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11033526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
11043526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11053526Sxy150489 
11063526Sxy150489 	rx_ring = Adapter->rx_ring;
11073526Sxy150489 
11087436STed.You@Sun.COM 	mutex_init(&rx_ring->rx_lock, NULL,
11097436STed.You@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11103526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
11113526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11128275SEric Cheng 	mutex_init(&rx_ring->recycle_lock, NULL,
11138275SEric Cheng 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11143526Sxy150489 }
11153526Sxy150489 
11163526Sxy150489 static void
11173526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
11183526Sxy150489 {
11193526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11203526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11213526Sxy150489 
11223526Sxy150489 	tx_ring = Adapter->tx_ring;
11233526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
11243526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
11253526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
11263526Sxy150489 
11273526Sxy150489 	rx_ring = Adapter->rx_ring;
11287436STed.You@Sun.COM 	mutex_destroy(&rx_ring->rx_lock);
11293526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
11308275SEric Cheng 	mutex_destroy(&rx_ring->recycle_lock);
11313526Sxy150489 
11324919Sxy150489 	mutex_destroy(&Adapter->link_lock);
11334919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
11343526Sxy150489 	rw_destroy(&Adapter->chip_lock);
11353526Sxy150489 }
11363526Sxy150489 
11373526Sxy150489 static int
11383526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11393526Sxy150489 {
11403526Sxy150489 	struct e1000g *Adapter;
11413526Sxy150489 
11423526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11433526Sxy150489 	if (Adapter == NULL)
11448479SChenlu.Chen@Sun.COM 		e1000g_log(Adapter, CE_PANIC,
11458479SChenlu.Chen@Sun.COM 		    "Instance pointer is null\n");
11468479SChenlu.Chen@Sun.COM 
11478479SChenlu.Chen@Sun.COM 	if (Adapter->dip != devinfo)
11488479SChenlu.Chen@Sun.COM 		e1000g_log(Adapter, CE_PANIC,
11498479SChenlu.Chen@Sun.COM 		    "Devinfo is not the same as saved devinfo\n");
11508479SChenlu.Chen@Sun.COM 
11518479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11528479SChenlu.Chen@Sun.COM 
11538479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_STARTED) {
11548479SChenlu.Chen@Sun.COM 		if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
11558479SChenlu.Chen@Sun.COM 			rw_exit(&Adapter->chip_lock);
11568479SChenlu.Chen@Sun.COM 			/*
11578479SChenlu.Chen@Sun.COM 			 * We note the failure, but return success, as the
11588479SChenlu.Chen@Sun.COM 			 * system is still usable without this controller.
11598479SChenlu.Chen@Sun.COM 			 */
11608479SChenlu.Chen@Sun.COM 			e1000g_log(Adapter, CE_WARN,
11618479SChenlu.Chen@Sun.COM 			    "e1000g_resume: failed to restart controller\n");
11628479SChenlu.Chen@Sun.COM 			return (DDI_SUCCESS);
11638479SChenlu.Chen@Sun.COM 		}
11648479SChenlu.Chen@Sun.COM 		/* Enable and start the watchdog timer */
11658479SChenlu.Chen@Sun.COM 		enable_watchdog_timer(Adapter);
11668479SChenlu.Chen@Sun.COM 	}
11678479SChenlu.Chen@Sun.COM 
11688479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state &= ~E1000G_SUSPENDED;
11698479SChenlu.Chen@Sun.COM 
11708479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
11713526Sxy150489 
11723526Sxy150489 	return (DDI_SUCCESS);
11733526Sxy150489 }
11743526Sxy150489 
11753526Sxy150489 static int
11763526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11773526Sxy150489 {
11783526Sxy150489 	struct e1000g *Adapter;
11793526Sxy150489 
11803526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11813526Sxy150489 	if (Adapter == NULL)
11823526Sxy150489 		return (DDI_FAILURE);
11833526Sxy150489 
11848479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11858479SChenlu.Chen@Sun.COM 
11868479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state |= E1000G_SUSPENDED;
11878479SChenlu.Chen@Sun.COM 
11888479SChenlu.Chen@Sun.COM 	/* if the port isn't plumbed, we can simply return */
11898479SChenlu.Chen@Sun.COM 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
11908479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
11918479SChenlu.Chen@Sun.COM 		return (DDI_SUCCESS);
11928479SChenlu.Chen@Sun.COM 	}
11938479SChenlu.Chen@Sun.COM 
11948479SChenlu.Chen@Sun.COM 	e1000g_stop(Adapter, B_FALSE);
11958479SChenlu.Chen@Sun.COM 
11968479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
11978479SChenlu.Chen@Sun.COM 
11988479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
11998479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
12008479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
12018479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
12023526Sxy150489 
12033526Sxy150489 	return (DDI_SUCCESS);
12043526Sxy150489 }
12053526Sxy150489 
12063526Sxy150489 static int
12073526Sxy150489 e1000g_init(struct e1000g *Adapter)
12083526Sxy150489 {
12093526Sxy150489 	uint32_t pba;
12104919Sxy150489 	uint32_t high_water;
12113526Sxy150489 	struct e1000_hw *hw;
12124061Sxy150489 	clock_t link_timeout;
12137133Scc210113 	int result;
12143526Sxy150489 
12154919Sxy150489 	hw = &Adapter->shared;
12163526Sxy150489 
12173526Sxy150489 	/*
12183526Sxy150489 	 * reset to put the hardware in a known state
12193526Sxy150489 	 * before we try to do anything with the eeprom
12203526Sxy150489 	 */
12217133Scc210113 	mutex_enter(&e1000g_nvm_lock);
12227133Scc210113 	result = e1000_reset_hw(hw);
12237133Scc210113 	mutex_exit(&e1000g_nvm_lock);
12247133Scc210113 
12257133Scc210113 	if (result != E1000_SUCCESS) {
12265273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12275273Sgl147354 		goto init_fail;
12285273Sgl147354 	}
12293526Sxy150489 
12307133Scc210113 	mutex_enter(&e1000g_nvm_lock);
12317133Scc210113 	result = e1000_validate_nvm_checksum(hw);
12327133Scc210113 	if (result < E1000_SUCCESS) {
12334061Sxy150489 		/*
12344061Sxy150489 		 * Some PCI-E parts fail the first check due to
12354061Sxy150489 		 * the link being in sleep state.  Call it again,
12364061Sxy150489 		 * if it fails a second time its a real issue.
12374061Sxy150489 		 */
12387133Scc210113 		result = e1000_validate_nvm_checksum(hw);
12397133Scc210113 	}
12407133Scc210113 	mutex_exit(&e1000g_nvm_lock);
12417133Scc210113 
12427133Scc210113 	if (result < E1000_SUCCESS) {
12437133Scc210113 		e1000g_log(Adapter, CE_WARN,
12447133Scc210113 		    "Invalid NVM checksum. Please contact "
12457133Scc210113 		    "the vendor to update the NVM.");
12467133Scc210113 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12477133Scc210113 		goto init_fail;
12487133Scc210113 	}
12497133Scc210113 
12507133Scc210113 	result = 0;
12513526Sxy150489 #ifdef __sparc
12523526Sxy150489 	/*
12537607STed.You@Sun.COM 	 * First, we try to get the local ethernet address from OBP. If
12547133Scc210113 	 * failed, then we get it from the EEPROM of NIC card.
12553526Sxy150489 	 */
12567133Scc210113 	result = e1000g_find_mac_address(Adapter);
12577133Scc210113 #endif
12583526Sxy150489 	/* Get the local ethernet address. */
12597133Scc210113 	if (!result) {
12607133Scc210113 		mutex_enter(&e1000g_nvm_lock);
12617140Scc210113 		result = e1000_read_mac_addr(hw);
12627133Scc210113 		mutex_exit(&e1000g_nvm_lock);
12637133Scc210113 	}
12647133Scc210113 
12657133Scc210113 	if (result < E1000_SUCCESS) {
12663526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
12675273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12683526Sxy150489 		goto init_fail;
12693526Sxy150489 	}
12703526Sxy150489 
12713526Sxy150489 	/* check for valid mac address */
12724919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12733526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
12745273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12753526Sxy150489 		goto init_fail;
12763526Sxy150489 	}
12773526Sxy150489 
12784919Sxy150489 	/* Set LAA state for 82571 chipset */
12794919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
12803526Sxy150489 
12813526Sxy150489 	/* Master Latency Timer implementation */
12824919Sxy150489 	if (Adapter->master_latency_timer) {
12834919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
12844919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
12853526Sxy150489 	}
12863526Sxy150489 
12874919Sxy150489 	if (hw->mac.type < e1000_82547) {
12883526Sxy150489 		/*
12893526Sxy150489 		 * Total FIFO is 64K
12903526Sxy150489 		 */
12916735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12923526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
12933526Sxy150489 		else
12943526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
12957140Scc210113 	} else if ((hw->mac.type == e1000_82571) ||
12967140Scc210113 	    (hw->mac.type == e1000_82572) ||
12977140Scc210113 	    (hw->mac.type == e1000_80003es2lan)) {
12983526Sxy150489 		/*
12993526Sxy150489 		 * Total FIFO is 48K
13003526Sxy150489 		 */
13016735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
13023526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
13033526Sxy150489 		else
13043526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
13057607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82573) {
13067607STed.You@Sun.COM 		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
13077607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82574) {
13087607STed.You@Sun.COM 		/* Keep adapter default: 20K for Rx, 20K for Tx */
13097607STed.You@Sun.COM 		pba = E1000_READ_REG(hw, E1000_PBA);
13104919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
13113526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
13124919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
13137607STed.You@Sun.COM 		pba = E1000_PBA_10K;
13147607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_ich10lan) {
13157607STed.You@Sun.COM 		pba = E1000_PBA_10K;
13163526Sxy150489 	} else {
13173526Sxy150489 		/*
13183526Sxy150489 		 * Total FIFO is 40K
13193526Sxy150489 		 */
13206735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
13213526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
13223526Sxy150489 		else
13233526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
13243526Sxy150489 	}
13254919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
13263526Sxy150489 
13273526Sxy150489 	/*
13283526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
13293526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
13303526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
13313526Sxy150489 	 * file.
13323526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
13333526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
13343526Sxy150489 	 * high-water mark.
13354919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
13364919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
13374919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
13384919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
13394919Sxy150489 	 * Rx FIFO size minus one full frame.
13403526Sxy150489 	 */
13414919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
13428479SChenlu.Chen@Sun.COM 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_82574 ||
13438479SChenlu.Chen@Sun.COM 	    hw->mac.type == e1000_ich9lan || hw->mac.type == e1000_ich10lan) ?
13444919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
13456735Scc210113 	    ((pba << 10) - Adapter->max_frame_size)));
13466735Scc210113 
13476735Scc210113 	hw->fc.high_water = high_water & 0xFFF8;
13486735Scc210113 	hw->fc.low_water = hw->fc.high_water - 8;
13494919Sxy150489 
13504919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
13516735Scc210113 		hw->fc.pause_time = 0xFFFF;
13524919Sxy150489 	else
13536735Scc210113 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
13546735Scc210113 	hw->fc.send_xon = B_TRUE;
13553526Sxy150489 
13563526Sxy150489 	/*
13573526Sxy150489 	 * Reset the adapter hardware the second time.
13583526Sxy150489 	 */
13597133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13607133Scc210113 	result = e1000_reset_hw(hw);
13617133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13627133Scc210113 
13637133Scc210113 	if (result != E1000_SUCCESS) {
13645273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13655273Sgl147354 		goto init_fail;
13665273Sgl147354 	}
13673526Sxy150489 
13683526Sxy150489 	/* disable wakeup control by default */
13694919Sxy150489 	if (hw->mac.type >= e1000_82544)
13704919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
13713526Sxy150489 
13728178SChenlu.Chen@Sun.COM 	/*
13738178SChenlu.Chen@Sun.COM 	 * MWI should be disabled on 82546.
13748178SChenlu.Chen@Sun.COM 	 */
13758178SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82546)
13768178SChenlu.Chen@Sun.COM 		e1000_pci_clear_mwi(hw);
13778178SChenlu.Chen@Sun.COM 	else
13788178SChenlu.Chen@Sun.COM 		e1000_pci_set_mwi(hw);
13793526Sxy150489 
13803526Sxy150489 	/*
13813526Sxy150489 	 * Configure/Initialize hardware
13823526Sxy150489 	 */
13837133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13847133Scc210113 	result = e1000_init_hw(hw);
13857133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13867133Scc210113 
13877133Scc210113 	if (result < E1000_SUCCESS) {
13883526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
13895273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13903526Sxy150489 		goto init_fail;
13913526Sxy150489 	}
13923526Sxy150489 
13937436STed.You@Sun.COM 	/*
13947436STed.You@Sun.COM 	 * Restore LED settings to the default from EEPROM
13957436STed.You@Sun.COM 	 * to meet the standard for Sun platforms.
13967436STed.You@Sun.COM 	 */
13978572SMin.Xu@Sun.COM 	if ((hw->mac.type != e1000_82541) &&
13988572SMin.Xu@Sun.COM 	    (hw->mac.type != e1000_82541_rev_2) &&
13998572SMin.Xu@Sun.COM 	    (hw->mac.type != e1000_82547) &&
14008572SMin.Xu@Sun.COM 	    (hw->mac.type != e1000_82547_rev_2))
14018572SMin.Xu@Sun.COM 		(void) e1000_cleanup_led(hw);
14027436STed.You@Sun.COM 
14033526Sxy150489 	/* Disable Smart Power Down */
14043526Sxy150489 	phy_spd_state(hw, B_FALSE);
14053526Sxy150489 
14065082Syy150190 	/* Make sure driver has control */
14075082Syy150190 	e1000g_get_driver_control(hw);
14085082Syy150190 
14093526Sxy150489 	/*
14103526Sxy150489 	 * Initialize unicast addresses.
14113526Sxy150489 	 */
14123526Sxy150489 	e1000g_init_unicst(Adapter);
14133526Sxy150489 
14143526Sxy150489 	/*
14153526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
14163526Sxy150489 	 * completes  Multicast table will be set
14173526Sxy150489 	 */
14184919Sxy150489 	e1000g_setup_multicast(Adapter);
14194919Sxy150489 	msec_delay(5);
14203526Sxy150489 
14213526Sxy150489 	/*
14223526Sxy150489 	 * Implement Adaptive IFS
14233526Sxy150489 	 */
14243526Sxy150489 	e1000_reset_adaptive(hw);
14253526Sxy150489 
14263526Sxy150489 	/* Setup Interrupt Throttling Register */
14275882Syy150190 	if (hw->mac.type >= e1000_82540) {
14285882Syy150190 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
14295882Syy150190 	} else
14305882Syy150190 		Adapter->intr_adaptive = B_FALSE;
14313526Sxy150489 
14324061Sxy150489 	/* Start the timer for link setup */
14334919Sxy150489 	if (hw->mac.autoneg)
14344919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
14354061Sxy150489 	else
14364919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
14374919Sxy150489 
14384919Sxy150489 	mutex_enter(&Adapter->link_lock);
14396735Scc210113 	if (hw->phy.autoneg_wait_to_complete) {
14404061Sxy150489 		Adapter->link_complete = B_TRUE;
14413526Sxy150489 	} else {
14424061Sxy150489 		Adapter->link_complete = B_FALSE;
14434061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
14444061Sxy150489 		    (void *)Adapter, link_timeout);
14453526Sxy150489 	}
14464919Sxy150489 	mutex_exit(&Adapter->link_lock);
14473526Sxy150489 
14483526Sxy150489 	/* Enable PCI-Ex master */
14494919Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
14503526Sxy150489 		e1000_enable_pciex_master(hw);
14513526Sxy150489 	}
14523526Sxy150489 
14535082Syy150190 	/* Save the state of the phy */
14545082Syy150190 	e1000g_get_phy_state(Adapter);
14555082Syy150190 
14566512Ssowmini 	e1000g_param_sync(Adapter);
14576512Ssowmini 
14583526Sxy150489 	Adapter->init_count++;
14593526Sxy150489 
14605273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
14615273Sgl147354 		goto init_fail;
14625273Sgl147354 	}
14635273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
14645273Sgl147354 		goto init_fail;
14655273Sgl147354 	}
14665273Sgl147354 
14678275SEric Cheng 	Adapter->poll_mode = e1000g_poll_mode;
14688275SEric Cheng 
14693526Sxy150489 	return (DDI_SUCCESS);
14703526Sxy150489 
14713526Sxy150489 init_fail:
14725273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
14733526Sxy150489 	return (DDI_FAILURE);
14743526Sxy150489 }
14753526Sxy150489 
14763526Sxy150489 /*
14773526Sxy150489  * Check if the link is up
14783526Sxy150489  */
14793526Sxy150489 static boolean_t
14803526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
14813526Sxy150489 {
14823526Sxy150489 	struct e1000_hw *hw;
14833526Sxy150489 	boolean_t link_up;
14843526Sxy150489 
14854919Sxy150489 	hw = &Adapter->shared;
14863526Sxy150489 
14877426SChenliang.Xu@Sun.COM 	(void) e1000_check_for_link(hw);
14883526Sxy150489 
14894919Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
14904919Sxy150489 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
14916735Scc210113 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
14924919Sxy150489 	    (hw->mac.serdes_has_link))) {
14933526Sxy150489 		link_up = B_TRUE;
14943526Sxy150489 	} else {
14953526Sxy150489 		link_up = B_FALSE;
14963526Sxy150489 	}
14973526Sxy150489 
14983526Sxy150489 	return (link_up);
14993526Sxy150489 }
15003526Sxy150489 
15013526Sxy150489 static void
15023526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
15033526Sxy150489 {
15043526Sxy150489 	struct iocblk *iocp;
15053526Sxy150489 	struct e1000g *e1000gp;
15063526Sxy150489 	enum ioc_reply status;
15077426SChenliang.Xu@Sun.COM 
15087426SChenliang.Xu@Sun.COM 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
15093526Sxy150489 	iocp->ioc_error = 0;
15103526Sxy150489 	e1000gp = (struct e1000g *)arg;
15113526Sxy150489 
15123526Sxy150489 	ASSERT(e1000gp);
15133526Sxy150489 	if (e1000gp == NULL) {
15143526Sxy150489 		miocnak(q, mp, 0, EINVAL);
15153526Sxy150489 		return;
15163526Sxy150489 	}
15173526Sxy150489 
15188479SChenlu.Chen@Sun.COM 	rw_enter(&e1000gp->chip_lock, RW_READER);
15198479SChenlu.Chen@Sun.COM 	if (e1000gp->e1000g_state & E1000G_SUSPENDED) {
15208479SChenlu.Chen@Sun.COM 		rw_exit(&e1000gp->chip_lock);
15218479SChenlu.Chen@Sun.COM 		miocnak(q, mp, 0, EINVAL);
15228479SChenlu.Chen@Sun.COM 		return;
15238479SChenlu.Chen@Sun.COM 	}
15248479SChenlu.Chen@Sun.COM 	rw_exit(&e1000gp->chip_lock);
15258479SChenlu.Chen@Sun.COM 
15263526Sxy150489 	switch (iocp->ioc_cmd) {
15273526Sxy150489 
15283526Sxy150489 	case LB_GET_INFO_SIZE:
15293526Sxy150489 	case LB_GET_INFO:
15303526Sxy150489 	case LB_GET_MODE:
15313526Sxy150489 	case LB_SET_MODE:
15323526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
15333526Sxy150489 		break;
15343526Sxy150489 
15353526Sxy150489 
15364919Sxy150489 #ifdef E1000G_DEBUG
15373526Sxy150489 	case E1000G_IOC_REG_PEEK:
15383526Sxy150489 	case E1000G_IOC_REG_POKE:
15393526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
15403526Sxy150489 		break;
15413526Sxy150489 	case E1000G_IOC_CHIP_RESET:
15423526Sxy150489 		e1000gp->reset_count++;
15437656SSherry.Moore@Sun.COM 		if (e1000g_reset_adapter(e1000gp))
15443526Sxy150489 			status = IOC_ACK;
15453526Sxy150489 		else
15463526Sxy150489 			status = IOC_INVAL;
15473526Sxy150489 		break;
15484919Sxy150489 #endif
15493526Sxy150489 	default:
15503526Sxy150489 		status = IOC_INVAL;
15513526Sxy150489 		break;
15523526Sxy150489 	}
15533526Sxy150489 
15543526Sxy150489 	/*
15553526Sxy150489 	 * Decide how to reply
15563526Sxy150489 	 */
15573526Sxy150489 	switch (status) {
15583526Sxy150489 	default:
15593526Sxy150489 	case IOC_INVAL:
15603526Sxy150489 		/*
15613526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
15623526Sxy150489 		 */
15633526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
15644349Sxy150489 		    EINVAL : iocp->ioc_error);
15653526Sxy150489 		break;
15663526Sxy150489 
15673526Sxy150489 	case IOC_DONE:
15683526Sxy150489 		/*
15693526Sxy150489 		 * OK, reply already sent
15703526Sxy150489 		 */
15713526Sxy150489 		break;
15723526Sxy150489 
15733526Sxy150489 	case IOC_ACK:
15743526Sxy150489 		/*
15753526Sxy150489 		 * OK, reply with an ACK
15763526Sxy150489 		 */
15773526Sxy150489 		miocack(q, mp, 0, 0);
15783526Sxy150489 		break;
15793526Sxy150489 
15803526Sxy150489 	case IOC_REPLY:
15813526Sxy150489 		/*
15823526Sxy150489 		 * OK, send prepared reply as ACK or NAK
15833526Sxy150489 		 */
15843526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
15854349Sxy150489 		    M_IOCACK : M_IOCNAK;
15863526Sxy150489 		qreply(q, mp);
15873526Sxy150489 		break;
15883526Sxy150489 	}
15893526Sxy150489 }
15903526Sxy150489 
15918275SEric Cheng /*
15928275SEric Cheng  * The default value of e1000g_poll_mode == 0 assumes that the NIC is
15938275SEric Cheng  * capable of supporting only one interrupt and we shouldn't disable
15948275SEric Cheng  * the physical interrupt. In this case we let the interrupt come and
15958275SEric Cheng  * we queue the packets in the rx ring itself in case we are in polling
15968275SEric Cheng  * mode (better latency but slightly lower performance and a very
15978275SEric Cheng  * high intrrupt count in mpstat which is harmless).
15988275SEric Cheng  *
15998275SEric Cheng  * e1000g_poll_mode == 1 assumes that we have per Rx ring interrupt
16008275SEric Cheng  * which can be disabled in poll mode. This gives better overall
16018275SEric Cheng  * throughput (compared to the mode above), shows very low interrupt
16028275SEric Cheng  * count but has slightly higher latency since we pick the packets when
16038275SEric Cheng  * the poll thread does polling.
16048275SEric Cheng  *
16058275SEric Cheng  * Currently, this flag should be enabled only while doing performance
16068275SEric Cheng  * measurement or when it can be guaranteed that entire NIC going
16078275SEric Cheng  * in poll mode will not harm any traffic like cluster heartbeat etc.
16088275SEric Cheng  */
16098275SEric Cheng int e1000g_poll_mode = 0;
16108275SEric Cheng 
16118275SEric Cheng /*
16128275SEric Cheng  * Called from the upper layers when driver is in polling mode to
16138275SEric Cheng  * pick up any queued packets. Care should be taken to not block
16148275SEric Cheng  * this thread.
16158275SEric Cheng  */
16168275SEric Cheng static mblk_t *e1000g_poll_ring(void *arg, int bytes_to_pickup)
16178275SEric Cheng {
16188275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)arg;
16198275SEric Cheng 	mblk_t			*mp = NULL;
16208275SEric Cheng 	mblk_t			*tail;
16218275SEric Cheng 	struct e1000g 		*adapter;
16228275SEric Cheng 
16238275SEric Cheng 	adapter = rx_ring->adapter;
16248275SEric Cheng 
16258479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
16268479SChenlu.Chen@Sun.COM 
16278479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
16288479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
16298479SChenlu.Chen@Sun.COM 		return (NULL);
16308479SChenlu.Chen@Sun.COM 	}
16318479SChenlu.Chen@Sun.COM 
16328275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
1633*8833SVenu.Iyer@Sun.COM 	mp = e1000g_receive(rx_ring, &tail, bytes_to_pickup);
16348275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
16358479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
16368275SEric Cheng 	return (mp);
16378275SEric Cheng }
16388275SEric Cheng 
16393526Sxy150489 static int
16403526Sxy150489 e1000g_m_start(void *arg)
16413526Sxy150489 {
16423526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
16433526Sxy150489 
16448479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16458479SChenlu.Chen@Sun.COM 
16468479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
16478479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
16488479SChenlu.Chen@Sun.COM 		return (ECANCELED);
16498479SChenlu.Chen@Sun.COM 	}
16508479SChenlu.Chen@Sun.COM 
16518479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
16528479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
16538479SChenlu.Chen@Sun.COM 		return (ENOTACTIVE);
16548479SChenlu.Chen@Sun.COM 	}
16558479SChenlu.Chen@Sun.COM 
16568479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state |= E1000G_STARTED;
16578479SChenlu.Chen@Sun.COM 
16588479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
16598479SChenlu.Chen@Sun.COM 
16608479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
16618479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
16628479SChenlu.Chen@Sun.COM 
16638479SChenlu.Chen@Sun.COM 	return (0);
16643526Sxy150489 }
16653526Sxy150489 
16663526Sxy150489 static int
16674919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
16683526Sxy150489 {
16694919Sxy150489 	if (global) {
16704919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
16714919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
16724919Sxy150489 			e1000g_log(Adapter, CE_WARN,
16734919Sxy150489 			    "Alloc DMA resources failed");
16748479SChenlu.Chen@Sun.COM 			return (DDI_FAILURE);
16754919Sxy150489 		}
16764919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
16774919Sxy150489 	}
16784919Sxy150489 
16793526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
16803526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
16813526Sxy150489 			e1000g_log(Adapter, CE_WARN,
16823526Sxy150489 			    "Adapter initialization failed");
16834919Sxy150489 			if (global)
16844919Sxy150489 				e1000g_release_dma_resources(Adapter);
16858479SChenlu.Chen@Sun.COM 			return (DDI_FAILURE);
16863526Sxy150489 		}
16873526Sxy150489 	}
16883526Sxy150489 
16894919Sxy150489 	/* Setup and initialize the transmit structures */
16904919Sxy150489 	e1000g_tx_setup(Adapter);
16914919Sxy150489 	msec_delay(5);
16924919Sxy150489 
16934919Sxy150489 	/* Setup and initialize the receive structures */
16944919Sxy150489 	e1000g_rx_setup(Adapter);
16954919Sxy150489 	msec_delay(5);
16964919Sxy150489 
16977722SShuguo.Yang@Sun.COM 	/* Restore the e1000g promiscuous mode */
16987722SShuguo.Yang@Sun.COM 	e1000g_restore_promisc(Adapter);
16997722SShuguo.Yang@Sun.COM 
17004919Sxy150489 	e1000g_mask_interrupt(Adapter);
17013526Sxy150489 
17028479SChenlu.Chen@Sun.COM 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
17038479SChenlu.Chen@Sun.COM 
17045273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
17055273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
17068479SChenlu.Chen@Sun.COM 		return (DDI_FAILURE);
17078479SChenlu.Chen@Sun.COM 	}
17088479SChenlu.Chen@Sun.COM 
17098479SChenlu.Chen@Sun.COM 	return (DDI_SUCCESS);
17103526Sxy150489 }
17113526Sxy150489 
17123526Sxy150489 static void
17133526Sxy150489 e1000g_m_stop(void *arg)
17143526Sxy150489 {
17153526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
17163526Sxy150489 
17178479SChenlu.Chen@Sun.COM 	/* Drain tx sessions */
17188479SChenlu.Chen@Sun.COM 	(void) e1000g_tx_drain(Adapter);
17198479SChenlu.Chen@Sun.COM 
17208479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
17218479SChenlu.Chen@Sun.COM 
17228479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
17238479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
17248479SChenlu.Chen@Sun.COM 		return;
17258479SChenlu.Chen@Sun.COM 	}
17268479SChenlu.Chen@Sun.COM 	Adapter->e1000g_state &= ~E1000G_STARTED;
17274919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
17288479SChenlu.Chen@Sun.COM 
17298479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
17308479SChenlu.Chen@Sun.COM 
17318479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
17328479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
17338479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
17348479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
17353526Sxy150489 }
17363526Sxy150489 
17373526Sxy150489 static void
17384919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
17393526Sxy150489 {
17407133Scc210113 	int result;
17417133Scc210113 
17423526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
17433526Sxy150489 
17443526Sxy150489 	/* Stop the chip and release pending resources */
17453526Sxy150489 
17467607STed.You@Sun.COM 	/* Tell firmware driver is no longer in control */
17477607STed.You@Sun.COM 	e1000g_release_driver_control(&Adapter->shared);
17487607STed.You@Sun.COM 
17494919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
17507133Scc210113 
17517133Scc210113 	mutex_enter(&e1000g_nvm_lock);
17527133Scc210113 	result = e1000_reset_hw(&Adapter->shared);
17537133Scc210113 	mutex_exit(&e1000g_nvm_lock);
17547133Scc210113 
17557133Scc210113 	if (result != E1000_SUCCESS) {
17565273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
17575273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
17585273Sgl147354 	}
17593526Sxy150489 
17603526Sxy150489 	/* Release resources still held by the TX descriptors */
17614919Sxy150489 	e1000g_tx_clean(Adapter);
17624061Sxy150489 
17635273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
17645273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
17655273Sgl147354 
17664061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
17674919Sxy150489 	e1000g_rx_clean(Adapter);
17684061Sxy150489 
17694919Sxy150489 	if (global)
17704919Sxy150489 		e1000g_release_dma_resources(Adapter);
17714061Sxy150489 }
17724061Sxy150489 
17734061Sxy150489 static void
17744919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
17754919Sxy150489 {
17764919Sxy150489 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
17774919Sxy150489 
17784919Sxy150489 	if (rx_ring->rx_mblk != NULL) {
17794919Sxy150489 		freemsg(rx_ring->rx_mblk);
17804919Sxy150489 		rx_ring->rx_mblk = NULL;
17814919Sxy150489 		rx_ring->rx_mblk_tail = NULL;
17824919Sxy150489 		rx_ring->rx_mblk_len = 0;
17834919Sxy150489 	}
17844919Sxy150489 }
17854919Sxy150489 
17864919Sxy150489 static void
17874919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
17884061Sxy150489 {
17894061Sxy150489 	e1000g_tx_ring_t *tx_ring;
17904919Sxy150489 	p_tx_sw_packet_t packet;
17914061Sxy150489 	mblk_t *mp;
17924061Sxy150489 	mblk_t *nmp;
17934061Sxy150489 	uint32_t packet_count;
17944061Sxy150489 
17954061Sxy150489 	tx_ring = Adapter->tx_ring;
17964061Sxy150489 
17973526Sxy150489 	/*
17983526Sxy150489 	 * Here we don't need to protect the lists using
17993526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
18003526Sxy150489 	 * have been protected by the chip_lock.
18013526Sxy150489 	 */
18023526Sxy150489 	mp = NULL;
18033526Sxy150489 	nmp = NULL;
18044061Sxy150489 	packet_count = 0;
18054919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
18063526Sxy150489 	while (packet != NULL) {
18073526Sxy150489 		if (packet->mp != NULL) {
18083526Sxy150489 			/* Assemble the message chain */
18093526Sxy150489 			if (mp == NULL) {
18103526Sxy150489 				mp = packet->mp;
18113526Sxy150489 				nmp = packet->mp;
18123526Sxy150489 			} else {
18133526Sxy150489 				nmp->b_next = packet->mp;
18143526Sxy150489 				nmp = packet->mp;
18153526Sxy150489 			}
18163526Sxy150489 			/* Disconnect the message from the sw packet */
18173526Sxy150489 			packet->mp = NULL;
18183526Sxy150489 		}
18193526Sxy150489 
18204919Sxy150489 		e1000g_free_tx_swpkt(packet);
18214061Sxy150489 		packet_count++;
18223526Sxy150489 
18234919Sxy150489 		packet = (p_tx_sw_packet_t)
18243526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
18253526Sxy150489 	}
18263526Sxy150489 
18275882Syy150190 	if (mp != NULL)
18285882Syy150190 		freemsgchain(mp);
18294061Sxy150489 
18304061Sxy150489 	if (packet_count > 0) {
18314061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
18324061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
18334061Sxy150489 
18344061Sxy150489 		/* Setup TX descriptor pointers */
18354061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
18364061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
18374061Sxy150489 
18384061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
18396735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
18406735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
18413526Sxy150489 	}
18423526Sxy150489 }
18433526Sxy150489 
18443526Sxy150489 static boolean_t
18453526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
18463526Sxy150489 {
18473526Sxy150489 	int i;
18483526Sxy150489 	boolean_t done;
18493526Sxy150489 	e1000g_tx_ring_t *tx_ring;
18503526Sxy150489 
18513526Sxy150489 	tx_ring = Adapter->tx_ring;
18523526Sxy150489 
18533526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
18544919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
18553526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
18563526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
18573526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
18583526Sxy150489 
18593526Sxy150489 		if (done)
18603526Sxy150489 			break;
18613526Sxy150489 
18623526Sxy150489 		msec_delay(1);
18633526Sxy150489 	}
18643526Sxy150489 
18653526Sxy150489 	return (done);
18663526Sxy150489 }
18673526Sxy150489 
18683526Sxy150489 static boolean_t
18693526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
18703526Sxy150489 {
18714919Sxy150489 	e1000g_rx_ring_t *rx_ring;
18724919Sxy150489 	p_rx_sw_packet_t packet;
18733526Sxy150489 	boolean_t done;
18743526Sxy150489 
18754919Sxy150489 	rx_ring = Adapter->rx_ring;
18764919Sxy150489 	done = B_TRUE;
18774919Sxy150489 
18784919Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
18794919Sxy150489 
18804919Sxy150489 	while (rx_ring->pending_list != NULL) {
18814919Sxy150489 		packet = rx_ring->pending_list;
18824919Sxy150489 		rx_ring->pending_list =
18834919Sxy150489 		    rx_ring->pending_list->next;
18844919Sxy150489 
18854919Sxy150489 		if (packet->flag == E1000G_RX_SW_STOP) {
18864919Sxy150489 			packet->flag = E1000G_RX_SW_DETACH;
18874919Sxy150489 			done = B_FALSE;
18884919Sxy150489 		} else {
18894919Sxy150489 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
18904919Sxy150489 			ASSERT(packet->mp == NULL);
18914919Sxy150489 			e1000g_free_rx_sw_packet(packet);
18924919Sxy150489 		}
18934919Sxy150489 	}
18944919Sxy150489 
18954919Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
18963526Sxy150489 
18973526Sxy150489 	return (done);
18983526Sxy150489 }
18993526Sxy150489 
19007656SSherry.Moore@Sun.COM static boolean_t
19017656SSherry.Moore@Sun.COM e1000g_reset_adapter(struct e1000g *Adapter)
19023526Sxy150489 {
19038479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
19048479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
19058479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
19068479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
19078479SChenlu.Chen@Sun.COM 
19088479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
19098479SChenlu.Chen@Sun.COM 
19104919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
19114919Sxy150489 
19128479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
19138479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
19143526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
19158479SChenlu.Chen@Sun.COM 			return (B_FALSE);
19168479SChenlu.Chen@Sun.COM 	}
19178479SChenlu.Chen@Sun.COM 
19188479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
19198479SChenlu.Chen@Sun.COM 
19208479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
19218479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
19223526Sxy150489 
19233526Sxy150489 	return (B_TRUE);
19243526Sxy150489 }
19253526Sxy150489 
19265273Sgl147354 boolean_t
19275273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
19285273Sgl147354 {
19298479SChenlu.Chen@Sun.COM 	/* Disable and stop all the timers */
19308479SChenlu.Chen@Sun.COM 	disable_watchdog_timer(Adapter);
19318479SChenlu.Chen@Sun.COM 	stop_link_timer(Adapter);
19328479SChenlu.Chen@Sun.COM 	stop_82547_timer(Adapter->tx_ring);
19338479SChenlu.Chen@Sun.COM 
19348479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
19358479SChenlu.Chen@Sun.COM 
19365273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
19375273Sgl147354 
19385273Sgl147354 	Adapter->init_count = 0;
19395273Sgl147354 
19408479SChenlu.Chen@Sun.COM 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
19418479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
19425273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
19435273Sgl147354 		return (B_FALSE);
19445273Sgl147354 	}
19455273Sgl147354 
19468479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
19478479SChenlu.Chen@Sun.COM 
19488479SChenlu.Chen@Sun.COM 	/* Enable and start the watchdog timer */
19498479SChenlu.Chen@Sun.COM 	enable_watchdog_timer(Adapter);
19508479SChenlu.Chen@Sun.COM 
19515273Sgl147354 	return (B_TRUE);
19525273Sgl147354 }
19535273Sgl147354 
19543526Sxy150489 /*
19554919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
19564919Sxy150489  *
19574919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
19584919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
19594919Sxy150489  * bit is set.
19603526Sxy150489  */
19613526Sxy150489 static uint_t
19623526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
19633526Sxy150489 {
19643526Sxy150489 	struct e1000g *Adapter;
19654919Sxy150489 	uint32_t icr;
19663526Sxy150489 
19677426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
19684919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
19694919Sxy150489 
19705273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
19715273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
19725273Sgl147354 
19734919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
19743526Sxy150489 		/*
19753526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
19763526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
19773526Sxy150489 		 * look for work to do.
19783526Sxy150489 		 */
19794919Sxy150489 		e1000g_intr_work(Adapter, icr);
19803526Sxy150489 		return (DDI_INTR_CLAIMED);
19813526Sxy150489 	} else {
19823526Sxy150489 		/*
19833526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
19843526Sxy150489 		 * Don't claim this interrupt, return immediately.
19853526Sxy150489 		 */
19863526Sxy150489 		return (DDI_INTR_UNCLAIMED);
19873526Sxy150489 	}
19883526Sxy150489 }
19893526Sxy150489 
19903526Sxy150489 /*
19914919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
19924919Sxy150489  *
19934919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
19944919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
19954919Sxy150489  * bit is set or not.
19963526Sxy150489  */
19973526Sxy150489 static uint_t
19983526Sxy150489 e1000g_intr(caddr_t arg)
19993526Sxy150489 {
20003526Sxy150489 	struct e1000g *Adapter;
20014919Sxy150489 	uint32_t icr;
20023526Sxy150489 
20037426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
20044919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
20054919Sxy150489 
20065273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
20075273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
20085273Sgl147354 
20094919Sxy150489 	if (icr) {
20103526Sxy150489 		/*
20113526Sxy150489 		 * Any bit was set in ICR:
20123526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
20133526Sxy150489 		 * look for work to do.
20143526Sxy150489 		 */
20154919Sxy150489 		e1000g_intr_work(Adapter, icr);
20163526Sxy150489 		return (DDI_INTR_CLAIMED);
20173526Sxy150489 	} else {
20183526Sxy150489 		/*
20193526Sxy150489 		 * No bit was set in ICR:
20203526Sxy150489 		 * Don't claim this interrupt, return immediately.
20213526Sxy150489 		 */
20223526Sxy150489 		return (DDI_INTR_UNCLAIMED);
20233526Sxy150489 	}
20243526Sxy150489 }
20253526Sxy150489 
20263526Sxy150489 /*
20274919Sxy150489  * e1000g_intr_work - actual processing of ISR
20284919Sxy150489  *
20294919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
20304919Sxy150489  * processing routines.
20313526Sxy150489  */
20323526Sxy150489 static void
20334919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
20343526Sxy150489 {
20355882Syy150190 	struct e1000_hw *hw;
20365882Syy150190 	hw = &Adapter->shared;
20375882Syy150190 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
20385882Syy150190 
20395882Syy150190 	Adapter->rx_pkt_cnt = 0;
20405882Syy150190 	Adapter->tx_pkt_cnt = 0;
20415882Syy150190 
20424919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
20438479SChenlu.Chen@Sun.COM 
20448479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
20458479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
20468479SChenlu.Chen@Sun.COM 		return;
20478479SChenlu.Chen@Sun.COM 	}
20484919Sxy150489 	/*
20498479SChenlu.Chen@Sun.COM 	 * Here we need to check the "e1000g_state" flag within the chip_lock to
20504919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
20514919Sxy150489 	 * being reset.
20524919Sxy150489 	 */
20538479SChenlu.Chen@Sun.COM 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
20544919Sxy150489 		rw_exit(&Adapter->chip_lock);
20554919Sxy150489 		return;
20564919Sxy150489 	}
20574919Sxy150489 
20584919Sxy150489 	if (icr & E1000_ICR_RXT0) {
2059*8833SVenu.Iyer@Sun.COM 		mblk_t			*mp = NULL;
2060*8833SVenu.Iyer@Sun.COM 		mblk_t			*tail = NULL;
20618275SEric Cheng 		e1000g_rx_ring_t	*rx_ring;
20628275SEric Cheng 
20638275SEric Cheng 		rx_ring = Adapter->rx_ring;
20648275SEric Cheng 		mutex_enter(&rx_ring->rx_lock);
20658275SEric Cheng 		/*
2066*8833SVenu.Iyer@Sun.COM 		 * Sometimes with legacy interrupts, it possible that
2067*8833SVenu.Iyer@Sun.COM 		 * there is a single interrupt for Rx/Tx. In which
2068*8833SVenu.Iyer@Sun.COM 		 * case, if poll flag is set, we shouldn't really
2069*8833SVenu.Iyer@Sun.COM 		 * be doing Rx processing.
20708275SEric Cheng 		 */
2071*8833SVenu.Iyer@Sun.COM 		if (!rx_ring->poll_flag)
2072*8833SVenu.Iyer@Sun.COM 			mp = e1000g_receive(rx_ring, &tail,
2073*8833SVenu.Iyer@Sun.COM 			    E1000G_CHAIN_NO_LIMIT);
2074*8833SVenu.Iyer@Sun.COM 		mutex_exit(&rx_ring->rx_lock);
20754919Sxy150489 		rw_exit(&Adapter->chip_lock);
2076*8833SVenu.Iyer@Sun.COM 		if (mp != NULL)
20778275SEric Cheng 			mac_rx_ring(Adapter->mh, rx_ring->mrh,
2078*8833SVenu.Iyer@Sun.COM 			    mp, rx_ring->ring_gen_num);
20794919Sxy150489 	} else
20804919Sxy150489 		rw_exit(&Adapter->chip_lock);
20813526Sxy150489 
20825882Syy150190 	if (icr & E1000_ICR_TXDW) {
20835882Syy150190 		if (!Adapter->tx_intr_enable)
20845882Syy150190 			e1000g_clear_tx_interrupt(Adapter);
20855882Syy150190 
20865882Syy150190 		/* Recycle the tx descriptors */
20875882Syy150190 		rw_enter(&Adapter->chip_lock, RW_READER);
20887426SChenliang.Xu@Sun.COM 		(void) e1000g_recycle(tx_ring);
20895882Syy150190 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
20905882Syy150190 		rw_exit(&Adapter->chip_lock);
20915882Syy150190 
20925882Syy150190 		if (tx_ring->resched_needed &&
20935882Syy150190 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
20945882Syy150190 			tx_ring->resched_needed = B_FALSE;
20955882Syy150190 			mac_tx_update(Adapter->mh);
20965882Syy150190 			E1000G_STAT(tx_ring->stat_reschedule);
20975882Syy150190 		}
20985882Syy150190 	}
20995882Syy150190 
21003526Sxy150489 	/*
21013526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
21023526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
21033526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
21043526Sxy150489 	 * are an indication that cable is not connected.
21053526Sxy150489 	 */
21064919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
21074919Sxy150489 	    (icr & E1000_ICR_LSC) ||
21084919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
21094061Sxy150489 		boolean_t link_changed;
21104061Sxy150489 		timeout_id_t tid = 0;
21113526Sxy150489 
21124919Sxy150489 		stop_watchdog_timer(Adapter);
21134919Sxy150489 
21145082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
21155082Syy150190 
21165082Syy150190 		/*
21175082Syy150190 		 * Because we got a link-status-change interrupt, force
21185082Syy150190 		 * e1000_check_for_link() to look at phy
21195082Syy150190 		 */
21205082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
21215082Syy150190 
21224061Sxy150489 		/* e1000g_link_check takes care of link status change */
21234061Sxy150489 		link_changed = e1000g_link_check(Adapter);
21245082Syy150190 
21255082Syy150190 		/* Get new phy state */
21265082Syy150190 		e1000g_get_phy_state(Adapter);
21275082Syy150190 
21284061Sxy150489 		/*
21294061Sxy150489 		 * If the link timer has not timed out, we'll not notify
21304919Sxy150489 		 * the upper layer with any link state until the link is up.
21314061Sxy150489 		 */
21324061Sxy150489 		if (link_changed && !Adapter->link_complete) {
21334061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
21345082Syy150190 				mutex_enter(&Adapter->link_lock);
21354061Sxy150489 				Adapter->link_complete = B_TRUE;
21364061Sxy150489 				tid = Adapter->link_tid;
21374061Sxy150489 				Adapter->link_tid = 0;
21385082Syy150190 				mutex_exit(&Adapter->link_lock);
21394061Sxy150489 			} else {
21404061Sxy150489 				link_changed = B_FALSE;
21414061Sxy150489 			}
21424061Sxy150489 		}
21435082Syy150190 		rw_exit(&Adapter->chip_lock);
21443526Sxy150489 
21454061Sxy150489 		if (link_changed) {
21464061Sxy150489 			if (tid != 0)
21474061Sxy150489 				(void) untimeout(tid);
21484061Sxy150489 
21494139Sxy150489 			/*
21504139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
21517133Scc210113 			 * down event. Stop receiver here and reset in watchdog.
21524139Sxy150489 			 */
21534139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
21547133Scc210113 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
21557133Scc210113 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
21567133Scc210113 				E1000_WRITE_REG(hw, E1000_RCTL,
21577133Scc210113 				    rctl & ~E1000_RCTL_EN);
21587133Scc210113 				e1000g_log(Adapter, CE_WARN,
21597133Scc210113 				    "ESB2 receiver disabled");
21607133Scc210113 				Adapter->esb2_workaround = B_TRUE;
21617133Scc210113 			}
21624139Sxy150489 
21634061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
21643526Sxy150489 		}
21653526Sxy150489 
21664919Sxy150489 		start_watchdog_timer(Adapter);
21673526Sxy150489 	}
21685882Syy150190 }
21695882Syy150190 
21703526Sxy150489 static void
21713526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
21723526Sxy150489 {
21733526Sxy150489 	struct e1000_hw *hw;
21743526Sxy150489 	int slot;
21753526Sxy150489 
21764919Sxy150489 	hw = &Adapter->shared;
21773526Sxy150489 
21788275SEric Cheng 	if (Adapter->init_count == 0) {
21793526Sxy150489 		/* Initialize the multiple unicast addresses */
21803526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
21813526Sxy150489 
21828275SEric Cheng 		/* Workaround for an erratum of 82571 chipst */
21834919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21844919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21853526Sxy150489 			Adapter->unicst_total--;
21863526Sxy150489 
21878275SEric Cheng 		Adapter->unicst_avail = Adapter->unicst_total;
21888275SEric Cheng 
21898275SEric Cheng 		for (slot = 0; slot < Adapter->unicst_total; slot++) {
21908275SEric Cheng 			/* Clear both the flag and MAC address */
21918275SEric Cheng 			Adapter->unicst_addr[slot].reg.high = 0;
21928275SEric Cheng 			Adapter->unicst_addr[slot].reg.low = 0;
21938275SEric Cheng 		}
21943526Sxy150489 	} else {
21958275SEric Cheng 		/* Workaround for an erratum of 82571 chipst */
21964919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21974919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21984919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
21993526Sxy150489 
22003526Sxy150489 		/* Re-configure the RAR registers */
22018275SEric Cheng 		for (slot = 0; slot < Adapter->unicst_total; slot++)
22028275SEric Cheng 			if (Adapter->unicst_addr[slot].mac.set == 1)
22038275SEric Cheng 				e1000_rar_set(hw,
22048275SEric Cheng 				    Adapter->unicst_addr[slot].mac.addr, slot);
22053526Sxy150489 	}
22065273Sgl147354 
22075273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
22085273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
22093526Sxy150489 }
22103526Sxy150489 
22113526Sxy150489 static int
22123526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
22138275SEric Cheng     int slot)
22143526Sxy150489 {
22153526Sxy150489 	struct e1000_hw *hw;
22163526Sxy150489 
22174919Sxy150489 	hw = &Adapter->shared;
22183526Sxy150489 
22193526Sxy150489 	/*
22203526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
22213526Sxy150489 	 * that requires the receiver to be in reset when any of the
22223526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
22233526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
22243526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
22253526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
22263526Sxy150489 	 * and work around any necessary HW errata.
22273526Sxy150489 	 */
22284919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
22294919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
22303526Sxy150489 		e1000_pci_clear_mwi(hw);
22314919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
22324919Sxy150489 		msec_delay(5);
22333526Sxy150489 	}
22348275SEric Cheng 	if (mac_addr == NULL) {
22358275SEric Cheng 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, slot << 1, 0);
22368275SEric Cheng 		E1000_WRITE_FLUSH(hw);
22378275SEric Cheng 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, (slot << 1) + 1, 0);
22388275SEric Cheng 		E1000_WRITE_FLUSH(hw);
22398275SEric Cheng 		/* Clear both the flag and MAC address */
22408275SEric Cheng 		Adapter->unicst_addr[slot].reg.high = 0;
22418275SEric Cheng 		Adapter->unicst_addr[slot].reg.low = 0;
22428275SEric Cheng 	} else {
22438275SEric Cheng 		bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr,
22448275SEric Cheng 		    ETHERADDRL);
22458275SEric Cheng 		e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
22468275SEric Cheng 		Adapter->unicst_addr[slot].mac.set = 1;
22478275SEric Cheng 	}
22488275SEric Cheng 
22498275SEric Cheng 	/* Workaround for an erratum of 82571 chipst */
22503526Sxy150489 	if (slot == 0) {
22514919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
22524919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
22538275SEric Cheng 			if (mac_addr == NULL) {
22548275SEric Cheng 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
22558275SEric Cheng 				    slot << 1, 0);
22568275SEric Cheng 				E1000_WRITE_FLUSH(hw);
22578275SEric Cheng 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
22588275SEric Cheng 				    (slot << 1) + 1, 0);
22598275SEric Cheng 				E1000_WRITE_FLUSH(hw);
22608275SEric Cheng 			} else {
22618275SEric Cheng 				e1000_rar_set(hw, (uint8_t *)mac_addr,
22628275SEric Cheng 				    LAST_RAR_ENTRY);
22638275SEric Cheng 			}
22643526Sxy150489 	}
22653526Sxy150489 
22663526Sxy150489 	/*
22673526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
22683526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
22693526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
22703526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
22713526Sxy150489 	 */
22724919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
22734919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
22744919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
22754919Sxy150489 		msec_delay(1);
22764919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
22773526Sxy150489 			e1000_pci_set_mwi(hw);
22784919Sxy150489 		e1000g_rx_setup(Adapter);
22793526Sxy150489 	}
22803526Sxy150489 
22815273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
22825273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
22835273Sgl147354 		return (EIO);
22845273Sgl147354 	}
22855273Sgl147354 
22863526Sxy150489 	return (0);
22873526Sxy150489 }
22883526Sxy150489 
22893526Sxy150489 static int
22903526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
22913526Sxy150489 {
22924919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
22933526Sxy150489 	int res = 0;
22943526Sxy150489 
22953526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
22963526Sxy150489 		res = EINVAL;
22973526Sxy150489 		goto done;
22983526Sxy150489 	}
22993526Sxy150489 
23003526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
23013526Sxy150489 		res = ENOENT;
23023526Sxy150489 		goto done;
23033526Sxy150489 	}
23043526Sxy150489 
23053526Sxy150489 	bcopy(multiaddr,
23063526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
23073526Sxy150489 	Adapter->mcast_count++;
23083526Sxy150489 
23093526Sxy150489 	/*
23103526Sxy150489 	 * Update the MC table in the hardware
23113526Sxy150489 	 */
23124919Sxy150489 	e1000g_clear_interrupt(Adapter);
23134919Sxy150489 
23144919Sxy150489 	e1000g_setup_multicast(Adapter);
23154919Sxy150489 
23164919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23174919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23184919Sxy150489 		e1000g_rx_setup(Adapter);
23194919Sxy150489 
23204919Sxy150489 	e1000g_mask_interrupt(Adapter);
23213526Sxy150489 
23225273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23235273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23245273Sgl147354 		res = EIO;
23255273Sgl147354 	}
23265273Sgl147354 
23278479SChenlu.Chen@Sun.COM done:
23283526Sxy150489 	return (res);
23293526Sxy150489 }
23303526Sxy150489 
23313526Sxy150489 static int
23323526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
23333526Sxy150489 {
23344919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23353526Sxy150489 	unsigned i;
23363526Sxy150489 
23373526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
23383526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
23393526Sxy150489 		    ETHERADDRL) == 0) {
23403526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
23413526Sxy150489 				Adapter->mcast_table[i - 1] =
23423526Sxy150489 				    Adapter->mcast_table[i];
23433526Sxy150489 			}
23443526Sxy150489 			Adapter->mcast_count--;
23453526Sxy150489 			break;
23463526Sxy150489 		}
23473526Sxy150489 	}
23483526Sxy150489 
23493526Sxy150489 	/*
23503526Sxy150489 	 * Update the MC table in the hardware
23513526Sxy150489 	 */
23524919Sxy150489 	e1000g_clear_interrupt(Adapter);
23534919Sxy150489 
23544919Sxy150489 	e1000g_setup_multicast(Adapter);
23554919Sxy150489 
23564919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23574919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23584919Sxy150489 		e1000g_rx_setup(Adapter);
23594919Sxy150489 
23604919Sxy150489 	e1000g_mask_interrupt(Adapter);
23613526Sxy150489 
23625273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23635273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23645273Sgl147354 		return (EIO);
23655273Sgl147354 	}
23665273Sgl147354 
23673526Sxy150489 	return (0);
23683526Sxy150489 }
23693526Sxy150489 
23704919Sxy150489 /*
23714919Sxy150489  * e1000g_setup_multicast - setup multicast data structures
23724919Sxy150489  *
23734919Sxy150489  * This routine initializes all of the multicast related structures.
23744919Sxy150489  */
23754919Sxy150489 void
23764919Sxy150489 e1000g_setup_multicast(struct e1000g *Adapter)
23774919Sxy150489 {
23784919Sxy150489 	uint8_t *mc_addr_list;
23794919Sxy150489 	uint32_t mc_addr_count;
23804919Sxy150489 	uint32_t rctl;
23814919Sxy150489 	struct e1000_hw *hw;
23824919Sxy150489 
23834919Sxy150489 	hw = &Adapter->shared;
23844919Sxy150489 
23854919Sxy150489 	/*
23864919Sxy150489 	 * The e1000g has the ability to do perfect filtering of 16
23874919Sxy150489 	 * addresses. The driver uses one of the e1000g's 16 receive
23884919Sxy150489 	 * address registers for its node/network/mac/individual address.
23894919Sxy150489 	 * So, we have room for up to 15 multicast addresses in the CAM,
23904919Sxy150489 	 * additional MC addresses are handled by the MTA (Multicast Table
23914919Sxy150489 	 * Array)
23924919Sxy150489 	 */
23934919Sxy150489 
23944919Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
23954919Sxy150489 
23964919Sxy150489 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
23974919Sxy150489 
23984919Sxy150489 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
23994919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
24004919Sxy150489 		    "Adapter requested more than %d MC Addresses.\n",
24014919Sxy150489 		    MAX_NUM_MULTICAST_ADDRESSES);
24024919Sxy150489 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
24034919Sxy150489 	} else {
24044919Sxy150489 		/*
24054919Sxy150489 		 * Set the number of MC addresses that we are being
24064919Sxy150489 		 * requested to use
24074919Sxy150489 		 */
24084919Sxy150489 		mc_addr_count = Adapter->mcast_count;
24094919Sxy150489 	}
24104919Sxy150489 	/*
24114919Sxy150489 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
24124919Sxy150489 	 * hang  while writing to the receive address registers if the receiver
24134919Sxy150489 	 * is not in reset before writing to the registers. Updating the RAR
24144919Sxy150489 	 * is done during the setting up of the multicast table, hence the
24154919Sxy150489 	 * receiver has to be put in reset before updating the multicast table
24164919Sxy150489 	 * and then taken out of reset at the end
24174919Sxy150489 	 */
24184919Sxy150489 	/*
24194919Sxy150489 	 * if WMI was enabled then dis able it before issueing the global
24204919Sxy150489 	 * reset to the hardware.
24214919Sxy150489 	 */
24224919Sxy150489 	/*
24234919Sxy150489 	 * Only required for WISEMAN_2_0
24244919Sxy150489 	 */
24254919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24264919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24274919Sxy150489 		e1000_pci_clear_mwi(hw);
24284919Sxy150489 		/*
24294919Sxy150489 		 * The e1000g must be in reset before changing any RA
24304919Sxy150489 		 * registers. Reset receive unit.  The chip will remain in
24314919Sxy150489 		 * the reset state until software explicitly restarts it.
24324919Sxy150489 		 */
24334919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
24344919Sxy150489 		/* Allow receiver time to go in to reset */
24354919Sxy150489 		msec_delay(5);
24364919Sxy150489 	}
24376735Scc210113 
24386735Scc210113 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
24394919Sxy150489 	    Adapter->unicst_total, hw->mac.rar_entry_count);
24404919Sxy150489 
24414919Sxy150489 	/*
24424919Sxy150489 	 * Only for Wiseman_2_0
24434919Sxy150489 	 * If MWI was enabled then re-enable it after issueing (as we
24444919Sxy150489 	 * disabled it up there) the receive reset command.
24454919Sxy150489 	 * Wainwright does not have a receive reset command and only thing
24464919Sxy150489 	 * close to it is global reset which will require tx setup also
24474919Sxy150489 	 */
24484919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24494919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24504919Sxy150489 		/*
24514919Sxy150489 		 * if WMI was enabled then reenable it after issueing the
24524919Sxy150489 		 * global or receive reset to the hardware.
24534919Sxy150489 		 */
24544919Sxy150489 
24554919Sxy150489 		/*
24564919Sxy150489 		 * Take receiver out of reset
24574919Sxy150489 		 * clear E1000_RCTL_RST bit (and all others)
24584919Sxy150489 		 */
24594919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
24604919Sxy150489 		msec_delay(5);
24614919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
24624919Sxy150489 			e1000_pci_set_mwi(hw);
24634919Sxy150489 	}
24644919Sxy150489 
24654919Sxy150489 	/*
24664919Sxy150489 	 * Restore original value
24674919Sxy150489 	 */
24684919Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
24694919Sxy150489 }
24704919Sxy150489 
24713526Sxy150489 int
24723526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
24733526Sxy150489 {
24743526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
24758479SChenlu.Chen@Sun.COM 	int result;
24768479SChenlu.Chen@Sun.COM 
24778479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
24788479SChenlu.Chen@Sun.COM 
24798479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
24808479SChenlu.Chen@Sun.COM 		result = ECANCELED;
24818479SChenlu.Chen@Sun.COM 		goto done;
24828479SChenlu.Chen@Sun.COM 	}
24838479SChenlu.Chen@Sun.COM 
24848479SChenlu.Chen@Sun.COM 	result = (add) ? multicst_add(Adapter, addr)
24858479SChenlu.Chen@Sun.COM 	    : multicst_remove(Adapter, addr);
24868479SChenlu.Chen@Sun.COM 
24878479SChenlu.Chen@Sun.COM done:
24888479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
24898479SChenlu.Chen@Sun.COM 	return (result);
24908479SChenlu.Chen@Sun.COM 
24913526Sxy150489 }
24923526Sxy150489 
24933526Sxy150489 int
24943526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
24953526Sxy150489 {
24963526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
24974919Sxy150489 	uint32_t rctl;
24983526Sxy150489 
24993526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25003526Sxy150489 
25018479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
25028479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
25038479SChenlu.Chen@Sun.COM 		return (ECANCELED);
25048479SChenlu.Chen@Sun.COM 	}
25058479SChenlu.Chen@Sun.COM 
25064919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
25073526Sxy150489 
25083526Sxy150489 	if (on)
25094919Sxy150489 		rctl |=
25103526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
25113526Sxy150489 	else
25124919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
25134919Sxy150489 
25144919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
25153526Sxy150489 
25163526Sxy150489 	Adapter->e1000g_promisc = on;
25173526Sxy150489 
25183526Sxy150489 	rw_exit(&Adapter->chip_lock);
25193526Sxy150489 
25205273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
25215273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
25225273Sgl147354 		return (EIO);
25235273Sgl147354 	}
25245273Sgl147354 
25253526Sxy150489 	return (0);
25263526Sxy150489 }
25273526Sxy150489 
25288275SEric Cheng /*
25298275SEric Cheng  * Entry points to enable and disable interrupts at the granularity of
25308275SEric Cheng  * a group.
25318275SEric Cheng  * Turns the poll_mode for the whole adapter on and off to enable or
25328275SEric Cheng  * override the ring level polling control over the hardware interrupts.
25338275SEric Cheng  */
25348275SEric Cheng static int
25358275SEric Cheng e1000g_rx_group_intr_enable(mac_intr_handle_t arg)
25368275SEric Cheng {
25378275SEric Cheng 	struct e1000g		*adapter = (struct e1000g *)arg;
25388275SEric Cheng 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
25398275SEric Cheng 
25408275SEric Cheng 	/*
25418275SEric Cheng 	 * Later interrupts at the granularity of the this ring will
25428275SEric Cheng 	 * invoke mac_rx() with NULL, indicating the need for another
25438275SEric Cheng 	 * software classification.
25448275SEric Cheng 	 * We have a single ring usable per adapter now, so we only need to
25458275SEric Cheng 	 * reset the rx handle for that one.
25468275SEric Cheng 	 * When more RX rings can be used, we should update each one of them.
25478275SEric Cheng 	 */
25488275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
25498275SEric Cheng 	rx_ring->mrh = NULL;
25508275SEric Cheng 	adapter->poll_mode = B_FALSE;
25518275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
25528275SEric Cheng 	return (0);
25538275SEric Cheng }
25548275SEric Cheng 
25558275SEric Cheng static int
25568275SEric Cheng e1000g_rx_group_intr_disable(mac_intr_handle_t arg)
25578275SEric Cheng {
25588275SEric Cheng 	struct e1000g *adapter = (struct e1000g *)arg;
25598275SEric Cheng 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
25608275SEric Cheng 
25618275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
25628275SEric Cheng 
25638275SEric Cheng 	/*
25648275SEric Cheng 	 * Later interrupts at the granularity of the this ring will
25658275SEric Cheng 	 * invoke mac_rx() with the handle for this ring;
25668275SEric Cheng 	 */
25678275SEric Cheng 	adapter->poll_mode = B_TRUE;
25688275SEric Cheng 	rx_ring->mrh = rx_ring->mrh_init;
25698275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
25708275SEric Cheng 	return (0);
25718275SEric Cheng }
25728275SEric Cheng 
25738275SEric Cheng /*
25748275SEric Cheng  * Entry points to enable and disable interrupts at the granularity of
25758275SEric Cheng  * a ring.
25768275SEric Cheng  * adapter poll_mode controls whether we actually proceed with hardware
25778275SEric Cheng  * interrupt toggling.
25788275SEric Cheng  */
25798275SEric Cheng static int
25808275SEric Cheng e1000g_rx_ring_intr_enable(mac_intr_handle_t intrh)
25818275SEric Cheng {
25828275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
25838275SEric Cheng 	struct e1000g 		*adapter = rx_ring->adapter;
25848275SEric Cheng 	struct e1000_hw 	*hw = &adapter->shared;
25858275SEric Cheng 	uint32_t		intr_mask;
25868275SEric Cheng 
25878479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
25888479SChenlu.Chen@Sun.COM 
25898479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
25908479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
25918479SChenlu.Chen@Sun.COM 		return (0);
25928479SChenlu.Chen@Sun.COM 	}
25938479SChenlu.Chen@Sun.COM 
25948275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
25958275SEric Cheng 	rx_ring->poll_flag = 0;
25968275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
25978275SEric Cheng 
2598*8833SVenu.Iyer@Sun.COM 	/* Rx interrupt enabling for MSI and legacy */
2599*8833SVenu.Iyer@Sun.COM 	intr_mask = E1000_READ_REG(hw, E1000_IMS);
2600*8833SVenu.Iyer@Sun.COM 	intr_mask |= E1000_IMS_RXT0;
2601*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMS, intr_mask);
2602*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
2603*8833SVenu.Iyer@Sun.COM 
2604*8833SVenu.Iyer@Sun.COM 	/* Trigger a Rx interrupt to check Rx ring */
2605*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
2606*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
26078479SChenlu.Chen@Sun.COM 
26088479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
26098275SEric Cheng 	return (0);
26108275SEric Cheng }
26118275SEric Cheng 
26128275SEric Cheng static int
26138275SEric Cheng e1000g_rx_ring_intr_disable(mac_intr_handle_t intrh)
26148275SEric Cheng {
26158275SEric Cheng 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
26168275SEric Cheng 	struct e1000g 		*adapter = rx_ring->adapter;
26178275SEric Cheng 	struct e1000_hw 	*hw = &adapter->shared;
26188275SEric Cheng 
26198479SChenlu.Chen@Sun.COM 	rw_enter(&adapter->chip_lock, RW_READER);
26208479SChenlu.Chen@Sun.COM 
26218479SChenlu.Chen@Sun.COM 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
26228479SChenlu.Chen@Sun.COM 		rw_exit(&adapter->chip_lock);
26238479SChenlu.Chen@Sun.COM 		return (0);
26248479SChenlu.Chen@Sun.COM 	}
26258275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
26268275SEric Cheng 	rx_ring->poll_flag = 1;
26278275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
26288275SEric Cheng 
2629*8833SVenu.Iyer@Sun.COM 	/* Rx interrupt disabling for MSI and legacy */
2630*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMC, E1000_IMS_RXT0);
2631*8833SVenu.Iyer@Sun.COM 	E1000_WRITE_FLUSH(hw);
26328479SChenlu.Chen@Sun.COM 
26338479SChenlu.Chen@Sun.COM 	rw_exit(&adapter->chip_lock);
26348275SEric Cheng 	return (0);
26358275SEric Cheng }
26368275SEric Cheng 
26378275SEric Cheng /*
26388275SEric Cheng  * e1000g_unicst_find - Find the slot for the specified unicast address
26398275SEric Cheng  */
26408275SEric Cheng static int
26418275SEric Cheng e1000g_unicst_find(struct e1000g *Adapter, const uint8_t *mac_addr)
26428275SEric Cheng {
26438275SEric Cheng 	int slot;
26448275SEric Cheng 
26458275SEric Cheng 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
26468479SChenlu.Chen@Sun.COM 		if ((Adapter->unicst_addr[slot].mac.set == 1) &&
26478479SChenlu.Chen@Sun.COM 		    (bcmp(Adapter->unicst_addr[slot].mac.addr,
26488479SChenlu.Chen@Sun.COM 		    mac_addr, ETHERADDRL) == 0))
26498275SEric Cheng 				return (slot);
26508275SEric Cheng 	}
26518275SEric Cheng 
26528275SEric Cheng 	return (-1);
26538275SEric Cheng }
26548275SEric Cheng 
26558275SEric Cheng /*
26568275SEric Cheng  * Entry points to add and remove a MAC address to a ring group.
26578275SEric Cheng  * The caller takes care of adding and removing the MAC addresses
26588275SEric Cheng  * to the filter via these two routines.
26598275SEric Cheng  */
26608275SEric Cheng 
26618275SEric Cheng static int
26628275SEric Cheng e1000g_addmac(void *arg, const uint8_t *mac_addr)
26638275SEric Cheng {
26648275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
26658400SNicolas.Droux@Sun.COM 	int slot, err;
26668275SEric Cheng 
26678479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
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 (ECANCELED);
26728479SChenlu.Chen@Sun.COM 	}
26738275SEric Cheng 
26748275SEric Cheng 	if (e1000g_unicst_find(Adapter, mac_addr) != -1) {
26758275SEric Cheng 		/* The same address is already in slot */
26768479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
26778275SEric Cheng 		return (0);
26788275SEric Cheng 	}
26798275SEric Cheng 
26808275SEric Cheng 	if (Adapter->unicst_avail == 0) {
26818275SEric Cheng 		/* no slots available */
26828479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
26838275SEric Cheng 		return (ENOSPC);
26848275SEric Cheng 	}
26858275SEric Cheng 
26868275SEric Cheng 	/* Search for a free slot */
26878275SEric Cheng 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
26888275SEric Cheng 		if (Adapter->unicst_addr[slot].mac.set == 0)
26898275SEric Cheng 			break;
26908275SEric Cheng 	}
26918275SEric Cheng 	ASSERT(slot < Adapter->unicst_total);
26928275SEric Cheng 
26938400SNicolas.Droux@Sun.COM 	err = e1000g_unicst_set(Adapter, mac_addr, slot);
26948400SNicolas.Droux@Sun.COM 	if (err == 0)
26958400SNicolas.Droux@Sun.COM 		Adapter->unicst_avail--;
26968275SEric Cheng 
26978479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
26988275SEric Cheng 
26998400SNicolas.Droux@Sun.COM 	return (err);
27008275SEric Cheng }
27018275SEric Cheng 
27028275SEric Cheng static int
27038275SEric Cheng e1000g_remmac(void *arg, const uint8_t *mac_addr)
27048275SEric Cheng {
27058275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
27068400SNicolas.Droux@Sun.COM 	int slot, err;
27078275SEric Cheng 
27088479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_WRITER);
27098479SChenlu.Chen@Sun.COM 
27108479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
27118479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27128479SChenlu.Chen@Sun.COM 		return (ECANCELED);
27138479SChenlu.Chen@Sun.COM 	}
27148275SEric Cheng 
27158275SEric Cheng 	slot = e1000g_unicst_find(Adapter, mac_addr);
27168275SEric Cheng 	if (slot == -1) {
27178479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
27188275SEric Cheng 		return (EINVAL);
27198275SEric Cheng 	}
27208275SEric Cheng 
27218275SEric Cheng 	ASSERT(Adapter->unicst_addr[slot].mac.set);
27228275SEric Cheng 
27238275SEric Cheng 	/* Clear this slot */
27248400SNicolas.Droux@Sun.COM 	err = e1000g_unicst_set(Adapter, NULL, slot);
27258400SNicolas.Droux@Sun.COM 	if (err == 0)
27268400SNicolas.Droux@Sun.COM 		Adapter->unicst_avail++;
27278275SEric Cheng 
27288479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
27298275SEric Cheng 
27308400SNicolas.Droux@Sun.COM 	return (err);
27318275SEric Cheng }
27328275SEric Cheng 
27338275SEric Cheng static int
27348275SEric Cheng e1000g_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
27358275SEric Cheng {
27368275SEric Cheng 	e1000g_rx_ring_t *rx_ring = (e1000g_rx_ring_t *)rh;
27378275SEric Cheng 
27388275SEric Cheng 	mutex_enter(&rx_ring->rx_lock);
27398275SEric Cheng 	rx_ring->ring_gen_num = mr_gen_num;
27408275SEric Cheng 	mutex_exit(&rx_ring->rx_lock);
27418275SEric Cheng 	return (0);
27428275SEric Cheng }
27438275SEric Cheng 
27448275SEric Cheng /*
27458275SEric Cheng  * Callback funtion for MAC layer to register all rings.
27468275SEric Cheng  *
27478275SEric Cheng  * The hardware supports a single group with currently only one ring
27488275SEric Cheng  * available.
27498275SEric Cheng  * Though not offering virtualization ability per se, exposing the
27508275SEric Cheng  * group/ring still enables the polling and interrupt toggling.
27518275SEric Cheng  */
27528275SEric Cheng void
27538275SEric Cheng e1000g_fill_ring(void *arg, mac_ring_type_t rtype, const int grp_index,
27548275SEric Cheng     const int ring_index, mac_ring_info_t *infop, mac_ring_handle_t rh)
27558275SEric Cheng {
27568275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
27578275SEric Cheng 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
27588275SEric Cheng 	mac_intr_t *mintr;
27598275SEric Cheng 
27608275SEric Cheng 	/*
27618275SEric Cheng 	 * We advertised only RX group/rings, so the MAC framework shouldn't
27628275SEric Cheng 	 * ask for any thing else.
27638275SEric Cheng 	 */
27648275SEric Cheng 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0 && ring_index == 0);
27658275SEric Cheng 
27668275SEric Cheng 	rx_ring->mrh = rx_ring->mrh_init = rh;
27678275SEric Cheng 	infop->mri_driver = (mac_ring_driver_t)rx_ring;
27688275SEric Cheng 	infop->mri_start = e1000g_ring_start;
27698275SEric Cheng 	infop->mri_stop = NULL;
27708275SEric Cheng 	infop->mri_poll = e1000g_poll_ring;
27718275SEric Cheng 
27728275SEric Cheng 	/* Ring level interrupts */
27738275SEric Cheng 	mintr = &infop->mri_intr;
27748275SEric Cheng 	mintr->mi_handle = (mac_intr_handle_t)rx_ring;
27758275SEric Cheng 	mintr->mi_enable = e1000g_rx_ring_intr_enable;
27768275SEric Cheng 	mintr->mi_disable = e1000g_rx_ring_intr_disable;
27778275SEric Cheng }
27788275SEric Cheng 
27798275SEric Cheng static void
27808275SEric Cheng e1000g_fill_group(void *arg, mac_ring_type_t rtype, const int grp_index,
27818275SEric Cheng     mac_group_info_t *infop, mac_group_handle_t gh)
27828275SEric Cheng {
27838275SEric Cheng 	struct e1000g *Adapter = (struct e1000g *)arg;
27848275SEric Cheng 	mac_intr_t *mintr;
27858275SEric Cheng 
27868275SEric Cheng 	/*
27878275SEric Cheng 	 * We advertised a single RX ring. Getting a request for anything else
27888275SEric Cheng 	 * signifies a bug in the MAC framework.
27898275SEric Cheng 	 */
27908275SEric Cheng 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0);
27918275SEric Cheng 
27928275SEric Cheng 	Adapter->rx_group = gh;
27938275SEric Cheng 
27948275SEric Cheng 	infop->mgi_driver = (mac_group_driver_t)Adapter;
27958275SEric Cheng 	infop->mgi_start = NULL;
27968275SEric Cheng 	infop->mgi_stop = NULL;
27978275SEric Cheng 	infop->mgi_addmac = e1000g_addmac;
27988275SEric Cheng 	infop->mgi_remmac = e1000g_remmac;
27998275SEric Cheng 	infop->mgi_count = 1;
28008275SEric Cheng 
28018275SEric Cheng 	/* Group level interrupts */
28028275SEric Cheng 	mintr = &infop->mgi_intr;
28038275SEric Cheng 	mintr->mi_handle = (mac_intr_handle_t)Adapter;
28048275SEric Cheng 	mintr->mi_enable = e1000g_rx_group_intr_enable;
28058275SEric Cheng 	mintr->mi_disable = e1000g_rx_group_intr_disable;
28068275SEric Cheng }
28078275SEric Cheng 
28083526Sxy150489 static boolean_t
28093526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
28103526Sxy150489 {
28113526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
28123526Sxy150489 
28133526Sxy150489 	switch (cap) {
28143526Sxy150489 	case MAC_CAPAB_HCKSUM: {
28153526Sxy150489 		uint32_t *txflags = cap_data;
28166986Smx205022 
28176986Smx205022 		if (Adapter->tx_hcksum_enable)
28186986Smx205022 			*txflags = HCKSUM_IPHDRCKSUM |
28196986Smx205022 			    HCKSUM_INET_PARTIAL;
28206986Smx205022 		else
28213526Sxy150489 			return (B_FALSE);
28223526Sxy150489 		break;
28233526Sxy150489 	}
28246986Smx205022 
28256986Smx205022 	case MAC_CAPAB_LSO: {
28266986Smx205022 		mac_capab_lso_t *cap_lso = cap_data;
28276986Smx205022 
28286986Smx205022 		if (Adapter->lso_enable) {
28296986Smx205022 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
28306986Smx205022 			cap_lso->lso_basic_tcp_ipv4.lso_max =
28316986Smx205022 			    E1000_LSO_MAXLEN;
28326986Smx205022 		} else
28336986Smx205022 			return (B_FALSE);
28346986Smx205022 		break;
28356986Smx205022 	}
28368275SEric Cheng 	case MAC_CAPAB_RINGS: {
28378275SEric Cheng 		mac_capab_rings_t *cap_rings = cap_data;
28388275SEric Cheng 
28398275SEric Cheng 		/* No TX rings exposed yet */
28408275SEric Cheng 		if (cap_rings->mr_type != MAC_RING_TYPE_RX)
28418275SEric Cheng 			return (B_FALSE);
28428275SEric Cheng 
28438275SEric Cheng 		cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
28448275SEric Cheng 		cap_rings->mr_rnum = 1;
28458275SEric Cheng 		cap_rings->mr_gnum = 1;
28468275SEric Cheng 		cap_rings->mr_rget = e1000g_fill_ring;
28478275SEric Cheng 		cap_rings->mr_gget = e1000g_fill_group;
28488275SEric Cheng 		break;
28498275SEric Cheng 	}
28503526Sxy150489 	default:
28513526Sxy150489 		return (B_FALSE);
28523526Sxy150489 	}
28533526Sxy150489 	return (B_TRUE);
28543526Sxy150489 }
28553526Sxy150489 
28566394Scc210113 static boolean_t
28576394Scc210113 e1000g_param_locked(mac_prop_id_t pr_num)
28586394Scc210113 {
28596394Scc210113 	/*
28606394Scc210113 	 * All en_* parameters are locked (read-only) while
28616394Scc210113 	 * the device is in any sort of loopback mode ...
28626394Scc210113 	 */
28636394Scc210113 	switch (pr_num) {
28646789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
28656789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
28666789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
28676789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
28686789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
28696789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
28706789Sam223141 		case MAC_PROP_AUTONEG:
28716789Sam223141 		case MAC_PROP_FLOWCTRL:
28726394Scc210113 			return (B_TRUE);
28736394Scc210113 	}
28746394Scc210113 	return (B_FALSE);
28756394Scc210113 }
28766394Scc210113 
28776394Scc210113 /*
28786394Scc210113  * callback function for set/get of properties
28796394Scc210113  */
28806394Scc210113 static int
28816394Scc210113 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
28826394Scc210113     uint_t pr_valsize, const void *pr_val)
28836394Scc210113 {
28846394Scc210113 	struct e1000g *Adapter = arg;
28856394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
28866394Scc210113 	struct e1000_phy_info *phy = &Adapter->shared.phy;
28876735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
28886394Scc210113 	int err = 0;
28896735Scc210113 	link_flowctrl_t flowctrl;
28906512Ssowmini 	uint32_t cur_mtu, new_mtu;
28916394Scc210113 	uint64_t tmp = 0;
28926394Scc210113 
28936394Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
28948479SChenlu.Chen@Sun.COM 
28958479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
28968479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
28978479SChenlu.Chen@Sun.COM 		return (ECANCELED);
28988479SChenlu.Chen@Sun.COM 	}
28998479SChenlu.Chen@Sun.COM 
29006394Scc210113 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
29016394Scc210113 	    e1000g_param_locked(pr_num)) {
29026394Scc210113 		/*
29036394Scc210113 		 * All en_* parameters are locked (read-only)
29046394Scc210113 		 * while the device is in any sort of loopback mode.
29056394Scc210113 		 */
29066394Scc210113 		rw_exit(&Adapter->chip_lock);
29076394Scc210113 		return (EBUSY);
29086394Scc210113 	}
29096394Scc210113 
29106394Scc210113 	switch (pr_num) {
29116789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
29126394Scc210113 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
29136394Scc210113 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
29146394Scc210113 			goto reset;
29156789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
29166394Scc210113 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
29176394Scc210113 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
29186394Scc210113 			goto reset;
29196789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
29206394Scc210113 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
29216394Scc210113 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
29226394Scc210113 			goto reset;
29236789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
29246394Scc210113 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
29256394Scc210113 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
29266394Scc210113 			goto reset;
29276789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
29286394Scc210113 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
29296394Scc210113 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
29306394Scc210113 			goto reset;
29316789Sam223141 		case MAC_PROP_AUTONEG:
29326394Scc210113 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
29336394Scc210113 			goto reset;
29346789Sam223141 		case MAC_PROP_FLOWCTRL:
29356735Scc210113 			fc->send_xon = B_TRUE;
29366735Scc210113 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
29376735Scc210113 
29386735Scc210113 			switch (flowctrl) {
29396394Scc210113 			default:
29406394Scc210113 				err = EINVAL;
29416394Scc210113 				break;
29426394Scc210113 			case LINK_FLOWCTRL_NONE:
29438539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_none;
29446394Scc210113 				break;
29456394Scc210113 			case LINK_FLOWCTRL_RX:
29468539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_rx_pause;
29476394Scc210113 				break;
29486394Scc210113 			case LINK_FLOWCTRL_TX:
29498539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_tx_pause;
29506394Scc210113 				break;
29516394Scc210113 			case LINK_FLOWCTRL_BI:
29528539SChenlu.Chen@Sun.COM 				fc->requested_mode = e1000_fc_full;
29536394Scc210113 				break;
29546394Scc210113 			}
29556394Scc210113 reset:
29566394Scc210113 			if (err == 0) {
29576394Scc210113 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
29586394Scc210113 					err = EINVAL;
29596394Scc210113 			}
29606394Scc210113 			break;
29616789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
29626789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
29636789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
29646789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
29656789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
29666789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
29678118SVasumathi.Sundaram@Sun.COM 		case MAC_PROP_EN_1000HDX_CAP:
29686789Sam223141 		case MAC_PROP_STATUS:
29696789Sam223141 		case MAC_PROP_SPEED:
29706789Sam223141 		case MAC_PROP_DUPLEX:
29716394Scc210113 			err = ENOTSUP; /* read-only prop. Can't set this. */
29726394Scc210113 			break;
29736789Sam223141 		case MAC_PROP_MTU:
29746394Scc210113 			cur_mtu = Adapter->default_mtu;
29756394Scc210113 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
29766394Scc210113 			if (new_mtu == cur_mtu) {
29776394Scc210113 				err = 0;
29786394Scc210113 				break;
29796394Scc210113 			}
29806394Scc210113 
29816394Scc210113 			tmp = new_mtu + sizeof (struct ether_vlan_header) +
29826394Scc210113 			    ETHERFCSL;
29836394Scc210113 			if ((tmp < DEFAULT_FRAME_SIZE) ||
29846394Scc210113 			    (tmp > MAXIMUM_FRAME_SIZE)) {
29856394Scc210113 				err = EINVAL;
29866394Scc210113 				break;
29876394Scc210113 			}
29886394Scc210113 
29897607STed.You@Sun.COM 			/* ich8 does not support jumbo frames */
29906394Scc210113 			if ((mac->type == e1000_ich8lan) &&
29916394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
29926394Scc210113 				err = EINVAL;
29936394Scc210113 				break;
29946394Scc210113 			}
29956394Scc210113 			/* ich9 does not do jumbo frames on one phy type */
29966394Scc210113 			if ((mac->type == e1000_ich9lan) &&
29976394Scc210113 			    (phy->type == e1000_phy_ife) &&
29986394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
29996394Scc210113 				err = EINVAL;
30006394Scc210113 				break;
30016394Scc210113 			}
30028479SChenlu.Chen@Sun.COM 			if (Adapter->e1000g_state & E1000G_STARTED) {
30036394Scc210113 				err = EBUSY;
30046394Scc210113 				break;
30056394Scc210113 			}
30066394Scc210113 
30076394Scc210113 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
30086394Scc210113 			if (err == 0) {
30097426SChenliang.Xu@Sun.COM 				Adapter->max_frame_size = (uint32_t)tmp;
30106394Scc210113 				Adapter->default_mtu = new_mtu;
30116394Scc210113 				e1000g_set_bufsize(Adapter);
30126394Scc210113 			}
30136394Scc210113 			break;
30146789Sam223141 		case MAC_PROP_PRIVATE:
30156394Scc210113 			err = e1000g_set_priv_prop(Adapter, pr_name,
30166394Scc210113 			    pr_valsize, pr_val);
30176394Scc210113 			break;
30186394Scc210113 		default:
30196394Scc210113 			err = ENOTSUP;
30206394Scc210113 			break;
30216394Scc210113 	}
30226394Scc210113 	rw_exit(&Adapter->chip_lock);
30236394Scc210113 	return (err);
30246394Scc210113 }
30256394Scc210113 
30266394Scc210113 static int
30276394Scc210113 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
30288118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
30296394Scc210113 {
30306394Scc210113 	struct e1000g *Adapter = arg;
30316735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
30326512Ssowmini 	int err = 0;
30336735Scc210113 	link_flowctrl_t flowctrl;
30346394Scc210113 	uint64_t tmp = 0;
30356394Scc210113 
30366512Ssowmini 	if (pr_valsize == 0)
30376512Ssowmini 		return (EINVAL);
30386512Ssowmini 
30398118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
30408118SVasumathi.Sundaram@Sun.COM 
30416394Scc210113 	bzero(pr_val, pr_valsize);
30426789Sam223141 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
30436512Ssowmini 		return (e1000g_get_def_val(Adapter, pr_num,
30446512Ssowmini 		    pr_valsize, pr_val));
30456512Ssowmini 	}
30466512Ssowmini 
30476394Scc210113 	switch (pr_num) {
30486789Sam223141 		case MAC_PROP_DUPLEX:
30498118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
30506512Ssowmini 			if (pr_valsize >= sizeof (link_duplex_t)) {
30516512Ssowmini 				bcopy(&Adapter->link_duplex, pr_val,
30526512Ssowmini 				    sizeof (link_duplex_t));
30536512Ssowmini 			} else
30546512Ssowmini 				err = EINVAL;
30556394Scc210113 			break;
30566789Sam223141 		case MAC_PROP_SPEED:
30578118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
30586394Scc210113 			if (pr_valsize >= sizeof (uint64_t)) {
30596394Scc210113 				tmp = Adapter->link_speed * 1000000ull;
30606394Scc210113 				bcopy(&tmp, pr_val, sizeof (tmp));
30616512Ssowmini 			} else
30626512Ssowmini 				err = EINVAL;
30636394Scc210113 			break;
30646789Sam223141 		case MAC_PROP_AUTONEG:
30656512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
30666394Scc210113 			break;
30676789Sam223141 		case MAC_PROP_FLOWCTRL:
30686394Scc210113 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
30698479SChenlu.Chen@Sun.COM 				switch (fc->current_mode) {
30706394Scc210113 					case e1000_fc_none:
30716735Scc210113 						flowctrl = LINK_FLOWCTRL_NONE;
30726394Scc210113 						break;
30736394Scc210113 					case e1000_fc_rx_pause:
30746735Scc210113 						flowctrl = LINK_FLOWCTRL_RX;
30756394Scc210113 						break;
30766394Scc210113 					case e1000_fc_tx_pause:
30776735Scc210113 						flowctrl = LINK_FLOWCTRL_TX;
30786394Scc210113 						break;
30796394Scc210113 					case e1000_fc_full:
30806735Scc210113 						flowctrl = LINK_FLOWCTRL_BI;
30816394Scc210113 						break;
30826394Scc210113 				}
30836735Scc210113 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
30846512Ssowmini 			} else
30856512Ssowmini 				err = EINVAL;
30866394Scc210113 			break;
30876789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
30888118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
30896512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
30906394Scc210113 			break;
30916789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
30926512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
30936394Scc210113 			break;
30946789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
30958118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
30966512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
30976394Scc210113 			break;
30986789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
30998118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31006512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
31016394Scc210113 			break;
31026789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
31038118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31046512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
31056394Scc210113 			break;
31066789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
31076512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
31086394Scc210113 			break;
31096789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
31108118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31116512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
31126394Scc210113 			break;
31136789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
31146512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
31156394Scc210113 			break;
31166789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
31178118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31186512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
31196394Scc210113 			break;
31206789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
31216512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
31226394Scc210113 			break;
31236789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
31248118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31256512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
31266394Scc210113 			break;
31276789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
31286512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
31296512Ssowmini 			break;
31306789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
31316789Sam223141 		case MAC_PROP_EN_100T4_CAP:
31328118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
31336512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
31346394Scc210113 			break;
31356789Sam223141 		case MAC_PROP_PRIVATE:
31366394Scc210113 			err = e1000g_get_priv_prop(Adapter, pr_name,
31378118SVasumathi.Sundaram@Sun.COM 			    pr_flags, pr_valsize, pr_val, perm);
31386394Scc210113 			break;
31396394Scc210113 		default:
31406394Scc210113 			err = ENOTSUP;
31416394Scc210113 			break;
31426394Scc210113 	}
31436394Scc210113 	return (err);
31446394Scc210113 }
31456394Scc210113 
31467426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
31476394Scc210113 static int
31486394Scc210113 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
31496394Scc210113     uint_t pr_valsize, const void *pr_val)
31506394Scc210113 {
31516394Scc210113 	int err = 0;
31526394Scc210113 	long result;
31536394Scc210113 	struct e1000_hw *hw = &Adapter->shared;
31546394Scc210113 
31556394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
31566394Scc210113 		if (pr_val == NULL) {
31576394Scc210113 			err = EINVAL;
31586394Scc210113 			return (err);
31596394Scc210113 		}
31606394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31616394Scc210113 		if (result < MIN_TX_BCOPY_THRESHOLD ||
31626394Scc210113 		    result > MAX_TX_BCOPY_THRESHOLD)
31636394Scc210113 			err = EINVAL;
31646394Scc210113 		else {
31656394Scc210113 			Adapter->tx_bcopy_thresh = (uint32_t)result;
31666394Scc210113 		}
31676394Scc210113 		return (err);
31686394Scc210113 	}
31696394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
31706394Scc210113 		if (pr_val == NULL) {
31716394Scc210113 			err = EINVAL;
31726394Scc210113 			return (err);
31736394Scc210113 		}
31746394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31756394Scc210113 		if (result < 0 || result > 1)
31766394Scc210113 			err = EINVAL;
31776394Scc210113 		else {
31786394Scc210113 			Adapter->tx_intr_enable = (result == 1) ?
31796394Scc210113 			    B_TRUE: B_FALSE;
31806394Scc210113 			if (Adapter->tx_intr_enable)
31816394Scc210113 				e1000g_mask_tx_interrupt(Adapter);
31826394Scc210113 			else
31836394Scc210113 				e1000g_clear_tx_interrupt(Adapter);
31846394Scc210113 			if (e1000g_check_acc_handle(
31856394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
31866394Scc210113 				ddi_fm_service_impact(Adapter->dip,
31876394Scc210113 				    DDI_SERVICE_DEGRADED);
31886394Scc210113 		}
31896394Scc210113 		return (err);
31906394Scc210113 	}
31916394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
31926394Scc210113 		if (pr_val == NULL) {
31936394Scc210113 			err = EINVAL;
31946394Scc210113 			return (err);
31956394Scc210113 		}
31966394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31976394Scc210113 		if (result < MIN_TX_INTR_DELAY ||
31986394Scc210113 		    result > MAX_TX_INTR_DELAY)
31996394Scc210113 			err = EINVAL;
32006394Scc210113 		else {
32016394Scc210113 			Adapter->tx_intr_delay = (uint32_t)result;
32026394Scc210113 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
32036394Scc210113 			if (e1000g_check_acc_handle(
32046394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
32056394Scc210113 				ddi_fm_service_impact(Adapter->dip,
32066394Scc210113 				    DDI_SERVICE_DEGRADED);
32076394Scc210113 		}
32086394Scc210113 		return (err);
32096394Scc210113 	}
32106394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
32116394Scc210113 		if (pr_val == NULL) {
32126394Scc210113 			err = EINVAL;
32136394Scc210113 			return (err);
32146394Scc210113 		}
32156394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32166394Scc210113 		if (result < MIN_TX_INTR_ABS_DELAY ||
32176394Scc210113 		    result > MAX_TX_INTR_ABS_DELAY)
32186394Scc210113 			err = EINVAL;
32196394Scc210113 		else {
32206394Scc210113 			Adapter->tx_intr_abs_delay = (uint32_t)result;
32216394Scc210113 			E1000_WRITE_REG(hw, E1000_TADV,
32226394Scc210113 			    Adapter->tx_intr_abs_delay);
32236394Scc210113 			if (e1000g_check_acc_handle(
32246394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
32256394Scc210113 				ddi_fm_service_impact(Adapter->dip,
32266394Scc210113 				    DDI_SERVICE_DEGRADED);
32276394Scc210113 		}
32286394Scc210113 		return (err);
32296394Scc210113 	}
32306394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
32316394Scc210113 		if (pr_val == NULL) {
32326394Scc210113 			err = EINVAL;
32336394Scc210113 			return (err);
32346394Scc210113 		}
32356394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32366394Scc210113 		if (result < MIN_RX_BCOPY_THRESHOLD ||
32376394Scc210113 		    result > MAX_RX_BCOPY_THRESHOLD)
32386394Scc210113 			err = EINVAL;
32396394Scc210113 		else
32406394Scc210113 			Adapter->rx_bcopy_thresh = (uint32_t)result;
32416394Scc210113 		return (err);
32426394Scc210113 	}
32436394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
32446394Scc210113 		if (pr_val == NULL) {
32456394Scc210113 			err = EINVAL;
32466394Scc210113 			return (err);
32476394Scc210113 		}
32486394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32496394Scc210113 		if (result < MIN_RX_LIMIT_ON_INTR ||
32506394Scc210113 		    result > MAX_RX_LIMIT_ON_INTR)
32516394Scc210113 			err = EINVAL;
32526394Scc210113 		else
32536394Scc210113 			Adapter->rx_limit_onintr = (uint32_t)result;
32546394Scc210113 		return (err);
32556394Scc210113 	}
32566394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
32576394Scc210113 		if (pr_val == NULL) {
32586394Scc210113 			err = EINVAL;
32596394Scc210113 			return (err);
32606394Scc210113 		}
32616394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32626394Scc210113 		if (result < MIN_RX_INTR_DELAY ||
32636394Scc210113 		    result > MAX_RX_INTR_DELAY)
32646394Scc210113 			err = EINVAL;
32656394Scc210113 		else {
32666394Scc210113 			Adapter->rx_intr_delay = (uint32_t)result;
32676394Scc210113 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
32686394Scc210113 			if (e1000g_check_acc_handle(
32696394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
32706394Scc210113 				ddi_fm_service_impact(Adapter->dip,
32716394Scc210113 				    DDI_SERVICE_DEGRADED);
32726394Scc210113 		}
32736394Scc210113 		return (err);
32746394Scc210113 	}
32756394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
32766394Scc210113 		if (pr_val == NULL) {
32776394Scc210113 			err = EINVAL;
32786394Scc210113 			return (err);
32796394Scc210113 		}
32806394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
32816394Scc210113 		if (result < MIN_RX_INTR_ABS_DELAY ||
32826394Scc210113 		    result > MAX_RX_INTR_ABS_DELAY)
32836394Scc210113 			err = EINVAL;
32846394Scc210113 		else {
32856394Scc210113 			Adapter->rx_intr_abs_delay = (uint32_t)result;
32866394Scc210113 			E1000_WRITE_REG(hw, E1000_RADV,
32876394Scc210113 			    Adapter->rx_intr_abs_delay);
32886394Scc210113 			if (e1000g_check_acc_handle(
32896394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
32906394Scc210113 				ddi_fm_service_impact(Adapter->dip,
32916394Scc210113 				    DDI_SERVICE_DEGRADED);
32926394Scc210113 		}
32936394Scc210113 		return (err);
32946394Scc210113 	}
32956394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
32966394Scc210113 		if (pr_val == NULL) {
32976394Scc210113 			err = EINVAL;
32986394Scc210113 			return (err);
32996394Scc210113 		}
33006394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33016394Scc210113 		if (result < MIN_INTR_THROTTLING ||
33026394Scc210113 		    result > MAX_INTR_THROTTLING)
33036394Scc210113 			err = EINVAL;
33046394Scc210113 		else {
33056394Scc210113 			if (hw->mac.type >= e1000_82540) {
33066394Scc210113 				Adapter->intr_throttling_rate =
33076394Scc210113 				    (uint32_t)result;
33086394Scc210113 				E1000_WRITE_REG(hw, E1000_ITR,
33096394Scc210113 				    Adapter->intr_throttling_rate);
33106394Scc210113 				if (e1000g_check_acc_handle(
33116394Scc210113 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
33126394Scc210113 					ddi_fm_service_impact(Adapter->dip,
33136394Scc210113 					    DDI_SERVICE_DEGRADED);
33146394Scc210113 			} else
33156394Scc210113 				err = EINVAL;
33166394Scc210113 		}
33176394Scc210113 		return (err);
33186394Scc210113 	}
33196394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
33206394Scc210113 		if (pr_val == NULL) {
33216394Scc210113 			err = EINVAL;
33226394Scc210113 			return (err);
33236394Scc210113 		}
33246394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
33256394Scc210113 		if (result < 0 || result > 1)
33266394Scc210113 			err = EINVAL;
33276394Scc210113 		else {
33286394Scc210113 			if (hw->mac.type >= e1000_82540) {
33296394Scc210113 				Adapter->intr_adaptive = (result == 1) ?
33306394Scc210113 				    B_TRUE : B_FALSE;
33316394Scc210113 			} else {
33326394Scc210113 				err = EINVAL;
33336394Scc210113 			}
33346394Scc210113 		}
33356394Scc210113 		return (err);
33366394Scc210113 	}
33376394Scc210113 	return (ENOTSUP);
33386394Scc210113 }
33396394Scc210113 
33406394Scc210113 static int
33416394Scc210113 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
33428118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
33436394Scc210113 {
33446394Scc210113 	int err = ENOTSUP;
33456789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
33466512Ssowmini 	int value;
33476512Ssowmini 
33486512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
33498118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
33506512Ssowmini 		if (is_default)
33516512Ssowmini 			goto done;
33526512Ssowmini 		value = Adapter->param_adv_pause;
33536512Ssowmini 		err = 0;
33546512Ssowmini 		goto done;
33556512Ssowmini 	}
33566512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
33578118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
33586512Ssowmini 		if (is_default)
33596512Ssowmini 			goto done;
33606512Ssowmini 		value = Adapter->param_adv_asym_pause;
33616512Ssowmini 		err = 0;
33626512Ssowmini 		goto done;
33636512Ssowmini 	}
33646394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
33656512Ssowmini 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
33666512Ssowmini 		    Adapter->tx_bcopy_thresh);
33676394Scc210113 		err = 0;
33686394Scc210113 		goto done;
33696394Scc210113 	}
33706394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
33716512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
33726512Ssowmini 		    Adapter->tx_intr_enable);
33736394Scc210113 		err = 0;
33746394Scc210113 		goto done;
33756394Scc210113 	}
33766394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
33776512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
33786512Ssowmini 		    Adapter->tx_intr_delay);
33796394Scc210113 		err = 0;
33806394Scc210113 		goto done;
33816394Scc210113 	}
33826394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
33836512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
33846512Ssowmini 		    Adapter->tx_intr_abs_delay);
33856394Scc210113 		err = 0;
33866394Scc210113 		goto done;
33876394Scc210113 	}
33886394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
33896512Ssowmini 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
33906512Ssowmini 		    Adapter->rx_bcopy_thresh);
33916394Scc210113 		err = 0;
33926394Scc210113 		goto done;
33936394Scc210113 	}
33946394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
33956512Ssowmini 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
33966512Ssowmini 		    Adapter->rx_limit_onintr);
33976394Scc210113 		err = 0;
33986394Scc210113 		goto done;
33996394Scc210113 	}
34006394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
34016512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
34026512Ssowmini 		    Adapter->rx_intr_delay);
34036394Scc210113 		err = 0;
34046394Scc210113 		goto done;
34056394Scc210113 	}
34066394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
34076512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
34086512Ssowmini 		    Adapter->rx_intr_abs_delay);
34096394Scc210113 		err = 0;
34106394Scc210113 		goto done;
34116394Scc210113 	}
34126394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
34136512Ssowmini 		value = (is_default ? DEFAULT_INTR_THROTTLING :
34146512Ssowmini 		    Adapter->intr_throttling_rate);
34156394Scc210113 		err = 0;
34166394Scc210113 		goto done;
34176394Scc210113 	}
34186394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
34196512Ssowmini 		value = (is_default ? 1 : Adapter->intr_adaptive);
34206394Scc210113 		err = 0;
34216394Scc210113 		goto done;
34226394Scc210113 	}
34236394Scc210113 done:
34246394Scc210113 	if (err == 0) {
34256512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
34266394Scc210113 	}
34276394Scc210113 	return (err);
34286394Scc210113 }
34296394Scc210113 
34303526Sxy150489 /*
34314919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
34324919Sxy150489  * This routine gets user-configured values out of the configuration
34334919Sxy150489  * file e1000g.conf.
34344919Sxy150489  *
34354919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
34364919Sxy150489  * default.
34374919Sxy150489  * If user does not configure a value, use the default.
34384919Sxy150489  * If user configures below the minimum, use the minumum.
34394919Sxy150489  * If user configures above the maximum, use the maxumum.
34403526Sxy150489  */
34413526Sxy150489 static void
34424919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
34433526Sxy150489 {
34444919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
34454919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
34464919Sxy150489 
34473526Sxy150489 	/*
34483526Sxy150489 	 * get each configurable property from e1000g.conf
34493526Sxy150489 	 */
34503526Sxy150489 
34513526Sxy150489 	/*
34523526Sxy150489 	 * NumTxDescriptors
34533526Sxy150489 	 */
34544919Sxy150489 	Adapter->tx_desc_num =
34554919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
34564919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
34574919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
34583526Sxy150489 
34593526Sxy150489 	/*
34603526Sxy150489 	 * NumRxDescriptors
34613526Sxy150489 	 */
34624919Sxy150489 	Adapter->rx_desc_num =
34634919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
34644919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
34654919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
34663526Sxy150489 
34673526Sxy150489 	/*
34683526Sxy150489 	 * NumRxFreeList
34693526Sxy150489 	 */
34704919Sxy150489 	Adapter->rx_freelist_num =
34714919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
34724919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
34734919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
34743526Sxy150489 
34753526Sxy150489 	/*
34763526Sxy150489 	 * NumTxPacketList
34773526Sxy150489 	 */
34784919Sxy150489 	Adapter->tx_freelist_num =
34794919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
34804919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
34814919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
34823526Sxy150489 
34833526Sxy150489 	/*
34843526Sxy150489 	 * FlowControl
34853526Sxy150489 	 */
34866735Scc210113 	hw->fc.send_xon = B_TRUE;
34878539SChenlu.Chen@Sun.COM 	hw->fc.requested_mode =
34884919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
34894919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
34903526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
34918539SChenlu.Chen@Sun.COM 	if (hw->fc.requested_mode == 4)
34928539SChenlu.Chen@Sun.COM 		hw->fc.requested_mode = e1000_fc_default;
34933526Sxy150489 
34943526Sxy150489 	/*
34954919Sxy150489 	 * Max Num Receive Packets on Interrupt
34963526Sxy150489 	 */
34974919Sxy150489 	Adapter->rx_limit_onintr =
34984919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
34994919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
35004919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
35013526Sxy150489 
35023526Sxy150489 	/*
35033526Sxy150489 	 * PHY master slave setting
35043526Sxy150489 	 */
35054919Sxy150489 	hw->phy.ms_type =
35064919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
35073526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
35083526Sxy150489 	    e1000_ms_hw_default);
35093526Sxy150489 
35103526Sxy150489 	/*
35113526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
35123526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
35133526Sxy150489 	 */
35144919Sxy150489 	tbi_compatibility =
35154919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
35164919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
35174919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
35183526Sxy150489 
35193526Sxy150489 	/*
35203526Sxy150489 	 * MSI Enable
35213526Sxy150489 	 */
35226986Smx205022 	Adapter->msi_enable =
35234919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
35244919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
35253526Sxy150489 
35263526Sxy150489 	/*
35273526Sxy150489 	 * Interrupt Throttling Rate
35283526Sxy150489 	 */
35293526Sxy150489 	Adapter->intr_throttling_rate =
35304919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
35314919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
35324919Sxy150489 	    DEFAULT_INTR_THROTTLING);
35333526Sxy150489 
35343526Sxy150489 	/*
35353526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
35363526Sxy150489 	 * It is enabled by default
35373526Sxy150489 	 */
35383526Sxy150489 	Adapter->intr_adaptive =
35394919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
35403526Sxy150489 	    B_TRUE : B_FALSE;
35415882Syy150190 
35425882Syy150190 	/*
35436011Ssv141092 	 * Hardware checksum enable/disable parameter
35446011Ssv141092 	 */
35456986Smx205022 	Adapter->tx_hcksum_enable =
35466986Smx205022 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
35476011Ssv141092 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
35486986Smx205022 	/*
35496986Smx205022 	 * Checksum on/off selection via global parameters.
35506986Smx205022 	 *
35516986Smx205022 	 * If the chip is flagged as not capable of (correctly)
35526986Smx205022 	 * handling checksumming, we don't enable it on either
35536986Smx205022 	 * Rx or Tx side.  Otherwise, we take this chip's settings
35546986Smx205022 	 * from the patchable global defaults.
35556986Smx205022 	 *
35566986Smx205022 	 * We advertise our capabilities only if TX offload is
35576986Smx205022 	 * enabled.  On receive, the stack will accept checksummed
35586986Smx205022 	 * packets anyway, even if we haven't said we can deliver
35596986Smx205022 	 * them.
35606986Smx205022 	 */
35616986Smx205022 	switch (hw->mac.type) {
35626986Smx205022 		case e1000_82540:
35636986Smx205022 		case e1000_82544:
35646986Smx205022 		case e1000_82545:
35656986Smx205022 		case e1000_82545_rev_3:
35666986Smx205022 		case e1000_82546:
35676986Smx205022 		case e1000_82546_rev_3:
35686986Smx205022 		case e1000_82571:
35696986Smx205022 		case e1000_82572:
35706986Smx205022 		case e1000_82573:
35716986Smx205022 		case e1000_80003es2lan:
35726986Smx205022 			break;
35736986Smx205022 		/*
35746986Smx205022 		 * For the following Intel PRO/1000 chipsets, we have not
35756986Smx205022 		 * tested the hardware checksum offload capability, so we
35766986Smx205022 		 * disable the capability for them.
35776986Smx205022 		 *	e1000_82542,
35786986Smx205022 		 *	e1000_82543,
35796986Smx205022 		 *	e1000_82541,
35806986Smx205022 		 *	e1000_82541_rev_2,
35816986Smx205022 		 *	e1000_82547,
35826986Smx205022 		 *	e1000_82547_rev_2,
35836986Smx205022 		 */
35846986Smx205022 		default:
35856986Smx205022 			Adapter->tx_hcksum_enable = B_FALSE;
35866986Smx205022 	}
35876986Smx205022 
35886986Smx205022 	/*
35896986Smx205022 	 * Large Send Offloading(LSO) Enable/Disable
35906986Smx205022 	 * If the tx hardware checksum is not enabled, LSO should be
35916986Smx205022 	 * disabled.
35926986Smx205022 	 */
35936986Smx205022 	Adapter->lso_enable =
35946986Smx205022 	    e1000g_get_prop(Adapter, "lso_enable",
35956986Smx205022 	    0, 1, DEFAULT_LSO_ENABLE);
35966986Smx205022 
35976986Smx205022 	switch (hw->mac.type) {
35986986Smx205022 		case e1000_82546:
35996986Smx205022 		case e1000_82546_rev_3:
36006986Smx205022 			if (Adapter->lso_enable)
36016986Smx205022 				Adapter->lso_premature_issue = B_TRUE;
36027426SChenliang.Xu@Sun.COM 			/* FALLTHRU */
36036986Smx205022 		case e1000_82571:
36046986Smx205022 		case e1000_82572:
36056986Smx205022 		case e1000_82573:
36068073SMin.Xu@Sun.COM 		case e1000_80003es2lan:
36076986Smx205022 			break;
36086986Smx205022 		default:
36096986Smx205022 			Adapter->lso_enable = B_FALSE;
36106986Smx205022 	}
36116986Smx205022 
36126986Smx205022 	if (!Adapter->tx_hcksum_enable) {
36136986Smx205022 		Adapter->lso_premature_issue = B_FALSE;
36146986Smx205022 		Adapter->lso_enable = B_FALSE;
36156986Smx205022 	}
36168417SChenlu.Chen@Sun.COM 
36178417SChenlu.Chen@Sun.COM 	/*
36188417SChenlu.Chen@Sun.COM 	 * If mem_workaround_82546 is enabled, the rx buffer allocated by
36198417SChenlu.Chen@Sun.COM 	 * e1000_82545, e1000_82546 and e1000_82546_rev_3
36208417SChenlu.Chen@Sun.COM 	 * will not cross 64k boundary.
36218417SChenlu.Chen@Sun.COM 	 */
36228417SChenlu.Chen@Sun.COM 	Adapter->mem_workaround_82546 =
36238417SChenlu.Chen@Sun.COM 	    e1000g_get_prop(Adapter, "mem_workaround_82546",
36248417SChenlu.Chen@Sun.COM 	    0, 1, DEFAULT_MEM_WORKAROUND_82546);
36253526Sxy150489 }
36263526Sxy150489 
36273526Sxy150489 /*
36284919Sxy150489  * e1000g_get_prop - routine to read properties
36294919Sxy150489  *
36304919Sxy150489  * Get a user-configure property value out of the configuration
36314919Sxy150489  * file e1000g.conf.
36324919Sxy150489  *
36334919Sxy150489  * Caller provides name of the property, a default value, a minimum
36344919Sxy150489  * value, and a maximum value.
36354919Sxy150489  *
36364919Sxy150489  * Return configured value of the property, with default, minimum and
36374919Sxy150489  * maximum properly applied.
36383526Sxy150489  */
36393526Sxy150489 static int
36404919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
36413526Sxy150489     char *propname,		/* name of the property */
36423526Sxy150489     int minval,			/* minimum acceptable value */
36433526Sxy150489     int maxval,			/* maximim acceptable value */
36443526Sxy150489     int defval)			/* default value */
36453526Sxy150489 {
36463526Sxy150489 	int propval;		/* value returned for requested property */
36473526Sxy150489 	int *props;		/* point to array of properties returned */
36483526Sxy150489 	uint_t nprops;		/* number of property value returned */
36493526Sxy150489 
36503526Sxy150489 	/*
36513526Sxy150489 	 * get the array of properties from the config file
36523526Sxy150489 	 */
36533526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
36543526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
36553526Sxy150489 		/* got some properties, test if we got enough */
36564919Sxy150489 		if (Adapter->instance < nprops) {
36574919Sxy150489 			propval = props[Adapter->instance];
36583526Sxy150489 		} else {
36593526Sxy150489 			/* not enough properties configured */
36603526Sxy150489 			propval = defval;
36614919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
36623526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
36633526Sxy150489 			    " - set to %d\n",
36643526Sxy150489 			    propname, propval);
36653526Sxy150489 		}
36663526Sxy150489 
36673526Sxy150489 		/* free memory allocated for properties */
36683526Sxy150489 		ddi_prop_free(props);
36693526Sxy150489 
36703526Sxy150489 	} else {
36713526Sxy150489 		propval = defval;
36723526Sxy150489 	}
36733526Sxy150489 
36743526Sxy150489 	/*
36753526Sxy150489 	 * enforce limits
36763526Sxy150489 	 */
36773526Sxy150489 	if (propval > maxval) {
36783526Sxy150489 		propval = maxval;
36794919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
36803526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
36813526Sxy150489 		    propname, propval);
36823526Sxy150489 	}
36833526Sxy150489 
36843526Sxy150489 	if (propval < minval) {
36853526Sxy150489 		propval = minval;
36864919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
36873526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
36883526Sxy150489 		    propname, propval);
36893526Sxy150489 	}
36903526Sxy150489 
36913526Sxy150489 	return (propval);
36923526Sxy150489 }
36933526Sxy150489 
36943526Sxy150489 static boolean_t
36954061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
36963526Sxy150489 {
36974061Sxy150489 	uint16_t speed, duplex, phydata;
36984061Sxy150489 	boolean_t link_changed = B_FALSE;
36993526Sxy150489 	struct e1000_hw *hw;
37003526Sxy150489 	uint32_t reg_tarc;
37013526Sxy150489 
37024919Sxy150489 	hw = &Adapter->shared;
37033526Sxy150489 
37043526Sxy150489 	if (e1000g_link_up(Adapter)) {
37053526Sxy150489 		/*
37063526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
37073526Sxy150489 		 */
37084061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
37097426SChenliang.Xu@Sun.COM 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
37104061Sxy150489 			Adapter->link_speed = speed;
37114061Sxy150489 			Adapter->link_duplex = duplex;
37124061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
37134061Sxy150489 			link_changed = B_TRUE;
37144061Sxy150489 
37154061Sxy150489 			Adapter->tx_link_down_timeout = 0;
37164061Sxy150489 
37174919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
37184919Sxy150489 			    (hw->mac.type == e1000_82572)) {
37196735Scc210113 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
37204061Sxy150489 				if (speed == SPEED_1000)
37214061Sxy150489 					reg_tarc |= (1 << 21);
37224061Sxy150489 				else
37234061Sxy150489 					reg_tarc &= ~(1 << 21);
37246735Scc210113 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
37253526Sxy150489 			}
37263526Sxy150489 		}
37273526Sxy150489 		Adapter->smartspeed = 0;
37283526Sxy150489 	} else {
37294061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
37303526Sxy150489 			Adapter->link_speed = 0;
37313526Sxy150489 			Adapter->link_duplex = 0;
37324061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
37334061Sxy150489 			link_changed = B_TRUE;
37344061Sxy150489 
37353526Sxy150489 			/*
37363526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
37373526Sxy150489 			 * driver loses link disable auto master/slave
37383526Sxy150489 			 * resolution.
37393526Sxy150489 			 */
37404919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
37417426SChenliang.Xu@Sun.COM 				(void) e1000_read_phy_reg(hw,
37423526Sxy150489 				    PHY_1000T_CTRL, &phydata);
37433526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
37447426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
37453526Sxy150489 				    PHY_1000T_CTRL, phydata);
37463526Sxy150489 			}
37473526Sxy150489 		} else {
37483526Sxy150489 			e1000g_smartspeed(Adapter);
37493526Sxy150489 		}
37504061Sxy150489 
37518479SChenlu.Chen@Sun.COM 		if (Adapter->e1000g_state & E1000G_STARTED) {
37524061Sxy150489 			if (Adapter->tx_link_down_timeout <
37534061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
37544061Sxy150489 				Adapter->tx_link_down_timeout++;
37554061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
37564061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
37574919Sxy150489 				e1000g_tx_clean(Adapter);
37584061Sxy150489 				Adapter->tx_link_down_timeout++;
37594061Sxy150489 			}
37604061Sxy150489 		}
37613526Sxy150489 	}
37623526Sxy150489 
37635273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
37645273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
37655273Sgl147354 
37664061Sxy150489 	return (link_changed);
37674061Sxy150489 }
37684061Sxy150489 
37696394Scc210113 /*
37706394Scc210113  * e1000g_reset_link - Using the link properties to setup the link
37716394Scc210113  */
37726394Scc210113 int
37736394Scc210113 e1000g_reset_link(struct e1000g *Adapter)
37746394Scc210113 {
37756394Scc210113 	struct e1000_mac_info *mac;
37766394Scc210113 	struct e1000_phy_info *phy;
37776394Scc210113 	boolean_t invalid;
37786394Scc210113 
37796394Scc210113 	mac = &Adapter->shared.mac;
37806394Scc210113 	phy = &Adapter->shared.phy;
37816394Scc210113 	invalid = B_FALSE;
37826394Scc210113 
37836394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
37846394Scc210113 		mac->autoneg = B_TRUE;
37856394Scc210113 		phy->autoneg_advertised = 0;
37866394Scc210113 
37876394Scc210113 		/*
37886394Scc210113 		 * 1000hdx is not supported for autonegotiation
37896394Scc210113 		 */
37906394Scc210113 		if (Adapter->param_adv_1000fdx == 1)
37916394Scc210113 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
37926394Scc210113 
37936394Scc210113 		if (Adapter->param_adv_100fdx == 1)
37946394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
37956394Scc210113 
37966394Scc210113 		if (Adapter->param_adv_100hdx == 1)
37976394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
37986394Scc210113 
37996394Scc210113 		if (Adapter->param_adv_10fdx == 1)
38006394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
38016394Scc210113 
38026394Scc210113 		if (Adapter->param_adv_10hdx == 1)
38036394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
38046394Scc210113 
38056394Scc210113 		if (phy->autoneg_advertised == 0)
38066394Scc210113 			invalid = B_TRUE;
38076394Scc210113 	} else {
38086394Scc210113 		mac->autoneg = B_FALSE;
38096394Scc210113 
38106394Scc210113 		/*
38116394Scc210113 		 * 1000fdx and 1000hdx are not supported for forced link
38126394Scc210113 		 */
38136394Scc210113 		if (Adapter->param_adv_100fdx == 1)
38146394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
38156394Scc210113 		else if (Adapter->param_adv_100hdx == 1)
38166394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
38176394Scc210113 		else if (Adapter->param_adv_10fdx == 1)
38186394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
38196394Scc210113 		else if (Adapter->param_adv_10hdx == 1)
38206394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
38216394Scc210113 		else
38226394Scc210113 			invalid = B_TRUE;
38236394Scc210113 
38246394Scc210113 	}
38256394Scc210113 
38266394Scc210113 	if (invalid) {
38276394Scc210113 		e1000g_log(Adapter, CE_WARN,
38286394Scc210113 		    "Invalid link sets. Setup link to"
38296394Scc210113 		    "support autonegotiation with all link capabilities.");
38306394Scc210113 		mac->autoneg = B_TRUE;
38316394Scc210113 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
38326394Scc210113 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
38336394Scc210113 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
38346394Scc210113 	}
38356394Scc210113 
38366394Scc210113 	return (e1000_setup_link(&Adapter->shared));
38376394Scc210113 }
38386394Scc210113 
38394061Sxy150489 static void
38408275SEric Cheng e1000g_timer_tx_resched(struct e1000g *Adapter)
38418275SEric Cheng {
38428275SEric Cheng 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
38438275SEric Cheng 
38448479SChenlu.Chen@Sun.COM 	rw_enter(&Adapter->chip_lock, RW_READER);
38458479SChenlu.Chen@Sun.COM 
38468275SEric Cheng 	if (tx_ring->resched_needed &&
38478275SEric Cheng 	    ((ddi_get_lbolt() - tx_ring->resched_timestamp) >
38488275SEric Cheng 	    drv_usectohz(1000000)) &&
38498479SChenlu.Chen@Sun.COM 	    (Adapter->e1000g_state & E1000G_STARTED) &&
38508275SEric Cheng 	    (tx_ring->tbd_avail >= DEFAULT_TX_NO_RESOURCE)) {
38518275SEric Cheng 		tx_ring->resched_needed = B_FALSE;
38528275SEric Cheng 		mac_tx_update(Adapter->mh);
38538275SEric Cheng 		E1000G_STAT(tx_ring->stat_reschedule);
38548275SEric Cheng 		E1000G_STAT(tx_ring->stat_timer_reschedule);
38558275SEric Cheng 	}
38568479SChenlu.Chen@Sun.COM 
38578479SChenlu.Chen@Sun.COM 	rw_exit(&Adapter->chip_lock);
38588275SEric Cheng }
38598275SEric Cheng 
38608275SEric Cheng static void
38614919Sxy150489 e1000g_local_timer(void *ws)
38624061Sxy150489 {
38634061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
38644061Sxy150489 	struct e1000_hw *hw;
38654061Sxy150489 	e1000g_ether_addr_t ether_addr;
38664061Sxy150489 	boolean_t link_changed;
38674061Sxy150489 
38684919Sxy150489 	hw = &Adapter->shared;
38694919Sxy150489 
38708479SChenlu.Chen@Sun.COM 	if (Adapter->e1000g_state & E1000G_ERROR) {
38718479SChenlu.Chen@Sun.COM 		rw_enter(&Adapter->chip_lock, RW_WRITER);
38728479SChenlu.Chen@Sun.COM 		Adapter->e1000g_state &= ~E1000G_ERROR;
38738479SChenlu.Chen@Sun.COM 		rw_exit(&Adapter->chip_lock);
38748479SChenlu.Chen@Sun.COM 
38755273Sgl147354 		Adapter->reset_count++;
38768275SEric Cheng 		if (e1000g_global_reset(Adapter)) {
38775273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
38785273Sgl147354 			    DDI_SERVICE_RESTORED);
38798275SEric Cheng 			e1000g_timer_tx_resched(Adapter);
38808275SEric Cheng 		} else
38815273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
38825273Sgl147354 			    DDI_SERVICE_LOST);
38835273Sgl147354 		return;
38845273Sgl147354 	}
38855273Sgl147354 
38864061Sxy150489 	if (e1000g_stall_check(Adapter)) {
38874919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
38884061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
38895273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
38908479SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
38914061Sxy150489 		Adapter->reset_count++;
38928275SEric Cheng 		if (e1000g_reset_adapter(Adapter)) {
38935273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
38945273Sgl147354 			    DDI_SERVICE_RESTORED);
38958275SEric Cheng 			e1000g_timer_tx_resched(Adapter);
38968479SChenlu.Chen@Sun.COM 		}
38975273Sgl147354 		return;
38984061Sxy150489 	}
38994061Sxy150489 
39004061Sxy150489 	link_changed = B_FALSE;
39015082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
39024061Sxy150489 	if (Adapter->link_complete)
39034061Sxy150489 		link_changed = e1000g_link_check(Adapter);
39045082Syy150190 	rw_exit(&Adapter->chip_lock);
39054061Sxy150489 
39067133Scc210113 	if (link_changed)
39074061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
39087133Scc210113 
39097133Scc210113 	/*
39107133Scc210113 	 * Workaround for esb2. Data stuck in fifo on a link
39117133Scc210113 	 * down event. Reset the adapter to recover it.
39127133Scc210113 	 */
39137133Scc210113 	if (Adapter->esb2_workaround) {
39147133Scc210113 		Adapter->esb2_workaround = B_FALSE;
39157656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
39168479SChenlu.Chen@Sun.COM 		return;
39174139Sxy150489 	}
39184061Sxy150489 
39193526Sxy150489 	/*
39203526Sxy150489 	 * With 82571 controllers, any locally administered address will
39213526Sxy150489 	 * be overwritten when there is a reset on the other port.
39223526Sxy150489 	 * Detect this circumstance and correct it.
39233526Sxy150489 	 */
39244919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
39254919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
39264919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
39274919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
39283526Sxy150489 
39293526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
39303526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
39313526Sxy150489 
39324919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
39334919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
39344919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
39354919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
39364919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
39374919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
39384919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
39393526Sxy150489 		}
39403526Sxy150489 	}
39413526Sxy150489 
39423526Sxy150489 	/*
39434919Sxy150489 	 * Long TTL workaround for 82541/82547
39443526Sxy150489 	 */
39457426SChenliang.Xu@Sun.COM 	(void) e1000_igp_ttl_workaround_82547(hw);
39463526Sxy150489 
39473526Sxy150489 	/*
39483526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
39493526Sxy150489 	 * change the value in steps...
39503526Sxy150489 	 * These properties should only be set for 10/100
39513526Sxy150489 	 */
39526735Scc210113 	if ((hw->phy.media_type == e1000_media_type_copper) &&
39534061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
39544061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
39553526Sxy150489 		e1000_update_adaptive(hw);
39563526Sxy150489 	}
39573526Sxy150489 	/*
39583526Sxy150489 	 * Set Timer Interrupts
39593526Sxy150489 	 */
39604919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
39614919Sxy150489 
39625273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
39635273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
39648275SEric Cheng 	else
39658275SEric Cheng 		e1000g_timer_tx_resched(Adapter);
39665273Sgl147354 
39674919Sxy150489 	restart_watchdog_timer(Adapter);
39683526Sxy150489 }
39693526Sxy150489 
39704061Sxy150489 /*
39714061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
39724061Sxy150489  * is expired, which indicates the completion of the link setup. The link
39734061Sxy150489  * state will not be updated until the link setup is completed. And the
39744061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
39754061Sxy150489  * in this function. It will be updated in the local timer routine or the
39764061Sxy150489  * interrupt service routine after the interface is started (plumbed).
39774061Sxy150489  */
39783526Sxy150489 static void
39794061Sxy150489 e1000g_link_timer(void *arg)
39803526Sxy150489 {
39814061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
39823526Sxy150489 
39834919Sxy150489 	mutex_enter(&Adapter->link_lock);
39844061Sxy150489 	Adapter->link_complete = B_TRUE;
39854061Sxy150489 	Adapter->link_tid = 0;
39864919Sxy150489 	mutex_exit(&Adapter->link_lock);
39873526Sxy150489 }
39883526Sxy150489 
39893526Sxy150489 /*
39904919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
39914919Sxy150489  *
39924919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
39934919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
39943526Sxy150489  */
39953526Sxy150489 static void
39963526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
39973526Sxy150489 {
39983526Sxy150489 	int forced;
39994919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
40004919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
40013526Sxy150489 
40023526Sxy150489 	/*
40033526Sxy150489 	 * get value out of config file
40043526Sxy150489 	 */
40054919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
40063526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
40073526Sxy150489 
40083526Sxy150489 	switch (forced) {
40093526Sxy150489 	case GDIAG_10_HALF:
40103526Sxy150489 		/*
40113526Sxy150489 		 * Disable Auto Negotiation
40123526Sxy150489 		 */
40134919Sxy150489 		mac->autoneg = B_FALSE;
40144919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
40153526Sxy150489 		break;
40163526Sxy150489 	case GDIAG_10_FULL:
40173526Sxy150489 		/*
40183526Sxy150489 		 * Disable Auto Negotiation
40193526Sxy150489 		 */
40204919Sxy150489 		mac->autoneg = B_FALSE;
40214919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
40223526Sxy150489 		break;
40233526Sxy150489 	case GDIAG_100_HALF:
40243526Sxy150489 		/*
40253526Sxy150489 		 * Disable Auto Negotiation
40263526Sxy150489 		 */
40274919Sxy150489 		mac->autoneg = B_FALSE;
40284919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
40293526Sxy150489 		break;
40303526Sxy150489 	case GDIAG_100_FULL:
40313526Sxy150489 		/*
40323526Sxy150489 		 * Disable Auto Negotiation
40333526Sxy150489 		 */
40344919Sxy150489 		mac->autoneg = B_FALSE;
40354919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
40363526Sxy150489 		break;
40373526Sxy150489 	case GDIAG_1000_FULL:
40383526Sxy150489 		/*
40393526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
40403526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
40413526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
40423526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
40433526Sxy150489 		 * we are allowed to link without any negotiation.
40443526Sxy150489 		 */
40454919Sxy150489 		mac->autoneg = B_TRUE;
40464919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
40473526Sxy150489 		break;
40483526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
40494919Sxy150489 		mac->autoneg = B_TRUE;
40504919Sxy150489 		phy->autoneg_advertised =
40514919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
40524349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
40534349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
40543526Sxy150489 		break;
40553526Sxy150489 	}	/* switch */
40563526Sxy150489 }
40573526Sxy150489 
40583526Sxy150489 /*
40594919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
40604919Sxy150489  *
40614919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
40623526Sxy150489  */
40633526Sxy150489 static void
40643526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
40653526Sxy150489 {
40663526Sxy150489 	int max_frame;
40674919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
40684919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
40693526Sxy150489 
40703526Sxy150489 	/*
40713526Sxy150489 	 * get value out of config file
40723526Sxy150489 	 */
40734919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
40743526Sxy150489 
40753526Sxy150489 	switch (max_frame) {
40763526Sxy150489 	case 0:
40776394Scc210113 		Adapter->default_mtu = ETHERMTU;
40783526Sxy150489 		break;
40796394Scc210113 	/*
40806394Scc210113 	 * To avoid excessive memory allocation for rx buffers,
40816394Scc210113 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
40826394Scc210113 	 */
40833526Sxy150489 	case 1:
40846394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
40856394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
40866394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
40873526Sxy150489 		break;
40883526Sxy150489 	case 2:
40896394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
40906394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
40916394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
40923526Sxy150489 		break;
40933526Sxy150489 	case 3:
40946394Scc210113 		if (mac->type >= e1000_82571)
40956394Scc210113 			Adapter->default_mtu = MAXIMUM_MTU;
40963526Sxy150489 		else
40976394Scc210113 			Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
40986394Scc210113 			    sizeof (struct ether_vlan_header) - ETHERFCSL -
40996394Scc210113 			    E1000G_IPALIGNPRESERVEROOM;
41003526Sxy150489 		break;
41013526Sxy150489 	default:
41026394Scc210113 		Adapter->default_mtu = ETHERMTU;
41033526Sxy150489 		break;
41043526Sxy150489 	}	/* switch */
41053526Sxy150489 
41066735Scc210113 	Adapter->max_frame_size = Adapter->default_mtu +
41076394Scc210113 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
41086394Scc210113 
41093526Sxy150489 	/* ich8 does not do jumbo frames */
41104919Sxy150489 	if (mac->type == e1000_ich8lan) {
41118178SChenlu.Chen@Sun.COM 		Adapter->default_mtu = ETHERMTU;
41127133Scc210113 		Adapter->max_frame_size = ETHERMTU +
41137133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
41144919Sxy150489 	}
41154919Sxy150489 
41164919Sxy150489 	/* ich9 does not do jumbo frames on one phy type */
41174919Sxy150489 	if ((mac->type == e1000_ich9lan) &&
41184919Sxy150489 	    (phy->type == e1000_phy_ife)) {
41198178SChenlu.Chen@Sun.COM 		Adapter->default_mtu = ETHERMTU;
41207133Scc210113 		Adapter->max_frame_size = ETHERMTU +
41217133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
41223526Sxy150489 	}
41233526Sxy150489 }
41243526Sxy150489 
41253526Sxy150489 static void
41264919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
41273526Sxy150489 {
41284919Sxy150489 	Adapter->watchdog_tid =
41294919Sxy150489 	    timeout(e1000g_local_timer,
41303526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
41313526Sxy150489 }
41324919Sxy150489 #pragma inline(arm_watchdog_timer)
41334919Sxy150489 
41344919Sxy150489 static void
41354919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
41364919Sxy150489 {
41374919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
41384919Sxy150489 
41394919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
41404919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
41414919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
41424919Sxy150489 		arm_watchdog_timer(Adapter);
41434919Sxy150489 	}
41444919Sxy150489 
41454919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
41464919Sxy150489 }
41473526Sxy150489 
41483526Sxy150489 static void
41494919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
41503526Sxy150489 {
41513526Sxy150489 	timeout_id_t tid;
41523526Sxy150489 
41534919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
41544919Sxy150489 
41554919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
41564919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
41574919Sxy150489 	tid = Adapter->watchdog_tid;
41584919Sxy150489 	Adapter->watchdog_tid = 0;
41594919Sxy150489 
41604919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
41613526Sxy150489 
41623526Sxy150489 	if (tid != 0)
41633526Sxy150489 		(void) untimeout(tid);
41643526Sxy150489 }
41653526Sxy150489 
41663526Sxy150489 static void
41674919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
41683526Sxy150489 {
41694919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
41704919Sxy150489 
41714919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
41724919Sxy150489 		if (!Adapter->watchdog_timer_started) {
41734919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
41744919Sxy150489 			arm_watchdog_timer(Adapter);
41753526Sxy150489 		}
41763526Sxy150489 	}
41773526Sxy150489 
41784919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
41794919Sxy150489 }
41804919Sxy150489 
41814919Sxy150489 static void
41824919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
41834919Sxy150489 {
41844919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
41854919Sxy150489 
41864919Sxy150489 	if (Adapter->watchdog_timer_started)
41874919Sxy150489 		arm_watchdog_timer(Adapter);
41884919Sxy150489 
41894919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
41903526Sxy150489 }
41913526Sxy150489 
41923526Sxy150489 static void
41934919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
41943526Sxy150489 {
41954919Sxy150489 	timeout_id_t tid;
41964919Sxy150489 
41974919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
41984919Sxy150489 
41994919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
42004919Sxy150489 	tid = Adapter->watchdog_tid;
42014919Sxy150489 	Adapter->watchdog_tid = 0;
42024919Sxy150489 
42034919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
42044919Sxy150489 
42054919Sxy150489 	if (tid != 0)
42064919Sxy150489 		(void) untimeout(tid);
42073526Sxy150489 }
42083526Sxy150489 
42093526Sxy150489 static void
42104919Sxy150489 stop_link_timer(struct e1000g *Adapter)
42113526Sxy150489 {
42123526Sxy150489 	timeout_id_t tid;
42133526Sxy150489 
42144919Sxy150489 	/* Disable the link timer */
42154919Sxy150489 	mutex_enter(&Adapter->link_lock);
42164919Sxy150489 
42174919Sxy150489 	tid = Adapter->link_tid;
42184919Sxy150489 	Adapter->link_tid = 0;
42194919Sxy150489 
42204919Sxy150489 	mutex_exit(&Adapter->link_lock);
42214919Sxy150489 
42224919Sxy150489 	if (tid != 0)
42234919Sxy150489 		(void) untimeout(tid);
42244919Sxy150489 }
42254919Sxy150489 
42264919Sxy150489 static void
42274919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
42284919Sxy150489 {
42294919Sxy150489 	timeout_id_t tid;
42304919Sxy150489 
42314919Sxy150489 	/* Disable the tx timer for 82547 chipset */
42324919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
42334919Sxy150489 
42344919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
42354919Sxy150489 	tid = tx_ring->timer_id_82547;
42364919Sxy150489 	tx_ring->timer_id_82547 = 0;
42374919Sxy150489 
42384919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
42393526Sxy150489 
42403526Sxy150489 	if (tid != 0)
42413526Sxy150489 		(void) untimeout(tid);
42423526Sxy150489 }
42433526Sxy150489 
42443526Sxy150489 void
42454919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
42463526Sxy150489 {
42474919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
42484919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
42493526Sxy150489 }
42503526Sxy150489 
42513526Sxy150489 void
42524919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
42533526Sxy150489 {
42544919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
42555882Syy150190 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
42565882Syy150190 
42575882Syy150190 	if (Adapter->tx_intr_enable)
42585882Syy150190 		e1000g_mask_tx_interrupt(Adapter);
42593526Sxy150489 }
42603526Sxy150489 
42617656SSherry.Moore@Sun.COM /*
42627656SSherry.Moore@Sun.COM  * This routine is called by e1000g_quiesce(), therefore must not block.
42637656SSherry.Moore@Sun.COM  */
42643526Sxy150489 void
42654919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
42663526Sxy150489 {
42674919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
42683526Sxy150489 }
42693526Sxy150489 
42703526Sxy150489 void
42714919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
42723526Sxy150489 {
42735882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
42743526Sxy150489 }
42753526Sxy150489 
42763526Sxy150489 void
42774919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
42783526Sxy150489 {
42795882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
42803526Sxy150489 }
42813526Sxy150489 
42823526Sxy150489 static void
42834919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
42843526Sxy150489 {
42854919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
42863526Sxy150489 	uint16_t phy_status;
42873526Sxy150489 	uint16_t phy_ctrl;
42883526Sxy150489 
42893526Sxy150489 	/*
42903526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
42913526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
42923526Sxy150489 	 */
42934919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
42944919Sxy150489 	    !hw->mac.autoneg ||
42954919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
42963526Sxy150489 		return;
42973526Sxy150489 
42983526Sxy150489 	/*
42993526Sxy150489 	 * True if this is the first call of this function or after every
43003526Sxy150489 	 * 30 seconds of not having link
43013526Sxy150489 	 */
43024919Sxy150489 	if (Adapter->smartspeed == 0) {
43033526Sxy150489 		/*
43043526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
43053526Sxy150489 		 * assume back-to-back
43063526Sxy150489 		 */
43077426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
43083526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
43093526Sxy150489 			return;
43103526Sxy150489 
43117426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
43123526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
43133526Sxy150489 			return;
43143526Sxy150489 		/*
43153526Sxy150489 		 * We're assuming back-2-back because our status register
43163526Sxy150489 		 * insists! there's a fault in the master/slave
43173526Sxy150489 		 * relationship that was "negotiated"
43183526Sxy150489 		 */
43197426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
43203526Sxy150489 		/*
43213526Sxy150489 		 * Is the phy configured for manual configuration of
43223526Sxy150489 		 * master/slave?
43233526Sxy150489 		 */
43243526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
43253526Sxy150489 			/*
43263526Sxy150489 			 * Yes.  Then disable manual configuration (enable
43273526Sxy150489 			 * auto configuration) of master/slave
43283526Sxy150489 			 */
43293526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
43307426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw,
43313526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
43323526Sxy150489 			/*
43333526Sxy150489 			 * Effectively starting the clock
43343526Sxy150489 			 */
43354919Sxy150489 			Adapter->smartspeed++;
43363526Sxy150489 			/*
43373526Sxy150489 			 * Restart autonegotiation
43383526Sxy150489 			 */
43394919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
43404919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
43413526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
43423526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
43437426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
43444919Sxy150489 				    PHY_CONTROL, phy_ctrl);
43453526Sxy150489 			}
43463526Sxy150489 		}
43473526Sxy150489 		return;
43483526Sxy150489 		/*
43493526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
43503526Sxy150489 		 * you should reset the smartspeed counter once you obtain
43513526Sxy150489 		 * link
43523526Sxy150489 		 */
43534919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
43543526Sxy150489 		/*
43553526Sxy150489 		 * Yes.  Remember, we did at the start determine that
43563526Sxy150489 		 * there's a master/slave configuration fault, so we're
43573526Sxy150489 		 * still assuming there's someone on the other end, but we
43583526Sxy150489 		 * just haven't yet been able to talk to it. We then
43593526Sxy150489 		 * re-enable auto configuration of master/slave to see if
43603526Sxy150489 		 * we're running 2/3 pair cables.
43613526Sxy150489 		 */
43623526Sxy150489 		/*
43633526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
43643526Sxy150489 		 */
43657426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
43663526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
43677426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
43683526Sxy150489 		/*
43693526Sxy150489 		 * Restart autoneg with phy enabled for manual
43703526Sxy150489 		 * configuration of master/slave
43713526Sxy150489 		 */
43724919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
43734919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
43743526Sxy150489 			phy_ctrl |=
43753526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
43767426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
43773526Sxy150489 		}
43783526Sxy150489 		/*
43793526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
43803526Sxy150489 		 * link as a result.
43813526Sxy150489 		 */
43823526Sxy150489 	}
43833526Sxy150489 	/*
43843526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
43853526Sxy150489 	 * seconds)
43863526Sxy150489 	 */
43874919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
43884919Sxy150489 		Adapter->smartspeed = 0;
43893526Sxy150489 }
43903526Sxy150489 
43913526Sxy150489 static boolean_t
43923526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
43933526Sxy150489 {
43943526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
43953526Sxy150489 	const uint8_t addr_test2[6] =
43963526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
43973526Sxy150489 
43983526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
43993526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
44003526Sxy150489 		return (B_FALSE);
44013526Sxy150489 
44023526Sxy150489 	return (B_TRUE);
44033526Sxy150489 }
44043526Sxy150489 
44053526Sxy150489 /*
44064919Sxy150489  * e1000g_stall_check - check for tx stall
44074919Sxy150489  *
44084919Sxy150489  * This function checks if the adapter is stalled (in transmit).
44094919Sxy150489  *
44104919Sxy150489  * It is called each time the watchdog timeout is invoked.
44114919Sxy150489  * If the transmit descriptor reclaim continuously fails,
44124919Sxy150489  * the watchdog value will increment by 1. If the watchdog
44134919Sxy150489  * value exceeds the threshold, the adapter is assumed to
44144919Sxy150489  * have stalled and need to be reset.
44153526Sxy150489  */
44163526Sxy150489 static boolean_t
44173526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
44183526Sxy150489 {
44194919Sxy150489 	e1000g_tx_ring_t *tx_ring;
44204919Sxy150489 
44214919Sxy150489 	tx_ring = Adapter->tx_ring;
44224919Sxy150489 
44234061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
44243526Sxy150489 		return (B_FALSE);
44253526Sxy150489 
44264919Sxy150489 	if (tx_ring->recycle_fail > 0)
44274919Sxy150489 		tx_ring->stall_watchdog++;
44283526Sxy150489 	else
44294919Sxy150489 		tx_ring->stall_watchdog = 0;
44304919Sxy150489 
44314919Sxy150489 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
44323526Sxy150489 		return (B_FALSE);
44333526Sxy150489 
44344919Sxy150489 	tx_ring->stall_watchdog = 0;
44354919Sxy150489 	tx_ring->recycle_fail = 0;
44364919Sxy150489 
44373526Sxy150489 	return (B_TRUE);
44383526Sxy150489 }
44393526Sxy150489 
44404919Sxy150489 #ifdef E1000G_DEBUG
44413526Sxy150489 static enum ioc_reply
44423526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
44433526Sxy150489 {
44443526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
44453526Sxy150489 	e1000g_peekpoke_t *ppd;
44463526Sxy150489 	uint64_t mem_va;
44473526Sxy150489 	uint64_t maxoff;
44483526Sxy150489 	boolean_t peek;
44493526Sxy150489 
44503526Sxy150489 	switch (iocp->ioc_cmd) {
44513526Sxy150489 
44523526Sxy150489 	case E1000G_IOC_REG_PEEK:
44533526Sxy150489 		peek = B_TRUE;
44543526Sxy150489 		break;
44553526Sxy150489 
44563526Sxy150489 	case E1000G_IOC_REG_POKE:
44573526Sxy150489 		peek = B_FALSE;
44583526Sxy150489 		break;
44593526Sxy150489 
44603526Sxy150489 	deault:
44614919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
44624349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
44634349Sxy150489 		    iocp->ioc_cmd);
44643526Sxy150489 		return (IOC_INVAL);
44653526Sxy150489 	}
44663526Sxy150489 
44673526Sxy150489 	/*
44683526Sxy150489 	 * Validate format of ioctl
44693526Sxy150489 	 */
44703526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
44713526Sxy150489 		return (IOC_INVAL);
44723526Sxy150489 	if (mp->b_cont == NULL)
44733526Sxy150489 		return (IOC_INVAL);
44743526Sxy150489 
44757426SChenliang.Xu@Sun.COM 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
44763526Sxy150489 
44773526Sxy150489 	/*
44783526Sxy150489 	 * Validate request parameters
44793526Sxy150489 	 */
44803526Sxy150489 	switch (ppd->pp_acc_space) {
44813526Sxy150489 
44823526Sxy150489 	default:
44834919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
44844349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
44854349Sxy150489 		    ppd->pp_acc_space);
44863526Sxy150489 		return (IOC_INVAL);
44873526Sxy150489 
44883526Sxy150489 	case E1000G_PP_SPACE_REG:
44893526Sxy150489 		/*
44903526Sxy150489 		 * Memory-mapped I/O space
44913526Sxy150489 		 */
44923526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
44933526Sxy150489 		if (ppd->pp_acc_size != 4)
44943526Sxy150489 			return (IOC_INVAL);
44953526Sxy150489 
44963526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
44973526Sxy150489 			return (IOC_INVAL);
44983526Sxy150489 
44993526Sxy150489 		mem_va = 0;
45003526Sxy150489 		maxoff = 0x10000;
45013526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
45023526Sxy150489 		break;
45033526Sxy150489 
45043526Sxy150489 	case E1000G_PP_SPACE_E1000G:
45053526Sxy150489 		/*
45063526Sxy150489 		 * E1000g data structure!
45073526Sxy150489 		 */
45083526Sxy150489 		mem_va = (uintptr_t)e1000gp;
45093526Sxy150489 		maxoff = sizeof (struct e1000g);
45103526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
45113526Sxy150489 		break;
45123526Sxy150489 
45133526Sxy150489 	}
45143526Sxy150489 
45153526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
45163526Sxy150489 		return (IOC_INVAL);
45173526Sxy150489 
45183526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
45193526Sxy150489 		return (IOC_INVAL);
45203526Sxy150489 
45213526Sxy150489 	/*
45223526Sxy150489 	 * All OK - go!
45233526Sxy150489 	 */
45243526Sxy150489 	ppd->pp_acc_offset += mem_va;
45253526Sxy150489 	(*ppfn)(e1000gp, ppd);
45263526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
45273526Sxy150489 }
45283526Sxy150489 
45293526Sxy150489 static void
45303526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
45313526Sxy150489 {
45323526Sxy150489 	ddi_acc_handle_t handle;
45333526Sxy150489 	uint32_t *regaddr;
45343526Sxy150489 
45354919Sxy150489 	handle = e1000gp->osdep.reg_handle;
45367426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
45377426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
45383526Sxy150489 
45393526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
45403526Sxy150489 }
45413526Sxy150489 
45423526Sxy150489 static void
45433526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
45443526Sxy150489 {
45453526Sxy150489 	ddi_acc_handle_t handle;
45463526Sxy150489 	uint32_t *regaddr;
45473526Sxy150489 	uint32_t value;
45483526Sxy150489 
45494919Sxy150489 	handle = e1000gp->osdep.reg_handle;
45507426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
45517426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
45523526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
45533526Sxy150489 
45543526Sxy150489 	ddi_put32(handle, regaddr, value);
45553526Sxy150489 }
45563526Sxy150489 
45573526Sxy150489 static void
45583526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
45593526Sxy150489 {
45603526Sxy150489 	uint64_t value;
45613526Sxy150489 	void *vaddr;
45623526Sxy150489 
45633526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
45643526Sxy150489 
45653526Sxy150489 	switch (ppd->pp_acc_size) {
45663526Sxy150489 	case 1:
45673526Sxy150489 		value = *(uint8_t *)vaddr;
45683526Sxy150489 		break;
45693526Sxy150489 
45703526Sxy150489 	case 2:
45713526Sxy150489 		value = *(uint16_t *)vaddr;
45723526Sxy150489 		break;
45733526Sxy150489 
45743526Sxy150489 	case 4:
45753526Sxy150489 		value = *(uint32_t *)vaddr;
45763526Sxy150489 		break;
45773526Sxy150489 
45783526Sxy150489 	case 8:
45793526Sxy150489 		value = *(uint64_t *)vaddr;
45803526Sxy150489 		break;
45813526Sxy150489 	}
45823526Sxy150489 
45834919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
45844349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
45854349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
45863526Sxy150489 
45873526Sxy150489 	ppd->pp_acc_data = value;
45883526Sxy150489 }
45893526Sxy150489 
45903526Sxy150489 static void
45913526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
45923526Sxy150489 {
45933526Sxy150489 	uint64_t value;
45943526Sxy150489 	void *vaddr;
45953526Sxy150489 
45963526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
45973526Sxy150489 	value = ppd->pp_acc_data;
45983526Sxy150489 
45994919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
46004349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
46014349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
46023526Sxy150489 
46033526Sxy150489 	switch (ppd->pp_acc_size) {
46043526Sxy150489 	case 1:
46053526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
46063526Sxy150489 		break;
46073526Sxy150489 
46083526Sxy150489 	case 2:
46093526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
46103526Sxy150489 		break;
46113526Sxy150489 
46123526Sxy150489 	case 4:
46133526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
46143526Sxy150489 		break;
46153526Sxy150489 
46163526Sxy150489 	case 8:
46173526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
46183526Sxy150489 		break;
46193526Sxy150489 	}
46203526Sxy150489 }
46214919Sxy150489 #endif
46223526Sxy150489 
46233526Sxy150489 /*
46243526Sxy150489  * Loopback Support
46253526Sxy150489  */
46263526Sxy150489 static lb_property_t lb_normal =
46273526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
46283526Sxy150489 static lb_property_t lb_external1000 =
46293526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
46303526Sxy150489 static lb_property_t lb_external100 =
46313526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
46323526Sxy150489 static lb_property_t lb_external10 =
46333526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
46343526Sxy150489 static lb_property_t lb_phy =
46353526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
46363526Sxy150489 
46373526Sxy150489 static enum ioc_reply
46383526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
46393526Sxy150489 {
46403526Sxy150489 	lb_info_sz_t *lbsp;
46413526Sxy150489 	lb_property_t *lbpp;
46423526Sxy150489 	struct e1000_hw *hw;
46433526Sxy150489 	uint32_t *lbmp;
46443526Sxy150489 	uint32_t size;
46453526Sxy150489 	uint32_t value;
46463526Sxy150489 
46474919Sxy150489 	hw = &Adapter->shared;
46483526Sxy150489 
46493526Sxy150489 	if (mp->b_cont == NULL)
46503526Sxy150489 		return (IOC_INVAL);
46513526Sxy150489 
46527133Scc210113 	if (!e1000g_check_loopback_support(hw)) {
46537133Scc210113 		e1000g_log(NULL, CE_WARN,
46547133Scc210113 		    "Loopback is not supported on e1000g%d", Adapter->instance);
46557133Scc210113 		return (IOC_INVAL);
46567133Scc210113 	}
46577133Scc210113 
46583526Sxy150489 	switch (iocp->ioc_cmd) {
46593526Sxy150489 	default:
46603526Sxy150489 		return (IOC_INVAL);
46613526Sxy150489 
46623526Sxy150489 	case LB_GET_INFO_SIZE:
46633526Sxy150489 		size = sizeof (lb_info_sz_t);
46643526Sxy150489 		if (iocp->ioc_count != size)
46653526Sxy150489 			return (IOC_INVAL);
46663526Sxy150489 
46675082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
46685082Syy150190 		e1000g_get_phy_state(Adapter);
46695082Syy150190 
46705082Syy150190 		/*
46715082Syy150190 		 * Workaround for hardware faults. In order to get a stable
46725082Syy150190 		 * state of phy, we will wait for a specific interval and
46735082Syy150190 		 * try again. The time delay is an experiential value based
46745082Syy150190 		 * on our testing.
46755082Syy150190 		 */
46765082Syy150190 		msec_delay(100);
46775082Syy150190 		e1000g_get_phy_state(Adapter);
46785082Syy150190 		rw_exit(&Adapter->chip_lock);
46793526Sxy150489 
46803526Sxy150489 		value = sizeof (lb_normal);
46815082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
46825082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
46836735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
46846735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
46853526Sxy150489 			value += sizeof (lb_phy);
46864919Sxy150489 			switch (hw->mac.type) {
46873526Sxy150489 			case e1000_82571:
46883526Sxy150489 			case e1000_82572:
46897133Scc210113 			case e1000_80003es2lan:
46903526Sxy150489 				value += sizeof (lb_external1000);
46913526Sxy150489 				break;
46923526Sxy150489 			}
46933526Sxy150489 		}
46945082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
46955082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
46963526Sxy150489 			value += sizeof (lb_external100);
46975082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
46983526Sxy150489 			value += sizeof (lb_external10);
46993526Sxy150489 
47007426SChenliang.Xu@Sun.COM 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
47013526Sxy150489 		*lbsp = value;
47023526Sxy150489 		break;
47033526Sxy150489 
47043526Sxy150489 	case LB_GET_INFO:
47053526Sxy150489 		value = sizeof (lb_normal);
47065082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
47075082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
47086735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
47096735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
47103526Sxy150489 			value += sizeof (lb_phy);
47114919Sxy150489 			switch (hw->mac.type) {
47123526Sxy150489 			case e1000_82571:
47133526Sxy150489 			case e1000_82572:
47147133Scc210113 			case e1000_80003es2lan:
47153526Sxy150489 				value += sizeof (lb_external1000);
47163526Sxy150489 				break;
47173526Sxy150489 			}
47183526Sxy150489 		}
47195082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
47205082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
47213526Sxy150489 			value += sizeof (lb_external100);
47225082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
47233526Sxy150489 			value += sizeof (lb_external10);
47243526Sxy150489 
47253526Sxy150489 		size = value;
47263526Sxy150489 		if (iocp->ioc_count != size)
47273526Sxy150489 			return (IOC_INVAL);
47283526Sxy150489 
47293526Sxy150489 		value = 0;
47307426SChenliang.Xu@Sun.COM 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
47313526Sxy150489 		lbpp[value++] = lb_normal;
47325082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
47335082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
47346735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
47356735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
47363526Sxy150489 			lbpp[value++] = lb_phy;
47374919Sxy150489 			switch (hw->mac.type) {
47383526Sxy150489 			case e1000_82571:
47393526Sxy150489 			case e1000_82572:
47407133Scc210113 			case e1000_80003es2lan:
47413526Sxy150489 				lbpp[value++] = lb_external1000;
47423526Sxy150489 				break;
47433526Sxy150489 			}
47443526Sxy150489 		}
47455082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
47465082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
47473526Sxy150489 			lbpp[value++] = lb_external100;
47485082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
47493526Sxy150489 			lbpp[value++] = lb_external10;
47503526Sxy150489 		break;
47513526Sxy150489 
47523526Sxy150489 	case LB_GET_MODE:
47533526Sxy150489 		size = sizeof (uint32_t);
47543526Sxy150489 		if (iocp->ioc_count != size)
47553526Sxy150489 			return (IOC_INVAL);
47563526Sxy150489 
47577426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
47583526Sxy150489 		*lbmp = Adapter->loopback_mode;
47593526Sxy150489 		break;
47603526Sxy150489 
47613526Sxy150489 	case LB_SET_MODE:
47623526Sxy150489 		size = 0;
47633526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
47643526Sxy150489 			return (IOC_INVAL);
47653526Sxy150489 
47667426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
47673526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
47683526Sxy150489 			return (IOC_INVAL);
47693526Sxy150489 		break;
47703526Sxy150489 	}
47713526Sxy150489 
47723526Sxy150489 	iocp->ioc_count = size;
47733526Sxy150489 	iocp->ioc_error = 0;
47743526Sxy150489 
47755273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
47765273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
47775273Sgl147354 		return (IOC_INVAL);
47785273Sgl147354 	}
47795273Sgl147354 
47803526Sxy150489 	return (IOC_REPLY);
47813526Sxy150489 }
47823526Sxy150489 
47833526Sxy150489 static boolean_t
47847133Scc210113 e1000g_check_loopback_support(struct e1000_hw *hw)
47857133Scc210113 {
47867133Scc210113 	switch (hw->mac.type) {
47877133Scc210113 	case e1000_82540:
47887133Scc210113 	case e1000_82545:
47897133Scc210113 	case e1000_82545_rev_3:
47907133Scc210113 	case e1000_82546:
47917133Scc210113 	case e1000_82546_rev_3:
47927133Scc210113 	case e1000_82541:
47937133Scc210113 	case e1000_82541_rev_2:
47947133Scc210113 	case e1000_82547:
47957133Scc210113 	case e1000_82547_rev_2:
47967133Scc210113 	case e1000_82571:
47977133Scc210113 	case e1000_82572:
47987133Scc210113 	case e1000_82573:
47998479SChenlu.Chen@Sun.COM 	case e1000_82574:
48007133Scc210113 	case e1000_80003es2lan:
48018479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
48028479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
48037133Scc210113 		return (B_TRUE);
48047133Scc210113 	}
48057133Scc210113 	return (B_FALSE);
48067133Scc210113 }
48077133Scc210113 
48087133Scc210113 static boolean_t
48093526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
48103526Sxy150489 {
48113526Sxy150489 	struct e1000_hw *hw;
48123526Sxy150489 	int i, times;
48135082Syy150190 	boolean_t link_up;
48143526Sxy150489 
48153526Sxy150489 	if (mode == Adapter->loopback_mode)
48163526Sxy150489 		return (B_TRUE);
48173526Sxy150489 
48184919Sxy150489 	hw = &Adapter->shared;
48193526Sxy150489 	times = 0;
48203526Sxy150489 
48215082Syy150190 	Adapter->loopback_mode = mode;
48225082Syy150190 
48235082Syy150190 	if (mode == E1000G_LB_NONE) {
48243526Sxy150489 		/* Reset the chip */
48256735Scc210113 		hw->phy.autoneg_wait_to_complete = B_TRUE;
48267656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
48276735Scc210113 		hw->phy.autoneg_wait_to_complete = B_FALSE;
48285082Syy150190 		return (B_TRUE);
48295082Syy150190 	}
48305082Syy150190 
48315082Syy150190 again:
48325082Syy150190 
48335082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
48345082Syy150190 
48355082Syy150190 	switch (mode) {
48365082Syy150190 	default:
48375082Syy150190 		rw_exit(&Adapter->chip_lock);
48385082Syy150190 		return (B_FALSE);
48393526Sxy150489 
48403526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
48413526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
48423526Sxy150489 		break;
48433526Sxy150489 
48443526Sxy150489 	case E1000G_LB_EXTERNAL_100:
48453526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
48463526Sxy150489 		break;
48473526Sxy150489 
48483526Sxy150489 	case E1000G_LB_EXTERNAL_10:
48493526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
48503526Sxy150489 		break;
48513526Sxy150489 
48523526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
48533526Sxy150489 		e1000g_set_internal_loopback(Adapter);
48543526Sxy150489 		break;
48553526Sxy150489 	}
48563526Sxy150489 
48573526Sxy150489 	times++;
48583526Sxy150489 
48595858Scc210113 	rw_exit(&Adapter->chip_lock);
48605858Scc210113 
48615082Syy150190 	/* Wait for link up */
48625082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
48635082Syy150190 		msec_delay(100);
48645082Syy150190 
48655858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
48665858Scc210113 
48675082Syy150190 	link_up = e1000g_link_up(Adapter);
48685082Syy150190 
48695082Syy150190 	rw_exit(&Adapter->chip_lock);
48705082Syy150190 
48715082Syy150190 	if (!link_up) {
48725082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
48735082Syy150190 		    "Failed to get the link up");
48745082Syy150190 		if (times < 2) {
48755082Syy150190 			/* Reset the link */
48764919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
48775082Syy150190 			    "Reset the link ...");
48787656SSherry.Moore@Sun.COM 			(void) e1000g_reset_adapter(Adapter);
48795082Syy150190 			goto again;
48803526Sxy150489 		}
48813526Sxy150489 	}
48823526Sxy150489 
48833526Sxy150489 	return (B_TRUE);
48843526Sxy150489 }
48853526Sxy150489 
48863526Sxy150489 /*
48873526Sxy150489  * The following loopback settings are from Intel's technical
48883526Sxy150489  * document - "How To Loopback". All the register settings and
48893526Sxy150489  * time delay values are directly inherited from the document
48903526Sxy150489  * without more explanations available.
48913526Sxy150489  */
48923526Sxy150489 static void
48933526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
48943526Sxy150489 {
48953526Sxy150489 	struct e1000_hw *hw;
48963526Sxy150489 	uint32_t ctrl;
48973526Sxy150489 	uint32_t status;
48983526Sxy150489 	uint16_t phy_ctrl;
48998479SChenlu.Chen@Sun.COM 	uint16_t phy_reg;
49005082Syy150190 	uint32_t txcw;
49013526Sxy150489 
49024919Sxy150489 	hw = &Adapter->shared;
49033526Sxy150489 
49043526Sxy150489 	/* Disable Smart Power Down */
49053526Sxy150489 	phy_spd_state(hw, B_FALSE);
49063526Sxy150489 
49077426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
49083526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
49093526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
49103526Sxy150489 
49114919Sxy150489 	switch (hw->mac.type) {
49123526Sxy150489 	case e1000_82540:
49133526Sxy150489 	case e1000_82545:
49143526Sxy150489 	case e1000_82545_rev_3:
49153526Sxy150489 	case e1000_82546:
49163526Sxy150489 	case e1000_82546_rev_3:
49173526Sxy150489 	case e1000_82573:
49183526Sxy150489 		/* Auto-MDI/MDIX off */
49197426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
49203526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
49217426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49224349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
49233526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
49247426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49254349Sxy150489 		    phy_ctrl | MII_CR_RESET);
49265082Syy150190 		/*
49275082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
49285082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
49295082Syy150190 		 * background.
49305082Syy150190 		 */
49317426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
49327426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
49337426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
49347426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
49353526Sxy150489 		break;
49367133Scc210113 	case e1000_80003es2lan:
49377133Scc210113 		/* Force Link Up */
49387426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
49397426SChenliang.Xu@Sun.COM 		    0x1CC);
49407133Scc210113 		/* Sets PCS loopback at 1Gbs */
49417426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
49427426SChenliang.Xu@Sun.COM 		    0x1046);
49437133Scc210113 		break;
49443526Sxy150489 	}
49453526Sxy150489 
49468479SChenlu.Chen@Sun.COM 	/*
49478479SChenlu.Chen@Sun.COM 	 * The following registers should be set for e1000_phy_bm phy type.
49488479SChenlu.Chen@Sun.COM 	 * e1000_82574, e1000_ich10lan and some e1000_ich9lan use this phy.
49498479SChenlu.Chen@Sun.COM 	 * For others, we do not need to set these registers.
49508479SChenlu.Chen@Sun.COM 	 */
49518479SChenlu.Chen@Sun.COM 	if (hw->phy.type == e1000_phy_bm) {
49528479SChenlu.Chen@Sun.COM 		/* Set Default MAC Interface speed to 1GB */
49538479SChenlu.Chen@Sun.COM 		e1000_read_phy_reg(hw, PHY_REG(2, 21), &phy_reg);
49548479SChenlu.Chen@Sun.COM 		phy_reg &= ~0x0007;
49558479SChenlu.Chen@Sun.COM 		phy_reg |= 0x006;
49568479SChenlu.Chen@Sun.COM 		e1000_write_phy_reg(hw, PHY_REG(2, 21), phy_reg);
49578479SChenlu.Chen@Sun.COM 		/* Assert SW reset for above settings to take effect */
49588479SChenlu.Chen@Sun.COM 		e1000_phy_commit(hw);
49598479SChenlu.Chen@Sun.COM 		msec_delay(1);
49608479SChenlu.Chen@Sun.COM 		/* Force Full Duplex */
49618479SChenlu.Chen@Sun.COM 		e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
49628479SChenlu.Chen@Sun.COM 		e1000_write_phy_reg(hw, PHY_REG(769, 16), phy_reg | 0x000C);
49638479SChenlu.Chen@Sun.COM 		/* Set Link Up (in force link) */
49648479SChenlu.Chen@Sun.COM 		e1000_read_phy_reg(hw, PHY_REG(776, 16), &phy_reg);
49658479SChenlu.Chen@Sun.COM 		e1000_write_phy_reg(hw, PHY_REG(776, 16), phy_reg | 0x0040);
49668479SChenlu.Chen@Sun.COM 		/* Force Link */
49678479SChenlu.Chen@Sun.COM 		e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
49688479SChenlu.Chen@Sun.COM 		e1000_write_phy_reg(hw, PHY_REG(769, 16), phy_reg | 0x0040);
49698479SChenlu.Chen@Sun.COM 		/* Set Early Link Enable */
49708479SChenlu.Chen@Sun.COM 		e1000_read_phy_reg(hw, PHY_REG(769, 20), &phy_reg);
49718479SChenlu.Chen@Sun.COM 		e1000_write_phy_reg(hw, PHY_REG(769, 20), phy_reg | 0x0400);
49728479SChenlu.Chen@Sun.COM 	}
49738479SChenlu.Chen@Sun.COM 
49743526Sxy150489 	/* Set loopback */
49757426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
49763526Sxy150489 
49773526Sxy150489 	msec_delay(250);
49783526Sxy150489 
49793526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
49804919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49813526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49823526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
49834349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49844349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
49854349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49863526Sxy150489 
49874919Sxy150489 	switch (hw->mac.type) {
49883526Sxy150489 	case e1000_82540:
49893526Sxy150489 	case e1000_82545:
49903526Sxy150489 	case e1000_82545_rev_3:
49913526Sxy150489 	case e1000_82546:
49923526Sxy150489 	case e1000_82546_rev_3:
49933526Sxy150489 		/*
49943526Sxy150489 		 * For some serdes we'll need to commit the writes now
49953526Sxy150489 		 * so that the status is updated on link
49963526Sxy150489 		 */
49976735Scc210113 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
49984919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49993526Sxy150489 			msec_delay(100);
50004919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
50013526Sxy150489 		}
50023526Sxy150489 
50036735Scc210113 		if (hw->phy.media_type == e1000_media_type_copper) {
50043526Sxy150489 			/* Invert Loss of Signal */
50053526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
50063526Sxy150489 		} else {
50073526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
50084919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
50093526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
50103526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
50113526Sxy150489 		}
50123526Sxy150489 		break;
50133526Sxy150489 
50143526Sxy150489 	case e1000_82571:
50153526Sxy150489 	case e1000_82572:
50165082Syy150190 		/*
50175082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
50185082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
50195082Syy150190 		 * using SerDes analog loopback.
50205082Syy150190 		 */
50216735Scc210113 		if (hw->phy.media_type != e1000_media_type_copper) {
50225082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
50235082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
50245082Syy150190 			txcw &= ~((uint32_t)1 << 31);
50255082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
50265082Syy150190 
50275082Syy150190 			/*
50285082Syy150190 			 * Write 0x410 to Serdes Control register
50295082Syy150190 			 * to enable Serdes analog loopback
50305082Syy150190 			 */
50315082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
50325082Syy150190 			msec_delay(10);
50333526Sxy150489 		}
50348479SChenlu.Chen@Sun.COM 
50358479SChenlu.Chen@Sun.COM 		status = E1000_READ_REG(hw, E1000_STATUS);
50368479SChenlu.Chen@Sun.COM 		/* Set ILOS on fiber nic if half duplex is detected */
50378479SChenlu.Chen@Sun.COM 		if ((hw->phy.media_type == e1000_media_type_fiber) &&
50388479SChenlu.Chen@Sun.COM 		    ((status & E1000_STATUS_FD) == 0 ||
50398479SChenlu.Chen@Sun.COM 		    (status & E1000_STATUS_LU) == 0))
50408479SChenlu.Chen@Sun.COM 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
50418479SChenlu.Chen@Sun.COM 		else if (hw->phy.media_type == e1000_media_type_internal_serdes)
50428479SChenlu.Chen@Sun.COM 			ctrl |= E1000_CTRL_SLU;
50433526Sxy150489 		break;
50443526Sxy150489 
50453526Sxy150489 	case e1000_82573:
50463526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
50478479SChenlu.Chen@Sun.COM 		break;
50488479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
50498479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
50508479SChenlu.Chen@Sun.COM 		ctrl |= E1000_CTRL_SLU;
50513526Sxy150489 		break;
50523526Sxy150489 	}
50538479SChenlu.Chen@Sun.COM 	if (hw->phy.type == e1000_phy_bm)
50548479SChenlu.Chen@Sun.COM 		ctrl |= E1000_CTRL_SLU | E1000_CTRL_ILOS;
50558479SChenlu.Chen@Sun.COM 
50568479SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
50573526Sxy150489 }
50583526Sxy150489 
50593526Sxy150489 static void
50603526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
50613526Sxy150489 {
50623526Sxy150489 	struct e1000_hw *hw;
50633526Sxy150489 	uint32_t rctl;
50643526Sxy150489 	uint32_t ctrl_ext;
50653526Sxy150489 	uint32_t ctrl;
50663526Sxy150489 	uint32_t status;
50673526Sxy150489 	uint32_t txcw;
50687133Scc210113 	uint16_t phydata;
50693526Sxy150489 
50704919Sxy150489 	hw = &Adapter->shared;
50713526Sxy150489 
50723526Sxy150489 	/* Disable Smart Power Down */
50733526Sxy150489 	phy_spd_state(hw, B_FALSE);
50743526Sxy150489 
50757133Scc210113 	switch (hw->mac.type) {
50767133Scc210113 	case e1000_82571:
50777133Scc210113 	case e1000_82572:
50787133Scc210113 		switch (hw->phy.media_type) {
50797133Scc210113 		case e1000_media_type_copper:
50807133Scc210113 			/* Force link up (Must be done before the PHY writes) */
50817133Scc210113 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
50827133Scc210113 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
50837133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
50847133Scc210113 
50857133Scc210113 			rctl = E1000_READ_REG(hw, E1000_RCTL);
50867133Scc210113 			rctl |= (E1000_RCTL_EN |
50877133Scc210113 			    E1000_RCTL_SBP |
50887133Scc210113 			    E1000_RCTL_UPE |
50897133Scc210113 			    E1000_RCTL_MPE |
50907133Scc210113 			    E1000_RCTL_LPE |
50917133Scc210113 			    E1000_RCTL_BAM);		/* 0x803E */
50927133Scc210113 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
50937133Scc210113 
50947133Scc210113 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
50957133Scc210113 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
50967133Scc210113 			    E1000_CTRL_EXT_SDP6_DATA |
50977133Scc210113 			    E1000_CTRL_EXT_SDP7_DATA |
50987133Scc210113 			    E1000_CTRL_EXT_SDP4_DIR |
50997133Scc210113 			    E1000_CTRL_EXT_SDP6_DIR |
51007133Scc210113 			    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
51017133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
51027133Scc210113 
51037133Scc210113 			/*
51047133Scc210113 			 * This sequence tunes the PHY's SDP and no customer
51057133Scc210113 			 * settable values. For background, see comments above
51067133Scc210113 			 * e1000g_set_internal_loopback().
51077133Scc210113 			 */
51087426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
51097133Scc210113 			msec_delay(10);
51107426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
51117426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
51127426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
51137426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
51147426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
51157426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
51167426SChenliang.Xu@Sun.COM 
51177426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
51187426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
51197426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
51207426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
51217426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
51227133Scc210113 
51237133Scc210113 			msec_delay(50);
51247133Scc210113 			break;
51257133Scc210113 		case e1000_media_type_fiber:
51267133Scc210113 		case e1000_media_type_internal_serdes:
51277133Scc210113 			status = E1000_READ_REG(hw, E1000_STATUS);
51287133Scc210113 			if (((status & E1000_STATUS_LU) == 0) ||
51297133Scc210113 			    (hw->phy.media_type ==
51307133Scc210113 			    e1000_media_type_internal_serdes)) {
51317133Scc210113 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
51327133Scc210113 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
51337133Scc210113 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
51347133Scc210113 			}
51357133Scc210113 
51367133Scc210113 			/* Disable autoneg by setting bit 31 of TXCW to zero */
51377133Scc210113 			txcw = E1000_READ_REG(hw, E1000_TXCW);
51387133Scc210113 			txcw &= ~((uint32_t)1 << 31);
51397133Scc210113 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
51407133Scc210113 
51417133Scc210113 			/*
51427133Scc210113 			 * Write 0x410 to Serdes Control register
51437133Scc210113 			 * to enable Serdes analog loopback
51447133Scc210113 			 */
51457133Scc210113 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
51467133Scc210113 			msec_delay(10);
51477133Scc210113 			break;
51487133Scc210113 		default:
51497133Scc210113 			break;
51507133Scc210113 		}
51513526Sxy150489 		break;
51528479SChenlu.Chen@Sun.COM 	case e1000_82574:
51537133Scc210113 	case e1000_80003es2lan:
51548479SChenlu.Chen@Sun.COM 	case e1000_ich9lan:
51558479SChenlu.Chen@Sun.COM 	case e1000_ich10lan:
51567426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
51577426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
51587426SChenliang.Xu@Sun.COM 		    phydata | (1 << 5));
51597133Scc210113 		Adapter->param_adv_autoneg = 1;
51607133Scc210113 		Adapter->param_adv_1000fdx = 1;
51617426SChenliang.Xu@Sun.COM 		(void) e1000g_reset_link(Adapter);
51623526Sxy150489 		break;
51633526Sxy150489 	}
51643526Sxy150489 }
51653526Sxy150489 
51663526Sxy150489 static void
51673526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
51683526Sxy150489 {
51693526Sxy150489 	struct e1000_hw *hw;
51703526Sxy150489 	uint32_t ctrl;
51713526Sxy150489 	uint16_t phy_ctrl;
51723526Sxy150489 
51734919Sxy150489 	hw = &Adapter->shared;
51743526Sxy150489 
51753526Sxy150489 	/* Disable Smart Power Down */
51763526Sxy150489 	phy_spd_state(hw, B_FALSE);
51773526Sxy150489 
51783526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
51794349Sxy150489 	    MII_CR_SPEED_100);
51803526Sxy150489 
51813526Sxy150489 	/* Force 100/FD, reset PHY */
51827426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
51834349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
51843526Sxy150489 	msec_delay(10);
51853526Sxy150489 
51863526Sxy150489 	/* Force 100/FD */
51877426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
51884349Sxy150489 	    phy_ctrl);			/* 0x2100 */
51893526Sxy150489 	msec_delay(10);
51903526Sxy150489 
51913526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
51924919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
51933526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
51943526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
51954349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
51964349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
51974349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
51984349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
51993526Sxy150489 
52004919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
52013526Sxy150489 }
52023526Sxy150489 
52033526Sxy150489 static void
52043526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
52053526Sxy150489 {
52063526Sxy150489 	struct e1000_hw *hw;
52073526Sxy150489 	uint32_t ctrl;
52083526Sxy150489 	uint16_t phy_ctrl;
52093526Sxy150489 
52104919Sxy150489 	hw = &Adapter->shared;
52113526Sxy150489 
52123526Sxy150489 	/* Disable Smart Power Down */
52133526Sxy150489 	phy_spd_state(hw, B_FALSE);
52143526Sxy150489 
52153526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
52164349Sxy150489 	    MII_CR_SPEED_10);
52173526Sxy150489 
52183526Sxy150489 	/* Force 10/FD, reset PHY */
52197426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
52204349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
52213526Sxy150489 	msec_delay(10);
52223526Sxy150489 
52233526Sxy150489 	/* Force 10/FD */
52247426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
52254349Sxy150489 	    phy_ctrl);			/* 0x0100 */
52263526Sxy150489 	msec_delay(10);
52273526Sxy150489 
52283526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
52294919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
52303526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
52313526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
52324349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
52334349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
52344349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
52354349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
52363526Sxy150489 
52374919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
52383526Sxy150489 }
52393526Sxy150489 
52403526Sxy150489 #ifdef __sparc
52413526Sxy150489 static boolean_t
52423526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
52433526Sxy150489 {
52444919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
52453526Sxy150489 	uchar_t *bytes;
52463526Sxy150489 	struct ether_addr sysaddr;
52473526Sxy150489 	uint_t nelts;
52483526Sxy150489 	int err;
52493526Sxy150489 	boolean_t found = B_FALSE;
52503526Sxy150489 
52513526Sxy150489 	/*
52523526Sxy150489 	 * The "vendor's factory-set address" may already have
52533526Sxy150489 	 * been extracted from the chip, but if the property
52543526Sxy150489 	 * "local-mac-address" is set we use that instead.
52553526Sxy150489 	 *
52563526Sxy150489 	 * We check whether it looks like an array of 6
52573526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
52583526Sxy150489 	 * make sense of it this way, we'll ignore it.
52593526Sxy150489 	 */
52603526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
52613526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
52623526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
52633526Sxy150489 		if (nelts == ETHERADDRL) {
52643526Sxy150489 			while (nelts--)
52654919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
52663526Sxy150489 			found = B_TRUE;
52673526Sxy150489 		}
52683526Sxy150489 		ddi_prop_free(bytes);
52693526Sxy150489 	}
52703526Sxy150489 
52713526Sxy150489 	/*
52723526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
52733526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
52743526Sxy150489 	 */
52753526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
52763526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
52773526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
52783526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
52794919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
52803526Sxy150489 				found = B_TRUE;
52813526Sxy150489 			}
52823526Sxy150489 		}
52833526Sxy150489 		ddi_prop_free(bytes);
52843526Sxy150489 	}
52853526Sxy150489 
52863526Sxy150489 	/*
52873526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
52883526Sxy150489 	 * if we netbooted from this interface), we must use this instead
52893526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
52903526Sxy150489 	 * get confused by the address changing as Solaris takes over!
52913526Sxy150489 	 */
52923526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
52933526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
52943526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
52953526Sxy150489 		if (nelts == ETHERADDRL) {
52963526Sxy150489 			while (nelts--)
52974919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
52983526Sxy150489 			found = B_TRUE;
52993526Sxy150489 		}
53003526Sxy150489 		ddi_prop_free(bytes);
53013526Sxy150489 	}
53023526Sxy150489 
53033526Sxy150489 	if (found) {
53044919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
53053526Sxy150489 		    ETHERADDRL);
53063526Sxy150489 	}
53073526Sxy150489 
53083526Sxy150489 	return (found);
53093526Sxy150489 }
53103526Sxy150489 #endif
53113526Sxy150489 
53123526Sxy150489 static int
53133526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
53143526Sxy150489 {
53153526Sxy150489 	dev_info_t *devinfo;
53163526Sxy150489 	int intr_types;
53173526Sxy150489 	int rc;
53183526Sxy150489 
53193526Sxy150489 	devinfo = Adapter->dip;
53203526Sxy150489 
53213526Sxy150489 	/* Get supported interrupt types */
53223526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
53233526Sxy150489 
53243526Sxy150489 	if (rc != DDI_SUCCESS) {
53254919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53263526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
53273526Sxy150489 		return (DDI_FAILURE);
53283526Sxy150489 	}
53293526Sxy150489 
53303526Sxy150489 	/*
53313526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
53323526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
53333526Sxy150489 	 * that it supports MSI, but in fact has problems.
53343526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
53353526Sxy150489 	 * PCI/PCI-X NICs.
53363526Sxy150489 	 */
53374919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
53386986Smx205022 		Adapter->msi_enable = B_FALSE;
53396986Smx205022 
53406986Smx205022 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
53413526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
53423526Sxy150489 
53433526Sxy150489 		if (rc != DDI_SUCCESS) {
53444919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
53453526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
53463526Sxy150489 		} else {
53473526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
53483526Sxy150489 		}
53493526Sxy150489 	}
53503526Sxy150489 
53513526Sxy150489 	if ((Adapter->intr_type == 0) &&
53523526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
53533526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
53543526Sxy150489 
53553526Sxy150489 		if (rc != DDI_SUCCESS) {
53564919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
53573526Sxy150489 			    "Add Legacy interrupts failed\n");
53583526Sxy150489 			return (DDI_FAILURE);
53593526Sxy150489 		}
53603526Sxy150489 
53613526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
53623526Sxy150489 	}
53633526Sxy150489 
53643526Sxy150489 	if (Adapter->intr_type == 0) {
53654919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
53663526Sxy150489 		    "No interrupts registered\n");
53673526Sxy150489 		return (DDI_FAILURE);
53683526Sxy150489 	}
53693526Sxy150489 
53703526Sxy150489 	return (DDI_SUCCESS);
53713526Sxy150489 }
53723526Sxy150489 
53733526Sxy150489 /*
53743526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
53753526Sxy150489  */
53763526Sxy150489 static int
53773526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
53783526Sxy150489 {
53793526Sxy150489 	dev_info_t *devinfo;
53803526Sxy150489 	int count, avail, actual;
53813526Sxy150489 	int x, y, rc, inum = 0;
53823526Sxy150489 	int flag;
53833526Sxy150489 	ddi_intr_handler_t *intr_handler;
53843526Sxy150489 
53853526Sxy150489 	devinfo = Adapter->dip;
53863526Sxy150489 
53873526Sxy150489 	/* get number of interrupts */
53883526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
53893526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
53904919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
53913526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
53923526Sxy150489 		    rc, count);
53933526Sxy150489 		return (DDI_FAILURE);
53943526Sxy150489 	}
53953526Sxy150489 
53963526Sxy150489 	/* get number of available interrupts */
53973526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
53983526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
53994919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
54003526Sxy150489 		    "Get interrupt available number failed. "
54013526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
54023526Sxy150489 		return (DDI_FAILURE);
54033526Sxy150489 	}
54043526Sxy150489 
54053526Sxy150489 	if (avail < count) {
54064919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
54073526Sxy150489 		    "Interrupts count: %d, available: %d\n",
54083526Sxy150489 		    count, avail);
54093526Sxy150489 	}
54103526Sxy150489 
54113526Sxy150489 	/* Allocate an array of interrupt handles */
54123526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
54133526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
54143526Sxy150489 
54153526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
54163526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
54173526Sxy150489 
54183526Sxy150489 	/* call ddi_intr_alloc() */
54193526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
54203526Sxy150489 	    count, &actual, flag);
54213526Sxy150489 
54223526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
54234919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
54243526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
54253526Sxy150489 
54263526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
54273526Sxy150489 		return (DDI_FAILURE);
54283526Sxy150489 	}
54293526Sxy150489 
54303526Sxy150489 	if (actual < count) {
54314919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
54323526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
54333526Sxy150489 		    count, actual);
54343526Sxy150489 	}
54353526Sxy150489 
54363526Sxy150489 	Adapter->intr_cnt = actual;
54373526Sxy150489 
54383526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
54393526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
54403526Sxy150489 
54413526Sxy150489 	if (rc != DDI_SUCCESS) {
54424919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
54433526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
54443526Sxy150489 
54453526Sxy150489 		/* Free already allocated intr */
54463526Sxy150489 		for (y = 0; y < actual; y++)
54473526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
54483526Sxy150489 
54493526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
54503526Sxy150489 		return (DDI_FAILURE);
54513526Sxy150489 	}
54523526Sxy150489 
54533526Sxy150489 	/*
54543526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
54553526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
54563526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
54573526Sxy150489 	 * devices.
54583526Sxy150489 	 */
54594919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
54603526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
54613526Sxy150489 	else
54623526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
54633526Sxy150489 
54643526Sxy150489 	/* Call ddi_intr_add_handler() */
54653526Sxy150489 	for (x = 0; x < actual; x++) {
54663526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
54673526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
54683526Sxy150489 
54693526Sxy150489 		if (rc != DDI_SUCCESS) {
54704919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
54713526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
54723526Sxy150489 
54733526Sxy150489 			/* Remove already added handler */
54743526Sxy150489 			for (y = 0; y < x; y++)
54753526Sxy150489 				(void) ddi_intr_remove_handler(
54763526Sxy150489 				    Adapter->htable[y]);
54773526Sxy150489 
54783526Sxy150489 			/* Free already allocated intr */
54793526Sxy150489 			for (y = 0; y < actual; y++)
54803526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
54813526Sxy150489 
54823526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
54833526Sxy150489 			return (DDI_FAILURE);
54843526Sxy150489 		}
54853526Sxy150489 	}
54863526Sxy150489 
54873526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
54883526Sxy150489 
54893526Sxy150489 	if (rc != DDI_SUCCESS) {
54904919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
54913526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
54923526Sxy150489 
54933526Sxy150489 		/* Free already allocated intr */
54943526Sxy150489 		for (y = 0; y < actual; y++) {
54953526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
54963526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
54973526Sxy150489 		}
54983526Sxy150489 
54993526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
55003526Sxy150489 		return (DDI_FAILURE);
55013526Sxy150489 	}
55023526Sxy150489 
55033526Sxy150489 	return (DDI_SUCCESS);
55043526Sxy150489 }
55053526Sxy150489 
55063526Sxy150489 static int
55073526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
55083526Sxy150489 {
55093526Sxy150489 	int x;
55103526Sxy150489 	int rc;
55113526Sxy150489 
55123526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
55133526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
55143526Sxy150489 		if (rc != DDI_SUCCESS) {
55154919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55163526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
55173526Sxy150489 			return (DDI_FAILURE);
55183526Sxy150489 		}
55193526Sxy150489 
55203526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
55213526Sxy150489 		if (rc != DDI_SUCCESS) {
55224919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55233526Sxy150489 			    "Free intr failed: %d\n", rc);
55243526Sxy150489 			return (DDI_FAILURE);
55253526Sxy150489 		}
55263526Sxy150489 	}
55273526Sxy150489 
55283526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
55293526Sxy150489 
55303526Sxy150489 	return (DDI_SUCCESS);
55313526Sxy150489 }
55323526Sxy150489 
55333526Sxy150489 static int
55343526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
55353526Sxy150489 {
55363526Sxy150489 	int x;
55373526Sxy150489 	int rc;
55383526Sxy150489 
55393526Sxy150489 	/* Enable interrupts */
55403526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
55413526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
55423526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
55433526Sxy150489 		    Adapter->intr_cnt);
55443526Sxy150489 		if (rc != DDI_SUCCESS) {
55454919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55463526Sxy150489 			    "Enable block intr failed: %d\n", rc);
55473526Sxy150489 			return (DDI_FAILURE);
55483526Sxy150489 		}
55493526Sxy150489 	} else {
55503526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
55513526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
55523526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
55533526Sxy150489 			if (rc != DDI_SUCCESS) {
55544919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55553526Sxy150489 				    "Enable intr failed: %d\n", rc);
55563526Sxy150489 				return (DDI_FAILURE);
55573526Sxy150489 			}
55583526Sxy150489 		}
55593526Sxy150489 	}
55603526Sxy150489 
55613526Sxy150489 	return (DDI_SUCCESS);
55623526Sxy150489 }
55633526Sxy150489 
55643526Sxy150489 static int
55653526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
55663526Sxy150489 {
55673526Sxy150489 	int x;
55683526Sxy150489 	int rc;
55693526Sxy150489 
55703526Sxy150489 	/* Disable all interrupts */
55713526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
55723526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
55733526Sxy150489 		    Adapter->intr_cnt);
55743526Sxy150489 		if (rc != DDI_SUCCESS) {
55754919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55763526Sxy150489 			    "Disable block intr failed: %d\n", rc);
55773526Sxy150489 			return (DDI_FAILURE);
55783526Sxy150489 		}
55793526Sxy150489 	} else {
55803526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
55813526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
55823526Sxy150489 			if (rc != DDI_SUCCESS) {
55834919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
55843526Sxy150489 				    "Disable intr failed: %d\n", rc);
55853526Sxy150489 				return (DDI_FAILURE);
55863526Sxy150489 			}
55873526Sxy150489 		}
55883526Sxy150489 	}
55893526Sxy150489 
55903526Sxy150489 	return (DDI_SUCCESS);
55913526Sxy150489 }
55925082Syy150190 
55935082Syy150190 /*
55945082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
55955082Syy150190  */
55965082Syy150190 static void
55975082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
55985082Syy150190 {
55995082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
56005082Syy150190 
56017426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
56027426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
56037426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
56047426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
56057426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
56067426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
56077426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
56087426SChenliang.Xu@Sun.COM 	    &Adapter->phy_1000t_status);
56097426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
56106394Scc210113 
56116394Scc210113 	Adapter->param_autoneg_cap =
56126394Scc210113 	    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
56136394Scc210113 	Adapter->param_pause_cap =
56146394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
56156394Scc210113 	Adapter->param_asym_pause_cap =
56166394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
56176394Scc210113 	Adapter->param_1000fdx_cap =
56186394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
56196394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
56206394Scc210113 	Adapter->param_1000hdx_cap =
56216394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
56226394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
56236394Scc210113 	Adapter->param_100t4_cap =
56246394Scc210113 	    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
56256394Scc210113 	Adapter->param_100fdx_cap =
56266394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
56276394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
56286394Scc210113 	Adapter->param_100hdx_cap =
56296394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
56306394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
56316394Scc210113 	Adapter->param_10fdx_cap =
56326394Scc210113 	    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
56336394Scc210113 	Adapter->param_10hdx_cap =
56346394Scc210113 	    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
56356394Scc210113 
56366394Scc210113 	Adapter->param_adv_autoneg = hw->mac.autoneg;
56376394Scc210113 	Adapter->param_adv_pause =
56386394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
56396394Scc210113 	Adapter->param_adv_asym_pause =
56406394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
56416394Scc210113 	Adapter->param_adv_1000hdx =
56426394Scc210113 	    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
56436394Scc210113 	Adapter->param_adv_100t4 =
56446394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
56456394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
56466394Scc210113 		Adapter->param_adv_1000fdx =
56476394Scc210113 		    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
56486394Scc210113 		Adapter->param_adv_100fdx =
56496394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
56506394Scc210113 		Adapter->param_adv_100hdx =
56516394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
56526394Scc210113 		Adapter->param_adv_10fdx =
56536394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
56546394Scc210113 		Adapter->param_adv_10hdx =
56556394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
56566394Scc210113 	}
56576394Scc210113 
56586394Scc210113 	Adapter->param_lp_autoneg =
56596394Scc210113 	    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
56606394Scc210113 	Adapter->param_lp_pause =
56616394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
56626394Scc210113 	Adapter->param_lp_asym_pause =
56636394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
56646394Scc210113 	Adapter->param_lp_1000fdx =
56656394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
56666394Scc210113 	Adapter->param_lp_1000hdx =
56676394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
56686394Scc210113 	Adapter->param_lp_100t4 =
56696394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
56706394Scc210113 	Adapter->param_lp_100fdx =
56716394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
56726394Scc210113 	Adapter->param_lp_100hdx =
56736394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
56746394Scc210113 	Adapter->param_lp_10fdx =
56756394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
56766394Scc210113 	Adapter->param_lp_10hdx =
56776394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
56785082Syy150190 }
56795273Sgl147354 
56805273Sgl147354 /*
56815273Sgl147354  * FMA support
56825273Sgl147354  */
56835273Sgl147354 
56845273Sgl147354 int
56855273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
56865273Sgl147354 {
56875273Sgl147354 	ddi_fm_error_t de;
56885273Sgl147354 
56895273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
56905273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
56915273Sgl147354 	return (de.fme_status);
56925273Sgl147354 }
56935273Sgl147354 
56945273Sgl147354 int
56955273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
56965273Sgl147354 {
56975273Sgl147354 	ddi_fm_error_t de;
56985273Sgl147354 
56995273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
57005273Sgl147354 	return (de.fme_status);
57015273Sgl147354 }
57025273Sgl147354 
57035273Sgl147354 /*
57045273Sgl147354  * The IO fault service error handling callback function
57055273Sgl147354  */
57067426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
57075273Sgl147354 static int
57085273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
57095273Sgl147354 {
57105273Sgl147354 	/*
57115273Sgl147354 	 * as the driver can always deal with an error in any dma or
57125273Sgl147354 	 * access handle, we can just return the fme_status value.
57135273Sgl147354 	 */
57145273Sgl147354 	pci_ereport_post(dip, err, NULL);
57155273Sgl147354 	return (err->fme_status);
57165273Sgl147354 }
57175273Sgl147354 
57185273Sgl147354 static void
57195273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
57205273Sgl147354 {
57215273Sgl147354 	ddi_iblock_cookie_t iblk;
57225273Sgl147354 	int fma_acc_flag, fma_dma_flag;
57235273Sgl147354 
57245273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
57255273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
57265273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
57275273Sgl147354 		fma_acc_flag = 1;
57285273Sgl147354 	} else {
57295273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
57305273Sgl147354 		fma_acc_flag = 0;
57315273Sgl147354 	}
57325273Sgl147354 
57335273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
57345273Sgl147354 		fma_dma_flag = 1;
57355273Sgl147354 	} else {
57365273Sgl147354 		fma_dma_flag = 0;
57375273Sgl147354 	}
57385273Sgl147354 
57395273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
57405273Sgl147354 
57415273Sgl147354 	if (Adapter->fm_capabilities) {
57425273Sgl147354 
57435273Sgl147354 		/* Register capabilities with IO Fault Services */
57445273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
57455273Sgl147354 
57465273Sgl147354 		/*
57475273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
57485273Sgl147354 		 */
57495273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
57505273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
57515273Sgl147354 			pci_ereport_setup(Adapter->dip);
57525273Sgl147354 
57535273Sgl147354 		/*
57545273Sgl147354 		 * Register error callback if error callback capable
57555273Sgl147354 		 */
57565273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
57575273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
57585273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
57595273Sgl147354 	}
57605273Sgl147354 }
57615273Sgl147354 
57625273Sgl147354 static void
57635273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
57645273Sgl147354 {
57655273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
57665273Sgl147354 	if (Adapter->fm_capabilities) {
57675273Sgl147354 
57685273Sgl147354 		/*
57695273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
57705273Sgl147354 		 */
57715273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
57725273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
57735273Sgl147354 			pci_ereport_teardown(Adapter->dip);
57745273Sgl147354 
57755273Sgl147354 		/*
57765273Sgl147354 		 * Un-register error callback if error callback capable
57775273Sgl147354 		 */
57785273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
57795273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
57805273Sgl147354 
57815273Sgl147354 		/* Unregister from IO Fault Services */
57825273Sgl147354 		ddi_fm_fini(Adapter->dip);
57835273Sgl147354 	}
57845273Sgl147354 }
57855273Sgl147354 
57865273Sgl147354 void
57875273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
57885273Sgl147354 {
57895273Sgl147354 	uint64_t ena;
57905273Sgl147354 	char buf[FM_MAX_CLASS];
57915273Sgl147354 
57925273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
57935273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
57945273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
57955273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
57965273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
57975273Sgl147354 	}
57985273Sgl147354 }
57996512Ssowmini 
58007656SSherry.Moore@Sun.COM /*
58017656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
58027656SSherry.Moore@Sun.COM  *
58037656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
58047656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
58057656SSherry.Moore@Sun.COM  * blocked.
58067656SSherry.Moore@Sun.COM  *
58077656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
58087656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
58097656SSherry.Moore@Sun.COM  */
58107656SSherry.Moore@Sun.COM static int
58117656SSherry.Moore@Sun.COM e1000g_quiesce(dev_info_t *devinfo)
58127656SSherry.Moore@Sun.COM {
58137656SSherry.Moore@Sun.COM 	struct e1000g *Adapter;
58147656SSherry.Moore@Sun.COM 
58157656SSherry.Moore@Sun.COM 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
58167656SSherry.Moore@Sun.COM 
58177656SSherry.Moore@Sun.COM 	if (Adapter == NULL)
58187656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
58197656SSherry.Moore@Sun.COM 
58207656SSherry.Moore@Sun.COM 	e1000g_clear_all_interrupts(Adapter);
58217656SSherry.Moore@Sun.COM 
58227656SSherry.Moore@Sun.COM 	(void) e1000_reset_hw(&Adapter->shared);
58237656SSherry.Moore@Sun.COM 
58247656SSherry.Moore@Sun.COM 	/* Setup our HW Tx Head & Tail descriptor pointers */
58257656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
58267656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
58277656SSherry.Moore@Sun.COM 
58287656SSherry.Moore@Sun.COM 	/* Setup our HW Rx Head & Tail descriptor pointers */
58297656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDH(0), 0);
58307656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDT(0), 0);
58317656SSherry.Moore@Sun.COM 
58327656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
58337656SSherry.Moore@Sun.COM }
58347656SSherry.Moore@Sun.COM 
58356512Ssowmini static int
58366512Ssowmini e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
58376512Ssowmini     uint_t pr_valsize, void *pr_val)
58386512Ssowmini {
58396512Ssowmini 	link_flowctrl_t fl;
58406512Ssowmini 	int err = 0;
58416512Ssowmini 
58426512Ssowmini 	ASSERT(pr_valsize > 0);
58436512Ssowmini 	switch (pr_num) {
58446789Sam223141 	case MAC_PROP_AUTONEG:
58456512Ssowmini 		*(uint8_t *)pr_val =
58466512Ssowmini 		    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0);
58476512Ssowmini 		break;
58486789Sam223141 	case MAC_PROP_FLOWCTRL:
58496512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
58506512Ssowmini 			return (EINVAL);
58516512Ssowmini 		fl = LINK_FLOWCTRL_BI;
58526512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
58536512Ssowmini 		break;
58546789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
58556789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
58566512Ssowmini 		*(uint8_t *)pr_val =
58576512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
58586512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
58596512Ssowmini 		break;
58606789Sam223141 	case MAC_PROP_ADV_1000HDX_CAP:
58616789Sam223141 	case MAC_PROP_EN_1000HDX_CAP:
58626512Ssowmini 		*(uint8_t *)pr_val =
58636512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
58646512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
58656512Ssowmini 		break;
58666789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
58676789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
58686512Ssowmini 		*(uint8_t *)pr_val =
58696512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
58706512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
58717426SChenliang.Xu@Sun.COM 		break;
58726789Sam223141 	case MAC_PROP_ADV_100HDX_CAP:
58736789Sam223141 	case MAC_PROP_EN_100HDX_CAP:
58746512Ssowmini 		*(uint8_t *)pr_val =
58756512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
58766512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
58776512Ssowmini 		break;
58786789Sam223141 	case MAC_PROP_ADV_10FDX_CAP:
58796789Sam223141 	case MAC_PROP_EN_10FDX_CAP:
58806512Ssowmini 		*(uint8_t *)pr_val =
58816512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
58826512Ssowmini 		break;
58836789Sam223141 	case MAC_PROP_ADV_10HDX_CAP:
58846789Sam223141 	case MAC_PROP_EN_10HDX_CAP:
58856512Ssowmini 		*(uint8_t *)pr_val =
58866512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
58876512Ssowmini 		break;
58886512Ssowmini 	default:
58896512Ssowmini 		err = ENOTSUP;
58906512Ssowmini 		break;
58916512Ssowmini 	}
58926512Ssowmini 	return (err);
58936512Ssowmini }
58946512Ssowmini 
58956512Ssowmini /*
58966512Ssowmini  * synchronize the adv* and en* parameters.
58976512Ssowmini  *
58986512Ssowmini  * See comments in <sys/dld.h> for details of the *_en_*
58996512Ssowmini  * parameters. The usage of ndd for setting adv parameters will
59006512Ssowmini  * synchronize all the en parameters with the e1000g parameters,
59017607STed.You@Sun.COM  * implicitly disabling any settings made via dladm.
59026512Ssowmini  */
59036512Ssowmini static void
59046512Ssowmini e1000g_param_sync(struct e1000g *Adapter)
59056512Ssowmini {
59066512Ssowmini 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
59076512Ssowmini 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
59086512Ssowmini 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
59096512Ssowmini 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
59106512Ssowmini 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
59116512Ssowmini 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
59126512Ssowmini }
59137607STed.You@Sun.COM 
59147607STed.You@Sun.COM /*
59157607STed.You@Sun.COM  * e1000g_get_driver_control - tell manageability firmware that the driver
59167607STed.You@Sun.COM  * has control.
59177607STed.You@Sun.COM  */
59187607STed.You@Sun.COM static void
59197607STed.You@Sun.COM e1000g_get_driver_control(struct e1000_hw *hw)
59207607STed.You@Sun.COM {
59217607STed.You@Sun.COM 	uint32_t ctrl_ext;
59227607STed.You@Sun.COM 	uint32_t swsm;
59237607STed.You@Sun.COM 
59247607STed.You@Sun.COM 	/* tell manageability firmware the driver has taken over */
59257607STed.You@Sun.COM 	switch (hw->mac.type) {
59267607STed.You@Sun.COM 	case e1000_82573:
59277607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
59287607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
59297607STed.You@Sun.COM 		break;
59307607STed.You@Sun.COM 	case e1000_82571:
59317607STed.You@Sun.COM 	case e1000_82572:
59327607STed.You@Sun.COM 	case e1000_82574:
59337607STed.You@Sun.COM 	case e1000_80003es2lan:
59347607STed.You@Sun.COM 	case e1000_ich8lan:
59357607STed.You@Sun.COM 	case e1000_ich9lan:
59367607STed.You@Sun.COM 	case e1000_ich10lan:
59377607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
59387607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
59397607STed.You@Sun.COM 		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
59407607STed.You@Sun.COM 		break;
59417607STed.You@Sun.COM 	default:
59427607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
59437607STed.You@Sun.COM 		break;
59447607STed.You@Sun.COM 	}
59457607STed.You@Sun.COM }
59467607STed.You@Sun.COM 
59477607STed.You@Sun.COM /*
59487607STed.You@Sun.COM  * e1000g_release_driver_control - tell manageability firmware that the driver
59497607STed.You@Sun.COM  * has released control.
59507607STed.You@Sun.COM  */
59517607STed.You@Sun.COM static void
59527607STed.You@Sun.COM e1000g_release_driver_control(struct e1000_hw *hw)
59537607STed.You@Sun.COM {
59547607STed.You@Sun.COM 	uint32_t ctrl_ext;
59557607STed.You@Sun.COM 	uint32_t swsm;
59567607STed.You@Sun.COM 
59577607STed.You@Sun.COM 	/* tell manageability firmware the driver has released control */
59587607STed.You@Sun.COM 	switch (hw->mac.type) {
59597607STed.You@Sun.COM 	case e1000_82573:
59607607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
59617607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
59627607STed.You@Sun.COM 		break;
59637607STed.You@Sun.COM 	case e1000_82571:
59647607STed.You@Sun.COM 	case e1000_82572:
59657607STed.You@Sun.COM 	case e1000_82574:
59667607STed.You@Sun.COM 	case e1000_80003es2lan:
59677607STed.You@Sun.COM 	case e1000_ich8lan:
59687607STed.You@Sun.COM 	case e1000_ich9lan:
59697607STed.You@Sun.COM 	case e1000_ich10lan:
59707607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
59717607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
59727607STed.You@Sun.COM 		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
59737607STed.You@Sun.COM 		break;
59747607STed.You@Sun.COM 	default:
59757607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
59767607STed.You@Sun.COM 		break;
59777607STed.You@Sun.COM 	}
59787607STed.You@Sun.COM }
59797722SShuguo.Yang@Sun.COM 
59807722SShuguo.Yang@Sun.COM /*
59817722SShuguo.Yang@Sun.COM  * Restore e1000g promiscuous mode.
59827722SShuguo.Yang@Sun.COM  */
59837722SShuguo.Yang@Sun.COM static void
59847722SShuguo.Yang@Sun.COM e1000g_restore_promisc(struct e1000g *Adapter)
59857722SShuguo.Yang@Sun.COM {
59867722SShuguo.Yang@Sun.COM 	if (Adapter->e1000g_promisc) {
59877722SShuguo.Yang@Sun.COM 		uint32_t rctl;
59887722SShuguo.Yang@Sun.COM 
59897722SShuguo.Yang@Sun.COM 		rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
59907722SShuguo.Yang@Sun.COM 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
59917722SShuguo.Yang@Sun.COM 		E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
59927722SShuguo.Yang@Sun.COM 	}
59937722SShuguo.Yang@Sun.COM }
5994