xref: /onnv-gate/usr/src/uts/common/io/igb/igb_main.c (revision 10319:0355b7a83c0d)
15779Sxy150489 /*
25779Sxy150489  * CDDL HEADER START
35779Sxy150489  *
48571SChenlu.Chen@Sun.COM  * Copyright(c) 2007-2009 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  *
98571SChenlu.Chen@Sun.COM  * You can obtain a copy of the license at:
108571SChenlu.Chen@Sun.COM  *	http://www.opensolaris.org/os/licensing.
115779Sxy150489  * See the License for the specific language governing permissions
125779Sxy150489  * and limitations under the License.
135779Sxy150489  *
148571SChenlu.Chen@Sun.COM  * When using or redistributing this file, you may do so under the
158571SChenlu.Chen@Sun.COM  * License only. No other modification of this header is permitted.
168571SChenlu.Chen@Sun.COM  *
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 /*
258571SChenlu.Chen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
269188SPaul.Guo@Sun.COM  * Use is subject to license terms.
275779Sxy150489  */
285779Sxy150489 
295779Sxy150489 #include "igb_sw.h"
305779Sxy150489 
317656SSherry.Moore@Sun.COM static char ident[] = "Intel 1Gb Ethernet";
32*10319SJason.Xu@Sun.COM static char igb_version[] = "igb 1.1.8";
335779Sxy150489 
345779Sxy150489 /*
355779Sxy150489  * Local function protoypes
365779Sxy150489  */
375779Sxy150489 static int igb_register_mac(igb_t *);
385779Sxy150489 static int igb_identify_hardware(igb_t *);
395779Sxy150489 static int igb_regs_map(igb_t *);
405779Sxy150489 static void igb_init_properties(igb_t *);
415779Sxy150489 static int igb_init_driver_settings(igb_t *);
425779Sxy150489 static void igb_init_locks(igb_t *);
435779Sxy150489 static void igb_destroy_locks(igb_t *);
448955SChenlu.Chen@Sun.COM static int igb_init_mac_address(igb_t *);
455779Sxy150489 static int igb_init(igb_t *);
468955SChenlu.Chen@Sun.COM static int igb_init_adapter(igb_t *);
478955SChenlu.Chen@Sun.COM static void igb_stop_adapter(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 void igb_free_rings(igb_t *);
545779Sxy150489 static void igb_setup_rings(igb_t *);
555779Sxy150489 static void igb_setup_rx(igb_t *);
565779Sxy150489 static void igb_setup_tx(igb_t *);
575779Sxy150489 static void igb_setup_rx_ring(igb_rx_ring_t *);
585779Sxy150489 static void igb_setup_tx_ring(igb_tx_ring_t *);
595779Sxy150489 static void igb_setup_rss(igb_t *);
608275SEric Cheng static void igb_setup_mac_rss_classify(igb_t *);
618275SEric Cheng static void igb_setup_mac_classify(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 *);
758571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82575(igb_t *);
768571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82576(igb_t *);
775779Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
785779Sxy150489 static boolean_t igb_stall_check(igb_t *);
795779Sxy150489 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
805779Sxy150489 static void igb_set_external_loopback(igb_t *);
815779Sxy150489 static void igb_set_internal_mac_loopback(igb_t *);
825779Sxy150489 static void igb_set_internal_phy_loopback(igb_t *);
835779Sxy150489 static void igb_set_internal_serdes_loopback(igb_t *);
845779Sxy150489 static boolean_t igb_find_mac_address(igb_t *);
855779Sxy150489 static int igb_alloc_intrs(igb_t *);
867072Sxy150489 static int igb_alloc_intr_handles(igb_t *, int);
875779Sxy150489 static int igb_add_intr_handlers(igb_t *);
885779Sxy150489 static void igb_rem_intr_handlers(igb_t *);
895779Sxy150489 static void igb_rem_intrs(igb_t *);
905779Sxy150489 static int igb_enable_intrs(igb_t *);
915779Sxy150489 static int igb_disable_intrs(igb_t *);
928571SChenlu.Chen@Sun.COM static void igb_setup_msix_82575(igb_t *);
938571SChenlu.Chen@Sun.COM static void igb_setup_msix_82576(igb_t *);
945779Sxy150489 static uint_t igb_intr_legacy(void *, void *);
955779Sxy150489 static uint_t igb_intr_msi(void *, void *);
965779Sxy150489 static uint_t igb_intr_rx(void *, void *);
978275SEric Cheng static uint_t igb_intr_tx(void *, void *);
985779Sxy150489 static uint_t igb_intr_tx_other(void *, void *);
995779Sxy150489 static void igb_intr_rx_work(igb_rx_ring_t *);
1005779Sxy150489 static void igb_intr_tx_work(igb_tx_ring_t *);
1018275SEric Cheng static void igb_intr_link_work(igb_t *);
1025779Sxy150489 static void igb_get_driver_control(struct e1000_hw *);
1035779Sxy150489 static void igb_release_driver_control(struct e1000_hw *);
1045779Sxy150489 
1055779Sxy150489 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
1065779Sxy150489 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
1075779Sxy150489 static int igb_resume(dev_info_t *);
1085779Sxy150489 static int igb_suspend(dev_info_t *);
1097656SSherry.Moore@Sun.COM static int igb_quiesce(dev_info_t *);
1105779Sxy150489 static void igb_unconfigure(dev_info_t *, igb_t *);
1116624Sgl147354 static int igb_fm_error_cb(dev_info_t *, ddi_fm_error_t *,
1126624Sgl147354     const void *);
1136624Sgl147354 static void igb_fm_init(igb_t *);
1146624Sgl147354 static void igb_fm_fini(igb_t *);
1159775SVitezslav.Batrla@Sun.COM static void igb_release_multicast(igb_t *);
1165779Sxy150489 
1175779Sxy150489 static struct cb_ops igb_cb_ops = {
1185779Sxy150489 	nulldev,		/* cb_open */
1195779Sxy150489 	nulldev,		/* cb_close */
1205779Sxy150489 	nodev,			/* cb_strategy */
1215779Sxy150489 	nodev,			/* cb_print */
1225779Sxy150489 	nodev,			/* cb_dump */
1235779Sxy150489 	nodev,			/* cb_read */
1245779Sxy150489 	nodev,			/* cb_write */
1255779Sxy150489 	nodev,			/* cb_ioctl */
1265779Sxy150489 	nodev,			/* cb_devmap */
1275779Sxy150489 	nodev,			/* cb_mmap */
1285779Sxy150489 	nodev,			/* cb_segmap */
1295779Sxy150489 	nochpoll,		/* cb_chpoll */
1305779Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1315779Sxy150489 	NULL,			/* cb_stream */
1325779Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1335779Sxy150489 	CB_REV,			/* cb_rev */
1345779Sxy150489 	nodev,			/* cb_aread */
1355779Sxy150489 	nodev			/* cb_awrite */
1365779Sxy150489 };
1375779Sxy150489 
1385779Sxy150489 static struct dev_ops igb_dev_ops = {
1395779Sxy150489 	DEVO_REV,		/* devo_rev */
1405779Sxy150489 	0,			/* devo_refcnt */
1415779Sxy150489 	NULL,			/* devo_getinfo */
1425779Sxy150489 	nulldev,		/* devo_identify */
1435779Sxy150489 	nulldev,		/* devo_probe */
1445779Sxy150489 	igb_attach,		/* devo_attach */
1455779Sxy150489 	igb_detach,		/* devo_detach */
1465779Sxy150489 	nodev,			/* devo_reset */
1475779Sxy150489 	&igb_cb_ops,		/* devo_cb_ops */
1485779Sxy150489 	NULL,			/* devo_bus_ops */
1497656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
1507656SSherry.Moore@Sun.COM 	igb_quiesce,	/* devo_quiesce */
1515779Sxy150489 };
1525779Sxy150489 
1535779Sxy150489 static struct modldrv igb_modldrv = {
1545779Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1555779Sxy150489 	ident,			/* Discription string */
1565779Sxy150489 	&igb_dev_ops,		/* driver ops */
1575779Sxy150489 };
1585779Sxy150489 
1595779Sxy150489 static struct modlinkage igb_modlinkage = {
1605779Sxy150489 	MODREV_1, &igb_modldrv, NULL
1615779Sxy150489 };
1625779Sxy150489 
1635779Sxy150489 /* Access attributes for register mapping */
1645779Sxy150489 ddi_device_acc_attr_t igb_regs_acc_attr = {
1655779Sxy150489 	DDI_DEVICE_ATTR_V0,
1665779Sxy150489 	DDI_STRUCTURE_LE_ACC,
1675779Sxy150489 	DDI_STRICTORDER_ACC,
1686624Sgl147354 	DDI_FLAGERR_ACC
1695779Sxy150489 };
1705779Sxy150489 
1715779Sxy150489 #define	IGB_M_CALLBACK_FLAGS	(MC_IOCTL | MC_GETCAPAB)
1725779Sxy150489 
1735779Sxy150489 static mac_callbacks_t igb_m_callbacks = {
1745779Sxy150489 	IGB_M_CALLBACK_FLAGS,
1755779Sxy150489 	igb_m_stat,
1765779Sxy150489 	igb_m_start,
1775779Sxy150489 	igb_m_stop,
1785779Sxy150489 	igb_m_promisc,
1795779Sxy150489 	igb_m_multicst,
1808275SEric Cheng 	NULL,
1815779Sxy150489 	NULL,
1825779Sxy150489 	igb_m_ioctl,
1835779Sxy150489 	igb_m_getcapab
1845779Sxy150489 };
1855779Sxy150489 
1865779Sxy150489 /*
1878571SChenlu.Chen@Sun.COM  * Initialize capabilities of each supported adapter type
1888571SChenlu.Chen@Sun.COM  */
1898571SChenlu.Chen@Sun.COM static adapter_info_t igb_82575_cap = {
1908571SChenlu.Chen@Sun.COM 	/* limits */
1918571SChenlu.Chen@Sun.COM 	4,		/* maximum number of rx queues */
1928571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
1938571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
1948571SChenlu.Chen@Sun.COM 	4,		/* maximum number of tx queues */
1958571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
1968571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
1978571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
1988571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
1998571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
2008571SChenlu.Chen@Sun.COM 
2018571SChenlu.Chen@Sun.COM 	/* function pointers */
2028571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82575,
2038571SChenlu.Chen@Sun.COM 	igb_setup_msix_82575,
2048571SChenlu.Chen@Sun.COM 
2058571SChenlu.Chen@Sun.COM 	/* capabilities */
2068571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
2078955SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL),
2088955SChenlu.Chen@Sun.COM 
2098955SChenlu.Chen@Sun.COM 	0xffc00000		/* mask for RXDCTL register */
2108571SChenlu.Chen@Sun.COM };
2118571SChenlu.Chen@Sun.COM 
2128571SChenlu.Chen@Sun.COM static adapter_info_t igb_82576_cap = {
2138571SChenlu.Chen@Sun.COM 	/* limits */
2148955SChenlu.Chen@Sun.COM 	16,		/* maximum number of rx queues */
2158571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
2168571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
2178955SChenlu.Chen@Sun.COM 	16,		/* maximum number of tx queues */
2188571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
2198571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
2208571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
2218571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
2228571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
2238571SChenlu.Chen@Sun.COM 
2248571SChenlu.Chen@Sun.COM 	/* function pointers */
2258571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82576,
2268571SChenlu.Chen@Sun.COM 	igb_setup_msix_82576,
2278571SChenlu.Chen@Sun.COM 
2288571SChenlu.Chen@Sun.COM 	/* capabilities */
2298571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
2308571SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL |
2318955SChenlu.Chen@Sun.COM 	IGB_FLAG_NEED_CTX_IDX),
2328955SChenlu.Chen@Sun.COM 
2338955SChenlu.Chen@Sun.COM 	0xffe00000		/* mask for RXDCTL register */
2348571SChenlu.Chen@Sun.COM };
2358571SChenlu.Chen@Sun.COM 
2368571SChenlu.Chen@Sun.COM /*
2375779Sxy150489  * Module Initialization Functions
2385779Sxy150489  */
2395779Sxy150489 
2405779Sxy150489 int
2415779Sxy150489 _init(void)
2425779Sxy150489 {
2435779Sxy150489 	int status;
2445779Sxy150489 
2455779Sxy150489 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
2465779Sxy150489 
2475779Sxy150489 	status = mod_install(&igb_modlinkage);
2485779Sxy150489 
2495779Sxy150489 	if (status != DDI_SUCCESS) {
2505779Sxy150489 		mac_fini_ops(&igb_dev_ops);
2515779Sxy150489 	}
2525779Sxy150489 
2535779Sxy150489 	return (status);
2545779Sxy150489 }
2555779Sxy150489 
2565779Sxy150489 int
2575779Sxy150489 _fini(void)
2585779Sxy150489 {
2595779Sxy150489 	int status;
2605779Sxy150489 
2615779Sxy150489 	status = mod_remove(&igb_modlinkage);
2625779Sxy150489 
2635779Sxy150489 	if (status == DDI_SUCCESS) {
2645779Sxy150489 		mac_fini_ops(&igb_dev_ops);
2655779Sxy150489 	}
2665779Sxy150489 
2675779Sxy150489 	return (status);
2685779Sxy150489 
2695779Sxy150489 }
2705779Sxy150489 
2715779Sxy150489 int
2725779Sxy150489 _info(struct modinfo *modinfop)
2735779Sxy150489 {
2745779Sxy150489 	int status;
2755779Sxy150489 
2765779Sxy150489 	status = mod_info(&igb_modlinkage, modinfop);
2775779Sxy150489 
2785779Sxy150489 	return (status);
2795779Sxy150489 }
2805779Sxy150489 
2815779Sxy150489 /*
2825779Sxy150489  * igb_attach - driver attach
2835779Sxy150489  *
2845779Sxy150489  * This function is the device specific initialization entry
2855779Sxy150489  * point. This entry point is required and must be written.
2865779Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
2875779Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
2885779Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
2895779Sxy150489  * available for use by the driver.
2905779Sxy150489  *
2915779Sxy150489  * The attach() function will be called once for each instance
2925779Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
2935779Sxy150489  * Until attach() succeeds, the only driver entry points which
2945779Sxy150489  * may be called are open(9E) and getinfo(9E).
2955779Sxy150489  */
2965779Sxy150489 static int
2975779Sxy150489 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2985779Sxy150489 {
2995779Sxy150489 	igb_t *igb;
3005779Sxy150489 	struct igb_osdep *osdep;
3015779Sxy150489 	struct e1000_hw *hw;
3025779Sxy150489 	int instance;
3035779Sxy150489 
3045779Sxy150489 	/*
3055779Sxy150489 	 * Check the command and perform corresponding operations
3065779Sxy150489 	 */
3075779Sxy150489 	switch (cmd) {
3085779Sxy150489 	default:
3095779Sxy150489 		return (DDI_FAILURE);
3105779Sxy150489 
3115779Sxy150489 	case DDI_RESUME:
3125779Sxy150489 		return (igb_resume(devinfo));
3135779Sxy150489 
3145779Sxy150489 	case DDI_ATTACH:
3155779Sxy150489 		break;
3165779Sxy150489 	}
3175779Sxy150489 
3185779Sxy150489 	/* Get the device instance */
3195779Sxy150489 	instance = ddi_get_instance(devinfo);
3205779Sxy150489 
3215779Sxy150489 	/* Allocate memory for the instance data structure */
3225779Sxy150489 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
3235779Sxy150489 
3245779Sxy150489 	igb->dip = devinfo;
3255779Sxy150489 	igb->instance = instance;
3265779Sxy150489 
3275779Sxy150489 	hw = &igb->hw;
3285779Sxy150489 	osdep = &igb->osdep;
3295779Sxy150489 	hw->back = osdep;
3305779Sxy150489 	osdep->igb = igb;
3315779Sxy150489 
3325779Sxy150489 	/* Attach the instance pointer to the dev_info data structure */
3335779Sxy150489 	ddi_set_driver_private(devinfo, igb);
3345779Sxy150489 
3356624Sgl147354 
3366624Sgl147354 	/* Initialize for fma support */
3376624Sgl147354 	igb->fm_capabilities = igb_get_prop(igb, "fm-capable",
3386624Sgl147354 	    0, 0x0f,
3396624Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
3406624Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
3416624Sgl147354 	igb_fm_init(igb);
3426624Sgl147354 	igb->attach_progress |= ATTACH_PROGRESS_FMINIT;
3436624Sgl147354 
3445779Sxy150489 	/*
3455779Sxy150489 	 * Map PCI config space registers
3465779Sxy150489 	 */
3475779Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
3485779Sxy150489 		igb_error(igb, "Failed to map PCI configurations");
3495779Sxy150489 		goto attach_fail;
3505779Sxy150489 	}
3515779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
3525779Sxy150489 
3535779Sxy150489 	/*
3545779Sxy150489 	 * Identify the chipset family
3555779Sxy150489 	 */
3565779Sxy150489 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
3575779Sxy150489 		igb_error(igb, "Failed to identify hardware");
3585779Sxy150489 		goto attach_fail;
3595779Sxy150489 	}
3605779Sxy150489 
3615779Sxy150489 	/*
3625779Sxy150489 	 * Map device registers
3635779Sxy150489 	 */
3645779Sxy150489 	if (igb_regs_map(igb) != IGB_SUCCESS) {
3655779Sxy150489 		igb_error(igb, "Failed to map device registers");
3665779Sxy150489 		goto attach_fail;
3675779Sxy150489 	}
3685779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
3695779Sxy150489 
3705779Sxy150489 	/*
3715779Sxy150489 	 * Initialize driver parameters
3725779Sxy150489 	 */
3735779Sxy150489 	igb_init_properties(igb);
3745779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
3755779Sxy150489 
3765779Sxy150489 	/*
3775779Sxy150489 	 * Allocate interrupts
3785779Sxy150489 	 */
3795779Sxy150489 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
3805779Sxy150489 		igb_error(igb, "Failed to allocate interrupts");
3815779Sxy150489 		goto attach_fail;
3825779Sxy150489 	}
3835779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
3845779Sxy150489 
3855779Sxy150489 	/*
3865779Sxy150489 	 * Allocate rx/tx rings based on the ring numbers.
3875779Sxy150489 	 * The actual numbers of rx/tx rings are decided by the number of
3885779Sxy150489 	 * allocated interrupt vectors, so we should allocate the rings after
3895779Sxy150489 	 * interrupts are allocated.
3905779Sxy150489 	 */
3915779Sxy150489 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
3928275SEric Cheng 		igb_error(igb, "Failed to allocate rx/tx rings or groups");
3935779Sxy150489 		goto attach_fail;
3945779Sxy150489 	}
3955779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
3965779Sxy150489 
3975779Sxy150489 	/*
3985779Sxy150489 	 * Add interrupt handlers
3995779Sxy150489 	 */
4005779Sxy150489 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
4015779Sxy150489 		igb_error(igb, "Failed to add interrupt handlers");
4025779Sxy150489 		goto attach_fail;
4035779Sxy150489 	}
4045779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
4055779Sxy150489 
4065779Sxy150489 	/*
4075779Sxy150489 	 * Initialize driver parameters
4085779Sxy150489 	 */
4095779Sxy150489 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
4105779Sxy150489 		igb_error(igb, "Failed to initialize driver settings");
4115779Sxy150489 		goto attach_fail;
4125779Sxy150489 	}
4135779Sxy150489 
4146624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
4156624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
4166624Sgl147354 		goto attach_fail;
4176624Sgl147354 	}
4186624Sgl147354 
4195779Sxy150489 	/*
4205779Sxy150489 	 * Initialize mutexes for this device.
4215779Sxy150489 	 * Do this before enabling the interrupt handler and
4225779Sxy150489 	 * register the softint to avoid the condition where
4235779Sxy150489 	 * interrupt handler can try using uninitialized mutex
4245779Sxy150489 	 */
4255779Sxy150489 	igb_init_locks(igb);
4265779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
4275779Sxy150489 
4285779Sxy150489 	/*
4298955SChenlu.Chen@Sun.COM 	 * Allocate DMA resources
4305779Sxy150489 	 */
4318955SChenlu.Chen@Sun.COM 	if (igb_alloc_dma(igb) != IGB_SUCCESS) {
4328955SChenlu.Chen@Sun.COM 		igb_error(igb, "Failed to allocate DMA resources");
4338955SChenlu.Chen@Sun.COM 		goto attach_fail;
4348955SChenlu.Chen@Sun.COM 	}
4358955SChenlu.Chen@Sun.COM 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_DMA;
4368955SChenlu.Chen@Sun.COM 
4378955SChenlu.Chen@Sun.COM 	/*
4388955SChenlu.Chen@Sun.COM 	 * Initialize the adapter and setup the rx/tx rings
4398955SChenlu.Chen@Sun.COM 	 */
4405779Sxy150489 	if (igb_init(igb) != IGB_SUCCESS) {
4415779Sxy150489 		igb_error(igb, "Failed to initialize adapter");
4425779Sxy150489 		goto attach_fail;
4435779Sxy150489 	}
4448955SChenlu.Chen@Sun.COM 	igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
4455779Sxy150489 
4465779Sxy150489 	/*
4475779Sxy150489 	 * Initialize statistics
4485779Sxy150489 	 */
4495779Sxy150489 	if (igb_init_stats(igb) != IGB_SUCCESS) {
4505779Sxy150489 		igb_error(igb, "Failed to initialize statistics");
4515779Sxy150489 		goto attach_fail;
4525779Sxy150489 	}
4535779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_STATS;
4545779Sxy150489 
4555779Sxy150489 	/*
4565779Sxy150489 	 * Initialize NDD parameters
4575779Sxy150489 	 */
4585779Sxy150489 	if (igb_nd_init(igb) != IGB_SUCCESS) {
4595779Sxy150489 		igb_error(igb, "Failed to initialize ndd");
4605779Sxy150489 		goto attach_fail;
4615779Sxy150489 	}
4625779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_NDD;
4635779Sxy150489 
4645779Sxy150489 	/*
4655779Sxy150489 	 * Register the driver to the MAC
4665779Sxy150489 	 */
4675779Sxy150489 	if (igb_register_mac(igb) != IGB_SUCCESS) {
4685779Sxy150489 		igb_error(igb, "Failed to register MAC");
4695779Sxy150489 		goto attach_fail;
4705779Sxy150489 	}
4715779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_MAC;
4725779Sxy150489 
4735779Sxy150489 	/*
4745779Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
4755779Sxy150489 	 * initialized, enable interrupts.
4765779Sxy150489 	 */
4775779Sxy150489 	if (igb_enable_intrs(igb) != IGB_SUCCESS) {
4785779Sxy150489 		igb_error(igb, "Failed to enable DDI interrupts");
4795779Sxy150489 		goto attach_fail;
4805779Sxy150489 	}
4815779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
4825779Sxy150489 
4838571SChenlu.Chen@Sun.COM 	igb_log(igb, "%s", igb_version);
4845779Sxy150489 	igb->igb_state |= IGB_INITIALIZED;
4855779Sxy150489 
4865779Sxy150489 	return (DDI_SUCCESS);
4875779Sxy150489 
4885779Sxy150489 attach_fail:
4895779Sxy150489 	igb_unconfigure(devinfo, igb);
4905779Sxy150489 	return (DDI_FAILURE);
4915779Sxy150489 }
4925779Sxy150489 
4935779Sxy150489 /*
4945779Sxy150489  * igb_detach - driver detach
4955779Sxy150489  *
4965779Sxy150489  * The detach() function is the complement of the attach routine.
4975779Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
4985779Sxy150489  * state  associated  with  a  given  instance of a device node
4995779Sxy150489  * prior to the removal of that instance from the system.
5005779Sxy150489  *
5015779Sxy150489  * The detach() function will be called once for each  instance
5025779Sxy150489  * of the device for which there has been a successful attach()
5035779Sxy150489  * once there are no longer  any  opens  on  the  device.
5045779Sxy150489  *
5055779Sxy150489  * Interrupts routine are disabled, All memory allocated by this
5065779Sxy150489  * driver are freed.
5075779Sxy150489  */
5085779Sxy150489 static int
5095779Sxy150489 igb_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
5105779Sxy150489 {
5115779Sxy150489 	igb_t *igb;
5125779Sxy150489 
5135779Sxy150489 	/*
5145779Sxy150489 	 * Check detach command
5155779Sxy150489 	 */
5165779Sxy150489 	switch (cmd) {
5175779Sxy150489 	default:
5185779Sxy150489 		return (DDI_FAILURE);
5195779Sxy150489 
5205779Sxy150489 	case DDI_SUSPEND:
5215779Sxy150489 		return (igb_suspend(devinfo));
5225779Sxy150489 
5235779Sxy150489 	case DDI_DETACH:
5245779Sxy150489 		break;
5255779Sxy150489 	}
5265779Sxy150489 
5275779Sxy150489 
5285779Sxy150489 	/*
5295779Sxy150489 	 * Get the pointer to the driver private data structure
5305779Sxy150489 	 */
5315779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
5325779Sxy150489 	if (igb == NULL)
5335779Sxy150489 		return (DDI_FAILURE);
5345779Sxy150489 
5355779Sxy150489 	/*
5365779Sxy150489 	 * Unregister MAC. If failed, we have to fail the detach
5375779Sxy150489 	 */
5385779Sxy150489 	if (mac_unregister(igb->mac_hdl) != 0) {
5395779Sxy150489 		igb_error(igb, "Failed to unregister MAC");
5405779Sxy150489 		return (DDI_FAILURE);
5415779Sxy150489 	}
5425779Sxy150489 	igb->attach_progress &= ~ATTACH_PROGRESS_MAC;
5435779Sxy150489 
5445779Sxy150489 	/*
5455779Sxy150489 	 * If the device is still running, it needs to be stopped first.
5465779Sxy150489 	 * This check is necessary because under some specific circumstances,
5475779Sxy150489 	 * the detach routine can be called without stopping the interface
5485779Sxy150489 	 * first.
5495779Sxy150489 	 */
5505779Sxy150489 	mutex_enter(&igb->gen_lock);
5515779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
5525779Sxy150489 		igb->igb_state &= ~IGB_STARTED;
5535779Sxy150489 		igb_stop(igb);
5545779Sxy150489 		mutex_exit(&igb->gen_lock);
5555779Sxy150489 		/* Disable and stop the watchdog timer */
5565779Sxy150489 		igb_disable_watchdog_timer(igb);
5575779Sxy150489 	} else
5585779Sxy150489 		mutex_exit(&igb->gen_lock);
5595779Sxy150489 
5605779Sxy150489 	/*
5615779Sxy150489 	 * Check if there are still rx buffers held by the upper layer.
5625779Sxy150489 	 * If so, fail the detach.
5635779Sxy150489 	 */
5645779Sxy150489 	if (!igb_rx_drain(igb))
5655779Sxy150489 		return (DDI_FAILURE);
5665779Sxy150489 
5675779Sxy150489 	/*
5685779Sxy150489 	 * Do the remaining unconfigure routines
5695779Sxy150489 	 */
5705779Sxy150489 	igb_unconfigure(devinfo, igb);
5715779Sxy150489 
5725779Sxy150489 	return (DDI_SUCCESS);
5735779Sxy150489 }
5745779Sxy150489 
5757656SSherry.Moore@Sun.COM /*
5767656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
5777656SSherry.Moore@Sun.COM  *
5787656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
5797656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
5807656SSherry.Moore@Sun.COM  * blocked.
5817656SSherry.Moore@Sun.COM  *
5827656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
5837656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
5847656SSherry.Moore@Sun.COM  */
5857656SSherry.Moore@Sun.COM static int
5867656SSherry.Moore@Sun.COM igb_quiesce(dev_info_t *devinfo)
5877656SSherry.Moore@Sun.COM {
5887656SSherry.Moore@Sun.COM 	igb_t *igb;
5897656SSherry.Moore@Sun.COM 	struct e1000_hw *hw;
5907656SSherry.Moore@Sun.COM 
5917656SSherry.Moore@Sun.COM 	igb = (igb_t *)ddi_get_driver_private(devinfo);
5927656SSherry.Moore@Sun.COM 
5937656SSherry.Moore@Sun.COM 	if (igb == NULL)
5947656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
5957656SSherry.Moore@Sun.COM 
5967656SSherry.Moore@Sun.COM 	hw = &igb->hw;
5977656SSherry.Moore@Sun.COM 
5987656SSherry.Moore@Sun.COM 	/*
5997656SSherry.Moore@Sun.COM 	 * Disable the adapter interrupts
6007656SSherry.Moore@Sun.COM 	 */
6017656SSherry.Moore@Sun.COM 	igb_disable_adapter_interrupts(igb);
6027656SSherry.Moore@Sun.COM 
6037656SSherry.Moore@Sun.COM 	/* Tell firmware driver is no longer in control */
6047656SSherry.Moore@Sun.COM 	igb_release_driver_control(hw);
6057656SSherry.Moore@Sun.COM 
6067656SSherry.Moore@Sun.COM 	/*
6077656SSherry.Moore@Sun.COM 	 * Reset the chipset
6087656SSherry.Moore@Sun.COM 	 */
6097656SSherry.Moore@Sun.COM 	(void) e1000_reset_hw(hw);
6107656SSherry.Moore@Sun.COM 
6117656SSherry.Moore@Sun.COM 	/*
6127656SSherry.Moore@Sun.COM 	 * Reset PHY if possible
6137656SSherry.Moore@Sun.COM 	 */
6147656SSherry.Moore@Sun.COM 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
6157656SSherry.Moore@Sun.COM 		(void) e1000_phy_hw_reset(hw);
6167656SSherry.Moore@Sun.COM 
6177656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
6187656SSherry.Moore@Sun.COM }
6197656SSherry.Moore@Sun.COM 
6208955SChenlu.Chen@Sun.COM /*
6218955SChenlu.Chen@Sun.COM  * igb_unconfigure - release all resources held by this instance
6228955SChenlu.Chen@Sun.COM  */
6235779Sxy150489 static void
6245779Sxy150489 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
6255779Sxy150489 {
6265779Sxy150489 	/*
6275779Sxy150489 	 * Disable interrupt
6285779Sxy150489 	 */
6295779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
6305779Sxy150489 		(void) igb_disable_intrs(igb);
6315779Sxy150489 	}
6325779Sxy150489 
6335779Sxy150489 	/*
6345779Sxy150489 	 * Unregister MAC
6355779Sxy150489 	 */
6365779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
6375779Sxy150489 		(void) mac_unregister(igb->mac_hdl);
6385779Sxy150489 	}
6395779Sxy150489 
6405779Sxy150489 	/*
6415779Sxy150489 	 * Free ndd parameters
6425779Sxy150489 	 */
6435779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_NDD) {
6445779Sxy150489 		igb_nd_cleanup(igb);
6455779Sxy150489 	}
6465779Sxy150489 
6475779Sxy150489 	/*
6485779Sxy150489 	 * Free statistics
6495779Sxy150489 	 */
6505779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
6515779Sxy150489 		kstat_delete((kstat_t *)igb->igb_ks);
6525779Sxy150489 	}
6535779Sxy150489 
6545779Sxy150489 	/*
6555779Sxy150489 	 * Remove interrupt handlers
6565779Sxy150489 	 */
6575779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
6585779Sxy150489 		igb_rem_intr_handlers(igb);
6595779Sxy150489 	}
6605779Sxy150489 
6615779Sxy150489 	/*
6625779Sxy150489 	 * Remove interrupts
6635779Sxy150489 	 */
6645779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
6655779Sxy150489 		igb_rem_intrs(igb);
6665779Sxy150489 	}
6675779Sxy150489 
6685779Sxy150489 	/*
6695779Sxy150489 	 * Remove driver properties
6705779Sxy150489 	 */
6715779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
6725779Sxy150489 		(void) ddi_prop_remove_all(devinfo);
6735779Sxy150489 	}
6745779Sxy150489 
6755779Sxy150489 	/*
6765779Sxy150489 	 * Release the DMA resources of rx/tx rings
6775779Sxy150489 	 */
6788955SChenlu.Chen@Sun.COM 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_DMA) {
6798955SChenlu.Chen@Sun.COM 		igb_free_dma(igb);
6805779Sxy150489 	}
6815779Sxy150489 
6825779Sxy150489 	/*
6838955SChenlu.Chen@Sun.COM 	 * Stop the adapter
6845779Sxy150489 	 */
6858955SChenlu.Chen@Sun.COM 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) {
6865779Sxy150489 		mutex_enter(&igb->gen_lock);
6878955SChenlu.Chen@Sun.COM 		igb_stop_adapter(igb);
6885779Sxy150489 		mutex_exit(&igb->gen_lock);
6896624Sgl147354 		if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
6906624Sgl147354 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_UNAFFECTED);
6915779Sxy150489 	}
6925779Sxy150489 
6935779Sxy150489 	/*
6949775SVitezslav.Batrla@Sun.COM 	 * Free multicast table
6959775SVitezslav.Batrla@Sun.COM 	 */
6969775SVitezslav.Batrla@Sun.COM 	igb_release_multicast(igb);
6979775SVitezslav.Batrla@Sun.COM 
6989775SVitezslav.Batrla@Sun.COM 	/*
6995779Sxy150489 	 * Free register handle
7005779Sxy150489 	 */
7015779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
7025779Sxy150489 		if (igb->osdep.reg_handle != NULL)
7035779Sxy150489 			ddi_regs_map_free(&igb->osdep.reg_handle);
7045779Sxy150489 	}
7055779Sxy150489 
7065779Sxy150489 	/*
7075779Sxy150489 	 * Free PCI config handle
7085779Sxy150489 	 */
7095779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
7105779Sxy150489 		if (igb->osdep.cfg_handle != NULL)
7115779Sxy150489 			pci_config_teardown(&igb->osdep.cfg_handle);
7125779Sxy150489 	}
7135779Sxy150489 
7145779Sxy150489 	/*
7155779Sxy150489 	 * Free locks
7165779Sxy150489 	 */
7175779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
7185779Sxy150489 		igb_destroy_locks(igb);
7195779Sxy150489 	}
7205779Sxy150489 
7215779Sxy150489 	/*
7225779Sxy150489 	 * Free the rx/tx rings
7235779Sxy150489 	 */
7245779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
7255779Sxy150489 		igb_free_rings(igb);
7265779Sxy150489 	}
7275779Sxy150489 
7285779Sxy150489 	/*
7296624Sgl147354 	 * Remove FMA
7306624Sgl147354 	 */
7316624Sgl147354 	if (igb->attach_progress & ATTACH_PROGRESS_FMINIT) {
7326624Sgl147354 		igb_fm_fini(igb);
7336624Sgl147354 	}
7346624Sgl147354 
7356624Sgl147354 	/*
7365779Sxy150489 	 * Free the driver data structure
7375779Sxy150489 	 */
7385779Sxy150489 	kmem_free(igb, sizeof (igb_t));
7395779Sxy150489 
7405779Sxy150489 	ddi_set_driver_private(devinfo, NULL);
7415779Sxy150489 }
7425779Sxy150489 
7435779Sxy150489 /*
7445779Sxy150489  * igb_register_mac - Register the driver and its function pointers with
7455779Sxy150489  * the GLD interface
7465779Sxy150489  */
7475779Sxy150489 static int
7485779Sxy150489 igb_register_mac(igb_t *igb)
7495779Sxy150489 {
7505779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7515779Sxy150489 	mac_register_t *mac;
7525779Sxy150489 	int status;
7535779Sxy150489 
7545779Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
7555779Sxy150489 		return (IGB_FAILURE);
7565779Sxy150489 
7575779Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
7585779Sxy150489 	mac->m_driver = igb;
7595779Sxy150489 	mac->m_dip = igb->dip;
7605779Sxy150489 	mac->m_src_addr = hw->mac.addr;
7615779Sxy150489 	mac->m_callbacks = &igb_m_callbacks;
7625779Sxy150489 	mac->m_min_sdu = 0;
7635779Sxy150489 	mac->m_max_sdu = igb->max_frame_size -
7645779Sxy150489 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
7655895Syz147064 	mac->m_margin = VLAN_TAGSZ;
7668275SEric Cheng 	mac->m_v12n = MAC_VIRT_LEVEL1;
7675779Sxy150489 
7685779Sxy150489 	status = mac_register(mac, &igb->mac_hdl);
7695779Sxy150489 
7705779Sxy150489 	mac_free(mac);
7715779Sxy150489 
7725779Sxy150489 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
7735779Sxy150489 }
7745779Sxy150489 
7755779Sxy150489 /*
7765779Sxy150489  * igb_identify_hardware - Identify the type of the chipset
7775779Sxy150489  */
7785779Sxy150489 static int
7795779Sxy150489 igb_identify_hardware(igb_t *igb)
7805779Sxy150489 {
7815779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7825779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
7835779Sxy150489 
7845779Sxy150489 	/*
7855779Sxy150489 	 * Get the device id
7865779Sxy150489 	 */
7875779Sxy150489 	hw->vendor_id =
7885779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
7895779Sxy150489 	hw->device_id =
7905779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
7915779Sxy150489 	hw->revision_id =
7925779Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
7935779Sxy150489 	hw->subsystem_device_id =
7945779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
7955779Sxy150489 	hw->subsystem_vendor_id =
7965779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
7975779Sxy150489 
7985779Sxy150489 	/*
7995779Sxy150489 	 * Set the mac type of the adapter based on the device id
8005779Sxy150489 	 */
8015779Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
8025779Sxy150489 		return (IGB_FAILURE);
8035779Sxy150489 	}
8045779Sxy150489 
8058571SChenlu.Chen@Sun.COM 	/*
8068571SChenlu.Chen@Sun.COM 	 * Install adapter capabilities based on mac type
8078571SChenlu.Chen@Sun.COM 	 */
8088571SChenlu.Chen@Sun.COM 	switch (hw->mac.type) {
8098571SChenlu.Chen@Sun.COM 	case e1000_82575:
8108571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82575_cap;
8118571SChenlu.Chen@Sun.COM 		break;
8128571SChenlu.Chen@Sun.COM 	case e1000_82576:
8138571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82576_cap;
8148571SChenlu.Chen@Sun.COM 		break;
8158571SChenlu.Chen@Sun.COM 	default:
8168571SChenlu.Chen@Sun.COM 		return (IGB_FAILURE);
8178571SChenlu.Chen@Sun.COM 	}
8188571SChenlu.Chen@Sun.COM 
8195779Sxy150489 	return (IGB_SUCCESS);
8205779Sxy150489 }
8215779Sxy150489 
8225779Sxy150489 /*
8235779Sxy150489  * igb_regs_map - Map the device registers
8245779Sxy150489  */
8255779Sxy150489 static int
8265779Sxy150489 igb_regs_map(igb_t *igb)
8275779Sxy150489 {
8285779Sxy150489 	dev_info_t *devinfo = igb->dip;
8295779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8305779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
8315779Sxy150489 	off_t mem_size;
8325779Sxy150489 
8335779Sxy150489 	/*
8345779Sxy150489 	 * First get the size of device registers to be mapped.
8355779Sxy150489 	 */
8368571SChenlu.Chen@Sun.COM 	if (ddi_dev_regsize(devinfo, IGB_ADAPTER_REGSET, &mem_size) !=
8378571SChenlu.Chen@Sun.COM 	    DDI_SUCCESS) {
8385779Sxy150489 		return (IGB_FAILURE);
8395779Sxy150489 	}
8405779Sxy150489 
8415779Sxy150489 	/*
8425779Sxy150489 	 * Call ddi_regs_map_setup() to map registers
8435779Sxy150489 	 */
8448571SChenlu.Chen@Sun.COM 	if ((ddi_regs_map_setup(devinfo, IGB_ADAPTER_REGSET,
8455779Sxy150489 	    (caddr_t *)&hw->hw_addr, 0,
8465779Sxy150489 	    mem_size, &igb_regs_acc_attr,
8475779Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
8485779Sxy150489 		return (IGB_FAILURE);
8495779Sxy150489 	}
8505779Sxy150489 
8515779Sxy150489 	return (IGB_SUCCESS);
8525779Sxy150489 }
8535779Sxy150489 
8545779Sxy150489 /*
8555779Sxy150489  * igb_init_properties - Initialize driver properties
8565779Sxy150489  */
8575779Sxy150489 static void
8585779Sxy150489 igb_init_properties(igb_t *igb)
8595779Sxy150489 {
8605779Sxy150489 	/*
8615779Sxy150489 	 * Get conf file properties, including link settings
8625779Sxy150489 	 * jumbo frames, ring number, descriptor number, etc.
8635779Sxy150489 	 */
8645779Sxy150489 	igb_get_conf(igb);
8655779Sxy150489 }
8665779Sxy150489 
8675779Sxy150489 /*
8685779Sxy150489  * igb_init_driver_settings - Initialize driver settings
8695779Sxy150489  *
8705779Sxy150489  * The settings include hardware function pointers, bus information,
8715779Sxy150489  * rx/tx rings settings, link state, and any other parameters that
8725779Sxy150489  * need to be setup during driver initialization.
8735779Sxy150489  */
8745779Sxy150489 static int
8755779Sxy150489 igb_init_driver_settings(igb_t *igb)
8765779Sxy150489 {
8775779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8785779Sxy150489 	igb_rx_ring_t *rx_ring;
8795779Sxy150489 	igb_tx_ring_t *tx_ring;
8805779Sxy150489 	uint32_t rx_size;
8815779Sxy150489 	uint32_t tx_size;
8825779Sxy150489 	int i;
8835779Sxy150489 
8845779Sxy150489 	/*
8855779Sxy150489 	 * Initialize chipset specific hardware function pointers
8865779Sxy150489 	 */
8875779Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
8885779Sxy150489 		return (IGB_FAILURE);
8895779Sxy150489 	}
8905779Sxy150489 
8915779Sxy150489 	/*
8925779Sxy150489 	 * Get bus information
8935779Sxy150489 	 */
8945779Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
8955779Sxy150489 		return (IGB_FAILURE);
8965779Sxy150489 	}
8975779Sxy150489 
8985779Sxy150489 	/*
8999188SPaul.Guo@Sun.COM 	 * Get the system page size
9009188SPaul.Guo@Sun.COM 	 */
9019188SPaul.Guo@Sun.COM 	igb->page_size = ddi_ptob(igb->dip, (ulong_t)1);
9029188SPaul.Guo@Sun.COM 
9039188SPaul.Guo@Sun.COM 	/*
9045779Sxy150489 	 * Set rx buffer size
9055779Sxy150489 	 * The IP header alignment room is counted in the calculation.
9065779Sxy150489 	 * The rx buffer size is in unit of 1K that is required by the
9075779Sxy150489 	 * chipset hardware.
9085779Sxy150489 	 */
9095779Sxy150489 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
9105779Sxy150489 	igb->rx_buf_size = ((rx_size >> 10) +
9115779Sxy150489 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9125779Sxy150489 
9135779Sxy150489 	/*
9145779Sxy150489 	 * Set tx buffer size
9155779Sxy150489 	 */
9165779Sxy150489 	tx_size = igb->max_frame_size;
9175779Sxy150489 	igb->tx_buf_size = ((tx_size >> 10) +
9185779Sxy150489 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9195779Sxy150489 
9205779Sxy150489 	/*
9215779Sxy150489 	 * Initialize rx/tx rings parameters
9225779Sxy150489 	 */
9235779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9245779Sxy150489 		rx_ring = &igb->rx_rings[i];
9255779Sxy150489 		rx_ring->index = i;
9265779Sxy150489 		rx_ring->igb = igb;
9275779Sxy150489 
9285779Sxy150489 		rx_ring->ring_size = igb->rx_ring_size;
9295779Sxy150489 		rx_ring->free_list_size = igb->rx_ring_size;
9305779Sxy150489 		rx_ring->copy_thresh = igb->rx_copy_thresh;
9315779Sxy150489 		rx_ring->limit_per_intr = igb->rx_limit_per_intr;
9325779Sxy150489 	}
9335779Sxy150489 
9345779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9355779Sxy150489 		tx_ring = &igb->tx_rings[i];
9365779Sxy150489 		tx_ring->index = i;
9375779Sxy150489 		tx_ring->igb = igb;
9385779Sxy150489 		if (igb->tx_head_wb_enable)
9395779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
9405779Sxy150489 		else
9415779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
9425779Sxy150489 
9435779Sxy150489 		tx_ring->ring_size = igb->tx_ring_size;
9445779Sxy150489 		tx_ring->free_list_size = igb->tx_ring_size +
9455779Sxy150489 		    (igb->tx_ring_size >> 1);
9465779Sxy150489 		tx_ring->copy_thresh = igb->tx_copy_thresh;
9475779Sxy150489 		tx_ring->recycle_thresh = igb->tx_recycle_thresh;
9485779Sxy150489 		tx_ring->overload_thresh = igb->tx_overload_thresh;
9495779Sxy150489 		tx_ring->resched_thresh = igb->tx_resched_thresh;
9505779Sxy150489 	}
9515779Sxy150489 
9525779Sxy150489 	/*
9538571SChenlu.Chen@Sun.COM 	 * Initialize values of interrupt throttling rates
9545779Sxy150489 	 */
9555779Sxy150489 	for (i = 1; i < MAX_NUM_EITR; i++)
9565779Sxy150489 		igb->intr_throttling[i] = igb->intr_throttling[0];
9575779Sxy150489 
9585779Sxy150489 	/*
9595779Sxy150489 	 * The initial link state should be "unknown"
9605779Sxy150489 	 */
9615779Sxy150489 	igb->link_state = LINK_STATE_UNKNOWN;
9625779Sxy150489 
9635779Sxy150489 	return (IGB_SUCCESS);
9645779Sxy150489 }
9655779Sxy150489 
9665779Sxy150489 /*
9675779Sxy150489  * igb_init_locks - Initialize locks
9685779Sxy150489  */
9695779Sxy150489 static void
9705779Sxy150489 igb_init_locks(igb_t *igb)
9715779Sxy150489 {
9725779Sxy150489 	igb_rx_ring_t *rx_ring;
9735779Sxy150489 	igb_tx_ring_t *tx_ring;
9745779Sxy150489 	int i;
9755779Sxy150489 
9765779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9775779Sxy150489 		rx_ring = &igb->rx_rings[i];
9785779Sxy150489 		mutex_init(&rx_ring->rx_lock, NULL,
9795779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9805779Sxy150489 		mutex_init(&rx_ring->recycle_lock, NULL,
9815779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9825779Sxy150489 	}
9835779Sxy150489 
9845779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9855779Sxy150489 		tx_ring = &igb->tx_rings[i];
9865779Sxy150489 		mutex_init(&tx_ring->tx_lock, NULL,
9875779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9885779Sxy150489 		mutex_init(&tx_ring->recycle_lock, NULL,
9895779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9905779Sxy150489 		mutex_init(&tx_ring->tcb_head_lock, NULL,
9915779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9925779Sxy150489 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
9935779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9945779Sxy150489 	}
9955779Sxy150489 
9965779Sxy150489 	mutex_init(&igb->gen_lock, NULL,
9975779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9985779Sxy150489 
9995779Sxy150489 	mutex_init(&igb->watchdog_lock, NULL,
10005779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10015779Sxy150489 }
10025779Sxy150489 
10035779Sxy150489 /*
10045779Sxy150489  * igb_destroy_locks - Destroy locks
10055779Sxy150489  */
10065779Sxy150489 static void
10075779Sxy150489 igb_destroy_locks(igb_t *igb)
10085779Sxy150489 {
10095779Sxy150489 	igb_rx_ring_t *rx_ring;
10105779Sxy150489 	igb_tx_ring_t *tx_ring;
10115779Sxy150489 	int i;
10125779Sxy150489 
10135779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
10145779Sxy150489 		rx_ring = &igb->rx_rings[i];
10155779Sxy150489 		mutex_destroy(&rx_ring->rx_lock);
10165779Sxy150489 		mutex_destroy(&rx_ring->recycle_lock);
10175779Sxy150489 	}
10185779Sxy150489 
10195779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
10205779Sxy150489 		tx_ring = &igb->tx_rings[i];
10215779Sxy150489 		mutex_destroy(&tx_ring->tx_lock);
10225779Sxy150489 		mutex_destroy(&tx_ring->recycle_lock);
10235779Sxy150489 		mutex_destroy(&tx_ring->tcb_head_lock);
10245779Sxy150489 		mutex_destroy(&tx_ring->tcb_tail_lock);
10255779Sxy150489 	}
10265779Sxy150489 
10275779Sxy150489 	mutex_destroy(&igb->gen_lock);
10285779Sxy150489 	mutex_destroy(&igb->watchdog_lock);
10295779Sxy150489 }
10305779Sxy150489 
10315779Sxy150489 static int
10325779Sxy150489 igb_resume(dev_info_t *devinfo)
10335779Sxy150489 {
10345779Sxy150489 	igb_t *igb;
10355779Sxy150489 
10365779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10375779Sxy150489 	if (igb == NULL)
10385779Sxy150489 		return (DDI_FAILURE);
10395779Sxy150489 
10405779Sxy150489 	mutex_enter(&igb->gen_lock);
10415779Sxy150489 
10425779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
10435779Sxy150489 		if (igb_start(igb) != IGB_SUCCESS) {
10445779Sxy150489 			mutex_exit(&igb->gen_lock);
10455779Sxy150489 			return (DDI_FAILURE);
10465779Sxy150489 		}
10475779Sxy150489 
10485779Sxy150489 		/*
10495779Sxy150489 		 * Enable and start the watchdog timer
10505779Sxy150489 		 */
10515779Sxy150489 		igb_enable_watchdog_timer(igb);
10525779Sxy150489 	}
10535779Sxy150489 
10545779Sxy150489 	igb->igb_state &= ~IGB_SUSPENDED;
10555779Sxy150489 
10565779Sxy150489 	mutex_exit(&igb->gen_lock);
10575779Sxy150489 
10585779Sxy150489 	return (DDI_SUCCESS);
10595779Sxy150489 }
10605779Sxy150489 
10615779Sxy150489 static int
10625779Sxy150489 igb_suspend(dev_info_t *devinfo)
10635779Sxy150489 {
10645779Sxy150489 	igb_t *igb;
10655779Sxy150489 
10665779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10675779Sxy150489 	if (igb == NULL)
10685779Sxy150489 		return (DDI_FAILURE);
10695779Sxy150489 
10705779Sxy150489 	mutex_enter(&igb->gen_lock);
10715779Sxy150489 
10725779Sxy150489 	igb->igb_state |= IGB_SUSPENDED;
10735779Sxy150489 
10748955SChenlu.Chen@Sun.COM 	if (!(igb->igb_state & IGB_STARTED)) {
10758955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->gen_lock);
10768955SChenlu.Chen@Sun.COM 		return (DDI_SUCCESS);
10778955SChenlu.Chen@Sun.COM 	}
10788955SChenlu.Chen@Sun.COM 
10795779Sxy150489 	igb_stop(igb);
10805779Sxy150489 
10815779Sxy150489 	mutex_exit(&igb->gen_lock);
10825779Sxy150489 
10835779Sxy150489 	/*
10845779Sxy150489 	 * Disable and stop the watchdog timer
10855779Sxy150489 	 */
10865779Sxy150489 	igb_disable_watchdog_timer(igb);
10875779Sxy150489 
10885779Sxy150489 	return (DDI_SUCCESS);
10895779Sxy150489 }
10905779Sxy150489 
10915779Sxy150489 static int
10925779Sxy150489 igb_init(igb_t *igb)
10935779Sxy150489 {
10948955SChenlu.Chen@Sun.COM 	int i;
10958955SChenlu.Chen@Sun.COM 
10968955SChenlu.Chen@Sun.COM 	mutex_enter(&igb->gen_lock);
10978955SChenlu.Chen@Sun.COM 
10988955SChenlu.Chen@Sun.COM 	/*
10998955SChenlu.Chen@Sun.COM 	 * Initilize the adapter
11008955SChenlu.Chen@Sun.COM 	 */
11018955SChenlu.Chen@Sun.COM 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
11028955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->gen_lock);
11038955SChenlu.Chen@Sun.COM 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
11048955SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
11058955SChenlu.Chen@Sun.COM 		return (IGB_FAILURE);
11068955SChenlu.Chen@Sun.COM 	}
11078955SChenlu.Chen@Sun.COM 
11088955SChenlu.Chen@Sun.COM 	/*
11098955SChenlu.Chen@Sun.COM 	 * Setup the rx/tx rings
11108955SChenlu.Chen@Sun.COM 	 */
11118955SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++)
11128955SChenlu.Chen@Sun.COM 		mutex_enter(&igb->rx_rings[i].rx_lock);
11138955SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_tx_rings; i++)
11148955SChenlu.Chen@Sun.COM 		mutex_enter(&igb->tx_rings[i].tx_lock);
11158955SChenlu.Chen@Sun.COM 
11168955SChenlu.Chen@Sun.COM 	igb_setup_rings(igb);
11178955SChenlu.Chen@Sun.COM 
11188955SChenlu.Chen@Sun.COM 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
11198955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->tx_rings[i].tx_lock);
11208955SChenlu.Chen@Sun.COM 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
11218955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->rx_rings[i].rx_lock);
11228955SChenlu.Chen@Sun.COM 
11238955SChenlu.Chen@Sun.COM 	mutex_exit(&igb->gen_lock);
11248955SChenlu.Chen@Sun.COM 
11258955SChenlu.Chen@Sun.COM 	return (IGB_SUCCESS);
11268955SChenlu.Chen@Sun.COM }
11278955SChenlu.Chen@Sun.COM 
11288955SChenlu.Chen@Sun.COM /*
11298955SChenlu.Chen@Sun.COM  * igb_init_mac_address - Initialize the default MAC address
11308955SChenlu.Chen@Sun.COM  *
11318955SChenlu.Chen@Sun.COM  * On success, the MAC address is entered in the igb->hw.mac.addr
11328955SChenlu.Chen@Sun.COM  * and hw->mac.perm_addr fields and the adapter's RAR(0) receive
11338955SChenlu.Chen@Sun.COM  * address register.
11348955SChenlu.Chen@Sun.COM  *
11358955SChenlu.Chen@Sun.COM  * Important side effects:
11368955SChenlu.Chen@Sun.COM  * 1. adapter is reset - this is required to put it in a known state.
11378955SChenlu.Chen@Sun.COM  * 2. all of non-volatile memory (NVM) is read & checksummed - NVM is where
11388955SChenlu.Chen@Sun.COM  * MAC address and all default settings are stored, so a valid checksum
11398955SChenlu.Chen@Sun.COM  * is required.
11408955SChenlu.Chen@Sun.COM  */
11418955SChenlu.Chen@Sun.COM static int
11428955SChenlu.Chen@Sun.COM igb_init_mac_address(igb_t *igb)
11438955SChenlu.Chen@Sun.COM {
11445779Sxy150489 	struct e1000_hw *hw = &igb->hw;
11455779Sxy150489 
11468275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
11475779Sxy150489 
11485779Sxy150489 	/*
11495779Sxy150489 	 * Reset chipset to put the hardware in a known state
11508955SChenlu.Chen@Sun.COM 	 * before we try to get MAC address from NVM.
11515779Sxy150489 	 */
11526624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
11538955SChenlu.Chen@Sun.COM 		igb_error(igb, "Adapter reset failed.");
11548955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
11556624Sgl147354 	}
11565779Sxy150489 
11575779Sxy150489 	/*
11585779Sxy150489 	 * NVM validation
11595779Sxy150489 	 */
11605779Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
11615779Sxy150489 		/*
11625779Sxy150489 		 * Some PCI-E parts fail the first check due to
11635779Sxy150489 		 * the link being in sleep state.  Call it again,
11645779Sxy150489 		 * if it fails a second time its a real issue.
11655779Sxy150489 		 */
11665779Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
11675779Sxy150489 			igb_error(igb,
11685779Sxy150489 			    "Invalid NVM checksum. Please contact "
11695779Sxy150489 			    "the vendor to update the NVM.");
11708955SChenlu.Chen@Sun.COM 			goto init_mac_fail;
11715779Sxy150489 		}
11725779Sxy150489 	}
11735779Sxy150489 
11745779Sxy150489 	/*
11758955SChenlu.Chen@Sun.COM 	 * Get the mac address
11768955SChenlu.Chen@Sun.COM 	 * This function should handle SPARC case correctly.
11778955SChenlu.Chen@Sun.COM 	 */
11788955SChenlu.Chen@Sun.COM 	if (!igb_find_mac_address(igb)) {
11798955SChenlu.Chen@Sun.COM 		igb_error(igb, "Failed to get the mac address");
11808955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
11818955SChenlu.Chen@Sun.COM 	}
11828955SChenlu.Chen@Sun.COM 
11838955SChenlu.Chen@Sun.COM 	/* Validate mac address */
11848955SChenlu.Chen@Sun.COM 	if (!is_valid_mac_addr(hw->mac.addr)) {
11858955SChenlu.Chen@Sun.COM 		igb_error(igb, "Invalid mac address");
11868955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
11878955SChenlu.Chen@Sun.COM 	}
11888955SChenlu.Chen@Sun.COM 
11898955SChenlu.Chen@Sun.COM 	return (IGB_SUCCESS);
11908955SChenlu.Chen@Sun.COM 
11918955SChenlu.Chen@Sun.COM init_mac_fail:
11928955SChenlu.Chen@Sun.COM 	return (IGB_FAILURE);
11938955SChenlu.Chen@Sun.COM }
11948955SChenlu.Chen@Sun.COM 
11958955SChenlu.Chen@Sun.COM /*
11968955SChenlu.Chen@Sun.COM  * igb_init_adapter - Initialize the adapter
11978955SChenlu.Chen@Sun.COM  */
11988955SChenlu.Chen@Sun.COM static int
11998955SChenlu.Chen@Sun.COM igb_init_adapter(igb_t *igb)
12008955SChenlu.Chen@Sun.COM {
12018955SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
12028955SChenlu.Chen@Sun.COM 	uint32_t pba;
12038955SChenlu.Chen@Sun.COM 	uint32_t high_water;
12048955SChenlu.Chen@Sun.COM 	int i;
12058955SChenlu.Chen@Sun.COM 
12068955SChenlu.Chen@Sun.COM 	ASSERT(mutex_owned(&igb->gen_lock));
12078955SChenlu.Chen@Sun.COM 
12088955SChenlu.Chen@Sun.COM 	/*
12098955SChenlu.Chen@Sun.COM 	 * In order to obtain the default MAC address, this will reset the
12108955SChenlu.Chen@Sun.COM 	 * adapter and validate the NVM that the address and many other
12118955SChenlu.Chen@Sun.COM 	 * default settings come from.
12128955SChenlu.Chen@Sun.COM 	 */
12138955SChenlu.Chen@Sun.COM 	if (igb_init_mac_address(igb) != IGB_SUCCESS) {
12148955SChenlu.Chen@Sun.COM 		igb_error(igb, "Failed to initialize MAC address");
12158955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
12168955SChenlu.Chen@Sun.COM 	}
12178955SChenlu.Chen@Sun.COM 
12188955SChenlu.Chen@Sun.COM 	/*
12195779Sxy150489 	 * Setup flow control
12205779Sxy150489 	 *
12215779Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
12225779Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
12235779Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
12245779Sxy150489 	 * file.
12255779Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
12265779Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
12275779Sxy150489 	 * high-water mark.
12285779Sxy150489 	 * The high water mark must be low enough to fit one full frame above
12295779Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
12305779Sxy150489 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
12315779Sxy150489 	 * frame.
12325779Sxy150489 	 */
12338955SChenlu.Chen@Sun.COM 	/*
12348955SChenlu.Chen@Sun.COM 	 * The default setting of PBA is correct for 82575 and other supported
12358955SChenlu.Chen@Sun.COM 	 * adapters do not have the E1000_PBA register, so PBA value is only
12368955SChenlu.Chen@Sun.COM 	 * used for calculation here and is never written to the adapter.
12378955SChenlu.Chen@Sun.COM 	 */
12388571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
12398571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_34K;
12408571SChenlu.Chen@Sun.COM 	} else {
12418571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_64K;
12428571SChenlu.Chen@Sun.COM 	}
12438571SChenlu.Chen@Sun.COM 
12445779Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
12455779Sxy150489 	    ((pba << 10) - igb->max_frame_size));
12465779Sxy150489 
12478571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
12488571SChenlu.Chen@Sun.COM 		/* 8-byte granularity */
12498571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF8;
12508571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 8;
12518571SChenlu.Chen@Sun.COM 	} else {
12528571SChenlu.Chen@Sun.COM 		/* 16-byte granularity */
12538571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF0;
12548571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 16;
12558571SChenlu.Chen@Sun.COM 	}
12568571SChenlu.Chen@Sun.COM 
12575779Sxy150489 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
12585779Sxy150489 	hw->fc.send_xon = B_TRUE;
12595779Sxy150489 
12608955SChenlu.Chen@Sun.COM 	e1000_validate_mdi_setting(hw);
12618955SChenlu.Chen@Sun.COM 
12625779Sxy150489 	/*
12638955SChenlu.Chen@Sun.COM 	 * Reset the chipset hardware the second time to put PBA settings
12648955SChenlu.Chen@Sun.COM 	 * into effect.
12655779Sxy150489 	 */
12666624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
12678955SChenlu.Chen@Sun.COM 		igb_error(igb, "Second reset failed");
12688955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
12696624Sgl147354 	}
12705779Sxy150489 
12715779Sxy150489 	/*
12725779Sxy150489 	 * Don't wait for auto-negotiation to complete
12735779Sxy150489 	 */
12745779Sxy150489 	hw->phy.autoneg_wait_to_complete = B_FALSE;
12755779Sxy150489 
12765779Sxy150489 	/*
12775779Sxy150489 	 * Copper options
12785779Sxy150489 	 */
12795779Sxy150489 	if (hw->phy.media_type == e1000_media_type_copper) {
12805779Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
12815779Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
12825779Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
12835779Sxy150489 	}
12845779Sxy150489 
12855779Sxy150489 	/*
12865779Sxy150489 	 * Initialize link settings
12875779Sxy150489 	 */
12885779Sxy150489 	(void) igb_setup_link(igb, B_FALSE);
12895779Sxy150489 
12905779Sxy150489 	/*
12915779Sxy150489 	 * Configure/Initialize hardware
12925779Sxy150489 	 */
12935779Sxy150489 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
12945779Sxy150489 		igb_error(igb, "Failed to initialize hardware");
12958955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
12965779Sxy150489 	}
12975779Sxy150489 
12985779Sxy150489 	/*
12998955SChenlu.Chen@Sun.COM 	 * Disable wakeup control by default
13008955SChenlu.Chen@Sun.COM 	 */
13018955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_WUC, 0);
13028955SChenlu.Chen@Sun.COM 
13038955SChenlu.Chen@Sun.COM 	/*
13048955SChenlu.Chen@Sun.COM 	 * Record phy info in hw struct
13058955SChenlu.Chen@Sun.COM 	 */
13068955SChenlu.Chen@Sun.COM 	(void) e1000_get_phy_info(hw);
13078955SChenlu.Chen@Sun.COM 
13088955SChenlu.Chen@Sun.COM 	/*
13095779Sxy150489 	 * Make sure driver has control
13105779Sxy150489 	 */
13115779Sxy150489 	igb_get_driver_control(hw);
13125779Sxy150489 
13135779Sxy150489 	/*
13148955SChenlu.Chen@Sun.COM 	 * Restore LED settings to the default from EEPROM
13158955SChenlu.Chen@Sun.COM 	 * to meet the standard for Sun platforms.
13168955SChenlu.Chen@Sun.COM 	 */
13178955SChenlu.Chen@Sun.COM 	(void) e1000_cleanup_led(hw);
13188955SChenlu.Chen@Sun.COM 
13198955SChenlu.Chen@Sun.COM 	/*
13205779Sxy150489 	 * Setup MSI-X interrupts
13215779Sxy150489 	 */
13225779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
13238571SChenlu.Chen@Sun.COM 		igb->capab->setup_msix(igb);
13245779Sxy150489 
13255779Sxy150489 	/*
13265779Sxy150489 	 * Initialize unicast addresses.
13275779Sxy150489 	 */
13285779Sxy150489 	igb_init_unicst(igb);
13295779Sxy150489 
13305779Sxy150489 	/*
13315779Sxy150489 	 * Setup and initialize the mctable structures.
13325779Sxy150489 	 */
13335779Sxy150489 	igb_setup_multicst(igb);
13345779Sxy150489 
13355779Sxy150489 	/*
13365779Sxy150489 	 * Set interrupt throttling rate
13375779Sxy150489 	 */
13385779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++)
13395779Sxy150489 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
13405779Sxy150489 
13415779Sxy150489 	/*
13425779Sxy150489 	 * Save the state of the phy
13435779Sxy150489 	 */
13445779Sxy150489 	igb_get_phy_state(igb);
13455779Sxy150489 
13465779Sxy150489 	return (IGB_SUCCESS);
13478955SChenlu.Chen@Sun.COM 
13488955SChenlu.Chen@Sun.COM init_adapter_fail:
13498955SChenlu.Chen@Sun.COM 	/*
13508955SChenlu.Chen@Sun.COM 	 * Reset PHY if possible
13518955SChenlu.Chen@Sun.COM 	 */
13528955SChenlu.Chen@Sun.COM 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
13538955SChenlu.Chen@Sun.COM 		(void) e1000_phy_hw_reset(hw);
13548955SChenlu.Chen@Sun.COM 
13558955SChenlu.Chen@Sun.COM 	return (IGB_FAILURE);
13565779Sxy150489 }
13575779Sxy150489 
13585779Sxy150489 /*
13598955SChenlu.Chen@Sun.COM  * igb_stop_adapter - Stop the adapter
13605779Sxy150489  */
13615779Sxy150489 static void
13628955SChenlu.Chen@Sun.COM igb_stop_adapter(igb_t *igb)
13635779Sxy150489 {
13645779Sxy150489 	struct e1000_hw *hw = &igb->hw;
13655779Sxy150489 
13665779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
13675779Sxy150489 
13685779Sxy150489 	/* Tell firmware driver is no longer in control */
13695779Sxy150489 	igb_release_driver_control(hw);
13705779Sxy150489 
13715779Sxy150489 	/*
13725779Sxy150489 	 * Reset the chipset
13735779Sxy150489 	 */
13746624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
13756624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
13766624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
13776624Sgl147354 	}
13785779Sxy150489 
13795779Sxy150489 	/*
13808955SChenlu.Chen@Sun.COM 	 * e1000_phy_hw_reset is not needed here, MAC reset above is sufficient
13815779Sxy150489 	 */
13825779Sxy150489 }
13835779Sxy150489 
13845779Sxy150489 /*
13855779Sxy150489  * igb_reset - Reset the chipset and restart the driver.
13865779Sxy150489  *
13875779Sxy150489  * It involves stopping and re-starting the chipset,
13885779Sxy150489  * and re-configuring the rx/tx rings.
13895779Sxy150489  */
13905779Sxy150489 static int
13915779Sxy150489 igb_reset(igb_t *igb)
13925779Sxy150489 {
13935779Sxy150489 	int i;
13945779Sxy150489 
13955779Sxy150489 	mutex_enter(&igb->gen_lock);
13965779Sxy150489 
13975779Sxy150489 	ASSERT(igb->igb_state & IGB_STARTED);
13985779Sxy150489 
13995779Sxy150489 	/*
14005779Sxy150489 	 * Disable the adapter interrupts to stop any rx/tx activities
14015779Sxy150489 	 * before draining pending data and resetting hardware.
14025779Sxy150489 	 */
14035779Sxy150489 	igb_disable_adapter_interrupts(igb);
14045779Sxy150489 
14055779Sxy150489 	/*
14065779Sxy150489 	 * Drain the pending transmit packets
14075779Sxy150489 	 */
14085779Sxy150489 	(void) igb_tx_drain(igb);
14095779Sxy150489 
14105779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
14115779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
14125779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
14135779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
14145779Sxy150489 
14155779Sxy150489 	/*
14168955SChenlu.Chen@Sun.COM 	 * Stop the adapter
14175779Sxy150489 	 */
14188955SChenlu.Chen@Sun.COM 	igb_stop_adapter(igb);
14195779Sxy150489 
14205779Sxy150489 	/*
14215779Sxy150489 	 * Clean the pending tx data/resources
14225779Sxy150489 	 */
14235779Sxy150489 	igb_tx_clean(igb);
14245779Sxy150489 
14255779Sxy150489 	/*
14268955SChenlu.Chen@Sun.COM 	 * Start the adapter
14275779Sxy150489 	 */
14288955SChenlu.Chen@Sun.COM 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
14296624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
14305779Sxy150489 		goto reset_failure;
14315779Sxy150489 	}
14325779Sxy150489 
14335779Sxy150489 	/*
14345779Sxy150489 	 * Setup the rx/tx rings
14355779Sxy150489 	 */
14365779Sxy150489 	igb_setup_rings(igb);
14375779Sxy150489 
14385779Sxy150489 	/*
14395779Sxy150489 	 * Enable adapter interrupts
14405779Sxy150489 	 * The interrupts must be enabled after the driver state is START
14415779Sxy150489 	 */
14428571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
14435779Sxy150489 
14446624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
14456624Sgl147354 		goto reset_failure;
14466624Sgl147354 
14476624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
14486624Sgl147354 		goto reset_failure;
14496624Sgl147354 
14505779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14515779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14525779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14535779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14545779Sxy150489 
14555779Sxy150489 	mutex_exit(&igb->gen_lock);
14565779Sxy150489 
14575779Sxy150489 	return (IGB_SUCCESS);
14585779Sxy150489 
14595779Sxy150489 reset_failure:
14605779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14615779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14625779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14635779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14645779Sxy150489 
14655779Sxy150489 	mutex_exit(&igb->gen_lock);
14665779Sxy150489 
14676624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
14686624Sgl147354 
14695779Sxy150489 	return (IGB_FAILURE);
14705779Sxy150489 }
14715779Sxy150489 
14725779Sxy150489 /*
14735779Sxy150489  * igb_tx_clean - Clean the pending transmit packets and DMA resources
14745779Sxy150489  */
14755779Sxy150489 static void
14765779Sxy150489 igb_tx_clean(igb_t *igb)
14775779Sxy150489 {
14785779Sxy150489 	igb_tx_ring_t *tx_ring;
14795779Sxy150489 	tx_control_block_t *tcb;
14805779Sxy150489 	link_list_t pending_list;
14815779Sxy150489 	uint32_t desc_num;
14825779Sxy150489 	int i, j;
14835779Sxy150489 
14845779Sxy150489 	LINK_LIST_INIT(&pending_list);
14855779Sxy150489 
14865779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
14875779Sxy150489 		tx_ring = &igb->tx_rings[i];
14885779Sxy150489 
14895779Sxy150489 		mutex_enter(&tx_ring->recycle_lock);
14905779Sxy150489 
14915779Sxy150489 		/*
14925779Sxy150489 		 * Clean the pending tx data - the pending packets in the
14935779Sxy150489 		 * work_list that have no chances to be transmitted again.
14945779Sxy150489 		 *
14955779Sxy150489 		 * We must ensure the chipset is stopped or the link is down
14965779Sxy150489 		 * before cleaning the transmit packets.
14975779Sxy150489 		 */
14985779Sxy150489 		desc_num = 0;
14995779Sxy150489 		for (j = 0; j < tx_ring->ring_size; j++) {
15005779Sxy150489 			tcb = tx_ring->work_list[j];
15015779Sxy150489 			if (tcb != NULL) {
15025779Sxy150489 				desc_num += tcb->desc_num;
15035779Sxy150489 
15045779Sxy150489 				tx_ring->work_list[j] = NULL;
15055779Sxy150489 
15065779Sxy150489 				igb_free_tcb(tcb);
15075779Sxy150489 
15085779Sxy150489 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
15095779Sxy150489 			}
15105779Sxy150489 		}
15115779Sxy150489 
15125779Sxy150489 		if (desc_num > 0) {
15135779Sxy150489 			atomic_add_32(&tx_ring->tbd_free, desc_num);
15145779Sxy150489 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
15155779Sxy150489 
15165779Sxy150489 			/*
15177072Sxy150489 			 * Reset the head and tail pointers of the tbd ring;
15187072Sxy150489 			 * Reset the head write-back if it is enabled.
15195779Sxy150489 			 */
15205779Sxy150489 			tx_ring->tbd_head = 0;
15215779Sxy150489 			tx_ring->tbd_tail = 0;
15227072Sxy150489 			if (igb->tx_head_wb_enable)
15237072Sxy150489 				*tx_ring->tbd_head_wb = 0;
15245779Sxy150489 
15255779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
15265779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
15275779Sxy150489 		}
15285779Sxy150489 
15295779Sxy150489 		mutex_exit(&tx_ring->recycle_lock);
15305779Sxy150489 
15315779Sxy150489 		/*
15325779Sxy150489 		 * Add the tx control blocks in the pending list to
15335779Sxy150489 		 * the free list.
15345779Sxy150489 		 */
15355779Sxy150489 		igb_put_free_list(tx_ring, &pending_list);
15365779Sxy150489 	}
15375779Sxy150489 }
15385779Sxy150489 
15395779Sxy150489 /*
15405779Sxy150489  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
15415779Sxy150489  */
15425779Sxy150489 static boolean_t
15435779Sxy150489 igb_tx_drain(igb_t *igb)
15445779Sxy150489 {
15455779Sxy150489 	igb_tx_ring_t *tx_ring;
15465779Sxy150489 	boolean_t done;
15475779Sxy150489 	int i, j;
15485779Sxy150489 
15495779Sxy150489 	/*
15505779Sxy150489 	 * Wait for a specific time to allow pending tx packets
15515779Sxy150489 	 * to be transmitted.
15525779Sxy150489 	 *
15535779Sxy150489 	 * Check the counter tbd_free to see if transmission is done.
15545779Sxy150489 	 * No lock protection is needed here.
15555779Sxy150489 	 *
15565779Sxy150489 	 * Return B_TRUE if all pending packets have been transmitted;
15575779Sxy150489 	 * Otherwise return B_FALSE;
15585779Sxy150489 	 */
15595779Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
15605779Sxy150489 
15615779Sxy150489 		done = B_TRUE;
15625779Sxy150489 		for (j = 0; j < igb->num_tx_rings; j++) {
15635779Sxy150489 			tx_ring = &igb->tx_rings[j];
15645779Sxy150489 			done = done &&
15655779Sxy150489 			    (tx_ring->tbd_free == tx_ring->ring_size);
15665779Sxy150489 		}
15675779Sxy150489 
15685779Sxy150489 		if (done)
15695779Sxy150489 			break;
15705779Sxy150489 
15715779Sxy150489 		msec_delay(1);
15725779Sxy150489 	}
15735779Sxy150489 
15745779Sxy150489 	return (done);
15755779Sxy150489 }
15765779Sxy150489 
15775779Sxy150489 /*
15785779Sxy150489  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
15795779Sxy150489  */
15805779Sxy150489 static boolean_t
15815779Sxy150489 igb_rx_drain(igb_t *igb)
15825779Sxy150489 {
15835779Sxy150489 	igb_rx_ring_t *rx_ring;
15845779Sxy150489 	boolean_t done;
15855779Sxy150489 	int i, j;
15865779Sxy150489 
15875779Sxy150489 	/*
15885779Sxy150489 	 * Polling the rx free list to check if those rx buffers held by
15895779Sxy150489 	 * the upper layer are released.
15905779Sxy150489 	 *
15915779Sxy150489 	 * Check the counter rcb_free to see if all pending buffers are
15925779Sxy150489 	 * released. No lock protection is needed here.
15935779Sxy150489 	 *
15945779Sxy150489 	 * Return B_TRUE if all pending buffers have been released;
15955779Sxy150489 	 * Otherwise return B_FALSE;
15965779Sxy150489 	 */
15975779Sxy150489 	for (i = 0; i < RX_DRAIN_TIME; i++) {
15985779Sxy150489 
15995779Sxy150489 		done = B_TRUE;
16005779Sxy150489 		for (j = 0; j < igb->num_rx_rings; j++) {
16015779Sxy150489 			rx_ring = &igb->rx_rings[j];
16025779Sxy150489 			done = done &&
16035779Sxy150489 			    (rx_ring->rcb_free == rx_ring->free_list_size);
16045779Sxy150489 		}
16055779Sxy150489 
16065779Sxy150489 		if (done)
16075779Sxy150489 			break;
16085779Sxy150489 
16095779Sxy150489 		msec_delay(1);
16105779Sxy150489 	}
16115779Sxy150489 
16125779Sxy150489 	return (done);
16135779Sxy150489 }
16145779Sxy150489 
16155779Sxy150489 /*
16165779Sxy150489  * igb_start - Start the driver/chipset
16175779Sxy150489  */
16185779Sxy150489 int
16195779Sxy150489 igb_start(igb_t *igb)
16205779Sxy150489 {
16215779Sxy150489 	int i;
16225779Sxy150489 
16235779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16245779Sxy150489 
16255779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
16265779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
16275779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
16285779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
16295779Sxy150489 
16305779Sxy150489 	/*
16318955SChenlu.Chen@Sun.COM 	 * Start the adapter
16325779Sxy150489 	 */
16338955SChenlu.Chen@Sun.COM 	if ((igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) == 0) {
16348955SChenlu.Chen@Sun.COM 		if (igb_init_adapter(igb) != IGB_SUCCESS) {
16358275SEric Cheng 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
16368275SEric Cheng 			goto start_failure;
16378275SEric Cheng 		}
16388955SChenlu.Chen@Sun.COM 		igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
16398955SChenlu.Chen@Sun.COM 
16408955SChenlu.Chen@Sun.COM 		/*
16418955SChenlu.Chen@Sun.COM 		 * Setup the rx/tx rings
16428955SChenlu.Chen@Sun.COM 		 */
16438955SChenlu.Chen@Sun.COM 		igb_setup_rings(igb);
16445779Sxy150489 	}
16455779Sxy150489 
16465779Sxy150489 	/*
16475779Sxy150489 	 * Enable adapter interrupts
16485779Sxy150489 	 * The interrupts must be enabled after the driver state is START
16495779Sxy150489 	 */
16508571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
16515779Sxy150489 
16526624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
16536624Sgl147354 		goto start_failure;
16546624Sgl147354 
16556624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
16566624Sgl147354 		goto start_failure;
16576624Sgl147354 
16585779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
16595779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
16605779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
16615779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
16625779Sxy150489 
16635779Sxy150489 	return (IGB_SUCCESS);
16645779Sxy150489 
16655779Sxy150489 start_failure:
16665779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
16675779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
16685779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
16695779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
16705779Sxy150489 
16716624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
16726624Sgl147354 
16735779Sxy150489 	return (IGB_FAILURE);
16745779Sxy150489 }
16755779Sxy150489 
16765779Sxy150489 /*
16775779Sxy150489  * igb_stop - Stop the driver/chipset
16785779Sxy150489  */
16795779Sxy150489 void
16805779Sxy150489 igb_stop(igb_t *igb)
16815779Sxy150489 {
16825779Sxy150489 	int i;
16835779Sxy150489 
16845779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16855779Sxy150489 
16868955SChenlu.Chen@Sun.COM 	igb->attach_progress &= ~ATTACH_PROGRESS_INIT_ADAPTER;
16878275SEric Cheng 
16885779Sxy150489 	/*
16895779Sxy150489 	 * Disable the adapter interrupts
16905779Sxy150489 	 */
16915779Sxy150489 	igb_disable_adapter_interrupts(igb);
16925779Sxy150489 
16935779Sxy150489 	/*
16945779Sxy150489 	 * Drain the pending tx packets
16955779Sxy150489 	 */
16965779Sxy150489 	(void) igb_tx_drain(igb);
16975779Sxy150489 
16985779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
16995779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
17005779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
17015779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
17025779Sxy150489 
17035779Sxy150489 	/*
17048955SChenlu.Chen@Sun.COM 	 * Stop the adapter
17055779Sxy150489 	 */
17068955SChenlu.Chen@Sun.COM 	igb_stop_adapter(igb);
17075779Sxy150489 
17085779Sxy150489 	/*
17095779Sxy150489 	 * Clean the pending tx data/resources
17105779Sxy150489 	 */
17115779Sxy150489 	igb_tx_clean(igb);
17125779Sxy150489 
17135779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
17145779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
17155779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
17165779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
17176624Sgl147354 
17186624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
17196624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
17205779Sxy150489 }
17215779Sxy150489 
17225779Sxy150489 /*
17235779Sxy150489  * igb_alloc_rings - Allocate memory space for rx/tx rings
17245779Sxy150489  */
17255779Sxy150489 static int
17265779Sxy150489 igb_alloc_rings(igb_t *igb)
17275779Sxy150489 {
17285779Sxy150489 	/*
17295779Sxy150489 	 * Allocate memory space for rx rings
17305779Sxy150489 	 */
17315779Sxy150489 	igb->rx_rings = kmem_zalloc(
17325779Sxy150489 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
17335779Sxy150489 	    KM_NOSLEEP);
17345779Sxy150489 
17355779Sxy150489 	if (igb->rx_rings == NULL) {
17365779Sxy150489 		return (IGB_FAILURE);
17375779Sxy150489 	}
17385779Sxy150489 
17395779Sxy150489 	/*
17405779Sxy150489 	 * Allocate memory space for tx rings
17415779Sxy150489 	 */
17425779Sxy150489 	igb->tx_rings = kmem_zalloc(
17435779Sxy150489 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
17445779Sxy150489 	    KM_NOSLEEP);
17455779Sxy150489 
17465779Sxy150489 	if (igb->tx_rings == NULL) {
17475779Sxy150489 		kmem_free(igb->rx_rings,
17485779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17495779Sxy150489 		igb->rx_rings = NULL;
17505779Sxy150489 		return (IGB_FAILURE);
17515779Sxy150489 	}
17525779Sxy150489 
17538275SEric Cheng 	/*
17548275SEric Cheng 	 * Allocate memory space for rx ring groups
17558275SEric Cheng 	 */
17568275SEric Cheng 	igb->rx_groups = kmem_zalloc(
17578275SEric Cheng 	    sizeof (igb_rx_group_t) * igb->num_rx_groups,
17588275SEric Cheng 	    KM_NOSLEEP);
17598275SEric Cheng 
17608275SEric Cheng 	if (igb->rx_groups == NULL) {
17618275SEric Cheng 		kmem_free(igb->rx_rings,
17628275SEric Cheng 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17638275SEric Cheng 		kmem_free(igb->tx_rings,
17648275SEric Cheng 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
17658275SEric Cheng 		igb->rx_rings = NULL;
17668275SEric Cheng 		igb->tx_rings = NULL;
17678275SEric Cheng 		return (IGB_FAILURE);
17688275SEric Cheng 	}
17698275SEric Cheng 
17705779Sxy150489 	return (IGB_SUCCESS);
17715779Sxy150489 }
17725779Sxy150489 
17735779Sxy150489 /*
17745779Sxy150489  * igb_free_rings - Free the memory space of rx/tx rings.
17755779Sxy150489  */
17765779Sxy150489 static void
17775779Sxy150489 igb_free_rings(igb_t *igb)
17785779Sxy150489 {
17795779Sxy150489 	if (igb->rx_rings != NULL) {
17805779Sxy150489 		kmem_free(igb->rx_rings,
17815779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17825779Sxy150489 		igb->rx_rings = NULL;
17835779Sxy150489 	}
17845779Sxy150489 
17855779Sxy150489 	if (igb->tx_rings != NULL) {
17865779Sxy150489 		kmem_free(igb->tx_rings,
17875779Sxy150489 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
17885779Sxy150489 		igb->tx_rings = NULL;
17895779Sxy150489 	}
17908275SEric Cheng 
17918275SEric Cheng 	if (igb->rx_groups != NULL) {
17928275SEric Cheng 		kmem_free(igb->rx_groups,
17938275SEric Cheng 		    sizeof (igb_rx_group_t) * igb->num_rx_groups);
17948275SEric Cheng 		igb->rx_groups = NULL;
17958275SEric Cheng 	}
17965779Sxy150489 }
17975779Sxy150489 
17985779Sxy150489 /*
17995779Sxy150489  * igb_setup_rings - Setup rx/tx rings
18005779Sxy150489  */
18015779Sxy150489 static void
18025779Sxy150489 igb_setup_rings(igb_t *igb)
18035779Sxy150489 {
18045779Sxy150489 	/*
18055779Sxy150489 	 * Setup the rx/tx rings, including the following:
18065779Sxy150489 	 *
18075779Sxy150489 	 * 1. Setup the descriptor ring and the control block buffers;
18085779Sxy150489 	 * 2. Initialize necessary registers for receive/transmit;
18095779Sxy150489 	 * 3. Initialize software pointers/parameters for receive/transmit;
18105779Sxy150489 	 */
18115779Sxy150489 	igb_setup_rx(igb);
18125779Sxy150489 
18135779Sxy150489 	igb_setup_tx(igb);
18146624Sgl147354 
18156624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
18166624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
18175779Sxy150489 }
18185779Sxy150489 
18195779Sxy150489 static void
18205779Sxy150489 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
18215779Sxy150489 {
18225779Sxy150489 	igb_t *igb = rx_ring->igb;
18235779Sxy150489 	struct e1000_hw *hw = &igb->hw;
18245779Sxy150489 	rx_control_block_t *rcb;
18255779Sxy150489 	union e1000_adv_rx_desc	*rbd;
18265779Sxy150489 	uint32_t size;
18275779Sxy150489 	uint32_t buf_low;
18285779Sxy150489 	uint32_t buf_high;
18298955SChenlu.Chen@Sun.COM 	uint32_t rxdctl;
18305779Sxy150489 	int i;
18315779Sxy150489 
18325779Sxy150489 	ASSERT(mutex_owned(&rx_ring->rx_lock));
18335779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
18345779Sxy150489 
18358955SChenlu.Chen@Sun.COM 	/*
18368955SChenlu.Chen@Sun.COM 	 * Initialize descriptor ring with buffer addresses
18378955SChenlu.Chen@Sun.COM 	 */
18385779Sxy150489 	for (i = 0; i < igb->rx_ring_size; i++) {
18395779Sxy150489 		rcb = rx_ring->work_list[i];
18405779Sxy150489 		rbd = &rx_ring->rbd_ring[i];
18415779Sxy150489 
18425779Sxy150489 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
18435779Sxy150489 		rbd->read.hdr_addr = NULL;
18445779Sxy150489 	}
18455779Sxy150489 
18465779Sxy150489 	/*
18475779Sxy150489 	 * Initialize the base address registers
18485779Sxy150489 	 */
18495779Sxy150489 	buf_low = (uint32_t)rx_ring->rbd_area.dma_address;
18505779Sxy150489 	buf_high = (uint32_t)(rx_ring->rbd_area.dma_address >> 32);
18515779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
18525779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
18535779Sxy150489 
18545779Sxy150489 	/*
18558955SChenlu.Chen@Sun.COM 	 * Initialize the length register
18568955SChenlu.Chen@Sun.COM 	 */
18578955SChenlu.Chen@Sun.COM 	size = rx_ring->ring_size * sizeof (union e1000_adv_rx_desc);
18588955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
18598955SChenlu.Chen@Sun.COM 
18608955SChenlu.Chen@Sun.COM 	/*
18618955SChenlu.Chen@Sun.COM 	 * Initialize buffer size & descriptor type
18625779Sxy150489 	 */
18638955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index),
18648955SChenlu.Chen@Sun.COM 	    ((igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
18658955SChenlu.Chen@Sun.COM 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF));
18668955SChenlu.Chen@Sun.COM 
18678955SChenlu.Chen@Sun.COM 	/*
18688955SChenlu.Chen@Sun.COM 	 * Setup the Receive Descriptor Control Register (RXDCTL)
18698955SChenlu.Chen@Sun.COM 	 */
18708955SChenlu.Chen@Sun.COM 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
18718955SChenlu.Chen@Sun.COM 	rxdctl &= igb->capab->rxdctl_mask;
18728955SChenlu.Chen@Sun.COM 	rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
18738955SChenlu.Chen@Sun.COM 	rxdctl |= 16;		/* pthresh */
18748955SChenlu.Chen@Sun.COM 	rxdctl |= 8 << 8;	/* hthresh */
18758955SChenlu.Chen@Sun.COM 	rxdctl |= 1 << 16;	/* wthresh */
18768955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), rxdctl);
18775779Sxy150489 
18785779Sxy150489 	rx_ring->rbd_next = 0;
18795779Sxy150489 
18805779Sxy150489 	/*
18815779Sxy150489 	 * Note: Considering the case that the chipset is being reset
18825779Sxy150489 	 * and there are still some buffers held by the upper layer,
18835779Sxy150489 	 * we should not reset the values of rcb_head, rcb_tail and
18845779Sxy150489 	 * rcb_free;
18855779Sxy150489 	 */
18865779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
18875779Sxy150489 		rx_ring->rcb_head = 0;
18885779Sxy150489 		rx_ring->rcb_tail = 0;
18895779Sxy150489 		rx_ring->rcb_free = rx_ring->free_list_size;
18905779Sxy150489 	}
18915779Sxy150489 }
18925779Sxy150489 
18935779Sxy150489 static void
18945779Sxy150489 igb_setup_rx(igb_t *igb)
18955779Sxy150489 {
18965779Sxy150489 	igb_rx_ring_t *rx_ring;
18978275SEric Cheng 	igb_rx_group_t *rx_group;
18985779Sxy150489 	struct e1000_hw *hw = &igb->hw;
18998955SChenlu.Chen@Sun.COM 	uint32_t rctl, rxcsum;
19008275SEric Cheng 	uint32_t ring_per_group;
19015779Sxy150489 	int i;
19025779Sxy150489 
19035779Sxy150489 	/*
19048955SChenlu.Chen@Sun.COM 	 * Setup the Receive Control Register (RCTL), and enable the
19058955SChenlu.Chen@Sun.COM 	 * receiver. The initial configuration is to: enable the receiver,
19068955SChenlu.Chen@Sun.COM 	 * accept broadcasts, discard bad packets, accept long packets,
19078955SChenlu.Chen@Sun.COM 	 * disable VLAN filter checking, and set receive buffer size to
19088955SChenlu.Chen@Sun.COM 	 * 2k.  For 82575, also set the receive descriptor minimum
19098955SChenlu.Chen@Sun.COM 	 * threshold size to 1/2 the ring.
19105779Sxy150489 	 */
19118571SChenlu.Chen@Sun.COM 	rctl = E1000_READ_REG(hw, E1000_RCTL);
19128571SChenlu.Chen@Sun.COM 
19138571SChenlu.Chen@Sun.COM 	/*
19148955SChenlu.Chen@Sun.COM 	 * Clear the field used for wakeup control.  This driver doesn't do
19158955SChenlu.Chen@Sun.COM 	 * wakeup but leave this here for completeness.
19168571SChenlu.Chen@Sun.COM 	 */
19178571SChenlu.Chen@Sun.COM 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
19188571SChenlu.Chen@Sun.COM 
19198955SChenlu.Chen@Sun.COM 	switch (hw->mac.type) {
19208955SChenlu.Chen@Sun.COM 	case e1000_82575:
19218955SChenlu.Chen@Sun.COM 		rctl |= (E1000_RCTL_EN |	/* Enable Receive Unit */
19228955SChenlu.Chen@Sun.COM 		    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
19238955SChenlu.Chen@Sun.COM 		    E1000_RCTL_LPE |		/* Large Packet Enable */
19248955SChenlu.Chen@Sun.COM 						/* Multicast filter offset */
19258955SChenlu.Chen@Sun.COM 		    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
19268955SChenlu.Chen@Sun.COM 		    E1000_RCTL_RDMTS_HALF |	/* rx descriptor threshold */
19278955SChenlu.Chen@Sun.COM 		    E1000_RCTL_SECRC);		/* Strip Ethernet CRC */
19288955SChenlu.Chen@Sun.COM 		break;
19298955SChenlu.Chen@Sun.COM 
19308955SChenlu.Chen@Sun.COM 	case e1000_82576:
19318955SChenlu.Chen@Sun.COM 		rctl |= (E1000_RCTL_EN |	/* Enable Receive Unit */
19328955SChenlu.Chen@Sun.COM 		    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
19338955SChenlu.Chen@Sun.COM 		    E1000_RCTL_LPE |		/* Large Packet Enable */
19348955SChenlu.Chen@Sun.COM 						/* Multicast filter offset */
19358955SChenlu.Chen@Sun.COM 		    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
19368955SChenlu.Chen@Sun.COM 		    E1000_RCTL_SECRC);		/* Strip Ethernet CRC */
19378955SChenlu.Chen@Sun.COM 		break;
19388955SChenlu.Chen@Sun.COM 
19398955SChenlu.Chen@Sun.COM 	default:
19408955SChenlu.Chen@Sun.COM 		igb_log(igb, "unsupported MAC type: %d", hw->mac.type);
19418955SChenlu.Chen@Sun.COM 		return;	/* should never come here; this will cause rx failure */
19428955SChenlu.Chen@Sun.COM 	}
19435779Sxy150489 
19448275SEric Cheng 	for (i = 0; i < igb->num_rx_groups; i++) {
19458275SEric Cheng 		rx_group = &igb->rx_groups[i];
19468275SEric Cheng 		rx_group->index = i;
19478275SEric Cheng 		rx_group->igb = igb;
19488275SEric Cheng 	}
19498275SEric Cheng 
19505779Sxy150489 	/*
19518955SChenlu.Chen@Sun.COM 	 * Set up all rx descriptor rings - must be called before receive unit
19528955SChenlu.Chen@Sun.COM 	 * enabled.
19535812Sxy150489 	 */
19548275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
19555812Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
19565812Sxy150489 		rx_ring = &igb->rx_rings[i];
19575812Sxy150489 		igb_setup_rx_ring(rx_ring);
19588275SEric Cheng 
19598275SEric Cheng 		/*
19608275SEric Cheng 		 * Map a ring to a group by assigning a group index
19618275SEric Cheng 		 */
19628275SEric Cheng 		rx_ring->group_index = i / ring_per_group;
19635812Sxy150489 	}
19645812Sxy150489 
19655812Sxy150489 	/*
19665779Sxy150489 	 * Setup the Rx Long Packet Max Length register
19675779Sxy150489 	 */
19685779Sxy150489 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
19695779Sxy150489 
19705779Sxy150489 	/*
19715779Sxy150489 	 * Hardware checksum settings
19725779Sxy150489 	 */
19735779Sxy150489 	if (igb->rx_hcksum_enable) {
19748955SChenlu.Chen@Sun.COM 		rxcsum =
19755779Sxy150489 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
19765779Sxy150489 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
19775779Sxy150489 
19788955SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
19795779Sxy150489 	}
19805779Sxy150489 
19815779Sxy150489 	/*
19828275SEric Cheng 	 * Setup classify and RSS for multiple receive queues
19835779Sxy150489 	 */
19848275SEric Cheng 	switch (igb->vmdq_mode) {
19858275SEric Cheng 	case E1000_VMDQ_OFF:
19868275SEric Cheng 		/*
19878275SEric Cheng 		 * One ring group, only RSS is needed when more than
19888275SEric Cheng 		 * one ring enabled.
19898275SEric Cheng 		 */
19908275SEric Cheng 		if (igb->num_rx_rings > 1)
19918275SEric Cheng 			igb_setup_rss(igb);
19928275SEric Cheng 		break;
19938275SEric Cheng 	case E1000_VMDQ_MAC:
19948275SEric Cheng 		/*
19958275SEric Cheng 		 * Multiple groups, each group has one ring,
19968275SEric Cheng 		 * only the MAC classification is needed.
19978275SEric Cheng 		 */
19988275SEric Cheng 		igb_setup_mac_classify(igb);
19998275SEric Cheng 		break;
20008275SEric Cheng 	case E1000_VMDQ_MAC_RSS:
20018275SEric Cheng 		/*
20028275SEric Cheng 		 * Multiple groups and multiple rings, both
20038275SEric Cheng 		 * MAC classification and RSS are needed.
20048275SEric Cheng 		 */
20058275SEric Cheng 		igb_setup_mac_rss_classify(igb);
20068275SEric Cheng 		break;
20078275SEric Cheng 	}
20088955SChenlu.Chen@Sun.COM 
20098955SChenlu.Chen@Sun.COM 	/*
20108955SChenlu.Chen@Sun.COM 	 * Enable the receive unit - must be done after all
20118955SChenlu.Chen@Sun.COM 	 * the rx setup above.
20128955SChenlu.Chen@Sun.COM 	 */
20138955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
20148955SChenlu.Chen@Sun.COM 
20158955SChenlu.Chen@Sun.COM 	/*
20168955SChenlu.Chen@Sun.COM 	 * Initialize all adapter ring head & tail pointers - must
20178955SChenlu.Chen@Sun.COM 	 * be done after receive unit is enabled
20188955SChenlu.Chen@Sun.COM 	 */
20198955SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
20208955SChenlu.Chen@Sun.COM 		rx_ring = &igb->rx_rings[i];
20218955SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RDH(i), 0);
20228955SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RDT(i), rx_ring->ring_size - 1);
20238955SChenlu.Chen@Sun.COM 	}
20248955SChenlu.Chen@Sun.COM 
20258955SChenlu.Chen@Sun.COM 	/*
20268955SChenlu.Chen@Sun.COM 	 * 82575 with manageability enabled needs a special flush to make
20278955SChenlu.Chen@Sun.COM 	 * sure the fifos start clean.
20288955SChenlu.Chen@Sun.COM 	 */
20298955SChenlu.Chen@Sun.COM 	if ((hw->mac.type == e1000_82575) &&
20308955SChenlu.Chen@Sun.COM 	    (E1000_READ_REG(hw, E1000_MANC) & E1000_MANC_RCV_TCO_EN)) {
20318955SChenlu.Chen@Sun.COM 		e1000_rx_fifo_flush_82575(hw);
20328955SChenlu.Chen@Sun.COM 	}
20335779Sxy150489 }
20345779Sxy150489 
20355779Sxy150489 static void
20365779Sxy150489 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
20375779Sxy150489 {
20385779Sxy150489 	igb_t *igb = tx_ring->igb;
20395779Sxy150489 	struct e1000_hw *hw = &igb->hw;
20405779Sxy150489 	uint32_t size;
20415779Sxy150489 	uint32_t buf_low;
20425779Sxy150489 	uint32_t buf_high;
20435779Sxy150489 	uint32_t reg_val;
20445779Sxy150489 
20455779Sxy150489 	ASSERT(mutex_owned(&tx_ring->tx_lock));
20465779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
20475779Sxy150489 
20488275SEric Cheng 
20495779Sxy150489 	/*
20505779Sxy150489 	 * Initialize the length register
20515779Sxy150489 	 */
20525779Sxy150489 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
20535779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
20545779Sxy150489 
20555779Sxy150489 	/*
20565779Sxy150489 	 * Initialize the base address registers
20575779Sxy150489 	 */
20585779Sxy150489 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
20595779Sxy150489 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
20605779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
20615779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
20625779Sxy150489 
20635779Sxy150489 	/*
20645779Sxy150489 	 * Setup head & tail pointers
20655779Sxy150489 	 */
20665779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
20675779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
20685779Sxy150489 
20695779Sxy150489 	/*
20705779Sxy150489 	 * Setup head write-back
20715779Sxy150489 	 */
20725779Sxy150489 	if (igb->tx_head_wb_enable) {
20735779Sxy150489 		/*
20745779Sxy150489 		 * The memory of the head write-back is allocated using
20755779Sxy150489 		 * the extra tbd beyond the tail of the tbd ring.
20765779Sxy150489 		 */
20775779Sxy150489 		tx_ring->tbd_head_wb = (uint32_t *)
20785779Sxy150489 		    ((uintptr_t)tx_ring->tbd_area.address + size);
20797072Sxy150489 		*tx_ring->tbd_head_wb = 0;
20805779Sxy150489 
20815779Sxy150489 		buf_low = (uint32_t)
20825779Sxy150489 		    (tx_ring->tbd_area.dma_address + size);
20835779Sxy150489 		buf_high = (uint32_t)
20845779Sxy150489 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
20855779Sxy150489 
20865779Sxy150489 		/* Set the head write-back enable bit */
20875779Sxy150489 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
20885779Sxy150489 
20895779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
20905779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
20915779Sxy150489 
20925779Sxy150489 		/*
20935779Sxy150489 		 * Turn off relaxed ordering for head write back or it will
20945779Sxy150489 		 * cause problems with the tx recycling
20955779Sxy150489 		 */
20965779Sxy150489 		reg_val = E1000_READ_REG(hw,
20975779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index));
20985779Sxy150489 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
20995779Sxy150489 		E1000_WRITE_REG(hw,
21005779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
21015779Sxy150489 	} else {
21025779Sxy150489 		tx_ring->tbd_head_wb = NULL;
21035779Sxy150489 	}
21045779Sxy150489 
21055779Sxy150489 	tx_ring->tbd_head = 0;
21065779Sxy150489 	tx_ring->tbd_tail = 0;
21075779Sxy150489 	tx_ring->tbd_free = tx_ring->ring_size;
21085779Sxy150489 
21095779Sxy150489 	/*
21108571SChenlu.Chen@Sun.COM 	 * Note: for the case that the chipset is being reset, we should not
21118571SChenlu.Chen@Sun.COM 	 * reset the values of tcb_head, tcb_tail. And considering there might
21128571SChenlu.Chen@Sun.COM 	 * still be some packets kept in the pending_list, we should not assert
21138571SChenlu.Chen@Sun.COM 	 * (tcb_free == free_list_size) here.
21145779Sxy150489 	 */
21155779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
21165779Sxy150489 		tx_ring->tcb_head = 0;
21175779Sxy150489 		tx_ring->tcb_tail = 0;
21185779Sxy150489 		tx_ring->tcb_free = tx_ring->free_list_size;
21195779Sxy150489 	}
21205779Sxy150489 
21215779Sxy150489 	/*
21228571SChenlu.Chen@Sun.COM 	 * Enable TXDCTL per queue
21238571SChenlu.Chen@Sun.COM 	 */
21248571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TXDCTL(tx_ring->index));
21258571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TXDCTL_QUEUE_ENABLE;
21268571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_TXDCTL(tx_ring->index), reg_val);
21279188SPaul.Guo@Sun.COM 
21289188SPaul.Guo@Sun.COM 	/*
21299188SPaul.Guo@Sun.COM 	 * Initialize hardware checksum offload settings
21309188SPaul.Guo@Sun.COM 	 */
21319188SPaul.Guo@Sun.COM 	bzero(&tx_ring->tx_context, sizeof (tx_context_t));
21325779Sxy150489 }
21335779Sxy150489 
21345779Sxy150489 static void
21355779Sxy150489 igb_setup_tx(igb_t *igb)
21365779Sxy150489 {
21375779Sxy150489 	igb_tx_ring_t *tx_ring;
21385779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21395779Sxy150489 	uint32_t reg_val;
21405779Sxy150489 	int i;
21415779Sxy150489 
21425779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
21435779Sxy150489 		tx_ring = &igb->tx_rings[i];
21445779Sxy150489 		igb_setup_tx_ring(tx_ring);
21455779Sxy150489 	}
21465779Sxy150489 
21475779Sxy150489 	/*
21485779Sxy150489 	 * Setup the Transmit Control Register (TCTL)
21495779Sxy150489 	 */
21508571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TCTL);
21518571SChenlu.Chen@Sun.COM 	reg_val &= ~E1000_TCTL_CT;
21528571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
21538571SChenlu.Chen@Sun.COM 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
21548571SChenlu.Chen@Sun.COM 
21558571SChenlu.Chen@Sun.COM 	/* Enable transmits */
21568571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_EN;
21575779Sxy150489 
21585779Sxy150489 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
21595779Sxy150489 }
21605779Sxy150489 
21615779Sxy150489 /*
21625779Sxy150489  * igb_setup_rss - Setup receive-side scaling feature
21635779Sxy150489  */
21645779Sxy150489 static void
21655779Sxy150489 igb_setup_rss(igb_t *igb)
21665779Sxy150489 {
21675779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21685779Sxy150489 	uint32_t i, mrqc, rxcsum;
21698571SChenlu.Chen@Sun.COM 	int shift = 0;
21705779Sxy150489 	uint32_t random;
21715779Sxy150489 	union e1000_reta {
21725779Sxy150489 		uint32_t	dword;
21735779Sxy150489 		uint8_t		bytes[4];
21745779Sxy150489 	} reta;
21755779Sxy150489 
21765779Sxy150489 	/* Setup the Redirection Table */
21778571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82576) {
21788571SChenlu.Chen@Sun.COM 		shift = 0;
21798571SChenlu.Chen@Sun.COM 	} else if (hw->mac.type == e1000_82575) {
21808571SChenlu.Chen@Sun.COM 		shift = 6;
21818571SChenlu.Chen@Sun.COM 	}
21825779Sxy150489 	for (i = 0; i < (32 * 4); i++) {
21835779Sxy150489 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
21845779Sxy150489 		if ((i & 3) == 3) {
21855779Sxy150489 			E1000_WRITE_REG(hw,
21865779Sxy150489 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
21875779Sxy150489 		}
21885779Sxy150489 	}
21895779Sxy150489 
21905779Sxy150489 	/* Fill out hash function seeds */
21915779Sxy150489 	for (i = 0; i < 10; i++) {
21925779Sxy150489 		(void) random_get_pseudo_bytes((uint8_t *)&random,
21935779Sxy150489 		    sizeof (uint32_t));
21945779Sxy150489 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
21955779Sxy150489 	}
21965779Sxy150489 
21975779Sxy150489 	/* Setup the Multiple Receive Queue Control register */
21985779Sxy150489 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
21995779Sxy150489 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
22005779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
22015779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6 |
22025779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
22035779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
22045779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
22055779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
22065779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
22075779Sxy150489 
22085779Sxy150489 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
22095779Sxy150489 
22105779Sxy150489 	/*
22115779Sxy150489 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
22125779Sxy150489 	 *
22135779Sxy150489 	 * The Packet Checksum is not ethernet CRC. It is another kind of
22145779Sxy150489 	 * checksum offloading provided by the 82575 chipset besides the IP
22155779Sxy150489 	 * header checksum offloading and the TCP/UDP checksum offloading.
22165779Sxy150489 	 * The Packet Checksum is by default computed over the entire packet
22175779Sxy150489 	 * from the first byte of the DA through the last byte of the CRC,
22185779Sxy150489 	 * including the Ethernet and IP headers.
22195779Sxy150489 	 *
22205779Sxy150489 	 * It is a hardware limitation that Packet Checksum is mutually
22215779Sxy150489 	 * exclusive with RSS.
22225779Sxy150489 	 */
22235779Sxy150489 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
22245779Sxy150489 	rxcsum |= E1000_RXCSUM_PCSD;
22255779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
22265779Sxy150489 }
22275779Sxy150489 
22285779Sxy150489 /*
22298275SEric Cheng  * igb_setup_mac_rss_classify - Setup MAC classification and rss
22308275SEric Cheng  */
22318275SEric Cheng static void
22328275SEric Cheng igb_setup_mac_rss_classify(igb_t *igb)
22338275SEric Cheng {
22348275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
22358275SEric Cheng 	uint32_t i, mrqc, vmdctl, rxcsum;
22368275SEric Cheng 	uint32_t ring_per_group;
22378275SEric Cheng 	int shift_group0, shift_group1;
22388275SEric Cheng 	uint32_t random;
22398275SEric Cheng 	union e1000_reta {
22408275SEric Cheng 		uint32_t	dword;
22418275SEric Cheng 		uint8_t		bytes[4];
22428275SEric Cheng 	} reta;
22438275SEric Cheng 
22448275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
22458275SEric Cheng 
22468275SEric Cheng 	/* Setup the Redirection Table, it is shared between two groups */
22478275SEric Cheng 	shift_group0 = 2;
22488275SEric Cheng 	shift_group1 = 6;
22498275SEric Cheng 	for (i = 0; i < (32 * 4); i++) {
22508275SEric Cheng 		reta.bytes[i & 3] = ((i % ring_per_group) << shift_group0) |
22518275SEric Cheng 		    ((ring_per_group + (i % ring_per_group)) << shift_group1);
22528275SEric Cheng 		if ((i & 3) == 3) {
22538275SEric Cheng 			E1000_WRITE_REG(hw,
22548275SEric Cheng 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
22558275SEric Cheng 		}
22568275SEric Cheng 	}
22578275SEric Cheng 
22588275SEric Cheng 	/* Fill out hash function seeds */
22598275SEric Cheng 	for (i = 0; i < 10; i++) {
22608275SEric Cheng 		(void) random_get_pseudo_bytes((uint8_t *)&random,
22618275SEric Cheng 		    sizeof (uint32_t));
22628275SEric Cheng 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
22638275SEric Cheng 	}
22648275SEric Cheng 
22658275SEric Cheng 	/*
22668275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
22678275SEric Cheng 	 * enable VMDq based on packet destination MAC address and RSS.
22688275SEric Cheng 	 */
22698275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_RSS_GROUP;
22708275SEric Cheng 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
22718275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
22728275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6 |
22738275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
22748275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
22758275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
22768275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
22778275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
22788275SEric Cheng 
22798275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
22808275SEric Cheng 
22818275SEric Cheng 
22828275SEric Cheng 	/* Define the default group and default queues */
22838275SEric Cheng 	vmdctl = E1000_VMDQ_MAC_GROUP_DEFAULT_QUEUE;
22848571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_VT_CTL, vmdctl);
22858275SEric Cheng 
22868275SEric Cheng 	/*
22878275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
22888275SEric Cheng 	 *
22898275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
22908275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
22918275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
22928275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
22938275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
22948275SEric Cheng 	 * including the Ethernet and IP headers.
22958275SEric Cheng 	 *
22968275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
22978275SEric Cheng 	 * exclusive with RSS.
22988275SEric Cheng 	 */
22998275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
23008275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
23018275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
23028275SEric Cheng }
23038275SEric Cheng 
23048275SEric Cheng /*
23058275SEric Cheng  * igb_setup_mac_classify - Setup MAC classification feature
23068275SEric Cheng  */
23078275SEric Cheng static void
23088275SEric Cheng igb_setup_mac_classify(igb_t *igb)
23098275SEric Cheng {
23108275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
23118275SEric Cheng 	uint32_t mrqc, rxcsum;
23128275SEric Cheng 
23138275SEric Cheng 	/*
23148275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
23158275SEric Cheng 	 * enable VMDq based on packet destination MAC address.
23168275SEric Cheng 	 */
23178275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_GROUP;
23188275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
23198275SEric Cheng 
23208275SEric Cheng 	/*
23218275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
23228275SEric Cheng 	 *
23238275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
23248275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
23258275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
23268275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
23278275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
23288275SEric Cheng 	 * including the Ethernet and IP headers.
23298275SEric Cheng 	 *
23308275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
23318275SEric Cheng 	 * exclusive with RSS.
23328275SEric Cheng 	 */
23338275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
23348275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
23358275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
23368275SEric Cheng 
23378275SEric Cheng }
23388275SEric Cheng 
23398275SEric Cheng /*
23405779Sxy150489  * igb_init_unicst - Initialize the unicast addresses
23415779Sxy150489  */
23425779Sxy150489 static void
23435779Sxy150489 igb_init_unicst(igb_t *igb)
23445779Sxy150489 {
23455779Sxy150489 	struct e1000_hw *hw = &igb->hw;
23465779Sxy150489 	int slot;
23475779Sxy150489 
23485779Sxy150489 	/*
23495779Sxy150489 	 * Here we should consider two situations:
23505779Sxy150489 	 *
23515779Sxy150489 	 * 1. Chipset is initialized the first time
23525779Sxy150489 	 *    Initialize the multiple unicast addresses, and
23538275SEric Cheng 	 *    save the default MAC address.
23545779Sxy150489 	 *
23555779Sxy150489 	 * 2. Chipset is reset
23565779Sxy150489 	 *    Recover the multiple unicast addresses from the
23575779Sxy150489 	 *    software data structure to the RAR registers.
23585779Sxy150489 	 */
23598275SEric Cheng 
23608275SEric Cheng 	/*
23618275SEric Cheng 	 * Clear the default MAC address in the RAR0 rgister,
23628275SEric Cheng 	 * which is loaded from EEPROM when system boot or chipreset,
23638275SEric Cheng 	 * this will cause the conficts with add_mac/rem_mac entry
23648275SEric Cheng 	 * points when VMDq is enabled. For this reason, the RAR0
23658275SEric Cheng 	 * must be cleared for both cases mentioned above.
23668275SEric Cheng 	 */
23678275SEric Cheng 	e1000_rar_clear(hw, 0);
23688275SEric Cheng 
23695779Sxy150489 	if (!igb->unicst_init) {
23708275SEric Cheng 
23715779Sxy150489 		/* Initialize the multiple unicast addresses */
23725779Sxy150489 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
23738275SEric Cheng 		igb->unicst_avail = igb->unicst_total;
23748275SEric Cheng 
23758275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++)
23765779Sxy150489 			igb->unicst_addr[slot].mac.set = 0;
23775779Sxy150489 
23785779Sxy150489 		igb->unicst_init = B_TRUE;
23795779Sxy150489 	} else {
23805779Sxy150489 		/* Re-configure the RAR registers */
23818275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++) {
23828275SEric Cheng 			e1000_rar_set_vmdq(hw, igb->unicst_addr[slot].mac.addr,
23838275SEric Cheng 			    slot, igb->vmdq_mode,
23848275SEric Cheng 			    igb->unicst_addr[slot].mac.group_index);
23858275SEric Cheng 		}
23865779Sxy150489 	}
23876624Sgl147354 
23886624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
23896624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
23905779Sxy150489 }
23915779Sxy150489 
23925779Sxy150489 /*
23938275SEric Cheng  * igb_unicst_find - Find the slot for the specified unicast address
23948275SEric Cheng  */
23958275SEric Cheng int
23968275SEric Cheng igb_unicst_find(igb_t *igb, const uint8_t *mac_addr)
23978275SEric Cheng {
23988275SEric Cheng 	int slot;
23998275SEric Cheng 
24008275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
24018275SEric Cheng 
24028275SEric Cheng 	for (slot = 0; slot < igb->unicst_total; slot++) {
24038275SEric Cheng 		if (bcmp(igb->unicst_addr[slot].mac.addr,
24048275SEric Cheng 		    mac_addr, ETHERADDRL) == 0)
24058275SEric Cheng 			return (slot);
24068275SEric Cheng 	}
24078275SEric Cheng 
24088275SEric Cheng 	return (-1);
24098275SEric Cheng }
24108275SEric Cheng 
24118275SEric Cheng /*
24125779Sxy150489  * igb_unicst_set - Set the unicast address to the specified slot
24135779Sxy150489  */
24145779Sxy150489 int
24155779Sxy150489 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
24168275SEric Cheng     int slot)
24175779Sxy150489 {
24185779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24195779Sxy150489 
24205779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24215779Sxy150489 
24225779Sxy150489 	/*
24235779Sxy150489 	 * Save the unicast address in the software data structure
24245779Sxy150489 	 */
24255779Sxy150489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
24265779Sxy150489 
24275779Sxy150489 	/*
24285779Sxy150489 	 * Set the unicast address to the RAR register
24295779Sxy150489 	 */
24305779Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
24315779Sxy150489 
24326624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
24336624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24346624Sgl147354 		return (EIO);
24356624Sgl147354 	}
24366624Sgl147354 
24375779Sxy150489 	return (0);
24385779Sxy150489 }
24395779Sxy150489 
24405779Sxy150489 /*
24415779Sxy150489  * igb_multicst_add - Add a multicst address
24425779Sxy150489  */
24435779Sxy150489 int
24445779Sxy150489 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
24455779Sxy150489 {
24469775SVitezslav.Batrla@Sun.COM 	struct ether_addr *new_table;
24479775SVitezslav.Batrla@Sun.COM 	size_t new_len;
24489775SVitezslav.Batrla@Sun.COM 	size_t old_len;
24499775SVitezslav.Batrla@Sun.COM 
24505779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24515779Sxy150489 
24525779Sxy150489 	if ((multiaddr[0] & 01) == 0) {
24539775SVitezslav.Batrla@Sun.COM 		igb_error(igb, "Illegal multicast address");
24545779Sxy150489 		return (EINVAL);
24555779Sxy150489 	}
24565779Sxy150489 
24579775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_count >= igb->mcast_max_num) {
24589775SVitezslav.Batrla@Sun.COM 		igb_error(igb, "Adapter requested more than %d mcast addresses",
24599775SVitezslav.Batrla@Sun.COM 		    igb->mcast_max_num);
24605779Sxy150489 		return (ENOENT);
24615779Sxy150489 	}
24625779Sxy150489 
24639775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_count == igb->mcast_alloc_count) {
24649775SVitezslav.Batrla@Sun.COM 		old_len = igb->mcast_alloc_count *
24659775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
24669775SVitezslav.Batrla@Sun.COM 		new_len = (igb->mcast_alloc_count + MCAST_ALLOC_COUNT) *
24679775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
24689775SVitezslav.Batrla@Sun.COM 
24699775SVitezslav.Batrla@Sun.COM 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
24709775SVitezslav.Batrla@Sun.COM 		if (new_table == NULL) {
24719775SVitezslav.Batrla@Sun.COM 			igb_error(igb,
24729775SVitezslav.Batrla@Sun.COM 			    "Not enough memory to alloc mcast table");
24739775SVitezslav.Batrla@Sun.COM 			return (ENOMEM);
24749775SVitezslav.Batrla@Sun.COM 		}
24759775SVitezslav.Batrla@Sun.COM 
24769775SVitezslav.Batrla@Sun.COM 		if (igb->mcast_table != NULL) {
24779775SVitezslav.Batrla@Sun.COM 			bcopy(igb->mcast_table, new_table, old_len);
24789775SVitezslav.Batrla@Sun.COM 			kmem_free(igb->mcast_table, old_len);
24799775SVitezslav.Batrla@Sun.COM 		}
24809775SVitezslav.Batrla@Sun.COM 		igb->mcast_alloc_count += MCAST_ALLOC_COUNT;
24819775SVitezslav.Batrla@Sun.COM 		igb->mcast_table = new_table;
24829775SVitezslav.Batrla@Sun.COM 	}
24839775SVitezslav.Batrla@Sun.COM 
24845779Sxy150489 	bcopy(multiaddr,
24855779Sxy150489 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
24865779Sxy150489 	igb->mcast_count++;
24875779Sxy150489 
24885779Sxy150489 	/*
24895779Sxy150489 	 * Update the multicast table in the hardware
24905779Sxy150489 	 */
24915779Sxy150489 	igb_setup_multicst(igb);
24925779Sxy150489 
24936624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
24946624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24956624Sgl147354 		return (EIO);
24966624Sgl147354 	}
24976624Sgl147354 
24985779Sxy150489 	return (0);
24995779Sxy150489 }
25005779Sxy150489 
25015779Sxy150489 /*
25025779Sxy150489  * igb_multicst_remove - Remove a multicst address
25035779Sxy150489  */
25045779Sxy150489 int
25055779Sxy150489 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
25065779Sxy150489 {
25079775SVitezslav.Batrla@Sun.COM 	struct ether_addr *new_table;
25089775SVitezslav.Batrla@Sun.COM 	size_t new_len;
25099775SVitezslav.Batrla@Sun.COM 	size_t old_len;
25105779Sxy150489 	int i;
25115779Sxy150489 
25125779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
25135779Sxy150489 
25145779Sxy150489 	for (i = 0; i < igb->mcast_count; i++) {
25155779Sxy150489 		if (bcmp(multiaddr, &igb->mcast_table[i],
25165779Sxy150489 		    ETHERADDRL) == 0) {
25175779Sxy150489 			for (i++; i < igb->mcast_count; i++) {
25185779Sxy150489 				igb->mcast_table[i - 1] =
25195779Sxy150489 				    igb->mcast_table[i];
25205779Sxy150489 			}
25215779Sxy150489 			igb->mcast_count--;
25225779Sxy150489 			break;
25235779Sxy150489 		}
25245779Sxy150489 	}
25255779Sxy150489 
25269775SVitezslav.Batrla@Sun.COM 	if ((igb->mcast_alloc_count - igb->mcast_count) >
25279775SVitezslav.Batrla@Sun.COM 	    MCAST_ALLOC_COUNT) {
25289775SVitezslav.Batrla@Sun.COM 		old_len = igb->mcast_alloc_count *
25299775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25309775SVitezslav.Batrla@Sun.COM 		new_len = (igb->mcast_alloc_count - MCAST_ALLOC_COUNT) *
25319775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25329775SVitezslav.Batrla@Sun.COM 
25339775SVitezslav.Batrla@Sun.COM 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
25349775SVitezslav.Batrla@Sun.COM 		if (new_table != NULL) {
25359775SVitezslav.Batrla@Sun.COM 			bcopy(igb->mcast_table, new_table, new_len);
25369775SVitezslav.Batrla@Sun.COM 			kmem_free(igb->mcast_table, old_len);
25379775SVitezslav.Batrla@Sun.COM 			igb->mcast_alloc_count -= MCAST_ALLOC_COUNT;
25389775SVitezslav.Batrla@Sun.COM 			igb->mcast_table = new_table;
25399775SVitezslav.Batrla@Sun.COM 		}
25409775SVitezslav.Batrla@Sun.COM 	}
25419775SVitezslav.Batrla@Sun.COM 
25425779Sxy150489 	/*
25435779Sxy150489 	 * Update the multicast table in the hardware
25445779Sxy150489 	 */
25455779Sxy150489 	igb_setup_multicst(igb);
25465779Sxy150489 
25476624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
25486624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
25496624Sgl147354 		return (EIO);
25506624Sgl147354 	}
25516624Sgl147354 
25525779Sxy150489 	return (0);
25535779Sxy150489 }
25545779Sxy150489 
25559775SVitezslav.Batrla@Sun.COM static void
25569775SVitezslav.Batrla@Sun.COM igb_release_multicast(igb_t *igb)
25579775SVitezslav.Batrla@Sun.COM {
25589775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_table != NULL) {
25599775SVitezslav.Batrla@Sun.COM 		kmem_free(igb->mcast_table,
25609775SVitezslav.Batrla@Sun.COM 		    igb->mcast_alloc_count * sizeof (struct ether_addr));
25619775SVitezslav.Batrla@Sun.COM 		igb->mcast_table = NULL;
25629775SVitezslav.Batrla@Sun.COM 	}
25639775SVitezslav.Batrla@Sun.COM }
25649775SVitezslav.Batrla@Sun.COM 
25655779Sxy150489 /*
25665779Sxy150489  * igb_setup_multicast - setup multicast data structures
25675779Sxy150489  *
25685779Sxy150489  * This routine initializes all of the multicast related structures
25695779Sxy150489  * and save them in the hardware registers.
25705779Sxy150489  */
25715779Sxy150489 static void
25725779Sxy150489 igb_setup_multicst(igb_t *igb)
25735779Sxy150489 {
25745779Sxy150489 	uint8_t *mc_addr_list;
25755779Sxy150489 	uint32_t mc_addr_count;
25765779Sxy150489 	struct e1000_hw *hw = &igb->hw;
25775779Sxy150489 
25785779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
25799775SVitezslav.Batrla@Sun.COM 	ASSERT(igb->mcast_count <= igb->mcast_max_num);
25805779Sxy150489 
25815779Sxy150489 	mc_addr_list = (uint8_t *)igb->mcast_table;
25825779Sxy150489 	mc_addr_count = igb->mcast_count;
25835779Sxy150489 
25845779Sxy150489 	/*
25855779Sxy150489 	 * Update the multicase addresses to the MTA registers
25865779Sxy150489 	 */
2587*10319SJason.Xu@Sun.COM 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count);
25885779Sxy150489 }
25895779Sxy150489 
25905779Sxy150489 /*
25915779Sxy150489  * igb_get_conf - Get driver configurations set in driver.conf
25925779Sxy150489  *
25935779Sxy150489  * This routine gets user-configured values out of the configuration
25945779Sxy150489  * file igb.conf.
25955779Sxy150489  *
25965779Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
25975779Sxy150489  * default.
25985779Sxy150489  * If user does not configure a value, use the default.
25995779Sxy150489  * If user configures below the minimum, use the minumum.
26005779Sxy150489  * If user configures above the maximum, use the maxumum.
26015779Sxy150489  */
26025779Sxy150489 static void
26035779Sxy150489 igb_get_conf(igb_t *igb)
26045779Sxy150489 {
26055779Sxy150489 	struct e1000_hw *hw = &igb->hw;
26065779Sxy150489 	uint32_t default_mtu;
26075779Sxy150489 	uint32_t flow_control;
26088275SEric Cheng 	uint32_t ring_per_group;
26098275SEric Cheng 	int i;
26105779Sxy150489 
26115779Sxy150489 	/*
26125779Sxy150489 	 * igb driver supports the following user configurations:
26135779Sxy150489 	 *
26145779Sxy150489 	 * Link configurations:
26155779Sxy150489 	 *    adv_autoneg_cap
26165779Sxy150489 	 *    adv_1000fdx_cap
26175779Sxy150489 	 *    adv_100fdx_cap
26185779Sxy150489 	 *    adv_100hdx_cap
26195779Sxy150489 	 *    adv_10fdx_cap
26205779Sxy150489 	 *    adv_10hdx_cap
26215779Sxy150489 	 * Note: 1000hdx is not supported.
26225779Sxy150489 	 *
26235779Sxy150489 	 * Jumbo frame configuration:
26245779Sxy150489 	 *    default_mtu
26255779Sxy150489 	 *
26265779Sxy150489 	 * Ethernet flow control configuration:
26275779Sxy150489 	 *    flow_control
26285779Sxy150489 	 *
26295779Sxy150489 	 * Multiple rings configurations:
26305779Sxy150489 	 *    tx_queue_number
26315779Sxy150489 	 *    tx_ring_size
26325779Sxy150489 	 *    rx_queue_number
26335779Sxy150489 	 *    rx_ring_size
26345779Sxy150489 	 *
26355779Sxy150489 	 * Call igb_get_prop() to get the value for a specific
26365779Sxy150489 	 * configuration parameter.
26375779Sxy150489 	 */
26385779Sxy150489 
26395779Sxy150489 	/*
26405779Sxy150489 	 * Link configurations
26415779Sxy150489 	 */
26425779Sxy150489 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
26435779Sxy150489 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
26445779Sxy150489 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
26455779Sxy150489 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
26465779Sxy150489 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
26475779Sxy150489 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
26485779Sxy150489 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
26495779Sxy150489 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
26505779Sxy150489 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
26515779Sxy150489 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
26525779Sxy150489 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
26535779Sxy150489 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
26545779Sxy150489 
26555779Sxy150489 	/*
26565779Sxy150489 	 * Jumbo frame configurations
26575779Sxy150489 	 */
26585779Sxy150489 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
26595779Sxy150489 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
26605779Sxy150489 
26615779Sxy150489 	igb->max_frame_size = default_mtu +
26625779Sxy150489 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
26635779Sxy150489 
26645779Sxy150489 	/*
26655779Sxy150489 	 * Ethernet flow control configuration
26665779Sxy150489 	 */
26675779Sxy150489 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
26685779Sxy150489 	    e1000_fc_none, 4, e1000_fc_full);
26695779Sxy150489 	if (flow_control == 4)
26705779Sxy150489 		flow_control = e1000_fc_default;
26715779Sxy150489 
26728571SChenlu.Chen@Sun.COM 	hw->fc.requested_mode = flow_control;
26735779Sxy150489 
26745779Sxy150489 	/*
26755779Sxy150489 	 * Multiple rings configurations
26765779Sxy150489 	 */
26775779Sxy150489 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
26785779Sxy150489 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
26795779Sxy150489 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
26805779Sxy150489 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
26815779Sxy150489 
2682*10319SJason.Xu@Sun.COM 	igb->mr_enable = igb_get_prop(igb, PROP_MR_ENABLE, 0, 1, 0);
26838275SEric Cheng 	igb->num_rx_groups = igb_get_prop(igb, PROP_RX_GROUP_NUM,
26848275SEric Cheng 	    MIN_RX_GROUP_NUM, MAX_RX_GROUP_NUM, DEFAULT_RX_GROUP_NUM);
26858571SChenlu.Chen@Sun.COM 	/*
26868571SChenlu.Chen@Sun.COM 	 * Currently we do not support VMDq for 82576.
26878571SChenlu.Chen@Sun.COM 	 * If it is e1000_82576, set num_rx_groups to 1.
26888571SChenlu.Chen@Sun.COM 	 */
26898571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82576)
26908571SChenlu.Chen@Sun.COM 		igb->num_rx_groups = 1;
26918275SEric Cheng 
26928275SEric Cheng 	if (igb->mr_enable) {
26938571SChenlu.Chen@Sun.COM 		igb->num_tx_rings = igb->capab->def_tx_que_num;
26948571SChenlu.Chen@Sun.COM 		igb->num_rx_rings = igb->capab->def_rx_que_num;
26958275SEric Cheng 	} else {
26968275SEric Cheng 		igb->num_tx_rings = 1;
26978275SEric Cheng 		igb->num_rx_rings = 1;
26988275SEric Cheng 
26998275SEric Cheng 		if (igb->num_rx_groups > 1) {
27008275SEric Cheng 			igb_error(igb,
27018275SEric Cheng 			    "Invalid rx groups number. Please enable multiple "
27028275SEric Cheng 			    "rings first");
27038275SEric Cheng 			igb->num_rx_groups = 1;
27048275SEric Cheng 		}
27058275SEric Cheng 	}
27068275SEric Cheng 
27078275SEric Cheng 	/*
27088275SEric Cheng 	 * Check the divisibility between rx rings and rx groups.
27098275SEric Cheng 	 */
27108275SEric Cheng 	for (i = igb->num_rx_groups; i > 0; i--) {
27118275SEric Cheng 		if ((igb->num_rx_rings % i) == 0)
27128275SEric Cheng 			break;
27138275SEric Cheng 	}
27148275SEric Cheng 	if (i != igb->num_rx_groups) {
27158275SEric Cheng 		igb_error(igb,
27168275SEric Cheng 		    "Invalid rx groups number. Downgrade the rx group "
27178275SEric Cheng 		    "number to %d.", i);
27188275SEric Cheng 		igb->num_rx_groups = i;
27198275SEric Cheng 	}
27208275SEric Cheng 
27218275SEric Cheng 	/*
27228275SEric Cheng 	 * Get the ring number per group.
27238275SEric Cheng 	 */
27248275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
27258275SEric Cheng 
27268275SEric Cheng 	if (igb->num_rx_groups == 1) {
27278275SEric Cheng 		/*
27288275SEric Cheng 		 * One rx ring group, the rx ring number is num_rx_rings.
27298275SEric Cheng 		 */
27308275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_OFF;
27318275SEric Cheng 	} else if (ring_per_group == 1) {
27328275SEric Cheng 		/*
27338275SEric Cheng 		 * Multiple rx groups, each group has one rx ring.
27348275SEric Cheng 		 */
27358275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC;
27368275SEric Cheng 	} else {
27378275SEric Cheng 		/*
27388275SEric Cheng 		 * Multiple groups and multiple rings.
27398275SEric Cheng 		 */
27408275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC_RSS;
27418275SEric Cheng 	}
27428275SEric Cheng 
27435779Sxy150489 	/*
27445779Sxy150489 	 * Tunable used to force an interrupt type. The only use is
27455779Sxy150489 	 * for testing of the lesser interrupt types.
27465779Sxy150489 	 * 0 = don't force interrupt type
27475779Sxy150489 	 * 1 = force interrupt type MSIX
27485779Sxy150489 	 * 2 = force interrupt type MSI
27495779Sxy150489 	 * 3 = force interrupt type Legacy
27505779Sxy150489 	 */
27515779Sxy150489 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
27525812Sxy150489 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_NONE);
27535779Sxy150489 
27545779Sxy150489 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
27555779Sxy150489 	    0, 1, 1);
27565779Sxy150489 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
27575779Sxy150489 	    0, 1, 1);
27585779Sxy150489 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
27599188SPaul.Guo@Sun.COM 	    0, 1, 1);
27605779Sxy150489 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
27615779Sxy150489 	    0, 1, 1);
27625779Sxy150489 
27639188SPaul.Guo@Sun.COM 	/*
27649188SPaul.Guo@Sun.COM 	 * igb LSO needs the tx h/w checksum support.
27659188SPaul.Guo@Sun.COM 	 * Here LSO will be disabled if tx h/w checksum has been disabled.
27669188SPaul.Guo@Sun.COM 	 */
27679188SPaul.Guo@Sun.COM 	if (igb->tx_hcksum_enable == B_FALSE)
27689188SPaul.Guo@Sun.COM 		igb->lso_enable = B_FALSE;
27699188SPaul.Guo@Sun.COM 
27705779Sxy150489 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
27715779Sxy150489 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
27725779Sxy150489 	    DEFAULT_TX_COPY_THRESHOLD);
27735779Sxy150489 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
27745779Sxy150489 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
27755779Sxy150489 	    DEFAULT_TX_RECYCLE_THRESHOLD);
27765779Sxy150489 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
27775779Sxy150489 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
27785779Sxy150489 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
27795779Sxy150489 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
27805779Sxy150489 	    MIN_TX_RESCHED_THRESHOLD, MAX_TX_RESCHED_THRESHOLD,
27815779Sxy150489 	    DEFAULT_TX_RESCHED_THRESHOLD);
27825779Sxy150489 
27835779Sxy150489 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
27845779Sxy150489 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
27855779Sxy150489 	    DEFAULT_RX_COPY_THRESHOLD);
27865779Sxy150489 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
27875779Sxy150489 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
27885779Sxy150489 	    DEFAULT_RX_LIMIT_PER_INTR);
27895779Sxy150489 
27905779Sxy150489 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
27918571SChenlu.Chen@Sun.COM 	    igb->capab->min_intr_throttle,
27928571SChenlu.Chen@Sun.COM 	    igb->capab->max_intr_throttle,
27938571SChenlu.Chen@Sun.COM 	    igb->capab->def_intr_throttle);
27949775SVitezslav.Batrla@Sun.COM 
27959775SVitezslav.Batrla@Sun.COM 	/*
27969775SVitezslav.Batrla@Sun.COM 	 * Max number of multicast addresses
27979775SVitezslav.Batrla@Sun.COM 	 */
27989775SVitezslav.Batrla@Sun.COM 	igb->mcast_max_num =
27999775SVitezslav.Batrla@Sun.COM 	    igb_get_prop(igb, PROP_MCAST_MAX_NUM,
28009775SVitezslav.Batrla@Sun.COM 	    MIN_MCAST_NUM, MAX_MCAST_NUM, DEFAULT_MCAST_NUM);
28015779Sxy150489 }
28025779Sxy150489 
28035779Sxy150489 /*
28045779Sxy150489  * igb_get_prop - Get a property value out of the configuration file igb.conf
28055779Sxy150489  *
28065779Sxy150489  * Caller provides the name of the property, a default value, a minimum
28075779Sxy150489  * value, and a maximum value.
28085779Sxy150489  *
28095779Sxy150489  * Return configured value of the property, with default, minimum and
28105779Sxy150489  * maximum properly applied.
28115779Sxy150489  */
28125779Sxy150489 static int
28135779Sxy150489 igb_get_prop(igb_t *igb,
28145779Sxy150489     char *propname,	/* name of the property */
28155779Sxy150489     int minval,		/* minimum acceptable value */
28165779Sxy150489     int maxval,		/* maximim acceptable value */
28175779Sxy150489     int defval)		/* default value */
28185779Sxy150489 {
28195779Sxy150489 	int value;
28205779Sxy150489 
28215779Sxy150489 	/*
28225779Sxy150489 	 * Call ddi_prop_get_int() to read the conf settings
28235779Sxy150489 	 */
28245779Sxy150489 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
28255779Sxy150489 	    DDI_PROP_DONTPASS, propname, defval);
28265779Sxy150489 
28275779Sxy150489 	if (value > maxval)
28285779Sxy150489 		value = maxval;
28295779Sxy150489 
28305779Sxy150489 	if (value < minval)
28315779Sxy150489 		value = minval;
28325779Sxy150489 
28335779Sxy150489 	return (value);
28345779Sxy150489 }
28355779Sxy150489 
28365779Sxy150489 /*
28375779Sxy150489  * igb_setup_link - Using the link properties to setup the link
28385779Sxy150489  */
28395779Sxy150489 int
28405779Sxy150489 igb_setup_link(igb_t *igb, boolean_t setup_hw)
28415779Sxy150489 {
28425779Sxy150489 	struct e1000_mac_info *mac;
28435779Sxy150489 	struct e1000_phy_info *phy;
28445779Sxy150489 	boolean_t invalid;
28455779Sxy150489 
28465779Sxy150489 	mac = &igb->hw.mac;
28475779Sxy150489 	phy = &igb->hw.phy;
28485779Sxy150489 	invalid = B_FALSE;
28495779Sxy150489 
28505779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
28515779Sxy150489 		mac->autoneg = B_TRUE;
28525779Sxy150489 		phy->autoneg_advertised = 0;
28535779Sxy150489 
28545779Sxy150489 		/*
28555779Sxy150489 		 * 1000hdx is not supported for autonegotiation
28565779Sxy150489 		 */
28575779Sxy150489 		if (igb->param_adv_1000fdx_cap == 1)
28585779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
28595779Sxy150489 
28605779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
28615779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
28625779Sxy150489 
28635779Sxy150489 		if (igb->param_adv_100hdx_cap == 1)
28645779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
28655779Sxy150489 
28665779Sxy150489 		if (igb->param_adv_10fdx_cap == 1)
28675779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
28685779Sxy150489 
28695779Sxy150489 		if (igb->param_adv_10hdx_cap == 1)
28705779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
28715779Sxy150489 
28725779Sxy150489 		if (phy->autoneg_advertised == 0)
28735779Sxy150489 			invalid = B_TRUE;
28745779Sxy150489 	} else {
28755779Sxy150489 		mac->autoneg = B_FALSE;
28765779Sxy150489 
28775779Sxy150489 		/*
28785779Sxy150489 		 * 1000fdx and 1000hdx are not supported for forced link
28795779Sxy150489 		 */
28805779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
28815779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
28825779Sxy150489 		else if (igb->param_adv_100hdx_cap == 1)
28835779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
28845779Sxy150489 		else if (igb->param_adv_10fdx_cap == 1)
28855779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
28865779Sxy150489 		else if (igb->param_adv_10hdx_cap == 1)
28875779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
28885779Sxy150489 		else
28895779Sxy150489 			invalid = B_TRUE;
28905779Sxy150489 	}
28915779Sxy150489 
28925779Sxy150489 	if (invalid) {
28935779Sxy150489 		igb_notice(igb, "Invalid link settings. Setup link to "
28945779Sxy150489 		    "autonegotiation with full link capabilities.");
28955779Sxy150489 		mac->autoneg = B_TRUE;
28965779Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
28975779Sxy150489 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
28985779Sxy150489 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
28995779Sxy150489 	}
29005779Sxy150489 
29015779Sxy150489 	if (setup_hw) {
29025779Sxy150489 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
29035779Sxy150489 			return (IGB_FAILURE);
29045779Sxy150489 	}
29055779Sxy150489 
29065779Sxy150489 	return (IGB_SUCCESS);
29075779Sxy150489 }
29085779Sxy150489 
29095779Sxy150489 
29105779Sxy150489 /*
29115779Sxy150489  * igb_is_link_up - Check if the link is up
29125779Sxy150489  */
29135779Sxy150489 static boolean_t
29145779Sxy150489 igb_is_link_up(igb_t *igb)
29155779Sxy150489 {
29165779Sxy150489 	struct e1000_hw *hw = &igb->hw;
29178955SChenlu.Chen@Sun.COM 	boolean_t link_up = B_FALSE;
29185779Sxy150489 
29195779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
29205779Sxy150489 
29218955SChenlu.Chen@Sun.COM 	/*
29228955SChenlu.Chen@Sun.COM 	 * get_link_status is set in the interrupt handler on link-status-change
29238955SChenlu.Chen@Sun.COM 	 * or rx sequence error interrupt.  get_link_status will stay
29248955SChenlu.Chen@Sun.COM 	 * false until the e1000_check_for_link establishes link only
29258955SChenlu.Chen@Sun.COM 	 * for copper adapters.
29268955SChenlu.Chen@Sun.COM 	 */
29278955SChenlu.Chen@Sun.COM 	switch (hw->phy.media_type) {
29288955SChenlu.Chen@Sun.COM 	case e1000_media_type_copper:
29298955SChenlu.Chen@Sun.COM 		if (hw->mac.get_link_status) {
29308955SChenlu.Chen@Sun.COM 			(void) e1000_check_for_link(hw);
29318955SChenlu.Chen@Sun.COM 			link_up = !hw->mac.get_link_status;
29328955SChenlu.Chen@Sun.COM 		} else {
29338955SChenlu.Chen@Sun.COM 			link_up = B_TRUE;
29348955SChenlu.Chen@Sun.COM 		}
29358955SChenlu.Chen@Sun.COM 		break;
29368955SChenlu.Chen@Sun.COM 	case e1000_media_type_fiber:
29378955SChenlu.Chen@Sun.COM 		(void) e1000_check_for_link(hw);
29388955SChenlu.Chen@Sun.COM 		link_up = (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU);
29398955SChenlu.Chen@Sun.COM 		break;
29408955SChenlu.Chen@Sun.COM 	case e1000_media_type_internal_serdes:
29418955SChenlu.Chen@Sun.COM 		(void) e1000_check_for_link(hw);
29428955SChenlu.Chen@Sun.COM 		link_up = hw->mac.serdes_has_link;
29438955SChenlu.Chen@Sun.COM 		break;
29445779Sxy150489 	}
29455779Sxy150489 
29465779Sxy150489 	return (link_up);
29475779Sxy150489 }
29485779Sxy150489 
29495779Sxy150489 /*
29505779Sxy150489  * igb_link_check - Link status processing
29515779Sxy150489  */
29525779Sxy150489 static boolean_t
29535779Sxy150489 igb_link_check(igb_t *igb)
29545779Sxy150489 {
29555779Sxy150489 	struct e1000_hw *hw = &igb->hw;
29565779Sxy150489 	uint16_t speed = 0, duplex = 0;
29575779Sxy150489 	boolean_t link_changed = B_FALSE;
29585779Sxy150489 
29595779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
29605779Sxy150489 
29615779Sxy150489 	if (igb_is_link_up(igb)) {
29625779Sxy150489 		/*
29635779Sxy150489 		 * The Link is up, check whether it was marked as down earlier
29645779Sxy150489 		 */
29655779Sxy150489 		if (igb->link_state != LINK_STATE_UP) {
29665779Sxy150489 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
29675779Sxy150489 			igb->link_speed = speed;
29685779Sxy150489 			igb->link_duplex = duplex;
29695779Sxy150489 			igb->link_state = LINK_STATE_UP;
29705779Sxy150489 			igb->link_down_timeout = 0;
29715779Sxy150489 			link_changed = B_TRUE;
29725779Sxy150489 		}
29735779Sxy150489 	} else {
29745779Sxy150489 		if (igb->link_state != LINK_STATE_DOWN) {
29755779Sxy150489 			igb->link_speed = 0;
29765779Sxy150489 			igb->link_duplex = 0;
29775779Sxy150489 			igb->link_state = LINK_STATE_DOWN;
29785779Sxy150489 			link_changed = B_TRUE;
29795779Sxy150489 		}
29805779Sxy150489 
29815779Sxy150489 		if (igb->igb_state & IGB_STARTED) {
29825779Sxy150489 			if (igb->link_down_timeout < MAX_LINK_DOWN_TIMEOUT) {
29835779Sxy150489 				igb->link_down_timeout++;
29845779Sxy150489 			} else if (igb->link_down_timeout ==
29855779Sxy150489 			    MAX_LINK_DOWN_TIMEOUT) {
29865779Sxy150489 				igb_tx_clean(igb);
29875779Sxy150489 				igb->link_down_timeout++;
29885779Sxy150489 			}
29895779Sxy150489 		}
29905779Sxy150489 	}
29915779Sxy150489 
29926624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
29936624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
29946624Sgl147354 
29955779Sxy150489 	return (link_changed);
29965779Sxy150489 }
29975779Sxy150489 
29985779Sxy150489 /*
29995779Sxy150489  * igb_local_timer - driver watchdog function
30005779Sxy150489  *
30015779Sxy150489  * This function will handle the transmit stall check, link status check and
30025779Sxy150489  * other routines.
30035779Sxy150489  */
30045779Sxy150489 static void
30055779Sxy150489 igb_local_timer(void *arg)
30065779Sxy150489 {
30075779Sxy150489 	igb_t *igb = (igb_t *)arg;
30088955SChenlu.Chen@Sun.COM 	boolean_t link_changed = B_FALSE;
30095779Sxy150489 
30105779Sxy150489 	if (igb_stall_check(igb)) {
30116624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_STALL);
30128955SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
30135779Sxy150489 		igb->reset_count++;
30146624Sgl147354 		if (igb_reset(igb) == IGB_SUCCESS)
30156624Sgl147354 			ddi_fm_service_impact(igb->dip,
30166624Sgl147354 			    DDI_SERVICE_RESTORED);
30175779Sxy150489 	}
30185779Sxy150489 
30195779Sxy150489 	mutex_enter(&igb->gen_lock);
30208955SChenlu.Chen@Sun.COM 	if (!(igb->igb_state & IGB_SUSPENDED) && (igb->igb_state & IGB_STARTED))
30218955SChenlu.Chen@Sun.COM 		link_changed = igb_link_check(igb);
30225779Sxy150489 	mutex_exit(&igb->gen_lock);
30235779Sxy150489 
30245779Sxy150489 	if (link_changed)
30255779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
30265779Sxy150489 
30276624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
30286624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
30296624Sgl147354 
30305779Sxy150489 	igb_restart_watchdog_timer(igb);
30315779Sxy150489 }
30325779Sxy150489 
30335779Sxy150489 /*
30345779Sxy150489  * igb_stall_check - check for transmit stall
30355779Sxy150489  *
30365779Sxy150489  * This function checks if the adapter is stalled (in transmit).
30375779Sxy150489  *
30385779Sxy150489  * It is called each time the watchdog timeout is invoked.
30395779Sxy150489  * If the transmit descriptor reclaim continuously fails,
30405779Sxy150489  * the watchdog value will increment by 1. If the watchdog
30415779Sxy150489  * value exceeds the threshold, the igb is assumed to
30425779Sxy150489  * have stalled and need to be reset.
30435779Sxy150489  */
30445779Sxy150489 static boolean_t
30455779Sxy150489 igb_stall_check(igb_t *igb)
30465779Sxy150489 {
30475779Sxy150489 	igb_tx_ring_t *tx_ring;
30485779Sxy150489 	boolean_t result;
30495779Sxy150489 	int i;
30505779Sxy150489 
30515779Sxy150489 	if (igb->link_state != LINK_STATE_UP)
30525779Sxy150489 		return (B_FALSE);
30535779Sxy150489 
30545779Sxy150489 	/*
30555779Sxy150489 	 * If any tx ring is stalled, we'll reset the chipset
30565779Sxy150489 	 */
30575779Sxy150489 	result = B_FALSE;
30585779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
30595779Sxy150489 		tx_ring = &igb->tx_rings[i];
30605779Sxy150489 
30615779Sxy150489 		if (tx_ring->recycle_fail > 0)
30625779Sxy150489 			tx_ring->stall_watchdog++;
30635779Sxy150489 		else
30645779Sxy150489 			tx_ring->stall_watchdog = 0;
30655779Sxy150489 
30665779Sxy150489 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
30675779Sxy150489 			result = B_TRUE;
30685779Sxy150489 			break;
30695779Sxy150489 		}
30705779Sxy150489 	}
30715779Sxy150489 
30725779Sxy150489 	if (result) {
30735779Sxy150489 		tx_ring->stall_watchdog = 0;
30745779Sxy150489 		tx_ring->recycle_fail = 0;
30755779Sxy150489 	}
30765779Sxy150489 
30775779Sxy150489 	return (result);
30785779Sxy150489 }
30795779Sxy150489 
30805779Sxy150489 
30815779Sxy150489 /*
30825779Sxy150489  * is_valid_mac_addr - Check if the mac address is valid
30835779Sxy150489  */
30845779Sxy150489 static boolean_t
30855779Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
30865779Sxy150489 {
30875779Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
30885779Sxy150489 	const uint8_t addr_test2[6] =
30895779Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
30905779Sxy150489 
30915779Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
30925779Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
30935779Sxy150489 		return (B_FALSE);
30945779Sxy150489 
30955779Sxy150489 	return (B_TRUE);
30965779Sxy150489 }
30975779Sxy150489 
30985779Sxy150489 static boolean_t
30995779Sxy150489 igb_find_mac_address(igb_t *igb)
31005779Sxy150489 {
31015779Sxy150489 	struct e1000_hw *hw = &igb->hw;
31025779Sxy150489 #ifdef __sparc
31035779Sxy150489 	uchar_t *bytes;
31045779Sxy150489 	struct ether_addr sysaddr;
31055779Sxy150489 	uint_t nelts;
31065779Sxy150489 	int err;
31075779Sxy150489 	boolean_t found = B_FALSE;
31085779Sxy150489 
31095779Sxy150489 	/*
31105779Sxy150489 	 * The "vendor's factory-set address" may already have
31115779Sxy150489 	 * been extracted from the chip, but if the property
31125779Sxy150489 	 * "local-mac-address" is set we use that instead.
31135779Sxy150489 	 *
31145779Sxy150489 	 * We check whether it looks like an array of 6
31155779Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
31165779Sxy150489 	 * make sense of it this way, we'll ignore it.
31175779Sxy150489 	 */
31185779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
31195779Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
31205779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
31215779Sxy150489 		if (nelts == ETHERADDRL) {
31225779Sxy150489 			while (nelts--)
31235779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
31245779Sxy150489 			found = B_TRUE;
31255779Sxy150489 		}
31265779Sxy150489 		ddi_prop_free(bytes);
31275779Sxy150489 	}
31285779Sxy150489 
31295779Sxy150489 	/*
31305779Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
31315779Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
31325779Sxy150489 	 */
31335779Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
31345779Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
31355779Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
31365779Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
31375779Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
31385779Sxy150489 				found = B_TRUE;
31395779Sxy150489 			}
31405779Sxy150489 		}
31415779Sxy150489 		ddi_prop_free(bytes);
31425779Sxy150489 	}
31435779Sxy150489 
31445779Sxy150489 	/*
31455779Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
31465779Sxy150489 	 * if we netbooted from this interface), we must use this instead
31475779Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
31485779Sxy150489 	 * get confused by the address changing as Solaris takes over!
31495779Sxy150489 	 */
31505779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
31515779Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
31525779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
31535779Sxy150489 		if (nelts == ETHERADDRL) {
31545779Sxy150489 			while (nelts--)
31555779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
31565779Sxy150489 			found = B_TRUE;
31575779Sxy150489 		}
31585779Sxy150489 		ddi_prop_free(bytes);
31595779Sxy150489 	}
31605779Sxy150489 
31615779Sxy150489 	if (found) {
31625779Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
31635779Sxy150489 		return (B_TRUE);
31645779Sxy150489 	}
31655779Sxy150489 #endif
31665779Sxy150489 
31675779Sxy150489 	/*
31685779Sxy150489 	 * Read the device MAC address from the EEPROM
31695779Sxy150489 	 */
31705779Sxy150489 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
31715779Sxy150489 		return (B_FALSE);
31725779Sxy150489 
31735779Sxy150489 	return (B_TRUE);
31745779Sxy150489 }
31755779Sxy150489 
31765779Sxy150489 #pragma inline(igb_arm_watchdog_timer)
31775779Sxy150489 
31785779Sxy150489 static void
31795779Sxy150489 igb_arm_watchdog_timer(igb_t *igb)
31805779Sxy150489 {
31815779Sxy150489 	/*
31825779Sxy150489 	 * Fire a watchdog timer
31835779Sxy150489 	 */
31845779Sxy150489 	igb->watchdog_tid =
31855779Sxy150489 	    timeout(igb_local_timer,
31865779Sxy150489 	    (void *)igb, 1 * drv_usectohz(1000000));
31875779Sxy150489 
31885779Sxy150489 }
31895779Sxy150489 
31905779Sxy150489 /*
31915779Sxy150489  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
31925779Sxy150489  */
31935779Sxy150489 void
31945779Sxy150489 igb_enable_watchdog_timer(igb_t *igb)
31955779Sxy150489 {
31965779Sxy150489 	mutex_enter(&igb->watchdog_lock);
31975779Sxy150489 
31985779Sxy150489 	if (!igb->watchdog_enable) {
31995779Sxy150489 		igb->watchdog_enable = B_TRUE;
32005779Sxy150489 		igb->watchdog_start = B_TRUE;
32015779Sxy150489 		igb_arm_watchdog_timer(igb);
32025779Sxy150489 	}
32035779Sxy150489 
32045779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32055779Sxy150489 
32065779Sxy150489 }
32075779Sxy150489 
32085779Sxy150489 /*
32095779Sxy150489  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
32105779Sxy150489  */
32115779Sxy150489 void
32125779Sxy150489 igb_disable_watchdog_timer(igb_t *igb)
32135779Sxy150489 {
32145779Sxy150489 	timeout_id_t tid;
32155779Sxy150489 
32165779Sxy150489 	mutex_enter(&igb->watchdog_lock);
32175779Sxy150489 
32185779Sxy150489 	igb->watchdog_enable = B_FALSE;
32195779Sxy150489 	igb->watchdog_start = B_FALSE;
32205779Sxy150489 	tid = igb->watchdog_tid;
32215779Sxy150489 	igb->watchdog_tid = 0;
32225779Sxy150489 
32235779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32245779Sxy150489 
32255779Sxy150489 	if (tid != 0)
32265779Sxy150489 		(void) untimeout(tid);
32275779Sxy150489 
32285779Sxy150489 }
32295779Sxy150489 
32305779Sxy150489 /*
32315779Sxy150489  * igb_start_watchdog_timer - Start the driver watchdog timer
32325779Sxy150489  */
32335779Sxy150489 static void
32345779Sxy150489 igb_start_watchdog_timer(igb_t *igb)
32355779Sxy150489 {
32365779Sxy150489 	mutex_enter(&igb->watchdog_lock);
32375779Sxy150489 
32385779Sxy150489 	if (igb->watchdog_enable) {
32395779Sxy150489 		if (!igb->watchdog_start) {
32405779Sxy150489 			igb->watchdog_start = B_TRUE;
32415779Sxy150489 			igb_arm_watchdog_timer(igb);
32425779Sxy150489 		}
32435779Sxy150489 	}
32445779Sxy150489 
32455779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32465779Sxy150489 }
32475779Sxy150489 
32485779Sxy150489 /*
32495779Sxy150489  * igb_restart_watchdog_timer - Restart the driver watchdog timer
32505779Sxy150489  */
32515779Sxy150489 static void
32525779Sxy150489 igb_restart_watchdog_timer(igb_t *igb)
32535779Sxy150489 {
32545779Sxy150489 	mutex_enter(&igb->watchdog_lock);
32555779Sxy150489 
32565779Sxy150489 	if (igb->watchdog_start)
32575779Sxy150489 		igb_arm_watchdog_timer(igb);
32585779Sxy150489 
32595779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32605779Sxy150489 }
32615779Sxy150489 
32625779Sxy150489 /*
32635779Sxy150489  * igb_stop_watchdog_timer - Stop the driver watchdog timer
32645779Sxy150489  */
32655779Sxy150489 static void
32665779Sxy150489 igb_stop_watchdog_timer(igb_t *igb)
32675779Sxy150489 {
32685779Sxy150489 	timeout_id_t tid;
32695779Sxy150489 
32705779Sxy150489 	mutex_enter(&igb->watchdog_lock);
32715779Sxy150489 
32725779Sxy150489 	igb->watchdog_start = B_FALSE;
32735779Sxy150489 	tid = igb->watchdog_tid;
32745779Sxy150489 	igb->watchdog_tid = 0;
32755779Sxy150489 
32765779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32775779Sxy150489 
32785779Sxy150489 	if (tid != 0)
32795779Sxy150489 		(void) untimeout(tid);
32805779Sxy150489 }
32815779Sxy150489 
32825779Sxy150489 /*
32835779Sxy150489  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
32845779Sxy150489  */
32855779Sxy150489 static void
32865779Sxy150489 igb_disable_adapter_interrupts(igb_t *igb)
32875779Sxy150489 {
32885779Sxy150489 	struct e1000_hw *hw = &igb->hw;
32895779Sxy150489 
32905779Sxy150489 	/*
32915779Sxy150489 	 * Set the IMC register to mask all the interrupts,
32925779Sxy150489 	 * including the tx interrupts.
32935779Sxy150489 	 */
32948571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMC, ~0);
32958571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
32965779Sxy150489 
32975779Sxy150489 	/*
32985779Sxy150489 	 * Additional disabling for MSI-X
32995779Sxy150489 	 */
33005779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
33018571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMC, ~0);
33028571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, 0);
33038571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAM, 0);
33045779Sxy150489 	}
33055779Sxy150489 
33065779Sxy150489 	E1000_WRITE_FLUSH(hw);
33075779Sxy150489 }
33085779Sxy150489 
33095779Sxy150489 /*
33108571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82576 - Enable NIC interrupts for 82576
33115779Sxy150489  */
33125779Sxy150489 static void
33138571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82576(igb_t *igb)
33148571SChenlu.Chen@Sun.COM {
33158571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
33168571SChenlu.Chen@Sun.COM 
33178955SChenlu.Chen@Sun.COM 	/* Clear any pending interrupts */
33188955SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(hw, E1000_ICR);
33198955SChenlu.Chen@Sun.COM 
33208571SChenlu.Chen@Sun.COM 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
33218571SChenlu.Chen@Sun.COM 
33228571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI-X */
33238571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
33248571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
33258571SChenlu.Chen@Sun.COM 		igb->ims_mask = E1000_IMS_LSC;
33268571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
33278571SChenlu.Chen@Sun.COM 	} else {
33288571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI and legacy */
33298571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
33308571SChenlu.Chen@Sun.COM 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
33318571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS,
33328571SChenlu.Chen@Sun.COM 		    (IMS_ENABLE_MASK | E1000_IMS_TXQE));
33338571SChenlu.Chen@Sun.COM 	}
33348571SChenlu.Chen@Sun.COM 
33358571SChenlu.Chen@Sun.COM 	/* Disable auto-mask for ICR interrupt bits */
33368571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
33378571SChenlu.Chen@Sun.COM 
33388571SChenlu.Chen@Sun.COM 	E1000_WRITE_FLUSH(hw);
33398571SChenlu.Chen@Sun.COM }
33408571SChenlu.Chen@Sun.COM 
33418571SChenlu.Chen@Sun.COM /*
33428571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82575 - Enable NIC interrupts for 82575
33438571SChenlu.Chen@Sun.COM  */
33448571SChenlu.Chen@Sun.COM static void
33458571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82575(igb_t *igb)
33465779Sxy150489 {
33475779Sxy150489 	struct e1000_hw *hw = &igb->hw;
33485779Sxy150489 	uint32_t reg;
33495779Sxy150489 
33508955SChenlu.Chen@Sun.COM 	/* Clear any pending interrupts */
33518955SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(hw, E1000_ICR);
33528955SChenlu.Chen@Sun.COM 
33535779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
33545779Sxy150489 		/* Interrupt enabling for MSI-X */
33555779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
33565779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
33578275SEric Cheng 		igb->ims_mask = E1000_IMS_LSC;
33585779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
33595779Sxy150489 
33605779Sxy150489 		/* Enable MSI-X PBA support */
33615779Sxy150489 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
33625779Sxy150489 		reg |= E1000_CTRL_EXT_PBA_CLR;
33635779Sxy150489 
33645779Sxy150489 		/* Non-selective interrupt clear-on-read */
33655779Sxy150489 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
33665779Sxy150489 
33675779Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
33685779Sxy150489 	} else {
33695779Sxy150489 		/* Interrupt enabling for MSI and legacy */
33708275SEric Cheng 		igb->ims_mask = IMS_ENABLE_MASK;
33715779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
33725779Sxy150489 	}
33735779Sxy150489 
33745779Sxy150489 	E1000_WRITE_FLUSH(hw);
33755779Sxy150489 }
33765779Sxy150489 
33775779Sxy150489 /*
33785779Sxy150489  * Loopback Support
33795779Sxy150489  */
33805779Sxy150489 static lb_property_t lb_normal =
33815779Sxy150489 	{ normal,	"normal",	IGB_LB_NONE		};
33825779Sxy150489 static lb_property_t lb_external =
33835779Sxy150489 	{ external,	"External",	IGB_LB_EXTERNAL		};
33845779Sxy150489 static lb_property_t lb_mac =
33855779Sxy150489 	{ internal,	"MAC",		IGB_LB_INTERNAL_MAC	};
33865779Sxy150489 static lb_property_t lb_phy =
33875779Sxy150489 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
33885779Sxy150489 static lb_property_t lb_serdes =
33895779Sxy150489 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
33905779Sxy150489 
33915779Sxy150489 enum ioc_reply
33925779Sxy150489 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
33935779Sxy150489 {
33945779Sxy150489 	lb_info_sz_t *lbsp;
33955779Sxy150489 	lb_property_t *lbpp;
33965779Sxy150489 	struct e1000_hw *hw;
33975779Sxy150489 	uint32_t *lbmp;
33985779Sxy150489 	uint32_t size;
33995779Sxy150489 	uint32_t value;
34005779Sxy150489 
34015779Sxy150489 	hw = &igb->hw;
34025779Sxy150489 
34035779Sxy150489 	if (mp->b_cont == NULL)
34045779Sxy150489 		return (IOC_INVAL);
34055779Sxy150489 
34065779Sxy150489 	switch (iocp->ioc_cmd) {
34075779Sxy150489 	default:
34085779Sxy150489 		return (IOC_INVAL);
34095779Sxy150489 
34105779Sxy150489 	case LB_GET_INFO_SIZE:
34115779Sxy150489 		size = sizeof (lb_info_sz_t);
34125779Sxy150489 		if (iocp->ioc_count != size)
34135779Sxy150489 			return (IOC_INVAL);
34145779Sxy150489 
34155779Sxy150489 		value = sizeof (lb_normal);
34165779Sxy150489 		value += sizeof (lb_mac);
34175779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
34185779Sxy150489 			value += sizeof (lb_phy);
34195779Sxy150489 		else
34205779Sxy150489 			value += sizeof (lb_serdes);
34215779Sxy150489 		value += sizeof (lb_external);
34225779Sxy150489 
34235779Sxy150489 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
34245779Sxy150489 		*lbsp = value;
34255779Sxy150489 		break;
34265779Sxy150489 
34275779Sxy150489 	case LB_GET_INFO:
34285779Sxy150489 		value = sizeof (lb_normal);
34295779Sxy150489 		value += sizeof (lb_mac);
34305779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
34315779Sxy150489 			value += sizeof (lb_phy);
34325779Sxy150489 		else
34335779Sxy150489 			value += sizeof (lb_serdes);
34345779Sxy150489 		value += sizeof (lb_external);
34355779Sxy150489 
34365779Sxy150489 		size = value;
34375779Sxy150489 		if (iocp->ioc_count != size)
34385779Sxy150489 			return (IOC_INVAL);
34395779Sxy150489 
34405779Sxy150489 		value = 0;
34415779Sxy150489 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
34425779Sxy150489 
34435779Sxy150489 		lbpp[value++] = lb_normal;
34445779Sxy150489 		lbpp[value++] = lb_mac;
34455779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
34465779Sxy150489 			lbpp[value++] = lb_phy;
34475779Sxy150489 		else
34485779Sxy150489 			lbpp[value++] = lb_serdes;
34495779Sxy150489 		lbpp[value++] = lb_external;
34505779Sxy150489 		break;
34515779Sxy150489 
34525779Sxy150489 	case LB_GET_MODE:
34535779Sxy150489 		size = sizeof (uint32_t);
34545779Sxy150489 		if (iocp->ioc_count != size)
34555779Sxy150489 			return (IOC_INVAL);
34565779Sxy150489 
34575779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
34585779Sxy150489 		*lbmp = igb->loopback_mode;
34595779Sxy150489 		break;
34605779Sxy150489 
34615779Sxy150489 	case LB_SET_MODE:
34625779Sxy150489 		size = 0;
34635779Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
34645779Sxy150489 			return (IOC_INVAL);
34655779Sxy150489 
34665779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
34675779Sxy150489 		if (!igb_set_loopback_mode(igb, *lbmp))
34685779Sxy150489 			return (IOC_INVAL);
34695779Sxy150489 		break;
34705779Sxy150489 	}
34715779Sxy150489 
34725779Sxy150489 	iocp->ioc_count = size;
34735779Sxy150489 	iocp->ioc_error = 0;
34745779Sxy150489 
34756624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
34766624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
34776624Sgl147354 		return (IOC_INVAL);
34786624Sgl147354 	}
34796624Sgl147354 
34805779Sxy150489 	return (IOC_REPLY);
34815779Sxy150489 }
34825779Sxy150489 
34835779Sxy150489 /*
34845779Sxy150489  * igb_set_loopback_mode - Setup loopback based on the loopback mode
34855779Sxy150489  */
34865779Sxy150489 static boolean_t
34875779Sxy150489 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
34885779Sxy150489 {
34895779Sxy150489 	struct e1000_hw *hw;
34905779Sxy150489 
34915779Sxy150489 	if (mode == igb->loopback_mode)
34925779Sxy150489 		return (B_TRUE);
34935779Sxy150489 
34945779Sxy150489 	hw = &igb->hw;
34955779Sxy150489 
34965779Sxy150489 	igb->loopback_mode = mode;
34975779Sxy150489 
34985779Sxy150489 	if (mode == IGB_LB_NONE) {
34995779Sxy150489 		/* Reset the chip */
35005779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_TRUE;
35015779Sxy150489 		(void) igb_reset(igb);
35025779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_FALSE;
35035779Sxy150489 		return (B_TRUE);
35045779Sxy150489 	}
35055779Sxy150489 
35065779Sxy150489 	mutex_enter(&igb->gen_lock);
35075779Sxy150489 
35085779Sxy150489 	switch (mode) {
35095779Sxy150489 	default:
35105779Sxy150489 		mutex_exit(&igb->gen_lock);
35115779Sxy150489 		return (B_FALSE);
35125779Sxy150489 
35135779Sxy150489 	case IGB_LB_EXTERNAL:
35145779Sxy150489 		igb_set_external_loopback(igb);
35155779Sxy150489 		break;
35165779Sxy150489 
35175779Sxy150489 	case IGB_LB_INTERNAL_MAC:
35185779Sxy150489 		igb_set_internal_mac_loopback(igb);
35195779Sxy150489 		break;
35205779Sxy150489 
35215779Sxy150489 	case IGB_LB_INTERNAL_PHY:
35225779Sxy150489 		igb_set_internal_phy_loopback(igb);
35235779Sxy150489 		break;
35245779Sxy150489 
35255779Sxy150489 	case IGB_LB_INTERNAL_SERDES:
35265779Sxy150489 		igb_set_internal_serdes_loopback(igb);
35275779Sxy150489 		break;
35285779Sxy150489 	}
35295779Sxy150489 
35305779Sxy150489 	mutex_exit(&igb->gen_lock);
35315779Sxy150489 
35325779Sxy150489 	return (B_TRUE);
35335779Sxy150489 }
35345779Sxy150489 
35355779Sxy150489 /*
35365779Sxy150489  * igb_set_external_loopback - Set the external loopback mode
35375779Sxy150489  */
35385779Sxy150489 static void
35395779Sxy150489 igb_set_external_loopback(igb_t *igb)
35405779Sxy150489 {
35415779Sxy150489 	struct e1000_hw *hw;
35425779Sxy150489 
35435779Sxy150489 	hw = &igb->hw;
35445779Sxy150489 
35455779Sxy150489 	/* Set phy to known state */
35465779Sxy150489 	(void) e1000_phy_hw_reset(hw);
35475779Sxy150489 
35485779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
35495779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x9, 0x1b00);
35505779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
35515779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
35525779Sxy150489 }
35535779Sxy150489 
35545779Sxy150489 /*
35555779Sxy150489  * igb_set_internal_mac_loopback - Set the internal MAC loopback mode
35565779Sxy150489  */
35575779Sxy150489 static void
35585779Sxy150489 igb_set_internal_mac_loopback(igb_t *igb)
35595779Sxy150489 {
35605779Sxy150489 	struct e1000_hw *hw;
35615779Sxy150489 	uint32_t ctrl;
35625779Sxy150489 	uint32_t rctl;
35638955SChenlu.Chen@Sun.COM 	uint32_t ctrl_ext;
35648955SChenlu.Chen@Sun.COM 	uint16_t phy_ctrl;
35658955SChenlu.Chen@Sun.COM 	uint16_t phy_status;
35665779Sxy150489 
35675779Sxy150489 	hw = &igb->hw;
35685779Sxy150489 
35698955SChenlu.Chen@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
35708955SChenlu.Chen@Sun.COM 	phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
35718955SChenlu.Chen@Sun.COM 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
35728955SChenlu.Chen@Sun.COM 
35738955SChenlu.Chen@Sun.COM 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
35748955SChenlu.Chen@Sun.COM 
35758955SChenlu.Chen@Sun.COM 	/* Set link mode to PHY (00b) in the Extended Control register */
35768955SChenlu.Chen@Sun.COM 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
35778955SChenlu.Chen@Sun.COM 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
35788955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
35798955SChenlu.Chen@Sun.COM 
35808955SChenlu.Chen@Sun.COM 	/* Set the Device Control register */
35818955SChenlu.Chen@Sun.COM 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
35828955SChenlu.Chen@Sun.COM 	if (!(phy_status & MII_SR_LINK_STATUS))
35838955SChenlu.Chen@Sun.COM 		ctrl |= E1000_CTRL_ILOS; /* Set ILOS when the link is down */
35848955SChenlu.Chen@Sun.COM 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
35858955SChenlu.Chen@Sun.COM 	ctrl |= (E1000_CTRL_SLU |	/* Force link up */
35868955SChenlu.Chen@Sun.COM 	    E1000_CTRL_FRCSPD |		/* Force speed */
35878955SChenlu.Chen@Sun.COM 	    E1000_CTRL_FRCDPX |		/* Force duplex */
35888955SChenlu.Chen@Sun.COM 	    E1000_CTRL_SPD_1000 |	/* Force speed to 1000 */
35898955SChenlu.Chen@Sun.COM 	    E1000_CTRL_FD);		/* Force full duplex */
35908955SChenlu.Chen@Sun.COM 
35918955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
35928955SChenlu.Chen@Sun.COM 
35935779Sxy150489 	/* Set the Receive Control register */
35945779Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
35955779Sxy150489 	rctl &= ~E1000_RCTL_LBM_TCVR;
35965779Sxy150489 	rctl |= E1000_RCTL_LBM_MAC;
35975779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
35985779Sxy150489 }
35995779Sxy150489 
36005779Sxy150489 /*
36015779Sxy150489  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
36025779Sxy150489  */
36035779Sxy150489 static void
36045779Sxy150489 igb_set_internal_phy_loopback(igb_t *igb)
36055779Sxy150489 {
36065779Sxy150489 	struct e1000_hw *hw;
36075779Sxy150489 	uint32_t ctrl_ext;
36085779Sxy150489 	uint16_t phy_ctrl;
36095779Sxy150489 	uint16_t phy_pconf;
36105779Sxy150489 
36115779Sxy150489 	hw = &igb->hw;
36125779Sxy150489 
36135779Sxy150489 	/* Set link mode to PHY (00b) in the Extended Control register */
36145779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
36155779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
36165779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
36175779Sxy150489 
36185779Sxy150489 	/*
36195779Sxy150489 	 * Set PHY control register (0x4140):
36205779Sxy150489 	 *    Set full duplex mode
36215779Sxy150489 	 *    Set loopback bit
36225779Sxy150489 	 *    Clear auto-neg enable bit
36235779Sxy150489 	 *    Set PHY speed
36245779Sxy150489 	 */
36255779Sxy150489 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
36265779Sxy150489 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
36275779Sxy150489 
36285779Sxy150489 	/* Set the link disable bit in the Port Configuration register */
36295779Sxy150489 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
36305779Sxy150489 	phy_pconf |= (uint16_t)1 << 14;
36315779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
36325779Sxy150489 }
36335779Sxy150489 
36345779Sxy150489 /*
36355779Sxy150489  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
36365779Sxy150489  */
36375779Sxy150489 static void
36385779Sxy150489 igb_set_internal_serdes_loopback(igb_t *igb)
36395779Sxy150489 {
36405779Sxy150489 	struct e1000_hw *hw;
36415779Sxy150489 	uint32_t ctrl_ext;
36425779Sxy150489 	uint32_t ctrl;
36435779Sxy150489 	uint32_t pcs_lctl;
36445779Sxy150489 	uint32_t connsw;
36455779Sxy150489 
36465779Sxy150489 	hw = &igb->hw;
36475779Sxy150489 
36485779Sxy150489 	/* Set link mode to SerDes (11b) in the Extended Control register */
36495779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
36505779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
36515779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
36525779Sxy150489 
36535779Sxy150489 	/* Configure the SerDes to loopback */
36545779Sxy150489 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
36555779Sxy150489 
36565779Sxy150489 	/* Set Device Control register */
36575779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
36585779Sxy150489 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
36595779Sxy150489 	    E1000_CTRL_SLU);		/* Force link up */
36605779Sxy150489 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
36615779Sxy150489 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
36625779Sxy150489 	    E1000_CTRL_LRST);		/* Clear link reset */
36635779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
36645779Sxy150489 
36655779Sxy150489 	/* Set PCS Link Control register */
36665779Sxy150489 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
36675779Sxy150489 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
36685779Sxy150489 	    E1000_PCS_LCTL_FSD |
36695779Sxy150489 	    E1000_PCS_LCTL_FDV_FULL |
36705779Sxy150489 	    E1000_PCS_LCTL_FLV_LINK_UP);
36715779Sxy150489 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
36725779Sxy150489 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
36735779Sxy150489 
36745779Sxy150489 	/* Set the Copper/Fiber Switch Control - CONNSW register */
36755779Sxy150489 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
36765779Sxy150489 	connsw &= ~E1000_CONNSW_ENRGSRC;
36775779Sxy150489 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
36785779Sxy150489 }
36795779Sxy150489 
36805779Sxy150489 #pragma inline(igb_intr_rx_work)
36815779Sxy150489 /*
36825779Sxy150489  * igb_intr_rx_work - rx processing of ISR
36835779Sxy150489  */
36845779Sxy150489 static void
36855779Sxy150489 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
36865779Sxy150489 {
36875779Sxy150489 	mblk_t *mp;
36885779Sxy150489 
36895779Sxy150489 	mutex_enter(&rx_ring->rx_lock);
36908275SEric Cheng 	mp = igb_rx(rx_ring, IGB_NO_POLL);
36915779Sxy150489 	mutex_exit(&rx_ring->rx_lock);
36925779Sxy150489 
36935779Sxy150489 	if (mp != NULL)
36948275SEric Cheng 		mac_rx_ring(rx_ring->igb->mac_hdl, rx_ring->ring_handle, mp,
36958275SEric Cheng 		    rx_ring->ring_gen_num);
36965779Sxy150489 }
36975779Sxy150489 
36985779Sxy150489 #pragma inline(igb_intr_tx_work)
36995779Sxy150489 /*
37005779Sxy150489  * igb_intr_tx_work - tx processing of ISR
37015779Sxy150489  */
37025779Sxy150489 static void
37035779Sxy150489 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
37045779Sxy150489 {
37055779Sxy150489 	/* Recycle the tx descriptors */
37065779Sxy150489 	tx_ring->tx_recycle(tx_ring);
37075779Sxy150489 
37085779Sxy150489 	/* Schedule the re-transmit */
37095779Sxy150489 	if (tx_ring->reschedule &&
37105779Sxy150489 	    (tx_ring->tbd_free >= tx_ring->resched_thresh)) {
37115779Sxy150489 		tx_ring->reschedule = B_FALSE;
37128275SEric Cheng 		mac_tx_ring_update(tx_ring->igb->mac_hdl, tx_ring->ring_handle);
37135779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
37145779Sxy150489 	}
37155779Sxy150489 }
37165779Sxy150489 
37178275SEric Cheng #pragma inline(igb_intr_link_work)
37185779Sxy150489 /*
37198275SEric Cheng  * igb_intr_link_work - link-status-change processing of ISR
37205779Sxy150489  */
37215779Sxy150489 static void
37228275SEric Cheng igb_intr_link_work(igb_t *igb)
37235779Sxy150489 {
37245779Sxy150489 	boolean_t link_changed;
37255779Sxy150489 
37265779Sxy150489 	igb_stop_watchdog_timer(igb);
37275779Sxy150489 
37285779Sxy150489 	mutex_enter(&igb->gen_lock);
37295779Sxy150489 
37305779Sxy150489 	/*
37315779Sxy150489 	 * Because we got a link-status-change interrupt, force
37325779Sxy150489 	 * e1000_check_for_link() to look at phy
37335779Sxy150489 	 */
37345779Sxy150489 	igb->hw.mac.get_link_status = B_TRUE;
37355779Sxy150489 
37365779Sxy150489 	/* igb_link_check takes care of link status change */
37375779Sxy150489 	link_changed = igb_link_check(igb);
37385779Sxy150489 
37395779Sxy150489 	/* Get new phy state */
37405779Sxy150489 	igb_get_phy_state(igb);
37415779Sxy150489 
37425779Sxy150489 	mutex_exit(&igb->gen_lock);
37435779Sxy150489 
37445779Sxy150489 	if (link_changed)
37455779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
37465779Sxy150489 
37475779Sxy150489 	igb_start_watchdog_timer(igb);
37485779Sxy150489 }
37495779Sxy150489 
37505779Sxy150489 /*
37515779Sxy150489  * igb_intr_legacy - Interrupt handler for legacy interrupts
37525779Sxy150489  */
37535779Sxy150489 static uint_t
37545779Sxy150489 igb_intr_legacy(void *arg1, void *arg2)
37555779Sxy150489 {
37565779Sxy150489 	igb_t *igb = (igb_t *)arg1;
37575779Sxy150489 	igb_tx_ring_t *tx_ring;
37585779Sxy150489 	uint32_t icr;
37595779Sxy150489 	mblk_t *mp;
37605779Sxy150489 	boolean_t tx_reschedule;
37615779Sxy150489 	boolean_t link_changed;
37625779Sxy150489 	uint_t result;
37635779Sxy150489 
37645779Sxy150489 	_NOTE(ARGUNUSED(arg2));
37655779Sxy150489 
37665779Sxy150489 	mutex_enter(&igb->gen_lock);
37675779Sxy150489 
37685779Sxy150489 	if (igb->igb_state & IGB_SUSPENDED) {
37695779Sxy150489 		mutex_exit(&igb->gen_lock);
37705779Sxy150489 		return (DDI_INTR_UNCLAIMED);
37715779Sxy150489 	}
37725779Sxy150489 
37735779Sxy150489 	mp = NULL;
37745779Sxy150489 	tx_reschedule = B_FALSE;
37755779Sxy150489 	link_changed = B_FALSE;
37765779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
37775779Sxy150489 
37785779Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
37795779Sxy150489 		/*
37805779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
37815779Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
37825779Sxy150489 		 * look for work to do.
37835779Sxy150489 		 */
37845779Sxy150489 		ASSERT(igb->num_rx_rings == 1);
37855779Sxy150489 		ASSERT(igb->num_tx_rings == 1);
37865779Sxy150489 
37878571SChenlu.Chen@Sun.COM 		/* Make sure all interrupt causes cleared */
37888571SChenlu.Chen@Sun.COM 		(void) E1000_READ_REG(&igb->hw, E1000_EICR);
37898571SChenlu.Chen@Sun.COM 
37905779Sxy150489 		if (icr & E1000_ICR_RXT0) {
37918275SEric Cheng 			mp = igb_rx(&igb->rx_rings[0], IGB_NO_POLL);
37925779Sxy150489 		}
37935779Sxy150489 
37945779Sxy150489 		if (icr & E1000_ICR_TXDW) {
37955779Sxy150489 			tx_ring = &igb->tx_rings[0];
37965779Sxy150489 
37975779Sxy150489 			/* Recycle the tx descriptors */
37985779Sxy150489 			tx_ring->tx_recycle(tx_ring);
37995779Sxy150489 
38005779Sxy150489 			/* Schedule the re-transmit */
38015779Sxy150489 			tx_reschedule = (tx_ring->reschedule &&
38025779Sxy150489 			    (tx_ring->tbd_free >= tx_ring->resched_thresh));
38035779Sxy150489 		}
38045779Sxy150489 
38055779Sxy150489 		if (icr & E1000_ICR_LSC) {
38065779Sxy150489 			/*
38075779Sxy150489 			 * Because we got a link-status-change interrupt, force
38085779Sxy150489 			 * e1000_check_for_link() to look at phy
38095779Sxy150489 			 */
38105779Sxy150489 			igb->hw.mac.get_link_status = B_TRUE;
38115779Sxy150489 
38125779Sxy150489 			/* igb_link_check takes care of link status change */
38135779Sxy150489 			link_changed = igb_link_check(igb);
38145779Sxy150489 
38155779Sxy150489 			/* Get new phy state */
38165779Sxy150489 			igb_get_phy_state(igb);
38175779Sxy150489 		}
38185779Sxy150489 
38195779Sxy150489 		result = DDI_INTR_CLAIMED;
38205779Sxy150489 	} else {
38215779Sxy150489 		/*
38225779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
38235779Sxy150489 		 * Don't claim this interrupt.
38245779Sxy150489 		 */
38255779Sxy150489 		result = DDI_INTR_UNCLAIMED;
38265779Sxy150489 	}
38275779Sxy150489 
38285779Sxy150489 	mutex_exit(&igb->gen_lock);
38295779Sxy150489 
38305779Sxy150489 	/*
38315779Sxy150489 	 * Do the following work outside of the gen_lock
38325779Sxy150489 	 */
38335779Sxy150489 	if (mp != NULL)
38345779Sxy150489 		mac_rx(igb->mac_hdl, NULL, mp);
38355779Sxy150489 
38365779Sxy150489 	if (tx_reschedule)  {
38375779Sxy150489 		tx_ring->reschedule = B_FALSE;
38388275SEric Cheng 		mac_tx_ring_update(igb->mac_hdl, tx_ring->ring_handle);
38395779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
38405779Sxy150489 	}
38415779Sxy150489 
38425779Sxy150489 	if (link_changed)
38435779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
38445779Sxy150489 
38455779Sxy150489 	return (result);
38465779Sxy150489 }
38475779Sxy150489 
38485779Sxy150489 /*
38495779Sxy150489  * igb_intr_msi - Interrupt handler for MSI
38505779Sxy150489  */
38515779Sxy150489 static uint_t
38525779Sxy150489 igb_intr_msi(void *arg1, void *arg2)
38535779Sxy150489 {
38545779Sxy150489 	igb_t *igb = (igb_t *)arg1;
38555779Sxy150489 	uint32_t icr;
38565779Sxy150489 
38575779Sxy150489 	_NOTE(ARGUNUSED(arg2));
38585779Sxy150489 
38595779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
38605779Sxy150489 
38618571SChenlu.Chen@Sun.COM 	/* Make sure all interrupt causes cleared */
38628571SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(&igb->hw, E1000_EICR);
38638571SChenlu.Chen@Sun.COM 
38645779Sxy150489 	/*
38655779Sxy150489 	 * For MSI interrupt, we have only one vector,
38665779Sxy150489 	 * so we have only one rx ring and one tx ring enabled.
38675779Sxy150489 	 */
38685779Sxy150489 	ASSERT(igb->num_rx_rings == 1);
38695779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
38705779Sxy150489 
38715779Sxy150489 	if (icr & E1000_ICR_RXT0) {
38725779Sxy150489 		igb_intr_rx_work(&igb->rx_rings[0]);
38735779Sxy150489 	}
38745779Sxy150489 
38755779Sxy150489 	if (icr & E1000_ICR_TXDW) {
38765779Sxy150489 		igb_intr_tx_work(&igb->tx_rings[0]);
38775779Sxy150489 	}
38785779Sxy150489 
38795779Sxy150489 	if (icr & E1000_ICR_LSC) {
38808275SEric Cheng 		igb_intr_link_work(igb);
38815779Sxy150489 	}
38825779Sxy150489 
38835779Sxy150489 	return (DDI_INTR_CLAIMED);
38845779Sxy150489 }
38855779Sxy150489 
38865779Sxy150489 /*
38875779Sxy150489  * igb_intr_rx - Interrupt handler for rx
38885779Sxy150489  */
38895779Sxy150489 static uint_t
38905779Sxy150489 igb_intr_rx(void *arg1, void *arg2)
38915779Sxy150489 {
38925779Sxy150489 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
38935779Sxy150489 
38945779Sxy150489 	_NOTE(ARGUNUSED(arg2));
38955779Sxy150489 
38965779Sxy150489 	/*
38975779Sxy150489 	 * Only used via MSI-X vector so don't check cause bits
38985779Sxy150489 	 * and only clean the given ring.
38995779Sxy150489 	 */
39005779Sxy150489 	igb_intr_rx_work(rx_ring);
39015779Sxy150489 
39025779Sxy150489 	return (DDI_INTR_CLAIMED);
39035779Sxy150489 }
39045779Sxy150489 
39055779Sxy150489 /*
39068275SEric Cheng  * igb_intr_tx - Interrupt handler for tx
39078275SEric Cheng  */
39088275SEric Cheng static uint_t
39098275SEric Cheng igb_intr_tx(void *arg1, void *arg2)
39108275SEric Cheng {
39118275SEric Cheng 	igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg1;
39128275SEric Cheng 
39138275SEric Cheng 	_NOTE(ARGUNUSED(arg2));
39148275SEric Cheng 
39158275SEric Cheng 	/*
39168275SEric Cheng 	 * Only used via MSI-X vector so don't check cause bits
39178275SEric Cheng 	 * and only clean the given ring.
39188275SEric Cheng 	 */
39198275SEric Cheng 	igb_intr_tx_work(tx_ring);
39208275SEric Cheng 
39218275SEric Cheng 	return (DDI_INTR_CLAIMED);
39228275SEric Cheng }
39238275SEric Cheng 
39248275SEric Cheng /*
39255779Sxy150489  * igb_intr_tx_other - Interrupt handler for both tx and other
39265779Sxy150489  *
39275779Sxy150489  */
39285779Sxy150489 static uint_t
39295779Sxy150489 igb_intr_tx_other(void *arg1, void *arg2)
39305779Sxy150489 {
39315779Sxy150489 	igb_t *igb = (igb_t *)arg1;
39325779Sxy150489 	uint32_t icr;
39335779Sxy150489 
39345779Sxy150489 	_NOTE(ARGUNUSED(arg2));
39355779Sxy150489 
39365779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
39375779Sxy150489 
39385779Sxy150489 	/*
39398275SEric Cheng 	 * Look for tx reclaiming work first. Remember, in the
39408275SEric Cheng 	 * case of only interrupt sharing, only one tx ring is
39418275SEric Cheng 	 * used
39425779Sxy150489 	 */
39435779Sxy150489 	igb_intr_tx_work(&igb->tx_rings[0]);
39445779Sxy150489 
39455779Sxy150489 	/*
39468955SChenlu.Chen@Sun.COM 	 * Check for "other" causes.
39475779Sxy150489 	 */
39485779Sxy150489 	if (icr & E1000_ICR_LSC) {
39498275SEric Cheng 		igb_intr_link_work(igb);
39505779Sxy150489 	}
39515779Sxy150489 
39528955SChenlu.Chen@Sun.COM 	/*
39538955SChenlu.Chen@Sun.COM 	 * The DOUTSYNC bit indicates a tx packet dropped because
39548955SChenlu.Chen@Sun.COM 	 * DMA engine gets "out of sync". There isn't a real fix
39558955SChenlu.Chen@Sun.COM 	 * for this. The Intel recommendation is to count the number
39568955SChenlu.Chen@Sun.COM 	 * of occurrences so user can detect when it is happening.
39578955SChenlu.Chen@Sun.COM 	 * The issue is non-fatal and there's no recovery action
39588955SChenlu.Chen@Sun.COM 	 * available.
39598955SChenlu.Chen@Sun.COM 	 */
39608955SChenlu.Chen@Sun.COM 	if (icr & E1000_ICR_DOUTSYNC) {
39618955SChenlu.Chen@Sun.COM 		IGB_STAT(igb->dout_sync);
39628955SChenlu.Chen@Sun.COM 	}
39638955SChenlu.Chen@Sun.COM 
39645779Sxy150489 	return (DDI_INTR_CLAIMED);
39655779Sxy150489 }
39665779Sxy150489 
39675779Sxy150489 /*
39685779Sxy150489  * igb_alloc_intrs - Allocate interrupts for the driver
39695779Sxy150489  *
39705779Sxy150489  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
39715779Sxy150489  * if not successful, try Legacy.
39725779Sxy150489  * igb->intr_force can be used to force sequence to start with
39735779Sxy150489  * any of the 3 types.
39745779Sxy150489  * If MSI-X is not used, number of tx/rx rings is forced to 1.
39755779Sxy150489  */
39765779Sxy150489 static int
39775779Sxy150489 igb_alloc_intrs(igb_t *igb)
39785779Sxy150489 {
39795779Sxy150489 	dev_info_t *devinfo;
39805779Sxy150489 	int intr_types;
39815779Sxy150489 	int rc;
39825779Sxy150489 
39835779Sxy150489 	devinfo = igb->dip;
39845779Sxy150489 
39855779Sxy150489 	/* Get supported interrupt types */
39865779Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
39875779Sxy150489 
39885779Sxy150489 	if (rc != DDI_SUCCESS) {
39895779Sxy150489 		igb_log(igb,
39905779Sxy150489 		    "Get supported interrupt types failed: %d", rc);
39915779Sxy150489 		return (IGB_FAILURE);
39925779Sxy150489 	}
39935779Sxy150489 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
39945779Sxy150489 
39955779Sxy150489 	igb->intr_type = 0;
39965779Sxy150489 
39975779Sxy150489 	/* Install MSI-X interrupts */
39985779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
39995779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSIX)) {
40007072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSIX);
40015779Sxy150489 
40025779Sxy150489 		if (rc == IGB_SUCCESS)
40035779Sxy150489 			return (IGB_SUCCESS);
40045779Sxy150489 
40055779Sxy150489 		igb_log(igb,
40065779Sxy150489 		    "Allocate MSI-X failed, trying MSI interrupts...");
40075779Sxy150489 	}
40085779Sxy150489 
40095779Sxy150489 	/* MSI-X not used, force rings to 1 */
40105779Sxy150489 	igb->num_rx_rings = 1;
40115779Sxy150489 	igb->num_tx_rings = 1;
40125779Sxy150489 	igb_log(igb,
40135779Sxy150489 	    "MSI-X not used, force rx and tx queue number to 1");
40145779Sxy150489 
40155779Sxy150489 	/* Install MSI interrupts */
40165779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
40175779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSI)) {
40187072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSI);
40195779Sxy150489 
40205779Sxy150489 		if (rc == IGB_SUCCESS)
40215779Sxy150489 			return (IGB_SUCCESS);
40225779Sxy150489 
40235779Sxy150489 		igb_log(igb,
40245779Sxy150489 		    "Allocate MSI failed, trying Legacy interrupts...");
40255779Sxy150489 	}
40265779Sxy150489 
40275779Sxy150489 	/* Install legacy interrupts */
40285779Sxy150489 	if (intr_types & DDI_INTR_TYPE_FIXED) {
40297072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_FIXED);
40305779Sxy150489 
40315779Sxy150489 		if (rc == IGB_SUCCESS)
40325779Sxy150489 			return (IGB_SUCCESS);
40335779Sxy150489 
40345779Sxy150489 		igb_log(igb,
40355779Sxy150489 		    "Allocate Legacy interrupts failed");
40365779Sxy150489 	}
40375779Sxy150489 
40385779Sxy150489 	/* If none of the 3 types succeeded, return failure */
40395779Sxy150489 	return (IGB_FAILURE);
40405779Sxy150489 }
40415779Sxy150489 
40425779Sxy150489 /*
40437072Sxy150489  * igb_alloc_intr_handles - Allocate interrupt handles.
40445779Sxy150489  *
40457072Sxy150489  * For legacy and MSI, only 1 handle is needed.  For MSI-X,
40467072Sxy150489  * if fewer than 2 handles are available, return failure.
40475779Sxy150489  * Upon success, this sets the number of Rx rings to a number that
40487072Sxy150489  * matches the handles available for Rx interrupts.
40495779Sxy150489  */
40505779Sxy150489 static int
40517072Sxy150489 igb_alloc_intr_handles(igb_t *igb, int intr_type)
40525779Sxy150489 {
40535779Sxy150489 	dev_info_t *devinfo;
40548275SEric Cheng 	int orig, request, count, avail, actual;
40558275SEric Cheng 	int diff, minimum;
40565779Sxy150489 	int rc;
40575779Sxy150489 
40585779Sxy150489 	devinfo = igb->dip;
40595779Sxy150489 
40607072Sxy150489 	switch (intr_type) {
40617072Sxy150489 	case DDI_INTR_TYPE_FIXED:
40627072Sxy150489 		request = 1;	/* Request 1 legacy interrupt handle */
40637072Sxy150489 		minimum = 1;
40647072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: legacy");
40657072Sxy150489 		break;
40667072Sxy150489 
40677072Sxy150489 	case DDI_INTR_TYPE_MSI:
40687072Sxy150489 		request = 1;	/* Request 1 MSI interrupt handle */
40697072Sxy150489 		minimum = 1;
40707072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI");
40717072Sxy150489 		break;
40727072Sxy150489 
40737072Sxy150489 	case DDI_INTR_TYPE_MSIX:
40747072Sxy150489 		/*
40758275SEric Cheng 		 * Number of vectors for the adapter is
40768275SEric Cheng 		 * # rx rings + # tx rings
40778275SEric Cheng 		 * One of tx vectors is for tx & other
40787072Sxy150489 		 */
40798275SEric Cheng 		request = igb->num_rx_rings + igb->num_tx_rings;
40808275SEric Cheng 		orig = request;
40817072Sxy150489 		minimum = 2;
40827072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI-X");
40837072Sxy150489 		break;
40847072Sxy150489 
40857072Sxy150489 	default:
40865779Sxy150489 		igb_log(igb,
40877072Sxy150489 		    "invalid call to igb_alloc_intr_handles(): %d\n",
40887072Sxy150489 		    intr_type);
40895779Sxy150489 		return (IGB_FAILURE);
40905779Sxy150489 	}
40917072Sxy150489 	IGB_DEBUGLOG_2(igb, "interrupt handles requested: %d  minimum: %d",
40927072Sxy150489 	    request, minimum);
40937072Sxy150489 
40947072Sxy150489 	/*
40957072Sxy150489 	 * Get number of supported interrupts
40967072Sxy150489 	 */
40977072Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
40987072Sxy150489 	if ((rc != DDI_SUCCESS) || (count < minimum)) {
40995779Sxy150489 		igb_log(igb,
41007072Sxy150489 		    "Get supported interrupt number failed. "
41017072Sxy150489 		    "Return: %d, count: %d", rc, count);
41027072Sxy150489 		return (IGB_FAILURE);
41037072Sxy150489 	}
41047072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts supported: %d", count);
41057072Sxy150489 
41067072Sxy150489 	/*
41077072Sxy150489 	 * Get number of available interrupts
41087072Sxy150489 	 */
41097072Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
41107072Sxy150489 	if ((rc != DDI_SUCCESS) || (avail < minimum)) {
41117072Sxy150489 		igb_log(igb,
41127072Sxy150489 		    "Get available interrupt number failed. "
41135779Sxy150489 		    "Return: %d, available: %d", rc, avail);
41145779Sxy150489 		return (IGB_FAILURE);
41155779Sxy150489 	}
41167072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts available: %d", avail);
41175779Sxy150489 
41185779Sxy150489 	if (avail < request) {
41197072Sxy150489 		igb_log(igb, "Request %d handles, %d available",
41205779Sxy150489 		    request, avail);
41215779Sxy150489 		request = avail;
41225779Sxy150489 	}
41235779Sxy150489 
41245779Sxy150489 	actual = 0;
41255779Sxy150489 	igb->intr_cnt = 0;
41265779Sxy150489 
41277072Sxy150489 	/*
41287072Sxy150489 	 * Allocate an array of interrupt handles
41297072Sxy150489 	 */
41305779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
41315779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
41325779Sxy150489 
41337072Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, intr_type, 0,
41345779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
41355779Sxy150489 	if (rc != DDI_SUCCESS) {
41367072Sxy150489 		igb_log(igb, "Allocate interrupts failed. "
41375779Sxy150489 		    "return: %d, request: %d, actual: %d",
41385779Sxy150489 		    rc, request, actual);
41397072Sxy150489 		goto alloc_handle_fail;
41405779Sxy150489 	}
41417072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts actually allocated: %d", actual);
41425779Sxy150489 
41435779Sxy150489 	igb->intr_cnt = actual;
41445779Sxy150489 
41457072Sxy150489 	if (actual < minimum) {
41467072Sxy150489 		igb_log(igb, "Insufficient interrupt handles allocated: %d",
41477072Sxy150489 		    actual);
41487072Sxy150489 		goto alloc_handle_fail;
41497072Sxy150489 	}
41507072Sxy150489 
41515779Sxy150489 	/*
41528275SEric Cheng 	 * For MSI-X, actual might force us to reduce number of tx & rx rings
41535779Sxy150489 	 */
41548275SEric Cheng 	if ((intr_type == DDI_INTR_TYPE_MSIX) && (orig > actual)) {
41558275SEric Cheng 		diff = orig - actual;
41568275SEric Cheng 		if (diff < igb->num_tx_rings) {
41578275SEric Cheng 			igb_log(igb,
41588275SEric Cheng 			    "MSI-X vectors force Tx queue number to %d",
41598275SEric Cheng 			    igb->num_tx_rings - diff);
41608275SEric Cheng 			igb->num_tx_rings -= diff;
41618275SEric Cheng 		} else {
41628275SEric Cheng 			igb_log(igb,
41638275SEric Cheng 			    "MSI-X vectors force Tx queue number to 1");
41648275SEric Cheng 			igb->num_tx_rings = 1;
41658275SEric Cheng 
41667072Sxy150489 			igb_log(igb,
41677072Sxy150489 			    "MSI-X vectors force Rx queue number to %d",
41688275SEric Cheng 			    actual - 1);
41698275SEric Cheng 			igb->num_rx_rings = actual - 1;
41707072Sxy150489 		}
41715779Sxy150489 	}
41725779Sxy150489 
41737072Sxy150489 	/*
41747072Sxy150489 	 * Get priority for first vector, assume remaining are all the same
41757072Sxy150489 	 */
41765779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
41775779Sxy150489 	if (rc != DDI_SUCCESS) {
41785779Sxy150489 		igb_log(igb,
41795779Sxy150489 		    "Get interrupt priority failed: %d", rc);
41807072Sxy150489 		goto alloc_handle_fail;
41815779Sxy150489 	}
41825779Sxy150489 
41835779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
41845779Sxy150489 	if (rc != DDI_SUCCESS) {
41855779Sxy150489 		igb_log(igb,
41865779Sxy150489 		    "Get interrupt cap failed: %d", rc);
41877072Sxy150489 		goto alloc_handle_fail;
41885779Sxy150489 	}
41895779Sxy150489 
41907072Sxy150489 	igb->intr_type = intr_type;
41915779Sxy150489 
41925779Sxy150489 	return (IGB_SUCCESS);
41935779Sxy150489 
41947072Sxy150489 alloc_handle_fail:
41955779Sxy150489 	igb_rem_intrs(igb);
41965779Sxy150489 
41975779Sxy150489 	return (IGB_FAILURE);
41985779Sxy150489 }
41995779Sxy150489 
42005779Sxy150489 /*
42015779Sxy150489  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
42025779Sxy150489  *
42035779Sxy150489  * Before adding the interrupt handlers, the interrupt vectors have
42045779Sxy150489  * been allocated, and the rx/tx rings have also been allocated.
42055779Sxy150489  */
42065779Sxy150489 static int
42075779Sxy150489 igb_add_intr_handlers(igb_t *igb)
42085779Sxy150489 {
42095779Sxy150489 	igb_rx_ring_t *rx_ring;
42108275SEric Cheng 	igb_tx_ring_t *tx_ring;
42115779Sxy150489 	int vector;
42125779Sxy150489 	int rc;
42135779Sxy150489 	int i;
42145779Sxy150489 
42155779Sxy150489 	vector = 0;
42165779Sxy150489 
42175779Sxy150489 	switch (igb->intr_type) {
42185779Sxy150489 	case DDI_INTR_TYPE_MSIX:
42195779Sxy150489 		/* Add interrupt handler for tx + other */
42208275SEric Cheng 		tx_ring = &igb->tx_rings[0];
42215779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
42225779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_tx_other,
42235779Sxy150489 		    (void *)igb, NULL);
42248275SEric Cheng 
42255779Sxy150489 		if (rc != DDI_SUCCESS) {
42265779Sxy150489 			igb_log(igb,
42275779Sxy150489 			    "Add tx/other interrupt handler failed: %d", rc);
42285779Sxy150489 			return (IGB_FAILURE);
42295779Sxy150489 		}
42308275SEric Cheng 		tx_ring->intr_vector = vector;
42315779Sxy150489 		vector++;
42325779Sxy150489 
42335779Sxy150489 		/* Add interrupt handler for each rx ring */
42345779Sxy150489 		for (i = 0; i < igb->num_rx_rings; i++) {
42355779Sxy150489 			rx_ring = &igb->rx_rings[i];
42365779Sxy150489 
42375779Sxy150489 			rc = ddi_intr_add_handler(igb->htable[vector],
42385779Sxy150489 			    (ddi_intr_handler_t *)igb_intr_rx,
42395779Sxy150489 			    (void *)rx_ring, NULL);
42405779Sxy150489 
42415779Sxy150489 			if (rc != DDI_SUCCESS) {
42425779Sxy150489 				igb_log(igb,
42435779Sxy150489 				    "Add rx interrupt handler failed. "
42445779Sxy150489 				    "return: %d, rx ring: %d", rc, i);
42455779Sxy150489 				for (vector--; vector >= 0; vector--) {
42465779Sxy150489 					(void) ddi_intr_remove_handler(
42475779Sxy150489 					    igb->htable[vector]);
42485779Sxy150489 				}
42495779Sxy150489 				return (IGB_FAILURE);
42505779Sxy150489 			}
42515779Sxy150489 
42525779Sxy150489 			rx_ring->intr_vector = vector;
42535779Sxy150489 
42545779Sxy150489 			vector++;
42555779Sxy150489 		}
42568275SEric Cheng 
42578275SEric Cheng 		/* Add interrupt handler for each tx ring from 2nd ring */
42588275SEric Cheng 		for (i = 1; i < igb->num_tx_rings; i++) {
42598275SEric Cheng 			tx_ring = &igb->tx_rings[i];
42608275SEric Cheng 
42618275SEric Cheng 			rc = ddi_intr_add_handler(igb->htable[vector],
42628275SEric Cheng 			    (ddi_intr_handler_t *)igb_intr_tx,
42638275SEric Cheng 			    (void *)tx_ring, NULL);
42648275SEric Cheng 
42658275SEric Cheng 			if (rc != DDI_SUCCESS) {
42668275SEric Cheng 				igb_log(igb,
42678275SEric Cheng 				    "Add tx interrupt handler failed. "
42688275SEric Cheng 				    "return: %d, tx ring: %d", rc, i);
42698275SEric Cheng 				for (vector--; vector >= 0; vector--) {
42708275SEric Cheng 					(void) ddi_intr_remove_handler(
42718275SEric Cheng 					    igb->htable[vector]);
42728275SEric Cheng 				}
42738275SEric Cheng 				return (IGB_FAILURE);
42748275SEric Cheng 			}
42758275SEric Cheng 
42768275SEric Cheng 			tx_ring->intr_vector = vector;
42778275SEric Cheng 
42788275SEric Cheng 			vector++;
42798275SEric Cheng 		}
42808275SEric Cheng 
42815779Sxy150489 		break;
42825779Sxy150489 
42835779Sxy150489 	case DDI_INTR_TYPE_MSI:
42845779Sxy150489 		/* Add interrupt handlers for the only vector */
42855779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
42865779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_msi,
42875779Sxy150489 		    (void *)igb, NULL);
42885779Sxy150489 
42895779Sxy150489 		if (rc != DDI_SUCCESS) {
42905779Sxy150489 			igb_log(igb,
42915779Sxy150489 			    "Add MSI interrupt handler failed: %d", rc);
42925779Sxy150489 			return (IGB_FAILURE);
42935779Sxy150489 		}
42945779Sxy150489 
42955779Sxy150489 		rx_ring = &igb->rx_rings[0];
42965779Sxy150489 		rx_ring->intr_vector = vector;
42975779Sxy150489 
42985779Sxy150489 		vector++;
42995779Sxy150489 		break;
43005779Sxy150489 
43015779Sxy150489 	case DDI_INTR_TYPE_FIXED:
43025779Sxy150489 		/* Add interrupt handlers for the only vector */
43035779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
43045779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_legacy,
43055779Sxy150489 		    (void *)igb, NULL);
43065779Sxy150489 
43075779Sxy150489 		if (rc != DDI_SUCCESS) {
43085779Sxy150489 			igb_log(igb,
43095779Sxy150489 			    "Add legacy interrupt handler failed: %d", rc);
43105779Sxy150489 			return (IGB_FAILURE);
43115779Sxy150489 		}
43125779Sxy150489 
43135779Sxy150489 		rx_ring = &igb->rx_rings[0];
43145779Sxy150489 		rx_ring->intr_vector = vector;
43155779Sxy150489 
43165779Sxy150489 		vector++;
43175779Sxy150489 		break;
43185779Sxy150489 
43195779Sxy150489 	default:
43205779Sxy150489 		return (IGB_FAILURE);
43215779Sxy150489 	}
43225779Sxy150489 
43235779Sxy150489 	ASSERT(vector == igb->intr_cnt);
43245779Sxy150489 
43255779Sxy150489 	return (IGB_SUCCESS);
43265779Sxy150489 }
43275779Sxy150489 
43285779Sxy150489 /*
43298571SChenlu.Chen@Sun.COM  * igb_setup_msix_82575 - setup 82575 adapter to use MSI-X interrupts
43305779Sxy150489  *
43315779Sxy150489  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
43325779Sxy150489  */
43335779Sxy150489 static void
43348571SChenlu.Chen@Sun.COM igb_setup_msix_82575(igb_t *igb)
43355779Sxy150489 {
43365779Sxy150489 	uint32_t eims = 0;
43375779Sxy150489 	int i, vector;
43385779Sxy150489 	struct e1000_hw *hw = &igb->hw;
43395779Sxy150489 
43405779Sxy150489 	/*
43418571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
43428571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
43435779Sxy150489 	 */
43445779Sxy150489 	vector = 0;
43458275SEric Cheng 
43465779Sxy150489 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
43475779Sxy150489 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
43485779Sxy150489 	vector++;
43498275SEric Cheng 
43505779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
43515779Sxy150489 		/*
43525779Sxy150489 		 * Set vector for each rx ring
43535779Sxy150489 		 */
43545779Sxy150489 		eims = (E1000_EICR_RX_QUEUE0 << i);
43555779Sxy150489 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
43565779Sxy150489 
43575779Sxy150489 		/*
43588571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
43598571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
43605779Sxy150489 		 */
43615779Sxy150489 		igb->eims_mask |= eims;
43625779Sxy150489 
43635779Sxy150489 		vector++;
43645779Sxy150489 	}
43655779Sxy150489 
43668275SEric Cheng 	for (i = 1; i < igb->num_tx_rings; i++) {
43678275SEric Cheng 		/*
43688275SEric Cheng 		 * Set vector for each tx ring from 2nd tx ring
43698275SEric Cheng 		 */
43708275SEric Cheng 		eims = (E1000_EICR_TX_QUEUE0 << i);
43718275SEric Cheng 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
43728275SEric Cheng 
43738275SEric Cheng 		/*
43748571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
43758571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
43768275SEric Cheng 		 */
43778275SEric Cheng 		igb->eims_mask |= eims;
43788275SEric Cheng 
43798275SEric Cheng 		vector++;
43808275SEric Cheng 	}
43818275SEric Cheng 
43825779Sxy150489 	ASSERT(vector == igb->intr_cnt);
43835779Sxy150489 
43845779Sxy150489 	/*
43855779Sxy150489 	 * Disable IAM for ICR interrupt bits
43865779Sxy150489 	 */
43875779Sxy150489 	E1000_WRITE_REG(hw, E1000_IAM, 0);
43885779Sxy150489 	E1000_WRITE_FLUSH(hw);
43895779Sxy150489 }
43905779Sxy150489 
43915779Sxy150489 /*
43928571SChenlu.Chen@Sun.COM  * igb_setup_msix_82576 - setup 82576 adapter to use MSI-X interrupts
43938571SChenlu.Chen@Sun.COM  *
43948571SChenlu.Chen@Sun.COM  * 82576 uses a table based method for assigning vectors.  Each queue has a
43958571SChenlu.Chen@Sun.COM  * single entry in the table to which we write a vector number along with a
43968571SChenlu.Chen@Sun.COM  * "valid" bit.  The entry is a single byte in a 4-byte register.  Vectors
43978571SChenlu.Chen@Sun.COM  * take a different position in the 4-byte register depending on whether
43988571SChenlu.Chen@Sun.COM  * they are numbered above or below 8.
43998571SChenlu.Chen@Sun.COM  */
44008571SChenlu.Chen@Sun.COM static void
44018571SChenlu.Chen@Sun.COM igb_setup_msix_82576(igb_t *igb)
44028571SChenlu.Chen@Sun.COM {
44038571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
44048571SChenlu.Chen@Sun.COM 	uint32_t ivar, index, vector;
44058571SChenlu.Chen@Sun.COM 	int i;
44068571SChenlu.Chen@Sun.COM 
44078571SChenlu.Chen@Sun.COM 	/* must enable msi-x capability before IVAR settings */
44088571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_GPIE,
44098571SChenlu.Chen@Sun.COM 	    (E1000_GPIE_MSIX_MODE | E1000_GPIE_PBA | E1000_GPIE_NSICR));
44108571SChenlu.Chen@Sun.COM 
44118571SChenlu.Chen@Sun.COM 	/*
44128571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
44138571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
44148571SChenlu.Chen@Sun.COM 	 * This is also interdependent with installation of interrupt service
44158571SChenlu.Chen@Sun.COM 	 * routines in igb_add_intr_handlers().
44168571SChenlu.Chen@Sun.COM 	 */
44178571SChenlu.Chen@Sun.COM 
44188571SChenlu.Chen@Sun.COM 	/* assign "other" causes to vector 0 */
44198571SChenlu.Chen@Sun.COM 	vector = 0;
44208571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
44218571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
44228571SChenlu.Chen@Sun.COM 
44238571SChenlu.Chen@Sun.COM 	/* assign tx ring 0 to vector 0 */
44248571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
44258571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
44268571SChenlu.Chen@Sun.COM 
44278571SChenlu.Chen@Sun.COM 	/* prepare to enable tx & other interrupt causes */
44288571SChenlu.Chen@Sun.COM 	igb->eims_mask = (1 << vector);
44298571SChenlu.Chen@Sun.COM 
44308571SChenlu.Chen@Sun.COM 	vector ++;
44318571SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
44328571SChenlu.Chen@Sun.COM 		/*
44338571SChenlu.Chen@Sun.COM 		 * Set vector for each rx ring
44348571SChenlu.Chen@Sun.COM 		 */
44358571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
44368571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
44378571SChenlu.Chen@Sun.COM 
44388571SChenlu.Chen@Sun.COM 		if (i < 8) {
44398571SChenlu.Chen@Sun.COM 			/* vector goes into low byte of register */
44408571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFFFF00;
44418571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID);
44428571SChenlu.Chen@Sun.COM 		} else {
44438571SChenlu.Chen@Sun.COM 			/* vector goes into third byte of register */
44448571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFF00FFFF;
44458571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
44468571SChenlu.Chen@Sun.COM 		}
44478571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
44488571SChenlu.Chen@Sun.COM 
44498571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
44508571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
44518571SChenlu.Chen@Sun.COM 
44528571SChenlu.Chen@Sun.COM 		vector ++;
44538571SChenlu.Chen@Sun.COM 	}
44548571SChenlu.Chen@Sun.COM 
44558571SChenlu.Chen@Sun.COM 	for (i = 1; i < igb->num_tx_rings; i++) {
44568571SChenlu.Chen@Sun.COM 		/*
44578571SChenlu.Chen@Sun.COM 		 * Set vector for each tx ring from 2nd tx ring.
44588571SChenlu.Chen@Sun.COM 		 * Note assumption that tx vectors numericall follow rx vectors.
44598571SChenlu.Chen@Sun.COM 		 */
44608571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
44618571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
44628571SChenlu.Chen@Sun.COM 
44638571SChenlu.Chen@Sun.COM 		if (i < 8) {
44648571SChenlu.Chen@Sun.COM 			/* vector goes into second byte of register */
44658571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFF00FF;
44668571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 8);
44678571SChenlu.Chen@Sun.COM 		} else {
44688571SChenlu.Chen@Sun.COM 			/* vector goes into fourth byte of register */
44698571SChenlu.Chen@Sun.COM 			ivar = ivar & 0x00FFFFFF;
44708571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID) << 24;
44718571SChenlu.Chen@Sun.COM 		}
44728571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
44738571SChenlu.Chen@Sun.COM 
44748571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
44758571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
44768571SChenlu.Chen@Sun.COM 
44778571SChenlu.Chen@Sun.COM 		vector ++;
44788571SChenlu.Chen@Sun.COM 	}
44798571SChenlu.Chen@Sun.COM 
44808571SChenlu.Chen@Sun.COM 	ASSERT(vector == igb->intr_cnt);
44818571SChenlu.Chen@Sun.COM }
44828571SChenlu.Chen@Sun.COM 
44838571SChenlu.Chen@Sun.COM /*
44845779Sxy150489  * igb_rem_intr_handlers - remove the interrupt handlers
44855779Sxy150489  */
44865779Sxy150489 static void
44875779Sxy150489 igb_rem_intr_handlers(igb_t *igb)
44885779Sxy150489 {
44895779Sxy150489 	int i;
44905779Sxy150489 	int rc;
44915779Sxy150489 
44925779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
44935779Sxy150489 		rc = ddi_intr_remove_handler(igb->htable[i]);
44945779Sxy150489 		if (rc != DDI_SUCCESS) {
44955779Sxy150489 			IGB_DEBUGLOG_1(igb,
44965779Sxy150489 			    "Remove intr handler failed: %d", rc);
44975779Sxy150489 		}
44985779Sxy150489 	}
44995779Sxy150489 }
45005779Sxy150489 
45015779Sxy150489 /*
45025779Sxy150489  * igb_rem_intrs - remove the allocated interrupts
45035779Sxy150489  */
45045779Sxy150489 static void
45055779Sxy150489 igb_rem_intrs(igb_t *igb)
45065779Sxy150489 {
45075779Sxy150489 	int i;
45085779Sxy150489 	int rc;
45095779Sxy150489 
45105779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
45115779Sxy150489 		rc = ddi_intr_free(igb->htable[i]);
45125779Sxy150489 		if (rc != DDI_SUCCESS) {
45135779Sxy150489 			IGB_DEBUGLOG_1(igb,
45145779Sxy150489 			    "Free intr failed: %d", rc);
45155779Sxy150489 		}
45165779Sxy150489 	}
45175779Sxy150489 
45185779Sxy150489 	kmem_free(igb->htable, igb->intr_size);
45195779Sxy150489 	igb->htable = NULL;
45205779Sxy150489 }
45215779Sxy150489 
45225779Sxy150489 /*
45235779Sxy150489  * igb_enable_intrs - enable all the ddi interrupts
45245779Sxy150489  */
45255779Sxy150489 static int
45265779Sxy150489 igb_enable_intrs(igb_t *igb)
45275779Sxy150489 {
45285779Sxy150489 	int i;
45295779Sxy150489 	int rc;
45305779Sxy150489 
45315779Sxy150489 	/* Enable interrupts */
45325779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
45335779Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
45345779Sxy150489 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
45355779Sxy150489 		if (rc != DDI_SUCCESS) {
45365779Sxy150489 			igb_log(igb,
45375779Sxy150489 			    "Enable block intr failed: %d", rc);
45385779Sxy150489 			return (IGB_FAILURE);
45395779Sxy150489 		}
45405779Sxy150489 	} else {
45415779Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
45425779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
45435779Sxy150489 			rc = ddi_intr_enable(igb->htable[i]);
45445779Sxy150489 			if (rc != DDI_SUCCESS) {
45455779Sxy150489 				igb_log(igb,
45465779Sxy150489 				    "Enable intr failed: %d", rc);
45475779Sxy150489 				return (IGB_FAILURE);
45485779Sxy150489 			}
45495779Sxy150489 		}
45505779Sxy150489 	}
45515779Sxy150489 
45525779Sxy150489 	return (IGB_SUCCESS);
45535779Sxy150489 }
45545779Sxy150489 
45555779Sxy150489 /*
45565779Sxy150489  * igb_disable_intrs - disable all the ddi interrupts
45575779Sxy150489  */
45585779Sxy150489 static int
45595779Sxy150489 igb_disable_intrs(igb_t *igb)
45605779Sxy150489 {
45615779Sxy150489 	int i;
45625779Sxy150489 	int rc;
45635779Sxy150489 
45645779Sxy150489 	/* Disable all interrupts */
45655779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
45665779Sxy150489 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
45675779Sxy150489 		if (rc != DDI_SUCCESS) {
45685779Sxy150489 			igb_log(igb,
45695779Sxy150489 			    "Disable block intr failed: %d", rc);
45705779Sxy150489 			return (IGB_FAILURE);
45715779Sxy150489 		}
45725779Sxy150489 	} else {
45735779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
45745779Sxy150489 			rc = ddi_intr_disable(igb->htable[i]);
45755779Sxy150489 			if (rc != DDI_SUCCESS) {
45765779Sxy150489 				igb_log(igb,
45775779Sxy150489 				    "Disable intr failed: %d", rc);
45785779Sxy150489 				return (IGB_FAILURE);
45795779Sxy150489 			}
45805779Sxy150489 		}
45815779Sxy150489 	}
45825779Sxy150489 
45835779Sxy150489 	return (IGB_SUCCESS);
45845779Sxy150489 }
45855779Sxy150489 
45865779Sxy150489 /*
45875779Sxy150489  * igb_get_phy_state - Get and save the parameters read from PHY registers
45885779Sxy150489  */
45895779Sxy150489 static void
45905779Sxy150489 igb_get_phy_state(igb_t *igb)
45915779Sxy150489 {
45925779Sxy150489 	struct e1000_hw *hw = &igb->hw;
45935779Sxy150489 	uint16_t phy_ctrl;
45945779Sxy150489 	uint16_t phy_status;
45955779Sxy150489 	uint16_t phy_an_adv;
45965779Sxy150489 	uint16_t phy_an_exp;
45975779Sxy150489 	uint16_t phy_ext_status;
45985779Sxy150489 	uint16_t phy_1000t_ctrl;
45995779Sxy150489 	uint16_t phy_1000t_status;
46005779Sxy150489 	uint16_t phy_lp_able;
46015779Sxy150489 
46025779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
46035779Sxy150489 
46045779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
46055779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
46065779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
46075779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
46085779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
46095779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
46105779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_1000t_status);
46115779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
46125779Sxy150489 
46135779Sxy150489 	igb->param_autoneg_cap =
46145779Sxy150489 	    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
46155779Sxy150489 	igb->param_pause_cap =
46165779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
46175779Sxy150489 	igb->param_asym_pause_cap =
46185779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
46195779Sxy150489 	igb->param_1000fdx_cap = ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
46205779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
46215779Sxy150489 	igb->param_1000hdx_cap = ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
46225779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
46235779Sxy150489 	igb->param_100t4_cap =
46245779Sxy150489 	    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
46255779Sxy150489 	igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
46265779Sxy150489 	    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
46275779Sxy150489 	igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
46285779Sxy150489 	    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
46295779Sxy150489 	igb->param_10fdx_cap =
46305779Sxy150489 	    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
46315779Sxy150489 	igb->param_10hdx_cap =
46325779Sxy150489 	    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
46335779Sxy150489 	igb->param_rem_fault =
46345779Sxy150489 	    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
46355779Sxy150489 
46365779Sxy150489 	igb->param_adv_autoneg_cap = hw->mac.autoneg;
46375779Sxy150489 	igb->param_adv_pause_cap =
46385779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
46395779Sxy150489 	igb->param_adv_asym_pause_cap =
46405779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
46415779Sxy150489 	igb->param_adv_1000hdx_cap =
46425779Sxy150489 	    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
46435779Sxy150489 	igb->param_adv_100t4_cap =
46445779Sxy150489 	    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
46455779Sxy150489 	igb->param_adv_rem_fault =
46465779Sxy150489 	    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
46475779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
46485779Sxy150489 		igb->param_adv_1000fdx_cap =
46495779Sxy150489 		    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
46505779Sxy150489 		igb->param_adv_100fdx_cap =
46515779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
46525779Sxy150489 		igb->param_adv_100hdx_cap =
46535779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
46545779Sxy150489 		igb->param_adv_10fdx_cap =
46555779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
46565779Sxy150489 		igb->param_adv_10hdx_cap =
46575779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
46585779Sxy150489 	}
46595779Sxy150489 
46605779Sxy150489 	igb->param_lp_autoneg_cap =
46615779Sxy150489 	    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
46625779Sxy150489 	igb->param_lp_pause_cap =
46635779Sxy150489 	    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
46645779Sxy150489 	igb->param_lp_asym_pause_cap =
46655779Sxy150489 	    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
46665779Sxy150489 	igb->param_lp_1000fdx_cap =
46675779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
46685779Sxy150489 	igb->param_lp_1000hdx_cap =
46695779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
46705779Sxy150489 	igb->param_lp_100t4_cap =
46715779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
46725779Sxy150489 	igb->param_lp_100fdx_cap =
46735779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
46745779Sxy150489 	igb->param_lp_100hdx_cap =
46755779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
46765779Sxy150489 	igb->param_lp_10fdx_cap =
46775779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
46785779Sxy150489 	igb->param_lp_10hdx_cap =
46795779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
46805779Sxy150489 	igb->param_lp_rem_fault =
46815779Sxy150489 	    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
46825779Sxy150489 }
46835779Sxy150489 
46845779Sxy150489 /*
46855779Sxy150489  * igb_get_driver_control
46865779Sxy150489  */
46875779Sxy150489 static void
46885779Sxy150489 igb_get_driver_control(struct e1000_hw *hw)
46895779Sxy150489 {
46905779Sxy150489 	uint32_t ctrl_ext;
46915779Sxy150489 
46925779Sxy150489 	/* Notify firmware that driver is in control of device */
46935779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
46945779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
46955779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
46965779Sxy150489 }
46975779Sxy150489 
46985779Sxy150489 /*
46995779Sxy150489  * igb_release_driver_control
47005779Sxy150489  */
47015779Sxy150489 static void
47025779Sxy150489 igb_release_driver_control(struct e1000_hw *hw)
47035779Sxy150489 {
47045779Sxy150489 	uint32_t ctrl_ext;
47055779Sxy150489 
47065779Sxy150489 	/* Notify firmware that driver is no longer in control of device */
47075779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
47085779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
47095779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
47105779Sxy150489 }
47115779Sxy150489 
47125779Sxy150489 /*
47135779Sxy150489  * igb_atomic_reserve - Atomic decrease operation
47145779Sxy150489  */
47155779Sxy150489 int
47165779Sxy150489 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
47175779Sxy150489 {
47185779Sxy150489 	uint32_t oldval;
47195779Sxy150489 	uint32_t newval;
47205779Sxy150489 
47215779Sxy150489 	/* ATOMICALLY */
47225779Sxy150489 	do {
47235779Sxy150489 		oldval = *count_p;
47245779Sxy150489 		if (oldval < n)
47255779Sxy150489 			return (-1);
47265779Sxy150489 		newval = oldval - n;
47275779Sxy150489 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
47285779Sxy150489 
47295779Sxy150489 	return (newval);
47305779Sxy150489 }
47316624Sgl147354 
47326624Sgl147354 /*
47336624Sgl147354  * FMA support
47346624Sgl147354  */
47356624Sgl147354 
47366624Sgl147354 int
47376624Sgl147354 igb_check_acc_handle(ddi_acc_handle_t handle)
47386624Sgl147354 {
47396624Sgl147354 	ddi_fm_error_t de;
47406624Sgl147354 
47416624Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
47426624Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
47436624Sgl147354 	return (de.fme_status);
47446624Sgl147354 }
47456624Sgl147354 
47466624Sgl147354 int
47476624Sgl147354 igb_check_dma_handle(ddi_dma_handle_t handle)
47486624Sgl147354 {
47496624Sgl147354 	ddi_fm_error_t de;
47506624Sgl147354 
47516624Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
47526624Sgl147354 	return (de.fme_status);
47536624Sgl147354 }
47546624Sgl147354 
47556624Sgl147354 /*
47566624Sgl147354  * The IO fault service error handling callback function
47576624Sgl147354  */
47586624Sgl147354 /*ARGSUSED*/
47596624Sgl147354 static int
47606624Sgl147354 igb_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
47616624Sgl147354 {
47626624Sgl147354 	/*
47636624Sgl147354 	 * as the driver can always deal with an error in any dma or
47646624Sgl147354 	 * access handle, we can just return the fme_status value.
47656624Sgl147354 	 */
47666624Sgl147354 	pci_ereport_post(dip, err, NULL);
47676624Sgl147354 	return (err->fme_status);
47686624Sgl147354 }
47696624Sgl147354 
47706624Sgl147354 static void
47716624Sgl147354 igb_fm_init(igb_t *igb)
47726624Sgl147354 {
47736624Sgl147354 	ddi_iblock_cookie_t iblk;
47746624Sgl147354 	int fma_acc_flag, fma_dma_flag;
47756624Sgl147354 
47766624Sgl147354 	/* Only register with IO Fault Services if we have some capability */
47776624Sgl147354 	if (igb->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
47786624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
47796624Sgl147354 		fma_acc_flag = 1;
47806624Sgl147354 	} else {
47816624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
47826624Sgl147354 		fma_acc_flag = 0;
47836624Sgl147354 	}
47846624Sgl147354 
47856624Sgl147354 	if (igb->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
47866624Sgl147354 		fma_dma_flag = 1;
47876624Sgl147354 	} else {
47886624Sgl147354 		fma_dma_flag = 0;
47896624Sgl147354 	}
47906624Sgl147354 
47916624Sgl147354 	(void) igb_set_fma_flags(fma_acc_flag, fma_dma_flag);
47926624Sgl147354 
47936624Sgl147354 	if (igb->fm_capabilities) {
47946624Sgl147354 
47956624Sgl147354 		/* Register capabilities with IO Fault Services */
47966624Sgl147354 		ddi_fm_init(igb->dip, &igb->fm_capabilities, &iblk);
47976624Sgl147354 
47986624Sgl147354 		/*
47996624Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
48006624Sgl147354 		 */
48016624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
48026624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
48036624Sgl147354 			pci_ereport_setup(igb->dip);
48046624Sgl147354 
48056624Sgl147354 		/*
48066624Sgl147354 		 * Register error callback if error callback capable
48076624Sgl147354 		 */
48086624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
48096624Sgl147354 			ddi_fm_handler_register(igb->dip,
48106624Sgl147354 			    igb_fm_error_cb, (void*) igb);
48116624Sgl147354 	}
48126624Sgl147354 }
48136624Sgl147354 
48146624Sgl147354 static void
48156624Sgl147354 igb_fm_fini(igb_t *igb)
48166624Sgl147354 {
48176624Sgl147354 	/* Only unregister FMA capabilities if we registered some */
48186624Sgl147354 	if (igb->fm_capabilities) {
48196624Sgl147354 
48206624Sgl147354 		/*
48216624Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
48226624Sgl147354 		 */
48236624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
48246624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
48256624Sgl147354 			pci_ereport_teardown(igb->dip);
48266624Sgl147354 
48276624Sgl147354 		/*
48286624Sgl147354 		 * Un-register error callback if error callback capable
48296624Sgl147354 		 */
48306624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
48316624Sgl147354 			ddi_fm_handler_unregister(igb->dip);
48326624Sgl147354 
48336624Sgl147354 		/* Unregister from IO Fault Services */
48346624Sgl147354 		ddi_fm_fini(igb->dip);
48356624Sgl147354 	}
48366624Sgl147354 }
48376624Sgl147354 
48386624Sgl147354 void
48396624Sgl147354 igb_fm_ereport(igb_t *igb, char *detail)
48406624Sgl147354 {
48416624Sgl147354 	uint64_t ena;
48426624Sgl147354 	char buf[FM_MAX_CLASS];
48436624Sgl147354 
48446624Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
48456624Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
48466624Sgl147354 	if (DDI_FM_EREPORT_CAP(igb->fm_capabilities)) {
48476624Sgl147354 		ddi_fm_ereport_post(igb->dip, buf, ena, DDI_NOSLEEP,
48486624Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
48496624Sgl147354 	}
48506624Sgl147354 }
4851