xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 8178:951feae9d474)
13526Sxy150489 /*
23526Sxy150489  * This file is provided under a CDDLv1 license.  When using or
33526Sxy150489  * redistributing this file, you may do so under this license.
43526Sxy150489  * In redistributing this file this license must be included
53526Sxy150489  * and no other modification of this header file is permitted.
63526Sxy150489  *
73526Sxy150489  * CDDL LICENSE SUMMARY
83526Sxy150489  *
95882Syy150190  * Copyright(c) 1999 - 2008 Intel Corporation. All rights reserved.
103526Sxy150489  *
113526Sxy150489  * The contents of this file are subject to the terms of Version
123526Sxy150489  * 1.0 of the Common Development and Distribution License (the "License").
133526Sxy150489  *
143526Sxy150489  * You should have received a copy of the License with this software.
153526Sxy150489  * You can obtain a copy of the License at
163526Sxy150489  *	http://www.opensolaris.org/os/licensing.
173526Sxy150489  * See the License for the specific language governing permissions
183526Sxy150489  * and limitations under the License.
193526Sxy150489  */
203526Sxy150489 
213526Sxy150489 /*
225858Scc210113  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
238118SVasumathi.Sundaram@Sun.COM  * Use is subject to license terms.
243526Sxy150489  */
253526Sxy150489 
263526Sxy150489 /*
273526Sxy150489  * **********************************************************************
283526Sxy150489  *									*
293526Sxy150489  * Module Name:								*
303526Sxy150489  *   e1000g_main.c							*
313526Sxy150489  *									*
323526Sxy150489  * Abstract:								*
334919Sxy150489  *   This file contains the interface routines for the solaris OS.	*
344919Sxy150489  *   It has all DDI entry point routines and GLD entry point routines.	*
353526Sxy150489  *									*
364919Sxy150489  *   This file also contains routines that take care of initialization	*
374919Sxy150489  *   uninit routine and interrupt routine.				*
383526Sxy150489  *									*
393526Sxy150489  * **********************************************************************
403526Sxy150489  */
413526Sxy150489 
423526Sxy150489 #include <sys/dlpi.h>
433526Sxy150489 #include <sys/mac.h>
443526Sxy150489 #include "e1000g_sw.h"
453526Sxy150489 #include "e1000g_debug.h"
463526Sxy150489 
477656SSherry.Moore@Sun.COM static char ident[] = "Intel PRO/1000 Ethernet";
483526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
49*8178SChenlu.Chen@Sun.COM static char e1000g_version[] = "Driver Ver. 5.2.14";
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)
675882Syy150190 static uint32_t e1000g_get_itr(uint32_t, uint32_t, uint32_t);
685882Syy150190 #pragma inline(e1000g_get_itr)
693526Sxy150489 static int e1000g_init(struct e1000g *);
704919Sxy150489 static int e1000g_start(struct e1000g *, boolean_t);
714919Sxy150489 static void e1000g_stop(struct e1000g *, boolean_t);
723526Sxy150489 static int e1000g_m_start(void *);
733526Sxy150489 static void e1000g_m_stop(void *);
743526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
753526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
763526Sxy150489 static int e1000g_m_unicst(void *, const uint8_t *);
773526Sxy150489 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
783526Sxy150489 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
793526Sxy150489 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
803526Sxy150489 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
813526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
823526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
836394Scc210113 static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
846394Scc210113     uint_t, const void *);
856394Scc210113 static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
868118SVasumathi.Sundaram@Sun.COM     uint_t, uint_t, void *, uint_t *);
876394Scc210113 static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
886394Scc210113     const void *);
896394Scc210113 static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t,
908118SVasumathi.Sundaram@Sun.COM     uint_t, void *, uint_t *);
914919Sxy150489 static void e1000g_init_locks(struct e1000g *);
924919Sxy150489 static void e1000g_destroy_locks(struct e1000g *);
934919Sxy150489 static int e1000g_identify_hardware(struct e1000g *);
944919Sxy150489 static int e1000g_regs_map(struct e1000g *);
954919Sxy150489 static int e1000g_set_driver_params(struct e1000g *);
966394Scc210113 static void e1000g_set_bufsize(struct e1000g *);
974919Sxy150489 static int e1000g_register_mac(struct e1000g *);
984919Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *);
994919Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *);
1004919Sxy150489 static void e1000g_init_unicst(struct e1000g *);
1013526Sxy150489 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
1023526Sxy150489 
1033526Sxy150489 /*
1043526Sxy150489  * Local routines
1053526Sxy150489  */
1067656SSherry.Moore@Sun.COM static boolean_t e1000g_reset_adapter(struct e1000g *);
1074919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1084919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1094061Sxy150489 static void e1000g_link_timer(void *);
1104919Sxy150489 static void e1000g_local_timer(void *);
1114061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1123526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1133526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1144919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1154919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1164919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1174919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1184919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1194919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1204919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1214919Sxy150489 static void stop_link_timer(struct e1000g *);
1224919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1234919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
1244919Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *);
1254919Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
1263526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1274919Sxy150489 #ifdef E1000G_DEBUG
1284919Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
1294919Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
1304919Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
1314919Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
1324919Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
1334919Sxy150489     struct iocblk *, mblk_t *);
1344919Sxy150489 #endif
1354919Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
1364919Sxy150489     struct iocblk *, mblk_t *);
1377133Scc210113 static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
1384919Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
1394919Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *);
1404919Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *);
1414919Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *);
1424919Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *);
1434919Sxy150489 static int e1000g_add_intrs(struct e1000g *);
1444919Sxy150489 static int e1000g_intr_add(struct e1000g *, int);
1454919Sxy150489 static int e1000g_rem_intrs(struct e1000g *);
1464919Sxy150489 static int e1000g_enable_intrs(struct e1000g *);
1474919Sxy150489 static int e1000g_disable_intrs(struct e1000g *);
1484919Sxy150489 static boolean_t e1000g_link_up(struct e1000g *);
1493526Sxy150489 #ifdef __sparc
1504919Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *);
1513526Sxy150489 #endif
1525082Syy150190 static void e1000g_get_phy_state(struct e1000g *);
1534982Syy150190 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
1545273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1555273Sgl147354     const void *impl_data);
1565273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1575273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1586512Ssowmini static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
1596512Ssowmini static void e1000g_param_sync(struct e1000g *);
1607607STed.You@Sun.COM static void e1000g_get_driver_control(struct e1000_hw *);
1617607STed.You@Sun.COM static void e1000g_release_driver_control(struct e1000_hw *);
1627722SShuguo.Yang@Sun.COM static void e1000g_restore_promisc(struct e1000g *Adapter);
1636512Ssowmini 
1646512Ssowmini mac_priv_prop_t e1000g_priv_props[] = {
1656512Ssowmini 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
1666512Ssowmini 	{"_tx_interrupt_enable", MAC_PROP_PERM_RW},
1676512Ssowmini 	{"_tx_intr_delay", MAC_PROP_PERM_RW},
1686512Ssowmini 	{"_tx_intr_abs_delay", MAC_PROP_PERM_RW},
1696512Ssowmini 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
1706512Ssowmini 	{"_max_num_rcv_packets", MAC_PROP_PERM_RW},
1716512Ssowmini 	{"_rx_intr_delay", MAC_PROP_PERM_RW},
1726512Ssowmini 	{"_rx_intr_abs_delay", MAC_PROP_PERM_RW},
1736512Ssowmini 	{"_intr_throttling_rate", MAC_PROP_PERM_RW},
1746512Ssowmini 	{"_intr_adaptive", MAC_PROP_PERM_RW},
1756512Ssowmini 	{"_tx_recycle_thresh", MAC_PROP_PERM_RW},
1766512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
1776512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
1786512Ssowmini 	{"_tx_recycle_num", MAC_PROP_PERM_RW}
1796512Ssowmini };
1806512Ssowmini #define	E1000G_MAX_PRIV_PROPS	\
1816512Ssowmini 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
1826512Ssowmini 
1833526Sxy150489 
1843526Sxy150489 static struct cb_ops cb_ws_ops = {
1853526Sxy150489 	nulldev,		/* cb_open */
1863526Sxy150489 	nulldev,		/* cb_close */
1873526Sxy150489 	nodev,			/* cb_strategy */
1883526Sxy150489 	nodev,			/* cb_print */
1893526Sxy150489 	nodev,			/* cb_dump */
1903526Sxy150489 	nodev,			/* cb_read */
1913526Sxy150489 	nodev,			/* cb_write */
1923526Sxy150489 	nodev,			/* cb_ioctl */
1933526Sxy150489 	nodev,			/* cb_devmap */
1943526Sxy150489 	nodev,			/* cb_mmap */
1953526Sxy150489 	nodev,			/* cb_segmap */
1963526Sxy150489 	nochpoll,		/* cb_chpoll */
1973526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1983526Sxy150489 	NULL,			/* cb_stream */
1993526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
2003526Sxy150489 	CB_REV,			/* cb_rev */
2013526Sxy150489 	nodev,			/* cb_aread */
2023526Sxy150489 	nodev			/* cb_awrite */
2033526Sxy150489 };
2043526Sxy150489 
2053526Sxy150489 static struct dev_ops ws_ops = {
2063526Sxy150489 	DEVO_REV,		/* devo_rev */
2073526Sxy150489 	0,			/* devo_refcnt */
2083526Sxy150489 	NULL,			/* devo_getinfo */
2093526Sxy150489 	nulldev,		/* devo_identify */
2103526Sxy150489 	nulldev,		/* devo_probe */
2114919Sxy150489 	e1000g_attach,		/* devo_attach */
2124919Sxy150489 	e1000g_detach,		/* devo_detach */
2133526Sxy150489 	nodev,			/* devo_reset */
2143526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
2153526Sxy150489 	NULL,			/* devo_bus_ops */
2167656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
2177656SSherry.Moore@Sun.COM 	e1000g_quiesce		/* devo_quiesce */
2183526Sxy150489 };
2193526Sxy150489 
2203526Sxy150489 static struct modldrv modldrv = {
2213526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
2223526Sxy150489 	ident,			/* Discription string */
2233526Sxy150489 	&ws_ops,		/* driver ops */
2243526Sxy150489 };
2253526Sxy150489 
2263526Sxy150489 static struct modlinkage modlinkage = {
2273526Sxy150489 	MODREV_1, &modldrv, NULL
2283526Sxy150489 };
2293526Sxy150489 
2304919Sxy150489 /* Access attributes for register mapping */
2314919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
2323526Sxy150489 	DDI_DEVICE_ATTR_V0,
2333526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2343526Sxy150489 	DDI_STRICTORDER_ACC,
2355273Sgl147354 	DDI_FLAGERR_ACC
2363526Sxy150489 };
2373526Sxy150489 
2386394Scc210113 #define	E1000G_M_CALLBACK_FLAGS \
2396394Scc210113 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
2403526Sxy150489 
2413526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2423526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2433526Sxy150489 	e1000g_m_stat,
2443526Sxy150489 	e1000g_m_start,
2453526Sxy150489 	e1000g_m_stop,
2463526Sxy150489 	e1000g_m_promisc,
2473526Sxy150489 	e1000g_m_multicst,
2483526Sxy150489 	e1000g_m_unicst,
2493526Sxy150489 	e1000g_m_tx,
2505882Syy150190 	NULL,
2513526Sxy150489 	e1000g_m_ioctl,
2526394Scc210113 	e1000g_m_getcapab,
2536394Scc210113 	NULL,
2546394Scc210113 	NULL,
2556394Scc210113 	e1000g_m_setprop,
2566394Scc210113 	e1000g_m_getprop
2573526Sxy150489 };
2583526Sxy150489 
2593526Sxy150489 /*
2603526Sxy150489  * Global variables
2613526Sxy150489  */
2623526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2633526Sxy150489 /*
2644894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2654349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2664349Sxy150489  * enabled. If we force the driver to detach while there are still some
2674349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2684349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2694349Sxy150489  * structure will be freed after the driver is detached. However when we
2704349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2714349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2724894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2734894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2744894Syy150190  * after the system board level DR operation. For this reason, the global
2754894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2764349Sxy150489  */
2774894Syy150190 #ifdef __sparc
2784894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2794894Syy150190 #else
2804894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2814894Syy150190 #endif
2824349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2834894Syy150190 
2844349Sxy150489 /*
2853526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2863526Sxy150489  * and the rx packets release in detach processing to make them mutually
2873526Sxy150489  * exclusive.
2883526Sxy150489  * The rx recycling processes different rx packets in different threads,
2893526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2903526Sxy150489  * recycling threads.
2913526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2923526Sxy150489  * it mutually exclusive with the rx recycling.
2933526Sxy150489  */
2943526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2953526Sxy150489 /*
2963526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2973526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2983526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2993526Sxy150489  * resources during the initialization of the instances, then the flag will
3003526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
3013526Sxy150489  * in parallel, we need to use this lock to protect the flag.
3023526Sxy150489  */
3033526Sxy150489 krwlock_t e1000g_dma_type_lock;
3043526Sxy150489 
3057133Scc210113 /*
3067133Scc210113  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
3077133Scc210113  * Based on the information from Intel, the 82546 chipset has some hardware
3087133Scc210113  * problem. When one port is being reset and the other port is trying to
3097133Scc210113  * access the eeprom, it could cause system hang or panic. To workaround this
3107133Scc210113  * hardware problem, we use a global mutex to prevent such operations from
3117133Scc210113  * happening simultaneously on different instances. This workaround is applied
3127133Scc210113  * to all the devices supported by this driver.
3137133Scc210113  */
3147133Scc210113 kmutex_t e1000g_nvm_lock;
3153526Sxy150489 
3163526Sxy150489 /*
3173526Sxy150489  * Loadable module configuration entry points for the driver
3183526Sxy150489  */
3193526Sxy150489 
3203526Sxy150489 /*
3214919Sxy150489  * _init - module initialization
3223526Sxy150489  */
3233526Sxy150489 int
3243526Sxy150489 _init(void)
3253526Sxy150489 {
3263526Sxy150489 	int status;
3273526Sxy150489 
3283526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
3293526Sxy150489 	status = mod_install(&modlinkage);
3303526Sxy150489 	if (status != DDI_SUCCESS)
3313526Sxy150489 		mac_fini_ops(&ws_ops);
3323526Sxy150489 	else {
3333526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
3343526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
3357133Scc210113 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
3363526Sxy150489 	}
3373526Sxy150489 
3383526Sxy150489 	return (status);
3393526Sxy150489 }
3403526Sxy150489 
3413526Sxy150489 /*
3424919Sxy150489  * _fini - module finalization
3433526Sxy150489  */
3443526Sxy150489 int
3453526Sxy150489 _fini(void)
3463526Sxy150489 {
3473526Sxy150489 	int status;
3483526Sxy150489 
3493526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3503526Sxy150489 	if (e1000g_mblks_pending != 0) {
3513526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3523526Sxy150489 		return (EBUSY);
3533526Sxy150489 	}
3543526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3553526Sxy150489 
3563526Sxy150489 	status = mod_remove(&modlinkage);
3573526Sxy150489 	if (status == DDI_SUCCESS) {
3583526Sxy150489 		mac_fini_ops(&ws_ops);
3594349Sxy150489 
3604349Sxy150489 		if (e1000g_force_detach) {
3614349Sxy150489 			private_devi_list_t *devi_node;
3624349Sxy150489 
3634349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3644349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3654349Sxy150489 				devi_node = e1000g_private_devi_list;
3664349Sxy150489 				e1000g_private_devi_list =
3674349Sxy150489 				    e1000g_private_devi_list->next;
3684349Sxy150489 
3694349Sxy150489 				kmem_free(devi_node->priv_dip,
3704349Sxy150489 				    sizeof (struct dev_info));
3714349Sxy150489 				kmem_free(devi_node,
3724349Sxy150489 				    sizeof (private_devi_list_t));
3734349Sxy150489 			}
3744349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3754349Sxy150489 		}
3764349Sxy150489 
3773526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3783526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3797133Scc210113 		mutex_destroy(&e1000g_nvm_lock);
3803526Sxy150489 	}
3813526Sxy150489 
3823526Sxy150489 	return (status);
3833526Sxy150489 }
3843526Sxy150489 
3853526Sxy150489 /*
3864919Sxy150489  * _info - module information
3873526Sxy150489  */
3883526Sxy150489 int
3893526Sxy150489 _info(struct modinfo *modinfop)
3903526Sxy150489 {
3913526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3923526Sxy150489 }
3933526Sxy150489 
3943526Sxy150489 /*
3954919Sxy150489  * e1000g_attach - driver attach
3964919Sxy150489  *
3974919Sxy150489  * This function is the device-specific initialization entry
3984919Sxy150489  * point. This entry point is required and must be written.
3994919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
4004919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
4014919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
4024919Sxy150489  * available for use by the driver.
4034919Sxy150489  *
4044919Sxy150489  * The attach() function will be called once for each instance
4054919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
4064919Sxy150489  * Until attach() succeeds, the only driver entry points which
4074919Sxy150489  * may be called are open(9E) and getinfo(9E).
4083526Sxy150489  */
4093526Sxy150489 static int
4104919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
4113526Sxy150489 {
4123526Sxy150489 	struct e1000g *Adapter;
4133526Sxy150489 	struct e1000_hw *hw;
4144919Sxy150489 	struct e1000g_osdep *osdep;
4153526Sxy150489 	int instance;
4163526Sxy150489 
4173526Sxy150489 	switch (cmd) {
4183526Sxy150489 	default:
4193526Sxy150489 		e1000g_log(NULL, CE_WARN,
4204919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
4213526Sxy150489 		return (DDI_FAILURE);
4223526Sxy150489 
4233526Sxy150489 	case DDI_RESUME:
4243526Sxy150489 		return (e1000g_resume(devinfo));
4253526Sxy150489 
4263526Sxy150489 	case DDI_ATTACH:
4273526Sxy150489 		break;
4283526Sxy150489 	}
4293526Sxy150489 
4303526Sxy150489 	/*
4313526Sxy150489 	 * get device instance number
4323526Sxy150489 	 */
4333526Sxy150489 	instance = ddi_get_instance(devinfo);
4343526Sxy150489 
4353526Sxy150489 	/*
4363526Sxy150489 	 * Allocate soft data structure
4373526Sxy150489 	 */
4383526Sxy150489 	Adapter =
4393526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4403526Sxy150489 
4413526Sxy150489 	Adapter->dip = devinfo;
4424919Sxy150489 	Adapter->instance = instance;
4433526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4443526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4453526Sxy150489 
4464919Sxy150489 	hw = &Adapter->shared;
4474919Sxy150489 	osdep = &Adapter->osdep;
4484919Sxy150489 	hw->back = osdep;
4494919Sxy150489 	osdep->adapter = Adapter;
4504919Sxy150489 
4513526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4523526Sxy150489 
4534919Sxy150489 	/*
4545273Sgl147354 	 * Initialize for fma support
4555273Sgl147354 	 */
4565273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4575273Sgl147354 	    0, 0x0f,
4585273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4595273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4605273Sgl147354 	e1000g_fm_init(Adapter);
4615273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4625273Sgl147354 
4635273Sgl147354 	/*
4644919Sxy150489 	 * PCI Configure
4654919Sxy150489 	 */
4664919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4674919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4684919Sxy150489 		goto attach_fail;
4694919Sxy150489 	}
4704919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4714919Sxy150489 
4724919Sxy150489 	/*
4734919Sxy150489 	 * Setup hardware
4744919Sxy150489 	 */
4754919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4764919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4774919Sxy150489 		goto attach_fail;
4784919Sxy150489 	}
4793526Sxy150489 
4803526Sxy150489 	/*
4813526Sxy150489 	 * Map in the device registers.
4823526Sxy150489 	 */
4834919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4844919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4853526Sxy150489 		goto attach_fail;
4863526Sxy150489 	}
4874919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4883526Sxy150489 
4893526Sxy150489 	/*
4903526Sxy150489 	 * Initialize driver parameters
4913526Sxy150489 	 */
4923526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4933526Sxy150489 		goto attach_fail;
4943526Sxy150489 	}
4954919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4963526Sxy150489 
4975273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4985273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4995273Sgl147354 		goto attach_fail;
5005273Sgl147354 	}
5015273Sgl147354 
5023526Sxy150489 	/*
5033526Sxy150489 	 * Initialize interrupts
5043526Sxy150489 	 */
5053526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
5063526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
5073526Sxy150489 		goto attach_fail;
5083526Sxy150489 	}
5094919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
5103526Sxy150489 
5113526Sxy150489 	/*
5123526Sxy150489 	 * Initialize mutex's for this device.
5133526Sxy150489 	 * Do this before enabling the interrupt handler and
5143526Sxy150489 	 * register the softint to avoid the condition where
5153526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5163526Sxy150489 	 */
5173526Sxy150489 	e1000g_init_locks(Adapter);
5183526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5193526Sxy150489 
5203526Sxy150489 	/*
5213526Sxy150489 	 * Initialize Driver Counters
5223526Sxy150489 	 */
5234919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
5243526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5253526Sxy150489 		goto attach_fail;
5263526Sxy150489 	}
5273526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5283526Sxy150489 
5293526Sxy150489 	/*
5303526Sxy150489 	 * Initialize chip hardware and software structures
5313526Sxy150489 	 */
5323526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5333526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5343526Sxy150489 		goto attach_fail;
5353526Sxy150489 	}
5363526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5373526Sxy150489 
5383526Sxy150489 	/*
5393526Sxy150489 	 * Register the driver to the MAC
5403526Sxy150489 	 */
5413526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5423526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5433526Sxy150489 		goto attach_fail;
5443526Sxy150489 	}
5454919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5463526Sxy150489 
5473526Sxy150489 	/*
5483526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5493526Sxy150489 	 * initialized, enable interrupts.
5503526Sxy150489 	 */
5513526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5523526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5533526Sxy150489 		goto attach_fail;
5543526Sxy150489 	}
5554919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5563526Sxy150489 
5574982Syy150190 	/*
5584982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5594982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5604982Syy150190 	 * supports after driver detached.
5614982Syy150190 	 */
5624982Syy150190 	if (e1000g_force_detach) {
5634982Syy150190 		private_devi_list_t *devi_node;
5644982Syy150190 
5654982Syy150190 		Adapter->priv_dip =
5664982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5674982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5684982Syy150190 		    sizeof (struct dev_info));
5694982Syy150190 
5704982Syy150190 		devi_node =
5714982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5724982Syy150190 
5734982Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
5744982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5754982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5764982Syy150190 		devi_node->next = e1000g_private_devi_list;
5774982Syy150190 		e1000g_private_devi_list = devi_node;
5784982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
5794982Syy150190 	}
5804982Syy150190 
5813526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5823526Sxy150489 
5833526Sxy150489 	return (DDI_SUCCESS);
5843526Sxy150489 
5853526Sxy150489 attach_fail:
5863526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5873526Sxy150489 	return (DDI_FAILURE);
5883526Sxy150489 }
5893526Sxy150489 
5903526Sxy150489 static int
5913526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5923526Sxy150489 {
5934919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5943526Sxy150489 	mac_register_t *mac;
5953526Sxy150489 	int err;
5963526Sxy150489 
5973526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5983526Sxy150489 		return (DDI_FAILURE);
5994919Sxy150489 
6003526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
6013526Sxy150489 	mac->m_driver = Adapter;
6023526Sxy150489 	mac->m_dip = Adapter->dip;
6034919Sxy150489 	mac->m_src_addr = hw->mac.addr;
6043526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
6053526Sxy150489 	mac->m_min_sdu = 0;
6066394Scc210113 	mac->m_max_sdu = Adapter->default_mtu;
6075895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6086512Ssowmini 	mac->m_priv_props = e1000g_priv_props;
6096512Ssowmini 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
6104919Sxy150489 
6113526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6123526Sxy150489 	mac_free(mac);
6134919Sxy150489 
6143526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6153526Sxy150489 }
6163526Sxy150489 
6173526Sxy150489 static int
6184919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
6194919Sxy150489 {
6204919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6214919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6224919Sxy150489 
6234919Sxy150489 	/* Get the device id */
6244919Sxy150489 	hw->vendor_id =
6254919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6264919Sxy150489 	hw->device_id =
6274919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6284919Sxy150489 	hw->revision_id =
6294919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6304919Sxy150489 	hw->subsystem_device_id =
6314919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6324919Sxy150489 	hw->subsystem_vendor_id =
6334919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6344919Sxy150489 
6354919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6364919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6374919Sxy150489 		    "MAC type could not be set properly.");
6384919Sxy150489 		return (DDI_FAILURE);
6394919Sxy150489 	}
6404919Sxy150489 
6414919Sxy150489 	return (DDI_SUCCESS);
6424919Sxy150489 }
6434919Sxy150489 
6444919Sxy150489 static int
6454919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6464919Sxy150489 {
6474919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6484919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6494919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6504919Sxy150489 	off_t mem_size;
6514919Sxy150489 
6527607STed.You@Sun.COM 	/* Get size of adapter register memory */
6537607STed.You@Sun.COM 	if (ddi_dev_regsize(devinfo, ADAPTER_REG_SET, &mem_size) !=
6547607STed.You@Sun.COM 	    DDI_SUCCESS) {
6554919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6564919Sxy150489 		    "ddi_dev_regsize for registers failed");
6574919Sxy150489 		return (DDI_FAILURE);
6584919Sxy150489 	}
6594919Sxy150489 
6607607STed.You@Sun.COM 	/* Map adapter register memory */
6617607STed.You@Sun.COM 	if ((ddi_regs_map_setup(devinfo, ADAPTER_REG_SET,
6624919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6634919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6644919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6654919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6664919Sxy150489 		goto regs_map_fail;
6674919Sxy150489 	}
6684919Sxy150489 
6694919Sxy150489 	/* ICH needs to map flash memory */
6707607STed.You@Sun.COM 	if (hw->mac.type == e1000_ich8lan ||
6717607STed.You@Sun.COM 	    hw->mac.type == e1000_ich9lan ||
6727607STed.You@Sun.COM 	    hw->mac.type == e1000_ich10lan) {
6734919Sxy150489 		/* get flash size */
6744919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6754919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6764919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6774919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6784919Sxy150489 			goto regs_map_fail;
6794919Sxy150489 		}
6804919Sxy150489 
6814919Sxy150489 		/* map flash in */
6824919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6834919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6844919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6854919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6864919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6874919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6884919Sxy150489 			goto regs_map_fail;
6894919Sxy150489 		}
6904919Sxy150489 	}
6914919Sxy150489 
6924919Sxy150489 	return (DDI_SUCCESS);
6934919Sxy150489 
6944919Sxy150489 regs_map_fail:
6954919Sxy150489 	if (osdep->reg_handle != NULL)
6964919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
6974919Sxy150489 
6984919Sxy150489 	return (DDI_FAILURE);
6994919Sxy150489 }
7004919Sxy150489 
7014919Sxy150489 static int
7023526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
7033526Sxy150489 {
7043526Sxy150489 	struct e1000_hw *hw;
7054919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
7063526Sxy150489 
7074919Sxy150489 	hw = &Adapter->shared;
7084919Sxy150489 
7094919Sxy150489 	/* Set MAC type and initialize hardware functions */
7104919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7114919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7124919Sxy150489 		    "Could not setup hardware functions");
7133526Sxy150489 		return (DDI_FAILURE);
7143526Sxy150489 	}
7153526Sxy150489 
7164919Sxy150489 	/* Get bus information */
7174919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7184919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7194919Sxy150489 		    "Could not get bus information");
7204919Sxy150489 		return (DDI_FAILURE);
7213526Sxy150489 	}
7223526Sxy150489 
7233526Sxy150489 	/* get mem_base addr */
7244919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7254919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7263526Sxy150489 
7273526Sxy150489 	/* get io_base addr */
7284919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7294919Sxy150489 		if (bar64) {
7303526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7314919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7324919Sxy150489 			    PCI_CONF_BASE4);
7333526Sxy150489 		} else {
7343526Sxy150489 			/* normal 32-bit BAR mode */
7354919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7364919Sxy150489 			    PCI_CONF_BASE2);
7373526Sxy150489 		}
7383526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7393526Sxy150489 	} else {
7403526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7413526Sxy150489 		hw->io_base = 0x0;
7423526Sxy150489 	}
7433526Sxy150489 
7444919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7454919Sxy150489 
7464919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7474919Sxy150489 
7486735Scc210113 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
7496735Scc210113 	hw->phy.autoneg_wait_to_complete = B_FALSE;
7503526Sxy150489 
7513526Sxy150489 	/* Adaptive IFS related changes */
7524919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7534919Sxy150489 
7544919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7554919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7564919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7574919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7584919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7594919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7604919Sxy150489 
7614919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7624919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7633526Sxy150489 
7644608Syy150190 #ifdef __sparc
7654608Syy150190 	Adapter->strip_crc = B_TRUE;
7664608Syy150190 #else
7674608Syy150190 	Adapter->strip_crc = B_FALSE;
7684608Syy150190 #endif
7694608Syy150190 
7703526Sxy150489 	/* Get conf file properties */
7714919Sxy150489 	e1000g_get_conf(Adapter);
7724919Sxy150489 
7734919Sxy150489 	/* Get speed/duplex settings in conf file */
7744919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7754919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7763526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7773526Sxy150489 
7784919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7793526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7803526Sxy150489 
7813526Sxy150489 	/* Set Rx/Tx buffer size */
7826394Scc210113 	e1000g_set_bufsize(Adapter);
7834919Sxy150489 
7844919Sxy150489 	/* Master Latency Timer */
7854919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
7864919Sxy150489 
7873526Sxy150489 	/* copper options */
7886735Scc210113 	if (hw->phy.media_type == e1000_media_type_copper) {
7894919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
7904919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
7914919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
7923526Sxy150489 	}
7933526Sxy150489 
7944919Sxy150489 	/* The initial link state should be "unknown" */
7954061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
7964061Sxy150489 
7975882Syy150190 	/* Initialize rx parameters */
7985882Syy150190 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
7995882Syy150190 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
8005882Syy150190 
8014919Sxy150489 	/* Initialize tx parameters */
8024919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
8034919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
8045882Syy150190 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
8055882Syy150190 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
8064919Sxy150489 
8074919Sxy150489 	/* Initialize rx parameters */
8084919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8094919Sxy150489 
8103526Sxy150489 	return (DDI_SUCCESS);
8113526Sxy150489 }
8123526Sxy150489 
8136394Scc210113 static void
8146394Scc210113 e1000g_set_bufsize(struct e1000g *Adapter)
8156394Scc210113 {
8166394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
8176394Scc210113 	uint64_t rx_size;
8186394Scc210113 	uint64_t tx_size;
8196394Scc210113 
8208073SMin.Xu@Sun.COM 	dev_info_t *devinfo = Adapter->dip;
8216394Scc210113 #ifdef __sparc
8226394Scc210113 	ulong_t iommu_pagesize;
8238073SMin.Xu@Sun.COM #endif
8246394Scc210113 	/* Get the system page size */
8256394Scc210113 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
8268073SMin.Xu@Sun.COM 
8278073SMin.Xu@Sun.COM #ifdef __sparc
8286394Scc210113 	iommu_pagesize = dvma_pagesize(devinfo);
8296394Scc210113 	if (iommu_pagesize != 0) {
8306394Scc210113 		if (Adapter->sys_page_sz == iommu_pagesize) {
8316394Scc210113 			if (iommu_pagesize > 0x4000)
8326394Scc210113 				Adapter->sys_page_sz = 0x4000;
8336394Scc210113 		} else {
8346394Scc210113 			if (Adapter->sys_page_sz > iommu_pagesize)
8356394Scc210113 				Adapter->sys_page_sz = iommu_pagesize;
8366394Scc210113 		}
8376394Scc210113 	}
8386986Smx205022 	if (Adapter->lso_enable) {
8396986Smx205022 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
8406986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8416986Smx205022 	} else {
8426986Smx205022 		Adapter->dvma_page_num = Adapter->max_frame_size /
8436986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8446986Smx205022 	}
8456394Scc210113 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
8466394Scc210113 #endif
8476394Scc210113 
8486735Scc210113 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
8496735Scc210113 
850*8178SChenlu.Chen@Sun.COM 	if ((mac->type == e1000_82545) ||
851*8178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546) ||
852*8178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546_rev_3)) {
8536394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
854*8178SChenlu.Chen@Sun.COM 	} else {
855*8178SChenlu.Chen@Sun.COM 		rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
856*8178SChenlu.Chen@Sun.COM 		if ((rx_size > FRAME_SIZE_UPTO_2K) &&
857*8178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_4K))
858*8178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
859*8178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
860*8178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_8K))
861*8178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
862*8178SChenlu.Chen@Sun.COM 		else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
863*8178SChenlu.Chen@Sun.COM 		    (rx_size <= FRAME_SIZE_UPTO_16K))
864*8178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
865*8178SChenlu.Chen@Sun.COM 		else
866*8178SChenlu.Chen@Sun.COM 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
867*8178SChenlu.Chen@Sun.COM 	}
8686394Scc210113 
8696735Scc210113 	tx_size = Adapter->max_frame_size;
8706394Scc210113 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
8716394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
8726394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
8736394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_8K))
8746394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
8756394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
8766394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_16K))
8776394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
8786394Scc210113 	else
8796394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
8806394Scc210113 
8816394Scc210113 	/*
8826394Scc210113 	 * For Wiseman adapters we have an requirement of having receive
8836394Scc210113 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8846394Scc210113 	 * require this and forcing it for all hardwares will have
8856394Scc210113 	 * performance implications, I am making it applicable only for
8866394Scc210113 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8876394Scc210113 	 * it is okay to have normal frames...but it does involve a
8886394Scc210113 	 * potential risk where we may loose data if buffer is not
8896394Scc210113 	 * aligned...so all wiseman boards to have 256 byte aligned
8906394Scc210113 	 * buffers
8916394Scc210113 	 */
8926394Scc210113 	if (mac->type < e1000_82543)
8936394Scc210113 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
894*8178SChenlu.Chen@Sun.COM 	else if ((mac->type == e1000_82545) ||
895*8178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546) ||
896*8178SChenlu.Chen@Sun.COM 	    (mac->type == e1000_82546_rev_3))
897*8178SChenlu.Chen@Sun.COM 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE_82546;
8986394Scc210113 	else
8996394Scc210113 		Adapter->rx_buf_align = 1;
9006394Scc210113 }
9016394Scc210113 
9023526Sxy150489 /*
9034919Sxy150489  * e1000g_detach - driver detach
9044919Sxy150489  *
9054919Sxy150489  * The detach() function is the complement of the attach routine.
9064919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
9074919Sxy150489  * state  associated  with  a  given  instance of a device node
9084919Sxy150489  * prior to the removal of that instance from the system.
9094919Sxy150489  *
9104919Sxy150489  * The detach() function will be called once for each  instance
9114919Sxy150489  * of the device for which there has been a successful attach()
9124919Sxy150489  * once there are no longer  any  opens  on  the  device.
9134919Sxy150489  *
9144919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
9154919Sxy150489  * driver are freed.
9163526Sxy150489  */
9173526Sxy150489 static int
9184919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
9193526Sxy150489 {
9203526Sxy150489 	struct e1000g *Adapter;
9214982Syy150190 	boolean_t rx_drain;
9223526Sxy150489 
9233526Sxy150489 	switch (cmd) {
9243526Sxy150489 	default:
9253526Sxy150489 		return (DDI_FAILURE);
9263526Sxy150489 
9273526Sxy150489 	case DDI_SUSPEND:
9283526Sxy150489 		return (e1000g_suspend(devinfo));
9293526Sxy150489 
9303526Sxy150489 	case DDI_DETACH:
9313526Sxy150489 		break;
9323526Sxy150489 	}
9333526Sxy150489 
9343526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
9353526Sxy150489 	if (Adapter == NULL)
9363526Sxy150489 		return (DDI_FAILURE);
9373526Sxy150489 
9384919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9394919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
9404919Sxy150489 		return (DDI_FAILURE);
9414919Sxy150489 	}
9424919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
9434919Sxy150489 
9445273Sgl147354 
9455273Sgl147354 	if (Adapter->chip_state != E1000G_STOP)
9464919Sxy150489 		e1000g_stop(Adapter, B_TRUE);
9473526Sxy150489 
9484982Syy150190 	rx_drain = e1000g_rx_drain(Adapter);
9494982Syy150190 
9504982Syy150190 	/*
9514982Syy150190 	 * If e1000g_force_detach is enabled, driver detach is safe.
9524982Syy150190 	 * We will let e1000g_free_priv_devi_node routine determine
9534982Syy150190 	 * whether we need to free the priv_dip entry for current
9544982Syy150190 	 * driver instance.
9554982Syy150190 	 */
9564982Syy150190 	if (e1000g_force_detach) {
9574982Syy150190 		e1000g_free_priv_devi_node(Adapter, rx_drain);
9584982Syy150190 	} else {
9594982Syy150190 		if (!rx_drain)
9603526Sxy150489 			return (DDI_FAILURE);
9613526Sxy150489 	}
9623526Sxy150489 
9633526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9643526Sxy150489 
9653526Sxy150489 	return (DDI_SUCCESS);
9663526Sxy150489 }
9673526Sxy150489 
9684982Syy150190 /*
9694982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
9704982Syy150190  *
9714982Syy150190  * If free_flag is true, that indicates the upper layer is not holding
9724982Syy150190  * the rx buffers, we could free the priv_dip entry safely.
9734982Syy150190  *
9744982Syy150190  * Otherwise, we have to keep this entry even after driver detached,
9754982Syy150190  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
9764982Syy150190  * so that driver could free it while all of rx buffers are returned
9774982Syy150190  * by upper layer later.
9784982Syy150190  */
9794982Syy150190 static void
9804982Syy150190 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
9814982Syy150190 {
9824982Syy150190 	private_devi_list_t *devi_node, *devi_del;
9834982Syy150190 
9844982Syy150190 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
9854982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
9864982Syy150190 	ASSERT(Adapter->priv_dip != NULL);
9874982Syy150190 
9884982Syy150190 	devi_node = e1000g_private_devi_list;
9894982Syy150190 	if (devi_node->priv_dip == Adapter->priv_dip) {
9904982Syy150190 		if (free_flag) {
9914982Syy150190 			e1000g_private_devi_list =
9924982Syy150190 			    devi_node->next;
9934982Syy150190 			kmem_free(devi_node->priv_dip,
9944982Syy150190 			    sizeof (struct dev_info));
9954982Syy150190 			kmem_free(devi_node,
9964982Syy150190 			    sizeof (private_devi_list_t));
9974982Syy150190 		} else {
9984982Syy150190 			ASSERT(e1000g_mblks_pending != 0);
9994982Syy150190 			devi_node->flag =
10004982Syy150190 			    E1000G_PRIV_DEVI_DETACH;
10014982Syy150190 		}
10024982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
10034982Syy150190 		return;
10044982Syy150190 	}
10054982Syy150190 
10064982Syy150190 	devi_node = e1000g_private_devi_list;
10074982Syy150190 	while (devi_node->next != NULL) {
10084982Syy150190 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
10094982Syy150190 			if (free_flag) {
10104982Syy150190 				devi_del = devi_node->next;
10114982Syy150190 				devi_node->next = devi_del->next;
10124982Syy150190 				kmem_free(devi_del->priv_dip,
10134982Syy150190 				    sizeof (struct dev_info));
10144982Syy150190 				kmem_free(devi_del,
10154982Syy150190 				    sizeof (private_devi_list_t));
10164982Syy150190 			} else {
10174982Syy150190 				ASSERT(e1000g_mblks_pending != 0);
10184982Syy150190 				devi_node->next->flag =
10194982Syy150190 				    E1000G_PRIV_DEVI_DETACH;
10204982Syy150190 			}
10214982Syy150190 			break;
10224982Syy150190 		}
10234982Syy150190 		devi_node = devi_node->next;
10244982Syy150190 	}
10254982Syy150190 	rw_exit(&e1000g_rx_detach_lock);
10264982Syy150190 }
10274982Syy150190 
10283526Sxy150489 static void
10293526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
10303526Sxy150489 {
10317133Scc210113 	int result;
10327133Scc210113 
10334919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
10343526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
10353526Sxy150489 	}
10363526Sxy150489 
10374919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
10383526Sxy150489 		(void) mac_unregister(Adapter->mh);
10393526Sxy150489 	}
10403526Sxy150489 
10414919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
10423526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
10433526Sxy150489 	}
10443526Sxy150489 
10454919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10463526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10473526Sxy150489 	}
10483526Sxy150489 
10493526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10503526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10513526Sxy150489 	}
10523526Sxy150489 
10533526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10544919Sxy150489 		stop_link_timer(Adapter);
10557133Scc210113 
10567133Scc210113 		mutex_enter(&e1000g_nvm_lock);
10577133Scc210113 		result = e1000_reset_hw(&Adapter->shared);
10587133Scc210113 		mutex_exit(&e1000g_nvm_lock);
10597133Scc210113 
10607133Scc210113 		if (result != E1000_SUCCESS) {
10615273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10625273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10635273Sgl147354 		}
10643526Sxy150489 	}
10653526Sxy150489 
10664919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10674919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10684919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10694919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10704919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10713526Sxy150489 	}
10723526Sxy150489 
10734919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10744919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10754919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10763526Sxy150489 	}
10773526Sxy150489 
10783526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10793526Sxy150489 		e1000g_destroy_locks(Adapter);
10803526Sxy150489 	}
10813526Sxy150489 
10825273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10835273Sgl147354 		e1000g_fm_fini(Adapter);
10845273Sgl147354 	}
10855273Sgl147354 
10864919Sxy150489 	e1000_remove_device(&Adapter->shared);
10874919Sxy150489 
10883526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
10893526Sxy150489 
10903526Sxy150489 	/*
10913526Sxy150489 	 * Another hotplug spec requirement,
10923526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
10933526Sxy150489 	 */
10943526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
10953526Sxy150489 }
10963526Sxy150489 
10973526Sxy150489 static void
10983526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
10993526Sxy150489 {
11003526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11013526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11023526Sxy150489 
11033526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
11043526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11054919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
11063526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11074919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
11083526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11093526Sxy150489 
11103526Sxy150489 	tx_ring = Adapter->tx_ring;
11113526Sxy150489 
11123526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
11133526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11143526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
11153526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11163526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
11173526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11183526Sxy150489 
11193526Sxy150489 	rx_ring = Adapter->rx_ring;
11203526Sxy150489 
11217436STed.You@Sun.COM 	mutex_init(&rx_ring->rx_lock, NULL,
11227436STed.You@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11233526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
11243526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11253526Sxy150489 }
11263526Sxy150489 
11273526Sxy150489 static void
11283526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
11293526Sxy150489 {
11303526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11313526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11323526Sxy150489 
11333526Sxy150489 	tx_ring = Adapter->tx_ring;
11343526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
11353526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
11363526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
11373526Sxy150489 
11383526Sxy150489 	rx_ring = Adapter->rx_ring;
11397436STed.You@Sun.COM 	mutex_destroy(&rx_ring->rx_lock);
11403526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
11413526Sxy150489 
11424919Sxy150489 	mutex_destroy(&Adapter->link_lock);
11434919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
11443526Sxy150489 	rw_destroy(&Adapter->chip_lock);
11453526Sxy150489 }
11463526Sxy150489 
11473526Sxy150489 static int
11483526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11493526Sxy150489 {
11503526Sxy150489 	struct e1000g *Adapter;
11513526Sxy150489 
11523526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11533526Sxy150489 	if (Adapter == NULL)
11543526Sxy150489 		return (DDI_FAILURE);
11553526Sxy150489 
11564919Sxy150489 	if (e1000g_start(Adapter, B_TRUE))
11573526Sxy150489 		return (DDI_FAILURE);
11583526Sxy150489 
11593526Sxy150489 	return (DDI_SUCCESS);
11603526Sxy150489 }
11613526Sxy150489 
11623526Sxy150489 static int
11633526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11643526Sxy150489 {
11653526Sxy150489 	struct e1000g *Adapter;
11663526Sxy150489 
11673526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11683526Sxy150489 	if (Adapter == NULL)
11693526Sxy150489 		return (DDI_FAILURE);
11703526Sxy150489 
11714919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
11723526Sxy150489 
11733526Sxy150489 	return (DDI_SUCCESS);
11743526Sxy150489 }
11753526Sxy150489 
11763526Sxy150489 static int
11773526Sxy150489 e1000g_init(struct e1000g *Adapter)
11783526Sxy150489 {
11793526Sxy150489 	uint32_t pba;
11804919Sxy150489 	uint32_t high_water;
11813526Sxy150489 	struct e1000_hw *hw;
11824061Sxy150489 	clock_t link_timeout;
11837133Scc210113 	int result;
11843526Sxy150489 
11854919Sxy150489 	hw = &Adapter->shared;
11863526Sxy150489 
11873526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11883526Sxy150489 
11893526Sxy150489 	/*
11903526Sxy150489 	 * reset to put the hardware in a known state
11913526Sxy150489 	 * before we try to do anything with the eeprom
11923526Sxy150489 	 */
11937133Scc210113 	mutex_enter(&e1000g_nvm_lock);
11947133Scc210113 	result = e1000_reset_hw(hw);
11957133Scc210113 	mutex_exit(&e1000g_nvm_lock);
11967133Scc210113 
11977133Scc210113 	if (result != E1000_SUCCESS) {
11985273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11995273Sgl147354 		goto init_fail;
12005273Sgl147354 	}
12013526Sxy150489 
12027133Scc210113 	mutex_enter(&e1000g_nvm_lock);
12037133Scc210113 	result = e1000_validate_nvm_checksum(hw);
12047133Scc210113 	if (result < E1000_SUCCESS) {
12054061Sxy150489 		/*
12064061Sxy150489 		 * Some PCI-E parts fail the first check due to
12074061Sxy150489 		 * the link being in sleep state.  Call it again,
12084061Sxy150489 		 * if it fails a second time its a real issue.
12094061Sxy150489 		 */
12107133Scc210113 		result = e1000_validate_nvm_checksum(hw);
12117133Scc210113 	}
12127133Scc210113 	mutex_exit(&e1000g_nvm_lock);
12137133Scc210113 
12147133Scc210113 	if (result < E1000_SUCCESS) {
12157133Scc210113 		e1000g_log(Adapter, CE_WARN,
12167133Scc210113 		    "Invalid NVM checksum. Please contact "
12177133Scc210113 		    "the vendor to update the NVM.");
12187133Scc210113 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12197133Scc210113 		goto init_fail;
12207133Scc210113 	}
12217133Scc210113 
12227133Scc210113 	result = 0;
12233526Sxy150489 #ifdef __sparc
12243526Sxy150489 	/*
12257607STed.You@Sun.COM 	 * First, we try to get the local ethernet address from OBP. If
12267133Scc210113 	 * failed, then we get it from the EEPROM of NIC card.
12273526Sxy150489 	 */
12287133Scc210113 	result = e1000g_find_mac_address(Adapter);
12297133Scc210113 #endif
12303526Sxy150489 	/* Get the local ethernet address. */
12317133Scc210113 	if (!result) {
12327133Scc210113 		mutex_enter(&e1000g_nvm_lock);
12337140Scc210113 		result = e1000_read_mac_addr(hw);
12347133Scc210113 		mutex_exit(&e1000g_nvm_lock);
12357133Scc210113 	}
12367133Scc210113 
12377133Scc210113 	if (result < E1000_SUCCESS) {
12383526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
12395273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12403526Sxy150489 		goto init_fail;
12413526Sxy150489 	}
12423526Sxy150489 
12433526Sxy150489 	/* check for valid mac address */
12444919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12453526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
12465273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12473526Sxy150489 		goto init_fail;
12483526Sxy150489 	}
12493526Sxy150489 
12504919Sxy150489 	/* Set LAA state for 82571 chipset */
12514919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
12523526Sxy150489 
12533526Sxy150489 	/* Master Latency Timer implementation */
12544919Sxy150489 	if (Adapter->master_latency_timer) {
12554919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
12564919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
12573526Sxy150489 	}
12583526Sxy150489 
12594919Sxy150489 	if (hw->mac.type < e1000_82547) {
12603526Sxy150489 		/*
12613526Sxy150489 		 * Total FIFO is 64K
12623526Sxy150489 		 */
12636735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12643526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
12653526Sxy150489 		else
12663526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
12677140Scc210113 	} else if ((hw->mac.type == e1000_82571) ||
12687140Scc210113 	    (hw->mac.type == e1000_82572) ||
12697140Scc210113 	    (hw->mac.type == e1000_80003es2lan)) {
12703526Sxy150489 		/*
12713526Sxy150489 		 * Total FIFO is 48K
12723526Sxy150489 		 */
12736735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12743526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
12753526Sxy150489 		else
12763526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
12777607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82573) {
12787607STed.You@Sun.COM 		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
12797607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_82574) {
12807607STed.You@Sun.COM 		/* Keep adapter default: 20K for Rx, 20K for Tx */
12817607STed.You@Sun.COM 		pba = E1000_READ_REG(hw, E1000_PBA);
12824919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
12833526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
12844919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
12857607STed.You@Sun.COM 		pba = E1000_PBA_10K;
12867607STed.You@Sun.COM 	} else if (hw->mac.type == e1000_ich10lan) {
12877607STed.You@Sun.COM 		pba = E1000_PBA_10K;
12883526Sxy150489 	} else {
12893526Sxy150489 		/*
12903526Sxy150489 		 * Total FIFO is 40K
12913526Sxy150489 		 */
12926735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12933526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
12943526Sxy150489 		else
12953526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
12963526Sxy150489 	}
12974919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
12983526Sxy150489 
12993526Sxy150489 	/*
13003526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
13013526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
13023526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
13033526Sxy150489 	 * file.
13043526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
13053526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
13063526Sxy150489 	 * high-water mark.
13074919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
13084919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
13094919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
13104919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
13114919Sxy150489 	 * Rx FIFO size minus one full frame.
13123526Sxy150489 	 */
13134919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
13147607STed.You@Sun.COM 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_ich9lan ||
13157607STed.You@Sun.COM 	    hw->mac.type == e1000_ich10lan) ?
13164919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
13176735Scc210113 	    ((pba << 10) - Adapter->max_frame_size)));
13186735Scc210113 
13196735Scc210113 	hw->fc.high_water = high_water & 0xFFF8;
13206735Scc210113 	hw->fc.low_water = hw->fc.high_water - 8;
13214919Sxy150489 
13224919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
13236735Scc210113 		hw->fc.pause_time = 0xFFFF;
13244919Sxy150489 	else
13256735Scc210113 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
13266735Scc210113 	hw->fc.send_xon = B_TRUE;
13273526Sxy150489 
13283526Sxy150489 	/*
13293526Sxy150489 	 * Reset the adapter hardware the second time.
13303526Sxy150489 	 */
13317133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13327133Scc210113 	result = e1000_reset_hw(hw);
13337133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13347133Scc210113 
13357133Scc210113 	if (result != E1000_SUCCESS) {
13365273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13375273Sgl147354 		goto init_fail;
13385273Sgl147354 	}
13393526Sxy150489 
13403526Sxy150489 	/* disable wakeup control by default */
13414919Sxy150489 	if (hw->mac.type >= e1000_82544)
13424919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
13433526Sxy150489 
1344*8178SChenlu.Chen@Sun.COM 	/*
1345*8178SChenlu.Chen@Sun.COM 	 * MWI should be disabled on 82546.
1346*8178SChenlu.Chen@Sun.COM 	 */
1347*8178SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82546)
1348*8178SChenlu.Chen@Sun.COM 		e1000_pci_clear_mwi(hw);
1349*8178SChenlu.Chen@Sun.COM 	else
1350*8178SChenlu.Chen@Sun.COM 		e1000_pci_set_mwi(hw);
13513526Sxy150489 
13523526Sxy150489 	/*
13533526Sxy150489 	 * Configure/Initialize hardware
13543526Sxy150489 	 */
13557133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13567133Scc210113 	result = e1000_init_hw(hw);
13577133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13587133Scc210113 
13597133Scc210113 	if (result < E1000_SUCCESS) {
13603526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
13615273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13623526Sxy150489 		goto init_fail;
13633526Sxy150489 	}
13643526Sxy150489 
13657436STed.You@Sun.COM 	/*
13667436STed.You@Sun.COM 	 * Restore LED settings to the default from EEPROM
13677436STed.You@Sun.COM 	 * to meet the standard for Sun platforms.
13687436STed.You@Sun.COM 	 */
13697436STed.You@Sun.COM 	(void) e1000_cleanup_led(hw);
13707436STed.You@Sun.COM 
13713526Sxy150489 	/* Disable Smart Power Down */
13723526Sxy150489 	phy_spd_state(hw, B_FALSE);
13733526Sxy150489 
13745082Syy150190 	/* Make sure driver has control */
13755082Syy150190 	e1000g_get_driver_control(hw);
13765082Syy150190 
13773526Sxy150489 	/*
13783526Sxy150489 	 * Initialize unicast addresses.
13793526Sxy150489 	 */
13803526Sxy150489 	e1000g_init_unicst(Adapter);
13813526Sxy150489 
13823526Sxy150489 	/*
13833526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
13843526Sxy150489 	 * completes  Multicast table will be set
13853526Sxy150489 	 */
13864919Sxy150489 	e1000g_setup_multicast(Adapter);
13874919Sxy150489 	msec_delay(5);
13883526Sxy150489 
13893526Sxy150489 	/*
13903526Sxy150489 	 * Implement Adaptive IFS
13913526Sxy150489 	 */
13923526Sxy150489 	e1000_reset_adaptive(hw);
13933526Sxy150489 
13943526Sxy150489 	/* Setup Interrupt Throttling Register */
13955882Syy150190 	if (hw->mac.type >= e1000_82540) {
13965882Syy150190 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
13975882Syy150190 	} else
13985882Syy150190 		Adapter->intr_adaptive = B_FALSE;
13993526Sxy150489 
14004061Sxy150489 	/* Start the timer for link setup */
14014919Sxy150489 	if (hw->mac.autoneg)
14024919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
14034061Sxy150489 	else
14044919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
14054919Sxy150489 
14064919Sxy150489 	mutex_enter(&Adapter->link_lock);
14076735Scc210113 	if (hw->phy.autoneg_wait_to_complete) {
14084061Sxy150489 		Adapter->link_complete = B_TRUE;
14093526Sxy150489 	} else {
14104061Sxy150489 		Adapter->link_complete = B_FALSE;
14114061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
14124061Sxy150489 		    (void *)Adapter, link_timeout);
14133526Sxy150489 	}
14144919Sxy150489 	mutex_exit(&Adapter->link_lock);
14153526Sxy150489 
14163526Sxy150489 	/* Enable PCI-Ex master */
14174919Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
14183526Sxy150489 		e1000_enable_pciex_master(hw);
14193526Sxy150489 	}
14203526Sxy150489 
14215082Syy150190 	/* Save the state of the phy */
14225082Syy150190 	e1000g_get_phy_state(Adapter);
14235082Syy150190 
14246512Ssowmini 	e1000g_param_sync(Adapter);
14256512Ssowmini 
14263526Sxy150489 	Adapter->init_count++;
14273526Sxy150489 
14285273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
14295273Sgl147354 		goto init_fail;
14305273Sgl147354 	}
14315273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
14325273Sgl147354 		goto init_fail;
14335273Sgl147354 	}
14345273Sgl147354 
14353526Sxy150489 	rw_exit(&Adapter->chip_lock);
14363526Sxy150489 
14373526Sxy150489 	return (DDI_SUCCESS);
14383526Sxy150489 
14393526Sxy150489 init_fail:
14403526Sxy150489 	rw_exit(&Adapter->chip_lock);
14415273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
14423526Sxy150489 	return (DDI_FAILURE);
14433526Sxy150489 }
14443526Sxy150489 
14453526Sxy150489 /*
14463526Sxy150489  * Check if the link is up
14473526Sxy150489  */
14483526Sxy150489 static boolean_t
14493526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
14503526Sxy150489 {
14513526Sxy150489 	struct e1000_hw *hw;
14523526Sxy150489 	boolean_t link_up;
14533526Sxy150489 
14544919Sxy150489 	hw = &Adapter->shared;
14553526Sxy150489 
14567426SChenliang.Xu@Sun.COM 	(void) e1000_check_for_link(hw);
14573526Sxy150489 
14584919Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
14594919Sxy150489 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
14606735Scc210113 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
14614919Sxy150489 	    (hw->mac.serdes_has_link))) {
14623526Sxy150489 		link_up = B_TRUE;
14633526Sxy150489 	} else {
14643526Sxy150489 		link_up = B_FALSE;
14653526Sxy150489 	}
14663526Sxy150489 
14673526Sxy150489 	return (link_up);
14683526Sxy150489 }
14693526Sxy150489 
14703526Sxy150489 static void
14713526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
14723526Sxy150489 {
14733526Sxy150489 	struct iocblk *iocp;
14743526Sxy150489 	struct e1000g *e1000gp;
14753526Sxy150489 	enum ioc_reply status;
14767426SChenliang.Xu@Sun.COM 
14777426SChenliang.Xu@Sun.COM 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
14783526Sxy150489 	iocp->ioc_error = 0;
14793526Sxy150489 	e1000gp = (struct e1000g *)arg;
14803526Sxy150489 
14813526Sxy150489 	ASSERT(e1000gp);
14823526Sxy150489 	if (e1000gp == NULL) {
14833526Sxy150489 		miocnak(q, mp, 0, EINVAL);
14843526Sxy150489 		return;
14853526Sxy150489 	}
14863526Sxy150489 
14873526Sxy150489 	switch (iocp->ioc_cmd) {
14883526Sxy150489 
14893526Sxy150489 	case LB_GET_INFO_SIZE:
14903526Sxy150489 	case LB_GET_INFO:
14913526Sxy150489 	case LB_GET_MODE:
14923526Sxy150489 	case LB_SET_MODE:
14933526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
14943526Sxy150489 		break;
14953526Sxy150489 
14963526Sxy150489 
14974919Sxy150489 #ifdef E1000G_DEBUG
14983526Sxy150489 	case E1000G_IOC_REG_PEEK:
14993526Sxy150489 	case E1000G_IOC_REG_POKE:
15003526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
15013526Sxy150489 		break;
15023526Sxy150489 	case E1000G_IOC_CHIP_RESET:
15033526Sxy150489 		e1000gp->reset_count++;
15047656SSherry.Moore@Sun.COM 		if (e1000g_reset_adapter(e1000gp))
15053526Sxy150489 			status = IOC_ACK;
15063526Sxy150489 		else
15073526Sxy150489 			status = IOC_INVAL;
15083526Sxy150489 		break;
15094919Sxy150489 #endif
15103526Sxy150489 	default:
15113526Sxy150489 		status = IOC_INVAL;
15123526Sxy150489 		break;
15133526Sxy150489 	}
15143526Sxy150489 
15153526Sxy150489 	/*
15163526Sxy150489 	 * Decide how to reply
15173526Sxy150489 	 */
15183526Sxy150489 	switch (status) {
15193526Sxy150489 	default:
15203526Sxy150489 	case IOC_INVAL:
15213526Sxy150489 		/*
15223526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
15233526Sxy150489 		 */
15243526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
15254349Sxy150489 		    EINVAL : iocp->ioc_error);
15263526Sxy150489 		break;
15273526Sxy150489 
15283526Sxy150489 	case IOC_DONE:
15293526Sxy150489 		/*
15303526Sxy150489 		 * OK, reply already sent
15313526Sxy150489 		 */
15323526Sxy150489 		break;
15333526Sxy150489 
15343526Sxy150489 	case IOC_ACK:
15353526Sxy150489 		/*
15363526Sxy150489 		 * OK, reply with an ACK
15373526Sxy150489 		 */
15383526Sxy150489 		miocack(q, mp, 0, 0);
15393526Sxy150489 		break;
15403526Sxy150489 
15413526Sxy150489 	case IOC_REPLY:
15423526Sxy150489 		/*
15433526Sxy150489 		 * OK, send prepared reply as ACK or NAK
15443526Sxy150489 		 */
15453526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
15464349Sxy150489 		    M_IOCACK : M_IOCNAK;
15473526Sxy150489 		qreply(q, mp);
15483526Sxy150489 		break;
15493526Sxy150489 	}
15503526Sxy150489 }
15513526Sxy150489 
15523526Sxy150489 static int
15533526Sxy150489 e1000g_m_start(void *arg)
15543526Sxy150489 {
15553526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15563526Sxy150489 
15574919Sxy150489 	return (e1000g_start(Adapter, B_TRUE));
15583526Sxy150489 }
15593526Sxy150489 
15603526Sxy150489 static int
15614919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
15623526Sxy150489 {
15634919Sxy150489 	if (global) {
15644919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
15654919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
15664919Sxy150489 			e1000g_log(Adapter, CE_WARN,
15674919Sxy150489 			    "Alloc DMA resources failed");
15684919Sxy150489 			return (ENOTACTIVE);
15694919Sxy150489 		}
15704919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
15714919Sxy150489 	}
15724919Sxy150489 
15733526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
15743526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
15753526Sxy150489 			e1000g_log(Adapter, CE_WARN,
15763526Sxy150489 			    "Adapter initialization failed");
15774919Sxy150489 			if (global)
15784919Sxy150489 				e1000g_release_dma_resources(Adapter);
15793526Sxy150489 			return (ENOTACTIVE);
15803526Sxy150489 		}
15813526Sxy150489 	}
15823526Sxy150489 
15833526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15843526Sxy150489 
15854919Sxy150489 	/* Setup and initialize the transmit structures */
15864919Sxy150489 	e1000g_tx_setup(Adapter);
15874919Sxy150489 	msec_delay(5);
15884919Sxy150489 
15894919Sxy150489 	/* Setup and initialize the receive structures */
15904919Sxy150489 	e1000g_rx_setup(Adapter);
15914919Sxy150489 	msec_delay(5);
15924919Sxy150489 
15937722SShuguo.Yang@Sun.COM 	/* Restore the e1000g promiscuous mode */
15947722SShuguo.Yang@Sun.COM 	e1000g_restore_promisc(Adapter);
15957722SShuguo.Yang@Sun.COM 
15964919Sxy150489 	e1000g_mask_interrupt(Adapter);
15973526Sxy150489 
15985273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
15995273Sgl147354 		rw_exit(&Adapter->chip_lock);
16005273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16015273Sgl147354 		return (ENOTACTIVE);
16025273Sgl147354 	}
16035273Sgl147354 
16045273Sgl147354 	Adapter->chip_state = E1000G_START;
16053526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
16063526Sxy150489 
16073526Sxy150489 	rw_exit(&Adapter->chip_lock);
16083526Sxy150489 
16094919Sxy150489 	/* Enable and start the watchdog timer */
16104919Sxy150489 	enable_watchdog_timer(Adapter);
16114919Sxy150489 
16123526Sxy150489 	return (0);
16133526Sxy150489 }
16143526Sxy150489 
16153526Sxy150489 static void
16163526Sxy150489 e1000g_m_stop(void *arg)
16173526Sxy150489 {
16183526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
16193526Sxy150489 
16204919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
16213526Sxy150489 }
16223526Sxy150489 
16233526Sxy150489 static void
16244919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
16253526Sxy150489 {
16267133Scc210113 	int result;
16277133Scc210113 
16283526Sxy150489 	/* Set stop flags */
16293526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16303526Sxy150489 
16315273Sgl147354 	Adapter->chip_state = E1000G_STOP;
16323526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
16333526Sxy150489 
16343526Sxy150489 	rw_exit(&Adapter->chip_lock);
16353526Sxy150489 
16363526Sxy150489 	/* Drain tx sessions */
16373526Sxy150489 	(void) e1000g_tx_drain(Adapter);
16383526Sxy150489 
16394919Sxy150489 	/* Disable and stop all the timers */
16404919Sxy150489 	disable_watchdog_timer(Adapter);
16414919Sxy150489 	stop_link_timer(Adapter);
16424919Sxy150489 	stop_82547_timer(Adapter->tx_ring);
16434061Sxy150489 
16443526Sxy150489 	/* Stop the chip and release pending resources */
16453526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16463526Sxy150489 
16477607STed.You@Sun.COM 	/* Tell firmware driver is no longer in control */
16487607STed.You@Sun.COM 	e1000g_release_driver_control(&Adapter->shared);
16497607STed.You@Sun.COM 
16504919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
16517133Scc210113 
16527133Scc210113 	mutex_enter(&e1000g_nvm_lock);
16537133Scc210113 	result = e1000_reset_hw(&Adapter->shared);
16547133Scc210113 	mutex_exit(&e1000g_nvm_lock);
16557133Scc210113 
16567133Scc210113 	if (result != E1000_SUCCESS) {
16575273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
16585273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16595273Sgl147354 	}
16603526Sxy150489 
16613526Sxy150489 	/* Release resources still held by the TX descriptors */
16624919Sxy150489 	e1000g_tx_clean(Adapter);
16634061Sxy150489 
16645273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
16655273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16665273Sgl147354 
16674061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
16684919Sxy150489 	e1000g_rx_clean(Adapter);
16694061Sxy150489 
16704061Sxy150489 	rw_exit(&Adapter->chip_lock);
16714919Sxy150489 
16724919Sxy150489 	if (global)
16734919Sxy150489 		e1000g_release_dma_resources(Adapter);
16744061Sxy150489 }
16754061Sxy150489 
16764061Sxy150489 static void
16774919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
16784919Sxy150489 {
16794919Sxy150489 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
16804919Sxy150489 
16814919Sxy150489 	if (rx_ring->rx_mblk != NULL) {
16824919Sxy150489 		freemsg(rx_ring->rx_mblk);
16834919Sxy150489 		rx_ring->rx_mblk = NULL;
16844919Sxy150489 		rx_ring->rx_mblk_tail = NULL;
16854919Sxy150489 		rx_ring->rx_mblk_len = 0;
16864919Sxy150489 	}
16874919Sxy150489 }
16884919Sxy150489 
16894919Sxy150489 static void
16904919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
16914061Sxy150489 {
16924061Sxy150489 	e1000g_tx_ring_t *tx_ring;
16934919Sxy150489 	p_tx_sw_packet_t packet;
16944061Sxy150489 	mblk_t *mp;
16954061Sxy150489 	mblk_t *nmp;
16964061Sxy150489 	uint32_t packet_count;
16974061Sxy150489 
16984061Sxy150489 	tx_ring = Adapter->tx_ring;
16994061Sxy150489 
17003526Sxy150489 	/*
17013526Sxy150489 	 * Here we don't need to protect the lists using
17023526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
17033526Sxy150489 	 * have been protected by the chip_lock.
17043526Sxy150489 	 */
17053526Sxy150489 	mp = NULL;
17063526Sxy150489 	nmp = NULL;
17074061Sxy150489 	packet_count = 0;
17084919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
17093526Sxy150489 	while (packet != NULL) {
17103526Sxy150489 		if (packet->mp != NULL) {
17113526Sxy150489 			/* Assemble the message chain */
17123526Sxy150489 			if (mp == NULL) {
17133526Sxy150489 				mp = packet->mp;
17143526Sxy150489 				nmp = packet->mp;
17153526Sxy150489 			} else {
17163526Sxy150489 				nmp->b_next = packet->mp;
17173526Sxy150489 				nmp = packet->mp;
17183526Sxy150489 			}
17193526Sxy150489 			/* Disconnect the message from the sw packet */
17203526Sxy150489 			packet->mp = NULL;
17213526Sxy150489 		}
17223526Sxy150489 
17234919Sxy150489 		e1000g_free_tx_swpkt(packet);
17244061Sxy150489 		packet_count++;
17253526Sxy150489 
17264919Sxy150489 		packet = (p_tx_sw_packet_t)
17273526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
17283526Sxy150489 	}
17293526Sxy150489 
17305882Syy150190 	if (mp != NULL)
17315882Syy150190 		freemsgchain(mp);
17324061Sxy150489 
17334061Sxy150489 	if (packet_count > 0) {
17344061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
17354061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
17364061Sxy150489 
17374061Sxy150489 		/* Setup TX descriptor pointers */
17384061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
17394061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
17404061Sxy150489 
17414061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
17426735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
17436735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
17443526Sxy150489 	}
17453526Sxy150489 }
17463526Sxy150489 
17473526Sxy150489 static boolean_t
17483526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
17493526Sxy150489 {
17503526Sxy150489 	int i;
17513526Sxy150489 	boolean_t done;
17523526Sxy150489 	e1000g_tx_ring_t *tx_ring;
17533526Sxy150489 
17543526Sxy150489 	tx_ring = Adapter->tx_ring;
17553526Sxy150489 
17563526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
17574919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
17583526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
17593526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
17603526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
17613526Sxy150489 
17623526Sxy150489 		if (done)
17633526Sxy150489 			break;
17643526Sxy150489 
17653526Sxy150489 		msec_delay(1);
17663526Sxy150489 	}
17673526Sxy150489 
17683526Sxy150489 	return (done);
17693526Sxy150489 }
17703526Sxy150489 
17713526Sxy150489 static boolean_t
17723526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
17733526Sxy150489 {
17744919Sxy150489 	e1000g_rx_ring_t *rx_ring;
17754919Sxy150489 	p_rx_sw_packet_t packet;
17763526Sxy150489 	boolean_t done;
17773526Sxy150489 
17784919Sxy150489 	rx_ring = Adapter->rx_ring;
17794919Sxy150489 	done = B_TRUE;
17804919Sxy150489 
17814919Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
17824919Sxy150489 
17834919Sxy150489 	while (rx_ring->pending_list != NULL) {
17844919Sxy150489 		packet = rx_ring->pending_list;
17854919Sxy150489 		rx_ring->pending_list =
17864919Sxy150489 		    rx_ring->pending_list->next;
17874919Sxy150489 
17884919Sxy150489 		if (packet->flag == E1000G_RX_SW_STOP) {
17894919Sxy150489 			packet->flag = E1000G_RX_SW_DETACH;
17904919Sxy150489 			done = B_FALSE;
17914919Sxy150489 		} else {
17924919Sxy150489 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
17934919Sxy150489 			ASSERT(packet->mp == NULL);
17944919Sxy150489 			e1000g_free_rx_sw_packet(packet);
17954919Sxy150489 		}
17964919Sxy150489 	}
17974919Sxy150489 
17984919Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
17993526Sxy150489 
18003526Sxy150489 	return (done);
18013526Sxy150489 }
18023526Sxy150489 
18037656SSherry.Moore@Sun.COM static boolean_t
18047656SSherry.Moore@Sun.COM e1000g_reset_adapter(struct e1000g *Adapter)
18053526Sxy150489 {
18064919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
18074919Sxy150489 
18084919Sxy150489 	if (e1000g_start(Adapter, B_FALSE)) {
18093526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
18103526Sxy150489 		return (B_FALSE);
18113526Sxy150489 	}
18123526Sxy150489 
18133526Sxy150489 	return (B_TRUE);
18143526Sxy150489 }
18153526Sxy150489 
18165273Sgl147354 boolean_t
18175273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
18185273Sgl147354 {
18195273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
18205273Sgl147354 
18215273Sgl147354 	Adapter->init_count = 0;
18225273Sgl147354 
18235273Sgl147354 	if (e1000g_start(Adapter, B_TRUE)) {
18245273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
18255273Sgl147354 		return (B_FALSE);
18265273Sgl147354 	}
18275273Sgl147354 
18285273Sgl147354 	return (B_TRUE);
18295273Sgl147354 }
18305273Sgl147354 
18313526Sxy150489 /*
18324919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
18334919Sxy150489  *
18344919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
18354919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
18364919Sxy150489  * bit is set.
18373526Sxy150489  */
18383526Sxy150489 static uint_t
18393526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
18403526Sxy150489 {
18413526Sxy150489 	struct e1000g *Adapter;
18424919Sxy150489 	uint32_t icr;
18433526Sxy150489 
18447426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
18454919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18464919Sxy150489 
18475273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18485273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18495273Sgl147354 
18504919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
18513526Sxy150489 		/*
18523526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
18533526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18543526Sxy150489 		 * look for work to do.
18553526Sxy150489 		 */
18564919Sxy150489 		e1000g_intr_work(Adapter, icr);
18573526Sxy150489 		return (DDI_INTR_CLAIMED);
18583526Sxy150489 	} else {
18593526Sxy150489 		/*
18603526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
18613526Sxy150489 		 * Don't claim this interrupt, return immediately.
18623526Sxy150489 		 */
18633526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18643526Sxy150489 	}
18653526Sxy150489 }
18663526Sxy150489 
18673526Sxy150489 /*
18684919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
18694919Sxy150489  *
18704919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
18714919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
18724919Sxy150489  * bit is set or not.
18733526Sxy150489  */
18743526Sxy150489 static uint_t
18753526Sxy150489 e1000g_intr(caddr_t arg)
18763526Sxy150489 {
18773526Sxy150489 	struct e1000g *Adapter;
18784919Sxy150489 	uint32_t icr;
18793526Sxy150489 
18807426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
18814919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18824919Sxy150489 
18835273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18845273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18855273Sgl147354 
18864919Sxy150489 	if (icr) {
18873526Sxy150489 		/*
18883526Sxy150489 		 * Any bit was set in ICR:
18893526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18903526Sxy150489 		 * look for work to do.
18913526Sxy150489 		 */
18924919Sxy150489 		e1000g_intr_work(Adapter, icr);
18933526Sxy150489 		return (DDI_INTR_CLAIMED);
18943526Sxy150489 	} else {
18953526Sxy150489 		/*
18963526Sxy150489 		 * No bit was set in ICR:
18973526Sxy150489 		 * Don't claim this interrupt, return immediately.
18983526Sxy150489 		 */
18993526Sxy150489 		return (DDI_INTR_UNCLAIMED);
19003526Sxy150489 	}
19013526Sxy150489 }
19023526Sxy150489 
19033526Sxy150489 /*
19044919Sxy150489  * e1000g_intr_work - actual processing of ISR
19054919Sxy150489  *
19064919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
19074919Sxy150489  * processing routines.
19083526Sxy150489  */
19093526Sxy150489 static void
19104919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
19113526Sxy150489 {
19125882Syy150190 	struct e1000_hw *hw;
19135882Syy150190 	hw = &Adapter->shared;
19145882Syy150190 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
19155882Syy150190 	uint32_t itr;
19165882Syy150190 
19175882Syy150190 	Adapter->rx_pkt_cnt = 0;
19185882Syy150190 	Adapter->tx_pkt_cnt = 0;
19195882Syy150190 
19204919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
19214919Sxy150489 	/*
19225273Sgl147354 	 * Here we need to check the "chip_state" flag within the chip_lock to
19234919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
19244919Sxy150489 	 * being reset.
19254919Sxy150489 	 */
19265273Sgl147354 	if (Adapter->chip_state != E1000G_START) {
19274919Sxy150489 		rw_exit(&Adapter->chip_lock);
19284919Sxy150489 		return;
19294919Sxy150489 	}
19304919Sxy150489 
19314919Sxy150489 	if (icr & E1000_ICR_RXT0) {
19323526Sxy150489 		mblk_t *mp;
19333526Sxy150489 
19346735Scc210113 		mutex_enter(&Adapter->rx_ring->rx_lock);
19354919Sxy150489 		mp = e1000g_receive(Adapter);
19366735Scc210113 		mutex_exit(&Adapter->rx_ring->rx_lock);
19374919Sxy150489 
19384919Sxy150489 		rw_exit(&Adapter->chip_lock);
19394919Sxy150489 
19404919Sxy150489 		if (mp != NULL)
19414919Sxy150489 			mac_rx(Adapter->mh, Adapter->mrh, mp);
19424919Sxy150489 	} else
19434919Sxy150489 		rw_exit(&Adapter->chip_lock);
19443526Sxy150489 
19455882Syy150190 	if (icr & E1000_ICR_TXDW) {
19465882Syy150190 		if (!Adapter->tx_intr_enable)
19475882Syy150190 			e1000g_clear_tx_interrupt(Adapter);
19485882Syy150190 
19495882Syy150190 		/* Recycle the tx descriptors */
19505882Syy150190 		rw_enter(&Adapter->chip_lock, RW_READER);
19517426SChenliang.Xu@Sun.COM 		(void) e1000g_recycle(tx_ring);
19525882Syy150190 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
19535882Syy150190 		rw_exit(&Adapter->chip_lock);
19545882Syy150190 
19555882Syy150190 		/* Schedule the re-transmit */
19565882Syy150190 		if (tx_ring->resched_needed &&
19575882Syy150190 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
19585882Syy150190 			tx_ring->resched_needed = B_FALSE;
19595882Syy150190 			mac_tx_update(Adapter->mh);
19605882Syy150190 			E1000G_STAT(tx_ring->stat_reschedule);
19615882Syy150190 		}
19625882Syy150190 	}
19635882Syy150190 
19645882Syy150190 	if (Adapter->intr_adaptive) {
19655882Syy150190 		itr = e1000g_get_itr(Adapter->rx_pkt_cnt, Adapter->tx_pkt_cnt,
19665882Syy150190 		    Adapter->intr_throttling_rate);
19675882Syy150190 		if (itr) {
19685882Syy150190 			E1000_WRITE_REG(hw, E1000_ITR, itr);
19695882Syy150190 			Adapter->intr_throttling_rate = itr;
19705882Syy150190 		}
19715882Syy150190 	}
19725882Syy150190 
19733526Sxy150489 	/*
19743526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
19753526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
19763526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
19773526Sxy150489 	 * are an indication that cable is not connected.
19783526Sxy150489 	 */
19794919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
19804919Sxy150489 	    (icr & E1000_ICR_LSC) ||
19814919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
19824061Sxy150489 		boolean_t link_changed;
19834061Sxy150489 		timeout_id_t tid = 0;
19843526Sxy150489 
19854919Sxy150489 		stop_watchdog_timer(Adapter);
19864919Sxy150489 
19875082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
19885082Syy150190 
19895082Syy150190 		/*
19905082Syy150190 		 * Because we got a link-status-change interrupt, force
19915082Syy150190 		 * e1000_check_for_link() to look at phy
19925082Syy150190 		 */
19935082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
19945082Syy150190 
19954061Sxy150489 		/* e1000g_link_check takes care of link status change */
19964061Sxy150489 		link_changed = e1000g_link_check(Adapter);
19975082Syy150190 
19985082Syy150190 		/* Get new phy state */
19995082Syy150190 		e1000g_get_phy_state(Adapter);
20005082Syy150190 
20014061Sxy150489 		/*
20024061Sxy150489 		 * If the link timer has not timed out, we'll not notify
20034919Sxy150489 		 * the upper layer with any link state until the link is up.
20044061Sxy150489 		 */
20054061Sxy150489 		if (link_changed && !Adapter->link_complete) {
20064061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
20075082Syy150190 				mutex_enter(&Adapter->link_lock);
20084061Sxy150489 				Adapter->link_complete = B_TRUE;
20094061Sxy150489 				tid = Adapter->link_tid;
20104061Sxy150489 				Adapter->link_tid = 0;
20115082Syy150190 				mutex_exit(&Adapter->link_lock);
20124061Sxy150489 			} else {
20134061Sxy150489 				link_changed = B_FALSE;
20144061Sxy150489 			}
20154061Sxy150489 		}
20165082Syy150190 		rw_exit(&Adapter->chip_lock);
20173526Sxy150489 
20184061Sxy150489 		if (link_changed) {
20194061Sxy150489 			if (tid != 0)
20204061Sxy150489 				(void) untimeout(tid);
20214061Sxy150489 
20224139Sxy150489 			/*
20234139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
20247133Scc210113 			 * down event. Stop receiver here and reset in watchdog.
20254139Sxy150489 			 */
20264139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
20277133Scc210113 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
20287133Scc210113 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
20297133Scc210113 				E1000_WRITE_REG(hw, E1000_RCTL,
20307133Scc210113 				    rctl & ~E1000_RCTL_EN);
20317133Scc210113 				e1000g_log(Adapter, CE_WARN,
20327133Scc210113 				    "ESB2 receiver disabled");
20337133Scc210113 				Adapter->esb2_workaround = B_TRUE;
20347133Scc210113 			}
20354139Sxy150489 
20364061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
20373526Sxy150489 		}
20383526Sxy150489 
20394919Sxy150489 		start_watchdog_timer(Adapter);
20403526Sxy150489 	}
20415882Syy150190 }
20425882Syy150190 
20435882Syy150190 static uint32_t
20445882Syy150190 e1000g_get_itr(uint32_t rx_packet, uint32_t tx_packet, uint32_t cur_itr)
20455882Syy150190 {
20465882Syy150190 	uint32_t new_itr;
20475882Syy150190 
20485882Syy150190 	/*
20495882Syy150190 	 * Determine a propper itr according to rx/tx packet count
20505882Syy150190 	 * per interrupt, the value of itr are based on document
20515882Syy150190 	 * and testing.
20525882Syy150190 	 */
20535882Syy150190 	if ((rx_packet < DEFAULT_INTR_PACKET_LOW) ||
20545882Syy150190 	    (tx_packet < DEFAULT_INTR_PACKET_LOW)) {
20555882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20565882Syy150190 		goto itr_done;
20573526Sxy150489 	}
20585882Syy150190 	if ((rx_packet > DEFAULT_INTR_PACKET_HIGH) ||
20595882Syy150190 	    (tx_packet > DEFAULT_INTR_PACKET_HIGH)) {
20605882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20615882Syy150190 		goto itr_done;
20625882Syy150190 	}
20635882Syy150190 	if (cur_itr < DEFAULT_INTR_THROTTLING_HIGH) {
20645882Syy150190 		new_itr = cur_itr + (DEFAULT_INTR_THROTTLING_HIGH >> 2);
20655882Syy150190 		if (new_itr > DEFAULT_INTR_THROTTLING_HIGH)
20665882Syy150190 			new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20675882Syy150190 	} else
20685882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20695882Syy150190 
20705882Syy150190 itr_done:
20715882Syy150190 	if (cur_itr == new_itr)
20725882Syy150190 		return (0);
20735882Syy150190 	else
20745882Syy150190 		return (new_itr);
20753526Sxy150489 }
20763526Sxy150489 
20773526Sxy150489 static void
20783526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
20793526Sxy150489 {
20803526Sxy150489 	struct e1000_hw *hw;
20813526Sxy150489 	int slot;
20823526Sxy150489 
20834919Sxy150489 	hw = &Adapter->shared;
20843526Sxy150489 
20855273Sgl147354 	if (!Adapter->unicst_init) {
20863526Sxy150489 		/* Initialize the multiple unicast addresses */
20873526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
20883526Sxy150489 
20894919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20904919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20913526Sxy150489 			Adapter->unicst_total--;
20923526Sxy150489 
20933526Sxy150489 		Adapter->unicst_avail = Adapter->unicst_total - 1;
20943526Sxy150489 
20953526Sxy150489 		/* Store the default mac address */
20964919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20974919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20984919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20994919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
21004919Sxy150489 
21014919Sxy150489 		bcopy(hw->mac.addr, Adapter->unicst_addr[0].mac.addr,
21023526Sxy150489 		    ETHERADDRL);
21033526Sxy150489 		Adapter->unicst_addr[0].mac.set = 1;
21043526Sxy150489 
21053526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
21063526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 0;
21075273Sgl147354 
21085273Sgl147354 		Adapter->unicst_init = B_TRUE;
21093526Sxy150489 	} else {
21103526Sxy150489 		/* Recover the default mac address */
21114919Sxy150489 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac.addr,
21123526Sxy150489 		    ETHERADDRL);
21133526Sxy150489 
21143526Sxy150489 		/* Store the default mac address */
21154919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
21164919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21174919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21184919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
21193526Sxy150489 
21203526Sxy150489 		/* Re-configure the RAR registers */
21213526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
21223526Sxy150489 			e1000_rar_set(hw,
21233526Sxy150489 			    Adapter->unicst_addr[slot].mac.addr, slot);
21243526Sxy150489 	}
21255273Sgl147354 
21265273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
21275273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21283526Sxy150489 }
21293526Sxy150489 
21303526Sxy150489 static int
21313526Sxy150489 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
21323526Sxy150489 {
21333526Sxy150489 	struct e1000g *Adapter;
21343526Sxy150489 
21353526Sxy150489 	Adapter = (struct e1000g *)arg;
21363526Sxy150489 
21373526Sxy150489 	/* Store the default MAC address */
21384919Sxy150489 	bcopy(mac_addr, Adapter->shared.mac.addr, ETHERADDRL);
21393526Sxy150489 
21403526Sxy150489 	/* Set MAC address in address slot 0, which is the default address */
21413526Sxy150489 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
21423526Sxy150489 }
21433526Sxy150489 
21443526Sxy150489 static int
21453526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
21463526Sxy150489     mac_addr_slot_t slot)
21473526Sxy150489 {
21483526Sxy150489 	struct e1000_hw *hw;
21493526Sxy150489 
21504919Sxy150489 	hw = &Adapter->shared;
21513526Sxy150489 
21523526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21533526Sxy150489 
21543526Sxy150489 	/*
21553526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
21563526Sxy150489 	 * that requires the receiver to be in reset when any of the
21573526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
21583526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
21593526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
21603526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
21613526Sxy150489 	 * and work around any necessary HW errata.
21623526Sxy150489 	 */
21634919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21644919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21653526Sxy150489 		e1000_pci_clear_mwi(hw);
21664919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
21674919Sxy150489 		msec_delay(5);
21683526Sxy150489 	}
21693526Sxy150489 
21703526Sxy150489 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
21713526Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
21723526Sxy150489 
21733526Sxy150489 	if (slot == 0) {
21744919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21754919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21764919Sxy150489 			e1000_rar_set(hw, (uint8_t *)mac_addr, LAST_RAR_ENTRY);
21773526Sxy150489 	}
21783526Sxy150489 
21793526Sxy150489 	/*
21803526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
21813526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
21823526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
21833526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
21843526Sxy150489 	 */
21854919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21864919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21874919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
21884919Sxy150489 		msec_delay(1);
21894919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
21903526Sxy150489 			e1000_pci_set_mwi(hw);
21914919Sxy150489 		e1000g_rx_setup(Adapter);
21923526Sxy150489 	}
21933526Sxy150489 
21943526Sxy150489 	rw_exit(&Adapter->chip_lock);
21953526Sxy150489 
21965273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
21975273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21985273Sgl147354 		return (EIO);
21995273Sgl147354 	}
22005273Sgl147354 
22013526Sxy150489 	return (0);
22023526Sxy150489 }
22033526Sxy150489 
22043526Sxy150489 /*
22053526Sxy150489  * e1000g_m_unicst_add() - will find an unused address slot, set the
22063526Sxy150489  * address value to the one specified, reserve that slot and enable
22073526Sxy150489  * the NIC to start filtering on the new MAC address.
22083526Sxy150489  * Returns 0 on success.
22093526Sxy150489  */
22103526Sxy150489 static int
22113526Sxy150489 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
22123526Sxy150489 {
22133526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22143526Sxy150489 	mac_addr_slot_t slot;
22153526Sxy150489 	int err;
22163526Sxy150489 
22173526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
22183526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
22193526Sxy150489 		return (EINVAL);
22203526Sxy150489 
22213526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22223526Sxy150489 	if (Adapter->unicst_avail == 0) {
22233526Sxy150489 		/* no slots available */
22243526Sxy150489 		rw_exit(&Adapter->chip_lock);
22253526Sxy150489 		return (ENOSPC);
22263526Sxy150489 	}
22273526Sxy150489 
22283526Sxy150489 	/*
22293526Sxy150489 	 * Primary/default address is in slot 0. The next addresses
22303526Sxy150489 	 * are the multiple MAC addresses. So multiple MAC address 0
22313526Sxy150489 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
22323526Sxy150489 	 * MAC address resides in slot 1.
22333526Sxy150489 	 */
22343526Sxy150489 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
22353526Sxy150489 		if (Adapter->unicst_addr[slot].mac.set == 0) {
22363526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
22373526Sxy150489 			break;
22383526Sxy150489 		}
22393526Sxy150489 	}
22403526Sxy150489 
22413526Sxy150489 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
22423526Sxy150489 
22433526Sxy150489 	Adapter->unicst_avail--;
22443526Sxy150489 	rw_exit(&Adapter->chip_lock);
22453526Sxy150489 
22463526Sxy150489 	maddr->mma_slot = slot;
22473526Sxy150489 
22483526Sxy150489 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
22493526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_WRITER);
22503526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22513526Sxy150489 		Adapter->unicst_avail++;
22523526Sxy150489 		rw_exit(&Adapter->chip_lock);
22533526Sxy150489 	}
22543526Sxy150489 
22553526Sxy150489 	return (err);
22563526Sxy150489 }
22573526Sxy150489 
22583526Sxy150489 /*
22593526Sxy150489  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
22603526Sxy150489  * call to e1000g_m_unicst_add(). The slot number that was returned in
22613526Sxy150489  * e1000g_m_unicst_add() is passed in the call to remove the address.
22623526Sxy150489  * Returns 0 on success.
22633526Sxy150489  */
22643526Sxy150489 static int
22653526Sxy150489 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
22663526Sxy150489 {
22673526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22683526Sxy150489 	int err;
22693526Sxy150489 
22703526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22713526Sxy150489 		return (EINVAL);
22723526Sxy150489 
22733526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22743526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22753526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22763526Sxy150489 		Adapter->unicst_avail++;
22773526Sxy150489 		rw_exit(&Adapter->chip_lock);
22783526Sxy150489 
22793526Sxy150489 		/* Copy the default address to the passed slot */
22807426SChenliang.Xu@Sun.COM 		if ((err = e1000g_unicst_set(Adapter,
22817426SChenliang.Xu@Sun.COM 		    Adapter->unicst_addr[0].mac.addr, slot)) != 0) {
22823526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_WRITER);
22833526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
22843526Sxy150489 			Adapter->unicst_avail--;
22853526Sxy150489 			rw_exit(&Adapter->chip_lock);
22863526Sxy150489 		}
22873526Sxy150489 		return (err);
22883526Sxy150489 	}
22893526Sxy150489 	rw_exit(&Adapter->chip_lock);
22903526Sxy150489 
22913526Sxy150489 	return (EINVAL);
22923526Sxy150489 }
22933526Sxy150489 
22943526Sxy150489 /*
22953526Sxy150489  * e1000g_m_unicst_modify() - modifies the value of an address that
22963526Sxy150489  * has been added by e1000g_m_unicst_add(). The new address, address
22973526Sxy150489  * length and the slot number that was returned in the call to add
22983526Sxy150489  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
22993526Sxy150489  * set to 0. Returns 0 on success.
23003526Sxy150489  */
23013526Sxy150489 static int
23023526Sxy150489 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
23033526Sxy150489 {
23043526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
23053526Sxy150489 	mac_addr_slot_t slot;
23063526Sxy150489 
23073526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
23083526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
23093526Sxy150489 		return (EINVAL);
23103526Sxy150489 
23113526Sxy150489 	slot = maddr->mma_slot;
23123526Sxy150489 
23133526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
23143526Sxy150489 		return (EINVAL);
23153526Sxy150489 
23163526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23173526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
23183526Sxy150489 		rw_exit(&Adapter->chip_lock);
23193526Sxy150489 
23203526Sxy150489 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
23213526Sxy150489 	}
23223526Sxy150489 	rw_exit(&Adapter->chip_lock);
23233526Sxy150489 
23243526Sxy150489 	return (EINVAL);
23253526Sxy150489 }
23263526Sxy150489 
23273526Sxy150489 /*
23283526Sxy150489  * e1000g_m_unicst_get() - will get the MAC address and all other
23293526Sxy150489  * information related to the address slot passed in mac_multi_addr_t.
23303526Sxy150489  * mma_flags should be set to 0 in the call.
23313526Sxy150489  * On return, mma_flags can take the following values:
23323526Sxy150489  * 1) MMAC_SLOT_UNUSED
23333526Sxy150489  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
23343526Sxy150489  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
23353526Sxy150489  * 4) MMAC_SLOT_USED
23363526Sxy150489  */
23373526Sxy150489 static int
23383526Sxy150489 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
23393526Sxy150489 {
23403526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
23413526Sxy150489 	mac_addr_slot_t slot;
23423526Sxy150489 
23433526Sxy150489 	slot = maddr->mma_slot;
23443526Sxy150489 
23453526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
23463526Sxy150489 		return (EINVAL);
23473526Sxy150489 
23483526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23493526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
23503526Sxy150489 		bcopy(Adapter->unicst_addr[slot].mac.addr,
23513526Sxy150489 		    maddr->mma_addr, ETHERADDRL);
23523526Sxy150489 		maddr->mma_flags = MMAC_SLOT_USED;
23533526Sxy150489 	} else {
23543526Sxy150489 		maddr->mma_flags = MMAC_SLOT_UNUSED;
23553526Sxy150489 	}
23563526Sxy150489 	rw_exit(&Adapter->chip_lock);
23573526Sxy150489 
23583526Sxy150489 	return (0);
23593526Sxy150489 }
23603526Sxy150489 
23613526Sxy150489 static int
23623526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
23633526Sxy150489 {
23644919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23653526Sxy150489 	int res = 0;
23663526Sxy150489 
23673526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23683526Sxy150489 
23693526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
23703526Sxy150489 		res = EINVAL;
23713526Sxy150489 		goto done;
23723526Sxy150489 	}
23733526Sxy150489 
23743526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
23753526Sxy150489 		res = ENOENT;
23763526Sxy150489 		goto done;
23773526Sxy150489 	}
23783526Sxy150489 
23793526Sxy150489 	bcopy(multiaddr,
23803526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
23813526Sxy150489 	Adapter->mcast_count++;
23823526Sxy150489 
23833526Sxy150489 	/*
23843526Sxy150489 	 * Update the MC table in the hardware
23853526Sxy150489 	 */
23864919Sxy150489 	e1000g_clear_interrupt(Adapter);
23874919Sxy150489 
23884919Sxy150489 	e1000g_setup_multicast(Adapter);
23894919Sxy150489 
23904919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23914919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23924919Sxy150489 		e1000g_rx_setup(Adapter);
23934919Sxy150489 
23944919Sxy150489 	e1000g_mask_interrupt(Adapter);
23953526Sxy150489 
23963526Sxy150489 done:
23973526Sxy150489 	rw_exit(&Adapter->chip_lock);
23985273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23995273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24005273Sgl147354 		res = EIO;
24015273Sgl147354 	}
24025273Sgl147354 
24033526Sxy150489 	return (res);
24043526Sxy150489 }
24053526Sxy150489 
24063526Sxy150489 static int
24073526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
24083526Sxy150489 {
24094919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
24103526Sxy150489 	unsigned i;
24113526Sxy150489 
24123526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
24133526Sxy150489 
24143526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
24153526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
24163526Sxy150489 		    ETHERADDRL) == 0) {
24173526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
24183526Sxy150489 				Adapter->mcast_table[i - 1] =
24193526Sxy150489 				    Adapter->mcast_table[i];
24203526Sxy150489 			}
24213526Sxy150489 			Adapter->mcast_count--;
24223526Sxy150489 			break;
24233526Sxy150489 		}
24243526Sxy150489 	}
24253526Sxy150489 
24263526Sxy150489 	/*
24273526Sxy150489 	 * Update the MC table in the hardware
24283526Sxy150489 	 */
24294919Sxy150489 	e1000g_clear_interrupt(Adapter);
24304919Sxy150489 
24314919Sxy150489 	e1000g_setup_multicast(Adapter);
24324919Sxy150489 
24334919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24344919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
24354919Sxy150489 		e1000g_rx_setup(Adapter);
24364919Sxy150489 
24374919Sxy150489 	e1000g_mask_interrupt(Adapter);
24383526Sxy150489 
24393526Sxy150489 done:
24403526Sxy150489 	rw_exit(&Adapter->chip_lock);
24415273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
24425273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24435273Sgl147354 		return (EIO);
24445273Sgl147354 	}
24455273Sgl147354 
24463526Sxy150489 	return (0);
24473526Sxy150489 }
24483526Sxy150489 
24494919Sxy150489 /*
24504919Sxy150489  * e1000g_setup_multicast - setup multicast data structures
24514919Sxy150489  *
24524919Sxy150489  * This routine initializes all of the multicast related structures.
24534919Sxy150489  */
24544919Sxy150489 void
24554919Sxy150489 e1000g_setup_multicast(struct e1000g *Adapter)
24564919Sxy150489 {
24574919Sxy150489 	uint8_t *mc_addr_list;
24584919Sxy150489 	uint32_t mc_addr_count;
24594919Sxy150489 	uint32_t rctl;
24604919Sxy150489 	struct e1000_hw *hw;
24614919Sxy150489 
24624919Sxy150489 	hw = &Adapter->shared;
24634919Sxy150489 
24644919Sxy150489 	/*
24654919Sxy150489 	 * The e1000g has the ability to do perfect filtering of 16
24664919Sxy150489 	 * addresses. The driver uses one of the e1000g's 16 receive
24674919Sxy150489 	 * address registers for its node/network/mac/individual address.
24684919Sxy150489 	 * So, we have room for up to 15 multicast addresses in the CAM,
24694919Sxy150489 	 * additional MC addresses are handled by the MTA (Multicast Table
24704919Sxy150489 	 * Array)
24714919Sxy150489 	 */
24724919Sxy150489 
24734919Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
24744919Sxy150489 
24754919Sxy150489 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
24764919Sxy150489 
24774919Sxy150489 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
24784919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
24794919Sxy150489 		    "Adapter requested more than %d MC Addresses.\n",
24804919Sxy150489 		    MAX_NUM_MULTICAST_ADDRESSES);
24814919Sxy150489 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
24824919Sxy150489 	} else {
24834919Sxy150489 		/*
24844919Sxy150489 		 * Set the number of MC addresses that we are being
24854919Sxy150489 		 * requested to use
24864919Sxy150489 		 */
24874919Sxy150489 		mc_addr_count = Adapter->mcast_count;
24884919Sxy150489 	}
24894919Sxy150489 	/*
24904919Sxy150489 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
24914919Sxy150489 	 * hang  while writing to the receive address registers if the receiver
24924919Sxy150489 	 * is not in reset before writing to the registers. Updating the RAR
24934919Sxy150489 	 * is done during the setting up of the multicast table, hence the
24944919Sxy150489 	 * receiver has to be put in reset before updating the multicast table
24954919Sxy150489 	 * and then taken out of reset at the end
24964919Sxy150489 	 */
24974919Sxy150489 	/*
24984919Sxy150489 	 * if WMI was enabled then dis able it before issueing the global
24994919Sxy150489 	 * reset to the hardware.
25004919Sxy150489 	 */
25014919Sxy150489 	/*
25024919Sxy150489 	 * Only required for WISEMAN_2_0
25034919Sxy150489 	 */
25044919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
25054919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
25064919Sxy150489 		e1000_pci_clear_mwi(hw);
25074919Sxy150489 		/*
25084919Sxy150489 		 * The e1000g must be in reset before changing any RA
25094919Sxy150489 		 * registers. Reset receive unit.  The chip will remain in
25104919Sxy150489 		 * the reset state until software explicitly restarts it.
25114919Sxy150489 		 */
25124919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
25134919Sxy150489 		/* Allow receiver time to go in to reset */
25144919Sxy150489 		msec_delay(5);
25154919Sxy150489 	}
25166735Scc210113 
25176735Scc210113 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
25184919Sxy150489 	    Adapter->unicst_total, hw->mac.rar_entry_count);
25194919Sxy150489 
25204919Sxy150489 	/*
25214919Sxy150489 	 * Only for Wiseman_2_0
25224919Sxy150489 	 * If MWI was enabled then re-enable it after issueing (as we
25234919Sxy150489 	 * disabled it up there) the receive reset command.
25244919Sxy150489 	 * Wainwright does not have a receive reset command and only thing
25254919Sxy150489 	 * close to it is global reset which will require tx setup also
25264919Sxy150489 	 */
25274919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
25284919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
25294919Sxy150489 		/*
25304919Sxy150489 		 * if WMI was enabled then reenable it after issueing the
25314919Sxy150489 		 * global or receive reset to the hardware.
25324919Sxy150489 		 */
25334919Sxy150489 
25344919Sxy150489 		/*
25354919Sxy150489 		 * Take receiver out of reset
25364919Sxy150489 		 * clear E1000_RCTL_RST bit (and all others)
25374919Sxy150489 		 */
25384919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
25394919Sxy150489 		msec_delay(5);
25404919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
25414919Sxy150489 			e1000_pci_set_mwi(hw);
25424919Sxy150489 	}
25434919Sxy150489 
25444919Sxy150489 	/*
25454919Sxy150489 	 * Restore original value
25464919Sxy150489 	 */
25474919Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
25484919Sxy150489 }
25494919Sxy150489 
25503526Sxy150489 int
25513526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
25523526Sxy150489 {
25533526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25543526Sxy150489 
25553526Sxy150489 	return ((add) ? multicst_add(Adapter, addr)
25564349Sxy150489 	    : multicst_remove(Adapter, addr));
25573526Sxy150489 }
25583526Sxy150489 
25593526Sxy150489 int
25603526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
25613526Sxy150489 {
25623526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25634919Sxy150489 	uint32_t rctl;
25643526Sxy150489 
25653526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25663526Sxy150489 
25674919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
25683526Sxy150489 
25693526Sxy150489 	if (on)
25704919Sxy150489 		rctl |=
25713526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
25723526Sxy150489 	else
25734919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
25744919Sxy150489 
25754919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
25763526Sxy150489 
25773526Sxy150489 	Adapter->e1000g_promisc = on;
25783526Sxy150489 
25793526Sxy150489 	rw_exit(&Adapter->chip_lock);
25803526Sxy150489 
25815273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
25825273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
25835273Sgl147354 		return (EIO);
25845273Sgl147354 	}
25855273Sgl147354 
25863526Sxy150489 	return (0);
25873526Sxy150489 }
25883526Sxy150489 
25893526Sxy150489 static boolean_t
25903526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
25913526Sxy150489 {
25923526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25933526Sxy150489 
25943526Sxy150489 	switch (cap) {
25953526Sxy150489 	case MAC_CAPAB_HCKSUM: {
25963526Sxy150489 		uint32_t *txflags = cap_data;
25976986Smx205022 
25986986Smx205022 		if (Adapter->tx_hcksum_enable)
25996986Smx205022 			*txflags = HCKSUM_IPHDRCKSUM |
26006986Smx205022 			    HCKSUM_INET_PARTIAL;
26016986Smx205022 		else
26023526Sxy150489 			return (B_FALSE);
26033526Sxy150489 		break;
26043526Sxy150489 	}
26053526Sxy150489 	case MAC_CAPAB_POLL:
26063526Sxy150489 		/*
26073526Sxy150489 		 * There's nothing for us to fill in, simply returning
26083526Sxy150489 		 * B_TRUE stating that we support polling is sufficient.
26093526Sxy150489 		 */
26103526Sxy150489 		break;
26113526Sxy150489 
26123526Sxy150489 	case MAC_CAPAB_MULTIADDRESS: {
26133526Sxy150489 		multiaddress_capab_t *mmacp = cap_data;
26143526Sxy150489 
26153526Sxy150489 		/*
26163526Sxy150489 		 * The number of MAC addresses made available by
26173526Sxy150489 		 * this capability is one less than the total as
26183526Sxy150489 		 * the primary address in slot 0 is counted in
26193526Sxy150489 		 * the total.
26203526Sxy150489 		 */
26213526Sxy150489 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
26223526Sxy150489 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
26233526Sxy150489 		/* No multiple factory addresses, set mma_flag to 0 */
26243526Sxy150489 		mmacp->maddr_flag = 0;
26253526Sxy150489 		mmacp->maddr_handle = Adapter;
26263526Sxy150489 		mmacp->maddr_add = e1000g_m_unicst_add;
26273526Sxy150489 		mmacp->maddr_remove = e1000g_m_unicst_remove;
26283526Sxy150489 		mmacp->maddr_modify = e1000g_m_unicst_modify;
26293526Sxy150489 		mmacp->maddr_get = e1000g_m_unicst_get;
26303526Sxy150489 		mmacp->maddr_reserve = NULL;
26313526Sxy150489 		break;
26323526Sxy150489 	}
26336986Smx205022 
26346986Smx205022 	case MAC_CAPAB_LSO: {
26356986Smx205022 		mac_capab_lso_t *cap_lso = cap_data;
26366986Smx205022 
26376986Smx205022 		if (Adapter->lso_enable) {
26386986Smx205022 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
26396986Smx205022 			cap_lso->lso_basic_tcp_ipv4.lso_max =
26406986Smx205022 			    E1000_LSO_MAXLEN;
26416986Smx205022 		} else
26426986Smx205022 			return (B_FALSE);
26436986Smx205022 		break;
26446986Smx205022 	}
26456986Smx205022 
26463526Sxy150489 	default:
26473526Sxy150489 		return (B_FALSE);
26483526Sxy150489 	}
26493526Sxy150489 	return (B_TRUE);
26503526Sxy150489 }
26513526Sxy150489 
26526394Scc210113 static boolean_t
26536394Scc210113 e1000g_param_locked(mac_prop_id_t pr_num)
26546394Scc210113 {
26556394Scc210113 	/*
26566394Scc210113 	 * All en_* parameters are locked (read-only) while
26576394Scc210113 	 * the device is in any sort of loopback mode ...
26586394Scc210113 	 */
26596394Scc210113 	switch (pr_num) {
26606789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
26616789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
26626789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
26636789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
26646789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
26656789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
26666789Sam223141 		case MAC_PROP_AUTONEG:
26676789Sam223141 		case MAC_PROP_FLOWCTRL:
26686394Scc210113 			return (B_TRUE);
26696394Scc210113 	}
26706394Scc210113 	return (B_FALSE);
26716394Scc210113 }
26726394Scc210113 
26736394Scc210113 /*
26746394Scc210113  * callback function for set/get of properties
26756394Scc210113  */
26766394Scc210113 static int
26776394Scc210113 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
26786394Scc210113     uint_t pr_valsize, const void *pr_val)
26796394Scc210113 {
26806394Scc210113 	struct e1000g *Adapter = arg;
26816394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
26826394Scc210113 	struct e1000_phy_info *phy = &Adapter->shared.phy;
26836735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
26846394Scc210113 	int err = 0;
26856735Scc210113 	link_flowctrl_t flowctrl;
26866512Ssowmini 	uint32_t cur_mtu, new_mtu;
26876394Scc210113 	uint64_t tmp = 0;
26886394Scc210113 
26896394Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
26906394Scc210113 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
26916394Scc210113 	    e1000g_param_locked(pr_num)) {
26926394Scc210113 		/*
26936394Scc210113 		 * All en_* parameters are locked (read-only)
26946394Scc210113 		 * while the device is in any sort of loopback mode.
26956394Scc210113 		 */
26966394Scc210113 		rw_exit(&Adapter->chip_lock);
26976394Scc210113 		return (EBUSY);
26986394Scc210113 	}
26996394Scc210113 
27006394Scc210113 	switch (pr_num) {
27016789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
27026394Scc210113 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
27036394Scc210113 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
27046394Scc210113 			goto reset;
27056789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
27066394Scc210113 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
27076394Scc210113 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
27086394Scc210113 			goto reset;
27096789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
27106394Scc210113 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
27116394Scc210113 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
27126394Scc210113 			goto reset;
27136789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
27146394Scc210113 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
27156394Scc210113 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
27166394Scc210113 			goto reset;
27176789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
27186394Scc210113 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
27196394Scc210113 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
27206394Scc210113 			goto reset;
27216789Sam223141 		case MAC_PROP_AUTONEG:
27226394Scc210113 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
27236394Scc210113 			goto reset;
27246789Sam223141 		case MAC_PROP_FLOWCTRL:
27256735Scc210113 			fc->send_xon = B_TRUE;
27266735Scc210113 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
27276735Scc210113 
27286735Scc210113 			switch (flowctrl) {
27296394Scc210113 			default:
27306394Scc210113 				err = EINVAL;
27316394Scc210113 				break;
27326394Scc210113 			case LINK_FLOWCTRL_NONE:
27336735Scc210113 				fc->type = e1000_fc_none;
27346394Scc210113 				break;
27356394Scc210113 			case LINK_FLOWCTRL_RX:
27366735Scc210113 				fc->type = e1000_fc_rx_pause;
27376394Scc210113 				break;
27386394Scc210113 			case LINK_FLOWCTRL_TX:
27396735Scc210113 				fc->type = e1000_fc_tx_pause;
27406394Scc210113 				break;
27416394Scc210113 			case LINK_FLOWCTRL_BI:
27426735Scc210113 				fc->type = e1000_fc_full;
27436394Scc210113 				break;
27446394Scc210113 			}
27456394Scc210113 reset:
27466394Scc210113 			if (err == 0) {
27476394Scc210113 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
27486394Scc210113 					err = EINVAL;
27496394Scc210113 			}
27506394Scc210113 			break;
27516789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
27526789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
27536789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
27546789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
27556789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
27566789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
27578118SVasumathi.Sundaram@Sun.COM 		case MAC_PROP_EN_1000HDX_CAP:
27586789Sam223141 		case MAC_PROP_STATUS:
27596789Sam223141 		case MAC_PROP_SPEED:
27606789Sam223141 		case MAC_PROP_DUPLEX:
27616394Scc210113 			err = ENOTSUP; /* read-only prop. Can't set this. */
27626394Scc210113 			break;
27636789Sam223141 		case MAC_PROP_MTU:
27646394Scc210113 			cur_mtu = Adapter->default_mtu;
27656394Scc210113 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
27666394Scc210113 			if (new_mtu == cur_mtu) {
27676394Scc210113 				err = 0;
27686394Scc210113 				break;
27696394Scc210113 			}
27706394Scc210113 
27716394Scc210113 			tmp = new_mtu + sizeof (struct ether_vlan_header) +
27726394Scc210113 			    ETHERFCSL;
27736394Scc210113 			if ((tmp < DEFAULT_FRAME_SIZE) ||
27746394Scc210113 			    (tmp > MAXIMUM_FRAME_SIZE)) {
27756394Scc210113 				err = EINVAL;
27766394Scc210113 				break;
27776394Scc210113 			}
27786394Scc210113 
27797607STed.You@Sun.COM 			/* ich8 does not support jumbo frames */
27806394Scc210113 			if ((mac->type == e1000_ich8lan) &&
27816394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27826394Scc210113 				err = EINVAL;
27836394Scc210113 				break;
27846394Scc210113 			}
27856394Scc210113 			/* ich9 does not do jumbo frames on one phy type */
27866394Scc210113 			if ((mac->type == e1000_ich9lan) &&
27876394Scc210113 			    (phy->type == e1000_phy_ife) &&
27886394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27896394Scc210113 				err = EINVAL;
27906394Scc210113 				break;
27916394Scc210113 			}
27926394Scc210113 			if (Adapter->chip_state != E1000G_STOP) {
27936394Scc210113 				err = EBUSY;
27946394Scc210113 				break;
27956394Scc210113 			}
27966394Scc210113 
27976394Scc210113 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
27986394Scc210113 			if (err == 0) {
27997426SChenliang.Xu@Sun.COM 				Adapter->max_frame_size = (uint32_t)tmp;
28006394Scc210113 				Adapter->default_mtu = new_mtu;
28016394Scc210113 				e1000g_set_bufsize(Adapter);
28026394Scc210113 			}
28036394Scc210113 			break;
28046789Sam223141 		case MAC_PROP_PRIVATE:
28056394Scc210113 			err = e1000g_set_priv_prop(Adapter, pr_name,
28066394Scc210113 			    pr_valsize, pr_val);
28076394Scc210113 			break;
28086394Scc210113 		default:
28096394Scc210113 			err = ENOTSUP;
28106394Scc210113 			break;
28116394Scc210113 	}
28126394Scc210113 	rw_exit(&Adapter->chip_lock);
28136394Scc210113 	return (err);
28146394Scc210113 }
28156394Scc210113 
28166394Scc210113 static int
28176394Scc210113 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
28188118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
28196394Scc210113 {
28206394Scc210113 	struct e1000g *Adapter = arg;
28216735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
28226512Ssowmini 	int err = 0;
28236735Scc210113 	link_flowctrl_t flowctrl;
28246394Scc210113 	uint64_t tmp = 0;
28256394Scc210113 
28266512Ssowmini 	if (pr_valsize == 0)
28276512Ssowmini 		return (EINVAL);
28286512Ssowmini 
28298118SVasumathi.Sundaram@Sun.COM 	*perm = MAC_PROP_PERM_RW;
28308118SVasumathi.Sundaram@Sun.COM 
28316394Scc210113 	bzero(pr_val, pr_valsize);
28326789Sam223141 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
28336512Ssowmini 		return (e1000g_get_def_val(Adapter, pr_num,
28346512Ssowmini 		    pr_valsize, pr_val));
28356512Ssowmini 	}
28366512Ssowmini 
28376394Scc210113 	switch (pr_num) {
28386789Sam223141 		case MAC_PROP_DUPLEX:
28398118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28406512Ssowmini 			if (pr_valsize >= sizeof (link_duplex_t)) {
28416512Ssowmini 				bcopy(&Adapter->link_duplex, pr_val,
28426512Ssowmini 				    sizeof (link_duplex_t));
28436512Ssowmini 			} else
28446512Ssowmini 				err = EINVAL;
28456394Scc210113 			break;
28466789Sam223141 		case MAC_PROP_SPEED:
28478118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28486394Scc210113 			if (pr_valsize >= sizeof (uint64_t)) {
28496394Scc210113 				tmp = Adapter->link_speed * 1000000ull;
28506394Scc210113 				bcopy(&tmp, pr_val, sizeof (tmp));
28516512Ssowmini 			} else
28526512Ssowmini 				err = EINVAL;
28536394Scc210113 			break;
28546789Sam223141 		case MAC_PROP_AUTONEG:
28556512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
28566394Scc210113 			break;
28576789Sam223141 		case MAC_PROP_FLOWCTRL:
28586394Scc210113 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
28596735Scc210113 				switch (fc->type) {
28606394Scc210113 					case e1000_fc_none:
28616735Scc210113 						flowctrl = LINK_FLOWCTRL_NONE;
28626394Scc210113 						break;
28636394Scc210113 					case e1000_fc_rx_pause:
28646735Scc210113 						flowctrl = LINK_FLOWCTRL_RX;
28656394Scc210113 						break;
28666394Scc210113 					case e1000_fc_tx_pause:
28676735Scc210113 						flowctrl = LINK_FLOWCTRL_TX;
28686394Scc210113 						break;
28696394Scc210113 					case e1000_fc_full:
28706735Scc210113 						flowctrl = LINK_FLOWCTRL_BI;
28716394Scc210113 						break;
28726394Scc210113 				}
28736735Scc210113 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
28746512Ssowmini 			} else
28756512Ssowmini 				err = EINVAL;
28766394Scc210113 			break;
28776789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
28788118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28796512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
28806394Scc210113 			break;
28816789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
28826512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
28836394Scc210113 			break;
28846789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
28858118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28866512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
28876394Scc210113 			break;
28886789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
28898118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28906512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
28916394Scc210113 			break;
28926789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
28938118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
28946512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
28956394Scc210113 			break;
28966789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
28976512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
28986394Scc210113 			break;
28996789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
29008118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
29016512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
29026394Scc210113 			break;
29036789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
29046512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
29056394Scc210113 			break;
29066789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
29078118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
29086512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
29096394Scc210113 			break;
29106789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
29116512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
29126394Scc210113 			break;
29136789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
29148118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
29156512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
29166394Scc210113 			break;
29176789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
29186512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
29196512Ssowmini 			break;
29206789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
29216789Sam223141 		case MAC_PROP_EN_100T4_CAP:
29228118SVasumathi.Sundaram@Sun.COM 			*perm = MAC_PROP_PERM_READ;
29236512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
29246394Scc210113 			break;
29256789Sam223141 		case MAC_PROP_PRIVATE:
29266394Scc210113 			err = e1000g_get_priv_prop(Adapter, pr_name,
29278118SVasumathi.Sundaram@Sun.COM 			    pr_flags, pr_valsize, pr_val, perm);
29286394Scc210113 			break;
29296394Scc210113 		default:
29306394Scc210113 			err = ENOTSUP;
29316394Scc210113 			break;
29326394Scc210113 	}
29336394Scc210113 	return (err);
29346394Scc210113 }
29356394Scc210113 
29367426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
29376394Scc210113 static int
29386394Scc210113 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
29396394Scc210113     uint_t pr_valsize, const void *pr_val)
29406394Scc210113 {
29416394Scc210113 	int err = 0;
29426394Scc210113 	long result;
29436394Scc210113 	struct e1000_hw *hw = &Adapter->shared;
29446394Scc210113 
29456394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
29466394Scc210113 		if (pr_val == NULL) {
29476394Scc210113 			err = EINVAL;
29486394Scc210113 			return (err);
29496394Scc210113 		}
29506394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29516394Scc210113 		if (result < MIN_TX_BCOPY_THRESHOLD ||
29526394Scc210113 		    result > MAX_TX_BCOPY_THRESHOLD)
29536394Scc210113 			err = EINVAL;
29546394Scc210113 		else {
29556394Scc210113 			Adapter->tx_bcopy_thresh = (uint32_t)result;
29566394Scc210113 		}
29576394Scc210113 		return (err);
29586394Scc210113 	}
29596394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
29606394Scc210113 		if (pr_val == NULL) {
29616394Scc210113 			err = EINVAL;
29626394Scc210113 			return (err);
29636394Scc210113 		}
29646394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29656394Scc210113 		if (result < 0 || result > 1)
29666394Scc210113 			err = EINVAL;
29676394Scc210113 		else {
29686394Scc210113 			Adapter->tx_intr_enable = (result == 1) ?
29696394Scc210113 			    B_TRUE: B_FALSE;
29706394Scc210113 			if (Adapter->tx_intr_enable)
29716394Scc210113 				e1000g_mask_tx_interrupt(Adapter);
29726394Scc210113 			else
29736394Scc210113 				e1000g_clear_tx_interrupt(Adapter);
29746394Scc210113 			if (e1000g_check_acc_handle(
29756394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29766394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29776394Scc210113 				    DDI_SERVICE_DEGRADED);
29786394Scc210113 		}
29796394Scc210113 		return (err);
29806394Scc210113 	}
29816394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
29826394Scc210113 		if (pr_val == NULL) {
29836394Scc210113 			err = EINVAL;
29846394Scc210113 			return (err);
29856394Scc210113 		}
29866394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29876394Scc210113 		if (result < MIN_TX_INTR_DELAY ||
29886394Scc210113 		    result > MAX_TX_INTR_DELAY)
29896394Scc210113 			err = EINVAL;
29906394Scc210113 		else {
29916394Scc210113 			Adapter->tx_intr_delay = (uint32_t)result;
29926394Scc210113 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
29936394Scc210113 			if (e1000g_check_acc_handle(
29946394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29956394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29966394Scc210113 				    DDI_SERVICE_DEGRADED);
29976394Scc210113 		}
29986394Scc210113 		return (err);
29996394Scc210113 	}
30006394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
30016394Scc210113 		if (pr_val == NULL) {
30026394Scc210113 			err = EINVAL;
30036394Scc210113 			return (err);
30046394Scc210113 		}
30056394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30066394Scc210113 		if (result < MIN_TX_INTR_ABS_DELAY ||
30076394Scc210113 		    result > MAX_TX_INTR_ABS_DELAY)
30086394Scc210113 			err = EINVAL;
30096394Scc210113 		else {
30106394Scc210113 			Adapter->tx_intr_abs_delay = (uint32_t)result;
30116394Scc210113 			E1000_WRITE_REG(hw, E1000_TADV,
30126394Scc210113 			    Adapter->tx_intr_abs_delay);
30136394Scc210113 			if (e1000g_check_acc_handle(
30146394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30156394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30166394Scc210113 				    DDI_SERVICE_DEGRADED);
30176394Scc210113 		}
30186394Scc210113 		return (err);
30196394Scc210113 	}
30206394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
30216394Scc210113 		if (pr_val == NULL) {
30226394Scc210113 			err = EINVAL;
30236394Scc210113 			return (err);
30246394Scc210113 		}
30256394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30266394Scc210113 		if (result < MIN_RX_BCOPY_THRESHOLD ||
30276394Scc210113 		    result > MAX_RX_BCOPY_THRESHOLD)
30286394Scc210113 			err = EINVAL;
30296394Scc210113 		else
30306394Scc210113 			Adapter->rx_bcopy_thresh = (uint32_t)result;
30316394Scc210113 		return (err);
30326394Scc210113 	}
30336394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
30346394Scc210113 		if (pr_val == NULL) {
30356394Scc210113 			err = EINVAL;
30366394Scc210113 			return (err);
30376394Scc210113 		}
30386394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30396394Scc210113 		if (result < MIN_RX_LIMIT_ON_INTR ||
30406394Scc210113 		    result > MAX_RX_LIMIT_ON_INTR)
30416394Scc210113 			err = EINVAL;
30426394Scc210113 		else
30436394Scc210113 			Adapter->rx_limit_onintr = (uint32_t)result;
30446394Scc210113 		return (err);
30456394Scc210113 	}
30466394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
30476394Scc210113 		if (pr_val == NULL) {
30486394Scc210113 			err = EINVAL;
30496394Scc210113 			return (err);
30506394Scc210113 		}
30516394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30526394Scc210113 		if (result < MIN_RX_INTR_DELAY ||
30536394Scc210113 		    result > MAX_RX_INTR_DELAY)
30546394Scc210113 			err = EINVAL;
30556394Scc210113 		else {
30566394Scc210113 			Adapter->rx_intr_delay = (uint32_t)result;
30576394Scc210113 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
30586394Scc210113 			if (e1000g_check_acc_handle(
30596394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30606394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30616394Scc210113 				    DDI_SERVICE_DEGRADED);
30626394Scc210113 		}
30636394Scc210113 		return (err);
30646394Scc210113 	}
30656394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
30666394Scc210113 		if (pr_val == NULL) {
30676394Scc210113 			err = EINVAL;
30686394Scc210113 			return (err);
30696394Scc210113 		}
30706394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30716394Scc210113 		if (result < MIN_RX_INTR_ABS_DELAY ||
30726394Scc210113 		    result > MAX_RX_INTR_ABS_DELAY)
30736394Scc210113 			err = EINVAL;
30746394Scc210113 		else {
30756394Scc210113 			Adapter->rx_intr_abs_delay = (uint32_t)result;
30766394Scc210113 			E1000_WRITE_REG(hw, E1000_RADV,
30776394Scc210113 			    Adapter->rx_intr_abs_delay);
30786394Scc210113 			if (e1000g_check_acc_handle(
30796394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30806394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30816394Scc210113 				    DDI_SERVICE_DEGRADED);
30826394Scc210113 		}
30836394Scc210113 		return (err);
30846394Scc210113 	}
30856394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
30866394Scc210113 		if (pr_val == NULL) {
30876394Scc210113 			err = EINVAL;
30886394Scc210113 			return (err);
30896394Scc210113 		}
30906394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30916394Scc210113 		if (result < MIN_INTR_THROTTLING ||
30926394Scc210113 		    result > MAX_INTR_THROTTLING)
30936394Scc210113 			err = EINVAL;
30946394Scc210113 		else {
30956394Scc210113 			if (hw->mac.type >= e1000_82540) {
30966394Scc210113 				Adapter->intr_throttling_rate =
30976394Scc210113 				    (uint32_t)result;
30986394Scc210113 				E1000_WRITE_REG(hw, E1000_ITR,
30996394Scc210113 				    Adapter->intr_throttling_rate);
31006394Scc210113 				if (e1000g_check_acc_handle(
31016394Scc210113 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
31026394Scc210113 					ddi_fm_service_impact(Adapter->dip,
31036394Scc210113 					    DDI_SERVICE_DEGRADED);
31046394Scc210113 			} else
31056394Scc210113 				err = EINVAL;
31066394Scc210113 		}
31076394Scc210113 		return (err);
31086394Scc210113 	}
31096394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
31106394Scc210113 		if (pr_val == NULL) {
31116394Scc210113 			err = EINVAL;
31126394Scc210113 			return (err);
31136394Scc210113 		}
31146394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31156394Scc210113 		if (result < 0 || result > 1)
31166394Scc210113 			err = EINVAL;
31176394Scc210113 		else {
31186394Scc210113 			if (hw->mac.type >= e1000_82540) {
31196394Scc210113 				Adapter->intr_adaptive = (result == 1) ?
31206394Scc210113 				    B_TRUE : B_FALSE;
31216394Scc210113 			} else {
31226394Scc210113 				err = EINVAL;
31236394Scc210113 			}
31246394Scc210113 		}
31256394Scc210113 		return (err);
31266394Scc210113 	}
31276394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
31286394Scc210113 		if (pr_val == NULL) {
31296394Scc210113 			err = EINVAL;
31306394Scc210113 			return (err);
31316394Scc210113 		}
31326394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31336394Scc210113 		if (result < MIN_TX_RECYCLE_THRESHOLD ||
31346394Scc210113 		    result > MAX_TX_RECYCLE_THRESHOLD)
31356394Scc210113 			err = EINVAL;
31366394Scc210113 		else
31376394Scc210113 			Adapter->tx_recycle_thresh = (uint32_t)result;
31386394Scc210113 		return (err);
31396394Scc210113 	}
31406394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 0) {
31416394Scc210113 		if (pr_val == NULL) {
31426394Scc210113 			err = EINVAL;
31436394Scc210113 			return (err);
31446394Scc210113 		}
31456394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
31466394Scc210113 		if (result < MIN_TX_RECYCLE_NUM ||
31476394Scc210113 		    result > MAX_TX_RECYCLE_NUM)
31486394Scc210113 			err = EINVAL;
31496394Scc210113 		else
31506394Scc210113 			Adapter->tx_recycle_num = (uint32_t)result;
31516394Scc210113 		return (err);
31526394Scc210113 	}
31536394Scc210113 	return (ENOTSUP);
31546394Scc210113 }
31556394Scc210113 
31566394Scc210113 static int
31576394Scc210113 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
31588118SVasumathi.Sundaram@Sun.COM     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
31596394Scc210113 {
31606394Scc210113 	int err = ENOTSUP;
31616789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
31626512Ssowmini 	int value;
31636512Ssowmini 
31646512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
31658118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
31666512Ssowmini 		if (is_default)
31676512Ssowmini 			goto done;
31686512Ssowmini 		value = Adapter->param_adv_pause;
31696512Ssowmini 		err = 0;
31706512Ssowmini 		goto done;
31716512Ssowmini 	}
31726512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
31738118SVasumathi.Sundaram@Sun.COM 		*perm = MAC_PROP_PERM_READ;
31746512Ssowmini 		if (is_default)
31756512Ssowmini 			goto done;
31766512Ssowmini 		value = Adapter->param_adv_asym_pause;
31776512Ssowmini 		err = 0;
31786512Ssowmini 		goto done;
31796512Ssowmini 	}
31806394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
31816512Ssowmini 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
31826512Ssowmini 		    Adapter->tx_bcopy_thresh);
31836394Scc210113 		err = 0;
31846394Scc210113 		goto done;
31856394Scc210113 	}
31866394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
31876512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
31886512Ssowmini 		    Adapter->tx_intr_enable);
31896394Scc210113 		err = 0;
31906394Scc210113 		goto done;
31916394Scc210113 	}
31926394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
31936512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
31946512Ssowmini 		    Adapter->tx_intr_delay);
31956394Scc210113 		err = 0;
31966394Scc210113 		goto done;
31976394Scc210113 	}
31986394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
31996512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
32006512Ssowmini 		    Adapter->tx_intr_abs_delay);
32016394Scc210113 		err = 0;
32026394Scc210113 		goto done;
32036394Scc210113 	}
32046394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
32056512Ssowmini 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
32066512Ssowmini 		    Adapter->rx_bcopy_thresh);
32076394Scc210113 		err = 0;
32086394Scc210113 		goto done;
32096394Scc210113 	}
32106394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
32116512Ssowmini 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
32126512Ssowmini 		    Adapter->rx_limit_onintr);
32136394Scc210113 		err = 0;
32146394Scc210113 		goto done;
32156394Scc210113 	}
32166394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
32176512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
32186512Ssowmini 		    Adapter->rx_intr_delay);
32196394Scc210113 		err = 0;
32206394Scc210113 		goto done;
32216394Scc210113 	}
32226394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
32236512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
32246512Ssowmini 		    Adapter->rx_intr_abs_delay);
32256394Scc210113 		err = 0;
32266394Scc210113 		goto done;
32276394Scc210113 	}
32286394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
32296512Ssowmini 		value = (is_default ? DEFAULT_INTR_THROTTLING :
32306512Ssowmini 		    Adapter->intr_throttling_rate);
32316394Scc210113 		err = 0;
32326394Scc210113 		goto done;
32336394Scc210113 	}
32346394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
32356512Ssowmini 		value = (is_default ? 1 : Adapter->intr_adaptive);
32366394Scc210113 		err = 0;
32376394Scc210113 		goto done;
32386394Scc210113 	}
32396394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
32406512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_THRESHOLD :
32416512Ssowmini 		    Adapter->tx_recycle_thresh);
32426394Scc210113 		err = 0;
32436394Scc210113 		goto done;
32446394Scc210113 	}
32456394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 0) {
32466512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_NUM :
32476512Ssowmini 		    Adapter->tx_recycle_num);
32486394Scc210113 		err = 0;
32496394Scc210113 		goto done;
32506394Scc210113 	}
32516394Scc210113 done:
32526394Scc210113 	if (err == 0) {
32536512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
32546394Scc210113 	}
32556394Scc210113 	return (err);
32566394Scc210113 }
32576394Scc210113 
32583526Sxy150489 /*
32594919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
32604919Sxy150489  * This routine gets user-configured values out of the configuration
32614919Sxy150489  * file e1000g.conf.
32624919Sxy150489  *
32634919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
32644919Sxy150489  * default.
32654919Sxy150489  * If user does not configure a value, use the default.
32664919Sxy150489  * If user configures below the minimum, use the minumum.
32674919Sxy150489  * If user configures above the maximum, use the maxumum.
32683526Sxy150489  */
32693526Sxy150489 static void
32704919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
32713526Sxy150489 {
32724919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
32734919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
32744919Sxy150489 
32753526Sxy150489 	/*
32763526Sxy150489 	 * get each configurable property from e1000g.conf
32773526Sxy150489 	 */
32783526Sxy150489 
32793526Sxy150489 	/*
32803526Sxy150489 	 * NumTxDescriptors
32813526Sxy150489 	 */
32824919Sxy150489 	Adapter->tx_desc_num =
32834919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
32844919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
32854919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
32863526Sxy150489 
32873526Sxy150489 	/*
32883526Sxy150489 	 * NumRxDescriptors
32893526Sxy150489 	 */
32904919Sxy150489 	Adapter->rx_desc_num =
32914919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
32924919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
32934919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
32943526Sxy150489 
32953526Sxy150489 	/*
32963526Sxy150489 	 * NumRxFreeList
32973526Sxy150489 	 */
32984919Sxy150489 	Adapter->rx_freelist_num =
32994919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
33004919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
33014919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
33023526Sxy150489 
33033526Sxy150489 	/*
33043526Sxy150489 	 * NumTxPacketList
33053526Sxy150489 	 */
33064919Sxy150489 	Adapter->tx_freelist_num =
33074919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
33084919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
33094919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
33103526Sxy150489 
33113526Sxy150489 	/*
33123526Sxy150489 	 * FlowControl
33133526Sxy150489 	 */
33146735Scc210113 	hw->fc.send_xon = B_TRUE;
33156735Scc210113 	hw->fc.type =
33164919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
33174919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
33183526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
33196735Scc210113 	if (hw->fc.type == 4)
33206735Scc210113 		hw->fc.type = e1000_fc_default;
33213526Sxy150489 
33223526Sxy150489 	/*
33234919Sxy150489 	 * Max Num Receive Packets on Interrupt
33243526Sxy150489 	 */
33254919Sxy150489 	Adapter->rx_limit_onintr =
33264919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
33274919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
33284919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
33293526Sxy150489 
33303526Sxy150489 	/*
33313526Sxy150489 	 * PHY master slave setting
33323526Sxy150489 	 */
33334919Sxy150489 	hw->phy.ms_type =
33344919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
33353526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
33363526Sxy150489 	    e1000_ms_hw_default);
33373526Sxy150489 
33383526Sxy150489 	/*
33393526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
33403526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
33413526Sxy150489 	 */
33424919Sxy150489 	tbi_compatibility =
33434919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
33444919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
33454919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
33463526Sxy150489 
33473526Sxy150489 	/*
33483526Sxy150489 	 * MSI Enable
33493526Sxy150489 	 */
33506986Smx205022 	Adapter->msi_enable =
33514919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
33524919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
33533526Sxy150489 
33543526Sxy150489 	/*
33553526Sxy150489 	 * Interrupt Throttling Rate
33563526Sxy150489 	 */
33573526Sxy150489 	Adapter->intr_throttling_rate =
33584919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
33594919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
33604919Sxy150489 	    DEFAULT_INTR_THROTTLING);
33613526Sxy150489 
33623526Sxy150489 	/*
33633526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
33643526Sxy150489 	 * It is enabled by default
33653526Sxy150489 	 */
33663526Sxy150489 	Adapter->intr_adaptive =
33674919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
33683526Sxy150489 	    B_TRUE : B_FALSE;
33695882Syy150190 
33705882Syy150190 	/*
33715882Syy150190 	 * Tx recycle threshold
33725882Syy150190 	 */
33735882Syy150190 	Adapter->tx_recycle_thresh =
33745882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_thresh",
33755882Syy150190 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
33765882Syy150190 	    DEFAULT_TX_RECYCLE_THRESHOLD);
33775882Syy150190 
33785882Syy150190 	/*
33795882Syy150190 	 * Tx recycle descriptor number
33805882Syy150190 	 */
33815882Syy150190 	Adapter->tx_recycle_num =
33825882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_num",
33835882Syy150190 	    MIN_TX_RECYCLE_NUM, MAX_TX_RECYCLE_NUM,
33845882Syy150190 	    DEFAULT_TX_RECYCLE_NUM);
33856011Ssv141092 
33866011Ssv141092 	/*
33876011Ssv141092 	 * Hardware checksum enable/disable parameter
33886011Ssv141092 	 */
33896986Smx205022 	Adapter->tx_hcksum_enable =
33906986Smx205022 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
33916011Ssv141092 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
33926986Smx205022 	/*
33936986Smx205022 	 * Checksum on/off selection via global parameters.
33946986Smx205022 	 *
33956986Smx205022 	 * If the chip is flagged as not capable of (correctly)
33966986Smx205022 	 * handling checksumming, we don't enable it on either
33976986Smx205022 	 * Rx or Tx side.  Otherwise, we take this chip's settings
33986986Smx205022 	 * from the patchable global defaults.
33996986Smx205022 	 *
34006986Smx205022 	 * We advertise our capabilities only if TX offload is
34016986Smx205022 	 * enabled.  On receive, the stack will accept checksummed
34026986Smx205022 	 * packets anyway, even if we haven't said we can deliver
34036986Smx205022 	 * them.
34046986Smx205022 	 */
34056986Smx205022 	switch (hw->mac.type) {
34066986Smx205022 		case e1000_82540:
34076986Smx205022 		case e1000_82544:
34086986Smx205022 		case e1000_82545:
34096986Smx205022 		case e1000_82545_rev_3:
34106986Smx205022 		case e1000_82546:
34116986Smx205022 		case e1000_82546_rev_3:
34126986Smx205022 		case e1000_82571:
34136986Smx205022 		case e1000_82572:
34146986Smx205022 		case e1000_82573:
34156986Smx205022 		case e1000_80003es2lan:
34166986Smx205022 			break;
34176986Smx205022 		/*
34186986Smx205022 		 * For the following Intel PRO/1000 chipsets, we have not
34196986Smx205022 		 * tested the hardware checksum offload capability, so we
34206986Smx205022 		 * disable the capability for them.
34216986Smx205022 		 *	e1000_82542,
34226986Smx205022 		 *	e1000_82543,
34236986Smx205022 		 *	e1000_82541,
34246986Smx205022 		 *	e1000_82541_rev_2,
34256986Smx205022 		 *	e1000_82547,
34266986Smx205022 		 *	e1000_82547_rev_2,
34276986Smx205022 		 */
34286986Smx205022 		default:
34296986Smx205022 			Adapter->tx_hcksum_enable = B_FALSE;
34306986Smx205022 	}
34316986Smx205022 
34326986Smx205022 	/*
34336986Smx205022 	 * Large Send Offloading(LSO) Enable/Disable
34346986Smx205022 	 * If the tx hardware checksum is not enabled, LSO should be
34356986Smx205022 	 * disabled.
34366986Smx205022 	 */
34376986Smx205022 	Adapter->lso_enable =
34386986Smx205022 	    e1000g_get_prop(Adapter, "lso_enable",
34396986Smx205022 	    0, 1, DEFAULT_LSO_ENABLE);
34406986Smx205022 
34416986Smx205022 	switch (hw->mac.type) {
34426986Smx205022 		case e1000_82546:
34436986Smx205022 		case e1000_82546_rev_3:
34446986Smx205022 			if (Adapter->lso_enable)
34456986Smx205022 				Adapter->lso_premature_issue = B_TRUE;
34467426SChenliang.Xu@Sun.COM 			/* FALLTHRU */
34476986Smx205022 		case e1000_82571:
34486986Smx205022 		case e1000_82572:
34496986Smx205022 		case e1000_82573:
34508073SMin.Xu@Sun.COM 		case e1000_80003es2lan:
34516986Smx205022 			break;
34526986Smx205022 		default:
34536986Smx205022 			Adapter->lso_enable = B_FALSE;
34546986Smx205022 	}
34556986Smx205022 
34566986Smx205022 	if (!Adapter->tx_hcksum_enable) {
34576986Smx205022 		Adapter->lso_premature_issue = B_FALSE;
34586986Smx205022 		Adapter->lso_enable = B_FALSE;
34596986Smx205022 	}
34603526Sxy150489 }
34613526Sxy150489 
34623526Sxy150489 /*
34634919Sxy150489  * e1000g_get_prop - routine to read properties
34644919Sxy150489  *
34654919Sxy150489  * Get a user-configure property value out of the configuration
34664919Sxy150489  * file e1000g.conf.
34674919Sxy150489  *
34684919Sxy150489  * Caller provides name of the property, a default value, a minimum
34694919Sxy150489  * value, and a maximum value.
34704919Sxy150489  *
34714919Sxy150489  * Return configured value of the property, with default, minimum and
34724919Sxy150489  * maximum properly applied.
34733526Sxy150489  */
34743526Sxy150489 static int
34754919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
34763526Sxy150489     char *propname,		/* name of the property */
34773526Sxy150489     int minval,			/* minimum acceptable value */
34783526Sxy150489     int maxval,			/* maximim acceptable value */
34793526Sxy150489     int defval)			/* default value */
34803526Sxy150489 {
34813526Sxy150489 	int propval;		/* value returned for requested property */
34823526Sxy150489 	int *props;		/* point to array of properties returned */
34833526Sxy150489 	uint_t nprops;		/* number of property value returned */
34843526Sxy150489 
34853526Sxy150489 	/*
34863526Sxy150489 	 * get the array of properties from the config file
34873526Sxy150489 	 */
34883526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
34893526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
34903526Sxy150489 		/* got some properties, test if we got enough */
34914919Sxy150489 		if (Adapter->instance < nprops) {
34924919Sxy150489 			propval = props[Adapter->instance];
34933526Sxy150489 		} else {
34943526Sxy150489 			/* not enough properties configured */
34953526Sxy150489 			propval = defval;
34964919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34973526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
34983526Sxy150489 			    " - set to %d\n",
34993526Sxy150489 			    propname, propval);
35003526Sxy150489 		}
35013526Sxy150489 
35023526Sxy150489 		/* free memory allocated for properties */
35033526Sxy150489 		ddi_prop_free(props);
35043526Sxy150489 
35053526Sxy150489 	} else {
35063526Sxy150489 		propval = defval;
35073526Sxy150489 	}
35083526Sxy150489 
35093526Sxy150489 	/*
35103526Sxy150489 	 * enforce limits
35113526Sxy150489 	 */
35123526Sxy150489 	if (propval > maxval) {
35133526Sxy150489 		propval = maxval;
35144919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
35153526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
35163526Sxy150489 		    propname, propval);
35173526Sxy150489 	}
35183526Sxy150489 
35193526Sxy150489 	if (propval < minval) {
35203526Sxy150489 		propval = minval;
35214919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
35223526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
35233526Sxy150489 		    propname, propval);
35243526Sxy150489 	}
35253526Sxy150489 
35263526Sxy150489 	return (propval);
35273526Sxy150489 }
35283526Sxy150489 
35293526Sxy150489 static boolean_t
35304061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
35313526Sxy150489 {
35324061Sxy150489 	uint16_t speed, duplex, phydata;
35334061Sxy150489 	boolean_t link_changed = B_FALSE;
35343526Sxy150489 	struct e1000_hw *hw;
35353526Sxy150489 	uint32_t reg_tarc;
35363526Sxy150489 
35374919Sxy150489 	hw = &Adapter->shared;
35383526Sxy150489 
35393526Sxy150489 	if (e1000g_link_up(Adapter)) {
35403526Sxy150489 		/*
35413526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
35423526Sxy150489 		 */
35434061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
35447426SChenliang.Xu@Sun.COM 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
35454061Sxy150489 			Adapter->link_speed = speed;
35464061Sxy150489 			Adapter->link_duplex = duplex;
35474061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
35484061Sxy150489 			link_changed = B_TRUE;
35494061Sxy150489 
35504061Sxy150489 			Adapter->tx_link_down_timeout = 0;
35514061Sxy150489 
35524919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
35534919Sxy150489 			    (hw->mac.type == e1000_82572)) {
35546735Scc210113 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
35554061Sxy150489 				if (speed == SPEED_1000)
35564061Sxy150489 					reg_tarc |= (1 << 21);
35574061Sxy150489 				else
35584061Sxy150489 					reg_tarc &= ~(1 << 21);
35596735Scc210113 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
35603526Sxy150489 			}
35613526Sxy150489 		}
35623526Sxy150489 		Adapter->smartspeed = 0;
35633526Sxy150489 	} else {
35644061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
35653526Sxy150489 			Adapter->link_speed = 0;
35663526Sxy150489 			Adapter->link_duplex = 0;
35674061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
35684061Sxy150489 			link_changed = B_TRUE;
35694061Sxy150489 
35703526Sxy150489 			/*
35713526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
35723526Sxy150489 			 * driver loses link disable auto master/slave
35733526Sxy150489 			 * resolution.
35743526Sxy150489 			 */
35754919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
35767426SChenliang.Xu@Sun.COM 				(void) e1000_read_phy_reg(hw,
35773526Sxy150489 				    PHY_1000T_CTRL, &phydata);
35783526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
35797426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
35803526Sxy150489 				    PHY_1000T_CTRL, phydata);
35813526Sxy150489 			}
35823526Sxy150489 		} else {
35833526Sxy150489 			e1000g_smartspeed(Adapter);
35843526Sxy150489 		}
35854061Sxy150489 
35865273Sgl147354 		if (Adapter->chip_state == E1000G_START) {
35874061Sxy150489 			if (Adapter->tx_link_down_timeout <
35884061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35894061Sxy150489 				Adapter->tx_link_down_timeout++;
35904061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
35914061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35924919Sxy150489 				e1000g_tx_clean(Adapter);
35934061Sxy150489 				Adapter->tx_link_down_timeout++;
35944061Sxy150489 			}
35954061Sxy150489 		}
35963526Sxy150489 	}
35973526Sxy150489 
35985273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
35995273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
36005273Sgl147354 
36014061Sxy150489 	return (link_changed);
36024061Sxy150489 }
36034061Sxy150489 
36046394Scc210113 /*
36056394Scc210113  * e1000g_reset_link - Using the link properties to setup the link
36066394Scc210113  */
36076394Scc210113 int
36086394Scc210113 e1000g_reset_link(struct e1000g *Adapter)
36096394Scc210113 {
36106394Scc210113 	struct e1000_mac_info *mac;
36116394Scc210113 	struct e1000_phy_info *phy;
36126394Scc210113 	boolean_t invalid;
36136394Scc210113 
36146394Scc210113 	mac = &Adapter->shared.mac;
36156394Scc210113 	phy = &Adapter->shared.phy;
36166394Scc210113 	invalid = B_FALSE;
36176394Scc210113 
36186394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
36196394Scc210113 		mac->autoneg = B_TRUE;
36206394Scc210113 		phy->autoneg_advertised = 0;
36216394Scc210113 
36226394Scc210113 		/*
36236394Scc210113 		 * 1000hdx is not supported for autonegotiation
36246394Scc210113 		 */
36256394Scc210113 		if (Adapter->param_adv_1000fdx == 1)
36266394Scc210113 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
36276394Scc210113 
36286394Scc210113 		if (Adapter->param_adv_100fdx == 1)
36296394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
36306394Scc210113 
36316394Scc210113 		if (Adapter->param_adv_100hdx == 1)
36326394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
36336394Scc210113 
36346394Scc210113 		if (Adapter->param_adv_10fdx == 1)
36356394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
36366394Scc210113 
36376394Scc210113 		if (Adapter->param_adv_10hdx == 1)
36386394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
36396394Scc210113 
36406394Scc210113 		if (phy->autoneg_advertised == 0)
36416394Scc210113 			invalid = B_TRUE;
36426394Scc210113 	} else {
36436394Scc210113 		mac->autoneg = B_FALSE;
36446394Scc210113 
36456394Scc210113 		/*
36466394Scc210113 		 * 1000fdx and 1000hdx are not supported for forced link
36476394Scc210113 		 */
36486394Scc210113 		if (Adapter->param_adv_100fdx == 1)
36496394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
36506394Scc210113 		else if (Adapter->param_adv_100hdx == 1)
36516394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
36526394Scc210113 		else if (Adapter->param_adv_10fdx == 1)
36536394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
36546394Scc210113 		else if (Adapter->param_adv_10hdx == 1)
36556394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
36566394Scc210113 		else
36576394Scc210113 			invalid = B_TRUE;
36586394Scc210113 
36596394Scc210113 	}
36606394Scc210113 
36616394Scc210113 	if (invalid) {
36626394Scc210113 		e1000g_log(Adapter, CE_WARN,
36636394Scc210113 		    "Invalid link sets. Setup link to"
36646394Scc210113 		    "support autonegotiation with all link capabilities.");
36656394Scc210113 		mac->autoneg = B_TRUE;
36666394Scc210113 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
36676394Scc210113 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
36686394Scc210113 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
36696394Scc210113 	}
36706394Scc210113 
36716394Scc210113 	return (e1000_setup_link(&Adapter->shared));
36726394Scc210113 }
36736394Scc210113 
36744061Sxy150489 static void
36754919Sxy150489 e1000g_local_timer(void *ws)
36764061Sxy150489 {
36774061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
36784061Sxy150489 	struct e1000_hw *hw;
36794061Sxy150489 	e1000g_ether_addr_t ether_addr;
36804061Sxy150489 	boolean_t link_changed;
36814061Sxy150489 
36824919Sxy150489 	hw = &Adapter->shared;
36834919Sxy150489 
36845273Sgl147354 	if (Adapter->chip_state == E1000G_ERROR) {
36855273Sgl147354 		Adapter->reset_count++;
36865273Sgl147354 		if (e1000g_global_reset(Adapter))
36875273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36885273Sgl147354 			    DDI_SERVICE_RESTORED);
36895273Sgl147354 		else
36905273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36915273Sgl147354 			    DDI_SERVICE_LOST);
36925273Sgl147354 		return;
36935273Sgl147354 	}
36945273Sgl147354 
36954061Sxy150489 	if (e1000g_stall_check(Adapter)) {
36964919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
36974061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
36985273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
36994061Sxy150489 		Adapter->reset_count++;
37007656SSherry.Moore@Sun.COM 		if (e1000g_reset_adapter(Adapter))
37015273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
37025273Sgl147354 			    DDI_SERVICE_RESTORED);
37035273Sgl147354 		else
37045273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
37055273Sgl147354 			    DDI_SERVICE_LOST);
37065273Sgl147354 		return;
37074061Sxy150489 	}
37084061Sxy150489 
37094061Sxy150489 	link_changed = B_FALSE;
37105082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
37114061Sxy150489 	if (Adapter->link_complete)
37124061Sxy150489 		link_changed = e1000g_link_check(Adapter);
37135082Syy150190 	rw_exit(&Adapter->chip_lock);
37144061Sxy150489 
37157133Scc210113 	if (link_changed)
37164061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
37177133Scc210113 
37187133Scc210113 	/*
37197133Scc210113 	 * Workaround for esb2. Data stuck in fifo on a link
37207133Scc210113 	 * down event. Reset the adapter to recover it.
37217133Scc210113 	 */
37227133Scc210113 	if (Adapter->esb2_workaround) {
37237133Scc210113 		Adapter->esb2_workaround = B_FALSE;
37247656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
37254139Sxy150489 	}
37264061Sxy150489 
37273526Sxy150489 	/*
37283526Sxy150489 	 * With 82571 controllers, any locally administered address will
37293526Sxy150489 	 * be overwritten when there is a reset on the other port.
37303526Sxy150489 	 * Detect this circumstance and correct it.
37313526Sxy150489 	 */
37324919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
37334919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
37344919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
37354919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
37363526Sxy150489 
37373526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
37383526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
37393526Sxy150489 
37404919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
37414919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
37424919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
37434919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
37444919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
37454919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
37464919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
37473526Sxy150489 		}
37483526Sxy150489 	}
37493526Sxy150489 
37503526Sxy150489 	/*
37514919Sxy150489 	 * Long TTL workaround for 82541/82547
37523526Sxy150489 	 */
37537426SChenliang.Xu@Sun.COM 	(void) e1000_igp_ttl_workaround_82547(hw);
37543526Sxy150489 
37553526Sxy150489 	/*
37563526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
37573526Sxy150489 	 * change the value in steps...
37583526Sxy150489 	 * These properties should only be set for 10/100
37593526Sxy150489 	 */
37606735Scc210113 	if ((hw->phy.media_type == e1000_media_type_copper) &&
37614061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
37624061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
37633526Sxy150489 		e1000_update_adaptive(hw);
37643526Sxy150489 	}
37653526Sxy150489 	/*
37663526Sxy150489 	 * Set Timer Interrupts
37673526Sxy150489 	 */
37684919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
37694919Sxy150489 
37705273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
37715273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
37725273Sgl147354 
37734919Sxy150489 	restart_watchdog_timer(Adapter);
37743526Sxy150489 }
37753526Sxy150489 
37764061Sxy150489 /*
37774061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
37784061Sxy150489  * is expired, which indicates the completion of the link setup. The link
37794061Sxy150489  * state will not be updated until the link setup is completed. And the
37804061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
37814061Sxy150489  * in this function. It will be updated in the local timer routine or the
37824061Sxy150489  * interrupt service routine after the interface is started (plumbed).
37834061Sxy150489  */
37843526Sxy150489 static void
37854061Sxy150489 e1000g_link_timer(void *arg)
37863526Sxy150489 {
37874061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
37883526Sxy150489 
37894919Sxy150489 	mutex_enter(&Adapter->link_lock);
37904061Sxy150489 	Adapter->link_complete = B_TRUE;
37914061Sxy150489 	Adapter->link_tid = 0;
37924919Sxy150489 	mutex_exit(&Adapter->link_lock);
37933526Sxy150489 }
37943526Sxy150489 
37953526Sxy150489 /*
37964919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
37974919Sxy150489  *
37984919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
37994919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
38003526Sxy150489  */
38013526Sxy150489 static void
38023526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
38033526Sxy150489 {
38043526Sxy150489 	int forced;
38054919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
38064919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
38073526Sxy150489 
38083526Sxy150489 	/*
38093526Sxy150489 	 * get value out of config file
38103526Sxy150489 	 */
38114919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
38123526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
38133526Sxy150489 
38143526Sxy150489 	switch (forced) {
38153526Sxy150489 	case GDIAG_10_HALF:
38163526Sxy150489 		/*
38173526Sxy150489 		 * Disable Auto Negotiation
38183526Sxy150489 		 */
38194919Sxy150489 		mac->autoneg = B_FALSE;
38204919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
38213526Sxy150489 		break;
38223526Sxy150489 	case GDIAG_10_FULL:
38233526Sxy150489 		/*
38243526Sxy150489 		 * Disable Auto Negotiation
38253526Sxy150489 		 */
38264919Sxy150489 		mac->autoneg = B_FALSE;
38274919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
38283526Sxy150489 		break;
38293526Sxy150489 	case GDIAG_100_HALF:
38303526Sxy150489 		/*
38313526Sxy150489 		 * Disable Auto Negotiation
38323526Sxy150489 		 */
38334919Sxy150489 		mac->autoneg = B_FALSE;
38344919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
38353526Sxy150489 		break;
38363526Sxy150489 	case GDIAG_100_FULL:
38373526Sxy150489 		/*
38383526Sxy150489 		 * Disable Auto Negotiation
38393526Sxy150489 		 */
38404919Sxy150489 		mac->autoneg = B_FALSE;
38414919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
38423526Sxy150489 		break;
38433526Sxy150489 	case GDIAG_1000_FULL:
38443526Sxy150489 		/*
38453526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
38463526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
38473526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
38483526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
38493526Sxy150489 		 * we are allowed to link without any negotiation.
38503526Sxy150489 		 */
38514919Sxy150489 		mac->autoneg = B_TRUE;
38524919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
38533526Sxy150489 		break;
38543526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
38554919Sxy150489 		mac->autoneg = B_TRUE;
38564919Sxy150489 		phy->autoneg_advertised =
38574919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
38584349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
38594349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
38603526Sxy150489 		break;
38613526Sxy150489 	}	/* switch */
38623526Sxy150489 }
38633526Sxy150489 
38643526Sxy150489 /*
38654919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
38664919Sxy150489  *
38674919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
38683526Sxy150489  */
38693526Sxy150489 static void
38703526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
38713526Sxy150489 {
38723526Sxy150489 	int max_frame;
38734919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
38744919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
38753526Sxy150489 
38763526Sxy150489 	/*
38773526Sxy150489 	 * get value out of config file
38783526Sxy150489 	 */
38794919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
38803526Sxy150489 
38813526Sxy150489 	switch (max_frame) {
38823526Sxy150489 	case 0:
38836394Scc210113 		Adapter->default_mtu = ETHERMTU;
38843526Sxy150489 		break;
38856394Scc210113 	/*
38866394Scc210113 	 * To avoid excessive memory allocation for rx buffers,
38876394Scc210113 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
38886394Scc210113 	 */
38893526Sxy150489 	case 1:
38906394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
38916394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38926394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38933526Sxy150489 		break;
38943526Sxy150489 	case 2:
38956394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
38966394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38976394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38983526Sxy150489 		break;
38993526Sxy150489 	case 3:
39006394Scc210113 		if (mac->type >= e1000_82571)
39016394Scc210113 			Adapter->default_mtu = MAXIMUM_MTU;
39023526Sxy150489 		else
39036394Scc210113 			Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
39046394Scc210113 			    sizeof (struct ether_vlan_header) - ETHERFCSL -
39056394Scc210113 			    E1000G_IPALIGNPRESERVEROOM;
39063526Sxy150489 		break;
39073526Sxy150489 	default:
39086394Scc210113 		Adapter->default_mtu = ETHERMTU;
39093526Sxy150489 		break;
39103526Sxy150489 	}	/* switch */
39113526Sxy150489 
39126735Scc210113 	Adapter->max_frame_size = Adapter->default_mtu +
39136394Scc210113 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
39146394Scc210113 
39153526Sxy150489 	/* ich8 does not do jumbo frames */
39164919Sxy150489 	if (mac->type == e1000_ich8lan) {
3917*8178SChenlu.Chen@Sun.COM 		Adapter->default_mtu = ETHERMTU;
39187133Scc210113 		Adapter->max_frame_size = ETHERMTU +
39197133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
39204919Sxy150489 	}
39214919Sxy150489 
39224919Sxy150489 	/* ich9 does not do jumbo frames on one phy type */
39234919Sxy150489 	if ((mac->type == e1000_ich9lan) &&
39244919Sxy150489 	    (phy->type == e1000_phy_ife)) {
3925*8178SChenlu.Chen@Sun.COM 		Adapter->default_mtu = ETHERMTU;
39267133Scc210113 		Adapter->max_frame_size = ETHERMTU +
39277133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
39283526Sxy150489 	}
39293526Sxy150489 }
39303526Sxy150489 
39313526Sxy150489 static void
39324919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
39333526Sxy150489 {
39344919Sxy150489 	Adapter->watchdog_tid =
39354919Sxy150489 	    timeout(e1000g_local_timer,
39363526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
39373526Sxy150489 }
39384919Sxy150489 #pragma inline(arm_watchdog_timer)
39394919Sxy150489 
39404919Sxy150489 static void
39414919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
39424919Sxy150489 {
39434919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39444919Sxy150489 
39454919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
39464919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
39474919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
39484919Sxy150489 		arm_watchdog_timer(Adapter);
39494919Sxy150489 	}
39504919Sxy150489 
39514919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39524919Sxy150489 }
39533526Sxy150489 
39543526Sxy150489 static void
39554919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
39563526Sxy150489 {
39573526Sxy150489 	timeout_id_t tid;
39583526Sxy150489 
39594919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39604919Sxy150489 
39614919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
39624919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
39634919Sxy150489 	tid = Adapter->watchdog_tid;
39644919Sxy150489 	Adapter->watchdog_tid = 0;
39654919Sxy150489 
39664919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39673526Sxy150489 
39683526Sxy150489 	if (tid != 0)
39693526Sxy150489 		(void) untimeout(tid);
39703526Sxy150489 }
39713526Sxy150489 
39723526Sxy150489 static void
39734919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
39743526Sxy150489 {
39754919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39764919Sxy150489 
39774919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
39784919Sxy150489 		if (!Adapter->watchdog_timer_started) {
39794919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
39804919Sxy150489 			arm_watchdog_timer(Adapter);
39813526Sxy150489 		}
39823526Sxy150489 	}
39833526Sxy150489 
39844919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39854919Sxy150489 }
39864919Sxy150489 
39874919Sxy150489 static void
39884919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
39894919Sxy150489 {
39904919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39914919Sxy150489 
39924919Sxy150489 	if (Adapter->watchdog_timer_started)
39934919Sxy150489 		arm_watchdog_timer(Adapter);
39944919Sxy150489 
39954919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39963526Sxy150489 }
39973526Sxy150489 
39983526Sxy150489 static void
39994919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
40003526Sxy150489 {
40014919Sxy150489 	timeout_id_t tid;
40024919Sxy150489 
40034919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
40044919Sxy150489 
40054919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
40064919Sxy150489 	tid = Adapter->watchdog_tid;
40074919Sxy150489 	Adapter->watchdog_tid = 0;
40084919Sxy150489 
40094919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
40104919Sxy150489 
40114919Sxy150489 	if (tid != 0)
40124919Sxy150489 		(void) untimeout(tid);
40133526Sxy150489 }
40143526Sxy150489 
40153526Sxy150489 static void
40164919Sxy150489 stop_link_timer(struct e1000g *Adapter)
40173526Sxy150489 {
40183526Sxy150489 	timeout_id_t tid;
40193526Sxy150489 
40204919Sxy150489 	/* Disable the link timer */
40214919Sxy150489 	mutex_enter(&Adapter->link_lock);
40224919Sxy150489 
40234919Sxy150489 	tid = Adapter->link_tid;
40244919Sxy150489 	Adapter->link_tid = 0;
40254919Sxy150489 
40264919Sxy150489 	mutex_exit(&Adapter->link_lock);
40274919Sxy150489 
40284919Sxy150489 	if (tid != 0)
40294919Sxy150489 		(void) untimeout(tid);
40304919Sxy150489 }
40314919Sxy150489 
40324919Sxy150489 static void
40334919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
40344919Sxy150489 {
40354919Sxy150489 	timeout_id_t tid;
40364919Sxy150489 
40374919Sxy150489 	/* Disable the tx timer for 82547 chipset */
40384919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
40394919Sxy150489 
40404919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
40414919Sxy150489 	tid = tx_ring->timer_id_82547;
40424919Sxy150489 	tx_ring->timer_id_82547 = 0;
40434919Sxy150489 
40444919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
40453526Sxy150489 
40463526Sxy150489 	if (tid != 0)
40473526Sxy150489 		(void) untimeout(tid);
40483526Sxy150489 }
40493526Sxy150489 
40503526Sxy150489 void
40514919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
40523526Sxy150489 {
40534919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
40544919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
40553526Sxy150489 }
40563526Sxy150489 
40573526Sxy150489 void
40584919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
40593526Sxy150489 {
40604919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
40615882Syy150190 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
40625882Syy150190 
40635882Syy150190 	if (Adapter->tx_intr_enable)
40645882Syy150190 		e1000g_mask_tx_interrupt(Adapter);
40653526Sxy150489 }
40663526Sxy150489 
40677656SSherry.Moore@Sun.COM /*
40687656SSherry.Moore@Sun.COM  * This routine is called by e1000g_quiesce(), therefore must not block.
40697656SSherry.Moore@Sun.COM  */
40703526Sxy150489 void
40714919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
40723526Sxy150489 {
40734919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
40743526Sxy150489 }
40753526Sxy150489 
40763526Sxy150489 void
40774919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
40783526Sxy150489 {
40795882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
40803526Sxy150489 }
40813526Sxy150489 
40823526Sxy150489 void
40834919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
40843526Sxy150489 {
40855882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
40863526Sxy150489 }
40873526Sxy150489 
40883526Sxy150489 static void
40894919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
40903526Sxy150489 {
40914919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
40923526Sxy150489 	uint16_t phy_status;
40933526Sxy150489 	uint16_t phy_ctrl;
40943526Sxy150489 
40953526Sxy150489 	/*
40963526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
40973526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
40983526Sxy150489 	 */
40994919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
41004919Sxy150489 	    !hw->mac.autoneg ||
41014919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
41023526Sxy150489 		return;
41033526Sxy150489 
41043526Sxy150489 	/*
41053526Sxy150489 	 * True if this is the first call of this function or after every
41063526Sxy150489 	 * 30 seconds of not having link
41073526Sxy150489 	 */
41084919Sxy150489 	if (Adapter->smartspeed == 0) {
41093526Sxy150489 		/*
41103526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
41113526Sxy150489 		 * assume back-to-back
41123526Sxy150489 		 */
41137426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
41143526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
41153526Sxy150489 			return;
41163526Sxy150489 
41177426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
41183526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
41193526Sxy150489 			return;
41203526Sxy150489 		/*
41213526Sxy150489 		 * We're assuming back-2-back because our status register
41223526Sxy150489 		 * insists! there's a fault in the master/slave
41233526Sxy150489 		 * relationship that was "negotiated"
41243526Sxy150489 		 */
41257426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
41263526Sxy150489 		/*
41273526Sxy150489 		 * Is the phy configured for manual configuration of
41283526Sxy150489 		 * master/slave?
41293526Sxy150489 		 */
41303526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
41313526Sxy150489 			/*
41323526Sxy150489 			 * Yes.  Then disable manual configuration (enable
41333526Sxy150489 			 * auto configuration) of master/slave
41343526Sxy150489 			 */
41353526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
41367426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw,
41373526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
41383526Sxy150489 			/*
41393526Sxy150489 			 * Effectively starting the clock
41403526Sxy150489 			 */
41414919Sxy150489 			Adapter->smartspeed++;
41423526Sxy150489 			/*
41433526Sxy150489 			 * Restart autonegotiation
41443526Sxy150489 			 */
41454919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
41464919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
41473526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
41483526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
41497426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
41504919Sxy150489 				    PHY_CONTROL, phy_ctrl);
41513526Sxy150489 			}
41523526Sxy150489 		}
41533526Sxy150489 		return;
41543526Sxy150489 		/*
41553526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
41563526Sxy150489 		 * you should reset the smartspeed counter once you obtain
41573526Sxy150489 		 * link
41583526Sxy150489 		 */
41594919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
41603526Sxy150489 		/*
41613526Sxy150489 		 * Yes.  Remember, we did at the start determine that
41623526Sxy150489 		 * there's a master/slave configuration fault, so we're
41633526Sxy150489 		 * still assuming there's someone on the other end, but we
41643526Sxy150489 		 * just haven't yet been able to talk to it. We then
41653526Sxy150489 		 * re-enable auto configuration of master/slave to see if
41663526Sxy150489 		 * we're running 2/3 pair cables.
41673526Sxy150489 		 */
41683526Sxy150489 		/*
41693526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
41703526Sxy150489 		 */
41717426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
41723526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
41737426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
41743526Sxy150489 		/*
41753526Sxy150489 		 * Restart autoneg with phy enabled for manual
41763526Sxy150489 		 * configuration of master/slave
41773526Sxy150489 		 */
41784919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
41794919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
41803526Sxy150489 			phy_ctrl |=
41813526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
41827426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
41833526Sxy150489 		}
41843526Sxy150489 		/*
41853526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
41863526Sxy150489 		 * link as a result.
41873526Sxy150489 		 */
41883526Sxy150489 	}
41893526Sxy150489 	/*
41903526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
41913526Sxy150489 	 * seconds)
41923526Sxy150489 	 */
41934919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
41944919Sxy150489 		Adapter->smartspeed = 0;
41953526Sxy150489 }
41963526Sxy150489 
41973526Sxy150489 static boolean_t
41983526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
41993526Sxy150489 {
42003526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
42013526Sxy150489 	const uint8_t addr_test2[6] =
42023526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
42033526Sxy150489 
42043526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
42053526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
42063526Sxy150489 		return (B_FALSE);
42073526Sxy150489 
42083526Sxy150489 	return (B_TRUE);
42093526Sxy150489 }
42103526Sxy150489 
42113526Sxy150489 /*
42124919Sxy150489  * e1000g_stall_check - check for tx stall
42134919Sxy150489  *
42144919Sxy150489  * This function checks if the adapter is stalled (in transmit).
42154919Sxy150489  *
42164919Sxy150489  * It is called each time the watchdog timeout is invoked.
42174919Sxy150489  * If the transmit descriptor reclaim continuously fails,
42184919Sxy150489  * the watchdog value will increment by 1. If the watchdog
42194919Sxy150489  * value exceeds the threshold, the adapter is assumed to
42204919Sxy150489  * have stalled and need to be reset.
42213526Sxy150489  */
42223526Sxy150489 static boolean_t
42233526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
42243526Sxy150489 {
42254919Sxy150489 	e1000g_tx_ring_t *tx_ring;
42264919Sxy150489 
42274919Sxy150489 	tx_ring = Adapter->tx_ring;
42284919Sxy150489 
42294061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
42303526Sxy150489 		return (B_FALSE);
42313526Sxy150489 
42324919Sxy150489 	if (tx_ring->recycle_fail > 0)
42334919Sxy150489 		tx_ring->stall_watchdog++;
42343526Sxy150489 	else
42354919Sxy150489 		tx_ring->stall_watchdog = 0;
42364919Sxy150489 
42374919Sxy150489 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
42383526Sxy150489 		return (B_FALSE);
42393526Sxy150489 
42404919Sxy150489 	tx_ring->stall_watchdog = 0;
42414919Sxy150489 	tx_ring->recycle_fail = 0;
42424919Sxy150489 
42433526Sxy150489 	return (B_TRUE);
42443526Sxy150489 }
42453526Sxy150489 
42464919Sxy150489 #ifdef E1000G_DEBUG
42473526Sxy150489 static enum ioc_reply
42483526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
42493526Sxy150489 {
42503526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
42513526Sxy150489 	e1000g_peekpoke_t *ppd;
42523526Sxy150489 	uint64_t mem_va;
42533526Sxy150489 	uint64_t maxoff;
42543526Sxy150489 	boolean_t peek;
42553526Sxy150489 
42563526Sxy150489 	switch (iocp->ioc_cmd) {
42573526Sxy150489 
42583526Sxy150489 	case E1000G_IOC_REG_PEEK:
42593526Sxy150489 		peek = B_TRUE;
42603526Sxy150489 		break;
42613526Sxy150489 
42623526Sxy150489 	case E1000G_IOC_REG_POKE:
42633526Sxy150489 		peek = B_FALSE;
42643526Sxy150489 		break;
42653526Sxy150489 
42663526Sxy150489 	deault:
42674919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42684349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
42694349Sxy150489 		    iocp->ioc_cmd);
42703526Sxy150489 		return (IOC_INVAL);
42713526Sxy150489 	}
42723526Sxy150489 
42733526Sxy150489 	/*
42743526Sxy150489 	 * Validate format of ioctl
42753526Sxy150489 	 */
42763526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
42773526Sxy150489 		return (IOC_INVAL);
42783526Sxy150489 	if (mp->b_cont == NULL)
42793526Sxy150489 		return (IOC_INVAL);
42803526Sxy150489 
42817426SChenliang.Xu@Sun.COM 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
42823526Sxy150489 
42833526Sxy150489 	/*
42843526Sxy150489 	 * Validate request parameters
42853526Sxy150489 	 */
42863526Sxy150489 	switch (ppd->pp_acc_space) {
42873526Sxy150489 
42883526Sxy150489 	default:
42894919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42904349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
42914349Sxy150489 		    ppd->pp_acc_space);
42923526Sxy150489 		return (IOC_INVAL);
42933526Sxy150489 
42943526Sxy150489 	case E1000G_PP_SPACE_REG:
42953526Sxy150489 		/*
42963526Sxy150489 		 * Memory-mapped I/O space
42973526Sxy150489 		 */
42983526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
42993526Sxy150489 		if (ppd->pp_acc_size != 4)
43003526Sxy150489 			return (IOC_INVAL);
43013526Sxy150489 
43023526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
43033526Sxy150489 			return (IOC_INVAL);
43043526Sxy150489 
43053526Sxy150489 		mem_va = 0;
43063526Sxy150489 		maxoff = 0x10000;
43073526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
43083526Sxy150489 		break;
43093526Sxy150489 
43103526Sxy150489 	case E1000G_PP_SPACE_E1000G:
43113526Sxy150489 		/*
43123526Sxy150489 		 * E1000g data structure!
43133526Sxy150489 		 */
43143526Sxy150489 		mem_va = (uintptr_t)e1000gp;
43153526Sxy150489 		maxoff = sizeof (struct e1000g);
43163526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
43173526Sxy150489 		break;
43183526Sxy150489 
43193526Sxy150489 	}
43203526Sxy150489 
43213526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
43223526Sxy150489 		return (IOC_INVAL);
43233526Sxy150489 
43243526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
43253526Sxy150489 		return (IOC_INVAL);
43263526Sxy150489 
43273526Sxy150489 	/*
43283526Sxy150489 	 * All OK - go!
43293526Sxy150489 	 */
43303526Sxy150489 	ppd->pp_acc_offset += mem_va;
43313526Sxy150489 	(*ppfn)(e1000gp, ppd);
43323526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
43333526Sxy150489 }
43343526Sxy150489 
43353526Sxy150489 static void
43363526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43373526Sxy150489 {
43383526Sxy150489 	ddi_acc_handle_t handle;
43393526Sxy150489 	uint32_t *regaddr;
43403526Sxy150489 
43414919Sxy150489 	handle = e1000gp->osdep.reg_handle;
43427426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
43437426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
43443526Sxy150489 
43453526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
43463526Sxy150489 }
43473526Sxy150489 
43483526Sxy150489 static void
43493526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43503526Sxy150489 {
43513526Sxy150489 	ddi_acc_handle_t handle;
43523526Sxy150489 	uint32_t *regaddr;
43533526Sxy150489 	uint32_t value;
43543526Sxy150489 
43554919Sxy150489 	handle = e1000gp->osdep.reg_handle;
43567426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
43577426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
43583526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
43593526Sxy150489 
43603526Sxy150489 	ddi_put32(handle, regaddr, value);
43613526Sxy150489 }
43623526Sxy150489 
43633526Sxy150489 static void
43643526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43653526Sxy150489 {
43663526Sxy150489 	uint64_t value;
43673526Sxy150489 	void *vaddr;
43683526Sxy150489 
43693526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
43703526Sxy150489 
43713526Sxy150489 	switch (ppd->pp_acc_size) {
43723526Sxy150489 	case 1:
43733526Sxy150489 		value = *(uint8_t *)vaddr;
43743526Sxy150489 		break;
43753526Sxy150489 
43763526Sxy150489 	case 2:
43773526Sxy150489 		value = *(uint16_t *)vaddr;
43783526Sxy150489 		break;
43793526Sxy150489 
43803526Sxy150489 	case 4:
43813526Sxy150489 		value = *(uint32_t *)vaddr;
43823526Sxy150489 		break;
43833526Sxy150489 
43843526Sxy150489 	case 8:
43853526Sxy150489 		value = *(uint64_t *)vaddr;
43863526Sxy150489 		break;
43873526Sxy150489 	}
43883526Sxy150489 
43894919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
43904349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
43914349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
43923526Sxy150489 
43933526Sxy150489 	ppd->pp_acc_data = value;
43943526Sxy150489 }
43953526Sxy150489 
43963526Sxy150489 static void
43973526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43983526Sxy150489 {
43993526Sxy150489 	uint64_t value;
44003526Sxy150489 	void *vaddr;
44013526Sxy150489 
44023526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
44033526Sxy150489 	value = ppd->pp_acc_data;
44043526Sxy150489 
44054919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
44064349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
44074349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
44083526Sxy150489 
44093526Sxy150489 	switch (ppd->pp_acc_size) {
44103526Sxy150489 	case 1:
44113526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
44123526Sxy150489 		break;
44133526Sxy150489 
44143526Sxy150489 	case 2:
44153526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
44163526Sxy150489 		break;
44173526Sxy150489 
44183526Sxy150489 	case 4:
44193526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
44203526Sxy150489 		break;
44213526Sxy150489 
44223526Sxy150489 	case 8:
44233526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
44243526Sxy150489 		break;
44253526Sxy150489 	}
44263526Sxy150489 }
44274919Sxy150489 #endif
44283526Sxy150489 
44293526Sxy150489 /*
44303526Sxy150489  * Loopback Support
44313526Sxy150489  */
44323526Sxy150489 static lb_property_t lb_normal =
44333526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
44343526Sxy150489 static lb_property_t lb_external1000 =
44353526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
44363526Sxy150489 static lb_property_t lb_external100 =
44373526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
44383526Sxy150489 static lb_property_t lb_external10 =
44393526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
44403526Sxy150489 static lb_property_t lb_phy =
44413526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
44423526Sxy150489 
44433526Sxy150489 static enum ioc_reply
44443526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
44453526Sxy150489 {
44463526Sxy150489 	lb_info_sz_t *lbsp;
44473526Sxy150489 	lb_property_t *lbpp;
44483526Sxy150489 	struct e1000_hw *hw;
44493526Sxy150489 	uint32_t *lbmp;
44503526Sxy150489 	uint32_t size;
44513526Sxy150489 	uint32_t value;
44523526Sxy150489 
44534919Sxy150489 	hw = &Adapter->shared;
44543526Sxy150489 
44553526Sxy150489 	if (mp->b_cont == NULL)
44563526Sxy150489 		return (IOC_INVAL);
44573526Sxy150489 
44587133Scc210113 	if (!e1000g_check_loopback_support(hw)) {
44597133Scc210113 		e1000g_log(NULL, CE_WARN,
44607133Scc210113 		    "Loopback is not supported on e1000g%d", Adapter->instance);
44617133Scc210113 		return (IOC_INVAL);
44627133Scc210113 	}
44637133Scc210113 
44643526Sxy150489 	switch (iocp->ioc_cmd) {
44653526Sxy150489 	default:
44663526Sxy150489 		return (IOC_INVAL);
44673526Sxy150489 
44683526Sxy150489 	case LB_GET_INFO_SIZE:
44693526Sxy150489 		size = sizeof (lb_info_sz_t);
44703526Sxy150489 		if (iocp->ioc_count != size)
44713526Sxy150489 			return (IOC_INVAL);
44723526Sxy150489 
44735082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
44745082Syy150190 		e1000g_get_phy_state(Adapter);
44755082Syy150190 
44765082Syy150190 		/*
44775082Syy150190 		 * Workaround for hardware faults. In order to get a stable
44785082Syy150190 		 * state of phy, we will wait for a specific interval and
44795082Syy150190 		 * try again. The time delay is an experiential value based
44805082Syy150190 		 * on our testing.
44815082Syy150190 		 */
44825082Syy150190 		msec_delay(100);
44835082Syy150190 		e1000g_get_phy_state(Adapter);
44845082Syy150190 		rw_exit(&Adapter->chip_lock);
44853526Sxy150489 
44863526Sxy150489 		value = sizeof (lb_normal);
44875082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44885082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44896735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44906735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44913526Sxy150489 			value += sizeof (lb_phy);
44924919Sxy150489 			switch (hw->mac.type) {
44933526Sxy150489 			case e1000_82571:
44943526Sxy150489 			case e1000_82572:
44957133Scc210113 			case e1000_80003es2lan:
44963526Sxy150489 				value += sizeof (lb_external1000);
44973526Sxy150489 				break;
44983526Sxy150489 			}
44993526Sxy150489 		}
45005082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
45015082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
45023526Sxy150489 			value += sizeof (lb_external100);
45035082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
45043526Sxy150489 			value += sizeof (lb_external10);
45053526Sxy150489 
45067426SChenliang.Xu@Sun.COM 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
45073526Sxy150489 		*lbsp = value;
45083526Sxy150489 		break;
45093526Sxy150489 
45103526Sxy150489 	case LB_GET_INFO:
45113526Sxy150489 		value = sizeof (lb_normal);
45125082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
45135082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
45146735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
45156735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
45163526Sxy150489 			value += sizeof (lb_phy);
45174919Sxy150489 			switch (hw->mac.type) {
45183526Sxy150489 			case e1000_82571:
45193526Sxy150489 			case e1000_82572:
45207133Scc210113 			case e1000_80003es2lan:
45213526Sxy150489 				value += sizeof (lb_external1000);
45223526Sxy150489 				break;
45233526Sxy150489 			}
45243526Sxy150489 		}
45255082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
45265082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
45273526Sxy150489 			value += sizeof (lb_external100);
45285082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
45293526Sxy150489 			value += sizeof (lb_external10);
45303526Sxy150489 
45313526Sxy150489 		size = value;
45323526Sxy150489 		if (iocp->ioc_count != size)
45333526Sxy150489 			return (IOC_INVAL);
45343526Sxy150489 
45353526Sxy150489 		value = 0;
45367426SChenliang.Xu@Sun.COM 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
45373526Sxy150489 		lbpp[value++] = lb_normal;
45385082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
45395082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
45406735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
45416735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
45423526Sxy150489 			lbpp[value++] = lb_phy;
45434919Sxy150489 			switch (hw->mac.type) {
45443526Sxy150489 			case e1000_82571:
45453526Sxy150489 			case e1000_82572:
45467133Scc210113 			case e1000_80003es2lan:
45473526Sxy150489 				lbpp[value++] = lb_external1000;
45483526Sxy150489 				break;
45493526Sxy150489 			}
45503526Sxy150489 		}
45515082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
45525082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
45533526Sxy150489 			lbpp[value++] = lb_external100;
45545082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
45553526Sxy150489 			lbpp[value++] = lb_external10;
45563526Sxy150489 		break;
45573526Sxy150489 
45583526Sxy150489 	case LB_GET_MODE:
45593526Sxy150489 		size = sizeof (uint32_t);
45603526Sxy150489 		if (iocp->ioc_count != size)
45613526Sxy150489 			return (IOC_INVAL);
45623526Sxy150489 
45637426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45643526Sxy150489 		*lbmp = Adapter->loopback_mode;
45653526Sxy150489 		break;
45663526Sxy150489 
45673526Sxy150489 	case LB_SET_MODE:
45683526Sxy150489 		size = 0;
45693526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
45703526Sxy150489 			return (IOC_INVAL);
45713526Sxy150489 
45727426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45733526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
45743526Sxy150489 			return (IOC_INVAL);
45753526Sxy150489 		break;
45763526Sxy150489 	}
45773526Sxy150489 
45783526Sxy150489 	iocp->ioc_count = size;
45793526Sxy150489 	iocp->ioc_error = 0;
45803526Sxy150489 
45815273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
45825273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
45835273Sgl147354 		return (IOC_INVAL);
45845273Sgl147354 	}
45855273Sgl147354 
45863526Sxy150489 	return (IOC_REPLY);
45873526Sxy150489 }
45883526Sxy150489 
45893526Sxy150489 static boolean_t
45907133Scc210113 e1000g_check_loopback_support(struct e1000_hw *hw)
45917133Scc210113 {
45927133Scc210113 	switch (hw->mac.type) {
45937133Scc210113 	case e1000_82540:
45947133Scc210113 	case e1000_82545:
45957133Scc210113 	case e1000_82545_rev_3:
45967133Scc210113 	case e1000_82546:
45977133Scc210113 	case e1000_82546_rev_3:
45987133Scc210113 	case e1000_82541:
45997133Scc210113 	case e1000_82541_rev_2:
46007133Scc210113 	case e1000_82547:
46017133Scc210113 	case e1000_82547_rev_2:
46027133Scc210113 	case e1000_82571:
46037133Scc210113 	case e1000_82572:
46047133Scc210113 	case e1000_82573:
46057133Scc210113 	case e1000_80003es2lan:
46067133Scc210113 		return (B_TRUE);
46077133Scc210113 	}
46087133Scc210113 	return (B_FALSE);
46097133Scc210113 }
46107133Scc210113 
46117133Scc210113 static boolean_t
46123526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
46133526Sxy150489 {
46143526Sxy150489 	struct e1000_hw *hw;
46153526Sxy150489 	int i, times;
46165082Syy150190 	boolean_t link_up;
46173526Sxy150489 
46183526Sxy150489 	if (mode == Adapter->loopback_mode)
46193526Sxy150489 		return (B_TRUE);
46203526Sxy150489 
46214919Sxy150489 	hw = &Adapter->shared;
46223526Sxy150489 	times = 0;
46233526Sxy150489 
46245082Syy150190 	Adapter->loopback_mode = mode;
46255082Syy150190 
46265082Syy150190 	if (mode == E1000G_LB_NONE) {
46273526Sxy150489 		/* Reset the chip */
46286735Scc210113 		hw->phy.autoneg_wait_to_complete = B_TRUE;
46297656SSherry.Moore@Sun.COM 		(void) e1000g_reset_adapter(Adapter);
46306735Scc210113 		hw->phy.autoneg_wait_to_complete = B_FALSE;
46315082Syy150190 		return (B_TRUE);
46325082Syy150190 	}
46335082Syy150190 
46345082Syy150190 again:
46355082Syy150190 
46365082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
46375082Syy150190 
46385082Syy150190 	switch (mode) {
46395082Syy150190 	default:
46405082Syy150190 		rw_exit(&Adapter->chip_lock);
46415082Syy150190 		return (B_FALSE);
46423526Sxy150489 
46433526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
46443526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
46453526Sxy150489 		break;
46463526Sxy150489 
46473526Sxy150489 	case E1000G_LB_EXTERNAL_100:
46483526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
46493526Sxy150489 		break;
46503526Sxy150489 
46513526Sxy150489 	case E1000G_LB_EXTERNAL_10:
46523526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
46533526Sxy150489 		break;
46543526Sxy150489 
46553526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
46563526Sxy150489 		e1000g_set_internal_loopback(Adapter);
46573526Sxy150489 		break;
46583526Sxy150489 	}
46593526Sxy150489 
46603526Sxy150489 	times++;
46613526Sxy150489 
46625858Scc210113 	rw_exit(&Adapter->chip_lock);
46635858Scc210113 
46645082Syy150190 	/* Wait for link up */
46655082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
46665082Syy150190 		msec_delay(100);
46675082Syy150190 
46685858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
46695858Scc210113 
46705082Syy150190 	link_up = e1000g_link_up(Adapter);
46715082Syy150190 
46725082Syy150190 	rw_exit(&Adapter->chip_lock);
46735082Syy150190 
46745082Syy150190 	if (!link_up) {
46755082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46765082Syy150190 		    "Failed to get the link up");
46775082Syy150190 		if (times < 2) {
46785082Syy150190 			/* Reset the link */
46794919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46805082Syy150190 			    "Reset the link ...");
46817656SSherry.Moore@Sun.COM 			(void) e1000g_reset_adapter(Adapter);
46825082Syy150190 			goto again;
46833526Sxy150489 		}
46843526Sxy150489 	}
46853526Sxy150489 
46863526Sxy150489 	return (B_TRUE);
46873526Sxy150489 }
46883526Sxy150489 
46893526Sxy150489 /*
46903526Sxy150489  * The following loopback settings are from Intel's technical
46913526Sxy150489  * document - "How To Loopback". All the register settings and
46923526Sxy150489  * time delay values are directly inherited from the document
46933526Sxy150489  * without more explanations available.
46943526Sxy150489  */
46953526Sxy150489 static void
46963526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
46973526Sxy150489 {
46983526Sxy150489 	struct e1000_hw *hw;
46993526Sxy150489 	uint32_t ctrl;
47003526Sxy150489 	uint32_t status;
47013526Sxy150489 	uint16_t phy_ctrl;
47025082Syy150190 	uint32_t txcw;
47033526Sxy150489 
47044919Sxy150489 	hw = &Adapter->shared;
47053526Sxy150489 
47063526Sxy150489 	/* Disable Smart Power Down */
47073526Sxy150489 	phy_spd_state(hw, B_FALSE);
47083526Sxy150489 
47097426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
47103526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
47113526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
47123526Sxy150489 
47134919Sxy150489 	switch (hw->mac.type) {
47143526Sxy150489 	case e1000_82540:
47153526Sxy150489 	case e1000_82545:
47163526Sxy150489 	case e1000_82545_rev_3:
47173526Sxy150489 	case e1000_82546:
47183526Sxy150489 	case e1000_82546_rev_3:
47193526Sxy150489 	case e1000_82573:
47203526Sxy150489 		/* Auto-MDI/MDIX off */
47217426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
47223526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
47237426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
47244349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
47253526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
47267426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
47274349Sxy150489 		    phy_ctrl | MII_CR_RESET);
47285082Syy150190 		/*
47295082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
47305082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
47315082Syy150190 		 * background.
47325082Syy150190 		 */
47337426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
47347426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
47357426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
47367426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
47373526Sxy150489 		break;
47387133Scc210113 	case e1000_80003es2lan:
47397133Scc210113 		/* Force Link Up */
47407426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
47417426SChenliang.Xu@Sun.COM 		    0x1CC);
47427133Scc210113 		/* Sets PCS loopback at 1Gbs */
47437426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
47447426SChenliang.Xu@Sun.COM 		    0x1046);
47457133Scc210113 		break;
47463526Sxy150489 	}
47473526Sxy150489 
47483526Sxy150489 	/* Set loopback */
47497426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
47503526Sxy150489 
47513526Sxy150489 	msec_delay(250);
47523526Sxy150489 
47533526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
47544919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
47553526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
47563526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
47574349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
47584349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
47594349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
47603526Sxy150489 
47614919Sxy150489 	switch (hw->mac.type) {
47623526Sxy150489 	case e1000_82540:
47633526Sxy150489 	case e1000_82545:
47643526Sxy150489 	case e1000_82545_rev_3:
47653526Sxy150489 	case e1000_82546:
47663526Sxy150489 	case e1000_82546_rev_3:
47673526Sxy150489 		/*
47683526Sxy150489 		 * For some serdes we'll need to commit the writes now
47693526Sxy150489 		 * so that the status is updated on link
47703526Sxy150489 		 */
47716735Scc210113 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
47724919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47733526Sxy150489 			msec_delay(100);
47744919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
47753526Sxy150489 		}
47763526Sxy150489 
47776735Scc210113 		if (hw->phy.media_type == e1000_media_type_copper) {
47783526Sxy150489 			/* Invert Loss of Signal */
47793526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
47803526Sxy150489 		} else {
47813526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
47824919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47833526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
47843526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
47853526Sxy150489 		}
47867133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47873526Sxy150489 		break;
47883526Sxy150489 
47893526Sxy150489 	case e1000_82571:
47903526Sxy150489 	case e1000_82572:
47915082Syy150190 		/*
47925082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
47935082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
47945082Syy150190 		 * using SerDes analog loopback.
47955082Syy150190 		 */
47966735Scc210113 		if (hw->phy.media_type != e1000_media_type_copper) {
47974919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47985082Syy150190 			/* Set ILOS on fiber nic if half duplex is detected */
47995082Syy150190 			if (((status & E1000_STATUS_LU) == 0) ||
48005082Syy150190 			    ((status & E1000_STATUS_FD) == 0) ||
48016735Scc210113 			    (hw->phy.media_type ==
48025082Syy150190 			    e1000_media_type_internal_serdes))
48033526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
48045082Syy150190 
48055082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
48065082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
48075082Syy150190 			txcw &= ~((uint32_t)1 << 31);
48085082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
48095082Syy150190 
48105082Syy150190 			/*
48115082Syy150190 			 * Write 0x410 to Serdes Control register
48125082Syy150190 			 * to enable Serdes analog loopback
48135082Syy150190 			 */
48145082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
48155082Syy150190 			msec_delay(10);
48163526Sxy150489 		}
48177133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48183526Sxy150489 		break;
48193526Sxy150489 
48203526Sxy150489 	case e1000_82573:
48213526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
48227133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48233526Sxy150489 		break;
48243526Sxy150489 	}
48253526Sxy150489 }
48263526Sxy150489 
48273526Sxy150489 static void
48283526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
48293526Sxy150489 {
48303526Sxy150489 	struct e1000_hw *hw;
48313526Sxy150489 	uint32_t rctl;
48323526Sxy150489 	uint32_t ctrl_ext;
48333526Sxy150489 	uint32_t ctrl;
48343526Sxy150489 	uint32_t status;
48353526Sxy150489 	uint32_t txcw;
48367133Scc210113 	uint16_t phydata;
48373526Sxy150489 
48384919Sxy150489 	hw = &Adapter->shared;
48393526Sxy150489 
48403526Sxy150489 	/* Disable Smart Power Down */
48413526Sxy150489 	phy_spd_state(hw, B_FALSE);
48423526Sxy150489 
48437133Scc210113 	switch (hw->mac.type) {
48447133Scc210113 	case e1000_82571:
48457133Scc210113 	case e1000_82572:
48467133Scc210113 		switch (hw->phy.media_type) {
48477133Scc210113 		case e1000_media_type_copper:
48487133Scc210113 			/* Force link up (Must be done before the PHY writes) */
48497133Scc210113 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
48507133Scc210113 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
48517133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48527133Scc210113 
48537133Scc210113 			rctl = E1000_READ_REG(hw, E1000_RCTL);
48547133Scc210113 			rctl |= (E1000_RCTL_EN |
48557133Scc210113 			    E1000_RCTL_SBP |
48567133Scc210113 			    E1000_RCTL_UPE |
48577133Scc210113 			    E1000_RCTL_MPE |
48587133Scc210113 			    E1000_RCTL_LPE |
48597133Scc210113 			    E1000_RCTL_BAM);		/* 0x803E */
48607133Scc210113 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
48617133Scc210113 
48627133Scc210113 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
48637133Scc210113 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
48647133Scc210113 			    E1000_CTRL_EXT_SDP6_DATA |
48657133Scc210113 			    E1000_CTRL_EXT_SDP7_DATA |
48667133Scc210113 			    E1000_CTRL_EXT_SDP4_DIR |
48677133Scc210113 			    E1000_CTRL_EXT_SDP6_DIR |
48687133Scc210113 			    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
48697133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
48707133Scc210113 
48717133Scc210113 			/*
48727133Scc210113 			 * This sequence tunes the PHY's SDP and no customer
48737133Scc210113 			 * settable values. For background, see comments above
48747133Scc210113 			 * e1000g_set_internal_loopback().
48757133Scc210113 			 */
48767426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
48777133Scc210113 			msec_delay(10);
48787426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
48797426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
48807426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
48817426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
48827426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
48837426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
48847426SChenliang.Xu@Sun.COM 
48857426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
48867426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
48877426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
48887426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
48897426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
48907133Scc210113 
48917133Scc210113 			msec_delay(50);
48927133Scc210113 			break;
48937133Scc210113 		case e1000_media_type_fiber:
48947133Scc210113 		case e1000_media_type_internal_serdes:
48957133Scc210113 			status = E1000_READ_REG(hw, E1000_STATUS);
48967133Scc210113 			if (((status & E1000_STATUS_LU) == 0) ||
48977133Scc210113 			    (hw->phy.media_type ==
48987133Scc210113 			    e1000_media_type_internal_serdes)) {
48997133Scc210113 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
49007133Scc210113 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
49017133Scc210113 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49027133Scc210113 			}
49037133Scc210113 
49047133Scc210113 			/* Disable autoneg by setting bit 31 of TXCW to zero */
49057133Scc210113 			txcw = E1000_READ_REG(hw, E1000_TXCW);
49067133Scc210113 			txcw &= ~((uint32_t)1 << 31);
49077133Scc210113 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
49087133Scc210113 
49097133Scc210113 			/*
49107133Scc210113 			 * Write 0x410 to Serdes Control register
49117133Scc210113 			 * to enable Serdes analog loopback
49127133Scc210113 			 */
49137133Scc210113 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
49147133Scc210113 			msec_delay(10);
49157133Scc210113 			break;
49167133Scc210113 		default:
49177133Scc210113 			break;
49187133Scc210113 		}
49193526Sxy150489 		break;
49207133Scc210113 	case e1000_80003es2lan:
49217426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
49227426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
49237426SChenliang.Xu@Sun.COM 		    phydata | (1 << 5));
49247133Scc210113 		Adapter->param_adv_autoneg = 1;
49257133Scc210113 		Adapter->param_adv_1000fdx = 1;
49267426SChenliang.Xu@Sun.COM 		(void) e1000g_reset_link(Adapter);
49273526Sxy150489 		break;
49283526Sxy150489 	}
49293526Sxy150489 }
49303526Sxy150489 
49313526Sxy150489 static void
49323526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
49333526Sxy150489 {
49343526Sxy150489 	struct e1000_hw *hw;
49353526Sxy150489 	uint32_t ctrl;
49363526Sxy150489 	uint16_t phy_ctrl;
49373526Sxy150489 
49384919Sxy150489 	hw = &Adapter->shared;
49393526Sxy150489 
49403526Sxy150489 	/* Disable Smart Power Down */
49413526Sxy150489 	phy_spd_state(hw, B_FALSE);
49423526Sxy150489 
49433526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
49444349Sxy150489 	    MII_CR_SPEED_100);
49453526Sxy150489 
49463526Sxy150489 	/* Force 100/FD, reset PHY */
49477426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49484349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
49493526Sxy150489 	msec_delay(10);
49503526Sxy150489 
49513526Sxy150489 	/* Force 100/FD */
49527426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49534349Sxy150489 	    phy_ctrl);			/* 0x2100 */
49543526Sxy150489 	msec_delay(10);
49553526Sxy150489 
49563526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
49574919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49583526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49593526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
49604349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
49614349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49624349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
49634349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49643526Sxy150489 
49654919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49663526Sxy150489 }
49673526Sxy150489 
49683526Sxy150489 static void
49693526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
49703526Sxy150489 {
49713526Sxy150489 	struct e1000_hw *hw;
49723526Sxy150489 	uint32_t ctrl;
49733526Sxy150489 	uint16_t phy_ctrl;
49743526Sxy150489 
49754919Sxy150489 	hw = &Adapter->shared;
49763526Sxy150489 
49773526Sxy150489 	/* Disable Smart Power Down */
49783526Sxy150489 	phy_spd_state(hw, B_FALSE);
49793526Sxy150489 
49803526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
49814349Sxy150489 	    MII_CR_SPEED_10);
49823526Sxy150489 
49833526Sxy150489 	/* Force 10/FD, reset PHY */
49847426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49854349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
49863526Sxy150489 	msec_delay(10);
49873526Sxy150489 
49883526Sxy150489 	/* Force 10/FD */
49897426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49904349Sxy150489 	    phy_ctrl);			/* 0x0100 */
49913526Sxy150489 	msec_delay(10);
49923526Sxy150489 
49933526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
49944919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49953526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49963526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
49974349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
49984349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49994349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
50004349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
50013526Sxy150489 
50024919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
50033526Sxy150489 }
50043526Sxy150489 
50053526Sxy150489 #ifdef __sparc
50063526Sxy150489 static boolean_t
50073526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
50083526Sxy150489 {
50094919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
50103526Sxy150489 	uchar_t *bytes;
50113526Sxy150489 	struct ether_addr sysaddr;
50123526Sxy150489 	uint_t nelts;
50133526Sxy150489 	int err;
50143526Sxy150489 	boolean_t found = B_FALSE;
50153526Sxy150489 
50163526Sxy150489 	/*
50173526Sxy150489 	 * The "vendor's factory-set address" may already have
50183526Sxy150489 	 * been extracted from the chip, but if the property
50193526Sxy150489 	 * "local-mac-address" is set we use that instead.
50203526Sxy150489 	 *
50213526Sxy150489 	 * We check whether it looks like an array of 6
50223526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
50233526Sxy150489 	 * make sense of it this way, we'll ignore it.
50243526Sxy150489 	 */
50253526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
50263526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
50273526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
50283526Sxy150489 		if (nelts == ETHERADDRL) {
50293526Sxy150489 			while (nelts--)
50304919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
50313526Sxy150489 			found = B_TRUE;
50323526Sxy150489 		}
50333526Sxy150489 		ddi_prop_free(bytes);
50343526Sxy150489 	}
50353526Sxy150489 
50363526Sxy150489 	/*
50373526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
50383526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
50393526Sxy150489 	 */
50403526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
50413526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
50423526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
50433526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
50444919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
50453526Sxy150489 				found = B_TRUE;
50463526Sxy150489 			}
50473526Sxy150489 		}
50483526Sxy150489 		ddi_prop_free(bytes);
50493526Sxy150489 	}
50503526Sxy150489 
50513526Sxy150489 	/*
50523526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
50533526Sxy150489 	 * if we netbooted from this interface), we must use this instead
50543526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
50553526Sxy150489 	 * get confused by the address changing as Solaris takes over!
50563526Sxy150489 	 */
50573526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
50583526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
50593526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
50603526Sxy150489 		if (nelts == ETHERADDRL) {
50613526Sxy150489 			while (nelts--)
50624919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
50633526Sxy150489 			found = B_TRUE;
50643526Sxy150489 		}
50653526Sxy150489 		ddi_prop_free(bytes);
50663526Sxy150489 	}
50673526Sxy150489 
50683526Sxy150489 	if (found) {
50694919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
50703526Sxy150489 		    ETHERADDRL);
50713526Sxy150489 	}
50723526Sxy150489 
50733526Sxy150489 	return (found);
50743526Sxy150489 }
50753526Sxy150489 #endif
50763526Sxy150489 
50773526Sxy150489 static int
50783526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
50793526Sxy150489 {
50803526Sxy150489 	dev_info_t *devinfo;
50813526Sxy150489 	int intr_types;
50823526Sxy150489 	int rc;
50833526Sxy150489 
50843526Sxy150489 	devinfo = Adapter->dip;
50853526Sxy150489 
50863526Sxy150489 	/* Get supported interrupt types */
50873526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
50883526Sxy150489 
50893526Sxy150489 	if (rc != DDI_SUCCESS) {
50904919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
50913526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
50923526Sxy150489 		return (DDI_FAILURE);
50933526Sxy150489 	}
50943526Sxy150489 
50953526Sxy150489 	/*
50963526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
50973526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
50983526Sxy150489 	 * that it supports MSI, but in fact has problems.
50993526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
51003526Sxy150489 	 * PCI/PCI-X NICs.
51013526Sxy150489 	 */
51024919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
51036986Smx205022 		Adapter->msi_enable = B_FALSE;
51046986Smx205022 
51056986Smx205022 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
51063526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
51073526Sxy150489 
51083526Sxy150489 		if (rc != DDI_SUCCESS) {
51094919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
51103526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
51113526Sxy150489 		} else {
51123526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
51133526Sxy150489 		}
51143526Sxy150489 	}
51153526Sxy150489 
51163526Sxy150489 	if ((Adapter->intr_type == 0) &&
51173526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
51183526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
51193526Sxy150489 
51203526Sxy150489 		if (rc != DDI_SUCCESS) {
51214919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
51223526Sxy150489 			    "Add Legacy interrupts failed\n");
51233526Sxy150489 			return (DDI_FAILURE);
51243526Sxy150489 		}
51253526Sxy150489 
51263526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
51273526Sxy150489 	}
51283526Sxy150489 
51293526Sxy150489 	if (Adapter->intr_type == 0) {
51304919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
51313526Sxy150489 		    "No interrupts registered\n");
51323526Sxy150489 		return (DDI_FAILURE);
51333526Sxy150489 	}
51343526Sxy150489 
51353526Sxy150489 	return (DDI_SUCCESS);
51363526Sxy150489 }
51373526Sxy150489 
51383526Sxy150489 /*
51393526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
51403526Sxy150489  */
51413526Sxy150489 static int
51423526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
51433526Sxy150489 {
51443526Sxy150489 	dev_info_t *devinfo;
51453526Sxy150489 	int count, avail, actual;
51463526Sxy150489 	int x, y, rc, inum = 0;
51473526Sxy150489 	int flag;
51483526Sxy150489 	ddi_intr_handler_t *intr_handler;
51493526Sxy150489 
51503526Sxy150489 	devinfo = Adapter->dip;
51513526Sxy150489 
51523526Sxy150489 	/* get number of interrupts */
51533526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
51543526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
51554919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51563526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
51573526Sxy150489 		    rc, count);
51583526Sxy150489 		return (DDI_FAILURE);
51593526Sxy150489 	}
51603526Sxy150489 
51613526Sxy150489 	/* get number of available interrupts */
51623526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
51633526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
51644919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51653526Sxy150489 		    "Get interrupt available number failed. "
51663526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
51673526Sxy150489 		return (DDI_FAILURE);
51683526Sxy150489 	}
51693526Sxy150489 
51703526Sxy150489 	if (avail < count) {
51714919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51723526Sxy150489 		    "Interrupts count: %d, available: %d\n",
51733526Sxy150489 		    count, avail);
51743526Sxy150489 	}
51753526Sxy150489 
51763526Sxy150489 	/* Allocate an array of interrupt handles */
51773526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
51783526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
51793526Sxy150489 
51803526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
51813526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
51823526Sxy150489 
51833526Sxy150489 	/* call ddi_intr_alloc() */
51843526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
51853526Sxy150489 	    count, &actual, flag);
51863526Sxy150489 
51873526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
51884919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51893526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
51903526Sxy150489 
51913526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
51923526Sxy150489 		return (DDI_FAILURE);
51933526Sxy150489 	}
51943526Sxy150489 
51953526Sxy150489 	if (actual < count) {
51964919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51973526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
51983526Sxy150489 		    count, actual);
51993526Sxy150489 	}
52003526Sxy150489 
52013526Sxy150489 	Adapter->intr_cnt = actual;
52023526Sxy150489 
52033526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
52043526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
52053526Sxy150489 
52063526Sxy150489 	if (rc != DDI_SUCCESS) {
52074919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52083526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
52093526Sxy150489 
52103526Sxy150489 		/* Free already allocated intr */
52113526Sxy150489 		for (y = 0; y < actual; y++)
52123526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
52133526Sxy150489 
52143526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
52153526Sxy150489 		return (DDI_FAILURE);
52163526Sxy150489 	}
52173526Sxy150489 
52183526Sxy150489 	/*
52193526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
52203526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
52213526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
52223526Sxy150489 	 * devices.
52233526Sxy150489 	 */
52244919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
52253526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
52263526Sxy150489 	else
52273526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
52283526Sxy150489 
52293526Sxy150489 	/* Call ddi_intr_add_handler() */
52303526Sxy150489 	for (x = 0; x < actual; x++) {
52313526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
52323526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
52333526Sxy150489 
52343526Sxy150489 		if (rc != DDI_SUCCESS) {
52354919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52363526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
52373526Sxy150489 
52383526Sxy150489 			/* Remove already added handler */
52393526Sxy150489 			for (y = 0; y < x; y++)
52403526Sxy150489 				(void) ddi_intr_remove_handler(
52413526Sxy150489 				    Adapter->htable[y]);
52423526Sxy150489 
52433526Sxy150489 			/* Free already allocated intr */
52443526Sxy150489 			for (y = 0; y < actual; y++)
52453526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
52463526Sxy150489 
52473526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
52483526Sxy150489 			return (DDI_FAILURE);
52493526Sxy150489 		}
52503526Sxy150489 	}
52513526Sxy150489 
52523526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
52533526Sxy150489 
52543526Sxy150489 	if (rc != DDI_SUCCESS) {
52554919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52563526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
52573526Sxy150489 
52583526Sxy150489 		/* Free already allocated intr */
52593526Sxy150489 		for (y = 0; y < actual; y++) {
52603526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
52613526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
52623526Sxy150489 		}
52633526Sxy150489 
52643526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
52653526Sxy150489 		return (DDI_FAILURE);
52663526Sxy150489 	}
52673526Sxy150489 
52683526Sxy150489 	return (DDI_SUCCESS);
52693526Sxy150489 }
52703526Sxy150489 
52713526Sxy150489 static int
52723526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
52733526Sxy150489 {
52743526Sxy150489 	int x;
52753526Sxy150489 	int rc;
52763526Sxy150489 
52773526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
52783526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
52793526Sxy150489 		if (rc != DDI_SUCCESS) {
52804919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52813526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
52823526Sxy150489 			return (DDI_FAILURE);
52833526Sxy150489 		}
52843526Sxy150489 
52853526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
52863526Sxy150489 		if (rc != DDI_SUCCESS) {
52874919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52883526Sxy150489 			    "Free intr failed: %d\n", rc);
52893526Sxy150489 			return (DDI_FAILURE);
52903526Sxy150489 		}
52913526Sxy150489 	}
52923526Sxy150489 
52933526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
52943526Sxy150489 
52953526Sxy150489 	return (DDI_SUCCESS);
52963526Sxy150489 }
52973526Sxy150489 
52983526Sxy150489 static int
52993526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
53003526Sxy150489 {
53013526Sxy150489 	int x;
53023526Sxy150489 	int rc;
53033526Sxy150489 
53043526Sxy150489 	/* Enable interrupts */
53053526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
53063526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
53073526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
53083526Sxy150489 		    Adapter->intr_cnt);
53093526Sxy150489 		if (rc != DDI_SUCCESS) {
53104919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53113526Sxy150489 			    "Enable block intr failed: %d\n", rc);
53123526Sxy150489 			return (DDI_FAILURE);
53133526Sxy150489 		}
53143526Sxy150489 	} else {
53153526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
53163526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
53173526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
53183526Sxy150489 			if (rc != DDI_SUCCESS) {
53194919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53203526Sxy150489 				    "Enable intr failed: %d\n", rc);
53213526Sxy150489 				return (DDI_FAILURE);
53223526Sxy150489 			}
53233526Sxy150489 		}
53243526Sxy150489 	}
53253526Sxy150489 
53263526Sxy150489 	return (DDI_SUCCESS);
53273526Sxy150489 }
53283526Sxy150489 
53293526Sxy150489 static int
53303526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
53313526Sxy150489 {
53323526Sxy150489 	int x;
53333526Sxy150489 	int rc;
53343526Sxy150489 
53353526Sxy150489 	/* Disable all interrupts */
53363526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
53373526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
53383526Sxy150489 		    Adapter->intr_cnt);
53393526Sxy150489 		if (rc != DDI_SUCCESS) {
53404919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53413526Sxy150489 			    "Disable block intr failed: %d\n", rc);
53423526Sxy150489 			return (DDI_FAILURE);
53433526Sxy150489 		}
53443526Sxy150489 	} else {
53453526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
53463526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
53473526Sxy150489 			if (rc != DDI_SUCCESS) {
53484919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
53493526Sxy150489 				    "Disable intr failed: %d\n", rc);
53503526Sxy150489 				return (DDI_FAILURE);
53513526Sxy150489 			}
53523526Sxy150489 		}
53533526Sxy150489 	}
53543526Sxy150489 
53553526Sxy150489 	return (DDI_SUCCESS);
53563526Sxy150489 }
53575082Syy150190 
53585082Syy150190 /*
53595082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
53605082Syy150190  */
53615082Syy150190 static void
53625082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
53635082Syy150190 {
53645082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
53655082Syy150190 
53667426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
53677426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
53687426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
53697426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
53707426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
53717426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
53727426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
53737426SChenliang.Xu@Sun.COM 	    &Adapter->phy_1000t_status);
53747426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
53756394Scc210113 
53766394Scc210113 	Adapter->param_autoneg_cap =
53776394Scc210113 	    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
53786394Scc210113 	Adapter->param_pause_cap =
53796394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
53806394Scc210113 	Adapter->param_asym_pause_cap =
53816394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
53826394Scc210113 	Adapter->param_1000fdx_cap =
53836394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
53846394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
53856394Scc210113 	Adapter->param_1000hdx_cap =
53866394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
53876394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
53886394Scc210113 	Adapter->param_100t4_cap =
53896394Scc210113 	    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
53906394Scc210113 	Adapter->param_100fdx_cap =
53916394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
53926394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
53936394Scc210113 	Adapter->param_100hdx_cap =
53946394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
53956394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
53966394Scc210113 	Adapter->param_10fdx_cap =
53976394Scc210113 	    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
53986394Scc210113 	Adapter->param_10hdx_cap =
53996394Scc210113 	    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
54006394Scc210113 
54016394Scc210113 	Adapter->param_adv_autoneg = hw->mac.autoneg;
54026394Scc210113 	Adapter->param_adv_pause =
54036394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
54046394Scc210113 	Adapter->param_adv_asym_pause =
54056394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
54066394Scc210113 	Adapter->param_adv_1000hdx =
54076394Scc210113 	    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
54086394Scc210113 	Adapter->param_adv_100t4 =
54096394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
54106394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
54116394Scc210113 		Adapter->param_adv_1000fdx =
54126394Scc210113 		    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
54136394Scc210113 		Adapter->param_adv_100fdx =
54146394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
54156394Scc210113 		Adapter->param_adv_100hdx =
54166394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
54176394Scc210113 		Adapter->param_adv_10fdx =
54186394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
54196394Scc210113 		Adapter->param_adv_10hdx =
54206394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
54216394Scc210113 	}
54226394Scc210113 
54236394Scc210113 	Adapter->param_lp_autoneg =
54246394Scc210113 	    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
54256394Scc210113 	Adapter->param_lp_pause =
54266394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
54276394Scc210113 	Adapter->param_lp_asym_pause =
54286394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
54296394Scc210113 	Adapter->param_lp_1000fdx =
54306394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
54316394Scc210113 	Adapter->param_lp_1000hdx =
54326394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
54336394Scc210113 	Adapter->param_lp_100t4 =
54346394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
54356394Scc210113 	Adapter->param_lp_100fdx =
54366394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
54376394Scc210113 	Adapter->param_lp_100hdx =
54386394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
54396394Scc210113 	Adapter->param_lp_10fdx =
54406394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
54416394Scc210113 	Adapter->param_lp_10hdx =
54426394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
54435082Syy150190 }
54445273Sgl147354 
54455273Sgl147354 /*
54465273Sgl147354  * FMA support
54475273Sgl147354  */
54485273Sgl147354 
54495273Sgl147354 int
54505273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
54515273Sgl147354 {
54525273Sgl147354 	ddi_fm_error_t de;
54535273Sgl147354 
54545273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
54555273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
54565273Sgl147354 	return (de.fme_status);
54575273Sgl147354 }
54585273Sgl147354 
54595273Sgl147354 int
54605273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
54615273Sgl147354 {
54625273Sgl147354 	ddi_fm_error_t de;
54635273Sgl147354 
54645273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
54655273Sgl147354 	return (de.fme_status);
54665273Sgl147354 }
54675273Sgl147354 
54685273Sgl147354 /*
54695273Sgl147354  * The IO fault service error handling callback function
54705273Sgl147354  */
54717426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
54725273Sgl147354 static int
54735273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
54745273Sgl147354 {
54755273Sgl147354 	/*
54765273Sgl147354 	 * as the driver can always deal with an error in any dma or
54775273Sgl147354 	 * access handle, we can just return the fme_status value.
54785273Sgl147354 	 */
54795273Sgl147354 	pci_ereport_post(dip, err, NULL);
54805273Sgl147354 	return (err->fme_status);
54815273Sgl147354 }
54825273Sgl147354 
54835273Sgl147354 static void
54845273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
54855273Sgl147354 {
54865273Sgl147354 	ddi_iblock_cookie_t iblk;
54875273Sgl147354 	int fma_acc_flag, fma_dma_flag;
54885273Sgl147354 
54895273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
54905273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
54915273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
54925273Sgl147354 		fma_acc_flag = 1;
54935273Sgl147354 	} else {
54945273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
54955273Sgl147354 		fma_acc_flag = 0;
54965273Sgl147354 	}
54975273Sgl147354 
54985273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
54995273Sgl147354 		fma_dma_flag = 1;
55005273Sgl147354 	} else {
55015273Sgl147354 		fma_dma_flag = 0;
55025273Sgl147354 	}
55035273Sgl147354 
55045273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
55055273Sgl147354 
55065273Sgl147354 	if (Adapter->fm_capabilities) {
55075273Sgl147354 
55085273Sgl147354 		/* Register capabilities with IO Fault Services */
55095273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
55105273Sgl147354 
55115273Sgl147354 		/*
55125273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
55135273Sgl147354 		 */
55145273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
55155273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
55165273Sgl147354 			pci_ereport_setup(Adapter->dip);
55175273Sgl147354 
55185273Sgl147354 		/*
55195273Sgl147354 		 * Register error callback if error callback capable
55205273Sgl147354 		 */
55215273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
55225273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
55235273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
55245273Sgl147354 	}
55255273Sgl147354 }
55265273Sgl147354 
55275273Sgl147354 static void
55285273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
55295273Sgl147354 {
55305273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
55315273Sgl147354 	if (Adapter->fm_capabilities) {
55325273Sgl147354 
55335273Sgl147354 		/*
55345273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
55355273Sgl147354 		 */
55365273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
55375273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
55385273Sgl147354 			pci_ereport_teardown(Adapter->dip);
55395273Sgl147354 
55405273Sgl147354 		/*
55415273Sgl147354 		 * Un-register error callback if error callback capable
55425273Sgl147354 		 */
55435273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
55445273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
55455273Sgl147354 
55465273Sgl147354 		/* Unregister from IO Fault Services */
55475273Sgl147354 		ddi_fm_fini(Adapter->dip);
55485273Sgl147354 	}
55495273Sgl147354 }
55505273Sgl147354 
55515273Sgl147354 void
55525273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
55535273Sgl147354 {
55545273Sgl147354 	uint64_t ena;
55555273Sgl147354 	char buf[FM_MAX_CLASS];
55565273Sgl147354 
55575273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
55585273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
55595273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
55605273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
55615273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
55625273Sgl147354 	}
55635273Sgl147354 }
55646512Ssowmini 
55657656SSherry.Moore@Sun.COM /*
55667656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
55677656SSherry.Moore@Sun.COM  *
55687656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
55697656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
55707656SSherry.Moore@Sun.COM  * blocked.
55717656SSherry.Moore@Sun.COM  *
55727656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
55737656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
55747656SSherry.Moore@Sun.COM  */
55757656SSherry.Moore@Sun.COM static int
55767656SSherry.Moore@Sun.COM e1000g_quiesce(dev_info_t *devinfo)
55777656SSherry.Moore@Sun.COM {
55787656SSherry.Moore@Sun.COM 	struct e1000g *Adapter;
55797656SSherry.Moore@Sun.COM 
55807656SSherry.Moore@Sun.COM 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
55817656SSherry.Moore@Sun.COM 
55827656SSherry.Moore@Sun.COM 	if (Adapter == NULL)
55837656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
55847656SSherry.Moore@Sun.COM 
55857656SSherry.Moore@Sun.COM 	e1000g_clear_all_interrupts(Adapter);
55867656SSherry.Moore@Sun.COM 
55877656SSherry.Moore@Sun.COM 	(void) e1000_reset_hw(&Adapter->shared);
55887656SSherry.Moore@Sun.COM 
55897656SSherry.Moore@Sun.COM 	/* Setup our HW Tx Head & Tail descriptor pointers */
55907656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
55917656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
55927656SSherry.Moore@Sun.COM 
55937656SSherry.Moore@Sun.COM 	/* Setup our HW Rx Head & Tail descriptor pointers */
55947656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDH(0), 0);
55957656SSherry.Moore@Sun.COM 	E1000_WRITE_REG(&Adapter->shared, E1000_RDT(0), 0);
55967656SSherry.Moore@Sun.COM 
55977656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
55987656SSherry.Moore@Sun.COM }
55997656SSherry.Moore@Sun.COM 
56006512Ssowmini static int
56016512Ssowmini e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
56026512Ssowmini     uint_t pr_valsize, void *pr_val)
56036512Ssowmini {
56046512Ssowmini 	link_flowctrl_t fl;
56056512Ssowmini 	int err = 0;
56066512Ssowmini 
56076512Ssowmini 	ASSERT(pr_valsize > 0);
56086512Ssowmini 	switch (pr_num) {
56096789Sam223141 	case MAC_PROP_AUTONEG:
56106512Ssowmini 		*(uint8_t *)pr_val =
56116512Ssowmini 		    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0);
56126512Ssowmini 		break;
56136789Sam223141 	case MAC_PROP_FLOWCTRL:
56146512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
56156512Ssowmini 			return (EINVAL);
56166512Ssowmini 		fl = LINK_FLOWCTRL_BI;
56176512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
56186512Ssowmini 		break;
56196789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
56206789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
56216512Ssowmini 		*(uint8_t *)pr_val =
56226512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
56236512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
56246512Ssowmini 		break;
56256789Sam223141 	case MAC_PROP_ADV_1000HDX_CAP:
56266789Sam223141 	case MAC_PROP_EN_1000HDX_CAP:
56276512Ssowmini 		*(uint8_t *)pr_val =
56286512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
56296512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
56306512Ssowmini 		break;
56316789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
56326789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
56336512Ssowmini 		*(uint8_t *)pr_val =
56346512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
56356512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
56367426SChenliang.Xu@Sun.COM 		break;
56376789Sam223141 	case MAC_PROP_ADV_100HDX_CAP:
56386789Sam223141 	case MAC_PROP_EN_100HDX_CAP:
56396512Ssowmini 		*(uint8_t *)pr_val =
56406512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
56416512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
56426512Ssowmini 		break;
56436789Sam223141 	case MAC_PROP_ADV_10FDX_CAP:
56446789Sam223141 	case MAC_PROP_EN_10FDX_CAP:
56456512Ssowmini 		*(uint8_t *)pr_val =
56466512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
56476512Ssowmini 		break;
56486789Sam223141 	case MAC_PROP_ADV_10HDX_CAP:
56496789Sam223141 	case MAC_PROP_EN_10HDX_CAP:
56506512Ssowmini 		*(uint8_t *)pr_val =
56516512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
56526512Ssowmini 		break;
56536512Ssowmini 	default:
56546512Ssowmini 		err = ENOTSUP;
56556512Ssowmini 		break;
56566512Ssowmini 	}
56576512Ssowmini 	return (err);
56586512Ssowmini }
56596512Ssowmini 
56606512Ssowmini /*
56616512Ssowmini  * synchronize the adv* and en* parameters.
56626512Ssowmini  *
56636512Ssowmini  * See comments in <sys/dld.h> for details of the *_en_*
56646512Ssowmini  * parameters. The usage of ndd for setting adv parameters will
56656512Ssowmini  * synchronize all the en parameters with the e1000g parameters,
56667607STed.You@Sun.COM  * implicitly disabling any settings made via dladm.
56676512Ssowmini  */
56686512Ssowmini static void
56696512Ssowmini e1000g_param_sync(struct e1000g *Adapter)
56706512Ssowmini {
56716512Ssowmini 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
56726512Ssowmini 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
56736512Ssowmini 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
56746512Ssowmini 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
56756512Ssowmini 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
56766512Ssowmini 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
56776512Ssowmini }
56787607STed.You@Sun.COM 
56797607STed.You@Sun.COM /*
56807607STed.You@Sun.COM  * e1000g_get_driver_control - tell manageability firmware that the driver
56817607STed.You@Sun.COM  * has control.
56827607STed.You@Sun.COM  */
56837607STed.You@Sun.COM static void
56847607STed.You@Sun.COM e1000g_get_driver_control(struct e1000_hw *hw)
56857607STed.You@Sun.COM {
56867607STed.You@Sun.COM 	uint32_t ctrl_ext;
56877607STed.You@Sun.COM 	uint32_t swsm;
56887607STed.You@Sun.COM 
56897607STed.You@Sun.COM 	/* tell manageability firmware the driver has taken over */
56907607STed.You@Sun.COM 	switch (hw->mac.type) {
56917607STed.You@Sun.COM 	case e1000_82573:
56927607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
56937607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
56947607STed.You@Sun.COM 		break;
56957607STed.You@Sun.COM 	case e1000_82571:
56967607STed.You@Sun.COM 	case e1000_82572:
56977607STed.You@Sun.COM 	case e1000_82574:
56987607STed.You@Sun.COM 	case e1000_80003es2lan:
56997607STed.You@Sun.COM 	case e1000_ich8lan:
57007607STed.You@Sun.COM 	case e1000_ich9lan:
57017607STed.You@Sun.COM 	case e1000_ich10lan:
57027607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
57037607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
57047607STed.You@Sun.COM 		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
57057607STed.You@Sun.COM 		break;
57067607STed.You@Sun.COM 	default:
57077607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
57087607STed.You@Sun.COM 		break;
57097607STed.You@Sun.COM 	}
57107607STed.You@Sun.COM }
57117607STed.You@Sun.COM 
57127607STed.You@Sun.COM /*
57137607STed.You@Sun.COM  * e1000g_release_driver_control - tell manageability firmware that the driver
57147607STed.You@Sun.COM  * has released control.
57157607STed.You@Sun.COM  */
57167607STed.You@Sun.COM static void
57177607STed.You@Sun.COM e1000g_release_driver_control(struct e1000_hw *hw)
57187607STed.You@Sun.COM {
57197607STed.You@Sun.COM 	uint32_t ctrl_ext;
57207607STed.You@Sun.COM 	uint32_t swsm;
57217607STed.You@Sun.COM 
57227607STed.You@Sun.COM 	/* tell manageability firmware the driver has released control */
57237607STed.You@Sun.COM 	switch (hw->mac.type) {
57247607STed.You@Sun.COM 	case e1000_82573:
57257607STed.You@Sun.COM 		swsm = E1000_READ_REG(hw, E1000_SWSM);
57267607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
57277607STed.You@Sun.COM 		break;
57287607STed.You@Sun.COM 	case e1000_82571:
57297607STed.You@Sun.COM 	case e1000_82572:
57307607STed.You@Sun.COM 	case e1000_82574:
57317607STed.You@Sun.COM 	case e1000_80003es2lan:
57327607STed.You@Sun.COM 	case e1000_ich8lan:
57337607STed.You@Sun.COM 	case e1000_ich9lan:
57347607STed.You@Sun.COM 	case e1000_ich10lan:
57357607STed.You@Sun.COM 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
57367607STed.You@Sun.COM 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
57377607STed.You@Sun.COM 		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
57387607STed.You@Sun.COM 		break;
57397607STed.You@Sun.COM 	default:
57407607STed.You@Sun.COM 		/* no manageability firmware: do nothing */
57417607STed.You@Sun.COM 		break;
57427607STed.You@Sun.COM 	}
57437607STed.You@Sun.COM }
57447722SShuguo.Yang@Sun.COM 
57457722SShuguo.Yang@Sun.COM /*
57467722SShuguo.Yang@Sun.COM  * Restore e1000g promiscuous mode.
57477722SShuguo.Yang@Sun.COM  */
57487722SShuguo.Yang@Sun.COM static void
57497722SShuguo.Yang@Sun.COM e1000g_restore_promisc(struct e1000g *Adapter)
57507722SShuguo.Yang@Sun.COM {
57517722SShuguo.Yang@Sun.COM 	if (Adapter->e1000g_promisc) {
57527722SShuguo.Yang@Sun.COM 		uint32_t rctl;
57537722SShuguo.Yang@Sun.COM 
57547722SShuguo.Yang@Sun.COM 		rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
57557722SShuguo.Yang@Sun.COM 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
57567722SShuguo.Yang@Sun.COM 		E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
57577722SShuguo.Yang@Sun.COM 	}
57587722SShuguo.Yang@Sun.COM }
5759