xref: /onnv-gate/usr/src/uts/common/io/igb/igb_main.c (revision 5779:e875a8701bfc)
1*5779Sxy150489 /*
2*5779Sxy150489  * CDDL HEADER START
3*5779Sxy150489  *
4*5779Sxy150489  * Copyright(c) 2007-2008 Intel Corporation. All rights reserved.
5*5779Sxy150489  * The contents of this file are subject to the terms of the
6*5779Sxy150489  * Common Development and Distribution License (the "License").
7*5779Sxy150489  * You may not use this file except in compliance with the License.
8*5779Sxy150489  *
9*5779Sxy150489  * You can obtain a copy of the license at:
10*5779Sxy150489  *	http://www.opensolaris.org/os/licensing.
11*5779Sxy150489  * See the License for the specific language governing permissions
12*5779Sxy150489  * and limitations under the License.
13*5779Sxy150489  *
14*5779Sxy150489  * When using or redistributing this file, you may do so under the
15*5779Sxy150489  * License only. No other modification of this header is permitted.
16*5779Sxy150489  *
17*5779Sxy150489  * If applicable, add the following below this CDDL HEADER, with the
18*5779Sxy150489  * fields enclosed by brackets "[]" replaced with your own identifying
19*5779Sxy150489  * information: Portions Copyright [yyyy] [name of copyright owner]
20*5779Sxy150489  *
21*5779Sxy150489  * CDDL HEADER END
22*5779Sxy150489  */
23*5779Sxy150489 
24*5779Sxy150489 /*
25*5779Sxy150489  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26*5779Sxy150489  * Use is subject to license terms of the CDDL.
27*5779Sxy150489  */
28*5779Sxy150489 
29*5779Sxy150489 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*5779Sxy150489 
31*5779Sxy150489 #include "igb_sw.h"
32*5779Sxy150489 
33*5779Sxy150489 static char ident[] = "Intel 1Gb Ethernet 1.1.0";
34*5779Sxy150489 
35*5779Sxy150489 /*
36*5779Sxy150489  * Local function protoypes
37*5779Sxy150489  */
38*5779Sxy150489 static int igb_register_mac(igb_t *);
39*5779Sxy150489 static int igb_identify_hardware(igb_t *);
40*5779Sxy150489 static int igb_regs_map(igb_t *);
41*5779Sxy150489 static void igb_init_properties(igb_t *);
42*5779Sxy150489 static int igb_init_driver_settings(igb_t *);
43*5779Sxy150489 static void igb_init_locks(igb_t *);
44*5779Sxy150489 static void igb_destroy_locks(igb_t *);
45*5779Sxy150489 static int igb_init(igb_t *);
46*5779Sxy150489 static int igb_chip_start(igb_t *);
47*5779Sxy150489 static void igb_chip_stop(igb_t *);
48*5779Sxy150489 static int igb_reset(igb_t *);
49*5779Sxy150489 static void igb_tx_clean(igb_t *);
50*5779Sxy150489 static boolean_t igb_tx_drain(igb_t *);
51*5779Sxy150489 static boolean_t igb_rx_drain(igb_t *);
52*5779Sxy150489 static int igb_alloc_rings(igb_t *);
53*5779Sxy150489 static int igb_init_rings(igb_t *);
54*5779Sxy150489 static void igb_free_rings(igb_t *);
55*5779Sxy150489 static void igb_fini_rings(igb_t *);
56*5779Sxy150489 static void igb_setup_rings(igb_t *);
57*5779Sxy150489 static void igb_setup_rx(igb_t *);
58*5779Sxy150489 static void igb_setup_tx(igb_t *);
59*5779Sxy150489 static void igb_setup_rx_ring(igb_rx_ring_t *);
60*5779Sxy150489 static void igb_setup_tx_ring(igb_tx_ring_t *);
61*5779Sxy150489 static void igb_setup_rss(igb_t *);
62*5779Sxy150489 static void igb_init_unicst(igb_t *);
63*5779Sxy150489 static void igb_setup_multicst(igb_t *);
64*5779Sxy150489 static void igb_get_phy_state(igb_t *);
65*5779Sxy150489 static void igb_get_conf(igb_t *);
66*5779Sxy150489 static int igb_get_prop(igb_t *, char *, int, int, int);
67*5779Sxy150489 static boolean_t igb_is_link_up(igb_t *);
68*5779Sxy150489 static boolean_t igb_link_check(igb_t *);
69*5779Sxy150489 static void igb_local_timer(void *);
70*5779Sxy150489 static void igb_arm_watchdog_timer(igb_t *);
71*5779Sxy150489 static void igb_start_watchdog_timer(igb_t *);
72*5779Sxy150489 static void igb_restart_watchdog_timer(igb_t *);
73*5779Sxy150489 static void igb_stop_watchdog_timer(igb_t *);
74*5779Sxy150489 static void igb_disable_adapter_interrupts(igb_t *);
75*5779Sxy150489 static void igb_enable_adapter_interrupts(igb_t *);
76*5779Sxy150489 static boolean_t is_valid_mac_addr(uint8_t *);
77*5779Sxy150489 static boolean_t igb_stall_check(igb_t *);
78*5779Sxy150489 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
79*5779Sxy150489 static void igb_set_external_loopback(igb_t *);
80*5779Sxy150489 static void igb_set_internal_mac_loopback(igb_t *);
81*5779Sxy150489 static void igb_set_internal_phy_loopback(igb_t *);
82*5779Sxy150489 static void igb_set_internal_serdes_loopback(igb_t *);
83*5779Sxy150489 static boolean_t igb_find_mac_address(igb_t *);
84*5779Sxy150489 static int igb_alloc_intrs(igb_t *);
85*5779Sxy150489 static int igb_alloc_intrs_msix(igb_t *);
86*5779Sxy150489 static int igb_alloc_intrs_msi(igb_t *);
87*5779Sxy150489 static int igb_alloc_intrs_legacy(igb_t *);
88*5779Sxy150489 static int igb_add_intr_handlers(igb_t *);
89*5779Sxy150489 static void igb_rem_intr_handlers(igb_t *);
90*5779Sxy150489 static void igb_rem_intrs(igb_t *);
91*5779Sxy150489 static int igb_enable_intrs(igb_t *);
92*5779Sxy150489 static int igb_disable_intrs(igb_t *);
93*5779Sxy150489 static void igb_setup_adapter_msix(igb_t *);
94*5779Sxy150489 static uint_t igb_intr_legacy(void *, void *);
95*5779Sxy150489 static uint_t igb_intr_msi(void *, void *);
96*5779Sxy150489 static uint_t igb_intr_rx(void *, void *);
97*5779Sxy150489 static uint_t igb_intr_tx_other(void *, void *);
98*5779Sxy150489 static void igb_intr_rx_work(igb_rx_ring_t *);
99*5779Sxy150489 static void igb_intr_tx_work(igb_tx_ring_t *);
100*5779Sxy150489 static void igb_intr_other_work(igb_t *);
101*5779Sxy150489 static void igb_get_driver_control(struct e1000_hw *);
102*5779Sxy150489 static void igb_release_driver_control(struct e1000_hw *);
103*5779Sxy150489 
104*5779Sxy150489 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
105*5779Sxy150489 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
106*5779Sxy150489 static int igb_resume(dev_info_t *);
107*5779Sxy150489 static int igb_suspend(dev_info_t *);
108*5779Sxy150489 static void igb_unconfigure(dev_info_t *, igb_t *);
109*5779Sxy150489 
110*5779Sxy150489 static struct cb_ops igb_cb_ops = {
111*5779Sxy150489 	nulldev,		/* cb_open */
112*5779Sxy150489 	nulldev,		/* cb_close */
113*5779Sxy150489 	nodev,			/* cb_strategy */
114*5779Sxy150489 	nodev,			/* cb_print */
115*5779Sxy150489 	nodev,			/* cb_dump */
116*5779Sxy150489 	nodev,			/* cb_read */
117*5779Sxy150489 	nodev,			/* cb_write */
118*5779Sxy150489 	nodev,			/* cb_ioctl */
119*5779Sxy150489 	nodev,			/* cb_devmap */
120*5779Sxy150489 	nodev,			/* cb_mmap */
121*5779Sxy150489 	nodev,			/* cb_segmap */
122*5779Sxy150489 	nochpoll,		/* cb_chpoll */
123*5779Sxy150489 	ddi_prop_op,		/* cb_prop_op */
124*5779Sxy150489 	NULL,			/* cb_stream */
125*5779Sxy150489 	D_MP | D_HOTPLUG,	/* cb_flag */
126*5779Sxy150489 	CB_REV,			/* cb_rev */
127*5779Sxy150489 	nodev,			/* cb_aread */
128*5779Sxy150489 	nodev			/* cb_awrite */
129*5779Sxy150489 };
130*5779Sxy150489 
131*5779Sxy150489 static struct dev_ops igb_dev_ops = {
132*5779Sxy150489 	DEVO_REV,		/* devo_rev */
133*5779Sxy150489 	0,			/* devo_refcnt */
134*5779Sxy150489 	NULL,			/* devo_getinfo */
135*5779Sxy150489 	nulldev,		/* devo_identify */
136*5779Sxy150489 	nulldev,		/* devo_probe */
137*5779Sxy150489 	igb_attach,		/* devo_attach */
138*5779Sxy150489 	igb_detach,		/* devo_detach */
139*5779Sxy150489 	nodev,			/* devo_reset */
140*5779Sxy150489 	&igb_cb_ops,		/* devo_cb_ops */
141*5779Sxy150489 	NULL,			/* devo_bus_ops */
142*5779Sxy150489 	ddi_power		/* devo_power */
143*5779Sxy150489 };
144*5779Sxy150489 
145*5779Sxy150489 static struct modldrv igb_modldrv = {
146*5779Sxy150489 	&mod_driverops,		/* Type of module.  This one is a driver */
147*5779Sxy150489 	ident,			/* Discription string */
148*5779Sxy150489 	&igb_dev_ops,		/* driver ops */
149*5779Sxy150489 };
150*5779Sxy150489 
151*5779Sxy150489 static struct modlinkage igb_modlinkage = {
152*5779Sxy150489 	MODREV_1, &igb_modldrv, NULL
153*5779Sxy150489 };
154*5779Sxy150489 
155*5779Sxy150489 /* Access attributes for register mapping */
156*5779Sxy150489 ddi_device_acc_attr_t igb_regs_acc_attr = {
157*5779Sxy150489 	DDI_DEVICE_ATTR_V0,
158*5779Sxy150489 	DDI_STRUCTURE_LE_ACC,
159*5779Sxy150489 	DDI_STRICTORDER_ACC,
160*5779Sxy150489 };
161*5779Sxy150489 
162*5779Sxy150489 #define	IGB_M_CALLBACK_FLAGS	(MC_IOCTL | MC_GETCAPAB)
163*5779Sxy150489 
164*5779Sxy150489 static mac_callbacks_t igb_m_callbacks = {
165*5779Sxy150489 	IGB_M_CALLBACK_FLAGS,
166*5779Sxy150489 	igb_m_stat,
167*5779Sxy150489 	igb_m_start,
168*5779Sxy150489 	igb_m_stop,
169*5779Sxy150489 	igb_m_promisc,
170*5779Sxy150489 	igb_m_multicst,
171*5779Sxy150489 	igb_m_unicst,
172*5779Sxy150489 	igb_m_tx,
173*5779Sxy150489 	NULL,
174*5779Sxy150489 	igb_m_ioctl,
175*5779Sxy150489 	igb_m_getcapab
176*5779Sxy150489 };
177*5779Sxy150489 
178*5779Sxy150489 
179*5779Sxy150489 /*
180*5779Sxy150489  * Module Initialization Functions
181*5779Sxy150489  */
182*5779Sxy150489 
183*5779Sxy150489 int
184*5779Sxy150489 _init(void)
185*5779Sxy150489 {
186*5779Sxy150489 	int status;
187*5779Sxy150489 
188*5779Sxy150489 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
189*5779Sxy150489 
190*5779Sxy150489 	status = mod_install(&igb_modlinkage);
191*5779Sxy150489 
192*5779Sxy150489 	if (status != DDI_SUCCESS) {
193*5779Sxy150489 		mac_fini_ops(&igb_dev_ops);
194*5779Sxy150489 	}
195*5779Sxy150489 
196*5779Sxy150489 	return (status);
197*5779Sxy150489 }
198*5779Sxy150489 
199*5779Sxy150489 int
200*5779Sxy150489 _fini(void)
201*5779Sxy150489 {
202*5779Sxy150489 	int status;
203*5779Sxy150489 
204*5779Sxy150489 	status = mod_remove(&igb_modlinkage);
205*5779Sxy150489 
206*5779Sxy150489 	if (status == DDI_SUCCESS) {
207*5779Sxy150489 		mac_fini_ops(&igb_dev_ops);
208*5779Sxy150489 	}
209*5779Sxy150489 
210*5779Sxy150489 	return (status);
211*5779Sxy150489 
212*5779Sxy150489 }
213*5779Sxy150489 
214*5779Sxy150489 int
215*5779Sxy150489 _info(struct modinfo *modinfop)
216*5779Sxy150489 {
217*5779Sxy150489 	int status;
218*5779Sxy150489 
219*5779Sxy150489 	status = mod_info(&igb_modlinkage, modinfop);
220*5779Sxy150489 
221*5779Sxy150489 	return (status);
222*5779Sxy150489 }
223*5779Sxy150489 
224*5779Sxy150489 /*
225*5779Sxy150489  * igb_attach - driver attach
226*5779Sxy150489  *
227*5779Sxy150489  * This function is the device specific initialization entry
228*5779Sxy150489  * point. This entry point is required and must be written.
229*5779Sxy150489  * The DDI_ATTACH command must be provided in the attach entry
230*5779Sxy150489  * point. When attach() is called with cmd set to DDI_ATTACH,
231*5779Sxy150489  * all normal kernel services (such as kmem_alloc(9F)) are
232*5779Sxy150489  * available for use by the driver.
233*5779Sxy150489  *
234*5779Sxy150489  * The attach() function will be called once for each instance
235*5779Sxy150489  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
236*5779Sxy150489  * Until attach() succeeds, the only driver entry points which
237*5779Sxy150489  * may be called are open(9E) and getinfo(9E).
238*5779Sxy150489  */
239*5779Sxy150489 static int
240*5779Sxy150489 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
241*5779Sxy150489 {
242*5779Sxy150489 	igb_t *igb;
243*5779Sxy150489 	struct igb_osdep *osdep;
244*5779Sxy150489 	struct e1000_hw *hw;
245*5779Sxy150489 	int instance;
246*5779Sxy150489 
247*5779Sxy150489 	/*
248*5779Sxy150489 	 * Check the command and perform corresponding operations
249*5779Sxy150489 	 */
250*5779Sxy150489 	switch (cmd) {
251*5779Sxy150489 	default:
252*5779Sxy150489 		return (DDI_FAILURE);
253*5779Sxy150489 
254*5779Sxy150489 	case DDI_RESUME:
255*5779Sxy150489 		return (igb_resume(devinfo));
256*5779Sxy150489 
257*5779Sxy150489 	case DDI_ATTACH:
258*5779Sxy150489 		break;
259*5779Sxy150489 	}
260*5779Sxy150489 
261*5779Sxy150489 	/* Get the device instance */
262*5779Sxy150489 	instance = ddi_get_instance(devinfo);
263*5779Sxy150489 
264*5779Sxy150489 	/* Allocate memory for the instance data structure */
265*5779Sxy150489 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
266*5779Sxy150489 
267*5779Sxy150489 	igb->dip = devinfo;
268*5779Sxy150489 	igb->instance = instance;
269*5779Sxy150489 
270*5779Sxy150489 	hw = &igb->hw;
271*5779Sxy150489 	osdep = &igb->osdep;
272*5779Sxy150489 	hw->back = osdep;
273*5779Sxy150489 	osdep->igb = igb;
274*5779Sxy150489 
275*5779Sxy150489 	/* Attach the instance pointer to the dev_info data structure */
276*5779Sxy150489 	ddi_set_driver_private(devinfo, igb);
277*5779Sxy150489 
278*5779Sxy150489 	/*
279*5779Sxy150489 	 * Map PCI config space registers
280*5779Sxy150489 	 */
281*5779Sxy150489 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
282*5779Sxy150489 		igb_error(igb, "Failed to map PCI configurations");
283*5779Sxy150489 		goto attach_fail;
284*5779Sxy150489 	}
285*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
286*5779Sxy150489 
287*5779Sxy150489 	/*
288*5779Sxy150489 	 * Identify the chipset family
289*5779Sxy150489 	 */
290*5779Sxy150489 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
291*5779Sxy150489 		igb_error(igb, "Failed to identify hardware");
292*5779Sxy150489 		goto attach_fail;
293*5779Sxy150489 	}
294*5779Sxy150489 
295*5779Sxy150489 	/*
296*5779Sxy150489 	 * Map device registers
297*5779Sxy150489 	 */
298*5779Sxy150489 	if (igb_regs_map(igb) != IGB_SUCCESS) {
299*5779Sxy150489 		igb_error(igb, "Failed to map device registers");
300*5779Sxy150489 		goto attach_fail;
301*5779Sxy150489 	}
302*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
303*5779Sxy150489 
304*5779Sxy150489 	/*
305*5779Sxy150489 	 * Initialize driver parameters
306*5779Sxy150489 	 */
307*5779Sxy150489 	igb_init_properties(igb);
308*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
309*5779Sxy150489 
310*5779Sxy150489 	/*
311*5779Sxy150489 	 * Allocate interrupts
312*5779Sxy150489 	 */
313*5779Sxy150489 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
314*5779Sxy150489 		igb_error(igb, "Failed to allocate interrupts");
315*5779Sxy150489 		goto attach_fail;
316*5779Sxy150489 	}
317*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
318*5779Sxy150489 
319*5779Sxy150489 	/*
320*5779Sxy150489 	 * Allocate rx/tx rings based on the ring numbers.
321*5779Sxy150489 	 * The actual numbers of rx/tx rings are decided by the number of
322*5779Sxy150489 	 * allocated interrupt vectors, so we should allocate the rings after
323*5779Sxy150489 	 * interrupts are allocated.
324*5779Sxy150489 	 */
325*5779Sxy150489 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
326*5779Sxy150489 		igb_error(igb, "Failed to allocate rx and tx rings");
327*5779Sxy150489 		goto attach_fail;
328*5779Sxy150489 	}
329*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
330*5779Sxy150489 
331*5779Sxy150489 	/*
332*5779Sxy150489 	 * Add interrupt handlers
333*5779Sxy150489 	 */
334*5779Sxy150489 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
335*5779Sxy150489 		igb_error(igb, "Failed to add interrupt handlers");
336*5779Sxy150489 		goto attach_fail;
337*5779Sxy150489 	}
338*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
339*5779Sxy150489 
340*5779Sxy150489 	/*
341*5779Sxy150489 	 * Initialize driver parameters
342*5779Sxy150489 	 */
343*5779Sxy150489 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
344*5779Sxy150489 		igb_error(igb, "Failed to initialize driver settings");
345*5779Sxy150489 		goto attach_fail;
346*5779Sxy150489 	}
347*5779Sxy150489 
348*5779Sxy150489 	/*
349*5779Sxy150489 	 * Initialize mutexes for this device.
350*5779Sxy150489 	 * Do this before enabling the interrupt handler and
351*5779Sxy150489 	 * register the softint to avoid the condition where
352*5779Sxy150489 	 * interrupt handler can try using uninitialized mutex
353*5779Sxy150489 	 */
354*5779Sxy150489 	igb_init_locks(igb);
355*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
356*5779Sxy150489 
357*5779Sxy150489 	/*
358*5779Sxy150489 	 * Initialize chipset hardware
359*5779Sxy150489 	 */
360*5779Sxy150489 	if (igb_init(igb) != IGB_SUCCESS) {
361*5779Sxy150489 		igb_error(igb, "Failed to initialize adapter");
362*5779Sxy150489 		goto attach_fail;
363*5779Sxy150489 	}
364*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT;
365*5779Sxy150489 
366*5779Sxy150489 	/*
367*5779Sxy150489 	 * Initialize DMA and hardware settings for rx/tx rings
368*5779Sxy150489 	 */
369*5779Sxy150489 	if (igb_init_rings(igb) != IGB_SUCCESS) {
370*5779Sxy150489 		igb_error(igb, "Failed to initialize rings");
371*5779Sxy150489 		goto attach_fail;
372*5779Sxy150489 	}
373*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_INIT_RINGS;
374*5779Sxy150489 
375*5779Sxy150489 	/*
376*5779Sxy150489 	 * Initialize statistics
377*5779Sxy150489 	 */
378*5779Sxy150489 	if (igb_init_stats(igb) != IGB_SUCCESS) {
379*5779Sxy150489 		igb_error(igb, "Failed to initialize statistics");
380*5779Sxy150489 		goto attach_fail;
381*5779Sxy150489 	}
382*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_STATS;
383*5779Sxy150489 
384*5779Sxy150489 	/*
385*5779Sxy150489 	 * Initialize NDD parameters
386*5779Sxy150489 	 */
387*5779Sxy150489 	if (igb_nd_init(igb) != IGB_SUCCESS) {
388*5779Sxy150489 		igb_error(igb, "Failed to initialize ndd");
389*5779Sxy150489 		goto attach_fail;
390*5779Sxy150489 	}
391*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_NDD;
392*5779Sxy150489 
393*5779Sxy150489 	/*
394*5779Sxy150489 	 * Register the driver to the MAC
395*5779Sxy150489 	 */
396*5779Sxy150489 	if (igb_register_mac(igb) != IGB_SUCCESS) {
397*5779Sxy150489 		igb_error(igb, "Failed to register MAC");
398*5779Sxy150489 		goto attach_fail;
399*5779Sxy150489 	}
400*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_MAC;
401*5779Sxy150489 
402*5779Sxy150489 	/*
403*5779Sxy150489 	 * Now that mutex locks are initialized, and the chip is also
404*5779Sxy150489 	 * initialized, enable interrupts.
405*5779Sxy150489 	 */
406*5779Sxy150489 	if (igb_enable_intrs(igb) != IGB_SUCCESS) {
407*5779Sxy150489 		igb_error(igb, "Failed to enable DDI interrupts");
408*5779Sxy150489 		goto attach_fail;
409*5779Sxy150489 	}
410*5779Sxy150489 	igb->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
411*5779Sxy150489 
412*5779Sxy150489 	igb->igb_state |= IGB_INITIALIZED;
413*5779Sxy150489 
414*5779Sxy150489 	return (DDI_SUCCESS);
415*5779Sxy150489 
416*5779Sxy150489 attach_fail:
417*5779Sxy150489 	igb_unconfigure(devinfo, igb);
418*5779Sxy150489 	return (DDI_FAILURE);
419*5779Sxy150489 }
420*5779Sxy150489 
421*5779Sxy150489 /*
422*5779Sxy150489  * igb_detach - driver detach
423*5779Sxy150489  *
424*5779Sxy150489  * The detach() function is the complement of the attach routine.
425*5779Sxy150489  * If cmd is set to DDI_DETACH, detach() is used to remove  the
426*5779Sxy150489  * state  associated  with  a  given  instance of a device node
427*5779Sxy150489  * prior to the removal of that instance from the system.
428*5779Sxy150489  *
429*5779Sxy150489  * The detach() function will be called once for each  instance
430*5779Sxy150489  * of the device for which there has been a successful attach()
431*5779Sxy150489  * once there are no longer  any  opens  on  the  device.
432*5779Sxy150489  *
433*5779Sxy150489  * Interrupts routine are disabled, All memory allocated by this
434*5779Sxy150489  * driver are freed.
435*5779Sxy150489  */
436*5779Sxy150489 static int
437*5779Sxy150489 igb_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
438*5779Sxy150489 {
439*5779Sxy150489 	igb_t *igb;
440*5779Sxy150489 
441*5779Sxy150489 	/*
442*5779Sxy150489 	 * Check detach command
443*5779Sxy150489 	 */
444*5779Sxy150489 	switch (cmd) {
445*5779Sxy150489 	default:
446*5779Sxy150489 		return (DDI_FAILURE);
447*5779Sxy150489 
448*5779Sxy150489 	case DDI_SUSPEND:
449*5779Sxy150489 		return (igb_suspend(devinfo));
450*5779Sxy150489 
451*5779Sxy150489 	case DDI_DETACH:
452*5779Sxy150489 		break;
453*5779Sxy150489 	}
454*5779Sxy150489 
455*5779Sxy150489 
456*5779Sxy150489 	/*
457*5779Sxy150489 	 * Get the pointer to the driver private data structure
458*5779Sxy150489 	 */
459*5779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
460*5779Sxy150489 	if (igb == NULL)
461*5779Sxy150489 		return (DDI_FAILURE);
462*5779Sxy150489 
463*5779Sxy150489 	/*
464*5779Sxy150489 	 * Unregister MAC. If failed, we have to fail the detach
465*5779Sxy150489 	 */
466*5779Sxy150489 	if (mac_unregister(igb->mac_hdl) != 0) {
467*5779Sxy150489 		igb_error(igb, "Failed to unregister MAC");
468*5779Sxy150489 		return (DDI_FAILURE);
469*5779Sxy150489 	}
470*5779Sxy150489 	igb->attach_progress &= ~ATTACH_PROGRESS_MAC;
471*5779Sxy150489 
472*5779Sxy150489 	/*
473*5779Sxy150489 	 * If the device is still running, it needs to be stopped first.
474*5779Sxy150489 	 * This check is necessary because under some specific circumstances,
475*5779Sxy150489 	 * the detach routine can be called without stopping the interface
476*5779Sxy150489 	 * first.
477*5779Sxy150489 	 */
478*5779Sxy150489 	mutex_enter(&igb->gen_lock);
479*5779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
480*5779Sxy150489 		igb->igb_state &= ~IGB_STARTED;
481*5779Sxy150489 		igb_stop(igb);
482*5779Sxy150489 		mutex_exit(&igb->gen_lock);
483*5779Sxy150489 		/* Disable and stop the watchdog timer */
484*5779Sxy150489 		igb_disable_watchdog_timer(igb);
485*5779Sxy150489 	} else
486*5779Sxy150489 		mutex_exit(&igb->gen_lock);
487*5779Sxy150489 
488*5779Sxy150489 	/*
489*5779Sxy150489 	 * Check if there are still rx buffers held by the upper layer.
490*5779Sxy150489 	 * If so, fail the detach.
491*5779Sxy150489 	 */
492*5779Sxy150489 	if (!igb_rx_drain(igb))
493*5779Sxy150489 		return (DDI_FAILURE);
494*5779Sxy150489 
495*5779Sxy150489 	/*
496*5779Sxy150489 	 * Do the remaining unconfigure routines
497*5779Sxy150489 	 */
498*5779Sxy150489 	igb_unconfigure(devinfo, igb);
499*5779Sxy150489 
500*5779Sxy150489 	return (DDI_SUCCESS);
501*5779Sxy150489 }
502*5779Sxy150489 
503*5779Sxy150489 static void
504*5779Sxy150489 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
505*5779Sxy150489 {
506*5779Sxy150489 	/*
507*5779Sxy150489 	 * Disable interrupt
508*5779Sxy150489 	 */
509*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
510*5779Sxy150489 		(void) igb_disable_intrs(igb);
511*5779Sxy150489 	}
512*5779Sxy150489 
513*5779Sxy150489 	/*
514*5779Sxy150489 	 * Unregister MAC
515*5779Sxy150489 	 */
516*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
517*5779Sxy150489 		(void) mac_unregister(igb->mac_hdl);
518*5779Sxy150489 	}
519*5779Sxy150489 
520*5779Sxy150489 	/*
521*5779Sxy150489 	 * Free ndd parameters
522*5779Sxy150489 	 */
523*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_NDD) {
524*5779Sxy150489 		igb_nd_cleanup(igb);
525*5779Sxy150489 	}
526*5779Sxy150489 
527*5779Sxy150489 	/*
528*5779Sxy150489 	 * Free statistics
529*5779Sxy150489 	 */
530*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
531*5779Sxy150489 		kstat_delete((kstat_t *)igb->igb_ks);
532*5779Sxy150489 	}
533*5779Sxy150489 
534*5779Sxy150489 	/*
535*5779Sxy150489 	 * Remove interrupt handlers
536*5779Sxy150489 	 */
537*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
538*5779Sxy150489 		igb_rem_intr_handlers(igb);
539*5779Sxy150489 	}
540*5779Sxy150489 
541*5779Sxy150489 	/*
542*5779Sxy150489 	 * Remove interrupts
543*5779Sxy150489 	 */
544*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
545*5779Sxy150489 		igb_rem_intrs(igb);
546*5779Sxy150489 	}
547*5779Sxy150489 
548*5779Sxy150489 	/*
549*5779Sxy150489 	 * Remove driver properties
550*5779Sxy150489 	 */
551*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
552*5779Sxy150489 		(void) ddi_prop_remove_all(devinfo);
553*5779Sxy150489 	}
554*5779Sxy150489 
555*5779Sxy150489 	/*
556*5779Sxy150489 	 * Release the DMA resources of rx/tx rings
557*5779Sxy150489 	 */
558*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_RINGS) {
559*5779Sxy150489 		igb_fini_rings(igb);
560*5779Sxy150489 	}
561*5779Sxy150489 
562*5779Sxy150489 	/*
563*5779Sxy150489 	 * Stop the chipset
564*5779Sxy150489 	 */
565*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_INIT) {
566*5779Sxy150489 		mutex_enter(&igb->gen_lock);
567*5779Sxy150489 		igb_chip_stop(igb);
568*5779Sxy150489 		mutex_exit(&igb->gen_lock);
569*5779Sxy150489 	}
570*5779Sxy150489 
571*5779Sxy150489 	/*
572*5779Sxy150489 	 * Free register handle
573*5779Sxy150489 	 */
574*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
575*5779Sxy150489 		if (igb->osdep.reg_handle != NULL)
576*5779Sxy150489 			ddi_regs_map_free(&igb->osdep.reg_handle);
577*5779Sxy150489 	}
578*5779Sxy150489 
579*5779Sxy150489 	/*
580*5779Sxy150489 	 * Free PCI config handle
581*5779Sxy150489 	 */
582*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
583*5779Sxy150489 		if (igb->osdep.cfg_handle != NULL)
584*5779Sxy150489 			pci_config_teardown(&igb->osdep.cfg_handle);
585*5779Sxy150489 	}
586*5779Sxy150489 
587*5779Sxy150489 	/*
588*5779Sxy150489 	 * Free locks
589*5779Sxy150489 	 */
590*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
591*5779Sxy150489 		igb_destroy_locks(igb);
592*5779Sxy150489 	}
593*5779Sxy150489 
594*5779Sxy150489 	/*
595*5779Sxy150489 	 * Free the rx/tx rings
596*5779Sxy150489 	 */
597*5779Sxy150489 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
598*5779Sxy150489 		igb_free_rings(igb);
599*5779Sxy150489 	}
600*5779Sxy150489 
601*5779Sxy150489 	/*
602*5779Sxy150489 	 * Free device specific structure
603*5779Sxy150489 	 */
604*5779Sxy150489 	e1000_remove_device(&igb->hw);
605*5779Sxy150489 
606*5779Sxy150489 	/*
607*5779Sxy150489 	 * Free the driver data structure
608*5779Sxy150489 	 */
609*5779Sxy150489 	kmem_free(igb, sizeof (igb_t));
610*5779Sxy150489 
611*5779Sxy150489 	ddi_set_driver_private(devinfo, NULL);
612*5779Sxy150489 }
613*5779Sxy150489 
614*5779Sxy150489 /*
615*5779Sxy150489  * igb_register_mac - Register the driver and its function pointers with
616*5779Sxy150489  * the GLD interface
617*5779Sxy150489  */
618*5779Sxy150489 static int
619*5779Sxy150489 igb_register_mac(igb_t *igb)
620*5779Sxy150489 {
621*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
622*5779Sxy150489 	mac_register_t *mac;
623*5779Sxy150489 	int status;
624*5779Sxy150489 
625*5779Sxy150489 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
626*5779Sxy150489 		return (IGB_FAILURE);
627*5779Sxy150489 
628*5779Sxy150489 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
629*5779Sxy150489 	mac->m_driver = igb;
630*5779Sxy150489 	mac->m_dip = igb->dip;
631*5779Sxy150489 	mac->m_src_addr = hw->mac.addr;
632*5779Sxy150489 	mac->m_callbacks = &igb_m_callbacks;
633*5779Sxy150489 	mac->m_min_sdu = 0;
634*5779Sxy150489 	mac->m_max_sdu = igb->max_frame_size -
635*5779Sxy150489 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
636*5779Sxy150489 
637*5779Sxy150489 	status = mac_register(mac, &igb->mac_hdl);
638*5779Sxy150489 
639*5779Sxy150489 	mac_free(mac);
640*5779Sxy150489 
641*5779Sxy150489 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
642*5779Sxy150489 }
643*5779Sxy150489 
644*5779Sxy150489 /*
645*5779Sxy150489  * igb_identify_hardware - Identify the type of the chipset
646*5779Sxy150489  */
647*5779Sxy150489 static int
648*5779Sxy150489 igb_identify_hardware(igb_t *igb)
649*5779Sxy150489 {
650*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
651*5779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
652*5779Sxy150489 
653*5779Sxy150489 	/*
654*5779Sxy150489 	 * Get the device id
655*5779Sxy150489 	 */
656*5779Sxy150489 	hw->vendor_id =
657*5779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
658*5779Sxy150489 	hw->device_id =
659*5779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
660*5779Sxy150489 	hw->revision_id =
661*5779Sxy150489 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
662*5779Sxy150489 	hw->subsystem_device_id =
663*5779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
664*5779Sxy150489 	hw->subsystem_vendor_id =
665*5779Sxy150489 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
666*5779Sxy150489 
667*5779Sxy150489 	/*
668*5779Sxy150489 	 * Set the mac type of the adapter based on the device id
669*5779Sxy150489 	 */
670*5779Sxy150489 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
671*5779Sxy150489 		return (IGB_FAILURE);
672*5779Sxy150489 	}
673*5779Sxy150489 
674*5779Sxy150489 	return (IGB_SUCCESS);
675*5779Sxy150489 }
676*5779Sxy150489 
677*5779Sxy150489 /*
678*5779Sxy150489  * igb_regs_map - Map the device registers
679*5779Sxy150489  */
680*5779Sxy150489 static int
681*5779Sxy150489 igb_regs_map(igb_t *igb)
682*5779Sxy150489 {
683*5779Sxy150489 	dev_info_t *devinfo = igb->dip;
684*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
685*5779Sxy150489 	struct igb_osdep *osdep = &igb->osdep;
686*5779Sxy150489 	off_t mem_size;
687*5779Sxy150489 
688*5779Sxy150489 	/*
689*5779Sxy150489 	 * First get the size of device registers to be mapped.
690*5779Sxy150489 	 */
691*5779Sxy150489 	if (ddi_dev_regsize(devinfo, 1, &mem_size) != DDI_SUCCESS) {
692*5779Sxy150489 		return (IGB_FAILURE);
693*5779Sxy150489 	}
694*5779Sxy150489 
695*5779Sxy150489 	/*
696*5779Sxy150489 	 * Call ddi_regs_map_setup() to map registers
697*5779Sxy150489 	 */
698*5779Sxy150489 	if ((ddi_regs_map_setup(devinfo, 1,
699*5779Sxy150489 	    (caddr_t *)&hw->hw_addr, 0,
700*5779Sxy150489 	    mem_size, &igb_regs_acc_attr,
701*5779Sxy150489 	    &osdep->reg_handle)) != DDI_SUCCESS) {
702*5779Sxy150489 		return (IGB_FAILURE);
703*5779Sxy150489 	}
704*5779Sxy150489 
705*5779Sxy150489 	return (IGB_SUCCESS);
706*5779Sxy150489 }
707*5779Sxy150489 
708*5779Sxy150489 /*
709*5779Sxy150489  * igb_init_properties - Initialize driver properties
710*5779Sxy150489  */
711*5779Sxy150489 static void
712*5779Sxy150489 igb_init_properties(igb_t *igb)
713*5779Sxy150489 {
714*5779Sxy150489 	/*
715*5779Sxy150489 	 * Get conf file properties, including link settings
716*5779Sxy150489 	 * jumbo frames, ring number, descriptor number, etc.
717*5779Sxy150489 	 */
718*5779Sxy150489 	igb_get_conf(igb);
719*5779Sxy150489 }
720*5779Sxy150489 
721*5779Sxy150489 /*
722*5779Sxy150489  * igb_init_driver_settings - Initialize driver settings
723*5779Sxy150489  *
724*5779Sxy150489  * The settings include hardware function pointers, bus information,
725*5779Sxy150489  * rx/tx rings settings, link state, and any other parameters that
726*5779Sxy150489  * need to be setup during driver initialization.
727*5779Sxy150489  */
728*5779Sxy150489 static int
729*5779Sxy150489 igb_init_driver_settings(igb_t *igb)
730*5779Sxy150489 {
731*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
732*5779Sxy150489 	igb_rx_ring_t *rx_ring;
733*5779Sxy150489 	igb_tx_ring_t *tx_ring;
734*5779Sxy150489 	uint32_t rx_size;
735*5779Sxy150489 	uint32_t tx_size;
736*5779Sxy150489 	int i;
737*5779Sxy150489 
738*5779Sxy150489 	/*
739*5779Sxy150489 	 * Initialize chipset specific hardware function pointers
740*5779Sxy150489 	 */
741*5779Sxy150489 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
742*5779Sxy150489 		return (IGB_FAILURE);
743*5779Sxy150489 	}
744*5779Sxy150489 
745*5779Sxy150489 	/*
746*5779Sxy150489 	 * Get bus information
747*5779Sxy150489 	 */
748*5779Sxy150489 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
749*5779Sxy150489 		return (IGB_FAILURE);
750*5779Sxy150489 	}
751*5779Sxy150489 
752*5779Sxy150489 	/*
753*5779Sxy150489 	 * Set rx buffer size
754*5779Sxy150489 	 * The IP header alignment room is counted in the calculation.
755*5779Sxy150489 	 * The rx buffer size is in unit of 1K that is required by the
756*5779Sxy150489 	 * chipset hardware.
757*5779Sxy150489 	 */
758*5779Sxy150489 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
759*5779Sxy150489 	igb->rx_buf_size = ((rx_size >> 10) +
760*5779Sxy150489 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
761*5779Sxy150489 
762*5779Sxy150489 	/*
763*5779Sxy150489 	 * Set tx buffer size
764*5779Sxy150489 	 */
765*5779Sxy150489 	tx_size = igb->max_frame_size;
766*5779Sxy150489 	igb->tx_buf_size = ((tx_size >> 10) +
767*5779Sxy150489 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
768*5779Sxy150489 
769*5779Sxy150489 	/*
770*5779Sxy150489 	 * Initialize rx/tx rings parameters
771*5779Sxy150489 	 */
772*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
773*5779Sxy150489 		rx_ring = &igb->rx_rings[i];
774*5779Sxy150489 		rx_ring->index = i;
775*5779Sxy150489 		rx_ring->igb = igb;
776*5779Sxy150489 
777*5779Sxy150489 		rx_ring->ring_size = igb->rx_ring_size;
778*5779Sxy150489 		rx_ring->free_list_size = igb->rx_ring_size;
779*5779Sxy150489 		rx_ring->copy_thresh = igb->rx_copy_thresh;
780*5779Sxy150489 		rx_ring->limit_per_intr = igb->rx_limit_per_intr;
781*5779Sxy150489 	}
782*5779Sxy150489 
783*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
784*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
785*5779Sxy150489 		tx_ring->index = i;
786*5779Sxy150489 		tx_ring->igb = igb;
787*5779Sxy150489 		if (igb->tx_head_wb_enable)
788*5779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
789*5779Sxy150489 		else
790*5779Sxy150489 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
791*5779Sxy150489 
792*5779Sxy150489 		tx_ring->ring_size = igb->tx_ring_size;
793*5779Sxy150489 		tx_ring->free_list_size = igb->tx_ring_size +
794*5779Sxy150489 		    (igb->tx_ring_size >> 1);
795*5779Sxy150489 		tx_ring->copy_thresh = igb->tx_copy_thresh;
796*5779Sxy150489 		tx_ring->recycle_thresh = igb->tx_recycle_thresh;
797*5779Sxy150489 		tx_ring->overload_thresh = igb->tx_overload_thresh;
798*5779Sxy150489 		tx_ring->resched_thresh = igb->tx_resched_thresh;
799*5779Sxy150489 	}
800*5779Sxy150489 
801*5779Sxy150489 	/*
802*5779Sxy150489 	 * Initialize values of interrupt throttling rate
803*5779Sxy150489 	 */
804*5779Sxy150489 	for (i = 1; i < MAX_NUM_EITR; i++)
805*5779Sxy150489 		igb->intr_throttling[i] = igb->intr_throttling[0];
806*5779Sxy150489 
807*5779Sxy150489 	/*
808*5779Sxy150489 	 * The initial link state should be "unknown"
809*5779Sxy150489 	 */
810*5779Sxy150489 	igb->link_state = LINK_STATE_UNKNOWN;
811*5779Sxy150489 
812*5779Sxy150489 	return (IGB_SUCCESS);
813*5779Sxy150489 }
814*5779Sxy150489 
815*5779Sxy150489 /*
816*5779Sxy150489  * igb_init_locks - Initialize locks
817*5779Sxy150489  */
818*5779Sxy150489 static void
819*5779Sxy150489 igb_init_locks(igb_t *igb)
820*5779Sxy150489 {
821*5779Sxy150489 	igb_rx_ring_t *rx_ring;
822*5779Sxy150489 	igb_tx_ring_t *tx_ring;
823*5779Sxy150489 	int i;
824*5779Sxy150489 
825*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
826*5779Sxy150489 		rx_ring = &igb->rx_rings[i];
827*5779Sxy150489 		mutex_init(&rx_ring->rx_lock, NULL,
828*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
829*5779Sxy150489 		mutex_init(&rx_ring->recycle_lock, NULL,
830*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
831*5779Sxy150489 	}
832*5779Sxy150489 
833*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
834*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
835*5779Sxy150489 		mutex_init(&tx_ring->tx_lock, NULL,
836*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
837*5779Sxy150489 		mutex_init(&tx_ring->recycle_lock, NULL,
838*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
839*5779Sxy150489 		mutex_init(&tx_ring->tcb_head_lock, NULL,
840*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
841*5779Sxy150489 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
842*5779Sxy150489 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
843*5779Sxy150489 	}
844*5779Sxy150489 
845*5779Sxy150489 	mutex_init(&igb->gen_lock, NULL,
846*5779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
847*5779Sxy150489 
848*5779Sxy150489 	mutex_init(&igb->watchdog_lock, NULL,
849*5779Sxy150489 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
850*5779Sxy150489 }
851*5779Sxy150489 
852*5779Sxy150489 /*
853*5779Sxy150489  * igb_destroy_locks - Destroy locks
854*5779Sxy150489  */
855*5779Sxy150489 static void
856*5779Sxy150489 igb_destroy_locks(igb_t *igb)
857*5779Sxy150489 {
858*5779Sxy150489 	igb_rx_ring_t *rx_ring;
859*5779Sxy150489 	igb_tx_ring_t *tx_ring;
860*5779Sxy150489 	int i;
861*5779Sxy150489 
862*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
863*5779Sxy150489 		rx_ring = &igb->rx_rings[i];
864*5779Sxy150489 		mutex_destroy(&rx_ring->rx_lock);
865*5779Sxy150489 		mutex_destroy(&rx_ring->recycle_lock);
866*5779Sxy150489 	}
867*5779Sxy150489 
868*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
869*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
870*5779Sxy150489 		mutex_destroy(&tx_ring->tx_lock);
871*5779Sxy150489 		mutex_destroy(&tx_ring->recycle_lock);
872*5779Sxy150489 		mutex_destroy(&tx_ring->tcb_head_lock);
873*5779Sxy150489 		mutex_destroy(&tx_ring->tcb_tail_lock);
874*5779Sxy150489 	}
875*5779Sxy150489 
876*5779Sxy150489 	mutex_destroy(&igb->gen_lock);
877*5779Sxy150489 	mutex_destroy(&igb->watchdog_lock);
878*5779Sxy150489 }
879*5779Sxy150489 
880*5779Sxy150489 static int
881*5779Sxy150489 igb_resume(dev_info_t *devinfo)
882*5779Sxy150489 {
883*5779Sxy150489 	igb_t *igb;
884*5779Sxy150489 
885*5779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
886*5779Sxy150489 	if (igb == NULL)
887*5779Sxy150489 		return (DDI_FAILURE);
888*5779Sxy150489 
889*5779Sxy150489 	mutex_enter(&igb->gen_lock);
890*5779Sxy150489 
891*5779Sxy150489 	if (igb->igb_state & IGB_STARTED) {
892*5779Sxy150489 		if (igb_start(igb) != IGB_SUCCESS) {
893*5779Sxy150489 			mutex_exit(&igb->gen_lock);
894*5779Sxy150489 			return (DDI_FAILURE);
895*5779Sxy150489 		}
896*5779Sxy150489 
897*5779Sxy150489 		/*
898*5779Sxy150489 		 * Enable and start the watchdog timer
899*5779Sxy150489 		 */
900*5779Sxy150489 		igb_enable_watchdog_timer(igb);
901*5779Sxy150489 	}
902*5779Sxy150489 
903*5779Sxy150489 	igb->igb_state &= ~IGB_SUSPENDED;
904*5779Sxy150489 
905*5779Sxy150489 	mutex_exit(&igb->gen_lock);
906*5779Sxy150489 
907*5779Sxy150489 	return (DDI_SUCCESS);
908*5779Sxy150489 }
909*5779Sxy150489 
910*5779Sxy150489 static int
911*5779Sxy150489 igb_suspend(dev_info_t *devinfo)
912*5779Sxy150489 {
913*5779Sxy150489 	igb_t *igb;
914*5779Sxy150489 
915*5779Sxy150489 	igb = (igb_t *)ddi_get_driver_private(devinfo);
916*5779Sxy150489 	if (igb == NULL)
917*5779Sxy150489 		return (DDI_FAILURE);
918*5779Sxy150489 
919*5779Sxy150489 	mutex_enter(&igb->gen_lock);
920*5779Sxy150489 
921*5779Sxy150489 	igb->igb_state |= IGB_SUSPENDED;
922*5779Sxy150489 
923*5779Sxy150489 	igb_stop(igb);
924*5779Sxy150489 
925*5779Sxy150489 	mutex_exit(&igb->gen_lock);
926*5779Sxy150489 
927*5779Sxy150489 	/*
928*5779Sxy150489 	 * Disable and stop the watchdog timer
929*5779Sxy150489 	 */
930*5779Sxy150489 	igb_disable_watchdog_timer(igb);
931*5779Sxy150489 
932*5779Sxy150489 	return (DDI_SUCCESS);
933*5779Sxy150489 }
934*5779Sxy150489 
935*5779Sxy150489 /*
936*5779Sxy150489  * igb_init - Initialize the device
937*5779Sxy150489  */
938*5779Sxy150489 static int
939*5779Sxy150489 igb_init(igb_t *igb)
940*5779Sxy150489 {
941*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
942*5779Sxy150489 	uint32_t pba;
943*5779Sxy150489 	uint32_t high_water;
944*5779Sxy150489 
945*5779Sxy150489 	mutex_enter(&igb->gen_lock);
946*5779Sxy150489 
947*5779Sxy150489 	/*
948*5779Sxy150489 	 * Reset chipset to put the hardware in a known state
949*5779Sxy150489 	 * before we try to do anything with the eeprom
950*5779Sxy150489 	 */
951*5779Sxy150489 	(void) e1000_reset_hw(hw);
952*5779Sxy150489 
953*5779Sxy150489 	/*
954*5779Sxy150489 	 * NVM validation
955*5779Sxy150489 	 */
956*5779Sxy150489 	if (e1000_validate_nvm_checksum(hw) < 0) {
957*5779Sxy150489 		/*
958*5779Sxy150489 		 * Some PCI-E parts fail the first check due to
959*5779Sxy150489 		 * the link being in sleep state.  Call it again,
960*5779Sxy150489 		 * if it fails a second time its a real issue.
961*5779Sxy150489 		 */
962*5779Sxy150489 		if (e1000_validate_nvm_checksum(hw) < 0) {
963*5779Sxy150489 			igb_error(igb,
964*5779Sxy150489 			    "Invalid NVM checksum. Please contact "
965*5779Sxy150489 			    "the vendor to update the NVM.");
966*5779Sxy150489 			goto init_fail;
967*5779Sxy150489 		}
968*5779Sxy150489 	}
969*5779Sxy150489 
970*5779Sxy150489 	/*
971*5779Sxy150489 	 * Set the FIFO size
972*5779Sxy150489 	 */
973*5779Sxy150489 	pba = E1000_PBA_32K;	/* 32K for Rx, 16K for Tx */
974*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_PBA, pba);
975*5779Sxy150489 
976*5779Sxy150489 	/*
977*5779Sxy150489 	 * Setup flow control
978*5779Sxy150489 	 *
979*5779Sxy150489 	 * These parameters set thresholds for the adapter's generation(Tx)
980*5779Sxy150489 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
981*5779Sxy150489 	 * settings.  Flow control is enabled or disabled in the configuration
982*5779Sxy150489 	 * file.
983*5779Sxy150489 	 * High-water mark is set down from the top of the rx fifo (not
984*5779Sxy150489 	 * sensitive to max_frame_size) and low-water is set just below
985*5779Sxy150489 	 * high-water mark.
986*5779Sxy150489 	 * The high water mark must be low enough to fit one full frame above
987*5779Sxy150489 	 * it in the rx FIFO.  Should be the lower of:
988*5779Sxy150489 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
989*5779Sxy150489 	 * frame.
990*5779Sxy150489 	 */
991*5779Sxy150489 	high_water = min(((pba << 10) * 9 / 10),
992*5779Sxy150489 	    ((pba << 10) - igb->max_frame_size));
993*5779Sxy150489 
994*5779Sxy150489 	hw->fc.high_water = high_water & 0xFFF8;
995*5779Sxy150489 	hw->fc.low_water = hw->fc.high_water - 8;
996*5779Sxy150489 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
997*5779Sxy150489 	hw->fc.send_xon = B_TRUE;
998*5779Sxy150489 
999*5779Sxy150489 	/*
1000*5779Sxy150489 	 * Reset the chipset hardware the second time to validate
1001*5779Sxy150489 	 * the PBA setting.
1002*5779Sxy150489 	 */
1003*5779Sxy150489 	(void) e1000_reset_hw(hw);
1004*5779Sxy150489 
1005*5779Sxy150489 	/*
1006*5779Sxy150489 	 * Don't wait for auto-negotiation to complete
1007*5779Sxy150489 	 */
1008*5779Sxy150489 	hw->phy.autoneg_wait_to_complete = B_FALSE;
1009*5779Sxy150489 
1010*5779Sxy150489 	/*
1011*5779Sxy150489 	 * Copper options
1012*5779Sxy150489 	 */
1013*5779Sxy150489 	if (hw->phy.media_type == e1000_media_type_copper) {
1014*5779Sxy150489 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
1015*5779Sxy150489 		hw->phy.disable_polarity_correction = B_FALSE;
1016*5779Sxy150489 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
1017*5779Sxy150489 	}
1018*5779Sxy150489 
1019*5779Sxy150489 	/*
1020*5779Sxy150489 	 * Initialize link settings
1021*5779Sxy150489 	 */
1022*5779Sxy150489 	(void) igb_setup_link(igb, B_FALSE);
1023*5779Sxy150489 
1024*5779Sxy150489 	/*
1025*5779Sxy150489 	 * Initialize the chipset hardware
1026*5779Sxy150489 	 */
1027*5779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
1028*5779Sxy150489 		goto init_fail;
1029*5779Sxy150489 	}
1030*5779Sxy150489 
1031*5779Sxy150489 	mutex_exit(&igb->gen_lock);
1032*5779Sxy150489 	return (IGB_SUCCESS);
1033*5779Sxy150489 
1034*5779Sxy150489 init_fail:
1035*5779Sxy150489 	/*
1036*5779Sxy150489 	 * Reset PHY if possible
1037*5779Sxy150489 	 */
1038*5779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
1039*5779Sxy150489 		(void) e1000_phy_hw_reset(hw);
1040*5779Sxy150489 
1041*5779Sxy150489 	mutex_exit(&igb->gen_lock);
1042*5779Sxy150489 	return (IGB_FAILURE);
1043*5779Sxy150489 }
1044*5779Sxy150489 
1045*5779Sxy150489 /*
1046*5779Sxy150489  * igb_init_rings - Allocate DMA resources for all rx/tx rings and
1047*5779Sxy150489  * initialize relevant hardware settings.
1048*5779Sxy150489  */
1049*5779Sxy150489 static int
1050*5779Sxy150489 igb_init_rings(igb_t *igb)
1051*5779Sxy150489 {
1052*5779Sxy150489 	int i;
1053*5779Sxy150489 
1054*5779Sxy150489 	/*
1055*5779Sxy150489 	 * Allocate buffers for all the rx/tx rings
1056*5779Sxy150489 	 */
1057*5779Sxy150489 	if (igb_alloc_dma(igb) != IGB_SUCCESS)
1058*5779Sxy150489 		return (IGB_FAILURE);
1059*5779Sxy150489 
1060*5779Sxy150489 	/*
1061*5779Sxy150489 	 * Setup the rx/tx rings
1062*5779Sxy150489 	 */
1063*5779Sxy150489 	mutex_enter(&igb->gen_lock);
1064*5779Sxy150489 
1065*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
1066*5779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
1067*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
1068*5779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
1069*5779Sxy150489 
1070*5779Sxy150489 	igb_setup_rings(igb);
1071*5779Sxy150489 
1072*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1073*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1074*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1075*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1076*5779Sxy150489 
1077*5779Sxy150489 	mutex_exit(&igb->gen_lock);
1078*5779Sxy150489 
1079*5779Sxy150489 	return (IGB_SUCCESS);
1080*5779Sxy150489 }
1081*5779Sxy150489 
1082*5779Sxy150489 /*
1083*5779Sxy150489  * igb_fini_rings - Release DMA resources of all rx/tx rings
1084*5779Sxy150489  */
1085*5779Sxy150489 static void
1086*5779Sxy150489 igb_fini_rings(igb_t *igb)
1087*5779Sxy150489 {
1088*5779Sxy150489 	/*
1089*5779Sxy150489 	 * Release the DMA/memory resources of rx/tx rings
1090*5779Sxy150489 	 */
1091*5779Sxy150489 	igb_free_dma(igb);
1092*5779Sxy150489 }
1093*5779Sxy150489 
1094*5779Sxy150489 /*
1095*5779Sxy150489  * igb_chip_start - Initialize and start the chipset hardware
1096*5779Sxy150489  */
1097*5779Sxy150489 static int
1098*5779Sxy150489 igb_chip_start(igb_t *igb)
1099*5779Sxy150489 {
1100*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1101*5779Sxy150489 	int i;
1102*5779Sxy150489 
1103*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1104*5779Sxy150489 
1105*5779Sxy150489 	/*
1106*5779Sxy150489 	 * Get the mac address
1107*5779Sxy150489 	 * This function should handle SPARC case correctly.
1108*5779Sxy150489 	 */
1109*5779Sxy150489 	if (!igb_find_mac_address(igb)) {
1110*5779Sxy150489 		igb_error(igb, "Failed to get the mac address");
1111*5779Sxy150489 		return (IGB_FAILURE);
1112*5779Sxy150489 	}
1113*5779Sxy150489 
1114*5779Sxy150489 	/* Validate mac address */
1115*5779Sxy150489 	if (!is_valid_mac_addr(hw->mac.addr)) {
1116*5779Sxy150489 		igb_error(igb, "Invalid mac address");
1117*5779Sxy150489 		return (IGB_FAILURE);
1118*5779Sxy150489 	}
1119*5779Sxy150489 
1120*5779Sxy150489 	/* Disable wakeup control by default */
1121*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_WUC, 0);
1122*5779Sxy150489 
1123*5779Sxy150489 	/*
1124*5779Sxy150489 	 * Configure/Initialize hardware
1125*5779Sxy150489 	 */
1126*5779Sxy150489 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
1127*5779Sxy150489 		igb_error(igb, "Failed to initialize hardware");
1128*5779Sxy150489 		return (IGB_FAILURE);
1129*5779Sxy150489 	}
1130*5779Sxy150489 
1131*5779Sxy150489 	/*
1132*5779Sxy150489 	 * Make sure driver has control
1133*5779Sxy150489 	 */
1134*5779Sxy150489 	igb_get_driver_control(hw);
1135*5779Sxy150489 
1136*5779Sxy150489 	/*
1137*5779Sxy150489 	 * Setup MSI-X interrupts
1138*5779Sxy150489 	 */
1139*5779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
1140*5779Sxy150489 		igb_setup_adapter_msix(igb);
1141*5779Sxy150489 
1142*5779Sxy150489 	/*
1143*5779Sxy150489 	 * Initialize unicast addresses.
1144*5779Sxy150489 	 */
1145*5779Sxy150489 	igb_init_unicst(igb);
1146*5779Sxy150489 
1147*5779Sxy150489 	/*
1148*5779Sxy150489 	 * Setup and initialize the mctable structures.
1149*5779Sxy150489 	 */
1150*5779Sxy150489 	igb_setup_multicst(igb);
1151*5779Sxy150489 
1152*5779Sxy150489 	/*
1153*5779Sxy150489 	 * Set interrupt throttling rate
1154*5779Sxy150489 	 */
1155*5779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++)
1156*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
1157*5779Sxy150489 
1158*5779Sxy150489 	/* Enable PCI-E master */
1159*5779Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express) {
1160*5779Sxy150489 		e1000_enable_pciex_master(hw);
1161*5779Sxy150489 	}
1162*5779Sxy150489 
1163*5779Sxy150489 	/*
1164*5779Sxy150489 	 * Save the state of the phy
1165*5779Sxy150489 	 */
1166*5779Sxy150489 	igb_get_phy_state(igb);
1167*5779Sxy150489 
1168*5779Sxy150489 	return (IGB_SUCCESS);
1169*5779Sxy150489 }
1170*5779Sxy150489 
1171*5779Sxy150489 /*
1172*5779Sxy150489  * igb_chip_stop - Stop the chipset hardware
1173*5779Sxy150489  */
1174*5779Sxy150489 static void
1175*5779Sxy150489 igb_chip_stop(igb_t *igb)
1176*5779Sxy150489 {
1177*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1178*5779Sxy150489 
1179*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1180*5779Sxy150489 
1181*5779Sxy150489 	/* Tell firmware driver is no longer in control */
1182*5779Sxy150489 	igb_release_driver_control(hw);
1183*5779Sxy150489 
1184*5779Sxy150489 	/*
1185*5779Sxy150489 	 * Reset the chipset
1186*5779Sxy150489 	 */
1187*5779Sxy150489 	(void) e1000_reset_hw(hw);
1188*5779Sxy150489 
1189*5779Sxy150489 	/*
1190*5779Sxy150489 	 * Reset PHY if possible
1191*5779Sxy150489 	 */
1192*5779Sxy150489 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
1193*5779Sxy150489 		(void) e1000_phy_hw_reset(hw);
1194*5779Sxy150489 }
1195*5779Sxy150489 
1196*5779Sxy150489 /*
1197*5779Sxy150489  * igb_reset - Reset the chipset and restart the driver.
1198*5779Sxy150489  *
1199*5779Sxy150489  * It involves stopping and re-starting the chipset,
1200*5779Sxy150489  * and re-configuring the rx/tx rings.
1201*5779Sxy150489  */
1202*5779Sxy150489 static int
1203*5779Sxy150489 igb_reset(igb_t *igb)
1204*5779Sxy150489 {
1205*5779Sxy150489 	int i;
1206*5779Sxy150489 
1207*5779Sxy150489 	mutex_enter(&igb->gen_lock);
1208*5779Sxy150489 
1209*5779Sxy150489 	ASSERT(igb->igb_state & IGB_STARTED);
1210*5779Sxy150489 
1211*5779Sxy150489 	/*
1212*5779Sxy150489 	 * Disable the adapter interrupts to stop any rx/tx activities
1213*5779Sxy150489 	 * before draining pending data and resetting hardware.
1214*5779Sxy150489 	 */
1215*5779Sxy150489 	igb_disable_adapter_interrupts(igb);
1216*5779Sxy150489 
1217*5779Sxy150489 	/*
1218*5779Sxy150489 	 * Drain the pending transmit packets
1219*5779Sxy150489 	 */
1220*5779Sxy150489 	(void) igb_tx_drain(igb);
1221*5779Sxy150489 
1222*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
1223*5779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
1224*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
1225*5779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
1226*5779Sxy150489 
1227*5779Sxy150489 	/*
1228*5779Sxy150489 	 * Stop the chipset hardware
1229*5779Sxy150489 	 */
1230*5779Sxy150489 	igb_chip_stop(igb);
1231*5779Sxy150489 
1232*5779Sxy150489 	/*
1233*5779Sxy150489 	 * Clean the pending tx data/resources
1234*5779Sxy150489 	 */
1235*5779Sxy150489 	igb_tx_clean(igb);
1236*5779Sxy150489 
1237*5779Sxy150489 	/*
1238*5779Sxy150489 	 * Start the chipset hardware
1239*5779Sxy150489 	 */
1240*5779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
1241*5779Sxy150489 		goto reset_failure;
1242*5779Sxy150489 	}
1243*5779Sxy150489 
1244*5779Sxy150489 	/*
1245*5779Sxy150489 	 * Setup the rx/tx rings
1246*5779Sxy150489 	 */
1247*5779Sxy150489 	igb_setup_rings(igb);
1248*5779Sxy150489 
1249*5779Sxy150489 	/*
1250*5779Sxy150489 	 * Enable adapter interrupts
1251*5779Sxy150489 	 * The interrupts must be enabled after the driver state is START
1252*5779Sxy150489 	 */
1253*5779Sxy150489 	igb_enable_adapter_interrupts(igb);
1254*5779Sxy150489 
1255*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1256*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1257*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1258*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1259*5779Sxy150489 
1260*5779Sxy150489 	mutex_exit(&igb->gen_lock);
1261*5779Sxy150489 
1262*5779Sxy150489 	return (IGB_SUCCESS);
1263*5779Sxy150489 
1264*5779Sxy150489 reset_failure:
1265*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1266*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1267*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1268*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1269*5779Sxy150489 
1270*5779Sxy150489 	mutex_exit(&igb->gen_lock);
1271*5779Sxy150489 
1272*5779Sxy150489 	return (IGB_FAILURE);
1273*5779Sxy150489 }
1274*5779Sxy150489 
1275*5779Sxy150489 /*
1276*5779Sxy150489  * igb_tx_clean - Clean the pending transmit packets and DMA resources
1277*5779Sxy150489  */
1278*5779Sxy150489 static void
1279*5779Sxy150489 igb_tx_clean(igb_t *igb)
1280*5779Sxy150489 {
1281*5779Sxy150489 	igb_tx_ring_t *tx_ring;
1282*5779Sxy150489 	tx_control_block_t *tcb;
1283*5779Sxy150489 	link_list_t pending_list;
1284*5779Sxy150489 	uint32_t desc_num;
1285*5779Sxy150489 	int i, j;
1286*5779Sxy150489 
1287*5779Sxy150489 	LINK_LIST_INIT(&pending_list);
1288*5779Sxy150489 
1289*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
1290*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
1291*5779Sxy150489 
1292*5779Sxy150489 		mutex_enter(&tx_ring->recycle_lock);
1293*5779Sxy150489 
1294*5779Sxy150489 		/*
1295*5779Sxy150489 		 * Clean the pending tx data - the pending packets in the
1296*5779Sxy150489 		 * work_list that have no chances to be transmitted again.
1297*5779Sxy150489 		 *
1298*5779Sxy150489 		 * We must ensure the chipset is stopped or the link is down
1299*5779Sxy150489 		 * before cleaning the transmit packets.
1300*5779Sxy150489 		 */
1301*5779Sxy150489 		desc_num = 0;
1302*5779Sxy150489 		for (j = 0; j < tx_ring->ring_size; j++) {
1303*5779Sxy150489 			tcb = tx_ring->work_list[j];
1304*5779Sxy150489 			if (tcb != NULL) {
1305*5779Sxy150489 				desc_num += tcb->desc_num;
1306*5779Sxy150489 
1307*5779Sxy150489 				tx_ring->work_list[j] = NULL;
1308*5779Sxy150489 
1309*5779Sxy150489 				igb_free_tcb(tcb);
1310*5779Sxy150489 
1311*5779Sxy150489 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
1312*5779Sxy150489 			}
1313*5779Sxy150489 		}
1314*5779Sxy150489 
1315*5779Sxy150489 		if (desc_num > 0) {
1316*5779Sxy150489 			atomic_add_32(&tx_ring->tbd_free, desc_num);
1317*5779Sxy150489 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
1318*5779Sxy150489 
1319*5779Sxy150489 			/*
1320*5779Sxy150489 			 * Reset the head and tail pointers of the tbd ring
1321*5779Sxy150489 			 */
1322*5779Sxy150489 			tx_ring->tbd_head = 0;
1323*5779Sxy150489 			tx_ring->tbd_tail = 0;
1324*5779Sxy150489 
1325*5779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
1326*5779Sxy150489 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
1327*5779Sxy150489 		}
1328*5779Sxy150489 
1329*5779Sxy150489 		mutex_exit(&tx_ring->recycle_lock);
1330*5779Sxy150489 
1331*5779Sxy150489 		/*
1332*5779Sxy150489 		 * Add the tx control blocks in the pending list to
1333*5779Sxy150489 		 * the free list.
1334*5779Sxy150489 		 */
1335*5779Sxy150489 		igb_put_free_list(tx_ring, &pending_list);
1336*5779Sxy150489 	}
1337*5779Sxy150489 }
1338*5779Sxy150489 
1339*5779Sxy150489 /*
1340*5779Sxy150489  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
1341*5779Sxy150489  */
1342*5779Sxy150489 static boolean_t
1343*5779Sxy150489 igb_tx_drain(igb_t *igb)
1344*5779Sxy150489 {
1345*5779Sxy150489 	igb_tx_ring_t *tx_ring;
1346*5779Sxy150489 	boolean_t done;
1347*5779Sxy150489 	int i, j;
1348*5779Sxy150489 
1349*5779Sxy150489 	/*
1350*5779Sxy150489 	 * Wait for a specific time to allow pending tx packets
1351*5779Sxy150489 	 * to be transmitted.
1352*5779Sxy150489 	 *
1353*5779Sxy150489 	 * Check the counter tbd_free to see if transmission is done.
1354*5779Sxy150489 	 * No lock protection is needed here.
1355*5779Sxy150489 	 *
1356*5779Sxy150489 	 * Return B_TRUE if all pending packets have been transmitted;
1357*5779Sxy150489 	 * Otherwise return B_FALSE;
1358*5779Sxy150489 	 */
1359*5779Sxy150489 	for (i = 0; i < TX_DRAIN_TIME; i++) {
1360*5779Sxy150489 
1361*5779Sxy150489 		done = B_TRUE;
1362*5779Sxy150489 		for (j = 0; j < igb->num_tx_rings; j++) {
1363*5779Sxy150489 			tx_ring = &igb->tx_rings[j];
1364*5779Sxy150489 			done = done &&
1365*5779Sxy150489 			    (tx_ring->tbd_free == tx_ring->ring_size);
1366*5779Sxy150489 		}
1367*5779Sxy150489 
1368*5779Sxy150489 		if (done)
1369*5779Sxy150489 			break;
1370*5779Sxy150489 
1371*5779Sxy150489 		msec_delay(1);
1372*5779Sxy150489 	}
1373*5779Sxy150489 
1374*5779Sxy150489 	return (done);
1375*5779Sxy150489 }
1376*5779Sxy150489 
1377*5779Sxy150489 /*
1378*5779Sxy150489  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
1379*5779Sxy150489  */
1380*5779Sxy150489 static boolean_t
1381*5779Sxy150489 igb_rx_drain(igb_t *igb)
1382*5779Sxy150489 {
1383*5779Sxy150489 	igb_rx_ring_t *rx_ring;
1384*5779Sxy150489 	boolean_t done;
1385*5779Sxy150489 	int i, j;
1386*5779Sxy150489 
1387*5779Sxy150489 	/*
1388*5779Sxy150489 	 * Polling the rx free list to check if those rx buffers held by
1389*5779Sxy150489 	 * the upper layer are released.
1390*5779Sxy150489 	 *
1391*5779Sxy150489 	 * Check the counter rcb_free to see if all pending buffers are
1392*5779Sxy150489 	 * released. No lock protection is needed here.
1393*5779Sxy150489 	 *
1394*5779Sxy150489 	 * Return B_TRUE if all pending buffers have been released;
1395*5779Sxy150489 	 * Otherwise return B_FALSE;
1396*5779Sxy150489 	 */
1397*5779Sxy150489 	for (i = 0; i < RX_DRAIN_TIME; i++) {
1398*5779Sxy150489 
1399*5779Sxy150489 		done = B_TRUE;
1400*5779Sxy150489 		for (j = 0; j < igb->num_rx_rings; j++) {
1401*5779Sxy150489 			rx_ring = &igb->rx_rings[j];
1402*5779Sxy150489 			done = done &&
1403*5779Sxy150489 			    (rx_ring->rcb_free == rx_ring->free_list_size);
1404*5779Sxy150489 		}
1405*5779Sxy150489 
1406*5779Sxy150489 		if (done)
1407*5779Sxy150489 			break;
1408*5779Sxy150489 
1409*5779Sxy150489 		msec_delay(1);
1410*5779Sxy150489 	}
1411*5779Sxy150489 
1412*5779Sxy150489 	return (done);
1413*5779Sxy150489 }
1414*5779Sxy150489 
1415*5779Sxy150489 /*
1416*5779Sxy150489  * igb_start - Start the driver/chipset
1417*5779Sxy150489  */
1418*5779Sxy150489 int
1419*5779Sxy150489 igb_start(igb_t *igb)
1420*5779Sxy150489 {
1421*5779Sxy150489 	int i;
1422*5779Sxy150489 
1423*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1424*5779Sxy150489 
1425*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
1426*5779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
1427*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
1428*5779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
1429*5779Sxy150489 
1430*5779Sxy150489 	/*
1431*5779Sxy150489 	 * Start the chipset hardware
1432*5779Sxy150489 	 */
1433*5779Sxy150489 	if (igb_chip_start(igb) != IGB_SUCCESS) {
1434*5779Sxy150489 		goto start_failure;
1435*5779Sxy150489 	}
1436*5779Sxy150489 
1437*5779Sxy150489 	/*
1438*5779Sxy150489 	 * Setup the rx/tx rings
1439*5779Sxy150489 	 */
1440*5779Sxy150489 	igb_setup_rings(igb);
1441*5779Sxy150489 
1442*5779Sxy150489 	/*
1443*5779Sxy150489 	 * Enable adapter interrupts
1444*5779Sxy150489 	 * The interrupts must be enabled after the driver state is START
1445*5779Sxy150489 	 */
1446*5779Sxy150489 	igb_enable_adapter_interrupts(igb);
1447*5779Sxy150489 
1448*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1449*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1450*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1451*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1452*5779Sxy150489 
1453*5779Sxy150489 	return (IGB_SUCCESS);
1454*5779Sxy150489 
1455*5779Sxy150489 start_failure:
1456*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1457*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1458*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1459*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1460*5779Sxy150489 
1461*5779Sxy150489 	return (IGB_FAILURE);
1462*5779Sxy150489 }
1463*5779Sxy150489 
1464*5779Sxy150489 /*
1465*5779Sxy150489  * igb_stop - Stop the driver/chipset
1466*5779Sxy150489  */
1467*5779Sxy150489 void
1468*5779Sxy150489 igb_stop(igb_t *igb)
1469*5779Sxy150489 {
1470*5779Sxy150489 	int i;
1471*5779Sxy150489 
1472*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1473*5779Sxy150489 
1474*5779Sxy150489 	/*
1475*5779Sxy150489 	 * Disable the adapter interrupts
1476*5779Sxy150489 	 */
1477*5779Sxy150489 	igb_disable_adapter_interrupts(igb);
1478*5779Sxy150489 
1479*5779Sxy150489 	/*
1480*5779Sxy150489 	 * Drain the pending tx packets
1481*5779Sxy150489 	 */
1482*5779Sxy150489 	(void) igb_tx_drain(igb);
1483*5779Sxy150489 
1484*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++)
1485*5779Sxy150489 		mutex_enter(&igb->rx_rings[i].rx_lock);
1486*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++)
1487*5779Sxy150489 		mutex_enter(&igb->tx_rings[i].tx_lock);
1488*5779Sxy150489 
1489*5779Sxy150489 	/*
1490*5779Sxy150489 	 * Stop the chipset hardware
1491*5779Sxy150489 	 */
1492*5779Sxy150489 	igb_chip_stop(igb);
1493*5779Sxy150489 
1494*5779Sxy150489 	/*
1495*5779Sxy150489 	 * Clean the pending tx data/resources
1496*5779Sxy150489 	 */
1497*5779Sxy150489 	igb_tx_clean(igb);
1498*5779Sxy150489 
1499*5779Sxy150489 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1500*5779Sxy150489 		mutex_exit(&igb->tx_rings[i].tx_lock);
1501*5779Sxy150489 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1502*5779Sxy150489 		mutex_exit(&igb->rx_rings[i].rx_lock);
1503*5779Sxy150489 }
1504*5779Sxy150489 
1505*5779Sxy150489 /*
1506*5779Sxy150489  * igb_alloc_rings - Allocate memory space for rx/tx rings
1507*5779Sxy150489  */
1508*5779Sxy150489 static int
1509*5779Sxy150489 igb_alloc_rings(igb_t *igb)
1510*5779Sxy150489 {
1511*5779Sxy150489 	/*
1512*5779Sxy150489 	 * Allocate memory space for rx rings
1513*5779Sxy150489 	 */
1514*5779Sxy150489 	igb->rx_rings = kmem_zalloc(
1515*5779Sxy150489 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
1516*5779Sxy150489 	    KM_NOSLEEP);
1517*5779Sxy150489 
1518*5779Sxy150489 	if (igb->rx_rings == NULL) {
1519*5779Sxy150489 		return (IGB_FAILURE);
1520*5779Sxy150489 	}
1521*5779Sxy150489 
1522*5779Sxy150489 	/*
1523*5779Sxy150489 	 * Allocate memory space for tx rings
1524*5779Sxy150489 	 */
1525*5779Sxy150489 	igb->tx_rings = kmem_zalloc(
1526*5779Sxy150489 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
1527*5779Sxy150489 	    KM_NOSLEEP);
1528*5779Sxy150489 
1529*5779Sxy150489 	if (igb->tx_rings == NULL) {
1530*5779Sxy150489 		kmem_free(igb->rx_rings,
1531*5779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
1532*5779Sxy150489 		igb->rx_rings = NULL;
1533*5779Sxy150489 		return (IGB_FAILURE);
1534*5779Sxy150489 	}
1535*5779Sxy150489 
1536*5779Sxy150489 	return (IGB_SUCCESS);
1537*5779Sxy150489 }
1538*5779Sxy150489 
1539*5779Sxy150489 /*
1540*5779Sxy150489  * igb_free_rings - Free the memory space of rx/tx rings.
1541*5779Sxy150489  */
1542*5779Sxy150489 static void
1543*5779Sxy150489 igb_free_rings(igb_t *igb)
1544*5779Sxy150489 {
1545*5779Sxy150489 	if (igb->rx_rings != NULL) {
1546*5779Sxy150489 		kmem_free(igb->rx_rings,
1547*5779Sxy150489 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
1548*5779Sxy150489 		igb->rx_rings = NULL;
1549*5779Sxy150489 	}
1550*5779Sxy150489 
1551*5779Sxy150489 	if (igb->tx_rings != NULL) {
1552*5779Sxy150489 		kmem_free(igb->tx_rings,
1553*5779Sxy150489 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
1554*5779Sxy150489 		igb->tx_rings = NULL;
1555*5779Sxy150489 	}
1556*5779Sxy150489 }
1557*5779Sxy150489 
1558*5779Sxy150489 /*
1559*5779Sxy150489  * igb_setup_rings - Setup rx/tx rings
1560*5779Sxy150489  */
1561*5779Sxy150489 static void
1562*5779Sxy150489 igb_setup_rings(igb_t *igb)
1563*5779Sxy150489 {
1564*5779Sxy150489 	/*
1565*5779Sxy150489 	 * Setup the rx/tx rings, including the following:
1566*5779Sxy150489 	 *
1567*5779Sxy150489 	 * 1. Setup the descriptor ring and the control block buffers;
1568*5779Sxy150489 	 * 2. Initialize necessary registers for receive/transmit;
1569*5779Sxy150489 	 * 3. Initialize software pointers/parameters for receive/transmit;
1570*5779Sxy150489 	 */
1571*5779Sxy150489 	igb_setup_rx(igb);
1572*5779Sxy150489 
1573*5779Sxy150489 	igb_setup_tx(igb);
1574*5779Sxy150489 }
1575*5779Sxy150489 
1576*5779Sxy150489 static void
1577*5779Sxy150489 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
1578*5779Sxy150489 {
1579*5779Sxy150489 	igb_t *igb = rx_ring->igb;
1580*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1581*5779Sxy150489 	rx_control_block_t *rcb;
1582*5779Sxy150489 	union e1000_adv_rx_desc	*rbd;
1583*5779Sxy150489 	uint32_t size;
1584*5779Sxy150489 	uint32_t buf_low;
1585*5779Sxy150489 	uint32_t buf_high;
1586*5779Sxy150489 	uint32_t reg_val;
1587*5779Sxy150489 	int i;
1588*5779Sxy150489 
1589*5779Sxy150489 	ASSERT(mutex_owned(&rx_ring->rx_lock));
1590*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1591*5779Sxy150489 
1592*5779Sxy150489 	for (i = 0; i < igb->rx_ring_size; i++) {
1593*5779Sxy150489 		rcb = rx_ring->work_list[i];
1594*5779Sxy150489 		rbd = &rx_ring->rbd_ring[i];
1595*5779Sxy150489 
1596*5779Sxy150489 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
1597*5779Sxy150489 		rbd->read.hdr_addr = NULL;
1598*5779Sxy150489 	}
1599*5779Sxy150489 
1600*5779Sxy150489 	/*
1601*5779Sxy150489 	 * Initialize the length register
1602*5779Sxy150489 	 */
1603*5779Sxy150489 	size = rx_ring->ring_size * sizeof (union e1000_adv_rx_desc);
1604*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
1605*5779Sxy150489 
1606*5779Sxy150489 	/*
1607*5779Sxy150489 	 * Initialize the base address registers
1608*5779Sxy150489 	 */
1609*5779Sxy150489 	buf_low = (uint32_t)rx_ring->rbd_area.dma_address;
1610*5779Sxy150489 	buf_high = (uint32_t)(rx_ring->rbd_area.dma_address >> 32);
1611*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
1612*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
1613*5779Sxy150489 
1614*5779Sxy150489 	/*
1615*5779Sxy150489 	 * Setup head & tail pointers
1616*5779Sxy150489 	 */
1617*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDT(rx_ring->index), rx_ring->ring_size - 1);
1618*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RDH(rx_ring->index), 0);
1619*5779Sxy150489 
1620*5779Sxy150489 	rx_ring->rbd_next = 0;
1621*5779Sxy150489 
1622*5779Sxy150489 	/*
1623*5779Sxy150489 	 * Note: Considering the case that the chipset is being reset
1624*5779Sxy150489 	 * and there are still some buffers held by the upper layer,
1625*5779Sxy150489 	 * we should not reset the values of rcb_head, rcb_tail and
1626*5779Sxy150489 	 * rcb_free;
1627*5779Sxy150489 	 */
1628*5779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
1629*5779Sxy150489 		rx_ring->rcb_head = 0;
1630*5779Sxy150489 		rx_ring->rcb_tail = 0;
1631*5779Sxy150489 		rx_ring->rcb_free = rx_ring->free_list_size;
1632*5779Sxy150489 	}
1633*5779Sxy150489 
1634*5779Sxy150489 	/*
1635*5779Sxy150489 	 * Setup the Receive Descriptor Control Register (RXDCTL)
1636*5779Sxy150489 	 */
1637*5779Sxy150489 	reg_val = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
1638*5779Sxy150489 	reg_val |= E1000_RXDCTL_QUEUE_ENABLE;
1639*5779Sxy150489 	reg_val &= 0xFFF00000;
1640*5779Sxy150489 	reg_val |= 16;		/* pthresh */
1641*5779Sxy150489 	reg_val |= 8 << 8;	/* hthresh */
1642*5779Sxy150489 	reg_val |= 1 << 16;	/* wthresh */
1643*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), reg_val);
1644*5779Sxy150489 
1645*5779Sxy150489 	/*
1646*5779Sxy150489 	 * Setup the Split and Replication Receive Control Register.
1647*5779Sxy150489 	 * Set the rx buffer size and the advanced descriptor type.
1648*5779Sxy150489 	 */
1649*5779Sxy150489 	reg_val = (igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
1650*5779Sxy150489 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
1651*5779Sxy150489 
1652*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index), reg_val);
1653*5779Sxy150489 }
1654*5779Sxy150489 
1655*5779Sxy150489 static void
1656*5779Sxy150489 igb_setup_rx(igb_t *igb)
1657*5779Sxy150489 {
1658*5779Sxy150489 	igb_rx_ring_t *rx_ring;
1659*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1660*5779Sxy150489 	uint32_t reg_val;
1661*5779Sxy150489 	int i;
1662*5779Sxy150489 
1663*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
1664*5779Sxy150489 		rx_ring = &igb->rx_rings[i];
1665*5779Sxy150489 		igb_setup_rx_ring(rx_ring);
1666*5779Sxy150489 	}
1667*5779Sxy150489 
1668*5779Sxy150489 	/*
1669*5779Sxy150489 	 * Setup the Receive Control Register (RCTL), and ENABLE the
1670*5779Sxy150489 	 * receiver. The initial configuration is to: Enable the receiver,
1671*5779Sxy150489 	 * accept broadcasts, discard bad packets (and long packets),
1672*5779Sxy150489 	 * disable VLAN filter checking, set the receive descriptor
1673*5779Sxy150489 	 * minimum threshold size to 1/2, and the receive buffer size to
1674*5779Sxy150489 	 * 2k.
1675*5779Sxy150489 	 */
1676*5779Sxy150489 	reg_val = E1000_RCTL_EN |	/* Enable Receive Unit */
1677*5779Sxy150489 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
1678*5779Sxy150489 	    E1000_RCTL_LPE |		/* Large Packet Enable bit */
1679*5779Sxy150489 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
1680*5779Sxy150489 	    E1000_RCTL_RDMTS_HALF |
1681*5779Sxy150489 	    E1000_RCTL_SECRC |		/* Strip Ethernet CRC */
1682*5779Sxy150489 	    E1000_RCTL_LBM_NO;		/* Loopback Mode = none */
1683*5779Sxy150489 
1684*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, reg_val);
1685*5779Sxy150489 
1686*5779Sxy150489 	/*
1687*5779Sxy150489 	 * Setup the Rx Long Packet Max Length register
1688*5779Sxy150489 	 */
1689*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
1690*5779Sxy150489 
1691*5779Sxy150489 	/*
1692*5779Sxy150489 	 * Hardware checksum settings
1693*5779Sxy150489 	 */
1694*5779Sxy150489 	if (igb->rx_hcksum_enable) {
1695*5779Sxy150489 		reg_val =
1696*5779Sxy150489 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
1697*5779Sxy150489 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
1698*5779Sxy150489 
1699*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_RXCSUM, reg_val);
1700*5779Sxy150489 	}
1701*5779Sxy150489 
1702*5779Sxy150489 	/*
1703*5779Sxy150489 	 * Setup RSS for multiple receive queues
1704*5779Sxy150489 	 */
1705*5779Sxy150489 	if (igb->num_rx_rings > 1)
1706*5779Sxy150489 		igb_setup_rss(igb);
1707*5779Sxy150489 }
1708*5779Sxy150489 
1709*5779Sxy150489 static void
1710*5779Sxy150489 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
1711*5779Sxy150489 {
1712*5779Sxy150489 	igb_t *igb = tx_ring->igb;
1713*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1714*5779Sxy150489 	uint32_t size;
1715*5779Sxy150489 	uint32_t buf_low;
1716*5779Sxy150489 	uint32_t buf_high;
1717*5779Sxy150489 	uint32_t reg_val;
1718*5779Sxy150489 
1719*5779Sxy150489 	ASSERT(mutex_owned(&tx_ring->tx_lock));
1720*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1721*5779Sxy150489 
1722*5779Sxy150489 	/*
1723*5779Sxy150489 	 * Initialize the length register
1724*5779Sxy150489 	 */
1725*5779Sxy150489 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
1726*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
1727*5779Sxy150489 
1728*5779Sxy150489 	/*
1729*5779Sxy150489 	 * Initialize the base address registers
1730*5779Sxy150489 	 */
1731*5779Sxy150489 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
1732*5779Sxy150489 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
1733*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
1734*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
1735*5779Sxy150489 
1736*5779Sxy150489 	/*
1737*5779Sxy150489 	 * Setup head & tail pointers
1738*5779Sxy150489 	 */
1739*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
1740*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
1741*5779Sxy150489 
1742*5779Sxy150489 	/*
1743*5779Sxy150489 	 * Setup head write-back
1744*5779Sxy150489 	 */
1745*5779Sxy150489 	if (igb->tx_head_wb_enable) {
1746*5779Sxy150489 		/*
1747*5779Sxy150489 		 * The memory of the head write-back is allocated using
1748*5779Sxy150489 		 * the extra tbd beyond the tail of the tbd ring.
1749*5779Sxy150489 		 */
1750*5779Sxy150489 		tx_ring->tbd_head_wb = (uint32_t *)
1751*5779Sxy150489 		    ((uintptr_t)tx_ring->tbd_area.address + size);
1752*5779Sxy150489 
1753*5779Sxy150489 		buf_low = (uint32_t)
1754*5779Sxy150489 		    (tx_ring->tbd_area.dma_address + size);
1755*5779Sxy150489 		buf_high = (uint32_t)
1756*5779Sxy150489 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
1757*5779Sxy150489 
1758*5779Sxy150489 		/* Set the head write-back enable bit */
1759*5779Sxy150489 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
1760*5779Sxy150489 
1761*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
1762*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
1763*5779Sxy150489 
1764*5779Sxy150489 		/*
1765*5779Sxy150489 		 * Turn off relaxed ordering for head write back or it will
1766*5779Sxy150489 		 * cause problems with the tx recycling
1767*5779Sxy150489 		 */
1768*5779Sxy150489 		reg_val = E1000_READ_REG(hw,
1769*5779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index));
1770*5779Sxy150489 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
1771*5779Sxy150489 		E1000_WRITE_REG(hw,
1772*5779Sxy150489 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
1773*5779Sxy150489 	} else {
1774*5779Sxy150489 		tx_ring->tbd_head_wb = NULL;
1775*5779Sxy150489 	}
1776*5779Sxy150489 
1777*5779Sxy150489 	tx_ring->tbd_head = 0;
1778*5779Sxy150489 	tx_ring->tbd_tail = 0;
1779*5779Sxy150489 	tx_ring->tbd_free = tx_ring->ring_size;
1780*5779Sxy150489 
1781*5779Sxy150489 	/*
1782*5779Sxy150489 	 * Note: Considering the case that the chipset is being reset,
1783*5779Sxy150489 	 * and there are still some buffers held by the upper layer,
1784*5779Sxy150489 	 * we should not reset the values of tcb_head, tcb_tail.
1785*5779Sxy150489 	 */
1786*5779Sxy150489 	if (igb->igb_state == IGB_UNKNOWN) {
1787*5779Sxy150489 		tx_ring->tcb_head = 0;
1788*5779Sxy150489 		tx_ring->tcb_tail = 0;
1789*5779Sxy150489 		tx_ring->tcb_free = tx_ring->free_list_size;
1790*5779Sxy150489 	} else {
1791*5779Sxy150489 		ASSERT(tx_ring->tcb_free == tx_ring->free_list_size);
1792*5779Sxy150489 	}
1793*5779Sxy150489 
1794*5779Sxy150489 	/*
1795*5779Sxy150489 	 * Initialize hardware checksum offload settings
1796*5779Sxy150489 	 */
1797*5779Sxy150489 	tx_ring->hcksum_context.hcksum_flags = 0;
1798*5779Sxy150489 	tx_ring->hcksum_context.ip_hdr_len = 0;
1799*5779Sxy150489 	tx_ring->hcksum_context.mac_hdr_len = 0;
1800*5779Sxy150489 	tx_ring->hcksum_context.l4_proto = 0;
1801*5779Sxy150489 }
1802*5779Sxy150489 
1803*5779Sxy150489 static void
1804*5779Sxy150489 igb_setup_tx(igb_t *igb)
1805*5779Sxy150489 {
1806*5779Sxy150489 	igb_tx_ring_t *tx_ring;
1807*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1808*5779Sxy150489 	uint32_t reg_val;
1809*5779Sxy150489 	int i;
1810*5779Sxy150489 
1811*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
1812*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
1813*5779Sxy150489 		igb_setup_tx_ring(tx_ring);
1814*5779Sxy150489 	}
1815*5779Sxy150489 
1816*5779Sxy150489 	/*
1817*5779Sxy150489 	 * Setup the Transmit Control Register (TCTL)
1818*5779Sxy150489 	 */
1819*5779Sxy150489 	reg_val = E1000_TCTL_PSP | E1000_TCTL_EN |
1820*5779Sxy150489 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT) |
1821*5779Sxy150489 	    (E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT) |
1822*5779Sxy150489 	    E1000_TCTL_RTLC;
1823*5779Sxy150489 
1824*5779Sxy150489 	/* Enable the MULR bit */
1825*5779Sxy150489 	if (hw->bus.type == e1000_bus_type_pci_express)
1826*5779Sxy150489 		reg_val |= E1000_TCTL_MULR;
1827*5779Sxy150489 
1828*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
1829*5779Sxy150489 
1830*5779Sxy150489 	/*
1831*5779Sxy150489 	 * Set the default values for the Tx Inter Packet Gap timer
1832*5779Sxy150489 	 */
1833*5779Sxy150489 	if (hw->phy.media_type == e1000_media_type_fiber)
1834*5779Sxy150489 		reg_val = DEFAULT_82543_TIPG_IPGT_FIBER;
1835*5779Sxy150489 	else
1836*5779Sxy150489 		reg_val = DEFAULT_82543_TIPG_IPGT_COPPER;
1837*5779Sxy150489 	reg_val |=
1838*5779Sxy150489 	    DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT;
1839*5779Sxy150489 	reg_val |=
1840*5779Sxy150489 	    DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT;
1841*5779Sxy150489 
1842*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_TIPG, reg_val);
1843*5779Sxy150489 }
1844*5779Sxy150489 
1845*5779Sxy150489 /*
1846*5779Sxy150489  * igb_setup_rss - Setup receive-side scaling feature
1847*5779Sxy150489  */
1848*5779Sxy150489 static void
1849*5779Sxy150489 igb_setup_rss(igb_t *igb)
1850*5779Sxy150489 {
1851*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1852*5779Sxy150489 	uint32_t i, mrqc, rxcsum;
1853*5779Sxy150489 	int shift;
1854*5779Sxy150489 	uint32_t random;
1855*5779Sxy150489 	union e1000_reta {
1856*5779Sxy150489 		uint32_t	dword;
1857*5779Sxy150489 		uint8_t		bytes[4];
1858*5779Sxy150489 	} reta;
1859*5779Sxy150489 
1860*5779Sxy150489 	/* Setup the Redirection Table */
1861*5779Sxy150489 	shift = 6;
1862*5779Sxy150489 	for (i = 0; i < (32 * 4); i++) {
1863*5779Sxy150489 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
1864*5779Sxy150489 		if ((i & 3) == 3) {
1865*5779Sxy150489 			E1000_WRITE_REG(hw,
1866*5779Sxy150489 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
1867*5779Sxy150489 		}
1868*5779Sxy150489 	}
1869*5779Sxy150489 
1870*5779Sxy150489 	/* Fill out hash function seeds */
1871*5779Sxy150489 	for (i = 0; i < 10; i++) {
1872*5779Sxy150489 		(void) random_get_pseudo_bytes((uint8_t *)&random,
1873*5779Sxy150489 		    sizeof (uint32_t));
1874*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
1875*5779Sxy150489 	}
1876*5779Sxy150489 
1877*5779Sxy150489 	/* Setup the Multiple Receive Queue Control register */
1878*5779Sxy150489 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
1879*5779Sxy150489 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
1880*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
1881*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6 |
1882*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
1883*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
1884*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
1885*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
1886*5779Sxy150489 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
1887*5779Sxy150489 
1888*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
1889*5779Sxy150489 
1890*5779Sxy150489 	/*
1891*5779Sxy150489 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
1892*5779Sxy150489 	 *
1893*5779Sxy150489 	 * The Packet Checksum is not ethernet CRC. It is another kind of
1894*5779Sxy150489 	 * checksum offloading provided by the 82575 chipset besides the IP
1895*5779Sxy150489 	 * header checksum offloading and the TCP/UDP checksum offloading.
1896*5779Sxy150489 	 * The Packet Checksum is by default computed over the entire packet
1897*5779Sxy150489 	 * from the first byte of the DA through the last byte of the CRC,
1898*5779Sxy150489 	 * including the Ethernet and IP headers.
1899*5779Sxy150489 	 *
1900*5779Sxy150489 	 * It is a hardware limitation that Packet Checksum is mutually
1901*5779Sxy150489 	 * exclusive with RSS.
1902*5779Sxy150489 	 */
1903*5779Sxy150489 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1904*5779Sxy150489 	rxcsum |= E1000_RXCSUM_PCSD;
1905*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1906*5779Sxy150489 }
1907*5779Sxy150489 
1908*5779Sxy150489 /*
1909*5779Sxy150489  * igb_init_unicst - Initialize the unicast addresses
1910*5779Sxy150489  */
1911*5779Sxy150489 static void
1912*5779Sxy150489 igb_init_unicst(igb_t *igb)
1913*5779Sxy150489 {
1914*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1915*5779Sxy150489 	int slot;
1916*5779Sxy150489 
1917*5779Sxy150489 	/*
1918*5779Sxy150489 	 * Here we should consider two situations:
1919*5779Sxy150489 	 *
1920*5779Sxy150489 	 * 1. Chipset is initialized the first time
1921*5779Sxy150489 	 *    Initialize the multiple unicast addresses, and
1922*5779Sxy150489 	 *    save the default mac address.
1923*5779Sxy150489 	 *
1924*5779Sxy150489 	 * 2. Chipset is reset
1925*5779Sxy150489 	 *    Recover the multiple unicast addresses from the
1926*5779Sxy150489 	 *    software data structure to the RAR registers.
1927*5779Sxy150489 	 */
1928*5779Sxy150489 	if (!igb->unicst_init) {
1929*5779Sxy150489 		/* Initialize the multiple unicast addresses */
1930*5779Sxy150489 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
1931*5779Sxy150489 
1932*5779Sxy150489 		igb->unicst_avail = igb->unicst_total - 1;
1933*5779Sxy150489 
1934*5779Sxy150489 		/* Store the default mac address */
1935*5779Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
1936*5779Sxy150489 
1937*5779Sxy150489 		bcopy(hw->mac.addr, igb->unicst_addr[0].mac.addr,
1938*5779Sxy150489 		    ETHERADDRL);
1939*5779Sxy150489 		igb->unicst_addr[0].mac.set = 1;
1940*5779Sxy150489 
1941*5779Sxy150489 		for (slot = 1; slot < igb->unicst_total; slot++)
1942*5779Sxy150489 			igb->unicst_addr[slot].mac.set = 0;
1943*5779Sxy150489 
1944*5779Sxy150489 		igb->unicst_init = B_TRUE;
1945*5779Sxy150489 	} else {
1946*5779Sxy150489 		/* Recover the default mac address */
1947*5779Sxy150489 		bcopy(igb->unicst_addr[0].mac.addr, hw->mac.addr,
1948*5779Sxy150489 		    ETHERADDRL);
1949*5779Sxy150489 
1950*5779Sxy150489 		/* Store the default mac address */
1951*5779Sxy150489 		e1000_rar_set(hw, hw->mac.addr, 0);
1952*5779Sxy150489 
1953*5779Sxy150489 		/* Re-configure the RAR registers */
1954*5779Sxy150489 		for (slot = 1; slot < igb->unicst_total; slot++)
1955*5779Sxy150489 			e1000_rar_set(hw,
1956*5779Sxy150489 			    igb->unicst_addr[slot].mac.addr, slot);
1957*5779Sxy150489 	}
1958*5779Sxy150489 }
1959*5779Sxy150489 
1960*5779Sxy150489 /*
1961*5779Sxy150489  * igb_unicst_set - Set the unicast address to the specified slot
1962*5779Sxy150489  */
1963*5779Sxy150489 int
1964*5779Sxy150489 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
1965*5779Sxy150489     mac_addr_slot_t slot)
1966*5779Sxy150489 {
1967*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
1968*5779Sxy150489 
1969*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1970*5779Sxy150489 
1971*5779Sxy150489 	/*
1972*5779Sxy150489 	 * Save the unicast address in the software data structure
1973*5779Sxy150489 	 */
1974*5779Sxy150489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
1975*5779Sxy150489 
1976*5779Sxy150489 	/*
1977*5779Sxy150489 	 * Set the unicast address to the RAR register
1978*5779Sxy150489 	 */
1979*5779Sxy150489 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
1980*5779Sxy150489 
1981*5779Sxy150489 	return (0);
1982*5779Sxy150489 }
1983*5779Sxy150489 
1984*5779Sxy150489 /*
1985*5779Sxy150489  * igb_multicst_add - Add a multicst address
1986*5779Sxy150489  */
1987*5779Sxy150489 int
1988*5779Sxy150489 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
1989*5779Sxy150489 {
1990*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
1991*5779Sxy150489 
1992*5779Sxy150489 	if ((multiaddr[0] & 01) == 0) {
1993*5779Sxy150489 		return (EINVAL);
1994*5779Sxy150489 	}
1995*5779Sxy150489 
1996*5779Sxy150489 	if (igb->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
1997*5779Sxy150489 		return (ENOENT);
1998*5779Sxy150489 	}
1999*5779Sxy150489 
2000*5779Sxy150489 	bcopy(multiaddr,
2001*5779Sxy150489 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
2002*5779Sxy150489 	igb->mcast_count++;
2003*5779Sxy150489 
2004*5779Sxy150489 	/*
2005*5779Sxy150489 	 * Update the multicast table in the hardware
2006*5779Sxy150489 	 */
2007*5779Sxy150489 	igb_setup_multicst(igb);
2008*5779Sxy150489 
2009*5779Sxy150489 	return (0);
2010*5779Sxy150489 }
2011*5779Sxy150489 
2012*5779Sxy150489 /*
2013*5779Sxy150489  * igb_multicst_remove - Remove a multicst address
2014*5779Sxy150489  */
2015*5779Sxy150489 int
2016*5779Sxy150489 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
2017*5779Sxy150489 {
2018*5779Sxy150489 	int i;
2019*5779Sxy150489 
2020*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
2021*5779Sxy150489 
2022*5779Sxy150489 	for (i = 0; i < igb->mcast_count; i++) {
2023*5779Sxy150489 		if (bcmp(multiaddr, &igb->mcast_table[i],
2024*5779Sxy150489 		    ETHERADDRL) == 0) {
2025*5779Sxy150489 			for (i++; i < igb->mcast_count; i++) {
2026*5779Sxy150489 				igb->mcast_table[i - 1] =
2027*5779Sxy150489 				    igb->mcast_table[i];
2028*5779Sxy150489 			}
2029*5779Sxy150489 			igb->mcast_count--;
2030*5779Sxy150489 			break;
2031*5779Sxy150489 		}
2032*5779Sxy150489 	}
2033*5779Sxy150489 
2034*5779Sxy150489 	/*
2035*5779Sxy150489 	 * Update the multicast table in the hardware
2036*5779Sxy150489 	 */
2037*5779Sxy150489 	igb_setup_multicst(igb);
2038*5779Sxy150489 
2039*5779Sxy150489 	return (0);
2040*5779Sxy150489 }
2041*5779Sxy150489 
2042*5779Sxy150489 /*
2043*5779Sxy150489  * igb_setup_multicast - setup multicast data structures
2044*5779Sxy150489  *
2045*5779Sxy150489  * This routine initializes all of the multicast related structures
2046*5779Sxy150489  * and save them in the hardware registers.
2047*5779Sxy150489  */
2048*5779Sxy150489 static void
2049*5779Sxy150489 igb_setup_multicst(igb_t *igb)
2050*5779Sxy150489 {
2051*5779Sxy150489 	uint8_t *mc_addr_list;
2052*5779Sxy150489 	uint32_t mc_addr_count;
2053*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2054*5779Sxy150489 
2055*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
2056*5779Sxy150489 
2057*5779Sxy150489 	ASSERT(igb->mcast_count <= MAX_NUM_MULTICAST_ADDRESSES);
2058*5779Sxy150489 
2059*5779Sxy150489 	mc_addr_list = (uint8_t *)igb->mcast_table;
2060*5779Sxy150489 	mc_addr_count = igb->mcast_count;
2061*5779Sxy150489 
2062*5779Sxy150489 	/*
2063*5779Sxy150489 	 * Update the multicase addresses to the MTA registers
2064*5779Sxy150489 	 */
2065*5779Sxy150489 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
2066*5779Sxy150489 	    igb->unicst_total, hw->mac.rar_entry_count);
2067*5779Sxy150489 }
2068*5779Sxy150489 
2069*5779Sxy150489 /*
2070*5779Sxy150489  * igb_get_conf - Get driver configurations set in driver.conf
2071*5779Sxy150489  *
2072*5779Sxy150489  * This routine gets user-configured values out of the configuration
2073*5779Sxy150489  * file igb.conf.
2074*5779Sxy150489  *
2075*5779Sxy150489  * For each configurable value, there is a minimum, a maximum, and a
2076*5779Sxy150489  * default.
2077*5779Sxy150489  * If user does not configure a value, use the default.
2078*5779Sxy150489  * If user configures below the minimum, use the minumum.
2079*5779Sxy150489  * If user configures above the maximum, use the maxumum.
2080*5779Sxy150489  */
2081*5779Sxy150489 static void
2082*5779Sxy150489 igb_get_conf(igb_t *igb)
2083*5779Sxy150489 {
2084*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2085*5779Sxy150489 	uint32_t default_mtu;
2086*5779Sxy150489 	uint32_t flow_control;
2087*5779Sxy150489 
2088*5779Sxy150489 	/*
2089*5779Sxy150489 	 * igb driver supports the following user configurations:
2090*5779Sxy150489 	 *
2091*5779Sxy150489 	 * Link configurations:
2092*5779Sxy150489 	 *    adv_autoneg_cap
2093*5779Sxy150489 	 *    adv_1000fdx_cap
2094*5779Sxy150489 	 *    adv_100fdx_cap
2095*5779Sxy150489 	 *    adv_100hdx_cap
2096*5779Sxy150489 	 *    adv_10fdx_cap
2097*5779Sxy150489 	 *    adv_10hdx_cap
2098*5779Sxy150489 	 * Note: 1000hdx is not supported.
2099*5779Sxy150489 	 *
2100*5779Sxy150489 	 * Jumbo frame configuration:
2101*5779Sxy150489 	 *    default_mtu
2102*5779Sxy150489 	 *
2103*5779Sxy150489 	 * Ethernet flow control configuration:
2104*5779Sxy150489 	 *    flow_control
2105*5779Sxy150489 	 *
2106*5779Sxy150489 	 * Multiple rings configurations:
2107*5779Sxy150489 	 *    tx_queue_number
2108*5779Sxy150489 	 *    tx_ring_size
2109*5779Sxy150489 	 *    rx_queue_number
2110*5779Sxy150489 	 *    rx_ring_size
2111*5779Sxy150489 	 *
2112*5779Sxy150489 	 * Call igb_get_prop() to get the value for a specific
2113*5779Sxy150489 	 * configuration parameter.
2114*5779Sxy150489 	 */
2115*5779Sxy150489 
2116*5779Sxy150489 	/*
2117*5779Sxy150489 	 * Link configurations
2118*5779Sxy150489 	 */
2119*5779Sxy150489 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
2120*5779Sxy150489 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
2121*5779Sxy150489 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
2122*5779Sxy150489 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
2123*5779Sxy150489 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
2124*5779Sxy150489 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
2125*5779Sxy150489 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
2126*5779Sxy150489 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
2127*5779Sxy150489 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
2128*5779Sxy150489 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
2129*5779Sxy150489 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
2130*5779Sxy150489 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
2131*5779Sxy150489 
2132*5779Sxy150489 	/*
2133*5779Sxy150489 	 * Jumbo frame configurations
2134*5779Sxy150489 	 */
2135*5779Sxy150489 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
2136*5779Sxy150489 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
2137*5779Sxy150489 
2138*5779Sxy150489 	igb->max_frame_size = default_mtu +
2139*5779Sxy150489 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
2140*5779Sxy150489 
2141*5779Sxy150489 	/*
2142*5779Sxy150489 	 * Ethernet flow control configuration
2143*5779Sxy150489 	 */
2144*5779Sxy150489 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
2145*5779Sxy150489 	    e1000_fc_none, 4, e1000_fc_full);
2146*5779Sxy150489 	if (flow_control == 4)
2147*5779Sxy150489 		flow_control = e1000_fc_default;
2148*5779Sxy150489 
2149*5779Sxy150489 	hw->fc.type = flow_control;
2150*5779Sxy150489 
2151*5779Sxy150489 	/*
2152*5779Sxy150489 	 * Multiple rings configurations
2153*5779Sxy150489 	 */
2154*5779Sxy150489 	igb->num_tx_rings = igb_get_prop(igb, PROP_TX_QUEUE_NUM,
2155*5779Sxy150489 	    MIN_TX_QUEUE_NUM, MAX_TX_QUEUE_NUM, DEFAULT_TX_QUEUE_NUM);
2156*5779Sxy150489 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
2157*5779Sxy150489 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
2158*5779Sxy150489 
2159*5779Sxy150489 	igb->num_rx_rings = igb_get_prop(igb, PROP_RX_QUEUE_NUM,
2160*5779Sxy150489 	    MIN_RX_QUEUE_NUM, MAX_RX_QUEUE_NUM, DEFAULT_RX_QUEUE_NUM);
2161*5779Sxy150489 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
2162*5779Sxy150489 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
2163*5779Sxy150489 
2164*5779Sxy150489 	/*
2165*5779Sxy150489 	 * Tunable used to force an interrupt type. The only use is
2166*5779Sxy150489 	 * for testing of the lesser interrupt types.
2167*5779Sxy150489 	 * 0 = don't force interrupt type
2168*5779Sxy150489 	 * 1 = force interrupt type MSIX
2169*5779Sxy150489 	 * 2 = force interrupt type MSI
2170*5779Sxy150489 	 * 3 = force interrupt type Legacy
2171*5779Sxy150489 	 */
2172*5779Sxy150489 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
2173*5779Sxy150489 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_MSI);
2174*5779Sxy150489 
2175*5779Sxy150489 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
2176*5779Sxy150489 	    0, 1, 1);
2177*5779Sxy150489 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
2178*5779Sxy150489 	    0, 1, 1);
2179*5779Sxy150489 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
2180*5779Sxy150489 	    0, 1, 0);
2181*5779Sxy150489 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
2182*5779Sxy150489 	    0, 1, 1);
2183*5779Sxy150489 
2184*5779Sxy150489 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
2185*5779Sxy150489 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
2186*5779Sxy150489 	    DEFAULT_TX_COPY_THRESHOLD);
2187*5779Sxy150489 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
2188*5779Sxy150489 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
2189*5779Sxy150489 	    DEFAULT_TX_RECYCLE_THRESHOLD);
2190*5779Sxy150489 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
2191*5779Sxy150489 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
2192*5779Sxy150489 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
2193*5779Sxy150489 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
2194*5779Sxy150489 	    MIN_TX_RESCHED_THRESHOLD, MAX_TX_RESCHED_THRESHOLD,
2195*5779Sxy150489 	    DEFAULT_TX_RESCHED_THRESHOLD);
2196*5779Sxy150489 
2197*5779Sxy150489 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
2198*5779Sxy150489 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
2199*5779Sxy150489 	    DEFAULT_RX_COPY_THRESHOLD);
2200*5779Sxy150489 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
2201*5779Sxy150489 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
2202*5779Sxy150489 	    DEFAULT_RX_LIMIT_PER_INTR);
2203*5779Sxy150489 
2204*5779Sxy150489 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
2205*5779Sxy150489 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
2206*5779Sxy150489 	    DEFAULT_INTR_THROTTLING);
2207*5779Sxy150489 }
2208*5779Sxy150489 
2209*5779Sxy150489 /*
2210*5779Sxy150489  * igb_get_prop - Get a property value out of the configuration file igb.conf
2211*5779Sxy150489  *
2212*5779Sxy150489  * Caller provides the name of the property, a default value, a minimum
2213*5779Sxy150489  * value, and a maximum value.
2214*5779Sxy150489  *
2215*5779Sxy150489  * Return configured value of the property, with default, minimum and
2216*5779Sxy150489  * maximum properly applied.
2217*5779Sxy150489  */
2218*5779Sxy150489 static int
2219*5779Sxy150489 igb_get_prop(igb_t *igb,
2220*5779Sxy150489     char *propname,	/* name of the property */
2221*5779Sxy150489     int minval,		/* minimum acceptable value */
2222*5779Sxy150489     int maxval,		/* maximim acceptable value */
2223*5779Sxy150489     int defval)		/* default value */
2224*5779Sxy150489 {
2225*5779Sxy150489 	int value;
2226*5779Sxy150489 
2227*5779Sxy150489 	/*
2228*5779Sxy150489 	 * Call ddi_prop_get_int() to read the conf settings
2229*5779Sxy150489 	 */
2230*5779Sxy150489 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
2231*5779Sxy150489 	    DDI_PROP_DONTPASS, propname, defval);
2232*5779Sxy150489 
2233*5779Sxy150489 	if (value > maxval)
2234*5779Sxy150489 		value = maxval;
2235*5779Sxy150489 
2236*5779Sxy150489 	if (value < minval)
2237*5779Sxy150489 		value = minval;
2238*5779Sxy150489 
2239*5779Sxy150489 	return (value);
2240*5779Sxy150489 }
2241*5779Sxy150489 
2242*5779Sxy150489 /*
2243*5779Sxy150489  * igb_setup_link - Using the link properties to setup the link
2244*5779Sxy150489  */
2245*5779Sxy150489 int
2246*5779Sxy150489 igb_setup_link(igb_t *igb, boolean_t setup_hw)
2247*5779Sxy150489 {
2248*5779Sxy150489 	struct e1000_mac_info *mac;
2249*5779Sxy150489 	struct e1000_phy_info *phy;
2250*5779Sxy150489 	boolean_t invalid;
2251*5779Sxy150489 
2252*5779Sxy150489 	mac = &igb->hw.mac;
2253*5779Sxy150489 	phy = &igb->hw.phy;
2254*5779Sxy150489 	invalid = B_FALSE;
2255*5779Sxy150489 
2256*5779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
2257*5779Sxy150489 		mac->autoneg = B_TRUE;
2258*5779Sxy150489 		phy->autoneg_advertised = 0;
2259*5779Sxy150489 
2260*5779Sxy150489 		/*
2261*5779Sxy150489 		 * 1000hdx is not supported for autonegotiation
2262*5779Sxy150489 		 */
2263*5779Sxy150489 		if (igb->param_adv_1000fdx_cap == 1)
2264*5779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
2265*5779Sxy150489 
2266*5779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
2267*5779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
2268*5779Sxy150489 
2269*5779Sxy150489 		if (igb->param_adv_100hdx_cap == 1)
2270*5779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
2271*5779Sxy150489 
2272*5779Sxy150489 		if (igb->param_adv_10fdx_cap == 1)
2273*5779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
2274*5779Sxy150489 
2275*5779Sxy150489 		if (igb->param_adv_10hdx_cap == 1)
2276*5779Sxy150489 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
2277*5779Sxy150489 
2278*5779Sxy150489 		if (phy->autoneg_advertised == 0)
2279*5779Sxy150489 			invalid = B_TRUE;
2280*5779Sxy150489 	} else {
2281*5779Sxy150489 		mac->autoneg = B_FALSE;
2282*5779Sxy150489 
2283*5779Sxy150489 		/*
2284*5779Sxy150489 		 * 1000fdx and 1000hdx are not supported for forced link
2285*5779Sxy150489 		 */
2286*5779Sxy150489 		if (igb->param_adv_100fdx_cap == 1)
2287*5779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
2288*5779Sxy150489 		else if (igb->param_adv_100hdx_cap == 1)
2289*5779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
2290*5779Sxy150489 		else if (igb->param_adv_10fdx_cap == 1)
2291*5779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
2292*5779Sxy150489 		else if (igb->param_adv_10hdx_cap == 1)
2293*5779Sxy150489 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
2294*5779Sxy150489 		else
2295*5779Sxy150489 			invalid = B_TRUE;
2296*5779Sxy150489 	}
2297*5779Sxy150489 
2298*5779Sxy150489 	if (invalid) {
2299*5779Sxy150489 		igb_notice(igb, "Invalid link settings. Setup link to "
2300*5779Sxy150489 		    "autonegotiation with full link capabilities.");
2301*5779Sxy150489 		mac->autoneg = B_TRUE;
2302*5779Sxy150489 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
2303*5779Sxy150489 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
2304*5779Sxy150489 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
2305*5779Sxy150489 	}
2306*5779Sxy150489 
2307*5779Sxy150489 	if (setup_hw) {
2308*5779Sxy150489 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
2309*5779Sxy150489 			return (IGB_FAILURE);
2310*5779Sxy150489 	}
2311*5779Sxy150489 
2312*5779Sxy150489 	return (IGB_SUCCESS);
2313*5779Sxy150489 }
2314*5779Sxy150489 
2315*5779Sxy150489 
2316*5779Sxy150489 /*
2317*5779Sxy150489  * igb_is_link_up - Check if the link is up
2318*5779Sxy150489  */
2319*5779Sxy150489 static boolean_t
2320*5779Sxy150489 igb_is_link_up(igb_t *igb)
2321*5779Sxy150489 {
2322*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2323*5779Sxy150489 	boolean_t link_up;
2324*5779Sxy150489 
2325*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
2326*5779Sxy150489 
2327*5779Sxy150489 	(void) e1000_check_for_link(hw);
2328*5779Sxy150489 
2329*5779Sxy150489 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
2330*5779Sxy150489 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
2331*5779Sxy150489 	    (hw->mac.serdes_has_link))) {
2332*5779Sxy150489 		link_up = B_TRUE;
2333*5779Sxy150489 	} else {
2334*5779Sxy150489 		link_up = B_FALSE;
2335*5779Sxy150489 	}
2336*5779Sxy150489 
2337*5779Sxy150489 	return (link_up);
2338*5779Sxy150489 }
2339*5779Sxy150489 
2340*5779Sxy150489 /*
2341*5779Sxy150489  * igb_link_check - Link status processing
2342*5779Sxy150489  */
2343*5779Sxy150489 static boolean_t
2344*5779Sxy150489 igb_link_check(igb_t *igb)
2345*5779Sxy150489 {
2346*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2347*5779Sxy150489 	uint16_t speed = 0, duplex = 0;
2348*5779Sxy150489 	boolean_t link_changed = B_FALSE;
2349*5779Sxy150489 
2350*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
2351*5779Sxy150489 
2352*5779Sxy150489 	if (igb_is_link_up(igb)) {
2353*5779Sxy150489 		/*
2354*5779Sxy150489 		 * The Link is up, check whether it was marked as down earlier
2355*5779Sxy150489 		 */
2356*5779Sxy150489 		if (igb->link_state != LINK_STATE_UP) {
2357*5779Sxy150489 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
2358*5779Sxy150489 			igb->link_speed = speed;
2359*5779Sxy150489 			igb->link_duplex = duplex;
2360*5779Sxy150489 			igb->link_state = LINK_STATE_UP;
2361*5779Sxy150489 			igb->link_down_timeout = 0;
2362*5779Sxy150489 			link_changed = B_TRUE;
2363*5779Sxy150489 		}
2364*5779Sxy150489 	} else {
2365*5779Sxy150489 		if (igb->link_state != LINK_STATE_DOWN) {
2366*5779Sxy150489 			igb->link_speed = 0;
2367*5779Sxy150489 			igb->link_duplex = 0;
2368*5779Sxy150489 			igb->link_state = LINK_STATE_DOWN;
2369*5779Sxy150489 			link_changed = B_TRUE;
2370*5779Sxy150489 		}
2371*5779Sxy150489 
2372*5779Sxy150489 		if (igb->igb_state & IGB_STARTED) {
2373*5779Sxy150489 			if (igb->link_down_timeout < MAX_LINK_DOWN_TIMEOUT) {
2374*5779Sxy150489 				igb->link_down_timeout++;
2375*5779Sxy150489 			} else if (igb->link_down_timeout ==
2376*5779Sxy150489 			    MAX_LINK_DOWN_TIMEOUT) {
2377*5779Sxy150489 				igb_tx_clean(igb);
2378*5779Sxy150489 				igb->link_down_timeout++;
2379*5779Sxy150489 			}
2380*5779Sxy150489 		}
2381*5779Sxy150489 	}
2382*5779Sxy150489 
2383*5779Sxy150489 	return (link_changed);
2384*5779Sxy150489 }
2385*5779Sxy150489 
2386*5779Sxy150489 /*
2387*5779Sxy150489  * igb_local_timer - driver watchdog function
2388*5779Sxy150489  *
2389*5779Sxy150489  * This function will handle the transmit stall check, link status check and
2390*5779Sxy150489  * other routines.
2391*5779Sxy150489  */
2392*5779Sxy150489 static void
2393*5779Sxy150489 igb_local_timer(void *arg)
2394*5779Sxy150489 {
2395*5779Sxy150489 	igb_t *igb = (igb_t *)arg;
2396*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2397*5779Sxy150489 	boolean_t link_changed;
2398*5779Sxy150489 
2399*5779Sxy150489 	if (igb_stall_check(igb)) {
2400*5779Sxy150489 		igb->reset_count++;
2401*5779Sxy150489 		(void) igb_reset(igb);
2402*5779Sxy150489 	}
2403*5779Sxy150489 
2404*5779Sxy150489 	mutex_enter(&igb->gen_lock);
2405*5779Sxy150489 	link_changed = igb_link_check(igb);
2406*5779Sxy150489 	mutex_exit(&igb->gen_lock);
2407*5779Sxy150489 
2408*5779Sxy150489 	if (link_changed)
2409*5779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
2410*5779Sxy150489 
2411*5779Sxy150489 	/*
2412*5779Sxy150489 	 * Set Timer Interrupts
2413*5779Sxy150489 	 */
2414*5779Sxy150489 	if (igb->intr_type != DDI_INTR_TYPE_MSIX)
2415*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
2416*5779Sxy150489 
2417*5779Sxy150489 	igb_restart_watchdog_timer(igb);
2418*5779Sxy150489 }
2419*5779Sxy150489 
2420*5779Sxy150489 /*
2421*5779Sxy150489  * igb_stall_check - check for transmit stall
2422*5779Sxy150489  *
2423*5779Sxy150489  * This function checks if the adapter is stalled (in transmit).
2424*5779Sxy150489  *
2425*5779Sxy150489  * It is called each time the watchdog timeout is invoked.
2426*5779Sxy150489  * If the transmit descriptor reclaim continuously fails,
2427*5779Sxy150489  * the watchdog value will increment by 1. If the watchdog
2428*5779Sxy150489  * value exceeds the threshold, the igb is assumed to
2429*5779Sxy150489  * have stalled and need to be reset.
2430*5779Sxy150489  */
2431*5779Sxy150489 static boolean_t
2432*5779Sxy150489 igb_stall_check(igb_t *igb)
2433*5779Sxy150489 {
2434*5779Sxy150489 	igb_tx_ring_t *tx_ring;
2435*5779Sxy150489 	boolean_t result;
2436*5779Sxy150489 	int i;
2437*5779Sxy150489 
2438*5779Sxy150489 	if (igb->link_state != LINK_STATE_UP)
2439*5779Sxy150489 		return (B_FALSE);
2440*5779Sxy150489 
2441*5779Sxy150489 	/*
2442*5779Sxy150489 	 * If any tx ring is stalled, we'll reset the chipset
2443*5779Sxy150489 	 */
2444*5779Sxy150489 	result = B_FALSE;
2445*5779Sxy150489 	for (i = 0; i < igb->num_tx_rings; i++) {
2446*5779Sxy150489 		tx_ring = &igb->tx_rings[i];
2447*5779Sxy150489 
2448*5779Sxy150489 		if (tx_ring->recycle_fail > 0)
2449*5779Sxy150489 			tx_ring->stall_watchdog++;
2450*5779Sxy150489 		else
2451*5779Sxy150489 			tx_ring->stall_watchdog = 0;
2452*5779Sxy150489 
2453*5779Sxy150489 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
2454*5779Sxy150489 			result = B_TRUE;
2455*5779Sxy150489 			break;
2456*5779Sxy150489 		}
2457*5779Sxy150489 	}
2458*5779Sxy150489 
2459*5779Sxy150489 	if (result) {
2460*5779Sxy150489 		tx_ring->stall_watchdog = 0;
2461*5779Sxy150489 		tx_ring->recycle_fail = 0;
2462*5779Sxy150489 	}
2463*5779Sxy150489 
2464*5779Sxy150489 	return (result);
2465*5779Sxy150489 }
2466*5779Sxy150489 
2467*5779Sxy150489 
2468*5779Sxy150489 /*
2469*5779Sxy150489  * is_valid_mac_addr - Check if the mac address is valid
2470*5779Sxy150489  */
2471*5779Sxy150489 static boolean_t
2472*5779Sxy150489 is_valid_mac_addr(uint8_t *mac_addr)
2473*5779Sxy150489 {
2474*5779Sxy150489 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
2475*5779Sxy150489 	const uint8_t addr_test2[6] =
2476*5779Sxy150489 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2477*5779Sxy150489 
2478*5779Sxy150489 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
2479*5779Sxy150489 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
2480*5779Sxy150489 		return (B_FALSE);
2481*5779Sxy150489 
2482*5779Sxy150489 	return (B_TRUE);
2483*5779Sxy150489 }
2484*5779Sxy150489 
2485*5779Sxy150489 static boolean_t
2486*5779Sxy150489 igb_find_mac_address(igb_t *igb)
2487*5779Sxy150489 {
2488*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2489*5779Sxy150489 #ifdef __sparc
2490*5779Sxy150489 	uchar_t *bytes;
2491*5779Sxy150489 	struct ether_addr sysaddr;
2492*5779Sxy150489 	uint_t nelts;
2493*5779Sxy150489 	int err;
2494*5779Sxy150489 	boolean_t found = B_FALSE;
2495*5779Sxy150489 
2496*5779Sxy150489 	/*
2497*5779Sxy150489 	 * The "vendor's factory-set address" may already have
2498*5779Sxy150489 	 * been extracted from the chip, but if the property
2499*5779Sxy150489 	 * "local-mac-address" is set we use that instead.
2500*5779Sxy150489 	 *
2501*5779Sxy150489 	 * We check whether it looks like an array of 6
2502*5779Sxy150489 	 * bytes (which it should, if OBP set it).  If we can't
2503*5779Sxy150489 	 * make sense of it this way, we'll ignore it.
2504*5779Sxy150489 	 */
2505*5779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
2506*5779Sxy150489 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
2507*5779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
2508*5779Sxy150489 		if (nelts == ETHERADDRL) {
2509*5779Sxy150489 			while (nelts--)
2510*5779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
2511*5779Sxy150489 			found = B_TRUE;
2512*5779Sxy150489 		}
2513*5779Sxy150489 		ddi_prop_free(bytes);
2514*5779Sxy150489 	}
2515*5779Sxy150489 
2516*5779Sxy150489 	/*
2517*5779Sxy150489 	 * Look up the OBP property "local-mac-address?". If the user has set
2518*5779Sxy150489 	 * 'local-mac-address? = false', use "the system address" instead.
2519*5779Sxy150489 	 */
2520*5779Sxy150489 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
2521*5779Sxy150489 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
2522*5779Sxy150489 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
2523*5779Sxy150489 			if (localetheraddr(NULL, &sysaddr) != 0) {
2524*5779Sxy150489 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
2525*5779Sxy150489 				found = B_TRUE;
2526*5779Sxy150489 			}
2527*5779Sxy150489 		}
2528*5779Sxy150489 		ddi_prop_free(bytes);
2529*5779Sxy150489 	}
2530*5779Sxy150489 
2531*5779Sxy150489 	/*
2532*5779Sxy150489 	 * Finally(!), if there's a valid "mac-address" property (created
2533*5779Sxy150489 	 * if we netbooted from this interface), we must use this instead
2534*5779Sxy150489 	 * of any of the above to ensure that the NFS/install server doesn't
2535*5779Sxy150489 	 * get confused by the address changing as Solaris takes over!
2536*5779Sxy150489 	 */
2537*5779Sxy150489 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
2538*5779Sxy150489 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
2539*5779Sxy150489 	if (err == DDI_PROP_SUCCESS) {
2540*5779Sxy150489 		if (nelts == ETHERADDRL) {
2541*5779Sxy150489 			while (nelts--)
2542*5779Sxy150489 				hw->mac.addr[nelts] = bytes[nelts];
2543*5779Sxy150489 			found = B_TRUE;
2544*5779Sxy150489 		}
2545*5779Sxy150489 		ddi_prop_free(bytes);
2546*5779Sxy150489 	}
2547*5779Sxy150489 
2548*5779Sxy150489 	if (found) {
2549*5779Sxy150489 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
2550*5779Sxy150489 		return (B_TRUE);
2551*5779Sxy150489 	}
2552*5779Sxy150489 #endif
2553*5779Sxy150489 
2554*5779Sxy150489 	/*
2555*5779Sxy150489 	 * Read the device MAC address from the EEPROM
2556*5779Sxy150489 	 */
2557*5779Sxy150489 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
2558*5779Sxy150489 		return (B_FALSE);
2559*5779Sxy150489 
2560*5779Sxy150489 	return (B_TRUE);
2561*5779Sxy150489 }
2562*5779Sxy150489 
2563*5779Sxy150489 #pragma inline(igb_arm_watchdog_timer)
2564*5779Sxy150489 
2565*5779Sxy150489 static void
2566*5779Sxy150489 igb_arm_watchdog_timer(igb_t *igb)
2567*5779Sxy150489 {
2568*5779Sxy150489 	/*
2569*5779Sxy150489 	 * Fire a watchdog timer
2570*5779Sxy150489 	 */
2571*5779Sxy150489 	igb->watchdog_tid =
2572*5779Sxy150489 	    timeout(igb_local_timer,
2573*5779Sxy150489 	    (void *)igb, 1 * drv_usectohz(1000000));
2574*5779Sxy150489 
2575*5779Sxy150489 }
2576*5779Sxy150489 
2577*5779Sxy150489 /*
2578*5779Sxy150489  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
2579*5779Sxy150489  */
2580*5779Sxy150489 void
2581*5779Sxy150489 igb_enable_watchdog_timer(igb_t *igb)
2582*5779Sxy150489 {
2583*5779Sxy150489 	mutex_enter(&igb->watchdog_lock);
2584*5779Sxy150489 
2585*5779Sxy150489 	if (!igb->watchdog_enable) {
2586*5779Sxy150489 		igb->watchdog_enable = B_TRUE;
2587*5779Sxy150489 		igb->watchdog_start = B_TRUE;
2588*5779Sxy150489 		igb_arm_watchdog_timer(igb);
2589*5779Sxy150489 	}
2590*5779Sxy150489 
2591*5779Sxy150489 	mutex_exit(&igb->watchdog_lock);
2592*5779Sxy150489 
2593*5779Sxy150489 }
2594*5779Sxy150489 
2595*5779Sxy150489 /*
2596*5779Sxy150489  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
2597*5779Sxy150489  */
2598*5779Sxy150489 void
2599*5779Sxy150489 igb_disable_watchdog_timer(igb_t *igb)
2600*5779Sxy150489 {
2601*5779Sxy150489 	timeout_id_t tid;
2602*5779Sxy150489 
2603*5779Sxy150489 	mutex_enter(&igb->watchdog_lock);
2604*5779Sxy150489 
2605*5779Sxy150489 	igb->watchdog_enable = B_FALSE;
2606*5779Sxy150489 	igb->watchdog_start = B_FALSE;
2607*5779Sxy150489 	tid = igb->watchdog_tid;
2608*5779Sxy150489 	igb->watchdog_tid = 0;
2609*5779Sxy150489 
2610*5779Sxy150489 	mutex_exit(&igb->watchdog_lock);
2611*5779Sxy150489 
2612*5779Sxy150489 	if (tid != 0)
2613*5779Sxy150489 		(void) untimeout(tid);
2614*5779Sxy150489 
2615*5779Sxy150489 }
2616*5779Sxy150489 
2617*5779Sxy150489 /*
2618*5779Sxy150489  * igb_start_watchdog_timer - Start the driver watchdog timer
2619*5779Sxy150489  */
2620*5779Sxy150489 static void
2621*5779Sxy150489 igb_start_watchdog_timer(igb_t *igb)
2622*5779Sxy150489 {
2623*5779Sxy150489 	mutex_enter(&igb->watchdog_lock);
2624*5779Sxy150489 
2625*5779Sxy150489 	if (igb->watchdog_enable) {
2626*5779Sxy150489 		if (!igb->watchdog_start) {
2627*5779Sxy150489 			igb->watchdog_start = B_TRUE;
2628*5779Sxy150489 			igb_arm_watchdog_timer(igb);
2629*5779Sxy150489 		}
2630*5779Sxy150489 	}
2631*5779Sxy150489 
2632*5779Sxy150489 	mutex_exit(&igb->watchdog_lock);
2633*5779Sxy150489 }
2634*5779Sxy150489 
2635*5779Sxy150489 /*
2636*5779Sxy150489  * igb_restart_watchdog_timer - Restart the driver watchdog timer
2637*5779Sxy150489  */
2638*5779Sxy150489 static void
2639*5779Sxy150489 igb_restart_watchdog_timer(igb_t *igb)
2640*5779Sxy150489 {
2641*5779Sxy150489 	mutex_enter(&igb->watchdog_lock);
2642*5779Sxy150489 
2643*5779Sxy150489 	if (igb->watchdog_start)
2644*5779Sxy150489 		igb_arm_watchdog_timer(igb);
2645*5779Sxy150489 
2646*5779Sxy150489 	mutex_exit(&igb->watchdog_lock);
2647*5779Sxy150489 }
2648*5779Sxy150489 
2649*5779Sxy150489 /*
2650*5779Sxy150489  * igb_stop_watchdog_timer - Stop the driver watchdog timer
2651*5779Sxy150489  */
2652*5779Sxy150489 static void
2653*5779Sxy150489 igb_stop_watchdog_timer(igb_t *igb)
2654*5779Sxy150489 {
2655*5779Sxy150489 	timeout_id_t tid;
2656*5779Sxy150489 
2657*5779Sxy150489 	mutex_enter(&igb->watchdog_lock);
2658*5779Sxy150489 
2659*5779Sxy150489 	igb->watchdog_start = B_FALSE;
2660*5779Sxy150489 	tid = igb->watchdog_tid;
2661*5779Sxy150489 	igb->watchdog_tid = 0;
2662*5779Sxy150489 
2663*5779Sxy150489 	mutex_exit(&igb->watchdog_lock);
2664*5779Sxy150489 
2665*5779Sxy150489 	if (tid != 0)
2666*5779Sxy150489 		(void) untimeout(tid);
2667*5779Sxy150489 }
2668*5779Sxy150489 
2669*5779Sxy150489 /*
2670*5779Sxy150489  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
2671*5779Sxy150489  */
2672*5779Sxy150489 static void
2673*5779Sxy150489 igb_disable_adapter_interrupts(igb_t *igb)
2674*5779Sxy150489 {
2675*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2676*5779Sxy150489 
2677*5779Sxy150489 	/*
2678*5779Sxy150489 	 * Set the IMC register to mask all the interrupts,
2679*5779Sxy150489 	 * including the tx interrupts.
2680*5779Sxy150489 	 */
2681*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_IMC, 0xffffffff);
2682*5779Sxy150489 
2683*5779Sxy150489 	/*
2684*5779Sxy150489 	 * Additional disabling for MSI-X
2685*5779Sxy150489 	 */
2686*5779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
2687*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMC, 0xffffffff);
2688*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, 0x0);
2689*5779Sxy150489 	}
2690*5779Sxy150489 
2691*5779Sxy150489 	E1000_WRITE_FLUSH(hw);
2692*5779Sxy150489 }
2693*5779Sxy150489 
2694*5779Sxy150489 /*
2695*5779Sxy150489  * igb_enable_adapter_interrupts - Mask/enable all hardware interrupts
2696*5779Sxy150489  */
2697*5779Sxy150489 static void
2698*5779Sxy150489 igb_enable_adapter_interrupts(igb_t *igb)
2699*5779Sxy150489 {
2700*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
2701*5779Sxy150489 	uint32_t reg;
2702*5779Sxy150489 
2703*5779Sxy150489 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
2704*5779Sxy150489 		/* Interrupt enabling for MSI-X */
2705*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
2706*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
2707*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
2708*5779Sxy150489 
2709*5779Sxy150489 		/* Enable MSI-X PBA support */
2710*5779Sxy150489 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
2711*5779Sxy150489 		reg |= E1000_CTRL_EXT_PBA_CLR;
2712*5779Sxy150489 
2713*5779Sxy150489 		/* Non-selective interrupt clear-on-read */
2714*5779Sxy150489 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
2715*5779Sxy150489 
2716*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
2717*5779Sxy150489 	} else {
2718*5779Sxy150489 		/* Interrupt enabling for MSI and legacy */
2719*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
2720*5779Sxy150489 	}
2721*5779Sxy150489 
2722*5779Sxy150489 	E1000_WRITE_FLUSH(hw);
2723*5779Sxy150489 }
2724*5779Sxy150489 
2725*5779Sxy150489 /*
2726*5779Sxy150489  * Loopback Support
2727*5779Sxy150489  */
2728*5779Sxy150489 static lb_property_t lb_normal =
2729*5779Sxy150489 	{ normal,	"normal",	IGB_LB_NONE		};
2730*5779Sxy150489 static lb_property_t lb_external =
2731*5779Sxy150489 	{ external,	"External",	IGB_LB_EXTERNAL		};
2732*5779Sxy150489 static lb_property_t lb_mac =
2733*5779Sxy150489 	{ internal,	"MAC",		IGB_LB_INTERNAL_MAC	};
2734*5779Sxy150489 static lb_property_t lb_phy =
2735*5779Sxy150489 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
2736*5779Sxy150489 static lb_property_t lb_serdes =
2737*5779Sxy150489 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
2738*5779Sxy150489 
2739*5779Sxy150489 enum ioc_reply
2740*5779Sxy150489 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
2741*5779Sxy150489 {
2742*5779Sxy150489 	lb_info_sz_t *lbsp;
2743*5779Sxy150489 	lb_property_t *lbpp;
2744*5779Sxy150489 	struct e1000_hw *hw;
2745*5779Sxy150489 	uint32_t *lbmp;
2746*5779Sxy150489 	uint32_t size;
2747*5779Sxy150489 	uint32_t value;
2748*5779Sxy150489 
2749*5779Sxy150489 	hw = &igb->hw;
2750*5779Sxy150489 
2751*5779Sxy150489 	if (mp->b_cont == NULL)
2752*5779Sxy150489 		return (IOC_INVAL);
2753*5779Sxy150489 
2754*5779Sxy150489 	switch (iocp->ioc_cmd) {
2755*5779Sxy150489 	default:
2756*5779Sxy150489 		return (IOC_INVAL);
2757*5779Sxy150489 
2758*5779Sxy150489 	case LB_GET_INFO_SIZE:
2759*5779Sxy150489 		size = sizeof (lb_info_sz_t);
2760*5779Sxy150489 		if (iocp->ioc_count != size)
2761*5779Sxy150489 			return (IOC_INVAL);
2762*5779Sxy150489 
2763*5779Sxy150489 		value = sizeof (lb_normal);
2764*5779Sxy150489 		value += sizeof (lb_mac);
2765*5779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
2766*5779Sxy150489 			value += sizeof (lb_phy);
2767*5779Sxy150489 		else
2768*5779Sxy150489 			value += sizeof (lb_serdes);
2769*5779Sxy150489 		value += sizeof (lb_external);
2770*5779Sxy150489 
2771*5779Sxy150489 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
2772*5779Sxy150489 		*lbsp = value;
2773*5779Sxy150489 		break;
2774*5779Sxy150489 
2775*5779Sxy150489 	case LB_GET_INFO:
2776*5779Sxy150489 		value = sizeof (lb_normal);
2777*5779Sxy150489 		value += sizeof (lb_mac);
2778*5779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
2779*5779Sxy150489 			value += sizeof (lb_phy);
2780*5779Sxy150489 		else
2781*5779Sxy150489 			value += sizeof (lb_serdes);
2782*5779Sxy150489 		value += sizeof (lb_external);
2783*5779Sxy150489 
2784*5779Sxy150489 		size = value;
2785*5779Sxy150489 		if (iocp->ioc_count != size)
2786*5779Sxy150489 			return (IOC_INVAL);
2787*5779Sxy150489 
2788*5779Sxy150489 		value = 0;
2789*5779Sxy150489 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
2790*5779Sxy150489 
2791*5779Sxy150489 		lbpp[value++] = lb_normal;
2792*5779Sxy150489 		lbpp[value++] = lb_mac;
2793*5779Sxy150489 		if (hw->phy.media_type == e1000_media_type_copper)
2794*5779Sxy150489 			lbpp[value++] = lb_phy;
2795*5779Sxy150489 		else
2796*5779Sxy150489 			lbpp[value++] = lb_serdes;
2797*5779Sxy150489 		lbpp[value++] = lb_external;
2798*5779Sxy150489 		break;
2799*5779Sxy150489 
2800*5779Sxy150489 	case LB_GET_MODE:
2801*5779Sxy150489 		size = sizeof (uint32_t);
2802*5779Sxy150489 		if (iocp->ioc_count != size)
2803*5779Sxy150489 			return (IOC_INVAL);
2804*5779Sxy150489 
2805*5779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
2806*5779Sxy150489 		*lbmp = igb->loopback_mode;
2807*5779Sxy150489 		break;
2808*5779Sxy150489 
2809*5779Sxy150489 	case LB_SET_MODE:
2810*5779Sxy150489 		size = 0;
2811*5779Sxy150489 		if (iocp->ioc_count != sizeof (uint32_t))
2812*5779Sxy150489 			return (IOC_INVAL);
2813*5779Sxy150489 
2814*5779Sxy150489 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
2815*5779Sxy150489 		if (!igb_set_loopback_mode(igb, *lbmp))
2816*5779Sxy150489 			return (IOC_INVAL);
2817*5779Sxy150489 		break;
2818*5779Sxy150489 	}
2819*5779Sxy150489 
2820*5779Sxy150489 	iocp->ioc_count = size;
2821*5779Sxy150489 	iocp->ioc_error = 0;
2822*5779Sxy150489 
2823*5779Sxy150489 	return (IOC_REPLY);
2824*5779Sxy150489 }
2825*5779Sxy150489 
2826*5779Sxy150489 /*
2827*5779Sxy150489  * igb_set_loopback_mode - Setup loopback based on the loopback mode
2828*5779Sxy150489  */
2829*5779Sxy150489 static boolean_t
2830*5779Sxy150489 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
2831*5779Sxy150489 {
2832*5779Sxy150489 	struct e1000_hw *hw;
2833*5779Sxy150489 
2834*5779Sxy150489 	if (mode == igb->loopback_mode)
2835*5779Sxy150489 		return (B_TRUE);
2836*5779Sxy150489 
2837*5779Sxy150489 	hw = &igb->hw;
2838*5779Sxy150489 
2839*5779Sxy150489 	igb->loopback_mode = mode;
2840*5779Sxy150489 
2841*5779Sxy150489 	if (mode == IGB_LB_NONE) {
2842*5779Sxy150489 		/* Reset the chip */
2843*5779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_TRUE;
2844*5779Sxy150489 		(void) igb_reset(igb);
2845*5779Sxy150489 		hw->phy.autoneg_wait_to_complete = B_FALSE;
2846*5779Sxy150489 		return (B_TRUE);
2847*5779Sxy150489 	}
2848*5779Sxy150489 
2849*5779Sxy150489 	mutex_enter(&igb->gen_lock);
2850*5779Sxy150489 
2851*5779Sxy150489 	switch (mode) {
2852*5779Sxy150489 	default:
2853*5779Sxy150489 		mutex_exit(&igb->gen_lock);
2854*5779Sxy150489 		return (B_FALSE);
2855*5779Sxy150489 
2856*5779Sxy150489 	case IGB_LB_EXTERNAL:
2857*5779Sxy150489 		igb_set_external_loopback(igb);
2858*5779Sxy150489 		break;
2859*5779Sxy150489 
2860*5779Sxy150489 	case IGB_LB_INTERNAL_MAC:
2861*5779Sxy150489 		igb_set_internal_mac_loopback(igb);
2862*5779Sxy150489 		break;
2863*5779Sxy150489 
2864*5779Sxy150489 	case IGB_LB_INTERNAL_PHY:
2865*5779Sxy150489 		igb_set_internal_phy_loopback(igb);
2866*5779Sxy150489 		break;
2867*5779Sxy150489 
2868*5779Sxy150489 	case IGB_LB_INTERNAL_SERDES:
2869*5779Sxy150489 		igb_set_internal_serdes_loopback(igb);
2870*5779Sxy150489 		break;
2871*5779Sxy150489 	}
2872*5779Sxy150489 
2873*5779Sxy150489 	mutex_exit(&igb->gen_lock);
2874*5779Sxy150489 
2875*5779Sxy150489 	return (B_TRUE);
2876*5779Sxy150489 }
2877*5779Sxy150489 
2878*5779Sxy150489 /*
2879*5779Sxy150489  * igb_set_external_loopback - Set the external loopback mode
2880*5779Sxy150489  */
2881*5779Sxy150489 static void
2882*5779Sxy150489 igb_set_external_loopback(igb_t *igb)
2883*5779Sxy150489 {
2884*5779Sxy150489 	struct e1000_hw *hw;
2885*5779Sxy150489 
2886*5779Sxy150489 	hw = &igb->hw;
2887*5779Sxy150489 
2888*5779Sxy150489 	/* Set phy to known state */
2889*5779Sxy150489 	(void) e1000_phy_hw_reset(hw);
2890*5779Sxy150489 
2891*5779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
2892*5779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x9, 0x1b00);
2893*5779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
2894*5779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
2895*5779Sxy150489 }
2896*5779Sxy150489 
2897*5779Sxy150489 /*
2898*5779Sxy150489  * igb_set_internal_mac_loopback - Set the internal MAC loopback mode
2899*5779Sxy150489  */
2900*5779Sxy150489 static void
2901*5779Sxy150489 igb_set_internal_mac_loopback(igb_t *igb)
2902*5779Sxy150489 {
2903*5779Sxy150489 	struct e1000_hw *hw;
2904*5779Sxy150489 	uint32_t ctrl;
2905*5779Sxy150489 	uint32_t rctl;
2906*5779Sxy150489 
2907*5779Sxy150489 	hw = &igb->hw;
2908*5779Sxy150489 
2909*5779Sxy150489 	/* Set the Receive Control register */
2910*5779Sxy150489 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2911*5779Sxy150489 	rctl &= ~E1000_RCTL_LBM_TCVR;
2912*5779Sxy150489 	rctl |= E1000_RCTL_LBM_MAC;
2913*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2914*5779Sxy150489 
2915*5779Sxy150489 	/* Set the Device Control register */
2916*5779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
2917*5779Sxy150489 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
2918*5779Sxy150489 	ctrl |= (E1000_CTRL_SLU |	/* Force link up */
2919*5779Sxy150489 	    E1000_CTRL_FRCSPD |		/* Force speed */
2920*5779Sxy150489 	    E1000_CTRL_FRCDPX |		/* Force duplex */
2921*5779Sxy150489 	    E1000_CTRL_SPD_1000 |	/* Force speed to 1000 */
2922*5779Sxy150489 	    E1000_CTRL_FD);		/* Force full duplex */
2923*5779Sxy150489 	ctrl &= ~E1000_CTRL_ILOS;	/* Clear ILOS when there's a link */
2924*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2925*5779Sxy150489 }
2926*5779Sxy150489 
2927*5779Sxy150489 /*
2928*5779Sxy150489  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
2929*5779Sxy150489  */
2930*5779Sxy150489 static void
2931*5779Sxy150489 igb_set_internal_phy_loopback(igb_t *igb)
2932*5779Sxy150489 {
2933*5779Sxy150489 	struct e1000_hw *hw;
2934*5779Sxy150489 	uint32_t ctrl_ext;
2935*5779Sxy150489 	uint16_t phy_ctrl;
2936*5779Sxy150489 	uint16_t phy_pconf;
2937*5779Sxy150489 
2938*5779Sxy150489 	hw = &igb->hw;
2939*5779Sxy150489 
2940*5779Sxy150489 	/* Set link mode to PHY (00b) in the Extended Control register */
2941*5779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
2942*5779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
2943*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
2944*5779Sxy150489 
2945*5779Sxy150489 	/*
2946*5779Sxy150489 	 * Set PHY control register (0x4140):
2947*5779Sxy150489 	 *    Set full duplex mode
2948*5779Sxy150489 	 *    Set loopback bit
2949*5779Sxy150489 	 *    Clear auto-neg enable bit
2950*5779Sxy150489 	 *    Set PHY speed
2951*5779Sxy150489 	 */
2952*5779Sxy150489 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
2953*5779Sxy150489 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
2954*5779Sxy150489 
2955*5779Sxy150489 	/* Set the link disable bit in the Port Configuration register */
2956*5779Sxy150489 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
2957*5779Sxy150489 	phy_pconf |= (uint16_t)1 << 14;
2958*5779Sxy150489 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
2959*5779Sxy150489 }
2960*5779Sxy150489 
2961*5779Sxy150489 /*
2962*5779Sxy150489  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
2963*5779Sxy150489  */
2964*5779Sxy150489 static void
2965*5779Sxy150489 igb_set_internal_serdes_loopback(igb_t *igb)
2966*5779Sxy150489 {
2967*5779Sxy150489 	struct e1000_hw *hw;
2968*5779Sxy150489 	uint32_t ctrl_ext;
2969*5779Sxy150489 	uint32_t ctrl;
2970*5779Sxy150489 	uint32_t pcs_lctl;
2971*5779Sxy150489 	uint32_t connsw;
2972*5779Sxy150489 
2973*5779Sxy150489 	hw = &igb->hw;
2974*5779Sxy150489 
2975*5779Sxy150489 	/* Set link mode to SerDes (11b) in the Extended Control register */
2976*5779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
2977*5779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
2978*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
2979*5779Sxy150489 
2980*5779Sxy150489 	/* Configure the SerDes to loopback */
2981*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
2982*5779Sxy150489 
2983*5779Sxy150489 	/* Set Device Control register */
2984*5779Sxy150489 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
2985*5779Sxy150489 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
2986*5779Sxy150489 	    E1000_CTRL_SLU);		/* Force link up */
2987*5779Sxy150489 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
2988*5779Sxy150489 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
2989*5779Sxy150489 	    E1000_CTRL_LRST);		/* Clear link reset */
2990*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2991*5779Sxy150489 
2992*5779Sxy150489 	/* Set PCS Link Control register */
2993*5779Sxy150489 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
2994*5779Sxy150489 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
2995*5779Sxy150489 	    E1000_PCS_LCTL_FSD |
2996*5779Sxy150489 	    E1000_PCS_LCTL_FDV_FULL |
2997*5779Sxy150489 	    E1000_PCS_LCTL_FLV_LINK_UP);
2998*5779Sxy150489 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
2999*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
3000*5779Sxy150489 
3001*5779Sxy150489 	/* Set the Copper/Fiber Switch Control - CONNSW register */
3002*5779Sxy150489 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
3003*5779Sxy150489 	connsw &= ~E1000_CONNSW_ENRGSRC;
3004*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
3005*5779Sxy150489 }
3006*5779Sxy150489 
3007*5779Sxy150489 #pragma inline(igb_intr_rx_work)
3008*5779Sxy150489 /*
3009*5779Sxy150489  * igb_intr_rx_work - rx processing of ISR
3010*5779Sxy150489  */
3011*5779Sxy150489 static void
3012*5779Sxy150489 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
3013*5779Sxy150489 {
3014*5779Sxy150489 	mblk_t *mp;
3015*5779Sxy150489 
3016*5779Sxy150489 	mutex_enter(&rx_ring->rx_lock);
3017*5779Sxy150489 	mp = igb_rx(rx_ring);
3018*5779Sxy150489 	mutex_exit(&rx_ring->rx_lock);
3019*5779Sxy150489 
3020*5779Sxy150489 	if (mp != NULL)
3021*5779Sxy150489 		mac_rx(rx_ring->igb->mac_hdl, NULL, mp);
3022*5779Sxy150489 }
3023*5779Sxy150489 
3024*5779Sxy150489 #pragma inline(igb_intr_tx_work)
3025*5779Sxy150489 /*
3026*5779Sxy150489  * igb_intr_tx_work - tx processing of ISR
3027*5779Sxy150489  */
3028*5779Sxy150489 static void
3029*5779Sxy150489 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
3030*5779Sxy150489 {
3031*5779Sxy150489 	/* Recycle the tx descriptors */
3032*5779Sxy150489 	tx_ring->tx_recycle(tx_ring);
3033*5779Sxy150489 
3034*5779Sxy150489 	/* Schedule the re-transmit */
3035*5779Sxy150489 	if (tx_ring->reschedule &&
3036*5779Sxy150489 	    (tx_ring->tbd_free >= tx_ring->resched_thresh)) {
3037*5779Sxy150489 		tx_ring->reschedule = B_FALSE;
3038*5779Sxy150489 		mac_tx_update(tx_ring->igb->mac_hdl);
3039*5779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
3040*5779Sxy150489 	}
3041*5779Sxy150489 }
3042*5779Sxy150489 
3043*5779Sxy150489 #pragma inline(igb_intr_other_work)
3044*5779Sxy150489 /*
3045*5779Sxy150489  * igb_intr_other_work - other processing of ISR
3046*5779Sxy150489  */
3047*5779Sxy150489 static void
3048*5779Sxy150489 igb_intr_other_work(igb_t *igb)
3049*5779Sxy150489 {
3050*5779Sxy150489 	boolean_t link_changed;
3051*5779Sxy150489 
3052*5779Sxy150489 	igb_stop_watchdog_timer(igb);
3053*5779Sxy150489 
3054*5779Sxy150489 	mutex_enter(&igb->gen_lock);
3055*5779Sxy150489 
3056*5779Sxy150489 	/*
3057*5779Sxy150489 	 * Because we got a link-status-change interrupt, force
3058*5779Sxy150489 	 * e1000_check_for_link() to look at phy
3059*5779Sxy150489 	 */
3060*5779Sxy150489 	igb->hw.mac.get_link_status = B_TRUE;
3061*5779Sxy150489 
3062*5779Sxy150489 	/* igb_link_check takes care of link status change */
3063*5779Sxy150489 	link_changed = igb_link_check(igb);
3064*5779Sxy150489 
3065*5779Sxy150489 	/* Get new phy state */
3066*5779Sxy150489 	igb_get_phy_state(igb);
3067*5779Sxy150489 
3068*5779Sxy150489 	mutex_exit(&igb->gen_lock);
3069*5779Sxy150489 
3070*5779Sxy150489 	if (link_changed)
3071*5779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
3072*5779Sxy150489 
3073*5779Sxy150489 	igb_start_watchdog_timer(igb);
3074*5779Sxy150489 }
3075*5779Sxy150489 
3076*5779Sxy150489 /*
3077*5779Sxy150489  * igb_intr_legacy - Interrupt handler for legacy interrupts
3078*5779Sxy150489  */
3079*5779Sxy150489 static uint_t
3080*5779Sxy150489 igb_intr_legacy(void *arg1, void *arg2)
3081*5779Sxy150489 {
3082*5779Sxy150489 	igb_t *igb = (igb_t *)arg1;
3083*5779Sxy150489 	igb_tx_ring_t *tx_ring;
3084*5779Sxy150489 	uint32_t icr;
3085*5779Sxy150489 	mblk_t *mp;
3086*5779Sxy150489 	boolean_t tx_reschedule;
3087*5779Sxy150489 	boolean_t link_changed;
3088*5779Sxy150489 	uint_t result;
3089*5779Sxy150489 
3090*5779Sxy150489 	_NOTE(ARGUNUSED(arg2));
3091*5779Sxy150489 
3092*5779Sxy150489 	mutex_enter(&igb->gen_lock);
3093*5779Sxy150489 
3094*5779Sxy150489 	if (igb->igb_state & IGB_SUSPENDED) {
3095*5779Sxy150489 		mutex_exit(&igb->gen_lock);
3096*5779Sxy150489 		return (DDI_INTR_UNCLAIMED);
3097*5779Sxy150489 	}
3098*5779Sxy150489 
3099*5779Sxy150489 	mp = NULL;
3100*5779Sxy150489 	tx_reschedule = B_FALSE;
3101*5779Sxy150489 	link_changed = B_FALSE;
3102*5779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
3103*5779Sxy150489 
3104*5779Sxy150489 	if (icr & E1000_ICR_INT_ASSERTED) {
3105*5779Sxy150489 		/*
3106*5779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was set:
3107*5779Sxy150489 		 * Read(Clear) the ICR, claim this interrupt,
3108*5779Sxy150489 		 * look for work to do.
3109*5779Sxy150489 		 */
3110*5779Sxy150489 		ASSERT(igb->num_rx_rings == 1);
3111*5779Sxy150489 		ASSERT(igb->num_tx_rings == 1);
3112*5779Sxy150489 
3113*5779Sxy150489 		if (icr & E1000_ICR_RXT0) {
3114*5779Sxy150489 			mp = igb_rx(&igb->rx_rings[0]);
3115*5779Sxy150489 		}
3116*5779Sxy150489 
3117*5779Sxy150489 		if (icr & E1000_ICR_TXDW) {
3118*5779Sxy150489 			tx_ring = &igb->tx_rings[0];
3119*5779Sxy150489 
3120*5779Sxy150489 			/* Recycle the tx descriptors */
3121*5779Sxy150489 			tx_ring->tx_recycle(tx_ring);
3122*5779Sxy150489 
3123*5779Sxy150489 			/* Schedule the re-transmit */
3124*5779Sxy150489 			tx_reschedule = (tx_ring->reschedule &&
3125*5779Sxy150489 			    (tx_ring->tbd_free >= tx_ring->resched_thresh));
3126*5779Sxy150489 		}
3127*5779Sxy150489 
3128*5779Sxy150489 		if (icr & E1000_ICR_LSC) {
3129*5779Sxy150489 			/*
3130*5779Sxy150489 			 * Because we got a link-status-change interrupt, force
3131*5779Sxy150489 			 * e1000_check_for_link() to look at phy
3132*5779Sxy150489 			 */
3133*5779Sxy150489 			igb->hw.mac.get_link_status = B_TRUE;
3134*5779Sxy150489 
3135*5779Sxy150489 			/* igb_link_check takes care of link status change */
3136*5779Sxy150489 			link_changed = igb_link_check(igb);
3137*5779Sxy150489 
3138*5779Sxy150489 			/* Get new phy state */
3139*5779Sxy150489 			igb_get_phy_state(igb);
3140*5779Sxy150489 		}
3141*5779Sxy150489 
3142*5779Sxy150489 		result = DDI_INTR_CLAIMED;
3143*5779Sxy150489 	} else {
3144*5779Sxy150489 		/*
3145*5779Sxy150489 		 * E1000_ICR_INT_ASSERTED bit was not set:
3146*5779Sxy150489 		 * Don't claim this interrupt.
3147*5779Sxy150489 		 */
3148*5779Sxy150489 		result = DDI_INTR_UNCLAIMED;
3149*5779Sxy150489 	}
3150*5779Sxy150489 
3151*5779Sxy150489 	mutex_exit(&igb->gen_lock);
3152*5779Sxy150489 
3153*5779Sxy150489 	/*
3154*5779Sxy150489 	 * Do the following work outside of the gen_lock
3155*5779Sxy150489 	 */
3156*5779Sxy150489 	if (mp != NULL)
3157*5779Sxy150489 		mac_rx(igb->mac_hdl, NULL, mp);
3158*5779Sxy150489 
3159*5779Sxy150489 	if (tx_reschedule)  {
3160*5779Sxy150489 		tx_ring->reschedule = B_FALSE;
3161*5779Sxy150489 		mac_tx_update(igb->mac_hdl);
3162*5779Sxy150489 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
3163*5779Sxy150489 	}
3164*5779Sxy150489 
3165*5779Sxy150489 	if (link_changed)
3166*5779Sxy150489 		mac_link_update(igb->mac_hdl, igb->link_state);
3167*5779Sxy150489 
3168*5779Sxy150489 	return (result);
3169*5779Sxy150489 }
3170*5779Sxy150489 
3171*5779Sxy150489 /*
3172*5779Sxy150489  * igb_intr_msi - Interrupt handler for MSI
3173*5779Sxy150489  */
3174*5779Sxy150489 static uint_t
3175*5779Sxy150489 igb_intr_msi(void *arg1, void *arg2)
3176*5779Sxy150489 {
3177*5779Sxy150489 	igb_t *igb = (igb_t *)arg1;
3178*5779Sxy150489 	uint32_t icr;
3179*5779Sxy150489 
3180*5779Sxy150489 	_NOTE(ARGUNUSED(arg2));
3181*5779Sxy150489 
3182*5779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
3183*5779Sxy150489 
3184*5779Sxy150489 	/*
3185*5779Sxy150489 	 * For MSI interrupt, we have only one vector,
3186*5779Sxy150489 	 * so we have only one rx ring and one tx ring enabled.
3187*5779Sxy150489 	 */
3188*5779Sxy150489 	ASSERT(igb->num_rx_rings == 1);
3189*5779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
3190*5779Sxy150489 
3191*5779Sxy150489 	if (icr & E1000_ICR_RXT0) {
3192*5779Sxy150489 		igb_intr_rx_work(&igb->rx_rings[0]);
3193*5779Sxy150489 	}
3194*5779Sxy150489 
3195*5779Sxy150489 	if (icr & E1000_ICR_TXDW) {
3196*5779Sxy150489 		igb_intr_tx_work(&igb->tx_rings[0]);
3197*5779Sxy150489 	}
3198*5779Sxy150489 
3199*5779Sxy150489 	if (icr & E1000_ICR_LSC) {
3200*5779Sxy150489 		igb_intr_other_work(igb);
3201*5779Sxy150489 	}
3202*5779Sxy150489 
3203*5779Sxy150489 	return (DDI_INTR_CLAIMED);
3204*5779Sxy150489 }
3205*5779Sxy150489 
3206*5779Sxy150489 /*
3207*5779Sxy150489  * igb_intr_rx - Interrupt handler for rx
3208*5779Sxy150489  */
3209*5779Sxy150489 static uint_t
3210*5779Sxy150489 igb_intr_rx(void *arg1, void *arg2)
3211*5779Sxy150489 {
3212*5779Sxy150489 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
3213*5779Sxy150489 
3214*5779Sxy150489 	_NOTE(ARGUNUSED(arg2));
3215*5779Sxy150489 
3216*5779Sxy150489 	/*
3217*5779Sxy150489 	 * Only used via MSI-X vector so don't check cause bits
3218*5779Sxy150489 	 * and only clean the given ring.
3219*5779Sxy150489 	 */
3220*5779Sxy150489 	igb_intr_rx_work(rx_ring);
3221*5779Sxy150489 
3222*5779Sxy150489 	return (DDI_INTR_CLAIMED);
3223*5779Sxy150489 }
3224*5779Sxy150489 
3225*5779Sxy150489 /*
3226*5779Sxy150489  * igb_intr_tx_other - Interrupt handler for both tx and other
3227*5779Sxy150489  *
3228*5779Sxy150489  * Always look for Tx cleanup work.  Only look for other work if the right
3229*5779Sxy150489  * bits are set in the Interrupt Cause Register.
3230*5779Sxy150489  */
3231*5779Sxy150489 static uint_t
3232*5779Sxy150489 igb_intr_tx_other(void *arg1, void *arg2)
3233*5779Sxy150489 {
3234*5779Sxy150489 	igb_t *igb = (igb_t *)arg1;
3235*5779Sxy150489 	uint32_t icr;
3236*5779Sxy150489 
3237*5779Sxy150489 	_NOTE(ARGUNUSED(arg2));
3238*5779Sxy150489 
3239*5779Sxy150489 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
3240*5779Sxy150489 
3241*5779Sxy150489 	/*
3242*5779Sxy150489 	 * Always look for Tx cleanup work.  We don't have separate
3243*5779Sxy150489 	 * transmit vectors, so we have only one tx ring enabled.
3244*5779Sxy150489 	 */
3245*5779Sxy150489 	ASSERT(igb->num_tx_rings == 1);
3246*5779Sxy150489 	igb_intr_tx_work(&igb->tx_rings[0]);
3247*5779Sxy150489 
3248*5779Sxy150489 	/*
3249*5779Sxy150489 	 * Check for "other" causes.
3250*5779Sxy150489 	 */
3251*5779Sxy150489 	if (icr & E1000_ICR_LSC) {
3252*5779Sxy150489 		igb_intr_other_work(igb);
3253*5779Sxy150489 	}
3254*5779Sxy150489 
3255*5779Sxy150489 	return (DDI_INTR_CLAIMED);
3256*5779Sxy150489 }
3257*5779Sxy150489 
3258*5779Sxy150489 /*
3259*5779Sxy150489  * igb_alloc_intrs - Allocate interrupts for the driver
3260*5779Sxy150489  *
3261*5779Sxy150489  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
3262*5779Sxy150489  * if not successful, try Legacy.
3263*5779Sxy150489  * igb->intr_force can be used to force sequence to start with
3264*5779Sxy150489  * any of the 3 types.
3265*5779Sxy150489  * If MSI-X is not used, number of tx/rx rings is forced to 1.
3266*5779Sxy150489  */
3267*5779Sxy150489 static int
3268*5779Sxy150489 igb_alloc_intrs(igb_t *igb)
3269*5779Sxy150489 {
3270*5779Sxy150489 	dev_info_t *devinfo;
3271*5779Sxy150489 	int intr_types;
3272*5779Sxy150489 	int rc;
3273*5779Sxy150489 
3274*5779Sxy150489 	devinfo = igb->dip;
3275*5779Sxy150489 
3276*5779Sxy150489 	/* Get supported interrupt types */
3277*5779Sxy150489 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
3278*5779Sxy150489 
3279*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3280*5779Sxy150489 		igb_log(igb,
3281*5779Sxy150489 		    "Get supported interrupt types failed: %d", rc);
3282*5779Sxy150489 		return (IGB_FAILURE);
3283*5779Sxy150489 	}
3284*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
3285*5779Sxy150489 
3286*5779Sxy150489 	igb->intr_type = 0;
3287*5779Sxy150489 
3288*5779Sxy150489 	/* Install MSI-X interrupts */
3289*5779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
3290*5779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSIX)) {
3291*5779Sxy150489 		rc = igb_alloc_intrs_msix(igb);
3292*5779Sxy150489 
3293*5779Sxy150489 		if (rc == IGB_SUCCESS)
3294*5779Sxy150489 			return (IGB_SUCCESS);
3295*5779Sxy150489 
3296*5779Sxy150489 		igb_log(igb,
3297*5779Sxy150489 		    "Allocate MSI-X failed, trying MSI interrupts...");
3298*5779Sxy150489 	}
3299*5779Sxy150489 
3300*5779Sxy150489 	/* MSI-X not used, force rings to 1 */
3301*5779Sxy150489 	igb->num_rx_rings = 1;
3302*5779Sxy150489 	igb->num_tx_rings = 1;
3303*5779Sxy150489 	igb_log(igb,
3304*5779Sxy150489 	    "MSI-X not used, force rx and tx queue number to 1");
3305*5779Sxy150489 
3306*5779Sxy150489 	/* Install MSI interrupts */
3307*5779Sxy150489 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
3308*5779Sxy150489 	    (igb->intr_force <= IGB_INTR_MSI)) {
3309*5779Sxy150489 		rc = igb_alloc_intrs_msi(igb);
3310*5779Sxy150489 
3311*5779Sxy150489 		if (rc == IGB_SUCCESS)
3312*5779Sxy150489 			return (IGB_SUCCESS);
3313*5779Sxy150489 
3314*5779Sxy150489 		igb_log(igb,
3315*5779Sxy150489 		    "Allocate MSI failed, trying Legacy interrupts...");
3316*5779Sxy150489 	}
3317*5779Sxy150489 
3318*5779Sxy150489 	/* Install legacy interrupts */
3319*5779Sxy150489 	if (intr_types & DDI_INTR_TYPE_FIXED) {
3320*5779Sxy150489 		rc = igb_alloc_intrs_legacy(igb);
3321*5779Sxy150489 
3322*5779Sxy150489 		if (rc == IGB_SUCCESS)
3323*5779Sxy150489 			return (IGB_SUCCESS);
3324*5779Sxy150489 
3325*5779Sxy150489 		igb_log(igb,
3326*5779Sxy150489 		    "Allocate Legacy interrupts failed");
3327*5779Sxy150489 	}
3328*5779Sxy150489 
3329*5779Sxy150489 	/* If none of the 3 types succeeded, return failure */
3330*5779Sxy150489 	return (IGB_FAILURE);
3331*5779Sxy150489 }
3332*5779Sxy150489 
3333*5779Sxy150489 /*
3334*5779Sxy150489  * igb_alloc_intrs_msix - Allocate the MSIX interrupts
3335*5779Sxy150489  *
3336*5779Sxy150489  * If fewer than 2 vectors are available, return failure.
3337*5779Sxy150489  * Upon success, this sets the number of Rx rings to a number that
3338*5779Sxy150489  * matches the vectors available for Rx interrupts.
3339*5779Sxy150489  */
3340*5779Sxy150489 static int
3341*5779Sxy150489 igb_alloc_intrs_msix(igb_t *igb)
3342*5779Sxy150489 {
3343*5779Sxy150489 	dev_info_t *devinfo;
3344*5779Sxy150489 	int request, count, avail, actual;
3345*5779Sxy150489 	int rx_rings;
3346*5779Sxy150489 	int rc;
3347*5779Sxy150489 
3348*5779Sxy150489 	devinfo = igb->dip;
3349*5779Sxy150489 
3350*5779Sxy150489 	/*
3351*5779Sxy150489 	 * Currently only 1 tx ring is supported. More tx rings
3352*5779Sxy150489 	 * will be supported with future enhancement.
3353*5779Sxy150489 	 */
3354*5779Sxy150489 	if (igb->num_tx_rings > 1) {
3355*5779Sxy150489 		igb->num_tx_rings = 1;
3356*5779Sxy150489 		igb_log(igb,
3357*5779Sxy150489 		    "Use only 1 MSI-X vector for tx, "
3358*5779Sxy150489 		    "force tx queue number to 1");
3359*5779Sxy150489 	}
3360*5779Sxy150489 
3361*5779Sxy150489 	/*
3362*5779Sxy150489 	 * Best number of vectors for the adapter is
3363*5779Sxy150489 	 * # rx rings + # tx rings + 1 for other
3364*5779Sxy150489 	 * But currently we only support number of vectors of
3365*5779Sxy150489 	 * # rx rings + 1 for tx & other
3366*5779Sxy150489 	 */
3367*5779Sxy150489 	request = igb->num_rx_rings + 1;
3368*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI-X interrupts requested: %d", request);
3369*5779Sxy150489 
3370*5779Sxy150489 	/* Get number of supported interrupts */
3371*5779Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_MSIX, &count);
3372*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
3373*5779Sxy150489 		igb_log(igb,
3374*5779Sxy150489 		    "Get interrupt number failed. Return: %d, count: %d",
3375*5779Sxy150489 		    rc, count);
3376*5779Sxy150489 		return (IGB_FAILURE);
3377*5779Sxy150489 	}
3378*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI-X interrupts supported: %d", count);
3379*5779Sxy150489 
3380*5779Sxy150489 	/* Get number of available interrupts */
3381*5779Sxy150489 	rc = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_MSIX, &avail);
3382*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
3383*5779Sxy150489 		igb_log(igb,
3384*5779Sxy150489 		    "Get interrupt available number failed. "
3385*5779Sxy150489 		    "Return: %d, available: %d", rc, avail);
3386*5779Sxy150489 		return (IGB_FAILURE);
3387*5779Sxy150489 	}
3388*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI-X interrupts available: %d", avail);
3389*5779Sxy150489 
3390*5779Sxy150489 	if (avail < request) {
3391*5779Sxy150489 		igb_log(igb,
3392*5779Sxy150489 		    "Request %d MSI-X vectors, %d available",
3393*5779Sxy150489 		    request, avail);
3394*5779Sxy150489 		request = avail;
3395*5779Sxy150489 	}
3396*5779Sxy150489 
3397*5779Sxy150489 	actual = 0;
3398*5779Sxy150489 	igb->intr_cnt = 0;
3399*5779Sxy150489 
3400*5779Sxy150489 	/* Allocate an array of interrupt handles */
3401*5779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
3402*5779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
3403*5779Sxy150489 
3404*5779Sxy150489 	/* Call ddi_intr_alloc() */
3405*5779Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, DDI_INTR_TYPE_MSIX, 0,
3406*5779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
3407*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3408*5779Sxy150489 		igb_log(igb, "Allocate MSI-X interrupts failed. "
3409*5779Sxy150489 		    "return: %d, request: %d, actual: %d",
3410*5779Sxy150489 		    rc, request, actual);
3411*5779Sxy150489 		goto alloc_msix_fail;
3412*5779Sxy150489 	}
3413*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI-X interrupts actually allocated: %d", actual);
3414*5779Sxy150489 
3415*5779Sxy150489 	igb->intr_cnt = actual;
3416*5779Sxy150489 
3417*5779Sxy150489 	/*
3418*5779Sxy150489 	 * Now we know the actual number of vectors.  Here we assume that
3419*5779Sxy150489 	 * tx and other will share 1 vector and all remaining (must be at
3420*5779Sxy150489 	 * least 1 remaining) will be used for rx.
3421*5779Sxy150489 	 */
3422*5779Sxy150489 	if (actual < 2) {
3423*5779Sxy150489 		igb_log(igb, "Insufficient MSI-X interrupts available: %d",
3424*5779Sxy150489 		    actual);
3425*5779Sxy150489 		goto alloc_msix_fail;
3426*5779Sxy150489 	}
3427*5779Sxy150489 
3428*5779Sxy150489 	rx_rings = actual - 1;
3429*5779Sxy150489 	if (rx_rings < igb->num_rx_rings) {
3430*5779Sxy150489 		igb_log(igb, "MSI-X vectors force Rx queue number to %d",
3431*5779Sxy150489 		    rx_rings);
3432*5779Sxy150489 		igb->num_rx_rings = rx_rings;
3433*5779Sxy150489 	}
3434*5779Sxy150489 
3435*5779Sxy150489 	/* Get priority for first vector, assume remaining are all the same */
3436*5779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
3437*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3438*5779Sxy150489 		igb_log(igb,
3439*5779Sxy150489 		    "Get interrupt priority failed: %d", rc);
3440*5779Sxy150489 		goto alloc_msix_fail;
3441*5779Sxy150489 	}
3442*5779Sxy150489 
3443*5779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
3444*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3445*5779Sxy150489 		igb_log(igb,
3446*5779Sxy150489 		    "Get interrupt cap failed: %d", rc);
3447*5779Sxy150489 		goto alloc_msix_fail;
3448*5779Sxy150489 	}
3449*5779Sxy150489 
3450*5779Sxy150489 	igb->intr_type = DDI_INTR_TYPE_MSIX;
3451*5779Sxy150489 
3452*5779Sxy150489 	return (IGB_SUCCESS);
3453*5779Sxy150489 
3454*5779Sxy150489 alloc_msix_fail:
3455*5779Sxy150489 	igb_rem_intrs(igb);
3456*5779Sxy150489 
3457*5779Sxy150489 	return (IGB_FAILURE);
3458*5779Sxy150489 }
3459*5779Sxy150489 
3460*5779Sxy150489 /*
3461*5779Sxy150489  * igb_alloc_intrs_msi - Allocate the MSI interrupts
3462*5779Sxy150489  */
3463*5779Sxy150489 static int
3464*5779Sxy150489 igb_alloc_intrs_msi(igb_t *igb)
3465*5779Sxy150489 {
3466*5779Sxy150489 	dev_info_t *devinfo;
3467*5779Sxy150489 	int request, count, avail, actual;
3468*5779Sxy150489 	int rc;
3469*5779Sxy150489 
3470*5779Sxy150489 	devinfo = igb->dip;
3471*5779Sxy150489 
3472*5779Sxy150489 	/* Request 1 MSI interrupt vector */
3473*5779Sxy150489 	request = 1;
3474*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI interrupts requested: %d", request);
3475*5779Sxy150489 
3476*5779Sxy150489 	/* Get number of supported interrupts */
3477*5779Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_MSI, &count);
3478*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
3479*5779Sxy150489 		igb_log(igb,
3480*5779Sxy150489 		    "Get MSI supported number failed. Return: %d, count: %d",
3481*5779Sxy150489 		    rc, count);
3482*5779Sxy150489 		return (IGB_FAILURE);
3483*5779Sxy150489 	}
3484*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI interrupts supported: %d", count);
3485*5779Sxy150489 
3486*5779Sxy150489 	/* Get number of available interrupts */
3487*5779Sxy150489 	rc = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_MSI, &avail);
3488*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
3489*5779Sxy150489 		igb_log(igb,
3490*5779Sxy150489 		    "Get MSI available number failed. "
3491*5779Sxy150489 		    "Return: %d, available: %d", rc, avail);
3492*5779Sxy150489 		return (IGB_FAILURE);
3493*5779Sxy150489 	}
3494*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "MSI interrupts available: %d", avail);
3495*5779Sxy150489 
3496*5779Sxy150489 	actual = 0;
3497*5779Sxy150489 	igb->intr_cnt = 0;
3498*5779Sxy150489 
3499*5779Sxy150489 	/* Allocate an array of interrupt handles */
3500*5779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
3501*5779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
3502*5779Sxy150489 
3503*5779Sxy150489 	/* Call ddi_intr_alloc() */
3504*5779Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, DDI_INTR_TYPE_MSI, 0,
3505*5779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
3506*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
3507*5779Sxy150489 		igb_log(igb,
3508*5779Sxy150489 		    "Allocate MSI interrupts failed: %d", rc);
3509*5779Sxy150489 		goto alloc_msi_fail;
3510*5779Sxy150489 	}
3511*5779Sxy150489 
3512*5779Sxy150489 	ASSERT(actual == 1);
3513*5779Sxy150489 	igb->intr_cnt = actual;
3514*5779Sxy150489 
3515*5779Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
3516*5779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
3517*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3518*5779Sxy150489 		igb_log(igb,
3519*5779Sxy150489 		    "Get interrupt priority failed: %d", rc);
3520*5779Sxy150489 		goto alloc_msi_fail;
3521*5779Sxy150489 	}
3522*5779Sxy150489 
3523*5779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
3524*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3525*5779Sxy150489 		igb_log(igb,
3526*5779Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
3527*5779Sxy150489 		goto alloc_msi_fail;
3528*5779Sxy150489 
3529*5779Sxy150489 	}
3530*5779Sxy150489 
3531*5779Sxy150489 	igb->intr_type = DDI_INTR_TYPE_MSI;
3532*5779Sxy150489 
3533*5779Sxy150489 	return (IGB_SUCCESS);
3534*5779Sxy150489 
3535*5779Sxy150489 alloc_msi_fail:
3536*5779Sxy150489 	igb_rem_intrs(igb);
3537*5779Sxy150489 
3538*5779Sxy150489 	return (IGB_FAILURE);
3539*5779Sxy150489 }
3540*5779Sxy150489 
3541*5779Sxy150489 /*
3542*5779Sxy150489  * igb_alloc_intrs_legacy - Allocate the Legacy interrupts
3543*5779Sxy150489  */
3544*5779Sxy150489 static int
3545*5779Sxy150489 igb_alloc_intrs_legacy(igb_t *igb)
3546*5779Sxy150489 {
3547*5779Sxy150489 	dev_info_t *devinfo;
3548*5779Sxy150489 	int request, count, avail, actual;
3549*5779Sxy150489 	int rc;
3550*5779Sxy150489 
3551*5779Sxy150489 	devinfo = igb->dip;
3552*5779Sxy150489 
3553*5779Sxy150489 	/* Request 1 Legacy interrupt vector */
3554*5779Sxy150489 	request = 1;
3555*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "Legacy interrupts requested: %d", request);
3556*5779Sxy150489 
3557*5779Sxy150489 	/* Get number of supported interrupts */
3558*5779Sxy150489 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
3559*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (count == 0)) {
3560*5779Sxy150489 		igb_log(igb,
3561*5779Sxy150489 		    "Get Legacy supported number failed. Return: %d, count: %d",
3562*5779Sxy150489 		    rc, count);
3563*5779Sxy150489 		return (IGB_FAILURE);
3564*5779Sxy150489 	}
3565*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "Legacy interrupts supported: %d", count);
3566*5779Sxy150489 
3567*5779Sxy150489 	/* Get number of available interrupts */
3568*5779Sxy150489 	rc = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_FIXED, &avail);
3569*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
3570*5779Sxy150489 		igb_log(igb,
3571*5779Sxy150489 		    "Get Legacy available number failed. "
3572*5779Sxy150489 		    "Return: %d, available: %d", rc, avail);
3573*5779Sxy150489 		return (IGB_FAILURE);
3574*5779Sxy150489 	}
3575*5779Sxy150489 	IGB_DEBUGLOG_1(igb, "Legacy interrupts available: %d", avail);
3576*5779Sxy150489 
3577*5779Sxy150489 	actual = 0;
3578*5779Sxy150489 	igb->intr_cnt = 0;
3579*5779Sxy150489 
3580*5779Sxy150489 	/* Allocate an array of interrupt handles */
3581*5779Sxy150489 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
3582*5779Sxy150489 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
3583*5779Sxy150489 
3584*5779Sxy150489 	/* Call ddi_intr_alloc() */
3585*5779Sxy150489 	rc = ddi_intr_alloc(devinfo, igb->htable, DDI_INTR_TYPE_FIXED, 0,
3586*5779Sxy150489 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
3587*5779Sxy150489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
3588*5779Sxy150489 		igb_log(igb,
3589*5779Sxy150489 		    "Allocate Legacy interrupts failed: %d", rc);
3590*5779Sxy150489 		goto alloc_legacy_fail;
3591*5779Sxy150489 	}
3592*5779Sxy150489 
3593*5779Sxy150489 	ASSERT(actual == 1);
3594*5779Sxy150489 	igb->intr_cnt = actual;
3595*5779Sxy150489 
3596*5779Sxy150489 	/* Get priority for first msi, assume remaining are all the same */
3597*5779Sxy150489 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
3598*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3599*5779Sxy150489 		igb_log(igb,
3600*5779Sxy150489 		    "Get interrupt priority failed: %d", rc);
3601*5779Sxy150489 		goto alloc_legacy_fail;
3602*5779Sxy150489 	}
3603*5779Sxy150489 
3604*5779Sxy150489 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
3605*5779Sxy150489 	if (rc != DDI_SUCCESS) {
3606*5779Sxy150489 		igb_log(igb,
3607*5779Sxy150489 		    "Get interrupt cap failed: %d\n", rc);
3608*5779Sxy150489 		goto alloc_legacy_fail;
3609*5779Sxy150489 	}
3610*5779Sxy150489 
3611*5779Sxy150489 	igb->intr_type = DDI_INTR_TYPE_FIXED;
3612*5779Sxy150489 
3613*5779Sxy150489 	return (IGB_SUCCESS);
3614*5779Sxy150489 
3615*5779Sxy150489 alloc_legacy_fail:
3616*5779Sxy150489 	igb_rem_intrs(igb);
3617*5779Sxy150489 
3618*5779Sxy150489 	return (IGB_FAILURE);
3619*5779Sxy150489 }
3620*5779Sxy150489 
3621*5779Sxy150489 /*
3622*5779Sxy150489  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
3623*5779Sxy150489  *
3624*5779Sxy150489  * Before adding the interrupt handlers, the interrupt vectors have
3625*5779Sxy150489  * been allocated, and the rx/tx rings have also been allocated.
3626*5779Sxy150489  */
3627*5779Sxy150489 static int
3628*5779Sxy150489 igb_add_intr_handlers(igb_t *igb)
3629*5779Sxy150489 {
3630*5779Sxy150489 	igb_rx_ring_t *rx_ring;
3631*5779Sxy150489 	int vector;
3632*5779Sxy150489 	int rc;
3633*5779Sxy150489 	int i;
3634*5779Sxy150489 
3635*5779Sxy150489 	vector = 0;
3636*5779Sxy150489 
3637*5779Sxy150489 	switch (igb->intr_type) {
3638*5779Sxy150489 	case DDI_INTR_TYPE_MSIX:
3639*5779Sxy150489 		/* Add interrupt handler for tx + other */
3640*5779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
3641*5779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_tx_other,
3642*5779Sxy150489 		    (void *)igb, NULL);
3643*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3644*5779Sxy150489 			igb_log(igb,
3645*5779Sxy150489 			    "Add tx/other interrupt handler failed: %d", rc);
3646*5779Sxy150489 			return (IGB_FAILURE);
3647*5779Sxy150489 		}
3648*5779Sxy150489 		vector++;
3649*5779Sxy150489 
3650*5779Sxy150489 		/* Add interrupt handler for each rx ring */
3651*5779Sxy150489 		for (i = 0; i < igb->num_rx_rings; i++) {
3652*5779Sxy150489 			rx_ring = &igb->rx_rings[i];
3653*5779Sxy150489 
3654*5779Sxy150489 			rc = ddi_intr_add_handler(igb->htable[vector],
3655*5779Sxy150489 			    (ddi_intr_handler_t *)igb_intr_rx,
3656*5779Sxy150489 			    (void *)rx_ring, NULL);
3657*5779Sxy150489 
3658*5779Sxy150489 			if (rc != DDI_SUCCESS) {
3659*5779Sxy150489 				igb_log(igb,
3660*5779Sxy150489 				    "Add rx interrupt handler failed. "
3661*5779Sxy150489 				    "return: %d, rx ring: %d", rc, i);
3662*5779Sxy150489 				for (vector--; vector >= 0; vector--) {
3663*5779Sxy150489 					(void) ddi_intr_remove_handler(
3664*5779Sxy150489 					    igb->htable[vector]);
3665*5779Sxy150489 				}
3666*5779Sxy150489 				return (IGB_FAILURE);
3667*5779Sxy150489 			}
3668*5779Sxy150489 
3669*5779Sxy150489 			rx_ring->intr_vector = vector;
3670*5779Sxy150489 
3671*5779Sxy150489 			vector++;
3672*5779Sxy150489 		}
3673*5779Sxy150489 		break;
3674*5779Sxy150489 
3675*5779Sxy150489 	case DDI_INTR_TYPE_MSI:
3676*5779Sxy150489 		/* Add interrupt handlers for the only vector */
3677*5779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
3678*5779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_msi,
3679*5779Sxy150489 		    (void *)igb, NULL);
3680*5779Sxy150489 
3681*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3682*5779Sxy150489 			igb_log(igb,
3683*5779Sxy150489 			    "Add MSI interrupt handler failed: %d", rc);
3684*5779Sxy150489 			return (IGB_FAILURE);
3685*5779Sxy150489 		}
3686*5779Sxy150489 
3687*5779Sxy150489 		rx_ring = &igb->rx_rings[0];
3688*5779Sxy150489 		rx_ring->intr_vector = vector;
3689*5779Sxy150489 
3690*5779Sxy150489 		vector++;
3691*5779Sxy150489 		break;
3692*5779Sxy150489 
3693*5779Sxy150489 	case DDI_INTR_TYPE_FIXED:
3694*5779Sxy150489 		/* Add interrupt handlers for the only vector */
3695*5779Sxy150489 		rc = ddi_intr_add_handler(igb->htable[vector],
3696*5779Sxy150489 		    (ddi_intr_handler_t *)igb_intr_legacy,
3697*5779Sxy150489 		    (void *)igb, NULL);
3698*5779Sxy150489 
3699*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3700*5779Sxy150489 			igb_log(igb,
3701*5779Sxy150489 			    "Add legacy interrupt handler failed: %d", rc);
3702*5779Sxy150489 			return (IGB_FAILURE);
3703*5779Sxy150489 		}
3704*5779Sxy150489 
3705*5779Sxy150489 		rx_ring = &igb->rx_rings[0];
3706*5779Sxy150489 		rx_ring->intr_vector = vector;
3707*5779Sxy150489 
3708*5779Sxy150489 		vector++;
3709*5779Sxy150489 		break;
3710*5779Sxy150489 
3711*5779Sxy150489 	default:
3712*5779Sxy150489 		return (IGB_FAILURE);
3713*5779Sxy150489 	}
3714*5779Sxy150489 
3715*5779Sxy150489 	ASSERT(vector == igb->intr_cnt);
3716*5779Sxy150489 
3717*5779Sxy150489 	return (IGB_SUCCESS);
3718*5779Sxy150489 }
3719*5779Sxy150489 
3720*5779Sxy150489 /*
3721*5779Sxy150489  * igb_setup_adapter_msix - setup the adapter to use MSI-X interrupts
3722*5779Sxy150489  *
3723*5779Sxy150489  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
3724*5779Sxy150489  */
3725*5779Sxy150489 static void
3726*5779Sxy150489 igb_setup_adapter_msix(igb_t *igb)
3727*5779Sxy150489 {
3728*5779Sxy150489 	uint32_t eims = 0;
3729*5779Sxy150489 	int i, vector;
3730*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
3731*5779Sxy150489 
3732*5779Sxy150489 	/*
3733*5779Sxy150489 	 * Set vector for Tx + Other causes
3734*5779Sxy150489 	 * NOTE assumption that there is only one of these and it is vector 0
3735*5779Sxy150489 	 */
3736*5779Sxy150489 	vector = 0;
3737*5779Sxy150489 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
3738*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
3739*5779Sxy150489 
3740*5779Sxy150489 	vector++;
3741*5779Sxy150489 	for (i = 0; i < igb->num_rx_rings; i++) {
3742*5779Sxy150489 		/*
3743*5779Sxy150489 		 * Set vector for each rx ring
3744*5779Sxy150489 		 */
3745*5779Sxy150489 		eims = (E1000_EICR_RX_QUEUE0 << i);
3746*5779Sxy150489 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
3747*5779Sxy150489 
3748*5779Sxy150489 		/*
3749*5779Sxy150489 		 * Accumulate bits to enable in igb_enable_adapter_interrupts()
3750*5779Sxy150489 		 */
3751*5779Sxy150489 		igb->eims_mask |= eims;
3752*5779Sxy150489 
3753*5779Sxy150489 		vector++;
3754*5779Sxy150489 	}
3755*5779Sxy150489 
3756*5779Sxy150489 	ASSERT(vector == igb->intr_cnt);
3757*5779Sxy150489 
3758*5779Sxy150489 	/*
3759*5779Sxy150489 	 * Disable IAM for ICR interrupt bits
3760*5779Sxy150489 	 */
3761*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_IAM, 0);
3762*5779Sxy150489 	E1000_WRITE_FLUSH(hw);
3763*5779Sxy150489 }
3764*5779Sxy150489 
3765*5779Sxy150489 /*
3766*5779Sxy150489  * igb_rem_intr_handlers - remove the interrupt handlers
3767*5779Sxy150489  */
3768*5779Sxy150489 static void
3769*5779Sxy150489 igb_rem_intr_handlers(igb_t *igb)
3770*5779Sxy150489 {
3771*5779Sxy150489 	int i;
3772*5779Sxy150489 	int rc;
3773*5779Sxy150489 
3774*5779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
3775*5779Sxy150489 		rc = ddi_intr_remove_handler(igb->htable[i]);
3776*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3777*5779Sxy150489 			IGB_DEBUGLOG_1(igb,
3778*5779Sxy150489 			    "Remove intr handler failed: %d", rc);
3779*5779Sxy150489 		}
3780*5779Sxy150489 	}
3781*5779Sxy150489 }
3782*5779Sxy150489 
3783*5779Sxy150489 /*
3784*5779Sxy150489  * igb_rem_intrs - remove the allocated interrupts
3785*5779Sxy150489  */
3786*5779Sxy150489 static void
3787*5779Sxy150489 igb_rem_intrs(igb_t *igb)
3788*5779Sxy150489 {
3789*5779Sxy150489 	int i;
3790*5779Sxy150489 	int rc;
3791*5779Sxy150489 
3792*5779Sxy150489 	for (i = 0; i < igb->intr_cnt; i++) {
3793*5779Sxy150489 		rc = ddi_intr_free(igb->htable[i]);
3794*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3795*5779Sxy150489 			IGB_DEBUGLOG_1(igb,
3796*5779Sxy150489 			    "Free intr failed: %d", rc);
3797*5779Sxy150489 		}
3798*5779Sxy150489 	}
3799*5779Sxy150489 
3800*5779Sxy150489 	kmem_free(igb->htable, igb->intr_size);
3801*5779Sxy150489 	igb->htable = NULL;
3802*5779Sxy150489 }
3803*5779Sxy150489 
3804*5779Sxy150489 /*
3805*5779Sxy150489  * igb_enable_intrs - enable all the ddi interrupts
3806*5779Sxy150489  */
3807*5779Sxy150489 static int
3808*5779Sxy150489 igb_enable_intrs(igb_t *igb)
3809*5779Sxy150489 {
3810*5779Sxy150489 	int i;
3811*5779Sxy150489 	int rc;
3812*5779Sxy150489 
3813*5779Sxy150489 	/* Enable interrupts */
3814*5779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
3815*5779Sxy150489 		/* Call ddi_intr_block_enable() for MSI */
3816*5779Sxy150489 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
3817*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3818*5779Sxy150489 			igb_log(igb,
3819*5779Sxy150489 			    "Enable block intr failed: %d", rc);
3820*5779Sxy150489 			return (IGB_FAILURE);
3821*5779Sxy150489 		}
3822*5779Sxy150489 	} else {
3823*5779Sxy150489 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
3824*5779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
3825*5779Sxy150489 			rc = ddi_intr_enable(igb->htable[i]);
3826*5779Sxy150489 			if (rc != DDI_SUCCESS) {
3827*5779Sxy150489 				igb_log(igb,
3828*5779Sxy150489 				    "Enable intr failed: %d", rc);
3829*5779Sxy150489 				return (IGB_FAILURE);
3830*5779Sxy150489 			}
3831*5779Sxy150489 		}
3832*5779Sxy150489 	}
3833*5779Sxy150489 
3834*5779Sxy150489 	return (IGB_SUCCESS);
3835*5779Sxy150489 }
3836*5779Sxy150489 
3837*5779Sxy150489 /*
3838*5779Sxy150489  * igb_disable_intrs - disable all the ddi interrupts
3839*5779Sxy150489  */
3840*5779Sxy150489 static int
3841*5779Sxy150489 igb_disable_intrs(igb_t *igb)
3842*5779Sxy150489 {
3843*5779Sxy150489 	int i;
3844*5779Sxy150489 	int rc;
3845*5779Sxy150489 
3846*5779Sxy150489 	/* Disable all interrupts */
3847*5779Sxy150489 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
3848*5779Sxy150489 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
3849*5779Sxy150489 		if (rc != DDI_SUCCESS) {
3850*5779Sxy150489 			igb_log(igb,
3851*5779Sxy150489 			    "Disable block intr failed: %d", rc);
3852*5779Sxy150489 			return (IGB_FAILURE);
3853*5779Sxy150489 		}
3854*5779Sxy150489 	} else {
3855*5779Sxy150489 		for (i = 0; i < igb->intr_cnt; i++) {
3856*5779Sxy150489 			rc = ddi_intr_disable(igb->htable[i]);
3857*5779Sxy150489 			if (rc != DDI_SUCCESS) {
3858*5779Sxy150489 				igb_log(igb,
3859*5779Sxy150489 				    "Disable intr failed: %d", rc);
3860*5779Sxy150489 				return (IGB_FAILURE);
3861*5779Sxy150489 			}
3862*5779Sxy150489 		}
3863*5779Sxy150489 	}
3864*5779Sxy150489 
3865*5779Sxy150489 	return (IGB_SUCCESS);
3866*5779Sxy150489 }
3867*5779Sxy150489 
3868*5779Sxy150489 /*
3869*5779Sxy150489  * igb_get_phy_state - Get and save the parameters read from PHY registers
3870*5779Sxy150489  */
3871*5779Sxy150489 static void
3872*5779Sxy150489 igb_get_phy_state(igb_t *igb)
3873*5779Sxy150489 {
3874*5779Sxy150489 	struct e1000_hw *hw = &igb->hw;
3875*5779Sxy150489 	uint16_t phy_ctrl;
3876*5779Sxy150489 	uint16_t phy_status;
3877*5779Sxy150489 	uint16_t phy_an_adv;
3878*5779Sxy150489 	uint16_t phy_an_exp;
3879*5779Sxy150489 	uint16_t phy_ext_status;
3880*5779Sxy150489 	uint16_t phy_1000t_ctrl;
3881*5779Sxy150489 	uint16_t phy_1000t_status;
3882*5779Sxy150489 	uint16_t phy_lp_able;
3883*5779Sxy150489 
3884*5779Sxy150489 	ASSERT(mutex_owned(&igb->gen_lock));
3885*5779Sxy150489 
3886*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
3887*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
3888*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
3889*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
3890*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
3891*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
3892*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_1000t_status);
3893*5779Sxy150489 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
3894*5779Sxy150489 
3895*5779Sxy150489 	igb->param_autoneg_cap =
3896*5779Sxy150489 	    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
3897*5779Sxy150489 	igb->param_pause_cap =
3898*5779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
3899*5779Sxy150489 	igb->param_asym_pause_cap =
3900*5779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
3901*5779Sxy150489 	igb->param_1000fdx_cap = ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3902*5779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
3903*5779Sxy150489 	igb->param_1000hdx_cap = ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
3904*5779Sxy150489 	    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
3905*5779Sxy150489 	igb->param_100t4_cap =
3906*5779Sxy150489 	    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
3907*5779Sxy150489 	igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
3908*5779Sxy150489 	    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
3909*5779Sxy150489 	igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
3910*5779Sxy150489 	    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
3911*5779Sxy150489 	igb->param_10fdx_cap =
3912*5779Sxy150489 	    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
3913*5779Sxy150489 	igb->param_10hdx_cap =
3914*5779Sxy150489 	    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
3915*5779Sxy150489 	igb->param_rem_fault =
3916*5779Sxy150489 	    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
3917*5779Sxy150489 
3918*5779Sxy150489 	igb->param_adv_autoneg_cap = hw->mac.autoneg;
3919*5779Sxy150489 	igb->param_adv_pause_cap =
3920*5779Sxy150489 	    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
3921*5779Sxy150489 	igb->param_adv_asym_pause_cap =
3922*5779Sxy150489 	    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
3923*5779Sxy150489 	igb->param_adv_1000hdx_cap =
3924*5779Sxy150489 	    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
3925*5779Sxy150489 	igb->param_adv_100t4_cap =
3926*5779Sxy150489 	    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
3927*5779Sxy150489 	igb->param_adv_rem_fault =
3928*5779Sxy150489 	    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
3929*5779Sxy150489 	if (igb->param_adv_autoneg_cap == 1) {
3930*5779Sxy150489 		igb->param_adv_1000fdx_cap =
3931*5779Sxy150489 		    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
3932*5779Sxy150489 		igb->param_adv_100fdx_cap =
3933*5779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
3934*5779Sxy150489 		igb->param_adv_100hdx_cap =
3935*5779Sxy150489 		    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
3936*5779Sxy150489 		igb->param_adv_10fdx_cap =
3937*5779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
3938*5779Sxy150489 		igb->param_adv_10hdx_cap =
3939*5779Sxy150489 		    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
3940*5779Sxy150489 	}
3941*5779Sxy150489 
3942*5779Sxy150489 	igb->param_lp_autoneg_cap =
3943*5779Sxy150489 	    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
3944*5779Sxy150489 	igb->param_lp_pause_cap =
3945*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
3946*5779Sxy150489 	igb->param_lp_asym_pause_cap =
3947*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
3948*5779Sxy150489 	igb->param_lp_1000fdx_cap =
3949*5779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
3950*5779Sxy150489 	igb->param_lp_1000hdx_cap =
3951*5779Sxy150489 	    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
3952*5779Sxy150489 	igb->param_lp_100t4_cap =
3953*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
3954*5779Sxy150489 	igb->param_lp_100fdx_cap =
3955*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
3956*5779Sxy150489 	igb->param_lp_100hdx_cap =
3957*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
3958*5779Sxy150489 	igb->param_lp_10fdx_cap =
3959*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
3960*5779Sxy150489 	igb->param_lp_10hdx_cap =
3961*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
3962*5779Sxy150489 	igb->param_lp_rem_fault =
3963*5779Sxy150489 	    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
3964*5779Sxy150489 }
3965*5779Sxy150489 
3966*5779Sxy150489 /*
3967*5779Sxy150489  * igb_get_driver_control
3968*5779Sxy150489  */
3969*5779Sxy150489 static void
3970*5779Sxy150489 igb_get_driver_control(struct e1000_hw *hw)
3971*5779Sxy150489 {
3972*5779Sxy150489 	uint32_t ctrl_ext;
3973*5779Sxy150489 
3974*5779Sxy150489 	/* Notify firmware that driver is in control of device */
3975*5779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3976*5779Sxy150489 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
3977*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
3978*5779Sxy150489 }
3979*5779Sxy150489 
3980*5779Sxy150489 /*
3981*5779Sxy150489  * igb_release_driver_control
3982*5779Sxy150489  */
3983*5779Sxy150489 static void
3984*5779Sxy150489 igb_release_driver_control(struct e1000_hw *hw)
3985*5779Sxy150489 {
3986*5779Sxy150489 	uint32_t ctrl_ext;
3987*5779Sxy150489 
3988*5779Sxy150489 	/* Notify firmware that driver is no longer in control of device */
3989*5779Sxy150489 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3990*5779Sxy150489 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
3991*5779Sxy150489 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
3992*5779Sxy150489 }
3993*5779Sxy150489 
3994*5779Sxy150489 /*
3995*5779Sxy150489  * igb_atomic_reserve - Atomic decrease operation
3996*5779Sxy150489  */
3997*5779Sxy150489 int
3998*5779Sxy150489 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
3999*5779Sxy150489 {
4000*5779Sxy150489 	uint32_t oldval;
4001*5779Sxy150489 	uint32_t newval;
4002*5779Sxy150489 
4003*5779Sxy150489 	/* ATOMICALLY */
4004*5779Sxy150489 	do {
4005*5779Sxy150489 		oldval = *count_p;
4006*5779Sxy150489 		if (oldval < n)
4007*5779Sxy150489 			return (-1);
4008*5779Sxy150489 		newval = oldval - n;
4009*5779Sxy150489 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
4010*5779Sxy150489 
4011*5779Sxy150489 	return (newval);
4012*5779Sxy150489 }
4013