xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 7426:008ea04d81d3)
13526Sxy150489 /*
23526Sxy150489  * This file is provided under a CDDLv1 license.  When using or
33526Sxy150489  * redistributing this file, you may do so under this license.
43526Sxy150489  * In redistributing this file this license must be included
53526Sxy150489  * and no other modification of this header file is permitted.
63526Sxy150489  *
73526Sxy150489  * CDDL LICENSE SUMMARY
83526Sxy150489  *
95882Syy150190  * Copyright(c) 1999 - 2008 Intel Corporation. All rights reserved.
103526Sxy150489  *
113526Sxy150489  * The contents of this file are subject to the terms of Version
123526Sxy150489  * 1.0 of the Common Development and Distribution License (the "License").
133526Sxy150489  *
143526Sxy150489  * You should have received a copy of the License with this software.
153526Sxy150489  * You can obtain a copy of the License at
163526Sxy150489  *	http://www.opensolaris.org/os/licensing.
173526Sxy150489  * See the License for the specific language governing permissions
183526Sxy150489  * and limitations under the License.
193526Sxy150489  */
203526Sxy150489 
213526Sxy150489 /*
225858Scc210113  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237133Scc210113  * Use is subject to license terms of the CDDLv1.
243526Sxy150489  */
253526Sxy150489 
263526Sxy150489 /*
273526Sxy150489  * **********************************************************************
283526Sxy150489  *									*
293526Sxy150489  * Module Name:								*
303526Sxy150489  *   e1000g_main.c							*
313526Sxy150489  *									*
323526Sxy150489  * Abstract:								*
334919Sxy150489  *   This file contains the interface routines for the solaris OS.	*
344919Sxy150489  *   It has all DDI entry point routines and GLD entry point routines.	*
353526Sxy150489  *									*
364919Sxy150489  *   This file also contains routines that take care of initialization	*
374919Sxy150489  *   uninit routine and interrupt routine.				*
383526Sxy150489  *									*
393526Sxy150489  * **********************************************************************
403526Sxy150489  */
413526Sxy150489 
423526Sxy150489 #include <sys/dlpi.h>
433526Sxy150489 #include <sys/mac.h>
443526Sxy150489 #include "e1000g_sw.h"
453526Sxy150489 #include "e1000g_debug.h"
463526Sxy150489 
47*7426SChenliang.Xu@Sun.COM static char ident[] = "Intel PRO/1000 Ethernet 5.2.12";
483526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
49*7426SChenliang.Xu@Sun.COM static char e1000g_version[] = "Driver Ver. 5.2.12";
503526Sxy150489 
513526Sxy150489 /*
523526Sxy150489  * Proto types for DDI entry points
533526Sxy150489  */
544919Sxy150489 static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
554919Sxy150489 static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
563526Sxy150489 
573526Sxy150489 /*
583526Sxy150489  * init and intr routines prototype
593526Sxy150489  */
604919Sxy150489 static int e1000g_resume(dev_info_t *);
614919Sxy150489 static int e1000g_suspend(dev_info_t *);
623526Sxy150489 static uint_t e1000g_intr_pciexpress(caddr_t);
633526Sxy150489 static uint_t e1000g_intr(caddr_t);
643526Sxy150489 static void e1000g_intr_work(struct e1000g *, uint32_t);
653526Sxy150489 #pragma inline(e1000g_intr_work)
665882Syy150190 static uint32_t e1000g_get_itr(uint32_t, uint32_t, uint32_t);
675882Syy150190 #pragma inline(e1000g_get_itr)
683526Sxy150489 static int e1000g_init(struct e1000g *);
694919Sxy150489 static int e1000g_start(struct e1000g *, boolean_t);
704919Sxy150489 static void e1000g_stop(struct e1000g *, boolean_t);
713526Sxy150489 static int e1000g_m_start(void *);
723526Sxy150489 static void e1000g_m_stop(void *);
733526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
743526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
753526Sxy150489 static int e1000g_m_unicst(void *, const uint8_t *);
763526Sxy150489 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
773526Sxy150489 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
783526Sxy150489 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
793526Sxy150489 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
803526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
813526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
826394Scc210113 static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
836394Scc210113     uint_t, const void *);
846394Scc210113 static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
856512Ssowmini     uint_t, uint_t, void *);
866394Scc210113 static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
876394Scc210113     const void *);
886394Scc210113 static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t,
896512Ssowmini     uint_t, void *);
904919Sxy150489 static void e1000g_init_locks(struct e1000g *);
914919Sxy150489 static void e1000g_destroy_locks(struct e1000g *);
924919Sxy150489 static int e1000g_identify_hardware(struct e1000g *);
934919Sxy150489 static int e1000g_regs_map(struct e1000g *);
944919Sxy150489 static int e1000g_set_driver_params(struct e1000g *);
956394Scc210113 static void e1000g_set_bufsize(struct e1000g *);
964919Sxy150489 static int e1000g_register_mac(struct e1000g *);
974919Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *);
984919Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *);
994919Sxy150489 static void e1000g_init_unicst(struct e1000g *);
1003526Sxy150489 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
1013526Sxy150489 
1023526Sxy150489 /*
1033526Sxy150489  * Local routines
1043526Sxy150489  */
1054919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1064919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1074061Sxy150489 static void e1000g_link_timer(void *);
1084919Sxy150489 static void e1000g_local_timer(void *);
1094061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1103526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1113526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1124919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1134919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1144919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1154919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1164919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1174919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1184919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1194919Sxy150489 static void stop_link_timer(struct e1000g *);
1204919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1214919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
1224919Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *);
1234919Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
1243526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1254919Sxy150489 #ifdef E1000G_DEBUG
1264919Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
1274919Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
1284919Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
1294919Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
1304919Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
1314919Sxy150489     struct iocblk *, mblk_t *);
1324919Sxy150489 #endif
1334919Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
1344919Sxy150489     struct iocblk *, mblk_t *);
1357133Scc210113 static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
1364919Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
1374919Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *);
1384919Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *);
1394919Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *);
1404919Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *);
1414919Sxy150489 static int e1000g_add_intrs(struct e1000g *);
1424919Sxy150489 static int e1000g_intr_add(struct e1000g *, int);
1434919Sxy150489 static int e1000g_rem_intrs(struct e1000g *);
1444919Sxy150489 static int e1000g_enable_intrs(struct e1000g *);
1454919Sxy150489 static int e1000g_disable_intrs(struct e1000g *);
1464919Sxy150489 static boolean_t e1000g_link_up(struct e1000g *);
1473526Sxy150489 #ifdef __sparc
1484919Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *);
1493526Sxy150489 #endif
1505082Syy150190 static void e1000g_get_phy_state(struct e1000g *);
1514982Syy150190 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
1525273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1535273Sgl147354     const void *impl_data);
1545273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1555273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1566512Ssowmini static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
1576512Ssowmini static void e1000g_param_sync(struct e1000g *);
1586512Ssowmini 
1596512Ssowmini mac_priv_prop_t e1000g_priv_props[] = {
1606512Ssowmini 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
1616512Ssowmini 	{"_tx_interrupt_enable", MAC_PROP_PERM_RW},
1626512Ssowmini 	{"_tx_intr_delay", MAC_PROP_PERM_RW},
1636512Ssowmini 	{"_tx_intr_abs_delay", MAC_PROP_PERM_RW},
1646512Ssowmini 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
1656512Ssowmini 	{"_max_num_rcv_packets", MAC_PROP_PERM_RW},
1666512Ssowmini 	{"_rx_intr_delay", MAC_PROP_PERM_RW},
1676512Ssowmini 	{"_rx_intr_abs_delay", MAC_PROP_PERM_RW},
1686512Ssowmini 	{"_intr_throttling_rate", MAC_PROP_PERM_RW},
1696512Ssowmini 	{"_intr_adaptive", MAC_PROP_PERM_RW},
1706512Ssowmini 	{"_tx_recycle_thresh", MAC_PROP_PERM_RW},
1716512Ssowmini 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
1726512Ssowmini 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
1736512Ssowmini 	{"_tx_recycle_num", MAC_PROP_PERM_RW}
1746512Ssowmini };
1756512Ssowmini #define	E1000G_MAX_PRIV_PROPS	\
1766512Ssowmini 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
1776512Ssowmini 
1783526Sxy150489 
1793526Sxy150489 static struct cb_ops cb_ws_ops = {
1803526Sxy150489 	nulldev,		/* cb_open */
1813526Sxy150489 	nulldev,		/* cb_close */
1823526Sxy150489 	nodev,			/* cb_strategy */
1833526Sxy150489 	nodev,			/* cb_print */
1843526Sxy150489 	nodev,			/* cb_dump */
1853526Sxy150489 	nodev,			/* cb_read */
1863526Sxy150489 	nodev,			/* cb_write */
1873526Sxy150489 	nodev,			/* cb_ioctl */
1883526Sxy150489 	nodev,			/* cb_devmap */
1893526Sxy150489 	nodev,			/* cb_mmap */
1903526Sxy150489 	nodev,			/* cb_segmap */
1913526Sxy150489 	nochpoll,		/* cb_chpoll */
1923526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1933526Sxy150489 	NULL,			/* cb_stream */
1943526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1953526Sxy150489 	CB_REV,			/* cb_rev */
1963526Sxy150489 	nodev,			/* cb_aread */
1973526Sxy150489 	nodev			/* cb_awrite */
1983526Sxy150489 };
1993526Sxy150489 
2003526Sxy150489 static struct dev_ops ws_ops = {
2013526Sxy150489 	DEVO_REV,		/* devo_rev */
2023526Sxy150489 	0,			/* devo_refcnt */
2033526Sxy150489 	NULL,			/* devo_getinfo */
2043526Sxy150489 	nulldev,		/* devo_identify */
2053526Sxy150489 	nulldev,		/* devo_probe */
2064919Sxy150489 	e1000g_attach,		/* devo_attach */
2074919Sxy150489 	e1000g_detach,		/* devo_detach */
2083526Sxy150489 	nodev,			/* devo_reset */
2093526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
2103526Sxy150489 	NULL,			/* devo_bus_ops */
2113526Sxy150489 	ddi_power		/* devo_power */
2123526Sxy150489 };
2133526Sxy150489 
2143526Sxy150489 static struct modldrv modldrv = {
2153526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
2163526Sxy150489 	ident,			/* Discription string */
2173526Sxy150489 	&ws_ops,		/* driver ops */
2183526Sxy150489 };
2193526Sxy150489 
2203526Sxy150489 static struct modlinkage modlinkage = {
2213526Sxy150489 	MODREV_1, &modldrv, NULL
2223526Sxy150489 };
2233526Sxy150489 
2244919Sxy150489 /* Access attributes for register mapping */
2254919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
2263526Sxy150489 	DDI_DEVICE_ATTR_V0,
2273526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2283526Sxy150489 	DDI_STRICTORDER_ACC,
2295273Sgl147354 	DDI_FLAGERR_ACC
2303526Sxy150489 };
2313526Sxy150489 
2326394Scc210113 #define	E1000G_M_CALLBACK_FLAGS \
2336394Scc210113 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
2343526Sxy150489 
2353526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2363526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2373526Sxy150489 	e1000g_m_stat,
2383526Sxy150489 	e1000g_m_start,
2393526Sxy150489 	e1000g_m_stop,
2403526Sxy150489 	e1000g_m_promisc,
2413526Sxy150489 	e1000g_m_multicst,
2423526Sxy150489 	e1000g_m_unicst,
2433526Sxy150489 	e1000g_m_tx,
2445882Syy150190 	NULL,
2453526Sxy150489 	e1000g_m_ioctl,
2466394Scc210113 	e1000g_m_getcapab,
2476394Scc210113 	NULL,
2486394Scc210113 	NULL,
2496394Scc210113 	e1000g_m_setprop,
2506394Scc210113 	e1000g_m_getprop
2513526Sxy150489 };
2523526Sxy150489 
2533526Sxy150489 /*
2543526Sxy150489  * Global variables
2553526Sxy150489  */
2563526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2573526Sxy150489 /*
2584894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2594349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2604349Sxy150489  * enabled. If we force the driver to detach while there are still some
2614349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2624349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2634349Sxy150489  * structure will be freed after the driver is detached. However when we
2644349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2654349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2664894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2674894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2684894Syy150190  * after the system board level DR operation. For this reason, the global
2694894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2704349Sxy150489  */
2714894Syy150190 #ifdef __sparc
2724894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2734894Syy150190 #else
2744894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2754894Syy150190 #endif
2764349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2774894Syy150190 
2784349Sxy150489 /*
2793526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2803526Sxy150489  * and the rx packets release in detach processing to make them mutually
2813526Sxy150489  * exclusive.
2823526Sxy150489  * The rx recycling processes different rx packets in different threads,
2833526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2843526Sxy150489  * recycling threads.
2853526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2863526Sxy150489  * it mutually exclusive with the rx recycling.
2873526Sxy150489  */
2883526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2893526Sxy150489 /*
2903526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2913526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2923526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2933526Sxy150489  * resources during the initialization of the instances, then the flag will
2943526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2953526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2963526Sxy150489  */
2973526Sxy150489 krwlock_t e1000g_dma_type_lock;
2983526Sxy150489 
2997133Scc210113 /*
3007133Scc210113  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
3017133Scc210113  * Based on the information from Intel, the 82546 chipset has some hardware
3027133Scc210113  * problem. When one port is being reset and the other port is trying to
3037133Scc210113  * access the eeprom, it could cause system hang or panic. To workaround this
3047133Scc210113  * hardware problem, we use a global mutex to prevent such operations from
3057133Scc210113  * happening simultaneously on different instances. This workaround is applied
3067133Scc210113  * to all the devices supported by this driver.
3077133Scc210113  */
3087133Scc210113 kmutex_t e1000g_nvm_lock;
3093526Sxy150489 
3103526Sxy150489 /*
3113526Sxy150489  * Loadable module configuration entry points for the driver
3123526Sxy150489  */
3133526Sxy150489 
3143526Sxy150489 /*
3154919Sxy150489  * _init - module initialization
3163526Sxy150489  */
3173526Sxy150489 int
3183526Sxy150489 _init(void)
3193526Sxy150489 {
3203526Sxy150489 	int status;
3213526Sxy150489 
3223526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
3233526Sxy150489 	status = mod_install(&modlinkage);
3243526Sxy150489 	if (status != DDI_SUCCESS)
3253526Sxy150489 		mac_fini_ops(&ws_ops);
3263526Sxy150489 	else {
3273526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
3283526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
3297133Scc210113 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
3303526Sxy150489 	}
3313526Sxy150489 
3323526Sxy150489 	return (status);
3333526Sxy150489 }
3343526Sxy150489 
3353526Sxy150489 /*
3364919Sxy150489  * _fini - module finalization
3373526Sxy150489  */
3383526Sxy150489 int
3393526Sxy150489 _fini(void)
3403526Sxy150489 {
3413526Sxy150489 	int status;
3423526Sxy150489 
3433526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3443526Sxy150489 	if (e1000g_mblks_pending != 0) {
3453526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3463526Sxy150489 		return (EBUSY);
3473526Sxy150489 	}
3483526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3493526Sxy150489 
3503526Sxy150489 	status = mod_remove(&modlinkage);
3513526Sxy150489 	if (status == DDI_SUCCESS) {
3523526Sxy150489 		mac_fini_ops(&ws_ops);
3534349Sxy150489 
3544349Sxy150489 		if (e1000g_force_detach) {
3554349Sxy150489 			private_devi_list_t *devi_node;
3564349Sxy150489 
3574349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3584349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3594349Sxy150489 				devi_node = e1000g_private_devi_list;
3604349Sxy150489 				e1000g_private_devi_list =
3614349Sxy150489 				    e1000g_private_devi_list->next;
3624349Sxy150489 
3634349Sxy150489 				kmem_free(devi_node->priv_dip,
3644349Sxy150489 				    sizeof (struct dev_info));
3654349Sxy150489 				kmem_free(devi_node,
3664349Sxy150489 				    sizeof (private_devi_list_t));
3674349Sxy150489 			}
3684349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3694349Sxy150489 		}
3704349Sxy150489 
3713526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3723526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3737133Scc210113 		mutex_destroy(&e1000g_nvm_lock);
3743526Sxy150489 	}
3753526Sxy150489 
3763526Sxy150489 	return (status);
3773526Sxy150489 }
3783526Sxy150489 
3793526Sxy150489 /*
3804919Sxy150489  * _info - module information
3813526Sxy150489  */
3823526Sxy150489 int
3833526Sxy150489 _info(struct modinfo *modinfop)
3843526Sxy150489 {
3853526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3863526Sxy150489 }
3873526Sxy150489 
3883526Sxy150489 /*
3894919Sxy150489  * e1000g_attach - driver attach
3904919Sxy150489  *
3914919Sxy150489  * This function is the device-specific initialization entry
3924919Sxy150489  * point. This entry point is required and must be written.
3934919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3944919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3954919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3964919Sxy150489  * available for use by the driver.
3974919Sxy150489  *
3984919Sxy150489  * The attach() function will be called once for each instance
3994919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
4004919Sxy150489  * Until attach() succeeds, the only driver entry points which
4014919Sxy150489  * may be called are open(9E) and getinfo(9E).
4023526Sxy150489  */
4033526Sxy150489 static int
4044919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
4053526Sxy150489 {
4063526Sxy150489 	struct e1000g *Adapter;
4073526Sxy150489 	struct e1000_hw *hw;
4084919Sxy150489 	struct e1000g_osdep *osdep;
4093526Sxy150489 	int instance;
4103526Sxy150489 
4113526Sxy150489 	switch (cmd) {
4123526Sxy150489 	default:
4133526Sxy150489 		e1000g_log(NULL, CE_WARN,
4144919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
4153526Sxy150489 		return (DDI_FAILURE);
4163526Sxy150489 
4173526Sxy150489 	case DDI_RESUME:
4183526Sxy150489 		return (e1000g_resume(devinfo));
4193526Sxy150489 
4203526Sxy150489 	case DDI_ATTACH:
4213526Sxy150489 		break;
4223526Sxy150489 	}
4233526Sxy150489 
4243526Sxy150489 	/*
4253526Sxy150489 	 * get device instance number
4263526Sxy150489 	 */
4273526Sxy150489 	instance = ddi_get_instance(devinfo);
4283526Sxy150489 
4293526Sxy150489 	/*
4303526Sxy150489 	 * Allocate soft data structure
4313526Sxy150489 	 */
4323526Sxy150489 	Adapter =
4333526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4343526Sxy150489 
4353526Sxy150489 	Adapter->dip = devinfo;
4364919Sxy150489 	Adapter->instance = instance;
4373526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4383526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4393526Sxy150489 
4404919Sxy150489 	hw = &Adapter->shared;
4414919Sxy150489 	osdep = &Adapter->osdep;
4424919Sxy150489 	hw->back = osdep;
4434919Sxy150489 	osdep->adapter = Adapter;
4444919Sxy150489 
4453526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4463526Sxy150489 
4474919Sxy150489 	/*
4485273Sgl147354 	 * Initialize for fma support
4495273Sgl147354 	 */
4505273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4515273Sgl147354 	    0, 0x0f,
4525273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4535273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4545273Sgl147354 	e1000g_fm_init(Adapter);
4555273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4565273Sgl147354 
4575273Sgl147354 	/*
4584919Sxy150489 	 * PCI Configure
4594919Sxy150489 	 */
4604919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4614919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4624919Sxy150489 		goto attach_fail;
4634919Sxy150489 	}
4644919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4654919Sxy150489 
4664919Sxy150489 	/*
4674919Sxy150489 	 * Setup hardware
4684919Sxy150489 	 */
4694919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4704919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4714919Sxy150489 		goto attach_fail;
4724919Sxy150489 	}
4733526Sxy150489 
4743526Sxy150489 	/*
4753526Sxy150489 	 * Map in the device registers.
4763526Sxy150489 	 */
4774919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4784919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4793526Sxy150489 		goto attach_fail;
4803526Sxy150489 	}
4814919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4823526Sxy150489 
4833526Sxy150489 	/*
4843526Sxy150489 	 * Initialize driver parameters
4853526Sxy150489 	 */
4863526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4873526Sxy150489 		goto attach_fail;
4883526Sxy150489 	}
4894919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4903526Sxy150489 
4915273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4925273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4935273Sgl147354 		goto attach_fail;
4945273Sgl147354 	}
4955273Sgl147354 
4963526Sxy150489 	/*
4973526Sxy150489 	 * Initialize interrupts
4983526Sxy150489 	 */
4993526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
5003526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
5013526Sxy150489 		goto attach_fail;
5023526Sxy150489 	}
5034919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
5043526Sxy150489 
5053526Sxy150489 	/*
5063526Sxy150489 	 * Initialize mutex's for this device.
5073526Sxy150489 	 * Do this before enabling the interrupt handler and
5083526Sxy150489 	 * register the softint to avoid the condition where
5093526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5103526Sxy150489 	 */
5113526Sxy150489 	e1000g_init_locks(Adapter);
5123526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5133526Sxy150489 
5143526Sxy150489 	/*
5153526Sxy150489 	 * Initialize Driver Counters
5163526Sxy150489 	 */
5174919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
5183526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5193526Sxy150489 		goto attach_fail;
5203526Sxy150489 	}
5213526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5223526Sxy150489 
5233526Sxy150489 	/*
5243526Sxy150489 	 * Initialize chip hardware and software structures
5253526Sxy150489 	 */
5263526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5273526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5283526Sxy150489 		goto attach_fail;
5293526Sxy150489 	}
5303526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5313526Sxy150489 
5323526Sxy150489 	/*
5333526Sxy150489 	 * Register the driver to the MAC
5343526Sxy150489 	 */
5353526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5363526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5373526Sxy150489 		goto attach_fail;
5383526Sxy150489 	}
5394919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5403526Sxy150489 
5413526Sxy150489 	/*
5423526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5433526Sxy150489 	 * initialized, enable interrupts.
5443526Sxy150489 	 */
5453526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5463526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5473526Sxy150489 		goto attach_fail;
5483526Sxy150489 	}
5494919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5503526Sxy150489 
5514982Syy150190 	/*
5524982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5534982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5544982Syy150190 	 * supports after driver detached.
5554982Syy150190 	 */
5564982Syy150190 	if (e1000g_force_detach) {
5574982Syy150190 		private_devi_list_t *devi_node;
5584982Syy150190 
5594982Syy150190 		Adapter->priv_dip =
5604982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5614982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5624982Syy150190 		    sizeof (struct dev_info));
5634982Syy150190 
5644982Syy150190 		devi_node =
5654982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5664982Syy150190 
5674982Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
5684982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5694982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5704982Syy150190 		devi_node->next = e1000g_private_devi_list;
5714982Syy150190 		e1000g_private_devi_list = devi_node;
5724982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
5734982Syy150190 	}
5744982Syy150190 
5753526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5763526Sxy150489 
5773526Sxy150489 	return (DDI_SUCCESS);
5783526Sxy150489 
5793526Sxy150489 attach_fail:
5803526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5813526Sxy150489 	return (DDI_FAILURE);
5823526Sxy150489 }
5833526Sxy150489 
5843526Sxy150489 static int
5853526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5863526Sxy150489 {
5874919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5883526Sxy150489 	mac_register_t *mac;
5893526Sxy150489 	int err;
5903526Sxy150489 
5913526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5923526Sxy150489 		return (DDI_FAILURE);
5934919Sxy150489 
5943526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
5953526Sxy150489 	mac->m_driver = Adapter;
5963526Sxy150489 	mac->m_dip = Adapter->dip;
5974919Sxy150489 	mac->m_src_addr = hw->mac.addr;
5983526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
5993526Sxy150489 	mac->m_min_sdu = 0;
6006394Scc210113 	mac->m_max_sdu = Adapter->default_mtu;
6015895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6026512Ssowmini 	mac->m_priv_props = e1000g_priv_props;
6036512Ssowmini 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
6044919Sxy150489 
6053526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6063526Sxy150489 	mac_free(mac);
6074919Sxy150489 
6083526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6093526Sxy150489 }
6103526Sxy150489 
6113526Sxy150489 static int
6124919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
6134919Sxy150489 {
6144919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6154919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6164919Sxy150489 
6174919Sxy150489 	/* Get the device id */
6184919Sxy150489 	hw->vendor_id =
6194919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6204919Sxy150489 	hw->device_id =
6214919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6224919Sxy150489 	hw->revision_id =
6234919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6244919Sxy150489 	hw->subsystem_device_id =
6254919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6264919Sxy150489 	hw->subsystem_vendor_id =
6274919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6284919Sxy150489 
6294919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6304919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6314919Sxy150489 		    "MAC type could not be set properly.");
6324919Sxy150489 		return (DDI_FAILURE);
6334919Sxy150489 	}
6344919Sxy150489 
6354919Sxy150489 	return (DDI_SUCCESS);
6364919Sxy150489 }
6374919Sxy150489 
6384919Sxy150489 static int
6394919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6404919Sxy150489 {
6414919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6424919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6434919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6444919Sxy150489 	off_t mem_size;
6454919Sxy150489 
6464919Sxy150489 	/*
6474919Sxy150489 	 * first get the size of device register to be mapped. The
6484919Sxy150489 	 * second parameter is the register we are interested. I our
6494919Sxy150489 	 * wiseman 0 is for config registers and 1 is for memory mapped
6504919Sxy150489 	 * registers Mem size should have memory mapped region size
6514919Sxy150489 	 */
6524919Sxy150489 	if (ddi_dev_regsize(devinfo, 1, &mem_size) != DDI_SUCCESS) {
6534919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6544919Sxy150489 		    "ddi_dev_regsize for registers failed");
6554919Sxy150489 		return (DDI_FAILURE);
6564919Sxy150489 	}
6574919Sxy150489 
6584919Sxy150489 	if ((ddi_regs_map_setup(devinfo, 1, /* register of interest */
6594919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6604919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6614919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6624919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6634919Sxy150489 		goto regs_map_fail;
6644919Sxy150489 	}
6654919Sxy150489 
6664919Sxy150489 	/* ICH needs to map flash memory */
6674919Sxy150489 	if (hw->mac.type == e1000_ich8lan || hw->mac.type == e1000_ich9lan) {
6684919Sxy150489 		/* get flash size */
6694919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6704919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6714919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6724919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6734919Sxy150489 			goto regs_map_fail;
6744919Sxy150489 		}
6754919Sxy150489 
6764919Sxy150489 		/* map flash in */
6774919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6784919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6794919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6804919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6814919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6824919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6834919Sxy150489 			goto regs_map_fail;
6844919Sxy150489 		}
6854919Sxy150489 	}
6864919Sxy150489 
6874919Sxy150489 	return (DDI_SUCCESS);
6884919Sxy150489 
6894919Sxy150489 regs_map_fail:
6904919Sxy150489 	if (osdep->reg_handle != NULL)
6914919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
6924919Sxy150489 
6934919Sxy150489 	return (DDI_FAILURE);
6944919Sxy150489 }
6954919Sxy150489 
6964919Sxy150489 static int
6973526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
6983526Sxy150489 {
6993526Sxy150489 	struct e1000_hw *hw;
7004919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
7013526Sxy150489 
7024919Sxy150489 	hw = &Adapter->shared;
7034919Sxy150489 
7044919Sxy150489 	/* Set MAC type and initialize hardware functions */
7054919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7064919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7074919Sxy150489 		    "Could not setup hardware functions");
7083526Sxy150489 		return (DDI_FAILURE);
7093526Sxy150489 	}
7103526Sxy150489 
7114919Sxy150489 	/* Get bus information */
7124919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7134919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
7144919Sxy150489 		    "Could not get bus information");
7154919Sxy150489 		return (DDI_FAILURE);
7163526Sxy150489 	}
7173526Sxy150489 
7183526Sxy150489 	/* get mem_base addr */
7194919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7204919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7213526Sxy150489 
7223526Sxy150489 	/* get io_base addr */
7234919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7244919Sxy150489 		if (bar64) {
7253526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7264919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7274919Sxy150489 			    PCI_CONF_BASE4);
7283526Sxy150489 		} else {
7293526Sxy150489 			/* normal 32-bit BAR mode */
7304919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7314919Sxy150489 			    PCI_CONF_BASE2);
7323526Sxy150489 		}
7333526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7343526Sxy150489 	} else {
7353526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7363526Sxy150489 		hw->io_base = 0x0;
7373526Sxy150489 	}
7383526Sxy150489 
7394919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7404919Sxy150489 
7414919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7424919Sxy150489 
7436735Scc210113 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
7446735Scc210113 	hw->phy.autoneg_wait_to_complete = B_FALSE;
7453526Sxy150489 
7463526Sxy150489 	/* Adaptive IFS related changes */
7474919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7484919Sxy150489 
7494919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7504919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7514919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7524919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7534919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7544919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7554919Sxy150489 
7564919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7574919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7583526Sxy150489 
7594608Syy150190 #ifdef __sparc
7604608Syy150190 	Adapter->strip_crc = B_TRUE;
7614608Syy150190 #else
7624608Syy150190 	Adapter->strip_crc = B_FALSE;
7634608Syy150190 #endif
7644608Syy150190 
7653526Sxy150489 	/* Get conf file properties */
7664919Sxy150489 	e1000g_get_conf(Adapter);
7674919Sxy150489 
7684919Sxy150489 	/* Get speed/duplex settings in conf file */
7694919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7704919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7713526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7723526Sxy150489 
7734919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7743526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7753526Sxy150489 
7763526Sxy150489 	/* Set Rx/Tx buffer size */
7776394Scc210113 	e1000g_set_bufsize(Adapter);
7784919Sxy150489 
7794919Sxy150489 	/* Master Latency Timer */
7804919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
7814919Sxy150489 
7823526Sxy150489 	/* copper options */
7836735Scc210113 	if (hw->phy.media_type == e1000_media_type_copper) {
7844919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
7854919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
7864919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
7873526Sxy150489 	}
7883526Sxy150489 
7894919Sxy150489 	/* The initial link state should be "unknown" */
7904061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
7914061Sxy150489 
7925882Syy150190 	/* Initialize rx parameters */
7935882Syy150190 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
7945882Syy150190 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
7955882Syy150190 
7964919Sxy150489 	/* Initialize tx parameters */
7974919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
7984919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
7995882Syy150190 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
8005882Syy150190 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
8014919Sxy150489 
8024919Sxy150489 	/* Initialize rx parameters */
8034919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8044919Sxy150489 
8053526Sxy150489 	return (DDI_SUCCESS);
8063526Sxy150489 }
8073526Sxy150489 
8086394Scc210113 static void
8096394Scc210113 e1000g_set_bufsize(struct e1000g *Adapter)
8106394Scc210113 {
8116394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
8126394Scc210113 	uint64_t rx_size;
8136394Scc210113 	uint64_t tx_size;
8146394Scc210113 
8156394Scc210113 #ifdef __sparc
8166394Scc210113 	dev_info_t *devinfo = Adapter->dip;
8176394Scc210113 	ulong_t iommu_pagesize;
8186394Scc210113 
8196394Scc210113 	/* Get the system page size */
8206394Scc210113 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
8216394Scc210113 	iommu_pagesize = dvma_pagesize(devinfo);
8226394Scc210113 	if (iommu_pagesize != 0) {
8236394Scc210113 		if (Adapter->sys_page_sz == iommu_pagesize) {
8246394Scc210113 			if (iommu_pagesize > 0x4000)
8256394Scc210113 				Adapter->sys_page_sz = 0x4000;
8266394Scc210113 		} else {
8276394Scc210113 			if (Adapter->sys_page_sz > iommu_pagesize)
8286394Scc210113 				Adapter->sys_page_sz = iommu_pagesize;
8296394Scc210113 		}
8306394Scc210113 	}
8316986Smx205022 	if (Adapter->lso_enable) {
8326986Smx205022 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
8336986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8346986Smx205022 	} else {
8356986Smx205022 		Adapter->dvma_page_num = Adapter->max_frame_size /
8366986Smx205022 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
8376986Smx205022 	}
8386394Scc210113 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
8396394Scc210113 #endif
8406394Scc210113 
8416735Scc210113 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
8426735Scc210113 
8436735Scc210113 	rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
8446394Scc210113 	if ((rx_size > FRAME_SIZE_UPTO_2K) && (rx_size <= FRAME_SIZE_UPTO_4K))
8456394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
8466394Scc210113 	else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
8476394Scc210113 	    (rx_size <= FRAME_SIZE_UPTO_8K))
8486394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
8496394Scc210113 	else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
8506394Scc210113 	    (rx_size <= FRAME_SIZE_UPTO_16K))
8516394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
8526394Scc210113 	else
8536394Scc210113 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
8546394Scc210113 
8556735Scc210113 	tx_size = Adapter->max_frame_size;
8566394Scc210113 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
8576394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
8586394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
8596394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_8K))
8606394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
8616394Scc210113 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
8626394Scc210113 	    (tx_size <= FRAME_SIZE_UPTO_16K))
8636394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
8646394Scc210113 	else
8656394Scc210113 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
8666394Scc210113 
8676394Scc210113 	/*
8686394Scc210113 	 * For Wiseman adapters we have an requirement of having receive
8696394Scc210113 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8706394Scc210113 	 * require this and forcing it for all hardwares will have
8716394Scc210113 	 * performance implications, I am making it applicable only for
8726394Scc210113 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8736394Scc210113 	 * it is okay to have normal frames...but it does involve a
8746394Scc210113 	 * potential risk where we may loose data if buffer is not
8756394Scc210113 	 * aligned...so all wiseman boards to have 256 byte aligned
8766394Scc210113 	 * buffers
8776394Scc210113 	 */
8786394Scc210113 	if (mac->type < e1000_82543)
8796394Scc210113 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
8806394Scc210113 	else
8816394Scc210113 		Adapter->rx_buf_align = 1;
8826394Scc210113 }
8836394Scc210113 
8843526Sxy150489 /*
8854919Sxy150489  * e1000g_detach - driver detach
8864919Sxy150489  *
8874919Sxy150489  * The detach() function is the complement of the attach routine.
8884919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
8894919Sxy150489  * state  associated  with  a  given  instance of a device node
8904919Sxy150489  * prior to the removal of that instance from the system.
8914919Sxy150489  *
8924919Sxy150489  * The detach() function will be called once for each  instance
8934919Sxy150489  * of the device for which there has been a successful attach()
8944919Sxy150489  * once there are no longer  any  opens  on  the  device.
8954919Sxy150489  *
8964919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
8974919Sxy150489  * driver are freed.
8983526Sxy150489  */
8993526Sxy150489 static int
9004919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
9013526Sxy150489 {
9023526Sxy150489 	struct e1000g *Adapter;
9034982Syy150190 	boolean_t rx_drain;
9043526Sxy150489 
9053526Sxy150489 	switch (cmd) {
9063526Sxy150489 	default:
9073526Sxy150489 		return (DDI_FAILURE);
9083526Sxy150489 
9093526Sxy150489 	case DDI_SUSPEND:
9103526Sxy150489 		return (e1000g_suspend(devinfo));
9113526Sxy150489 
9123526Sxy150489 	case DDI_DETACH:
9133526Sxy150489 		break;
9143526Sxy150489 	}
9153526Sxy150489 
9163526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
9173526Sxy150489 	if (Adapter == NULL)
9183526Sxy150489 		return (DDI_FAILURE);
9193526Sxy150489 
9204919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9214919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
9224919Sxy150489 		return (DDI_FAILURE);
9234919Sxy150489 	}
9244919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
9254919Sxy150489 
9265273Sgl147354 
9275273Sgl147354 	if (Adapter->chip_state != E1000G_STOP)
9284919Sxy150489 		e1000g_stop(Adapter, B_TRUE);
9293526Sxy150489 
9304982Syy150190 	rx_drain = e1000g_rx_drain(Adapter);
9314982Syy150190 
9324982Syy150190 	/*
9334982Syy150190 	 * If e1000g_force_detach is enabled, driver detach is safe.
9344982Syy150190 	 * We will let e1000g_free_priv_devi_node routine determine
9354982Syy150190 	 * whether we need to free the priv_dip entry for current
9364982Syy150190 	 * driver instance.
9374982Syy150190 	 */
9384982Syy150190 	if (e1000g_force_detach) {
9394982Syy150190 		e1000g_free_priv_devi_node(Adapter, rx_drain);
9404982Syy150190 	} else {
9414982Syy150190 		if (!rx_drain)
9423526Sxy150489 			return (DDI_FAILURE);
9433526Sxy150489 	}
9443526Sxy150489 
9453526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9463526Sxy150489 
9473526Sxy150489 	return (DDI_SUCCESS);
9483526Sxy150489 }
9493526Sxy150489 
9504982Syy150190 /*
9514982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
9524982Syy150190  *
9534982Syy150190  * If free_flag is true, that indicates the upper layer is not holding
9544982Syy150190  * the rx buffers, we could free the priv_dip entry safely.
9554982Syy150190  *
9564982Syy150190  * Otherwise, we have to keep this entry even after driver detached,
9574982Syy150190  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
9584982Syy150190  * so that driver could free it while all of rx buffers are returned
9594982Syy150190  * by upper layer later.
9604982Syy150190  */
9614982Syy150190 static void
9624982Syy150190 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
9634982Syy150190 {
9644982Syy150190 	private_devi_list_t *devi_node, *devi_del;
9654982Syy150190 
9664982Syy150190 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
9674982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
9684982Syy150190 	ASSERT(Adapter->priv_dip != NULL);
9694982Syy150190 
9704982Syy150190 	devi_node = e1000g_private_devi_list;
9714982Syy150190 	if (devi_node->priv_dip == Adapter->priv_dip) {
9724982Syy150190 		if (free_flag) {
9734982Syy150190 			e1000g_private_devi_list =
9744982Syy150190 			    devi_node->next;
9754982Syy150190 			kmem_free(devi_node->priv_dip,
9764982Syy150190 			    sizeof (struct dev_info));
9774982Syy150190 			kmem_free(devi_node,
9784982Syy150190 			    sizeof (private_devi_list_t));
9794982Syy150190 		} else {
9804982Syy150190 			ASSERT(e1000g_mblks_pending != 0);
9814982Syy150190 			devi_node->flag =
9824982Syy150190 			    E1000G_PRIV_DEVI_DETACH;
9834982Syy150190 		}
9844982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
9854982Syy150190 		return;
9864982Syy150190 	}
9874982Syy150190 
9884982Syy150190 	devi_node = e1000g_private_devi_list;
9894982Syy150190 	while (devi_node->next != NULL) {
9904982Syy150190 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
9914982Syy150190 			if (free_flag) {
9924982Syy150190 				devi_del = devi_node->next;
9934982Syy150190 				devi_node->next = devi_del->next;
9944982Syy150190 				kmem_free(devi_del->priv_dip,
9954982Syy150190 				    sizeof (struct dev_info));
9964982Syy150190 				kmem_free(devi_del,
9974982Syy150190 				    sizeof (private_devi_list_t));
9984982Syy150190 			} else {
9994982Syy150190 				ASSERT(e1000g_mblks_pending != 0);
10004982Syy150190 				devi_node->next->flag =
10014982Syy150190 				    E1000G_PRIV_DEVI_DETACH;
10024982Syy150190 			}
10034982Syy150190 			break;
10044982Syy150190 		}
10054982Syy150190 		devi_node = devi_node->next;
10064982Syy150190 	}
10074982Syy150190 	rw_exit(&e1000g_rx_detach_lock);
10084982Syy150190 }
10094982Syy150190 
10103526Sxy150489 static void
10113526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
10123526Sxy150489 {
10137133Scc210113 	int result;
10147133Scc210113 
10154919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
10163526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
10173526Sxy150489 	}
10183526Sxy150489 
10194919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
10203526Sxy150489 		(void) mac_unregister(Adapter->mh);
10213526Sxy150489 	}
10223526Sxy150489 
10234919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
10243526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
10253526Sxy150489 	}
10263526Sxy150489 
10274919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10283526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10293526Sxy150489 	}
10303526Sxy150489 
10313526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10323526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10333526Sxy150489 	}
10343526Sxy150489 
10353526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10364919Sxy150489 		stop_link_timer(Adapter);
10377133Scc210113 
10387133Scc210113 		mutex_enter(&e1000g_nvm_lock);
10397133Scc210113 		result = e1000_reset_hw(&Adapter->shared);
10407133Scc210113 		mutex_exit(&e1000g_nvm_lock);
10417133Scc210113 
10427133Scc210113 		if (result != E1000_SUCCESS) {
10435273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10445273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10455273Sgl147354 		}
10463526Sxy150489 	}
10473526Sxy150489 
10484919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10494919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10504919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10514919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10524919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10533526Sxy150489 	}
10543526Sxy150489 
10554919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10564919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10574919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10583526Sxy150489 	}
10593526Sxy150489 
10603526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10613526Sxy150489 		e1000g_destroy_locks(Adapter);
10623526Sxy150489 	}
10633526Sxy150489 
10645273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10655273Sgl147354 		e1000g_fm_fini(Adapter);
10665273Sgl147354 	}
10675273Sgl147354 
10684919Sxy150489 	e1000_remove_device(&Adapter->shared);
10694919Sxy150489 
10703526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
10713526Sxy150489 
10723526Sxy150489 	/*
10733526Sxy150489 	 * Another hotplug spec requirement,
10743526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
10753526Sxy150489 	 */
10763526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
10773526Sxy150489 }
10783526Sxy150489 
10793526Sxy150489 static void
10803526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
10813526Sxy150489 {
10823526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10833526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10843526Sxy150489 
10853526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
10863526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10874919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
10883526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10894919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
10903526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10913526Sxy150489 
10923526Sxy150489 	tx_ring = Adapter->tx_ring;
10933526Sxy150489 
10943526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
10953526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10963526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
10973526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10983526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
10993526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11003526Sxy150489 
11013526Sxy150489 	rx_ring = Adapter->rx_ring;
11023526Sxy150489 
11033526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
11043526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
11053526Sxy150489 }
11063526Sxy150489 
11073526Sxy150489 static void
11083526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
11093526Sxy150489 {
11103526Sxy150489 	e1000g_tx_ring_t *tx_ring;
11113526Sxy150489 	e1000g_rx_ring_t *rx_ring;
11123526Sxy150489 
11133526Sxy150489 	tx_ring = Adapter->tx_ring;
11143526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
11153526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
11163526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
11173526Sxy150489 
11183526Sxy150489 	rx_ring = Adapter->rx_ring;
11193526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
11203526Sxy150489 
11214919Sxy150489 	mutex_destroy(&Adapter->link_lock);
11224919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
11233526Sxy150489 	rw_destroy(&Adapter->chip_lock);
11243526Sxy150489 }
11253526Sxy150489 
11263526Sxy150489 static int
11273526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11283526Sxy150489 {
11293526Sxy150489 	struct e1000g *Adapter;
11303526Sxy150489 
11313526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11323526Sxy150489 	if (Adapter == NULL)
11333526Sxy150489 		return (DDI_FAILURE);
11343526Sxy150489 
11354919Sxy150489 	if (e1000g_start(Adapter, B_TRUE))
11363526Sxy150489 		return (DDI_FAILURE);
11373526Sxy150489 
11383526Sxy150489 	return (DDI_SUCCESS);
11393526Sxy150489 }
11403526Sxy150489 
11413526Sxy150489 static int
11423526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11433526Sxy150489 {
11443526Sxy150489 	struct e1000g *Adapter;
11453526Sxy150489 
11463526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11473526Sxy150489 	if (Adapter == NULL)
11483526Sxy150489 		return (DDI_FAILURE);
11493526Sxy150489 
11504919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
11513526Sxy150489 
11523526Sxy150489 	return (DDI_SUCCESS);
11533526Sxy150489 }
11543526Sxy150489 
11553526Sxy150489 static int
11563526Sxy150489 e1000g_init(struct e1000g *Adapter)
11573526Sxy150489 {
11583526Sxy150489 	uint32_t pba;
11594919Sxy150489 	uint32_t high_water;
11603526Sxy150489 	struct e1000_hw *hw;
11614061Sxy150489 	clock_t link_timeout;
11627133Scc210113 	int result;
11633526Sxy150489 
11644919Sxy150489 	hw = &Adapter->shared;
11653526Sxy150489 
11663526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11673526Sxy150489 
11683526Sxy150489 	/*
11693526Sxy150489 	 * reset to put the hardware in a known state
11703526Sxy150489 	 * before we try to do anything with the eeprom
11713526Sxy150489 	 */
11727133Scc210113 	mutex_enter(&e1000g_nvm_lock);
11737133Scc210113 	result = e1000_reset_hw(hw);
11747133Scc210113 	mutex_exit(&e1000g_nvm_lock);
11757133Scc210113 
11767133Scc210113 	if (result != E1000_SUCCESS) {
11775273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11785273Sgl147354 		goto init_fail;
11795273Sgl147354 	}
11803526Sxy150489 
11817133Scc210113 	mutex_enter(&e1000g_nvm_lock);
11827133Scc210113 	result = e1000_validate_nvm_checksum(hw);
11837133Scc210113 	if (result < E1000_SUCCESS) {
11844061Sxy150489 		/*
11854061Sxy150489 		 * Some PCI-E parts fail the first check due to
11864061Sxy150489 		 * the link being in sleep state.  Call it again,
11874061Sxy150489 		 * if it fails a second time its a real issue.
11884061Sxy150489 		 */
11897133Scc210113 		result = e1000_validate_nvm_checksum(hw);
11907133Scc210113 	}
11917133Scc210113 	mutex_exit(&e1000g_nvm_lock);
11927133Scc210113 
11937133Scc210113 	if (result < E1000_SUCCESS) {
11947133Scc210113 		e1000g_log(Adapter, CE_WARN,
11957133Scc210113 		    "Invalid NVM checksum. Please contact "
11967133Scc210113 		    "the vendor to update the NVM.");
11977133Scc210113 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11987133Scc210113 		goto init_fail;
11997133Scc210113 	}
12007133Scc210113 
12017133Scc210113 	result = 0;
12023526Sxy150489 #ifdef __sparc
12033526Sxy150489 	/*
12043526Sxy150489 	 * Firstly, we try to get the local ethernet address from OBP. If
12057133Scc210113 	 * failed, then we get it from the EEPROM of NIC card.
12063526Sxy150489 	 */
12077133Scc210113 	result = e1000g_find_mac_address(Adapter);
12087133Scc210113 #endif
12093526Sxy150489 	/* Get the local ethernet address. */
12107133Scc210113 	if (!result) {
12117133Scc210113 		mutex_enter(&e1000g_nvm_lock);
12127140Scc210113 		result = e1000_read_mac_addr(hw);
12137133Scc210113 		mutex_exit(&e1000g_nvm_lock);
12147133Scc210113 	}
12157133Scc210113 
12167133Scc210113 	if (result < E1000_SUCCESS) {
12173526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
12185273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12193526Sxy150489 		goto init_fail;
12203526Sxy150489 	}
12213526Sxy150489 
12223526Sxy150489 	/* check for valid mac address */
12234919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12243526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
12255273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12263526Sxy150489 		goto init_fail;
12273526Sxy150489 	}
12283526Sxy150489 
12294919Sxy150489 	/* Set LAA state for 82571 chipset */
12304919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
12313526Sxy150489 
12323526Sxy150489 	/* Master Latency Timer implementation */
12334919Sxy150489 	if (Adapter->master_latency_timer) {
12344919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
12354919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
12363526Sxy150489 	}
12373526Sxy150489 
12384919Sxy150489 	if (hw->mac.type < e1000_82547) {
12393526Sxy150489 		/*
12403526Sxy150489 		 * Total FIFO is 64K
12413526Sxy150489 		 */
12426735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12433526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
12443526Sxy150489 		else
12453526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
12467140Scc210113 	} else if ((hw->mac.type == e1000_82571) ||
12477140Scc210113 	    (hw->mac.type == e1000_82572) ||
12487140Scc210113 	    (hw->mac.type == e1000_80003es2lan)) {
12493526Sxy150489 		/*
12503526Sxy150489 		 * Total FIFO is 48K
12513526Sxy150489 		 */
12526735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12533526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
12543526Sxy150489 		else
12553526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
12564919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
12573526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
12584919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
12594919Sxy150489 		pba = E1000_PBA_12K;
12603526Sxy150489 	} else {
12613526Sxy150489 		/*
12623526Sxy150489 		 * Total FIFO is 40K
12633526Sxy150489 		 */
12646735Scc210113 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
12653526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
12663526Sxy150489 		else
12673526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
12683526Sxy150489 	}
12694919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
12703526Sxy150489 
12713526Sxy150489 	/*
12723526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
12733526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
12743526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
12753526Sxy150489 	 * file.
12763526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
12773526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
12783526Sxy150489 	 * high-water mark.
12794919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
12804919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
12814919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
12824919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
12834919Sxy150489 	 * Rx FIFO size minus one full frame.
12843526Sxy150489 	 */
12854919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
12864919Sxy150489 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_ich9lan) ?
12874919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
12886735Scc210113 	    ((pba << 10) - Adapter->max_frame_size)));
12896735Scc210113 
12906735Scc210113 	hw->fc.high_water = high_water & 0xFFF8;
12916735Scc210113 	hw->fc.low_water = hw->fc.high_water - 8;
12924919Sxy150489 
12934919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
12946735Scc210113 		hw->fc.pause_time = 0xFFFF;
12954919Sxy150489 	else
12966735Scc210113 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
12976735Scc210113 	hw->fc.send_xon = B_TRUE;
12983526Sxy150489 
12993526Sxy150489 	/*
13003526Sxy150489 	 * Reset the adapter hardware the second time.
13013526Sxy150489 	 */
13027133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13037133Scc210113 	result = e1000_reset_hw(hw);
13047133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13057133Scc210113 
13067133Scc210113 	if (result != E1000_SUCCESS) {
13075273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13085273Sgl147354 		goto init_fail;
13095273Sgl147354 	}
13103526Sxy150489 
13113526Sxy150489 	/* disable wakeup control by default */
13124919Sxy150489 	if (hw->mac.type >= e1000_82544)
13134919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
13143526Sxy150489 
13153526Sxy150489 	/* MWI setup */
13164919Sxy150489 	e1000_pci_set_mwi(hw);
13173526Sxy150489 
13183526Sxy150489 	/*
13193526Sxy150489 	 * Configure/Initialize hardware
13203526Sxy150489 	 */
13217133Scc210113 	mutex_enter(&e1000g_nvm_lock);
13227133Scc210113 	result = e1000_init_hw(hw);
13237133Scc210113 	mutex_exit(&e1000g_nvm_lock);
13247133Scc210113 
13257133Scc210113 	if (result < E1000_SUCCESS) {
13263526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
13275273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
13283526Sxy150489 		goto init_fail;
13293526Sxy150489 	}
13303526Sxy150489 
13313526Sxy150489 	/* Disable Smart Power Down */
13323526Sxy150489 	phy_spd_state(hw, B_FALSE);
13333526Sxy150489 
13345082Syy150190 	/* Make sure driver has control */
13355082Syy150190 	e1000g_get_driver_control(hw);
13365082Syy150190 
13373526Sxy150489 	/*
13383526Sxy150489 	 * Initialize unicast addresses.
13393526Sxy150489 	 */
13403526Sxy150489 	e1000g_init_unicst(Adapter);
13413526Sxy150489 
13423526Sxy150489 	/*
13433526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
13443526Sxy150489 	 * completes  Multicast table will be set
13453526Sxy150489 	 */
13464919Sxy150489 	e1000g_setup_multicast(Adapter);
13474919Sxy150489 	msec_delay(5);
13483526Sxy150489 
13493526Sxy150489 	/*
13503526Sxy150489 	 * Implement Adaptive IFS
13513526Sxy150489 	 */
13523526Sxy150489 	e1000_reset_adaptive(hw);
13533526Sxy150489 
13543526Sxy150489 	/* Setup Interrupt Throttling Register */
13555882Syy150190 	if (hw->mac.type >= e1000_82540) {
13565882Syy150190 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
13575882Syy150190 	} else
13585882Syy150190 		Adapter->intr_adaptive = B_FALSE;
13593526Sxy150489 
13604061Sxy150489 	/* Start the timer for link setup */
13614919Sxy150489 	if (hw->mac.autoneg)
13624919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
13634061Sxy150489 	else
13644919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
13654919Sxy150489 
13664919Sxy150489 	mutex_enter(&Adapter->link_lock);
13676735Scc210113 	if (hw->phy.autoneg_wait_to_complete) {
13684061Sxy150489 		Adapter->link_complete = B_TRUE;
13693526Sxy150489 	} else {
13704061Sxy150489 		Adapter->link_complete = B_FALSE;
13714061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
13724061Sxy150489 		    (void *)Adapter, link_timeout);
13733526Sxy150489 	}
13744919Sxy150489 	mutex_exit(&Adapter->link_lock);
13753526Sxy150489 
13763526Sxy150489 	/* Enable PCI-Ex master */
13774919Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
13783526Sxy150489 		e1000_enable_pciex_master(hw);
13793526Sxy150489 	}
13803526Sxy150489 
13815082Syy150190 	/* Save the state of the phy */
13825082Syy150190 	e1000g_get_phy_state(Adapter);
13835082Syy150190 
13846512Ssowmini 	e1000g_param_sync(Adapter);
13856512Ssowmini 
13863526Sxy150489 	Adapter->init_count++;
13873526Sxy150489 
13885273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
13895273Sgl147354 		goto init_fail;
13905273Sgl147354 	}
13915273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
13925273Sgl147354 		goto init_fail;
13935273Sgl147354 	}
13945273Sgl147354 
13953526Sxy150489 	rw_exit(&Adapter->chip_lock);
13963526Sxy150489 
13973526Sxy150489 	return (DDI_SUCCESS);
13983526Sxy150489 
13993526Sxy150489 init_fail:
14003526Sxy150489 	rw_exit(&Adapter->chip_lock);
14015273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
14023526Sxy150489 	return (DDI_FAILURE);
14033526Sxy150489 }
14043526Sxy150489 
14053526Sxy150489 /*
14063526Sxy150489  * Check if the link is up
14073526Sxy150489  */
14083526Sxy150489 static boolean_t
14093526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
14103526Sxy150489 {
14113526Sxy150489 	struct e1000_hw *hw;
14123526Sxy150489 	boolean_t link_up;
14133526Sxy150489 
14144919Sxy150489 	hw = &Adapter->shared;
14153526Sxy150489 
1416*7426SChenliang.Xu@Sun.COM 	(void) e1000_check_for_link(hw);
14173526Sxy150489 
14184919Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
14194919Sxy150489 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
14206735Scc210113 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
14214919Sxy150489 	    (hw->mac.serdes_has_link))) {
14223526Sxy150489 		link_up = B_TRUE;
14233526Sxy150489 	} else {
14243526Sxy150489 		link_up = B_FALSE;
14253526Sxy150489 	}
14263526Sxy150489 
14273526Sxy150489 	return (link_up);
14283526Sxy150489 }
14293526Sxy150489 
14303526Sxy150489 static void
14313526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
14323526Sxy150489 {
14333526Sxy150489 	struct iocblk *iocp;
14343526Sxy150489 	struct e1000g *e1000gp;
14353526Sxy150489 	enum ioc_reply status;
1436*7426SChenliang.Xu@Sun.COM 
1437*7426SChenliang.Xu@Sun.COM 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
14383526Sxy150489 	iocp->ioc_error = 0;
14393526Sxy150489 	e1000gp = (struct e1000g *)arg;
14403526Sxy150489 
14413526Sxy150489 	ASSERT(e1000gp);
14423526Sxy150489 	if (e1000gp == NULL) {
14433526Sxy150489 		miocnak(q, mp, 0, EINVAL);
14443526Sxy150489 		return;
14453526Sxy150489 	}
14463526Sxy150489 
14473526Sxy150489 	switch (iocp->ioc_cmd) {
14483526Sxy150489 
14493526Sxy150489 	case LB_GET_INFO_SIZE:
14503526Sxy150489 	case LB_GET_INFO:
14513526Sxy150489 	case LB_GET_MODE:
14523526Sxy150489 	case LB_SET_MODE:
14533526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
14543526Sxy150489 		break;
14553526Sxy150489 
14563526Sxy150489 
14574919Sxy150489 #ifdef E1000G_DEBUG
14583526Sxy150489 	case E1000G_IOC_REG_PEEK:
14593526Sxy150489 	case E1000G_IOC_REG_POKE:
14603526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
14613526Sxy150489 		break;
14623526Sxy150489 	case E1000G_IOC_CHIP_RESET:
14633526Sxy150489 		e1000gp->reset_count++;
14643526Sxy150489 		if (e1000g_reset(e1000gp))
14653526Sxy150489 			status = IOC_ACK;
14663526Sxy150489 		else
14673526Sxy150489 			status = IOC_INVAL;
14683526Sxy150489 		break;
14694919Sxy150489 #endif
14703526Sxy150489 	default:
14713526Sxy150489 		status = IOC_INVAL;
14723526Sxy150489 		break;
14733526Sxy150489 	}
14743526Sxy150489 
14753526Sxy150489 	/*
14763526Sxy150489 	 * Decide how to reply
14773526Sxy150489 	 */
14783526Sxy150489 	switch (status) {
14793526Sxy150489 	default:
14803526Sxy150489 	case IOC_INVAL:
14813526Sxy150489 		/*
14823526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
14833526Sxy150489 		 */
14843526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
14854349Sxy150489 		    EINVAL : iocp->ioc_error);
14863526Sxy150489 		break;
14873526Sxy150489 
14883526Sxy150489 	case IOC_DONE:
14893526Sxy150489 		/*
14903526Sxy150489 		 * OK, reply already sent
14913526Sxy150489 		 */
14923526Sxy150489 		break;
14933526Sxy150489 
14943526Sxy150489 	case IOC_ACK:
14953526Sxy150489 		/*
14963526Sxy150489 		 * OK, reply with an ACK
14973526Sxy150489 		 */
14983526Sxy150489 		miocack(q, mp, 0, 0);
14993526Sxy150489 		break;
15003526Sxy150489 
15013526Sxy150489 	case IOC_REPLY:
15023526Sxy150489 		/*
15033526Sxy150489 		 * OK, send prepared reply as ACK or NAK
15043526Sxy150489 		 */
15053526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
15064349Sxy150489 		    M_IOCACK : M_IOCNAK;
15073526Sxy150489 		qreply(q, mp);
15083526Sxy150489 		break;
15093526Sxy150489 	}
15103526Sxy150489 }
15113526Sxy150489 
15123526Sxy150489 static int
15133526Sxy150489 e1000g_m_start(void *arg)
15143526Sxy150489 {
15153526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15163526Sxy150489 
15174919Sxy150489 	return (e1000g_start(Adapter, B_TRUE));
15183526Sxy150489 }
15193526Sxy150489 
15203526Sxy150489 static int
15214919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
15223526Sxy150489 {
15234919Sxy150489 	if (global) {
15244919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
15254919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
15264919Sxy150489 			e1000g_log(Adapter, CE_WARN,
15274919Sxy150489 			    "Alloc DMA resources failed");
15284919Sxy150489 			return (ENOTACTIVE);
15294919Sxy150489 		}
15304919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
15314919Sxy150489 	}
15324919Sxy150489 
15333526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
15343526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
15353526Sxy150489 			e1000g_log(Adapter, CE_WARN,
15363526Sxy150489 			    "Adapter initialization failed");
15374919Sxy150489 			if (global)
15384919Sxy150489 				e1000g_release_dma_resources(Adapter);
15393526Sxy150489 			return (ENOTACTIVE);
15403526Sxy150489 		}
15413526Sxy150489 	}
15423526Sxy150489 
15433526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15443526Sxy150489 
15454919Sxy150489 	/* Setup and initialize the transmit structures */
15464919Sxy150489 	e1000g_tx_setup(Adapter);
15474919Sxy150489 	msec_delay(5);
15484919Sxy150489 
15494919Sxy150489 	/* Setup and initialize the receive structures */
15504919Sxy150489 	e1000g_rx_setup(Adapter);
15514919Sxy150489 	msec_delay(5);
15524919Sxy150489 
15534919Sxy150489 	e1000g_mask_interrupt(Adapter);
15543526Sxy150489 
15555273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
15565273Sgl147354 		rw_exit(&Adapter->chip_lock);
15575273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
15585273Sgl147354 		return (ENOTACTIVE);
15595273Sgl147354 	}
15605273Sgl147354 
15615273Sgl147354 	Adapter->chip_state = E1000G_START;
15623526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
15633526Sxy150489 
15643526Sxy150489 	rw_exit(&Adapter->chip_lock);
15653526Sxy150489 
15664919Sxy150489 	/* Enable and start the watchdog timer */
15674919Sxy150489 	enable_watchdog_timer(Adapter);
15684919Sxy150489 
15693526Sxy150489 	return (0);
15703526Sxy150489 }
15713526Sxy150489 
15723526Sxy150489 static void
15733526Sxy150489 e1000g_m_stop(void *arg)
15743526Sxy150489 {
15753526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15763526Sxy150489 
15774919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
15783526Sxy150489 }
15793526Sxy150489 
15803526Sxy150489 static void
15814919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
15823526Sxy150489 {
15837133Scc210113 	int result;
15847133Scc210113 
15853526Sxy150489 	/* Set stop flags */
15863526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15873526Sxy150489 
15885273Sgl147354 	Adapter->chip_state = E1000G_STOP;
15893526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
15903526Sxy150489 
15913526Sxy150489 	rw_exit(&Adapter->chip_lock);
15923526Sxy150489 
15933526Sxy150489 	/* Drain tx sessions */
15943526Sxy150489 	(void) e1000g_tx_drain(Adapter);
15953526Sxy150489 
15964919Sxy150489 	/* Disable and stop all the timers */
15974919Sxy150489 	disable_watchdog_timer(Adapter);
15984919Sxy150489 	stop_link_timer(Adapter);
15994919Sxy150489 	stop_82547_timer(Adapter->tx_ring);
16004061Sxy150489 
16013526Sxy150489 	/* Stop the chip and release pending resources */
16023526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
16033526Sxy150489 
16044919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
16057133Scc210113 
16067133Scc210113 	mutex_enter(&e1000g_nvm_lock);
16077133Scc210113 	result = e1000_reset_hw(&Adapter->shared);
16087133Scc210113 	mutex_exit(&e1000g_nvm_lock);
16097133Scc210113 
16107133Scc210113 	if (result != E1000_SUCCESS) {
16115273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
16125273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16135273Sgl147354 	}
16143526Sxy150489 
16153526Sxy150489 	/* Release resources still held by the TX descriptors */
16164919Sxy150489 	e1000g_tx_clean(Adapter);
16174061Sxy150489 
16185273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
16195273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16205273Sgl147354 
16214061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
16224919Sxy150489 	e1000g_rx_clean(Adapter);
16234061Sxy150489 
16244061Sxy150489 	rw_exit(&Adapter->chip_lock);
16254919Sxy150489 
16264919Sxy150489 	if (global)
16274919Sxy150489 		e1000g_release_dma_resources(Adapter);
16284061Sxy150489 }
16294061Sxy150489 
16304061Sxy150489 static void
16314919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
16324919Sxy150489 {
16334919Sxy150489 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
16344919Sxy150489 
16354919Sxy150489 	if (rx_ring->rx_mblk != NULL) {
16364919Sxy150489 		freemsg(rx_ring->rx_mblk);
16374919Sxy150489 		rx_ring->rx_mblk = NULL;
16384919Sxy150489 		rx_ring->rx_mblk_tail = NULL;
16394919Sxy150489 		rx_ring->rx_mblk_len = 0;
16404919Sxy150489 	}
16414919Sxy150489 }
16424919Sxy150489 
16434919Sxy150489 static void
16444919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
16454061Sxy150489 {
16464061Sxy150489 	e1000g_tx_ring_t *tx_ring;
16474919Sxy150489 	p_tx_sw_packet_t packet;
16484061Sxy150489 	mblk_t *mp;
16494061Sxy150489 	mblk_t *nmp;
16504061Sxy150489 	uint32_t packet_count;
16514061Sxy150489 
16524061Sxy150489 	tx_ring = Adapter->tx_ring;
16534061Sxy150489 
16543526Sxy150489 	/*
16553526Sxy150489 	 * Here we don't need to protect the lists using
16563526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
16573526Sxy150489 	 * have been protected by the chip_lock.
16583526Sxy150489 	 */
16593526Sxy150489 	mp = NULL;
16603526Sxy150489 	nmp = NULL;
16614061Sxy150489 	packet_count = 0;
16624919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
16633526Sxy150489 	while (packet != NULL) {
16643526Sxy150489 		if (packet->mp != NULL) {
16653526Sxy150489 			/* Assemble the message chain */
16663526Sxy150489 			if (mp == NULL) {
16673526Sxy150489 				mp = packet->mp;
16683526Sxy150489 				nmp = packet->mp;
16693526Sxy150489 			} else {
16703526Sxy150489 				nmp->b_next = packet->mp;
16713526Sxy150489 				nmp = packet->mp;
16723526Sxy150489 			}
16733526Sxy150489 			/* Disconnect the message from the sw packet */
16743526Sxy150489 			packet->mp = NULL;
16753526Sxy150489 		}
16763526Sxy150489 
16774919Sxy150489 		e1000g_free_tx_swpkt(packet);
16784061Sxy150489 		packet_count++;
16793526Sxy150489 
16804919Sxy150489 		packet = (p_tx_sw_packet_t)
16813526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
16823526Sxy150489 	}
16833526Sxy150489 
16845882Syy150190 	if (mp != NULL)
16855882Syy150190 		freemsgchain(mp);
16864061Sxy150489 
16874061Sxy150489 	if (packet_count > 0) {
16884061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
16894061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
16904061Sxy150489 
16914061Sxy150489 		/* Setup TX descriptor pointers */
16924061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
16934061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
16944061Sxy150489 
16954061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
16966735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
16976735Scc210113 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
16983526Sxy150489 	}
16993526Sxy150489 }
17003526Sxy150489 
17013526Sxy150489 static boolean_t
17023526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
17033526Sxy150489 {
17043526Sxy150489 	int i;
17053526Sxy150489 	boolean_t done;
17063526Sxy150489 	e1000g_tx_ring_t *tx_ring;
17073526Sxy150489 
17083526Sxy150489 	tx_ring = Adapter->tx_ring;
17093526Sxy150489 
17103526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
17114919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
17123526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
17133526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
17143526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
17153526Sxy150489 
17163526Sxy150489 		if (done)
17173526Sxy150489 			break;
17183526Sxy150489 
17193526Sxy150489 		msec_delay(1);
17203526Sxy150489 	}
17213526Sxy150489 
17223526Sxy150489 	return (done);
17233526Sxy150489 }
17243526Sxy150489 
17253526Sxy150489 static boolean_t
17263526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
17273526Sxy150489 {
17284919Sxy150489 	e1000g_rx_ring_t *rx_ring;
17294919Sxy150489 	p_rx_sw_packet_t packet;
17303526Sxy150489 	boolean_t done;
17313526Sxy150489 
17324919Sxy150489 	rx_ring = Adapter->rx_ring;
17334919Sxy150489 	done = B_TRUE;
17344919Sxy150489 
17354919Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
17364919Sxy150489 
17374919Sxy150489 	while (rx_ring->pending_list != NULL) {
17384919Sxy150489 		packet = rx_ring->pending_list;
17394919Sxy150489 		rx_ring->pending_list =
17404919Sxy150489 		    rx_ring->pending_list->next;
17414919Sxy150489 
17424919Sxy150489 		if (packet->flag == E1000G_RX_SW_STOP) {
17434919Sxy150489 			packet->flag = E1000G_RX_SW_DETACH;
17444919Sxy150489 			done = B_FALSE;
17454919Sxy150489 		} else {
17464919Sxy150489 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
17474919Sxy150489 			ASSERT(packet->mp == NULL);
17484919Sxy150489 			e1000g_free_rx_sw_packet(packet);
17494919Sxy150489 		}
17504919Sxy150489 	}
17514919Sxy150489 
17524919Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
17533526Sxy150489 
17543526Sxy150489 	return (done);
17553526Sxy150489 }
17563526Sxy150489 
17574061Sxy150489 boolean_t
17583526Sxy150489 e1000g_reset(struct e1000g *Adapter)
17593526Sxy150489 {
17604919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
17614919Sxy150489 
17624919Sxy150489 	if (e1000g_start(Adapter, B_FALSE)) {
17633526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
17643526Sxy150489 		return (B_FALSE);
17653526Sxy150489 	}
17663526Sxy150489 
17673526Sxy150489 	return (B_TRUE);
17683526Sxy150489 }
17693526Sxy150489 
17705273Sgl147354 boolean_t
17715273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
17725273Sgl147354 {
17735273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
17745273Sgl147354 
17755273Sgl147354 	Adapter->init_count = 0;
17765273Sgl147354 
17775273Sgl147354 	if (e1000g_start(Adapter, B_TRUE)) {
17785273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
17795273Sgl147354 		return (B_FALSE);
17805273Sgl147354 	}
17815273Sgl147354 
17825273Sgl147354 	return (B_TRUE);
17835273Sgl147354 }
17845273Sgl147354 
17853526Sxy150489 /*
17864919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
17874919Sxy150489  *
17884919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
17894919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
17904919Sxy150489  * bit is set.
17913526Sxy150489  */
17923526Sxy150489 static uint_t
17933526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
17943526Sxy150489 {
17953526Sxy150489 	struct e1000g *Adapter;
17964919Sxy150489 	uint32_t icr;
17973526Sxy150489 
1798*7426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
17994919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18004919Sxy150489 
18015273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18025273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18035273Sgl147354 
18044919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
18053526Sxy150489 		/*
18063526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
18073526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18083526Sxy150489 		 * look for work to do.
18093526Sxy150489 		 */
18104919Sxy150489 		e1000g_intr_work(Adapter, icr);
18113526Sxy150489 		return (DDI_INTR_CLAIMED);
18123526Sxy150489 	} else {
18133526Sxy150489 		/*
18143526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
18153526Sxy150489 		 * Don't claim this interrupt, return immediately.
18163526Sxy150489 		 */
18173526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18183526Sxy150489 	}
18193526Sxy150489 }
18203526Sxy150489 
18213526Sxy150489 /*
18224919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
18234919Sxy150489  *
18244919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
18254919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
18264919Sxy150489  * bit is set or not.
18273526Sxy150489  */
18283526Sxy150489 static uint_t
18293526Sxy150489 e1000g_intr(caddr_t arg)
18303526Sxy150489 {
18313526Sxy150489 	struct e1000g *Adapter;
18324919Sxy150489 	uint32_t icr;
18333526Sxy150489 
1834*7426SChenliang.Xu@Sun.COM 	Adapter = (struct e1000g *)(uintptr_t)arg;
18354919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18364919Sxy150489 
18375273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18385273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18395273Sgl147354 
18404919Sxy150489 	if (icr) {
18413526Sxy150489 		/*
18423526Sxy150489 		 * Any bit was set in ICR:
18433526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18443526Sxy150489 		 * look for work to do.
18453526Sxy150489 		 */
18464919Sxy150489 		e1000g_intr_work(Adapter, icr);
18473526Sxy150489 		return (DDI_INTR_CLAIMED);
18483526Sxy150489 	} else {
18493526Sxy150489 		/*
18503526Sxy150489 		 * No bit was set in ICR:
18513526Sxy150489 		 * Don't claim this interrupt, return immediately.
18523526Sxy150489 		 */
18533526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18543526Sxy150489 	}
18553526Sxy150489 }
18563526Sxy150489 
18573526Sxy150489 /*
18584919Sxy150489  * e1000g_intr_work - actual processing of ISR
18594919Sxy150489  *
18604919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
18614919Sxy150489  * processing routines.
18623526Sxy150489  */
18633526Sxy150489 static void
18644919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
18653526Sxy150489 {
18665882Syy150190 	struct e1000_hw *hw;
18675882Syy150190 	hw = &Adapter->shared;
18685882Syy150190 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
18695882Syy150190 	uint32_t itr;
18705882Syy150190 
18715882Syy150190 	Adapter->rx_pkt_cnt = 0;
18725882Syy150190 	Adapter->tx_pkt_cnt = 0;
18735882Syy150190 
18744919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
18754919Sxy150489 	/*
18765273Sgl147354 	 * Here we need to check the "chip_state" flag within the chip_lock to
18774919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
18784919Sxy150489 	 * being reset.
18794919Sxy150489 	 */
18805273Sgl147354 	if (Adapter->chip_state != E1000G_START) {
18814919Sxy150489 		rw_exit(&Adapter->chip_lock);
18824919Sxy150489 		return;
18834919Sxy150489 	}
18844919Sxy150489 
18854919Sxy150489 	if (icr & E1000_ICR_RXT0) {
18863526Sxy150489 		mblk_t *mp;
18873526Sxy150489 
18886735Scc210113 		mutex_enter(&Adapter->rx_ring->rx_lock);
18894919Sxy150489 		mp = e1000g_receive(Adapter);
18906735Scc210113 		mutex_exit(&Adapter->rx_ring->rx_lock);
18914919Sxy150489 
18924919Sxy150489 		rw_exit(&Adapter->chip_lock);
18934919Sxy150489 
18944919Sxy150489 		if (mp != NULL)
18954919Sxy150489 			mac_rx(Adapter->mh, Adapter->mrh, mp);
18964919Sxy150489 	} else
18974919Sxy150489 		rw_exit(&Adapter->chip_lock);
18983526Sxy150489 
18995882Syy150190 	if (icr & E1000_ICR_TXDW) {
19005882Syy150190 		if (!Adapter->tx_intr_enable)
19015882Syy150190 			e1000g_clear_tx_interrupt(Adapter);
19025882Syy150190 
19035882Syy150190 		/* Recycle the tx descriptors */
19045882Syy150190 		rw_enter(&Adapter->chip_lock, RW_READER);
1905*7426SChenliang.Xu@Sun.COM 		(void) e1000g_recycle(tx_ring);
19065882Syy150190 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
19075882Syy150190 		rw_exit(&Adapter->chip_lock);
19085882Syy150190 
19095882Syy150190 		/* Schedule the re-transmit */
19105882Syy150190 		if (tx_ring->resched_needed &&
19115882Syy150190 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
19125882Syy150190 			tx_ring->resched_needed = B_FALSE;
19135882Syy150190 			mac_tx_update(Adapter->mh);
19145882Syy150190 			E1000G_STAT(tx_ring->stat_reschedule);
19155882Syy150190 		}
19165882Syy150190 	}
19175882Syy150190 
19185882Syy150190 	if (Adapter->intr_adaptive) {
19195882Syy150190 		itr = e1000g_get_itr(Adapter->rx_pkt_cnt, Adapter->tx_pkt_cnt,
19205882Syy150190 		    Adapter->intr_throttling_rate);
19215882Syy150190 		if (itr) {
19225882Syy150190 			E1000_WRITE_REG(hw, E1000_ITR, itr);
19235882Syy150190 			Adapter->intr_throttling_rate = itr;
19245882Syy150190 		}
19255882Syy150190 	}
19265882Syy150190 
19273526Sxy150489 	/*
19283526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
19293526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
19303526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
19313526Sxy150489 	 * are an indication that cable is not connected.
19323526Sxy150489 	 */
19334919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
19344919Sxy150489 	    (icr & E1000_ICR_LSC) ||
19354919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
19364061Sxy150489 		boolean_t link_changed;
19374061Sxy150489 		timeout_id_t tid = 0;
19383526Sxy150489 
19394919Sxy150489 		stop_watchdog_timer(Adapter);
19404919Sxy150489 
19415082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
19425082Syy150190 
19435082Syy150190 		/*
19445082Syy150190 		 * Because we got a link-status-change interrupt, force
19455082Syy150190 		 * e1000_check_for_link() to look at phy
19465082Syy150190 		 */
19475082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
19485082Syy150190 
19494061Sxy150489 		/* e1000g_link_check takes care of link status change */
19504061Sxy150489 		link_changed = e1000g_link_check(Adapter);
19515082Syy150190 
19525082Syy150190 		/* Get new phy state */
19535082Syy150190 		e1000g_get_phy_state(Adapter);
19545082Syy150190 
19554061Sxy150489 		/*
19564061Sxy150489 		 * If the link timer has not timed out, we'll not notify
19574919Sxy150489 		 * the upper layer with any link state until the link is up.
19584061Sxy150489 		 */
19594061Sxy150489 		if (link_changed && !Adapter->link_complete) {
19604061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
19615082Syy150190 				mutex_enter(&Adapter->link_lock);
19624061Sxy150489 				Adapter->link_complete = B_TRUE;
19634061Sxy150489 				tid = Adapter->link_tid;
19644061Sxy150489 				Adapter->link_tid = 0;
19655082Syy150190 				mutex_exit(&Adapter->link_lock);
19664061Sxy150489 			} else {
19674061Sxy150489 				link_changed = B_FALSE;
19684061Sxy150489 			}
19694061Sxy150489 		}
19705082Syy150190 		rw_exit(&Adapter->chip_lock);
19713526Sxy150489 
19724061Sxy150489 		if (link_changed) {
19734061Sxy150489 			if (tid != 0)
19744061Sxy150489 				(void) untimeout(tid);
19754061Sxy150489 
19764139Sxy150489 			/*
19774139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
19787133Scc210113 			 * down event. Stop receiver here and reset in watchdog.
19794139Sxy150489 			 */
19804139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
19817133Scc210113 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
19827133Scc210113 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
19837133Scc210113 				E1000_WRITE_REG(hw, E1000_RCTL,
19847133Scc210113 				    rctl & ~E1000_RCTL_EN);
19857133Scc210113 				e1000g_log(Adapter, CE_WARN,
19867133Scc210113 				    "ESB2 receiver disabled");
19877133Scc210113 				Adapter->esb2_workaround = B_TRUE;
19887133Scc210113 			}
19894139Sxy150489 
19904061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
19913526Sxy150489 		}
19923526Sxy150489 
19934919Sxy150489 		start_watchdog_timer(Adapter);
19943526Sxy150489 	}
19955882Syy150190 }
19965882Syy150190 
19975882Syy150190 static uint32_t
19985882Syy150190 e1000g_get_itr(uint32_t rx_packet, uint32_t tx_packet, uint32_t cur_itr)
19995882Syy150190 {
20005882Syy150190 	uint32_t new_itr;
20015882Syy150190 
20025882Syy150190 	/*
20035882Syy150190 	 * Determine a propper itr according to rx/tx packet count
20045882Syy150190 	 * per interrupt, the value of itr are based on document
20055882Syy150190 	 * and testing.
20065882Syy150190 	 */
20075882Syy150190 	if ((rx_packet < DEFAULT_INTR_PACKET_LOW) ||
20085882Syy150190 	    (tx_packet < DEFAULT_INTR_PACKET_LOW)) {
20095882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20105882Syy150190 		goto itr_done;
20113526Sxy150489 	}
20125882Syy150190 	if ((rx_packet > DEFAULT_INTR_PACKET_HIGH) ||
20135882Syy150190 	    (tx_packet > DEFAULT_INTR_PACKET_HIGH)) {
20145882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
20155882Syy150190 		goto itr_done;
20165882Syy150190 	}
20175882Syy150190 	if (cur_itr < DEFAULT_INTR_THROTTLING_HIGH) {
20185882Syy150190 		new_itr = cur_itr + (DEFAULT_INTR_THROTTLING_HIGH >> 2);
20195882Syy150190 		if (new_itr > DEFAULT_INTR_THROTTLING_HIGH)
20205882Syy150190 			new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20215882Syy150190 	} else
20225882Syy150190 		new_itr = DEFAULT_INTR_THROTTLING_HIGH;
20235882Syy150190 
20245882Syy150190 itr_done:
20255882Syy150190 	if (cur_itr == new_itr)
20265882Syy150190 		return (0);
20275882Syy150190 	else
20285882Syy150190 		return (new_itr);
20293526Sxy150489 }
20303526Sxy150489 
20313526Sxy150489 static void
20323526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
20333526Sxy150489 {
20343526Sxy150489 	struct e1000_hw *hw;
20353526Sxy150489 	int slot;
20363526Sxy150489 
20374919Sxy150489 	hw = &Adapter->shared;
20383526Sxy150489 
20395273Sgl147354 	if (!Adapter->unicst_init) {
20403526Sxy150489 		/* Initialize the multiple unicast addresses */
20413526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
20423526Sxy150489 
20434919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20444919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20453526Sxy150489 			Adapter->unicst_total--;
20463526Sxy150489 
20473526Sxy150489 		Adapter->unicst_avail = Adapter->unicst_total - 1;
20483526Sxy150489 
20493526Sxy150489 		/* Store the default mac address */
20504919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20514919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20524919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20534919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20544919Sxy150489 
20554919Sxy150489 		bcopy(hw->mac.addr, Adapter->unicst_addr[0].mac.addr,
20563526Sxy150489 		    ETHERADDRL);
20573526Sxy150489 		Adapter->unicst_addr[0].mac.set = 1;
20583526Sxy150489 
20593526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20603526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 0;
20615273Sgl147354 
20625273Sgl147354 		Adapter->unicst_init = B_TRUE;
20633526Sxy150489 	} else {
20643526Sxy150489 		/* Recover the default mac address */
20654919Sxy150489 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac.addr,
20663526Sxy150489 		    ETHERADDRL);
20673526Sxy150489 
20683526Sxy150489 		/* Store the default mac address */
20694919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20704919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20714919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20724919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20733526Sxy150489 
20743526Sxy150489 		/* Re-configure the RAR registers */
20753526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20763526Sxy150489 			e1000_rar_set(hw,
20773526Sxy150489 			    Adapter->unicst_addr[slot].mac.addr, slot);
20783526Sxy150489 	}
20795273Sgl147354 
20805273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
20815273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
20823526Sxy150489 }
20833526Sxy150489 
20843526Sxy150489 static int
20853526Sxy150489 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
20863526Sxy150489 {
20873526Sxy150489 	struct e1000g *Adapter;
20883526Sxy150489 
20893526Sxy150489 	Adapter = (struct e1000g *)arg;
20903526Sxy150489 
20913526Sxy150489 	/* Store the default MAC address */
20924919Sxy150489 	bcopy(mac_addr, Adapter->shared.mac.addr, ETHERADDRL);
20933526Sxy150489 
20943526Sxy150489 	/* Set MAC address in address slot 0, which is the default address */
20953526Sxy150489 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
20963526Sxy150489 }
20973526Sxy150489 
20983526Sxy150489 static int
20993526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
21003526Sxy150489     mac_addr_slot_t slot)
21013526Sxy150489 {
21023526Sxy150489 	struct e1000_hw *hw;
21033526Sxy150489 
21044919Sxy150489 	hw = &Adapter->shared;
21053526Sxy150489 
21063526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21073526Sxy150489 
21083526Sxy150489 	/*
21093526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
21103526Sxy150489 	 * that requires the receiver to be in reset when any of the
21113526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
21123526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
21133526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
21143526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
21153526Sxy150489 	 * and work around any necessary HW errata.
21163526Sxy150489 	 */
21174919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21184919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21193526Sxy150489 		e1000_pci_clear_mwi(hw);
21204919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
21214919Sxy150489 		msec_delay(5);
21223526Sxy150489 	}
21233526Sxy150489 
21243526Sxy150489 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
21253526Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
21263526Sxy150489 
21273526Sxy150489 	if (slot == 0) {
21284919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
21294919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
21304919Sxy150489 			e1000_rar_set(hw, (uint8_t *)mac_addr, LAST_RAR_ENTRY);
21313526Sxy150489 	}
21323526Sxy150489 
21333526Sxy150489 	/*
21343526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
21353526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
21363526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
21373526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
21383526Sxy150489 	 */
21394919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
21404919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
21414919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
21424919Sxy150489 		msec_delay(1);
21434919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
21443526Sxy150489 			e1000_pci_set_mwi(hw);
21454919Sxy150489 		e1000g_rx_setup(Adapter);
21463526Sxy150489 	}
21473526Sxy150489 
21483526Sxy150489 	rw_exit(&Adapter->chip_lock);
21493526Sxy150489 
21505273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
21515273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21525273Sgl147354 		return (EIO);
21535273Sgl147354 	}
21545273Sgl147354 
21553526Sxy150489 	return (0);
21563526Sxy150489 }
21573526Sxy150489 
21583526Sxy150489 /*
21593526Sxy150489  * e1000g_m_unicst_add() - will find an unused address slot, set the
21603526Sxy150489  * address value to the one specified, reserve that slot and enable
21613526Sxy150489  * the NIC to start filtering on the new MAC address.
21623526Sxy150489  * Returns 0 on success.
21633526Sxy150489  */
21643526Sxy150489 static int
21653526Sxy150489 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
21663526Sxy150489 {
21673526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
21683526Sxy150489 	mac_addr_slot_t slot;
21693526Sxy150489 	int err;
21703526Sxy150489 
21713526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
21723526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
21733526Sxy150489 		return (EINVAL);
21743526Sxy150489 
21753526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21763526Sxy150489 	if (Adapter->unicst_avail == 0) {
21773526Sxy150489 		/* no slots available */
21783526Sxy150489 		rw_exit(&Adapter->chip_lock);
21793526Sxy150489 		return (ENOSPC);
21803526Sxy150489 	}
21813526Sxy150489 
21823526Sxy150489 	/*
21833526Sxy150489 	 * Primary/default address is in slot 0. The next addresses
21843526Sxy150489 	 * are the multiple MAC addresses. So multiple MAC address 0
21853526Sxy150489 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
21863526Sxy150489 	 * MAC address resides in slot 1.
21873526Sxy150489 	 */
21883526Sxy150489 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
21893526Sxy150489 		if (Adapter->unicst_addr[slot].mac.set == 0) {
21903526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
21913526Sxy150489 			break;
21923526Sxy150489 		}
21933526Sxy150489 	}
21943526Sxy150489 
21953526Sxy150489 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
21963526Sxy150489 
21973526Sxy150489 	Adapter->unicst_avail--;
21983526Sxy150489 	rw_exit(&Adapter->chip_lock);
21993526Sxy150489 
22003526Sxy150489 	maddr->mma_slot = slot;
22013526Sxy150489 
22023526Sxy150489 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
22033526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_WRITER);
22043526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22053526Sxy150489 		Adapter->unicst_avail++;
22063526Sxy150489 		rw_exit(&Adapter->chip_lock);
22073526Sxy150489 	}
22083526Sxy150489 
22093526Sxy150489 	return (err);
22103526Sxy150489 }
22113526Sxy150489 
22123526Sxy150489 /*
22133526Sxy150489  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
22143526Sxy150489  * call to e1000g_m_unicst_add(). The slot number that was returned in
22153526Sxy150489  * e1000g_m_unicst_add() is passed in the call to remove the address.
22163526Sxy150489  * Returns 0 on success.
22173526Sxy150489  */
22183526Sxy150489 static int
22193526Sxy150489 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
22203526Sxy150489 {
22213526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22223526Sxy150489 	int err;
22233526Sxy150489 
22243526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22253526Sxy150489 		return (EINVAL);
22263526Sxy150489 
22273526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22283526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22293526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
22303526Sxy150489 		Adapter->unicst_avail++;
22313526Sxy150489 		rw_exit(&Adapter->chip_lock);
22323526Sxy150489 
22333526Sxy150489 		/* Copy the default address to the passed slot */
2234*7426SChenliang.Xu@Sun.COM 		if ((err = e1000g_unicst_set(Adapter,
2235*7426SChenliang.Xu@Sun.COM 		    Adapter->unicst_addr[0].mac.addr, slot)) != 0) {
22363526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_WRITER);
22373526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
22383526Sxy150489 			Adapter->unicst_avail--;
22393526Sxy150489 			rw_exit(&Adapter->chip_lock);
22403526Sxy150489 		}
22413526Sxy150489 		return (err);
22423526Sxy150489 	}
22433526Sxy150489 	rw_exit(&Adapter->chip_lock);
22443526Sxy150489 
22453526Sxy150489 	return (EINVAL);
22463526Sxy150489 }
22473526Sxy150489 
22483526Sxy150489 /*
22493526Sxy150489  * e1000g_m_unicst_modify() - modifies the value of an address that
22503526Sxy150489  * has been added by e1000g_m_unicst_add(). The new address, address
22513526Sxy150489  * length and the slot number that was returned in the call to add
22523526Sxy150489  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
22533526Sxy150489  * set to 0. Returns 0 on success.
22543526Sxy150489  */
22553526Sxy150489 static int
22563526Sxy150489 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
22573526Sxy150489 {
22583526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22593526Sxy150489 	mac_addr_slot_t slot;
22603526Sxy150489 
22613526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
22623526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
22633526Sxy150489 		return (EINVAL);
22643526Sxy150489 
22653526Sxy150489 	slot = maddr->mma_slot;
22663526Sxy150489 
22673526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22683526Sxy150489 		return (EINVAL);
22693526Sxy150489 
22703526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22713526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22723526Sxy150489 		rw_exit(&Adapter->chip_lock);
22733526Sxy150489 
22743526Sxy150489 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
22753526Sxy150489 	}
22763526Sxy150489 	rw_exit(&Adapter->chip_lock);
22773526Sxy150489 
22783526Sxy150489 	return (EINVAL);
22793526Sxy150489 }
22803526Sxy150489 
22813526Sxy150489 /*
22823526Sxy150489  * e1000g_m_unicst_get() - will get the MAC address and all other
22833526Sxy150489  * information related to the address slot passed in mac_multi_addr_t.
22843526Sxy150489  * mma_flags should be set to 0 in the call.
22853526Sxy150489  * On return, mma_flags can take the following values:
22863526Sxy150489  * 1) MMAC_SLOT_UNUSED
22873526Sxy150489  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
22883526Sxy150489  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
22893526Sxy150489  * 4) MMAC_SLOT_USED
22903526Sxy150489  */
22913526Sxy150489 static int
22923526Sxy150489 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
22933526Sxy150489 {
22943526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22953526Sxy150489 	mac_addr_slot_t slot;
22963526Sxy150489 
22973526Sxy150489 	slot = maddr->mma_slot;
22983526Sxy150489 
22993526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
23003526Sxy150489 		return (EINVAL);
23013526Sxy150489 
23023526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23033526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
23043526Sxy150489 		bcopy(Adapter->unicst_addr[slot].mac.addr,
23053526Sxy150489 		    maddr->mma_addr, ETHERADDRL);
23063526Sxy150489 		maddr->mma_flags = MMAC_SLOT_USED;
23073526Sxy150489 	} else {
23083526Sxy150489 		maddr->mma_flags = MMAC_SLOT_UNUSED;
23093526Sxy150489 	}
23103526Sxy150489 	rw_exit(&Adapter->chip_lock);
23113526Sxy150489 
23123526Sxy150489 	return (0);
23133526Sxy150489 }
23143526Sxy150489 
23153526Sxy150489 static int
23163526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
23173526Sxy150489 {
23184919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23193526Sxy150489 	int res = 0;
23203526Sxy150489 
23213526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23223526Sxy150489 
23233526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
23243526Sxy150489 		res = EINVAL;
23253526Sxy150489 		goto done;
23263526Sxy150489 	}
23273526Sxy150489 
23283526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
23293526Sxy150489 		res = ENOENT;
23303526Sxy150489 		goto done;
23313526Sxy150489 	}
23323526Sxy150489 
23333526Sxy150489 	bcopy(multiaddr,
23343526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
23353526Sxy150489 	Adapter->mcast_count++;
23363526Sxy150489 
23373526Sxy150489 	/*
23383526Sxy150489 	 * Update the MC table in the hardware
23393526Sxy150489 	 */
23404919Sxy150489 	e1000g_clear_interrupt(Adapter);
23414919Sxy150489 
23424919Sxy150489 	e1000g_setup_multicast(Adapter);
23434919Sxy150489 
23444919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23454919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23464919Sxy150489 		e1000g_rx_setup(Adapter);
23474919Sxy150489 
23484919Sxy150489 	e1000g_mask_interrupt(Adapter);
23493526Sxy150489 
23503526Sxy150489 done:
23513526Sxy150489 	rw_exit(&Adapter->chip_lock);
23525273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23535273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23545273Sgl147354 		res = EIO;
23555273Sgl147354 	}
23565273Sgl147354 
23573526Sxy150489 	return (res);
23583526Sxy150489 }
23593526Sxy150489 
23603526Sxy150489 static int
23613526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
23623526Sxy150489 {
23634919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23643526Sxy150489 	unsigned i;
23653526Sxy150489 
23663526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23673526Sxy150489 
23683526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
23693526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
23703526Sxy150489 		    ETHERADDRL) == 0) {
23713526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
23723526Sxy150489 				Adapter->mcast_table[i - 1] =
23733526Sxy150489 				    Adapter->mcast_table[i];
23743526Sxy150489 			}
23753526Sxy150489 			Adapter->mcast_count--;
23763526Sxy150489 			break;
23773526Sxy150489 		}
23783526Sxy150489 	}
23793526Sxy150489 
23803526Sxy150489 	/*
23813526Sxy150489 	 * Update the MC table in the hardware
23823526Sxy150489 	 */
23834919Sxy150489 	e1000g_clear_interrupt(Adapter);
23844919Sxy150489 
23854919Sxy150489 	e1000g_setup_multicast(Adapter);
23864919Sxy150489 
23874919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23884919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23894919Sxy150489 		e1000g_rx_setup(Adapter);
23904919Sxy150489 
23914919Sxy150489 	e1000g_mask_interrupt(Adapter);
23923526Sxy150489 
23933526Sxy150489 done:
23943526Sxy150489 	rw_exit(&Adapter->chip_lock);
23955273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23965273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23975273Sgl147354 		return (EIO);
23985273Sgl147354 	}
23995273Sgl147354 
24003526Sxy150489 	return (0);
24013526Sxy150489 }
24023526Sxy150489 
24034919Sxy150489 /*
24044919Sxy150489  * e1000g_setup_multicast - setup multicast data structures
24054919Sxy150489  *
24064919Sxy150489  * This routine initializes all of the multicast related structures.
24074919Sxy150489  */
24084919Sxy150489 void
24094919Sxy150489 e1000g_setup_multicast(struct e1000g *Adapter)
24104919Sxy150489 {
24114919Sxy150489 	uint8_t *mc_addr_list;
24124919Sxy150489 	uint32_t mc_addr_count;
24134919Sxy150489 	uint32_t rctl;
24144919Sxy150489 	struct e1000_hw *hw;
24154919Sxy150489 
24164919Sxy150489 	hw = &Adapter->shared;
24174919Sxy150489 
24184919Sxy150489 	/*
24194919Sxy150489 	 * The e1000g has the ability to do perfect filtering of 16
24204919Sxy150489 	 * addresses. The driver uses one of the e1000g's 16 receive
24214919Sxy150489 	 * address registers for its node/network/mac/individual address.
24224919Sxy150489 	 * So, we have room for up to 15 multicast addresses in the CAM,
24234919Sxy150489 	 * additional MC addresses are handled by the MTA (Multicast Table
24244919Sxy150489 	 * Array)
24254919Sxy150489 	 */
24264919Sxy150489 
24274919Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
24284919Sxy150489 
24294919Sxy150489 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
24304919Sxy150489 
24314919Sxy150489 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
24324919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
24334919Sxy150489 		    "Adapter requested more than %d MC Addresses.\n",
24344919Sxy150489 		    MAX_NUM_MULTICAST_ADDRESSES);
24354919Sxy150489 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
24364919Sxy150489 	} else {
24374919Sxy150489 		/*
24384919Sxy150489 		 * Set the number of MC addresses that we are being
24394919Sxy150489 		 * requested to use
24404919Sxy150489 		 */
24414919Sxy150489 		mc_addr_count = Adapter->mcast_count;
24424919Sxy150489 	}
24434919Sxy150489 	/*
24444919Sxy150489 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
24454919Sxy150489 	 * hang  while writing to the receive address registers if the receiver
24464919Sxy150489 	 * is not in reset before writing to the registers. Updating the RAR
24474919Sxy150489 	 * is done during the setting up of the multicast table, hence the
24484919Sxy150489 	 * receiver has to be put in reset before updating the multicast table
24494919Sxy150489 	 * and then taken out of reset at the end
24504919Sxy150489 	 */
24514919Sxy150489 	/*
24524919Sxy150489 	 * if WMI was enabled then dis able it before issueing the global
24534919Sxy150489 	 * reset to the hardware.
24544919Sxy150489 	 */
24554919Sxy150489 	/*
24564919Sxy150489 	 * Only required for WISEMAN_2_0
24574919Sxy150489 	 */
24584919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24594919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24604919Sxy150489 		e1000_pci_clear_mwi(hw);
24614919Sxy150489 		/*
24624919Sxy150489 		 * The e1000g must be in reset before changing any RA
24634919Sxy150489 		 * registers. Reset receive unit.  The chip will remain in
24644919Sxy150489 		 * the reset state until software explicitly restarts it.
24654919Sxy150489 		 */
24664919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
24674919Sxy150489 		/* Allow receiver time to go in to reset */
24684919Sxy150489 		msec_delay(5);
24694919Sxy150489 	}
24706735Scc210113 
24716735Scc210113 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
24724919Sxy150489 	    Adapter->unicst_total, hw->mac.rar_entry_count);
24734919Sxy150489 
24744919Sxy150489 	/*
24754919Sxy150489 	 * Only for Wiseman_2_0
24764919Sxy150489 	 * If MWI was enabled then re-enable it after issueing (as we
24774919Sxy150489 	 * disabled it up there) the receive reset command.
24784919Sxy150489 	 * Wainwright does not have a receive reset command and only thing
24794919Sxy150489 	 * close to it is global reset which will require tx setup also
24804919Sxy150489 	 */
24814919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24824919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24834919Sxy150489 		/*
24844919Sxy150489 		 * if WMI was enabled then reenable it after issueing the
24854919Sxy150489 		 * global or receive reset to the hardware.
24864919Sxy150489 		 */
24874919Sxy150489 
24884919Sxy150489 		/*
24894919Sxy150489 		 * Take receiver out of reset
24904919Sxy150489 		 * clear E1000_RCTL_RST bit (and all others)
24914919Sxy150489 		 */
24924919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
24934919Sxy150489 		msec_delay(5);
24944919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
24954919Sxy150489 			e1000_pci_set_mwi(hw);
24964919Sxy150489 	}
24974919Sxy150489 
24984919Sxy150489 	/*
24994919Sxy150489 	 * Restore original value
25004919Sxy150489 	 */
25014919Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
25024919Sxy150489 }
25034919Sxy150489 
25043526Sxy150489 int
25053526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
25063526Sxy150489 {
25073526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25083526Sxy150489 
25093526Sxy150489 	return ((add) ? multicst_add(Adapter, addr)
25104349Sxy150489 	    : multicst_remove(Adapter, addr));
25113526Sxy150489 }
25123526Sxy150489 
25133526Sxy150489 int
25143526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
25153526Sxy150489 {
25163526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25174919Sxy150489 	uint32_t rctl;
25183526Sxy150489 
25193526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
25203526Sxy150489 
25214919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
25223526Sxy150489 
25233526Sxy150489 	if (on)
25244919Sxy150489 		rctl |=
25253526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
25263526Sxy150489 	else
25274919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
25284919Sxy150489 
25294919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
25303526Sxy150489 
25313526Sxy150489 	Adapter->e1000g_promisc = on;
25323526Sxy150489 
25333526Sxy150489 	rw_exit(&Adapter->chip_lock);
25343526Sxy150489 
25355273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
25365273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
25375273Sgl147354 		return (EIO);
25385273Sgl147354 	}
25395273Sgl147354 
25403526Sxy150489 	return (0);
25413526Sxy150489 }
25423526Sxy150489 
25433526Sxy150489 static boolean_t
25443526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
25453526Sxy150489 {
25463526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25473526Sxy150489 
25483526Sxy150489 	switch (cap) {
25493526Sxy150489 	case MAC_CAPAB_HCKSUM: {
25503526Sxy150489 		uint32_t *txflags = cap_data;
25516986Smx205022 
25526986Smx205022 		if (Adapter->tx_hcksum_enable)
25536986Smx205022 			*txflags = HCKSUM_IPHDRCKSUM |
25546986Smx205022 			    HCKSUM_INET_PARTIAL;
25556986Smx205022 		else
25563526Sxy150489 			return (B_FALSE);
25573526Sxy150489 		break;
25583526Sxy150489 	}
25593526Sxy150489 	case MAC_CAPAB_POLL:
25603526Sxy150489 		/*
25613526Sxy150489 		 * There's nothing for us to fill in, simply returning
25623526Sxy150489 		 * B_TRUE stating that we support polling is sufficient.
25633526Sxy150489 		 */
25643526Sxy150489 		break;
25653526Sxy150489 
25663526Sxy150489 	case MAC_CAPAB_MULTIADDRESS: {
25673526Sxy150489 		multiaddress_capab_t *mmacp = cap_data;
25683526Sxy150489 
25693526Sxy150489 		/*
25703526Sxy150489 		 * The number of MAC addresses made available by
25713526Sxy150489 		 * this capability is one less than the total as
25723526Sxy150489 		 * the primary address in slot 0 is counted in
25733526Sxy150489 		 * the total.
25743526Sxy150489 		 */
25753526Sxy150489 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
25763526Sxy150489 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
25773526Sxy150489 		/* No multiple factory addresses, set mma_flag to 0 */
25783526Sxy150489 		mmacp->maddr_flag = 0;
25793526Sxy150489 		mmacp->maddr_handle = Adapter;
25803526Sxy150489 		mmacp->maddr_add = e1000g_m_unicst_add;
25813526Sxy150489 		mmacp->maddr_remove = e1000g_m_unicst_remove;
25823526Sxy150489 		mmacp->maddr_modify = e1000g_m_unicst_modify;
25833526Sxy150489 		mmacp->maddr_get = e1000g_m_unicst_get;
25843526Sxy150489 		mmacp->maddr_reserve = NULL;
25853526Sxy150489 		break;
25863526Sxy150489 	}
25876986Smx205022 
25886986Smx205022 	case MAC_CAPAB_LSO: {
25896986Smx205022 		mac_capab_lso_t *cap_lso = cap_data;
25906986Smx205022 
25916986Smx205022 		if (Adapter->lso_enable) {
25926986Smx205022 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
25936986Smx205022 			cap_lso->lso_basic_tcp_ipv4.lso_max =
25946986Smx205022 			    E1000_LSO_MAXLEN;
25956986Smx205022 		} else
25966986Smx205022 			return (B_FALSE);
25976986Smx205022 		break;
25986986Smx205022 	}
25996986Smx205022 
26003526Sxy150489 	default:
26013526Sxy150489 		return (B_FALSE);
26023526Sxy150489 	}
26033526Sxy150489 	return (B_TRUE);
26043526Sxy150489 }
26053526Sxy150489 
26066394Scc210113 static boolean_t
26076394Scc210113 e1000g_param_locked(mac_prop_id_t pr_num)
26086394Scc210113 {
26096394Scc210113 	/*
26106394Scc210113 	 * All en_* parameters are locked (read-only) while
26116394Scc210113 	 * the device is in any sort of loopback mode ...
26126394Scc210113 	 */
26136394Scc210113 	switch (pr_num) {
26146789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
26156789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
26166789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
26176789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
26186789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
26196789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
26206789Sam223141 		case MAC_PROP_AUTONEG:
26216789Sam223141 		case MAC_PROP_FLOWCTRL:
26226394Scc210113 			return (B_TRUE);
26236394Scc210113 	}
26246394Scc210113 	return (B_FALSE);
26256394Scc210113 }
26266394Scc210113 
26276394Scc210113 /*
26286394Scc210113  * callback function for set/get of properties
26296394Scc210113  */
26306394Scc210113 static int
26316394Scc210113 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
26326394Scc210113     uint_t pr_valsize, const void *pr_val)
26336394Scc210113 {
26346394Scc210113 	struct e1000g *Adapter = arg;
26356394Scc210113 	struct e1000_mac_info *mac = &Adapter->shared.mac;
26366394Scc210113 	struct e1000_phy_info *phy = &Adapter->shared.phy;
26376735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
26386394Scc210113 	int err = 0;
26396735Scc210113 	link_flowctrl_t flowctrl;
26406512Ssowmini 	uint32_t cur_mtu, new_mtu;
26416394Scc210113 	uint64_t tmp = 0;
26426394Scc210113 
26436394Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
26446394Scc210113 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
26456394Scc210113 	    e1000g_param_locked(pr_num)) {
26466394Scc210113 		/*
26476394Scc210113 		 * All en_* parameters are locked (read-only)
26486394Scc210113 		 * while the device is in any sort of loopback mode.
26496394Scc210113 		 */
26506394Scc210113 		rw_exit(&Adapter->chip_lock);
26516394Scc210113 		return (EBUSY);
26526394Scc210113 	}
26536394Scc210113 
26546394Scc210113 	switch (pr_num) {
26556789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
26566394Scc210113 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
26576394Scc210113 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
26586394Scc210113 			goto reset;
26596789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
26606394Scc210113 			Adapter->param_en_1000hdx = *(uint8_t *)pr_val;
26616394Scc210113 			Adapter->param_adv_1000hdx = *(uint8_t *)pr_val;
26626394Scc210113 			goto reset;
26636789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
26646394Scc210113 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
26656394Scc210113 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
26666394Scc210113 			goto reset;
26676789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
26686394Scc210113 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
26696394Scc210113 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
26706394Scc210113 			goto reset;
26716789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
26726394Scc210113 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
26736394Scc210113 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
26746394Scc210113 			goto reset;
26756789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
26766394Scc210113 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
26776394Scc210113 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
26786394Scc210113 			goto reset;
26796789Sam223141 		case MAC_PROP_AUTONEG:
26806394Scc210113 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
26816394Scc210113 			goto reset;
26826789Sam223141 		case MAC_PROP_FLOWCTRL:
26836735Scc210113 			fc->send_xon = B_TRUE;
26846735Scc210113 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
26856735Scc210113 
26866735Scc210113 			switch (flowctrl) {
26876394Scc210113 			default:
26886394Scc210113 				err = EINVAL;
26896394Scc210113 				break;
26906394Scc210113 			case LINK_FLOWCTRL_NONE:
26916735Scc210113 				fc->type = e1000_fc_none;
26926394Scc210113 				break;
26936394Scc210113 			case LINK_FLOWCTRL_RX:
26946735Scc210113 				fc->type = e1000_fc_rx_pause;
26956394Scc210113 				break;
26966394Scc210113 			case LINK_FLOWCTRL_TX:
26976735Scc210113 				fc->type = e1000_fc_tx_pause;
26986394Scc210113 				break;
26996394Scc210113 			case LINK_FLOWCTRL_BI:
27006735Scc210113 				fc->type = e1000_fc_full;
27016394Scc210113 				break;
27026394Scc210113 			}
27036394Scc210113 reset:
27046394Scc210113 			if (err == 0) {
27056394Scc210113 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
27066394Scc210113 					err = EINVAL;
27076394Scc210113 			}
27086394Scc210113 			break;
27096789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
27106789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
27116789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
27126789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
27136789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
27146789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
27156789Sam223141 		case MAC_PROP_STATUS:
27166789Sam223141 		case MAC_PROP_SPEED:
27176789Sam223141 		case MAC_PROP_DUPLEX:
27186394Scc210113 			err = ENOTSUP; /* read-only prop. Can't set this. */
27196394Scc210113 			break;
27206789Sam223141 		case MAC_PROP_MTU:
27216394Scc210113 			cur_mtu = Adapter->default_mtu;
27226394Scc210113 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
27236394Scc210113 			if (new_mtu == cur_mtu) {
27246394Scc210113 				err = 0;
27256394Scc210113 				break;
27266394Scc210113 			}
27276394Scc210113 
27286394Scc210113 			tmp = new_mtu + sizeof (struct ether_vlan_header) +
27296394Scc210113 			    ETHERFCSL;
27306394Scc210113 			if ((tmp < DEFAULT_FRAME_SIZE) ||
27316394Scc210113 			    (tmp > MAXIMUM_FRAME_SIZE)) {
27326394Scc210113 				err = EINVAL;
27336394Scc210113 				break;
27346394Scc210113 			}
27356394Scc210113 
27366394Scc210113 			/* ich8 doed not support jumbo frames */
27376394Scc210113 			if ((mac->type == e1000_ich8lan) &&
27386394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27396394Scc210113 				err = EINVAL;
27406394Scc210113 				break;
27416394Scc210113 			}
27426394Scc210113 			/* ich9 does not do jumbo frames on one phy type */
27436394Scc210113 			if ((mac->type == e1000_ich9lan) &&
27446394Scc210113 			    (phy->type == e1000_phy_ife) &&
27456394Scc210113 			    (tmp > DEFAULT_FRAME_SIZE)) {
27466394Scc210113 				err = EINVAL;
27476394Scc210113 				break;
27486394Scc210113 			}
27496394Scc210113 			if (Adapter->chip_state != E1000G_STOP) {
27506394Scc210113 				err = EBUSY;
27516394Scc210113 				break;
27526394Scc210113 			}
27536394Scc210113 
27546394Scc210113 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
27556394Scc210113 			if (err == 0) {
2756*7426SChenliang.Xu@Sun.COM 				Adapter->max_frame_size = (uint32_t)tmp;
27576394Scc210113 				Adapter->default_mtu = new_mtu;
27586394Scc210113 				e1000g_set_bufsize(Adapter);
27596394Scc210113 			}
27606394Scc210113 			break;
27616789Sam223141 		case MAC_PROP_PRIVATE:
27626394Scc210113 			err = e1000g_set_priv_prop(Adapter, pr_name,
27636394Scc210113 			    pr_valsize, pr_val);
27646394Scc210113 			break;
27656394Scc210113 		default:
27666394Scc210113 			err = ENOTSUP;
27676394Scc210113 			break;
27686394Scc210113 	}
27696394Scc210113 	rw_exit(&Adapter->chip_lock);
27706394Scc210113 	return (err);
27716394Scc210113 }
27726394Scc210113 
27736394Scc210113 static int
27746394Scc210113 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
27756512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
27766394Scc210113 {
27776394Scc210113 	struct e1000g *Adapter = arg;
27786735Scc210113 	struct e1000_fc_info *fc = &Adapter->shared.fc;
27796512Ssowmini 	int err = 0;
27806735Scc210113 	link_flowctrl_t flowctrl;
27816394Scc210113 	uint64_t tmp = 0;
27826394Scc210113 
27836512Ssowmini 	if (pr_valsize == 0)
27846512Ssowmini 		return (EINVAL);
27856512Ssowmini 
27866394Scc210113 	bzero(pr_val, pr_valsize);
27876789Sam223141 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
27886512Ssowmini 		return (e1000g_get_def_val(Adapter, pr_num,
27896512Ssowmini 		    pr_valsize, pr_val));
27906512Ssowmini 	}
27916512Ssowmini 
27926394Scc210113 	switch (pr_num) {
27936789Sam223141 		case MAC_PROP_DUPLEX:
27946512Ssowmini 			if (pr_valsize >= sizeof (link_duplex_t)) {
27956512Ssowmini 				bcopy(&Adapter->link_duplex, pr_val,
27966512Ssowmini 				    sizeof (link_duplex_t));
27976512Ssowmini 			} else
27986512Ssowmini 				err = EINVAL;
27996394Scc210113 			break;
28006789Sam223141 		case MAC_PROP_SPEED:
28016394Scc210113 			if (pr_valsize >= sizeof (uint64_t)) {
28026394Scc210113 				tmp = Adapter->link_speed * 1000000ull;
28036394Scc210113 				bcopy(&tmp, pr_val, sizeof (tmp));
28046512Ssowmini 			} else
28056512Ssowmini 				err = EINVAL;
28066394Scc210113 			break;
28076789Sam223141 		case MAC_PROP_AUTONEG:
28086512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
28096394Scc210113 			break;
28106789Sam223141 		case MAC_PROP_FLOWCTRL:
28116394Scc210113 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
28126735Scc210113 				switch (fc->type) {
28136394Scc210113 					case e1000_fc_none:
28146735Scc210113 						flowctrl = LINK_FLOWCTRL_NONE;
28156394Scc210113 						break;
28166394Scc210113 					case e1000_fc_rx_pause:
28176735Scc210113 						flowctrl = LINK_FLOWCTRL_RX;
28186394Scc210113 						break;
28196394Scc210113 					case e1000_fc_tx_pause:
28206735Scc210113 						flowctrl = LINK_FLOWCTRL_TX;
28216394Scc210113 						break;
28226394Scc210113 					case e1000_fc_full:
28236735Scc210113 						flowctrl = LINK_FLOWCTRL_BI;
28246394Scc210113 						break;
28256394Scc210113 				}
28266735Scc210113 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
28276512Ssowmini 			} else
28286512Ssowmini 				err = EINVAL;
28296394Scc210113 			break;
28306789Sam223141 		case MAC_PROP_ADV_1000FDX_CAP:
28316512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
28326394Scc210113 			break;
28336789Sam223141 		case MAC_PROP_EN_1000FDX_CAP:
28346512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
28356394Scc210113 			break;
28366789Sam223141 		case MAC_PROP_ADV_1000HDX_CAP:
28376512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
28386394Scc210113 			break;
28396789Sam223141 		case MAC_PROP_EN_1000HDX_CAP:
28406512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
28416394Scc210113 			break;
28426789Sam223141 		case MAC_PROP_ADV_100FDX_CAP:
28436512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
28446394Scc210113 			break;
28456789Sam223141 		case MAC_PROP_EN_100FDX_CAP:
28466512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
28476394Scc210113 			break;
28486789Sam223141 		case MAC_PROP_ADV_100HDX_CAP:
28496512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
28506394Scc210113 			break;
28516789Sam223141 		case MAC_PROP_EN_100HDX_CAP:
28526512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
28536394Scc210113 			break;
28546789Sam223141 		case MAC_PROP_ADV_10FDX_CAP:
28556512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
28566394Scc210113 			break;
28576789Sam223141 		case MAC_PROP_EN_10FDX_CAP:
28586512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
28596394Scc210113 			break;
28606789Sam223141 		case MAC_PROP_ADV_10HDX_CAP:
28616512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
28626394Scc210113 			break;
28636789Sam223141 		case MAC_PROP_EN_10HDX_CAP:
28646512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
28656512Ssowmini 			break;
28666789Sam223141 		case MAC_PROP_ADV_100T4_CAP:
28676789Sam223141 		case MAC_PROP_EN_100T4_CAP:
28686512Ssowmini 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
28696394Scc210113 			break;
28706789Sam223141 		case MAC_PROP_PRIVATE:
28716394Scc210113 			err = e1000g_get_priv_prop(Adapter, pr_name,
28726512Ssowmini 			    pr_flags, pr_valsize, pr_val);
28736394Scc210113 			break;
28746394Scc210113 		default:
28756394Scc210113 			err = ENOTSUP;
28766394Scc210113 			break;
28776394Scc210113 	}
28786394Scc210113 	return (err);
28796394Scc210113 }
28806394Scc210113 
2881*7426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
28826394Scc210113 static int
28836394Scc210113 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
28846394Scc210113     uint_t pr_valsize, const void *pr_val)
28856394Scc210113 {
28866394Scc210113 	int err = 0;
28876394Scc210113 	long result;
28886394Scc210113 	struct e1000_hw *hw = &Adapter->shared;
28896394Scc210113 
28906394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
28916394Scc210113 		if (pr_val == NULL) {
28926394Scc210113 			err = EINVAL;
28936394Scc210113 			return (err);
28946394Scc210113 		}
28956394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
28966394Scc210113 		if (result < MIN_TX_BCOPY_THRESHOLD ||
28976394Scc210113 		    result > MAX_TX_BCOPY_THRESHOLD)
28986394Scc210113 			err = EINVAL;
28996394Scc210113 		else {
29006394Scc210113 			Adapter->tx_bcopy_thresh = (uint32_t)result;
29016394Scc210113 		}
29026394Scc210113 		return (err);
29036394Scc210113 	}
29046394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
29056394Scc210113 		if (pr_val == NULL) {
29066394Scc210113 			err = EINVAL;
29076394Scc210113 			return (err);
29086394Scc210113 		}
29096394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29106394Scc210113 		if (result < 0 || result > 1)
29116394Scc210113 			err = EINVAL;
29126394Scc210113 		else {
29136394Scc210113 			Adapter->tx_intr_enable = (result == 1) ?
29146394Scc210113 			    B_TRUE: B_FALSE;
29156394Scc210113 			if (Adapter->tx_intr_enable)
29166394Scc210113 				e1000g_mask_tx_interrupt(Adapter);
29176394Scc210113 			else
29186394Scc210113 				e1000g_clear_tx_interrupt(Adapter);
29196394Scc210113 			if (e1000g_check_acc_handle(
29206394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29216394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29226394Scc210113 				    DDI_SERVICE_DEGRADED);
29236394Scc210113 		}
29246394Scc210113 		return (err);
29256394Scc210113 	}
29266394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
29276394Scc210113 		if (pr_val == NULL) {
29286394Scc210113 			err = EINVAL;
29296394Scc210113 			return (err);
29306394Scc210113 		}
29316394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29326394Scc210113 		if (result < MIN_TX_INTR_DELAY ||
29336394Scc210113 		    result > MAX_TX_INTR_DELAY)
29346394Scc210113 			err = EINVAL;
29356394Scc210113 		else {
29366394Scc210113 			Adapter->tx_intr_delay = (uint32_t)result;
29376394Scc210113 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
29386394Scc210113 			if (e1000g_check_acc_handle(
29396394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29406394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29416394Scc210113 				    DDI_SERVICE_DEGRADED);
29426394Scc210113 		}
29436394Scc210113 		return (err);
29446394Scc210113 	}
29456394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 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_INTR_ABS_DELAY ||
29526394Scc210113 		    result > MAX_TX_INTR_ABS_DELAY)
29536394Scc210113 			err = EINVAL;
29546394Scc210113 		else {
29556394Scc210113 			Adapter->tx_intr_abs_delay = (uint32_t)result;
29566394Scc210113 			E1000_WRITE_REG(hw, E1000_TADV,
29576394Scc210113 			    Adapter->tx_intr_abs_delay);
29586394Scc210113 			if (e1000g_check_acc_handle(
29596394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
29606394Scc210113 				ddi_fm_service_impact(Adapter->dip,
29616394Scc210113 				    DDI_SERVICE_DEGRADED);
29626394Scc210113 		}
29636394Scc210113 		return (err);
29646394Scc210113 	}
29656394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
29666394Scc210113 		if (pr_val == NULL) {
29676394Scc210113 			err = EINVAL;
29686394Scc210113 			return (err);
29696394Scc210113 		}
29706394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29716394Scc210113 		if (result < MIN_RX_BCOPY_THRESHOLD ||
29726394Scc210113 		    result > MAX_RX_BCOPY_THRESHOLD)
29736394Scc210113 			err = EINVAL;
29746394Scc210113 		else
29756394Scc210113 			Adapter->rx_bcopy_thresh = (uint32_t)result;
29766394Scc210113 		return (err);
29776394Scc210113 	}
29786394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
29796394Scc210113 		if (pr_val == NULL) {
29806394Scc210113 			err = EINVAL;
29816394Scc210113 			return (err);
29826394Scc210113 		}
29836394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29846394Scc210113 		if (result < MIN_RX_LIMIT_ON_INTR ||
29856394Scc210113 		    result > MAX_RX_LIMIT_ON_INTR)
29866394Scc210113 			err = EINVAL;
29876394Scc210113 		else
29886394Scc210113 			Adapter->rx_limit_onintr = (uint32_t)result;
29896394Scc210113 		return (err);
29906394Scc210113 	}
29916394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
29926394Scc210113 		if (pr_val == NULL) {
29936394Scc210113 			err = EINVAL;
29946394Scc210113 			return (err);
29956394Scc210113 		}
29966394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
29976394Scc210113 		if (result < MIN_RX_INTR_DELAY ||
29986394Scc210113 		    result > MAX_RX_INTR_DELAY)
29996394Scc210113 			err = EINVAL;
30006394Scc210113 		else {
30016394Scc210113 			Adapter->rx_intr_delay = (uint32_t)result;
30026394Scc210113 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
30036394Scc210113 			if (e1000g_check_acc_handle(
30046394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30056394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30066394Scc210113 				    DDI_SERVICE_DEGRADED);
30076394Scc210113 		}
30086394Scc210113 		return (err);
30096394Scc210113 	}
30106394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
30116394Scc210113 		if (pr_val == NULL) {
30126394Scc210113 			err = EINVAL;
30136394Scc210113 			return (err);
30146394Scc210113 		}
30156394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30166394Scc210113 		if (result < MIN_RX_INTR_ABS_DELAY ||
30176394Scc210113 		    result > MAX_RX_INTR_ABS_DELAY)
30186394Scc210113 			err = EINVAL;
30196394Scc210113 		else {
30206394Scc210113 			Adapter->rx_intr_abs_delay = (uint32_t)result;
30216394Scc210113 			E1000_WRITE_REG(hw, E1000_RADV,
30226394Scc210113 			    Adapter->rx_intr_abs_delay);
30236394Scc210113 			if (e1000g_check_acc_handle(
30246394Scc210113 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
30256394Scc210113 				ddi_fm_service_impact(Adapter->dip,
30266394Scc210113 				    DDI_SERVICE_DEGRADED);
30276394Scc210113 		}
30286394Scc210113 		return (err);
30296394Scc210113 	}
30306394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
30316394Scc210113 		if (pr_val == NULL) {
30326394Scc210113 			err = EINVAL;
30336394Scc210113 			return (err);
30346394Scc210113 		}
30356394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30366394Scc210113 		if (result < MIN_INTR_THROTTLING ||
30376394Scc210113 		    result > MAX_INTR_THROTTLING)
30386394Scc210113 			err = EINVAL;
30396394Scc210113 		else {
30406394Scc210113 			if (hw->mac.type >= e1000_82540) {
30416394Scc210113 				Adapter->intr_throttling_rate =
30426394Scc210113 				    (uint32_t)result;
30436394Scc210113 				E1000_WRITE_REG(hw, E1000_ITR,
30446394Scc210113 				    Adapter->intr_throttling_rate);
30456394Scc210113 				if (e1000g_check_acc_handle(
30466394Scc210113 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
30476394Scc210113 					ddi_fm_service_impact(Adapter->dip,
30486394Scc210113 					    DDI_SERVICE_DEGRADED);
30496394Scc210113 			} else
30506394Scc210113 				err = EINVAL;
30516394Scc210113 		}
30526394Scc210113 		return (err);
30536394Scc210113 	}
30546394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
30556394Scc210113 		if (pr_val == NULL) {
30566394Scc210113 			err = EINVAL;
30576394Scc210113 			return (err);
30586394Scc210113 		}
30596394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30606394Scc210113 		if (result < 0 || result > 1)
30616394Scc210113 			err = EINVAL;
30626394Scc210113 		else {
30636394Scc210113 			if (hw->mac.type >= e1000_82540) {
30646394Scc210113 				Adapter->intr_adaptive = (result == 1) ?
30656394Scc210113 				    B_TRUE : B_FALSE;
30666394Scc210113 			} else {
30676394Scc210113 				err = EINVAL;
30686394Scc210113 			}
30696394Scc210113 		}
30706394Scc210113 		return (err);
30716394Scc210113 	}
30726394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
30736394Scc210113 		if (pr_val == NULL) {
30746394Scc210113 			err = EINVAL;
30756394Scc210113 			return (err);
30766394Scc210113 		}
30776394Scc210113 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
30786394Scc210113 		if (result < MIN_TX_RECYCLE_THRESHOLD ||
30796394Scc210113 		    result > MAX_TX_RECYCLE_THRESHOLD)
30806394Scc210113 			err = EINVAL;
30816394Scc210113 		else
30826394Scc210113 			Adapter->tx_recycle_thresh = (uint32_t)result;
30836394Scc210113 		return (err);
30846394Scc210113 	}
30856394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 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_TX_RECYCLE_NUM ||
30926394Scc210113 		    result > MAX_TX_RECYCLE_NUM)
30936394Scc210113 			err = EINVAL;
30946394Scc210113 		else
30956394Scc210113 			Adapter->tx_recycle_num = (uint32_t)result;
30966394Scc210113 		return (err);
30976394Scc210113 	}
30986394Scc210113 	return (ENOTSUP);
30996394Scc210113 }
31006394Scc210113 
31016394Scc210113 static int
31026394Scc210113 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
31036512Ssowmini     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
31046394Scc210113 {
31056394Scc210113 	int err = ENOTSUP;
31066789Sam223141 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
31076512Ssowmini 	int value;
31086512Ssowmini 
31096512Ssowmini 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
31106512Ssowmini 		if (is_default)
31116512Ssowmini 			goto done;
31126512Ssowmini 		value = Adapter->param_adv_pause;
31136512Ssowmini 		err = 0;
31146512Ssowmini 		goto done;
31156512Ssowmini 	}
31166512Ssowmini 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
31176512Ssowmini 		if (is_default)
31186512Ssowmini 			goto done;
31196512Ssowmini 		value = Adapter->param_adv_asym_pause;
31206512Ssowmini 		err = 0;
31216512Ssowmini 		goto done;
31226512Ssowmini 	}
31236394Scc210113 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
31246512Ssowmini 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
31256512Ssowmini 		    Adapter->tx_bcopy_thresh);
31266394Scc210113 		err = 0;
31276394Scc210113 		goto done;
31286394Scc210113 	}
31296394Scc210113 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
31306512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
31316512Ssowmini 		    Adapter->tx_intr_enable);
31326394Scc210113 		err = 0;
31336394Scc210113 		goto done;
31346394Scc210113 	}
31356394Scc210113 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
31366512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
31376512Ssowmini 		    Adapter->tx_intr_delay);
31386394Scc210113 		err = 0;
31396394Scc210113 		goto done;
31406394Scc210113 	}
31416394Scc210113 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
31426512Ssowmini 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
31436512Ssowmini 		    Adapter->tx_intr_abs_delay);
31446394Scc210113 		err = 0;
31456394Scc210113 		goto done;
31466394Scc210113 	}
31476394Scc210113 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
31486512Ssowmini 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
31496512Ssowmini 		    Adapter->rx_bcopy_thresh);
31506394Scc210113 		err = 0;
31516394Scc210113 		goto done;
31526394Scc210113 	}
31536394Scc210113 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
31546512Ssowmini 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
31556512Ssowmini 		    Adapter->rx_limit_onintr);
31566394Scc210113 		err = 0;
31576394Scc210113 		goto done;
31586394Scc210113 	}
31596394Scc210113 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
31606512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
31616512Ssowmini 		    Adapter->rx_intr_delay);
31626394Scc210113 		err = 0;
31636394Scc210113 		goto done;
31646394Scc210113 	}
31656394Scc210113 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
31666512Ssowmini 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
31676512Ssowmini 		    Adapter->rx_intr_abs_delay);
31686394Scc210113 		err = 0;
31696394Scc210113 		goto done;
31706394Scc210113 	}
31716394Scc210113 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
31726512Ssowmini 		value = (is_default ? DEFAULT_INTR_THROTTLING :
31736512Ssowmini 		    Adapter->intr_throttling_rate);
31746394Scc210113 		err = 0;
31756394Scc210113 		goto done;
31766394Scc210113 	}
31776394Scc210113 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
31786512Ssowmini 		value = (is_default ? 1 : Adapter->intr_adaptive);
31796394Scc210113 		err = 0;
31806394Scc210113 		goto done;
31816394Scc210113 	}
31826394Scc210113 	if (strcmp(pr_name, "_tx_recycle_thresh") == 0) {
31836512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_THRESHOLD :
31846512Ssowmini 		    Adapter->tx_recycle_thresh);
31856394Scc210113 		err = 0;
31866394Scc210113 		goto done;
31876394Scc210113 	}
31886394Scc210113 	if (strcmp(pr_name, "_tx_recycle_num") == 0) {
31896512Ssowmini 		value = (is_default ? DEFAULT_TX_RECYCLE_NUM :
31906512Ssowmini 		    Adapter->tx_recycle_num);
31916394Scc210113 		err = 0;
31926394Scc210113 		goto done;
31936394Scc210113 	}
31946394Scc210113 done:
31956394Scc210113 	if (err == 0) {
31966512Ssowmini 		(void) snprintf(pr_val, pr_valsize, "%d", value);
31976394Scc210113 	}
31986394Scc210113 	return (err);
31996394Scc210113 }
32006394Scc210113 
32013526Sxy150489 /*
32024919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
32034919Sxy150489  * This routine gets user-configured values out of the configuration
32044919Sxy150489  * file e1000g.conf.
32054919Sxy150489  *
32064919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
32074919Sxy150489  * default.
32084919Sxy150489  * If user does not configure a value, use the default.
32094919Sxy150489  * If user configures below the minimum, use the minumum.
32104919Sxy150489  * If user configures above the maximum, use the maxumum.
32113526Sxy150489  */
32123526Sxy150489 static void
32134919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
32143526Sxy150489 {
32154919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
32164919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
32174919Sxy150489 
32183526Sxy150489 	/*
32193526Sxy150489 	 * get each configurable property from e1000g.conf
32203526Sxy150489 	 */
32213526Sxy150489 
32223526Sxy150489 	/*
32233526Sxy150489 	 * NumTxDescriptors
32243526Sxy150489 	 */
32254919Sxy150489 	Adapter->tx_desc_num =
32264919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
32274919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
32284919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
32293526Sxy150489 
32303526Sxy150489 	/*
32313526Sxy150489 	 * NumRxDescriptors
32323526Sxy150489 	 */
32334919Sxy150489 	Adapter->rx_desc_num =
32344919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
32354919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
32364919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
32373526Sxy150489 
32383526Sxy150489 	/*
32393526Sxy150489 	 * NumRxFreeList
32403526Sxy150489 	 */
32414919Sxy150489 	Adapter->rx_freelist_num =
32424919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
32434919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
32444919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
32453526Sxy150489 
32463526Sxy150489 	/*
32473526Sxy150489 	 * NumTxPacketList
32483526Sxy150489 	 */
32494919Sxy150489 	Adapter->tx_freelist_num =
32504919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
32514919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
32524919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
32533526Sxy150489 
32543526Sxy150489 	/*
32553526Sxy150489 	 * FlowControl
32563526Sxy150489 	 */
32576735Scc210113 	hw->fc.send_xon = B_TRUE;
32586735Scc210113 	hw->fc.type =
32594919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
32604919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
32613526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
32626735Scc210113 	if (hw->fc.type == 4)
32636735Scc210113 		hw->fc.type = e1000_fc_default;
32643526Sxy150489 
32653526Sxy150489 	/*
32664919Sxy150489 	 * Max Num Receive Packets on Interrupt
32673526Sxy150489 	 */
32684919Sxy150489 	Adapter->rx_limit_onintr =
32694919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
32704919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
32714919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
32723526Sxy150489 
32733526Sxy150489 	/*
32743526Sxy150489 	 * PHY master slave setting
32753526Sxy150489 	 */
32764919Sxy150489 	hw->phy.ms_type =
32774919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
32783526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
32793526Sxy150489 	    e1000_ms_hw_default);
32803526Sxy150489 
32813526Sxy150489 	/*
32823526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
32833526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
32843526Sxy150489 	 */
32854919Sxy150489 	tbi_compatibility =
32864919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
32874919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
32884919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
32893526Sxy150489 
32903526Sxy150489 	/*
32913526Sxy150489 	 * MSI Enable
32923526Sxy150489 	 */
32936986Smx205022 	Adapter->msi_enable =
32944919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
32954919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
32963526Sxy150489 
32973526Sxy150489 	/*
32983526Sxy150489 	 * Interrupt Throttling Rate
32993526Sxy150489 	 */
33003526Sxy150489 	Adapter->intr_throttling_rate =
33014919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
33024919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
33034919Sxy150489 	    DEFAULT_INTR_THROTTLING);
33043526Sxy150489 
33053526Sxy150489 	/*
33063526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
33073526Sxy150489 	 * It is enabled by default
33083526Sxy150489 	 */
33093526Sxy150489 	Adapter->intr_adaptive =
33104919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
33113526Sxy150489 	    B_TRUE : B_FALSE;
33125882Syy150190 
33135882Syy150190 	/*
33145882Syy150190 	 * Tx recycle threshold
33155882Syy150190 	 */
33165882Syy150190 	Adapter->tx_recycle_thresh =
33175882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_thresh",
33185882Syy150190 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
33195882Syy150190 	    DEFAULT_TX_RECYCLE_THRESHOLD);
33205882Syy150190 
33215882Syy150190 	/*
33225882Syy150190 	 * Tx recycle descriptor number
33235882Syy150190 	 */
33245882Syy150190 	Adapter->tx_recycle_num =
33255882Syy150190 	    e1000g_get_prop(Adapter, "tx_recycle_num",
33265882Syy150190 	    MIN_TX_RECYCLE_NUM, MAX_TX_RECYCLE_NUM,
33275882Syy150190 	    DEFAULT_TX_RECYCLE_NUM);
33286011Ssv141092 
33296011Ssv141092 	/*
33306011Ssv141092 	 * Hardware checksum enable/disable parameter
33316011Ssv141092 	 */
33326986Smx205022 	Adapter->tx_hcksum_enable =
33336986Smx205022 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
33346011Ssv141092 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
33356986Smx205022 	/*
33366986Smx205022 	 * Checksum on/off selection via global parameters.
33376986Smx205022 	 *
33386986Smx205022 	 * If the chip is flagged as not capable of (correctly)
33396986Smx205022 	 * handling checksumming, we don't enable it on either
33406986Smx205022 	 * Rx or Tx side.  Otherwise, we take this chip's settings
33416986Smx205022 	 * from the patchable global defaults.
33426986Smx205022 	 *
33436986Smx205022 	 * We advertise our capabilities only if TX offload is
33446986Smx205022 	 * enabled.  On receive, the stack will accept checksummed
33456986Smx205022 	 * packets anyway, even if we haven't said we can deliver
33466986Smx205022 	 * them.
33476986Smx205022 	 */
33486986Smx205022 	switch (hw->mac.type) {
33496986Smx205022 		case e1000_82540:
33506986Smx205022 		case e1000_82544:
33516986Smx205022 		case e1000_82545:
33526986Smx205022 		case e1000_82545_rev_3:
33536986Smx205022 		case e1000_82546:
33546986Smx205022 		case e1000_82546_rev_3:
33556986Smx205022 		case e1000_82571:
33566986Smx205022 		case e1000_82572:
33576986Smx205022 		case e1000_82573:
33586986Smx205022 		case e1000_80003es2lan:
33596986Smx205022 			break;
33606986Smx205022 		/*
33616986Smx205022 		 * For the following Intel PRO/1000 chipsets, we have not
33626986Smx205022 		 * tested the hardware checksum offload capability, so we
33636986Smx205022 		 * disable the capability for them.
33646986Smx205022 		 *	e1000_82542,
33656986Smx205022 		 *	e1000_82543,
33666986Smx205022 		 *	e1000_82541,
33676986Smx205022 		 *	e1000_82541_rev_2,
33686986Smx205022 		 *	e1000_82547,
33696986Smx205022 		 *	e1000_82547_rev_2,
33706986Smx205022 		 */
33716986Smx205022 		default:
33726986Smx205022 			Adapter->tx_hcksum_enable = B_FALSE;
33736986Smx205022 	}
33746986Smx205022 
33756986Smx205022 	/*
33766986Smx205022 	 * Large Send Offloading(LSO) Enable/Disable
33776986Smx205022 	 * If the tx hardware checksum is not enabled, LSO should be
33786986Smx205022 	 * disabled.
33796986Smx205022 	 */
33806986Smx205022 	Adapter->lso_enable =
33816986Smx205022 	    e1000g_get_prop(Adapter, "lso_enable",
33826986Smx205022 	    0, 1, DEFAULT_LSO_ENABLE);
33836986Smx205022 
33846986Smx205022 	switch (hw->mac.type) {
33856986Smx205022 		case e1000_82546:
33866986Smx205022 		case e1000_82546_rev_3:
33876986Smx205022 			if (Adapter->lso_enable)
33886986Smx205022 				Adapter->lso_premature_issue = B_TRUE;
3389*7426SChenliang.Xu@Sun.COM 			/* FALLTHRU */
33906986Smx205022 		case e1000_82571:
33916986Smx205022 		case e1000_82572:
33926986Smx205022 		case e1000_82573:
33936986Smx205022 			break;
33946986Smx205022 		default:
33956986Smx205022 			Adapter->lso_enable = B_FALSE;
33966986Smx205022 	}
33976986Smx205022 
33986986Smx205022 	if (!Adapter->tx_hcksum_enable) {
33996986Smx205022 		Adapter->lso_premature_issue = B_FALSE;
34006986Smx205022 		Adapter->lso_enable = B_FALSE;
34016986Smx205022 	}
34023526Sxy150489 }
34033526Sxy150489 
34043526Sxy150489 /*
34054919Sxy150489  * e1000g_get_prop - routine to read properties
34064919Sxy150489  *
34074919Sxy150489  * Get a user-configure property value out of the configuration
34084919Sxy150489  * file e1000g.conf.
34094919Sxy150489  *
34104919Sxy150489  * Caller provides name of the property, a default value, a minimum
34114919Sxy150489  * value, and a maximum value.
34124919Sxy150489  *
34134919Sxy150489  * Return configured value of the property, with default, minimum and
34144919Sxy150489  * maximum properly applied.
34153526Sxy150489  */
34163526Sxy150489 static int
34174919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
34183526Sxy150489     char *propname,		/* name of the property */
34193526Sxy150489     int minval,			/* minimum acceptable value */
34203526Sxy150489     int maxval,			/* maximim acceptable value */
34213526Sxy150489     int defval)			/* default value */
34223526Sxy150489 {
34233526Sxy150489 	int propval;		/* value returned for requested property */
34243526Sxy150489 	int *props;		/* point to array of properties returned */
34253526Sxy150489 	uint_t nprops;		/* number of property value returned */
34263526Sxy150489 
34273526Sxy150489 	/*
34283526Sxy150489 	 * get the array of properties from the config file
34293526Sxy150489 	 */
34303526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
34313526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
34323526Sxy150489 		/* got some properties, test if we got enough */
34334919Sxy150489 		if (Adapter->instance < nprops) {
34344919Sxy150489 			propval = props[Adapter->instance];
34353526Sxy150489 		} else {
34363526Sxy150489 			/* not enough properties configured */
34373526Sxy150489 			propval = defval;
34384919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34393526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
34403526Sxy150489 			    " - set to %d\n",
34413526Sxy150489 			    propname, propval);
34423526Sxy150489 		}
34433526Sxy150489 
34443526Sxy150489 		/* free memory allocated for properties */
34453526Sxy150489 		ddi_prop_free(props);
34463526Sxy150489 
34473526Sxy150489 	} else {
34483526Sxy150489 		propval = defval;
34493526Sxy150489 	}
34503526Sxy150489 
34513526Sxy150489 	/*
34523526Sxy150489 	 * enforce limits
34533526Sxy150489 	 */
34543526Sxy150489 	if (propval > maxval) {
34553526Sxy150489 		propval = maxval;
34564919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34573526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
34583526Sxy150489 		    propname, propval);
34593526Sxy150489 	}
34603526Sxy150489 
34613526Sxy150489 	if (propval < minval) {
34623526Sxy150489 		propval = minval;
34634919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
34643526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
34653526Sxy150489 		    propname, propval);
34663526Sxy150489 	}
34673526Sxy150489 
34683526Sxy150489 	return (propval);
34693526Sxy150489 }
34703526Sxy150489 
34713526Sxy150489 static boolean_t
34724061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
34733526Sxy150489 {
34744061Sxy150489 	uint16_t speed, duplex, phydata;
34754061Sxy150489 	boolean_t link_changed = B_FALSE;
34763526Sxy150489 	struct e1000_hw *hw;
34773526Sxy150489 	uint32_t reg_tarc;
34783526Sxy150489 
34794919Sxy150489 	hw = &Adapter->shared;
34803526Sxy150489 
34813526Sxy150489 	if (e1000g_link_up(Adapter)) {
34823526Sxy150489 		/*
34833526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
34843526Sxy150489 		 */
34854061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
3486*7426SChenliang.Xu@Sun.COM 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
34874061Sxy150489 			Adapter->link_speed = speed;
34884061Sxy150489 			Adapter->link_duplex = duplex;
34894061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
34904061Sxy150489 			link_changed = B_TRUE;
34914061Sxy150489 
34924061Sxy150489 			Adapter->tx_link_down_timeout = 0;
34934061Sxy150489 
34944919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
34954919Sxy150489 			    (hw->mac.type == e1000_82572)) {
34966735Scc210113 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
34974061Sxy150489 				if (speed == SPEED_1000)
34984061Sxy150489 					reg_tarc |= (1 << 21);
34994061Sxy150489 				else
35004061Sxy150489 					reg_tarc &= ~(1 << 21);
35016735Scc210113 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
35023526Sxy150489 			}
35033526Sxy150489 		}
35043526Sxy150489 		Adapter->smartspeed = 0;
35053526Sxy150489 	} else {
35064061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
35073526Sxy150489 			Adapter->link_speed = 0;
35083526Sxy150489 			Adapter->link_duplex = 0;
35094061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
35104061Sxy150489 			link_changed = B_TRUE;
35114061Sxy150489 
35123526Sxy150489 			/*
35133526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
35143526Sxy150489 			 * driver loses link disable auto master/slave
35153526Sxy150489 			 * resolution.
35163526Sxy150489 			 */
35174919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
3518*7426SChenliang.Xu@Sun.COM 				(void) e1000_read_phy_reg(hw,
35193526Sxy150489 				    PHY_1000T_CTRL, &phydata);
35203526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
3521*7426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
35223526Sxy150489 				    PHY_1000T_CTRL, phydata);
35233526Sxy150489 			}
35243526Sxy150489 		} else {
35253526Sxy150489 			e1000g_smartspeed(Adapter);
35263526Sxy150489 		}
35274061Sxy150489 
35285273Sgl147354 		if (Adapter->chip_state == E1000G_START) {
35294061Sxy150489 			if (Adapter->tx_link_down_timeout <
35304061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35314061Sxy150489 				Adapter->tx_link_down_timeout++;
35324061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
35334061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
35344919Sxy150489 				e1000g_tx_clean(Adapter);
35354061Sxy150489 				Adapter->tx_link_down_timeout++;
35364061Sxy150489 			}
35374061Sxy150489 		}
35383526Sxy150489 	}
35393526Sxy150489 
35405273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
35415273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
35425273Sgl147354 
35434061Sxy150489 	return (link_changed);
35444061Sxy150489 }
35454061Sxy150489 
35466394Scc210113 /*
35476394Scc210113  * e1000g_reset_link - Using the link properties to setup the link
35486394Scc210113  */
35496394Scc210113 int
35506394Scc210113 e1000g_reset_link(struct e1000g *Adapter)
35516394Scc210113 {
35526394Scc210113 	struct e1000_mac_info *mac;
35536394Scc210113 	struct e1000_phy_info *phy;
35546394Scc210113 	boolean_t invalid;
35556394Scc210113 
35566394Scc210113 	mac = &Adapter->shared.mac;
35576394Scc210113 	phy = &Adapter->shared.phy;
35586394Scc210113 	invalid = B_FALSE;
35596394Scc210113 
35606394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
35616394Scc210113 		mac->autoneg = B_TRUE;
35626394Scc210113 		phy->autoneg_advertised = 0;
35636394Scc210113 
35646394Scc210113 		/*
35656394Scc210113 		 * 1000hdx is not supported for autonegotiation
35666394Scc210113 		 */
35676394Scc210113 		if (Adapter->param_adv_1000fdx == 1)
35686394Scc210113 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
35696394Scc210113 
35706394Scc210113 		if (Adapter->param_adv_100fdx == 1)
35716394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
35726394Scc210113 
35736394Scc210113 		if (Adapter->param_adv_100hdx == 1)
35746394Scc210113 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
35756394Scc210113 
35766394Scc210113 		if (Adapter->param_adv_10fdx == 1)
35776394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
35786394Scc210113 
35796394Scc210113 		if (Adapter->param_adv_10hdx == 1)
35806394Scc210113 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
35816394Scc210113 
35826394Scc210113 		if (phy->autoneg_advertised == 0)
35836394Scc210113 			invalid = B_TRUE;
35846394Scc210113 	} else {
35856394Scc210113 		mac->autoneg = B_FALSE;
35866394Scc210113 
35876394Scc210113 		/*
35886394Scc210113 		 * 1000fdx and 1000hdx are not supported for forced link
35896394Scc210113 		 */
35906394Scc210113 		if (Adapter->param_adv_100fdx == 1)
35916394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
35926394Scc210113 		else if (Adapter->param_adv_100hdx == 1)
35936394Scc210113 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
35946394Scc210113 		else if (Adapter->param_adv_10fdx == 1)
35956394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
35966394Scc210113 		else if (Adapter->param_adv_10hdx == 1)
35976394Scc210113 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
35986394Scc210113 		else
35996394Scc210113 			invalid = B_TRUE;
36006394Scc210113 
36016394Scc210113 	}
36026394Scc210113 
36036394Scc210113 	if (invalid) {
36046394Scc210113 		e1000g_log(Adapter, CE_WARN,
36056394Scc210113 		    "Invalid link sets. Setup link to"
36066394Scc210113 		    "support autonegotiation with all link capabilities.");
36076394Scc210113 		mac->autoneg = B_TRUE;
36086394Scc210113 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
36096394Scc210113 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
36106394Scc210113 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
36116394Scc210113 	}
36126394Scc210113 
36136394Scc210113 	return (e1000_setup_link(&Adapter->shared));
36146394Scc210113 }
36156394Scc210113 
36164061Sxy150489 static void
36174919Sxy150489 e1000g_local_timer(void *ws)
36184061Sxy150489 {
36194061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
36204061Sxy150489 	struct e1000_hw *hw;
36214061Sxy150489 	e1000g_ether_addr_t ether_addr;
36224061Sxy150489 	boolean_t link_changed;
36234061Sxy150489 
36244919Sxy150489 	hw = &Adapter->shared;
36254919Sxy150489 
36265273Sgl147354 	if (Adapter->chip_state == E1000G_ERROR) {
36275273Sgl147354 		Adapter->reset_count++;
36285273Sgl147354 		if (e1000g_global_reset(Adapter))
36295273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36305273Sgl147354 			    DDI_SERVICE_RESTORED);
36315273Sgl147354 		else
36325273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36335273Sgl147354 			    DDI_SERVICE_LOST);
36345273Sgl147354 		return;
36355273Sgl147354 	}
36365273Sgl147354 
36374061Sxy150489 	if (e1000g_stall_check(Adapter)) {
36384919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
36394061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
36405273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
36414061Sxy150489 		Adapter->reset_count++;
36425273Sgl147354 		if (e1000g_reset(Adapter))
36435273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36445273Sgl147354 			    DDI_SERVICE_RESTORED);
36455273Sgl147354 		else
36465273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
36475273Sgl147354 			    DDI_SERVICE_LOST);
36485273Sgl147354 		return;
36494061Sxy150489 	}
36504061Sxy150489 
36514061Sxy150489 	link_changed = B_FALSE;
36525082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
36534061Sxy150489 	if (Adapter->link_complete)
36544061Sxy150489 		link_changed = e1000g_link_check(Adapter);
36555082Syy150190 	rw_exit(&Adapter->chip_lock);
36564061Sxy150489 
36577133Scc210113 	if (link_changed)
36584061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
36597133Scc210113 
36607133Scc210113 	/*
36617133Scc210113 	 * Workaround for esb2. Data stuck in fifo on a link
36627133Scc210113 	 * down event. Reset the adapter to recover it.
36637133Scc210113 	 */
36647133Scc210113 	if (Adapter->esb2_workaround) {
36657133Scc210113 		Adapter->esb2_workaround = B_FALSE;
36667133Scc210113 		(void) e1000g_reset(Adapter);
36674139Sxy150489 	}
36684061Sxy150489 
36693526Sxy150489 	/*
36703526Sxy150489 	 * With 82571 controllers, any locally administered address will
36713526Sxy150489 	 * be overwritten when there is a reset on the other port.
36723526Sxy150489 	 * Detect this circumstance and correct it.
36733526Sxy150489 	 */
36744919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
36754919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
36764919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
36774919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
36783526Sxy150489 
36793526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
36803526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
36813526Sxy150489 
36824919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
36834919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
36844919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
36854919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
36864919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
36874919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
36884919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
36893526Sxy150489 		}
36903526Sxy150489 	}
36913526Sxy150489 
36923526Sxy150489 	/*
36934919Sxy150489 	 * Long TTL workaround for 82541/82547
36943526Sxy150489 	 */
3695*7426SChenliang.Xu@Sun.COM 	(void) e1000_igp_ttl_workaround_82547(hw);
36963526Sxy150489 
36973526Sxy150489 	/*
36983526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
36993526Sxy150489 	 * change the value in steps...
37003526Sxy150489 	 * These properties should only be set for 10/100
37013526Sxy150489 	 */
37026735Scc210113 	if ((hw->phy.media_type == e1000_media_type_copper) &&
37034061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
37044061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
37053526Sxy150489 		e1000_update_adaptive(hw);
37063526Sxy150489 	}
37073526Sxy150489 	/*
37083526Sxy150489 	 * Set Timer Interrupts
37093526Sxy150489 	 */
37104919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
37114919Sxy150489 
37125273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
37135273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
37145273Sgl147354 
37154919Sxy150489 	restart_watchdog_timer(Adapter);
37163526Sxy150489 }
37173526Sxy150489 
37184061Sxy150489 /*
37194061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
37204061Sxy150489  * is expired, which indicates the completion of the link setup. The link
37214061Sxy150489  * state will not be updated until the link setup is completed. And the
37224061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
37234061Sxy150489  * in this function. It will be updated in the local timer routine or the
37244061Sxy150489  * interrupt service routine after the interface is started (plumbed).
37254061Sxy150489  */
37263526Sxy150489 static void
37274061Sxy150489 e1000g_link_timer(void *arg)
37283526Sxy150489 {
37294061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
37303526Sxy150489 
37314919Sxy150489 	mutex_enter(&Adapter->link_lock);
37324061Sxy150489 	Adapter->link_complete = B_TRUE;
37334061Sxy150489 	Adapter->link_tid = 0;
37344919Sxy150489 	mutex_exit(&Adapter->link_lock);
37353526Sxy150489 }
37363526Sxy150489 
37373526Sxy150489 /*
37384919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
37394919Sxy150489  *
37404919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
37414919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
37423526Sxy150489  */
37433526Sxy150489 static void
37443526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
37453526Sxy150489 {
37463526Sxy150489 	int forced;
37474919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
37484919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
37493526Sxy150489 
37503526Sxy150489 	/*
37513526Sxy150489 	 * get value out of config file
37523526Sxy150489 	 */
37534919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
37543526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
37553526Sxy150489 
37563526Sxy150489 	switch (forced) {
37573526Sxy150489 	case GDIAG_10_HALF:
37583526Sxy150489 		/*
37593526Sxy150489 		 * Disable Auto Negotiation
37603526Sxy150489 		 */
37614919Sxy150489 		mac->autoneg = B_FALSE;
37624919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
37633526Sxy150489 		break;
37643526Sxy150489 	case GDIAG_10_FULL:
37653526Sxy150489 		/*
37663526Sxy150489 		 * Disable Auto Negotiation
37673526Sxy150489 		 */
37684919Sxy150489 		mac->autoneg = B_FALSE;
37694919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
37703526Sxy150489 		break;
37713526Sxy150489 	case GDIAG_100_HALF:
37723526Sxy150489 		/*
37733526Sxy150489 		 * Disable Auto Negotiation
37743526Sxy150489 		 */
37754919Sxy150489 		mac->autoneg = B_FALSE;
37764919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
37773526Sxy150489 		break;
37783526Sxy150489 	case GDIAG_100_FULL:
37793526Sxy150489 		/*
37803526Sxy150489 		 * Disable Auto Negotiation
37813526Sxy150489 		 */
37824919Sxy150489 		mac->autoneg = B_FALSE;
37834919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
37843526Sxy150489 		break;
37853526Sxy150489 	case GDIAG_1000_FULL:
37863526Sxy150489 		/*
37873526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
37883526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
37893526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
37903526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
37913526Sxy150489 		 * we are allowed to link without any negotiation.
37923526Sxy150489 		 */
37934919Sxy150489 		mac->autoneg = B_TRUE;
37944919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
37953526Sxy150489 		break;
37963526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
37974919Sxy150489 		mac->autoneg = B_TRUE;
37984919Sxy150489 		phy->autoneg_advertised =
37994919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
38004349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
38014349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
38023526Sxy150489 		break;
38033526Sxy150489 	}	/* switch */
38043526Sxy150489 }
38053526Sxy150489 
38063526Sxy150489 /*
38074919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
38084919Sxy150489  *
38094919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
38103526Sxy150489  */
38113526Sxy150489 static void
38123526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
38133526Sxy150489 {
38143526Sxy150489 	int max_frame;
38154919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
38164919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
38173526Sxy150489 
38183526Sxy150489 	/*
38193526Sxy150489 	 * get value out of config file
38203526Sxy150489 	 */
38214919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
38223526Sxy150489 
38233526Sxy150489 	switch (max_frame) {
38243526Sxy150489 	case 0:
38256394Scc210113 		Adapter->default_mtu = ETHERMTU;
38263526Sxy150489 		break;
38276394Scc210113 	/*
38286394Scc210113 	 * To avoid excessive memory allocation for rx buffers,
38296394Scc210113 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
38306394Scc210113 	 */
38313526Sxy150489 	case 1:
38326394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
38336394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38346394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38353526Sxy150489 		break;
38363526Sxy150489 	case 2:
38376394Scc210113 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
38386394Scc210113 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
38396394Scc210113 		    E1000G_IPALIGNPRESERVEROOM;
38403526Sxy150489 		break;
38413526Sxy150489 	case 3:
38426394Scc210113 		if (mac->type >= e1000_82571)
38436394Scc210113 			Adapter->default_mtu = MAXIMUM_MTU;
38443526Sxy150489 		else
38456394Scc210113 			Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
38466394Scc210113 			    sizeof (struct ether_vlan_header) - ETHERFCSL -
38476394Scc210113 			    E1000G_IPALIGNPRESERVEROOM;
38483526Sxy150489 		break;
38493526Sxy150489 	default:
38506394Scc210113 		Adapter->default_mtu = ETHERMTU;
38513526Sxy150489 		break;
38523526Sxy150489 	}	/* switch */
38533526Sxy150489 
38546735Scc210113 	Adapter->max_frame_size = Adapter->default_mtu +
38556394Scc210113 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
38566394Scc210113 
38573526Sxy150489 	/* ich8 does not do jumbo frames */
38584919Sxy150489 	if (mac->type == e1000_ich8lan) {
38597133Scc210113 		Adapter->max_frame_size = ETHERMTU +
38607133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
38614919Sxy150489 	}
38624919Sxy150489 
38634919Sxy150489 	/* ich9 does not do jumbo frames on one phy type */
38644919Sxy150489 	if ((mac->type == e1000_ich9lan) &&
38654919Sxy150489 	    (phy->type == e1000_phy_ife)) {
38667133Scc210113 		Adapter->max_frame_size = ETHERMTU +
38677133Scc210113 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
38683526Sxy150489 	}
38693526Sxy150489 }
38703526Sxy150489 
38713526Sxy150489 static void
38724919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
38733526Sxy150489 {
38744919Sxy150489 	Adapter->watchdog_tid =
38754919Sxy150489 	    timeout(e1000g_local_timer,
38763526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
38773526Sxy150489 }
38784919Sxy150489 #pragma inline(arm_watchdog_timer)
38794919Sxy150489 
38804919Sxy150489 static void
38814919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
38824919Sxy150489 {
38834919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
38844919Sxy150489 
38854919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
38864919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
38874919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
38884919Sxy150489 		arm_watchdog_timer(Adapter);
38894919Sxy150489 	}
38904919Sxy150489 
38914919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
38924919Sxy150489 }
38933526Sxy150489 
38943526Sxy150489 static void
38954919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
38963526Sxy150489 {
38973526Sxy150489 	timeout_id_t tid;
38983526Sxy150489 
38994919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39004919Sxy150489 
39014919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
39024919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
39034919Sxy150489 	tid = Adapter->watchdog_tid;
39044919Sxy150489 	Adapter->watchdog_tid = 0;
39054919Sxy150489 
39064919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39073526Sxy150489 
39083526Sxy150489 	if (tid != 0)
39093526Sxy150489 		(void) untimeout(tid);
39103526Sxy150489 }
39113526Sxy150489 
39123526Sxy150489 static void
39134919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
39143526Sxy150489 {
39154919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39164919Sxy150489 
39174919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
39184919Sxy150489 		if (!Adapter->watchdog_timer_started) {
39194919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
39204919Sxy150489 			arm_watchdog_timer(Adapter);
39213526Sxy150489 		}
39223526Sxy150489 	}
39233526Sxy150489 
39244919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39254919Sxy150489 }
39264919Sxy150489 
39274919Sxy150489 static void
39284919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
39294919Sxy150489 {
39304919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39314919Sxy150489 
39324919Sxy150489 	if (Adapter->watchdog_timer_started)
39334919Sxy150489 		arm_watchdog_timer(Adapter);
39344919Sxy150489 
39354919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39363526Sxy150489 }
39373526Sxy150489 
39383526Sxy150489 static void
39394919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
39403526Sxy150489 {
39414919Sxy150489 	timeout_id_t tid;
39424919Sxy150489 
39434919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
39444919Sxy150489 
39454919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
39464919Sxy150489 	tid = Adapter->watchdog_tid;
39474919Sxy150489 	Adapter->watchdog_tid = 0;
39484919Sxy150489 
39494919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
39504919Sxy150489 
39514919Sxy150489 	if (tid != 0)
39524919Sxy150489 		(void) untimeout(tid);
39533526Sxy150489 }
39543526Sxy150489 
39553526Sxy150489 static void
39564919Sxy150489 stop_link_timer(struct e1000g *Adapter)
39573526Sxy150489 {
39583526Sxy150489 	timeout_id_t tid;
39593526Sxy150489 
39604919Sxy150489 	/* Disable the link timer */
39614919Sxy150489 	mutex_enter(&Adapter->link_lock);
39624919Sxy150489 
39634919Sxy150489 	tid = Adapter->link_tid;
39644919Sxy150489 	Adapter->link_tid = 0;
39654919Sxy150489 
39664919Sxy150489 	mutex_exit(&Adapter->link_lock);
39674919Sxy150489 
39684919Sxy150489 	if (tid != 0)
39694919Sxy150489 		(void) untimeout(tid);
39704919Sxy150489 }
39714919Sxy150489 
39724919Sxy150489 static void
39734919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
39744919Sxy150489 {
39754919Sxy150489 	timeout_id_t tid;
39764919Sxy150489 
39774919Sxy150489 	/* Disable the tx timer for 82547 chipset */
39784919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
39794919Sxy150489 
39804919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
39814919Sxy150489 	tid = tx_ring->timer_id_82547;
39824919Sxy150489 	tx_ring->timer_id_82547 = 0;
39834919Sxy150489 
39844919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
39853526Sxy150489 
39863526Sxy150489 	if (tid != 0)
39873526Sxy150489 		(void) untimeout(tid);
39883526Sxy150489 }
39893526Sxy150489 
39903526Sxy150489 void
39914919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
39923526Sxy150489 {
39934919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
39944919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
39953526Sxy150489 }
39963526Sxy150489 
39973526Sxy150489 void
39984919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
39993526Sxy150489 {
40004919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
40015882Syy150190 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
40025882Syy150190 
40035882Syy150190 	if (Adapter->tx_intr_enable)
40045882Syy150190 		e1000g_mask_tx_interrupt(Adapter);
40053526Sxy150489 }
40063526Sxy150489 
40073526Sxy150489 void
40084919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
40093526Sxy150489 {
40104919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
40113526Sxy150489 }
40123526Sxy150489 
40133526Sxy150489 void
40144919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
40153526Sxy150489 {
40165882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
40173526Sxy150489 }
40183526Sxy150489 
40193526Sxy150489 void
40204919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
40213526Sxy150489 {
40225882Syy150190 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
40233526Sxy150489 }
40243526Sxy150489 
40253526Sxy150489 static void
40264919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
40273526Sxy150489 {
40284919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
40293526Sxy150489 	uint16_t phy_status;
40303526Sxy150489 	uint16_t phy_ctrl;
40313526Sxy150489 
40323526Sxy150489 	/*
40333526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
40343526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
40353526Sxy150489 	 */
40364919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
40374919Sxy150489 	    !hw->mac.autoneg ||
40384919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
40393526Sxy150489 		return;
40403526Sxy150489 
40413526Sxy150489 	/*
40423526Sxy150489 	 * True if this is the first call of this function or after every
40433526Sxy150489 	 * 30 seconds of not having link
40443526Sxy150489 	 */
40454919Sxy150489 	if (Adapter->smartspeed == 0) {
40463526Sxy150489 		/*
40473526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
40483526Sxy150489 		 * assume back-to-back
40493526Sxy150489 		 */
4050*7426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
40513526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
40523526Sxy150489 			return;
40533526Sxy150489 
4054*7426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
40553526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
40563526Sxy150489 			return;
40573526Sxy150489 		/*
40583526Sxy150489 		 * We're assuming back-2-back because our status register
40593526Sxy150489 		 * insists! there's a fault in the master/slave
40603526Sxy150489 		 * relationship that was "negotiated"
40613526Sxy150489 		 */
4062*7426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
40633526Sxy150489 		/*
40643526Sxy150489 		 * Is the phy configured for manual configuration of
40653526Sxy150489 		 * master/slave?
40663526Sxy150489 		 */
40673526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
40683526Sxy150489 			/*
40693526Sxy150489 			 * Yes.  Then disable manual configuration (enable
40703526Sxy150489 			 * auto configuration) of master/slave
40713526Sxy150489 			 */
40723526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
4073*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw,
40743526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
40753526Sxy150489 			/*
40763526Sxy150489 			 * Effectively starting the clock
40773526Sxy150489 			 */
40784919Sxy150489 			Adapter->smartspeed++;
40793526Sxy150489 			/*
40803526Sxy150489 			 * Restart autonegotiation
40813526Sxy150489 			 */
40824919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
40834919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
40843526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
40853526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
4086*7426SChenliang.Xu@Sun.COM 				(void) e1000_write_phy_reg(hw,
40874919Sxy150489 				    PHY_CONTROL, phy_ctrl);
40883526Sxy150489 			}
40893526Sxy150489 		}
40903526Sxy150489 		return;
40913526Sxy150489 		/*
40923526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
40933526Sxy150489 		 * you should reset the smartspeed counter once you obtain
40943526Sxy150489 		 * link
40953526Sxy150489 		 */
40964919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
40973526Sxy150489 		/*
40983526Sxy150489 		 * Yes.  Remember, we did at the start determine that
40993526Sxy150489 		 * there's a master/slave configuration fault, so we're
41003526Sxy150489 		 * still assuming there's someone on the other end, but we
41013526Sxy150489 		 * just haven't yet been able to talk to it. We then
41023526Sxy150489 		 * re-enable auto configuration of master/slave to see if
41033526Sxy150489 		 * we're running 2/3 pair cables.
41043526Sxy150489 		 */
41053526Sxy150489 		/*
41063526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
41073526Sxy150489 		 */
4108*7426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
41093526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
4110*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
41113526Sxy150489 		/*
41123526Sxy150489 		 * Restart autoneg with phy enabled for manual
41133526Sxy150489 		 * configuration of master/slave
41143526Sxy150489 		 */
41154919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
41164919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
41173526Sxy150489 			phy_ctrl |=
41183526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
4119*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
41203526Sxy150489 		}
41213526Sxy150489 		/*
41223526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
41233526Sxy150489 		 * link as a result.
41243526Sxy150489 		 */
41253526Sxy150489 	}
41263526Sxy150489 	/*
41273526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
41283526Sxy150489 	 * seconds)
41293526Sxy150489 	 */
41304919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
41314919Sxy150489 		Adapter->smartspeed = 0;
41323526Sxy150489 }
41333526Sxy150489 
41343526Sxy150489 static boolean_t
41353526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
41363526Sxy150489 {
41373526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
41383526Sxy150489 	const uint8_t addr_test2[6] =
41393526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
41403526Sxy150489 
41413526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
41423526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
41433526Sxy150489 		return (B_FALSE);
41443526Sxy150489 
41453526Sxy150489 	return (B_TRUE);
41463526Sxy150489 }
41473526Sxy150489 
41483526Sxy150489 /*
41494919Sxy150489  * e1000g_stall_check - check for tx stall
41504919Sxy150489  *
41514919Sxy150489  * This function checks if the adapter is stalled (in transmit).
41524919Sxy150489  *
41534919Sxy150489  * It is called each time the watchdog timeout is invoked.
41544919Sxy150489  * If the transmit descriptor reclaim continuously fails,
41554919Sxy150489  * the watchdog value will increment by 1. If the watchdog
41564919Sxy150489  * value exceeds the threshold, the adapter is assumed to
41574919Sxy150489  * have stalled and need to be reset.
41583526Sxy150489  */
41593526Sxy150489 static boolean_t
41603526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
41613526Sxy150489 {
41624919Sxy150489 	e1000g_tx_ring_t *tx_ring;
41634919Sxy150489 
41644919Sxy150489 	tx_ring = Adapter->tx_ring;
41654919Sxy150489 
41664061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
41673526Sxy150489 		return (B_FALSE);
41683526Sxy150489 
41694919Sxy150489 	if (tx_ring->recycle_fail > 0)
41704919Sxy150489 		tx_ring->stall_watchdog++;
41713526Sxy150489 	else
41724919Sxy150489 		tx_ring->stall_watchdog = 0;
41734919Sxy150489 
41744919Sxy150489 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
41753526Sxy150489 		return (B_FALSE);
41763526Sxy150489 
41774919Sxy150489 	tx_ring->stall_watchdog = 0;
41784919Sxy150489 	tx_ring->recycle_fail = 0;
41794919Sxy150489 
41803526Sxy150489 	return (B_TRUE);
41813526Sxy150489 }
41823526Sxy150489 
41834919Sxy150489 #ifdef E1000G_DEBUG
41843526Sxy150489 static enum ioc_reply
41853526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
41863526Sxy150489 {
41873526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
41883526Sxy150489 	e1000g_peekpoke_t *ppd;
41893526Sxy150489 	uint64_t mem_va;
41903526Sxy150489 	uint64_t maxoff;
41913526Sxy150489 	boolean_t peek;
41923526Sxy150489 
41933526Sxy150489 	switch (iocp->ioc_cmd) {
41943526Sxy150489 
41953526Sxy150489 	case E1000G_IOC_REG_PEEK:
41963526Sxy150489 		peek = B_TRUE;
41973526Sxy150489 		break;
41983526Sxy150489 
41993526Sxy150489 	case E1000G_IOC_REG_POKE:
42003526Sxy150489 		peek = B_FALSE;
42013526Sxy150489 		break;
42023526Sxy150489 
42033526Sxy150489 	deault:
42044919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42054349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
42064349Sxy150489 		    iocp->ioc_cmd);
42073526Sxy150489 		return (IOC_INVAL);
42083526Sxy150489 	}
42093526Sxy150489 
42103526Sxy150489 	/*
42113526Sxy150489 	 * Validate format of ioctl
42123526Sxy150489 	 */
42133526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
42143526Sxy150489 		return (IOC_INVAL);
42153526Sxy150489 	if (mp->b_cont == NULL)
42163526Sxy150489 		return (IOC_INVAL);
42173526Sxy150489 
4218*7426SChenliang.Xu@Sun.COM 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
42193526Sxy150489 
42203526Sxy150489 	/*
42213526Sxy150489 	 * Validate request parameters
42223526Sxy150489 	 */
42233526Sxy150489 	switch (ppd->pp_acc_space) {
42243526Sxy150489 
42253526Sxy150489 	default:
42264919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
42274349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
42284349Sxy150489 		    ppd->pp_acc_space);
42293526Sxy150489 		return (IOC_INVAL);
42303526Sxy150489 
42313526Sxy150489 	case E1000G_PP_SPACE_REG:
42323526Sxy150489 		/*
42333526Sxy150489 		 * Memory-mapped I/O space
42343526Sxy150489 		 */
42353526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
42363526Sxy150489 		if (ppd->pp_acc_size != 4)
42373526Sxy150489 			return (IOC_INVAL);
42383526Sxy150489 
42393526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
42403526Sxy150489 			return (IOC_INVAL);
42413526Sxy150489 
42423526Sxy150489 		mem_va = 0;
42433526Sxy150489 		maxoff = 0x10000;
42443526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
42453526Sxy150489 		break;
42463526Sxy150489 
42473526Sxy150489 	case E1000G_PP_SPACE_E1000G:
42483526Sxy150489 		/*
42493526Sxy150489 		 * E1000g data structure!
42503526Sxy150489 		 */
42513526Sxy150489 		mem_va = (uintptr_t)e1000gp;
42523526Sxy150489 		maxoff = sizeof (struct e1000g);
42533526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
42543526Sxy150489 		break;
42553526Sxy150489 
42563526Sxy150489 	}
42573526Sxy150489 
42583526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
42593526Sxy150489 		return (IOC_INVAL);
42603526Sxy150489 
42613526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
42623526Sxy150489 		return (IOC_INVAL);
42633526Sxy150489 
42643526Sxy150489 	/*
42653526Sxy150489 	 * All OK - go!
42663526Sxy150489 	 */
42673526Sxy150489 	ppd->pp_acc_offset += mem_va;
42683526Sxy150489 	(*ppfn)(e1000gp, ppd);
42693526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
42703526Sxy150489 }
42713526Sxy150489 
42723526Sxy150489 static void
42733526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
42743526Sxy150489 {
42753526Sxy150489 	ddi_acc_handle_t handle;
42763526Sxy150489 	uint32_t *regaddr;
42773526Sxy150489 
42784919Sxy150489 	handle = e1000gp->osdep.reg_handle;
4279*7426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
4280*7426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
42813526Sxy150489 
42823526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
42833526Sxy150489 }
42843526Sxy150489 
42853526Sxy150489 static void
42863526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
42873526Sxy150489 {
42883526Sxy150489 	ddi_acc_handle_t handle;
42893526Sxy150489 	uint32_t *regaddr;
42903526Sxy150489 	uint32_t value;
42913526Sxy150489 
42924919Sxy150489 	handle = e1000gp->osdep.reg_handle;
4293*7426SChenliang.Xu@Sun.COM 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
4294*7426SChenliang.Xu@Sun.COM 	    (uintptr_t)ppd->pp_acc_offset);
42953526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
42963526Sxy150489 
42973526Sxy150489 	ddi_put32(handle, regaddr, value);
42983526Sxy150489 }
42993526Sxy150489 
43003526Sxy150489 static void
43013526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43023526Sxy150489 {
43033526Sxy150489 	uint64_t value;
43043526Sxy150489 	void *vaddr;
43053526Sxy150489 
43063526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
43073526Sxy150489 
43083526Sxy150489 	switch (ppd->pp_acc_size) {
43093526Sxy150489 	case 1:
43103526Sxy150489 		value = *(uint8_t *)vaddr;
43113526Sxy150489 		break;
43123526Sxy150489 
43133526Sxy150489 	case 2:
43143526Sxy150489 		value = *(uint16_t *)vaddr;
43153526Sxy150489 		break;
43163526Sxy150489 
43173526Sxy150489 	case 4:
43183526Sxy150489 		value = *(uint32_t *)vaddr;
43193526Sxy150489 		break;
43203526Sxy150489 
43213526Sxy150489 	case 8:
43223526Sxy150489 		value = *(uint64_t *)vaddr;
43233526Sxy150489 		break;
43243526Sxy150489 	}
43253526Sxy150489 
43264919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
43274349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
43284349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
43293526Sxy150489 
43303526Sxy150489 	ppd->pp_acc_data = value;
43313526Sxy150489 }
43323526Sxy150489 
43333526Sxy150489 static void
43343526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
43353526Sxy150489 {
43363526Sxy150489 	uint64_t value;
43373526Sxy150489 	void *vaddr;
43383526Sxy150489 
43393526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
43403526Sxy150489 	value = ppd->pp_acc_data;
43413526Sxy150489 
43424919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
43434349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
43444349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
43453526Sxy150489 
43463526Sxy150489 	switch (ppd->pp_acc_size) {
43473526Sxy150489 	case 1:
43483526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
43493526Sxy150489 		break;
43503526Sxy150489 
43513526Sxy150489 	case 2:
43523526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
43533526Sxy150489 		break;
43543526Sxy150489 
43553526Sxy150489 	case 4:
43563526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
43573526Sxy150489 		break;
43583526Sxy150489 
43593526Sxy150489 	case 8:
43603526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
43613526Sxy150489 		break;
43623526Sxy150489 	}
43633526Sxy150489 }
43644919Sxy150489 #endif
43653526Sxy150489 
43663526Sxy150489 /*
43673526Sxy150489  * Loopback Support
43683526Sxy150489  */
43693526Sxy150489 static lb_property_t lb_normal =
43703526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
43713526Sxy150489 static lb_property_t lb_external1000 =
43723526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
43733526Sxy150489 static lb_property_t lb_external100 =
43743526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
43753526Sxy150489 static lb_property_t lb_external10 =
43763526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
43773526Sxy150489 static lb_property_t lb_phy =
43783526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
43793526Sxy150489 
43803526Sxy150489 static enum ioc_reply
43813526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
43823526Sxy150489 {
43833526Sxy150489 	lb_info_sz_t *lbsp;
43843526Sxy150489 	lb_property_t *lbpp;
43853526Sxy150489 	struct e1000_hw *hw;
43863526Sxy150489 	uint32_t *lbmp;
43873526Sxy150489 	uint32_t size;
43883526Sxy150489 	uint32_t value;
43893526Sxy150489 
43904919Sxy150489 	hw = &Adapter->shared;
43913526Sxy150489 
43923526Sxy150489 	if (mp->b_cont == NULL)
43933526Sxy150489 		return (IOC_INVAL);
43943526Sxy150489 
43957133Scc210113 	if (!e1000g_check_loopback_support(hw)) {
43967133Scc210113 		e1000g_log(NULL, CE_WARN,
43977133Scc210113 		    "Loopback is not supported on e1000g%d", Adapter->instance);
43987133Scc210113 		return (IOC_INVAL);
43997133Scc210113 	}
44007133Scc210113 
44013526Sxy150489 	switch (iocp->ioc_cmd) {
44023526Sxy150489 	default:
44033526Sxy150489 		return (IOC_INVAL);
44043526Sxy150489 
44053526Sxy150489 	case LB_GET_INFO_SIZE:
44063526Sxy150489 		size = sizeof (lb_info_sz_t);
44073526Sxy150489 		if (iocp->ioc_count != size)
44083526Sxy150489 			return (IOC_INVAL);
44093526Sxy150489 
44105082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
44115082Syy150190 		e1000g_get_phy_state(Adapter);
44125082Syy150190 
44135082Syy150190 		/*
44145082Syy150190 		 * Workaround for hardware faults. In order to get a stable
44155082Syy150190 		 * state of phy, we will wait for a specific interval and
44165082Syy150190 		 * try again. The time delay is an experiential value based
44175082Syy150190 		 * on our testing.
44185082Syy150190 		 */
44195082Syy150190 		msec_delay(100);
44205082Syy150190 		e1000g_get_phy_state(Adapter);
44215082Syy150190 		rw_exit(&Adapter->chip_lock);
44223526Sxy150489 
44233526Sxy150489 		value = sizeof (lb_normal);
44245082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44255082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44266735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44276735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44283526Sxy150489 			value += sizeof (lb_phy);
44294919Sxy150489 			switch (hw->mac.type) {
44303526Sxy150489 			case e1000_82571:
44313526Sxy150489 			case e1000_82572:
44327133Scc210113 			case e1000_80003es2lan:
44333526Sxy150489 				value += sizeof (lb_external1000);
44343526Sxy150489 				break;
44353526Sxy150489 			}
44363526Sxy150489 		}
44375082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
44385082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
44393526Sxy150489 			value += sizeof (lb_external100);
44405082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
44413526Sxy150489 			value += sizeof (lb_external10);
44423526Sxy150489 
4443*7426SChenliang.Xu@Sun.COM 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
44443526Sxy150489 		*lbsp = value;
44453526Sxy150489 		break;
44463526Sxy150489 
44473526Sxy150489 	case LB_GET_INFO:
44483526Sxy150489 		value = sizeof (lb_normal);
44495082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44505082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44516735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44526735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44533526Sxy150489 			value += sizeof (lb_phy);
44544919Sxy150489 			switch (hw->mac.type) {
44553526Sxy150489 			case e1000_82571:
44563526Sxy150489 			case e1000_82572:
44577133Scc210113 			case e1000_80003es2lan:
44583526Sxy150489 				value += sizeof (lb_external1000);
44593526Sxy150489 				break;
44603526Sxy150489 			}
44613526Sxy150489 		}
44625082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
44635082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
44643526Sxy150489 			value += sizeof (lb_external100);
44655082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
44663526Sxy150489 			value += sizeof (lb_external10);
44673526Sxy150489 
44683526Sxy150489 		size = value;
44693526Sxy150489 		if (iocp->ioc_count != size)
44703526Sxy150489 			return (IOC_INVAL);
44713526Sxy150489 
44723526Sxy150489 		value = 0;
4473*7426SChenliang.Xu@Sun.COM 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
44743526Sxy150489 		lbpp[value++] = lb_normal;
44755082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44765082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
44776735Scc210113 		    (hw->phy.media_type == e1000_media_type_fiber) ||
44786735Scc210113 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
44793526Sxy150489 			lbpp[value++] = lb_phy;
44804919Sxy150489 			switch (hw->mac.type) {
44813526Sxy150489 			case e1000_82571:
44823526Sxy150489 			case e1000_82572:
44837133Scc210113 			case e1000_80003es2lan:
44843526Sxy150489 				lbpp[value++] = lb_external1000;
44853526Sxy150489 				break;
44863526Sxy150489 			}
44873526Sxy150489 		}
44885082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
44895082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
44903526Sxy150489 			lbpp[value++] = lb_external100;
44915082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
44923526Sxy150489 			lbpp[value++] = lb_external10;
44933526Sxy150489 		break;
44943526Sxy150489 
44953526Sxy150489 	case LB_GET_MODE:
44963526Sxy150489 		size = sizeof (uint32_t);
44973526Sxy150489 		if (iocp->ioc_count != size)
44983526Sxy150489 			return (IOC_INVAL);
44993526Sxy150489 
4500*7426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45013526Sxy150489 		*lbmp = Adapter->loopback_mode;
45023526Sxy150489 		break;
45033526Sxy150489 
45043526Sxy150489 	case LB_SET_MODE:
45053526Sxy150489 		size = 0;
45063526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
45073526Sxy150489 			return (IOC_INVAL);
45083526Sxy150489 
4509*7426SChenliang.Xu@Sun.COM 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
45103526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
45113526Sxy150489 			return (IOC_INVAL);
45123526Sxy150489 		break;
45133526Sxy150489 	}
45143526Sxy150489 
45153526Sxy150489 	iocp->ioc_count = size;
45163526Sxy150489 	iocp->ioc_error = 0;
45173526Sxy150489 
45185273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
45195273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
45205273Sgl147354 		return (IOC_INVAL);
45215273Sgl147354 	}
45225273Sgl147354 
45233526Sxy150489 	return (IOC_REPLY);
45243526Sxy150489 }
45253526Sxy150489 
45263526Sxy150489 static boolean_t
45277133Scc210113 e1000g_check_loopback_support(struct e1000_hw *hw)
45287133Scc210113 {
45297133Scc210113 	switch (hw->mac.type) {
45307133Scc210113 	case e1000_82540:
45317133Scc210113 	case e1000_82545:
45327133Scc210113 	case e1000_82545_rev_3:
45337133Scc210113 	case e1000_82546:
45347133Scc210113 	case e1000_82546_rev_3:
45357133Scc210113 	case e1000_82541:
45367133Scc210113 	case e1000_82541_rev_2:
45377133Scc210113 	case e1000_82547:
45387133Scc210113 	case e1000_82547_rev_2:
45397133Scc210113 	case e1000_82571:
45407133Scc210113 	case e1000_82572:
45417133Scc210113 	case e1000_82573:
45427133Scc210113 	case e1000_80003es2lan:
45437133Scc210113 		return (B_TRUE);
45447133Scc210113 	}
45457133Scc210113 	return (B_FALSE);
45467133Scc210113 }
45477133Scc210113 
45487133Scc210113 static boolean_t
45493526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
45503526Sxy150489 {
45513526Sxy150489 	struct e1000_hw *hw;
45523526Sxy150489 	int i, times;
45535082Syy150190 	boolean_t link_up;
45543526Sxy150489 
45553526Sxy150489 	if (mode == Adapter->loopback_mode)
45563526Sxy150489 		return (B_TRUE);
45573526Sxy150489 
45584919Sxy150489 	hw = &Adapter->shared;
45593526Sxy150489 	times = 0;
45603526Sxy150489 
45615082Syy150190 	Adapter->loopback_mode = mode;
45625082Syy150190 
45635082Syy150190 	if (mode == E1000G_LB_NONE) {
45643526Sxy150489 		/* Reset the chip */
45656735Scc210113 		hw->phy.autoneg_wait_to_complete = B_TRUE;
45663526Sxy150489 		(void) e1000g_reset(Adapter);
45676735Scc210113 		hw->phy.autoneg_wait_to_complete = B_FALSE;
45685082Syy150190 		return (B_TRUE);
45695082Syy150190 	}
45705082Syy150190 
45715082Syy150190 again:
45725082Syy150190 
45735082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
45745082Syy150190 
45755082Syy150190 	switch (mode) {
45765082Syy150190 	default:
45775082Syy150190 		rw_exit(&Adapter->chip_lock);
45785082Syy150190 		return (B_FALSE);
45793526Sxy150489 
45803526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
45813526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
45823526Sxy150489 		break;
45833526Sxy150489 
45843526Sxy150489 	case E1000G_LB_EXTERNAL_100:
45853526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
45863526Sxy150489 		break;
45873526Sxy150489 
45883526Sxy150489 	case E1000G_LB_EXTERNAL_10:
45893526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
45903526Sxy150489 		break;
45913526Sxy150489 
45923526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
45933526Sxy150489 		e1000g_set_internal_loopback(Adapter);
45943526Sxy150489 		break;
45953526Sxy150489 	}
45963526Sxy150489 
45973526Sxy150489 	times++;
45983526Sxy150489 
45995858Scc210113 	rw_exit(&Adapter->chip_lock);
46005858Scc210113 
46015082Syy150190 	/* Wait for link up */
46025082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
46035082Syy150190 		msec_delay(100);
46045082Syy150190 
46055858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
46065858Scc210113 
46075082Syy150190 	link_up = e1000g_link_up(Adapter);
46085082Syy150190 
46095082Syy150190 	rw_exit(&Adapter->chip_lock);
46105082Syy150190 
46115082Syy150190 	if (!link_up) {
46125082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46135082Syy150190 		    "Failed to get the link up");
46145082Syy150190 		if (times < 2) {
46155082Syy150190 			/* Reset the link */
46164919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
46175082Syy150190 			    "Reset the link ...");
46185082Syy150190 			(void) e1000g_reset(Adapter);
46195082Syy150190 			goto again;
46203526Sxy150489 		}
46213526Sxy150489 	}
46223526Sxy150489 
46233526Sxy150489 	return (B_TRUE);
46243526Sxy150489 }
46253526Sxy150489 
46263526Sxy150489 /*
46273526Sxy150489  * The following loopback settings are from Intel's technical
46283526Sxy150489  * document - "How To Loopback". All the register settings and
46293526Sxy150489  * time delay values are directly inherited from the document
46303526Sxy150489  * without more explanations available.
46313526Sxy150489  */
46323526Sxy150489 static void
46333526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
46343526Sxy150489 {
46353526Sxy150489 	struct e1000_hw *hw;
46363526Sxy150489 	uint32_t ctrl;
46373526Sxy150489 	uint32_t status;
46383526Sxy150489 	uint16_t phy_ctrl;
46395082Syy150190 	uint32_t txcw;
46403526Sxy150489 
46414919Sxy150489 	hw = &Adapter->shared;
46423526Sxy150489 
46433526Sxy150489 	/* Disable Smart Power Down */
46443526Sxy150489 	phy_spd_state(hw, B_FALSE);
46453526Sxy150489 
4646*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
46473526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
46483526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
46493526Sxy150489 
46504919Sxy150489 	switch (hw->mac.type) {
46513526Sxy150489 	case e1000_82540:
46523526Sxy150489 	case e1000_82545:
46533526Sxy150489 	case e1000_82545_rev_3:
46543526Sxy150489 	case e1000_82546:
46553526Sxy150489 	case e1000_82546_rev_3:
46563526Sxy150489 	case e1000_82573:
46573526Sxy150489 		/* Auto-MDI/MDIX off */
4658*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
46593526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
4660*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
46614349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
46623526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
4663*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
46644349Sxy150489 		    phy_ctrl | MII_CR_RESET);
46655082Syy150190 		/*
46665082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
46675082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
46685082Syy150190 		 * background.
46695082Syy150190 		 */
4670*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
4671*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
4672*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
4673*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
46743526Sxy150489 		break;
46757133Scc210113 	case e1000_80003es2lan:
46767133Scc210113 		/* Force Link Up */
4677*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
4678*7426SChenliang.Xu@Sun.COM 		    0x1CC);
46797133Scc210113 		/* Sets PCS loopback at 1Gbs */
4680*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
4681*7426SChenliang.Xu@Sun.COM 		    0x1046);
46827133Scc210113 		break;
46833526Sxy150489 	}
46843526Sxy150489 
46853526Sxy150489 	/* Set loopback */
4686*7426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
46873526Sxy150489 
46883526Sxy150489 	msec_delay(250);
46893526Sxy150489 
46903526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
46914919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
46923526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
46933526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
46944349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
46954349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
46964349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
46973526Sxy150489 
46984919Sxy150489 	switch (hw->mac.type) {
46993526Sxy150489 	case e1000_82540:
47003526Sxy150489 	case e1000_82545:
47013526Sxy150489 	case e1000_82545_rev_3:
47023526Sxy150489 	case e1000_82546:
47033526Sxy150489 	case e1000_82546_rev_3:
47043526Sxy150489 		/*
47053526Sxy150489 		 * For some serdes we'll need to commit the writes now
47063526Sxy150489 		 * so that the status is updated on link
47073526Sxy150489 		 */
47086735Scc210113 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
47094919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47103526Sxy150489 			msec_delay(100);
47114919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
47123526Sxy150489 		}
47133526Sxy150489 
47146735Scc210113 		if (hw->phy.media_type == e1000_media_type_copper) {
47153526Sxy150489 			/* Invert Loss of Signal */
47163526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
47173526Sxy150489 		} else {
47183526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
47194919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47203526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
47213526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
47223526Sxy150489 		}
47237133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47243526Sxy150489 		break;
47253526Sxy150489 
47263526Sxy150489 	case e1000_82571:
47273526Sxy150489 	case e1000_82572:
47285082Syy150190 		/*
47295082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
47305082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
47315082Syy150190 		 * using SerDes analog loopback.
47325082Syy150190 		 */
47336735Scc210113 		if (hw->phy.media_type != e1000_media_type_copper) {
47344919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
47355082Syy150190 			/* Set ILOS on fiber nic if half duplex is detected */
47365082Syy150190 			if (((status & E1000_STATUS_LU) == 0) ||
47375082Syy150190 			    ((status & E1000_STATUS_FD) == 0) ||
47386735Scc210113 			    (hw->phy.media_type ==
47395082Syy150190 			    e1000_media_type_internal_serdes))
47403526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
47415082Syy150190 
47425082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
47435082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
47445082Syy150190 			txcw &= ~((uint32_t)1 << 31);
47455082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
47465082Syy150190 
47475082Syy150190 			/*
47485082Syy150190 			 * Write 0x410 to Serdes Control register
47495082Syy150190 			 * to enable Serdes analog loopback
47505082Syy150190 			 */
47515082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
47525082Syy150190 			msec_delay(10);
47533526Sxy150489 		}
47547133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47553526Sxy150489 		break;
47563526Sxy150489 
47573526Sxy150489 	case e1000_82573:
47583526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
47597133Scc210113 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47603526Sxy150489 		break;
47613526Sxy150489 	}
47623526Sxy150489 }
47633526Sxy150489 
47643526Sxy150489 static void
47653526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
47663526Sxy150489 {
47673526Sxy150489 	struct e1000_hw *hw;
47683526Sxy150489 	uint32_t rctl;
47693526Sxy150489 	uint32_t ctrl_ext;
47703526Sxy150489 	uint32_t ctrl;
47713526Sxy150489 	uint32_t status;
47723526Sxy150489 	uint32_t txcw;
47737133Scc210113 	uint16_t phydata;
47743526Sxy150489 
47754919Sxy150489 	hw = &Adapter->shared;
47763526Sxy150489 
47773526Sxy150489 	/* Disable Smart Power Down */
47783526Sxy150489 	phy_spd_state(hw, B_FALSE);
47793526Sxy150489 
47807133Scc210113 	switch (hw->mac.type) {
47817133Scc210113 	case e1000_82571:
47827133Scc210113 	case e1000_82572:
47837133Scc210113 		switch (hw->phy.media_type) {
47847133Scc210113 		case e1000_media_type_copper:
47857133Scc210113 			/* Force link up (Must be done before the PHY writes) */
47867133Scc210113 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
47877133Scc210113 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
47887133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
47897133Scc210113 
47907133Scc210113 			rctl = E1000_READ_REG(hw, E1000_RCTL);
47917133Scc210113 			rctl |= (E1000_RCTL_EN |
47927133Scc210113 			    E1000_RCTL_SBP |
47937133Scc210113 			    E1000_RCTL_UPE |
47947133Scc210113 			    E1000_RCTL_MPE |
47957133Scc210113 			    E1000_RCTL_LPE |
47967133Scc210113 			    E1000_RCTL_BAM);		/* 0x803E */
47977133Scc210113 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
47987133Scc210113 
47997133Scc210113 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
48007133Scc210113 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
48017133Scc210113 			    E1000_CTRL_EXT_SDP6_DATA |
48027133Scc210113 			    E1000_CTRL_EXT_SDP7_DATA |
48037133Scc210113 			    E1000_CTRL_EXT_SDP4_DIR |
48047133Scc210113 			    E1000_CTRL_EXT_SDP6_DIR |
48057133Scc210113 			    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
48067133Scc210113 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
48077133Scc210113 
48087133Scc210113 			/*
48097133Scc210113 			 * This sequence tunes the PHY's SDP and no customer
48107133Scc210113 			 * settable values. For background, see comments above
48117133Scc210113 			 * e1000g_set_internal_loopback().
48127133Scc210113 			 */
4813*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
48147133Scc210113 			msec_delay(10);
4815*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
4816*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
4817*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
4818*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
4819*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
4820*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
4821*7426SChenliang.Xu@Sun.COM 
4822*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
4823*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
4824*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
4825*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
4826*7426SChenliang.Xu@Sun.COM 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
48277133Scc210113 
48287133Scc210113 			msec_delay(50);
48297133Scc210113 			break;
48307133Scc210113 		case e1000_media_type_fiber:
48317133Scc210113 		case e1000_media_type_internal_serdes:
48327133Scc210113 			status = E1000_READ_REG(hw, E1000_STATUS);
48337133Scc210113 			if (((status & E1000_STATUS_LU) == 0) ||
48347133Scc210113 			    (hw->phy.media_type ==
48357133Scc210113 			    e1000_media_type_internal_serdes)) {
48367133Scc210113 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
48377133Scc210113 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
48387133Scc210113 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
48397133Scc210113 			}
48407133Scc210113 
48417133Scc210113 			/* Disable autoneg by setting bit 31 of TXCW to zero */
48427133Scc210113 			txcw = E1000_READ_REG(hw, E1000_TXCW);
48437133Scc210113 			txcw &= ~((uint32_t)1 << 31);
48447133Scc210113 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
48457133Scc210113 
48467133Scc210113 			/*
48477133Scc210113 			 * Write 0x410 to Serdes Control register
48487133Scc210113 			 * to enable Serdes analog loopback
48497133Scc210113 			 */
48507133Scc210113 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
48517133Scc210113 			msec_delay(10);
48527133Scc210113 			break;
48537133Scc210113 		default:
48547133Scc210113 			break;
48557133Scc210113 		}
48563526Sxy150489 		break;
48577133Scc210113 	case e1000_80003es2lan:
4858*7426SChenliang.Xu@Sun.COM 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
4859*7426SChenliang.Xu@Sun.COM 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
4860*7426SChenliang.Xu@Sun.COM 		    phydata | (1 << 5));
48617133Scc210113 		Adapter->param_adv_autoneg = 1;
48627133Scc210113 		Adapter->param_adv_1000fdx = 1;
4863*7426SChenliang.Xu@Sun.COM 		(void) e1000g_reset_link(Adapter);
48643526Sxy150489 		break;
48653526Sxy150489 	}
48663526Sxy150489 }
48673526Sxy150489 
48683526Sxy150489 static void
48693526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
48703526Sxy150489 {
48713526Sxy150489 	struct e1000_hw *hw;
48723526Sxy150489 	uint32_t ctrl;
48733526Sxy150489 	uint16_t phy_ctrl;
48743526Sxy150489 
48754919Sxy150489 	hw = &Adapter->shared;
48763526Sxy150489 
48773526Sxy150489 	/* Disable Smart Power Down */
48783526Sxy150489 	phy_spd_state(hw, B_FALSE);
48793526Sxy150489 
48803526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
48814349Sxy150489 	    MII_CR_SPEED_100);
48823526Sxy150489 
48833526Sxy150489 	/* Force 100/FD, reset PHY */
4884*7426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
48854349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
48863526Sxy150489 	msec_delay(10);
48873526Sxy150489 
48883526Sxy150489 	/* Force 100/FD */
4889*7426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
48904349Sxy150489 	    phy_ctrl);			/* 0x2100 */
48913526Sxy150489 	msec_delay(10);
48923526Sxy150489 
48933526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
48944919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
48953526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
48963526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
48974349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
48984349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
48994349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
49004349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49013526Sxy150489 
49024919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49033526Sxy150489 }
49043526Sxy150489 
49053526Sxy150489 static void
49063526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
49073526Sxy150489 {
49083526Sxy150489 	struct e1000_hw *hw;
49093526Sxy150489 	uint32_t ctrl;
49103526Sxy150489 	uint16_t phy_ctrl;
49113526Sxy150489 
49124919Sxy150489 	hw = &Adapter->shared;
49133526Sxy150489 
49143526Sxy150489 	/* Disable Smart Power Down */
49153526Sxy150489 	phy_spd_state(hw, B_FALSE);
49163526Sxy150489 
49173526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
49184349Sxy150489 	    MII_CR_SPEED_10);
49193526Sxy150489 
49203526Sxy150489 	/* Force 10/FD, reset PHY */
4921*7426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49224349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
49233526Sxy150489 	msec_delay(10);
49243526Sxy150489 
49253526Sxy150489 	/* Force 10/FD */
4926*7426SChenliang.Xu@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
49274349Sxy150489 	    phy_ctrl);			/* 0x0100 */
49283526Sxy150489 	msec_delay(10);
49293526Sxy150489 
49303526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
49314919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
49323526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
49333526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
49344349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
49354349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
49364349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
49374349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
49383526Sxy150489 
49394919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
49403526Sxy150489 }
49413526Sxy150489 
49423526Sxy150489 #ifdef __sparc
49433526Sxy150489 static boolean_t
49443526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
49453526Sxy150489 {
49464919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
49473526Sxy150489 	uchar_t *bytes;
49483526Sxy150489 	struct ether_addr sysaddr;
49493526Sxy150489 	uint_t nelts;
49503526Sxy150489 	int err;
49513526Sxy150489 	boolean_t found = B_FALSE;
49523526Sxy150489 
49533526Sxy150489 	/*
49543526Sxy150489 	 * The "vendor's factory-set address" may already have
49553526Sxy150489 	 * been extracted from the chip, but if the property
49563526Sxy150489 	 * "local-mac-address" is set we use that instead.
49573526Sxy150489 	 *
49583526Sxy150489 	 * We check whether it looks like an array of 6
49593526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
49603526Sxy150489 	 * make sense of it this way, we'll ignore it.
49613526Sxy150489 	 */
49623526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
49633526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
49643526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
49653526Sxy150489 		if (nelts == ETHERADDRL) {
49663526Sxy150489 			while (nelts--)
49674919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
49683526Sxy150489 			found = B_TRUE;
49693526Sxy150489 		}
49703526Sxy150489 		ddi_prop_free(bytes);
49713526Sxy150489 	}
49723526Sxy150489 
49733526Sxy150489 	/*
49743526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
49753526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
49763526Sxy150489 	 */
49773526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
49783526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
49793526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
49803526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
49814919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
49823526Sxy150489 				found = B_TRUE;
49833526Sxy150489 			}
49843526Sxy150489 		}
49853526Sxy150489 		ddi_prop_free(bytes);
49863526Sxy150489 	}
49873526Sxy150489 
49883526Sxy150489 	/*
49893526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
49903526Sxy150489 	 * if we netbooted from this interface), we must use this instead
49913526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
49923526Sxy150489 	 * get confused by the address changing as Solaris takes over!
49933526Sxy150489 	 */
49943526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
49953526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
49963526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
49973526Sxy150489 		if (nelts == ETHERADDRL) {
49983526Sxy150489 			while (nelts--)
49994919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
50003526Sxy150489 			found = B_TRUE;
50013526Sxy150489 		}
50023526Sxy150489 		ddi_prop_free(bytes);
50033526Sxy150489 	}
50043526Sxy150489 
50053526Sxy150489 	if (found) {
50064919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
50073526Sxy150489 		    ETHERADDRL);
50083526Sxy150489 	}
50093526Sxy150489 
50103526Sxy150489 	return (found);
50113526Sxy150489 }
50123526Sxy150489 #endif
50133526Sxy150489 
50143526Sxy150489 static int
50153526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
50163526Sxy150489 {
50173526Sxy150489 	dev_info_t *devinfo;
50183526Sxy150489 	int intr_types;
50193526Sxy150489 	int rc;
50203526Sxy150489 
50213526Sxy150489 	devinfo = Adapter->dip;
50223526Sxy150489 
50233526Sxy150489 	/* Get supported interrupt types */
50243526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
50253526Sxy150489 
50263526Sxy150489 	if (rc != DDI_SUCCESS) {
50274919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
50283526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
50293526Sxy150489 		return (DDI_FAILURE);
50303526Sxy150489 	}
50313526Sxy150489 
50323526Sxy150489 	/*
50333526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
50343526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
50353526Sxy150489 	 * that it supports MSI, but in fact has problems.
50363526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
50373526Sxy150489 	 * PCI/PCI-X NICs.
50383526Sxy150489 	 */
50394919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
50406986Smx205022 		Adapter->msi_enable = B_FALSE;
50416986Smx205022 
50426986Smx205022 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
50433526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
50443526Sxy150489 
50453526Sxy150489 		if (rc != DDI_SUCCESS) {
50464919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50473526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
50483526Sxy150489 		} else {
50493526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
50503526Sxy150489 		}
50513526Sxy150489 	}
50523526Sxy150489 
50533526Sxy150489 	if ((Adapter->intr_type == 0) &&
50543526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
50553526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
50563526Sxy150489 
50573526Sxy150489 		if (rc != DDI_SUCCESS) {
50584919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50593526Sxy150489 			    "Add Legacy interrupts failed\n");
50603526Sxy150489 			return (DDI_FAILURE);
50613526Sxy150489 		}
50623526Sxy150489 
50633526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
50643526Sxy150489 	}
50653526Sxy150489 
50663526Sxy150489 	if (Adapter->intr_type == 0) {
50674919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
50683526Sxy150489 		    "No interrupts registered\n");
50693526Sxy150489 		return (DDI_FAILURE);
50703526Sxy150489 	}
50713526Sxy150489 
50723526Sxy150489 	return (DDI_SUCCESS);
50733526Sxy150489 }
50743526Sxy150489 
50753526Sxy150489 /*
50763526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
50773526Sxy150489  */
50783526Sxy150489 static int
50793526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
50803526Sxy150489 {
50813526Sxy150489 	dev_info_t *devinfo;
50823526Sxy150489 	int count, avail, actual;
50833526Sxy150489 	int x, y, rc, inum = 0;
50843526Sxy150489 	int flag;
50853526Sxy150489 	ddi_intr_handler_t *intr_handler;
50863526Sxy150489 
50873526Sxy150489 	devinfo = Adapter->dip;
50883526Sxy150489 
50893526Sxy150489 	/* get number of interrupts */
50903526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
50913526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
50924919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
50933526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
50943526Sxy150489 		    rc, count);
50953526Sxy150489 		return (DDI_FAILURE);
50963526Sxy150489 	}
50973526Sxy150489 
50983526Sxy150489 	/* get number of available interrupts */
50993526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
51003526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
51014919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51023526Sxy150489 		    "Get interrupt available number failed. "
51033526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
51043526Sxy150489 		return (DDI_FAILURE);
51053526Sxy150489 	}
51063526Sxy150489 
51073526Sxy150489 	if (avail < count) {
51084919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51093526Sxy150489 		    "Interrupts count: %d, available: %d\n",
51103526Sxy150489 		    count, avail);
51113526Sxy150489 	}
51123526Sxy150489 
51133526Sxy150489 	/* Allocate an array of interrupt handles */
51143526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
51153526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
51163526Sxy150489 
51173526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
51183526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
51193526Sxy150489 
51203526Sxy150489 	/* call ddi_intr_alloc() */
51213526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
51223526Sxy150489 	    count, &actual, flag);
51233526Sxy150489 
51243526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
51254919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51263526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
51273526Sxy150489 
51283526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
51293526Sxy150489 		return (DDI_FAILURE);
51303526Sxy150489 	}
51313526Sxy150489 
51323526Sxy150489 	if (actual < count) {
51334919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
51343526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
51353526Sxy150489 		    count, actual);
51363526Sxy150489 	}
51373526Sxy150489 
51383526Sxy150489 	Adapter->intr_cnt = actual;
51393526Sxy150489 
51403526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
51413526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
51423526Sxy150489 
51433526Sxy150489 	if (rc != DDI_SUCCESS) {
51444919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51453526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
51463526Sxy150489 
51473526Sxy150489 		/* Free already allocated intr */
51483526Sxy150489 		for (y = 0; y < actual; y++)
51493526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
51503526Sxy150489 
51513526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
51523526Sxy150489 		return (DDI_FAILURE);
51533526Sxy150489 	}
51543526Sxy150489 
51553526Sxy150489 	/*
51563526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
51573526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
51583526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
51593526Sxy150489 	 * devices.
51603526Sxy150489 	 */
51614919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
51623526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
51633526Sxy150489 	else
51643526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
51653526Sxy150489 
51663526Sxy150489 	/* Call ddi_intr_add_handler() */
51673526Sxy150489 	for (x = 0; x < actual; x++) {
51683526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
51693526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
51703526Sxy150489 
51713526Sxy150489 		if (rc != DDI_SUCCESS) {
51724919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51733526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
51743526Sxy150489 
51753526Sxy150489 			/* Remove already added handler */
51763526Sxy150489 			for (y = 0; y < x; y++)
51773526Sxy150489 				(void) ddi_intr_remove_handler(
51783526Sxy150489 				    Adapter->htable[y]);
51793526Sxy150489 
51803526Sxy150489 			/* Free already allocated intr */
51813526Sxy150489 			for (y = 0; y < actual; y++)
51823526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
51833526Sxy150489 
51843526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
51853526Sxy150489 			return (DDI_FAILURE);
51863526Sxy150489 		}
51873526Sxy150489 	}
51883526Sxy150489 
51893526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
51903526Sxy150489 
51913526Sxy150489 	if (rc != DDI_SUCCESS) {
51924919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
51933526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
51943526Sxy150489 
51953526Sxy150489 		/* Free already allocated intr */
51963526Sxy150489 		for (y = 0; y < actual; y++) {
51973526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
51983526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
51993526Sxy150489 		}
52003526Sxy150489 
52013526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
52023526Sxy150489 		return (DDI_FAILURE);
52033526Sxy150489 	}
52043526Sxy150489 
52053526Sxy150489 	return (DDI_SUCCESS);
52063526Sxy150489 }
52073526Sxy150489 
52083526Sxy150489 static int
52093526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
52103526Sxy150489 {
52113526Sxy150489 	int x;
52123526Sxy150489 	int rc;
52133526Sxy150489 
52143526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
52153526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
52163526Sxy150489 		if (rc != DDI_SUCCESS) {
52174919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52183526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
52193526Sxy150489 			return (DDI_FAILURE);
52203526Sxy150489 		}
52213526Sxy150489 
52223526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
52233526Sxy150489 		if (rc != DDI_SUCCESS) {
52244919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52253526Sxy150489 			    "Free intr failed: %d\n", rc);
52263526Sxy150489 			return (DDI_FAILURE);
52273526Sxy150489 		}
52283526Sxy150489 	}
52293526Sxy150489 
52303526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
52313526Sxy150489 
52323526Sxy150489 	return (DDI_SUCCESS);
52333526Sxy150489 }
52343526Sxy150489 
52353526Sxy150489 static int
52363526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
52373526Sxy150489 {
52383526Sxy150489 	int x;
52393526Sxy150489 	int rc;
52403526Sxy150489 
52413526Sxy150489 	/* Enable interrupts */
52423526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
52433526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
52443526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
52453526Sxy150489 		    Adapter->intr_cnt);
52463526Sxy150489 		if (rc != DDI_SUCCESS) {
52474919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52483526Sxy150489 			    "Enable block intr failed: %d\n", rc);
52493526Sxy150489 			return (DDI_FAILURE);
52503526Sxy150489 		}
52513526Sxy150489 	} else {
52523526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
52533526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
52543526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
52553526Sxy150489 			if (rc != DDI_SUCCESS) {
52564919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52573526Sxy150489 				    "Enable intr failed: %d\n", rc);
52583526Sxy150489 				return (DDI_FAILURE);
52593526Sxy150489 			}
52603526Sxy150489 		}
52613526Sxy150489 	}
52623526Sxy150489 
52633526Sxy150489 	return (DDI_SUCCESS);
52643526Sxy150489 }
52653526Sxy150489 
52663526Sxy150489 static int
52673526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
52683526Sxy150489 {
52693526Sxy150489 	int x;
52703526Sxy150489 	int rc;
52713526Sxy150489 
52723526Sxy150489 	/* Disable all interrupts */
52733526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
52743526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
52753526Sxy150489 		    Adapter->intr_cnt);
52763526Sxy150489 		if (rc != DDI_SUCCESS) {
52774919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52783526Sxy150489 			    "Disable block intr failed: %d\n", rc);
52793526Sxy150489 			return (DDI_FAILURE);
52803526Sxy150489 		}
52813526Sxy150489 	} else {
52823526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
52833526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
52843526Sxy150489 			if (rc != DDI_SUCCESS) {
52854919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
52863526Sxy150489 				    "Disable intr failed: %d\n", rc);
52873526Sxy150489 				return (DDI_FAILURE);
52883526Sxy150489 			}
52893526Sxy150489 		}
52903526Sxy150489 	}
52913526Sxy150489 
52923526Sxy150489 	return (DDI_SUCCESS);
52933526Sxy150489 }
52945082Syy150190 
52955082Syy150190 /*
52965082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
52975082Syy150190  */
52985082Syy150190 static void
52995082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
53005082Syy150190 {
53015082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
53025082Syy150190 
5303*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
5304*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
5305*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
5306*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
5307*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
5308*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
5309*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5310*7426SChenliang.Xu@Sun.COM 	    &Adapter->phy_1000t_status);
5311*7426SChenliang.Xu@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
53126394Scc210113 
53136394Scc210113 	Adapter->param_autoneg_cap =
53146394Scc210113 	    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
53156394Scc210113 	Adapter->param_pause_cap =
53166394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
53176394Scc210113 	Adapter->param_asym_pause_cap =
53186394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
53196394Scc210113 	Adapter->param_1000fdx_cap =
53206394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
53216394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
53226394Scc210113 	Adapter->param_1000hdx_cap =
53236394Scc210113 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
53246394Scc210113 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
53256394Scc210113 	Adapter->param_100t4_cap =
53266394Scc210113 	    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
53276394Scc210113 	Adapter->param_100fdx_cap =
53286394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
53296394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
53306394Scc210113 	Adapter->param_100hdx_cap =
53316394Scc210113 	    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
53326394Scc210113 	    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
53336394Scc210113 	Adapter->param_10fdx_cap =
53346394Scc210113 	    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
53356394Scc210113 	Adapter->param_10hdx_cap =
53366394Scc210113 	    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
53376394Scc210113 
53386394Scc210113 	Adapter->param_adv_autoneg = hw->mac.autoneg;
53396394Scc210113 	Adapter->param_adv_pause =
53406394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
53416394Scc210113 	Adapter->param_adv_asym_pause =
53426394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
53436394Scc210113 	Adapter->param_adv_1000hdx =
53446394Scc210113 	    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
53456394Scc210113 	Adapter->param_adv_100t4 =
53466394Scc210113 	    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
53476394Scc210113 	if (Adapter->param_adv_autoneg == 1) {
53486394Scc210113 		Adapter->param_adv_1000fdx =
53496394Scc210113 		    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
53506394Scc210113 		Adapter->param_adv_100fdx =
53516394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
53526394Scc210113 		Adapter->param_adv_100hdx =
53536394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
53546394Scc210113 		Adapter->param_adv_10fdx =
53556394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
53566394Scc210113 		Adapter->param_adv_10hdx =
53576394Scc210113 		    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
53586394Scc210113 	}
53596394Scc210113 
53606394Scc210113 	Adapter->param_lp_autoneg =
53616394Scc210113 	    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
53626394Scc210113 	Adapter->param_lp_pause =
53636394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
53646394Scc210113 	Adapter->param_lp_asym_pause =
53656394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
53666394Scc210113 	Adapter->param_lp_1000fdx =
53676394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
53686394Scc210113 	Adapter->param_lp_1000hdx =
53696394Scc210113 	    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
53706394Scc210113 	Adapter->param_lp_100t4 =
53716394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
53726394Scc210113 	Adapter->param_lp_100fdx =
53736394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
53746394Scc210113 	Adapter->param_lp_100hdx =
53756394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
53766394Scc210113 	Adapter->param_lp_10fdx =
53776394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
53786394Scc210113 	Adapter->param_lp_10hdx =
53796394Scc210113 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
53805082Syy150190 }
53815273Sgl147354 
53825273Sgl147354 /*
53835273Sgl147354  * FMA support
53845273Sgl147354  */
53855273Sgl147354 
53865273Sgl147354 int
53875273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
53885273Sgl147354 {
53895273Sgl147354 	ddi_fm_error_t de;
53905273Sgl147354 
53915273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
53925273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
53935273Sgl147354 	return (de.fme_status);
53945273Sgl147354 }
53955273Sgl147354 
53965273Sgl147354 int
53975273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
53985273Sgl147354 {
53995273Sgl147354 	ddi_fm_error_t de;
54005273Sgl147354 
54015273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
54025273Sgl147354 	return (de.fme_status);
54035273Sgl147354 }
54045273Sgl147354 
54055273Sgl147354 /*
54065273Sgl147354  * The IO fault service error handling callback function
54075273Sgl147354  */
5408*7426SChenliang.Xu@Sun.COM /* ARGSUSED2 */
54095273Sgl147354 static int
54105273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
54115273Sgl147354 {
54125273Sgl147354 	/*
54135273Sgl147354 	 * as the driver can always deal with an error in any dma or
54145273Sgl147354 	 * access handle, we can just return the fme_status value.
54155273Sgl147354 	 */
54165273Sgl147354 	pci_ereport_post(dip, err, NULL);
54175273Sgl147354 	return (err->fme_status);
54185273Sgl147354 }
54195273Sgl147354 
54205273Sgl147354 static void
54215273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
54225273Sgl147354 {
54235273Sgl147354 	ddi_iblock_cookie_t iblk;
54245273Sgl147354 	int fma_acc_flag, fma_dma_flag;
54255273Sgl147354 
54265273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
54275273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
54285273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
54295273Sgl147354 		fma_acc_flag = 1;
54305273Sgl147354 	} else {
54315273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
54325273Sgl147354 		fma_acc_flag = 0;
54335273Sgl147354 	}
54345273Sgl147354 
54355273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
54365273Sgl147354 		fma_dma_flag = 1;
54375273Sgl147354 	} else {
54385273Sgl147354 		fma_dma_flag = 0;
54395273Sgl147354 	}
54405273Sgl147354 
54415273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
54425273Sgl147354 
54435273Sgl147354 	if (Adapter->fm_capabilities) {
54445273Sgl147354 
54455273Sgl147354 		/* Register capabilities with IO Fault Services */
54465273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
54475273Sgl147354 
54485273Sgl147354 		/*
54495273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
54505273Sgl147354 		 */
54515273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
54525273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54535273Sgl147354 			pci_ereport_setup(Adapter->dip);
54545273Sgl147354 
54555273Sgl147354 		/*
54565273Sgl147354 		 * Register error callback if error callback capable
54575273Sgl147354 		 */
54585273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54595273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
54605273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
54615273Sgl147354 	}
54625273Sgl147354 }
54635273Sgl147354 
54645273Sgl147354 static void
54655273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
54665273Sgl147354 {
54675273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
54685273Sgl147354 	if (Adapter->fm_capabilities) {
54695273Sgl147354 
54705273Sgl147354 		/*
54715273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
54725273Sgl147354 		 */
54735273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
54745273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54755273Sgl147354 			pci_ereport_teardown(Adapter->dip);
54765273Sgl147354 
54775273Sgl147354 		/*
54785273Sgl147354 		 * Un-register error callback if error callback capable
54795273Sgl147354 		 */
54805273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
54815273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
54825273Sgl147354 
54835273Sgl147354 		/* Unregister from IO Fault Services */
54845273Sgl147354 		ddi_fm_fini(Adapter->dip);
54855273Sgl147354 	}
54865273Sgl147354 }
54875273Sgl147354 
54885273Sgl147354 void
54895273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
54905273Sgl147354 {
54915273Sgl147354 	uint64_t ena;
54925273Sgl147354 	char buf[FM_MAX_CLASS];
54935273Sgl147354 
54945273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
54955273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
54965273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
54975273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
54985273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
54995273Sgl147354 	}
55005273Sgl147354 }
55016512Ssowmini 
55026512Ssowmini static int
55036512Ssowmini e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
55046512Ssowmini     uint_t pr_valsize, void *pr_val)
55056512Ssowmini {
55066512Ssowmini 	link_flowctrl_t fl;
55076512Ssowmini 	int err = 0;
55086512Ssowmini 
55096512Ssowmini 	ASSERT(pr_valsize > 0);
55106512Ssowmini 	switch (pr_num) {
55116789Sam223141 	case MAC_PROP_AUTONEG:
55126512Ssowmini 		*(uint8_t *)pr_val =
55136512Ssowmini 		    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0);
55146512Ssowmini 		break;
55156789Sam223141 	case MAC_PROP_FLOWCTRL:
55166512Ssowmini 		if (pr_valsize < sizeof (link_flowctrl_t))
55176512Ssowmini 			return (EINVAL);
55186512Ssowmini 		fl = LINK_FLOWCTRL_BI;
55196512Ssowmini 		bcopy(&fl, pr_val, sizeof (fl));
55206512Ssowmini 		break;
55216789Sam223141 	case MAC_PROP_ADV_1000FDX_CAP:
55226789Sam223141 	case MAC_PROP_EN_1000FDX_CAP:
55236512Ssowmini 		*(uint8_t *)pr_val =
55246512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
55256512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
55266512Ssowmini 		break;
55276789Sam223141 	case MAC_PROP_ADV_1000HDX_CAP:
55286789Sam223141 	case MAC_PROP_EN_1000HDX_CAP:
55296512Ssowmini 		*(uint8_t *)pr_val =
55306512Ssowmini 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
55316512Ssowmini 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
55326512Ssowmini 		break;
55336789Sam223141 	case MAC_PROP_ADV_100FDX_CAP:
55346789Sam223141 	case MAC_PROP_EN_100FDX_CAP:
55356512Ssowmini 		*(uint8_t *)pr_val =
55366512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
55376512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
5538*7426SChenliang.Xu@Sun.COM 		break;
55396789Sam223141 	case MAC_PROP_ADV_100HDX_CAP:
55406789Sam223141 	case MAC_PROP_EN_100HDX_CAP:
55416512Ssowmini 		*(uint8_t *)pr_val =
55426512Ssowmini 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
55436512Ssowmini 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
55446512Ssowmini 		break;
55456789Sam223141 	case MAC_PROP_ADV_10FDX_CAP:
55466789Sam223141 	case MAC_PROP_EN_10FDX_CAP:
55476512Ssowmini 		*(uint8_t *)pr_val =
55486512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
55496512Ssowmini 		break;
55506789Sam223141 	case MAC_PROP_ADV_10HDX_CAP:
55516789Sam223141 	case MAC_PROP_EN_10HDX_CAP:
55526512Ssowmini 		*(uint8_t *)pr_val =
55536512Ssowmini 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
55546512Ssowmini 		break;
55556512Ssowmini 	default:
55566512Ssowmini 		err = ENOTSUP;
55576512Ssowmini 		break;
55586512Ssowmini 	}
55596512Ssowmini 	return (err);
55606512Ssowmini }
55616512Ssowmini 
55626512Ssowmini /*
55636512Ssowmini  * synchronize the adv* and en* parameters.
55646512Ssowmini  *
55656512Ssowmini  * See comments in <sys/dld.h> for details of the *_en_*
55666512Ssowmini  * parameters. The usage of ndd for setting adv parameters will
55676512Ssowmini  * synchronize all the en parameters with the e1000g parameters,
55686512Ssowmini  * implicity disalbing any settings made via dladm.
55696512Ssowmini  */
55706512Ssowmini static void
55716512Ssowmini e1000g_param_sync(struct e1000g *Adapter)
55726512Ssowmini {
55736512Ssowmini 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
55746512Ssowmini 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
55756512Ssowmini 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
55766512Ssowmini 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
55776512Ssowmini 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
55786512Ssowmini 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
55796512Ssowmini }
5580