xref: /onnv-gate/usr/src/uts/common/io/igb/igb_main.c (revision 7072:6eef27c9577f)
15779Sxy150489 /*
25779Sxy150489  * CDDL HEADER START
35779Sxy150489  *
45779Sxy150489  * Copyright(c) 2007-2008 Intel Corporation. All rights reserved.
55779Sxy150489  * The contents of this file are subject to the terms of the
65779Sxy150489  * Common Development and Distribution License (the "License").
75779Sxy150489  * You may not use this file except in compliance with the License.
85779Sxy150489  *
95779Sxy150489  * You can obtain a copy of the license at:
105779Sxy150489  *	http://www.opensolaris.org/os/licensing.
115779Sxy150489  * See the License for the specific language governing permissions
125779Sxy150489  * and limitations under the License.
135779Sxy150489  *
145779Sxy150489  * When using or redistributing this file, you may do so under the
155779Sxy150489  * License only. No other modification of this header is permitted.
165779Sxy150489  *
175779Sxy150489  * If applicable, add the following below this CDDL HEADER, with the
185779Sxy150489  * fields enclosed by brackets "[]" replaced with your own identifying
195779Sxy150489  * information: Portions Copyright [yyyy] [name of copyright owner]
205779Sxy150489  *
215779Sxy150489  * CDDL HEADER END
225779Sxy150489  */
235779Sxy150489 
245779Sxy150489 /*
255779Sxy150489  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
265779Sxy150489  * Use is subject to license terms of the CDDL.
275779Sxy150489  */
285779Sxy150489 
295779Sxy150489 #pragma ident	"%Z%%M%	%I%	%E% SMI"
305779Sxy150489 
315779Sxy150489 #include "igb_sw.h"
325779Sxy150489 
33*7072Sxy150489 static char ident[] = "Intel 1Gb Ethernet 1.1.3";
345779Sxy150489 
355779Sxy150489 /*
365779Sxy150489  * Local function protoypes
375779Sxy150489  */
385779Sxy150489 static int igb_register_mac(igb_t *);
395779Sxy150489 static int igb_identify_hardware(igb_t *);
405779Sxy150489 static int igb_regs_map(igb_t *);
415779Sxy150489 static void igb_init_properties(igb_t *);
425779Sxy150489 static int igb_init_driver_settings(igb_t *);
435779Sxy150489 static void igb_init_locks(igb_t *);
445779Sxy150489 static void igb_destroy_locks(igb_t *);
455779Sxy150489 static int igb_init(igb_t *);
465779Sxy150489 static int igb_chip_start(igb_t *);
475779Sxy150489 static void igb_chip_stop(igb_t *);
485779Sxy150489 static int igb_reset(igb_t *);
495779Sxy150489 static void igb_tx_clean(igb_t *);
505779Sxy150489 static boolean_t igb_tx_drain(igb_t *);
515779Sxy150489 static boolean_t igb_rx_drain(igb_t *);
525779Sxy150489 static int igb_alloc_rings(igb_t *);
535779Sxy150489 static int igb_init_rings(igb_t *);
545779Sxy150489 static void igb_free_rings(igb_t *);
555779Sxy150489 static void igb_fini_rings(igb_t *);
565779Sxy150489 static void igb_setup_rings(igb_t *);
575779Sxy150489 static void igb_setup_rx(igb_t *);
585779Sxy150489 static void igb_setup_tx(igb_t *);
595779Sxy150489 static void igb_setup_rx_ring(igb_rx_ring_t *);
605779Sxy150489 static void igb_setup_tx_ring(igb_tx_ring_t *);
615779Sxy150489 static void igb_setup_rss(igb_t *);
625779Sxy150489 static void igb_init_unicst(igb_t *);
635779Sxy150489 static void igb_setup_multicst(igb_t *);
645779Sxy150489 static void igb_get_phy_state(igb_t *);
655779Sxy150489 static void igb_get_conf(igb_t *);
665779Sxy150489 static int igb_get_prop(igb_t *, char *, int, int, int);
675779Sxy150489 static boolean_t igb_is_link_up(igb_t *);
685779Sxy150489 static boolean_t igb_link_check(igb_t *);
695779Sxy150489 static void igb_local_timer(void *);
705779Sxy150489 static void igb_arm_watchdog_timer(igb_t *);
715779Sxy150489 static void igb_start_watchdog_timer(igb_t *);
725779Sxy150489 static void igb_restart_watchdog_timer(igb_t *);
735779Sxy150489 static void igb_stop_watchdog_timer(igb_t *);
745779Sxy150489 static void igb_disable_adapter_interrupts(igb_t *);
755779Sxy150489 static void igb_enable_adapter_interrupts(igb_t *);
765779Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
775779Sxy150489 static boolean_t igb_stall_check(igb_t *);
785779Sxy150489 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
795779Sxy150489 static void igb_set_external_loopback(igb_t *);
805779Sxy150489 static void igb_set_internal_mac_loopback(igb_t *);
815779Sxy150489 static void igb_set_internal_phy_loopback(igb_t *);
825779Sxy150489 static void igb_set_internal_serdes_loopback(igb_t *);
835779Sxy150489 static boolean_t igb_find_mac_address(igb_t *);
845779Sxy150489 static int igb_alloc_intrs(igb_t *);
85*7072Sxy150489 static int igb_alloc_intr_handles(igb_t *, int);
865779Sxy150489 static int igb_add_intr_handlers(igb_t *);
875779Sxy150489 static void igb_rem_intr_handlers(igb_t *);
885779Sxy150489 static void igb_rem_intrs(igb_t *);
895779Sxy150489 static int igb_enable_intrs(igb_t *);
905779Sxy150489 static int igb_disable_intrs(igb_t *);
915779Sxy150489 static void igb_setup_adapter_msix(igb_t *);
925779Sxy150489 static uint_t igb_intr_legacy(void *, void *);
935779Sxy150489 static uint_t igb_intr_msi(void *, void *);
945779Sxy150489 static uint_t igb_intr_rx(void *, void *);
955779Sxy150489 static uint_t igb_intr_tx_other(void *, void *);
965779Sxy150489 static void igb_intr_rx_work(igb_rx_ring_t *);
975779Sxy150489 static void igb_intr_tx_work(igb_tx_ring_t *);
985779Sxy150489 static void igb_intr_other_work(igb_t *);
995779Sxy150489 static void igb_get_driver_control(struct e1000_hw *);
1005779Sxy150489 static void igb_release_driver_control(struct e1000_hw *);
1015779Sxy150489 
1025779Sxy150489 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
1035779Sxy150489 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
1045779Sxy150489 static int igb_resume(dev_info_t *);
1055779Sxy150489 static int igb_suspend(dev_info_t *);
1065779Sxy150489 static void igb_unconfigure(dev_info_t *, igb_t *);
1076624Sgl147354 static int igb_fm_error_cb(dev_info_t *, ddi_fm_error_t *,
1086624Sgl147354     const void *);
1096624Sgl147354 static void igb_fm_init(igb_t *);
1106624Sgl147354 static void igb_fm_fini(igb_t *);
1116624Sgl147354 
1125779Sxy150489 
1135779Sxy150489 static struct cb_ops igb_cb_ops = {
1145779Sxy150489 	nulldev,		/* cb_open */
1155779Sxy150489 	nulldev,		/* cb_close */
1165779Sxy150489 	nodev,			/* cb_strategy */
1175779Sxy150489 	nodev,			/* cb_print */
1185779Sxy150489 	nodev,			/* cb_dump */
1195779Sxy150489 	nodev,			/* cb_read */
1205779Sxy150489 	nodev,			/* cb_write */
1215779Sxy150489 	nodev,			/* cb_ioctl */
1225779Sxy150489 	nodev,			/* cb_devmap */
1235779Sxy150489 	nodev,			/* cb_mmap */
1245779Sxy150489 	nodev,			/* cb_segmap */
1255779Sxy150489 	nochpoll,		/* cb_chpoll */
1265779Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1275779Sxy150489 	NULL,			/* cb_stream */
1285779Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1295779Sxy150489 	CB_REV,			/* cb_rev */
1305779Sxy150489 	nodev,			/* cb_aread */
1315779Sxy150489 	nodev			/* cb_awrite */
1325779Sxy150489 };
1335779Sxy150489 
1345779Sxy150489 static struct dev_ops igb_dev_ops = {
1355779Sxy150489 	DEVO_REV,		/* devo_rev */
1365779Sxy150489 	0,			/* devo_refcnt */
1375779Sxy150489 	NULL,			/* devo_getinfo */
1385779Sxy150489 	nulldev,		/* devo_identify */
1395779Sxy150489 	nulldev,		/* devo_probe */
1405779Sxy150489 	igb_attach,		/* devo_attach */
1415779Sxy150489 	igb_detach,		/* devo_detach */
1425779Sxy150489 	nodev,			/* devo_reset */
1435779Sxy150489 	&igb_cb_ops,		/* devo_cb_ops */
1445779Sxy150489 	NULL,			/* devo_bus_ops */
1455779Sxy150489 	ddi_power		/* devo_power */
1465779Sxy150489 };
1475779Sxy150489 
1485779Sxy150489 static struct modldrv igb_modldrv = {
1495779Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1505779Sxy150489 	ident,			/* Discription string */
1515779Sxy150489 	&igb_dev_ops,		/* driver ops */
1525779Sxy150489 };
1535779Sxy150489 
1545779Sxy150489 static struct modlinkage igb_modlinkage = {
1555779Sxy150489 	MODREV_1, &igb_modldrv, NULL
1565779Sxy150489 };
1575779Sxy150489 
1585779Sxy150489 /* Access attributes for register mapping */
1595779Sxy150489 ddi_device_acc_attr_t igb_regs_acc_attr = {
1605779Sxy150489 	DDI_DEVICE_ATTR_V0,
1615779Sxy150489 	DDI_STRUCTURE_LE_ACC,
1625779Sxy150489 	DDI_STRICTORDER_ACC,
1636624Sgl147354 	DDI_FLAGERR_ACC
1645779Sxy150489 };
1655779Sxy150489 
1665779Sxy150489 #define	IGB_M_CALLBACK_FLAGS	(MC_IOCTL | MC_GETCAPAB)
1675779Sxy150489 
1685779Sxy150489 static mac_callbacks_t igb_m_callbacks = {
1695779Sxy150489 	IGB_M_CALLBACK_FLAGS,
1705779Sxy150489 	igb_m_stat,
1715779Sxy150489 	igb_m_start,
1725779Sxy150489 	igb_m_stop,
1735779Sxy150489 	igb_m_promisc,
1745779Sxy150489 	igb_m_multicst,
1755779Sxy150489 	igb_m_unicst,
1765779Sxy150489 	igb_m_tx,
1775779Sxy150489 	NULL,
1785779Sxy150489 	igb_m_ioctl,
1795779Sxy150489 	igb_m_getcapab
1805779Sxy150489 };
1815779Sxy150489 
1825779Sxy150489 
1835779Sxy150489 /*
1845779Sxy150489  * Module Initialization Functions
1855779Sxy150489  */
1865779Sxy150489 
1875779Sxy150489 int
1885779Sxy150489 _init(void)
1895779Sxy150489 {
1905779Sxy150489 	int status;
1915779Sxy150489 
1925779Sxy150489 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
1935779Sxy150489 
1945779Sxy150489 	status = mod_install(&igb_modlinkage);
1955779Sxy150489 
1965779Sxy150489 	if (status != DDI_SUCCESS) {
1975779Sxy150489 		mac_fini_ops(&igb_dev_ops);
1985779Sxy150489 	}
1995779Sxy150489 
2005779Sxy150489 	return (status);
2015779Sxy150489 }
2025779Sxy150489 
2035779Sxy150489 int
2045779Sxy150489 _fini(void)
2055779Sxy150489 {
2065779Sxy150489 	int status;
2075779Sxy150489 
2085779Sxy150489 	status = mod_remove(&igb_modlinkage);
2095779Sxy150489 
2105779Sxy150489 	if (status == DDI_SUCCESS) {
2115779Sxy150489 		mac_fini_ops(&igb_dev_ops);
2125779Sxy150489 	}
2135779Sxy150489 
2145779Sxy150489 	return (status);
2155779Sxy150489 
2165779Sxy150489 }
2175779Sxy150489 
2185779Sxy150489 int
2195779Sxy150489 _info(struct modinfo *modinfop)
2205779Sxy150489 {
2215779Sxy150489 	int status;
2225779Sxy150489 
2235779Sxy150489 	status = mod_info(&igb_modlinkage, modinfop);
2245779Sxy150489 
2255779Sxy150489 	return (status);
2265779Sxy150489 }
2275779Sxy150489 
2285779Sxy150489 /*
2295779Sxy150489  * igb_attach - driver attach
2305779Sxy150489  *
2315779Sxy150489  * This function is the device specific initialization entry
2325779Sxy150489  * point. This entry point is required and must be written.
2335779Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
2345779Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
2355779Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
2365779Sxy150489  * available for use by the driver.
2375779Sxy150489  *
2385779Sxy150489  * The attach() function will be called once for each instance
2395779Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
2405779Sxy150489  * Until attach() succeeds, the only driver entry points which
2415779Sxy150489  * may be called are open(9E) and getinfo(9E).
2425779Sxy150489  */
2435779Sxy150489 static int
2445779Sxy150489 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2455779Sxy150489 {
2465779Sxy150489 	igb_t *igb;
2475779Sxy150489 	struct igb_osdep *osdep;
2485779Sxy150489 	struct e1000_hw *hw;
2495779Sxy150489 	int instance;
2505779Sxy150489 
2515779Sxy150489 	/*
2525779Sxy150489 	 * Check the command and perform corresponding operations
2535779Sxy150489 	 */
2545779Sxy150489 	switch (cmd) {
2555779Sxy150489 	default:
2565779Sxy150489 		return (DDI_FAILURE);
2575779Sxy150489 
2585779Sxy150489 	case DDI_RESUME:
2595779Sxy150489 		return (igb_resume(devinfo));
2605779Sxy150489 
2615779Sxy150489 	case DDI_ATTACH:
2625779Sxy150489 		break;
2635779Sxy150489 	}
2645779Sxy150489 
2655779Sxy150489 	/* Get the device instance */
2665779Sxy150489 	instance = ddi_get_instance(devinfo);
2675779Sxy150489 
2685779Sxy150489 	/* Allocate memory for the instance data structure */
2695779Sxy150489 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
2705779Sxy150489 
2715779Sxy150489 	igb->dip = devinfo;
2725779Sxy150489 	igb->instance = instance;
2735779Sxy150489 
2745779Sxy150489 	hw = &igb->hw;
2755779Sxy150489 	osdep = &igb->osdep;
2765779Sxy150489 	hw->back = osdep;
2775779Sxy150489 	osdep->igb = igb;
2785779Sxy150489 
2795779Sxy150489 	/* Attach the instance pointer to the dev_info data structure */
2805779Sxy150489 	ddi_set_driver_private(devinfo, igb);
2815779Sxy150489 
2826624Sgl147354 
2836624Sgl147354 	/* Initialize for fma support */
2846624Sgl147354 	igb->fm_capabilities = igb_get_prop(igb, "fm-capable",
2856624Sgl147354 	    0, 0x0f,
2866624Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
2876624Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
2886624Sgl147354 	igb_fm_init(igb);
2896624Sgl147354 	igb->attach_progress |= ATTACH_PROGRESS_FMINIT;
2906624Sgl147354 
2915779Sxy150489 	/*
2925779Sxy150489 	 * Map PCI config space registers
2935779Sxy150489 	 */
2945779Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
2955779Sxy150489 		igb_error(igb, "Failed to map PCI configurations");
2965779Sxy150489 		goto attach_fail;
2975779Sxy150489 	}
2985779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
2995779Sxy150489 
3005779Sxy150489 	/*
3015779Sxy150489 	 * Identify the chipset family
3025779Sxy150489 	 */
3035779Sxy150489 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
3045779Sxy150489 		igb_error(igb, "Failed to identify hardware");
3055779Sxy150489 		goto attach_fail;
3065779Sxy150489 	}
3075779Sxy150489 
3085779Sxy150489 	/*
3095779Sxy150489 	 * Map device registers
3105779Sxy150489 	 */
3115779Sxy150489 	if (igb_regs_map(igb) != IGB_SUCCESS) {
3125779Sxy150489 		igb_error(igb, "Failed to map device registers");
3135779Sxy150489 		goto attach_fail;
3145779Sxy150489 	}
3155779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
3165779Sxy150489 
3175779Sxy150489 	/*
3185779Sxy150489 	 * Initialize driver parameters
3195779Sxy150489 	 */
3205779Sxy150489 	igb_init_properties(igb);
3215779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
3225779Sxy150489 
3235779Sxy150489 	/*
3245779Sxy150489 	 * Allocate interrupts
3255779Sxy150489 	 */
3265779Sxy150489 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
3275779Sxy150489 		igb_error(igb, "Failed to allocate interrupts");
3285779Sxy150489 		goto attach_fail;
3295779Sxy150489 	}
3305779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
3315779Sxy150489 
3325779Sxy150489 	/*
3335779Sxy150489 	 * Allocate rx/tx rings based on the ring numbers.
3345779Sxy150489 	 * The actual numbers of rx/tx rings are decided by the number of
3355779Sxy150489 	 * allocated interrupt vectors, so we should allocate the rings after
3365779Sxy150489 	 * interrupts are allocated.
3375779Sxy150489 	 */
3385779Sxy150489 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
3395779Sxy150489 		igb_error(igb, "Failed to allocate rx and tx rings");
3405779Sxy150489 		goto attach_fail;
3415779Sxy150489 	}
3425779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
3435779Sxy150489 
3445779Sxy150489 	/*
3455779Sxy150489 	 * Add interrupt handlers
3465779Sxy150489 	 */
3475779Sxy150489 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
3485779Sxy150489 		igb_error(igb, "Failed to add interrupt handlers");
3495779Sxy150489 		goto attach_fail;
3505779Sxy150489 	}
3515779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
3525779Sxy150489 
3535779Sxy150489 	/*
3545779Sxy150489 	 * Initialize driver parameters
3555779Sxy150489 	 */
3565779Sxy150489 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
3575779Sxy150489 		igb_error(igb, "Failed to initialize driver settings");
3585779Sxy150489 		goto attach_fail;
3595779Sxy150489 	}
3605779Sxy150489 
3616624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
3626624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
3636624Sgl147354 		goto attach_fail;
3646624Sgl147354 	}
3656624Sgl147354 
3665779Sxy150489 	/*
3675779Sxy150489 	 * Initialize mutexes for this device.
3685779Sxy150489 	 * Do this before enabling the interrupt handler and
3695779Sxy150489 	 * register the softint to avoid the condition where
3705779Sxy150489 	 * interrupt handler can try using uninitialized mutex
3715779Sxy150489 	 */
3725779Sxy150489 	igb_init_locks(igb);
3735779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
3745779Sxy150489 
3755779Sxy150489 	/*
3765779Sxy150489 	 * Initialize chipset hardware
3775779Sxy150489 	 */
3785779Sxy150489 	if (igb_init(igb) != IGB_SUCCESS) {
3795779Sxy150489 		igb_error(igb, "Failed to initialize adapter");
3805779Sxy150489 		goto attach_fail;
3815779Sxy150489 	}
3825779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT;
3835779Sxy150489 
3845779Sxy150489 	/*
3855779Sxy150489 	 * Initialize DMA and hardware settings for rx/tx rings
3865779Sxy150489 	 */
3875779Sxy150489 	if (igb_init_rings(igb) != IGB_SUCCESS) {
3885779Sxy150489 		igb_error(igb, "Failed to initialize rings");
3895779Sxy150489 		goto attach_fail;
3905779Sxy150489 	}
3915779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT_RINGS;
3925779Sxy150489 
3935779Sxy150489 	/*
3945779Sxy150489 	 * Initialize statistics
3955779Sxy150489 	 */
3965779Sxy150489 	if (igb_init_stats(igb) != IGB_SUCCESS) {
3975779Sxy150489 		igb_error(igb, "Failed to initialize statistics");
3985779Sxy150489 		goto attach_fail;
3995779Sxy150489 	}
4005779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_STATS;
4015779Sxy150489 
4025779Sxy150489 	/*
4035779Sxy150489 	 * Initialize NDD parameters
4045779Sxy150489 	 */
4055779Sxy150489 	if (igb_nd_init(igb) != IGB_SUCCESS) {
4065779Sxy150489 		igb_error(igb, "Failed to initialize ndd");
4075779Sxy150489 		goto attach_fail;
4085779Sxy150489 	}
4095779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_NDD;
4105779Sxy150489 
4115779Sxy150489 	/*
4125779Sxy150489 	 * Register the driver to the MAC
4135779Sxy150489 	 */
4145779Sxy150489 	if (igb_register_mac(igb) != IGB_SUCCESS) {
4155779Sxy150489 		igb_error(igb, "Failed to register MAC");
4165779Sxy150489 		goto attach_fail;
4175779Sxy150489 	}
4185779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_MAC;
4195779Sxy150489 
4205779Sxy150489 	/*
4215779Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
4225779Sxy150489 	 * initialized, enable interrupts.
4235779Sxy150489 	 */
4245779Sxy150489 	if (igb_enable_intrs(igb) != IGB_SUCCESS) {
4255779Sxy150489 		igb_error(igb, "Failed to enable DDI interrupts");
4265779Sxy150489 		goto attach_fail;
4275779Sxy150489 	}
4285779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
4295779Sxy150489 
4305779Sxy150489 	igb->igb_state |= IGB_INITIALIZED;
4315779Sxy150489 
4325779Sxy150489 	return (DDI_SUCCESS);
4335779Sxy150489 
4345779Sxy150489 attach_fail:
4355779Sxy150489 	igb_unconfigure(devinfo, igb);
4365779Sxy150489 	return (DDI_FAILURE);
4375779Sxy150489 }
4385779Sxy150489 
4395779Sxy150489 /*
4405779Sxy150489  * igb_detach - driver detach
4415779Sxy150489  *
4425779Sxy150489  * The detach() function is the complement of the attach routine.
4435779Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
4445779Sxy150489  * state  associated  with  a  given  instance of a device node
4455779Sxy150489  * prior to the removal of that instance from the system.
4465779Sxy150489  *
4475779Sxy150489  * The detach() function will be called once for each  instance
4485779Sxy150489  * of the device for which there has been a successful attach()
4495779Sxy150489  * once there are no longer  any  opens  on  the  device.
4505779Sxy150489  *
4515779Sxy150489  * Interrupts routine are disabled, All memory allocated by this
4525779Sxy150489  * driver are freed.
4535779Sxy150489  */
4545779Sxy150489 static int
4555779Sxy150489 igb_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
4565779Sxy150489 {
4575779Sxy150489 	igb_t *igb;
4585779Sxy150489 
4595779Sxy150489 	/*
4605779Sxy150489 	 * Check detach command
4615779Sxy150489 	 */
4625779Sxy150489 	switch (cmd) {
4635779Sxy150489 	default:
4645779Sxy150489 		return (DDI_FAILURE);
4655779Sxy150489 
4665779Sxy150489 	case DDI_SUSPEND:
4675779Sxy150489 		return (igb_suspend(devinfo));
4685779Sxy150489 
4695779Sxy150489 	case DDI_DETACH:
4705779Sxy150489 		break;
4715779Sxy150489 	}
4725779Sxy150489 
4735779Sxy150489 
4745779Sxy150489 	/*
4755779Sxy150489 	 * Get the pointer to the driver private data structure
4765779Sxy150489 	 */
4775779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
4785779Sxy150489 	if (igb == NULL)
4795779Sxy150489 		return (DDI_FAILURE);
4805779Sxy150489 
4815779Sxy150489 	/*
4825779Sxy150489 	 * Unregister MAC. If failed, we have to fail the detach
4835779Sxy150489 	 */
4845779Sxy150489 	if (mac_unregister(igb->mac_hdl) != 0) {
4855779Sxy150489 		igb_error(igb, "Failed to unregister MAC");
4865779Sxy150489 		return (DDI_FAILURE);
4875779Sxy150489 	}
4885779Sxy150489 	igb->attach_progress &= ~ATTACH_PROGRESS_MAC;
4895779Sxy150489 
4905779Sxy150489 	/*
4915779Sxy150489 	 * If the device is still running, it needs to be stopped first.
4925779Sxy150489 	 * This check is necessary because under some specific circumstances,
4935779Sxy150489 	 * the detach routine can be called without stopping the interface
4945779Sxy150489 	 * first.
4955779Sxy150489 	 */
4965779Sxy150489 	mutex_enter(&igb->gen_lock);
4975779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
4985779Sxy150489 		igb->igb_state &= ~IGB_STARTED;
4995779Sxy150489 		igb_stop(igb);
5005779Sxy150489 		mutex_exit(&igb->gen_lock);
5015779Sxy150489 		/* Disable and stop the watchdog timer */
5025779Sxy150489 		igb_disable_watchdog_timer(igb);
5035779Sxy150489 	} else
5045779Sxy150489 		mutex_exit(&igb->gen_lock);
5055779Sxy150489 
5065779Sxy150489 	/*
5075779Sxy150489 	 * Check if there are still rx buffers held by the upper layer.
5085779Sxy150489 	 * If so, fail the detach.
5095779Sxy150489 	 */
5105779Sxy150489 	if (!igb_rx_drain(igb))
5115779Sxy150489 		return (DDI_FAILURE);
5125779Sxy150489 
5135779Sxy150489 	/*
5145779Sxy150489 	 * Do the remaining unconfigure routines
5155779Sxy150489 	 */
5165779Sxy150489 	igb_unconfigure(devinfo, igb);
5175779Sxy150489 
5185779Sxy150489 	return (DDI_SUCCESS);
5195779Sxy150489 }
5205779Sxy150489 
5215779Sxy150489 static void
5225779Sxy150489 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
5235779Sxy150489 {
5245779Sxy150489 	/*
5255779Sxy150489 	 * Disable interrupt
5265779Sxy150489 	 */
5275779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
5285779Sxy150489 		(void) igb_disable_intrs(igb);
5295779Sxy150489 	}
5305779Sxy150489 
5315779Sxy150489 	/*
5325779Sxy150489 	 * Unregister MAC
5335779Sxy150489 	 */
5345779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
5355779Sxy150489 		(void) mac_unregister(igb->mac_hdl);
5365779Sxy150489 	}
5375779Sxy150489 
5385779Sxy150489 	/*
5395779Sxy150489 	 * Free ndd parameters
5405779Sxy150489 	 */
5415779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_NDD) {
5425779Sxy150489 		igb_nd_cleanup(igb);
5435779Sxy150489 	}
5445779Sxy150489 
5455779Sxy150489 	/*
5465779Sxy150489 	 * Free statistics
5475779Sxy150489 	 */
5485779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
5495779Sxy150489 		kstat_delete((kstat_t *)igb->igb_ks);
5505779Sxy150489 	}
5515779Sxy150489 
5525779Sxy150489 	/*
5535779Sxy150489 	 * Remove interrupt handlers
5545779Sxy150489 	 */
5555779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
5565779Sxy150489 		igb_rem_intr_handlers(igb);
5575779Sxy150489 	}
5585779Sxy150489 
5595779Sxy150489 	/*
5605779Sxy150489 	 * Remove interrupts
5615779Sxy150489 	 */
5625779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
5635779Sxy150489 		igb_rem_intrs(igb);
5645779Sxy150489 	}
5655779Sxy150489 
5665779Sxy150489 	/*
5675779Sxy150489 	 * Remove driver properties
5685779Sxy150489 	 */
5695779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
5705779Sxy150489 		(void) ddi_prop_remove_all(devinfo);
5715779Sxy150489 	}
5725779Sxy150489 
5735779Sxy150489 	/*
5745779Sxy150489 	 * Release the DMA resources of rx/tx rings
5755779Sxy150489 	 */
5765779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_RINGS) {
5775779Sxy150489 		igb_fini_rings(igb);
5785779Sxy150489 	}
5795779Sxy150489 
5805779Sxy150489 	/*
5815779Sxy150489 	 * Stop the chipset
5825779Sxy150489 	 */
5835779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT) {
5845779Sxy150489 		mutex_enter(&igb->gen_lock);
5855779Sxy150489 		igb_chip_stop(igb);
5865779Sxy150489 		mutex_exit(&igb->gen_lock);
5876624Sgl147354 		if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
5886624Sgl147354 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_UNAFFECTED);
5895779Sxy150489 	}
5905779Sxy150489 
5915779Sxy150489 	/*
5925779Sxy150489 	 * Free register handle
5935779Sxy150489 	 */
5945779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
5955779Sxy150489 		if (igb->osdep.reg_handle != NULL)
5965779Sxy150489 			ddi_regs_map_free(&igb->osdep.reg_handle);
5975779Sxy150489 	}
5985779Sxy150489 
5995779Sxy150489 	/*
6005779Sxy150489 	 * Free PCI config handle
6015779Sxy150489 	 */
6025779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
6035779Sxy150489 		if (igb->osdep.cfg_handle != NULL)
6045779Sxy150489 			pci_config_teardown(&igb->osdep.cfg_handle);
6055779Sxy150489 	}
6065779Sxy150489 
6075779Sxy150489 	/*
6085779Sxy150489 	 * Free locks
6095779Sxy150489 	 */
6105779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
6115779Sxy150489 		igb_destroy_locks(igb);
6125779Sxy150489 	}
6135779Sxy150489 
6145779Sxy150489 	/*
6155779Sxy150489 	 * Free the rx/tx rings
6165779Sxy150489 	 */
6175779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
6185779Sxy150489 		igb_free_rings(igb);
6195779Sxy150489 	}
6205779Sxy150489 
6215779Sxy150489 	/*
6226624Sgl147354 	 * Remove FMA
6236624Sgl147354 	 */
6246624Sgl147354 	if (igb->attach_progress & ATTACH_PROGRESS_FMINIT) {
6256624Sgl147354 		igb_fm_fini(igb);
6266624Sgl147354 	}
6276624Sgl147354 
6286624Sgl147354 	/*
6295779Sxy150489 	 * Free device specific structure
6305779Sxy150489 	 */
6315779Sxy150489 	e1000_remove_device(&igb->hw);
6325779Sxy150489 
6335779Sxy150489 	/*
6345779Sxy150489 	 * Free the driver data structure
6355779Sxy150489 	 */
6365779Sxy150489 	kmem_free(igb, sizeof (igb_t));
6375779Sxy150489 
6385779Sxy150489 	ddi_set_driver_private(devinfo, NULL);
6395779Sxy150489 }
6405779Sxy150489 
6415779Sxy150489 /*
6425779Sxy150489  * igb_register_mac - Register the driver and its function pointers with
6435779Sxy150489  * the GLD interface
6445779Sxy150489  */
6455779Sxy150489 static int
6465779Sxy150489 igb_register_mac(igb_t *igb)
6475779Sxy150489 {
6485779Sxy150489 	struct e1000_hw *hw = &igb->hw;
6495779Sxy150489 	mac_register_t *mac;
6505779Sxy150489 	int status;
6515779Sxy150489 
6525779Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
6535779Sxy150489 		return (IGB_FAILURE);
6545779Sxy150489 
6555779Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
6565779Sxy150489 	mac->m_driver = igb;
6575779Sxy150489 	mac->m_dip = igb->dip;
6585779Sxy150489 	mac->m_src_addr = hw->mac.addr;
6595779Sxy150489 	mac->m_callbacks = &igb_m_callbacks;
6605779Sxy150489 	mac->m_min_sdu = 0;
6615779Sxy150489 	mac->m_max_sdu = igb->max_frame_size -
6625779Sxy150489 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
6635895Syz147064 	mac->m_margin = VLAN_TAGSZ;
6645779Sxy150489 
6655779Sxy150489 	status = mac_register(mac, &igb->mac_hdl);
6665779Sxy150489 
6675779Sxy150489 	mac_free(mac);
6685779Sxy150489 
6695779Sxy150489 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
6705779Sxy150489 }
6715779Sxy150489 
6725779Sxy150489 /*
6735779Sxy150489  * igb_identify_hardware - Identify the type of the chipset
6745779Sxy150489  */
6755779Sxy150489 static int
6765779Sxy150489 igb_identify_hardware(igb_t *igb)
6775779Sxy150489 {
6785779Sxy150489 	struct e1000_hw *hw = &igb->hw;
6795779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
6805779Sxy150489 
6815779Sxy150489 	/*
6825779Sxy150489 	 * Get the device id
6835779Sxy150489 	 */
6845779Sxy150489 	hw->vendor_id =
6855779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
6865779Sxy150489 	hw->device_id =
6875779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
6885779Sxy150489 	hw->revision_id =
6895779Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
6905779Sxy150489 	hw->subsystem_device_id =
6915779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
6925779Sxy150489 	hw->subsystem_vendor_id =
6935779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
6945779Sxy150489 
6955779Sxy150489 	/*
6965779Sxy150489 	 * Set the mac type of the adapter based on the device id
6975779Sxy150489 	 */
6985779Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
6995779Sxy150489 		return (IGB_FAILURE);
7005779Sxy150489 	}
7015779Sxy150489 
7025779Sxy150489 	return (IGB_SUCCESS);
7035779Sxy150489 }
7045779Sxy150489 
7055779Sxy150489 /*
7065779Sxy150489  * igb_regs_map - Map the device registers
7075779Sxy150489  */
7085779Sxy150489 static int
7095779Sxy150489 igb_regs_map(igb_t *igb)
7105779Sxy150489 {
7115779Sxy150489 	dev_info_t *devinfo = igb->dip;
7125779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7135779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
7145779Sxy150489 	off_t mem_size;
7155779Sxy150489 
7165779Sxy150489 	/*
7175779Sxy150489 	 * First get the size of device registers to be mapped.
7185779Sxy150489 	 */
7195779Sxy150489 	if (ddi_dev_regsize(devinfo, 1, &mem_size) != DDI_SUCCESS) {
7205779Sxy150489 		return (IGB_FAILURE);
7215779Sxy150489 	}
7225779Sxy150489 
7235779Sxy150489 	/*
7245779Sxy150489 	 * Call ddi_regs_map_setup() to map registers
7255779Sxy150489 	 */
7265779Sxy150489 	if ((ddi_regs_map_setup(devinfo, 1,
7275779Sxy150489 	    (caddr_t *)&hw->hw_addr, 0,
7285779Sxy150489 	    mem_size, &igb_regs_acc_attr,
7295779Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
7305779Sxy150489 		return (IGB_FAILURE);
7315779Sxy150489 	}
7325779Sxy150489 
7335779Sxy150489 	return (IGB_SUCCESS);
7345779Sxy150489 }
7355779Sxy150489 
7365779Sxy150489 /*
7375779Sxy150489  * igb_init_properties - Initialize driver properties
7385779Sxy150489  */
7395779Sxy150489 static void
7405779Sxy150489 igb_init_properties(igb_t *igb)
7415779Sxy150489 {
7425779Sxy150489 	/*
7435779Sxy150489 	 * Get conf file properties, including link settings
7445779Sxy150489 	 * jumbo frames, ring number, descriptor number, etc.
7455779Sxy150489 	 */
7465779Sxy150489 	igb_get_conf(igb);
7475779Sxy150489 }
7485779Sxy150489 
7495779Sxy150489 /*
7505779Sxy150489  * igb_init_driver_settings - Initialize driver settings
7515779Sxy150489  *
7525779Sxy150489  * The settings include hardware function pointers, bus information,
7535779Sxy150489  * rx/tx rings settings, link state, and any other parameters that
7545779Sxy150489  * need to be setup during driver initialization.
7555779Sxy150489  */
7565779Sxy150489 static int
7575779Sxy150489 igb_init_driver_settings(igb_t *igb)
7585779Sxy150489 {
7595779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7605779Sxy150489 	igb_rx_ring_t *rx_ring;
7615779Sxy150489 	igb_tx_ring_t *tx_ring;
7625779Sxy150489 	uint32_t rx_size;
7635779Sxy150489 	uint32_t tx_size;
7645779Sxy150489 	int i;
7655779Sxy150489 
7665779Sxy150489 	/*
7675779Sxy150489 	 * Initialize chipset specific hardware function pointers
7685779Sxy150489 	 */
7695779Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
7705779Sxy150489 		return (IGB_FAILURE);
7715779Sxy150489 	}
7725779Sxy150489 
7735779Sxy150489 	/*
7745779Sxy150489 	 * Get bus information
7755779Sxy150489 	 */
7765779Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
7775779Sxy150489 		return (IGB_FAILURE);
7785779Sxy150489 	}
7795779Sxy150489 
7805779Sxy150489 	/*
7815779Sxy150489 	 * Set rx buffer size
7825779Sxy150489 	 * The IP header alignment room is counted in the calculation.
7835779Sxy150489 	 * The rx buffer size is in unit of 1K that is required by the
7845779Sxy150489 	 * chipset hardware.
7855779Sxy150489 	 */
7865779Sxy150489 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
7875779Sxy150489 	igb->rx_buf_size = ((rx_size >> 10) +
7885779Sxy150489 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
7895779Sxy150489 
7905779Sxy150489 	/*
7915779Sxy150489 	 * Set tx buffer size
7925779Sxy150489 	 */
7935779Sxy150489 	tx_size = igb->max_frame_size;
7945779Sxy150489 	igb->tx_buf_size = ((tx_size >> 10) +
7955779Sxy150489 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
7965779Sxy150489 
7975779Sxy150489 	/*
7985779Sxy150489 	 * Initialize rx/tx rings parameters
7995779Sxy150489 	 */
8005779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
8015779Sxy150489 		rx_ring = &igb->rx_rings[i];
8025779Sxy150489 		rx_ring->index = i;
8035779Sxy150489 		rx_ring->igb = igb;
8045779Sxy150489 
8055779Sxy150489 		rx_ring->ring_size = igb->rx_ring_size;
8065779Sxy150489 		rx_ring->free_list_size = igb->rx_ring_size;
8075779Sxy150489 		rx_ring->copy_thresh = igb->rx_copy_thresh;
8085779Sxy150489 		rx_ring->limit_per_intr = igb->rx_limit_per_intr;
8095779Sxy150489 	}
8105779Sxy150489 
8115779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
8125779Sxy150489 		tx_ring = &igb->tx_rings[i];
8135779Sxy150489 		tx_ring->index = i;
8145779Sxy150489 		tx_ring->igb = igb;
8155779Sxy150489 		if (igb->tx_head_wb_enable)
8165779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
8175779Sxy150489 		else
8185779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
8195779Sxy150489 
8205779Sxy150489 		tx_ring->ring_size = igb->tx_ring_size;
8215779Sxy150489 		tx_ring->free_list_size = igb->tx_ring_size +
8225779Sxy150489 		    (igb->tx_ring_size >> 1);
8235779Sxy150489 		tx_ring->copy_thresh = igb->tx_copy_thresh;
8245779Sxy150489 		tx_ring->recycle_thresh = igb->tx_recycle_thresh;
8255779Sxy150489 		tx_ring->overload_thresh = igb->tx_overload_thresh;
8265779Sxy150489 		tx_ring->resched_thresh = igb->tx_resched_thresh;
8275779Sxy150489 	}
8285779Sxy150489 
8295779Sxy150489 	/*
8305779Sxy150489 	 * Initialize values of interrupt throttling rate
8315779Sxy150489 	 */
8325779Sxy150489 	for (i = 1; i < MAX_NUM_EITR; i++)
8335779Sxy150489 		igb->intr_throttling[i] = igb->intr_throttling[0];
8345779Sxy150489 
8355779Sxy150489 	/*
8365779Sxy150489 	 * The initial link state should be "unknown"
8375779Sxy150489 	 */
8385779Sxy150489 	igb->link_state = LINK_STATE_UNKNOWN;
8395779Sxy150489 
8405779Sxy150489 	return (IGB_SUCCESS);
8415779Sxy150489 }
8425779Sxy150489 
8435779Sxy150489 /*
8445779Sxy150489  * igb_init_locks - Initialize locks
8455779Sxy150489  */
8465779Sxy150489 static void
8475779Sxy150489 igb_init_locks(igb_t *igb)
8485779Sxy150489 {
8495779Sxy150489 	igb_rx_ring_t *rx_ring;
8505779Sxy150489 	igb_tx_ring_t *tx_ring;
8515779Sxy150489 	int i;
8525779Sxy150489 
8535779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
8545779Sxy150489 		rx_ring = &igb->rx_rings[i];
8555779Sxy150489 		mutex_init(&rx_ring->rx_lock, NULL,
8565779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8575779Sxy150489 		mutex_init(&rx_ring->recycle_lock, NULL,
8585779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8595779Sxy150489 	}
8605779Sxy150489 
8615779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
8625779Sxy150489 		tx_ring = &igb->tx_rings[i];
8635779Sxy150489 		mutex_init(&tx_ring->tx_lock, NULL,
8645779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8655779Sxy150489 		mutex_init(&tx_ring->recycle_lock, NULL,
8665779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8675779Sxy150489 		mutex_init(&tx_ring->tcb_head_lock, NULL,
8685779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8695779Sxy150489 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
8705779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8715779Sxy150489 	}
8725779Sxy150489 
8735779Sxy150489 	mutex_init(&igb->gen_lock, NULL,
8745779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8755779Sxy150489 
8765779Sxy150489 	mutex_init(&igb->watchdog_lock, NULL,
8775779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
8785779Sxy150489 }
8795779Sxy150489 
8805779Sxy150489 /*
8815779Sxy150489  * igb_destroy_locks - Destroy locks
8825779Sxy150489  */
8835779Sxy150489 static void
8845779Sxy150489 igb_destroy_locks(igb_t *igb)
8855779Sxy150489 {
8865779Sxy150489 	igb_rx_ring_t *rx_ring;
8875779Sxy150489 	igb_tx_ring_t *tx_ring;
8885779Sxy150489 	int i;
8895779Sxy150489 
8905779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
8915779Sxy150489 		rx_ring = &igb->rx_rings[i];
8925779Sxy150489 		mutex_destroy(&rx_ring->rx_lock);
8935779Sxy150489 		mutex_destroy(&rx_ring->recycle_lock);
8945779Sxy150489 	}
8955779Sxy150489 
8965779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
8975779Sxy150489 		tx_ring = &igb->tx_rings[i];
8985779Sxy150489 		mutex_destroy(&tx_ring->tx_lock);
8995779Sxy150489 		mutex_destroy(&tx_ring->recycle_lock);
9005779Sxy150489 		mutex_destroy(&tx_ring->tcb_head_lock);
9015779Sxy150489 		mutex_destroy(&tx_ring->tcb_tail_lock);
9025779Sxy150489 	}
9035779Sxy150489 
9045779Sxy150489 	mutex_destroy(&igb->gen_lock);
9055779Sxy150489 	mutex_destroy(&igb->watchdog_lock);
9065779Sxy150489 }
9075779Sxy150489 
9085779Sxy150489 static int
9095779Sxy150489 igb_resume(dev_info_t *devinfo)
9105779Sxy150489 {
9115779Sxy150489 	igb_t *igb;
9125779Sxy150489 
9135779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
9145779Sxy150489 	if (igb == NULL)
9155779Sxy150489 		return (DDI_FAILURE);
9165779Sxy150489 
9175779Sxy150489 	mutex_enter(&igb->gen_lock);
9185779Sxy150489 
9195779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
9205779Sxy150489 		if (igb_start(igb) != IGB_SUCCESS) {
9215779Sxy150489 			mutex_exit(&igb->gen_lock);
9225779Sxy150489 			return (DDI_FAILURE);
9235779Sxy150489 		}
9245779Sxy150489 
9255779Sxy150489 		/*
9265779Sxy150489 		 * Enable and start the watchdog timer
9275779Sxy150489 		 */
9285779Sxy150489 		igb_enable_watchdog_timer(igb);
9295779Sxy150489 	}
9305779Sxy150489 
9315779Sxy150489 	igb->igb_state &= ~IGB_SUSPENDED;
9325779Sxy150489 
9335779Sxy150489 	mutex_exit(&igb->gen_lock);
9345779Sxy150489 
9355779Sxy150489 	return (DDI_SUCCESS);
9365779Sxy150489 }
9375779Sxy150489 
9385779Sxy150489 static int
9395779Sxy150489 igb_suspend(dev_info_t *devinfo)
9405779Sxy150489 {
9415779Sxy150489 	igb_t *igb;
9425779Sxy150489 
9435779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
9445779Sxy150489 	if (igb == NULL)
9455779Sxy150489 		return (DDI_FAILURE);
9465779Sxy150489 
9475779Sxy150489 	mutex_enter(&igb->gen_lock);
9485779Sxy150489 
9495779Sxy150489 	igb->igb_state |= IGB_SUSPENDED;
9505779Sxy150489 
9515779Sxy150489 	igb_stop(igb);
9525779Sxy150489 
9535779Sxy150489 	mutex_exit(&igb->gen_lock);
9545779Sxy150489 
9555779Sxy150489 	/*
9565779Sxy150489 	 * Disable and stop the watchdog timer
9575779Sxy150489 	 */
9585779Sxy150489 	igb_disable_watchdog_timer(igb);
9595779Sxy150489 
9605779Sxy150489 	return (DDI_SUCCESS);
9615779Sxy150489 }
9625779Sxy150489 
9635779Sxy150489 /*
9645779Sxy150489  * igb_init - Initialize the device
9655779Sxy150489  */
9665779Sxy150489 static int
9675779Sxy150489 igb_init(igb_t *igb)
9685779Sxy150489 {
9695779Sxy150489 	struct e1000_hw *hw = &igb->hw;
9705779Sxy150489 	uint32_t pba;
9715779Sxy150489 	uint32_t high_water;
9725779Sxy150489 
9735779Sxy150489 	mutex_enter(&igb->gen_lock);
9745779Sxy150489 
9755779Sxy150489 	/*
9765779Sxy150489 	 * Reset chipset to put the hardware in a known state
9775779Sxy150489 	 * before we try to do anything with the eeprom
9785779Sxy150489 	 */
9796624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
9806624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
9816624Sgl147354 		goto init_fail;
9826624Sgl147354 	}
9835779Sxy150489 
9845779Sxy150489 	/*
9855779Sxy150489 	 * NVM validation
9865779Sxy150489 	 */
9875779Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
9885779Sxy150489 		/*
9895779Sxy150489 		 * Some PCI-E parts fail the first check due to
9905779Sxy150489 		 * the link being in sleep state.  Call it again,
9915779Sxy150489 		 * if it fails a second time its a real issue.
9925779Sxy150489 		 */
9935779Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
9945779Sxy150489 			igb_error(igb,
9955779Sxy150489 			    "Invalid NVM checksum. Please contact "
9965779Sxy150489 			    "the vendor to update the NVM.");
9976624Sgl147354 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
9985779Sxy150489 			goto init_fail;
9995779Sxy150489 		}
10005779Sxy150489 	}
10015779Sxy150489 
10025779Sxy150489 	/*
10035779Sxy150489 	 * Set the FIFO size
10045779Sxy150489 	 */
10055779Sxy150489 	pba = E1000_PBA_32K;	/* 32K for Rx, 16K for Tx */
10065779Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
10075779Sxy150489 
10085779Sxy150489 	/*
10095779Sxy150489 	 * Setup flow control
10105779Sxy150489 	 *
10115779Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
10125779Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
10135779Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
10145779Sxy150489 	 * file.
10155779Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
10165779Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
10175779Sxy150489 	 * high-water mark.
10185779Sxy150489 	 * The high water mark must be low enough to fit one full frame above
10195779Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
10205779Sxy150489 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
10215779Sxy150489 	 * frame.
10225779Sxy150489 	 */
10235779Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
10245779Sxy150489 	    ((pba << 10) - igb->max_frame_size));
10255779Sxy150489 
10265779Sxy150489 	hw->fc.high_water = high_water & 0xFFF8;
10275779Sxy150489 	hw->fc.low_water = hw->fc.high_water - 8;
10285779Sxy150489 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
10295779Sxy150489 	hw->fc.send_xon = B_TRUE;
10305779Sxy150489 
10315779Sxy150489 	/*
10325779Sxy150489 	 * Reset the chipset hardware the second time to validate
10335779Sxy150489 	 * the PBA setting.
10345779Sxy150489 	 */
10356624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
10366624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
10376624Sgl147354 		goto init_fail;
10386624Sgl147354 	}
10395779Sxy150489 
10405779Sxy150489 	/*
10415779Sxy150489 	 * Don't wait for auto-negotiation to complete
10425779Sxy150489 	 */
10435779Sxy150489 	hw->phy.autoneg_wait_to_complete = B_FALSE;
10445779Sxy150489 
10455779Sxy150489 	/*
10465779Sxy150489 	 * Copper options
10475779Sxy150489 	 */
10485779Sxy150489 	if (hw->phy.media_type == e1000_media_type_copper) {
10495779Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
10505779Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
10515779Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
10525779Sxy150489 	}
10535779Sxy150489 
10545779Sxy150489 	/*
10555779Sxy150489 	 * Initialize link settings
10565779Sxy150489 	 */
10575779Sxy150489 	(void) igb_setup_link(igb, B_FALSE);
10585779Sxy150489 
10595779Sxy150489 	/*
10605779Sxy150489 	 * Initialize the chipset hardware
10615779Sxy150489 	 */
10625779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
10636624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
10646624Sgl147354 		goto init_fail;
10656624Sgl147354 	}
10666624Sgl147354 
10676624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
10686624Sgl147354 		goto init_fail;
10696624Sgl147354 	}
10706624Sgl147354 
10716624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
10725779Sxy150489 		goto init_fail;
10735779Sxy150489 	}
10745779Sxy150489 
10755779Sxy150489 	mutex_exit(&igb->gen_lock);
10765779Sxy150489 	return (IGB_SUCCESS);
10775779Sxy150489 
10785779Sxy150489 init_fail:
10795779Sxy150489 	/*
10805779Sxy150489 	 * Reset PHY if possible
10815779Sxy150489 	 */
10825779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
10835779Sxy150489 		(void) e1000_phy_hw_reset(hw);
10845779Sxy150489 
10855779Sxy150489 	mutex_exit(&igb->gen_lock);
10866624Sgl147354 
10876624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
10886624Sgl147354 
10895779Sxy150489 	return (IGB_FAILURE);
10905779Sxy150489 }
10915779Sxy150489 
10925779Sxy150489 /*
10935779Sxy150489  * igb_init_rings - Allocate DMA resources for all rx/tx rings and
10945779Sxy150489  * initialize relevant hardware settings.
10955779Sxy150489  */
10965779Sxy150489 static int
10975779Sxy150489 igb_init_rings(igb_t *igb)
10985779Sxy150489 {
10995779Sxy150489 	int i;
11005779Sxy150489 
11015779Sxy150489 	/*
11025779Sxy150489 	 * Allocate buffers for all the rx/tx rings
11035779Sxy150489 	 */
11045779Sxy150489 	if (igb_alloc_dma(igb) != IGB_SUCCESS)
11055779Sxy150489 		return (IGB_FAILURE);
11065779Sxy150489 
11075779Sxy150489 	/*
11085779Sxy150489 	 * Setup the rx/tx rings
11095779Sxy150489 	 */
11105779Sxy150489 	mutex_enter(&igb->gen_lock);
11115779Sxy150489 
11125779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
11135779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
11145779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
11155779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
11165779Sxy150489 
11175779Sxy150489 	igb_setup_rings(igb);
11185779Sxy150489 
11195779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
11205779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
11215779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
11225779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
11235779Sxy150489 
11245779Sxy150489 	mutex_exit(&igb->gen_lock);
11255779Sxy150489 
11265779Sxy150489 	return (IGB_SUCCESS);
11275779Sxy150489 }
11285779Sxy150489 
11295779Sxy150489 /*
11305779Sxy150489  * igb_fini_rings - Release DMA resources of all rx/tx rings
11315779Sxy150489  */
11325779Sxy150489 static void
11335779Sxy150489 igb_fini_rings(igb_t *igb)
11345779Sxy150489 {
11355779Sxy150489 	/*
11365779Sxy150489 	 * Release the DMA/memory resources of rx/tx rings
11375779Sxy150489 	 */
11385779Sxy150489 	igb_free_dma(igb);
11395779Sxy150489 }
11405779Sxy150489 
11415779Sxy150489 /*
11425779Sxy150489  * igb_chip_start - Initialize and start the chipset hardware
11435779Sxy150489  */
11445779Sxy150489 static int
11455779Sxy150489 igb_chip_start(igb_t *igb)
11465779Sxy150489 {
11475779Sxy150489 	struct e1000_hw *hw = &igb->hw;
11485779Sxy150489 	int i;
11495779Sxy150489 
11505779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
11515779Sxy150489 
11525779Sxy150489 	/*
11535779Sxy150489 	 * Get the mac address
11545779Sxy150489 	 * This function should handle SPARC case correctly.
11555779Sxy150489 	 */
11565779Sxy150489 	if (!igb_find_mac_address(igb)) {
11575779Sxy150489 		igb_error(igb, "Failed to get the mac address");
11585779Sxy150489 		return (IGB_FAILURE);
11595779Sxy150489 	}
11605779Sxy150489 
11615779Sxy150489 	/* Validate mac address */
11625779Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
11635779Sxy150489 		igb_error(igb, "Invalid mac address");
11645779Sxy150489 		return (IGB_FAILURE);
11655779Sxy150489 	}
11665779Sxy150489 
11675779Sxy150489 	/* Disable wakeup control by default */
11685779Sxy150489 	E1000_WRITE_REG(hw, E1000_WUC, 0);
11695779Sxy150489 
11705779Sxy150489 	/*
11715779Sxy150489 	 * Configure/Initialize hardware
11725779Sxy150489 	 */
11735779Sxy150489 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
11745779Sxy150489 		igb_error(igb, "Failed to initialize hardware");
11755779Sxy150489 		return (IGB_FAILURE);
11765779Sxy150489 	}
11775779Sxy150489 
11785779Sxy150489 	/*
11795779Sxy150489 	 * Make sure driver has control
11805779Sxy150489 	 */
11815779Sxy150489 	igb_get_driver_control(hw);
11825779Sxy150489 
11835779Sxy150489 	/*
11845779Sxy150489 	 * Setup MSI-X interrupts
11855779Sxy150489 	 */
11865779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
11875779Sxy150489 		igb_setup_adapter_msix(igb);
11885779Sxy150489 
11895779Sxy150489 	/*
11905779Sxy150489 	 * Initialize unicast addresses.
11915779Sxy150489 	 */
11925779Sxy150489 	igb_init_unicst(igb);
11935779Sxy150489 
11945779Sxy150489 	/*
11955779Sxy150489 	 * Setup and initialize the mctable structures.
11965779Sxy150489 	 */
11975779Sxy150489 	igb_setup_multicst(igb);
11985779Sxy150489 
11995779Sxy150489 	/*
12005779Sxy150489 	 * Set interrupt throttling rate
12015779Sxy150489 	 */
12025779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++)
12035779Sxy150489 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
12045779Sxy150489 
12055779Sxy150489 	/* Enable PCI-E master */
12065779Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
12075779Sxy150489 		e1000_enable_pciex_master(hw);
12085779Sxy150489 	}
12095779Sxy150489 
12105779Sxy150489 	/*
12115779Sxy150489 	 * Save the state of the phy
12125779Sxy150489 	 */
12135779Sxy150489 	igb_get_phy_state(igb);
12145779Sxy150489 
12155779Sxy150489 	return (IGB_SUCCESS);
12165779Sxy150489 }
12175779Sxy150489 
12185779Sxy150489 /*
12195779Sxy150489  * igb_chip_stop - Stop the chipset hardware
12205779Sxy150489  */
12215779Sxy150489 static void
12225779Sxy150489 igb_chip_stop(igb_t *igb)
12235779Sxy150489 {
12245779Sxy150489 	struct e1000_hw *hw = &igb->hw;
12255779Sxy150489 
12265779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
12275779Sxy150489 
12285779Sxy150489 	/* Tell firmware driver is no longer in control */
12295779Sxy150489 	igb_release_driver_control(hw);
12305779Sxy150489 
12315779Sxy150489 	/*
12325779Sxy150489 	 * Reset the chipset
12335779Sxy150489 	 */
12346624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
12356624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
12366624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
12376624Sgl147354 	}
12385779Sxy150489 
12395779Sxy150489 	/*
12405779Sxy150489 	 * Reset PHY if possible
12415779Sxy150489 	 */
12425779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
12435779Sxy150489 		(void) e1000_phy_hw_reset(hw);
12445779Sxy150489 }
12455779Sxy150489 
12465779Sxy150489 /*
12475779Sxy150489  * igb_reset - Reset the chipset and restart the driver.
12485779Sxy150489  *
12495779Sxy150489  * It involves stopping and re-starting the chipset,
12505779Sxy150489  * and re-configuring the rx/tx rings.
12515779Sxy150489  */
12525779Sxy150489 static int
12535779Sxy150489 igb_reset(igb_t *igb)
12545779Sxy150489 {
12555779Sxy150489 	int i;
12565779Sxy150489 
12575779Sxy150489 	mutex_enter(&igb->gen_lock);
12585779Sxy150489 
12595779Sxy150489 	ASSERT(igb->igb_state & IGB_STARTED);
12605779Sxy150489 
12615779Sxy150489 	/*
12625779Sxy150489 	 * Disable the adapter interrupts to stop any rx/tx activities
12635779Sxy150489 	 * before draining pending data and resetting hardware.
12645779Sxy150489 	 */
12655779Sxy150489 	igb_disable_adapter_interrupts(igb);
12665779Sxy150489 
12675779Sxy150489 	/*
12685779Sxy150489 	 * Drain the pending transmit packets
12695779Sxy150489 	 */
12705779Sxy150489 	(void) igb_tx_drain(igb);
12715779Sxy150489 
12725779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
12735779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
12745779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
12755779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
12765779Sxy150489 
12775779Sxy150489 	/*
12785779Sxy150489 	 * Stop the chipset hardware
12795779Sxy150489 	 */
12805779Sxy150489 	igb_chip_stop(igb);
12815779Sxy150489 
12825779Sxy150489 	/*
12835779Sxy150489 	 * Clean the pending tx data/resources
12845779Sxy150489 	 */
12855779Sxy150489 	igb_tx_clean(igb);
12865779Sxy150489 
12875779Sxy150489 	/*
12885779Sxy150489 	 * Start the chipset hardware
12895779Sxy150489 	 */
12905779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
12916624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
12925779Sxy150489 		goto reset_failure;
12935779Sxy150489 	}
12945779Sxy150489 
12955779Sxy150489 	/*
12965779Sxy150489 	 * Setup the rx/tx rings
12975779Sxy150489 	 */
12985779Sxy150489 	igb_setup_rings(igb);
12995779Sxy150489 
13005779Sxy150489 	/*
13015779Sxy150489 	 * Enable adapter interrupts
13025779Sxy150489 	 * The interrupts must be enabled after the driver state is START
13035779Sxy150489 	 */
13045779Sxy150489 	igb_enable_adapter_interrupts(igb);
13055779Sxy150489 
13066624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
13076624Sgl147354 		goto reset_failure;
13086624Sgl147354 
13096624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
13106624Sgl147354 		goto reset_failure;
13116624Sgl147354 
13125779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
13135779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
13145779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
13155779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
13165779Sxy150489 
13175779Sxy150489 	mutex_exit(&igb->gen_lock);
13185779Sxy150489 
13195779Sxy150489 	return (IGB_SUCCESS);
13205779Sxy150489 
13215779Sxy150489 reset_failure:
13225779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
13235779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
13245779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
13255779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
13265779Sxy150489 
13275779Sxy150489 	mutex_exit(&igb->gen_lock);
13285779Sxy150489 
13296624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
13306624Sgl147354 
13315779Sxy150489 	return (IGB_FAILURE);
13325779Sxy150489 }
13335779Sxy150489 
13345779Sxy150489 /*
13355779Sxy150489  * igb_tx_clean - Clean the pending transmit packets and DMA resources
13365779Sxy150489  */
13375779Sxy150489 static void
13385779Sxy150489 igb_tx_clean(igb_t *igb)
13395779Sxy150489 {
13405779Sxy150489 	igb_tx_ring_t *tx_ring;
13415779Sxy150489 	tx_control_block_t *tcb;
13425779Sxy150489 	link_list_t pending_list;
13435779Sxy150489 	uint32_t desc_num;
13445779Sxy150489 	int i, j;
13455779Sxy150489 
13465779Sxy150489 	LINK_LIST_INIT(&pending_list);
13475779Sxy150489 
13485779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
13495779Sxy150489 		tx_ring = &igb->tx_rings[i];
13505779Sxy150489 
13515779Sxy150489 		mutex_enter(&tx_ring->recycle_lock);
13525779Sxy150489 
13535779Sxy150489 		/*
13545779Sxy150489 		 * Clean the pending tx data - the pending packets in the
13555779Sxy150489 		 * work_list that have no chances to be transmitted again.
13565779Sxy150489 		 *
13575779Sxy150489 		 * We must ensure the chipset is stopped or the link is down
13585779Sxy150489 		 * before cleaning the transmit packets.
13595779Sxy150489 		 */
13605779Sxy150489 		desc_num = 0;
13615779Sxy150489 		for (j = 0; j < tx_ring->ring_size; j++) {
13625779Sxy150489 			tcb = tx_ring->work_list[j];
13635779Sxy150489 			if (tcb != NULL) {
13645779Sxy150489 				desc_num += tcb->desc_num;
13655779Sxy150489 
13665779Sxy150489 				tx_ring->work_list[j] = NULL;
13675779Sxy150489 
13685779Sxy150489 				igb_free_tcb(tcb);
13695779Sxy150489 
13705779Sxy150489 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
13715779Sxy150489 			}
13725779Sxy150489 		}
13735779Sxy150489 
13745779Sxy150489 		if (desc_num > 0) {
13755779Sxy150489 			atomic_add_32(&tx_ring->tbd_free, desc_num);
13765779Sxy150489 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
13775779Sxy150489 
13785779Sxy150489 			/*
1379*7072Sxy150489 			 * Reset the head and tail pointers of the tbd ring;
1380*7072Sxy150489 			 * Reset the head write-back if it is enabled.
13815779Sxy150489 			 */
13825779Sxy150489 			tx_ring->tbd_head = 0;
13835779Sxy150489 			tx_ring->tbd_tail = 0;
1384*7072Sxy150489 			if (igb->tx_head_wb_enable)
1385*7072Sxy150489 				*tx_ring->tbd_head_wb = 0;
13865779Sxy150489 
13875779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
13885779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
13895779Sxy150489 		}
13905779Sxy150489 
13915779Sxy150489 		mutex_exit(&tx_ring->recycle_lock);
13925779Sxy150489 
13935779Sxy150489 		/*
13945779Sxy150489 		 * Add the tx control blocks in the pending list to
13955779Sxy150489 		 * the free list.
13965779Sxy150489 		 */
13975779Sxy150489 		igb_put_free_list(tx_ring, &pending_list);
13985779Sxy150489 	}
13995779Sxy150489 }
14005779Sxy150489 
14015779Sxy150489 /*
14025779Sxy150489  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
14035779Sxy150489  */
14045779Sxy150489 static boolean_t
14055779Sxy150489 igb_tx_drain(igb_t *igb)
14065779Sxy150489 {
14075779Sxy150489 	igb_tx_ring_t *tx_ring;
14085779Sxy150489 	boolean_t done;
14095779Sxy150489 	int i, j;
14105779Sxy150489 
14115779Sxy150489 	/*
14125779Sxy150489 	 * Wait for a specific time to allow pending tx packets
14135779Sxy150489 	 * to be transmitted.
14145779Sxy150489 	 *
14155779Sxy150489 	 * Check the counter tbd_free to see if transmission is done.
14165779Sxy150489 	 * No lock protection is needed here.
14175779Sxy150489 	 *
14185779Sxy150489 	 * Return B_TRUE if all pending packets have been transmitted;
14195779Sxy150489 	 * Otherwise return B_FALSE;
14205779Sxy150489 	 */
14215779Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
14225779Sxy150489 
14235779Sxy150489 		done = B_TRUE;
14245779Sxy150489 		for (j = 0; j < igb->num_tx_rings; j++) {
14255779Sxy150489 			tx_ring = &igb->tx_rings[j];
14265779Sxy150489 			done = done &&
14275779Sxy150489 			    (tx_ring->tbd_free == tx_ring->ring_size);
14285779Sxy150489 		}
14295779Sxy150489 
14305779Sxy150489 		if (done)
14315779Sxy150489 			break;
14325779Sxy150489 
14335779Sxy150489 		msec_delay(1);
14345779Sxy150489 	}
14355779Sxy150489 
14365779Sxy150489 	return (done);
14375779Sxy150489 }
14385779Sxy150489 
14395779Sxy150489 /*
14405779Sxy150489  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
14415779Sxy150489  */
14425779Sxy150489 static boolean_t
14435779Sxy150489 igb_rx_drain(igb_t *igb)
14445779Sxy150489 {
14455779Sxy150489 	igb_rx_ring_t *rx_ring;
14465779Sxy150489 	boolean_t done;
14475779Sxy150489 	int i, j;
14485779Sxy150489 
14495779Sxy150489 	/*
14505779Sxy150489 	 * Polling the rx free list to check if those rx buffers held by
14515779Sxy150489 	 * the upper layer are released.
14525779Sxy150489 	 *
14535779Sxy150489 	 * Check the counter rcb_free to see if all pending buffers are
14545779Sxy150489 	 * released. No lock protection is needed here.
14555779Sxy150489 	 *
14565779Sxy150489 	 * Return B_TRUE if all pending buffers have been released;
14575779Sxy150489 	 * Otherwise return B_FALSE;
14585779Sxy150489 	 */
14595779Sxy150489 	for (i = 0; i < RX_DRAIN_TIME; i++) {
14605779Sxy150489 
14615779Sxy150489 		done = B_TRUE;
14625779Sxy150489 		for (j = 0; j < igb->num_rx_rings; j++) {
14635779Sxy150489 			rx_ring = &igb->rx_rings[j];
14645779Sxy150489 			done = done &&
14655779Sxy150489 			    (rx_ring->rcb_free == rx_ring->free_list_size);
14665779Sxy150489 		}
14675779Sxy150489 
14685779Sxy150489 		if (done)
14695779Sxy150489 			break;
14705779Sxy150489 
14715779Sxy150489 		msec_delay(1);
14725779Sxy150489 	}
14735779Sxy150489 
14745779Sxy150489 	return (done);
14755779Sxy150489 }
14765779Sxy150489 
14775779Sxy150489 /*
14785779Sxy150489  * igb_start - Start the driver/chipset
14795779Sxy150489  */
14805779Sxy150489 int
14815779Sxy150489 igb_start(igb_t *igb)
14825779Sxy150489 {
14835779Sxy150489 	int i;
14845779Sxy150489 
14855779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
14865779Sxy150489 
14875779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
14885779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
14895779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
14905779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
14915779Sxy150489 
14925779Sxy150489 	/*
14935779Sxy150489 	 * Start the chipset hardware
14945779Sxy150489 	 */
14955779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
14966624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
14975779Sxy150489 		goto start_failure;
14985779Sxy150489 	}
14995779Sxy150489 
15005779Sxy150489 	/*
15015779Sxy150489 	 * Setup the rx/tx rings
15025779Sxy150489 	 */
15035779Sxy150489 	igb_setup_rings(igb);
15045779Sxy150489 
15055779Sxy150489 	/*
15065779Sxy150489 	 * Enable adapter interrupts
15075779Sxy150489 	 * The interrupts must be enabled after the driver state is START
15085779Sxy150489 	 */
15095779Sxy150489 	igb_enable_adapter_interrupts(igb);
15105779Sxy150489 
15116624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
15126624Sgl147354 		goto start_failure;
15136624Sgl147354 
15146624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
15156624Sgl147354 		goto start_failure;
15166624Sgl147354 
15175779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
15185779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
15195779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
15205779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
15215779Sxy150489 
15225779Sxy150489 	return (IGB_SUCCESS);
15235779Sxy150489 
15245779Sxy150489 start_failure:
15255779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
15265779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
15275779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
15285779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
15295779Sxy150489 
15306624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
15316624Sgl147354 
15325779Sxy150489 	return (IGB_FAILURE);
15335779Sxy150489 }
15345779Sxy150489 
15355779Sxy150489 /*
15365779Sxy150489  * igb_stop - Stop the driver/chipset
15375779Sxy150489  */
15385779Sxy150489 void
15395779Sxy150489 igb_stop(igb_t *igb)
15405779Sxy150489 {
15415779Sxy150489 	int i;
15425779Sxy150489 
15435779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
15445779Sxy150489 
15455779Sxy150489 	/*
15465779Sxy150489 	 * Disable the adapter interrupts
15475779Sxy150489 	 */
15485779Sxy150489 	igb_disable_adapter_interrupts(igb);
15495779Sxy150489 
15505779Sxy150489 	/*
15515779Sxy150489 	 * Drain the pending tx packets
15525779Sxy150489 	 */
15535779Sxy150489 	(void) igb_tx_drain(igb);
15545779Sxy150489 
15555779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
15565779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
15575779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
15585779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
15595779Sxy150489 
15605779Sxy150489 	/*
15615779Sxy150489 	 * Stop the chipset hardware
15625779Sxy150489 	 */
15635779Sxy150489 	igb_chip_stop(igb);
15645779Sxy150489 
15655779Sxy150489 	/*
15665779Sxy150489 	 * Clean the pending tx data/resources
15675779Sxy150489 	 */
15685779Sxy150489 	igb_tx_clean(igb);
15695779Sxy150489 
15705779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
15715779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
15725779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
15735779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
15746624Sgl147354 
15756624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
15766624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
15775779Sxy150489 }
15785779Sxy150489 
15795779Sxy150489 /*
15805779Sxy150489  * igb_alloc_rings - Allocate memory space for rx/tx rings
15815779Sxy150489  */
15825779Sxy150489 static int
15835779Sxy150489 igb_alloc_rings(igb_t *igb)
15845779Sxy150489 {
15855779Sxy150489 	/*
15865779Sxy150489 	 * Allocate memory space for rx rings
15875779Sxy150489 	 */
15885779Sxy150489 	igb->rx_rings = kmem_zalloc(
15895779Sxy150489 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
15905779Sxy150489 	    KM_NOSLEEP);
15915779Sxy150489 
15925779Sxy150489 	if (igb->rx_rings == NULL) {
15935779Sxy150489 		return (IGB_FAILURE);
15945779Sxy150489 	}
15955779Sxy150489 
15965779Sxy150489 	/*
15975779Sxy150489 	 * Allocate memory space for tx rings
15985779Sxy150489 	 */
15995779Sxy150489 	igb->tx_rings = kmem_zalloc(
16005779Sxy150489 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
16015779Sxy150489 	    KM_NOSLEEP);
16025779Sxy150489 
16035779Sxy150489 	if (igb->tx_rings == NULL) {
16045779Sxy150489 		kmem_free(igb->rx_rings,
16055779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
16065779Sxy150489 		igb->rx_rings = NULL;
16075779Sxy150489 		return (IGB_FAILURE);
16085779Sxy150489 	}
16095779Sxy150489 
16105779Sxy150489 	return (IGB_SUCCESS);
16115779Sxy150489 }
16125779Sxy150489 
16135779Sxy150489 /*
16145779Sxy150489  * igb_free_rings - Free the memory space of rx/tx rings.
16155779Sxy150489  */
16165779Sxy150489 static void
16175779Sxy150489 igb_free_rings(igb_t *igb)
16185779Sxy150489 {
16195779Sxy150489 	if (igb->rx_rings != NULL) {
16205779Sxy150489 		kmem_free(igb->rx_rings,
16215779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
16225779Sxy150489 		igb->rx_rings = NULL;
16235779Sxy150489 	}
16245779Sxy150489 
16255779Sxy150489 	if (igb->tx_rings != NULL) {
16265779Sxy150489 		kmem_free(igb->tx_rings,
16275779Sxy150489 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
16285779Sxy150489 		igb->tx_rings = NULL;
16295779Sxy150489 	}
16305779Sxy150489 }
16315779Sxy150489 
16325779Sxy150489 /*
16335779Sxy150489  * igb_setup_rings - Setup rx/tx rings
16345779Sxy150489  */
16355779Sxy150489 static void
16365779Sxy150489 igb_setup_rings(igb_t *igb)
16375779Sxy150489 {
16385779Sxy150489 	/*
16395779Sxy150489 	 * Setup the rx/tx rings, including the following:
16405779Sxy150489 	 *
16415779Sxy150489 	 * 1. Setup the descriptor ring and the control block buffers;
16425779Sxy150489 	 * 2. Initialize necessary registers for receive/transmit;
16435779Sxy150489 	 * 3. Initialize software pointers/parameters for receive/transmit;
16445779Sxy150489 	 */
16455779Sxy150489 	igb_setup_rx(igb);
16465779Sxy150489 
16475779Sxy150489 	igb_setup_tx(igb);
16486624Sgl147354 
16496624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
16506624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
16515779Sxy150489 }
16525779Sxy150489 
16535779Sxy150489 static void
16545779Sxy150489 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
16555779Sxy150489 {
16565779Sxy150489 	igb_t *igb = rx_ring->igb;
16575779Sxy150489 	struct e1000_hw *hw = &igb->hw;
16585779Sxy150489 	rx_control_block_t *rcb;
16595779Sxy150489 	union e1000_adv_rx_desc	*rbd;
16605779Sxy150489 	uint32_t size;
16615779Sxy150489 	uint32_t buf_low;
16625779Sxy150489 	uint32_t buf_high;
16635779Sxy150489 	uint32_t reg_val;
16645779Sxy150489 	int i;
16655779Sxy150489 
16665779Sxy150489 	ASSERT(mutex_owned(&rx_ring->rx_lock));
16675779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16685779Sxy150489 
16695779Sxy150489 	for (i = 0; i < igb->rx_ring_size; i++) {
16705779Sxy150489 		rcb = rx_ring->work_list[i];
16715779Sxy150489 		rbd = &rx_ring->rbd_ring[i];
16725779Sxy150489 
16735779Sxy150489 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
16745779Sxy150489 		rbd->read.hdr_addr = NULL;
16755779Sxy150489 	}
16765779Sxy150489 
16775779Sxy150489 	/*
16785779Sxy150489 	 * Initialize the length register
16795779Sxy150489 	 */
16805779Sxy150489 	size = rx_ring->ring_size * sizeof (union e1000_adv_rx_desc);
16815779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
16825779Sxy150489 
16835779Sxy150489 	/*
16845779Sxy150489 	 * Initialize the base address registers
16855779Sxy150489 	 */
16865779Sxy150489 	buf_low = (uint32_t)rx_ring->rbd_area.dma_address;
16875779Sxy150489 	buf_high = (uint32_t)(rx_ring->rbd_area.dma_address >> 32);
16885779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
16895779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
16905779Sxy150489 
16915779Sxy150489 	/*
16925779Sxy150489 	 * Setup head & tail pointers
16935779Sxy150489 	 */
16945779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDT(rx_ring->index), rx_ring->ring_size - 1);
16955779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDH(rx_ring->index), 0);
16965779Sxy150489 
16975779Sxy150489 	rx_ring->rbd_next = 0;
16985779Sxy150489 
16995779Sxy150489 	/*
17005779Sxy150489 	 * Note: Considering the case that the chipset is being reset
17015779Sxy150489 	 * and there are still some buffers held by the upper layer,
17025779Sxy150489 	 * we should not reset the values of rcb_head, rcb_tail and
17035779Sxy150489 	 * rcb_free;
17045779Sxy150489 	 */
17055779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
17065779Sxy150489 		rx_ring->rcb_head = 0;
17075779Sxy150489 		rx_ring->rcb_tail = 0;
17085779Sxy150489 		rx_ring->rcb_free = rx_ring->free_list_size;
17095779Sxy150489 	}
17105779Sxy150489 
17115779Sxy150489 	/*
17125779Sxy150489 	 * Setup the Receive Descriptor Control Register (RXDCTL)
17135779Sxy150489 	 */
17145779Sxy150489 	reg_val = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
17155779Sxy150489 	reg_val |= E1000_RXDCTL_QUEUE_ENABLE;
17165779Sxy150489 	reg_val &= 0xFFF00000;
17175779Sxy150489 	reg_val |= 16;		/* pthresh */
17185779Sxy150489 	reg_val |= 8 << 8;	/* hthresh */
17195779Sxy150489 	reg_val |= 1 << 16;	/* wthresh */
17205779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), reg_val);
17215779Sxy150489 
17225779Sxy150489 	/*
17235779Sxy150489 	 * Setup the Split and Replication Receive Control Register.
17245779Sxy150489 	 * Set the rx buffer size and the advanced descriptor type.
17255779Sxy150489 	 */
17265779Sxy150489 	reg_val = (igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
17275779Sxy150489 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
17285779Sxy150489 
17295779Sxy150489 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index), reg_val);
17305779Sxy150489 }
17315779Sxy150489 
17325779Sxy150489 static void
17335779Sxy150489 igb_setup_rx(igb_t *igb)
17345779Sxy150489 {
17355779Sxy150489 	igb_rx_ring_t *rx_ring;
17365779Sxy150489 	struct e1000_hw *hw = &igb->hw;
17375779Sxy150489 	uint32_t reg_val;
17385779Sxy150489 	int i;
17395779Sxy150489 
17405779Sxy150489 	/*
17415779Sxy150489 	 * Setup the Receive Control Register (RCTL), and ENABLE the
17425779Sxy150489 	 * receiver. The initial configuration is to: Enable the receiver,
17435779Sxy150489 	 * accept broadcasts, discard bad packets (and long packets),
17445779Sxy150489 	 * disable VLAN filter checking, set the receive descriptor
17455779Sxy150489 	 * minimum threshold size to 1/2, and the receive buffer size to
17465779Sxy150489 	 * 2k.
17475779Sxy150489 	 */
17485779Sxy150489 	reg_val = E1000_RCTL_EN |	/* Enable Receive Unit */
17495779Sxy150489 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
17505779Sxy150489 	    E1000_RCTL_LPE |		/* Large Packet Enable bit */
17515779Sxy150489 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
17525779Sxy150489 	    E1000_RCTL_RDMTS_HALF |
17535779Sxy150489 	    E1000_RCTL_SECRC |		/* Strip Ethernet CRC */
17545779Sxy150489 	    E1000_RCTL_LBM_NO;		/* Loopback Mode = none */
17555779Sxy150489 
17565779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, reg_val);
17575779Sxy150489 
17585779Sxy150489 	/*
17595812Sxy150489 	 * igb_setup_rx_ring must be called after configuring RCTL
17605812Sxy150489 	 */
17615812Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
17625812Sxy150489 		rx_ring = &igb->rx_rings[i];
17635812Sxy150489 		igb_setup_rx_ring(rx_ring);
17645812Sxy150489 	}
17655812Sxy150489 
17665812Sxy150489 	/*
17675779Sxy150489 	 * Setup the Rx Long Packet Max Length register
17685779Sxy150489 	 */
17695779Sxy150489 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
17705779Sxy150489 
17715779Sxy150489 	/*
17725779Sxy150489 	 * Hardware checksum settings
17735779Sxy150489 	 */
17745779Sxy150489 	if (igb->rx_hcksum_enable) {
17755779Sxy150489 		reg_val =
17765779Sxy150489 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
17775779Sxy150489 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
17785779Sxy150489 
17795779Sxy150489 		E1000_WRITE_REG(hw, E1000_RXCSUM, reg_val);
17805779Sxy150489 	}
17815779Sxy150489 
17825779Sxy150489 	/*
17835779Sxy150489 	 * Setup RSS for multiple receive queues
17845779Sxy150489 	 */
17855779Sxy150489 	if (igb->num_rx_rings > 1)
17865779Sxy150489 		igb_setup_rss(igb);
17875779Sxy150489 }
17885779Sxy150489 
17895779Sxy150489 static void
17905779Sxy150489 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
17915779Sxy150489 {
17925779Sxy150489 	igb_t *igb = tx_ring->igb;
17935779Sxy150489 	struct e1000_hw *hw = &igb->hw;
17945779Sxy150489 	uint32_t size;
17955779Sxy150489 	uint32_t buf_low;
17965779Sxy150489 	uint32_t buf_high;
17975779Sxy150489 	uint32_t reg_val;
17985779Sxy150489 
17995779Sxy150489 	ASSERT(mutex_owned(&tx_ring->tx_lock));
18005779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
18015779Sxy150489 
18025779Sxy150489 	/*
18035779Sxy150489 	 * Initialize the length register
18045779Sxy150489 	 */
18055779Sxy150489 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
18065779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
18075779Sxy150489 
18085779Sxy150489 	/*
18095779Sxy150489 	 * Initialize the base address registers
18105779Sxy150489 	 */
18115779Sxy150489 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
18125779Sxy150489 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
18135779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
18145779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
18155779Sxy150489 
18165779Sxy150489 	/*
18175779Sxy150489 	 * Setup head & tail pointers
18185779Sxy150489 	 */
18195779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
18205779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
18215779Sxy150489 
18225779Sxy150489 	/*
18235779Sxy150489 	 * Setup head write-back
18245779Sxy150489 	 */
18255779Sxy150489 	if (igb->tx_head_wb_enable) {
18265779Sxy150489 		/*
18275779Sxy150489 		 * The memory of the head write-back is allocated using
18285779Sxy150489 		 * the extra tbd beyond the tail of the tbd ring.
18295779Sxy150489 		 */
18305779Sxy150489 		tx_ring->tbd_head_wb = (uint32_t *)
18315779Sxy150489 		    ((uintptr_t)tx_ring->tbd_area.address + size);
1832*7072Sxy150489 		*tx_ring->tbd_head_wb = 0;
18335779Sxy150489 
18345779Sxy150489 		buf_low = (uint32_t)
18355779Sxy150489 		    (tx_ring->tbd_area.dma_address + size);
18365779Sxy150489 		buf_high = (uint32_t)
18375779Sxy150489 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
18385779Sxy150489 
18395779Sxy150489 		/* Set the head write-back enable bit */
18405779Sxy150489 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
18415779Sxy150489 
18425779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
18435779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
18445779Sxy150489 
18455779Sxy150489 		/*
18465779Sxy150489 		 * Turn off relaxed ordering for head write back or it will
18475779Sxy150489 		 * cause problems with the tx recycling
18485779Sxy150489 		 */
18495779Sxy150489 		reg_val = E1000_READ_REG(hw,
18505779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index));
18515779Sxy150489 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
18525779Sxy150489 		E1000_WRITE_REG(hw,
18535779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
18545779Sxy150489 	} else {
18555779Sxy150489 		tx_ring->tbd_head_wb = NULL;
18565779Sxy150489 	}
18575779Sxy150489 
18585779Sxy150489 	tx_ring->tbd_head = 0;
18595779Sxy150489 	tx_ring->tbd_tail = 0;
18605779Sxy150489 	tx_ring->tbd_free = tx_ring->ring_size;
18615779Sxy150489 
18625779Sxy150489 	/*
18635779Sxy150489 	 * Note: Considering the case that the chipset is being reset,
18645779Sxy150489 	 * and there are still some buffers held by the upper layer,
18655779Sxy150489 	 * we should not reset the values of tcb_head, tcb_tail.
18665779Sxy150489 	 */
18675779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
18685779Sxy150489 		tx_ring->tcb_head = 0;
18695779Sxy150489 		tx_ring->tcb_tail = 0;
18705779Sxy150489 		tx_ring->tcb_free = tx_ring->free_list_size;
18715779Sxy150489 	} else {
18725779Sxy150489 		ASSERT(tx_ring->tcb_free == tx_ring->free_list_size);
18735779Sxy150489 	}
18745779Sxy150489 
18755779Sxy150489 	/*
18765779Sxy150489 	 * Initialize hardware checksum offload settings
18775779Sxy150489 	 */
18785779Sxy150489 	tx_ring->hcksum_context.hcksum_flags = 0;
18795779Sxy150489 	tx_ring->hcksum_context.ip_hdr_len = 0;
18805779Sxy150489 	tx_ring->hcksum_context.mac_hdr_len = 0;
18815779Sxy150489 	tx_ring->hcksum_context.l4_proto = 0;
18825779Sxy150489 }
18835779Sxy150489 
18845779Sxy150489 static void
18855779Sxy150489 igb_setup_tx(igb_t *igb)
18865779Sxy150489 {
18875779Sxy150489 	igb_tx_ring_t *tx_ring;
18885779Sxy150489 	struct e1000_hw *hw = &igb->hw;
18895779Sxy150489 	uint32_t reg_val;
18905779Sxy150489 	int i;
18915779Sxy150489 
18925779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
18935779Sxy150489 		tx_ring = &igb->tx_rings[i];
18945779Sxy150489 		igb_setup_tx_ring(tx_ring);
18955779Sxy150489 	}
18965779Sxy150489 
18975779Sxy150489 	/*
18985779Sxy150489 	 * Setup the Transmit Control Register (TCTL)
18995779Sxy150489 	 */
19005779Sxy150489 	reg_val = E1000_TCTL_PSP | E1000_TCTL_EN |
19015779Sxy150489 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT) |
19025779Sxy150489 	    (E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT) |
19035779Sxy150489 	    E1000_TCTL_RTLC;
19045779Sxy150489 
19055779Sxy150489 	/* Enable the MULR bit */
19065779Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express)
19075779Sxy150489 		reg_val |= E1000_TCTL_MULR;
19085779Sxy150489 
19095779Sxy150489 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
19105779Sxy150489 
19115779Sxy150489 	/*
19125779Sxy150489 	 * Set the default values for the Tx Inter Packet Gap timer
19135779Sxy150489 	 */
19145779Sxy150489 	if (hw->phy.media_type == e1000_media_type_fiber)
19155779Sxy150489 		reg_val = DEFAULT_82543_TIPG_IPGT_FIBER;
19165779Sxy150489 	else
19175779Sxy150489 		reg_val = DEFAULT_82543_TIPG_IPGT_COPPER;
19185779Sxy150489 	reg_val |=
19195779Sxy150489 	    DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
19205779Sxy150489 	reg_val |=
19215779Sxy150489 	    DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
19225779Sxy150489 
19235779Sxy150489 	E1000_WRITE_REG(hw, E1000_TIPG, reg_val);
19245779Sxy150489 }
19255779Sxy150489 
19265779Sxy150489 /*
19275779Sxy150489  * igb_setup_rss - Setup receive-side scaling feature
19285779Sxy150489  */
19295779Sxy150489 static void
19305779Sxy150489 igb_setup_rss(igb_t *igb)
19315779Sxy150489 {
19325779Sxy150489 	struct e1000_hw *hw = &igb->hw;
19335779Sxy150489 	uint32_t i, mrqc, rxcsum;
19345779Sxy150489 	int shift;
19355779Sxy150489 	uint32_t random;
19365779Sxy150489 	union e1000_reta {
19375779Sxy150489 		uint32_t	dword;
19385779Sxy150489 		uint8_t		bytes[4];
19395779Sxy150489 	} reta;
19405779Sxy150489 
19415779Sxy150489 	/* Setup the Redirection Table */
19425779Sxy150489 	shift = 6;
19435779Sxy150489 	for (i = 0; i < (32 * 4); i++) {
19445779Sxy150489 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
19455779Sxy150489 		if ((i & 3) == 3) {
19465779Sxy150489 			E1000_WRITE_REG(hw,
19475779Sxy150489 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
19485779Sxy150489 		}
19495779Sxy150489 	}
19505779Sxy150489 
19515779Sxy150489 	/* Fill out hash function seeds */
19525779Sxy150489 	for (i = 0; i < 10; i++) {
19535779Sxy150489 		(void) random_get_pseudo_bytes((uint8_t *)&random,
19545779Sxy150489 		    sizeof (uint32_t));
19555779Sxy150489 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
19565779Sxy150489 	}
19575779Sxy150489 
19585779Sxy150489 	/* Setup the Multiple Receive Queue Control register */
19595779Sxy150489 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
19605779Sxy150489 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
19615779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
19625779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6 |
19635779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
19645779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
19655779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
19665779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
19675779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
19685779Sxy150489 
19695779Sxy150489 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
19705779Sxy150489 
19715779Sxy150489 	/*
19725779Sxy150489 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
19735779Sxy150489 	 *
19745779Sxy150489 	 * The Packet Checksum is not ethernet CRC. It is another kind of
19755779Sxy150489 	 * checksum offloading provided by the 82575 chipset besides the IP
19765779Sxy150489 	 * header checksum offloading and the TCP/UDP checksum offloading.
19775779Sxy150489 	 * The Packet Checksum is by default computed over the entire packet
19785779Sxy150489 	 * from the first byte of the DA through the last byte of the CRC,
19795779Sxy150489 	 * including the Ethernet and IP headers.
19805779Sxy150489 	 *
19815779Sxy150489 	 * It is a hardware limitation that Packet Checksum is mutually
19825779Sxy150489 	 * exclusive with RSS.
19835779Sxy150489 	 */
19845779Sxy150489 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
19855779Sxy150489 	rxcsum |= E1000_RXCSUM_PCSD;
19865779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
19875779Sxy150489 }
19885779Sxy150489 
19895779Sxy150489 /*
19905779Sxy150489  * igb_init_unicst - Initialize the unicast addresses
19915779Sxy150489  */
19925779Sxy150489 static void
19935779Sxy150489 igb_init_unicst(igb_t *igb)
19945779Sxy150489 {
19955779Sxy150489 	struct e1000_hw *hw = &igb->hw;
19965779Sxy150489 	int slot;
19975779Sxy150489 
19985779Sxy150489 	/*
19995779Sxy150489 	 * Here we should consider two situations:
20005779Sxy150489 	 *
20015779Sxy150489 	 * 1. Chipset is initialized the first time
20025779Sxy150489 	 *    Initialize the multiple unicast addresses, and
20035779Sxy150489 	 *    save the default mac address.
20045779Sxy150489 	 *
20055779Sxy150489 	 * 2. Chipset is reset
20065779Sxy150489 	 *    Recover the multiple unicast addresses from the
20075779Sxy150489 	 *    software data structure to the RAR registers.
20085779Sxy150489 	 */
20095779Sxy150489 	if (!igb->unicst_init) {
20105779Sxy150489 		/* Initialize the multiple unicast addresses */
20115779Sxy150489 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
20125779Sxy150489 
20135779Sxy150489 		igb->unicst_avail = igb->unicst_total - 1;
20145779Sxy150489 
20155779Sxy150489 		/* Store the default mac address */
20165779Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20175779Sxy150489 
20185779Sxy150489 		bcopy(hw->mac.addr, igb->unicst_addr[0].mac.addr,
20195779Sxy150489 		    ETHERADDRL);
20205779Sxy150489 		igb->unicst_addr[0].mac.set = 1;
20215779Sxy150489 
20225779Sxy150489 		for (slot = 1; slot < igb->unicst_total; slot++)
20235779Sxy150489 			igb->unicst_addr[slot].mac.set = 0;
20245779Sxy150489 
20255779Sxy150489 		igb->unicst_init = B_TRUE;
20265779Sxy150489 	} else {
20275779Sxy150489 		/* Recover the default mac address */
20285779Sxy150489 		bcopy(igb->unicst_addr[0].mac.addr, hw->mac.addr,
20295779Sxy150489 		    ETHERADDRL);
20305779Sxy150489 
20315779Sxy150489 		/* Store the default mac address */
20325779Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
20335779Sxy150489 
20345779Sxy150489 		/* Re-configure the RAR registers */
20355779Sxy150489 		for (slot = 1; slot < igb->unicst_total; slot++)
20365779Sxy150489 			e1000_rar_set(hw,
20375779Sxy150489 			    igb->unicst_addr[slot].mac.addr, slot);
20385779Sxy150489 	}
20396624Sgl147354 
20406624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
20416624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
20425779Sxy150489 }
20435779Sxy150489 
20445779Sxy150489 /*
20455779Sxy150489  * igb_unicst_set - Set the unicast address to the specified slot
20465779Sxy150489  */
20475779Sxy150489 int
20485779Sxy150489 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
20495779Sxy150489     mac_addr_slot_t slot)
20505779Sxy150489 {
20515779Sxy150489 	struct e1000_hw *hw = &igb->hw;
20525779Sxy150489 
20535779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
20545779Sxy150489 
20555779Sxy150489 	/*
20565779Sxy150489 	 * Save the unicast address in the software data structure
20575779Sxy150489 	 */
20585779Sxy150489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
20595779Sxy150489 
20605779Sxy150489 	/*
20615779Sxy150489 	 * Set the unicast address to the RAR register
20625779Sxy150489 	 */
20635779Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
20645779Sxy150489 
20656624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
20666624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
20676624Sgl147354 		return (EIO);
20686624Sgl147354 	}
20696624Sgl147354 
20705779Sxy150489 	return (0);
20715779Sxy150489 }
20725779Sxy150489 
20735779Sxy150489 /*
20745779Sxy150489  * igb_multicst_add - Add a multicst address
20755779Sxy150489  */
20765779Sxy150489 int
20775779Sxy150489 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
20785779Sxy150489 {
20795779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
20805779Sxy150489 
20815779Sxy150489 	if ((multiaddr[0] & 01) == 0) {
20825779Sxy150489 		return (EINVAL);
20835779Sxy150489 	}
20845779Sxy150489 
20855779Sxy150489 	if (igb->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
20865779Sxy150489 		return (ENOENT);
20875779Sxy150489 	}
20885779Sxy150489 
20895779Sxy150489 	bcopy(multiaddr,
20905779Sxy150489 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
20915779Sxy150489 	igb->mcast_count++;
20925779Sxy150489 
20935779Sxy150489 	/*
20945779Sxy150489 	 * Update the multicast table in the hardware
20955779Sxy150489 	 */
20965779Sxy150489 	igb_setup_multicst(igb);
20975779Sxy150489 
20986624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
20996624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
21006624Sgl147354 		return (EIO);
21016624Sgl147354 	}
21026624Sgl147354 
21035779Sxy150489 	return (0);
21045779Sxy150489 }
21055779Sxy150489 
21065779Sxy150489 /*
21075779Sxy150489  * igb_multicst_remove - Remove a multicst address
21085779Sxy150489  */
21095779Sxy150489 int
21105779Sxy150489 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
21115779Sxy150489 {
21125779Sxy150489 	int i;
21135779Sxy150489 
21145779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
21155779Sxy150489 
21165779Sxy150489 	for (i = 0; i < igb->mcast_count; i++) {
21175779Sxy150489 		if (bcmp(multiaddr, &igb->mcast_table[i],
21185779Sxy150489 		    ETHERADDRL) == 0) {
21195779Sxy150489 			for (i++; i < igb->mcast_count; i++) {
21205779Sxy150489 				igb->mcast_table[i - 1] =
21215779Sxy150489 				    igb->mcast_table[i];
21225779Sxy150489 			}
21235779Sxy150489 			igb->mcast_count--;
21245779Sxy150489 			break;
21255779Sxy150489 		}
21265779Sxy150489 	}
21275779Sxy150489 
21285779Sxy150489 	/*
21295779Sxy150489 	 * Update the multicast table in the hardware
21305779Sxy150489 	 */
21315779Sxy150489 	igb_setup_multicst(igb);
21325779Sxy150489 
21336624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
21346624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
21356624Sgl147354 		return (EIO);
21366624Sgl147354 	}
21376624Sgl147354 
21385779Sxy150489 	return (0);
21395779Sxy150489 }
21405779Sxy150489 
21415779Sxy150489 /*
21425779Sxy150489  * igb_setup_multicast - setup multicast data structures
21435779Sxy150489  *
21445779Sxy150489  * This routine initializes all of the multicast related structures
21455779Sxy150489  * and save them in the hardware registers.
21465779Sxy150489  */
21475779Sxy150489 static void
21485779Sxy150489 igb_setup_multicst(igb_t *igb)
21495779Sxy150489 {
21505779Sxy150489 	uint8_t *mc_addr_list;
21515779Sxy150489 	uint32_t mc_addr_count;
21525779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21535779Sxy150489 
21545779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
21555779Sxy150489 
21565779Sxy150489 	ASSERT(igb->mcast_count <= MAX_NUM_MULTICAST_ADDRESSES);
21575779Sxy150489 
21585779Sxy150489 	mc_addr_list = (uint8_t *)igb->mcast_table;
21595779Sxy150489 	mc_addr_count = igb->mcast_count;
21605779Sxy150489 
21615779Sxy150489 	/*
21625779Sxy150489 	 * Update the multicase addresses to the MTA registers
21635779Sxy150489 	 */
21645779Sxy150489 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
21655779Sxy150489 	    igb->unicst_total, hw->mac.rar_entry_count);
21665779Sxy150489 }
21675779Sxy150489 
21685779Sxy150489 /*
21695779Sxy150489  * igb_get_conf - Get driver configurations set in driver.conf
21705779Sxy150489  *
21715779Sxy150489  * This routine gets user-configured values out of the configuration
21725779Sxy150489  * file igb.conf.
21735779Sxy150489  *
21745779Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
21755779Sxy150489  * default.
21765779Sxy150489  * If user does not configure a value, use the default.
21775779Sxy150489  * If user configures below the minimum, use the minumum.
21785779Sxy150489  * If user configures above the maximum, use the maxumum.
21795779Sxy150489  */
21805779Sxy150489 static void
21815779Sxy150489 igb_get_conf(igb_t *igb)
21825779Sxy150489 {
21835779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21845779Sxy150489 	uint32_t default_mtu;
21855779Sxy150489 	uint32_t flow_control;
21865779Sxy150489 
21875779Sxy150489 	/*
21885779Sxy150489 	 * igb driver supports the following user configurations:
21895779Sxy150489 	 *
21905779Sxy150489 	 * Link configurations:
21915779Sxy150489 	 *    adv_autoneg_cap
21925779Sxy150489 	 *    adv_1000fdx_cap
21935779Sxy150489 	 *    adv_100fdx_cap
21945779Sxy150489 	 *    adv_100hdx_cap
21955779Sxy150489 	 *    adv_10fdx_cap
21965779Sxy150489 	 *    adv_10hdx_cap
21975779Sxy150489 	 * Note: 1000hdx is not supported.
21985779Sxy150489 	 *
21995779Sxy150489 	 * Jumbo frame configuration:
22005779Sxy150489 	 *    default_mtu
22015779Sxy150489 	 *
22025779Sxy150489 	 * Ethernet flow control configuration:
22035779Sxy150489 	 *    flow_control
22045779Sxy150489 	 *
22055779Sxy150489 	 * Multiple rings configurations:
22065779Sxy150489 	 *    tx_queue_number
22075779Sxy150489 	 *    tx_ring_size
22085779Sxy150489 	 *    rx_queue_number
22095779Sxy150489 	 *    rx_ring_size
22105779Sxy150489 	 *
22115779Sxy150489 	 * Call igb_get_prop() to get the value for a specific
22125779Sxy150489 	 * configuration parameter.
22135779Sxy150489 	 */
22145779Sxy150489 
22155779Sxy150489 	/*
22165779Sxy150489 	 * Link configurations
22175779Sxy150489 	 */
22185779Sxy150489 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
22195779Sxy150489 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
22205779Sxy150489 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
22215779Sxy150489 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
22225779Sxy150489 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
22235779Sxy150489 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
22245779Sxy150489 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
22255779Sxy150489 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
22265779Sxy150489 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
22275779Sxy150489 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
22285779Sxy150489 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
22295779Sxy150489 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
22305779Sxy150489 
22315779Sxy150489 	/*
22325779Sxy150489 	 * Jumbo frame configurations
22335779Sxy150489 	 */
22345779Sxy150489 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
22355779Sxy150489 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
22365779Sxy150489 
22375779Sxy150489 	igb->max_frame_size = default_mtu +
22385779Sxy150489 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
22395779Sxy150489 
22405779Sxy150489 	/*
22415779Sxy150489 	 * Ethernet flow control configuration
22425779Sxy150489 	 */
22435779Sxy150489 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
22445779Sxy150489 	    e1000_fc_none, 4, e1000_fc_full);
22455779Sxy150489 	if (flow_control == 4)
22465779Sxy150489 		flow_control = e1000_fc_default;
22475779Sxy150489 
22485779Sxy150489 	hw->fc.type = flow_control;
22495779Sxy150489 
22505779Sxy150489 	/*
22515779Sxy150489 	 * Multiple rings configurations
22525779Sxy150489 	 */
22535779Sxy150489 	igb->num_tx_rings = igb_get_prop(igb, PROP_TX_QUEUE_NUM,
22545779Sxy150489 	    MIN_TX_QUEUE_NUM, MAX_TX_QUEUE_NUM, DEFAULT_TX_QUEUE_NUM);
22555779Sxy150489 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
22565779Sxy150489 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
22575779Sxy150489 
22585779Sxy150489 	igb->num_rx_rings = igb_get_prop(igb, PROP_RX_QUEUE_NUM,
22595779Sxy150489 	    MIN_RX_QUEUE_NUM, MAX_RX_QUEUE_NUM, DEFAULT_RX_QUEUE_NUM);
22605779Sxy150489 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
22615779Sxy150489 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
22625779Sxy150489 
22635779Sxy150489 	/*
22645779Sxy150489 	 * Tunable used to force an interrupt type. The only use is
22655779Sxy150489 	 * for testing of the lesser interrupt types.
22665779Sxy150489 	 * 0 = don't force interrupt type
22675779Sxy150489 	 * 1 = force interrupt type MSIX
22685779Sxy150489 	 * 2 = force interrupt type MSI
22695779Sxy150489 	 * 3 = force interrupt type Legacy
22705779Sxy150489 	 */
22715779Sxy150489 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
22725812Sxy150489 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_NONE);
22735779Sxy150489 
22745779Sxy150489 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
22755779Sxy150489 	    0, 1, 1);
22765779Sxy150489 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
22775779Sxy150489 	    0, 1, 1);
22785779Sxy150489 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
22795779Sxy150489 	    0, 1, 0);
22805779Sxy150489 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
22815779Sxy150489 	    0, 1, 1);
22825779Sxy150489 
22835779Sxy150489 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
22845779Sxy150489 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
22855779Sxy150489 	    DEFAULT_TX_COPY_THRESHOLD);
22865779Sxy150489 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
22875779Sxy150489 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
22885779Sxy150489 	    DEFAULT_TX_RECYCLE_THRESHOLD);
22895779Sxy150489 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
22905779Sxy150489 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
22915779Sxy150489 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
22925779Sxy150489 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
22935779Sxy150489 	    MIN_TX_RESCHED_THRESHOLD, MAX_TX_RESCHED_THRESHOLD,
22945779Sxy150489 	    DEFAULT_TX_RESCHED_THRESHOLD);
22955779Sxy150489 
22965779Sxy150489 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
22975779Sxy150489 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
22985779Sxy150489 	    DEFAULT_RX_COPY_THRESHOLD);
22995779Sxy150489 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
23005779Sxy150489 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
23015779Sxy150489 	    DEFAULT_RX_LIMIT_PER_INTR);
23025779Sxy150489 
23035779Sxy150489 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
23045779Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
23055779Sxy150489 	    DEFAULT_INTR_THROTTLING);
23065779Sxy150489 }
23075779Sxy150489 
23085779Sxy150489 /*
23095779Sxy150489  * igb_get_prop - Get a property value out of the configuration file igb.conf
23105779Sxy150489  *
23115779Sxy150489  * Caller provides the name of the property, a default value, a minimum
23125779Sxy150489  * value, and a maximum value.
23135779Sxy150489  *
23145779Sxy150489  * Return configured value of the property, with default, minimum and
23155779Sxy150489  * maximum properly applied.
23165779Sxy150489  */
23175779Sxy150489 static int
23185779Sxy150489 igb_get_prop(igb_t *igb,
23195779Sxy150489     char *propname,	/* name of the property */
23205779Sxy150489     int minval,		/* minimum acceptable value */
23215779Sxy150489     int maxval,		/* maximim acceptable value */
23225779Sxy150489     int defval)		/* default value */
23235779Sxy150489 {
23245779Sxy150489 	int value;
23255779Sxy150489 
23265779Sxy150489 	/*
23275779Sxy150489 	 * Call ddi_prop_get_int() to read the conf settings
23285779Sxy150489 	 */
23295779Sxy150489 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
23305779Sxy150489 	    DDI_PROP_DONTPASS, propname, defval);
23315779Sxy150489 
23325779Sxy150489 	if (value > maxval)
23335779Sxy150489 		value = maxval;
23345779Sxy150489 
23355779Sxy150489 	if (value < minval)
23365779Sxy150489 		value = minval;
23375779Sxy150489 
23385779Sxy150489 	return (value);
23395779Sxy150489 }
23405779Sxy150489 
23415779Sxy150489 /*
23425779Sxy150489  * igb_setup_link - Using the link properties to setup the link
23435779Sxy150489  */
23445779Sxy150489 int
23455779Sxy150489 igb_setup_link(igb_t *igb, boolean_t setup_hw)
23465779Sxy150489 {
23475779Sxy150489 	struct e1000_mac_info *mac;
23485779Sxy150489 	struct e1000_phy_info *phy;
23495779Sxy150489 	boolean_t invalid;
23505779Sxy150489 
23515779Sxy150489 	mac = &igb->hw.mac;
23525779Sxy150489 	phy = &igb->hw.phy;
23535779Sxy150489 	invalid = B_FALSE;
23545779Sxy150489 
23555779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
23565779Sxy150489 		mac->autoneg = B_TRUE;
23575779Sxy150489 		phy->autoneg_advertised = 0;
23585779Sxy150489 
23595779Sxy150489 		/*
23605779Sxy150489 		 * 1000hdx is not supported for autonegotiation
23615779Sxy150489 		 */
23625779Sxy150489 		if (igb->param_adv_1000fdx_cap == 1)
23635779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
23645779Sxy150489 
23655779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
23665779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
23675779Sxy150489 
23685779Sxy150489 		if (igb->param_adv_100hdx_cap == 1)
23695779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
23705779Sxy150489 
23715779Sxy150489 		if (igb->param_adv_10fdx_cap == 1)
23725779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
23735779Sxy150489 
23745779Sxy150489 		if (igb->param_adv_10hdx_cap == 1)
23755779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
23765779Sxy150489 
23775779Sxy150489 		if (phy->autoneg_advertised == 0)
23785779Sxy150489 			invalid = B_TRUE;
23795779Sxy150489 	} else {
23805779Sxy150489 		mac->autoneg = B_FALSE;
23815779Sxy150489 
23825779Sxy150489 		/*
23835779Sxy150489 		 * 1000fdx and 1000hdx are not supported for forced link
23845779Sxy150489 		 */
23855779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
23865779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
23875779Sxy150489 		else if (igb->param_adv_100hdx_cap == 1)
23885779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
23895779Sxy150489 		else if (igb->param_adv_10fdx_cap == 1)
23905779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
23915779Sxy150489 		else if (igb->param_adv_10hdx_cap == 1)
23925779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
23935779Sxy150489 		else
23945779Sxy150489 			invalid = B_TRUE;
23955779Sxy150489 	}
23965779Sxy150489 
23975779Sxy150489 	if (invalid) {
23985779Sxy150489 		igb_notice(igb, "Invalid link settings. Setup link to "
23995779Sxy150489 		    "autonegotiation with full link capabilities.");
24005779Sxy150489 		mac->autoneg = B_TRUE;
24015779Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
24025779Sxy150489 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
24035779Sxy150489 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
24045779Sxy150489 	}
24055779Sxy150489 
24065779Sxy150489 	if (setup_hw) {
24075779Sxy150489 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
24085779Sxy150489 			return (IGB_FAILURE);
24095779Sxy150489 	}
24105779Sxy150489 
24115779Sxy150489 	return (IGB_SUCCESS);
24125779Sxy150489 }
24135779Sxy150489 
24145779Sxy150489 
24155779Sxy150489 /*
24165779Sxy150489  * igb_is_link_up - Check if the link is up
24175779Sxy150489  */
24185779Sxy150489 static boolean_t
24195779Sxy150489 igb_is_link_up(igb_t *igb)
24205779Sxy150489 {
24215779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24225779Sxy150489 	boolean_t link_up;
24235779Sxy150489 
24245779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24255779Sxy150489 
24265779Sxy150489 	(void) e1000_check_for_link(hw);
24275779Sxy150489 
24285779Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
24295779Sxy150489 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
24305779Sxy150489 	    (hw->mac.serdes_has_link))) {
24315779Sxy150489 		link_up = B_TRUE;
24325779Sxy150489 	} else {
24335779Sxy150489 		link_up = B_FALSE;
24345779Sxy150489 	}
24355779Sxy150489 
24365779Sxy150489 	return (link_up);
24375779Sxy150489 }
24385779Sxy150489 
24395779Sxy150489 /*
24405779Sxy150489  * igb_link_check - Link status processing
24415779Sxy150489  */
24425779Sxy150489 static boolean_t
24435779Sxy150489 igb_link_check(igb_t *igb)
24445779Sxy150489 {
24455779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24465779Sxy150489 	uint16_t speed = 0, duplex = 0;
24475779Sxy150489 	boolean_t link_changed = B_FALSE;
24485779Sxy150489 
24495779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24505779Sxy150489 
24515779Sxy150489 	if (igb_is_link_up(igb)) {
24525779Sxy150489 		/*
24535779Sxy150489 		 * The Link is up, check whether it was marked as down earlier
24545779Sxy150489 		 */
24555779Sxy150489 		if (igb->link_state != LINK_STATE_UP) {
24565779Sxy150489 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
24575779Sxy150489 			igb->link_speed = speed;
24585779Sxy150489 			igb->link_duplex = duplex;
24595779Sxy150489 			igb->link_state = LINK_STATE_UP;
24605779Sxy150489 			igb->link_down_timeout = 0;
24615779Sxy150489 			link_changed = B_TRUE;
24625779Sxy150489 		}
24635779Sxy150489 	} else {
24645779Sxy150489 		if (igb->link_state != LINK_STATE_DOWN) {
24655779Sxy150489 			igb->link_speed = 0;
24665779Sxy150489 			igb->link_duplex = 0;
24675779Sxy150489 			igb->link_state = LINK_STATE_DOWN;
24685779Sxy150489 			link_changed = B_TRUE;
24695779Sxy150489 		}
24705779Sxy150489 
24715779Sxy150489 		if (igb->igb_state & IGB_STARTED) {
24725779Sxy150489 			if (igb->link_down_timeout < MAX_LINK_DOWN_TIMEOUT) {
24735779Sxy150489 				igb->link_down_timeout++;
24745779Sxy150489 			} else if (igb->link_down_timeout ==
24755779Sxy150489 			    MAX_LINK_DOWN_TIMEOUT) {
24765779Sxy150489 				igb_tx_clean(igb);
24775779Sxy150489 				igb->link_down_timeout++;
24785779Sxy150489 			}
24795779Sxy150489 		}
24805779Sxy150489 	}
24815779Sxy150489 
24826624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
24836624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24846624Sgl147354 
24855779Sxy150489 	return (link_changed);
24865779Sxy150489 }
24875779Sxy150489 
24885779Sxy150489 /*
24895779Sxy150489  * igb_local_timer - driver watchdog function
24905779Sxy150489  *
24915779Sxy150489  * This function will handle the transmit stall check, link status check and
24925779Sxy150489  * other routines.
24935779Sxy150489  */
24945779Sxy150489 static void
24955779Sxy150489 igb_local_timer(void *arg)
24965779Sxy150489 {
24975779Sxy150489 	igb_t *igb = (igb_t *)arg;
24985779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24995779Sxy150489 	boolean_t link_changed;
25005779Sxy150489 
25015779Sxy150489 	if (igb_stall_check(igb)) {
25026624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_STALL);
25035779Sxy150489 		igb->reset_count++;
25046624Sgl147354 		if (igb_reset(igb) == IGB_SUCCESS)
25056624Sgl147354 			ddi_fm_service_impact(igb->dip,
25066624Sgl147354 			    DDI_SERVICE_RESTORED);
25075779Sxy150489 	}
25085779Sxy150489 
25095779Sxy150489 	mutex_enter(&igb->gen_lock);
25105779Sxy150489 	link_changed = igb_link_check(igb);
25115779Sxy150489 	mutex_exit(&igb->gen_lock);
25125779Sxy150489 
25135779Sxy150489 	if (link_changed)
25145779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
25155779Sxy150489 
25165779Sxy150489 	/*
25175779Sxy150489 	 * Set Timer Interrupts
25185779Sxy150489 	 */
25195779Sxy150489 	if (igb->intr_type != DDI_INTR_TYPE_MSIX)
25205779Sxy150489 		E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
25215779Sxy150489 
25226624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
25236624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
25246624Sgl147354 
25255779Sxy150489 	igb_restart_watchdog_timer(igb);
25265779Sxy150489 }
25275779Sxy150489 
25285779Sxy150489 /*
25295779Sxy150489  * igb_stall_check - check for transmit stall
25305779Sxy150489  *
25315779Sxy150489  * This function checks if the adapter is stalled (in transmit).
25325779Sxy150489  *
25335779Sxy150489  * It is called each time the watchdog timeout is invoked.
25345779Sxy150489  * If the transmit descriptor reclaim continuously fails,
25355779Sxy150489  * the watchdog value will increment by 1. If the watchdog
25365779Sxy150489  * value exceeds the threshold, the igb is assumed to
25375779Sxy150489  * have stalled and need to be reset.
25385779Sxy150489  */
25395779Sxy150489 static boolean_t
25405779Sxy150489 igb_stall_check(igb_t *igb)
25415779Sxy150489 {
25425779Sxy150489 	igb_tx_ring_t *tx_ring;
25435779Sxy150489 	boolean_t result;
25445779Sxy150489 	int i;
25455779Sxy150489 
25465779Sxy150489 	if (igb->link_state != LINK_STATE_UP)
25475779Sxy150489 		return (B_FALSE);
25485779Sxy150489 
25495779Sxy150489 	/*
25505779Sxy150489 	 * If any tx ring is stalled, we'll reset the chipset
25515779Sxy150489 	 */
25525779Sxy150489 	result = B_FALSE;
25535779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
25545779Sxy150489 		tx_ring = &igb->tx_rings[i];
25555779Sxy150489 
25565779Sxy150489 		if (tx_ring->recycle_fail > 0)
25575779Sxy150489 			tx_ring->stall_watchdog++;
25585779Sxy150489 		else
25595779Sxy150489 			tx_ring->stall_watchdog = 0;
25605779Sxy150489 
25615779Sxy150489 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
25625779Sxy150489 			result = B_TRUE;
25635779Sxy150489 			break;
25645779Sxy150489 		}
25655779Sxy150489 	}
25665779Sxy150489 
25675779Sxy150489 	if (result) {
25685779Sxy150489 		tx_ring->stall_watchdog = 0;
25695779Sxy150489 		tx_ring->recycle_fail = 0;
25705779Sxy150489 	}
25715779Sxy150489 
25725779Sxy150489 	return (result);
25735779Sxy150489 }
25745779Sxy150489 
25755779Sxy150489 
25765779Sxy150489 /*
25775779Sxy150489  * is_valid_mac_addr - Check if the mac address is valid
25785779Sxy150489  */
25795779Sxy150489 static boolean_t
25805779Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
25815779Sxy150489 {
25825779Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
25835779Sxy150489 	const uint8_t addr_test2[6] =
25845779Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
25855779Sxy150489 
25865779Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
25875779Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
25885779Sxy150489 		return (B_FALSE);
25895779Sxy150489 
25905779Sxy150489 	return (B_TRUE);
25915779Sxy150489 }
25925779Sxy150489 
25935779Sxy150489 static boolean_t
25945779Sxy150489 igb_find_mac_address(igb_t *igb)
25955779Sxy150489 {
25965779Sxy150489 	struct e1000_hw *hw = &igb->hw;
25975779Sxy150489 #ifdef __sparc
25985779Sxy150489 	uchar_t *bytes;
25995779Sxy150489 	struct ether_addr sysaddr;
26005779Sxy150489 	uint_t nelts;
26015779Sxy150489 	int err;
26025779Sxy150489 	boolean_t found = B_FALSE;
26035779Sxy150489 
26045779Sxy150489 	/*
26055779Sxy150489 	 * The "vendor's factory-set address" may already have
26065779Sxy150489 	 * been extracted from the chip, but if the property
26075779Sxy150489 	 * "local-mac-address" is set we use that instead.
26085779Sxy150489 	 *
26095779Sxy150489 	 * We check whether it looks like an array of 6
26105779Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
26115779Sxy150489 	 * make sense of it this way, we'll ignore it.
26125779Sxy150489 	 */
26135779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
26145779Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
26155779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
26165779Sxy150489 		if (nelts == ETHERADDRL) {
26175779Sxy150489 			while (nelts--)
26185779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
26195779Sxy150489 			found = B_TRUE;
26205779Sxy150489 		}
26215779Sxy150489 		ddi_prop_free(bytes);
26225779Sxy150489 	}
26235779Sxy150489 
26245779Sxy150489 	/*
26255779Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
26265779Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
26275779Sxy150489 	 */
26285779Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
26295779Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
26305779Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
26315779Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
26325779Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
26335779Sxy150489 				found = B_TRUE;
26345779Sxy150489 			}
26355779Sxy150489 		}
26365779Sxy150489 		ddi_prop_free(bytes);
26375779Sxy150489 	}
26385779Sxy150489 
26395779Sxy150489 	/*
26405779Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
26415779Sxy150489 	 * if we netbooted from this interface), we must use this instead
26425779Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
26435779Sxy150489 	 * get confused by the address changing as Solaris takes over!
26445779Sxy150489 	 */
26455779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
26465779Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
26475779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
26485779Sxy150489 		if (nelts == ETHERADDRL) {
26495779Sxy150489 			while (nelts--)
26505779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
26515779Sxy150489 			found = B_TRUE;
26525779Sxy150489 		}
26535779Sxy150489 		ddi_prop_free(bytes);
26545779Sxy150489 	}
26555779Sxy150489 
26565779Sxy150489 	if (found) {
26575779Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
26585779Sxy150489 		return (B_TRUE);
26595779Sxy150489 	}
26605779Sxy150489 #endif
26615779Sxy150489 
26625779Sxy150489 	/*
26635779Sxy150489 	 * Read the device MAC address from the EEPROM
26645779Sxy150489 	 */
26655779Sxy150489 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
26665779Sxy150489 		return (B_FALSE);
26675779Sxy150489 
26685779Sxy150489 	return (B_TRUE);
26695779Sxy150489 }
26705779Sxy150489 
26715779Sxy150489 #pragma inline(igb_arm_watchdog_timer)
26725779Sxy150489 
26735779Sxy150489 static void
26745779Sxy150489 igb_arm_watchdog_timer(igb_t *igb)
26755779Sxy150489 {
26765779Sxy150489 	/*
26775779Sxy150489 	 * Fire a watchdog timer
26785779Sxy150489 	 */
26795779Sxy150489 	igb->watchdog_tid =
26805779Sxy150489 	    timeout(igb_local_timer,
26815779Sxy150489 	    (void *)igb, 1 * drv_usectohz(1000000));
26825779Sxy150489 
26835779Sxy150489 }
26845779Sxy150489 
26855779Sxy150489 /*
26865779Sxy150489  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
26875779Sxy150489  */
26885779Sxy150489 void
26895779Sxy150489 igb_enable_watchdog_timer(igb_t *igb)
26905779Sxy150489 {
26915779Sxy150489 	mutex_enter(&igb->watchdog_lock);
26925779Sxy150489 
26935779Sxy150489 	if (!igb->watchdog_enable) {
26945779Sxy150489 		igb->watchdog_enable = B_TRUE;
26955779Sxy150489 		igb->watchdog_start = B_TRUE;
26965779Sxy150489 		igb_arm_watchdog_timer(igb);
26975779Sxy150489 	}
26985779Sxy150489 
26995779Sxy150489 	mutex_exit(&igb->watchdog_lock);
27005779Sxy150489 
27015779Sxy150489 }
27025779Sxy150489 
27035779Sxy150489 /*
27045779Sxy150489  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
27055779Sxy150489  */
27065779Sxy150489 void
27075779Sxy150489 igb_disable_watchdog_timer(igb_t *igb)
27085779Sxy150489 {
27095779Sxy150489 	timeout_id_t tid;
27105779Sxy150489 
27115779Sxy150489 	mutex_enter(&igb->watchdog_lock);
27125779Sxy150489 
27135779Sxy150489 	igb->watchdog_enable = B_FALSE;
27145779Sxy150489 	igb->watchdog_start = B_FALSE;
27155779Sxy150489 	tid = igb->watchdog_tid;
27165779Sxy150489 	igb->watchdog_tid = 0;
27175779Sxy150489 
27185779Sxy150489 	mutex_exit(&igb->watchdog_lock);
27195779Sxy150489 
27205779Sxy150489 	if (tid != 0)
27215779Sxy150489 		(void) untimeout(tid);
27225779Sxy150489 
27235779Sxy150489 }
27245779Sxy150489 
27255779Sxy150489 /*
27265779Sxy150489  * igb_start_watchdog_timer - Start the driver watchdog timer
27275779Sxy150489  */
27285779Sxy150489 static void
27295779Sxy150489 igb_start_watchdog_timer(igb_t *igb)
27305779Sxy150489 {
27315779Sxy150489 	mutex_enter(&igb->watchdog_lock);
27325779Sxy150489 
27335779Sxy150489 	if (igb->watchdog_enable) {
27345779Sxy150489 		if (!igb->watchdog_start) {
27355779Sxy150489 			igb->watchdog_start = B_TRUE;
27365779Sxy150489 			igb_arm_watchdog_timer(igb);
27375779Sxy150489 		}
27385779Sxy150489 	}
27395779Sxy150489 
27405779Sxy150489 	mutex_exit(&igb->watchdog_lock);
27415779Sxy150489 }
27425779Sxy150489 
27435779Sxy150489 /*
27445779Sxy150489  * igb_restart_watchdog_timer - Restart the driver watchdog timer
27455779Sxy150489  */
27465779Sxy150489 static void
27475779Sxy150489 igb_restart_watchdog_timer(igb_t *igb)
27485779Sxy150489 {
27495779Sxy150489 	mutex_enter(&igb->watchdog_lock);
27505779Sxy150489 
27515779Sxy150489 	if (igb->watchdog_start)
27525779Sxy150489 		igb_arm_watchdog_timer(igb);
27535779Sxy150489 
27545779Sxy150489 	mutex_exit(&igb->watchdog_lock);
27555779Sxy150489 }
27565779Sxy150489 
27575779Sxy150489 /*
27585779Sxy150489  * igb_stop_watchdog_timer - Stop the driver watchdog timer
27595779Sxy150489  */
27605779Sxy150489 static void
27615779Sxy150489 igb_stop_watchdog_timer(igb_t *igb)
27625779Sxy150489 {
27635779Sxy150489 	timeout_id_t tid;
27645779Sxy150489 
27655779Sxy150489 	mutex_enter(&igb->watchdog_lock);
27665779Sxy150489 
27675779Sxy150489 	igb->watchdog_start = B_FALSE;
27685779Sxy150489 	tid = igb->watchdog_tid;
27695779Sxy150489 	igb->watchdog_tid = 0;
27705779Sxy150489 
27715779Sxy150489 	mutex_exit(&igb->watchdog_lock);
27725779Sxy150489 
27735779Sxy150489 	if (tid != 0)
27745779Sxy150489 		(void) untimeout(tid);
27755779Sxy150489 }
27765779Sxy150489 
27775779Sxy150489 /*
27785779Sxy150489  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
27795779Sxy150489  */
27805779Sxy150489 static void
27815779Sxy150489 igb_disable_adapter_interrupts(igb_t *igb)
27825779Sxy150489 {
27835779Sxy150489 	struct e1000_hw *hw = &igb->hw;
27845779Sxy150489 
27855779Sxy150489 	/*
27865779Sxy150489 	 * Set the IMC register to mask all the interrupts,
27875779Sxy150489 	 * including the tx interrupts.
27885779Sxy150489 	 */
27895779Sxy150489 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
27905779Sxy150489 
27915779Sxy150489 	/*
27925779Sxy150489 	 * Additional disabling for MSI-X
27935779Sxy150489 	 */
27945779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
27955779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMC, 0xffffffff);
27965779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, 0x0);
27975779Sxy150489 	}
27985779Sxy150489 
27995779Sxy150489 	E1000_WRITE_FLUSH(hw);
28005779Sxy150489 }
28015779Sxy150489 
28025779Sxy150489 /*
28035779Sxy150489  * igb_enable_adapter_interrupts - Mask/enable all hardware interrupts
28045779Sxy150489  */
28055779Sxy150489 static void
28065779Sxy150489 igb_enable_adapter_interrupts(igb_t *igb)
28075779Sxy150489 {
28085779Sxy150489 	struct e1000_hw *hw = &igb->hw;
28095779Sxy150489 	uint32_t reg;
28105779Sxy150489 
28115779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
28125779Sxy150489 		/* Interrupt enabling for MSI-X */
28135779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
28145779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
28155779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
28165779Sxy150489 
28175779Sxy150489 		/* Enable MSI-X PBA support */
28185779Sxy150489 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
28195779Sxy150489 		reg |= E1000_CTRL_EXT_PBA_CLR;
28205779Sxy150489 
28215779Sxy150489 		/* Non-selective interrupt clear-on-read */
28225779Sxy150489 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
28235779Sxy150489 
28245779Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
28255779Sxy150489 	} else {
28265779Sxy150489 		/* Interrupt enabling for MSI and legacy */
28275779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
28285779Sxy150489 	}
28295779Sxy150489 
28305779Sxy150489 	E1000_WRITE_FLUSH(hw);
28315779Sxy150489 }
28325779Sxy150489 
28335779Sxy150489 /*
28345779Sxy150489  * Loopback Support
28355779Sxy150489  */
28365779Sxy150489 static lb_property_t lb_normal =
28375779Sxy150489 	{ normal,	"normal",	IGB_LB_NONE		};
28385779Sxy150489 static lb_property_t lb_external =
28395779Sxy150489 	{ external,	"External",	IGB_LB_EXTERNAL		};
28405779Sxy150489 static lb_property_t lb_mac =
28415779Sxy150489 	{ internal,	"MAC",		IGB_LB_INTERNAL_MAC	};
28425779Sxy150489 static lb_property_t lb_phy =
28435779Sxy150489 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
28445779Sxy150489 static lb_property_t lb_serdes =
28455779Sxy150489 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
28465779Sxy150489 
28475779Sxy150489 enum ioc_reply
28485779Sxy150489 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
28495779Sxy150489 {
28505779Sxy150489 	lb_info_sz_t *lbsp;
28515779Sxy150489 	lb_property_t *lbpp;
28525779Sxy150489 	struct e1000_hw *hw;
28535779Sxy150489 	uint32_t *lbmp;
28545779Sxy150489 	uint32_t size;
28555779Sxy150489 	uint32_t value;
28565779Sxy150489 
28575779Sxy150489 	hw = &igb->hw;
28585779Sxy150489 
28595779Sxy150489 	if (mp->b_cont == NULL)
28605779Sxy150489 		return (IOC_INVAL);
28615779Sxy150489 
28625779Sxy150489 	switch (iocp->ioc_cmd) {
28635779Sxy150489 	default:
28645779Sxy150489 		return (IOC_INVAL);
28655779Sxy150489 
28665779Sxy150489 	case LB_GET_INFO_SIZE:
28675779Sxy150489 		size = sizeof (lb_info_sz_t);
28685779Sxy150489 		if (iocp->ioc_count != size)
28695779Sxy150489 			return (IOC_INVAL);
28705779Sxy150489 
28715779Sxy150489 		value = sizeof (lb_normal);
28725779Sxy150489 		value += sizeof (lb_mac);
28735779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
28745779Sxy150489 			value += sizeof (lb_phy);
28755779Sxy150489 		else
28765779Sxy150489 			value += sizeof (lb_serdes);
28775779Sxy150489 		value += sizeof (lb_external);
28785779Sxy150489 
28795779Sxy150489 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
28805779Sxy150489 		*lbsp = value;
28815779Sxy150489 		break;
28825779Sxy150489 
28835779Sxy150489 	case LB_GET_INFO:
28845779Sxy150489 		value = sizeof (lb_normal);
28855779Sxy150489 		value += sizeof (lb_mac);
28865779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
28875779Sxy150489 			value += sizeof (lb_phy);
28885779Sxy150489 		else
28895779Sxy150489 			value += sizeof (lb_serdes);
28905779Sxy150489 		value += sizeof (lb_external);
28915779Sxy150489 
28925779Sxy150489 		size = value;
28935779Sxy150489 		if (iocp->ioc_count != size)
28945779Sxy150489 			return (IOC_INVAL);
28955779Sxy150489 
28965779Sxy150489 		value = 0;
28975779Sxy150489 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
28985779Sxy150489 
28995779Sxy150489 		lbpp[value++] = lb_normal;
29005779Sxy150489 		lbpp[value++] = lb_mac;
29015779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
29025779Sxy150489 			lbpp[value++] = lb_phy;
29035779Sxy150489 		else
29045779Sxy150489 			lbpp[value++] = lb_serdes;
29055779Sxy150489 		lbpp[value++] = lb_external;
29065779Sxy150489 		break;
29075779Sxy150489 
29085779Sxy150489 	case LB_GET_MODE:
29095779Sxy150489 		size = sizeof (uint32_t);
29105779Sxy150489 		if (iocp->ioc_count != size)
29115779Sxy150489 			return (IOC_INVAL);
29125779Sxy150489 
29135779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
29145779Sxy150489 		*lbmp = igb->loopback_mode;
29155779Sxy150489 		break;
29165779Sxy150489 
29175779Sxy150489 	case LB_SET_MODE:
29185779Sxy150489 		size = 0;
29195779Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
29205779Sxy150489 			return (IOC_INVAL);
29215779Sxy150489 
29225779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
29235779Sxy150489 		if (!igb_set_loopback_mode(igb, *lbmp))
29245779Sxy150489 			return (IOC_INVAL);
29255779Sxy150489 		break;
29265779Sxy150489 	}
29275779Sxy150489 
29285779Sxy150489 	iocp->ioc_count = size;
29295779Sxy150489 	iocp->ioc_error = 0;
29305779Sxy150489 
29316624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
29326624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
29336624Sgl147354 		return (IOC_INVAL);
29346624Sgl147354 	}
29356624Sgl147354 
29365779Sxy150489 	return (IOC_REPLY);
29375779Sxy150489 }
29385779Sxy150489 
29395779Sxy150489 /*
29405779Sxy150489  * igb_set_loopback_mode - Setup loopback based on the loopback mode
29415779Sxy150489  */
29425779Sxy150489 static boolean_t
29435779Sxy150489 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
29445779Sxy150489 {
29455779Sxy150489 	struct e1000_hw *hw;
29465779Sxy150489 
29475779Sxy150489 	if (mode == igb->loopback_mode)
29485779Sxy150489 		return (B_TRUE);
29495779Sxy150489 
29505779Sxy150489 	hw = &igb->hw;
29515779Sxy150489 
29525779Sxy150489 	igb->loopback_mode = mode;
29535779Sxy150489 
29545779Sxy150489 	if (mode == IGB_LB_NONE) {
29555779Sxy150489 		/* Reset the chip */
29565779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_TRUE;
29575779Sxy150489 		(void) igb_reset(igb);
29585779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_FALSE;
29595779Sxy150489 		return (B_TRUE);
29605779Sxy150489 	}
29615779Sxy150489 
29625779Sxy150489 	mutex_enter(&igb->gen_lock);
29635779Sxy150489 
29645779Sxy150489 	switch (mode) {
29655779Sxy150489 	default:
29665779Sxy150489 		mutex_exit(&igb->gen_lock);
29675779Sxy150489 		return (B_FALSE);
29685779Sxy150489 
29695779Sxy150489 	case IGB_LB_EXTERNAL:
29705779Sxy150489 		igb_set_external_loopback(igb);
29715779Sxy150489 		break;
29725779Sxy150489 
29735779Sxy150489 	case IGB_LB_INTERNAL_MAC:
29745779Sxy150489 		igb_set_internal_mac_loopback(igb);
29755779Sxy150489 		break;
29765779Sxy150489 
29775779Sxy150489 	case IGB_LB_INTERNAL_PHY:
29785779Sxy150489 		igb_set_internal_phy_loopback(igb);
29795779Sxy150489 		break;
29805779Sxy150489 
29815779Sxy150489 	case IGB_LB_INTERNAL_SERDES:
29825779Sxy150489 		igb_set_internal_serdes_loopback(igb);
29835779Sxy150489 		break;
29845779Sxy150489 	}
29855779Sxy150489 
29865779Sxy150489 	mutex_exit(&igb->gen_lock);
29875779Sxy150489 
29885779Sxy150489 	return (B_TRUE);
29895779Sxy150489 }
29905779Sxy150489 
29915779Sxy150489 /*
29925779Sxy150489  * igb_set_external_loopback - Set the external loopback mode
29935779Sxy150489  */
29945779Sxy150489 static void
29955779Sxy150489 igb_set_external_loopback(igb_t *igb)
29965779Sxy150489 {
29975779Sxy150489 	struct e1000_hw *hw;
29985779Sxy150489 
29995779Sxy150489 	hw = &igb->hw;
30005779Sxy150489 
30015779Sxy150489 	/* Set phy to known state */
30025779Sxy150489 	(void) e1000_phy_hw_reset(hw);
30035779Sxy150489 
30045779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
30055779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x9, 0x1b00);
30065779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
30075779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
30085779Sxy150489 }
30095779Sxy150489 
30105779Sxy150489 /*
30115779Sxy150489  * igb_set_internal_mac_loopback - Set the internal MAC loopback mode
30125779Sxy150489  */
30135779Sxy150489 static void
30145779Sxy150489 igb_set_internal_mac_loopback(igb_t *igb)
30155779Sxy150489 {
30165779Sxy150489 	struct e1000_hw *hw;
30175779Sxy150489 	uint32_t ctrl;
30185779Sxy150489 	uint32_t rctl;
30195779Sxy150489 
30205779Sxy150489 	hw = &igb->hw;
30215779Sxy150489 
30225779Sxy150489 	/* Set the Receive Control register */
30235779Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
30245779Sxy150489 	rctl &= ~E1000_RCTL_LBM_TCVR;
30255779Sxy150489 	rctl |= E1000_RCTL_LBM_MAC;
30265779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
30275779Sxy150489 
30285779Sxy150489 	/* Set the Device Control register */
30295779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
30305779Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
30315779Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force link up */
30325779Sxy150489 	    E1000_CTRL_FRCSPD |		/* Force speed */
30335779Sxy150489 	    E1000_CTRL_FRCDPX |		/* Force duplex */
30345779Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force speed to 1000 */
30355779Sxy150489 	    E1000_CTRL_FD);		/* Force full duplex */
30365779Sxy150489 	ctrl &= ~E1000_CTRL_ILOS;	/* Clear ILOS when there's a link */
30375779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
30385779Sxy150489 }
30395779Sxy150489 
30405779Sxy150489 /*
30415779Sxy150489  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
30425779Sxy150489  */
30435779Sxy150489 static void
30445779Sxy150489 igb_set_internal_phy_loopback(igb_t *igb)
30455779Sxy150489 {
30465779Sxy150489 	struct e1000_hw *hw;
30475779Sxy150489 	uint32_t ctrl_ext;
30485779Sxy150489 	uint16_t phy_ctrl;
30495779Sxy150489 	uint16_t phy_pconf;
30505779Sxy150489 
30515779Sxy150489 	hw = &igb->hw;
30525779Sxy150489 
30535779Sxy150489 	/* Set link mode to PHY (00b) in the Extended Control register */
30545779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
30555779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
30565779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
30575779Sxy150489 
30585779Sxy150489 	/*
30595779Sxy150489 	 * Set PHY control register (0x4140):
30605779Sxy150489 	 *    Set full duplex mode
30615779Sxy150489 	 *    Set loopback bit
30625779Sxy150489 	 *    Clear auto-neg enable bit
30635779Sxy150489 	 *    Set PHY speed
30645779Sxy150489 	 */
30655779Sxy150489 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
30665779Sxy150489 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
30675779Sxy150489 
30685779Sxy150489 	/* Set the link disable bit in the Port Configuration register */
30695779Sxy150489 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
30705779Sxy150489 	phy_pconf |= (uint16_t)1 << 14;
30715779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
30725779Sxy150489 }
30735779Sxy150489 
30745779Sxy150489 /*
30755779Sxy150489  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
30765779Sxy150489  */
30775779Sxy150489 static void
30785779Sxy150489 igb_set_internal_serdes_loopback(igb_t *igb)
30795779Sxy150489 {
30805779Sxy150489 	struct e1000_hw *hw;
30815779Sxy150489 	uint32_t ctrl_ext;
30825779Sxy150489 	uint32_t ctrl;
30835779Sxy150489 	uint32_t pcs_lctl;
30845779Sxy150489 	uint32_t connsw;
30855779Sxy150489 
30865779Sxy150489 	hw = &igb->hw;
30875779Sxy150489 
30885779Sxy150489 	/* Set link mode to SerDes (11b) in the Extended Control register */
30895779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
30905779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
30915779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
30925779Sxy150489 
30935779Sxy150489 	/* Configure the SerDes to loopback */
30945779Sxy150489 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
30955779Sxy150489 
30965779Sxy150489 	/* Set Device Control register */
30975779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
30985779Sxy150489 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
30995779Sxy150489 	    E1000_CTRL_SLU);		/* Force link up */
31005779Sxy150489 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
31015779Sxy150489 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
31025779Sxy150489 	    E1000_CTRL_LRST);		/* Clear link reset */
31035779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
31045779Sxy150489 
31055779Sxy150489 	/* Set PCS Link Control register */
31065779Sxy150489 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
31075779Sxy150489 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
31085779Sxy150489 	    E1000_PCS_LCTL_FSD |
31095779Sxy150489 	    E1000_PCS_LCTL_FDV_FULL |
31105779Sxy150489 	    E1000_PCS_LCTL_FLV_LINK_UP);
31115779Sxy150489 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
31125779Sxy150489 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
31135779Sxy150489 
31145779Sxy150489 	/* Set the Copper/Fiber Switch Control - CONNSW register */
31155779Sxy150489 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
31165779Sxy150489 	connsw &= ~E1000_CONNSW_ENRGSRC;
31175779Sxy150489 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
31185779Sxy150489 }
31195779Sxy150489 
31205779Sxy150489 #pragma inline(igb_intr_rx_work)
31215779Sxy150489 /*
31225779Sxy150489  * igb_intr_rx_work - rx processing of ISR
31235779Sxy150489  */
31245779Sxy150489 static void
31255779Sxy150489 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
31265779Sxy150489 {
31275779Sxy150489 	mblk_t *mp;
31285779Sxy150489 
31295779Sxy150489 	mutex_enter(&rx_ring->rx_lock);
31305779Sxy150489 	mp = igb_rx(rx_ring);
31315779Sxy150489 	mutex_exit(&rx_ring->rx_lock);
31325779Sxy150489 
31335779Sxy150489 	if (mp != NULL)
31345779Sxy150489 		mac_rx(rx_ring->igb->mac_hdl, NULL, mp);
31355779Sxy150489 }
31365779Sxy150489 
31375779Sxy150489 #pragma inline(igb_intr_tx_work)
31385779Sxy150489 /*
31395779Sxy150489  * igb_intr_tx_work - tx processing of ISR
31405779Sxy150489  */
31415779Sxy150489 static void
31425779Sxy150489 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
31435779Sxy150489 {
31445779Sxy150489 	/* Recycle the tx descriptors */
31455779Sxy150489 	tx_ring->tx_recycle(tx_ring);
31465779Sxy150489 
31475779Sxy150489 	/* Schedule the re-transmit */
31485779Sxy150489 	if (tx_ring->reschedule &&
31495779Sxy150489 	    (tx_ring->tbd_free >= tx_ring->resched_thresh)) {
31505779Sxy150489 		tx_ring->reschedule = B_FALSE;
31515779Sxy150489 		mac_tx_update(tx_ring->igb->mac_hdl);
31525779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
31535779Sxy150489 	}
31545779Sxy150489 }
31555779Sxy150489 
31565779Sxy150489 #pragma inline(igb_intr_other_work)
31575779Sxy150489 /*
31585779Sxy150489  * igb_intr_other_work - other processing of ISR
31595779Sxy150489  */
31605779Sxy150489 static void
31615779Sxy150489 igb_intr_other_work(igb_t *igb)
31625779Sxy150489 {
31635779Sxy150489 	boolean_t link_changed;
31645779Sxy150489 
31655779Sxy150489 	igb_stop_watchdog_timer(igb);
31665779Sxy150489 
31675779Sxy150489 	mutex_enter(&igb->gen_lock);
31685779Sxy150489 
31695779Sxy150489 	/*
31705779Sxy150489 	 * Because we got a link-status-change interrupt, force
31715779Sxy150489 	 * e1000_check_for_link() to look at phy
31725779Sxy150489 	 */
31735779Sxy150489 	igb->hw.mac.get_link_status = B_TRUE;
31745779Sxy150489 
31755779Sxy150489 	/* igb_link_check takes care of link status change */
31765779Sxy150489 	link_changed = igb_link_check(igb);
31775779Sxy150489 
31785779Sxy150489 	/* Get new phy state */
31795779Sxy150489 	igb_get_phy_state(igb);
31805779Sxy150489 
31815779Sxy150489 	mutex_exit(&igb->gen_lock);
31825779Sxy150489 
31835779Sxy150489 	if (link_changed)
31845779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
31855779Sxy150489 
31865779Sxy150489 	igb_start_watchdog_timer(igb);
31875779Sxy150489 }
31885779Sxy150489 
31895779Sxy150489 /*
31905779Sxy150489  * igb_intr_legacy - Interrupt handler for legacy interrupts
31915779Sxy150489  */
31925779Sxy150489 static uint_t
31935779Sxy150489 igb_intr_legacy(void *arg1, void *arg2)
31945779Sxy150489 {
31955779Sxy150489 	igb_t *igb = (igb_t *)arg1;
31965779Sxy150489 	igb_tx_ring_t *tx_ring;
31975779Sxy150489 	uint32_t icr;
31985779Sxy150489 	mblk_t *mp;
31995779Sxy150489 	boolean_t tx_reschedule;
32005779Sxy150489 	boolean_t link_changed;
32015779Sxy150489 	uint_t result;
32025779Sxy150489 
32035779Sxy150489 	_NOTE(ARGUNUSED(arg2));
32045779Sxy150489 
32055779Sxy150489 	mutex_enter(&igb->gen_lock);
32065779Sxy150489 
32075779Sxy150489 	if (igb->igb_state & IGB_SUSPENDED) {
32085779Sxy150489 		mutex_exit(&igb->gen_lock);
32095779Sxy150489 		return (DDI_INTR_UNCLAIMED);
32105779Sxy150489 	}
32115779Sxy150489 
32125779Sxy150489 	mp = NULL;
32135779Sxy150489 	tx_reschedule = B_FALSE;
32145779Sxy150489 	link_changed = B_FALSE;
32155779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
32165779Sxy150489 
32175779Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
32185779Sxy150489 		/*
32195779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
32205779Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
32215779Sxy150489 		 * look for work to do.
32225779Sxy150489 		 */
32235779Sxy150489 		ASSERT(igb->num_rx_rings == 1);
32245779Sxy150489 		ASSERT(igb->num_tx_rings == 1);
32255779Sxy150489 
32265779Sxy150489 		if (icr & E1000_ICR_RXT0) {
32275779Sxy150489 			mp = igb_rx(&igb->rx_rings[0]);
32285779Sxy150489 		}
32295779Sxy150489 
32305779Sxy150489 		if (icr & E1000_ICR_TXDW) {
32315779Sxy150489 			tx_ring = &igb->tx_rings[0];
32325779Sxy150489 
32335779Sxy150489 			/* Recycle the tx descriptors */
32345779Sxy150489 			tx_ring->tx_recycle(tx_ring);
32355779Sxy150489 
32365779Sxy150489 			/* Schedule the re-transmit */
32375779Sxy150489 			tx_reschedule = (tx_ring->reschedule &&
32385779Sxy150489 			    (tx_ring->tbd_free >= tx_ring->resched_thresh));
32395779Sxy150489 		}
32405779Sxy150489 
32415779Sxy150489 		if (icr & E1000_ICR_LSC) {
32425779Sxy150489 			/*
32435779Sxy150489 			 * Because we got a link-status-change interrupt, force
32445779Sxy150489 			 * e1000_check_for_link() to look at phy
32455779Sxy150489 			 */
32465779Sxy150489 			igb->hw.mac.get_link_status = B_TRUE;
32475779Sxy150489 
32485779Sxy150489 			/* igb_link_check takes care of link status change */
32495779Sxy150489 			link_changed = igb_link_check(igb);
32505779Sxy150489 
32515779Sxy150489 			/* Get new phy state */
32525779Sxy150489 			igb_get_phy_state(igb);
32535779Sxy150489 		}
32545779Sxy150489 
32555779Sxy150489 		result = DDI_INTR_CLAIMED;
32565779Sxy150489 	} else {
32575779Sxy150489 		/*
32585779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
32595779Sxy150489 		 * Don't claim this interrupt.
32605779Sxy150489 		 */
32615779Sxy150489 		result = DDI_INTR_UNCLAIMED;
32625779Sxy150489 	}
32635779Sxy150489 
32645779Sxy150489 	mutex_exit(&igb->gen_lock);
32655779Sxy150489 
32665779Sxy150489 	/*
32675779Sxy150489 	 * Do the following work outside of the gen_lock
32685779Sxy150489 	 */
32695779Sxy150489 	if (mp != NULL)
32705779Sxy150489 		mac_rx(igb->mac_hdl, NULL, mp);
32715779Sxy150489 
32725779Sxy150489 	if (tx_reschedule)  {
32735779Sxy150489 		tx_ring->reschedule = B_FALSE;
32745779Sxy150489 		mac_tx_update(igb->mac_hdl);
32755779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
32765779Sxy150489 	}
32775779Sxy150489 
32785779Sxy150489 	if (link_changed)
32795779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
32805779Sxy150489 
32815779Sxy150489 	return (result);
32825779Sxy150489 }
32835779Sxy150489 
32845779Sxy150489 /*
32855779Sxy150489  * igb_intr_msi - Interrupt handler for MSI
32865779Sxy150489  */
32875779Sxy150489 static uint_t
32885779Sxy150489 igb_intr_msi(void *arg1, void *arg2)
32895779Sxy150489 {
32905779Sxy150489 	igb_t *igb = (igb_t *)arg1;
32915779Sxy150489 	uint32_t icr;
32925779Sxy150489 
32935779Sxy150489 	_NOTE(ARGUNUSED(arg2));
32945779Sxy150489 
32955779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
32965779Sxy150489 
32975779Sxy150489 	/*
32985779Sxy150489 	 * For MSI interrupt, we have only one vector,
32995779Sxy150489 	 * so we have only one rx ring and one tx ring enabled.
33005779Sxy150489 	 */
33015779Sxy150489 	ASSERT(igb->num_rx_rings == 1);
33025779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
33035779Sxy150489 
33045779Sxy150489 	if (icr & E1000_ICR_RXT0) {
33055779Sxy150489 		igb_intr_rx_work(&igb->rx_rings[0]);
33065779Sxy150489 	}
33075779Sxy150489 
33085779Sxy150489 	if (icr & E1000_ICR_TXDW) {
33095779Sxy150489 		igb_intr_tx_work(&igb->tx_rings[0]);
33105779Sxy150489 	}
33115779Sxy150489 
33125779Sxy150489 	if (icr & E1000_ICR_LSC) {
33135779Sxy150489 		igb_intr_other_work(igb);
33145779Sxy150489 	}
33155779Sxy150489 
33165779Sxy150489 	return (DDI_INTR_CLAIMED);
33175779Sxy150489 }
33185779Sxy150489 
33195779Sxy150489 /*
33205779Sxy150489  * igb_intr_rx - Interrupt handler for rx
33215779Sxy150489  */
33225779Sxy150489 static uint_t
33235779Sxy150489 igb_intr_rx(void *arg1, void *arg2)
33245779Sxy150489 {
33255779Sxy150489 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
33265779Sxy150489 
33275779Sxy150489 	_NOTE(ARGUNUSED(arg2));
33285779Sxy150489 
33295779Sxy150489 	/*
33305779Sxy150489 	 * Only used via MSI-X vector so don't check cause bits
33315779Sxy150489 	 * and only clean the given ring.
33325779Sxy150489 	 */
33335779Sxy150489 	igb_intr_rx_work(rx_ring);
33345779Sxy150489 
33355779Sxy150489 	return (DDI_INTR_CLAIMED);
33365779Sxy150489 }
33375779Sxy150489 
33385779Sxy150489 /*
33395779Sxy150489  * igb_intr_tx_other - Interrupt handler for both tx and other
33405779Sxy150489  *
33415779Sxy150489  * Always look for Tx cleanup work.  Only look for other work if the right
33425779Sxy150489  * bits are set in the Interrupt Cause Register.
33435779Sxy150489  */
33445779Sxy150489 static uint_t
33455779Sxy150489 igb_intr_tx_other(void *arg1, void *arg2)
33465779Sxy150489 {
33475779Sxy150489 	igb_t *igb = (igb_t *)arg1;
33485779Sxy150489 	uint32_t icr;
33495779Sxy150489 
33505779Sxy150489 	_NOTE(ARGUNUSED(arg2));
33515779Sxy150489 
33525779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
33535779Sxy150489 
33545779Sxy150489 	/*
33555779Sxy150489 	 * Always look for Tx cleanup work.  We don't have separate
33565779Sxy150489 	 * transmit vectors, so we have only one tx ring enabled.
33575779Sxy150489 	 */
33585779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
33595779Sxy150489 	igb_intr_tx_work(&igb->tx_rings[0]);
33605779Sxy150489 
33615779Sxy150489 	/*
33625779Sxy150489 	 * Check for "other" causes.
33635779Sxy150489 	 */
33645779Sxy150489 	if (icr & E1000_ICR_LSC) {
33655779Sxy150489 		igb_intr_other_work(igb);
33665779Sxy150489 	}
33675779Sxy150489 
33685779Sxy150489 	return (DDI_INTR_CLAIMED);
33695779Sxy150489 }
33705779Sxy150489 
33715779Sxy150489 /*
33725779Sxy150489  * igb_alloc_intrs - Allocate interrupts for the driver
33735779Sxy150489  *
33745779Sxy150489  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
33755779Sxy150489  * if not successful, try Legacy.
33765779Sxy150489  * igb->intr_force can be used to force sequence to start with
33775779Sxy150489  * any of the 3 types.
33785779Sxy150489  * If MSI-X is not used, number of tx/rx rings is forced to 1.
33795779Sxy150489  */
33805779Sxy150489 static int
33815779Sxy150489 igb_alloc_intrs(igb_t *igb)
33825779Sxy150489 {
33835779Sxy150489 	dev_info_t *devinfo;
33845779Sxy150489 	int intr_types;
33855779Sxy150489 	int rc;
33865779Sxy150489 
33875779Sxy150489 	devinfo = igb->dip;
33885779Sxy150489 
33895779Sxy150489 	/* Get supported interrupt types */
33905779Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
33915779Sxy150489 
33925779Sxy150489 	if (rc != DDI_SUCCESS) {
33935779Sxy150489 		igb_log(igb,
33945779Sxy150489 		    "Get supported interrupt types failed: %d", rc);
33955779Sxy150489 		return (IGB_FAILURE);
33965779Sxy150489 	}
33975779Sxy150489 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
33985779Sxy150489 
33995779Sxy150489 	igb->intr_type = 0;
34005779Sxy150489 
34015779Sxy150489 	/* Install MSI-X interrupts */
34025779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
34035779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSIX)) {
3404*7072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSIX);
34055779Sxy150489 
34065779Sxy150489 		if (rc == IGB_SUCCESS)
34075779Sxy150489 			return (IGB_SUCCESS);
34085779Sxy150489 
34095779Sxy150489 		igb_log(igb,
34105779Sxy150489 		    "Allocate MSI-X failed, trying MSI interrupts...");
34115779Sxy150489 	}
34125779Sxy150489 
34135779Sxy150489 	/* MSI-X not used, force rings to 1 */
34145779Sxy150489 	igb->num_rx_rings = 1;
34155779Sxy150489 	igb->num_tx_rings = 1;
34165779Sxy150489 	igb_log(igb,
34175779Sxy150489 	    "MSI-X not used, force rx and tx queue number to 1");
34185779Sxy150489 
34195779Sxy150489 	/* Install MSI interrupts */
34205779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
34215779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSI)) {
3422*7072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSI);
34235779Sxy150489 
34245779Sxy150489 		if (rc == IGB_SUCCESS)
34255779Sxy150489 			return (IGB_SUCCESS);
34265779Sxy150489 
34275779Sxy150489 		igb_log(igb,
34285779Sxy150489 		    "Allocate MSI failed, trying Legacy interrupts...");
34295779Sxy150489 	}
34305779Sxy150489 
34315779Sxy150489 	/* Install legacy interrupts */
34325779Sxy150489 	if (intr_types & DDI_INTR_TYPE_FIXED) {
3433*7072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_FIXED);
34345779Sxy150489 
34355779Sxy150489 		if (rc == IGB_SUCCESS)
34365779Sxy150489 			return (IGB_SUCCESS);
34375779Sxy150489 
34385779Sxy150489 		igb_log(igb,
34395779Sxy150489 		    "Allocate Legacy interrupts failed");
34405779Sxy150489 	}
34415779Sxy150489 
34425779Sxy150489 	/* If none of the 3 types succeeded, return failure */
34435779Sxy150489 	return (IGB_FAILURE);
34445779Sxy150489 }
34455779Sxy150489 
34465779Sxy150489 /*
3447*7072Sxy150489  * igb_alloc_intr_handles - Allocate interrupt handles.
34485779Sxy150489  *
3449*7072Sxy150489  * For legacy and MSI, only 1 handle is needed.  For MSI-X,
3450*7072Sxy150489  * if fewer than 2 handles are available, return failure.
34515779Sxy150489  * Upon success, this sets the number of Rx rings to a number that
3452*7072Sxy150489  * matches the handles available for Rx interrupts.
34535779Sxy150489  */
34545779Sxy150489 static int
3455*7072Sxy150489 igb_alloc_intr_handles(igb_t *igb, int intr_type)
34565779Sxy150489 {
34575779Sxy150489 	dev_info_t *devinfo;
34585779Sxy150489 	int request, count, avail, actual;
3459*7072Sxy150489 	int rx_rings, minimum;
34605779Sxy150489 	int rc;
34615779Sxy150489 
34625779Sxy150489 	devinfo = igb->dip;
34635779Sxy150489 
34645779Sxy150489 	/*
34655779Sxy150489 	 * Currently only 1 tx ring is supported. More tx rings
34665779Sxy150489 	 * will be supported with future enhancement.
34675779Sxy150489 	 */
34685779Sxy150489 	if (igb->num_tx_rings > 1) {
34695779Sxy150489 		igb->num_tx_rings = 1;
34705779Sxy150489 		igb_log(igb,
34715779Sxy150489 		    "Use only 1 MSI-X vector for tx, "
34725779Sxy150489 		    "force tx queue number to 1");
34735779Sxy150489 	}
34745779Sxy150489 
3475*7072Sxy150489 	switch (intr_type) {
3476*7072Sxy150489 	case DDI_INTR_TYPE_FIXED:
3477*7072Sxy150489 		request = 1;	/* Request 1 legacy interrupt handle */
3478*7072Sxy150489 		minimum = 1;
3479*7072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: legacy");
3480*7072Sxy150489 		break;
3481*7072Sxy150489 
3482*7072Sxy150489 	case DDI_INTR_TYPE_MSI:
3483*7072Sxy150489 		request = 1;	/* Request 1 MSI interrupt handle */
3484*7072Sxy150489 		minimum = 1;
3485*7072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI");
3486*7072Sxy150489 		break;
3487*7072Sxy150489 
3488*7072Sxy150489 	case DDI_INTR_TYPE_MSIX:
3489*7072Sxy150489 		/*
3490*7072Sxy150489 		 * Best number of vectors for the adapter is
3491*7072Sxy150489 		 * # rx rings + # tx rings + 1 for other
3492*7072Sxy150489 		 * But currently we only support number of vectors of
3493*7072Sxy150489 		 * # rx rings + 1 for tx & other
3494*7072Sxy150489 		 */
3495*7072Sxy150489 		request = igb->num_rx_rings + 1;
3496*7072Sxy150489 		minimum = 2;
3497*7072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI-X");
3498*7072Sxy150489 		break;
3499*7072Sxy150489 
3500*7072Sxy150489 	default:
35015779Sxy150489 		igb_log(igb,
3502*7072Sxy150489 		    "invalid call to igb_alloc_intr_handles(): %d\n",
3503*7072Sxy150489 		    intr_type);
35045779Sxy150489 		return (IGB_FAILURE);
35055779Sxy150489 	}
3506*7072Sxy150489 	IGB_DEBUGLOG_2(igb, "interrupt handles requested: %d  minimum: %d",
3507*7072Sxy150489 	    request, minimum);
3508*7072Sxy150489 
3509*7072Sxy150489 	/*
3510*7072Sxy150489 	 * Get number of supported interrupts
3511*7072Sxy150489 	 */
3512*7072Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
3513*7072Sxy150489 	if ((rc != DDI_SUCCESS) || (count < minimum)) {
35145779Sxy150489 		igb_log(igb,
3515*7072Sxy150489 		    "Get supported interrupt number failed. "
3516*7072Sxy150489 		    "Return: %d, count: %d", rc, count);
3517*7072Sxy150489 		return (IGB_FAILURE);
3518*7072Sxy150489 	}
3519*7072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts supported: %d", count);
3520*7072Sxy150489 
3521*7072Sxy150489 	/*
3522*7072Sxy150489 	 * Get number of available interrupts
3523*7072Sxy150489 	 */
3524*7072Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
3525*7072Sxy150489 	if ((rc != DDI_SUCCESS) || (avail < minimum)) {
3526*7072Sxy150489 		igb_log(igb,
3527*7072Sxy150489 		    "Get available interrupt number failed. "
35285779Sxy150489 		    "Return: %d, available: %d", rc, avail);
35295779Sxy150489 		return (IGB_FAILURE);
35305779Sxy150489 	}
3531*7072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts available: %d", avail);
35325779Sxy150489 
35335779Sxy150489 	if (avail < request) {
3534*7072Sxy150489 		igb_log(igb, "Request %d handles, %d available",
35355779Sxy150489 		    request, avail);
35365779Sxy150489 		request = avail;
35375779Sxy150489 	}
35385779Sxy150489 
35395779Sxy150489 	actual = 0;
35405779Sxy150489 	igb->intr_cnt = 0;
35415779Sxy150489 
3542*7072Sxy150489 	/*
3543*7072Sxy150489 	 * Allocate an array of interrupt handles
3544*7072Sxy150489 	 */
35455779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
35465779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
35475779Sxy150489 
3548*7072Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, intr_type, 0,
35495779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
35505779Sxy150489 	if (rc != DDI_SUCCESS) {
3551*7072Sxy150489 		igb_log(igb, "Allocate interrupts failed. "
35525779Sxy150489 		    "return: %d, request: %d, actual: %d",
35535779Sxy150489 		    rc, request, actual);
3554*7072Sxy150489 		goto alloc_handle_fail;
35555779Sxy150489 	}
3556*7072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts actually allocated: %d", actual);
35575779Sxy150489 
35585779Sxy150489 	igb->intr_cnt = actual;
35595779Sxy150489 
3560*7072Sxy150489 	if (actual < minimum) {
3561*7072Sxy150489 		igb_log(igb, "Insufficient interrupt handles allocated: %d",
3562*7072Sxy150489 		    actual);
3563*7072Sxy150489 		goto alloc_handle_fail;
3564*7072Sxy150489 	}
3565*7072Sxy150489 
35665779Sxy150489 	/*
3567*7072Sxy150489 	 * For MSI-X, actual might force us to reduce number of rx rings
35685779Sxy150489 	 */
3569*7072Sxy150489 	if (intr_type == DDI_INTR_TYPE_MSIX) {
3570*7072Sxy150489 		rx_rings = actual - 1;
3571*7072Sxy150489 		if (rx_rings < igb->num_rx_rings) {
3572*7072Sxy150489 			igb_log(igb,
3573*7072Sxy150489 			    "MSI-X vectors force Rx queue number to %d",
3574*7072Sxy150489 			    rx_rings);
3575*7072Sxy150489 			igb->num_rx_rings = rx_rings;
3576*7072Sxy150489 		}
35775779Sxy150489 	}
35785779Sxy150489 
3579*7072Sxy150489 	/*
3580*7072Sxy150489 	 * Get priority for first vector, assume remaining are all the same
3581*7072Sxy150489 	 */
35825779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
35835779Sxy150489 	if (rc != DDI_SUCCESS) {
35845779Sxy150489 		igb_log(igb,
35855779Sxy150489 		    "Get interrupt priority failed: %d", rc);
3586*7072Sxy150489 		goto alloc_handle_fail;
35875779Sxy150489 	}
35885779Sxy150489 
35895779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
35905779Sxy150489 	if (rc != DDI_SUCCESS) {
35915779Sxy150489 		igb_log(igb,
35925779Sxy150489 		    "Get interrupt cap failed: %d", rc);
3593*7072Sxy150489 		goto alloc_handle_fail;
35945779Sxy150489 	}
35955779Sxy150489 
3596*7072Sxy150489 	igb->intr_type = intr_type;
35975779Sxy150489 
35985779Sxy150489 	return (IGB_SUCCESS);
35995779Sxy150489 
3600*7072Sxy150489 alloc_handle_fail:
36015779Sxy150489 	igb_rem_intrs(igb);
36025779Sxy150489 
36035779Sxy150489 	return (IGB_FAILURE);
36045779Sxy150489 }
36055779Sxy150489 
36065779Sxy150489 /*
36075779Sxy150489  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
36085779Sxy150489  *
36095779Sxy150489  * Before adding the interrupt handlers, the interrupt vectors have
36105779Sxy150489  * been allocated, and the rx/tx rings have also been allocated.
36115779Sxy150489  */
36125779Sxy150489 static int
36135779Sxy150489 igb_add_intr_handlers(igb_t *igb)
36145779Sxy150489 {
36155779Sxy150489 	igb_rx_ring_t *rx_ring;
36165779Sxy150489 	int vector;
36175779Sxy150489 	int rc;
36185779Sxy150489 	int i;
36195779Sxy150489 
36205779Sxy150489 	vector = 0;
36215779Sxy150489 
36225779Sxy150489 	switch (igb->intr_type) {
36235779Sxy150489 	case DDI_INTR_TYPE_MSIX:
36245779Sxy150489 		/* Add interrupt handler for tx + other */
36255779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
36265779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_tx_other,
36275779Sxy150489 		    (void *)igb, NULL);
36285779Sxy150489 		if (rc != DDI_SUCCESS) {
36295779Sxy150489 			igb_log(igb,
36305779Sxy150489 			    "Add tx/other interrupt handler failed: %d", rc);
36315779Sxy150489 			return (IGB_FAILURE);
36325779Sxy150489 		}
36335779Sxy150489 		vector++;
36345779Sxy150489 
36355779Sxy150489 		/* Add interrupt handler for each rx ring */
36365779Sxy150489 		for (i = 0; i < igb->num_rx_rings; i++) {
36375779Sxy150489 			rx_ring = &igb->rx_rings[i];
36385779Sxy150489 
36395779Sxy150489 			rc = ddi_intr_add_handler(igb->htable[vector],
36405779Sxy150489 			    (ddi_intr_handler_t *)igb_intr_rx,
36415779Sxy150489 			    (void *)rx_ring, NULL);
36425779Sxy150489 
36435779Sxy150489 			if (rc != DDI_SUCCESS) {
36445779Sxy150489 				igb_log(igb,
36455779Sxy150489 				    "Add rx interrupt handler failed. "
36465779Sxy150489 				    "return: %d, rx ring: %d", rc, i);
36475779Sxy150489 				for (vector--; vector >= 0; vector--) {
36485779Sxy150489 					(void) ddi_intr_remove_handler(
36495779Sxy150489 					    igb->htable[vector]);
36505779Sxy150489 				}
36515779Sxy150489 				return (IGB_FAILURE);
36525779Sxy150489 			}
36535779Sxy150489 
36545779Sxy150489 			rx_ring->intr_vector = vector;
36555779Sxy150489 
36565779Sxy150489 			vector++;
36575779Sxy150489 		}
36585779Sxy150489 		break;
36595779Sxy150489 
36605779Sxy150489 	case DDI_INTR_TYPE_MSI:
36615779Sxy150489 		/* Add interrupt handlers for the only vector */
36625779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
36635779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_msi,
36645779Sxy150489 		    (void *)igb, NULL);
36655779Sxy150489 
36665779Sxy150489 		if (rc != DDI_SUCCESS) {
36675779Sxy150489 			igb_log(igb,
36685779Sxy150489 			    "Add MSI interrupt handler failed: %d", rc);
36695779Sxy150489 			return (IGB_FAILURE);
36705779Sxy150489 		}
36715779Sxy150489 
36725779Sxy150489 		rx_ring = &igb->rx_rings[0];
36735779Sxy150489 		rx_ring->intr_vector = vector;
36745779Sxy150489 
36755779Sxy150489 		vector++;
36765779Sxy150489 		break;
36775779Sxy150489 
36785779Sxy150489 	case DDI_INTR_TYPE_FIXED:
36795779Sxy150489 		/* Add interrupt handlers for the only vector */
36805779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
36815779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_legacy,
36825779Sxy150489 		    (void *)igb, NULL);
36835779Sxy150489 
36845779Sxy150489 		if (rc != DDI_SUCCESS) {
36855779Sxy150489 			igb_log(igb,
36865779Sxy150489 			    "Add legacy interrupt handler failed: %d", rc);
36875779Sxy150489 			return (IGB_FAILURE);
36885779Sxy150489 		}
36895779Sxy150489 
36905779Sxy150489 		rx_ring = &igb->rx_rings[0];
36915779Sxy150489 		rx_ring->intr_vector = vector;
36925779Sxy150489 
36935779Sxy150489 		vector++;
36945779Sxy150489 		break;
36955779Sxy150489 
36965779Sxy150489 	default:
36975779Sxy150489 		return (IGB_FAILURE);
36985779Sxy150489 	}
36995779Sxy150489 
37005779Sxy150489 	ASSERT(vector == igb->intr_cnt);
37015779Sxy150489 
37025779Sxy150489 	return (IGB_SUCCESS);
37035779Sxy150489 }
37045779Sxy150489 
37055779Sxy150489 /*
37065779Sxy150489  * igb_setup_adapter_msix - setup the adapter to use MSI-X interrupts
37075779Sxy150489  *
37085779Sxy150489  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
37095779Sxy150489  */
37105779Sxy150489 static void
37115779Sxy150489 igb_setup_adapter_msix(igb_t *igb)
37125779Sxy150489 {
37135779Sxy150489 	uint32_t eims = 0;
37145779Sxy150489 	int i, vector;
37155779Sxy150489 	struct e1000_hw *hw = &igb->hw;
37165779Sxy150489 
37175779Sxy150489 	/*
37185779Sxy150489 	 * Set vector for Tx + Other causes
37195779Sxy150489 	 * NOTE assumption that there is only one of these and it is vector 0
37205779Sxy150489 	 */
37215779Sxy150489 	vector = 0;
37225779Sxy150489 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
37235779Sxy150489 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
37245779Sxy150489 
37255779Sxy150489 	vector++;
37265779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
37275779Sxy150489 		/*
37285779Sxy150489 		 * Set vector for each rx ring
37295779Sxy150489 		 */
37305779Sxy150489 		eims = (E1000_EICR_RX_QUEUE0 << i);
37315779Sxy150489 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
37325779Sxy150489 
37335779Sxy150489 		/*
37345779Sxy150489 		 * Accumulate bits to enable in igb_enable_adapter_interrupts()
37355779Sxy150489 		 */
37365779Sxy150489 		igb->eims_mask |= eims;
37375779Sxy150489 
37385779Sxy150489 		vector++;
37395779Sxy150489 	}
37405779Sxy150489 
37415779Sxy150489 	ASSERT(vector == igb->intr_cnt);
37425779Sxy150489 
37435779Sxy150489 	/*
37445779Sxy150489 	 * Disable IAM for ICR interrupt bits
37455779Sxy150489 	 */
37465779Sxy150489 	E1000_WRITE_REG(hw, E1000_IAM, 0);
37475779Sxy150489 	E1000_WRITE_FLUSH(hw);
37485779Sxy150489 }
37495779Sxy150489 
37505779Sxy150489 /*
37515779Sxy150489  * igb_rem_intr_handlers - remove the interrupt handlers
37525779Sxy150489  */
37535779Sxy150489 static void
37545779Sxy150489 igb_rem_intr_handlers(igb_t *igb)
37555779Sxy150489 {
37565779Sxy150489 	int i;
37575779Sxy150489 	int rc;
37585779Sxy150489 
37595779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
37605779Sxy150489 		rc = ddi_intr_remove_handler(igb->htable[i]);
37615779Sxy150489 		if (rc != DDI_SUCCESS) {
37625779Sxy150489 			IGB_DEBUGLOG_1(igb,
37635779Sxy150489 			    "Remove intr handler failed: %d", rc);
37645779Sxy150489 		}
37655779Sxy150489 	}
37665779Sxy150489 }
37675779Sxy150489 
37685779Sxy150489 /*
37695779Sxy150489  * igb_rem_intrs - remove the allocated interrupts
37705779Sxy150489  */
37715779Sxy150489 static void
37725779Sxy150489 igb_rem_intrs(igb_t *igb)
37735779Sxy150489 {
37745779Sxy150489 	int i;
37755779Sxy150489 	int rc;
37765779Sxy150489 
37775779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
37785779Sxy150489 		rc = ddi_intr_free(igb->htable[i]);
37795779Sxy150489 		if (rc != DDI_SUCCESS) {
37805779Sxy150489 			IGB_DEBUGLOG_1(igb,
37815779Sxy150489 			    "Free intr failed: %d", rc);
37825779Sxy150489 		}
37835779Sxy150489 	}
37845779Sxy150489 
37855779Sxy150489 	kmem_free(igb->htable, igb->intr_size);
37865779Sxy150489 	igb->htable = NULL;
37875779Sxy150489 }
37885779Sxy150489 
37895779Sxy150489 /*
37905779Sxy150489  * igb_enable_intrs - enable all the ddi interrupts
37915779Sxy150489  */
37925779Sxy150489 static int
37935779Sxy150489 igb_enable_intrs(igb_t *igb)
37945779Sxy150489 {
37955779Sxy150489 	int i;
37965779Sxy150489 	int rc;
37975779Sxy150489 
37985779Sxy150489 	/* Enable interrupts */
37995779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
38005779Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
38015779Sxy150489 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
38025779Sxy150489 		if (rc != DDI_SUCCESS) {
38035779Sxy150489 			igb_log(igb,
38045779Sxy150489 			    "Enable block intr failed: %d", rc);
38055779Sxy150489 			return (IGB_FAILURE);
38065779Sxy150489 		}
38075779Sxy150489 	} else {
38085779Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
38095779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
38105779Sxy150489 			rc = ddi_intr_enable(igb->htable[i]);
38115779Sxy150489 			if (rc != DDI_SUCCESS) {
38125779Sxy150489 				igb_log(igb,
38135779Sxy150489 				    "Enable intr failed: %d", rc);
38145779Sxy150489 				return (IGB_FAILURE);
38155779Sxy150489 			}
38165779Sxy150489 		}
38175779Sxy150489 	}
38185779Sxy150489 
38195779Sxy150489 	return (IGB_SUCCESS);
38205779Sxy150489 }
38215779Sxy150489 
38225779Sxy150489 /*
38235779Sxy150489  * igb_disable_intrs - disable all the ddi interrupts
38245779Sxy150489  */
38255779Sxy150489 static int
38265779Sxy150489 igb_disable_intrs(igb_t *igb)
38275779Sxy150489 {
38285779Sxy150489 	int i;
38295779Sxy150489 	int rc;
38305779Sxy150489 
38315779Sxy150489 	/* Disable all interrupts */
38325779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
38335779Sxy150489 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
38345779Sxy150489 		if (rc != DDI_SUCCESS) {
38355779Sxy150489 			igb_log(igb,
38365779Sxy150489 			    "Disable block intr failed: %d", rc);
38375779Sxy150489 			return (IGB_FAILURE);
38385779Sxy150489 		}
38395779Sxy150489 	} else {
38405779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
38415779Sxy150489 			rc = ddi_intr_disable(igb->htable[i]);
38425779Sxy150489 			if (rc != DDI_SUCCESS) {
38435779Sxy150489 				igb_log(igb,
38445779Sxy150489 				    "Disable intr failed: %d", rc);
38455779Sxy150489 				return (IGB_FAILURE);
38465779Sxy150489 			}
38475779Sxy150489 		}
38485779Sxy150489 	}
38495779Sxy150489 
38505779Sxy150489 	return (IGB_SUCCESS);
38515779Sxy150489 }
38525779Sxy150489 
38535779Sxy150489 /*
38545779Sxy150489  * igb_get_phy_state - Get and save the parameters read from PHY registers
38555779Sxy150489  */
38565779Sxy150489 static void
38575779Sxy150489 igb_get_phy_state(igb_t *igb)
38585779Sxy150489 {
38595779Sxy150489 	struct e1000_hw *hw = &igb->hw;
38605779Sxy150489 	uint16_t phy_ctrl;
38615779Sxy150489 	uint16_t phy_status;
38625779Sxy150489 	uint16_t phy_an_adv;
38635779Sxy150489 	uint16_t phy_an_exp;
38645779Sxy150489 	uint16_t phy_ext_status;
38655779Sxy150489 	uint16_t phy_1000t_ctrl;
38665779Sxy150489 	uint16_t phy_1000t_status;
38675779Sxy150489 	uint16_t phy_lp_able;
38685779Sxy150489 
38695779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
38705779Sxy150489 
38715779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
38725779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
38735779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
38745779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
38755779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
38765779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
38775779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_1000t_status);
38785779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
38795779Sxy150489 
38805779Sxy150489 	igb->param_autoneg_cap =
38815779Sxy150489 	    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
38825779Sxy150489 	igb->param_pause_cap =
38835779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
38845779Sxy150489 	igb->param_asym_pause_cap =
38855779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
38865779Sxy150489 	igb->param_1000fdx_cap = ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
38875779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
38885779Sxy150489 	igb->param_1000hdx_cap = ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
38895779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
38905779Sxy150489 	igb->param_100t4_cap =
38915779Sxy150489 	    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
38925779Sxy150489 	igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
38935779Sxy150489 	    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
38945779Sxy150489 	igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
38955779Sxy150489 	    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
38965779Sxy150489 	igb->param_10fdx_cap =
38975779Sxy150489 	    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
38985779Sxy150489 	igb->param_10hdx_cap =
38995779Sxy150489 	    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
39005779Sxy150489 	igb->param_rem_fault =
39015779Sxy150489 	    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
39025779Sxy150489 
39035779Sxy150489 	igb->param_adv_autoneg_cap = hw->mac.autoneg;
39045779Sxy150489 	igb->param_adv_pause_cap =
39055779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
39065779Sxy150489 	igb->param_adv_asym_pause_cap =
39075779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
39085779Sxy150489 	igb->param_adv_1000hdx_cap =
39095779Sxy150489 	    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
39105779Sxy150489 	igb->param_adv_100t4_cap =
39115779Sxy150489 	    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
39125779Sxy150489 	igb->param_adv_rem_fault =
39135779Sxy150489 	    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
39145779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
39155779Sxy150489 		igb->param_adv_1000fdx_cap =
39165779Sxy150489 		    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
39175779Sxy150489 		igb->param_adv_100fdx_cap =
39185779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
39195779Sxy150489 		igb->param_adv_100hdx_cap =
39205779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
39215779Sxy150489 		igb->param_adv_10fdx_cap =
39225779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
39235779Sxy150489 		igb->param_adv_10hdx_cap =
39245779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
39255779Sxy150489 	}
39265779Sxy150489 
39275779Sxy150489 	igb->param_lp_autoneg_cap =
39285779Sxy150489 	    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
39295779Sxy150489 	igb->param_lp_pause_cap =
39305779Sxy150489 	    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
39315779Sxy150489 	igb->param_lp_asym_pause_cap =
39325779Sxy150489 	    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
39335779Sxy150489 	igb->param_lp_1000fdx_cap =
39345779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
39355779Sxy150489 	igb->param_lp_1000hdx_cap =
39365779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
39375779Sxy150489 	igb->param_lp_100t4_cap =
39385779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
39395779Sxy150489 	igb->param_lp_100fdx_cap =
39405779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
39415779Sxy150489 	igb->param_lp_100hdx_cap =
39425779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
39435779Sxy150489 	igb->param_lp_10fdx_cap =
39445779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
39455779Sxy150489 	igb->param_lp_10hdx_cap =
39465779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
39475779Sxy150489 	igb->param_lp_rem_fault =
39485779Sxy150489 	    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
39495779Sxy150489 }
39505779Sxy150489 
39515779Sxy150489 /*
39525779Sxy150489  * igb_get_driver_control
39535779Sxy150489  */
39545779Sxy150489 static void
39555779Sxy150489 igb_get_driver_control(struct e1000_hw *hw)
39565779Sxy150489 {
39575779Sxy150489 	uint32_t ctrl_ext;
39585779Sxy150489 
39595779Sxy150489 	/* Notify firmware that driver is in control of device */
39605779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
39615779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
39625779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
39635779Sxy150489 }
39645779Sxy150489 
39655779Sxy150489 /*
39665779Sxy150489  * igb_release_driver_control
39675779Sxy150489  */
39685779Sxy150489 static void
39695779Sxy150489 igb_release_driver_control(struct e1000_hw *hw)
39705779Sxy150489 {
39715779Sxy150489 	uint32_t ctrl_ext;
39725779Sxy150489 
39735779Sxy150489 	/* Notify firmware that driver is no longer in control of device */
39745779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
39755779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
39765779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
39775779Sxy150489 }
39785779Sxy150489 
39795779Sxy150489 /*
39805779Sxy150489  * igb_atomic_reserve - Atomic decrease operation
39815779Sxy150489  */
39825779Sxy150489 int
39835779Sxy150489 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
39845779Sxy150489 {
39855779Sxy150489 	uint32_t oldval;
39865779Sxy150489 	uint32_t newval;
39875779Sxy150489 
39885779Sxy150489 	/* ATOMICALLY */
39895779Sxy150489 	do {
39905779Sxy150489 		oldval = *count_p;
39915779Sxy150489 		if (oldval < n)
39925779Sxy150489 			return (-1);
39935779Sxy150489 		newval = oldval - n;
39945779Sxy150489 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
39955779Sxy150489 
39965779Sxy150489 	return (newval);
39975779Sxy150489 }
39986624Sgl147354 
39996624Sgl147354 /*
40006624Sgl147354  * FMA support
40016624Sgl147354  */
40026624Sgl147354 
40036624Sgl147354 int
40046624Sgl147354 igb_check_acc_handle(ddi_acc_handle_t handle)
40056624Sgl147354 {
40066624Sgl147354 	ddi_fm_error_t de;
40076624Sgl147354 
40086624Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
40096624Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
40106624Sgl147354 	return (de.fme_status);
40116624Sgl147354 }
40126624Sgl147354 
40136624Sgl147354 int
40146624Sgl147354 igb_check_dma_handle(ddi_dma_handle_t handle)
40156624Sgl147354 {
40166624Sgl147354 	ddi_fm_error_t de;
40176624Sgl147354 
40186624Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
40196624Sgl147354 	return (de.fme_status);
40206624Sgl147354 }
40216624Sgl147354 
40226624Sgl147354 /*
40236624Sgl147354  * The IO fault service error handling callback function
40246624Sgl147354  */
40256624Sgl147354 /*ARGSUSED*/
40266624Sgl147354 static int
40276624Sgl147354 igb_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
40286624Sgl147354 {
40296624Sgl147354 	/*
40306624Sgl147354 	 * as the driver can always deal with an error in any dma or
40316624Sgl147354 	 * access handle, we can just return the fme_status value.
40326624Sgl147354 	 */
40336624Sgl147354 	pci_ereport_post(dip, err, NULL);
40346624Sgl147354 	return (err->fme_status);
40356624Sgl147354 }
40366624Sgl147354 
40376624Sgl147354 static void
40386624Sgl147354 igb_fm_init(igb_t *igb)
40396624Sgl147354 {
40406624Sgl147354 	ddi_iblock_cookie_t iblk;
40416624Sgl147354 	int fma_acc_flag, fma_dma_flag;
40426624Sgl147354 
40436624Sgl147354 	/* Only register with IO Fault Services if we have some capability */
40446624Sgl147354 	if (igb->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
40456624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
40466624Sgl147354 		fma_acc_flag = 1;
40476624Sgl147354 	} else {
40486624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
40496624Sgl147354 		fma_acc_flag = 0;
40506624Sgl147354 	}
40516624Sgl147354 
40526624Sgl147354 	if (igb->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
40536624Sgl147354 		fma_dma_flag = 1;
40546624Sgl147354 	} else {
40556624Sgl147354 		fma_dma_flag = 0;
40566624Sgl147354 	}
40576624Sgl147354 
40586624Sgl147354 	(void) igb_set_fma_flags(fma_acc_flag, fma_dma_flag);
40596624Sgl147354 
40606624Sgl147354 	if (igb->fm_capabilities) {
40616624Sgl147354 
40626624Sgl147354 		/* Register capabilities with IO Fault Services */
40636624Sgl147354 		ddi_fm_init(igb->dip, &igb->fm_capabilities, &iblk);
40646624Sgl147354 
40656624Sgl147354 		/*
40666624Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
40676624Sgl147354 		 */
40686624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
40696624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
40706624Sgl147354 			pci_ereport_setup(igb->dip);
40716624Sgl147354 
40726624Sgl147354 		/*
40736624Sgl147354 		 * Register error callback if error callback capable
40746624Sgl147354 		 */
40756624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
40766624Sgl147354 			ddi_fm_handler_register(igb->dip,
40776624Sgl147354 			    igb_fm_error_cb, (void*) igb);
40786624Sgl147354 	}
40796624Sgl147354 }
40806624Sgl147354 
40816624Sgl147354 static void
40826624Sgl147354 igb_fm_fini(igb_t *igb)
40836624Sgl147354 {
40846624Sgl147354 	/* Only unregister FMA capabilities if we registered some */
40856624Sgl147354 	if (igb->fm_capabilities) {
40866624Sgl147354 
40876624Sgl147354 		/*
40886624Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
40896624Sgl147354 		 */
40906624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
40916624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
40926624Sgl147354 			pci_ereport_teardown(igb->dip);
40936624Sgl147354 
40946624Sgl147354 		/*
40956624Sgl147354 		 * Un-register error callback if error callback capable
40966624Sgl147354 		 */
40976624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
40986624Sgl147354 			ddi_fm_handler_unregister(igb->dip);
40996624Sgl147354 
41006624Sgl147354 		/* Unregister from IO Fault Services */
41016624Sgl147354 		ddi_fm_fini(igb->dip);
41026624Sgl147354 	}
41036624Sgl147354 }
41046624Sgl147354 
41056624Sgl147354 void
41066624Sgl147354 igb_fm_ereport(igb_t *igb, char *detail)
41076624Sgl147354 {
41086624Sgl147354 	uint64_t ena;
41096624Sgl147354 	char buf[FM_MAX_CLASS];
41106624Sgl147354 
41116624Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
41126624Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
41136624Sgl147354 	if (DDI_FM_EREPORT_CAP(igb->fm_capabilities)) {
41146624Sgl147354 		ddi_fm_ereport_post(igb->dip, buf, ena, DDI_NOSLEEP,
41156624Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
41166624Sgl147354 	}
41176624Sgl147354 }
4118