xref: /titanic_52/usr/src/uts/common/io/igb/igb_main.c (revision 17a2b317610f531d565bf4e940433aab2d9e6985)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright(c) 2007-2010 Intel Corporation. All rights reserved.
24  */
25 
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  */
29 
30 #include "igb_sw.h"
31 
32 static char ident[] = "Intel 1Gb Ethernet";
33 static char igb_version[] = "igb 1.1.16";
34 
35 /*
36  * Local function protoypes
37  */
38 static int igb_register_mac(igb_t *);
39 static int igb_identify_hardware(igb_t *);
40 static int igb_regs_map(igb_t *);
41 static void igb_init_properties(igb_t *);
42 static int igb_init_driver_settings(igb_t *);
43 static void igb_init_locks(igb_t *);
44 static void igb_destroy_locks(igb_t *);
45 static int igb_init_mac_address(igb_t *);
46 static int igb_init(igb_t *);
47 static int igb_init_adapter(igb_t *);
48 static void igb_stop_adapter(igb_t *);
49 static int igb_reset(igb_t *);
50 static void igb_tx_clean(igb_t *);
51 static boolean_t igb_tx_drain(igb_t *);
52 static boolean_t igb_rx_drain(igb_t *);
53 static int igb_alloc_rings(igb_t *);
54 static int igb_alloc_rx_data(igb_t *);
55 static void igb_free_rx_data(igb_t *);
56 static void igb_free_rings(igb_t *);
57 static void igb_setup_rings(igb_t *);
58 static void igb_setup_rx(igb_t *);
59 static void igb_setup_tx(igb_t *);
60 static void igb_setup_rx_ring(igb_rx_ring_t *);
61 static void igb_setup_tx_ring(igb_tx_ring_t *);
62 static void igb_setup_rss(igb_t *);
63 static void igb_setup_mac_rss_classify(igb_t *);
64 static void igb_setup_mac_classify(igb_t *);
65 static void igb_init_unicst(igb_t *);
66 static void igb_setup_multicst(igb_t *);
67 static void igb_get_phy_state(igb_t *);
68 static void igb_param_sync(igb_t *);
69 static void igb_get_conf(igb_t *);
70 static int igb_get_prop(igb_t *, char *, int, int, int);
71 static boolean_t igb_is_link_up(igb_t *);
72 static boolean_t igb_link_check(igb_t *);
73 static void igb_local_timer(void *);
74 static void igb_link_timer(void *);
75 static void igb_arm_watchdog_timer(igb_t *);
76 static void igb_start_watchdog_timer(igb_t *);
77 static void igb_restart_watchdog_timer(igb_t *);
78 static void igb_stop_watchdog_timer(igb_t *);
79 static void igb_start_link_timer(igb_t *);
80 static void igb_stop_link_timer(igb_t *);
81 static void igb_disable_adapter_interrupts(igb_t *);
82 static void igb_enable_adapter_interrupts_82575(igb_t *);
83 static void igb_enable_adapter_interrupts_82576(igb_t *);
84 static void igb_enable_adapter_interrupts_82580(igb_t *);
85 static boolean_t is_valid_mac_addr(uint8_t *);
86 static boolean_t igb_stall_check(igb_t *);
87 static boolean_t igb_set_loopback_mode(igb_t *, uint32_t);
88 static void igb_set_external_loopback(igb_t *);
89 static void igb_set_internal_phy_loopback(igb_t *);
90 static void igb_set_internal_serdes_loopback(igb_t *);
91 static boolean_t igb_find_mac_address(igb_t *);
92 static int igb_alloc_intrs(igb_t *);
93 static int igb_alloc_intr_handles(igb_t *, int);
94 static int igb_add_intr_handlers(igb_t *);
95 static void igb_rem_intr_handlers(igb_t *);
96 static void igb_rem_intrs(igb_t *);
97 static int igb_enable_intrs(igb_t *);
98 static int igb_disable_intrs(igb_t *);
99 static void igb_setup_msix_82575(igb_t *);
100 static void igb_setup_msix_82576(igb_t *);
101 static void igb_setup_msix_82580(igb_t *);
102 static uint_t igb_intr_legacy(void *, void *);
103 static uint_t igb_intr_msi(void *, void *);
104 static uint_t igb_intr_rx(void *, void *);
105 static uint_t igb_intr_tx(void *, void *);
106 static uint_t igb_intr_tx_other(void *, void *);
107 static void igb_intr_rx_work(igb_rx_ring_t *);
108 static void igb_intr_tx_work(igb_tx_ring_t *);
109 static void igb_intr_link_work(igb_t *);
110 static void igb_get_driver_control(struct e1000_hw *);
111 static void igb_release_driver_control(struct e1000_hw *);
112 
113 static int igb_attach(dev_info_t *, ddi_attach_cmd_t);
114 static int igb_detach(dev_info_t *, ddi_detach_cmd_t);
115 static int igb_resume(dev_info_t *);
116 static int igb_suspend(dev_info_t *);
117 static int igb_quiesce(dev_info_t *);
118 static void igb_unconfigure(dev_info_t *, igb_t *);
119 static int igb_fm_error_cb(dev_info_t *, ddi_fm_error_t *,
120     const void *);
121 static void igb_fm_init(igb_t *);
122 static void igb_fm_fini(igb_t *);
123 static void igb_release_multicast(igb_t *);
124 
125 char *igb_priv_props[] = {
126 	"_tx_copy_thresh",
127 	"_tx_recycle_thresh",
128 	"_tx_overload_thresh",
129 	"_tx_resched_thresh",
130 	"_rx_copy_thresh",
131 	"_rx_limit_per_intr",
132 	"_intr_throttling",
133 	"_adv_pause_cap",
134 	"_adv_asym_pause_cap",
135 	NULL
136 };
137 
138 static struct cb_ops igb_cb_ops = {
139 	nulldev,		/* cb_open */
140 	nulldev,		/* cb_close */
141 	nodev,			/* cb_strategy */
142 	nodev,			/* cb_print */
143 	nodev,			/* cb_dump */
144 	nodev,			/* cb_read */
145 	nodev,			/* cb_write */
146 	nodev,			/* cb_ioctl */
147 	nodev,			/* cb_devmap */
148 	nodev,			/* cb_mmap */
149 	nodev,			/* cb_segmap */
150 	nochpoll,		/* cb_chpoll */
151 	ddi_prop_op,		/* cb_prop_op */
152 	NULL,			/* cb_stream */
153 	D_MP | D_HOTPLUG,	/* cb_flag */
154 	CB_REV,			/* cb_rev */
155 	nodev,			/* cb_aread */
156 	nodev			/* cb_awrite */
157 };
158 
159 static struct dev_ops igb_dev_ops = {
160 	DEVO_REV,		/* devo_rev */
161 	0,			/* devo_refcnt */
162 	NULL,			/* devo_getinfo */
163 	nulldev,		/* devo_identify */
164 	nulldev,		/* devo_probe */
165 	igb_attach,		/* devo_attach */
166 	igb_detach,		/* devo_detach */
167 	nodev,			/* devo_reset */
168 	&igb_cb_ops,		/* devo_cb_ops */
169 	NULL,			/* devo_bus_ops */
170 	ddi_power,		/* devo_power */
171 	igb_quiesce,	/* devo_quiesce */
172 };
173 
174 static struct modldrv igb_modldrv = {
175 	&mod_driverops,		/* Type of module.  This one is a driver */
176 	ident,			/* Discription string */
177 	&igb_dev_ops,		/* driver ops */
178 };
179 
180 static struct modlinkage igb_modlinkage = {
181 	MODREV_1, &igb_modldrv, NULL
182 };
183 
184 /* Access attributes for register mapping */
185 ddi_device_acc_attr_t igb_regs_acc_attr = {
186 	DDI_DEVICE_ATTR_V1,
187 	DDI_STRUCTURE_LE_ACC,
188 	DDI_STRICTORDER_ACC,
189 	DDI_FLAGERR_ACC
190 };
191 
192 #define	IGB_M_CALLBACK_FLAGS \
193 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO)
194 
195 static mac_callbacks_t igb_m_callbacks = {
196 	IGB_M_CALLBACK_FLAGS,
197 	igb_m_stat,
198 	igb_m_start,
199 	igb_m_stop,
200 	igb_m_promisc,
201 	igb_m_multicst,
202 	NULL,
203 	NULL,
204 	NULL,
205 	igb_m_ioctl,
206 	igb_m_getcapab,
207 	NULL,
208 	NULL,
209 	igb_m_setprop,
210 	igb_m_getprop,
211 	igb_m_propinfo
212 };
213 
214 /*
215  * Initialize capabilities of each supported adapter type
216  */
217 static adapter_info_t igb_82575_cap = {
218 	/* limits */
219 	4,		/* maximum number of rx queues */
220 	1,		/* minimum number of rx queues */
221 	4,		/* default number of rx queues */
222 	4,		/* maximum number of tx queues */
223 	1,		/* minimum number of tx queues */
224 	4,		/* default number of tx queues */
225 	65535,		/* maximum interrupt throttle rate */
226 	0,		/* minimum interrupt throttle rate */
227 	200,		/* default interrupt throttle rate */
228 
229 	/* function pointers */
230 	igb_enable_adapter_interrupts_82575,
231 	igb_setup_msix_82575,
232 
233 	/* capabilities */
234 	(IGB_FLAG_HAS_DCA |	/* capability flags */
235 	IGB_FLAG_VMDQ_POOL),
236 
237 	0xffc00000		/* mask for RXDCTL register */
238 };
239 
240 static adapter_info_t igb_82576_cap = {
241 	/* limits */
242 	16,		/* maximum number of rx queues */
243 	1,		/* minimum number of rx queues */
244 	4,		/* default number of rx queues */
245 	16,		/* maximum number of tx queues */
246 	1,		/* minimum number of tx queues */
247 	4,		/* default number of tx queues */
248 	65535,		/* maximum interrupt throttle rate */
249 	0,		/* minimum interrupt throttle rate */
250 	200,		/* default interrupt throttle rate */
251 
252 	/* function pointers */
253 	igb_enable_adapter_interrupts_82576,
254 	igb_setup_msix_82576,
255 
256 	/* capabilities */
257 	(IGB_FLAG_HAS_DCA |	/* capability flags */
258 	IGB_FLAG_VMDQ_POOL |
259 	IGB_FLAG_NEED_CTX_IDX),
260 
261 	0xffe00000		/* mask for RXDCTL register */
262 };
263 
264 static adapter_info_t igb_82580_cap = {
265 	/* limits */
266 	8,		/* maximum number of rx queues */
267 	1,		/* minimum number of rx queues */
268 	4,		/* default number of rx queues */
269 	8,		/* maximum number of tx queues */
270 	1,		/* minimum number of tx queues */
271 	4,		/* default number of tx queues */
272 	65535,		/* maximum interrupt throttle rate */
273 	0,		/* minimum interrupt throttle rate */
274 	200,		/* default interrupt throttle rate */
275 
276 	/* function pointers */
277 	igb_enable_adapter_interrupts_82580,
278 	igb_setup_msix_82580,
279 
280 	/* capabilities */
281 	(IGB_FLAG_HAS_DCA |	/* capability flags */
282 	IGB_FLAG_VMDQ_POOL |
283 	IGB_FLAG_NEED_CTX_IDX),
284 
285 	0xffe00000		/* mask for RXDCTL register */
286 };
287 
288 /*
289  * Module Initialization Functions
290  */
291 
292 int
293 _init(void)
294 {
295 	int status;
296 
297 	mac_init_ops(&igb_dev_ops, MODULE_NAME);
298 
299 	status = mod_install(&igb_modlinkage);
300 
301 	if (status != DDI_SUCCESS) {
302 		mac_fini_ops(&igb_dev_ops);
303 	}
304 
305 	return (status);
306 }
307 
308 int
309 _fini(void)
310 {
311 	int status;
312 
313 	status = mod_remove(&igb_modlinkage);
314 
315 	if (status == DDI_SUCCESS) {
316 		mac_fini_ops(&igb_dev_ops);
317 	}
318 
319 	return (status);
320 
321 }
322 
323 int
324 _info(struct modinfo *modinfop)
325 {
326 	int status;
327 
328 	status = mod_info(&igb_modlinkage, modinfop);
329 
330 	return (status);
331 }
332 
333 /*
334  * igb_attach - driver attach
335  *
336  * This function is the device specific initialization entry
337  * point. This entry point is required and must be written.
338  * The DDI_ATTACH command must be provided in the attach entry
339  * point. When attach() is called with cmd set to DDI_ATTACH,
340  * all normal kernel services (such as kmem_alloc(9F)) are
341  * available for use by the driver.
342  *
343  * The attach() function will be called once for each instance
344  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
345  * Until attach() succeeds, the only driver entry points which
346  * may be called are open(9E) and getinfo(9E).
347  */
348 static int
349 igb_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
350 {
351 	igb_t *igb;
352 	struct igb_osdep *osdep;
353 	struct e1000_hw *hw;
354 	int instance;
355 
356 	/*
357 	 * Check the command and perform corresponding operations
358 	 */
359 	switch (cmd) {
360 	default:
361 		return (DDI_FAILURE);
362 
363 	case DDI_RESUME:
364 		return (igb_resume(devinfo));
365 
366 	case DDI_ATTACH:
367 		break;
368 	}
369 
370 	/* Get the device instance */
371 	instance = ddi_get_instance(devinfo);
372 
373 	/* Allocate memory for the instance data structure */
374 	igb = kmem_zalloc(sizeof (igb_t), KM_SLEEP);
375 
376 	igb->dip = devinfo;
377 	igb->instance = instance;
378 
379 	hw = &igb->hw;
380 	osdep = &igb->osdep;
381 	hw->back = osdep;
382 	osdep->igb = igb;
383 
384 	/* Attach the instance pointer to the dev_info data structure */
385 	ddi_set_driver_private(devinfo, igb);
386 
387 
388 	/* Initialize for fma support */
389 	igb->fm_capabilities = igb_get_prop(igb, "fm-capable",
390 	    0, 0x0f,
391 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
392 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
393 	igb_fm_init(igb);
394 	igb->attach_progress |= ATTACH_PROGRESS_FMINIT;
395 
396 	/*
397 	 * Map PCI config space registers
398 	 */
399 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
400 		igb_error(igb, "Failed to map PCI configurations");
401 		goto attach_fail;
402 	}
403 	igb->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
404 
405 	/*
406 	 * Identify the chipset family
407 	 */
408 	if (igb_identify_hardware(igb) != IGB_SUCCESS) {
409 		igb_error(igb, "Failed to identify hardware");
410 		goto attach_fail;
411 	}
412 
413 	/*
414 	 * Map device registers
415 	 */
416 	if (igb_regs_map(igb) != IGB_SUCCESS) {
417 		igb_error(igb, "Failed to map device registers");
418 		goto attach_fail;
419 	}
420 	igb->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
421 
422 	/*
423 	 * Initialize driver parameters
424 	 */
425 	igb_init_properties(igb);
426 	igb->attach_progress |= ATTACH_PROGRESS_PROPS;
427 
428 	/*
429 	 * Allocate interrupts
430 	 */
431 	if (igb_alloc_intrs(igb) != IGB_SUCCESS) {
432 		igb_error(igb, "Failed to allocate interrupts");
433 		goto attach_fail;
434 	}
435 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_INTR;
436 
437 	/*
438 	 * Allocate rx/tx rings based on the ring numbers.
439 	 * The actual numbers of rx/tx rings are decided by the number of
440 	 * allocated interrupt vectors, so we should allocate the rings after
441 	 * interrupts are allocated.
442 	 */
443 	if (igb_alloc_rings(igb) != IGB_SUCCESS) {
444 		igb_error(igb, "Failed to allocate rx/tx rings or groups");
445 		goto attach_fail;
446 	}
447 	igb->attach_progress |= ATTACH_PROGRESS_ALLOC_RINGS;
448 
449 	/*
450 	 * Add interrupt handlers
451 	 */
452 	if (igb_add_intr_handlers(igb) != IGB_SUCCESS) {
453 		igb_error(igb, "Failed to add interrupt handlers");
454 		goto attach_fail;
455 	}
456 	igb->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
457 
458 	/*
459 	 * Initialize driver parameters
460 	 */
461 	if (igb_init_driver_settings(igb) != IGB_SUCCESS) {
462 		igb_error(igb, "Failed to initialize driver settings");
463 		goto attach_fail;
464 	}
465 
466 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK) {
467 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
468 		goto attach_fail;
469 	}
470 
471 	/*
472 	 * Initialize mutexes for this device.
473 	 * Do this before enabling the interrupt handler and
474 	 * register the softint to avoid the condition where
475 	 * interrupt handler can try using uninitialized mutex
476 	 */
477 	igb_init_locks(igb);
478 	igb->attach_progress |= ATTACH_PROGRESS_LOCKS;
479 
480 	/*
481 	 * Initialize the adapter
482 	 */
483 	if (igb_init(igb) != IGB_SUCCESS) {
484 		igb_error(igb, "Failed to initialize adapter");
485 		goto attach_fail;
486 	}
487 	igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
488 
489 	/*
490 	 * Initialize statistics
491 	 */
492 	if (igb_init_stats(igb) != IGB_SUCCESS) {
493 		igb_error(igb, "Failed to initialize statistics");
494 		goto attach_fail;
495 	}
496 	igb->attach_progress |= ATTACH_PROGRESS_STATS;
497 
498 	/*
499 	 * Register the driver to the MAC
500 	 */
501 	if (igb_register_mac(igb) != IGB_SUCCESS) {
502 		igb_error(igb, "Failed to register MAC");
503 		goto attach_fail;
504 	}
505 	igb->attach_progress |= ATTACH_PROGRESS_MAC;
506 
507 	/*
508 	 * Now that mutex locks are initialized, and the chip is also
509 	 * initialized, enable interrupts.
510 	 */
511 	if (igb_enable_intrs(igb) != IGB_SUCCESS) {
512 		igb_error(igb, "Failed to enable DDI interrupts");
513 		goto attach_fail;
514 	}
515 	igb->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
516 
517 	igb_log(igb, "%s", igb_version);
518 	atomic_or_32(&igb->igb_state, IGB_INITIALIZED);
519 
520 	return (DDI_SUCCESS);
521 
522 attach_fail:
523 	igb_unconfigure(devinfo, igb);
524 	return (DDI_FAILURE);
525 }
526 
527 /*
528  * igb_detach - driver detach
529  *
530  * The detach() function is the complement of the attach routine.
531  * If cmd is set to DDI_DETACH, detach() is used to remove  the
532  * state  associated  with  a  given  instance of a device node
533  * prior to the removal of that instance from the system.
534  *
535  * The detach() function will be called once for each  instance
536  * of the device for which there has been a successful attach()
537  * once there are no longer  any  opens  on  the  device.
538  *
539  * Interrupts routine are disabled, All memory allocated by this
540  * driver are freed.
541  */
542 static int
543 igb_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
544 {
545 	igb_t *igb;
546 
547 	/*
548 	 * Check detach command
549 	 */
550 	switch (cmd) {
551 	default:
552 		return (DDI_FAILURE);
553 
554 	case DDI_SUSPEND:
555 		return (igb_suspend(devinfo));
556 
557 	case DDI_DETACH:
558 		break;
559 	}
560 
561 
562 	/*
563 	 * Get the pointer to the driver private data structure
564 	 */
565 	igb = (igb_t *)ddi_get_driver_private(devinfo);
566 	if (igb == NULL)
567 		return (DDI_FAILURE);
568 
569 	/*
570 	 * Unregister MAC. If failed, we have to fail the detach
571 	 */
572 	if (mac_unregister(igb->mac_hdl) != 0) {
573 		igb_error(igb, "Failed to unregister MAC");
574 		return (DDI_FAILURE);
575 	}
576 	igb->attach_progress &= ~ATTACH_PROGRESS_MAC;
577 
578 	/*
579 	 * If the device is still running, it needs to be stopped first.
580 	 * This check is necessary because under some specific circumstances,
581 	 * the detach routine can be called without stopping the interface
582 	 * first.
583 	 */
584 	mutex_enter(&igb->gen_lock);
585 	if (igb->igb_state & IGB_STARTED) {
586 		atomic_and_32(&igb->igb_state, ~IGB_STARTED);
587 		igb_stop(igb, B_TRUE);
588 		mutex_exit(&igb->gen_lock);
589 		/* Disable and stop the watchdog timer */
590 		igb_disable_watchdog_timer(igb);
591 	} else
592 		mutex_exit(&igb->gen_lock);
593 
594 	/*
595 	 * Check if there are still rx buffers held by the upper layer.
596 	 * If so, fail the detach.
597 	 */
598 	if (!igb_rx_drain(igb))
599 		return (DDI_FAILURE);
600 
601 	/*
602 	 * Do the remaining unconfigure routines
603 	 */
604 	igb_unconfigure(devinfo, igb);
605 
606 	return (DDI_SUCCESS);
607 }
608 
609 /*
610  * quiesce(9E) entry point.
611  *
612  * This function is called when the system is single-threaded at high
613  * PIL with preemption disabled. Therefore, this function must not be
614  * blocked.
615  *
616  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
617  * DDI_FAILURE indicates an error condition and should almost never happen.
618  */
619 static int
620 igb_quiesce(dev_info_t *devinfo)
621 {
622 	igb_t *igb;
623 	struct e1000_hw *hw;
624 
625 	igb = (igb_t *)ddi_get_driver_private(devinfo);
626 
627 	if (igb == NULL)
628 		return (DDI_FAILURE);
629 
630 	hw = &igb->hw;
631 
632 	/*
633 	 * Disable the adapter interrupts
634 	 */
635 	igb_disable_adapter_interrupts(igb);
636 
637 	/* Tell firmware driver is no longer in control */
638 	igb_release_driver_control(hw);
639 
640 	/*
641 	 * Reset the chipset
642 	 */
643 	(void) e1000_reset_hw(hw);
644 
645 	/*
646 	 * Reset PHY if possible
647 	 */
648 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
649 		(void) e1000_phy_hw_reset(hw);
650 
651 	return (DDI_SUCCESS);
652 }
653 
654 /*
655  * igb_unconfigure - release all resources held by this instance
656  */
657 static void
658 igb_unconfigure(dev_info_t *devinfo, igb_t *igb)
659 {
660 	/*
661 	 * Disable interrupt
662 	 */
663 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
664 		(void) igb_disable_intrs(igb);
665 	}
666 
667 	/*
668 	 * Unregister MAC
669 	 */
670 	if (igb->attach_progress & ATTACH_PROGRESS_MAC) {
671 		(void) mac_unregister(igb->mac_hdl);
672 	}
673 
674 	/*
675 	 * Free statistics
676 	 */
677 	if (igb->attach_progress & ATTACH_PROGRESS_STATS) {
678 		kstat_delete((kstat_t *)igb->igb_ks);
679 	}
680 
681 	/*
682 	 * Remove interrupt handlers
683 	 */
684 	if (igb->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
685 		igb_rem_intr_handlers(igb);
686 	}
687 
688 	/*
689 	 * Remove interrupts
690 	 */
691 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_INTR) {
692 		igb_rem_intrs(igb);
693 	}
694 
695 	/*
696 	 * Remove driver properties
697 	 */
698 	if (igb->attach_progress & ATTACH_PROGRESS_PROPS) {
699 		(void) ddi_prop_remove_all(devinfo);
700 	}
701 
702 	/*
703 	 * Stop the adapter
704 	 */
705 	if (igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) {
706 		mutex_enter(&igb->gen_lock);
707 		igb_stop_adapter(igb);
708 		mutex_exit(&igb->gen_lock);
709 		if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
710 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_UNAFFECTED);
711 	}
712 
713 	/*
714 	 * Free multicast table
715 	 */
716 	igb_release_multicast(igb);
717 
718 	/*
719 	 * Free register handle
720 	 */
721 	if (igb->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
722 		if (igb->osdep.reg_handle != NULL)
723 			ddi_regs_map_free(&igb->osdep.reg_handle);
724 	}
725 
726 	/*
727 	 * Free PCI config handle
728 	 */
729 	if (igb->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
730 		if (igb->osdep.cfg_handle != NULL)
731 			pci_config_teardown(&igb->osdep.cfg_handle);
732 	}
733 
734 	/*
735 	 * Free locks
736 	 */
737 	if (igb->attach_progress & ATTACH_PROGRESS_LOCKS) {
738 		igb_destroy_locks(igb);
739 	}
740 
741 	/*
742 	 * Free the rx/tx rings
743 	 */
744 	if (igb->attach_progress & ATTACH_PROGRESS_ALLOC_RINGS) {
745 		igb_free_rings(igb);
746 	}
747 
748 	/*
749 	 * Remove FMA
750 	 */
751 	if (igb->attach_progress & ATTACH_PROGRESS_FMINIT) {
752 		igb_fm_fini(igb);
753 	}
754 
755 	/*
756 	 * Free the driver data structure
757 	 */
758 	kmem_free(igb, sizeof (igb_t));
759 
760 	ddi_set_driver_private(devinfo, NULL);
761 }
762 
763 /*
764  * igb_register_mac - Register the driver and its function pointers with
765  * the GLD interface
766  */
767 static int
768 igb_register_mac(igb_t *igb)
769 {
770 	struct e1000_hw *hw = &igb->hw;
771 	mac_register_t *mac;
772 	int status;
773 
774 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
775 		return (IGB_FAILURE);
776 
777 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
778 	mac->m_driver = igb;
779 	mac->m_dip = igb->dip;
780 	mac->m_src_addr = hw->mac.addr;
781 	mac->m_callbacks = &igb_m_callbacks;
782 	mac->m_min_sdu = 0;
783 	mac->m_max_sdu = igb->max_frame_size -
784 	    sizeof (struct ether_vlan_header) - ETHERFCSL;
785 	mac->m_margin = VLAN_TAGSZ;
786 	mac->m_priv_props = igb_priv_props;
787 	mac->m_v12n = MAC_VIRT_LEVEL1;
788 
789 	status = mac_register(mac, &igb->mac_hdl);
790 
791 	mac_free(mac);
792 
793 	return ((status == 0) ? IGB_SUCCESS : IGB_FAILURE);
794 }
795 
796 /*
797  * igb_identify_hardware - Identify the type of the chipset
798  */
799 static int
800 igb_identify_hardware(igb_t *igb)
801 {
802 	struct e1000_hw *hw = &igb->hw;
803 	struct igb_osdep *osdep = &igb->osdep;
804 
805 	/*
806 	 * Get the device id
807 	 */
808 	hw->vendor_id =
809 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
810 	hw->device_id =
811 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
812 	hw->revision_id =
813 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
814 	hw->subsystem_device_id =
815 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
816 	hw->subsystem_vendor_id =
817 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
818 
819 	/*
820 	 * Set the mac type of the adapter based on the device id
821 	 */
822 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
823 		return (IGB_FAILURE);
824 	}
825 
826 	/*
827 	 * Install adapter capabilities based on mac type
828 	 */
829 	switch (hw->mac.type) {
830 	case e1000_82575:
831 		igb->capab = &igb_82575_cap;
832 		break;
833 	case e1000_82576:
834 		igb->capab = &igb_82576_cap;
835 		break;
836 	case e1000_82580:
837 		igb->capab = &igb_82580_cap;
838 		break;
839 	default:
840 		return (IGB_FAILURE);
841 	}
842 
843 	return (IGB_SUCCESS);
844 }
845 
846 /*
847  * igb_regs_map - Map the device registers
848  */
849 static int
850 igb_regs_map(igb_t *igb)
851 {
852 	dev_info_t *devinfo = igb->dip;
853 	struct e1000_hw *hw = &igb->hw;
854 	struct igb_osdep *osdep = &igb->osdep;
855 	off_t mem_size;
856 
857 	/*
858 	 * First get the size of device registers to be mapped.
859 	 */
860 	if (ddi_dev_regsize(devinfo, IGB_ADAPTER_REGSET, &mem_size) !=
861 	    DDI_SUCCESS) {
862 		return (IGB_FAILURE);
863 	}
864 
865 	/*
866 	 * Call ddi_regs_map_setup() to map registers
867 	 */
868 	if ((ddi_regs_map_setup(devinfo, IGB_ADAPTER_REGSET,
869 	    (caddr_t *)&hw->hw_addr, 0,
870 	    mem_size, &igb_regs_acc_attr,
871 	    &osdep->reg_handle)) != DDI_SUCCESS) {
872 		return (IGB_FAILURE);
873 	}
874 
875 	return (IGB_SUCCESS);
876 }
877 
878 /*
879  * igb_init_properties - Initialize driver properties
880  */
881 static void
882 igb_init_properties(igb_t *igb)
883 {
884 	/*
885 	 * Get conf file properties, including link settings
886 	 * jumbo frames, ring number, descriptor number, etc.
887 	 */
888 	igb_get_conf(igb);
889 }
890 
891 /*
892  * igb_init_driver_settings - Initialize driver settings
893  *
894  * The settings include hardware function pointers, bus information,
895  * rx/tx rings settings, link state, and any other parameters that
896  * need to be setup during driver initialization.
897  */
898 static int
899 igb_init_driver_settings(igb_t *igb)
900 {
901 	struct e1000_hw *hw = &igb->hw;
902 	igb_rx_ring_t *rx_ring;
903 	igb_tx_ring_t *tx_ring;
904 	uint32_t rx_size;
905 	uint32_t tx_size;
906 	int i;
907 
908 	/*
909 	 * Initialize chipset specific hardware function pointers
910 	 */
911 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
912 		return (IGB_FAILURE);
913 	}
914 
915 	/*
916 	 * Get bus information
917 	 */
918 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
919 		return (IGB_FAILURE);
920 	}
921 
922 	/*
923 	 * Get the system page size
924 	 */
925 	igb->page_size = ddi_ptob(igb->dip, (ulong_t)1);
926 
927 	/*
928 	 * Set rx buffer size
929 	 * The IP header alignment room is counted in the calculation.
930 	 * The rx buffer size is in unit of 1K that is required by the
931 	 * chipset hardware.
932 	 */
933 	rx_size = igb->max_frame_size + IPHDR_ALIGN_ROOM;
934 	igb->rx_buf_size = ((rx_size >> 10) +
935 	    ((rx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
936 
937 	/*
938 	 * Set tx buffer size
939 	 */
940 	tx_size = igb->max_frame_size;
941 	igb->tx_buf_size = ((tx_size >> 10) +
942 	    ((tx_size & (((uint32_t)1 << 10) - 1)) > 0 ? 1 : 0)) << 10;
943 
944 	/*
945 	 * Initialize rx/tx rings parameters
946 	 */
947 	for (i = 0; i < igb->num_rx_rings; i++) {
948 		rx_ring = &igb->rx_rings[i];
949 		rx_ring->index = i;
950 		rx_ring->igb = igb;
951 	}
952 
953 	for (i = 0; i < igb->num_tx_rings; i++) {
954 		tx_ring = &igb->tx_rings[i];
955 		tx_ring->index = i;
956 		tx_ring->igb = igb;
957 		if (igb->tx_head_wb_enable)
958 			tx_ring->tx_recycle = igb_tx_recycle_head_wb;
959 		else
960 			tx_ring->tx_recycle = igb_tx_recycle_legacy;
961 
962 		tx_ring->ring_size = igb->tx_ring_size;
963 		tx_ring->free_list_size = igb->tx_ring_size +
964 		    (igb->tx_ring_size >> 1);
965 	}
966 
967 	/*
968 	 * Initialize values of interrupt throttling rates
969 	 */
970 	for (i = 1; i < MAX_NUM_EITR; i++)
971 		igb->intr_throttling[i] = igb->intr_throttling[0];
972 
973 	/*
974 	 * The initial link state should be "unknown"
975 	 */
976 	igb->link_state = LINK_STATE_UNKNOWN;
977 
978 	return (IGB_SUCCESS);
979 }
980 
981 /*
982  * igb_init_locks - Initialize locks
983  */
984 static void
985 igb_init_locks(igb_t *igb)
986 {
987 	igb_rx_ring_t *rx_ring;
988 	igb_tx_ring_t *tx_ring;
989 	int i;
990 
991 	for (i = 0; i < igb->num_rx_rings; i++) {
992 		rx_ring = &igb->rx_rings[i];
993 		mutex_init(&rx_ring->rx_lock, NULL,
994 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
995 	}
996 
997 	for (i = 0; i < igb->num_tx_rings; i++) {
998 		tx_ring = &igb->tx_rings[i];
999 		mutex_init(&tx_ring->tx_lock, NULL,
1000 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1001 		mutex_init(&tx_ring->recycle_lock, NULL,
1002 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1003 		mutex_init(&tx_ring->tcb_head_lock, NULL,
1004 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1005 		mutex_init(&tx_ring->tcb_tail_lock, NULL,
1006 		    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1007 	}
1008 
1009 	mutex_init(&igb->gen_lock, NULL,
1010 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1011 
1012 	mutex_init(&igb->watchdog_lock, NULL,
1013 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1014 
1015 	mutex_init(&igb->link_lock, NULL,
1016 	    MUTEX_DRIVER, DDI_INTR_PRI(igb->intr_pri));
1017 }
1018 
1019 /*
1020  * igb_destroy_locks - Destroy locks
1021  */
1022 static void
1023 igb_destroy_locks(igb_t *igb)
1024 {
1025 	igb_rx_ring_t *rx_ring;
1026 	igb_tx_ring_t *tx_ring;
1027 	int i;
1028 
1029 	for (i = 0; i < igb->num_rx_rings; i++) {
1030 		rx_ring = &igb->rx_rings[i];
1031 		mutex_destroy(&rx_ring->rx_lock);
1032 	}
1033 
1034 	for (i = 0; i < igb->num_tx_rings; i++) {
1035 		tx_ring = &igb->tx_rings[i];
1036 		mutex_destroy(&tx_ring->tx_lock);
1037 		mutex_destroy(&tx_ring->recycle_lock);
1038 		mutex_destroy(&tx_ring->tcb_head_lock);
1039 		mutex_destroy(&tx_ring->tcb_tail_lock);
1040 	}
1041 
1042 	mutex_destroy(&igb->gen_lock);
1043 	mutex_destroy(&igb->watchdog_lock);
1044 	mutex_destroy(&igb->link_lock);
1045 }
1046 
1047 static int
1048 igb_resume(dev_info_t *devinfo)
1049 {
1050 	igb_t *igb;
1051 
1052 	igb = (igb_t *)ddi_get_driver_private(devinfo);
1053 	if (igb == NULL)
1054 		return (DDI_FAILURE);
1055 
1056 	mutex_enter(&igb->gen_lock);
1057 
1058 	/*
1059 	 * Enable interrupts
1060 	 */
1061 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
1062 		if (igb_enable_intrs(igb) != IGB_SUCCESS) {
1063 			igb_error(igb, "Failed to enable DDI interrupts");
1064 			mutex_exit(&igb->gen_lock);
1065 			return (DDI_FAILURE);
1066 		}
1067 	}
1068 
1069 	if (igb->igb_state & IGB_STARTED) {
1070 		if (igb_start(igb, B_FALSE) != IGB_SUCCESS) {
1071 			mutex_exit(&igb->gen_lock);
1072 			return (DDI_FAILURE);
1073 		}
1074 
1075 		/*
1076 		 * Enable and start the watchdog timer
1077 		 */
1078 		igb_enable_watchdog_timer(igb);
1079 	}
1080 
1081 	atomic_and_32(&igb->igb_state, ~IGB_SUSPENDED);
1082 
1083 	mutex_exit(&igb->gen_lock);
1084 
1085 	return (DDI_SUCCESS);
1086 }
1087 
1088 static int
1089 igb_suspend(dev_info_t *devinfo)
1090 {
1091 	igb_t *igb;
1092 
1093 	igb = (igb_t *)ddi_get_driver_private(devinfo);
1094 	if (igb == NULL)
1095 		return (DDI_FAILURE);
1096 
1097 	mutex_enter(&igb->gen_lock);
1098 
1099 	atomic_or_32(&igb->igb_state, IGB_SUSPENDED);
1100 
1101 	/*
1102 	 * Disable interrupts
1103 	 */
1104 	if (igb->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
1105 		(void) igb_disable_intrs(igb);
1106 	}
1107 
1108 	if (!(igb->igb_state & IGB_STARTED)) {
1109 		mutex_exit(&igb->gen_lock);
1110 		return (DDI_SUCCESS);
1111 	}
1112 
1113 	igb_stop(igb, B_FALSE);
1114 
1115 	mutex_exit(&igb->gen_lock);
1116 
1117 	/*
1118 	 * Disable and stop the watchdog timer
1119 	 */
1120 	igb_disable_watchdog_timer(igb);
1121 
1122 	return (DDI_SUCCESS);
1123 }
1124 
1125 static int
1126 igb_init(igb_t *igb)
1127 {
1128 	mutex_enter(&igb->gen_lock);
1129 
1130 	/*
1131 	 * Initilize the adapter
1132 	 */
1133 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
1134 		mutex_exit(&igb->gen_lock);
1135 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
1136 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
1137 		return (IGB_FAILURE);
1138 	}
1139 
1140 	mutex_exit(&igb->gen_lock);
1141 
1142 	return (IGB_SUCCESS);
1143 }
1144 
1145 /*
1146  * igb_init_mac_address - Initialize the default MAC address
1147  *
1148  * On success, the MAC address is entered in the igb->hw.mac.addr
1149  * and hw->mac.perm_addr fields and the adapter's RAR(0) receive
1150  * address register.
1151  *
1152  * Important side effects:
1153  * 1. adapter is reset - this is required to put it in a known state.
1154  * 2. all of non-volatile memory (NVM) is read & checksummed - NVM is where
1155  * MAC address and all default settings are stored, so a valid checksum
1156  * is required.
1157  */
1158 static int
1159 igb_init_mac_address(igb_t *igb)
1160 {
1161 	struct e1000_hw *hw = &igb->hw;
1162 
1163 	ASSERT(mutex_owned(&igb->gen_lock));
1164 
1165 	/*
1166 	 * Reset chipset to put the hardware in a known state
1167 	 * before we try to get MAC address from NVM.
1168 	 */
1169 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
1170 		igb_error(igb, "Adapter reset failed.");
1171 		goto init_mac_fail;
1172 	}
1173 
1174 	/*
1175 	 * NVM validation
1176 	 */
1177 	if (e1000_validate_nvm_checksum(hw) < 0) {
1178 		/*
1179 		 * Some PCI-E parts fail the first check due to
1180 		 * the link being in sleep state.  Call it again,
1181 		 * if it fails a second time its a real issue.
1182 		 */
1183 		if (e1000_validate_nvm_checksum(hw) < 0) {
1184 			igb_error(igb,
1185 			    "Invalid NVM checksum. Please contact "
1186 			    "the vendor to update the NVM.");
1187 			goto init_mac_fail;
1188 		}
1189 	}
1190 
1191 	/*
1192 	 * Get the mac address
1193 	 * This function should handle SPARC case correctly.
1194 	 */
1195 	if (!igb_find_mac_address(igb)) {
1196 		igb_error(igb, "Failed to get the mac address");
1197 		goto init_mac_fail;
1198 	}
1199 
1200 	/* Validate mac address */
1201 	if (!is_valid_mac_addr(hw->mac.addr)) {
1202 		igb_error(igb, "Invalid mac address");
1203 		goto init_mac_fail;
1204 	}
1205 
1206 	return (IGB_SUCCESS);
1207 
1208 init_mac_fail:
1209 	return (IGB_FAILURE);
1210 }
1211 
1212 /*
1213  * igb_init_adapter - Initialize the adapter
1214  */
1215 static int
1216 igb_init_adapter(igb_t *igb)
1217 {
1218 	struct e1000_hw *hw = &igb->hw;
1219 	uint32_t pba;
1220 	uint32_t high_water;
1221 	int i;
1222 
1223 	ASSERT(mutex_owned(&igb->gen_lock));
1224 
1225 	/*
1226 	 * In order to obtain the default MAC address, this will reset the
1227 	 * adapter and validate the NVM that the address and many other
1228 	 * default settings come from.
1229 	 */
1230 	if (igb_init_mac_address(igb) != IGB_SUCCESS) {
1231 		igb_error(igb, "Failed to initialize MAC address");
1232 		goto init_adapter_fail;
1233 	}
1234 
1235 	/*
1236 	 * Setup flow control
1237 	 *
1238 	 * These parameters set thresholds for the adapter's generation(Tx)
1239 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
1240 	 * settings.  Flow control is enabled or disabled in the configuration
1241 	 * file.
1242 	 * High-water mark is set down from the top of the rx fifo (not
1243 	 * sensitive to max_frame_size) and low-water is set just below
1244 	 * high-water mark.
1245 	 * The high water mark must be low enough to fit one full frame above
1246 	 * it in the rx FIFO.  Should be the lower of:
1247 	 * 90% of the Rx FIFO size, or the full Rx FIFO size minus one full
1248 	 * frame.
1249 	 */
1250 	/*
1251 	 * The default setting of PBA is correct for 82575 and other supported
1252 	 * adapters do not have the E1000_PBA register, so PBA value is only
1253 	 * used for calculation here and is never written to the adapter.
1254 	 */
1255 	if (hw->mac.type == e1000_82575) {
1256 		pba = E1000_PBA_34K;
1257 	} else {
1258 		pba = E1000_PBA_64K;
1259 	}
1260 
1261 	high_water = min(((pba << 10) * 9 / 10),
1262 	    ((pba << 10) - igb->max_frame_size));
1263 
1264 	if (hw->mac.type == e1000_82575) {
1265 		/* 8-byte granularity */
1266 		hw->fc.high_water = high_water & 0xFFF8;
1267 		hw->fc.low_water = hw->fc.high_water - 8;
1268 	} else {
1269 		/* 16-byte granularity */
1270 		hw->fc.high_water = high_water & 0xFFF0;
1271 		hw->fc.low_water = hw->fc.high_water - 16;
1272 	}
1273 
1274 	hw->fc.pause_time = E1000_FC_PAUSE_TIME;
1275 	hw->fc.send_xon = B_TRUE;
1276 
1277 	(void) e1000_validate_mdi_setting(hw);
1278 
1279 	/*
1280 	 * Reset the chipset hardware the second time to put PBA settings
1281 	 * into effect.
1282 	 */
1283 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
1284 		igb_error(igb, "Second reset failed");
1285 		goto init_adapter_fail;
1286 	}
1287 
1288 	/*
1289 	 * Don't wait for auto-negotiation to complete
1290 	 */
1291 	hw->phy.autoneg_wait_to_complete = B_FALSE;
1292 
1293 	/*
1294 	 * Copper options
1295 	 */
1296 	if (hw->phy.media_type == e1000_media_type_copper) {
1297 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
1298 		hw->phy.disable_polarity_correction = B_FALSE;
1299 		hw->phy.ms_type = e1000_ms_hw_default; /* E1000_MASTER_SLAVE */
1300 	}
1301 
1302 	/*
1303 	 * Initialize link settings
1304 	 */
1305 	(void) igb_setup_link(igb, B_FALSE);
1306 
1307 	/*
1308 	 * Configure/Initialize hardware
1309 	 */
1310 	if (e1000_init_hw(hw) != E1000_SUCCESS) {
1311 		igb_error(igb, "Failed to initialize hardware");
1312 		goto init_adapter_fail;
1313 	}
1314 
1315 	/*
1316 	 *  Start the link setup timer
1317 	 */
1318 	igb_start_link_timer(igb);
1319 
1320 	/*
1321 	 * Disable wakeup control by default
1322 	 */
1323 	E1000_WRITE_REG(hw, E1000_WUC, 0);
1324 
1325 	/*
1326 	 * Record phy info in hw struct
1327 	 */
1328 	(void) e1000_get_phy_info(hw);
1329 
1330 	/*
1331 	 * Make sure driver has control
1332 	 */
1333 	igb_get_driver_control(hw);
1334 
1335 	/*
1336 	 * Restore LED settings to the default from EEPROM
1337 	 * to meet the standard for Sun platforms.
1338 	 */
1339 	(void) e1000_cleanup_led(hw);
1340 
1341 	/*
1342 	 * Setup MSI-X interrupts
1343 	 */
1344 	if (igb->intr_type == DDI_INTR_TYPE_MSIX)
1345 		igb->capab->setup_msix(igb);
1346 
1347 	/*
1348 	 * Initialize unicast addresses.
1349 	 */
1350 	igb_init_unicst(igb);
1351 
1352 	/*
1353 	 * Setup and initialize the mctable structures.
1354 	 */
1355 	igb_setup_multicst(igb);
1356 
1357 	/*
1358 	 * Set interrupt throttling rate
1359 	 */
1360 	for (i = 0; i < igb->intr_cnt; i++)
1361 		E1000_WRITE_REG(hw, E1000_EITR(i), igb->intr_throttling[i]);
1362 
1363 	/*
1364 	 * Save the state of the phy
1365 	 */
1366 	igb_get_phy_state(igb);
1367 
1368 	igb_param_sync(igb);
1369 
1370 	return (IGB_SUCCESS);
1371 
1372 init_adapter_fail:
1373 	/*
1374 	 * Reset PHY if possible
1375 	 */
1376 	if (e1000_check_reset_block(hw) == E1000_SUCCESS)
1377 		(void) e1000_phy_hw_reset(hw);
1378 
1379 	return (IGB_FAILURE);
1380 }
1381 
1382 /*
1383  * igb_stop_adapter - Stop the adapter
1384  */
1385 static void
1386 igb_stop_adapter(igb_t *igb)
1387 {
1388 	struct e1000_hw *hw = &igb->hw;
1389 
1390 	ASSERT(mutex_owned(&igb->gen_lock));
1391 
1392 	/* Stop the link setup timer */
1393 	igb_stop_link_timer(igb);
1394 
1395 	/* Tell firmware driver is no longer in control */
1396 	igb_release_driver_control(hw);
1397 
1398 	/*
1399 	 * Reset the chipset
1400 	 */
1401 	if (e1000_reset_hw(hw) != E1000_SUCCESS) {
1402 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
1403 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
1404 	}
1405 
1406 	/*
1407 	 * e1000_phy_hw_reset is not needed here, MAC reset above is sufficient
1408 	 */
1409 }
1410 
1411 /*
1412  * igb_reset - Reset the chipset and restart the driver.
1413  *
1414  * It involves stopping and re-starting the chipset,
1415  * and re-configuring the rx/tx rings.
1416  */
1417 static int
1418 igb_reset(igb_t *igb)
1419 {
1420 	int i;
1421 
1422 	mutex_enter(&igb->gen_lock);
1423 
1424 	ASSERT(igb->igb_state & IGB_STARTED);
1425 	atomic_and_32(&igb->igb_state, ~IGB_STARTED);
1426 
1427 	/*
1428 	 * Disable the adapter interrupts to stop any rx/tx activities
1429 	 * before draining pending data and resetting hardware.
1430 	 */
1431 	igb_disable_adapter_interrupts(igb);
1432 
1433 	/*
1434 	 * Drain the pending transmit packets
1435 	 */
1436 	(void) igb_tx_drain(igb);
1437 
1438 	for (i = 0; i < igb->num_rx_rings; i++)
1439 		mutex_enter(&igb->rx_rings[i].rx_lock);
1440 	for (i = 0; i < igb->num_tx_rings; i++)
1441 		mutex_enter(&igb->tx_rings[i].tx_lock);
1442 
1443 	/*
1444 	 * Stop the adapter
1445 	 */
1446 	igb_stop_adapter(igb);
1447 
1448 	/*
1449 	 * Clean the pending tx data/resources
1450 	 */
1451 	igb_tx_clean(igb);
1452 
1453 	/*
1454 	 * Start the adapter
1455 	 */
1456 	if (igb_init_adapter(igb) != IGB_SUCCESS) {
1457 		igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
1458 		goto reset_failure;
1459 	}
1460 
1461 	/*
1462 	 * Setup the rx/tx rings
1463 	 */
1464 	igb->tx_ring_init = B_FALSE;
1465 	igb_setup_rings(igb);
1466 
1467 	atomic_and_32(&igb->igb_state, ~(IGB_ERROR | IGB_STALL));
1468 
1469 	/*
1470 	 * Enable adapter interrupts
1471 	 * The interrupts must be enabled after the driver state is START
1472 	 */
1473 	igb->capab->enable_intr(igb);
1474 
1475 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
1476 		goto reset_failure;
1477 
1478 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
1479 		goto reset_failure;
1480 
1481 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1482 		mutex_exit(&igb->tx_rings[i].tx_lock);
1483 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1484 		mutex_exit(&igb->rx_rings[i].rx_lock);
1485 
1486 	atomic_or_32(&igb->igb_state, IGB_STARTED);
1487 
1488 	mutex_exit(&igb->gen_lock);
1489 
1490 	return (IGB_SUCCESS);
1491 
1492 reset_failure:
1493 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1494 		mutex_exit(&igb->tx_rings[i].tx_lock);
1495 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1496 		mutex_exit(&igb->rx_rings[i].rx_lock);
1497 
1498 	mutex_exit(&igb->gen_lock);
1499 
1500 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
1501 
1502 	return (IGB_FAILURE);
1503 }
1504 
1505 /*
1506  * igb_tx_clean - Clean the pending transmit packets and DMA resources
1507  */
1508 static void
1509 igb_tx_clean(igb_t *igb)
1510 {
1511 	igb_tx_ring_t *tx_ring;
1512 	tx_control_block_t *tcb;
1513 	link_list_t pending_list;
1514 	uint32_t desc_num;
1515 	int i, j;
1516 
1517 	LINK_LIST_INIT(&pending_list);
1518 
1519 	for (i = 0; i < igb->num_tx_rings; i++) {
1520 		tx_ring = &igb->tx_rings[i];
1521 
1522 		mutex_enter(&tx_ring->recycle_lock);
1523 
1524 		/*
1525 		 * Clean the pending tx data - the pending packets in the
1526 		 * work_list that have no chances to be transmitted again.
1527 		 *
1528 		 * We must ensure the chipset is stopped or the link is down
1529 		 * before cleaning the transmit packets.
1530 		 */
1531 		desc_num = 0;
1532 		for (j = 0; j < tx_ring->ring_size; j++) {
1533 			tcb = tx_ring->work_list[j];
1534 			if (tcb != NULL) {
1535 				desc_num += tcb->desc_num;
1536 
1537 				tx_ring->work_list[j] = NULL;
1538 
1539 				igb_free_tcb(tcb);
1540 
1541 				LIST_PUSH_TAIL(&pending_list, &tcb->link);
1542 			}
1543 		}
1544 
1545 		if (desc_num > 0) {
1546 			atomic_add_32(&tx_ring->tbd_free, desc_num);
1547 			ASSERT(tx_ring->tbd_free == tx_ring->ring_size);
1548 
1549 			/*
1550 			 * Reset the head and tail pointers of the tbd ring;
1551 			 * Reset the head write-back if it is enabled.
1552 			 */
1553 			tx_ring->tbd_head = 0;
1554 			tx_ring->tbd_tail = 0;
1555 			if (igb->tx_head_wb_enable)
1556 				*tx_ring->tbd_head_wb = 0;
1557 
1558 			E1000_WRITE_REG(&igb->hw, E1000_TDH(tx_ring->index), 0);
1559 			E1000_WRITE_REG(&igb->hw, E1000_TDT(tx_ring->index), 0);
1560 		}
1561 
1562 		mutex_exit(&tx_ring->recycle_lock);
1563 
1564 		/*
1565 		 * Add the tx control blocks in the pending list to
1566 		 * the free list.
1567 		 */
1568 		igb_put_free_list(tx_ring, &pending_list);
1569 	}
1570 }
1571 
1572 /*
1573  * igb_tx_drain - Drain the tx rings to allow pending packets to be transmitted
1574  */
1575 static boolean_t
1576 igb_tx_drain(igb_t *igb)
1577 {
1578 	igb_tx_ring_t *tx_ring;
1579 	boolean_t done;
1580 	int i, j;
1581 
1582 	/*
1583 	 * Wait for a specific time to allow pending tx packets
1584 	 * to be transmitted.
1585 	 *
1586 	 * Check the counter tbd_free to see if transmission is done.
1587 	 * No lock protection is needed here.
1588 	 *
1589 	 * Return B_TRUE if all pending packets have been transmitted;
1590 	 * Otherwise return B_FALSE;
1591 	 */
1592 	for (i = 0; i < TX_DRAIN_TIME; i++) {
1593 
1594 		done = B_TRUE;
1595 		for (j = 0; j < igb->num_tx_rings; j++) {
1596 			tx_ring = &igb->tx_rings[j];
1597 			done = done &&
1598 			    (tx_ring->tbd_free == tx_ring->ring_size);
1599 		}
1600 
1601 		if (done)
1602 			break;
1603 
1604 		msec_delay(1);
1605 	}
1606 
1607 	return (done);
1608 }
1609 
1610 /*
1611  * igb_rx_drain - Wait for all rx buffers to be released by upper layer
1612  */
1613 static boolean_t
1614 igb_rx_drain(igb_t *igb)
1615 {
1616 	boolean_t done;
1617 	int i;
1618 
1619 	/*
1620 	 * Polling the rx free list to check if those rx buffers held by
1621 	 * the upper layer are released.
1622 	 *
1623 	 * Check the counter rcb_free to see if all pending buffers are
1624 	 * released. No lock protection is needed here.
1625 	 *
1626 	 * Return B_TRUE if all pending buffers have been released;
1627 	 * Otherwise return B_FALSE;
1628 	 */
1629 	for (i = 0; i < RX_DRAIN_TIME; i++) {
1630 		done = (igb->rcb_pending == 0);
1631 
1632 		if (done)
1633 			break;
1634 
1635 		msec_delay(1);
1636 	}
1637 
1638 	return (done);
1639 }
1640 
1641 /*
1642  * igb_start - Start the driver/chipset
1643  */
1644 int
1645 igb_start(igb_t *igb, boolean_t alloc_buffer)
1646 {
1647 	int i;
1648 
1649 	ASSERT(mutex_owned(&igb->gen_lock));
1650 
1651 	if (alloc_buffer) {
1652 		if (igb_alloc_rx_data(igb) != IGB_SUCCESS) {
1653 			igb_error(igb,
1654 			    "Failed to allocate software receive rings");
1655 			return (IGB_FAILURE);
1656 		}
1657 
1658 		/* Allocate buffers for all the rx/tx rings */
1659 		if (igb_alloc_dma(igb) != IGB_SUCCESS) {
1660 			igb_error(igb, "Failed to allocate DMA resource");
1661 			return (IGB_FAILURE);
1662 		}
1663 
1664 		igb->tx_ring_init = B_TRUE;
1665 	} else {
1666 		igb->tx_ring_init = B_FALSE;
1667 	}
1668 
1669 	for (i = 0; i < igb->num_rx_rings; i++)
1670 		mutex_enter(&igb->rx_rings[i].rx_lock);
1671 	for (i = 0; i < igb->num_tx_rings; i++)
1672 		mutex_enter(&igb->tx_rings[i].tx_lock);
1673 
1674 	/*
1675 	 * Start the adapter
1676 	 */
1677 	if ((igb->attach_progress & ATTACH_PROGRESS_INIT_ADAPTER) == 0) {
1678 		if (igb_init_adapter(igb) != IGB_SUCCESS) {
1679 			igb_fm_ereport(igb, DDI_FM_DEVICE_INVAL_STATE);
1680 			goto start_failure;
1681 		}
1682 		igb->attach_progress |= ATTACH_PROGRESS_INIT_ADAPTER;
1683 	}
1684 
1685 	/*
1686 	 * Setup the rx/tx rings
1687 	 */
1688 	igb_setup_rings(igb);
1689 
1690 	/*
1691 	 * Enable adapter interrupts
1692 	 * The interrupts must be enabled after the driver state is START
1693 	 */
1694 	igb->capab->enable_intr(igb);
1695 
1696 	if (igb_check_acc_handle(igb->osdep.cfg_handle) != DDI_FM_OK)
1697 		goto start_failure;
1698 
1699 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
1700 		goto start_failure;
1701 
1702 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1703 		mutex_exit(&igb->tx_rings[i].tx_lock);
1704 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1705 		mutex_exit(&igb->rx_rings[i].rx_lock);
1706 
1707 	return (IGB_SUCCESS);
1708 
1709 start_failure:
1710 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1711 		mutex_exit(&igb->tx_rings[i].tx_lock);
1712 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1713 		mutex_exit(&igb->rx_rings[i].rx_lock);
1714 
1715 	ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
1716 
1717 	return (IGB_FAILURE);
1718 }
1719 
1720 /*
1721  * igb_stop - Stop the driver/chipset
1722  */
1723 void
1724 igb_stop(igb_t *igb, boolean_t free_buffer)
1725 {
1726 	int i;
1727 
1728 	ASSERT(mutex_owned(&igb->gen_lock));
1729 
1730 	igb->attach_progress &= ~ATTACH_PROGRESS_INIT_ADAPTER;
1731 
1732 	/*
1733 	 * Disable the adapter interrupts
1734 	 */
1735 	igb_disable_adapter_interrupts(igb);
1736 
1737 	/*
1738 	 * Drain the pending tx packets
1739 	 */
1740 	(void) igb_tx_drain(igb);
1741 
1742 	for (i = 0; i < igb->num_rx_rings; i++)
1743 		mutex_enter(&igb->rx_rings[i].rx_lock);
1744 	for (i = 0; i < igb->num_tx_rings; i++)
1745 		mutex_enter(&igb->tx_rings[i].tx_lock);
1746 
1747 	/*
1748 	 * Stop the adapter
1749 	 */
1750 	igb_stop_adapter(igb);
1751 
1752 	/*
1753 	 * Clean the pending tx data/resources
1754 	 */
1755 	igb_tx_clean(igb);
1756 
1757 	for (i = igb->num_tx_rings - 1; i >= 0; i--)
1758 		mutex_exit(&igb->tx_rings[i].tx_lock);
1759 	for (i = igb->num_rx_rings - 1; i >= 0; i--)
1760 		mutex_exit(&igb->rx_rings[i].rx_lock);
1761 
1762 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK)
1763 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
1764 
1765 	if (igb->link_state == LINK_STATE_UP) {
1766 		igb->link_state = LINK_STATE_UNKNOWN;
1767 		mac_link_update(igb->mac_hdl, igb->link_state);
1768 	}
1769 
1770 	if (free_buffer) {
1771 		/*
1772 		 * Release the DMA/memory resources of rx/tx rings
1773 		 */
1774 		igb_free_dma(igb);
1775 		igb_free_rx_data(igb);
1776 	}
1777 }
1778 
1779 /*
1780  * igb_alloc_rings - Allocate memory space for rx/tx rings
1781  */
1782 static int
1783 igb_alloc_rings(igb_t *igb)
1784 {
1785 	/*
1786 	 * Allocate memory space for rx rings
1787 	 */
1788 	igb->rx_rings = kmem_zalloc(
1789 	    sizeof (igb_rx_ring_t) * igb->num_rx_rings,
1790 	    KM_NOSLEEP);
1791 
1792 	if (igb->rx_rings == NULL) {
1793 		return (IGB_FAILURE);
1794 	}
1795 
1796 	/*
1797 	 * Allocate memory space for tx rings
1798 	 */
1799 	igb->tx_rings = kmem_zalloc(
1800 	    sizeof (igb_tx_ring_t) * igb->num_tx_rings,
1801 	    KM_NOSLEEP);
1802 
1803 	if (igb->tx_rings == NULL) {
1804 		kmem_free(igb->rx_rings,
1805 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
1806 		igb->rx_rings = NULL;
1807 		return (IGB_FAILURE);
1808 	}
1809 
1810 	/*
1811 	 * Allocate memory space for rx ring groups
1812 	 */
1813 	igb->rx_groups = kmem_zalloc(
1814 	    sizeof (igb_rx_group_t) * igb->num_rx_groups,
1815 	    KM_NOSLEEP);
1816 
1817 	if (igb->rx_groups == NULL) {
1818 		kmem_free(igb->rx_rings,
1819 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
1820 		kmem_free(igb->tx_rings,
1821 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
1822 		igb->rx_rings = NULL;
1823 		igb->tx_rings = NULL;
1824 		return (IGB_FAILURE);
1825 	}
1826 
1827 	return (IGB_SUCCESS);
1828 }
1829 
1830 /*
1831  * igb_free_rings - Free the memory space of rx/tx rings.
1832  */
1833 static void
1834 igb_free_rings(igb_t *igb)
1835 {
1836 	if (igb->rx_rings != NULL) {
1837 		kmem_free(igb->rx_rings,
1838 		    sizeof (igb_rx_ring_t) * igb->num_rx_rings);
1839 		igb->rx_rings = NULL;
1840 	}
1841 
1842 	if (igb->tx_rings != NULL) {
1843 		kmem_free(igb->tx_rings,
1844 		    sizeof (igb_tx_ring_t) * igb->num_tx_rings);
1845 		igb->tx_rings = NULL;
1846 	}
1847 
1848 	if (igb->rx_groups != NULL) {
1849 		kmem_free(igb->rx_groups,
1850 		    sizeof (igb_rx_group_t) * igb->num_rx_groups);
1851 		igb->rx_groups = NULL;
1852 	}
1853 }
1854 
1855 static int
1856 igb_alloc_rx_data(igb_t *igb)
1857 {
1858 	igb_rx_ring_t *rx_ring;
1859 	int i;
1860 
1861 	for (i = 0; i < igb->num_rx_rings; i++) {
1862 		rx_ring = &igb->rx_rings[i];
1863 		if (igb_alloc_rx_ring_data(rx_ring) != IGB_SUCCESS)
1864 			goto alloc_rx_rings_failure;
1865 	}
1866 	return (IGB_SUCCESS);
1867 
1868 alloc_rx_rings_failure:
1869 	igb_free_rx_data(igb);
1870 	return (IGB_FAILURE);
1871 }
1872 
1873 static void
1874 igb_free_rx_data(igb_t *igb)
1875 {
1876 	igb_rx_ring_t *rx_ring;
1877 	igb_rx_data_t *rx_data;
1878 	int i;
1879 
1880 	for (i = 0; i < igb->num_rx_rings; i++) {
1881 		rx_ring = &igb->rx_rings[i];
1882 
1883 		mutex_enter(&igb->rx_pending_lock);
1884 		rx_data = rx_ring->rx_data;
1885 
1886 		if (rx_data != NULL) {
1887 			rx_data->flag |= IGB_RX_STOPPED;
1888 
1889 			if (rx_data->rcb_pending == 0) {
1890 				igb_free_rx_ring_data(rx_data);
1891 				rx_ring->rx_data = NULL;
1892 			}
1893 		}
1894 
1895 		mutex_exit(&igb->rx_pending_lock);
1896 	}
1897 }
1898 
1899 /*
1900  * igb_setup_rings - Setup rx/tx rings
1901  */
1902 static void
1903 igb_setup_rings(igb_t *igb)
1904 {
1905 	/*
1906 	 * Setup the rx/tx rings, including the following:
1907 	 *
1908 	 * 1. Setup the descriptor ring and the control block buffers;
1909 	 * 2. Initialize necessary registers for receive/transmit;
1910 	 * 3. Initialize software pointers/parameters for receive/transmit;
1911 	 */
1912 	igb_setup_rx(igb);
1913 
1914 	igb_setup_tx(igb);
1915 }
1916 
1917 static void
1918 igb_setup_rx_ring(igb_rx_ring_t *rx_ring)
1919 {
1920 	igb_t *igb = rx_ring->igb;
1921 	igb_rx_data_t *rx_data = rx_ring->rx_data;
1922 	struct e1000_hw *hw = &igb->hw;
1923 	rx_control_block_t *rcb;
1924 	union e1000_adv_rx_desc	*rbd;
1925 	uint32_t size;
1926 	uint32_t buf_low;
1927 	uint32_t buf_high;
1928 	uint32_t rxdctl;
1929 	int i;
1930 
1931 	ASSERT(mutex_owned(&rx_ring->rx_lock));
1932 	ASSERT(mutex_owned(&igb->gen_lock));
1933 
1934 	/*
1935 	 * Initialize descriptor ring with buffer addresses
1936 	 */
1937 	for (i = 0; i < igb->rx_ring_size; i++) {
1938 		rcb = rx_data->work_list[i];
1939 		rbd = &rx_data->rbd_ring[i];
1940 
1941 		rbd->read.pkt_addr = rcb->rx_buf.dma_address;
1942 		rbd->read.hdr_addr = NULL;
1943 	}
1944 
1945 	/*
1946 	 * Initialize the base address registers
1947 	 */
1948 	buf_low = (uint32_t)rx_data->rbd_area.dma_address;
1949 	buf_high = (uint32_t)(rx_data->rbd_area.dma_address >> 32);
1950 	E1000_WRITE_REG(hw, E1000_RDBAH(rx_ring->index), buf_high);
1951 	E1000_WRITE_REG(hw, E1000_RDBAL(rx_ring->index), buf_low);
1952 
1953 	/*
1954 	 * Initialize the length register
1955 	 */
1956 	size = rx_data->ring_size * sizeof (union e1000_adv_rx_desc);
1957 	E1000_WRITE_REG(hw, E1000_RDLEN(rx_ring->index), size);
1958 
1959 	/*
1960 	 * Initialize buffer size & descriptor type
1961 	 */
1962 	E1000_WRITE_REG(hw, E1000_SRRCTL(rx_ring->index),
1963 	    ((igb->rx_buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) |
1964 	    E1000_SRRCTL_DESCTYPE_ADV_ONEBUF));
1965 
1966 	/*
1967 	 * Setup the Receive Descriptor Control Register (RXDCTL)
1968 	 */
1969 	rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(rx_ring->index));
1970 	rxdctl &= igb->capab->rxdctl_mask;
1971 	rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
1972 	rxdctl |= 16;		/* pthresh */
1973 	rxdctl |= 8 << 8;	/* hthresh */
1974 	rxdctl |= 1 << 16;	/* wthresh */
1975 	E1000_WRITE_REG(hw, E1000_RXDCTL(rx_ring->index), rxdctl);
1976 
1977 	rx_data->rbd_next = 0;
1978 }
1979 
1980 static void
1981 igb_setup_rx(igb_t *igb)
1982 {
1983 	igb_rx_ring_t *rx_ring;
1984 	igb_rx_data_t *rx_data;
1985 	igb_rx_group_t *rx_group;
1986 	struct e1000_hw *hw = &igb->hw;
1987 	uint32_t rctl, rxcsum;
1988 	uint32_t ring_per_group;
1989 	int i;
1990 
1991 	/*
1992 	 * Setup the Receive Control Register (RCTL), and enable the
1993 	 * receiver. The initial configuration is to: enable the receiver,
1994 	 * accept broadcasts, discard bad packets, accept long packets,
1995 	 * disable VLAN filter checking, and set receive buffer size to
1996 	 * 2k.  For 82575, also set the receive descriptor minimum
1997 	 * threshold size to 1/2 the ring.
1998 	 */
1999 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2000 
2001 	/*
2002 	 * Clear the field used for wakeup control.  This driver doesn't do
2003 	 * wakeup but leave this here for completeness.
2004 	 */
2005 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2006 	rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
2007 
2008 	rctl |= (E1000_RCTL_EN |	/* Enable Receive Unit */
2009 	    E1000_RCTL_BAM |		/* Accept Broadcast Packets */
2010 	    E1000_RCTL_LPE |		/* Large Packet Enable */
2011 					/* Multicast filter offset */
2012 	    (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT) |
2013 	    E1000_RCTL_RDMTS_HALF |	/* rx descriptor threshold */
2014 	    E1000_RCTL_SECRC);		/* Strip Ethernet CRC */
2015 
2016 	for (i = 0; i < igb->num_rx_groups; i++) {
2017 		rx_group = &igb->rx_groups[i];
2018 		rx_group->index = i;
2019 		rx_group->igb = igb;
2020 	}
2021 
2022 	/*
2023 	 * Set up all rx descriptor rings - must be called before receive unit
2024 	 * enabled.
2025 	 */
2026 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
2027 	for (i = 0; i < igb->num_rx_rings; i++) {
2028 		rx_ring = &igb->rx_rings[i];
2029 		igb_setup_rx_ring(rx_ring);
2030 
2031 		/*
2032 		 * Map a ring to a group by assigning a group index
2033 		 */
2034 		rx_ring->group_index = i / ring_per_group;
2035 	}
2036 
2037 	/*
2038 	 * Setup the Rx Long Packet Max Length register
2039 	 */
2040 	E1000_WRITE_REG(hw, E1000_RLPML, igb->max_frame_size);
2041 
2042 	/*
2043 	 * Hardware checksum settings
2044 	 */
2045 	if (igb->rx_hcksum_enable) {
2046 		rxcsum =
2047 		    E1000_RXCSUM_TUOFL |	/* TCP/UDP checksum */
2048 		    E1000_RXCSUM_IPOFL;		/* IP checksum */
2049 
2050 		E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
2051 	}
2052 
2053 	/*
2054 	 * Setup classify and RSS for multiple receive queues
2055 	 */
2056 	switch (igb->vmdq_mode) {
2057 	case E1000_VMDQ_OFF:
2058 		/*
2059 		 * One ring group, only RSS is needed when more than
2060 		 * one ring enabled.
2061 		 */
2062 		if (igb->num_rx_rings > 1)
2063 			igb_setup_rss(igb);
2064 		break;
2065 	case E1000_VMDQ_MAC:
2066 		/*
2067 		 * Multiple groups, each group has one ring,
2068 		 * only the MAC classification is needed.
2069 		 */
2070 		igb_setup_mac_classify(igb);
2071 		break;
2072 	case E1000_VMDQ_MAC_RSS:
2073 		/*
2074 		 * Multiple groups and multiple rings, both
2075 		 * MAC classification and RSS are needed.
2076 		 */
2077 		igb_setup_mac_rss_classify(igb);
2078 		break;
2079 	}
2080 
2081 	/*
2082 	 * Enable the receive unit - must be done after all
2083 	 * the rx setup above.
2084 	 */
2085 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2086 
2087 	/*
2088 	 * Initialize all adapter ring head & tail pointers - must
2089 	 * be done after receive unit is enabled
2090 	 */
2091 	for (i = 0; i < igb->num_rx_rings; i++) {
2092 		rx_ring = &igb->rx_rings[i];
2093 		rx_data = rx_ring->rx_data;
2094 		E1000_WRITE_REG(hw, E1000_RDH(i), 0);
2095 		E1000_WRITE_REG(hw, E1000_RDT(i), rx_data->ring_size - 1);
2096 	}
2097 
2098 	/*
2099 	 * 82575 with manageability enabled needs a special flush to make
2100 	 * sure the fifos start clean.
2101 	 */
2102 	if ((hw->mac.type == e1000_82575) &&
2103 	    (E1000_READ_REG(hw, E1000_MANC) & E1000_MANC_RCV_TCO_EN)) {
2104 		e1000_rx_fifo_flush_82575(hw);
2105 	}
2106 }
2107 
2108 static void
2109 igb_setup_tx_ring(igb_tx_ring_t *tx_ring)
2110 {
2111 	igb_t *igb = tx_ring->igb;
2112 	struct e1000_hw *hw = &igb->hw;
2113 	uint32_t size;
2114 	uint32_t buf_low;
2115 	uint32_t buf_high;
2116 	uint32_t reg_val;
2117 
2118 	ASSERT(mutex_owned(&tx_ring->tx_lock));
2119 	ASSERT(mutex_owned(&igb->gen_lock));
2120 
2121 
2122 	/*
2123 	 * Initialize the length register
2124 	 */
2125 	size = tx_ring->ring_size * sizeof (union e1000_adv_tx_desc);
2126 	E1000_WRITE_REG(hw, E1000_TDLEN(tx_ring->index), size);
2127 
2128 	/*
2129 	 * Initialize the base address registers
2130 	 */
2131 	buf_low = (uint32_t)tx_ring->tbd_area.dma_address;
2132 	buf_high = (uint32_t)(tx_ring->tbd_area.dma_address >> 32);
2133 	E1000_WRITE_REG(hw, E1000_TDBAL(tx_ring->index), buf_low);
2134 	E1000_WRITE_REG(hw, E1000_TDBAH(tx_ring->index), buf_high);
2135 
2136 	/*
2137 	 * Setup head & tail pointers
2138 	 */
2139 	E1000_WRITE_REG(hw, E1000_TDH(tx_ring->index), 0);
2140 	E1000_WRITE_REG(hw, E1000_TDT(tx_ring->index), 0);
2141 
2142 	/*
2143 	 * Setup head write-back
2144 	 */
2145 	if (igb->tx_head_wb_enable) {
2146 		/*
2147 		 * The memory of the head write-back is allocated using
2148 		 * the extra tbd beyond the tail of the tbd ring.
2149 		 */
2150 		tx_ring->tbd_head_wb = (uint32_t *)
2151 		    ((uintptr_t)tx_ring->tbd_area.address + size);
2152 		*tx_ring->tbd_head_wb = 0;
2153 
2154 		buf_low = (uint32_t)
2155 		    (tx_ring->tbd_area.dma_address + size);
2156 		buf_high = (uint32_t)
2157 		    ((tx_ring->tbd_area.dma_address + size) >> 32);
2158 
2159 		/* Set the head write-back enable bit */
2160 		buf_low |= E1000_TX_HEAD_WB_ENABLE;
2161 
2162 		E1000_WRITE_REG(hw, E1000_TDWBAL(tx_ring->index), buf_low);
2163 		E1000_WRITE_REG(hw, E1000_TDWBAH(tx_ring->index), buf_high);
2164 
2165 		/*
2166 		 * Turn off relaxed ordering for head write back or it will
2167 		 * cause problems with the tx recycling
2168 		 */
2169 		reg_val = E1000_READ_REG(hw,
2170 		    E1000_DCA_TXCTRL(tx_ring->index));
2171 		reg_val &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
2172 		E1000_WRITE_REG(hw,
2173 		    E1000_DCA_TXCTRL(tx_ring->index), reg_val);
2174 	} else {
2175 		tx_ring->tbd_head_wb = NULL;
2176 	}
2177 
2178 	tx_ring->tbd_head = 0;
2179 	tx_ring->tbd_tail = 0;
2180 	tx_ring->tbd_free = tx_ring->ring_size;
2181 
2182 	if (igb->tx_ring_init == B_TRUE) {
2183 		tx_ring->tcb_head = 0;
2184 		tx_ring->tcb_tail = 0;
2185 		tx_ring->tcb_free = tx_ring->free_list_size;
2186 	}
2187 
2188 	/*
2189 	 * Enable TXDCTL per queue
2190 	 */
2191 	reg_val = E1000_READ_REG(hw, E1000_TXDCTL(tx_ring->index));
2192 	reg_val |= E1000_TXDCTL_QUEUE_ENABLE;
2193 	E1000_WRITE_REG(hw, E1000_TXDCTL(tx_ring->index), reg_val);
2194 
2195 	/*
2196 	 * Initialize hardware checksum offload settings
2197 	 */
2198 	bzero(&tx_ring->tx_context, sizeof (tx_context_t));
2199 }
2200 
2201 static void
2202 igb_setup_tx(igb_t *igb)
2203 {
2204 	igb_tx_ring_t *tx_ring;
2205 	struct e1000_hw *hw = &igb->hw;
2206 	uint32_t reg_val;
2207 	int i;
2208 
2209 	for (i = 0; i < igb->num_tx_rings; i++) {
2210 		tx_ring = &igb->tx_rings[i];
2211 		igb_setup_tx_ring(tx_ring);
2212 	}
2213 
2214 	/*
2215 	 * Setup the Transmit Control Register (TCTL)
2216 	 */
2217 	reg_val = E1000_READ_REG(hw, E1000_TCTL);
2218 	reg_val &= ~E1000_TCTL_CT;
2219 	reg_val |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2220 	    (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2221 
2222 	/* Enable transmits */
2223 	reg_val |= E1000_TCTL_EN;
2224 
2225 	E1000_WRITE_REG(hw, E1000_TCTL, reg_val);
2226 }
2227 
2228 /*
2229  * igb_setup_rss - Setup receive-side scaling feature
2230  */
2231 static void
2232 igb_setup_rss(igb_t *igb)
2233 {
2234 	struct e1000_hw *hw = &igb->hw;
2235 	uint32_t i, mrqc, rxcsum;
2236 	int shift = 0;
2237 	uint32_t random;
2238 	union e1000_reta {
2239 		uint32_t	dword;
2240 		uint8_t		bytes[4];
2241 	} reta;
2242 
2243 	/* Setup the Redirection Table */
2244 	if (hw->mac.type == e1000_82576) {
2245 		shift = 3;
2246 	} else if (hw->mac.type == e1000_82575) {
2247 		shift = 6;
2248 	}
2249 	for (i = 0; i < (32 * 4); i++) {
2250 		reta.bytes[i & 3] = (i % igb->num_rx_rings) << shift;
2251 		if ((i & 3) == 3) {
2252 			E1000_WRITE_REG(hw,
2253 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
2254 		}
2255 	}
2256 
2257 	/* Fill out hash function seeds */
2258 	for (i = 0; i < 10; i++) {
2259 		(void) random_get_pseudo_bytes((uint8_t *)&random,
2260 		    sizeof (uint32_t));
2261 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
2262 	}
2263 
2264 	/* Setup the Multiple Receive Queue Control register */
2265 	mrqc = E1000_MRQC_ENABLE_RSS_4Q;
2266 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
2267 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
2268 	    E1000_MRQC_RSS_FIELD_IPV6 |
2269 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
2270 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
2271 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
2272 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
2273 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
2274 
2275 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2276 
2277 	/*
2278 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
2279 	 *
2280 	 * The Packet Checksum is not ethernet CRC. It is another kind of
2281 	 * checksum offloading provided by the 82575 chipset besides the IP
2282 	 * header checksum offloading and the TCP/UDP checksum offloading.
2283 	 * The Packet Checksum is by default computed over the entire packet
2284 	 * from the first byte of the DA through the last byte of the CRC,
2285 	 * including the Ethernet and IP headers.
2286 	 *
2287 	 * It is a hardware limitation that Packet Checksum is mutually
2288 	 * exclusive with RSS.
2289 	 */
2290 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
2291 	rxcsum |= E1000_RXCSUM_PCSD;
2292 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
2293 }
2294 
2295 /*
2296  * igb_setup_mac_rss_classify - Setup MAC classification and rss
2297  */
2298 static void
2299 igb_setup_mac_rss_classify(igb_t *igb)
2300 {
2301 	struct e1000_hw *hw = &igb->hw;
2302 	uint32_t i, mrqc, vmdctl, rxcsum;
2303 	uint32_t ring_per_group;
2304 	int shift_group0, shift_group1;
2305 	uint32_t random;
2306 	union e1000_reta {
2307 		uint32_t	dword;
2308 		uint8_t		bytes[4];
2309 	} reta;
2310 
2311 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
2312 
2313 	/* Setup the Redirection Table, it is shared between two groups */
2314 	shift_group0 = 2;
2315 	shift_group1 = 6;
2316 	for (i = 0; i < (32 * 4); i++) {
2317 		reta.bytes[i & 3] = ((i % ring_per_group) << shift_group0) |
2318 		    ((ring_per_group + (i % ring_per_group)) << shift_group1);
2319 		if ((i & 3) == 3) {
2320 			E1000_WRITE_REG(hw,
2321 			    (E1000_RETA(0) + (i & ~3)), reta.dword);
2322 		}
2323 	}
2324 
2325 	/* Fill out hash function seeds */
2326 	for (i = 0; i < 10; i++) {
2327 		(void) random_get_pseudo_bytes((uint8_t *)&random,
2328 		    sizeof (uint32_t));
2329 		E1000_WRITE_REG(hw, E1000_RSSRK(i), random);
2330 	}
2331 
2332 	/*
2333 	 * Setup the Multiple Receive Queue Control register,
2334 	 * enable VMDq based on packet destination MAC address and RSS.
2335 	 */
2336 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_RSS_GROUP;
2337 	mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
2338 	    E1000_MRQC_RSS_FIELD_IPV4_TCP |
2339 	    E1000_MRQC_RSS_FIELD_IPV6 |
2340 	    E1000_MRQC_RSS_FIELD_IPV6_TCP |
2341 	    E1000_MRQC_RSS_FIELD_IPV4_UDP |
2342 	    E1000_MRQC_RSS_FIELD_IPV6_UDP |
2343 	    E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
2344 	    E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
2345 
2346 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2347 
2348 
2349 	/* Define the default group and default queues */
2350 	vmdctl = E1000_VMDQ_MAC_GROUP_DEFAULT_QUEUE;
2351 	E1000_WRITE_REG(hw, E1000_VT_CTL, vmdctl);
2352 
2353 	/*
2354 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
2355 	 *
2356 	 * The Packet Checksum is not ethernet CRC. It is another kind of
2357 	 * checksum offloading provided by the 82575 chipset besides the IP
2358 	 * header checksum offloading and the TCP/UDP checksum offloading.
2359 	 * The Packet Checksum is by default computed over the entire packet
2360 	 * from the first byte of the DA through the last byte of the CRC,
2361 	 * including the Ethernet and IP headers.
2362 	 *
2363 	 * It is a hardware limitation that Packet Checksum is mutually
2364 	 * exclusive with RSS.
2365 	 */
2366 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
2367 	rxcsum |= E1000_RXCSUM_PCSD;
2368 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
2369 }
2370 
2371 /*
2372  * igb_setup_mac_classify - Setup MAC classification feature
2373  */
2374 static void
2375 igb_setup_mac_classify(igb_t *igb)
2376 {
2377 	struct e1000_hw *hw = &igb->hw;
2378 	uint32_t mrqc, rxcsum;
2379 
2380 	/*
2381 	 * Setup the Multiple Receive Queue Control register,
2382 	 * enable VMDq based on packet destination MAC address.
2383 	 */
2384 	mrqc = E1000_MRQC_ENABLE_VMDQ_MAC_GROUP;
2385 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2386 
2387 	/*
2388 	 * Disable Packet Checksum to enable RSS for multiple receive queues.
2389 	 *
2390 	 * The Packet Checksum is not ethernet CRC. It is another kind of
2391 	 * checksum offloading provided by the 82575 chipset besides the IP
2392 	 * header checksum offloading and the TCP/UDP checksum offloading.
2393 	 * The Packet Checksum is by default computed over the entire packet
2394 	 * from the first byte of the DA through the last byte of the CRC,
2395 	 * including the Ethernet and IP headers.
2396 	 *
2397 	 * It is a hardware limitation that Packet Checksum is mutually
2398 	 * exclusive with RSS.
2399 	 */
2400 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
2401 	rxcsum |= E1000_RXCSUM_PCSD;
2402 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
2403 
2404 }
2405 
2406 /*
2407  * igb_init_unicst - Initialize the unicast addresses
2408  */
2409 static void
2410 igb_init_unicst(igb_t *igb)
2411 {
2412 	struct e1000_hw *hw = &igb->hw;
2413 	int slot;
2414 
2415 	/*
2416 	 * Here we should consider two situations:
2417 	 *
2418 	 * 1. Chipset is initialized the first time
2419 	 *    Initialize the multiple unicast addresses, and
2420 	 *    save the default MAC address.
2421 	 *
2422 	 * 2. Chipset is reset
2423 	 *    Recover the multiple unicast addresses from the
2424 	 *    software data structure to the RAR registers.
2425 	 */
2426 
2427 	/*
2428 	 * Clear the default MAC address in the RAR0 rgister,
2429 	 * which is loaded from EEPROM when system boot or chipreset,
2430 	 * this will cause the conficts with add_mac/rem_mac entry
2431 	 * points when VMDq is enabled. For this reason, the RAR0
2432 	 * must be cleared for both cases mentioned above.
2433 	 */
2434 	e1000_rar_clear(hw, 0);
2435 
2436 	if (!igb->unicst_init) {
2437 
2438 		/* Initialize the multiple unicast addresses */
2439 		igb->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
2440 		igb->unicst_avail = igb->unicst_total;
2441 
2442 		for (slot = 0; slot < igb->unicst_total; slot++)
2443 			igb->unicst_addr[slot].mac.set = 0;
2444 
2445 		igb->unicst_init = B_TRUE;
2446 	} else {
2447 		/* Re-configure the RAR registers */
2448 		for (slot = 0; slot < igb->unicst_total; slot++) {
2449 			e1000_rar_set_vmdq(hw, igb->unicst_addr[slot].mac.addr,
2450 			    slot, igb->vmdq_mode,
2451 			    igb->unicst_addr[slot].mac.group_index);
2452 		}
2453 	}
2454 }
2455 
2456 /*
2457  * igb_unicst_find - Find the slot for the specified unicast address
2458  */
2459 int
2460 igb_unicst_find(igb_t *igb, const uint8_t *mac_addr)
2461 {
2462 	int slot;
2463 
2464 	ASSERT(mutex_owned(&igb->gen_lock));
2465 
2466 	for (slot = 0; slot < igb->unicst_total; slot++) {
2467 		if (bcmp(igb->unicst_addr[slot].mac.addr,
2468 		    mac_addr, ETHERADDRL) == 0)
2469 			return (slot);
2470 	}
2471 
2472 	return (-1);
2473 }
2474 
2475 /*
2476  * igb_unicst_set - Set the unicast address to the specified slot
2477  */
2478 int
2479 igb_unicst_set(igb_t *igb, const uint8_t *mac_addr,
2480     int slot)
2481 {
2482 	struct e1000_hw *hw = &igb->hw;
2483 
2484 	ASSERT(mutex_owned(&igb->gen_lock));
2485 
2486 	/*
2487 	 * Save the unicast address in the software data structure
2488 	 */
2489 	bcopy(mac_addr, igb->unicst_addr[slot].mac.addr, ETHERADDRL);
2490 
2491 	/*
2492 	 * Set the unicast address to the RAR register
2493 	 */
2494 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
2495 
2496 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
2497 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
2498 		return (EIO);
2499 	}
2500 
2501 	return (0);
2502 }
2503 
2504 /*
2505  * igb_multicst_add - Add a multicst address
2506  */
2507 int
2508 igb_multicst_add(igb_t *igb, const uint8_t *multiaddr)
2509 {
2510 	struct ether_addr *new_table;
2511 	size_t new_len;
2512 	size_t old_len;
2513 
2514 	ASSERT(mutex_owned(&igb->gen_lock));
2515 
2516 	if ((multiaddr[0] & 01) == 0) {
2517 		igb_error(igb, "Illegal multicast address");
2518 		return (EINVAL);
2519 	}
2520 
2521 	if (igb->mcast_count >= igb->mcast_max_num) {
2522 		igb_error(igb, "Adapter requested more than %d mcast addresses",
2523 		    igb->mcast_max_num);
2524 		return (ENOENT);
2525 	}
2526 
2527 	if (igb->mcast_count == igb->mcast_alloc_count) {
2528 		old_len = igb->mcast_alloc_count *
2529 		    sizeof (struct ether_addr);
2530 		new_len = (igb->mcast_alloc_count + MCAST_ALLOC_COUNT) *
2531 		    sizeof (struct ether_addr);
2532 
2533 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
2534 		if (new_table == NULL) {
2535 			igb_error(igb,
2536 			    "Not enough memory to alloc mcast table");
2537 			return (ENOMEM);
2538 		}
2539 
2540 		if (igb->mcast_table != NULL) {
2541 			bcopy(igb->mcast_table, new_table, old_len);
2542 			kmem_free(igb->mcast_table, old_len);
2543 		}
2544 		igb->mcast_alloc_count += MCAST_ALLOC_COUNT;
2545 		igb->mcast_table = new_table;
2546 	}
2547 
2548 	bcopy(multiaddr,
2549 	    &igb->mcast_table[igb->mcast_count], ETHERADDRL);
2550 	igb->mcast_count++;
2551 
2552 	/*
2553 	 * Update the multicast table in the hardware
2554 	 */
2555 	igb_setup_multicst(igb);
2556 
2557 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
2558 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
2559 		return (EIO);
2560 	}
2561 
2562 	return (0);
2563 }
2564 
2565 /*
2566  * igb_multicst_remove - Remove a multicst address
2567  */
2568 int
2569 igb_multicst_remove(igb_t *igb, const uint8_t *multiaddr)
2570 {
2571 	struct ether_addr *new_table;
2572 	size_t new_len;
2573 	size_t old_len;
2574 	int i;
2575 
2576 	ASSERT(mutex_owned(&igb->gen_lock));
2577 
2578 	for (i = 0; i < igb->mcast_count; i++) {
2579 		if (bcmp(multiaddr, &igb->mcast_table[i],
2580 		    ETHERADDRL) == 0) {
2581 			for (i++; i < igb->mcast_count; i++) {
2582 				igb->mcast_table[i - 1] =
2583 				    igb->mcast_table[i];
2584 			}
2585 			igb->mcast_count--;
2586 			break;
2587 		}
2588 	}
2589 
2590 	if ((igb->mcast_alloc_count - igb->mcast_count) >
2591 	    MCAST_ALLOC_COUNT) {
2592 		old_len = igb->mcast_alloc_count *
2593 		    sizeof (struct ether_addr);
2594 		new_len = (igb->mcast_alloc_count - MCAST_ALLOC_COUNT) *
2595 		    sizeof (struct ether_addr);
2596 
2597 		new_table = kmem_alloc(new_len, KM_NOSLEEP);
2598 		if (new_table != NULL) {
2599 			bcopy(igb->mcast_table, new_table, new_len);
2600 			kmem_free(igb->mcast_table, old_len);
2601 			igb->mcast_alloc_count -= MCAST_ALLOC_COUNT;
2602 			igb->mcast_table = new_table;
2603 		}
2604 	}
2605 
2606 	/*
2607 	 * Update the multicast table in the hardware
2608 	 */
2609 	igb_setup_multicst(igb);
2610 
2611 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
2612 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
2613 		return (EIO);
2614 	}
2615 
2616 	return (0);
2617 }
2618 
2619 static void
2620 igb_release_multicast(igb_t *igb)
2621 {
2622 	if (igb->mcast_table != NULL) {
2623 		kmem_free(igb->mcast_table,
2624 		    igb->mcast_alloc_count * sizeof (struct ether_addr));
2625 		igb->mcast_table = NULL;
2626 	}
2627 }
2628 
2629 /*
2630  * igb_setup_multicast - setup multicast data structures
2631  *
2632  * This routine initializes all of the multicast related structures
2633  * and save them in the hardware registers.
2634  */
2635 static void
2636 igb_setup_multicst(igb_t *igb)
2637 {
2638 	uint8_t *mc_addr_list;
2639 	uint32_t mc_addr_count;
2640 	struct e1000_hw *hw = &igb->hw;
2641 
2642 	ASSERT(mutex_owned(&igb->gen_lock));
2643 	ASSERT(igb->mcast_count <= igb->mcast_max_num);
2644 
2645 	mc_addr_list = (uint8_t *)igb->mcast_table;
2646 	mc_addr_count = igb->mcast_count;
2647 
2648 	/*
2649 	 * Update the multicase addresses to the MTA registers
2650 	 */
2651 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count);
2652 }
2653 
2654 /*
2655  * igb_get_conf - Get driver configurations set in driver.conf
2656  *
2657  * This routine gets user-configured values out of the configuration
2658  * file igb.conf.
2659  *
2660  * For each configurable value, there is a minimum, a maximum, and a
2661  * default.
2662  * If user does not configure a value, use the default.
2663  * If user configures below the minimum, use the minumum.
2664  * If user configures above the maximum, use the maxumum.
2665  */
2666 static void
2667 igb_get_conf(igb_t *igb)
2668 {
2669 	struct e1000_hw *hw = &igb->hw;
2670 	uint32_t default_mtu;
2671 	uint32_t flow_control;
2672 	uint32_t ring_per_group;
2673 	int i;
2674 
2675 	/*
2676 	 * igb driver supports the following user configurations:
2677 	 *
2678 	 * Link configurations:
2679 	 *    adv_autoneg_cap
2680 	 *    adv_1000fdx_cap
2681 	 *    adv_100fdx_cap
2682 	 *    adv_100hdx_cap
2683 	 *    adv_10fdx_cap
2684 	 *    adv_10hdx_cap
2685 	 * Note: 1000hdx is not supported.
2686 	 *
2687 	 * Jumbo frame configuration:
2688 	 *    default_mtu
2689 	 *
2690 	 * Ethernet flow control configuration:
2691 	 *    flow_control
2692 	 *
2693 	 * Multiple rings configurations:
2694 	 *    tx_queue_number
2695 	 *    tx_ring_size
2696 	 *    rx_queue_number
2697 	 *    rx_ring_size
2698 	 *
2699 	 * Call igb_get_prop() to get the value for a specific
2700 	 * configuration parameter.
2701 	 */
2702 
2703 	/*
2704 	 * Link configurations
2705 	 */
2706 	igb->param_adv_autoneg_cap = igb_get_prop(igb,
2707 	    PROP_ADV_AUTONEG_CAP, 0, 1, 1);
2708 	igb->param_adv_1000fdx_cap = igb_get_prop(igb,
2709 	    PROP_ADV_1000FDX_CAP, 0, 1, 1);
2710 	igb->param_adv_100fdx_cap = igb_get_prop(igb,
2711 	    PROP_ADV_100FDX_CAP, 0, 1, 1);
2712 	igb->param_adv_100hdx_cap = igb_get_prop(igb,
2713 	    PROP_ADV_100HDX_CAP, 0, 1, 1);
2714 	igb->param_adv_10fdx_cap = igb_get_prop(igb,
2715 	    PROP_ADV_10FDX_CAP, 0, 1, 1);
2716 	igb->param_adv_10hdx_cap = igb_get_prop(igb,
2717 	    PROP_ADV_10HDX_CAP, 0, 1, 1);
2718 
2719 	/*
2720 	 * Jumbo frame configurations
2721 	 */
2722 	default_mtu = igb_get_prop(igb, PROP_DEFAULT_MTU,
2723 	    MIN_MTU, MAX_MTU, DEFAULT_MTU);
2724 
2725 	igb->max_frame_size = default_mtu +
2726 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
2727 
2728 	/*
2729 	 * Ethernet flow control configuration
2730 	 */
2731 	flow_control = igb_get_prop(igb, PROP_FLOW_CONTROL,
2732 	    e1000_fc_none, 4, e1000_fc_full);
2733 	if (flow_control == 4)
2734 		flow_control = e1000_fc_default;
2735 
2736 	hw->fc.requested_mode = flow_control;
2737 
2738 	/*
2739 	 * Multiple rings configurations
2740 	 */
2741 	igb->tx_ring_size = igb_get_prop(igb, PROP_TX_RING_SIZE,
2742 	    MIN_TX_RING_SIZE, MAX_TX_RING_SIZE, DEFAULT_TX_RING_SIZE);
2743 	igb->rx_ring_size = igb_get_prop(igb, PROP_RX_RING_SIZE,
2744 	    MIN_RX_RING_SIZE, MAX_RX_RING_SIZE, DEFAULT_RX_RING_SIZE);
2745 
2746 	igb->mr_enable = igb_get_prop(igb, PROP_MR_ENABLE, 0, 1, 0);
2747 	igb->num_rx_groups = igb_get_prop(igb, PROP_RX_GROUP_NUM,
2748 	    MIN_RX_GROUP_NUM, MAX_RX_GROUP_NUM, DEFAULT_RX_GROUP_NUM);
2749 	/*
2750 	 * Currently we do not support VMDq for 82576 and 82580.
2751 	 * If it is e1000_82576, set num_rx_groups to 1.
2752 	 */
2753 	if (hw->mac.type >= e1000_82576)
2754 		igb->num_rx_groups = 1;
2755 
2756 	if (igb->mr_enable) {
2757 		igb->num_tx_rings = igb->capab->def_tx_que_num;
2758 		igb->num_rx_rings = igb->capab->def_rx_que_num;
2759 	} else {
2760 		igb->num_tx_rings = 1;
2761 		igb->num_rx_rings = 1;
2762 
2763 		if (igb->num_rx_groups > 1) {
2764 			igb_error(igb,
2765 			    "Invalid rx groups number. Please enable multiple "
2766 			    "rings first");
2767 			igb->num_rx_groups = 1;
2768 		}
2769 	}
2770 
2771 	/*
2772 	 * Check the divisibility between rx rings and rx groups.
2773 	 */
2774 	for (i = igb->num_rx_groups; i > 0; i--) {
2775 		if ((igb->num_rx_rings % i) == 0)
2776 			break;
2777 	}
2778 	if (i != igb->num_rx_groups) {
2779 		igb_error(igb,
2780 		    "Invalid rx groups number. Downgrade the rx group "
2781 		    "number to %d.", i);
2782 		igb->num_rx_groups = i;
2783 	}
2784 
2785 	/*
2786 	 * Get the ring number per group.
2787 	 */
2788 	ring_per_group = igb->num_rx_rings / igb->num_rx_groups;
2789 
2790 	if (igb->num_rx_groups == 1) {
2791 		/*
2792 		 * One rx ring group, the rx ring number is num_rx_rings.
2793 		 */
2794 		igb->vmdq_mode = E1000_VMDQ_OFF;
2795 	} else if (ring_per_group == 1) {
2796 		/*
2797 		 * Multiple rx groups, each group has one rx ring.
2798 		 */
2799 		igb->vmdq_mode = E1000_VMDQ_MAC;
2800 	} else {
2801 		/*
2802 		 * Multiple groups and multiple rings.
2803 		 */
2804 		igb->vmdq_mode = E1000_VMDQ_MAC_RSS;
2805 	}
2806 
2807 	/*
2808 	 * Tunable used to force an interrupt type. The only use is
2809 	 * for testing of the lesser interrupt types.
2810 	 * 0 = don't force interrupt type
2811 	 * 1 = force interrupt type MSIX
2812 	 * 2 = force interrupt type MSI
2813 	 * 3 = force interrupt type Legacy
2814 	 */
2815 	igb->intr_force = igb_get_prop(igb, PROP_INTR_FORCE,
2816 	    IGB_INTR_NONE, IGB_INTR_LEGACY, IGB_INTR_NONE);
2817 
2818 	igb->tx_hcksum_enable = igb_get_prop(igb, PROP_TX_HCKSUM_ENABLE,
2819 	    0, 1, 1);
2820 	igb->rx_hcksum_enable = igb_get_prop(igb, PROP_RX_HCKSUM_ENABLE,
2821 	    0, 1, 1);
2822 	igb->lso_enable = igb_get_prop(igb, PROP_LSO_ENABLE,
2823 	    0, 1, 1);
2824 	igb->tx_head_wb_enable = igb_get_prop(igb, PROP_TX_HEAD_WB_ENABLE,
2825 	    0, 1, 1);
2826 
2827 	/*
2828 	 * igb LSO needs the tx h/w checksum support.
2829 	 * Here LSO will be disabled if tx h/w checksum has been disabled.
2830 	 */
2831 	if (igb->tx_hcksum_enable == B_FALSE)
2832 		igb->lso_enable = B_FALSE;
2833 
2834 	igb->tx_copy_thresh = igb_get_prop(igb, PROP_TX_COPY_THRESHOLD,
2835 	    MIN_TX_COPY_THRESHOLD, MAX_TX_COPY_THRESHOLD,
2836 	    DEFAULT_TX_COPY_THRESHOLD);
2837 	igb->tx_recycle_thresh = igb_get_prop(igb, PROP_TX_RECYCLE_THRESHOLD,
2838 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
2839 	    DEFAULT_TX_RECYCLE_THRESHOLD);
2840 	igb->tx_overload_thresh = igb_get_prop(igb, PROP_TX_OVERLOAD_THRESHOLD,
2841 	    MIN_TX_OVERLOAD_THRESHOLD, MAX_TX_OVERLOAD_THRESHOLD,
2842 	    DEFAULT_TX_OVERLOAD_THRESHOLD);
2843 	igb->tx_resched_thresh = igb_get_prop(igb, PROP_TX_RESCHED_THRESHOLD,
2844 	    MIN_TX_RESCHED_THRESHOLD, MAX_TX_RESCHED_THRESHOLD,
2845 	    DEFAULT_TX_RESCHED_THRESHOLD);
2846 
2847 	igb->rx_copy_thresh = igb_get_prop(igb, PROP_RX_COPY_THRESHOLD,
2848 	    MIN_RX_COPY_THRESHOLD, MAX_RX_COPY_THRESHOLD,
2849 	    DEFAULT_RX_COPY_THRESHOLD);
2850 	igb->rx_limit_per_intr = igb_get_prop(igb, PROP_RX_LIMIT_PER_INTR,
2851 	    MIN_RX_LIMIT_PER_INTR, MAX_RX_LIMIT_PER_INTR,
2852 	    DEFAULT_RX_LIMIT_PER_INTR);
2853 
2854 	igb->intr_throttling[0] = igb_get_prop(igb, PROP_INTR_THROTTLING,
2855 	    igb->capab->min_intr_throttle,
2856 	    igb->capab->max_intr_throttle,
2857 	    igb->capab->def_intr_throttle);
2858 
2859 	/*
2860 	 * Max number of multicast addresses
2861 	 */
2862 	igb->mcast_max_num =
2863 	    igb_get_prop(igb, PROP_MCAST_MAX_NUM,
2864 	    MIN_MCAST_NUM, MAX_MCAST_NUM, DEFAULT_MCAST_NUM);
2865 }
2866 
2867 /*
2868  * igb_get_prop - Get a property value out of the configuration file igb.conf
2869  *
2870  * Caller provides the name of the property, a default value, a minimum
2871  * value, and a maximum value.
2872  *
2873  * Return configured value of the property, with default, minimum and
2874  * maximum properly applied.
2875  */
2876 static int
2877 igb_get_prop(igb_t *igb,
2878     char *propname,	/* name of the property */
2879     int minval,		/* minimum acceptable value */
2880     int maxval,		/* maximim acceptable value */
2881     int defval)		/* default value */
2882 {
2883 	int value;
2884 
2885 	/*
2886 	 * Call ddi_prop_get_int() to read the conf settings
2887 	 */
2888 	value = ddi_prop_get_int(DDI_DEV_T_ANY, igb->dip,
2889 	    DDI_PROP_DONTPASS, propname, defval);
2890 
2891 	if (value > maxval)
2892 		value = maxval;
2893 
2894 	if (value < minval)
2895 		value = minval;
2896 
2897 	return (value);
2898 }
2899 
2900 /*
2901  * igb_setup_link - Using the link properties to setup the link
2902  */
2903 int
2904 igb_setup_link(igb_t *igb, boolean_t setup_hw)
2905 {
2906 	struct e1000_mac_info *mac;
2907 	struct e1000_phy_info *phy;
2908 	boolean_t invalid;
2909 
2910 	mac = &igb->hw.mac;
2911 	phy = &igb->hw.phy;
2912 	invalid = B_FALSE;
2913 
2914 	if (igb->param_adv_autoneg_cap == 1) {
2915 		mac->autoneg = B_TRUE;
2916 		phy->autoneg_advertised = 0;
2917 
2918 		/*
2919 		 * 1000hdx is not supported for autonegotiation
2920 		 */
2921 		if (igb->param_adv_1000fdx_cap == 1)
2922 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
2923 
2924 		if (igb->param_adv_100fdx_cap == 1)
2925 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
2926 
2927 		if (igb->param_adv_100hdx_cap == 1)
2928 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
2929 
2930 		if (igb->param_adv_10fdx_cap == 1)
2931 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
2932 
2933 		if (igb->param_adv_10hdx_cap == 1)
2934 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
2935 
2936 		if (phy->autoneg_advertised == 0)
2937 			invalid = B_TRUE;
2938 	} else {
2939 		mac->autoneg = B_FALSE;
2940 
2941 		/*
2942 		 * 1000fdx and 1000hdx are not supported for forced link
2943 		 */
2944 		if (igb->param_adv_100fdx_cap == 1)
2945 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
2946 		else if (igb->param_adv_100hdx_cap == 1)
2947 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
2948 		else if (igb->param_adv_10fdx_cap == 1)
2949 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
2950 		else if (igb->param_adv_10hdx_cap == 1)
2951 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
2952 		else
2953 			invalid = B_TRUE;
2954 	}
2955 
2956 	if (invalid) {
2957 		igb_notice(igb, "Invalid link settings. Setup link to "
2958 		    "autonegotiation with full link capabilities.");
2959 		mac->autoneg = B_TRUE;
2960 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
2961 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
2962 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
2963 	}
2964 
2965 	if (setup_hw) {
2966 		if (e1000_setup_link(&igb->hw) != E1000_SUCCESS)
2967 			return (IGB_FAILURE);
2968 	}
2969 
2970 	return (IGB_SUCCESS);
2971 }
2972 
2973 
2974 /*
2975  * igb_is_link_up - Check if the link is up
2976  */
2977 static boolean_t
2978 igb_is_link_up(igb_t *igb)
2979 {
2980 	struct e1000_hw *hw = &igb->hw;
2981 	boolean_t link_up = B_FALSE;
2982 
2983 	ASSERT(mutex_owned(&igb->gen_lock));
2984 
2985 	/*
2986 	 * get_link_status is set in the interrupt handler on link-status-change
2987 	 * or rx sequence error interrupt.  get_link_status will stay
2988 	 * false until the e1000_check_for_link establishes link only
2989 	 * for copper adapters.
2990 	 */
2991 	switch (hw->phy.media_type) {
2992 	case e1000_media_type_copper:
2993 		if (hw->mac.get_link_status) {
2994 			(void) e1000_check_for_link(hw);
2995 			link_up = !hw->mac.get_link_status;
2996 		} else {
2997 			link_up = B_TRUE;
2998 		}
2999 		break;
3000 	case e1000_media_type_fiber:
3001 		(void) e1000_check_for_link(hw);
3002 		link_up = (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU);
3003 		break;
3004 	case e1000_media_type_internal_serdes:
3005 		(void) e1000_check_for_link(hw);
3006 		link_up = hw->mac.serdes_has_link;
3007 		break;
3008 	}
3009 
3010 	return (link_up);
3011 }
3012 
3013 /*
3014  * igb_link_check - Link status processing
3015  */
3016 static boolean_t
3017 igb_link_check(igb_t *igb)
3018 {
3019 	struct e1000_hw *hw = &igb->hw;
3020 	uint16_t speed = 0, duplex = 0;
3021 	boolean_t link_changed = B_FALSE;
3022 
3023 	ASSERT(mutex_owned(&igb->gen_lock));
3024 
3025 	if (igb_is_link_up(igb)) {
3026 		/*
3027 		 * The Link is up, check whether it was marked as down earlier
3028 		 */
3029 		if (igb->link_state != LINK_STATE_UP) {
3030 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
3031 			igb->link_speed = speed;
3032 			igb->link_duplex = duplex;
3033 			igb->link_state = LINK_STATE_UP;
3034 			link_changed = B_TRUE;
3035 			if (!igb->link_complete)
3036 				igb_stop_link_timer(igb);
3037 		}
3038 	} else if (igb->link_complete) {
3039 		if (igb->link_state != LINK_STATE_DOWN) {
3040 			igb->link_speed = 0;
3041 			igb->link_duplex = 0;
3042 			igb->link_state = LINK_STATE_DOWN;
3043 			link_changed = B_TRUE;
3044 		}
3045 	}
3046 
3047 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
3048 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
3049 		return (B_FALSE);
3050 	}
3051 
3052 	return (link_changed);
3053 }
3054 
3055 /*
3056  * igb_local_timer - driver watchdog function
3057  *
3058  * This function will handle the hardware stall check, link status
3059  * check and other routines.
3060  */
3061 static void
3062 igb_local_timer(void *arg)
3063 {
3064 	igb_t *igb = (igb_t *)arg;
3065 	boolean_t link_changed = B_FALSE;
3066 
3067 	if (igb->igb_state & IGB_ERROR) {
3068 		igb->reset_count++;
3069 		if (igb_reset(igb) == IGB_SUCCESS)
3070 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_RESTORED);
3071 
3072 		igb_restart_watchdog_timer(igb);
3073 		return;
3074 	}
3075 
3076 	if (igb_stall_check(igb) || (igb->igb_state & IGB_STALL)) {
3077 		igb_fm_ereport(igb, DDI_FM_DEVICE_STALL);
3078 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_LOST);
3079 		igb->reset_count++;
3080 		if (igb_reset(igb) == IGB_SUCCESS)
3081 			ddi_fm_service_impact(igb->dip, DDI_SERVICE_RESTORED);
3082 
3083 		igb_restart_watchdog_timer(igb);
3084 		return;
3085 	}
3086 
3087 	mutex_enter(&igb->gen_lock);
3088 	if (!(igb->igb_state & IGB_SUSPENDED) && (igb->igb_state & IGB_STARTED))
3089 		link_changed = igb_link_check(igb);
3090 	mutex_exit(&igb->gen_lock);
3091 
3092 	if (link_changed)
3093 		mac_link_update(igb->mac_hdl, igb->link_state);
3094 
3095 	igb_restart_watchdog_timer(igb);
3096 }
3097 
3098 /*
3099  * igb_link_timer - link setup timer function
3100  *
3101  * It is called when the timer for link setup is expired, which indicates
3102  * the completion of the link setup. The link state will not be updated
3103  * until the link setup is completed. And the link state will not be sent
3104  * to the upper layer through mac_link_update() in this function. It will
3105  * be updated in the local timer routine or the interrupts service routine
3106  * after the interface is started (plumbed).
3107  */
3108 static void
3109 igb_link_timer(void *arg)
3110 {
3111 	igb_t *igb = (igb_t *)arg;
3112 
3113 	mutex_enter(&igb->link_lock);
3114 	igb->link_complete = B_TRUE;
3115 	igb->link_tid = 0;
3116 	mutex_exit(&igb->link_lock);
3117 }
3118 /*
3119  * igb_stall_check - check for transmit stall
3120  *
3121  * This function checks if the adapter is stalled (in transmit).
3122  *
3123  * It is called each time the watchdog timeout is invoked.
3124  * If the transmit descriptor reclaim continuously fails,
3125  * the watchdog value will increment by 1. If the watchdog
3126  * value exceeds the threshold, the igb is assumed to
3127  * have stalled and need to be reset.
3128  */
3129 static boolean_t
3130 igb_stall_check(igb_t *igb)
3131 {
3132 	igb_tx_ring_t *tx_ring;
3133 	struct e1000_hw *hw = &igb->hw;
3134 	boolean_t result;
3135 	int i;
3136 
3137 	if (igb->link_state != LINK_STATE_UP)
3138 		return (B_FALSE);
3139 
3140 	/*
3141 	 * If any tx ring is stalled, we'll reset the chipset
3142 	 */
3143 	result = B_FALSE;
3144 	for (i = 0; i < igb->num_tx_rings; i++) {
3145 		tx_ring = &igb->tx_rings[i];
3146 
3147 		if (tx_ring->recycle_fail > 0)
3148 			tx_ring->stall_watchdog++;
3149 		else
3150 			tx_ring->stall_watchdog = 0;
3151 
3152 		if (tx_ring->stall_watchdog >= STALL_WATCHDOG_TIMEOUT) {
3153 			result = B_TRUE;
3154 			if (hw->mac.type == e1000_82580) {
3155 				hw->dev_spec._82575.global_device_reset
3156 				    = B_TRUE;
3157 			}
3158 			break;
3159 		}
3160 	}
3161 
3162 	if (result) {
3163 		tx_ring->stall_watchdog = 0;
3164 		tx_ring->recycle_fail = 0;
3165 	}
3166 
3167 	return (result);
3168 }
3169 
3170 
3171 /*
3172  * is_valid_mac_addr - Check if the mac address is valid
3173  */
3174 static boolean_t
3175 is_valid_mac_addr(uint8_t *mac_addr)
3176 {
3177 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
3178 	const uint8_t addr_test2[6] =
3179 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3180 
3181 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
3182 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
3183 		return (B_FALSE);
3184 
3185 	return (B_TRUE);
3186 }
3187 
3188 static boolean_t
3189 igb_find_mac_address(igb_t *igb)
3190 {
3191 	struct e1000_hw *hw = &igb->hw;
3192 #ifdef __sparc
3193 	uchar_t *bytes;
3194 	struct ether_addr sysaddr;
3195 	uint_t nelts;
3196 	int err;
3197 	boolean_t found = B_FALSE;
3198 
3199 	/*
3200 	 * The "vendor's factory-set address" may already have
3201 	 * been extracted from the chip, but if the property
3202 	 * "local-mac-address" is set we use that instead.
3203 	 *
3204 	 * We check whether it looks like an array of 6
3205 	 * bytes (which it should, if OBP set it).  If we can't
3206 	 * make sense of it this way, we'll ignore it.
3207 	 */
3208 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
3209 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
3210 	if (err == DDI_PROP_SUCCESS) {
3211 		if (nelts == ETHERADDRL) {
3212 			while (nelts--)
3213 				hw->mac.addr[nelts] = bytes[nelts];
3214 			found = B_TRUE;
3215 		}
3216 		ddi_prop_free(bytes);
3217 	}
3218 
3219 	/*
3220 	 * Look up the OBP property "local-mac-address?". If the user has set
3221 	 * 'local-mac-address? = false', use "the system address" instead.
3222 	 */
3223 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip, 0,
3224 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
3225 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
3226 			if (localetheraddr(NULL, &sysaddr) != 0) {
3227 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
3228 				found = B_TRUE;
3229 			}
3230 		}
3231 		ddi_prop_free(bytes);
3232 	}
3233 
3234 	/*
3235 	 * Finally(!), if there's a valid "mac-address" property (created
3236 	 * if we netbooted from this interface), we must use this instead
3237 	 * of any of the above to ensure that the NFS/install server doesn't
3238 	 * get confused by the address changing as Solaris takes over!
3239 	 */
3240 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, igb->dip,
3241 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
3242 	if (err == DDI_PROP_SUCCESS) {
3243 		if (nelts == ETHERADDRL) {
3244 			while (nelts--)
3245 				hw->mac.addr[nelts] = bytes[nelts];
3246 			found = B_TRUE;
3247 		}
3248 		ddi_prop_free(bytes);
3249 	}
3250 
3251 	if (found) {
3252 		bcopy(hw->mac.addr, hw->mac.perm_addr, ETHERADDRL);
3253 		return (B_TRUE);
3254 	}
3255 #endif
3256 
3257 	/*
3258 	 * Read the device MAC address from the EEPROM
3259 	 */
3260 	if (e1000_read_mac_addr(hw) != E1000_SUCCESS)
3261 		return (B_FALSE);
3262 
3263 	return (B_TRUE);
3264 }
3265 
3266 #pragma inline(igb_arm_watchdog_timer)
3267 
3268 static void
3269 igb_arm_watchdog_timer(igb_t *igb)
3270 {
3271 	/*
3272 	 * Fire a watchdog timer
3273 	 */
3274 	igb->watchdog_tid =
3275 	    timeout(igb_local_timer,
3276 	    (void *)igb, 1 * drv_usectohz(1000000));
3277 
3278 }
3279 
3280 /*
3281  * igb_enable_watchdog_timer - Enable and start the driver watchdog timer
3282  */
3283 void
3284 igb_enable_watchdog_timer(igb_t *igb)
3285 {
3286 	mutex_enter(&igb->watchdog_lock);
3287 
3288 	if (!igb->watchdog_enable) {
3289 		igb->watchdog_enable = B_TRUE;
3290 		igb->watchdog_start = B_TRUE;
3291 		igb_arm_watchdog_timer(igb);
3292 	}
3293 
3294 	mutex_exit(&igb->watchdog_lock);
3295 
3296 }
3297 
3298 /*
3299  * igb_disable_watchdog_timer - Disable and stop the driver watchdog timer
3300  */
3301 void
3302 igb_disable_watchdog_timer(igb_t *igb)
3303 {
3304 	timeout_id_t tid;
3305 
3306 	mutex_enter(&igb->watchdog_lock);
3307 
3308 	igb->watchdog_enable = B_FALSE;
3309 	igb->watchdog_start = B_FALSE;
3310 	tid = igb->watchdog_tid;
3311 	igb->watchdog_tid = 0;
3312 
3313 	mutex_exit(&igb->watchdog_lock);
3314 
3315 	if (tid != 0)
3316 		(void) untimeout(tid);
3317 
3318 }
3319 
3320 /*
3321  * igb_start_watchdog_timer - Start the driver watchdog timer
3322  */
3323 static void
3324 igb_start_watchdog_timer(igb_t *igb)
3325 {
3326 	mutex_enter(&igb->watchdog_lock);
3327 
3328 	if (igb->watchdog_enable) {
3329 		if (!igb->watchdog_start) {
3330 			igb->watchdog_start = B_TRUE;
3331 			igb_arm_watchdog_timer(igb);
3332 		}
3333 	}
3334 
3335 	mutex_exit(&igb->watchdog_lock);
3336 }
3337 
3338 /*
3339  * igb_restart_watchdog_timer - Restart the driver watchdog timer
3340  */
3341 static void
3342 igb_restart_watchdog_timer(igb_t *igb)
3343 {
3344 	mutex_enter(&igb->watchdog_lock);
3345 
3346 	if (igb->watchdog_start)
3347 		igb_arm_watchdog_timer(igb);
3348 
3349 	mutex_exit(&igb->watchdog_lock);
3350 }
3351 
3352 /*
3353  * igb_stop_watchdog_timer - Stop the driver watchdog timer
3354  */
3355 static void
3356 igb_stop_watchdog_timer(igb_t *igb)
3357 {
3358 	timeout_id_t tid;
3359 
3360 	mutex_enter(&igb->watchdog_lock);
3361 
3362 	igb->watchdog_start = B_FALSE;
3363 	tid = igb->watchdog_tid;
3364 	igb->watchdog_tid = 0;
3365 
3366 	mutex_exit(&igb->watchdog_lock);
3367 
3368 	if (tid != 0)
3369 		(void) untimeout(tid);
3370 }
3371 
3372 /*
3373  * igb_start_link_timer - Start the link setup timer
3374  */
3375 static void
3376 igb_start_link_timer(struct igb *igb)
3377 {
3378 	struct e1000_hw *hw = &igb->hw;
3379 	clock_t link_timeout;
3380 
3381 	if (hw->mac.autoneg)
3382 		link_timeout = PHY_AUTO_NEG_LIMIT *
3383 		    drv_usectohz(100000);
3384 	else
3385 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
3386 
3387 	mutex_enter(&igb->link_lock);
3388 	if (hw->phy.autoneg_wait_to_complete) {
3389 		igb->link_complete = B_TRUE;
3390 	} else {
3391 		igb->link_complete = B_FALSE;
3392 		igb->link_tid = timeout(igb_link_timer, (void *)igb,
3393 		    link_timeout);
3394 	}
3395 	mutex_exit(&igb->link_lock);
3396 }
3397 
3398 /*
3399  * igb_stop_link_timer - Stop the link setup timer
3400  */
3401 static void
3402 igb_stop_link_timer(struct igb *igb)
3403 {
3404 	timeout_id_t tid;
3405 
3406 	mutex_enter(&igb->link_lock);
3407 	igb->link_complete = B_TRUE;
3408 	tid = igb->link_tid;
3409 	igb->link_tid = 0;
3410 	mutex_exit(&igb->link_lock);
3411 
3412 	if (tid != 0)
3413 		(void) untimeout(tid);
3414 }
3415 
3416 /*
3417  * igb_disable_adapter_interrupts - Clear/disable all hardware interrupts
3418  */
3419 static void
3420 igb_disable_adapter_interrupts(igb_t *igb)
3421 {
3422 	struct e1000_hw *hw = &igb->hw;
3423 
3424 	/*
3425 	 * Set the IMC register to mask all the interrupts,
3426 	 * including the tx interrupts.
3427 	 */
3428 	E1000_WRITE_REG(hw, E1000_IMC, ~0);
3429 	E1000_WRITE_REG(hw, E1000_IAM, 0);
3430 
3431 	/*
3432 	 * Additional disabling for MSI-X
3433 	 */
3434 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3435 		E1000_WRITE_REG(hw, E1000_EIMC, ~0);
3436 		E1000_WRITE_REG(hw, E1000_EIAC, 0);
3437 		E1000_WRITE_REG(hw, E1000_EIAM, 0);
3438 	}
3439 
3440 	E1000_WRITE_FLUSH(hw);
3441 }
3442 
3443 /*
3444  * igb_enable_adapter_interrupts_82580 - Enable NIC interrupts for 82580
3445  */
3446 static void
3447 igb_enable_adapter_interrupts_82580(igb_t *igb)
3448 {
3449 	struct e1000_hw *hw = &igb->hw;
3450 
3451 	/* Clear any pending interrupts */
3452 	(void) E1000_READ_REG(hw, E1000_ICR);
3453 	igb->ims_mask |= E1000_IMS_DRSTA;
3454 
3455 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3456 
3457 		/* Interrupt enabling for MSI-X */
3458 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
3459 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
3460 		igb->ims_mask = (E1000_IMS_LSC | E1000_IMS_DRSTA);
3461 		E1000_WRITE_REG(hw, E1000_IMS, igb->ims_mask);
3462 	} else { /* Interrupt enabling for MSI and legacy */
3463 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
3464 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
3465 		igb->ims_mask |= E1000_IMS_DRSTA;
3466 		E1000_WRITE_REG(hw, E1000_IMS, igb->ims_mask);
3467 	}
3468 
3469 	/* Disable auto-mask for ICR interrupt bits */
3470 	E1000_WRITE_REG(hw, E1000_IAM, 0);
3471 
3472 	E1000_WRITE_FLUSH(hw);
3473 }
3474 
3475 /*
3476  * igb_enable_adapter_interrupts_82576 - Enable NIC interrupts for 82576
3477  */
3478 static void
3479 igb_enable_adapter_interrupts_82576(igb_t *igb)
3480 {
3481 	struct e1000_hw *hw = &igb->hw;
3482 
3483 	/* Clear any pending interrupts */
3484 	(void) E1000_READ_REG(hw, E1000_ICR);
3485 
3486 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3487 
3488 		/* Interrupt enabling for MSI-X */
3489 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
3490 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
3491 		igb->ims_mask = E1000_IMS_LSC;
3492 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
3493 	} else {
3494 		/* Interrupt enabling for MSI and legacy */
3495 		E1000_WRITE_REG(hw, E1000_IVAR0, E1000_IVAR_VALID);
3496 		igb->ims_mask = IMS_ENABLE_MASK | E1000_IMS_TXQE;
3497 		E1000_WRITE_REG(hw, E1000_IMS,
3498 		    (IMS_ENABLE_MASK | E1000_IMS_TXQE));
3499 	}
3500 
3501 	/* Disable auto-mask for ICR interrupt bits */
3502 	E1000_WRITE_REG(hw, E1000_IAM, 0);
3503 
3504 	E1000_WRITE_FLUSH(hw);
3505 }
3506 
3507 /*
3508  * igb_enable_adapter_interrupts_82575 - Enable NIC interrupts for 82575
3509  */
3510 static void
3511 igb_enable_adapter_interrupts_82575(igb_t *igb)
3512 {
3513 	struct e1000_hw *hw = &igb->hw;
3514 	uint32_t reg;
3515 
3516 	/* Clear any pending interrupts */
3517 	(void) E1000_READ_REG(hw, E1000_ICR);
3518 
3519 	if (igb->intr_type == DDI_INTR_TYPE_MSIX) {
3520 		/* Interrupt enabling for MSI-X */
3521 		E1000_WRITE_REG(hw, E1000_EIMS, igb->eims_mask);
3522 		E1000_WRITE_REG(hw, E1000_EIAC, igb->eims_mask);
3523 		igb->ims_mask = E1000_IMS_LSC;
3524 		E1000_WRITE_REG(hw, E1000_IMS, E1000_IMS_LSC);
3525 
3526 		/* Enable MSI-X PBA support */
3527 		reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
3528 		reg |= E1000_CTRL_EXT_PBA_CLR;
3529 
3530 		/* Non-selective interrupt clear-on-read */
3531 		reg |= E1000_CTRL_EXT_IRCA;	/* Called NSICR in the EAS */
3532 
3533 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
3534 	} else {
3535 		/* Interrupt enabling for MSI and legacy */
3536 		igb->ims_mask = IMS_ENABLE_MASK;
3537 		E1000_WRITE_REG(hw, E1000_IMS, IMS_ENABLE_MASK);
3538 	}
3539 
3540 	E1000_WRITE_FLUSH(hw);
3541 }
3542 
3543 /*
3544  * Loopback Support
3545  */
3546 static lb_property_t lb_normal =
3547 	{ normal,	"normal",	IGB_LB_NONE		};
3548 static lb_property_t lb_external =
3549 	{ external,	"External",	IGB_LB_EXTERNAL		};
3550 static lb_property_t lb_phy =
3551 	{ internal,	"PHY",		IGB_LB_INTERNAL_PHY	};
3552 static lb_property_t lb_serdes =
3553 	{ internal,	"SerDes",	IGB_LB_INTERNAL_SERDES	};
3554 
3555 enum ioc_reply
3556 igb_loopback_ioctl(igb_t *igb, struct iocblk *iocp, mblk_t *mp)
3557 {
3558 	lb_info_sz_t *lbsp;
3559 	lb_property_t *lbpp;
3560 	struct e1000_hw *hw;
3561 	uint32_t *lbmp;
3562 	uint32_t size;
3563 	uint32_t value;
3564 
3565 	hw = &igb->hw;
3566 
3567 	if (mp->b_cont == NULL)
3568 		return (IOC_INVAL);
3569 
3570 	switch (iocp->ioc_cmd) {
3571 	default:
3572 		return (IOC_INVAL);
3573 
3574 	case LB_GET_INFO_SIZE:
3575 		size = sizeof (lb_info_sz_t);
3576 		if (iocp->ioc_count != size)
3577 			return (IOC_INVAL);
3578 
3579 		value = sizeof (lb_normal);
3580 		if (hw->phy.media_type == e1000_media_type_copper)
3581 			value += sizeof (lb_phy);
3582 		else
3583 			value += sizeof (lb_serdes);
3584 		value += sizeof (lb_external);
3585 
3586 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
3587 		*lbsp = value;
3588 		break;
3589 
3590 	case LB_GET_INFO:
3591 		value = sizeof (lb_normal);
3592 		if (hw->phy.media_type == e1000_media_type_copper)
3593 			value += sizeof (lb_phy);
3594 		else
3595 			value += sizeof (lb_serdes);
3596 		value += sizeof (lb_external);
3597 
3598 		size = value;
3599 		if (iocp->ioc_count != size)
3600 			return (IOC_INVAL);
3601 
3602 		value = 0;
3603 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
3604 
3605 		lbpp[value++] = lb_normal;
3606 		if (hw->phy.media_type == e1000_media_type_copper)
3607 			lbpp[value++] = lb_phy;
3608 		else
3609 			lbpp[value++] = lb_serdes;
3610 		lbpp[value++] = lb_external;
3611 		break;
3612 
3613 	case LB_GET_MODE:
3614 		size = sizeof (uint32_t);
3615 		if (iocp->ioc_count != size)
3616 			return (IOC_INVAL);
3617 
3618 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
3619 		*lbmp = igb->loopback_mode;
3620 		break;
3621 
3622 	case LB_SET_MODE:
3623 		size = 0;
3624 		if (iocp->ioc_count != sizeof (uint32_t))
3625 			return (IOC_INVAL);
3626 
3627 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
3628 		if (!igb_set_loopback_mode(igb, *lbmp))
3629 			return (IOC_INVAL);
3630 		break;
3631 	}
3632 
3633 	iocp->ioc_count = size;
3634 	iocp->ioc_error = 0;
3635 
3636 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
3637 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
3638 		return (IOC_INVAL);
3639 	}
3640 
3641 	return (IOC_REPLY);
3642 }
3643 
3644 /*
3645  * igb_set_loopback_mode - Setup loopback based on the loopback mode
3646  */
3647 static boolean_t
3648 igb_set_loopback_mode(igb_t *igb, uint32_t mode)
3649 {
3650 	struct e1000_hw *hw;
3651 	int i;
3652 
3653 	if (mode == igb->loopback_mode)
3654 		return (B_TRUE);
3655 
3656 	hw = &igb->hw;
3657 
3658 	igb->loopback_mode = mode;
3659 
3660 	if (mode == IGB_LB_NONE) {
3661 		/* Reset the chip */
3662 		hw->phy.autoneg_wait_to_complete = B_TRUE;
3663 		(void) igb_reset(igb);
3664 		hw->phy.autoneg_wait_to_complete = B_FALSE;
3665 		return (B_TRUE);
3666 	}
3667 
3668 	mutex_enter(&igb->gen_lock);
3669 
3670 	switch (mode) {
3671 	default:
3672 		mutex_exit(&igb->gen_lock);
3673 		return (B_FALSE);
3674 
3675 	case IGB_LB_EXTERNAL:
3676 		igb_set_external_loopback(igb);
3677 		break;
3678 
3679 	case IGB_LB_INTERNAL_PHY:
3680 		igb_set_internal_phy_loopback(igb);
3681 		break;
3682 
3683 	case IGB_LB_INTERNAL_SERDES:
3684 		igb_set_internal_serdes_loopback(igb);
3685 		break;
3686 	}
3687 
3688 	mutex_exit(&igb->gen_lock);
3689 
3690 	/*
3691 	 * When external loopback is set, wait up to 1000ms to get the link up.
3692 	 * According to test, 1000ms can work and it's an experimental value.
3693 	 */
3694 	if (mode == IGB_LB_EXTERNAL) {
3695 		for (i = 0; i <= 10; i++) {
3696 			mutex_enter(&igb->gen_lock);
3697 			(void) igb_link_check(igb);
3698 			mutex_exit(&igb->gen_lock);
3699 
3700 			if (igb->link_state == LINK_STATE_UP)
3701 				break;
3702 
3703 			msec_delay(100);
3704 		}
3705 
3706 		if (igb->link_state != LINK_STATE_UP) {
3707 			/*
3708 			 * Does not support external loopback.
3709 			 * Reset driver to loopback none.
3710 			 */
3711 			igb->loopback_mode = IGB_LB_NONE;
3712 
3713 			/* Reset the chip */
3714 			hw->phy.autoneg_wait_to_complete = B_TRUE;
3715 			(void) igb_reset(igb);
3716 			hw->phy.autoneg_wait_to_complete = B_FALSE;
3717 
3718 			IGB_DEBUGLOG_0(igb, "Set external loopback failed, "
3719 			    "reset to loopback none.");
3720 
3721 			return (B_FALSE);
3722 		}
3723 	}
3724 
3725 	return (B_TRUE);
3726 }
3727 
3728 /*
3729  * igb_set_external_loopback - Set the external loopback mode
3730  */
3731 static void
3732 igb_set_external_loopback(igb_t *igb)
3733 {
3734 	struct e1000_hw *hw;
3735 
3736 	hw = &igb->hw;
3737 
3738 	/* Set phy to known state */
3739 	(void) e1000_phy_hw_reset(hw);
3740 
3741 	(void) e1000_write_phy_reg(hw, 0x0, 0x0140);
3742 	(void) e1000_write_phy_reg(hw, 0x9, 0x1b00);
3743 	(void) e1000_write_phy_reg(hw, 0x12, 0x1610);
3744 	(void) e1000_write_phy_reg(hw, 0x1f37, 0x3f1c);
3745 }
3746 
3747 /*
3748  * igb_set_internal_phy_loopback - Set the internal PHY loopback mode
3749  */
3750 static void
3751 igb_set_internal_phy_loopback(igb_t *igb)
3752 {
3753 	struct e1000_hw *hw;
3754 	uint32_t ctrl_ext;
3755 	uint16_t phy_ctrl;
3756 	uint16_t phy_pconf;
3757 
3758 	hw = &igb->hw;
3759 
3760 	/* Set link mode to PHY (00b) in the Extended Control register */
3761 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3762 	ctrl_ext &= ~E1000_CTRL_EXT_LINK_MODE_MASK;
3763 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
3764 
3765 	/*
3766 	 * Set PHY control register (0x4140):
3767 	 *    Set full duplex mode
3768 	 *    Set loopback bit
3769 	 *    Clear auto-neg enable bit
3770 	 *    Set PHY speed
3771 	 */
3772 	phy_ctrl = MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000 | MII_CR_LOOPBACK;
3773 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
3774 
3775 	/* Set the link disable bit in the Port Configuration register */
3776 	(void) e1000_read_phy_reg(hw, 0x10, &phy_pconf);
3777 	phy_pconf |= (uint16_t)1 << 14;
3778 	(void) e1000_write_phy_reg(hw, 0x10, phy_pconf);
3779 }
3780 
3781 /*
3782  * igb_set_internal_serdes_loopback - Set the internal SerDes loopback mode
3783  */
3784 static void
3785 igb_set_internal_serdes_loopback(igb_t *igb)
3786 {
3787 	struct e1000_hw *hw;
3788 	uint32_t ctrl_ext;
3789 	uint32_t ctrl;
3790 	uint32_t pcs_lctl;
3791 	uint32_t connsw;
3792 
3793 	hw = &igb->hw;
3794 
3795 	/* Set link mode to SerDes (11b) in the Extended Control register */
3796 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3797 	ctrl_ext |= E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
3798 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
3799 
3800 	/* Configure the SerDes to loopback */
3801 	E1000_WRITE_REG(hw, E1000_SCTL, 0x410);
3802 
3803 	/* Set Device Control register */
3804 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
3805 	ctrl |= (E1000_CTRL_FD |	/* Force full duplex */
3806 	    E1000_CTRL_SLU);		/* Force link up */
3807 	ctrl &= ~(E1000_CTRL_RFCE |	/* Disable receive flow control */
3808 	    E1000_CTRL_TFCE |		/* Disable transmit flow control */
3809 	    E1000_CTRL_LRST);		/* Clear link reset */
3810 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
3811 
3812 	/* Set PCS Link Control register */
3813 	pcs_lctl = E1000_READ_REG(hw, E1000_PCS_LCTL);
3814 	pcs_lctl |= (E1000_PCS_LCTL_FORCE_LINK |
3815 	    E1000_PCS_LCTL_FSD |
3816 	    E1000_PCS_LCTL_FDV_FULL |
3817 	    E1000_PCS_LCTL_FLV_LINK_UP);
3818 	pcs_lctl &= ~E1000_PCS_LCTL_AN_ENABLE;
3819 	E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_lctl);
3820 
3821 	/* Set the Copper/Fiber Switch Control - CONNSW register */
3822 	connsw = E1000_READ_REG(hw, E1000_CONNSW);
3823 	connsw &= ~E1000_CONNSW_ENRGSRC;
3824 	E1000_WRITE_REG(hw, E1000_CONNSW, connsw);
3825 }
3826 
3827 #pragma inline(igb_intr_rx_work)
3828 /*
3829  * igb_intr_rx_work - rx processing of ISR
3830  */
3831 static void
3832 igb_intr_rx_work(igb_rx_ring_t *rx_ring)
3833 {
3834 	mblk_t *mp;
3835 
3836 	mutex_enter(&rx_ring->rx_lock);
3837 	mp = igb_rx(rx_ring, IGB_NO_POLL);
3838 	mutex_exit(&rx_ring->rx_lock);
3839 
3840 	if (mp != NULL)
3841 		mac_rx_ring(rx_ring->igb->mac_hdl, rx_ring->ring_handle, mp,
3842 		    rx_ring->ring_gen_num);
3843 }
3844 
3845 #pragma inline(igb_intr_tx_work)
3846 /*
3847  * igb_intr_tx_work - tx processing of ISR
3848  */
3849 static void
3850 igb_intr_tx_work(igb_tx_ring_t *tx_ring)
3851 {
3852 	igb_t *igb = tx_ring->igb;
3853 
3854 	/* Recycle the tx descriptors */
3855 	tx_ring->tx_recycle(tx_ring);
3856 
3857 	/* Schedule the re-transmit */
3858 	if (tx_ring->reschedule &&
3859 	    (tx_ring->tbd_free >= igb->tx_resched_thresh)) {
3860 		tx_ring->reschedule = B_FALSE;
3861 		mac_tx_ring_update(tx_ring->igb->mac_hdl, tx_ring->ring_handle);
3862 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
3863 	}
3864 }
3865 
3866 #pragma inline(igb_intr_link_work)
3867 /*
3868  * igb_intr_link_work - link-status-change processing of ISR
3869  */
3870 static void
3871 igb_intr_link_work(igb_t *igb)
3872 {
3873 	boolean_t link_changed;
3874 
3875 	igb_stop_watchdog_timer(igb);
3876 
3877 	mutex_enter(&igb->gen_lock);
3878 
3879 	/*
3880 	 * Because we got a link-status-change interrupt, force
3881 	 * e1000_check_for_link() to look at phy
3882 	 */
3883 	igb->hw.mac.get_link_status = B_TRUE;
3884 
3885 	/* igb_link_check takes care of link status change */
3886 	link_changed = igb_link_check(igb);
3887 
3888 	/* Get new phy state */
3889 	igb_get_phy_state(igb);
3890 
3891 	mutex_exit(&igb->gen_lock);
3892 
3893 	if (link_changed)
3894 		mac_link_update(igb->mac_hdl, igb->link_state);
3895 
3896 	igb_start_watchdog_timer(igb);
3897 }
3898 
3899 /*
3900  * igb_intr_legacy - Interrupt handler for legacy interrupts
3901  */
3902 static uint_t
3903 igb_intr_legacy(void *arg1, void *arg2)
3904 {
3905 	igb_t *igb = (igb_t *)arg1;
3906 	igb_tx_ring_t *tx_ring;
3907 	uint32_t icr;
3908 	mblk_t *mp;
3909 	boolean_t tx_reschedule;
3910 	boolean_t link_changed;
3911 	uint_t result;
3912 
3913 	_NOTE(ARGUNUSED(arg2));
3914 
3915 	mutex_enter(&igb->gen_lock);
3916 
3917 	if (igb->igb_state & IGB_SUSPENDED) {
3918 		mutex_exit(&igb->gen_lock);
3919 		return (DDI_INTR_UNCLAIMED);
3920 	}
3921 
3922 	mp = NULL;
3923 	tx_reschedule = B_FALSE;
3924 	link_changed = B_FALSE;
3925 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
3926 
3927 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
3928 		mutex_exit(&igb->gen_lock);
3929 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
3930 		atomic_or_32(&igb->igb_state, IGB_ERROR);
3931 		return (DDI_INTR_UNCLAIMED);
3932 	}
3933 
3934 	if (icr & E1000_ICR_INT_ASSERTED) {
3935 		/*
3936 		 * E1000_ICR_INT_ASSERTED bit was set:
3937 		 * Read(Clear) the ICR, claim this interrupt,
3938 		 * look for work to do.
3939 		 */
3940 		ASSERT(igb->num_rx_rings == 1);
3941 		ASSERT(igb->num_tx_rings == 1);
3942 
3943 		/* Make sure all interrupt causes cleared */
3944 		(void) E1000_READ_REG(&igb->hw, E1000_EICR);
3945 
3946 		if (icr & E1000_ICR_RXT0) {
3947 			mp = igb_rx(&igb->rx_rings[0], IGB_NO_POLL);
3948 		}
3949 
3950 		if (icr & E1000_ICR_TXDW) {
3951 			tx_ring = &igb->tx_rings[0];
3952 
3953 			/* Recycle the tx descriptors */
3954 			tx_ring->tx_recycle(tx_ring);
3955 
3956 			/* Schedule the re-transmit */
3957 			tx_reschedule = (tx_ring->reschedule &&
3958 			    (tx_ring->tbd_free >= igb->tx_resched_thresh));
3959 		}
3960 
3961 		if (icr & E1000_ICR_LSC) {
3962 			/*
3963 			 * Because we got a link-status-change interrupt, force
3964 			 * e1000_check_for_link() to look at phy
3965 			 */
3966 			igb->hw.mac.get_link_status = B_TRUE;
3967 
3968 			/* igb_link_check takes care of link status change */
3969 			link_changed = igb_link_check(igb);
3970 
3971 			/* Get new phy state */
3972 			igb_get_phy_state(igb);
3973 		}
3974 
3975 		if (icr & E1000_ICR_DRSTA) {
3976 			/* 82580 Full Device Reset needed */
3977 			atomic_or_32(&igb->igb_state, IGB_STALL);
3978 		}
3979 
3980 		result = DDI_INTR_CLAIMED;
3981 	} else {
3982 		/*
3983 		 * E1000_ICR_INT_ASSERTED bit was not set:
3984 		 * Don't claim this interrupt.
3985 		 */
3986 		result = DDI_INTR_UNCLAIMED;
3987 	}
3988 
3989 	mutex_exit(&igb->gen_lock);
3990 
3991 	/*
3992 	 * Do the following work outside of the gen_lock
3993 	 */
3994 	if (mp != NULL)
3995 		mac_rx(igb->mac_hdl, NULL, mp);
3996 
3997 	if (tx_reschedule)  {
3998 		tx_ring->reschedule = B_FALSE;
3999 		mac_tx_ring_update(igb->mac_hdl, tx_ring->ring_handle);
4000 		IGB_DEBUG_STAT(tx_ring->stat_reschedule);
4001 	}
4002 
4003 	if (link_changed)
4004 		mac_link_update(igb->mac_hdl, igb->link_state);
4005 
4006 	return (result);
4007 }
4008 
4009 /*
4010  * igb_intr_msi - Interrupt handler for MSI
4011  */
4012 static uint_t
4013 igb_intr_msi(void *arg1, void *arg2)
4014 {
4015 	igb_t *igb = (igb_t *)arg1;
4016 	uint32_t icr;
4017 
4018 	_NOTE(ARGUNUSED(arg2));
4019 
4020 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
4021 
4022 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
4023 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
4024 		atomic_or_32(&igb->igb_state, IGB_ERROR);
4025 		return (DDI_INTR_CLAIMED);
4026 	}
4027 
4028 	/* Make sure all interrupt causes cleared */
4029 	(void) E1000_READ_REG(&igb->hw, E1000_EICR);
4030 
4031 	/*
4032 	 * For MSI interrupt, we have only one vector,
4033 	 * so we have only one rx ring and one tx ring enabled.
4034 	 */
4035 	ASSERT(igb->num_rx_rings == 1);
4036 	ASSERT(igb->num_tx_rings == 1);
4037 
4038 	if (icr & E1000_ICR_RXT0) {
4039 		igb_intr_rx_work(&igb->rx_rings[0]);
4040 	}
4041 
4042 	if (icr & E1000_ICR_TXDW) {
4043 		igb_intr_tx_work(&igb->tx_rings[0]);
4044 	}
4045 
4046 	if (icr & E1000_ICR_LSC) {
4047 		igb_intr_link_work(igb);
4048 	}
4049 
4050 	if (icr & E1000_ICR_DRSTA) {
4051 		/* 82580 Full Device Reset needed */
4052 		atomic_or_32(&igb->igb_state, IGB_STALL);
4053 	}
4054 
4055 	return (DDI_INTR_CLAIMED);
4056 }
4057 
4058 /*
4059  * igb_intr_rx - Interrupt handler for rx
4060  */
4061 static uint_t
4062 igb_intr_rx(void *arg1, void *arg2)
4063 {
4064 	igb_rx_ring_t *rx_ring = (igb_rx_ring_t *)arg1;
4065 
4066 	_NOTE(ARGUNUSED(arg2));
4067 
4068 	/*
4069 	 * Only used via MSI-X vector so don't check cause bits
4070 	 * and only clean the given ring.
4071 	 */
4072 	igb_intr_rx_work(rx_ring);
4073 
4074 	return (DDI_INTR_CLAIMED);
4075 }
4076 
4077 /*
4078  * igb_intr_tx - Interrupt handler for tx
4079  */
4080 static uint_t
4081 igb_intr_tx(void *arg1, void *arg2)
4082 {
4083 	igb_tx_ring_t *tx_ring = (igb_tx_ring_t *)arg1;
4084 
4085 	_NOTE(ARGUNUSED(arg2));
4086 
4087 	/*
4088 	 * Only used via MSI-X vector so don't check cause bits
4089 	 * and only clean the given ring.
4090 	 */
4091 	igb_intr_tx_work(tx_ring);
4092 
4093 	return (DDI_INTR_CLAIMED);
4094 }
4095 
4096 /*
4097  * igb_intr_tx_other - Interrupt handler for both tx and other
4098  *
4099  */
4100 static uint_t
4101 igb_intr_tx_other(void *arg1, void *arg2)
4102 {
4103 	igb_t *igb = (igb_t *)arg1;
4104 	uint32_t icr;
4105 
4106 	_NOTE(ARGUNUSED(arg2));
4107 
4108 	icr = E1000_READ_REG(&igb->hw, E1000_ICR);
4109 
4110 	if (igb_check_acc_handle(igb->osdep.reg_handle) != DDI_FM_OK) {
4111 		ddi_fm_service_impact(igb->dip, DDI_SERVICE_DEGRADED);
4112 		atomic_or_32(&igb->igb_state, IGB_ERROR);
4113 		return (DDI_INTR_CLAIMED);
4114 	}
4115 
4116 	/*
4117 	 * Look for tx reclaiming work first. Remember, in the
4118 	 * case of only interrupt sharing, only one tx ring is
4119 	 * used
4120 	 */
4121 	igb_intr_tx_work(&igb->tx_rings[0]);
4122 
4123 	/*
4124 	 * Check for "other" causes.
4125 	 */
4126 	if (icr & E1000_ICR_LSC) {
4127 		igb_intr_link_work(igb);
4128 	}
4129 
4130 	/*
4131 	 * The DOUTSYNC bit indicates a tx packet dropped because
4132 	 * DMA engine gets "out of sync". There isn't a real fix
4133 	 * for this. The Intel recommendation is to count the number
4134 	 * of occurrences so user can detect when it is happening.
4135 	 * The issue is non-fatal and there's no recovery action
4136 	 * available.
4137 	 */
4138 	if (icr & E1000_ICR_DOUTSYNC) {
4139 		IGB_STAT(igb->dout_sync);
4140 	}
4141 
4142 	if (icr & E1000_ICR_DRSTA) {
4143 		/* 82580 Full Device Reset needed */
4144 		atomic_or_32(&igb->igb_state, IGB_STALL);
4145 	}
4146 
4147 	return (DDI_INTR_CLAIMED);
4148 }
4149 
4150 /*
4151  * igb_alloc_intrs - Allocate interrupts for the driver
4152  *
4153  * Normal sequence is to try MSI-X; if not sucessful, try MSI;
4154  * if not successful, try Legacy.
4155  * igb->intr_force can be used to force sequence to start with
4156  * any of the 3 types.
4157  * If MSI-X is not used, number of tx/rx rings is forced to 1.
4158  */
4159 static int
4160 igb_alloc_intrs(igb_t *igb)
4161 {
4162 	dev_info_t *devinfo;
4163 	int intr_types;
4164 	int rc;
4165 
4166 	devinfo = igb->dip;
4167 
4168 	/* Get supported interrupt types */
4169 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
4170 
4171 	if (rc != DDI_SUCCESS) {
4172 		igb_log(igb,
4173 		    "Get supported interrupt types failed: %d", rc);
4174 		return (IGB_FAILURE);
4175 	}
4176 	IGB_DEBUGLOG_1(igb, "Supported interrupt types: %x", intr_types);
4177 
4178 	igb->intr_type = 0;
4179 
4180 	/* Install MSI-X interrupts */
4181 	if ((intr_types & DDI_INTR_TYPE_MSIX) &&
4182 	    (igb->intr_force <= IGB_INTR_MSIX)) {
4183 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSIX);
4184 
4185 		if (rc == IGB_SUCCESS)
4186 			return (IGB_SUCCESS);
4187 
4188 		igb_log(igb,
4189 		    "Allocate MSI-X failed, trying MSI interrupts...");
4190 	}
4191 
4192 	/* MSI-X not used, force rings to 1 */
4193 	igb->num_rx_rings = 1;
4194 	igb->num_tx_rings = 1;
4195 	igb_log(igb,
4196 	    "MSI-X not used, force rx and tx queue number to 1");
4197 
4198 	/* Install MSI interrupts */
4199 	if ((intr_types & DDI_INTR_TYPE_MSI) &&
4200 	    (igb->intr_force <= IGB_INTR_MSI)) {
4201 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_MSI);
4202 
4203 		if (rc == IGB_SUCCESS)
4204 			return (IGB_SUCCESS);
4205 
4206 		igb_log(igb,
4207 		    "Allocate MSI failed, trying Legacy interrupts...");
4208 	}
4209 
4210 	/* Install legacy interrupts */
4211 	if (intr_types & DDI_INTR_TYPE_FIXED) {
4212 		rc = igb_alloc_intr_handles(igb, DDI_INTR_TYPE_FIXED);
4213 
4214 		if (rc == IGB_SUCCESS)
4215 			return (IGB_SUCCESS);
4216 
4217 		igb_log(igb,
4218 		    "Allocate Legacy interrupts failed");
4219 	}
4220 
4221 	/* If none of the 3 types succeeded, return failure */
4222 	return (IGB_FAILURE);
4223 }
4224 
4225 /*
4226  * igb_alloc_intr_handles - Allocate interrupt handles.
4227  *
4228  * For legacy and MSI, only 1 handle is needed.  For MSI-X,
4229  * if fewer than 2 handles are available, return failure.
4230  * Upon success, this sets the number of Rx rings to a number that
4231  * matches the handles available for Rx interrupts.
4232  */
4233 static int
4234 igb_alloc_intr_handles(igb_t *igb, int intr_type)
4235 {
4236 	dev_info_t *devinfo;
4237 	int orig, request, count, avail, actual;
4238 	int diff, minimum;
4239 	int rc;
4240 
4241 	devinfo = igb->dip;
4242 
4243 	switch (intr_type) {
4244 	case DDI_INTR_TYPE_FIXED:
4245 		request = 1;	/* Request 1 legacy interrupt handle */
4246 		minimum = 1;
4247 		IGB_DEBUGLOG_0(igb, "interrupt type: legacy");
4248 		break;
4249 
4250 	case DDI_INTR_TYPE_MSI:
4251 		request = 1;	/* Request 1 MSI interrupt handle */
4252 		minimum = 1;
4253 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI");
4254 		break;
4255 
4256 	case DDI_INTR_TYPE_MSIX:
4257 		/*
4258 		 * Number of vectors for the adapter is
4259 		 * # rx rings + # tx rings
4260 		 * One of tx vectors is for tx & other
4261 		 */
4262 		request = igb->num_rx_rings + igb->num_tx_rings;
4263 		orig = request;
4264 		minimum = 2;
4265 		IGB_DEBUGLOG_0(igb, "interrupt type: MSI-X");
4266 		break;
4267 
4268 	default:
4269 		igb_log(igb,
4270 		    "invalid call to igb_alloc_intr_handles(): %d\n",
4271 		    intr_type);
4272 		return (IGB_FAILURE);
4273 	}
4274 	IGB_DEBUGLOG_2(igb, "interrupt handles requested: %d  minimum: %d",
4275 	    request, minimum);
4276 
4277 	/*
4278 	 * Get number of supported interrupts
4279 	 */
4280 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
4281 	if ((rc != DDI_SUCCESS) || (count < minimum)) {
4282 		igb_log(igb,
4283 		    "Get supported interrupt number failed. "
4284 		    "Return: %d, count: %d", rc, count);
4285 		return (IGB_FAILURE);
4286 	}
4287 	IGB_DEBUGLOG_1(igb, "interrupts supported: %d", count);
4288 
4289 	/*
4290 	 * Get number of available interrupts
4291 	 */
4292 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
4293 	if ((rc != DDI_SUCCESS) || (avail < minimum)) {
4294 		igb_log(igb,
4295 		    "Get available interrupt number failed. "
4296 		    "Return: %d, available: %d", rc, avail);
4297 		return (IGB_FAILURE);
4298 	}
4299 	IGB_DEBUGLOG_1(igb, "interrupts available: %d", avail);
4300 
4301 	if (avail < request) {
4302 		igb_log(igb, "Request %d handles, %d available",
4303 		    request, avail);
4304 		request = avail;
4305 	}
4306 
4307 	actual = 0;
4308 	igb->intr_cnt = 0;
4309 
4310 	/*
4311 	 * Allocate an array of interrupt handles
4312 	 */
4313 	igb->intr_size = request * sizeof (ddi_intr_handle_t);
4314 	igb->htable = kmem_alloc(igb->intr_size, KM_SLEEP);
4315 
4316 	rc = ddi_intr_alloc(devinfo, igb->htable, intr_type, 0,
4317 	    request, &actual, DDI_INTR_ALLOC_NORMAL);
4318 	if (rc != DDI_SUCCESS) {
4319 		igb_log(igb, "Allocate interrupts failed. "
4320 		    "return: %d, request: %d, actual: %d",
4321 		    rc, request, actual);
4322 		goto alloc_handle_fail;
4323 	}
4324 	IGB_DEBUGLOG_1(igb, "interrupts actually allocated: %d", actual);
4325 
4326 	igb->intr_cnt = actual;
4327 
4328 	if (actual < minimum) {
4329 		igb_log(igb, "Insufficient interrupt handles allocated: %d",
4330 		    actual);
4331 		goto alloc_handle_fail;
4332 	}
4333 
4334 	/*
4335 	 * For MSI-X, actual might force us to reduce number of tx & rx rings
4336 	 */
4337 	if ((intr_type == DDI_INTR_TYPE_MSIX) && (orig > actual)) {
4338 		diff = orig - actual;
4339 		if (diff < igb->num_tx_rings) {
4340 			igb_log(igb,
4341 			    "MSI-X vectors force Tx queue number to %d",
4342 			    igb->num_tx_rings - diff);
4343 			igb->num_tx_rings -= diff;
4344 		} else {
4345 			igb_log(igb,
4346 			    "MSI-X vectors force Tx queue number to 1");
4347 			igb->num_tx_rings = 1;
4348 
4349 			igb_log(igb,
4350 			    "MSI-X vectors force Rx queue number to %d",
4351 			    actual - 1);
4352 			igb->num_rx_rings = actual - 1;
4353 		}
4354 	}
4355 
4356 	/*
4357 	 * Get priority for first vector, assume remaining are all the same
4358 	 */
4359 	rc = ddi_intr_get_pri(igb->htable[0], &igb->intr_pri);
4360 	if (rc != DDI_SUCCESS) {
4361 		igb_log(igb,
4362 		    "Get interrupt priority failed: %d", rc);
4363 		goto alloc_handle_fail;
4364 	}
4365 
4366 	rc = ddi_intr_get_cap(igb->htable[0], &igb->intr_cap);
4367 	if (rc != DDI_SUCCESS) {
4368 		igb_log(igb,
4369 		    "Get interrupt cap failed: %d", rc);
4370 		goto alloc_handle_fail;
4371 	}
4372 
4373 	igb->intr_type = intr_type;
4374 
4375 	return (IGB_SUCCESS);
4376 
4377 alloc_handle_fail:
4378 	igb_rem_intrs(igb);
4379 
4380 	return (IGB_FAILURE);
4381 }
4382 
4383 /*
4384  * igb_add_intr_handlers - Add interrupt handlers based on the interrupt type
4385  *
4386  * Before adding the interrupt handlers, the interrupt vectors have
4387  * been allocated, and the rx/tx rings have also been allocated.
4388  */
4389 static int
4390 igb_add_intr_handlers(igb_t *igb)
4391 {
4392 	igb_rx_ring_t *rx_ring;
4393 	igb_tx_ring_t *tx_ring;
4394 	int vector;
4395 	int rc;
4396 	int i;
4397 
4398 	vector = 0;
4399 
4400 	switch (igb->intr_type) {
4401 	case DDI_INTR_TYPE_MSIX:
4402 		/* Add interrupt handler for tx + other */
4403 		tx_ring = &igb->tx_rings[0];
4404 		rc = ddi_intr_add_handler(igb->htable[vector],
4405 		    (ddi_intr_handler_t *)igb_intr_tx_other,
4406 		    (void *)igb, NULL);
4407 
4408 		if (rc != DDI_SUCCESS) {
4409 			igb_log(igb,
4410 			    "Add tx/other interrupt handler failed: %d", rc);
4411 			return (IGB_FAILURE);
4412 		}
4413 		tx_ring->intr_vector = vector;
4414 		vector++;
4415 
4416 		/* Add interrupt handler for each rx ring */
4417 		for (i = 0; i < igb->num_rx_rings; i++) {
4418 			rx_ring = &igb->rx_rings[i];
4419 
4420 			rc = ddi_intr_add_handler(igb->htable[vector],
4421 			    (ddi_intr_handler_t *)igb_intr_rx,
4422 			    (void *)rx_ring, NULL);
4423 
4424 			if (rc != DDI_SUCCESS) {
4425 				igb_log(igb,
4426 				    "Add rx interrupt handler failed. "
4427 				    "return: %d, rx ring: %d", rc, i);
4428 				for (vector--; vector >= 0; vector--) {
4429 					(void) ddi_intr_remove_handler(
4430 					    igb->htable[vector]);
4431 				}
4432 				return (IGB_FAILURE);
4433 			}
4434 
4435 			rx_ring->intr_vector = vector;
4436 
4437 			vector++;
4438 		}
4439 
4440 		/* Add interrupt handler for each tx ring from 2nd ring */
4441 		for (i = 1; i < igb->num_tx_rings; i++) {
4442 			tx_ring = &igb->tx_rings[i];
4443 
4444 			rc = ddi_intr_add_handler(igb->htable[vector],
4445 			    (ddi_intr_handler_t *)igb_intr_tx,
4446 			    (void *)tx_ring, NULL);
4447 
4448 			if (rc != DDI_SUCCESS) {
4449 				igb_log(igb,
4450 				    "Add tx interrupt handler failed. "
4451 				    "return: %d, tx ring: %d", rc, i);
4452 				for (vector--; vector >= 0; vector--) {
4453 					(void) ddi_intr_remove_handler(
4454 					    igb->htable[vector]);
4455 				}
4456 				return (IGB_FAILURE);
4457 			}
4458 
4459 			tx_ring->intr_vector = vector;
4460 
4461 			vector++;
4462 		}
4463 
4464 		break;
4465 
4466 	case DDI_INTR_TYPE_MSI:
4467 		/* Add interrupt handlers for the only vector */
4468 		rc = ddi_intr_add_handler(igb->htable[vector],
4469 		    (ddi_intr_handler_t *)igb_intr_msi,
4470 		    (void *)igb, NULL);
4471 
4472 		if (rc != DDI_SUCCESS) {
4473 			igb_log(igb,
4474 			    "Add MSI interrupt handler failed: %d", rc);
4475 			return (IGB_FAILURE);
4476 		}
4477 
4478 		rx_ring = &igb->rx_rings[0];
4479 		rx_ring->intr_vector = vector;
4480 
4481 		vector++;
4482 		break;
4483 
4484 	case DDI_INTR_TYPE_FIXED:
4485 		/* Add interrupt handlers for the only vector */
4486 		rc = ddi_intr_add_handler(igb->htable[vector],
4487 		    (ddi_intr_handler_t *)igb_intr_legacy,
4488 		    (void *)igb, NULL);
4489 
4490 		if (rc != DDI_SUCCESS) {
4491 			igb_log(igb,
4492 			    "Add legacy interrupt handler failed: %d", rc);
4493 			return (IGB_FAILURE);
4494 		}
4495 
4496 		rx_ring = &igb->rx_rings[0];
4497 		rx_ring->intr_vector = vector;
4498 
4499 		vector++;
4500 		break;
4501 
4502 	default:
4503 		return (IGB_FAILURE);
4504 	}
4505 
4506 	ASSERT(vector == igb->intr_cnt);
4507 
4508 	return (IGB_SUCCESS);
4509 }
4510 
4511 /*
4512  * igb_setup_msix_82575 - setup 82575 adapter to use MSI-X interrupts
4513  *
4514  * For each vector enabled on the adapter, Set the MSIXBM register accordingly
4515  */
4516 static void
4517 igb_setup_msix_82575(igb_t *igb)
4518 {
4519 	uint32_t eims = 0;
4520 	int i, vector;
4521 	struct e1000_hw *hw = &igb->hw;
4522 
4523 	/*
4524 	 * Set vector for tx ring 0 and other causes.
4525 	 * NOTE assumption that it is vector 0.
4526 	 */
4527 	vector = 0;
4528 
4529 	igb->eims_mask = E1000_EICR_TX_QUEUE0 | E1000_EICR_OTHER;
4530 	E1000_WRITE_REG(hw, E1000_MSIXBM(vector), igb->eims_mask);
4531 	vector++;
4532 
4533 	for (i = 0; i < igb->num_rx_rings; i++) {
4534 		/*
4535 		 * Set vector for each rx ring
4536 		 */
4537 		eims = (E1000_EICR_RX_QUEUE0 << i);
4538 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
4539 
4540 		/*
4541 		 * Accumulate bits to enable in
4542 		 * igb_enable_adapter_interrupts_82575()
4543 		 */
4544 		igb->eims_mask |= eims;
4545 
4546 		vector++;
4547 	}
4548 
4549 	for (i = 1; i < igb->num_tx_rings; i++) {
4550 		/*
4551 		 * Set vector for each tx ring from 2nd tx ring
4552 		 */
4553 		eims = (E1000_EICR_TX_QUEUE0 << i);
4554 		E1000_WRITE_REG(hw, E1000_MSIXBM(vector), eims);
4555 
4556 		/*
4557 		 * Accumulate bits to enable in
4558 		 * igb_enable_adapter_interrupts_82575()
4559 		 */
4560 		igb->eims_mask |= eims;
4561 
4562 		vector++;
4563 	}
4564 
4565 	ASSERT(vector == igb->intr_cnt);
4566 
4567 	/*
4568 	 * Disable IAM for ICR interrupt bits
4569 	 */
4570 	E1000_WRITE_REG(hw, E1000_IAM, 0);
4571 	E1000_WRITE_FLUSH(hw);
4572 }
4573 
4574 /*
4575  * igb_setup_msix_82576 - setup 82576 adapter to use MSI-X interrupts
4576  *
4577  * 82576 uses a table based method for assigning vectors.  Each queue has a
4578  * single entry in the table to which we write a vector number along with a
4579  * "valid" bit.  The entry is a single byte in a 4-byte register.  Vectors
4580  * take a different position in the 4-byte register depending on whether
4581  * they are numbered above or below 8.
4582  */
4583 static void
4584 igb_setup_msix_82576(igb_t *igb)
4585 {
4586 	struct e1000_hw *hw = &igb->hw;
4587 	uint32_t ivar, index, vector;
4588 	int i;
4589 
4590 	/* must enable msi-x capability before IVAR settings */
4591 	E1000_WRITE_REG(hw, E1000_GPIE,
4592 	    (E1000_GPIE_MSIX_MODE | E1000_GPIE_PBA | E1000_GPIE_NSICR));
4593 
4594 	/*
4595 	 * Set vector for tx ring 0 and other causes.
4596 	 * NOTE assumption that it is vector 0.
4597 	 * This is also interdependent with installation of interrupt service
4598 	 * routines in igb_add_intr_handlers().
4599 	 */
4600 
4601 	/* assign "other" causes to vector 0 */
4602 	vector = 0;
4603 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4604 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4605 
4606 	/* assign tx ring 0 to vector 0 */
4607 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4608 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
4609 
4610 	/* prepare to enable tx & other interrupt causes */
4611 	igb->eims_mask = (1 << vector);
4612 
4613 	vector ++;
4614 	for (i = 0; i < igb->num_rx_rings; i++) {
4615 		/*
4616 		 * Set vector for each rx ring
4617 		 */
4618 		index = (i & 0x7);
4619 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4620 
4621 		if (i < 8) {
4622 			/* vector goes into low byte of register */
4623 			ivar = ivar & 0xFFFFFF00;
4624 			ivar |= (vector | E1000_IVAR_VALID);
4625 		} else {
4626 			/* vector goes into third byte of register */
4627 			ivar = ivar & 0xFF00FFFF;
4628 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
4629 		}
4630 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4631 
4632 		/* Accumulate interrupt-cause bits to enable */
4633 		igb->eims_mask |= (1 << vector);
4634 
4635 		vector ++;
4636 	}
4637 
4638 	for (i = 1; i < igb->num_tx_rings; i++) {
4639 		/*
4640 		 * Set vector for each tx ring from 2nd tx ring.
4641 		 * Note assumption that tx vectors numericall follow rx vectors.
4642 		 */
4643 		index = (i & 0x7);
4644 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4645 
4646 		if (i < 8) {
4647 			/* vector goes into second byte of register */
4648 			ivar = ivar & 0xFFFF00FF;
4649 			ivar |= ((vector | E1000_IVAR_VALID) << 8);
4650 		} else {
4651 			/* vector goes into fourth byte of register */
4652 			ivar = ivar & 0x00FFFFFF;
4653 			ivar |= (vector | E1000_IVAR_VALID) << 24;
4654 		}
4655 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4656 
4657 		/* Accumulate interrupt-cause bits to enable */
4658 		igb->eims_mask |= (1 << vector);
4659 
4660 		vector ++;
4661 	}
4662 
4663 	ASSERT(vector == igb->intr_cnt);
4664 }
4665 
4666 /*
4667  * igb_setup_msix_82580 - setup 82580 adapter to use MSI-X interrupts
4668  *
4669  * 82580 uses same table approach at 82576 but has fewer entries.  Each
4670  * queue has a single entry in the table to which we write a vector number
4671  * along with a "valid" bit.  Vectors take a different position in the
4672  * register depending on * whether * they are numbered above or below 4.
4673  */
4674 static void
4675 igb_setup_msix_82580(igb_t *igb)
4676 {
4677 	struct e1000_hw *hw = &igb->hw;
4678 	uint32_t ivar, index, vector;
4679 	int i;
4680 
4681 	/* must enable msi-x capability before IVAR settings */
4682 	E1000_WRITE_REG(hw, E1000_GPIE, (E1000_GPIE_MSIX_MODE |
4683 	    E1000_GPIE_PBA | E1000_GPIE_NSICR | E1000_GPIE_EIAME));
4684 	/*
4685 	 * Set vector for tx ring 0 and other causes.
4686 	 * NOTE assumption that it is vector 0.
4687 	 * This is also interdependent with installation of interrupt service
4688 	 * routines in igb_add_intr_handlers().
4689 	 */
4690 
4691 	/* assign "other" causes to vector 0 */
4692 	vector = 0;
4693 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4694 	E1000_WRITE_REG(hw, E1000_IVAR_MISC, ivar);
4695 
4696 	/* assign tx ring 0 to vector 0 */
4697 	ivar = ((vector | E1000_IVAR_VALID) << 8);
4698 	E1000_WRITE_REG(hw, E1000_IVAR0, ivar);
4699 
4700 	/* prepare to enable tx & other interrupt causes */
4701 	igb->eims_mask = (1 << vector);
4702 
4703 	vector ++;
4704 
4705 	for (i = 0; i < igb->num_rx_rings; i++) {
4706 		/*
4707 		 * Set vector for each rx ring
4708 		 */
4709 		index = (i >> 1);
4710 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4711 
4712 		if (i & 1) {
4713 			/* vector goes into third byte of register */
4714 			ivar = ivar & 0xFF00FFFF;
4715 			ivar |= ((vector | E1000_IVAR_VALID) << 16);
4716 		} else {
4717 			/* vector goes into low byte of register */
4718 			ivar = ivar & 0xFFFFFF00;
4719 			ivar |= (vector | E1000_IVAR_VALID);
4720 		}
4721 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4722 
4723 		/* Accumulate interrupt-cause bits to enable */
4724 		igb->eims_mask |= (1 << vector);
4725 
4726 		vector ++;
4727 	}
4728 
4729 	for (i = 1; i < igb->num_tx_rings; i++) {
4730 		/*
4731 		 * Set vector for each tx ring from 2nd tx ring.
4732 		 * Note assumption that tx vectors numericall follow rx vectors.
4733 		 */
4734 		index = (i >> 1);
4735 		ivar = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
4736 
4737 		if (i & 1) {
4738 			/* vector goes into high byte of register */
4739 			ivar = ivar & 0x00FFFFFF;
4740 			ivar |= ((vector | E1000_IVAR_VALID) << 24);
4741 		} else {
4742 			/* vector goes into second byte of register */
4743 			ivar = ivar & 0xFFFF00FF;
4744 			ivar |= (vector | E1000_IVAR_VALID) << 8;
4745 		}
4746 		E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, ivar);
4747 
4748 		/* Accumulate interrupt-cause bits to enable */
4749 		igb->eims_mask |= (1 << vector);
4750 
4751 		vector ++;
4752 	}
4753 	ASSERT(vector == igb->intr_cnt);
4754 }
4755 
4756 /*
4757  * igb_rem_intr_handlers - remove the interrupt handlers
4758  */
4759 static void
4760 igb_rem_intr_handlers(igb_t *igb)
4761 {
4762 	int i;
4763 	int rc;
4764 
4765 	for (i = 0; i < igb->intr_cnt; i++) {
4766 		rc = ddi_intr_remove_handler(igb->htable[i]);
4767 		if (rc != DDI_SUCCESS) {
4768 			IGB_DEBUGLOG_1(igb,
4769 			    "Remove intr handler failed: %d", rc);
4770 		}
4771 	}
4772 }
4773 
4774 /*
4775  * igb_rem_intrs - remove the allocated interrupts
4776  */
4777 static void
4778 igb_rem_intrs(igb_t *igb)
4779 {
4780 	int i;
4781 	int rc;
4782 
4783 	for (i = 0; i < igb->intr_cnt; i++) {
4784 		rc = ddi_intr_free(igb->htable[i]);
4785 		if (rc != DDI_SUCCESS) {
4786 			IGB_DEBUGLOG_1(igb,
4787 			    "Free intr failed: %d", rc);
4788 		}
4789 	}
4790 
4791 	kmem_free(igb->htable, igb->intr_size);
4792 	igb->htable = NULL;
4793 }
4794 
4795 /*
4796  * igb_enable_intrs - enable all the ddi interrupts
4797  */
4798 static int
4799 igb_enable_intrs(igb_t *igb)
4800 {
4801 	int i;
4802 	int rc;
4803 
4804 	/* Enable interrupts */
4805 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
4806 		/* Call ddi_intr_block_enable() for MSI */
4807 		rc = ddi_intr_block_enable(igb->htable, igb->intr_cnt);
4808 		if (rc != DDI_SUCCESS) {
4809 			igb_log(igb,
4810 			    "Enable block intr failed: %d", rc);
4811 			return (IGB_FAILURE);
4812 		}
4813 	} else {
4814 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
4815 		for (i = 0; i < igb->intr_cnt; i++) {
4816 			rc = ddi_intr_enable(igb->htable[i]);
4817 			if (rc != DDI_SUCCESS) {
4818 				igb_log(igb,
4819 				    "Enable intr failed: %d", rc);
4820 				return (IGB_FAILURE);
4821 			}
4822 		}
4823 	}
4824 
4825 	return (IGB_SUCCESS);
4826 }
4827 
4828 /*
4829  * igb_disable_intrs - disable all the ddi interrupts
4830  */
4831 static int
4832 igb_disable_intrs(igb_t *igb)
4833 {
4834 	int i;
4835 	int rc;
4836 
4837 	/* Disable all interrupts */
4838 	if (igb->intr_cap & DDI_INTR_FLAG_BLOCK) {
4839 		rc = ddi_intr_block_disable(igb->htable, igb->intr_cnt);
4840 		if (rc != DDI_SUCCESS) {
4841 			igb_log(igb,
4842 			    "Disable block intr failed: %d", rc);
4843 			return (IGB_FAILURE);
4844 		}
4845 	} else {
4846 		for (i = 0; i < igb->intr_cnt; i++) {
4847 			rc = ddi_intr_disable(igb->htable[i]);
4848 			if (rc != DDI_SUCCESS) {
4849 				igb_log(igb,
4850 				    "Disable intr failed: %d", rc);
4851 				return (IGB_FAILURE);
4852 			}
4853 		}
4854 	}
4855 
4856 	return (IGB_SUCCESS);
4857 }
4858 
4859 /*
4860  * igb_get_phy_state - Get and save the parameters read from PHY registers
4861  */
4862 static void
4863 igb_get_phy_state(igb_t *igb)
4864 {
4865 	struct e1000_hw *hw = &igb->hw;
4866 	uint16_t phy_ctrl;
4867 	uint16_t phy_status;
4868 	uint16_t phy_an_adv;
4869 	uint16_t phy_an_exp;
4870 	uint16_t phy_ext_status;
4871 	uint16_t phy_1000t_ctrl;
4872 	uint16_t phy_1000t_status;
4873 	uint16_t phy_lp_able;
4874 
4875 	ASSERT(mutex_owned(&igb->gen_lock));
4876 
4877 	if (hw->phy.media_type == e1000_media_type_copper) {
4878 		(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
4879 		(void) e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
4880 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &phy_an_adv);
4881 		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_an_exp);
4882 		(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
4883 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_1000t_ctrl);
4884 		(void) e1000_read_phy_reg(hw,
4885 		    PHY_1000T_STATUS, &phy_1000t_status);
4886 		(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_lp_able);
4887 
4888 		igb->param_autoneg_cap =
4889 		    (phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
4890 		igb->param_pause_cap =
4891 		    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
4892 		igb->param_asym_pause_cap =
4893 		    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
4894 		igb->param_1000fdx_cap =
4895 		    ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
4896 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
4897 		igb->param_1000hdx_cap =
4898 		    ((phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
4899 		    (phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
4900 		igb->param_100t4_cap =
4901 		    (phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
4902 		igb->param_100fdx_cap = ((phy_status & MII_SR_100X_FD_CAPS) ||
4903 		    (phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
4904 		igb->param_100hdx_cap = ((phy_status & MII_SR_100X_HD_CAPS) ||
4905 		    (phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
4906 		igb->param_10fdx_cap =
4907 		    (phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
4908 		igb->param_10hdx_cap =
4909 		    (phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
4910 		igb->param_rem_fault =
4911 		    (phy_status & MII_SR_REMOTE_FAULT) ? 1 : 0;
4912 
4913 		igb->param_adv_autoneg_cap = hw->mac.autoneg;
4914 		igb->param_adv_pause_cap =
4915 		    (phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
4916 		igb->param_adv_asym_pause_cap =
4917 		    (phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
4918 		igb->param_adv_1000hdx_cap =
4919 		    (phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
4920 		igb->param_adv_100t4_cap =
4921 		    (phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
4922 		igb->param_adv_rem_fault =
4923 		    (phy_an_adv & NWAY_AR_REMOTE_FAULT) ? 1 : 0;
4924 		if (igb->param_adv_autoneg_cap == 1) {
4925 			igb->param_adv_1000fdx_cap =
4926 			    (phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
4927 			igb->param_adv_100fdx_cap =
4928 			    (phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
4929 			igb->param_adv_100hdx_cap =
4930 			    (phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
4931 			igb->param_adv_10fdx_cap =
4932 			    (phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
4933 			igb->param_adv_10hdx_cap =
4934 			    (phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
4935 		}
4936 
4937 		igb->param_lp_autoneg_cap =
4938 		    (phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
4939 		igb->param_lp_pause_cap =
4940 		    (phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
4941 		igb->param_lp_asym_pause_cap =
4942 		    (phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
4943 		igb->param_lp_1000fdx_cap =
4944 		    (phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
4945 		igb->param_lp_1000hdx_cap =
4946 		    (phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
4947 		igb->param_lp_100t4_cap =
4948 		    (phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
4949 		igb->param_lp_100fdx_cap =
4950 		    (phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
4951 		igb->param_lp_100hdx_cap =
4952 		    (phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
4953 		igb->param_lp_10fdx_cap =
4954 		    (phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
4955 		igb->param_lp_10hdx_cap =
4956 		    (phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
4957 		igb->param_lp_rem_fault =
4958 		    (phy_lp_able & NWAY_LPAR_REMOTE_FAULT) ? 1 : 0;
4959 	} else {
4960 		/*
4961 		 * 1Gig Fiber adapter only offers 1Gig Full Duplex.
4962 		 */
4963 		igb->param_autoneg_cap = 0;
4964 		igb->param_pause_cap = 1;
4965 		igb->param_asym_pause_cap = 1;
4966 		igb->param_1000fdx_cap = 1;
4967 		igb->param_1000hdx_cap = 0;
4968 		igb->param_100t4_cap = 0;
4969 		igb->param_100fdx_cap = 0;
4970 		igb->param_100hdx_cap = 0;
4971 		igb->param_10fdx_cap = 0;
4972 		igb->param_10hdx_cap = 0;
4973 
4974 		igb->param_adv_autoneg_cap = 0;
4975 		igb->param_adv_pause_cap = 1;
4976 		igb->param_adv_asym_pause_cap = 1;
4977 		igb->param_adv_1000fdx_cap = 1;
4978 		igb->param_adv_1000hdx_cap = 0;
4979 		igb->param_adv_100t4_cap = 0;
4980 		igb->param_adv_100fdx_cap = 0;
4981 		igb->param_adv_100hdx_cap = 0;
4982 		igb->param_adv_10fdx_cap = 0;
4983 		igb->param_adv_10hdx_cap = 0;
4984 
4985 		igb->param_lp_autoneg_cap = 0;
4986 		igb->param_lp_pause_cap = 0;
4987 		igb->param_lp_asym_pause_cap = 0;
4988 		igb->param_lp_1000fdx_cap = 0;
4989 		igb->param_lp_1000hdx_cap = 0;
4990 		igb->param_lp_100t4_cap = 0;
4991 		igb->param_lp_100fdx_cap = 0;
4992 		igb->param_lp_100hdx_cap = 0;
4993 		igb->param_lp_10fdx_cap = 0;
4994 		igb->param_lp_10hdx_cap = 0;
4995 		igb->param_lp_rem_fault = 0;
4996 	}
4997 }
4998 
4999 /*
5000  * synchronize the adv* and en* parameters.
5001  *
5002  * See comments in <sys/dld.h> for details of the *_en_*
5003  * parameters. The usage of ndd for setting adv parameters will
5004  * synchronize all the en parameters with the e1000g parameters,
5005  * implicitly disabling any settings made via dladm.
5006  */
5007 static void
5008 igb_param_sync(igb_t *igb)
5009 {
5010 	igb->param_en_1000fdx_cap = igb->param_adv_1000fdx_cap;
5011 	igb->param_en_1000hdx_cap = igb->param_adv_1000hdx_cap;
5012 	igb->param_en_100t4_cap = igb->param_adv_100t4_cap;
5013 	igb->param_en_100fdx_cap = igb->param_adv_100fdx_cap;
5014 	igb->param_en_100hdx_cap = igb->param_adv_100hdx_cap;
5015 	igb->param_en_10fdx_cap = igb->param_adv_10fdx_cap;
5016 	igb->param_en_10hdx_cap = igb->param_adv_10hdx_cap;
5017 }
5018 
5019 /*
5020  * igb_get_driver_control
5021  */
5022 static void
5023 igb_get_driver_control(struct e1000_hw *hw)
5024 {
5025 	uint32_t ctrl_ext;
5026 
5027 	/* Notify firmware that driver is in control of device */
5028 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
5029 	ctrl_ext |= E1000_CTRL_EXT_DRV_LOAD;
5030 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
5031 }
5032 
5033 /*
5034  * igb_release_driver_control
5035  */
5036 static void
5037 igb_release_driver_control(struct e1000_hw *hw)
5038 {
5039 	uint32_t ctrl_ext;
5040 
5041 	/* Notify firmware that driver is no longer in control of device */
5042 	ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
5043 	ctrl_ext &= ~E1000_CTRL_EXT_DRV_LOAD;
5044 	E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
5045 }
5046 
5047 /*
5048  * igb_atomic_reserve - Atomic decrease operation
5049  */
5050 int
5051 igb_atomic_reserve(uint32_t *count_p, uint32_t n)
5052 {
5053 	uint32_t oldval;
5054 	uint32_t newval;
5055 
5056 	/* ATOMICALLY */
5057 	do {
5058 		oldval = *count_p;
5059 		if (oldval < n)
5060 			return (-1);
5061 		newval = oldval - n;
5062 	} while (atomic_cas_32(count_p, oldval, newval) != oldval);
5063 
5064 	return (newval);
5065 }
5066 
5067 /*
5068  * FMA support
5069  */
5070 
5071 int
5072 igb_check_acc_handle(ddi_acc_handle_t handle)
5073 {
5074 	ddi_fm_error_t de;
5075 
5076 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
5077 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
5078 	return (de.fme_status);
5079 }
5080 
5081 int
5082 igb_check_dma_handle(ddi_dma_handle_t handle)
5083 {
5084 	ddi_fm_error_t de;
5085 
5086 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
5087 	return (de.fme_status);
5088 }
5089 
5090 /*
5091  * The IO fault service error handling callback function
5092  */
5093 /*ARGSUSED*/
5094 static int
5095 igb_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
5096 {
5097 	/*
5098 	 * as the driver can always deal with an error in any dma or
5099 	 * access handle, we can just return the fme_status value.
5100 	 */
5101 	pci_ereport_post(dip, err, NULL);
5102 	return (err->fme_status);
5103 }
5104 
5105 static void
5106 igb_fm_init(igb_t *igb)
5107 {
5108 	ddi_iblock_cookie_t iblk;
5109 	int fma_dma_flag;
5110 
5111 	/* Only register with IO Fault Services if we have some capability */
5112 	if (igb->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
5113 		igb_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
5114 	} else {
5115 		igb_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
5116 	}
5117 
5118 	if (igb->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
5119 		fma_dma_flag = 1;
5120 	} else {
5121 		fma_dma_flag = 0;
5122 	}
5123 
5124 	(void) igb_set_fma_flags(fma_dma_flag);
5125 
5126 	if (igb->fm_capabilities) {
5127 
5128 		/* Register capabilities with IO Fault Services */
5129 		ddi_fm_init(igb->dip, &igb->fm_capabilities, &iblk);
5130 
5131 		/*
5132 		 * Initialize pci ereport capabilities if ereport capable
5133 		 */
5134 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
5135 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
5136 			pci_ereport_setup(igb->dip);
5137 
5138 		/*
5139 		 * Register error callback if error callback capable
5140 		 */
5141 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
5142 			ddi_fm_handler_register(igb->dip,
5143 			    igb_fm_error_cb, (void*) igb);
5144 	}
5145 }
5146 
5147 static void
5148 igb_fm_fini(igb_t *igb)
5149 {
5150 	/* Only unregister FMA capabilities if we registered some */
5151 	if (igb->fm_capabilities) {
5152 
5153 		/*
5154 		 * Release any resources allocated by pci_ereport_setup()
5155 		 */
5156 		if (DDI_FM_EREPORT_CAP(igb->fm_capabilities) ||
5157 		    DDI_FM_ERRCB_CAP(igb->fm_capabilities))
5158 			pci_ereport_teardown(igb->dip);
5159 
5160 		/*
5161 		 * Un-register error callback if error callback capable
5162 		 */
5163 		if (DDI_FM_ERRCB_CAP(igb->fm_capabilities))
5164 			ddi_fm_handler_unregister(igb->dip);
5165 
5166 		/* Unregister from IO Fault Services */
5167 		ddi_fm_fini(igb->dip);
5168 	}
5169 }
5170 
5171 void
5172 igb_fm_ereport(igb_t *igb, char *detail)
5173 {
5174 	uint64_t ena;
5175 	char buf[FM_MAX_CLASS];
5176 
5177 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
5178 	ena = fm_ena_generate(0, FM_ENA_FMT1);
5179 	if (DDI_FM_EREPORT_CAP(igb->fm_capabilities)) {
5180 		ddi_fm_ereport_post(igb->dip, buf, ena, DDI_NOSLEEP,
5181 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
5182 	}
5183 }
5184