xref: /onnv-gate/usr/src/uts/common/io/igb/igb_main.c (revision 12980:c2b4f6c2d38a)
15779Sxy150489 /*
25779Sxy150489  * CDDL HEADER START
35779Sxy150489  *
45779Sxy150489  * The contents of this file are subject to the terms of the
55779Sxy150489  * Common Development and Distribution License (the "License").
65779Sxy150489  * You may not use this file except in compliance with the License.
75779Sxy150489  *
811878SVenu.Iyer@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
911878SVenu.Iyer@Sun.COM  * or http://www.opensolaris.org/os/licensing.
105779Sxy150489  * See the License for the specific language governing permissions
115779Sxy150489  * and limitations under the License.
125779Sxy150489  *
1311878SVenu.Iyer@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
1411878SVenu.Iyer@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155779Sxy150489  * If applicable, add the following below this CDDL HEADER, with the
165779Sxy150489  * fields enclosed by brackets "[]" replaced with your own identifying
175779Sxy150489  * information: Portions Copyright [yyyy] [name of copyright owner]
185779Sxy150489  *
195779Sxy150489  * CDDL HEADER END
205779Sxy150489  */
215779Sxy150489 
225779Sxy150489 /*
2312111SGuoqing.Zhu@Sun.COM  * Copyright(c) 2007-2010 Intel Corporation. All rights reserved.
2412111SGuoqing.Zhu@Sun.COM  */
2512111SGuoqing.Zhu@Sun.COM 
2612111SGuoqing.Zhu@Sun.COM /*
2712111SGuoqing.Zhu@Sun.COM  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
285779Sxy150489  */
295779Sxy150489 
305779Sxy150489 #include "igb_sw.h"
315779Sxy150489 
327656SSherry.Moore@Sun.COM static char ident[] = "Intel 1Gb Ethernet";
33*12980SGuoqing.Zhu@Sun.COM static char igb_version[] = "igb 1.1.17";
345779Sxy150489 
355779Sxy150489 /*
365779Sxy150489  * Local function protoypes
375779Sxy150489  */
385779Sxy150489 static int igb_register_mac(igb_t *);
395779Sxy150489 static int igb_identify_hardware(igb_t *);
405779Sxy150489 static int igb_regs_map(igb_t *);
415779Sxy150489 static void igb_init_properties(igb_t *);
425779Sxy150489 static int igb_init_driver_settings(igb_t *);
435779Sxy150489 static void igb_init_locks(igb_t *);
445779Sxy150489 static void igb_destroy_locks(igb_t *);
458955SChenlu.Chen@Sun.COM static int igb_init_mac_address(igb_t *);
465779Sxy150489 static int igb_init(igb_t *);
478955SChenlu.Chen@Sun.COM static int igb_init_adapter(igb_t *);
488955SChenlu.Chen@Sun.COM static void igb_stop_adapter(igb_t *);
495779Sxy150489 static int igb_reset(igb_t *);
505779Sxy150489 static void igb_tx_clean(igb_t *);
515779Sxy150489 static boolean_t igb_tx_drain(igb_t *);
525779Sxy150489 static boolean_t igb_rx_drain(igb_t *);
535779Sxy150489 static int igb_alloc_rings(igb_t *);
5411502SChenlu.Chen@Sun.COM static int igb_alloc_rx_data(igb_t *);
5511502SChenlu.Chen@Sun.COM static void igb_free_rx_data(igb_t *);
565779Sxy150489 static void igb_free_rings(igb_t *);
575779Sxy150489 static void igb_setup_rings(igb_t *);
585779Sxy150489 static void igb_setup_rx(igb_t *);
595779Sxy150489 static void igb_setup_tx(igb_t *);
605779Sxy150489 static void igb_setup_rx_ring(igb_rx_ring_t *);
615779Sxy150489 static void igb_setup_tx_ring(igb_tx_ring_t *);
625779Sxy150489 static void igb_setup_rss(igb_t *);
638275SEric Cheng static void igb_setup_mac_rss_classify(igb_t *);
648275SEric Cheng static void igb_setup_mac_classify(igb_t *);
655779Sxy150489 static void igb_init_unicst(igb_t *);
665779Sxy150489 static void igb_setup_multicst(igb_t *);
675779Sxy150489 static void igb_get_phy_state(igb_t *);
6811502SChenlu.Chen@Sun.COM static void igb_param_sync(igb_t *);
695779Sxy150489 static void igb_get_conf(igb_t *);
705779Sxy150489 static int igb_get_prop(igb_t *, char *, int, int, int);
715779Sxy150489 static boolean_t igb_is_link_up(igb_t *);
725779Sxy150489 static boolean_t igb_link_check(igb_t *);
735779Sxy150489 static void igb_local_timer(void *);
7411367SJason.Xu@Sun.COM static void igb_link_timer(void *);
755779Sxy150489 static void igb_arm_watchdog_timer(igb_t *);
765779Sxy150489 static void igb_start_watchdog_timer(igb_t *);
775779Sxy150489 static void igb_restart_watchdog_timer(igb_t *);
785779Sxy150489 static void igb_stop_watchdog_timer(igb_t *);
7911367SJason.Xu@Sun.COM static void igb_start_link_timer(igb_t *);
8011367SJason.Xu@Sun.COM static void igb_stop_link_timer(igb_t *);
815779Sxy150489 static void igb_disable_adapter_interrupts(igb_t *);
828571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82575(igb_t *);
838571SChenlu.Chen@Sun.COM static void igb_enable_adapter_interrupts_82576(igb_t *);
8411155SJason.Xu@Sun.COM static void igb_enable_adapter_interrupts_82580(igb_t *);
855779Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
865779Sxy150489 static boolean_t igb_stall_check(igb_t *);
875779Sxy150489 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
885779Sxy150489 static void igb_set_external_loopback(igb_t *);
895779Sxy150489 static void igb_set_internal_phy_loopback(igb_t *);
905779Sxy150489 static void igb_set_internal_serdes_loopback(igb_t *);
915779Sxy150489 static boolean_t igb_find_mac_address(igb_t *);
925779Sxy150489 static int igb_alloc_intrs(igb_t *);
937072Sxy150489 static int igb_alloc_intr_handles(igb_t *, int);
945779Sxy150489 static int igb_add_intr_handlers(igb_t *);
955779Sxy150489 static void igb_rem_intr_handlers(igb_t *);
965779Sxy150489 static void igb_rem_intrs(igb_t *);
975779Sxy150489 static int igb_enable_intrs(igb_t *);
985779Sxy150489 static int igb_disable_intrs(igb_t *);
998571SChenlu.Chen@Sun.COM static void igb_setup_msix_82575(igb_t *);
1008571SChenlu.Chen@Sun.COM static void igb_setup_msix_82576(igb_t *);
10111155SJason.Xu@Sun.COM static void igb_setup_msix_82580(igb_t *);
1025779Sxy150489 static uint_t igb_intr_legacy(void *, void *);
1035779Sxy150489 static uint_t igb_intr_msi(void *, void *);
1045779Sxy150489 static uint_t igb_intr_rx(void *, void *);
1058275SEric Cheng static uint_t igb_intr_tx(void *, void *);
1065779Sxy150489 static uint_t igb_intr_tx_other(void *, void *);
1075779Sxy150489 static void igb_intr_rx_work(igb_rx_ring_t *);
1085779Sxy150489 static void igb_intr_tx_work(igb_tx_ring_t *);
1098275SEric Cheng static void igb_intr_link_work(igb_t *);
1105779Sxy150489 static void igb_get_driver_control(struct e1000_hw *);
1115779Sxy150489 static void igb_release_driver_control(struct e1000_hw *);
1125779Sxy150489 
1135779Sxy150489 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
1145779Sxy150489 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
1155779Sxy150489 static int igb_resume(dev_info_t *);
1165779Sxy150489 static int igb_suspend(dev_info_t *);
1177656SSherry.Moore@Sun.COM static int igb_quiesce(dev_info_t *);
1185779Sxy150489 static void igb_unconfigure(dev_info_t *, igb_t *);
1196624Sgl147354 static int igb_fm_error_cb(dev_info_t *, ddi_fm_error_t *,
1206624Sgl147354     const void *);
1216624Sgl147354 static void igb_fm_init(igb_t *);
1226624Sgl147354 static void igb_fm_fini(igb_t *);
1239775SVitezslav.Batrla@Sun.COM static void igb_release_multicast(igb_t *);
1245779Sxy150489 
12511878SVenu.Iyer@Sun.COM char *igb_priv_props[] = {
12611878SVenu.Iyer@Sun.COM 	"_tx_copy_thresh",
12711878SVenu.Iyer@Sun.COM 	"_tx_recycle_thresh",
12811878SVenu.Iyer@Sun.COM 	"_tx_overload_thresh",
12911878SVenu.Iyer@Sun.COM 	"_tx_resched_thresh",
13011878SVenu.Iyer@Sun.COM 	"_rx_copy_thresh",
13111878SVenu.Iyer@Sun.COM 	"_rx_limit_per_intr",
13211878SVenu.Iyer@Sun.COM 	"_intr_throttling",
13311878SVenu.Iyer@Sun.COM 	"_adv_pause_cap",
13411878SVenu.Iyer@Sun.COM 	"_adv_asym_pause_cap",
13511878SVenu.Iyer@Sun.COM 	NULL
13611502SChenlu.Chen@Sun.COM };
13711502SChenlu.Chen@Sun.COM 
1385779Sxy150489 static struct cb_ops igb_cb_ops = {
1395779Sxy150489 	nulldev,		/* cb_open */
1405779Sxy150489 	nulldev,		/* cb_close */
1415779Sxy150489 	nodev,			/* cb_strategy */
1425779Sxy150489 	nodev,			/* cb_print */
1435779Sxy150489 	nodev,			/* cb_dump */
1445779Sxy150489 	nodev,			/* cb_read */
1455779Sxy150489 	nodev,			/* cb_write */
1465779Sxy150489 	nodev,			/* cb_ioctl */
1475779Sxy150489 	nodev,			/* cb_devmap */
1485779Sxy150489 	nodev,			/* cb_mmap */
1495779Sxy150489 	nodev,			/* cb_segmap */
1505779Sxy150489 	nochpoll,		/* cb_chpoll */
1515779Sxy150489 	ddi_prop_op,		/* cb_prop_op */
1525779Sxy150489 	NULL,			/* cb_stream */
1535779Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
1545779Sxy150489 	CB_REV,			/* cb_rev */
1555779Sxy150489 	nodev,			/* cb_aread */
1565779Sxy150489 	nodev			/* cb_awrite */
1575779Sxy150489 };
1585779Sxy150489 
1595779Sxy150489 static struct dev_ops igb_dev_ops = {
1605779Sxy150489 	DEVO_REV,		/* devo_rev */
1615779Sxy150489 	0,			/* devo_refcnt */
1625779Sxy150489 	NULL,			/* devo_getinfo */
1635779Sxy150489 	nulldev,		/* devo_identify */
1645779Sxy150489 	nulldev,		/* devo_probe */
1655779Sxy150489 	igb_attach,		/* devo_attach */
1665779Sxy150489 	igb_detach,		/* devo_detach */
1675779Sxy150489 	nodev,			/* devo_reset */
1685779Sxy150489 	&igb_cb_ops,		/* devo_cb_ops */
1695779Sxy150489 	NULL,			/* devo_bus_ops */
1707656SSherry.Moore@Sun.COM 	ddi_power,		/* devo_power */
1717656SSherry.Moore@Sun.COM 	igb_quiesce,	/* devo_quiesce */
1725779Sxy150489 };
1735779Sxy150489 
1745779Sxy150489 static struct modldrv igb_modldrv = {
1755779Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
1765779Sxy150489 	ident,			/* Discription string */
1775779Sxy150489 	&igb_dev_ops,		/* driver ops */
1785779Sxy150489 };
1795779Sxy150489 
1805779Sxy150489 static struct modlinkage igb_modlinkage = {
1815779Sxy150489 	MODREV_1, &igb_modldrv, NULL
1825779Sxy150489 };
1835779Sxy150489 
1845779Sxy150489 /* Access attributes for register mapping */
1855779Sxy150489 ddi_device_acc_attr_t igb_regs_acc_attr = {
18611236SStephen.Hanson@Sun.COM 	DDI_DEVICE_ATTR_V1,
1875779Sxy150489 	DDI_STRUCTURE_LE_ACC,
1885779Sxy150489 	DDI_STRICTORDER_ACC,
1896624Sgl147354 	DDI_FLAGERR_ACC
1905779Sxy150489 };
1915779Sxy150489 
19211502SChenlu.Chen@Sun.COM #define	IGB_M_CALLBACK_FLAGS \
19311878SVenu.Iyer@Sun.COM 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO)
1945779Sxy150489 
1955779Sxy150489 static mac_callbacks_t igb_m_callbacks = {
1965779Sxy150489 	IGB_M_CALLBACK_FLAGS,
1975779Sxy150489 	igb_m_stat,
1985779Sxy150489 	igb_m_start,
1995779Sxy150489 	igb_m_stop,
2005779Sxy150489 	igb_m_promisc,
2015779Sxy150489 	igb_m_multicst,
2028275SEric Cheng 	NULL,
2035779Sxy150489 	NULL,
20411878SVenu.Iyer@Sun.COM 	NULL,
2055779Sxy150489 	igb_m_ioctl,
20611502SChenlu.Chen@Sun.COM 	igb_m_getcapab,
20711502SChenlu.Chen@Sun.COM 	NULL,
20811502SChenlu.Chen@Sun.COM 	NULL,
20911502SChenlu.Chen@Sun.COM 	igb_m_setprop,
21011878SVenu.Iyer@Sun.COM 	igb_m_getprop,
21111878SVenu.Iyer@Sun.COM 	igb_m_propinfo
2125779Sxy150489 };
2135779Sxy150489 
2145779Sxy150489 /*
2158571SChenlu.Chen@Sun.COM  * Initialize capabilities of each supported adapter type
2168571SChenlu.Chen@Sun.COM  */
2178571SChenlu.Chen@Sun.COM static adapter_info_t igb_82575_cap = {
2188571SChenlu.Chen@Sun.COM 	/* limits */
2198571SChenlu.Chen@Sun.COM 	4,		/* maximum number of rx queues */
2208571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
2218571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
2228571SChenlu.Chen@Sun.COM 	4,		/* maximum number of tx queues */
2238571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
2248571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
2258571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
2268571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
2278571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
2288571SChenlu.Chen@Sun.COM 
2298571SChenlu.Chen@Sun.COM 	/* function pointers */
2308571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82575,
2318571SChenlu.Chen@Sun.COM 	igb_setup_msix_82575,
2328571SChenlu.Chen@Sun.COM 
2338571SChenlu.Chen@Sun.COM 	/* capabilities */
2348571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
2358955SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL),
2368955SChenlu.Chen@Sun.COM 
2378955SChenlu.Chen@Sun.COM 	0xffc00000		/* mask for RXDCTL register */
2388571SChenlu.Chen@Sun.COM };
2398571SChenlu.Chen@Sun.COM 
2408571SChenlu.Chen@Sun.COM static adapter_info_t igb_82576_cap = {
2418571SChenlu.Chen@Sun.COM 	/* limits */
2428955SChenlu.Chen@Sun.COM 	16,		/* maximum number of rx queues */
2438571SChenlu.Chen@Sun.COM 	1,		/* minimum number of rx queues */
2448571SChenlu.Chen@Sun.COM 	4,		/* default number of rx queues */
2458955SChenlu.Chen@Sun.COM 	16,		/* maximum number of tx queues */
2468571SChenlu.Chen@Sun.COM 	1,		/* minimum number of tx queues */
2478571SChenlu.Chen@Sun.COM 	4,		/* default number of tx queues */
2488571SChenlu.Chen@Sun.COM 	65535,		/* maximum interrupt throttle rate */
2498571SChenlu.Chen@Sun.COM 	0,		/* minimum interrupt throttle rate */
2508571SChenlu.Chen@Sun.COM 	200,		/* default interrupt throttle rate */
2518571SChenlu.Chen@Sun.COM 
2528571SChenlu.Chen@Sun.COM 	/* function pointers */
2538571SChenlu.Chen@Sun.COM 	igb_enable_adapter_interrupts_82576,
2548571SChenlu.Chen@Sun.COM 	igb_setup_msix_82576,
2558571SChenlu.Chen@Sun.COM 
2568571SChenlu.Chen@Sun.COM 	/* capabilities */
2578571SChenlu.Chen@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
2588571SChenlu.Chen@Sun.COM 	IGB_FLAG_VMDQ_POOL |
2598955SChenlu.Chen@Sun.COM 	IGB_FLAG_NEED_CTX_IDX),
2608955SChenlu.Chen@Sun.COM 
2618955SChenlu.Chen@Sun.COM 	0xffe00000		/* mask for RXDCTL register */
2628571SChenlu.Chen@Sun.COM };
2638571SChenlu.Chen@Sun.COM 
26411155SJason.Xu@Sun.COM static adapter_info_t igb_82580_cap = {
26511155SJason.Xu@Sun.COM 	/* limits */
26611155SJason.Xu@Sun.COM 	8,		/* maximum number of rx queues */
26711155SJason.Xu@Sun.COM 	1,		/* minimum number of rx queues */
26811155SJason.Xu@Sun.COM 	4,		/* default number of rx queues */
26911155SJason.Xu@Sun.COM 	8,		/* maximum number of tx queues */
27011155SJason.Xu@Sun.COM 	1,		/* minimum number of tx queues */
27111155SJason.Xu@Sun.COM 	4,		/* default number of tx queues */
27211155SJason.Xu@Sun.COM 	65535,		/* maximum interrupt throttle rate */
27311155SJason.Xu@Sun.COM 	0,		/* minimum interrupt throttle rate */
27411155SJason.Xu@Sun.COM 	200,		/* default interrupt throttle rate */
27511155SJason.Xu@Sun.COM 
27611155SJason.Xu@Sun.COM 	/* function pointers */
27711155SJason.Xu@Sun.COM 	igb_enable_adapter_interrupts_82580,
27811155SJason.Xu@Sun.COM 	igb_setup_msix_82580,
27911155SJason.Xu@Sun.COM 
28011155SJason.Xu@Sun.COM 	/* capabilities */
28111155SJason.Xu@Sun.COM 	(IGB_FLAG_HAS_DCA |	/* capability flags */
28211155SJason.Xu@Sun.COM 	IGB_FLAG_VMDQ_POOL |
28311155SJason.Xu@Sun.COM 	IGB_FLAG_NEED_CTX_IDX),
28411155SJason.Xu@Sun.COM 
28511155SJason.Xu@Sun.COM 	0xffe00000		/* mask for RXDCTL register */
28611155SJason.Xu@Sun.COM };
28711155SJason.Xu@Sun.COM 
2888571SChenlu.Chen@Sun.COM /*
2895779Sxy150489  * Module Initialization Functions
2905779Sxy150489  */
2915779Sxy150489 
2925779Sxy150489 int
_init(void)2935779Sxy150489 _init(void)
2945779Sxy150489 {
2955779Sxy150489 	int status;
2965779Sxy150489 
2975779Sxy150489 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
2985779Sxy150489 
2995779Sxy150489 	status = mod_install(&igb_modlinkage);
3005779Sxy150489 
3015779Sxy150489 	if (status != DDI_SUCCESS) {
3025779Sxy150489 		mac_fini_ops(&igb_dev_ops);
3035779Sxy150489 	}
3045779Sxy150489 
3055779Sxy150489 	return (status);
3065779Sxy150489 }
3075779Sxy150489 
3085779Sxy150489 int
_fini(void)3095779Sxy150489 _fini(void)
3105779Sxy150489 {
3115779Sxy150489 	int status;
3125779Sxy150489 
3135779Sxy150489 	status = mod_remove(&igb_modlinkage);
3145779Sxy150489 
3155779Sxy150489 	if (status == DDI_SUCCESS) {
3165779Sxy150489 		mac_fini_ops(&igb_dev_ops);
3175779Sxy150489 	}
3185779Sxy150489 
3195779Sxy150489 	return (status);
3205779Sxy150489 
3215779Sxy150489 }
3225779Sxy150489 
3235779Sxy150489 int
_info(struct modinfo * modinfop)3245779Sxy150489 _info(struct modinfo *modinfop)
3255779Sxy150489 {
3265779Sxy150489 	int status;
3275779Sxy150489 
3285779Sxy150489 	status = mod_info(&igb_modlinkage, modinfop);
3295779Sxy150489 
3305779Sxy150489 	return (status);
3315779Sxy150489 }
3325779Sxy150489 
3335779Sxy150489 /*
3345779Sxy150489  * igb_attach - driver attach
3355779Sxy150489  *
3365779Sxy150489  * This function is the device specific initialization entry
3375779Sxy150489  * point. This entry point is required and must be written.
3385779Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
3395779Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
3405779Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
3415779Sxy150489  * available for use by the driver.
3425779Sxy150489  *
3435779Sxy150489  * The attach() function will be called once for each instance
3445779Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
3455779Sxy150489  * Until attach() succeeds, the only driver entry points which
3465779Sxy150489  * may be called are open(9E) and getinfo(9E).
3475779Sxy150489  */
3485779Sxy150489 static int
igb_attach(dev_info_t * devinfo,ddi_attach_cmd_t cmd)3495779Sxy150489 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
3505779Sxy150489 {
3515779Sxy150489 	igb_t *igb;
3525779Sxy150489 	struct igb_osdep *osdep;
3535779Sxy150489 	struct e1000_hw *hw;
3545779Sxy150489 	int instance;
3555779Sxy150489 
3565779Sxy150489 	/*
3575779Sxy150489 	 * Check the command and perform corresponding operations
3585779Sxy150489 	 */
3595779Sxy150489 	switch (cmd) {
3605779Sxy150489 	default:
3615779Sxy150489 		return (DDI_FAILURE);
3625779Sxy150489 
3635779Sxy150489 	case DDI_RESUME:
3645779Sxy150489 		return (igb_resume(devinfo));
3655779Sxy150489 
3665779Sxy150489 	case DDI_ATTACH:
3675779Sxy150489 		break;
3685779Sxy150489 	}
3695779Sxy150489 
3705779Sxy150489 	/* Get the device instance */
3715779Sxy150489 	instance = ddi_get_instance(devinfo);
3725779Sxy150489 
3735779Sxy150489 	/* Allocate memory for the instance data structure */
3745779Sxy150489 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
3755779Sxy150489 
3765779Sxy150489 	igb->dip = devinfo;
3775779Sxy150489 	igb->instance = instance;
3785779Sxy150489 
3795779Sxy150489 	hw = &igb->hw;
3805779Sxy150489 	osdep = &igb->osdep;
3815779Sxy150489 	hw->back = osdep;
3825779Sxy150489 	osdep->igb = igb;
3835779Sxy150489 
3845779Sxy150489 	/* Attach the instance pointer to the dev_info data structure */
3855779Sxy150489 	ddi_set_driver_private(devinfo, igb);
3865779Sxy150489 
3876624Sgl147354 
3886624Sgl147354 	/* Initialize for fma support */
3896624Sgl147354 	igb->fm_capabilities = igb_get_prop(igb, "fm-capable",
3906624Sgl147354 	    0, 0x0f,
3916624Sgl147354 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
3926624Sgl147354 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
3936624Sgl147354 	igb_fm_init(igb);
3946624Sgl147354 	igb->attach_progress |= ATTACH_PROGRESS_FMINIT;
3956624Sgl147354 
3965779Sxy150489 	/*
3975779Sxy150489 	 * Map PCI config space registers
3985779Sxy150489 	 */
3995779Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
4005779Sxy150489 		igb_error(igb, "Failed to map PCI configurations");
4015779Sxy150489 		goto attach_fail;
4025779Sxy150489 	}
4035779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
4045779Sxy150489 
4055779Sxy150489 	/*
4065779Sxy150489 	 * Identify the chipset family
4075779Sxy150489 	 */
4085779Sxy150489 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
4095779Sxy150489 		igb_error(igb, "Failed to identify hardware");
4105779Sxy150489 		goto attach_fail;
4115779Sxy150489 	}
4125779Sxy150489 
4135779Sxy150489 	/*
4145779Sxy150489 	 * Map device registers
4155779Sxy150489 	 */
4165779Sxy150489 	if (igb_regs_map(igb) != IGB_SUCCESS) {
4175779Sxy150489 		igb_error(igb, "Failed to map device registers");
4185779Sxy150489 		goto attach_fail;
4195779Sxy150489 	}
4205779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
4215779Sxy150489 
4225779Sxy150489 	/*
4235779Sxy150489 	 * Initialize driver parameters
4245779Sxy150489 	 */
4255779Sxy150489 	igb_init_properties(igb);
4265779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
4275779Sxy150489 
4285779Sxy150489 	/*
4295779Sxy150489 	 * Allocate interrupts
4305779Sxy150489 	 */
4315779Sxy150489 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
4325779Sxy150489 		igb_error(igb, "Failed to allocate interrupts");
4335779Sxy150489 		goto attach_fail;
4345779Sxy150489 	}
4355779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
4365779Sxy150489 
4375779Sxy150489 	/*
4385779Sxy150489 	 * Allocate rx/tx rings based on the ring numbers.
4395779Sxy150489 	 * The actual numbers of rx/tx rings are decided by the number of
4405779Sxy150489 	 * allocated interrupt vectors, so we should allocate the rings after
4415779Sxy150489 	 * interrupts are allocated.
4425779Sxy150489 	 */
4435779Sxy150489 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
4448275SEric Cheng 		igb_error(igb, "Failed to allocate rx/tx rings or groups");
4455779Sxy150489 		goto attach_fail;
4465779Sxy150489 	}
4475779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
4485779Sxy150489 
4495779Sxy150489 	/*
4505779Sxy150489 	 * Add interrupt handlers
4515779Sxy150489 	 */
4525779Sxy150489 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
4535779Sxy150489 		igb_error(igb, "Failed to add interrupt handlers");
4545779Sxy150489 		goto attach_fail;
4555779Sxy150489 	}
4565779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
4575779Sxy150489 
4585779Sxy150489 	/*
4595779Sxy150489 	 * Initialize driver parameters
4605779Sxy150489 	 */
4615779Sxy150489 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
4625779Sxy150489 		igb_error(igb, "Failed to initialize driver settings");
4635779Sxy150489 		goto attach_fail;
4645779Sxy150489 	}
4655779Sxy150489 
4666624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
4676624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
4686624Sgl147354 		goto attach_fail;
4696624Sgl147354 	}
4706624Sgl147354 
4715779Sxy150489 	/*
4725779Sxy150489 	 * Initialize mutexes for this device.
4735779Sxy150489 	 * Do this before enabling the interrupt handler and
4745779Sxy150489 	 * register the softint to avoid the condition where
4755779Sxy150489 	 * interrupt handler can try using uninitialized mutex
4765779Sxy150489 	 */
4775779Sxy150489 	igb_init_locks(igb);
4785779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
4795779Sxy150489 
4805779Sxy150489 	/*
48111502SChenlu.Chen@Sun.COM 	 * Initialize the adapter
4828955SChenlu.Chen@Sun.COM 	 */
4835779Sxy150489 	if (igb_init(igb) != IGB_SUCCESS) {
4845779Sxy150489 		igb_error(igb, "Failed to initialize adapter");
4855779Sxy150489 		goto attach_fail;
4865779Sxy150489 	}
4878955SChenlu.Chen@Sun.COM 	igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
4885779Sxy150489 
4895779Sxy150489 	/*
4905779Sxy150489 	 * Initialize statistics
4915779Sxy150489 	 */
4925779Sxy150489 	if (igb_init_stats(igb) != IGB_SUCCESS) {
4935779Sxy150489 		igb_error(igb, "Failed to initialize statistics");
4945779Sxy150489 		goto attach_fail;
4955779Sxy150489 	}
4965779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_STATS;
4975779Sxy150489 
4985779Sxy150489 	/*
4995779Sxy150489 	 * Register the driver to the MAC
5005779Sxy150489 	 */
5015779Sxy150489 	if (igb_register_mac(igb) != IGB_SUCCESS) {
5025779Sxy150489 		igb_error(igb, "Failed to register MAC");
5035779Sxy150489 		goto attach_fail;
5045779Sxy150489 	}
5055779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_MAC;
5065779Sxy150489 
5075779Sxy150489 	/*
5085779Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
5095779Sxy150489 	 * initialized, enable interrupts.
5105779Sxy150489 	 */
5115779Sxy150489 	if (igb_enable_intrs(igb) != IGB_SUCCESS) {
5125779Sxy150489 		igb_error(igb, "Failed to enable DDI interrupts");
5135779Sxy150489 		goto attach_fail;
5145779Sxy150489 	}
5155779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
5165779Sxy150489 
5178571SChenlu.Chen@Sun.COM 	igb_log(igb, "%s", igb_version);
51811367SJason.Xu@Sun.COM 	atomic_or_32(&igb->igb_state, IGB_INITIALIZED);
5195779Sxy150489 
5205779Sxy150489 	return (DDI_SUCCESS);
5215779Sxy150489 
5225779Sxy150489 attach_fail:
5235779Sxy150489 	igb_unconfigure(devinfo, igb);
5245779Sxy150489 	return (DDI_FAILURE);
5255779Sxy150489 }
5265779Sxy150489 
5275779Sxy150489 /*
5285779Sxy150489  * igb_detach - driver detach
5295779Sxy150489  *
5305779Sxy150489  * The detach() function is the complement of the attach routine.
5315779Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
5325779Sxy150489  * state  associated  with  a  given  instance of a device node
5335779Sxy150489  * prior to the removal of that instance from the system.
5345779Sxy150489  *
5355779Sxy150489  * The detach() function will be called once for each  instance
5365779Sxy150489  * of the device for which there has been a successful attach()
5375779Sxy150489  * once there are no longer  any  opens  on  the  device.
5385779Sxy150489  *
5395779Sxy150489  * Interrupts routine are disabled, All memory allocated by this
5405779Sxy150489  * driver are freed.
5415779Sxy150489  */
5425779Sxy150489 static int
igb_detach(dev_info_t * devinfo,ddi_detach_cmd_t cmd)5435779Sxy150489 igb_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
5445779Sxy150489 {
5455779Sxy150489 	igb_t *igb;
5465779Sxy150489 
5475779Sxy150489 	/*
5485779Sxy150489 	 * Check detach command
5495779Sxy150489 	 */
5505779Sxy150489 	switch (cmd) {
5515779Sxy150489 	default:
5525779Sxy150489 		return (DDI_FAILURE);
5535779Sxy150489 
5545779Sxy150489 	case DDI_SUSPEND:
5555779Sxy150489 		return (igb_suspend(devinfo));
5565779Sxy150489 
5575779Sxy150489 	case DDI_DETACH:
5585779Sxy150489 		break;
5595779Sxy150489 	}
5605779Sxy150489 
5615779Sxy150489 
5625779Sxy150489 	/*
5635779Sxy150489 	 * Get the pointer to the driver private data structure
5645779Sxy150489 	 */
5655779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
5665779Sxy150489 	if (igb == NULL)
5675779Sxy150489 		return (DDI_FAILURE);
5685779Sxy150489 
5695779Sxy150489 	/*
5705779Sxy150489 	 * Unregister MAC. If failed, we have to fail the detach
5715779Sxy150489 	 */
5725779Sxy150489 	if (mac_unregister(igb->mac_hdl) != 0) {
5735779Sxy150489 		igb_error(igb, "Failed to unregister MAC");
5745779Sxy150489 		return (DDI_FAILURE);
5755779Sxy150489 	}
5765779Sxy150489 	igb->attach_progress &= ~ATTACH_PROGRESS_MAC;
5775779Sxy150489 
5785779Sxy150489 	/*
5795779Sxy150489 	 * If the device is still running, it needs to be stopped first.
5805779Sxy150489 	 * This check is necessary because under some specific circumstances,
5815779Sxy150489 	 * the detach routine can be called without stopping the interface
5825779Sxy150489 	 * first.
5835779Sxy150489 	 */
5845779Sxy150489 	mutex_enter(&igb->gen_lock);
5855779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
58611367SJason.Xu@Sun.COM 		atomic_and_32(&igb->igb_state, ~IGB_STARTED);
58711502SChenlu.Chen@Sun.COM 		igb_stop(igb, B_TRUE);
5885779Sxy150489 		mutex_exit(&igb->gen_lock);
5895779Sxy150489 		/* Disable and stop the watchdog timer */
5905779Sxy150489 		igb_disable_watchdog_timer(igb);
5915779Sxy150489 	} else
5925779Sxy150489 		mutex_exit(&igb->gen_lock);
5935779Sxy150489 
5945779Sxy150489 	/*
5955779Sxy150489 	 * Check if there are still rx buffers held by the upper layer.
5965779Sxy150489 	 * If so, fail the detach.
5975779Sxy150489 	 */
5985779Sxy150489 	if (!igb_rx_drain(igb))
5995779Sxy150489 		return (DDI_FAILURE);
6005779Sxy150489 
6015779Sxy150489 	/*
6025779Sxy150489 	 * Do the remaining unconfigure routines
6035779Sxy150489 	 */
6045779Sxy150489 	igb_unconfigure(devinfo, igb);
6055779Sxy150489 
6065779Sxy150489 	return (DDI_SUCCESS);
6075779Sxy150489 }
6085779Sxy150489 
6097656SSherry.Moore@Sun.COM /*
6107656SSherry.Moore@Sun.COM  * quiesce(9E) entry point.
6117656SSherry.Moore@Sun.COM  *
6127656SSherry.Moore@Sun.COM  * This function is called when the system is single-threaded at high
6137656SSherry.Moore@Sun.COM  * PIL with preemption disabled. Therefore, this function must not be
6147656SSherry.Moore@Sun.COM  * blocked.
6157656SSherry.Moore@Sun.COM  *
6167656SSherry.Moore@Sun.COM  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
6177656SSherry.Moore@Sun.COM  * DDI_FAILURE indicates an error condition and should almost never happen.
6187656SSherry.Moore@Sun.COM  */
6197656SSherry.Moore@Sun.COM static int
igb_quiesce(dev_info_t * devinfo)6207656SSherry.Moore@Sun.COM igb_quiesce(dev_info_t *devinfo)
6217656SSherry.Moore@Sun.COM {
6227656SSherry.Moore@Sun.COM 	igb_t *igb;
6237656SSherry.Moore@Sun.COM 	struct e1000_hw *hw;
6247656SSherry.Moore@Sun.COM 
6257656SSherry.Moore@Sun.COM 	igb = (igb_t *)ddi_get_driver_private(devinfo);
6267656SSherry.Moore@Sun.COM 
6277656SSherry.Moore@Sun.COM 	if (igb == NULL)
6287656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
6297656SSherry.Moore@Sun.COM 
6307656SSherry.Moore@Sun.COM 	hw = &igb->hw;
6317656SSherry.Moore@Sun.COM 
6327656SSherry.Moore@Sun.COM 	/*
6337656SSherry.Moore@Sun.COM 	 * Disable the adapter interrupts
6347656SSherry.Moore@Sun.COM 	 */
6357656SSherry.Moore@Sun.COM 	igb_disable_adapter_interrupts(igb);
6367656SSherry.Moore@Sun.COM 
6377656SSherry.Moore@Sun.COM 	/* Tell firmware driver is no longer in control */
6387656SSherry.Moore@Sun.COM 	igb_release_driver_control(hw);
6397656SSherry.Moore@Sun.COM 
6407656SSherry.Moore@Sun.COM 	/*
6417656SSherry.Moore@Sun.COM 	 * Reset the chipset
6427656SSherry.Moore@Sun.COM 	 */
6437656SSherry.Moore@Sun.COM 	(void) e1000_reset_hw(hw);
6447656SSherry.Moore@Sun.COM 
6457656SSherry.Moore@Sun.COM 	/*
6467656SSherry.Moore@Sun.COM 	 * Reset PHY if possible
6477656SSherry.Moore@Sun.COM 	 */
6487656SSherry.Moore@Sun.COM 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
6497656SSherry.Moore@Sun.COM 		(void) e1000_phy_hw_reset(hw);
6507656SSherry.Moore@Sun.COM 
6517656SSherry.Moore@Sun.COM 	return (DDI_SUCCESS);
6527656SSherry.Moore@Sun.COM }
6537656SSherry.Moore@Sun.COM 
6548955SChenlu.Chen@Sun.COM /*
6558955SChenlu.Chen@Sun.COM  * igb_unconfigure - release all resources held by this instance
6568955SChenlu.Chen@Sun.COM  */
6575779Sxy150489 static void
igb_unconfigure(dev_info_t * devinfo,igb_t * igb)6585779Sxy150489 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
6595779Sxy150489 {
6605779Sxy150489 	/*
6615779Sxy150489 	 * Disable interrupt
6625779Sxy150489 	 */
6635779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
6645779Sxy150489 		(void) igb_disable_intrs(igb);
6655779Sxy150489 	}
6665779Sxy150489 
6675779Sxy150489 	/*
6685779Sxy150489 	 * Unregister MAC
6695779Sxy150489 	 */
6705779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
6715779Sxy150489 		(void) mac_unregister(igb->mac_hdl);
6725779Sxy150489 	}
6735779Sxy150489 
6745779Sxy150489 	/*
6755779Sxy150489 	 * Free statistics
6765779Sxy150489 	 */
6775779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
6785779Sxy150489 		kstat_delete((kstat_t *)igb->igb_ks);
6795779Sxy150489 	}
6805779Sxy150489 
6815779Sxy150489 	/*
6825779Sxy150489 	 * Remove interrupt handlers
6835779Sxy150489 	 */
6845779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
6855779Sxy150489 		igb_rem_intr_handlers(igb);
6865779Sxy150489 	}
6875779Sxy150489 
6885779Sxy150489 	/*
6895779Sxy150489 	 * Remove interrupts
6905779Sxy150489 	 */
6915779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
6925779Sxy150489 		igb_rem_intrs(igb);
6935779Sxy150489 	}
6945779Sxy150489 
6955779Sxy150489 	/*
6965779Sxy150489 	 * Remove driver properties
6975779Sxy150489 	 */
6985779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
6995779Sxy150489 		(void) ddi_prop_remove_all(devinfo);
7005779Sxy150489 	}
7015779Sxy150489 
7025779Sxy150489 	/*
7038955SChenlu.Chen@Sun.COM 	 * Stop the adapter
7045779Sxy150489 	 */
7058955SChenlu.Chen@Sun.COM 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) {
7065779Sxy150489 		mutex_enter(&igb->gen_lock);
7078955SChenlu.Chen@Sun.COM 		igb_stop_adapter(igb);
7085779Sxy150489 		mutex_exit(&igb->gen_lock);
7096624Sgl147354 		if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
7106624Sgl147354 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_UNAFFECTED);
7115779Sxy150489 	}
7125779Sxy150489 
7135779Sxy150489 	/*
7149775SVitezslav.Batrla@Sun.COM 	 * Free multicast table
7159775SVitezslav.Batrla@Sun.COM 	 */
7169775SVitezslav.Batrla@Sun.COM 	igb_release_multicast(igb);
7179775SVitezslav.Batrla@Sun.COM 
7189775SVitezslav.Batrla@Sun.COM 	/*
7195779Sxy150489 	 * Free register handle
7205779Sxy150489 	 */
7215779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
7225779Sxy150489 		if (igb->osdep.reg_handle != NULL)
7235779Sxy150489 			ddi_regs_map_free(&igb->osdep.reg_handle);
7245779Sxy150489 	}
7255779Sxy150489 
7265779Sxy150489 	/*
7275779Sxy150489 	 * Free PCI config handle
7285779Sxy150489 	 */
7295779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
7305779Sxy150489 		if (igb->osdep.cfg_handle != NULL)
7315779Sxy150489 			pci_config_teardown(&igb->osdep.cfg_handle);
7325779Sxy150489 	}
7335779Sxy150489 
7345779Sxy150489 	/*
7355779Sxy150489 	 * Free locks
7365779Sxy150489 	 */
7375779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
7385779Sxy150489 		igb_destroy_locks(igb);
7395779Sxy150489 	}
7405779Sxy150489 
7415779Sxy150489 	/*
7425779Sxy150489 	 * Free the rx/tx rings
7435779Sxy150489 	 */
7445779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
7455779Sxy150489 		igb_free_rings(igb);
7465779Sxy150489 	}
7475779Sxy150489 
7485779Sxy150489 	/*
7496624Sgl147354 	 * Remove FMA
7506624Sgl147354 	 */
7516624Sgl147354 	if (igb->attach_progress & ATTACH_PROGRESS_FMINIT) {
7526624Sgl147354 		igb_fm_fini(igb);
7536624Sgl147354 	}
7546624Sgl147354 
7556624Sgl147354 	/*
7565779Sxy150489 	 * Free the driver data structure
7575779Sxy150489 	 */
7585779Sxy150489 	kmem_free(igb, sizeof (igb_t));
7595779Sxy150489 
7605779Sxy150489 	ddi_set_driver_private(devinfo, NULL);
7615779Sxy150489 }
7625779Sxy150489 
7635779Sxy150489 /*
7645779Sxy150489  * igb_register_mac - Register the driver and its function pointers with
7655779Sxy150489  * the GLD interface
7665779Sxy150489  */
7675779Sxy150489 static int
igb_register_mac(igb_t * igb)7685779Sxy150489 igb_register_mac(igb_t *igb)
7695779Sxy150489 {
7705779Sxy150489 	struct e1000_hw *hw = &igb->hw;
7715779Sxy150489 	mac_register_t *mac;
7725779Sxy150489 	int status;
7735779Sxy150489 
7745779Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
7755779Sxy150489 		return (IGB_FAILURE);
7765779Sxy150489 
7775779Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
7785779Sxy150489 	mac->m_driver = igb;
7795779Sxy150489 	mac->m_dip = igb->dip;
7805779Sxy150489 	mac->m_src_addr = hw->mac.addr;
7815779Sxy150489 	mac->m_callbacks = &igb_m_callbacks;
7825779Sxy150489 	mac->m_min_sdu = 0;
7835779Sxy150489 	mac->m_max_sdu = igb->max_frame_size -
7845779Sxy150489 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
7855895Syz147064 	mac->m_margin = VLAN_TAGSZ;
78611502SChenlu.Chen@Sun.COM 	mac->m_priv_props = igb_priv_props;
7878275SEric Cheng 	mac->m_v12n = MAC_VIRT_LEVEL1;
7885779Sxy150489 
7895779Sxy150489 	status = mac_register(mac, &igb->mac_hdl);
7905779Sxy150489 
7915779Sxy150489 	mac_free(mac);
7925779Sxy150489 
7935779Sxy150489 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
7945779Sxy150489 }
7955779Sxy150489 
7965779Sxy150489 /*
7975779Sxy150489  * igb_identify_hardware - Identify the type of the chipset
7985779Sxy150489  */
7995779Sxy150489 static int
igb_identify_hardware(igb_t * igb)8005779Sxy150489 igb_identify_hardware(igb_t *igb)
8015779Sxy150489 {
8025779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8035779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
8045779Sxy150489 
8055779Sxy150489 	/*
8065779Sxy150489 	 * Get the device id
8075779Sxy150489 	 */
8085779Sxy150489 	hw->vendor_id =
8095779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
8105779Sxy150489 	hw->device_id =
8115779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
8125779Sxy150489 	hw->revision_id =
8135779Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
8145779Sxy150489 	hw->subsystem_device_id =
8155779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
8165779Sxy150489 	hw->subsystem_vendor_id =
8175779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
8185779Sxy150489 
8195779Sxy150489 	/*
8205779Sxy150489 	 * Set the mac type of the adapter based on the device id
8215779Sxy150489 	 */
8225779Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
8235779Sxy150489 		return (IGB_FAILURE);
8245779Sxy150489 	}
8255779Sxy150489 
8268571SChenlu.Chen@Sun.COM 	/*
8278571SChenlu.Chen@Sun.COM 	 * Install adapter capabilities based on mac type
8288571SChenlu.Chen@Sun.COM 	 */
8298571SChenlu.Chen@Sun.COM 	switch (hw->mac.type) {
8308571SChenlu.Chen@Sun.COM 	case e1000_82575:
8318571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82575_cap;
8328571SChenlu.Chen@Sun.COM 		break;
8338571SChenlu.Chen@Sun.COM 	case e1000_82576:
8348571SChenlu.Chen@Sun.COM 		igb->capab = &igb_82576_cap;
8358571SChenlu.Chen@Sun.COM 		break;
83611155SJason.Xu@Sun.COM 	case e1000_82580:
83711155SJason.Xu@Sun.COM 		igb->capab = &igb_82580_cap;
83811155SJason.Xu@Sun.COM 		break;
8398571SChenlu.Chen@Sun.COM 	default:
8408571SChenlu.Chen@Sun.COM 		return (IGB_FAILURE);
8418571SChenlu.Chen@Sun.COM 	}
8428571SChenlu.Chen@Sun.COM 
8435779Sxy150489 	return (IGB_SUCCESS);
8445779Sxy150489 }
8455779Sxy150489 
8465779Sxy150489 /*
8475779Sxy150489  * igb_regs_map - Map the device registers
8485779Sxy150489  */
8495779Sxy150489 static int
igb_regs_map(igb_t * igb)8505779Sxy150489 igb_regs_map(igb_t *igb)
8515779Sxy150489 {
8525779Sxy150489 	dev_info_t *devinfo = igb->dip;
8535779Sxy150489 	struct e1000_hw *hw = &igb->hw;
8545779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
8555779Sxy150489 	off_t mem_size;
8565779Sxy150489 
8575779Sxy150489 	/*
8585779Sxy150489 	 * First get the size of device registers to be mapped.
8595779Sxy150489 	 */
8608571SChenlu.Chen@Sun.COM 	if (ddi_dev_regsize(devinfo, IGB_ADAPTER_REGSET, &mem_size) !=
8618571SChenlu.Chen@Sun.COM 	    DDI_SUCCESS) {
8625779Sxy150489 		return (IGB_FAILURE);
8635779Sxy150489 	}
8645779Sxy150489 
8655779Sxy150489 	/*
8665779Sxy150489 	 * Call ddi_regs_map_setup() to map registers
8675779Sxy150489 	 */
8688571SChenlu.Chen@Sun.COM 	if ((ddi_regs_map_setup(devinfo, IGB_ADAPTER_REGSET,
8695779Sxy150489 	    (caddr_t *)&hw->hw_addr, 0,
8705779Sxy150489 	    mem_size, &igb_regs_acc_attr,
8715779Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
8725779Sxy150489 		return (IGB_FAILURE);
8735779Sxy150489 	}
8745779Sxy150489 
8755779Sxy150489 	return (IGB_SUCCESS);
8765779Sxy150489 }
8775779Sxy150489 
8785779Sxy150489 /*
8795779Sxy150489  * igb_init_properties - Initialize driver properties
8805779Sxy150489  */
8815779Sxy150489 static void
igb_init_properties(igb_t * igb)8825779Sxy150489 igb_init_properties(igb_t *igb)
8835779Sxy150489 {
8845779Sxy150489 	/*
8855779Sxy150489 	 * Get conf file properties, including link settings
8865779Sxy150489 	 * jumbo frames, ring number, descriptor number, etc.
8875779Sxy150489 	 */
8885779Sxy150489 	igb_get_conf(igb);
8895779Sxy150489 }
8905779Sxy150489 
8915779Sxy150489 /*
8925779Sxy150489  * igb_init_driver_settings - Initialize driver settings
8935779Sxy150489  *
8945779Sxy150489  * The settings include hardware function pointers, bus information,
8955779Sxy150489  * rx/tx rings settings, link state, and any other parameters that
8965779Sxy150489  * need to be setup during driver initialization.
8975779Sxy150489  */
8985779Sxy150489 static int
igb_init_driver_settings(igb_t * igb)8995779Sxy150489 igb_init_driver_settings(igb_t *igb)
9005779Sxy150489 {
9015779Sxy150489 	struct e1000_hw *hw = &igb->hw;
9025779Sxy150489 	igb_rx_ring_t *rx_ring;
9035779Sxy150489 	igb_tx_ring_t *tx_ring;
9045779Sxy150489 	uint32_t rx_size;
9055779Sxy150489 	uint32_t tx_size;
9065779Sxy150489 	int i;
9075779Sxy150489 
9085779Sxy150489 	/*
9095779Sxy150489 	 * Initialize chipset specific hardware function pointers
9105779Sxy150489 	 */
9115779Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
9125779Sxy150489 		return (IGB_FAILURE);
9135779Sxy150489 	}
9145779Sxy150489 
9155779Sxy150489 	/*
9165779Sxy150489 	 * Get bus information
9175779Sxy150489 	 */
9185779Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
9195779Sxy150489 		return (IGB_FAILURE);
9205779Sxy150489 	}
9215779Sxy150489 
9225779Sxy150489 	/*
9239188SPaul.Guo@Sun.COM 	 * Get the system page size
9249188SPaul.Guo@Sun.COM 	 */
9259188SPaul.Guo@Sun.COM 	igb->page_size = ddi_ptob(igb->dip, (ulong_t)1);
9269188SPaul.Guo@Sun.COM 
9279188SPaul.Guo@Sun.COM 	/*
9285779Sxy150489 	 * Set rx buffer size
9295779Sxy150489 	 * The IP header alignment room is counted in the calculation.
9305779Sxy150489 	 * The rx buffer size is in unit of 1K that is required by the
9315779Sxy150489 	 * chipset hardware.
9325779Sxy150489 	 */
9335779Sxy150489 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
9345779Sxy150489 	igb->rx_buf_size = ((rx_size >> 10) +
9355779Sxy150489 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9365779Sxy150489 
9375779Sxy150489 	/*
9385779Sxy150489 	 * Set tx buffer size
9395779Sxy150489 	 */
9405779Sxy150489 	tx_size = igb->max_frame_size;
9415779Sxy150489 	igb->tx_buf_size = ((tx_size >> 10) +
9425779Sxy150489 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
9435779Sxy150489 
9445779Sxy150489 	/*
9455779Sxy150489 	 * Initialize rx/tx rings parameters
9465779Sxy150489 	 */
9475779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9485779Sxy150489 		rx_ring = &igb->rx_rings[i];
9495779Sxy150489 		rx_ring->index = i;
9505779Sxy150489 		rx_ring->igb = igb;
9515779Sxy150489 	}
9525779Sxy150489 
9535779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9545779Sxy150489 		tx_ring = &igb->tx_rings[i];
9555779Sxy150489 		tx_ring->index = i;
9565779Sxy150489 		tx_ring->igb = igb;
9575779Sxy150489 		if (igb->tx_head_wb_enable)
9585779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
9595779Sxy150489 		else
9605779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
9615779Sxy150489 
9625779Sxy150489 		tx_ring->ring_size = igb->tx_ring_size;
9635779Sxy150489 		tx_ring->free_list_size = igb->tx_ring_size +
9645779Sxy150489 		    (igb->tx_ring_size >> 1);
9655779Sxy150489 	}
9665779Sxy150489 
9675779Sxy150489 	/*
9688571SChenlu.Chen@Sun.COM 	 * Initialize values of interrupt throttling rates
9695779Sxy150489 	 */
9705779Sxy150489 	for (i = 1; i < MAX_NUM_EITR; i++)
9715779Sxy150489 		igb->intr_throttling[i] = igb->intr_throttling[0];
9725779Sxy150489 
9735779Sxy150489 	/*
9745779Sxy150489 	 * The initial link state should be "unknown"
9755779Sxy150489 	 */
9765779Sxy150489 	igb->link_state = LINK_STATE_UNKNOWN;
9775779Sxy150489 
9785779Sxy150489 	return (IGB_SUCCESS);
9795779Sxy150489 }
9805779Sxy150489 
9815779Sxy150489 /*
9825779Sxy150489  * igb_init_locks - Initialize locks
9835779Sxy150489  */
9845779Sxy150489 static void
igb_init_locks(igb_t * igb)9855779Sxy150489 igb_init_locks(igb_t *igb)
9865779Sxy150489 {
9875779Sxy150489 	igb_rx_ring_t *rx_ring;
9885779Sxy150489 	igb_tx_ring_t *tx_ring;
9895779Sxy150489 	int i;
9905779Sxy150489 
9915779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
9925779Sxy150489 		rx_ring = &igb->rx_rings[i];
9935779Sxy150489 		mutex_init(&rx_ring->rx_lock, NULL,
9945779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
9955779Sxy150489 	}
9965779Sxy150489 
9975779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
9985779Sxy150489 		tx_ring = &igb->tx_rings[i];
9995779Sxy150489 		mutex_init(&tx_ring->tx_lock, NULL,
10005779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10015779Sxy150489 		mutex_init(&tx_ring->recycle_lock, NULL,
10025779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10035779Sxy150489 		mutex_init(&tx_ring->tcb_head_lock, NULL,
10045779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10055779Sxy150489 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
10065779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10075779Sxy150489 	}
10085779Sxy150489 
10095779Sxy150489 	mutex_init(&igb->gen_lock, NULL,
10105779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10115779Sxy150489 
10125779Sxy150489 	mutex_init(&igb->watchdog_lock, NULL,
10135779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
101411367SJason.Xu@Sun.COM 
101511367SJason.Xu@Sun.COM 	mutex_init(&igb->link_lock, NULL,
101611367SJason.Xu@Sun.COM 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
10175779Sxy150489 }
10185779Sxy150489 
10195779Sxy150489 /*
10205779Sxy150489  * igb_destroy_locks - Destroy locks
10215779Sxy150489  */
10225779Sxy150489 static void
igb_destroy_locks(igb_t * igb)10235779Sxy150489 igb_destroy_locks(igb_t *igb)
10245779Sxy150489 {
10255779Sxy150489 	igb_rx_ring_t *rx_ring;
10265779Sxy150489 	igb_tx_ring_t *tx_ring;
10275779Sxy150489 	int i;
10285779Sxy150489 
10295779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
10305779Sxy150489 		rx_ring = &igb->rx_rings[i];
10315779Sxy150489 		mutex_destroy(&rx_ring->rx_lock);
10325779Sxy150489 	}
10335779Sxy150489 
10345779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
10355779Sxy150489 		tx_ring = &igb->tx_rings[i];
10365779Sxy150489 		mutex_destroy(&tx_ring->tx_lock);
10375779Sxy150489 		mutex_destroy(&tx_ring->recycle_lock);
10385779Sxy150489 		mutex_destroy(&tx_ring->tcb_head_lock);
10395779Sxy150489 		mutex_destroy(&tx_ring->tcb_tail_lock);
10405779Sxy150489 	}
10415779Sxy150489 
10425779Sxy150489 	mutex_destroy(&igb->gen_lock);
10435779Sxy150489 	mutex_destroy(&igb->watchdog_lock);
104411367SJason.Xu@Sun.COM 	mutex_destroy(&igb->link_lock);
10455779Sxy150489 }
10465779Sxy150489 
10475779Sxy150489 static int
igb_resume(dev_info_t * devinfo)10485779Sxy150489 igb_resume(dev_info_t *devinfo)
10495779Sxy150489 {
10505779Sxy150489 	igb_t *igb;
10515779Sxy150489 
10525779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10535779Sxy150489 	if (igb == NULL)
10545779Sxy150489 		return (DDI_FAILURE);
10555779Sxy150489 
10565779Sxy150489 	mutex_enter(&igb->gen_lock);
10575779Sxy150489 
105812818SCrisson.Hu@Sun.COM 	/*
105912818SCrisson.Hu@Sun.COM 	 * Enable interrupts
106012818SCrisson.Hu@Sun.COM 	 */
106112818SCrisson.Hu@Sun.COM 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
106212818SCrisson.Hu@Sun.COM 		if (igb_enable_intrs(igb) != IGB_SUCCESS) {
106312818SCrisson.Hu@Sun.COM 			igb_error(igb, "Failed to enable DDI interrupts");
106412818SCrisson.Hu@Sun.COM 			mutex_exit(&igb->gen_lock);
106512818SCrisson.Hu@Sun.COM 			return (DDI_FAILURE);
106612818SCrisson.Hu@Sun.COM 		}
106712818SCrisson.Hu@Sun.COM 	}
106812818SCrisson.Hu@Sun.COM 
10695779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
107011502SChenlu.Chen@Sun.COM 		if (igb_start(igb, B_FALSE) != IGB_SUCCESS) {
10715779Sxy150489 			mutex_exit(&igb->gen_lock);
10725779Sxy150489 			return (DDI_FAILURE);
10735779Sxy150489 		}
10745779Sxy150489 
10755779Sxy150489 		/*
10765779Sxy150489 		 * Enable and start the watchdog timer
10775779Sxy150489 		 */
10785779Sxy150489 		igb_enable_watchdog_timer(igb);
10795779Sxy150489 	}
10805779Sxy150489 
108111367SJason.Xu@Sun.COM 	atomic_and_32(&igb->igb_state, ~IGB_SUSPENDED);
10825779Sxy150489 
10835779Sxy150489 	mutex_exit(&igb->gen_lock);
10845779Sxy150489 
10855779Sxy150489 	return (DDI_SUCCESS);
10865779Sxy150489 }
10875779Sxy150489 
10885779Sxy150489 static int
igb_suspend(dev_info_t * devinfo)10895779Sxy150489 igb_suspend(dev_info_t *devinfo)
10905779Sxy150489 {
10915779Sxy150489 	igb_t *igb;
10925779Sxy150489 
10935779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
10945779Sxy150489 	if (igb == NULL)
10955779Sxy150489 		return (DDI_FAILURE);
10965779Sxy150489 
10975779Sxy150489 	mutex_enter(&igb->gen_lock);
10985779Sxy150489 
109911367SJason.Xu@Sun.COM 	atomic_or_32(&igb->igb_state, IGB_SUSPENDED);
11005779Sxy150489 
110112818SCrisson.Hu@Sun.COM 	/*
110212818SCrisson.Hu@Sun.COM 	 * Disable interrupts
110312818SCrisson.Hu@Sun.COM 	 */
110412818SCrisson.Hu@Sun.COM 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
110512818SCrisson.Hu@Sun.COM 		(void) igb_disable_intrs(igb);
110612818SCrisson.Hu@Sun.COM 	}
110712818SCrisson.Hu@Sun.COM 
11088955SChenlu.Chen@Sun.COM 	if (!(igb->igb_state & IGB_STARTED)) {
11098955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->gen_lock);
11108955SChenlu.Chen@Sun.COM 		return (DDI_SUCCESS);
11118955SChenlu.Chen@Sun.COM 	}
11128955SChenlu.Chen@Sun.COM 
111311502SChenlu.Chen@Sun.COM 	igb_stop(igb, B_FALSE);
11145779Sxy150489 
11155779Sxy150489 	mutex_exit(&igb->gen_lock);
11165779Sxy150489 
11175779Sxy150489 	/*
11185779Sxy150489 	 * Disable and stop the watchdog timer
11195779Sxy150489 	 */
11205779Sxy150489 	igb_disable_watchdog_timer(igb);
11215779Sxy150489 
11225779Sxy150489 	return (DDI_SUCCESS);
11235779Sxy150489 }
11245779Sxy150489 
11255779Sxy150489 static int
igb_init(igb_t * igb)11265779Sxy150489 igb_init(igb_t *igb)
11275779Sxy150489 {
11288955SChenlu.Chen@Sun.COM 	mutex_enter(&igb->gen_lock);
11298955SChenlu.Chen@Sun.COM 
11308955SChenlu.Chen@Sun.COM 	/*
11318955SChenlu.Chen@Sun.COM 	 * Initilize the adapter
11328955SChenlu.Chen@Sun.COM 	 */
11338955SChenlu.Chen@Sun.COM 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
11348955SChenlu.Chen@Sun.COM 		mutex_exit(&igb->gen_lock);
11358955SChenlu.Chen@Sun.COM 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
11368955SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
11378955SChenlu.Chen@Sun.COM 		return (IGB_FAILURE);
11388955SChenlu.Chen@Sun.COM 	}
11398955SChenlu.Chen@Sun.COM 
11408955SChenlu.Chen@Sun.COM 	mutex_exit(&igb->gen_lock);
11418955SChenlu.Chen@Sun.COM 
11428955SChenlu.Chen@Sun.COM 	return (IGB_SUCCESS);
11438955SChenlu.Chen@Sun.COM }
11448955SChenlu.Chen@Sun.COM 
11458955SChenlu.Chen@Sun.COM /*
11468955SChenlu.Chen@Sun.COM  * igb_init_mac_address - Initialize the default MAC address
11478955SChenlu.Chen@Sun.COM  *
11488955SChenlu.Chen@Sun.COM  * On success, the MAC address is entered in the igb->hw.mac.addr
11498955SChenlu.Chen@Sun.COM  * and hw->mac.perm_addr fields and the adapter's RAR(0) receive
11508955SChenlu.Chen@Sun.COM  * address register.
11518955SChenlu.Chen@Sun.COM  *
11528955SChenlu.Chen@Sun.COM  * Important side effects:
11538955SChenlu.Chen@Sun.COM  * 1. adapter is reset - this is required to put it in a known state.
11548955SChenlu.Chen@Sun.COM  * 2. all of non-volatile memory (NVM) is read & checksummed - NVM is where
11558955SChenlu.Chen@Sun.COM  * MAC address and all default settings are stored, so a valid checksum
11568955SChenlu.Chen@Sun.COM  * is required.
11578955SChenlu.Chen@Sun.COM  */
11588955SChenlu.Chen@Sun.COM static int
igb_init_mac_address(igb_t * igb)11598955SChenlu.Chen@Sun.COM igb_init_mac_address(igb_t *igb)
11608955SChenlu.Chen@Sun.COM {
11615779Sxy150489 	struct e1000_hw *hw = &igb->hw;
11625779Sxy150489 
11638275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
11645779Sxy150489 
11655779Sxy150489 	/*
11665779Sxy150489 	 * Reset chipset to put the hardware in a known state
11678955SChenlu.Chen@Sun.COM 	 * before we try to get MAC address from NVM.
11685779Sxy150489 	 */
11696624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
11708955SChenlu.Chen@Sun.COM 		igb_error(igb, "Adapter reset failed.");
11718955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
11726624Sgl147354 	}
11735779Sxy150489 
11745779Sxy150489 	/*
11755779Sxy150489 	 * NVM validation
11765779Sxy150489 	 */
11775779Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
11785779Sxy150489 		/*
11795779Sxy150489 		 * Some PCI-E parts fail the first check due to
11805779Sxy150489 		 * the link being in sleep state.  Call it again,
11815779Sxy150489 		 * if it fails a second time its a real issue.
11825779Sxy150489 		 */
11835779Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
11845779Sxy150489 			igb_error(igb,
11855779Sxy150489 			    "Invalid NVM checksum. Please contact "
11865779Sxy150489 			    "the vendor to update the NVM.");
11878955SChenlu.Chen@Sun.COM 			goto init_mac_fail;
11885779Sxy150489 		}
11895779Sxy150489 	}
11905779Sxy150489 
11915779Sxy150489 	/*
11928955SChenlu.Chen@Sun.COM 	 * Get the mac address
11938955SChenlu.Chen@Sun.COM 	 * This function should handle SPARC case correctly.
11948955SChenlu.Chen@Sun.COM 	 */
11958955SChenlu.Chen@Sun.COM 	if (!igb_find_mac_address(igb)) {
11968955SChenlu.Chen@Sun.COM 		igb_error(igb, "Failed to get the mac address");
11978955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
11988955SChenlu.Chen@Sun.COM 	}
11998955SChenlu.Chen@Sun.COM 
12008955SChenlu.Chen@Sun.COM 	/* Validate mac address */
12018955SChenlu.Chen@Sun.COM 	if (!is_valid_mac_addr(hw->mac.addr)) {
12028955SChenlu.Chen@Sun.COM 		igb_error(igb, "Invalid mac address");
12038955SChenlu.Chen@Sun.COM 		goto init_mac_fail;
12048955SChenlu.Chen@Sun.COM 	}
12058955SChenlu.Chen@Sun.COM 
12068955SChenlu.Chen@Sun.COM 	return (IGB_SUCCESS);
12078955SChenlu.Chen@Sun.COM 
12088955SChenlu.Chen@Sun.COM init_mac_fail:
12098955SChenlu.Chen@Sun.COM 	return (IGB_FAILURE);
12108955SChenlu.Chen@Sun.COM }
12118955SChenlu.Chen@Sun.COM 
12128955SChenlu.Chen@Sun.COM /*
12138955SChenlu.Chen@Sun.COM  * igb_init_adapter - Initialize the adapter
12148955SChenlu.Chen@Sun.COM  */
12158955SChenlu.Chen@Sun.COM static int
igb_init_adapter(igb_t * igb)12168955SChenlu.Chen@Sun.COM igb_init_adapter(igb_t *igb)
12178955SChenlu.Chen@Sun.COM {
12188955SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
12198955SChenlu.Chen@Sun.COM 	uint32_t pba;
12208955SChenlu.Chen@Sun.COM 	uint32_t high_water;
12218955SChenlu.Chen@Sun.COM 	int i;
12228955SChenlu.Chen@Sun.COM 
12238955SChenlu.Chen@Sun.COM 	ASSERT(mutex_owned(&igb->gen_lock));
12248955SChenlu.Chen@Sun.COM 
12258955SChenlu.Chen@Sun.COM 	/*
12268955SChenlu.Chen@Sun.COM 	 * In order to obtain the default MAC address, this will reset the
12278955SChenlu.Chen@Sun.COM 	 * adapter and validate the NVM that the address and many other
12288955SChenlu.Chen@Sun.COM 	 * default settings come from.
12298955SChenlu.Chen@Sun.COM 	 */
12308955SChenlu.Chen@Sun.COM 	if (igb_init_mac_address(igb) != IGB_SUCCESS) {
12318955SChenlu.Chen@Sun.COM 		igb_error(igb, "Failed to initialize MAC address");
12328955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
12338955SChenlu.Chen@Sun.COM 	}
12348955SChenlu.Chen@Sun.COM 
12358955SChenlu.Chen@Sun.COM 	/*
12365779Sxy150489 	 * Setup flow control
12375779Sxy150489 	 *
12385779Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
12395779Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
12405779Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
12415779Sxy150489 	 * file.
12425779Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
12435779Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
12445779Sxy150489 	 * high-water mark.
12455779Sxy150489 	 * The high water mark must be low enough to fit one full frame above
12465779Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
12475779Sxy150489 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
12485779Sxy150489 	 * frame.
12495779Sxy150489 	 */
12508955SChenlu.Chen@Sun.COM 	/*
12518955SChenlu.Chen@Sun.COM 	 * The default setting of PBA is correct for 82575 and other supported
12528955SChenlu.Chen@Sun.COM 	 * adapters do not have the E1000_PBA register, so PBA value is only
12538955SChenlu.Chen@Sun.COM 	 * used for calculation here and is never written to the adapter.
12548955SChenlu.Chen@Sun.COM 	 */
12558571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
12568571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_34K;
12578571SChenlu.Chen@Sun.COM 	} else {
12588571SChenlu.Chen@Sun.COM 		pba = E1000_PBA_64K;
12598571SChenlu.Chen@Sun.COM 	}
12608571SChenlu.Chen@Sun.COM 
12615779Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
12625779Sxy150489 	    ((pba << 10) - igb->max_frame_size));
12635779Sxy150489 
12648571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82575) {
12658571SChenlu.Chen@Sun.COM 		/* 8-byte granularity */
12668571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF8;
12678571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 8;
12688571SChenlu.Chen@Sun.COM 	} else {
12698571SChenlu.Chen@Sun.COM 		/* 16-byte granularity */
12708571SChenlu.Chen@Sun.COM 		hw->fc.high_water = high_water & 0xFFF0;
12718571SChenlu.Chen@Sun.COM 		hw->fc.low_water = hw->fc.high_water - 16;
12728571SChenlu.Chen@Sun.COM 	}
12738571SChenlu.Chen@Sun.COM 
12745779Sxy150489 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
12755779Sxy150489 	hw->fc.send_xon = B_TRUE;
12765779Sxy150489 
127711155SJason.Xu@Sun.COM 	(void) e1000_validate_mdi_setting(hw);
12788955SChenlu.Chen@Sun.COM 
12795779Sxy150489 	/*
12808955SChenlu.Chen@Sun.COM 	 * Reset the chipset hardware the second time to put PBA settings
12818955SChenlu.Chen@Sun.COM 	 * into effect.
12825779Sxy150489 	 */
12836624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
12848955SChenlu.Chen@Sun.COM 		igb_error(igb, "Second reset failed");
12858955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
12866624Sgl147354 	}
12875779Sxy150489 
12885779Sxy150489 	/*
12895779Sxy150489 	 * Don't wait for auto-negotiation to complete
12905779Sxy150489 	 */
12915779Sxy150489 	hw->phy.autoneg_wait_to_complete = B_FALSE;
12925779Sxy150489 
12935779Sxy150489 	/*
12945779Sxy150489 	 * Copper options
12955779Sxy150489 	 */
12965779Sxy150489 	if (hw->phy.media_type == e1000_media_type_copper) {
12975779Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
12985779Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
12995779Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
13005779Sxy150489 	}
13015779Sxy150489 
13025779Sxy150489 	/*
13035779Sxy150489 	 * Initialize link settings
13045779Sxy150489 	 */
13055779Sxy150489 	(void) igb_setup_link(igb, B_FALSE);
13065779Sxy150489 
13075779Sxy150489 	/*
13085779Sxy150489 	 * Configure/Initialize hardware
13095779Sxy150489 	 */
13105779Sxy150489 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
13115779Sxy150489 		igb_error(igb, "Failed to initialize hardware");
13128955SChenlu.Chen@Sun.COM 		goto init_adapter_fail;
13135779Sxy150489 	}
13145779Sxy150489 
13155779Sxy150489 	/*
131611367SJason.Xu@Sun.COM 	 *  Start the link setup timer
131711367SJason.Xu@Sun.COM 	 */
131811367SJason.Xu@Sun.COM 	igb_start_link_timer(igb);
131911367SJason.Xu@Sun.COM 
132011367SJason.Xu@Sun.COM 	/*
13218955SChenlu.Chen@Sun.COM 	 * Disable wakeup control by default
13228955SChenlu.Chen@Sun.COM 	 */
13238955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_WUC, 0);
13248955SChenlu.Chen@Sun.COM 
13258955SChenlu.Chen@Sun.COM 	/*
13268955SChenlu.Chen@Sun.COM 	 * Record phy info in hw struct
13278955SChenlu.Chen@Sun.COM 	 */
13288955SChenlu.Chen@Sun.COM 	(void) e1000_get_phy_info(hw);
13298955SChenlu.Chen@Sun.COM 
13308955SChenlu.Chen@Sun.COM 	/*
13315779Sxy150489 	 * Make sure driver has control
13325779Sxy150489 	 */
13335779Sxy150489 	igb_get_driver_control(hw);
13345779Sxy150489 
13355779Sxy150489 	/*
13368955SChenlu.Chen@Sun.COM 	 * Restore LED settings to the default from EEPROM
13378955SChenlu.Chen@Sun.COM 	 * to meet the standard for Sun platforms.
13388955SChenlu.Chen@Sun.COM 	 */
13398955SChenlu.Chen@Sun.COM 	(void) e1000_cleanup_led(hw);
13408955SChenlu.Chen@Sun.COM 
13418955SChenlu.Chen@Sun.COM 	/*
13425779Sxy150489 	 * Setup MSI-X interrupts
13435779Sxy150489 	 */
13445779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
13458571SChenlu.Chen@Sun.COM 		igb->capab->setup_msix(igb);
13465779Sxy150489 
13475779Sxy150489 	/*
13485779Sxy150489 	 * Initialize unicast addresses.
13495779Sxy150489 	 */
13505779Sxy150489 	igb_init_unicst(igb);
13515779Sxy150489 
13525779Sxy150489 	/*
13535779Sxy150489 	 * Setup and initialize the mctable structures.
13545779Sxy150489 	 */
13555779Sxy150489 	igb_setup_multicst(igb);
13565779Sxy150489 
13575779Sxy150489 	/*
13585779Sxy150489 	 * Set interrupt throttling rate
13595779Sxy150489 	 */
13605779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++)
13615779Sxy150489 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
13625779Sxy150489 
13635779Sxy150489 	/*
13645779Sxy150489 	 * Save the state of the phy
13655779Sxy150489 	 */
13665779Sxy150489 	igb_get_phy_state(igb);
13675779Sxy150489 
136811502SChenlu.Chen@Sun.COM 	igb_param_sync(igb);
136911502SChenlu.Chen@Sun.COM 
13705779Sxy150489 	return (IGB_SUCCESS);
13718955SChenlu.Chen@Sun.COM 
13728955SChenlu.Chen@Sun.COM init_adapter_fail:
13738955SChenlu.Chen@Sun.COM 	/*
13748955SChenlu.Chen@Sun.COM 	 * Reset PHY if possible
13758955SChenlu.Chen@Sun.COM 	 */
13768955SChenlu.Chen@Sun.COM 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
13778955SChenlu.Chen@Sun.COM 		(void) e1000_phy_hw_reset(hw);
13788955SChenlu.Chen@Sun.COM 
13798955SChenlu.Chen@Sun.COM 	return (IGB_FAILURE);
13805779Sxy150489 }
13815779Sxy150489 
13825779Sxy150489 /*
13838955SChenlu.Chen@Sun.COM  * igb_stop_adapter - Stop the adapter
13845779Sxy150489  */
13855779Sxy150489 static void
igb_stop_adapter(igb_t * igb)13868955SChenlu.Chen@Sun.COM igb_stop_adapter(igb_t *igb)
13875779Sxy150489 {
13885779Sxy150489 	struct e1000_hw *hw = &igb->hw;
13895779Sxy150489 
13905779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
13915779Sxy150489 
139211367SJason.Xu@Sun.COM 	/* Stop the link setup timer */
139311367SJason.Xu@Sun.COM 	igb_stop_link_timer(igb);
139411367SJason.Xu@Sun.COM 
13955779Sxy150489 	/* Tell firmware driver is no longer in control */
13965779Sxy150489 	igb_release_driver_control(hw);
13975779Sxy150489 
13985779Sxy150489 	/*
13995779Sxy150489 	 * Reset the chipset
14005779Sxy150489 	 */
14016624Sgl147354 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
14026624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
14036624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
14046624Sgl147354 	}
14055779Sxy150489 
14065779Sxy150489 	/*
14078955SChenlu.Chen@Sun.COM 	 * e1000_phy_hw_reset is not needed here, MAC reset above is sufficient
14085779Sxy150489 	 */
14095779Sxy150489 }
14105779Sxy150489 
14115779Sxy150489 /*
14125779Sxy150489  * igb_reset - Reset the chipset and restart the driver.
14135779Sxy150489  *
14145779Sxy150489  * It involves stopping and re-starting the chipset,
14155779Sxy150489  * and re-configuring the rx/tx rings.
14165779Sxy150489  */
14175779Sxy150489 static int
igb_reset(igb_t * igb)14185779Sxy150489 igb_reset(igb_t *igb)
14195779Sxy150489 {
14205779Sxy150489 	int i;
14215779Sxy150489 
14225779Sxy150489 	mutex_enter(&igb->gen_lock);
14235779Sxy150489 
14245779Sxy150489 	ASSERT(igb->igb_state & IGB_STARTED);
142511367SJason.Xu@Sun.COM 	atomic_and_32(&igb->igb_state, ~IGB_STARTED);
14265779Sxy150489 
14275779Sxy150489 	/*
14285779Sxy150489 	 * Disable the adapter interrupts to stop any rx/tx activities
14295779Sxy150489 	 * before draining pending data and resetting hardware.
14305779Sxy150489 	 */
14315779Sxy150489 	igb_disable_adapter_interrupts(igb);
14325779Sxy150489 
14335779Sxy150489 	/*
14345779Sxy150489 	 * Drain the pending transmit packets
14355779Sxy150489 	 */
14365779Sxy150489 	(void) igb_tx_drain(igb);
14375779Sxy150489 
14385779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
14395779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
14405779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
14415779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
14425779Sxy150489 
14435779Sxy150489 	/*
14448955SChenlu.Chen@Sun.COM 	 * Stop the adapter
14455779Sxy150489 	 */
14468955SChenlu.Chen@Sun.COM 	igb_stop_adapter(igb);
14475779Sxy150489 
14485779Sxy150489 	/*
14495779Sxy150489 	 * Clean the pending tx data/resources
14505779Sxy150489 	 */
14515779Sxy150489 	igb_tx_clean(igb);
14525779Sxy150489 
14535779Sxy150489 	/*
14548955SChenlu.Chen@Sun.COM 	 * Start the adapter
14555779Sxy150489 	 */
14568955SChenlu.Chen@Sun.COM 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
14576624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
14585779Sxy150489 		goto reset_failure;
14595779Sxy150489 	}
14605779Sxy150489 
14615779Sxy150489 	/*
14625779Sxy150489 	 * Setup the rx/tx rings
14635779Sxy150489 	 */
146411502SChenlu.Chen@Sun.COM 	igb->tx_ring_init = B_FALSE;
14655779Sxy150489 	igb_setup_rings(igb);
14665779Sxy150489 
146711367SJason.Xu@Sun.COM 	atomic_and_32(&igb->igb_state, ~(IGB_ERROR | IGB_STALL));
146811367SJason.Xu@Sun.COM 
14695779Sxy150489 	/*
14705779Sxy150489 	 * Enable adapter interrupts
14715779Sxy150489 	 * The interrupts must be enabled after the driver state is START
14725779Sxy150489 	 */
14738571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
14745779Sxy150489 
14756624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
14766624Sgl147354 		goto reset_failure;
14776624Sgl147354 
14786624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
14796624Sgl147354 		goto reset_failure;
14806624Sgl147354 
14815779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14825779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14835779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14845779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14855779Sxy150489 
148611367SJason.Xu@Sun.COM 	atomic_or_32(&igb->igb_state, IGB_STARTED);
148711367SJason.Xu@Sun.COM 
14885779Sxy150489 	mutex_exit(&igb->gen_lock);
14895779Sxy150489 
14905779Sxy150489 	return (IGB_SUCCESS);
14915779Sxy150489 
14925779Sxy150489 reset_failure:
14935779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
14945779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
14955779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
14965779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
14975779Sxy150489 
14985779Sxy150489 	mutex_exit(&igb->gen_lock);
14995779Sxy150489 
15006624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
15016624Sgl147354 
15025779Sxy150489 	return (IGB_FAILURE);
15035779Sxy150489 }
15045779Sxy150489 
15055779Sxy150489 /*
15065779Sxy150489  * igb_tx_clean - Clean the pending transmit packets and DMA resources
15075779Sxy150489  */
15085779Sxy150489 static void
igb_tx_clean(igb_t * igb)15095779Sxy150489 igb_tx_clean(igb_t *igb)
15105779Sxy150489 {
15115779Sxy150489 	igb_tx_ring_t *tx_ring;
15125779Sxy150489 	tx_control_block_t *tcb;
15135779Sxy150489 	link_list_t pending_list;
15145779Sxy150489 	uint32_t desc_num;
15155779Sxy150489 	int i, j;
15165779Sxy150489 
15175779Sxy150489 	LINK_LIST_INIT(&pending_list);
15185779Sxy150489 
15195779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
15205779Sxy150489 		tx_ring = &igb->tx_rings[i];
15215779Sxy150489 
15225779Sxy150489 		mutex_enter(&tx_ring->recycle_lock);
15235779Sxy150489 
15245779Sxy150489 		/*
15255779Sxy150489 		 * Clean the pending tx data - the pending packets in the
15265779Sxy150489 		 * work_list that have no chances to be transmitted again.
15275779Sxy150489 		 *
15285779Sxy150489 		 * We must ensure the chipset is stopped or the link is down
15295779Sxy150489 		 * before cleaning the transmit packets.
15305779Sxy150489 		 */
15315779Sxy150489 		desc_num = 0;
15325779Sxy150489 		for (j = 0; j < tx_ring->ring_size; j++) {
15335779Sxy150489 			tcb = tx_ring->work_list[j];
15345779Sxy150489 			if (tcb != NULL) {
15355779Sxy150489 				desc_num += tcb->desc_num;
15365779Sxy150489 
15375779Sxy150489 				tx_ring->work_list[j] = NULL;
15385779Sxy150489 
15395779Sxy150489 				igb_free_tcb(tcb);
15405779Sxy150489 
15415779Sxy150489 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
15425779Sxy150489 			}
15435779Sxy150489 		}
15445779Sxy150489 
15455779Sxy150489 		if (desc_num > 0) {
15465779Sxy150489 			atomic_add_32(&tx_ring->tbd_free, desc_num);
15475779Sxy150489 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
15485779Sxy150489 
15495779Sxy150489 			/*
15507072Sxy150489 			 * Reset the head and tail pointers of the tbd ring;
15517072Sxy150489 			 * Reset the head write-back if it is enabled.
15525779Sxy150489 			 */
15535779Sxy150489 			tx_ring->tbd_head = 0;
15545779Sxy150489 			tx_ring->tbd_tail = 0;
15557072Sxy150489 			if (igb->tx_head_wb_enable)
15567072Sxy150489 				*tx_ring->tbd_head_wb = 0;
15575779Sxy150489 
15585779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
15595779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
15605779Sxy150489 		}
15615779Sxy150489 
15625779Sxy150489 		mutex_exit(&tx_ring->recycle_lock);
15635779Sxy150489 
15645779Sxy150489 		/*
15655779Sxy150489 		 * Add the tx control blocks in the pending list to
15665779Sxy150489 		 * the free list.
15675779Sxy150489 		 */
15685779Sxy150489 		igb_put_free_list(tx_ring, &pending_list);
15695779Sxy150489 	}
15705779Sxy150489 }
15715779Sxy150489 
15725779Sxy150489 /*
15735779Sxy150489  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
15745779Sxy150489  */
15755779Sxy150489 static boolean_t
igb_tx_drain(igb_t * igb)15765779Sxy150489 igb_tx_drain(igb_t *igb)
15775779Sxy150489 {
15785779Sxy150489 	igb_tx_ring_t *tx_ring;
15795779Sxy150489 	boolean_t done;
15805779Sxy150489 	int i, j;
15815779Sxy150489 
15825779Sxy150489 	/*
15835779Sxy150489 	 * Wait for a specific time to allow pending tx packets
15845779Sxy150489 	 * to be transmitted.
15855779Sxy150489 	 *
15865779Sxy150489 	 * Check the counter tbd_free to see if transmission is done.
15875779Sxy150489 	 * No lock protection is needed here.
15885779Sxy150489 	 *
15895779Sxy150489 	 * Return B_TRUE if all pending packets have been transmitted;
15905779Sxy150489 	 * Otherwise return B_FALSE;
15915779Sxy150489 	 */
15925779Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
15935779Sxy150489 
15945779Sxy150489 		done = B_TRUE;
15955779Sxy150489 		for (j = 0; j < igb->num_tx_rings; j++) {
15965779Sxy150489 			tx_ring = &igb->tx_rings[j];
15975779Sxy150489 			done = done &&
15985779Sxy150489 			    (tx_ring->tbd_free == tx_ring->ring_size);
15995779Sxy150489 		}
16005779Sxy150489 
16015779Sxy150489 		if (done)
16025779Sxy150489 			break;
16035779Sxy150489 
16045779Sxy150489 		msec_delay(1);
16055779Sxy150489 	}
16065779Sxy150489 
16075779Sxy150489 	return (done);
16085779Sxy150489 }
16095779Sxy150489 
16105779Sxy150489 /*
16115779Sxy150489  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
16125779Sxy150489  */
16135779Sxy150489 static boolean_t
igb_rx_drain(igb_t * igb)16145779Sxy150489 igb_rx_drain(igb_t *igb)
16155779Sxy150489 {
16165779Sxy150489 	boolean_t done;
161711502SChenlu.Chen@Sun.COM 	int i;
16185779Sxy150489 
16195779Sxy150489 	/*
16205779Sxy150489 	 * Polling the rx free list to check if those rx buffers held by
16215779Sxy150489 	 * the upper layer are released.
16225779Sxy150489 	 *
16235779Sxy150489 	 * Check the counter rcb_free to see if all pending buffers are
16245779Sxy150489 	 * released. No lock protection is needed here.
16255779Sxy150489 	 *
16265779Sxy150489 	 * Return B_TRUE if all pending buffers have been released;
16275779Sxy150489 	 * Otherwise return B_FALSE;
16285779Sxy150489 	 */
16295779Sxy150489 	for (i = 0; i < RX_DRAIN_TIME; i++) {
163011502SChenlu.Chen@Sun.COM 		done = (igb->rcb_pending == 0);
16315779Sxy150489 
16325779Sxy150489 		if (done)
16335779Sxy150489 			break;
16345779Sxy150489 
16355779Sxy150489 		msec_delay(1);
16365779Sxy150489 	}
16375779Sxy150489 
16385779Sxy150489 	return (done);
16395779Sxy150489 }
16405779Sxy150489 
16415779Sxy150489 /*
16425779Sxy150489  * igb_start - Start the driver/chipset
16435779Sxy150489  */
16445779Sxy150489 int
igb_start(igb_t * igb,boolean_t alloc_buffer)164511502SChenlu.Chen@Sun.COM igb_start(igb_t *igb, boolean_t alloc_buffer)
16465779Sxy150489 {
16475779Sxy150489 	int i;
16485779Sxy150489 
16495779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
16505779Sxy150489 
165111502SChenlu.Chen@Sun.COM 	if (alloc_buffer) {
165211502SChenlu.Chen@Sun.COM 		if (igb_alloc_rx_data(igb) != IGB_SUCCESS) {
165311502SChenlu.Chen@Sun.COM 			igb_error(igb,
165411502SChenlu.Chen@Sun.COM 			    "Failed to allocate software receive rings");
165511502SChenlu.Chen@Sun.COM 			return (IGB_FAILURE);
165611502SChenlu.Chen@Sun.COM 		}
165711502SChenlu.Chen@Sun.COM 
165811502SChenlu.Chen@Sun.COM 		/* Allocate buffers for all the rx/tx rings */
165911502SChenlu.Chen@Sun.COM 		if (igb_alloc_dma(igb) != IGB_SUCCESS) {
166011502SChenlu.Chen@Sun.COM 			igb_error(igb, "Failed to allocate DMA resource");
166111502SChenlu.Chen@Sun.COM 			return (IGB_FAILURE);
166211502SChenlu.Chen@Sun.COM 		}
166311502SChenlu.Chen@Sun.COM 
166411502SChenlu.Chen@Sun.COM 		igb->tx_ring_init = B_TRUE;
166511502SChenlu.Chen@Sun.COM 	} else {
166611502SChenlu.Chen@Sun.COM 		igb->tx_ring_init = B_FALSE;
166711502SChenlu.Chen@Sun.COM 	}
166811502SChenlu.Chen@Sun.COM 
16695779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
16705779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
16715779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
16725779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
16735779Sxy150489 
16745779Sxy150489 	/*
16758955SChenlu.Chen@Sun.COM 	 * Start the adapter
16765779Sxy150489 	 */
16778955SChenlu.Chen@Sun.COM 	if ((igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) == 0) {
16788955SChenlu.Chen@Sun.COM 		if (igb_init_adapter(igb) != IGB_SUCCESS) {
16798275SEric Cheng 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
16808275SEric Cheng 			goto start_failure;
16818275SEric Cheng 		}
16828955SChenlu.Chen@Sun.COM 		igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
16835779Sxy150489 	}
16845779Sxy150489 
16855779Sxy150489 	/*
168611502SChenlu.Chen@Sun.COM 	 * Setup the rx/tx rings
168711502SChenlu.Chen@Sun.COM 	 */
168811502SChenlu.Chen@Sun.COM 	igb_setup_rings(igb);
168911502SChenlu.Chen@Sun.COM 
169011502SChenlu.Chen@Sun.COM 	/*
16915779Sxy150489 	 * Enable adapter interrupts
16925779Sxy150489 	 * The interrupts must be enabled after the driver state is START
16935779Sxy150489 	 */
16948571SChenlu.Chen@Sun.COM 	igb->capab->enable_intr(igb);
16955779Sxy150489 
16966624Sgl147354 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
16976624Sgl147354 		goto start_failure;
16986624Sgl147354 
16996624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
17006624Sgl147354 		goto start_failure;
17016624Sgl147354 
17025779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
17035779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
17045779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
17055779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
17065779Sxy150489 
17075779Sxy150489 	return (IGB_SUCCESS);
17085779Sxy150489 
17095779Sxy150489 start_failure:
17105779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
17115779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
17125779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
17135779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
17145779Sxy150489 
17156624Sgl147354 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
17166624Sgl147354 
17175779Sxy150489 	return (IGB_FAILURE);
17185779Sxy150489 }
17195779Sxy150489 
17205779Sxy150489 /*
17215779Sxy150489  * igb_stop - Stop the driver/chipset
17225779Sxy150489  */
17235779Sxy150489 void
igb_stop(igb_t * igb,boolean_t free_buffer)172411502SChenlu.Chen@Sun.COM igb_stop(igb_t *igb, boolean_t free_buffer)
17255779Sxy150489 {
17265779Sxy150489 	int i;
17275779Sxy150489 
17285779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
17295779Sxy150489 
17308955SChenlu.Chen@Sun.COM 	igb->attach_progress &= ~ATTACH_PROGRESS_INIT_ADAPTER;
17318275SEric Cheng 
17325779Sxy150489 	/*
17335779Sxy150489 	 * Disable the adapter interrupts
17345779Sxy150489 	 */
17355779Sxy150489 	igb_disable_adapter_interrupts(igb);
17365779Sxy150489 
17375779Sxy150489 	/*
17385779Sxy150489 	 * Drain the pending tx packets
17395779Sxy150489 	 */
17405779Sxy150489 	(void) igb_tx_drain(igb);
17415779Sxy150489 
17425779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
17435779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
17445779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
17455779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
17465779Sxy150489 
17475779Sxy150489 	/*
17488955SChenlu.Chen@Sun.COM 	 * Stop the adapter
17495779Sxy150489 	 */
17508955SChenlu.Chen@Sun.COM 	igb_stop_adapter(igb);
17515779Sxy150489 
17525779Sxy150489 	/*
17535779Sxy150489 	 * Clean the pending tx data/resources
17545779Sxy150489 	 */
17555779Sxy150489 	igb_tx_clean(igb);
17565779Sxy150489 
17575779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
17585779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
17595779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
17605779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
17616624Sgl147354 
17626624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
17636624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
176411502SChenlu.Chen@Sun.COM 
176511502SChenlu.Chen@Sun.COM 	if (igb->link_state == LINK_STATE_UP) {
176611502SChenlu.Chen@Sun.COM 		igb->link_state = LINK_STATE_UNKNOWN;
176711502SChenlu.Chen@Sun.COM 		mac_link_update(igb->mac_hdl, igb->link_state);
176811502SChenlu.Chen@Sun.COM 	}
176911502SChenlu.Chen@Sun.COM 
177011502SChenlu.Chen@Sun.COM 	if (free_buffer) {
177111502SChenlu.Chen@Sun.COM 		/*
177211502SChenlu.Chen@Sun.COM 		 * Release the DMA/memory resources of rx/tx rings
177311502SChenlu.Chen@Sun.COM 		 */
177411502SChenlu.Chen@Sun.COM 		igb_free_dma(igb);
177511502SChenlu.Chen@Sun.COM 		igb_free_rx_data(igb);
177611502SChenlu.Chen@Sun.COM 	}
17775779Sxy150489 }
17785779Sxy150489 
17795779Sxy150489 /*
17805779Sxy150489  * igb_alloc_rings - Allocate memory space for rx/tx rings
17815779Sxy150489  */
17825779Sxy150489 static int
igb_alloc_rings(igb_t * igb)17835779Sxy150489 igb_alloc_rings(igb_t *igb)
17845779Sxy150489 {
17855779Sxy150489 	/*
17865779Sxy150489 	 * Allocate memory space for rx rings
17875779Sxy150489 	 */
17885779Sxy150489 	igb->rx_rings = kmem_zalloc(
17895779Sxy150489 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
17905779Sxy150489 	    KM_NOSLEEP);
17915779Sxy150489 
17925779Sxy150489 	if (igb->rx_rings == NULL) {
17935779Sxy150489 		return (IGB_FAILURE);
17945779Sxy150489 	}
17955779Sxy150489 
17965779Sxy150489 	/*
17975779Sxy150489 	 * Allocate memory space for tx rings
17985779Sxy150489 	 */
17995779Sxy150489 	igb->tx_rings = kmem_zalloc(
18005779Sxy150489 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
18015779Sxy150489 	    KM_NOSLEEP);
18025779Sxy150489 
18035779Sxy150489 	if (igb->tx_rings == NULL) {
18045779Sxy150489 		kmem_free(igb->rx_rings,
18055779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
18065779Sxy150489 		igb->rx_rings = NULL;
18075779Sxy150489 		return (IGB_FAILURE);
18085779Sxy150489 	}
18095779Sxy150489 
18108275SEric Cheng 	/*
18118275SEric Cheng 	 * Allocate memory space for rx ring groups
18128275SEric Cheng 	 */
18138275SEric Cheng 	igb->rx_groups = kmem_zalloc(
18148275SEric Cheng 	    sizeof (igb_rx_group_t) * igb->num_rx_groups,
18158275SEric Cheng 	    KM_NOSLEEP);
18168275SEric Cheng 
18178275SEric Cheng 	if (igb->rx_groups == NULL) {
18188275SEric Cheng 		kmem_free(igb->rx_rings,
18198275SEric Cheng 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
18208275SEric Cheng 		kmem_free(igb->tx_rings,
18218275SEric Cheng 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
18228275SEric Cheng 		igb->rx_rings = NULL;
18238275SEric Cheng 		igb->tx_rings = NULL;
18248275SEric Cheng 		return (IGB_FAILURE);
18258275SEric Cheng 	}
18268275SEric Cheng 
18275779Sxy150489 	return (IGB_SUCCESS);
18285779Sxy150489 }
18295779Sxy150489 
18305779Sxy150489 /*
18315779Sxy150489  * igb_free_rings - Free the memory space of rx/tx rings.
18325779Sxy150489  */
18335779Sxy150489 static void
igb_free_rings(igb_t * igb)18345779Sxy150489 igb_free_rings(igb_t *igb)
18355779Sxy150489 {
18365779Sxy150489 	if (igb->rx_rings != NULL) {
18375779Sxy150489 		kmem_free(igb->rx_rings,
18385779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
18395779Sxy150489 		igb->rx_rings = NULL;
18405779Sxy150489 	}
18415779Sxy150489 
18425779Sxy150489 	if (igb->tx_rings != NULL) {
18435779Sxy150489 		kmem_free(igb->tx_rings,
18445779Sxy150489 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
18455779Sxy150489 		igb->tx_rings = NULL;
18465779Sxy150489 	}
18478275SEric Cheng 
18488275SEric Cheng 	if (igb->rx_groups != NULL) {
18498275SEric Cheng 		kmem_free(igb->rx_groups,
18508275SEric Cheng 		    sizeof (igb_rx_group_t) * igb->num_rx_groups);
18518275SEric Cheng 		igb->rx_groups = NULL;
18528275SEric Cheng 	}
18535779Sxy150489 }
18545779Sxy150489 
185511502SChenlu.Chen@Sun.COM static int
igb_alloc_rx_data(igb_t * igb)185611502SChenlu.Chen@Sun.COM igb_alloc_rx_data(igb_t *igb)
185711502SChenlu.Chen@Sun.COM {
185811502SChenlu.Chen@Sun.COM 	igb_rx_ring_t *rx_ring;
185911502SChenlu.Chen@Sun.COM 	int i;
186011502SChenlu.Chen@Sun.COM 
186111502SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
186211502SChenlu.Chen@Sun.COM 		rx_ring = &igb->rx_rings[i];
186311502SChenlu.Chen@Sun.COM 		if (igb_alloc_rx_ring_data(rx_ring) != IGB_SUCCESS)
186411502SChenlu.Chen@Sun.COM 			goto alloc_rx_rings_failure;
186511502SChenlu.Chen@Sun.COM 	}
186611502SChenlu.Chen@Sun.COM 	return (IGB_SUCCESS);
186711502SChenlu.Chen@Sun.COM 
186811502SChenlu.Chen@Sun.COM alloc_rx_rings_failure:
186911502SChenlu.Chen@Sun.COM 	igb_free_rx_data(igb);
187011502SChenlu.Chen@Sun.COM 	return (IGB_FAILURE);
187111502SChenlu.Chen@Sun.COM }
187211502SChenlu.Chen@Sun.COM 
187311502SChenlu.Chen@Sun.COM static void
igb_free_rx_data(igb_t * igb)187411502SChenlu.Chen@Sun.COM igb_free_rx_data(igb_t *igb)
187511502SChenlu.Chen@Sun.COM {
187611502SChenlu.Chen@Sun.COM 	igb_rx_ring_t *rx_ring;
187711502SChenlu.Chen@Sun.COM 	igb_rx_data_t *rx_data;
187811502SChenlu.Chen@Sun.COM 	int i;
187911502SChenlu.Chen@Sun.COM 
188011502SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
188111502SChenlu.Chen@Sun.COM 		rx_ring = &igb->rx_rings[i];
188211502SChenlu.Chen@Sun.COM 
188311502SChenlu.Chen@Sun.COM 		mutex_enter(&igb->rx_pending_lock);
188411502SChenlu.Chen@Sun.COM 		rx_data = rx_ring->rx_data;
188511502SChenlu.Chen@Sun.COM 
188611502SChenlu.Chen@Sun.COM 		if (rx_data != NULL) {
188711502SChenlu.Chen@Sun.COM 			rx_data->flag |= IGB_RX_STOPPED;
188811502SChenlu.Chen@Sun.COM 
188911502SChenlu.Chen@Sun.COM 			if (rx_data->rcb_pending == 0) {
189011502SChenlu.Chen@Sun.COM 				igb_free_rx_ring_data(rx_data);
189111502SChenlu.Chen@Sun.COM 				rx_ring->rx_data = NULL;
189211502SChenlu.Chen@Sun.COM 			}
189311502SChenlu.Chen@Sun.COM 		}
189411502SChenlu.Chen@Sun.COM 
189511502SChenlu.Chen@Sun.COM 		mutex_exit(&igb->rx_pending_lock);
189611502SChenlu.Chen@Sun.COM 	}
189711502SChenlu.Chen@Sun.COM }
189811502SChenlu.Chen@Sun.COM 
18995779Sxy150489 /*
19005779Sxy150489  * igb_setup_rings - Setup rx/tx rings
19015779Sxy150489  */
19025779Sxy150489 static void
igb_setup_rings(igb_t * igb)19035779Sxy150489 igb_setup_rings(igb_t *igb)
19045779Sxy150489 {
19055779Sxy150489 	/*
19065779Sxy150489 	 * Setup the rx/tx rings, including the following:
19075779Sxy150489 	 *
19085779Sxy150489 	 * 1. Setup the descriptor ring and the control block buffers;
19095779Sxy150489 	 * 2. Initialize necessary registers for receive/transmit;
19105779Sxy150489 	 * 3. Initialize software pointers/parameters for receive/transmit;
19115779Sxy150489 	 */
19125779Sxy150489 	igb_setup_rx(igb);
19135779Sxy150489 
19145779Sxy150489 	igb_setup_tx(igb);
19155779Sxy150489 }
19165779Sxy150489 
19175779Sxy150489 static void
igb_setup_rx_ring(igb_rx_ring_t * rx_ring)19185779Sxy150489 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
19195779Sxy150489 {
19205779Sxy150489 	igb_t *igb = rx_ring->igb;
192111502SChenlu.Chen@Sun.COM 	igb_rx_data_t *rx_data = rx_ring->rx_data;
19225779Sxy150489 	struct e1000_hw *hw = &igb->hw;
19235779Sxy150489 	rx_control_block_t *rcb;
19245779Sxy150489 	union e1000_adv_rx_desc	*rbd;
19255779Sxy150489 	uint32_t size;
19265779Sxy150489 	uint32_t buf_low;
19275779Sxy150489 	uint32_t buf_high;
19288955SChenlu.Chen@Sun.COM 	uint32_t rxdctl;
19295779Sxy150489 	int i;
19305779Sxy150489 
19315779Sxy150489 	ASSERT(mutex_owned(&rx_ring->rx_lock));
19325779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
19335779Sxy150489 
19348955SChenlu.Chen@Sun.COM 	/*
19358955SChenlu.Chen@Sun.COM 	 * Initialize descriptor ring with buffer addresses
19368955SChenlu.Chen@Sun.COM 	 */
19375779Sxy150489 	for (i = 0; i < igb->rx_ring_size; i++) {
193811502SChenlu.Chen@Sun.COM 		rcb = rx_data->work_list[i];
193911502SChenlu.Chen@Sun.COM 		rbd = &rx_data->rbd_ring[i];
19405779Sxy150489 
19415779Sxy150489 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
19425779Sxy150489 		rbd->read.hdr_addr = NULL;
19435779Sxy150489 	}
19445779Sxy150489 
19455779Sxy150489 	/*
19465779Sxy150489 	 * Initialize the base address registers
19475779Sxy150489 	 */
194811502SChenlu.Chen@Sun.COM 	buf_low = (uint32_t)rx_data->rbd_area.dma_address;
194911502SChenlu.Chen@Sun.COM 	buf_high = (uint32_t)(rx_data->rbd_area.dma_address >> 32);
19505779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
19515779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
19525779Sxy150489 
19535779Sxy150489 	/*
19548955SChenlu.Chen@Sun.COM 	 * Initialize the length register
19558955SChenlu.Chen@Sun.COM 	 */
195611502SChenlu.Chen@Sun.COM 	size = rx_data->ring_size * sizeof (union e1000_adv_rx_desc);
19578955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
19588955SChenlu.Chen@Sun.COM 
19598955SChenlu.Chen@Sun.COM 	/*
19608955SChenlu.Chen@Sun.COM 	 * Initialize buffer size & descriptor type
19615779Sxy150489 	 */
19628955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index),
19638955SChenlu.Chen@Sun.COM 	    ((igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
19648955SChenlu.Chen@Sun.COM 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF));
19658955SChenlu.Chen@Sun.COM 
19668955SChenlu.Chen@Sun.COM 	/*
19678955SChenlu.Chen@Sun.COM 	 * Setup the Receive Descriptor Control Register (RXDCTL)
19688955SChenlu.Chen@Sun.COM 	 */
19698955SChenlu.Chen@Sun.COM 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
19708955SChenlu.Chen@Sun.COM 	rxdctl &= igb->capab->rxdctl_mask;
19718955SChenlu.Chen@Sun.COM 	rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
19728955SChenlu.Chen@Sun.COM 	rxdctl |= 16;		/* pthresh */
19738955SChenlu.Chen@Sun.COM 	rxdctl |= 8 << 8;	/* hthresh */
19748955SChenlu.Chen@Sun.COM 	rxdctl |= 1 << 16;	/* wthresh */
19758955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), rxdctl);
19765779Sxy150489 
197711502SChenlu.Chen@Sun.COM 	rx_data->rbd_next = 0;
19785779Sxy150489 }
19795779Sxy150489 
19805779Sxy150489 static void
igb_setup_rx(igb_t * igb)19815779Sxy150489 igb_setup_rx(igb_t *igb)
19825779Sxy150489 {
19835779Sxy150489 	igb_rx_ring_t *rx_ring;
198411502SChenlu.Chen@Sun.COM 	igb_rx_data_t *rx_data;
19858275SEric Cheng 	igb_rx_group_t *rx_group;
19865779Sxy150489 	struct e1000_hw *hw = &igb->hw;
19878955SChenlu.Chen@Sun.COM 	uint32_t rctl, rxcsum;
19888275SEric Cheng 	uint32_t ring_per_group;
19895779Sxy150489 	int i;
19905779Sxy150489 
19915779Sxy150489 	/*
19928955SChenlu.Chen@Sun.COM 	 * Setup the Receive Control Register (RCTL), and enable the
19938955SChenlu.Chen@Sun.COM 	 * receiver. The initial configuration is to: enable the receiver,
19948955SChenlu.Chen@Sun.COM 	 * accept broadcasts, discard bad packets, accept long packets,
19958955SChenlu.Chen@Sun.COM 	 * disable VLAN filter checking, and set receive buffer size to
19968955SChenlu.Chen@Sun.COM 	 * 2k.  For 82575, also set the receive descriptor minimum
19978955SChenlu.Chen@Sun.COM 	 * threshold size to 1/2 the ring.
19985779Sxy150489 	 */
19998571SChenlu.Chen@Sun.COM 	rctl = E1000_READ_REG(hw, E1000_RCTL);
20008571SChenlu.Chen@Sun.COM 
20018571SChenlu.Chen@Sun.COM 	/*
20028955SChenlu.Chen@Sun.COM 	 * Clear the field used for wakeup control.  This driver doesn't do
20038955SChenlu.Chen@Sun.COM 	 * wakeup but leave this here for completeness.
20048571SChenlu.Chen@Sun.COM 	 */
20058571SChenlu.Chen@Sun.COM 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
200611155SJason.Xu@Sun.COM 	rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
200711155SJason.Xu@Sun.COM 
200811155SJason.Xu@Sun.COM 	rctl |= (E1000_RCTL_EN |	/* Enable Receive Unit */
200911155SJason.Xu@Sun.COM 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
201011155SJason.Xu@Sun.COM 	    E1000_RCTL_LPE |		/* Large Packet Enable */
201111155SJason.Xu@Sun.COM 					/* Multicast filter offset */
201211155SJason.Xu@Sun.COM 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
201311155SJason.Xu@Sun.COM 	    E1000_RCTL_RDMTS_HALF |	/* rx descriptor threshold */
201411155SJason.Xu@Sun.COM 	    E1000_RCTL_SECRC);		/* Strip Ethernet CRC */
20155779Sxy150489 
20168275SEric Cheng 	for (i = 0; i < igb->num_rx_groups; i++) {
20178275SEric Cheng 		rx_group = &igb->rx_groups[i];
20188275SEric Cheng 		rx_group->index = i;
20198275SEric Cheng 		rx_group->igb = igb;
20208275SEric Cheng 	}
20218275SEric Cheng 
20225779Sxy150489 	/*
20238955SChenlu.Chen@Sun.COM 	 * Set up all rx descriptor rings - must be called before receive unit
20248955SChenlu.Chen@Sun.COM 	 * enabled.
20255812Sxy150489 	 */
20268275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
20275812Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
20285812Sxy150489 		rx_ring = &igb->rx_rings[i];
20295812Sxy150489 		igb_setup_rx_ring(rx_ring);
20308275SEric Cheng 
20318275SEric Cheng 		/*
20328275SEric Cheng 		 * Map a ring to a group by assigning a group index
20338275SEric Cheng 		 */
20348275SEric Cheng 		rx_ring->group_index = i / ring_per_group;
20355812Sxy150489 	}
20365812Sxy150489 
20375812Sxy150489 	/*
20385779Sxy150489 	 * Setup the Rx Long Packet Max Length register
20395779Sxy150489 	 */
20405779Sxy150489 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
20415779Sxy150489 
20425779Sxy150489 	/*
20435779Sxy150489 	 * Hardware checksum settings
20445779Sxy150489 	 */
20455779Sxy150489 	if (igb->rx_hcksum_enable) {
20468955SChenlu.Chen@Sun.COM 		rxcsum =
20475779Sxy150489 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
20485779Sxy150489 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
20495779Sxy150489 
20508955SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
20515779Sxy150489 	}
20525779Sxy150489 
20535779Sxy150489 	/*
20548275SEric Cheng 	 * Setup classify and RSS for multiple receive queues
20555779Sxy150489 	 */
20568275SEric Cheng 	switch (igb->vmdq_mode) {
20578275SEric Cheng 	case E1000_VMDQ_OFF:
20588275SEric Cheng 		/*
20598275SEric Cheng 		 * One ring group, only RSS is needed when more than
20608275SEric Cheng 		 * one ring enabled.
20618275SEric Cheng 		 */
20628275SEric Cheng 		if (igb->num_rx_rings > 1)
20638275SEric Cheng 			igb_setup_rss(igb);
20648275SEric Cheng 		break;
20658275SEric Cheng 	case E1000_VMDQ_MAC:
20668275SEric Cheng 		/*
20678275SEric Cheng 		 * Multiple groups, each group has one ring,
20688275SEric Cheng 		 * only the MAC classification is needed.
20698275SEric Cheng 		 */
20708275SEric Cheng 		igb_setup_mac_classify(igb);
20718275SEric Cheng 		break;
20728275SEric Cheng 	case E1000_VMDQ_MAC_RSS:
20738275SEric Cheng 		/*
20748275SEric Cheng 		 * Multiple groups and multiple rings, both
20758275SEric Cheng 		 * MAC classification and RSS are needed.
20768275SEric Cheng 		 */
20778275SEric Cheng 		igb_setup_mac_rss_classify(igb);
20788275SEric Cheng 		break;
20798275SEric Cheng 	}
20808955SChenlu.Chen@Sun.COM 
20818955SChenlu.Chen@Sun.COM 	/*
20828955SChenlu.Chen@Sun.COM 	 * Enable the receive unit - must be done after all
20838955SChenlu.Chen@Sun.COM 	 * the rx setup above.
20848955SChenlu.Chen@Sun.COM 	 */
20858955SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
20868955SChenlu.Chen@Sun.COM 
20878955SChenlu.Chen@Sun.COM 	/*
20888955SChenlu.Chen@Sun.COM 	 * Initialize all adapter ring head & tail pointers - must
20898955SChenlu.Chen@Sun.COM 	 * be done after receive unit is enabled
20908955SChenlu.Chen@Sun.COM 	 */
20918955SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
20928955SChenlu.Chen@Sun.COM 		rx_ring = &igb->rx_rings[i];
209311502SChenlu.Chen@Sun.COM 		rx_data = rx_ring->rx_data;
20948955SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RDH(i), 0);
209511502SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_RDT(i), rx_data->ring_size - 1);
20968955SChenlu.Chen@Sun.COM 	}
20978955SChenlu.Chen@Sun.COM 
20988955SChenlu.Chen@Sun.COM 	/*
20998955SChenlu.Chen@Sun.COM 	 * 82575 with manageability enabled needs a special flush to make
21008955SChenlu.Chen@Sun.COM 	 * sure the fifos start clean.
21018955SChenlu.Chen@Sun.COM 	 */
21028955SChenlu.Chen@Sun.COM 	if ((hw->mac.type == e1000_82575) &&
21038955SChenlu.Chen@Sun.COM 	    (E1000_READ_REG(hw, E1000_MANC) & E1000_MANC_RCV_TCO_EN)) {
21048955SChenlu.Chen@Sun.COM 		e1000_rx_fifo_flush_82575(hw);
21058955SChenlu.Chen@Sun.COM 	}
21065779Sxy150489 }
21075779Sxy150489 
21085779Sxy150489 static void
igb_setup_tx_ring(igb_tx_ring_t * tx_ring)21095779Sxy150489 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
21105779Sxy150489 {
21115779Sxy150489 	igb_t *igb = tx_ring->igb;
21125779Sxy150489 	struct e1000_hw *hw = &igb->hw;
21135779Sxy150489 	uint32_t size;
21145779Sxy150489 	uint32_t buf_low;
21155779Sxy150489 	uint32_t buf_high;
21165779Sxy150489 	uint32_t reg_val;
21175779Sxy150489 
21185779Sxy150489 	ASSERT(mutex_owned(&tx_ring->tx_lock));
21195779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
21205779Sxy150489 
21218275SEric Cheng 
21225779Sxy150489 	/*
21235779Sxy150489 	 * Initialize the length register
21245779Sxy150489 	 */
21255779Sxy150489 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
21265779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
21275779Sxy150489 
21285779Sxy150489 	/*
21295779Sxy150489 	 * Initialize the base address registers
21305779Sxy150489 	 */
21315779Sxy150489 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
21325779Sxy150489 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
21335779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
21345779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
21355779Sxy150489 
21365779Sxy150489 	/*
21375779Sxy150489 	 * Setup head & tail pointers
21385779Sxy150489 	 */
21395779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
21405779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
21415779Sxy150489 
21425779Sxy150489 	/*
21435779Sxy150489 	 * Setup head write-back
21445779Sxy150489 	 */
21455779Sxy150489 	if (igb->tx_head_wb_enable) {
21465779Sxy150489 		/*
21475779Sxy150489 		 * The memory of the head write-back is allocated using
21485779Sxy150489 		 * the extra tbd beyond the tail of the tbd ring.
21495779Sxy150489 		 */
21505779Sxy150489 		tx_ring->tbd_head_wb = (uint32_t *)
21515779Sxy150489 		    ((uintptr_t)tx_ring->tbd_area.address + size);
21527072Sxy150489 		*tx_ring->tbd_head_wb = 0;
21535779Sxy150489 
21545779Sxy150489 		buf_low = (uint32_t)
21555779Sxy150489 		    (tx_ring->tbd_area.dma_address + size);
21565779Sxy150489 		buf_high = (uint32_t)
21575779Sxy150489 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
21585779Sxy150489 
21595779Sxy150489 		/* Set the head write-back enable bit */
21605779Sxy150489 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
21615779Sxy150489 
21625779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
21635779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
21645779Sxy150489 
21655779Sxy150489 		/*
21665779Sxy150489 		 * Turn off relaxed ordering for head write back or it will
21675779Sxy150489 		 * cause problems with the tx recycling
21685779Sxy150489 		 */
21695779Sxy150489 		reg_val = E1000_READ_REG(hw,
21705779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index));
21715779Sxy150489 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
21725779Sxy150489 		E1000_WRITE_REG(hw,
21735779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
21745779Sxy150489 	} else {
21755779Sxy150489 		tx_ring->tbd_head_wb = NULL;
21765779Sxy150489 	}
21775779Sxy150489 
21785779Sxy150489 	tx_ring->tbd_head = 0;
21795779Sxy150489 	tx_ring->tbd_tail = 0;
21805779Sxy150489 	tx_ring->tbd_free = tx_ring->ring_size;
21815779Sxy150489 
218211502SChenlu.Chen@Sun.COM 	if (igb->tx_ring_init == B_TRUE) {
21835779Sxy150489 		tx_ring->tcb_head = 0;
21845779Sxy150489 		tx_ring->tcb_tail = 0;
21855779Sxy150489 		tx_ring->tcb_free = tx_ring->free_list_size;
21865779Sxy150489 	}
21875779Sxy150489 
21885779Sxy150489 	/*
21898571SChenlu.Chen@Sun.COM 	 * Enable TXDCTL per queue
21908571SChenlu.Chen@Sun.COM 	 */
21918571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TXDCTL(tx_ring->index));
21928571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TXDCTL_QUEUE_ENABLE;
21938571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_TXDCTL(tx_ring->index), reg_val);
21949188SPaul.Guo@Sun.COM 
21959188SPaul.Guo@Sun.COM 	/*
21969188SPaul.Guo@Sun.COM 	 * Initialize hardware checksum offload settings
21979188SPaul.Guo@Sun.COM 	 */
21989188SPaul.Guo@Sun.COM 	bzero(&tx_ring->tx_context, sizeof (tx_context_t));
21995779Sxy150489 }
22005779Sxy150489 
22015779Sxy150489 static void
igb_setup_tx(igb_t * igb)22025779Sxy150489 igb_setup_tx(igb_t *igb)
22035779Sxy150489 {
22045779Sxy150489 	igb_tx_ring_t *tx_ring;
22055779Sxy150489 	struct e1000_hw *hw = &igb->hw;
22065779Sxy150489 	uint32_t reg_val;
22075779Sxy150489 	int i;
22085779Sxy150489 
22095779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
22105779Sxy150489 		tx_ring = &igb->tx_rings[i];
22115779Sxy150489 		igb_setup_tx_ring(tx_ring);
22125779Sxy150489 	}
22135779Sxy150489 
22145779Sxy150489 	/*
22155779Sxy150489 	 * Setup the Transmit Control Register (TCTL)
22165779Sxy150489 	 */
22178571SChenlu.Chen@Sun.COM 	reg_val = E1000_READ_REG(hw, E1000_TCTL);
22188571SChenlu.Chen@Sun.COM 	reg_val &= ~E1000_TCTL_CT;
22198571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
22208571SChenlu.Chen@Sun.COM 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
22218571SChenlu.Chen@Sun.COM 
22228571SChenlu.Chen@Sun.COM 	/* Enable transmits */
22238571SChenlu.Chen@Sun.COM 	reg_val |= E1000_TCTL_EN;
22245779Sxy150489 
22255779Sxy150489 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
22265779Sxy150489 }
22275779Sxy150489 
22285779Sxy150489 /*
22295779Sxy150489  * igb_setup_rss - Setup receive-side scaling feature
22305779Sxy150489  */
22315779Sxy150489 static void
igb_setup_rss(igb_t * igb)22325779Sxy150489 igb_setup_rss(igb_t *igb)
22335779Sxy150489 {
22345779Sxy150489 	struct e1000_hw *hw = &igb->hw;
22355779Sxy150489 	uint32_t i, mrqc, rxcsum;
22368571SChenlu.Chen@Sun.COM 	int shift = 0;
22375779Sxy150489 	uint32_t random;
22385779Sxy150489 	union e1000_reta {
22395779Sxy150489 		uint32_t	dword;
22405779Sxy150489 		uint8_t		bytes[4];
22415779Sxy150489 	} reta;
22425779Sxy150489 
22435779Sxy150489 	/* Setup the Redirection Table */
22448571SChenlu.Chen@Sun.COM 	if (hw->mac.type == e1000_82576) {
224511155SJason.Xu@Sun.COM 		shift = 3;
22468571SChenlu.Chen@Sun.COM 	} else if (hw->mac.type == e1000_82575) {
22478571SChenlu.Chen@Sun.COM 		shift = 6;
22488571SChenlu.Chen@Sun.COM 	}
22495779Sxy150489 	for (i = 0; i < (32 * 4); i++) {
22505779Sxy150489 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
22515779Sxy150489 		if ((i & 3) == 3) {
22525779Sxy150489 			E1000_WRITE_REG(hw,
22535779Sxy150489 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
22545779Sxy150489 		}
22555779Sxy150489 	}
22565779Sxy150489 
22575779Sxy150489 	/* Fill out hash function seeds */
22585779Sxy150489 	for (i = 0; i < 10; i++) {
22595779Sxy150489 		(void) random_get_pseudo_bytes((uint8_t *)&random,
22605779Sxy150489 		    sizeof (uint32_t));
22615779Sxy150489 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
22625779Sxy150489 	}
22635779Sxy150489 
22645779Sxy150489 	/* Setup the Multiple Receive Queue Control register */
22655779Sxy150489 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
22665779Sxy150489 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
22675779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
22685779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6 |
22695779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
22705779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
22715779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
22725779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
22735779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
22745779Sxy150489 
22755779Sxy150489 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
22765779Sxy150489 
22775779Sxy150489 	/*
22785779Sxy150489 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
22795779Sxy150489 	 *
22805779Sxy150489 	 * The Packet Checksum is not ethernet CRC. It is another kind of
22815779Sxy150489 	 * checksum offloading provided by the 82575 chipset besides the IP
22825779Sxy150489 	 * header checksum offloading and the TCP/UDP checksum offloading.
22835779Sxy150489 	 * The Packet Checksum is by default computed over the entire packet
22845779Sxy150489 	 * from the first byte of the DA through the last byte of the CRC,
22855779Sxy150489 	 * including the Ethernet and IP headers.
22865779Sxy150489 	 *
22875779Sxy150489 	 * It is a hardware limitation that Packet Checksum is mutually
22885779Sxy150489 	 * exclusive with RSS.
22895779Sxy150489 	 */
22905779Sxy150489 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
22915779Sxy150489 	rxcsum |= E1000_RXCSUM_PCSD;
22925779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
22935779Sxy150489 }
22945779Sxy150489 
22955779Sxy150489 /*
22968275SEric Cheng  * igb_setup_mac_rss_classify - Setup MAC classification and rss
22978275SEric Cheng  */
22988275SEric Cheng static void
igb_setup_mac_rss_classify(igb_t * igb)22998275SEric Cheng igb_setup_mac_rss_classify(igb_t *igb)
23008275SEric Cheng {
23018275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
23028275SEric Cheng 	uint32_t i, mrqc, vmdctl, rxcsum;
23038275SEric Cheng 	uint32_t ring_per_group;
23048275SEric Cheng 	int shift_group0, shift_group1;
23058275SEric Cheng 	uint32_t random;
23068275SEric Cheng 	union e1000_reta {
23078275SEric Cheng 		uint32_t	dword;
23088275SEric Cheng 		uint8_t		bytes[4];
23098275SEric Cheng 	} reta;
23108275SEric Cheng 
23118275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
23128275SEric Cheng 
23138275SEric Cheng 	/* Setup the Redirection Table, it is shared between two groups */
23148275SEric Cheng 	shift_group0 = 2;
23158275SEric Cheng 	shift_group1 = 6;
23168275SEric Cheng 	for (i = 0; i < (32 * 4); i++) {
23178275SEric Cheng 		reta.bytes[i & 3] = ((i % ring_per_group) << shift_group0) |
23188275SEric Cheng 		    ((ring_per_group + (i % ring_per_group)) << shift_group1);
23198275SEric Cheng 		if ((i & 3) == 3) {
23208275SEric Cheng 			E1000_WRITE_REG(hw,
23218275SEric Cheng 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
23228275SEric Cheng 		}
23238275SEric Cheng 	}
23248275SEric Cheng 
23258275SEric Cheng 	/* Fill out hash function seeds */
23268275SEric Cheng 	for (i = 0; i < 10; i++) {
23278275SEric Cheng 		(void) random_get_pseudo_bytes((uint8_t *)&random,
23288275SEric Cheng 		    sizeof (uint32_t));
23298275SEric Cheng 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
23308275SEric Cheng 	}
23318275SEric Cheng 
23328275SEric Cheng 	/*
23338275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
23348275SEric Cheng 	 * enable VMDq based on packet destination MAC address and RSS.
23358275SEric Cheng 	 */
23368275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_RSS_GROUP;
23378275SEric Cheng 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
23388275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
23398275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6 |
23408275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
23418275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
23428275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
23438275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
23448275SEric Cheng 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
23458275SEric Cheng 
23468275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
23478275SEric Cheng 
23488275SEric Cheng 
23498275SEric Cheng 	/* Define the default group and default queues */
23508275SEric Cheng 	vmdctl = E1000_VMDQ_MAC_GROUP_DEFAULT_QUEUE;
23518571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_VT_CTL, vmdctl);
23528275SEric Cheng 
23538275SEric Cheng 	/*
23548275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
23558275SEric Cheng 	 *
23568275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
23578275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
23588275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
23598275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
23608275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
23618275SEric Cheng 	 * including the Ethernet and IP headers.
23628275SEric Cheng 	 *
23638275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
23648275SEric Cheng 	 * exclusive with RSS.
23658275SEric Cheng 	 */
23668275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
23678275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
23688275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
23698275SEric Cheng }
23708275SEric Cheng 
23718275SEric Cheng /*
23728275SEric Cheng  * igb_setup_mac_classify - Setup MAC classification feature
23738275SEric Cheng  */
23748275SEric Cheng static void
igb_setup_mac_classify(igb_t * igb)23758275SEric Cheng igb_setup_mac_classify(igb_t *igb)
23768275SEric Cheng {
23778275SEric Cheng 	struct e1000_hw *hw = &igb->hw;
23788275SEric Cheng 	uint32_t mrqc, rxcsum;
23798275SEric Cheng 
23808275SEric Cheng 	/*
23818275SEric Cheng 	 * Setup the Multiple Receive Queue Control register,
23828275SEric Cheng 	 * enable VMDq based on packet destination MAC address.
23838275SEric Cheng 	 */
23848275SEric Cheng 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_GROUP;
23858275SEric Cheng 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
23868275SEric Cheng 
23878275SEric Cheng 	/*
23888275SEric Cheng 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
23898275SEric Cheng 	 *
23908275SEric Cheng 	 * The Packet Checksum is not ethernet CRC. It is another kind of
23918275SEric Cheng 	 * checksum offloading provided by the 82575 chipset besides the IP
23928275SEric Cheng 	 * header checksum offloading and the TCP/UDP checksum offloading.
23938275SEric Cheng 	 * The Packet Checksum is by default computed over the entire packet
23948275SEric Cheng 	 * from the first byte of the DA through the last byte of the CRC,
23958275SEric Cheng 	 * including the Ethernet and IP headers.
23968275SEric Cheng 	 *
23978275SEric Cheng 	 * It is a hardware limitation that Packet Checksum is mutually
23988275SEric Cheng 	 * exclusive with RSS.
23998275SEric Cheng 	 */
24008275SEric Cheng 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
24018275SEric Cheng 	rxcsum |= E1000_RXCSUM_PCSD;
24028275SEric Cheng 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
24038275SEric Cheng 
24048275SEric Cheng }
24058275SEric Cheng 
24068275SEric Cheng /*
24075779Sxy150489  * igb_init_unicst - Initialize the unicast addresses
24085779Sxy150489  */
24095779Sxy150489 static void
igb_init_unicst(igb_t * igb)24105779Sxy150489 igb_init_unicst(igb_t *igb)
24115779Sxy150489 {
24125779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24135779Sxy150489 	int slot;
24145779Sxy150489 
24155779Sxy150489 	/*
24165779Sxy150489 	 * Here we should consider two situations:
24175779Sxy150489 	 *
24185779Sxy150489 	 * 1. Chipset is initialized the first time
24195779Sxy150489 	 *    Initialize the multiple unicast addresses, and
24208275SEric Cheng 	 *    save the default MAC address.
24215779Sxy150489 	 *
24225779Sxy150489 	 * 2. Chipset is reset
24235779Sxy150489 	 *    Recover the multiple unicast addresses from the
24245779Sxy150489 	 *    software data structure to the RAR registers.
24255779Sxy150489 	 */
24268275SEric Cheng 
24278275SEric Cheng 	/*
24288275SEric Cheng 	 * Clear the default MAC address in the RAR0 rgister,
24298275SEric Cheng 	 * which is loaded from EEPROM when system boot or chipreset,
24308275SEric Cheng 	 * this will cause the conficts with add_mac/rem_mac entry
24318275SEric Cheng 	 * points when VMDq is enabled. For this reason, the RAR0
24328275SEric Cheng 	 * must be cleared for both cases mentioned above.
24338275SEric Cheng 	 */
24348275SEric Cheng 	e1000_rar_clear(hw, 0);
24358275SEric Cheng 
24365779Sxy150489 	if (!igb->unicst_init) {
24378275SEric Cheng 
24385779Sxy150489 		/* Initialize the multiple unicast addresses */
24395779Sxy150489 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
24408275SEric Cheng 		igb->unicst_avail = igb->unicst_total;
24418275SEric Cheng 
24428275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++)
24435779Sxy150489 			igb->unicst_addr[slot].mac.set = 0;
24445779Sxy150489 
24455779Sxy150489 		igb->unicst_init = B_TRUE;
24465779Sxy150489 	} else {
24475779Sxy150489 		/* Re-configure the RAR registers */
24488275SEric Cheng 		for (slot = 0; slot < igb->unicst_total; slot++) {
24498275SEric Cheng 			e1000_rar_set_vmdq(hw, igb->unicst_addr[slot].mac.addr,
24508275SEric Cheng 			    slot, igb->vmdq_mode,
24518275SEric Cheng 			    igb->unicst_addr[slot].mac.group_index);
24528275SEric Cheng 		}
24535779Sxy150489 	}
24545779Sxy150489 }
24555779Sxy150489 
24565779Sxy150489 /*
24578275SEric Cheng  * igb_unicst_find - Find the slot for the specified unicast address
24588275SEric Cheng  */
24598275SEric Cheng int
igb_unicst_find(igb_t * igb,const uint8_t * mac_addr)24608275SEric Cheng igb_unicst_find(igb_t *igb, const uint8_t *mac_addr)
24618275SEric Cheng {
24628275SEric Cheng 	int slot;
24638275SEric Cheng 
24648275SEric Cheng 	ASSERT(mutex_owned(&igb->gen_lock));
24658275SEric Cheng 
24668275SEric Cheng 	for (slot = 0; slot < igb->unicst_total; slot++) {
24678275SEric Cheng 		if (bcmp(igb->unicst_addr[slot].mac.addr,
24688275SEric Cheng 		    mac_addr, ETHERADDRL) == 0)
24698275SEric Cheng 			return (slot);
24708275SEric Cheng 	}
24718275SEric Cheng 
24728275SEric Cheng 	return (-1);
24738275SEric Cheng }
24748275SEric Cheng 
24758275SEric Cheng /*
24765779Sxy150489  * igb_unicst_set - Set the unicast address to the specified slot
24775779Sxy150489  */
24785779Sxy150489 int
igb_unicst_set(igb_t * igb,const uint8_t * mac_addr,int slot)24795779Sxy150489 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
24808275SEric Cheng     int slot)
24815779Sxy150489 {
24825779Sxy150489 	struct e1000_hw *hw = &igb->hw;
24835779Sxy150489 
24845779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
24855779Sxy150489 
24865779Sxy150489 	/*
24875779Sxy150489 	 * Save the unicast address in the software data structure
24885779Sxy150489 	 */
24895779Sxy150489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
24905779Sxy150489 
24915779Sxy150489 	/*
24925779Sxy150489 	 * Set the unicast address to the RAR register
24935779Sxy150489 	 */
24945779Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
24955779Sxy150489 
24966624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
24976624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
24986624Sgl147354 		return (EIO);
24996624Sgl147354 	}
25006624Sgl147354 
25015779Sxy150489 	return (0);
25025779Sxy150489 }
25035779Sxy150489 
25045779Sxy150489 /*
25055779Sxy150489  * igb_multicst_add - Add a multicst address
25065779Sxy150489  */
25075779Sxy150489 int
igb_multicst_add(igb_t * igb,const uint8_t * multiaddr)25085779Sxy150489 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
25095779Sxy150489 {
25109775SVitezslav.Batrla@Sun.COM 	struct ether_addr *new_table;
25119775SVitezslav.Batrla@Sun.COM 	size_t new_len;
25129775SVitezslav.Batrla@Sun.COM 	size_t old_len;
25139775SVitezslav.Batrla@Sun.COM 
25145779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
25155779Sxy150489 
25165779Sxy150489 	if ((multiaddr[0] & 01) == 0) {
25179775SVitezslav.Batrla@Sun.COM 		igb_error(igb, "Illegal multicast address");
25185779Sxy150489 		return (EINVAL);
25195779Sxy150489 	}
25205779Sxy150489 
25219775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_count >= igb->mcast_max_num) {
25229775SVitezslav.Batrla@Sun.COM 		igb_error(igb, "Adapter requested more than %d mcast addresses",
25239775SVitezslav.Batrla@Sun.COM 		    igb->mcast_max_num);
25245779Sxy150489 		return (ENOENT);
25255779Sxy150489 	}
25265779Sxy150489 
25279775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_count == igb->mcast_alloc_count) {
25289775SVitezslav.Batrla@Sun.COM 		old_len = igb->mcast_alloc_count *
25299775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25309775SVitezslav.Batrla@Sun.COM 		new_len = (igb->mcast_alloc_count + MCAST_ALLOC_COUNT) *
25319775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25329775SVitezslav.Batrla@Sun.COM 
25339775SVitezslav.Batrla@Sun.COM 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
25349775SVitezslav.Batrla@Sun.COM 		if (new_table == NULL) {
25359775SVitezslav.Batrla@Sun.COM 			igb_error(igb,
25369775SVitezslav.Batrla@Sun.COM 			    "Not enough memory to alloc mcast table");
25379775SVitezslav.Batrla@Sun.COM 			return (ENOMEM);
25389775SVitezslav.Batrla@Sun.COM 		}
25399775SVitezslav.Batrla@Sun.COM 
25409775SVitezslav.Batrla@Sun.COM 		if (igb->mcast_table != NULL) {
25419775SVitezslav.Batrla@Sun.COM 			bcopy(igb->mcast_table, new_table, old_len);
25429775SVitezslav.Batrla@Sun.COM 			kmem_free(igb->mcast_table, old_len);
25439775SVitezslav.Batrla@Sun.COM 		}
25449775SVitezslav.Batrla@Sun.COM 		igb->mcast_alloc_count += MCAST_ALLOC_COUNT;
25459775SVitezslav.Batrla@Sun.COM 		igb->mcast_table = new_table;
25469775SVitezslav.Batrla@Sun.COM 	}
25479775SVitezslav.Batrla@Sun.COM 
25485779Sxy150489 	bcopy(multiaddr,
25495779Sxy150489 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
25505779Sxy150489 	igb->mcast_count++;
25515779Sxy150489 
25525779Sxy150489 	/*
25535779Sxy150489 	 * Update the multicast table in the hardware
25545779Sxy150489 	 */
25555779Sxy150489 	igb_setup_multicst(igb);
25565779Sxy150489 
25576624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
25586624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
25596624Sgl147354 		return (EIO);
25606624Sgl147354 	}
25616624Sgl147354 
25625779Sxy150489 	return (0);
25635779Sxy150489 }
25645779Sxy150489 
25655779Sxy150489 /*
25665779Sxy150489  * igb_multicst_remove - Remove a multicst address
25675779Sxy150489  */
25685779Sxy150489 int
igb_multicst_remove(igb_t * igb,const uint8_t * multiaddr)25695779Sxy150489 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
25705779Sxy150489 {
25719775SVitezslav.Batrla@Sun.COM 	struct ether_addr *new_table;
25729775SVitezslav.Batrla@Sun.COM 	size_t new_len;
25739775SVitezslav.Batrla@Sun.COM 	size_t old_len;
25745779Sxy150489 	int i;
25755779Sxy150489 
25765779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
25775779Sxy150489 
25785779Sxy150489 	for (i = 0; i < igb->mcast_count; i++) {
25795779Sxy150489 		if (bcmp(multiaddr, &igb->mcast_table[i],
25805779Sxy150489 		    ETHERADDRL) == 0) {
25815779Sxy150489 			for (i++; i < igb->mcast_count; i++) {
25825779Sxy150489 				igb->mcast_table[i - 1] =
25835779Sxy150489 				    igb->mcast_table[i];
25845779Sxy150489 			}
25855779Sxy150489 			igb->mcast_count--;
25865779Sxy150489 			break;
25875779Sxy150489 		}
25885779Sxy150489 	}
25895779Sxy150489 
25909775SVitezslav.Batrla@Sun.COM 	if ((igb->mcast_alloc_count - igb->mcast_count) >
25919775SVitezslav.Batrla@Sun.COM 	    MCAST_ALLOC_COUNT) {
25929775SVitezslav.Batrla@Sun.COM 		old_len = igb->mcast_alloc_count *
25939775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25949775SVitezslav.Batrla@Sun.COM 		new_len = (igb->mcast_alloc_count - MCAST_ALLOC_COUNT) *
25959775SVitezslav.Batrla@Sun.COM 		    sizeof (struct ether_addr);
25969775SVitezslav.Batrla@Sun.COM 
25979775SVitezslav.Batrla@Sun.COM 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
25989775SVitezslav.Batrla@Sun.COM 		if (new_table != NULL) {
25999775SVitezslav.Batrla@Sun.COM 			bcopy(igb->mcast_table, new_table, new_len);
26009775SVitezslav.Batrla@Sun.COM 			kmem_free(igb->mcast_table, old_len);
26019775SVitezslav.Batrla@Sun.COM 			igb->mcast_alloc_count -= MCAST_ALLOC_COUNT;
26029775SVitezslav.Batrla@Sun.COM 			igb->mcast_table = new_table;
26039775SVitezslav.Batrla@Sun.COM 		}
26049775SVitezslav.Batrla@Sun.COM 	}
26059775SVitezslav.Batrla@Sun.COM 
26065779Sxy150489 	/*
26075779Sxy150489 	 * Update the multicast table in the hardware
26085779Sxy150489 	 */
26095779Sxy150489 	igb_setup_multicst(igb);
26105779Sxy150489 
26116624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
26126624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
26136624Sgl147354 		return (EIO);
26146624Sgl147354 	}
26156624Sgl147354 
26165779Sxy150489 	return (0);
26175779Sxy150489 }
26185779Sxy150489 
26199775SVitezslav.Batrla@Sun.COM static void
igb_release_multicast(igb_t * igb)26209775SVitezslav.Batrla@Sun.COM igb_release_multicast(igb_t *igb)
26219775SVitezslav.Batrla@Sun.COM {
26229775SVitezslav.Batrla@Sun.COM 	if (igb->mcast_table != NULL) {
26239775SVitezslav.Batrla@Sun.COM 		kmem_free(igb->mcast_table,
26249775SVitezslav.Batrla@Sun.COM 		    igb->mcast_alloc_count * sizeof (struct ether_addr));
26259775SVitezslav.Batrla@Sun.COM 		igb->mcast_table = NULL;
26269775SVitezslav.Batrla@Sun.COM 	}
26279775SVitezslav.Batrla@Sun.COM }
26289775SVitezslav.Batrla@Sun.COM 
26295779Sxy150489 /*
26305779Sxy150489  * igb_setup_multicast - setup multicast data structures
26315779Sxy150489  *
26325779Sxy150489  * This routine initializes all of the multicast related structures
26335779Sxy150489  * and save them in the hardware registers.
26345779Sxy150489  */
26355779Sxy150489 static void
igb_setup_multicst(igb_t * igb)26365779Sxy150489 igb_setup_multicst(igb_t *igb)
26375779Sxy150489 {
26385779Sxy150489 	uint8_t *mc_addr_list;
26395779Sxy150489 	uint32_t mc_addr_count;
26405779Sxy150489 	struct e1000_hw *hw = &igb->hw;
26415779Sxy150489 
26425779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
26439775SVitezslav.Batrla@Sun.COM 	ASSERT(igb->mcast_count <= igb->mcast_max_num);
26445779Sxy150489 
26455779Sxy150489 	mc_addr_list = (uint8_t *)igb->mcast_table;
26465779Sxy150489 	mc_addr_count = igb->mcast_count;
26475779Sxy150489 
26485779Sxy150489 	/*
26495779Sxy150489 	 * Update the multicase addresses to the MTA registers
26505779Sxy150489 	 */
265110319SJason.Xu@Sun.COM 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count);
26525779Sxy150489 }
26535779Sxy150489 
26545779Sxy150489 /*
26555779Sxy150489  * igb_get_conf - Get driver configurations set in driver.conf
26565779Sxy150489  *
26575779Sxy150489  * This routine gets user-configured values out of the configuration
26585779Sxy150489  * file igb.conf.
26595779Sxy150489  *
26605779Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
26615779Sxy150489  * default.
26625779Sxy150489  * If user does not configure a value, use the default.
26635779Sxy150489  * If user configures below the minimum, use the minumum.
26645779Sxy150489  * If user configures above the maximum, use the maxumum.
26655779Sxy150489  */
26665779Sxy150489 static void
igb_get_conf(igb_t * igb)26675779Sxy150489 igb_get_conf(igb_t *igb)
26685779Sxy150489 {
26695779Sxy150489 	struct e1000_hw *hw = &igb->hw;
26705779Sxy150489 	uint32_t default_mtu;
26715779Sxy150489 	uint32_t flow_control;
26728275SEric Cheng 	uint32_t ring_per_group;
26738275SEric Cheng 	int i;
26745779Sxy150489 
26755779Sxy150489 	/*
26765779Sxy150489 	 * igb driver supports the following user configurations:
26775779Sxy150489 	 *
26785779Sxy150489 	 * Link configurations:
26795779Sxy150489 	 *    adv_autoneg_cap
26805779Sxy150489 	 *    adv_1000fdx_cap
26815779Sxy150489 	 *    adv_100fdx_cap
26825779Sxy150489 	 *    adv_100hdx_cap
26835779Sxy150489 	 *    adv_10fdx_cap
26845779Sxy150489 	 *    adv_10hdx_cap
26855779Sxy150489 	 * Note: 1000hdx is not supported.
26865779Sxy150489 	 *
26875779Sxy150489 	 * Jumbo frame configuration:
26885779Sxy150489 	 *    default_mtu
26895779Sxy150489 	 *
26905779Sxy150489 	 * Ethernet flow control configuration:
26915779Sxy150489 	 *    flow_control
26925779Sxy150489 	 *
26935779Sxy150489 	 * Multiple rings configurations:
26945779Sxy150489 	 *    tx_queue_number
26955779Sxy150489 	 *    tx_ring_size
26965779Sxy150489 	 *    rx_queue_number
26975779Sxy150489 	 *    rx_ring_size
26985779Sxy150489 	 *
26995779Sxy150489 	 * Call igb_get_prop() to get the value for a specific
27005779Sxy150489 	 * configuration parameter.
27015779Sxy150489 	 */
27025779Sxy150489 
27035779Sxy150489 	/*
27045779Sxy150489 	 * Link configurations
27055779Sxy150489 	 */
27065779Sxy150489 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
27075779Sxy150489 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
27085779Sxy150489 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
27095779Sxy150489 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
27105779Sxy150489 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
27115779Sxy150489 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
27125779Sxy150489 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
27135779Sxy150489 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
27145779Sxy150489 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
27155779Sxy150489 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
27165779Sxy150489 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
27175779Sxy150489 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
27185779Sxy150489 
27195779Sxy150489 	/*
27205779Sxy150489 	 * Jumbo frame configurations
27215779Sxy150489 	 */
27225779Sxy150489 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
27235779Sxy150489 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
27245779Sxy150489 
27255779Sxy150489 	igb->max_frame_size = default_mtu +
27265779Sxy150489 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
27275779Sxy150489 
27285779Sxy150489 	/*
27295779Sxy150489 	 * Ethernet flow control configuration
27305779Sxy150489 	 */
27315779Sxy150489 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
27325779Sxy150489 	    e1000_fc_none, 4, e1000_fc_full);
27335779Sxy150489 	if (flow_control == 4)
27345779Sxy150489 		flow_control = e1000_fc_default;
27355779Sxy150489 
27368571SChenlu.Chen@Sun.COM 	hw->fc.requested_mode = flow_control;
27375779Sxy150489 
27385779Sxy150489 	/*
27395779Sxy150489 	 * Multiple rings configurations
27405779Sxy150489 	 */
27415779Sxy150489 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
27425779Sxy150489 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
27435779Sxy150489 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
27445779Sxy150489 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
27455779Sxy150489 
274610319SJason.Xu@Sun.COM 	igb->mr_enable = igb_get_prop(igb, PROP_MR_ENABLE, 0, 1, 0);
27478275SEric Cheng 	igb->num_rx_groups = igb_get_prop(igb, PROP_RX_GROUP_NUM,
27488275SEric Cheng 	    MIN_RX_GROUP_NUM, MAX_RX_GROUP_NUM, DEFAULT_RX_GROUP_NUM);
27498571SChenlu.Chen@Sun.COM 	/*
275011155SJason.Xu@Sun.COM 	 * Currently we do not support VMDq for 82576 and 82580.
27518571SChenlu.Chen@Sun.COM 	 * If it is e1000_82576, set num_rx_groups to 1.
27528571SChenlu.Chen@Sun.COM 	 */
275311155SJason.Xu@Sun.COM 	if (hw->mac.type >= e1000_82576)
27548571SChenlu.Chen@Sun.COM 		igb->num_rx_groups = 1;
27558275SEric Cheng 
27568275SEric Cheng 	if (igb->mr_enable) {
27578571SChenlu.Chen@Sun.COM 		igb->num_tx_rings = igb->capab->def_tx_que_num;
27588571SChenlu.Chen@Sun.COM 		igb->num_rx_rings = igb->capab->def_rx_que_num;
27598275SEric Cheng 	} else {
27608275SEric Cheng 		igb->num_tx_rings = 1;
27618275SEric Cheng 		igb->num_rx_rings = 1;
27628275SEric Cheng 
27638275SEric Cheng 		if (igb->num_rx_groups > 1) {
27648275SEric Cheng 			igb_error(igb,
27658275SEric Cheng 			    "Invalid rx groups number. Please enable multiple "
27668275SEric Cheng 			    "rings first");
27678275SEric Cheng 			igb->num_rx_groups = 1;
27688275SEric Cheng 		}
27698275SEric Cheng 	}
27708275SEric Cheng 
27718275SEric Cheng 	/*
27728275SEric Cheng 	 * Check the divisibility between rx rings and rx groups.
27738275SEric Cheng 	 */
27748275SEric Cheng 	for (i = igb->num_rx_groups; i > 0; i--) {
27758275SEric Cheng 		if ((igb->num_rx_rings % i) == 0)
27768275SEric Cheng 			break;
27778275SEric Cheng 	}
27788275SEric Cheng 	if (i != igb->num_rx_groups) {
27798275SEric Cheng 		igb_error(igb,
27808275SEric Cheng 		    "Invalid rx groups number. Downgrade the rx group "
27818275SEric Cheng 		    "number to %d.", i);
27828275SEric Cheng 		igb->num_rx_groups = i;
27838275SEric Cheng 	}
27848275SEric Cheng 
27858275SEric Cheng 	/*
27868275SEric Cheng 	 * Get the ring number per group.
27878275SEric Cheng 	 */
27888275SEric Cheng 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
27898275SEric Cheng 
27908275SEric Cheng 	if (igb->num_rx_groups == 1) {
27918275SEric Cheng 		/*
27928275SEric Cheng 		 * One rx ring group, the rx ring number is num_rx_rings.
27938275SEric Cheng 		 */
27948275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_OFF;
27958275SEric Cheng 	} else if (ring_per_group == 1) {
27968275SEric Cheng 		/*
27978275SEric Cheng 		 * Multiple rx groups, each group has one rx ring.
27988275SEric Cheng 		 */
27998275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC;
28008275SEric Cheng 	} else {
28018275SEric Cheng 		/*
28028275SEric Cheng 		 * Multiple groups and multiple rings.
28038275SEric Cheng 		 */
28048275SEric Cheng 		igb->vmdq_mode = E1000_VMDQ_MAC_RSS;
28058275SEric Cheng 	}
28068275SEric Cheng 
28075779Sxy150489 	/*
28085779Sxy150489 	 * Tunable used to force an interrupt type. The only use is
28095779Sxy150489 	 * for testing of the lesser interrupt types.
28105779Sxy150489 	 * 0 = don't force interrupt type
28115779Sxy150489 	 * 1 = force interrupt type MSIX
28125779Sxy150489 	 * 2 = force interrupt type MSI
28135779Sxy150489 	 * 3 = force interrupt type Legacy
28145779Sxy150489 	 */
28155779Sxy150489 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
28165812Sxy150489 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_NONE);
28175779Sxy150489 
28185779Sxy150489 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
28195779Sxy150489 	    0, 1, 1);
28205779Sxy150489 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
28215779Sxy150489 	    0, 1, 1);
28225779Sxy150489 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
28239188SPaul.Guo@Sun.COM 	    0, 1, 1);
28245779Sxy150489 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
28255779Sxy150489 	    0, 1, 1);
28265779Sxy150489 
28279188SPaul.Guo@Sun.COM 	/*
28289188SPaul.Guo@Sun.COM 	 * igb LSO needs the tx h/w checksum support.
28299188SPaul.Guo@Sun.COM 	 * Here LSO will be disabled if tx h/w checksum has been disabled.
28309188SPaul.Guo@Sun.COM 	 */
28319188SPaul.Guo@Sun.COM 	if (igb->tx_hcksum_enable == B_FALSE)
28329188SPaul.Guo@Sun.COM 		igb->lso_enable = B_FALSE;
28339188SPaul.Guo@Sun.COM 
28345779Sxy150489 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
28355779Sxy150489 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
28365779Sxy150489 	    DEFAULT_TX_COPY_THRESHOLD);
28375779Sxy150489 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
28385779Sxy150489 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
28395779Sxy150489 	    DEFAULT_TX_RECYCLE_THRESHOLD);
28405779Sxy150489 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
28415779Sxy150489 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
28425779Sxy150489 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
28435779Sxy150489 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
2844*12980SGuoqing.Zhu@Sun.COM 	    MIN_TX_RESCHED_THRESHOLD,
2845*12980SGuoqing.Zhu@Sun.COM 	    MIN(igb->tx_ring_size, MAX_TX_RESCHED_THRESHOLD),
2846*12980SGuoqing.Zhu@Sun.COM 	    igb->tx_ring_size > DEFAULT_TX_RESCHED_THRESHOLD ?
2847*12980SGuoqing.Zhu@Sun.COM 	    DEFAULT_TX_RESCHED_THRESHOLD : DEFAULT_TX_RESCHED_THRESHOLD_LOW);
28485779Sxy150489 
28495779Sxy150489 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
28505779Sxy150489 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
28515779Sxy150489 	    DEFAULT_RX_COPY_THRESHOLD);
28525779Sxy150489 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
28535779Sxy150489 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
28545779Sxy150489 	    DEFAULT_RX_LIMIT_PER_INTR);
28555779Sxy150489 
28565779Sxy150489 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
28578571SChenlu.Chen@Sun.COM 	    igb->capab->min_intr_throttle,
28588571SChenlu.Chen@Sun.COM 	    igb->capab->max_intr_throttle,
28598571SChenlu.Chen@Sun.COM 	    igb->capab->def_intr_throttle);
28609775SVitezslav.Batrla@Sun.COM 
28619775SVitezslav.Batrla@Sun.COM 	/*
28629775SVitezslav.Batrla@Sun.COM 	 * Max number of multicast addresses
28639775SVitezslav.Batrla@Sun.COM 	 */
28649775SVitezslav.Batrla@Sun.COM 	igb->mcast_max_num =
28659775SVitezslav.Batrla@Sun.COM 	    igb_get_prop(igb, PROP_MCAST_MAX_NUM,
28669775SVitezslav.Batrla@Sun.COM 	    MIN_MCAST_NUM, MAX_MCAST_NUM, DEFAULT_MCAST_NUM);
28675779Sxy150489 }
28685779Sxy150489 
28695779Sxy150489 /*
28705779Sxy150489  * igb_get_prop - Get a property value out of the configuration file igb.conf
28715779Sxy150489  *
28725779Sxy150489  * Caller provides the name of the property, a default value, a minimum
28735779Sxy150489  * value, and a maximum value.
28745779Sxy150489  *
28755779Sxy150489  * Return configured value of the property, with default, minimum and
28765779Sxy150489  * maximum properly applied.
28775779Sxy150489  */
28785779Sxy150489 static int
igb_get_prop(igb_t * igb,char * propname,int minval,int maxval,int defval)28795779Sxy150489 igb_get_prop(igb_t *igb,
28805779Sxy150489     char *propname,	/* name of the property */
28815779Sxy150489     int minval,		/* minimum acceptable value */
28825779Sxy150489     int maxval,		/* maximim acceptable value */
28835779Sxy150489     int defval)		/* default value */
28845779Sxy150489 {
28855779Sxy150489 	int value;
28865779Sxy150489 
28875779Sxy150489 	/*
28885779Sxy150489 	 * Call ddi_prop_get_int() to read the conf settings
28895779Sxy150489 	 */
28905779Sxy150489 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
28915779Sxy150489 	    DDI_PROP_DONTPASS, propname, defval);
28925779Sxy150489 
28935779Sxy150489 	if (value > maxval)
28945779Sxy150489 		value = maxval;
28955779Sxy150489 
28965779Sxy150489 	if (value < minval)
28975779Sxy150489 		value = minval;
28985779Sxy150489 
28995779Sxy150489 	return (value);
29005779Sxy150489 }
29015779Sxy150489 
29025779Sxy150489 /*
29035779Sxy150489  * igb_setup_link - Using the link properties to setup the link
29045779Sxy150489  */
29055779Sxy150489 int
igb_setup_link(igb_t * igb,boolean_t setup_hw)29065779Sxy150489 igb_setup_link(igb_t *igb, boolean_t setup_hw)
29075779Sxy150489 {
29085779Sxy150489 	struct e1000_mac_info *mac;
29095779Sxy150489 	struct e1000_phy_info *phy;
29105779Sxy150489 	boolean_t invalid;
29115779Sxy150489 
29125779Sxy150489 	mac = &igb->hw.mac;
29135779Sxy150489 	phy = &igb->hw.phy;
29145779Sxy150489 	invalid = B_FALSE;
29155779Sxy150489 
29165779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
29175779Sxy150489 		mac->autoneg = B_TRUE;
29185779Sxy150489 		phy->autoneg_advertised = 0;
29195779Sxy150489 
29205779Sxy150489 		/*
29215779Sxy150489 		 * 1000hdx is not supported for autonegotiation
29225779Sxy150489 		 */
29235779Sxy150489 		if (igb->param_adv_1000fdx_cap == 1)
29245779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
29255779Sxy150489 
29265779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
29275779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
29285779Sxy150489 
29295779Sxy150489 		if (igb->param_adv_100hdx_cap == 1)
29305779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
29315779Sxy150489 
29325779Sxy150489 		if (igb->param_adv_10fdx_cap == 1)
29335779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
29345779Sxy150489 
29355779Sxy150489 		if (igb->param_adv_10hdx_cap == 1)
29365779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
29375779Sxy150489 
29385779Sxy150489 		if (phy->autoneg_advertised == 0)
29395779Sxy150489 			invalid = B_TRUE;
29405779Sxy150489 	} else {
29415779Sxy150489 		mac->autoneg = B_FALSE;
29425779Sxy150489 
29435779Sxy150489 		/*
29445779Sxy150489 		 * 1000fdx and 1000hdx are not supported for forced link
29455779Sxy150489 		 */
29465779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
29475779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
29485779Sxy150489 		else if (igb->param_adv_100hdx_cap == 1)
29495779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
29505779Sxy150489 		else if (igb->param_adv_10fdx_cap == 1)
29515779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
29525779Sxy150489 		else if (igb->param_adv_10hdx_cap == 1)
29535779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
29545779Sxy150489 		else
29555779Sxy150489 			invalid = B_TRUE;
29565779Sxy150489 	}
29575779Sxy150489 
29585779Sxy150489 	if (invalid) {
29595779Sxy150489 		igb_notice(igb, "Invalid link settings. Setup link to "
29605779Sxy150489 		    "autonegotiation with full link capabilities.");
29615779Sxy150489 		mac->autoneg = B_TRUE;
29625779Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
29635779Sxy150489 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
29645779Sxy150489 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
29655779Sxy150489 	}
29665779Sxy150489 
29675779Sxy150489 	if (setup_hw) {
29685779Sxy150489 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
29695779Sxy150489 			return (IGB_FAILURE);
29705779Sxy150489 	}
29715779Sxy150489 
29725779Sxy150489 	return (IGB_SUCCESS);
29735779Sxy150489 }
29745779Sxy150489 
29755779Sxy150489 
29765779Sxy150489 /*
29775779Sxy150489  * igb_is_link_up - Check if the link is up
29785779Sxy150489  */
29795779Sxy150489 static boolean_t
igb_is_link_up(igb_t * igb)29805779Sxy150489 igb_is_link_up(igb_t *igb)
29815779Sxy150489 {
29825779Sxy150489 	struct e1000_hw *hw = &igb->hw;
29838955SChenlu.Chen@Sun.COM 	boolean_t link_up = B_FALSE;
29845779Sxy150489 
29855779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
29865779Sxy150489 
29878955SChenlu.Chen@Sun.COM 	/*
29888955SChenlu.Chen@Sun.COM 	 * get_link_status is set in the interrupt handler on link-status-change
29898955SChenlu.Chen@Sun.COM 	 * or rx sequence error interrupt.  get_link_status will stay
29908955SChenlu.Chen@Sun.COM 	 * false until the e1000_check_for_link establishes link only
29918955SChenlu.Chen@Sun.COM 	 * for copper adapters.
29928955SChenlu.Chen@Sun.COM 	 */
29938955SChenlu.Chen@Sun.COM 	switch (hw->phy.media_type) {
29948955SChenlu.Chen@Sun.COM 	case e1000_media_type_copper:
29958955SChenlu.Chen@Sun.COM 		if (hw->mac.get_link_status) {
29968955SChenlu.Chen@Sun.COM 			(void) e1000_check_for_link(hw);
29978955SChenlu.Chen@Sun.COM 			link_up = !hw->mac.get_link_status;
29988955SChenlu.Chen@Sun.COM 		} else {
29998955SChenlu.Chen@Sun.COM 			link_up = B_TRUE;
30008955SChenlu.Chen@Sun.COM 		}
30018955SChenlu.Chen@Sun.COM 		break;
30028955SChenlu.Chen@Sun.COM 	case e1000_media_type_fiber:
30038955SChenlu.Chen@Sun.COM 		(void) e1000_check_for_link(hw);
30048955SChenlu.Chen@Sun.COM 		link_up = (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU);
30058955SChenlu.Chen@Sun.COM 		break;
30068955SChenlu.Chen@Sun.COM 	case e1000_media_type_internal_serdes:
30078955SChenlu.Chen@Sun.COM 		(void) e1000_check_for_link(hw);
30088955SChenlu.Chen@Sun.COM 		link_up = hw->mac.serdes_has_link;
30098955SChenlu.Chen@Sun.COM 		break;
30105779Sxy150489 	}
30115779Sxy150489 
30125779Sxy150489 	return (link_up);
30135779Sxy150489 }
30145779Sxy150489 
30155779Sxy150489 /*
30165779Sxy150489  * igb_link_check - Link status processing
30175779Sxy150489  */
30185779Sxy150489 static boolean_t
igb_link_check(igb_t * igb)30195779Sxy150489 igb_link_check(igb_t *igb)
30205779Sxy150489 {
30215779Sxy150489 	struct e1000_hw *hw = &igb->hw;
30225779Sxy150489 	uint16_t speed = 0, duplex = 0;
30235779Sxy150489 	boolean_t link_changed = B_FALSE;
30245779Sxy150489 
30255779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
30265779Sxy150489 
30275779Sxy150489 	if (igb_is_link_up(igb)) {
30285779Sxy150489 		/*
30295779Sxy150489 		 * The Link is up, check whether it was marked as down earlier
30305779Sxy150489 		 */
30315779Sxy150489 		if (igb->link_state != LINK_STATE_UP) {
30325779Sxy150489 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
30335779Sxy150489 			igb->link_speed = speed;
30345779Sxy150489 			igb->link_duplex = duplex;
30355779Sxy150489 			igb->link_state = LINK_STATE_UP;
30365779Sxy150489 			link_changed = B_TRUE;
303711367SJason.Xu@Sun.COM 			if (!igb->link_complete)
303811367SJason.Xu@Sun.COM 				igb_stop_link_timer(igb);
30395779Sxy150489 		}
304011367SJason.Xu@Sun.COM 	} else if (igb->link_complete) {
30415779Sxy150489 		if (igb->link_state != LINK_STATE_DOWN) {
30425779Sxy150489 			igb->link_speed = 0;
30435779Sxy150489 			igb->link_duplex = 0;
30445779Sxy150489 			igb->link_state = LINK_STATE_DOWN;
30455779Sxy150489 			link_changed = B_TRUE;
30465779Sxy150489 		}
30475779Sxy150489 	}
30485779Sxy150489 
304911367SJason.Xu@Sun.COM 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
30506624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
305111367SJason.Xu@Sun.COM 		return (B_FALSE);
305211367SJason.Xu@Sun.COM 	}
30536624Sgl147354 
30545779Sxy150489 	return (link_changed);
30555779Sxy150489 }
30565779Sxy150489 
30575779Sxy150489 /*
30585779Sxy150489  * igb_local_timer - driver watchdog function
30595779Sxy150489  *
306011155SJason.Xu@Sun.COM  * This function will handle the hardware stall check, link status
306111155SJason.Xu@Sun.COM  * check and other routines.
30625779Sxy150489  */
30635779Sxy150489 static void
igb_local_timer(void * arg)30645779Sxy150489 igb_local_timer(void *arg)
30655779Sxy150489 {
30665779Sxy150489 	igb_t *igb = (igb_t *)arg;
30678955SChenlu.Chen@Sun.COM 	boolean_t link_changed = B_FALSE;
30685779Sxy150489 
306911367SJason.Xu@Sun.COM 	if (igb->igb_state & IGB_ERROR) {
307011367SJason.Xu@Sun.COM 		igb->reset_count++;
307111367SJason.Xu@Sun.COM 		if (igb_reset(igb) == IGB_SUCCESS)
307211367SJason.Xu@Sun.COM 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_RESTORED);
307311367SJason.Xu@Sun.COM 
307411367SJason.Xu@Sun.COM 		igb_restart_watchdog_timer(igb);
307511367SJason.Xu@Sun.COM 		return;
307611367SJason.Xu@Sun.COM 	}
307711367SJason.Xu@Sun.COM 
307811367SJason.Xu@Sun.COM 	if (igb_stall_check(igb) || (igb->igb_state & IGB_STALL)) {
30796624Sgl147354 		igb_fm_ereport(igb, DDI_FM_DEVICE_STALL);
30808955SChenlu.Chen@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
30815779Sxy150489 		igb->reset_count++;
30826624Sgl147354 		if (igb_reset(igb) == IGB_SUCCESS)
308311367SJason.Xu@Sun.COM 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_RESTORED);
308411367SJason.Xu@Sun.COM 
308511367SJason.Xu@Sun.COM 		igb_restart_watchdog_timer(igb);
308611367SJason.Xu@Sun.COM 		return;
30875779Sxy150489 	}
30885779Sxy150489 
30895779Sxy150489 	mutex_enter(&igb->gen_lock);
30908955SChenlu.Chen@Sun.COM 	if (!(igb->igb_state & IGB_SUSPENDED) && (igb->igb_state & IGB_STARTED))
30918955SChenlu.Chen@Sun.COM 		link_changed = igb_link_check(igb);
30925779Sxy150489 	mutex_exit(&igb->gen_lock);
30935779Sxy150489 
30945779Sxy150489 	if (link_changed)
30955779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
30965779Sxy150489 
30975779Sxy150489 	igb_restart_watchdog_timer(igb);
30985779Sxy150489 }
30995779Sxy150489 
31005779Sxy150489 /*
310111367SJason.Xu@Sun.COM  * igb_link_timer - link setup timer function
310211367SJason.Xu@Sun.COM  *
310311367SJason.Xu@Sun.COM  * It is called when the timer for link setup is expired, which indicates
310411367SJason.Xu@Sun.COM  * the completion of the link setup. The link state will not be updated
310511367SJason.Xu@Sun.COM  * until the link setup is completed. And the link state will not be sent
310611367SJason.Xu@Sun.COM  * to the upper layer through mac_link_update() in this function. It will
310711367SJason.Xu@Sun.COM  * be updated in the local timer routine or the interrupts service routine
310811367SJason.Xu@Sun.COM  * after the interface is started (plumbed).
310911367SJason.Xu@Sun.COM  */
311011367SJason.Xu@Sun.COM static void
igb_link_timer(void * arg)311111367SJason.Xu@Sun.COM igb_link_timer(void *arg)
311211367SJason.Xu@Sun.COM {
311311367SJason.Xu@Sun.COM 	igb_t *igb = (igb_t *)arg;
311411367SJason.Xu@Sun.COM 
311511367SJason.Xu@Sun.COM 	mutex_enter(&igb->link_lock);
311611367SJason.Xu@Sun.COM 	igb->link_complete = B_TRUE;
311711367SJason.Xu@Sun.COM 	igb->link_tid = 0;
311811367SJason.Xu@Sun.COM 	mutex_exit(&igb->link_lock);
311911367SJason.Xu@Sun.COM }
312011367SJason.Xu@Sun.COM /*
31215779Sxy150489  * igb_stall_check - check for transmit stall
31225779Sxy150489  *
31235779Sxy150489  * This function checks if the adapter is stalled (in transmit).
31245779Sxy150489  *
31255779Sxy150489  * It is called each time the watchdog timeout is invoked.
31265779Sxy150489  * If the transmit descriptor reclaim continuously fails,
31275779Sxy150489  * the watchdog value will increment by 1. If the watchdog
31285779Sxy150489  * value exceeds the threshold, the igb is assumed to
31295779Sxy150489  * have stalled and need to be reset.
31305779Sxy150489  */
31315779Sxy150489 static boolean_t
igb_stall_check(igb_t * igb)31325779Sxy150489 igb_stall_check(igb_t *igb)
31335779Sxy150489 {
31345779Sxy150489 	igb_tx_ring_t *tx_ring;
313511155SJason.Xu@Sun.COM 	struct e1000_hw *hw = &igb->hw;
31365779Sxy150489 	boolean_t result;
31375779Sxy150489 	int i;
31385779Sxy150489 
31395779Sxy150489 	if (igb->link_state != LINK_STATE_UP)
31405779Sxy150489 		return (B_FALSE);
31415779Sxy150489 
31425779Sxy150489 	/*
31435779Sxy150489 	 * If any tx ring is stalled, we'll reset the chipset
31445779Sxy150489 	 */
31455779Sxy150489 	result = B_FALSE;
31465779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
31475779Sxy150489 		tx_ring = &igb->tx_rings[i];
31485779Sxy150489 
31495779Sxy150489 		if (tx_ring->recycle_fail > 0)
31505779Sxy150489 			tx_ring->stall_watchdog++;
31515779Sxy150489 		else
31525779Sxy150489 			tx_ring->stall_watchdog = 0;
31535779Sxy150489 
31545779Sxy150489 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
31555779Sxy150489 			result = B_TRUE;
315611155SJason.Xu@Sun.COM 			if (hw->mac.type == e1000_82580) {
315711155SJason.Xu@Sun.COM 				hw->dev_spec._82575.global_device_reset
315811155SJason.Xu@Sun.COM 				    = B_TRUE;
315911155SJason.Xu@Sun.COM 			}
31605779Sxy150489 			break;
31615779Sxy150489 		}
31625779Sxy150489 	}
31635779Sxy150489 
31645779Sxy150489 	if (result) {
31655779Sxy150489 		tx_ring->stall_watchdog = 0;
31665779Sxy150489 		tx_ring->recycle_fail = 0;
31675779Sxy150489 	}
31685779Sxy150489 
31695779Sxy150489 	return (result);
31705779Sxy150489 }
31715779Sxy150489 
31725779Sxy150489 
31735779Sxy150489 /*
31745779Sxy150489  * is_valid_mac_addr - Check if the mac address is valid
31755779Sxy150489  */
31765779Sxy150489 static boolean_t
is_valid_mac_addr(uint8_t * mac_addr)31775779Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
31785779Sxy150489 {
31795779Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
31805779Sxy150489 	const uint8_t addr_test2[6] =
31815779Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
31825779Sxy150489 
31835779Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
31845779Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
31855779Sxy150489 		return (B_FALSE);
31865779Sxy150489 
31875779Sxy150489 	return (B_TRUE);
31885779Sxy150489 }
31895779Sxy150489 
31905779Sxy150489 static boolean_t
igb_find_mac_address(igb_t * igb)31915779Sxy150489 igb_find_mac_address(igb_t *igb)
31925779Sxy150489 {
31935779Sxy150489 	struct e1000_hw *hw = &igb->hw;
31945779Sxy150489 #ifdef __sparc
31955779Sxy150489 	uchar_t *bytes;
31965779Sxy150489 	struct ether_addr sysaddr;
31975779Sxy150489 	uint_t nelts;
31985779Sxy150489 	int err;
31995779Sxy150489 	boolean_t found = B_FALSE;
32005779Sxy150489 
32015779Sxy150489 	/*
32025779Sxy150489 	 * The "vendor's factory-set address" may already have
32035779Sxy150489 	 * been extracted from the chip, but if the property
32045779Sxy150489 	 * "local-mac-address" is set we use that instead.
32055779Sxy150489 	 *
32065779Sxy150489 	 * We check whether it looks like an array of 6
32075779Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
32085779Sxy150489 	 * make sense of it this way, we'll ignore it.
32095779Sxy150489 	 */
32105779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
32115779Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
32125779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
32135779Sxy150489 		if (nelts == ETHERADDRL) {
32145779Sxy150489 			while (nelts--)
32155779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
32165779Sxy150489 			found = B_TRUE;
32175779Sxy150489 		}
32185779Sxy150489 		ddi_prop_free(bytes);
32195779Sxy150489 	}
32205779Sxy150489 
32215779Sxy150489 	/*
32225779Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
32235779Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
32245779Sxy150489 	 */
32255779Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
32265779Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
32275779Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
32285779Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
32295779Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
32305779Sxy150489 				found = B_TRUE;
32315779Sxy150489 			}
32325779Sxy150489 		}
32335779Sxy150489 		ddi_prop_free(bytes);
32345779Sxy150489 	}
32355779Sxy150489 
32365779Sxy150489 	/*
32375779Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
32385779Sxy150489 	 * if we netbooted from this interface), we must use this instead
32395779Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
32405779Sxy150489 	 * get confused by the address changing as Solaris takes over!
32415779Sxy150489 	 */
32425779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
32435779Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
32445779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
32455779Sxy150489 		if (nelts == ETHERADDRL) {
32465779Sxy150489 			while (nelts--)
32475779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
32485779Sxy150489 			found = B_TRUE;
32495779Sxy150489 		}
32505779Sxy150489 		ddi_prop_free(bytes);
32515779Sxy150489 	}
32525779Sxy150489 
32535779Sxy150489 	if (found) {
32545779Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
32555779Sxy150489 		return (B_TRUE);
32565779Sxy150489 	}
32575779Sxy150489 #endif
32585779Sxy150489 
32595779Sxy150489 	/*
32605779Sxy150489 	 * Read the device MAC address from the EEPROM
32615779Sxy150489 	 */
32625779Sxy150489 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
32635779Sxy150489 		return (B_FALSE);
32645779Sxy150489 
32655779Sxy150489 	return (B_TRUE);
32665779Sxy150489 }
32675779Sxy150489 
32685779Sxy150489 #pragma inline(igb_arm_watchdog_timer)
32695779Sxy150489 
32705779Sxy150489 static void
igb_arm_watchdog_timer(igb_t * igb)32715779Sxy150489 igb_arm_watchdog_timer(igb_t *igb)
32725779Sxy150489 {
32735779Sxy150489 	/*
32745779Sxy150489 	 * Fire a watchdog timer
32755779Sxy150489 	 */
32765779Sxy150489 	igb->watchdog_tid =
32775779Sxy150489 	    timeout(igb_local_timer,
32785779Sxy150489 	    (void *)igb, 1 * drv_usectohz(1000000));
32795779Sxy150489 
32805779Sxy150489 }
32815779Sxy150489 
32825779Sxy150489 /*
32835779Sxy150489  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
32845779Sxy150489  */
32855779Sxy150489 void
igb_enable_watchdog_timer(igb_t * igb)32865779Sxy150489 igb_enable_watchdog_timer(igb_t *igb)
32875779Sxy150489 {
32885779Sxy150489 	mutex_enter(&igb->watchdog_lock);
32895779Sxy150489 
32905779Sxy150489 	if (!igb->watchdog_enable) {
32915779Sxy150489 		igb->watchdog_enable = B_TRUE;
32925779Sxy150489 		igb->watchdog_start = B_TRUE;
32935779Sxy150489 		igb_arm_watchdog_timer(igb);
32945779Sxy150489 	}
32955779Sxy150489 
32965779Sxy150489 	mutex_exit(&igb->watchdog_lock);
32975779Sxy150489 
32985779Sxy150489 }
32995779Sxy150489 
33005779Sxy150489 /*
33015779Sxy150489  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
33025779Sxy150489  */
33035779Sxy150489 void
igb_disable_watchdog_timer(igb_t * igb)33045779Sxy150489 igb_disable_watchdog_timer(igb_t *igb)
33055779Sxy150489 {
33065779Sxy150489 	timeout_id_t tid;
33075779Sxy150489 
33085779Sxy150489 	mutex_enter(&igb->watchdog_lock);
33095779Sxy150489 
33105779Sxy150489 	igb->watchdog_enable = B_FALSE;
33115779Sxy150489 	igb->watchdog_start = B_FALSE;
33125779Sxy150489 	tid = igb->watchdog_tid;
33135779Sxy150489 	igb->watchdog_tid = 0;
33145779Sxy150489 
33155779Sxy150489 	mutex_exit(&igb->watchdog_lock);
33165779Sxy150489 
33175779Sxy150489 	if (tid != 0)
33185779Sxy150489 		(void) untimeout(tid);
33195779Sxy150489 
33205779Sxy150489 }
33215779Sxy150489 
33225779Sxy150489 /*
33235779Sxy150489  * igb_start_watchdog_timer - Start the driver watchdog timer
33245779Sxy150489  */
33255779Sxy150489 static void
igb_start_watchdog_timer(igb_t * igb)33265779Sxy150489 igb_start_watchdog_timer(igb_t *igb)
33275779Sxy150489 {
33285779Sxy150489 	mutex_enter(&igb->watchdog_lock);
33295779Sxy150489 
33305779Sxy150489 	if (igb->watchdog_enable) {
33315779Sxy150489 		if (!igb->watchdog_start) {
33325779Sxy150489 			igb->watchdog_start = B_TRUE;
33335779Sxy150489 			igb_arm_watchdog_timer(igb);
33345779Sxy150489 		}
33355779Sxy150489 	}
33365779Sxy150489 
33375779Sxy150489 	mutex_exit(&igb->watchdog_lock);
33385779Sxy150489 }
33395779Sxy150489 
33405779Sxy150489 /*
33415779Sxy150489  * igb_restart_watchdog_timer - Restart the driver watchdog timer
33425779Sxy150489  */
33435779Sxy150489 static void
igb_restart_watchdog_timer(igb_t * igb)33445779Sxy150489 igb_restart_watchdog_timer(igb_t *igb)
33455779Sxy150489 {
33465779Sxy150489 	mutex_enter(&igb->watchdog_lock);
33475779Sxy150489 
33485779Sxy150489 	if (igb->watchdog_start)
33495779Sxy150489 		igb_arm_watchdog_timer(igb);
33505779Sxy150489 
33515779Sxy150489 	mutex_exit(&igb->watchdog_lock);
33525779Sxy150489 }
33535779Sxy150489 
33545779Sxy150489 /*
33555779Sxy150489  * igb_stop_watchdog_timer - Stop the driver watchdog timer
33565779Sxy150489  */
33575779Sxy150489 static void
igb_stop_watchdog_timer(igb_t * igb)33585779Sxy150489 igb_stop_watchdog_timer(igb_t *igb)
33595779Sxy150489 {
33605779Sxy150489 	timeout_id_t tid;
33615779Sxy150489 
33625779Sxy150489 	mutex_enter(&igb->watchdog_lock);
33635779Sxy150489 
33645779Sxy150489 	igb->watchdog_start = B_FALSE;
33655779Sxy150489 	tid = igb->watchdog_tid;
33665779Sxy150489 	igb->watchdog_tid = 0;
33675779Sxy150489 
33685779Sxy150489 	mutex_exit(&igb->watchdog_lock);
33695779Sxy150489 
33705779Sxy150489 	if (tid != 0)
33715779Sxy150489 		(void) untimeout(tid);
33725779Sxy150489 }
33735779Sxy150489 
33745779Sxy150489 /*
337511367SJason.Xu@Sun.COM  * igb_start_link_timer - Start the link setup timer
337611367SJason.Xu@Sun.COM  */
337711367SJason.Xu@Sun.COM static void
igb_start_link_timer(struct igb * igb)337811367SJason.Xu@Sun.COM igb_start_link_timer(struct igb *igb)
337911367SJason.Xu@Sun.COM {
338011367SJason.Xu@Sun.COM 	struct e1000_hw *hw = &igb->hw;
338111367SJason.Xu@Sun.COM 	clock_t link_timeout;
338211367SJason.Xu@Sun.COM 
338311367SJason.Xu@Sun.COM 	if (hw->mac.autoneg)
338411367SJason.Xu@Sun.COM 		link_timeout = PHY_AUTO_NEG_LIMIT *
338511367SJason.Xu@Sun.COM 		    drv_usectohz(100000);
338611367SJason.Xu@Sun.COM 	else
338711367SJason.Xu@Sun.COM 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
338811367SJason.Xu@Sun.COM 
338911367SJason.Xu@Sun.COM 	mutex_enter(&igb->link_lock);
339011367SJason.Xu@Sun.COM 	if (hw->phy.autoneg_wait_to_complete) {
339111367SJason.Xu@Sun.COM 		igb->link_complete = B_TRUE;
339211367SJason.Xu@Sun.COM 	} else {
339311367SJason.Xu@Sun.COM 		igb->link_complete = B_FALSE;
339411367SJason.Xu@Sun.COM 		igb->link_tid = timeout(igb_link_timer, (void *)igb,
339511367SJason.Xu@Sun.COM 		    link_timeout);
339611367SJason.Xu@Sun.COM 	}
339711367SJason.Xu@Sun.COM 	mutex_exit(&igb->link_lock);
339811367SJason.Xu@Sun.COM }
339911367SJason.Xu@Sun.COM 
340011367SJason.Xu@Sun.COM /*
340111367SJason.Xu@Sun.COM  * igb_stop_link_timer - Stop the link setup timer
340211367SJason.Xu@Sun.COM  */
340311367SJason.Xu@Sun.COM static void
igb_stop_link_timer(struct igb * igb)340411367SJason.Xu@Sun.COM igb_stop_link_timer(struct igb *igb)
340511367SJason.Xu@Sun.COM {
340611367SJason.Xu@Sun.COM 	timeout_id_t tid;
340711367SJason.Xu@Sun.COM 
340811367SJason.Xu@Sun.COM 	mutex_enter(&igb->link_lock);
340911367SJason.Xu@Sun.COM 	igb->link_complete = B_TRUE;
341011367SJason.Xu@Sun.COM 	tid = igb->link_tid;
341111367SJason.Xu@Sun.COM 	igb->link_tid = 0;
341211367SJason.Xu@Sun.COM 	mutex_exit(&igb->link_lock);
341311367SJason.Xu@Sun.COM 
341411367SJason.Xu@Sun.COM 	if (tid != 0)
341511367SJason.Xu@Sun.COM 		(void) untimeout(tid);
341611367SJason.Xu@Sun.COM }
341711367SJason.Xu@Sun.COM 
341811367SJason.Xu@Sun.COM /*
34195779Sxy150489  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
34205779Sxy150489  */
34215779Sxy150489 static void
igb_disable_adapter_interrupts(igb_t * igb)34225779Sxy150489 igb_disable_adapter_interrupts(igb_t *igb)
34235779Sxy150489 {
34245779Sxy150489 	struct e1000_hw *hw = &igb->hw;
34255779Sxy150489 
34265779Sxy150489 	/*
34275779Sxy150489 	 * Set the IMC register to mask all the interrupts,
34285779Sxy150489 	 * including the tx interrupts.
34295779Sxy150489 	 */
34308571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IMC, ~0);
34318571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
34325779Sxy150489 
34335779Sxy150489 	/*
34345779Sxy150489 	 * Additional disabling for MSI-X
34355779Sxy150489 	 */
34365779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
34378571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMC, ~0);
34388571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, 0);
34398571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAM, 0);
34405779Sxy150489 	}
34415779Sxy150489 
34425779Sxy150489 	E1000_WRITE_FLUSH(hw);
34435779Sxy150489 }
34445779Sxy150489 
34455779Sxy150489 /*
344611155SJason.Xu@Sun.COM  * igb_enable_adapter_interrupts_82580 - Enable NIC interrupts for 82580
344711155SJason.Xu@Sun.COM  */
344811155SJason.Xu@Sun.COM static void
igb_enable_adapter_interrupts_82580(igb_t * igb)344911155SJason.Xu@Sun.COM igb_enable_adapter_interrupts_82580(igb_t *igb)
345011155SJason.Xu@Sun.COM {
345111155SJason.Xu@Sun.COM 	struct e1000_hw *hw = &igb->hw;
345211155SJason.Xu@Sun.COM 
345311155SJason.Xu@Sun.COM 	/* Clear any pending interrupts */
345411155SJason.Xu@Sun.COM 	(void) E1000_READ_REG(hw, E1000_ICR);
345511155SJason.Xu@Sun.COM 	igb->ims_mask |= E1000_IMS_DRSTA;
345611155SJason.Xu@Sun.COM 
345711155SJason.Xu@Sun.COM 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
345811155SJason.Xu@Sun.COM 
345911155SJason.Xu@Sun.COM 		/* Interrupt enabling for MSI-X */
346011155SJason.Xu@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
346111155SJason.Xu@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
346211155SJason.Xu@Sun.COM 		igb->ims_mask = (E1000_IMS_LSC | E1000_IMS_DRSTA);
346311155SJason.Xu@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS, igb->ims_mask);
346411155SJason.Xu@Sun.COM 	} else { /* Interrupt enabling for MSI and legacy */
346511155SJason.Xu@Sun.COM 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
346611155SJason.Xu@Sun.COM 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
346711155SJason.Xu@Sun.COM 		igb->ims_mask |= E1000_IMS_DRSTA;
346811155SJason.Xu@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS, igb->ims_mask);
346911155SJason.Xu@Sun.COM 	}
347011155SJason.Xu@Sun.COM 
347111155SJason.Xu@Sun.COM 	/* Disable auto-mask for ICR interrupt bits */
347211155SJason.Xu@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
347311155SJason.Xu@Sun.COM 
347411155SJason.Xu@Sun.COM 	E1000_WRITE_FLUSH(hw);
347511155SJason.Xu@Sun.COM }
347611155SJason.Xu@Sun.COM 
347711155SJason.Xu@Sun.COM /*
34788571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82576 - Enable NIC interrupts for 82576
34795779Sxy150489  */
34805779Sxy150489 static void
igb_enable_adapter_interrupts_82576(igb_t * igb)34818571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82576(igb_t *igb)
34828571SChenlu.Chen@Sun.COM {
34838571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
34848571SChenlu.Chen@Sun.COM 
34858955SChenlu.Chen@Sun.COM 	/* Clear any pending interrupts */
34868955SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(hw, E1000_ICR);
34878955SChenlu.Chen@Sun.COM 
34888571SChenlu.Chen@Sun.COM 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
34898571SChenlu.Chen@Sun.COM 
34908571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI-X */
34918571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
34928571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
34938571SChenlu.Chen@Sun.COM 		igb->ims_mask = E1000_IMS_LSC;
34948571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
34958571SChenlu.Chen@Sun.COM 	} else {
34968571SChenlu.Chen@Sun.COM 		/* Interrupt enabling for MSI and legacy */
34978571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
34988571SChenlu.Chen@Sun.COM 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
34998571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG(hw, E1000_IMS,
35008571SChenlu.Chen@Sun.COM 		    (IMS_ENABLE_MASK | E1000_IMS_TXQE));
35018571SChenlu.Chen@Sun.COM 	}
35028571SChenlu.Chen@Sun.COM 
35038571SChenlu.Chen@Sun.COM 	/* Disable auto-mask for ICR interrupt bits */
35048571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IAM, 0);
35058571SChenlu.Chen@Sun.COM 
35068571SChenlu.Chen@Sun.COM 	E1000_WRITE_FLUSH(hw);
35078571SChenlu.Chen@Sun.COM }
35088571SChenlu.Chen@Sun.COM 
35098571SChenlu.Chen@Sun.COM /*
35108571SChenlu.Chen@Sun.COM  * igb_enable_adapter_interrupts_82575 - Enable NIC interrupts for 82575
35118571SChenlu.Chen@Sun.COM  */
35128571SChenlu.Chen@Sun.COM static void
igb_enable_adapter_interrupts_82575(igb_t * igb)35138571SChenlu.Chen@Sun.COM igb_enable_adapter_interrupts_82575(igb_t *igb)
35145779Sxy150489 {
35155779Sxy150489 	struct e1000_hw *hw = &igb->hw;
35165779Sxy150489 	uint32_t reg;
35175779Sxy150489 
35188955SChenlu.Chen@Sun.COM 	/* Clear any pending interrupts */
35198955SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(hw, E1000_ICR);
35208955SChenlu.Chen@Sun.COM 
35215779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
35225779Sxy150489 		/* Interrupt enabling for MSI-X */
35235779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
35245779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
35258275SEric Cheng 		igb->ims_mask = E1000_IMS_LSC;
35265779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
35275779Sxy150489 
35285779Sxy150489 		/* Enable MSI-X PBA support */
35295779Sxy150489 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
35305779Sxy150489 		reg |= E1000_CTRL_EXT_PBA_CLR;
35315779Sxy150489 
35325779Sxy150489 		/* Non-selective interrupt clear-on-read */
35335779Sxy150489 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
35345779Sxy150489 
35355779Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
35365779Sxy150489 	} else {
35375779Sxy150489 		/* Interrupt enabling for MSI and legacy */
35388275SEric Cheng 		igb->ims_mask = IMS_ENABLE_MASK;
35395779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
35405779Sxy150489 	}
35415779Sxy150489 
35425779Sxy150489 	E1000_WRITE_FLUSH(hw);
35435779Sxy150489 }
35445779Sxy150489 
35455779Sxy150489 /*
35465779Sxy150489  * Loopback Support
35475779Sxy150489  */
35485779Sxy150489 static lb_property_t lb_normal =
35495779Sxy150489 	{ normal,	"normal",	IGB_LB_NONE		};
35505779Sxy150489 static lb_property_t lb_external =
35515779Sxy150489 	{ external,	"External",	IGB_LB_EXTERNAL		};
35525779Sxy150489 static lb_property_t lb_phy =
35535779Sxy150489 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
35545779Sxy150489 static lb_property_t lb_serdes =
35555779Sxy150489 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
35565779Sxy150489 
35575779Sxy150489 enum ioc_reply
igb_loopback_ioctl(igb_t * igb,struct iocblk * iocp,mblk_t * mp)35585779Sxy150489 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
35595779Sxy150489 {
35605779Sxy150489 	lb_info_sz_t *lbsp;
35615779Sxy150489 	lb_property_t *lbpp;
35625779Sxy150489 	struct e1000_hw *hw;
35635779Sxy150489 	uint32_t *lbmp;
35645779Sxy150489 	uint32_t size;
35655779Sxy150489 	uint32_t value;
35665779Sxy150489 
35675779Sxy150489 	hw = &igb->hw;
35685779Sxy150489 
35695779Sxy150489 	if (mp->b_cont == NULL)
35705779Sxy150489 		return (IOC_INVAL);
35715779Sxy150489 
35725779Sxy150489 	switch (iocp->ioc_cmd) {
35735779Sxy150489 	default:
35745779Sxy150489 		return (IOC_INVAL);
35755779Sxy150489 
35765779Sxy150489 	case LB_GET_INFO_SIZE:
35775779Sxy150489 		size = sizeof (lb_info_sz_t);
35785779Sxy150489 		if (iocp->ioc_count != size)
35795779Sxy150489 			return (IOC_INVAL);
35805779Sxy150489 
35815779Sxy150489 		value = sizeof (lb_normal);
35825779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
35835779Sxy150489 			value += sizeof (lb_phy);
35845779Sxy150489 		else
35855779Sxy150489 			value += sizeof (lb_serdes);
35865779Sxy150489 		value += sizeof (lb_external);
35875779Sxy150489 
35885779Sxy150489 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
35895779Sxy150489 		*lbsp = value;
35905779Sxy150489 		break;
35915779Sxy150489 
35925779Sxy150489 	case LB_GET_INFO:
35935779Sxy150489 		value = sizeof (lb_normal);
35945779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
35955779Sxy150489 			value += sizeof (lb_phy);
35965779Sxy150489 		else
35975779Sxy150489 			value += sizeof (lb_serdes);
35985779Sxy150489 		value += sizeof (lb_external);
35995779Sxy150489 
36005779Sxy150489 		size = value;
36015779Sxy150489 		if (iocp->ioc_count != size)
36025779Sxy150489 			return (IOC_INVAL);
36035779Sxy150489 
36045779Sxy150489 		value = 0;
36055779Sxy150489 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
36065779Sxy150489 
36075779Sxy150489 		lbpp[value++] = lb_normal;
36085779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
36095779Sxy150489 			lbpp[value++] = lb_phy;
36105779Sxy150489 		else
36115779Sxy150489 			lbpp[value++] = lb_serdes;
36125779Sxy150489 		lbpp[value++] = lb_external;
36135779Sxy150489 		break;
36145779Sxy150489 
36155779Sxy150489 	case LB_GET_MODE:
36165779Sxy150489 		size = sizeof (uint32_t);
36175779Sxy150489 		if (iocp->ioc_count != size)
36185779Sxy150489 			return (IOC_INVAL);
36195779Sxy150489 
36205779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
36215779Sxy150489 		*lbmp = igb->loopback_mode;
36225779Sxy150489 		break;
36235779Sxy150489 
36245779Sxy150489 	case LB_SET_MODE:
36255779Sxy150489 		size = 0;
36265779Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
36275779Sxy150489 			return (IOC_INVAL);
36285779Sxy150489 
36295779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
36305779Sxy150489 		if (!igb_set_loopback_mode(igb, *lbmp))
36315779Sxy150489 			return (IOC_INVAL);
36325779Sxy150489 		break;
36335779Sxy150489 	}
36345779Sxy150489 
36355779Sxy150489 	iocp->ioc_count = size;
36365779Sxy150489 	iocp->ioc_error = 0;
36375779Sxy150489 
36386624Sgl147354 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
36396624Sgl147354 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
36406624Sgl147354 		return (IOC_INVAL);
36416624Sgl147354 	}
36426624Sgl147354 
36435779Sxy150489 	return (IOC_REPLY);
36445779Sxy150489 }
36455779Sxy150489 
36465779Sxy150489 /*
36475779Sxy150489  * igb_set_loopback_mode - Setup loopback based on the loopback mode
36485779Sxy150489  */
36495779Sxy150489 static boolean_t
igb_set_loopback_mode(igb_t * igb,uint32_t mode)36505779Sxy150489 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
36515779Sxy150489 {
36525779Sxy150489 	struct e1000_hw *hw;
365311502SChenlu.Chen@Sun.COM 	int i;
36545779Sxy150489 
36555779Sxy150489 	if (mode == igb->loopback_mode)
36565779Sxy150489 		return (B_TRUE);
36575779Sxy150489 
36585779Sxy150489 	hw = &igb->hw;
36595779Sxy150489 
36605779Sxy150489 	igb->loopback_mode = mode;
36615779Sxy150489 
36625779Sxy150489 	if (mode == IGB_LB_NONE) {
36635779Sxy150489 		/* Reset the chip */
36645779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_TRUE;
36655779Sxy150489 		(void) igb_reset(igb);
36665779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_FALSE;
36675779Sxy150489 		return (B_TRUE);
36685779Sxy150489 	}
36695779Sxy150489 
36705779Sxy150489 	mutex_enter(&igb->gen_lock);
36715779Sxy150489 
36725779Sxy150489 	switch (mode) {
36735779Sxy150489 	default:
36745779Sxy150489 		mutex_exit(&igb->gen_lock);
36755779Sxy150489 		return (B_FALSE);
36765779Sxy150489 
36775779Sxy150489 	case IGB_LB_EXTERNAL:
36785779Sxy150489 		igb_set_external_loopback(igb);
36795779Sxy150489 		break;
36805779Sxy150489 
36815779Sxy150489 	case IGB_LB_INTERNAL_PHY:
36825779Sxy150489 		igb_set_internal_phy_loopback(igb);
36835779Sxy150489 		break;
36845779Sxy150489 
36855779Sxy150489 	case IGB_LB_INTERNAL_SERDES:
36865779Sxy150489 		igb_set_internal_serdes_loopback(igb);
36875779Sxy150489 		break;
36885779Sxy150489 	}
36895779Sxy150489 
36905779Sxy150489 	mutex_exit(&igb->gen_lock);
36915779Sxy150489 
369211502SChenlu.Chen@Sun.COM 	/*
369311502SChenlu.Chen@Sun.COM 	 * When external loopback is set, wait up to 1000ms to get the link up.
369411502SChenlu.Chen@Sun.COM 	 * According to test, 1000ms can work and it's an experimental value.
369511502SChenlu.Chen@Sun.COM 	 */
369611502SChenlu.Chen@Sun.COM 	if (mode == IGB_LB_EXTERNAL) {
369711502SChenlu.Chen@Sun.COM 		for (i = 0; i <= 10; i++) {
369811502SChenlu.Chen@Sun.COM 			mutex_enter(&igb->gen_lock);
369911502SChenlu.Chen@Sun.COM 			(void) igb_link_check(igb);
370011502SChenlu.Chen@Sun.COM 			mutex_exit(&igb->gen_lock);
370111502SChenlu.Chen@Sun.COM 
370211502SChenlu.Chen@Sun.COM 			if (igb->link_state == LINK_STATE_UP)
370311502SChenlu.Chen@Sun.COM 				break;
370411502SChenlu.Chen@Sun.COM 
370511502SChenlu.Chen@Sun.COM 			msec_delay(100);
370611502SChenlu.Chen@Sun.COM 		}
370711502SChenlu.Chen@Sun.COM 
370811502SChenlu.Chen@Sun.COM 		if (igb->link_state != LINK_STATE_UP) {
370911502SChenlu.Chen@Sun.COM 			/*
371011502SChenlu.Chen@Sun.COM 			 * Does not support external loopback.
371111502SChenlu.Chen@Sun.COM 			 * Reset driver to loopback none.
371211502SChenlu.Chen@Sun.COM 			 */
371311502SChenlu.Chen@Sun.COM 			igb->loopback_mode = IGB_LB_NONE;
371411502SChenlu.Chen@Sun.COM 
371511502SChenlu.Chen@Sun.COM 			/* Reset the chip */
371611502SChenlu.Chen@Sun.COM 			hw->phy.autoneg_wait_to_complete = B_TRUE;
371711502SChenlu.Chen@Sun.COM 			(void) igb_reset(igb);
371811502SChenlu.Chen@Sun.COM 			hw->phy.autoneg_wait_to_complete = B_FALSE;
371911502SChenlu.Chen@Sun.COM 
372011502SChenlu.Chen@Sun.COM 			IGB_DEBUGLOG_0(igb, "Set external loopback failed, "
372111502SChenlu.Chen@Sun.COM 			    "reset to loopback none.");
372211502SChenlu.Chen@Sun.COM 
372311502SChenlu.Chen@Sun.COM 			return (B_FALSE);
372411502SChenlu.Chen@Sun.COM 		}
372511502SChenlu.Chen@Sun.COM 	}
372611502SChenlu.Chen@Sun.COM 
37275779Sxy150489 	return (B_TRUE);
37285779Sxy150489 }
37295779Sxy150489 
37305779Sxy150489 /*
37315779Sxy150489  * igb_set_external_loopback - Set the external loopback mode
37325779Sxy150489  */
37335779Sxy150489 static void
igb_set_external_loopback(igb_t * igb)37345779Sxy150489 igb_set_external_loopback(igb_t *igb)
37355779Sxy150489 {
37365779Sxy150489 	struct e1000_hw *hw;
3737*12980SGuoqing.Zhu@Sun.COM 	uint32_t ctrl_ext;
37385779Sxy150489 
37395779Sxy150489 	hw = &igb->hw;
37405779Sxy150489 
3741*12980SGuoqing.Zhu@Sun.COM 	/* Set link mode to PHY (00b) in the Extended Control register */
3742*12980SGuoqing.Zhu@Sun.COM 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3743*12980SGuoqing.Zhu@Sun.COM 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
3744*12980SGuoqing.Zhu@Sun.COM 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
37455779Sxy150489 
37465779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
3747*12980SGuoqing.Zhu@Sun.COM 	(void) e1000_write_phy_reg(hw, 0x9, 0x1a00);
37485779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
37495779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
37505779Sxy150489 }
37515779Sxy150489 
37525779Sxy150489 /*
37535779Sxy150489  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
37545779Sxy150489  */
37555779Sxy150489 static void
igb_set_internal_phy_loopback(igb_t * igb)37565779Sxy150489 igb_set_internal_phy_loopback(igb_t *igb)
37575779Sxy150489 {
37585779Sxy150489 	struct e1000_hw *hw;
37595779Sxy150489 	uint32_t ctrl_ext;
37605779Sxy150489 	uint16_t phy_ctrl;
37615779Sxy150489 	uint16_t phy_pconf;
37625779Sxy150489 
37635779Sxy150489 	hw = &igb->hw;
37645779Sxy150489 
37655779Sxy150489 	/* Set link mode to PHY (00b) in the Extended Control register */
37665779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
37675779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
37685779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
37695779Sxy150489 
37705779Sxy150489 	/*
37715779Sxy150489 	 * Set PHY control register (0x4140):
37725779Sxy150489 	 *    Set full duplex mode
37735779Sxy150489 	 *    Set loopback bit
37745779Sxy150489 	 *    Clear auto-neg enable bit
37755779Sxy150489 	 *    Set PHY speed
37765779Sxy150489 	 */
37775779Sxy150489 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
37785779Sxy150489 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
37795779Sxy150489 
37805779Sxy150489 	/* Set the link disable bit in the Port Configuration register */
37815779Sxy150489 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
37825779Sxy150489 	phy_pconf |= (uint16_t)1 << 14;
37835779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
37845779Sxy150489 }
37855779Sxy150489 
37865779Sxy150489 /*
37875779Sxy150489  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
37885779Sxy150489  */
37895779Sxy150489 static void
igb_set_internal_serdes_loopback(igb_t * igb)37905779Sxy150489 igb_set_internal_serdes_loopback(igb_t *igb)
37915779Sxy150489 {
37925779Sxy150489 	struct e1000_hw *hw;
37935779Sxy150489 	uint32_t ctrl_ext;
37945779Sxy150489 	uint32_t ctrl;
37955779Sxy150489 	uint32_t pcs_lctl;
37965779Sxy150489 	uint32_t connsw;
37975779Sxy150489 
37985779Sxy150489 	hw = &igb->hw;
37995779Sxy150489 
38005779Sxy150489 	/* Set link mode to SerDes (11b) in the Extended Control register */
38015779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
38025779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
38035779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
38045779Sxy150489 
38055779Sxy150489 	/* Configure the SerDes to loopback */
38065779Sxy150489 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
38075779Sxy150489 
38085779Sxy150489 	/* Set Device Control register */
38095779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
38105779Sxy150489 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
38115779Sxy150489 	    E1000_CTRL_SLU);		/* Force link up */
38125779Sxy150489 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
38135779Sxy150489 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
38145779Sxy150489 	    E1000_CTRL_LRST);		/* Clear link reset */
38155779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
38165779Sxy150489 
38175779Sxy150489 	/* Set PCS Link Control register */
38185779Sxy150489 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
38195779Sxy150489 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
38205779Sxy150489 	    E1000_PCS_LCTL_FSD |
38215779Sxy150489 	    E1000_PCS_LCTL_FDV_FULL |
38225779Sxy150489 	    E1000_PCS_LCTL_FLV_LINK_UP);
38235779Sxy150489 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
38245779Sxy150489 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
38255779Sxy150489 
38265779Sxy150489 	/* Set the Copper/Fiber Switch Control - CONNSW register */
38275779Sxy150489 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
38285779Sxy150489 	connsw &= ~E1000_CONNSW_ENRGSRC;
38295779Sxy150489 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
38305779Sxy150489 }
38315779Sxy150489 
38325779Sxy150489 #pragma inline(igb_intr_rx_work)
38335779Sxy150489 /*
38345779Sxy150489  * igb_intr_rx_work - rx processing of ISR
38355779Sxy150489  */
38365779Sxy150489 static void
igb_intr_rx_work(igb_rx_ring_t * rx_ring)38375779Sxy150489 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
38385779Sxy150489 {
38395779Sxy150489 	mblk_t *mp;
38405779Sxy150489 
38415779Sxy150489 	mutex_enter(&rx_ring->rx_lock);
38428275SEric Cheng 	mp = igb_rx(rx_ring, IGB_NO_POLL);
38435779Sxy150489 	mutex_exit(&rx_ring->rx_lock);
38445779Sxy150489 
38455779Sxy150489 	if (mp != NULL)
38468275SEric Cheng 		mac_rx_ring(rx_ring->igb->mac_hdl, rx_ring->ring_handle, mp,
38478275SEric Cheng 		    rx_ring->ring_gen_num);
38485779Sxy150489 }
38495779Sxy150489 
38505779Sxy150489 #pragma inline(igb_intr_tx_work)
38515779Sxy150489 /*
38525779Sxy150489  * igb_intr_tx_work - tx processing of ISR
38535779Sxy150489  */
38545779Sxy150489 static void
igb_intr_tx_work(igb_tx_ring_t * tx_ring)38555779Sxy150489 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
38565779Sxy150489 {
385711502SChenlu.Chen@Sun.COM 	igb_t *igb = tx_ring->igb;
385811502SChenlu.Chen@Sun.COM 
38595779Sxy150489 	/* Recycle the tx descriptors */
38605779Sxy150489 	tx_ring->tx_recycle(tx_ring);
38615779Sxy150489 
38625779Sxy150489 	/* Schedule the re-transmit */
38635779Sxy150489 	if (tx_ring->reschedule &&
386411502SChenlu.Chen@Sun.COM 	    (tx_ring->tbd_free >= igb->tx_resched_thresh)) {
38655779Sxy150489 		tx_ring->reschedule = B_FALSE;
38668275SEric Cheng 		mac_tx_ring_update(tx_ring->igb->mac_hdl, tx_ring->ring_handle);
38675779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
38685779Sxy150489 	}
38695779Sxy150489 }
38705779Sxy150489 
38718275SEric Cheng #pragma inline(igb_intr_link_work)
38725779Sxy150489 /*
38738275SEric Cheng  * igb_intr_link_work - link-status-change processing of ISR
38745779Sxy150489  */
38755779Sxy150489 static void
igb_intr_link_work(igb_t * igb)38768275SEric Cheng igb_intr_link_work(igb_t *igb)
38775779Sxy150489 {
38785779Sxy150489 	boolean_t link_changed;
38795779Sxy150489 
38805779Sxy150489 	igb_stop_watchdog_timer(igb);
38815779Sxy150489 
38825779Sxy150489 	mutex_enter(&igb->gen_lock);
38835779Sxy150489 
38845779Sxy150489 	/*
38855779Sxy150489 	 * Because we got a link-status-change interrupt, force
38865779Sxy150489 	 * e1000_check_for_link() to look at phy
38875779Sxy150489 	 */
38885779Sxy150489 	igb->hw.mac.get_link_status = B_TRUE;
38895779Sxy150489 
38905779Sxy150489 	/* igb_link_check takes care of link status change */
38915779Sxy150489 	link_changed = igb_link_check(igb);
38925779Sxy150489 
38935779Sxy150489 	/* Get new phy state */
38945779Sxy150489 	igb_get_phy_state(igb);
38955779Sxy150489 
38965779Sxy150489 	mutex_exit(&igb->gen_lock);
38975779Sxy150489 
38985779Sxy150489 	if (link_changed)
38995779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
39005779Sxy150489 
39015779Sxy150489 	igb_start_watchdog_timer(igb);
39025779Sxy150489 }
39035779Sxy150489 
39045779Sxy150489 /*
39055779Sxy150489  * igb_intr_legacy - Interrupt handler for legacy interrupts
39065779Sxy150489  */
39075779Sxy150489 static uint_t
igb_intr_legacy(void * arg1,void * arg2)39085779Sxy150489 igb_intr_legacy(void *arg1, void *arg2)
39095779Sxy150489 {
39105779Sxy150489 	igb_t *igb = (igb_t *)arg1;
39115779Sxy150489 	igb_tx_ring_t *tx_ring;
39125779Sxy150489 	uint32_t icr;
39135779Sxy150489 	mblk_t *mp;
39145779Sxy150489 	boolean_t tx_reschedule;
39155779Sxy150489 	boolean_t link_changed;
39165779Sxy150489 	uint_t result;
39175779Sxy150489 
39185779Sxy150489 	_NOTE(ARGUNUSED(arg2));
39195779Sxy150489 
39205779Sxy150489 	mutex_enter(&igb->gen_lock);
39215779Sxy150489 
39225779Sxy150489 	if (igb->igb_state & IGB_SUSPENDED) {
39235779Sxy150489 		mutex_exit(&igb->gen_lock);
39245779Sxy150489 		return (DDI_INTR_UNCLAIMED);
39255779Sxy150489 	}
39265779Sxy150489 
39275779Sxy150489 	mp = NULL;
39285779Sxy150489 	tx_reschedule = B_FALSE;
39295779Sxy150489 	link_changed = B_FALSE;
39305779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
39315779Sxy150489 
393211367SJason.Xu@Sun.COM 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
393311557SChenlu.Chen@Sun.COM 		mutex_exit(&igb->gen_lock);
393411367SJason.Xu@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
393511367SJason.Xu@Sun.COM 		atomic_or_32(&igb->igb_state, IGB_ERROR);
393611367SJason.Xu@Sun.COM 		return (DDI_INTR_UNCLAIMED);
393711367SJason.Xu@Sun.COM 	}
393811367SJason.Xu@Sun.COM 
39395779Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
39405779Sxy150489 		/*
39415779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
39425779Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
39435779Sxy150489 		 * look for work to do.
39445779Sxy150489 		 */
39455779Sxy150489 		ASSERT(igb->num_rx_rings == 1);
39465779Sxy150489 		ASSERT(igb->num_tx_rings == 1);
39475779Sxy150489 
39488571SChenlu.Chen@Sun.COM 		/* Make sure all interrupt causes cleared */
39498571SChenlu.Chen@Sun.COM 		(void) E1000_READ_REG(&igb->hw, E1000_EICR);
39508571SChenlu.Chen@Sun.COM 
39515779Sxy150489 		if (icr & E1000_ICR_RXT0) {
39528275SEric Cheng 			mp = igb_rx(&igb->rx_rings[0], IGB_NO_POLL);
39535779Sxy150489 		}
39545779Sxy150489 
39555779Sxy150489 		if (icr & E1000_ICR_TXDW) {
39565779Sxy150489 			tx_ring = &igb->tx_rings[0];
39575779Sxy150489 
39585779Sxy150489 			/* Recycle the tx descriptors */
39595779Sxy150489 			tx_ring->tx_recycle(tx_ring);
39605779Sxy150489 
39615779Sxy150489 			/* Schedule the re-transmit */
39625779Sxy150489 			tx_reschedule = (tx_ring->reschedule &&
396311502SChenlu.Chen@Sun.COM 			    (tx_ring->tbd_free >= igb->tx_resched_thresh));
39645779Sxy150489 		}
39655779Sxy150489 
39665779Sxy150489 		if (icr & E1000_ICR_LSC) {
39675779Sxy150489 			/*
39685779Sxy150489 			 * Because we got a link-status-change interrupt, force
39695779Sxy150489 			 * e1000_check_for_link() to look at phy
39705779Sxy150489 			 */
39715779Sxy150489 			igb->hw.mac.get_link_status = B_TRUE;
39725779Sxy150489 
39735779Sxy150489 			/* igb_link_check takes care of link status change */
39745779Sxy150489 			link_changed = igb_link_check(igb);
39755779Sxy150489 
39765779Sxy150489 			/* Get new phy state */
39775779Sxy150489 			igb_get_phy_state(igb);
39785779Sxy150489 		}
39795779Sxy150489 
398011155SJason.Xu@Sun.COM 		if (icr & E1000_ICR_DRSTA) {
398111155SJason.Xu@Sun.COM 			/* 82580 Full Device Reset needed */
398211367SJason.Xu@Sun.COM 			atomic_or_32(&igb->igb_state, IGB_STALL);
398311155SJason.Xu@Sun.COM 		}
398411155SJason.Xu@Sun.COM 
39855779Sxy150489 		result = DDI_INTR_CLAIMED;
39865779Sxy150489 	} else {
39875779Sxy150489 		/*
39885779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
39895779Sxy150489 		 * Don't claim this interrupt.
39905779Sxy150489 		 */
39915779Sxy150489 		result = DDI_INTR_UNCLAIMED;
39925779Sxy150489 	}
39935779Sxy150489 
39945779Sxy150489 	mutex_exit(&igb->gen_lock);
39955779Sxy150489 
39965779Sxy150489 	/*
39975779Sxy150489 	 * Do the following work outside of the gen_lock
39985779Sxy150489 	 */
39995779Sxy150489 	if (mp != NULL)
40005779Sxy150489 		mac_rx(igb->mac_hdl, NULL, mp);
40015779Sxy150489 
40025779Sxy150489 	if (tx_reschedule)  {
40035779Sxy150489 		tx_ring->reschedule = B_FALSE;
40048275SEric Cheng 		mac_tx_ring_update(igb->mac_hdl, tx_ring->ring_handle);
40055779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
40065779Sxy150489 	}
40075779Sxy150489 
40085779Sxy150489 	if (link_changed)
40095779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
40105779Sxy150489 
40115779Sxy150489 	return (result);
40125779Sxy150489 }
40135779Sxy150489 
40145779Sxy150489 /*
40155779Sxy150489  * igb_intr_msi - Interrupt handler for MSI
40165779Sxy150489  */
40175779Sxy150489 static uint_t
igb_intr_msi(void * arg1,void * arg2)40185779Sxy150489 igb_intr_msi(void *arg1, void *arg2)
40195779Sxy150489 {
40205779Sxy150489 	igb_t *igb = (igb_t *)arg1;
40215779Sxy150489 	uint32_t icr;
40225779Sxy150489 
40235779Sxy150489 	_NOTE(ARGUNUSED(arg2));
40245779Sxy150489 
40255779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
40265779Sxy150489 
402711367SJason.Xu@Sun.COM 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
402811367SJason.Xu@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
402911367SJason.Xu@Sun.COM 		atomic_or_32(&igb->igb_state, IGB_ERROR);
403011367SJason.Xu@Sun.COM 		return (DDI_INTR_CLAIMED);
403111367SJason.Xu@Sun.COM 	}
403211367SJason.Xu@Sun.COM 
40338571SChenlu.Chen@Sun.COM 	/* Make sure all interrupt causes cleared */
40348571SChenlu.Chen@Sun.COM 	(void) E1000_READ_REG(&igb->hw, E1000_EICR);
40358571SChenlu.Chen@Sun.COM 
40365779Sxy150489 	/*
40375779Sxy150489 	 * For MSI interrupt, we have only one vector,
40385779Sxy150489 	 * so we have only one rx ring and one tx ring enabled.
40395779Sxy150489 	 */
40405779Sxy150489 	ASSERT(igb->num_rx_rings == 1);
40415779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
40425779Sxy150489 
40435779Sxy150489 	if (icr & E1000_ICR_RXT0) {
40445779Sxy150489 		igb_intr_rx_work(&igb->rx_rings[0]);
40455779Sxy150489 	}
40465779Sxy150489 
40475779Sxy150489 	if (icr & E1000_ICR_TXDW) {
40485779Sxy150489 		igb_intr_tx_work(&igb->tx_rings[0]);
40495779Sxy150489 	}
40505779Sxy150489 
40515779Sxy150489 	if (icr & E1000_ICR_LSC) {
40528275SEric Cheng 		igb_intr_link_work(igb);
40535779Sxy150489 	}
40545779Sxy150489 
405511155SJason.Xu@Sun.COM 	if (icr & E1000_ICR_DRSTA) {
405611155SJason.Xu@Sun.COM 		/* 82580 Full Device Reset needed */
405711367SJason.Xu@Sun.COM 		atomic_or_32(&igb->igb_state, IGB_STALL);
405811155SJason.Xu@Sun.COM 	}
405911155SJason.Xu@Sun.COM 
40605779Sxy150489 	return (DDI_INTR_CLAIMED);
40615779Sxy150489 }
40625779Sxy150489 
40635779Sxy150489 /*
40645779Sxy150489  * igb_intr_rx - Interrupt handler for rx
40655779Sxy150489  */
40665779Sxy150489 static uint_t
igb_intr_rx(void * arg1,void * arg2)40675779Sxy150489 igb_intr_rx(void *arg1, void *arg2)
40685779Sxy150489 {
40695779Sxy150489 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
40705779Sxy150489 
40715779Sxy150489 	_NOTE(ARGUNUSED(arg2));
40725779Sxy150489 
40735779Sxy150489 	/*
40745779Sxy150489 	 * Only used via MSI-X vector so don't check cause bits
40755779Sxy150489 	 * and only clean the given ring.
40765779Sxy150489 	 */
40775779Sxy150489 	igb_intr_rx_work(rx_ring);
40785779Sxy150489 
40795779Sxy150489 	return (DDI_INTR_CLAIMED);
40805779Sxy150489 }
40815779Sxy150489 
40825779Sxy150489 /*
40838275SEric Cheng  * igb_intr_tx - Interrupt handler for tx
40848275SEric Cheng  */
40858275SEric Cheng static uint_t
igb_intr_tx(void * arg1,void * arg2)40868275SEric Cheng igb_intr_tx(void *arg1, void *arg2)
40878275SEric Cheng {
40888275SEric Cheng 	igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg1;
40898275SEric Cheng 
40908275SEric Cheng 	_NOTE(ARGUNUSED(arg2));
40918275SEric Cheng 
40928275SEric Cheng 	/*
40938275SEric Cheng 	 * Only used via MSI-X vector so don't check cause bits
40948275SEric Cheng 	 * and only clean the given ring.
40958275SEric Cheng 	 */
40968275SEric Cheng 	igb_intr_tx_work(tx_ring);
40978275SEric Cheng 
40988275SEric Cheng 	return (DDI_INTR_CLAIMED);
40998275SEric Cheng }
41008275SEric Cheng 
41018275SEric Cheng /*
41025779Sxy150489  * igb_intr_tx_other - Interrupt handler for both tx and other
41035779Sxy150489  *
41045779Sxy150489  */
41055779Sxy150489 static uint_t
igb_intr_tx_other(void * arg1,void * arg2)41065779Sxy150489 igb_intr_tx_other(void *arg1, void *arg2)
41075779Sxy150489 {
41085779Sxy150489 	igb_t *igb = (igb_t *)arg1;
41095779Sxy150489 	uint32_t icr;
41105779Sxy150489 
41115779Sxy150489 	_NOTE(ARGUNUSED(arg2));
41125779Sxy150489 
41135779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
41145779Sxy150489 
411511367SJason.Xu@Sun.COM 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
411611367SJason.Xu@Sun.COM 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
411711367SJason.Xu@Sun.COM 		atomic_or_32(&igb->igb_state, IGB_ERROR);
411811367SJason.Xu@Sun.COM 		return (DDI_INTR_CLAIMED);
411911367SJason.Xu@Sun.COM 	}
412011367SJason.Xu@Sun.COM 
41215779Sxy150489 	/*
41228275SEric Cheng 	 * Look for tx reclaiming work first. Remember, in the
41238275SEric Cheng 	 * case of only interrupt sharing, only one tx ring is
41248275SEric Cheng 	 * used
41255779Sxy150489 	 */
41265779Sxy150489 	igb_intr_tx_work(&igb->tx_rings[0]);
41275779Sxy150489 
41285779Sxy150489 	/*
41298955SChenlu.Chen@Sun.COM 	 * Check for "other" causes.
41305779Sxy150489 	 */
41315779Sxy150489 	if (icr & E1000_ICR_LSC) {
41328275SEric Cheng 		igb_intr_link_work(igb);
41335779Sxy150489 	}
41345779Sxy150489 
41358955SChenlu.Chen@Sun.COM 	/*
41368955SChenlu.Chen@Sun.COM 	 * The DOUTSYNC bit indicates a tx packet dropped because
41378955SChenlu.Chen@Sun.COM 	 * DMA engine gets "out of sync". There isn't a real fix
41388955SChenlu.Chen@Sun.COM 	 * for this. The Intel recommendation is to count the number
41398955SChenlu.Chen@Sun.COM 	 * of occurrences so user can detect when it is happening.
41408955SChenlu.Chen@Sun.COM 	 * The issue is non-fatal and there's no recovery action
41418955SChenlu.Chen@Sun.COM 	 * available.
41428955SChenlu.Chen@Sun.COM 	 */
41438955SChenlu.Chen@Sun.COM 	if (icr & E1000_ICR_DOUTSYNC) {
41448955SChenlu.Chen@Sun.COM 		IGB_STAT(igb->dout_sync);
41458955SChenlu.Chen@Sun.COM 	}
41468955SChenlu.Chen@Sun.COM 
414711155SJason.Xu@Sun.COM 	if (icr & E1000_ICR_DRSTA) {
414811155SJason.Xu@Sun.COM 		/* 82580 Full Device Reset needed */
414911367SJason.Xu@Sun.COM 		atomic_or_32(&igb->igb_state, IGB_STALL);
415011155SJason.Xu@Sun.COM 	}
415111155SJason.Xu@Sun.COM 
41525779Sxy150489 	return (DDI_INTR_CLAIMED);
41535779Sxy150489 }
41545779Sxy150489 
41555779Sxy150489 /*
41565779Sxy150489  * igb_alloc_intrs - Allocate interrupts for the driver
41575779Sxy150489  *
41585779Sxy150489  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
41595779Sxy150489  * if not successful, try Legacy.
41605779Sxy150489  * igb->intr_force can be used to force sequence to start with
41615779Sxy150489  * any of the 3 types.
41625779Sxy150489  * If MSI-X is not used, number of tx/rx rings is forced to 1.
41635779Sxy150489  */
41645779Sxy150489 static int
igb_alloc_intrs(igb_t * igb)41655779Sxy150489 igb_alloc_intrs(igb_t *igb)
41665779Sxy150489 {
41675779Sxy150489 	dev_info_t *devinfo;
41685779Sxy150489 	int intr_types;
41695779Sxy150489 	int rc;
41705779Sxy150489 
41715779Sxy150489 	devinfo = igb->dip;
41725779Sxy150489 
41735779Sxy150489 	/* Get supported interrupt types */
41745779Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
41755779Sxy150489 
41765779Sxy150489 	if (rc != DDI_SUCCESS) {
41775779Sxy150489 		igb_log(igb,
41785779Sxy150489 		    "Get supported interrupt types failed: %d", rc);
41795779Sxy150489 		return (IGB_FAILURE);
41805779Sxy150489 	}
41815779Sxy150489 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
41825779Sxy150489 
41835779Sxy150489 	igb->intr_type = 0;
41845779Sxy150489 
41855779Sxy150489 	/* Install MSI-X interrupts */
41865779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
41875779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSIX)) {
41887072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSIX);
41895779Sxy150489 
41905779Sxy150489 		if (rc == IGB_SUCCESS)
41915779Sxy150489 			return (IGB_SUCCESS);
41925779Sxy150489 
41935779Sxy150489 		igb_log(igb,
41945779Sxy150489 		    "Allocate MSI-X failed, trying MSI interrupts...");
41955779Sxy150489 	}
41965779Sxy150489 
41975779Sxy150489 	/* MSI-X not used, force rings to 1 */
41985779Sxy150489 	igb->num_rx_rings = 1;
41995779Sxy150489 	igb->num_tx_rings = 1;
42005779Sxy150489 	igb_log(igb,
42015779Sxy150489 	    "MSI-X not used, force rx and tx queue number to 1");
42025779Sxy150489 
42035779Sxy150489 	/* Install MSI interrupts */
42045779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
42055779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSI)) {
42067072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSI);
42075779Sxy150489 
42085779Sxy150489 		if (rc == IGB_SUCCESS)
42095779Sxy150489 			return (IGB_SUCCESS);
42105779Sxy150489 
42115779Sxy150489 		igb_log(igb,
42125779Sxy150489 		    "Allocate MSI failed, trying Legacy interrupts...");
42135779Sxy150489 	}
42145779Sxy150489 
42155779Sxy150489 	/* Install legacy interrupts */
42165779Sxy150489 	if (intr_types & DDI_INTR_TYPE_FIXED) {
42177072Sxy150489 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_FIXED);
42185779Sxy150489 
42195779Sxy150489 		if (rc == IGB_SUCCESS)
42205779Sxy150489 			return (IGB_SUCCESS);
42215779Sxy150489 
42225779Sxy150489 		igb_log(igb,
42235779Sxy150489 		    "Allocate Legacy interrupts failed");
42245779Sxy150489 	}
42255779Sxy150489 
42265779Sxy150489 	/* If none of the 3 types succeeded, return failure */
42275779Sxy150489 	return (IGB_FAILURE);
42285779Sxy150489 }
42295779Sxy150489 
42305779Sxy150489 /*
42317072Sxy150489  * igb_alloc_intr_handles - Allocate interrupt handles.
42325779Sxy150489  *
42337072Sxy150489  * For legacy and MSI, only 1 handle is needed.  For MSI-X,
42347072Sxy150489  * if fewer than 2 handles are available, return failure.
42355779Sxy150489  * Upon success, this sets the number of Rx rings to a number that
42367072Sxy150489  * matches the handles available for Rx interrupts.
42375779Sxy150489  */
42385779Sxy150489 static int
igb_alloc_intr_handles(igb_t * igb,int intr_type)42397072Sxy150489 igb_alloc_intr_handles(igb_t *igb, int intr_type)
42405779Sxy150489 {
42415779Sxy150489 	dev_info_t *devinfo;
42428275SEric Cheng 	int orig, request, count, avail, actual;
42438275SEric Cheng 	int diff, minimum;
42445779Sxy150489 	int rc;
42455779Sxy150489 
42465779Sxy150489 	devinfo = igb->dip;
42475779Sxy150489 
42487072Sxy150489 	switch (intr_type) {
42497072Sxy150489 	case DDI_INTR_TYPE_FIXED:
42507072Sxy150489 		request = 1;	/* Request 1 legacy interrupt handle */
42517072Sxy150489 		minimum = 1;
42527072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: legacy");
42537072Sxy150489 		break;
42547072Sxy150489 
42557072Sxy150489 	case DDI_INTR_TYPE_MSI:
42567072Sxy150489 		request = 1;	/* Request 1 MSI interrupt handle */
42577072Sxy150489 		minimum = 1;
42587072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI");
42597072Sxy150489 		break;
42607072Sxy150489 
42617072Sxy150489 	case DDI_INTR_TYPE_MSIX:
42627072Sxy150489 		/*
42638275SEric Cheng 		 * Number of vectors for the adapter is
42648275SEric Cheng 		 * # rx rings + # tx rings
42658275SEric Cheng 		 * One of tx vectors is for tx & other
42667072Sxy150489 		 */
42678275SEric Cheng 		request = igb->num_rx_rings + igb->num_tx_rings;
42688275SEric Cheng 		orig = request;
42697072Sxy150489 		minimum = 2;
42707072Sxy150489 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI-X");
42717072Sxy150489 		break;
42727072Sxy150489 
42737072Sxy150489 	default:
42745779Sxy150489 		igb_log(igb,
42757072Sxy150489 		    "invalid call to igb_alloc_intr_handles(): %d\n",
42767072Sxy150489 		    intr_type);
42775779Sxy150489 		return (IGB_FAILURE);
42785779Sxy150489 	}
42797072Sxy150489 	IGB_DEBUGLOG_2(igb, "interrupt handles requested: %d  minimum: %d",
42807072Sxy150489 	    request, minimum);
42817072Sxy150489 
42827072Sxy150489 	/*
42837072Sxy150489 	 * Get number of supported interrupts
42847072Sxy150489 	 */
42857072Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
42867072Sxy150489 	if ((rc != DDI_SUCCESS) || (count < minimum)) {
42875779Sxy150489 		igb_log(igb,
42887072Sxy150489 		    "Get supported interrupt number failed. "
42897072Sxy150489 		    "Return: %d, count: %d", rc, count);
42907072Sxy150489 		return (IGB_FAILURE);
42917072Sxy150489 	}
42927072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts supported: %d", count);
42937072Sxy150489 
42947072Sxy150489 	/*
42957072Sxy150489 	 * Get number of available interrupts
42967072Sxy150489 	 */
42977072Sxy150489 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
42987072Sxy150489 	if ((rc != DDI_SUCCESS) || (avail < minimum)) {
42997072Sxy150489 		igb_log(igb,
43007072Sxy150489 		    "Get available interrupt number failed. "
43015779Sxy150489 		    "Return: %d, available: %d", rc, avail);
43025779Sxy150489 		return (IGB_FAILURE);
43035779Sxy150489 	}
43047072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts available: %d", avail);
43055779Sxy150489 
43065779Sxy150489 	if (avail < request) {
43077072Sxy150489 		igb_log(igb, "Request %d handles, %d available",
43085779Sxy150489 		    request, avail);
43095779Sxy150489 		request = avail;
43105779Sxy150489 	}
43115779Sxy150489 
43125779Sxy150489 	actual = 0;
43135779Sxy150489 	igb->intr_cnt = 0;
43145779Sxy150489 
43157072Sxy150489 	/*
43167072Sxy150489 	 * Allocate an array of interrupt handles
43177072Sxy150489 	 */
43185779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
43195779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
43205779Sxy150489 
43217072Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, intr_type, 0,
43225779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
43235779Sxy150489 	if (rc != DDI_SUCCESS) {
43247072Sxy150489 		igb_log(igb, "Allocate interrupts failed. "
43255779Sxy150489 		    "return: %d, request: %d, actual: %d",
43265779Sxy150489 		    rc, request, actual);
43277072Sxy150489 		goto alloc_handle_fail;
43285779Sxy150489 	}
43297072Sxy150489 	IGB_DEBUGLOG_1(igb, "interrupts actually allocated: %d", actual);
43305779Sxy150489 
43315779Sxy150489 	igb->intr_cnt = actual;
43325779Sxy150489 
43337072Sxy150489 	if (actual < minimum) {
43347072Sxy150489 		igb_log(igb, "Insufficient interrupt handles allocated: %d",
43357072Sxy150489 		    actual);
43367072Sxy150489 		goto alloc_handle_fail;
43377072Sxy150489 	}
43387072Sxy150489 
43395779Sxy150489 	/*
43408275SEric Cheng 	 * For MSI-X, actual might force us to reduce number of tx & rx rings
43415779Sxy150489 	 */
43428275SEric Cheng 	if ((intr_type == DDI_INTR_TYPE_MSIX) && (orig > actual)) {
43438275SEric Cheng 		diff = orig - actual;
43448275SEric Cheng 		if (diff < igb->num_tx_rings) {
43458275SEric Cheng 			igb_log(igb,
43468275SEric Cheng 			    "MSI-X vectors force Tx queue number to %d",
43478275SEric Cheng 			    igb->num_tx_rings - diff);
43488275SEric Cheng 			igb->num_tx_rings -= diff;
43498275SEric Cheng 		} else {
43508275SEric Cheng 			igb_log(igb,
43518275SEric Cheng 			    "MSI-X vectors force Tx queue number to 1");
43528275SEric Cheng 			igb->num_tx_rings = 1;
43538275SEric Cheng 
43547072Sxy150489 			igb_log(igb,
43557072Sxy150489 			    "MSI-X vectors force Rx queue number to %d",
43568275SEric Cheng 			    actual - 1);
43578275SEric Cheng 			igb->num_rx_rings = actual - 1;
43587072Sxy150489 		}
43595779Sxy150489 	}
43605779Sxy150489 
43617072Sxy150489 	/*
43627072Sxy150489 	 * Get priority for first vector, assume remaining are all the same
43637072Sxy150489 	 */
43645779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
43655779Sxy150489 	if (rc != DDI_SUCCESS) {
43665779Sxy150489 		igb_log(igb,
43675779Sxy150489 		    "Get interrupt priority failed: %d", rc);
43687072Sxy150489 		goto alloc_handle_fail;
43695779Sxy150489 	}
43705779Sxy150489 
43715779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
43725779Sxy150489 	if (rc != DDI_SUCCESS) {
43735779Sxy150489 		igb_log(igb,
43745779Sxy150489 		    "Get interrupt cap failed: %d", rc);
43757072Sxy150489 		goto alloc_handle_fail;
43765779Sxy150489 	}
43775779Sxy150489 
43787072Sxy150489 	igb->intr_type = intr_type;
43795779Sxy150489 
43805779Sxy150489 	return (IGB_SUCCESS);
43815779Sxy150489 
43827072Sxy150489 alloc_handle_fail:
43835779Sxy150489 	igb_rem_intrs(igb);
43845779Sxy150489 
43855779Sxy150489 	return (IGB_FAILURE);
43865779Sxy150489 }
43875779Sxy150489 
43885779Sxy150489 /*
43895779Sxy150489  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
43905779Sxy150489  *
43915779Sxy150489  * Before adding the interrupt handlers, the interrupt vectors have
43925779Sxy150489  * been allocated, and the rx/tx rings have also been allocated.
43935779Sxy150489  */
43945779Sxy150489 static int
igb_add_intr_handlers(igb_t * igb)43955779Sxy150489 igb_add_intr_handlers(igb_t *igb)
43965779Sxy150489 {
43975779Sxy150489 	igb_rx_ring_t *rx_ring;
43988275SEric Cheng 	igb_tx_ring_t *tx_ring;
43995779Sxy150489 	int vector;
44005779Sxy150489 	int rc;
44015779Sxy150489 	int i;
44025779Sxy150489 
44035779Sxy150489 	vector = 0;
44045779Sxy150489 
44055779Sxy150489 	switch (igb->intr_type) {
44065779Sxy150489 	case DDI_INTR_TYPE_MSIX:
44075779Sxy150489 		/* Add interrupt handler for tx + other */
44088275SEric Cheng 		tx_ring = &igb->tx_rings[0];
44095779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
44105779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_tx_other,
44115779Sxy150489 		    (void *)igb, NULL);
44128275SEric Cheng 
44135779Sxy150489 		if (rc != DDI_SUCCESS) {
44145779Sxy150489 			igb_log(igb,
44155779Sxy150489 			    "Add tx/other interrupt handler failed: %d", rc);
44165779Sxy150489 			return (IGB_FAILURE);
44175779Sxy150489 		}
44188275SEric Cheng 		tx_ring->intr_vector = vector;
44195779Sxy150489 		vector++;
44205779Sxy150489 
44215779Sxy150489 		/* Add interrupt handler for each rx ring */
44225779Sxy150489 		for (i = 0; i < igb->num_rx_rings; i++) {
44235779Sxy150489 			rx_ring = &igb->rx_rings[i];
44245779Sxy150489 
44255779Sxy150489 			rc = ddi_intr_add_handler(igb->htable[vector],
44265779Sxy150489 			    (ddi_intr_handler_t *)igb_intr_rx,
44275779Sxy150489 			    (void *)rx_ring, NULL);
44285779Sxy150489 
44295779Sxy150489 			if (rc != DDI_SUCCESS) {
44305779Sxy150489 				igb_log(igb,
44315779Sxy150489 				    "Add rx interrupt handler failed. "
44325779Sxy150489 				    "return: %d, rx ring: %d", rc, i);
44335779Sxy150489 				for (vector--; vector >= 0; vector--) {
44345779Sxy150489 					(void) ddi_intr_remove_handler(
44355779Sxy150489 					    igb->htable[vector]);
44365779Sxy150489 				}
44375779Sxy150489 				return (IGB_FAILURE);
44385779Sxy150489 			}
44395779Sxy150489 
44405779Sxy150489 			rx_ring->intr_vector = vector;
44415779Sxy150489 
44425779Sxy150489 			vector++;
44435779Sxy150489 		}
44448275SEric Cheng 
44458275SEric Cheng 		/* Add interrupt handler for each tx ring from 2nd ring */
44468275SEric Cheng 		for (i = 1; i < igb->num_tx_rings; i++) {
44478275SEric Cheng 			tx_ring = &igb->tx_rings[i];
44488275SEric Cheng 
44498275SEric Cheng 			rc = ddi_intr_add_handler(igb->htable[vector],
44508275SEric Cheng 			    (ddi_intr_handler_t *)igb_intr_tx,
44518275SEric Cheng 			    (void *)tx_ring, NULL);
44528275SEric Cheng 
44538275SEric Cheng 			if (rc != DDI_SUCCESS) {
44548275SEric Cheng 				igb_log(igb,
44558275SEric Cheng 				    "Add tx interrupt handler failed. "
44568275SEric Cheng 				    "return: %d, tx ring: %d", rc, i);
44578275SEric Cheng 				for (vector--; vector >= 0; vector--) {
44588275SEric Cheng 					(void) ddi_intr_remove_handler(
44598275SEric Cheng 					    igb->htable[vector]);
44608275SEric Cheng 				}
44618275SEric Cheng 				return (IGB_FAILURE);
44628275SEric Cheng 			}
44638275SEric Cheng 
44648275SEric Cheng 			tx_ring->intr_vector = vector;
44658275SEric Cheng 
44668275SEric Cheng 			vector++;
44678275SEric Cheng 		}
44688275SEric Cheng 
44695779Sxy150489 		break;
44705779Sxy150489 
44715779Sxy150489 	case DDI_INTR_TYPE_MSI:
44725779Sxy150489 		/* Add interrupt handlers for the only vector */
44735779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
44745779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_msi,
44755779Sxy150489 		    (void *)igb, NULL);
44765779Sxy150489 
44775779Sxy150489 		if (rc != DDI_SUCCESS) {
44785779Sxy150489 			igb_log(igb,
44795779Sxy150489 			    "Add MSI interrupt handler failed: %d", rc);
44805779Sxy150489 			return (IGB_FAILURE);
44815779Sxy150489 		}
44825779Sxy150489 
44835779Sxy150489 		rx_ring = &igb->rx_rings[0];
44845779Sxy150489 		rx_ring->intr_vector = vector;
44855779Sxy150489 
44865779Sxy150489 		vector++;
44875779Sxy150489 		break;
44885779Sxy150489 
44895779Sxy150489 	case DDI_INTR_TYPE_FIXED:
44905779Sxy150489 		/* Add interrupt handlers for the only vector */
44915779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
44925779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_legacy,
44935779Sxy150489 		    (void *)igb, NULL);
44945779Sxy150489 
44955779Sxy150489 		if (rc != DDI_SUCCESS) {
44965779Sxy150489 			igb_log(igb,
44975779Sxy150489 			    "Add legacy interrupt handler failed: %d", rc);
44985779Sxy150489 			return (IGB_FAILURE);
44995779Sxy150489 		}
45005779Sxy150489 
45015779Sxy150489 		rx_ring = &igb->rx_rings[0];
45025779Sxy150489 		rx_ring->intr_vector = vector;
45035779Sxy150489 
45045779Sxy150489 		vector++;
45055779Sxy150489 		break;
45065779Sxy150489 
45075779Sxy150489 	default:
45085779Sxy150489 		return (IGB_FAILURE);
45095779Sxy150489 	}
45105779Sxy150489 
45115779Sxy150489 	ASSERT(vector == igb->intr_cnt);
45125779Sxy150489 
45135779Sxy150489 	return (IGB_SUCCESS);
45145779Sxy150489 }
45155779Sxy150489 
45165779Sxy150489 /*
45178571SChenlu.Chen@Sun.COM  * igb_setup_msix_82575 - setup 82575 adapter to use MSI-X interrupts
45185779Sxy150489  *
45195779Sxy150489  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
45205779Sxy150489  */
45215779Sxy150489 static void
igb_setup_msix_82575(igb_t * igb)45228571SChenlu.Chen@Sun.COM igb_setup_msix_82575(igb_t *igb)
45235779Sxy150489 {
45245779Sxy150489 	uint32_t eims = 0;
45255779Sxy150489 	int i, vector;
45265779Sxy150489 	struct e1000_hw *hw = &igb->hw;
45275779Sxy150489 
45285779Sxy150489 	/*
45298571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
45308571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
45315779Sxy150489 	 */
45325779Sxy150489 	vector = 0;
45338275SEric Cheng 
45345779Sxy150489 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
45355779Sxy150489 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
45365779Sxy150489 	vector++;
45378275SEric Cheng 
45385779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
45395779Sxy150489 		/*
45405779Sxy150489 		 * Set vector for each rx ring
45415779Sxy150489 		 */
45425779Sxy150489 		eims = (E1000_EICR_RX_QUEUE0 << i);
45435779Sxy150489 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
45445779Sxy150489 
45455779Sxy150489 		/*
45468571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
45478571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
45485779Sxy150489 		 */
45495779Sxy150489 		igb->eims_mask |= eims;
45505779Sxy150489 
45515779Sxy150489 		vector++;
45525779Sxy150489 	}
45535779Sxy150489 
45548275SEric Cheng 	for (i = 1; i < igb->num_tx_rings; i++) {
45558275SEric Cheng 		/*
45568275SEric Cheng 		 * Set vector for each tx ring from 2nd tx ring
45578275SEric Cheng 		 */
45588275SEric Cheng 		eims = (E1000_EICR_TX_QUEUE0 << i);
45598275SEric Cheng 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
45608275SEric Cheng 
45618275SEric Cheng 		/*
45628571SChenlu.Chen@Sun.COM 		 * Accumulate bits to enable in
45638571SChenlu.Chen@Sun.COM 		 * igb_enable_adapter_interrupts_82575()
45648275SEric Cheng 		 */
45658275SEric Cheng 		igb->eims_mask |= eims;
45668275SEric Cheng 
45678275SEric Cheng 		vector++;
45688275SEric Cheng 	}
45698275SEric Cheng 
45705779Sxy150489 	ASSERT(vector == igb->intr_cnt);
45715779Sxy150489 
45725779Sxy150489 	/*
45735779Sxy150489 	 * Disable IAM for ICR interrupt bits
45745779Sxy150489 	 */
45755779Sxy150489 	E1000_WRITE_REG(hw, E1000_IAM, 0);
45765779Sxy150489 	E1000_WRITE_FLUSH(hw);
45775779Sxy150489 }
45785779Sxy150489 
45795779Sxy150489 /*
45808571SChenlu.Chen@Sun.COM  * igb_setup_msix_82576 - setup 82576 adapter to use MSI-X interrupts
45818571SChenlu.Chen@Sun.COM  *
45828571SChenlu.Chen@Sun.COM  * 82576 uses a table based method for assigning vectors.  Each queue has a
45838571SChenlu.Chen@Sun.COM  * single entry in the table to which we write a vector number along with a
45848571SChenlu.Chen@Sun.COM  * "valid" bit.  The entry is a single byte in a 4-byte register.  Vectors
45858571SChenlu.Chen@Sun.COM  * take a different position in the 4-byte register depending on whether
45868571SChenlu.Chen@Sun.COM  * they are numbered above or below 8.
45878571SChenlu.Chen@Sun.COM  */
45888571SChenlu.Chen@Sun.COM static void
igb_setup_msix_82576(igb_t * igb)45898571SChenlu.Chen@Sun.COM igb_setup_msix_82576(igb_t *igb)
45908571SChenlu.Chen@Sun.COM {
45918571SChenlu.Chen@Sun.COM 	struct e1000_hw *hw = &igb->hw;
45928571SChenlu.Chen@Sun.COM 	uint32_t ivar, index, vector;
45938571SChenlu.Chen@Sun.COM 	int i;
45948571SChenlu.Chen@Sun.COM 
45958571SChenlu.Chen@Sun.COM 	/* must enable msi-x capability before IVAR settings */
45968571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_GPIE,
45978571SChenlu.Chen@Sun.COM 	    (E1000_GPIE_MSIX_MODE | E1000_GPIE_PBA | E1000_GPIE_NSICR));
45988571SChenlu.Chen@Sun.COM 
45998571SChenlu.Chen@Sun.COM 	/*
46008571SChenlu.Chen@Sun.COM 	 * Set vector for tx ring 0 and other causes.
46018571SChenlu.Chen@Sun.COM 	 * NOTE assumption that it is vector 0.
46028571SChenlu.Chen@Sun.COM 	 * This is also interdependent with installation of interrupt service
46038571SChenlu.Chen@Sun.COM 	 * routines in igb_add_intr_handlers().
46048571SChenlu.Chen@Sun.COM 	 */
46058571SChenlu.Chen@Sun.COM 
46068571SChenlu.Chen@Sun.COM 	/* assign "other" causes to vector 0 */
46078571SChenlu.Chen@Sun.COM 	vector = 0;
46088571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
46098571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
46108571SChenlu.Chen@Sun.COM 
46118571SChenlu.Chen@Sun.COM 	/* assign tx ring 0 to vector 0 */
46128571SChenlu.Chen@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
46138571SChenlu.Chen@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
46148571SChenlu.Chen@Sun.COM 
46158571SChenlu.Chen@Sun.COM 	/* prepare to enable tx & other interrupt causes */
46168571SChenlu.Chen@Sun.COM 	igb->eims_mask = (1 << vector);
46178571SChenlu.Chen@Sun.COM 
46188571SChenlu.Chen@Sun.COM 	vector ++;
46198571SChenlu.Chen@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
46208571SChenlu.Chen@Sun.COM 		/*
46218571SChenlu.Chen@Sun.COM 		 * Set vector for each rx ring
46228571SChenlu.Chen@Sun.COM 		 */
46238571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
46248571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
46258571SChenlu.Chen@Sun.COM 
46268571SChenlu.Chen@Sun.COM 		if (i < 8) {
46278571SChenlu.Chen@Sun.COM 			/* vector goes into low byte of register */
46288571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFFFF00;
46298571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID);
46308571SChenlu.Chen@Sun.COM 		} else {
46318571SChenlu.Chen@Sun.COM 			/* vector goes into third byte of register */
46328571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFF00FFFF;
46338571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
46348571SChenlu.Chen@Sun.COM 		}
46358571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
46368571SChenlu.Chen@Sun.COM 
46378571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
46388571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
46398571SChenlu.Chen@Sun.COM 
46408571SChenlu.Chen@Sun.COM 		vector ++;
46418571SChenlu.Chen@Sun.COM 	}
46428571SChenlu.Chen@Sun.COM 
46438571SChenlu.Chen@Sun.COM 	for (i = 1; i < igb->num_tx_rings; i++) {
46448571SChenlu.Chen@Sun.COM 		/*
46458571SChenlu.Chen@Sun.COM 		 * Set vector for each tx ring from 2nd tx ring.
46468571SChenlu.Chen@Sun.COM 		 * Note assumption that tx vectors numericall follow rx vectors.
46478571SChenlu.Chen@Sun.COM 		 */
46488571SChenlu.Chen@Sun.COM 		index = (i & 0x7);
46498571SChenlu.Chen@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
46508571SChenlu.Chen@Sun.COM 
46518571SChenlu.Chen@Sun.COM 		if (i < 8) {
46528571SChenlu.Chen@Sun.COM 			/* vector goes into second byte of register */
46538571SChenlu.Chen@Sun.COM 			ivar = ivar & 0xFFFF00FF;
46548571SChenlu.Chen@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 8);
46558571SChenlu.Chen@Sun.COM 		} else {
46568571SChenlu.Chen@Sun.COM 			/* vector goes into fourth byte of register */
46578571SChenlu.Chen@Sun.COM 			ivar = ivar & 0x00FFFFFF;
46588571SChenlu.Chen@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID) << 24;
46598571SChenlu.Chen@Sun.COM 		}
46608571SChenlu.Chen@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
46618571SChenlu.Chen@Sun.COM 
46628571SChenlu.Chen@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
46638571SChenlu.Chen@Sun.COM 		igb->eims_mask |= (1 << vector);
46648571SChenlu.Chen@Sun.COM 
46658571SChenlu.Chen@Sun.COM 		vector ++;
46668571SChenlu.Chen@Sun.COM 	}
46678571SChenlu.Chen@Sun.COM 
46688571SChenlu.Chen@Sun.COM 	ASSERT(vector == igb->intr_cnt);
46698571SChenlu.Chen@Sun.COM }
46708571SChenlu.Chen@Sun.COM 
46718571SChenlu.Chen@Sun.COM /*
467211155SJason.Xu@Sun.COM  * igb_setup_msix_82580 - setup 82580 adapter to use MSI-X interrupts
467311155SJason.Xu@Sun.COM  *
467411155SJason.Xu@Sun.COM  * 82580 uses same table approach at 82576 but has fewer entries.  Each
467511155SJason.Xu@Sun.COM  * queue has a single entry in the table to which we write a vector number
467611155SJason.Xu@Sun.COM  * along with a "valid" bit.  Vectors take a different position in the
467711155SJason.Xu@Sun.COM  * register depending on * whether * they are numbered above or below 4.
467811155SJason.Xu@Sun.COM  */
467911155SJason.Xu@Sun.COM static void
igb_setup_msix_82580(igb_t * igb)468011155SJason.Xu@Sun.COM igb_setup_msix_82580(igb_t *igb)
468111155SJason.Xu@Sun.COM {
468211155SJason.Xu@Sun.COM 	struct e1000_hw *hw = &igb->hw;
468311155SJason.Xu@Sun.COM 	uint32_t ivar, index, vector;
468411155SJason.Xu@Sun.COM 	int i;
468511155SJason.Xu@Sun.COM 
468611155SJason.Xu@Sun.COM 	/* must enable msi-x capability before IVAR settings */
468711155SJason.Xu@Sun.COM 	E1000_WRITE_REG(hw, E1000_GPIE, (E1000_GPIE_MSIX_MODE |
468811155SJason.Xu@Sun.COM 	    E1000_GPIE_PBA | E1000_GPIE_NSICR | E1000_GPIE_EIAME));
468911155SJason.Xu@Sun.COM 	/*
469011155SJason.Xu@Sun.COM 	 * Set vector for tx ring 0 and other causes.
469111155SJason.Xu@Sun.COM 	 * NOTE assumption that it is vector 0.
469211155SJason.Xu@Sun.COM 	 * This is also interdependent with installation of interrupt service
469311155SJason.Xu@Sun.COM 	 * routines in igb_add_intr_handlers().
469411155SJason.Xu@Sun.COM 	 */
469511155SJason.Xu@Sun.COM 
469611155SJason.Xu@Sun.COM 	/* assign "other" causes to vector 0 */
469711155SJason.Xu@Sun.COM 	vector = 0;
469811155SJason.Xu@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
469911155SJason.Xu@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
470011155SJason.Xu@Sun.COM 
470111155SJason.Xu@Sun.COM 	/* assign tx ring 0 to vector 0 */
470211155SJason.Xu@Sun.COM 	ivar = ((vector | E1000_IVAR_VALID) << 8);
470311155SJason.Xu@Sun.COM 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
470411155SJason.Xu@Sun.COM 
470511155SJason.Xu@Sun.COM 	/* prepare to enable tx & other interrupt causes */
470611155SJason.Xu@Sun.COM 	igb->eims_mask = (1 << vector);
470711155SJason.Xu@Sun.COM 
470811155SJason.Xu@Sun.COM 	vector ++;
470911155SJason.Xu@Sun.COM 
471011155SJason.Xu@Sun.COM 	for (i = 0; i < igb->num_rx_rings; i++) {
471111155SJason.Xu@Sun.COM 		/*
471211155SJason.Xu@Sun.COM 		 * Set vector for each rx ring
471311155SJason.Xu@Sun.COM 		 */
471411155SJason.Xu@Sun.COM 		index = (i >> 1);
471511155SJason.Xu@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
471611155SJason.Xu@Sun.COM 
471711155SJason.Xu@Sun.COM 		if (i & 1) {
471811155SJason.Xu@Sun.COM 			/* vector goes into third byte of register */
471911155SJason.Xu@Sun.COM 			ivar = ivar & 0xFF00FFFF;
472011155SJason.Xu@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
472111155SJason.Xu@Sun.COM 		} else {
472211155SJason.Xu@Sun.COM 			/* vector goes into low byte of register */
472311155SJason.Xu@Sun.COM 			ivar = ivar & 0xFFFFFF00;
472411155SJason.Xu@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID);
472511155SJason.Xu@Sun.COM 		}
472611155SJason.Xu@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
472711155SJason.Xu@Sun.COM 
472811155SJason.Xu@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
472911155SJason.Xu@Sun.COM 		igb->eims_mask |= (1 << vector);
473011155SJason.Xu@Sun.COM 
473111155SJason.Xu@Sun.COM 		vector ++;
473211155SJason.Xu@Sun.COM 	}
473311155SJason.Xu@Sun.COM 
473411155SJason.Xu@Sun.COM 	for (i = 1; i < igb->num_tx_rings; i++) {
473511155SJason.Xu@Sun.COM 		/*
473611155SJason.Xu@Sun.COM 		 * Set vector for each tx ring from 2nd tx ring.
473711155SJason.Xu@Sun.COM 		 * Note assumption that tx vectors numericall follow rx vectors.
473811155SJason.Xu@Sun.COM 		 */
473911155SJason.Xu@Sun.COM 		index = (i >> 1);
474011155SJason.Xu@Sun.COM 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
474111155SJason.Xu@Sun.COM 
474211155SJason.Xu@Sun.COM 		if (i & 1) {
474311155SJason.Xu@Sun.COM 			/* vector goes into high byte of register */
474411155SJason.Xu@Sun.COM 			ivar = ivar & 0x00FFFFFF;
474511155SJason.Xu@Sun.COM 			ivar |= ((vector | E1000_IVAR_VALID) << 24);
474611155SJason.Xu@Sun.COM 		} else {
474711155SJason.Xu@Sun.COM 			/* vector goes into second byte of register */
474811155SJason.Xu@Sun.COM 			ivar = ivar & 0xFFFF00FF;
474911155SJason.Xu@Sun.COM 			ivar |= (vector | E1000_IVAR_VALID) << 8;
475011155SJason.Xu@Sun.COM 		}
475111155SJason.Xu@Sun.COM 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
475211155SJason.Xu@Sun.COM 
475311155SJason.Xu@Sun.COM 		/* Accumulate interrupt-cause bits to enable */
475411155SJason.Xu@Sun.COM 		igb->eims_mask |= (1 << vector);
475511155SJason.Xu@Sun.COM 
475611155SJason.Xu@Sun.COM 		vector ++;
475711155SJason.Xu@Sun.COM 	}
475811155SJason.Xu@Sun.COM 	ASSERT(vector == igb->intr_cnt);
475911155SJason.Xu@Sun.COM }
476011155SJason.Xu@Sun.COM 
476111155SJason.Xu@Sun.COM /*
47625779Sxy150489  * igb_rem_intr_handlers - remove the interrupt handlers
47635779Sxy150489  */
47645779Sxy150489 static void
igb_rem_intr_handlers(igb_t * igb)47655779Sxy150489 igb_rem_intr_handlers(igb_t *igb)
47665779Sxy150489 {
47675779Sxy150489 	int i;
47685779Sxy150489 	int rc;
47695779Sxy150489 
47705779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
47715779Sxy150489 		rc = ddi_intr_remove_handler(igb->htable[i]);
47725779Sxy150489 		if (rc != DDI_SUCCESS) {
47735779Sxy150489 			IGB_DEBUGLOG_1(igb,
47745779Sxy150489 			    "Remove intr handler failed: %d", rc);
47755779Sxy150489 		}
47765779Sxy150489 	}
47775779Sxy150489 }
47785779Sxy150489 
47795779Sxy150489 /*
47805779Sxy150489  * igb_rem_intrs - remove the allocated interrupts
47815779Sxy150489  */
47825779Sxy150489 static void
igb_rem_intrs(igb_t * igb)47835779Sxy150489 igb_rem_intrs(igb_t *igb)
47845779Sxy150489 {
47855779Sxy150489 	int i;
47865779Sxy150489 	int rc;
47875779Sxy150489 
47885779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
47895779Sxy150489 		rc = ddi_intr_free(igb->htable[i]);
47905779Sxy150489 		if (rc != DDI_SUCCESS) {
47915779Sxy150489 			IGB_DEBUGLOG_1(igb,
47925779Sxy150489 			    "Free intr failed: %d", rc);
47935779Sxy150489 		}
47945779Sxy150489 	}
47955779Sxy150489 
47965779Sxy150489 	kmem_free(igb->htable, igb->intr_size);
47975779Sxy150489 	igb->htable = NULL;
47985779Sxy150489 }
47995779Sxy150489 
48005779Sxy150489 /*
48015779Sxy150489  * igb_enable_intrs - enable all the ddi interrupts
48025779Sxy150489  */
48035779Sxy150489 static int
igb_enable_intrs(igb_t * igb)48045779Sxy150489 igb_enable_intrs(igb_t *igb)
48055779Sxy150489 {
48065779Sxy150489 	int i;
48075779Sxy150489 	int rc;
48085779Sxy150489 
48095779Sxy150489 	/* Enable interrupts */
48105779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
48115779Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
48125779Sxy150489 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
48135779Sxy150489 		if (rc != DDI_SUCCESS) {
48145779Sxy150489 			igb_log(igb,
48155779Sxy150489 			    "Enable block intr failed: %d", rc);
48165779Sxy150489 			return (IGB_FAILURE);
48175779Sxy150489 		}
48185779Sxy150489 	} else {
48195779Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
48205779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
48215779Sxy150489 			rc = ddi_intr_enable(igb->htable[i]);
48225779Sxy150489 			if (rc != DDI_SUCCESS) {
48235779Sxy150489 				igb_log(igb,
48245779Sxy150489 				    "Enable intr failed: %d", rc);
48255779Sxy150489 				return (IGB_FAILURE);
48265779Sxy150489 			}
48275779Sxy150489 		}
48285779Sxy150489 	}
48295779Sxy150489 
48305779Sxy150489 	return (IGB_SUCCESS);
48315779Sxy150489 }
48325779Sxy150489 
48335779Sxy150489 /*
48345779Sxy150489  * igb_disable_intrs - disable all the ddi interrupts
48355779Sxy150489  */
48365779Sxy150489 static int
igb_disable_intrs(igb_t * igb)48375779Sxy150489 igb_disable_intrs(igb_t *igb)
48385779Sxy150489 {
48395779Sxy150489 	int i;
48405779Sxy150489 	int rc;
48415779Sxy150489 
48425779Sxy150489 	/* Disable all interrupts */
48435779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
48445779Sxy150489 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
48455779Sxy150489 		if (rc != DDI_SUCCESS) {
48465779Sxy150489 			igb_log(igb,
48475779Sxy150489 			    "Disable block intr failed: %d", rc);
48485779Sxy150489 			return (IGB_FAILURE);
48495779Sxy150489 		}
48505779Sxy150489 	} else {
48515779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
48525779Sxy150489 			rc = ddi_intr_disable(igb->htable[i]);
48535779Sxy150489 			if (rc != DDI_SUCCESS) {
48545779Sxy150489 				igb_log(igb,
48555779Sxy150489 				    "Disable intr failed: %d", rc);
48565779Sxy150489 				return (IGB_FAILURE);
48575779Sxy150489 			}
48585779Sxy150489 		}
48595779Sxy150489 	}
48605779Sxy150489 
48615779Sxy150489 	return (IGB_SUCCESS);
48625779Sxy150489 }
48635779Sxy150489 
48645779Sxy150489 /*
48655779Sxy150489  * igb_get_phy_state - Get and save the parameters read from PHY registers
48665779Sxy150489  */
48675779Sxy150489 static void
igb_get_phy_state(igb_t * igb)48685779Sxy150489 igb_get_phy_state(igb_t *igb)
48695779Sxy150489 {
48705779Sxy150489 	struct e1000_hw *hw = &igb->hw;
48715779Sxy150489 	uint16_t phy_ctrl;
48725779Sxy150489 	uint16_t phy_status;
48735779Sxy150489 	uint16_t phy_an_adv;
48745779Sxy150489 	uint16_t phy_an_exp;
48755779Sxy150489 	uint16_t phy_ext_status;
48765779Sxy150489 	uint16_t phy_1000t_ctrl;
48775779Sxy150489 	uint16_t phy_1000t_status;
48785779Sxy150489 	uint16_t phy_lp_able;
48795779Sxy150489 
48805779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
48815779Sxy150489 
488211502SChenlu.Chen@Sun.COM 	if (hw->phy.media_type == e1000_media_type_copper) {
488311502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
488411502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
488511502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
488611502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
488711502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
488811502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
488911502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw,
489011502SChenlu.Chen@Sun.COM 		    PHY_1000T_STATUS, &phy_1000t_status);
489111502SChenlu.Chen@Sun.COM 		(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
489211502SChenlu.Chen@Sun.COM 
489311502SChenlu.Chen@Sun.COM 		igb->param_autoneg_cap =
489411502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
489511502SChenlu.Chen@Sun.COM 		igb->param_pause_cap =
489611502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
489711502SChenlu.Chen@Sun.COM 		igb->param_asym_pause_cap =
489811502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
489911502SChenlu.Chen@Sun.COM 		igb->param_1000fdx_cap =
490011502SChenlu.Chen@Sun.COM 		    ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
490111502SChenlu.Chen@Sun.COM 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
490211502SChenlu.Chen@Sun.COM 		igb->param_1000hdx_cap =
490311502SChenlu.Chen@Sun.COM 		    ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
490411502SChenlu.Chen@Sun.COM 		    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
490511502SChenlu.Chen@Sun.COM 		igb->param_100t4_cap =
490611502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
490711502SChenlu.Chen@Sun.COM 		igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
490811502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
490911502SChenlu.Chen@Sun.COM 		igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
491011502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
491111502SChenlu.Chen@Sun.COM 		igb->param_10fdx_cap =
491211502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
491311502SChenlu.Chen@Sun.COM 		igb->param_10hdx_cap =
491411502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
491511502SChenlu.Chen@Sun.COM 		igb->param_rem_fault =
491611502SChenlu.Chen@Sun.COM 		    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
491711502SChenlu.Chen@Sun.COM 
491811502SChenlu.Chen@Sun.COM 		igb->param_adv_autoneg_cap = hw->mac.autoneg;
491911502SChenlu.Chen@Sun.COM 		igb->param_adv_pause_cap =
492011502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
492111502SChenlu.Chen@Sun.COM 		igb->param_adv_asym_pause_cap =
492211502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
492311502SChenlu.Chen@Sun.COM 		igb->param_adv_1000hdx_cap =
492411502SChenlu.Chen@Sun.COM 		    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
492511502SChenlu.Chen@Sun.COM 		igb->param_adv_100t4_cap =
492611502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
492711502SChenlu.Chen@Sun.COM 		igb->param_adv_rem_fault =
492811502SChenlu.Chen@Sun.COM 		    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
492911502SChenlu.Chen@Sun.COM 		if (igb->param_adv_autoneg_cap == 1) {
493011502SChenlu.Chen@Sun.COM 			igb->param_adv_1000fdx_cap =
493111502SChenlu.Chen@Sun.COM 			    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
493211502SChenlu.Chen@Sun.COM 			igb->param_adv_100fdx_cap =
493311502SChenlu.Chen@Sun.COM 			    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
493411502SChenlu.Chen@Sun.COM 			igb->param_adv_100hdx_cap =
493511502SChenlu.Chen@Sun.COM 			    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
493611502SChenlu.Chen@Sun.COM 			igb->param_adv_10fdx_cap =
493711502SChenlu.Chen@Sun.COM 			    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
493811502SChenlu.Chen@Sun.COM 			igb->param_adv_10hdx_cap =
493911502SChenlu.Chen@Sun.COM 			    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
494011502SChenlu.Chen@Sun.COM 		}
494111502SChenlu.Chen@Sun.COM 
494211502SChenlu.Chen@Sun.COM 		igb->param_lp_autoneg_cap =
494311502SChenlu.Chen@Sun.COM 		    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
494411502SChenlu.Chen@Sun.COM 		igb->param_lp_pause_cap =
494511502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
494611502SChenlu.Chen@Sun.COM 		igb->param_lp_asym_pause_cap =
494711502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
494811502SChenlu.Chen@Sun.COM 		igb->param_lp_1000fdx_cap =
494911502SChenlu.Chen@Sun.COM 		    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
495011502SChenlu.Chen@Sun.COM 		igb->param_lp_1000hdx_cap =
495111502SChenlu.Chen@Sun.COM 		    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
495211502SChenlu.Chen@Sun.COM 		igb->param_lp_100t4_cap =
495311502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
495411502SChenlu.Chen@Sun.COM 		igb->param_lp_100fdx_cap =
495511502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
495611502SChenlu.Chen@Sun.COM 		igb->param_lp_100hdx_cap =
495711502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
495811502SChenlu.Chen@Sun.COM 		igb->param_lp_10fdx_cap =
495911502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
496011502SChenlu.Chen@Sun.COM 		igb->param_lp_10hdx_cap =
496111502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
496211502SChenlu.Chen@Sun.COM 		igb->param_lp_rem_fault =
496311502SChenlu.Chen@Sun.COM 		    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
496411502SChenlu.Chen@Sun.COM 	} else {
496511502SChenlu.Chen@Sun.COM 		/*
496611502SChenlu.Chen@Sun.COM 		 * 1Gig Fiber adapter only offers 1Gig Full Duplex.
496711502SChenlu.Chen@Sun.COM 		 */
496811502SChenlu.Chen@Sun.COM 		igb->param_autoneg_cap = 0;
496911502SChenlu.Chen@Sun.COM 		igb->param_pause_cap = 1;
497011502SChenlu.Chen@Sun.COM 		igb->param_asym_pause_cap = 1;
497111502SChenlu.Chen@Sun.COM 		igb->param_1000fdx_cap = 1;
497211502SChenlu.Chen@Sun.COM 		igb->param_1000hdx_cap = 0;
497311502SChenlu.Chen@Sun.COM 		igb->param_100t4_cap = 0;
497411502SChenlu.Chen@Sun.COM 		igb->param_100fdx_cap = 0;
497511502SChenlu.Chen@Sun.COM 		igb->param_100hdx_cap = 0;
497611502SChenlu.Chen@Sun.COM 		igb->param_10fdx_cap = 0;
497711502SChenlu.Chen@Sun.COM 		igb->param_10hdx_cap = 0;
497811502SChenlu.Chen@Sun.COM 
497911502SChenlu.Chen@Sun.COM 		igb->param_adv_autoneg_cap = 0;
498011502SChenlu.Chen@Sun.COM 		igb->param_adv_pause_cap = 1;
498111502SChenlu.Chen@Sun.COM 		igb->param_adv_asym_pause_cap = 1;
498211502SChenlu.Chen@Sun.COM 		igb->param_adv_1000fdx_cap = 1;
498311502SChenlu.Chen@Sun.COM 		igb->param_adv_1000hdx_cap = 0;
498411502SChenlu.Chen@Sun.COM 		igb->param_adv_100t4_cap = 0;
498511502SChenlu.Chen@Sun.COM 		igb->param_adv_100fdx_cap = 0;
498611502SChenlu.Chen@Sun.COM 		igb->param_adv_100hdx_cap = 0;
498711502SChenlu.Chen@Sun.COM 		igb->param_adv_10fdx_cap = 0;
498811502SChenlu.Chen@Sun.COM 		igb->param_adv_10hdx_cap = 0;
498911502SChenlu.Chen@Sun.COM 
499011502SChenlu.Chen@Sun.COM 		igb->param_lp_autoneg_cap = 0;
499111502SChenlu.Chen@Sun.COM 		igb->param_lp_pause_cap = 0;
499211502SChenlu.Chen@Sun.COM 		igb->param_lp_asym_pause_cap = 0;
499311502SChenlu.Chen@Sun.COM 		igb->param_lp_1000fdx_cap = 0;
499411502SChenlu.Chen@Sun.COM 		igb->param_lp_1000hdx_cap = 0;
499511502SChenlu.Chen@Sun.COM 		igb->param_lp_100t4_cap = 0;
499611502SChenlu.Chen@Sun.COM 		igb->param_lp_100fdx_cap = 0;
499711502SChenlu.Chen@Sun.COM 		igb->param_lp_100hdx_cap = 0;
499811502SChenlu.Chen@Sun.COM 		igb->param_lp_10fdx_cap = 0;
499911502SChenlu.Chen@Sun.COM 		igb->param_lp_10hdx_cap = 0;
500011502SChenlu.Chen@Sun.COM 		igb->param_lp_rem_fault = 0;
50015779Sxy150489 	}
500211502SChenlu.Chen@Sun.COM }
500311502SChenlu.Chen@Sun.COM 
500411502SChenlu.Chen@Sun.COM /*
500511502SChenlu.Chen@Sun.COM  * synchronize the adv* and en* parameters.
500611502SChenlu.Chen@Sun.COM  *
500711502SChenlu.Chen@Sun.COM  * See comments in <sys/dld.h> for details of the *_en_*
500811502SChenlu.Chen@Sun.COM  * parameters. The usage of ndd for setting adv parameters will
500911502SChenlu.Chen@Sun.COM  * synchronize all the en parameters with the e1000g parameters,
501011502SChenlu.Chen@Sun.COM  * implicitly disabling any settings made via dladm.
501111502SChenlu.Chen@Sun.COM  */
501211502SChenlu.Chen@Sun.COM static void
igb_param_sync(igb_t * igb)501311502SChenlu.Chen@Sun.COM igb_param_sync(igb_t *igb)
501411502SChenlu.Chen@Sun.COM {
501511502SChenlu.Chen@Sun.COM 	igb->param_en_1000fdx_cap = igb->param_adv_1000fdx_cap;
501611502SChenlu.Chen@Sun.COM 	igb->param_en_1000hdx_cap = igb->param_adv_1000hdx_cap;
501711502SChenlu.Chen@Sun.COM 	igb->param_en_100t4_cap = igb->param_adv_100t4_cap;
501811502SChenlu.Chen@Sun.COM 	igb->param_en_100fdx_cap = igb->param_adv_100fdx_cap;
501911502SChenlu.Chen@Sun.COM 	igb->param_en_100hdx_cap = igb->param_adv_100hdx_cap;
502011502SChenlu.Chen@Sun.COM 	igb->param_en_10fdx_cap = igb->param_adv_10fdx_cap;
502111502SChenlu.Chen@Sun.COM 	igb->param_en_10hdx_cap = igb->param_adv_10hdx_cap;
50225779Sxy150489 }
50235779Sxy150489 
50245779Sxy150489 /*
50255779Sxy150489  * igb_get_driver_control
50265779Sxy150489  */
50275779Sxy150489 static void
igb_get_driver_control(struct e1000_hw * hw)50285779Sxy150489 igb_get_driver_control(struct e1000_hw *hw)
50295779Sxy150489 {
50305779Sxy150489 	uint32_t ctrl_ext;
50315779Sxy150489 
50325779Sxy150489 	/* Notify firmware that driver is in control of device */
50335779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
50345779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
50355779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
50365779Sxy150489 }
50375779Sxy150489 
50385779Sxy150489 /*
50395779Sxy150489  * igb_release_driver_control
50405779Sxy150489  */
50415779Sxy150489 static void
igb_release_driver_control(struct e1000_hw * hw)50425779Sxy150489 igb_release_driver_control(struct e1000_hw *hw)
50435779Sxy150489 {
50445779Sxy150489 	uint32_t ctrl_ext;
50455779Sxy150489 
50465779Sxy150489 	/* Notify firmware that driver is no longer in control of device */
50475779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
50485779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
50495779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
50505779Sxy150489 }
50515779Sxy150489 
50525779Sxy150489 /*
50535779Sxy150489  * igb_atomic_reserve - Atomic decrease operation
50545779Sxy150489  */
50555779Sxy150489 int
igb_atomic_reserve(uint32_t * count_p,uint32_t n)50565779Sxy150489 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
50575779Sxy150489 {
50585779Sxy150489 	uint32_t oldval;
50595779Sxy150489 	uint32_t newval;
50605779Sxy150489 
50615779Sxy150489 	/* ATOMICALLY */
50625779Sxy150489 	do {
50635779Sxy150489 		oldval = *count_p;
50645779Sxy150489 		if (oldval < n)
50655779Sxy150489 			return (-1);
50665779Sxy150489 		newval = oldval - n;
50675779Sxy150489 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
50685779Sxy150489 
50695779Sxy150489 	return (newval);
50705779Sxy150489 }
50716624Sgl147354 
50726624Sgl147354 /*
50736624Sgl147354  * FMA support
50746624Sgl147354  */
50756624Sgl147354 
50766624Sgl147354 int
igb_check_acc_handle(ddi_acc_handle_t handle)50776624Sgl147354 igb_check_acc_handle(ddi_acc_handle_t handle)
50786624Sgl147354 {
50796624Sgl147354 	ddi_fm_error_t de;
50806624Sgl147354 
50816624Sgl147354 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
50826624Sgl147354 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
50836624Sgl147354 	return (de.fme_status);
50846624Sgl147354 }
50856624Sgl147354 
50866624Sgl147354 int
igb_check_dma_handle(ddi_dma_handle_t handle)50876624Sgl147354 igb_check_dma_handle(ddi_dma_handle_t handle)
50886624Sgl147354 {
50896624Sgl147354 	ddi_fm_error_t de;
50906624Sgl147354 
50916624Sgl147354 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
50926624Sgl147354 	return (de.fme_status);
50936624Sgl147354 }
50946624Sgl147354 
50956624Sgl147354 /*
50966624Sgl147354  * The IO fault service error handling callback function
50976624Sgl147354  */
50986624Sgl147354 /*ARGSUSED*/
50996624Sgl147354 static int
igb_fm_error_cb(dev_info_t * dip,ddi_fm_error_t * err,const void * impl_data)51006624Sgl147354 igb_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
51016624Sgl147354 {
51026624Sgl147354 	/*
51036624Sgl147354 	 * as the driver can always deal with an error in any dma or
51046624Sgl147354 	 * access handle, we can just return the fme_status value.
51056624Sgl147354 	 */
51066624Sgl147354 	pci_ereport_post(dip, err, NULL);
51076624Sgl147354 	return (err->fme_status);
51086624Sgl147354 }
51096624Sgl147354 
51106624Sgl147354 static void
igb_fm_init(igb_t * igb)51116624Sgl147354 igb_fm_init(igb_t *igb)
51126624Sgl147354 {
51136624Sgl147354 	ddi_iblock_cookie_t iblk;
511411236SStephen.Hanson@Sun.COM 	int fma_dma_flag;
51156624Sgl147354 
51166624Sgl147354 	/* Only register with IO Fault Services if we have some capability */
51176624Sgl147354 	if (igb->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
51186624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
51196624Sgl147354 	} else {
51206624Sgl147354 		igb_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
51216624Sgl147354 	}
51226624Sgl147354 
51236624Sgl147354 	if (igb->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
51246624Sgl147354 		fma_dma_flag = 1;
51256624Sgl147354 	} else {
51266624Sgl147354 		fma_dma_flag = 0;
51276624Sgl147354 	}
51286624Sgl147354 
512911236SStephen.Hanson@Sun.COM 	(void) igb_set_fma_flags(fma_dma_flag);
51306624Sgl147354 
51316624Sgl147354 	if (igb->fm_capabilities) {
51326624Sgl147354 
51336624Sgl147354 		/* Register capabilities with IO Fault Services */
51346624Sgl147354 		ddi_fm_init(igb->dip, &igb->fm_capabilities, &iblk);
51356624Sgl147354 
51366624Sgl147354 		/*
51376624Sgl147354 		 * Initialize pci ereport capabilities if ereport capable
51386624Sgl147354 		 */
51396624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
51406624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
51416624Sgl147354 			pci_ereport_setup(igb->dip);
51426624Sgl147354 
51436624Sgl147354 		/*
51446624Sgl147354 		 * Register error callback if error callback capable
51456624Sgl147354 		 */
51466624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
51476624Sgl147354 			ddi_fm_handler_register(igb->dip,
51486624Sgl147354 			    igb_fm_error_cb, (void*) igb);
51496624Sgl147354 	}
51506624Sgl147354 }
51516624Sgl147354 
51526624Sgl147354 static void
igb_fm_fini(igb_t * igb)51536624Sgl147354 igb_fm_fini(igb_t *igb)
51546624Sgl147354 {
51556624Sgl147354 	/* Only unregister FMA capabilities if we registered some */
51566624Sgl147354 	if (igb->fm_capabilities) {
51576624Sgl147354 
51586624Sgl147354 		/*
51596624Sgl147354 		 * Release any resources allocated by pci_ereport_setup()
51606624Sgl147354 		 */
51616624Sgl147354 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
51626624Sgl147354 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
51636624Sgl147354 			pci_ereport_teardown(igb->dip);
51646624Sgl147354 
51656624Sgl147354 		/*
51666624Sgl147354 		 * Un-register error callback if error callback capable
51676624Sgl147354 		 */
51686624Sgl147354 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
51696624Sgl147354 			ddi_fm_handler_unregister(igb->dip);
51706624Sgl147354 
51716624Sgl147354 		/* Unregister from IO Fault Services */
51726624Sgl147354 		ddi_fm_fini(igb->dip);
51736624Sgl147354 	}
51746624Sgl147354 }
51756624Sgl147354 
51766624Sgl147354 void
igb_fm_ereport(igb_t * igb,char * detail)51776624Sgl147354 igb_fm_ereport(igb_t *igb, char *detail)
51786624Sgl147354 {
51796624Sgl147354 	uint64_t ena;
51806624Sgl147354 	char buf[FM_MAX_CLASS];
51816624Sgl147354 
51826624Sgl147354 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
51836624Sgl147354 	ena = fm_ena_generate(0, FM_ENA_FMT1);
51846624Sgl147354 	if (DDI_FM_EREPORT_CAP(igb->fm_capabilities)) {
51856624Sgl147354 		ddi_fm_ereport_post(igb->dip, buf, ena, DDI_NOSLEEP,
51866624Sgl147354 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
51876624Sgl147354 	}
51886624Sgl147354 }
5189