xref: /onnv-gate/usr/src/uts/common/io/igb/igb_main.c (revision 8571:60e408ef19cc)
15779Sxy150489 /*
25779Sxy150489  * CDDL HEADER START
35779Sxy150489  *
4*8571SChenlu.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  *
9*8571SChenlu.Chen@Sun.COM  * You can obtain a copy of the license at:
10*8571SChenlu.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  *
14*8571SChenlu.Chen@Sun.COM  * When using or redistributing this file, you may do so under the
15*8571SChenlu.Chen@Sun.COM  * License only. No other modification of this header is permitted.
16*8571SChenlu.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 /*
25*8571SChenlu.Chen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26*8571SChenlu.Chen@Sun.COM  * Use is subject to license terms of the CDDL.
275779Sxy150489  */
285779Sxy150489 
295779Sxy150489 #include "igb_sw.h"
305779Sxy150489 
317656SSherry.Moore@Sun.COM static char ident[] = "Intel 1Gb Ethernet";
32*8571SChenlu.Chen@Sun.COM static char igb_version[] = "igb 1.1.4";
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 *);
445779Sxy150489 static int igb_init(igb_t *);
455779Sxy150489 static int igb_chip_start(igb_t *);
465779Sxy150489 static void igb_chip_stop(igb_t *);
475779Sxy150489 static int igb_reset(igb_t *);
485779Sxy150489 static void igb_tx_clean(igb_t *);
495779Sxy150489 static boolean_t igb_tx_drain(igb_t *);
505779Sxy150489 static boolean_t igb_rx_drain(igb_t *);
515779Sxy150489 static int igb_alloc_rings(igb_t *);
525779Sxy150489 static int igb_init_rings(igb_t *);
535779Sxy150489 static void igb_free_rings(igb_t *);
545779Sxy150489 static void igb_fini_rings(igb_t *);
555779Sxy150489 static void igb_setup_rings(igb_t *);
565779Sxy150489 static void igb_setup_rx(igb_t *);
575779Sxy150489 static void igb_setup_tx(igb_t *);
585779Sxy150489 static void igb_setup_rx_ring(igb_rx_ring_t *);
595779Sxy150489 static void igb_setup_tx_ring(igb_tx_ring_t *);
605779Sxy150489 static void igb_setup_rss(igb_t *);
618275SEric Cheng static void igb_setup_mac_rss_classify(igb_t *);
628275SEric Cheng static void igb_setup_mac_classify(igb_t *);
635779Sxy150489 static void igb_init_unicst(igb_t *);
645779Sxy150489 static void igb_setup_multicst(igb_t *);
655779Sxy150489 static void igb_get_phy_state(igb_t *);
665779Sxy150489 static void igb_get_conf(igb_t *);
675779Sxy150489 static int igb_get_prop(igb_t *, char *, int, int, int);
685779Sxy150489 static boolean_t igb_is_link_up(igb_t *);
695779Sxy150489 static boolean_t igb_link_check(igb_t *);
705779Sxy150489 static void igb_local_timer(void *);
715779Sxy150489 static void igb_arm_watchdog_timer(igb_t *);
725779Sxy150489 static void igb_start_watchdog_timer(igb_t *);
735779Sxy150489 static void igb_restart_watchdog_timer(igb_t *);
745779Sxy150489 static void igb_stop_watchdog_timer(igb_t *);
755779Sxy150489 static void igb_disable_adapter_interrupts(igb_t *);
76*8571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82575(igb_t *);
77*8571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82576(igb_t *);
785779Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
795779Sxy150489 static boolean_t igb_stall_check(igb_t *);
805779Sxy150489 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
815779Sxy150489 static void igb_set_external_loopback(igb_t *);
825779Sxy150489 static void igb_set_internal_mac_loopback(igb_t *);
835779Sxy150489 static void igb_set_internal_phy_loopback(igb_t *);
845779Sxy150489 static void igb_set_internal_serdes_loopback(igb_t *);
855779Sxy150489 static boolean_t igb_find_mac_address(igb_t *);
865779Sxy150489 static int igb_alloc_intrs(igb_t *);
877072Sxy150489 static int igb_alloc_intr_handles(igb_t *, int);
885779Sxy150489 static int igb_add_intr_handlers(igb_t *);
895779Sxy150489 static void igb_rem_intr_handlers(igb_t *);
905779Sxy150489 static void igb_rem_intrs(igb_t *);
915779Sxy150489 static int igb_enable_intrs(igb_t *);
925779Sxy150489 static int igb_disable_intrs(igb_t *);
93*8571SChenlu.Chen@Sun.COM static void igb_setup_msix_82575(igb_t *);
94*8571SChenlu.Chen@Sun.COM static void igb_setup_msix_82576(igb_t *);
955779Sxy150489 static uint_t igb_intr_legacy(void *, void *);
965779Sxy150489 static uint_t igb_intr_msi(void *, void *);
975779Sxy150489 static uint_t igb_intr_rx(void *, void *);
988275SEric Cheng static uint_t igb_intr_tx(void *, void *);
995779Sxy150489 static uint_t igb_intr_tx_other(void *, void *);
1005779Sxy150489 static void igb_intr_rx_work(igb_rx_ring_t *);
1015779Sxy150489 static void igb_intr_tx_work(igb_tx_ring_t *);
1028275SEric Cheng static void igb_intr_link_work(igb_t *);
1035779Sxy150489 static void igb_get_driver_control(struct e1000_hw *);
1045779Sxy150489 static void igb_release_driver_control(struct e1000_hw *);
1055779Sxy150489 
1065779Sxy150489 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
1075779Sxy150489 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
1085779Sxy150489 static int igb_resume(dev_info_t *);
1095779Sxy150489 static int igb_suspend(dev_info_t *);
1107656SSherry.Moore@Sun.COM static int igb_quiesce(dev_info_t *);
1115779Sxy150489 static void igb_unconfigure(dev_info_t *, igb_t *);
1126624Sgl147354 static int igb_fm_error_cb(dev_info_t *, ddi_fm_error_t *,
1136624Sgl147354     const void *);
1146624Sgl147354 static void igb_fm_init(igb_t *);
1156624Sgl147354 static void igb_fm_fini(igb_t *);
1166624Sgl147354 
1175779Sxy150489 
1185779Sxy150489 static struct cb_ops igb_cb_ops = {
1195779Sxy150489 	nulldev,		/* cb_open */
1205779Sxy150489 	nulldev,		/* cb_close */
1215779Sxy150489 	nodev,			/* cb_strategy */
1225779Sxy150489 	nodev,			/* cb_print */
1235779Sxy150489 	nodev,			/* cb_dump */
1245779Sxy150489 	nodev,			/* cb_read */
1255779Sxy150489 	nodev,			/* cb_write */
1265779Sxy150489 	nodev,			/* cb_ioctl */
1275779Sxy150489 	nodev,			/* cb_devmap */
1285779Sxy150489 	nodev,			/* cb_mmap */
1295779Sxy150489 	nodev,			/* cb_segmap */
1305779Sxy150489 	nochpoll,		/* cb_chpoll */
1315779Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1325779Sxy150489 	NULL,			/* cb_stream */
1335779Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1345779Sxy150489 	CB_REV,			/* cb_rev */
1355779Sxy150489 	nodev,			/* cb_aread */
1365779Sxy150489 	nodev			/* cb_awrite */
1375779Sxy150489 };
1385779Sxy150489 
1395779Sxy150489 static struct dev_ops igb_dev_ops = {
1405779Sxy150489 	DEVO_REV,		/* devo_rev */
1415779Sxy150489 	0,			/* devo_refcnt */
1425779Sxy150489 	NULL,			/* devo_getinfo */
1435779Sxy150489 	nulldev,		/* devo_identify */
1445779Sxy150489 	nulldev,		/* devo_probe */
1455779Sxy150489 	igb_attach,		/* devo_attach */
1465779Sxy150489 	igb_detach,		/* devo_detach */
1475779Sxy150489 	nodev,			/* devo_reset */
1485779Sxy150489 	&igb_cb_ops,		/* devo_cb_ops */
1495779Sxy150489 	NULL,			/* devo_bus_ops */
1507656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
1517656SSherry.Moore@Sun.COM 	igb_quiesce,	/* devo_quiesce */
1525779Sxy150489 };
1535779Sxy150489 
1545779Sxy150489 static struct modldrv igb_modldrv = {
1555779Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1565779Sxy150489 	ident,			/* Discription string */
1575779Sxy150489 	&igb_dev_ops,		/* driver ops */
1585779Sxy150489 };
1595779Sxy150489 
1605779Sxy150489 static struct modlinkage igb_modlinkage = {
1615779Sxy150489 	MODREV_1, &igb_modldrv, NULL
1625779Sxy150489 };
1635779Sxy150489 
1645779Sxy150489 /* Access attributes for register mapping */
1655779Sxy150489 ddi_device_acc_attr_t igb_regs_acc_attr = {
1665779Sxy150489 	DDI_DEVICE_ATTR_V0,
1675779Sxy150489 	DDI_STRUCTURE_LE_ACC,
1685779Sxy150489 	DDI_STRICTORDER_ACC,
1696624Sgl147354 	DDI_FLAGERR_ACC
1705779Sxy150489 };
1715779Sxy150489 
1725779Sxy150489 #define	IGB_M_CALLBACK_FLAGS	(MC_IOCTL | MC_GETCAPAB)
1735779Sxy150489 
1745779Sxy150489 static mac_callbacks_t igb_m_callbacks = {
1755779Sxy150489 	IGB_M_CALLBACK_FLAGS,
1765779Sxy150489 	igb_m_stat,
1775779Sxy150489 	igb_m_start,
1785779Sxy150489 	igb_m_stop,
1795779Sxy150489 	igb_m_promisc,
1805779Sxy150489 	igb_m_multicst,
1818275SEric Cheng 	NULL,
1825779Sxy150489 	NULL,
1835779Sxy150489 	igb_m_ioctl,
1845779Sxy150489 	igb_m_getcapab
1855779Sxy150489 };
1865779Sxy150489 
1875779Sxy150489 /*
188*8571SChenlu.Chen@Sun.COM  * Initialize capabilities of each supported adapter type
189*8571SChenlu.Chen@Sun.COM  */
190*8571SChenlu.Chen@Sun.COM static adapter_info_t igb_82575_cap = {
191*8571SChenlu.Chen@Sun.COM 	/* limits */
192*8571SChenlu.Chen@Sun.COM 	4,		/* maximum number of rx queues */
193*8571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
194*8571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
195*8571SChenlu.Chen@Sun.COM 	4,		/* maximum number of tx queues */
196*8571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
197*8571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
198*8571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
199*8571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
200*8571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
201*8571SChenlu.Chen@Sun.COM 
202*8571SChenlu.Chen@Sun.COM 	/* function pointers */
203*8571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82575,
204*8571SChenlu.Chen@Sun.COM 	igb_setup_msix_82575,
205*8571SChenlu.Chen@Sun.COM 
206*8571SChenlu.Chen@Sun.COM 	/* capabilities */
207*8571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
208*8571SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL)
209*8571SChenlu.Chen@Sun.COM };
210*8571SChenlu.Chen@Sun.COM 
211*8571SChenlu.Chen@Sun.COM static adapter_info_t igb_82576_cap = {
212*8571SChenlu.Chen@Sun.COM 	/* limits */
213*8571SChenlu.Chen@Sun.COM 	12,		/* maximum number of rx queues */
214*8571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
215*8571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
216*8571SChenlu.Chen@Sun.COM 	12,		/* maximum number of tx queues */
217*8571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
218*8571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
219*8571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
220*8571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
221*8571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
222*8571SChenlu.Chen@Sun.COM 
223*8571SChenlu.Chen@Sun.COM 	/* function pointers */
224*8571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82576,
225*8571SChenlu.Chen@Sun.COM 	igb_setup_msix_82576,
226*8571SChenlu.Chen@Sun.COM 
227*8571SChenlu.Chen@Sun.COM 	/* capabilities */
228*8571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
229*8571SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL |
230*8571SChenlu.Chen@Sun.COM 	IGB_FLAG_NEED_CTX_IDX)
231*8571SChenlu.Chen@Sun.COM };
232*8571SChenlu.Chen@Sun.COM 
233*8571SChenlu.Chen@Sun.COM /*
2345779Sxy150489  * Module Initialization Functions
2355779Sxy150489  */
2365779Sxy150489 
2375779Sxy150489 int
2385779Sxy150489 _init(void)
2395779Sxy150489 {
2405779Sxy150489 	int status;
2415779Sxy150489 
2425779Sxy150489 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
2435779Sxy150489 
2445779Sxy150489 	status = mod_install(&igb_modlinkage);
2455779Sxy150489 
2465779Sxy150489 	if (status != DDI_SUCCESS) {
2475779Sxy150489 		mac_fini_ops(&igb_dev_ops);
2485779Sxy150489 	}
2495779Sxy150489 
2505779Sxy150489 	return (status);
2515779Sxy150489 }
2525779Sxy150489 
2535779Sxy150489 int
2545779Sxy150489 _fini(void)
2555779Sxy150489 {
2565779Sxy150489 	int status;
2575779Sxy150489 
2585779Sxy150489 	status = mod_remove(&igb_modlinkage);
2595779Sxy150489 
2605779Sxy150489 	if (status == DDI_SUCCESS) {
2615779Sxy150489 		mac_fini_ops(&igb_dev_ops);
2625779Sxy150489 	}
2635779Sxy150489 
2645779Sxy150489 	return (status);
2655779Sxy150489 
2665779Sxy150489 }
2675779Sxy150489 
2685779Sxy150489 int
2695779Sxy150489 _info(struct modinfo *modinfop)
2705779Sxy150489 {
2715779Sxy150489 	int status;
2725779Sxy150489 
2735779Sxy150489 	status = mod_info(&igb_modlinkage, modinfop);
2745779Sxy150489 
2755779Sxy150489 	return (status);
2765779Sxy150489 }
2775779Sxy150489 
2785779Sxy150489 /*
2795779Sxy150489  * igb_attach - driver attach
2805779Sxy150489  *
2815779Sxy150489  * This function is the device specific initialization entry
2825779Sxy150489  * point. This entry point is required and must be written.
2835779Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
2845779Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
2855779Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
2865779Sxy150489  * available for use by the driver.
2875779Sxy150489  *
2885779Sxy150489  * The attach() function will be called once for each instance
2895779Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
2905779Sxy150489  * Until attach() succeeds, the only driver entry points which
2915779Sxy150489  * may be called are open(9E) and getinfo(9E).
2925779Sxy150489  */
2935779Sxy150489 static int
2945779Sxy150489 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2955779Sxy150489 {
2965779Sxy150489 	igb_t *igb;
2975779Sxy150489 	struct igb_osdep *osdep;
2985779Sxy150489 	struct e1000_hw *hw;
2995779Sxy150489 	int instance;
3005779Sxy150489 
3015779Sxy150489 	/*
3025779Sxy150489 	 * Check the command and perform corresponding operations
3035779Sxy150489 	 */
3045779Sxy150489 	switch (cmd) {
3055779Sxy150489 	default:
3065779Sxy150489 		return (DDI_FAILURE);
3075779Sxy150489 
3085779Sxy150489 	case DDI_RESUME:
3095779Sxy150489 		return (igb_resume(devinfo));
3105779Sxy150489 
3115779Sxy150489 	case DDI_ATTACH:
3125779Sxy150489 		break;
3135779Sxy150489 	}
3145779Sxy150489 
3155779Sxy150489 	/* Get the device instance */
3165779Sxy150489 	instance = ddi_get_instance(devinfo);
3175779Sxy150489 
3185779Sxy150489 	/* Allocate memory for the instance data structure */
3195779Sxy150489 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
3205779Sxy150489 
3215779Sxy150489 	igb->dip = devinfo;
3225779Sxy150489 	igb->instance = instance;
3235779Sxy150489 
3245779Sxy150489 	hw = &igb->hw;
3255779Sxy150489 	osdep = &igb->osdep;
3265779Sxy150489 	hw->back = osdep;
3275779Sxy150489 	osdep->igb = igb;
3285779Sxy150489 
3295779Sxy150489 	/* Attach the instance pointer to the dev_info data structure */
3305779Sxy150489 	ddi_set_driver_private(devinfo, igb);
3315779Sxy150489 
3326624Sgl147354 
3336624Sgl147354 	/* Initialize for fma support */
3346624Sgl147354 	igb->fm_capabilities = igb_get_prop(igb, "fm-capable",
3356624Sgl147354 	    0, 0x0f,
3366624Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
3376624Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
3386624Sgl147354 	igb_fm_init(igb);
3396624Sgl147354 	igb->attach_progress |= ATTACH_PROGRESS_FMINIT;
3406624Sgl147354 
3415779Sxy150489 	/*
3425779Sxy150489 	 * Map PCI config space registers
3435779Sxy150489 	 */
3445779Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
3455779Sxy150489 		igb_error(igb, "Failed to map PCI configurations");
3465779Sxy150489 		goto attach_fail;
3475779Sxy150489 	}
3485779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
3495779Sxy150489 
3505779Sxy150489 	/*
3515779Sxy150489 	 * Identify the chipset family
3525779Sxy150489 	 */
3535779Sxy150489 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
3545779Sxy150489 		igb_error(igb, "Failed to identify hardware");
3555779Sxy150489 		goto attach_fail;
3565779Sxy150489 	}
3575779Sxy150489 
3585779Sxy150489 	/*
3595779Sxy150489 	 * Map device registers
3605779Sxy150489 	 */
3615779Sxy150489 	if (igb_regs_map(igb) != IGB_SUCCESS) {
3625779Sxy150489 		igb_error(igb, "Failed to map device registers");
3635779Sxy150489 		goto attach_fail;
3645779Sxy150489 	}
3655779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
3665779Sxy150489 
3675779Sxy150489 	/*
3685779Sxy150489 	 * Initialize driver parameters
3695779Sxy150489 	 */
3705779Sxy150489 	igb_init_properties(igb);
3715779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
3725779Sxy150489 
3735779Sxy150489 	/*
3745779Sxy150489 	 * Allocate interrupts
3755779Sxy150489 	 */
3765779Sxy150489 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
3775779Sxy150489 		igb_error(igb, "Failed to allocate interrupts");
3785779Sxy150489 		goto attach_fail;
3795779Sxy150489 	}
3805779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
3815779Sxy150489 
3825779Sxy150489 	/*
3835779Sxy150489 	 * Allocate rx/tx rings based on the ring numbers.
3845779Sxy150489 	 * The actual numbers of rx/tx rings are decided by the number of
3855779Sxy150489 	 * allocated interrupt vectors, so we should allocate the rings after
3865779Sxy150489 	 * interrupts are allocated.
3875779Sxy150489 	 */
3885779Sxy150489 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
3898275SEric Cheng 		igb_error(igb, "Failed to allocate rx/tx rings or groups");
3905779Sxy150489 		goto attach_fail;
3915779Sxy150489 	}
3925779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
3935779Sxy150489 
3945779Sxy150489 	/*
3955779Sxy150489 	 * Add interrupt handlers
3965779Sxy150489 	 */
3975779Sxy150489 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
3985779Sxy150489 		igb_error(igb, "Failed to add interrupt handlers");
3995779Sxy150489 		goto attach_fail;
4005779Sxy150489 	}
4015779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
4025779Sxy150489 
4035779Sxy150489 	/*
4045779Sxy150489 	 * Initialize driver parameters
4055779Sxy150489 	 */
4065779Sxy150489 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
4075779Sxy150489 		igb_error(igb, "Failed to initialize driver settings");
4085779Sxy150489 		goto attach_fail;
4095779Sxy150489 	}
4105779Sxy150489 
4116624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
4126624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
4136624Sgl147354 		goto attach_fail;
4146624Sgl147354 	}
4156624Sgl147354 
4165779Sxy150489 	/*
4175779Sxy150489 	 * Initialize mutexes for this device.
4185779Sxy150489 	 * Do this before enabling the interrupt handler and
4195779Sxy150489 	 * register the softint to avoid the condition where
4205779Sxy150489 	 * interrupt handler can try using uninitialized mutex
4215779Sxy150489 	 */
4225779Sxy150489 	igb_init_locks(igb);
4235779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
4245779Sxy150489 
4255779Sxy150489 	/*
4265779Sxy150489 	 * Initialize chipset hardware
4275779Sxy150489 	 */
4288275SEric Cheng 	mutex_enter(&igb->gen_lock);
4295779Sxy150489 	if (igb_init(igb) != IGB_SUCCESS) {
4308275SEric Cheng 		mutex_exit(&igb->gen_lock);
4315779Sxy150489 		igb_error(igb, "Failed to initialize adapter");
4325779Sxy150489 		goto attach_fail;
4335779Sxy150489 	}
4348275SEric Cheng 	mutex_exit(&igb->gen_lock);
4355779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT;
4365779Sxy150489 
4375779Sxy150489 	/*
4385779Sxy150489 	 * Initialize DMA and hardware settings for rx/tx rings
4395779Sxy150489 	 */
4405779Sxy150489 	if (igb_init_rings(igb) != IGB_SUCCESS) {
4415779Sxy150489 		igb_error(igb, "Failed to initialize rings");
4425779Sxy150489 		goto attach_fail;
4435779Sxy150489 	}
4445779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT_RINGS;
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 
483*8571SChenlu.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 
6207656SSherry.Moore@Sun.COM 
6215779Sxy150489 static void
6225779Sxy150489 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
6235779Sxy150489 {
6245779Sxy150489 	/*
6255779Sxy150489 	 * Disable interrupt
6265779Sxy150489 	 */
6275779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
6285779Sxy150489 		(void) igb_disable_intrs(igb);
6295779Sxy150489 	}
6305779Sxy150489 
6315779Sxy150489 	/*
6325779Sxy150489 	 * Unregister MAC
6335779Sxy150489 	 */
6345779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
6355779Sxy150489 		(void) mac_unregister(igb->mac_hdl);
6365779Sxy150489 	}
6375779Sxy150489 
6385779Sxy150489 	/*
6395779Sxy150489 	 * Free ndd parameters
6405779Sxy150489 	 */
6415779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_NDD) {
6425779Sxy150489 		igb_nd_cleanup(igb);
6435779Sxy150489 	}
6445779Sxy150489 
6455779Sxy150489 	/*
6465779Sxy150489 	 * Free statistics
6475779Sxy150489 	 */
6485779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
6495779Sxy150489 		kstat_delete((kstat_t *)igb->igb_ks);
6505779Sxy150489 	}
6515779Sxy150489 
6525779Sxy150489 	/*
6535779Sxy150489 	 * Remove interrupt handlers
6545779Sxy150489 	 */
6555779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
6565779Sxy150489 		igb_rem_intr_handlers(igb);
6575779Sxy150489 	}
6585779Sxy150489 
6595779Sxy150489 	/*
6605779Sxy150489 	 * Remove interrupts
6615779Sxy150489 	 */
6625779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
6635779Sxy150489 		igb_rem_intrs(igb);
6645779Sxy150489 	}
6655779Sxy150489 
6665779Sxy150489 	/*
6675779Sxy150489 	 * Remove driver properties
6685779Sxy150489 	 */
6695779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
6705779Sxy150489 		(void) ddi_prop_remove_all(devinfo);
6715779Sxy150489 	}
6725779Sxy150489 
6735779Sxy150489 	/*
6745779Sxy150489 	 * Release the DMA resources of rx/tx rings
6755779Sxy150489 	 */
6765779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_RINGS) {
6775779Sxy150489 		igb_fini_rings(igb);
6785779Sxy150489 	}
6795779Sxy150489 
6805779Sxy150489 	/*
6815779Sxy150489 	 * Stop the chipset
6825779Sxy150489 	 */
6835779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT) {
6845779Sxy150489 		mutex_enter(&igb->gen_lock);
6855779Sxy150489 		igb_chip_stop(igb);
6865779Sxy150489 		mutex_exit(&igb->gen_lock);
6876624Sgl147354 		if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
6886624Sgl147354 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_UNAFFECTED);
6895779Sxy150489 	}
6905779Sxy150489 
6915779Sxy150489 	/*
6925779Sxy150489 	 * Free register handle
6935779Sxy150489 	 */
6945779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
6955779Sxy150489 		if (igb->osdep.reg_handle != NULL)
6965779Sxy150489 			ddi_regs_map_free(&igb->osdep.reg_handle);
6975779Sxy150489 	}
6985779Sxy150489 
6995779Sxy150489 	/*
7005779Sxy150489 	 * Free PCI config handle
7015779Sxy150489 	 */
7025779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
7035779Sxy150489 		if (igb->osdep.cfg_handle != NULL)
7045779Sxy150489 			pci_config_teardown(&igb->osdep.cfg_handle);
7055779Sxy150489 	}
7065779Sxy150489 
7075779Sxy150489 	/*
7085779Sxy150489 	 * Free locks
7095779Sxy150489 	 */
7105779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
7115779Sxy150489 		igb_destroy_locks(igb);
7125779Sxy150489 	}
7135779Sxy150489 
7145779Sxy150489 	/*
7155779Sxy150489 	 * Free the rx/tx rings
7165779Sxy150489 	 */
7175779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
7185779Sxy150489 		igb_free_rings(igb);
7195779Sxy150489 	}
7205779Sxy150489 
7215779Sxy150489 	/*
7226624Sgl147354 	 * Remove FMA
7236624Sgl147354 	 */
7246624Sgl147354 	if (igb->attach_progress & ATTACH_PROGRESS_FMINIT) {
7256624Sgl147354 		igb_fm_fini(igb);
7266624Sgl147354 	}
7276624Sgl147354 
7286624Sgl147354 	/*
7295779Sxy150489 	 * Free the driver data structure
7305779Sxy150489 	 */
7315779Sxy150489 	kmem_free(igb, sizeof (igb_t));
7325779Sxy150489 
7335779Sxy150489 	ddi_set_driver_private(devinfo, NULL);
7345779Sxy150489 }
7355779Sxy150489 
7365779Sxy150489 /*
7375779Sxy150489  * igb_register_mac - Register the driver and its function pointers with
7385779Sxy150489  * the GLD interface
7395779Sxy150489  */
7405779Sxy150489 static int
7415779Sxy150489 igb_register_mac(igb_t *igb)
7425779Sxy150489 {
7435779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7445779Sxy150489 	mac_register_t *mac;
7455779Sxy150489 	int status;
7465779Sxy150489 
7475779Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
7485779Sxy150489 		return (IGB_FAILURE);
7495779Sxy150489 
7505779Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
7515779Sxy150489 	mac->m_driver = igb;
7525779Sxy150489 	mac->m_dip = igb->dip;
7535779Sxy150489 	mac->m_src_addr = hw->mac.addr;
7545779Sxy150489 	mac->m_callbacks = &igb_m_callbacks;
7555779Sxy150489 	mac->m_min_sdu = 0;
7565779Sxy150489 	mac->m_max_sdu = igb->max_frame_size -
7575779Sxy150489 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
7585895Syz147064 	mac->m_margin = VLAN_TAGSZ;
7598275SEric Cheng 	mac->m_v12n = MAC_VIRT_LEVEL1;
7605779Sxy150489 
7615779Sxy150489 	status = mac_register(mac, &igb->mac_hdl);
7625779Sxy150489 
7635779Sxy150489 	mac_free(mac);
7645779Sxy150489 
7655779Sxy150489 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
7665779Sxy150489 }
7675779Sxy150489 
7685779Sxy150489 /*
7695779Sxy150489  * igb_identify_hardware - Identify the type of the chipset
7705779Sxy150489  */
7715779Sxy150489 static int
7725779Sxy150489 igb_identify_hardware(igb_t *igb)
7735779Sxy150489 {
7745779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7755779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
7765779Sxy150489 
7775779Sxy150489 	/*
7785779Sxy150489 	 * Get the device id
7795779Sxy150489 	 */
7805779Sxy150489 	hw->vendor_id =
7815779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
7825779Sxy150489 	hw->device_id =
7835779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
7845779Sxy150489 	hw->revision_id =
7855779Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
7865779Sxy150489 	hw->subsystem_device_id =
7875779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
7885779Sxy150489 	hw->subsystem_vendor_id =
7895779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
7905779Sxy150489 
7915779Sxy150489 	/*
7925779Sxy150489 	 * Set the mac type of the adapter based on the device id
7935779Sxy150489 	 */
7945779Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
7955779Sxy150489 		return (IGB_FAILURE);
7965779Sxy150489 	}
7975779Sxy150489 
798*8571SChenlu.Chen@Sun.COM 	/*
799*8571SChenlu.Chen@Sun.COM 	 * Install adapter capabilities based on mac type
800*8571SChenlu.Chen@Sun.COM 	 */
801*8571SChenlu.Chen@Sun.COM 	switch (hw->mac.type) {
802*8571SChenlu.Chen@Sun.COM 	case e1000_82575:
803*8571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82575_cap;
804*8571SChenlu.Chen@Sun.COM 		break;
805*8571SChenlu.Chen@Sun.COM 	case e1000_82576:
806*8571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82576_cap;
807*8571SChenlu.Chen@Sun.COM 		break;
808*8571SChenlu.Chen@Sun.COM 	default:
809*8571SChenlu.Chen@Sun.COM 		return (IGB_FAILURE);
810*8571SChenlu.Chen@Sun.COM 	}
811*8571SChenlu.Chen@Sun.COM 
8125779Sxy150489 	return (IGB_SUCCESS);
8135779Sxy150489 }
8145779Sxy150489 
8155779Sxy150489 /*
8165779Sxy150489  * igb_regs_map - Map the device registers
8175779Sxy150489  */
8185779Sxy150489 static int
8195779Sxy150489 igb_regs_map(igb_t *igb)
8205779Sxy150489 {
8215779Sxy150489 	dev_info_t *devinfo = igb->dip;
8225779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8235779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
8245779Sxy150489 	off_t mem_size;
8255779Sxy150489 
8265779Sxy150489 	/*
8275779Sxy150489 	 * First get the size of device registers to be mapped.
8285779Sxy150489 	 */
829*8571SChenlu.Chen@Sun.COM 	if (ddi_dev_regsize(devinfo, IGB_ADAPTER_REGSET, &mem_size) !=
830*8571SChenlu.Chen@Sun.COM 	    DDI_SUCCESS) {
8315779Sxy150489 		return (IGB_FAILURE);
8325779Sxy150489 	}
8335779Sxy150489 
8345779Sxy150489 	/*
8355779Sxy150489 	 * Call ddi_regs_map_setup() to map registers
8365779Sxy150489 	 */
837*8571SChenlu.Chen@Sun.COM 	if ((ddi_regs_map_setup(devinfo, IGB_ADAPTER_REGSET,
8385779Sxy150489 	    (caddr_t *)&hw->hw_addr, 0,
8395779Sxy150489 	    mem_size, &igb_regs_acc_attr,
8405779Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
8415779Sxy150489 		return (IGB_FAILURE);
8425779Sxy150489 	}
8435779Sxy150489 
8445779Sxy150489 	return (IGB_SUCCESS);
8455779Sxy150489 }
8465779Sxy150489 
8475779Sxy150489 /*
8485779Sxy150489  * igb_init_properties - Initialize driver properties
8495779Sxy150489  */
8505779Sxy150489 static void
8515779Sxy150489 igb_init_properties(igb_t *igb)
8525779Sxy150489 {
8535779Sxy150489 	/*
8545779Sxy150489 	 * Get conf file properties, including link settings
8555779Sxy150489 	 * jumbo frames, ring number, descriptor number, etc.
8565779Sxy150489 	 */
8575779Sxy150489 	igb_get_conf(igb);
8585779Sxy150489 }
8595779Sxy150489 
8605779Sxy150489 /*
8615779Sxy150489  * igb_init_driver_settings - Initialize driver settings
8625779Sxy150489  *
8635779Sxy150489  * The settings include hardware function pointers, bus information,
8645779Sxy150489  * rx/tx rings settings, link state, and any other parameters that
8655779Sxy150489  * need to be setup during driver initialization.
8665779Sxy150489  */
8675779Sxy150489 static int
8685779Sxy150489 igb_init_driver_settings(igb_t *igb)
8695779Sxy150489 {
8705779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8715779Sxy150489 	igb_rx_ring_t *rx_ring;
8725779Sxy150489 	igb_tx_ring_t *tx_ring;
8735779Sxy150489 	uint32_t rx_size;
8745779Sxy150489 	uint32_t tx_size;
8755779Sxy150489 	int i;
8765779Sxy150489 
8775779Sxy150489 	/*
8785779Sxy150489 	 * Initialize chipset specific hardware function pointers
8795779Sxy150489 	 */
8805779Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
8815779Sxy150489 		return (IGB_FAILURE);
8825779Sxy150489 	}
8835779Sxy150489 
8845779Sxy150489 	/*
8855779Sxy150489 	 * Get bus information
8865779Sxy150489 	 */
8875779Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
8885779Sxy150489 		return (IGB_FAILURE);
8895779Sxy150489 	}
8905779Sxy150489 
8915779Sxy150489 	/*
8925779Sxy150489 	 * Set rx buffer size
8935779Sxy150489 	 * The IP header alignment room is counted in the calculation.
8945779Sxy150489 	 * The rx buffer size is in unit of 1K that is required by the
8955779Sxy150489 	 * chipset hardware.
8965779Sxy150489 	 */
8975779Sxy150489 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
8985779Sxy150489 	igb->rx_buf_size = ((rx_size >> 10) +
8995779Sxy150489 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9005779Sxy150489 
9015779Sxy150489 	/*
9025779Sxy150489 	 * Set tx buffer size
9035779Sxy150489 	 */
9045779Sxy150489 	tx_size = igb->max_frame_size;
9055779Sxy150489 	igb->tx_buf_size = ((tx_size >> 10) +
9065779Sxy150489 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9075779Sxy150489 
9085779Sxy150489 	/*
9095779Sxy150489 	 * Initialize rx/tx rings parameters
9105779Sxy150489 	 */
9115779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9125779Sxy150489 		rx_ring = &igb->rx_rings[i];
9135779Sxy150489 		rx_ring->index = i;
9145779Sxy150489 		rx_ring->igb = igb;
9155779Sxy150489 
9165779Sxy150489 		rx_ring->ring_size = igb->rx_ring_size;
9175779Sxy150489 		rx_ring->free_list_size = igb->rx_ring_size;
9185779Sxy150489 		rx_ring->copy_thresh = igb->rx_copy_thresh;
9195779Sxy150489 		rx_ring->limit_per_intr = igb->rx_limit_per_intr;
9205779Sxy150489 	}
9215779Sxy150489 
9225779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9235779Sxy150489 		tx_ring = &igb->tx_rings[i];
9245779Sxy150489 		tx_ring->index = i;
9255779Sxy150489 		tx_ring->igb = igb;
9265779Sxy150489 		if (igb->tx_head_wb_enable)
9275779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
9285779Sxy150489 		else
9295779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
9305779Sxy150489 
9315779Sxy150489 		tx_ring->ring_size = igb->tx_ring_size;
9325779Sxy150489 		tx_ring->free_list_size = igb->tx_ring_size +
9335779Sxy150489 		    (igb->tx_ring_size >> 1);
9345779Sxy150489 		tx_ring->copy_thresh = igb->tx_copy_thresh;
9355779Sxy150489 		tx_ring->recycle_thresh = igb->tx_recycle_thresh;
9365779Sxy150489 		tx_ring->overload_thresh = igb->tx_overload_thresh;
9375779Sxy150489 		tx_ring->resched_thresh = igb->tx_resched_thresh;
9385779Sxy150489 	}
9395779Sxy150489 
9405779Sxy150489 	/*
941*8571SChenlu.Chen@Sun.COM 	 * Initialize values of interrupt throttling rates
9425779Sxy150489 	 */
9435779Sxy150489 	for (i = 1; i < MAX_NUM_EITR; i++)
9445779Sxy150489 		igb->intr_throttling[i] = igb->intr_throttling[0];
9455779Sxy150489 
9465779Sxy150489 	/*
9475779Sxy150489 	 * The initial link state should be "unknown"
9485779Sxy150489 	 */
9495779Sxy150489 	igb->link_state = LINK_STATE_UNKNOWN;
9505779Sxy150489 
9515779Sxy150489 	return (IGB_SUCCESS);
9525779Sxy150489 }
9535779Sxy150489 
9545779Sxy150489 /*
9555779Sxy150489  * igb_init_locks - Initialize locks
9565779Sxy150489  */
9575779Sxy150489 static void
9585779Sxy150489 igb_init_locks(igb_t *igb)
9595779Sxy150489 {
9605779Sxy150489 	igb_rx_ring_t *rx_ring;
9615779Sxy150489 	igb_tx_ring_t *tx_ring;
9625779Sxy150489 	int i;
9635779Sxy150489 
9645779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9655779Sxy150489 		rx_ring = &igb->rx_rings[i];
9665779Sxy150489 		mutex_init(&rx_ring->rx_lock, NULL,
9675779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9685779Sxy150489 		mutex_init(&rx_ring->recycle_lock, NULL,
9695779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9705779Sxy150489 	}
9715779Sxy150489 
9725779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9735779Sxy150489 		tx_ring = &igb->tx_rings[i];
9745779Sxy150489 		mutex_init(&tx_ring->tx_lock, NULL,
9755779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9765779Sxy150489 		mutex_init(&tx_ring->recycle_lock, NULL,
9775779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9785779Sxy150489 		mutex_init(&tx_ring->tcb_head_lock, NULL,
9795779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9805779Sxy150489 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
9815779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9825779Sxy150489 	}
9835779Sxy150489 
9845779Sxy150489 	mutex_init(&igb->gen_lock, NULL,
9855779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9865779Sxy150489 
9875779Sxy150489 	mutex_init(&igb->watchdog_lock, NULL,
9885779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9895779Sxy150489 }
9905779Sxy150489 
9915779Sxy150489 /*
9925779Sxy150489  * igb_destroy_locks - Destroy locks
9935779Sxy150489  */
9945779Sxy150489 static void
9955779Sxy150489 igb_destroy_locks(igb_t *igb)
9965779Sxy150489 {
9975779Sxy150489 	igb_rx_ring_t *rx_ring;
9985779Sxy150489 	igb_tx_ring_t *tx_ring;
9995779Sxy150489 	int i;
10005779Sxy150489 
10015779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
10025779Sxy150489 		rx_ring = &igb->rx_rings[i];
10035779Sxy150489 		mutex_destroy(&rx_ring->rx_lock);
10045779Sxy150489 		mutex_destroy(&rx_ring->recycle_lock);
10055779Sxy150489 	}
10065779Sxy150489 
10075779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
10085779Sxy150489 		tx_ring = &igb->tx_rings[i];
10095779Sxy150489 		mutex_destroy(&tx_ring->tx_lock);
10105779Sxy150489 		mutex_destroy(&tx_ring->recycle_lock);
10115779Sxy150489 		mutex_destroy(&tx_ring->tcb_head_lock);
10125779Sxy150489 		mutex_destroy(&tx_ring->tcb_tail_lock);
10135779Sxy150489 	}
10145779Sxy150489 
10155779Sxy150489 	mutex_destroy(&igb->gen_lock);
10165779Sxy150489 	mutex_destroy(&igb->watchdog_lock);
10175779Sxy150489 }
10185779Sxy150489 
10195779Sxy150489 static int
10205779Sxy150489 igb_resume(dev_info_t *devinfo)
10215779Sxy150489 {
10225779Sxy150489 	igb_t *igb;
10235779Sxy150489 
10245779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10255779Sxy150489 	if (igb == NULL)
10265779Sxy150489 		return (DDI_FAILURE);
10275779Sxy150489 
10285779Sxy150489 	mutex_enter(&igb->gen_lock);
10295779Sxy150489 
10305779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
10315779Sxy150489 		if (igb_start(igb) != IGB_SUCCESS) {
10325779Sxy150489 			mutex_exit(&igb->gen_lock);
10335779Sxy150489 			return (DDI_FAILURE);
10345779Sxy150489 		}
10355779Sxy150489 
10365779Sxy150489 		/*
10375779Sxy150489 		 * Enable and start the watchdog timer
10385779Sxy150489 		 */
10395779Sxy150489 		igb_enable_watchdog_timer(igb);
10405779Sxy150489 	}
10415779Sxy150489 
10425779Sxy150489 	igb->igb_state &= ~IGB_SUSPENDED;
10435779Sxy150489 
10445779Sxy150489 	mutex_exit(&igb->gen_lock);
10455779Sxy150489 
10465779Sxy150489 	return (DDI_SUCCESS);
10475779Sxy150489 }
10485779Sxy150489 
10495779Sxy150489 static int
10505779Sxy150489 igb_suspend(dev_info_t *devinfo)
10515779Sxy150489 {
10525779Sxy150489 	igb_t *igb;
10535779Sxy150489 
10545779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10555779Sxy150489 	if (igb == NULL)
10565779Sxy150489 		return (DDI_FAILURE);
10575779Sxy150489 
10585779Sxy150489 	mutex_enter(&igb->gen_lock);
10595779Sxy150489 
10605779Sxy150489 	igb->igb_state |= IGB_SUSPENDED;
10615779Sxy150489 
10625779Sxy150489 	igb_stop(igb);
10635779Sxy150489 
10645779Sxy150489 	mutex_exit(&igb->gen_lock);
10655779Sxy150489 
10665779Sxy150489 	/*
10675779Sxy150489 	 * Disable and stop the watchdog timer
10685779Sxy150489 	 */
10695779Sxy150489 	igb_disable_watchdog_timer(igb);
10705779Sxy150489 
10715779Sxy150489 	return (DDI_SUCCESS);
10725779Sxy150489 }
10735779Sxy150489 
10745779Sxy150489 /*
10755779Sxy150489  * igb_init - Initialize the device
10765779Sxy150489  */
10775779Sxy150489 static int
10785779Sxy150489 igb_init(igb_t *igb)
10795779Sxy150489 {
10805779Sxy150489 	struct e1000_hw *hw = &igb->hw;
10815779Sxy150489 	uint32_t pba;
10825779Sxy150489 	uint32_t high_water;
10835779Sxy150489 
10848275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
10855779Sxy150489 
10865779Sxy150489 	/*
10875779Sxy150489 	 * Reset chipset to put the hardware in a known state
10885779Sxy150489 	 * before we try to do anything with the eeprom
10895779Sxy150489 	 */
10906624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
10916624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
10926624Sgl147354 		goto init_fail;
10936624Sgl147354 	}
10945779Sxy150489 
10955779Sxy150489 	/*
10965779Sxy150489 	 * NVM validation
10975779Sxy150489 	 */
10985779Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
10995779Sxy150489 		/*
11005779Sxy150489 		 * Some PCI-E parts fail the first check due to
11015779Sxy150489 		 * the link being in sleep state.  Call it again,
11025779Sxy150489 		 * if it fails a second time its a real issue.
11035779Sxy150489 		 */
11045779Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
11055779Sxy150489 			igb_error(igb,
11065779Sxy150489 			    "Invalid NVM checksum. Please contact "
11075779Sxy150489 			    "the vendor to update the NVM.");
11086624Sgl147354 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
11095779Sxy150489 			goto init_fail;
11105779Sxy150489 		}
11115779Sxy150489 	}
11125779Sxy150489 
11135779Sxy150489 	/*
11145779Sxy150489 	 * Setup flow control
11155779Sxy150489 	 *
11165779Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
11175779Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
11185779Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
11195779Sxy150489 	 * file.
11205779Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
11215779Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
11225779Sxy150489 	 * high-water mark.
11235779Sxy150489 	 * The high water mark must be low enough to fit one full frame above
11245779Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
11255779Sxy150489 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
11265779Sxy150489 	 * frame.
11275779Sxy150489 	 */
1128*8571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
1129*8571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_34K;
1130*8571SChenlu.Chen@Sun.COM 	} else {
1131*8571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_64K;
1132*8571SChenlu.Chen@Sun.COM 	}
1133*8571SChenlu.Chen@Sun.COM 
11345779Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
11355779Sxy150489 	    ((pba << 10) - igb->max_frame_size));
11365779Sxy150489 
1137*8571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
1138*8571SChenlu.Chen@Sun.COM 		/* 8-byte granularity */
1139*8571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF8;
1140*8571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 8;
1141*8571SChenlu.Chen@Sun.COM 	} else {
1142*8571SChenlu.Chen@Sun.COM 		/* 16-byte granularity */
1143*8571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF0;
1144*8571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 16;
1145*8571SChenlu.Chen@Sun.COM 	}
1146*8571SChenlu.Chen@Sun.COM 
11475779Sxy150489 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
11485779Sxy150489 	hw->fc.send_xon = B_TRUE;
11495779Sxy150489 
11505779Sxy150489 	/*
11515779Sxy150489 	 * Reset the chipset hardware the second time to validate
11525779Sxy150489 	 * the PBA setting.
11535779Sxy150489 	 */
11546624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
11556624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
11566624Sgl147354 		goto init_fail;
11576624Sgl147354 	}
11585779Sxy150489 
11595779Sxy150489 	/*
11605779Sxy150489 	 * Don't wait for auto-negotiation to complete
11615779Sxy150489 	 */
11625779Sxy150489 	hw->phy.autoneg_wait_to_complete = B_FALSE;
11635779Sxy150489 
11645779Sxy150489 	/*
11655779Sxy150489 	 * Copper options
11665779Sxy150489 	 */
11675779Sxy150489 	if (hw->phy.media_type == e1000_media_type_copper) {
11685779Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
11695779Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
11705779Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
11715779Sxy150489 	}
11725779Sxy150489 
11735779Sxy150489 	/*
11745779Sxy150489 	 * Initialize link settings
11755779Sxy150489 	 */
11765779Sxy150489 	(void) igb_setup_link(igb, B_FALSE);
11775779Sxy150489 
11785779Sxy150489 	/*
11795779Sxy150489 	 * Initialize the chipset hardware
11805779Sxy150489 	 */
11815779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
11826624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
11836624Sgl147354 		goto init_fail;
11846624Sgl147354 	}
11856624Sgl147354 
11866624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
11876624Sgl147354 		goto init_fail;
11886624Sgl147354 	}
11896624Sgl147354 
11906624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
11915779Sxy150489 		goto init_fail;
11925779Sxy150489 	}
11935779Sxy150489 
11945779Sxy150489 	return (IGB_SUCCESS);
11955779Sxy150489 
11965779Sxy150489 init_fail:
11975779Sxy150489 	/*
11985779Sxy150489 	 * Reset PHY if possible
11995779Sxy150489 	 */
12005779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
12015779Sxy150489 		(void) e1000_phy_hw_reset(hw);
12025779Sxy150489 
12036624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
12046624Sgl147354 
12055779Sxy150489 	return (IGB_FAILURE);
12065779Sxy150489 }
12075779Sxy150489 
12085779Sxy150489 /*
12095779Sxy150489  * igb_init_rings - Allocate DMA resources for all rx/tx rings and
12105779Sxy150489  * initialize relevant hardware settings.
12115779Sxy150489  */
12125779Sxy150489 static int
12135779Sxy150489 igb_init_rings(igb_t *igb)
12145779Sxy150489 {
12155779Sxy150489 	int i;
12165779Sxy150489 
12175779Sxy150489 	/*
12185779Sxy150489 	 * Allocate buffers for all the rx/tx rings
12195779Sxy150489 	 */
12205779Sxy150489 	if (igb_alloc_dma(igb) != IGB_SUCCESS)
12215779Sxy150489 		return (IGB_FAILURE);
12225779Sxy150489 
12235779Sxy150489 	/*
12245779Sxy150489 	 * Setup the rx/tx rings
12255779Sxy150489 	 */
12265779Sxy150489 	mutex_enter(&igb->gen_lock);
12275779Sxy150489 
12285779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
12295779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
12305779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
12315779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
12325779Sxy150489 
12335779Sxy150489 	igb_setup_rings(igb);
12345779Sxy150489 
12355779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
12365779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
12375779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
12385779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
12395779Sxy150489 
12405779Sxy150489 	mutex_exit(&igb->gen_lock);
12415779Sxy150489 
12425779Sxy150489 	return (IGB_SUCCESS);
12435779Sxy150489 }
12445779Sxy150489 
12455779Sxy150489 /*
12465779Sxy150489  * igb_fini_rings - Release DMA resources of all rx/tx rings
12475779Sxy150489  */
12485779Sxy150489 static void
12495779Sxy150489 igb_fini_rings(igb_t *igb)
12505779Sxy150489 {
12515779Sxy150489 	/*
12525779Sxy150489 	 * Release the DMA/memory resources of rx/tx rings
12535779Sxy150489 	 */
12545779Sxy150489 	igb_free_dma(igb);
12555779Sxy150489 }
12565779Sxy150489 
12575779Sxy150489 /*
12585779Sxy150489  * igb_chip_start - Initialize and start the chipset hardware
12595779Sxy150489  */
12605779Sxy150489 static int
12615779Sxy150489 igb_chip_start(igb_t *igb)
12625779Sxy150489 {
12635779Sxy150489 	struct e1000_hw *hw = &igb->hw;
12645779Sxy150489 	int i;
12655779Sxy150489 
12665779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
12675779Sxy150489 
12685779Sxy150489 	/*
12695779Sxy150489 	 * Get the mac address
12705779Sxy150489 	 * This function should handle SPARC case correctly.
12715779Sxy150489 	 */
12725779Sxy150489 	if (!igb_find_mac_address(igb)) {
12735779Sxy150489 		igb_error(igb, "Failed to get the mac address");
12745779Sxy150489 		return (IGB_FAILURE);
12755779Sxy150489 	}
12765779Sxy150489 
12775779Sxy150489 	/* Validate mac address */
12785779Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
12795779Sxy150489 		igb_error(igb, "Invalid mac address");
12805779Sxy150489 		return (IGB_FAILURE);
12815779Sxy150489 	}
12825779Sxy150489 
12835779Sxy150489 	/* Disable wakeup control by default */
12845779Sxy150489 	E1000_WRITE_REG(hw, E1000_WUC, 0);
12855779Sxy150489 
12865779Sxy150489 	/*
12875779Sxy150489 	 * Configure/Initialize hardware
12885779Sxy150489 	 */
12895779Sxy150489 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
12905779Sxy150489 		igb_error(igb, "Failed to initialize hardware");
12915779Sxy150489 		return (IGB_FAILURE);
12925779Sxy150489 	}
12935779Sxy150489 
12945779Sxy150489 	/*
12955779Sxy150489 	 * Make sure driver has control
12965779Sxy150489 	 */
12975779Sxy150489 	igb_get_driver_control(hw);
12985779Sxy150489 
12995779Sxy150489 	/*
13005779Sxy150489 	 * Setup MSI-X interrupts
13015779Sxy150489 	 */
13025779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
1303*8571SChenlu.Chen@Sun.COM 		igb->capab->setup_msix(igb);
13045779Sxy150489 
13055779Sxy150489 	/*
13065779Sxy150489 	 * Initialize unicast addresses.
13075779Sxy150489 	 */
13085779Sxy150489 	igb_init_unicst(igb);
13095779Sxy150489 
13105779Sxy150489 	/*
13115779Sxy150489 	 * Setup and initialize the mctable structures.
13125779Sxy150489 	 */
13135779Sxy150489 	igb_setup_multicst(igb);
13145779Sxy150489 
13155779Sxy150489 	/*
13165779Sxy150489 	 * Set interrupt throttling rate
13175779Sxy150489 	 */
13185779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++)
13195779Sxy150489 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
13205779Sxy150489 
13215779Sxy150489 	/* Enable PCI-E master */
13225779Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
13235779Sxy150489 		e1000_enable_pciex_master(hw);
13245779Sxy150489 	}
13255779Sxy150489 
13265779Sxy150489 	/*
13275779Sxy150489 	 * Save the state of the phy
13285779Sxy150489 	 */
13295779Sxy150489 	igb_get_phy_state(igb);
13305779Sxy150489 
13315779Sxy150489 	return (IGB_SUCCESS);
13325779Sxy150489 }
13335779Sxy150489 
13345779Sxy150489 /*
13355779Sxy150489  * igb_chip_stop - Stop the chipset hardware
13365779Sxy150489  */
13375779Sxy150489 static void
13385779Sxy150489 igb_chip_stop(igb_t *igb)
13395779Sxy150489 {
13405779Sxy150489 	struct e1000_hw *hw = &igb->hw;
13415779Sxy150489 
13425779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
13435779Sxy150489 
13445779Sxy150489 	/* Tell firmware driver is no longer in control */
13455779Sxy150489 	igb_release_driver_control(hw);
13465779Sxy150489 
13475779Sxy150489 	/*
13485779Sxy150489 	 * Reset the chipset
13495779Sxy150489 	 */
13506624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
13516624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
13526624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
13536624Sgl147354 	}
13545779Sxy150489 
13555779Sxy150489 	/*
13565779Sxy150489 	 * Reset PHY if possible
13575779Sxy150489 	 */
13585779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
13595779Sxy150489 		(void) e1000_phy_hw_reset(hw);
13605779Sxy150489 }
13615779Sxy150489 
13625779Sxy150489 /*
13635779Sxy150489  * igb_reset - Reset the chipset and restart the driver.
13645779Sxy150489  *
13655779Sxy150489  * It involves stopping and re-starting the chipset,
13665779Sxy150489  * and re-configuring the rx/tx rings.
13675779Sxy150489  */
13685779Sxy150489 static int
13695779Sxy150489 igb_reset(igb_t *igb)
13705779Sxy150489 {
13715779Sxy150489 	int i;
13725779Sxy150489 
13735779Sxy150489 	mutex_enter(&igb->gen_lock);
13745779Sxy150489 
13755779Sxy150489 	ASSERT(igb->igb_state & IGB_STARTED);
13765779Sxy150489 
13775779Sxy150489 	/*
13785779Sxy150489 	 * Disable the adapter interrupts to stop any rx/tx activities
13795779Sxy150489 	 * before draining pending data and resetting hardware.
13805779Sxy150489 	 */
13815779Sxy150489 	igb_disable_adapter_interrupts(igb);
13825779Sxy150489 
13835779Sxy150489 	/*
13845779Sxy150489 	 * Drain the pending transmit packets
13855779Sxy150489 	 */
13865779Sxy150489 	(void) igb_tx_drain(igb);
13875779Sxy150489 
13885779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
13895779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
13905779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
13915779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
13925779Sxy150489 
13935779Sxy150489 	/*
13945779Sxy150489 	 * Stop the chipset hardware
13955779Sxy150489 	 */
13965779Sxy150489 	igb_chip_stop(igb);
13975779Sxy150489 
13985779Sxy150489 	/*
13995779Sxy150489 	 * Clean the pending tx data/resources
14005779Sxy150489 	 */
14015779Sxy150489 	igb_tx_clean(igb);
14025779Sxy150489 
14035779Sxy150489 	/*
14045779Sxy150489 	 * Start the chipset hardware
14055779Sxy150489 	 */
14065779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
14076624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
14085779Sxy150489 		goto reset_failure;
14095779Sxy150489 	}
14105779Sxy150489 
14115779Sxy150489 	/*
14125779Sxy150489 	 * Setup the rx/tx rings
14135779Sxy150489 	 */
14145779Sxy150489 	igb_setup_rings(igb);
14155779Sxy150489 
14165779Sxy150489 	/*
14175779Sxy150489 	 * Enable adapter interrupts
14185779Sxy150489 	 * The interrupts must be enabled after the driver state is START
14195779Sxy150489 	 */
1420*8571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
14215779Sxy150489 
14226624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
14236624Sgl147354 		goto reset_failure;
14246624Sgl147354 
14256624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
14266624Sgl147354 		goto reset_failure;
14276624Sgl147354 
14285779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14295779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14305779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14315779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14325779Sxy150489 
14335779Sxy150489 	mutex_exit(&igb->gen_lock);
14345779Sxy150489 
14355779Sxy150489 	return (IGB_SUCCESS);
14365779Sxy150489 
14375779Sxy150489 reset_failure:
14385779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14395779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14405779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14415779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14425779Sxy150489 
14435779Sxy150489 	mutex_exit(&igb->gen_lock);
14445779Sxy150489 
14456624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
14466624Sgl147354 
14475779Sxy150489 	return (IGB_FAILURE);
14485779Sxy150489 }
14495779Sxy150489 
14505779Sxy150489 /*
14515779Sxy150489  * igb_tx_clean - Clean the pending transmit packets and DMA resources
14525779Sxy150489  */
14535779Sxy150489 static void
14545779Sxy150489 igb_tx_clean(igb_t *igb)
14555779Sxy150489 {
14565779Sxy150489 	igb_tx_ring_t *tx_ring;
14575779Sxy150489 	tx_control_block_t *tcb;
14585779Sxy150489 	link_list_t pending_list;
14595779Sxy150489 	uint32_t desc_num;
14605779Sxy150489 	int i, j;
14615779Sxy150489 
14625779Sxy150489 	LINK_LIST_INIT(&pending_list);
14635779Sxy150489 
14645779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
14655779Sxy150489 		tx_ring = &igb->tx_rings[i];
14665779Sxy150489 
14675779Sxy150489 		mutex_enter(&tx_ring->recycle_lock);
14685779Sxy150489 
14695779Sxy150489 		/*
14705779Sxy150489 		 * Clean the pending tx data - the pending packets in the
14715779Sxy150489 		 * work_list that have no chances to be transmitted again.
14725779Sxy150489 		 *
14735779Sxy150489 		 * We must ensure the chipset is stopped or the link is down
14745779Sxy150489 		 * before cleaning the transmit packets.
14755779Sxy150489 		 */
14765779Sxy150489 		desc_num = 0;
14775779Sxy150489 		for (j = 0; j < tx_ring->ring_size; j++) {
14785779Sxy150489 			tcb = tx_ring->work_list[j];
14795779Sxy150489 			if (tcb != NULL) {
14805779Sxy150489 				desc_num += tcb->desc_num;
14815779Sxy150489 
14825779Sxy150489 				tx_ring->work_list[j] = NULL;
14835779Sxy150489 
14845779Sxy150489 				igb_free_tcb(tcb);
14855779Sxy150489 
14865779Sxy150489 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
14875779Sxy150489 			}
14885779Sxy150489 		}
14895779Sxy150489 
14905779Sxy150489 		if (desc_num > 0) {
14915779Sxy150489 			atomic_add_32(&tx_ring->tbd_free, desc_num);
14925779Sxy150489 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
14935779Sxy150489 
14945779Sxy150489 			/*
14957072Sxy150489 			 * Reset the head and tail pointers of the tbd ring;
14967072Sxy150489 			 * Reset the head write-back if it is enabled.
14975779Sxy150489 			 */
14985779Sxy150489 			tx_ring->tbd_head = 0;
14995779Sxy150489 			tx_ring->tbd_tail = 0;
15007072Sxy150489 			if (igb->tx_head_wb_enable)
15017072Sxy150489 				*tx_ring->tbd_head_wb = 0;
15025779Sxy150489 
15035779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
15045779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
15055779Sxy150489 		}
15065779Sxy150489 
15075779Sxy150489 		mutex_exit(&tx_ring->recycle_lock);
15085779Sxy150489 
15095779Sxy150489 		/*
15105779Sxy150489 		 * Add the tx control blocks in the pending list to
15115779Sxy150489 		 * the free list.
15125779Sxy150489 		 */
15135779Sxy150489 		igb_put_free_list(tx_ring, &pending_list);
15145779Sxy150489 	}
15155779Sxy150489 }
15165779Sxy150489 
15175779Sxy150489 /*
15185779Sxy150489  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
15195779Sxy150489  */
15205779Sxy150489 static boolean_t
15215779Sxy150489 igb_tx_drain(igb_t *igb)
15225779Sxy150489 {
15235779Sxy150489 	igb_tx_ring_t *tx_ring;
15245779Sxy150489 	boolean_t done;
15255779Sxy150489 	int i, j;
15265779Sxy150489 
15275779Sxy150489 	/*
15285779Sxy150489 	 * Wait for a specific time to allow pending tx packets
15295779Sxy150489 	 * to be transmitted.
15305779Sxy150489 	 *
15315779Sxy150489 	 * Check the counter tbd_free to see if transmission is done.
15325779Sxy150489 	 * No lock protection is needed here.
15335779Sxy150489 	 *
15345779Sxy150489 	 * Return B_TRUE if all pending packets have been transmitted;
15355779Sxy150489 	 * Otherwise return B_FALSE;
15365779Sxy150489 	 */
15375779Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
15385779Sxy150489 
15395779Sxy150489 		done = B_TRUE;
15405779Sxy150489 		for (j = 0; j < igb->num_tx_rings; j++) {
15415779Sxy150489 			tx_ring = &igb->tx_rings[j];
15425779Sxy150489 			done = done &&
15435779Sxy150489 			    (tx_ring->tbd_free == tx_ring->ring_size);
15445779Sxy150489 		}
15455779Sxy150489 
15465779Sxy150489 		if (done)
15475779Sxy150489 			break;
15485779Sxy150489 
15495779Sxy150489 		msec_delay(1);
15505779Sxy150489 	}
15515779Sxy150489 
15525779Sxy150489 	return (done);
15535779Sxy150489 }
15545779Sxy150489 
15555779Sxy150489 /*
15565779Sxy150489  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
15575779Sxy150489  */
15585779Sxy150489 static boolean_t
15595779Sxy150489 igb_rx_drain(igb_t *igb)
15605779Sxy150489 {
15615779Sxy150489 	igb_rx_ring_t *rx_ring;
15625779Sxy150489 	boolean_t done;
15635779Sxy150489 	int i, j;
15645779Sxy150489 
15655779Sxy150489 	/*
15665779Sxy150489 	 * Polling the rx free list to check if those rx buffers held by
15675779Sxy150489 	 * the upper layer are released.
15685779Sxy150489 	 *
15695779Sxy150489 	 * Check the counter rcb_free to see if all pending buffers are
15705779Sxy150489 	 * released. No lock protection is needed here.
15715779Sxy150489 	 *
15725779Sxy150489 	 * Return B_TRUE if all pending buffers have been released;
15735779Sxy150489 	 * Otherwise return B_FALSE;
15745779Sxy150489 	 */
15755779Sxy150489 	for (i = 0; i < RX_DRAIN_TIME; i++) {
15765779Sxy150489 
15775779Sxy150489 		done = B_TRUE;
15785779Sxy150489 		for (j = 0; j < igb->num_rx_rings; j++) {
15795779Sxy150489 			rx_ring = &igb->rx_rings[j];
15805779Sxy150489 			done = done &&
15815779Sxy150489 			    (rx_ring->rcb_free == rx_ring->free_list_size);
15825779Sxy150489 		}
15835779Sxy150489 
15845779Sxy150489 		if (done)
15855779Sxy150489 			break;
15865779Sxy150489 
15875779Sxy150489 		msec_delay(1);
15885779Sxy150489 	}
15895779Sxy150489 
15905779Sxy150489 	return (done);
15915779Sxy150489 }
15925779Sxy150489 
15935779Sxy150489 /*
15945779Sxy150489  * igb_start - Start the driver/chipset
15955779Sxy150489  */
15965779Sxy150489 int
15975779Sxy150489 igb_start(igb_t *igb)
15985779Sxy150489 {
15995779Sxy150489 	int i;
16005779Sxy150489 
16015779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16025779Sxy150489 
16035779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
16045779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
16055779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
16065779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
16075779Sxy150489 
16085779Sxy150489 	/*
16095779Sxy150489 	 * Start the chipset hardware
16105779Sxy150489 	 */
16118275SEric Cheng 	if (!(igb->attach_progress & ATTACH_PROGRESS_INIT)) {
16128275SEric Cheng 		if (igb_init(igb) != IGB_SUCCESS) {
16138275SEric Cheng 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
16148275SEric Cheng 			goto start_failure;
16158275SEric Cheng 		}
16168275SEric Cheng 		igb->attach_progress |= ATTACH_PROGRESS_INIT;
16175779Sxy150489 	}
16185779Sxy150489 
16195779Sxy150489 	/*
16205779Sxy150489 	 * Setup the rx/tx rings
16215779Sxy150489 	 */
16225779Sxy150489 	igb_setup_rings(igb);
16235779Sxy150489 
16245779Sxy150489 	/*
16255779Sxy150489 	 * Enable adapter interrupts
16265779Sxy150489 	 * The interrupts must be enabled after the driver state is START
16275779Sxy150489 	 */
1628*8571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
16295779Sxy150489 
16306624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
16316624Sgl147354 		goto start_failure;
16326624Sgl147354 
16336624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
16346624Sgl147354 		goto start_failure;
16356624Sgl147354 
16365779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
16375779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
16385779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
16395779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
16405779Sxy150489 
16415779Sxy150489 	return (IGB_SUCCESS);
16425779Sxy150489 
16435779Sxy150489 start_failure:
16445779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
16455779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
16465779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
16475779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
16485779Sxy150489 
16496624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
16506624Sgl147354 
16515779Sxy150489 	return (IGB_FAILURE);
16525779Sxy150489 }
16535779Sxy150489 
16545779Sxy150489 /*
16555779Sxy150489  * igb_stop - Stop the driver/chipset
16565779Sxy150489  */
16575779Sxy150489 void
16585779Sxy150489 igb_stop(igb_t *igb)
16595779Sxy150489 {
16605779Sxy150489 	int i;
16615779Sxy150489 
16625779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16635779Sxy150489 
16648275SEric Cheng 	igb->attach_progress &= ~ ATTACH_PROGRESS_INIT;
16658275SEric Cheng 
16665779Sxy150489 	/*
16675779Sxy150489 	 * Disable the adapter interrupts
16685779Sxy150489 	 */
16695779Sxy150489 	igb_disable_adapter_interrupts(igb);
16705779Sxy150489 
16715779Sxy150489 	/*
16725779Sxy150489 	 * Drain the pending tx packets
16735779Sxy150489 	 */
16745779Sxy150489 	(void) igb_tx_drain(igb);
16755779Sxy150489 
16765779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
16775779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
16785779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
16795779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
16805779Sxy150489 
16815779Sxy150489 	/*
16825779Sxy150489 	 * Stop the chipset hardware
16835779Sxy150489 	 */
16845779Sxy150489 	igb_chip_stop(igb);
16855779Sxy150489 
16865779Sxy150489 	/*
16875779Sxy150489 	 * Clean the pending tx data/resources
16885779Sxy150489 	 */
16895779Sxy150489 	igb_tx_clean(igb);
16905779Sxy150489 
16915779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
16925779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
16935779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
16945779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
16956624Sgl147354 
16966624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
16976624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
16985779Sxy150489 }
16995779Sxy150489 
17005779Sxy150489 /*
17015779Sxy150489  * igb_alloc_rings - Allocate memory space for rx/tx rings
17025779Sxy150489  */
17035779Sxy150489 static int
17045779Sxy150489 igb_alloc_rings(igb_t *igb)
17055779Sxy150489 {
17065779Sxy150489 	/*
17075779Sxy150489 	 * Allocate memory space for rx rings
17085779Sxy150489 	 */
17095779Sxy150489 	igb->rx_rings = kmem_zalloc(
17105779Sxy150489 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
17115779Sxy150489 	    KM_NOSLEEP);
17125779Sxy150489 
17135779Sxy150489 	if (igb->rx_rings == NULL) {
17145779Sxy150489 		return (IGB_FAILURE);
17155779Sxy150489 	}
17165779Sxy150489 
17175779Sxy150489 	/*
17185779Sxy150489 	 * Allocate memory space for tx rings
17195779Sxy150489 	 */
17205779Sxy150489 	igb->tx_rings = kmem_zalloc(
17215779Sxy150489 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
17225779Sxy150489 	    KM_NOSLEEP);
17235779Sxy150489 
17245779Sxy150489 	if (igb->tx_rings == NULL) {
17255779Sxy150489 		kmem_free(igb->rx_rings,
17265779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17275779Sxy150489 		igb->rx_rings = NULL;
17285779Sxy150489 		return (IGB_FAILURE);
17295779Sxy150489 	}
17305779Sxy150489 
17318275SEric Cheng 	/*
17328275SEric Cheng 	 * Allocate memory space for rx ring groups
17338275SEric Cheng 	 */
17348275SEric Cheng 	igb->rx_groups = kmem_zalloc(
17358275SEric Cheng 	    sizeof (igb_rx_group_t) * igb->num_rx_groups,
17368275SEric Cheng 	    KM_NOSLEEP);
17378275SEric Cheng 
17388275SEric Cheng 	if (igb->rx_groups == NULL) {
17398275SEric Cheng 		kmem_free(igb->rx_rings,
17408275SEric Cheng 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17418275SEric Cheng 		kmem_free(igb->tx_rings,
17428275SEric Cheng 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
17438275SEric Cheng 		igb->rx_rings = NULL;
17448275SEric Cheng 		igb->tx_rings = NULL;
17458275SEric Cheng 		return (IGB_FAILURE);
17468275SEric Cheng 	}
17478275SEric Cheng 
17485779Sxy150489 	return (IGB_SUCCESS);
17495779Sxy150489 }
17505779Sxy150489 
17515779Sxy150489 /*
17525779Sxy150489  * igb_free_rings - Free the memory space of rx/tx rings.
17535779Sxy150489  */
17545779Sxy150489 static void
17555779Sxy150489 igb_free_rings(igb_t *igb)
17565779Sxy150489 {
17575779Sxy150489 	if (igb->rx_rings != NULL) {
17585779Sxy150489 		kmem_free(igb->rx_rings,
17595779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
17605779Sxy150489 		igb->rx_rings = NULL;
17615779Sxy150489 	}
17625779Sxy150489 
17635779Sxy150489 	if (igb->tx_rings != NULL) {
17645779Sxy150489 		kmem_free(igb->tx_rings,
17655779Sxy150489 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
17665779Sxy150489 		igb->tx_rings = NULL;
17675779Sxy150489 	}
17688275SEric Cheng 
17698275SEric Cheng 	if (igb->rx_groups != NULL) {
17708275SEric Cheng 		kmem_free(igb->rx_groups,
17718275SEric Cheng 		    sizeof (igb_rx_group_t) * igb->num_rx_groups);
17728275SEric Cheng 		igb->rx_groups = NULL;
17738275SEric Cheng 	}
17745779Sxy150489 }
17755779Sxy150489 
17765779Sxy150489 /*
17775779Sxy150489  * igb_setup_rings - Setup rx/tx rings
17785779Sxy150489  */
17795779Sxy150489 static void
17805779Sxy150489 igb_setup_rings(igb_t *igb)
17815779Sxy150489 {
17825779Sxy150489 	/*
17835779Sxy150489 	 * Setup the rx/tx rings, including the following:
17845779Sxy150489 	 *
17855779Sxy150489 	 * 1. Setup the descriptor ring and the control block buffers;
17865779Sxy150489 	 * 2. Initialize necessary registers for receive/transmit;
17875779Sxy150489 	 * 3. Initialize software pointers/parameters for receive/transmit;
17885779Sxy150489 	 */
17895779Sxy150489 	igb_setup_rx(igb);
17905779Sxy150489 
17915779Sxy150489 	igb_setup_tx(igb);
17926624Sgl147354 
17936624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
17946624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
17955779Sxy150489 }
17965779Sxy150489 
17975779Sxy150489 static void
17985779Sxy150489 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
17995779Sxy150489 {
18005779Sxy150489 	igb_t *igb = rx_ring->igb;
18015779Sxy150489 	struct e1000_hw *hw = &igb->hw;
18025779Sxy150489 	rx_control_block_t *rcb;
18035779Sxy150489 	union e1000_adv_rx_desc	*rbd;
18045779Sxy150489 	uint32_t size;
18055779Sxy150489 	uint32_t buf_low;
18065779Sxy150489 	uint32_t buf_high;
18075779Sxy150489 	uint32_t reg_val;
18085779Sxy150489 	int i;
18095779Sxy150489 
18105779Sxy150489 	ASSERT(mutex_owned(&rx_ring->rx_lock));
18115779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
18125779Sxy150489 
18135779Sxy150489 	for (i = 0; i < igb->rx_ring_size; i++) {
18145779Sxy150489 		rcb = rx_ring->work_list[i];
18155779Sxy150489 		rbd = &rx_ring->rbd_ring[i];
18165779Sxy150489 
18175779Sxy150489 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
18185779Sxy150489 		rbd->read.hdr_addr = NULL;
18195779Sxy150489 	}
18205779Sxy150489 
18215779Sxy150489 	/*
18225779Sxy150489 	 * Initialize the length register
18235779Sxy150489 	 */
18245779Sxy150489 	size = rx_ring->ring_size * sizeof (union e1000_adv_rx_desc);
18255779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
18265779Sxy150489 
18275779Sxy150489 	/*
18285779Sxy150489 	 * Initialize the base address registers
18295779Sxy150489 	 */
18305779Sxy150489 	buf_low = (uint32_t)rx_ring->rbd_area.dma_address;
18315779Sxy150489 	buf_high = (uint32_t)(rx_ring->rbd_area.dma_address >> 32);
18325779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
18335779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
18345779Sxy150489 
18355779Sxy150489 	/*
18365779Sxy150489 	 * Setup head & tail pointers
18375779Sxy150489 	 */
18385779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDT(rx_ring->index), rx_ring->ring_size - 1);
18395779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDH(rx_ring->index), 0);
18405779Sxy150489 
18415779Sxy150489 	rx_ring->rbd_next = 0;
18425779Sxy150489 
18435779Sxy150489 	/*
18445779Sxy150489 	 * Note: Considering the case that the chipset is being reset
18455779Sxy150489 	 * and there are still some buffers held by the upper layer,
18465779Sxy150489 	 * we should not reset the values of rcb_head, rcb_tail and
18475779Sxy150489 	 * rcb_free;
18485779Sxy150489 	 */
18495779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
18505779Sxy150489 		rx_ring->rcb_head = 0;
18515779Sxy150489 		rx_ring->rcb_tail = 0;
18525779Sxy150489 		rx_ring->rcb_free = rx_ring->free_list_size;
18535779Sxy150489 	}
18545779Sxy150489 
18555779Sxy150489 	/*
18565779Sxy150489 	 * Setup the Receive Descriptor Control Register (RXDCTL)
18575779Sxy150489 	 */
18585779Sxy150489 	reg_val = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
18595779Sxy150489 	reg_val |= E1000_RXDCTL_QUEUE_ENABLE;
18605779Sxy150489 	reg_val &= 0xFFF00000;
18615779Sxy150489 	reg_val |= 16;		/* pthresh */
18625779Sxy150489 	reg_val |= 8 << 8;	/* hthresh */
18635779Sxy150489 	reg_val |= 1 << 16;	/* wthresh */
18645779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), reg_val);
18655779Sxy150489 
18665779Sxy150489 	/*
18675779Sxy150489 	 * Setup the Split and Replication Receive Control Register.
18685779Sxy150489 	 * Set the rx buffer size and the advanced descriptor type.
18695779Sxy150489 	 */
18705779Sxy150489 	reg_val = (igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
18715779Sxy150489 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
18725779Sxy150489 
18735779Sxy150489 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index), reg_val);
18745779Sxy150489 }
18755779Sxy150489 
18765779Sxy150489 static void
18775779Sxy150489 igb_setup_rx(igb_t *igb)
18785779Sxy150489 {
18795779Sxy150489 	igb_rx_ring_t *rx_ring;
18808275SEric Cheng 	igb_rx_group_t *rx_group;
18815779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1882*8571SChenlu.Chen@Sun.COM 	uint32_t reg_val, rctl;
18838275SEric Cheng 	uint32_t ring_per_group;
18845779Sxy150489 	int i;
18855779Sxy150489 
18865779Sxy150489 	/*
18875779Sxy150489 	 * Setup the Receive Control Register (RCTL), and ENABLE the
18885779Sxy150489 	 * receiver. The initial configuration is to: Enable the receiver,
18895779Sxy150489 	 * accept broadcasts, discard bad packets (and long packets),
18905779Sxy150489 	 * disable VLAN filter checking, set the receive descriptor
18915779Sxy150489 	 * minimum threshold size to 1/2, and the receive buffer size to
18925779Sxy150489 	 * 2k.
18935779Sxy150489 	 */
1894*8571SChenlu.Chen@Sun.COM 	rctl = E1000_READ_REG(hw, E1000_RCTL);
1895*8571SChenlu.Chen@Sun.COM 
1896*8571SChenlu.Chen@Sun.COM 	/*
1897*8571SChenlu.Chen@Sun.COM 	 * only used for wakeup control.  This driver doesn't do wakeup
1898*8571SChenlu.Chen@Sun.COM 	 * but leave this here for completeness.
1899*8571SChenlu.Chen@Sun.COM 	 */
1900*8571SChenlu.Chen@Sun.COM 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1901*8571SChenlu.Chen@Sun.COM 
1902*8571SChenlu.Chen@Sun.COM 	rctl |= E1000_RCTL_EN |		/* Enable Receive Unit */
19035779Sxy150489 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
19045779Sxy150489 	    E1000_RCTL_LPE |		/* Large Packet Enable bit */
19055779Sxy150489 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
19065779Sxy150489 	    E1000_RCTL_RDMTS_HALF |
19075779Sxy150489 	    E1000_RCTL_SECRC |		/* Strip Ethernet CRC */
19085779Sxy150489 	    E1000_RCTL_LBM_NO;		/* Loopback Mode = none */
19095779Sxy150489 
1910*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
19115779Sxy150489 
19128275SEric Cheng 	for (i = 0; i < igb->num_rx_groups; i++) {
19138275SEric Cheng 		rx_group = &igb->rx_groups[i];
19148275SEric Cheng 		rx_group->index = i;
19158275SEric Cheng 		rx_group->igb = igb;
19168275SEric Cheng 	}
19178275SEric Cheng 
19185779Sxy150489 	/*
19195812Sxy150489 	 * igb_setup_rx_ring must be called after configuring RCTL
19205812Sxy150489 	 */
19218275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
19225812Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
19235812Sxy150489 		rx_ring = &igb->rx_rings[i];
19245812Sxy150489 		igb_setup_rx_ring(rx_ring);
19258275SEric Cheng 
19268275SEric Cheng 		/*
19278275SEric Cheng 		 * Map a ring to a group by assigning a group index
19288275SEric Cheng 		 */
19298275SEric Cheng 		rx_ring->group_index = i / ring_per_group;
19305812Sxy150489 	}
19315812Sxy150489 
19325812Sxy150489 	/*
19335779Sxy150489 	 * Setup the Rx Long Packet Max Length register
19345779Sxy150489 	 */
19355779Sxy150489 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
19365779Sxy150489 
19375779Sxy150489 	/*
19385779Sxy150489 	 * Hardware checksum settings
19395779Sxy150489 	 */
19405779Sxy150489 	if (igb->rx_hcksum_enable) {
19415779Sxy150489 		reg_val =
19425779Sxy150489 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
19435779Sxy150489 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
19445779Sxy150489 
19455779Sxy150489 		E1000_WRITE_REG(hw, E1000_RXCSUM, reg_val);
19465779Sxy150489 	}
19475779Sxy150489 
19485779Sxy150489 	/*
19498275SEric Cheng 	 * Setup classify and RSS for multiple receive queues
19505779Sxy150489 	 */
19518275SEric Cheng 	switch (igb->vmdq_mode) {
19528275SEric Cheng 	case E1000_VMDQ_OFF:
19538275SEric Cheng 		/*
19548275SEric Cheng 		 * One ring group, only RSS is needed when more than
19558275SEric Cheng 		 * one ring enabled.
19568275SEric Cheng 		 */
19578275SEric Cheng 		if (igb->num_rx_rings > 1)
19588275SEric Cheng 			igb_setup_rss(igb);
19598275SEric Cheng 		break;
19608275SEric Cheng 	case E1000_VMDQ_MAC:
19618275SEric Cheng 		/*
19628275SEric Cheng 		 * Multiple groups, each group has one ring,
19638275SEric Cheng 		 * only the MAC classification is needed.
19648275SEric Cheng 		 */
19658275SEric Cheng 		igb_setup_mac_classify(igb);
19668275SEric Cheng 		break;
19678275SEric Cheng 	case E1000_VMDQ_MAC_RSS:
19688275SEric Cheng 		/*
19698275SEric Cheng 		 * Multiple groups and multiple rings, both
19708275SEric Cheng 		 * MAC classification and RSS are needed.
19718275SEric Cheng 		 */
19728275SEric Cheng 		igb_setup_mac_rss_classify(igb);
19738275SEric Cheng 		break;
19748275SEric Cheng 	}
19755779Sxy150489 }
19765779Sxy150489 
19775779Sxy150489 static void
19785779Sxy150489 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
19795779Sxy150489 {
19805779Sxy150489 	igb_t *igb = tx_ring->igb;
19815779Sxy150489 	struct e1000_hw *hw = &igb->hw;
19825779Sxy150489 	uint32_t size;
19835779Sxy150489 	uint32_t buf_low;
19845779Sxy150489 	uint32_t buf_high;
19855779Sxy150489 	uint32_t reg_val;
19865779Sxy150489 
19875779Sxy150489 	ASSERT(mutex_owned(&tx_ring->tx_lock));
19885779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
19895779Sxy150489 
19908275SEric Cheng 
19915779Sxy150489 	/*
19925779Sxy150489 	 * Initialize the length register
19935779Sxy150489 	 */
19945779Sxy150489 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
19955779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
19965779Sxy150489 
19975779Sxy150489 	/*
19985779Sxy150489 	 * Initialize the base address registers
19995779Sxy150489 	 */
20005779Sxy150489 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
20015779Sxy150489 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
20025779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
20035779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
20045779Sxy150489 
20055779Sxy150489 	/*
20065779Sxy150489 	 * Setup head & tail pointers
20075779Sxy150489 	 */
20085779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
20095779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
20105779Sxy150489 
20115779Sxy150489 	/*
20125779Sxy150489 	 * Setup head write-back
20135779Sxy150489 	 */
20145779Sxy150489 	if (igb->tx_head_wb_enable) {
20155779Sxy150489 		/*
20165779Sxy150489 		 * The memory of the head write-back is allocated using
20175779Sxy150489 		 * the extra tbd beyond the tail of the tbd ring.
20185779Sxy150489 		 */
20195779Sxy150489 		tx_ring->tbd_head_wb = (uint32_t *)
20205779Sxy150489 		    ((uintptr_t)tx_ring->tbd_area.address + size);
20217072Sxy150489 		*tx_ring->tbd_head_wb = 0;
20225779Sxy150489 
20235779Sxy150489 		buf_low = (uint32_t)
20245779Sxy150489 		    (tx_ring->tbd_area.dma_address + size);
20255779Sxy150489 		buf_high = (uint32_t)
20265779Sxy150489 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
20275779Sxy150489 
20285779Sxy150489 		/* Set the head write-back enable bit */
20295779Sxy150489 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
20305779Sxy150489 
20315779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
20325779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
20335779Sxy150489 
20345779Sxy150489 		/*
20355779Sxy150489 		 * Turn off relaxed ordering for head write back or it will
20365779Sxy150489 		 * cause problems with the tx recycling
20375779Sxy150489 		 */
20385779Sxy150489 		reg_val = E1000_READ_REG(hw,
20395779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index));
20405779Sxy150489 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
20415779Sxy150489 		E1000_WRITE_REG(hw,
20425779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
20435779Sxy150489 	} else {
20445779Sxy150489 		tx_ring->tbd_head_wb = NULL;
20455779Sxy150489 	}
20465779Sxy150489 
20475779Sxy150489 	tx_ring->tbd_head = 0;
20485779Sxy150489 	tx_ring->tbd_tail = 0;
20495779Sxy150489 	tx_ring->tbd_free = tx_ring->ring_size;
20505779Sxy150489 
20515779Sxy150489 	/*
2052*8571SChenlu.Chen@Sun.COM 	 * Note: for the case that the chipset is being reset, we should not
2053*8571SChenlu.Chen@Sun.COM 	 * reset the values of tcb_head, tcb_tail. And considering there might
2054*8571SChenlu.Chen@Sun.COM 	 * still be some packets kept in the pending_list, we should not assert
2055*8571SChenlu.Chen@Sun.COM 	 * (tcb_free == free_list_size) here.
20565779Sxy150489 	 */
20575779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
20585779Sxy150489 		tx_ring->tcb_head = 0;
20595779Sxy150489 		tx_ring->tcb_tail = 0;
20605779Sxy150489 		tx_ring->tcb_free = tx_ring->free_list_size;
20615779Sxy150489 	}
20625779Sxy150489 
20635779Sxy150489 	/*
20648275SEric Cheng 	 * Enable specific tx ring, it is required by multiple tx
20658275SEric Cheng 	 * ring support.
20668275SEric Cheng 	 */
20678275SEric Cheng 	reg_val = E1000_READ_REG(hw, E1000_TXDCTL(tx_ring->index));
20688275SEric Cheng 	reg_val |= E1000_TXDCTL_QUEUE_ENABLE;
20698275SEric Cheng 	E1000_WRITE_REG(hw, E1000_TXDCTL(tx_ring->index), reg_val);
20708275SEric Cheng 
20718275SEric Cheng 	/*
20725779Sxy150489 	 * Initialize hardware checksum offload settings
20735779Sxy150489 	 */
20745779Sxy150489 	tx_ring->hcksum_context.hcksum_flags = 0;
20755779Sxy150489 	tx_ring->hcksum_context.ip_hdr_len = 0;
20765779Sxy150489 	tx_ring->hcksum_context.mac_hdr_len = 0;
20775779Sxy150489 	tx_ring->hcksum_context.l4_proto = 0;
2078*8571SChenlu.Chen@Sun.COM 
2079*8571SChenlu.Chen@Sun.COM 	/*
2080*8571SChenlu.Chen@Sun.COM 	 * Enable TXDCTL per queue
2081*8571SChenlu.Chen@Sun.COM 	 */
2082*8571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TXDCTL(tx_ring->index));
2083*8571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TXDCTL_QUEUE_ENABLE;
2084*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_TXDCTL(tx_ring->index), reg_val);
20855779Sxy150489 }
20865779Sxy150489 
20875779Sxy150489 static void
20885779Sxy150489 igb_setup_tx(igb_t *igb)
20895779Sxy150489 {
20905779Sxy150489 	igb_tx_ring_t *tx_ring;
20915779Sxy150489 	struct e1000_hw *hw = &igb->hw;
20925779Sxy150489 	uint32_t reg_val;
20935779Sxy150489 	int i;
20945779Sxy150489 
20955779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
20965779Sxy150489 		tx_ring = &igb->tx_rings[i];
20975779Sxy150489 		igb_setup_tx_ring(tx_ring);
20985779Sxy150489 	}
20995779Sxy150489 
21005779Sxy150489 	/*
21015779Sxy150489 	 * Setup the Transmit Control Register (TCTL)
21025779Sxy150489 	 */
2103*8571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TCTL);
2104*8571SChenlu.Chen@Sun.COM 	reg_val &= ~E1000_TCTL_CT;
2105*8571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2106*8571SChenlu.Chen@Sun.COM 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2107*8571SChenlu.Chen@Sun.COM 
2108*8571SChenlu.Chen@Sun.COM 	e1000_config_collision_dist(hw);
2109*8571SChenlu.Chen@Sun.COM 
2110*8571SChenlu.Chen@Sun.COM 	/* Enable transmits */
2111*8571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_EN;
21125779Sxy150489 
21135779Sxy150489 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
21145779Sxy150489 }
21155779Sxy150489 
21165779Sxy150489 /*
21175779Sxy150489  * igb_setup_rss - Setup receive-side scaling feature
21185779Sxy150489  */
21195779Sxy150489 static void
21205779Sxy150489 igb_setup_rss(igb_t *igb)
21215779Sxy150489 {
21225779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21235779Sxy150489 	uint32_t i, mrqc, rxcsum;
2124*8571SChenlu.Chen@Sun.COM 	int shift = 0;
21255779Sxy150489 	uint32_t random;
21265779Sxy150489 	union e1000_reta {
21275779Sxy150489 		uint32_t	dword;
21285779Sxy150489 		uint8_t		bytes[4];
21295779Sxy150489 	} reta;
21305779Sxy150489 
21315779Sxy150489 	/* Setup the Redirection Table */
2132*8571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82576) {
2133*8571SChenlu.Chen@Sun.COM 		shift = 0;
2134*8571SChenlu.Chen@Sun.COM 	} else if (hw->mac.type == e1000_82575) {
2135*8571SChenlu.Chen@Sun.COM 		shift = 6;
2136*8571SChenlu.Chen@Sun.COM 	}
21375779Sxy150489 	for (i = 0; i < (32 * 4); i++) {
21385779Sxy150489 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
21395779Sxy150489 		if ((i & 3) == 3) {
21405779Sxy150489 			E1000_WRITE_REG(hw,
21415779Sxy150489 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
21425779Sxy150489 		}
21435779Sxy150489 	}
21445779Sxy150489 
21455779Sxy150489 	/* Fill out hash function seeds */
21465779Sxy150489 	for (i = 0; i < 10; i++) {
21475779Sxy150489 		(void) random_get_pseudo_bytes((uint8_t *)&random,
21485779Sxy150489 		    sizeof (uint32_t));
21495779Sxy150489 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
21505779Sxy150489 	}
21515779Sxy150489 
21525779Sxy150489 	/* Setup the Multiple Receive Queue Control register */
21535779Sxy150489 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
21545779Sxy150489 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
21555779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
21565779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6 |
21575779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
21585779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
21595779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
21605779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
21615779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
21625779Sxy150489 
21635779Sxy150489 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
21645779Sxy150489 
21655779Sxy150489 	/*
21665779Sxy150489 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
21675779Sxy150489 	 *
21685779Sxy150489 	 * The Packet Checksum is not ethernet CRC. It is another kind of
21695779Sxy150489 	 * checksum offloading provided by the 82575 chipset besides the IP
21705779Sxy150489 	 * header checksum offloading and the TCP/UDP checksum offloading.
21715779Sxy150489 	 * The Packet Checksum is by default computed over the entire packet
21725779Sxy150489 	 * from the first byte of the DA through the last byte of the CRC,
21735779Sxy150489 	 * including the Ethernet and IP headers.
21745779Sxy150489 	 *
21755779Sxy150489 	 * It is a hardware limitation that Packet Checksum is mutually
21765779Sxy150489 	 * exclusive with RSS.
21775779Sxy150489 	 */
21785779Sxy150489 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
21795779Sxy150489 	rxcsum |= E1000_RXCSUM_PCSD;
21805779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
21815779Sxy150489 }
21825779Sxy150489 
21835779Sxy150489 /*
21848275SEric Cheng  * igb_setup_mac_rss_classify - Setup MAC classification and rss
21858275SEric Cheng  */
21868275SEric Cheng static void
21878275SEric Cheng igb_setup_mac_rss_classify(igb_t *igb)
21888275SEric Cheng {
21898275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
21908275SEric Cheng 	uint32_t i, mrqc, vmdctl, rxcsum;
21918275SEric Cheng 	uint32_t ring_per_group;
21928275SEric Cheng 	int shift_group0, shift_group1;
21938275SEric Cheng 	uint32_t random;
21948275SEric Cheng 	union e1000_reta {
21958275SEric Cheng 		uint32_t	dword;
21968275SEric Cheng 		uint8_t		bytes[4];
21978275SEric Cheng 	} reta;
21988275SEric Cheng 
21998275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
22008275SEric Cheng 
22018275SEric Cheng 	/* Setup the Redirection Table, it is shared between two groups */
22028275SEric Cheng 	shift_group0 = 2;
22038275SEric Cheng 	shift_group1 = 6;
22048275SEric Cheng 	for (i = 0; i < (32 * 4); i++) {
22058275SEric Cheng 		reta.bytes[i & 3] = ((i % ring_per_group) << shift_group0) |
22068275SEric Cheng 		    ((ring_per_group + (i % ring_per_group)) << shift_group1);
22078275SEric Cheng 		if ((i & 3) == 3) {
22088275SEric Cheng 			E1000_WRITE_REG(hw,
22098275SEric Cheng 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
22108275SEric Cheng 		}
22118275SEric Cheng 	}
22128275SEric Cheng 
22138275SEric Cheng 	/* Fill out hash function seeds */
22148275SEric Cheng 	for (i = 0; i < 10; i++) {
22158275SEric Cheng 		(void) random_get_pseudo_bytes((uint8_t *)&random,
22168275SEric Cheng 		    sizeof (uint32_t));
22178275SEric Cheng 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
22188275SEric Cheng 	}
22198275SEric Cheng 
22208275SEric Cheng 	/*
22218275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
22228275SEric Cheng 	 * enable VMDq based on packet destination MAC address and RSS.
22238275SEric Cheng 	 */
22248275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_RSS_GROUP;
22258275SEric Cheng 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
22268275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
22278275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6 |
22288275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
22298275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
22308275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
22318275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
22328275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
22338275SEric Cheng 
22348275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
22358275SEric Cheng 
22368275SEric Cheng 
22378275SEric Cheng 	/* Define the default group and default queues */
22388275SEric Cheng 	vmdctl = E1000_VMDQ_MAC_GROUP_DEFAULT_QUEUE;
2239*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_VT_CTL, vmdctl);
22408275SEric Cheng 
22418275SEric Cheng 	/*
22428275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
22438275SEric Cheng 	 *
22448275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
22458275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
22468275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
22478275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
22488275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
22498275SEric Cheng 	 * including the Ethernet and IP headers.
22508275SEric Cheng 	 *
22518275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
22528275SEric Cheng 	 * exclusive with RSS.
22538275SEric Cheng 	 */
22548275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
22558275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
22568275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
22578275SEric Cheng }
22588275SEric Cheng 
22598275SEric Cheng /*
22608275SEric Cheng  * igb_setup_mac_classify - Setup MAC classification feature
22618275SEric Cheng  */
22628275SEric Cheng static void
22638275SEric Cheng igb_setup_mac_classify(igb_t *igb)
22648275SEric Cheng {
22658275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
22668275SEric Cheng 	uint32_t mrqc, rxcsum;
22678275SEric Cheng 
22688275SEric Cheng 	/*
22698275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
22708275SEric Cheng 	 * enable VMDq based on packet destination MAC address.
22718275SEric Cheng 	 */
22728275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_GROUP;
22738275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
22748275SEric Cheng 
22758275SEric Cheng 	/*
22768275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
22778275SEric Cheng 	 *
22788275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
22798275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
22808275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
22818275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
22828275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
22838275SEric Cheng 	 * including the Ethernet and IP headers.
22848275SEric Cheng 	 *
22858275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
22868275SEric Cheng 	 * exclusive with RSS.
22878275SEric Cheng 	 */
22888275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
22898275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
22908275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
22918275SEric Cheng 
22928275SEric Cheng }
22938275SEric Cheng 
22948275SEric Cheng /*
22955779Sxy150489  * igb_init_unicst - Initialize the unicast addresses
22965779Sxy150489  */
22975779Sxy150489 static void
22985779Sxy150489 igb_init_unicst(igb_t *igb)
22995779Sxy150489 {
23005779Sxy150489 	struct e1000_hw *hw = &igb->hw;
23015779Sxy150489 	int slot;
23025779Sxy150489 
23035779Sxy150489 	/*
23045779Sxy150489 	 * Here we should consider two situations:
23055779Sxy150489 	 *
23065779Sxy150489 	 * 1. Chipset is initialized the first time
23075779Sxy150489 	 *    Initialize the multiple unicast addresses, and
23088275SEric Cheng 	 *    save the default MAC address.
23095779Sxy150489 	 *
23105779Sxy150489 	 * 2. Chipset is reset
23115779Sxy150489 	 *    Recover the multiple unicast addresses from the
23125779Sxy150489 	 *    software data structure to the RAR registers.
23135779Sxy150489 	 */
23148275SEric Cheng 
23158275SEric Cheng 	/*
23168275SEric Cheng 	 * Clear the default MAC address in the RAR0 rgister,
23178275SEric Cheng 	 * which is loaded from EEPROM when system boot or chipreset,
23188275SEric Cheng 	 * this will cause the conficts with add_mac/rem_mac entry
23198275SEric Cheng 	 * points when VMDq is enabled. For this reason, the RAR0
23208275SEric Cheng 	 * must be cleared for both cases mentioned above.
23218275SEric Cheng 	 */
23228275SEric Cheng 	e1000_rar_clear(hw, 0);
23238275SEric Cheng 
23245779Sxy150489 	if (!igb->unicst_init) {
23258275SEric Cheng 
23265779Sxy150489 		/* Initialize the multiple unicast addresses */
23275779Sxy150489 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
23288275SEric Cheng 		igb->unicst_avail = igb->unicst_total;
23298275SEric Cheng 
23308275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++)
23315779Sxy150489 			igb->unicst_addr[slot].mac.set = 0;
23325779Sxy150489 
23335779Sxy150489 		igb->unicst_init = B_TRUE;
23345779Sxy150489 	} else {
23355779Sxy150489 		/* Re-configure the RAR registers */
23368275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++) {
23378275SEric Cheng 			e1000_rar_set_vmdq(hw, igb->unicst_addr[slot].mac.addr,
23388275SEric Cheng 			    slot, igb->vmdq_mode,
23398275SEric Cheng 			    igb->unicst_addr[slot].mac.group_index);
23408275SEric Cheng 		}
23415779Sxy150489 	}
23426624Sgl147354 
23436624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
23446624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
23455779Sxy150489 }
23465779Sxy150489 
23475779Sxy150489 /*
23488275SEric Cheng  * igb_unicst_find - Find the slot for the specified unicast address
23498275SEric Cheng  */
23508275SEric Cheng int
23518275SEric Cheng igb_unicst_find(igb_t *igb, const uint8_t *mac_addr)
23528275SEric Cheng {
23538275SEric Cheng 	int slot;
23548275SEric Cheng 
23558275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
23568275SEric Cheng 
23578275SEric Cheng 	for (slot = 0; slot < igb->unicst_total; slot++) {
23588275SEric Cheng 		if (bcmp(igb->unicst_addr[slot].mac.addr,
23598275SEric Cheng 		    mac_addr, ETHERADDRL) == 0)
23608275SEric Cheng 			return (slot);
23618275SEric Cheng 	}
23628275SEric Cheng 
23638275SEric Cheng 	return (-1);
23648275SEric Cheng }
23658275SEric Cheng 
23668275SEric Cheng /*
23675779Sxy150489  * igb_unicst_set - Set the unicast address to the specified slot
23685779Sxy150489  */
23695779Sxy150489 int
23705779Sxy150489 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
23718275SEric Cheng     int slot)
23725779Sxy150489 {
23735779Sxy150489 	struct e1000_hw *hw = &igb->hw;
23745779Sxy150489 
23755779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
23765779Sxy150489 
23775779Sxy150489 	/*
23785779Sxy150489 	 * Save the unicast address in the software data structure
23795779Sxy150489 	 */
23805779Sxy150489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
23815779Sxy150489 
23825779Sxy150489 	/*
23835779Sxy150489 	 * Set the unicast address to the RAR register
23845779Sxy150489 	 */
23855779Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
23865779Sxy150489 
23876624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
23886624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
23896624Sgl147354 		return (EIO);
23906624Sgl147354 	}
23916624Sgl147354 
23925779Sxy150489 	return (0);
23935779Sxy150489 }
23945779Sxy150489 
23955779Sxy150489 /*
23965779Sxy150489  * igb_multicst_add - Add a multicst address
23975779Sxy150489  */
23985779Sxy150489 int
23995779Sxy150489 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
24005779Sxy150489 {
24015779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24025779Sxy150489 
24035779Sxy150489 	if ((multiaddr[0] & 01) == 0) {
24045779Sxy150489 		return (EINVAL);
24055779Sxy150489 	}
24065779Sxy150489 
24075779Sxy150489 	if (igb->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
24085779Sxy150489 		return (ENOENT);
24095779Sxy150489 	}
24105779Sxy150489 
24115779Sxy150489 	bcopy(multiaddr,
24125779Sxy150489 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
24135779Sxy150489 	igb->mcast_count++;
24145779Sxy150489 
24155779Sxy150489 	/*
24165779Sxy150489 	 * Update the multicast table in the hardware
24175779Sxy150489 	 */
24185779Sxy150489 	igb_setup_multicst(igb);
24195779Sxy150489 
24206624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
24216624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24226624Sgl147354 		return (EIO);
24236624Sgl147354 	}
24246624Sgl147354 
24255779Sxy150489 	return (0);
24265779Sxy150489 }
24275779Sxy150489 
24285779Sxy150489 /*
24295779Sxy150489  * igb_multicst_remove - Remove a multicst address
24305779Sxy150489  */
24315779Sxy150489 int
24325779Sxy150489 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
24335779Sxy150489 {
24345779Sxy150489 	int i;
24355779Sxy150489 
24365779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24375779Sxy150489 
24385779Sxy150489 	for (i = 0; i < igb->mcast_count; i++) {
24395779Sxy150489 		if (bcmp(multiaddr, &igb->mcast_table[i],
24405779Sxy150489 		    ETHERADDRL) == 0) {
24415779Sxy150489 			for (i++; i < igb->mcast_count; i++) {
24425779Sxy150489 				igb->mcast_table[i - 1] =
24435779Sxy150489 				    igb->mcast_table[i];
24445779Sxy150489 			}
24455779Sxy150489 			igb->mcast_count--;
24465779Sxy150489 			break;
24475779Sxy150489 		}
24485779Sxy150489 	}
24495779Sxy150489 
24505779Sxy150489 	/*
24515779Sxy150489 	 * Update the multicast table in the hardware
24525779Sxy150489 	 */
24535779Sxy150489 	igb_setup_multicst(igb);
24545779Sxy150489 
24556624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
24566624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24576624Sgl147354 		return (EIO);
24586624Sgl147354 	}
24596624Sgl147354 
24605779Sxy150489 	return (0);
24615779Sxy150489 }
24625779Sxy150489 
24635779Sxy150489 /*
24645779Sxy150489  * igb_setup_multicast - setup multicast data structures
24655779Sxy150489  *
24665779Sxy150489  * This routine initializes all of the multicast related structures
24675779Sxy150489  * and save them in the hardware registers.
24685779Sxy150489  */
24695779Sxy150489 static void
24705779Sxy150489 igb_setup_multicst(igb_t *igb)
24715779Sxy150489 {
24725779Sxy150489 	uint8_t *mc_addr_list;
24735779Sxy150489 	uint32_t mc_addr_count;
24745779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24755779Sxy150489 
24765779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24775779Sxy150489 
24785779Sxy150489 	ASSERT(igb->mcast_count <= MAX_NUM_MULTICAST_ADDRESSES);
24795779Sxy150489 
24805779Sxy150489 	mc_addr_list = (uint8_t *)igb->mcast_table;
24815779Sxy150489 	mc_addr_count = igb->mcast_count;
24825779Sxy150489 
24835779Sxy150489 	/*
24845779Sxy150489 	 * Update the multicase addresses to the MTA registers
24855779Sxy150489 	 */
24865779Sxy150489 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
24875779Sxy150489 	    igb->unicst_total, hw->mac.rar_entry_count);
24885779Sxy150489 }
24895779Sxy150489 
24905779Sxy150489 /*
24915779Sxy150489  * igb_get_conf - Get driver configurations set in driver.conf
24925779Sxy150489  *
24935779Sxy150489  * This routine gets user-configured values out of the configuration
24945779Sxy150489  * file igb.conf.
24955779Sxy150489  *
24965779Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
24975779Sxy150489  * default.
24985779Sxy150489  * If user does not configure a value, use the default.
24995779Sxy150489  * If user configures below the minimum, use the minumum.
25005779Sxy150489  * If user configures above the maximum, use the maxumum.
25015779Sxy150489  */
25025779Sxy150489 static void
25035779Sxy150489 igb_get_conf(igb_t *igb)
25045779Sxy150489 {
25055779Sxy150489 	struct e1000_hw *hw = &igb->hw;
25065779Sxy150489 	uint32_t default_mtu;
25075779Sxy150489 	uint32_t flow_control;
25088275SEric Cheng 	uint32_t ring_per_group;
25098275SEric Cheng 	int i;
25105779Sxy150489 
25115779Sxy150489 	/*
25125779Sxy150489 	 * igb driver supports the following user configurations:
25135779Sxy150489 	 *
25145779Sxy150489 	 * Link configurations:
25155779Sxy150489 	 *    adv_autoneg_cap
25165779Sxy150489 	 *    adv_1000fdx_cap
25175779Sxy150489 	 *    adv_100fdx_cap
25185779Sxy150489 	 *    adv_100hdx_cap
25195779Sxy150489 	 *    adv_10fdx_cap
25205779Sxy150489 	 *    adv_10hdx_cap
25215779Sxy150489 	 * Note: 1000hdx is not supported.
25225779Sxy150489 	 *
25235779Sxy150489 	 * Jumbo frame configuration:
25245779Sxy150489 	 *    default_mtu
25255779Sxy150489 	 *
25265779Sxy150489 	 * Ethernet flow control configuration:
25275779Sxy150489 	 *    flow_control
25285779Sxy150489 	 *
25295779Sxy150489 	 * Multiple rings configurations:
25305779Sxy150489 	 *    tx_queue_number
25315779Sxy150489 	 *    tx_ring_size
25325779Sxy150489 	 *    rx_queue_number
25335779Sxy150489 	 *    rx_ring_size
25345779Sxy150489 	 *
25355779Sxy150489 	 * Call igb_get_prop() to get the value for a specific
25365779Sxy150489 	 * configuration parameter.
25375779Sxy150489 	 */
25385779Sxy150489 
25395779Sxy150489 	/*
25405779Sxy150489 	 * Link configurations
25415779Sxy150489 	 */
25425779Sxy150489 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
25435779Sxy150489 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
25445779Sxy150489 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
25455779Sxy150489 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
25465779Sxy150489 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
25475779Sxy150489 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
25485779Sxy150489 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
25495779Sxy150489 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
25505779Sxy150489 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
25515779Sxy150489 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
25525779Sxy150489 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
25535779Sxy150489 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
25545779Sxy150489 
25555779Sxy150489 	/*
25565779Sxy150489 	 * Jumbo frame configurations
25575779Sxy150489 	 */
25585779Sxy150489 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
25595779Sxy150489 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
25605779Sxy150489 
25615779Sxy150489 	igb->max_frame_size = default_mtu +
25625779Sxy150489 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
25635779Sxy150489 
25645779Sxy150489 	/*
25655779Sxy150489 	 * Ethernet flow control configuration
25665779Sxy150489 	 */
25675779Sxy150489 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
25685779Sxy150489 	    e1000_fc_none, 4, e1000_fc_full);
25695779Sxy150489 	if (flow_control == 4)
25705779Sxy150489 		flow_control = e1000_fc_default;
25715779Sxy150489 
2572*8571SChenlu.Chen@Sun.COM 	hw->fc.requested_mode = flow_control;
25735779Sxy150489 
25745779Sxy150489 	/*
25755779Sxy150489 	 * Multiple rings configurations
25765779Sxy150489 	 */
25775779Sxy150489 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
25785779Sxy150489 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
25795779Sxy150489 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
25805779Sxy150489 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
25815779Sxy150489 
25828275SEric Cheng 	igb->mr_enable = igb_get_prop(igb, PROP_MR_ENABLE, 0, 1, 1);
25838275SEric Cheng 	igb->num_rx_groups = igb_get_prop(igb, PROP_RX_GROUP_NUM,
25848275SEric Cheng 	    MIN_RX_GROUP_NUM, MAX_RX_GROUP_NUM, DEFAULT_RX_GROUP_NUM);
2585*8571SChenlu.Chen@Sun.COM 	/*
2586*8571SChenlu.Chen@Sun.COM 	 * Currently we do not support VMDq for 82576.
2587*8571SChenlu.Chen@Sun.COM 	 * If it is e1000_82576, set num_rx_groups to 1.
2588*8571SChenlu.Chen@Sun.COM 	 */
2589*8571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82576)
2590*8571SChenlu.Chen@Sun.COM 		igb->num_rx_groups = 1;
25918275SEric Cheng 
25928275SEric Cheng 	if (igb->mr_enable) {
2593*8571SChenlu.Chen@Sun.COM 		igb->num_tx_rings = igb->capab->def_tx_que_num;
2594*8571SChenlu.Chen@Sun.COM 		igb->num_rx_rings = igb->capab->def_rx_que_num;
25958275SEric Cheng 	} else {
25968275SEric Cheng 		igb->num_tx_rings = 1;
25978275SEric Cheng 		igb->num_rx_rings = 1;
25988275SEric Cheng 
25998275SEric Cheng 		if (igb->num_rx_groups > 1) {
26008275SEric Cheng 			igb_error(igb,
26018275SEric Cheng 			    "Invalid rx groups number. Please enable multiple "
26028275SEric Cheng 			    "rings first");
26038275SEric Cheng 			igb->num_rx_groups = 1;
26048275SEric Cheng 		}
26058275SEric Cheng 	}
26068275SEric Cheng 
26078275SEric Cheng 	/*
26088275SEric Cheng 	 * Check the divisibility between rx rings and rx groups.
26098275SEric Cheng 	 */
26108275SEric Cheng 	for (i = igb->num_rx_groups; i > 0; i--) {
26118275SEric Cheng 		if ((igb->num_rx_rings % i) == 0)
26128275SEric Cheng 			break;
26138275SEric Cheng 	}
26148275SEric Cheng 	if (i != igb->num_rx_groups) {
26158275SEric Cheng 		igb_error(igb,
26168275SEric Cheng 		    "Invalid rx groups number. Downgrade the rx group "
26178275SEric Cheng 		    "number to %d.", i);
26188275SEric Cheng 		igb->num_rx_groups = i;
26198275SEric Cheng 	}
26208275SEric Cheng 
26218275SEric Cheng 	/*
26228275SEric Cheng 	 * Get the ring number per group.
26238275SEric Cheng 	 */
26248275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
26258275SEric Cheng 
26268275SEric Cheng 	if (igb->num_rx_groups == 1) {
26278275SEric Cheng 		/*
26288275SEric Cheng 		 * One rx ring group, the rx ring number is num_rx_rings.
26298275SEric Cheng 		 */
26308275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_OFF;
26318275SEric Cheng 	} else if (ring_per_group == 1) {
26328275SEric Cheng 		/*
26338275SEric Cheng 		 * Multiple rx groups, each group has one rx ring.
26348275SEric Cheng 		 */
26358275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC;
26368275SEric Cheng 	} else {
26378275SEric Cheng 		/*
26388275SEric Cheng 		 * Multiple groups and multiple rings.
26398275SEric Cheng 		 */
26408275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC_RSS;
26418275SEric Cheng 	}
26428275SEric Cheng 
26435779Sxy150489 	/*
26445779Sxy150489 	 * Tunable used to force an interrupt type. The only use is
26455779Sxy150489 	 * for testing of the lesser interrupt types.
26465779Sxy150489 	 * 0 = don't force interrupt type
26475779Sxy150489 	 * 1 = force interrupt type MSIX
26485779Sxy150489 	 * 2 = force interrupt type MSI
26495779Sxy150489 	 * 3 = force interrupt type Legacy
26505779Sxy150489 	 */
26515779Sxy150489 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
26525812Sxy150489 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_NONE);
26535779Sxy150489 
26545779Sxy150489 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
26555779Sxy150489 	    0, 1, 1);
26565779Sxy150489 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
26575779Sxy150489 	    0, 1, 1);
26585779Sxy150489 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
26595779Sxy150489 	    0, 1, 0);
26605779Sxy150489 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
26615779Sxy150489 	    0, 1, 1);
26625779Sxy150489 
26635779Sxy150489 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
26645779Sxy150489 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
26655779Sxy150489 	    DEFAULT_TX_COPY_THRESHOLD);
26665779Sxy150489 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
26675779Sxy150489 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
26685779Sxy150489 	    DEFAULT_TX_RECYCLE_THRESHOLD);
26695779Sxy150489 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
26705779Sxy150489 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
26715779Sxy150489 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
26725779Sxy150489 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
26735779Sxy150489 	    MIN_TX_RESCHED_THRESHOLD, MAX_TX_RESCHED_THRESHOLD,
26745779Sxy150489 	    DEFAULT_TX_RESCHED_THRESHOLD);
26755779Sxy150489 
26765779Sxy150489 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
26775779Sxy150489 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
26785779Sxy150489 	    DEFAULT_RX_COPY_THRESHOLD);
26795779Sxy150489 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
26805779Sxy150489 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
26815779Sxy150489 	    DEFAULT_RX_LIMIT_PER_INTR);
26825779Sxy150489 
26835779Sxy150489 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
2684*8571SChenlu.Chen@Sun.COM 	    igb->capab->min_intr_throttle,
2685*8571SChenlu.Chen@Sun.COM 	    igb->capab->max_intr_throttle,
2686*8571SChenlu.Chen@Sun.COM 	    igb->capab->def_intr_throttle);
26875779Sxy150489 }
26885779Sxy150489 
26895779Sxy150489 /*
26905779Sxy150489  * igb_get_prop - Get a property value out of the configuration file igb.conf
26915779Sxy150489  *
26925779Sxy150489  * Caller provides the name of the property, a default value, a minimum
26935779Sxy150489  * value, and a maximum value.
26945779Sxy150489  *
26955779Sxy150489  * Return configured value of the property, with default, minimum and
26965779Sxy150489  * maximum properly applied.
26975779Sxy150489  */
26985779Sxy150489 static int
26995779Sxy150489 igb_get_prop(igb_t *igb,
27005779Sxy150489     char *propname,	/* name of the property */
27015779Sxy150489     int minval,		/* minimum acceptable value */
27025779Sxy150489     int maxval,		/* maximim acceptable value */
27035779Sxy150489     int defval)		/* default value */
27045779Sxy150489 {
27055779Sxy150489 	int value;
27065779Sxy150489 
27075779Sxy150489 	/*
27085779Sxy150489 	 * Call ddi_prop_get_int() to read the conf settings
27095779Sxy150489 	 */
27105779Sxy150489 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
27115779Sxy150489 	    DDI_PROP_DONTPASS, propname, defval);
27125779Sxy150489 
27135779Sxy150489 	if (value > maxval)
27145779Sxy150489 		value = maxval;
27155779Sxy150489 
27165779Sxy150489 	if (value < minval)
27175779Sxy150489 		value = minval;
27185779Sxy150489 
27195779Sxy150489 	return (value);
27205779Sxy150489 }
27215779Sxy150489 
27225779Sxy150489 /*
27235779Sxy150489  * igb_setup_link - Using the link properties to setup the link
27245779Sxy150489  */
27255779Sxy150489 int
27265779Sxy150489 igb_setup_link(igb_t *igb, boolean_t setup_hw)
27275779Sxy150489 {
27285779Sxy150489 	struct e1000_mac_info *mac;
27295779Sxy150489 	struct e1000_phy_info *phy;
27305779Sxy150489 	boolean_t invalid;
27315779Sxy150489 
27325779Sxy150489 	mac = &igb->hw.mac;
27335779Sxy150489 	phy = &igb->hw.phy;
27345779Sxy150489 	invalid = B_FALSE;
27355779Sxy150489 
27365779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
27375779Sxy150489 		mac->autoneg = B_TRUE;
27385779Sxy150489 		phy->autoneg_advertised = 0;
27395779Sxy150489 
27405779Sxy150489 		/*
27415779Sxy150489 		 * 1000hdx is not supported for autonegotiation
27425779Sxy150489 		 */
27435779Sxy150489 		if (igb->param_adv_1000fdx_cap == 1)
27445779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
27455779Sxy150489 
27465779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
27475779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
27485779Sxy150489 
27495779Sxy150489 		if (igb->param_adv_100hdx_cap == 1)
27505779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
27515779Sxy150489 
27525779Sxy150489 		if (igb->param_adv_10fdx_cap == 1)
27535779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
27545779Sxy150489 
27555779Sxy150489 		if (igb->param_adv_10hdx_cap == 1)
27565779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
27575779Sxy150489 
27585779Sxy150489 		if (phy->autoneg_advertised == 0)
27595779Sxy150489 			invalid = B_TRUE;
27605779Sxy150489 	} else {
27615779Sxy150489 		mac->autoneg = B_FALSE;
27625779Sxy150489 
27635779Sxy150489 		/*
27645779Sxy150489 		 * 1000fdx and 1000hdx are not supported for forced link
27655779Sxy150489 		 */
27665779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
27675779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
27685779Sxy150489 		else if (igb->param_adv_100hdx_cap == 1)
27695779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
27705779Sxy150489 		else if (igb->param_adv_10fdx_cap == 1)
27715779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
27725779Sxy150489 		else if (igb->param_adv_10hdx_cap == 1)
27735779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
27745779Sxy150489 		else
27755779Sxy150489 			invalid = B_TRUE;
27765779Sxy150489 	}
27775779Sxy150489 
27785779Sxy150489 	if (invalid) {
27795779Sxy150489 		igb_notice(igb, "Invalid link settings. Setup link to "
27805779Sxy150489 		    "autonegotiation with full link capabilities.");
27815779Sxy150489 		mac->autoneg = B_TRUE;
27825779Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
27835779Sxy150489 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
27845779Sxy150489 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
27855779Sxy150489 	}
27865779Sxy150489 
27875779Sxy150489 	if (setup_hw) {
27885779Sxy150489 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
27895779Sxy150489 			return (IGB_FAILURE);
27905779Sxy150489 	}
27915779Sxy150489 
27925779Sxy150489 	return (IGB_SUCCESS);
27935779Sxy150489 }
27945779Sxy150489 
27955779Sxy150489 
27965779Sxy150489 /*
27975779Sxy150489  * igb_is_link_up - Check if the link is up
27985779Sxy150489  */
27995779Sxy150489 static boolean_t
28005779Sxy150489 igb_is_link_up(igb_t *igb)
28015779Sxy150489 {
28025779Sxy150489 	struct e1000_hw *hw = &igb->hw;
28035779Sxy150489 	boolean_t link_up;
28045779Sxy150489 
28055779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
28065779Sxy150489 
28075779Sxy150489 	(void) e1000_check_for_link(hw);
28085779Sxy150489 
28095779Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
28105779Sxy150489 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
28115779Sxy150489 	    (hw->mac.serdes_has_link))) {
28125779Sxy150489 		link_up = B_TRUE;
28135779Sxy150489 	} else {
28145779Sxy150489 		link_up = B_FALSE;
28155779Sxy150489 	}
28165779Sxy150489 
28175779Sxy150489 	return (link_up);
28185779Sxy150489 }
28195779Sxy150489 
28205779Sxy150489 /*
28215779Sxy150489  * igb_link_check - Link status processing
28225779Sxy150489  */
28235779Sxy150489 static boolean_t
28245779Sxy150489 igb_link_check(igb_t *igb)
28255779Sxy150489 {
28265779Sxy150489 	struct e1000_hw *hw = &igb->hw;
28275779Sxy150489 	uint16_t speed = 0, duplex = 0;
28285779Sxy150489 	boolean_t link_changed = B_FALSE;
28295779Sxy150489 
28305779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
28315779Sxy150489 
28325779Sxy150489 	if (igb_is_link_up(igb)) {
28335779Sxy150489 		/*
28345779Sxy150489 		 * The Link is up, check whether it was marked as down earlier
28355779Sxy150489 		 */
28365779Sxy150489 		if (igb->link_state != LINK_STATE_UP) {
28375779Sxy150489 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
28385779Sxy150489 			igb->link_speed = speed;
28395779Sxy150489 			igb->link_duplex = duplex;
28405779Sxy150489 			igb->link_state = LINK_STATE_UP;
28415779Sxy150489 			igb->link_down_timeout = 0;
28425779Sxy150489 			link_changed = B_TRUE;
28435779Sxy150489 		}
28445779Sxy150489 	} else {
28455779Sxy150489 		if (igb->link_state != LINK_STATE_DOWN) {
28465779Sxy150489 			igb->link_speed = 0;
28475779Sxy150489 			igb->link_duplex = 0;
28485779Sxy150489 			igb->link_state = LINK_STATE_DOWN;
28495779Sxy150489 			link_changed = B_TRUE;
28505779Sxy150489 		}
28515779Sxy150489 
28525779Sxy150489 		if (igb->igb_state & IGB_STARTED) {
28535779Sxy150489 			if (igb->link_down_timeout < MAX_LINK_DOWN_TIMEOUT) {
28545779Sxy150489 				igb->link_down_timeout++;
28555779Sxy150489 			} else if (igb->link_down_timeout ==
28565779Sxy150489 			    MAX_LINK_DOWN_TIMEOUT) {
28575779Sxy150489 				igb_tx_clean(igb);
28585779Sxy150489 				igb->link_down_timeout++;
28595779Sxy150489 			}
28605779Sxy150489 		}
28615779Sxy150489 	}
28625779Sxy150489 
28636624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
28646624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
28656624Sgl147354 
28665779Sxy150489 	return (link_changed);
28675779Sxy150489 }
28685779Sxy150489 
28695779Sxy150489 /*
28705779Sxy150489  * igb_local_timer - driver watchdog function
28715779Sxy150489  *
28725779Sxy150489  * This function will handle the transmit stall check, link status check and
28735779Sxy150489  * other routines.
28745779Sxy150489  */
28755779Sxy150489 static void
28765779Sxy150489 igb_local_timer(void *arg)
28775779Sxy150489 {
28785779Sxy150489 	igb_t *igb = (igb_t *)arg;
28795779Sxy150489 	struct e1000_hw *hw = &igb->hw;
28805779Sxy150489 	boolean_t link_changed;
28815779Sxy150489 
28825779Sxy150489 	if (igb_stall_check(igb)) {
28836624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_STALL);
28845779Sxy150489 		igb->reset_count++;
28856624Sgl147354 		if (igb_reset(igb) == IGB_SUCCESS)
28866624Sgl147354 			ddi_fm_service_impact(igb->dip,
28876624Sgl147354 			    DDI_SERVICE_RESTORED);
28885779Sxy150489 	}
28895779Sxy150489 
28905779Sxy150489 	mutex_enter(&igb->gen_lock);
28915779Sxy150489 	link_changed = igb_link_check(igb);
28925779Sxy150489 	mutex_exit(&igb->gen_lock);
28935779Sxy150489 
28945779Sxy150489 	if (link_changed)
28955779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
28965779Sxy150489 
28975779Sxy150489 	/*
28985779Sxy150489 	 * Set Timer Interrupts
28995779Sxy150489 	 */
29005779Sxy150489 	if (igb->intr_type != DDI_INTR_TYPE_MSIX)
29015779Sxy150489 		E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
29025779Sxy150489 
29036624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
29046624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
29056624Sgl147354 
29065779Sxy150489 	igb_restart_watchdog_timer(igb);
29075779Sxy150489 }
29085779Sxy150489 
29095779Sxy150489 /*
29105779Sxy150489  * igb_stall_check - check for transmit stall
29115779Sxy150489  *
29125779Sxy150489  * This function checks if the adapter is stalled (in transmit).
29135779Sxy150489  *
29145779Sxy150489  * It is called each time the watchdog timeout is invoked.
29155779Sxy150489  * If the transmit descriptor reclaim continuously fails,
29165779Sxy150489  * the watchdog value will increment by 1. If the watchdog
29175779Sxy150489  * value exceeds the threshold, the igb is assumed to
29185779Sxy150489  * have stalled and need to be reset.
29195779Sxy150489  */
29205779Sxy150489 static boolean_t
29215779Sxy150489 igb_stall_check(igb_t *igb)
29225779Sxy150489 {
29235779Sxy150489 	igb_tx_ring_t *tx_ring;
29245779Sxy150489 	boolean_t result;
29255779Sxy150489 	int i;
29265779Sxy150489 
29275779Sxy150489 	if (igb->link_state != LINK_STATE_UP)
29285779Sxy150489 		return (B_FALSE);
29295779Sxy150489 
29305779Sxy150489 	/*
29315779Sxy150489 	 * If any tx ring is stalled, we'll reset the chipset
29325779Sxy150489 	 */
29335779Sxy150489 	result = B_FALSE;
29345779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
29355779Sxy150489 		tx_ring = &igb->tx_rings[i];
29365779Sxy150489 
29375779Sxy150489 		if (tx_ring->recycle_fail > 0)
29385779Sxy150489 			tx_ring->stall_watchdog++;
29395779Sxy150489 		else
29405779Sxy150489 			tx_ring->stall_watchdog = 0;
29415779Sxy150489 
29425779Sxy150489 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
29435779Sxy150489 			result = B_TRUE;
29445779Sxy150489 			break;
29455779Sxy150489 		}
29465779Sxy150489 	}
29475779Sxy150489 
29485779Sxy150489 	if (result) {
29495779Sxy150489 		tx_ring->stall_watchdog = 0;
29505779Sxy150489 		tx_ring->recycle_fail = 0;
29515779Sxy150489 	}
29525779Sxy150489 
29535779Sxy150489 	return (result);
29545779Sxy150489 }
29555779Sxy150489 
29565779Sxy150489 
29575779Sxy150489 /*
29585779Sxy150489  * is_valid_mac_addr - Check if the mac address is valid
29595779Sxy150489  */
29605779Sxy150489 static boolean_t
29615779Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
29625779Sxy150489 {
29635779Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
29645779Sxy150489 	const uint8_t addr_test2[6] =
29655779Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
29665779Sxy150489 
29675779Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
29685779Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
29695779Sxy150489 		return (B_FALSE);
29705779Sxy150489 
29715779Sxy150489 	return (B_TRUE);
29725779Sxy150489 }
29735779Sxy150489 
29745779Sxy150489 static boolean_t
29755779Sxy150489 igb_find_mac_address(igb_t *igb)
29765779Sxy150489 {
29775779Sxy150489 	struct e1000_hw *hw = &igb->hw;
29785779Sxy150489 #ifdef __sparc
29795779Sxy150489 	uchar_t *bytes;
29805779Sxy150489 	struct ether_addr sysaddr;
29815779Sxy150489 	uint_t nelts;
29825779Sxy150489 	int err;
29835779Sxy150489 	boolean_t found = B_FALSE;
29845779Sxy150489 
29855779Sxy150489 	/*
29865779Sxy150489 	 * The "vendor's factory-set address" may already have
29875779Sxy150489 	 * been extracted from the chip, but if the property
29885779Sxy150489 	 * "local-mac-address" is set we use that instead.
29895779Sxy150489 	 *
29905779Sxy150489 	 * We check whether it looks like an array of 6
29915779Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
29925779Sxy150489 	 * make sense of it this way, we'll ignore it.
29935779Sxy150489 	 */
29945779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
29955779Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
29965779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
29975779Sxy150489 		if (nelts == ETHERADDRL) {
29985779Sxy150489 			while (nelts--)
29995779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
30005779Sxy150489 			found = B_TRUE;
30015779Sxy150489 		}
30025779Sxy150489 		ddi_prop_free(bytes);
30035779Sxy150489 	}
30045779Sxy150489 
30055779Sxy150489 	/*
30065779Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
30075779Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
30085779Sxy150489 	 */
30095779Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
30105779Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
30115779Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
30125779Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
30135779Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
30145779Sxy150489 				found = B_TRUE;
30155779Sxy150489 			}
30165779Sxy150489 		}
30175779Sxy150489 		ddi_prop_free(bytes);
30185779Sxy150489 	}
30195779Sxy150489 
30205779Sxy150489 	/*
30215779Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
30225779Sxy150489 	 * if we netbooted from this interface), we must use this instead
30235779Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
30245779Sxy150489 	 * get confused by the address changing as Solaris takes over!
30255779Sxy150489 	 */
30265779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
30275779Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
30285779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
30295779Sxy150489 		if (nelts == ETHERADDRL) {
30305779Sxy150489 			while (nelts--)
30315779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
30325779Sxy150489 			found = B_TRUE;
30335779Sxy150489 		}
30345779Sxy150489 		ddi_prop_free(bytes);
30355779Sxy150489 	}
30365779Sxy150489 
30375779Sxy150489 	if (found) {
30385779Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
30395779Sxy150489 		return (B_TRUE);
30405779Sxy150489 	}
30415779Sxy150489 #endif
30425779Sxy150489 
30435779Sxy150489 	/*
30445779Sxy150489 	 * Read the device MAC address from the EEPROM
30455779Sxy150489 	 */
30465779Sxy150489 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
30475779Sxy150489 		return (B_FALSE);
30485779Sxy150489 
30495779Sxy150489 	return (B_TRUE);
30505779Sxy150489 }
30515779Sxy150489 
30525779Sxy150489 #pragma inline(igb_arm_watchdog_timer)
30535779Sxy150489 
30545779Sxy150489 static void
30555779Sxy150489 igb_arm_watchdog_timer(igb_t *igb)
30565779Sxy150489 {
30575779Sxy150489 	/*
30585779Sxy150489 	 * Fire a watchdog timer
30595779Sxy150489 	 */
30605779Sxy150489 	igb->watchdog_tid =
30615779Sxy150489 	    timeout(igb_local_timer,
30625779Sxy150489 	    (void *)igb, 1 * drv_usectohz(1000000));
30635779Sxy150489 
30645779Sxy150489 }
30655779Sxy150489 
30665779Sxy150489 /*
30675779Sxy150489  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
30685779Sxy150489  */
30695779Sxy150489 void
30705779Sxy150489 igb_enable_watchdog_timer(igb_t *igb)
30715779Sxy150489 {
30725779Sxy150489 	mutex_enter(&igb->watchdog_lock);
30735779Sxy150489 
30745779Sxy150489 	if (!igb->watchdog_enable) {
30755779Sxy150489 		igb->watchdog_enable = B_TRUE;
30765779Sxy150489 		igb->watchdog_start = B_TRUE;
30775779Sxy150489 		igb_arm_watchdog_timer(igb);
30785779Sxy150489 	}
30795779Sxy150489 
30805779Sxy150489 	mutex_exit(&igb->watchdog_lock);
30815779Sxy150489 
30825779Sxy150489 }
30835779Sxy150489 
30845779Sxy150489 /*
30855779Sxy150489  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
30865779Sxy150489  */
30875779Sxy150489 void
30885779Sxy150489 igb_disable_watchdog_timer(igb_t *igb)
30895779Sxy150489 {
30905779Sxy150489 	timeout_id_t tid;
30915779Sxy150489 
30925779Sxy150489 	mutex_enter(&igb->watchdog_lock);
30935779Sxy150489 
30945779Sxy150489 	igb->watchdog_enable = B_FALSE;
30955779Sxy150489 	igb->watchdog_start = B_FALSE;
30965779Sxy150489 	tid = igb->watchdog_tid;
30975779Sxy150489 	igb->watchdog_tid = 0;
30985779Sxy150489 
30995779Sxy150489 	mutex_exit(&igb->watchdog_lock);
31005779Sxy150489 
31015779Sxy150489 	if (tid != 0)
31025779Sxy150489 		(void) untimeout(tid);
31035779Sxy150489 
31045779Sxy150489 }
31055779Sxy150489 
31065779Sxy150489 /*
31075779Sxy150489  * igb_start_watchdog_timer - Start the driver watchdog timer
31085779Sxy150489  */
31095779Sxy150489 static void
31105779Sxy150489 igb_start_watchdog_timer(igb_t *igb)
31115779Sxy150489 {
31125779Sxy150489 	mutex_enter(&igb->watchdog_lock);
31135779Sxy150489 
31145779Sxy150489 	if (igb->watchdog_enable) {
31155779Sxy150489 		if (!igb->watchdog_start) {
31165779Sxy150489 			igb->watchdog_start = B_TRUE;
31175779Sxy150489 			igb_arm_watchdog_timer(igb);
31185779Sxy150489 		}
31195779Sxy150489 	}
31205779Sxy150489 
31215779Sxy150489 	mutex_exit(&igb->watchdog_lock);
31225779Sxy150489 }
31235779Sxy150489 
31245779Sxy150489 /*
31255779Sxy150489  * igb_restart_watchdog_timer - Restart the driver watchdog timer
31265779Sxy150489  */
31275779Sxy150489 static void
31285779Sxy150489 igb_restart_watchdog_timer(igb_t *igb)
31295779Sxy150489 {
31305779Sxy150489 	mutex_enter(&igb->watchdog_lock);
31315779Sxy150489 
31325779Sxy150489 	if (igb->watchdog_start)
31335779Sxy150489 		igb_arm_watchdog_timer(igb);
31345779Sxy150489 
31355779Sxy150489 	mutex_exit(&igb->watchdog_lock);
31365779Sxy150489 }
31375779Sxy150489 
31385779Sxy150489 /*
31395779Sxy150489  * igb_stop_watchdog_timer - Stop the driver watchdog timer
31405779Sxy150489  */
31415779Sxy150489 static void
31425779Sxy150489 igb_stop_watchdog_timer(igb_t *igb)
31435779Sxy150489 {
31445779Sxy150489 	timeout_id_t tid;
31455779Sxy150489 
31465779Sxy150489 	mutex_enter(&igb->watchdog_lock);
31475779Sxy150489 
31485779Sxy150489 	igb->watchdog_start = B_FALSE;
31495779Sxy150489 	tid = igb->watchdog_tid;
31505779Sxy150489 	igb->watchdog_tid = 0;
31515779Sxy150489 
31525779Sxy150489 	mutex_exit(&igb->watchdog_lock);
31535779Sxy150489 
31545779Sxy150489 	if (tid != 0)
31555779Sxy150489 		(void) untimeout(tid);
31565779Sxy150489 }
31575779Sxy150489 
31585779Sxy150489 /*
31595779Sxy150489  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
31605779Sxy150489  */
31615779Sxy150489 static void
31625779Sxy150489 igb_disable_adapter_interrupts(igb_t *igb)
31635779Sxy150489 {
31645779Sxy150489 	struct e1000_hw *hw = &igb->hw;
31655779Sxy150489 
31665779Sxy150489 	/*
31675779Sxy150489 	 * Set the IMC register to mask all the interrupts,
31685779Sxy150489 	 * including the tx interrupts.
31695779Sxy150489 	 */
3170*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMC, ~0);
3171*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
31725779Sxy150489 
31735779Sxy150489 	/*
31745779Sxy150489 	 * Additional disabling for MSI-X
31755779Sxy150489 	 */
31765779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3177*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMC, ~0);
3178*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, 0);
3179*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAM, 0);
31805779Sxy150489 	}
31815779Sxy150489 
31825779Sxy150489 	E1000_WRITE_FLUSH(hw);
31835779Sxy150489 }
31845779Sxy150489 
31855779Sxy150489 /*
3186*8571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82576 - Enable NIC interrupts for 82576
31875779Sxy150489  */
31885779Sxy150489 static void
3189*8571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82576(igb_t *igb)
3190*8571SChenlu.Chen@Sun.COM {
3191*8571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
3192*8571SChenlu.Chen@Sun.COM 
3193*8571SChenlu.Chen@Sun.COM 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3194*8571SChenlu.Chen@Sun.COM 
3195*8571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI-X */
3196*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
3197*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
3198*8571SChenlu.Chen@Sun.COM 		igb->ims_mask = E1000_IMS_LSC;
3199*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
3200*8571SChenlu.Chen@Sun.COM 	} else {
3201*8571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI and legacy */
3202*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
3203*8571SChenlu.Chen@Sun.COM 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
3204*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS,
3205*8571SChenlu.Chen@Sun.COM 		    (IMS_ENABLE_MASK | E1000_IMS_TXQE));
3206*8571SChenlu.Chen@Sun.COM 	}
3207*8571SChenlu.Chen@Sun.COM 
3208*8571SChenlu.Chen@Sun.COM 	/* Disable auto-mask for ICR interrupt bits */
3209*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
3210*8571SChenlu.Chen@Sun.COM 
3211*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_FLUSH(hw);
3212*8571SChenlu.Chen@Sun.COM }
3213*8571SChenlu.Chen@Sun.COM 
3214*8571SChenlu.Chen@Sun.COM /*
3215*8571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82575 - Enable NIC interrupts for 82575
3216*8571SChenlu.Chen@Sun.COM  */
3217*8571SChenlu.Chen@Sun.COM static void
3218*8571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82575(igb_t *igb)
32195779Sxy150489 {
32205779Sxy150489 	struct e1000_hw *hw = &igb->hw;
32215779Sxy150489 	uint32_t reg;
32225779Sxy150489 
32235779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
32245779Sxy150489 		/* Interrupt enabling for MSI-X */
32255779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
32265779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
32278275SEric Cheng 		igb->ims_mask = E1000_IMS_LSC;
32285779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
32295779Sxy150489 
32305779Sxy150489 		/* Enable MSI-X PBA support */
32315779Sxy150489 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
32325779Sxy150489 		reg |= E1000_CTRL_EXT_PBA_CLR;
32335779Sxy150489 
32345779Sxy150489 		/* Non-selective interrupt clear-on-read */
32355779Sxy150489 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
32365779Sxy150489 
32375779Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
32385779Sxy150489 	} else {
32395779Sxy150489 		/* Interrupt enabling for MSI and legacy */
32408275SEric Cheng 		igb->ims_mask = IMS_ENABLE_MASK;
32415779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
32425779Sxy150489 	}
32435779Sxy150489 
32445779Sxy150489 	E1000_WRITE_FLUSH(hw);
32455779Sxy150489 }
32465779Sxy150489 
32475779Sxy150489 /*
32485779Sxy150489  * Loopback Support
32495779Sxy150489  */
32505779Sxy150489 static lb_property_t lb_normal =
32515779Sxy150489 	{ normal,	"normal",	IGB_LB_NONE		};
32525779Sxy150489 static lb_property_t lb_external =
32535779Sxy150489 	{ external,	"External",	IGB_LB_EXTERNAL		};
32545779Sxy150489 static lb_property_t lb_mac =
32555779Sxy150489 	{ internal,	"MAC",		IGB_LB_INTERNAL_MAC	};
32565779Sxy150489 static lb_property_t lb_phy =
32575779Sxy150489 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
32585779Sxy150489 static lb_property_t lb_serdes =
32595779Sxy150489 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
32605779Sxy150489 
32615779Sxy150489 enum ioc_reply
32625779Sxy150489 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
32635779Sxy150489 {
32645779Sxy150489 	lb_info_sz_t *lbsp;
32655779Sxy150489 	lb_property_t *lbpp;
32665779Sxy150489 	struct e1000_hw *hw;
32675779Sxy150489 	uint32_t *lbmp;
32685779Sxy150489 	uint32_t size;
32695779Sxy150489 	uint32_t value;
32705779Sxy150489 
32715779Sxy150489 	hw = &igb->hw;
32725779Sxy150489 
32735779Sxy150489 	if (mp->b_cont == NULL)
32745779Sxy150489 		return (IOC_INVAL);
32755779Sxy150489 
32765779Sxy150489 	switch (iocp->ioc_cmd) {
32775779Sxy150489 	default:
32785779Sxy150489 		return (IOC_INVAL);
32795779Sxy150489 
32805779Sxy150489 	case LB_GET_INFO_SIZE:
32815779Sxy150489 		size = sizeof (lb_info_sz_t);
32825779Sxy150489 		if (iocp->ioc_count != size)
32835779Sxy150489 			return (IOC_INVAL);
32845779Sxy150489 
32855779Sxy150489 		value = sizeof (lb_normal);
32865779Sxy150489 		value += sizeof (lb_mac);
32875779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
32885779Sxy150489 			value += sizeof (lb_phy);
32895779Sxy150489 		else
32905779Sxy150489 			value += sizeof (lb_serdes);
32915779Sxy150489 		value += sizeof (lb_external);
32925779Sxy150489 
32935779Sxy150489 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
32945779Sxy150489 		*lbsp = value;
32955779Sxy150489 		break;
32965779Sxy150489 
32975779Sxy150489 	case LB_GET_INFO:
32985779Sxy150489 		value = sizeof (lb_normal);
32995779Sxy150489 		value += sizeof (lb_mac);
33005779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
33015779Sxy150489 			value += sizeof (lb_phy);
33025779Sxy150489 		else
33035779Sxy150489 			value += sizeof (lb_serdes);
33045779Sxy150489 		value += sizeof (lb_external);
33055779Sxy150489 
33065779Sxy150489 		size = value;
33075779Sxy150489 		if (iocp->ioc_count != size)
33085779Sxy150489 			return (IOC_INVAL);
33095779Sxy150489 
33105779Sxy150489 		value = 0;
33115779Sxy150489 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
33125779Sxy150489 
33135779Sxy150489 		lbpp[value++] = lb_normal;
33145779Sxy150489 		lbpp[value++] = lb_mac;
33155779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
33165779Sxy150489 			lbpp[value++] = lb_phy;
33175779Sxy150489 		else
33185779Sxy150489 			lbpp[value++] = lb_serdes;
33195779Sxy150489 		lbpp[value++] = lb_external;
33205779Sxy150489 		break;
33215779Sxy150489 
33225779Sxy150489 	case LB_GET_MODE:
33235779Sxy150489 		size = sizeof (uint32_t);
33245779Sxy150489 		if (iocp->ioc_count != size)
33255779Sxy150489 			return (IOC_INVAL);
33265779Sxy150489 
33275779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
33285779Sxy150489 		*lbmp = igb->loopback_mode;
33295779Sxy150489 		break;
33305779Sxy150489 
33315779Sxy150489 	case LB_SET_MODE:
33325779Sxy150489 		size = 0;
33335779Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
33345779Sxy150489 			return (IOC_INVAL);
33355779Sxy150489 
33365779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
33375779Sxy150489 		if (!igb_set_loopback_mode(igb, *lbmp))
33385779Sxy150489 			return (IOC_INVAL);
33395779Sxy150489 		break;
33405779Sxy150489 	}
33415779Sxy150489 
33425779Sxy150489 	iocp->ioc_count = size;
33435779Sxy150489 	iocp->ioc_error = 0;
33445779Sxy150489 
33456624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
33466624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
33476624Sgl147354 		return (IOC_INVAL);
33486624Sgl147354 	}
33496624Sgl147354 
33505779Sxy150489 	return (IOC_REPLY);
33515779Sxy150489 }
33525779Sxy150489 
33535779Sxy150489 /*
33545779Sxy150489  * igb_set_loopback_mode - Setup loopback based on the loopback mode
33555779Sxy150489  */
33565779Sxy150489 static boolean_t
33575779Sxy150489 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
33585779Sxy150489 {
33595779Sxy150489 	struct e1000_hw *hw;
33605779Sxy150489 
33615779Sxy150489 	if (mode == igb->loopback_mode)
33625779Sxy150489 		return (B_TRUE);
33635779Sxy150489 
33645779Sxy150489 	hw = &igb->hw;
33655779Sxy150489 
33665779Sxy150489 	igb->loopback_mode = mode;
33675779Sxy150489 
33685779Sxy150489 	if (mode == IGB_LB_NONE) {
33695779Sxy150489 		/* Reset the chip */
33705779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_TRUE;
33715779Sxy150489 		(void) igb_reset(igb);
33725779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_FALSE;
33735779Sxy150489 		return (B_TRUE);
33745779Sxy150489 	}
33755779Sxy150489 
33765779Sxy150489 	mutex_enter(&igb->gen_lock);
33775779Sxy150489 
33785779Sxy150489 	switch (mode) {
33795779Sxy150489 	default:
33805779Sxy150489 		mutex_exit(&igb->gen_lock);
33815779Sxy150489 		return (B_FALSE);
33825779Sxy150489 
33835779Sxy150489 	case IGB_LB_EXTERNAL:
33845779Sxy150489 		igb_set_external_loopback(igb);
33855779Sxy150489 		break;
33865779Sxy150489 
33875779Sxy150489 	case IGB_LB_INTERNAL_MAC:
33885779Sxy150489 		igb_set_internal_mac_loopback(igb);
33895779Sxy150489 		break;
33905779Sxy150489 
33915779Sxy150489 	case IGB_LB_INTERNAL_PHY:
33925779Sxy150489 		igb_set_internal_phy_loopback(igb);
33935779Sxy150489 		break;
33945779Sxy150489 
33955779Sxy150489 	case IGB_LB_INTERNAL_SERDES:
33965779Sxy150489 		igb_set_internal_serdes_loopback(igb);
33975779Sxy150489 		break;
33985779Sxy150489 	}
33995779Sxy150489 
34005779Sxy150489 	mutex_exit(&igb->gen_lock);
34015779Sxy150489 
34025779Sxy150489 	return (B_TRUE);
34035779Sxy150489 }
34045779Sxy150489 
34055779Sxy150489 /*
34065779Sxy150489  * igb_set_external_loopback - Set the external loopback mode
34075779Sxy150489  */
34085779Sxy150489 static void
34095779Sxy150489 igb_set_external_loopback(igb_t *igb)
34105779Sxy150489 {
34115779Sxy150489 	struct e1000_hw *hw;
34125779Sxy150489 
34135779Sxy150489 	hw = &igb->hw;
34145779Sxy150489 
34155779Sxy150489 	/* Set phy to known state */
34165779Sxy150489 	(void) e1000_phy_hw_reset(hw);
34175779Sxy150489 
34185779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
34195779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x9, 0x1b00);
34205779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
34215779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
34225779Sxy150489 }
34235779Sxy150489 
34245779Sxy150489 /*
34255779Sxy150489  * igb_set_internal_mac_loopback - Set the internal MAC loopback mode
34265779Sxy150489  */
34275779Sxy150489 static void
34285779Sxy150489 igb_set_internal_mac_loopback(igb_t *igb)
34295779Sxy150489 {
34305779Sxy150489 	struct e1000_hw *hw;
34315779Sxy150489 	uint32_t ctrl;
34325779Sxy150489 	uint32_t rctl;
34335779Sxy150489 
34345779Sxy150489 	hw = &igb->hw;
34355779Sxy150489 
34365779Sxy150489 	/* Set the Receive Control register */
34375779Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
34385779Sxy150489 	rctl &= ~E1000_RCTL_LBM_TCVR;
34395779Sxy150489 	rctl |= E1000_RCTL_LBM_MAC;
34405779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
34415779Sxy150489 
34425779Sxy150489 	/* Set the Device Control register */
34435779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
34445779Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
34455779Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force link up */
34465779Sxy150489 	    E1000_CTRL_FRCSPD |		/* Force speed */
34475779Sxy150489 	    E1000_CTRL_FRCDPX |		/* Force duplex */
34485779Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force speed to 1000 */
34495779Sxy150489 	    E1000_CTRL_FD);		/* Force full duplex */
34505779Sxy150489 	ctrl &= ~E1000_CTRL_ILOS;	/* Clear ILOS when there's a link */
34515779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
34525779Sxy150489 }
34535779Sxy150489 
34545779Sxy150489 /*
34555779Sxy150489  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
34565779Sxy150489  */
34575779Sxy150489 static void
34585779Sxy150489 igb_set_internal_phy_loopback(igb_t *igb)
34595779Sxy150489 {
34605779Sxy150489 	struct e1000_hw *hw;
34615779Sxy150489 	uint32_t ctrl_ext;
34625779Sxy150489 	uint16_t phy_ctrl;
34635779Sxy150489 	uint16_t phy_pconf;
34645779Sxy150489 
34655779Sxy150489 	hw = &igb->hw;
34665779Sxy150489 
34675779Sxy150489 	/* Set link mode to PHY (00b) in the Extended Control register */
34685779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
34695779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
34705779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
34715779Sxy150489 
34725779Sxy150489 	/*
34735779Sxy150489 	 * Set PHY control register (0x4140):
34745779Sxy150489 	 *    Set full duplex mode
34755779Sxy150489 	 *    Set loopback bit
34765779Sxy150489 	 *    Clear auto-neg enable bit
34775779Sxy150489 	 *    Set PHY speed
34785779Sxy150489 	 */
34795779Sxy150489 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
34805779Sxy150489 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
34815779Sxy150489 
34825779Sxy150489 	/* Set the link disable bit in the Port Configuration register */
34835779Sxy150489 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
34845779Sxy150489 	phy_pconf |= (uint16_t)1 << 14;
34855779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
34865779Sxy150489 }
34875779Sxy150489 
34885779Sxy150489 /*
34895779Sxy150489  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
34905779Sxy150489  */
34915779Sxy150489 static void
34925779Sxy150489 igb_set_internal_serdes_loopback(igb_t *igb)
34935779Sxy150489 {
34945779Sxy150489 	struct e1000_hw *hw;
34955779Sxy150489 	uint32_t ctrl_ext;
34965779Sxy150489 	uint32_t ctrl;
34975779Sxy150489 	uint32_t pcs_lctl;
34985779Sxy150489 	uint32_t connsw;
34995779Sxy150489 
35005779Sxy150489 	hw = &igb->hw;
35015779Sxy150489 
35025779Sxy150489 	/* Set link mode to SerDes (11b) in the Extended Control register */
35035779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
35045779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
35055779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
35065779Sxy150489 
35075779Sxy150489 	/* Configure the SerDes to loopback */
35085779Sxy150489 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
35095779Sxy150489 
35105779Sxy150489 	/* Set Device Control register */
35115779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
35125779Sxy150489 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
35135779Sxy150489 	    E1000_CTRL_SLU);		/* Force link up */
35145779Sxy150489 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
35155779Sxy150489 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
35165779Sxy150489 	    E1000_CTRL_LRST);		/* Clear link reset */
35175779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
35185779Sxy150489 
35195779Sxy150489 	/* Set PCS Link Control register */
35205779Sxy150489 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
35215779Sxy150489 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
35225779Sxy150489 	    E1000_PCS_LCTL_FSD |
35235779Sxy150489 	    E1000_PCS_LCTL_FDV_FULL |
35245779Sxy150489 	    E1000_PCS_LCTL_FLV_LINK_UP);
35255779Sxy150489 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
35265779Sxy150489 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
35275779Sxy150489 
35285779Sxy150489 	/* Set the Copper/Fiber Switch Control - CONNSW register */
35295779Sxy150489 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
35305779Sxy150489 	connsw &= ~E1000_CONNSW_ENRGSRC;
35315779Sxy150489 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
35325779Sxy150489 }
35335779Sxy150489 
35345779Sxy150489 #pragma inline(igb_intr_rx_work)
35355779Sxy150489 /*
35365779Sxy150489  * igb_intr_rx_work - rx processing of ISR
35375779Sxy150489  */
35385779Sxy150489 static void
35395779Sxy150489 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
35405779Sxy150489 {
35415779Sxy150489 	mblk_t *mp;
35425779Sxy150489 
35435779Sxy150489 	mutex_enter(&rx_ring->rx_lock);
35448275SEric Cheng 	mp = igb_rx(rx_ring, IGB_NO_POLL);
35455779Sxy150489 	mutex_exit(&rx_ring->rx_lock);
35465779Sxy150489 
35475779Sxy150489 	if (mp != NULL)
35488275SEric Cheng 		mac_rx_ring(rx_ring->igb->mac_hdl, rx_ring->ring_handle, mp,
35498275SEric Cheng 		    rx_ring->ring_gen_num);
35505779Sxy150489 }
35515779Sxy150489 
35525779Sxy150489 #pragma inline(igb_intr_tx_work)
35535779Sxy150489 /*
35545779Sxy150489  * igb_intr_tx_work - tx processing of ISR
35555779Sxy150489  */
35565779Sxy150489 static void
35575779Sxy150489 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
35585779Sxy150489 {
35595779Sxy150489 	/* Recycle the tx descriptors */
35605779Sxy150489 	tx_ring->tx_recycle(tx_ring);
35615779Sxy150489 
35625779Sxy150489 	/* Schedule the re-transmit */
35635779Sxy150489 	if (tx_ring->reschedule &&
35645779Sxy150489 	    (tx_ring->tbd_free >= tx_ring->resched_thresh)) {
35655779Sxy150489 		tx_ring->reschedule = B_FALSE;
35668275SEric Cheng 		mac_tx_ring_update(tx_ring->igb->mac_hdl, tx_ring->ring_handle);
35675779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
35685779Sxy150489 	}
35695779Sxy150489 }
35705779Sxy150489 
35718275SEric Cheng #pragma inline(igb_intr_link_work)
35725779Sxy150489 /*
35738275SEric Cheng  * igb_intr_link_work - link-status-change processing of ISR
35745779Sxy150489  */
35755779Sxy150489 static void
35768275SEric Cheng igb_intr_link_work(igb_t *igb)
35775779Sxy150489 {
35785779Sxy150489 	boolean_t link_changed;
35795779Sxy150489 
35805779Sxy150489 	igb_stop_watchdog_timer(igb);
35815779Sxy150489 
35825779Sxy150489 	mutex_enter(&igb->gen_lock);
35835779Sxy150489 
35845779Sxy150489 	/*
35855779Sxy150489 	 * Because we got a link-status-change interrupt, force
35865779Sxy150489 	 * e1000_check_for_link() to look at phy
35875779Sxy150489 	 */
35885779Sxy150489 	igb->hw.mac.get_link_status = B_TRUE;
35895779Sxy150489 
35905779Sxy150489 	/* igb_link_check takes care of link status change */
35915779Sxy150489 	link_changed = igb_link_check(igb);
35925779Sxy150489 
35935779Sxy150489 	/* Get new phy state */
35945779Sxy150489 	igb_get_phy_state(igb);
35955779Sxy150489 
35965779Sxy150489 	mutex_exit(&igb->gen_lock);
35975779Sxy150489 
35985779Sxy150489 	if (link_changed)
35995779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
36005779Sxy150489 
36015779Sxy150489 	igb_start_watchdog_timer(igb);
36025779Sxy150489 }
36035779Sxy150489 
36045779Sxy150489 /*
36055779Sxy150489  * igb_intr_legacy - Interrupt handler for legacy interrupts
36065779Sxy150489  */
36075779Sxy150489 static uint_t
36085779Sxy150489 igb_intr_legacy(void *arg1, void *arg2)
36095779Sxy150489 {
36105779Sxy150489 	igb_t *igb = (igb_t *)arg1;
36115779Sxy150489 	igb_tx_ring_t *tx_ring;
36125779Sxy150489 	uint32_t icr;
36135779Sxy150489 	mblk_t *mp;
36145779Sxy150489 	boolean_t tx_reschedule;
36155779Sxy150489 	boolean_t link_changed;
36165779Sxy150489 	uint_t result;
36175779Sxy150489 
36185779Sxy150489 	_NOTE(ARGUNUSED(arg2));
36195779Sxy150489 
36205779Sxy150489 	mutex_enter(&igb->gen_lock);
36215779Sxy150489 
36225779Sxy150489 	if (igb->igb_state & IGB_SUSPENDED) {
36235779Sxy150489 		mutex_exit(&igb->gen_lock);
36245779Sxy150489 		return (DDI_INTR_UNCLAIMED);
36255779Sxy150489 	}
36265779Sxy150489 
36275779Sxy150489 	mp = NULL;
36285779Sxy150489 	tx_reschedule = B_FALSE;
36295779Sxy150489 	link_changed = B_FALSE;
36305779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
36315779Sxy150489 
36325779Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
36335779Sxy150489 		/*
36345779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
36355779Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
36365779Sxy150489 		 * look for work to do.
36375779Sxy150489 		 */
36385779Sxy150489 		ASSERT(igb->num_rx_rings == 1);
36395779Sxy150489 		ASSERT(igb->num_tx_rings == 1);
36405779Sxy150489 
3641*8571SChenlu.Chen@Sun.COM 		/* Make sure all interrupt causes cleared */
3642*8571SChenlu.Chen@Sun.COM 		(void) E1000_READ_REG(&igb->hw, E1000_EICR);
3643*8571SChenlu.Chen@Sun.COM 
36445779Sxy150489 		if (icr & E1000_ICR_RXT0) {
36458275SEric Cheng 			mp = igb_rx(&igb->rx_rings[0], IGB_NO_POLL);
36465779Sxy150489 		}
36475779Sxy150489 
36485779Sxy150489 		if (icr & E1000_ICR_TXDW) {
36495779Sxy150489 			tx_ring = &igb->tx_rings[0];
36505779Sxy150489 
36515779Sxy150489 			/* Recycle the tx descriptors */
36525779Sxy150489 			tx_ring->tx_recycle(tx_ring);
36535779Sxy150489 
36545779Sxy150489 			/* Schedule the re-transmit */
36555779Sxy150489 			tx_reschedule = (tx_ring->reschedule &&
36565779Sxy150489 			    (tx_ring->tbd_free >= tx_ring->resched_thresh));
36575779Sxy150489 		}
36585779Sxy150489 
36595779Sxy150489 		if (icr & E1000_ICR_LSC) {
36605779Sxy150489 			/*
36615779Sxy150489 			 * Because we got a link-status-change interrupt, force
36625779Sxy150489 			 * e1000_check_for_link() to look at phy
36635779Sxy150489 			 */
36645779Sxy150489 			igb->hw.mac.get_link_status = B_TRUE;
36655779Sxy150489 
36665779Sxy150489 			/* igb_link_check takes care of link status change */
36675779Sxy150489 			link_changed = igb_link_check(igb);
36685779Sxy150489 
36695779Sxy150489 			/* Get new phy state */
36705779Sxy150489 			igb_get_phy_state(igb);
36715779Sxy150489 		}
36725779Sxy150489 
36735779Sxy150489 		result = DDI_INTR_CLAIMED;
36745779Sxy150489 	} else {
36755779Sxy150489 		/*
36765779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
36775779Sxy150489 		 * Don't claim this interrupt.
36785779Sxy150489 		 */
36795779Sxy150489 		result = DDI_INTR_UNCLAIMED;
36805779Sxy150489 	}
36815779Sxy150489 
36825779Sxy150489 	mutex_exit(&igb->gen_lock);
36835779Sxy150489 
36845779Sxy150489 	/*
36855779Sxy150489 	 * Do the following work outside of the gen_lock
36865779Sxy150489 	 */
36875779Sxy150489 	if (mp != NULL)
36885779Sxy150489 		mac_rx(igb->mac_hdl, NULL, mp);
36895779Sxy150489 
36905779Sxy150489 	if (tx_reschedule)  {
36915779Sxy150489 		tx_ring->reschedule = B_FALSE;
36928275SEric Cheng 		mac_tx_ring_update(igb->mac_hdl, tx_ring->ring_handle);
36935779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
36945779Sxy150489 	}
36955779Sxy150489 
36965779Sxy150489 	if (link_changed)
36975779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
36985779Sxy150489 
36995779Sxy150489 	return (result);
37005779Sxy150489 }
37015779Sxy150489 
37025779Sxy150489 /*
37035779Sxy150489  * igb_intr_msi - Interrupt handler for MSI
37045779Sxy150489  */
37055779Sxy150489 static uint_t
37065779Sxy150489 igb_intr_msi(void *arg1, void *arg2)
37075779Sxy150489 {
37085779Sxy150489 	igb_t *igb = (igb_t *)arg1;
37095779Sxy150489 	uint32_t icr;
37105779Sxy150489 
37115779Sxy150489 	_NOTE(ARGUNUSED(arg2));
37125779Sxy150489 
37135779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
37145779Sxy150489 
3715*8571SChenlu.Chen@Sun.COM 	/* Make sure all interrupt causes cleared */
3716*8571SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(&igb->hw, E1000_EICR);
3717*8571SChenlu.Chen@Sun.COM 
37185779Sxy150489 	/*
37195779Sxy150489 	 * For MSI interrupt, we have only one vector,
37205779Sxy150489 	 * so we have only one rx ring and one tx ring enabled.
37215779Sxy150489 	 */
37225779Sxy150489 	ASSERT(igb->num_rx_rings == 1);
37235779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
37245779Sxy150489 
37255779Sxy150489 	if (icr & E1000_ICR_RXT0) {
37265779Sxy150489 		igb_intr_rx_work(&igb->rx_rings[0]);
37275779Sxy150489 	}
37285779Sxy150489 
37295779Sxy150489 	if (icr & E1000_ICR_TXDW) {
37305779Sxy150489 		igb_intr_tx_work(&igb->tx_rings[0]);
37315779Sxy150489 	}
37325779Sxy150489 
37335779Sxy150489 	if (icr & E1000_ICR_LSC) {
37348275SEric Cheng 		igb_intr_link_work(igb);
37355779Sxy150489 	}
37365779Sxy150489 
37375779Sxy150489 	return (DDI_INTR_CLAIMED);
37385779Sxy150489 }
37395779Sxy150489 
37405779Sxy150489 /*
37415779Sxy150489  * igb_intr_rx - Interrupt handler for rx
37425779Sxy150489  */
37435779Sxy150489 static uint_t
37445779Sxy150489 igb_intr_rx(void *arg1, void *arg2)
37455779Sxy150489 {
37465779Sxy150489 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
37475779Sxy150489 
37485779Sxy150489 	_NOTE(ARGUNUSED(arg2));
37495779Sxy150489 
37505779Sxy150489 	/*
37515779Sxy150489 	 * Only used via MSI-X vector so don't check cause bits
37525779Sxy150489 	 * and only clean the given ring.
37535779Sxy150489 	 */
37545779Sxy150489 	igb_intr_rx_work(rx_ring);
37555779Sxy150489 
37565779Sxy150489 	return (DDI_INTR_CLAIMED);
37575779Sxy150489 }
37585779Sxy150489 
37595779Sxy150489 /*
37608275SEric Cheng  * igb_intr_tx - Interrupt handler for tx
37618275SEric Cheng  */
37628275SEric Cheng static uint_t
37638275SEric Cheng igb_intr_tx(void *arg1, void *arg2)
37648275SEric Cheng {
37658275SEric Cheng 	igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg1;
37668275SEric Cheng 
37678275SEric Cheng 	_NOTE(ARGUNUSED(arg2));
37688275SEric Cheng 
37698275SEric Cheng 	/*
37708275SEric Cheng 	 * Only used via MSI-X vector so don't check cause bits
37718275SEric Cheng 	 * and only clean the given ring.
37728275SEric Cheng 	 */
37738275SEric Cheng 	igb_intr_tx_work(tx_ring);
37748275SEric Cheng 
37758275SEric Cheng 	return (DDI_INTR_CLAIMED);
37768275SEric Cheng }
37778275SEric Cheng 
37788275SEric Cheng /*
37795779Sxy150489  * igb_intr_tx_other - Interrupt handler for both tx and other
37805779Sxy150489  *
37815779Sxy150489  */
37825779Sxy150489 static uint_t
37835779Sxy150489 igb_intr_tx_other(void *arg1, void *arg2)
37845779Sxy150489 {
37855779Sxy150489 	igb_t *igb = (igb_t *)arg1;
37865779Sxy150489 	uint32_t icr;
37875779Sxy150489 
37885779Sxy150489 	_NOTE(ARGUNUSED(arg2));
37895779Sxy150489 
37905779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
37915779Sxy150489 
37925779Sxy150489 	/*
37938275SEric Cheng 	 * Look for tx reclaiming work first. Remember, in the
37948275SEric Cheng 	 * case of only interrupt sharing, only one tx ring is
37958275SEric Cheng 	 * used
37965779Sxy150489 	 */
37975779Sxy150489 	igb_intr_tx_work(&igb->tx_rings[0]);
37985779Sxy150489 
37995779Sxy150489 	/*
38008275SEric Cheng 	 * Need check cause bits and only link change will
38018275SEric Cheng 	 * be processed
38025779Sxy150489 	 */
38035779Sxy150489 	if (icr & E1000_ICR_LSC) {
38048275SEric Cheng 		igb_intr_link_work(igb);
38055779Sxy150489 	}
38065779Sxy150489 
38075779Sxy150489 	return (DDI_INTR_CLAIMED);
38085779Sxy150489 }
38095779Sxy150489 
38105779Sxy150489 /*
38115779Sxy150489  * igb_alloc_intrs - Allocate interrupts for the driver
38125779Sxy150489  *
38135779Sxy150489  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
38145779Sxy150489  * if not successful, try Legacy.
38155779Sxy150489  * igb->intr_force can be used to force sequence to start with
38165779Sxy150489  * any of the 3 types.
38175779Sxy150489  * If MSI-X is not used, number of tx/rx rings is forced to 1.
38185779Sxy150489  */
38195779Sxy150489 static int
38205779Sxy150489 igb_alloc_intrs(igb_t *igb)
38215779Sxy150489 {
38225779Sxy150489 	dev_info_t *devinfo;
38235779Sxy150489 	int intr_types;
38245779Sxy150489 	int rc;
38255779Sxy150489 
38265779Sxy150489 	devinfo = igb->dip;
38275779Sxy150489 
38285779Sxy150489 	/* Get supported interrupt types */
38295779Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
38305779Sxy150489 
38315779Sxy150489 	if (rc != DDI_SUCCESS) {
38325779Sxy150489 		igb_log(igb,
38335779Sxy150489 		    "Get supported interrupt types failed: %d", rc);
38345779Sxy150489 		return (IGB_FAILURE);
38355779Sxy150489 	}
38365779Sxy150489 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
38375779Sxy150489 
38385779Sxy150489 	igb->intr_type = 0;
38395779Sxy150489 
38405779Sxy150489 	/* Install MSI-X interrupts */
38415779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
38425779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSIX)) {
38437072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSIX);
38445779Sxy150489 
38455779Sxy150489 		if (rc == IGB_SUCCESS)
38465779Sxy150489 			return (IGB_SUCCESS);
38475779Sxy150489 
38485779Sxy150489 		igb_log(igb,
38495779Sxy150489 		    "Allocate MSI-X failed, trying MSI interrupts...");
38505779Sxy150489 	}
38515779Sxy150489 
38525779Sxy150489 	/* MSI-X not used, force rings to 1 */
38535779Sxy150489 	igb->num_rx_rings = 1;
38545779Sxy150489 	igb->num_tx_rings = 1;
38555779Sxy150489 	igb_log(igb,
38565779Sxy150489 	    "MSI-X not used, force rx and tx queue number to 1");
38575779Sxy150489 
38585779Sxy150489 	/* Install MSI interrupts */
38595779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
38605779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSI)) {
38617072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSI);
38625779Sxy150489 
38635779Sxy150489 		if (rc == IGB_SUCCESS)
38645779Sxy150489 			return (IGB_SUCCESS);
38655779Sxy150489 
38665779Sxy150489 		igb_log(igb,
38675779Sxy150489 		    "Allocate MSI failed, trying Legacy interrupts...");
38685779Sxy150489 	}
38695779Sxy150489 
38705779Sxy150489 	/* Install legacy interrupts */
38715779Sxy150489 	if (intr_types & DDI_INTR_TYPE_FIXED) {
38727072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_FIXED);
38735779Sxy150489 
38745779Sxy150489 		if (rc == IGB_SUCCESS)
38755779Sxy150489 			return (IGB_SUCCESS);
38765779Sxy150489 
38775779Sxy150489 		igb_log(igb,
38785779Sxy150489 		    "Allocate Legacy interrupts failed");
38795779Sxy150489 	}
38805779Sxy150489 
38815779Sxy150489 	/* If none of the 3 types succeeded, return failure */
38825779Sxy150489 	return (IGB_FAILURE);
38835779Sxy150489 }
38845779Sxy150489 
38855779Sxy150489 /*
38867072Sxy150489  * igb_alloc_intr_handles - Allocate interrupt handles.
38875779Sxy150489  *
38887072Sxy150489  * For legacy and MSI, only 1 handle is needed.  For MSI-X,
38897072Sxy150489  * if fewer than 2 handles are available, return failure.
38905779Sxy150489  * Upon success, this sets the number of Rx rings to a number that
38917072Sxy150489  * matches the handles available for Rx interrupts.
38925779Sxy150489  */
38935779Sxy150489 static int
38947072Sxy150489 igb_alloc_intr_handles(igb_t *igb, int intr_type)
38955779Sxy150489 {
38965779Sxy150489 	dev_info_t *devinfo;
38978275SEric Cheng 	int orig, request, count, avail, actual;
38988275SEric Cheng 	int diff, minimum;
38995779Sxy150489 	int rc;
39005779Sxy150489 
39015779Sxy150489 	devinfo = igb->dip;
39025779Sxy150489 
39037072Sxy150489 	switch (intr_type) {
39047072Sxy150489 	case DDI_INTR_TYPE_FIXED:
39057072Sxy150489 		request = 1;	/* Request 1 legacy interrupt handle */
39067072Sxy150489 		minimum = 1;
39077072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: legacy");
39087072Sxy150489 		break;
39097072Sxy150489 
39107072Sxy150489 	case DDI_INTR_TYPE_MSI:
39117072Sxy150489 		request = 1;	/* Request 1 MSI interrupt handle */
39127072Sxy150489 		minimum = 1;
39137072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI");
39147072Sxy150489 		break;
39157072Sxy150489 
39167072Sxy150489 	case DDI_INTR_TYPE_MSIX:
39177072Sxy150489 		/*
39188275SEric Cheng 		 * Number of vectors for the adapter is
39198275SEric Cheng 		 * # rx rings + # tx rings
39208275SEric Cheng 		 * One of tx vectors is for tx & other
39217072Sxy150489 		 */
39228275SEric Cheng 		request = igb->num_rx_rings + igb->num_tx_rings;
39238275SEric Cheng 		orig = request;
39247072Sxy150489 		minimum = 2;
39257072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI-X");
39267072Sxy150489 		break;
39277072Sxy150489 
39287072Sxy150489 	default:
39295779Sxy150489 		igb_log(igb,
39307072Sxy150489 		    "invalid call to igb_alloc_intr_handles(): %d\n",
39317072Sxy150489 		    intr_type);
39325779Sxy150489 		return (IGB_FAILURE);
39335779Sxy150489 	}
39347072Sxy150489 	IGB_DEBUGLOG_2(igb, "interrupt handles requested: %d  minimum: %d",
39357072Sxy150489 	    request, minimum);
39367072Sxy150489 
39377072Sxy150489 	/*
39387072Sxy150489 	 * Get number of supported interrupts
39397072Sxy150489 	 */
39407072Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
39417072Sxy150489 	if ((rc != DDI_SUCCESS) || (count < minimum)) {
39425779Sxy150489 		igb_log(igb,
39437072Sxy150489 		    "Get supported interrupt number failed. "
39447072Sxy150489 		    "Return: %d, count: %d", rc, count);
39457072Sxy150489 		return (IGB_FAILURE);
39467072Sxy150489 	}
39477072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts supported: %d", count);
39487072Sxy150489 
39497072Sxy150489 	/*
39507072Sxy150489 	 * Get number of available interrupts
39517072Sxy150489 	 */
39527072Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
39537072Sxy150489 	if ((rc != DDI_SUCCESS) || (avail < minimum)) {
39547072Sxy150489 		igb_log(igb,
39557072Sxy150489 		    "Get available interrupt number failed. "
39565779Sxy150489 		    "Return: %d, available: %d", rc, avail);
39575779Sxy150489 		return (IGB_FAILURE);
39585779Sxy150489 	}
39597072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts available: %d", avail);
39605779Sxy150489 
39615779Sxy150489 	if (avail < request) {
39627072Sxy150489 		igb_log(igb, "Request %d handles, %d available",
39635779Sxy150489 		    request, avail);
39645779Sxy150489 		request = avail;
39655779Sxy150489 	}
39665779Sxy150489 
39675779Sxy150489 	actual = 0;
39685779Sxy150489 	igb->intr_cnt = 0;
39695779Sxy150489 
39707072Sxy150489 	/*
39717072Sxy150489 	 * Allocate an array of interrupt handles
39727072Sxy150489 	 */
39735779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
39745779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
39755779Sxy150489 
39767072Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, intr_type, 0,
39775779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
39785779Sxy150489 	if (rc != DDI_SUCCESS) {
39797072Sxy150489 		igb_log(igb, "Allocate interrupts failed. "
39805779Sxy150489 		    "return: %d, request: %d, actual: %d",
39815779Sxy150489 		    rc, request, actual);
39827072Sxy150489 		goto alloc_handle_fail;
39835779Sxy150489 	}
39847072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts actually allocated: %d", actual);
39855779Sxy150489 
39865779Sxy150489 	igb->intr_cnt = actual;
39875779Sxy150489 
39887072Sxy150489 	if (actual < minimum) {
39897072Sxy150489 		igb_log(igb, "Insufficient interrupt handles allocated: %d",
39907072Sxy150489 		    actual);
39917072Sxy150489 		goto alloc_handle_fail;
39927072Sxy150489 	}
39937072Sxy150489 
39945779Sxy150489 	/*
39958275SEric Cheng 	 * For MSI-X, actual might force us to reduce number of tx & rx rings
39965779Sxy150489 	 */
39978275SEric Cheng 	if ((intr_type == DDI_INTR_TYPE_MSIX) && (orig > actual)) {
39988275SEric Cheng 		diff = orig - actual;
39998275SEric Cheng 		if (diff < igb->num_tx_rings) {
40008275SEric Cheng 			igb_log(igb,
40018275SEric Cheng 			    "MSI-X vectors force Tx queue number to %d",
40028275SEric Cheng 			    igb->num_tx_rings - diff);
40038275SEric Cheng 			igb->num_tx_rings -= diff;
40048275SEric Cheng 		} else {
40058275SEric Cheng 			igb_log(igb,
40068275SEric Cheng 			    "MSI-X vectors force Tx queue number to 1");
40078275SEric Cheng 			igb->num_tx_rings = 1;
40088275SEric Cheng 
40097072Sxy150489 			igb_log(igb,
40107072Sxy150489 			    "MSI-X vectors force Rx queue number to %d",
40118275SEric Cheng 			    actual - 1);
40128275SEric Cheng 			igb->num_rx_rings = actual - 1;
40137072Sxy150489 		}
40145779Sxy150489 	}
40155779Sxy150489 
40167072Sxy150489 	/*
40177072Sxy150489 	 * Get priority for first vector, assume remaining are all the same
40187072Sxy150489 	 */
40195779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
40205779Sxy150489 	if (rc != DDI_SUCCESS) {
40215779Sxy150489 		igb_log(igb,
40225779Sxy150489 		    "Get interrupt priority failed: %d", rc);
40237072Sxy150489 		goto alloc_handle_fail;
40245779Sxy150489 	}
40255779Sxy150489 
40265779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
40275779Sxy150489 	if (rc != DDI_SUCCESS) {
40285779Sxy150489 		igb_log(igb,
40295779Sxy150489 		    "Get interrupt cap failed: %d", rc);
40307072Sxy150489 		goto alloc_handle_fail;
40315779Sxy150489 	}
40325779Sxy150489 
40337072Sxy150489 	igb->intr_type = intr_type;
40345779Sxy150489 
40355779Sxy150489 	return (IGB_SUCCESS);
40365779Sxy150489 
40377072Sxy150489 alloc_handle_fail:
40385779Sxy150489 	igb_rem_intrs(igb);
40395779Sxy150489 
40405779Sxy150489 	return (IGB_FAILURE);
40415779Sxy150489 }
40425779Sxy150489 
40435779Sxy150489 /*
40445779Sxy150489  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
40455779Sxy150489  *
40465779Sxy150489  * Before adding the interrupt handlers, the interrupt vectors have
40475779Sxy150489  * been allocated, and the rx/tx rings have also been allocated.
40485779Sxy150489  */
40495779Sxy150489 static int
40505779Sxy150489 igb_add_intr_handlers(igb_t *igb)
40515779Sxy150489 {
40525779Sxy150489 	igb_rx_ring_t *rx_ring;
40538275SEric Cheng 	igb_tx_ring_t *tx_ring;
40545779Sxy150489 	int vector;
40555779Sxy150489 	int rc;
40565779Sxy150489 	int i;
40575779Sxy150489 
40585779Sxy150489 	vector = 0;
40595779Sxy150489 
40605779Sxy150489 	switch (igb->intr_type) {
40615779Sxy150489 	case DDI_INTR_TYPE_MSIX:
40625779Sxy150489 		/* Add interrupt handler for tx + other */
40638275SEric Cheng 		tx_ring = &igb->tx_rings[0];
40645779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
40655779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_tx_other,
40665779Sxy150489 		    (void *)igb, NULL);
40678275SEric Cheng 
40685779Sxy150489 		if (rc != DDI_SUCCESS) {
40695779Sxy150489 			igb_log(igb,
40705779Sxy150489 			    "Add tx/other interrupt handler failed: %d", rc);
40715779Sxy150489 			return (IGB_FAILURE);
40725779Sxy150489 		}
40738275SEric Cheng 		tx_ring->intr_vector = vector;
40745779Sxy150489 		vector++;
40755779Sxy150489 
40765779Sxy150489 		/* Add interrupt handler for each rx ring */
40775779Sxy150489 		for (i = 0; i < igb->num_rx_rings; i++) {
40785779Sxy150489 			rx_ring = &igb->rx_rings[i];
40795779Sxy150489 
40805779Sxy150489 			rc = ddi_intr_add_handler(igb->htable[vector],
40815779Sxy150489 			    (ddi_intr_handler_t *)igb_intr_rx,
40825779Sxy150489 			    (void *)rx_ring, NULL);
40835779Sxy150489 
40845779Sxy150489 			if (rc != DDI_SUCCESS) {
40855779Sxy150489 				igb_log(igb,
40865779Sxy150489 				    "Add rx interrupt handler failed. "
40875779Sxy150489 				    "return: %d, rx ring: %d", rc, i);
40885779Sxy150489 				for (vector--; vector >= 0; vector--) {
40895779Sxy150489 					(void) ddi_intr_remove_handler(
40905779Sxy150489 					    igb->htable[vector]);
40915779Sxy150489 				}
40925779Sxy150489 				return (IGB_FAILURE);
40935779Sxy150489 			}
40945779Sxy150489 
40955779Sxy150489 			rx_ring->intr_vector = vector;
40965779Sxy150489 
40975779Sxy150489 			vector++;
40985779Sxy150489 		}
40998275SEric Cheng 
41008275SEric Cheng 		/* Add interrupt handler for each tx ring from 2nd ring */
41018275SEric Cheng 		for (i = 1; i < igb->num_tx_rings; i++) {
41028275SEric Cheng 			tx_ring = &igb->tx_rings[i];
41038275SEric Cheng 
41048275SEric Cheng 			rc = ddi_intr_add_handler(igb->htable[vector],
41058275SEric Cheng 			    (ddi_intr_handler_t *)igb_intr_tx,
41068275SEric Cheng 			    (void *)tx_ring, NULL);
41078275SEric Cheng 
41088275SEric Cheng 			if (rc != DDI_SUCCESS) {
41098275SEric Cheng 				igb_log(igb,
41108275SEric Cheng 				    "Add tx interrupt handler failed. "
41118275SEric Cheng 				    "return: %d, tx ring: %d", rc, i);
41128275SEric Cheng 				for (vector--; vector >= 0; vector--) {
41138275SEric Cheng 					(void) ddi_intr_remove_handler(
41148275SEric Cheng 					    igb->htable[vector]);
41158275SEric Cheng 				}
41168275SEric Cheng 				return (IGB_FAILURE);
41178275SEric Cheng 			}
41188275SEric Cheng 
41198275SEric Cheng 			tx_ring->intr_vector = vector;
41208275SEric Cheng 
41218275SEric Cheng 			vector++;
41228275SEric Cheng 		}
41238275SEric Cheng 
41245779Sxy150489 		break;
41255779Sxy150489 
41265779Sxy150489 	case DDI_INTR_TYPE_MSI:
41275779Sxy150489 		/* Add interrupt handlers for the only vector */
41285779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
41295779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_msi,
41305779Sxy150489 		    (void *)igb, NULL);
41315779Sxy150489 
41325779Sxy150489 		if (rc != DDI_SUCCESS) {
41335779Sxy150489 			igb_log(igb,
41345779Sxy150489 			    "Add MSI interrupt handler failed: %d", rc);
41355779Sxy150489 			return (IGB_FAILURE);
41365779Sxy150489 		}
41375779Sxy150489 
41385779Sxy150489 		rx_ring = &igb->rx_rings[0];
41395779Sxy150489 		rx_ring->intr_vector = vector;
41405779Sxy150489 
41415779Sxy150489 		vector++;
41425779Sxy150489 		break;
41435779Sxy150489 
41445779Sxy150489 	case DDI_INTR_TYPE_FIXED:
41455779Sxy150489 		/* Add interrupt handlers for the only vector */
41465779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
41475779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_legacy,
41485779Sxy150489 		    (void *)igb, NULL);
41495779Sxy150489 
41505779Sxy150489 		if (rc != DDI_SUCCESS) {
41515779Sxy150489 			igb_log(igb,
41525779Sxy150489 			    "Add legacy interrupt handler failed: %d", rc);
41535779Sxy150489 			return (IGB_FAILURE);
41545779Sxy150489 		}
41555779Sxy150489 
41565779Sxy150489 		rx_ring = &igb->rx_rings[0];
41575779Sxy150489 		rx_ring->intr_vector = vector;
41585779Sxy150489 
41595779Sxy150489 		vector++;
41605779Sxy150489 		break;
41615779Sxy150489 
41625779Sxy150489 	default:
41635779Sxy150489 		return (IGB_FAILURE);
41645779Sxy150489 	}
41655779Sxy150489 
41665779Sxy150489 	ASSERT(vector == igb->intr_cnt);
41675779Sxy150489 
41685779Sxy150489 	return (IGB_SUCCESS);
41695779Sxy150489 }
41705779Sxy150489 
41715779Sxy150489 /*
4172*8571SChenlu.Chen@Sun.COM  * igb_setup_msix_82575 - setup 82575 adapter to use MSI-X interrupts
41735779Sxy150489  *
41745779Sxy150489  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
41755779Sxy150489  */
41765779Sxy150489 static void
4177*8571SChenlu.Chen@Sun.COM igb_setup_msix_82575(igb_t *igb)
41785779Sxy150489 {
41795779Sxy150489 	uint32_t eims = 0;
41805779Sxy150489 	int i, vector;
41815779Sxy150489 	struct e1000_hw *hw = &igb->hw;
41825779Sxy150489 
41835779Sxy150489 	/*
4184*8571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
4185*8571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
41865779Sxy150489 	 */
41875779Sxy150489 	vector = 0;
41888275SEric Cheng 
41895779Sxy150489 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
41905779Sxy150489 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
41915779Sxy150489 	vector++;
41928275SEric Cheng 
41935779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
41945779Sxy150489 		/*
41955779Sxy150489 		 * Set vector for each rx ring
41965779Sxy150489 		 */
41975779Sxy150489 		eims = (E1000_EICR_RX_QUEUE0 << i);
41985779Sxy150489 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
41995779Sxy150489 
42005779Sxy150489 		/*
4201*8571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
4202*8571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
42035779Sxy150489 		 */
42045779Sxy150489 		igb->eims_mask |= eims;
42055779Sxy150489 
42065779Sxy150489 		vector++;
42075779Sxy150489 	}
42085779Sxy150489 
42098275SEric Cheng 	for (i = 1; i < igb->num_tx_rings; i++) {
42108275SEric Cheng 		/*
42118275SEric Cheng 		 * Set vector for each tx ring from 2nd tx ring
42128275SEric Cheng 		 */
42138275SEric Cheng 		eims = (E1000_EICR_TX_QUEUE0 << i);
42148275SEric Cheng 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
42158275SEric Cheng 
42168275SEric Cheng 		/*
4217*8571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
4218*8571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
42198275SEric Cheng 		 */
42208275SEric Cheng 		igb->eims_mask |= eims;
42218275SEric Cheng 
42228275SEric Cheng 		vector++;
42238275SEric Cheng 	}
42248275SEric Cheng 
42255779Sxy150489 	ASSERT(vector == igb->intr_cnt);
42265779Sxy150489 
42275779Sxy150489 	/*
42285779Sxy150489 	 * Disable IAM for ICR interrupt bits
42295779Sxy150489 	 */
42305779Sxy150489 	E1000_WRITE_REG(hw, E1000_IAM, 0);
42315779Sxy150489 	E1000_WRITE_FLUSH(hw);
42325779Sxy150489 }
42335779Sxy150489 
42345779Sxy150489 /*
4235*8571SChenlu.Chen@Sun.COM  * igb_setup_msix_82576 - setup 82576 adapter to use MSI-X interrupts
4236*8571SChenlu.Chen@Sun.COM  *
4237*8571SChenlu.Chen@Sun.COM  * 82576 uses a table based method for assigning vectors.  Each queue has a
4238*8571SChenlu.Chen@Sun.COM  * single entry in the table to which we write a vector number along with a
4239*8571SChenlu.Chen@Sun.COM  * "valid" bit.  The entry is a single byte in a 4-byte register.  Vectors
4240*8571SChenlu.Chen@Sun.COM  * take a different position in the 4-byte register depending on whether
4241*8571SChenlu.Chen@Sun.COM  * they are numbered above or below 8.
4242*8571SChenlu.Chen@Sun.COM  */
4243*8571SChenlu.Chen@Sun.COM static void
4244*8571SChenlu.Chen@Sun.COM igb_setup_msix_82576(igb_t *igb)
4245*8571SChenlu.Chen@Sun.COM {
4246*8571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
4247*8571SChenlu.Chen@Sun.COM 	uint32_t ivar, index, vector;
4248*8571SChenlu.Chen@Sun.COM 	int i;
4249*8571SChenlu.Chen@Sun.COM 
4250*8571SChenlu.Chen@Sun.COM 	/* must enable msi-x capability before IVAR settings */
4251*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_GPIE,
4252*8571SChenlu.Chen@Sun.COM 	    (E1000_GPIE_MSIX_MODE | E1000_GPIE_PBA | E1000_GPIE_NSICR));
4253*8571SChenlu.Chen@Sun.COM 
4254*8571SChenlu.Chen@Sun.COM 	/*
4255*8571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
4256*8571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
4257*8571SChenlu.Chen@Sun.COM 	 * This is also interdependent with installation of interrupt service
4258*8571SChenlu.Chen@Sun.COM 	 * routines in igb_add_intr_handlers().
4259*8571SChenlu.Chen@Sun.COM 	 */
4260*8571SChenlu.Chen@Sun.COM 
4261*8571SChenlu.Chen@Sun.COM 	/* assign "other" causes to vector 0 */
4262*8571SChenlu.Chen@Sun.COM 	vector = 0;
4263*8571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4264*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4265*8571SChenlu.Chen@Sun.COM 
4266*8571SChenlu.Chen@Sun.COM 	/* assign tx ring 0 to vector 0 */
4267*8571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4268*8571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
4269*8571SChenlu.Chen@Sun.COM 
4270*8571SChenlu.Chen@Sun.COM 	/* prepare to enable tx & other interrupt causes */
4271*8571SChenlu.Chen@Sun.COM 	igb->eims_mask = (1 << vector);
4272*8571SChenlu.Chen@Sun.COM 
4273*8571SChenlu.Chen@Sun.COM 	vector ++;
4274*8571SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
4275*8571SChenlu.Chen@Sun.COM 		/*
4276*8571SChenlu.Chen@Sun.COM 		 * Set vector for each rx ring
4277*8571SChenlu.Chen@Sun.COM 		 */
4278*8571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
4279*8571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4280*8571SChenlu.Chen@Sun.COM 
4281*8571SChenlu.Chen@Sun.COM 		if (i < 8) {
4282*8571SChenlu.Chen@Sun.COM 			/* vector goes into low byte of register */
4283*8571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFFFF00;
4284*8571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID);
4285*8571SChenlu.Chen@Sun.COM 		} else {
4286*8571SChenlu.Chen@Sun.COM 			/* vector goes into third byte of register */
4287*8571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFF00FFFF;
4288*8571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
4289*8571SChenlu.Chen@Sun.COM 		}
4290*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4291*8571SChenlu.Chen@Sun.COM 
4292*8571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
4293*8571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
4294*8571SChenlu.Chen@Sun.COM 
4295*8571SChenlu.Chen@Sun.COM 		vector ++;
4296*8571SChenlu.Chen@Sun.COM 	}
4297*8571SChenlu.Chen@Sun.COM 
4298*8571SChenlu.Chen@Sun.COM 	for (i = 1; i < igb->num_tx_rings; i++) {
4299*8571SChenlu.Chen@Sun.COM 		/*
4300*8571SChenlu.Chen@Sun.COM 		 * Set vector for each tx ring from 2nd tx ring.
4301*8571SChenlu.Chen@Sun.COM 		 * Note assumption that tx vectors numericall follow rx vectors.
4302*8571SChenlu.Chen@Sun.COM 		 */
4303*8571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
4304*8571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4305*8571SChenlu.Chen@Sun.COM 
4306*8571SChenlu.Chen@Sun.COM 		if (i < 8) {
4307*8571SChenlu.Chen@Sun.COM 			/* vector goes into second byte of register */
4308*8571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFF00FF;
4309*8571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 8);
4310*8571SChenlu.Chen@Sun.COM 		} else {
4311*8571SChenlu.Chen@Sun.COM 			/* vector goes into fourth byte of register */
4312*8571SChenlu.Chen@Sun.COM 			ivar = ivar & 0x00FFFFFF;
4313*8571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID) << 24;
4314*8571SChenlu.Chen@Sun.COM 		}
4315*8571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4316*8571SChenlu.Chen@Sun.COM 
4317*8571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
4318*8571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
4319*8571SChenlu.Chen@Sun.COM 
4320*8571SChenlu.Chen@Sun.COM 		vector ++;
4321*8571SChenlu.Chen@Sun.COM 	}
4322*8571SChenlu.Chen@Sun.COM 
4323*8571SChenlu.Chen@Sun.COM 	ASSERT(vector == igb->intr_cnt);
4324*8571SChenlu.Chen@Sun.COM }
4325*8571SChenlu.Chen@Sun.COM 
4326*8571SChenlu.Chen@Sun.COM /*
43275779Sxy150489  * igb_rem_intr_handlers - remove the interrupt handlers
43285779Sxy150489  */
43295779Sxy150489 static void
43305779Sxy150489 igb_rem_intr_handlers(igb_t *igb)
43315779Sxy150489 {
43325779Sxy150489 	int i;
43335779Sxy150489 	int rc;
43345779Sxy150489 
43355779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
43365779Sxy150489 		rc = ddi_intr_remove_handler(igb->htable[i]);
43375779Sxy150489 		if (rc != DDI_SUCCESS) {
43385779Sxy150489 			IGB_DEBUGLOG_1(igb,
43395779Sxy150489 			    "Remove intr handler failed: %d", rc);
43405779Sxy150489 		}
43415779Sxy150489 	}
43425779Sxy150489 }
43435779Sxy150489 
43445779Sxy150489 /*
43455779Sxy150489  * igb_rem_intrs - remove the allocated interrupts
43465779Sxy150489  */
43475779Sxy150489 static void
43485779Sxy150489 igb_rem_intrs(igb_t *igb)
43495779Sxy150489 {
43505779Sxy150489 	int i;
43515779Sxy150489 	int rc;
43525779Sxy150489 
43535779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
43545779Sxy150489 		rc = ddi_intr_free(igb->htable[i]);
43555779Sxy150489 		if (rc != DDI_SUCCESS) {
43565779Sxy150489 			IGB_DEBUGLOG_1(igb,
43575779Sxy150489 			    "Free intr failed: %d", rc);
43585779Sxy150489 		}
43595779Sxy150489 	}
43605779Sxy150489 
43615779Sxy150489 	kmem_free(igb->htable, igb->intr_size);
43625779Sxy150489 	igb->htable = NULL;
43635779Sxy150489 }
43645779Sxy150489 
43655779Sxy150489 /*
43665779Sxy150489  * igb_enable_intrs - enable all the ddi interrupts
43675779Sxy150489  */
43685779Sxy150489 static int
43695779Sxy150489 igb_enable_intrs(igb_t *igb)
43705779Sxy150489 {
43715779Sxy150489 	int i;
43725779Sxy150489 	int rc;
43735779Sxy150489 
43745779Sxy150489 	/* Enable interrupts */
43755779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
43765779Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
43775779Sxy150489 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
43785779Sxy150489 		if (rc != DDI_SUCCESS) {
43795779Sxy150489 			igb_log(igb,
43805779Sxy150489 			    "Enable block intr failed: %d", rc);
43815779Sxy150489 			return (IGB_FAILURE);
43825779Sxy150489 		}
43835779Sxy150489 	} else {
43845779Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
43855779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
43865779Sxy150489 			rc = ddi_intr_enable(igb->htable[i]);
43875779Sxy150489 			if (rc != DDI_SUCCESS) {
43885779Sxy150489 				igb_log(igb,
43895779Sxy150489 				    "Enable intr failed: %d", rc);
43905779Sxy150489 				return (IGB_FAILURE);
43915779Sxy150489 			}
43925779Sxy150489 		}
43935779Sxy150489 	}
43945779Sxy150489 
43955779Sxy150489 	return (IGB_SUCCESS);
43965779Sxy150489 }
43975779Sxy150489 
43985779Sxy150489 /*
43995779Sxy150489  * igb_disable_intrs - disable all the ddi interrupts
44005779Sxy150489  */
44015779Sxy150489 static int
44025779Sxy150489 igb_disable_intrs(igb_t *igb)
44035779Sxy150489 {
44045779Sxy150489 	int i;
44055779Sxy150489 	int rc;
44065779Sxy150489 
44075779Sxy150489 	/* Disable all interrupts */
44085779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
44095779Sxy150489 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
44105779Sxy150489 		if (rc != DDI_SUCCESS) {
44115779Sxy150489 			igb_log(igb,
44125779Sxy150489 			    "Disable block intr failed: %d", rc);
44135779Sxy150489 			return (IGB_FAILURE);
44145779Sxy150489 		}
44155779Sxy150489 	} else {
44165779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
44175779Sxy150489 			rc = ddi_intr_disable(igb->htable[i]);
44185779Sxy150489 			if (rc != DDI_SUCCESS) {
44195779Sxy150489 				igb_log(igb,
44205779Sxy150489 				    "Disable intr failed: %d", rc);
44215779Sxy150489 				return (IGB_FAILURE);
44225779Sxy150489 			}
44235779Sxy150489 		}
44245779Sxy150489 	}
44255779Sxy150489 
44265779Sxy150489 	return (IGB_SUCCESS);
44275779Sxy150489 }
44285779Sxy150489 
44295779Sxy150489 /*
44305779Sxy150489  * igb_get_phy_state - Get and save the parameters read from PHY registers
44315779Sxy150489  */
44325779Sxy150489 static void
44335779Sxy150489 igb_get_phy_state(igb_t *igb)
44345779Sxy150489 {
44355779Sxy150489 	struct e1000_hw *hw = &igb->hw;
44365779Sxy150489 	uint16_t phy_ctrl;
44375779Sxy150489 	uint16_t phy_status;
44385779Sxy150489 	uint16_t phy_an_adv;
44395779Sxy150489 	uint16_t phy_an_exp;
44405779Sxy150489 	uint16_t phy_ext_status;
44415779Sxy150489 	uint16_t phy_1000t_ctrl;
44425779Sxy150489 	uint16_t phy_1000t_status;
44435779Sxy150489 	uint16_t phy_lp_able;
44445779Sxy150489 
44455779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
44465779Sxy150489 
44475779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
44485779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
44495779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
44505779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
44515779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
44525779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
44535779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_1000t_status);
44545779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
44555779Sxy150489 
44565779Sxy150489 	igb->param_autoneg_cap =
44575779Sxy150489 	    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
44585779Sxy150489 	igb->param_pause_cap =
44595779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
44605779Sxy150489 	igb->param_asym_pause_cap =
44615779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
44625779Sxy150489 	igb->param_1000fdx_cap = ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
44635779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
44645779Sxy150489 	igb->param_1000hdx_cap = ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
44655779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
44665779Sxy150489 	igb->param_100t4_cap =
44675779Sxy150489 	    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
44685779Sxy150489 	igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
44695779Sxy150489 	    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
44705779Sxy150489 	igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
44715779Sxy150489 	    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
44725779Sxy150489 	igb->param_10fdx_cap =
44735779Sxy150489 	    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
44745779Sxy150489 	igb->param_10hdx_cap =
44755779Sxy150489 	    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
44765779Sxy150489 	igb->param_rem_fault =
44775779Sxy150489 	    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
44785779Sxy150489 
44795779Sxy150489 	igb->param_adv_autoneg_cap = hw->mac.autoneg;
44805779Sxy150489 	igb->param_adv_pause_cap =
44815779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
44825779Sxy150489 	igb->param_adv_asym_pause_cap =
44835779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
44845779Sxy150489 	igb->param_adv_1000hdx_cap =
44855779Sxy150489 	    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
44865779Sxy150489 	igb->param_adv_100t4_cap =
44875779Sxy150489 	    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
44885779Sxy150489 	igb->param_adv_rem_fault =
44895779Sxy150489 	    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
44905779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
44915779Sxy150489 		igb->param_adv_1000fdx_cap =
44925779Sxy150489 		    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
44935779Sxy150489 		igb->param_adv_100fdx_cap =
44945779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
44955779Sxy150489 		igb->param_adv_100hdx_cap =
44965779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
44975779Sxy150489 		igb->param_adv_10fdx_cap =
44985779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
44995779Sxy150489 		igb->param_adv_10hdx_cap =
45005779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
45015779Sxy150489 	}
45025779Sxy150489 
45035779Sxy150489 	igb->param_lp_autoneg_cap =
45045779Sxy150489 	    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
45055779Sxy150489 	igb->param_lp_pause_cap =
45065779Sxy150489 	    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
45075779Sxy150489 	igb->param_lp_asym_pause_cap =
45085779Sxy150489 	    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
45095779Sxy150489 	igb->param_lp_1000fdx_cap =
45105779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
45115779Sxy150489 	igb->param_lp_1000hdx_cap =
45125779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
45135779Sxy150489 	igb->param_lp_100t4_cap =
45145779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
45155779Sxy150489 	igb->param_lp_100fdx_cap =
45165779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
45175779Sxy150489 	igb->param_lp_100hdx_cap =
45185779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
45195779Sxy150489 	igb->param_lp_10fdx_cap =
45205779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
45215779Sxy150489 	igb->param_lp_10hdx_cap =
45225779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
45235779Sxy150489 	igb->param_lp_rem_fault =
45245779Sxy150489 	    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
45255779Sxy150489 }
45265779Sxy150489 
45275779Sxy150489 /*
45285779Sxy150489  * igb_get_driver_control
45295779Sxy150489  */
45305779Sxy150489 static void
45315779Sxy150489 igb_get_driver_control(struct e1000_hw *hw)
45325779Sxy150489 {
45335779Sxy150489 	uint32_t ctrl_ext;
45345779Sxy150489 
45355779Sxy150489 	/* Notify firmware that driver is in control of device */
45365779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
45375779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
45385779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
45395779Sxy150489 }
45405779Sxy150489 
45415779Sxy150489 /*
45425779Sxy150489  * igb_release_driver_control
45435779Sxy150489  */
45445779Sxy150489 static void
45455779Sxy150489 igb_release_driver_control(struct e1000_hw *hw)
45465779Sxy150489 {
45475779Sxy150489 	uint32_t ctrl_ext;
45485779Sxy150489 
45495779Sxy150489 	/* Notify firmware that driver is no longer in control of device */
45505779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
45515779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
45525779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
45535779Sxy150489 }
45545779Sxy150489 
45555779Sxy150489 /*
45565779Sxy150489  * igb_atomic_reserve - Atomic decrease operation
45575779Sxy150489  */
45585779Sxy150489 int
45595779Sxy150489 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
45605779Sxy150489 {
45615779Sxy150489 	uint32_t oldval;
45625779Sxy150489 	uint32_t newval;
45635779Sxy150489 
45645779Sxy150489 	/* ATOMICALLY */
45655779Sxy150489 	do {
45665779Sxy150489 		oldval = *count_p;
45675779Sxy150489 		if (oldval < n)
45685779Sxy150489 			return (-1);
45695779Sxy150489 		newval = oldval - n;
45705779Sxy150489 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
45715779Sxy150489 
45725779Sxy150489 	return (newval);
45735779Sxy150489 }
45746624Sgl147354 
45756624Sgl147354 /*
45766624Sgl147354  * FMA support
45776624Sgl147354  */
45786624Sgl147354 
45796624Sgl147354 int
45806624Sgl147354 igb_check_acc_handle(ddi_acc_handle_t handle)
45816624Sgl147354 {
45826624Sgl147354 	ddi_fm_error_t de;
45836624Sgl147354 
45846624Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
45856624Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
45866624Sgl147354 	return (de.fme_status);
45876624Sgl147354 }
45886624Sgl147354 
45896624Sgl147354 int
45906624Sgl147354 igb_check_dma_handle(ddi_dma_handle_t handle)
45916624Sgl147354 {
45926624Sgl147354 	ddi_fm_error_t de;
45936624Sgl147354 
45946624Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
45956624Sgl147354 	return (de.fme_status);
45966624Sgl147354 }
45976624Sgl147354 
45986624Sgl147354 /*
45996624Sgl147354  * The IO fault service error handling callback function
46006624Sgl147354  */
46016624Sgl147354 /*ARGSUSED*/
46026624Sgl147354 static int
46036624Sgl147354 igb_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
46046624Sgl147354 {
46056624Sgl147354 	/*
46066624Sgl147354 	 * as the driver can always deal with an error in any dma or
46076624Sgl147354 	 * access handle, we can just return the fme_status value.
46086624Sgl147354 	 */
46096624Sgl147354 	pci_ereport_post(dip, err, NULL);
46106624Sgl147354 	return (err->fme_status);
46116624Sgl147354 }
46126624Sgl147354 
46136624Sgl147354 static void
46146624Sgl147354 igb_fm_init(igb_t *igb)
46156624Sgl147354 {
46166624Sgl147354 	ddi_iblock_cookie_t iblk;
46176624Sgl147354 	int fma_acc_flag, fma_dma_flag;
46186624Sgl147354 
46196624Sgl147354 	/* Only register with IO Fault Services if we have some capability */
46206624Sgl147354 	if (igb->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
46216624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
46226624Sgl147354 		fma_acc_flag = 1;
46236624Sgl147354 	} else {
46246624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
46256624Sgl147354 		fma_acc_flag = 0;
46266624Sgl147354 	}
46276624Sgl147354 
46286624Sgl147354 	if (igb->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
46296624Sgl147354 		fma_dma_flag = 1;
46306624Sgl147354 	} else {
46316624Sgl147354 		fma_dma_flag = 0;
46326624Sgl147354 	}
46336624Sgl147354 
46346624Sgl147354 	(void) igb_set_fma_flags(fma_acc_flag, fma_dma_flag);
46356624Sgl147354 
46366624Sgl147354 	if (igb->fm_capabilities) {
46376624Sgl147354 
46386624Sgl147354 		/* Register capabilities with IO Fault Services */
46396624Sgl147354 		ddi_fm_init(igb->dip, &igb->fm_capabilities, &iblk);
46406624Sgl147354 
46416624Sgl147354 		/*
46426624Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
46436624Sgl147354 		 */
46446624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
46456624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
46466624Sgl147354 			pci_ereport_setup(igb->dip);
46476624Sgl147354 
46486624Sgl147354 		/*
46496624Sgl147354 		 * Register error callback if error callback capable
46506624Sgl147354 		 */
46516624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
46526624Sgl147354 			ddi_fm_handler_register(igb->dip,
46536624Sgl147354 			    igb_fm_error_cb, (void*) igb);
46546624Sgl147354 	}
46556624Sgl147354 }
46566624Sgl147354 
46576624Sgl147354 static void
46586624Sgl147354 igb_fm_fini(igb_t *igb)
46596624Sgl147354 {
46606624Sgl147354 	/* Only unregister FMA capabilities if we registered some */
46616624Sgl147354 	if (igb->fm_capabilities) {
46626624Sgl147354 
46636624Sgl147354 		/*
46646624Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
46656624Sgl147354 		 */
46666624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
46676624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
46686624Sgl147354 			pci_ereport_teardown(igb->dip);
46696624Sgl147354 
46706624Sgl147354 		/*
46716624Sgl147354 		 * Un-register error callback if error callback capable
46726624Sgl147354 		 */
46736624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
46746624Sgl147354 			ddi_fm_handler_unregister(igb->dip);
46756624Sgl147354 
46766624Sgl147354 		/* Unregister from IO Fault Services */
46776624Sgl147354 		ddi_fm_fini(igb->dip);
46786624Sgl147354 	}
46796624Sgl147354 }
46806624Sgl147354 
46816624Sgl147354 void
46826624Sgl147354 igb_fm_ereport(igb_t *igb, char *detail)
46836624Sgl147354 {
46846624Sgl147354 	uint64_t ena;
46856624Sgl147354 	char buf[FM_MAX_CLASS];
46866624Sgl147354 
46876624Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
46886624Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
46896624Sgl147354 	if (DDI_FM_EREPORT_CAP(igb->fm_capabilities)) {
46906624Sgl147354 		ddi_fm_ereport_post(igb->dip, buf, ena, DDI_NOSLEEP,
46916624Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
46926624Sgl147354 	}
46936624Sgl147354 }
4694