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