xref: /onnv-gate/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 4894:fbbba3ea60f1)
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 /*
223526Sxy150489  * Copyright 2007 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:								*
353526Sxy150489  *   This file contains the interface routine for the solaris OS.	*
363526Sxy150489  *   It has all DDI entry point routines and GLD entry point		*
373526Sxy150489  *   routines.								*
383526Sxy150489  *   This file also contains routines that takes care of initialization	*
393526Sxy150489  *   uninit routine and interrupt routine				*
403526Sxy150489  *									*
413526Sxy150489  *									*
423526Sxy150489  * Environment:								*
433526Sxy150489  *   Kernel Mode -							*
443526Sxy150489  *									*
453526Sxy150489  * **********************************************************************
463526Sxy150489  */
473526Sxy150489 
483526Sxy150489 #include <sys/dlpi.h>
493526Sxy150489 #include <sys/mac.h>
503526Sxy150489 #include "e1000g_sw.h"
513526Sxy150489 #include "e1000g_debug.h"
523526Sxy150489 
533526Sxy150489 #define	E1000_RX_INTPT_TIME	128
543526Sxy150489 #define	E1000_RX_PKT_CNT	8
553526Sxy150489 
56*4894Syy150190 static char ident[] = "Intel PRO/1000 Ethernet 5.1.11";
573526Sxy150489 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
58*4894Syy150190 static char e1000g_version[] = "Driver Ver. 5.1.11";
593526Sxy150489 
603526Sxy150489 /*
613526Sxy150489  * Proto types for DDI entry points
623526Sxy150489  */
633526Sxy150489 static int e1000gattach(dev_info_t *, ddi_attach_cmd_t);
643526Sxy150489 static int e1000gdetach(dev_info_t *, ddi_detach_cmd_t);
653526Sxy150489 
663526Sxy150489 /*
673526Sxy150489  * init and intr routines prototype
683526Sxy150489  */
693526Sxy150489 static int e1000g_resume(dev_info_t *devinfo);
703526Sxy150489 static int e1000g_suspend(dev_info_t *devinfo);
713526Sxy150489 static uint_t e1000g_intr_pciexpress(caddr_t);
723526Sxy150489 static uint_t e1000g_intr(caddr_t);
733526Sxy150489 static void e1000g_intr_work(struct e1000g *, uint32_t);
743526Sxy150489 #pragma inline(e1000g_intr_work)
753526Sxy150489 static int e1000g_init(struct e1000g *);
763526Sxy150489 static int e1000g_start(struct e1000g *);
773526Sxy150489 static void e1000g_stop(struct e1000g *);
783526Sxy150489 static int e1000g_m_start(void *);
793526Sxy150489 static void e1000g_m_stop(void *);
803526Sxy150489 static int e1000g_m_promisc(void *, boolean_t);
813526Sxy150489 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
823526Sxy150489 static int e1000g_m_unicst(void *, const uint8_t *);
833526Sxy150489 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
843526Sxy150489 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
853526Sxy150489 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
863526Sxy150489 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
873526Sxy150489 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
883526Sxy150489 static void e1000g_m_blank(void *, time_t, uint32_t);
893526Sxy150489 static void e1000g_m_resources(void *);
903526Sxy150489 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
913526Sxy150489 static void e1000g_init_locks(struct e1000g *Adapter);
923526Sxy150489 static void e1000g_destroy_locks(struct e1000g *Adapter);
933526Sxy150489 static int e1000g_set_driver_params(struct e1000g *Adapter);
943526Sxy150489 static int e1000g_register_mac(struct e1000g *Adapter);
953526Sxy150489 static boolean_t e1000g_rx_drain(struct e1000g *Adapter);
963526Sxy150489 static boolean_t e1000g_tx_drain(struct e1000g *Adapter);
973526Sxy150489 static void e1000g_init_unicst(struct e1000g *Adapter);
983526Sxy150489 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
993526Sxy150489 
1003526Sxy150489 /*
1013526Sxy150489  * Local routines
1023526Sxy150489  */
1034061Sxy150489 static void e1000g_tx_drop(struct e1000g *Adapter);
1044061Sxy150489 static void e1000g_link_timer(void *);
1053526Sxy150489 static void e1000g_LocalTimer(void *);
1064061Sxy150489 static boolean_t e1000g_link_check(struct e1000g *);
1073526Sxy150489 static boolean_t e1000g_stall_check(struct e1000g *);
1083526Sxy150489 static void e1000g_smartspeed(struct e1000g *);
1093526Sxy150489 static void e1000g_getparam(struct e1000g *Adapter);
1103526Sxy150489 static int e1000g_getprop(struct e1000g *, char *, int, int, int);
1113526Sxy150489 static void e1000g_error(dev_info_t *dip, char *fmt, char *a1,
1123526Sxy150489     char *a2, char *a3, char *a4, char *a5, char *a6);
1133526Sxy150489 static void enable_timeout(struct e1000g *Adapter);
1143526Sxy150489 static void disable_timeout(struct e1000g *Adapter);
1153526Sxy150489 static void start_timeout(struct e1000g *Adapter);
1163526Sxy150489 static void restart_timeout(struct e1000g *Adapter);
1173526Sxy150489 static void stop_timeout(struct e1000g *Adapter);
1183526Sxy150489 static void e1000g_force_speed_duplex(struct e1000g *Adapter);
1193526Sxy150489 static void e1000g_get_max_frame_size(struct e1000g *Adapter);
1203526Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *mac_addr);
1213526Sxy150489 static void e1000g_unattach(dev_info_t *, struct e1000g *);
1223526Sxy150489 static void e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
1233526Sxy150489 static void e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
1243526Sxy150489 static void e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
1253526Sxy150489 static void e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
1263526Sxy150489 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *e1000gp,
1273526Sxy150489     struct iocblk *iocp, mblk_t *mp);
1283526Sxy150489 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *Adapter,
1293526Sxy150489     struct iocblk *iocp, mblk_t *mp);
1303526Sxy150489 static boolean_t e1000g_set_loopback_mode(struct e1000g *Adapter,
1313526Sxy150489     uint32_t mode);
1323526Sxy150489 static void e1000g_set_internal_loopback(struct e1000g *Adapter);
1333526Sxy150489 static void e1000g_set_external_loopback_1000(struct e1000g *Adapter);
1343526Sxy150489 static void e1000g_set_external_loopback_100(struct e1000g *Adapter);
1353526Sxy150489 static void e1000g_set_external_loopback_10(struct e1000g *Adapter);
1363526Sxy150489 static int e1000g_add_intrs(struct e1000g *Adapter);
1373526Sxy150489 static int e1000g_intr_add(struct e1000g *Adapter, int intr_type);
1383526Sxy150489 static int e1000g_rem_intrs(struct e1000g *Adapter);
1393526Sxy150489 static int e1000g_enable_intrs(struct e1000g *Adapter);
1403526Sxy150489 static int e1000g_disable_intrs(struct e1000g *Adapter);
1413526Sxy150489 static boolean_t e1000g_link_up(struct e1000g *Adapter);
1423526Sxy150489 #ifdef __sparc
1433526Sxy150489 static boolean_t e1000g_find_mac_address(struct e1000g *Adapter);
1443526Sxy150489 #endif
1453526Sxy150489 
1463526Sxy150489 static struct cb_ops cb_ws_ops = {
1473526Sxy150489 	nulldev,		/* cb_open */
1483526Sxy150489 	nulldev,		/* cb_close */
1493526Sxy150489 	nodev,			/* cb_strategy */
1503526Sxy150489 	nodev,			/* cb_print */
1513526Sxy150489 	nodev,			/* cb_dump */
1523526Sxy150489 	nodev,			/* cb_read */
1533526Sxy150489 	nodev,			/* cb_write */
1543526Sxy150489 	nodev,			/* cb_ioctl */
1553526Sxy150489 	nodev,			/* cb_devmap */
1563526Sxy150489 	nodev,			/* cb_mmap */
1573526Sxy150489 	nodev,			/* cb_segmap */
1583526Sxy150489 	nochpoll,		/* cb_chpoll */
1593526Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1603526Sxy150489 	NULL,			/* cb_stream */
1613526Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1623526Sxy150489 	CB_REV,			/* cb_rev */
1633526Sxy150489 	nodev,			/* cb_aread */
1643526Sxy150489 	nodev			/* cb_awrite */
1653526Sxy150489 };
1663526Sxy150489 
1673526Sxy150489 static struct dev_ops ws_ops = {
1683526Sxy150489 	DEVO_REV,		/* devo_rev */
1693526Sxy150489 	0,			/* devo_refcnt */
1703526Sxy150489 	NULL,			/* devo_getinfo */
1713526Sxy150489 	nulldev,		/* devo_identify */
1723526Sxy150489 	nulldev,		/* devo_probe */
1733526Sxy150489 	e1000gattach,		/* devo_attach */
1743526Sxy150489 	e1000gdetach,		/* devo_detach */
1753526Sxy150489 	nodev,			/* devo_reset */
1763526Sxy150489 	&cb_ws_ops,		/* devo_cb_ops */
1773526Sxy150489 	NULL,			/* devo_bus_ops */
1783526Sxy150489 	ddi_power		/* devo_power */
1793526Sxy150489 };
1803526Sxy150489 
1813526Sxy150489 static struct modldrv modldrv = {
1823526Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1833526Sxy150489 	ident,			/* Discription string */
1843526Sxy150489 	&ws_ops,		/* driver ops */
1853526Sxy150489 };
1863526Sxy150489 
1873526Sxy150489 static struct modlinkage modlinkage = {
1883526Sxy150489 	MODREV_1, &modldrv, NULL
1893526Sxy150489 };
1903526Sxy150489 
1913526Sxy150489 /*
1923526Sxy150489  * DMA access attributes <Little Endian Card>
1933526Sxy150489  */
1943526Sxy150489 static ddi_device_acc_attr_t accattr1 = {
1953526Sxy150489 	DDI_DEVICE_ATTR_V0,
1963526Sxy150489 	DDI_STRUCTURE_LE_ACC,
1973526Sxy150489 	DDI_STRICTORDER_ACC,
1983526Sxy150489 };
1993526Sxy150489 
2003526Sxy150489 #define	E1000G_M_CALLBACK_FLAGS	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB)
2013526Sxy150489 
2023526Sxy150489 static mac_callbacks_t e1000g_m_callbacks = {
2033526Sxy150489 	E1000G_M_CALLBACK_FLAGS,
2043526Sxy150489 	e1000g_m_stat,
2053526Sxy150489 	e1000g_m_start,
2063526Sxy150489 	e1000g_m_stop,
2073526Sxy150489 	e1000g_m_promisc,
2083526Sxy150489 	e1000g_m_multicst,
2093526Sxy150489 	e1000g_m_unicst,
2103526Sxy150489 	e1000g_m_tx,
2113526Sxy150489 	e1000g_m_resources,
2123526Sxy150489 	e1000g_m_ioctl,
2133526Sxy150489 	e1000g_m_getcapab
2143526Sxy150489 };
2153526Sxy150489 
2163526Sxy150489 /*
2173526Sxy150489  * Global variables
2183526Sxy150489  */
2193526Sxy150489 uint32_t e1000g_mblks_pending = 0;
2203526Sxy150489 /*
221*4894Syy150190  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
2224349Sxy150489  * Here we maintain a private dev_info list if e1000g_force_detach is
2234349Sxy150489  * enabled. If we force the driver to detach while there are still some
2244349Sxy150489  * rx buffers retained in the upper layer, we have to keep a copy of the
2254349Sxy150489  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
2264349Sxy150489  * structure will be freed after the driver is detached. However when we
2274349Sxy150489  * finally free those rx buffers released by the upper layer, we need to
2284349Sxy150489  * refer to the dev_info to free the dma buffers. So we save a copy of
229*4894Syy150190  * the dev_info for this purpose. On x86 platform, we assume this copy
230*4894Syy150190  * of dev_info is always valid, but on SPARC platform, it could be invalid
231*4894Syy150190  * after the system board level DR operation. For this reason, the global
232*4894Syy150190  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
2334349Sxy150489  */
234*4894Syy150190 #ifdef __sparc
235*4894Syy150190 boolean_t e1000g_force_detach = B_FALSE;
236*4894Syy150190 #else
237*4894Syy150190 boolean_t e1000g_force_detach = B_TRUE;
238*4894Syy150190 #endif
2394349Sxy150489 private_devi_list_t *e1000g_private_devi_list = NULL;
240*4894Syy150190 
2414349Sxy150489 /*
2423526Sxy150489  * The rwlock is defined to protect the whole processing of rx recycling
2433526Sxy150489  * and the rx packets release in detach processing to make them mutually
2443526Sxy150489  * exclusive.
2453526Sxy150489  * The rx recycling processes different rx packets in different threads,
2463526Sxy150489  * so it will be protected with RW_READER and it won't block any other rx
2473526Sxy150489  * recycling threads.
2483526Sxy150489  * While the detach processing will be protected with RW_WRITER to make
2493526Sxy150489  * it mutually exclusive with the rx recycling.
2503526Sxy150489  */
2513526Sxy150489 krwlock_t e1000g_rx_detach_lock;
2523526Sxy150489 /*
2533526Sxy150489  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
2543526Sxy150489  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
2553526Sxy150489  * If there are many e1000g instances, the system may run out of DVMA
2563526Sxy150489  * resources during the initialization of the instances, then the flag will
2573526Sxy150489  * be changed to "USE_DMA". Because different e1000g instances are initialized
2583526Sxy150489  * in parallel, we need to use this lock to protect the flag.
2593526Sxy150489  */
2603526Sxy150489 krwlock_t e1000g_dma_type_lock;
2613526Sxy150489 
2623526Sxy150489 
2633526Sxy150489 /*
2643526Sxy150489  * Loadable module configuration entry points for the driver
2653526Sxy150489  */
2663526Sxy150489 
2673526Sxy150489 /*
2683526Sxy150489  * **********************************************************************
2693526Sxy150489  * Name:      _init							*
2703526Sxy150489  *									*
2713526Sxy150489  * Description:								*
2723526Sxy150489  *     Initializes a loadable module. It is  called  before		*
2733526Sxy150489  *     any other routine in a loadable module.				*
2743526Sxy150489  *     All global locks are intialised here and it returns the retun 	*
2753526Sxy150489  *     value from mod_install()						*
2763526Sxy150489  *     This is mandotary function for the driver			*
2773526Sxy150489  * Parameter Passed:							*
2783526Sxy150489  *     None								*
2793526Sxy150489  * Return Value:							*
2803526Sxy150489  *     0 on success							*
2813526Sxy150489  * Functions called							*
2823526Sxy150489  *     mod_install()	     (system call)				*
2833526Sxy150489  *									*
2843526Sxy150489  * **********************************************************************
2853526Sxy150489  */
2863526Sxy150489 int
2873526Sxy150489 _init(void)
2883526Sxy150489 {
2893526Sxy150489 	int status;
2903526Sxy150489 
2913526Sxy150489 	mac_init_ops(&ws_ops, WSNAME);
2923526Sxy150489 	status = mod_install(&modlinkage);
2933526Sxy150489 	if (status != DDI_SUCCESS)
2943526Sxy150489 		mac_fini_ops(&ws_ops);
2953526Sxy150489 	else {
2963526Sxy150489 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
2973526Sxy150489 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
2983526Sxy150489 	}
2993526Sxy150489 
3003526Sxy150489 	return (status);
3013526Sxy150489 }
3023526Sxy150489 
3033526Sxy150489 /*
3043526Sxy150489  * **********************************************************************
3053526Sxy150489  *  Name:      _fini							*
3063526Sxy150489  *									*
3073526Sxy150489  *  Description:							*
3083526Sxy150489  *     Prepares a loadable module  for  unloading.   It  is		*
3093526Sxy150489  *     called  when  the  system  wants to unload a module.		*
3103526Sxy150489  *     This is mandotary function for the driver			*
3113526Sxy150489  *  Parameter Passed:							*
3123526Sxy150489  *     None								*
3133526Sxy150489  *  Return Value:							*
3143526Sxy150489  *     0 on success							*
3153526Sxy150489  *  Functions called							*
3163526Sxy150489  *     mod_remove()	      (system call)				*
3173526Sxy150489  *									*
3183526Sxy150489  *									*
3193526Sxy150489  *									*
3203526Sxy150489  * **********************************************************************
3213526Sxy150489  */
3223526Sxy150489 int
3233526Sxy150489 _fini(void)
3243526Sxy150489 {
3253526Sxy150489 	int status;
3263526Sxy150489 
3273526Sxy150489 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
3283526Sxy150489 	if (e1000g_mblks_pending != 0) {
3293526Sxy150489 		rw_exit(&e1000g_rx_detach_lock);
3303526Sxy150489 		return (EBUSY);
3313526Sxy150489 	}
3323526Sxy150489 	rw_exit(&e1000g_rx_detach_lock);
3333526Sxy150489 
3343526Sxy150489 	status = mod_remove(&modlinkage);
3353526Sxy150489 	if (status == DDI_SUCCESS) {
3363526Sxy150489 		mac_fini_ops(&ws_ops);
3374349Sxy150489 
3384349Sxy150489 		if (e1000g_force_detach) {
3394349Sxy150489 			private_devi_list_t *devi_node;
3404349Sxy150489 
3414349Sxy150489 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
3424349Sxy150489 			while (e1000g_private_devi_list != NULL) {
3434349Sxy150489 				devi_node = e1000g_private_devi_list;
3444349Sxy150489 				e1000g_private_devi_list =
3454349Sxy150489 				    e1000g_private_devi_list->next;
3464349Sxy150489 
3474349Sxy150489 				kmem_free(devi_node->priv_dip,
3484349Sxy150489 				    sizeof (struct dev_info));
3494349Sxy150489 				kmem_free(devi_node,
3504349Sxy150489 				    sizeof (private_devi_list_t));
3514349Sxy150489 			}
3524349Sxy150489 			rw_exit(&e1000g_rx_detach_lock);
3534349Sxy150489 		}
3544349Sxy150489 
3553526Sxy150489 		rw_destroy(&e1000g_rx_detach_lock);
3563526Sxy150489 		rw_destroy(&e1000g_dma_type_lock);
3573526Sxy150489 	}
3583526Sxy150489 
3593526Sxy150489 	return (status);
3603526Sxy150489 }
3613526Sxy150489 
3623526Sxy150489 /*
3633526Sxy150489  * **********************************************************************
3643526Sxy150489  * Name:      _info							*
3653526Sxy150489  *									*
3663526Sxy150489  * Description:								*
3673526Sxy150489  *     Returns  information  about  a   loadable   module.		*
3683526Sxy150489  *     This is mandotary function for the driver			*
3693526Sxy150489  * Parameter Passed:							*
3703526Sxy150489  *     module info structure						*
3713526Sxy150489  * Return Value:							*
3723526Sxy150489  *     0 on success							*
3733526Sxy150489  * Functions called							*
3743526Sxy150489  *     mod_info()		(system call)				*
3753526Sxy150489  *									*
3763526Sxy150489  *									*
3773526Sxy150489  * **********************************************************************
3783526Sxy150489  */
3793526Sxy150489 int
3803526Sxy150489 _info(struct modinfo *modinfop)
3813526Sxy150489 {
3823526Sxy150489 	return (mod_info(&modlinkage, modinfop));
3833526Sxy150489 }
3843526Sxy150489 
3853526Sxy150489 /*
3863526Sxy150489  * Interface exists: make available by filling in network interface
3873526Sxy150489  * record.  System will initialize the interface when it is ready
3883526Sxy150489  * to accept packets.
3893526Sxy150489  */
3903526Sxy150489 
3913526Sxy150489 /*
3923526Sxy150489  * **********************************************************************
3933526Sxy150489  * Name:      e1000gattach						*
3943526Sxy150489  *									*
3953526Sxy150489  * Description:								*
3963526Sxy150489  *     This function is the device-specific  initialization		*
3973526Sxy150489  *     entry point.  This entry point is required and must be writ-	*
3983526Sxy150489  *     ten.  The DDI_ATTACH command must be provided in the  attach	*
3993526Sxy150489  *     entry point. When attach() is called with cmd set to DDI_ATTACH,	*
4003526Sxy150489  *     all normal kernel services (such as  kmem_alloc(9F))  are	*
4013526Sxy150489  *     available  for  use by the driver. Device interrupts are not	*
4023526Sxy150489  *     blocked when attaching a device to the system.			*
4033526Sxy150489  *									*
4043526Sxy150489  *     The attach() function will be called once for each  instance	*
4053526Sxy150489  *     of  the  device  on  the  system with cmd set to DDI_ATTACH.	*
4063526Sxy150489  *     Until attach() succeeds, the only driver entry points  which	*
4073526Sxy150489  *     may  be called are open(9E) and getinfo(9E).			*
4083526Sxy150489  *									*
4093526Sxy150489  *									*
4103526Sxy150489  *									*
4113526Sxy150489  * Parameter Passed:							*
4123526Sxy150489  *									*
4133526Sxy150489  * Return Value:							*
4143526Sxy150489  *									*
4153526Sxy150489  * Functions called							*
4163526Sxy150489  *									*
4173526Sxy150489  *									*
4183526Sxy150489  * **********************************************************************
4193526Sxy150489  */
4203526Sxy150489 static int
4213526Sxy150489 e1000gattach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
4223526Sxy150489 {
4233526Sxy150489 	struct e1000g *Adapter;
4243526Sxy150489 	struct e1000_hw *hw;
4253526Sxy150489 	ddi_acc_handle_t handle;
4263526Sxy150489 	off_t mem_size;
4273526Sxy150489 	int instance;
4283526Sxy150489 
4293526Sxy150489 	switch (cmd) {
4303526Sxy150489 	default:
4313526Sxy150489 		e1000g_log(NULL, CE_WARN,
4323526Sxy150489 		    "Unsupported command send to e1000gattach... ");
4333526Sxy150489 		return (DDI_FAILURE);
4343526Sxy150489 
4353526Sxy150489 	case DDI_RESUME:
4363526Sxy150489 		return (e1000g_resume(devinfo));
4373526Sxy150489 
4383526Sxy150489 	case DDI_ATTACH:
4393526Sxy150489 		break;
4403526Sxy150489 	}
4413526Sxy150489 
4423526Sxy150489 	/*
4433526Sxy150489 	 * get device instance number
4443526Sxy150489 	 */
4453526Sxy150489 	instance = ddi_get_instance(devinfo);
4463526Sxy150489 
4473526Sxy150489 	/*
4483526Sxy150489 	 * Allocate soft data structure
4493526Sxy150489 	 */
4503526Sxy150489 	Adapter =
4513526Sxy150489 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
4523526Sxy150489 
4533526Sxy150489 	Adapter->dip = devinfo;
4543526Sxy150489 	Adapter->AdapterInstance = instance;
4553526Sxy150489 	Adapter->tx_ring->adapter = Adapter;
4563526Sxy150489 	Adapter->rx_ring->adapter = Adapter;
4573526Sxy150489 
4583526Sxy150489 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
4593526Sxy150489 
4604349Sxy150489 	if (e1000g_force_detach) {
4614349Sxy150489 		private_devi_list_t *devi_node;
462*4894Syy150190 
463*4894Syy150190 		Adapter->priv_dip =
464*4894Syy150190 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
465*4894Syy150190 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
466*4894Syy150190 		    sizeof (struct dev_info));
467*4894Syy150190 
468*4894Syy150190 		devi_node =
469*4894Syy150190 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
470*4894Syy150190 
471*4894Syy150190 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
472*4894Syy150190 		devi_node->dip = devinfo;
473*4894Syy150190 		devi_node->priv_dip = Adapter->priv_dip;
474*4894Syy150190 		devi_node->next = e1000g_private_devi_list;
475*4894Syy150190 		e1000g_private_devi_list = devi_node;
476*4894Syy150190 		rw_exit(&e1000g_rx_detach_lock);
4774349Sxy150489 	}
4784349Sxy150489 
4793526Sxy150489 	hw = &Adapter->Shared;
4803526Sxy150489 
4813526Sxy150489 	/*
4823526Sxy150489 	 * Map in the device registers.
4833526Sxy150489 	 *
4843526Sxy150489 	 * first get the size of device register to be mapped. The
4853526Sxy150489 	 * second parameter is the register we are interested. I our
4863526Sxy150489 	 * wiseman 0 is for config registers and 1 is for memory mapped
4873526Sxy150489 	 * registers Mem size should have memory mapped region size
4883526Sxy150489 	 */
4893526Sxy150489 	ddi_dev_regsize(devinfo, 1, /* register of interest */
4903526Sxy150489 	    (off_t *)&mem_size);
4913526Sxy150489 
4923526Sxy150489 	if ((ddi_regs_map_setup(devinfo, 1, /* register of interest */
4934349Sxy150489 	    (caddr_t *)&hw->hw_addr,
4944349Sxy150489 	    0, mem_size, &accattr1, &Adapter->E1000_handle))
4954349Sxy150489 	    != DDI_SUCCESS) {
4963526Sxy150489 		e1000g_log(Adapter, CE_WARN, "ddi_regs_map_setup failed");
4973526Sxy150489 		goto attach_fail;
4983526Sxy150489 	}
4993526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_REGSMAPPED;
5003526Sxy150489 
5013526Sxy150489 	Adapter->osdep.E1000_handle = Adapter->E1000_handle;
5023526Sxy150489 	hw->back = &Adapter->osdep;
5033526Sxy150489 
5043526Sxy150489 	/*
5053526Sxy150489 	 * PCI Configure
5063526Sxy150489 	 */
5073526Sxy150489 	if (pci_config_setup(devinfo, &handle) != DDI_SUCCESS) {
5083526Sxy150489 		e1000g_log(Adapter, CE_WARN,
5093526Sxy150489 		    "PCI configuration could not be read.");
5103526Sxy150489 		goto attach_fail;
5113526Sxy150489 	}
5123526Sxy150489 
5133526Sxy150489 	Adapter->handle = handle;
5143526Sxy150489 	Adapter->osdep.handle = handle;
5153526Sxy150489 
5163526Sxy150489 	hw->vendor_id =
5173526Sxy150489 	    pci_config_get16(handle, PCI_CONF_VENID);
5183526Sxy150489 	hw->device_id =
5193526Sxy150489 	    pci_config_get16(handle, PCI_CONF_DEVID);
5203526Sxy150489 	hw->revision_id =
5213526Sxy150489 	    pci_config_get8(handle, PCI_CONF_REVID);
5223526Sxy150489 	hw->subsystem_id =
5233526Sxy150489 	    pci_config_get16(handle, PCI_CONF_SUBSYSID);
5243526Sxy150489 	hw->subsystem_vendor_id =
5253526Sxy150489 	    pci_config_get16(handle, PCI_CONF_SUBVENID);
5263526Sxy150489 
5273526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PCICONFIG;
5283526Sxy150489 
5293526Sxy150489 	/*
5303526Sxy150489 	 * Initialize driver parameters
5313526Sxy150489 	 */
5323526Sxy150489 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
5333526Sxy150489 		goto attach_fail;
5343526Sxy150489 	}
5353526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_PROP;
5363526Sxy150489 
5373526Sxy150489 	/*
5383526Sxy150489 	 * Initialize interrupts
5393526Sxy150489 	 */
5403526Sxy150489 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
5413526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
5423526Sxy150489 		goto attach_fail;
5433526Sxy150489 	}
5443526Sxy150489 	Adapter->tx_softint_pri = DDI_INTR_SOFTPRI_MAX;
5453526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INTRADDED;
5463526Sxy150489 
5473526Sxy150489 	/*
5483526Sxy150489 	 * Initialize mutex's for this device.
5493526Sxy150489 	 * Do this before enabling the interrupt handler and
5503526Sxy150489 	 * register the softint to avoid the condition where
5513526Sxy150489 	 * interrupt handler can try using uninitialized mutex
5523526Sxy150489 	 */
5533526Sxy150489 	e1000g_init_locks(Adapter);
5543526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
5553526Sxy150489 
5563526Sxy150489 	if (ddi_intr_add_softint(devinfo,
5573526Sxy150489 	    &Adapter->tx_softint_handle, Adapter->tx_softint_pri,
5583526Sxy150489 	    e1000g_tx_freemsg, (caddr_t)Adapter) != DDI_SUCCESS) {
5593526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Add soft intr failed");
5603526Sxy150489 		goto attach_fail;
5613526Sxy150489 	}
5623526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_SOFTINTR;
5633526Sxy150489 
5643526Sxy150489 	/*
5653526Sxy150489 	 * Initialize Driver Counters
5663526Sxy150489 	 */
5673526Sxy150489 	if (InitStatsCounters(Adapter) != DDI_SUCCESS) {
5683526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
5693526Sxy150489 		goto attach_fail;
5703526Sxy150489 	}
5713526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
5723526Sxy150489 
5733526Sxy150489 	/*
5743526Sxy150489 	 * Allocate dma resources for descriptors and buffers
5753526Sxy150489 	 */
5763526Sxy150489 	if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
5773526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Alloc dma resources failed");
5783526Sxy150489 		goto attach_fail;
5793526Sxy150489 	}
5803526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_ALLOC;
5813526Sxy150489 
5823526Sxy150489 	/*
5833526Sxy150489 	 * Initialize chip hardware and software structures
5843526Sxy150489 	 */
5853526Sxy150489 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
5863526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
5873526Sxy150489 		goto attach_fail;
5883526Sxy150489 	}
5893526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
5903526Sxy150489 
5913526Sxy150489 	/*
5923526Sxy150489 	 * Initialize NDD parameters
5933526Sxy150489 	 */
5943526Sxy150489 	if (e1000g_nd_init(Adapter) != DDI_SUCCESS) {
5953526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Init NDD failed");
5963526Sxy150489 		goto attach_fail;
5973526Sxy150489 	}
5983526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_NDD;
5993526Sxy150489 
6003526Sxy150489 	/*
6013526Sxy150489 	 * Register the driver to the MAC
6023526Sxy150489 	 */
6033526Sxy150489 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
6043526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
6053526Sxy150489 		goto attach_fail;
6063526Sxy150489 	}
6073526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_MACREGISTERED;
6083526Sxy150489 
6093526Sxy150489 	/*
6103526Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
6113526Sxy150489 	 * initialized, enable interrupts.
6123526Sxy150489 	 */
6133526Sxy150489 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
6143526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
6153526Sxy150489 		goto attach_fail;
6163526Sxy150489 	}
6173526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INTRENABLED;
6183526Sxy150489 
6193526Sxy150489 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
6203526Sxy150489 
6213526Sxy150489 	return (DDI_SUCCESS);
6223526Sxy150489 
6233526Sxy150489 attach_fail:
6243526Sxy150489 	e1000g_unattach(devinfo, Adapter);
6253526Sxy150489 	return (DDI_FAILURE);
6263526Sxy150489 }
6273526Sxy150489 
6283526Sxy150489 static int
6293526Sxy150489 e1000g_register_mac(struct e1000g *Adapter)
6303526Sxy150489 {
6313526Sxy150489 	struct e1000_hw *hw = &Adapter->Shared;
6323526Sxy150489 	mac_register_t *mac;
6333526Sxy150489 	int err;
6343526Sxy150489 
6353526Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
6363526Sxy150489 		return (DDI_FAILURE);
6373526Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
6383526Sxy150489 	mac->m_driver = Adapter;
6393526Sxy150489 	mac->m_dip = Adapter->dip;
6403526Sxy150489 	mac->m_src_addr = hw->mac_addr;
6413526Sxy150489 	mac->m_callbacks = &e1000g_m_callbacks;
6423526Sxy150489 	mac->m_min_sdu = 0;
6433526Sxy150489 	mac->m_max_sdu =
6443526Sxy150489 	    (hw->max_frame_size > FRAME_SIZE_UPTO_8K) ?
6453526Sxy150489 	    hw->max_frame_size - 256 :
6463526Sxy150489 	    (hw->max_frame_size != ETHERMAX) ?
6473526Sxy150489 	    hw->max_frame_size - 24 : ETHERMTU;
6483526Sxy150489 	err = mac_register(mac, &Adapter->mh);
6493526Sxy150489 	mac_free(mac);
6503526Sxy150489 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
6513526Sxy150489 }
6523526Sxy150489 
6533526Sxy150489 static int
6543526Sxy150489 e1000g_set_driver_params(struct e1000g *Adapter)
6553526Sxy150489 {
6563526Sxy150489 	dev_info_t *devinfo;
6573526Sxy150489 	ddi_acc_handle_t handle;
6583526Sxy150489 	struct e1000_hw *hw;
6593526Sxy150489 	uint32_t mem_bar, io_bar;
6603526Sxy150489 #ifdef __sparc
6613526Sxy150489 	ulong_t iommu_pagesize;
6623526Sxy150489 #endif
6633526Sxy150489 
6643526Sxy150489 	devinfo = Adapter->dip;
6653526Sxy150489 	handle = Adapter->handle;
6663526Sxy150489 	hw = &Adapter->Shared;
6673526Sxy150489 
6683526Sxy150489 	/* Set Mac Type */
6693526Sxy150489 	if (e1000_set_mac_type(hw) != 0) {
6703526Sxy150489 		e1000g_log(Adapter, CE_WARN,
6713526Sxy150489 		    "Could not identify hardware");
6723526Sxy150489 		return (DDI_FAILURE);
6733526Sxy150489 	}
6743526Sxy150489 
6753526Sxy150489 	/* ich8 needs to map flash memory */
6763526Sxy150489 	if (hw->mac_type == e1000_ich8lan) {
6773526Sxy150489 		/* get flash size */
6783526Sxy150489 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
6793526Sxy150489 		    &Adapter->osdep.ich_flash_size) != DDI_SUCCESS) {
6803526Sxy150489 			e1000g_log(Adapter, CE_WARN,
6813526Sxy150489 			    "ddi_dev_regsize for ich8 flash failed");
6823526Sxy150489 			return (DDI_FAILURE);
6833526Sxy150489 		}
6843526Sxy150489 
6853526Sxy150489 		/* map flash in */
6863526Sxy150489 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
6873526Sxy150489 		    &Adapter->osdep.ich_flash_base, 0,
6883526Sxy150489 		    Adapter->osdep.ich_flash_size,
6893526Sxy150489 		    &accattr1,
6903526Sxy150489 		    &Adapter->osdep.ich_flash_handle) != DDI_SUCCESS) {
6913526Sxy150489 			e1000g_log(Adapter, CE_WARN,
6923526Sxy150489 			    "ddi_regs_map_setup for for ich8 flash failed");
6933526Sxy150489 			return (DDI_FAILURE);
6943526Sxy150489 		}
6953526Sxy150489 	}
6963526Sxy150489 
6973526Sxy150489 	/* get mem_base addr */
6983526Sxy150489 	mem_bar = pci_config_get32(handle, PCI_CONF_BASE0);
6993526Sxy150489 	Adapter->bar64 = mem_bar & PCI_BASE_TYPE_ALL;
7003526Sxy150489 
7013526Sxy150489 	/* get io_base addr */
7023526Sxy150489 	if (hw->mac_type >= e1000_82544) {
7033526Sxy150489 		if (Adapter->bar64) {
7043526Sxy150489 			/* IO BAR is different for 64 bit BAR mode */
7053526Sxy150489 			io_bar = pci_config_get32(handle, PCI_CONF_BASE4);
7063526Sxy150489 		} else {
7073526Sxy150489 			/* normal 32-bit BAR mode */
7083526Sxy150489 			io_bar = pci_config_get32(handle, PCI_CONF_BASE2);
7093526Sxy150489 		}
7103526Sxy150489 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
7113526Sxy150489 	} else {
7123526Sxy150489 		/* no I/O access for adapters prior to 82544 */
7133526Sxy150489 		hw->io_base = 0x0;
7143526Sxy150489 	}
7153526Sxy150489 
7163526Sxy150489 	e1000_read_pci_cfg(hw,
7173526Sxy150489 	    PCI_COMMAND_REGISTER, &(hw->pci_cmd_word));
7183526Sxy150489 
7193526Sxy150489 	/* Set the wait_autoneg_complete flag to B_FALSE */
7203526Sxy150489 	hw->wait_autoneg_complete = B_FALSE;
7213526Sxy150489 
7223526Sxy150489 	/* Adaptive IFS related changes */
7233526Sxy150489 	hw->adaptive_ifs = B_TRUE;
7243526Sxy150489 
7253526Sxy150489 	/* set phy init script revision */
7263526Sxy150489 	if ((hw->mac_type == e1000_82547) ||
7273526Sxy150489 	    (hw->mac_type == e1000_82541) ||
7283526Sxy150489 	    (hw->mac_type == e1000_82547_rev_2) ||
7293526Sxy150489 	    (hw->mac_type == e1000_82541_rev_2))
7303526Sxy150489 		hw->phy_init_script = 1;
7313526Sxy150489 
7323526Sxy150489 	/* Enable the TTL workaround for TnT: DCR 49 */
7333526Sxy150489 	hw->ttl_wa_activation = 1;
7343526Sxy150489 
7353526Sxy150489 	if (hw->mac_type == e1000_82571)
7363526Sxy150489 		hw->laa_is_present = B_TRUE;
7373526Sxy150489 
7384608Syy150190 #ifdef __sparc
7394608Syy150190 	Adapter->strip_crc = B_TRUE;
7404608Syy150190 #else
7414608Syy150190 	Adapter->strip_crc = B_FALSE;
7424608Syy150190 #endif
7434608Syy150190 
7443526Sxy150489 	/* Get conf file properties */
7453526Sxy150489 	e1000g_getparam(Adapter);
7463526Sxy150489 
7474061Sxy150489 	hw->forced_speed_duplex = e1000_100_full;
7484061Sxy150489 	hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
7493526Sxy150489 	e1000g_force_speed_duplex(Adapter);
7503526Sxy150489 
7513526Sxy150489 	e1000g_get_max_frame_size(Adapter);
7523526Sxy150489 	hw->min_frame_size =
7533526Sxy150489 	    MINIMUM_ETHERNET_PACKET_SIZE + CRC_LENGTH;
7543526Sxy150489 
7553526Sxy150489 #ifdef __sparc
7563526Sxy150489 	/* Get the system page size */
7573526Sxy150489 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
7583526Sxy150489 	iommu_pagesize = dvma_pagesize(devinfo);
7593526Sxy150489 	if (iommu_pagesize != 0) {
7603526Sxy150489 		if (Adapter->sys_page_sz == iommu_pagesize) {
7613526Sxy150489 			if (iommu_pagesize > 0x4000)
7623526Sxy150489 				Adapter->sys_page_sz = 0x4000;
7633526Sxy150489 		} else {
7643526Sxy150489 			if (Adapter->sys_page_sz > iommu_pagesize)
7653526Sxy150489 				Adapter->sys_page_sz = iommu_pagesize;
7663526Sxy150489 		}
7673526Sxy150489 	}
7683526Sxy150489 	Adapter->dvma_page_num = hw->max_frame_size /
7693526Sxy150489 	    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
7703526Sxy150489 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
7713526Sxy150489 #endif
7723526Sxy150489 
7733526Sxy150489 	/* Set Rx/Tx buffer size */
7743526Sxy150489 	switch (hw->max_frame_size) {
7753526Sxy150489 	case ETHERMAX:
7763526Sxy150489 		Adapter->RxBufferSize = E1000_RX_BUFFER_SIZE_2K;
7773526Sxy150489 		Adapter->TxBufferSize = E1000_TX_BUFFER_SIZE_2K;
7783526Sxy150489 		break;
7793526Sxy150489 	case FRAME_SIZE_UPTO_4K:
7803526Sxy150489 		Adapter->RxBufferSize = E1000_RX_BUFFER_SIZE_4K;
7813526Sxy150489 		Adapter->TxBufferSize = E1000_TX_BUFFER_SIZE_4K;
7823526Sxy150489 		break;
7833526Sxy150489 	case FRAME_SIZE_UPTO_8K:
7843526Sxy150489 		Adapter->RxBufferSize = E1000_RX_BUFFER_SIZE_8K;
7853526Sxy150489 		Adapter->TxBufferSize = E1000_TX_BUFFER_SIZE_8K;
7863526Sxy150489 		break;
7873526Sxy150489 	case FRAME_SIZE_UPTO_10K:
7883526Sxy150489 	case FRAME_SIZE_UPTO_16K:
7893526Sxy150489 		Adapter->RxBufferSize = E1000_RX_BUFFER_SIZE_16K;
7903526Sxy150489 		Adapter->TxBufferSize = E1000_TX_BUFFER_SIZE_16K;
7913526Sxy150489 		break;
7923526Sxy150489 	default:
7933526Sxy150489 		Adapter->RxBufferSize = E1000_RX_BUFFER_SIZE_2K;
7943526Sxy150489 		Adapter->TxBufferSize = E1000_TX_BUFFER_SIZE_2K;
7953526Sxy150489 		break;
7963526Sxy150489 	}
7973526Sxy150489 	Adapter->RxBufferSize += E1000G_IPALIGNPRESERVEROOM;
7983526Sxy150489 
7993526Sxy150489 	/*
8003526Sxy150489 	 * For Wiseman adapters we have an requirement of having receive
8013526Sxy150489 	 * buffers aligned at 256 byte boundary. Since Livengood does not
8023526Sxy150489 	 * require this and forcing it for all hardwares will have
8033526Sxy150489 	 * performance implications, I am making it applicable only for
8043526Sxy150489 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
8053526Sxy150489 	 * it is okay to have normal frames...but it does involve a
8063526Sxy150489 	 * potential risk where we may loose data if buffer is not
8073526Sxy150489 	 * aligned...so all wiseman boards to have 256 byte aligned
8083526Sxy150489 	 * buffers
8093526Sxy150489 	 */
8103526Sxy150489 	if (hw->mac_type < e1000_82543)
8113526Sxy150489 		Adapter->RcvBufferAlignment = RECEIVE_BUFFER_ALIGN_SIZE;
8123526Sxy150489 	else
8133526Sxy150489 		/*
8143526Sxy150489 		 * For livengood, there is no such Rcv buf alignment
8153526Sxy150489 		 * requirement
8163526Sxy150489 		 */
8173526Sxy150489 		Adapter->RcvBufferAlignment = 1;
8183526Sxy150489 
8193526Sxy150489 	/* DmaFairness */
8203526Sxy150489 	if (hw->mac_type <= e1000_82543)
8213526Sxy150489 		hw->dma_fairness = DEFAULTRXPCIPRIORITYVAL;
8223526Sxy150489 	else
8233526Sxy150489 		hw->dma_fairness = 0;
8243526Sxy150489 
8253526Sxy150489 	/* MasterLatencyTimer */
8263526Sxy150489 	Adapter->MasterLatencyTimer = DEFAULTMASTERLATENCYTIMERVAL;
8273526Sxy150489 
8283526Sxy150489 	/* MWIEnable */
8293526Sxy150489 	Adapter->MWIEnable = DEFAULTMWIENABLEVAL;
8303526Sxy150489 
8313526Sxy150489 	/* profile jumbo traffic */
8323526Sxy150489 	Adapter->ProfileJumboTraffic = DEFAULTPROFILEJUMBOTRAFFIC;
8333526Sxy150489 
8343526Sxy150489 	e1000_set_media_type(hw);
8353526Sxy150489 	/* copper options */
8363526Sxy150489 	if (hw->media_type == e1000_media_type_copper) {
8373526Sxy150489 		hw->mdix = 0;	/* AUTO_ALL_MODES */
8383526Sxy150489 		hw->disable_polarity_correction = B_FALSE;
8393526Sxy150489 		hw->master_slave = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
8403526Sxy150489 	}
8413526Sxy150489 
8424061Sxy150489 	Adapter->link_state = LINK_STATE_UNKNOWN;
8434061Sxy150489 
8443526Sxy150489 	return (DDI_SUCCESS);
8453526Sxy150489 }
8463526Sxy150489 
8473526Sxy150489 /*
8483526Sxy150489  * **********************************************************************
8493526Sxy150489  * Name:      e1000gdettach						*
8503526Sxy150489  *									*
8513526Sxy150489  * Description:								*
8523526Sxy150489  *    The detach() function is the complement of the attach routine.	*
8533526Sxy150489  *    If cmd is set to DDI_DETACH, detach() is used to remove  the	*
8543526Sxy150489  *    state  associated  with  a  given  instance of a device node	*
8553526Sxy150489  *    prior to the removal of that instance from the system.		*
8563526Sxy150489  *									*
8573526Sxy150489  *    The detach() function will be called once for each  instance	*
8583526Sxy150489  *    of the device for which there has been a successful attach()	*
8593526Sxy150489  *    once there are no longer  any  opens  on  the  device.		*
8603526Sxy150489  *									*
8613526Sxy150489  *    Interrupts routine are disabled, All memory allocated by this	*
8623526Sxy150489  *    driver are freed.							*
8633526Sxy150489  *									*
8643526Sxy150489  * Parameter Passed:							*
8653526Sxy150489  *    devinfo structure, cmd						*
8663526Sxy150489  *									*
8673526Sxy150489  * Return Value:							*
8683526Sxy150489  *    DDI_SUCCESS on success						*
8693526Sxy150489  *									*
8703526Sxy150489  * Functions called							*
8713526Sxy150489  *									*
8723526Sxy150489  *									*
8733526Sxy150489  * **********************************************************************
8743526Sxy150489  */
8753526Sxy150489 static int
8763526Sxy150489 e1000gdetach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
8773526Sxy150489 {
8783526Sxy150489 	struct e1000g *Adapter;
8793526Sxy150489 
8803526Sxy150489 	switch (cmd) {
8813526Sxy150489 	default:
8823526Sxy150489 		return (DDI_FAILURE);
8833526Sxy150489 
8843526Sxy150489 	case DDI_SUSPEND:
8853526Sxy150489 		return (e1000g_suspend(devinfo));
8863526Sxy150489 
8873526Sxy150489 	case DDI_DETACH:
8883526Sxy150489 		break;
8893526Sxy150489 	}
8903526Sxy150489 
8913526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
8923526Sxy150489 	if (Adapter == NULL)
8933526Sxy150489 		return (DDI_FAILURE);
8943526Sxy150489 
8953526Sxy150489 	if (Adapter->started)
8963526Sxy150489 		e1000g_stop(Adapter);
8973526Sxy150489 
8983526Sxy150489 	if (!e1000g_rx_drain(Adapter)) {
8994349Sxy150489 		if (!e1000g_force_detach)
9003526Sxy150489 			return (DDI_FAILURE);
9013526Sxy150489 	}
9023526Sxy150489 
9033526Sxy150489 	if (e1000g_disable_intrs(Adapter) != DDI_SUCCESS) {
9043526Sxy150489 		e1000g_log(Adapter, CE_WARN,
9053526Sxy150489 		    "Disable DDI interrupts failed");
9063526Sxy150489 		return (DDI_FAILURE);
9073526Sxy150489 	}
9083526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INTRENABLED;
9093526Sxy150489 
9103526Sxy150489 	if (mac_unregister(Adapter->mh) != 0) {
9113526Sxy150489 		e1000g_log(Adapter, CE_WARN,
9123526Sxy150489 		    "Unregister MAC failed");
9133526Sxy150489 		return (DDI_FAILURE);
9143526Sxy150489 	}
9153526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MACREGISTERED;
9163526Sxy150489 
9173526Sxy150489 	e1000g_unattach(devinfo, Adapter);
9183526Sxy150489 
9193526Sxy150489 	return (DDI_SUCCESS);
9203526Sxy150489 }
9213526Sxy150489 
9223526Sxy150489 static void
9233526Sxy150489 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
9243526Sxy150489 {
9253526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INTRENABLED) {
9263526Sxy150489 		(void) e1000g_disable_intrs(Adapter);
9273526Sxy150489 	}
9283526Sxy150489 
9293526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_MACREGISTERED) {
9303526Sxy150489 		(void) mac_unregister(Adapter->mh);
9313526Sxy150489 	}
9323526Sxy150489 
9333526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_NDD) {
9343526Sxy150489 		e1000g_nd_cleanup(Adapter);
9353526Sxy150489 	}
9363526Sxy150489 
9373526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INTRADDED) {
9383526Sxy150489 		(void) e1000g_rem_intrs(Adapter);
9393526Sxy150489 	}
9403526Sxy150489 
9413526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_SOFTINTR) {
9423526Sxy150489 		(void) ddi_intr_remove_softint(Adapter->tx_softint_handle);
9433526Sxy150489 	}
9443526Sxy150489 
9453526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PROP) {
9463526Sxy150489 		(void) ddi_prop_remove_all(devinfo);
9473526Sxy150489 	}
9483526Sxy150489 
9493526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
9503526Sxy150489 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
9513526Sxy150489 	}
9523526Sxy150489 
9533526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
9544139Sxy150489 		timeout_id_t tid = 0;
9554139Sxy150489 
9564139Sxy150489 		/* Disable the link timer */
9574139Sxy150489 		mutex_enter(&Adapter->e1000g_linklock);
9584139Sxy150489 		tid = Adapter->link_tid;
9594139Sxy150489 		Adapter->link_tid = 0;
9604139Sxy150489 		mutex_exit(&Adapter->e1000g_linklock);
9614139Sxy150489 
9624139Sxy150489 		if (tid != 0)
9634139Sxy150489 			(void) untimeout(tid);
9644139Sxy150489 
9653526Sxy150489 		e1000_reset_hw(&Adapter->Shared);
9663526Sxy150489 	}
9673526Sxy150489 
9683526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGSMAPPED) {
9693526Sxy150489 		ddi_regs_map_free(&Adapter->E1000_handle);
9703526Sxy150489 	}
9713526Sxy150489 
9723526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCICONFIG) {
9733526Sxy150489 		pci_config_teardown(&Adapter->handle);
9743526Sxy150489 	}
9753526Sxy150489 
9763526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_ALLOC) {
9773526Sxy150489 		e1000g_release_dma_resources(Adapter);
9783526Sxy150489 	}
9793526Sxy150489 
9803526Sxy150489 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
9813526Sxy150489 		e1000g_destroy_locks(Adapter);
9823526Sxy150489 	}
9833526Sxy150489 
9843526Sxy150489 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
9853526Sxy150489 
9863526Sxy150489 	/*
9873526Sxy150489 	 * Another hotplug spec requirement,
9883526Sxy150489 	 * run ddi_set_driver_private(devinfo, null);
9893526Sxy150489 	 */
9903526Sxy150489 	ddi_set_driver_private(devinfo, NULL);
9913526Sxy150489 }
9923526Sxy150489 
9933526Sxy150489 static void
9943526Sxy150489 e1000g_init_locks(struct e1000g *Adapter)
9953526Sxy150489 {
9963526Sxy150489 	e1000g_tx_ring_t *tx_ring;
9973526Sxy150489 	e1000g_rx_ring_t *rx_ring;
9983526Sxy150489 
9993526Sxy150489 	rw_init(&Adapter->chip_lock, NULL,
10003526Sxy150489 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10013526Sxy150489 	mutex_init(&Adapter->e1000g_linklock, NULL,
10023526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10033526Sxy150489 	mutex_init(&Adapter->e1000g_timeout_lock, NULL,
10043526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10053526Sxy150489 	mutex_init(&Adapter->TbiCntrMutex, NULL,
10063526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10073526Sxy150489 
10083526Sxy150489 	mutex_init(&Adapter->tx_msg_chain->lock, NULL,
10093526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->tx_softint_pri));
10103526Sxy150489 
10113526Sxy150489 	tx_ring = Adapter->tx_ring;
10123526Sxy150489 
10133526Sxy150489 	mutex_init(&tx_ring->tx_lock, NULL,
10143526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10153526Sxy150489 	mutex_init(&tx_ring->usedlist_lock, NULL,
10163526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10173526Sxy150489 	mutex_init(&tx_ring->freelist_lock, NULL,
10183526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10193526Sxy150489 
10203526Sxy150489 	rx_ring = Adapter->rx_ring;
10213526Sxy150489 
10223526Sxy150489 	mutex_init(&rx_ring->rx_lock, NULL,
10233526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10243526Sxy150489 	mutex_init(&rx_ring->freelist_lock, NULL,
10253526Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
10263526Sxy150489 }
10273526Sxy150489 
10283526Sxy150489 static void
10293526Sxy150489 e1000g_destroy_locks(struct e1000g *Adapter)
10303526Sxy150489 {
10313526Sxy150489 	e1000g_tx_ring_t *tx_ring;
10323526Sxy150489 	e1000g_rx_ring_t *rx_ring;
10333526Sxy150489 
10343526Sxy150489 	tx_ring = Adapter->tx_ring;
10353526Sxy150489 	mutex_destroy(&tx_ring->tx_lock);
10363526Sxy150489 	mutex_destroy(&tx_ring->usedlist_lock);
10373526Sxy150489 	mutex_destroy(&tx_ring->freelist_lock);
10383526Sxy150489 
10393526Sxy150489 	rx_ring = Adapter->rx_ring;
10403526Sxy150489 	mutex_destroy(&rx_ring->rx_lock);
10413526Sxy150489 	mutex_destroy(&rx_ring->freelist_lock);
10423526Sxy150489 
10433526Sxy150489 	mutex_destroy(&Adapter->tx_msg_chain->lock);
10443526Sxy150489 	mutex_destroy(&Adapter->e1000g_linklock);
10453526Sxy150489 	mutex_destroy(&Adapter->TbiCntrMutex);
10463526Sxy150489 	mutex_destroy(&Adapter->e1000g_timeout_lock);
10473526Sxy150489 	rw_destroy(&Adapter->chip_lock);
10483526Sxy150489 }
10493526Sxy150489 
10503526Sxy150489 static int
10513526Sxy150489 e1000g_resume(dev_info_t *devinfo)
10523526Sxy150489 {
10533526Sxy150489 	struct e1000g *Adapter;
10543526Sxy150489 
10553526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
10563526Sxy150489 	if (Adapter == NULL)
10573526Sxy150489 		return (DDI_FAILURE);
10583526Sxy150489 
10593526Sxy150489 	if (e1000g_start(Adapter))
10603526Sxy150489 		return (DDI_FAILURE);
10613526Sxy150489 
10623526Sxy150489 	return (DDI_SUCCESS);
10633526Sxy150489 }
10643526Sxy150489 
10653526Sxy150489 static int
10663526Sxy150489 e1000g_suspend(dev_info_t *devinfo)
10673526Sxy150489 {
10683526Sxy150489 	struct e1000g *Adapter;
10693526Sxy150489 
10703526Sxy150489 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
10713526Sxy150489 	if (Adapter == NULL)
10723526Sxy150489 		return (DDI_FAILURE);
10733526Sxy150489 
10743526Sxy150489 	e1000g_stop(Adapter);
10753526Sxy150489 
10763526Sxy150489 	return (DDI_SUCCESS);
10773526Sxy150489 }
10783526Sxy150489 
10793526Sxy150489 static int
10803526Sxy150489 e1000g_init(struct e1000g *Adapter)
10813526Sxy150489 {
10823526Sxy150489 	uint32_t pba;
10833526Sxy150489 	uint32_t ctrl;
10843526Sxy150489 	struct e1000_hw *hw;
10854061Sxy150489 	clock_t link_timeout;
10863526Sxy150489 
10873526Sxy150489 	hw = &Adapter->Shared;
10883526Sxy150489 
10893526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
10903526Sxy150489 
10913526Sxy150489 	/* Preserve manageability features */
10923526Sxy150489 	e1000_check_phy_reset_block(hw);
10933526Sxy150489 
10943526Sxy150489 	/*
10953526Sxy150489 	 * reset to put the hardware in a known state
10963526Sxy150489 	 * before we try to do anything with the eeprom
10973526Sxy150489 	 */
10983526Sxy150489 	(void) e1000_reset_hw(hw);
10993526Sxy150489 
11003526Sxy150489 	(void) e1000_init_eeprom_params(hw);
11013526Sxy150489 
11023526Sxy150489 	if (e1000_validate_eeprom_checksum(hw) < 0) {
11034061Sxy150489 		/*
11044061Sxy150489 		 * Some PCI-E parts fail the first check due to
11054061Sxy150489 		 * the link being in sleep state.  Call it again,
11064061Sxy150489 		 * if it fails a second time its a real issue.
11074061Sxy150489 		 */
11084061Sxy150489 		if (e1000_validate_eeprom_checksum(hw) < 0) {
11094061Sxy150489 			e1000g_log(Adapter, CE_WARN,
11104061Sxy150489 			    "Invalid EEPROM checksum. Please contact "
11114061Sxy150489 			    "the vendor to update the EEPROM.");
11124061Sxy150489 			goto init_fail;
11134061Sxy150489 		}
11143526Sxy150489 	}
11153526Sxy150489 
11163526Sxy150489 #ifdef __sparc
11173526Sxy150489 	/*
11183526Sxy150489 	 * Firstly, we try to get the local ethernet address from OBP. If
11193526Sxy150489 	 * fail, we get from EEPROM of NIC card.
11203526Sxy150489 	 */
11213526Sxy150489 	if (!e1000g_find_mac_address(Adapter)) {
11223526Sxy150489 		if (e1000_read_mac_addr(hw) < 0) {
11233526Sxy150489 			e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
11243526Sxy150489 			goto init_fail;
11253526Sxy150489 		}
11263526Sxy150489 	}
11273526Sxy150489 #else
11283526Sxy150489 	/* Get the local ethernet address. */
11293526Sxy150489 	if (e1000_read_mac_addr(hw) < 0) {
11303526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
11313526Sxy150489 		goto init_fail;
11323526Sxy150489 	}
11333526Sxy150489 #endif
11343526Sxy150489 
11353526Sxy150489 	/* check for valid mac address */
11363526Sxy150489 	if (!is_valid_mac_addr(hw->mac_addr)) {
11373526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
11383526Sxy150489 		goto init_fail;
11393526Sxy150489 	}
11403526Sxy150489 
11413526Sxy150489 	e1000_get_bus_info(hw);
11423526Sxy150489 
11433526Sxy150489 	/* Master Latency Timer implementation */
11443526Sxy150489 	if (Adapter->MasterLatencyTimer) {
11453526Sxy150489 		pci_config_put8(Adapter->handle, PCI_CONF_LATENCY_TIMER,
11463526Sxy150489 		    Adapter->MasterLatencyTimer);
11473526Sxy150489 	}
11483526Sxy150489 
11493526Sxy150489 	if (hw->mac_type < e1000_82547) {
11503526Sxy150489 		/*
11513526Sxy150489 		 * Total FIFO is 64K
11523526Sxy150489 		 */
11533526Sxy150489 		if (hw->max_frame_size > FRAME_SIZE_UPTO_8K)
11543526Sxy150489 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
11553526Sxy150489 		else
11563526Sxy150489 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
11573526Sxy150489 	} else if (hw->mac_type >= e1000_82571 &&
11584349Sxy150489 	    hw->mac_type <= e1000_82572) {
11593526Sxy150489 		/*
11603526Sxy150489 		 * Total FIFO is 48K
11613526Sxy150489 		 */
11623526Sxy150489 		if (hw->max_frame_size > FRAME_SIZE_UPTO_8K)
11633526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
11643526Sxy150489 		else
11653526Sxy150489 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
11663526Sxy150489 	} else if (hw->mac_type == e1000_ich8lan) {
11673526Sxy150489 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
11683526Sxy150489 	} else {
11693526Sxy150489 		/*
11703526Sxy150489 		 * Total FIFO is 40K
11713526Sxy150489 		 */
11723526Sxy150489 		if (hw->max_frame_size > FRAME_SIZE_UPTO_8K)
11733526Sxy150489 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
11743526Sxy150489 		else
11753526Sxy150489 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
11763526Sxy150489 	}
11773526Sxy150489 	E1000_WRITE_REG(hw, PBA, pba);
11783526Sxy150489 
11793526Sxy150489 	/*
11803526Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
11813526Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
11823526Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
11833526Sxy150489 	 * file.
11843526Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
11853526Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
11863526Sxy150489 	 * high-water mark.
11873526Sxy150489 	 */
11883526Sxy150489 	hw->fc_high_water =
11893526Sxy150489 	    ((pba & E1000_PBA_MASK) << E1000_PBA_SHIFT) -
11903526Sxy150489 	    E1000_FC_HIGH_DIFF;
11913526Sxy150489 	hw->fc_low_water =
11923526Sxy150489 	    ((pba & E1000_PBA_MASK) << E1000_PBA_SHIFT) -
11933526Sxy150489 	    E1000_FC_LOW_DIFF;
11943526Sxy150489 	hw->fc_pause_time = E1000_FC_PAUSE_TIME;
11953526Sxy150489 	hw->fc_send_xon = B_TRUE;
11963526Sxy150489 
11973526Sxy150489 	/*
11983526Sxy150489 	 * Reset the adapter hardware the second time.
11993526Sxy150489 	 */
12003526Sxy150489 	(void) e1000_reset_hw(hw);
12013526Sxy150489 
12023526Sxy150489 	/* disable wakeup control by default */
12033526Sxy150489 	if (hw->mac_type >= e1000_82544)
12043526Sxy150489 		E1000_WRITE_REG(hw, WUC, 0);
12053526Sxy150489 
12063526Sxy150489 	/* MWI setup */
12073526Sxy150489 	if (Adapter->MWIEnable) {
12083526Sxy150489 		hw->pci_cmd_word |= CMD_MEM_WRT_INVALIDATE;
12093526Sxy150489 		e1000_pci_set_mwi(hw);
12103526Sxy150489 	} else
12113526Sxy150489 		e1000_pci_clear_mwi(hw);
12123526Sxy150489 
12133526Sxy150489 	/*
12143526Sxy150489 	 * Configure/Initialize hardware
12153526Sxy150489 	 */
12163526Sxy150489 	if (e1000_init_hw(hw) < 0) {
12173526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
12183526Sxy150489 		goto init_fail;
12193526Sxy150489 	}
12203526Sxy150489 
12213526Sxy150489 	/* Disable Smart Power Down */
12223526Sxy150489 	phy_spd_state(hw, B_FALSE);
12233526Sxy150489 
12243526Sxy150489 	/*
12253526Sxy150489 	 * Initialize unicast addresses.
12263526Sxy150489 	 */
12273526Sxy150489 	e1000g_init_unicst(Adapter);
12283526Sxy150489 
12293526Sxy150489 	/*
12303526Sxy150489 	 * Setup and initialize the transmit structures.
12313526Sxy150489 	 */
12323526Sxy150489 	SetupTransmitStructures(Adapter);
12333526Sxy150489 	DelayInMilliseconds(5);
12343526Sxy150489 
12353526Sxy150489 	/*
12363526Sxy150489 	 * Setup and initialize the mctable structures.  After this routine
12373526Sxy150489 	 * completes  Multicast table will be set
12383526Sxy150489 	 */
12393526Sxy150489 	SetupMulticastTable(Adapter);
12403526Sxy150489 	DelayInMilliseconds(5);
12413526Sxy150489 
12423526Sxy150489 	/*
12433526Sxy150489 	 * Setup and initialize the receive structures.  After this routine
12443526Sxy150489 	 * completes we can receive packets off of the wire.
12453526Sxy150489 	 */
12463526Sxy150489 	SetupReceiveStructures(Adapter);
12473526Sxy150489 	DelayInMilliseconds(5);
12483526Sxy150489 
12493526Sxy150489 	/*
12503526Sxy150489 	 * Implement Adaptive IFS
12513526Sxy150489 	 */
12523526Sxy150489 	e1000_reset_adaptive(hw);
12533526Sxy150489 
12543526Sxy150489 	/* Setup Interrupt Throttling Register */
12553526Sxy150489 	E1000_WRITE_REG(hw, ITR, Adapter->intr_throttling_rate);
12563526Sxy150489 
12574061Sxy150489 	/* Start the timer for link setup */
12584061Sxy150489 	if (hw->autoneg)
12594061Sxy150489 		link_timeout = PHY_AUTO_NEG_TIME * drv_usectohz(100000);
12604061Sxy150489 	else
12614061Sxy150489 		link_timeout = PHY_FORCE_TIME * drv_usectohz(100000);
12624061Sxy150489 
12634061Sxy150489 	mutex_enter(&Adapter->e1000g_linklock);
12644061Sxy150489 	if (hw->wait_autoneg_complete) {
12654061Sxy150489 		Adapter->link_complete = B_TRUE;
12663526Sxy150489 	} else {
12674061Sxy150489 		Adapter->link_complete = B_FALSE;
12684061Sxy150489 		Adapter->link_tid = timeout(e1000g_link_timer,
12694061Sxy150489 		    (void *)Adapter, link_timeout);
12703526Sxy150489 	}
12714061Sxy150489 	mutex_exit(&Adapter->e1000g_linklock);
12723526Sxy150489 
12733526Sxy150489 	/* Enable PCI-Ex master */
12743526Sxy150489 	if (hw->bus_type == e1000_bus_type_pci_express) {
12753526Sxy150489 		e1000_enable_pciex_master(hw);
12763526Sxy150489 	}
12773526Sxy150489 
12783526Sxy150489 	Adapter->init_count++;
12793526Sxy150489 
12803526Sxy150489 	rw_exit(&Adapter->chip_lock);
12813526Sxy150489 
12823526Sxy150489 	return (DDI_SUCCESS);
12833526Sxy150489 
12843526Sxy150489 init_fail:
12853526Sxy150489 	rw_exit(&Adapter->chip_lock);
12863526Sxy150489 	return (DDI_FAILURE);
12873526Sxy150489 }
12883526Sxy150489 
12893526Sxy150489 /*
12903526Sxy150489  * Check if the link is up
12913526Sxy150489  */
12923526Sxy150489 static boolean_t
12933526Sxy150489 e1000g_link_up(struct e1000g *Adapter)
12943526Sxy150489 {
12953526Sxy150489 	struct e1000_hw *hw;
12963526Sxy150489 	boolean_t link_up;
12973526Sxy150489 
12983526Sxy150489 	hw = &Adapter->Shared;
12993526Sxy150489 
13003526Sxy150489 	/* Ensure this is set to get accurate copper link status */
13013526Sxy150489 	hw->get_link_status = B_TRUE;
13023526Sxy150489 
13033526Sxy150489 	e1000_check_for_link(hw);
13043526Sxy150489 
13053526Sxy150489 	if ((E1000_READ_REG(hw, STATUS) & E1000_STATUS_LU) ||
13063526Sxy150489 	    ((!hw->get_link_status) && (hw->mac_type == e1000_82543)) ||
13073526Sxy150489 	    ((hw->media_type == e1000_media_type_internal_serdes) &&
13084349Sxy150489 	    (!hw->serdes_link_down))) {
13093526Sxy150489 		link_up = B_TRUE;
13103526Sxy150489 	} else {
13113526Sxy150489 		link_up = B_FALSE;
13123526Sxy150489 	}
13133526Sxy150489 
13143526Sxy150489 	return (link_up);
13153526Sxy150489 }
13163526Sxy150489 
13173526Sxy150489 static void
13183526Sxy150489 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
13193526Sxy150489 {
13203526Sxy150489 	struct iocblk *iocp;
13213526Sxy150489 	struct e1000g *e1000gp;
13223526Sxy150489 	enum ioc_reply status;
13233526Sxy150489 	int err;
13243526Sxy150489 
13253526Sxy150489 	iocp = (struct iocblk *)mp->b_rptr;
13263526Sxy150489 	iocp->ioc_error = 0;
13273526Sxy150489 	e1000gp = (struct e1000g *)arg;
13283526Sxy150489 
13293526Sxy150489 	ASSERT(e1000gp);
13303526Sxy150489 	if (e1000gp == NULL) {
13313526Sxy150489 		miocnak(q, mp, 0, EINVAL);
13323526Sxy150489 		return;
13333526Sxy150489 	}
13343526Sxy150489 
13353526Sxy150489 	switch (iocp->ioc_cmd) {
13363526Sxy150489 
13373526Sxy150489 	case LB_GET_INFO_SIZE:
13383526Sxy150489 	case LB_GET_INFO:
13393526Sxy150489 	case LB_GET_MODE:
13403526Sxy150489 	case LB_SET_MODE:
13413526Sxy150489 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
13423526Sxy150489 		break;
13433526Sxy150489 
13443526Sxy150489 	case ND_GET:
13453526Sxy150489 	case ND_SET:
13463526Sxy150489 		status = e1000g_nd_ioctl(e1000gp, q, mp, iocp);
13473526Sxy150489 		break;
13483526Sxy150489 
13493526Sxy150489 	case E1000G_IOC_REG_PEEK:
13503526Sxy150489 	case E1000G_IOC_REG_POKE:
13513526Sxy150489 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
13523526Sxy150489 		break;
13533526Sxy150489 	case E1000G_IOC_CHIP_RESET:
13543526Sxy150489 		e1000gp->reset_count++;
13553526Sxy150489 		if (e1000g_reset(e1000gp))
13563526Sxy150489 			status = IOC_ACK;
13573526Sxy150489 		else
13583526Sxy150489 			status = IOC_INVAL;
13593526Sxy150489 		break;
13603526Sxy150489 	default:
13613526Sxy150489 		status = IOC_INVAL;
13623526Sxy150489 		break;
13633526Sxy150489 	}
13643526Sxy150489 
13653526Sxy150489 	/*
13663526Sxy150489 	 * Decide how to reply
13673526Sxy150489 	 */
13683526Sxy150489 	switch (status) {
13693526Sxy150489 	default:
13703526Sxy150489 	case IOC_INVAL:
13713526Sxy150489 		/*
13723526Sxy150489 		 * Error, reply with a NAK and EINVAL or the specified error
13733526Sxy150489 		 */
13743526Sxy150489 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
13754349Sxy150489 		    EINVAL : iocp->ioc_error);
13763526Sxy150489 		break;
13773526Sxy150489 
13783526Sxy150489 	case IOC_DONE:
13793526Sxy150489 		/*
13803526Sxy150489 		 * OK, reply already sent
13813526Sxy150489 		 */
13823526Sxy150489 		break;
13833526Sxy150489 
13843526Sxy150489 	case IOC_ACK:
13853526Sxy150489 		/*
13863526Sxy150489 		 * OK, reply with an ACK
13873526Sxy150489 		 */
13883526Sxy150489 		miocack(q, mp, 0, 0);
13893526Sxy150489 		break;
13903526Sxy150489 
13913526Sxy150489 	case IOC_REPLY:
13923526Sxy150489 		/*
13933526Sxy150489 		 * OK, send prepared reply as ACK or NAK
13943526Sxy150489 		 */
13953526Sxy150489 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
13964349Sxy150489 		    M_IOCACK : M_IOCNAK;
13973526Sxy150489 		qreply(q, mp);
13983526Sxy150489 		break;
13993526Sxy150489 	}
14003526Sxy150489 }
14013526Sxy150489 
14023526Sxy150489 static void e1000g_m_blank(void *arg, time_t ticks, uint32_t count)
14033526Sxy150489 {
14043526Sxy150489 	struct e1000g *Adapter;
14053526Sxy150489 
14063526Sxy150489 	Adapter = (struct e1000g *)arg;
14073526Sxy150489 
14083526Sxy150489 	/*
14093526Sxy150489 	 * Adjust ITR (Interrupt Throttling Register) to coalesce
14103526Sxy150489 	 * interrupts. This formula and its coefficient come from
14113526Sxy150489 	 * our experiments.
14123526Sxy150489 	 */
14133526Sxy150489 	if (Adapter->intr_adaptive) {
14143526Sxy150489 		Adapter->intr_throttling_rate = count << 5;
14153526Sxy150489 		E1000_WRITE_REG(&Adapter->Shared, ITR,
14163526Sxy150489 		    Adapter->intr_throttling_rate);
14173526Sxy150489 	}
14183526Sxy150489 }
14193526Sxy150489 
14203526Sxy150489 static void
14213526Sxy150489 e1000g_m_resources(void *arg)
14223526Sxy150489 {
14233526Sxy150489 	struct e1000g *adapter = (struct e1000g *)arg;
14243526Sxy150489 	mac_rx_fifo_t mrf;
14253526Sxy150489 
14263526Sxy150489 	mrf.mrf_type = MAC_RX_FIFO;
14273526Sxy150489 	mrf.mrf_blank = e1000g_m_blank;
14283526Sxy150489 	mrf.mrf_arg = (void *)adapter;
14293526Sxy150489 	mrf.mrf_normal_blank_time = E1000_RX_INTPT_TIME;
14303526Sxy150489 	mrf.mrf_normal_pkt_count = E1000_RX_PKT_CNT;
14313526Sxy150489 
14323526Sxy150489 	adapter->mrh = mac_resource_add(adapter->mh, (mac_resource_t *)&mrf);
14333526Sxy150489 }
14343526Sxy150489 
14353526Sxy150489 static int
14363526Sxy150489 e1000g_m_start(void *arg)
14373526Sxy150489 {
14383526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
14393526Sxy150489 
14403526Sxy150489 	return (e1000g_start(Adapter));
14413526Sxy150489 }
14423526Sxy150489 
14433526Sxy150489 static int
14443526Sxy150489 e1000g_start(struct e1000g *Adapter)
14453526Sxy150489 {
14463526Sxy150489 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
14473526Sxy150489 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
14483526Sxy150489 			e1000g_log(Adapter, CE_WARN,
14493526Sxy150489 			    "Adapter initialization failed");
14503526Sxy150489 			return (ENOTACTIVE);
14513526Sxy150489 		}
14523526Sxy150489 	}
14533526Sxy150489 
14543526Sxy150489 	enable_timeout(Adapter);
14553526Sxy150489 
14563526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
14573526Sxy150489 
14583526Sxy150489 	e1000g_EnableInterrupt(Adapter);
14593526Sxy150489 	if (Adapter->tx_intr_enable)
14603526Sxy150489 		e1000g_EnableTxInterrupt(Adapter);
14613526Sxy150489 
14623526Sxy150489 	Adapter->started = B_TRUE;
14633526Sxy150489 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
14643526Sxy150489 
14653526Sxy150489 	rw_exit(&Adapter->chip_lock);
14663526Sxy150489 
14673526Sxy150489 	return (0);
14683526Sxy150489 }
14693526Sxy150489 
14703526Sxy150489 static void
14713526Sxy150489 e1000g_m_stop(void *arg)
14723526Sxy150489 {
14733526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
14743526Sxy150489 
14753526Sxy150489 	e1000g_stop(Adapter);
14763526Sxy150489 }
14773526Sxy150489 
14783526Sxy150489 static void
14793526Sxy150489 e1000g_stop(struct e1000g *Adapter)
14803526Sxy150489 {
14813526Sxy150489 	timeout_id_t tid;
14823526Sxy150489 	e1000g_tx_ring_t *tx_ring;
14834061Sxy150489 	boolean_t link_changed;
14843526Sxy150489 
14853526Sxy150489 	tx_ring = Adapter->tx_ring;
14863526Sxy150489 
14873526Sxy150489 	/* Set stop flags */
14883526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
14893526Sxy150489 
14903526Sxy150489 	Adapter->started = B_FALSE;
14913526Sxy150489 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
14923526Sxy150489 
14933526Sxy150489 	rw_exit(&Adapter->chip_lock);
14943526Sxy150489 
14953526Sxy150489 	/* Drain tx sessions */
14963526Sxy150489 	(void) e1000g_tx_drain(Adapter);
14973526Sxy150489 
14983526Sxy150489 	/* Disable timers */
14993526Sxy150489 	disable_timeout(Adapter);
15003526Sxy150489 
15014061Sxy150489 	/* Disable the tx timer for 82547 chipset */
15023526Sxy150489 	mutex_enter(&tx_ring->tx_lock);
15033526Sxy150489 	tx_ring->timer_enable_82547 = B_FALSE;
15043526Sxy150489 	tid = tx_ring->timer_id_82547;
15053526Sxy150489 	tx_ring->timer_id_82547 = 0;
15063526Sxy150489 	mutex_exit(&tx_ring->tx_lock);
15073526Sxy150489 
15083526Sxy150489 	if (tid != 0)
15093526Sxy150489 		(void) untimeout(tid);
15103526Sxy150489 
15114061Sxy150489 	/* Disable the link timer */
15124061Sxy150489 	mutex_enter(&Adapter->e1000g_linklock);
15134061Sxy150489 	tid = Adapter->link_tid;
15144061Sxy150489 	Adapter->link_tid = 0;
15154061Sxy150489 	mutex_exit(&Adapter->e1000g_linklock);
15164061Sxy150489 
15174061Sxy150489 	if (tid != 0)
15184061Sxy150489 		(void) untimeout(tid);
15194061Sxy150489 
15203526Sxy150489 	/* Stop the chip and release pending resources */
15213526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
15223526Sxy150489 
15233526Sxy150489 	e1000g_DisableAllInterrupts(Adapter);
15243526Sxy150489 
15253526Sxy150489 	e1000_reset_hw(&Adapter->Shared);
15263526Sxy150489 
15273526Sxy150489 	/* Release resources still held by the TX descriptors */
15284061Sxy150489 	e1000g_tx_drop(Adapter);
15294061Sxy150489 
15304061Sxy150489 	/* Clean the pending rx jumbo packet fragment */
15314061Sxy150489 	if (Adapter->rx_mblk != NULL) {
15324061Sxy150489 		freemsg(Adapter->rx_mblk);
15334061Sxy150489 		Adapter->rx_mblk = NULL;
15344061Sxy150489 		Adapter->rx_mblk_tail = NULL;
15354061Sxy150489 		Adapter->rx_packet_len = 0;
15364061Sxy150489 	}
15374061Sxy150489 
15384061Sxy150489 	rw_exit(&Adapter->chip_lock);
15394061Sxy150489 }
15404061Sxy150489 
15414061Sxy150489 static void
15424061Sxy150489 e1000g_tx_drop(struct e1000g *Adapter)
15434061Sxy150489 {
15444061Sxy150489 	e1000g_tx_ring_t *tx_ring;
15454061Sxy150489 	e1000g_msg_chain_t *msg_chain;
15464061Sxy150489 	PTX_SW_PACKET packet;
15474061Sxy150489 	mblk_t *mp;
15484061Sxy150489 	mblk_t *nmp;
15494061Sxy150489 	uint32_t packet_count;
15504061Sxy150489 
15514061Sxy150489 	tx_ring = Adapter->tx_ring;
15524061Sxy150489 
15533526Sxy150489 	/*
15543526Sxy150489 	 * Here we don't need to protect the lists using
15553526Sxy150489 	 * the usedlist_lock and freelist_lock, for they
15563526Sxy150489 	 * have been protected by the chip_lock.
15573526Sxy150489 	 */
15583526Sxy150489 	mp = NULL;
15593526Sxy150489 	nmp = NULL;
15604061Sxy150489 	packet_count = 0;
15613526Sxy150489 	packet = (PTX_SW_PACKET) QUEUE_GET_HEAD(&tx_ring->used_list);
15623526Sxy150489 	while (packet != NULL) {
15633526Sxy150489 		if (packet->mp != NULL) {
15643526Sxy150489 			/* Assemble the message chain */
15653526Sxy150489 			if (mp == NULL) {
15663526Sxy150489 				mp = packet->mp;
15673526Sxy150489 				nmp = packet->mp;
15683526Sxy150489 			} else {
15693526Sxy150489 				nmp->b_next = packet->mp;
15703526Sxy150489 				nmp = packet->mp;
15713526Sxy150489 			}
15723526Sxy150489 			/* Disconnect the message from the sw packet */
15733526Sxy150489 			packet->mp = NULL;
15743526Sxy150489 		}
15753526Sxy150489 
15763526Sxy150489 		FreeTxSwPacket(packet);
15774061Sxy150489 		packet_count++;
15783526Sxy150489 
15793526Sxy150489 		packet = (PTX_SW_PACKET)
15803526Sxy150489 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
15813526Sxy150489 	}
15823526Sxy150489 
15833526Sxy150489 	if (mp != NULL) {
15843526Sxy150489 		msg_chain = Adapter->tx_msg_chain;
15853526Sxy150489 		mutex_enter(&msg_chain->lock);
15863526Sxy150489 		if (msg_chain->head == NULL) {
15873526Sxy150489 			msg_chain->head = mp;
15883526Sxy150489 			msg_chain->tail = nmp;
15893526Sxy150489 		} else {
15903526Sxy150489 			msg_chain->tail->b_next = mp;
15913526Sxy150489 			msg_chain->tail = nmp;
15923526Sxy150489 		}
15933526Sxy150489 		mutex_exit(&msg_chain->lock);
15943526Sxy150489 	}
15953526Sxy150489 
15964061Sxy150489 	ddi_intr_trigger_softint(Adapter->tx_softint_handle, NULL);
15974061Sxy150489 
15984061Sxy150489 	if (packet_count > 0) {
15994061Sxy150489 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
16004061Sxy150489 		QUEUE_INIT_LIST(&tx_ring->used_list);
16014061Sxy150489 
16024061Sxy150489 		/* Setup TX descriptor pointers */
16034061Sxy150489 		tx_ring->tbd_next = tx_ring->tbd_first;
16044061Sxy150489 		tx_ring->tbd_oldest = tx_ring->tbd_first;
16054061Sxy150489 
16064061Sxy150489 		/* Setup our HW Tx Head & Tail descriptor pointers */
16074061Sxy150489 		E1000_WRITE_REG(&Adapter->Shared, TDH, 0);
16084061Sxy150489 		E1000_WRITE_REG(&Adapter->Shared, TDT, 0);
16093526Sxy150489 	}
16103526Sxy150489 }
16113526Sxy150489 
16123526Sxy150489 static boolean_t
16133526Sxy150489 e1000g_tx_drain(struct e1000g *Adapter)
16143526Sxy150489 {
16153526Sxy150489 	int i;
16163526Sxy150489 	boolean_t done;
16173526Sxy150489 	e1000g_tx_ring_t *tx_ring;
16183526Sxy150489 
16193526Sxy150489 	tx_ring = Adapter->tx_ring;
16203526Sxy150489 
16213526Sxy150489 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
16223526Sxy150489 	for (i = 0; i < WSDRAINTIME; i++) {
16233526Sxy150489 		mutex_enter(&tx_ring->usedlist_lock);
16243526Sxy150489 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
16253526Sxy150489 		mutex_exit(&tx_ring->usedlist_lock);
16263526Sxy150489 
16273526Sxy150489 		if (done)
16283526Sxy150489 			break;
16293526Sxy150489 
16303526Sxy150489 		msec_delay(1);
16313526Sxy150489 	}
16323526Sxy150489 
16333526Sxy150489 	return (done);
16343526Sxy150489 }
16353526Sxy150489 
16363526Sxy150489 static boolean_t
16373526Sxy150489 e1000g_rx_drain(struct e1000g *Adapter)
16383526Sxy150489 {
16393526Sxy150489 	boolean_t done;
16403526Sxy150489 
16413526Sxy150489 	mutex_enter(&Adapter->rx_ring->freelist_lock);
16423526Sxy150489 	done = (Adapter->rx_avail_freepkt == Adapter->NumRxFreeList);
16433526Sxy150489 	mutex_exit(&Adapter->rx_ring->freelist_lock);
16443526Sxy150489 
16453526Sxy150489 	return (done);
16463526Sxy150489 }
16473526Sxy150489 
16484061Sxy150489 boolean_t
16493526Sxy150489 e1000g_reset(struct e1000g *Adapter)
16503526Sxy150489 {
16513526Sxy150489 	e1000g_stop(Adapter);
16523526Sxy150489 
16533526Sxy150489 	if (e1000g_start(Adapter)) {
16543526Sxy150489 		e1000g_log(Adapter, CE_WARN, "Reset failed");
16553526Sxy150489 		return (B_FALSE);
16563526Sxy150489 	}
16573526Sxy150489 
16583526Sxy150489 	return (B_TRUE);
16593526Sxy150489 }
16603526Sxy150489 
16613526Sxy150489 /*
16623526Sxy150489  * **********************************************************************
16633526Sxy150489  * Name:	e1000g_intr_pciexpress					*
16643526Sxy150489  *									*
16653526Sxy150489  * Description:								*
16663526Sxy150489  *	This interrupt service routine is for PCI-Express adapters.	*
16673526Sxy150489  *	The ICR contents is valid only when the E1000_ICR_INT_ASSERTED	*
16683526Sxy150489  *	bit is set.							*
16693526Sxy150489  *									*
16703526Sxy150489  * Parameter Passed:							*
16713526Sxy150489  *									*
16723526Sxy150489  * Return Value:							*
16733526Sxy150489  *									*
16743526Sxy150489  * Functions called:							*
16753526Sxy150489  *	e1000g_intr_work						*
16763526Sxy150489  *									*
16773526Sxy150489  * **********************************************************************
16783526Sxy150489  */
16793526Sxy150489 static uint_t
16803526Sxy150489 e1000g_intr_pciexpress(caddr_t arg)
16813526Sxy150489 {
16823526Sxy150489 	struct e1000g *Adapter;
16833526Sxy150489 	uint32_t ICRContents;
16843526Sxy150489 
16853526Sxy150489 	Adapter = (struct e1000g *)arg;
16863526Sxy150489 	ICRContents = E1000_READ_REG(&Adapter->Shared, ICR);
16873526Sxy150489 
16883526Sxy150489 	if (ICRContents & E1000_ICR_INT_ASSERTED) {
16893526Sxy150489 		/*
16903526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
16913526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
16923526Sxy150489 		 * look for work to do.
16933526Sxy150489 		 */
16943526Sxy150489 		e1000g_intr_work(Adapter, ICRContents);
16953526Sxy150489 		return (DDI_INTR_CLAIMED);
16963526Sxy150489 	} else {
16973526Sxy150489 		/*
16983526Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
16993526Sxy150489 		 * Don't claim this interrupt, return immediately.
17003526Sxy150489 		 */
17013526Sxy150489 		return (DDI_INTR_UNCLAIMED);
17023526Sxy150489 	}
17033526Sxy150489 }
17043526Sxy150489 
17053526Sxy150489 /*
17063526Sxy150489  * **********************************************************************
17073526Sxy150489  * Name:	e1000g_intr						*
17083526Sxy150489  *									*
17093526Sxy150489  * Description:								*
17103526Sxy150489  *	This interrupt service routine is for PCI/PCI-X adapters.	*
17113526Sxy150489  *	We check the ICR contents no matter the E1000_ICR_INT_ASSERTED	*
17123526Sxy150489  *	bit is set or not.						*
17133526Sxy150489  *									*
17143526Sxy150489  * Parameter Passed:							*
17153526Sxy150489  *									*
17163526Sxy150489  * Return Value:							*
17173526Sxy150489  *									*
17183526Sxy150489  * Functions called:							*
17193526Sxy150489  *	e1000g_intr_work						*
17203526Sxy150489  *									*
17213526Sxy150489  * **********************************************************************
17223526Sxy150489  */
17233526Sxy150489 static uint_t
17243526Sxy150489 e1000g_intr(caddr_t arg)
17253526Sxy150489 {
17263526Sxy150489 	struct e1000g *Adapter;
17273526Sxy150489 	uint32_t ICRContents;
17283526Sxy150489 
17293526Sxy150489 	Adapter = (struct e1000g *)arg;
17303526Sxy150489 	ICRContents = E1000_READ_REG(&Adapter->Shared, ICR);
17313526Sxy150489 
17323526Sxy150489 	if (ICRContents) {
17333526Sxy150489 		/*
17343526Sxy150489 		 * Any bit was set in ICR:
17353526Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
17363526Sxy150489 		 * look for work to do.
17373526Sxy150489 		 */
17383526Sxy150489 		e1000g_intr_work(Adapter, ICRContents);
17393526Sxy150489 		return (DDI_INTR_CLAIMED);
17403526Sxy150489 	} else {
17413526Sxy150489 		/*
17423526Sxy150489 		 * No bit was set in ICR:
17433526Sxy150489 		 * Don't claim this interrupt, return immediately.
17443526Sxy150489 		 */
17453526Sxy150489 		return (DDI_INTR_UNCLAIMED);
17463526Sxy150489 	}
17473526Sxy150489 }
17483526Sxy150489 
17493526Sxy150489 /*
17503526Sxy150489  * **********************************************************************
17513526Sxy150489  * Name:	e1000g_intr_work					*
17523526Sxy150489  *									*
17533526Sxy150489  * Description:								*
17543526Sxy150489  *	Called from interrupt service routines.				*
17553526Sxy150489  *	Read(clear) the ICR contents and call appropriate interrupt	*
17563526Sxy150489  *	processing routines.						*
17573526Sxy150489  *									*
17583526Sxy150489  * Parameter Passed:							*
17593526Sxy150489  *									*
17603526Sxy150489  * Return Value:							*
17613526Sxy150489  *									*
17623526Sxy150489  * Functions called:							*
17634061Sxy150489  *	e1000g_receive							*
17644061Sxy150489  *	e1000g_link_check						*
17654061Sxy150489  *	e1000g_recycle							*
17663526Sxy150489  *									*
17673526Sxy150489  * **********************************************************************
17683526Sxy150489  */
17693526Sxy150489 static void
17703526Sxy150489 e1000g_intr_work(struct e1000g *Adapter, uint32_t ICRContents)
17713526Sxy150489 {
17723526Sxy150489 	if (ICRContents & E1000_ICR_RXT0) {
17733526Sxy150489 		mblk_t *mp;
17743526Sxy150489 
17753526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_READER);
17763526Sxy150489 		/*
17773526Sxy150489 		 * Here we need to check the "started" flag to ensure the
17783526Sxy150489 		 * receive routine will not execute when the adapter is
17793526Sxy150489 		 * stopped or being reset.
17803526Sxy150489 		 */
17813526Sxy150489 		if (Adapter->started) {
17823526Sxy150489 			mutex_enter(&Adapter->rx_ring->rx_lock);
17833526Sxy150489 			mp = e1000g_receive(Adapter);
17843526Sxy150489 			mutex_exit(&Adapter->rx_ring->rx_lock);
17853526Sxy150489 
17863526Sxy150489 			rw_exit(&Adapter->chip_lock);
17873526Sxy150489 
17883526Sxy150489 			if (mp != NULL)
17893526Sxy150489 				mac_rx(Adapter->mh, Adapter->mrh, mp);
17903526Sxy150489 		} else {
17913526Sxy150489 			rw_exit(&Adapter->chip_lock);
17923526Sxy150489 		}
17933526Sxy150489 	}
17943526Sxy150489 
17953526Sxy150489 	/*
17963526Sxy150489 	 * The Receive Sequence errors RXSEQ and the link status change LSC
17973526Sxy150489 	 * are checked to detect that the cable has been pulled out. For
17983526Sxy150489 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
17993526Sxy150489 	 * are an indication that cable is not connected.
18003526Sxy150489 	 */
18013526Sxy150489 	if ((ICRContents & E1000_ICR_RXSEQ) ||
18023526Sxy150489 	    (ICRContents & E1000_ICR_LSC) ||
18033526Sxy150489 	    (ICRContents & E1000_ICR_GPI_EN1)) {
18044061Sxy150489 		boolean_t link_changed;
18054061Sxy150489 		timeout_id_t tid = 0;
18063526Sxy150489 
18073526Sxy150489 		/*
18083526Sxy150489 		 * Encountered RX Sequence Error!!! Link maybe forced and
18093526Sxy150489 		 * the cable may have just been disconnected so we will
18103526Sxy150489 		 * read the LOS to see.
18113526Sxy150489 		 */
18123526Sxy150489 		if (ICRContents & E1000_ICR_RXSEQ)
18133526Sxy150489 			Adapter->rx_seq_intr++;
18143526Sxy150489 
18153526Sxy150489 		stop_timeout(Adapter);
18163526Sxy150489 
18173526Sxy150489 		mutex_enter(&Adapter->e1000g_linklock);
18184061Sxy150489 		/* e1000g_link_check takes care of link status change */
18194061Sxy150489 		link_changed = e1000g_link_check(Adapter);
18204061Sxy150489 		/*
18214061Sxy150489 		 * If the link timer has not timed out, we'll not notify
18224061Sxy150489 		 * the upper layer with any link state until the link
18234061Sxy150489 		 * is up.
18244061Sxy150489 		 */
18254061Sxy150489 		if (link_changed && !Adapter->link_complete) {
18264061Sxy150489 			if (Adapter->link_state == LINK_STATE_UP) {
18274061Sxy150489 				Adapter->link_complete = B_TRUE;
18284061Sxy150489 				tid = Adapter->link_tid;
18294061Sxy150489 				Adapter->link_tid = 0;
18304061Sxy150489 			} else {
18314061Sxy150489 				link_changed = B_FALSE;
18324061Sxy150489 			}
18334061Sxy150489 		}
18343526Sxy150489 		mutex_exit(&Adapter->e1000g_linklock);
18353526Sxy150489 
18364061Sxy150489 		if (link_changed) {
18374061Sxy150489 			if (tid != 0)
18384061Sxy150489 				(void) untimeout(tid);
18394061Sxy150489 
18404139Sxy150489 			/*
18414139Sxy150489 			 * Workaround for esb2. Data stuck in fifo on a link
18424139Sxy150489 			 * down event. Reset the adapter to recover it.
18434139Sxy150489 			 */
18444139Sxy150489 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
18454139Sxy150489 			    (Adapter->Shared.mac_type == e1000_80003es2lan))
18464139Sxy150489 				(void) e1000g_reset(Adapter);
18474139Sxy150489 
18484061Sxy150489 			mac_link_update(Adapter->mh, Adapter->link_state);
18493526Sxy150489 		}
18503526Sxy150489 
18513526Sxy150489 		start_timeout(Adapter);
18523526Sxy150489 	}
18533526Sxy150489 
18543526Sxy150489 	if (ICRContents & E1000G_ICR_TX_INTR) {
18553526Sxy150489 		if (!Adapter->tx_intr_enable)
18563526Sxy150489 			e1000g_DisableTxInterrupt(Adapter);
18573526Sxy150489 		/* Schedule the re-transmit */
18583526Sxy150489 		if (Adapter->resched_needed) {
18593526Sxy150489 			Adapter->tx_reschedule++;
18603526Sxy150489 			Adapter->resched_needed = B_FALSE;
18613526Sxy150489 			mac_tx_update(Adapter->mh);
18623526Sxy150489 		}
18633526Sxy150489 		if (Adapter->tx_intr_enable) {
18643526Sxy150489 			/* Recycle the tx descriptors */
18653526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_READER);
18663526Sxy150489 			Adapter->tx_recycle_intr++;
18673526Sxy150489 			e1000g_recycle(Adapter->tx_ring);
18683526Sxy150489 			rw_exit(&Adapter->chip_lock);
18693526Sxy150489 			/* Free the recycled messages */
18703526Sxy150489 			ddi_intr_trigger_softint(Adapter->tx_softint_handle,
18713526Sxy150489 			    NULL);
18723526Sxy150489 		}
18733526Sxy150489 	}
18743526Sxy150489 }
18753526Sxy150489 
18763526Sxy150489 static void
18773526Sxy150489 e1000g_init_unicst(struct e1000g *Adapter)
18783526Sxy150489 {
18793526Sxy150489 	struct e1000_hw *hw;
18803526Sxy150489 	int slot;
18813526Sxy150489 
18823526Sxy150489 	hw = &Adapter->Shared;
18833526Sxy150489 
18843526Sxy150489 	if (Adapter->init_count == 0) {
18853526Sxy150489 		/* Initialize the multiple unicast addresses */
18863526Sxy150489 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
18873526Sxy150489 
18883526Sxy150489 		if ((hw->mac_type == e1000_82571) && hw->laa_is_present)
18893526Sxy150489 			Adapter->unicst_total--;
18903526Sxy150489 
18913526Sxy150489 		Adapter->unicst_avail = Adapter->unicst_total - 1;
18923526Sxy150489 
18933526Sxy150489 		/* Store the default mac address */
18943526Sxy150489 		e1000_rar_set(hw, hw->mac_addr, 0);
18953526Sxy150489 		if ((hw->mac_type == e1000_82571) && hw->laa_is_present)
18963526Sxy150489 			e1000_rar_set(hw, hw->mac_addr, LAST_RAR_ENTRY);
18973526Sxy150489 
18983526Sxy150489 		bcopy(hw->mac_addr, Adapter->unicst_addr[0].mac.addr,
18993526Sxy150489 		    ETHERADDRL);
19003526Sxy150489 		Adapter->unicst_addr[0].mac.set = 1;
19013526Sxy150489 
19023526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
19033526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 0;
19043526Sxy150489 	} else {
19053526Sxy150489 		/* Recover the default mac address */
19063526Sxy150489 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac_addr,
19073526Sxy150489 		    ETHERADDRL);
19083526Sxy150489 
19093526Sxy150489 		/* Store the default mac address */
19103526Sxy150489 		e1000_rar_set(hw, hw->mac_addr, 0);
19113526Sxy150489 		if ((hw->mac_type == e1000_82571) && hw->laa_is_present)
19123526Sxy150489 			e1000_rar_set(hw, hw->mac_addr, LAST_RAR_ENTRY);
19133526Sxy150489 
19143526Sxy150489 		/* Re-configure the RAR registers */
19153526Sxy150489 		for (slot = 1; slot < Adapter->unicst_total; slot++)
19163526Sxy150489 			e1000_rar_set(hw,
19173526Sxy150489 			    Adapter->unicst_addr[slot].mac.addr, slot);
19183526Sxy150489 	}
19193526Sxy150489 }
19203526Sxy150489 
19213526Sxy150489 static int
19223526Sxy150489 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
19233526Sxy150489 {
19243526Sxy150489 	struct e1000g *Adapter;
19253526Sxy150489 
19263526Sxy150489 	Adapter = (struct e1000g *)arg;
19273526Sxy150489 
19283526Sxy150489 	/* Store the default MAC address */
19293526Sxy150489 	bcopy(mac_addr, Adapter->Shared.mac_addr, ETHERADDRL);
19303526Sxy150489 
19313526Sxy150489 	/* Set MAC address in address slot 0, which is the default address */
19323526Sxy150489 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
19333526Sxy150489 }
19343526Sxy150489 
19353526Sxy150489 static int
19363526Sxy150489 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
19373526Sxy150489     mac_addr_slot_t slot)
19383526Sxy150489 {
19393526Sxy150489 	struct e1000_hw *hw;
19403526Sxy150489 
19413526Sxy150489 	hw = &Adapter->Shared;
19423526Sxy150489 
19433526Sxy150489 	/*
19443526Sxy150489 	 * Error if the address specified is a multicast or broadcast
19453526Sxy150489 	 * address.
19463526Sxy150489 	 */
19473526Sxy150489 	if (((mac_addr[0] & 01) == 1) ||
19483526Sxy150489 	    (bcmp(mac_addr, &etherbroadcastaddr, ETHERADDRL) == 0))
19493526Sxy150489 		return (EINVAL);
19503526Sxy150489 
19513526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
19523526Sxy150489 
19533526Sxy150489 	/*
19543526Sxy150489 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
19553526Sxy150489 	 * that requires the receiver to be in reset when any of the
19563526Sxy150489 	 * receive address registers (RAR regs) are accessed.  The first
19573526Sxy150489 	 * rev of Wiseman silicon also requires MWI to be disabled when
19583526Sxy150489 	 * a global reset or a receive reset is issued.  So before we
19593526Sxy150489 	 * initialize the RARs, we check the rev of the Wiseman controller
19603526Sxy150489 	 * and work around any necessary HW errata.
19613526Sxy150489 	 */
19623526Sxy150489 	if (hw->mac_type == e1000_82542_rev2_0) {
19633526Sxy150489 		e1000_pci_clear_mwi(hw);
19643526Sxy150489 		E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
19653526Sxy150489 		DelayInMilliseconds(5);
19663526Sxy150489 	}
19673526Sxy150489 
19683526Sxy150489 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
19693526Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
19703526Sxy150489 
19713526Sxy150489 	if (slot == 0) {
19723526Sxy150489 		if ((hw->mac_type == e1000_82571) && hw->laa_is_present)
19733526Sxy150489 			e1000_rar_set(hw, hw->mac_addr, LAST_RAR_ENTRY);
19743526Sxy150489 	}
19753526Sxy150489 
19763526Sxy150489 	/*
19773526Sxy150489 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
19783526Sxy150489 	 * put the receive in reset, and disabled MWI, to work around some
19793526Sxy150489 	 * HW errata.  Now we should take the receiver out of reset, and
19803526Sxy150489 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
19813526Sxy150489 	 */
19823526Sxy150489 	if (hw->mac_type == e1000_82542_rev2_0) {
19833526Sxy150489 		E1000_WRITE_REG(hw, RCTL, 0);
19843526Sxy150489 		DelayInMilliseconds(1);
19853526Sxy150489 		if (hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
19863526Sxy150489 			e1000_pci_set_mwi(hw);
19873526Sxy150489 		SetupReceiveStructures(Adapter);
19883526Sxy150489 	}
19893526Sxy150489 
19903526Sxy150489 	rw_exit(&Adapter->chip_lock);
19913526Sxy150489 
19923526Sxy150489 	return (0);
19933526Sxy150489 }
19943526Sxy150489 
19953526Sxy150489 /*
19963526Sxy150489  * e1000g_m_unicst_add() - will find an unused address slot, set the
19973526Sxy150489  * address value to the one specified, reserve that slot and enable
19983526Sxy150489  * the NIC to start filtering on the new MAC address.
19993526Sxy150489  * Returns 0 on success.
20003526Sxy150489  */
20013526Sxy150489 static int
20023526Sxy150489 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
20033526Sxy150489 {
20043526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
20053526Sxy150489 	mac_addr_slot_t slot;
20063526Sxy150489 	int err;
20073526Sxy150489 
20083526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
20093526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
20103526Sxy150489 		return (EINVAL);
20113526Sxy150489 
20123526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
20133526Sxy150489 	if (Adapter->unicst_avail == 0) {
20143526Sxy150489 		/* no slots available */
20153526Sxy150489 		rw_exit(&Adapter->chip_lock);
20163526Sxy150489 		return (ENOSPC);
20173526Sxy150489 	}
20183526Sxy150489 
20193526Sxy150489 	/*
20203526Sxy150489 	 * Primary/default address is in slot 0. The next addresses
20213526Sxy150489 	 * are the multiple MAC addresses. So multiple MAC address 0
20223526Sxy150489 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
20233526Sxy150489 	 * MAC address resides in slot 1.
20243526Sxy150489 	 */
20253526Sxy150489 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
20263526Sxy150489 		if (Adapter->unicst_addr[slot].mac.set == 0) {
20273526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
20283526Sxy150489 			break;
20293526Sxy150489 		}
20303526Sxy150489 	}
20313526Sxy150489 
20323526Sxy150489 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
20333526Sxy150489 
20343526Sxy150489 	Adapter->unicst_avail--;
20353526Sxy150489 	rw_exit(&Adapter->chip_lock);
20363526Sxy150489 
20373526Sxy150489 	maddr->mma_slot = slot;
20383526Sxy150489 
20393526Sxy150489 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
20403526Sxy150489 		rw_enter(&Adapter->chip_lock, RW_WRITER);
20413526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
20423526Sxy150489 		Adapter->unicst_avail++;
20433526Sxy150489 		rw_exit(&Adapter->chip_lock);
20443526Sxy150489 	}
20453526Sxy150489 
20463526Sxy150489 	return (err);
20473526Sxy150489 }
20483526Sxy150489 
20493526Sxy150489 /*
20503526Sxy150489  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
20513526Sxy150489  * call to e1000g_m_unicst_add(). The slot number that was returned in
20523526Sxy150489  * e1000g_m_unicst_add() is passed in the call to remove the address.
20533526Sxy150489  * Returns 0 on success.
20543526Sxy150489  */
20553526Sxy150489 static int
20563526Sxy150489 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
20573526Sxy150489 {
20583526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
20593526Sxy150489 	int err;
20603526Sxy150489 
20613526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
20623526Sxy150489 		return (EINVAL);
20633526Sxy150489 
20643526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
20653526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
20663526Sxy150489 		Adapter->unicst_addr[slot].mac.set = 0;
20673526Sxy150489 		Adapter->unicst_avail++;
20683526Sxy150489 		rw_exit(&Adapter->chip_lock);
20693526Sxy150489 
20703526Sxy150489 		/* Copy the default address to the passed slot */
20713526Sxy150489 		if (err = e1000g_unicst_set(Adapter,
20723526Sxy150489 		    Adapter->unicst_addr[0].mac.addr, slot) != 0) {
20733526Sxy150489 			rw_enter(&Adapter->chip_lock, RW_WRITER);
20743526Sxy150489 			Adapter->unicst_addr[slot].mac.set = 1;
20753526Sxy150489 			Adapter->unicst_avail--;
20763526Sxy150489 			rw_exit(&Adapter->chip_lock);
20773526Sxy150489 		}
20783526Sxy150489 		return (err);
20793526Sxy150489 	}
20803526Sxy150489 	rw_exit(&Adapter->chip_lock);
20813526Sxy150489 
20823526Sxy150489 	return (EINVAL);
20833526Sxy150489 }
20843526Sxy150489 
20853526Sxy150489 /*
20863526Sxy150489  * e1000g_m_unicst_modify() - modifies the value of an address that
20873526Sxy150489  * has been added by e1000g_m_unicst_add(). The new address, address
20883526Sxy150489  * length and the slot number that was returned in the call to add
20893526Sxy150489  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
20903526Sxy150489  * set to 0. Returns 0 on success.
20913526Sxy150489  */
20923526Sxy150489 static int
20933526Sxy150489 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
20943526Sxy150489 {
20953526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
20963526Sxy150489 	mac_addr_slot_t slot;
20973526Sxy150489 
20983526Sxy150489 	if (mac_unicst_verify(Adapter->mh,
20993526Sxy150489 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
21003526Sxy150489 		return (EINVAL);
21013526Sxy150489 
21023526Sxy150489 	slot = maddr->mma_slot;
21033526Sxy150489 
21043526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
21053526Sxy150489 		return (EINVAL);
21063526Sxy150489 
21073526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21083526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
21093526Sxy150489 		rw_exit(&Adapter->chip_lock);
21103526Sxy150489 
21113526Sxy150489 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
21123526Sxy150489 	}
21133526Sxy150489 	rw_exit(&Adapter->chip_lock);
21143526Sxy150489 
21153526Sxy150489 	return (EINVAL);
21163526Sxy150489 }
21173526Sxy150489 
21183526Sxy150489 /*
21193526Sxy150489  * e1000g_m_unicst_get() - will get the MAC address and all other
21203526Sxy150489  * information related to the address slot passed in mac_multi_addr_t.
21213526Sxy150489  * mma_flags should be set to 0 in the call.
21223526Sxy150489  * On return, mma_flags can take the following values:
21233526Sxy150489  * 1) MMAC_SLOT_UNUSED
21243526Sxy150489  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
21253526Sxy150489  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
21263526Sxy150489  * 4) MMAC_SLOT_USED
21273526Sxy150489  */
21283526Sxy150489 static int
21293526Sxy150489 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
21303526Sxy150489 {
21313526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
21323526Sxy150489 	mac_addr_slot_t slot;
21333526Sxy150489 
21343526Sxy150489 	slot = maddr->mma_slot;
21353526Sxy150489 
21363526Sxy150489 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
21373526Sxy150489 		return (EINVAL);
21383526Sxy150489 
21393526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21403526Sxy150489 	if (Adapter->unicst_addr[slot].mac.set == 1) {
21413526Sxy150489 		bcopy(Adapter->unicst_addr[slot].mac.addr,
21423526Sxy150489 		    maddr->mma_addr, ETHERADDRL);
21433526Sxy150489 		maddr->mma_flags = MMAC_SLOT_USED;
21443526Sxy150489 	} else {
21453526Sxy150489 		maddr->mma_flags = MMAC_SLOT_UNUSED;
21463526Sxy150489 	}
21473526Sxy150489 	rw_exit(&Adapter->chip_lock);
21483526Sxy150489 
21493526Sxy150489 	return (0);
21503526Sxy150489 }
21513526Sxy150489 
21523526Sxy150489 static int
21533526Sxy150489 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
21543526Sxy150489 {
21553526Sxy150489 	unsigned i;
21563526Sxy150489 	int res = 0;
21573526Sxy150489 
21583526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21593526Sxy150489 
21603526Sxy150489 	if ((multiaddr[0] & 01) == 0) {
21613526Sxy150489 		res = EINVAL;
21623526Sxy150489 		goto done;
21633526Sxy150489 	}
21643526Sxy150489 
21653526Sxy150489 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
21663526Sxy150489 		res = ENOENT;
21673526Sxy150489 		goto done;
21683526Sxy150489 	}
21693526Sxy150489 
21703526Sxy150489 	bcopy(multiaddr,
21713526Sxy150489 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
21723526Sxy150489 	Adapter->mcast_count++;
21733526Sxy150489 
21743526Sxy150489 	/*
21753526Sxy150489 	 * Update the MC table in the hardware
21763526Sxy150489 	 */
21773526Sxy150489 	e1000g_DisableInterrupt(Adapter);
21783526Sxy150489 
21793526Sxy150489 	SetupMulticastTable(Adapter);
21803526Sxy150489 
21813526Sxy150489 	if (Adapter->Shared.mac_type == e1000_82542_rev2_0)
21823526Sxy150489 		SetupReceiveStructures(Adapter);
21833526Sxy150489 
21843526Sxy150489 	e1000g_EnableInterrupt(Adapter);
21853526Sxy150489 
21863526Sxy150489 done:
21873526Sxy150489 	rw_exit(&Adapter->chip_lock);
21883526Sxy150489 	return (res);
21893526Sxy150489 }
21903526Sxy150489 
21913526Sxy150489 static int
21923526Sxy150489 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
21933526Sxy150489 {
21943526Sxy150489 	unsigned i;
21953526Sxy150489 
21963526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
21973526Sxy150489 
21983526Sxy150489 	for (i = 0; i < Adapter->mcast_count; i++) {
21993526Sxy150489 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
22003526Sxy150489 		    ETHERADDRL) == 0) {
22013526Sxy150489 			for (i++; i < Adapter->mcast_count; i++) {
22023526Sxy150489 				Adapter->mcast_table[i - 1] =
22033526Sxy150489 				    Adapter->mcast_table[i];
22043526Sxy150489 			}
22053526Sxy150489 			Adapter->mcast_count--;
22063526Sxy150489 			break;
22073526Sxy150489 		}
22083526Sxy150489 	}
22093526Sxy150489 
22103526Sxy150489 	/*
22113526Sxy150489 	 * Update the MC table in the hardware
22123526Sxy150489 	 */
22133526Sxy150489 	e1000g_DisableInterrupt(Adapter);
22143526Sxy150489 
22153526Sxy150489 	SetupMulticastTable(Adapter);
22163526Sxy150489 
22173526Sxy150489 	if (Adapter->Shared.mac_type == e1000_82542_rev2_0)
22183526Sxy150489 		SetupReceiveStructures(Adapter);
22193526Sxy150489 
22203526Sxy150489 	e1000g_EnableInterrupt(Adapter);
22213526Sxy150489 
22223526Sxy150489 done:
22233526Sxy150489 	rw_exit(&Adapter->chip_lock);
22243526Sxy150489 	return (0);
22253526Sxy150489 }
22263526Sxy150489 
22273526Sxy150489 int
22283526Sxy150489 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
22293526Sxy150489 {
22303526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22313526Sxy150489 
22323526Sxy150489 	return ((add) ? multicst_add(Adapter, addr)
22334349Sxy150489 	    : multicst_remove(Adapter, addr));
22343526Sxy150489 }
22353526Sxy150489 
22363526Sxy150489 int
22373526Sxy150489 e1000g_m_promisc(void *arg, boolean_t on)
22383526Sxy150489 {
22393526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22403526Sxy150489 	ULONG RctlRegValue;
22413526Sxy150489 
22423526Sxy150489 	rw_enter(&Adapter->chip_lock, RW_WRITER);
22433526Sxy150489 
22443526Sxy150489 	RctlRegValue = E1000_READ_REG(&Adapter->Shared, RCTL);
22453526Sxy150489 
22463526Sxy150489 	if (on)
22473526Sxy150489 		RctlRegValue |=
22483526Sxy150489 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
22493526Sxy150489 	else
22503526Sxy150489 		RctlRegValue &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
22513526Sxy150489 
22523526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, RCTL, RctlRegValue);
22533526Sxy150489 
22543526Sxy150489 	Adapter->e1000g_promisc = on;
22553526Sxy150489 
22563526Sxy150489 	rw_exit(&Adapter->chip_lock);
22573526Sxy150489 
22583526Sxy150489 	return (0);
22593526Sxy150489 }
22603526Sxy150489 
22613526Sxy150489 static boolean_t
22623526Sxy150489 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
22633526Sxy150489 {
22643526Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
22653526Sxy150489 
22663526Sxy150489 	switch (cap) {
22673526Sxy150489 	case MAC_CAPAB_HCKSUM: {
22683526Sxy150489 		uint32_t *txflags = cap_data;
22693526Sxy150489 
22703526Sxy150489 		/*
22713526Sxy150489 		 * In Jumbo mode, enabling hardware checksum will cause
22723526Sxy150489 		 * port hang.
22733526Sxy150489 		 */
22743526Sxy150489 		if (Adapter->Shared.max_frame_size > ETHERMAX)
22753526Sxy150489 			return (B_FALSE);
22763526Sxy150489 
22773526Sxy150489 		/*
22783526Sxy150489 		 * Checksum on/off selection via global parameters.
22793526Sxy150489 		 *
22803526Sxy150489 		 * If the chip is flagged as not capable of (correctly)
22813526Sxy150489 		 * handling FULL checksumming, we don't enable it on either
22823526Sxy150489 		 * Rx or Tx side.  Otherwise, we take this chip's settings
22833526Sxy150489 		 * from the patchable global defaults.
22843526Sxy150489 		 *
22853526Sxy150489 		 * We advertise our capabilities only if TX offload is
22863526Sxy150489 		 * enabled.  On receive, the stack will accept checksummed
22873526Sxy150489 		 * packets anyway, even if we haven't said we can deliver
22883526Sxy150489 		 * them.
22893526Sxy150489 		 */
22903526Sxy150489 		switch (Adapter->Shared.mac_type) {
22913526Sxy150489 		/*
22923526Sxy150489 		 * Switch on hardware checksum offload of
22933526Sxy150489 		 * chip 82540, 82545, 82546
22943526Sxy150489 		 */
22953526Sxy150489 		case e1000_82540:
22963526Sxy150489 		case e1000_82544:	/* pci8086,1008 */
22973526Sxy150489 		case e1000_82545:
22983526Sxy150489 		case e1000_82545_rev_3:	/* pci8086,1026 */
22993526Sxy150489 		case e1000_82571:
23003526Sxy150489 		case e1000_82572:
23013526Sxy150489 		case e1000_82573:
23023526Sxy150489 		case e1000_80003es2lan:
23033526Sxy150489 			*txflags = HCKSUM_IPHDRCKSUM | HCKSUM_INET_PARTIAL;
23043526Sxy150489 			break;
23053526Sxy150489 
23063526Sxy150489 		case e1000_82546:	/* 82546EB. devID: 1010, 101d */
23073526Sxy150489 		case e1000_82546_rev_3:	/* 82546GB. devID: 1079, 107a */
23083526Sxy150489 #if !defined(__sparc) && !defined(__amd64)
23093526Sxy150489 			/* Workaround for Galaxy on 32bit */
23103526Sxy150489 			return (B_FALSE);
23113526Sxy150489 #else
23123526Sxy150489 			*txflags = HCKSUM_IPHDRCKSUM | HCKSUM_INET_PARTIAL;
23133526Sxy150489 			break;
23143526Sxy150489 #endif
23153526Sxy150489 
23163526Sxy150489 		/*
23173526Sxy150489 		 * We don't have the following PRO 1000 chip types at
23183526Sxy150489 		 * hand and haven't tested their hardware checksum
23193526Sxy150489 		 * offload capability.  We had better switch them off.
23203526Sxy150489 		 *	e1000_undefined = 0,
23213526Sxy150489 		 *	e1000_82542_rev2_0,
23223526Sxy150489 		 *	e1000_82542_rev2_1,
23233526Sxy150489 		 *	e1000_82543,
23243526Sxy150489 		 *	e1000_82541,
23253526Sxy150489 		 *	e1000_82541_rev_2,
23263526Sxy150489 		 *	e1000_82547,
23273526Sxy150489 		 *	e1000_82547_rev_2,
23283526Sxy150489 		 *	e1000_num_macs
23293526Sxy150489 		 */
23303526Sxy150489 		default:
23313526Sxy150489 			return (B_FALSE);
23323526Sxy150489 		}
23333526Sxy150489 
23343526Sxy150489 		break;
23353526Sxy150489 	}
23363526Sxy150489 	case MAC_CAPAB_POLL:
23373526Sxy150489 		/*
23383526Sxy150489 		 * There's nothing for us to fill in, simply returning
23393526Sxy150489 		 * B_TRUE stating that we support polling is sufficient.
23403526Sxy150489 		 */
23413526Sxy150489 		break;
23423526Sxy150489 
23433526Sxy150489 	case MAC_CAPAB_MULTIADDRESS: {
23443526Sxy150489 		multiaddress_capab_t *mmacp = cap_data;
23453526Sxy150489 
23463526Sxy150489 		/*
23473526Sxy150489 		 * The number of MAC addresses made available by
23483526Sxy150489 		 * this capability is one less than the total as
23493526Sxy150489 		 * the primary address in slot 0 is counted in
23503526Sxy150489 		 * the total.
23513526Sxy150489 		 */
23523526Sxy150489 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
23533526Sxy150489 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
23543526Sxy150489 		/* No multiple factory addresses, set mma_flag to 0 */
23553526Sxy150489 		mmacp->maddr_flag = 0;
23563526Sxy150489 		mmacp->maddr_handle = Adapter;
23573526Sxy150489 		mmacp->maddr_add = e1000g_m_unicst_add;
23583526Sxy150489 		mmacp->maddr_remove = e1000g_m_unicst_remove;
23593526Sxy150489 		mmacp->maddr_modify = e1000g_m_unicst_modify;
23603526Sxy150489 		mmacp->maddr_get = e1000g_m_unicst_get;
23613526Sxy150489 		mmacp->maddr_reserve = NULL;
23623526Sxy150489 		break;
23633526Sxy150489 	}
23643526Sxy150489 	default:
23653526Sxy150489 		return (B_FALSE);
23663526Sxy150489 	}
23673526Sxy150489 	return (B_TRUE);
23683526Sxy150489 }
23693526Sxy150489 
23703526Sxy150489 /*
23713526Sxy150489  * **********************************************************************
23723526Sxy150489  * Name:	 e1000g_getparam					*
23733526Sxy150489  *									*
23743526Sxy150489  * Description: This routine gets user-configured values out of the	*
23753526Sxy150489  *	      configuration file e1000g.conf.				*
23763526Sxy150489  * For each configurable value, there is a minimum, a maximum, and a	*
23773526Sxy150489  * default.								*
23783526Sxy150489  * If user does not configure a value, use the default.			*
23793526Sxy150489  * If user configures below the minimum, use the minumum.		*
23803526Sxy150489  * If user configures above the maximum, use the maxumum.		*
23813526Sxy150489  *									*
23823526Sxy150489  * Arguments:								*
23833526Sxy150489  *      Adapter - A pointer to our adapter structure			*
23843526Sxy150489  *									*
23853526Sxy150489  * Returns:     None							*
23863526Sxy150489  * **********************************************************************
23873526Sxy150489  */
23883526Sxy150489 static void
23893526Sxy150489 e1000g_getparam(struct e1000g *Adapter)
23903526Sxy150489 {
23913526Sxy150489 	/*
23923526Sxy150489 	 * get each configurable property from e1000g.conf
23933526Sxy150489 	 */
23943526Sxy150489 
23953526Sxy150489 	/*
23963526Sxy150489 	 * NumTxDescriptors
23973526Sxy150489 	 */
23983526Sxy150489 	Adapter->NumTxDescriptors =
23993526Sxy150489 	    e1000g_getprop(Adapter, "NumTxDescriptors",
24004349Sxy150489 	    MINNUMTXDESCRIPTOR, MAXNUMTXDESCRIPTOR,
24014349Sxy150489 	    DEFAULTNUMTXDESCRIPTOR);
24023526Sxy150489 
24033526Sxy150489 	/*
24043526Sxy150489 	 * NumRxDescriptors
24053526Sxy150489 	 */
24063526Sxy150489 	Adapter->NumRxDescriptors =
24073526Sxy150489 	    e1000g_getprop(Adapter, "NumRxDescriptors",
24084349Sxy150489 	    MINNUMRXDESCRIPTOR, MAXNUMRXDESCRIPTOR,
24094349Sxy150489 	    DEFAULTNUMRXDESCRIPTOR);
24103526Sxy150489 
24113526Sxy150489 	/*
24123526Sxy150489 	 * NumRxFreeList
24133526Sxy150489 	 */
24143526Sxy150489 	Adapter->NumRxFreeList =
24153526Sxy150489 	    e1000g_getprop(Adapter, "NumRxFreeList",
24164349Sxy150489 	    MINNUMRXFREELIST, MAXNUMRXFREELIST,
24174349Sxy150489 	    DEFAULTNUMRXFREELIST);
24183526Sxy150489 
24193526Sxy150489 	/*
24203526Sxy150489 	 * NumTxPacketList
24213526Sxy150489 	 */
24223526Sxy150489 	Adapter->NumTxSwPacket =
24233526Sxy150489 	    e1000g_getprop(Adapter, "NumTxPacketList",
24244349Sxy150489 	    MINNUMTXSWPACKET, MAXNUMTXSWPACKET,
24254349Sxy150489 	    DEFAULTNUMTXSWPACKET);
24263526Sxy150489 
24273526Sxy150489 	/*
24283526Sxy150489 	 * FlowControl
24293526Sxy150489 	 */
24303526Sxy150489 	Adapter->Shared.fc_send_xon = B_TRUE;
24313526Sxy150489 	Adapter->Shared.fc =
24323526Sxy150489 	    e1000g_getprop(Adapter, "FlowControl",
24333526Sxy150489 	    E1000_FC_NONE, 4, DEFAULTFLOWCONTROLVAL);
24343526Sxy150489 	/* 4 is the setting that says "let the eeprom decide" */
24353526Sxy150489 	if (Adapter->Shared.fc == 4)
24363526Sxy150489 		Adapter->Shared.fc = E1000_FC_DEFAULT;
24373526Sxy150489 
24383526Sxy150489 	/*
24393526Sxy150489 	 * MaxNumReceivePackets
24403526Sxy150489 	 */
24413526Sxy150489 	Adapter->MaxNumReceivePackets =
24423526Sxy150489 	    e1000g_getprop(Adapter, "MaxNumReceivePackets",
24433526Sxy150489 	    MINNUMRCVPKTONINTR, MAXNUMRCVPKTONINTR,
24443526Sxy150489 	    DEFAULTMAXNUMRCVPKTONINTR);
24453526Sxy150489 
24463526Sxy150489 	/*
24473526Sxy150489 	 * TxInterruptDelay
24483526Sxy150489 	 */
24493526Sxy150489 	Adapter->TxInterruptDelay =
24503526Sxy150489 	    e1000g_getprop(Adapter, "TxInterruptDelay",
24513526Sxy150489 	    MINTXINTERRUPTDELAYVAL, MAXTXINTERRUPTDELAYVAL,
24523526Sxy150489 	    DEFAULTTXINTERRUPTDELAYVAL);
24533526Sxy150489 
24543526Sxy150489 	/*
24553526Sxy150489 	 * PHY master slave setting
24563526Sxy150489 	 */
24573526Sxy150489 	Adapter->Shared.master_slave =
24583526Sxy150489 	    e1000g_getprop(Adapter, "SetMasterSlave",
24593526Sxy150489 	    e1000_ms_hw_default, e1000_ms_auto,
24603526Sxy150489 	    e1000_ms_hw_default);
24613526Sxy150489 
24623526Sxy150489 	/*
24633526Sxy150489 	 * Parameter which controls TBI mode workaround, which is only
24643526Sxy150489 	 * needed on certain switches such as Cisco 6500/Foundry
24653526Sxy150489 	 */
24663526Sxy150489 	Adapter->Shared.tbi_compatibility_en =
24673526Sxy150489 	    e1000g_getprop(Adapter, "TbiCompatibilityEnable",
24683526Sxy150489 	    0, 1, DEFAULTTBICOMPATIBILITYENABLE);
24693526Sxy150489 
24703526Sxy150489 	/*
24713526Sxy150489 	 * MSI Enable
24723526Sxy150489 	 */
24733526Sxy150489 	Adapter->msi_enabled =
24743526Sxy150489 	    e1000g_getprop(Adapter, "MSIEnable",
24753526Sxy150489 	    0, 1, DEFAULTMSIENABLE);
24763526Sxy150489 
24773526Sxy150489 	/*
24783526Sxy150489 	 * Interrupt Throttling Rate
24793526Sxy150489 	 */
24803526Sxy150489 	Adapter->intr_throttling_rate =
24813526Sxy150489 	    e1000g_getprop(Adapter, "intr_throttling_rate",
24823526Sxy150489 	    MININTERRUPTTHROTTLINGVAL, MAXINTERRUPTTHROTTLINGVAL,
24833526Sxy150489 	    DEFAULTINTERRUPTTHROTTLINGVAL);
24843526Sxy150489 
24853526Sxy150489 	/*
24863526Sxy150489 	 * Adaptive Interrupt Blanking Enable/Disable
24873526Sxy150489 	 * It is enabled by default
24883526Sxy150489 	 */
24893526Sxy150489 	Adapter->intr_adaptive =
24903526Sxy150489 	    (e1000g_getprop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
24913526Sxy150489 	    B_TRUE : B_FALSE;
24923526Sxy150489 }
24933526Sxy150489 
24943526Sxy150489 /*
24953526Sxy150489  * **********************************************************************
24963526Sxy150489  * Name:	 e1000g_getprop						*
24973526Sxy150489  *									*
24983526Sxy150489  * Description: get a user-configure property value out of the		*
24993526Sxy150489  *   configuration file e1000g.conf.					*
25003526Sxy150489  *   Caller provides name of the property, a default value, a		*
25013526Sxy150489  *   minimum value, and a maximum value.				*
25023526Sxy150489  *									*
25033526Sxy150489  * Returns: configured value of the property, with default, minimum and	*
25043526Sxy150489  *   maximum properly applied.						*
25053526Sxy150489  * **********************************************************************
25063526Sxy150489  */
25073526Sxy150489 static int
25083526Sxy150489 e1000g_getprop(struct e1000g *Adapter,	/* point to per-adapter structure */
25093526Sxy150489     char *propname,		/* name of the property */
25103526Sxy150489     int minval,			/* minimum acceptable value */
25113526Sxy150489     int maxval,			/* maximim acceptable value */
25123526Sxy150489     int defval)			/* default value */
25133526Sxy150489 {
25143526Sxy150489 	int propval;		/* value returned for requested property */
25153526Sxy150489 	int *props;		/* point to array of properties returned */
25163526Sxy150489 	uint_t nprops;		/* number of property value returned */
25173526Sxy150489 
25183526Sxy150489 	/*
25193526Sxy150489 	 * get the array of properties from the config file
25203526Sxy150489 	 */
25213526Sxy150489 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
25223526Sxy150489 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
25233526Sxy150489 		/* got some properties, test if we got enough */
25243526Sxy150489 		if (Adapter->AdapterInstance < nprops) {
25253526Sxy150489 			propval = props[Adapter->AdapterInstance];
25263526Sxy150489 		} else {
25273526Sxy150489 			/* not enough properties configured */
25283526Sxy150489 			propval = defval;
25293526Sxy150489 			e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
25303526Sxy150489 			    "Not Enough %s values found in e1000g.conf"
25313526Sxy150489 			    " - set to %d\n",
25323526Sxy150489 			    propname, propval);
25333526Sxy150489 		}
25343526Sxy150489 
25353526Sxy150489 		/* free memory allocated for properties */
25363526Sxy150489 		ddi_prop_free(props);
25373526Sxy150489 
25383526Sxy150489 	} else {
25393526Sxy150489 		propval = defval;
25403526Sxy150489 	}
25413526Sxy150489 
25423526Sxy150489 	/*
25433526Sxy150489 	 * enforce limits
25443526Sxy150489 	 */
25453526Sxy150489 	if (propval > maxval) {
25463526Sxy150489 		propval = maxval;
25473526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
25483526Sxy150489 		    "Too High %s value in e1000g.conf - set to %d\n",
25493526Sxy150489 		    propname, propval);
25503526Sxy150489 	}
25513526Sxy150489 
25523526Sxy150489 	if (propval < minval) {
25533526Sxy150489 		propval = minval;
25543526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
25553526Sxy150489 		    "Too Low %s value in e1000g.conf - set to %d\n",
25563526Sxy150489 		    propname, propval);
25573526Sxy150489 	}
25583526Sxy150489 
25593526Sxy150489 	return (propval);
25603526Sxy150489 }
25613526Sxy150489 
25623526Sxy150489 static boolean_t
25634061Sxy150489 e1000g_link_check(struct e1000g *Adapter)
25643526Sxy150489 {
25654061Sxy150489 	uint16_t speed, duplex, phydata;
25664061Sxy150489 	boolean_t link_changed = B_FALSE;
25673526Sxy150489 	struct e1000_hw *hw;
25683526Sxy150489 	uint32_t reg_tarc;
25693526Sxy150489 
25703526Sxy150489 	hw = &Adapter->Shared;
25713526Sxy150489 
25723526Sxy150489 	if (e1000g_link_up(Adapter)) {
25733526Sxy150489 		/*
25743526Sxy150489 		 * The Link is up, check whether it was marked as down earlier
25753526Sxy150489 		 */
25764061Sxy150489 		if (Adapter->link_state != LINK_STATE_UP) {
25774061Sxy150489 			e1000_get_speed_and_duplex(hw, &speed, &duplex);
25784061Sxy150489 			Adapter->link_speed = speed;
25794061Sxy150489 			Adapter->link_duplex = duplex;
25804061Sxy150489 			Adapter->link_state = LINK_STATE_UP;
25814061Sxy150489 			link_changed = B_TRUE;
25824061Sxy150489 
25834061Sxy150489 			Adapter->tx_link_down_timeout = 0;
25844061Sxy150489 
25854061Sxy150489 			if ((hw->mac_type == e1000_82571) ||
25864061Sxy150489 			    (hw->mac_type == e1000_82572)) {
25874061Sxy150489 				reg_tarc = E1000_READ_REG(hw, TARC0);
25884061Sxy150489 				if (speed == SPEED_1000)
25894061Sxy150489 					reg_tarc |= (1 << 21);
25904061Sxy150489 				else
25914061Sxy150489 					reg_tarc &= ~(1 << 21);
25924061Sxy150489 				E1000_WRITE_REG(hw, TARC0, reg_tarc);
25933526Sxy150489 			}
25943526Sxy150489 		}
25953526Sxy150489 		Adapter->smartspeed = 0;
25963526Sxy150489 	} else {
25974061Sxy150489 		if (Adapter->link_state != LINK_STATE_DOWN) {
25983526Sxy150489 			Adapter->link_speed = 0;
25993526Sxy150489 			Adapter->link_duplex = 0;
26004061Sxy150489 			Adapter->link_state = LINK_STATE_DOWN;
26014061Sxy150489 			link_changed = B_TRUE;
26024061Sxy150489 
26033526Sxy150489 			/*
26043526Sxy150489 			 * SmartSpeed workaround for Tabor/TanaX, When the
26053526Sxy150489 			 * driver loses link disable auto master/slave
26063526Sxy150489 			 * resolution.
26073526Sxy150489 			 */
26083526Sxy150489 			if (hw->phy_type == e1000_phy_igp) {
26093526Sxy150489 				e1000_read_phy_reg(hw,
26103526Sxy150489 				    PHY_1000T_CTRL, &phydata);
26113526Sxy150489 				phydata |= CR_1000T_MS_ENABLE;
26123526Sxy150489 				e1000_write_phy_reg(hw,
26133526Sxy150489 				    PHY_1000T_CTRL, phydata);
26143526Sxy150489 			}
26153526Sxy150489 		} else {
26163526Sxy150489 			e1000g_smartspeed(Adapter);
26173526Sxy150489 		}
26184061Sxy150489 
26194061Sxy150489 		if (Adapter->started) {
26204061Sxy150489 			if (Adapter->tx_link_down_timeout <
26214061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
26224061Sxy150489 				Adapter->tx_link_down_timeout++;
26234061Sxy150489 			} else if (Adapter->tx_link_down_timeout ==
26244061Sxy150489 			    MAX_TX_LINK_DOWN_TIMEOUT) {
26254061Sxy150489 				rw_enter(&Adapter->chip_lock, RW_WRITER);
26264061Sxy150489 				e1000g_tx_drop(Adapter);
26274061Sxy150489 				rw_exit(&Adapter->chip_lock);
26284061Sxy150489 				Adapter->tx_link_down_timeout++;
26294061Sxy150489 			}
26304061Sxy150489 		}
26313526Sxy150489 	}
26323526Sxy150489 
26334061Sxy150489 	return (link_changed);
26344061Sxy150489 }
26354061Sxy150489 
26364061Sxy150489 static void
26374061Sxy150489 e1000g_LocalTimer(void *ws)
26384061Sxy150489 {
26394061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)ws;
26404061Sxy150489 	struct e1000_hw *hw;
26414061Sxy150489 	e1000g_ether_addr_t ether_addr;
26424061Sxy150489 	boolean_t link_changed;
26434061Sxy150489 
26444061Sxy150489 	hw = &Adapter->Shared;
26454061Sxy150489 
26464061Sxy150489 	(void) e1000g_tx_freemsg((caddr_t)Adapter, NULL);
26474061Sxy150489 
26484061Sxy150489 	if (e1000g_stall_check(Adapter)) {
26494061Sxy150489 		e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
26504061Sxy150489 		    "Tx stall detected. Activate automatic recovery.\n");
26514061Sxy150489 		Adapter->StallWatchdog = 0;
26524061Sxy150489 		Adapter->tx_recycle_fail = 0;
26534061Sxy150489 		Adapter->reset_count++;
26544061Sxy150489 		(void) e1000g_reset(Adapter);
26554061Sxy150489 	}
26564061Sxy150489 
26574061Sxy150489 	link_changed = B_FALSE;
26584061Sxy150489 	mutex_enter(&Adapter->e1000g_linklock);
26594061Sxy150489 	if (Adapter->link_complete)
26604061Sxy150489 		link_changed = e1000g_link_check(Adapter);
26614061Sxy150489 	mutex_exit(&Adapter->e1000g_linklock);
26624061Sxy150489 
26634139Sxy150489 	if (link_changed) {
26644139Sxy150489 		/*
26654139Sxy150489 		 * Workaround for esb2. Data stuck in fifo on a link
26664139Sxy150489 		 * down event. Reset the adapter to recover it.
26674139Sxy150489 		 */
26684139Sxy150489 		if ((Adapter->link_state == LINK_STATE_DOWN) &&
26694139Sxy150489 		    (hw->mac_type == e1000_80003es2lan))
26704139Sxy150489 			(void) e1000g_reset(Adapter);
26714139Sxy150489 
26724061Sxy150489 		mac_link_update(Adapter->mh, Adapter->link_state);
26734139Sxy150489 	}
26744061Sxy150489 
26753526Sxy150489 	/*
26763526Sxy150489 	 * With 82571 controllers, any locally administered address will
26773526Sxy150489 	 * be overwritten when there is a reset on the other port.
26783526Sxy150489 	 * Detect this circumstance and correct it.
26793526Sxy150489 	 */
26803526Sxy150489 	if ((hw->mac_type == e1000_82571) && hw->laa_is_present) {
26813526Sxy150489 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, RA, 0);
26823526Sxy150489 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, RA, 1);
26833526Sxy150489 
26843526Sxy150489 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
26853526Sxy150489 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
26863526Sxy150489 
26873526Sxy150489 		if ((ether_addr.mac.addr[5] != hw->mac_addr[0]) ||
26883526Sxy150489 		    (ether_addr.mac.addr[4] != hw->mac_addr[1]) ||
26893526Sxy150489 		    (ether_addr.mac.addr[3] != hw->mac_addr[2]) ||
26903526Sxy150489 		    (ether_addr.mac.addr[2] != hw->mac_addr[3]) ||
26913526Sxy150489 		    (ether_addr.mac.addr[1] != hw->mac_addr[4]) ||
26923526Sxy150489 		    (ether_addr.mac.addr[0] != hw->mac_addr[5])) {
26933526Sxy150489 			e1000_rar_set(hw, hw->mac_addr, 0);
26943526Sxy150489 		}
26953526Sxy150489 	}
26963526Sxy150489 
26973526Sxy150489 	/*
26983526Sxy150489 	 * RP: ttl_workaround : DCR 49
26993526Sxy150489 	 */
27003526Sxy150489 	e1000_igp_ttl_workaround(hw);
27013526Sxy150489 
27023526Sxy150489 	/*
27033526Sxy150489 	 * Check for Adaptive IFS settings If there are lots of collisions
27043526Sxy150489 	 * change the value in steps...
27053526Sxy150489 	 * These properties should only be set for 10/100
27063526Sxy150489 	 */
27073526Sxy150489 	if ((hw->media_type == e1000_media_type_copper) &&
27084061Sxy150489 	    ((Adapter->link_speed == SPEED_100) ||
27094061Sxy150489 	    (Adapter->link_speed == SPEED_10))) {
27103526Sxy150489 		e1000_update_adaptive(hw);
27113526Sxy150489 	}
27123526Sxy150489 	/*
27133526Sxy150489 	 * Set Timer Interrupts
27143526Sxy150489 	 */
27153526Sxy150489 	E1000_WRITE_REG(hw, ICS, E1000_IMS_RXT0);
27163526Sxy150489 
27174061Sxy150489 	restart_timeout(Adapter);
27183526Sxy150489 }
27193526Sxy150489 
27204061Sxy150489 /*
27214061Sxy150489  * The function e1000g_link_timer() is called when the timer for link setup
27224061Sxy150489  * is expired, which indicates the completion of the link setup. The link
27234061Sxy150489  * state will not be updated until the link setup is completed. And the
27244061Sxy150489  * link state will not be sent to the upper layer through mac_link_update()
27254061Sxy150489  * in this function. It will be updated in the local timer routine or the
27264061Sxy150489  * interrupt service routine after the interface is started (plumbed).
27274061Sxy150489  */
27283526Sxy150489 static void
27294061Sxy150489 e1000g_link_timer(void *arg)
27303526Sxy150489 {
27314061Sxy150489 	struct e1000g *Adapter = (struct e1000g *)arg;
27323526Sxy150489 
27333526Sxy150489 	mutex_enter(&Adapter->e1000g_linklock);
27344061Sxy150489 	Adapter->link_complete = B_TRUE;
27354061Sxy150489 	Adapter->link_tid = 0;
27363526Sxy150489 	mutex_exit(&Adapter->e1000g_linklock);
27373526Sxy150489 }
27383526Sxy150489 
27393526Sxy150489 /*
27403526Sxy150489  * **********************************************************************
27413526Sxy150489  * Name:      e1000g_force_speed_duplex					*
27423526Sxy150489  *									*
27433526Sxy150489  * Description:								*
27443526Sxy150489  *   This function forces speed and duplex for 10/100 Mbps speeds	*
27453526Sxy150489  *   and also for 1000 Mbps speeds, it advertises half or full duplex	*
27463526Sxy150489  *									*
27473526Sxy150489  * Parameter Passed:							*
27483526Sxy150489  *   struct e1000g* (information of adpater)				*
27493526Sxy150489  *									*
27503526Sxy150489  * Return Value:							*
27513526Sxy150489  *									*
27523526Sxy150489  * Functions called:							*
27533526Sxy150489  * **********************************************************************
27543526Sxy150489  */
27553526Sxy150489 static void
27563526Sxy150489 e1000g_force_speed_duplex(struct e1000g *Adapter)
27573526Sxy150489 {
27583526Sxy150489 	int forced;
27593526Sxy150489 
27603526Sxy150489 	/*
27613526Sxy150489 	 * get value out of config file
27623526Sxy150489 	 */
27633526Sxy150489 	forced = e1000g_getprop(Adapter, "ForceSpeedDuplex",
27643526Sxy150489 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
27653526Sxy150489 
27663526Sxy150489 	switch (forced) {
27673526Sxy150489 	case GDIAG_10_HALF:
27683526Sxy150489 		/*
27693526Sxy150489 		 * Disable Auto Negotiation
27703526Sxy150489 		 */
27713526Sxy150489 		Adapter->Shared.autoneg = B_FALSE;
27723526Sxy150489 		Adapter->Shared.forced_speed_duplex = e1000_10_half;
27733526Sxy150489 		break;
27743526Sxy150489 	case GDIAG_10_FULL:
27753526Sxy150489 		/*
27763526Sxy150489 		 * Disable Auto Negotiation
27773526Sxy150489 		 */
27783526Sxy150489 		Adapter->Shared.autoneg = B_FALSE;
27793526Sxy150489 		Adapter->Shared.forced_speed_duplex = e1000_10_full;
27803526Sxy150489 		break;
27813526Sxy150489 	case GDIAG_100_HALF:
27823526Sxy150489 		/*
27833526Sxy150489 		 * Disable Auto Negotiation
27843526Sxy150489 		 */
27853526Sxy150489 		Adapter->Shared.autoneg = B_FALSE;
27863526Sxy150489 		Adapter->Shared.forced_speed_duplex = e1000_100_half;
27873526Sxy150489 		break;
27883526Sxy150489 	case GDIAG_100_FULL:
27893526Sxy150489 		/*
27903526Sxy150489 		 * Disable Auto Negotiation
27913526Sxy150489 		 */
27923526Sxy150489 		Adapter->Shared.autoneg = B_FALSE;
27933526Sxy150489 		Adapter->Shared.forced_speed_duplex = e1000_100_full;
27943526Sxy150489 		break;
27953526Sxy150489 	case GDIAG_1000_FULL:
27963526Sxy150489 		/*
27973526Sxy150489 		 * The gigabit spec requires autonegotiation.  Therefore,
27983526Sxy150489 		 * when the user wants to force the speed to 1000Mbps, we
27993526Sxy150489 		 * enable AutoNeg, but only allow the harware to advertise
28003526Sxy150489 		 * 1000Mbps.  This is different from 10/100 operation, where
28013526Sxy150489 		 * we are allowed to link without any negotiation.
28023526Sxy150489 		 */
28033526Sxy150489 		Adapter->Shared.autoneg = B_TRUE;
28043526Sxy150489 		Adapter->Shared.autoneg_advertised = ADVERTISE_1000_FULL;
28053526Sxy150489 		break;
28063526Sxy150489 	default:	/* obey the setting of AutoNegAdvertised */
28073526Sxy150489 		Adapter->Shared.autoneg = B_TRUE;
28083526Sxy150489 		Adapter->Shared.autoneg_advertised =
28093526Sxy150489 		    (uint16_t)e1000g_getprop(Adapter, "AutoNegAdvertised",
28104349Sxy150489 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
28114349Sxy150489 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
28123526Sxy150489 		break;
28133526Sxy150489 	}	/* switch */
28143526Sxy150489 }
28153526Sxy150489 
28163526Sxy150489 /*
28173526Sxy150489  * **********************************************************************
28183526Sxy150489  * Name:      e1000g_get_max_frame_size					*
28193526Sxy150489  *									*
28203526Sxy150489  * Description:								*
28213526Sxy150489  *   This function reads MaxFrameSize from e1000g.conf and sets it for	*
28223526Sxy150489  *   adapter.								*
28233526Sxy150489  *									*
28243526Sxy150489  * Parameter Passed:							*
28253526Sxy150489  *   struct e1000g* (information of adpater)				*
28263526Sxy150489  *									*
28273526Sxy150489  * Return Value:							*
28283526Sxy150489  *									*
28293526Sxy150489  * Functions called:							*
28303526Sxy150489  * **********************************************************************
28313526Sxy150489  */
28323526Sxy150489 static void
28333526Sxy150489 e1000g_get_max_frame_size(struct e1000g *Adapter)
28343526Sxy150489 {
28353526Sxy150489 	int max_frame;
28363526Sxy150489 
28373526Sxy150489 	/*
28383526Sxy150489 	 * get value out of config file
28393526Sxy150489 	 */
28403526Sxy150489 	max_frame = e1000g_getprop(Adapter, "MaxFrameSize", 0, 3, 0);
28413526Sxy150489 
28423526Sxy150489 	switch (max_frame) {
28433526Sxy150489 	case 0:
28443526Sxy150489 		Adapter->Shared.max_frame_size = ETHERMAX;
28453526Sxy150489 		break;
28463526Sxy150489 	case 1:
28473526Sxy150489 		Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_4K;
28483526Sxy150489 		break;
28493526Sxy150489 	case 2:
28503526Sxy150489 		Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_8K;
28513526Sxy150489 		break;
28523526Sxy150489 	case 3:
28533526Sxy150489 		if (Adapter->Shared.mac_type < e1000_82571)
28543526Sxy150489 			Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_16K;
28553526Sxy150489 		else
28563526Sxy150489 			Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_10K;
28573526Sxy150489 		break;
28583526Sxy150489 	default:
28593526Sxy150489 		Adapter->Shared.max_frame_size = ETHERMAX;
28603526Sxy150489 		break;
28613526Sxy150489 	}	/* switch */
28623526Sxy150489 
28633526Sxy150489 	/* ich8 does not do jumbo frames */
28643526Sxy150489 	if (Adapter->Shared.mac_type == e1000_ich8lan) {
28653526Sxy150489 		Adapter->Shared.max_frame_size = ETHERMAX;
28663526Sxy150489 	}
28673526Sxy150489 }
28683526Sxy150489 
28693526Sxy150489 static void
28703526Sxy150489 arm_timer(struct e1000g *Adapter)
28713526Sxy150489 {
28723526Sxy150489 	Adapter->WatchDogTimer_id =
28733526Sxy150489 	    timeout(e1000g_LocalTimer,
28743526Sxy150489 	    (void *)Adapter, 1 * drv_usectohz(1000000));
28753526Sxy150489 }
28763526Sxy150489 
28773526Sxy150489 static void
28783526Sxy150489 enable_timeout(struct e1000g *Adapter)
28793526Sxy150489 {
28803526Sxy150489 	mutex_enter(&Adapter->e1000g_timeout_lock);
28813526Sxy150489 
28823526Sxy150489 	if (!Adapter->timeout_enabled) {
28833526Sxy150489 		Adapter->timeout_enabled = B_TRUE;
28843526Sxy150489 		Adapter->timeout_started = B_TRUE;
28853526Sxy150489 
28863526Sxy150489 		arm_timer(Adapter);
28873526Sxy150489 	}
28883526Sxy150489 
28893526Sxy150489 	mutex_exit(&Adapter->e1000g_timeout_lock);
28903526Sxy150489 }
28913526Sxy150489 
28923526Sxy150489 static void
28933526Sxy150489 disable_timeout(struct e1000g *Adapter)
28943526Sxy150489 {
28953526Sxy150489 	timeout_id_t tid;
28963526Sxy150489 
28973526Sxy150489 	mutex_enter(&Adapter->e1000g_timeout_lock);
28983526Sxy150489 
28993526Sxy150489 	Adapter->timeout_enabled = B_FALSE;
29003526Sxy150489 	Adapter->timeout_started = B_FALSE;
29013526Sxy150489 
29023526Sxy150489 	tid = Adapter->WatchDogTimer_id;
29033526Sxy150489 	Adapter->WatchDogTimer_id = 0;
29043526Sxy150489 
29053526Sxy150489 	mutex_exit(&Adapter->e1000g_timeout_lock);
29063526Sxy150489 
29073526Sxy150489 	if (tid != 0)
29083526Sxy150489 		(void) untimeout(tid);
29093526Sxy150489 }
29103526Sxy150489 
29113526Sxy150489 static void
29123526Sxy150489 start_timeout(struct e1000g *Adapter)
29133526Sxy150489 {
29143526Sxy150489 	mutex_enter(&Adapter->e1000g_timeout_lock);
29153526Sxy150489 
29163526Sxy150489 	if (Adapter->timeout_enabled) {
29173526Sxy150489 		if (!Adapter->timeout_started) {
29183526Sxy150489 			Adapter->timeout_started = B_TRUE;
29193526Sxy150489 			arm_timer(Adapter);
29203526Sxy150489 		}
29213526Sxy150489 	}
29223526Sxy150489 
29233526Sxy150489 	mutex_exit(&Adapter->e1000g_timeout_lock);
29243526Sxy150489 }
29253526Sxy150489 
29263526Sxy150489 static void
29273526Sxy150489 restart_timeout(struct e1000g *Adapter)
29283526Sxy150489 {
29293526Sxy150489 	mutex_enter(&Adapter->e1000g_timeout_lock);
29303526Sxy150489 
29313526Sxy150489 	if (Adapter->timeout_started)
29323526Sxy150489 		arm_timer(Adapter);
29333526Sxy150489 
29343526Sxy150489 	mutex_exit(&Adapter->e1000g_timeout_lock);
29353526Sxy150489 }
29363526Sxy150489 
29373526Sxy150489 static void
29383526Sxy150489 stop_timeout(struct e1000g *Adapter)
29393526Sxy150489 {
29403526Sxy150489 	timeout_id_t tid;
29413526Sxy150489 
29423526Sxy150489 	mutex_enter(&Adapter->e1000g_timeout_lock);
29433526Sxy150489 
29443526Sxy150489 	Adapter->timeout_started = B_FALSE;
29453526Sxy150489 
29463526Sxy150489 	tid = Adapter->WatchDogTimer_id;
29473526Sxy150489 	Adapter->WatchDogTimer_id = 0;
29483526Sxy150489 
29493526Sxy150489 	mutex_exit(&Adapter->e1000g_timeout_lock);
29503526Sxy150489 
29513526Sxy150489 	if (tid != 0)
29523526Sxy150489 		(void) untimeout(tid);
29533526Sxy150489 }
29543526Sxy150489 
29553526Sxy150489 void
29563526Sxy150489 e1000g_DisableInterrupt(struct e1000g *Adapter)
29573526Sxy150489 {
29583526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, IMC,
29593526Sxy150489 	    0xffffffff & ~E1000_IMC_RXSEQ);
29603526Sxy150489 }
29613526Sxy150489 
29623526Sxy150489 void
29633526Sxy150489 e1000g_EnableInterrupt(struct e1000g *Adapter)
29643526Sxy150489 {
29653526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, IMS,
29663526Sxy150489 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW & ~E1000_IMS_TXQE);
29673526Sxy150489 }
29683526Sxy150489 
29693526Sxy150489 void
29703526Sxy150489 e1000g_DisableAllInterrupts(struct e1000g *Adapter)
29713526Sxy150489 {
29723526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, IMC, 0xffffffff)
29733526Sxy150489 }
29743526Sxy150489 
29753526Sxy150489 void
29763526Sxy150489 e1000g_EnableTxInterrupt(struct e1000g *Adapter)
29773526Sxy150489 {
29783526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, IMS, E1000G_IMS_TX_INTR);
29793526Sxy150489 }
29803526Sxy150489 
29813526Sxy150489 void
29823526Sxy150489 e1000g_DisableTxInterrupt(struct e1000g *Adapter)
29833526Sxy150489 {
29843526Sxy150489 	E1000_WRITE_REG(&Adapter->Shared, IMC, E1000G_IMC_TX_INTR);
29853526Sxy150489 }
29863526Sxy150489 
29873526Sxy150489 void
29883526Sxy150489 e1000_pci_set_mwi(struct e1000_hw *hw)
29893526Sxy150489 {
29903526Sxy150489 	uint16_t val = hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE;
29913526Sxy150489 	e1000_write_pci_cfg(hw, PCI_COMMAND_REGISTER, &val);
29923526Sxy150489 }
29933526Sxy150489 
29943526Sxy150489 void
29953526Sxy150489 e1000_pci_clear_mwi(struct e1000_hw *hw)
29963526Sxy150489 {
29973526Sxy150489 	uint16_t val = hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE;
29983526Sxy150489 	e1000_write_pci_cfg(hw, PCI_COMMAND_REGISTER, &val);
29993526Sxy150489 }
30003526Sxy150489 
30013526Sxy150489 void
30023526Sxy150489 e1000_write_pci_cfg(struct e1000_hw *adapter,
30033526Sxy150489     uint32_t reg, uint16_t *value)
30043526Sxy150489 {
30053526Sxy150489 	pci_config_put16(((struct e1000g_osdep *)(adapter->back))->handle,
30063526Sxy150489 	    reg, *value);
30073526Sxy150489 }
30083526Sxy150489 
30093526Sxy150489 void
30103526Sxy150489 e1000_read_pci_cfg(struct e1000_hw *adapter,
30113526Sxy150489     uint32_t reg, uint16_t *value)
30123526Sxy150489 {
30133526Sxy150489 	*value =
30143526Sxy150489 	    pci_config_get16(((struct e1000g_osdep *)(adapter->back))->
30153526Sxy150489 	    handle, reg);
30163526Sxy150489 }
30173526Sxy150489 
30183526Sxy150489 #ifndef __sparc
30193526Sxy150489 void
30203526Sxy150489 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
30213526Sxy150489 {
30223526Sxy150489 	outl(port, value);
30233526Sxy150489 }
30243526Sxy150489 
30253526Sxy150489 uint32_t
30263526Sxy150489 e1000_io_read(struct e1000_hw *hw, unsigned long port)
30273526Sxy150489 {
30283526Sxy150489 	return (inl(port));
30293526Sxy150489 }
30303526Sxy150489 #endif
30313526Sxy150489 
30323526Sxy150489 static void
30333526Sxy150489 e1000g_smartspeed(struct e1000g *adapter)
30343526Sxy150489 {
30353526Sxy150489 	uint16_t phy_status;
30363526Sxy150489 	uint16_t phy_ctrl;
30373526Sxy150489 
30383526Sxy150489 	/*
30393526Sxy150489 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
30403526Sxy150489 	 * advertising 1000Full, we don't even use the workaround
30413526Sxy150489 	 */
30423526Sxy150489 	if ((adapter->Shared.phy_type != e1000_phy_igp) ||
30433526Sxy150489 	    !adapter->Shared.autoneg ||
30443526Sxy150489 	    !(adapter->Shared.autoneg_advertised & ADVERTISE_1000_FULL))
30453526Sxy150489 		return;
30463526Sxy150489 
30473526Sxy150489 	/*
30483526Sxy150489 	 * True if this is the first call of this function or after every
30493526Sxy150489 	 * 30 seconds of not having link
30503526Sxy150489 	 */
30513526Sxy150489 	if (adapter->smartspeed == 0) {
30523526Sxy150489 		/*
30533526Sxy150489 		 * If Master/Slave config fault is asserted twice, we
30543526Sxy150489 		 * assume back-to-back
30553526Sxy150489 		 */
30563526Sxy150489 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_STATUS,
30573526Sxy150489 		    &phy_status);
30583526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
30593526Sxy150489 			return;
30603526Sxy150489 
30613526Sxy150489 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_STATUS,
30623526Sxy150489 		    &phy_status);
30633526Sxy150489 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
30643526Sxy150489 			return;
30653526Sxy150489 		/*
30663526Sxy150489 		 * We're assuming back-2-back because our status register
30673526Sxy150489 		 * insists! there's a fault in the master/slave
30683526Sxy150489 		 * relationship that was "negotiated"
30693526Sxy150489 		 */
30703526Sxy150489 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
30713526Sxy150489 		    &phy_ctrl);
30723526Sxy150489 		/*
30733526Sxy150489 		 * Is the phy configured for manual configuration of
30743526Sxy150489 		 * master/slave?
30753526Sxy150489 		 */
30763526Sxy150489 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
30773526Sxy150489 			/*
30783526Sxy150489 			 * Yes.  Then disable manual configuration (enable
30793526Sxy150489 			 * auto configuration) of master/slave
30803526Sxy150489 			 */
30813526Sxy150489 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
30823526Sxy150489 			e1000_write_phy_reg(&adapter->Shared,
30833526Sxy150489 			    PHY_1000T_CTRL, phy_ctrl);
30843526Sxy150489 			/*
30853526Sxy150489 			 * Effectively starting the clock
30863526Sxy150489 			 */
30873526Sxy150489 			adapter->smartspeed++;
30883526Sxy150489 			/*
30893526Sxy150489 			 * Restart autonegotiation
30903526Sxy150489 			 */
30913526Sxy150489 			if (!e1000_phy_setup_autoneg(&adapter->Shared) &&
30923526Sxy150489 			    !e1000_read_phy_reg(&adapter->Shared, PHY_CTRL,
30934349Sxy150489 			    &phy_ctrl)) {
30943526Sxy150489 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
30953526Sxy150489 				    MII_CR_RESTART_AUTO_NEG);
30963526Sxy150489 				e1000_write_phy_reg(&adapter->Shared,
30973526Sxy150489 				    PHY_CTRL, phy_ctrl);
30983526Sxy150489 			}
30993526Sxy150489 		}
31003526Sxy150489 		return;
31013526Sxy150489 		/*
31023526Sxy150489 		 * Has 6 seconds transpired still without link? Remember,
31033526Sxy150489 		 * you should reset the smartspeed counter once you obtain
31043526Sxy150489 		 * link
31053526Sxy150489 		 */
31063526Sxy150489 	} else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
31073526Sxy150489 		/*
31083526Sxy150489 		 * Yes.  Remember, we did at the start determine that
31093526Sxy150489 		 * there's a master/slave configuration fault, so we're
31103526Sxy150489 		 * still assuming there's someone on the other end, but we
31113526Sxy150489 		 * just haven't yet been able to talk to it. We then
31123526Sxy150489 		 * re-enable auto configuration of master/slave to see if
31133526Sxy150489 		 * we're running 2/3 pair cables.
31143526Sxy150489 		 */
31153526Sxy150489 		/*
31163526Sxy150489 		 * If still no link, perhaps using 2/3 pair cable
31173526Sxy150489 		 */
31183526Sxy150489 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
31193526Sxy150489 		    &phy_ctrl);
31203526Sxy150489 		phy_ctrl |= CR_1000T_MS_ENABLE;
31213526Sxy150489 		e1000_write_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
31223526Sxy150489 		    phy_ctrl);
31233526Sxy150489 		/*
31243526Sxy150489 		 * Restart autoneg with phy enabled for manual
31253526Sxy150489 		 * configuration of master/slave
31263526Sxy150489 		 */
31273526Sxy150489 		if (!e1000_phy_setup_autoneg(&adapter->Shared) &&
31283526Sxy150489 		    !e1000_read_phy_reg(&adapter->Shared, PHY_CTRL,
31294349Sxy150489 		    &phy_ctrl)) {
31303526Sxy150489 			phy_ctrl |=
31313526Sxy150489 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
31323526Sxy150489 			e1000_write_phy_reg(&adapter->Shared, PHY_CTRL,
31333526Sxy150489 			    phy_ctrl);
31343526Sxy150489 		}
31353526Sxy150489 		/*
31363526Sxy150489 		 * Hopefully, there are no more faults and we've obtained
31373526Sxy150489 		 * link as a result.
31383526Sxy150489 		 */
31393526Sxy150489 	}
31403526Sxy150489 	/*
31413526Sxy150489 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
31423526Sxy150489 	 * seconds)
31433526Sxy150489 	 */
31443526Sxy150489 	if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
31453526Sxy150489 		adapter->smartspeed = 0;
31463526Sxy150489 }
31473526Sxy150489 
31483526Sxy150489 static boolean_t
31493526Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
31503526Sxy150489 {
31513526Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
31523526Sxy150489 	const uint8_t addr_test2[6] =
31533526Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
31543526Sxy150489 
31553526Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
31563526Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
31573526Sxy150489 		return (B_FALSE);
31583526Sxy150489 
31593526Sxy150489 	return (B_TRUE);
31603526Sxy150489 }
31613526Sxy150489 
31623526Sxy150489 /*
31633526Sxy150489  * **********************************************************************
31643526Sxy150489  * Name:								*
31653526Sxy150489  *	e1000g_stall_check						*
31663526Sxy150489  *									*
31673526Sxy150489  * Description:								*
31683526Sxy150489  *	This function checks if the adapter is stalled. (In transmit)	*
31693526Sxy150489  *									*
31703526Sxy150489  *	It is called each time the timeout is invoked.			*
31713526Sxy150489  *	If the transmit descriptor reclaim continuously fails,		*
31723526Sxy150489  *	the watchdog value will increment by 1. If the watchdog		*
31733526Sxy150489  *	value exceeds the threshold, the adapter is assumed to		*
31743526Sxy150489  *	have stalled and need to be reset.				*
31753526Sxy150489  *									*
31763526Sxy150489  * Arguments:								*
31773526Sxy150489  *	Adapter - A pointer to our context sensitive "Adapter"		*
31783526Sxy150489  *	structure.							*
31793526Sxy150489  *									*
31803526Sxy150489  * Returns:								*
31813526Sxy150489  *	B_TRUE - The dapter is assumed to have stalled.			*
31823526Sxy150489  *	B_FALSE								*
31833526Sxy150489  *									*
31843526Sxy150489  * **********************************************************************
31853526Sxy150489  */
31863526Sxy150489 static boolean_t
31873526Sxy150489 e1000g_stall_check(struct e1000g *Adapter)
31883526Sxy150489 {
31894061Sxy150489 	if (Adapter->link_state != LINK_STATE_UP)
31903526Sxy150489 		return (B_FALSE);
31913526Sxy150489 
31923526Sxy150489 	if (Adapter->tx_recycle_fail > 0)
31933526Sxy150489 		Adapter->StallWatchdog++;
31943526Sxy150489 	else
31953526Sxy150489 		Adapter->StallWatchdog = 0;
31963526Sxy150489 
31973526Sxy150489 	if (Adapter->StallWatchdog < E1000G_STALL_WATCHDOG_COUNT)
31983526Sxy150489 		return (B_FALSE);
31993526Sxy150489 
32003526Sxy150489 	return (B_TRUE);
32013526Sxy150489 }
32023526Sxy150489 
32033526Sxy150489 
32043526Sxy150489 static enum ioc_reply
32053526Sxy150489 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
32063526Sxy150489 {
32073526Sxy150489 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
32083526Sxy150489 	e1000g_peekpoke_t *ppd;
32093526Sxy150489 	uint64_t mem_va;
32103526Sxy150489 	uint64_t maxoff;
32113526Sxy150489 	boolean_t peek;
32123526Sxy150489 
32133526Sxy150489 	switch (iocp->ioc_cmd) {
32143526Sxy150489 
32153526Sxy150489 	case E1000G_IOC_REG_PEEK:
32163526Sxy150489 		peek = B_TRUE;
32173526Sxy150489 		break;
32183526Sxy150489 
32193526Sxy150489 	case E1000G_IOC_REG_POKE:
32203526Sxy150489 		peek = B_FALSE;
32213526Sxy150489 		break;
32223526Sxy150489 
32233526Sxy150489 	deault:
32243526Sxy150489 		e1000g_DEBUGLOG_1(e1000gp, e1000g_INFO_LEVEL,
32254349Sxy150489 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
32264349Sxy150489 		    iocp->ioc_cmd);
32273526Sxy150489 		return (IOC_INVAL);
32283526Sxy150489 	}
32293526Sxy150489 
32303526Sxy150489 	/*
32313526Sxy150489 	 * Validate format of ioctl
32323526Sxy150489 	 */
32333526Sxy150489 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
32343526Sxy150489 		return (IOC_INVAL);
32353526Sxy150489 	if (mp->b_cont == NULL)
32363526Sxy150489 		return (IOC_INVAL);
32373526Sxy150489 
32383526Sxy150489 	ppd = (e1000g_peekpoke_t *)mp->b_cont->b_rptr;
32393526Sxy150489 
32403526Sxy150489 	/*
32413526Sxy150489 	 * Validate request parameters
32423526Sxy150489 	 */
32433526Sxy150489 	switch (ppd->pp_acc_space) {
32443526Sxy150489 
32453526Sxy150489 	default:
32463526Sxy150489 		e1000g_DEBUGLOG_1(e1000gp, e1000g_INFO_LEVEL,
32474349Sxy150489 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
32484349Sxy150489 		    ppd->pp_acc_space);
32493526Sxy150489 		return (IOC_INVAL);
32503526Sxy150489 
32513526Sxy150489 	case E1000G_PP_SPACE_REG:
32523526Sxy150489 		/*
32533526Sxy150489 		 * Memory-mapped I/O space
32543526Sxy150489 		 */
32553526Sxy150489 		ASSERT(ppd->pp_acc_size == 4);
32563526Sxy150489 		if (ppd->pp_acc_size != 4)
32573526Sxy150489 			return (IOC_INVAL);
32583526Sxy150489 
32593526Sxy150489 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
32603526Sxy150489 			return (IOC_INVAL);
32613526Sxy150489 
32623526Sxy150489 		mem_va = 0;
32633526Sxy150489 		maxoff = 0x10000;
32643526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
32653526Sxy150489 		break;
32663526Sxy150489 
32673526Sxy150489 	case E1000G_PP_SPACE_E1000G:
32683526Sxy150489 		/*
32693526Sxy150489 		 * E1000g data structure!
32703526Sxy150489 		 */
32713526Sxy150489 		mem_va = (uintptr_t)e1000gp;
32723526Sxy150489 		maxoff = sizeof (struct e1000g);
32733526Sxy150489 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
32743526Sxy150489 		break;
32753526Sxy150489 
32763526Sxy150489 	}
32773526Sxy150489 
32783526Sxy150489 	if (ppd->pp_acc_offset >= maxoff)
32793526Sxy150489 		return (IOC_INVAL);
32803526Sxy150489 
32813526Sxy150489 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
32823526Sxy150489 		return (IOC_INVAL);
32833526Sxy150489 
32843526Sxy150489 	/*
32853526Sxy150489 	 * All OK - go!
32863526Sxy150489 	 */
32873526Sxy150489 	ppd->pp_acc_offset += mem_va;
32883526Sxy150489 	(*ppfn)(e1000gp, ppd);
32893526Sxy150489 	return (peek ? IOC_REPLY : IOC_ACK);
32903526Sxy150489 }
32913526Sxy150489 
32923526Sxy150489 static void
32933526Sxy150489 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
32943526Sxy150489 {
32953526Sxy150489 	ddi_acc_handle_t handle;
32963526Sxy150489 	uint32_t *regaddr;
32973526Sxy150489 
32983526Sxy150489 	handle =
32994349Sxy150489 	    ((struct e1000g_osdep *)(&e1000gp->Shared)->back)->E1000_handle;
33003526Sxy150489 	regaddr =
33013526Sxy150489 	    (uint32_t *)((&e1000gp->Shared)->hw_addr + ppd->pp_acc_offset);
33023526Sxy150489 
33033526Sxy150489 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
33043526Sxy150489 }
33053526Sxy150489 
33063526Sxy150489 static void
33073526Sxy150489 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
33083526Sxy150489 {
33093526Sxy150489 	ddi_acc_handle_t handle;
33103526Sxy150489 	uint32_t *regaddr;
33113526Sxy150489 	uint32_t value;
33123526Sxy150489 
33133526Sxy150489 	handle =
33144349Sxy150489 	    ((struct e1000g_osdep *)(&e1000gp->Shared)->back)->E1000_handle;
33153526Sxy150489 	regaddr =
33163526Sxy150489 	    (uint32_t *)((&e1000gp->Shared)->hw_addr + ppd->pp_acc_offset);
33173526Sxy150489 	value = (uint32_t)ppd->pp_acc_data;
33183526Sxy150489 
33193526Sxy150489 	ddi_put32(handle, regaddr, value);
33203526Sxy150489 }
33213526Sxy150489 
33223526Sxy150489 static void
33233526Sxy150489 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
33243526Sxy150489 {
33253526Sxy150489 	uint64_t value;
33263526Sxy150489 	void *vaddr;
33273526Sxy150489 
33283526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
33293526Sxy150489 
33303526Sxy150489 	switch (ppd->pp_acc_size) {
33313526Sxy150489 	case 1:
33323526Sxy150489 		value = *(uint8_t *)vaddr;
33333526Sxy150489 		break;
33343526Sxy150489 
33353526Sxy150489 	case 2:
33363526Sxy150489 		value = *(uint16_t *)vaddr;
33373526Sxy150489 		break;
33383526Sxy150489 
33393526Sxy150489 	case 4:
33403526Sxy150489 		value = *(uint32_t *)vaddr;
33413526Sxy150489 		break;
33423526Sxy150489 
33433526Sxy150489 	case 8:
33443526Sxy150489 		value = *(uint64_t *)vaddr;
33453526Sxy150489 		break;
33463526Sxy150489 	}
33473526Sxy150489 
33483526Sxy150489 	e1000g_DEBUGLOG_4(e1000gp, e1000g_INFO_LEVEL,
33494349Sxy150489 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
33504349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
33513526Sxy150489 
33523526Sxy150489 	ppd->pp_acc_data = value;
33533526Sxy150489 }
33543526Sxy150489 
33553526Sxy150489 static void
33563526Sxy150489 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
33573526Sxy150489 {
33583526Sxy150489 	uint64_t value;
33593526Sxy150489 	void *vaddr;
33603526Sxy150489 
33613526Sxy150489 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
33623526Sxy150489 	value = ppd->pp_acc_data;
33633526Sxy150489 
33643526Sxy150489 	e1000g_DEBUGLOG_4(e1000gp, e1000g_INFO_LEVEL,
33654349Sxy150489 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
33664349Sxy150489 	    (void *)e1000gp, (void *)ppd, value, vaddr);
33673526Sxy150489 
33683526Sxy150489 	switch (ppd->pp_acc_size) {
33693526Sxy150489 	case 1:
33703526Sxy150489 		*(uint8_t *)vaddr = (uint8_t)value;
33713526Sxy150489 		break;
33723526Sxy150489 
33733526Sxy150489 	case 2:
33743526Sxy150489 		*(uint16_t *)vaddr = (uint16_t)value;
33753526Sxy150489 		break;
33763526Sxy150489 
33773526Sxy150489 	case 4:
33783526Sxy150489 		*(uint32_t *)vaddr = (uint32_t)value;
33793526Sxy150489 		break;
33803526Sxy150489 
33813526Sxy150489 	case 8:
33823526Sxy150489 		*(uint64_t *)vaddr = (uint64_t)value;
33833526Sxy150489 		break;
33843526Sxy150489 	}
33853526Sxy150489 }
33863526Sxy150489 
33873526Sxy150489 /*
33883526Sxy150489  * Loopback Support
33893526Sxy150489  */
33903526Sxy150489 static lb_property_t lb_normal =
33913526Sxy150489 	{ normal,	"normal",	E1000G_LB_NONE		};
33923526Sxy150489 static lb_property_t lb_external1000 =
33933526Sxy150489 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
33943526Sxy150489 static lb_property_t lb_external100 =
33953526Sxy150489 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
33963526Sxy150489 static lb_property_t lb_external10 =
33973526Sxy150489 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
33983526Sxy150489 static lb_property_t lb_phy =
33993526Sxy150489 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
34003526Sxy150489 
34013526Sxy150489 static enum ioc_reply
34023526Sxy150489 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
34033526Sxy150489 {
34043526Sxy150489 	lb_info_sz_t *lbsp;
34053526Sxy150489 	lb_property_t *lbpp;
34063526Sxy150489 	struct e1000_hw *hw;
34073526Sxy150489 	uint32_t *lbmp;
34083526Sxy150489 	uint32_t size;
34093526Sxy150489 	uint32_t value;
34103526Sxy150489 	uint16_t phy_status;
34113526Sxy150489 	uint16_t phy_ext_status;
34123526Sxy150489 
34133526Sxy150489 	hw = &Adapter->Shared;
34143526Sxy150489 
34153526Sxy150489 	if (mp->b_cont == NULL)
34163526Sxy150489 		return (IOC_INVAL);
34173526Sxy150489 
34183526Sxy150489 	switch (iocp->ioc_cmd) {
34193526Sxy150489 	default:
34203526Sxy150489 		return (IOC_INVAL);
34213526Sxy150489 
34223526Sxy150489 	case LB_GET_INFO_SIZE:
34233526Sxy150489 		size = sizeof (lb_info_sz_t);
34243526Sxy150489 		if (iocp->ioc_count != size)
34253526Sxy150489 			return (IOC_INVAL);
34263526Sxy150489 
34273526Sxy150489 		e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
34283526Sxy150489 		e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
34293526Sxy150489 
34303526Sxy150489 		value = sizeof (lb_normal);
34313526Sxy150489 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
34323526Sxy150489 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
34333526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
34343526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
34353526Sxy150489 			value += sizeof (lb_phy);
34363526Sxy150489 			switch (hw->mac_type) {
34373526Sxy150489 			case e1000_82571:
34383526Sxy150489 			case e1000_82572:
34393526Sxy150489 				value += sizeof (lb_external1000);
34403526Sxy150489 				break;
34413526Sxy150489 			}
34423526Sxy150489 		}
34433526Sxy150489 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
34443526Sxy150489 		    (phy_status & MII_SR_100T2_FD_CAPS))
34453526Sxy150489 			value += sizeof (lb_external100);
34463526Sxy150489 		if (phy_status & MII_SR_10T_FD_CAPS)
34473526Sxy150489 			value += sizeof (lb_external10);
34483526Sxy150489 
34493526Sxy150489 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
34503526Sxy150489 		*lbsp = value;
34513526Sxy150489 		break;
34523526Sxy150489 
34533526Sxy150489 	case LB_GET_INFO:
34543526Sxy150489 		e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
34553526Sxy150489 		e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
34563526Sxy150489 
34573526Sxy150489 		value = sizeof (lb_normal);
34583526Sxy150489 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
34593526Sxy150489 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
34603526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
34613526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
34623526Sxy150489 			value += sizeof (lb_phy);
34633526Sxy150489 			switch (hw->mac_type) {
34643526Sxy150489 			case e1000_82571:
34653526Sxy150489 			case e1000_82572:
34663526Sxy150489 				value += sizeof (lb_external1000);
34673526Sxy150489 				break;
34683526Sxy150489 			}
34693526Sxy150489 		}
34703526Sxy150489 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
34713526Sxy150489 		    (phy_status & MII_SR_100T2_FD_CAPS))
34723526Sxy150489 			value += sizeof (lb_external100);
34733526Sxy150489 		if (phy_status & MII_SR_10T_FD_CAPS)
34743526Sxy150489 			value += sizeof (lb_external10);
34753526Sxy150489 
34763526Sxy150489 		size = value;
34773526Sxy150489 		if (iocp->ioc_count != size)
34783526Sxy150489 			return (IOC_INVAL);
34793526Sxy150489 
34803526Sxy150489 		value = 0;
34813526Sxy150489 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
34823526Sxy150489 		lbpp[value++] = lb_normal;
34833526Sxy150489 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
34843526Sxy150489 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
34853526Sxy150489 		    (hw->media_type == e1000_media_type_fiber) ||
34863526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
34873526Sxy150489 			lbpp[value++] = lb_phy;
34883526Sxy150489 			switch (hw->mac_type) {
34893526Sxy150489 			case e1000_82571:
34903526Sxy150489 			case e1000_82572:
34913526Sxy150489 				lbpp[value++] = lb_external1000;
34923526Sxy150489 				break;
34933526Sxy150489 			}
34943526Sxy150489 		}
34953526Sxy150489 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
34963526Sxy150489 		    (phy_status & MII_SR_100T2_FD_CAPS))
34973526Sxy150489 			lbpp[value++] = lb_external100;
34983526Sxy150489 		if (phy_status & MII_SR_10T_FD_CAPS)
34993526Sxy150489 			lbpp[value++] = lb_external10;
35003526Sxy150489 		break;
35013526Sxy150489 
35023526Sxy150489 	case LB_GET_MODE:
35033526Sxy150489 		size = sizeof (uint32_t);
35043526Sxy150489 		if (iocp->ioc_count != size)
35053526Sxy150489 			return (IOC_INVAL);
35063526Sxy150489 
35073526Sxy150489 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
35083526Sxy150489 		*lbmp = Adapter->loopback_mode;
35093526Sxy150489 		break;
35103526Sxy150489 
35113526Sxy150489 	case LB_SET_MODE:
35123526Sxy150489 		size = 0;
35133526Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
35143526Sxy150489 			return (IOC_INVAL);
35153526Sxy150489 
35163526Sxy150489 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
35173526Sxy150489 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
35183526Sxy150489 			return (IOC_INVAL);
35193526Sxy150489 		break;
35203526Sxy150489 	}
35213526Sxy150489 
35223526Sxy150489 	iocp->ioc_count = size;
35233526Sxy150489 	iocp->ioc_error = 0;
35243526Sxy150489 
35253526Sxy150489 	return (IOC_REPLY);
35263526Sxy150489 }
35273526Sxy150489 
35283526Sxy150489 static boolean_t
35293526Sxy150489 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
35303526Sxy150489 {
35313526Sxy150489 	struct e1000_hw *hw;
35323526Sxy150489 #ifndef __sparc
35333526Sxy150489 	uint32_t reg_rctl;
35343526Sxy150489 #endif
35353526Sxy150489 	int i, times;
35363526Sxy150489 
35373526Sxy150489 	if (mode == Adapter->loopback_mode)
35383526Sxy150489 		return (B_TRUE);
35393526Sxy150489 
35403526Sxy150489 	hw = &Adapter->Shared;
35413526Sxy150489 	times = 0;
35423526Sxy150489 
35433526Sxy150489 again:
35443526Sxy150489 	switch (mode) {
35453526Sxy150489 	default:
35463526Sxy150489 		return (B_FALSE);
35473526Sxy150489 
35483526Sxy150489 	case E1000G_LB_NONE:
35493526Sxy150489 		/* Get original speed and duplex settings */
35503526Sxy150489 		e1000g_force_speed_duplex(Adapter);
35513526Sxy150489 		/* Reset the chip */
35523526Sxy150489 		hw->wait_autoneg_complete = B_TRUE;
35533526Sxy150489 		(void) e1000g_reset(Adapter);
35543526Sxy150489 		hw->wait_autoneg_complete = B_FALSE;
35553526Sxy150489 		break;
35563526Sxy150489 
35573526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
35583526Sxy150489 		e1000g_set_external_loopback_1000(Adapter);
35593526Sxy150489 		break;
35603526Sxy150489 
35613526Sxy150489 	case E1000G_LB_EXTERNAL_100:
35623526Sxy150489 		e1000g_set_external_loopback_100(Adapter);
35633526Sxy150489 		break;
35643526Sxy150489 
35653526Sxy150489 	case E1000G_LB_EXTERNAL_10:
35663526Sxy150489 		e1000g_set_external_loopback_10(Adapter);
35673526Sxy150489 		break;
35683526Sxy150489 
35693526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
35703526Sxy150489 		e1000g_set_internal_loopback(Adapter);
35713526Sxy150489 		break;
35723526Sxy150489 	}
35733526Sxy150489 
35743526Sxy150489 	times++;
35753526Sxy150489 
35763526Sxy150489 	switch (mode) {
35773526Sxy150489 	case E1000G_LB_EXTERNAL_1000:
35783526Sxy150489 	case E1000G_LB_EXTERNAL_100:
35793526Sxy150489 	case E1000G_LB_EXTERNAL_10:
35803526Sxy150489 	case E1000G_LB_INTERNAL_PHY:
35813526Sxy150489 		/* Wait for link up */
35823526Sxy150489 		for (i = (PHY_FORCE_TIME * 2); i > 0; i--)
35833526Sxy150489 			msec_delay(100);
35843526Sxy150489 
35853526Sxy150489 		if (!e1000g_link_up(Adapter)) {
35863526Sxy150489 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
35873526Sxy150489 			    "Failed to get the link up");
35883526Sxy150489 			if (times < 2) {
35893526Sxy150489 				/* Reset the link */
35903526Sxy150489 				e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
35913526Sxy150489 				    "Reset the link ...");
35923526Sxy150489 				(void) e1000g_reset(Adapter);
35933526Sxy150489 				goto again;
35943526Sxy150489 			}
35953526Sxy150489 		}
35963526Sxy150489 		break;
35973526Sxy150489 	}
35983526Sxy150489 
35993526Sxy150489 	Adapter->loopback_mode = mode;
36003526Sxy150489 
36013526Sxy150489 	return (B_TRUE);
36023526Sxy150489 }
36033526Sxy150489 
36043526Sxy150489 /*
36053526Sxy150489  * The following loopback settings are from Intel's technical
36063526Sxy150489  * document - "How To Loopback". All the register settings and
36073526Sxy150489  * time delay values are directly inherited from the document
36083526Sxy150489  * without more explanations available.
36093526Sxy150489  */
36103526Sxy150489 static void
36113526Sxy150489 e1000g_set_internal_loopback(struct e1000g *Adapter)
36123526Sxy150489 {
36133526Sxy150489 	struct e1000_hw *hw;
36143526Sxy150489 	uint32_t ctrl;
36153526Sxy150489 	uint32_t status;
36163526Sxy150489 	uint16_t phy_ctrl;
36173526Sxy150489 
36183526Sxy150489 	hw = &Adapter->Shared;
36193526Sxy150489 
36203526Sxy150489 	/* Disable Smart Power Down */
36213526Sxy150489 	phy_spd_state(hw, B_FALSE);
36223526Sxy150489 
36233526Sxy150489 	e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl);
36243526Sxy150489 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
36253526Sxy150489 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
36263526Sxy150489 
36273526Sxy150489 	switch (hw->mac_type) {
36283526Sxy150489 	case e1000_82540:
36293526Sxy150489 	case e1000_82545:
36303526Sxy150489 	case e1000_82545_rev_3:
36313526Sxy150489 	case e1000_82546:
36323526Sxy150489 	case e1000_82546_rev_3:
36333526Sxy150489 	case e1000_82573:
36343526Sxy150489 		/* Auto-MDI/MDIX off */
36353526Sxy150489 		e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
36363526Sxy150489 		/* Reset PHY to update Auto-MDI/MDIX */
36373526Sxy150489 		e1000_write_phy_reg(hw, PHY_CTRL,
36384349Sxy150489 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
36393526Sxy150489 		/* Reset PHY to auto-neg off and force 1000 */
36403526Sxy150489 		e1000_write_phy_reg(hw, PHY_CTRL,
36414349Sxy150489 		    phy_ctrl | MII_CR_RESET);
36423526Sxy150489 		break;
36433526Sxy150489 	}
36443526Sxy150489 
36453526Sxy150489 	/* Set loopback */
36463526Sxy150489 	e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl | MII_CR_LOOPBACK);
36473526Sxy150489 
36483526Sxy150489 	msec_delay(250);
36493526Sxy150489 
36503526Sxy150489 	/* Now set up the MAC to the same speed/duplex as the PHY. */
36513526Sxy150489 	ctrl = E1000_READ_REG(hw, CTRL);
36523526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
36533526Sxy150489 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
36544349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
36554349Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
36564349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
36573526Sxy150489 
36583526Sxy150489 	switch (hw->mac_type) {
36593526Sxy150489 	case e1000_82540:
36603526Sxy150489 	case e1000_82545:
36613526Sxy150489 	case e1000_82545_rev_3:
36623526Sxy150489 	case e1000_82546:
36633526Sxy150489 	case e1000_82546_rev_3:
36643526Sxy150489 		/*
36653526Sxy150489 		 * For some serdes we'll need to commit the writes now
36663526Sxy150489 		 * so that the status is updated on link
36673526Sxy150489 		 */
36683526Sxy150489 		if (hw->media_type == e1000_media_type_internal_serdes) {
36693526Sxy150489 			E1000_WRITE_REG(hw, CTRL, ctrl);
36703526Sxy150489 			msec_delay(100);
36713526Sxy150489 			ctrl = E1000_READ_REG(hw, CTRL);
36723526Sxy150489 		}
36733526Sxy150489 
36743526Sxy150489 		if (hw->media_type == e1000_media_type_copper) {
36753526Sxy150489 			/* Invert Loss of Signal */
36763526Sxy150489 			ctrl |= E1000_CTRL_ILOS;
36773526Sxy150489 		} else {
36783526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
36793526Sxy150489 			status = E1000_READ_REG(hw, STATUS);
36803526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
36813526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
36823526Sxy150489 		}
36833526Sxy150489 		break;
36843526Sxy150489 
36853526Sxy150489 	case e1000_82571:
36863526Sxy150489 	case e1000_82572:
36873526Sxy150489 		if (hw->media_type != e1000_media_type_copper) {
36883526Sxy150489 			/* Set ILOS on fiber nic if half duplex is detected */
36893526Sxy150489 			status = E1000_READ_REG(hw, STATUS);
36903526Sxy150489 			if ((status & E1000_STATUS_FD) == 0)
36913526Sxy150489 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
36923526Sxy150489 		}
36933526Sxy150489 		break;
36943526Sxy150489 
36953526Sxy150489 	case e1000_82573:
36963526Sxy150489 		ctrl |= E1000_CTRL_ILOS;
36973526Sxy150489 		break;
36983526Sxy150489 	}
36993526Sxy150489 
37003526Sxy150489 	E1000_WRITE_REG(hw, CTRL, ctrl);
37013526Sxy150489 
37023526Sxy150489 	/*
37033526Sxy150489 	 * Disable PHY receiver for 82540/545/546 and 82573 Family.
37043526Sxy150489 	 * For background, see comments above e1000g_set_internal_loopback().
37053526Sxy150489 	 */
37063526Sxy150489 	switch (hw->mac_type) {
37073526Sxy150489 	case e1000_82540:
37083526Sxy150489 	case e1000_82545:
37093526Sxy150489 	case e1000_82545_rev_3:
37103526Sxy150489 	case e1000_82546:
37113526Sxy150489 	case e1000_82546_rev_3:
37123526Sxy150489 	case e1000_82573:
37133526Sxy150489 		e1000_write_phy_reg(hw, 29, 0x001F);
37143526Sxy150489 		e1000_write_phy_reg(hw, 30, 0x8FFC);
37153526Sxy150489 		e1000_write_phy_reg(hw, 29, 0x001A);
37163526Sxy150489 		e1000_write_phy_reg(hw, 30, 0x8FF0);
37173526Sxy150489 		break;
37183526Sxy150489 	}
37193526Sxy150489 }
37203526Sxy150489 
37213526Sxy150489 static void
37223526Sxy150489 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
37233526Sxy150489 {
37243526Sxy150489 	struct e1000_hw *hw;
37253526Sxy150489 	uint32_t rctl;
37263526Sxy150489 	uint32_t ctrl_ext;
37273526Sxy150489 	uint32_t ctrl;
37283526Sxy150489 	uint32_t status;
37293526Sxy150489 	uint32_t txcw;
37303526Sxy150489 
37313526Sxy150489 	hw = &Adapter->Shared;
37323526Sxy150489 
37333526Sxy150489 	/* Disable Smart Power Down */
37343526Sxy150489 	phy_spd_state(hw, B_FALSE);
37353526Sxy150489 
37363526Sxy150489 	switch (hw->media_type) {
37373526Sxy150489 	case e1000_media_type_copper:
37383526Sxy150489 		/* Force link up (Must be done before the PHY writes) */
37393526Sxy150489 		ctrl = E1000_READ_REG(hw, CTRL);
37403526Sxy150489 		ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
37413526Sxy150489 		E1000_WRITE_REG(hw, CTRL, ctrl);
37423526Sxy150489 
37433526Sxy150489 		rctl = E1000_READ_REG(hw, RCTL);
37443526Sxy150489 		rctl |= (E1000_RCTL_EN |
37454349Sxy150489 		    E1000_RCTL_SBP |
37464349Sxy150489 		    E1000_RCTL_UPE |
37474349Sxy150489 		    E1000_RCTL_MPE |
37484349Sxy150489 		    E1000_RCTL_LPE |
37494349Sxy150489 		    E1000_RCTL_BAM);		/* 0x803E */
37503526Sxy150489 		E1000_WRITE_REG(hw, RCTL, rctl);
37513526Sxy150489 
37523526Sxy150489 		ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
37533526Sxy150489 		ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
37544349Sxy150489 		    E1000_CTRL_EXT_SDP6_DATA |
37554349Sxy150489 		    E1000_CTRL_EXT_SDP7_DATA |
37564349Sxy150489 		    E1000_CTRL_EXT_SDP4_DIR |
37574349Sxy150489 		    E1000_CTRL_EXT_SDP6_DIR |
37584349Sxy150489 		    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
37593526Sxy150489 		E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
37603526Sxy150489 
37613526Sxy150489 		/*
37623526Sxy150489 		 * This sequence tunes the PHY's SDP and no customer
37633526Sxy150489 		 * settable values. For background, see comments above
37643526Sxy150489 		 * e1000g_set_internal_loopback().
37653526Sxy150489 		 */
37663526Sxy150489 		e1000_write_phy_reg(hw, 0x0, 0x140);
37673526Sxy150489 		msec_delay(10);
37683526Sxy150489 		e1000_write_phy_reg(hw, 0x9, 0x1A00);
37693526Sxy150489 		e1000_write_phy_reg(hw, 0x12, 0xC10);
37703526Sxy150489 		e1000_write_phy_reg(hw, 0x12, 0x1C10);
37713526Sxy150489 		e1000_write_phy_reg(hw, 0x1F37, 0x76);
37723526Sxy150489 		e1000_write_phy_reg(hw, 0x1F33, 0x1);
37733526Sxy150489 		e1000_write_phy_reg(hw, 0x1F33, 0x0);
37743526Sxy150489 
37753526Sxy150489 		e1000_write_phy_reg(hw, 0x1F35, 0x65);
37763526Sxy150489 		e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
37773526Sxy150489 		e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
37783526Sxy150489 		e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
37793526Sxy150489 		e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
37803526Sxy150489 
37813526Sxy150489 		msec_delay(50);
37823526Sxy150489 		break;
37833526Sxy150489 	case e1000_media_type_fiber:
37843526Sxy150489 	case e1000_media_type_internal_serdes:
37853526Sxy150489 		status = E1000_READ_REG(hw, STATUS);
37863526Sxy150489 		if (((status & E1000_STATUS_LU) == 0) ||
37873526Sxy150489 		    (hw->media_type == e1000_media_type_internal_serdes)) {
37883526Sxy150489 			ctrl = E1000_READ_REG(hw, CTRL);
37893526Sxy150489 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
37903526Sxy150489 			E1000_WRITE_REG(hw, CTRL, ctrl);
37913526Sxy150489 		}
37923526Sxy150489 
37933526Sxy150489 		/* Disable autoneg by setting bit 31 of TXCW to zero */
37943526Sxy150489 		txcw = E1000_READ_REG(hw, TXCW);
37953526Sxy150489 		txcw &= ~((uint32_t)1 << 31);
37963526Sxy150489 		E1000_WRITE_REG(hw, TXCW, txcw);
37973526Sxy150489 
37983526Sxy150489 		/*
37993526Sxy150489 		 * Write 0x410 to Serdes Control register
38003526Sxy150489 		 * to enable Serdes analog loopback
38013526Sxy150489 		 */
38023526Sxy150489 		E1000_WRITE_REG(hw, SCTL, 0x0410);
38033526Sxy150489 		msec_delay(10);
38043526Sxy150489 		break;
38053526Sxy150489 	default:
38063526Sxy150489 		break;
38073526Sxy150489 	}
38083526Sxy150489 }
38093526Sxy150489 
38103526Sxy150489 static void
38113526Sxy150489 e1000g_set_external_loopback_100(struct e1000g *Adapter)
38123526Sxy150489 {
38133526Sxy150489 	struct e1000_hw *hw;
38143526Sxy150489 	uint32_t ctrl;
38153526Sxy150489 	uint16_t phy_ctrl;
38163526Sxy150489 
38173526Sxy150489 	hw = &Adapter->Shared;
38183526Sxy150489 
38193526Sxy150489 	/* Disable Smart Power Down */
38203526Sxy150489 	phy_spd_state(hw, B_FALSE);
38213526Sxy150489 
38223526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
38234349Sxy150489 	    MII_CR_SPEED_100);
38243526Sxy150489 
38253526Sxy150489 	/* Force 100/FD, reset PHY */
38263526Sxy150489 	e1000_write_phy_reg(hw, PHY_CTRL,
38274349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
38283526Sxy150489 	msec_delay(10);
38293526Sxy150489 
38303526Sxy150489 	/* Force 100/FD */
38313526Sxy150489 	e1000_write_phy_reg(hw, PHY_CTRL,
38324349Sxy150489 	    phy_ctrl);			/* 0x2100 */
38333526Sxy150489 	msec_delay(10);
38343526Sxy150489 
38353526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
38363526Sxy150489 	ctrl = E1000_READ_REG(hw, CTRL);
38373526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
38383526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
38394349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
38404349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
38414349Sxy150489 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
38424349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
38433526Sxy150489 
38443526Sxy150489 	E1000_WRITE_REG(hw, CTRL, ctrl);
38453526Sxy150489 }
38463526Sxy150489 
38473526Sxy150489 static void
38483526Sxy150489 e1000g_set_external_loopback_10(struct e1000g *Adapter)
38493526Sxy150489 {
38503526Sxy150489 	struct e1000_hw *hw;
38513526Sxy150489 	uint32_t ctrl;
38523526Sxy150489 	uint16_t phy_ctrl;
38533526Sxy150489 
38543526Sxy150489 	hw = &Adapter->Shared;
38553526Sxy150489 
38563526Sxy150489 	/* Disable Smart Power Down */
38573526Sxy150489 	phy_spd_state(hw, B_FALSE);
38583526Sxy150489 
38593526Sxy150489 	phy_ctrl = (MII_CR_FULL_DUPLEX |
38604349Sxy150489 	    MII_CR_SPEED_10);
38613526Sxy150489 
38623526Sxy150489 	/* Force 10/FD, reset PHY */
38633526Sxy150489 	e1000_write_phy_reg(hw, PHY_CTRL,
38644349Sxy150489 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
38653526Sxy150489 	msec_delay(10);
38663526Sxy150489 
38673526Sxy150489 	/* Force 10/FD */
38683526Sxy150489 	e1000_write_phy_reg(hw, PHY_CTRL,
38694349Sxy150489 	    phy_ctrl);			/* 0x0100 */
38703526Sxy150489 	msec_delay(10);
38713526Sxy150489 
38723526Sxy150489 	/* Now setup the MAC to the same speed/duplex as the PHY. */
38733526Sxy150489 	ctrl = E1000_READ_REG(hw, CTRL);
38743526Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
38753526Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
38764349Sxy150489 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
38774349Sxy150489 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
38784349Sxy150489 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
38794349Sxy150489 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
38803526Sxy150489 
38813526Sxy150489 	E1000_WRITE_REG(hw, CTRL, ctrl);
38823526Sxy150489 }
38833526Sxy150489 
38843526Sxy150489 #ifdef __sparc
38853526Sxy150489 static boolean_t
38863526Sxy150489 e1000g_find_mac_address(struct e1000g *Adapter)
38873526Sxy150489 {
38883526Sxy150489 	uchar_t *bytes;
38893526Sxy150489 	struct ether_addr sysaddr;
38903526Sxy150489 	uint_t nelts;
38913526Sxy150489 	int err;
38923526Sxy150489 	boolean_t found = B_FALSE;
38933526Sxy150489 
38943526Sxy150489 	/*
38953526Sxy150489 	 * The "vendor's factory-set address" may already have
38963526Sxy150489 	 * been extracted from the chip, but if the property
38973526Sxy150489 	 * "local-mac-address" is set we use that instead.
38983526Sxy150489 	 *
38993526Sxy150489 	 * We check whether it looks like an array of 6
39003526Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
39013526Sxy150489 	 * make sense of it this way, we'll ignore it.
39023526Sxy150489 	 */
39033526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
39043526Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
39053526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
39063526Sxy150489 		if (nelts == ETHERADDRL) {
39073526Sxy150489 			while (nelts--)
39083526Sxy150489 				Adapter->Shared.mac_addr[nelts] = bytes[nelts];
39093526Sxy150489 			found = B_TRUE;
39103526Sxy150489 		}
39113526Sxy150489 		ddi_prop_free(bytes);
39123526Sxy150489 	}
39133526Sxy150489 
39143526Sxy150489 	/*
39153526Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
39163526Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
39173526Sxy150489 	 */
39183526Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
39193526Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
39203526Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
39213526Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
39223526Sxy150489 				bcopy(&sysaddr, Adapter->Shared.mac_addr,
39233526Sxy150489 				    ETHERADDRL);
39243526Sxy150489 				found = B_TRUE;
39253526Sxy150489 			}
39263526Sxy150489 		}
39273526Sxy150489 		ddi_prop_free(bytes);
39283526Sxy150489 	}
39293526Sxy150489 
39303526Sxy150489 	/*
39313526Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
39323526Sxy150489 	 * if we netbooted from this interface), we must use this instead
39333526Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
39343526Sxy150489 	 * get confused by the address changing as Solaris takes over!
39353526Sxy150489 	 */
39363526Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
39373526Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
39383526Sxy150489 	if (err == DDI_PROP_SUCCESS) {
39393526Sxy150489 		if (nelts == ETHERADDRL) {
39403526Sxy150489 			while (nelts--)
39413526Sxy150489 				Adapter->Shared.mac_addr[nelts] = bytes[nelts];
39423526Sxy150489 			found = B_TRUE;
39433526Sxy150489 		}
39443526Sxy150489 		ddi_prop_free(bytes);
39453526Sxy150489 	}
39463526Sxy150489 
39473526Sxy150489 	if (found) {
39483526Sxy150489 		bcopy(Adapter->Shared.mac_addr, Adapter->Shared.perm_mac_addr,
39493526Sxy150489 		    ETHERADDRL);
39503526Sxy150489 	}
39513526Sxy150489 
39523526Sxy150489 	return (found);
39533526Sxy150489 }
39543526Sxy150489 #endif
39553526Sxy150489 
39563526Sxy150489 static int
39573526Sxy150489 e1000g_add_intrs(struct e1000g *Adapter)
39583526Sxy150489 {
39593526Sxy150489 	dev_info_t *devinfo;
39603526Sxy150489 	int intr_types;
39613526Sxy150489 	int rc;
39623526Sxy150489 
39633526Sxy150489 	devinfo = Adapter->dip;
39643526Sxy150489 
39653526Sxy150489 	/* Get supported interrupt types */
39663526Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
39673526Sxy150489 
39683526Sxy150489 	if (rc != DDI_SUCCESS) {
39693526Sxy150489 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
39703526Sxy150489 		    "Get supported interrupt types failed: %d\n", rc);
39713526Sxy150489 		return (DDI_FAILURE);
39723526Sxy150489 	}
39733526Sxy150489 
39743526Sxy150489 	/*
39753526Sxy150489 	 * Based on Intel Technical Advisory document (TA-160), there are some
39763526Sxy150489 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
39773526Sxy150489 	 * that it supports MSI, but in fact has problems.
39783526Sxy150489 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
39793526Sxy150489 	 * PCI/PCI-X NICs.
39803526Sxy150489 	 */
39813526Sxy150489 	if (Adapter->Shared.mac_type < e1000_82571)
39823526Sxy150489 		Adapter->msi_enabled = B_FALSE;
39833526Sxy150489 
39843526Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enabled) {
39853526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
39863526Sxy150489 
39873526Sxy150489 		if (rc != DDI_SUCCESS) {
39883526Sxy150489 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
39893526Sxy150489 			    "Add MSI failed, trying Legacy interrupts\n");
39903526Sxy150489 		} else {
39913526Sxy150489 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
39923526Sxy150489 		}
39933526Sxy150489 	}
39943526Sxy150489 
39953526Sxy150489 	if ((Adapter->intr_type == 0) &&
39963526Sxy150489 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
39973526Sxy150489 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
39983526Sxy150489 
39993526Sxy150489 		if (rc != DDI_SUCCESS) {
40003526Sxy150489 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
40013526Sxy150489 			    "Add Legacy interrupts failed\n");
40023526Sxy150489 			return (DDI_FAILURE);
40033526Sxy150489 		}
40043526Sxy150489 
40053526Sxy150489 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
40063526Sxy150489 	}
40073526Sxy150489 
40083526Sxy150489 	if (Adapter->intr_type == 0) {
40093526Sxy150489 		e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
40103526Sxy150489 		    "No interrupts registered\n");
40113526Sxy150489 		return (DDI_FAILURE);
40123526Sxy150489 	}
40133526Sxy150489 
40143526Sxy150489 	return (DDI_SUCCESS);
40153526Sxy150489 }
40163526Sxy150489 
40173526Sxy150489 /*
40183526Sxy150489  * e1000g_intr_add() handles MSI/Legacy interrupts
40193526Sxy150489  */
40203526Sxy150489 static int
40213526Sxy150489 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
40223526Sxy150489 {
40233526Sxy150489 	dev_info_t *devinfo;
40243526Sxy150489 	int count, avail, actual;
40253526Sxy150489 	int x, y, rc, inum = 0;
40263526Sxy150489 	int flag;
40273526Sxy150489 	ddi_intr_handler_t *intr_handler;
40283526Sxy150489 
40293526Sxy150489 	devinfo = Adapter->dip;
40303526Sxy150489 
40313526Sxy150489 	/* get number of interrupts */
40323526Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
40333526Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
40343526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
40353526Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d\n",
40363526Sxy150489 		    rc, count);
40373526Sxy150489 		return (DDI_FAILURE);
40383526Sxy150489 	}
40393526Sxy150489 
40403526Sxy150489 	/* get number of available interrupts */
40413526Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
40423526Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
40433526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
40443526Sxy150489 		    "Get interrupt available number failed. "
40453526Sxy150489 		    "Return: %d, available: %d\n", rc, avail);
40463526Sxy150489 		return (DDI_FAILURE);
40473526Sxy150489 	}
40483526Sxy150489 
40493526Sxy150489 	if (avail < count) {
40503526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
40513526Sxy150489 		    "Interrupts count: %d, available: %d\n",
40523526Sxy150489 		    count, avail);
40533526Sxy150489 	}
40543526Sxy150489 
40553526Sxy150489 	/* Allocate an array of interrupt handles */
40563526Sxy150489 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
40573526Sxy150489 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
40583526Sxy150489 
40593526Sxy150489 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
40603526Sxy150489 	flag = DDI_INTR_ALLOC_NORMAL;
40613526Sxy150489 
40623526Sxy150489 	/* call ddi_intr_alloc() */
40633526Sxy150489 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
40643526Sxy150489 	    count, &actual, flag);
40653526Sxy150489 
40663526Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
40673526Sxy150489 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
40683526Sxy150489 		    "Allocate interrupts failed: %d\n", rc);
40693526Sxy150489 
40703526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
40713526Sxy150489 		return (DDI_FAILURE);
40723526Sxy150489 	}
40733526Sxy150489 
40743526Sxy150489 	if (actual < count) {
40753526Sxy150489 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
40763526Sxy150489 		    "Interrupts requested: %d, received: %d\n",
40773526Sxy150489 		    count, actual);
40783526Sxy150489 	}
40793526Sxy150489 
40803526Sxy150489 	Adapter->intr_cnt = actual;
40813526Sxy150489 
40823526Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
40833526Sxy150489 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
40843526Sxy150489 
40853526Sxy150489 	if (rc != DDI_SUCCESS) {
40863526Sxy150489 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
40873526Sxy150489 		    "Get interrupt priority failed: %d\n", rc);
40883526Sxy150489 
40893526Sxy150489 		/* Free already allocated intr */
40903526Sxy150489 		for (y = 0; y < actual; y++)
40913526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
40923526Sxy150489 
40933526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
40943526Sxy150489 		return (DDI_FAILURE);
40953526Sxy150489 	}
40963526Sxy150489 
40973526Sxy150489 	/*
40983526Sxy150489 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
40993526Sxy150489 	 * use the interrupt service routine e1000g_intr_pciexpress()
41003526Sxy150489 	 * to avoid interrupt stealing when sharing interrupt with other
41013526Sxy150489 	 * devices.
41023526Sxy150489 	 */
41033526Sxy150489 	if (Adapter->Shared.mac_type < e1000_82571)
41043526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
41053526Sxy150489 	else
41063526Sxy150489 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
41073526Sxy150489 
41083526Sxy150489 	/* Call ddi_intr_add_handler() */
41093526Sxy150489 	for (x = 0; x < actual; x++) {
41103526Sxy150489 		rc = ddi_intr_add_handler(Adapter->htable[x],
41113526Sxy150489 		    intr_handler, (caddr_t)Adapter, NULL);
41123526Sxy150489 
41133526Sxy150489 		if (rc != DDI_SUCCESS) {
41143526Sxy150489 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41153526Sxy150489 			    "Add interrupt handler failed: %d\n", rc);
41163526Sxy150489 
41173526Sxy150489 			/* Remove already added handler */
41183526Sxy150489 			for (y = 0; y < x; y++)
41193526Sxy150489 				(void) ddi_intr_remove_handler(
41203526Sxy150489 				    Adapter->htable[y]);
41213526Sxy150489 
41223526Sxy150489 			/* Free already allocated intr */
41233526Sxy150489 			for (y = 0; y < actual; y++)
41243526Sxy150489 				(void) ddi_intr_free(Adapter->htable[y]);
41253526Sxy150489 
41263526Sxy150489 			kmem_free(Adapter->htable, Adapter->intr_size);
41273526Sxy150489 			return (DDI_FAILURE);
41283526Sxy150489 		}
41293526Sxy150489 	}
41303526Sxy150489 
41313526Sxy150489 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
41323526Sxy150489 
41333526Sxy150489 	if (rc != DDI_SUCCESS) {
41343526Sxy150489 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41353526Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
41363526Sxy150489 
41373526Sxy150489 		/* Free already allocated intr */
41383526Sxy150489 		for (y = 0; y < actual; y++) {
41393526Sxy150489 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
41403526Sxy150489 			(void) ddi_intr_free(Adapter->htable[y]);
41413526Sxy150489 		}
41423526Sxy150489 
41433526Sxy150489 		kmem_free(Adapter->htable, Adapter->intr_size);
41443526Sxy150489 		return (DDI_FAILURE);
41453526Sxy150489 	}
41463526Sxy150489 
41473526Sxy150489 	return (DDI_SUCCESS);
41483526Sxy150489 }
41493526Sxy150489 
41503526Sxy150489 static int
41513526Sxy150489 e1000g_rem_intrs(struct e1000g *Adapter)
41523526Sxy150489 {
41533526Sxy150489 	int x;
41543526Sxy150489 	int rc;
41553526Sxy150489 
41563526Sxy150489 	for (x = 0; x < Adapter->intr_cnt; x++) {
41573526Sxy150489 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
41583526Sxy150489 		if (rc != DDI_SUCCESS) {
41593526Sxy150489 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41603526Sxy150489 			    "Remove intr handler failed: %d\n", rc);
41613526Sxy150489 			return (DDI_FAILURE);
41623526Sxy150489 		}
41633526Sxy150489 
41643526Sxy150489 		rc = ddi_intr_free(Adapter->htable[x]);
41653526Sxy150489 		if (rc != DDI_SUCCESS) {
41663526Sxy150489 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41673526Sxy150489 			    "Free intr failed: %d\n", rc);
41683526Sxy150489 			return (DDI_FAILURE);
41693526Sxy150489 		}
41703526Sxy150489 	}
41713526Sxy150489 
41723526Sxy150489 	kmem_free(Adapter->htable, Adapter->intr_size);
41733526Sxy150489 
41743526Sxy150489 	return (DDI_SUCCESS);
41753526Sxy150489 }
41763526Sxy150489 
41773526Sxy150489 static int
41783526Sxy150489 e1000g_enable_intrs(struct e1000g *Adapter)
41793526Sxy150489 {
41803526Sxy150489 	int x;
41813526Sxy150489 	int rc;
41823526Sxy150489 
41833526Sxy150489 	/* Enable interrupts */
41843526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
41853526Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
41863526Sxy150489 		rc = ddi_intr_block_enable(Adapter->htable,
41873526Sxy150489 		    Adapter->intr_cnt);
41883526Sxy150489 		if (rc != DDI_SUCCESS) {
41893526Sxy150489 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41903526Sxy150489 			    "Enable block intr failed: %d\n", rc);
41913526Sxy150489 			return (DDI_FAILURE);
41923526Sxy150489 		}
41933526Sxy150489 	} else {
41943526Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
41953526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
41963526Sxy150489 			rc = ddi_intr_enable(Adapter->htable[x]);
41973526Sxy150489 			if (rc != DDI_SUCCESS) {
41983526Sxy150489 				e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
41993526Sxy150489 				    "Enable intr failed: %d\n", rc);
42003526Sxy150489 				return (DDI_FAILURE);
42013526Sxy150489 			}
42023526Sxy150489 		}
42033526Sxy150489 	}
42043526Sxy150489 
42053526Sxy150489 	return (DDI_SUCCESS);
42063526Sxy150489 }
42073526Sxy150489 
42083526Sxy150489 static int
42093526Sxy150489 e1000g_disable_intrs(struct e1000g *Adapter)
42103526Sxy150489 {
42113526Sxy150489 	int x;
42123526Sxy150489 	int rc;
42133526Sxy150489 
42143526Sxy150489 	/* Disable all interrupts */
42153526Sxy150489 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
42163526Sxy150489 		rc = ddi_intr_block_disable(Adapter->htable,
42173526Sxy150489 		    Adapter->intr_cnt);
42183526Sxy150489 		if (rc != DDI_SUCCESS) {
42193526Sxy150489 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
42203526Sxy150489 			    "Disable block intr failed: %d\n", rc);
42213526Sxy150489 			return (DDI_FAILURE);
42223526Sxy150489 		}
42233526Sxy150489 	} else {
42243526Sxy150489 		for (x = 0; x < Adapter->intr_cnt; x++) {
42253526Sxy150489 			rc = ddi_intr_disable(Adapter->htable[x]);
42263526Sxy150489 			if (rc != DDI_SUCCESS) {
42273526Sxy150489 				e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
42283526Sxy150489 				    "Disable intr failed: %d\n", rc);
42293526Sxy150489 				return (DDI_FAILURE);
42303526Sxy150489 			}
42313526Sxy150489 		}
42323526Sxy150489 	}
42333526Sxy150489 
42343526Sxy150489 	return (DDI_SUCCESS);
42353526Sxy150489 }
42363526Sxy150489 
42373526Sxy150489 /*
42383526Sxy150489  * phy_spd_state - set smart-power-down (SPD) state
42393526Sxy150489  *
42403526Sxy150489  * This only acts on the 82541/47 family and the 82571/72 family.
42413526Sxy150489  * For any others, return without doing anything.
42423526Sxy150489  */
42433526Sxy150489 void
42443526Sxy150489 phy_spd_state(struct e1000_hw *hw, boolean_t enable)
42453526Sxy150489 {
42463526Sxy150489 	int32_t offset;		/* offset to register */
42473526Sxy150489 	uint16_t spd_bit;	/* bit to be set */
42483526Sxy150489 	uint16_t reg;		/* register contents */
42493526Sxy150489 
42503526Sxy150489 	switch (hw->mac_type) {
42513526Sxy150489 	case e1000_82541:
42523526Sxy150489 	case e1000_82547:
42533526Sxy150489 	case e1000_82541_rev_2:
42543526Sxy150489 	case e1000_82547_rev_2:
42553526Sxy150489 		offset = IGP01E1000_GMII_FIFO;
42563526Sxy150489 		spd_bit = IGP01E1000_GMII_SPD;
42573526Sxy150489 		break;
42583526Sxy150489 	case e1000_82571:
42593526Sxy150489 	case e1000_82572:
42603526Sxy150489 		offset = IGP02E1000_PHY_POWER_MGMT;
42613526Sxy150489 		spd_bit = IGP02E1000_PM_SPD;
42623526Sxy150489 		break;
42633526Sxy150489 	default:
42643526Sxy150489 		return;		/* no action */
42653526Sxy150489 	}
42663526Sxy150489 
42673526Sxy150489 	e1000_read_phy_reg(hw, offset, &reg);
42683526Sxy150489 
42693526Sxy150489 	if (enable)
42703526Sxy150489 		reg |= spd_bit;		/* enable: set the spd bit */
42713526Sxy150489 	else
42723526Sxy150489 		reg &= ~spd_bit;	/* disable: clear the spd bit */
42733526Sxy150489 
42743526Sxy150489 	e1000_write_phy_reg(hw, offset, reg);
42753526Sxy150489 }
42763526Sxy150489 
42773526Sxy150489 /*
42783526Sxy150489  * The real intent of this routine is to return the value from pci-e
42793526Sxy150489  * config space at offset reg into the capability space.
42803526Sxy150489  * ICH devices are "PCI Express"-ish.  They have a configuration space,
42813526Sxy150489  * but do not contain PCI Express Capability registers, so this returns
42823526Sxy150489  * the equivalent of "not supported"
42833526Sxy150489  */
42843526Sxy150489 int32_t
42853526Sxy150489 e1000_read_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
42863526Sxy150489 {
42873526Sxy150489 	*value = pci_config_get16(((struct e1000g_osdep *)hw->back)->handle,
42884349Sxy150489 	    PCI_EX_CONF_CAP + reg);
42893526Sxy150489 
42903526Sxy150489 	return (0);
42913526Sxy150489 }
42923526Sxy150489 
42933526Sxy150489 /*
42943526Sxy150489  * Enables PCI-Express master access.
42953526Sxy150489  *
42963526Sxy150489  * hw: Struct containing variables accessed by shared code
42973526Sxy150489  *
42983526Sxy150489  * returns: - none.
42993526Sxy150489  */
43003526Sxy150489 void
43013526Sxy150489 e1000_enable_pciex_master(struct e1000_hw *hw)
43023526Sxy150489 {
43033526Sxy150489 	uint32_t ctrl;
43043526Sxy150489 
43053526Sxy150489 	if (hw->bus_type != e1000_bus_type_pci_express)
43063526Sxy150489 		return;
43073526Sxy150489 
43083526Sxy150489 	ctrl = E1000_READ_REG(hw, CTRL);
43093526Sxy150489 	ctrl &= ~E1000_CTRL_GIO_MASTER_DISABLE;
43103526Sxy150489 	E1000_WRITE_REG(hw, CTRL, ctrl);
43113526Sxy150489 }
4312