xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 5858:b2b39d034f5b)
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  *
93526Sxy150489  * Copyright(c) 1999 - 2007 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 /*
22*5858Scc210113  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
233526Sxy150489  * Use is subject to license terms of the CDDLv1.
243526Sxy150489  */
253526Sxy150489 
263526Sxy150489 #pragma ident	"%Z%%M%	%I%	%E% SMI"
273526Sxy150489 
283526Sxy150489 /*
293526Sxy150489  * **********************************************************************
303526Sxy150489  *									*
313526Sxy150489  * Module Name:								*
323526Sxy150489  *   e1000g_main.c							*
333526Sxy150489  *									*
343526Sxy150489  * Abstract:								*
354919Sxy150489  *   This file contains the interface routines for the solaris OS.	*
364919Sxy150489  *   It has all DDI entry point routines and GLD entry point routines.	*
373526Sxy150489  *									*
384919Sxy150489  *   This file also contains routines that take care of initialization	*
394919Sxy150489  *   uninit routine and interrupt routine.				*
403526Sxy150489  *									*
413526Sxy150489  * **********************************************************************
423526Sxy150489  */
433526Sxy150489 
443526Sxy150489 #include <sys/dlpi.h>
453526Sxy150489 #include <sys/mac.h>
463526Sxy150489 #include "e1000g_sw.h"
473526Sxy150489 #include "e1000g_debug.h"
483526Sxy150489 
493526Sxy150489 #define	E1000_RX_INTPT_TIME	128
503526Sxy150489 #define	E1000_RX_PKT_CNT	8
513526Sxy150489 
525273Sgl147354 static char ident[] = "Intel PRO/1000 Ethernet 5.2.3";
533526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
545273Sgl147354 static char e1000g_version[] = "Driver Ver. 5.2.3";
553526Sxy150489 
563526Sxy150489 /*
573526Sxy150489  * Proto types for DDI entry points
583526Sxy150489  */
594919Sxy150489 static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
604919Sxy150489 static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
613526Sxy150489 
623526Sxy150489 /*
633526Sxy150489  * init and intr routines prototype
643526Sxy150489  */
654919Sxy150489 static int e1000g_resume(dev_info_t *);
664919Sxy150489 static int e1000g_suspend(dev_info_t *);
673526Sxy150489 static uint_t e1000g_intr_pciexpress(caddr_t);
683526Sxy150489 static uint_t e1000g_intr(caddr_t);
693526Sxy150489 static void e1000g_intr_work(struct e1000g *, uint32_t);
703526Sxy150489 #pragma inline(e1000g_intr_work)
713526Sxy150489 static int e1000g_init(struct e1000g *);
724919Sxy150489 static int e1000g_start(struct e1000g *, boolean_t);
734919Sxy150489 static void e1000g_stop(struct e1000g *, boolean_t);
743526Sxy150489 static int e1000g_m_start(void *);
753526Sxy150489 static void e1000g_m_stop(void *);
763526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
773526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
783526Sxy150489 static int e1000g_m_unicst(void *, const uint8_t *);
793526Sxy150489 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
803526Sxy150489 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
813526Sxy150489 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
823526Sxy150489 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
833526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
843526Sxy150489 static void e1000g_m_blank(void *, time_t, uint32_t);
853526Sxy150489 static void e1000g_m_resources(void *);
863526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
874919Sxy150489 static void e1000g_init_locks(struct e1000g *);
884919Sxy150489 static void e1000g_destroy_locks(struct e1000g *);
894919Sxy150489 static int e1000g_identify_hardware(struct e1000g *);
904919Sxy150489 static int e1000g_regs_map(struct e1000g *);
914919Sxy150489 static int e1000g_set_driver_params(struct e1000g *);
924919Sxy150489 static int e1000g_register_mac(struct e1000g *);
934919Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *);
944919Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *);
954919Sxy150489 static void e1000g_init_unicst(struct e1000g *);
963526Sxy150489 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
973526Sxy150489 
983526Sxy150489 /*
993526Sxy150489  * Local routines
1003526Sxy150489  */
1014919Sxy150489 static void e1000g_tx_clean(struct e1000g *);
1024919Sxy150489 static void e1000g_rx_clean(struct e1000g *);
1034061Sxy150489 static void e1000g_link_timer(void *);
1044919Sxy150489 static void e1000g_local_timer(void *);
1054061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1063526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1073526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1084919Sxy150489 static void e1000g_get_conf(struct e1000g *);
1094919Sxy150489 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
1104919Sxy150489 static void enable_watchdog_timer(struct e1000g *);
1114919Sxy150489 static void disable_watchdog_timer(struct e1000g *);
1124919Sxy150489 static void start_watchdog_timer(struct e1000g *);
1134919Sxy150489 static void restart_watchdog_timer(struct e1000g *);
1144919Sxy150489 static void stop_watchdog_timer(struct e1000g *);
1154919Sxy150489 static void stop_link_timer(struct e1000g *);
1164919Sxy150489 static void stop_82547_timer(e1000g_tx_ring_t *);
1174919Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *);
1184919Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *);
1194919Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
1203526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1214919Sxy150489 #ifdef E1000G_DEBUG
1224919Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
1234919Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
1244919Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
1254919Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
1264919Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
1274919Sxy150489     struct iocblk *, mblk_t *);
1284919Sxy150489 #endif
1294919Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
1304919Sxy150489     struct iocblk *, mblk_t *);
1314919Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
1324919Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *);
1334919Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *);
1344919Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *);
1354919Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *);
1364919Sxy150489 static int e1000g_add_intrs(struct e1000g *);
1374919Sxy150489 static int e1000g_intr_add(struct e1000g *, int);
1384919Sxy150489 static int e1000g_rem_intrs(struct e1000g *);
1394919Sxy150489 static int e1000g_enable_intrs(struct e1000g *);
1404919Sxy150489 static int e1000g_disable_intrs(struct e1000g *);
1414919Sxy150489 static boolean_t e1000g_link_up(struct e1000g *);
1423526Sxy150489 #ifdef __sparc
1434919Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *);
1443526Sxy150489 #endif
1455082Syy150190 static void e1000g_get_phy_state(struct e1000g *);
1464982Syy150190 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
1475273Sgl147354 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
1485273Sgl147354     const void *impl_data);
1495273Sgl147354 static void e1000g_fm_init(struct e1000g *Adapter);
1505273Sgl147354 static void e1000g_fm_fini(struct e1000g *Adapter);
1513526Sxy150489 
1523526Sxy150489 static struct cb_ops cb_ws_ops = {
1533526Sxy150489 	nulldev,		/* cb_open */
1543526Sxy150489 	nulldev,		/* cb_close */
1553526Sxy150489 	nodev,			/* cb_strategy */
1563526Sxy150489 	nodev,			/* cb_print */
1573526Sxy150489 	nodev,			/* cb_dump */
1583526Sxy150489 	nodev,			/* cb_read */
1593526Sxy150489 	nodev,			/* cb_write */
1603526Sxy150489 	nodev,			/* cb_ioctl */
1613526Sxy150489 	nodev,			/* cb_devmap */
1623526Sxy150489 	nodev,			/* cb_mmap */
1633526Sxy150489 	nodev,			/* cb_segmap */
1643526Sxy150489 	nochpoll,		/* cb_chpoll */
1653526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1663526Sxy150489 	NULL,			/* cb_stream */
1673526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1683526Sxy150489 	CB_REV,			/* cb_rev */
1693526Sxy150489 	nodev,			/* cb_aread */
1703526Sxy150489 	nodev			/* cb_awrite */
1713526Sxy150489 };
1723526Sxy150489 
1733526Sxy150489 static struct dev_ops ws_ops = {
1743526Sxy150489 	DEVO_REV,		/* devo_rev */
1753526Sxy150489 	0,			/* devo_refcnt */
1763526Sxy150489 	NULL,			/* devo_getinfo */
1773526Sxy150489 	nulldev,		/* devo_identify */
1783526Sxy150489 	nulldev,		/* devo_probe */
1794919Sxy150489 	e1000g_attach,		/* devo_attach */
1804919Sxy150489 	e1000g_detach,		/* devo_detach */
1813526Sxy150489 	nodev,			/* devo_reset */
1823526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
1833526Sxy150489 	NULL,			/* devo_bus_ops */
1843526Sxy150489 	ddi_power		/* devo_power */
1853526Sxy150489 };
1863526Sxy150489 
1873526Sxy150489 static struct modldrv modldrv = {
1883526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1893526Sxy150489 	ident,			/* Discription string */
1903526Sxy150489 	&ws_ops,		/* driver ops */
1913526Sxy150489 };
1923526Sxy150489 
1933526Sxy150489 static struct modlinkage modlinkage = {
1943526Sxy150489 	MODREV_1, &modldrv, NULL
1953526Sxy150489 };
1963526Sxy150489 
1974919Sxy150489 /* Access attributes for register mapping */
1984919Sxy150489 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
1993526Sxy150489 	DDI_DEVICE_ATTR_V0,
2003526Sxy150489 	DDI_STRUCTURE_LE_ACC,
2013526Sxy150489 	DDI_STRICTORDER_ACC,
2025273Sgl147354 	DDI_FLAGERR_ACC
2033526Sxy150489 };
2043526Sxy150489 
2053526Sxy150489 #define	E1000G_M_CALLBACK_FLAGS	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB)
2063526Sxy150489 
2073526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2083526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2093526Sxy150489 	e1000g_m_stat,
2103526Sxy150489 	e1000g_m_start,
2113526Sxy150489 	e1000g_m_stop,
2123526Sxy150489 	e1000g_m_promisc,
2133526Sxy150489 	e1000g_m_multicst,
2143526Sxy150489 	e1000g_m_unicst,
2153526Sxy150489 	e1000g_m_tx,
2163526Sxy150489 	e1000g_m_resources,
2173526Sxy150489 	e1000g_m_ioctl,
2183526Sxy150489 	e1000g_m_getcapab
2193526Sxy150489 };
2203526Sxy150489 
2213526Sxy150489 /*
2223526Sxy150489  * Global variables
2233526Sxy150489  */
2244982Syy150190 
2253526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2263526Sxy150489 /*
2274894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2284349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2294349Sxy150489  * enabled. If we force the driver to detach while there are still some
2304349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2314349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2324349Sxy150489  * structure will be freed after the driver is detached. However when we
2334349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2344349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
2354894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
2364894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
2374894Syy150190  * after the system board level DR operation. For this reason, the global
2384894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2394349Sxy150489  */
2404894Syy150190 #ifdef __sparc
2414894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
2424894Syy150190 #else
2434894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
2444894Syy150190 #endif
2454349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
2464894Syy150190 
2474349Sxy150489 /*
2483526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2493526Sxy150489  * and the rx packets release in detach processing to make them mutually
2503526Sxy150489  * exclusive.
2513526Sxy150489  * The rx recycling processes different rx packets in different threads,
2523526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2533526Sxy150489  * recycling threads.
2543526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2553526Sxy150489  * it mutually exclusive with the rx recycling.
2563526Sxy150489  */
2573526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2583526Sxy150489 /*
2593526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2603526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2613526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2623526Sxy150489  * resources during the initialization of the instances, then the flag will
2633526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2643526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2653526Sxy150489  */
2663526Sxy150489 krwlock_t e1000g_dma_type_lock;
2673526Sxy150489 
2683526Sxy150489 
2693526Sxy150489 /*
2703526Sxy150489  * Loadable module configuration entry points for the driver
2713526Sxy150489  */
2723526Sxy150489 
2733526Sxy150489 /*
2744919Sxy150489  * _init - module initialization
2753526Sxy150489  */
2763526Sxy150489 int
2773526Sxy150489 _init(void)
2783526Sxy150489 {
2793526Sxy150489 	int status;
2803526Sxy150489 
2813526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
2823526Sxy150489 	status = mod_install(&modlinkage);
2833526Sxy150489 	if (status != DDI_SUCCESS)
2843526Sxy150489 		mac_fini_ops(&ws_ops);
2853526Sxy150489 	else {
2863526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
2873526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
2883526Sxy150489 	}
2893526Sxy150489 
2903526Sxy150489 	return (status);
2913526Sxy150489 }
2923526Sxy150489 
2933526Sxy150489 /*
2944919Sxy150489  * _fini - module finalization
2953526Sxy150489  */
2963526Sxy150489 int
2973526Sxy150489 _fini(void)
2983526Sxy150489 {
2993526Sxy150489 	int status;
3003526Sxy150489 
3013526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3023526Sxy150489 	if (e1000g_mblks_pending != 0) {
3033526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3043526Sxy150489 		return (EBUSY);
3053526Sxy150489 	}
3063526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3073526Sxy150489 
3083526Sxy150489 	status = mod_remove(&modlinkage);
3093526Sxy150489 	if (status == DDI_SUCCESS) {
3103526Sxy150489 		mac_fini_ops(&ws_ops);
3114349Sxy150489 
3124349Sxy150489 		if (e1000g_force_detach) {
3134349Sxy150489 			private_devi_list_t *devi_node;
3144349Sxy150489 
3154349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3164349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3174349Sxy150489 				devi_node = e1000g_private_devi_list;
3184349Sxy150489 				e1000g_private_devi_list =
3194349Sxy150489 				    e1000g_private_devi_list->next;
3204349Sxy150489 
3214349Sxy150489 				kmem_free(devi_node->priv_dip,
3224349Sxy150489 				    sizeof (struct dev_info));
3234349Sxy150489 				kmem_free(devi_node,
3244349Sxy150489 				    sizeof (private_devi_list_t));
3254349Sxy150489 			}
3264349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3274349Sxy150489 		}
3284349Sxy150489 
3293526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3303526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3313526Sxy150489 	}
3323526Sxy150489 
3333526Sxy150489 	return (status);
3343526Sxy150489 }
3353526Sxy150489 
3363526Sxy150489 /*
3374919Sxy150489  * _info - module information
3383526Sxy150489  */
3393526Sxy150489 int
3403526Sxy150489 _info(struct modinfo *modinfop)
3413526Sxy150489 {
3423526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3433526Sxy150489 }
3443526Sxy150489 
3453526Sxy150489 /*
3464919Sxy150489  * e1000g_attach - driver attach
3474919Sxy150489  *
3484919Sxy150489  * This function is the device-specific initialization entry
3494919Sxy150489  * point. This entry point is required and must be written.
3504919Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3514919Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3524919Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3534919Sxy150489  * available for use by the driver.
3544919Sxy150489  *
3554919Sxy150489  * The attach() function will be called once for each instance
3564919Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
3574919Sxy150489  * Until attach() succeeds, the only driver entry points which
3584919Sxy150489  * may be called are open(9E) and getinfo(9E).
3593526Sxy150489  */
3603526Sxy150489 static int
3614919Sxy150489 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
3623526Sxy150489 {
3633526Sxy150489 	struct e1000g *Adapter;
3643526Sxy150489 	struct e1000_hw *hw;
3654919Sxy150489 	struct e1000g_osdep *osdep;
3663526Sxy150489 	int instance;
3673526Sxy150489 
3683526Sxy150489 	switch (cmd) {
3693526Sxy150489 	default:
3703526Sxy150489 		e1000g_log(NULL, CE_WARN,
3714919Sxy150489 		    "Unsupported command send to e1000g_attach... ");
3723526Sxy150489 		return (DDI_FAILURE);
3733526Sxy150489 
3743526Sxy150489 	case DDI_RESUME:
3753526Sxy150489 		return (e1000g_resume(devinfo));
3763526Sxy150489 
3773526Sxy150489 	case DDI_ATTACH:
3783526Sxy150489 		break;
3793526Sxy150489 	}
3803526Sxy150489 
3813526Sxy150489 	/*
3823526Sxy150489 	 * get device instance number
3833526Sxy150489 	 */
3843526Sxy150489 	instance = ddi_get_instance(devinfo);
3853526Sxy150489 
3863526Sxy150489 	/*
3873526Sxy150489 	 * Allocate soft data structure
3883526Sxy150489 	 */
3893526Sxy150489 	Adapter =
3903526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
3913526Sxy150489 
3923526Sxy150489 	Adapter->dip = devinfo;
3934919Sxy150489 	Adapter->instance = instance;
3943526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
3953526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
3963526Sxy150489 
3974919Sxy150489 	hw = &Adapter->shared;
3984919Sxy150489 	osdep = &Adapter->osdep;
3994919Sxy150489 	hw->back = osdep;
4004919Sxy150489 	osdep->adapter = Adapter;
4014919Sxy150489 
4023526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4033526Sxy150489 
4044919Sxy150489 	/*
4055273Sgl147354 	 * Initialize for fma support
4065273Sgl147354 	 */
4075273Sgl147354 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
4085273Sgl147354 	    0, 0x0f,
4095273Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
4105273Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
4115273Sgl147354 	e1000g_fm_init(Adapter);
4125273Sgl147354 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
4135273Sgl147354 
4145273Sgl147354 	/*
4154919Sxy150489 	 * PCI Configure
4164919Sxy150489 	 */
4174919Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4184919Sxy150489 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
4194919Sxy150489 		goto attach_fail;
4204919Sxy150489 	}
4214919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4224919Sxy150489 
4234919Sxy150489 	/*
4244919Sxy150489 	 * Setup hardware
4254919Sxy150489 	 */
4264919Sxy150489 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
4274919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
4284919Sxy150489 		goto attach_fail;
4294919Sxy150489 	}
4303526Sxy150489 
4313526Sxy150489 	/*
4323526Sxy150489 	 * Map in the device registers.
4333526Sxy150489 	 */
4344919Sxy150489 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
4354919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
4363526Sxy150489 		goto attach_fail;
4373526Sxy150489 	}
4384919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4393526Sxy150489 
4403526Sxy150489 	/*
4413526Sxy150489 	 * Initialize driver parameters
4423526Sxy150489 	 */
4433526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
4443526Sxy150489 		goto attach_fail;
4453526Sxy150489 	}
4464919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
4473526Sxy150489 
4485273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
4495273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
4505273Sgl147354 		goto attach_fail;
4515273Sgl147354 	}
4525273Sgl147354 
4533526Sxy150489 	/*
4543526Sxy150489 	 * Initialize interrupts
4553526Sxy150489 	 */
4563526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
4573526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
4583526Sxy150489 		goto attach_fail;
4593526Sxy150489 	}
4604919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
4613526Sxy150489 
4623526Sxy150489 	/*
4633526Sxy150489 	 * Initialize mutex's for this device.
4643526Sxy150489 	 * Do this before enabling the interrupt handler and
4653526Sxy150489 	 * register the softint to avoid the condition where
4663526Sxy150489 	 * interrupt handler can try using uninitialized mutex
4673526Sxy150489 	 */
4683526Sxy150489 	e1000g_init_locks(Adapter);
4693526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
4703526Sxy150489 
4714919Sxy150489 	Adapter->tx_softint_pri = DDI_INTR_SOFTPRI_MAX;
4723526Sxy150489 	if (ddi_intr_add_softint(devinfo,
4733526Sxy150489 	    &Adapter->tx_softint_handle, Adapter->tx_softint_pri,
4744919Sxy150489 	    e1000g_tx_softint_worker, (caddr_t)Adapter) != DDI_SUCCESS) {
4753526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add soft intr failed");
4763526Sxy150489 		goto attach_fail;
4773526Sxy150489 	}
4784919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SOFT_INTR;
4793526Sxy150489 
4803526Sxy150489 	/*
4813526Sxy150489 	 * Initialize Driver Counters
4823526Sxy150489 	 */
4834919Sxy150489 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
4843526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
4853526Sxy150489 		goto attach_fail;
4863526Sxy150489 	}
4873526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
4883526Sxy150489 
4893526Sxy150489 	/*
4903526Sxy150489 	 * Initialize chip hardware and software structures
4913526Sxy150489 	 */
4923526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
4933526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
4943526Sxy150489 		goto attach_fail;
4953526Sxy150489 	}
4963526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
4973526Sxy150489 
4983526Sxy150489 	/*
4993526Sxy150489 	 * Initialize NDD parameters
5003526Sxy150489 	 */
5013526Sxy150489 	if (e1000g_nd_init(Adapter) != DDI_SUCCESS) {
5024919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init ndd failed");
5033526Sxy150489 		goto attach_fail;
5043526Sxy150489 	}
5053526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_NDD;
5063526Sxy150489 
5073526Sxy150489 	/*
5083526Sxy150489 	 * Register the driver to the MAC
5093526Sxy150489 	 */
5103526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
5113526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
5123526Sxy150489 		goto attach_fail;
5133526Sxy150489 	}
5144919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
5153526Sxy150489 
5163526Sxy150489 	/*
5173526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5183526Sxy150489 	 * initialized, enable interrupts.
5193526Sxy150489 	 */
5203526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
5213526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
5223526Sxy150489 		goto attach_fail;
5233526Sxy150489 	}
5244919Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5253526Sxy150489 
5264982Syy150190 	/*
5274982Syy150190 	 * If e1000g_force_detach is enabled, in global private dip list,
5284982Syy150190 	 * we will create a new entry, which maintains the priv_dip for DR
5294982Syy150190 	 * supports after driver detached.
5304982Syy150190 	 */
5314982Syy150190 	if (e1000g_force_detach) {
5324982Syy150190 		private_devi_list_t *devi_node;
5334982Syy150190 
5344982Syy150190 		Adapter->priv_dip =
5354982Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
5364982Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
5374982Syy150190 		    sizeof (struct dev_info));
5384982Syy150190 
5394982Syy150190 		devi_node =
5404982Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
5414982Syy150190 
5424982Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
5434982Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
5444982Syy150190 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
5454982Syy150190 		devi_node->next = e1000g_private_devi_list;
5464982Syy150190 		e1000g_private_devi_list = devi_node;
5474982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
5484982Syy150190 	}
5494982Syy150190 
5503526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
5513526Sxy150489 
5523526Sxy150489 	return (DDI_SUCCESS);
5533526Sxy150489 
5543526Sxy150489 attach_fail:
5553526Sxy150489 	e1000g_unattach(devinfo, Adapter);
5563526Sxy150489 	return (DDI_FAILURE);
5573526Sxy150489 }
5583526Sxy150489 
5593526Sxy150489 static int
5603526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
5613526Sxy150489 {
5624919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5633526Sxy150489 	mac_register_t *mac;
5643526Sxy150489 	int err;
5653526Sxy150489 
5663526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
5673526Sxy150489 		return (DDI_FAILURE);
5684919Sxy150489 
5693526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
5703526Sxy150489 	mac->m_driver = Adapter;
5713526Sxy150489 	mac->m_dip = Adapter->dip;
5724919Sxy150489 	mac->m_src_addr = hw->mac.addr;
5733526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
5743526Sxy150489 	mac->m_min_sdu = 0;
5753526Sxy150489 	mac->m_max_sdu =
5764919Sxy150489 	    (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K) ?
5774919Sxy150489 	    hw->mac.max_frame_size - 256 :
5784919Sxy150489 	    (hw->mac.max_frame_size != ETHERMAX) ?
5794919Sxy150489 	    hw->mac.max_frame_size - 24 : ETHERMTU;
5804919Sxy150489 
5813526Sxy150489 	err = mac_register(mac, &Adapter->mh);
5823526Sxy150489 	mac_free(mac);
5834919Sxy150489 
5843526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
5853526Sxy150489 }
5863526Sxy150489 
5873526Sxy150489 static int
5884919Sxy150489 e1000g_identify_hardware(struct e1000g *Adapter)
5894919Sxy150489 {
5904919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
5914919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
5924919Sxy150489 
5934919Sxy150489 	/* Get the device id */
5944919Sxy150489 	hw->vendor_id =
5954919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
5964919Sxy150489 	hw->device_id =
5974919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
5984919Sxy150489 	hw->revision_id =
5994919Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6004919Sxy150489 	hw->subsystem_device_id =
6014919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6024919Sxy150489 	hw->subsystem_vendor_id =
6034919Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6044919Sxy150489 
6054919Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6064919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
6074919Sxy150489 		    "MAC type could not be set properly.");
6084919Sxy150489 		return (DDI_FAILURE);
6094919Sxy150489 	}
6104919Sxy150489 
6114919Sxy150489 	return (DDI_SUCCESS);
6124919Sxy150489 }
6134919Sxy150489 
6144919Sxy150489 static int
6154919Sxy150489 e1000g_regs_map(struct e1000g *Adapter)
6164919Sxy150489 {
6174919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6184919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
6194919Sxy150489 	struct e1000g_osdep *osdep = &Adapter->osdep;
6204919Sxy150489 	off_t mem_size;
6214919Sxy150489 
6224919Sxy150489 	/*
6234919Sxy150489 	 * first get the size of device register to be mapped. The
6244919Sxy150489 	 * second parameter is the register we are interested. I our
6254919Sxy150489 	 * wiseman 0 is for config registers and 1 is for memory mapped
6264919Sxy150489 	 * registers Mem size should have memory mapped region size
6274919Sxy150489 	 */
6284919Sxy150489 	if (ddi_dev_regsize(devinfo, 1, &mem_size) != DDI_SUCCESS) {
6294919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6304919Sxy150489 		    "ddi_dev_regsize for registers failed");
6314919Sxy150489 		return (DDI_FAILURE);
6324919Sxy150489 	}
6334919Sxy150489 
6344919Sxy150489 	if ((ddi_regs_map_setup(devinfo, 1, /* register of interest */
6354919Sxy150489 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
6364919Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
6374919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6384919Sxy150489 		    "ddi_regs_map_setup for registers failed");
6394919Sxy150489 		goto regs_map_fail;
6404919Sxy150489 	}
6414919Sxy150489 
6424919Sxy150489 	/* ICH needs to map flash memory */
6434919Sxy150489 	if (hw->mac.type == e1000_ich8lan || hw->mac.type == e1000_ich9lan) {
6444919Sxy150489 		/* get flash size */
6454919Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6464919Sxy150489 		    &mem_size) != DDI_SUCCESS) {
6474919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6484919Sxy150489 			    "ddi_dev_regsize for ICH flash failed");
6494919Sxy150489 			goto regs_map_fail;
6504919Sxy150489 		}
6514919Sxy150489 
6524919Sxy150489 		/* map flash in */
6534919Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6544919Sxy150489 		    (caddr_t *)&hw->flash_address, 0,
6554919Sxy150489 		    mem_size, &e1000g_regs_acc_attr,
6564919Sxy150489 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
6574919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6584919Sxy150489 			    "ddi_regs_map_setup for ICH flash failed");
6594919Sxy150489 			goto regs_map_fail;
6604919Sxy150489 		}
6614919Sxy150489 	}
6624919Sxy150489 
6634919Sxy150489 	return (DDI_SUCCESS);
6644919Sxy150489 
6654919Sxy150489 regs_map_fail:
6664919Sxy150489 	if (osdep->reg_handle != NULL)
6674919Sxy150489 		ddi_regs_map_free(&osdep->reg_handle);
6684919Sxy150489 
6694919Sxy150489 	return (DDI_FAILURE);
6704919Sxy150489 }
6714919Sxy150489 
6724919Sxy150489 static int
6733526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
6743526Sxy150489 {
6753526Sxy150489 	struct e1000_hw *hw;
6764919Sxy150489 	e1000g_tx_ring_t *tx_ring;
6774919Sxy150489 	uint32_t mem_bar, io_bar, bar64;
6783526Sxy150489 #ifdef __sparc
6794919Sxy150489 	dev_info_t *devinfo = Adapter->dip;
6803526Sxy150489 	ulong_t iommu_pagesize;
6813526Sxy150489 #endif
6823526Sxy150489 
6834919Sxy150489 	hw = &Adapter->shared;
6844919Sxy150489 
6854919Sxy150489 	/* Set MAC type and initialize hardware functions */
6864919Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
6874919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6884919Sxy150489 		    "Could not setup hardware functions");
6893526Sxy150489 		return (DDI_FAILURE);
6903526Sxy150489 	}
6913526Sxy150489 
6924919Sxy150489 	/* Get bus information */
6934919Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
6944919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
6954919Sxy150489 		    "Could not get bus information");
6964919Sxy150489 		return (DDI_FAILURE);
6973526Sxy150489 	}
6983526Sxy150489 
6993526Sxy150489 	/* get mem_base addr */
7004919Sxy150489 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
7014919Sxy150489 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7023526Sxy150489 
7033526Sxy150489 	/* get io_base addr */
7044919Sxy150489 	if (hw->mac.type >= e1000_82544) {
7054919Sxy150489 		if (bar64) {
7063526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7074919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7084919Sxy150489 			    PCI_CONF_BASE4);
7093526Sxy150489 		} else {
7103526Sxy150489 			/* normal 32-bit BAR mode */
7114919Sxy150489 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
7124919Sxy150489 			    PCI_CONF_BASE2);
7133526Sxy150489 		}
7143526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7153526Sxy150489 	} else {
7163526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7173526Sxy150489 		hw->io_base = 0x0;
7183526Sxy150489 	}
7193526Sxy150489 
7204919Sxy150489 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
7214919Sxy150489 
7224919Sxy150489 	hw->mac.autoneg_failed = B_TRUE;
7234919Sxy150489 
7244919Sxy150489 	/* Set the wait_for_link flag to B_FALSE */
7254919Sxy150489 	hw->phy.wait_for_link = B_FALSE;
7263526Sxy150489 
7273526Sxy150489 	/* Adaptive IFS related changes */
7284919Sxy150489 	hw->mac.adaptive_ifs = B_TRUE;
7294919Sxy150489 
7304919Sxy150489 	/* Enable phy init script for IGP phy of 82541/82547 */
7314919Sxy150489 	if ((hw->mac.type == e1000_82547) ||
7324919Sxy150489 	    (hw->mac.type == e1000_82541) ||
7334919Sxy150489 	    (hw->mac.type == e1000_82547_rev_2) ||
7344919Sxy150489 	    (hw->mac.type == e1000_82541_rev_2))
7354919Sxy150489 		e1000_init_script_state_82541(hw, B_TRUE);
7364919Sxy150489 
7374919Sxy150489 	/* Enable the TTL workaround for 82541/82547 */
7384919Sxy150489 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
7393526Sxy150489 
7404608Syy150190 #ifdef __sparc
7414608Syy150190 	Adapter->strip_crc = B_TRUE;
7424608Syy150190 #else
7434608Syy150190 	Adapter->strip_crc = B_FALSE;
7444608Syy150190 #endif
7454608Syy150190 
7463526Sxy150489 	/* Get conf file properties */
7474919Sxy150489 	e1000g_get_conf(Adapter);
7484919Sxy150489 
7494919Sxy150489 	/* Get speed/duplex settings in conf file */
7504919Sxy150489 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
7514919Sxy150489 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7523526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7533526Sxy150489 
7544919Sxy150489 	/* Get Jumbo Frames settings in conf file */
7553526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7564919Sxy150489 	hw->mac.min_frame_size =
7573526Sxy150489 	    MINIMUM_ETHERNET_PACKET_SIZE + CRC_LENGTH;
7583526Sxy150489 
7593526Sxy150489 #ifdef __sparc
7603526Sxy150489 	/* Get the system page size */
7613526Sxy150489 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
7623526Sxy150489 	iommu_pagesize = dvma_pagesize(devinfo);
7633526Sxy150489 	if (iommu_pagesize != 0) {
7643526Sxy150489 		if (Adapter->sys_page_sz == iommu_pagesize) {
7653526Sxy150489 			if (iommu_pagesize > 0x4000)
7663526Sxy150489 				Adapter->sys_page_sz = 0x4000;
7673526Sxy150489 		} else {
7683526Sxy150489 			if (Adapter->sys_page_sz > iommu_pagesize)
7693526Sxy150489 				Adapter->sys_page_sz = iommu_pagesize;
7703526Sxy150489 		}
7713526Sxy150489 	}
7724919Sxy150489 	Adapter->dvma_page_num = hw->mac.max_frame_size /
7733526Sxy150489 	    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
7743526Sxy150489 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
7753526Sxy150489 #endif
7763526Sxy150489 
7773526Sxy150489 	/* Set Rx/Tx buffer size */
7784919Sxy150489 	switch (hw->mac.max_frame_size) {
7793526Sxy150489 	case ETHERMAX:
7804919Sxy150489 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
7814919Sxy150489 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
7823526Sxy150489 		break;
7833526Sxy150489 	case FRAME_SIZE_UPTO_4K:
7844919Sxy150489 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
7854919Sxy150489 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
7863526Sxy150489 		break;
7873526Sxy150489 	case FRAME_SIZE_UPTO_8K:
7884919Sxy150489 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
7894919Sxy150489 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
7903526Sxy150489 		break;
7914919Sxy150489 	case FRAME_SIZE_UPTO_9K:
7923526Sxy150489 	case FRAME_SIZE_UPTO_16K:
7934919Sxy150489 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
7944919Sxy150489 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
7953526Sxy150489 		break;
7963526Sxy150489 	default:
7974919Sxy150489 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
7984919Sxy150489 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
7993526Sxy150489 		break;
8003526Sxy150489 	}
8014919Sxy150489 	Adapter->rx_buffer_size += E1000G_IPALIGNPRESERVEROOM;
8024919Sxy150489 
8034919Sxy150489 #ifndef NO_82542_SUPPORT
8043526Sxy150489 	/*
8053526Sxy150489 	 * For Wiseman adapters we have an requirement of having receive
8063526Sxy150489 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8073526Sxy150489 	 * require this and forcing it for all hardwares will have
8083526Sxy150489 	 * performance implications, I am making it applicable only for
8093526Sxy150489 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8103526Sxy150489 	 * it is okay to have normal frames...but it does involve a
8113526Sxy150489 	 * potential risk where we may loose data if buffer is not
8123526Sxy150489 	 * aligned...so all wiseman boards to have 256 byte aligned
8133526Sxy150489 	 * buffers
8143526Sxy150489 	 */
8154919Sxy150489 	if (hw->mac.type < e1000_82543)
8164919Sxy150489 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
8173526Sxy150489 	else
8184919Sxy150489 		Adapter->rx_buf_align = 1;
8194919Sxy150489 #endif
8204919Sxy150489 
8214919Sxy150489 	/* Master Latency Timer */
8224919Sxy150489 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
8234919Sxy150489 
8243526Sxy150489 	/* copper options */
8253526Sxy150489 	if (hw->media_type == e1000_media_type_copper) {
8264919Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
8274919Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
8284919Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
8293526Sxy150489 	}
8303526Sxy150489 
8314919Sxy150489 	/* The initial link state should be "unknown" */
8324061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
8334061Sxy150489 
8344919Sxy150489 	/* Initialize tx parameters */
8354919Sxy150489 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
8364919Sxy150489 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
8374919Sxy150489 
8384919Sxy150489 	tx_ring = Adapter->tx_ring;
8394919Sxy150489 	tx_ring->recycle_low_water = DEFAULT_TX_RECYCLE_LOW_WATER;
8404919Sxy150489 	tx_ring->recycle_num = DEFAULT_TX_RECYCLE_NUM;
8414919Sxy150489 	tx_ring->frags_limit =
8424919Sxy150489 	    (hw->mac.max_frame_size / Adapter->tx_bcopy_thresh) + 2;
8434919Sxy150489 	if (tx_ring->frags_limit > (MAX_TX_DESC_PER_PACKET >> 1))
8444919Sxy150489 		tx_ring->frags_limit = (MAX_TX_DESC_PER_PACKET >> 1);
8454919Sxy150489 
8464919Sxy150489 	/* Initialize rx parameters */
8474919Sxy150489 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
8484919Sxy150489 
8493526Sxy150489 	return (DDI_SUCCESS);
8503526Sxy150489 }
8513526Sxy150489 
8523526Sxy150489 /*
8534919Sxy150489  * e1000g_detach - driver detach
8544919Sxy150489  *
8554919Sxy150489  * The detach() function is the complement of the attach routine.
8564919Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
8574919Sxy150489  * state  associated  with  a  given  instance of a device node
8584919Sxy150489  * prior to the removal of that instance from the system.
8594919Sxy150489  *
8604919Sxy150489  * The detach() function will be called once for each  instance
8614919Sxy150489  * of the device for which there has been a successful attach()
8624919Sxy150489  * once there are no longer  any  opens  on  the  device.
8634919Sxy150489  *
8644919Sxy150489  * Interrupts routine are disabled, All memory allocated by this
8654919Sxy150489  * driver are freed.
8663526Sxy150489  */
8673526Sxy150489 static int
8684919Sxy150489 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
8693526Sxy150489 {
8703526Sxy150489 	struct e1000g *Adapter;
8714982Syy150190 	boolean_t rx_drain;
8723526Sxy150489 
8733526Sxy150489 	switch (cmd) {
8743526Sxy150489 	default:
8753526Sxy150489 		return (DDI_FAILURE);
8763526Sxy150489 
8773526Sxy150489 	case DDI_SUSPEND:
8783526Sxy150489 		return (e1000g_suspend(devinfo));
8793526Sxy150489 
8803526Sxy150489 	case DDI_DETACH:
8813526Sxy150489 		break;
8823526Sxy150489 	}
8833526Sxy150489 
8843526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
8853526Sxy150489 	if (Adapter == NULL)
8863526Sxy150489 		return (DDI_FAILURE);
8873526Sxy150489 
8884919Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
8894919Sxy150489 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
8904919Sxy150489 		return (DDI_FAILURE);
8914919Sxy150489 	}
8924919Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
8934919Sxy150489 
8945273Sgl147354 
8955273Sgl147354 	if (Adapter->chip_state != E1000G_STOP)
8964919Sxy150489 		e1000g_stop(Adapter, B_TRUE);
8973526Sxy150489 
8984982Syy150190 	rx_drain = e1000g_rx_drain(Adapter);
8994982Syy150190 
9004982Syy150190 	/*
9014982Syy150190 	 * If e1000g_force_detach is enabled, driver detach is safe.
9024982Syy150190 	 * We will let e1000g_free_priv_devi_node routine determine
9034982Syy150190 	 * whether we need to free the priv_dip entry for current
9044982Syy150190 	 * driver instance.
9054982Syy150190 	 */
9064982Syy150190 	if (e1000g_force_detach) {
9074982Syy150190 		e1000g_free_priv_devi_node(Adapter, rx_drain);
9084982Syy150190 	} else {
9094982Syy150190 		if (!rx_drain)
9103526Sxy150489 			return (DDI_FAILURE);
9113526Sxy150489 	}
9123526Sxy150489 
9133526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9143526Sxy150489 
9153526Sxy150489 	return (DDI_SUCCESS);
9163526Sxy150489 }
9173526Sxy150489 
9184982Syy150190 /*
9194982Syy150190  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
9204982Syy150190  *
9214982Syy150190  * If free_flag is true, that indicates the upper layer is not holding
9224982Syy150190  * the rx buffers, we could free the priv_dip entry safely.
9234982Syy150190  *
9244982Syy150190  * Otherwise, we have to keep this entry even after driver detached,
9254982Syy150190  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
9264982Syy150190  * so that driver could free it while all of rx buffers are returned
9274982Syy150190  * by upper layer later.
9284982Syy150190  */
9294982Syy150190 static void
9304982Syy150190 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
9314982Syy150190 {
9324982Syy150190 	private_devi_list_t *devi_node, *devi_del;
9334982Syy150190 
9344982Syy150190 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
9354982Syy150190 	ASSERT(e1000g_private_devi_list != NULL);
9364982Syy150190 	ASSERT(Adapter->priv_dip != NULL);
9374982Syy150190 
9384982Syy150190 	devi_node = e1000g_private_devi_list;
9394982Syy150190 	if (devi_node->priv_dip == Adapter->priv_dip) {
9404982Syy150190 		if (free_flag) {
9414982Syy150190 			e1000g_private_devi_list =
9424982Syy150190 			    devi_node->next;
9434982Syy150190 			kmem_free(devi_node->priv_dip,
9444982Syy150190 			    sizeof (struct dev_info));
9454982Syy150190 			kmem_free(devi_node,
9464982Syy150190 			    sizeof (private_devi_list_t));
9474982Syy150190 		} else {
9484982Syy150190 			ASSERT(e1000g_mblks_pending != 0);
9494982Syy150190 			devi_node->flag =
9504982Syy150190 			    E1000G_PRIV_DEVI_DETACH;
9514982Syy150190 		}
9524982Syy150190 		rw_exit(&e1000g_rx_detach_lock);
9534982Syy150190 		return;
9544982Syy150190 	}
9554982Syy150190 
9564982Syy150190 	devi_node = e1000g_private_devi_list;
9574982Syy150190 	while (devi_node->next != NULL) {
9584982Syy150190 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
9594982Syy150190 			if (free_flag) {
9604982Syy150190 				devi_del = devi_node->next;
9614982Syy150190 				devi_node->next = devi_del->next;
9624982Syy150190 				kmem_free(devi_del->priv_dip,
9634982Syy150190 				    sizeof (struct dev_info));
9644982Syy150190 				kmem_free(devi_del,
9654982Syy150190 				    sizeof (private_devi_list_t));
9664982Syy150190 			} else {
9674982Syy150190 				ASSERT(e1000g_mblks_pending != 0);
9684982Syy150190 				devi_node->next->flag =
9694982Syy150190 				    E1000G_PRIV_DEVI_DETACH;
9704982Syy150190 			}
9714982Syy150190 			break;
9724982Syy150190 		}
9734982Syy150190 		devi_node = devi_node->next;
9744982Syy150190 	}
9754982Syy150190 	rw_exit(&e1000g_rx_detach_lock);
9764982Syy150190 }
9774982Syy150190 
9783526Sxy150489 static void
9793526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
9803526Sxy150489 {
9814919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
9823526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
9833526Sxy150489 	}
9843526Sxy150489 
9854919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
9863526Sxy150489 		(void) mac_unregister(Adapter->mh);
9873526Sxy150489 	}
9883526Sxy150489 
9893526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_NDD) {
9903526Sxy150489 		e1000g_nd_cleanup(Adapter);
9913526Sxy150489 	}
9923526Sxy150489 
9934919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
9943526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
9953526Sxy150489 	}
9963526Sxy150489 
9974919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SOFT_INTR) {
9983526Sxy150489 		(void) ddi_intr_remove_softint(Adapter->tx_softint_handle);
9993526Sxy150489 	}
10003526Sxy150489 
10014919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
10023526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
10033526Sxy150489 	}
10043526Sxy150489 
10053526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
10063526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
10073526Sxy150489 	}
10083526Sxy150489 
10093526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
10104919Sxy150489 		stop_link_timer(Adapter);
10115273Sgl147354 		if (e1000_reset_hw(&Adapter->shared) != 0) {
10125273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
10135273Sgl147354 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
10145273Sgl147354 		}
10153526Sxy150489 	}
10163526Sxy150489 
10174919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
10184919Sxy150489 		if (Adapter->osdep.reg_handle != NULL)
10194919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
10204919Sxy150489 		if (Adapter->osdep.ich_flash_handle != NULL)
10214919Sxy150489 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
10223526Sxy150489 	}
10233526Sxy150489 
10244919Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
10254919Sxy150489 		if (Adapter->osdep.cfg_handle != NULL)
10264919Sxy150489 			pci_config_teardown(&Adapter->osdep.cfg_handle);
10273526Sxy150489 	}
10283526Sxy150489 
10293526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
10303526Sxy150489 		e1000g_destroy_locks(Adapter);
10313526Sxy150489 	}
10323526Sxy150489 
10335273Sgl147354 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
10345273Sgl147354 		e1000g_fm_fini(Adapter);
10355273Sgl147354 	}
10365273Sgl147354 
10374919Sxy150489 	e1000_remove_device(&Adapter->shared);
10384919Sxy150489 
10393526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
10403526Sxy150489 
10413526Sxy150489 	/*
10423526Sxy150489 	 * Another hotplug spec requirement,
10433526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
10443526Sxy150489 	 */
10453526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
10463526Sxy150489 }
10473526Sxy150489 
10483526Sxy150489 static void
10493526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
10503526Sxy150489 {
10513526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10523526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10533526Sxy150489 
10543526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
10553526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10564919Sxy150489 	mutex_init(&Adapter->link_lock, NULL,
10573526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10584919Sxy150489 	mutex_init(&Adapter->watchdog_lock, NULL,
10593526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10603526Sxy150489 
10613526Sxy150489 	tx_ring = Adapter->tx_ring;
10623526Sxy150489 
10633526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
10643526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10653526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
10663526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10673526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
10683526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10694919Sxy150489 	mutex_init(&tx_ring->mblks_lock, NULL,
10704919Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10713526Sxy150489 
10723526Sxy150489 	rx_ring = Adapter->rx_ring;
10733526Sxy150489 
10743526Sxy150489 	mutex_init(&rx_ring->rx_lock, NULL,
10753526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10763526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
10773526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10783526Sxy150489 }
10793526Sxy150489 
10803526Sxy150489 static void
10813526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
10823526Sxy150489 {
10833526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10843526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10853526Sxy150489 
10863526Sxy150489 	tx_ring = Adapter->tx_ring;
10873526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
10883526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
10893526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
10904919Sxy150489 	mutex_destroy(&tx_ring->mblks_lock);
10913526Sxy150489 
10923526Sxy150489 	rx_ring = Adapter->rx_ring;
10933526Sxy150489 	mutex_destroy(&rx_ring->rx_lock);
10943526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
10953526Sxy150489 
10964919Sxy150489 	mutex_destroy(&Adapter->link_lock);
10974919Sxy150489 	mutex_destroy(&Adapter->watchdog_lock);
10983526Sxy150489 	rw_destroy(&Adapter->chip_lock);
10993526Sxy150489 }
11003526Sxy150489 
11013526Sxy150489 static int
11023526Sxy150489 e1000g_resume(dev_info_t *devinfo)
11033526Sxy150489 {
11043526Sxy150489 	struct e1000g *Adapter;
11053526Sxy150489 
11063526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11073526Sxy150489 	if (Adapter == NULL)
11083526Sxy150489 		return (DDI_FAILURE);
11093526Sxy150489 
11104919Sxy150489 	if (e1000g_start(Adapter, B_TRUE))
11113526Sxy150489 		return (DDI_FAILURE);
11123526Sxy150489 
11133526Sxy150489 	return (DDI_SUCCESS);
11143526Sxy150489 }
11153526Sxy150489 
11163526Sxy150489 static int
11173526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
11183526Sxy150489 {
11193526Sxy150489 	struct e1000g *Adapter;
11203526Sxy150489 
11213526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
11223526Sxy150489 	if (Adapter == NULL)
11233526Sxy150489 		return (DDI_FAILURE);
11243526Sxy150489 
11254919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
11263526Sxy150489 
11273526Sxy150489 	return (DDI_SUCCESS);
11283526Sxy150489 }
11293526Sxy150489 
11303526Sxy150489 static int
11313526Sxy150489 e1000g_init(struct e1000g *Adapter)
11323526Sxy150489 {
11333526Sxy150489 	uint32_t pba;
11344919Sxy150489 	uint32_t high_water;
11353526Sxy150489 	struct e1000_hw *hw;
11364061Sxy150489 	clock_t link_timeout;
11373526Sxy150489 
11384919Sxy150489 	hw = &Adapter->shared;
11393526Sxy150489 
11403526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
11413526Sxy150489 
11423526Sxy150489 	/*
11433526Sxy150489 	 * reset to put the hardware in a known state
11443526Sxy150489 	 * before we try to do anything with the eeprom
11453526Sxy150489 	 */
11465273Sgl147354 	if (e1000_reset_hw(hw) != 0) {
11475273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11485273Sgl147354 		goto init_fail;
11495273Sgl147354 	}
11503526Sxy150489 
11514919Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
11524061Sxy150489 		/*
11534061Sxy150489 		 * Some PCI-E parts fail the first check due to
11544061Sxy150489 		 * the link being in sleep state.  Call it again,
11554061Sxy150489 		 * if it fails a second time its a real issue.
11564061Sxy150489 		 */
11574919Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
11584061Sxy150489 			e1000g_log(Adapter, CE_WARN,
11594919Sxy150489 			    "Invalid NVM checksum. Please contact "
11604919Sxy150489 			    "the vendor to update the NVM.");
11615273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11624061Sxy150489 			goto init_fail;
11634061Sxy150489 		}
11643526Sxy150489 	}
11653526Sxy150489 
11663526Sxy150489 #ifdef __sparc
11673526Sxy150489 	/*
11683526Sxy150489 	 * Firstly, we try to get the local ethernet address from OBP. If
11693526Sxy150489 	 * fail, we get from EEPROM of NIC card.
11703526Sxy150489 	 */
11713526Sxy150489 	if (!e1000g_find_mac_address(Adapter)) {
11723526Sxy150489 		if (e1000_read_mac_addr(hw) < 0) {
11733526Sxy150489 			e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
11745273Sgl147354 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11753526Sxy150489 			goto init_fail;
11763526Sxy150489 		}
11773526Sxy150489 	}
11783526Sxy150489 #else
11793526Sxy150489 	/* Get the local ethernet address. */
11803526Sxy150489 	if (e1000_read_mac_addr(hw) < 0) {
11813526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
11825273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11833526Sxy150489 		goto init_fail;
11843526Sxy150489 	}
11853526Sxy150489 #endif
11863526Sxy150489 
11873526Sxy150489 	/* check for valid mac address */
11884919Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
11893526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
11905273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
11913526Sxy150489 		goto init_fail;
11923526Sxy150489 	}
11933526Sxy150489 
11944919Sxy150489 	/* Set LAA state for 82571 chipset */
11954919Sxy150489 	e1000_set_laa_state_82571(hw, B_TRUE);
11963526Sxy150489 
11973526Sxy150489 	/* Master Latency Timer implementation */
11984919Sxy150489 	if (Adapter->master_latency_timer) {
11994919Sxy150489 		pci_config_put8(Adapter->osdep.cfg_handle,
12004919Sxy150489 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
12013526Sxy150489 	}
12023526Sxy150489 
12034919Sxy150489 	if (hw->mac.type < e1000_82547) {
12043526Sxy150489 		/*
12053526Sxy150489 		 * Total FIFO is 64K
12063526Sxy150489 		 */
12074919Sxy150489 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
12083526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
12093526Sxy150489 		else
12103526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
12114919Sxy150489 	} else if (hw->mac.type >= e1000_82571 &&
12124919Sxy150489 	    hw->mac.type <= e1000_82572) {
12133526Sxy150489 		/*
12143526Sxy150489 		 * Total FIFO is 48K
12153526Sxy150489 		 */
12164919Sxy150489 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
12173526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
12183526Sxy150489 		else
12193526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
12204919Sxy150489 	} else if (hw->mac.type == e1000_ich8lan) {
12213526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
12224919Sxy150489 	} else if (hw->mac.type == e1000_ich9lan) {
12234919Sxy150489 		pba = E1000_PBA_12K;
12243526Sxy150489 	} else {
12253526Sxy150489 		/*
12263526Sxy150489 		 * Total FIFO is 40K
12273526Sxy150489 		 */
12284919Sxy150489 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
12293526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
12303526Sxy150489 		else
12313526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
12323526Sxy150489 	}
12334919Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
12343526Sxy150489 
12353526Sxy150489 	/*
12363526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
12373526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
12383526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
12393526Sxy150489 	 * file.
12403526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
12413526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
12423526Sxy150489 	 * high-water mark.
12434919Sxy150489 	 * The high water mark must be low enough to fit one full frame above
12444919Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
12454919Sxy150489 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
12464919Sxy150489 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
12474919Sxy150489 	 * Rx FIFO size minus one full frame.
12483526Sxy150489 	 */
12494919Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
12504919Sxy150489 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_ich9lan) ?
12514919Sxy150489 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
12524919Sxy150489 	    ((pba << 10) - hw->mac.max_frame_size)));
12534919Sxy150489 
12544919Sxy150489 	hw->mac.fc_high_water = high_water & 0xFFF8;
12554919Sxy150489 	hw->mac.fc_low_water = hw->mac.fc_high_water - 8;
12564919Sxy150489 
12574919Sxy150489 	if (hw->mac.type == e1000_80003es2lan)
12584919Sxy150489 		hw->mac.fc_pause_time = 0xFFFF;
12594919Sxy150489 	else
12604919Sxy150489 		hw->mac.fc_pause_time = E1000_FC_PAUSE_TIME;
12614919Sxy150489 	hw->mac.fc_send_xon = B_TRUE;
12624919Sxy150489 	hw->mac.fc = hw->mac.original_fc;
12633526Sxy150489 
12643526Sxy150489 	/*
12653526Sxy150489 	 * Reset the adapter hardware the second time.
12663526Sxy150489 	 */
12675273Sgl147354 	if (e1000_reset_hw(hw) != 0) {
12685273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12695273Sgl147354 		goto init_fail;
12705273Sgl147354 	}
12713526Sxy150489 
12723526Sxy150489 	/* disable wakeup control by default */
12734919Sxy150489 	if (hw->mac.type >= e1000_82544)
12744919Sxy150489 		E1000_WRITE_REG(hw, E1000_WUC, 0);
12753526Sxy150489 
12763526Sxy150489 	/* MWI setup */
12774919Sxy150489 	e1000_pci_set_mwi(hw);
12783526Sxy150489 
12793526Sxy150489 	/*
12803526Sxy150489 	 * Configure/Initialize hardware
12813526Sxy150489 	 */
12823526Sxy150489 	if (e1000_init_hw(hw) < 0) {
12833526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
12845273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
12853526Sxy150489 		goto init_fail;
12863526Sxy150489 	}
12873526Sxy150489 
12883526Sxy150489 	/* Disable Smart Power Down */
12893526Sxy150489 	phy_spd_state(hw, B_FALSE);
12903526Sxy150489 
12915082Syy150190 	/* Make sure driver has control */
12925082Syy150190 	e1000g_get_driver_control(hw);
12935082Syy150190 
12943526Sxy150489 	/*
12953526Sxy150489 	 * Initialize unicast addresses.
12963526Sxy150489 	 */
12973526Sxy150489 	e1000g_init_unicst(Adapter);
12983526Sxy150489 
12993526Sxy150489 	/*
13003526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
13013526Sxy150489 	 * completes  Multicast table will be set
13023526Sxy150489 	 */
13034919Sxy150489 	e1000g_setup_multicast(Adapter);
13044919Sxy150489 	msec_delay(5);
13053526Sxy150489 
13063526Sxy150489 	/*
13073526Sxy150489 	 * Implement Adaptive IFS
13083526Sxy150489 	 */
13093526Sxy150489 	e1000_reset_adaptive(hw);
13103526Sxy150489 
13113526Sxy150489 	/* Setup Interrupt Throttling Register */
13124919Sxy150489 	E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
13133526Sxy150489 
13144061Sxy150489 	/* Start the timer for link setup */
13154919Sxy150489 	if (hw->mac.autoneg)
13164919Sxy150489 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
13174061Sxy150489 	else
13184919Sxy150489 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
13194919Sxy150489 
13204919Sxy150489 	mutex_enter(&Adapter->link_lock);
13214919Sxy150489 	if (hw->phy.wait_for_link) {
13224061Sxy150489 		Adapter->link_complete = B_TRUE;
13233526Sxy150489 	} else {
13244061Sxy150489 		Adapter->link_complete = B_FALSE;
13254061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
13264061Sxy150489 		    (void *)Adapter, link_timeout);
13273526Sxy150489 	}
13284919Sxy150489 	mutex_exit(&Adapter->link_lock);
13293526Sxy150489 
13303526Sxy150489 	/* Enable PCI-Ex master */
13314919Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
13323526Sxy150489 		e1000_enable_pciex_master(hw);
13333526Sxy150489 	}
13343526Sxy150489 
13355082Syy150190 	/* Save the state of the phy */
13365082Syy150190 	e1000g_get_phy_state(Adapter);
13375082Syy150190 
13383526Sxy150489 	Adapter->init_count++;
13393526Sxy150489 
13405273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
13415273Sgl147354 		goto init_fail;
13425273Sgl147354 	}
13435273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
13445273Sgl147354 		goto init_fail;
13455273Sgl147354 	}
13465273Sgl147354 
13473526Sxy150489 	rw_exit(&Adapter->chip_lock);
13483526Sxy150489 
13493526Sxy150489 	return (DDI_SUCCESS);
13503526Sxy150489 
13513526Sxy150489 init_fail:
13523526Sxy150489 	rw_exit(&Adapter->chip_lock);
13535273Sgl147354 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
13543526Sxy150489 	return (DDI_FAILURE);
13553526Sxy150489 }
13563526Sxy150489 
13573526Sxy150489 /*
13583526Sxy150489  * Check if the link is up
13593526Sxy150489  */
13603526Sxy150489 static boolean_t
13613526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
13623526Sxy150489 {
13633526Sxy150489 	struct e1000_hw *hw;
13643526Sxy150489 	boolean_t link_up;
13653526Sxy150489 
13664919Sxy150489 	hw = &Adapter->shared;
13673526Sxy150489 
13683526Sxy150489 	e1000_check_for_link(hw);
13693526Sxy150489 
13704919Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
13714919Sxy150489 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
13723526Sxy150489 	    ((hw->media_type == e1000_media_type_internal_serdes) &&
13734919Sxy150489 	    (hw->mac.serdes_has_link))) {
13743526Sxy150489 		link_up = B_TRUE;
13753526Sxy150489 	} else {
13763526Sxy150489 		link_up = B_FALSE;
13773526Sxy150489 	}
13783526Sxy150489 
13793526Sxy150489 	return (link_up);
13803526Sxy150489 }
13813526Sxy150489 
13823526Sxy150489 static void
13833526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
13843526Sxy150489 {
13853526Sxy150489 	struct iocblk *iocp;
13863526Sxy150489 	struct e1000g *e1000gp;
13873526Sxy150489 	enum ioc_reply status;
13883526Sxy150489 	int err;
13893526Sxy150489 
13903526Sxy150489 	iocp = (struct iocblk *)mp->b_rptr;
13913526Sxy150489 	iocp->ioc_error = 0;
13923526Sxy150489 	e1000gp = (struct e1000g *)arg;
13933526Sxy150489 
13943526Sxy150489 	ASSERT(e1000gp);
13953526Sxy150489 	if (e1000gp == NULL) {
13963526Sxy150489 		miocnak(q, mp, 0, EINVAL);
13973526Sxy150489 		return;
13983526Sxy150489 	}
13993526Sxy150489 
14003526Sxy150489 	switch (iocp->ioc_cmd) {
14013526Sxy150489 
14023526Sxy150489 	case LB_GET_INFO_SIZE:
14033526Sxy150489 	case LB_GET_INFO:
14043526Sxy150489 	case LB_GET_MODE:
14053526Sxy150489 	case LB_SET_MODE:
14063526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
14073526Sxy150489 		break;
14083526Sxy150489 
14093526Sxy150489 	case ND_GET:
14103526Sxy150489 	case ND_SET:
14113526Sxy150489 		status = e1000g_nd_ioctl(e1000gp, q, mp, iocp);
14123526Sxy150489 		break;
14133526Sxy150489 
14144919Sxy150489 #ifdef E1000G_DEBUG
14153526Sxy150489 	case E1000G_IOC_REG_PEEK:
14163526Sxy150489 	case E1000G_IOC_REG_POKE:
14173526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
14183526Sxy150489 		break;
14193526Sxy150489 	case E1000G_IOC_CHIP_RESET:
14203526Sxy150489 		e1000gp->reset_count++;
14213526Sxy150489 		if (e1000g_reset(e1000gp))
14223526Sxy150489 			status = IOC_ACK;
14233526Sxy150489 		else
14243526Sxy150489 			status = IOC_INVAL;
14253526Sxy150489 		break;
14264919Sxy150489 #endif
14273526Sxy150489 	default:
14283526Sxy150489 		status = IOC_INVAL;
14293526Sxy150489 		break;
14303526Sxy150489 	}
14313526Sxy150489 
14323526Sxy150489 	/*
14333526Sxy150489 	 * Decide how to reply
14343526Sxy150489 	 */
14353526Sxy150489 	switch (status) {
14363526Sxy150489 	default:
14373526Sxy150489 	case IOC_INVAL:
14383526Sxy150489 		/*
14393526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
14403526Sxy150489 		 */
14413526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
14424349Sxy150489 		    EINVAL : iocp->ioc_error);
14433526Sxy150489 		break;
14443526Sxy150489 
14453526Sxy150489 	case IOC_DONE:
14463526Sxy150489 		/*
14473526Sxy150489 		 * OK, reply already sent
14483526Sxy150489 		 */
14493526Sxy150489 		break;
14503526Sxy150489 
14513526Sxy150489 	case IOC_ACK:
14523526Sxy150489 		/*
14533526Sxy150489 		 * OK, reply with an ACK
14543526Sxy150489 		 */
14553526Sxy150489 		miocack(q, mp, 0, 0);
14563526Sxy150489 		break;
14573526Sxy150489 
14583526Sxy150489 	case IOC_REPLY:
14593526Sxy150489 		/*
14603526Sxy150489 		 * OK, send prepared reply as ACK or NAK
14613526Sxy150489 		 */
14623526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
14634349Sxy150489 		    M_IOCACK : M_IOCNAK;
14643526Sxy150489 		qreply(q, mp);
14653526Sxy150489 		break;
14663526Sxy150489 	}
14673526Sxy150489 }
14683526Sxy150489 
14693526Sxy150489 static void e1000g_m_blank(void *arg, time_t ticks, uint32_t count)
14703526Sxy150489 {
14713526Sxy150489 	struct e1000g *Adapter;
14723526Sxy150489 
14733526Sxy150489 	Adapter = (struct e1000g *)arg;
14743526Sxy150489 
14753526Sxy150489 	/*
14763526Sxy150489 	 * Adjust ITR (Interrupt Throttling Register) to coalesce
14773526Sxy150489 	 * interrupts. This formula and its coefficient come from
14783526Sxy150489 	 * our experiments.
14793526Sxy150489 	 */
14803526Sxy150489 	if (Adapter->intr_adaptive) {
14813526Sxy150489 		Adapter->intr_throttling_rate = count << 5;
14824919Sxy150489 		E1000_WRITE_REG(&Adapter->shared, E1000_ITR,
14833526Sxy150489 		    Adapter->intr_throttling_rate);
14843526Sxy150489 	}
14855273Sgl147354 
14865273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
14875273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_UNAFFECTED);
14883526Sxy150489 }
14893526Sxy150489 
14903526Sxy150489 static void
14913526Sxy150489 e1000g_m_resources(void *arg)
14923526Sxy150489 {
14933526Sxy150489 	struct e1000g *adapter = (struct e1000g *)arg;
14943526Sxy150489 	mac_rx_fifo_t mrf;
14953526Sxy150489 
14963526Sxy150489 	mrf.mrf_type = MAC_RX_FIFO;
14973526Sxy150489 	mrf.mrf_blank = e1000g_m_blank;
14983526Sxy150489 	mrf.mrf_arg = (void *)adapter;
14993526Sxy150489 	mrf.mrf_normal_blank_time = E1000_RX_INTPT_TIME;
15003526Sxy150489 	mrf.mrf_normal_pkt_count = E1000_RX_PKT_CNT;
15013526Sxy150489 
15023526Sxy150489 	adapter->mrh = mac_resource_add(adapter->mh, (mac_resource_t *)&mrf);
15033526Sxy150489 }
15043526Sxy150489 
15053526Sxy150489 static int
15063526Sxy150489 e1000g_m_start(void *arg)
15073526Sxy150489 {
15083526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15093526Sxy150489 
15104919Sxy150489 	return (e1000g_start(Adapter, B_TRUE));
15113526Sxy150489 }
15123526Sxy150489 
15133526Sxy150489 static int
15144919Sxy150489 e1000g_start(struct e1000g *Adapter, boolean_t global)
15153526Sxy150489 {
15164919Sxy150489 	if (global) {
15174919Sxy150489 		/* Allocate dma resources for descriptors and buffers */
15184919Sxy150489 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
15194919Sxy150489 			e1000g_log(Adapter, CE_WARN,
15204919Sxy150489 			    "Alloc DMA resources failed");
15214919Sxy150489 			return (ENOTACTIVE);
15224919Sxy150489 		}
15234919Sxy150489 		Adapter->rx_buffer_setup = B_FALSE;
15244919Sxy150489 	}
15254919Sxy150489 
15263526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
15273526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
15283526Sxy150489 			e1000g_log(Adapter, CE_WARN,
15293526Sxy150489 			    "Adapter initialization failed");
15304919Sxy150489 			if (global)
15314919Sxy150489 				e1000g_release_dma_resources(Adapter);
15323526Sxy150489 			return (ENOTACTIVE);
15333526Sxy150489 		}
15343526Sxy150489 	}
15353526Sxy150489 
15363526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15373526Sxy150489 
15384919Sxy150489 	/* Setup and initialize the transmit structures */
15394919Sxy150489 	e1000g_tx_setup(Adapter);
15404919Sxy150489 	msec_delay(5);
15414919Sxy150489 
15424919Sxy150489 	/* Setup and initialize the receive structures */
15434919Sxy150489 	e1000g_rx_setup(Adapter);
15444919Sxy150489 	msec_delay(5);
15454919Sxy150489 
15464919Sxy150489 	e1000g_mask_interrupt(Adapter);
15473526Sxy150489 	if (Adapter->tx_intr_enable)
15484919Sxy150489 		e1000g_mask_tx_interrupt(Adapter);
15493526Sxy150489 
15505273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
15515273Sgl147354 		rw_exit(&Adapter->chip_lock);
15525273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
15535273Sgl147354 		return (ENOTACTIVE);
15545273Sgl147354 	}
15555273Sgl147354 
15565273Sgl147354 	Adapter->chip_state = E1000G_START;
15573526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
15583526Sxy150489 
15593526Sxy150489 	rw_exit(&Adapter->chip_lock);
15603526Sxy150489 
15614919Sxy150489 	/* Enable and start the watchdog timer */
15624919Sxy150489 	enable_watchdog_timer(Adapter);
15634919Sxy150489 
15643526Sxy150489 	return (0);
15653526Sxy150489 }
15663526Sxy150489 
15673526Sxy150489 static void
15683526Sxy150489 e1000g_m_stop(void *arg)
15693526Sxy150489 {
15703526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
15713526Sxy150489 
15724919Sxy150489 	e1000g_stop(Adapter, B_TRUE);
15733526Sxy150489 }
15743526Sxy150489 
15753526Sxy150489 static void
15764919Sxy150489 e1000g_stop(struct e1000g *Adapter, boolean_t global)
15773526Sxy150489 {
15783526Sxy150489 	/* Set stop flags */
15793526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15803526Sxy150489 
15815273Sgl147354 	Adapter->chip_state = E1000G_STOP;
15823526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
15833526Sxy150489 
15843526Sxy150489 	rw_exit(&Adapter->chip_lock);
15853526Sxy150489 
15863526Sxy150489 	/* Drain tx sessions */
15873526Sxy150489 	(void) e1000g_tx_drain(Adapter);
15883526Sxy150489 
15894919Sxy150489 	/* Disable and stop all the timers */
15904919Sxy150489 	disable_watchdog_timer(Adapter);
15914919Sxy150489 	stop_link_timer(Adapter);
15924919Sxy150489 	stop_82547_timer(Adapter->tx_ring);
15934061Sxy150489 
15943526Sxy150489 	/* Stop the chip and release pending resources */
15953526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15963526Sxy150489 
15974919Sxy150489 	e1000g_clear_all_interrupts(Adapter);
15985273Sgl147354 	if (e1000_reset_hw(&Adapter->shared) != 0) {
15995273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
16005273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16015273Sgl147354 	}
16023526Sxy150489 
16033526Sxy150489 	/* Release resources still held by the TX descriptors */
16044919Sxy150489 	e1000g_tx_clean(Adapter);
16054061Sxy150489 
16065273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
16075273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
16085273Sgl147354 
16094061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
16104919Sxy150489 	e1000g_rx_clean(Adapter);
16114061Sxy150489 
16124061Sxy150489 	rw_exit(&Adapter->chip_lock);
16134919Sxy150489 
16144919Sxy150489 	if (global)
16154919Sxy150489 		e1000g_release_dma_resources(Adapter);
16164061Sxy150489 }
16174061Sxy150489 
16184061Sxy150489 static void
16194919Sxy150489 e1000g_rx_clean(struct e1000g *Adapter)
16204919Sxy150489 {
16214919Sxy150489 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
16224919Sxy150489 
16234919Sxy150489 	if (rx_ring->rx_mblk != NULL) {
16244919Sxy150489 		freemsg(rx_ring->rx_mblk);
16254919Sxy150489 		rx_ring->rx_mblk = NULL;
16264919Sxy150489 		rx_ring->rx_mblk_tail = NULL;
16274919Sxy150489 		rx_ring->rx_mblk_len = 0;
16284919Sxy150489 	}
16294919Sxy150489 }
16304919Sxy150489 
16314919Sxy150489 static void
16324919Sxy150489 e1000g_tx_clean(struct e1000g *Adapter)
16334061Sxy150489 {
16344061Sxy150489 	e1000g_tx_ring_t *tx_ring;
16354919Sxy150489 	p_tx_sw_packet_t packet;
16364061Sxy150489 	mblk_t *mp;
16374061Sxy150489 	mblk_t *nmp;
16384061Sxy150489 	uint32_t packet_count;
16394061Sxy150489 
16404061Sxy150489 	tx_ring = Adapter->tx_ring;
16414061Sxy150489 
16423526Sxy150489 	/*
16433526Sxy150489 	 * Here we don't need to protect the lists using
16443526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
16453526Sxy150489 	 * have been protected by the chip_lock.
16463526Sxy150489 	 */
16473526Sxy150489 	mp = NULL;
16483526Sxy150489 	nmp = NULL;
16494061Sxy150489 	packet_count = 0;
16504919Sxy150489 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
16513526Sxy150489 	while (packet != NULL) {
16523526Sxy150489 		if (packet->mp != NULL) {
16533526Sxy150489 			/* Assemble the message chain */
16543526Sxy150489 			if (mp == NULL) {
16553526Sxy150489 				mp = packet->mp;
16563526Sxy150489 				nmp = packet->mp;
16573526Sxy150489 			} else {
16583526Sxy150489 				nmp->b_next = packet->mp;
16593526Sxy150489 				nmp = packet->mp;
16603526Sxy150489 			}
16613526Sxy150489 			/* Disconnect the message from the sw packet */
16623526Sxy150489 			packet->mp = NULL;
16633526Sxy150489 		}
16643526Sxy150489 
16654919Sxy150489 		e1000g_free_tx_swpkt(packet);
16664061Sxy150489 		packet_count++;
16673526Sxy150489 
16684919Sxy150489 		packet = (p_tx_sw_packet_t)
16693526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
16703526Sxy150489 	}
16713526Sxy150489 
16723526Sxy150489 	if (mp != NULL) {
16734919Sxy150489 		mutex_enter(&tx_ring->mblks_lock);
16744919Sxy150489 		if (tx_ring->mblks.head == NULL) {
16754919Sxy150489 			tx_ring->mblks.head = mp;
16764919Sxy150489 			tx_ring->mblks.tail = nmp;
16773526Sxy150489 		} else {
16784919Sxy150489 			tx_ring->mblks.tail->b_next = mp;
16794919Sxy150489 			tx_ring->mblks.tail = nmp;
16803526Sxy150489 		}
16814919Sxy150489 		mutex_exit(&tx_ring->mblks_lock);
16823526Sxy150489 	}
16833526Sxy150489 
16844061Sxy150489 	ddi_intr_trigger_softint(Adapter->tx_softint_handle, NULL);
16854061Sxy150489 
16864061Sxy150489 	if (packet_count > 0) {
16874061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
16884061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
16894061Sxy150489 
16904061Sxy150489 		/* Setup TX descriptor pointers */
16914061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
16924061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
16934061Sxy150489 
16944061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
16954919Sxy150489 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH, 0);
16964919Sxy150489 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT, 0);
16973526Sxy150489 	}
16983526Sxy150489 }
16993526Sxy150489 
17003526Sxy150489 static boolean_t
17013526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
17023526Sxy150489 {
17033526Sxy150489 	int i;
17043526Sxy150489 	boolean_t done;
17053526Sxy150489 	e1000g_tx_ring_t *tx_ring;
17063526Sxy150489 
17073526Sxy150489 	tx_ring = Adapter->tx_ring;
17083526Sxy150489 
17093526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
17104919Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
17113526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
17123526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
17133526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
17143526Sxy150489 
17153526Sxy150489 		if (done)
17163526Sxy150489 			break;
17173526Sxy150489 
17183526Sxy150489 		msec_delay(1);
17193526Sxy150489 	}
17203526Sxy150489 
17213526Sxy150489 	return (done);
17223526Sxy150489 }
17233526Sxy150489 
17243526Sxy150489 static boolean_t
17253526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
17263526Sxy150489 {
17274919Sxy150489 	e1000g_rx_ring_t *rx_ring;
17284919Sxy150489 	p_rx_sw_packet_t packet;
17293526Sxy150489 	boolean_t done;
17303526Sxy150489 
17314919Sxy150489 	rx_ring = Adapter->rx_ring;
17324919Sxy150489 	done = B_TRUE;
17334919Sxy150489 
17344919Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
17354919Sxy150489 
17364919Sxy150489 	while (rx_ring->pending_list != NULL) {
17374919Sxy150489 		packet = rx_ring->pending_list;
17384919Sxy150489 		rx_ring->pending_list =
17394919Sxy150489 		    rx_ring->pending_list->next;
17404919Sxy150489 
17414919Sxy150489 		if (packet->flag == E1000G_RX_SW_STOP) {
17424919Sxy150489 			packet->flag = E1000G_RX_SW_DETACH;
17434919Sxy150489 			done = B_FALSE;
17444919Sxy150489 		} else {
17454919Sxy150489 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
17464919Sxy150489 			ASSERT(packet->mp == NULL);
17474919Sxy150489 			e1000g_free_rx_sw_packet(packet);
17484919Sxy150489 		}
17494919Sxy150489 	}
17504919Sxy150489 
17514919Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
17523526Sxy150489 
17533526Sxy150489 	return (done);
17543526Sxy150489 }
17553526Sxy150489 
17564061Sxy150489 boolean_t
17573526Sxy150489 e1000g_reset(struct e1000g *Adapter)
17583526Sxy150489 {
17594919Sxy150489 	e1000g_stop(Adapter, B_FALSE);
17604919Sxy150489 
17614919Sxy150489 	if (e1000g_start(Adapter, B_FALSE)) {
17623526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
17633526Sxy150489 		return (B_FALSE);
17643526Sxy150489 	}
17653526Sxy150489 
17663526Sxy150489 	return (B_TRUE);
17673526Sxy150489 }
17683526Sxy150489 
17695273Sgl147354 boolean_t
17705273Sgl147354 e1000g_global_reset(struct e1000g *Adapter)
17715273Sgl147354 {
17725273Sgl147354 	e1000g_stop(Adapter, B_TRUE);
17735273Sgl147354 
17745273Sgl147354 	Adapter->init_count = 0;
17755273Sgl147354 
17765273Sgl147354 	if (e1000g_start(Adapter, B_TRUE)) {
17775273Sgl147354 		e1000g_log(Adapter, CE_WARN, "Reset failed");
17785273Sgl147354 		return (B_FALSE);
17795273Sgl147354 	}
17805273Sgl147354 
17815273Sgl147354 	return (B_TRUE);
17825273Sgl147354 }
17835273Sgl147354 
17843526Sxy150489 /*
17854919Sxy150489  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
17864919Sxy150489  *
17874919Sxy150489  * This interrupt service routine is for PCI-Express adapters.
17884919Sxy150489  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
17894919Sxy150489  * bit is set.
17903526Sxy150489  */
17913526Sxy150489 static uint_t
17923526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
17933526Sxy150489 {
17943526Sxy150489 	struct e1000g *Adapter;
17954919Sxy150489 	uint32_t icr;
17963526Sxy150489 
17973526Sxy150489 	Adapter = (struct e1000g *)arg;
17984919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
17994919Sxy150489 
18005273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18015273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18025273Sgl147354 
18034919Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
18043526Sxy150489 		/*
18053526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
18063526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18073526Sxy150489 		 * look for work to do.
18083526Sxy150489 		 */
18094919Sxy150489 		e1000g_intr_work(Adapter, icr);
18103526Sxy150489 		return (DDI_INTR_CLAIMED);
18113526Sxy150489 	} else {
18123526Sxy150489 		/*
18133526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
18143526Sxy150489 		 * Don't claim this interrupt, return immediately.
18153526Sxy150489 		 */
18163526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18173526Sxy150489 	}
18183526Sxy150489 }
18193526Sxy150489 
18203526Sxy150489 /*
18214919Sxy150489  * e1000g_intr - ISR for PCI/PCI-X chipsets
18224919Sxy150489  *
18234919Sxy150489  * This interrupt service routine is for PCI/PCI-X adapters.
18244919Sxy150489  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
18254919Sxy150489  * bit is set or not.
18263526Sxy150489  */
18273526Sxy150489 static uint_t
18283526Sxy150489 e1000g_intr(caddr_t arg)
18293526Sxy150489 {
18303526Sxy150489 	struct e1000g *Adapter;
18314919Sxy150489 	uint32_t icr;
18323526Sxy150489 
18333526Sxy150489 	Adapter = (struct e1000g *)arg;
18344919Sxy150489 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
18354919Sxy150489 
18365273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
18375273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
18385273Sgl147354 
18394919Sxy150489 	if (icr) {
18403526Sxy150489 		/*
18413526Sxy150489 		 * Any bit was set in ICR:
18423526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
18433526Sxy150489 		 * look for work to do.
18443526Sxy150489 		 */
18454919Sxy150489 		e1000g_intr_work(Adapter, icr);
18463526Sxy150489 		return (DDI_INTR_CLAIMED);
18473526Sxy150489 	} else {
18483526Sxy150489 		/*
18493526Sxy150489 		 * No bit was set in ICR:
18503526Sxy150489 		 * Don't claim this interrupt, return immediately.
18513526Sxy150489 		 */
18523526Sxy150489 		return (DDI_INTR_UNCLAIMED);
18533526Sxy150489 	}
18543526Sxy150489 }
18553526Sxy150489 
18563526Sxy150489 /*
18574919Sxy150489  * e1000g_intr_work - actual processing of ISR
18584919Sxy150489  *
18594919Sxy150489  * Read(clear) the ICR contents and call appropriate interrupt
18604919Sxy150489  * processing routines.
18613526Sxy150489  */
18623526Sxy150489 static void
18634919Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
18643526Sxy150489 {
18654919Sxy150489 	rw_enter(&Adapter->chip_lock, RW_READER);
18664919Sxy150489 	/*
18675273Sgl147354 	 * Here we need to check the "chip_state" flag within the chip_lock to
18684919Sxy150489 	 * ensure the receive routine will not execute when the adapter is
18694919Sxy150489 	 * being reset.
18704919Sxy150489 	 */
18715273Sgl147354 	if (Adapter->chip_state != E1000G_START) {
18724919Sxy150489 		rw_exit(&Adapter->chip_lock);
18734919Sxy150489 		return;
18744919Sxy150489 	}
18754919Sxy150489 
18764919Sxy150489 	if (icr & E1000_ICR_RXT0) {
18773526Sxy150489 		mblk_t *mp;
18783526Sxy150489 
18794919Sxy150489 		mutex_enter(&Adapter->rx_ring->rx_lock);
18804919Sxy150489 		mp = e1000g_receive(Adapter);
18814919Sxy150489 		mutex_exit(&Adapter->rx_ring->rx_lock);
18824919Sxy150489 
18834919Sxy150489 		rw_exit(&Adapter->chip_lock);
18844919Sxy150489 
18854919Sxy150489 		if (mp != NULL)
18864919Sxy150489 			mac_rx(Adapter->mh, Adapter->mrh, mp);
18874919Sxy150489 	} else
18884919Sxy150489 		rw_exit(&Adapter->chip_lock);
18893526Sxy150489 
18903526Sxy150489 	/*
18913526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
18923526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
18933526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
18943526Sxy150489 	 * are an indication that cable is not connected.
18953526Sxy150489 	 */
18964919Sxy150489 	if ((icr & E1000_ICR_RXSEQ) ||
18974919Sxy150489 	    (icr & E1000_ICR_LSC) ||
18984919Sxy150489 	    (icr & E1000_ICR_GPI_EN1)) {
18994061Sxy150489 		boolean_t link_changed;
19004061Sxy150489 		timeout_id_t tid = 0;
19013526Sxy150489 
19024919Sxy150489 		stop_watchdog_timer(Adapter);
19034919Sxy150489 
19045082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
19055082Syy150190 
19065082Syy150190 		/*
19075082Syy150190 		 * Because we got a link-status-change interrupt, force
19085082Syy150190 		 * e1000_check_for_link() to look at phy
19095082Syy150190 		 */
19105082Syy150190 		Adapter->shared.mac.get_link_status = B_TRUE;
19115082Syy150190 
19124061Sxy150489 		/* e1000g_link_check takes care of link status change */
19134061Sxy150489 		link_changed = e1000g_link_check(Adapter);
19145082Syy150190 
19155082Syy150190 		/* Get new phy state */
19165082Syy150190 		e1000g_get_phy_state(Adapter);
19175082Syy150190 
19184061Sxy150489 		/*
19194061Sxy150489 		 * If the link timer has not timed out, we'll not notify
19204919Sxy150489 		 * the upper layer with any link state until the link is up.
19214061Sxy150489 		 */
19224061Sxy150489 		if (link_changed && !Adapter->link_complete) {
19234061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
19245082Syy150190 				mutex_enter(&Adapter->link_lock);
19254061Sxy150489 				Adapter->link_complete = B_TRUE;
19264061Sxy150489 				tid = Adapter->link_tid;
19274061Sxy150489 				Adapter->link_tid = 0;
19285082Syy150190 				mutex_exit(&Adapter->link_lock);
19294061Sxy150489 			} else {
19304061Sxy150489 				link_changed = B_FALSE;
19314061Sxy150489 			}
19324061Sxy150489 		}
19335082Syy150190 		rw_exit(&Adapter->chip_lock);
19343526Sxy150489 
19354061Sxy150489 		if (link_changed) {
19364061Sxy150489 			if (tid != 0)
19374061Sxy150489 				(void) untimeout(tid);
19384061Sxy150489 
19394139Sxy150489 			/*
19404139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
19414139Sxy150489 			 * down event. Reset the adapter to recover it.
19424139Sxy150489 			 */
19434139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
19444919Sxy150489 			    (Adapter->shared.mac.type == e1000_80003es2lan))
19454139Sxy150489 				(void) e1000g_reset(Adapter);
19464139Sxy150489 
19474061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
19483526Sxy150489 		}
19493526Sxy150489 
19504919Sxy150489 		start_watchdog_timer(Adapter);
19513526Sxy150489 	}
19523526Sxy150489 
19534919Sxy150489 	if (icr & E1000G_ICR_TX_INTR) {
19544919Sxy150489 		e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
19554919Sxy150489 
19563526Sxy150489 		if (!Adapter->tx_intr_enable)
19574919Sxy150489 			e1000g_clear_tx_interrupt(Adapter);
19583526Sxy150489 		/* Schedule the re-transmit */
19594919Sxy150489 		if (tx_ring->resched_needed) {
19604919Sxy150489 			E1000G_STAT(tx_ring->stat_reschedule);
19614919Sxy150489 			tx_ring->resched_needed = B_FALSE;
19623526Sxy150489 			mac_tx_update(Adapter->mh);
19633526Sxy150489 		}
19643526Sxy150489 		if (Adapter->tx_intr_enable) {
19653526Sxy150489 			/* Recycle the tx descriptors */
19663526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_READER);
19674919Sxy150489 			E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
19684919Sxy150489 			e1000g_recycle(tx_ring);
19693526Sxy150489 			rw_exit(&Adapter->chip_lock);
19703526Sxy150489 			/* Free the recycled messages */
19713526Sxy150489 			ddi_intr_trigger_softint(Adapter->tx_softint_handle,
19723526Sxy150489 			    NULL);
19733526Sxy150489 		}
19743526Sxy150489 	}
19753526Sxy150489 }
19763526Sxy150489 
19773526Sxy150489 static void
19783526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
19793526Sxy150489 {
19803526Sxy150489 	struct e1000_hw *hw;
19813526Sxy150489 	int slot;
19823526Sxy150489 
19834919Sxy150489 	hw = &Adapter->shared;
19843526Sxy150489 
19855273Sgl147354 	if (!Adapter->unicst_init) {
19863526Sxy150489 		/* Initialize the multiple unicast addresses */
19873526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
19883526Sxy150489 
19894919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
19904919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
19913526Sxy150489 			Adapter->unicst_total--;
19923526Sxy150489 
19933526Sxy150489 		Adapter->unicst_avail = Adapter->unicst_total - 1;
19943526Sxy150489 
19953526Sxy150489 		/* Store the default mac address */
19964919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
19974919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
19984919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
19994919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20004919Sxy150489 
20014919Sxy150489 		bcopy(hw->mac.addr, Adapter->unicst_addr[0].mac.addr,
20023526Sxy150489 		    ETHERADDRL);
20033526Sxy150489 		Adapter->unicst_addr[0].mac.set = 1;
20043526Sxy150489 
20053526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20063526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 0;
20075273Sgl147354 
20085273Sgl147354 		Adapter->unicst_init = B_TRUE;
20093526Sxy150489 	} else {
20103526Sxy150489 		/* Recover the default mac address */
20114919Sxy150489 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac.addr,
20123526Sxy150489 		    ETHERADDRL);
20133526Sxy150489 
20143526Sxy150489 		/* Store the default mac address */
20154919Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20164919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20174919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20184919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
20193526Sxy150489 
20203526Sxy150489 		/* Re-configure the RAR registers */
20213526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
20223526Sxy150489 			e1000_rar_set(hw,
20233526Sxy150489 			    Adapter->unicst_addr[slot].mac.addr, slot);
20243526Sxy150489 	}
20255273Sgl147354 
20265273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
20275273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
20283526Sxy150489 }
20293526Sxy150489 
20303526Sxy150489 static int
20313526Sxy150489 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
20323526Sxy150489 {
20333526Sxy150489 	struct e1000g *Adapter;
20343526Sxy150489 
20353526Sxy150489 	Adapter = (struct e1000g *)arg;
20363526Sxy150489 
20373526Sxy150489 	/* Store the default MAC address */
20384919Sxy150489 	bcopy(mac_addr, Adapter->shared.mac.addr, ETHERADDRL);
20393526Sxy150489 
20403526Sxy150489 	/* Set MAC address in address slot 0, which is the default address */
20413526Sxy150489 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
20423526Sxy150489 }
20433526Sxy150489 
20443526Sxy150489 static int
20453526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
20463526Sxy150489     mac_addr_slot_t slot)
20473526Sxy150489 {
20483526Sxy150489 	struct e1000_hw *hw;
20493526Sxy150489 
20504919Sxy150489 	hw = &Adapter->shared;
20513526Sxy150489 
20523526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
20533526Sxy150489 
20544919Sxy150489 #ifndef NO_82542_SUPPORT
20553526Sxy150489 	/*
20563526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
20573526Sxy150489 	 * that requires the receiver to be in reset when any of the
20583526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
20593526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
20603526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
20613526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
20623526Sxy150489 	 * and work around any necessary HW errata.
20633526Sxy150489 	 */
20644919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
20654919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
20663526Sxy150489 		e1000_pci_clear_mwi(hw);
20674919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
20684919Sxy150489 		msec_delay(5);
20693526Sxy150489 	}
20704919Sxy150489 #endif
20713526Sxy150489 
20723526Sxy150489 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
20733526Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
20743526Sxy150489 
20753526Sxy150489 	if (slot == 0) {
20764919Sxy150489 		if ((hw->mac.type == e1000_82571) &&
20774919Sxy150489 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
20784919Sxy150489 			e1000_rar_set(hw, (uint8_t *)mac_addr, LAST_RAR_ENTRY);
20793526Sxy150489 	}
20803526Sxy150489 
20814919Sxy150489 #ifndef NO_82542_SUPPORT
20823526Sxy150489 	/*
20833526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
20843526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
20853526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
20863526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
20873526Sxy150489 	 */
20884919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
20894919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
20904919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
20914919Sxy150489 		msec_delay(1);
20924919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
20933526Sxy150489 			e1000_pci_set_mwi(hw);
20944919Sxy150489 		e1000g_rx_setup(Adapter);
20953526Sxy150489 	}
20964919Sxy150489 #endif
20973526Sxy150489 
20983526Sxy150489 	rw_exit(&Adapter->chip_lock);
20993526Sxy150489 
21005273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
21015273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
21025273Sgl147354 		return (EIO);
21035273Sgl147354 	}
21045273Sgl147354 
21053526Sxy150489 	return (0);
21063526Sxy150489 }
21073526Sxy150489 
21083526Sxy150489 /*
21093526Sxy150489  * e1000g_m_unicst_add() - will find an unused address slot, set the
21103526Sxy150489  * address value to the one specified, reserve that slot and enable
21113526Sxy150489  * the NIC to start filtering on the new MAC address.
21123526Sxy150489  * Returns 0 on success.
21133526Sxy150489  */
21143526Sxy150489 static int
21153526Sxy150489 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
21163526Sxy150489 {
21173526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
21183526Sxy150489 	mac_addr_slot_t slot;
21193526Sxy150489 	int err;
21203526Sxy150489 
21213526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
21223526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
21233526Sxy150489 		return (EINVAL);
21243526Sxy150489 
21253526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21263526Sxy150489 	if (Adapter->unicst_avail == 0) {
21273526Sxy150489 		/* no slots available */
21283526Sxy150489 		rw_exit(&Adapter->chip_lock);
21293526Sxy150489 		return (ENOSPC);
21303526Sxy150489 	}
21313526Sxy150489 
21323526Sxy150489 	/*
21333526Sxy150489 	 * Primary/default address is in slot 0. The next addresses
21343526Sxy150489 	 * are the multiple MAC addresses. So multiple MAC address 0
21353526Sxy150489 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
21363526Sxy150489 	 * MAC address resides in slot 1.
21373526Sxy150489 	 */
21383526Sxy150489 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
21393526Sxy150489 		if (Adapter->unicst_addr[slot].mac.set == 0) {
21403526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
21413526Sxy150489 			break;
21423526Sxy150489 		}
21433526Sxy150489 	}
21443526Sxy150489 
21453526Sxy150489 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
21463526Sxy150489 
21473526Sxy150489 	Adapter->unicst_avail--;
21483526Sxy150489 	rw_exit(&Adapter->chip_lock);
21493526Sxy150489 
21503526Sxy150489 	maddr->mma_slot = slot;
21513526Sxy150489 
21523526Sxy150489 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
21533526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_WRITER);
21543526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
21553526Sxy150489 		Adapter->unicst_avail++;
21563526Sxy150489 		rw_exit(&Adapter->chip_lock);
21573526Sxy150489 	}
21583526Sxy150489 
21593526Sxy150489 	return (err);
21603526Sxy150489 }
21613526Sxy150489 
21623526Sxy150489 /*
21633526Sxy150489  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
21643526Sxy150489  * call to e1000g_m_unicst_add(). The slot number that was returned in
21653526Sxy150489  * e1000g_m_unicst_add() is passed in the call to remove the address.
21663526Sxy150489  * Returns 0 on success.
21673526Sxy150489  */
21683526Sxy150489 static int
21693526Sxy150489 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
21703526Sxy150489 {
21713526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
21723526Sxy150489 	int err;
21733526Sxy150489 
21743526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
21753526Sxy150489 		return (EINVAL);
21763526Sxy150489 
21773526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21783526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
21793526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
21803526Sxy150489 		Adapter->unicst_avail++;
21813526Sxy150489 		rw_exit(&Adapter->chip_lock);
21823526Sxy150489 
21833526Sxy150489 		/* Copy the default address to the passed slot */
21843526Sxy150489 		if (err = e1000g_unicst_set(Adapter,
21853526Sxy150489 		    Adapter->unicst_addr[0].mac.addr, slot) != 0) {
21863526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_WRITER);
21873526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
21883526Sxy150489 			Adapter->unicst_avail--;
21893526Sxy150489 			rw_exit(&Adapter->chip_lock);
21903526Sxy150489 		}
21913526Sxy150489 		return (err);
21923526Sxy150489 	}
21933526Sxy150489 	rw_exit(&Adapter->chip_lock);
21943526Sxy150489 
21953526Sxy150489 	return (EINVAL);
21963526Sxy150489 }
21973526Sxy150489 
21983526Sxy150489 /*
21993526Sxy150489  * e1000g_m_unicst_modify() - modifies the value of an address that
22003526Sxy150489  * has been added by e1000g_m_unicst_add(). The new address, address
22013526Sxy150489  * length and the slot number that was returned in the call to add
22023526Sxy150489  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
22033526Sxy150489  * set to 0. Returns 0 on success.
22043526Sxy150489  */
22053526Sxy150489 static int
22063526Sxy150489 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
22073526Sxy150489 {
22083526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22093526Sxy150489 	mac_addr_slot_t slot;
22103526Sxy150489 
22113526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
22123526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
22133526Sxy150489 		return (EINVAL);
22143526Sxy150489 
22153526Sxy150489 	slot = maddr->mma_slot;
22163526Sxy150489 
22173526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22183526Sxy150489 		return (EINVAL);
22193526Sxy150489 
22203526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22213526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22223526Sxy150489 		rw_exit(&Adapter->chip_lock);
22233526Sxy150489 
22243526Sxy150489 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
22253526Sxy150489 	}
22263526Sxy150489 	rw_exit(&Adapter->chip_lock);
22273526Sxy150489 
22283526Sxy150489 	return (EINVAL);
22293526Sxy150489 }
22303526Sxy150489 
22313526Sxy150489 /*
22323526Sxy150489  * e1000g_m_unicst_get() - will get the MAC address and all other
22333526Sxy150489  * information related to the address slot passed in mac_multi_addr_t.
22343526Sxy150489  * mma_flags should be set to 0 in the call.
22353526Sxy150489  * On return, mma_flags can take the following values:
22363526Sxy150489  * 1) MMAC_SLOT_UNUSED
22373526Sxy150489  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
22383526Sxy150489  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
22393526Sxy150489  * 4) MMAC_SLOT_USED
22403526Sxy150489  */
22413526Sxy150489 static int
22423526Sxy150489 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
22433526Sxy150489 {
22443526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22453526Sxy150489 	mac_addr_slot_t slot;
22463526Sxy150489 
22473526Sxy150489 	slot = maddr->mma_slot;
22483526Sxy150489 
22493526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
22503526Sxy150489 		return (EINVAL);
22513526Sxy150489 
22523526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22533526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
22543526Sxy150489 		bcopy(Adapter->unicst_addr[slot].mac.addr,
22553526Sxy150489 		    maddr->mma_addr, ETHERADDRL);
22563526Sxy150489 		maddr->mma_flags = MMAC_SLOT_USED;
22573526Sxy150489 	} else {
22583526Sxy150489 		maddr->mma_flags = MMAC_SLOT_UNUSED;
22593526Sxy150489 	}
22603526Sxy150489 	rw_exit(&Adapter->chip_lock);
22613526Sxy150489 
22623526Sxy150489 	return (0);
22633526Sxy150489 }
22643526Sxy150489 
22653526Sxy150489 static int
22663526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
22673526Sxy150489 {
22684919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
22693526Sxy150489 	unsigned i;
22703526Sxy150489 	int res = 0;
22713526Sxy150489 
22723526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22733526Sxy150489 
22743526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
22753526Sxy150489 		res = EINVAL;
22763526Sxy150489 		goto done;
22773526Sxy150489 	}
22783526Sxy150489 
22793526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
22803526Sxy150489 		res = ENOENT;
22813526Sxy150489 		goto done;
22823526Sxy150489 	}
22833526Sxy150489 
22843526Sxy150489 	bcopy(multiaddr,
22853526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
22863526Sxy150489 	Adapter->mcast_count++;
22873526Sxy150489 
22883526Sxy150489 	/*
22893526Sxy150489 	 * Update the MC table in the hardware
22903526Sxy150489 	 */
22914919Sxy150489 	e1000g_clear_interrupt(Adapter);
22924919Sxy150489 
22934919Sxy150489 	e1000g_setup_multicast(Adapter);
22944919Sxy150489 
22954919Sxy150489 #ifndef NO_82542_SUPPORT
22964919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
22974919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
22984919Sxy150489 		e1000g_rx_setup(Adapter);
22994919Sxy150489 #endif
23004919Sxy150489 
23014919Sxy150489 	e1000g_mask_interrupt(Adapter);
23023526Sxy150489 
23033526Sxy150489 done:
23043526Sxy150489 	rw_exit(&Adapter->chip_lock);
23055273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23065273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23075273Sgl147354 		res = EIO;
23085273Sgl147354 	}
23095273Sgl147354 
23103526Sxy150489 	return (res);
23113526Sxy150489 }
23123526Sxy150489 
23133526Sxy150489 static int
23143526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
23153526Sxy150489 {
23164919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
23173526Sxy150489 	unsigned i;
23183526Sxy150489 
23193526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
23203526Sxy150489 
23213526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
23223526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
23233526Sxy150489 		    ETHERADDRL) == 0) {
23243526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
23253526Sxy150489 				Adapter->mcast_table[i - 1] =
23263526Sxy150489 				    Adapter->mcast_table[i];
23273526Sxy150489 			}
23283526Sxy150489 			Adapter->mcast_count--;
23293526Sxy150489 			break;
23303526Sxy150489 		}
23313526Sxy150489 	}
23323526Sxy150489 
23333526Sxy150489 	/*
23343526Sxy150489 	 * Update the MC table in the hardware
23353526Sxy150489 	 */
23364919Sxy150489 	e1000g_clear_interrupt(Adapter);
23374919Sxy150489 
23384919Sxy150489 	e1000g_setup_multicast(Adapter);
23394919Sxy150489 
23404919Sxy150489 #ifndef NO_82542_SUPPORT
23414919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
23424919Sxy150489 	    (hw->revision_id == E1000_REVISION_2))
23434919Sxy150489 		e1000g_rx_setup(Adapter);
23444919Sxy150489 #endif
23454919Sxy150489 
23464919Sxy150489 	e1000g_mask_interrupt(Adapter);
23473526Sxy150489 
23483526Sxy150489 done:
23493526Sxy150489 	rw_exit(&Adapter->chip_lock);
23505273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
23515273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
23525273Sgl147354 		return (EIO);
23535273Sgl147354 	}
23545273Sgl147354 
23553526Sxy150489 	return (0);
23563526Sxy150489 }
23573526Sxy150489 
23584919Sxy150489 /*
23594919Sxy150489  * e1000g_setup_multicast - setup multicast data structures
23604919Sxy150489  *
23614919Sxy150489  * This routine initializes all of the multicast related structures.
23624919Sxy150489  */
23634919Sxy150489 void
23644919Sxy150489 e1000g_setup_multicast(struct e1000g *Adapter)
23654919Sxy150489 {
23664919Sxy150489 	uint8_t *mc_addr_list;
23674919Sxy150489 	uint32_t mc_addr_count;
23684919Sxy150489 	uint32_t rctl;
23694919Sxy150489 	struct e1000_hw *hw;
23704919Sxy150489 
23714919Sxy150489 	hw = &Adapter->shared;
23724919Sxy150489 
23734919Sxy150489 	/*
23744919Sxy150489 	 * The e1000g has the ability to do perfect filtering of 16
23754919Sxy150489 	 * addresses. The driver uses one of the e1000g's 16 receive
23764919Sxy150489 	 * address registers for its node/network/mac/individual address.
23774919Sxy150489 	 * So, we have room for up to 15 multicast addresses in the CAM,
23784919Sxy150489 	 * additional MC addresses are handled by the MTA (Multicast Table
23794919Sxy150489 	 * Array)
23804919Sxy150489 	 */
23814919Sxy150489 
23824919Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
23834919Sxy150489 
23844919Sxy150489 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
23854919Sxy150489 
23864919Sxy150489 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
23874919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
23884919Sxy150489 		    "Adapter requested more than %d MC Addresses.\n",
23894919Sxy150489 		    MAX_NUM_MULTICAST_ADDRESSES);
23904919Sxy150489 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
23914919Sxy150489 	} else {
23924919Sxy150489 		/*
23934919Sxy150489 		 * Set the number of MC addresses that we are being
23944919Sxy150489 		 * requested to use
23954919Sxy150489 		 */
23964919Sxy150489 		mc_addr_count = Adapter->mcast_count;
23974919Sxy150489 	}
23984919Sxy150489 #ifndef NO_82542_SUPPORT
23994919Sxy150489 	/*
24004919Sxy150489 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
24014919Sxy150489 	 * hang  while writing to the receive address registers if the receiver
24024919Sxy150489 	 * is not in reset before writing to the registers. Updating the RAR
24034919Sxy150489 	 * is done during the setting up of the multicast table, hence the
24044919Sxy150489 	 * receiver has to be put in reset before updating the multicast table
24054919Sxy150489 	 * and then taken out of reset at the end
24064919Sxy150489 	 */
24074919Sxy150489 	/*
24084919Sxy150489 	 * if WMI was enabled then dis able it before issueing the global
24094919Sxy150489 	 * reset to the hardware.
24104919Sxy150489 	 */
24114919Sxy150489 	/*
24124919Sxy150489 	 * Only required for WISEMAN_2_0
24134919Sxy150489 	 */
24144919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24154919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24164919Sxy150489 		e1000_pci_clear_mwi(hw);
24174919Sxy150489 		/*
24184919Sxy150489 		 * The e1000g must be in reset before changing any RA
24194919Sxy150489 		 * registers. Reset receive unit.  The chip will remain in
24204919Sxy150489 		 * the reset state until software explicitly restarts it.
24214919Sxy150489 		 */
24224919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
24234919Sxy150489 		/* Allow receiver time to go in to reset */
24244919Sxy150489 		msec_delay(5);
24254919Sxy150489 	}
24264919Sxy150489 #endif
24274919Sxy150489 
24284919Sxy150489 	e1000_mc_addr_list_update(hw, mc_addr_list, mc_addr_count,
24294919Sxy150489 	    Adapter->unicst_total, hw->mac.rar_entry_count);
24304919Sxy150489 
24314919Sxy150489 #ifndef NO_82542_SUPPORT
24324919Sxy150489 	/*
24334919Sxy150489 	 * Only for Wiseman_2_0
24344919Sxy150489 	 * If MWI was enabled then re-enable it after issueing (as we
24354919Sxy150489 	 * disabled it up there) the receive reset command.
24364919Sxy150489 	 * Wainwright does not have a receive reset command and only thing
24374919Sxy150489 	 * close to it is global reset which will require tx setup also
24384919Sxy150489 	 */
24394919Sxy150489 	if ((hw->mac.type == e1000_82542) &&
24404919Sxy150489 	    (hw->revision_id == E1000_REVISION_2)) {
24414919Sxy150489 		/*
24424919Sxy150489 		 * if WMI was enabled then reenable it after issueing the
24434919Sxy150489 		 * global or receive reset to the hardware.
24444919Sxy150489 		 */
24454919Sxy150489 
24464919Sxy150489 		/*
24474919Sxy150489 		 * Take receiver out of reset
24484919Sxy150489 		 * clear E1000_RCTL_RST bit (and all others)
24494919Sxy150489 		 */
24504919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
24514919Sxy150489 		msec_delay(5);
24524919Sxy150489 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
24534919Sxy150489 			e1000_pci_set_mwi(hw);
24544919Sxy150489 	}
24554919Sxy150489 #endif
24564919Sxy150489 
24574919Sxy150489 	/*
24584919Sxy150489 	 * Restore original value
24594919Sxy150489 	 */
24604919Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
24614919Sxy150489 }
24624919Sxy150489 
24633526Sxy150489 int
24643526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
24653526Sxy150489 {
24663526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
24673526Sxy150489 
24683526Sxy150489 	return ((add) ? multicst_add(Adapter, addr)
24694349Sxy150489 	    : multicst_remove(Adapter, addr));
24703526Sxy150489 }
24713526Sxy150489 
24723526Sxy150489 int
24733526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
24743526Sxy150489 {
24753526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
24764919Sxy150489 	uint32_t rctl;
24773526Sxy150489 
24783526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
24793526Sxy150489 
24804919Sxy150489 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
24813526Sxy150489 
24823526Sxy150489 	if (on)
24834919Sxy150489 		rctl |=
24843526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
24853526Sxy150489 	else
24864919Sxy150489 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
24874919Sxy150489 
24884919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
24893526Sxy150489 
24903526Sxy150489 	Adapter->e1000g_promisc = on;
24913526Sxy150489 
24923526Sxy150489 	rw_exit(&Adapter->chip_lock);
24933526Sxy150489 
24945273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
24955273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
24965273Sgl147354 		return (EIO);
24975273Sgl147354 	}
24985273Sgl147354 
24993526Sxy150489 	return (0);
25003526Sxy150489 }
25013526Sxy150489 
25023526Sxy150489 static boolean_t
25033526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
25043526Sxy150489 {
25053526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
25064919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
25073526Sxy150489 
25083526Sxy150489 	switch (cap) {
25093526Sxy150489 	case MAC_CAPAB_HCKSUM: {
25103526Sxy150489 		uint32_t *txflags = cap_data;
25113526Sxy150489 		/*
25123526Sxy150489 		 * Checksum on/off selection via global parameters.
25133526Sxy150489 		 *
25143526Sxy150489 		 * If the chip is flagged as not capable of (correctly)
25154919Sxy150489 		 * handling checksumming, we don't enable it on either
25163526Sxy150489 		 * Rx or Tx side.  Otherwise, we take this chip's settings
25173526Sxy150489 		 * from the patchable global defaults.
25183526Sxy150489 		 *
25193526Sxy150489 		 * We advertise our capabilities only if TX offload is
25203526Sxy150489 		 * enabled.  On receive, the stack will accept checksummed
25213526Sxy150489 		 * packets anyway, even if we haven't said we can deliver
25223526Sxy150489 		 * them.
25233526Sxy150489 		 */
25244919Sxy150489 		switch (hw->mac.type) {
25253526Sxy150489 		case e1000_82540:
25264919Sxy150489 		case e1000_82544:
25273526Sxy150489 		case e1000_82545:
25284919Sxy150489 		case e1000_82545_rev_3:
25294919Sxy150489 		case e1000_82546:
25304919Sxy150489 		case e1000_82546_rev_3:
25313526Sxy150489 		case e1000_82571:
25323526Sxy150489 		case e1000_82572:
25333526Sxy150489 		case e1000_82573:
25343526Sxy150489 		case e1000_80003es2lan:
25353526Sxy150489 			*txflags = HCKSUM_IPHDRCKSUM | HCKSUM_INET_PARTIAL;
25363526Sxy150489 			break;
25373526Sxy150489 
25383526Sxy150489 		/*
25394919Sxy150489 		 * For the following Intel PRO/1000 chipsets, we have not
25404919Sxy150489 		 * tested the hardware checksum offload capability, so we
25414919Sxy150489 		 * disable the capability for them.
25424919Sxy150489 		 *	e1000_82542,
25433526Sxy150489 		 *	e1000_82543,
25443526Sxy150489 		 *	e1000_82541,
25453526Sxy150489 		 *	e1000_82541_rev_2,
25463526Sxy150489 		 *	e1000_82547,
25473526Sxy150489 		 *	e1000_82547_rev_2,
25483526Sxy150489 		 */
25493526Sxy150489 		default:
25503526Sxy150489 			return (B_FALSE);
25513526Sxy150489 		}
25523526Sxy150489 
25533526Sxy150489 		break;
25543526Sxy150489 	}
25553526Sxy150489 	case MAC_CAPAB_POLL:
25563526Sxy150489 		/*
25573526Sxy150489 		 * There's nothing for us to fill in, simply returning
25583526Sxy150489 		 * B_TRUE stating that we support polling is sufficient.
25593526Sxy150489 		 */
25603526Sxy150489 		break;
25613526Sxy150489 
25623526Sxy150489 	case MAC_CAPAB_MULTIADDRESS: {
25633526Sxy150489 		multiaddress_capab_t *mmacp = cap_data;
25643526Sxy150489 
25653526Sxy150489 		/*
25663526Sxy150489 		 * The number of MAC addresses made available by
25673526Sxy150489 		 * this capability is one less than the total as
25683526Sxy150489 		 * the primary address in slot 0 is counted in
25693526Sxy150489 		 * the total.
25703526Sxy150489 		 */
25713526Sxy150489 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
25723526Sxy150489 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
25733526Sxy150489 		/* No multiple factory addresses, set mma_flag to 0 */
25743526Sxy150489 		mmacp->maddr_flag = 0;
25753526Sxy150489 		mmacp->maddr_handle = Adapter;
25763526Sxy150489 		mmacp->maddr_add = e1000g_m_unicst_add;
25773526Sxy150489 		mmacp->maddr_remove = e1000g_m_unicst_remove;
25783526Sxy150489 		mmacp->maddr_modify = e1000g_m_unicst_modify;
25793526Sxy150489 		mmacp->maddr_get = e1000g_m_unicst_get;
25803526Sxy150489 		mmacp->maddr_reserve = NULL;
25813526Sxy150489 		break;
25823526Sxy150489 	}
25833526Sxy150489 	default:
25843526Sxy150489 		return (B_FALSE);
25853526Sxy150489 	}
25863526Sxy150489 	return (B_TRUE);
25873526Sxy150489 }
25883526Sxy150489 
25893526Sxy150489 /*
25904919Sxy150489  * e1000g_get_conf - get configurations set in e1000g.conf
25914919Sxy150489  *
25924919Sxy150489  * This routine gets user-configured values out of the configuration
25934919Sxy150489  * file e1000g.conf.
25944919Sxy150489  *
25954919Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
25964919Sxy150489  * default.
25974919Sxy150489  * If user does not configure a value, use the default.
25984919Sxy150489  * If user configures below the minimum, use the minumum.
25994919Sxy150489  * If user configures above the maximum, use the maxumum.
26003526Sxy150489  */
26013526Sxy150489 static void
26024919Sxy150489 e1000g_get_conf(struct e1000g *Adapter)
26033526Sxy150489 {
26044919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
26054919Sxy150489 	boolean_t tbi_compatibility = B_FALSE;
26064919Sxy150489 
26073526Sxy150489 	/*
26083526Sxy150489 	 * get each configurable property from e1000g.conf
26093526Sxy150489 	 */
26103526Sxy150489 
26113526Sxy150489 	/*
26123526Sxy150489 	 * NumTxDescriptors
26133526Sxy150489 	 */
26144919Sxy150489 	Adapter->tx_desc_num =
26154919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
26164919Sxy150489 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
26174919Sxy150489 	    DEFAULT_NUM_TX_DESCRIPTOR);
26183526Sxy150489 
26193526Sxy150489 	/*
26203526Sxy150489 	 * NumRxDescriptors
26213526Sxy150489 	 */
26224919Sxy150489 	Adapter->rx_desc_num =
26234919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
26244919Sxy150489 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
26254919Sxy150489 	    DEFAULT_NUM_RX_DESCRIPTOR);
26263526Sxy150489 
26273526Sxy150489 	/*
26283526Sxy150489 	 * NumRxFreeList
26293526Sxy150489 	 */
26304919Sxy150489 	Adapter->rx_freelist_num =
26314919Sxy150489 	    e1000g_get_prop(Adapter, "NumRxFreeList",
26324919Sxy150489 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
26334919Sxy150489 	    DEFAULT_NUM_RX_FREELIST);
26343526Sxy150489 
26353526Sxy150489 	/*
26363526Sxy150489 	 * NumTxPacketList
26373526Sxy150489 	 */
26384919Sxy150489 	Adapter->tx_freelist_num =
26394919Sxy150489 	    e1000g_get_prop(Adapter, "NumTxPacketList",
26404919Sxy150489 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
26414919Sxy150489 	    DEFAULT_NUM_TX_FREELIST);
26423526Sxy150489 
26433526Sxy150489 	/*
26443526Sxy150489 	 * FlowControl
26453526Sxy150489 	 */
26464919Sxy150489 	hw->mac.fc_send_xon = B_TRUE;
26474919Sxy150489 	hw->mac.fc =
26484919Sxy150489 	    e1000g_get_prop(Adapter, "FlowControl",
26494919Sxy150489 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
26503526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
26514919Sxy150489 	if (hw->mac.fc == 4)
26524919Sxy150489 		hw->mac.fc = e1000_fc_default;
26533526Sxy150489 
26543526Sxy150489 	/*
26554919Sxy150489 	 * Max Num Receive Packets on Interrupt
26563526Sxy150489 	 */
26574919Sxy150489 	Adapter->rx_limit_onintr =
26584919Sxy150489 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
26594919Sxy150489 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
26604919Sxy150489 	    DEFAULT_RX_LIMIT_ON_INTR);
26613526Sxy150489 
26623526Sxy150489 	/*
26633526Sxy150489 	 * PHY master slave setting
26643526Sxy150489 	 */
26654919Sxy150489 	hw->phy.ms_type =
26664919Sxy150489 	    e1000g_get_prop(Adapter, "SetMasterSlave",
26673526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
26683526Sxy150489 	    e1000_ms_hw_default);
26693526Sxy150489 
26703526Sxy150489 	/*
26713526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
26723526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
26733526Sxy150489 	 */
26744919Sxy150489 	tbi_compatibility =
26754919Sxy150489 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
26764919Sxy150489 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
26774919Sxy150489 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
26783526Sxy150489 
26793526Sxy150489 	/*
26803526Sxy150489 	 * MSI Enable
26813526Sxy150489 	 */
26823526Sxy150489 	Adapter->msi_enabled =
26834919Sxy150489 	    e1000g_get_prop(Adapter, "MSIEnable",
26844919Sxy150489 	    0, 1, DEFAULT_MSI_ENABLE);
26853526Sxy150489 
26863526Sxy150489 	/*
26873526Sxy150489 	 * Interrupt Throttling Rate
26883526Sxy150489 	 */
26893526Sxy150489 	Adapter->intr_throttling_rate =
26904919Sxy150489 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
26914919Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
26924919Sxy150489 	    DEFAULT_INTR_THROTTLING);
26933526Sxy150489 
26943526Sxy150489 	/*
26953526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
26963526Sxy150489 	 * It is enabled by default
26973526Sxy150489 	 */
26983526Sxy150489 	Adapter->intr_adaptive =
26994919Sxy150489 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
27003526Sxy150489 	    B_TRUE : B_FALSE;
27013526Sxy150489 }
27023526Sxy150489 
27033526Sxy150489 /*
27044919Sxy150489  * e1000g_get_prop - routine to read properties
27054919Sxy150489  *
27064919Sxy150489  * Get a user-configure property value out of the configuration
27074919Sxy150489  * file e1000g.conf.
27084919Sxy150489  *
27094919Sxy150489  * Caller provides name of the property, a default value, a minimum
27104919Sxy150489  * value, and a maximum value.
27114919Sxy150489  *
27124919Sxy150489  * Return configured value of the property, with default, minimum and
27134919Sxy150489  * maximum properly applied.
27143526Sxy150489  */
27153526Sxy150489 static int
27164919Sxy150489 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
27173526Sxy150489     char *propname,		/* name of the property */
27183526Sxy150489     int minval,			/* minimum acceptable value */
27193526Sxy150489     int maxval,			/* maximim acceptable value */
27203526Sxy150489     int defval)			/* default value */
27213526Sxy150489 {
27223526Sxy150489 	int propval;		/* value returned for requested property */
27233526Sxy150489 	int *props;		/* point to array of properties returned */
27243526Sxy150489 	uint_t nprops;		/* number of property value returned */
27253526Sxy150489 
27263526Sxy150489 	/*
27273526Sxy150489 	 * get the array of properties from the config file
27283526Sxy150489 	 */
27293526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
27303526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
27313526Sxy150489 		/* got some properties, test if we got enough */
27324919Sxy150489 		if (Adapter->instance < nprops) {
27334919Sxy150489 			propval = props[Adapter->instance];
27343526Sxy150489 		} else {
27353526Sxy150489 			/* not enough properties configured */
27363526Sxy150489 			propval = defval;
27374919Sxy150489 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
27383526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
27393526Sxy150489 			    " - set to %d\n",
27403526Sxy150489 			    propname, propval);
27413526Sxy150489 		}
27423526Sxy150489 
27433526Sxy150489 		/* free memory allocated for properties */
27443526Sxy150489 		ddi_prop_free(props);
27453526Sxy150489 
27463526Sxy150489 	} else {
27473526Sxy150489 		propval = defval;
27483526Sxy150489 	}
27493526Sxy150489 
27503526Sxy150489 	/*
27513526Sxy150489 	 * enforce limits
27523526Sxy150489 	 */
27533526Sxy150489 	if (propval > maxval) {
27543526Sxy150489 		propval = maxval;
27554919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
27563526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
27573526Sxy150489 		    propname, propval);
27583526Sxy150489 	}
27593526Sxy150489 
27603526Sxy150489 	if (propval < minval) {
27613526Sxy150489 		propval = minval;
27624919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
27633526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
27643526Sxy150489 		    propname, propval);
27653526Sxy150489 	}
27663526Sxy150489 
27673526Sxy150489 	return (propval);
27683526Sxy150489 }
27693526Sxy150489 
27703526Sxy150489 static boolean_t
27714061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
27723526Sxy150489 {
27734061Sxy150489 	uint16_t speed, duplex, phydata;
27744061Sxy150489 	boolean_t link_changed = B_FALSE;
27753526Sxy150489 	struct e1000_hw *hw;
27763526Sxy150489 	uint32_t reg_tarc;
27773526Sxy150489 
27784919Sxy150489 	hw = &Adapter->shared;
27793526Sxy150489 
27803526Sxy150489 	if (e1000g_link_up(Adapter)) {
27813526Sxy150489 		/*
27823526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
27833526Sxy150489 		 */
27844061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
27854061Sxy150489 			e1000_get_speed_and_duplex(hw, &speed, &duplex);
27864061Sxy150489 			Adapter->link_speed = speed;
27874061Sxy150489 			Adapter->link_duplex = duplex;
27884061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
27894061Sxy150489 			link_changed = B_TRUE;
27904061Sxy150489 
27914061Sxy150489 			Adapter->tx_link_down_timeout = 0;
27924061Sxy150489 
27934919Sxy150489 			if ((hw->mac.type == e1000_82571) ||
27944919Sxy150489 			    (hw->mac.type == e1000_82572)) {
27954919Sxy150489 				reg_tarc = E1000_READ_REG(hw, E1000_TARC0);
27964061Sxy150489 				if (speed == SPEED_1000)
27974061Sxy150489 					reg_tarc |= (1 << 21);
27984061Sxy150489 				else
27994061Sxy150489 					reg_tarc &= ~(1 << 21);
28004919Sxy150489 				E1000_WRITE_REG(hw, E1000_TARC0, reg_tarc);
28013526Sxy150489 			}
28023526Sxy150489 		}
28033526Sxy150489 		Adapter->smartspeed = 0;
28043526Sxy150489 	} else {
28054061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
28063526Sxy150489 			Adapter->link_speed = 0;
28073526Sxy150489 			Adapter->link_duplex = 0;
28084061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
28094061Sxy150489 			link_changed = B_TRUE;
28104061Sxy150489 
28113526Sxy150489 			/*
28123526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
28133526Sxy150489 			 * driver loses link disable auto master/slave
28143526Sxy150489 			 * resolution.
28153526Sxy150489 			 */
28164919Sxy150489 			if (hw->phy.type == e1000_phy_igp) {
28173526Sxy150489 				e1000_read_phy_reg(hw,
28183526Sxy150489 				    PHY_1000T_CTRL, &phydata);
28193526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
28203526Sxy150489 				e1000_write_phy_reg(hw,
28213526Sxy150489 				    PHY_1000T_CTRL, phydata);
28223526Sxy150489 			}
28233526Sxy150489 		} else {
28243526Sxy150489 			e1000g_smartspeed(Adapter);
28253526Sxy150489 		}
28264061Sxy150489 
28275273Sgl147354 		if (Adapter->chip_state == E1000G_START) {
28284061Sxy150489 			if (Adapter->tx_link_down_timeout <
28294061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
28304061Sxy150489 				Adapter->tx_link_down_timeout++;
28314061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
28324061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
28334919Sxy150489 				e1000g_tx_clean(Adapter);
28344061Sxy150489 				Adapter->tx_link_down_timeout++;
28354061Sxy150489 			}
28364061Sxy150489 		}
28373526Sxy150489 	}
28383526Sxy150489 
28395273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
28405273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
28415273Sgl147354 
28424061Sxy150489 	return (link_changed);
28434061Sxy150489 }
28444061Sxy150489 
28454061Sxy150489 static void
28464919Sxy150489 e1000g_local_timer(void *ws)
28474061Sxy150489 {
28484061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
28494061Sxy150489 	struct e1000_hw *hw;
28504061Sxy150489 	e1000g_ether_addr_t ether_addr;
28514061Sxy150489 	boolean_t link_changed;
28524061Sxy150489 
28534919Sxy150489 	hw = &Adapter->shared;
28544919Sxy150489 
28555273Sgl147354 	if (Adapter->chip_state == E1000G_ERROR) {
28565273Sgl147354 		Adapter->reset_count++;
28575273Sgl147354 		if (e1000g_global_reset(Adapter))
28585273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
28595273Sgl147354 			    DDI_SERVICE_RESTORED);
28605273Sgl147354 		else
28615273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
28625273Sgl147354 			    DDI_SERVICE_LOST);
28635273Sgl147354 		return;
28645273Sgl147354 	}
28655273Sgl147354 
28664919Sxy150489 	(void) e1000g_tx_freemsg(Adapter->tx_ring);
28674061Sxy150489 
28684061Sxy150489 	if (e1000g_stall_check(Adapter)) {
28694919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
28704061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
28715273Sgl147354 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
28724061Sxy150489 		Adapter->reset_count++;
28735273Sgl147354 		if (e1000g_reset(Adapter))
28745273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
28755273Sgl147354 			    DDI_SERVICE_RESTORED);
28765273Sgl147354 		else
28775273Sgl147354 			ddi_fm_service_impact(Adapter->dip,
28785273Sgl147354 			    DDI_SERVICE_LOST);
28795273Sgl147354 		return;
28804061Sxy150489 	}
28814061Sxy150489 
28824061Sxy150489 	link_changed = B_FALSE;
28835082Syy150190 	rw_enter(&Adapter->chip_lock, RW_READER);
28844061Sxy150489 	if (Adapter->link_complete)
28854061Sxy150489 		link_changed = e1000g_link_check(Adapter);
28865082Syy150190 	rw_exit(&Adapter->chip_lock);
28874061Sxy150489 
28884139Sxy150489 	if (link_changed) {
28894139Sxy150489 		/*
28904139Sxy150489 		 * Workaround for esb2. Data stuck in fifo on a link
28914139Sxy150489 		 * down event. Reset the adapter to recover it.
28924139Sxy150489 		 */
28934139Sxy150489 		if ((Adapter->link_state == LINK_STATE_DOWN) &&
28944919Sxy150489 		    (hw->mac.type == e1000_80003es2lan))
28954139Sxy150489 			(void) e1000g_reset(Adapter);
28964139Sxy150489 
28974061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
28984139Sxy150489 	}
28994061Sxy150489 
29003526Sxy150489 	/*
29013526Sxy150489 	 * With 82571 controllers, any locally administered address will
29023526Sxy150489 	 * be overwritten when there is a reset on the other port.
29033526Sxy150489 	 * Detect this circumstance and correct it.
29043526Sxy150489 	 */
29054919Sxy150489 	if ((hw->mac.type == e1000_82571) &&
29064919Sxy150489 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
29074919Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
29084919Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
29093526Sxy150489 
29103526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
29113526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
29123526Sxy150489 
29134919Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
29144919Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
29154919Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
29164919Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
29174919Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
29184919Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
29194919Sxy150489 			e1000_rar_set(hw, hw->mac.addr, 0);
29203526Sxy150489 		}
29213526Sxy150489 	}
29223526Sxy150489 
29233526Sxy150489 	/*
29244919Sxy150489 	 * Long TTL workaround for 82541/82547
29253526Sxy150489 	 */
29264919Sxy150489 	e1000_igp_ttl_workaround_82547(hw);
29273526Sxy150489 
29283526Sxy150489 	/*
29293526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
29303526Sxy150489 	 * change the value in steps...
29313526Sxy150489 	 * These properties should only be set for 10/100
29323526Sxy150489 	 */
29333526Sxy150489 	if ((hw->media_type == e1000_media_type_copper) &&
29344061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
29354061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
29363526Sxy150489 		e1000_update_adaptive(hw);
29373526Sxy150489 	}
29383526Sxy150489 	/*
29393526Sxy150489 	 * Set Timer Interrupts
29403526Sxy150489 	 */
29414919Sxy150489 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
29424919Sxy150489 
29435273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
29445273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
29455273Sgl147354 
29464919Sxy150489 	restart_watchdog_timer(Adapter);
29473526Sxy150489 }
29483526Sxy150489 
29494061Sxy150489 /*
29504061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
29514061Sxy150489  * is expired, which indicates the completion of the link setup. The link
29524061Sxy150489  * state will not be updated until the link setup is completed. And the
29534061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
29544061Sxy150489  * in this function. It will be updated in the local timer routine or the
29554061Sxy150489  * interrupt service routine after the interface is started (plumbed).
29564061Sxy150489  */
29573526Sxy150489 static void
29584061Sxy150489 e1000g_link_timer(void *arg)
29593526Sxy150489 {
29604061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
29613526Sxy150489 
29624919Sxy150489 	mutex_enter(&Adapter->link_lock);
29634061Sxy150489 	Adapter->link_complete = B_TRUE;
29644061Sxy150489 	Adapter->link_tid = 0;
29654919Sxy150489 	mutex_exit(&Adapter->link_lock);
29663526Sxy150489 }
29673526Sxy150489 
29683526Sxy150489 /*
29694919Sxy150489  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
29704919Sxy150489  *
29714919Sxy150489  * This function read the forced speed and duplex for 10/100 Mbps speeds
29724919Sxy150489  * and also for 1000 Mbps speeds from the e1000g.conf file
29733526Sxy150489  */
29743526Sxy150489 static void
29753526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
29763526Sxy150489 {
29773526Sxy150489 	int forced;
29784919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
29794919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
29803526Sxy150489 
29813526Sxy150489 	/*
29823526Sxy150489 	 * get value out of config file
29833526Sxy150489 	 */
29844919Sxy150489 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
29853526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
29863526Sxy150489 
29873526Sxy150489 	switch (forced) {
29883526Sxy150489 	case GDIAG_10_HALF:
29893526Sxy150489 		/*
29903526Sxy150489 		 * Disable Auto Negotiation
29913526Sxy150489 		 */
29924919Sxy150489 		mac->autoneg = B_FALSE;
29934919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
29943526Sxy150489 		break;
29953526Sxy150489 	case GDIAG_10_FULL:
29963526Sxy150489 		/*
29973526Sxy150489 		 * Disable Auto Negotiation
29983526Sxy150489 		 */
29994919Sxy150489 		mac->autoneg = B_FALSE;
30004919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
30013526Sxy150489 		break;
30023526Sxy150489 	case GDIAG_100_HALF:
30033526Sxy150489 		/*
30043526Sxy150489 		 * Disable Auto Negotiation
30053526Sxy150489 		 */
30064919Sxy150489 		mac->autoneg = B_FALSE;
30074919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
30083526Sxy150489 		break;
30093526Sxy150489 	case GDIAG_100_FULL:
30103526Sxy150489 		/*
30113526Sxy150489 		 * Disable Auto Negotiation
30123526Sxy150489 		 */
30134919Sxy150489 		mac->autoneg = B_FALSE;
30144919Sxy150489 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
30153526Sxy150489 		break;
30163526Sxy150489 	case GDIAG_1000_FULL:
30173526Sxy150489 		/*
30183526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
30193526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
30203526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
30213526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
30223526Sxy150489 		 * we are allowed to link without any negotiation.
30233526Sxy150489 		 */
30244919Sxy150489 		mac->autoneg = B_TRUE;
30254919Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
30263526Sxy150489 		break;
30273526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
30284919Sxy150489 		mac->autoneg = B_TRUE;
30294919Sxy150489 		phy->autoneg_advertised =
30304919Sxy150489 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
30314349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
30324349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
30333526Sxy150489 		break;
30343526Sxy150489 	}	/* switch */
30353526Sxy150489 }
30363526Sxy150489 
30373526Sxy150489 /*
30384919Sxy150489  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
30394919Sxy150489  *
30404919Sxy150489  * This function reads MaxFrameSize from e1000g.conf
30413526Sxy150489  */
30423526Sxy150489 static void
30433526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
30443526Sxy150489 {
30453526Sxy150489 	int max_frame;
30464919Sxy150489 	struct e1000_mac_info *mac = &Adapter->shared.mac;
30474919Sxy150489 	struct e1000_phy_info *phy = &Adapter->shared.phy;
30483526Sxy150489 
30493526Sxy150489 	/*
30503526Sxy150489 	 * get value out of config file
30513526Sxy150489 	 */
30524919Sxy150489 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
30533526Sxy150489 
30543526Sxy150489 	switch (max_frame) {
30553526Sxy150489 	case 0:
30564919Sxy150489 		mac->max_frame_size = ETHERMAX;
30573526Sxy150489 		break;
30583526Sxy150489 	case 1:
30594919Sxy150489 		mac->max_frame_size = FRAME_SIZE_UPTO_4K;
30603526Sxy150489 		break;
30613526Sxy150489 	case 2:
30624919Sxy150489 		mac->max_frame_size = FRAME_SIZE_UPTO_8K;
30633526Sxy150489 		break;
30643526Sxy150489 	case 3:
30654919Sxy150489 		if (mac->type < e1000_82571)
30664919Sxy150489 			mac->max_frame_size = FRAME_SIZE_UPTO_16K;
30673526Sxy150489 		else
30684919Sxy150489 			mac->max_frame_size = FRAME_SIZE_UPTO_9K;
30693526Sxy150489 		break;
30703526Sxy150489 	default:
30714919Sxy150489 		mac->max_frame_size = ETHERMAX;
30723526Sxy150489 		break;
30733526Sxy150489 	}	/* switch */
30743526Sxy150489 
30753526Sxy150489 	/* ich8 does not do jumbo frames */
30764919Sxy150489 	if (mac->type == e1000_ich8lan) {
30774919Sxy150489 		mac->max_frame_size = ETHERMAX;
30784919Sxy150489 	}
30794919Sxy150489 
30804919Sxy150489 	/* ich9 does not do jumbo frames on one phy type */
30814919Sxy150489 	if ((mac->type == e1000_ich9lan) &&
30824919Sxy150489 	    (phy->type == e1000_phy_ife)) {
30834919Sxy150489 		mac->max_frame_size = ETHERMAX;
30843526Sxy150489 	}
30853526Sxy150489 }
30863526Sxy150489 
30873526Sxy150489 static void
30884919Sxy150489 arm_watchdog_timer(struct e1000g *Adapter)
30893526Sxy150489 {
30904919Sxy150489 	Adapter->watchdog_tid =
30914919Sxy150489 	    timeout(e1000g_local_timer,
30923526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
30933526Sxy150489 }
30944919Sxy150489 #pragma inline(arm_watchdog_timer)
30954919Sxy150489 
30964919Sxy150489 static void
30974919Sxy150489 enable_watchdog_timer(struct e1000g *Adapter)
30984919Sxy150489 {
30994919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
31004919Sxy150489 
31014919Sxy150489 	if (!Adapter->watchdog_timer_enabled) {
31024919Sxy150489 		Adapter->watchdog_timer_enabled = B_TRUE;
31034919Sxy150489 		Adapter->watchdog_timer_started = B_TRUE;
31044919Sxy150489 		arm_watchdog_timer(Adapter);
31054919Sxy150489 	}
31064919Sxy150489 
31074919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
31084919Sxy150489 }
31093526Sxy150489 
31103526Sxy150489 static void
31114919Sxy150489 disable_watchdog_timer(struct e1000g *Adapter)
31123526Sxy150489 {
31133526Sxy150489 	timeout_id_t tid;
31143526Sxy150489 
31154919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
31164919Sxy150489 
31174919Sxy150489 	Adapter->watchdog_timer_enabled = B_FALSE;
31184919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
31194919Sxy150489 	tid = Adapter->watchdog_tid;
31204919Sxy150489 	Adapter->watchdog_tid = 0;
31214919Sxy150489 
31224919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
31233526Sxy150489 
31243526Sxy150489 	if (tid != 0)
31253526Sxy150489 		(void) untimeout(tid);
31263526Sxy150489 }
31273526Sxy150489 
31283526Sxy150489 static void
31294919Sxy150489 start_watchdog_timer(struct e1000g *Adapter)
31303526Sxy150489 {
31314919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
31324919Sxy150489 
31334919Sxy150489 	if (Adapter->watchdog_timer_enabled) {
31344919Sxy150489 		if (!Adapter->watchdog_timer_started) {
31354919Sxy150489 			Adapter->watchdog_timer_started = B_TRUE;
31364919Sxy150489 			arm_watchdog_timer(Adapter);
31373526Sxy150489 		}
31383526Sxy150489 	}
31393526Sxy150489 
31404919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
31414919Sxy150489 }
31424919Sxy150489 
31434919Sxy150489 static void
31444919Sxy150489 restart_watchdog_timer(struct e1000g *Adapter)
31454919Sxy150489 {
31464919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
31474919Sxy150489 
31484919Sxy150489 	if (Adapter->watchdog_timer_started)
31494919Sxy150489 		arm_watchdog_timer(Adapter);
31504919Sxy150489 
31514919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
31523526Sxy150489 }
31533526Sxy150489 
31543526Sxy150489 static void
31554919Sxy150489 stop_watchdog_timer(struct e1000g *Adapter)
31563526Sxy150489 {
31574919Sxy150489 	timeout_id_t tid;
31584919Sxy150489 
31594919Sxy150489 	mutex_enter(&Adapter->watchdog_lock);
31604919Sxy150489 
31614919Sxy150489 	Adapter->watchdog_timer_started = B_FALSE;
31624919Sxy150489 	tid = Adapter->watchdog_tid;
31634919Sxy150489 	Adapter->watchdog_tid = 0;
31644919Sxy150489 
31654919Sxy150489 	mutex_exit(&Adapter->watchdog_lock);
31664919Sxy150489 
31674919Sxy150489 	if (tid != 0)
31684919Sxy150489 		(void) untimeout(tid);
31693526Sxy150489 }
31703526Sxy150489 
31713526Sxy150489 static void
31724919Sxy150489 stop_link_timer(struct e1000g *Adapter)
31733526Sxy150489 {
31743526Sxy150489 	timeout_id_t tid;
31753526Sxy150489 
31764919Sxy150489 	/* Disable the link timer */
31774919Sxy150489 	mutex_enter(&Adapter->link_lock);
31784919Sxy150489 
31794919Sxy150489 	tid = Adapter->link_tid;
31804919Sxy150489 	Adapter->link_tid = 0;
31814919Sxy150489 
31824919Sxy150489 	mutex_exit(&Adapter->link_lock);
31834919Sxy150489 
31844919Sxy150489 	if (tid != 0)
31854919Sxy150489 		(void) untimeout(tid);
31864919Sxy150489 }
31874919Sxy150489 
31884919Sxy150489 static void
31894919Sxy150489 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
31904919Sxy150489 {
31914919Sxy150489 	timeout_id_t tid;
31924919Sxy150489 
31934919Sxy150489 	/* Disable the tx timer for 82547 chipset */
31944919Sxy150489 	mutex_enter(&tx_ring->tx_lock);
31954919Sxy150489 
31964919Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
31974919Sxy150489 	tid = tx_ring->timer_id_82547;
31984919Sxy150489 	tx_ring->timer_id_82547 = 0;
31994919Sxy150489 
32004919Sxy150489 	mutex_exit(&tx_ring->tx_lock);
32013526Sxy150489 
32023526Sxy150489 	if (tid != 0)
32033526Sxy150489 		(void) untimeout(tid);
32043526Sxy150489 }
32053526Sxy150489 
32063526Sxy150489 void
32074919Sxy150489 e1000g_clear_interrupt(struct e1000g *Adapter)
32083526Sxy150489 {
32094919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
32104919Sxy150489 	    0xffffffff & ~E1000_IMS_RXSEQ);
32113526Sxy150489 }
32123526Sxy150489 
32133526Sxy150489 void
32144919Sxy150489 e1000g_mask_interrupt(struct e1000g *Adapter)
32153526Sxy150489 {
32164919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
32173526Sxy150489 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW & ~E1000_IMS_TXQE);
32183526Sxy150489 }
32193526Sxy150489 
32203526Sxy150489 void
32214919Sxy150489 e1000g_clear_all_interrupts(struct e1000g *Adapter)
32223526Sxy150489 {
32234919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
32243526Sxy150489 }
32253526Sxy150489 
32263526Sxy150489 void
32274919Sxy150489 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
32283526Sxy150489 {
32294919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000G_IMS_TX_INTR);
32303526Sxy150489 }
32313526Sxy150489 
32323526Sxy150489 void
32334919Sxy150489 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
32343526Sxy150489 {
32354919Sxy150489 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000G_IMS_TX_INTR);
32363526Sxy150489 }
32373526Sxy150489 
32383526Sxy150489 static void
32394919Sxy150489 e1000g_smartspeed(struct e1000g *Adapter)
32403526Sxy150489 {
32414919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
32423526Sxy150489 	uint16_t phy_status;
32433526Sxy150489 	uint16_t phy_ctrl;
32443526Sxy150489 
32453526Sxy150489 	/*
32463526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
32473526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
32483526Sxy150489 	 */
32494919Sxy150489 	if ((hw->phy.type != e1000_phy_igp) ||
32504919Sxy150489 	    !hw->mac.autoneg ||
32514919Sxy150489 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
32523526Sxy150489 		return;
32533526Sxy150489 
32543526Sxy150489 	/*
32553526Sxy150489 	 * True if this is the first call of this function or after every
32563526Sxy150489 	 * 30 seconds of not having link
32573526Sxy150489 	 */
32584919Sxy150489 	if (Adapter->smartspeed == 0) {
32593526Sxy150489 		/*
32603526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
32613526Sxy150489 		 * assume back-to-back
32623526Sxy150489 		 */
32634919Sxy150489 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
32643526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
32653526Sxy150489 			return;
32663526Sxy150489 
32674919Sxy150489 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
32683526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
32693526Sxy150489 			return;
32703526Sxy150489 		/*
32713526Sxy150489 		 * We're assuming back-2-back because our status register
32723526Sxy150489 		 * insists! there's a fault in the master/slave
32733526Sxy150489 		 * relationship that was "negotiated"
32743526Sxy150489 		 */
32754919Sxy150489 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
32763526Sxy150489 		/*
32773526Sxy150489 		 * Is the phy configured for manual configuration of
32783526Sxy150489 		 * master/slave?
32793526Sxy150489 		 */
32803526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
32813526Sxy150489 			/*
32823526Sxy150489 			 * Yes.  Then disable manual configuration (enable
32833526Sxy150489 			 * auto configuration) of master/slave
32843526Sxy150489 			 */
32853526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
32864919Sxy150489 			e1000_write_phy_reg(hw,
32873526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
32883526Sxy150489 			/*
32893526Sxy150489 			 * Effectively starting the clock
32903526Sxy150489 			 */
32914919Sxy150489 			Adapter->smartspeed++;
32923526Sxy150489 			/*
32933526Sxy150489 			 * Restart autonegotiation
32943526Sxy150489 			 */
32954919Sxy150489 			if (!e1000_phy_setup_autoneg(hw) &&
32964919Sxy150489 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
32973526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
32983526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
32994919Sxy150489 				e1000_write_phy_reg(hw,
33004919Sxy150489 				    PHY_CONTROL, phy_ctrl);
33013526Sxy150489 			}
33023526Sxy150489 		}
33033526Sxy150489 		return;
33043526Sxy150489 		/*
33053526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
33063526Sxy150489 		 * you should reset the smartspeed counter once you obtain
33073526Sxy150489 		 * link
33083526Sxy150489 		 */
33094919Sxy150489 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
33103526Sxy150489 		/*
33113526Sxy150489 		 * Yes.  Remember, we did at the start determine that
33123526Sxy150489 		 * there's a master/slave configuration fault, so we're
33133526Sxy150489 		 * still assuming there's someone on the other end, but we
33143526Sxy150489 		 * just haven't yet been able to talk to it. We then
33153526Sxy150489 		 * re-enable auto configuration of master/slave to see if
33163526Sxy150489 		 * we're running 2/3 pair cables.
33173526Sxy150489 		 */
33183526Sxy150489 		/*
33193526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
33203526Sxy150489 		 */
33214919Sxy150489 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
33223526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
33234919Sxy150489 		e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
33243526Sxy150489 		/*
33253526Sxy150489 		 * Restart autoneg with phy enabled for manual
33263526Sxy150489 		 * configuration of master/slave
33273526Sxy150489 		 */
33284919Sxy150489 		if (!e1000_phy_setup_autoneg(hw) &&
33294919Sxy150489 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
33303526Sxy150489 			phy_ctrl |=
33313526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
33324919Sxy150489 			e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
33333526Sxy150489 		}
33343526Sxy150489 		/*
33353526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
33363526Sxy150489 		 * link as a result.
33373526Sxy150489 		 */
33383526Sxy150489 	}
33393526Sxy150489 	/*
33403526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
33413526Sxy150489 	 * seconds)
33423526Sxy150489 	 */
33434919Sxy150489 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
33444919Sxy150489 		Adapter->smartspeed = 0;
33453526Sxy150489 }
33463526Sxy150489 
33473526Sxy150489 static boolean_t
33483526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
33493526Sxy150489 {
33503526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
33513526Sxy150489 	const uint8_t addr_test2[6] =
33523526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
33533526Sxy150489 
33543526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
33553526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
33563526Sxy150489 		return (B_FALSE);
33573526Sxy150489 
33583526Sxy150489 	return (B_TRUE);
33593526Sxy150489 }
33603526Sxy150489 
33613526Sxy150489 /*
33624919Sxy150489  * e1000g_stall_check - check for tx stall
33634919Sxy150489  *
33644919Sxy150489  * This function checks if the adapter is stalled (in transmit).
33654919Sxy150489  *
33664919Sxy150489  * It is called each time the watchdog timeout is invoked.
33674919Sxy150489  * If the transmit descriptor reclaim continuously fails,
33684919Sxy150489  * the watchdog value will increment by 1. If the watchdog
33694919Sxy150489  * value exceeds the threshold, the adapter is assumed to
33704919Sxy150489  * have stalled and need to be reset.
33713526Sxy150489  */
33723526Sxy150489 static boolean_t
33733526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
33743526Sxy150489 {
33754919Sxy150489 	e1000g_tx_ring_t *tx_ring;
33764919Sxy150489 
33774919Sxy150489 	tx_ring = Adapter->tx_ring;
33784919Sxy150489 
33794061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
33803526Sxy150489 		return (B_FALSE);
33813526Sxy150489 
33824919Sxy150489 	if (tx_ring->recycle_fail > 0)
33834919Sxy150489 		tx_ring->stall_watchdog++;
33843526Sxy150489 	else
33854919Sxy150489 		tx_ring->stall_watchdog = 0;
33864919Sxy150489 
33874919Sxy150489 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
33883526Sxy150489 		return (B_FALSE);
33893526Sxy150489 
33904919Sxy150489 	tx_ring->stall_watchdog = 0;
33914919Sxy150489 	tx_ring->recycle_fail = 0;
33924919Sxy150489 
33933526Sxy150489 	return (B_TRUE);
33943526Sxy150489 }
33953526Sxy150489 
33964919Sxy150489 #ifdef E1000G_DEBUG
33973526Sxy150489 static enum ioc_reply
33983526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
33993526Sxy150489 {
34003526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
34013526Sxy150489 	e1000g_peekpoke_t *ppd;
34023526Sxy150489 	uint64_t mem_va;
34033526Sxy150489 	uint64_t maxoff;
34043526Sxy150489 	boolean_t peek;
34053526Sxy150489 
34063526Sxy150489 	switch (iocp->ioc_cmd) {
34073526Sxy150489 
34083526Sxy150489 	case E1000G_IOC_REG_PEEK:
34093526Sxy150489 		peek = B_TRUE;
34103526Sxy150489 		break;
34113526Sxy150489 
34123526Sxy150489 	case E1000G_IOC_REG_POKE:
34133526Sxy150489 		peek = B_FALSE;
34143526Sxy150489 		break;
34153526Sxy150489 
34163526Sxy150489 	deault:
34174919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
34184349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
34194349Sxy150489 		    iocp->ioc_cmd);
34203526Sxy150489 		return (IOC_INVAL);
34213526Sxy150489 	}
34223526Sxy150489 
34233526Sxy150489 	/*
34243526Sxy150489 	 * Validate format of ioctl
34253526Sxy150489 	 */
34263526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
34273526Sxy150489 		return (IOC_INVAL);
34283526Sxy150489 	if (mp->b_cont == NULL)
34293526Sxy150489 		return (IOC_INVAL);
34303526Sxy150489 
34313526Sxy150489 	ppd = (e1000g_peekpoke_t *)mp->b_cont->b_rptr;
34323526Sxy150489 
34333526Sxy150489 	/*
34343526Sxy150489 	 * Validate request parameters
34353526Sxy150489 	 */
34363526Sxy150489 	switch (ppd->pp_acc_space) {
34373526Sxy150489 
34383526Sxy150489 	default:
34394919Sxy150489 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
34404349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
34414349Sxy150489 		    ppd->pp_acc_space);
34423526Sxy150489 		return (IOC_INVAL);
34433526Sxy150489 
34443526Sxy150489 	case E1000G_PP_SPACE_REG:
34453526Sxy150489 		/*
34463526Sxy150489 		 * Memory-mapped I/O space
34473526Sxy150489 		 */
34483526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
34493526Sxy150489 		if (ppd->pp_acc_size != 4)
34503526Sxy150489 			return (IOC_INVAL);
34513526Sxy150489 
34523526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
34533526Sxy150489 			return (IOC_INVAL);
34543526Sxy150489 
34553526Sxy150489 		mem_va = 0;
34563526Sxy150489 		maxoff = 0x10000;
34573526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
34583526Sxy150489 		break;
34593526Sxy150489 
34603526Sxy150489 	case E1000G_PP_SPACE_E1000G:
34613526Sxy150489 		/*
34623526Sxy150489 		 * E1000g data structure!
34633526Sxy150489 		 */
34643526Sxy150489 		mem_va = (uintptr_t)e1000gp;
34653526Sxy150489 		maxoff = sizeof (struct e1000g);
34663526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
34673526Sxy150489 		break;
34683526Sxy150489 
34693526Sxy150489 	}
34703526Sxy150489 
34713526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
34723526Sxy150489 		return (IOC_INVAL);
34733526Sxy150489 
34743526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
34753526Sxy150489 		return (IOC_INVAL);
34763526Sxy150489 
34773526Sxy150489 	/*
34783526Sxy150489 	 * All OK - go!
34793526Sxy150489 	 */
34803526Sxy150489 	ppd->pp_acc_offset += mem_va;
34813526Sxy150489 	(*ppfn)(e1000gp, ppd);
34823526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
34833526Sxy150489 }
34843526Sxy150489 
34853526Sxy150489 static void
34863526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
34873526Sxy150489 {
34883526Sxy150489 	ddi_acc_handle_t handle;
34893526Sxy150489 	uint32_t *regaddr;
34903526Sxy150489 
34914919Sxy150489 	handle = e1000gp->osdep.reg_handle;
34923526Sxy150489 	regaddr =
34934919Sxy150489 	    (uint32_t *)(e1000gp->shared.hw_addr + ppd->pp_acc_offset);
34943526Sxy150489 
34953526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
34963526Sxy150489 }
34973526Sxy150489 
34983526Sxy150489 static void
34993526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
35003526Sxy150489 {
35013526Sxy150489 	ddi_acc_handle_t handle;
35023526Sxy150489 	uint32_t *regaddr;
35033526Sxy150489 	uint32_t value;
35043526Sxy150489 
35054919Sxy150489 	handle = e1000gp->osdep.reg_handle;
35063526Sxy150489 	regaddr =
35074919Sxy150489 	    (uint32_t *)(e1000gp->shared.hw_addr + ppd->pp_acc_offset);
35083526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
35093526Sxy150489 
35103526Sxy150489 	ddi_put32(handle, regaddr, value);
35113526Sxy150489 }
35123526Sxy150489 
35133526Sxy150489 static void
35143526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
35153526Sxy150489 {
35163526Sxy150489 	uint64_t value;
35173526Sxy150489 	void *vaddr;
35183526Sxy150489 
35193526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
35203526Sxy150489 
35213526Sxy150489 	switch (ppd->pp_acc_size) {
35223526Sxy150489 	case 1:
35233526Sxy150489 		value = *(uint8_t *)vaddr;
35243526Sxy150489 		break;
35253526Sxy150489 
35263526Sxy150489 	case 2:
35273526Sxy150489 		value = *(uint16_t *)vaddr;
35283526Sxy150489 		break;
35293526Sxy150489 
35303526Sxy150489 	case 4:
35313526Sxy150489 		value = *(uint32_t *)vaddr;
35323526Sxy150489 		break;
35333526Sxy150489 
35343526Sxy150489 	case 8:
35353526Sxy150489 		value = *(uint64_t *)vaddr;
35363526Sxy150489 		break;
35373526Sxy150489 	}
35383526Sxy150489 
35394919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
35404349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
35414349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
35423526Sxy150489 
35433526Sxy150489 	ppd->pp_acc_data = value;
35443526Sxy150489 }
35453526Sxy150489 
35463526Sxy150489 static void
35473526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
35483526Sxy150489 {
35493526Sxy150489 	uint64_t value;
35503526Sxy150489 	void *vaddr;
35513526Sxy150489 
35523526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
35533526Sxy150489 	value = ppd->pp_acc_data;
35543526Sxy150489 
35554919Sxy150489 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
35564349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
35574349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
35583526Sxy150489 
35593526Sxy150489 	switch (ppd->pp_acc_size) {
35603526Sxy150489 	case 1:
35613526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
35623526Sxy150489 		break;
35633526Sxy150489 
35643526Sxy150489 	case 2:
35653526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
35663526Sxy150489 		break;
35673526Sxy150489 
35683526Sxy150489 	case 4:
35693526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
35703526Sxy150489 		break;
35713526Sxy150489 
35723526Sxy150489 	case 8:
35733526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
35743526Sxy150489 		break;
35753526Sxy150489 	}
35763526Sxy150489 }
35774919Sxy150489 #endif
35783526Sxy150489 
35793526Sxy150489 /*
35803526Sxy150489  * Loopback Support
35813526Sxy150489  */
35823526Sxy150489 static lb_property_t lb_normal =
35833526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
35843526Sxy150489 static lb_property_t lb_external1000 =
35853526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
35863526Sxy150489 static lb_property_t lb_external100 =
35873526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
35883526Sxy150489 static lb_property_t lb_external10 =
35893526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
35903526Sxy150489 static lb_property_t lb_phy =
35913526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
35923526Sxy150489 
35933526Sxy150489 static enum ioc_reply
35943526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
35953526Sxy150489 {
35963526Sxy150489 	lb_info_sz_t *lbsp;
35973526Sxy150489 	lb_property_t *lbpp;
35983526Sxy150489 	struct e1000_hw *hw;
35993526Sxy150489 	uint32_t *lbmp;
36003526Sxy150489 	uint32_t size;
36013526Sxy150489 	uint32_t value;
36023526Sxy150489 
36034919Sxy150489 	hw = &Adapter->shared;
36043526Sxy150489 
36053526Sxy150489 	if (mp->b_cont == NULL)
36063526Sxy150489 		return (IOC_INVAL);
36073526Sxy150489 
36083526Sxy150489 	switch (iocp->ioc_cmd) {
36093526Sxy150489 	default:
36103526Sxy150489 		return (IOC_INVAL);
36113526Sxy150489 
36123526Sxy150489 	case LB_GET_INFO_SIZE:
36133526Sxy150489 		size = sizeof (lb_info_sz_t);
36143526Sxy150489 		if (iocp->ioc_count != size)
36153526Sxy150489 			return (IOC_INVAL);
36163526Sxy150489 
36175082Syy150190 		rw_enter(&Adapter->chip_lock, RW_WRITER);
36185082Syy150190 		e1000g_get_phy_state(Adapter);
36195082Syy150190 
36205082Syy150190 		/*
36215082Syy150190 		 * Workaround for hardware faults. In order to get a stable
36225082Syy150190 		 * state of phy, we will wait for a specific interval and
36235082Syy150190 		 * try again. The time delay is an experiential value based
36245082Syy150190 		 * on our testing.
36255082Syy150190 		 */
36265082Syy150190 		msec_delay(100);
36275082Syy150190 		e1000g_get_phy_state(Adapter);
36285082Syy150190 		rw_exit(&Adapter->chip_lock);
36293526Sxy150489 
36303526Sxy150489 		value = sizeof (lb_normal);
36315082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
36325082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
36333526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
36343526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
36353526Sxy150489 			value += sizeof (lb_phy);
36364919Sxy150489 			switch (hw->mac.type) {
36373526Sxy150489 			case e1000_82571:
36383526Sxy150489 			case e1000_82572:
36393526Sxy150489 				value += sizeof (lb_external1000);
36403526Sxy150489 				break;
36413526Sxy150489 			}
36423526Sxy150489 		}
36435082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
36445082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
36453526Sxy150489 			value += sizeof (lb_external100);
36465082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
36473526Sxy150489 			value += sizeof (lb_external10);
36483526Sxy150489 
36493526Sxy150489 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
36503526Sxy150489 		*lbsp = value;
36513526Sxy150489 		break;
36523526Sxy150489 
36533526Sxy150489 	case LB_GET_INFO:
36543526Sxy150489 		value = sizeof (lb_normal);
36555082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
36565082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
36573526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
36583526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
36593526Sxy150489 			value += sizeof (lb_phy);
36604919Sxy150489 			switch (hw->mac.type) {
36613526Sxy150489 			case e1000_82571:
36623526Sxy150489 			case e1000_82572:
36633526Sxy150489 				value += sizeof (lb_external1000);
36643526Sxy150489 				break;
36653526Sxy150489 			}
36663526Sxy150489 		}
36675082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
36685082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
36693526Sxy150489 			value += sizeof (lb_external100);
36705082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
36713526Sxy150489 			value += sizeof (lb_external10);
36723526Sxy150489 
36733526Sxy150489 		size = value;
36743526Sxy150489 		if (iocp->ioc_count != size)
36753526Sxy150489 			return (IOC_INVAL);
36763526Sxy150489 
36773526Sxy150489 		value = 0;
36783526Sxy150489 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
36793526Sxy150489 		lbpp[value++] = lb_normal;
36805082Syy150190 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
36815082Syy150190 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
36823526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
36833526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
36843526Sxy150489 			lbpp[value++] = lb_phy;
36854919Sxy150489 			switch (hw->mac.type) {
36863526Sxy150489 			case e1000_82571:
36873526Sxy150489 			case e1000_82572:
36883526Sxy150489 				lbpp[value++] = lb_external1000;
36893526Sxy150489 				break;
36903526Sxy150489 			}
36913526Sxy150489 		}
36925082Syy150190 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
36935082Syy150190 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
36943526Sxy150489 			lbpp[value++] = lb_external100;
36955082Syy150190 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
36963526Sxy150489 			lbpp[value++] = lb_external10;
36973526Sxy150489 		break;
36983526Sxy150489 
36993526Sxy150489 	case LB_GET_MODE:
37003526Sxy150489 		size = sizeof (uint32_t);
37013526Sxy150489 		if (iocp->ioc_count != size)
37023526Sxy150489 			return (IOC_INVAL);
37033526Sxy150489 
37043526Sxy150489 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
37053526Sxy150489 		*lbmp = Adapter->loopback_mode;
37063526Sxy150489 		break;
37073526Sxy150489 
37083526Sxy150489 	case LB_SET_MODE:
37093526Sxy150489 		size = 0;
37103526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
37113526Sxy150489 			return (IOC_INVAL);
37123526Sxy150489 
37133526Sxy150489 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
37143526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
37153526Sxy150489 			return (IOC_INVAL);
37163526Sxy150489 		break;
37173526Sxy150489 	}
37183526Sxy150489 
37193526Sxy150489 	iocp->ioc_count = size;
37203526Sxy150489 	iocp->ioc_error = 0;
37213526Sxy150489 
37225273Sgl147354 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
37235273Sgl147354 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
37245273Sgl147354 		return (IOC_INVAL);
37255273Sgl147354 	}
37265273Sgl147354 
37273526Sxy150489 	return (IOC_REPLY);
37283526Sxy150489 }
37293526Sxy150489 
37303526Sxy150489 static boolean_t
37313526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
37323526Sxy150489 {
37333526Sxy150489 	struct e1000_hw *hw;
37343526Sxy150489 	int i, times;
37355082Syy150190 	boolean_t link_up;
37363526Sxy150489 
37373526Sxy150489 	if (mode == Adapter->loopback_mode)
37383526Sxy150489 		return (B_TRUE);
37393526Sxy150489 
37404919Sxy150489 	hw = &Adapter->shared;
37413526Sxy150489 	times = 0;
37423526Sxy150489 
37435082Syy150190 	Adapter->loopback_mode = mode;
37445082Syy150190 
37455082Syy150190 	if (mode == E1000G_LB_NONE) {
37463526Sxy150489 		/* Reset the chip */
37474919Sxy150489 		hw->phy.wait_for_link = B_TRUE;
37483526Sxy150489 		(void) e1000g_reset(Adapter);
37494919Sxy150489 		hw->phy.wait_for_link = B_FALSE;
37505082Syy150190 		return (B_TRUE);
37515082Syy150190 	}
37525082Syy150190 
37535082Syy150190 again:
37545082Syy150190 
37555082Syy150190 	rw_enter(&Adapter->chip_lock, RW_WRITER);
37565082Syy150190 
37575082Syy150190 	switch (mode) {
37585082Syy150190 	default:
37595082Syy150190 		rw_exit(&Adapter->chip_lock);
37605082Syy150190 		return (B_FALSE);
37613526Sxy150489 
37623526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
37633526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
37643526Sxy150489 		break;
37653526Sxy150489 
37663526Sxy150489 	case E1000G_LB_EXTERNAL_100:
37673526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
37683526Sxy150489 		break;
37693526Sxy150489 
37703526Sxy150489 	case E1000G_LB_EXTERNAL_10:
37713526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
37723526Sxy150489 		break;
37733526Sxy150489 
37743526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
37753526Sxy150489 		e1000g_set_internal_loopback(Adapter);
37763526Sxy150489 		break;
37773526Sxy150489 	}
37783526Sxy150489 
37793526Sxy150489 	times++;
37803526Sxy150489 
3781*5858Scc210113 	rw_exit(&Adapter->chip_lock);
3782*5858Scc210113 
37835082Syy150190 	/* Wait for link up */
37845082Syy150190 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
37855082Syy150190 		msec_delay(100);
37865082Syy150190 
3787*5858Scc210113 	rw_enter(&Adapter->chip_lock, RW_WRITER);
3788*5858Scc210113 
37895082Syy150190 	link_up = e1000g_link_up(Adapter);
37905082Syy150190 
37915082Syy150190 	rw_exit(&Adapter->chip_lock);
37925082Syy150190 
37935082Syy150190 	if (!link_up) {
37945082Syy150190 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
37955082Syy150190 		    "Failed to get the link up");
37965082Syy150190 		if (times < 2) {
37975082Syy150190 			/* Reset the link */
37984919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
37995082Syy150190 			    "Reset the link ...");
38005082Syy150190 			(void) e1000g_reset(Adapter);
38015082Syy150190 			goto again;
38023526Sxy150489 		}
38033526Sxy150489 	}
38043526Sxy150489 
38053526Sxy150489 	return (B_TRUE);
38063526Sxy150489 }
38073526Sxy150489 
38083526Sxy150489 /*
38093526Sxy150489  * The following loopback settings are from Intel's technical
38103526Sxy150489  * document - "How To Loopback". All the register settings and
38113526Sxy150489  * time delay values are directly inherited from the document
38123526Sxy150489  * without more explanations available.
38133526Sxy150489  */
38143526Sxy150489 static void
38153526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
38163526Sxy150489 {
38173526Sxy150489 	struct e1000_hw *hw;
38183526Sxy150489 	uint32_t ctrl;
38193526Sxy150489 	uint32_t status;
38203526Sxy150489 	uint16_t phy_ctrl;
38215082Syy150190 	uint32_t txcw;
38223526Sxy150489 
38234919Sxy150489 	hw = &Adapter->shared;
38243526Sxy150489 
38253526Sxy150489 	/* Disable Smart Power Down */
38263526Sxy150489 	phy_spd_state(hw, B_FALSE);
38273526Sxy150489 
38284919Sxy150489 	e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
38293526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
38303526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
38313526Sxy150489 
38324919Sxy150489 	switch (hw->mac.type) {
38333526Sxy150489 	case e1000_82540:
38343526Sxy150489 	case e1000_82545:
38353526Sxy150489 	case e1000_82545_rev_3:
38363526Sxy150489 	case e1000_82546:
38373526Sxy150489 	case e1000_82546_rev_3:
38383526Sxy150489 	case e1000_82573:
38393526Sxy150489 		/* Auto-MDI/MDIX off */
38403526Sxy150489 		e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
38413526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
38424919Sxy150489 		e1000_write_phy_reg(hw, PHY_CONTROL,
38434349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
38443526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
38454919Sxy150489 		e1000_write_phy_reg(hw, PHY_CONTROL,
38464349Sxy150489 		    phy_ctrl | MII_CR_RESET);
38475082Syy150190 		/*
38485082Syy150190 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
38495082Syy150190 		 * See comments above e1000g_set_internal_loopback() for the
38505082Syy150190 		 * background.
38515082Syy150190 		 */
38525082Syy150190 		e1000_write_phy_reg(hw, 29, 0x001F);
38535082Syy150190 		e1000_write_phy_reg(hw, 30, 0x8FFC);
38545082Syy150190 		e1000_write_phy_reg(hw, 29, 0x001A);
38555082Syy150190 		e1000_write_phy_reg(hw, 30, 0x8FF0);
38563526Sxy150489 		break;
38573526Sxy150489 	}
38583526Sxy150489 
38593526Sxy150489 	/* Set loopback */
38604919Sxy150489 	e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
38613526Sxy150489 
38623526Sxy150489 	msec_delay(250);
38633526Sxy150489 
38643526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
38654919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
38663526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
38673526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
38684349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
38694349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
38704349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
38713526Sxy150489 
38724919Sxy150489 	switch (hw->mac.type) {
38733526Sxy150489 	case e1000_82540:
38743526Sxy150489 	case e1000_82545:
38753526Sxy150489 	case e1000_82545_rev_3:
38763526Sxy150489 	case e1000_82546:
38773526Sxy150489 	case e1000_82546_rev_3:
38783526Sxy150489 		/*
38793526Sxy150489 		 * For some serdes we'll need to commit the writes now
38803526Sxy150489 		 * so that the status is updated on link
38813526Sxy150489 		 */
38823526Sxy150489 		if (hw->media_type == e1000_media_type_internal_serdes) {
38834919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
38843526Sxy150489 			msec_delay(100);
38854919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
38863526Sxy150489 		}
38873526Sxy150489 
38883526Sxy150489 		if (hw->media_type == e1000_media_type_copper) {
38893526Sxy150489 			/* Invert Loss of Signal */
38903526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
38913526Sxy150489 		} else {
38923526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
38934919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
38943526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
38953526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
38963526Sxy150489 		}
38973526Sxy150489 		break;
38983526Sxy150489 
38993526Sxy150489 	case e1000_82571:
39003526Sxy150489 	case e1000_82572:
39015082Syy150190 		/*
39025082Syy150190 		 * The fiber/SerDes versions of this adapter do not contain an
39035082Syy150190 		 * accessible PHY. Therefore, loopback beyond MAC must be done
39045082Syy150190 		 * using SerDes analog loopback.
39055082Syy150190 		 */
39063526Sxy150489 		if (hw->media_type != e1000_media_type_copper) {
39074919Sxy150489 			status = E1000_READ_REG(hw, E1000_STATUS);
39085082Syy150190 			/* Set ILOS on fiber nic if half duplex is detected */
39095082Syy150190 			if (((status & E1000_STATUS_LU) == 0) ||
39105082Syy150190 			    ((status & E1000_STATUS_FD) == 0) ||
39115082Syy150190 			    (hw->media_type ==
39125082Syy150190 			    e1000_media_type_internal_serdes))
39133526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
39145082Syy150190 
39155082Syy150190 			/* Disable autoneg by setting bit 31 of TXCW to zero */
39165082Syy150190 			txcw = E1000_READ_REG(hw, E1000_TXCW);
39175082Syy150190 			txcw &= ~((uint32_t)1 << 31);
39185082Syy150190 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
39195082Syy150190 
39205082Syy150190 			/*
39215082Syy150190 			 * Write 0x410 to Serdes Control register
39225082Syy150190 			 * to enable Serdes analog loopback
39235082Syy150190 			 */
39245082Syy150190 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
39255082Syy150190 			msec_delay(10);
39263526Sxy150489 		}
39273526Sxy150489 		break;
39283526Sxy150489 
39293526Sxy150489 	case e1000_82573:
39303526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
39313526Sxy150489 		break;
39323526Sxy150489 	}
39333526Sxy150489 
39344919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
39353526Sxy150489 
39363526Sxy150489 }
39373526Sxy150489 
39383526Sxy150489 static void
39393526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
39403526Sxy150489 {
39413526Sxy150489 	struct e1000_hw *hw;
39423526Sxy150489 	uint32_t rctl;
39433526Sxy150489 	uint32_t ctrl_ext;
39443526Sxy150489 	uint32_t ctrl;
39453526Sxy150489 	uint32_t status;
39463526Sxy150489 	uint32_t txcw;
39473526Sxy150489 
39484919Sxy150489 	hw = &Adapter->shared;
39493526Sxy150489 
39503526Sxy150489 	/* Disable Smart Power Down */
39513526Sxy150489 	phy_spd_state(hw, B_FALSE);
39523526Sxy150489 
39533526Sxy150489 	switch (hw->media_type) {
39543526Sxy150489 	case e1000_media_type_copper:
39553526Sxy150489 		/* Force link up (Must be done before the PHY writes) */
39564919Sxy150489 		ctrl = E1000_READ_REG(hw, E1000_CTRL);
39573526Sxy150489 		ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
39584919Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
39594919Sxy150489 
39604919Sxy150489 		rctl = E1000_READ_REG(hw, E1000_RCTL);
39613526Sxy150489 		rctl |= (E1000_RCTL_EN |
39624349Sxy150489 		    E1000_RCTL_SBP |
39634349Sxy150489 		    E1000_RCTL_UPE |
39644349Sxy150489 		    E1000_RCTL_MPE |
39654349Sxy150489 		    E1000_RCTL_LPE |
39664349Sxy150489 		    E1000_RCTL_BAM);		/* 0x803E */
39674919Sxy150489 		E1000_WRITE_REG(hw, E1000_RCTL, rctl);
39684919Sxy150489 
39694919Sxy150489 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
39703526Sxy150489 		ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
39714349Sxy150489 		    E1000_CTRL_EXT_SDP6_DATA |
39724349Sxy150489 		    E1000_CTRL_EXT_SDP7_DATA |
39734349Sxy150489 		    E1000_CTRL_EXT_SDP4_DIR |
39744349Sxy150489 		    E1000_CTRL_EXT_SDP6_DIR |
39754349Sxy150489 		    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
39764919Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
39773526Sxy150489 
39783526Sxy150489 		/*
39793526Sxy150489 		 * This sequence tunes the PHY's SDP and no customer
39803526Sxy150489 		 * settable values. For background, see comments above
39813526Sxy150489 		 * e1000g_set_internal_loopback().
39823526Sxy150489 		 */
39833526Sxy150489 		e1000_write_phy_reg(hw, 0x0, 0x140);
39843526Sxy150489 		msec_delay(10);
39853526Sxy150489 		e1000_write_phy_reg(hw, 0x9, 0x1A00);
39863526Sxy150489 		e1000_write_phy_reg(hw, 0x12, 0xC10);
39873526Sxy150489 		e1000_write_phy_reg(hw, 0x12, 0x1C10);
39883526Sxy150489 		e1000_write_phy_reg(hw, 0x1F37, 0x76);
39893526Sxy150489 		e1000_write_phy_reg(hw, 0x1F33, 0x1);
39903526Sxy150489 		e1000_write_phy_reg(hw, 0x1F33, 0x0);
39913526Sxy150489 
39923526Sxy150489 		e1000_write_phy_reg(hw, 0x1F35, 0x65);
39933526Sxy150489 		e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
39943526Sxy150489 		e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
39953526Sxy150489 		e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
39963526Sxy150489 		e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
39973526Sxy150489 
39983526Sxy150489 		msec_delay(50);
39993526Sxy150489 		break;
40003526Sxy150489 	case e1000_media_type_fiber:
40013526Sxy150489 	case e1000_media_type_internal_serdes:
40024919Sxy150489 		status = E1000_READ_REG(hw, E1000_STATUS);
40033526Sxy150489 		if (((status & E1000_STATUS_LU) == 0) ||
40043526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
40054919Sxy150489 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
40063526Sxy150489 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
40074919Sxy150489 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
40083526Sxy150489 		}
40093526Sxy150489 
40103526Sxy150489 		/* Disable autoneg by setting bit 31 of TXCW to zero */
40114919Sxy150489 		txcw = E1000_READ_REG(hw, E1000_TXCW);
40123526Sxy150489 		txcw &= ~((uint32_t)1 << 31);
40134919Sxy150489 		E1000_WRITE_REG(hw, E1000_TXCW, txcw);
40143526Sxy150489 
40153526Sxy150489 		/*
40163526Sxy150489 		 * Write 0x410 to Serdes Control register
40173526Sxy150489 		 * to enable Serdes analog loopback
40183526Sxy150489 		 */
40194919Sxy150489 		E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
40203526Sxy150489 		msec_delay(10);
40213526Sxy150489 		break;
40223526Sxy150489 	default:
40233526Sxy150489 		break;
40243526Sxy150489 	}
40253526Sxy150489 }
40263526Sxy150489 
40273526Sxy150489 static void
40283526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
40293526Sxy150489 {
40303526Sxy150489 	struct e1000_hw *hw;
40313526Sxy150489 	uint32_t ctrl;
40323526Sxy150489 	uint16_t phy_ctrl;
40333526Sxy150489 
40344919Sxy150489 	hw = &Adapter->shared;
40353526Sxy150489 
40363526Sxy150489 	/* Disable Smart Power Down */
40373526Sxy150489 	phy_spd_state(hw, B_FALSE);
40383526Sxy150489 
40393526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
40404349Sxy150489 	    MII_CR_SPEED_100);
40413526Sxy150489 
40423526Sxy150489 	/* Force 100/FD, reset PHY */
40434919Sxy150489 	e1000_write_phy_reg(hw, PHY_CONTROL,
40444349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
40453526Sxy150489 	msec_delay(10);
40463526Sxy150489 
40473526Sxy150489 	/* Force 100/FD */
40484919Sxy150489 	e1000_write_phy_reg(hw, PHY_CONTROL,
40494349Sxy150489 	    phy_ctrl);			/* 0x2100 */
40503526Sxy150489 	msec_delay(10);
40513526Sxy150489 
40523526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
40534919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
40543526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
40553526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
40564349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
40574349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
40584349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
40594349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
40603526Sxy150489 
40614919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
40623526Sxy150489 }
40633526Sxy150489 
40643526Sxy150489 static void
40653526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
40663526Sxy150489 {
40673526Sxy150489 	struct e1000_hw *hw;
40683526Sxy150489 	uint32_t ctrl;
40693526Sxy150489 	uint16_t phy_ctrl;
40703526Sxy150489 
40714919Sxy150489 	hw = &Adapter->shared;
40723526Sxy150489 
40733526Sxy150489 	/* Disable Smart Power Down */
40743526Sxy150489 	phy_spd_state(hw, B_FALSE);
40753526Sxy150489 
40763526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
40774349Sxy150489 	    MII_CR_SPEED_10);
40783526Sxy150489 
40793526Sxy150489 	/* Force 10/FD, reset PHY */
40804919Sxy150489 	e1000_write_phy_reg(hw, PHY_CONTROL,
40814349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
40823526Sxy150489 	msec_delay(10);
40833526Sxy150489 
40843526Sxy150489 	/* Force 10/FD */
40854919Sxy150489 	e1000_write_phy_reg(hw, PHY_CONTROL,
40864349Sxy150489 	    phy_ctrl);			/* 0x0100 */
40873526Sxy150489 	msec_delay(10);
40883526Sxy150489 
40893526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
40904919Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
40913526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
40923526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
40934349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
40944349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
40954349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
40964349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
40973526Sxy150489 
40984919Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
40993526Sxy150489 }
41003526Sxy150489 
41013526Sxy150489 #ifdef __sparc
41023526Sxy150489 static boolean_t
41033526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
41043526Sxy150489 {
41054919Sxy150489 	struct e1000_hw *hw = &Adapter->shared;
41063526Sxy150489 	uchar_t *bytes;
41073526Sxy150489 	struct ether_addr sysaddr;
41083526Sxy150489 	uint_t nelts;
41093526Sxy150489 	int err;
41103526Sxy150489 	boolean_t found = B_FALSE;
41113526Sxy150489 
41123526Sxy150489 	/*
41133526Sxy150489 	 * The "vendor's factory-set address" may already have
41143526Sxy150489 	 * been extracted from the chip, but if the property
41153526Sxy150489 	 * "local-mac-address" is set we use that instead.
41163526Sxy150489 	 *
41173526Sxy150489 	 * We check whether it looks like an array of 6
41183526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
41193526Sxy150489 	 * make sense of it this way, we'll ignore it.
41203526Sxy150489 	 */
41213526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
41223526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
41233526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
41243526Sxy150489 		if (nelts == ETHERADDRL) {
41253526Sxy150489 			while (nelts--)
41264919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
41273526Sxy150489 			found = B_TRUE;
41283526Sxy150489 		}
41293526Sxy150489 		ddi_prop_free(bytes);
41303526Sxy150489 	}
41313526Sxy150489 
41323526Sxy150489 	/*
41333526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
41343526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
41353526Sxy150489 	 */
41363526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
41373526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
41383526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
41393526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
41404919Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
41413526Sxy150489 				found = B_TRUE;
41423526Sxy150489 			}
41433526Sxy150489 		}
41443526Sxy150489 		ddi_prop_free(bytes);
41453526Sxy150489 	}
41463526Sxy150489 
41473526Sxy150489 	/*
41483526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
41493526Sxy150489 	 * if we netbooted from this interface), we must use this instead
41503526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
41513526Sxy150489 	 * get confused by the address changing as Solaris takes over!
41523526Sxy150489 	 */
41533526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
41543526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
41553526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
41563526Sxy150489 		if (nelts == ETHERADDRL) {
41573526Sxy150489 			while (nelts--)
41584919Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
41593526Sxy150489 			found = B_TRUE;
41603526Sxy150489 		}
41613526Sxy150489 		ddi_prop_free(bytes);
41623526Sxy150489 	}
41633526Sxy150489 
41643526Sxy150489 	if (found) {
41654919Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr,
41663526Sxy150489 		    ETHERADDRL);
41673526Sxy150489 	}
41683526Sxy150489 
41693526Sxy150489 	return (found);
41703526Sxy150489 }
41713526Sxy150489 #endif
41723526Sxy150489 
41733526Sxy150489 static int
41743526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
41753526Sxy150489 {
41763526Sxy150489 	dev_info_t *devinfo;
41773526Sxy150489 	int intr_types;
41783526Sxy150489 	int rc;
41793526Sxy150489 
41803526Sxy150489 	devinfo = Adapter->dip;
41813526Sxy150489 
41823526Sxy150489 	/* Get supported interrupt types */
41833526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
41843526Sxy150489 
41853526Sxy150489 	if (rc != DDI_SUCCESS) {
41864919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
41873526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
41883526Sxy150489 		return (DDI_FAILURE);
41893526Sxy150489 	}
41903526Sxy150489 
41913526Sxy150489 	/*
41923526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
41933526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
41943526Sxy150489 	 * that it supports MSI, but in fact has problems.
41953526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
41963526Sxy150489 	 * PCI/PCI-X NICs.
41973526Sxy150489 	 */
41984919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
41993526Sxy150489 		Adapter->msi_enabled = B_FALSE;
42003526Sxy150489 
42013526Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enabled) {
42023526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
42033526Sxy150489 
42043526Sxy150489 		if (rc != DDI_SUCCESS) {
42054919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
42063526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
42073526Sxy150489 		} else {
42083526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
42093526Sxy150489 		}
42103526Sxy150489 	}
42113526Sxy150489 
42123526Sxy150489 	if ((Adapter->intr_type == 0) &&
42133526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
42143526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
42153526Sxy150489 
42163526Sxy150489 		if (rc != DDI_SUCCESS) {
42174919Sxy150489 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
42183526Sxy150489 			    "Add Legacy interrupts failed\n");
42193526Sxy150489 			return (DDI_FAILURE);
42203526Sxy150489 		}
42213526Sxy150489 
42223526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
42233526Sxy150489 	}
42243526Sxy150489 
42253526Sxy150489 	if (Adapter->intr_type == 0) {
42264919Sxy150489 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
42273526Sxy150489 		    "No interrupts registered\n");
42283526Sxy150489 		return (DDI_FAILURE);
42293526Sxy150489 	}
42303526Sxy150489 
42313526Sxy150489 	return (DDI_SUCCESS);
42323526Sxy150489 }
42333526Sxy150489 
42343526Sxy150489 /*
42353526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
42363526Sxy150489  */
42373526Sxy150489 static int
42383526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
42393526Sxy150489 {
42403526Sxy150489 	dev_info_t *devinfo;
42413526Sxy150489 	int count, avail, actual;
42423526Sxy150489 	int x, y, rc, inum = 0;
42433526Sxy150489 	int flag;
42443526Sxy150489 	ddi_intr_handler_t *intr_handler;
42453526Sxy150489 
42463526Sxy150489 	devinfo = Adapter->dip;
42473526Sxy150489 
42483526Sxy150489 	/* get number of interrupts */
42493526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
42503526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
42514919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
42523526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
42533526Sxy150489 		    rc, count);
42543526Sxy150489 		return (DDI_FAILURE);
42553526Sxy150489 	}
42563526Sxy150489 
42573526Sxy150489 	/* get number of available interrupts */
42583526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
42593526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
42604919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
42613526Sxy150489 		    "Get interrupt available number failed. "
42623526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
42633526Sxy150489 		return (DDI_FAILURE);
42643526Sxy150489 	}
42653526Sxy150489 
42663526Sxy150489 	if (avail < count) {
42674919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
42683526Sxy150489 		    "Interrupts count: %d, available: %d\n",
42693526Sxy150489 		    count, avail);
42703526Sxy150489 	}
42713526Sxy150489 
42723526Sxy150489 	/* Allocate an array of interrupt handles */
42733526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
42743526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
42753526Sxy150489 
42763526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
42773526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
42783526Sxy150489 
42793526Sxy150489 	/* call ddi_intr_alloc() */
42803526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
42813526Sxy150489 	    count, &actual, flag);
42823526Sxy150489 
42833526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
42844919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
42853526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
42863526Sxy150489 
42873526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
42883526Sxy150489 		return (DDI_FAILURE);
42893526Sxy150489 	}
42903526Sxy150489 
42913526Sxy150489 	if (actual < count) {
42924919Sxy150489 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
42933526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
42943526Sxy150489 		    count, actual);
42953526Sxy150489 	}
42963526Sxy150489 
42973526Sxy150489 	Adapter->intr_cnt = actual;
42983526Sxy150489 
42993526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
43003526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
43013526Sxy150489 
43023526Sxy150489 	if (rc != DDI_SUCCESS) {
43034919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
43043526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
43053526Sxy150489 
43063526Sxy150489 		/* Free already allocated intr */
43073526Sxy150489 		for (y = 0; y < actual; y++)
43083526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
43093526Sxy150489 
43103526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
43113526Sxy150489 		return (DDI_FAILURE);
43123526Sxy150489 	}
43133526Sxy150489 
43143526Sxy150489 	/*
43153526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
43163526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
43173526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
43183526Sxy150489 	 * devices.
43193526Sxy150489 	 */
43204919Sxy150489 	if (Adapter->shared.mac.type < e1000_82571)
43213526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
43223526Sxy150489 	else
43233526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
43243526Sxy150489 
43253526Sxy150489 	/* Call ddi_intr_add_handler() */
43263526Sxy150489 	for (x = 0; x < actual; x++) {
43273526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
43283526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
43293526Sxy150489 
43303526Sxy150489 		if (rc != DDI_SUCCESS) {
43314919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
43323526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
43333526Sxy150489 
43343526Sxy150489 			/* Remove already added handler */
43353526Sxy150489 			for (y = 0; y < x; y++)
43363526Sxy150489 				(void) ddi_intr_remove_handler(
43373526Sxy150489 				    Adapter->htable[y]);
43383526Sxy150489 
43393526Sxy150489 			/* Free already allocated intr */
43403526Sxy150489 			for (y = 0; y < actual; y++)
43413526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
43423526Sxy150489 
43433526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
43443526Sxy150489 			return (DDI_FAILURE);
43453526Sxy150489 		}
43463526Sxy150489 	}
43473526Sxy150489 
43483526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
43493526Sxy150489 
43503526Sxy150489 	if (rc != DDI_SUCCESS) {
43514919Sxy150489 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
43523526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
43533526Sxy150489 
43543526Sxy150489 		/* Free already allocated intr */
43553526Sxy150489 		for (y = 0; y < actual; y++) {
43563526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
43573526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
43583526Sxy150489 		}
43593526Sxy150489 
43603526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
43613526Sxy150489 		return (DDI_FAILURE);
43623526Sxy150489 	}
43633526Sxy150489 
43643526Sxy150489 	return (DDI_SUCCESS);
43653526Sxy150489 }
43663526Sxy150489 
43673526Sxy150489 static int
43683526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
43693526Sxy150489 {
43703526Sxy150489 	int x;
43713526Sxy150489 	int rc;
43723526Sxy150489 
43733526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
43743526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
43753526Sxy150489 		if (rc != DDI_SUCCESS) {
43764919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
43773526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
43783526Sxy150489 			return (DDI_FAILURE);
43793526Sxy150489 		}
43803526Sxy150489 
43813526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
43823526Sxy150489 		if (rc != DDI_SUCCESS) {
43834919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
43843526Sxy150489 			    "Free intr failed: %d\n", rc);
43853526Sxy150489 			return (DDI_FAILURE);
43863526Sxy150489 		}
43873526Sxy150489 	}
43883526Sxy150489 
43893526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
43903526Sxy150489 
43913526Sxy150489 	return (DDI_SUCCESS);
43923526Sxy150489 }
43933526Sxy150489 
43943526Sxy150489 static int
43953526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
43963526Sxy150489 {
43973526Sxy150489 	int x;
43983526Sxy150489 	int rc;
43993526Sxy150489 
44003526Sxy150489 	/* Enable interrupts */
44013526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
44023526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
44033526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
44043526Sxy150489 		    Adapter->intr_cnt);
44053526Sxy150489 		if (rc != DDI_SUCCESS) {
44064919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
44073526Sxy150489 			    "Enable block intr failed: %d\n", rc);
44083526Sxy150489 			return (DDI_FAILURE);
44093526Sxy150489 		}
44103526Sxy150489 	} else {
44113526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
44123526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
44133526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
44143526Sxy150489 			if (rc != DDI_SUCCESS) {
44154919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
44163526Sxy150489 				    "Enable intr failed: %d\n", rc);
44173526Sxy150489 				return (DDI_FAILURE);
44183526Sxy150489 			}
44193526Sxy150489 		}
44203526Sxy150489 	}
44213526Sxy150489 
44223526Sxy150489 	return (DDI_SUCCESS);
44233526Sxy150489 }
44243526Sxy150489 
44253526Sxy150489 static int
44263526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
44273526Sxy150489 {
44283526Sxy150489 	int x;
44293526Sxy150489 	int rc;
44303526Sxy150489 
44313526Sxy150489 	/* Disable all interrupts */
44323526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
44333526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
44343526Sxy150489 		    Adapter->intr_cnt);
44353526Sxy150489 		if (rc != DDI_SUCCESS) {
44364919Sxy150489 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
44373526Sxy150489 			    "Disable block intr failed: %d\n", rc);
44383526Sxy150489 			return (DDI_FAILURE);
44393526Sxy150489 		}
44403526Sxy150489 	} else {
44413526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
44423526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
44433526Sxy150489 			if (rc != DDI_SUCCESS) {
44444919Sxy150489 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
44453526Sxy150489 				    "Disable intr failed: %d\n", rc);
44463526Sxy150489 				return (DDI_FAILURE);
44473526Sxy150489 			}
44483526Sxy150489 		}
44493526Sxy150489 	}
44503526Sxy150489 
44513526Sxy150489 	return (DDI_SUCCESS);
44523526Sxy150489 }
44535082Syy150190 
44545082Syy150190 /*
44555082Syy150190  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
44565082Syy150190  */
44575082Syy150190 static void
44585082Syy150190 e1000g_get_phy_state(struct e1000g *Adapter)
44595082Syy150190 {
44605082Syy150190 	struct e1000_hw *hw = &Adapter->shared;
44615082Syy150190 
44625082Syy150190 	e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
44635082Syy150190 	e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
44645082Syy150190 	e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
44655082Syy150190 	e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
44665082Syy150190 	e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
44675082Syy150190 	e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
44685082Syy150190 	e1000_read_phy_reg(hw, PHY_1000T_STATUS, &Adapter->phy_1000t_status);
44695082Syy150190 	e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
44705082Syy150190 }
44715273Sgl147354 
44725273Sgl147354 /*
44735273Sgl147354  * FMA support
44745273Sgl147354  */
44755273Sgl147354 
44765273Sgl147354 int
44775273Sgl147354 e1000g_check_acc_handle(ddi_acc_handle_t handle)
44785273Sgl147354 {
44795273Sgl147354 	ddi_fm_error_t de;
44805273Sgl147354 
44815273Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
44825273Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
44835273Sgl147354 	return (de.fme_status);
44845273Sgl147354 }
44855273Sgl147354 
44865273Sgl147354 int
44875273Sgl147354 e1000g_check_dma_handle(ddi_dma_handle_t handle)
44885273Sgl147354 {
44895273Sgl147354 	ddi_fm_error_t de;
44905273Sgl147354 
44915273Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
44925273Sgl147354 	return (de.fme_status);
44935273Sgl147354 }
44945273Sgl147354 
44955273Sgl147354 /*
44965273Sgl147354  * The IO fault service error handling callback function
44975273Sgl147354  */
44985273Sgl147354 static int
44995273Sgl147354 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
45005273Sgl147354 {
45015273Sgl147354 	/*
45025273Sgl147354 	 * as the driver can always deal with an error in any dma or
45035273Sgl147354 	 * access handle, we can just return the fme_status value.
45045273Sgl147354 	 */
45055273Sgl147354 	pci_ereport_post(dip, err, NULL);
45065273Sgl147354 	return (err->fme_status);
45075273Sgl147354 }
45085273Sgl147354 
45095273Sgl147354 static void
45105273Sgl147354 e1000g_fm_init(struct e1000g *Adapter)
45115273Sgl147354 {
45125273Sgl147354 	ddi_iblock_cookie_t iblk;
45135273Sgl147354 	int fma_acc_flag, fma_dma_flag;
45145273Sgl147354 
45155273Sgl147354 	/* Only register with IO Fault Services if we have some capability */
45165273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
45175273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
45185273Sgl147354 		fma_acc_flag = 1;
45195273Sgl147354 	} else {
45205273Sgl147354 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
45215273Sgl147354 		fma_acc_flag = 0;
45225273Sgl147354 	}
45235273Sgl147354 
45245273Sgl147354 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
45255273Sgl147354 		fma_dma_flag = 1;
45265273Sgl147354 	} else {
45275273Sgl147354 		fma_dma_flag = 0;
45285273Sgl147354 	}
45295273Sgl147354 
45305273Sgl147354 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
45315273Sgl147354 
45325273Sgl147354 	if (Adapter->fm_capabilities) {
45335273Sgl147354 
45345273Sgl147354 		/* Register capabilities with IO Fault Services */
45355273Sgl147354 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
45365273Sgl147354 
45375273Sgl147354 		/*
45385273Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
45395273Sgl147354 		 */
45405273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
45415273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
45425273Sgl147354 			pci_ereport_setup(Adapter->dip);
45435273Sgl147354 
45445273Sgl147354 		/*
45455273Sgl147354 		 * Register error callback if error callback capable
45465273Sgl147354 		 */
45475273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
45485273Sgl147354 			ddi_fm_handler_register(Adapter->dip,
45495273Sgl147354 			    e1000g_fm_error_cb, (void*) Adapter);
45505273Sgl147354 	}
45515273Sgl147354 }
45525273Sgl147354 
45535273Sgl147354 static void
45545273Sgl147354 e1000g_fm_fini(struct e1000g *Adapter)
45555273Sgl147354 {
45565273Sgl147354 	/* Only unregister FMA capabilities if we registered some */
45575273Sgl147354 	if (Adapter->fm_capabilities) {
45585273Sgl147354 
45595273Sgl147354 		/*
45605273Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
45615273Sgl147354 		 */
45625273Sgl147354 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
45635273Sgl147354 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
45645273Sgl147354 			pci_ereport_teardown(Adapter->dip);
45655273Sgl147354 
45665273Sgl147354 		/*
45675273Sgl147354 		 * Un-register error callback if error callback capable
45685273Sgl147354 		 */
45695273Sgl147354 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
45705273Sgl147354 			ddi_fm_handler_unregister(Adapter->dip);
45715273Sgl147354 
45725273Sgl147354 		/* Unregister from IO Fault Services */
45735273Sgl147354 		ddi_fm_fini(Adapter->dip);
45745273Sgl147354 	}
45755273Sgl147354 }
45765273Sgl147354 
45775273Sgl147354 void
45785273Sgl147354 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
45795273Sgl147354 {
45805273Sgl147354 	uint64_t ena;
45815273Sgl147354 	char buf[FM_MAX_CLASS];
45825273Sgl147354 
45835273Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
45845273Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
45855273Sgl147354 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
45865273Sgl147354 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
45875273Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
45885273Sgl147354 	}
45895273Sgl147354 }
4590