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